123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371 |
- //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -------------*- C++ -*-===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- //
- // This file implements the C++ Declaration portions of the Parser interfaces.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Parse/Parser.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/DeclTemplate.h"
- #include "clang/AST/PrettyDeclStackTrace.h"
- #include "clang/Basic/Attributes.h"
- #include "clang/Basic/CharInfo.h"
- #include "clang/Basic/OperatorKinds.h"
- #include "clang/Basic/TargetInfo.h"
- #include "clang/Parse/ParseDiagnostic.h"
- #include "clang/Parse/RAIIObjectsForParser.h"
- #include "clang/Sema/DeclSpec.h"
- #include "clang/Sema/ParsedTemplate.h"
- #include "clang/Sema/Scope.h"
- #include "llvm/ADT/SmallString.h"
- #include "llvm/Support/TimeProfiler.h"
- using namespace clang;
- /// ParseNamespace - We know that the current token is a namespace keyword. This
- /// may either be a top level namespace or a block-level namespace alias. If
- /// there was an inline keyword, it has already been parsed.
- ///
- /// namespace-definition: [C++: namespace.def]
- /// named-namespace-definition
- /// unnamed-namespace-definition
- /// nested-namespace-definition
- ///
- /// named-namespace-definition:
- /// 'inline'[opt] 'namespace' attributes[opt] identifier '{'
- /// namespace-body '}'
- ///
- /// unnamed-namespace-definition:
- /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
- ///
- /// nested-namespace-definition:
- /// 'namespace' enclosing-namespace-specifier '::' 'inline'[opt]
- /// identifier '{' namespace-body '}'
- ///
- /// enclosing-namespace-specifier:
- /// identifier
- /// enclosing-namespace-specifier '::' 'inline'[opt] identifier
- ///
- /// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
- /// 'namespace' identifier '=' qualified-namespace-specifier ';'
- ///
- Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
- SourceLocation &DeclEnd,
- SourceLocation InlineLoc) {
- assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
- SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
- ObjCDeclContextSwitch ObjCDC(*this);
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteNamespaceDecl(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- SourceLocation IdentLoc;
- IdentifierInfo *Ident = nullptr;
- InnerNamespaceInfoList ExtraNSs;
- SourceLocation FirstNestedInlineLoc;
- ParsedAttributesWithRange attrs(AttrFactory);
- SourceLocation attrLoc;
- if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
- ? diag::warn_cxx14_compat_ns_enum_attribute
- : diag::ext_ns_enum_attribute)
- << 0 /*namespace*/;
- attrLoc = Tok.getLocation();
- ParseCXX11Attributes(attrs);
- }
- if (Tok.is(tok::identifier)) {
- Ident = Tok.getIdentifierInfo();
- IdentLoc = ConsumeToken(); // eat the identifier.
- while (Tok.is(tok::coloncolon) &&
- (NextToken().is(tok::identifier) ||
- (NextToken().is(tok::kw_inline) &&
- GetLookAheadToken(2).is(tok::identifier)))) {
- InnerNamespaceInfo Info;
- Info.NamespaceLoc = ConsumeToken();
- if (Tok.is(tok::kw_inline)) {
- Info.InlineLoc = ConsumeToken();
- if (FirstNestedInlineLoc.isInvalid())
- FirstNestedInlineLoc = Info.InlineLoc;
- }
- Info.Ident = Tok.getIdentifierInfo();
- Info.IdentLoc = ConsumeToken();
- ExtraNSs.push_back(Info);
- }
- }
- // A nested namespace definition cannot have attributes.
- if (!ExtraNSs.empty() && attrLoc.isValid())
- Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
- // Read label attributes, if present.
- if (Tok.is(tok::kw___attribute)) {
- attrLoc = Tok.getLocation();
- ParseGNUAttributes(attrs);
- }
- if (Tok.is(tok::equal)) {
- if (!Ident) {
- Diag(Tok, diag::err_expected) << tok::identifier;
- // Skip to end of the definition and eat the ';'.
- SkipUntil(tok::semi);
- return nullptr;
- }
- if (attrLoc.isValid())
- Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
- if (InlineLoc.isValid())
- Diag(InlineLoc, diag::err_inline_namespace_alias)
- << FixItHint::CreateRemoval(InlineLoc);
- Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
- return Actions.ConvertDeclToDeclGroup(NSAlias);
- }
- BalancedDelimiterTracker T(*this, tok::l_brace);
- if (T.consumeOpen()) {
- if (Ident)
- Diag(Tok, diag::err_expected) << tok::l_brace;
- else
- Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
- return nullptr;
- }
- if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
- getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
- getCurScope()->getFnParent()) {
- Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
- SkipUntil(tok::r_brace);
- return nullptr;
- }
- if (ExtraNSs.empty()) {
- // Normal namespace definition, not a nested-namespace-definition.
- } else if (InlineLoc.isValid()) {
- Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
- } else if (getLangOpts().CPlusPlus2a) {
- Diag(ExtraNSs[0].NamespaceLoc,
- diag::warn_cxx14_compat_nested_namespace_definition);
- if (FirstNestedInlineLoc.isValid())
- Diag(FirstNestedInlineLoc,
- diag::warn_cxx17_compat_inline_nested_namespace_definition);
- } else if (getLangOpts().CPlusPlus17) {
- Diag(ExtraNSs[0].NamespaceLoc,
- diag::warn_cxx14_compat_nested_namespace_definition);
- if (FirstNestedInlineLoc.isValid())
- Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
- } else {
- TentativeParsingAction TPA(*this);
- SkipUntil(tok::r_brace, StopBeforeMatch);
- Token rBraceToken = Tok;
- TPA.Revert();
- if (!rBraceToken.is(tok::r_brace)) {
- Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
- << SourceRange(ExtraNSs.front().NamespaceLoc,
- ExtraNSs.back().IdentLoc);
- } else {
- std::string NamespaceFix;
- for (const auto &ExtraNS : ExtraNSs) {
- NamespaceFix += " { ";
- if (ExtraNS.InlineLoc.isValid())
- NamespaceFix += "inline ";
- NamespaceFix += "namespace ";
- NamespaceFix += ExtraNS.Ident->getName();
- }
- std::string RBraces;
- for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i)
- RBraces += "} ";
- Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
- << FixItHint::CreateReplacement(
- SourceRange(ExtraNSs.front().NamespaceLoc,
- ExtraNSs.back().IdentLoc),
- NamespaceFix)
- << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
- }
- // Warn about nested inline namespaces.
- if (FirstNestedInlineLoc.isValid())
- Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
- }
- // If we're still good, complain about inline namespaces in non-C++0x now.
- if (InlineLoc.isValid())
- Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
- diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
- // Enter a scope for the namespace.
- ParseScope NamespaceScope(this, Scope::DeclScope);
- UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
- Decl *NamespcDecl = Actions.ActOnStartNamespaceDef(
- getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident,
- T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl);
- PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl,
- NamespaceLoc, "parsing namespace");
- // Parse the contents of the namespace. This includes parsing recovery on
- // any improperly nested namespaces.
- ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T);
- // Leave the namespace scope.
- NamespaceScope.Exit();
- DeclEnd = T.getCloseLocation();
- Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
- return Actions.ConvertDeclToDeclGroup(NamespcDecl,
- ImplicitUsingDirectiveDecl);
- }
- /// ParseInnerNamespace - Parse the contents of a namespace.
- void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs,
- unsigned int index, SourceLocation &InlineLoc,
- ParsedAttributes &attrs,
- BalancedDelimiterTracker &Tracker) {
- if (index == InnerNSs.size()) {
- while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
- Tok.isNot(tok::eof)) {
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
- ParseExternalDeclaration(attrs);
- }
- // The caller is what called check -- we are simply calling
- // the close for it.
- Tracker.consumeClose();
- return;
- }
- // Handle a nested namespace definition.
- // FIXME: Preserve the source information through to the AST rather than
- // desugaring it here.
- ParseScope NamespaceScope(this, Scope::DeclScope);
- UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
- Decl *NamespcDecl = Actions.ActOnStartNamespaceDef(
- getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc,
- InnerNSs[index].IdentLoc, InnerNSs[index].Ident,
- Tracker.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl);
- assert(!ImplicitUsingDirectiveDecl &&
- "nested namespace definition cannot define anonymous namespace");
- ParseInnerNamespace(InnerNSs, ++index, InlineLoc, attrs, Tracker);
- NamespaceScope.Exit();
- Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
- }
- /// ParseNamespaceAlias - Parse the part after the '=' in a namespace
- /// alias definition.
- ///
- Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
- SourceLocation AliasLoc,
- IdentifierInfo *Alias,
- SourceLocation &DeclEnd) {
- assert(Tok.is(tok::equal) && "Not equal token");
- ConsumeToken(); // eat the '='.
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- CXXScopeSpec SS;
- // Parse (optional) nested-name-specifier.
- ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false,
- /*MayBePseudoDestructor=*/nullptr,
- /*IsTypename=*/false,
- /*LastII=*/nullptr,
- /*OnlyNamespace=*/true);
- if (Tok.isNot(tok::identifier)) {
- Diag(Tok, diag::err_expected_namespace_name);
- // Skip to end of the definition and eat the ';'.
- SkipUntil(tok::semi);
- return nullptr;
- }
- if (SS.isInvalid()) {
- // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier.
- // Skip to end of the definition and eat the ';'.
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Parse identifier.
- IdentifierInfo *Ident = Tok.getIdentifierInfo();
- SourceLocation IdentLoc = ConsumeToken();
- // Eat the ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
- SkipUntil(tok::semi);
- return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc,
- Alias, SS, IdentLoc, Ident);
- }
- /// ParseLinkage - We know that the current token is a string_literal
- /// and just before that, that extern was seen.
- ///
- /// linkage-specification: [C++ 7.5p2: dcl.link]
- /// 'extern' string-literal '{' declaration-seq[opt] '}'
- /// 'extern' string-literal declaration
- ///
- Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
- assert(isTokenStringLiteral() && "Not a string literal!");
- ExprResult Lang = ParseStringLiteralExpression(false);
- ParseScope LinkageScope(this, Scope::DeclScope);
- Decl *LinkageSpec =
- Lang.isInvalid()
- ? nullptr
- : Actions.ActOnStartLinkageSpecification(
- getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
- Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
- if (Tok.isNot(tok::l_brace)) {
- // Reset the source range in DS, as the leading "extern"
- // does not really belong to the inner declaration ...
- DS.SetRangeStart(SourceLocation());
- DS.SetRangeEnd(SourceLocation());
- // ... but anyway remember that such an "extern" was seen.
- DS.setExternInLinkageSpec(true);
- ParseExternalDeclaration(attrs, &DS);
- return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
- getCurScope(), LinkageSpec, SourceLocation())
- : nullptr;
- }
- DS.abort();
- ProhibitAttributes(attrs);
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- unsigned NestedModules = 0;
- while (true) {
- switch (Tok.getKind()) {
- case tok::annot_module_begin:
- ++NestedModules;
- ParseTopLevelDecl();
- continue;
- case tok::annot_module_end:
- if (!NestedModules)
- break;
- --NestedModules;
- ParseTopLevelDecl();
- continue;
- case tok::annot_module_include:
- ParseTopLevelDecl();
- continue;
- case tok::eof:
- break;
- case tok::r_brace:
- if (!NestedModules)
- break;
- LLVM_FALLTHROUGH;
- default:
- ParsedAttributesWithRange attrs(AttrFactory);
- MaybeParseCXX11Attributes(attrs);
- ParseExternalDeclaration(attrs);
- continue;
- }
- break;
- }
- T.consumeClose();
- return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
- getCurScope(), LinkageSpec, T.getCloseLocation())
- : nullptr;
- }
- /// Parse a C++ Modules TS export-declaration.
- ///
- /// export-declaration:
- /// 'export' declaration
- /// 'export' '{' declaration-seq[opt] '}'
- ///
- Decl *Parser::ParseExportDeclaration() {
- assert(Tok.is(tok::kw_export));
- SourceLocation ExportLoc = ConsumeToken();
- ParseScope ExportScope(this, Scope::DeclScope);
- Decl *ExportDecl = Actions.ActOnStartExportDecl(
- getCurScope(), ExportLoc,
- Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
- if (Tok.isNot(tok::l_brace)) {
- // FIXME: Factor out a ParseExternalDeclarationWithAttrs.
- ParsedAttributesWithRange Attrs(AttrFactory);
- MaybeParseCXX11Attributes(Attrs);
- MaybeParseMicrosoftAttributes(Attrs);
- ParseExternalDeclaration(Attrs);
- return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl,
- SourceLocation());
- }
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- // The Modules TS draft says "An export-declaration shall declare at least one
- // entity", but the intent is that it shall contain at least one declaration.
- if (Tok.is(tok::r_brace) && getLangOpts().ModulesTS) {
- Diag(ExportLoc, diag::err_export_empty)
- << SourceRange(ExportLoc, Tok.getLocation());
- }
- while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
- Tok.isNot(tok::eof)) {
- ParsedAttributesWithRange Attrs(AttrFactory);
- MaybeParseCXX11Attributes(Attrs);
- MaybeParseMicrosoftAttributes(Attrs);
- ParseExternalDeclaration(Attrs);
- }
- T.consumeClose();
- return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl,
- T.getCloseLocation());
- }
- /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
- /// using-directive. Assumes that current token is 'using'.
- Parser::DeclGroupPtrTy
- Parser::ParseUsingDirectiveOrDeclaration(DeclaratorContext Context,
- const ParsedTemplateInfo &TemplateInfo,
- SourceLocation &DeclEnd,
- ParsedAttributesWithRange &attrs) {
- assert(Tok.is(tok::kw_using) && "Not using token");
- ObjCDeclContextSwitch ObjCDC(*this);
- // Eat 'using'.
- SourceLocation UsingLoc = ConsumeToken();
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteUsing(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- // Consume unexpected 'template' keywords.
- while (Tok.is(tok::kw_template)) {
- SourceLocation TemplateLoc = ConsumeToken();
- Diag(TemplateLoc, diag::err_unexpected_template_after_using)
- << FixItHint::CreateRemoval(TemplateLoc);
- }
- // 'using namespace' means this is a using-directive.
- if (Tok.is(tok::kw_namespace)) {
- // Template parameters are always an error here.
- if (TemplateInfo.Kind) {
- SourceRange R = TemplateInfo.getSourceRange();
- Diag(UsingLoc, diag::err_templated_using_directive_declaration)
- << 0 /* directive */ << R << FixItHint::CreateRemoval(R);
- }
- Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
- return Actions.ConvertDeclToDeclGroup(UsingDir);
- }
- // Otherwise, it must be a using-declaration or an alias-declaration.
- // Using declarations can't have attributes.
- ProhibitAttributes(attrs);
- return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
- AS_none);
- }
- /// ParseUsingDirective - Parse C++ using-directive, assumes
- /// that current token is 'namespace' and 'using' was already parsed.
- ///
- /// using-directive: [C++ 7.3.p4: namespace.udir]
- /// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
- /// namespace-name ;
- /// [GNU] using-directive:
- /// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
- /// namespace-name attributes[opt] ;
- ///
- Decl *Parser::ParseUsingDirective(DeclaratorContext Context,
- SourceLocation UsingLoc,
- SourceLocation &DeclEnd,
- ParsedAttributes &attrs) {
- assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
- // Eat 'namespace'.
- SourceLocation NamespcLoc = ConsumeToken();
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteUsingDirective(getCurScope());
- cutOffParsing();
- return nullptr;
- }
- CXXScopeSpec SS;
- // Parse (optional) nested-name-specifier.
- ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false,
- /*MayBePseudoDestructor=*/nullptr,
- /*IsTypename=*/false,
- /*LastII=*/nullptr,
- /*OnlyNamespace=*/true);
- IdentifierInfo *NamespcName = nullptr;
- SourceLocation IdentLoc = SourceLocation();
- // Parse namespace-name.
- if (Tok.isNot(tok::identifier)) {
- Diag(Tok, diag::err_expected_namespace_name);
- // If there was invalid namespace name, skip to end of decl, and eat ';'.
- SkipUntil(tok::semi);
- // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
- return nullptr;
- }
- if (SS.isInvalid()) {
- // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier.
- // Skip to end of the definition and eat the ';'.
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Parse identifier.
- NamespcName = Tok.getIdentifierInfo();
- IdentLoc = ConsumeToken();
- // Parse (optional) attributes (most likely GNU strong-using extension).
- bool GNUAttr = false;
- if (Tok.is(tok::kw___attribute)) {
- GNUAttr = true;
- ParseGNUAttributes(attrs);
- }
- // Eat ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi,
- GNUAttr ? diag::err_expected_semi_after_attribute_list
- : diag::err_expected_semi_after_namespace_name))
- SkipUntil(tok::semi);
- return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
- IdentLoc, NamespcName, attrs);
- }
- /// Parse a using-declarator (or the identifier in a C++11 alias-declaration).
- ///
- /// using-declarator:
- /// 'typename'[opt] nested-name-specifier unqualified-id
- ///
- bool Parser::ParseUsingDeclarator(DeclaratorContext Context,
- UsingDeclarator &D) {
- D.clear();
- // Ignore optional 'typename'.
- // FIXME: This is wrong; we should parse this as a typename-specifier.
- TryConsumeToken(tok::kw_typename, D.TypenameLoc);
- if (Tok.is(tok::kw___super)) {
- Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
- return true;
- }
- // Parse nested-name-specifier.
- IdentifierInfo *LastII = nullptr;
- if (ParseOptionalCXXScopeSpecifier(D.SS, nullptr, /*EnteringContext=*/false,
- /*MayBePseudoDtor=*/nullptr,
- /*IsTypename=*/false,
- /*LastII=*/&LastII))
- return true;
- if (D.SS.isInvalid())
- return true;
- // Parse the unqualified-id. We allow parsing of both constructor and
- // destructor names and allow the action module to diagnose any semantic
- // errors.
- //
- // C++11 [class.qual]p2:
- // [...] in a using-declaration that is a member-declaration, if the name
- // specified after the nested-name-specifier is the same as the identifier
- // or the simple-template-id's template-name in the last component of the
- // nested-name-specifier, the name is [...] considered to name the
- // constructor.
- if (getLangOpts().CPlusPlus11 &&
- Context == DeclaratorContext::MemberContext &&
- Tok.is(tok::identifier) &&
- (NextToken().is(tok::semi) || NextToken().is(tok::comma) ||
- NextToken().is(tok::ellipsis)) &&
- D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
- !D.SS.getScopeRep()->getAsNamespace() &&
- !D.SS.getScopeRep()->getAsNamespaceAlias()) {
- SourceLocation IdLoc = ConsumeToken();
- ParsedType Type =
- Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII);
- D.Name.setConstructorName(Type, IdLoc, IdLoc);
- } else {
- if (ParseUnqualifiedId(
- D.SS, /*EnteringContext=*/false,
- /*AllowDestructorName=*/true,
- /*AllowConstructorName=*/!(Tok.is(tok::identifier) &&
- NextToken().is(tok::equal)),
- /*AllowDeductionGuide=*/false,
- nullptr, nullptr, D.Name))
- return true;
- }
- if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc))
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ?
- diag::warn_cxx17_compat_using_declaration_pack :
- diag::ext_using_declaration_pack);
- return false;
- }
- /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
- /// Assumes that 'using' was already seen.
- ///
- /// using-declaration: [C++ 7.3.p3: namespace.udecl]
- /// 'using' using-declarator-list[opt] ;
- ///
- /// using-declarator-list: [C++1z]
- /// using-declarator '...'[opt]
- /// using-declarator-list ',' using-declarator '...'[opt]
- ///
- /// using-declarator-list: [C++98-14]
- /// using-declarator
- ///
- /// alias-declaration: C++11 [dcl.dcl]p1
- /// 'using' identifier attribute-specifier-seq[opt] = type-id ;
- ///
- Parser::DeclGroupPtrTy
- Parser::ParseUsingDeclaration(DeclaratorContext Context,
- const ParsedTemplateInfo &TemplateInfo,
- SourceLocation UsingLoc, SourceLocation &DeclEnd,
- AccessSpecifier AS) {
- // Check for misplaced attributes before the identifier in an
- // alias-declaration.
- ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
- MaybeParseCXX11Attributes(MisplacedAttrs);
- UsingDeclarator D;
- bool InvalidDeclarator = ParseUsingDeclarator(Context, D);
- ParsedAttributesWithRange Attrs(AttrFactory);
- MaybeParseGNUAttributes(Attrs);
- MaybeParseCXX11Attributes(Attrs);
- // Maybe this is an alias-declaration.
- if (Tok.is(tok::equal)) {
- if (InvalidDeclarator) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- // If we had any misplaced attributes from earlier, this is where they
- // should have been written.
- if (MisplacedAttrs.Range.isValid()) {
- Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
- << FixItHint::CreateInsertionFromRange(
- Tok.getLocation(),
- CharSourceRange::getTokenRange(MisplacedAttrs.Range))
- << FixItHint::CreateRemoval(MisplacedAttrs.Range);
- Attrs.takeAllFrom(MisplacedAttrs);
- }
- Decl *DeclFromDeclSpec = nullptr;
- Decl *AD = ParseAliasDeclarationAfterDeclarator(
- TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec);
- return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
- }
- // C++11 attributes are not allowed on a using-declaration, but GNU ones
- // are.
- ProhibitAttributes(MisplacedAttrs);
- ProhibitAttributes(Attrs);
- // Diagnose an attempt to declare a templated using-declaration.
- // In C++11, alias-declarations can be templates:
- // template <...> using id = type;
- if (TemplateInfo.Kind) {
- SourceRange R = TemplateInfo.getSourceRange();
- Diag(UsingLoc, diag::err_templated_using_directive_declaration)
- << 1 /* declaration */ << R << FixItHint::CreateRemoval(R);
- // Unfortunately, we have to bail out instead of recovering by
- // ignoring the parameters, just in case the nested name specifier
- // depends on the parameters.
- return nullptr;
- }
- SmallVector<Decl *, 8> DeclsInGroup;
- while (true) {
- // Parse (optional) attributes (most likely GNU strong-using extension).
- MaybeParseGNUAttributes(Attrs);
- if (InvalidDeclarator)
- SkipUntil(tok::comma, tok::semi, StopBeforeMatch);
- else {
- // "typename" keyword is allowed for identifiers only,
- // because it may be a type definition.
- if (D.TypenameLoc.isValid() &&
- D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) {
- Diag(D.Name.getSourceRange().getBegin(),
- diag::err_typename_identifiers_only)
- << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc));
- // Proceed parsing, but discard the typename keyword.
- D.TypenameLoc = SourceLocation();
- }
- Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc,
- D.TypenameLoc, D.SS, D.Name,
- D.EllipsisLoc, Attrs);
- if (UD)
- DeclsInGroup.push_back(UD);
- }
- if (!TryConsumeToken(tok::comma))
- break;
- // Parse another using-declarator.
- Attrs.clear();
- InvalidDeclarator = ParseUsingDeclarator(Context, D);
- }
- if (DeclsInGroup.size() > 1)
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ?
- diag::warn_cxx17_compat_multi_using_declaration :
- diag::ext_multi_using_declaration);
- // Eat ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi, diag::err_expected_after,
- !Attrs.empty() ? "attributes list"
- : "using declaration"))
- SkipUntil(tok::semi);
- return Actions.BuildDeclaratorGroup(DeclsInGroup);
- }
- Decl *Parser::ParseAliasDeclarationAfterDeclarator(
- const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc,
- UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS,
- ParsedAttributes &Attrs, Decl **OwnedType) {
- if (ExpectAndConsume(tok::equal)) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
- diag::warn_cxx98_compat_alias_declaration :
- diag::ext_alias_declaration);
- // Type alias templates cannot be specialized.
- int SpecKind = -1;
- if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
- D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId)
- SpecKind = 0;
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
- SpecKind = 1;
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
- SpecKind = 2;
- if (SpecKind != -1) {
- SourceRange Range;
- if (SpecKind == 0)
- Range = SourceRange(D.Name.TemplateId->LAngleLoc,
- D.Name.TemplateId->RAngleLoc);
- else
- Range = TemplateInfo.getSourceRange();
- Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
- << SpecKind << Range;
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Name must be an identifier.
- if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) {
- Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier);
- // No removal fixit: can't recover from this.
- SkipUntil(tok::semi);
- return nullptr;
- } else if (D.TypenameLoc.isValid())
- Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier)
- << FixItHint::CreateRemoval(SourceRange(
- D.TypenameLoc,
- D.SS.isNotEmpty() ? D.SS.getEndLoc() : D.TypenameLoc));
- else if (D.SS.isNotEmpty())
- Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
- << FixItHint::CreateRemoval(D.SS.getRange());
- if (D.EllipsisLoc.isValid())
- Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion)
- << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc));
- Decl *DeclFromDeclSpec = nullptr;
- TypeResult TypeAlias = ParseTypeName(
- nullptr,
- TemplateInfo.Kind ? DeclaratorContext::AliasTemplateContext
- : DeclaratorContext::AliasDeclContext,
- AS, &DeclFromDeclSpec, &Attrs);
- if (OwnedType)
- *OwnedType = DeclFromDeclSpec;
- // Eat ';'.
- DeclEnd = Tok.getLocation();
- if (ExpectAndConsume(tok::semi, diag::err_expected_after,
- !Attrs.empty() ? "attributes list"
- : "alias declaration"))
- SkipUntil(tok::semi);
- TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
- MultiTemplateParamsArg TemplateParamsArg(
- TemplateParams ? TemplateParams->data() : nullptr,
- TemplateParams ? TemplateParams->size() : 0);
- return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
- UsingLoc, D.Name, Attrs, TypeAlias,
- DeclFromDeclSpec);
- }
- /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
- ///
- /// [C++0x] static_assert-declaration:
- /// static_assert ( constant-expression , string-literal ) ;
- ///
- /// [C11] static_assert-declaration:
- /// _Static_assert ( constant-expression , string-literal ) ;
- ///
- Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
- assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
- "Not a static_assert declaration");
- if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
- Diag(Tok, diag::ext_c11_static_assert);
- if (Tok.is(tok::kw_static_assert))
- Diag(Tok, diag::warn_cxx98_compat_static_assert);
- SourceLocation StaticAssertLoc = ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected) << tok::l_paren;
- SkipMalformedDecl();
- return nullptr;
- }
- EnterExpressionEvaluationContext ConstantEvaluated(
- Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
- ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext());
- if (AssertExpr.isInvalid()) {
- SkipMalformedDecl();
- return nullptr;
- }
- ExprResult AssertMessage;
- if (Tok.is(tok::r_paren)) {
- Diag(Tok, getLangOpts().CPlusPlus17
- ? diag::warn_cxx14_compat_static_assert_no_message
- : diag::ext_static_assert_no_message)
- << (getLangOpts().CPlusPlus17
- ? FixItHint()
- : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
- } else {
- if (ExpectAndConsume(tok::comma)) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- if (!isTokenStringLiteral()) {
- Diag(Tok, diag::err_expected_string_literal)
- << /*Source='static_assert'*/1;
- SkipMalformedDecl();
- return nullptr;
- }
- AssertMessage = ParseStringLiteralExpression();
- if (AssertMessage.isInvalid()) {
- SkipMalformedDecl();
- return nullptr;
- }
- }
- T.consumeClose();
- DeclEnd = Tok.getLocation();
- ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
- return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
- AssertExpr.get(),
- AssertMessage.get(),
- T.getCloseLocation());
- }
- /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
- ///
- /// 'decltype' ( expression )
- /// 'decltype' ( 'auto' ) [C++1y]
- ///
- SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
- assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)
- && "Not a decltype specifier");
- ExprResult Result;
- SourceLocation StartLoc = Tok.getLocation();
- SourceLocation EndLoc;
- if (Tok.is(tok::annot_decltype)) {
- Result = getExprAnnotation(Tok);
- EndLoc = Tok.getAnnotationEndLoc();
- ConsumeAnnotationToken();
- if (Result.isInvalid()) {
- DS.SetTypeSpecError();
- return EndLoc;
- }
- } else {
- if (Tok.getIdentifierInfo()->isStr("decltype"))
- Diag(Tok, diag::warn_cxx98_compat_decltype);
- ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.expectAndConsume(diag::err_expected_lparen_after,
- "decltype", tok::r_paren)) {
- DS.SetTypeSpecError();
- return T.getOpenLocation() == Tok.getLocation() ?
- StartLoc : T.getOpenLocation();
- }
- // Check for C++1y 'decltype(auto)'.
- if (Tok.is(tok::kw_auto)) {
- // No need to disambiguate here: an expression can't start with 'auto',
- // because the typename-specifier in a function-style cast operation can't
- // be 'auto'.
- Diag(Tok.getLocation(),
- getLangOpts().CPlusPlus14
- ? diag::warn_cxx11_compat_decltype_auto_type_specifier
- : diag::ext_decltype_auto_type_specifier);
- ConsumeToken();
- } else {
- // Parse the expression
- // C++11 [dcl.type.simple]p4:
- // The operand of the decltype specifier is an unevaluated operand.
- EnterExpressionEvaluationContext Unevaluated(
- Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr,
- Sema::ExpressionEvaluationContextRecord::EK_Decltype);
- Result =
- Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) {
- return E->hasPlaceholderType() ? ExprError() : E;
- });
- if (Result.isInvalid()) {
- DS.SetTypeSpecError();
- if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
- EndLoc = ConsumeParen();
- } else {
- if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
- // Backtrack to get the location of the last token before the semi.
- PP.RevertCachedTokens(2);
- ConsumeToken(); // the semi.
- EndLoc = ConsumeAnyToken();
- assert(Tok.is(tok::semi));
- } else {
- EndLoc = Tok.getLocation();
- }
- }
- return EndLoc;
- }
- Result = Actions.ActOnDecltypeExpression(Result.get());
- }
- // Match the ')'
- T.consumeClose();
- if (T.getCloseLocation().isInvalid()) {
- DS.SetTypeSpecError();
- // FIXME: this should return the location of the last token
- // that was consumed (by "consumeClose()")
- return T.getCloseLocation();
- }
- if (Result.isInvalid()) {
- DS.SetTypeSpecError();
- return T.getCloseLocation();
- }
- EndLoc = T.getCloseLocation();
- }
- assert(!Result.isInvalid());
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
- // Check for duplicate type specifiers (e.g. "int decltype(a)").
- if (Result.get()
- ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
- DiagID, Result.get(), Policy)
- : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
- DiagID, Policy)) {
- Diag(StartLoc, DiagID) << PrevSpec;
- DS.SetTypeSpecError();
- }
- return EndLoc;
- }
- void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
- SourceLocation StartLoc,
- SourceLocation EndLoc) {
- // make sure we have a token we can turn into an annotation token
- if (PP.isBacktrackEnabled())
- PP.RevertCachedTokens(1);
- else
- PP.EnterToken(Tok, /*IsReinject*/true);
- Tok.setKind(tok::annot_decltype);
- setExprAnnotation(Tok,
- DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
- DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
- ExprError());
- Tok.setAnnotationEndLoc(EndLoc);
- Tok.setLocation(StartLoc);
- PP.AnnotateCachedTokens(Tok);
- }
- void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
- assert(Tok.is(tok::kw___underlying_type) &&
- "Not an underlying type specifier");
- SourceLocation StartLoc = ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.expectAndConsume(diag::err_expected_lparen_after,
- "__underlying_type", tok::r_paren)) {
- return;
- }
- TypeResult Result = ParseTypeName();
- if (Result.isInvalid()) {
- SkipUntil(tok::r_paren, StopAtSemi);
- return;
- }
- // Match the ')'
- T.consumeClose();
- if (T.getCloseLocation().isInvalid())
- return;
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
- DiagID, Result.get(),
- Actions.getASTContext().getPrintingPolicy()))
- Diag(StartLoc, DiagID) << PrevSpec;
- DS.setTypeofParensRange(T.getRange());
- }
- /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
- /// class name or decltype-specifier. Note that we only check that the result
- /// names a type; semantic analysis will need to verify that the type names a
- /// class. The result is either a type or null, depending on whether a type
- /// name was found.
- ///
- /// base-type-specifier: [C++11 class.derived]
- /// class-or-decltype
- /// class-or-decltype: [C++11 class.derived]
- /// nested-name-specifier[opt] class-name
- /// decltype-specifier
- /// class-name: [C++ class.name]
- /// identifier
- /// simple-template-id
- ///
- /// In C++98, instead of base-type-specifier, we have:
- ///
- /// ::[opt] nested-name-specifier[opt] class-name
- TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
- SourceLocation &EndLocation) {
- // Ignore attempts to use typename
- if (Tok.is(tok::kw_typename)) {
- Diag(Tok, diag::err_expected_class_name_not_template)
- << FixItHint::CreateRemoval(Tok.getLocation());
- ConsumeToken();
- }
- // Parse optional nested-name-specifier
- CXXScopeSpec SS;
- if (ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false))
- return true;
- BaseLoc = Tok.getLocation();
- // Parse decltype-specifier
- // tok == kw_decltype is just error recovery, it can only happen when SS
- // isn't empty
- if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
- if (SS.isNotEmpty())
- Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
- << FixItHint::CreateRemoval(SS.getRange());
- // Fake up a Declarator to use with ActOnTypeName.
- DeclSpec DS(AttrFactory);
- EndLocation = ParseDecltypeSpecifier(DS);
- Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
- return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
- }
- // Check whether we have a template-id that names a type.
- if (Tok.is(tok::annot_template_id)) {
- TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
- if (TemplateId->Kind == TNK_Type_template ||
- TemplateId->Kind == TNK_Dependent_template_name ||
- TemplateId->Kind == TNK_Undeclared_template) {
- AnnotateTemplateIdTokenAsType(/*IsClassName*/true);
- assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
- ParsedType Type = getTypeAnnotation(Tok);
- EndLocation = Tok.getAnnotationEndLoc();
- ConsumeAnnotationToken();
- if (Type)
- return Type;
- return true;
- }
- // Fall through to produce an error below.
- }
- if (Tok.isNot(tok::identifier)) {
- Diag(Tok, diag::err_expected_class_name);
- return true;
- }
- IdentifierInfo *Id = Tok.getIdentifierInfo();
- SourceLocation IdLoc = ConsumeToken();
- if (Tok.is(tok::less)) {
- // It looks the user intended to write a template-id here, but the
- // template-name was wrong. Try to fix that.
- TemplateNameKind TNK = TNK_Type_template;
- TemplateTy Template;
- if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
- &SS, Template, TNK)) {
- Diag(IdLoc, diag::err_unknown_template_name)
- << Id;
- }
- if (!Template) {
- TemplateArgList TemplateArgs;
- SourceLocation LAngleLoc, RAngleLoc;
- ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs,
- RAngleLoc);
- return true;
- }
- // Form the template name
- UnqualifiedId TemplateName;
- TemplateName.setIdentifier(Id, IdLoc);
- // Parse the full template-id, then turn it into a type.
- if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
- TemplateName))
- return true;
- if (TNK == TNK_Type_template || TNK == TNK_Dependent_template_name)
- AnnotateTemplateIdTokenAsType(/*IsClassName*/true);
- // If we didn't end up with a typename token, there's nothing more we
- // can do.
- if (Tok.isNot(tok::annot_typename))
- return true;
- // Retrieve the type from the annotation token, consume that token, and
- // return.
- EndLocation = Tok.getAnnotationEndLoc();
- ParsedType Type = getTypeAnnotation(Tok);
- ConsumeAnnotationToken();
- return Type;
- }
- // We have an identifier; check whether it is actually a type.
- IdentifierInfo *CorrectedII = nullptr;
- ParsedType Type = Actions.getTypeName(
- *Id, IdLoc, getCurScope(), &SS, /*IsClassName=*/true, false, nullptr,
- /*IsCtorOrDtorName=*/false,
- /*NonTrivialTypeSourceInfo=*/true,
- /*IsClassTemplateDeductionContext*/ false, &CorrectedII);
- if (!Type) {
- Diag(IdLoc, diag::err_expected_class_name);
- return true;
- }
- // Consume the identifier.
- EndLocation = IdLoc;
- // Fake up a Declarator to use with ActOnTypeName.
- DeclSpec DS(AttrFactory);
- DS.SetRangeStart(IdLoc);
- DS.SetRangeEnd(EndLocation);
- DS.getTypeSpecScope() = SS;
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
- Actions.getASTContext().getPrintingPolicy());
- Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
- return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
- }
- void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
- while (Tok.isOneOf(tok::kw___single_inheritance,
- tok::kw___multiple_inheritance,
- tok::kw___virtual_inheritance)) {
- IdentifierInfo *AttrName = Tok.getIdentifierInfo();
- SourceLocation AttrNameLoc = ConsumeToken();
- attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
- ParsedAttr::AS_Keyword);
- }
- }
- /// Determine whether the following tokens are valid after a type-specifier
- /// which could be a standalone declaration. This will conservatively return
- /// true if there's any doubt, and is appropriate for insert-';' fixits.
- bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
- // This switch enumerates the valid "follow" set for type-specifiers.
- switch (Tok.getKind()) {
- default: break;
- case tok::semi: // struct foo {...} ;
- case tok::star: // struct foo {...} * P;
- case tok::amp: // struct foo {...} & R = ...
- case tok::ampamp: // struct foo {...} && R = ...
- case tok::identifier: // struct foo {...} V ;
- case tok::r_paren: //(struct foo {...} ) {4}
- case tok::coloncolon: // struct foo {...} :: a::b;
- case tok::annot_cxxscope: // struct foo {...} a:: b;
- case tok::annot_typename: // struct foo {...} a ::b;
- case tok::annot_template_id: // struct foo {...} a<int> ::b;
- case tok::kw_decltype: // struct foo {...} decltype (a)::b;
- case tok::l_paren: // struct foo {...} ( x);
- case tok::comma: // __builtin_offsetof(struct foo{...} ,
- case tok::kw_operator: // struct foo operator ++() {...}
- case tok::kw___declspec: // struct foo {...} __declspec(...)
- case tok::l_square: // void f(struct f [ 3])
- case tok::ellipsis: // void f(struct f ... [Ns])
- // FIXME: we should emit semantic diagnostic when declaration
- // attribute is in type attribute position.
- case tok::kw___attribute: // struct foo __attribute__((used)) x;
- case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop));
- // struct foo {...} _Pragma(section(...));
- case tok::annot_pragma_ms_pragma:
- // struct foo {...} _Pragma(vtordisp(pop));
- case tok::annot_pragma_ms_vtordisp:
- // struct foo {...} _Pragma(pointers_to_members(...));
- case tok::annot_pragma_ms_pointers_to_members:
- return true;
- case tok::colon:
- return CouldBeBitfield; // enum E { ... } : 2;
- // Microsoft compatibility
- case tok::kw___cdecl: // struct foo {...} __cdecl x;
- case tok::kw___fastcall: // struct foo {...} __fastcall x;
- case tok::kw___stdcall: // struct foo {...} __stdcall x;
- case tok::kw___thiscall: // struct foo {...} __thiscall x;
- case tok::kw___vectorcall: // struct foo {...} __vectorcall x;
- // We will diagnose these calling-convention specifiers on non-function
- // declarations later, so claim they are valid after a type specifier.
- return getLangOpts().MicrosoftExt;
- // Type qualifiers
- case tok::kw_const: // struct foo {...} const x;
- case tok::kw_volatile: // struct foo {...} volatile x;
- case tok::kw_restrict: // struct foo {...} restrict x;
- case tok::kw__Atomic: // struct foo {...} _Atomic x;
- case tok::kw___unaligned: // struct foo {...} __unaligned *x;
- // Function specifiers
- // Note, no 'explicit'. An explicit function must be either a conversion
- // operator or a constructor. Either way, it can't have a return type.
- case tok::kw_inline: // struct foo inline f();
- case tok::kw_virtual: // struct foo virtual f();
- case tok::kw_friend: // struct foo friend f();
- // Storage-class specifiers
- case tok::kw_static: // struct foo {...} static x;
- case tok::kw_extern: // struct foo {...} extern x;
- case tok::kw_typedef: // struct foo {...} typedef x;
- case tok::kw_register: // struct foo {...} register x;
- case tok::kw_auto: // struct foo {...} auto x;
- case tok::kw_mutable: // struct foo {...} mutable x;
- case tok::kw_thread_local: // struct foo {...} thread_local x;
- case tok::kw_constexpr: // struct foo {...} constexpr x;
- // As shown above, type qualifiers and storage class specifiers absolutely
- // can occur after class specifiers according to the grammar. However,
- // almost no one actually writes code like this. If we see one of these,
- // it is much more likely that someone missed a semi colon and the
- // type/storage class specifier we're seeing is part of the *next*
- // intended declaration, as in:
- //
- // struct foo { ... }
- // typedef int X;
- //
- // We'd really like to emit a missing semicolon error instead of emitting
- // an error on the 'int' saying that you can't have two type specifiers in
- // the same declaration of X. Because of this, we look ahead past this
- // token to see if it's a type specifier. If so, we know the code is
- // otherwise invalid, so we can produce the expected semi error.
- if (!isKnownToBeTypeSpecifier(NextToken()))
- return true;
- break;
- case tok::r_brace: // struct bar { struct foo {...} }
- // Missing ';' at end of struct is accepted as an extension in C mode.
- if (!getLangOpts().CPlusPlus)
- return true;
- break;
- case tok::greater:
- // template<class T = class X>
- return getLangOpts().CPlusPlus;
- }
- return false;
- }
- /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
- /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
- /// until we reach the start of a definition or see a token that
- /// cannot start a definition.
- ///
- /// class-specifier: [C++ class]
- /// class-head '{' member-specification[opt] '}'
- /// class-head '{' member-specification[opt] '}' attributes[opt]
- /// class-head:
- /// class-key identifier[opt] base-clause[opt]
- /// class-key nested-name-specifier identifier base-clause[opt]
- /// class-key nested-name-specifier[opt] simple-template-id
- /// base-clause[opt]
- /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
- /// [GNU] class-key attributes[opt] nested-name-specifier
- /// identifier base-clause[opt]
- /// [GNU] class-key attributes[opt] nested-name-specifier[opt]
- /// simple-template-id base-clause[opt]
- /// class-key:
- /// 'class'
- /// 'struct'
- /// 'union'
- ///
- /// elaborated-type-specifier: [C++ dcl.type.elab]
- /// class-key ::[opt] nested-name-specifier[opt] identifier
- /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
- /// simple-template-id
- ///
- /// Note that the C++ class-specifier and elaborated-type-specifier,
- /// together, subsume the C99 struct-or-union-specifier:
- ///
- /// struct-or-union-specifier: [C99 6.7.2.1]
- /// struct-or-union identifier[opt] '{' struct-contents '}'
- /// struct-or-union identifier
- /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
- /// '}' attributes[opt]
- /// [GNU] struct-or-union attributes[opt] identifier
- /// struct-or-union:
- /// 'struct'
- /// 'union'
- void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
- SourceLocation StartLoc, DeclSpec &DS,
- const ParsedTemplateInfo &TemplateInfo,
- AccessSpecifier AS,
- bool EnteringContext, DeclSpecContext DSC,
- ParsedAttributesWithRange &Attributes) {
- DeclSpec::TST TagType;
- if (TagTokKind == tok::kw_struct)
- TagType = DeclSpec::TST_struct;
- else if (TagTokKind == tok::kw___interface)
- TagType = DeclSpec::TST_interface;
- else if (TagTokKind == tok::kw_class)
- TagType = DeclSpec::TST_class;
- else {
- assert(TagTokKind == tok::kw_union && "Not a class specifier");
- TagType = DeclSpec::TST_union;
- }
- if (Tok.is(tok::code_completion)) {
- // Code completion for a struct, class, or union name.
- Actions.CodeCompleteTag(getCurScope(), TagType);
- return cutOffParsing();
- }
- // C++03 [temp.explicit] 14.7.2/8:
- // The usual access checking rules do not apply to names used to specify
- // explicit instantiations.
- //
- // As an extension we do not perform access checking on the names used to
- // specify explicit specializations either. This is important to allow
- // specializing traits classes for private types.
- //
- // Note that we don't suppress if this turns out to be an elaborated
- // type specifier.
- bool shouldDelayDiagsInTag =
- (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
- TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
- SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
- ParsedAttributesWithRange attrs(AttrFactory);
- // If attributes exist after tag, parse them.
- MaybeParseGNUAttributes(attrs);
- MaybeParseMicrosoftDeclSpecs(attrs);
- // Parse inheritance specifiers.
- if (Tok.isOneOf(tok::kw___single_inheritance,
- tok::kw___multiple_inheritance,
- tok::kw___virtual_inheritance))
- ParseMicrosoftInheritanceClassAttributes(attrs);
- // If C++0x attributes exist here, parse them.
- // FIXME: Are we consistent with the ordering of parsing of different
- // styles of attributes?
- MaybeParseCXX11Attributes(attrs);
- // Source location used by FIXIT to insert misplaced
- // C++11 attributes
- SourceLocation AttrFixitLoc = Tok.getLocation();
- if (TagType == DeclSpec::TST_struct &&
- Tok.isNot(tok::identifier) &&
- !Tok.isAnnotation() &&
- Tok.getIdentifierInfo() &&
- Tok.isOneOf(tok::kw___is_abstract,
- tok::kw___is_aggregate,
- tok::kw___is_arithmetic,
- tok::kw___is_array,
- tok::kw___is_assignable,
- tok::kw___is_base_of,
- tok::kw___is_class,
- tok::kw___is_complete_type,
- tok::kw___is_compound,
- tok::kw___is_const,
- tok::kw___is_constructible,
- tok::kw___is_convertible,
- tok::kw___is_convertible_to,
- tok::kw___is_destructible,
- tok::kw___is_empty,
- tok::kw___is_enum,
- tok::kw___is_floating_point,
- tok::kw___is_final,
- tok::kw___is_function,
- tok::kw___is_fundamental,
- tok::kw___is_integral,
- tok::kw___is_interface_class,
- tok::kw___is_literal,
- tok::kw___is_lvalue_expr,
- tok::kw___is_lvalue_reference,
- tok::kw___is_member_function_pointer,
- tok::kw___is_member_object_pointer,
- tok::kw___is_member_pointer,
- tok::kw___is_nothrow_assignable,
- tok::kw___is_nothrow_constructible,
- tok::kw___is_nothrow_destructible,
- tok::kw___is_object,
- tok::kw___is_pod,
- tok::kw___is_pointer,
- tok::kw___is_polymorphic,
- tok::kw___is_reference,
- tok::kw___is_rvalue_expr,
- tok::kw___is_rvalue_reference,
- tok::kw___is_same,
- tok::kw___is_scalar,
- tok::kw___is_sealed,
- tok::kw___is_signed,
- tok::kw___is_standard_layout,
- tok::kw___is_trivial,
- tok::kw___is_trivially_assignable,
- tok::kw___is_trivially_constructible,
- tok::kw___is_trivially_copyable,
- tok::kw___is_union,
- tok::kw___is_unsigned,
- tok::kw___is_void,
- tok::kw___is_volatile))
- // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
- // name of struct templates, but some are keywords in GCC >= 4.3
- // and Clang. Therefore, when we see the token sequence "struct
- // X", make X into a normal identifier rather than a keyword, to
- // allow libstdc++ 4.2 and libc++ to work properly.
- TryKeywordIdentFallback(true);
- struct PreserveAtomicIdentifierInfoRAII {
- PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled)
- : AtomicII(nullptr) {
- if (!Enabled)
- return;
- assert(Tok.is(tok::kw__Atomic));
- AtomicII = Tok.getIdentifierInfo();
- AtomicII->revertTokenIDToIdentifier();
- Tok.setKind(tok::identifier);
- }
- ~PreserveAtomicIdentifierInfoRAII() {
- if (!AtomicII)
- return;
- AtomicII->revertIdentifierToTokenID(tok::kw__Atomic);
- }
- IdentifierInfo *AtomicII;
- };
- // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
- // implementation for VS2013 uses _Atomic as an identifier for one of the
- // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider
- // '_Atomic' to be a keyword. We are careful to undo this so that clang can
- // use '_Atomic' in its own header files.
- bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat &&
- Tok.is(tok::kw__Atomic) &&
- TagType == DeclSpec::TST_struct;
- PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
- Tok, ShouldChangeAtomicToIdentifier);
- // Parse the (optional) nested-name-specifier.
- CXXScopeSpec &SS = DS.getTypeSpecScope();
- if (getLangOpts().CPlusPlus) {
- // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
- // is a base-specifier-list.
- ColonProtectionRAIIObject X(*this);
- CXXScopeSpec Spec;
- bool HasValidSpec = true;
- if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, EnteringContext)) {
- DS.SetTypeSpecError();
- HasValidSpec = false;
- }
- if (Spec.isSet())
- if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
- Diag(Tok, diag::err_expected) << tok::identifier;
- HasValidSpec = false;
- }
- if (HasValidSpec)
- SS = Spec;
- }
- TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
- auto RecoverFromUndeclaredTemplateName = [&](IdentifierInfo *Name,
- SourceLocation NameLoc,
- SourceRange TemplateArgRange,
- bool KnownUndeclared) {
- Diag(NameLoc, diag::err_explicit_spec_non_template)
- << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
- << TagTokKind << Name << TemplateArgRange << KnownUndeclared;
- // Strip off the last template parameter list if it was empty, since
- // we've removed its template argument list.
- if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
- if (TemplateParams->size() > 1) {
- TemplateParams->pop_back();
- } else {
- TemplateParams = nullptr;
- const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind =
- ParsedTemplateInfo::NonTemplate;
- }
- } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
- // Pretend this is just a forward declaration.
- TemplateParams = nullptr;
- const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind =
- ParsedTemplateInfo::NonTemplate;
- const_cast<ParsedTemplateInfo &>(TemplateInfo).TemplateLoc =
- SourceLocation();
- const_cast<ParsedTemplateInfo &>(TemplateInfo).ExternLoc =
- SourceLocation();
- }
- };
- // Parse the (optional) class name or simple-template-id.
- IdentifierInfo *Name = nullptr;
- SourceLocation NameLoc;
- TemplateIdAnnotation *TemplateId = nullptr;
- if (Tok.is(tok::identifier)) {
- Name = Tok.getIdentifierInfo();
- NameLoc = ConsumeToken();
- if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
- // The name was supposed to refer to a template, but didn't.
- // Eat the template argument list and try to continue parsing this as
- // a class (or template thereof).
- TemplateArgList TemplateArgs;
- SourceLocation LAngleLoc, RAngleLoc;
- if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs,
- RAngleLoc)) {
- // We couldn't parse the template argument list at all, so don't
- // try to give any location information for the list.
- LAngleLoc = RAngleLoc = SourceLocation();
- }
- RecoverFromUndeclaredTemplateName(
- Name, NameLoc, SourceRange(LAngleLoc, RAngleLoc), false);
- }
- } else if (Tok.is(tok::annot_template_id)) {
- TemplateId = takeTemplateIdAnnotation(Tok);
- NameLoc = ConsumeAnnotationToken();
- if (TemplateId->Kind == TNK_Undeclared_template) {
- // Try to resolve the template name to a type template.
- Actions.ActOnUndeclaredTypeTemplateName(getCurScope(), TemplateId->Template,
- TemplateId->Kind, NameLoc, Name);
- if (TemplateId->Kind == TNK_Undeclared_template) {
- RecoverFromUndeclaredTemplateName(
- Name, NameLoc,
- SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc), true);
- TemplateId = nullptr;
- }
- }
- if (TemplateId && TemplateId->Kind != TNK_Type_template &&
- TemplateId->Kind != TNK_Dependent_template_name) {
- // The template-name in the simple-template-id refers to
- // something other than a class template. Give an appropriate
- // error message and skip to the ';'.
- SourceRange Range(NameLoc);
- if (SS.isNotEmpty())
- Range.setBegin(SS.getBeginLoc());
- // FIXME: Name may be null here.
- Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
- << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
- DS.SetTypeSpecError();
- SkipUntil(tok::semi, StopBeforeMatch);
- return;
- }
- }
- // There are four options here.
- // - If we are in a trailing return type, this is always just a reference,
- // and we must not try to parse a definition. For instance,
- // [] () -> struct S { };
- // does not define a type.
- // - If we have 'struct foo {...', 'struct foo :...',
- // 'struct foo final :' or 'struct foo final {', then this is a definition.
- // - If we have 'struct foo;', then this is either a forward declaration
- // or a friend declaration, which have to be treated differently.
- // - Otherwise we have something like 'struct foo xyz', a reference.
- //
- // We also detect these erroneous cases to provide better diagnostic for
- // C++11 attributes parsing.
- // - attributes follow class name:
- // struct foo [[]] {};
- // - attributes appear before or after 'final':
- // struct foo [[]] final [[]] {};
- //
- // However, in type-specifier-seq's, things look like declarations but are
- // just references, e.g.
- // new struct s;
- // or
- // &T::operator struct s;
- // For these, DSC is DeclSpecContext::DSC_type_specifier or
- // DeclSpecContext::DSC_alias_declaration.
- // If there are attributes after class name, parse them.
- MaybeParseCXX11Attributes(Attributes);
- const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
- Sema::TagUseKind TUK;
- if (DSC == DeclSpecContext::DSC_trailing)
- TUK = Sema::TUK_Reference;
- else if (Tok.is(tok::l_brace) ||
- (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
- (isCXX11FinalKeyword() &&
- (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
- if (DS.isFriendSpecified()) {
- // C++ [class.friend]p2:
- // A class shall not be defined in a friend declaration.
- Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
- << SourceRange(DS.getFriendSpecLoc());
- // Skip everything up to the semicolon, so that this looks like a proper
- // friend class (or template thereof) declaration.
- SkipUntil(tok::semi, StopBeforeMatch);
- TUK = Sema::TUK_Friend;
- } else {
- // Okay, this is a class definition.
- TUK = Sema::TUK_Definition;
- }
- } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
- NextToken().is(tok::kw_alignas))) {
- // We can't tell if this is a definition or reference
- // until we skipped the 'final' and C++11 attribute specifiers.
- TentativeParsingAction PA(*this);
- // Skip the 'final' keyword.
- ConsumeToken();
- // Skip C++11 attribute specifiers.
- while (true) {
- if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
- ConsumeBracket();
- if (!SkipUntil(tok::r_square, StopAtSemi))
- break;
- } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
- ConsumeToken();
- ConsumeParen();
- if (!SkipUntil(tok::r_paren, StopAtSemi))
- break;
- } else {
- break;
- }
- }
- if (Tok.isOneOf(tok::l_brace, tok::colon))
- TUK = Sema::TUK_Definition;
- else
- TUK = Sema::TUK_Reference;
- PA.Revert();
- } else if (!isTypeSpecifier(DSC) &&
- (Tok.is(tok::semi) ||
- (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
- TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
- if (Tok.isNot(tok::semi)) {
- const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
- // A semicolon was missing after this declaration. Diagnose and recover.
- ExpectAndConsume(tok::semi, diag::err_expected_after,
- DeclSpec::getSpecifierName(TagType, PPol));
- PP.EnterToken(Tok, /*IsReinject*/true);
- Tok.setKind(tok::semi);
- }
- } else
- TUK = Sema::TUK_Reference;
- // Forbid misplaced attributes. In cases of a reference, we pass attributes
- // to caller to handle.
- if (TUK != Sema::TUK_Reference) {
- // If this is not a reference, then the only possible
- // valid place for C++11 attributes to appear here
- // is between class-key and class-name. If there are
- // any attributes after class-name, we try a fixit to move
- // them to the right place.
- SourceRange AttrRange = Attributes.Range;
- if (AttrRange.isValid()) {
- Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
- << AttrRange
- << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
- CharSourceRange(AttrRange, true))
- << FixItHint::CreateRemoval(AttrRange);
- // Recover by adding misplaced attributes to the attribute list
- // of the class so they can be applied on the class later.
- attrs.takeAllFrom(Attributes);
- }
- }
- // If this is an elaborated type specifier, and we delayed
- // diagnostics before, just merge them into the current pool.
- if (shouldDelayDiagsInTag) {
- diagsFromTag.done();
- if (TUK == Sema::TUK_Reference)
- diagsFromTag.redelay();
- }
- if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
- TUK != Sema::TUK_Definition)) {
- if (DS.getTypeSpecType() != DeclSpec::TST_error) {
- // We have a declaration or reference to an anonymous class.
- Diag(StartLoc, diag::err_anon_type_definition)
- << DeclSpec::getSpecifierName(TagType, Policy);
- }
- // If we are parsing a definition and stop at a base-clause, continue on
- // until the semicolon. Continuing from the comma will just trick us into
- // thinking we are seeing a variable declaration.
- if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
- SkipUntil(tok::semi, StopBeforeMatch);
- else
- SkipUntil(tok::comma, StopAtSemi);
- return;
- }
- // Create the tag portion of the class or class template.
- DeclResult TagOrTempResult = true; // invalid
- TypeResult TypeResult = true; // invalid
- bool Owned = false;
- Sema::SkipBodyInfo SkipBody;
- if (TemplateId) {
- // Explicit specialization, class template partial specialization,
- // or explicit instantiation.
- ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
- TemplateId->NumArgs);
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
- TUK == Sema::TUK_Declaration) {
- // This is an explicit instantiation of a class template.
- ProhibitAttributes(attrs);
- TagOrTempResult = Actions.ActOnExplicitInstantiation(
- getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
- TagType, StartLoc, SS, TemplateId->Template,
- TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, TemplateArgsPtr,
- TemplateId->RAngleLoc, attrs);
- // Friend template-ids are treated as references unless
- // they have template headers, in which case they're ill-formed
- // (FIXME: "template <class T> friend class A<T>::B<int>;").
- // We diagnose this error in ActOnClassTemplateSpecialization.
- } else if (TUK == Sema::TUK_Reference ||
- (TUK == Sema::TUK_Friend &&
- TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
- ProhibitAttributes(attrs);
- TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
- TemplateId->SS,
- TemplateId->TemplateKWLoc,
- TemplateId->Template,
- TemplateId->TemplateNameLoc,
- TemplateId->LAngleLoc,
- TemplateArgsPtr,
- TemplateId->RAngleLoc);
- } else {
- // This is an explicit specialization or a class template
- // partial specialization.
- TemplateParameterLists FakedParamLists;
- if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
- // This looks like an explicit instantiation, because we have
- // something like
- //
- // template class Foo<X>
- //
- // but it actually has a definition. Most likely, this was
- // meant to be an explicit specialization, but the user forgot
- // the '<>' after 'template'.
- // It this is friend declaration however, since it cannot have a
- // template header, it is most likely that the user meant to
- // remove the 'template' keyword.
- assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
- "Expected a definition here");
- if (TUK == Sema::TUK_Friend) {
- Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
- TemplateParams = nullptr;
- } else {
- SourceLocation LAngleLoc =
- PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
- Diag(TemplateId->TemplateNameLoc,
- diag::err_explicit_instantiation_with_definition)
- << SourceRange(TemplateInfo.TemplateLoc)
- << FixItHint::CreateInsertion(LAngleLoc, "<>");
- // Create a fake template parameter list that contains only
- // "template<>", so that we treat this construct as a class
- // template specialization.
- FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
- 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
- LAngleLoc, nullptr));
- TemplateParams = &FakedParamLists;
- }
- }
- // Build the class template specialization.
- TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
- getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
- *TemplateId, attrs,
- MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
- : nullptr,
- TemplateParams ? TemplateParams->size() : 0),
- &SkipBody);
- }
- } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
- TUK == Sema::TUK_Declaration) {
- // Explicit instantiation of a member of a class template
- // specialization, e.g.,
- //
- // template struct Outer<int>::Inner;
- //
- ProhibitAttributes(attrs);
- TagOrTempResult = Actions.ActOnExplicitInstantiation(
- getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
- TagType, StartLoc, SS, Name, NameLoc, attrs);
- } else if (TUK == Sema::TUK_Friend &&
- TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
- ProhibitAttributes(attrs);
- TagOrTempResult = Actions.ActOnTemplatedFriendTag(
- getCurScope(), DS.getFriendSpecLoc(), TagType, StartLoc, SS, Name,
- NameLoc, attrs,
- MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr,
- TemplateParams ? TemplateParams->size() : 0));
- } else {
- if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
- ProhibitAttributes(attrs);
- if (TUK == Sema::TUK_Definition &&
- TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
- // If the declarator-id is not a template-id, issue a diagnostic and
- // recover by ignoring the 'template' keyword.
- Diag(Tok, diag::err_template_defn_explicit_instantiation)
- << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
- TemplateParams = nullptr;
- }
- bool IsDependent = false;
- // Don't pass down template parameter lists if this is just a tag
- // reference. For example, we don't need the template parameters here:
- // template <class T> class A *makeA(T t);
- MultiTemplateParamsArg TParams;
- if (TUK != Sema::TUK_Reference && TemplateParams)
- TParams =
- MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
- stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
- // Declaration or definition of a class type
- TagOrTempResult = Actions.ActOnTag(
- getCurScope(), TagType, TUK, StartLoc, SS, Name, NameLoc, attrs, AS,
- DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent,
- SourceLocation(), false, clang::TypeResult(),
- DSC == DeclSpecContext::DSC_type_specifier,
- DSC == DeclSpecContext::DSC_template_param ||
- DSC == DeclSpecContext::DSC_template_type_arg,
- &SkipBody);
- // If ActOnTag said the type was dependent, try again with the
- // less common call.
- if (IsDependent) {
- assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
- TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
- SS, Name, StartLoc, NameLoc);
- }
- }
- // If there is a body, parse it and inform the actions module.
- if (TUK == Sema::TUK_Definition) {
- assert(Tok.is(tok::l_brace) ||
- (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
- isCXX11FinalKeyword());
- if (SkipBody.ShouldSkip)
- SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
- TagOrTempResult.get());
- else if (getLangOpts().CPlusPlus)
- ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
- TagOrTempResult.get());
- else {
- Decl *D =
- SkipBody.CheckSameAsPrevious ? SkipBody.New : TagOrTempResult.get();
- // Parse the definition body.
- ParseStructUnionBody(StartLoc, TagType, D);
- if (SkipBody.CheckSameAsPrevious &&
- !Actions.ActOnDuplicateDefinition(DS, TagOrTempResult.get(),
- SkipBody)) {
- DS.SetTypeSpecError();
- return;
- }
- }
- }
- if (!TagOrTempResult.isInvalid())
- // Delayed processing of attributes.
- Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs);
- const char *PrevSpec = nullptr;
- unsigned DiagID;
- bool Result;
- if (!TypeResult.isInvalid()) {
- Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
- NameLoc.isValid() ? NameLoc : StartLoc,
- PrevSpec, DiagID, TypeResult.get(), Policy);
- } else if (!TagOrTempResult.isInvalid()) {
- Result = DS.SetTypeSpecType(TagType, StartLoc,
- NameLoc.isValid() ? NameLoc : StartLoc,
- PrevSpec, DiagID, TagOrTempResult.get(), Owned,
- Policy);
- } else {
- DS.SetTypeSpecError();
- return;
- }
- if (Result)
- Diag(StartLoc, DiagID) << PrevSpec;
- // At this point, we've successfully parsed a class-specifier in 'definition'
- // form (e.g. "struct foo { int x; }". While we could just return here, we're
- // going to look at what comes after it to improve error recovery. If an
- // impossible token occurs next, we assume that the programmer forgot a ; at
- // the end of the declaration and recover that way.
- //
- // Also enforce C++ [temp]p3:
- // In a template-declaration which defines a class, no declarator
- // is permitted.
- //
- // After a type-specifier, we don't expect a semicolon. This only happens in
- // C, since definitions are not permitted in this context in C++.
- if (TUK == Sema::TUK_Definition &&
- (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
- (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
- if (Tok.isNot(tok::semi)) {
- const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
- ExpectAndConsume(tok::semi, diag::err_expected_after,
- DeclSpec::getSpecifierName(TagType, PPol));
- // Push this token back into the preprocessor and change our current token
- // to ';' so that the rest of the code recovers as though there were an
- // ';' after the definition.
- PP.EnterToken(Tok, /*IsReinject=*/true);
- Tok.setKind(tok::semi);
- }
- }
- }
- /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
- ///
- /// base-clause : [C++ class.derived]
- /// ':' base-specifier-list
- /// base-specifier-list:
- /// base-specifier '...'[opt]
- /// base-specifier-list ',' base-specifier '...'[opt]
- void Parser::ParseBaseClause(Decl *ClassDecl) {
- assert(Tok.is(tok::colon) && "Not a base clause");
- ConsumeToken();
- // Build up an array of parsed base specifiers.
- SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
- while (true) {
- // Parse a base-specifier.
- BaseResult Result = ParseBaseSpecifier(ClassDecl);
- if (Result.isInvalid()) {
- // Skip the rest of this base specifier, up until the comma or
- // opening brace.
- SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
- } else {
- // Add this to our array of base specifiers.
- BaseInfo.push_back(Result.get());
- }
- // If the next token is a comma, consume it and keep reading
- // base-specifiers.
- if (!TryConsumeToken(tok::comma))
- break;
- }
- // Attach the base specifiers
- Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo);
- }
- /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
- /// one entry in the base class list of a class specifier, for example:
- /// class foo : public bar, virtual private baz {
- /// 'public bar' and 'virtual private baz' are each base-specifiers.
- ///
- /// base-specifier: [C++ class.derived]
- /// attribute-specifier-seq[opt] base-type-specifier
- /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
- /// base-type-specifier
- /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
- /// base-type-specifier
- BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
- bool IsVirtual = false;
- SourceLocation StartLoc = Tok.getLocation();
- ParsedAttributesWithRange Attributes(AttrFactory);
- MaybeParseCXX11Attributes(Attributes);
- // Parse the 'virtual' keyword.
- if (TryConsumeToken(tok::kw_virtual))
- IsVirtual = true;
- CheckMisplacedCXX11Attribute(Attributes, StartLoc);
- // Parse an (optional) access specifier.
- AccessSpecifier Access = getAccessSpecifierIfPresent();
- if (Access != AS_none)
- ConsumeToken();
- CheckMisplacedCXX11Attribute(Attributes, StartLoc);
- // Parse the 'virtual' keyword (again!), in case it came after the
- // access specifier.
- if (Tok.is(tok::kw_virtual)) {
- SourceLocation VirtualLoc = ConsumeToken();
- if (IsVirtual) {
- // Complain about duplicate 'virtual'
- Diag(VirtualLoc, diag::err_dup_virtual)
- << FixItHint::CreateRemoval(VirtualLoc);
- }
- IsVirtual = true;
- }
- CheckMisplacedCXX11Attribute(Attributes, StartLoc);
- // Parse the class-name.
- // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
- // implementation for VS2013 uses _Atomic as an identifier for one of the
- // classes in <atomic>. Treat '_Atomic' to be an identifier when we are
- // parsing the class-name for a base specifier.
- if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
- NextToken().is(tok::less))
- Tok.setKind(tok::identifier);
- SourceLocation EndLocation;
- SourceLocation BaseLoc;
- TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
- if (BaseType.isInvalid())
- return true;
- // Parse the optional ellipsis (for a pack expansion). The ellipsis is
- // actually part of the base-specifier-list grammar productions, but we
- // parse it here for convenience.
- SourceLocation EllipsisLoc;
- TryConsumeToken(tok::ellipsis, EllipsisLoc);
- // Find the complete source range for the base-specifier.
- SourceRange Range(StartLoc, EndLocation);
- // Notify semantic analysis that we have parsed a complete
- // base-specifier.
- return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
- Access, BaseType.get(), BaseLoc,
- EllipsisLoc);
- }
- /// getAccessSpecifierIfPresent - Determine whether the next token is
- /// a C++ access-specifier.
- ///
- /// access-specifier: [C++ class.derived]
- /// 'private'
- /// 'protected'
- /// 'public'
- AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
- switch (Tok.getKind()) {
- default: return AS_none;
- case tok::kw_private: return AS_private;
- case tok::kw_protected: return AS_protected;
- case tok::kw_public: return AS_public;
- }
- }
- /// If the given declarator has any parts for which parsing has to be
- /// delayed, e.g., default arguments or an exception-specification, create a
- /// late-parsed method declaration record to handle the parsing at the end of
- /// the class definition.
- void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
- Decl *ThisDecl) {
- DeclaratorChunk::FunctionTypeInfo &FTI
- = DeclaratorInfo.getFunctionTypeInfo();
- // If there was a late-parsed exception-specification, we'll need a
- // late parse
- bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
- if (!NeedLateParse) {
- // Look ahead to see if there are any default args
- for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
- auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
- if (Param->hasUnparsedDefaultArg()) {
- NeedLateParse = true;
- break;
- }
- }
- }
- if (NeedLateParse) {
- // Push this method onto the stack of late-parsed method
- // declarations.
- auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
- getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
- LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
- // Stash the exception-specification tokens in the late-pased method.
- LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
- FTI.ExceptionSpecTokens = nullptr;
- // Push tokens for each parameter. Those that do not have
- // defaults will be NULL.
- LateMethod->DefaultArgs.reserve(FTI.NumParams);
- for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
- LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
- FTI.Params[ParamIdx].Param,
- std::move(FTI.Params[ParamIdx].DefaultArgTokens)));
- }
- }
- /// isCXX11VirtSpecifier - Determine whether the given token is a C++11
- /// virt-specifier.
- ///
- /// virt-specifier:
- /// override
- /// final
- /// __final
- VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
- if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
- return VirtSpecifiers::VS_None;
- IdentifierInfo *II = Tok.getIdentifierInfo();
- // Initialize the contextual keywords.
- if (!Ident_final) {
- Ident_final = &PP.getIdentifierTable().get("final");
- if (getLangOpts().GNUKeywords)
- Ident_GNU_final = &PP.getIdentifierTable().get("__final");
- if (getLangOpts().MicrosoftExt)
- Ident_sealed = &PP.getIdentifierTable().get("sealed");
- Ident_override = &PP.getIdentifierTable().get("override");
- }
- if (II == Ident_override)
- return VirtSpecifiers::VS_Override;
- if (II == Ident_sealed)
- return VirtSpecifiers::VS_Sealed;
- if (II == Ident_final)
- return VirtSpecifiers::VS_Final;
- if (II == Ident_GNU_final)
- return VirtSpecifiers::VS_GNU_Final;
- return VirtSpecifiers::VS_None;
- }
- /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
- ///
- /// virt-specifier-seq:
- /// virt-specifier
- /// virt-specifier-seq virt-specifier
- void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
- bool IsInterface,
- SourceLocation FriendLoc) {
- while (true) {
- VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
- if (Specifier == VirtSpecifiers::VS_None)
- return;
- if (FriendLoc.isValid()) {
- Diag(Tok.getLocation(), diag::err_friend_decl_spec)
- << VirtSpecifiers::getSpecifierName(Specifier)
- << FixItHint::CreateRemoval(Tok.getLocation())
- << SourceRange(FriendLoc, FriendLoc);
- ConsumeToken();
- continue;
- }
- // C++ [class.mem]p8:
- // A virt-specifier-seq shall contain at most one of each virt-specifier.
- const char *PrevSpec = nullptr;
- if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
- Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
- << PrevSpec
- << FixItHint::CreateRemoval(Tok.getLocation());
- if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
- Specifier == VirtSpecifiers::VS_Sealed)) {
- Diag(Tok.getLocation(), diag::err_override_control_interface)
- << VirtSpecifiers::getSpecifierName(Specifier);
- } else if (Specifier == VirtSpecifiers::VS_Sealed) {
- Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
- } else if (Specifier == VirtSpecifiers::VS_GNU_Final) {
- Diag(Tok.getLocation(), diag::ext_warn_gnu_final);
- } else {
- Diag(Tok.getLocation(),
- getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_override_control_keyword
- : diag::ext_override_control_keyword)
- << VirtSpecifiers::getSpecifierName(Specifier);
- }
- ConsumeToken();
- }
- }
- /// isCXX11FinalKeyword - Determine whether the next token is a C++11
- /// 'final' or Microsoft 'sealed' contextual keyword.
- bool Parser::isCXX11FinalKeyword() const {
- VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
- return Specifier == VirtSpecifiers::VS_Final ||
- Specifier == VirtSpecifiers::VS_GNU_Final ||
- Specifier == VirtSpecifiers::VS_Sealed;
- }
- /// Parse a C++ member-declarator up to, but not including, the optional
- /// brace-or-equal-initializer or pure-specifier.
- bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
- Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
- LateParsedAttrList &LateParsedAttrs) {
- // member-declarator:
- // declarator pure-specifier[opt]
- // declarator brace-or-equal-initializer[opt]
- // identifier[opt] ':' constant-expression
- if (Tok.isNot(tok::colon))
- ParseDeclarator(DeclaratorInfo);
- else
- DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
- if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
- assert(DeclaratorInfo.isPastIdentifier() &&
- "don't know where identifier would go yet?");
- BitfieldSize = ParseConstantExpression();
- if (BitfieldSize.isInvalid())
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- } else {
- ParseOptionalCXX11VirtSpecifierSeq(
- VS, getCurrentClass().IsInterface,
- DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
- if (!VS.isUnset())
- MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
- }
- // If a simple-asm-expr is present, parse it.
- if (Tok.is(tok::kw_asm)) {
- SourceLocation Loc;
- ExprResult AsmLabel(ParseSimpleAsm(&Loc));
- if (AsmLabel.isInvalid())
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- DeclaratorInfo.setAsmLabel(AsmLabel.get());
- DeclaratorInfo.SetRangeEnd(Loc);
- }
- // If attributes exist after the declarator, but before an '{', parse them.
- MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
- // For compatibility with code written to older Clang, also accept a
- // virt-specifier *after* the GNU attributes.
- if (BitfieldSize.isUnset() && VS.isUnset()) {
- ParseOptionalCXX11VirtSpecifierSeq(
- VS, getCurrentClass().IsInterface,
- DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
- if (!VS.isUnset()) {
- // If we saw any GNU-style attributes that are known to GCC followed by a
- // virt-specifier, issue a GCC-compat warning.
- for (const ParsedAttr &AL : DeclaratorInfo.getAttributes())
- if (AL.isKnownToGCC() && !AL.isCXX11Attribute())
- Diag(AL.getLoc(), diag::warn_gcc_attribute_location);
- MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
- }
- }
- // If this has neither a name nor a bit width, something has gone seriously
- // wrong. Skip until the semi-colon or }.
- if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
- // If so, skip until the semi-colon or a }.
- SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
- return true;
- }
- return false;
- }
- /// Look for declaration specifiers possibly occurring after C++11
- /// virt-specifier-seq and diagnose them.
- void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
- Declarator &D,
- VirtSpecifiers &VS) {
- DeclSpec DS(AttrFactory);
- // GNU-style and C++11 attributes are not allowed here, but they will be
- // handled by the caller. Diagnose everything else.
- ParseTypeQualifierListOpt(
- DS, AR_NoAttributesParsed, false,
- /*IdentifierRequired=*/false, llvm::function_ref<void()>([&]() {
- Actions.CodeCompleteFunctionQualifiers(DS, D, &VS);
- }));
- D.ExtendWithDeclSpec(DS);
- if (D.isFunctionDeclarator()) {
- auto &Function = D.getFunctionTypeInfo();
- if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
- auto DeclSpecCheck = [&](DeclSpec::TQ TypeQual, StringRef FixItName,
- SourceLocation SpecLoc) {
- FixItHint Insertion;
- auto &MQ = Function.getOrCreateMethodQualifiers();
- if (!(MQ.getTypeQualifiers() & TypeQual)) {
- std::string Name(FixItName.data());
- Name += " ";
- Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
- MQ.SetTypeQual(TypeQual, SpecLoc);
- }
- Diag(SpecLoc, diag::err_declspec_after_virtspec)
- << FixItName
- << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
- << FixItHint::CreateRemoval(SpecLoc) << Insertion;
- };
- DS.forEachQualifier(DeclSpecCheck);
- }
- // Parse ref-qualifiers.
- bool RefQualifierIsLValueRef = true;
- SourceLocation RefQualifierLoc;
- if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
- const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
- FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
- Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
- Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
- Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
- << (RefQualifierIsLValueRef ? "&" : "&&")
- << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
- << FixItHint::CreateRemoval(RefQualifierLoc)
- << Insertion;
- D.SetRangeEnd(RefQualifierLoc);
- }
- }
- }
- /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
- ///
- /// member-declaration:
- /// decl-specifier-seq[opt] member-declarator-list[opt] ';'
- /// function-definition ';'[opt]
- /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
- /// using-declaration [TODO]
- /// [C++0x] static_assert-declaration
- /// template-declaration
- /// [GNU] '__extension__' member-declaration
- ///
- /// member-declarator-list:
- /// member-declarator
- /// member-declarator-list ',' member-declarator
- ///
- /// member-declarator:
- /// declarator virt-specifier-seq[opt] pure-specifier[opt]
- /// declarator constant-initializer[opt]
- /// [C++11] declarator brace-or-equal-initializer[opt]
- /// identifier[opt] ':' constant-expression
- ///
- /// virt-specifier-seq:
- /// virt-specifier
- /// virt-specifier-seq virt-specifier
- ///
- /// virt-specifier:
- /// override
- /// final
- /// [MS] sealed
- ///
- /// pure-specifier:
- /// '= 0'
- ///
- /// constant-initializer:
- /// '=' constant-expression
- ///
- Parser::DeclGroupPtrTy
- Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
- ParsedAttributes &AccessAttrs,
- const ParsedTemplateInfo &TemplateInfo,
- ParsingDeclRAIIObject *TemplateDiags) {
- if (Tok.is(tok::at)) {
- if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs))
- Diag(Tok, diag::err_at_defs_cxx);
- else
- Diag(Tok, diag::err_at_in_class);
- ConsumeToken();
- SkipUntil(tok::r_brace, StopAtSemi);
- return nullptr;
- }
- // Turn on colon protection early, while parsing declspec, although there is
- // nothing to protect there. It prevents from false errors if error recovery
- // incorrectly determines where the declspec ends, as in the example:
- // struct A { enum class B { C }; };
- // const int C = 4;
- // struct D { A::B : C; };
- ColonProtectionRAIIObject X(*this);
- // Access declarations.
- bool MalformedTypeSpec = false;
- if (!TemplateInfo.Kind &&
- Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
- if (TryAnnotateCXXScopeToken())
- MalformedTypeSpec = true;
- bool isAccessDecl;
- if (Tok.isNot(tok::annot_cxxscope))
- isAccessDecl = false;
- else if (NextToken().is(tok::identifier))
- isAccessDecl = GetLookAheadToken(2).is(tok::semi);
- else
- isAccessDecl = NextToken().is(tok::kw_operator);
- if (isAccessDecl) {
- // Collect the scope specifier token we annotated earlier.
- CXXScopeSpec SS;
- ParseOptionalCXXScopeSpecifier(SS, nullptr,
- /*EnteringContext=*/false);
- if (SS.isInvalid()) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- // Try to parse an unqualified-id.
- SourceLocation TemplateKWLoc;
- UnqualifiedId Name;
- if (ParseUnqualifiedId(SS, false, true, true, false, nullptr,
- &TemplateKWLoc, Name)) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- // TODO: recover from mistakenly-qualified operator declarations.
- if (ExpectAndConsume(tok::semi, diag::err_expected_after,
- "access declaration")) {
- SkipUntil(tok::semi);
- return nullptr;
- }
- // FIXME: We should do something with the 'template' keyword here.
- return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration(
- getCurScope(), AS, /*UsingLoc*/ SourceLocation(),
- /*TypenameLoc*/ SourceLocation(), SS, Name,
- /*EllipsisLoc*/ SourceLocation(),
- /*AttrList*/ ParsedAttributesView())));
- }
- }
- // static_assert-declaration. A templated static_assert declaration is
- // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
- if (!TemplateInfo.Kind &&
- Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
- SourceLocation DeclEnd;
- return DeclGroupPtrTy::make(
- DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd)));
- }
- if (Tok.is(tok::kw_template)) {
- assert(!TemplateInfo.TemplateParams &&
- "Nested template improperly parsed?");
- ObjCDeclContextSwitch ObjCDC(*this);
- SourceLocation DeclEnd;
- return DeclGroupPtrTy::make(
- DeclGroupRef(ParseTemplateDeclarationOrSpecialization(
- DeclaratorContext::MemberContext, DeclEnd, AccessAttrs, AS)));
- }
- // Handle: member-declaration ::= '__extension__' member-declaration
- if (Tok.is(tok::kw___extension__)) {
- // __extension__ silences extension warnings in the subexpression.
- ExtensionRAIIObject O(Diags); // Use RAII to do this.
- ConsumeToken();
- return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
- TemplateInfo, TemplateDiags);
- }
- ParsedAttributesWithRange attrs(AttrFactory);
- ParsedAttributesViewWithRange FnAttrs;
- // Optional C++11 attribute-specifier
- MaybeParseCXX11Attributes(attrs);
- // We need to keep these attributes for future diagnostic
- // before they are taken over by declaration specifier.
- FnAttrs.addAll(attrs.begin(), attrs.end());
- FnAttrs.Range = attrs.Range;
- MaybeParseMicrosoftAttributes(attrs);
- if (Tok.is(tok::kw_using)) {
- ProhibitAttributes(attrs);
- // Eat 'using'.
- SourceLocation UsingLoc = ConsumeToken();
- // Consume unexpected 'template' keywords.
- while (Tok.is(tok::kw_template)) {
- SourceLocation TemplateLoc = ConsumeToken();
- Diag(TemplateLoc, diag::err_unexpected_template_after_using)
- << FixItHint::CreateRemoval(TemplateLoc);
- }
- if (Tok.is(tok::kw_namespace)) {
- Diag(UsingLoc, diag::err_using_namespace_in_class);
- SkipUntil(tok::semi, StopBeforeMatch);
- return nullptr;
- }
- SourceLocation DeclEnd;
- // Otherwise, it must be a using-declaration or an alias-declaration.
- return ParseUsingDeclaration(DeclaratorContext::MemberContext, TemplateInfo,
- UsingLoc, DeclEnd, AS);
- }
- // Hold late-parsed attributes so we can attach a Decl to them later.
- LateParsedAttrList CommonLateParsedAttrs;
- // decl-specifier-seq:
- // Parse the common declaration-specifiers piece.
- ParsingDeclSpec DS(*this, TemplateDiags);
- DS.takeAttributesFrom(attrs);
- if (MalformedTypeSpec)
- DS.SetTypeSpecError();
- ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class,
- &CommonLateParsedAttrs);
- // Turn off colon protection that was set for declspec.
- X.restore();
- // If we had a free-standing type definition with a missing semicolon, we
- // may get this far before the problem becomes obvious.
- if (DS.hasTagDefinition() &&
- TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
- DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class,
- &CommonLateParsedAttrs))
- return nullptr;
- MultiTemplateParamsArg TemplateParams(
- TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
- : nullptr,
- TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
- if (TryConsumeToken(tok::semi)) {
- if (DS.isFriendSpecified())
- ProhibitAttributes(FnAttrs);
- RecordDecl *AnonRecord = nullptr;
- Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
- getCurScope(), AS, DS, TemplateParams, false, AnonRecord);
- DS.complete(TheDecl);
- if (AnonRecord) {
- Decl* decls[] = {AnonRecord, TheDecl};
- return Actions.BuildDeclaratorGroup(decls);
- }
- return Actions.ConvertDeclToDeclGroup(TheDecl);
- }
- ParsingDeclarator DeclaratorInfo(*this, DS, DeclaratorContext::MemberContext);
- VirtSpecifiers VS;
- // Hold late-parsed attributes so we can attach a Decl to them later.
- LateParsedAttrList LateParsedAttrs;
- SourceLocation EqualLoc;
- SourceLocation PureSpecLoc;
- auto TryConsumePureSpecifier = [&] (bool AllowDefinition) {
- if (Tok.isNot(tok::equal))
- return false;
- auto &Zero = NextToken();
- SmallString<8> Buffer;
- if (Zero.isNot(tok::numeric_constant) || Zero.getLength() != 1 ||
- PP.getSpelling(Zero, Buffer) != "0")
- return false;
- auto &After = GetLookAheadToken(2);
- if (!After.isOneOf(tok::semi, tok::comma) &&
- !(AllowDefinition &&
- After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
- return false;
- EqualLoc = ConsumeToken();
- PureSpecLoc = ConsumeToken();
- return true;
- };
- SmallVector<Decl *, 8> DeclsInGroup;
- ExprResult BitfieldSize;
- bool ExpectSemi = true;
- // Parse the first declarator.
- if (ParseCXXMemberDeclaratorBeforeInitializer(
- DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
- TryConsumeToken(tok::semi);
- return nullptr;
- }
- // Check for a member function definition.
- if (BitfieldSize.isUnset()) {
- // MSVC permits pure specifier on inline functions defined at class scope.
- // Hence check for =0 before checking for function definition.
- if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
- TryConsumePureSpecifier(/*AllowDefinition*/ true);
- FunctionDefinitionKind DefinitionKind = FDK_Declaration;
- // function-definition:
- //
- // In C++11, a non-function declarator followed by an open brace is a
- // braced-init-list for an in-class member initialization, not an
- // erroneous function definition.
- if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
- DefinitionKind = FDK_Definition;
- } else if (DeclaratorInfo.isFunctionDeclarator()) {
- if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
- DefinitionKind = FDK_Definition;
- } else if (Tok.is(tok::equal)) {
- const Token &KW = NextToken();
- if (KW.is(tok::kw_default))
- DefinitionKind = FDK_Defaulted;
- else if (KW.is(tok::kw_delete))
- DefinitionKind = FDK_Deleted;
- }
- }
- DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
- // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
- // to a friend declaration, that declaration shall be a definition.
- if (DeclaratorInfo.isFunctionDeclarator() &&
- DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
- // Diagnose attributes that appear before decl specifier:
- // [[]] friend int foo();
- ProhibitAttributes(FnAttrs);
- }
- if (DefinitionKind != FDK_Declaration) {
- if (!DeclaratorInfo.isFunctionDeclarator()) {
- Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
- ConsumeBrace();
- SkipUntil(tok::r_brace);
- // Consume the optional ';'
- TryConsumeToken(tok::semi);
- return nullptr;
- }
- if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
- Diag(DeclaratorInfo.getIdentifierLoc(),
- diag::err_function_declared_typedef);
- // Recover by treating the 'typedef' as spurious.
- DS.ClearStorageClassSpecs();
- }
- Decl *FunDecl =
- ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
- VS, PureSpecLoc);
- if (FunDecl) {
- for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
- CommonLateParsedAttrs[i]->addDecl(FunDecl);
- }
- for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
- LateParsedAttrs[i]->addDecl(FunDecl);
- }
- }
- LateParsedAttrs.clear();
- // Consume the ';' - it's optional unless we have a delete or default
- if (Tok.is(tok::semi))
- ConsumeExtraSemi(AfterMemberFunctionDefinition);
- return DeclGroupPtrTy::make(DeclGroupRef(FunDecl));
- }
- }
- // member-declarator-list:
- // member-declarator
- // member-declarator-list ',' member-declarator
- while (1) {
- InClassInitStyle HasInClassInit = ICIS_NoInit;
- bool HasStaticInitializer = false;
- if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) {
- if (DeclaratorInfo.isDeclarationOfFunction()) {
- // It's a pure-specifier.
- if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false))
- // Parse it as an expression so that Sema can diagnose it.
- HasStaticInitializer = true;
- } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
- DeclSpec::SCS_static &&
- DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
- DeclSpec::SCS_typedef &&
- !DS.isFriendSpecified()) {
- // It's a default member initializer.
- if (BitfieldSize.get())
- Diag(Tok, getLangOpts().CPlusPlus2a
- ? diag::warn_cxx17_compat_bitfield_member_init
- : diag::ext_bitfield_member_init);
- HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
- } else {
- HasStaticInitializer = true;
- }
- }
- // NOTE: If Sema is the Action module and declarator is an instance field,
- // this call will *not* return the created decl; It will return null.
- // See Sema::ActOnCXXMemberDeclarator for details.
- NamedDecl *ThisDecl = nullptr;
- if (DS.isFriendSpecified()) {
- // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
- // to a friend declaration, that declaration shall be a definition.
- //
- // Diagnose attributes that appear in a friend member function declarator:
- // friend int foo [[]] ();
- SmallVector<SourceRange, 4> Ranges;
- DeclaratorInfo.getCXX11AttributeRanges(Ranges);
- for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
- E = Ranges.end(); I != E; ++I)
- Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
- ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
- TemplateParams);
- } else {
- ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
- DeclaratorInfo,
- TemplateParams,
- BitfieldSize.get(),
- VS, HasInClassInit);
- if (VarTemplateDecl *VT =
- ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
- // Re-direct this decl to refer to the templated decl so that we can
- // initialize it.
- ThisDecl = VT->getTemplatedDecl();
- if (ThisDecl)
- Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
- }
- // Error recovery might have converted a non-static member into a static
- // member.
- if (HasInClassInit != ICIS_NoInit &&
- DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
- DeclSpec::SCS_static) {
- HasInClassInit = ICIS_NoInit;
- HasStaticInitializer = true;
- }
- if (ThisDecl && PureSpecLoc.isValid())
- Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
- // Handle the initializer.
- if (HasInClassInit != ICIS_NoInit) {
- // The initializer was deferred; parse it and cache the tokens.
- Diag(Tok, getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_nonstatic_member_init
- : diag::ext_nonstatic_member_init);
- if (DeclaratorInfo.isArrayOfUnknownBound()) {
- // C++11 [dcl.array]p3: An array bound may also be omitted when the
- // declarator is followed by an initializer.
- //
- // A brace-or-equal-initializer for a member-declarator is not an
- // initializer in the grammar, so this is ill-formed.
- Diag(Tok, diag::err_incomplete_array_member_init);
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- // Avoid later warnings about a class member of incomplete type.
- if (ThisDecl)
- ThisDecl->setInvalidDecl();
- } else
- ParseCXXNonStaticMemberInitializer(ThisDecl);
- } else if (HasStaticInitializer) {
- // Normal initializer.
- ExprResult Init = ParseCXXMemberInitializer(
- ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
- if (Init.isInvalid())
- SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
- else if (ThisDecl)
- Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid());
- } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
- // No initializer.
- Actions.ActOnUninitializedDecl(ThisDecl);
- if (ThisDecl) {
- if (!ThisDecl->isInvalidDecl()) {
- // Set the Decl for any late parsed attributes
- for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
- CommonLateParsedAttrs[i]->addDecl(ThisDecl);
- for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
- LateParsedAttrs[i]->addDecl(ThisDecl);
- }
- Actions.FinalizeDeclaration(ThisDecl);
- DeclsInGroup.push_back(ThisDecl);
- if (DeclaratorInfo.isFunctionDeclarator() &&
- DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
- DeclSpec::SCS_typedef)
- HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
- }
- LateParsedAttrs.clear();
- DeclaratorInfo.complete(ThisDecl);
- // If we don't have a comma, it is either the end of the list (a ';')
- // or an error, bail out.
- SourceLocation CommaLoc;
- if (!TryConsumeToken(tok::comma, CommaLoc))
- break;
- if (Tok.isAtStartOfLine() &&
- !MightBeDeclarator(DeclaratorContext::MemberContext)) {
- // This comma was followed by a line-break and something which can't be
- // the start of a declarator. The comma was probably a typo for a
- // semicolon.
- Diag(CommaLoc, diag::err_expected_semi_declaration)
- << FixItHint::CreateReplacement(CommaLoc, ";");
- ExpectSemi = false;
- break;
- }
- // Parse the next declarator.
- DeclaratorInfo.clear();
- VS.clear();
- BitfieldSize = ExprResult(/*Invalid=*/false);
- EqualLoc = PureSpecLoc = SourceLocation();
- DeclaratorInfo.setCommaLoc(CommaLoc);
- // GNU attributes are allowed before the second and subsequent declarator.
- MaybeParseGNUAttributes(DeclaratorInfo);
- if (ParseCXXMemberDeclaratorBeforeInitializer(
- DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
- break;
- }
- if (ExpectSemi &&
- ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
- // Skip to end of block or statement.
- SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
- // If we stopped at a ';', eat it.
- TryConsumeToken(tok::semi);
- return nullptr;
- }
- return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
- }
- /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer.
- /// Also detect and reject any attempted defaulted/deleted function definition.
- /// The location of the '=', if any, will be placed in EqualLoc.
- ///
- /// This does not check for a pure-specifier; that's handled elsewhere.
- ///
- /// brace-or-equal-initializer:
- /// '=' initializer-expression
- /// braced-init-list
- ///
- /// initializer-clause:
- /// assignment-expression
- /// braced-init-list
- ///
- /// defaulted/deleted function-definition:
- /// '=' 'default'
- /// '=' 'delete'
- ///
- /// Prior to C++0x, the assignment-expression in an initializer-clause must
- /// be a constant-expression.
- ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
- SourceLocation &EqualLoc) {
- assert(Tok.isOneOf(tok::equal, tok::l_brace)
- && "Data member initializer not starting with '=' or '{'");
- EnterExpressionEvaluationContext Context(
- Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, D);
- if (TryConsumeToken(tok::equal, EqualLoc)) {
- if (Tok.is(tok::kw_delete)) {
- // In principle, an initializer of '= delete p;' is legal, but it will
- // never type-check. It's better to diagnose it as an ill-formed expression
- // than as an ill-formed deleted non-function member.
- // An initializer of '= delete p, foo' will never be parsed, because
- // a top-level comma always ends the initializer expression.
- const Token &Next = NextToken();
- if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) {
- if (IsFunction)
- Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
- << 1 /* delete */;
- else
- Diag(ConsumeToken(), diag::err_deleted_non_function);
- return ExprError();
- }
- } else if (Tok.is(tok::kw_default)) {
- if (IsFunction)
- Diag(Tok, diag::err_default_delete_in_multiple_declaration)
- << 0 /* default */;
- else
- Diag(ConsumeToken(), diag::err_default_special_members);
- return ExprError();
- }
- }
- if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
- Diag(Tok, diag::err_ms_property_initializer) << PD;
- return ExprError();
- }
- return ParseInitializer();
- }
- void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
- SourceLocation AttrFixitLoc,
- unsigned TagType, Decl *TagDecl) {
- // Skip the optional 'final' keyword.
- if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
- assert(isCXX11FinalKeyword() && "not a class definition");
- ConsumeToken();
- // Diagnose any C++11 attributes after 'final' keyword.
- // We deliberately discard these attributes.
- ParsedAttributesWithRange Attrs(AttrFactory);
- CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
- // This can only happen if we had malformed misplaced attributes;
- // we only get called if there is a colon or left-brace after the
- // attributes.
- if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
- return;
- }
- // Skip the base clauses. This requires actually parsing them, because
- // otherwise we can't be sure where they end (a left brace may appear
- // within a template argument).
- if (Tok.is(tok::colon)) {
- // Enter the scope of the class so that we can correctly parse its bases.
- ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
- ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
- TagType == DeclSpec::TST_interface);
- auto OldContext =
- Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
- // Parse the bases but don't attach them to the class.
- ParseBaseClause(nullptr);
- Actions.ActOnTagFinishSkippedDefinition(OldContext);
- if (!Tok.is(tok::l_brace)) {
- Diag(PP.getLocForEndOfToken(PrevTokLocation),
- diag::err_expected_lbrace_after_base_specifiers);
- return;
- }
- }
- // Skip the body.
- assert(Tok.is(tok::l_brace));
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- T.skipToEnd();
- // Parse and discard any trailing attributes.
- ParsedAttributes Attrs(AttrFactory);
- if (Tok.is(tok::kw___attribute))
- MaybeParseGNUAttributes(Attrs);
- }
- Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
- AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs,
- DeclSpec::TST TagType, Decl *TagDecl) {
- ParenBraceBracketBalancer BalancerRAIIObj(*this);
- switch (Tok.getKind()) {
- case tok::kw___if_exists:
- case tok::kw___if_not_exists:
- ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, AS);
- return nullptr;
- case tok::semi:
- // Check for extraneous top-level semicolon.
- ConsumeExtraSemi(InsideStruct, TagType);
- return nullptr;
- // Handle pragmas that can appear as member declarations.
- case tok::annot_pragma_vis:
- HandlePragmaVisibility();
- return nullptr;
- case tok::annot_pragma_pack:
- HandlePragmaPack();
- return nullptr;
- case tok::annot_pragma_align:
- HandlePragmaAlign();
- return nullptr;
- case tok::annot_pragma_ms_pointers_to_members:
- HandlePragmaMSPointersToMembers();
- return nullptr;
- case tok::annot_pragma_ms_pragma:
- HandlePragmaMSPragma();
- return nullptr;
- case tok::annot_pragma_ms_vtordisp:
- HandlePragmaMSVtorDisp();
- return nullptr;
- case tok::annot_pragma_dump:
- HandlePragmaDump();
- return nullptr;
- case tok::kw_namespace:
- // If we see a namespace here, a close brace was missing somewhere.
- DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
- return nullptr;
- case tok::kw_private:
- // FIXME: We don't accept GNU attributes on access specifiers in OpenCL mode
- // yet.
- if (getLangOpts().OpenCL && !NextToken().is(tok::colon))
- return ParseCXXClassMemberDeclaration(AS, AccessAttrs);
- LLVM_FALLTHROUGH;
- case tok::kw_public:
- case tok::kw_protected: {
- AccessSpecifier NewAS = getAccessSpecifierIfPresent();
- assert(NewAS != AS_none);
- // Current token is a C++ access specifier.
- AS = NewAS;
- SourceLocation ASLoc = Tok.getLocation();
- unsigned TokLength = Tok.getLength();
- ConsumeToken();
- AccessAttrs.clear();
- MaybeParseGNUAttributes(AccessAttrs);
- SourceLocation EndLoc;
- if (TryConsumeToken(tok::colon, EndLoc)) {
- } else if (TryConsumeToken(tok::semi, EndLoc)) {
- Diag(EndLoc, diag::err_expected)
- << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
- } else {
- EndLoc = ASLoc.getLocWithOffset(TokLength);
- Diag(EndLoc, diag::err_expected)
- << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
- }
- // The Microsoft extension __interface does not permit non-public
- // access specifiers.
- if (TagType == DeclSpec::TST_interface && AS != AS_public) {
- Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected);
- }
- if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc, AccessAttrs)) {
- // found another attribute than only annotations
- AccessAttrs.clear();
- }
- return nullptr;
- }
- case tok::annot_pragma_openmp:
- return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, AccessAttrs, TagType,
- TagDecl);
- default:
- return ParseCXXClassMemberDeclaration(AS, AccessAttrs);
- }
- }
- /// ParseCXXMemberSpecification - Parse the class definition.
- ///
- /// member-specification:
- /// member-declaration member-specification[opt]
- /// access-specifier ':' member-specification[opt]
- ///
- void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
- SourceLocation AttrFixitLoc,
- ParsedAttributesWithRange &Attrs,
- unsigned TagType, Decl *TagDecl) {
- assert((TagType == DeclSpec::TST_struct ||
- TagType == DeclSpec::TST_interface ||
- TagType == DeclSpec::TST_union ||
- TagType == DeclSpec::TST_class) && "Invalid TagType!");
- llvm::TimeTraceScope TimeScope("ParseClass", [&]() {
- if (auto *TD = dyn_cast_or_null<NamedDecl>(TagDecl))
- return TD->getQualifiedNameAsString();
- return std::string("<anonymous>");
- });
- PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
- "parsing struct/union/class body");
- // Determine whether this is a non-nested class. Note that local
- // classes are *not* considered to be nested classes.
- bool NonNestedClass = true;
- if (!ClassStack.empty()) {
- for (const Scope *S = getCurScope(); S; S = S->getParent()) {
- if (S->isClassScope()) {
- // We're inside a class scope, so this is a nested class.
- NonNestedClass = false;
- // The Microsoft extension __interface does not permit nested classes.
- if (getCurrentClass().IsInterface) {
- Diag(RecordLoc, diag::err_invalid_member_in_interface)
- << /*ErrorType=*/6
- << (isa<NamedDecl>(TagDecl)
- ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
- : "(anonymous)");
- }
- break;
- }
- if ((S->getFlags() & Scope::FnScope))
- // If we're in a function or function template then this is a local
- // class rather than a nested class.
- break;
- }
- }
- // Enter a scope for the class.
- ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
- // Note that we are parsing a new (potentially-nested) class definition.
- ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
- TagType == DeclSpec::TST_interface);
- if (TagDecl)
- Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
- SourceLocation FinalLoc;
- bool IsFinalSpelledSealed = false;
- // Parse the optional 'final' keyword.
- if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
- VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
- assert((Specifier == VirtSpecifiers::VS_Final ||
- Specifier == VirtSpecifiers::VS_GNU_Final ||
- Specifier == VirtSpecifiers::VS_Sealed) &&
- "not a class definition");
- FinalLoc = ConsumeToken();
- IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
- if (TagType == DeclSpec::TST_interface)
- Diag(FinalLoc, diag::err_override_control_interface)
- << VirtSpecifiers::getSpecifierName(Specifier);
- else if (Specifier == VirtSpecifiers::VS_Final)
- Diag(FinalLoc, getLangOpts().CPlusPlus11
- ? diag::warn_cxx98_compat_override_control_keyword
- : diag::ext_override_control_keyword)
- << VirtSpecifiers::getSpecifierName(Specifier);
- else if (Specifier == VirtSpecifiers::VS_Sealed)
- Diag(FinalLoc, diag::ext_ms_sealed_keyword);
- else if (Specifier == VirtSpecifiers::VS_GNU_Final)
- Diag(FinalLoc, diag::ext_warn_gnu_final);
- // Parse any C++11 attributes after 'final' keyword.
- // These attributes are not allowed to appear here,
- // and the only possible place for them to appertain
- // to the class would be between class-key and class-name.
- CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
- // ParseClassSpecifier() does only a superficial check for attributes before
- // deciding to call this method. For example, for
- // `class C final alignas ([l) {` it will decide that this looks like a
- // misplaced attribute since it sees `alignas '(' ')'`. But the actual
- // attribute parsing code will try to parse the '[' as a constexpr lambda
- // and consume enough tokens that the alignas parsing code will eat the
- // opening '{'. So bail out if the next token isn't one we expect.
- if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
- if (TagDecl)
- Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
- return;
- }
- }
- if (Tok.is(tok::colon)) {
- ParseScope InheritanceScope(this, getCurScope()->getFlags() |
- Scope::ClassInheritanceScope);
- ParseBaseClause(TagDecl);
- if (!Tok.is(tok::l_brace)) {
- bool SuggestFixIt = false;
- SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
- if (Tok.isAtStartOfLine()) {
- switch (Tok.getKind()) {
- case tok::kw_private:
- case tok::kw_protected:
- case tok::kw_public:
- SuggestFixIt = NextToken().getKind() == tok::colon;
- break;
- case tok::kw_static_assert:
- case tok::r_brace:
- case tok::kw_using:
- // base-clause can have simple-template-id; 'template' can't be there
- case tok::kw_template:
- SuggestFixIt = true;
- break;
- case tok::identifier:
- SuggestFixIt = isConstructorDeclarator(true);
- break;
- default:
- SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
- break;
- }
- }
- DiagnosticBuilder LBraceDiag =
- Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
- if (SuggestFixIt) {
- LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
- // Try recovering from missing { after base-clause.
- PP.EnterToken(Tok, /*IsReinject*/true);
- Tok.setKind(tok::l_brace);
- } else {
- if (TagDecl)
- Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
- return;
- }
- }
- }
- assert(Tok.is(tok::l_brace));
- BalancedDelimiterTracker T(*this, tok::l_brace);
- T.consumeOpen();
- if (TagDecl)
- Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
- IsFinalSpelledSealed,
- T.getOpenLocation());
- // C++ 11p3: Members of a class defined with the keyword class are private
- // by default. Members of a class defined with the keywords struct or union
- // are public by default.
- AccessSpecifier CurAS;
- if (TagType == DeclSpec::TST_class)
- CurAS = AS_private;
- else
- CurAS = AS_public;
- ParsedAttributesWithRange AccessAttrs(AttrFactory);
- if (TagDecl) {
- // While we still have something to read, read the member-declarations.
- while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
- Tok.isNot(tok::eof)) {
- // Each iteration of this loop reads one member-declaration.
- ParseCXXClassMemberDeclarationWithPragmas(
- CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl);
- }
- T.consumeClose();
- } else {
- SkipUntil(tok::r_brace);
- }
- // If attributes exist after class contents, parse them.
- ParsedAttributes attrs(AttrFactory);
- MaybeParseGNUAttributes(attrs);
- if (TagDecl)
- Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
- T.getOpenLocation(),
- T.getCloseLocation(), attrs);
- // C++11 [class.mem]p2:
- // Within the class member-specification, the class is regarded as complete
- // within function bodies, default arguments, exception-specifications, and
- // brace-or-equal-initializers for non-static data members (including such
- // things in nested classes).
- if (TagDecl && NonNestedClass) {
- // We are not inside a nested class. This class and its nested classes
- // are complete and we can parse the delayed portions of method
- // declarations and the lexed inline method definitions, along with any
- // delayed attributes.
- SourceLocation SavedPrevTokLocation = PrevTokLocation;
- ParseLexedAttributes(getCurrentClass());
- ParseLexedMethodDeclarations(getCurrentClass());
- // We've finished with all pending member declarations.
- Actions.ActOnFinishCXXMemberDecls();
- ParseLexedMemberInitializers(getCurrentClass());
- ParseLexedMethodDefs(getCurrentClass());
- PrevTokLocation = SavedPrevTokLocation;
- // We've finished parsing everything, including default argument
- // initializers.
- Actions.ActOnFinishCXXNonNestedClass(TagDecl);
- }
- if (TagDecl)
- Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
- // Leave the class scope.
- ParsingDef.Pop();
- ClassScope.Exit();
- }
- void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
- assert(Tok.is(tok::kw_namespace));
- // FIXME: Suggest where the close brace should have gone by looking
- // at indentation changes within the definition body.
- Diag(D->getLocation(),
- diag::err_missing_end_of_definition) << D;
- Diag(Tok.getLocation(),
- diag::note_missing_end_of_definition_before) << D;
- // Push '};' onto the token stream to recover.
- PP.EnterToken(Tok, /*IsReinject*/ true);
- Tok.startToken();
- Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
- Tok.setKind(tok::semi);
- PP.EnterToken(Tok, /*IsReinject*/ true);
- Tok.setKind(tok::r_brace);
- }
- /// ParseConstructorInitializer - Parse a C++ constructor initializer,
- /// which explicitly initializes the members or base classes of a
- /// class (C++ [class.base.init]). For example, the three initializers
- /// after the ':' in the Derived constructor below:
- ///
- /// @code
- /// class Base { };
- /// class Derived : Base {
- /// int x;
- /// float f;
- /// public:
- /// Derived(float f) : Base(), x(17), f(f) { }
- /// };
- /// @endcode
- ///
- /// [C++] ctor-initializer:
- /// ':' mem-initializer-list
- ///
- /// [C++] mem-initializer-list:
- /// mem-initializer ...[opt]
- /// mem-initializer ...[opt] , mem-initializer-list
- void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
- assert(Tok.is(tok::colon) &&
- "Constructor initializer always starts with ':'");
- // Poison the SEH identifiers so they are flagged as illegal in constructor
- // initializers.
- PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
- SourceLocation ColonLoc = ConsumeToken();
- SmallVector<CXXCtorInitializer*, 4> MemInitializers;
- bool AnyErrors = false;
- do {
- if (Tok.is(tok::code_completion)) {
- Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
- MemInitializers);
- return cutOffParsing();
- }
- MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
- if (!MemInit.isInvalid())
- MemInitializers.push_back(MemInit.get());
- else
- AnyErrors = true;
- if (Tok.is(tok::comma))
- ConsumeToken();
- else if (Tok.is(tok::l_brace))
- break;
- // If the previous initializer was valid and the next token looks like a
- // base or member initializer, assume that we're just missing a comma.
- else if (!MemInit.isInvalid() &&
- Tok.isOneOf(tok::identifier, tok::coloncolon)) {
- SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
- Diag(Loc, diag::err_ctor_init_missing_comma)
- << FixItHint::CreateInsertion(Loc, ", ");
- } else {
- // Skip over garbage, until we get to '{'. Don't eat the '{'.
- if (!MemInit.isInvalid())
- Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
- << tok::comma;
- SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
- break;
- }
- } while (true);
- Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
- AnyErrors);
- }
- /// ParseMemInitializer - Parse a C++ member initializer, which is
- /// part of a constructor initializer that explicitly initializes one
- /// member or base class (C++ [class.base.init]). See
- /// ParseConstructorInitializer for an example.
- ///
- /// [C++] mem-initializer:
- /// mem-initializer-id '(' expression-list[opt] ')'
- /// [C++0x] mem-initializer-id braced-init-list
- ///
- /// [C++] mem-initializer-id:
- /// '::'[opt] nested-name-specifier[opt] class-name
- /// identifier
- MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
- // parse '::'[opt] nested-name-specifier[opt]
- CXXScopeSpec SS;
- if (ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false))
- return true;
- // : identifier
- IdentifierInfo *II = nullptr;
- SourceLocation IdLoc = Tok.getLocation();
- // : declype(...)
- DeclSpec DS(AttrFactory);
- // : template_name<...>
- ParsedType TemplateTypeTy;
- if (Tok.is(tok::identifier)) {
- // Get the identifier. This may be a member name or a class name,
- // but we'll let the semantic analysis determine which it is.
- II = Tok.getIdentifierInfo();
- ConsumeToken();
- } else if (Tok.is(tok::annot_decltype)) {
- // Get the decltype expression, if there is one.
- // Uses of decltype will already have been converted to annot_decltype by
- // ParseOptionalCXXScopeSpecifier at this point.
- // FIXME: Can we get here with a scope specifier?
- ParseDecltypeSpecifier(DS);
- } else {
- TemplateIdAnnotation *TemplateId = Tok.is(tok::annot_template_id)
- ? takeTemplateIdAnnotation(Tok)
- : nullptr;
- if (TemplateId && (TemplateId->Kind == TNK_Type_template ||
- TemplateId->Kind == TNK_Dependent_template_name ||
- TemplateId->Kind == TNK_Undeclared_template)) {
- AnnotateTemplateIdTokenAsType(/*IsClassName*/true);
- assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
- TemplateTypeTy = getTypeAnnotation(Tok);
- ConsumeAnnotationToken();
- if (!TemplateTypeTy)
- return true;
- } else {
- Diag(Tok, diag::err_expected_member_or_base_name);
- return true;
- }
- }
- // Parse the '('.
- if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
- Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
- // FIXME: Add support for signature help inside initializer lists.
- ExprResult InitList = ParseBraceInitializer();
- if (InitList.isInvalid())
- return true;
- SourceLocation EllipsisLoc;
- TryConsumeToken(tok::ellipsis, EllipsisLoc);
- return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
- TemplateTypeTy, DS, IdLoc,
- InitList.get(), EllipsisLoc);
- } else if(Tok.is(tok::l_paren)) {
- BalancedDelimiterTracker T(*this, tok::l_paren);
- T.consumeOpen();
- // Parse the optional expression-list.
- ExprVector ArgExprs;
- CommaLocsTy CommaLocs;
- auto RunSignatureHelp = [&] {
- QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp(
- getCurScope(), ConstructorDecl, SS, TemplateTypeTy, ArgExprs, II,
- T.getOpenLocation());
- CalledSignatureHelp = true;
- return PreferredType;
- };
- if (Tok.isNot(tok::r_paren) &&
- ParseExpressionList(ArgExprs, CommaLocs, [&] {
- PreferredType.enterFunctionArgument(Tok.getLocation(),
- RunSignatureHelp);
- })) {
- if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
- RunSignatureHelp();
- SkipUntil(tok::r_paren, StopAtSemi);
- return true;
- }
- T.consumeClose();
- SourceLocation EllipsisLoc;
- TryConsumeToken(tok::ellipsis, EllipsisLoc);
- return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
- TemplateTypeTy, DS, IdLoc,
- T.getOpenLocation(), ArgExprs,
- T.getCloseLocation(), EllipsisLoc);
- }
- if (getLangOpts().CPlusPlus11)
- return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
- else
- return Diag(Tok, diag::err_expected) << tok::l_paren;
- }
- /// Parse a C++ exception-specification if present (C++0x [except.spec]).
- ///
- /// exception-specification:
- /// dynamic-exception-specification
- /// noexcept-specification
- ///
- /// noexcept-specification:
- /// 'noexcept'
- /// 'noexcept' '(' constant-expression ')'
- ExceptionSpecificationType
- Parser::tryParseExceptionSpecification(bool Delayed,
- SourceRange &SpecificationRange,
- SmallVectorImpl<ParsedType> &DynamicExceptions,
- SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
- ExprResult &NoexceptExpr,
- CachedTokens *&ExceptionSpecTokens) {
- ExceptionSpecificationType Result = EST_None;
- ExceptionSpecTokens = nullptr;
- // Handle delayed parsing of exception-specifications.
- if (Delayed) {
- if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
- return EST_None;
- // Consume and cache the starting token.
- bool IsNoexcept = Tok.is(tok::kw_noexcept);
- Token StartTok = Tok;
- SpecificationRange = SourceRange(ConsumeToken());
- // Check for a '('.
- if (!Tok.is(tok::l_paren)) {
- // If this is a bare 'noexcept', we're done.
- if (IsNoexcept) {
- Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
- NoexceptExpr = nullptr;
- return EST_BasicNoexcept;
- }
- Diag(Tok, diag::err_expected_lparen_after) << "throw";
- return EST_DynamicNone;
- }
- // Cache the tokens for the exception-specification.
- ExceptionSpecTokens = new CachedTokens;
- ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
- ExceptionSpecTokens->push_back(Tok); // '('
- SpecificationRange.setEnd(ConsumeParen()); // '('
- ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
- /*StopAtSemi=*/true,
- /*ConsumeFinalToken=*/true);
- SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation());
- return EST_Unparsed;
- }
- // See if there's a dynamic specification.
- if (Tok.is(tok::kw_throw)) {
- Result = ParseDynamicExceptionSpecification(SpecificationRange,
- DynamicExceptions,
- DynamicExceptionRanges);
- assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
- "Produced different number of exception types and ranges.");
- }
- // If there's no noexcept specification, we're done.
- if (Tok.isNot(tok::kw_noexcept))
- return Result;
- Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
- // If we already had a dynamic specification, parse the noexcept for,
- // recovery, but emit a diagnostic and don't store the results.
- SourceRange NoexceptRange;
- ExceptionSpecificationType NoexceptType = EST_None;
- SourceLocation KeywordLoc = ConsumeToken();
- if (Tok.is(tok::l_paren)) {
- // There is an argument.
- BalancedDelimiterTracker T(*this, tok::l_paren);
- T.consumeOpen();
- NoexceptExpr = ParseConstantExpression();
- T.consumeClose();
- if (!NoexceptExpr.isInvalid()) {
- NoexceptExpr = Actions.ActOnNoexceptSpec(KeywordLoc, NoexceptExpr.get(),
- NoexceptType);
- NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
- } else {
- NoexceptType = EST_BasicNoexcept;
- }
- } else {
- // There is no argument.
- NoexceptType = EST_BasicNoexcept;
- NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
- }
- if (Result == EST_None) {
- SpecificationRange = NoexceptRange;
- Result = NoexceptType;
- // If there's a dynamic specification after a noexcept specification,
- // parse that and ignore the results.
- if (Tok.is(tok::kw_throw)) {
- Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
- ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
- DynamicExceptionRanges);
- }
- } else {
- Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
- }
- return Result;
- }
- static void diagnoseDynamicExceptionSpecification(
- Parser &P, SourceRange Range, bool IsNoexcept) {
- if (P.getLangOpts().CPlusPlus11) {
- const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
- P.Diag(Range.getBegin(),
- P.getLangOpts().CPlusPlus17 && !IsNoexcept
- ? diag::ext_dynamic_exception_spec
- : diag::warn_exception_spec_deprecated)
- << Range;
- P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
- << Replacement << FixItHint::CreateReplacement(Range, Replacement);
- }
- }
- /// ParseDynamicExceptionSpecification - Parse a C++
- /// dynamic-exception-specification (C++ [except.spec]).
- ///
- /// dynamic-exception-specification:
- /// 'throw' '(' type-id-list [opt] ')'
- /// [MS] 'throw' '(' '...' ')'
- ///
- /// type-id-list:
- /// type-id ... [opt]
- /// type-id-list ',' type-id ... [opt]
- ///
- ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
- SourceRange &SpecificationRange,
- SmallVectorImpl<ParsedType> &Exceptions,
- SmallVectorImpl<SourceRange> &Ranges) {
- assert(Tok.is(tok::kw_throw) && "expected throw");
- SpecificationRange.setBegin(ConsumeToken());
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected_lparen_after) << "throw";
- SpecificationRange.setEnd(SpecificationRange.getBegin());
- return EST_DynamicNone;
- }
- // Parse throw(...), a Microsoft extension that means "this function
- // can throw anything".
- if (Tok.is(tok::ellipsis)) {
- SourceLocation EllipsisLoc = ConsumeToken();
- if (!getLangOpts().MicrosoftExt)
- Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
- T.consumeClose();
- SpecificationRange.setEnd(T.getCloseLocation());
- diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
- return EST_MSAny;
- }
- // Parse the sequence of type-ids.
- SourceRange Range;
- while (Tok.isNot(tok::r_paren)) {
- TypeResult Res(ParseTypeName(&Range));
- if (Tok.is(tok::ellipsis)) {
- // C++0x [temp.variadic]p5:
- // - In a dynamic-exception-specification (15.4); the pattern is a
- // type-id.
- SourceLocation Ellipsis = ConsumeToken();
- Range.setEnd(Ellipsis);
- if (!Res.isInvalid())
- Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
- }
- if (!Res.isInvalid()) {
- Exceptions.push_back(Res.get());
- Ranges.push_back(Range);
- }
- if (!TryConsumeToken(tok::comma))
- break;
- }
- T.consumeClose();
- SpecificationRange.setEnd(T.getCloseLocation());
- diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
- Exceptions.empty());
- return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
- }
- /// ParseTrailingReturnType - Parse a trailing return type on a new-style
- /// function declaration.
- TypeResult Parser::ParseTrailingReturnType(SourceRange &Range,
- bool MayBeFollowedByDirectInit) {
- assert(Tok.is(tok::arrow) && "expected arrow");
- ConsumeToken();
- return ParseTypeName(&Range, MayBeFollowedByDirectInit
- ? DeclaratorContext::TrailingReturnVarContext
- : DeclaratorContext::TrailingReturnContext);
- }
- /// We have just started parsing the definition of a new class,
- /// so push that class onto our stack of classes that is currently
- /// being parsed.
- Sema::ParsingClassState
- Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
- bool IsInterface) {
- assert((NonNestedClass || !ClassStack.empty()) &&
- "Nested class without outer class");
- ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
- return Actions.PushParsingClass();
- }
- /// Deallocate the given parsed class and all of its nested
- /// classes.
- void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
- for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
- delete Class->LateParsedDeclarations[I];
- delete Class;
- }
- /// Pop the top class of the stack of classes that are
- /// currently being parsed.
- ///
- /// This routine should be called when we have finished parsing the
- /// definition of a class, but have not yet popped the Scope
- /// associated with the class's definition.
- void Parser::PopParsingClass(Sema::ParsingClassState state) {
- assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
- Actions.PopParsingClass(state);
- ParsingClass *Victim = ClassStack.top();
- ClassStack.pop();
- if (Victim->TopLevelClass) {
- // Deallocate all of the nested classes of this class,
- // recursively: we don't need to keep any of this information.
- DeallocateParsedClasses(Victim);
- return;
- }
- assert(!ClassStack.empty() && "Missing top-level class?");
- if (Victim->LateParsedDeclarations.empty()) {
- // The victim is a nested class, but we will not need to perform
- // any processing after the definition of this class since it has
- // no members whose handling was delayed. Therefore, we can just
- // remove this nested class.
- DeallocateParsedClasses(Victim);
- return;
- }
- // This nested class has some members that will need to be processed
- // after the top-level class is completely defined. Therefore, add
- // it to the list of nested classes within its parent.
- assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
- ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
- Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
- }
- /// Try to parse an 'identifier' which appears within an attribute-token.
- ///
- /// \return the parsed identifier on success, and 0 if the next token is not an
- /// attribute-token.
- ///
- /// C++11 [dcl.attr.grammar]p3:
- /// If a keyword or an alternative token that satisfies the syntactic
- /// requirements of an identifier is contained in an attribute-token,
- /// it is considered an identifier.
- IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
- switch (Tok.getKind()) {
- default:
- // Identifiers and keywords have identifier info attached.
- if (!Tok.isAnnotation()) {
- if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
- Loc = ConsumeToken();
- return II;
- }
- }
- return nullptr;
- case tok::numeric_constant: {
- // If we got a numeric constant, check to see if it comes from a macro that
- // corresponds to the predefined __clang__ macro. If it does, warn the user
- // and recover by pretending they said _Clang instead.
- if (Tok.getLocation().isMacroID()) {
- SmallString<8> ExpansionBuf;
- SourceLocation ExpansionLoc =
- PP.getSourceManager().getExpansionLoc(Tok.getLocation());
- StringRef Spelling = PP.getSpelling(ExpansionLoc, ExpansionBuf);
- if (Spelling == "__clang__") {
- SourceRange TokRange(
- ExpansionLoc,
- PP.getSourceManager().getExpansionLoc(Tok.getEndLoc()));
- Diag(Tok, diag::warn_wrong_clang_attr_namespace)
- << FixItHint::CreateReplacement(TokRange, "_Clang");
- Loc = ConsumeToken();
- return &PP.getIdentifierTable().get("_Clang");
- }
- }
- return nullptr;
- }
- case tok::ampamp: // 'and'
- case tok::pipe: // 'bitor'
- case tok::pipepipe: // 'or'
- case tok::caret: // 'xor'
- case tok::tilde: // 'compl'
- case tok::amp: // 'bitand'
- case tok::ampequal: // 'and_eq'
- case tok::pipeequal: // 'or_eq'
- case tok::caretequal: // 'xor_eq'
- case tok::exclaim: // 'not'
- case tok::exclaimequal: // 'not_eq'
- // Alternative tokens do not have identifier info, but their spelling
- // starts with an alphabetical character.
- SmallString<8> SpellingBuf;
- SourceLocation SpellingLoc =
- PP.getSourceManager().getSpellingLoc(Tok.getLocation());
- StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf);
- if (isLetter(Spelling[0])) {
- Loc = ConsumeToken();
- return &PP.getIdentifierTable().get(Spelling);
- }
- return nullptr;
- }
- }
- static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
- IdentifierInfo *ScopeName) {
- switch (ParsedAttr::getKind(AttrName, ScopeName, ParsedAttr::AS_CXX11)) {
- case ParsedAttr::AT_CarriesDependency:
- case ParsedAttr::AT_Deprecated:
- case ParsedAttr::AT_FallThrough:
- case ParsedAttr::AT_CXX11NoReturn:
- case ParsedAttr::AT_NoUniqueAddress:
- return true;
- case ParsedAttr::AT_WarnUnusedResult:
- return !ScopeName && AttrName->getName().equals("nodiscard");
- case ParsedAttr::AT_Unused:
- return !ScopeName && AttrName->getName().equals("maybe_unused");
- default:
- return false;
- }
- }
- /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
- ///
- /// [C++11] attribute-argument-clause:
- /// '(' balanced-token-seq ')'
- ///
- /// [C++11] balanced-token-seq:
- /// balanced-token
- /// balanced-token-seq balanced-token
- ///
- /// [C++11] balanced-token:
- /// '(' balanced-token-seq ')'
- /// '[' balanced-token-seq ']'
- /// '{' balanced-token-seq '}'
- /// any token but '(', ')', '[', ']', '{', or '}'
- bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
- SourceLocation AttrNameLoc,
- ParsedAttributes &Attrs,
- SourceLocation *EndLoc,
- IdentifierInfo *ScopeName,
- SourceLocation ScopeLoc) {
- assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
- SourceLocation LParenLoc = Tok.getLocation();
- const LangOptions &LO = getLangOpts();
- ParsedAttr::Syntax Syntax =
- LO.CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x;
- // If the attribute isn't known, we will not attempt to parse any
- // arguments.
- if (!hasAttribute(LO.CPlusPlus ? AttrSyntax::CXX : AttrSyntax::C, ScopeName,
- AttrName, getTargetInfo(), getLangOpts())) {
- // Eat the left paren, then skip to the ending right paren.
- ConsumeParen();
- SkipUntil(tok::r_paren);
- return false;
- }
- if (ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"))) {
- // GNU-scoped attributes have some special cases to handle GNU-specific
- // behaviors.
- ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
- ScopeLoc, Syntax, nullptr);
- return true;
- }
- unsigned NumArgs;
- // Some Clang-scoped attributes have some special parsing behavior.
- if (ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang")))
- NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc,
- ScopeName, ScopeLoc, Syntax);
- else
- NumArgs =
- ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
- ScopeName, ScopeLoc, Syntax);
- if (!Attrs.empty() &&
- IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
- ParsedAttr &Attr = Attrs.back();
- // If the attribute is a standard or built-in attribute and we are
- // parsing an argument list, we need to determine whether this attribute
- // was allowed to have an argument list (such as [[deprecated]]), and how
- // many arguments were parsed (so we can diagnose on [[deprecated()]]).
- if (Attr.getMaxArgs() && !NumArgs) {
- // The attribute was allowed to have arguments, but none were provided
- // even though the attribute parsed successfully. This is an error.
- Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
- Attr.setInvalid(true);
- } else if (!Attr.getMaxArgs()) {
- // The attribute parsed successfully, but was not allowed to have any
- // arguments. It doesn't matter whether any were provided -- the
- // presence of the argument list (even if empty) is diagnosed.
- Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
- << AttrName
- << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
- Attr.setInvalid(true);
- }
- }
- return true;
- }
- /// ParseCXX11AttributeSpecifier - Parse a C++11 or C2x attribute-specifier.
- ///
- /// [C++11] attribute-specifier:
- /// '[' '[' attribute-list ']' ']'
- /// alignment-specifier
- ///
- /// [C++11] attribute-list:
- /// attribute[opt]
- /// attribute-list ',' attribute[opt]
- /// attribute '...'
- /// attribute-list ',' attribute '...'
- ///
- /// [C++11] attribute:
- /// attribute-token attribute-argument-clause[opt]
- ///
- /// [C++11] attribute-token:
- /// identifier
- /// attribute-scoped-token
- ///
- /// [C++11] attribute-scoped-token:
- /// attribute-namespace '::' identifier
- ///
- /// [C++11] attribute-namespace:
- /// identifier
- void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
- SourceLocation *endLoc) {
- if (Tok.is(tok::kw_alignas)) {
- Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
- ParseAlignmentSpecifier(attrs, endLoc);
- return;
- }
- assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) &&
- "Not a double square bracket attribute list");
- Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
- ConsumeBracket();
- ConsumeBracket();
- SourceLocation CommonScopeLoc;
- IdentifierInfo *CommonScopeName = nullptr;
- if (Tok.is(tok::kw_using)) {
- Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
- ? diag::warn_cxx14_compat_using_attribute_ns
- : diag::ext_using_attribute_ns);
- ConsumeToken();
- CommonScopeName = TryParseCXX11AttributeIdentifier(CommonScopeLoc);
- if (!CommonScopeName) {
- Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
- SkipUntil(tok::r_square, tok::colon, StopBeforeMatch);
- }
- if (!TryConsumeToken(tok::colon) && CommonScopeName)
- Diag(Tok.getLocation(), diag::err_expected) << tok::colon;
- }
- llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
- while (Tok.isNot(tok::r_square)) {
- // attribute not present
- if (TryConsumeToken(tok::comma))
- continue;
- SourceLocation ScopeLoc, AttrLoc;
- IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
- AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
- if (!AttrName)
- // Break out to the "expected ']'" diagnostic.
- break;
- // scoped attribute
- if (TryConsumeToken(tok::coloncolon)) {
- ScopeName = AttrName;
- ScopeLoc = AttrLoc;
- AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
- if (!AttrName) {
- Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
- SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
- continue;
- }
- }
- if (CommonScopeName) {
- if (ScopeName) {
- Diag(ScopeLoc, diag::err_using_attribute_ns_conflict)
- << SourceRange(CommonScopeLoc);
- } else {
- ScopeName = CommonScopeName;
- ScopeLoc = CommonScopeLoc;
- }
- }
- bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
- bool AttrParsed = false;
- if (StandardAttr &&
- !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
- Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
- << AttrName << SourceRange(SeenAttrs[AttrName]);
- // Parse attribute arguments
- if (Tok.is(tok::l_paren))
- AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
- ScopeName, ScopeLoc);
- if (!AttrParsed)
- attrs.addNew(
- AttrName,
- SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, AttrLoc),
- ScopeName, ScopeLoc, nullptr, 0,
- getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C2x);
- if (TryConsumeToken(tok::ellipsis))
- Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
- << AttrName;
- }
- if (ExpectAndConsume(tok::r_square))
- SkipUntil(tok::r_square);
- if (endLoc)
- *endLoc = Tok.getLocation();
- if (ExpectAndConsume(tok::r_square))
- SkipUntil(tok::r_square);
- }
- /// ParseCXX11Attributes - Parse a C++11 or C2x attribute-specifier-seq.
- ///
- /// attribute-specifier-seq:
- /// attribute-specifier-seq[opt] attribute-specifier
- void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
- SourceLocation *endLoc) {
- assert(standardAttributesAllowed());
- SourceLocation StartLoc = Tok.getLocation(), Loc;
- if (!endLoc)
- endLoc = &Loc;
- do {
- ParseCXX11AttributeSpecifier(attrs, endLoc);
- } while (isCXX11AttributeSpecifier());
- attrs.Range = SourceRange(StartLoc, *endLoc);
- }
- void Parser::DiagnoseAndSkipCXX11Attributes() {
- // Start and end location of an attribute or an attribute list.
- SourceLocation StartLoc = Tok.getLocation();
- SourceLocation EndLoc = SkipCXX11Attributes();
- if (EndLoc.isValid()) {
- SourceRange Range(StartLoc, EndLoc);
- Diag(StartLoc, diag::err_attributes_not_allowed)
- << Range;
- }
- }
- SourceLocation Parser::SkipCXX11Attributes() {
- SourceLocation EndLoc;
- if (!isCXX11AttributeSpecifier())
- return EndLoc;
- do {
- if (Tok.is(tok::l_square)) {
- BalancedDelimiterTracker T(*this, tok::l_square);
- T.consumeOpen();
- T.skipToEnd();
- EndLoc = T.getCloseLocation();
- } else {
- assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
- ConsumeToken();
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (!T.consumeOpen())
- T.skipToEnd();
- EndLoc = T.getCloseLocation();
- }
- } while (isCXX11AttributeSpecifier());
- return EndLoc;
- }
- /// Parse uuid() attribute when it appears in a [] Microsoft attribute.
- void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
- assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list");
- IdentifierInfo *UuidIdent = Tok.getIdentifierInfo();
- assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list");
- SourceLocation UuidLoc = Tok.getLocation();
- ConsumeToken();
- // Ignore the left paren location for now.
- BalancedDelimiterTracker T(*this, tok::l_paren);
- if (T.consumeOpen()) {
- Diag(Tok, diag::err_expected) << tok::l_paren;
- return;
- }
- ArgsVector ArgExprs;
- if (Tok.is(tok::string_literal)) {
- // Easy case: uuid("...") -- quoted string.
- ExprResult StringResult = ParseStringLiteralExpression();
- if (StringResult.isInvalid())
- return;
- ArgExprs.push_back(StringResult.get());
- } else {
- // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no
- // quotes in the parens. Just append the spelling of all tokens encountered
- // until the closing paren.
- SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul
- StrBuffer += "\"";
- // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace,
- // tok::r_brace, tok::minus, tok::identifier (think C000) and
- // tok::numeric_constant (0000) should be enough. But the spelling of the
- // uuid argument is checked later anyways, so there's no harm in accepting
- // almost anything here.
- // cl is very strict about whitespace in this form and errors out if any
- // is present, so check the space flags on the tokens.
- SourceLocation StartLoc = Tok.getLocation();
- while (Tok.isNot(tok::r_paren)) {
- if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
- Diag(Tok, diag::err_attribute_uuid_malformed_guid);
- SkipUntil(tok::r_paren, StopAtSemi);
- return;
- }
- SmallString<16> SpellingBuffer;
- SpellingBuffer.resize(Tok.getLength() + 1);
- bool Invalid = false;
- StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid);
- if (Invalid) {
- SkipUntil(tok::r_paren, StopAtSemi);
- return;
- }
- StrBuffer += TokSpelling;
- ConsumeAnyToken();
- }
- StrBuffer += "\"";
- if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
- Diag(Tok, diag::err_attribute_uuid_malformed_guid);
- ConsumeParen();
- return;
- }
- // Pretend the user wrote the appropriate string literal here.
- // ActOnStringLiteral() copies the string data into the literal, so it's
- // ok that the Token points to StrBuffer.
- Token Toks[1];
- Toks[0].startToken();
- Toks[0].setKind(tok::string_literal);
- Toks[0].setLocation(StartLoc);
- Toks[0].setLiteralData(StrBuffer.data());
- Toks[0].setLength(StrBuffer.size());
- StringLiteral *UuidString =
- cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
- ArgExprs.push_back(UuidString);
- }
- if (!T.consumeClose()) {
- Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr,
- SourceLocation(), ArgExprs.data(), ArgExprs.size(),
- ParsedAttr::AS_Microsoft);
- }
- }
- /// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr]
- ///
- /// [MS] ms-attribute:
- /// '[' token-seq ']'
- ///
- /// [MS] ms-attribute-seq:
- /// ms-attribute[opt]
- /// ms-attribute ms-attribute-seq
- void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
- SourceLocation *endLoc) {
- assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
- do {
- // FIXME: If this is actually a C++11 attribute, parse it as one.
- BalancedDelimiterTracker T(*this, tok::l_square);
- T.consumeOpen();
- // Skip most ms attributes except for a whitelist.
- while (true) {
- SkipUntil(tok::r_square, tok::identifier, StopAtSemi | StopBeforeMatch);
- if (Tok.isNot(tok::identifier)) // ']', but also eof
- break;
- if (Tok.getIdentifierInfo()->getName() == "uuid")
- ParseMicrosoftUuidAttributeArgs(attrs);
- else
- ConsumeToken();
- }
- T.consumeClose();
- if (endLoc)
- *endLoc = T.getCloseLocation();
- } while (Tok.is(tok::l_square));
- }
- void Parser::ParseMicrosoftIfExistsClassDeclaration(
- DeclSpec::TST TagType, ParsedAttributes &AccessAttrs,
- AccessSpecifier &CurAS) {
- IfExistsCondition Result;
- if (ParseMicrosoftIfExistsCondition(Result))
- return;
- BalancedDelimiterTracker Braces(*this, tok::l_brace);
- if (Braces.consumeOpen()) {
- Diag(Tok, diag::err_expected) << tok::l_brace;
- return;
- }
- switch (Result.Behavior) {
- case IEB_Parse:
- // Parse the declarations below.
- break;
- case IEB_Dependent:
- Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
- << Result.IsIfExists;
- // Fall through to skip.
- LLVM_FALLTHROUGH;
- case IEB_Skip:
- Braces.skipToEnd();
- return;
- }
- while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
- // __if_exists, __if_not_exists can nest.
- if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
- ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType,
- AccessAttrs, CurAS);
- continue;
- }
- // Check for extraneous top-level semicolon.
- if (Tok.is(tok::semi)) {
- ConsumeExtraSemi(InsideStruct, TagType);
- continue;
- }
- AccessSpecifier AS = getAccessSpecifierIfPresent();
- if (AS != AS_none) {
- // Current token is a C++ access specifier.
- CurAS = AS;
- SourceLocation ASLoc = Tok.getLocation();
- ConsumeToken();
- if (Tok.is(tok::colon))
- Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation(),
- ParsedAttributesView{});
- else
- Diag(Tok, diag::err_expected) << tok::colon;
- ConsumeToken();
- continue;
- }
- // Parse all the comma separated declarators.
- ParseCXXClassMemberDeclaration(CurAS, AccessAttrs);
- }
- Braces.consumeClose();
- }
|