Parse.cpp 507 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. #include "FormalsUtil.h"
  7. #include "../Runtime/Language/SourceDynamicProfileManager.h"
  8. #include "ByteCode/ByteCodeSerializer.h"
  9. #if DBG_DUMP
  10. void PrintPnodeWIndent(ParseNode *pnode, int indentAmt);
  11. #endif
  12. const char* const nopNames[knopLim] = {
  13. #define PTNODE(nop,sn,pc,nk,grfnop,json) sn,
  14. #include "ptlist.h"
  15. };
  16. void printNop(int nop) {
  17. Output::Print(_u("%S\n"), nopNames[nop]);
  18. }
  19. const uint ParseNode::mpnopgrfnop[knopLim] =
  20. {
  21. #define PTNODE(nop,sn,pc,nk,grfnop,json) grfnop,
  22. #include "ptlist.h"
  23. };
  24. struct BlockInfoStack
  25. {
  26. StmtNest pstmt;
  27. ParseNodeBlock *pnodeBlock;
  28. ParseNodePtr *m_ppnodeLex; // lexical variable list tail
  29. BlockInfoStack *pBlockInfoOuter; // containing block's BlockInfoStack
  30. BlockInfoStack *pBlockInfoFunction; // nearest function's BlockInfoStack (if pnodeBlock is a function, this points to itself)
  31. };
  32. #if DEBUG
  33. Parser::Parser(Js::ScriptContext* scriptContext, BOOL strictMode, PageAllocator *alloc, bool isBackground, size_t size)
  34. #else
  35. Parser::Parser(Js::ScriptContext* scriptContext, BOOL strictMode, PageAllocator *alloc, bool isBackground)
  36. #endif
  37. : m_nodeAllocator(_u("Parser"), alloc ? alloc : scriptContext->GetThreadContext()->GetPageAllocator(), Parser::OutOfMemory),
  38. m_cactIdentToNodeLookup(0),
  39. m_grfscr(fscrNil),
  40. m_length(0),
  41. m_originalLength(0),
  42. m_nextFunctionId(nullptr),
  43. m_sourceContextInfo(nullptr),
  44. #if ENABLE_BACKGROUND_PARSING
  45. m_isInBackground(isBackground),
  46. m_hasParallelJob(false),
  47. m_doingFastScan(false),
  48. #endif
  49. m_nextBlockId(0),
  50. m_tempGuestArenaReleased(false),
  51. m_tempGuestArena(scriptContext->GetTemporaryGuestAllocator(_u("ParserRegex")), scriptContext->GetRecycler()),
  52. // use the GuestArena directly for keeping the RegexPattern* alive during byte code generation
  53. m_registeredRegexPatterns(m_tempGuestArena->GetAllocator()),
  54. m_scriptContext(scriptContext),
  55. m_token(), // should initialize to 0/nullptrs
  56. m_scan(this, &m_token, scriptContext),
  57. m_currentNodeNonLambdaFunc(nullptr),
  58. m_currentNodeNonLambdaDeferredFunc(nullptr),
  59. m_currentNodeFunc(nullptr),
  60. m_currentNodeDeferredFunc(nullptr),
  61. m_currentNodeProg(nullptr),
  62. m_currDeferredStub(nullptr),
  63. m_currDeferredStubCount(0),
  64. m_pCurrentAstSize(nullptr),
  65. m_ppnodeScope(nullptr),
  66. m_ppnodeExprScope(nullptr),
  67. m_ppnodeVar(nullptr),
  68. m_inDeferredNestedFunc(false),
  69. m_reparsingLambdaParams(false),
  70. m_disallowImportExportStmt(false),
  71. m_isInParsingArgList(false),
  72. m_hasDestructuringPattern(false),
  73. m_hasDeferredShorthandInitError(false),
  74. m_deferCommaError(false),
  75. m_pnestedCount(nullptr),
  76. wellKnownPropertyPids(), // should initialize to nullptrs
  77. m_sourceLim(0),
  78. m_functionBody(nullptr),
  79. m_parseType(ParseType_Upfront),
  80. m_arrayDepth(0),
  81. m_funcInArrayDepth(0),
  82. m_funcInArray(0),
  83. m_scopeCountNoAst(0),
  84. m_funcParenExprDepth(0),
  85. m_deferEllipsisError(false),
  86. m_deferEllipsisErrorLoc(), // calls default initializer
  87. m_deferCommaErrorLoc(),
  88. m_tryCatchOrFinallyDepth(0),
  89. m_pstmtCur(nullptr),
  90. m_currentBlockInfo(nullptr),
  91. m_currentScope(nullptr),
  92. currBackgroundParseItem(nullptr),
  93. backgroundParseItems(nullptr),
  94. fastScannedRegExpNodes(nullptr),
  95. m_currentDynamicBlock(nullptr),
  96. m_UsesArgumentsAtGlobal(false),
  97. m_fUseStrictMode(strictMode),
  98. m_InAsmMode(false),
  99. m_deferAsmJs(true),
  100. m_fExpectExternalSource(FALSE),
  101. m_deferringAST(FALSE),
  102. m_stoppedDeferredParse(FALSE)
  103. {
  104. AssertMsg(size == sizeof(Parser), "verify conditionals affecting the size of Parser agree");
  105. Assert(scriptContext != nullptr);
  106. // init PID members
  107. InitPids();
  108. }
  109. Parser::~Parser(void)
  110. {
  111. this->ReleaseTemporaryGuestArena();
  112. #if ENABLE_BACKGROUND_PARSING
  113. if (this->m_hasParallelJob)
  114. {
  115. // Let the background threads know that they can decommit their arena pages.
  116. BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
  117. Assert(bgp);
  118. if (bgp->Processor()->ProcessesInBackground())
  119. {
  120. JsUtil::BackgroundJobProcessor *processor = static_cast<JsUtil::BackgroundJobProcessor*>(bgp->Processor());
  121. bool result = processor->IterateBackgroundThreads([&](JsUtil::ParallelThreadData *threadData)->bool {
  122. threadData->canDecommit = true;
  123. return false;
  124. });
  125. Assert(result);
  126. }
  127. }
  128. #endif
  129. }
  130. void Parser::OutOfMemory()
  131. {
  132. throw ParseExceptionObject(ERRnoMemory);
  133. }
  134. LPCWSTR Parser::GetTokenString(tokens token)
  135. {
  136. switch (token)
  137. {
  138. case tkNone : return _u("");
  139. case tkEOF : return _u("end of script");
  140. case tkIntCon : return _u("integer literal");
  141. case tkFltCon : return _u("float literal");
  142. case tkStrCon : return _u("string literal");
  143. case tkRegExp : return _u("regular expression literal");
  144. // keywords
  145. case tkABSTRACT : return _u("abstract");
  146. case tkASSERT : return _u("assert");
  147. case tkAWAIT : return _u("await");
  148. case tkBOOLEAN : return _u("boolean");
  149. case tkBREAK : return _u("break");
  150. case tkBYTE : return _u("byte");
  151. case tkCASE : return _u("case");
  152. case tkCATCH : return _u("catch");
  153. case tkCHAR : return _u("char");
  154. case tkCONTINUE : return _u("continue");
  155. case tkDEBUGGER : return _u("debugger");
  156. case tkDECIMAL : return _u("decimal");
  157. case tkDEFAULT : return _u("default");
  158. case tkDELETE : return _u("delete");
  159. case tkDO : return _u("do");
  160. case tkDOUBLE : return _u("double");
  161. case tkELSE : return _u("else");
  162. case tkENSURE : return _u("ensure");
  163. case tkEVENT : return _u("event");
  164. case tkFALSE : return _u("false");
  165. case tkFINAL : return _u("final");
  166. case tkFINALLY : return _u("finally");
  167. case tkFLOAT : return _u("float");
  168. case tkFOR : return _u("for");
  169. case tkFUNCTION : return _u("function");
  170. case tkGET : return _u("get");
  171. case tkGOTO : return _u("goto");
  172. case tkIF : return _u("if");
  173. case tkIN : return _u("in");
  174. case tkINSTANCEOF : return _u("instanceof");
  175. case tkINT : return _u("int");
  176. case tkINTERNAL : return _u("internal");
  177. case tkINVARIANT : return _u("invariant");
  178. case tkLONG : return _u("long");
  179. case tkNAMESPACE : return _u("namespace");
  180. case tkNATIVE : return _u("native");
  181. case tkNEW : return _u("new");
  182. case tkNULL : return _u("null");
  183. case tkREQUIRE : return _u("require");
  184. case tkRETURN : return _u("return");
  185. case tkSBYTE : return _u("sbyte");
  186. case tkSET : return _u("set");
  187. case tkSHORT : return _u("short");
  188. case tkSWITCH : return _u("switch");
  189. case tkSYNCHRONIZED : return _u("synchronized");
  190. case tkTHIS : return _u("this");
  191. case tkTHROW : return _u("throw");
  192. case tkTHROWS : return _u("throws");
  193. case tkTRANSIENT : return _u("transient");
  194. case tkTRUE : return _u("true");
  195. case tkTRY : return _u("try");
  196. case tkTYPEOF : return _u("typeof");
  197. case tkUINT : return _u("uint");
  198. case tkULONG : return _u("ulong");
  199. case tkUSE : return _u("use");
  200. case tkUSHORT : return _u("ushort");
  201. case tkVAR : return _u("var");
  202. case tkVOID : return _u("void");
  203. case tkVOLATILE : return _u("volatile");
  204. case tkWHILE : return _u("while");
  205. case tkWITH : return _u("with");
  206. // Future reserved words that become keywords in ES6
  207. case tkCLASS : return _u("class");
  208. case tkCONST : return _u("const");
  209. case tkEXPORT : return _u("export");
  210. case tkEXTENDS : return _u("extends");
  211. case tkIMPORT : return _u("import");
  212. case tkLET : return _u("let");
  213. case tkSUPER : return _u("super");
  214. case tkYIELD : return _u("yield");
  215. // Future reserved words in strict and non-strict modes
  216. case tkENUM : return _u("enum");
  217. // Additional future reserved words in strict mode
  218. case tkIMPLEMENTS : return _u("implements");
  219. case tkINTERFACE : return _u("interface");
  220. case tkPACKAGE : return _u("package");
  221. case tkPRIVATE : return _u("private");
  222. case tkPROTECTED : return _u("protected");
  223. case tkPUBLIC : return _u("public");
  224. case tkSTATIC : return _u("static");
  225. case tkID: return _u("identifier");
  226. // Non-operator non-identifier tokens
  227. case tkSColon: return _u(";");
  228. case tkRParen: return _u(")");
  229. case tkRBrack: return _u("]");
  230. case tkLCurly: return _u("{");
  231. case tkRCurly: return _u("}");
  232. // Operator non-identifier tokens
  233. case tkComma: return _u(",");
  234. case tkDArrow: return _u("=>");
  235. case tkAsg: return _u("=");
  236. case tkAsgAdd: return _u("+=");
  237. case tkAsgSub: return _u("-=");
  238. case tkAsgMul: return _u("*=");
  239. case tkAsgDiv: return _u("/=");
  240. case tkAsgExpo: return _u("**=");
  241. case tkAsgMod: return _u("%=");
  242. case tkAsgAnd: return _u("&=");
  243. case tkAsgXor: return _u("^=");
  244. case tkAsgOr: return _u("|=");
  245. case tkAsgLsh: return _u("<<=");
  246. case tkAsgRsh: return _u(">>=");
  247. case tkAsgRs2: return _u(">>>=");
  248. case tkQMark: return _u("?");
  249. case tkColon: return _u(":");
  250. case tkLogOr: return _u("||");
  251. case tkLogAnd: return _u("&&");
  252. case tkCoalesce: return _u("??");
  253. case tkOr: return _u("|");
  254. case tkXor: return _u("^");
  255. case tkAnd: return _u("&");
  256. case tkEQ: return _u("==");
  257. case tkNE: return _u("!=");
  258. case tkEqv: return _u("===");
  259. case tkNEqv: return _u("!==");
  260. case tkLT: return _u("<");
  261. case tkLE: return _u("<=");
  262. case tkGT: return _u(">");
  263. case tkGE: return _u(">=");
  264. case tkLsh: return _u("<<");
  265. case tkRsh: return _u(">>");
  266. case tkRs2: return _u(">>>");
  267. case tkAdd: return _u("+");
  268. case tkSub: return _u("-");
  269. case tkExpo: return _u("**");
  270. case tkStar: return _u("*");
  271. case tkDiv: return _u("/");
  272. case tkPct: return _u("%");
  273. case tkTilde: return _u("~");
  274. case tkBang: return _u("!");
  275. case tkInc: return _u("++");
  276. case tkDec: return _u("--");
  277. case tkEllipsis: return _u("...");
  278. case tkLParen: return _u("(");
  279. case tkLBrack: return _u("[");
  280. case tkDot: return _u(".");
  281. default:
  282. return _u("unknown token");
  283. }
  284. }
  285. void Parser::Error(HRESULT hr, LPCWSTR stringOne, LPCWSTR stringTwo)
  286. {
  287. throw ParseExceptionObject(hr, stringOne, stringTwo);
  288. }
  289. void Parser::Error(HRESULT hr, ParseNodePtr pnode)
  290. {
  291. if (pnode && pnode->ichLim)
  292. {
  293. Error(hr, pnode->ichMin, pnode->ichLim);
  294. }
  295. else
  296. {
  297. Error(hr);
  298. }
  299. }
  300. void Parser::Error(HRESULT hr, charcount_t ichMin, charcount_t ichLim, LPCWSTR stringOne, LPCWSTR stringTwo)
  301. {
  302. this->GetScanner()->SetErrorPosition(ichMin, ichLim);
  303. Error(hr, stringOne, stringTwo);
  304. }
  305. void Parser::IdentifierExpectedError(const Token& token)
  306. {
  307. Assert(token.tk != tkID);
  308. HRESULT hr;
  309. if (token.IsReservedWord())
  310. {
  311. if (token.IsKeyword())
  312. {
  313. hr = ERRKeywordNotId;
  314. }
  315. else
  316. {
  317. Assert(token.IsFutureReservedWord(true));
  318. if (token.IsFutureReservedWord(false))
  319. {
  320. // Future reserved word in strict and non-strict modes
  321. hr = ERRFutureReservedWordNotId;
  322. }
  323. else
  324. {
  325. // Future reserved word only in strict mode. The token would have been converted to tkID by the scanner if not
  326. // in strict mode.
  327. Assert(IsStrictMode());
  328. hr = ERRFutureReservedWordInStrictModeNotId;
  329. }
  330. }
  331. }
  332. else
  333. {
  334. hr = ERRnoIdent;
  335. }
  336. Error(hr);
  337. }
  338. HRESULT Parser::ValidateSyntax(LPCUTF8 pszSrc, size_t encodedCharCount, bool isGenerator, bool isAsync, CompileScriptException *pse, void (Parser::*validateFunction)())
  339. {
  340. Assert(pszSrc);
  341. PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
  342. HRESULT hr;
  343. SmartFPUControl smartFpuControl;
  344. bool handled = false;
  345. BOOL fDeferSave = m_deferringAST;
  346. try
  347. {
  348. hr = NOERROR;
  349. m_length = encodedCharCount;
  350. m_originalLength = encodedCharCount;
  351. // make sure deferred parsing is turned off
  352. ULONG grfscr = fscrNil;
  353. // Give the scanner the source and get the first token
  354. this->GetScanner()->SetText(pszSrc, 0, encodedCharCount, 0, false, grfscr);
  355. this->GetScanner()->SetYieldIsKeywordRegion(isGenerator);
  356. this->GetScanner()->SetAwaitIsKeywordRegion(isAsync);
  357. this->GetScanner()->Scan();
  358. uint nestedCount = 0;
  359. m_pnestedCount = &nestedCount;
  360. ParseNodePtr pnodeScope = nullptr;
  361. m_ppnodeScope = &pnodeScope;
  362. m_ppnodeExprScope = nullptr;
  363. uint nextFunctionId = 0;
  364. m_nextFunctionId = &nextFunctionId;
  365. m_inDeferredNestedFunc = false;
  366. m_deferringAST = true;
  367. m_nextBlockId = 0;
  368. ParseNodeFnc *pnodeFnc = CreateAllowDeferNodeForOpT<knopFncDecl>();
  369. pnodeFnc->SetIsGenerator(isGenerator);
  370. pnodeFnc->SetIsAsync(isAsync);
  371. m_ppnodeVar = &pnodeFnc->pnodeVars;
  372. m_currentNodeFunc = pnodeFnc;
  373. m_currentNodeDeferredFunc = NULL;
  374. m_sourceContextInfo = nullptr;
  375. AssertMsg(m_pstmtCur == NULL, "Statement stack should be empty when we start parse function body");
  376. ParseNodeBlock * block = StartParseBlock<false>(PnodeBlockType::Function, ScopeType_FunctionBody);
  377. (this->*validateFunction)();
  378. FinishParseBlock(block);
  379. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  380. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  381. pnodeFnc->pnodeVars = nullptr;
  382. // there should be nothing after successful parsing for a given construct
  383. if (m_token.tk != tkEOF)
  384. Error(ERRsyntax);
  385. m_deferringAST = fDeferSave;
  386. }
  387. catch (ParseExceptionObject& e)
  388. {
  389. m_deferringAST = fDeferSave;
  390. hr = e.GetError();
  391. hr = pse->ProcessError(this->GetScanner(), hr, /* pnodeBase */ NULL, e.GetStringOne(), e.GetStringTwo());
  392. handled = true;
  393. }
  394. if (handled == false && nullptr != pse && FAILED(hr))
  395. {
  396. hr = pse->ProcessError(this->GetScanner(), hr, /* pnodeBase */ NULL);
  397. }
  398. return hr;
  399. }
  400. HRESULT Parser::ParseSourceInternal(
  401. __out ParseNodeProg ** parseTree, LPCUTF8 pszSrc, size_t offsetInBytes, size_t encodedCharCount, charcount_t offsetInChars,
  402. bool isUtf8, ULONG grfscr, CompileScriptException *pse, Js::LocalFunctionId * nextFunctionId, ULONG lineNumber, SourceContextInfo * sourceContextInfo)
  403. {
  404. Assert(parseTree);
  405. Assert(pszSrc);
  406. if (this->IsBackgroundParser())
  407. {
  408. PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
  409. }
  410. else
  411. {
  412. PROBE_STACK(m_scriptContext, Js::Constants::MinStackDefault);
  413. }
  414. #ifdef PROFILE_EXEC
  415. m_scriptContext->ProfileBegin(Js::ParsePhase);
  416. #endif
  417. JS_ETW_INTERNAL(EventWriteJSCRIPT_PARSE_START(m_scriptContext, 0));
  418. *parseTree = NULL;
  419. m_sourceLim = 0;
  420. m_grfscr = grfscr;
  421. m_sourceContextInfo = sourceContextInfo;
  422. ParseNodeProg * pnodeBase = NULL;
  423. HRESULT hr;
  424. SmartFPUControl smartFpuControl;
  425. bool handled = false;
  426. try
  427. {
  428. if ((grfscr & fscrIsModuleCode) != 0)
  429. {
  430. // Module source flag should not be enabled unless module is enabled
  431. Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
  432. // Module code is always strict mode code.
  433. this->m_fUseStrictMode = TRUE;
  434. }
  435. if ((grfscr & fscrUseStrictMode) != 0)
  436. {
  437. this->m_fUseStrictMode = TRUE;
  438. }
  439. // parse the source
  440. pnodeBase = Parse(pszSrc, offsetInBytes, encodedCharCount, offsetInChars, isUtf8, grfscr, lineNumber, nextFunctionId, pse);
  441. Assert(pnodeBase);
  442. // Record the actual number of words parsed.
  443. m_sourceLim = pnodeBase->ichLim - offsetInChars;
  444. // TODO: The assert can be false positive in some scenarios and chuckj to fix it later
  445. // Assert(utf8::ByteIndexIntoCharacterIndex(pszSrc + offsetInBytes, encodedCharCount, isUtf8 ? utf8::doDefault : utf8::doAllowThreeByteSurrogates) == m_sourceLim);
  446. #if DBG_DUMP
  447. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::ParsePhase))
  448. {
  449. PrintPnodeWIndent(pnodeBase, 4);
  450. fflush(stdout);
  451. }
  452. #endif
  453. *parseTree = pnodeBase;
  454. hr = NOERROR;
  455. }
  456. catch (ParseExceptionObject& e)
  457. {
  458. hr = e.GetError();
  459. hr = pse->ProcessError(this->GetScanner(), hr, pnodeBase, e.GetStringOne(), e.GetStringTwo());
  460. handled = true;
  461. }
  462. catch (Js::AsmJsParseException&)
  463. {
  464. hr = JSERR_AsmJsCompileError;
  465. }
  466. if (handled == false && FAILED(hr))
  467. {
  468. hr = pse->ProcessError(this->GetScanner(), hr, pnodeBase);
  469. }
  470. #if ENABLE_BACKGROUND_PARSING
  471. if (this->m_hasParallelJob)
  472. {
  473. ///// Wait here for remaining jobs to finish. Then look for errors, do final const bindings.
  474. // pleath TODO: If there are remaining jobs, let the main thread help finish them.
  475. BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
  476. Assert(bgp);
  477. CompileScriptException se;
  478. this->WaitForBackgroundJobs(bgp, &se);
  479. BackgroundParseItem *failedItem = bgp->GetFailedBackgroundParseItem();
  480. if (failedItem)
  481. {
  482. CompileScriptException *bgPse = failedItem->GetPSE();
  483. Assert(bgPse);
  484. *pse = *bgPse;
  485. hr = failedItem->GetHR();
  486. bgp->SetFailedBackgroundParseItem(nullptr);
  487. }
  488. if (this->fastScannedRegExpNodes != nullptr)
  489. {
  490. this->FinishBackgroundRegExpNodes();
  491. }
  492. for (BackgroundParseItem *item = this->backgroundParseItems; item; item = item->GetNext())
  493. {
  494. Parser *parser = item->GetParser();
  495. parser->FinishBackgroundPidRefs(item, this != parser);
  496. }
  497. }
  498. #endif
  499. // done with the scanner
  500. this->GetScanner()->Clear();
  501. #ifdef PROFILE_EXEC
  502. m_scriptContext->ProfileEnd(Js::ParsePhase);
  503. #endif
  504. JS_ETW_INTERNAL(EventWriteJSCRIPT_PARSE_STOP(m_scriptContext, 0));
  505. return hr;
  506. }
  507. #if ENABLE_BACKGROUND_PARSING
  508. void Parser::WaitForBackgroundJobs(BackgroundParser *bgp, CompileScriptException *pse)
  509. {
  510. // The scan of the script is done, but there may be unfinished background jobs in the queue.
  511. // Enlist the main thread to help with those.
  512. BackgroundParseItem *item;
  513. if (!*bgp->GetPendingBackgroundItemsPtr())
  514. {
  515. // We're done.
  516. return;
  517. }
  518. // Save parser state, since we'll need to restore it in order to bind references correctly later.
  519. this->m_isInBackground = true;
  520. this->SetCurrBackgroundParseItem(nullptr);
  521. uint blockIdSave = this->m_nextBlockId;
  522. uint functionIdSave = *this->m_nextFunctionId;
  523. StmtNest *pstmtSave = this->m_pstmtCur;
  524. if (!bgp->Processor()->ProcessesInBackground())
  525. {
  526. // No background thread. Just walk the jobs with no locking and process them.
  527. for (item = bgp->GetNextUnprocessedItem(); item; item = bgp->GetNextUnprocessedItem())
  528. {
  529. bgp->Processor()->RemoveJob(item);
  530. bool succeeded = bgp->Process(item, this, pse);
  531. bgp->JobProcessed(item, succeeded);
  532. }
  533. Assert(!*bgp->GetPendingBackgroundItemsPtr());
  534. }
  535. else
  536. {
  537. // Background threads. We need to have the critical section in order to:
  538. // - Check for unprocessed jobs;
  539. // - Remove jobs from the processor queue;
  540. // - Do JobsProcessed work (such as removing jobs from the BackgroundParser's unprocessed list).
  541. CriticalSection *pcs = static_cast<JsUtil::BackgroundJobProcessor*>(bgp->Processor())->GetCriticalSection();
  542. pcs->Enter();
  543. for (;;)
  544. {
  545. // Grab a job (in lock)
  546. item = bgp->GetNextUnprocessedItem();
  547. if (item == nullptr)
  548. {
  549. break;
  550. }
  551. bgp->Processor()->RemoveJob(item);
  552. pcs->Leave();
  553. // Process job (if there is one) (outside lock)
  554. bool succeeded = bgp->Process(item, this, pse);
  555. pcs->Enter();
  556. bgp->JobProcessed(item, succeeded);
  557. }
  558. pcs->Leave();
  559. // Wait for the background threads to finish jobs they're already processing (if any).
  560. // TODO: Replace with a proper semaphore.
  561. while (*bgp->GetPendingBackgroundItemsPtr());
  562. }
  563. Assert(!*bgp->GetPendingBackgroundItemsPtr());
  564. // Restore parser state.
  565. this->m_pstmtCur = pstmtSave;
  566. this->m_isInBackground = false;
  567. this->m_nextBlockId = blockIdSave;
  568. *this->m_nextFunctionId = functionIdSave;
  569. }
  570. void Parser::FinishBackgroundPidRefs(BackgroundParseItem *item, bool isOtherParser)
  571. {
  572. for (BlockInfoStack *blockInfo = item->GetParseContext()->currentBlockInfo; blockInfo; blockInfo = blockInfo->pBlockInfoOuter)
  573. {
  574. if (isOtherParser)
  575. {
  576. this->BindPidRefs<true>(blockInfo, item->GetMaxBlockId());
  577. }
  578. else
  579. {
  580. this->BindPidRefs<false>(blockInfo, item->GetMaxBlockId());
  581. }
  582. }
  583. }
  584. void Parser::FinishBackgroundRegExpNodes()
  585. {
  586. // We have a list of RegExp nodes that we saw on the UI thread in functions we're parallel parsing,
  587. // and for each background job we have a list of RegExp nodes for which we couldn't allocate patterns.
  588. // We need to copy the pattern pointers from the UI thread nodes to the corresponding nodes on the
  589. // background nodes.
  590. // There may be UI thread nodes for which there are no background thread equivalents, because the UI thread
  591. // has to assume that the background thread won't defer anything.
  592. // Note that because these lists (and the list of background jobs) are SList's built by prepending, they are
  593. // all in reverse lexical order.
  594. Assert(!this->IsBackgroundParser());
  595. Assert(this->fastScannedRegExpNodes);
  596. Assert(this->backgroundParseItems != nullptr);
  597. BackgroundParseItem *currBackgroundItem;
  598. #if DBG
  599. for (currBackgroundItem = this->backgroundParseItems;
  600. currBackgroundItem;
  601. currBackgroundItem = currBackgroundItem->GetNext())
  602. {
  603. if (currBackgroundItem->RegExpNodeList())
  604. {
  605. FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnode, currBackgroundItem->RegExpNodeList())
  606. {
  607. Assert(pnode->AsParseNodeRegExp()->regexPattern == nullptr);
  608. }
  609. NEXT_DLIST_ENTRY;
  610. }
  611. }
  612. #endif
  613. // Hook up the patterns allocated on the main thread to the nodes created on the background thread.
  614. // Walk the list of foreground nodes, advancing through the work items and looking up each item.
  615. // Note that the background thread may have chosen to defer a given RegEx literal, so not every foreground
  616. // node will have a matching background node. Doesn't matter for correctness.
  617. // (It's inefficient, of course, to have to restart the inner loop from the beginning of the work item's
  618. // list, but it should be unusual to have many RegExes in a single work item's chunk of code. Figure out how
  619. // to start the inner loop from a known internal node within the list if that turns out to be important.)
  620. currBackgroundItem = this->backgroundParseItems;
  621. FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnodeFgnd, this->fastScannedRegExpNodes)
  622. {
  623. Assert(pnodeFgnd->nop == knopRegExp);
  624. Assert(pnodeFgnd->AsParseNodeRegExp()->regexPattern != nullptr);
  625. bool quit = false;
  626. while (!quit)
  627. {
  628. // Find the next work item with a RegEx in it.
  629. while (currBackgroundItem && currBackgroundItem->RegExpNodeList() == nullptr)
  630. {
  631. currBackgroundItem = currBackgroundItem->GetNext();
  632. }
  633. if (!currBackgroundItem)
  634. {
  635. break;
  636. }
  637. // Walk the RegExps in the work item.
  638. FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnodeBgnd, currBackgroundItem->RegExpNodeList())
  639. {
  640. Assert(pnodeBgnd->nop == knopRegExp);
  641. if (pnodeFgnd->ichMin <= pnodeBgnd->ichMin)
  642. {
  643. // Either we found a match, or the next background node is past the foreground node.
  644. // In any case, we can stop searching.
  645. if (pnodeFgnd->ichMin == pnodeBgnd->ichMin)
  646. {
  647. Assert(pnodeFgnd->ichLim == pnodeBgnd->ichLim);
  648. pnodeBgnd->AsParseNodeRegExp()->regexPattern = pnodeFgnd->AsParseNodeRegExp()->regexPattern;
  649. }
  650. quit = true;
  651. break;
  652. }
  653. }
  654. NEXT_DLIST_ENTRY;
  655. if (!quit)
  656. {
  657. // Need to advance to the next work item.
  658. currBackgroundItem = currBackgroundItem->GetNext();
  659. }
  660. }
  661. }
  662. NEXT_DLIST_ENTRY;
  663. #if DBG
  664. for (currBackgroundItem = this->backgroundParseItems;
  665. currBackgroundItem;
  666. currBackgroundItem = currBackgroundItem->GetNext())
  667. {
  668. if (currBackgroundItem->RegExpNodeList())
  669. {
  670. FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnode, currBackgroundItem->RegExpNodeList())
  671. {
  672. Assert(pnode->AsParseNodeRegExp()->regexPattern != nullptr);
  673. }
  674. NEXT_DLIST_ENTRY;
  675. }
  676. }
  677. #endif
  678. }
  679. #endif
  680. LabelId* Parser::CreateLabelId(IdentPtr pid)
  681. {
  682. LabelId* pLabelId;
  683. pLabelId = (LabelId*)m_nodeAllocator.Alloc(sizeof(LabelId));
  684. if (NULL == pLabelId)
  685. Error(ERRnoMemory);
  686. pLabelId->pid = pid;
  687. pLabelId->next = NULL;
  688. return pLabelId;
  689. }
  690. /*****************************************************************************
  691. The following set of routines allocate parse tree nodes of various kinds.
  692. They catch an exception on out of memory.
  693. *****************************************************************************/
  694. void
  695. Parser::AddAstSize(int size)
  696. {
  697. Assert(!this->m_deferringAST);
  698. Assert(m_pCurrentAstSize != NULL);
  699. *m_pCurrentAstSize += size;
  700. }
  701. void
  702. Parser::AddAstSizeAllowDefer(int size)
  703. {
  704. if (!this->m_deferringAST)
  705. {
  706. AddAstSize(size);
  707. }
  708. }
  709. // StaticCreate
  710. ParseNodeVar * Parser::StaticCreateTempNode(ParseNode* initExpr, ArenaAllocator * alloc)
  711. {
  712. ParseNodeVar * pnode = Anew(alloc, ParseNodeVar, knopTemp, 0, 0, nullptr);
  713. pnode->pnodeInit = initExpr;
  714. return pnode;
  715. }
  716. ParseNodeUni * Parser::StaticCreateTempRef(ParseNode* tempNode, ArenaAllocator * alloc)
  717. {
  718. return Anew(alloc, ParseNodeUni, knopTempRef, 0, 0, tempNode);
  719. }
  720. // Create Node with limit
  721. template <OpCode nop>
  722. typename OpCodeTrait<nop>::ParseNodeType * Parser::CreateNodeForOpT(charcount_t ichMin, charcount_t ichLim)
  723. {
  724. Assert(!this->m_deferringAST);
  725. typename OpCodeTrait<nop>::ParseNodeType * pnode = StaticCreateNodeT<nop>(&m_nodeAllocator, ichMin, ichLim);
  726. AddAstSize(sizeof(typename OpCodeTrait<nop>::ParseNodeType));
  727. return pnode;
  728. }
  729. template <OpCode nop>
  730. typename OpCodeTrait<nop>::ParseNodeType * Parser::CreateAllowDeferNodeForOpT(charcount_t ichMin, charcount_t ichLim)
  731. {
  732. CompileAssert(OpCodeTrait<nop>::AllowDefer);
  733. typename OpCodeTrait<nop>::ParseNodeType * pnode = StaticCreateNodeT<nop>(&m_nodeAllocator, ichMin, ichLim);
  734. AddAstSizeAllowDefer(sizeof(typename OpCodeTrait<nop>::ParseNodeType));
  735. return pnode;
  736. }
  737. #if DBG
  738. static const int g_mpnopcbNode[] =
  739. {
  740. #define PTNODE(nop,sn,pc,nk,ok,json) sizeof(ParseNode##nk),
  741. #include "ptlist.h"
  742. };
  743. void VerifyNodeSize(OpCode nop, int size)
  744. {
  745. Assert(nop >= 0 && nop < knopLim);
  746. __analysis_assume(nop < knopLim);
  747. Assert(g_mpnopcbNode[nop] == size);
  748. }
  749. #endif
  750. // Create ParseNodeUni
  751. ParseNodeUni * Parser::CreateUniNode(OpCode nop, ParseNodePtr pnode1)
  752. {
  753. charcount_t ichMin;
  754. charcount_t ichLim;
  755. if (nullptr == pnode1)
  756. {
  757. // no ops
  758. ichMin = this->GetScanner()->IchMinTok();
  759. ichLim = this->GetScanner()->IchLimTok();
  760. }
  761. else
  762. {
  763. // 1 op
  764. ichMin = pnode1->ichMin;
  765. ichLim = pnode1->ichLim;
  766. this->CheckArguments(pnode1);
  767. }
  768. return CreateUniNode(nop, pnode1, ichMin, ichLim);
  769. }
  770. ParseNodeUni * Parser::CreateUniNode(OpCode nop, ParseNodePtr pnode1, charcount_t ichMin, charcount_t ichLim)
  771. {
  772. Assert(!this->m_deferringAST);
  773. DebugOnly(VerifyNodeSize(nop, sizeof(ParseNodeUni)));
  774. ParseNodeUni * pnode = Anew(&m_nodeAllocator, ParseNodeUni, nop, ichMin, ichLim, pnode1);
  775. AddAstSize(sizeof(ParseNodeUni));
  776. return pnode;
  777. }
  778. // Create ParseNodeBin
  779. ParseNodeBin * Parser::StaticCreateBinNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2, ArenaAllocator* alloc, charcount_t ichMin, charcount_t ichLim)
  780. {
  781. DebugOnly(VerifyNodeSize(nop, sizeof(ParseNodeBin)));
  782. return Anew(alloc, ParseNodeBin, nop, ichMin, ichLim, pnode1, pnode2);
  783. }
  784. ParseNodeBin * Parser::CreateBinNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2)
  785. {
  786. Assert(!this->m_deferringAST);
  787. charcount_t ichMin;
  788. charcount_t ichLim;
  789. if (nullptr == pnode1)
  790. {
  791. // no ops
  792. Assert(nullptr == pnode2);
  793. ichMin = this->GetScanner()->IchMinTok();
  794. ichLim = this->GetScanner()->IchLimTok();
  795. }
  796. else
  797. {
  798. if (nullptr == pnode2)
  799. {
  800. // 1 op
  801. ichMin = pnode1->ichMin;
  802. ichLim = pnode1->ichLim;
  803. }
  804. else
  805. {
  806. // 2 ops
  807. ichMin = pnode1->ichMin;
  808. ichLim = pnode2->ichLim;
  809. if (nop != knopDot && nop != knopIndex)
  810. {
  811. this->CheckArguments(pnode2);
  812. }
  813. }
  814. if (nop != knopDot && nop != knopIndex)
  815. {
  816. this->CheckArguments(pnode1);
  817. }
  818. }
  819. return CreateBinNode(nop, pnode1, pnode2, ichMin, ichLim);
  820. }
  821. ParseNodeBin * Parser::CreateBinNode(OpCode nop, ParseNodePtr pnode1,
  822. ParseNodePtr pnode2, charcount_t ichMin, charcount_t ichLim)
  823. {
  824. Assert(!this->m_deferringAST);
  825. ParseNodeBin * pnode = StaticCreateBinNode(nop, pnode1, pnode2, &m_nodeAllocator, ichMin, ichLim);
  826. AddAstSize(sizeof(ParseNodeBin));
  827. return pnode;
  828. }
  829. // Create ParseNodeTri
  830. ParseNodeTri * Parser::CreateTriNode(OpCode nop, ParseNodePtr pnode1,
  831. ParseNodePtr pnode2, ParseNodePtr pnode3)
  832. {
  833. charcount_t ichMin;
  834. charcount_t ichLim;
  835. if (nullptr == pnode1)
  836. {
  837. // no ops
  838. Assert(nullptr == pnode2);
  839. Assert(nullptr == pnode3);
  840. ichMin = this->GetScanner()->IchMinTok();
  841. ichLim = this->GetScanner()->IchLimTok();
  842. }
  843. else if (nullptr == pnode2)
  844. {
  845. // 1 op
  846. Assert(nullptr == pnode3);
  847. ichMin = pnode1->ichMin;
  848. ichLim = pnode1->ichLim;
  849. }
  850. else if (nullptr == pnode3)
  851. {
  852. // 2 op
  853. ichMin = pnode1->ichMin;
  854. ichLim = pnode2->ichLim;
  855. }
  856. else
  857. {
  858. // 3 ops
  859. ichMin = pnode1->ichMin;
  860. ichLim = pnode3->ichLim;
  861. }
  862. return CreateTriNode(nop, pnode1, pnode2, pnode3, ichMin, ichLim);
  863. }
  864. ParseNodeTri * Parser::CreateTriNode(OpCode nop, ParseNodePtr pnode1,
  865. ParseNodePtr pnode2, ParseNodePtr pnode3,
  866. charcount_t ichMin, charcount_t ichLim)
  867. {
  868. Assert(!this->m_deferringAST);
  869. DebugOnly(VerifyNodeSize(nop, sizeof(ParseNodeTri)));
  870. ParseNodeTri * pnode = Anew(&m_nodeAllocator, ParseNodeTri, nop, ichMin, ichLim);
  871. AddAstSize(sizeof(ParseNodeTri));
  872. pnode->pnode1 = pnode1;
  873. pnode->pnode2 = pnode2;
  874. pnode->pnode3 = pnode3;
  875. return pnode;
  876. }
  877. // Create ParseNodeBlock
  878. ParseNodeBlock *
  879. Parser::StaticCreateBlockNode(ArenaAllocator* alloc, charcount_t ichMin, charcount_t ichLim, int blockId, PnodeBlockType blockType)
  880. {
  881. return Anew(alloc, ParseNodeBlock, ichMin, ichLim, blockId, blockType);
  882. }
  883. ParseNodeBlock * Parser::CreateBlockNode(PnodeBlockType blockType)
  884. {
  885. return CreateBlockNode(this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok(), blockType);
  886. }
  887. ParseNodeBlock * Parser::CreateBlockNode(charcount_t ichMin, charcount_t ichLim, PnodeBlockType blockType)
  888. {
  889. Assert(OpCodeTrait<knopBlock>::AllowDefer);
  890. ParseNodeBlock * pnode = StaticCreateBlockNode(&m_nodeAllocator, ichMin, ichLim, this->m_nextBlockId++, blockType);
  891. AddAstSizeAllowDefer(sizeof(ParseNodeBlock));
  892. return pnode;
  893. }
  894. // Create ParseNodeVar
  895. ParseNodeVar * Parser::CreateDeclNode(OpCode nop, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl)
  896. {
  897. Assert(nop == knopVarDecl || nop == knopLetDecl || nop == knopConstDecl);
  898. ParseNodeVar * pnode = Anew(&m_nodeAllocator, ParseNodeVar, nop, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok(), pid);
  899. if (symbolType != STUnknown)
  900. {
  901. pnode->sym = AddDeclForPid(pnode, pid, symbolType, errorOnRedecl);
  902. }
  903. return pnode;
  904. }
  905. ParseNodeInt * Parser::CreateIntNode(int32 lw)
  906. {
  907. Assert(!this->m_deferringAST);
  908. ParseNodeInt * pnode = Anew(&m_nodeAllocator, ParseNodeInt, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok(), lw);
  909. AddAstSize(sizeof(ParseNodeInt));
  910. return pnode;
  911. }
  912. ParseNodeStr * Parser::CreateStrNode(IdentPtr pid)
  913. {
  914. Assert(!this->m_deferringAST);
  915. ParseNodeStr * pnode = Anew(&m_nodeAllocator, ParseNodeStr, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok(), pid);
  916. pnode->grfpn |= PNodeFlags::fpnCanFlattenConcatExpr;
  917. AddAstSize(sizeof(ParseNodeStr));
  918. return pnode;
  919. }
  920. ParseNodeBigInt * Parser::CreateBigIntNode(IdentPtr pid)
  921. {
  922. Assert(!this->m_deferringAST);
  923. ParseNodeBigInt * pnode = Anew(&m_nodeAllocator, ParseNodeBigInt, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok(), pid);
  924. pnode->isNegative = false;
  925. AddAstSize(sizeof(ParseNodeBigInt));
  926. return pnode;
  927. }
  928. ParseNodeName * Parser::CreateNameNode(IdentPtr pid)
  929. {
  930. ParseNodeName * pnode = Anew(&m_nodeAllocator, ParseNodeName, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok(), pid);
  931. AddAstSizeAllowDefer(sizeof(ParseNodeName));
  932. return pnode;
  933. }
  934. ParseNodeName * Parser::CreateNameNode(IdentPtr pid, PidRefStack * ref, charcount_t ichMin, charcount_t ichLim)
  935. {
  936. ParseNodeName * pnode = Anew(&m_nodeAllocator, ParseNodeName, ichMin, ichLim, pid);
  937. pnode->SetSymRef(ref);
  938. AddAstSize(sizeof(ParseNodeName));
  939. return pnode;
  940. }
  941. ParseNodeSpecialName * Parser::CreateSpecialNameNode(IdentPtr pid, PidRefStack * ref, charcount_t ichMin, charcount_t ichLim)
  942. {
  943. Assert(!this->m_deferringAST);
  944. ParseNodeSpecialName * pnode = Anew(&m_nodeAllocator, ParseNodeSpecialName, ichMin, ichLim, pid);
  945. pnode->SetSymRef(ref);
  946. if (pid == wellKnownPropertyPids._this)
  947. {
  948. pnode->isThis = true;
  949. }
  950. else if (pid == wellKnownPropertyPids._super || pid == wellKnownPropertyPids._superConstructor)
  951. {
  952. pnode->isSuper = true;
  953. }
  954. AddAstSize(sizeof(ParseNodeSpecialName));
  955. return pnode;
  956. }
  957. ParseNodeSuperReference * Parser::CreateSuperReferenceNode(OpCode nop, ParseNodeSpecialName * pnode1, ParseNodePtr pnode2)
  958. {
  959. Assert(!this->m_deferringAST);
  960. Assert(pnode1 && pnode1->isSuper);
  961. Assert(pnode2 != nullptr);
  962. Assert(nop == knopDot || nop == knopIndex);
  963. ParseNodeSuperReference * pnode = Anew(&m_nodeAllocator, ParseNodeSuperReference, nop, pnode1->ichMin, pnode2->ichLim, pnode1, pnode2);
  964. AddAstSize(sizeof(ParseNodeSuperReference));
  965. return pnode;
  966. }
  967. ParseNodeProg * Parser::CreateProgNode(bool isModuleSource, ULONG lineNumber)
  968. {
  969. ParseNodeProg * pnodeProg;
  970. if (isModuleSource)
  971. {
  972. pnodeProg = CreateNodeForOpT<knopModule>();
  973. // knopModule is not actually handled anywhere since we would need to handle it everywhere we could
  974. // have knopProg and it would be treated exactly the same except for import/export statements.
  975. // We are only using it as a way to get the correct size for PnModule.
  976. // Consider: Should we add a flag to PnProg which is false but set to true in PnModule?
  977. // If we do, it can't be a virtual method since the parse nodes are all in a union.
  978. pnodeProg->nop = knopProg;
  979. }
  980. else
  981. {
  982. pnodeProg = CreateNodeForOpT<knopProg>();
  983. }
  984. pnodeProg->cbMin = this->GetScanner()->IecpMinTok();
  985. pnodeProg->cbStringMin = pnodeProg->cbMin;
  986. pnodeProg->cbStringLim = pnodeProg->cbLim;
  987. pnodeProg->lineNumber = lineNumber;
  988. pnodeProg->homeObjLocation = Js::Constants::NoRegister;
  989. pnodeProg->superRestrictionState = SuperRestrictionState::Disallowed;
  990. return pnodeProg;
  991. }
  992. ParseNodeCall * Parser::CreateCallNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2)
  993. {
  994. charcount_t ichMin;
  995. charcount_t ichLim;
  996. if (nullptr == pnode1)
  997. {
  998. Assert(nullptr == pnode2);
  999. ichMin = this->GetScanner()->IchMinTok();
  1000. ichLim = this->GetScanner()->IchLimTok();
  1001. }
  1002. else
  1003. {
  1004. ichMin = pnode1->ichMin;
  1005. ichLim = pnode2 == nullptr ? pnode1->ichLim : pnode2->ichLim;
  1006. if (pnode1->nop == knopDot || pnode1->nop == knopIndex)
  1007. {
  1008. this->CheckArguments(pnode1->AsParseNodeBin()->pnode1);
  1009. }
  1010. }
  1011. return CreateCallNode(nop, pnode1, pnode2, ichMin, ichLim);
  1012. }
  1013. ParseNodeCall * Parser::CreateCallNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2, charcount_t ichMin, charcount_t ichLim)
  1014. {
  1015. Assert(!this->m_deferringAST);
  1016. // Classes, derived from ParseNodeCall, can be created here as well,
  1017. // as long as their size matches kcbPnCall (that is, they don't add
  1018. // any data members of their own).
  1019. DebugOnly(VerifyNodeSize(nop, sizeof(ParseNodeCall)));
  1020. ParseNodeCall* pnode = Anew(&m_nodeAllocator, ParseNodeCall, nop, ichMin, ichLim, pnode1, pnode2);
  1021. AddAstSize(sizeof(ParseNodeCall));
  1022. return pnode;
  1023. }
  1024. ParseNodeSuperCall * Parser::CreateSuperCallNode(ParseNodeSpecialName * pnode1, ParseNodePtr pnode2)
  1025. {
  1026. Assert(!this->m_deferringAST);
  1027. Assert(pnode1 && pnode1->isSuper);
  1028. ParseNodeSuperCall* pnode = Anew(&m_nodeAllocator, ParseNodeSuperCall, knopCall, pnode1->ichMin, pnode2 == nullptr ? pnode1->ichLim : pnode2->ichLim, pnode1, pnode2);
  1029. AddAstSize(sizeof(ParseNodeSuperCall));
  1030. return pnode;
  1031. }
  1032. ParseNodeParamPattern * Parser::CreateParamPatternNode(ParseNode * pnode1)
  1033. {
  1034. ParseNodeParamPattern * paramPatternNode = CreateNodeForOpT<knopParamPattern>(pnode1->ichMin, pnode1->ichLim);
  1035. paramPatternNode->pnode1 = pnode1;
  1036. paramPatternNode->pnodeNext = nullptr;
  1037. paramPatternNode->location = Js::Constants::NoRegister;
  1038. return paramPatternNode;
  1039. }
  1040. ParseNodeParamPattern * Parser::CreateDummyParamPatternNode(charcount_t ichMin)
  1041. {
  1042. ParseNodeParamPattern * paramPatternNode = CreateNodeForOpT<knopParamPattern>(ichMin);
  1043. paramPatternNode->pnode1 = nullptr;
  1044. paramPatternNode->pnodeNext = nullptr;
  1045. paramPatternNode->location = Js::Constants::NoRegister;
  1046. return paramPatternNode;
  1047. }
  1048. ParseNodeObjLit * Parser::CreateObjectPatternNode(ParseNodePtr pnodeMemberList, charcount_t ichMin, charcount_t ichLim, bool convertToPattern) {
  1049. // Count the number of non-rest members in the object
  1050. uint32 staticCount = 0;
  1051. uint32 computedCount = 0;
  1052. bool hasRest = false;
  1053. ParseNodePtr pnodeMemberNodeList = convertToPattern ? nullptr : pnodeMemberList;
  1054. if (pnodeMemberList != nullptr)
  1055. {
  1056. Assert(pnodeMemberList->nop == knopList ||
  1057. (!convertToPattern && pnodeMemberList->nop == knopObjectPatternMember) ||
  1058. convertToPattern ||
  1059. pnodeMemberList->nop == knopEllipsis);
  1060. ForEachItemInList(pnodeMemberList, [&](ParseNodePtr item) {
  1061. ParseNodePtr memberNode = convertToPattern ? ConvertMemberToMemberPattern(item) : item;
  1062. if (convertToPattern)
  1063. {
  1064. AppendToList(&pnodeMemberNodeList, memberNode);
  1065. }
  1066. if (memberNode->nop != knopEllipsis)
  1067. {
  1068. ParseNodePtr nameNode = memberNode->AsParseNodeBin()->pnode1;
  1069. Assert(nameNode->nop == knopComputedName || nameNode->nop == knopStr);
  1070. if (nameNode->nop == knopComputedName)
  1071. {
  1072. computedCount++;
  1073. }
  1074. else
  1075. {
  1076. staticCount++;
  1077. }
  1078. }
  1079. else
  1080. {
  1081. hasRest = true;
  1082. }
  1083. });
  1084. }
  1085. ParseNodeObjLit * objectPatternNode = CreateNodeForOpT<knopObjectPattern>(ichMin, ichLim);
  1086. objectPatternNode->pnode1 = pnodeMemberNodeList;
  1087. objectPatternNode->computedCount = computedCount;
  1088. objectPatternNode->staticCount = staticCount;
  1089. objectPatternNode->hasRest = hasRest;
  1090. return objectPatternNode;
  1091. }
  1092. Symbol* Parser::AddDeclForPid(ParseNodeVar * pnodeVar, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl)
  1093. {
  1094. Assert(pnodeVar->IsVarLetOrConst());
  1095. PidRefStack *refForUse = nullptr, *refForDecl = nullptr;
  1096. BlockInfoStack *blockInfo;
  1097. bool fBlockScope = false;
  1098. if (pnodeVar->nop != knopVarDecl || symbolType == STFunction)
  1099. {
  1100. Assert(m_pstmtCur);
  1101. if (m_pstmtCur->GetNop() != knopBlock)
  1102. {
  1103. // Let/const declared in a bare statement context.
  1104. Error(ERRDeclOutOfStmt);
  1105. }
  1106. if (m_pstmtCur->pstmtOuter && m_pstmtCur->pstmtOuter->GetNop() == knopSwitch)
  1107. {
  1108. // Let/const declared inside a switch block (requiring conservative use-before-decl check).
  1109. pnodeVar->isSwitchStmtDecl = true;
  1110. }
  1111. fBlockScope = pnodeVar->nop != knopVarDecl ||
  1112. (
  1113. !GetCurrentBlockInfo()->pnodeBlock->scope ||
  1114. GetCurrentBlockInfo()->pnodeBlock->scope->GetScopeType() != ScopeType_GlobalEvalBlock
  1115. );
  1116. }
  1117. if (fBlockScope)
  1118. {
  1119. blockInfo = GetCurrentBlockInfo();
  1120. }
  1121. else
  1122. {
  1123. blockInfo = GetCurrentFunctionBlockInfo();
  1124. }
  1125. refForDecl = this->FindOrAddPidRef(pid, blockInfo->pnodeBlock->blockId, GetCurrentFunctionNode()->functionId);
  1126. if (refForDecl == nullptr)
  1127. {
  1128. Error(ERRnoMemory);
  1129. }
  1130. if (refForDecl->funcId != GetCurrentFunctionNode()->functionId)
  1131. {
  1132. // Fix up the function id, which is incorrect if we're reparsing lambda parameters
  1133. Assert(this->m_reparsingLambdaParams);
  1134. refForDecl->funcId = GetCurrentFunctionNode()->functionId;
  1135. }
  1136. if (blockInfo == GetCurrentBlockInfo())
  1137. {
  1138. refForUse = refForDecl;
  1139. }
  1140. else
  1141. {
  1142. refForUse = this->PushPidRef(pid);
  1143. }
  1144. pnodeVar->symRef = refForUse->GetSymRef();
  1145. Symbol *sym = refForDecl->GetSym();
  1146. if (sym != nullptr)
  1147. {
  1148. // Multiple declarations in the same scope. 3 possibilities: error, existing one wins, new one wins.
  1149. switch (pnodeVar->nop)
  1150. {
  1151. case knopLetDecl:
  1152. case knopConstDecl:
  1153. if (!sym->GetDecl()->AsParseNodeVar()->isBlockScopeFncDeclVar && !sym->IsArguments())
  1154. {
  1155. // If the built-in arguments is shadowed then don't throw
  1156. Assert(errorOnRedecl);
  1157. // Redeclaration error.
  1158. Error(ERRRedeclaration);
  1159. }
  1160. else
  1161. {
  1162. // (New) let/const hides the (old) var
  1163. sym->SetSymbolType(symbolType);
  1164. sym->SetDecl(pnodeVar);
  1165. }
  1166. break;
  1167. case knopVarDecl:
  1168. if (m_currentScope->GetScopeType() == ScopeType_Parameter && !sym->IsArguments())
  1169. {
  1170. // If this is a parameter list, mark the scope to indicate that it has duplicate definition unless it is shadowing the default arguments symbol.
  1171. // If later this turns out to be a non-simple param list (like function f(a, a, c = 1) {}) then it is a SyntaxError to have duplicate formals.
  1172. m_currentScope->SetHasDuplicateFormals();
  1173. }
  1174. if (sym->GetDecl() == nullptr)
  1175. {
  1176. sym->SetDecl(pnodeVar);
  1177. break;
  1178. }
  1179. switch (sym->GetDecl()->nop)
  1180. {
  1181. case knopLetDecl:
  1182. case knopConstDecl:
  1183. // Destructuring made possible to have the formals to be the let bind. But that shouldn't throw the error.
  1184. if (errorOnRedecl && (sym->GetSymbolType() != STFormal))
  1185. {
  1186. Error(ERRRedeclaration);
  1187. }
  1188. // If !errorOnRedecl, (old) let/const hides the (new) var, so do nothing.
  1189. break;
  1190. case knopVarDecl:
  1191. // Legal redeclaration. Who wins?
  1192. if (errorOnRedecl || sym->GetDecl()->AsParseNodeVar()->isBlockScopeFncDeclVar || sym->IsArguments())
  1193. {
  1194. if (symbolType == STFormal ||
  1195. (symbolType == STFunction && sym->GetSymbolType() != STFormal) ||
  1196. sym->GetSymbolType() == STVariable)
  1197. {
  1198. // New decl wins.
  1199. sym->SetSymbolType(symbolType);
  1200. sym->SetDecl(pnodeVar);
  1201. }
  1202. }
  1203. break;
  1204. }
  1205. break;
  1206. }
  1207. }
  1208. else
  1209. {
  1210. Scope *scope = blockInfo->pnodeBlock->scope;
  1211. if (scope == nullptr)
  1212. {
  1213. Assert(blockInfo->pnodeBlock->blockType == PnodeBlockType::Regular);
  1214. scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, ScopeType_Block);
  1215. if (this->IsCurBlockInLoop())
  1216. {
  1217. scope->SetIsBlockInLoop();
  1218. }
  1219. blockInfo->pnodeBlock->scope = scope;
  1220. PushScope(scope);
  1221. }
  1222. ParseNodeFnc * pnodeFnc = GetCurrentFunctionNode();
  1223. if (scope->GetScopeType() == ScopeType_GlobalEvalBlock)
  1224. {
  1225. Assert(fBlockScope);
  1226. Assert(scope->GetEnclosingScope() == m_currentNodeProg->scope);
  1227. // Check for same-named decl in Global scope.
  1228. CheckRedeclarationErrorForBlockId(pid, 0);
  1229. }
  1230. else if (scope->GetScopeType() == ScopeType_Global && (this->m_grfscr & fscrEvalCode) &&
  1231. !(m_functionBody && m_functionBody->GetScopeInfo()))
  1232. {
  1233. // Check for same-named decl in GlobalEvalBlock scope. Note that this is not necessary
  1234. // if we're compiling a deferred nested function and the global scope was restored from cached info,
  1235. // because in that case we don't need a GlobalEvalScope.
  1236. Assert(!fBlockScope || (this->m_grfscr & fscrConsoleScopeEval) == fscrConsoleScopeEval);
  1237. CheckRedeclarationErrorForBlockId(pid, 1);
  1238. }
  1239. else if (!pnodeFnc->IsBodyAndParamScopeMerged()
  1240. && scope->GetScopeType() == ScopeType_FunctionBody
  1241. && (pnodeVar->nop == knopLetDecl || pnodeVar->nop == knopConstDecl))
  1242. {
  1243. // In case of split scope function when we add a new let or const declaration to the body
  1244. // we have to check whether the param scope already has the same symbol defined.
  1245. CheckRedeclarationErrorForBlockId(pid, pnodeFnc->pnodeScopes->blockId);
  1246. }
  1247. if (!sym)
  1248. {
  1249. const char16 *name = reinterpret_cast<const char16*>(pid->Psz());
  1250. int nameLength = pid->Cch();
  1251. SymbolName const symName(name, nameLength);
  1252. Assert(!scope->FindLocalSymbol(symName));
  1253. sym = Anew(&m_nodeAllocator, Symbol, symName, pnodeVar, symbolType);
  1254. scope->AddNewSymbol(sym);
  1255. sym->SetPid(pid);
  1256. }
  1257. refForDecl->SetSym(sym);
  1258. }
  1259. return sym;
  1260. }
  1261. void Parser::CheckRedeclarationErrorForBlockId(IdentPtr pid, int blockId)
  1262. {
  1263. // If the ref stack entry for the blockId contains a sym then throw redeclaration error
  1264. PidRefStack *pidRefOld = pid->GetPidRefForScopeId(blockId);
  1265. if (pidRefOld && pidRefOld->GetSym() && !pidRefOld->GetSym()->IsArguments())
  1266. {
  1267. Error(ERRRedeclaration);
  1268. }
  1269. }
  1270. bool Parser::IsCurBlockInLoop() const
  1271. {
  1272. for (StmtNest *stmt = this->m_pstmtCur; stmt != nullptr; stmt = stmt->pstmtOuter)
  1273. {
  1274. OpCode nop = stmt->GetNop();
  1275. if (ParseNode::Grfnop(nop) & fnopContinue)
  1276. {
  1277. return true;
  1278. }
  1279. if (nop == knopFncDecl)
  1280. {
  1281. return false;
  1282. }
  1283. }
  1284. return false;
  1285. }
  1286. void Parser::RestorePidRefForSym(Symbol *sym)
  1287. {
  1288. IdentPtr pid = this->GetHashTbl()->PidHashNameLen(sym->GetName().GetBuffer(), sym->GetName().GetLength());
  1289. Assert(pid);
  1290. sym->SetPid(pid);
  1291. PidRefStack *ref = this->PushPidRef(pid);
  1292. ref->SetSym(sym);
  1293. }
  1294. void Parser::CheckPidIsValid(IdentPtr pid, bool autoArgumentsObject)
  1295. {
  1296. if (IsStrictMode())
  1297. {
  1298. // in strict mode, variable named 'eval' cannot be created
  1299. if (pid == wellKnownPropertyPids.eval)
  1300. {
  1301. Error(ERREvalUsage);
  1302. }
  1303. else if (pid == wellKnownPropertyPids.arguments && !autoArgumentsObject)
  1304. {
  1305. Error(ERRArgsUsage);
  1306. }
  1307. }
  1308. }
  1309. // CreateVarDecl needs m_ppnodeVar to be pointing to the right function.
  1310. // Post-parsing rewriting during bytecode gen may have m_ppnodeVar pointing to the last parsed function.
  1311. // This function sets up m_ppnodeVar to point to the given pnodeFnc and creates the new var declaration.
  1312. // This prevents accidentally adding var declarations to the last parsed function.
  1313. ParseNodeVar * Parser::AddVarDeclNode(IdentPtr pid, ParseNodeFnc * pnodeFnc)
  1314. {
  1315. AnalysisAssert(pnodeFnc);
  1316. ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
  1317. m_ppnodeVar = &pnodeFnc->pnodeVars;
  1318. while (*m_ppnodeVar != nullptr)
  1319. {
  1320. m_ppnodeVar = &(*m_ppnodeVar)->AsParseNodeVar()->pnodeNext;
  1321. }
  1322. ParseNodeVar * pnode = CreateVarDeclNode(pid, STUnknown, false, 0, /* checkReDecl = */ false);
  1323. m_ppnodeVar = ppnodeVarSave;
  1324. return pnode;
  1325. }
  1326. ParseNodeVar * Parser::CreateModuleImportDeclNode(IdentPtr localName)
  1327. {
  1328. ParseNodeVar * declNode = CreateBlockScopedDeclNode(localName, knopConstDecl);
  1329. Symbol* sym = declNode->sym;
  1330. sym->SetIsModuleExportStorage(true);
  1331. sym->SetIsModuleImport(true);
  1332. return declNode;
  1333. }
  1334. ParseNodeVar * Parser::CreateVarDeclNode(IdentPtr pid, SymbolType symbolType, bool autoArgumentsObject, ParseNodePtr pnodeFnc, bool errorOnRedecl)
  1335. {
  1336. ParseNodeVar * pnode = CreateDeclNode(knopVarDecl, pid, symbolType, errorOnRedecl);
  1337. // Append the variable to the end of the current variable list.
  1338. Assert(m_ppnodeVar);
  1339. pnode->pnodeNext = *m_ppnodeVar;
  1340. *m_ppnodeVar = pnode;
  1341. if (nullptr != pid)
  1342. {
  1343. // this is not a temp - make sure temps go after this node
  1344. Assert(pid);
  1345. m_ppnodeVar = &pnode->pnodeNext;
  1346. CheckPidIsValid(pid, autoArgumentsObject);
  1347. }
  1348. return pnode;
  1349. }
  1350. ParseNodeVar * Parser::CreateBlockScopedDeclNode(IdentPtr pid, OpCode nodeType)
  1351. {
  1352. Assert(nodeType == knopConstDecl || nodeType == knopLetDecl);
  1353. ParseNodeVar * pnode = CreateDeclNode(nodeType, pid, STVariable, true);
  1354. if (nullptr != pid)
  1355. {
  1356. Assert(pid);
  1357. AddVarDeclToBlock(pnode);
  1358. CheckPidIsValid(pid);
  1359. }
  1360. return pnode;
  1361. }
  1362. void Parser::AddVarDeclToBlock(ParseNodeVar *pnode)
  1363. {
  1364. Assert(pnode->nop == knopConstDecl || pnode->nop == knopLetDecl);
  1365. // Maintain a combined list of let and const declarations to keep
  1366. // track of declaration order.
  1367. Assert(m_currentBlockInfo->m_ppnodeLex);
  1368. *m_currentBlockInfo->m_ppnodeLex = pnode;
  1369. m_currentBlockInfo->m_ppnodeLex = &pnode->pnodeNext;
  1370. pnode->pnodeNext = nullptr;
  1371. }
  1372. void Parser::SetCurrentStatement(StmtNest *stmt)
  1373. {
  1374. m_pstmtCur = stmt;
  1375. }
  1376. template<bool buildAST>
  1377. ParseNodeBlock * Parser::StartParseBlockWithCapacity(PnodeBlockType blockType, ScopeType scopeType, int capacity)
  1378. {
  1379. Scope *scope = nullptr;
  1380. // Block scopes are not created lazily in the case where we're repopulating a persisted scope.
  1381. scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, scopeType, capacity);
  1382. PushScope(scope);
  1383. return StartParseBlockHelper<buildAST>(blockType, scope, nullptr);
  1384. }
  1385. template<bool buildAST>
  1386. ParseNodeBlock * Parser::StartParseBlock(PnodeBlockType blockType, ScopeType scopeType, LabelId* pLabelId)
  1387. {
  1388. Scope *scope = nullptr;
  1389. // Block scopes are created lazily when we discover block-scoped content.
  1390. if (scopeType != ScopeType_Unknown && scopeType != ScopeType_Block)
  1391. {
  1392. scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, scopeType);
  1393. PushScope(scope);
  1394. }
  1395. return StartParseBlockHelper<buildAST>(blockType, scope, pLabelId);
  1396. }
  1397. template<bool buildAST>
  1398. ParseNodeBlock * Parser::StartParseBlockHelper(PnodeBlockType blockType, Scope *scope, LabelId* pLabelId)
  1399. {
  1400. ParseNodeBlock * pnodeBlock = CreateBlockNode(blockType);
  1401. pnodeBlock->scope = scope;
  1402. BlockInfoStack *newBlockInfo = PushBlockInfo(pnodeBlock);
  1403. PushStmt<buildAST>(&newBlockInfo->pstmt, pnodeBlock, knopBlock, pLabelId);
  1404. return pnodeBlock;
  1405. }
  1406. void Parser::PushScope(Scope *scope)
  1407. {
  1408. Assert(scope);
  1409. scope->SetEnclosingScope(m_currentScope);
  1410. m_currentScope = scope;
  1411. }
  1412. void Parser::PopScope(Scope *scope)
  1413. {
  1414. Assert(scope == m_currentScope);
  1415. m_currentScope = scope->GetEnclosingScope();
  1416. scope->SetEnclosingScope(nullptr);
  1417. }
  1418. void Parser::PushFuncBlockScope(ParseNodeBlock * pnodeBlock, ParseNodePtr **ppnodeScopeSave, ParseNodePtr **ppnodeExprScopeSave)
  1419. {
  1420. // Maintain the scope tree.
  1421. pnodeBlock->pnodeScopes = nullptr;
  1422. pnodeBlock->pnodeNext = nullptr;
  1423. // Insert this block into the active list of scopes (m_ppnodeExprScope or m_ppnodeScope).
  1424. // Save the current block's "next" pointer as the new endpoint of that list.
  1425. if (m_ppnodeExprScope)
  1426. {
  1427. *ppnodeScopeSave = m_ppnodeScope;
  1428. Assert(*m_ppnodeExprScope == nullptr);
  1429. *m_ppnodeExprScope = pnodeBlock;
  1430. *ppnodeExprScopeSave = &pnodeBlock->pnodeNext;
  1431. }
  1432. else
  1433. {
  1434. Assert(m_ppnodeScope);
  1435. Assert(*m_ppnodeScope == nullptr);
  1436. *m_ppnodeScope = pnodeBlock;
  1437. *ppnodeScopeSave = &pnodeBlock->pnodeNext;
  1438. *ppnodeExprScopeSave = m_ppnodeExprScope;
  1439. }
  1440. // Advance the global scope list pointer to the new block's child list.
  1441. m_ppnodeScope = &pnodeBlock->pnodeScopes;
  1442. // Set m_ppnodeExprScope to NULL to make that list inactive.
  1443. m_ppnodeExprScope = nullptr;
  1444. }
  1445. void Parser::PopFuncBlockScope(ParseNodePtr *ppnodeScopeSave, ParseNodePtr *ppnodeExprScopeSave)
  1446. {
  1447. Assert(m_ppnodeExprScope == nullptr || *m_ppnodeExprScope == nullptr);
  1448. m_ppnodeExprScope = ppnodeExprScopeSave;
  1449. Assert(m_ppnodeScope);
  1450. Assert(nullptr == *m_ppnodeScope);
  1451. m_ppnodeScope = ppnodeScopeSave;
  1452. }
  1453. template<bool buildAST>
  1454. ParseNodeBlock * Parser::ParseBlock(LabelId* pLabelId)
  1455. {
  1456. ParseNodeBlock * pnodeBlock = nullptr;
  1457. ParseNodePtr *ppnodeScopeSave = nullptr;
  1458. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  1459. pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block, pLabelId);
  1460. BlockInfoStack* outerBlockInfo = m_currentBlockInfo->pBlockInfoOuter;
  1461. if (outerBlockInfo != nullptr && outerBlockInfo->pnodeBlock != nullptr
  1462. && outerBlockInfo->pnodeBlock->scope != nullptr
  1463. && outerBlockInfo->pnodeBlock->scope->GetScopeType() == ScopeType_CatchParamPattern)
  1464. {
  1465. // If we are parsing the catch block then destructured params can have let declarations. Let's add them to the new block.
  1466. for (ParseNodePtr pnode = m_currentBlockInfo->pBlockInfoOuter->pnodeBlock->pnodeLexVars; pnode; pnode = pnode->AsParseNodeVar()->pnodeNext)
  1467. {
  1468. PidRefStack* ref = PushPidRef(pnode->AsParseNodeVar()->sym->GetPid());
  1469. ref->SetSym(pnode->AsParseNodeVar()->sym);
  1470. }
  1471. }
  1472. ChkCurTok(tkLCurly, ERRnoLcurly);
  1473. ParseNodePtr * ppnodeList = nullptr;
  1474. if (buildAST)
  1475. {
  1476. PushFuncBlockScope(pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
  1477. ppnodeList = &pnodeBlock->pnodeStmt;
  1478. }
  1479. ParseStmtList<buildAST>(ppnodeList);
  1480. if (buildAST)
  1481. {
  1482. PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
  1483. }
  1484. FinishParseBlock(pnodeBlock);
  1485. ChkCurTok(tkRCurly, ERRnoRcurly);
  1486. return pnodeBlock;
  1487. }
  1488. bool Parser::IsSpecialName(IdentPtr pid)
  1489. {
  1490. return pid == wellKnownPropertyPids._this ||
  1491. pid == wellKnownPropertyPids._super ||
  1492. pid == wellKnownPropertyPids._superConstructor ||
  1493. pid == wellKnownPropertyPids._newTarget ||
  1494. pid == wellKnownPropertyPids._importMeta;
  1495. }
  1496. ParseNodeSpecialName * Parser::ReferenceSpecialName(IdentPtr pid, charcount_t ichMin, charcount_t ichLim, bool createNode)
  1497. {
  1498. PidRefStack* ref = this->PushPidRef(pid);
  1499. if (!createNode)
  1500. {
  1501. return nullptr;
  1502. }
  1503. return CreateSpecialNameNode(pid, ref, ichMin, ichLim);
  1504. }
  1505. ParseNodeVar * Parser::CreateSpecialVarDeclIfNeeded(ParseNodeFnc * pnodeFnc, IdentPtr pid, bool forceCreate)
  1506. {
  1507. Assert(pid != nullptr);
  1508. PidRefStack* ref = pid->GetTopRef();
  1509. // If the function has a reference to pid or we set forceCreate, make a special var decl
  1510. if (forceCreate || (ref && (ref->GetScopeId() >= m_currentBlockInfo->pnodeBlock->blockId && ref->GetFuncScopeId() >= pnodeFnc->functionId)))
  1511. {
  1512. return this->CreateSpecialVarDeclNode(pnodeFnc, pid);
  1513. }
  1514. return nullptr;
  1515. }
  1516. void Parser::CreateSpecialSymbolDeclarations(ParseNodeFnc * pnodeFnc)
  1517. {
  1518. // Lambda function cannot have any special bindings.
  1519. if (pnodeFnc->IsLambda())
  1520. {
  1521. return;
  1522. }
  1523. bool isTopLevelEventHandler = (this->m_grfscr & fscrImplicitThis) && !pnodeFnc->IsNested();
  1524. // Create a 'this' symbol for non-lambda functions with references to 'this', and all class constructors and top level event hanlders.
  1525. ParseNodePtr varDeclNode = CreateSpecialVarDeclIfNeeded(pnodeFnc, wellKnownPropertyPids._this, pnodeFnc->IsClassConstructor() || isTopLevelEventHandler);
  1526. if (varDeclNode)
  1527. {
  1528. varDeclNode->AsParseNodeVar()->sym->SetIsThis(true);
  1529. if (pnodeFnc->IsDerivedClassConstructor())
  1530. {
  1531. varDeclNode->AsParseNodeVar()->sym->SetNeedDeclaration(true);
  1532. }
  1533. }
  1534. // Create a 'new.target' symbol for any ordinary function with a reference and all class constructors.
  1535. varDeclNode = CreateSpecialVarDeclIfNeeded(pnodeFnc, wellKnownPropertyPids._newTarget, pnodeFnc->IsClassConstructor());
  1536. if (varDeclNode)
  1537. {
  1538. varDeclNode->AsParseNodeVar()->sym->SetIsNewTarget(true);
  1539. }
  1540. // Create a 'import.meta' symbol.
  1541. varDeclNode = CreateSpecialVarDeclIfNeeded(pnodeFnc, wellKnownPropertyPids._importMeta);
  1542. if (varDeclNode)
  1543. {
  1544. varDeclNode->AsParseNodeVar()->sym->SetIsImportMeta(true);
  1545. }
  1546. // Create a 'super' (as a reference) symbol.
  1547. varDeclNode = CreateSpecialVarDeclIfNeeded(pnodeFnc, wellKnownPropertyPids._super);
  1548. if (varDeclNode)
  1549. {
  1550. varDeclNode->AsParseNodeVar()->sym->SetIsSuper(true);
  1551. }
  1552. // Create a 'super' (as the call target for super()) symbol only for derived class constructors.
  1553. varDeclNode = CreateSpecialVarDeclIfNeeded(pnodeFnc, wellKnownPropertyPids._superConstructor);
  1554. if (varDeclNode)
  1555. {
  1556. varDeclNode->AsParseNodeVar()->sym->SetIsSuperConstructor(true);
  1557. }
  1558. }
  1559. void Parser::FinishParseBlock(ParseNodeBlock *pnodeBlock, bool needScanRCurly)
  1560. {
  1561. Assert(m_currentBlockInfo != nullptr && pnodeBlock == m_currentBlockInfo->pnodeBlock);
  1562. if (needScanRCurly)
  1563. {
  1564. // Only update the ichLim if we were expecting an RCurly. If there is an
  1565. // expression body without a necessary RCurly, the correct ichLim will
  1566. // have been set already.
  1567. pnodeBlock->ichLim = this->GetScanner()->IchLimTok();
  1568. }
  1569. BindPidRefs<false>(GetCurrentBlockInfo(), m_nextBlockId - 1);
  1570. PopStmt(&m_currentBlockInfo->pstmt);
  1571. PopBlockInfo();
  1572. Scope *scope = pnodeBlock->scope;
  1573. if (scope)
  1574. {
  1575. PopScope(scope);
  1576. }
  1577. }
  1578. void Parser::FinishParseFncExprScope(ParseNodeFnc * pnodeFnc, ParseNodeBlock * pnodeFncExprScope)
  1579. {
  1580. int fncExprScopeId = pnodeFncExprScope->blockId;
  1581. ParseNodePtr pnodeName = pnodeFnc->pnodeName;
  1582. if (pnodeName)
  1583. {
  1584. Assert(pnodeName->nop == knopVarDecl);
  1585. BindPidRefsInScope(pnodeName->AsParseNodeVar()->pid, pnodeName->AsParseNodeVar()->sym, fncExprScopeId, m_nextBlockId - 1);
  1586. }
  1587. FinishParseBlock(pnodeFncExprScope);
  1588. }
  1589. template <const bool backgroundPidRef>
  1590. void Parser::BindPidRefs(BlockInfoStack *blockInfo, uint maxBlockId)
  1591. {
  1592. // We need to bind all assignments in order to emit assignment to 'const' error
  1593. int blockId = blockInfo->pnodeBlock->blockId;
  1594. Scope *scope = blockInfo->pnodeBlock->scope;
  1595. if (scope)
  1596. {
  1597. auto bindPidRefs = [blockId, maxBlockId, this](Symbol *sym)
  1598. {
  1599. ParseNodePtr pnode = sym->GetDecl();
  1600. IdentPtr pid;
  1601. #if PROFILE_DICTIONARY
  1602. int depth = 0;
  1603. #endif
  1604. Assert(pnode);
  1605. switch (pnode->nop)
  1606. {
  1607. case knopVarDecl:
  1608. case knopLetDecl:
  1609. case knopConstDecl:
  1610. pid = pnode->AsParseNodeVar()->pid;
  1611. if (backgroundPidRef)
  1612. {
  1613. pid = this->GetHashTbl()->FindExistingPid(pid->Psz(), pid->Psz() + pid->Cch(), pid->Cch(), pid->Hash(), nullptr, nullptr
  1614. #if PROFILE_DICTIONARY
  1615. , depth
  1616. #endif
  1617. );
  1618. if (pid == nullptr)
  1619. {
  1620. break;
  1621. }
  1622. }
  1623. this->BindPidRefsInScope(pid, sym, blockId, maxBlockId);
  1624. break;
  1625. case knopName:
  1626. pid = pnode->AsParseNodeName()->pid;
  1627. if (backgroundPidRef)
  1628. {
  1629. pid = this->GetHashTbl()->FindExistingPid(pid->Psz(), pid->Psz() + pid->Cch(), pid->Cch(), pid->Hash(), nullptr, nullptr
  1630. #if PROFILE_DICTIONARY
  1631. , depth
  1632. #endif
  1633. );
  1634. if (pid == nullptr)
  1635. {
  1636. break;
  1637. }
  1638. }
  1639. this->BindPidRefsInScope(pid, sym, blockId, maxBlockId);
  1640. break;
  1641. default:
  1642. Assert(0);
  1643. break;
  1644. }
  1645. };
  1646. scope->ForEachSymbol(bindPidRefs);
  1647. }
  1648. }
  1649. void Parser::BindPidRefsInScope(IdentPtr pid, Symbol *sym, int blockId, uint maxBlockId)
  1650. {
  1651. PidRefStack *ref, *nextRef, *lastRef = nullptr;
  1652. Js::LocalFunctionId funcId = GetCurrentFunctionNode()->functionId;
  1653. Assert(sym);
  1654. if (pid->GetIsModuleExport() && IsTopLevelModuleFunc())
  1655. {
  1656. sym->SetIsModuleExportStorage(true);
  1657. }
  1658. bool hasFuncAssignment = sym->GetHasFuncAssignment();
  1659. bool doesEscape = false;
  1660. for (ref = pid->GetTopRef(); ref && ref->GetScopeId() >= blockId; ref = nextRef)
  1661. {
  1662. // Fix up sym* on PID ref.
  1663. Assert(!ref->GetSym() || ref->GetSym() == sym);
  1664. nextRef = ref->prev;
  1665. Assert(ref->GetScopeId() >= 0);
  1666. if ((uint)ref->GetScopeId() > maxBlockId)
  1667. {
  1668. lastRef = ref;
  1669. continue;
  1670. }
  1671. ref->SetSym(sym);
  1672. this->RemovePrevPidRef(pid, lastRef);
  1673. if (ref->IsUsedInLdElem())
  1674. {
  1675. sym->SetIsUsedInLdElem(true);
  1676. }
  1677. if (ref->IsAssignment())
  1678. {
  1679. sym->PromoteAssignmentState();
  1680. if (sym->GetIsFormal())
  1681. {
  1682. GetCurrentFunctionNode()->SetHasAnyWriteToFormals(true);
  1683. }
  1684. }
  1685. if (ref->GetFuncScopeId() != funcId && !sym->GetIsGlobal() && !sym->GetIsModuleExportStorage())
  1686. {
  1687. Assert(ref->GetFuncScopeId() > funcId);
  1688. sym->SetHasNonLocalReference();
  1689. if (ref->IsDynamicBinding())
  1690. {
  1691. sym->SetNeedsScopeObject();
  1692. }
  1693. }
  1694. if (ref->IsFuncAssignment())
  1695. {
  1696. hasFuncAssignment = true;
  1697. }
  1698. if (ref->IsEscape())
  1699. {
  1700. doesEscape = true;
  1701. }
  1702. if (m_currentNodeFunc && doesEscape && hasFuncAssignment)
  1703. {
  1704. if (m_sourceContextInfo ?
  1705. !PHASE_OFF_RAW(Js::DisableStackFuncOnDeferredEscapePhase, m_sourceContextInfo->sourceContextId, m_currentNodeFunc->functionId) :
  1706. !PHASE_OFF1(Js::DisableStackFuncOnDeferredEscapePhase))
  1707. {
  1708. m_currentNodeFunc->SetNestedFuncEscapes();
  1709. }
  1710. }
  1711. if (m_currentNodeFunc && m_currentNodeFunc->pnodeName && pid == m_currentNodeFunc->pnodeName->pid && !m_currentNodeFunc->IsDeclaration() && m_currentNodeFunc->IsBodyAndParamScopeMerged())
  1712. {
  1713. Scope* funcExprScope = m_currentNodeFunc->scope;
  1714. Assert(funcExprScope->GetScopeType() == ScopeType_FuncExpr);
  1715. ParseNodeBlock* bodyScope = m_currentNodeFunc->pnodeBodyScope;
  1716. Assert(bodyScope == nullptr || bodyScope->blockType == PnodeBlockType::Function);
  1717. if (bodyScope && ref->GetScopeId() < bodyScope->blockId && ref->GetScopeId() > blockId)
  1718. {
  1719. Assert(bodyScope->blockType == PnodeBlockType::Function);
  1720. funcExprScope->SetIsObject();
  1721. }
  1722. }
  1723. if (ref->GetScopeId() == blockId)
  1724. {
  1725. break;
  1726. }
  1727. }
  1728. }
  1729. void Parser::MarkEscapingRef(ParseNodePtr pnode, IdentToken *pToken)
  1730. {
  1731. if (m_currentNodeFunc == nullptr)
  1732. {
  1733. return;
  1734. }
  1735. if (pnode && pnode->nop == knopFncDecl)
  1736. {
  1737. this->SetNestedFuncEscapes();
  1738. }
  1739. else if (pToken->pid)
  1740. {
  1741. PidRefStack *pidRef = pToken->pid->GetTopRef();
  1742. if (pidRef->sym)
  1743. {
  1744. if (pidRef->sym->GetSymbolType() == STFunction)
  1745. {
  1746. this->SetNestedFuncEscapes();
  1747. }
  1748. }
  1749. else
  1750. {
  1751. pidRef->isEscape = true;
  1752. }
  1753. }
  1754. }
  1755. void Parser::SetNestedFuncEscapes() const
  1756. {
  1757. if (m_sourceContextInfo ?
  1758. !PHASE_OFF_RAW(Js::DisableStackFuncOnDeferredEscapePhase, m_sourceContextInfo->sourceContextId, m_currentNodeFunc->functionId) :
  1759. !PHASE_OFF1(Js::DisableStackFuncOnDeferredEscapePhase))
  1760. {
  1761. m_currentNodeFunc->SetNestedFuncEscapes();
  1762. }
  1763. }
  1764. void Parser::PopStmt(StmtNest *pStmt)
  1765. {
  1766. Assert(pStmt == m_pstmtCur);
  1767. SetCurrentStatement(m_pstmtCur->pstmtOuter);
  1768. }
  1769. BlockInfoStack *Parser::PushBlockInfo(ParseNodeBlock * pnodeBlock)
  1770. {
  1771. BlockInfoStack *newBlockInfo = (BlockInfoStack *)m_nodeAllocator.Alloc(sizeof(BlockInfoStack));
  1772. Assert(nullptr != newBlockInfo);
  1773. newBlockInfo->pnodeBlock = pnodeBlock;
  1774. newBlockInfo->pBlockInfoOuter = m_currentBlockInfo;
  1775. newBlockInfo->m_ppnodeLex = &pnodeBlock->pnodeLexVars;
  1776. if (pnodeBlock->blockType != PnodeBlockType::Regular)
  1777. {
  1778. newBlockInfo->pBlockInfoFunction = newBlockInfo;
  1779. }
  1780. else
  1781. {
  1782. Assert(m_currentBlockInfo);
  1783. newBlockInfo->pBlockInfoFunction = m_currentBlockInfo->pBlockInfoFunction;
  1784. }
  1785. m_currentBlockInfo = newBlockInfo;
  1786. return newBlockInfo;
  1787. }
  1788. void Parser::PopBlockInfo()
  1789. {
  1790. Assert(m_currentBlockInfo);
  1791. PopDynamicBlock();
  1792. m_currentBlockInfo = m_currentBlockInfo->pBlockInfoOuter;
  1793. }
  1794. void Parser::PushDynamicBlock()
  1795. {
  1796. Assert(GetCurrentBlock());
  1797. int blockId = GetCurrentBlock()->blockId;
  1798. if (m_currentDynamicBlock && m_currentDynamicBlock->id == blockId)
  1799. {
  1800. return;
  1801. }
  1802. BlockIdsStack *info = (BlockIdsStack *)m_nodeAllocator.Alloc(sizeof(BlockIdsStack));
  1803. if (nullptr == info)
  1804. {
  1805. Error(ERRnoMemory);
  1806. }
  1807. info->id = blockId;
  1808. info->prev = m_currentDynamicBlock;
  1809. m_currentDynamicBlock = info;
  1810. }
  1811. void Parser::PopDynamicBlock()
  1812. {
  1813. int blockId = GetCurrentDynamicBlockId();
  1814. if (GetCurrentBlock()->blockId != blockId || blockId == -1)
  1815. {
  1816. return;
  1817. }
  1818. Assert(m_currentDynamicBlock);
  1819. this->GetHashTbl()->VisitPids([&](IdentPtr pid) {
  1820. for (PidRefStack *ref = pid->GetTopRef(); ref && ref->GetScopeId() >= blockId; ref = ref->prev)
  1821. {
  1822. ref->SetDynamicBinding();
  1823. }
  1824. });
  1825. m_currentDynamicBlock = m_currentDynamicBlock->prev;
  1826. }
  1827. int Parser::GetCurrentDynamicBlockId() const
  1828. {
  1829. return m_currentDynamicBlock ? m_currentDynamicBlock->id : -1;
  1830. }
  1831. ParseNodeFnc *Parser::GetCurrentFunctionNode()
  1832. {
  1833. if (m_currentNodeDeferredFunc != nullptr)
  1834. {
  1835. return m_currentNodeDeferredFunc;
  1836. }
  1837. else if (m_currentNodeFunc != nullptr)
  1838. {
  1839. return m_currentNodeFunc;
  1840. }
  1841. else
  1842. {
  1843. AssertMsg(GetFunctionBlock()->blockType == PnodeBlockType::Global,
  1844. "Most likely we are trying to find a syntax error, related to 'let' or 'const' in deferred parsing mode with disabled support of 'let' and 'const'");
  1845. return m_currentNodeProg;
  1846. }
  1847. }
  1848. ParseNodeFnc *Parser::GetCurrentNonLambdaFunctionNode()
  1849. {
  1850. if (m_currentNodeNonLambdaDeferredFunc != nullptr)
  1851. {
  1852. return m_currentNodeNonLambdaDeferredFunc;
  1853. }
  1854. return m_currentNodeNonLambdaFunc;
  1855. }
  1856. void Parser::RegisterRegexPattern(UnifiedRegex::RegexPattern *const regexPattern)
  1857. {
  1858. Assert(regexPattern);
  1859. // ensure a no-throw add behavior here, to catch out of memory exceptions, using the guest arena allocator
  1860. if (!m_registeredRegexPatterns.PrependNoThrow(m_tempGuestArena->GetAllocator(), regexPattern))
  1861. {
  1862. Parser::Error(ERRnoMemory);
  1863. }
  1864. }
  1865. void Parser::CaptureState(ParserState *state)
  1866. {
  1867. Assert(state != nullptr);
  1868. state->m_funcInArraySave = m_funcInArray;
  1869. state->m_funcInArrayDepthSave = m_funcInArrayDepth;
  1870. state->m_nestedCountSave = *m_pnestedCount;
  1871. state->m_ppnodeScopeSave = m_ppnodeScope;
  1872. state->m_ppnodeExprScopeSave = m_ppnodeExprScope;
  1873. state->m_pCurrentAstSizeSave = m_pCurrentAstSize;
  1874. state->m_nextBlockId = m_nextBlockId;
  1875. Assert(state->m_ppnodeScopeSave == nullptr || *state->m_ppnodeScopeSave == nullptr);
  1876. Assert(state->m_ppnodeExprScopeSave == nullptr || *state->m_ppnodeExprScopeSave == nullptr);
  1877. #if DEBUG
  1878. state->m_currentBlockInfo = m_currentBlockInfo;
  1879. #endif
  1880. }
  1881. void Parser::RestoreStateFrom(ParserState *state)
  1882. {
  1883. Assert(state != nullptr);
  1884. Assert(state->m_currentBlockInfo == m_currentBlockInfo);
  1885. m_funcInArray = state->m_funcInArraySave;
  1886. m_funcInArrayDepth = state->m_funcInArrayDepthSave;
  1887. *m_pnestedCount = state->m_nestedCountSave;
  1888. m_pCurrentAstSize = state->m_pCurrentAstSizeSave;
  1889. m_nextBlockId = state->m_nextBlockId;
  1890. if (state->m_ppnodeScopeSave != nullptr)
  1891. {
  1892. *state->m_ppnodeScopeSave = nullptr;
  1893. }
  1894. if (state->m_ppnodeExprScopeSave != nullptr)
  1895. {
  1896. *state->m_ppnodeExprScopeSave = nullptr;
  1897. }
  1898. m_ppnodeScope = state->m_ppnodeScopeSave;
  1899. m_ppnodeExprScope = state->m_ppnodeExprScopeSave;
  1900. }
  1901. void Parser::AddToNodeListEscapedUse(ParseNode ** ppnodeList, ParseNode *** pppnodeLast,
  1902. ParseNode * pnodeAdd)
  1903. {
  1904. AddToNodeList(ppnodeList, pppnodeLast, pnodeAdd);
  1905. pnodeAdd->SetIsInList();
  1906. }
  1907. void Parser::AddToNodeList(ParseNode ** ppnodeList, ParseNode *** pppnodeLast,
  1908. ParseNode * pnodeAdd)
  1909. {
  1910. Assert(!this->m_deferringAST);
  1911. if (nullptr == *pppnodeLast)
  1912. {
  1913. // should be an empty list
  1914. Assert(nullptr == *ppnodeList);
  1915. *ppnodeList = pnodeAdd;
  1916. *pppnodeLast = ppnodeList;
  1917. }
  1918. else
  1919. {
  1920. Assert(*ppnodeList);
  1921. Assert(**pppnodeLast);
  1922. ParseNode *pnodeT = CreateBinNode(knopList, **pppnodeLast, pnodeAdd);
  1923. **pppnodeLast = pnodeT;
  1924. *pppnodeLast = &pnodeT->AsParseNodeBin()->pnode2;
  1925. }
  1926. }
  1927. // Check reference to "arguments" that indicates the object may escape.
  1928. void Parser::CheckArguments(ParseNodePtr pnode)
  1929. {
  1930. if (m_currentNodeFunc && this->NodeIsIdent(pnode, wellKnownPropertyPids.arguments))
  1931. {
  1932. m_currentNodeFunc->SetHasHeapArguments();
  1933. }
  1934. }
  1935. // Check use of "arguments" that requires instantiation of the object.
  1936. void Parser::CheckArgumentsUse(IdentPtr pid, ParseNodeFnc * pnodeFnc)
  1937. {
  1938. if (pid == wellKnownPropertyPids.arguments)
  1939. {
  1940. if (pnodeFnc != nullptr && pnodeFnc != m_currentNodeProg)
  1941. {
  1942. pnodeFnc->SetUsesArguments(TRUE);
  1943. }
  1944. else
  1945. {
  1946. m_UsesArgumentsAtGlobal = true;
  1947. }
  1948. }
  1949. }
  1950. void Parser::CheckStrictModeEvalArgumentsUsage(IdentPtr pid, ParseNodePtr pnode)
  1951. {
  1952. if (pid != nullptr)
  1953. {
  1954. // In strict mode, 'eval' / 'arguments' cannot be assigned to.
  1955. if (pid == wellKnownPropertyPids.eval)
  1956. {
  1957. Error(ERREvalUsage, pnode);
  1958. }
  1959. if (pid == wellKnownPropertyPids.arguments)
  1960. {
  1961. Error(ERRArgsUsage, pnode);
  1962. }
  1963. }
  1964. }
  1965. void Parser::ReduceDeferredScriptLength(size_t chars)
  1966. {
  1967. // If we're in deferred mode, subtract the given char count from the total length,
  1968. // and see if this puts us under the deferral threshold.
  1969. if (((m_grfscr & (fscrCanDeferFncParse | fscrWillDeferFncParse)) == (fscrCanDeferFncParse | fscrWillDeferFncParse)) &&
  1970. (
  1971. PHASE_OFF1(Js::DeferEventHandlersPhase) ||
  1972. (m_grfscr & fscrGlobalCode)
  1973. )
  1974. )
  1975. {
  1976. if (m_length > chars)
  1977. {
  1978. m_length -= chars;
  1979. }
  1980. else
  1981. {
  1982. m_length = 0;
  1983. }
  1984. if (m_length < Parser::GetDeferralThreshold(this->m_sourceContextInfo->IsSourceProfileLoaded()))
  1985. {
  1986. // Stop deferring.
  1987. m_grfscr &= ~fscrWillDeferFncParse;
  1988. m_stoppedDeferredParse = TRUE;
  1989. }
  1990. }
  1991. }
  1992. void Parser::EnsureStackAvailable()
  1993. {
  1994. bool isInterrupt = false;
  1995. if (!m_scriptContext->GetThreadContext()->IsStackAvailable(Js::Constants::MinStackCompile, &isInterrupt))
  1996. {
  1997. Error(isInterrupt ? E_ABORT : VBSERR_OutOfStack);
  1998. }
  1999. }
  2000. void Parser::ThrowNewTargetSyntaxErrForGlobalScope()
  2001. {
  2002. // If we are parsing a previously deferred function, we can skip throwing the SyntaxError for `new.target` at global scope.
  2003. // If we are at global scope, we would have thrown a SyntaxError when we did the Upfront parse pass and we would not have
  2004. // deferred the function in order to come back now and reparse it.
  2005. if (m_parseType == ParseType_Deferred)
  2006. {
  2007. return;
  2008. }
  2009. if (GetCurrentNonLambdaFunctionNode() != nullptr)
  2010. {
  2011. return;
  2012. }
  2013. if ((this->m_grfscr & fscrEval) != 0)
  2014. {
  2015. Js::JavascriptFunction * caller = nullptr;
  2016. if (Js::JavascriptStackWalker::GetCaller(&caller, m_scriptContext))
  2017. {
  2018. Js::FunctionBody * callerBody = caller->GetFunctionBody();
  2019. Assert(callerBody);
  2020. if (!callerBody->GetIsGlobalFunc() && !(callerBody->IsLambda() && callerBody->GetEnclosedByGlobalFunc()))
  2021. {
  2022. return;
  2023. }
  2024. }
  2025. }
  2026. Error(ERRInvalidNewTarget);
  2027. }
  2028. template<bool buildAST>
  2029. IdentPtr Parser::ParseMetaProperty(tokens metaParentKeyword, charcount_t ichMin, _Out_opt_ BOOL* pfCanAssign)
  2030. {
  2031. AssertMsg(metaParentKeyword == tkNEW || metaParentKeyword == tkIMPORT, "Only supported for tkNEW and tkIMPORT parent keywords");
  2032. AssertMsg(this->m_token.tk == tkDot, "We must be currently sitting on the dot after the parent keyword");
  2033. this->GetScanner()->Scan();
  2034. if (this->m_token.tk == tkID)
  2035. {
  2036. IdentPtr id = this->m_token.GetIdentifier(this->GetHashTbl());
  2037. switch (metaParentKeyword)
  2038. {
  2039. case tkNEW:
  2040. if (id == this->GetTargetPid())
  2041. {
  2042. ThrowNewTargetSyntaxErrForGlobalScope();
  2043. if (pfCanAssign)
  2044. {
  2045. *pfCanAssign = FALSE;
  2046. }
  2047. return wellKnownPropertyPids._newTarget;
  2048. }
  2049. break;
  2050. case tkIMPORT:
  2051. if (id == this->GetMetaPid())
  2052. {
  2053. if (pfCanAssign)
  2054. {
  2055. *pfCanAssign = FALSE;
  2056. }
  2057. return wellKnownPropertyPids._importMeta;
  2058. }
  2059. break;
  2060. }
  2061. }
  2062. if (metaParentKeyword == tkNEW)
  2063. {
  2064. Error(ERRValidIfFollowedBy, _u("'new.'"), _u("'target'"));
  2065. }
  2066. else
  2067. {
  2068. Error(ERRValidIfFollowedBy, _u("'import.'"), _u("'meta'"));
  2069. }
  2070. }
  2071. template<bool buildAST>
  2072. void Parser::ParseNamedImportOrExportClause(ModuleImportOrExportEntryList* importOrExportEntryList, bool isExportClause)
  2073. {
  2074. Assert(m_token.tk == tkLCurly);
  2075. Assert(importOrExportEntryList != nullptr);
  2076. this->GetScanner()->Scan();
  2077. while (m_token.tk != tkRCurly && m_token.tk != tkEOF)
  2078. {
  2079. tokens firstToken = m_token.tk;
  2080. if (!(m_token.IsIdentifier() || m_token.IsReservedWord()))
  2081. {
  2082. Error(ERRTokenAfter, GetTokenString(m_token.tk), GetTokenString(GetScanner()->GetPrevious()));
  2083. }
  2084. IdentPtr identifierName = m_token.GetIdentifier(this->GetHashTbl());
  2085. IdentPtr identifierAs = identifierName;
  2086. charcount_t offsetForError = this->GetScanner()->IchMinTok();
  2087. this->GetScanner()->Scan();
  2088. if (m_token.tk == tkID)
  2089. {
  2090. // We have the pattern "IdentifierName as"
  2091. if (!CheckContextualKeyword(wellKnownPropertyPids.as))
  2092. {
  2093. Error(ERRInvalidIdentifier, m_token.GetIdentifier(this->GetHashTbl())->Psz(), identifierName->Psz());
  2094. }
  2095. this->GetScanner()->Scan();
  2096. // If we are parsing an import statement, the token after 'as' must be a BindingIdentifier.
  2097. if (!isExportClause)
  2098. {
  2099. ChkCurTokNoScan(tkID, ERRValidIfFollowedBy, _u("'as'"), _u("an identifier."));
  2100. }
  2101. if (!(m_token.IsIdentifier() || m_token.IsReservedWord()))
  2102. {
  2103. Error(ERRValidIfFollowedBy, _u("'as'"), _u("an identifier."));
  2104. }
  2105. identifierAs = m_token.GetIdentifier(this->GetHashTbl());
  2106. // Scan to the next token.
  2107. this->GetScanner()->Scan();
  2108. }
  2109. else if (!isExportClause && firstToken != tkID)
  2110. {
  2111. // If we are parsing an import statement and this ImportSpecifier clause did not have
  2112. // 'as ImportedBinding' at the end of it, identifierName must be a BindingIdentifier.
  2113. Error(ERRnoIdent);
  2114. }
  2115. if (m_token.tk == tkComma)
  2116. {
  2117. // Consume a trailing comma
  2118. this->GetScanner()->Scan();
  2119. }
  2120. if (isExportClause)
  2121. {
  2122. identifierName->SetIsModuleExport();
  2123. AddModuleImportOrExportEntry(importOrExportEntryList, nullptr, identifierName, identifierAs, nullptr, offsetForError);
  2124. }
  2125. else if (buildAST)
  2126. {
  2127. // The name we will use 'as' this import/export is a binding identifier in import statements.
  2128. CreateModuleImportDeclNode(identifierAs);
  2129. AddModuleImportOrExportEntry(importOrExportEntryList, identifierName, identifierAs, nullptr, nullptr);
  2130. }
  2131. }
  2132. // Final token in a named import or export clause must be a '}'
  2133. ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
  2134. }
  2135. IdentPtrList* Parser::GetRequestedModulesList()
  2136. {
  2137. return m_currentNodeProg->AsParseNodeModule()->requestedModules;
  2138. }
  2139. void Parser::VerifyModuleLocalExportEntries()
  2140. {
  2141. ModuleImportOrExportEntryList* localExportRecordList = GetModuleLocalExportEntryList();
  2142. if (localExportRecordList != nullptr)
  2143. {
  2144. localExportRecordList->Map([=](ModuleImportOrExportEntry exportEntry) {
  2145. if (exportEntry.pidRefStack!=nullptr)
  2146. {
  2147. if (exportEntry.pidRefStack->GetSym() == nullptr)
  2148. {
  2149. Error(ERRUndeclaredExportName, exportEntry.offset, exportEntry.localName->Cch(), exportEntry.localName->Psz());
  2150. }
  2151. }
  2152. });
  2153. }
  2154. }
  2155. ModuleImportOrExportEntryList* Parser::GetModuleImportEntryList()
  2156. {
  2157. return m_currentNodeProg->AsParseNodeModule()->importEntries;
  2158. }
  2159. ModuleImportOrExportEntryList* Parser::GetModuleLocalExportEntryList()
  2160. {
  2161. return m_currentNodeProg->AsParseNodeModule()->localExportEntries;
  2162. }
  2163. ModuleImportOrExportEntryList* Parser::GetModuleIndirectExportEntryList()
  2164. {
  2165. return m_currentNodeProg->AsParseNodeModule()->indirectExportEntries;
  2166. }
  2167. ModuleImportOrExportEntryList* Parser::GetModuleStarExportEntryList()
  2168. {
  2169. return m_currentNodeProg->AsParseNodeModule()->starExportEntries;
  2170. }
  2171. IdentPtrList* Parser::EnsureRequestedModulesList()
  2172. {
  2173. if (m_currentNodeProg->AsParseNodeModule()->requestedModules == nullptr)
  2174. {
  2175. m_currentNodeProg->AsParseNodeModule()->requestedModules = Anew(&m_nodeAllocator, IdentPtrList, &m_nodeAllocator);
  2176. }
  2177. return m_currentNodeProg->AsParseNodeModule()->requestedModules;
  2178. }
  2179. ModuleImportOrExportEntryList* Parser::EnsureModuleImportEntryList()
  2180. {
  2181. if (m_currentNodeProg->AsParseNodeModule()->importEntries == nullptr)
  2182. {
  2183. m_currentNodeProg->AsParseNodeModule()->importEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
  2184. }
  2185. return m_currentNodeProg->AsParseNodeModule()->importEntries;
  2186. }
  2187. ModuleImportOrExportEntryList* Parser::EnsureModuleLocalExportEntryList()
  2188. {
  2189. if (m_currentNodeProg->AsParseNodeModule()->localExportEntries == nullptr)
  2190. {
  2191. m_currentNodeProg->AsParseNodeModule()->localExportEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
  2192. }
  2193. return m_currentNodeProg->AsParseNodeModule()->localExportEntries;
  2194. }
  2195. ModuleImportOrExportEntryList* Parser::EnsureModuleIndirectExportEntryList()
  2196. {
  2197. if (m_currentNodeProg->AsParseNodeModule()->indirectExportEntries == nullptr)
  2198. {
  2199. m_currentNodeProg->AsParseNodeModule()->indirectExportEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
  2200. }
  2201. return m_currentNodeProg->AsParseNodeModule()->indirectExportEntries;
  2202. }
  2203. ModuleImportOrExportEntryList* Parser::EnsureModuleStarExportEntryList()
  2204. {
  2205. if (m_currentNodeProg->AsParseNodeModule()->starExportEntries == nullptr)
  2206. {
  2207. m_currentNodeProg->AsParseNodeModule()->starExportEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
  2208. }
  2209. return m_currentNodeProg->AsParseNodeModule()->starExportEntries;
  2210. }
  2211. void Parser::AddModuleSpecifier(IdentPtr moduleRequest)
  2212. {
  2213. IdentPtrList* requestedModulesList = EnsureRequestedModulesList();
  2214. if (!requestedModulesList->Has(moduleRequest))
  2215. {
  2216. requestedModulesList->Prepend(moduleRequest);
  2217. }
  2218. }
  2219. ModuleImportOrExportEntry* Parser::AddModuleImportOrExportEntry(ModuleImportOrExportEntryList* importOrExportEntryList, ModuleImportOrExportEntry* importOrExportEntry)
  2220. {
  2221. if (importOrExportEntry->exportName != nullptr)
  2222. {
  2223. CheckForDuplicateExportEntry(importOrExportEntry->exportName);
  2224. }
  2225. importOrExportEntryList->Prepend(*importOrExportEntry);
  2226. return importOrExportEntry;
  2227. }
  2228. ModuleImportOrExportEntry* Parser::AddModuleImportOrExportEntry(ModuleImportOrExportEntryList* importOrExportEntryList, IdentPtr importName, IdentPtr localName, IdentPtr exportName, IdentPtr moduleRequest, charcount_t offsetForError)
  2229. {
  2230. ModuleImportOrExportEntry* importOrExportEntry = Anew(&m_nodeAllocator, ModuleImportOrExportEntry);
  2231. importOrExportEntry->importName = importName;
  2232. importOrExportEntry->localName = localName;
  2233. importOrExportEntry->exportName = exportName;
  2234. importOrExportEntry->moduleRequest = moduleRequest;
  2235. importOrExportEntry->pidRefStack = offsetForError == 0 ? nullptr : PushPidRef(localName);
  2236. importOrExportEntry->offset = offsetForError;
  2237. return AddModuleImportOrExportEntry(importOrExportEntryList, importOrExportEntry);
  2238. }
  2239. void Parser::AddModuleLocalExportEntry(ParseNodePtr varDeclNode)
  2240. {
  2241. AssertOrFailFast(varDeclNode->nop == knopVarDecl || varDeclNode->nop == knopLetDecl || varDeclNode->nop == knopConstDecl);
  2242. IdentPtr localName = varDeclNode->AsParseNodeVar()->pid;
  2243. varDeclNode->AsParseNodeVar()->sym->SetIsModuleExportStorage(true);
  2244. AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), nullptr, localName, localName, nullptr);
  2245. }
  2246. void Parser::CheckForDuplicateExportEntry(IdentPtr exportName)
  2247. {
  2248. if (m_currentNodeProg->AsParseNodeModule()->indirectExportEntries != nullptr)
  2249. {
  2250. CheckForDuplicateExportEntry(m_currentNodeProg->AsParseNodeModule()->indirectExportEntries, exportName);
  2251. }
  2252. if (m_currentNodeProg->AsParseNodeModule()->localExportEntries != nullptr)
  2253. {
  2254. CheckForDuplicateExportEntry(m_currentNodeProg->AsParseNodeModule()->localExportEntries, exportName);
  2255. }
  2256. }
  2257. void Parser::CheckForDuplicateExportEntry(ModuleImportOrExportEntryList* exportEntryList, IdentPtr exportName)
  2258. {
  2259. ModuleImportOrExportEntry* findResult = exportEntryList->Find([&](ModuleImportOrExportEntry exportEntry)
  2260. {
  2261. if (exportName == exportEntry.exportName)
  2262. {
  2263. return true;
  2264. }
  2265. return false;
  2266. });
  2267. if (findResult != nullptr)
  2268. {
  2269. Error(ERRDuplicateExport, exportName->Psz());
  2270. }
  2271. }
  2272. template<bool buildAST>
  2273. void Parser::ParseImportClause(ModuleImportOrExportEntryList* importEntryList, bool parsingAfterComma)
  2274. {
  2275. bool parsedNamespaceOrNamedImport = false;
  2276. switch (m_token.tk)
  2277. {
  2278. case tkID:
  2279. // This is the default binding identifier.
  2280. // If we already saw a comma in the import clause, this is a syntax error.
  2281. if (parsingAfterComma)
  2282. {
  2283. Error(ERRsyntax);
  2284. }
  2285. if (buildAST)
  2286. {
  2287. IdentPtr localName = m_token.GetIdentifier(this->GetHashTbl());
  2288. IdentPtr importName = wellKnownPropertyPids._default;
  2289. CreateModuleImportDeclNode(localName);
  2290. AddModuleImportOrExportEntry(importEntryList, importName, localName, nullptr, nullptr);
  2291. }
  2292. break;
  2293. case tkLCurly:
  2294. // This begins a list of named imports.
  2295. ParseNamedImportOrExportClause<buildAST>(importEntryList, false);
  2296. parsedNamespaceOrNamedImport = true;
  2297. break;
  2298. case tkStar:
  2299. // This begins a namespace import clause.
  2300. // "* as ImportedBinding"
  2301. // Token following * must be the identifier 'as'
  2302. this->GetScanner()->Scan();
  2303. if (!CheckContextualKeyword(wellKnownPropertyPids.as))
  2304. {
  2305. Error(ERRValidIfFollowedBy, _u("import *"), _u("as"));
  2306. }
  2307. // Token following 'as' must be a binding identifier.
  2308. this->GetScanner()->Scan();
  2309. ChkCurTokNoScan(tkID, ERRnoIdent);
  2310. if (buildAST)
  2311. {
  2312. IdentPtr localName = m_token.GetIdentifier(this->GetHashTbl());
  2313. IdentPtr importName = wellKnownPropertyPids._star;
  2314. CreateModuleImportDeclNode(localName);
  2315. AddModuleImportOrExportEntry(importEntryList, importName, localName, nullptr, nullptr);
  2316. }
  2317. parsedNamespaceOrNamedImport = true;
  2318. break;
  2319. default:
  2320. Error(ERRTokenAfter, GetTokenString(m_token.tk), GetTokenString(this->GetScanner()->GetPrevious()));
  2321. }
  2322. this->GetScanner()->Scan();
  2323. if (m_token.tk == tkComma)
  2324. {
  2325. // There cannot be more than one comma in a module import clause.
  2326. // There cannot be a namespace import or named imports list on the left of the comma in a module import clause.
  2327. if (parsingAfterComma || parsedNamespaceOrNamedImport)
  2328. {
  2329. Error(ERRTokenAfter, _u(","), GetTokenString(this->GetScanner()->GetPrevious()));
  2330. }
  2331. this->GetScanner()->Scan();
  2332. ParseImportClause<buildAST>(importEntryList, true);
  2333. }
  2334. }
  2335. void Parser::CheckIfImportOrExportStatementValidHere()
  2336. {
  2337. ParseNodeFnc * curFunc = GetCurrentFunctionNode();
  2338. if (curFunc->nop != knopFncDecl || !curFunc->IsModule())
  2339. {
  2340. Error(ERRModuleImportOrExportInScript);
  2341. }
  2342. if (this->m_currentBlockInfo->pnodeBlock != curFunc->pnodeBodyScope
  2343. || (this->m_grfscr & fscrEvalCode) == fscrEvalCode
  2344. || this->m_tryCatchOrFinallyDepth != 0
  2345. || this->m_disallowImportExportStmt)
  2346. {
  2347. Error(ERRInvalidModuleImportOrExport);
  2348. }
  2349. }
  2350. bool Parser::IsTopLevelModuleFunc()
  2351. {
  2352. ParseNodeFnc * curFunc = GetCurrentFunctionNode();
  2353. return curFunc->nop == knopFncDecl && curFunc->IsModule();
  2354. }
  2355. void Parser::MakeModuleAsync()
  2356. {
  2357. Assert(IsTopLevelModuleFunc());
  2358. if (!m_scriptContext->GetConfig()->IsESTopLevelAwaitEnabled())
  2359. {
  2360. Error(ERRExperimental);
  2361. }
  2362. ParseNodeFnc * curFunc = GetCurrentFunctionNode();
  2363. curFunc->SetIsAsync();
  2364. }
  2365. template<bool buildAST> ParseNodePtr Parser::ParseImportCall()
  2366. {
  2367. this->GetScanner()->Scan();
  2368. ParseNodePtr specifier = ParseExpr<buildAST>(koplCma, nullptr, /* fAllowIn */FALSE, /* fAllowEllipsis */FALSE);
  2369. if (m_token.tk != tkRParen)
  2370. {
  2371. Error(ERRnoRparen);
  2372. }
  2373. this->GetScanner()->Scan();
  2374. return buildAST ? CreateCallNode(knopCall, CreateNodeForOpT<knopImport>(), specifier) : nullptr;
  2375. }
  2376. template<bool buildAST>
  2377. ParseNodePtr Parser::ParseImport()
  2378. {
  2379. Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
  2380. Assert(m_token.tk == tkIMPORT);
  2381. charcount_t ichMin = this->GetScanner()->IchMinTok();
  2382. RestorePoint parsedImport;
  2383. this->GetScanner()->Capture(&parsedImport);
  2384. this->GetScanner()->Scan();
  2385. // import()
  2386. if (m_token.tk == tkLParen)
  2387. {
  2388. if (!m_scriptContext->GetConfig()->IsESDynamicImportEnabled())
  2389. {
  2390. Error(ERRExperimental);
  2391. }
  2392. ParseNodePtr pnode = ParseImportCall<buildAST>();
  2393. BOOL fCanAssign;
  2394. IdentToken token;
  2395. return ParsePostfixOperators<buildAST>(pnode, TRUE, FALSE, FALSE, TRUE, &fCanAssign, &token);
  2396. }
  2397. else if (m_token.tk == tkDot && m_scriptContext->GetConfig()->IsESImportMetaEnabled())
  2398. {
  2399. BOOL fCanAssign;
  2400. ParseMetaProperty<buildAST>(tkIMPORT, ichMin, &fCanAssign);
  2401. this->GetScanner()->SeekTo(parsedImport);
  2402. return ParseExpr<buildAST>();
  2403. }
  2404. this->GetScanner()->SeekTo(parsedImport);
  2405. CheckIfImportOrExportStatementValidHere();
  2406. // We just parsed an import token. Next valid token is *, {, string constant, or binding identifier.
  2407. this->GetScanner()->Scan();
  2408. if (m_token.tk == tkStrCon)
  2409. {
  2410. // This import declaration has no import clause.
  2411. // "import ModuleSpecifier;"
  2412. if (buildAST)
  2413. {
  2414. AddModuleSpecifier(m_token.GetStr());
  2415. }
  2416. // Scan past the module identifier.
  2417. this->GetScanner()->Scan();
  2418. }
  2419. else
  2420. {
  2421. ModuleImportOrExportEntryList importEntryList(&m_nodeAllocator);
  2422. // Parse the import clause (default binding can only exist before the comma).
  2423. ParseImportClause<buildAST>(&importEntryList);
  2424. // Token following import clause must be the identifier 'from'
  2425. IdentPtr moduleSpecifier = ParseImportOrExportFromClause<buildAST>(true);
  2426. if (buildAST)
  2427. {
  2428. Assert(moduleSpecifier != nullptr);
  2429. AddModuleSpecifier(moduleSpecifier);
  2430. importEntryList.Map([this, moduleSpecifier](ModuleImportOrExportEntry& importEntry) {
  2431. importEntry.moduleRequest = moduleSpecifier;
  2432. AddModuleImportOrExportEntry(EnsureModuleImportEntryList(), &importEntry);
  2433. });
  2434. }
  2435. importEntryList.Clear();
  2436. }
  2437. // Import statement is actually a nop, we hoist all the imported bindings to the top of the module.
  2438. return nullptr;
  2439. }
  2440. template<bool buildAST>
  2441. IdentPtr Parser::ParseImportOrExportFromClause(bool throwIfNotFound)
  2442. {
  2443. IdentPtr moduleSpecifier = nullptr;
  2444. if (CheckContextualKeyword(wellKnownPropertyPids.from))
  2445. {
  2446. this->GetScanner()->Scan();
  2447. // Token following the 'from' token must be a string constant - the module specifier.
  2448. ChkCurTokNoScan(tkStrCon, ERRValidIfFollowedBy, _u("'from'"), _u("a module specifier."));
  2449. if (buildAST)
  2450. {
  2451. moduleSpecifier = m_token.GetStr();
  2452. }
  2453. this->GetScanner()->Scan();
  2454. }
  2455. else if (throwIfNotFound)
  2456. {
  2457. Error(ERRMissingFrom);
  2458. }
  2459. return moduleSpecifier;
  2460. }
  2461. template<bool buildAST>
  2462. ParseNodePtr Parser::ParseDefaultExportClause()
  2463. {
  2464. Assert(m_token.tk == tkDEFAULT);
  2465. this->GetScanner()->Scan();
  2466. ParseNodePtr pnode = nullptr;
  2467. ushort flags = fFncNoFlgs;
  2468. switch (m_token.tk)
  2469. {
  2470. case tkCLASS:
  2471. {
  2472. // Before we parse the class itself we need to know if the class has an identifier name.
  2473. // If it does, we'll treat this class as an ordinary class declaration which will bind
  2474. // it to that name. Otherwise the class should parse as a nameless class expression and
  2475. // bind only to the export binding.
  2476. BOOL classHasName = false;
  2477. RestorePoint parsedClass;
  2478. this->GetScanner()->Capture(&parsedClass);
  2479. this->GetScanner()->Scan();
  2480. if (m_token.tk == tkID)
  2481. {
  2482. classHasName = true;
  2483. }
  2484. this->GetScanner()->SeekTo(parsedClass);
  2485. ParseNodeClass * pnodeClass;
  2486. pnode = pnodeClass = ParseClassDecl<buildAST>(classHasName, nullptr, nullptr, nullptr);
  2487. if (buildAST)
  2488. {
  2489. AnalysisAssert(pnode != nullptr);
  2490. Assert(pnode->nop == knopClassDecl);
  2491. pnodeClass->SetIsDefaultModuleExport(true);
  2492. }
  2493. break;
  2494. }
  2495. case tkID:
  2496. // If we parsed an async token, it could either modify the next token (if it is a
  2497. // function token) or it could be an identifier (let async = 0; export default async;).
  2498. // To handle both cases, when we parse an async token we need to keep the parser state
  2499. // and rewind if the next token is not function.
  2500. if (CheckContextualKeyword(wellKnownPropertyPids.async))
  2501. {
  2502. RestorePoint parsedAsync;
  2503. this->GetScanner()->Capture(&parsedAsync);
  2504. this->GetScanner()->Scan();
  2505. if (m_token.tk == tkFUNCTION)
  2506. {
  2507. // Token after async is function, consume the async token and continue to parse the
  2508. // function as an async function.
  2509. flags |= fFncAsync;
  2510. goto LFunction;
  2511. }
  2512. // Token after async is not function, no idea what the async token is supposed to mean
  2513. // so rewind and let the default case handle it.
  2514. this->GetScanner()->SeekTo(parsedAsync);
  2515. }
  2516. goto LDefault;
  2517. break;
  2518. case tkFUNCTION:
  2519. {
  2520. LFunction:
  2521. // We just parsed a function token but we need to figure out if the function
  2522. // has an identifier name or not before we call the helper.
  2523. RestorePoint parsedFunction;
  2524. this->GetScanner()->Capture(&parsedFunction);
  2525. this->GetScanner()->Scan();
  2526. if (m_token.tk == tkStar)
  2527. {
  2528. // If we saw 'function*' that indicates we are going to parse a generator,
  2529. // but doesn't tell us if the generator has an identifier or not.
  2530. // Skip the '*' token for now as it doesn't matter yet.
  2531. this->GetScanner()->Scan();
  2532. }
  2533. // We say that if the function has an identifier name, it is a 'normal' declaration
  2534. // and should create a binding to that identifier as well as one for our default export.
  2535. if (m_token.tk == tkID)
  2536. {
  2537. flags |= fFncDeclaration;
  2538. }
  2539. else
  2540. {
  2541. flags |= fFncNoName;
  2542. }
  2543. // Rewind back to the function token and let the helper handle the parsing.
  2544. this->GetScanner()->SeekTo(parsedFunction);
  2545. pnode = ParseFncDeclCheckScope<buildAST>(flags);
  2546. if (buildAST)
  2547. {
  2548. AnalysisAssert(pnode != nullptr);
  2549. Assert(pnode->nop == knopFncDecl);
  2550. pnode->AsParseNodeFnc()->SetIsDefaultModuleExport(true);
  2551. }
  2552. break;
  2553. }
  2554. default:
  2555. LDefault:
  2556. {
  2557. ParseNodePtr pnodeExpression = ParseExpr<buildAST>();
  2558. // Consider: Can we detect this syntax error earlier?
  2559. if (pnodeExpression && pnodeExpression->nop == knopComma)
  2560. {
  2561. Error(ERRsyntax);
  2562. }
  2563. if (buildAST)
  2564. {
  2565. AnalysisAssert(pnodeExpression != nullptr);
  2566. // Mark this node as the default module export. We need to make sure it is put into the correct
  2567. // module export slot when we emit the node.
  2568. ParseNodeExportDefault * pnodeExportDefault;
  2569. pnode = pnodeExportDefault = CreateNodeForOpT<knopExportDefault>();
  2570. pnode->AsParseNodeExportDefault()->pnodeExpr = pnodeExpression;
  2571. }
  2572. break;
  2573. }
  2574. }
  2575. IdentPtr exportName = wellKnownPropertyPids._default;
  2576. AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), nullptr, exportName, exportName, nullptr);
  2577. return pnode;
  2578. }
  2579. template<bool buildAST>
  2580. ParseNodePtr Parser::ParseExportDeclaration(bool *needTerminator)
  2581. {
  2582. Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
  2583. Assert(m_token.tk == tkEXPORT);
  2584. CheckIfImportOrExportStatementValidHere();
  2585. ParseNodePtr pnode = nullptr;
  2586. IdentPtr moduleIdentifier = nullptr;
  2587. tokens declarationType;
  2588. if (needTerminator != nullptr)
  2589. {
  2590. *needTerminator = false;
  2591. }
  2592. // We just parsed an export token. Next valid tokens are *, {, var, let, const, async, function, class, default.
  2593. this->GetScanner()->Scan();
  2594. switch (m_token.tk)
  2595. {
  2596. case tkStar:
  2597. {
  2598. this->GetScanner()->Scan();
  2599. IdentPtr exportName = nullptr;
  2600. if (m_scriptContext->GetConfig()->IsESExportNsAsEnabled())
  2601. {
  2602. // check for 'as'
  2603. if (CheckContextualKeyword(wellKnownPropertyPids.as))
  2604. {
  2605. // scan to the next token
  2606. this->GetScanner()->Scan();
  2607. // token after as must be an identifier
  2608. if (!(m_token.IsIdentifier() || m_token.IsReservedWord()))
  2609. {
  2610. Error(ERRValidIfFollowedBy, _u("'as'"), _u("an identifier."));
  2611. }
  2612. exportName = m_token.GetIdentifier(this->GetHashTbl());
  2613. // scan to next token
  2614. this->GetScanner()->Scan();
  2615. }
  2616. }
  2617. // A star token in an export declaration must be followed by a from clause which begins with a token 'from'.
  2618. moduleIdentifier = ParseImportOrExportFromClause<buildAST>(true);
  2619. if (buildAST)
  2620. {
  2621. Assert(moduleIdentifier != nullptr);
  2622. AddModuleSpecifier(moduleIdentifier);
  2623. if (!exportName)
  2624. {
  2625. AddModuleImportOrExportEntry(EnsureModuleStarExportEntryList(), wellKnownPropertyPids._star, nullptr, nullptr, moduleIdentifier);
  2626. }
  2627. else
  2628. {
  2629. CheckForDuplicateExportEntry(exportName);
  2630. AddModuleImportOrExportEntry(EnsureModuleIndirectExportEntryList(), wellKnownPropertyPids._star, nullptr, exportName, moduleIdentifier);
  2631. }
  2632. }
  2633. if (needTerminator != nullptr)
  2634. {
  2635. *needTerminator = true;
  2636. }
  2637. break;
  2638. }
  2639. case tkLCurly:
  2640. {
  2641. ModuleImportOrExportEntryList exportEntryList(&m_nodeAllocator);
  2642. ParseNamedImportOrExportClause<buildAST>(&exportEntryList, true);
  2643. this->GetScanner()->Scan();
  2644. // Export clause may be followed by a from clause.
  2645. moduleIdentifier = ParseImportOrExportFromClause<buildAST>(false);
  2646. if (buildAST)
  2647. {
  2648. if (moduleIdentifier != nullptr)
  2649. {
  2650. AddModuleSpecifier(moduleIdentifier);
  2651. }
  2652. exportEntryList.Map([this, moduleIdentifier](ModuleImportOrExportEntry& exportEntry) {
  2653. if (moduleIdentifier != nullptr)
  2654. {
  2655. exportEntry.moduleRequest = moduleIdentifier;
  2656. // We need to swap localname and importname when this is a re-export.
  2657. exportEntry.importName = exportEntry.localName;
  2658. exportEntry.localName = nullptr;
  2659. AddModuleImportOrExportEntry(EnsureModuleIndirectExportEntryList(), &exportEntry);
  2660. }
  2661. else
  2662. {
  2663. AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), &exportEntry);
  2664. }
  2665. });
  2666. exportEntryList.Clear();
  2667. }
  2668. }
  2669. if (needTerminator != nullptr)
  2670. {
  2671. *needTerminator = true;
  2672. }
  2673. break;
  2674. case tkID:
  2675. {
  2676. IdentPtr pid = nullptr;
  2677. if (!this->GetScanner()->LastIdentifierHasEscape())
  2678. {
  2679. pid = m_token.GetIdentifier(this->GetHashTbl());
  2680. }
  2681. if (pid == wellKnownPropertyPids.let)
  2682. {
  2683. declarationType = tkLET;
  2684. goto ParseVarDecl;
  2685. }
  2686. if (pid == wellKnownPropertyPids.async && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  2687. {
  2688. // In module export statements, async token is only valid if it's followed by function.
  2689. // We need to check here because ParseStatement would think 'async = 20' is a var decl.
  2690. RestorePoint parsedAsync;
  2691. this->GetScanner()->Capture(&parsedAsync);
  2692. this->GetScanner()->Scan();
  2693. if (m_token.tk == tkFUNCTION)
  2694. {
  2695. // Token after async is function, rewind to the async token and let ParseStatement handle it.
  2696. this->GetScanner()->SeekTo(parsedAsync);
  2697. goto ParseFunctionDecl;
  2698. }
  2699. // Token after async is not function, it's a syntax error.
  2700. }
  2701. goto ErrorToken;
  2702. }
  2703. case tkVAR:
  2704. case tkLET:
  2705. case tkCONST:
  2706. {
  2707. declarationType = m_token.tk;
  2708. ParseVarDecl:
  2709. this->GetScanner()->Scan();
  2710. pnode = ParseVariableDeclaration<buildAST>(declarationType, this->GetScanner()->IchMinTok());
  2711. if (buildAST)
  2712. {
  2713. ForEachItemInList(pnode, [&](ParseNodePtr item) {
  2714. if (item->nop == knopAsg)
  2715. {
  2716. Parser::MapBindIdentifier(item, [&](ParseNodePtr subItem)
  2717. {
  2718. AddModuleLocalExportEntry(subItem);
  2719. });
  2720. }
  2721. else
  2722. {
  2723. AddModuleLocalExportEntry(item);
  2724. }
  2725. });
  2726. }
  2727. }
  2728. break;
  2729. case tkFUNCTION:
  2730. case tkCLASS:
  2731. {
  2732. ParseFunctionDecl:
  2733. pnode = ParseStatement<buildAST>();
  2734. if (buildAST)
  2735. {
  2736. IdentPtr localName;
  2737. if (pnode->nop == knopClassDecl)
  2738. {
  2739. ParseNodeClass * pnodeClass = pnode->AsParseNodeClass();
  2740. pnodeClass->pnodeDeclName->sym->SetIsModuleExportStorage(true);
  2741. localName = pnodeClass->pnodeName->pid;
  2742. }
  2743. else
  2744. {
  2745. Assert(pnode->nop == knopFncDecl);
  2746. ParseNodeFnc * pnodeFnc = pnode->AsParseNodeFnc();
  2747. pnodeFnc->GetFuncSymbol()->SetIsModuleExportStorage(true);
  2748. localName = pnodeFnc->pid;
  2749. }
  2750. Assert(localName != nullptr);
  2751. AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), nullptr, localName, localName, nullptr);
  2752. }
  2753. }
  2754. break;
  2755. case tkDEFAULT:
  2756. {
  2757. pnode = ParseDefaultExportClause<buildAST>();
  2758. }
  2759. break;
  2760. default:
  2761. {
  2762. ErrorToken:
  2763. Error(ERRsyntax);
  2764. }
  2765. }
  2766. return pnode;
  2767. }
  2768. /***************************************************************************
  2769. Parse an expression term.
  2770. ***************************************************************************/
  2771. template<bool buildAST>
  2772. ParseNodePtr Parser::ParseTerm(BOOL fAllowCall,
  2773. LPCOLESTR pNameHint,
  2774. uint32 *pHintLength,
  2775. uint32 *pShortNameOffset,
  2776. _Inout_opt_ IdentToken* pToken /*= nullptr*/,
  2777. bool fUnaryOrParen /*= false*/,
  2778. BOOL fCanAssignToCall /*= TRUE*/,
  2779. _Out_opt_ BOOL* pfCanAssign /*= nullptr*/,
  2780. _Inout_opt_ BOOL* pfLikelyPattern /*= nullptr*/,
  2781. _Out_opt_ bool* pfIsDotOrIndex /*= nullptr*/,
  2782. _Inout_opt_ charcount_t *plastRParen /*= nullptr*/,
  2783. _Out_opt_ bool* looseCoalesce /*= nullptr*/)
  2784. {
  2785. ParseNodePtr pnode = nullptr;
  2786. PidRefStack *savedTopAsyncRef = nullptr;
  2787. charcount_t ichMin = 0;
  2788. charcount_t ichLim = 0;
  2789. size_t iecpMin = 0;
  2790. size_t iecpLim = 0;
  2791. size_t iuMin;
  2792. IdentToken term;
  2793. BOOL fInNew = FALSE;
  2794. BOOL fCanAssign = TRUE;
  2795. bool isAsyncExpr = false;
  2796. bool isLambdaExpr = false;
  2797. bool isSpecialName = false;
  2798. IdentPtr pid = nullptr;
  2799. Assert(pToken == nullptr || pToken->tk == tkNone); // Must be empty initially
  2800. PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackParseOneTerm);
  2801. switch (m_token.tk)
  2802. {
  2803. case tkID:
  2804. {
  2805. pid = m_token.GetIdentifier(this->GetHashTbl());
  2806. ichMin = this->GetScanner()->IchMinTok();
  2807. iecpMin = this->GetScanner()->IecpMinTok();
  2808. ichLim = this->GetScanner()->IchLimTok();
  2809. iecpLim = this->GetScanner()->IecpLimTok();
  2810. if (pid == wellKnownPropertyPids.async &&
  2811. !this->GetScanner()->LastIdentifierHasEscape() &&
  2812. m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  2813. {
  2814. isAsyncExpr = true;
  2815. }
  2816. // Put this into a block to avoid previousAwaitIsKeyword being not initialized after jump to LIdentifier
  2817. {
  2818. bool previousAwaitIsKeyword = this->GetScanner()->SetAwaitIsKeywordRegion(isAsyncExpr);
  2819. this->GetScanner()->Scan();
  2820. this->GetScanner()->SetAwaitIsKeywordRegion(previousAwaitIsKeyword);
  2821. }
  2822. // We search for an Async expression (a function declaration or an async lambda expression)
  2823. if (isAsyncExpr && !this->GetScanner()->FHadNewLine())
  2824. {
  2825. if (m_token.tk == tkFUNCTION)
  2826. {
  2827. goto LFunction;
  2828. }
  2829. else if (m_token.tk == tkID || m_token.tk == tkAWAIT)
  2830. {
  2831. isLambdaExpr = true;
  2832. goto LFunction;
  2833. }
  2834. else if (m_token.tk == tkLParen)
  2835. {
  2836. // This is potentially an async arrow function. Save the state of the async references
  2837. // in case it needs to be restored. (Note that the case of a single parameter with no ()'s
  2838. // is detected upstream and need not be handled here.)
  2839. savedTopAsyncRef = pid->GetTopRef();
  2840. }
  2841. }
  2842. CheckArgumentsUse(pid, GetCurrentFunctionNode());
  2843. // Assume this pid is not special - overwrite when we parse a special name
  2844. isSpecialName = false;
  2845. LIdentifier:
  2846. PidRefStack * ref = nullptr;
  2847. // Don't push a reference if this is a single lambda parameter, because we'll reparse with
  2848. // a correct function ID.
  2849. if (m_token.tk != tkDArrow)
  2850. {
  2851. ref = this->PushPidRef(pid);
  2852. }
  2853. if (buildAST)
  2854. {
  2855. if (isSpecialName)
  2856. {
  2857. pnode = CreateSpecialNameNode(pid, ref, ichMin, ichLim);
  2858. }
  2859. else
  2860. {
  2861. pnode = CreateNameNode(pid, ref, ichMin, ichLim);
  2862. }
  2863. }
  2864. else
  2865. {
  2866. // Remember the identifier start and end in case it turns out to be a statement label.
  2867. term.tk = tkID;
  2868. term.pid = pid; // Record the identifier for detection of eval
  2869. term.ichMin = static_cast<charcount_t>(iecpMin);
  2870. term.ichLim = static_cast<charcount_t>(iecpLim);
  2871. }
  2872. break;
  2873. }
  2874. case tkSUPER:
  2875. ichMin = this->GetScanner()->IchMinTok();
  2876. iecpMin = this->GetScanner()->IecpMinTok();
  2877. ichLim = this->GetScanner()->IchLimTok();
  2878. iecpLim = this->GetScanner()->IecpLimTok();
  2879. this->GetScanner()->Scan();
  2880. pid = ParseSuper<buildAST>(!!fAllowCall);
  2881. isSpecialName = true;
  2882. fCanAssign = FALSE;
  2883. // Super reference and super call need to push a pid ref to 'this' even when not building an AST
  2884. ReferenceSpecialName(wellKnownPropertyPids._this, ichMin, ichLim);
  2885. // Super call needs to reference 'new.target'
  2886. if (pid == wellKnownPropertyPids._superConstructor)
  2887. {
  2888. // super() will write to "this", so track the assignment.
  2889. PidRefStack *thisRef = wellKnownPropertyPids._this->GetTopRef();
  2890. thisRef->isAsg = true;
  2891. ReferenceSpecialName(wellKnownPropertyPids._newTarget, ichMin, ichLim);
  2892. }
  2893. goto LIdentifier;
  2894. case tkTHIS:
  2895. ichMin = this->GetScanner()->IchMinTok();
  2896. iecpMin = this->GetScanner()->IecpMinTok();
  2897. ichLim = this->GetScanner()->IchLimTok();
  2898. iecpLim = this->GetScanner()->IecpLimTok();
  2899. pid = wellKnownPropertyPids._this;
  2900. this->GetScanner()->Scan();
  2901. isSpecialName = true;
  2902. fCanAssign = FALSE;
  2903. goto LIdentifier;
  2904. case tkLParen:
  2905. {
  2906. ichMin = this->GetScanner()->IchMinTok();
  2907. iuMin = this->GetScanner()->IecpMinTok();
  2908. // If we are undeferring a function which has deferred stubs, we can check to see if the next deferred stub
  2909. // is a lambda at the current character. If it is, we know this LParen is the beginning of a lambda nested
  2910. // function and we can avoid parsing the next series of tokens as a parenthetical expression and reparsing
  2911. // after finding the => token.
  2912. if (buildAST && m_currDeferredStub != nullptr && GetCurrentFunctionNode() != nullptr && GetCurrentFunctionNode()->nestedCount < m_currDeferredStubCount)
  2913. {
  2914. DeferredFunctionStub* stub = m_currDeferredStub + GetCurrentFunctionNode()->nestedCount;
  2915. if (stub->ichMin == ichMin)
  2916. {
  2917. Assert((stub->fncFlags & kFunctionIsLambda) == kFunctionIsLambda);
  2918. pnode = ParseFncDeclCheckScope<true>(fFncLambda);
  2919. break;
  2920. }
  2921. }
  2922. this->GetScanner()->Scan();
  2923. if (m_token.tk == tkRParen)
  2924. {
  2925. // Empty parens can only be legal as an empty parameter list to a lambda declaration.
  2926. // We're in a lambda if the next token is =>.
  2927. fAllowCall = FALSE;
  2928. this->GetScanner()->Scan();
  2929. // If the token after the right paren is not => or if there was a newline between () and => this is a syntax error
  2930. if (!IsDoingFastScan() && (m_token.tk != tkDArrow || this->GetScanner()->FHadNewLine()))
  2931. {
  2932. Error(ERRValidIfFollowedBy, _u("Lambda parameter list"), _u("'=>' on the same line"));
  2933. }
  2934. if (buildAST)
  2935. {
  2936. pnode = CreateNodeForOpT<knopEmpty>();
  2937. }
  2938. break;
  2939. }
  2940. // Advance the block ID here in case this parenthetical expression turns out to be a lambda parameter list.
  2941. // That way the pid ref stacks will be created in their correct final form, and we can simply fix
  2942. // up function ID's.
  2943. uint saveNextBlockId = m_nextBlockId;
  2944. uint saveCurrBlockId = GetCurrentBlock()->blockId;
  2945. GetCurrentBlock()->blockId = m_nextBlockId++;
  2946. AutoDeferErrorsRestore deferErrorRestore(this);
  2947. this->m_funcParenExprDepth++;
  2948. pnode = ParseExpr<buildAST>(koplNo, &fCanAssign, TRUE, FALSE, nullptr, nullptr /*nameLength*/, nullptr /*pShortNameOffset*/, &term, true, nullptr, plastRParen, looseCoalesce);
  2949. this->m_funcParenExprDepth--;
  2950. if (buildAST && plastRParen)
  2951. {
  2952. *plastRParen = this->GetScanner()->IchLimTok();
  2953. }
  2954. ChkCurTok(tkRParen, ERRnoRparen);
  2955. if (looseCoalesce != nullptr)
  2956. {
  2957. *looseCoalesce = false;
  2958. }
  2959. GetCurrentBlock()->blockId = saveCurrBlockId;
  2960. if (m_token.tk == tkDArrow)
  2961. {
  2962. // We're going to rewind and reinterpret the expression as a parameter list.
  2963. // Put back the original next-block-ID so the existing pid ref stacks will be correct.
  2964. m_nextBlockId = saveNextBlockId;
  2965. }
  2966. else
  2967. {
  2968. // Emit a deferred ... error if one was parsed.
  2969. if (m_deferEllipsisError)
  2970. {
  2971. this->GetScanner()->SeekTo(m_deferEllipsisErrorLoc);
  2972. Error(ERRInvalidSpreadUse);
  2973. }
  2974. else if (m_deferCommaError)
  2975. {
  2976. // Emit a comma error if that was deferred.
  2977. this->GetScanner()->SeekTo(m_deferCommaErrorLoc);
  2978. Error(ERRsyntax);
  2979. }
  2980. }
  2981. break;
  2982. }
  2983. case tkIntCon:
  2984. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  2985. {
  2986. Error(ERRES5NoOctal);
  2987. }
  2988. if (buildAST)
  2989. {
  2990. pnode = CreateIntNode(m_token.GetLong());
  2991. }
  2992. fCanAssign = FALSE;
  2993. this->GetScanner()->Scan();
  2994. break;
  2995. case tkBigIntCon:
  2996. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  2997. {
  2998. Error(ERRES5NoOctal);
  2999. }
  3000. if (buildAST)
  3001. {
  3002. pnode = CreateBigIntNode(m_token.GetBigInt());
  3003. }
  3004. fCanAssign = FALSE;
  3005. this->GetScanner()->Scan();
  3006. break;
  3007. case tkFltCon:
  3008. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  3009. {
  3010. Error(ERRES5NoOctal);
  3011. }
  3012. if (buildAST)
  3013. {
  3014. ParseNodeFloat * pnodeFloat;
  3015. pnode = pnodeFloat = CreateNodeForOpT<knopFlt>();
  3016. pnodeFloat->dbl = m_token.GetDouble();
  3017. pnodeFloat->maybeInt = m_token.GetDoubleMayBeInt();
  3018. }
  3019. fCanAssign = FALSE;
  3020. this->GetScanner()->Scan();
  3021. break;
  3022. case tkStrCon:
  3023. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  3024. {
  3025. Error(ERRES5NoOctal);
  3026. }
  3027. if (buildAST)
  3028. {
  3029. pnode = CreateStrNode(m_token.GetStr());
  3030. }
  3031. else
  3032. {
  3033. // Subtract the string literal length from the total char count for the purpose
  3034. // of deciding whether to defer parsing and byte code generation.
  3035. this->ReduceDeferredScriptLength(this->GetScanner()->IchLimTok() - this->GetScanner()->IchMinTok());
  3036. }
  3037. fCanAssign = FALSE;
  3038. this->GetScanner()->Scan();
  3039. break;
  3040. case tkTRUE:
  3041. if (buildAST)
  3042. {
  3043. pnode = CreateNodeForOpT<knopTrue>();
  3044. }
  3045. fCanAssign = FALSE;
  3046. this->GetScanner()->Scan();
  3047. break;
  3048. case tkFALSE:
  3049. if (buildAST)
  3050. {
  3051. pnode = CreateNodeForOpT<knopFalse>();
  3052. }
  3053. fCanAssign = FALSE;
  3054. this->GetScanner()->Scan();
  3055. break;
  3056. case tkNULL:
  3057. if (buildAST)
  3058. {
  3059. pnode = CreateNodeForOpT<knopNull>();
  3060. }
  3061. fCanAssign = FALSE;
  3062. this->GetScanner()->Scan();
  3063. break;
  3064. case tkDiv:
  3065. case tkAsgDiv:
  3066. pnode = ParseRegExp<buildAST>();
  3067. fCanAssign = FALSE;
  3068. this->GetScanner()->Scan();
  3069. break;
  3070. case tkNEW:
  3071. {
  3072. ichMin = this->GetScanner()->IchMinTok();
  3073. iecpMin = this->GetScanner()->IecpMinTok();
  3074. this->GetScanner()->Scan();
  3075. if (m_token.tk == tkDot)
  3076. {
  3077. pid = ParseMetaProperty<buildAST>(tkNEW, ichMin, &fCanAssign);
  3078. ichLim = this->GetScanner()->IchLimTok();
  3079. iecpLim = this->GetScanner()->IecpLimTok();
  3080. this->GetScanner()->Scan();
  3081. isSpecialName = true;
  3082. fCanAssign = FALSE;
  3083. goto LIdentifier;
  3084. }
  3085. else
  3086. {
  3087. ParseNodePtr pnodeExpr = ParseTerm<buildAST>(FALSE, pNameHint, pHintLength, pShortNameOffset, nullptr, false, TRUE, nullptr, nullptr, nullptr, plastRParen);
  3088. if (buildAST)
  3089. {
  3090. pnode = CreateCallNode(knopNew, pnodeExpr, nullptr);
  3091. pnode->ichMin = ichMin;
  3092. }
  3093. fInNew = TRUE;
  3094. fCanAssign = FALSE;
  3095. }
  3096. break;
  3097. }
  3098. case tkLBrack:
  3099. {
  3100. ichMin = this->GetScanner()->IchMinTok();
  3101. this->GetScanner()->Scan();
  3102. pnode = ParseArrayLiteral<buildAST>();
  3103. if (buildAST)
  3104. {
  3105. pnode->ichMin = ichMin;
  3106. pnode->ichLim = this->GetScanner()->IchLimTok();
  3107. }
  3108. if (this->m_arrayDepth == 0)
  3109. {
  3110. Assert(this->GetScanner()->IchLimTok() - ichMin > m_funcInArray);
  3111. this->ReduceDeferredScriptLength(this->GetScanner()->IchLimTok() - ichMin - this->m_funcInArray);
  3112. this->m_funcInArray = 0;
  3113. this->m_funcInArrayDepth = 0;
  3114. }
  3115. ChkCurTok(tkRBrack, ERRnoRbrack);
  3116. if (pfLikelyPattern != nullptr && !IsPostFixOperators())
  3117. {
  3118. *pfLikelyPattern = TRUE;
  3119. }
  3120. fCanAssign = FALSE;
  3121. break;
  3122. }
  3123. case tkLCurly:
  3124. {
  3125. ichMin = this->GetScanner()->IchMinTok();
  3126. this->GetScanner()->ScanForcingPid();
  3127. ParseNodePtr pnodeMemberList = ParseMemberList<buildAST>(pNameHint, pHintLength);
  3128. if (buildAST)
  3129. {
  3130. pnode = CreateUniNode(knopObject, pnodeMemberList);
  3131. pnode->ichMin = ichMin;
  3132. pnode->ichLim = this->GetScanner()->IchLimTok();
  3133. }
  3134. ChkCurTok(tkRCurly, ERRnoRcurly);
  3135. if (pfLikelyPattern != nullptr && !IsPostFixOperators())
  3136. {
  3137. *pfLikelyPattern = TRUE;
  3138. }
  3139. fCanAssign = FALSE;
  3140. break;
  3141. }
  3142. case tkFUNCTION:
  3143. {
  3144. LFunction:
  3145. if (m_grfscr & fscrDeferredFncExpression)
  3146. {
  3147. // The top-level deferred function body was defined by a function expression whose parsing was deferred. We are now
  3148. // parsing it, so unset the flag so that any nested functions are parsed normally. This flag is only applicable the
  3149. // first time we see it.
  3150. //
  3151. // Normally, deferred functions will be parsed in ParseStatement upon encountering the 'function' token. The first
  3152. // token of the source code of the function may not a 'function' token though, so we still need to reset this flag
  3153. // for the first function we parse. This can happen in compat modes, for instance, for a function expression enclosed
  3154. // in parentheses, where the legacy behavior was to include the parentheses in the function's source code.
  3155. m_grfscr &= ~fscrDeferredFncExpression;
  3156. }
  3157. ushort flags = fFncNoFlgs;
  3158. if (isLambdaExpr)
  3159. {
  3160. flags |= fFncLambda;
  3161. }
  3162. if (isAsyncExpr)
  3163. {
  3164. flags |= fFncAsync;
  3165. }
  3166. pnode = ParseFncDeclNoCheckScope<buildAST>(flags, SuperRestrictionState::Disallowed, pNameHint, /* needsPIDOnRCurlyScan */ false, fUnaryOrParen);
  3167. if (isAsyncExpr)
  3168. {
  3169. pnode->AsParseNodeFnc()->cbStringMin = iecpMin;
  3170. pnode->AsParseNodeFnc()->cbStringLim = pnode->AsParseNodeFnc()->cbLim;
  3171. }
  3172. fCanAssign = FALSE;
  3173. break;
  3174. }
  3175. case tkCLASS:
  3176. pnode = ParseClassDecl<buildAST>(FALSE, pNameHint, pHintLength, pShortNameOffset);
  3177. fCanAssign = FALSE;
  3178. break;
  3179. case tkStrTmplBasic:
  3180. case tkStrTmplBegin:
  3181. pnode = ParseStringTemplateDecl<buildAST>(nullptr);
  3182. fCanAssign = FALSE;
  3183. break;
  3184. case tkIMPORT:
  3185. if (m_scriptContext->GetConfig()->IsES6ModuleEnabled())
  3186. {
  3187. ichMin = this->GetScanner()->IchMinTok();
  3188. iecpMin = this->GetScanner()->IecpMinTok();
  3189. this->GetScanner()->Scan();
  3190. switch (m_token.tk)
  3191. {
  3192. case tkLParen:
  3193. if (!m_scriptContext->GetConfig()->IsESDynamicImportEnabled())
  3194. {
  3195. goto LUnknown;
  3196. }
  3197. if (!fAllowCall)
  3198. {
  3199. Error(ERRTokenAfter, _u("import"), _u("new"));
  3200. }
  3201. pnode = ParseImportCall<buildAST>();
  3202. break;
  3203. case tkDot:
  3204. if (!(m_grfscr & fscrIsModuleCode) || !m_scriptContext->GetConfig()->IsESImportMetaEnabled())
  3205. {
  3206. goto LUnknown;
  3207. }
  3208. pid = ParseMetaProperty<buildAST>(tkIMPORT, ichMin, &fCanAssign);
  3209. ichLim = this->GetScanner()->IchLimTok();
  3210. iecpLim = this->GetScanner()->IecpLimTok();
  3211. this->GetScanner()->Scan();
  3212. isSpecialName = true;
  3213. goto LIdentifier;
  3214. default:
  3215. Error(ERRsyntax);
  3216. }
  3217. }
  3218. else
  3219. {
  3220. goto LUnknown;
  3221. }
  3222. break;
  3223. #if ENABLE_BACKGROUND_PARSING
  3224. case tkCASE:
  3225. {
  3226. if (!m_doingFastScan)
  3227. {
  3228. goto LUnknown;
  3229. }
  3230. ParseNodePtr pnodeUnused;
  3231. pnode = ParseCase<buildAST>(&pnodeUnused);
  3232. break;
  3233. }
  3234. case tkELSE:
  3235. if (!m_doingFastScan)
  3236. {
  3237. goto LUnknown;
  3238. }
  3239. this->GetScanner()->Scan();
  3240. ParseStatement<buildAST>();
  3241. break;
  3242. #endif
  3243. default:
  3244. LUnknown:
  3245. if (m_token.tk == tkNone)
  3246. {
  3247. Error(ERRInvalidIdentifier, m_token.GetIdentifier(this->GetHashTbl())->Psz(), GetTokenString(GetScanner()->GetPrevious()));
  3248. }
  3249. else if (m_token.IsKeyword())
  3250. {
  3251. Error(ERRKeywordAfter, GetTokenString(m_token.tk), GetTokenString(GetScanner()->GetPrevious()));
  3252. }
  3253. else
  3254. {
  3255. Error(ERRTokenAfter, GetTokenString(m_token.tk), GetTokenString(GetScanner()->GetPrevious()));
  3256. }
  3257. break;
  3258. }
  3259. pnode = ParsePostfixOperators<buildAST>(pnode, fAllowCall, fInNew, isAsyncExpr, fCanAssignToCall, &fCanAssign, &term, pfIsDotOrIndex);
  3260. if (savedTopAsyncRef != nullptr &&
  3261. this->m_token.tk == tkDArrow)
  3262. {
  3263. // This is an async arrow function; we're going to back up and reparse it.
  3264. // Make sure we don't leave behind a bogus reference to the 'async' identifier.
  3265. for (pid = wellKnownPropertyPids.async; pid->GetTopRef() != savedTopAsyncRef;)
  3266. {
  3267. Assert(pid->GetTopRef() != nullptr);
  3268. pid->RemovePrevPidRef(nullptr);
  3269. }
  3270. }
  3271. // Pass back identifier if requested
  3272. if (pToken && term.tk == tkID)
  3273. {
  3274. *pToken = term;
  3275. }
  3276. if (pfCanAssign)
  3277. {
  3278. *pfCanAssign = fCanAssign;
  3279. }
  3280. return pnode;
  3281. }
  3282. template <bool buildAST>
  3283. ParseNodeRegExp * Parser::ParseRegExp()
  3284. {
  3285. ParseNodeRegExp * pnode = nullptr;
  3286. if (buildAST || IsDoingFastScan())
  3287. {
  3288. this->GetScanner()->RescanRegExp();
  3289. #if ENABLE_BACKGROUND_PARSING
  3290. BOOL saveDeferringAST = this->m_deferringAST;
  3291. if (m_doingFastScan)
  3292. {
  3293. this->m_deferringAST = false;
  3294. }
  3295. #endif
  3296. pnode = CreateNodeForOpT<knopRegExp>();
  3297. pnode->regexPattern = m_token.GetRegex();
  3298. #if ENABLE_BACKGROUND_PARSING
  3299. if (m_doingFastScan)
  3300. {
  3301. this->m_deferringAST = saveDeferringAST;
  3302. this->AddFastScannedRegExpNode(pnode);
  3303. if (!buildAST)
  3304. {
  3305. pnode = nullptr;
  3306. }
  3307. }
  3308. else if (this->IsBackgroundParser())
  3309. {
  3310. Assert(pnode->regexPattern == nullptr);
  3311. this->AddBackgroundRegExpNode(pnode);
  3312. }
  3313. #endif
  3314. }
  3315. else
  3316. {
  3317. this->GetScanner()->RescanRegExpNoAST();
  3318. }
  3319. Assert(m_token.tk == tkRegExp);
  3320. return pnode;
  3321. }
  3322. BOOL Parser::NodeIsEvalName(ParseNodePtr pnode)
  3323. {
  3324. //WOOB 1107758 Special case of indirect eval binds to local scope in standards mode
  3325. return pnode->nop == knopName && (pnode->AsParseNodeName()->pid == wellKnownPropertyPids.eval);
  3326. }
  3327. BOOL Parser::NodeIsSuperName(ParseNodePtr pnode)
  3328. {
  3329. return pnode->nop == knopName && (pnode->AsParseNodeName()->pid == wellKnownPropertyPids._superConstructor);
  3330. }
  3331. BOOL Parser::NodeEqualsName(ParseNodePtr pnode, LPCOLESTR sz, uint32 cch)
  3332. {
  3333. return pnode->nop == knopName &&
  3334. pnode->AsParseNodeName()->pid->Cch() == cch &&
  3335. !wmemcmp(pnode->AsParseNodeName()->pid->Psz(), sz, cch);
  3336. }
  3337. BOOL Parser::NodeIsIdent(ParseNodePtr pnode, IdentPtr pid)
  3338. {
  3339. for (;;)
  3340. {
  3341. switch (pnode->nop)
  3342. {
  3343. case knopName:
  3344. return (pnode->AsParseNodeName()->pid == pid);
  3345. case knopComma:
  3346. pnode = pnode->AsParseNodeBin()->pnode2;
  3347. break;
  3348. default:
  3349. return FALSE;
  3350. }
  3351. }
  3352. }
  3353. template<bool buildAST>
  3354. ParseNodePtr Parser::ParsePostfixOperators(
  3355. ParseNodePtr pnode,
  3356. BOOL fAllowCall,
  3357. BOOL fInNew,
  3358. BOOL isAsyncExpr,
  3359. BOOL fCanAssignToCallResult,
  3360. BOOL *pfCanAssign,
  3361. _Inout_ IdentToken* pToken,
  3362. _Out_opt_ bool* pfIsDotOrIndex /*= nullptr */)
  3363. {
  3364. uint16 count = 0;
  3365. bool callOfConstants = false;
  3366. if (pfIsDotOrIndex)
  3367. {
  3368. *pfIsDotOrIndex = false;
  3369. }
  3370. for (;;)
  3371. {
  3372. uint16 spreadArgCount = 0;
  3373. switch (m_token.tk)
  3374. {
  3375. case tkLParen:
  3376. {
  3377. AutoMarkInParsingArgs autoMarkInParsingArgs(this);
  3378. if (fInNew)
  3379. {
  3380. ParseNodePtr pnodeArgs = ParseArgList<buildAST>(&callOfConstants, &spreadArgCount, &count);
  3381. if (buildAST)
  3382. {
  3383. Assert(pnode->nop == knopNew);
  3384. Assert(pnode->AsParseNodeCall()->pnodeArgs == nullptr);
  3385. pnode->AsParseNodeCall()->pnodeArgs = pnodeArgs;
  3386. pnode->AsParseNodeCall()->callOfConstants = callOfConstants;
  3387. pnode->AsParseNodeCall()->isApplyCall = false;
  3388. pnode->AsParseNodeCall()->isEvalCall = false;
  3389. pnode->AsParseNodeCall()->isSuperCall = false;
  3390. pnode->AsParseNodeCall()->hasDestructuring = m_hasDestructuringPattern;
  3391. Assert(!m_hasDestructuringPattern || count > 0);
  3392. pnode->AsParseNodeCall()->argCount = count;
  3393. pnode->AsParseNodeCall()->spreadArgCount = spreadArgCount;
  3394. pnode->ichLim = this->GetScanner()->IchLimTok();
  3395. }
  3396. else
  3397. {
  3398. pnode = nullptr;
  3399. pToken->tk = tkNone; // This is no longer an identifier
  3400. }
  3401. fInNew = FALSE;
  3402. ChkCurTok(tkRParen, ERRnoRparen);
  3403. }
  3404. else
  3405. {
  3406. if (!fAllowCall)
  3407. {
  3408. return pnode;
  3409. }
  3410. uint saveNextBlockId = m_nextBlockId;
  3411. uint saveCurrBlockId = GetCurrentBlock()->blockId;
  3412. if (isAsyncExpr)
  3413. {
  3414. // Advance the block ID here in case this parenthetical expression turns out to be a lambda parameter list.
  3415. // That way the pid ref stacks will be created in their correct final form, and we can simply fix
  3416. // up function ID's.
  3417. GetCurrentBlock()->blockId = m_nextBlockId++;
  3418. }
  3419. ParseNodePtr pnodeArgs = ParseArgList<buildAST>(&callOfConstants, &spreadArgCount, &count);
  3420. // We used to un-defer a deferred function body here if it was called as part of the expression that declared it.
  3421. // We now detect this case up front in ParseFncDecl, which is cheaper and simpler.
  3422. if (buildAST)
  3423. {
  3424. bool fCallIsEval = false;
  3425. // Detect super()
  3426. if (this->NodeIsSuperName(pnode))
  3427. {
  3428. pnode = CreateSuperCallNode(pnode->AsParseNodeSpecialName(), pnodeArgs);
  3429. Assert(pnode);
  3430. pnode->AsParseNodeSuperCall()->pnodeThis = ReferenceSpecialName(wellKnownPropertyPids._this, pnode->ichMin, this->GetScanner()->IchLimTok(), true);
  3431. pnode->AsParseNodeSuperCall()->pnodeNewTarget = ReferenceSpecialName(wellKnownPropertyPids._newTarget, pnode->ichMin, this->GetScanner()->IchLimTok(), true);
  3432. }
  3433. else
  3434. {
  3435. pnode = CreateCallNode(knopCall, pnode, pnodeArgs);
  3436. Assert(pnode);
  3437. }
  3438. // Detect call to "eval" and record it on the function.
  3439. // Note: we used to leave it up to the byte code generator to detect eval calls
  3440. // at global scope, but now it relies on the flag the parser sets, so set it here.
  3441. if (count > 0 && this->NodeIsEvalName(pnode->AsParseNodeCall()->pnodeTarget))
  3442. {
  3443. this->MarkEvalCaller();
  3444. fCallIsEval = true;
  3445. // Eval may reference any of the special symbols so we need to push refs to them here.
  3446. ReferenceSpecialName(wellKnownPropertyPids._this);
  3447. ReferenceSpecialName(wellKnownPropertyPids._newTarget);
  3448. ReferenceSpecialName(wellKnownPropertyPids._super);
  3449. ReferenceSpecialName(wellKnownPropertyPids._superConstructor);
  3450. ReferenceSpecialName(wellKnownPropertyPids.arguments);
  3451. }
  3452. pnode->AsParseNodeCall()->callOfConstants = callOfConstants;
  3453. pnode->AsParseNodeCall()->spreadArgCount = spreadArgCount;
  3454. pnode->AsParseNodeCall()->isApplyCall = false;
  3455. pnode->AsParseNodeCall()->isEvalCall = fCallIsEval;
  3456. pnode->AsParseNodeCall()->hasDestructuring = m_hasDestructuringPattern;
  3457. Assert(!m_hasDestructuringPattern || count > 0);
  3458. pnode->AsParseNodeCall()->argCount = count;
  3459. pnode->ichLim = this->GetScanner()->IchLimTok();
  3460. }
  3461. else
  3462. {
  3463. pnode = nullptr;
  3464. if (pToken->tk == tkID && pToken->pid == wellKnownPropertyPids.eval && count > 0) // Detect eval
  3465. {
  3466. this->MarkEvalCaller();
  3467. ReferenceSpecialName(wellKnownPropertyPids._this);
  3468. ReferenceSpecialName(wellKnownPropertyPids._newTarget);
  3469. ReferenceSpecialName(wellKnownPropertyPids._super);
  3470. ReferenceSpecialName(wellKnownPropertyPids._superConstructor);
  3471. ReferenceSpecialName(wellKnownPropertyPids.arguments);
  3472. }
  3473. pToken->tk = tkNone; // This is no longer an identifier
  3474. }
  3475. ChkCurTok(tkRParen, ERRnoRparen);
  3476. if (isAsyncExpr)
  3477. {
  3478. GetCurrentBlock()->blockId = saveCurrBlockId;
  3479. if (m_token.tk == tkDArrow)
  3480. {
  3481. // We're going to rewind and reinterpret the expression as a parameter list.
  3482. // Put back the original next-block-ID so the existing pid ref stacks will be correct.
  3483. m_nextBlockId = saveNextBlockId;
  3484. }
  3485. }
  3486. }
  3487. if (pfCanAssign)
  3488. {
  3489. *pfCanAssign = fCanAssignToCallResult &&
  3490. (m_sourceContextInfo ?
  3491. !PHASE_ON_RAW(Js::EarlyErrorOnAssignToCallPhase, m_sourceContextInfo->sourceContextId, GetCurrentFunctionNode()->functionId) :
  3492. !PHASE_ON1(Js::EarlyErrorOnAssignToCallPhase));
  3493. }
  3494. if (pfIsDotOrIndex)
  3495. {
  3496. *pfIsDotOrIndex = false;
  3497. }
  3498. break;
  3499. }
  3500. case tkLBrack:
  3501. {
  3502. this->GetScanner()->Scan();
  3503. IdentToken tok;
  3504. ParseNodePtr pnodeExpr = ParseExpr<buildAST>(0, FALSE, TRUE, FALSE, nullptr, nullptr, nullptr, &tok);
  3505. if (buildAST)
  3506. {
  3507. AnalysisAssert(pnodeExpr);
  3508. if (pnode && pnode->nop == knopName && pnode->AsParseNodeName()->IsSpecialName() && pnode->AsParseNodeSpecialName()->isSuper)
  3509. {
  3510. pnode = CreateSuperReferenceNode(knopIndex, pnode->AsParseNodeSpecialName(), pnodeExpr);
  3511. pnode->AsParseNodeSuperReference()->pnodeThis = ReferenceSpecialName(wellKnownPropertyPids._this, pnode->ichMin, pnode->ichLim, true);
  3512. }
  3513. else
  3514. {
  3515. pnode = CreateBinNode(knopIndex, pnode, pnodeExpr);
  3516. }
  3517. AnalysisAssert(pnode);
  3518. pnode->ichLim = this->GetScanner()->IchLimTok();
  3519. }
  3520. else
  3521. {
  3522. pnode = nullptr;
  3523. pToken->tk = tkNone; // This is no longer an identifier
  3524. }
  3525. ChkCurTok(tkRBrack, ERRnoRbrack);
  3526. if (pfCanAssign)
  3527. {
  3528. *pfCanAssign = TRUE;
  3529. }
  3530. if (pfIsDotOrIndex)
  3531. {
  3532. *pfIsDotOrIndex = true;
  3533. }
  3534. PidRefStack * topPidRef = nullptr;
  3535. if (buildAST)
  3536. {
  3537. if (pnodeExpr && pnodeExpr->nop == knopName)
  3538. {
  3539. topPidRef = pnodeExpr->AsParseNodeName()->pid->GetTopRef();
  3540. }
  3541. }
  3542. else if (tok.tk == tkID)
  3543. {
  3544. topPidRef = tok.pid->GetTopRef();
  3545. }
  3546. if (topPidRef)
  3547. {
  3548. topPidRef->SetIsUsedInLdElem(true);
  3549. }
  3550. if (!buildAST)
  3551. {
  3552. break;
  3553. }
  3554. bool shouldConvertToDot = false;
  3555. if (pnode->AsParseNodeBin()->pnode2->nop == knopStr)
  3556. {
  3557. // if the string is empty or contains escape character, we will not convert them to dot node
  3558. shouldConvertToDot = pnode->AsParseNodeBin()->pnode2->AsParseNodeStr()->pid->Cch() > 0 && !this->GetScanner()->IsEscapeOnLastTkStrCon();
  3559. }
  3560. if (shouldConvertToDot)
  3561. {
  3562. LPCOLESTR str = pnode->AsParseNodeBin()->pnode2->AsParseNodeStr()->pid->Psz();
  3563. // See if we can convert o["p"] into o.p and o["0"] into o[0] since they're equivalent and the latter forms
  3564. // are faster
  3565. uint32 uintValue;
  3566. if (Js::JavascriptOperators::TryConvertToUInt32(
  3567. str,
  3568. pnode->AsParseNodeBin()->pnode2->AsParseNodeStr()->pid->Cch(),
  3569. &uintValue) &&
  3570. !Js::TaggedInt::IsOverflow(uintValue)) // the optimization is not very useful if the number can't be represented as a TaggedInt
  3571. {
  3572. // No need to verify that uintValue != JavascriptArray::InvalidIndex since all nonnegative TaggedInts are valid indexes
  3573. auto intNode = CreateIntNode(uintValue); // implicit conversion from uint32 to int32
  3574. pnode->AsParseNodeBin()->pnode2 = intNode;
  3575. }
  3576. // Field optimization (see GlobOpt::KillLiveElems) checks for value being a Number,
  3577. // and since NaN/Infinity is a number it won't kill o.NaN/o.Infinity which would cause a problem
  3578. // if we decide to hoist o.NaN/o.Infinity.
  3579. // We need to keep o["NaN"] and o["+/-Infinity"] as array element access (we don't hoist that but we may hoist field access),
  3580. // so no matter if it's killed by o[x] inside a loop, we make sure that we never hoist these.
  3581. // We need to follow same logic for strings that convert to a floating point number.
  3582. else
  3583. {
  3584. bool doConvertToProperty = false; // Convert a["x"] -> a.x.
  3585. if (!Parser::IsNaNOrInfinityLiteral<true>(str))
  3586. {
  3587. const OLECHAR* terminalChar;
  3588. double dbl = Js::NumberUtilities::StrToDbl(str, &terminalChar, m_scriptContext);
  3589. bool convertsToFloat = !Js::NumberUtilities::IsNan(dbl);
  3590. doConvertToProperty = !convertsToFloat;
  3591. }
  3592. if (doConvertToProperty)
  3593. {
  3594. ParseNodeName * pnodeNewExpr = CreateNameNode(pnodeExpr->AsParseNodeStr()->pid);
  3595. pnodeNewExpr->ichMin = pnodeExpr->ichMin;
  3596. pnodeNewExpr->ichLim = pnodeExpr->ichLim;
  3597. pnode->AsParseNodeBin()->pnode2 = pnodeNewExpr;
  3598. pnode->nop = knopDot;
  3599. pnode->grfpn |= PNodeFlags::fpnIndexOperator;
  3600. }
  3601. }
  3602. }
  3603. }
  3604. break;
  3605. case tkDot:
  3606. {
  3607. ParseNodePtr name = nullptr;
  3608. OpCode opCode = knopDot;
  3609. this->GetScanner()->Scan();
  3610. if (!m_token.IsIdentifier())
  3611. {
  3612. //allow reserved words in ES5 mode
  3613. if (!(m_token.IsReservedWord()))
  3614. {
  3615. IdentifierExpectedError(m_token);
  3616. }
  3617. }
  3618. // Note: see comment above about field optimization WRT NaN/Infinity/-Infinity.
  3619. // Convert a.Nan, a.Infinity into a["NaN"], a["Infinity"].
  3620. // We don't care about -Infinity case here because x.-Infinity is invalid in JavaScript.
  3621. // Both NaN and Infinity are identifiers.
  3622. else if (buildAST && Parser::IsNaNOrInfinityLiteral<false>(m_token.GetIdentifier(this->GetHashTbl())->Psz()))
  3623. {
  3624. opCode = knopIndex;
  3625. }
  3626. if (buildAST)
  3627. {
  3628. if (opCode == knopDot)
  3629. {
  3630. name = CreateNameNode(m_token.GetIdentifier(this->GetHashTbl()));
  3631. }
  3632. else
  3633. {
  3634. Assert(opCode == knopIndex);
  3635. name = CreateStrNode(m_token.GetIdentifier(this->GetHashTbl()));
  3636. }
  3637. if (pnode && pnode->nop == knopName && pnode->AsParseNodeName()->IsSpecialName() && pnode->AsParseNodeSpecialName()->isSuper)
  3638. {
  3639. pnode = CreateSuperReferenceNode(opCode, pnode->AsParseNodeSpecialName(), name);
  3640. pnode->AsParseNodeSuperReference()->pnodeThis = ReferenceSpecialName(wellKnownPropertyPids._this, pnode->ichMin, pnode->ichLim, true);
  3641. }
  3642. else
  3643. {
  3644. pnode = CreateBinNode(opCode, pnode, name);
  3645. }
  3646. }
  3647. else
  3648. {
  3649. pnode = nullptr;
  3650. pToken->tk = tkNone;
  3651. }
  3652. if (pfCanAssign)
  3653. {
  3654. *pfCanAssign = TRUE;
  3655. }
  3656. if (pfIsDotOrIndex)
  3657. {
  3658. *pfIsDotOrIndex = true;
  3659. }
  3660. this->GetScanner()->Scan();
  3661. break;
  3662. }
  3663. case tkStrTmplBasic:
  3664. case tkStrTmplBegin:
  3665. {
  3666. ParseNode* templateNode = nullptr;
  3667. if (pnode != nullptr)
  3668. {
  3669. AutoMarkInParsingArgs autoMarkInParsingArgs(this);
  3670. templateNode = ParseStringTemplateDecl<buildAST>(pnode);
  3671. }
  3672. else
  3673. {
  3674. templateNode = ParseStringTemplateDecl<buildAST>(pnode);
  3675. }
  3676. if (!buildAST)
  3677. {
  3678. pToken->tk = tkNone; // This is no longer an identifier
  3679. }
  3680. pnode = templateNode;
  3681. if (pfCanAssign)
  3682. {
  3683. *pfCanAssign = FALSE;
  3684. }
  3685. if (pfIsDotOrIndex)
  3686. {
  3687. *pfIsDotOrIndex = false;
  3688. }
  3689. break;
  3690. }
  3691. default:
  3692. return pnode;
  3693. }
  3694. }
  3695. }
  3696. /***************************************************************************
  3697. Look for an existing label with the given name.
  3698. ***************************************************************************/
  3699. bool Parser::LabelExists(IdentPtr pid, LabelId* pLabelIdList)
  3700. {
  3701. StmtNest dummy;
  3702. dummy.pLabelId = pLabelIdList;
  3703. dummy.pstmtOuter = m_pstmtCur;
  3704. // Look through each label list for the current stack of statements
  3705. for (StmtNest* pStmt = &dummy; pStmt != nullptr; pStmt = pStmt->pstmtOuter)
  3706. {
  3707. for (LabelId* pLabelId = pStmt->pLabelId; pLabelId != nullptr; pLabelId = pLabelId->next)
  3708. {
  3709. if (pLabelId->pid == pid)
  3710. return true;
  3711. }
  3712. }
  3713. return false;
  3714. }
  3715. // Currently only ints and floats are treated as constants in function call
  3716. // TODO: Check if we need for other constants as well
  3717. BOOL Parser::IsConstantInFunctionCall(ParseNodePtr pnode)
  3718. {
  3719. if (pnode->nop == knopInt && !Js::TaggedInt::IsOverflow(pnode->AsParseNodeInt()->lw))
  3720. {
  3721. return TRUE;
  3722. }
  3723. if (pnode->nop == knopFlt)
  3724. {
  3725. return TRUE;
  3726. }
  3727. return FALSE;
  3728. }
  3729. /***************************************************************************
  3730. Parse a list of arguments.
  3731. ***************************************************************************/
  3732. template<bool buildAST>
  3733. ParseNodePtr Parser::ParseArgList(bool *pCallOfConstants, uint16 *pSpreadArgCount, uint16 * pCount)
  3734. {
  3735. ParseNodePtr pnodeArg;
  3736. ParseNodePtr pnodeList = nullptr;
  3737. ParseNodePtr *lastNodeRef = nullptr;
  3738. // Check for an empty list
  3739. Assert(m_token.tk == tkLParen);
  3740. if (this->GetScanner()->Scan() == tkRParen)
  3741. {
  3742. return nullptr;
  3743. }
  3744. *pCallOfConstants = true;
  3745. *pSpreadArgCount = 0;
  3746. int count = 0;
  3747. while (true)
  3748. {
  3749. if (count >= Js::Constants::MaxAllowedArgs)
  3750. {
  3751. Error(ERRTooManyArgs);
  3752. }
  3753. // Allow spread in argument lists.
  3754. IdentToken token;
  3755. pnodeArg = ParseExpr<buildAST>(koplCma, nullptr, TRUE, /* fAllowEllipsis */TRUE, NULL, nullptr, nullptr, &token);
  3756. ++count;
  3757. this->MarkEscapingRef(pnodeArg, &token);
  3758. if (buildAST)
  3759. {
  3760. this->CheckArguments(pnodeArg);
  3761. if (*pCallOfConstants && !IsConstantInFunctionCall(pnodeArg))
  3762. {
  3763. *pCallOfConstants = false;
  3764. }
  3765. if (pnodeArg->nop == knopEllipsis)
  3766. {
  3767. (*pSpreadArgCount)++;
  3768. }
  3769. AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeArg);
  3770. }
  3771. if (m_token.tk != tkComma)
  3772. {
  3773. break;
  3774. }
  3775. this->GetScanner()->Scan();
  3776. if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
  3777. {
  3778. break;
  3779. }
  3780. }
  3781. if (pSpreadArgCount != nullptr && (*pSpreadArgCount) > 0) {
  3782. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, SpreadFeature, m_scriptContext);
  3783. }
  3784. *pCount = static_cast<uint16>(count);
  3785. if (buildAST)
  3786. {
  3787. Assert(lastNodeRef);
  3788. Assert(*lastNodeRef);
  3789. pnodeList->ichLim = (*lastNodeRef)->ichLim;
  3790. }
  3791. return pnodeList;
  3792. }
  3793. // Currently only ints are treated as constants in ArrayLiterals
  3794. BOOL Parser::IsConstantInArrayLiteral(ParseNodePtr pnode)
  3795. {
  3796. if (pnode->nop == knopInt && !Js::TaggedInt::IsOverflow(pnode->AsParseNodeInt()->lw))
  3797. {
  3798. return TRUE;
  3799. }
  3800. return FALSE;
  3801. }
  3802. template<bool buildAST>
  3803. ParseNodeArrLit * Parser::ParseArrayLiteral()
  3804. {
  3805. ParseNodeArrLit * pnode = nullptr;
  3806. bool arrayOfTaggedInts = false;
  3807. bool arrayOfInts = false;
  3808. bool arrayOfNumbers = false;
  3809. bool hasMissingValues = false;
  3810. uint count = 0;
  3811. uint spreadCount = 0;
  3812. ParseNodePtr pnode1 = ParseArrayList<buildAST>(&arrayOfTaggedInts, &arrayOfInts, &arrayOfNumbers, &hasMissingValues, &count, &spreadCount);
  3813. if (buildAST)
  3814. {
  3815. pnode = CreateNodeForOpT<knopArray>();
  3816. pnode->pnode1 = pnode1;
  3817. pnode->arrayOfTaggedInts = arrayOfTaggedInts;
  3818. pnode->arrayOfInts = arrayOfInts;
  3819. pnode->arrayOfNumbers = arrayOfNumbers;
  3820. pnode->hasMissingValues = hasMissingValues;
  3821. pnode->count = count;
  3822. pnode->spreadCount = spreadCount;
  3823. if (pnode->pnode1)
  3824. {
  3825. this->CheckArguments(pnode->pnode1);
  3826. }
  3827. }
  3828. return pnode;
  3829. }
  3830. /***************************************************************************
  3831. Create an ArrayLiteral node
  3832. Parse a list of array elements. [ a, b, , c, ]
  3833. ***************************************************************************/
  3834. template<bool buildAST>
  3835. ParseNodePtr Parser::ParseArrayList(bool *pArrayOfTaggedInts, bool *pArrayOfInts, bool *pArrayOfNumbers, bool *pHasMissingValues, uint *count, uint *spreadCount)
  3836. {
  3837. ParseNodePtr pnodeArg = nullptr;
  3838. ParseNodePtr pnodeList = nullptr;
  3839. ParseNodePtr *lastNodeRef = nullptr;
  3840. *count = 0;
  3841. // Check for an empty list
  3842. if (tkRBrack == m_token.tk)
  3843. {
  3844. return nullptr;
  3845. }
  3846. this->m_arrayDepth++;
  3847. bool arrayOfTaggedInts = buildAST;
  3848. bool arrayOfInts = buildAST;
  3849. bool arrayOfNumbers = buildAST;
  3850. bool arrayOfVarInts = false;
  3851. bool hasMissingValues = false;
  3852. for (;;)
  3853. {
  3854. (*count)++;
  3855. if (tkComma == m_token.tk || tkRBrack == m_token.tk)
  3856. {
  3857. hasMissingValues = true;
  3858. arrayOfTaggedInts = false;
  3859. arrayOfInts = false;
  3860. arrayOfNumbers = false;
  3861. if (buildAST)
  3862. {
  3863. pnodeArg = CreateNodeForOpT<knopEmpty>();
  3864. }
  3865. }
  3866. else
  3867. {
  3868. // Allow Spread in array literals.
  3869. pnodeArg = ParseExpr<buildAST>(koplCma, nullptr, TRUE, /* fAllowEllipsis */ TRUE);
  3870. if (buildAST)
  3871. {
  3872. if (pnodeArg->nop == knopEllipsis)
  3873. {
  3874. (*spreadCount)++;
  3875. }
  3876. this->CheckArguments(pnodeArg);
  3877. }
  3878. }
  3879. #if DEBUG
  3880. if (m_grfscr & fscrEnforceJSON && !IsJSONValid(pnodeArg))
  3881. {
  3882. Error(ERRsyntax);
  3883. }
  3884. #endif
  3885. if (buildAST)
  3886. {
  3887. if (arrayOfNumbers)
  3888. {
  3889. if (pnodeArg->nop != knopInt)
  3890. {
  3891. arrayOfTaggedInts = false;
  3892. if (pnodeArg->nop != knopFlt)
  3893. {
  3894. // Not an array of constants.
  3895. arrayOfInts = false;
  3896. arrayOfNumbers = false;
  3897. }
  3898. else if (arrayOfInts && Js::JavascriptNumber::IsInt32OrUInt32(pnodeArg->AsParseNodeFloat()->dbl) && (!Js::JavascriptNumber::IsInt32(pnodeArg->AsParseNodeFloat()->dbl) || pnodeArg->AsParseNodeFloat()->dbl == -2147483648.0))
  3899. {
  3900. // We've seen nothing but ints, and this is a uint32 but not an int32.
  3901. // Unless we see an actual float at some point, we want an array of vars
  3902. // so we can work with tagged ints.
  3903. arrayOfVarInts = true;
  3904. }
  3905. else
  3906. {
  3907. // Not an int array, but it may still be a float array.
  3908. arrayOfInts = false;
  3909. }
  3910. }
  3911. else
  3912. {
  3913. if (Js::SparseArraySegment<int32>::IsMissingItem((int32*)&pnodeArg->AsParseNodeInt()->lw))
  3914. {
  3915. arrayOfInts = false;
  3916. }
  3917. if (Js::TaggedInt::IsOverflow(pnodeArg->AsParseNodeInt()->lw))
  3918. {
  3919. arrayOfTaggedInts = false;
  3920. }
  3921. }
  3922. }
  3923. AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeArg);
  3924. }
  3925. if (tkComma != m_token.tk)
  3926. {
  3927. break;
  3928. }
  3929. this->GetScanner()->Scan();
  3930. if (tkRBrack == m_token.tk)
  3931. {
  3932. break;
  3933. }
  3934. }
  3935. if (spreadCount != nullptr && *spreadCount > 0) {
  3936. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, SpreadFeature, m_scriptContext);
  3937. }
  3938. if (buildAST)
  3939. {
  3940. Assert(lastNodeRef);
  3941. Assert(*lastNodeRef);
  3942. pnodeList->ichLim = (*lastNodeRef)->ichLim;
  3943. if (arrayOfVarInts && arrayOfInts)
  3944. {
  3945. arrayOfInts = false;
  3946. arrayOfNumbers = false;
  3947. }
  3948. *pArrayOfTaggedInts = arrayOfTaggedInts;
  3949. *pArrayOfInts = arrayOfInts;
  3950. *pArrayOfNumbers = arrayOfNumbers;
  3951. *pHasMissingValues = hasMissingValues;
  3952. }
  3953. this->m_arrayDepth--;
  3954. return pnodeList;
  3955. }
  3956. Parser::MemberNameToTypeMap* Parser::CreateMemberNameMap(ArenaAllocator* pAllocator)
  3957. {
  3958. Assert(pAllocator);
  3959. return Anew(pAllocator, MemberNameToTypeMap, pAllocator, 5);
  3960. }
  3961. template<bool buildAST> void Parser::ParseComputedName(ParseNodePtr* ppnodeName, LPCOLESTR* ppNameHint, LPCOLESTR* ppFullNameHint, uint32 *pNameLength, uint32 *pShortNameOffset)
  3962. {
  3963. this->GetScanner()->Scan();
  3964. ParseNodePtr pnodeNameExpr = ParseExpr<buildAST>(koplCma, nullptr, TRUE, FALSE, *ppNameHint, pNameLength, pShortNameOffset);
  3965. if (buildAST)
  3966. {
  3967. *ppnodeName = CreateUniNode(knopComputedName, pnodeNameExpr, pnodeNameExpr->ichMin, pnodeNameExpr->ichLim);
  3968. }
  3969. if (ppFullNameHint && buildAST && CONFIG_FLAG(UseFullName))
  3970. {
  3971. *ppFullNameHint = FormatPropertyString(*ppNameHint, pnodeNameExpr, pNameLength, pShortNameOffset);
  3972. }
  3973. ChkCurTokNoScan(tkRBrack, ERRnoRbrack);
  3974. }
  3975. /***************************************************************************
  3976. Parse a list of object set/get members, e.g.:
  3977. { get foo(){ ... }, set bar(arg) { ... } }
  3978. ***************************************************************************/
  3979. template<bool buildAST>
  3980. ParseNodeBin * Parser::ParseMemberGetSet(OpCode nop, LPCOLESTR* ppNameHint, size_t iecpMin, charcount_t ichMin)
  3981. {
  3982. ParseNodePtr pnodeName = nullptr;
  3983. Assert(nop == knopGetMember || nop == knopSetMember);
  3984. Assert(ppNameHint);
  3985. IdentPtr pid = nullptr;
  3986. bool isComputedName = false;
  3987. *ppNameHint = nullptr;
  3988. switch (m_token.tk)
  3989. {
  3990. default:
  3991. if (!m_token.IsReservedWord())
  3992. {
  3993. Error(ERRnoMemberIdent);
  3994. }
  3995. // fall through
  3996. case tkID:
  3997. pid = m_token.GetIdentifier(this->GetHashTbl());
  3998. *ppNameHint = pid->Psz();
  3999. if (buildAST)
  4000. {
  4001. pnodeName = CreateStrNode(pid);
  4002. }
  4003. break;
  4004. case tkStrCon:
  4005. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  4006. {
  4007. Error(ERRES5NoOctal);
  4008. }
  4009. pid = m_token.GetStr();
  4010. *ppNameHint = pid->Psz();
  4011. if (buildAST)
  4012. {
  4013. pnodeName = CreateStrNode(pid);
  4014. }
  4015. break;
  4016. case tkIntCon:
  4017. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  4018. {
  4019. Error(ERRES5NoOctal);
  4020. }
  4021. pid = this->GetScanner()->PidFromLong(m_token.GetLong());
  4022. if (buildAST)
  4023. {
  4024. pnodeName = CreateStrNode(pid);
  4025. }
  4026. break;
  4027. case tkFltCon:
  4028. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  4029. {
  4030. Error(ERRES5NoOctal);
  4031. }
  4032. pid = this->GetScanner()->PidFromDbl(m_token.GetDouble());
  4033. if (buildAST)
  4034. {
  4035. pnodeName = CreateStrNode(pid);
  4036. }
  4037. break;
  4038. case tkLBrack:
  4039. // Computed property name: get|set [expr] () { }
  4040. if (!m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
  4041. {
  4042. Error(ERRnoMemberIdent);
  4043. }
  4044. LPCOLESTR emptyHint = nullptr;
  4045. uint32 offset = 0;
  4046. ParseComputedName<buildAST>(&pnodeName, &emptyHint, ppNameHint, &offset);
  4047. isComputedName = true;
  4048. break;
  4049. }
  4050. MemberType memberType;
  4051. ushort flags = fFncMethod | fFncNoName;
  4052. if (nop == knopGetMember)
  4053. {
  4054. memberType = MemberTypeGetter;
  4055. flags |= fFncNoArg;
  4056. }
  4057. else
  4058. {
  4059. Assert(nop == knopSetMember);
  4060. memberType = MemberTypeSetter;
  4061. flags |= fFncOneArg;
  4062. }
  4063. ParseNodeFnc * pnodeFnc = ParseFncDeclNoCheckScope<buildAST>(flags, SuperRestrictionState::PropertyAllowed, *ppNameHint,
  4064. /*needsPIDOnRCurlyScan*/ false);
  4065. pnodeFnc->cbStringMin = iecpMin;
  4066. pnodeFnc->cbStringLim = pnodeFnc->cbLim;
  4067. if (isComputedName)
  4068. {
  4069. pnodeFnc->SetHasComputedName();
  4070. }
  4071. pnodeFnc->SetHasHomeObj();
  4072. pnodeFnc->SetIsAccessor();
  4073. if (buildAST)
  4074. {
  4075. return CreateBinNode(nop, pnodeName, pnodeFnc);
  4076. }
  4077. else
  4078. {
  4079. return nullptr;
  4080. }
  4081. }
  4082. /***************************************************************************
  4083. Parse a list of object members. e.g. { x:foo, 'y me':bar }
  4084. ***************************************************************************/
  4085. template<bool buildAST>
  4086. ParseNodePtr Parser::ParseMemberList(LPCOLESTR pNameHint, uint32* pNameHintLength, tokens declarationType)
  4087. {
  4088. ParseNodeBin * pnodeArg = nullptr;
  4089. ParseNodePtr pnodeEllipsis = nullptr;
  4090. ParseNodePtr pnodeName = nullptr;
  4091. ParseNodePtr pnodeList = nullptr;
  4092. ParseNodePtr *lastNodeRef = nullptr;
  4093. LPCOLESTR pFullNameHint = nullptr; // A calculated full name
  4094. uint32 fullNameHintLength = pNameHintLength ? *pNameHintLength : 0;
  4095. uint32 shortNameOffset = 0;
  4096. bool isProtoDeclared = false;
  4097. bool seenRest = false;
  4098. // we get declaration tkLCurly - when the possible object pattern found under the expression.
  4099. bool isObjectPattern = (declarationType == tkVAR || declarationType == tkLET || declarationType == tkCONST || declarationType == tkLCurly);
  4100. // Check for an empty list
  4101. if (tkRCurly == m_token.tk)
  4102. {
  4103. return nullptr;
  4104. }
  4105. ArenaAllocator tempAllocator(_u("MemberNames"), m_nodeAllocator.GetPageAllocator(), Parser::OutOfMemory);
  4106. bool hasDeferredInitError = false;
  4107. for (;;)
  4108. {
  4109. bool isComputedName = false;
  4110. #if DEBUG
  4111. if ((m_grfscr & fscrEnforceJSON) && (tkStrCon != m_token.tk || !(this->GetScanner()->IsDoubleQuoteOnLastTkStrCon())))
  4112. {
  4113. Error(ERRsyntax);
  4114. }
  4115. #endif
  4116. bool isAsyncMethod = false;
  4117. charcount_t ichMin = this->GetScanner()->IchMinTok();
  4118. size_t iecpMin = this->GetScanner()->IecpMinTok();
  4119. if (CheckContextualKeyword(wellKnownPropertyPids.async) && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  4120. {
  4121. RestorePoint parsedAsync;
  4122. this->GetScanner()->Capture(&parsedAsync);
  4123. ichMin = this->GetScanner()->IchMinTok();
  4124. iecpMin = this->GetScanner()->IecpMinTok();
  4125. this->GetScanner()->ScanForcingPid();
  4126. if (m_token.tk == tkLParen || m_token.tk == tkColon || m_token.tk == tkRCurly || this->GetScanner()->FHadNewLine() || m_token.tk == tkComma)
  4127. {
  4128. this->GetScanner()->SeekTo(parsedAsync);
  4129. }
  4130. else
  4131. {
  4132. isAsyncMethod = true;
  4133. }
  4134. }
  4135. bool isGenerator = m_scriptContext->GetConfig()->IsES6GeneratorsEnabled() &&
  4136. m_token.tk == tkStar;
  4137. ushort fncDeclFlags = fFncNoName | fFncMethod;
  4138. if (isGenerator)
  4139. {
  4140. if (isAsyncMethod && !m_scriptContext->GetConfig()->IsES2018AsyncIterationEnabled())
  4141. {
  4142. Error(ERRExperimental);
  4143. }
  4144. // Include star character in the function extents
  4145. ichMin = this->GetScanner()->IchMinTok();
  4146. iecpMin = this->GetScanner()->IecpMinTok();
  4147. this->GetScanner()->ScanForcingPid();
  4148. fncDeclFlags |= fFncGenerator;
  4149. }
  4150. IdentPtr pidHint = nullptr; // A name scoped to current expression
  4151. Token tkHint = m_token;
  4152. charcount_t idHintIchMin = static_cast<charcount_t>(this->GetScanner()->IecpMinTok());
  4153. charcount_t idHintIchLim = static_cast<charcount_t>(this->GetScanner()->IecpLimTok());
  4154. bool wrapInBrackets = false;
  4155. bool seenEllipsis = false;
  4156. bool maybeKeyword = false;
  4157. switch (m_token.tk)
  4158. {
  4159. default:
  4160. if (!m_token.IsReservedWord())
  4161. {
  4162. Error(ERRnoMemberIdent);
  4163. }
  4164. // allow reserved words
  4165. wrapInBrackets = true;
  4166. // fall-through
  4167. case tkID:
  4168. pidHint = m_token.GetIdentifier(this->GetHashTbl());
  4169. maybeKeyword = !this->GetScanner()->LastIdentifierHasEscape();
  4170. if (buildAST)
  4171. {
  4172. pnodeName = CreateStrNode(pidHint);
  4173. }
  4174. break;
  4175. case tkStrCon:
  4176. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  4177. {
  4178. Error(ERRES5NoOctal);
  4179. }
  4180. wrapInBrackets = true;
  4181. pidHint = m_token.GetStr();
  4182. if (buildAST)
  4183. {
  4184. pnodeName = CreateStrNode(pidHint);
  4185. }
  4186. break;
  4187. case tkIntCon:
  4188. // Object initializers with numeric labels allowed in JS6
  4189. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  4190. {
  4191. Error(ERRES5NoOctal);
  4192. }
  4193. pidHint = this->GetScanner()->PidFromLong(m_token.GetLong());
  4194. if (buildAST)
  4195. {
  4196. pnodeName = CreateStrNode(pidHint);
  4197. }
  4198. break;
  4199. case tkFltCon:
  4200. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  4201. {
  4202. Error(ERRES5NoOctal);
  4203. }
  4204. pidHint = this->GetScanner()->PidFromDbl(m_token.GetDouble());
  4205. if (buildAST)
  4206. {
  4207. pnodeName = CreateStrNode(pidHint);
  4208. }
  4209. wrapInBrackets = true;
  4210. break;
  4211. case tkLBrack:
  4212. // Computed property name: [expr] : value
  4213. if (!m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
  4214. {
  4215. Error(ERRnoMemberIdent);
  4216. }
  4217. ParseComputedName<buildAST>(&pnodeName, &pNameHint, &pFullNameHint, &fullNameHintLength, &shortNameOffset);
  4218. isComputedName = true;
  4219. break;
  4220. case tkEllipsis:
  4221. if (CONFIG_FLAG(ES2018ObjectRestSpread))
  4222. {
  4223. seenEllipsis = true;
  4224. }
  4225. else
  4226. {
  4227. Error(ERRnoMemberIdent);
  4228. }
  4229. break;
  4230. }
  4231. if (pFullNameHint == nullptr)
  4232. {
  4233. if (CONFIG_FLAG(UseFullName))
  4234. {
  4235. pFullNameHint = AppendNameHints(pNameHint, pidHint, &fullNameHintLength, &shortNameOffset, false, wrapInBrackets);
  4236. }
  4237. else
  4238. {
  4239. pFullNameHint = pidHint ? pidHint->Psz() : nullptr;
  4240. fullNameHintLength = pidHint ? pidHint->Cch() : 0;
  4241. shortNameOffset = 0;
  4242. }
  4243. }
  4244. RestorePoint atPid;
  4245. // Only move to next token if spread op was not seen
  4246. if (!seenEllipsis)
  4247. {
  4248. this->GetScanner()->Capture(&atPid);
  4249. this->GetScanner()->ScanForcingPid();
  4250. }
  4251. if (isGenerator && m_token.tk != tkLParen)
  4252. {
  4253. Error(ERRnoLparen);
  4254. }
  4255. if (tkColon == m_token.tk)
  4256. {
  4257. // It is a syntax error if the production of the form __proto__ : <> occurs more than once. From B.3.1 in spec.
  4258. // Note that previous scan is important because only after that we can determine we have a variable.
  4259. if (!isComputedName && pidHint == wellKnownPropertyPids.__proto__)
  4260. {
  4261. if (isProtoDeclared)
  4262. {
  4263. Error(ERRsyntax);
  4264. }
  4265. else
  4266. {
  4267. isProtoDeclared = true;
  4268. }
  4269. }
  4270. this->GetScanner()->Scan();
  4271. ParseNodePtr pnodeExpr = nullptr;
  4272. if (isObjectPattern)
  4273. {
  4274. if (m_token.tk == tkEllipsis)
  4275. {
  4276. Error(ERRUnexpectedEllipsis);
  4277. }
  4278. RestorePoint atExpression;
  4279. if (!buildAST && declarationType == tkLCurly && IsPossiblePatternStart())
  4280. {
  4281. this->GetScanner()->Capture(&atExpression);
  4282. int saveNextBlockId = m_nextBlockId;
  4283. // It is possible that we might encounter the shorthand init error. Lets find that out.
  4284. bool savedDeferredInitError = m_hasDeferredShorthandInitError;
  4285. m_hasDeferredShorthandInitError = false;
  4286. IdentToken token;
  4287. BOOL fLikelyPattern = false;
  4288. // First identify that the current expression is indeed the object/array literal. Otherwise we will just use the ParsrExpr to parse that.
  4289. ParseTerm<buildAST>(/* fAllowCall */ m_token.tk != tkSUPER, nullptr /*pNameHint*/, nullptr /*pHintLength*/, nullptr /*pShortNameOffset*/, &token, false /*fUnaryOrParen*/,
  4290. TRUE, nullptr /*pfCanAssign*/, &fLikelyPattern);
  4291. m_nextBlockId = saveNextBlockId;
  4292. this->GetScanner()->SeekTo(atExpression);
  4293. if (fLikelyPattern)
  4294. {
  4295. pnodeExpr = ParseDestructuredVarDecl<buildAST>(declarationType, declarationType != tkLCurly, &seenRest/* *hasSeenRest*/, false /*topLevel*/, false /*allowEmptyExpression*/, true /*isObjectPattern*/);
  4296. if (m_token.tk != tkComma && m_token.tk != tkRCurly)
  4297. {
  4298. if (m_token.IsOperator())
  4299. {
  4300. Error(ERRDestructNoOper);
  4301. }
  4302. Error(ERRsyntax);
  4303. }
  4304. }
  4305. else
  4306. {
  4307. if (m_hasDeferredShorthandInitError)
  4308. {
  4309. Error(ERRnoColon);
  4310. }
  4311. pnodeExpr = ParseExpr<buildAST>(koplCma, nullptr/*pfCantAssign*/, TRUE/*fAllowIn*/, FALSE/*fAllowEllipsis*/, pFullNameHint, &fullNameHintLength, &shortNameOffset);
  4312. }
  4313. m_hasDeferredShorthandInitError = savedDeferredInitError;
  4314. }
  4315. else
  4316. {
  4317. pnodeExpr = ParseDestructuredVarDecl<buildAST>(declarationType, declarationType != tkLCurly, &seenRest/* *hasSeenRest*/, false /*topLevel*/, false /*allowEmptyExpression*/, true /*isObjectPattern*/);
  4318. if (m_token.tk != tkComma && m_token.tk != tkRCurly)
  4319. {
  4320. if (m_token.IsOperator())
  4321. {
  4322. Error(ERRDestructNoOper);
  4323. }
  4324. Error(ERRsyntax);
  4325. }
  4326. }
  4327. }
  4328. else
  4329. {
  4330. pnodeExpr = ParseExpr<buildAST>(koplCma, nullptr/*pfCantAssign*/, TRUE/*fAllowIn*/, FALSE/*fAllowEllipsis*/, pFullNameHint, &fullNameHintLength, &shortNameOffset);
  4331. ParseNodeFnc* funcNode = nullptr;
  4332. if (pnodeExpr)
  4333. {
  4334. if (pnodeExpr->nop == knopFncDecl)
  4335. {
  4336. funcNode = pnodeExpr->AsParseNodeFnc();
  4337. funcNode->SetHasHomeObj();
  4338. }
  4339. else if (pnodeExpr->nop == knopClassDecl)
  4340. {
  4341. funcNode = pnodeExpr->AsParseNodeClass()->pnodeConstructor;
  4342. }
  4343. if (funcNode && funcNode->pnodeName == nullptr && isComputedName)
  4344. {
  4345. funcNode->SetHasComputedName();
  4346. }
  4347. }
  4348. }
  4349. #if DEBUG
  4350. if ((m_grfscr & fscrEnforceJSON) && !IsJSONValid(pnodeExpr))
  4351. {
  4352. Error(ERRsyntax);
  4353. }
  4354. #endif
  4355. if (buildAST)
  4356. {
  4357. pnodeArg = CreateBinNode(isObjectPattern ? knopObjectPatternMember : knopMember, pnodeName, pnodeExpr);
  4358. if (pnodeArg->pnode1->nop == knopStr)
  4359. {
  4360. pnodeArg->pnode1->AsParseNodeStr()->pid->PromoteAssignmentState();
  4361. }
  4362. }
  4363. }
  4364. else if (m_token.tk == tkLParen && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
  4365. {
  4366. if (isObjectPattern)
  4367. {
  4368. Error(ERRInvalidAssignmentTarget);
  4369. }
  4370. // Shorthand syntax: foo() {} -> foo: function() {}
  4371. // Rewind to the PID and parse a function expression.
  4372. this->GetScanner()->SeekTo(atPid);
  4373. ParseNodeFnc * pnodeFnc = ParseFncDeclNoCheckScope<buildAST>(fncDeclFlags | (isAsyncMethod ? fFncAsync : fFncNoFlgs), SuperRestrictionState::PropertyAllowed, pFullNameHint,
  4374. /*needsPIDOnRCurlyScan*/ false);
  4375. if (isAsyncMethod || isGenerator)
  4376. {
  4377. pnodeFnc->cbStringMin = iecpMin;
  4378. pnodeFnc->cbStringLim = pnodeFnc->cbLim;
  4379. }
  4380. if (isComputedName)
  4381. {
  4382. pnodeFnc->SetHasComputedName();
  4383. pnodeFnc->cbStringMin = iecpMin;
  4384. pnodeFnc->cbStringLim = pnodeFnc->cbLim;
  4385. }
  4386. pnodeFnc->SetHasHomeObj();
  4387. if (buildAST)
  4388. {
  4389. pnodeArg = CreateBinNode(knopMember, pnodeName, pnodeFnc);
  4390. }
  4391. }
  4392. else if (seenEllipsis)
  4393. {
  4394. if (!isObjectPattern)
  4395. {
  4396. pnodeEllipsis = ParseExpr<buildAST>(koplCma, nullptr, TRUE, /* fAllowEllipsis */ TRUE);
  4397. }
  4398. else
  4399. {
  4400. pnodeEllipsis = ParseDestructuredVarDecl<buildAST>(declarationType, declarationType != tkLCurly, &seenRest/* *hasSeenRest*/, false /*topLevel*/, false /*allowEmptyExpression*/, true /*isObjectPattern*/);
  4401. }
  4402. if (buildAST)
  4403. {
  4404. this->CheckArguments(pnodeEllipsis);
  4405. }
  4406. }
  4407. else if (nullptr != pidHint) //It's either tkID/tkStrCon/tkFloatCon/tkIntCon
  4408. {
  4409. Assert(pidHint->Psz() != nullptr);
  4410. // get/set are only pseudo keywords when they are identifiers (i.e. not strings)
  4411. if ((pidHint == wellKnownPropertyPids.get || pidHint == wellKnownPropertyPids.set) &&
  4412. maybeKeyword &&
  4413. NextTokenIsPropertyNameStart())
  4414. {
  4415. if (isObjectPattern)
  4416. {
  4417. Error(ERRInvalidAssignmentTarget);
  4418. }
  4419. LPCOLESTR pNameGetOrSet = nullptr;
  4420. OpCode op = pidHint == wellKnownPropertyPids.get ? knopGetMember : knopSetMember;
  4421. pnodeArg = ParseMemberGetSet<buildAST>(op, &pNameGetOrSet, iecpMin, ichMin);
  4422. if (CONFIG_FLAG(UseFullName) && buildAST && pnodeArg->pnode2->nop == knopFncDecl)
  4423. {
  4424. // displays as "get object.funcname" or "set object.funcname"
  4425. uint32 getOrSetOffset = 0;
  4426. LPCOLESTR intermediateHint = AppendNameHints(pNameHint, pNameGetOrSet, &fullNameHintLength, &shortNameOffset);
  4427. pFullNameHint = AppendNameHints(pidHint, intermediateHint, &fullNameHintLength, &getOrSetOffset, true);
  4428. shortNameOffset += getOrSetOffset;
  4429. }
  4430. }
  4431. else if ((m_token.tk == tkRCurly || m_token.tk == tkComma || m_token.tk == tkAsg) && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
  4432. {
  4433. // Shorthand {foo} -> {foo:foo} syntax.
  4434. // {foo = <initializer>} supported only when on object pattern rules are being applied
  4435. if (tkHint.tk != tkID)
  4436. {
  4437. Assert(tkHint.IsReservedWord()
  4438. || tkHint.tk == tkIntCon || tkHint.tk == tkFltCon || tkHint.tk == tkStrCon);
  4439. // All keywords are banned in non-strict mode.
  4440. // Future reserved words are banned in strict mode.
  4441. if (IsStrictMode() || !tkHint.IsFutureReservedWord(true))
  4442. {
  4443. IdentifierExpectedError(tkHint);
  4444. }
  4445. }
  4446. CheckArgumentsUse(pidHint, GetCurrentFunctionNode());
  4447. bool couldBeObjectPattern = !isObjectPattern && m_token.tk == tkAsg;
  4448. // Saving the current state as we may change the isObjectPattern down below.
  4449. bool oldState = isObjectPattern;
  4450. if (couldBeObjectPattern)
  4451. {
  4452. declarationType = tkLCurly;
  4453. isObjectPattern = true;
  4454. // This may be an error but we are deferring for favouring destructuring.
  4455. hasDeferredInitError = true;
  4456. }
  4457. ParseNodePtr pnodeIdent = nullptr;
  4458. if (isObjectPattern)
  4459. {
  4460. this->GetScanner()->SeekTo(atPid);
  4461. pnodeIdent = ParseDestructuredVarDecl<buildAST>(declarationType, declarationType != tkLCurly, &seenRest/* *hasSeenRest*/, false /*topLevel*/, false /*allowEmptyExpression*/, true /*isObjectPattern*/);
  4462. if (m_token.tk != tkComma && m_token.tk != tkRCurly)
  4463. {
  4464. if (m_token.IsOperator())
  4465. {
  4466. Error(ERRDestructNoOper);
  4467. }
  4468. Error(ERRsyntax);
  4469. }
  4470. }
  4471. else
  4472. {
  4473. // Add a reference to the hinted name so we can bind it properly.
  4474. PidRefStack *ref = PushPidRef(pidHint);
  4475. if (buildAST)
  4476. {
  4477. pnodeIdent = CreateNameNode(pidHint, ref, idHintIchMin, idHintIchLim);
  4478. }
  4479. }
  4480. if (buildAST)
  4481. {
  4482. pnodeArg = CreateBinNode(isObjectPattern && !couldBeObjectPattern ? knopObjectPatternMember : knopMemberShort, pnodeName, pnodeIdent);
  4483. }
  4484. isObjectPattern = oldState;
  4485. }
  4486. else
  4487. {
  4488. Error(ERRnoColon);
  4489. }
  4490. }
  4491. else
  4492. {
  4493. Error(ERRnoColon);
  4494. }
  4495. if (buildAST)
  4496. {
  4497. if (seenEllipsis)
  4498. {
  4499. Assert(pnodeEllipsis != nullptr);
  4500. AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeEllipsis);
  4501. }
  4502. else
  4503. {
  4504. Assert(pnodeArg->pnode2 != nullptr);
  4505. if (pnodeArg->pnode2->nop == knopFncDecl)
  4506. {
  4507. Assert(fullNameHintLength >= shortNameOffset);
  4508. ParseNodeFnc * pnodeFunc = pnodeArg->pnode2->AsParseNodeFnc();
  4509. pnodeFunc->hint = pFullNameHint;
  4510. pnodeFunc->hintLength = fullNameHintLength;
  4511. pnodeFunc->hintOffset = shortNameOffset;
  4512. }
  4513. AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeArg);
  4514. }
  4515. }
  4516. pidHint = nullptr;
  4517. pFullNameHint = nullptr;
  4518. if (tkComma != m_token.tk)
  4519. {
  4520. break;
  4521. }
  4522. this->GetScanner()->ScanForcingPid();
  4523. if (tkRCurly == m_token.tk)
  4524. {
  4525. break;
  4526. }
  4527. if (seenRest) // Rest must be in the last position.
  4528. {
  4529. Error(ERRDestructRestLast);
  4530. }
  4531. }
  4532. m_hasDeferredShorthandInitError = m_hasDeferredShorthandInitError || hasDeferredInitError;
  4533. if (buildAST)
  4534. {
  4535. Assert(lastNodeRef);
  4536. Assert(*lastNodeRef);
  4537. pnodeList->ichLim = (*lastNodeRef)->ichLim;
  4538. }
  4539. return pnodeList;
  4540. }
  4541. BOOL Parser::WillDeferParse(Js::LocalFunctionId functionId)
  4542. {
  4543. if ((m_grfscr & fscrWillDeferFncParse) != 0)
  4544. {
  4545. if (m_stoppedDeferredParse)
  4546. {
  4547. return false;
  4548. }
  4549. if (!PHASE_ENABLED_RAW(DeferParsePhase, m_sourceContextInfo->sourceContextId, functionId))
  4550. {
  4551. return false;
  4552. }
  4553. if (PHASE_FORCE_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, functionId)
  4554. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  4555. || Js::Configuration::Global.flags.IsEnabled(Js::ForceUndoDeferFlag)
  4556. #endif
  4557. )
  4558. {
  4559. return true;
  4560. }
  4561. #if ENABLE_PROFILE_INFO
  4562. #ifndef DISABLE_DYNAMIC_PROFILE_DEFER_PARSE
  4563. if (m_sourceContextInfo->sourceDynamicProfileManager != nullptr)
  4564. {
  4565. Js::ExecutionFlags flags = m_sourceContextInfo->sourceDynamicProfileManager->IsFunctionExecuted(functionId);
  4566. return flags != Js::ExecutionFlags_Executed;
  4567. }
  4568. #endif
  4569. #endif
  4570. return true;
  4571. }
  4572. return false;
  4573. }
  4574. //
  4575. // Call this in ParseFncDecl only to check (and reset) if ParseFncDecl is re-parsing a deferred
  4576. // function body. If a deferred function is called and being re-parsed, it shouldn't be deferred again.
  4577. //
  4578. BOOL Parser::IsDeferredFnc()
  4579. {
  4580. if (m_grfscr & fscrDeferredFnc)
  4581. {
  4582. m_grfscr &= ~fscrDeferredFnc;
  4583. return true;
  4584. }
  4585. return false;
  4586. }
  4587. template<bool buildAST>
  4588. ParseNode * Parser::ParseFncDeclCheckScope(ushort flags, bool fAllowIn)
  4589. {
  4590. ParseNodeBlock * pnodeFncBlockScope = nullptr;
  4591. ParseNodePtr *ppnodeScopeSave = nullptr;
  4592. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  4593. bool fDeclaration = flags & fFncDeclaration;
  4594. bool noStmtContext = false;
  4595. if (fDeclaration)
  4596. {
  4597. noStmtContext = m_pstmtCur->GetNop() != knopBlock;
  4598. if (noStmtContext)
  4599. {
  4600. // We have a function declaration like "if (a) function f() {}". We didn't see
  4601. // a block scope on the way in, so we need to pretend we did. Note that this is a syntax error
  4602. // in strict mode.
  4603. if (!this->FncDeclAllowedWithoutContext(flags))
  4604. {
  4605. Error(ERRsyntax);
  4606. }
  4607. pnodeFncBlockScope = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
  4608. if (buildAST)
  4609. {
  4610. PushFuncBlockScope(pnodeFncBlockScope, &ppnodeScopeSave, &ppnodeExprScopeSave);
  4611. }
  4612. }
  4613. }
  4614. ParseNodeFnc * pnodeFnc = ParseFncDeclInternal<buildAST>(flags, nullptr, /* needsPIDOnRCurlyScan */ false, /* fUnaryOrParen */ false, noStmtContext, SuperRestrictionState::Disallowed, fAllowIn);
  4615. if (pnodeFncBlockScope)
  4616. {
  4617. Assert(pnodeFncBlockScope->pnodeStmt == nullptr);
  4618. pnodeFncBlockScope->pnodeStmt = pnodeFnc;
  4619. if (buildAST)
  4620. {
  4621. PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
  4622. }
  4623. FinishParseBlock(pnodeFncBlockScope);
  4624. return pnodeFncBlockScope;
  4625. }
  4626. return pnodeFnc;
  4627. }
  4628. template<bool buildAST>
  4629. ParseNodeFnc * Parser::ParseFncDeclNoCheckScope(ushort flags, SuperRestrictionState::State superRestrictionState, LPCOLESTR pNameHint, const bool needsPIDOnRCurlyScan, bool fUnaryOrParen, bool fAllowIn)
  4630. {
  4631. Assert((flags & fFncDeclaration) == 0);
  4632. return ParseFncDeclInternal<buildAST>(flags, pNameHint, needsPIDOnRCurlyScan, fUnaryOrParen, /* noStmtContext */ false, superRestrictionState, fAllowIn);
  4633. }
  4634. template<bool buildAST>
  4635. ParseNodeFnc * Parser::ParseFncDeclInternal(ushort flags, LPCOLESTR pNameHint, const bool needsPIDOnRCurlyScan, bool fUnaryOrParen, bool noStmtContext, SuperRestrictionState::State superRestrictionState, bool fAllowIn)
  4636. {
  4637. ParseNodeFnc * pnodeFnc = nullptr;
  4638. ParseNodePtr *ppnodeVarSave = nullptr;
  4639. bool fDeclaration = flags & fFncDeclaration;
  4640. bool fModule = (flags & fFncModule) != 0;
  4641. bool fLambda = (flags & fFncLambda) != 0;
  4642. charcount_t ichMin = this->GetScanner()->IchMinTok();
  4643. bool wasInDeferredNestedFunc = false;
  4644. uint tryCatchOrFinallyDepthSave = this->m_tryCatchOrFinallyDepth;
  4645. this->m_tryCatchOrFinallyDepth = 0;
  4646. if (this->m_arrayDepth)
  4647. {
  4648. this->m_funcInArrayDepth++; // Count function depth within array literal
  4649. }
  4650. // Update the count of functions nested in the current parent.
  4651. Assert(m_pnestedCount || !buildAST);
  4652. uint *pnestedCountSave = m_pnestedCount;
  4653. if (buildAST || m_pnestedCount)
  4654. {
  4655. (*m_pnestedCount)++;
  4656. }
  4657. uint scopeCountNoAstSave = m_scopeCountNoAst;
  4658. m_scopeCountNoAst = 0;
  4659. // Create the node.
  4660. pnodeFnc = CreateAllowDeferNodeForOpT<knopFncDecl>();
  4661. pnodeFnc->SetDeclaration(fDeclaration);
  4662. pnodeFnc->nestedFuncEscapes = false;
  4663. pnodeFnc->cbMin = this->GetScanner()->IecpMinTok();
  4664. pnodeFnc->cbStringMin = pnodeFnc->cbMin;
  4665. pnodeFnc->cbStringLim = pnodeFnc->cbLim;
  4666. pnodeFnc->functionId = (*m_nextFunctionId)++;
  4667. pnodeFnc->superRestrictionState = superRestrictionState;
  4668. // Push new parser state with this new function node
  4669. AppendFunctionToScopeList(fDeclaration, pnodeFnc);
  4670. // Start the argument list.
  4671. ppnodeVarSave = m_ppnodeVar;
  4672. if (buildAST)
  4673. {
  4674. pnodeFnc->lineNumber = this->GetScanner()->LineCur();
  4675. pnodeFnc->columnNumber = CalculateFunctionColumnNumber();
  4676. pnodeFnc->SetNested(m_currentNodeFunc != nullptr); // If there is a current function, then we're a nested function.
  4677. pnodeFnc->SetStrictMode(IsStrictMode()); // Inherit current strict mode -- may be overridden by the function itself if it contains a strict mode directive.
  4678. m_pCurrentAstSize = &pnodeFnc->astSize;
  4679. }
  4680. else // if !buildAST
  4681. {
  4682. wasInDeferredNestedFunc = m_inDeferredNestedFunc;
  4683. m_inDeferredNestedFunc = true;
  4684. }
  4685. m_pnestedCount = &pnodeFnc->nestedCount;
  4686. AnalysisAssert(pnodeFnc);
  4687. pnodeFnc->SetIsAsync((flags & fFncAsync) != 0);
  4688. pnodeFnc->SetIsLambda(fLambda);
  4689. pnodeFnc->SetIsMethod((flags & fFncMethod) != 0);
  4690. pnodeFnc->SetIsClassMember((flags & fFncClassMember) != 0);
  4691. pnodeFnc->SetIsModule(fModule);
  4692. pnodeFnc->SetIsClassConstructor((flags & fFncClassConstructor) != 0);
  4693. pnodeFnc->SetIsBaseClassConstructor((flags & fFncBaseClassConstructor) != 0);
  4694. pnodeFnc->SetHomeObjLocation(Js::Constants::NoRegister);
  4695. pnodeFnc->SetHasNonThisStmt(pnodeFnc->IsClassConstructor());
  4696. if (this->m_currentScope && this->m_currentScope->GetScopeType() == ScopeType_Parameter)
  4697. {
  4698. pnodeFnc->SetIsDeclaredInParamScope();
  4699. this->m_currentScope->SetHasNestedParamFunc();
  4700. }
  4701. if (this->m_currentScope && this->m_currentScope->GetScopeType() == ScopeType_Parameter)
  4702. {
  4703. pnodeFnc->SetIsDeclaredInParamScope();
  4704. this->m_currentScope->SetHasNestedParamFunc();
  4705. }
  4706. IdentPtr pFncNamePid = nullptr;
  4707. bool needScanRCurly = true;
  4708. ParseFncDeclHelper<buildAST>(pnodeFnc, pNameHint, flags, fUnaryOrParen, noStmtContext, &needScanRCurly, fModule, &pFncNamePid, fAllowIn);
  4709. AddNestedCapturedNames(pnodeFnc);
  4710. AnalysisAssert(pnodeFnc);
  4711. *m_ppnodeVar = nullptr;
  4712. m_ppnodeVar = ppnodeVarSave;
  4713. if (m_currentNodeFunc && (pnodeFnc->CallsEval() || pnodeFnc->ChildCallsEval()))
  4714. {
  4715. GetCurrentFunctionNode()->SetChildCallsEval(true);
  4716. }
  4717. // Lambdas do not have "arguments" and instead capture their parent's
  4718. // binding of "arguments. To ensure the arguments object of the enclosing
  4719. // non-lambda function is loaded propagate the UsesArguments flag up to
  4720. // the parent function
  4721. if (fLambda && (pnodeFnc->UsesArguments() || pnodeFnc->CallsEval()))
  4722. {
  4723. ParseNodeFnc * pnodeFncParent = GetCurrentFunctionNode();
  4724. if (pnodeFncParent != nullptr)
  4725. {
  4726. pnodeFncParent->SetUsesArguments();
  4727. }
  4728. else
  4729. {
  4730. m_UsesArgumentsAtGlobal = true;
  4731. }
  4732. }
  4733. if (needScanRCurly && !fModule)
  4734. {
  4735. // Consume the next token now that we're back in the enclosing function (whose strictness may be
  4736. // different from the function we just finished).
  4737. #if DBG
  4738. bool expectedTokenValid = m_token.tk == tkRCurly;
  4739. AssertMsg(expectedTokenValid, "Invalid token expected for RCurly match");
  4740. #endif
  4741. // The next token may need to have a PID created in !buildAST mode, as we may be parsing a method with a string name.
  4742. if (needsPIDOnRCurlyScan)
  4743. {
  4744. this->GetScanner()->ScanForcingPid();
  4745. }
  4746. else
  4747. {
  4748. this->GetScanner()->Scan();
  4749. }
  4750. }
  4751. m_pnestedCount = pnestedCountSave;
  4752. Assert(!buildAST || !wasInDeferredNestedFunc);
  4753. m_inDeferredNestedFunc = wasInDeferredNestedFunc;
  4754. if (this->m_arrayDepth)
  4755. {
  4756. this->m_funcInArrayDepth--;
  4757. if (this->m_funcInArrayDepth == 0)
  4758. {
  4759. // We disable deferred parsing if array literals dominate.
  4760. // But don't do this if the array literal is dominated by function bodies.
  4761. if (flags & (fFncMethod | fFncClassMember) && m_token.tk != tkSColon)
  4762. {
  4763. // Class member methods have optional separators. We need to check whether we are
  4764. // getting the IchLim of the correct token.
  4765. Assert(this->GetScanner()->m_tkPrevious == tkRCurly && needScanRCurly);
  4766. this->m_funcInArray += this->GetScanner()->IchMinTok() - /*tkRCurly*/ 1 - ichMin;
  4767. }
  4768. else
  4769. {
  4770. this->m_funcInArray += this->GetScanner()->IchLimTok() - ichMin;
  4771. }
  4772. }
  4773. }
  4774. m_scopeCountNoAst = scopeCountNoAstSave;
  4775. if (fDeclaration && !IsStrictMode())
  4776. {
  4777. if (pFncNamePid != nullptr &&
  4778. GetCurrentBlock() &&
  4779. GetCurrentBlock()->blockType == PnodeBlockType::Regular)
  4780. {
  4781. // Add a function-scoped VarDecl with the same name as the function for
  4782. // back compat with pre-ES6 code that declares functions in blocks. The
  4783. // idea is that the last executed declaration wins at the function scope
  4784. // level and we accomplish this by having each block scoped function
  4785. // declaration assign to both the block scoped "let" binding, as well
  4786. // as the function scoped "var" binding.
  4787. ParseNodeVar * vardecl = CreateVarDeclNode(pFncNamePid, STVariable, false, nullptr, false);
  4788. vardecl->isBlockScopeFncDeclVar = true;
  4789. if (GetCurrentFunctionNode() && vardecl->sym->GetIsFormal())
  4790. {
  4791. GetCurrentFunctionNode()->SetHasAnyWriteToFormals(true);
  4792. }
  4793. }
  4794. }
  4795. if (buildAST && fDeclaration)
  4796. {
  4797. Symbol* funcSym = pnodeFnc->GetFuncSymbol();
  4798. if (funcSym->GetIsFormal())
  4799. {
  4800. GetCurrentFunctionNode()->SetHasAnyWriteToFormals(true);
  4801. }
  4802. } this->m_tryCatchOrFinallyDepth = tryCatchOrFinallyDepthSave;
  4803. return pnodeFnc;
  4804. }
  4805. bool Parser::FncDeclAllowedWithoutContext(ushort flags)
  4806. {
  4807. // Statement context required for strict mode, async functions, and generators.
  4808. // Note that generators aren't detected yet when this method is called; they're checked elsewhere.
  4809. return !IsStrictMode() && !(flags & fFncAsync);
  4810. }
  4811. uint Parser::CalculateFunctionColumnNumber()
  4812. {
  4813. uint columnNumber;
  4814. charcount_t ichMinTok = this->GetScanner()->IchMinTok();
  4815. charcount_t ichMinLine = this->GetScanner()->IchMinLine();
  4816. if (ichMinTok >= ichMinLine)
  4817. {
  4818. // In scenarios involving defer parse IchMinLine() can be incorrect for the first line after defer parse
  4819. columnNumber = ichMinTok - ichMinLine;
  4820. if (m_functionBody != nullptr && m_functionBody->GetRelativeLineNumber() == this->GetScanner()->LineCur())
  4821. {
  4822. // Adjust the column if it falls on the first line, where the re-parse is happening.
  4823. columnNumber += m_functionBody->GetRelativeColumnNumber();
  4824. }
  4825. }
  4826. else if (m_currentNodeFunc)
  4827. {
  4828. // For the first line after defer parse, compute the column relative to the column number
  4829. // of the lexically parent function.
  4830. ULONG offsetFromCurrentFunction = ichMinTok - m_currentNodeFunc->ichMin;
  4831. columnNumber = m_currentNodeFunc->columnNumber + offsetFromCurrentFunction;
  4832. }
  4833. else
  4834. {
  4835. // if there is no current function, lets give a default of 0.
  4836. columnNumber = 0;
  4837. }
  4838. return columnNumber;
  4839. }
  4840. void Parser::AppendFunctionToScopeList(bool fDeclaration, ParseNodeFnc * pnodeFnc)
  4841. {
  4842. if (!fDeclaration && m_ppnodeExprScope)
  4843. {
  4844. // We're tracking function expressions separately from declarations in this scope
  4845. // (e.g., inside a catch scope in standards mode).
  4846. Assert(*m_ppnodeExprScope == nullptr);
  4847. *m_ppnodeExprScope = pnodeFnc;
  4848. m_ppnodeExprScope = &pnodeFnc->pnodeNext;
  4849. }
  4850. else
  4851. {
  4852. Assert(*m_ppnodeScope == nullptr);
  4853. *m_ppnodeScope = pnodeFnc;
  4854. m_ppnodeScope = &pnodeFnc->pnodeNext;
  4855. }
  4856. }
  4857. /***************************************************************************
  4858. Parse a function definition.
  4859. ***************************************************************************/
  4860. template<bool buildAST>
  4861. void Parser::ParseFncDeclHelper(ParseNodeFnc * pnodeFnc, LPCOLESTR pNameHint, ushort flags, bool fUnaryOrParen, bool noStmtContext, bool *pNeedScanRCurly, bool skipFormals, IdentPtr* pFncNamePid, bool fAllowIn)
  4862. {
  4863. Assert(pnodeFnc);
  4864. ParseNodeFnc * pnodeFncParent = GetCurrentFunctionNode();
  4865. // is the following correct? When buildAST is false, m_currentNodeDeferredFunc can be nullptr on transition to deferred parse from non-deferred
  4866. ParseNodeFnc * pnodeFncSave = buildAST ? m_currentNodeFunc : m_currentNodeDeferredFunc;
  4867. ParseNodeFnc * pnodeFncSaveNonLambda = buildAST ? m_currentNodeNonLambdaFunc : m_currentNodeNonLambdaDeferredFunc;
  4868. int32* pAstSizeSave = m_pCurrentAstSize;
  4869. bool fDeclaration = (flags & fFncDeclaration) != 0;
  4870. bool fLambda = (flags & fFncLambda) != 0;
  4871. bool fAsync = (flags & fFncAsync) != 0;
  4872. bool fModule = (flags & fFncModule) != 0;
  4873. bool fDeferred = false;
  4874. StmtNest *pstmtSave;
  4875. bool fFunctionInBlock = false;
  4876. if (buildAST)
  4877. {
  4878. fFunctionInBlock = GetCurrentBlockInfo() != GetCurrentFunctionBlockInfo() &&
  4879. (GetCurrentBlockInfo()->pnodeBlock->scope == nullptr ||
  4880. GetCurrentBlockInfo()->pnodeBlock->scope->GetScopeType() != ScopeType_GlobalEvalBlock);
  4881. }
  4882. // Save the position of the scanner in case we need to inspect the name hint later
  4883. RestorePoint beginNameHint;
  4884. this->GetScanner()->Capture(&beginNameHint);
  4885. ParseNodeBlock * pnodeFncExprScope = nullptr;
  4886. Scope *fncExprScope = nullptr;
  4887. if (!fDeclaration)
  4888. {
  4889. if (!fLambda)
  4890. {
  4891. pnodeFncExprScope = StartParseBlock<buildAST>(PnodeBlockType::Function, ScopeType_FuncExpr);
  4892. fncExprScope = pnodeFncExprScope->scope;
  4893. }
  4894. // Function expression: push the new function onto the stack now so that the name (if any) will be
  4895. // local to the new function.
  4896. this->UpdateCurrentNodeFunc<buildAST>(pnodeFnc, fLambda);
  4897. }
  4898. if (!fLambda && !fModule)
  4899. {
  4900. this->ParseFncName<buildAST>(pnodeFnc, flags, pFncNamePid);
  4901. }
  4902. if (fDeclaration)
  4903. {
  4904. // Declaration statement: push the new function now, after parsing the name, so the name is local to the
  4905. // enclosing function.
  4906. this->UpdateCurrentNodeFunc<buildAST>(pnodeFnc, fLambda);
  4907. }
  4908. if (noStmtContext && pnodeFnc->IsGenerator())
  4909. {
  4910. // Generator decl not allowed outside stmt context. (We have to wait until we've parsed the '*' to
  4911. // detect generator.)
  4912. Error(ERRsyntax, pnodeFnc);
  4913. }
  4914. // switch scanner to treat 'yield' as keyword in generator functions
  4915. // or as an identifier in non-generator functions
  4916. bool fPreviousYieldIsKeyword = this->GetScanner()->SetYieldIsKeywordRegion(pnodeFnc->IsGenerator());
  4917. bool fPreviousAwaitIsKeyword = this->GetScanner()->SetAwaitIsKeywordRegion(fAsync);
  4918. if (pnodeFnc->IsGenerator())
  4919. {
  4920. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Generator, m_scriptContext);
  4921. }
  4922. if (fncExprScope && pnodeFnc->pnodeName == nullptr)
  4923. {
  4924. FinishParseBlock(pnodeFncExprScope);
  4925. m_nextBlockId--;
  4926. Adelete(&m_nodeAllocator, fncExprScope);
  4927. fncExprScope = nullptr;
  4928. pnodeFncExprScope = nullptr;
  4929. }
  4930. pnodeFnc->scope = fncExprScope;
  4931. // Start a new statement stack.
  4932. bool topLevelStmt =
  4933. buildAST &&
  4934. !fFunctionInBlock &&
  4935. (this->m_pstmtCur == nullptr || this->m_pstmtCur->pnodeStmt->nop == knopBlock);
  4936. pstmtSave = m_pstmtCur;
  4937. SetCurrentStatement(nullptr);
  4938. RestorePoint beginFormals;
  4939. this->GetScanner()->Capture(&beginFormals);
  4940. BOOL fWasAlreadyStrictMode = IsStrictMode();
  4941. BOOL oldStrictMode = this->m_fUseStrictMode;
  4942. if (fLambda)
  4943. {
  4944. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Lambda, m_scriptContext);
  4945. }
  4946. uint uCanDeferSave = m_grfscr & fscrCanDeferFncParse;
  4947. uint uDeferSave = m_grfscr & fscrWillDeferFncParse;
  4948. bool isTopLevelDeferredFunc = false;
  4949. #if ENABLE_BACKGROUND_PARSING
  4950. struct AutoFastScanFlag {
  4951. bool savedDoingFastScan;
  4952. AutoFastScanFlag(Parser *parser) : m_parser(parser) { savedDoingFastScan = m_parser->m_doingFastScan; }
  4953. ~AutoFastScanFlag() { m_parser->m_doingFastScan = savedDoingFastScan; }
  4954. Parser *m_parser;
  4955. } flag(this);
  4956. #endif
  4957. bool doParallel = false;
  4958. #if ENABLE_BACKGROUND_PARSING
  4959. bool parallelJobStarted = false;
  4960. #endif
  4961. if (buildAST)
  4962. {
  4963. bool isLikelyIIFE = !fDeclaration && fUnaryOrParen;
  4964. BOOL isDeferredFnc = IsDeferredFnc();
  4965. // These are the conditions that prohibit upfront deferral *and* redeferral.
  4966. isTopLevelDeferredFunc =
  4967. (m_grfscr & fscrCanDeferFncParse)
  4968. && !m_InAsmMode
  4969. // Don't defer a module function wrapper because we need to do export resolution at parse time
  4970. && !fModule;
  4971. pnodeFnc->SetCanBeDeferred(isTopLevelDeferredFunc && ParseNodeFnc::CanBeRedeferred(pnodeFnc->fncFlags));
  4972. // These are heuristic conditions that prohibit upfront deferral but not redeferral.
  4973. isTopLevelDeferredFunc = isTopLevelDeferredFunc && !isDeferredFnc && WillDeferParse(pnodeFnc->functionId) &&
  4974. (!isLikelyIIFE || !topLevelStmt || PHASE_FORCE_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, pnodeFnc->functionId));
  4975. #if ENABLE_BACKGROUND_PARSING
  4976. if (!fLambda &&
  4977. !isDeferredFnc &&
  4978. !isLikelyIIFE &&
  4979. !this->IsBackgroundParser() &&
  4980. !this->m_doingFastScan &&
  4981. !(pnodeFncSave && m_currDeferredStub) &&
  4982. !(this->m_parseType == ParseType_Deferred && this->m_functionBody && this->m_functionBody->GetScopeInfo() && !isTopLevelDeferredFunc))
  4983. {
  4984. doParallel = DoParallelParse(pnodeFnc);
  4985. if (doParallel)
  4986. {
  4987. BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
  4988. Assert(bgp);
  4989. if (bgp->HasFailedBackgroundParseItem())
  4990. {
  4991. Error(ERRsyntax);
  4992. }
  4993. doParallel = bgp->ParseBackgroundItem(this, pnodeFnc, isTopLevelDeferredFunc);
  4994. if (doParallel)
  4995. {
  4996. parallelJobStarted = true;
  4997. this->m_hasParallelJob = true;
  4998. this->m_doingFastScan = true;
  4999. doParallel = FastScanFormalsAndBody();
  5000. if (doParallel)
  5001. {
  5002. // Let the foreground thread take care of marking the limit on the function node,
  5003. // because in some cases this function's caller will want to change that limit,
  5004. // so we don't want the background thread to try and touch it.
  5005. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  5006. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  5007. }
  5008. }
  5009. }
  5010. }
  5011. #endif
  5012. }
  5013. if (!doParallel)
  5014. {
  5015. #if ENABLE_BACKGROUND_PARSING
  5016. // We don't want to, or couldn't, let the main thread scan past this function body, so parse
  5017. // it for real.
  5018. ParseNodeFnc * pnodeRealFnc = pnodeFnc;
  5019. if (parallelJobStarted)
  5020. {
  5021. // We have to deal with a failure to fast-scan the function (due to syntax error? "/"?) when
  5022. // a background thread may already have begun to work on the job. Both threads can't be allowed to
  5023. // operate on the same node.
  5024. pnodeFnc = CreateDummyFuncNode(fDeclaration);
  5025. }
  5026. #endif
  5027. AnalysisAssert(pnodeFnc);
  5028. ParseNodeBlock * pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Parameter, ScopeType_Parameter);
  5029. AnalysisAssert(pnodeBlock != nullptr);
  5030. pnodeFnc->pnodeScopes = pnodeBlock;
  5031. m_ppnodeVar = &pnodeFnc->pnodeParams;
  5032. pnodeFnc->pnodeVars = nullptr;
  5033. ParseNodePtr* varNodesList = &pnodeFnc->pnodeVars;
  5034. ParseNodeVar * argNode = nullptr;
  5035. if (!fModule && !fLambda)
  5036. {
  5037. ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
  5038. m_ppnodeVar = &pnodeFnc->pnodeVars;
  5039. // Create the built-in arguments symbol
  5040. argNode = this->AddArgumentsNodeToVars(pnodeFnc);
  5041. // Save the updated var list
  5042. varNodesList = m_ppnodeVar;
  5043. m_ppnodeVar = ppnodeVarSave;
  5044. }
  5045. ParseNodePtr *ppnodeScopeSave = nullptr;
  5046. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  5047. ppnodeScopeSave = m_ppnodeScope;
  5048. if (pnodeBlock)
  5049. {
  5050. // This synthetic block scope will contain all the nested scopes.
  5051. m_ppnodeScope = &pnodeBlock->pnodeScopes;
  5052. pnodeBlock->pnodeStmt = pnodeFnc;
  5053. }
  5054. // Keep nested function declarations and expressions in the same list at function scope.
  5055. // (Indicate this by nulling out the current function expressions list.)
  5056. ppnodeExprScopeSave = m_ppnodeExprScope;
  5057. m_ppnodeExprScope = nullptr;
  5058. uint parenExprDepthSave = m_funcParenExprDepth;
  5059. m_funcParenExprDepth = 0;
  5060. if (!skipFormals)
  5061. {
  5062. bool fLambdaParamsSave = m_reparsingLambdaParams;
  5063. if (fLambda)
  5064. {
  5065. m_reparsingLambdaParams = true;
  5066. }
  5067. uint savedStubCount = m_currDeferredStubCount;
  5068. DeferredFunctionStub* savedStub = m_currDeferredStub;
  5069. ShiftCurrDeferredStubToChildFunction(pnodeFnc, pnodeFncParent);
  5070. this->ParseFncFormals<buildAST>(pnodeFnc, pnodeFncParent, flags, isTopLevelDeferredFunc);
  5071. m_currDeferredStub = savedStub;
  5072. m_currDeferredStubCount = savedStubCount;
  5073. m_reparsingLambdaParams = fLambdaParamsSave;
  5074. }
  5075. // Create function body scope
  5076. ParseNodeBlock * pnodeInnerBlock = StartParseBlock<buildAST>(PnodeBlockType::Function, ScopeType_FunctionBody);
  5077. // Set the parameter block's child to the function body block.
  5078. // The pnodeFnc->pnodeScopes list is constructed in such a way that it includes all the scopes in this list.
  5079. // For example if the param scope has one function and body scope has one function then the list will look like below,
  5080. // param scope block -> function decl from param scope -> body socpe block -> function decl from body scope.
  5081. *m_ppnodeScope = pnodeInnerBlock;
  5082. pnodeFnc->pnodeBodyScope = pnodeInnerBlock;
  5083. // This synthetic block scope will contain all the nested scopes.
  5084. m_ppnodeScope = &pnodeInnerBlock->pnodeScopes;
  5085. pnodeInnerBlock->pnodeStmt = pnodeFnc;
  5086. // DEFER: Begin deferral here (after names are parsed and name nodes created).
  5087. // Create no more AST nodes until we're done.
  5088. // Try to defer this func if all these are true:
  5089. // 0. We are not already in deferred parsing (i.e. buildAST is true)
  5090. // 1. We are not re-parsing a deferred func which is being invoked.
  5091. // 2. Dynamic profile suggests this func can be deferred (and deferred parse is on).
  5092. // 3. This func is top level or defer nested func is on.
  5093. // 4. Optionally, the function is non-nested and not in eval, or the deferral decision was based on cached profile info,
  5094. // or the function is sufficiently long. (I.e., don't defer little nested functions unless we're
  5095. // confident they'll never be executed, because un-deferring nested functions is more expensive.)
  5096. // NOTE: I'm disabling #4 by default, because we've found other ways to reduce the cost of un-deferral,
  5097. // and we don't want to create function bodies aggressively for little functions.
  5098. // We will also temporarily defer all asm.js functions, except for the asm.js
  5099. // module itself, which we will never defer
  5100. bool strictModeTurnedOn = false;
  5101. if (isTopLevelDeferredFunc &&
  5102. !(this->m_grfscr & fscrEvalCode) &&
  5103. pnodeFnc->IsNested() &&
  5104. #ifndef DISABLE_DYNAMIC_PROFILE_DEFER_PARSE
  5105. m_sourceContextInfo->sourceDynamicProfileManager == nullptr &&
  5106. #endif
  5107. PHASE_ON_RAW(Js::ScanAheadPhase, m_sourceContextInfo->sourceContextId, pnodeFnc->functionId) &&
  5108. (
  5109. !PHASE_FORCE_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, pnodeFnc->functionId) ||
  5110. PHASE_FORCE_RAW(Js::ScanAheadPhase, m_sourceContextInfo->sourceContextId, pnodeFnc->functionId)
  5111. ))
  5112. {
  5113. // Try to scan ahead to the end of the function. If we get there before we've scanned a minimum
  5114. // number of tokens, don't bother deferring, because it's too small.
  5115. if (this->ScanAheadToFunctionEnd(CONFIG_FLAG(MinDeferredFuncTokenCount)))
  5116. {
  5117. isTopLevelDeferredFunc = false;
  5118. }
  5119. }
  5120. Scope* paramScope = pnodeFnc->pnodeScopes ? pnodeFnc->pnodeScopes->scope : nullptr;
  5121. if (paramScope != nullptr)
  5122. {
  5123. if (CONFIG_FLAG(ForceSplitScope))
  5124. {
  5125. pnodeFnc->ResetBodyAndParamScopeMerged();
  5126. }
  5127. else if (pnodeFnc->HasNonSimpleParameterList() && pnodeFnc->IsBodyAndParamScopeMerged())
  5128. {
  5129. paramScope->ForEachSymbolUntil([this, paramScope, pnodeFnc](Symbol* sym) {
  5130. if (sym->GetPid()->GetTopRef()->GetFuncScopeId() > pnodeFnc->functionId)
  5131. {
  5132. // One of the symbol has non local reference. Mark the param scope as we can't merge it with body scope.
  5133. pnodeFnc->ResetBodyAndParamScopeMerged();
  5134. return true;
  5135. }
  5136. return false;
  5137. });
  5138. }
  5139. }
  5140. // If the param scope is merged with the body scope we want to use the param scope symbols in the body scope.
  5141. // So add a pid ref for the body using the param scope symbol. Note that in this case the same symbol will occur twice
  5142. // in the same pid ref stack.
  5143. if (paramScope != nullptr && pnodeFnc->IsBodyAndParamScopeMerged())
  5144. {
  5145. paramScope->ForEachSymbol([this](Symbol* paramSym)
  5146. {
  5147. PidRefStack* ref = PushPidRef(paramSym->GetPid());
  5148. ref->SetSym(paramSym);
  5149. });
  5150. }
  5151. AssertMsg(m_funcParenExprDepth == 0, "Paren exprs should have been resolved by the time we finish function formals");
  5152. if (fLambda)
  5153. {
  5154. #ifdef ASMJS_PLAT
  5155. if (m_InAsmMode && (isTopLevelDeferredFunc && m_deferAsmJs))
  5156. {
  5157. // asm.js doesn't support lambda functions
  5158. Js::AsmJSCompiler::OutputError(m_scriptContext, _u("Lambda functions are not supported."));
  5159. Js::AsmJSCompiler::OutputError(m_scriptContext, _u("Asm.js compilation failed."));
  5160. throw Js::AsmJsParseException();
  5161. }
  5162. #endif
  5163. }
  5164. if (m_token.tk == tkRParen)
  5165. {
  5166. this->GetScanner()->Scan();
  5167. }
  5168. if (fLambda)
  5169. {
  5170. BOOL hadNewLine = this->GetScanner()->FHadNewLine();
  5171. // it can be the case we do not have a fat arrow here if there is a valid expression on the left hand side
  5172. // of the fat arrow, but that expression does not parse as a parameter list. E.g.
  5173. // a.x => { }
  5174. // Therefore check for it and error if not found.
  5175. ChkCurTok(tkDArrow, ERRnoDArrow);
  5176. // Newline character between arrow parameters and fat arrow is a syntax error but we want to check for
  5177. // this after verifying there was a => token. Otherwise we would throw the wrong error.
  5178. if (hadNewLine)
  5179. {
  5180. Error(ERRValidIfFollowedBy, _u("Lambda parameter list"), _u("'=>' on the same line"));
  5181. }
  5182. }
  5183. if (isTopLevelDeferredFunc || (m_InAsmMode && m_deferAsmJs))
  5184. {
  5185. fDeferred = true;
  5186. this->ParseTopLevelDeferredFunc(pnodeFnc, pnodeFncSave, pNameHint, fLambda, pNeedScanRCurly, fAllowIn);
  5187. }
  5188. else
  5189. {
  5190. AnalysisAssert(pnodeFnc);
  5191. // Shouldn't be any temps in the arg list.
  5192. Assert(*m_ppnodeVar == nullptr);
  5193. // Start the var list.
  5194. m_ppnodeVar = varNodesList;
  5195. if (!pnodeFnc->IsBodyAndParamScopeMerged())
  5196. {
  5197. OUTPUT_TRACE_DEBUGONLY(Js::ParsePhase, _u("The param and body scope of the function %s cannot be merged\n"), pnodeFnc->pnodeName ? pnodeFnc->pnodeName->pid->Psz() : _u("Anonymous function"));
  5198. }
  5199. // Keep nested function declarations and expressions in the same list at function scope.
  5200. // (Indicate this by nulling out the current function expressions list.)
  5201. m_ppnodeExprScope = nullptr;
  5202. if (buildAST)
  5203. {
  5204. if (m_token.tk != tkLCurly && fLambda)
  5205. {
  5206. *pNeedScanRCurly = false;
  5207. }
  5208. uint savedStubCount = m_currDeferredStubCount;
  5209. DeferredFunctionStub* savedStub = m_currDeferredStub;
  5210. ShiftCurrDeferredStubToChildFunction(pnodeFnc, pnodeFncSave);
  5211. this->FinishFncDecl(pnodeFnc, pNameHint, fLambda, skipFormals, fAllowIn);
  5212. m_currDeferredStub = savedStub;
  5213. m_currDeferredStubCount = savedStubCount;
  5214. }
  5215. else
  5216. {
  5217. this->ParseNestedDeferredFunc(pnodeFnc, fLambda, pNeedScanRCurly, &strictModeTurnedOn, fAllowIn);
  5218. }
  5219. }
  5220. // Restore the paren count for any outer spread/rest error checking.
  5221. m_funcParenExprDepth = parenExprDepthSave;
  5222. if (pnodeInnerBlock)
  5223. {
  5224. FinishParseBlock(pnodeInnerBlock, *pNeedScanRCurly);
  5225. }
  5226. if (!fModule && (m_token.tk == tkLCurly || !fLambda))
  5227. {
  5228. UpdateArgumentsNode(pnodeFnc, argNode);
  5229. }
  5230. CreateSpecialSymbolDeclarations(pnodeFnc);
  5231. // Restore the lists of scopes that contain function expressions.
  5232. Assert(m_ppnodeExprScope == nullptr || *m_ppnodeExprScope == nullptr);
  5233. m_ppnodeExprScope = ppnodeExprScopeSave;
  5234. Assert(m_ppnodeScope);
  5235. Assert(nullptr == *m_ppnodeScope);
  5236. m_ppnodeScope = ppnodeScopeSave;
  5237. if (pnodeBlock)
  5238. {
  5239. FinishParseBlock(pnodeBlock, *pNeedScanRCurly);
  5240. }
  5241. if (IsStrictMode() || strictModeTurnedOn)
  5242. {
  5243. this->m_fUseStrictMode = TRUE; // Now we know this function is in strict mode
  5244. if (!fWasAlreadyStrictMode)
  5245. {
  5246. // If this function turned on strict mode then we didn't check the formal
  5247. // parameters or function name hint for future reserved word usage. So do that now.
  5248. RestorePoint afterFnc;
  5249. this->GetScanner()->Capture(&afterFnc);
  5250. if (pnodeFnc->pnodeName != nullptr)
  5251. {
  5252. // Rewind to the function name hint and check if the token is a reserved word.
  5253. this->GetScanner()->SeekTo(beginNameHint);
  5254. this->GetScanner()->Scan();
  5255. if (pnodeFnc->IsGenerator())
  5256. {
  5257. Assert(m_token.tk == tkStar);
  5258. Assert(m_scriptContext->GetConfig()->IsES6GeneratorsEnabled());
  5259. Assert(!(flags & fFncClassMember));
  5260. this->GetScanner()->Scan();
  5261. }
  5262. if (m_token.IsReservedWord())
  5263. {
  5264. IdentifierExpectedError(m_token);
  5265. }
  5266. CheckStrictModeEvalArgumentsUsage(m_token.GetIdentifier(this->GetHashTbl()));
  5267. }
  5268. // Fast forward to formal parameter list, check for future reserved words,
  5269. // then restore scanner as it was.
  5270. this->GetScanner()->SeekToForcingPid(beginFormals);
  5271. CheckStrictFormalParameters();
  5272. this->GetScanner()->SeekTo(afterFnc);
  5273. }
  5274. if (buildAST)
  5275. {
  5276. if (pnodeFnc->pnodeName != nullptr)
  5277. {
  5278. Assert(pnodeFnc->pnodeName->nop == knopVarDecl);
  5279. CheckStrictModeEvalArgumentsUsage(pnodeFnc->pnodeName->pid, pnodeFnc->pnodeName);
  5280. }
  5281. }
  5282. this->m_fUseStrictMode = oldStrictMode;
  5283. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, StrictModeFunction, m_scriptContext);
  5284. }
  5285. ProcessCapturedNames(pnodeFnc);
  5286. if (fDeferred)
  5287. {
  5288. AnalysisAssert(pnodeFnc);
  5289. pnodeFnc->pnodeVars = nullptr;
  5290. }
  5291. #if ENABLE_BACKGROUND_PARSING
  5292. if (parallelJobStarted)
  5293. {
  5294. pnodeFnc = pnodeRealFnc;
  5295. m_currentNodeFunc = pnodeRealFnc;
  5296. // Let the foreground thread take care of marking the limit on the function node,
  5297. // because in some cases this function's caller will want to change that limit,
  5298. // so we don't want the background thread to try and touch it.
  5299. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  5300. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  5301. }
  5302. #endif
  5303. }
  5304. // after parsing asm.js module, we want to reset asm.js state before continuing
  5305. AnalysisAssert(pnodeFnc);
  5306. if (pnodeFnc->GetAsmjsMode())
  5307. {
  5308. m_InAsmMode = false;
  5309. }
  5310. // Restore the statement stack.
  5311. Assert(nullptr == m_pstmtCur);
  5312. SetCurrentStatement(pstmtSave);
  5313. if (pnodeFncExprScope)
  5314. {
  5315. FinishParseFncExprScope(pnodeFnc, pnodeFncExprScope);
  5316. }
  5317. m_grfscr |= uCanDeferSave;
  5318. if (!m_stoppedDeferredParse)
  5319. {
  5320. m_grfscr |= uDeferSave;
  5321. }
  5322. this->GetScanner()->SetYieldIsKeywordRegion(fPreviousYieldIsKeyword);
  5323. this->GetScanner()->SetAwaitIsKeywordRegion(fPreviousAwaitIsKeyword);
  5324. // Restore the current function.
  5325. if (buildAST)
  5326. {
  5327. Assert(pnodeFnc == m_currentNodeFunc);
  5328. m_currentNodeFunc = pnodeFncSave;
  5329. m_pCurrentAstSize = pAstSizeSave;
  5330. if (!fLambda)
  5331. {
  5332. Assert(pnodeFnc == m_currentNodeNonLambdaFunc);
  5333. m_currentNodeNonLambdaFunc = pnodeFncSaveNonLambda;
  5334. }
  5335. }
  5336. else
  5337. {
  5338. Assert(pnodeFnc == m_currentNodeDeferredFunc);
  5339. if (!fLambda)
  5340. {
  5341. Assert(pnodeFnc == m_currentNodeNonLambdaDeferredFunc);
  5342. m_currentNodeNonLambdaDeferredFunc = pnodeFncSaveNonLambda;
  5343. }
  5344. m_currentNodeDeferredFunc = pnodeFncSave;
  5345. }
  5346. if (m_currentNodeFunc && pnodeFnc->HasWithStmt())
  5347. {
  5348. GetCurrentFunctionNode()->SetHasWithStmt(true);
  5349. }
  5350. }
  5351. template<bool buildAST>
  5352. void Parser::UpdateCurrentNodeFunc(ParseNodeFnc * pnodeFnc, bool fLambda)
  5353. {
  5354. if (buildAST)
  5355. {
  5356. // Make this the current function and start its sub-function list.
  5357. m_currentNodeFunc = pnodeFnc;
  5358. Assert(m_currentNodeDeferredFunc == nullptr);
  5359. if (!fLambda)
  5360. {
  5361. m_currentNodeNonLambdaFunc = pnodeFnc;
  5362. }
  5363. }
  5364. else // if !buildAST
  5365. {
  5366. AnalysisAssert(pnodeFnc);
  5367. if (!fLambda)
  5368. {
  5369. m_currentNodeNonLambdaDeferredFunc = pnodeFnc;
  5370. }
  5371. m_currentNodeDeferredFunc = pnodeFnc;
  5372. }
  5373. }
  5374. void Parser::ParseTopLevelDeferredFunc(ParseNodeFnc * pnodeFnc, ParseNodeFnc * pnodeFncParent, LPCOLESTR pNameHint, bool fLambda, bool *pNeedScanRCurly, bool fAllowIn)
  5375. {
  5376. // Parse a function body that is a transition point from building AST to doing fast syntax check.
  5377. pnodeFnc->pnodeVars = nullptr;
  5378. pnodeFnc->pnodeBody = nullptr;
  5379. this->m_deferringAST = TRUE;
  5380. // Put the scanner into "no hashing" mode.
  5381. BYTE deferFlags = this->GetScanner()->SetDeferredParse(TRUE);
  5382. if (!fLambda)
  5383. {
  5384. ChkCurTok(tkLCurly, ERRnoLcurly);
  5385. }
  5386. else
  5387. {
  5388. // Lambda may consist of a single expression instead of a block
  5389. if (this->GetScanner()->m_ptoken->tk == tkLCurly)
  5390. {
  5391. this->GetScanner()->Scan();
  5392. }
  5393. else
  5394. {
  5395. *pNeedScanRCurly = false;
  5396. }
  5397. }
  5398. ParseNodePtr *ppnodeVarSave = m_ppnodeVar;
  5399. m_ppnodeVar = &pnodeFnc->pnodeVars;
  5400. // Don't try and skip scanning nested deferred lambdas which have only a single expression in the body.
  5401. // Their more-complicated text extents won't match the deferred-stub and the single expression should be fast to scan, anyway.
  5402. if (fLambda && !*pNeedScanRCurly)
  5403. {
  5404. ParseExpressionLambdaBody<false>(pnodeFnc, fAllowIn);
  5405. }
  5406. else if (pnodeFncParent != nullptr && m_currDeferredStub != nullptr)
  5407. {
  5408. // We've already parsed this function body for syntax errors on the initial parse of the script.
  5409. // We have information that allows us to skip it, so do so.
  5410. Assert(pnodeFncParent->nestedCount != 0);
  5411. DeferredFunctionStub *stub = m_currDeferredStub + (pnodeFncParent->nestedCount - 1);
  5412. Assert(pnodeFnc->ichMin == stub->ichMin
  5413. || (stub->fncFlags & kFunctionIsAsync) == kFunctionIsAsync
  5414. || ((stub->fncFlags & kFunctionIsMethod) == kFunctionIsMethod && (
  5415. (stub->fncFlags & kFunctionIsAccessor) == kFunctionIsAccessor
  5416. || (stub->fncFlags & kFunctionIsGenerator) == kFunctionIsGenerator
  5417. || (stub->fncFlags & kFunctionHasComputedName) == kFunctionHasComputedName
  5418. )));
  5419. if (stub->fncFlags & kFunctionCallsEval)
  5420. {
  5421. this->MarkEvalCaller();
  5422. }
  5423. PHASE_PRINT_TRACE1(
  5424. Js::SkipNestedDeferredPhase,
  5425. _u("Skipping nested deferred function %d. %s: %d...%d\n"),
  5426. pnodeFnc->functionId, GetFunctionName(pnodeFnc, pNameHint), pnodeFnc->ichMin, stub->restorePoint.m_ichMinTok);
  5427. this->GetScanner()->SeekTo(stub->restorePoint, m_nextFunctionId);
  5428. // If we already incremented m_nextFunctionId when we saw some functions in the parameter scope
  5429. // (in default argument assignment, for example), we want to remove the count of those so the
  5430. // function ids following the one we are skipping right now are correct.
  5431. *m_nextFunctionId -= pnodeFnc->nestedCount;
  5432. for (uint i = 0; i < stub->capturedNameCount; i++)
  5433. {
  5434. int stringId = stub->capturedNameSerializedIds[i];
  5435. uint32 stringLength = 0;
  5436. LPCWSTR stringVal = Js::ByteCodeSerializer::DeserializeString(stub, stringId, stringLength);
  5437. OUTPUT_TRACE_DEBUGONLY(Js::SkipNestedDeferredPhase, _u("\tPushing a reference to '%s'\n"), stringVal);
  5438. IdentPtr pid = this->GetHashTbl()->PidHashNameLen(stringVal, stringLength);
  5439. PushPidRef(pid);
  5440. }
  5441. pnodeFnc->nestedCount = stub->nestedCount;
  5442. pnodeFnc->deferredStub = stub->deferredStubs;
  5443. pnodeFnc->fncFlags = (FncFlags)(pnodeFnc->fncFlags | stub->fncFlags);
  5444. }
  5445. else
  5446. {
  5447. ParseStmtList<false>(nullptr, nullptr, SM_DeferredParse, true /* isSourceElementList */);
  5448. }
  5449. if (!fLambda || *pNeedScanRCurly)
  5450. {
  5451. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  5452. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  5453. }
  5454. m_ppnodeVar = ppnodeVarSave;
  5455. // Restore the scanner's default hashing mode.
  5456. // Do this before we consume the next token.
  5457. this->GetScanner()->SetDeferredParseFlags(deferFlags);
  5458. if (*pNeedScanRCurly)
  5459. {
  5460. ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
  5461. }
  5462. #if DBG
  5463. pnodeFnc->deferredParseNextFunctionId = *this->m_nextFunctionId;
  5464. #endif
  5465. this->m_deferringAST = FALSE;
  5466. }
  5467. bool Parser::DoParallelParse(ParseNodeFnc * pnodeFnc) const
  5468. {
  5469. #if ENABLE_BACKGROUND_PARSING
  5470. if (!PHASE_ENABLED_RAW(ParallelParsePhase, m_sourceContextInfo->sourceContextId, pnodeFnc->functionId))
  5471. {
  5472. return false;
  5473. }
  5474. BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
  5475. return bgp != nullptr;
  5476. #else
  5477. return false;
  5478. #endif
  5479. }
  5480. bool Parser::ScanAheadToFunctionEnd(uint count)
  5481. {
  5482. bool found = false;
  5483. uint curlyDepth = 0;
  5484. RestorePoint funcStart;
  5485. this->GetScanner()->Capture(&funcStart);
  5486. for (uint i = 0; i < count; i++)
  5487. {
  5488. switch (m_token.tk)
  5489. {
  5490. case tkStrTmplBegin:
  5491. case tkStrTmplMid:
  5492. case tkStrTmplEnd:
  5493. case tkDiv:
  5494. case tkAsgDiv:
  5495. case tkScanError:
  5496. case tkEOF:
  5497. goto LEnd;
  5498. case tkLCurly:
  5499. UInt32Math::Inc(curlyDepth, Parser::OutOfMemory);
  5500. break;
  5501. case tkRCurly:
  5502. if (curlyDepth == 1)
  5503. {
  5504. found = true;
  5505. goto LEnd;
  5506. }
  5507. if (curlyDepth == 0)
  5508. {
  5509. goto LEnd;
  5510. }
  5511. curlyDepth--;
  5512. break;
  5513. }
  5514. this->GetScanner()->ScanAhead();
  5515. }
  5516. LEnd:
  5517. this->GetScanner()->SeekTo(funcStart);
  5518. return found;
  5519. }
  5520. #if ENABLE_BACKGROUND_PARSING
  5521. bool Parser::FastScanFormalsAndBody()
  5522. {
  5523. // The scanner is currently pointing just past the name of a function.
  5524. // The idea here is to find the end of the function body as quickly as possible,
  5525. // by tokenizing and tracking {}'s if possible.
  5526. // String templates require some extra logic but can be handled.
  5527. // The real wrinkle is "/" and "/=", which may indicate either a RegExp literal or a division, depending
  5528. // on the context.
  5529. // To handle this with minimal work, keep track of the last ";" seen at each {} depth. If we see one of the
  5530. // difficult tokens, rewind to the last ";" at the current {} depth and parse statements until we pass the
  5531. // point where we had to rewind. This will process the "/" as required.
  5532. RestorePoint funcStart;
  5533. this->GetScanner()->Capture(&funcStart);
  5534. const int maxRestorePointDepth = 16;
  5535. struct FastScanRestorePoint
  5536. {
  5537. RestorePoint restorePoint;
  5538. uint parenDepth;
  5539. Js::LocalFunctionId functionId;
  5540. int blockId;
  5541. FastScanRestorePoint() : restorePoint(), parenDepth(0) {};
  5542. };
  5543. FastScanRestorePoint lastSColonAtCurlyDepth[maxRestorePointDepth];
  5544. charcount_t ichStart = this->GetScanner()->IchMinTok();
  5545. uint blockIdSave = m_nextBlockId;
  5546. uint functionIdSave = *m_nextFunctionId;
  5547. uint curlyDepth = 0;
  5548. uint strTmplDepth = 0;
  5549. for (;;)
  5550. {
  5551. switch (m_token.tk)
  5552. {
  5553. case tkStrTmplBegin:
  5554. UInt32Math::Inc(strTmplDepth, Parser::OutOfMemory);
  5555. // Fall through
  5556. case tkStrTmplMid:
  5557. case tkLCurly:
  5558. UInt32Math::Inc(curlyDepth, Parser::OutOfMemory);
  5559. Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
  5560. break;
  5561. case tkStrTmplEnd:
  5562. // We can assert here, because the scanner will only return this token if we've told it we're
  5563. // in a string template.
  5564. Assert(strTmplDepth > 0);
  5565. strTmplDepth--;
  5566. break;
  5567. case tkRCurly:
  5568. if (curlyDepth == 1)
  5569. {
  5570. Assert(strTmplDepth == 0);
  5571. if (PHASE_TRACE1(Js::ParallelParsePhase))
  5572. {
  5573. Output::Print(_u("Finished fast seek: %d. %s -- %d...%d\n"),
  5574. m_currentNodeFunc->functionId,
  5575. GetFunctionName(m_currentNodeFunc, m_currentNodeFunc->hint),
  5576. ichStart, this->GetScanner()->IchLimTok());
  5577. }
  5578. return true;
  5579. }
  5580. if (curlyDepth < maxRestorePointDepth)
  5581. {
  5582. lastSColonAtCurlyDepth[curlyDepth].restorePoint.m_ichMinTok = (uint)-1;
  5583. }
  5584. curlyDepth--;
  5585. if (strTmplDepth > 0)
  5586. {
  5587. this->GetScanner()->SetScanState(Scanner_t::ScanState::ScanStateStringTemplateMiddleOrEnd);
  5588. }
  5589. break;
  5590. case tkSColon:
  5591. // Track the location of the ";" (if it's outside parens, as we don't, for instance, want
  5592. // to track the ";"'s in a for-loop header. If we find it's important to rewind within a paren
  5593. // expression, we can do something more sophisticated.)
  5594. if (curlyDepth < maxRestorePointDepth && lastSColonAtCurlyDepth[curlyDepth].parenDepth == 0)
  5595. {
  5596. this->GetScanner()->Capture(&lastSColonAtCurlyDepth[curlyDepth].restorePoint);
  5597. lastSColonAtCurlyDepth[curlyDepth].functionId = *this->m_nextFunctionId;
  5598. lastSColonAtCurlyDepth[curlyDepth].blockId = m_nextBlockId;
  5599. }
  5600. break;
  5601. case tkLParen:
  5602. if (curlyDepth < maxRestorePointDepth)
  5603. {
  5604. UInt32Math::Inc(lastSColonAtCurlyDepth[curlyDepth].parenDepth);
  5605. }
  5606. break;
  5607. case tkRParen:
  5608. if (curlyDepth < maxRestorePointDepth)
  5609. {
  5610. Assert(lastSColonAtCurlyDepth[curlyDepth].parenDepth != 0);
  5611. lastSColonAtCurlyDepth[curlyDepth].parenDepth--;
  5612. }
  5613. break;
  5614. case tkID:
  5615. {
  5616. charcount_t tokLength = this->GetScanner()->IchLimTok() - this->GetScanner()->IchMinTok();
  5617. // Detect the function and class keywords so we can track function ID's.
  5618. // (In fast mode, the scanner doesn't distinguish keywords and doesn't point the token
  5619. // to a PID.)
  5620. // Detect try/catch/for to increment block count for them.
  5621. switch (tokLength)
  5622. {
  5623. case 3:
  5624. if (!memcmp(this->GetScanner()->PchMinTok(), "try", 3) || !memcmp(this->GetScanner()->PchMinTok(), "for", 3))
  5625. {
  5626. Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
  5627. }
  5628. break;
  5629. case 5:
  5630. if (!memcmp(this->GetScanner()->PchMinTok(), "catch", 5))
  5631. {
  5632. Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
  5633. }
  5634. else if (!memcmp(this->GetScanner()->PchMinTok(), "class", 5))
  5635. {
  5636. Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
  5637. Int32Math::Inc(*this->m_nextFunctionId, (int*)this->m_nextFunctionId);
  5638. }
  5639. break;
  5640. case 8:
  5641. if (!memcmp(this->GetScanner()->PchMinTok(), "function", 8))
  5642. {
  5643. // Account for the possible func expr scope or dummy block for missing {}'s around a declaration
  5644. Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
  5645. Int32Math::Inc(*this->m_nextFunctionId, (int*)this->m_nextFunctionId);
  5646. }
  5647. break;
  5648. }
  5649. break;
  5650. }
  5651. case tkDArrow:
  5652. Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
  5653. Int32Math::Inc(*this->m_nextFunctionId, (int*)this->m_nextFunctionId);
  5654. break;
  5655. case tkDiv:
  5656. case tkAsgDiv:
  5657. {
  5658. int opl;
  5659. OpCode nop;
  5660. tokens tkPrev = this->GetScanner()->m_tkPrevious;
  5661. if ((this->GetHashTbl()->TokIsBinop(tkPrev, &opl, &nop) && nop != knopNone) ||
  5662. (this->GetHashTbl()->TokIsUnop(tkPrev, &opl, &nop) &&
  5663. nop != knopNone &&
  5664. tkPrev != tkInc &&
  5665. tkPrev != tkDec) ||
  5666. tkPrev == tkColon ||
  5667. tkPrev == tkLParen ||
  5668. tkPrev == tkLBrack ||
  5669. tkPrev == tkRETURN)
  5670. {
  5671. // Previous token indicates that we're starting an expression here and can't have a
  5672. // binary operator now.
  5673. // Assume this is a RegExp.
  5674. ParseRegExp<false>();
  5675. break;
  5676. }
  5677. uint tempCurlyDepth = curlyDepth < maxRestorePointDepth ? curlyDepth : maxRestorePointDepth - 1;
  5678. for (; tempCurlyDepth != (uint)-1; tempCurlyDepth--)
  5679. {
  5680. // We don't know whether we've got a RegExp or a divide. Rewind to the last safe ";"
  5681. // if we can and parse statements until we pass this point.
  5682. if (lastSColonAtCurlyDepth[tempCurlyDepth].restorePoint.m_ichMinTok != -1)
  5683. {
  5684. break;
  5685. }
  5686. }
  5687. if (tempCurlyDepth != (uint)-1)
  5688. {
  5689. ParseNodeFnc * pnodeFncSave = m_currentNodeFunc;
  5690. int32 *pastSizeSave = m_pCurrentAstSize;
  5691. uint *pnestedCountSave = m_pnestedCount;
  5692. ParseNodePtr *ppnodeScopeSave = m_ppnodeScope;
  5693. ParseNodePtr *ppnodeExprScopeSave = m_ppnodeExprScope;
  5694. ParseNodeFnc * pnodeFnc = CreateDummyFuncNode(true);
  5695. charcount_t ichStop = this->GetScanner()->IchLimTok();
  5696. curlyDepth = tempCurlyDepth;
  5697. this->GetScanner()->SeekTo(lastSColonAtCurlyDepth[tempCurlyDepth].restorePoint);
  5698. m_nextBlockId = lastSColonAtCurlyDepth[tempCurlyDepth].blockId;
  5699. *this->m_nextFunctionId = lastSColonAtCurlyDepth[tempCurlyDepth].functionId;
  5700. ParseNodeBlock * pnodeBlock = StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
  5701. pnodeFnc->pnodeScopes = pnodeBlock;
  5702. m_ppnodeScope = &pnodeBlock->pnodeScopes;
  5703. m_ppnodeExprScope = nullptr;
  5704. this->GetScanner()->Scan();
  5705. do
  5706. {
  5707. ParseStatement<false>();
  5708. } while (this->GetScanner()->IchMinTok() < ichStop);
  5709. FinishParseBlock(pnodeBlock);
  5710. m_currentNodeFunc = pnodeFncSave;
  5711. m_pCurrentAstSize = pastSizeSave;
  5712. m_pnestedCount = pnestedCountSave;
  5713. m_ppnodeScope = ppnodeScopeSave;
  5714. m_ppnodeExprScope = ppnodeExprScopeSave;
  5715. // We've already consumed the first token of the next statement, so just continue
  5716. // without a further scan.
  5717. continue;
  5718. }
  5719. }
  5720. // fall through to rewind to function start
  5721. case tkScanError:
  5722. case tkEOF:
  5723. // Unexpected token.
  5724. if (PHASE_TRACE1(Js::ParallelParsePhase))
  5725. {
  5726. Output::Print(_u("Failed fast seek: %d. %s -- %d...%d\n"),
  5727. m_currentNodeFunc->functionId,
  5728. GetFunctionName(m_currentNodeFunc, m_currentNodeFunc->hint),
  5729. ichStart, this->GetScanner()->IchLimTok());
  5730. }
  5731. m_nextBlockId = blockIdSave;
  5732. *m_nextFunctionId = functionIdSave;
  5733. this->GetScanner()->SeekTo(funcStart);
  5734. return false;
  5735. }
  5736. this->GetScanner()->ScanNoKeywords();
  5737. }
  5738. }
  5739. #endif
  5740. ParseNodeFnc * Parser::CreateDummyFuncNode(bool fDeclaration)
  5741. {
  5742. // Create a dummy node and make it look like the current function declaration.
  5743. // Do this in situations where we want to parse statements without impacting
  5744. // the state of the "real" AST.
  5745. ParseNodeFnc * pnodeFnc = CreateAllowDeferNodeForOpT<knopFncDecl>();
  5746. pnodeFnc->SetDeclaration(fDeclaration);
  5747. pnodeFnc->SetNested(m_currentNodeFunc != nullptr); // If there is a current function, then we're a nested function.
  5748. pnodeFnc->SetStrictMode(IsStrictMode()); // Inherit current strict mode -- may be overridden by the function itself if it contains a strict mode directive.
  5749. m_pCurrentAstSize = &pnodeFnc->astSize;
  5750. m_currentNodeFunc = pnodeFnc;
  5751. m_pnestedCount = &pnodeFnc->nestedCount;
  5752. return pnodeFnc;
  5753. }
  5754. void Parser::ParseNestedDeferredFunc(ParseNodeFnc * pnodeFnc, bool fLambda, bool *pNeedScanRCurly, bool *pStrictModeTurnedOn, bool fAllowIn)
  5755. {
  5756. // Parse a function nested inside another deferred function.
  5757. size_t lengthBeforeBody = this->GetSourceLength();
  5758. if (m_token.tk != tkLCurly && fLambda)
  5759. {
  5760. ParseExpressionLambdaBody<false>(pnodeFnc, fAllowIn);
  5761. *pNeedScanRCurly = false;
  5762. }
  5763. else
  5764. {
  5765. ChkCurTok(tkLCurly, ERRnoLcurly);
  5766. bool* detectStrictModeOn = IsStrictMode() ? nullptr : pStrictModeTurnedOn;
  5767. m_ppnodeVar = &m_currentNodeDeferredFunc->pnodeVars;
  5768. ParseStmtList<false>(nullptr, nullptr, SM_DeferredParse, true /* isSourceElementList */, detectStrictModeOn);
  5769. ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
  5770. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  5771. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  5772. }
  5773. if (*pStrictModeTurnedOn)
  5774. {
  5775. pnodeFnc->SetStrictMode(true);
  5776. }
  5777. if (!PHASE_OFF1(Js::SkipNestedDeferredPhase))
  5778. {
  5779. // Record the end of the function and the function ID increment that happens inside the function.
  5780. // Byte code gen will use this to build stub information to allow us to skip this function when the
  5781. // enclosing function is fully parsed.
  5782. RestorePoint *restorePoint = Anew(&m_nodeAllocator, RestorePoint);
  5783. this->GetScanner()->Capture(restorePoint,
  5784. *m_nextFunctionId - pnodeFnc->functionId - 1,
  5785. lengthBeforeBody - this->GetSourceLength());
  5786. pnodeFnc->pRestorePoint = restorePoint;
  5787. }
  5788. }
  5789. template<bool buildAST>
  5790. void Parser::ParseFncName(ParseNodeFnc * pnodeFnc, ushort flags, IdentPtr* pFncNamePid)
  5791. {
  5792. Assert(pnodeFnc);
  5793. BOOL fDeclaration = flags & fFncDeclaration;
  5794. BOOL fIsAsync = flags & fFncAsync;
  5795. this->GetScanner()->Scan();
  5796. // If generators are enabled then we are in a recent enough version
  5797. // that deferred parsing will create a parse node for pnodeFnc and
  5798. // it is safe to assume it is not null.
  5799. if (flags & fFncGenerator)
  5800. {
  5801. Assert(m_scriptContext->GetConfig()->IsES6GeneratorsEnabled());
  5802. pnodeFnc->SetIsGenerator();
  5803. }
  5804. else if (m_scriptContext->GetConfig()->IsES6GeneratorsEnabled() &&
  5805. m_token.tk == tkStar &&
  5806. !(flags & fFncClassMember))
  5807. {
  5808. if (!fDeclaration)
  5809. {
  5810. bool fPreviousYieldIsKeyword = this->GetScanner()->SetYieldIsKeywordRegion(!fDeclaration);
  5811. this->GetScanner()->Scan();
  5812. this->GetScanner()->SetYieldIsKeywordRegion(fPreviousYieldIsKeyword);
  5813. }
  5814. else
  5815. {
  5816. this->GetScanner()->Scan();
  5817. }
  5818. pnodeFnc->SetIsGenerator();
  5819. }
  5820. if (fIsAsync)
  5821. {
  5822. if (pnodeFnc->IsGenerator())
  5823. {
  5824. if (!m_scriptContext->GetConfig()->IsES2018AsyncIterationEnabled())
  5825. {
  5826. Error(ERRExperimental);
  5827. }
  5828. }
  5829. pnodeFnc->SetIsAsync();
  5830. }
  5831. pnodeFnc->pnodeName = nullptr;
  5832. if ((m_token.tk != tkID || flags & fFncNoName)
  5833. && (IsStrictMode() || fDeclaration
  5834. || pnodeFnc->IsGenerator() || pnodeFnc->IsAsync()
  5835. || (m_token.tk != tkYIELD && m_token.tk != tkAWAIT))) // Function expressions can have the name yield/await even inside generator/async functions
  5836. {
  5837. if (fDeclaration ||
  5838. m_token.IsReservedWord()) // For example: var x = (function break(){});
  5839. {
  5840. IdentifierExpectedError(m_token);
  5841. }
  5842. return;
  5843. }
  5844. Assert(m_token.tk == tkID || (m_token.tk == tkYIELD && !fDeclaration) || (m_token.tk == tkAWAIT && !fDeclaration));
  5845. if (IsStrictMode())
  5846. {
  5847. CheckStrictModeEvalArgumentsUsage(m_token.GetIdentifier(this->GetHashTbl()));
  5848. }
  5849. IdentPtr pidBase = m_token.GetIdentifier(this->GetHashTbl());
  5850. pnodeFnc->pnodeName = CreateDeclNode(knopVarDecl, pidBase, STFunction);
  5851. pnodeFnc->pid = pnodeFnc->pnodeName->pid;
  5852. if (pFncNamePid != nullptr)
  5853. {
  5854. *pFncNamePid = pidBase;
  5855. }
  5856. this->GetScanner()->Scan();
  5857. }
  5858. void Parser::ValidateFormals()
  5859. {
  5860. ParseFncFormals<false>(this->GetCurrentFunctionNode(), nullptr, fFncNoFlgs);
  5861. // Eat the tkRParen. The ParseFncDeclHelper caller expects to see it.
  5862. this->GetScanner()->Scan();
  5863. }
  5864. void Parser::ValidateSourceElementList()
  5865. {
  5866. ParseStmtList<false>(nullptr, nullptr, SM_NotUsed, true);
  5867. }
  5868. void Parser::UpdateOrCheckForDuplicateInFormals(IdentPtr pid, SList<IdentPtr> *formals)
  5869. {
  5870. bool isStrictMode = IsStrictMode();
  5871. if (isStrictMode)
  5872. {
  5873. CheckStrictModeEvalArgumentsUsage(pid);
  5874. }
  5875. if (formals->Has(pid))
  5876. {
  5877. if (isStrictMode)
  5878. {
  5879. Error(ERRES5ArgSame);
  5880. }
  5881. else
  5882. {
  5883. Error(ERRFormalSame);
  5884. }
  5885. }
  5886. else
  5887. {
  5888. formals->Prepend(pid);
  5889. }
  5890. }
  5891. template<bool buildAST>
  5892. void Parser::ParseFncFormals(ParseNodeFnc * pnodeFnc, ParseNodeFnc * pnodeParentFnc, ushort flags, bool isTopLevelDeferredFunc)
  5893. {
  5894. bool fLambda = (flags & fFncLambda) != 0;
  5895. bool fMethod = (flags & fFncMethod) != 0;
  5896. bool fNoArg = (flags & fFncNoArg) != 0;
  5897. bool fOneArg = (flags & fFncOneArg) != 0;
  5898. bool fAsync = (flags & fFncAsync) != 0;
  5899. bool fPreviousYieldIsKeyword = false;
  5900. bool fPreviousAwaitIsKeyword = false;
  5901. if (fLambda)
  5902. {
  5903. fPreviousYieldIsKeyword = this->GetScanner()->SetYieldIsKeywordRegion(pnodeParentFnc != nullptr && pnodeParentFnc->IsGenerator());
  5904. fPreviousAwaitIsKeyword = this->GetScanner()->SetAwaitIsKeywordRegion(fAsync || (pnodeParentFnc != nullptr && pnodeParentFnc->IsAsync()));
  5905. }
  5906. Assert(!fNoArg || !fOneArg); // fNoArg and fOneArg can never be true at the same time.
  5907. // strictFormals corresponds to the StrictFormalParameters grammar production
  5908. // in the ES spec which just means duplicate names are not allowed
  5909. bool fStrictFormals = IsStrictMode() || fLambda || fMethod;
  5910. // When detecting duplicated formals pids are needed so force PID creation (unless the function should take 0 or 1 arg).
  5911. bool forcePid = fStrictFormals && !fNoArg && !fOneArg;
  5912. AutoTempForcePid autoForcePid(this->GetScanner(), forcePid);
  5913. // Lambda's allow single formal specified by a single binding identifier without parentheses, special case it.
  5914. if (fLambda && m_token.tk == tkID)
  5915. {
  5916. IdentPtr pid = m_token.GetIdentifier(this->GetHashTbl());
  5917. CreateVarDeclNode(pid, STFormal, false, nullptr, false);
  5918. CheckPidIsValid(pid);
  5919. this->GetScanner()->Scan();
  5920. if (m_token.tk != tkDArrow)
  5921. {
  5922. Error(ERRsyntax, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok());
  5923. }
  5924. if (fLambda)
  5925. {
  5926. this->GetScanner()->SetYieldIsKeywordRegion(fPreviousYieldIsKeyword);
  5927. this->GetScanner()->SetAwaitIsKeywordRegion(fPreviousAwaitIsKeyword);
  5928. }
  5929. return;
  5930. }
  5931. else if (fLambda && m_token.tk == tkAWAIT)
  5932. {
  5933. // async await => {}
  5934. IdentifierExpectedError(m_token);
  5935. }
  5936. // Otherwise, must have a parameter list within parens.
  5937. ChkCurTok(tkLParen, ERRnoLparen);
  5938. // Now parse the list of arguments, if present
  5939. if (m_token.tk == tkRParen)
  5940. {
  5941. if (fOneArg)
  5942. {
  5943. Error(ERRSetterMustHaveOneParameter);
  5944. }
  5945. }
  5946. else
  5947. {
  5948. if (fNoArg)
  5949. {
  5950. Error(ERRGetterMustHaveNoParameters);
  5951. }
  5952. SList<IdentPtr> formals(&m_nodeAllocator);
  5953. ParseNodeVar * pnodeT = nullptr;
  5954. bool seenRestParameter = false;
  5955. bool isNonSimpleParameterList = false;
  5956. for (Js::ArgSlot argPos = 0; ; ++argPos)
  5957. {
  5958. bool isBindingPattern = false;
  5959. if (m_scriptContext->GetConfig()->IsES6RestEnabled() && m_token.tk == tkEllipsis)
  5960. {
  5961. if (flags & fFncOneArg)
  5962. {
  5963. // The parameter of a setter cannot be a rest parameter.
  5964. Error(ERRUnexpectedEllipsis);
  5965. }
  5966. // Possible rest parameter
  5967. this->GetScanner()->Scan();
  5968. seenRestParameter = true;
  5969. }
  5970. if (m_token.tk != tkID)
  5971. {
  5972. if (IsPossiblePatternStart())
  5973. {
  5974. // Mark that the function has a non simple parameter list before parsing the pattern since the pattern can have function definitions.
  5975. this->GetCurrentFunctionNode()->SetHasNonSimpleParameterList();
  5976. this->GetCurrentFunctionNode()->SetHasDestructuredParams();
  5977. ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
  5978. m_ppnodeVar = &pnodeFnc->pnodeVars;
  5979. ParseNodePtr * ppNodeLex = m_currentBlockInfo->m_ppnodeLex;
  5980. Assert(ppNodeLex != nullptr);
  5981. ParseNodeParamPattern * paramPattern = nullptr;
  5982. ParseNode * pnodePattern = nullptr;
  5983. if (isTopLevelDeferredFunc)
  5984. {
  5985. pnodePattern = ParseDestructuredLiteral<false>(tkLET, true /*isDecl*/, false /*topLevel*/);
  5986. }
  5987. else
  5988. {
  5989. pnodePattern = ParseDestructuredLiteral<buildAST>(tkLET, true /*isDecl*/, false /*topLevel*/);
  5990. }
  5991. // Instead of passing the STFormal all the way on many methods, it seems it is better to change the symbol type afterward.
  5992. for (ParseNodePtr lexNode = *ppNodeLex; lexNode != nullptr; lexNode = lexNode->AsParseNodeVar()->pnodeNext)
  5993. {
  5994. Assert(lexNode->IsVarLetOrConst());
  5995. UpdateOrCheckForDuplicateInFormals(lexNode->AsParseNodeVar()->pid, &formals);
  5996. lexNode->AsParseNodeVar()->sym->SetSymbolType(STFormal);
  5997. if (lexNode->AsParseNodeVar()->pid == wellKnownPropertyPids.arguments)
  5998. {
  5999. GetCurrentFunctionNode()->grfpn |= PNodeFlags::fpnArguments_overriddenInParam;
  6000. }
  6001. }
  6002. m_ppnodeVar = ppnodeVarSave;
  6003. if (buildAST)
  6004. {
  6005. if (isTopLevelDeferredFunc)
  6006. {
  6007. Assert(pnodePattern == nullptr);
  6008. // Create a dummy pattern node as we need the node to be considered for the param count
  6009. paramPattern = CreateDummyParamPatternNode(this->GetScanner()->IchMinTok());
  6010. }
  6011. else
  6012. {
  6013. Assert(pnodePattern);
  6014. paramPattern = CreateParamPatternNode(pnodePattern);
  6015. }
  6016. if (seenRestParameter)
  6017. {
  6018. Assert(pnodeFnc->pnodeRest == nullptr);
  6019. pnodeFnc->pnodeRest = paramPattern;
  6020. }
  6021. else
  6022. {
  6023. // Linking the current formal parameter (which is pattern parameter)
  6024. // with other formals.
  6025. *m_ppnodeVar = paramPattern;
  6026. paramPattern->pnodeNext = nullptr;
  6027. m_ppnodeVar = &paramPattern->pnodeNext;
  6028. }
  6029. }
  6030. isBindingPattern = true;
  6031. isNonSimpleParameterList = true;
  6032. }
  6033. else
  6034. {
  6035. IdentifierExpectedError(m_token);
  6036. }
  6037. }
  6038. if (!isBindingPattern)
  6039. {
  6040. IdentPtr pid = m_token.GetIdentifier(this->GetHashTbl());
  6041. LPCOLESTR pNameHint = pid->Psz();
  6042. uint32 nameHintLength = pid->Cch();
  6043. uint32 nameHintOffset = 0;
  6044. if (seenRestParameter)
  6045. {
  6046. this->GetCurrentFunctionNode()->SetHasNonSimpleParameterList();
  6047. pnodeT = CreateDeclNode(knopVarDecl, pid, STFormal, false);
  6048. pnodeT->sym->SetIsNonSimpleParameter(true);
  6049. if (buildAST)
  6050. {
  6051. // When only validating formals, we won't have a function node.
  6052. Assert(pnodeFnc->pnodeRest == nullptr);
  6053. pnodeFnc->pnodeRest = pnodeT;
  6054. if (!isNonSimpleParameterList)
  6055. {
  6056. // This is the first non-simple parameter we've seen. We need to go back
  6057. // and set the Symbols of all previous parameters.
  6058. MapFormalsWithoutRest(m_currentNodeFunc, [](ParseNodePtr pnodeArg)
  6059. {
  6060. pnodeArg->AsParseNodeVar()->sym->SetIsNonSimpleParameter(true);
  6061. });
  6062. }
  6063. }
  6064. isNonSimpleParameterList = true;
  6065. }
  6066. else
  6067. {
  6068. pnodeT = CreateVarDeclNode(pid, STFormal, false, nullptr, false);
  6069. if (isNonSimpleParameterList)
  6070. {
  6071. pnodeT->sym->SetIsNonSimpleParameter(true);
  6072. }
  6073. }
  6074. if (buildAST && pid == wellKnownPropertyPids.arguments)
  6075. {
  6076. // This formal parameter overrides the built-in 'arguments' object
  6077. m_currentNodeFunc->grfpn |= PNodeFlags::fpnArguments_overriddenInParam;
  6078. }
  6079. if (fStrictFormals)
  6080. {
  6081. UpdateOrCheckForDuplicateInFormals(pid, &formals);
  6082. }
  6083. this->GetScanner()->Scan();
  6084. if (m_token.tk == tkAsg)
  6085. {
  6086. if (seenRestParameter && m_scriptContext->GetConfig()->IsES6RestEnabled())
  6087. {
  6088. Error(ERRRestWithDefault);
  6089. }
  6090. // In defer parse mode we have to flag the function node to indicate that it has default arguments
  6091. // so that it will be considered for any syntax error scenario.
  6092. // Also mark it before parsing the expression as it may contain functions.
  6093. ParseNodeFnc * currentFncNode = GetCurrentFunctionNode();
  6094. if (!currentFncNode->HasDefaultArguments())
  6095. {
  6096. currentFncNode->SetHasDefaultArguments();
  6097. currentFncNode->SetHasNonSimpleParameterList();
  6098. currentFncNode->firstDefaultArg = argPos;
  6099. }
  6100. this->GetScanner()->Scan();
  6101. ParseNodePtr pnodeInit;
  6102. if (isTopLevelDeferredFunc)
  6103. {
  6104. // Defer default expressions if the function will be deferred, since we know they can't be evaluated
  6105. // until the function is fully compiled, and generating code for a function nested inside a deferred function
  6106. // creates inconsistencies.
  6107. pnodeInit = ParseExpr<false>(koplCma, nullptr, TRUE, FALSE, pNameHint, &nameHintLength, &nameHintOffset);
  6108. }
  6109. else
  6110. {
  6111. pnodeInit = ParseExpr<buildAST>(koplCma, nullptr, TRUE, FALSE, pNameHint, &nameHintLength, &nameHintOffset);
  6112. }
  6113. if (buildAST && pnodeInit && pnodeInit->nop == knopFncDecl)
  6114. {
  6115. Assert(nameHintLength >= nameHintOffset);
  6116. ParseNodeFnc * pnodeFncInit = pnodeInit->AsParseNodeFnc();
  6117. pnodeFncInit->hint = pNameHint;
  6118. pnodeFncInit->hintLength = nameHintLength;
  6119. pnodeFncInit->hintOffset = nameHintOffset;
  6120. }
  6121. AnalysisAssert(pnodeT);
  6122. pnodeT->sym->SetIsNonSimpleParameter(true);
  6123. if (!isNonSimpleParameterList)
  6124. {
  6125. if (buildAST)
  6126. {
  6127. // This is the first non-simple parameter we've seen. We need to go back
  6128. // and set the Symbols of all previous parameters.
  6129. MapFormalsWithoutRest(m_currentNodeFunc, [&](ParseNodePtr pnodeArg) { pnodeArg->AsParseNodeVar()->sym->SetIsNonSimpleParameter(true); });
  6130. }
  6131. // There may be previous parameters that need to be checked for duplicates.
  6132. isNonSimpleParameterList = true;
  6133. }
  6134. if (buildAST)
  6135. {
  6136. if (!m_currentNodeFunc->HasDefaultArguments())
  6137. {
  6138. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, DefaultArgFunction, m_scriptContext);
  6139. }
  6140. pnodeT->pnodeInit = pnodeInit;
  6141. pnodeT->ichLim = this->GetScanner()->IchLimTok();
  6142. }
  6143. }
  6144. }
  6145. if (isNonSimpleParameterList && m_currentScope->GetHasDuplicateFormals())
  6146. {
  6147. Error(ERRFormalSame);
  6148. }
  6149. if (flags & fFncOneArg)
  6150. {
  6151. if (m_token.tk != tkRParen)
  6152. {
  6153. Error(ERRSetterMustHaveOneParameter);
  6154. }
  6155. break; //enforce only one arg
  6156. }
  6157. if (m_token.tk != tkComma)
  6158. {
  6159. break;
  6160. }
  6161. this->GetScanner()->Scan();
  6162. if (seenRestParameter)
  6163. {
  6164. Error(ERRRestLastArg);
  6165. }
  6166. if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
  6167. {
  6168. break;
  6169. }
  6170. }
  6171. if (seenRestParameter)
  6172. {
  6173. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Rest, m_scriptContext);
  6174. }
  6175. if (m_token.tk != tkRParen)
  6176. {
  6177. Error(ERRnoRparen);
  6178. }
  6179. if (this->GetCurrentFunctionNode()->CallsEval() || this->GetCurrentFunctionNode()->ChildCallsEval())
  6180. {
  6181. Assert(pnodeFnc->HasNonSimpleParameterList());
  6182. pnodeFnc->ResetBodyAndParamScopeMerged();
  6183. }
  6184. }
  6185. Assert(m_token.tk == tkRParen);
  6186. if (fLambda)
  6187. {
  6188. this->GetScanner()->SetYieldIsKeywordRegion(fPreviousYieldIsKeyword);
  6189. this->GetScanner()->SetAwaitIsKeywordRegion(fPreviousAwaitIsKeyword);
  6190. }
  6191. }
  6192. template<bool buildAST>
  6193. ParseNodePtr Parser::GenerateModuleFunctionWrapper()
  6194. {
  6195. ParseNodePtr pnodeFnc = ParseFncDeclNoCheckScope<buildAST>(fFncModule, SuperRestrictionState::Disallowed, nullptr, /* needsPIDOnRCurlyScan */ false, /* fUnaryOrParen */ true);
  6196. // mark modules as generators after parsing - this is to enable cross-module hoisting of exported functions
  6197. pnodeFnc->AsParseNodeFnc()->SetIsGenerator(true);
  6198. ParseNodePtr callNode = CreateCallNode(knopCall, pnodeFnc, nullptr);
  6199. return callNode;
  6200. }
  6201. template<bool buildAST>
  6202. ParseNodeFnc * Parser::GenerateEmptyConstructor(bool extends)
  6203. {
  6204. ParseNodeFnc * pnodeFnc;
  6205. // Create the node.
  6206. pnodeFnc = CreateAllowDeferNodeForOpT<knopFncDecl>();
  6207. pnodeFnc->SetNested(NULL != m_currentNodeFunc);
  6208. pnodeFnc->SetStrictMode();
  6209. pnodeFnc->SetIsMethod(TRUE);
  6210. pnodeFnc->SetIsClassMember(TRUE);
  6211. pnodeFnc->SetIsClassConstructor(TRUE);
  6212. pnodeFnc->SetIsBaseClassConstructor(!extends);
  6213. pnodeFnc->SetHasNonThisStmt();
  6214. pnodeFnc->SetIsGeneratedDefault(TRUE);
  6215. pnodeFnc->SetHasHomeObj();
  6216. pnodeFnc->SetHomeObjLocation(Js::Constants::NoRegister);
  6217. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  6218. pnodeFnc->ichMin = this->GetScanner()->IchMinTok();
  6219. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  6220. pnodeFnc->cbMin = this->GetScanner()->IecpMinTok();
  6221. pnodeFnc->cbStringMin = pnodeFnc->cbMin;
  6222. pnodeFnc->cbStringLim = pnodeFnc->cbLim;
  6223. pnodeFnc->lineNumber = this->GetScanner()->LineCur();
  6224. pnodeFnc->functionId = (*m_nextFunctionId);
  6225. // In order to (re-)defer the default constructor, we need to, for instance, track
  6226. // deferred class expression the way we track function expression, since we lose the part of the source
  6227. // that tells us which we have.
  6228. Assert(!pnodeFnc->canBeDeferred);
  6229. #ifdef DBG
  6230. pnodeFnc->deferredParseNextFunctionId = *(this->m_nextFunctionId);
  6231. #endif
  6232. AppendFunctionToScopeList(true, pnodeFnc);
  6233. if (m_nextFunctionId)
  6234. {
  6235. (*m_nextFunctionId)++;
  6236. }
  6237. // Update the count of functions nested in the current parent.
  6238. if (m_pnestedCount)
  6239. {
  6240. (*m_pnestedCount)++;
  6241. }
  6242. if (this->GetScanner()->IchMinTok() >= this->GetScanner()->IchMinLine())
  6243. {
  6244. // In scenarios involving defer parse IchMinLine() can be incorrect for the first line after defer parse
  6245. pnodeFnc->columnNumber = this->GetScanner()->IchMinTok() - this->GetScanner()->IchMinLine();
  6246. }
  6247. else if (m_currentNodeFunc)
  6248. {
  6249. // For the first line after defer parse, compute the column relative to the column number
  6250. // of the lexically parent function.
  6251. ULONG offsetFromCurrentFunction = this->GetScanner()->IchMinTok() - m_currentNodeFunc->ichMin;
  6252. pnodeFnc->columnNumber = m_currentNodeFunc->columnNumber + offsetFromCurrentFunction;
  6253. }
  6254. else
  6255. {
  6256. // if there is no current function, lets give a default of 0.
  6257. pnodeFnc->columnNumber = 0;
  6258. }
  6259. int32 * pAstSizeSave = m_pCurrentAstSize;
  6260. m_pCurrentAstSize = &(pnodeFnc->astSize);
  6261. // Make this the current function.
  6262. ParseNodeFnc * pnodeFncSave = m_currentNodeFunc;
  6263. m_currentNodeFunc = pnodeFnc;
  6264. ParseNodeName * argsId = nullptr;
  6265. ParseNodePtr *lastNodeRef = nullptr;
  6266. ParseNodeBlock * pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Parameter, ScopeType_Parameter);
  6267. if (buildAST && extends)
  6268. {
  6269. // constructor(...args) { super(...args); }
  6270. // ^^^^^^^
  6271. ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
  6272. m_ppnodeVar = &pnodeFnc->pnodeVars;
  6273. IdentPtr pidargs = this->GetHashTbl()->PidHashNameLen(_u("args"), sizeof("args") - 1);
  6274. ParseNodeVar * pnodeT = CreateVarDeclNode(pidargs, STFormal);
  6275. pnodeT->sym->SetIsNonSimpleParameter(true);
  6276. pnodeFnc->pnodeRest = pnodeT;
  6277. PidRefStack *ref = this->PushPidRef(pidargs);
  6278. argsId = CreateNameNode(pidargs, ref, pnodeFnc->ichMin, pnodeFnc->ichLim);
  6279. m_ppnodeVar = ppnodeVarSave;
  6280. }
  6281. ParseNodeBlock * pnodeInnerBlock = StartParseBlock<buildAST>(PnodeBlockType::Function, ScopeType_FunctionBody);
  6282. pnodeBlock->pnodeScopes = pnodeInnerBlock;
  6283. pnodeFnc->pnodeBodyScope = pnodeInnerBlock;
  6284. pnodeFnc->pnodeScopes = pnodeBlock;
  6285. if (buildAST)
  6286. {
  6287. if (extends)
  6288. {
  6289. // constructor(...args) { super(...args); }
  6290. // ^^^^^^^^^^^^^^^
  6291. Assert(argsId);
  6292. ParseNodeUni * spreadArg = CreateUniNode(knopEllipsis, argsId, pnodeFnc->ichMin, pnodeFnc->ichLim);
  6293. ParseNodeSpecialName * superRef = ReferenceSpecialName(wellKnownPropertyPids._superConstructor, pnodeFnc->ichMin, pnodeFnc->ichLim, true);
  6294. pnodeFnc->SetHasSuperReference(TRUE);
  6295. ParseNodeSuperCall * callNode = CreateSuperCallNode(superRef, spreadArg);
  6296. callNode->pnodeThis = ReferenceSpecialName(wellKnownPropertyPids._this, pnodeFnc->ichMin, pnodeFnc->ichLim, true);
  6297. callNode->pnodeNewTarget = ReferenceSpecialName(wellKnownPropertyPids._newTarget, pnodeFnc->ichMin, pnodeFnc->ichLim, true);
  6298. callNode->spreadArgCount = 1;
  6299. AddToNodeList(&pnodeFnc->pnodeBody, &lastNodeRef, callNode);
  6300. }
  6301. AddToNodeList(&pnodeFnc->pnodeBody, &lastNodeRef, CreateNodeForOpT<knopEndCode>());
  6302. }
  6303. FinishParseBlock(pnodeInnerBlock);
  6304. CreateSpecialSymbolDeclarations(pnodeFnc);
  6305. FinishParseBlock(pnodeBlock);
  6306. m_currentNodeFunc = pnodeFncSave;
  6307. m_pCurrentAstSize = pAstSizeSave;
  6308. return pnodeFnc;
  6309. }
  6310. template<bool buildAST>
  6311. void Parser::ParseExpressionLambdaBody(ParseNodeFnc * pnodeLambda, bool fAllowIn)
  6312. {
  6313. ParseNodePtr *lastNodeRef = nullptr;
  6314. // The lambda body is a single expression, the result of which is the return value.
  6315. ParseNodeReturn * pnodeRet = nullptr;
  6316. if (buildAST)
  6317. {
  6318. pnodeRet = CreateNodeForOpT<knopReturn>();
  6319. pnodeRet->grfpn |= PNodeFlags::fpnSyntheticNode;
  6320. pnodeLambda->pnodeScopes->pnodeStmt = pnodeRet;
  6321. }
  6322. IdentToken token;
  6323. charcount_t lastRParen = 0;
  6324. // We need to disable deferred parse mode in the scanner because the lambda body doesn't end with a right paren.
  6325. // The scanner needs to create a pid in the case of a string constant token immediately following the lambda body expression.
  6326. // Otherwise, we'll save null for the string constant pid which will AV during ByteCode generation.
  6327. BYTE fScanDeferredFlagsSave = this->GetScanner()->SetDeferredParse(FALSE);
  6328. ParseNodePtr result = ParseExpr<buildAST>(koplAsg, nullptr, fAllowIn, FALSE, nullptr, nullptr, nullptr, &token, false, nullptr, &lastRParen);
  6329. this->GetScanner()->SetDeferredParseFlags(fScanDeferredFlagsSave);
  6330. this->MarkEscapingRef(result, &token);
  6331. if (buildAST)
  6332. {
  6333. pnodeRet->pnodeExpr = result;
  6334. pnodeRet->ichMin = pnodeRet->pnodeExpr->ichMin;
  6335. pnodeRet->ichLim = pnodeRet->pnodeExpr->ichLim;
  6336. // Pushing a statement node with PushStmt<>() normally does this initialization
  6337. // but do it here manually since we know there is no outer statement node.
  6338. pnodeRet->grfnop = 0;
  6339. pnodeLambda->ichLim = max(pnodeRet->ichLim, lastRParen);
  6340. pnodeLambda->cbLim = this->GetScanner()->IecpLimTokPrevious();
  6341. pnodeLambda->pnodeScopes->ichLim = pnodeRet->ichLim;
  6342. pnodeLambda->pnodeBody = nullptr;
  6343. AddToNodeList(&pnodeLambda->pnodeBody, &lastNodeRef, pnodeRet);
  6344. // Append an EndCode node.
  6345. ParseNodePtr end = CreateNodeForOpT<knopEndCode>(pnodeRet->ichLim);
  6346. end->ichLim = end->ichMin; // make end code zero width at the immediate end of lambda body
  6347. AddToNodeList(&pnodeLambda->pnodeBody, &lastNodeRef, end);
  6348. // Lambda's do not have arguments binding
  6349. pnodeLambda->SetHasReferenceableBuiltInArguments(false);
  6350. }
  6351. else
  6352. {
  6353. pnodeLambda->ichLim = max(this->GetScanner()->IchLimTokPrevious(), lastRParen);
  6354. pnodeLambda->cbLim = this->GetScanner()->IecpLimTokPrevious();
  6355. }
  6356. }
  6357. void Parser::CheckStrictFormalParameters()
  6358. {
  6359. if (m_token.tk == tkID)
  6360. {
  6361. // single parameter arrow function case
  6362. IdentPtr pid = m_token.GetIdentifier(this->GetHashTbl());
  6363. CheckStrictModeEvalArgumentsUsage(pid);
  6364. return;
  6365. }
  6366. Assert(m_token.tk == tkLParen);
  6367. this->GetScanner()->ScanForcingPid();
  6368. if (m_token.tk != tkRParen)
  6369. {
  6370. SList<IdentPtr> formals(&m_nodeAllocator);
  6371. for (;;)
  6372. {
  6373. if (m_token.tk != tkID)
  6374. {
  6375. IdentifierExpectedError(m_token);
  6376. }
  6377. IdentPtr pid = m_token.GetIdentifier(this->GetHashTbl());
  6378. CheckStrictModeEvalArgumentsUsage(pid);
  6379. if (formals.Has(pid))
  6380. {
  6381. Error(ERRES5ArgSame, this->GetScanner()->IchMinTok(), this->GetScanner()->IchLimTok());
  6382. }
  6383. else
  6384. {
  6385. formals.Prepend(pid);
  6386. }
  6387. this->GetScanner()->Scan();
  6388. if (m_token.tk == tkAsg)
  6389. {
  6390. this->GetScanner()->Scan();
  6391. // We can avoid building the AST since we are just checking the default expression.
  6392. ParseNodePtr pnodeInit = ParseExpr<false>(koplCma);
  6393. Assert(pnodeInit == nullptr);
  6394. }
  6395. if (m_token.tk != tkComma)
  6396. {
  6397. break;
  6398. }
  6399. this->GetScanner()->ScanForcingPid();
  6400. if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
  6401. {
  6402. break;
  6403. }
  6404. }
  6405. }
  6406. Assert(m_token.tk == tkRParen);
  6407. }
  6408. void Parser::FinishFncNode(ParseNodeFnc * pnodeFnc, bool fAllowIn)
  6409. {
  6410. AnalysisAssert(pnodeFnc);
  6411. // Finish the AST for a function that was deferred earlier, but which we decided
  6412. // to finish after the fact.
  6413. // We assume that the name(s) and arg(s) have already got parse nodes, so
  6414. // we just have to do the function body.
  6415. // Save the current next function Id, and resume from the old one.
  6416. Js::LocalFunctionId * nextFunctionIdSave = m_nextFunctionId;
  6417. Js::LocalFunctionId tempNextFunctionId = pnodeFnc->functionId + 1;
  6418. this->m_nextFunctionId = &tempNextFunctionId;
  6419. ParseNodeFnc * pnodeFncSave = m_currentNodeFunc;
  6420. uint *pnestedCountSave = m_pnestedCount;
  6421. int32* pAstSizeSave = m_pCurrentAstSize;
  6422. m_currentNodeFunc = pnodeFnc;
  6423. m_pCurrentAstSize = &(pnodeFnc->astSize);
  6424. pnodeFnc->nestedCount = 0;
  6425. m_pnestedCount = &pnodeFnc->nestedCount;
  6426. bool fLambda = pnodeFnc->IsLambda();
  6427. bool fMethod = pnodeFnc->IsMethod();
  6428. // Cue up the parser to the start of the function body.
  6429. if (pnodeFnc->pnodeName)
  6430. {
  6431. // Skip the name(s).
  6432. this->GetScanner()->SetCurrentCharacter(pnodeFnc->pnodeName->ichLim, pnodeFnc->lineNumber);
  6433. }
  6434. else
  6435. {
  6436. this->GetScanner()->SetCurrentCharacter(pnodeFnc->ichMin, pnodeFnc->lineNumber);
  6437. if (fMethod)
  6438. {
  6439. // Method. Skip identifier name, computed property name, "async", "get", "set", and '*' or '(' characters.
  6440. for (;;)
  6441. {
  6442. this->GetScanner()->Scan();
  6443. // '[' character indicates a computed property name for this method. We should consume it.
  6444. if (m_token.tk == tkLBrack)
  6445. {
  6446. // We don't care what the name expr is.
  6447. this->GetScanner()->Scan();
  6448. ParseExpr<false>();
  6449. Assert(m_token.tk == tkRBrack);
  6450. continue;
  6451. }
  6452. // Quit scanning ahead when we reach a '(' character which opens the arg list.
  6453. if (m_token.tk == tkLParen)
  6454. {
  6455. break;
  6456. }
  6457. }
  6458. }
  6459. else if (pnodeFnc->IsAccessor())
  6460. {
  6461. // Getter/setter. The node text starts with the name, so eat that.
  6462. this->GetScanner()->ScanNoKeywords();
  6463. }
  6464. else if (!fLambda)
  6465. {
  6466. // Anonymous function. Skip "async", "function", and '(' or '*' characters.
  6467. for (;;)
  6468. {
  6469. this->GetScanner()->Scan();
  6470. if (CheckContextualKeyword(wellKnownPropertyPids.async))
  6471. {
  6472. Assert(pnodeFnc->IsAsync());
  6473. continue;
  6474. }
  6475. // Quit scanning ahead when we reach a 'function' keyword which precedes the arg list.
  6476. if (m_token.tk == tkFUNCTION)
  6477. {
  6478. break;
  6479. }
  6480. Assert(m_token.tk == tkLParen || m_token.tk == tkStar);
  6481. }
  6482. }
  6483. }
  6484. // switch scanner to treat 'yield' as keyword in generator functions
  6485. // or as an identifier in non-generator functions
  6486. bool fPreviousYieldIsKeyword = this->GetScanner()->SetYieldIsKeywordRegion(pnodeFnc && pnodeFnc->IsGenerator());
  6487. bool fPreviousAwaitIsKeyword = this->GetScanner()->SetAwaitIsKeywordRegion(pnodeFnc && pnodeFnc->IsAsync());
  6488. // Skip the arg list.
  6489. if (!fMethod)
  6490. {
  6491. // If this is a method, we've already advanced to the '(' token.
  6492. this->GetScanner()->Scan();
  6493. }
  6494. if (m_token.tk == tkStar)
  6495. {
  6496. Assert(pnodeFnc->IsGenerator());
  6497. this->GetScanner()->ScanNoKeywords();
  6498. }
  6499. if (fLambda && CheckContextualKeyword(wellKnownPropertyPids.async))
  6500. {
  6501. Assert(pnodeFnc->IsAsync());
  6502. this->GetScanner()->ScanNoKeywords();
  6503. }
  6504. Assert(m_token.tk == tkLParen || (fLambda && m_token.tk == tkID));
  6505. this->GetScanner()->ScanNoKeywords();
  6506. if (m_token.tk != tkRParen && m_token.tk != tkDArrow)
  6507. {
  6508. for (;;)
  6509. {
  6510. if (m_token.tk == tkEllipsis)
  6511. {
  6512. this->GetScanner()->ScanNoKeywords();
  6513. }
  6514. if (m_token.tk == tkID)
  6515. {
  6516. this->GetScanner()->ScanNoKeywords();
  6517. if (m_token.tk == tkAsg)
  6518. {
  6519. // Eat the default expression
  6520. this->GetScanner()->Scan();
  6521. ParseExpr<false>(koplCma);
  6522. }
  6523. }
  6524. else if (IsPossiblePatternStart())
  6525. {
  6526. ParseDestructuredLiteralWithScopeSave(tkLET, false/*isDecl*/, false /*topLevel*/);
  6527. }
  6528. else
  6529. {
  6530. AssertMsg(false, "Unexpected identifier prefix while fast-scanning formals");
  6531. }
  6532. if (m_token.tk != tkComma)
  6533. {
  6534. break;
  6535. }
  6536. this->GetScanner()->ScanNoKeywords();
  6537. if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
  6538. {
  6539. break;
  6540. }
  6541. }
  6542. }
  6543. if (m_token.tk == tkRParen)
  6544. {
  6545. this->GetScanner()->Scan();
  6546. }
  6547. if (fLambda && m_token.tk == tkDArrow)
  6548. {
  6549. this->GetScanner()->Scan();
  6550. }
  6551. // Finish the function body.
  6552. {
  6553. // Note that in IE8- modes, surrounding parentheses are considered part of function body. e.g. "( function x(){} )".
  6554. // We lose that context here since we start from middle of function body. So save and restore source range info.
  6555. const charcount_t ichLim = pnodeFnc->ichLim;
  6556. const size_t cbLim = pnodeFnc->cbLim;
  6557. this->FinishFncDecl(pnodeFnc, NULL, fLambda, /* skipCurlyBraces */ false, fAllowIn);
  6558. #if DBG
  6559. // The pnode extent may not match the original extent.
  6560. // We expect this to happen only when there are trailing ")"'s.
  6561. // Consume them and make sure that's all we've got.
  6562. if (pnodeFnc->ichLim != ichLim)
  6563. {
  6564. Assert(pnodeFnc->ichLim < ichLim);
  6565. this->GetScanner()->SetCurrentCharacter(pnodeFnc->ichLim);
  6566. while (this->GetScanner()->IchLimTok() != ichLim)
  6567. {
  6568. this->GetScanner()->ScanNoKeywords();
  6569. Assert(m_token.tk == tkRParen);
  6570. }
  6571. }
  6572. #endif
  6573. pnodeFnc->ichLim = ichLim;
  6574. pnodeFnc->cbLim = cbLim;
  6575. }
  6576. m_currentNodeFunc = pnodeFncSave;
  6577. m_pCurrentAstSize = pAstSizeSave;
  6578. m_pnestedCount = pnestedCountSave;
  6579. Assert(m_pnestedCount);
  6580. Assert(tempNextFunctionId == pnodeFnc->deferredParseNextFunctionId);
  6581. this->m_nextFunctionId = nextFunctionIdSave;
  6582. this->GetScanner()->SetYieldIsKeywordRegion(fPreviousYieldIsKeyword);
  6583. this->GetScanner()->SetAwaitIsKeywordRegion(fPreviousAwaitIsKeyword);
  6584. }
  6585. void Parser::FinishFncDecl(ParseNodeFnc * pnodeFnc, LPCOLESTR pNameHint, bool fLambda, bool skipCurlyBraces, bool fAllowIn)
  6586. {
  6587. LPCOLESTR name = NULL;
  6588. JS_ETW(int32 startAstSize = *m_pCurrentAstSize);
  6589. if (IS_JS_ETW(EventEnabledJSCRIPT_PARSE_METHOD_START()) || PHASE_TRACE1(Js::DeferParsePhase))
  6590. {
  6591. name = GetFunctionName(pnodeFnc, pNameHint);
  6592. m_functionBody = NULL; // for nested functions we do not want to get the name of the top deferred function return name;
  6593. JS_ETW(EventWriteJSCRIPT_PARSE_METHOD_START(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), pnodeFnc->functionId, 0, m_parseType, name));
  6594. OUTPUT_TRACE(Js::DeferParsePhase, _u("Parsing function (%s) : %s (%d)\n"), GetParseType(), name, pnodeFnc->functionId);
  6595. }
  6596. JS_ETW_INTERNAL(EventWriteJSCRIPT_PARSE_FUNC(GetScriptContext(), pnodeFnc->functionId, /*Undefer*/FALSE));
  6597. // Do the work of creating an AST for a function body.
  6598. // This is common to the un-deferred case and the case in which we un-defer late in the game.
  6599. Assert(pnodeFnc->nop == knopFncDecl);
  6600. if (fLambda && m_token.tk != tkLCurly)
  6601. {
  6602. ParseExpressionLambdaBody<true>(pnodeFnc, fAllowIn);
  6603. }
  6604. else
  6605. {
  6606. if (!skipCurlyBraces)
  6607. {
  6608. ChkCurTok(tkLCurly, ERRnoLcurly);
  6609. }
  6610. ParseNodePtr * lastNodeRef = nullptr;
  6611. ParseStmtList<true>(&pnodeFnc->pnodeBody, &lastNodeRef, SM_OnFunctionCode, true /* isSourceElementList */);
  6612. // Append an EndCode node.
  6613. AddToNodeList(&pnodeFnc->pnodeBody, &lastNodeRef, CreateNodeForOpT<knopEndCode>());
  6614. if (!skipCurlyBraces)
  6615. {
  6616. ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
  6617. }
  6618. pnodeFnc->ichLim = this->GetScanner()->IchLimTok();
  6619. pnodeFnc->cbLim = this->GetScanner()->IecpLimTok();
  6620. }
  6621. #ifdef ENABLE_JS_ETW
  6622. int32 astSize = *m_pCurrentAstSize - startAstSize;
  6623. EventWriteJSCRIPT_PARSE_METHOD_STOP(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), pnodeFnc->functionId, astSize, m_parseType, name);
  6624. #endif
  6625. }
  6626. ParseNodeVar * Parser::CreateSpecialVarDeclNode(ParseNodeFnc * pnodeFnc, IdentPtr pid)
  6627. {
  6628. ParseNodeVar * pnode = InsertVarAtBeginning(pnodeFnc, pid);
  6629. pnode->grfpn |= fpnSpecialSymbol;
  6630. // special symbol must not be global
  6631. pnode->sym->SetIsGlobal(false);
  6632. return pnode;
  6633. }
  6634. ParseNodeVar * Parser::InsertVarAtBeginning(ParseNodeFnc * pnodeFnc, IdentPtr pid)
  6635. {
  6636. ParseNodeVar * pnode = nullptr;
  6637. if (m_ppnodeVar == &pnodeFnc->pnodeVars)
  6638. {
  6639. pnode = CreateVarDeclNode(pid, STVariable, true, pnodeFnc);
  6640. }
  6641. else
  6642. {
  6643. ParseNodePtr * const ppnodeVarSave = m_ppnodeVar;
  6644. m_ppnodeVar = &pnodeFnc->pnodeVars;
  6645. pnode = CreateVarDeclNode(pid, STVariable, true, pnodeFnc);
  6646. m_ppnodeVar = ppnodeVarSave;
  6647. }
  6648. Assert(pnode);
  6649. return pnode;
  6650. }
  6651. ParseNodeVar * Parser::AddArgumentsNodeToVars(ParseNodeFnc * pnodeFnc)
  6652. {
  6653. Assert(!GetCurrentFunctionNode()->IsLambda());
  6654. ParseNodeVar * argNode = InsertVarAtBeginning(pnodeFnc, wellKnownPropertyPids.arguments);
  6655. argNode->grfpn |= PNodeFlags::fpnArguments; // Flag this as the built-in arguments node
  6656. return argNode;
  6657. }
  6658. void Parser::UpdateArgumentsNode(ParseNodeFnc * pnodeFnc, ParseNodeVar * argNode)
  6659. {
  6660. if ((pnodeFnc->grfpn & PNodeFlags::fpnArguments_overriddenInParam) || pnodeFnc->IsLambda())
  6661. {
  6662. // There is a parameter named arguments. So we don't have to create the built-in arguments.
  6663. pnodeFnc->SetHasReferenceableBuiltInArguments(false);
  6664. }
  6665. else if ((pnodeFnc->grfpn & PNodeFlags::fpnArguments_overriddenByDecl) && pnodeFnc->IsBodyAndParamScopeMerged())
  6666. {
  6667. // In non-split scope case there is a var or function definition named arguments in the body
  6668. pnodeFnc->SetHasReferenceableBuiltInArguments(false);
  6669. }
  6670. else
  6671. {
  6672. pnodeFnc->SetHasReferenceableBuiltInArguments(true);
  6673. Assert(argNode);
  6674. }
  6675. if (argNode != nullptr && !argNode->sym->IsArguments())
  6676. {
  6677. // A duplicate definition has updated the declaration node. Need to reset it back.
  6678. argNode->grfpn |= PNodeFlags::fpnArguments;
  6679. argNode->sym->SetDecl(argNode);
  6680. }
  6681. }
  6682. LPCOLESTR Parser::GetFunctionName(ParseNodeFnc * pnodeFnc, LPCOLESTR pNameHint)
  6683. {
  6684. LPCOLESTR name = nullptr;
  6685. if (pnodeFnc->pnodeName != nullptr)
  6686. {
  6687. Assert(pnodeFnc->pnodeName->nop == knopVarDecl);
  6688. name = pnodeFnc->pnodeName->pid->Psz();
  6689. }
  6690. if (name == nullptr && pNameHint != nullptr)
  6691. {
  6692. name = pNameHint;
  6693. }
  6694. if (name == nullptr && (pnodeFnc->IsLambda() ||
  6695. (!pnodeFnc->IsDeclaration() && !pnodeFnc->IsMethod())))
  6696. {
  6697. name = Js::Constants::AnonymousFunction;
  6698. }
  6699. if (name == nullptr && m_functionBody != nullptr)
  6700. {
  6701. name = m_functionBody->GetExternalDisplayName();
  6702. }
  6703. else if (name == nullptr)
  6704. {
  6705. name = Js::Constants::AnonymousFunction;
  6706. }
  6707. return name;
  6708. }
  6709. IdentPtr Parser::ParseClassPropertyName(IdentPtr * pidHint)
  6710. {
  6711. if (m_token.tk == tkID || m_token.tk == tkStrCon || m_token.IsReservedWord())
  6712. {
  6713. IdentPtr pid;
  6714. if (m_token.tk == tkStrCon)
  6715. {
  6716. if (this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  6717. {
  6718. Error(ERRES5NoOctal);
  6719. }
  6720. pid = m_token.GetStr();
  6721. }
  6722. else
  6723. {
  6724. pid = m_token.GetIdentifier(this->GetHashTbl());
  6725. }
  6726. *pidHint = pid;
  6727. return pid;
  6728. }
  6729. else if (m_token.tk == tkIntCon)
  6730. {
  6731. if (this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  6732. {
  6733. Error(ERRES5NoOctal);
  6734. }
  6735. return this->GetScanner()->PidFromLong(m_token.GetLong());
  6736. }
  6737. else if (m_token.tk == tkFltCon)
  6738. {
  6739. if (this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  6740. {
  6741. Error(ERRES5NoOctal);
  6742. }
  6743. return this->GetScanner()->PidFromDbl(m_token.GetDouble());
  6744. }
  6745. Error(ERRnoMemberIdent);
  6746. }
  6747. LPCOLESTR Parser::ConstructFinalHintNode(IdentPtr pClassName, IdentPtr pMemberName, IdentPtr pGetSet, bool isStatic, uint32* nameLength, uint32* pShortNameOffset, bool isComputedName, LPCOLESTR pMemberNameHint)
  6748. {
  6749. if ((pMemberName == nullptr && !isComputedName) ||
  6750. (pMemberNameHint == nullptr && isComputedName) ||
  6751. !CONFIG_FLAG(UseFullName))
  6752. {
  6753. return nullptr;
  6754. }
  6755. LPCOLESTR pFinalName = isComputedName ? pMemberNameHint : pMemberName->Psz();
  6756. uint32 fullNameHintLength = (uint32)wcslen(pFinalName);
  6757. uint32 shortNameOffset = 0;
  6758. if (!isStatic)
  6759. {
  6760. // Add prototype.
  6761. pFinalName = AppendNameHints(wellKnownPropertyPids.prototype, pFinalName, &fullNameHintLength, &shortNameOffset);
  6762. }
  6763. if (pClassName)
  6764. {
  6765. uint32 classNameOffset = 0;
  6766. pFinalName = AppendNameHints(pClassName, pFinalName, &fullNameHintLength, &classNameOffset);
  6767. shortNameOffset += classNameOffset;
  6768. }
  6769. if (pGetSet)
  6770. {
  6771. // displays as get/set prototype.funcname
  6772. uint32 getSetOffset = 0;
  6773. pFinalName = AppendNameHints(pGetSet, pFinalName, &fullNameHintLength, &getSetOffset, true);
  6774. shortNameOffset += getSetOffset;
  6775. }
  6776. *nameLength = fullNameHintLength;
  6777. *pShortNameOffset = shortNameOffset;
  6778. return pFinalName;
  6779. }
  6780. template<bool buildAST>
  6781. ParseNodeClass * Parser::ParseClassDecl(BOOL isDeclaration, LPCOLESTR pNameHint, uint32 *pHintLength, uint32 *pShortNameOffset)
  6782. {
  6783. bool hasConstructor = false;
  6784. bool hasExtends = false;
  6785. IdentPtr name = nullptr;
  6786. ParseNodeVar * pnodeName = nullptr;
  6787. ParseNodeFnc * pnodeConstructor = nullptr;
  6788. ParseNodePtr pnodeExtends = nullptr;
  6789. ParseNodePtr pnodeMembers = nullptr;
  6790. ParseNodePtr *lastMemberNodeRef = nullptr;
  6791. uint32 nameHintLength = pHintLength ? *pHintLength : 0;
  6792. uint32 nameHintOffset = pShortNameOffset ? *pShortNameOffset : 0;
  6793. ArenaAllocator tempAllocator(_u("ClassMemberNames"), m_nodeAllocator.GetPageAllocator(), Parser::OutOfMemory);
  6794. size_t cbMinConstructor = 0;
  6795. ParseNodeClass * pnodeClass = nullptr;
  6796. if (buildAST)
  6797. {
  6798. pnodeClass = CreateNodeForOpT<knopClassDecl>();
  6799. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Class, m_scriptContext);
  6800. cbMinConstructor = this->GetScanner()->IecpMinTok();
  6801. }
  6802. BOOL strictSave = m_fUseStrictMode;
  6803. m_fUseStrictMode = TRUE;
  6804. this->GetScanner()->Scan();
  6805. if (m_token.tk == tkID)
  6806. {
  6807. name = m_token.GetIdentifier(this->GetHashTbl());
  6808. this->GetScanner()->Scan();
  6809. }
  6810. else if (isDeclaration)
  6811. {
  6812. IdentifierExpectedError(m_token);
  6813. }
  6814. if (isDeclaration && name == wellKnownPropertyPids.arguments && GetCurrentBlockInfo()->pnodeBlock->blockType == Function)
  6815. {
  6816. GetCurrentFunctionNode()->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
  6817. }
  6818. ParseNodeVar * pnodeDeclName = nullptr;
  6819. if (isDeclaration)
  6820. {
  6821. pnodeDeclName = CreateBlockScopedDeclNode(name, knopLetDecl);
  6822. }
  6823. ParseNodePtr *ppnodeScopeSave = nullptr;
  6824. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  6825. ParseNodeBlock * pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
  6826. if (buildAST)
  6827. {
  6828. PushFuncBlockScope(pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
  6829. pnodeClass->pnodeBlock = pnodeBlock;
  6830. }
  6831. if (name)
  6832. {
  6833. pnodeName = CreateBlockScopedDeclNode(name, knopConstDecl);
  6834. }
  6835. if (m_token.tk == tkEXTENDS)
  6836. {
  6837. this->GetScanner()->Scan();
  6838. pnodeExtends = ParseTerm<buildAST>();
  6839. hasExtends = true;
  6840. }
  6841. if (m_token.tk != tkLCurly)
  6842. {
  6843. Error(ERRnoLcurly);
  6844. }
  6845. OUTPUT_TRACE_DEBUGONLY(Js::ES6VerboseFlag, _u("Parsing class (%s) : %s\n"), GetParseType(), name ? name->Psz() : _u("anonymous class"));
  6846. RestorePoint beginClass;
  6847. this->GetScanner()->Capture(&beginClass);
  6848. this->GetScanner()->ScanForcingPid();
  6849. IdentPtr pClassNamePid = pnodeName ? pnodeName->pid : nullptr;
  6850. for (;;)
  6851. {
  6852. if (m_token.tk == tkSColon)
  6853. {
  6854. this->GetScanner()->ScanForcingPid();
  6855. continue;
  6856. }
  6857. if (m_token.tk == tkRCurly)
  6858. {
  6859. break;
  6860. }
  6861. bool isStatic = false;
  6862. if (m_token.tk == tkSTATIC)
  6863. {
  6864. // 'static' can be used as an IdentifierName here, even in strict mode code. We need to see the next token before we know
  6865. // if this is being used as a keyword. This is similar to the way we treat 'let' in some cases.
  6866. // See https://tc39.github.io/ecma262/#sec-keywords for more info.
  6867. RestorePoint beginStatic;
  6868. this->GetScanner()->Capture(&beginStatic);
  6869. this->GetScanner()->ScanForcingPid();
  6870. if (m_token.tk == tkLParen)
  6871. {
  6872. this->GetScanner()->SeekTo(beginStatic);
  6873. }
  6874. else
  6875. {
  6876. isStatic = true;
  6877. }
  6878. }
  6879. ushort fncDeclFlags = fFncNoName | fFncMethod | fFncClassMember;
  6880. charcount_t ichMin = this->GetScanner()->IchMinTok();
  6881. size_t iecpMin = this->GetScanner()->IecpMinTok();
  6882. ParseNodePtr pnodeMemberName = nullptr;
  6883. IdentPtr pidHint = nullptr;
  6884. IdentPtr memberPid = nullptr;
  6885. bool maybeAccessor = false;
  6886. LPCOLESTR pMemberNameHint = nullptr;
  6887. uint32 memberNameHintLength = 0;
  6888. uint32 memberNameOffset = 0;
  6889. bool isComputedName = false;
  6890. bool isAsyncMethod = false;
  6891. if (CheckContextualKeyword(wellKnownPropertyPids.async) && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  6892. {
  6893. RestorePoint parsedAsync;
  6894. this->GetScanner()->Capture(&parsedAsync);
  6895. ichMin = this->GetScanner()->IchMinTok();
  6896. iecpMin = this->GetScanner()->IecpMinTok();
  6897. this->GetScanner()->Scan();
  6898. if (m_token.tk == tkLParen || this->GetScanner()->FHadNewLine())
  6899. {
  6900. this->GetScanner()->SeekTo(parsedAsync);
  6901. }
  6902. else
  6903. {
  6904. isAsyncMethod = true;
  6905. }
  6906. }
  6907. bool isGenerator = m_scriptContext->GetConfig()->IsES6GeneratorsEnabled() &&
  6908. m_token.tk == tkStar;
  6909. if (isGenerator)
  6910. {
  6911. fncDeclFlags |= fFncGenerator;
  6912. this->GetScanner()->ScanForcingPid();
  6913. }
  6914. if (m_token.tk == tkLBrack && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
  6915. {
  6916. // Computed member name: [expr] () { }
  6917. LPCOLESTR emptyHint = nullptr;
  6918. ParseComputedName<buildAST>(&pnodeMemberName, &emptyHint, &pMemberNameHint, &memberNameHintLength, &memberNameOffset);
  6919. isComputedName = true;
  6920. }
  6921. else // not computed name
  6922. {
  6923. maybeAccessor = !this->GetScanner()->LastIdentifierHasEscape();
  6924. memberPid = this->ParseClassPropertyName(&pidHint);
  6925. if (pidHint)
  6926. {
  6927. pMemberNameHint = pidHint->Psz();
  6928. memberNameHintLength = pidHint->Cch();
  6929. }
  6930. }
  6931. if (buildAST && memberPid)
  6932. {
  6933. pnodeMemberName = CreateStrNode(memberPid);
  6934. }
  6935. if (!isStatic && memberPid == wellKnownPropertyPids.constructor)
  6936. {
  6937. if (hasConstructor || isAsyncMethod)
  6938. {
  6939. Error(ERRsyntax);
  6940. }
  6941. hasConstructor = true;
  6942. LPCOLESTR pConstructorName = nullptr;
  6943. uint32 constructorNameLength = 0;
  6944. uint32 constructorShortNameHintOffset = 0;
  6945. if (pnodeName && pnodeName->pid)
  6946. {
  6947. pConstructorName = pnodeName->pid->Psz();
  6948. constructorNameLength = pnodeName->pid->Cch();
  6949. }
  6950. else
  6951. {
  6952. pConstructorName = pNameHint;
  6953. constructorNameLength = nameHintLength;
  6954. constructorShortNameHintOffset = nameHintOffset;
  6955. }
  6956. {
  6957. SuperRestrictionState::State state = hasExtends ? SuperRestrictionState::CallAndPropertyAllowed : SuperRestrictionState::PropertyAllowed;
  6958. // Add the class constructor flag and base class constructor flag if pnodeExtends is nullptr
  6959. fncDeclFlags |= fFncClassConstructor | (hasExtends ? kFunctionNone : fFncBaseClassConstructor);
  6960. pnodeConstructor = ParseFncDeclNoCheckScope<buildAST>(fncDeclFlags, state, pConstructorName, /* needsPIDOnRCurlyScan */ true);
  6961. }
  6962. if (pnodeConstructor->IsGenerator())
  6963. {
  6964. Error(ERRConstructorCannotBeGenerator);
  6965. }
  6966. Assert(constructorNameLength >= constructorShortNameHintOffset);
  6967. // The constructor function will get the same name as class.
  6968. pnodeConstructor->hint = pConstructorName;
  6969. pnodeConstructor->hintLength = constructorNameLength;
  6970. pnodeConstructor->hintOffset = constructorShortNameHintOffset;
  6971. pnodeConstructor->pid = pnodeName && pnodeName->pid ? pnodeName->pid : wellKnownPropertyPids.constructor;
  6972. pnodeConstructor->SetHasNonThisStmt();
  6973. pnodeConstructor->SetHasHomeObj();
  6974. }
  6975. else
  6976. {
  6977. ParseNodePtr pnodeMember = nullptr;
  6978. RestorePoint beginMethodName;
  6979. this->GetScanner()->Capture(&beginMethodName);
  6980. if (maybeAccessor && (memberPid == wellKnownPropertyPids.get || memberPid == wellKnownPropertyPids.set))
  6981. {
  6982. this->GetScanner()->ScanForcingPid();
  6983. }
  6984. if (m_token.tk == tkLParen)
  6985. {
  6986. this->GetScanner()->SeekTo(beginMethodName);
  6987. maybeAccessor = false;
  6988. }
  6989. if (maybeAccessor && (memberPid == wellKnownPropertyPids.get || memberPid == wellKnownPropertyPids.set))
  6990. {
  6991. bool isGetter = (memberPid == wellKnownPropertyPids.get);
  6992. if (m_token.tk == tkLBrack && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
  6993. {
  6994. // Computed get/set member name: get|set [expr] () { }
  6995. LPCOLESTR emptyHint = nullptr;
  6996. ParseComputedName<buildAST>(&pnodeMemberName, &emptyHint, &pMemberNameHint, &memberNameHintLength, &memberNameOffset);
  6997. isComputedName = true;
  6998. }
  6999. else // not computed name
  7000. {
  7001. memberPid = this->ParseClassPropertyName(&pidHint);
  7002. }
  7003. if ((isStatic ? (memberPid == wellKnownPropertyPids.prototype) : (memberPid == wellKnownPropertyPids.constructor)) || isAsyncMethod)
  7004. {
  7005. Error(ERRsyntax);
  7006. }
  7007. if (buildAST && memberPid && !isComputedName)
  7008. {
  7009. pnodeMemberName = CreateStrNode(memberPid);
  7010. }
  7011. ParseNodeFnc * pnodeFnc = nullptr;
  7012. {
  7013. pnodeFnc = ParseFncDeclNoCheckScope<buildAST>(fncDeclFlags | (isGetter ? fFncNoArg : fFncOneArg),
  7014. SuperRestrictionState::PropertyAllowed, pidHint ? pidHint->Psz() : nullptr, /* needsPIDOnRCurlyScan */ true);
  7015. }
  7016. pnodeFnc->SetIsStaticMember(isStatic);
  7017. if (isComputedName)
  7018. {
  7019. pnodeFnc->SetHasComputedName();
  7020. }
  7021. pnodeFnc->SetHasHomeObj();
  7022. if (buildAST)
  7023. {
  7024. pnodeFnc->SetIsAccessor();
  7025. pnodeMember = CreateBinNode(isGetter ? knopGetMember : knopSetMember, pnodeMemberName, pnodeFnc);
  7026. pMemberNameHint = ConstructFinalHintNode(pClassNamePid, pidHint,
  7027. isGetter ? wellKnownPropertyPids.get : wellKnownPropertyPids.set, isStatic,
  7028. &memberNameHintLength, &memberNameOffset, isComputedName, pMemberNameHint);
  7029. }
  7030. }
  7031. else
  7032. {
  7033. if (isStatic && (memberPid == wellKnownPropertyPids.prototype))
  7034. {
  7035. Error(ERRsyntax);
  7036. }
  7037. ParseNodeFnc * pnodeFnc = nullptr;
  7038. {
  7039. if (isAsyncMethod)
  7040. {
  7041. fncDeclFlags |= fFncAsync;
  7042. }
  7043. pnodeFnc = ParseFncDeclNoCheckScope<buildAST>(fncDeclFlags, SuperRestrictionState::PropertyAllowed, pidHint ? pidHint->Psz() : nullptr, /* needsPIDOnRCurlyScan */ true);
  7044. if (isAsyncMethod || isGenerator || isComputedName)
  7045. {
  7046. pnodeFnc->cbStringMin = iecpMin;
  7047. pnodeFnc->cbStringLim = pnodeFnc->cbLim;
  7048. }
  7049. }
  7050. pnodeFnc->SetIsStaticMember(isStatic);
  7051. if (isComputedName)
  7052. {
  7053. pnodeFnc->SetHasComputedName();
  7054. }
  7055. pnodeFnc->SetHasHomeObj();
  7056. if (buildAST)
  7057. {
  7058. pnodeMember = CreateBinNode(knopMember, pnodeMemberName, pnodeFnc);
  7059. pMemberNameHint = ConstructFinalHintNode(pClassNamePid, pidHint, nullptr /*pgetset*/, isStatic, &memberNameHintLength, &memberNameOffset, isComputedName, pMemberNameHint);
  7060. }
  7061. }
  7062. if (buildAST)
  7063. {
  7064. Assert(memberNameHintLength >= memberNameOffset);
  7065. pnodeMember->AsParseNodeBin()->pnode2->AsParseNodeFnc()->hint = pMemberNameHint; // Fully qualified name
  7066. pnodeMember->AsParseNodeBin()->pnode2->AsParseNodeFnc()->hintLength = memberNameHintLength;
  7067. pnodeMember->AsParseNodeBin()->pnode2->AsParseNodeFnc()->hintOffset = memberNameOffset;
  7068. pnodeMember->AsParseNodeBin()->pnode2->AsParseNodeFnc()->pid = memberPid; // Short name
  7069. AddToNodeList(&pnodeMembers, &lastMemberNodeRef, pnodeMember);
  7070. }
  7071. }
  7072. }
  7073. size_t cbLimConstructor = 0;
  7074. if (buildAST)
  7075. {
  7076. pnodeClass->ichLim = this->GetScanner()->IchLimTok();
  7077. cbLimConstructor = this->GetScanner()->IecpLimTok();
  7078. }
  7079. if (!hasConstructor)
  7080. {
  7081. OUTPUT_TRACE_DEBUGONLY(Js::ES6VerboseFlag, _u("Generating constructor (%s) : %s\n"), GetParseType(), name ? name->Psz() : _u("anonymous class"));
  7082. RestorePoint endClass;
  7083. this->GetScanner()->Capture(&endClass);
  7084. this->GetScanner()->SeekTo(beginClass);
  7085. pnodeConstructor = GenerateEmptyConstructor<buildAST>(pnodeExtends != nullptr);
  7086. if (buildAST)
  7087. {
  7088. if (pClassNamePid)
  7089. {
  7090. pnodeConstructor->hint = pClassNamePid->Psz();
  7091. pnodeConstructor->hintLength = pClassNamePid->Cch();
  7092. pnodeConstructor->hintOffset = 0;
  7093. }
  7094. else
  7095. {
  7096. Assert(nameHintLength >= nameHintOffset);
  7097. pnodeConstructor->hint = pNameHint;
  7098. pnodeConstructor->hintLength = nameHintLength;
  7099. pnodeConstructor->hintOffset = nameHintOffset;
  7100. }
  7101. pnodeConstructor->pid = pClassNamePid;
  7102. }
  7103. this->GetScanner()->SeekTo(endClass);
  7104. }
  7105. if (buildAST)
  7106. {
  7107. pnodeConstructor->cbStringMin = cbMinConstructor;
  7108. pnodeConstructor->cbStringLim = cbLimConstructor;
  7109. PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
  7110. pnodeClass->pnodeDeclName = pnodeDeclName;
  7111. pnodeClass->pnodeName = pnodeName;
  7112. pnodeClass->pnodeConstructor = pnodeConstructor;
  7113. pnodeClass->pnodeExtends = pnodeExtends;
  7114. pnodeClass->pnodeMembers = pnodeMembers;
  7115. pnodeClass->isDefaultModuleExport = false;
  7116. }
  7117. FinishParseBlock(pnodeBlock);
  7118. m_fUseStrictMode = strictSave;
  7119. this->GetScanner()->Scan();
  7120. return pnodeClass;
  7121. }
  7122. template<bool buildAST>
  7123. ParseNodePtr Parser::ParseStringTemplateDecl(ParseNodePtr pnodeTagFnc)
  7124. {
  7125. ParseNodePtr pnodeStringLiterals = nullptr;
  7126. ParseNodePtr* lastStringLiteralNodeRef = nullptr;
  7127. ParseNodePtr pnodeRawStringLiterals = nullptr;
  7128. ParseNodePtr* lastRawStringLiteralNodeRef = nullptr;
  7129. ParseNodePtr pnodeSubstitutionExpressions = nullptr;
  7130. ParseNodePtr* lastSubstitutionExpressionNodeRef = nullptr;
  7131. ParseNodePtr pnodeTagFncArgs = nullptr;
  7132. ParseNodePtr* lastTagFncArgNodeRef = nullptr;
  7133. ParseNodeStr * stringLiteral = nullptr;
  7134. ParseNodeStr * stringLiteralRaw = nullptr;
  7135. ParseNodeStrTemplate * pnodeStringTemplate = nullptr;
  7136. ParseNode * pnodeReturn = nullptr;
  7137. bool templateClosed = false;
  7138. const bool isTagged = pnodeTagFnc != nullptr;
  7139. uint16 stringConstantCount = 0;
  7140. charcount_t ichMin = 0;
  7141. Assert(m_token.tk == tkStrTmplBasic || m_token.tk == tkStrTmplBegin);
  7142. if (buildAST)
  7143. {
  7144. pnodeReturn = pnodeStringTemplate = CreateNodeForOpT<knopStrTemplate>();
  7145. pnodeStringTemplate->countStringLiterals = 0;
  7146. pnodeStringTemplate->isTaggedTemplate = isTagged ? TRUE : FALSE;
  7147. // If this is a tagged string template, we need to start building the arg list for the call
  7148. if (isTagged)
  7149. {
  7150. ichMin = pnodeTagFnc->ichMin;
  7151. AddToNodeListEscapedUse(&pnodeTagFncArgs, &lastTagFncArgNodeRef, pnodeStringTemplate);
  7152. }
  7153. }
  7154. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, StringTemplates, m_scriptContext);
  7155. OUTPUT_TRACE_DEBUGONLY(
  7156. Js::StringTemplateParsePhase,
  7157. _u("Starting to parse a string template (%s)...\n\tis tagged = %s\n"),
  7158. GetParseType(),
  7159. isTagged ? _u("true") : _u("false (Raw and cooked strings will not differ!)"));
  7160. // String template grammar
  7161. // `...` Simple string template
  7162. // `...${ String template beginning
  7163. // }...${ String template middle
  7164. // }...` String template end
  7165. while (!templateClosed)
  7166. {
  7167. // First, extract the string constant part - we always have one
  7168. if (IsStrictMode() && this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber())
  7169. {
  7170. Error(ERRES5NoOctal);
  7171. }
  7172. // We are not able to pass more than a ushort worth of arguments to the tag
  7173. // so use that as a logical limit on the number of string constant pieces.
  7174. if (stringConstantCount >= Js::Constants::MaxAllowedArgs)
  7175. {
  7176. Error(ERRTooManyArgs);
  7177. }
  7178. // Keep track of the string literal count (must be the same for raw strings)
  7179. // We use this in code gen so we don't need to count the string literals list
  7180. stringConstantCount++;
  7181. // If we are not creating parse nodes, there is no need to create strings
  7182. if (buildAST)
  7183. {
  7184. stringLiteral = CreateStrNode(m_token.GetStr());
  7185. AddToNodeList(&pnodeStringLiterals, &lastStringLiteralNodeRef, stringLiteral);
  7186. // We only need to collect a raw string when we are going to pass the string template to a tag
  7187. if (isTagged)
  7188. {
  7189. // Make the scanner create a PID for the raw string constant for the preceding scan
  7190. IdentPtr pid = this->GetScanner()->GetSecondaryBufferAsPid();
  7191. stringLiteralRaw = CreateStrNode(pid);
  7192. // Should have gotten a raw string literal above
  7193. AddToNodeList(&pnodeRawStringLiterals, &lastRawStringLiteralNodeRef, stringLiteralRaw);
  7194. }
  7195. else
  7196. {
  7197. #if DBG
  7198. // Assign the raw string for debug tracing below
  7199. stringLiteralRaw = stringLiteral;
  7200. #endif
  7201. }
  7202. OUTPUT_TRACE_DEBUGONLY(
  7203. Js::StringTemplateParsePhase,
  7204. _u("Parsed string constant: \n\tcooked = \"%s\" \n\traw = \"%s\" \n\tdiffer = %d\n"),
  7205. stringLiteral->pid->Psz(),
  7206. stringLiteralRaw->pid->Psz(),
  7207. stringLiteral->pid->Psz() == stringLiteralRaw->pid->Psz() ? 0 : 1);
  7208. }
  7209. switch (m_token.tk)
  7210. {
  7211. case tkStrTmplEnd:
  7212. case tkStrTmplBasic:
  7213. // We do not need to parse an expression for either the end or basic string template tokens
  7214. templateClosed = true;
  7215. break;
  7216. case tkStrTmplBegin:
  7217. case tkStrTmplMid:
  7218. {
  7219. // In the middle or begin string template token case, we need to parse an expression next
  7220. this->GetScanner()->Scan();
  7221. // Parse the contents of the curly braces as an expression
  7222. ParseNodePtr expression = ParseExpr<buildAST>(0);
  7223. // After parsing expression, scan should leave us with an RCurly token.
  7224. // Use the NoScan version so we do not automatically perform a scan - we need to
  7225. // set the scan state before next scan but we don't want to set that state if
  7226. // the token is not as expected since we'll error in that case.
  7227. ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
  7228. // Notify the scanner that it should scan for a middle or end string template token
  7229. this->GetScanner()->SetScanState(Scanner_t::ScanState::ScanStateStringTemplateMiddleOrEnd);
  7230. this->GetScanner()->Scan();
  7231. if (buildAST)
  7232. {
  7233. // If we are going to call the tag function, add this expression into the list of args
  7234. if (isTagged)
  7235. {
  7236. AddToNodeListEscapedUse(&pnodeTagFncArgs, &lastTagFncArgNodeRef, expression);
  7237. }
  7238. else
  7239. {
  7240. // Otherwise add it to the substitution expression list
  7241. // TODO: Store the arguments and substitution expressions in a single list?
  7242. AddToNodeList(&pnodeSubstitutionExpressions, &lastSubstitutionExpressionNodeRef, expression);
  7243. }
  7244. }
  7245. if (!(m_token.tk == tkStrTmplMid || m_token.tk == tkStrTmplEnd))
  7246. {
  7247. // Scan with ScanState ScanStateStringTemplateMiddleOrEnd should only return
  7248. // tkStrTmpMid/End unless it is EOF or tkScanError
  7249. Assert(m_token.tk == tkEOF || m_token.tk == tkScanError);
  7250. Error(ERRsyntax);
  7251. }
  7252. OUTPUT_TRACE_DEBUGONLY(Js::StringTemplateParsePhase, _u("Parsed expression\n"));
  7253. }
  7254. break;
  7255. default:
  7256. Assert(false);
  7257. break;
  7258. }
  7259. }
  7260. if (buildAST)
  7261. {
  7262. pnodeStringTemplate->pnodeStringLiterals = pnodeStringLiterals;
  7263. pnodeStringTemplate->pnodeStringRawLiterals = pnodeRawStringLiterals;
  7264. pnodeStringTemplate->pnodeSubstitutionExpressions = pnodeSubstitutionExpressions;
  7265. pnodeStringTemplate->countStringLiterals = stringConstantCount;
  7266. // We should still have the last string literal.
  7267. // Use the char offset of the end of that constant as the end of the string template.
  7268. pnodeStringTemplate->ichLim = stringLiteral->ichLim;
  7269. // If this is a tagged template, we now have the argument list and can construct a call node
  7270. if (isTagged)
  7271. {
  7272. // Return the call node here and let the byte code generator Emit the string template automagically
  7273. ParseNodeCall * pnodeCall;
  7274. pnodeReturn = pnodeCall = CreateCallNode(knopCall, pnodeTagFnc, pnodeTagFncArgs, ichMin, pnodeStringTemplate->ichLim);
  7275. // We need to set the arg count explicitly
  7276. pnodeCall->argCount = stringConstantCount;
  7277. pnodeCall->hasDestructuring = m_hasDestructuringPattern;
  7278. }
  7279. }
  7280. this->GetScanner()->Scan();
  7281. return pnodeReturn;
  7282. }
  7283. LPCOLESTR Parser::FormatPropertyString(LPCOLESTR propertyString, ParseNodePtr pNode, uint32 *fullNameHintLength, uint32 *pShortNameOffset)
  7284. {
  7285. // propertyString could be null, such as 'this.foo' =
  7286. // propertyString could be empty, found in pattern as in (-1)[""][(x = z)]
  7287. OpCode op = pNode->nop;
  7288. LPCOLESTR rightNode = nullptr;
  7289. if (propertyString == nullptr)
  7290. {
  7291. propertyString = _u("");
  7292. }
  7293. if (op != knopInt && op != knopFlt && op != knopName && op != knopStr)
  7294. {
  7295. rightNode = _u("");
  7296. }
  7297. else if (op == knopStr)
  7298. {
  7299. return AppendNameHints(propertyString, pNode->AsParseNodeStr()->pid, fullNameHintLength, pShortNameOffset, false, true/*add brackets*/);
  7300. }
  7301. else if (op == knopFlt)
  7302. {
  7303. rightNode = this->GetScanner()->StringFromDbl(pNode->AsParseNodeFloat()->dbl);
  7304. }
  7305. else
  7306. {
  7307. rightNode = op == knopInt ? this->GetScanner()->StringFromLong(pNode->AsParseNodeInt()->lw)
  7308. : pNode->AsParseNodeName()->pid->Psz();
  7309. }
  7310. return AppendNameHints(propertyString, rightNode, fullNameHintLength, pShortNameOffset, false, true/*add brackets*/);
  7311. }
  7312. LPCOLESTR Parser::ConstructNameHint(ParseNodeBin * pNode, uint32* fullNameHintLength, uint32 *pShortNameOffset)
  7313. {
  7314. Assert(pNode != nullptr);
  7315. Assert(pNode->nop == knopDot || pNode->nop == knopIndex);
  7316. // This method recursively visits nodes in the AST and could cause an SOE crash for long knopDot chains.
  7317. // Although this method could be made non-recursive, Emit (ByteCodeEmitter.cpp) hits a stack probe
  7318. // for shorter chains than which cause SOE here, so add a stack probe to throw SOE rather than crash on SOE.
  7319. // Because of that correspondence, use Js::Constants::MinStackByteCodeVisitor (which is used in Emit)
  7320. // for the stack probe here. See OS#14711878.
  7321. PROBE_STACK_NO_DISPOSE(this->m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
  7322. LPCOLESTR leftNode = nullptr;
  7323. if (pNode->pnode1->nop == knopDot || pNode->pnode1->nop == knopIndex)
  7324. {
  7325. leftNode = ConstructNameHint(pNode->pnode1->AsParseNodeBin(), fullNameHintLength, pShortNameOffset);
  7326. }
  7327. else if (pNode->pnode1->nop == knopName && !pNode->pnode1->AsParseNodeName()->IsSpecialName())
  7328. {
  7329. // We need to skip special names like 'this' because those shouldn't be appended to the
  7330. // name hint in the debugger stack trace.
  7331. // function ctor() {
  7332. // this.func = function() {
  7333. // // If we break here, the stack should say we are in 'func' and not 'this.func'
  7334. // }
  7335. // }
  7336. IdentPtr pid = pNode->pnode1->AsParseNodeName()->pid;
  7337. leftNode = pid->Psz();
  7338. *fullNameHintLength = pid->Cch();
  7339. *pShortNameOffset = 0;
  7340. }
  7341. if (pNode->nop == knopIndex)
  7342. {
  7343. return FormatPropertyString(
  7344. leftNode ? leftNode : Js::Constants::AnonymousFunction, // e.g. f()[0] = function () {}
  7345. pNode->pnode2, fullNameHintLength, pShortNameOffset);
  7346. }
  7347. Assert(pNode->pnode2->nop == knopDot || pNode->pnode2->nop == knopName);
  7348. LPCOLESTR rightNode = nullptr;
  7349. bool wrapWithBrackets = false;
  7350. if (pNode->pnode2->nop == knopDot)
  7351. {
  7352. rightNode = ConstructNameHint(pNode->pnode2->AsParseNodeBin(), fullNameHintLength, pShortNameOffset);
  7353. }
  7354. else
  7355. {
  7356. rightNode = pNode->pnode2->AsParseNodeName()->pid->Psz();
  7357. wrapWithBrackets = PNodeFlags::fpnIndexOperator == (pNode->grfpn & PNodeFlags::fpnIndexOperator);
  7358. }
  7359. Assert(rightNode != nullptr);
  7360. return AppendNameHints(leftNode, rightNode, fullNameHintLength, pShortNameOffset, false, wrapWithBrackets);
  7361. }
  7362. LPCOLESTR Parser::AppendNameHints(LPCOLESTR leftStr, uint32 leftLen, LPCOLESTR rightStr, uint32 rightLen, uint32 *pNameLength, uint32 *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
  7363. {
  7364. Assert(rightStr != nullptr);
  7365. Assert(leftLen != 0 || wrapInBrackets);
  7366. Assert(rightLen != 0 || wrapInBrackets);
  7367. bool ignoreDot = rightStr[0] == _u('[') && !wrapInBrackets;//if we wrap in brackets it can be a string literal which can have brackets at the first char
  7368. uint32 totalLength = leftLen + rightLen + ((ignoreDot) ? 1 : 2); // 1 (for dot or [) + 1 (for null termination)
  7369. if (wrapInBrackets)
  7370. {
  7371. totalLength++; //1 for ']';
  7372. }
  7373. WCHAR * finalName = AllocateStringOfLength(totalLength);
  7374. if (leftStr != nullptr && leftLen != 0)
  7375. {
  7376. wcscpy_s(finalName, leftLen + 1, leftStr);
  7377. }
  7378. if (ignoreAddDotWithSpace)
  7379. {
  7380. finalName[leftLen++] = (OLECHAR)_u(' ');
  7381. }
  7382. // mutually exclusive from ignoreAddDotWithSpace which is used for getters/setters
  7383. else if (wrapInBrackets)
  7384. {
  7385. finalName[leftLen++] = (OLECHAR)_u('[');
  7386. finalName[totalLength - 2] = (OLECHAR)_u(']');
  7387. }
  7388. else if (!ignoreDot)
  7389. {
  7390. finalName[leftLen++] = (OLECHAR)_u('.');
  7391. }
  7392. //ignore case falls through
  7393. js_wmemcpy_s(finalName + leftLen, rightLen, rightStr, rightLen);
  7394. finalName[totalLength - 1] = (OLECHAR)_u('\0');
  7395. if (pNameLength != nullptr)
  7396. {
  7397. *pNameLength = totalLength - 1;
  7398. }
  7399. if (pShortNameOffset != nullptr)
  7400. {
  7401. *pShortNameOffset = leftLen;
  7402. }
  7403. return finalName;
  7404. }
  7405. WCHAR * Parser::AllocateStringOfLength(ULONG length)
  7406. {
  7407. Assert(length > 0);
  7408. ULONG totalBytes;
  7409. if (ULongMult(length, sizeof(OLECHAR), &totalBytes) != S_OK)
  7410. {
  7411. Error(ERRnoMemory);
  7412. }
  7413. WCHAR* finalName = (WCHAR*)this->GetHashTbl()->GetAllocator()->Alloc(totalBytes);
  7414. if (finalName == nullptr)
  7415. {
  7416. Error(ERRnoMemory);
  7417. }
  7418. return finalName;
  7419. }
  7420. LPCOLESTR Parser::AppendNameHints(IdentPtr left, IdentPtr right, uint32 *pNameLength, uint32 *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
  7421. {
  7422. if (pShortNameOffset != nullptr)
  7423. {
  7424. *pShortNameOffset = 0;
  7425. }
  7426. if (left == nullptr && !wrapInBrackets)
  7427. {
  7428. if (right)
  7429. {
  7430. *pNameLength = right->Cch();
  7431. return right->Psz();
  7432. }
  7433. return nullptr;
  7434. }
  7435. uint32 leftLen = 0;
  7436. LPCOLESTR leftStr = _u("");
  7437. if (left != nullptr) // if wrapInBrackets is true
  7438. {
  7439. leftStr = left->Psz();
  7440. leftLen = left->Cch();
  7441. }
  7442. if (right == nullptr)
  7443. {
  7444. *pNameLength = leftLen;
  7445. return left->Psz();
  7446. }
  7447. uint32 rightLen = right->Cch();
  7448. return AppendNameHints(leftStr, leftLen, right->Psz(), rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
  7449. }
  7450. LPCOLESTR Parser::AppendNameHints(IdentPtr left, LPCOLESTR right, uint32 *pNameLength, uint32 *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
  7451. {
  7452. uint32 rightLen = (right == nullptr) ? 0 : (uint32)wcslen(right);
  7453. if (pShortNameOffset != nullptr)
  7454. {
  7455. *pShortNameOffset = 0;
  7456. }
  7457. Assert(rightLen <= ULONG_MAX); // name hints should not exceed ULONG_MAX characters
  7458. if (left == nullptr && !wrapInBrackets)
  7459. {
  7460. *pNameLength = rightLen;
  7461. return right;
  7462. }
  7463. LPCOLESTR leftStr = _u("");
  7464. uint32 leftLen = 0;
  7465. if (left != nullptr) // if wrapInBrackets is true
  7466. {
  7467. leftStr = left->Psz();
  7468. leftLen = left->Cch();
  7469. }
  7470. if (rightLen == 0 && !wrapInBrackets)
  7471. {
  7472. *pNameLength = leftLen;
  7473. return left->Psz();
  7474. }
  7475. return AppendNameHints(leftStr, leftLen, right, rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
  7476. }
  7477. LPCOLESTR Parser::AppendNameHints(LPCOLESTR left, IdentPtr right, uint32 *pNameLength, uint32 *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
  7478. {
  7479. uint32 leftLen = (left == nullptr) ? 0 : (uint32)wcslen(left);
  7480. if (pShortNameOffset != nullptr)
  7481. {
  7482. *pShortNameOffset = 0;
  7483. }
  7484. Assert(leftLen <= ULONG_MAX); // name hints should not exceed ULONG_MAX characters
  7485. if (left == nullptr || (leftLen == 0 && !wrapInBrackets))
  7486. {
  7487. if (right != nullptr)
  7488. {
  7489. *pNameLength = right->Cch();
  7490. return right->Psz();
  7491. }
  7492. return nullptr;
  7493. }
  7494. if (right == nullptr)
  7495. {
  7496. *pNameLength = leftLen;
  7497. return left;
  7498. }
  7499. uint32 rightLen = right->Cch();
  7500. return AppendNameHints(left, leftLen, right->Psz(), rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
  7501. }
  7502. LPCOLESTR Parser::AppendNameHints(LPCOLESTR left, LPCOLESTR right, uint32 *pNameLength, uint32 *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
  7503. {
  7504. uint32 leftLen = (left == nullptr) ? 0 : (uint32)wcslen(left);
  7505. uint32 rightLen = (right == nullptr) ? 0 : (uint32)wcslen(right);
  7506. if (pShortNameOffset != nullptr)
  7507. {
  7508. *pShortNameOffset = 0;
  7509. }
  7510. Assert(rightLen <= ULONG_MAX && leftLen <= ULONG_MAX); // name hints should not exceed ULONG_MAX characters
  7511. if (leftLen == 0 && !wrapInBrackets)
  7512. {
  7513. *pNameLength = right ? rightLen : 0;
  7514. return right;
  7515. }
  7516. if (rightLen == 0 && !wrapInBrackets)
  7517. {
  7518. *pNameLength = leftLen;
  7519. return left;
  7520. }
  7521. return AppendNameHints(left, leftLen, right, rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
  7522. }
  7523. /**
  7524. * Emits a spread error if there is no ambiguity, or marks defers the error for
  7525. * when we can determine if it is a rest error or a spread error.
  7526. *
  7527. * The ambiguity arises when we are parsing a lambda parameter list but we have
  7528. * not seen the => token. At this point, we are either in a parenthesized
  7529. * expression or a parameter list, and cannot issue an error until the matching
  7530. * RParen has been scanned.
  7531. *
  7532. * The actual emission of the error happens in ParseExpr, when we first know if
  7533. * the expression is a lambda parameter list or not.
  7534. *
  7535. */
  7536. void Parser::DeferOrEmitPotentialSpreadError(ParseNodePtr pnodeT)
  7537. {
  7538. if (m_funcParenExprDepth > 0)
  7539. {
  7540. if (m_token.tk == tkRParen)
  7541. {
  7542. if (!m_deferEllipsisError)
  7543. {
  7544. // Capture only the first error instance. Because a lambda will cause a reparse in a formals context, we can assume
  7545. // that this will be a spread error. Nested paren exprs will have their own error instance.
  7546. this->GetScanner()->Capture(&m_deferEllipsisErrorLoc);
  7547. m_deferEllipsisError = true;
  7548. }
  7549. }
  7550. else
  7551. {
  7552. Error(ERRUnexpectedEllipsis);
  7553. }
  7554. }
  7555. else
  7556. {
  7557. Error(ERRInvalidSpreadUse);
  7558. }
  7559. }
  7560. bool Parser::IsTerminateToken(bool fAllowIn)
  7561. {
  7562. return (m_token.tk == tkRCurly ||
  7563. m_token.tk == tkRBrack ||
  7564. m_token.tk == tkRParen ||
  7565. m_token.tk == tkSColon ||
  7566. m_token.tk == tkColon ||
  7567. m_token.tk == tkComma ||
  7568. m_token.tk == tkLimKwd ||
  7569. (m_token.tk == tkIN && fAllowIn) ||
  7570. this->GetScanner()->FHadNewLine());
  7571. }
  7572. /***************************************************************************
  7573. Parse an optional sub expression returning null if there was no expression.
  7574. Checks for no expression by looking for a token that can follow an
  7575. Expression grammar production.
  7576. ***************************************************************************/
  7577. template<bool buildAST>
  7578. bool Parser::ParseOptionalExpr(ParseNodePtr* pnode, bool fUnaryOrParen, int oplMin, BOOL *pfCanAssign, BOOL fAllowIn, BOOL fAllowEllipsis, _Inout_opt_ IdentToken* pToken)
  7579. {
  7580. *pnode = nullptr;
  7581. if (IsTerminateToken(!fAllowIn))
  7582. {
  7583. return false;
  7584. }
  7585. IdentToken token;
  7586. ParseNodePtr pnodeT = ParseExpr<buildAST>(oplMin, pfCanAssign, fAllowIn, fAllowEllipsis, nullptr /*pNameHint*/, nullptr /*pHintLength*/, nullptr /*pShortNameOffset*/, &token, fUnaryOrParen);
  7587. // Detect nested function escapes of the pattern "return function(){...}" or "yield function(){...}".
  7588. // Doing so in the parser allows us to disable stack-nested-functions in common cases where an escape
  7589. // is not detected at byte code gen time because of deferred parsing.
  7590. this->MarkEscapingRef(pnodeT, &token);
  7591. if (pToken)
  7592. {
  7593. *pToken = token;
  7594. }
  7595. *pnode = pnodeT;
  7596. return true;
  7597. }
  7598. /***************************************************************************
  7599. Parse a sub expression.
  7600. 'fAllowIn' indicates if the 'in' operator should be allowed in the initializing
  7601. expression ( it is not allowed in the context of the first expression in a 'for' loop).
  7602. ***************************************************************************/
  7603. template<bool buildAST>
  7604. ParseNodePtr Parser::ParseExpr(int oplMin,
  7605. BOOL *pfCanAssign,
  7606. BOOL fAllowIn,
  7607. BOOL fAllowEllipsis,
  7608. LPCOLESTR pNameHint,
  7609. uint32 *pHintLength,
  7610. uint32 *pShortNameOffset,
  7611. _Inout_opt_ IdentToken* pToken,
  7612. bool fUnaryOrParen,
  7613. _Inout_opt_ bool* pfLikelyPattern,
  7614. _Inout_opt_ charcount_t *plastRParen,
  7615. _Out_opt_ bool* looseCoalesce)
  7616. {
  7617. Assert(pToken == nullptr || pToken->tk == tkNone); // Must be empty initially
  7618. int opl;
  7619. OpCode nop;
  7620. charcount_t ichMin;
  7621. ParseNodePtr pnode = nullptr;
  7622. ParseNodePtr pnodeT = nullptr;
  7623. BOOL fCanAssign = TRUE;
  7624. bool assignmentStmt = false;
  7625. bool fIsDotOrIndex = false;
  7626. IdentToken term;
  7627. RestorePoint termStart;
  7628. uint32 hintLength = 0;
  7629. uint32 hintOffset = 0;
  7630. BOOL fLikelyPattern = FALSE;
  7631. bool localCoalesce = false;
  7632. ParserState parserState;
  7633. if (pHintLength != nullptr)
  7634. {
  7635. hintLength = *pHintLength;
  7636. }
  7637. if (pShortNameOffset != nullptr)
  7638. {
  7639. hintOffset = *pShortNameOffset;
  7640. }
  7641. EnsureStackAvailable();
  7642. // Storing the state here as we need to restore this state back when we need to reparse the grammar under lambda syntax.
  7643. CaptureState(&parserState);
  7644. this->GetScanner()->Capture(&termStart);
  7645. bool savedDeferredInitError = m_hasDeferredShorthandInitError;
  7646. m_hasDeferredShorthandInitError = false;
  7647. // Is the current token a unary operator?
  7648. if (this->GetHashTbl()->TokIsUnop(m_token.tk, &opl, &nop) && nop != knopNone)
  7649. {
  7650. IdentToken operandToken;
  7651. ichMin = this->GetScanner()->IchMinTok();
  7652. if (nop == knopYield)
  7653. {
  7654. if (!this->GetScanner()->YieldIsKeywordRegion() || oplMin > opl)
  7655. {
  7656. // The case where 'yield' is scanned as a keyword (tkYIELD) but the scanner
  7657. // is not treating yield as a keyword (!this->GetScanner()->YieldIsKeywordRegion()) occurs
  7658. // in strict mode non-generator function contexts.
  7659. //
  7660. // That is, 'yield' is a keyword because of strict mode, but YieldExpression
  7661. // is not a grammar production outside of generator functions.
  7662. //
  7663. // Otherwise it is an error for a yield to appear in the context of a higher level
  7664. // binding operator, be it unary or binary.
  7665. Error(ERRsyntax);
  7666. }
  7667. if(m_currentScope->AncestorScopeIsParameter()) // Yield is not allowed within any parameter scope
  7668. {
  7669. Error(ERRsyntax);
  7670. }
  7671. }
  7672. else if (nop == knopAwait)
  7673. {
  7674. if (!this->GetScanner()->AwaitIsKeywordRegion() ||
  7675. m_currentScope->AncestorScopeIsParameter()) // Await is not allowed within any parameter scope
  7676. {
  7677. // As with the 'yield' keyword, the case where 'await' is scanned as a keyword (tkAWAIT)
  7678. // but the scanner is not treating await as a keyword (!this->GetScanner()->AwaitIsKeyword())
  7679. // occurs in strict mode non-async function contexts.
  7680. //
  7681. // That is, 'await' is a keyword because of strict mode, but AwaitExpression
  7682. // is not a grammar production outside of async functions.
  7683. //
  7684. // Further, await expressions are disallowed within parameter scopes.
  7685. if (IsTopLevelModuleFunc())
  7686. {
  7687. MakeModuleAsync();
  7688. }
  7689. else
  7690. {
  7691. Error(ERRBadAwait);
  7692. }
  7693. }
  7694. }
  7695. this->GetScanner()->Scan();
  7696. if (m_token.tk == tkEllipsis) {
  7697. // ... cannot have a unary prefix.
  7698. Error(ERRUnexpectedEllipsis);
  7699. }
  7700. if (nop == knopYield && !this->GetScanner()->FHadNewLine() && m_token.tk == tkStar)
  7701. {
  7702. this->GetScanner()->Scan();
  7703. nop = knopYieldStar;
  7704. }
  7705. if (nop == knopYield)
  7706. {
  7707. if (!ParseOptionalExpr<buildAST>(&pnodeT, false, opl, NULL, fAllowIn, fAllowEllipsis))
  7708. {
  7709. nop = knopYieldLeaf;
  7710. if (buildAST)
  7711. {
  7712. pnode = CreateNodeForOpT<knopYieldLeaf>(ichMin, this->GetScanner()->IchLimTok());
  7713. }
  7714. }
  7715. }
  7716. else if (nop == knopAwait && m_token.tk == tkColon)
  7717. {
  7718. Error(ERRAwaitAsLabelInAsync);
  7719. }
  7720. else
  7721. {
  7722. // Disallow spread after a unary operator.
  7723. pnodeT = ParseExpr<buildAST>(opl, &fCanAssign, TRUE, FALSE, nullptr /*hint*/, nullptr /*hintLength*/, nullptr /*hintOffset*/, &operandToken, true, nullptr, plastRParen);
  7724. }
  7725. if (nop != knopYieldLeaf)
  7726. {
  7727. if ((nop == knopIncPre || nop == knopDecPre) && (m_token.tk != tkDArrow))
  7728. {
  7729. if (!fCanAssign &&
  7730. (m_sourceContextInfo
  7731. ? !PHASE_OFF_RAW(Js::EarlyReferenceErrorsPhase, m_sourceContextInfo->sourceContextId, GetCurrentFunctionNode()->functionId)
  7732. : !PHASE_OFF1(Js::EarlyReferenceErrorsPhase)))
  7733. {
  7734. Error(ERRInvalidAsgTarget);
  7735. }
  7736. TrackAssignment<buildAST>(pnodeT, &operandToken);
  7737. if (buildAST)
  7738. {
  7739. if (IsStrictMode() && pnodeT->nop == knopName)
  7740. {
  7741. CheckStrictModeEvalArgumentsUsage(pnodeT->AsParseNodeName()->pid);
  7742. }
  7743. }
  7744. else
  7745. {
  7746. if (IsStrictMode() && operandToken.tk == tkID)
  7747. {
  7748. CheckStrictModeEvalArgumentsUsage(operandToken.pid);
  7749. }
  7750. }
  7751. }
  7752. else if (nop == knopEllipsis)
  7753. {
  7754. if (!fAllowEllipsis)
  7755. {
  7756. DeferOrEmitPotentialSpreadError(pnodeT);
  7757. }
  7758. }
  7759. else if (m_token.tk == tkExpo)
  7760. {
  7761. //Unary operator on the left hand-side of ** is unexpected, except ++, -- or ...
  7762. Error(ERRInvalidUseofExponentiationOperator);
  7763. }
  7764. if (buildAST)
  7765. {
  7766. //Do not do the folding for Asm in case of KnopPos as we need this to determine the type
  7767. if (nop == knopPos && (pnodeT->nop == knopInt || pnodeT->nop == knopFlt) && !this->m_InAsmMode)
  7768. {
  7769. // Fold away a unary '+' on a number.
  7770. pnode = pnodeT;
  7771. }
  7772. else if (nop == knopNeg &&
  7773. ((pnodeT->nop == knopInt && pnodeT->AsParseNodeInt()->lw != 0) ||
  7774. (pnodeT->nop == knopFlt && (pnodeT->AsParseNodeFloat()->dbl != 0 || this->m_InAsmMode)) ||
  7775. (pnodeT->nop == knopBigInt)))
  7776. {
  7777. // Fold a unary '-' on a number into the value of the number itself.
  7778. pnode = pnodeT;
  7779. if (pnode->nop == knopInt)
  7780. {
  7781. pnode->AsParseNodeInt()->lw = -pnode->AsParseNodeInt()->lw;
  7782. }
  7783. else if (pnode->nop == knopBigInt)
  7784. {
  7785. pnode->AsParseNodeBigInt()->isNegative = true;
  7786. }
  7787. else
  7788. {
  7789. pnode->AsParseNodeFloat()->dbl = -pnode->AsParseNodeFloat()->dbl;
  7790. }
  7791. }
  7792. else
  7793. {
  7794. pnode = CreateUniNode(nop, pnodeT);
  7795. this->CheckArguments(pnode->AsParseNodeUni()->pnode1);
  7796. }
  7797. pnode->ichMin = ichMin;
  7798. }
  7799. if (nop == knopDelete)
  7800. {
  7801. if (IsStrictMode())
  7802. {
  7803. if ((buildAST && pnode->AsParseNodeUni()->pnode1->IsUserIdentifier()) ||
  7804. (!buildAST && operandToken.tk == tkID && !this->IsSpecialName(operandToken.pid)))
  7805. {
  7806. Error(ERRInvalidDelete);
  7807. }
  7808. }
  7809. if (buildAST)
  7810. {
  7811. ParseNodePtr pnode1 = pnode->AsParseNodeUni()->pnode1;
  7812. if (m_currentNodeFunc)
  7813. {
  7814. if (pnode1->nop == knopDot || pnode1->nop == knopIndex)
  7815. {
  7816. // If we delete an arguments property, use the conservative,
  7817. // heap-allocated arguments object.
  7818. this->CheckArguments(pnode1->AsParseNodeBin()->pnode1);
  7819. }
  7820. }
  7821. }
  7822. }
  7823. }
  7824. fCanAssign = FALSE;
  7825. }
  7826. else
  7827. {
  7828. ichMin = this->GetScanner()->IchMinTok();
  7829. pnode = ParseTerm<buildAST>(TRUE, pNameHint, &hintLength, &hintOffset, &term, fUnaryOrParen, TRUE, &fCanAssign, &fLikelyPattern, &fIsDotOrIndex, plastRParen, &localCoalesce);
  7830. if (looseCoalesce != nullptr)
  7831. {
  7832. *looseCoalesce = localCoalesce;
  7833. }
  7834. if (pfLikelyPattern != nullptr)
  7835. {
  7836. *pfLikelyPattern = !!fLikelyPattern;
  7837. }
  7838. if (m_token.tk == tkDArrow
  7839. // If we have introduced shorthand error above in the ParseExpr, we need to reset if next token is the assignment.
  7840. || (m_token.tk == tkAsg && oplMin <= koplAsg))
  7841. {
  7842. m_hasDeferredShorthandInitError = false;
  7843. }
  7844. if (m_token.tk == tkAsg && oplMin <= koplAsg && fLikelyPattern)
  7845. {
  7846. this->GetScanner()->SeekTo(termStart);
  7847. // As we are reparsing from the beginning of the destructured literal we need to reset the Block IDs as well to make sure the Block IDs
  7848. // on the pidref stack match.
  7849. int saveNextBlockId = m_nextBlockId;
  7850. m_nextBlockId = parserState.m_nextBlockId;
  7851. ParseDestructuredLiteralWithScopeSave(tkLCurly, false/*isDecl*/, false /*topLevel*/, DIC_ShouldNotParseInitializer);
  7852. fCanAssign = TRUE;
  7853. // Restore the Block ID at the end of the reparsing so it matches the one at the end of the first pass. We need to do this
  7854. // because we don't parse initializers during reparse and there may be additional blocks (e.g. a class declaration)
  7855. // in the initializers that will cause the next Block ID at the end of the reparsing to be different.
  7856. m_nextBlockId = saveNextBlockId;
  7857. if (buildAST)
  7858. {
  7859. this->SetHasDestructuringPattern(true);
  7860. pnode = ConvertToPattern(pnode);
  7861. }
  7862. }
  7863. if (buildAST)
  7864. {
  7865. pNameHint = NULL;
  7866. if (pnode->nop == knopName)
  7867. {
  7868. IdentPtr pid = pnode->AsParseNodeName()->pid;
  7869. pNameHint = pid->Psz();
  7870. hintLength = pid->Cch();
  7871. hintOffset = 0;
  7872. }
  7873. else if (pnode->nop == knopDot || pnode->nop == knopIndex)
  7874. {
  7875. if (CONFIG_FLAG(UseFullName))
  7876. {
  7877. pNameHint = ConstructNameHint(pnode->AsParseNodeBin(), &hintLength, &hintOffset);
  7878. }
  7879. else
  7880. {
  7881. ParseNodePtr pnodeName = pnode;
  7882. while (pnodeName->nop == knopDot)
  7883. {
  7884. pnodeName = pnodeName->AsParseNodeBin()->pnode2;
  7885. }
  7886. if (pnodeName->nop == knopName)
  7887. {
  7888. IdentPtr pid = pnode->AsParseNodeName()->pid;
  7889. pNameHint = pid->Psz();
  7890. hintLength = pid->Cch();
  7891. hintOffset = 0;
  7892. }
  7893. }
  7894. }
  7895. }
  7896. // Check for postfix unary operators.
  7897. if (!this->GetScanner()->FHadNewLine() &&
  7898. (tkInc == m_token.tk || tkDec == m_token.tk))
  7899. {
  7900. if (!fCanAssign &&
  7901. (m_sourceContextInfo
  7902. ? !PHASE_OFF_RAW(Js::EarlyReferenceErrorsPhase, m_sourceContextInfo->sourceContextId, GetCurrentFunctionNode()->functionId)
  7903. : !PHASE_OFF1(Js::EarlyReferenceErrorsPhase)))
  7904. {
  7905. Error(ERRInvalidAsgTarget);
  7906. }
  7907. TrackAssignment<buildAST>(pnode, &term);
  7908. fCanAssign = FALSE;
  7909. if (buildAST)
  7910. {
  7911. if (IsStrictMode() && pnode->nop == knopName)
  7912. {
  7913. CheckStrictModeEvalArgumentsUsage(pnode->AsParseNodeName()->pid);
  7914. }
  7915. this->CheckArguments(pnode);
  7916. pnode = CreateUniNode(tkInc == m_token.tk ? knopIncPost : knopDecPost, pnode);
  7917. pnode->ichLim = this->GetScanner()->IchLimTok();
  7918. }
  7919. else
  7920. {
  7921. if (IsStrictMode() && term.tk == tkID)
  7922. {
  7923. CheckStrictModeEvalArgumentsUsage(term.pid);
  7924. }
  7925. // This expression is not an identifier
  7926. term.tk = tkNone;
  7927. }
  7928. this->GetScanner()->Scan();
  7929. }
  7930. }
  7931. // Process a sequence of operators and operands.
  7932. for (;;)
  7933. {
  7934. if (!this->GetHashTbl()->TokIsBinop(m_token.tk, &opl, &nop) || nop == knopNone)
  7935. {
  7936. break;
  7937. }
  7938. if (!fAllowIn && nop == knopIn)
  7939. {
  7940. break;
  7941. }
  7942. Assert(opl != koplNo);
  7943. if (opl == koplAsg)
  7944. {
  7945. if (m_token.tk != tkDArrow)
  7946. {
  7947. // Assignment operator. These are the only right associative
  7948. // binary operators. We also need to special case the left
  7949. // operand - it should only be a LeftHandSideExpression.
  7950. Assert(ParseNode::Grfnop(nop) & fnopAsg || nop == knopFncDecl);
  7951. TrackAssignment<buildAST>(pnode, &term);
  7952. if (buildAST)
  7953. {
  7954. if (IsStrictMode() && pnode->nop == knopName)
  7955. {
  7956. CheckStrictModeEvalArgumentsUsage(pnode->AsParseNodeName()->pid);
  7957. }
  7958. // Assignment stmt of the form "this.<id> = <expr>"
  7959. if (nop == knopAsg
  7960. && pnode->nop == knopDot
  7961. && pnode->AsParseNodeBin()->pnode1->nop == knopName
  7962. && pnode->AsParseNodeBin()->pnode1->AsParseNodeName()->pid == wellKnownPropertyPids._this
  7963. && pnode->AsParseNodeBin()->pnode2->nop == knopName)
  7964. {
  7965. if (pnode->AsParseNodeBin()->pnode2->AsParseNodeName()->pid != wellKnownPropertyPids.__proto__)
  7966. {
  7967. assignmentStmt = true;
  7968. }
  7969. }
  7970. }
  7971. else
  7972. {
  7973. if (IsStrictMode() && term.tk == tkID)
  7974. {
  7975. CheckStrictModeEvalArgumentsUsage(term.pid);
  7976. }
  7977. }
  7978. }
  7979. if (opl < oplMin)
  7980. {
  7981. break;
  7982. }
  7983. if (m_token.tk != tkDArrow && !fCanAssign &&
  7984. (m_sourceContextInfo
  7985. ? !PHASE_OFF_RAW(Js::EarlyReferenceErrorsPhase, m_sourceContextInfo->sourceContextId, GetCurrentFunctionNode()->functionId)
  7986. : !PHASE_OFF1(Js::EarlyReferenceErrorsPhase)))
  7987. {
  7988. Error(ERRInvalidAsgTarget);
  7989. // No recovery necessary since this is a semantic, not structural, error.
  7990. }
  7991. }
  7992. else if (opl == koplExpo)
  7993. {
  7994. // ** operator is right associative
  7995. if (opl < oplMin)
  7996. {
  7997. break;
  7998. }
  7999. }
  8000. else if (opl <= oplMin)
  8001. {
  8002. break;
  8003. }
  8004. // This expression is not an identifier
  8005. term.tk = tkNone;
  8006. // Precedence is high enough. Consume the operator token.
  8007. this->GetScanner()->Scan();
  8008. fCanAssign = !!fLikelyPattern;
  8009. // Special case the "?:" operator
  8010. if (nop == knopQmark)
  8011. {
  8012. pnodeT = ParseExpr<buildAST>(koplAsg, NULL, TRUE);
  8013. ChkCurTok(tkColon, ERRnoColon);
  8014. ParseNodePtr pnodeT2 = ParseExpr<buildAST>(koplAsg, NULL, fAllowIn, 0, nullptr, nullptr, nullptr, nullptr, false, nullptr, plastRParen);
  8015. if (buildAST)
  8016. {
  8017. pnode = CreateTriNode(nop, pnode, pnodeT, pnodeT2);
  8018. this->CheckArguments(pnode->AsParseNodeTri()->pnode2);
  8019. this->CheckArguments(pnode->AsParseNodeTri()->pnode3);
  8020. }
  8021. }
  8022. else if (nop == knopFncDecl)
  8023. {
  8024. ushort flags = fFncLambda;
  8025. size_t iecpMin = 0;
  8026. bool isAsyncMethod = false;
  8027. RestoreStateFrom(&parserState);
  8028. this->GetScanner()->SeekTo(termStart);
  8029. if (CheckContextualKeyword(wellKnownPropertyPids.async) && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  8030. {
  8031. ichMin = this->GetScanner()->IchMinTok();
  8032. iecpMin = this->GetScanner()->IecpMinTok();
  8033. this->GetScanner()->Scan();
  8034. if ((m_token.tk == tkID || m_token.tk == tkLParen) && !this->GetScanner()->FHadNewLine())
  8035. {
  8036. flags |= fFncAsync;
  8037. isAsyncMethod = true;
  8038. }
  8039. else
  8040. {
  8041. this->GetScanner()->SeekTo(termStart);
  8042. }
  8043. }
  8044. pnode = ParseFncDeclNoCheckScope<buildAST>(flags, SuperRestrictionState::Disallowed, nullptr, /* needsPIDOnRCurlyScan = */false, /* fUnaryOrParen = */ false, fAllowIn);
  8045. if (isAsyncMethod)
  8046. {
  8047. pnode->AsParseNodeFnc()->cbStringMin = iecpMin;
  8048. pnode->AsParseNodeFnc()->cbStringLim = pnode->AsParseNodeFnc()->cbLim;
  8049. }
  8050. // ArrowFunction/AsyncArrowFunction is part of AssignmentExpression, which should terminate the expression unless followed by a comma
  8051. if (m_token.tk != tkComma && m_token.tk != tkIN)
  8052. {
  8053. if (!(IsTerminateToken(false)))
  8054. {
  8055. Error(ERRnoSemic);
  8056. }
  8057. break;
  8058. }
  8059. }
  8060. else // a binary operator
  8061. {
  8062. if (nop == knopComma && m_token.tk == tkRParen)
  8063. {
  8064. // Trailing comma
  8065. this->GetScanner()->Capture(&m_deferCommaErrorLoc);
  8066. m_deferCommaError = true;
  8067. break;
  8068. }
  8069. ParseNode* pnode1 = pnode;
  8070. // Parse the operand, make a new node, and look for more
  8071. IdentToken token;
  8072. bool coalescing = false;
  8073. ParseNode* pnode2 = ParseExpr<buildAST>(
  8074. opl, nullptr, fAllowIn, FALSE, pNameHint, &hintLength, &hintOffset, &token, false, nullptr, plastRParen, &coalescing);
  8075. if (nop == knopLogAnd || nop == knopLogOr)
  8076. {
  8077. if (localCoalesce || coalescing)
  8078. {
  8079. Error(ERRCoalesce);
  8080. }
  8081. }
  8082. else if (nop == knopCoalesce)
  8083. {
  8084. localCoalesce = true;
  8085. if (looseCoalesce != nullptr)
  8086. {
  8087. *looseCoalesce = true;
  8088. }
  8089. }
  8090. // Detect nested function escapes of the pattern "o.f = function(){...}" or "o[s] = function(){...}".
  8091. // Doing so in the parser allows us to disable stack-nested-functions in common cases where an escape
  8092. // is not detected at byte code gen time because of deferred parsing.
  8093. if (fIsDotOrIndex && nop == knopAsg)
  8094. {
  8095. this->MarkEscapingRef(pnodeT, &token);
  8096. }
  8097. if (buildAST)
  8098. {
  8099. Assert(pnode2 != nullptr);
  8100. if (pnode2->nop == knopFncDecl)
  8101. {
  8102. Assert(hintLength >= hintOffset);
  8103. pnode2->AsParseNodeFnc()->hint = pNameHint;
  8104. pnode2->AsParseNodeFnc()->hintLength = hintLength;
  8105. pnode2->AsParseNodeFnc()->hintOffset = hintOffset;
  8106. if (pnode1->nop == knopDot)
  8107. {
  8108. pnode2->AsParseNodeFnc()->isNameIdentifierRef = false;
  8109. }
  8110. else if (pnode1->nop == knopName)
  8111. {
  8112. PidRefStack *pidRef = pnode1->AsParseNodeName()->pid->GetTopRef();
  8113. pidRef->isFuncAssignment = true;
  8114. }
  8115. }
  8116. else if (pnode2->nop == knopClassDecl && pnode1->nop == knopDot)
  8117. {
  8118. Assert(pnode2->AsParseNodeClass()->pnodeConstructor);
  8119. if (!pnode2->AsParseNodeClass()->pnodeConstructor->pid)
  8120. {
  8121. pnode2->AsParseNodeClass()->pnodeConstructor->isNameIdentifierRef = false;
  8122. }
  8123. }
  8124. else if (pnode1->nop == knopName && nop == knopIn)
  8125. {
  8126. PidRefStack* pidRef = pnode1->AsParseNodeName()->pid->GetTopRef();
  8127. pidRef->SetIsUsedInLdElem(true);
  8128. }
  8129. pnode = CreateBinNode(nop, pnode1, pnode2);
  8130. }
  8131. pNameHint = nullptr;
  8132. }
  8133. }
  8134. if (buildAST)
  8135. {
  8136. if (!assignmentStmt)
  8137. {
  8138. // Don't set the flag for following nodes
  8139. switch (pnode->nop)
  8140. {
  8141. case knopName:
  8142. case knopInt:
  8143. case knopBigInt:
  8144. case knopFlt:
  8145. case knopStr:
  8146. case knopRegExp:
  8147. case knopNull:
  8148. case knopFalse:
  8149. case knopTrue:
  8150. break;
  8151. default:
  8152. if (m_currentNodeFunc)
  8153. {
  8154. m_currentNodeFunc->SetHasNonThisStmt();
  8155. }
  8156. else if (m_currentNodeProg)
  8157. {
  8158. m_currentNodeProg->SetHasNonThisStmt();
  8159. }
  8160. }
  8161. }
  8162. }
  8163. m_hasDeferredShorthandInitError = m_hasDeferredShorthandInitError || savedDeferredInitError;
  8164. if (NULL != pfCanAssign)
  8165. {
  8166. *pfCanAssign = fCanAssign;
  8167. }
  8168. // Pass back identifier if requested
  8169. if (pToken && term.tk == tkID)
  8170. {
  8171. *pToken = term;
  8172. }
  8173. //Track "obj.a" assignment patterns here - Promote the Assignment state for the property's PID.
  8174. // This includes =, += etc.
  8175. if (pnode != NULL)
  8176. {
  8177. uint nodeType = ParseNode::Grfnop(pnode->nop);
  8178. if (nodeType & fnopAsg)
  8179. {
  8180. if (nodeType & fnopBin)
  8181. {
  8182. ParseNodePtr lhs = pnode->AsParseNodeBin()->pnode1;
  8183. Assert(lhs);
  8184. if (lhs->nop == knopDot)
  8185. {
  8186. ParseNodePtr propertyNode = lhs->AsParseNodeBin()->pnode2;
  8187. if (propertyNode->nop == knopName)
  8188. {
  8189. propertyNode->AsParseNodeName()->pid->PromoteAssignmentState();
  8190. }
  8191. }
  8192. }
  8193. else if (nodeType & fnopUni)
  8194. {
  8195. // cases like obj.a++, ++obj.a
  8196. ParseNodePtr lhs = pnode->AsParseNodeUni()->pnode1;
  8197. if (lhs->nop == knopDot)
  8198. {
  8199. ParseNodePtr propertyNode = lhs->AsParseNodeBin()->pnode2;
  8200. if (propertyNode->nop == knopName)
  8201. {
  8202. propertyNode->AsParseNodeName()->pid->PromoteAssignmentState();
  8203. }
  8204. }
  8205. }
  8206. }
  8207. }
  8208. return pnode;
  8209. }
  8210. template<bool buildAST>
  8211. void Parser::TrackAssignment(ParseNodePtr pnodeT, IdentToken* pToken)
  8212. {
  8213. if (buildAST)
  8214. {
  8215. Assert(pnodeT != nullptr);
  8216. if (pnodeT->nop == knopName)
  8217. {
  8218. PidRefStack *ref = pnodeT->AsParseNodeName()->pid->GetTopRef();
  8219. Assert(ref);
  8220. ref->isAsg = true;
  8221. }
  8222. }
  8223. else
  8224. {
  8225. Assert(pToken != nullptr);
  8226. if (pToken->tk == tkID)
  8227. {
  8228. PidRefStack *ref = pToken->pid->GetTopRef();
  8229. Assert(ref);
  8230. ref->isAsg = true;
  8231. }
  8232. }
  8233. }
  8234. PidRefStack* Parser::PushPidRef(IdentPtr pid)
  8235. {
  8236. ParseNodeFnc* currentFnc = GetCurrentFunctionNode();
  8237. if (this->IsCreatingStateCache())
  8238. {
  8239. IdentPtrSet* capturedNames = currentFnc->EnsureCapturedNames(&m_nodeAllocator);
  8240. capturedNames->AddNew(pid);
  8241. }
  8242. if (PHASE_ON1(Js::ParallelParsePhase))
  8243. {
  8244. // NOTE: the phase check is here to protect perf. See OSG 1020424.
  8245. // In some LS AST-rewrite cases we lose a lot of perf searching the PID ref stack rather
  8246. // than just pushing on the top. This hasn't shown up as a perf issue in non-LS benchmarks.
  8247. return pid->FindOrAddPidRef(&m_nodeAllocator, GetCurrentBlock()->blockId, currentFnc->functionId);
  8248. }
  8249. Assert(GetCurrentBlock() != nullptr);
  8250. AssertMsg(pid != nullptr, "PID should be created");
  8251. PidRefStack *ref = pid->GetTopRef(m_nextBlockId - 1);
  8252. int blockId = GetCurrentBlock()->blockId;
  8253. int funcId = currentFnc->functionId;
  8254. if (!ref || (ref->GetScopeId() < blockId))
  8255. {
  8256. ref = Anew(&m_nodeAllocator, PidRefStack);
  8257. if (ref == nullptr)
  8258. {
  8259. Error(ERRnoMemory);
  8260. }
  8261. pid->PushPidRef(blockId, funcId, ref);
  8262. }
  8263. else if (m_reparsingLambdaParams)
  8264. {
  8265. // If we're reparsing params, then we may have pid refs left behind from the first pass. Make sure we're
  8266. // working with the right ref at this point.
  8267. ref = this->FindOrAddPidRef(pid, blockId, funcId);
  8268. // Fix up the function ID if we're reparsing lambda parameters.
  8269. ref->funcId = funcId;
  8270. }
  8271. return ref;
  8272. }
  8273. PidRefStack* Parser::FindOrAddPidRef(IdentPtr pid, int scopeId, Js::LocalFunctionId funcId)
  8274. {
  8275. PidRefStack *ref = pid->FindOrAddPidRef(&m_nodeAllocator, scopeId, funcId);
  8276. if (ref == NULL)
  8277. {
  8278. Error(ERRnoMemory);
  8279. }
  8280. return ref;
  8281. }
  8282. void Parser::RemovePrevPidRef(IdentPtr pid, PidRefStack *ref)
  8283. {
  8284. PidRefStack *prevRef = pid->RemovePrevPidRef(ref);
  8285. Assert(prevRef);
  8286. if (prevRef->GetSym() == nullptr)
  8287. {
  8288. AllocatorDelete(ArenaAllocator, &m_nodeAllocator, prevRef);
  8289. }
  8290. }
  8291. void Parser::SetPidRefsInScopeDynamic(IdentPtr pid, int blockId)
  8292. {
  8293. PidRefStack *ref = pid->GetTopRef();
  8294. while (ref && ref->GetScopeId() >= blockId)
  8295. {
  8296. ref->SetDynamicBinding();
  8297. ref = ref->prev;
  8298. }
  8299. }
  8300. ParseNodeBlock* Parser::GetFunctionBlock()
  8301. {
  8302. Assert(m_currentBlockInfo != nullptr);
  8303. return m_currentBlockInfo->pBlockInfoFunction->pnodeBlock;
  8304. }
  8305. ParseNodeBlock* Parser::GetCurrentBlock()
  8306. {
  8307. return m_currentBlockInfo != nullptr ? m_currentBlockInfo->pnodeBlock : nullptr;
  8308. }
  8309. BlockInfoStack* Parser::GetCurrentBlockInfo()
  8310. {
  8311. return m_currentBlockInfo;
  8312. }
  8313. BlockInfoStack* Parser::GetCurrentFunctionBlockInfo()
  8314. {
  8315. return m_currentBlockInfo->pBlockInfoFunction;
  8316. }
  8317. /***************************************************************************
  8318. Parse a variable declaration.
  8319. 'fAllowIn' indicates if the 'in' operator should be allowed in the initializing
  8320. expression ( it is not allowed in the context of the first expression in a 'for' loop).
  8321. ***************************************************************************/
  8322. template<bool buildAST>
  8323. ParseNodePtr Parser::ParseVariableDeclaration(
  8324. tokens declarationType, charcount_t ichMin,
  8325. BOOL fAllowIn/* = TRUE*/,
  8326. BOOL* pfForInOk/* = nullptr*/,
  8327. BOOL singleDefOnly/* = FALSE*/,
  8328. BOOL allowInit/* = TRUE*/,
  8329. BOOL isTopVarParse/* = TRUE*/,
  8330. BOOL isFor/* = FALSE*/,
  8331. BOOL* nativeForOk /*= nullptr*/)
  8332. {
  8333. ParseNodePtr pnodeThis = nullptr;
  8334. ParseNodePtr pnodeInit;
  8335. ParseNodePtr pnodeList = nullptr;
  8336. ParseNodePtr *lastNodeRef = nullptr;
  8337. LPCOLESTR pNameHint = nullptr;
  8338. uint32 nameHintLength = 0;
  8339. uint32 nameHintOffset = 0;
  8340. Assert(declarationType == tkVAR || declarationType == tkCONST || declarationType == tkLET);
  8341. for (;;)
  8342. {
  8343. if (IsPossiblePatternStart())
  8344. {
  8345. pnodeThis = ParseDestructuredLiteral<buildAST>(declarationType, true, !!isTopVarParse, DIC_None, !!fAllowIn, pfForInOk, nativeForOk);
  8346. if (pnodeThis != nullptr)
  8347. {
  8348. pnodeThis->ichMin = ichMin;
  8349. pnodeThis->SetIsPatternDeclaration();
  8350. }
  8351. }
  8352. else
  8353. {
  8354. if (m_token.tk != tkID)
  8355. {
  8356. IdentifierExpectedError(m_token);
  8357. }
  8358. IdentPtr pid = m_token.GetIdentifier(this->GetHashTbl());
  8359. Assert(pid);
  8360. pNameHint = pid->Psz();
  8361. nameHintLength = pid->Cch();
  8362. nameHintOffset = 0;
  8363. if (pid == wellKnownPropertyPids.let && (declarationType == tkCONST || declarationType == tkLET))
  8364. {
  8365. Error(ERRLetIDInLexicalDecl, pnodeThis);
  8366. }
  8367. if (declarationType == tkVAR)
  8368. {
  8369. pnodeThis = CreateVarDeclNode(pid, STVariable);
  8370. }
  8371. else if (declarationType == tkCONST)
  8372. {
  8373. pnodeThis = CreateBlockScopedDeclNode(pid, knopConstDecl);
  8374. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Const, m_scriptContext);
  8375. }
  8376. else
  8377. {
  8378. pnodeThis = CreateBlockScopedDeclNode(pid, knopLetDecl);
  8379. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Let, m_scriptContext);
  8380. }
  8381. if (pid == wellKnownPropertyPids.arguments)
  8382. {
  8383. // This var declaration may change the way an 'arguments' identifier in the function is resolved
  8384. if (declarationType == tkVAR)
  8385. {
  8386. GetCurrentFunctionNode()->grfpn |= PNodeFlags::fpnArguments_varDeclaration;
  8387. }
  8388. else
  8389. {
  8390. if (GetCurrentBlockInfo()->pnodeBlock->blockType == Function)
  8391. {
  8392. // Only override arguments if we are at the function block level.
  8393. GetCurrentFunctionNode()->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
  8394. }
  8395. }
  8396. }
  8397. if (pnodeThis)
  8398. {
  8399. pnodeThis->ichMin = ichMin;
  8400. }
  8401. this->GetScanner()->Scan();
  8402. if (m_token.tk == tkAsg)
  8403. {
  8404. if (!allowInit)
  8405. {
  8406. Error(ERRUnexpectedDefault);
  8407. }
  8408. if (pfForInOk && (declarationType == tkLET || declarationType == tkCONST || IsStrictMode()))
  8409. {
  8410. *pfForInOk = FALSE;
  8411. }
  8412. this->GetScanner()->Scan();
  8413. pnodeInit = ParseExpr<buildAST>(koplCma, nullptr, fAllowIn, FALSE, pNameHint, &nameHintLength, &nameHintOffset);
  8414. if (buildAST)
  8415. {
  8416. AnalysisAssert(pnodeThis);
  8417. pnodeThis->AsParseNodeVar()->pnodeInit = pnodeInit;
  8418. pnodeThis->ichLim = pnodeInit->ichLim;
  8419. if (pnodeInit->nop == knopFncDecl)
  8420. {
  8421. Assert(nameHintLength >= nameHintOffset);
  8422. pnodeInit->AsParseNodeFnc()->hint = pNameHint;
  8423. pnodeInit->AsParseNodeFnc()->hintLength = nameHintLength;
  8424. pnodeInit->AsParseNodeFnc()->hintOffset = nameHintOffset;
  8425. pnodeThis->AsParseNodeVar()->pid->GetTopRef()->isFuncAssignment = true;
  8426. }
  8427. else
  8428. {
  8429. this->CheckArguments(pnodeInit);
  8430. }
  8431. pNameHint = nullptr;
  8432. }
  8433. //Track var a =, let a= , const a =
  8434. // This is for FixedFields Constant Heuristics
  8435. if (pnodeThis && pnodeThis->AsParseNodeVar()->pnodeInit != nullptr)
  8436. {
  8437. pnodeThis->AsParseNodeVar()->sym->PromoteAssignmentState();
  8438. }
  8439. }
  8440. else if (declarationType == tkCONST /*pnodeThis->nop == knopConstDecl*/
  8441. && !singleDefOnly
  8442. && !(isFor && TokIsForInOrForOf()))
  8443. {
  8444. Error(ERRUninitializedConst);
  8445. }
  8446. if (m_currentNodeFunc && pnodeThis && pnodeThis->AsParseNodeVar()->sym->GetIsFormal())
  8447. {
  8448. m_currentNodeFunc->SetHasAnyWriteToFormals(true);
  8449. }
  8450. }
  8451. if (singleDefOnly)
  8452. {
  8453. return pnodeThis;
  8454. }
  8455. if (buildAST)
  8456. {
  8457. AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeThis);
  8458. }
  8459. if (m_token.tk != tkComma)
  8460. {
  8461. return pnodeList;
  8462. }
  8463. if (pfForInOk)
  8464. {
  8465. // don't allow "for (var a, b in c)"
  8466. *pfForInOk = FALSE;
  8467. }
  8468. this->GetScanner()->Scan();
  8469. ichMin = this->GetScanner()->IchMinTok();
  8470. }
  8471. }
  8472. /***************************************************************************
  8473. Parse try-catch-finally statement
  8474. ***************************************************************************/
  8475. // The try-catch-finally tree nests the try-catch within a try-finally.
  8476. // This matches the new runtime implementation.
  8477. template<bool buildAST>
  8478. ParseNodeStmt * Parser::ParseTryCatchFinally()
  8479. {
  8480. this->m_tryCatchOrFinallyDepth++;
  8481. ParseNodeTry * pnodeT = ParseTry<buildAST>();
  8482. ParseNodeTryCatch * pnodeTC = nullptr;
  8483. StmtNest stmt;
  8484. bool hasCatch = false;
  8485. if (tkCATCH == m_token.tk)
  8486. {
  8487. hasCatch = true;
  8488. if (buildAST)
  8489. {
  8490. pnodeTC = CreateNodeForOpT<knopTryCatch>();
  8491. pnodeTC->pnodeTry = pnodeT;
  8492. }
  8493. PushStmt<buildAST>(&stmt, pnodeTC, knopTryCatch, nullptr);
  8494. ParseNodeCatch * pnodeCatch = ParseCatch<buildAST>();
  8495. if (buildAST)
  8496. {
  8497. pnodeTC->pnodeCatch = pnodeCatch;
  8498. }
  8499. PopStmt(&stmt);
  8500. }
  8501. if (tkFINALLY != m_token.tk)
  8502. {
  8503. if (!hasCatch)
  8504. {
  8505. Error(ERRnoCatch);
  8506. }
  8507. Assert(!buildAST || pnodeTC);
  8508. this->m_tryCatchOrFinallyDepth--;
  8509. return pnodeTC;
  8510. }
  8511. ParseNodeTryFinally * pnodeTF = nullptr;
  8512. if (buildAST)
  8513. {
  8514. pnodeTF = CreateNodeForOpT<knopTryFinally>();
  8515. }
  8516. PushStmt<buildAST>(&stmt, pnodeTF, knopTryFinally, nullptr);
  8517. ParseNodeFinally * pnodeFinally = ParseFinally<buildAST>();
  8518. if (buildAST)
  8519. {
  8520. if (!hasCatch)
  8521. {
  8522. pnodeTF->pnodeTry = pnodeT;
  8523. }
  8524. else
  8525. {
  8526. pnodeTF->pnodeTry = CreateNodeForOpT<knopTry>();
  8527. pnodeTF->pnodeTry->pnodeBody = pnodeTC;
  8528. }
  8529. pnodeTF->pnodeFinally = pnodeFinally;
  8530. }
  8531. PopStmt(&stmt);
  8532. this->m_tryCatchOrFinallyDepth--;
  8533. return pnodeTF;
  8534. }
  8535. template<bool buildAST>
  8536. ParseNodeTry * Parser::ParseTry()
  8537. {
  8538. ParseNodeTry * pnode = nullptr;
  8539. StmtNest stmt;
  8540. Assert(tkTRY == m_token.tk);
  8541. if (buildAST)
  8542. {
  8543. pnode = CreateNodeForOpT<knopTry>();
  8544. }
  8545. this->GetScanner()->Scan();
  8546. if (tkLCurly != m_token.tk)
  8547. {
  8548. Error(ERRnoLcurly);
  8549. }
  8550. PushStmt<buildAST>(&stmt, pnode, knopTry, nullptr);
  8551. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  8552. if (buildAST)
  8553. {
  8554. pnode->pnodeBody = pnodeBody;
  8555. if (pnode->pnodeBody)
  8556. pnode->ichLim = pnode->pnodeBody->ichLim;
  8557. }
  8558. PopStmt(&stmt);
  8559. return pnode;
  8560. }
  8561. template<bool buildAST>
  8562. ParseNodeFinally * Parser::ParseFinally()
  8563. {
  8564. ParseNodeFinally * pnode = nullptr;
  8565. StmtNest stmt;
  8566. Assert(tkFINALLY == m_token.tk);
  8567. if (buildAST)
  8568. {
  8569. pnode = CreateNodeForOpT<knopFinally>();
  8570. }
  8571. this->GetScanner()->Scan();
  8572. if (tkLCurly != m_token.tk)
  8573. {
  8574. Error(ERRnoLcurly);
  8575. }
  8576. PushStmt<buildAST>(&stmt, pnode, knopFinally, nullptr);
  8577. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  8578. if (buildAST)
  8579. {
  8580. pnode->pnodeBody = pnodeBody;
  8581. if (!pnode->pnodeBody)
  8582. // Will only occur due to error correction.
  8583. pnode->pnodeBody = CreateNodeForOpT<knopEmpty>();
  8584. else
  8585. pnode->ichLim = pnode->pnodeBody->ichLim;
  8586. }
  8587. PopStmt(&stmt);
  8588. return pnode;
  8589. }
  8590. template<bool buildAST>
  8591. ParseNodeCatch * Parser::ParseCatch()
  8592. {
  8593. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  8594. ParseNodeCatch * pnode = nullptr;
  8595. ParseNodeBlock * pnodeCatchScope = nullptr;
  8596. StmtNest stmt;
  8597. IdentPtr pidCatch = nullptr;
  8598. if (tkCATCH == m_token.tk)
  8599. {
  8600. charcount_t ichMin;
  8601. if (buildAST)
  8602. {
  8603. ichMin = this->GetScanner()->IchMinTok();
  8604. }
  8605. this->GetScanner()->Scan(); //catch
  8606. bool isPattern = false;
  8607. bool hasParam = false;
  8608. if (tkLParen == m_token.tk)
  8609. {
  8610. hasParam = true;
  8611. this->GetScanner()->Scan(); //catch(
  8612. if (tkID != m_token.tk)
  8613. {
  8614. isPattern = IsPossiblePatternStart();
  8615. if (!isPattern)
  8616. {
  8617. IdentifierExpectedError(m_token);
  8618. }
  8619. }
  8620. }
  8621. if (buildAST)
  8622. {
  8623. pnode = CreateNodeForOpT<knopCatch>(ichMin);
  8624. pnode->pnodeNext = nullptr;
  8625. PushStmt<buildAST>(&stmt, pnode, knopCatch, nullptr);
  8626. }
  8627. pnodeCatchScope = StartParseBlock<buildAST>(PnodeBlockType::Regular, isPattern ? ScopeType_CatchParamPattern : ScopeType_Catch);
  8628. if (buildAST)
  8629. {
  8630. // Add this catch to the current scope list.
  8631. if (m_ppnodeExprScope)
  8632. {
  8633. Assert(*m_ppnodeExprScope == nullptr);
  8634. *m_ppnodeExprScope = pnode;
  8635. m_ppnodeExprScope = &pnode->pnodeNext;
  8636. }
  8637. else
  8638. {
  8639. Assert(m_ppnodeScope);
  8640. Assert(*m_ppnodeScope == nullptr);
  8641. *m_ppnodeScope = pnode;
  8642. m_ppnodeScope = &pnode->pnodeNext;
  8643. }
  8644. // Keep a list of function expressions (not declarations) at this scope.
  8645. ppnodeExprScopeSave = m_ppnodeExprScope;
  8646. m_ppnodeExprScope = &pnode->pnodeScopes;
  8647. pnode->pnodeScopes = nullptr;
  8648. }
  8649. if (isPattern)
  8650. {
  8651. ParseNodePtr pnodePattern = ParseDestructuredLiteral<buildAST>(tkLET, true /*isDecl*/, true /*topLevel*/, DIC_ForceErrorOnInitializer);
  8652. if (buildAST)
  8653. {
  8654. pnode->SetParam(CreateParamPatternNode(pnodePattern));
  8655. Scope *scope = pnodeCatchScope->scope;
  8656. pnode->scope = scope;
  8657. }
  8658. }
  8659. else if (hasParam)
  8660. {
  8661. if (IsStrictMode())
  8662. {
  8663. IdentPtr pid = m_token.GetIdentifier(this->GetHashTbl());
  8664. if (pid == wellKnownPropertyPids.eval)
  8665. {
  8666. Error(ERREvalUsage);
  8667. }
  8668. else if (pid == wellKnownPropertyPids.arguments)
  8669. {
  8670. Error(ERRArgsUsage);
  8671. }
  8672. }
  8673. pidCatch = m_token.GetIdentifier(this->GetHashTbl());
  8674. PidRefStack *ref = this->FindOrAddPidRef(pidCatch, GetCurrentBlock()->blockId, GetCurrentFunctionNode()->functionId);
  8675. ParseNodeName * pnodeParam = CreateNameNode(pidCatch);
  8676. pnodeParam->SetSymRef(ref);
  8677. const char16 *name = reinterpret_cast<const char16*>(pidCatch->Psz());
  8678. int nameLength = pidCatch->Cch();
  8679. SymbolName const symName(name, nameLength);
  8680. Symbol *sym = Anew(&m_nodeAllocator, Symbol, symName, pnodeParam, STVariable);
  8681. if (sym == nullptr)
  8682. {
  8683. Error(ERRnoMemory);
  8684. }
  8685. sym->SetPid(pidCatch);
  8686. Assert(ref->GetSym() == nullptr);
  8687. ref->SetSym(sym);
  8688. Scope *scope = pnodeCatchScope->scope;
  8689. scope->AddNewSymbol(sym);
  8690. if (buildAST)
  8691. {
  8692. pnode->SetParam(pnodeParam);
  8693. pnode->scope = scope;
  8694. }
  8695. this->GetScanner()->Scan();
  8696. }
  8697. else
  8698. {
  8699. if (buildAST)
  8700. {
  8701. pnode->scope = pnodeCatchScope->scope;
  8702. }
  8703. }
  8704. charcount_t ichLim;
  8705. if (buildAST)
  8706. {
  8707. ichLim = this->GetScanner()->IchLimTok();
  8708. }
  8709. if (hasParam)
  8710. {
  8711. ChkCurTok(tkRParen, ERRnoRparen); //catch(id[:expr])
  8712. }
  8713. if (tkLCurly != m_token.tk)
  8714. {
  8715. Error(ERRnoLcurly);
  8716. }
  8717. ParseNodePtr pnodeBody = ParseStatement<buildAST>(); //catch(id[:expr]) {block}
  8718. if (buildAST)
  8719. {
  8720. pnode->pnodeBody = pnodeBody;
  8721. pnode->ichLim = ichLim;
  8722. }
  8723. if (pnodeCatchScope != nullptr)
  8724. {
  8725. FinishParseBlock(pnodeCatchScope);
  8726. }
  8727. if (pnodeCatchScope->GetCallsEval() || pnodeCatchScope->GetChildCallsEval())
  8728. {
  8729. GetCurrentBlock()->SetChildCallsEval(true);
  8730. }
  8731. if (pnodeCatchScope->GetCallsEval())
  8732. {
  8733. pnodeBody->AsParseNodeBlock()->SetCallsEval(true);
  8734. }
  8735. if (pnodeCatchScope->GetChildCallsEval())
  8736. {
  8737. pnodeBody->AsParseNodeBlock()->SetChildCallsEval(true);
  8738. }
  8739. if (buildAST)
  8740. {
  8741. PopStmt(&stmt);
  8742. // Restore the lists of function expression scopes.
  8743. Assert(m_ppnodeExprScope);
  8744. Assert(*m_ppnodeExprScope == nullptr);
  8745. m_ppnodeExprScope = ppnodeExprScopeSave;
  8746. }
  8747. }
  8748. return pnode;
  8749. }
  8750. template<bool buildAST>
  8751. ParseNodeCase * Parser::ParseCase(ParseNodePtr *ppnodeBody)
  8752. {
  8753. ParseNodeCase * pnodeT = nullptr;
  8754. charcount_t ichMinT = this->GetScanner()->IchMinTok();
  8755. this->GetScanner()->Scan();
  8756. ParseNodePtr pnodeExpr = ParseExpr<buildAST>();
  8757. charcount_t ichLim = this->GetScanner()->IchLimTok();
  8758. ChkCurTok(tkColon, ERRnoColon);
  8759. if (buildAST)
  8760. {
  8761. pnodeT = CreateNodeForOpT<knopCase>(ichMinT);
  8762. pnodeT->pnodeExpr = pnodeExpr;
  8763. pnodeT->ichLim = ichLim;
  8764. }
  8765. ParseStmtList<buildAST>(ppnodeBody);
  8766. return pnodeT;
  8767. }
  8768. /***************************************************************************
  8769. Parse a single statement. Digest a trailing semicolon.
  8770. ***************************************************************************/
  8771. template<bool buildAST>
  8772. ParseNodePtr Parser::ParseStatement()
  8773. {
  8774. ParseNodePtr pnode = nullptr;
  8775. LabelId* pLabelIdList = nullptr;
  8776. charcount_t ichMin = 0;
  8777. size_t iecpMin = 0;
  8778. StmtNest stmt;
  8779. StmtNest *pstmt;
  8780. BOOL fForInOrOfOkay;
  8781. BOOL fCanAssign;
  8782. IdentPtr pid;
  8783. uint fnop;
  8784. bool expressionStmt = false;
  8785. bool isAsyncMethod = false;
  8786. bool labelledStatement = false;
  8787. tokens tok;
  8788. #if EXCEPTION_RECOVERY
  8789. ParseNodeTryCatch * pParentTryCatch = nullptr;
  8790. ParseNodeBlock * pTryBlock = nullptr;
  8791. ParseNodeTry * pTry = nullptr;
  8792. ParseNodeBlock * pParentTryCatchBlock = nullptr;
  8793. StmtNest stmtTryCatchBlock;
  8794. StmtNest stmtTryCatch;
  8795. StmtNest stmtTry;
  8796. StmtNest stmtTryBlock;
  8797. #endif
  8798. if (buildAST)
  8799. {
  8800. #if EXCEPTION_RECOVERY
  8801. if (Js::Configuration::Global.flags.SwallowExceptions)
  8802. {
  8803. // If we're swallowing exceptions, surround this statement with a try/catch block:
  8804. //
  8805. // Before: x.y = 3;
  8806. // After: try { x.y = 3; } catch(__ehobj) { }
  8807. //
  8808. // This is done to force the runtime to recover from exceptions at the most granular
  8809. // possible point. Recovering from EH dramatically improves coverage of testing via
  8810. // fault injection.
  8811. // create and push the try-catch node
  8812. pParentTryCatchBlock = CreateBlockNode();
  8813. PushStmt<buildAST>(&stmtTryCatchBlock, pParentTryCatchBlock, knopBlock, nullptr);
  8814. pParentTryCatch = CreateNodeForOpT<knopTryCatch>();
  8815. PushStmt<buildAST>(&stmtTryCatch, pParentTryCatch, knopTryCatch, nullptr);
  8816. // create and push a try node
  8817. pTry = CreateNodeForOpT<knopTry>();
  8818. PushStmt<buildAST>(&stmtTry, pTry, knopTry, nullptr);
  8819. pTryBlock = CreateBlockNode();
  8820. PushStmt<buildAST>(&stmtTryBlock, pTryBlock, knopBlock, nullptr);
  8821. // these nodes will be closed after the statement is parsed.
  8822. }
  8823. #endif // EXCEPTION_RECOVERY
  8824. }
  8825. EnsureStackAvailable();
  8826. LRestart:
  8827. tok = m_token.tk;
  8828. switch (tok)
  8829. {
  8830. case tkEOF:
  8831. if (labelledStatement)
  8832. {
  8833. Error(ERRLabelFollowedByEOF);
  8834. }
  8835. if (buildAST)
  8836. {
  8837. pnode = nullptr;
  8838. }
  8839. break;
  8840. case tkFUNCTION:
  8841. {
  8842. LFunctionStatement:
  8843. if (m_grfscr & fscrDeferredFncExpression)
  8844. {
  8845. // The top-level deferred function body was defined by a function expression whose parsing was deferred. We are now
  8846. // parsing it, so unset the flag so that any nested functions are parsed normally. This flag is only applicable the
  8847. // first time we see it.
  8848. m_grfscr &= ~fscrDeferredFncExpression;
  8849. pnode = ParseFncDeclNoCheckScope<buildAST>(isAsyncMethod ? fFncAsync : fFncNoFlgs);
  8850. }
  8851. else
  8852. {
  8853. pnode = ParseFncDeclCheckScope<buildAST>(fFncDeclaration | (isAsyncMethod ? fFncAsync : fFncNoFlgs));
  8854. }
  8855. Assert(pnode != nullptr);
  8856. if (labelledStatement)
  8857. {
  8858. if (IsStrictMode())
  8859. {
  8860. Error(ERRFunctionAfterLabelInStrict);
  8861. }
  8862. // #sec-with-statement-static-semantics-early-errors states that the Statement of
  8863. // a WithStatement throws a Syntax Error if the Statement is a LabelledFunction.
  8864. else if (m_pstmtCur && m_pstmtCur->pnodeStmt && m_pstmtCur->GetNop() == knopWith)
  8865. {
  8866. Error(ERRStmtOfWithIsLabelledFunc);
  8867. }
  8868. ParseNodeFnc* pNodeFnc = nullptr;
  8869. // pnode can be a knopBlock due to ParseFncDeclCheckScope, which
  8870. // can return a ParseNodeBlock that contains a ParseNodeFnc.
  8871. if (pnode->nop == knopBlock)
  8872. {
  8873. ParseNodeBlock* pNodeBlock = pnode->AsParseNodeBlock();
  8874. if (pNodeBlock->pnodeStmt && pNodeBlock->pnodeStmt->nop == knopFncDecl)
  8875. {
  8876. pNodeFnc = pNodeBlock->pnodeStmt->AsParseNodeFnc();
  8877. }
  8878. }
  8879. if (pNodeFnc == nullptr)
  8880. {
  8881. pNodeFnc = pnode->AsParseNodeFnc();
  8882. }
  8883. if (pNodeFnc->IsAsync())
  8884. {
  8885. Error(ERRLabelBeforeAsyncFncDeclaration);
  8886. }
  8887. else if (pNodeFnc->IsGenerator())
  8888. {
  8889. Error(ERRLabelBeforeGeneratorDeclaration);
  8890. }
  8891. }
  8892. if (isAsyncMethod)
  8893. {
  8894. pnode->AsParseNodeFnc()->cbStringMin = iecpMin;
  8895. pnode->AsParseNodeFnc()->cbStringLim = pnode->AsParseNodeFnc()->cbLim;
  8896. }
  8897. break;
  8898. }
  8899. case tkCLASS:
  8900. if (labelledStatement)
  8901. {
  8902. Error(ERRLabelBeforeClassDeclaration);
  8903. }
  8904. else
  8905. {
  8906. pnode = ParseClassDecl<buildAST>(TRUE, nullptr, nullptr, nullptr);
  8907. }
  8908. break;
  8909. case tkID:
  8910. case tkLET:
  8911. if (tok == tkLET || CheckContextualKeyword(wellKnownPropertyPids.let))
  8912. {
  8913. // We see "let" at the start of a statement. This could either be a declaration or an identifier
  8914. // reference. The next token determines which.
  8915. RestorePoint parsedLet;
  8916. this->GetScanner()->Capture(&parsedLet);
  8917. ichMin = this->GetScanner()->IchMinTok();
  8918. this->GetScanner()->Scan();
  8919. if (labelledStatement)
  8920. {
  8921. if (!this->GetScanner()->FHadNewLine() || m_token.tk == tkLBrack)
  8922. {
  8923. // In the case where a label is followed by a let, we want to fail when parsing if there is no new line after let,
  8924. // otherwise fail at runtime as let will be viewed as undefined. A left bracket after a let signifies a syntax error regardless.
  8925. Error(ERRLabelBeforeLexicalDeclaration);
  8926. }
  8927. }
  8928. else if (this->NextTokenConfirmsLetDecl())
  8929. {
  8930. pnode = ParseVariableDeclaration<buildAST>(tkLET, ichMin);
  8931. goto LNeedTerminator;
  8932. }
  8933. this->GetScanner()->SeekTo(parsedLet);
  8934. }
  8935. else if (CheckContextualKeyword(wellKnownPropertyPids.async) && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
  8936. {
  8937. RestorePoint parsedAsync;
  8938. this->GetScanner()->Capture(&parsedAsync);
  8939. ichMin = this->GetScanner()->IchMinTok();
  8940. iecpMin = this->GetScanner()->IecpMinTok();
  8941. this->GetScanner()->Scan();
  8942. if (m_token.tk == tkFUNCTION && !this->GetScanner()->FHadNewLine())
  8943. {
  8944. isAsyncMethod = true;
  8945. goto LFunctionStatement;
  8946. }
  8947. this->GetScanner()->SeekTo(parsedAsync);
  8948. }
  8949. goto LDefaultToken;
  8950. case tkCONST:
  8951. if (labelledStatement)
  8952. {
  8953. Error(ERRLabelBeforeLexicalDeclaration);
  8954. }
  8955. ichMin = this->GetScanner()->IchMinTok();
  8956. this->GetScanner()->Scan();
  8957. pnode = ParseVariableDeclaration<buildAST>(tok, ichMin);
  8958. goto LNeedTerminator;
  8959. case tkVAR:
  8960. ichMin = this->GetScanner()->IchMinTok();
  8961. this->GetScanner()->Scan();
  8962. pnode = ParseVariableDeclaration<buildAST>(tok, ichMin);
  8963. goto LNeedTerminator;
  8964. case tkFOR:
  8965. {
  8966. ParseNodeBlock * pnodeBlock = nullptr;
  8967. ParseNodePtr *ppnodeScopeSave = nullptr;
  8968. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  8969. bool isForAwait = false;
  8970. ichMin = this->GetScanner()->IchMinTok();
  8971. this->GetScanner()->Scan();
  8972. if (m_token.tk == tkAWAIT || CheckContextualKeyword(wellKnownPropertyPids.await))
  8973. {
  8974. if (!this->GetScanner()->AwaitIsKeywordRegion())
  8975. {
  8976. if (IsTopLevelModuleFunc())
  8977. {
  8978. MakeModuleAsync();
  8979. }
  8980. else
  8981. {
  8982. Error(ERRBadAwait); // for await () in a non-async function
  8983. }
  8984. }
  8985. if (!m_scriptContext->GetConfig()->IsES2018AsyncIterationEnabled())
  8986. {
  8987. Error(ERRExperimental);
  8988. }
  8989. isForAwait = true;
  8990. this->GetScanner()->Scan();
  8991. }
  8992. ChkCurTok(tkLParen, ERRnoLparen);
  8993. pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
  8994. if (buildAST)
  8995. {
  8996. PushFuncBlockScope(pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
  8997. }
  8998. RestorePoint startExprOrIdentifier;
  8999. fForInOrOfOkay = TRUE;
  9000. fCanAssign = TRUE;
  9001. tok = m_token.tk;
  9002. BOOL nativeForOkay = TRUE;
  9003. ParseNodePtr pnodeT;
  9004. switch (tok)
  9005. {
  9006. case tkID:
  9007. if (CheckContextualKeyword(wellKnownPropertyPids.let))
  9008. {
  9009. // We see "let" in the init part of a for loop. This could either be a declaration or an identifier
  9010. // reference. The next token determines which.
  9011. RestorePoint parsedLet;
  9012. this->GetScanner()->Capture(&parsedLet);
  9013. auto ichMinInner = this->GetScanner()->IchMinTok();
  9014. this->GetScanner()->Scan();
  9015. if (IsPossiblePatternStart())
  9016. {
  9017. this->GetScanner()->Capture(&startExprOrIdentifier);
  9018. }
  9019. if (this->NextTokenConfirmsLetDecl() && m_token.tk != tkIN)
  9020. {
  9021. pnodeT = ParseVariableDeclaration<buildAST>(tkLET, ichMinInner
  9022. , /*fAllowIn = */FALSE
  9023. , /*pfForInOk = */&fForInOrOfOkay
  9024. , /*singleDefOnly*/FALSE
  9025. , /*allowInit*/TRUE
  9026. , /*isTopVarParse*/TRUE
  9027. , /*isFor*/TRUE
  9028. , &nativeForOkay);
  9029. break;
  9030. }
  9031. this->GetScanner()->SeekTo(parsedLet);
  9032. }
  9033. goto LDefaultTokenFor;
  9034. case tkLET:
  9035. case tkCONST:
  9036. case tkVAR:
  9037. {
  9038. auto ichMinInner = this->GetScanner()->IchMinTok();
  9039. this->GetScanner()->Scan();
  9040. if (IsPossiblePatternStart())
  9041. {
  9042. this->GetScanner()->Capture(&startExprOrIdentifier);
  9043. }
  9044. pnodeT = ParseVariableDeclaration<buildAST>(tok, ichMinInner
  9045. , /*fAllowIn = */FALSE
  9046. , /*pfForInOk = */&fForInOrOfOkay
  9047. , /*singleDefOnly*/FALSE
  9048. , /*allowInit*/TRUE
  9049. , /*isTopVarParse*/TRUE
  9050. , /*isFor*/TRUE
  9051. , &nativeForOkay);
  9052. }
  9053. break;
  9054. case tkSColon:
  9055. pnodeT = nullptr;
  9056. fForInOrOfOkay = FALSE;
  9057. break;
  9058. default:
  9059. {
  9060. LDefaultTokenFor:
  9061. RestorePoint exprStart;
  9062. tokens beforeToken = tok;
  9063. this->GetScanner()->Capture(&exprStart);
  9064. if (IsPossiblePatternStart())
  9065. {
  9066. this->GetScanner()->Capture(&startExprOrIdentifier);
  9067. }
  9068. bool fLikelyPattern = false;
  9069. if (beforeToken == tkLBrack || beforeToken == tkLCurly)
  9070. {
  9071. pnodeT = ParseExpr<buildAST>(koplNo,
  9072. &fCanAssign,
  9073. /*fAllowIn = */FALSE,
  9074. /*fAllowEllipsis*/FALSE,
  9075. /*pHint*/nullptr,
  9076. /*pHintLength*/nullptr,
  9077. /*pShortNameOffset*/nullptr,
  9078. /*pToken*/nullptr,
  9079. /**fUnaryOrParen*/false,
  9080. &fLikelyPattern);
  9081. }
  9082. else
  9083. {
  9084. IdentToken token;
  9085. pnodeT = ParseExpr<buildAST>(koplNo, &fCanAssign, /*fAllowIn = */FALSE, FALSE, NULL, nullptr, nullptr, &token);
  9086. TrackAssignment<buildAST>(pnodeT, &token);
  9087. }
  9088. // We would veryfiy the grammar as destructuring grammar only when for..in/of case. As in the native for loop case the above ParseExpr call
  9089. // has already converted them appropriately.
  9090. if (fLikelyPattern && TokIsForInOrForOf())
  9091. {
  9092. this->GetScanner()->SeekTo(exprStart);
  9093. ParseDestructuredLiteralWithScopeSave(tkNone, false/*isDecl*/, false /*topLevel*/, DIC_None, false /*allowIn*/);
  9094. fCanAssign = TRUE;
  9095. if (buildAST)
  9096. {
  9097. pnodeT = ConvertToPattern(pnodeT);
  9098. }
  9099. }
  9100. if (buildAST)
  9101. {
  9102. Assert(pnodeT);
  9103. pnodeT->isUsed = false;
  9104. }
  9105. }
  9106. break;
  9107. }
  9108. if (TokIsForInOrForOf())
  9109. {
  9110. bool isForOf = (m_token.tk != tkIN);
  9111. Assert(!isForOf || CheckContextualKeyword(wellKnownPropertyPids.of));
  9112. if (isForAwait && !isForOf)
  9113. {
  9114. Error(ERRTokenAfter, _u("in"), _u("for await"));
  9115. }
  9116. if ((buildAST && nullptr == pnodeT) || !fForInOrOfOkay)
  9117. {
  9118. if (isForOf)
  9119. {
  9120. Error(ERRForOfNoInitAllowed);
  9121. }
  9122. else
  9123. {
  9124. Error(ERRForInNoInitAllowed);
  9125. }
  9126. }
  9127. if (!fCanAssign &&
  9128. (m_sourceContextInfo
  9129. ? !PHASE_OFF_RAW(Js::EarlyReferenceErrorsPhase, m_sourceContextInfo->sourceContextId, GetCurrentFunctionNode()->functionId)
  9130. : !PHASE_OFF1(Js::EarlyReferenceErrorsPhase)))
  9131. {
  9132. Error(ERRInvalidLHSInFor);
  9133. }
  9134. this->GetScanner()->Scan();
  9135. ParseNodePtr pnodeObj = ParseExpr<buildAST>(isForOf ? koplCma : koplNo);
  9136. charcount_t ichLim = this->GetScanner()->IchLimTok();
  9137. ChkCurTok(tkRParen, ERRnoRparen);
  9138. ParseNodeForInOrForOf * pnodeForInOrForOf = nullptr;
  9139. if (buildAST)
  9140. {
  9141. if (isForAwait)
  9142. {
  9143. pnodeForInOrForOf = CreateNodeForOpT<knopForAwaitOf>(ichMin);
  9144. }
  9145. else if (isForOf)
  9146. {
  9147. pnodeForInOrForOf = CreateNodeForOpT<knopForOf>(ichMin);
  9148. }
  9149. else
  9150. {
  9151. pnodeForInOrForOf = CreateNodeForOpT<knopForIn>(ichMin);
  9152. }
  9153. pnodeForInOrForOf->pnodeBlock = pnodeBlock;
  9154. pnodeForInOrForOf->pnodeLval = pnodeT;
  9155. pnodeForInOrForOf->pnodeObj = pnodeObj;
  9156. pnodeForInOrForOf->ichLim = ichLim;
  9157. TrackAssignment<true>(pnodeT, nullptr);
  9158. }
  9159. PushStmt<buildAST>(&stmt, pnodeForInOrForOf, isForAwait ? knopForAwaitOf : (isForOf ? knopForOf : knopForIn), pLabelIdList);
  9160. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  9161. if (buildAST)
  9162. {
  9163. pnodeForInOrForOf->pnodeBody = pnodeBody;
  9164. pnode = pnodeForInOrForOf;
  9165. }
  9166. PopStmt(&stmt);
  9167. }
  9168. else
  9169. {
  9170. if (!nativeForOkay)
  9171. {
  9172. Error(ERRDestructInit);
  9173. }
  9174. if (isForAwait)
  9175. {
  9176. Error(ERRValidIfFollowedBy, _u("'for await'"), _u("'of'"));
  9177. }
  9178. ChkCurTok(tkSColon, ERRnoSemic);
  9179. ParseNodePtr pnodeCond = nullptr;
  9180. if (m_token.tk != tkSColon)
  9181. {
  9182. pnodeCond = ParseExpr<buildAST>();
  9183. if (m_token.tk != tkSColon)
  9184. {
  9185. Error(ERRnoSemic);
  9186. }
  9187. }
  9188. tokens tk;
  9189. tk = this->GetScanner()->Scan();
  9190. ParseNodePtr pnodeIncr = nullptr;
  9191. if (tk != tkRParen)
  9192. {
  9193. pnodeIncr = ParseExpr<buildAST>();
  9194. if (pnodeIncr)
  9195. {
  9196. pnodeIncr->isUsed = false;
  9197. }
  9198. }
  9199. charcount_t ichLim = this->GetScanner()->IchLimTok();
  9200. ChkCurTok(tkRParen, ERRnoRparen);
  9201. ParseNodeFor * pnodeFor = nullptr;
  9202. if (buildAST)
  9203. {
  9204. pnodeFor = CreateNodeForOpT<knopFor>(ichMin);
  9205. pnodeFor->pnodeBlock = pnodeBlock;
  9206. pnodeFor->pnodeInverted = nullptr;
  9207. pnodeFor->pnodeInit = pnodeT;
  9208. pnodeFor->pnodeCond = pnodeCond;
  9209. pnodeFor->pnodeIncr = pnodeIncr;
  9210. pnodeFor->ichLim = ichLim;
  9211. }
  9212. PushStmt<buildAST>(&stmt, pnodeFor, knopFor, pLabelIdList);
  9213. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  9214. if (buildAST)
  9215. {
  9216. pnodeFor->pnodeBody = pnodeBody;
  9217. pnode = pnodeFor;
  9218. }
  9219. PopStmt(&stmt);
  9220. }
  9221. if (buildAST)
  9222. {
  9223. PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
  9224. }
  9225. FinishParseBlock(pnodeBlock);
  9226. break;
  9227. }
  9228. case tkSWITCH:
  9229. {
  9230. BOOL fSeenDefault = FALSE;
  9231. ParseNodeBlock * pnodeBlock = nullptr;
  9232. ParseNodePtr *ppnodeScopeSave = nullptr;
  9233. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  9234. ichMin = this->GetScanner()->IchMinTok();
  9235. ChkNxtTok(tkLParen, ERRnoLparen);
  9236. ParseNodePtr pnodeVal = ParseExpr<buildAST>();
  9237. charcount_t ichLim = this->GetScanner()->IchLimTok();
  9238. ChkCurTok(tkRParen, ERRnoRparen);
  9239. ChkCurTok(tkLCurly, ERRnoLcurly);
  9240. ParseNodeSwitch * pnodeSwitch = nullptr;
  9241. if (buildAST)
  9242. {
  9243. pnodeSwitch = CreateNodeForOpT<knopSwitch>(ichMin);
  9244. }
  9245. PushStmt<buildAST>(&stmt, pnodeSwitch, knopSwitch, pLabelIdList);
  9246. pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
  9247. ParseNodeCase ** ppnodeCase = nullptr;
  9248. if (buildAST)
  9249. {
  9250. pnodeSwitch->pnodeVal = pnodeVal;
  9251. pnodeSwitch->pnodeBlock = pnodeBlock;
  9252. pnodeSwitch->ichLim = ichLim;
  9253. PushFuncBlockScope(pnodeSwitch->pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
  9254. pnodeSwitch->pnodeDefault = nullptr;
  9255. ppnodeCase = &pnodeSwitch->pnodeCases;
  9256. pnode = pnodeSwitch;
  9257. }
  9258. for (;;)
  9259. {
  9260. ParseNodeCase * pnodeCase;
  9261. ParseNodePtr pnodeBody = nullptr;
  9262. switch (m_token.tk)
  9263. {
  9264. default:
  9265. goto LEndSwitch;
  9266. case tkCASE:
  9267. {
  9268. pnodeCase = this->ParseCase<buildAST>(&pnodeBody);
  9269. break;
  9270. }
  9271. case tkDEFAULT:
  9272. if (fSeenDefault)
  9273. {
  9274. Error(ERRdupDefault);
  9275. // No recovery necessary since this is a semantic, not structural, error
  9276. }
  9277. fSeenDefault = TRUE;
  9278. charcount_t ichMinT = this->GetScanner()->IchMinTok();
  9279. this->GetScanner()->Scan();
  9280. charcount_t ichMinInner = this->GetScanner()->IchLimTok();
  9281. ChkCurTok(tkColon, ERRnoColon);
  9282. if (buildAST)
  9283. {
  9284. pnodeCase = CreateNodeForOpT<knopCase>(ichMinT);
  9285. pnodeSwitch->pnodeDefault = pnodeCase;
  9286. pnodeCase->ichLim = ichMinInner;
  9287. pnodeCase->pnodeExpr = nullptr;
  9288. }
  9289. ParseStmtList<buildAST>(&pnodeBody);
  9290. break;
  9291. }
  9292. // Create a block node to contain the statement list for this case.
  9293. // This helps us insert byte code to return the right value from
  9294. // global/eval code.
  9295. ParseNodeBlock * pnodeFakeBlock = CreateBlockNode();
  9296. if (buildAST)
  9297. {
  9298. if (pnodeBody)
  9299. {
  9300. pnodeFakeBlock->ichMin = pnodeCase->ichMin;
  9301. pnodeFakeBlock->ichLim = pnodeCase->ichLim;
  9302. pnodeCase->pnodeBody = pnodeFakeBlock;
  9303. pnodeCase->pnodeBody->grfpn |= PNodeFlags::fpnSyntheticNode; // block is not a user specifier block
  9304. pnodeCase->pnodeBody->pnodeStmt = pnodeBody;
  9305. }
  9306. else
  9307. {
  9308. pnodeCase->pnodeBody = nullptr;
  9309. }
  9310. *ppnodeCase = pnodeCase;
  9311. ppnodeCase = &pnodeCase->pnodeNext;
  9312. }
  9313. }
  9314. LEndSwitch:
  9315. ChkCurTok(tkRCurly, ERRnoRcurly);
  9316. if (buildAST)
  9317. {
  9318. *ppnodeCase = nullptr;
  9319. PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
  9320. FinishParseBlock(pnode->AsParseNodeSwitch()->pnodeBlock);
  9321. }
  9322. else
  9323. {
  9324. FinishParseBlock(pnodeBlock);
  9325. }
  9326. PopStmt(&stmt);
  9327. break;
  9328. }
  9329. case tkWHILE:
  9330. {
  9331. ichMin = this->GetScanner()->IchMinTok();
  9332. ChkNxtTok(tkLParen, ERRnoLparen);
  9333. ParseNodePtr pnodeCond = ParseExpr<buildAST>();
  9334. charcount_t ichLim = this->GetScanner()->IchLimTok();
  9335. ChkCurTok(tkRParen, ERRnoRparen);
  9336. ParseNodeWhile * pnodeWhile = nullptr;
  9337. if (buildAST)
  9338. {
  9339. pnodeWhile = CreateNodeForOpT<knopWhile>(ichMin);
  9340. pnodeWhile->pnodeCond = pnodeCond;
  9341. pnodeWhile->ichLim = ichLim;
  9342. }
  9343. bool stashedDisallowImportExportStmt = m_disallowImportExportStmt;
  9344. m_disallowImportExportStmt = true;
  9345. PushStmt<buildAST>(&stmt, pnodeWhile, knopWhile, pLabelIdList);
  9346. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  9347. PopStmt(&stmt);
  9348. if (buildAST)
  9349. {
  9350. pnodeWhile->pnodeBody = pnodeBody;
  9351. pnode = pnodeWhile;
  9352. }
  9353. m_disallowImportExportStmt = stashedDisallowImportExportStmt;
  9354. break;
  9355. }
  9356. case tkDO:
  9357. {
  9358. ParseNodeWhile * pnodeWhile = nullptr;
  9359. if (buildAST)
  9360. {
  9361. pnodeWhile = CreateNodeForOpT<knopDoWhile>();
  9362. }
  9363. PushStmt<buildAST>(&stmt, pnodeWhile, knopDoWhile, pLabelIdList);
  9364. this->GetScanner()->Scan();
  9365. bool stashedDisallowImportExportStmt = m_disallowImportExportStmt;
  9366. m_disallowImportExportStmt = true;
  9367. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  9368. m_disallowImportExportStmt = stashedDisallowImportExportStmt;
  9369. PopStmt(&stmt);
  9370. charcount_t ichMinT = this->GetScanner()->IchMinTok();
  9371. ChkCurTok(tkWHILE, ERRnoWhile);
  9372. ChkCurTok(tkLParen, ERRnoLparen);
  9373. ParseNodePtr pnodeCond = ParseExpr<buildAST>();
  9374. charcount_t ichLim = this->GetScanner()->IchLimTok();
  9375. ChkCurTok(tkRParen, ERRnoRparen);
  9376. if (buildAST)
  9377. {
  9378. pnodeWhile->pnodeBody = pnodeBody;
  9379. pnodeWhile->pnodeCond = pnodeCond;
  9380. pnodeWhile->ichLim = ichLim;
  9381. pnodeWhile->ichMin = ichMinT;
  9382. pnode = pnodeWhile;
  9383. }
  9384. // REVIEW: Allow do...while statements to be embedded in other compound statements like if..else, or do..while?
  9385. // goto LNeedTerminator;
  9386. // For now just eat the trailing semicolon if present.
  9387. if (m_token.tk == tkSColon)
  9388. {
  9389. if (pnode)
  9390. {
  9391. pnode->grfpn |= PNodeFlags::fpnExplicitSemicolon;
  9392. }
  9393. this->GetScanner()->Scan();
  9394. }
  9395. else if (pnode)
  9396. {
  9397. pnode->grfpn |= PNodeFlags::fpnAutomaticSemicolon;
  9398. }
  9399. break;
  9400. }
  9401. case tkIF:
  9402. {
  9403. ichMin = this->GetScanner()->IchMinTok();
  9404. ChkNxtTok(tkLParen, ERRnoLparen);
  9405. ParseNodePtr pnodeCond = ParseExpr<buildAST>();
  9406. ParseNodeIf * pnodeIf = nullptr;
  9407. if (buildAST)
  9408. {
  9409. pnodeIf = CreateNodeForOpT<knopIf>(ichMin);
  9410. pnodeIf->ichLim = this->GetScanner()->IchLimTok();
  9411. pnodeIf->pnodeCond = pnodeCond;
  9412. }
  9413. ChkCurTok(tkRParen, ERRnoRparen);
  9414. bool stashedDisallowImportExportStmt = m_disallowImportExportStmt;
  9415. m_disallowImportExportStmt = true;
  9416. PushStmt<buildAST>(&stmt, pnodeIf, knopIf, pLabelIdList);
  9417. ParseNodePtr pnodeTrue = ParseStatement<buildAST>();
  9418. ParseNodePtr pnodeFalse = nullptr;
  9419. if (m_token.tk == tkELSE)
  9420. {
  9421. this->GetScanner()->Scan();
  9422. pnodeFalse = ParseStatement<buildAST>();
  9423. }
  9424. if (buildAST)
  9425. {
  9426. pnodeIf->pnodeTrue = pnodeTrue;
  9427. pnodeIf->pnodeFalse = pnodeFalse;
  9428. pnode = pnodeIf;
  9429. }
  9430. PopStmt(&stmt);
  9431. m_disallowImportExportStmt = stashedDisallowImportExportStmt;
  9432. break;
  9433. }
  9434. case tkTRY:
  9435. {
  9436. ParseNodeBlock * pnodeBlock = CreateBlockNode();
  9437. pnodeBlock->grfpn |= PNodeFlags::fpnSyntheticNode; // block is not a user specifier block
  9438. PushStmt<buildAST>(&stmt, pnodeBlock, knopBlock, pLabelIdList);
  9439. ParseNodePtr pnodeStmt = ParseTryCatchFinally<buildAST>();
  9440. if (buildAST)
  9441. {
  9442. pnodeBlock->pnodeStmt = pnodeStmt;
  9443. }
  9444. PopStmt(&stmt);
  9445. pnode = pnodeBlock;
  9446. break;
  9447. }
  9448. case tkWITH:
  9449. {
  9450. if (IsStrictMode())
  9451. {
  9452. Error(ERRES5NoWith);
  9453. }
  9454. if (m_currentNodeFunc)
  9455. {
  9456. GetCurrentFunctionNode()->SetHasWithStmt(); // Used by DeferNested
  9457. }
  9458. ichMin = this->GetScanner()->IchMinTok();
  9459. ChkNxtTok(tkLParen, ERRnoLparen);
  9460. ParseNodePtr pnodeObj = ParseExpr<buildAST>();
  9461. if (!buildAST)
  9462. {
  9463. m_scopeCountNoAst++;
  9464. }
  9465. charcount_t ichLim = this->GetScanner()->IchLimTok();
  9466. ChkCurTok(tkRParen, ERRnoRparen);
  9467. ParseNodeWith * pnodeWith = nullptr;
  9468. if (buildAST)
  9469. {
  9470. pnodeWith = CreateNodeForOpT<knopWith>(ichMin);
  9471. }
  9472. PushStmt<buildAST>(&stmt, pnodeWith, knopWith, pLabelIdList);
  9473. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  9474. if (buildAST)
  9475. {
  9476. pnodeWith->pnodeObj = pnodeObj;
  9477. this->CheckArguments(pnodeWith->pnodeObj);
  9478. if (m_ppnodeExprScope)
  9479. {
  9480. Assert(*m_ppnodeExprScope == nullptr);
  9481. *m_ppnodeExprScope = pnodeWith;
  9482. m_ppnodeExprScope = &pnodeWith->pnodeNext;
  9483. }
  9484. else
  9485. {
  9486. Assert(m_ppnodeScope);
  9487. Assert(*m_ppnodeScope == nullptr);
  9488. *m_ppnodeScope = pnodeWith;
  9489. m_ppnodeScope = &pnodeWith->pnodeNext;
  9490. }
  9491. pnodeWith->pnodeNext = nullptr;
  9492. pnodeWith->scope = nullptr;
  9493. ppnodeExprScopeSave = m_ppnodeExprScope;
  9494. m_ppnodeExprScope = &pnodeWith->pnodeScopes;
  9495. pnodeWith->pnodeScopes = nullptr;
  9496. pnodeWith->ichLim = ichLim;
  9497. pnode = pnodeWith;
  9498. }
  9499. PushBlockInfo(CreateBlockNode());
  9500. PushDynamicBlock();
  9501. ParseNodePtr pnodeBody = ParseStatement<buildAST>();
  9502. if (buildAST)
  9503. {
  9504. pnode->AsParseNodeWith()->pnodeBody = pnodeBody;
  9505. m_ppnodeExprScope = ppnodeExprScopeSave;
  9506. }
  9507. else
  9508. {
  9509. m_scopeCountNoAst--;
  9510. }
  9511. // The dynamic block is not stored in the actual parse tree and so will not
  9512. // be visited by the byte code generator. Grab the callsEval flag off it and
  9513. // pass on to outer block in case of:
  9514. // with (...) eval(...); // i.e. blockless form of with
  9515. bool callsEval = GetCurrentBlock()->GetCallsEval();
  9516. PopBlockInfo();
  9517. if (callsEval)
  9518. {
  9519. // be careful not to overwrite an existing true with false
  9520. GetCurrentBlock()->SetCallsEval(true);
  9521. }
  9522. PopStmt(&stmt);
  9523. break;
  9524. }
  9525. case tkLCurly:
  9526. pnode = ParseBlock<buildAST>(pLabelIdList);
  9527. break;
  9528. case tkSColon:
  9529. pnode = nullptr;
  9530. this->GetScanner()->Scan();
  9531. break;
  9532. case tkBREAK:
  9533. if (buildAST)
  9534. {
  9535. pnode = CreateNodeForOpT<knopBreak>();
  9536. }
  9537. fnop = fnopBreak;
  9538. goto LGetJumpStatement;
  9539. case tkCONTINUE:
  9540. if (buildAST)
  9541. {
  9542. pnode = CreateNodeForOpT<knopContinue>();
  9543. }
  9544. fnop = fnopContinue;
  9545. LGetJumpStatement:
  9546. this->GetScanner()->ScanForcingPid();
  9547. if (tkID == m_token.tk && !this->GetScanner()->FHadNewLine())
  9548. {
  9549. // Labeled break or continue.
  9550. pid = m_token.GetIdentifier(this->GetHashTbl());
  9551. if (buildAST)
  9552. {
  9553. ParseNodeJump * pnodeJump = pnode->AsParseNodeJump();
  9554. pnodeJump->hasExplicitTarget = true;
  9555. pnodeJump->ichLim = this->GetScanner()->IchLimTok();
  9556. this->GetScanner()->Scan();
  9557. PushStmt<buildAST>(&stmt, pnodeJump, pnodeJump->nop, pLabelIdList);
  9558. Assert(pnodeJump->grfnop == 0);
  9559. for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
  9560. {
  9561. for (LabelId* label = pstmt->pLabelId; label != nullptr; label = label->next)
  9562. {
  9563. if (pid == label->pid)
  9564. {
  9565. // Found the label. Make sure we can use it. We can
  9566. // break out of any statement, but we can only
  9567. // continue loops.
  9568. if (fnop == fnopContinue &&
  9569. !(pstmt->pnodeStmt->Grfnop() & fnop))
  9570. {
  9571. Error(ERRbadContinue);
  9572. }
  9573. else
  9574. {
  9575. pstmt->pnodeStmt->grfnop |= fnop;
  9576. pnodeJump->pnodeTarget = pstmt->pnodeStmt;
  9577. }
  9578. PopStmt(&stmt);
  9579. goto LNeedTerminator;
  9580. }
  9581. }
  9582. pnodeJump->grfnop |=
  9583. (pstmt->pnodeStmt->Grfnop() & fnopCleanup);
  9584. }
  9585. }
  9586. else
  9587. {
  9588. this->GetScanner()->Scan();
  9589. // Check if label is found within the current label id list.
  9590. auto checkLabelList = [&](LabelId* list, StmtNest* checkStmtOp)
  9591. {
  9592. for (LabelId* pLabelId = list; pLabelId; pLabelId = pLabelId->next)
  9593. {
  9594. if (pid == pLabelId->pid)
  9595. {
  9596. // Found the label. Make sure we can use it. We can
  9597. // break out of any statement, but we can only
  9598. // continue loops.
  9599. if (fnop == fnopContinue &&
  9600. !(ParseNode::Grfnop(checkStmtOp->op) & fnop))
  9601. {
  9602. Error(ERRbadContinue);
  9603. }
  9604. return true;
  9605. }
  9606. }
  9607. return false;
  9608. };
  9609. if (checkLabelList(pLabelIdList, m_pstmtCur)) goto LNeedTerminator;
  9610. for (pstmt = m_pstmtCur; pstmt; pstmt = pstmt->pstmtOuter)
  9611. {
  9612. if (checkLabelList(pstmt->pLabelId, pstmt)) goto LNeedTerminator;
  9613. }
  9614. }
  9615. Error(ERRnoLabel);
  9616. }
  9617. else
  9618. {
  9619. // If we're doing a fast scan, we're not tracking labels, so we can't accurately do this analysis.
  9620. // Let the thread that's doing the full parse detect the error, if there is one.
  9621. if (!this->IsDoingFastScan())
  9622. {
  9623. // Unlabeled break or continue.
  9624. ParseNodeJump * pnodeJump = nullptr;
  9625. if (buildAST)
  9626. {
  9627. pnodeJump = pnode->AsParseNodeJump();
  9628. pnodeJump->hasExplicitTarget = false;
  9629. PushStmt<buildAST>(&stmt, pnodeJump, pnodeJump->nop, pLabelIdList);
  9630. Assert(pnodeJump->grfnop == 0);
  9631. }
  9632. for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
  9633. {
  9634. if (buildAST)
  9635. {
  9636. AnalysisAssert(pstmt->pnodeStmt);
  9637. if (pstmt->pnodeStmt->Grfnop() & fnop)
  9638. {
  9639. pstmt->pnodeStmt->grfnop |= fnop;
  9640. pnodeJump->pnodeTarget = pstmt->pnodeStmt;
  9641. PopStmt(&stmt);
  9642. goto LNeedTerminator;
  9643. }
  9644. pnodeJump->grfnop |=
  9645. (pstmt->pnodeStmt->Grfnop() & fnopCleanup);
  9646. }
  9647. else
  9648. {
  9649. if (ParseNode::Grfnop(pstmt->GetNop()) & fnop)
  9650. {
  9651. if (!pstmt->isDeferred)
  9652. {
  9653. AnalysisAssert(pstmt->pnodeStmt);
  9654. pstmt->pnodeStmt->grfnop |= fnop;
  9655. }
  9656. goto LNeedTerminator;
  9657. }
  9658. }
  9659. }
  9660. Error(fnop == fnopBreak ? ERRbadBreak : ERRbadContinue);
  9661. }
  9662. goto LNeedTerminator;
  9663. }
  9664. case tkRETURN:
  9665. {
  9666. ParseNodeReturn * pnodeReturn;
  9667. if (buildAST)
  9668. {
  9669. if (nullptr == m_currentNodeFunc || IsTopLevelModuleFunc())
  9670. {
  9671. Error(ERRbadReturn);
  9672. }
  9673. pnodeReturn = CreateNodeForOpT<knopReturn>();
  9674. }
  9675. this->GetScanner()->Scan();
  9676. ParseNodePtr pnodeExpr = nullptr;
  9677. ParseOptionalExpr<buildAST>(&pnodeExpr, true);
  9678. // Class constructors have special semantics regarding return statements.
  9679. // This might require a reference to 'this'
  9680. if (GetCurrentFunctionNode()->IsClassConstructor())
  9681. {
  9682. ReferenceSpecialName(wellKnownPropertyPids._this);
  9683. }
  9684. if (buildAST)
  9685. {
  9686. pnodeReturn->pnodeExpr = pnodeExpr;
  9687. if (pnodeExpr)
  9688. {
  9689. this->CheckArguments(pnodeReturn->pnodeExpr);
  9690. pnodeReturn->ichLim = pnodeReturn->pnodeExpr->ichLim;
  9691. }
  9692. // See if return should call finally
  9693. PushStmt<buildAST>(&stmt, pnodeReturn, knopReturn, pLabelIdList);
  9694. Assert(pnodeReturn->grfnop == 0);
  9695. for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
  9696. {
  9697. if (pstmt->pnodeStmt->Grfnop() & fnopCleanup)
  9698. {
  9699. pnodeReturn->grfnop |= fnopCleanup;
  9700. break;
  9701. }
  9702. }
  9703. PopStmt(&stmt);
  9704. pnode = pnodeReturn;
  9705. }
  9706. goto LNeedTerminator;
  9707. }
  9708. case tkTHROW:
  9709. {
  9710. if (buildAST)
  9711. {
  9712. pnode = CreateUniNode(knopThrow, nullptr);
  9713. }
  9714. this->GetScanner()->Scan();
  9715. ParseNodePtr pnode1 = nullptr;
  9716. if (m_token.tk != tkSColon &&
  9717. m_token.tk != tkRCurly &&
  9718. !this->GetScanner()->FHadNewLine())
  9719. {
  9720. pnode1 = ParseExpr<buildAST>();
  9721. }
  9722. else
  9723. {
  9724. Error(ERRdanglingThrow);
  9725. }
  9726. if (buildAST)
  9727. {
  9728. pnode->AsParseNodeUni()->pnode1 = pnode1;
  9729. if (pnode1)
  9730. {
  9731. this->CheckArguments(pnode->AsParseNodeUni()->pnode1);
  9732. pnode->ichLim = pnode->AsParseNodeUni()->pnode1->ichLim;
  9733. }
  9734. }
  9735. goto LNeedTerminator;
  9736. }
  9737. case tkDEBUGGER:
  9738. if (buildAST)
  9739. {
  9740. pnode = CreateNodeForOpT<knopDebugger>();
  9741. }
  9742. this->GetScanner()->Scan();
  9743. goto LNeedTerminator;
  9744. case tkIMPORT:
  9745. pnode = ParseImport<buildAST>();
  9746. goto LNeedTerminator;
  9747. case tkEXPORT:
  9748. {
  9749. if (!(m_grfscr & fscrIsModuleCode))
  9750. {
  9751. goto LDefaultToken;
  9752. }
  9753. bool needTerminator = false;
  9754. pnode = ParseExportDeclaration<buildAST>(&needTerminator);
  9755. if (needTerminator)
  9756. {
  9757. goto LNeedTerminator;
  9758. }
  9759. else
  9760. {
  9761. break;
  9762. }
  9763. }
  9764. LDefaultToken:
  9765. default:
  9766. {
  9767. // First check for a label via lookahead. If not found,
  9768. // rewind and reparse as expression statement.
  9769. if (m_token.tk == tkID)
  9770. {
  9771. RestorePoint idStart;
  9772. this->GetScanner()->Capture(&idStart);
  9773. IdentPtr pidInner = m_token.GetIdentifier(this->GetHashTbl());
  9774. this->GetScanner()->Scan();
  9775. if (m_token.tk == tkColon)
  9776. {
  9777. // We have a label.
  9778. if (LabelExists(pidInner, pLabelIdList))
  9779. {
  9780. Error(ERRbadLabel);
  9781. }
  9782. LabelId* pLabelId = CreateLabelId(pidInner);
  9783. pLabelId->next = pLabelIdList;
  9784. pLabelIdList = pLabelId;
  9785. this->GetScanner()->Scan();
  9786. labelledStatement = true;
  9787. goto LRestart;
  9788. }
  9789. // No label, rewind back to the tkID and parse an expression
  9790. this->GetScanner()->SeekTo(idStart);
  9791. }
  9792. // Must be an expression statement.
  9793. pnode = ParseExpr<buildAST>();
  9794. if (m_hasDeferredShorthandInitError)
  9795. {
  9796. Error(ERRnoColon);
  9797. }
  9798. if (buildAST)
  9799. {
  9800. expressionStmt = true;
  9801. AnalysisAssert(pnode);
  9802. pnode->isUsed = false;
  9803. }
  9804. }
  9805. LNeedTerminator:
  9806. // Need a semicolon, new-line, } or end-of-file.
  9807. // We digest a semicolon if it's there.
  9808. switch (m_token.tk)
  9809. {
  9810. case tkSColon:
  9811. this->GetScanner()->Scan();
  9812. if (pnode != nullptr) pnode->grfpn |= PNodeFlags::fpnExplicitSemicolon;
  9813. break;
  9814. case tkEOF:
  9815. case tkRCurly:
  9816. if (pnode != nullptr) pnode->grfpn |= PNodeFlags::fpnAutomaticSemicolon;
  9817. break;
  9818. default:
  9819. if (!this->GetScanner()->FHadNewLine())
  9820. {
  9821. Error(ERRnoSemic);
  9822. }
  9823. else
  9824. {
  9825. if (pnode != nullptr) pnode->grfpn |= PNodeFlags::fpnAutomaticSemicolon;
  9826. }
  9827. break;
  9828. }
  9829. break;
  9830. }
  9831. if (m_hasDeferredShorthandInitError)
  9832. {
  9833. Error(ERRnoColon);
  9834. }
  9835. if (buildAST)
  9836. {
  9837. // All non expression statements excluded from the "this.x" optimization
  9838. // Another check while parsing expressions
  9839. if (!expressionStmt)
  9840. {
  9841. if (m_currentNodeFunc)
  9842. {
  9843. m_currentNodeFunc->SetHasNonThisStmt();
  9844. }
  9845. else if (m_currentNodeProg)
  9846. {
  9847. m_currentNodeProg->SetHasNonThisStmt();
  9848. }
  9849. }
  9850. #if EXCEPTION_RECOVERY
  9851. // close the try/catch block
  9852. if (Js::Configuration::Global.flags.SwallowExceptions)
  9853. {
  9854. // pop the try block and fill in the body
  9855. PopStmt(&stmtTryBlock);
  9856. pTryBlock->pnodeStmt = pnode;
  9857. PopStmt(&stmtTry);
  9858. if (pnode != nullptr)
  9859. {
  9860. pTry->ichLim = pnode->ichLim;
  9861. }
  9862. pTry->pnodeBody = pTryBlock;
  9863. // create a catch block with an empty body
  9864. StmtNest stmtCatch;
  9865. ParseNodeCatch * pCatch;
  9866. pCatch = CreateNodeForOpT<knopCatch>();
  9867. PushStmt<buildAST>(&stmtCatch, pCatch, knopCatch, nullptr);
  9868. pCatch->pnodeBody = nullptr;
  9869. if (pnode != nullptr)
  9870. {
  9871. pCatch->ichLim = pnode->ichLim;
  9872. }
  9873. pCatch->grfnop = 0;
  9874. pCatch->pnodeNext = nullptr;
  9875. // create a fake name for the catch var.
  9876. const WCHAR *uniqueNameStr = _u("__ehobj");
  9877. IdentPtr uniqueName = this->GetHashTbl()->PidHashNameLen(uniqueNameStr, static_cast<int32>(wcslen(uniqueNameStr)));
  9878. pCatch->SetParam(CreateNameNode(uniqueName));
  9879. // Add this catch to the current list. We don't bother adjusting the catch and function expression
  9880. // lists here because the catch is just an empty statement.
  9881. if (m_ppnodeExprScope)
  9882. {
  9883. Assert(*m_ppnodeExprScope == nullptr);
  9884. *m_ppnodeExprScope = pCatch;
  9885. m_ppnodeExprScope = &pCatch->pnodeNext;
  9886. }
  9887. else
  9888. {
  9889. Assert(m_ppnodeScope);
  9890. Assert(*m_ppnodeScope == nullptr);
  9891. *m_ppnodeScope = pCatch;
  9892. m_ppnodeScope = &pCatch->pnodeNext;
  9893. }
  9894. pCatch->pnodeScopes = nullptr;
  9895. PopStmt(&stmtCatch);
  9896. // fill in and pop the try-catch
  9897. pParentTryCatch->pnodeTry = pTry;
  9898. pParentTryCatch->pnodeCatch = pCatch;
  9899. PopStmt(&stmtTryCatch);
  9900. PopStmt(&stmtTryCatchBlock);
  9901. // replace the node that's being returned
  9902. pParentTryCatchBlock->pnodeStmt = pParentTryCatch;
  9903. pnode = pParentTryCatchBlock;
  9904. }
  9905. #endif // EXCEPTION_RECOVERY
  9906. }
  9907. return pnode;
  9908. }
  9909. BOOL
  9910. Parser::TokIsForInOrForOf()
  9911. {
  9912. return m_token.tk == tkIN || CheckContextualKeyword(wellKnownPropertyPids.of);
  9913. }
  9914. /***************************************************************************
  9915. Parse a sequence of statements.
  9916. ***************************************************************************/
  9917. template<bool buildAST>
  9918. void Parser::ParseStmtList(ParseNodePtr *ppnodeList, ParseNodePtr **pppnodeLast, StrictModeEnvironment smEnvironment, const bool isSourceElementList, bool* strictModeOn)
  9919. {
  9920. BOOL doneDirectives = !isSourceElementList; // directives may only exist in a SourceElementList, not a StatementList
  9921. BOOL seenDirectiveContainingOctal = false; // Have we seen an octal directive before a use strict directive?
  9922. BOOL old_UseStrictMode = m_fUseStrictMode;
  9923. ParseNodePtr pnodeStmt;
  9924. ParseNodePtr *lastNodeRef = nullptr;
  9925. if (buildAST)
  9926. {
  9927. Assert(ppnodeList);
  9928. *ppnodeList = nullptr;
  9929. }
  9930. if (CONFIG_FLAG(ForceStrictMode))
  9931. {
  9932. m_fUseStrictMode = TRUE;
  9933. }
  9934. for (;;)
  9935. {
  9936. switch (m_token.tk)
  9937. {
  9938. case tkCASE:
  9939. case tkDEFAULT:
  9940. case tkRCurly:
  9941. case tkEOF:
  9942. if (buildAST && nullptr != pppnodeLast)
  9943. {
  9944. *pppnodeLast = lastNodeRef;
  9945. }
  9946. if (!buildAST)
  9947. {
  9948. m_fUseStrictMode = old_UseStrictMode;
  9949. }
  9950. return;
  9951. }
  9952. if (doneDirectives == FALSE)
  9953. {
  9954. bool isOctalInString = false;
  9955. bool isUseStrictDirective = false;
  9956. bool isUseAsmDirective = false;
  9957. if (smEnvironment != SM_NotUsed && CheckForDirective(&isUseStrictDirective, &isUseAsmDirective, &isOctalInString))
  9958. {
  9959. // Ignore "use asm" statement when not building the AST
  9960. isUseAsmDirective &= buildAST;
  9961. if (isUseStrictDirective)
  9962. {
  9963. // Functions with non-simple parameter list cannot be made strict mode
  9964. if (GetCurrentFunctionNode()->HasNonSimpleParameterList())
  9965. {
  9966. Error(ERRNonSimpleParamListInStrictMode);
  9967. }
  9968. if (seenDirectiveContainingOctal)
  9969. {
  9970. // Directives seen before a "use strict" cannot contain an octal.
  9971. Error(ERRES5NoOctal);
  9972. }
  9973. if (!buildAST)
  9974. {
  9975. // Turning on strict mode in deferred code.
  9976. m_fUseStrictMode = TRUE;
  9977. if (!m_inDeferredNestedFunc)
  9978. {
  9979. // Top-level deferred function, so there's a parse node
  9980. Assert(m_currentNodeFunc != nullptr);
  9981. m_currentNodeFunc->SetStrictMode();
  9982. }
  9983. else if (strictModeOn)
  9984. {
  9985. // This turns on strict mode in a deferred function, we need to go back
  9986. // and re-check duplicated formals.
  9987. *strictModeOn = true;
  9988. }
  9989. }
  9990. else
  9991. {
  9992. if (smEnvironment == SM_OnGlobalCode)
  9993. {
  9994. // Turning on strict mode at the top level
  9995. m_fUseStrictMode = TRUE;
  9996. }
  9997. else
  9998. {
  9999. // i.e. smEnvironment == SM_OnFunctionCode
  10000. Assert(m_currentNodeFunc != nullptr);
  10001. m_currentNodeFunc->SetStrictMode();
  10002. }
  10003. }
  10004. }
  10005. else if (isUseAsmDirective)
  10006. {
  10007. if (smEnvironment != SM_OnGlobalCode) //Top level use asm doesn't mean anything.
  10008. {
  10009. // i.e. smEnvironment == SM_OnFunctionCode
  10010. Assert(m_currentNodeFunc != nullptr);
  10011. m_currentNodeFunc->SetAsmjsMode();
  10012. m_currentNodeFunc->SetCanBeDeferred(false);
  10013. m_InAsmMode = true;
  10014. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, AsmJSFunction, m_scriptContext);
  10015. }
  10016. }
  10017. else if (isOctalInString)
  10018. {
  10019. seenDirectiveContainingOctal = TRUE;
  10020. }
  10021. }
  10022. else
  10023. {
  10024. // The first time we see anything other than a directive we can have no more directives.
  10025. doneDirectives = TRUE;
  10026. }
  10027. }
  10028. if (nullptr != (pnodeStmt = ParseStatement<buildAST>()))
  10029. {
  10030. if (buildAST)
  10031. {
  10032. AddToNodeList(ppnodeList, &lastNodeRef, pnodeStmt);
  10033. }
  10034. }
  10035. }
  10036. }
  10037. template <class Fn>
  10038. void Parser::FinishFunctionsInScope(ParseNodePtr pnodeScopeList, Fn fn)
  10039. {
  10040. Scope * scope;
  10041. Scope * origCurrentScope = this->m_currentScope;
  10042. ParseNodePtr pnodeScope;
  10043. ParseNodeBlock * pnodeBlock;
  10044. for (pnodeScope = pnodeScopeList; pnodeScope;)
  10045. {
  10046. switch (pnodeScope->nop)
  10047. {
  10048. case knopBlock:
  10049. {
  10050. ParseNodeBlock * pnodeBlockScope = pnodeScope->AsParseNodeBlock();
  10051. m_nextBlockId = pnodeBlockScope->blockId + 1;
  10052. PushBlockInfo(pnodeBlockScope);
  10053. scope = pnodeBlockScope->scope;
  10054. if (scope && scope != origCurrentScope)
  10055. {
  10056. PushScope(scope);
  10057. }
  10058. FinishFunctionsInScope(pnodeBlockScope->pnodeScopes, fn);
  10059. if (scope && scope != origCurrentScope)
  10060. {
  10061. BindPidRefs<false>(GetCurrentBlockInfo(), m_nextBlockId - 1);
  10062. PopScope(scope);
  10063. }
  10064. PopBlockInfo();
  10065. pnodeScope = pnodeBlockScope->pnodeNext;
  10066. break;
  10067. }
  10068. case knopFncDecl:
  10069. fn(pnodeScope->AsParseNodeFnc());
  10070. pnodeScope = pnodeScope->AsParseNodeFnc()->pnodeNext;
  10071. break;
  10072. case knopCatch:
  10073. scope = pnodeScope->AsParseNodeCatch()->scope;
  10074. if (scope)
  10075. {
  10076. PushScope(scope);
  10077. }
  10078. pnodeBlock = CreateBlockNode(PnodeBlockType::Regular);
  10079. pnodeBlock->scope = scope;
  10080. PushBlockInfo(pnodeBlock);
  10081. FinishFunctionsInScope(pnodeScope->AsParseNodeCatch()->pnodeScopes, fn);
  10082. if (scope)
  10083. {
  10084. BindPidRefs<false>(GetCurrentBlockInfo(), m_nextBlockId - 1);
  10085. PopScope(scope);
  10086. }
  10087. PopBlockInfo();
  10088. pnodeScope = pnodeScope->AsParseNodeCatch()->pnodeNext;
  10089. break;
  10090. case knopWith:
  10091. PushBlockInfo(CreateBlockNode());
  10092. PushDynamicBlock();
  10093. FinishFunctionsInScope(pnodeScope->AsParseNodeWith()->pnodeScopes, fn);
  10094. PopBlockInfo();
  10095. pnodeScope = pnodeScope->AsParseNodeWith()->pnodeNext;
  10096. break;
  10097. default:
  10098. AssertMsg(false, "Unexpected node with scope list");
  10099. return;
  10100. }
  10101. }
  10102. }
  10103. // Scripts above this size (minus string literals and comments) will have parsing of
  10104. // function bodies deferred.
  10105. ULONG Parser::GetDeferralThreshold(bool isProfileLoaded)
  10106. {
  10107. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  10108. if (CONFIG_FLAG(ForceDeferParse) ||
  10109. PHASE_FORCE1(Js::DeferParsePhase) ||
  10110. Js::Configuration::Global.flags.IsEnabled(Js::ForceUndoDeferFlag))
  10111. {
  10112. return 0;
  10113. }
  10114. else if (Js::Configuration::Global.flags.IsEnabled(Js::DeferParseFlag))
  10115. {
  10116. return Js::Configuration::Global.flags.DeferParse;
  10117. }
  10118. else
  10119. #endif
  10120. {
  10121. if (isProfileLoaded)
  10122. {
  10123. return DEFAULT_CONFIG_ProfileBasedDeferParseThreshold;
  10124. }
  10125. return DEFAULT_CONFIG_DeferParseThreshold;
  10126. }
  10127. }
  10128. void Parser::FinishDeferredFunction(ParseNodeBlock * pnodeScopeList)
  10129. {
  10130. ParseContext parseContext;
  10131. this->CaptureContext(&parseContext);
  10132. m_nextBlockId = pnodeScopeList->blockId + 1;
  10133. FinishFunctionsInScope(pnodeScopeList,
  10134. [this, &parseContext](ParseNodeFnc * pnodeFnc)
  10135. {
  10136. Assert(pnodeFnc->nop == knopFncDecl);
  10137. // We need to scan this function based on the already known limits of the function declaration as some of
  10138. // the state such as fAllowIn may not be available at this point. Some of this state depends on the context
  10139. // of the function declaration. For example, a function declaration may be inside a for..in statement's var
  10140. // declaration. It may not be appropriate/possible to try and save all such context information. Functions
  10141. // that actually get deferred achieve this by going through the ParseSourceWithOffset code path.
  10142. this->GetScanner()->Clear();
  10143. this->GetScanner()->SetText(parseContext.pszSrc, pnodeFnc->cbMin /*+ this->m_scan.m_cMinTokMultiUnits*/, pnodeFnc->LengthInBytes(), pnodeFnc->ichMin, parseContext.isUtf8, parseContext.grfscr, pnodeFnc->lineNumber);
  10144. this->GetScanner()->Scan();
  10145. // Non-simple params (such as default) require a good amount of logic to put vars on appropriate scopes. ParseFncDecl handles it
  10146. // properly (both on defer and non-defer case). This is to avoid write duplicated logic here as well. Function with non-simple-param
  10147. // will remain deferred until they are called.
  10148. if (pnodeFnc->pnodeBody == nullptr && !pnodeFnc->HasNonSimpleParameterList())
  10149. {
  10150. // Go back and generate an AST for this function.
  10151. JS_ETW_INTERNAL(EventWriteJSCRIPT_PARSE_FUNC(this->GetScriptContext(), pnodeFnc->functionId, /*Undefer*/TRUE));
  10152. ParseNodeFnc * pnodeFncSave = this->m_currentNodeFunc;
  10153. this->m_currentNodeFunc = pnodeFnc;
  10154. ParseNodeBlock * pnodeFncExprBlock = nullptr;
  10155. ParseNodePtr pnodeName = pnodeFnc->pnodeName;
  10156. if (pnodeName)
  10157. {
  10158. Assert(pnodeName->nop == knopVarDecl);
  10159. ParseNodeVar * pnodeVarName = pnodeName->AsParseNodeVar();
  10160. Assert(pnodeVarName->pnodeNext == nullptr);
  10161. if (!pnodeFnc->IsDeclaration())
  10162. {
  10163. // Set up the named function expression symbol so references inside the function can be bound.
  10164. pnodeFncExprBlock = this->StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FuncExpr);
  10165. PidRefStack *ref = this->PushPidRef(pnodeVarName->pid);
  10166. pnodeVarName->symRef = ref->GetSymRef();
  10167. ref->SetSym(pnodeVarName->sym);
  10168. Scope *fncExprScope = pnodeFncExprBlock->scope;
  10169. fncExprScope->AddNewSymbol(pnodeVarName->sym);
  10170. pnodeFnc->scope = fncExprScope;
  10171. }
  10172. }
  10173. ParseNodeBlock * pnodeBlock = this->StartParseBlock<true>(PnodeBlockType::Parameter, ScopeType_Parameter);
  10174. pnodeFnc->pnodeScopes = pnodeBlock;
  10175. m_ppnodeScope = &pnodeBlock->pnodeScopes;
  10176. pnodeBlock->pnodeStmt = pnodeFnc;
  10177. ParseNodePtr * varNodesList = &pnodeFnc->pnodeVars;
  10178. ParseNodeVar * argNode = nullptr;
  10179. if (!pnodeFnc->IsModule() && !pnodeFnc->IsLambda() && !(pnodeFnc->grfpn & PNodeFlags::fpnArguments_overriddenInParam))
  10180. {
  10181. ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
  10182. m_ppnodeVar = &pnodeFnc->pnodeVars;
  10183. argNode = this->AddArgumentsNodeToVars(pnodeFnc);
  10184. varNodesList = m_ppnodeVar;
  10185. m_ppnodeVar = ppnodeVarSave;
  10186. }
  10187. // Add the args to the scope, since we won't re-parse those.
  10188. Scope *scope = pnodeBlock->scope;
  10189. uint blockId = GetCurrentBlock()->blockId;
  10190. uint funcId = GetCurrentFunctionNode()->functionId;
  10191. auto addArgsToScope = [&](ParseNodePtr pnodeArg) {
  10192. if (pnodeArg->IsVarLetOrConst())
  10193. {
  10194. ParseNodeVar * pnodeVarArg = pnodeArg->AsParseNodeVar();
  10195. PidRefStack *ref = this->FindOrAddPidRef(pnodeVarArg->pid, blockId, funcId);
  10196. pnodeVarArg->symRef = ref->GetSymRef();
  10197. if (ref->GetSym() != nullptr)
  10198. {
  10199. // Duplicate parameter in a configuration that allows them.
  10200. // The symbol is already in the scope, just point it to the right declaration.
  10201. Assert(ref->GetSym() == pnodeVarArg->sym);
  10202. ref->GetSym()->SetDecl(pnodeVarArg);
  10203. }
  10204. else
  10205. {
  10206. ref->SetSym(pnodeArg->AsParseNodeVar()->sym);
  10207. scope->AddNewSymbol(pnodeVarArg->sym);
  10208. }
  10209. }
  10210. };
  10211. MapFormals(pnodeFnc, addArgsToScope);
  10212. MapFormalsFromPattern(pnodeFnc, addArgsToScope);
  10213. ParseNodeBlock * pnodeInnerBlock = this->StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
  10214. pnodeFnc->pnodeBodyScope = pnodeInnerBlock;
  10215. // Set the parameter block's child to the function body block.
  10216. *m_ppnodeScope = pnodeInnerBlock;
  10217. ParseNodePtr *ppnodeScopeSave = nullptr;
  10218. ParseNodePtr *ppnodeExprScopeSave = nullptr;
  10219. ppnodeScopeSave = m_ppnodeScope;
  10220. // This synthetic block scope will contain all the nested scopes.
  10221. m_ppnodeScope = &pnodeInnerBlock->pnodeScopes;
  10222. pnodeInnerBlock->pnodeStmt = pnodeFnc;
  10223. // Keep nested function declarations and expressions in the same list at function scope.
  10224. // (Indicate this by nulling out the current function expressions list.)
  10225. ppnodeExprScopeSave = m_ppnodeExprScope;
  10226. m_ppnodeExprScope = nullptr;
  10227. // Shouldn't be any temps in the arg list.
  10228. Assert(*m_ppnodeVar == nullptr);
  10229. // Start the var list.
  10230. m_ppnodeVar = varNodesList;
  10231. if (scope != nullptr)
  10232. {
  10233. Assert(pnodeFnc->IsBodyAndParamScopeMerged());
  10234. blockId = GetCurrentBlock()->blockId;
  10235. funcId = GetCurrentFunctionNode()->functionId;
  10236. scope->ForEachSymbol([this, blockId, funcId](Symbol* paramSym)
  10237. {
  10238. PidRefStack* ref = this->FindOrAddPidRef(paramSym->GetPid(), blockId, funcId);
  10239. ref->SetSym(paramSym);
  10240. });
  10241. }
  10242. Assert(m_currentNodeNonLambdaFunc == nullptr);
  10243. m_currentNodeNonLambdaFunc = pnodeFnc;
  10244. this->FinishFncNode(pnodeFnc);
  10245. Assert(pnodeFnc == m_currentNodeNonLambdaFunc);
  10246. m_currentNodeNonLambdaFunc = nullptr;
  10247. m_ppnodeExprScope = ppnodeExprScopeSave;
  10248. Assert(m_ppnodeScope);
  10249. Assert(nullptr == *m_ppnodeScope);
  10250. m_ppnodeScope = ppnodeScopeSave;
  10251. this->FinishParseBlock(pnodeInnerBlock);
  10252. if (!pnodeFnc->IsModule() && (m_token.tk == tkLCurly || !pnodeFnc->IsLambda()))
  10253. {
  10254. UpdateArgumentsNode(pnodeFnc, argNode);
  10255. }
  10256. CreateSpecialSymbolDeclarations(pnodeFnc);
  10257. this->FinishParseBlock(pnodeBlock);
  10258. if (pnodeFncExprBlock)
  10259. {
  10260. this->FinishParseBlock(pnodeFncExprBlock);
  10261. }
  10262. this->m_currentNodeFunc = pnodeFncSave;
  10263. }
  10264. });
  10265. this->RestoreContext(&parseContext);
  10266. }
  10267. void Parser::InitPids()
  10268. {
  10269. wellKnownPropertyPids.arguments = this->GetHashTbl()->PidHashNameLen(g_ssym_arguments.sz, g_ssym_arguments.cch);
  10270. wellKnownPropertyPids.async = this->GetHashTbl()->PidHashNameLen(g_ssym_async.sz, g_ssym_async.cch);
  10271. wellKnownPropertyPids.eval = this->GetHashTbl()->PidHashNameLen(g_ssym_eval.sz, g_ssym_eval.cch);
  10272. wellKnownPropertyPids.get = this->GetHashTbl()->PidHashNameLen(g_ssym_get.sz, g_ssym_get.cch);
  10273. wellKnownPropertyPids.set = this->GetHashTbl()->PidHashNameLen(g_ssym_set.sz, g_ssym_set.cch);
  10274. wellKnownPropertyPids.let = this->GetHashTbl()->PidHashNameLen(g_ssym_let.sz, g_ssym_let.cch);
  10275. wellKnownPropertyPids.await = this->GetHashTbl()->PidHashNameLen(g_ssym_await.sz, g_ssym_await.cch);
  10276. wellKnownPropertyPids.constructor = this->GetHashTbl()->PidHashNameLen(g_ssym_constructor.sz, g_ssym_constructor.cch);
  10277. wellKnownPropertyPids.prototype = this->GetHashTbl()->PidHashNameLen(g_ssym_prototype.sz, g_ssym_prototype.cch);
  10278. wellKnownPropertyPids.__proto__ = this->GetHashTbl()->PidHashNameLen(_u("__proto__"), sizeof("__proto__") - 1);
  10279. wellKnownPropertyPids.of = this->GetHashTbl()->PidHashNameLen(_u("of"), sizeof("of") - 1);
  10280. wellKnownPropertyPids.target = this->GetHashTbl()->PidHashNameLen(_u("target"), sizeof("target") - 1);
  10281. wellKnownPropertyPids.meta = this->GetHashTbl()->PidHashNameLen(_u("meta"), sizeof("meta") - 1);
  10282. wellKnownPropertyPids.as = this->GetHashTbl()->PidHashNameLen(_u("as"), sizeof("as") - 1);
  10283. wellKnownPropertyPids.from = this->GetHashTbl()->PidHashNameLen(_u("from"), sizeof("from") - 1);
  10284. wellKnownPropertyPids._default = this->GetHashTbl()->PidHashNameLen(_u("default"), sizeof("default") - 1);
  10285. wellKnownPropertyPids._star = this->GetHashTbl()->PidHashNameLen(_u("*"), sizeof("*") - 1);
  10286. wellKnownPropertyPids._this = this->GetHashTbl()->PidHashNameLen(_u("*this*"), sizeof("*this*") - 1);
  10287. wellKnownPropertyPids._newTarget = this->GetHashTbl()->PidHashNameLen(_u("*new.target*"), sizeof("*new.target*") - 1);
  10288. wellKnownPropertyPids._super = this->GetHashTbl()->PidHashNameLen(_u("*super*"), sizeof("*super*") - 1);
  10289. wellKnownPropertyPids._superConstructor = this->GetHashTbl()->PidHashNameLen(_u("*superconstructor*"), sizeof("*superconstructor*") - 1);
  10290. wellKnownPropertyPids._importMeta = this->GetHashTbl()->PidHashNameLen(_u("*import.meta*"), sizeof("*import.meta*") - 1);
  10291. }
  10292. void Parser::RestoreScopeInfo(Js::ScopeInfo * scopeInfo)
  10293. {
  10294. if (!scopeInfo)
  10295. {
  10296. return;
  10297. }
  10298. PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
  10299. RestoreScopeInfo(scopeInfo->GetParentScopeInfo()); // Recursively restore outer func scope info
  10300. scopeInfo->SetScopeId(m_nextBlockId);
  10301. ParseNodeBlock * pnodeScope = nullptr;
  10302. ScopeType scopeType = scopeInfo->GetScopeType();
  10303. PnodeBlockType blockType;
  10304. switch (scopeType)
  10305. {
  10306. case ScopeType_With:
  10307. PushDynamicBlock();
  10308. // fall through
  10309. case ScopeType_Block:
  10310. case ScopeType_Catch:
  10311. case ScopeType_CatchParamPattern:
  10312. case ScopeType_GlobalEvalBlock:
  10313. blockType = PnodeBlockType::Regular;
  10314. break;
  10315. case ScopeType_FunctionBody:
  10316. case ScopeType_FuncExpr:
  10317. blockType = PnodeBlockType::Function;
  10318. break;
  10319. case ScopeType_Parameter:
  10320. blockType = PnodeBlockType::Parameter;
  10321. break;
  10322. default:
  10323. Assert(0);
  10324. return;
  10325. }
  10326. pnodeScope = StartParseBlockWithCapacity<true>(blockType, scopeType, scopeInfo->GetSymbolCount());
  10327. Scope *scope = pnodeScope->scope;
  10328. scope->SetScopeInfo(scopeInfo);
  10329. scopeInfo->ExtractScopeInfo(this, /*nullptr, nullptr,*/ scope);
  10330. }
  10331. void Parser::FinishScopeInfo(Js::ScopeInfo * scopeInfo)
  10332. {
  10333. PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
  10334. for (; scopeInfo != nullptr; scopeInfo = scopeInfo->GetParentScopeInfo())
  10335. {
  10336. int scopeId = scopeInfo->GetScopeId();
  10337. scopeInfo->GetScope()->ForEachSymbol([this, scopeId](Symbol *sym)
  10338. {
  10339. this->BindPidRefsInScope(sym->GetPid(), sym, scopeId);
  10340. });
  10341. PopScope(scopeInfo->GetScope());
  10342. PopStmt(&m_currentBlockInfo->pstmt);
  10343. PopBlockInfo();
  10344. }
  10345. }
  10346. /***************************************************************************
  10347. Parse the code.
  10348. ***************************************************************************/
  10349. ParseNodeProg * Parser::Parse(LPCUTF8 pszSrc, size_t offset, size_t length, charcount_t charOffset, bool isUtf8, ULONG grfscr, ULONG lineNumber, Js::LocalFunctionId * nextFunctionId, CompileScriptException *pse)
  10350. {
  10351. ParseNodeProg * pnodeProg;
  10352. ParseNodePtr *lastNodeRef = nullptr;
  10353. m_nextBlockId = 0;
  10354. bool isDeferred = (grfscr & fscrDeferredFnc) != 0;
  10355. bool isModuleSource = (grfscr & fscrIsModuleCode) != 0;
  10356. bool isGlobalCode = (grfscr & fscrGlobalCode) != 0;
  10357. if (this->m_scriptContext->IsScriptContextInDebugMode()
  10358. #ifdef ENABLE_PREJIT
  10359. || Js::Configuration::Global.flags.Prejit
  10360. #endif
  10361. || ((grfscr & fscrNoDeferParse) != 0)
  10362. )
  10363. {
  10364. // Don't do deferred parsing if debugger is attached or feature is disabled
  10365. // by command-line switch.
  10366. grfscr &= ~fscrWillDeferFncParse;
  10367. }
  10368. else if (!isGlobalCode &&
  10369. (
  10370. PHASE_OFF1(Js::Phase::DeferEventHandlersPhase) ||
  10371. this->m_scriptContext->IsScriptContextInSourceRundownOrDebugMode()
  10372. )
  10373. )
  10374. {
  10375. // Don't defer event handlers in debug/rundown mode, because we need to register the document,
  10376. // so we need to create a full FunctionBody for the script body.
  10377. grfscr &= ~fscrWillDeferFncParse;
  10378. }
  10379. m_grfscr = grfscr;
  10380. m_length = length;
  10381. m_originalLength = length;
  10382. m_nextFunctionId = nextFunctionId;
  10383. if (m_parseType != ParseType_Deferred)
  10384. {
  10385. JS_ETW(EventWriteJSCRIPT_PARSE_METHOD_START(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), *m_nextFunctionId, 0, m_parseType, Js::Constants::GlobalFunction));
  10386. OUTPUT_TRACE(Js::DeferParsePhase, _u("Parsing function (%s) : %s (%d)\n"), GetParseType(), Js::Constants::GlobalFunction, *m_nextFunctionId);
  10387. }
  10388. // Give the scanner the source and get the first token
  10389. this->GetScanner()->SetText(pszSrc, offset, length, charOffset, isUtf8, grfscr, lineNumber);
  10390. this->GetScanner()->Scan();
  10391. // Make the main 'knopProg' node
  10392. int32 initSize = 0;
  10393. m_pCurrentAstSize = &initSize;
  10394. pnodeProg = CreateProgNode(isModuleSource, lineNumber);
  10395. if (!isDeferred || (isDeferred && isGlobalCode))
  10396. {
  10397. // In the deferred case, if the global function is deferred parse (which is in no-refresh case),
  10398. // we will re-use the same function body, so start with the correct functionId.
  10399. pnodeProg->functionId = (*m_nextFunctionId)++;
  10400. }
  10401. if (isModuleSource)
  10402. {
  10403. Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
  10404. pnodeProg->AsParseNodeModule()->localExportEntries = nullptr;
  10405. pnodeProg->AsParseNodeModule()->indirectExportEntries = nullptr;
  10406. pnodeProg->AsParseNodeModule()->starExportEntries = nullptr;
  10407. pnodeProg->AsParseNodeModule()->importEntries = nullptr;
  10408. pnodeProg->AsParseNodeModule()->requestedModules = nullptr;
  10409. }
  10410. m_pCurrentAstSize = &(pnodeProg->astSize);
  10411. // initialize parsing variables
  10412. m_currentNodeFunc = nullptr;
  10413. m_currentNodeDeferredFunc = nullptr;
  10414. m_currentNodeProg = pnodeProg;
  10415. m_cactIdentToNodeLookup = 1;
  10416. m_pnestedCount = &pnodeProg->nestedCount;
  10417. m_inDeferredNestedFunc = false;
  10418. m_ppnodeVar = &pnodeProg->pnodeVars;
  10419. SetCurrentStatement(nullptr);
  10420. AssertMsg(m_pstmtCur == nullptr, "Statement stack should be empty when we start parse global code");
  10421. // Create block for const's and let's
  10422. ParseNodeBlock * pnodeGlobalBlock = StartParseBlock<true>(PnodeBlockType::Global, ScopeType_Global);
  10423. pnodeProg->scope = pnodeGlobalBlock->scope;
  10424. ParseNodeBlock * pnodeGlobalEvalBlock = nullptr;
  10425. // Don't track function expressions separately from declarations at global scope.
  10426. m_ppnodeExprScope = nullptr;
  10427. // This synthetic block scope will contain all the nested scopes.
  10428. pnodeProg->pnodeScopes = pnodeGlobalBlock;
  10429. m_ppnodeScope = &pnodeGlobalBlock->pnodeScopes;
  10430. if ((this->m_grfscr & fscrEvalCode) &&
  10431. !(this->m_functionBody && this->m_functionBody->GetScopeInfo()))
  10432. {
  10433. pnodeGlobalEvalBlock = StartParseBlock<true>(PnodeBlockType::Regular, ScopeType_GlobalEvalBlock);
  10434. pnodeProg->pnodeScopes = pnodeGlobalEvalBlock;
  10435. m_ppnodeScope = &pnodeGlobalEvalBlock->pnodeScopes;
  10436. }
  10437. Js::ScopeInfo *scopeInfo = nullptr;
  10438. if (m_parseType == ParseType_Deferred && m_functionBody)
  10439. {
  10440. // this->m_functionBody can be cleared during parsing, but we need access to the scope info later.
  10441. scopeInfo = m_functionBody->GetScopeInfo();
  10442. if (scopeInfo)
  10443. {
  10444. // Create an enclosing function context.
  10445. m_currentNodeFunc = CreateNodeForOpT<knopFncDecl>();
  10446. m_currentNodeFunc->functionId = m_functionBody->GetLocalFunctionId();
  10447. m_currentNodeFunc->nestedCount = m_functionBody->GetNestedCount();
  10448. m_currentNodeFunc->SetStrictMode(!!this->m_fUseStrictMode);
  10449. this->RestoreScopeInfo(scopeInfo);
  10450. m_currentNodeFunc->SetIsGenerator(scopeInfo->IsGeneratorFunctionBody());
  10451. m_currentNodeFunc->SetIsAsync(scopeInfo->IsAsyncFunctionBody());
  10452. m_currentNodeFunc->SetIsClassConstructor(scopeInfo->IsClassConstructor());
  10453. }
  10454. }
  10455. // It's possible for the module global to be defer-parsed in debug scenarios.
  10456. if (isModuleSource && (!isDeferred || (isDeferred && isGlobalCode)))
  10457. {
  10458. ParseNodePtr moduleFunction = GenerateModuleFunctionWrapper<true>();
  10459. pnodeProg->pnodeBody = nullptr;
  10460. AddToNodeList(&pnodeProg->pnodeBody, &lastNodeRef, moduleFunction);
  10461. }
  10462. else
  10463. {
  10464. if (isDeferred && !isGlobalCode)
  10465. {
  10466. // Defer parse for a single function should just parse that one function - there are no other statements.
  10467. ushort flags = fFncNoFlgs;
  10468. bool isAsync = false;
  10469. bool isGenerator = false;
  10470. bool isMethod = false;
  10471. // The top-level deferred function body was defined by a function expression whose parsing was deferred. We are now
  10472. // parsing it, so unset the flag so that any nested functions are parsed normally. This flag is only applicable the
  10473. // first time we see it.
  10474. //
  10475. // Normally, deferred functions will be parsed in ParseStatement upon encountering the 'function' token. The first
  10476. // token of the source code of the function may not be a 'function' token though, so we still need to reset this flag
  10477. // for the first function we parse. This can happen in compat modes, for instance, for a function expression enclosed
  10478. // in parentheses, where the legacy behavior was to include the parentheses in the function's source code.
  10479. if (m_grfscr & fscrDeferredFncExpression)
  10480. {
  10481. m_grfscr &= ~fscrDeferredFncExpression;
  10482. }
  10483. else
  10484. {
  10485. flags |= fFncDeclaration;
  10486. }
  10487. if (m_grfscr & fscrDeferredFncIsMethod)
  10488. {
  10489. m_grfscr &= ~fscrDeferredFncIsMethod;
  10490. isMethod = true;
  10491. flags |= fFncNoName | fFncMethod;
  10492. if (m_grfscr & fscrDeferredFncIsGenerator)
  10493. {
  10494. m_grfscr &= ~fscrDeferredFncIsGenerator;
  10495. isGenerator = true;
  10496. flags |= fFncGenerator;
  10497. }
  10498. if (m_token.tk == tkStar && m_scriptContext->GetConfig()->IsES6GeneratorsEnabled())
  10499. {
  10500. Assert(isGenerator && !isMethod);
  10501. this->GetScanner()->Scan();
  10502. }
  10503. }
  10504. if (m_grfscr & fscrDeferredFncIsAsync)
  10505. {
  10506. m_grfscr &= ~fscrDeferredFncIsAsync;
  10507. isAsync = true;
  10508. flags |= fFncAsync;
  10509. }
  10510. if (m_grfscr & fscrDeferredFncIsClassConstructor)
  10511. {
  10512. m_grfscr &= ~fscrDeferredFncIsClassConstructor;
  10513. flags |= fFncClassConstructor | fFncClassMember;
  10514. }
  10515. if (m_grfscr & fscrDeferredFncIsBaseClassConstructor)
  10516. {
  10517. m_grfscr &= ~fscrDeferredFncIsBaseClassConstructor;
  10518. flags |= fFncBaseClassConstructor;
  10519. }
  10520. if (m_grfscr & fscrDeferredFncIsClassMember)
  10521. {
  10522. m_grfscr &= ~fscrDeferredFncIsClassMember;
  10523. flags |= fFncClassMember;
  10524. }
  10525. #if DBG
  10526. if (isMethod && m_token.tk == tkID)
  10527. {
  10528. RestorePoint atPid;
  10529. IdentPtr pidHint = m_token.GetIdentifier(this->GetHashTbl());
  10530. this->GetScanner()->Capture(&atPid);
  10531. this->GetScanner()->Scan();
  10532. if ((pidHint == wellKnownPropertyPids.get || pidHint == wellKnownPropertyPids.set) && NextTokenIsPropertyNameStart())
  10533. {
  10534. // Getter/setter
  10535. // Skip the get/set keyword and continue normally
  10536. AssertMsg(false, "We should not be re-parsing the get/set part of member accessor functions");
  10537. }
  10538. else
  10539. {
  10540. // Not a getter/setter; rewind and treat the token as a name.
  10541. this->GetScanner()->SeekTo(atPid);
  10542. }
  10543. }
  10544. #endif
  10545. // Ensure this isn't a computed name
  10546. AssertMsg(!(m_token.tk == tkLBrack && isMethod), "Can't defer parse a computed name expression, we should have started after this");
  10547. if (!isMethod && (m_token.tk == tkID || m_token.tk == tkLParen))
  10548. {
  10549. // If first token of the function is tkID or tkLParen, this is a lambda.
  10550. flags |= fFncLambda;
  10551. }
  10552. ParseNode * pnodeFnc = ParseFncDeclCheckScope<true>(flags);
  10553. pnodeProg->pnodeBody = nullptr;
  10554. AddToNodeList(&pnodeProg->pnodeBody, &lastNodeRef, pnodeFnc);
  10555. // No need to update the cbStringMin property since no ParseableFunctionInfo will be created from this defer-parsed pnodeFnc
  10556. }
  10557. else
  10558. {
  10559. // Process a sequence of statements/declarations
  10560. ParseStmtList<true>(
  10561. &pnodeProg->pnodeBody,
  10562. &lastNodeRef,
  10563. SM_OnGlobalCode,
  10564. !(m_grfscr & fscrDeferredFncExpression) /* isSourceElementList */);
  10565. }
  10566. }
  10567. if (m_parseType == ParseType_Deferred)
  10568. {
  10569. if (scopeInfo)
  10570. {
  10571. this->FinishScopeInfo(scopeInfo);
  10572. }
  10573. }
  10574. pnodeProg->m_UsesArgumentsAtGlobal = m_UsesArgumentsAtGlobal;
  10575. if (IsStrictMode())
  10576. {
  10577. pnodeProg->SetStrictMode();
  10578. }
  10579. #if DEBUG
  10580. if (m_grfscr & fscrEnforceJSON && !IsJSONValid(pnodeProg->pnodeBody))
  10581. {
  10582. Error(ERRsyntax);
  10583. }
  10584. #endif
  10585. if (tkEOF != m_token.tk)
  10586. Error(ERRsyntax);
  10587. // Append an EndCode node.
  10588. AddToNodeList(&pnodeProg->pnodeBody, &lastNodeRef,
  10589. CreateNodeForOpT<knopEndCode>());
  10590. Assert(lastNodeRef);
  10591. Assert(*lastNodeRef);
  10592. Assert((*lastNodeRef)->nop == knopEndCode);
  10593. (*lastNodeRef)->ichMin = 0;
  10594. (*lastNodeRef)->ichLim = 0;
  10595. // Get the extent of the code.
  10596. pnodeProg->ichLim = this->GetScanner()->IchLimTok();
  10597. pnodeProg->cbLim = this->GetScanner()->IecpLimTok();
  10598. // Terminate the local list
  10599. *m_ppnodeVar = nullptr;
  10600. Assert(nullptr == *m_ppnodeScope);
  10601. Assert(nullptr == pnodeProg->pnodeNext);
  10602. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  10603. if (Js::Configuration::Global.flags.IsEnabled(Js::ForceUndoDeferFlag))
  10604. {
  10605. m_stoppedDeferredParse = true;
  10606. }
  10607. #endif
  10608. if (m_stoppedDeferredParse)
  10609. {
  10610. #if ENABLE_BACKGROUND_PARSING
  10611. if (this->m_hasParallelJob)
  10612. {
  10613. BackgroundParser *bgp = static_cast<BackgroundParser*>(m_scriptContext->GetBackgroundParser());
  10614. Assert(bgp);
  10615. this->WaitForBackgroundJobs(bgp, pse);
  10616. }
  10617. #endif
  10618. // Do any remaining bindings of globals referenced in non-deferred functions.
  10619. if (pnodeGlobalEvalBlock)
  10620. {
  10621. FinishParseBlock(pnodeGlobalEvalBlock);
  10622. }
  10623. FinishParseBlock(pnodeGlobalBlock);
  10624. // Clear out references to undeclared identifiers.
  10625. this->GetHashTbl()->VisitPids([&](IdentPtr pid) { pid->SetTopRef(nullptr); });
  10626. // Restore global scope and blockinfo stacks preparatory to reparsing deferred functions.
  10627. PushScope(pnodeGlobalBlock->scope);
  10628. BlockInfoStack *newBlockInfo = PushBlockInfo(pnodeGlobalBlock);
  10629. PushStmt<true>(&newBlockInfo->pstmt, pnodeGlobalBlock, knopBlock, nullptr);
  10630. if (pnodeGlobalEvalBlock)
  10631. {
  10632. PushScope(pnodeGlobalEvalBlock->scope);
  10633. newBlockInfo = PushBlockInfo(pnodeGlobalEvalBlock);
  10634. PushStmt<true>(&newBlockInfo->pstmt, pnodeGlobalEvalBlock, knopBlock, nullptr);
  10635. }
  10636. // Finally, see if there are any function bodies we now want to generate because we
  10637. // decided to stop deferring.
  10638. FinishDeferredFunction(pnodeProg->pnodeScopes);
  10639. }
  10640. if (pnodeGlobalEvalBlock)
  10641. {
  10642. FinishParseBlock(pnodeGlobalEvalBlock);
  10643. }
  10644. // Append block as body of pnodeProg
  10645. FinishParseBlock(pnodeGlobalBlock);
  10646. m_scriptContext->AddSourceSize(m_length);
  10647. if (m_parseType != ParseType_Deferred)
  10648. {
  10649. JS_ETW(EventWriteJSCRIPT_PARSE_METHOD_STOP(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), pnodeProg->functionId, *m_pCurrentAstSize, false, Js::Constants::GlobalFunction));
  10650. }
  10651. if (isModuleSource)
  10652. {
  10653. // verify that any local module exports are defined
  10654. VerifyModuleLocalExportEntries();
  10655. }
  10656. return pnodeProg;
  10657. }
  10658. bool Parser::CheckForDirective(bool* pIsUseStrict, bool *pIsUseAsm, bool* pIsOctalInString)
  10659. {
  10660. // A directive is a string constant followed by a statement terminating token
  10661. if (m_token.tk != tkStrCon)
  10662. return false;
  10663. // Careful, need to check for octal before calling this->GetScanner()->Scan()
  10664. // because Scan() clears the "had octal" flag on the scanner and
  10665. // this->GetScanner()->Restore() does not restore this flag.
  10666. if (pIsOctalInString != nullptr)
  10667. {
  10668. *pIsOctalInString = this->GetScanner()->IsOctOrLeadingZeroOnLastTKNumber();
  10669. }
  10670. Ident* pidDirective = m_token.GetStr();
  10671. RestorePoint start;
  10672. this->GetScanner()->Capture(&start);
  10673. this->GetScanner()->Scan();
  10674. bool isDirective = true;
  10675. switch (m_token.tk)
  10676. {
  10677. case tkSColon:
  10678. case tkEOF:
  10679. case tkLCurly:
  10680. case tkRCurly:
  10681. break;
  10682. default:
  10683. if (!this->GetScanner()->FHadNewLine())
  10684. {
  10685. isDirective = false;
  10686. }
  10687. break;
  10688. }
  10689. if (isDirective)
  10690. {
  10691. if (pIsUseStrict != nullptr)
  10692. {
  10693. *pIsUseStrict = CheckStrictModeStrPid(pidDirective);
  10694. }
  10695. if (pIsUseAsm != nullptr)
  10696. {
  10697. *pIsUseAsm = CheckAsmjsModeStrPid(pidDirective);
  10698. }
  10699. }
  10700. this->GetScanner()->SeekTo(start);
  10701. return isDirective;
  10702. }
  10703. bool Parser::CheckStrictModeStrPid(IdentPtr pid)
  10704. {
  10705. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  10706. if (Js::Configuration::Global.flags.NoStrictMode)
  10707. return false;
  10708. #endif
  10709. return pid != nullptr &&
  10710. pid->Cch() == 10 &&
  10711. !this->GetScanner()->IsEscapeOnLastTkStrCon() &&
  10712. wcsncmp(pid->Psz(), _u("use strict"), 10) == 0;
  10713. }
  10714. bool Parser::CheckAsmjsModeStrPid(IdentPtr pid)
  10715. {
  10716. #ifdef ASMJS_PLAT
  10717. if (!CONFIG_FLAG(AsmJs))
  10718. {
  10719. return false;
  10720. }
  10721. bool isAsmCandidate = (pid != nullptr &&
  10722. AutoSystemInfo::Data.SSE2Available() &&
  10723. pid->Cch() == 7 &&
  10724. !this->GetScanner()->IsEscapeOnLastTkStrCon() &&
  10725. wcsncmp(pid->Psz(), _u("use asm"), 10) == 0);
  10726. #ifdef ENABLE_SCRIPT_DEBUGGING
  10727. if (isAsmCandidate && m_scriptContext->IsScriptContextInDebugMode())
  10728. {
  10729. // We would like to report this to debugger - they may choose to disable debugging.
  10730. // TODO : localization of the string?
  10731. m_scriptContext->RaiseMessageToDebugger(DEIT_ASMJS_IN_DEBUGGING, _u("AsmJs initialization error - AsmJs disabled due to script debugger"), m_sourceContextInfo && !m_sourceContextInfo->IsDynamic() ? m_sourceContextInfo->url : nullptr);
  10732. return false;
  10733. }
  10734. #endif
  10735. return isAsmCandidate && !(m_grfscr & fscrNoAsmJs);
  10736. #else
  10737. return false;
  10738. #endif
  10739. }
  10740. HRESULT Parser::ParseUtf8Source(__out ParseNodeProg ** parseTree, LPCUTF8 pSrc, size_t length, ULONG grfsrc, CompileScriptException *pse,
  10741. Js::LocalFunctionId * nextFunctionId, SourceContextInfo * sourceContextInfo)
  10742. {
  10743. m_functionBody = nullptr;
  10744. m_parseType = ParseType_Upfront;
  10745. return ParseSourceInternal(parseTree, pSrc, 0, length, 0, true, grfsrc, pse, nextFunctionId, 0, sourceContextInfo);
  10746. }
  10747. HRESULT Parser::ParseCesu8Source(__out ParseNodeProg ** parseTree, LPCUTF8 pSrc, size_t length, ULONG grfsrc, CompileScriptException *pse,
  10748. Js::LocalFunctionId * nextFunctionId, SourceContextInfo * sourceContextInfo)
  10749. {
  10750. m_functionBody = nullptr;
  10751. m_parseType = ParseType_Upfront;
  10752. return ParseSourceInternal(parseTree, pSrc, 0, length, 0, false, grfsrc, pse, nextFunctionId, 0, sourceContextInfo);
  10753. }
  10754. #if ENABLE_BACKGROUND_PARSING
  10755. void Parser::PrepareForBackgroundParse()
  10756. {
  10757. this->GetScanner()->PrepareForBackgroundParse(m_scriptContext);
  10758. }
  10759. void Parser::AddBackgroundParseItem(BackgroundParseItem *const item)
  10760. {
  10761. if (currBackgroundParseItem == nullptr)
  10762. {
  10763. backgroundParseItems = item;
  10764. }
  10765. else
  10766. {
  10767. currBackgroundParseItem->SetNext(item);
  10768. }
  10769. currBackgroundParseItem = item;
  10770. }
  10771. void Parser::AddFastScannedRegExpNode(ParseNodePtr const pnode)
  10772. {
  10773. Assert(!IsBackgroundParser());
  10774. Assert(m_doingFastScan);
  10775. if (fastScannedRegExpNodes == nullptr)
  10776. {
  10777. fastScannedRegExpNodes = Anew(&m_nodeAllocator, NodeDList, &m_nodeAllocator);
  10778. }
  10779. fastScannedRegExpNodes->Append(pnode);
  10780. }
  10781. void Parser::AddBackgroundRegExpNode(ParseNodePtr const pnode)
  10782. {
  10783. Assert(IsBackgroundParser());
  10784. Assert(currBackgroundParseItem != nullptr);
  10785. currBackgroundParseItem->AddRegExpNode(pnode, &m_nodeAllocator);
  10786. }
  10787. HRESULT Parser::ParseFunctionInBackground(ParseNodeFnc * pnodeFnc, ParseContext *parseContext, bool topLevelDeferred, CompileScriptException *pse)
  10788. {
  10789. m_functionBody = nullptr;
  10790. m_parseType = ParseType_Upfront;
  10791. HRESULT hr = S_OK;
  10792. SmartFPUControl smartFpuControl;
  10793. uint nextFunctionId = pnodeFnc->functionId + 1;
  10794. this->RestoreContext(parseContext);
  10795. m_nextFunctionId = &nextFunctionId;
  10796. m_deferringAST = topLevelDeferred;
  10797. m_inDeferredNestedFunc = false;
  10798. m_scopeCountNoAst = 0;
  10799. SetCurrentStatement(nullptr);
  10800. pnodeFnc->pnodeVars = nullptr;
  10801. pnodeFnc->pnodeParams = nullptr;
  10802. pnodeFnc->pnodeBody = nullptr;
  10803. pnodeFnc->nestedCount = 0;
  10804. ParseNodeFnc * pnodeParentFnc = GetCurrentFunctionNode();
  10805. m_currentNodeFunc = pnodeFnc;
  10806. m_currentNodeDeferredFunc = nullptr;
  10807. m_ppnodeScope = nullptr;
  10808. m_ppnodeExprScope = nullptr;
  10809. m_pnestedCount = &pnodeFnc->nestedCount;
  10810. m_pCurrentAstSize = &pnodeFnc->astSize;
  10811. ParseNodeBlock * pnodeBlock = StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
  10812. pnodeFnc->pnodeScopes = pnodeBlock;
  10813. m_ppnodeScope = &pnodeBlock->pnodeScopes;
  10814. bool handled = false;
  10815. uint uDeferSave = m_grfscr & (fscrCanDeferFncParse | fscrWillDeferFncParse);
  10816. try
  10817. {
  10818. this->GetScanner()->Scan();
  10819. m_ppnodeVar = &pnodeFnc->pnodeParams;
  10820. this->ParseFncFormals<true>(pnodeFnc, pnodeParentFnc, fFncNoFlgs);
  10821. if (m_token.tk == tkRParen)
  10822. {
  10823. this->GetScanner()->Scan();
  10824. }
  10825. ChkCurTok(tkLCurly, ERRnoLcurly);
  10826. m_ppnodeVar = &pnodeFnc->pnodeVars;
  10827. // Put the scanner into "no hashing" mode.
  10828. BYTE deferFlags = this->GetScanner()->SetDeferredParse(topLevelDeferred);
  10829. // Process a sequence of statements/declarations
  10830. if (topLevelDeferred)
  10831. {
  10832. ParseStmtList<false>(nullptr, nullptr, SM_DeferredParse, true);
  10833. }
  10834. else
  10835. {
  10836. ParseNodePtr *lastNodeRef = nullptr;
  10837. ParseStmtList<true>(&pnodeFnc->pnodeBody, &lastNodeRef, SM_OnFunctionCode, true);
  10838. AddArgumentsNodeToVars(pnodeFnc);
  10839. // Append an EndCode node.
  10840. AddToNodeList(&pnodeFnc->pnodeBody, &lastNodeRef, CreateNodeForOpT<knopEndCode>());
  10841. }
  10842. // Restore the scanner's default hashing mode.
  10843. this->GetScanner()->SetDeferredParseFlags(deferFlags);
  10844. #if DBG
  10845. pnodeFnc->deferredParseNextFunctionId = *this->m_nextFunctionId;
  10846. #endif
  10847. this->m_deferringAST = FALSE;
  10848. // Append block as body of pnodeProg
  10849. FinishParseBlock(pnodeBlock);
  10850. }
  10851. catch (ParseExceptionObject& e)
  10852. {
  10853. hr = e.GetError();
  10854. hr = pse->ProcessError(this->GetScanner(), hr, nullptr, e.GetStringOne(), e.GetStringTwo());
  10855. handled = true;
  10856. }
  10857. if (handled == false && FAILED(hr))
  10858. {
  10859. hr = pse->ProcessError(this->GetScanner(), hr, nullptr);
  10860. }
  10861. if (IsStrictMode())
  10862. {
  10863. pnodeFnc->SetStrictMode();
  10864. }
  10865. if (topLevelDeferred)
  10866. {
  10867. pnodeFnc->pnodeVars = nullptr;
  10868. }
  10869. m_grfscr |= uDeferSave;
  10870. Assert(nullptr == *m_ppnodeScope);
  10871. return hr;
  10872. }
  10873. #endif
  10874. HRESULT Parser::ParseSourceWithOffset(__out ParseNodeProg ** parseTree, LPCUTF8 pSrc, size_t offset, size_t cbLength, charcount_t cchOffset,
  10875. bool isCesu8, ULONG grfscr, CompileScriptException *pse, Js::LocalFunctionId * nextFunctionId, ULONG lineNumber, SourceContextInfo * sourceContextInfo,
  10876. Js::ParseableFunctionInfo* functionInfo)
  10877. {
  10878. m_functionBody = functionInfo;
  10879. if (m_functionBody)
  10880. {
  10881. m_currDeferredStub = m_functionBody->GetDeferredStubs();
  10882. m_currDeferredStubCount = m_currDeferredStub != nullptr ? m_functionBody->GetNestedCount() : 0;
  10883. m_InAsmMode = grfscr & fscrNoAsmJs ? false : m_functionBody->GetIsAsmjsMode();
  10884. }
  10885. m_deferAsmJs = !m_InAsmMode;
  10886. m_parseType = ParseType_Deferred;
  10887. return ParseSourceInternal(parseTree, pSrc, offset, cbLength, cchOffset, !isCesu8, grfscr, pse, nextFunctionId, lineNumber, sourceContextInfo);
  10888. }
  10889. bool Parser::IsStrictMode() const
  10890. {
  10891. return (m_fUseStrictMode ||
  10892. (m_currentNodeFunc != nullptr && m_currentNodeFunc->GetStrictMode()));
  10893. }
  10894. BOOL Parser::ExpectingExternalSource()
  10895. {
  10896. return m_fExpectExternalSource;
  10897. }
  10898. Symbol *ParseNodeFnc::GetFuncSymbol()
  10899. {
  10900. if (pnodeName)
  10901. {
  10902. Assert(pnodeName->nop == knopVarDecl);
  10903. return pnodeName->sym;
  10904. }
  10905. return nullptr;
  10906. }
  10907. void ParseNodeFnc::SetFuncSymbol(Symbol *sym)
  10908. {
  10909. Assert(pnodeName);
  10910. Assert(pnodeName->nop == knopVarDecl);
  10911. pnodeName->sym = sym;
  10912. }
  10913. ParseNodePtr ParseNodeFnc::GetParamScope() const
  10914. {
  10915. if (this->pnodeScopes == nullptr)
  10916. {
  10917. return nullptr;
  10918. }
  10919. Assert(this->pnodeScopes->nop == knopBlock &&
  10920. this->pnodeScopes->pnodeNext == nullptr);
  10921. return this->pnodeScopes->pnodeScopes;
  10922. }
  10923. ParseNodePtr ParseNodeFnc::GetBodyScope() const
  10924. {
  10925. if (this->pnodeBodyScope == nullptr)
  10926. {
  10927. return nullptr;
  10928. }
  10929. Assert(this->pnodeBodyScope->nop == knopBlock &&
  10930. this->pnodeBodyScope->pnodeNext == nullptr);
  10931. return this->pnodeBodyScope->pnodeScopes;
  10932. }
  10933. bool ParseNodeBlock::HasBlockScopedContent() const
  10934. {
  10935. // A block has its own content if a let, const, or function is declared there.
  10936. if (this->pnodeLexVars != nullptr || this->blockType == Parameter)
  10937. {
  10938. return true;
  10939. }
  10940. // The enclosing scopes can contain functions and other things, so walk the list
  10941. // looking specifically for functions.
  10942. for (ParseNodePtr pnode = this->pnodeScopes; pnode;)
  10943. {
  10944. switch (pnode->nop) {
  10945. case knopFncDecl:
  10946. return true;
  10947. case knopBlock:
  10948. pnode = pnode->AsParseNodeBlock()->pnodeNext;
  10949. break;
  10950. case knopCatch:
  10951. pnode = pnode->AsParseNodeCatch()->pnodeNext;
  10952. break;
  10953. case knopWith:
  10954. pnode = pnode->AsParseNodeWith()->pnodeNext;
  10955. break;
  10956. default:
  10957. Assert(UNREACHED);
  10958. return true;
  10959. }
  10960. }
  10961. return false;
  10962. }
  10963. class ByteCodeGenerator;
  10964. // Copy AST; this works mostly on expressions for now
  10965. ParseNode* Parser::CopyPnode(ParseNode *pnode) {
  10966. if (pnode == NULL)
  10967. return NULL;
  10968. switch (pnode->nop) {
  10969. //PTNODE(knopName , "name" ,None ,Pid ,fnopLeaf)
  10970. case knopName: {
  10971. ParseNodeName * nameNode = CreateNameNode(pnode->AsParseNodeName()->pid);
  10972. nameNode->ichMin = pnode->ichMin;
  10973. nameNode->ichLim = pnode->ichLim;
  10974. nameNode->sym = pnode->AsParseNodeName()->sym;
  10975. return nameNode;
  10976. }
  10977. //PTNODE(knopInt , "int const" ,None ,Int ,fnopLeaf|fnopConst)
  10978. case knopInt:
  10979. return pnode;
  10980. //PTNODE(knopBigInt , "bigint const" ,None ,BigInt ,fnopLeaf|fnopConst)
  10981. case knopBigInt:
  10982. return pnode;
  10983. //PTNODE(knopFlt , "flt const" ,None ,Flt ,fnopLeaf|fnopConst)
  10984. case knopFlt:
  10985. return pnode;
  10986. //PTNODE(knopStr , "str const" ,None ,Pid ,fnopLeaf|fnopConst)
  10987. case knopStr:
  10988. return pnode;
  10989. //PTNODE(knopRegExp , "reg expr" ,None ,Pid ,fnopLeaf|fnopConst)
  10990. case knopRegExp:
  10991. return pnode;
  10992. break;
  10993. //PTNODE(knopNull , "null" ,Null ,None ,fnopLeaf)
  10994. case knopNull:
  10995. return pnode;
  10996. //PTNODE(knopFalse , "false" ,False ,None ,fnopLeaf)
  10997. case knopFalse:
  10998. {
  10999. ParseNode* ret = CreateNodeForOpT<knopFalse>(pnode->ichMin, pnode->ichLim);
  11000. ret->location = pnode->location;
  11001. return ret;
  11002. }
  11003. //PTNODE(knopTrue , "true" ,True ,None ,fnopLeaf)
  11004. case knopTrue:
  11005. {
  11006. ParseNode* ret = CreateNodeForOpT<knopTrue>(pnode->ichMin, pnode->ichLim);
  11007. ret->location = pnode->location;
  11008. return ret;
  11009. }
  11010. //PTNODE(knopEmpty , "empty" ,Empty ,None ,fnopLeaf)
  11011. case knopEmpty:
  11012. return CreateNodeForOpT<knopEmpty>(pnode->ichMin, pnode->ichLim);
  11013. // Unary operators.
  11014. //PTNODE(knopNot , "~" ,BitNot ,Uni ,fnopUni)
  11015. //PTNODE(knopNeg , "unary -" ,Neg ,Uni ,fnopUni)
  11016. //PTNODE(knopPos , "unary +" ,Pos ,Uni ,fnopUni)
  11017. //PTNODE(knopLogNot , "!" ,LogNot ,Uni ,fnopUni)
  11018. //PTNODE(knopEllipsis , "..." ,Spread ,Uni , fnopUni)
  11019. //PTNODE(knopDecPost , "-- post" ,Dec ,Uni ,fnopUni|fnopAsg)
  11020. //PTNODE(knopIncPre , "++ pre" ,Inc ,Uni ,fnopUni|fnopAsg)
  11021. //PTNODE(knopDecPre , "-- pre" ,Dec ,Uni ,fnopUni|fnopAsg)
  11022. //PTNODE(knopTypeof , "typeof" ,None ,Uni ,fnopUni)
  11023. //PTNODE(knopVoid , "void" ,Void ,Uni ,fnopUni)
  11024. //PTNODE(knopDelete , "delete" ,None ,Uni ,fnopUni)
  11025. case knopNot:
  11026. case knopNeg:
  11027. case knopPos:
  11028. case knopLogNot:
  11029. case knopEllipsis:
  11030. case knopIncPost:
  11031. case knopDecPost:
  11032. case knopIncPre:
  11033. case knopDecPre:
  11034. case knopTypeof:
  11035. case knopVoid:
  11036. case knopDelete:
  11037. return CreateUniNode(pnode->nop, CopyPnode(pnode->AsParseNodeUni()->pnode1), pnode->ichMin, pnode->ichLim);
  11038. //PTNODE(knopArray , "arr cnst" ,None ,Uni ,fnopUni)
  11039. //PTNODE(knopObject , "obj cnst" ,None ,Uni ,fnopUni)
  11040. case knopArray:
  11041. case knopObject:
  11042. // TODO: need to copy arr
  11043. Assert(false);
  11044. break;
  11045. // Binary operators
  11046. //PTNODE(knopAdd , "+" ,Add ,Bin ,fnopBin)
  11047. //PTNODE(knopSub , "-" ,Sub ,Bin ,fnopBin)
  11048. //PTNODE(knopMul , "*" ,Mul ,Bin ,fnopBin)
  11049. //PTNODE(knopExpo , "**" ,Expo ,Bin ,fnopBin)
  11050. //PTNODE(knopDiv , "/" ,Div ,Bin ,fnopBin)
  11051. //PTNODE(knopMod , "%" ,Mod ,Bin ,fnopBin)
  11052. //PTNODE(knopOr , "|" ,BitOr ,Bin ,fnopBin)
  11053. //PTNODE(knopXor , "^" ,BitXor ,Bin ,fnopBin)
  11054. //PTNODE(knopAnd , "&" ,BitAnd ,Bin ,fnopBin)
  11055. //PTNODE(knopEq , "==" ,EQ ,Bin ,fnopBin|fnopRel)
  11056. //PTNODE(knopNe , "!=" ,NE ,Bin ,fnopBin|fnopRel)
  11057. //PTNODE(knopLt , "<" ,LT ,Bin ,fnopBin|fnopRel)
  11058. //PTNODE(knopLe , "<=" ,LE ,Bin ,fnopBin|fnopRel)
  11059. //PTNODE(knopGe , ">=" ,GE ,Bin ,fnopBin|fnopRel)
  11060. //PTNODE(knopGt , ">" ,GT ,Bin ,fnopBin|fnopRel)
  11061. //PTNODE(knopEqv , "===" ,Eqv ,Bin ,fnopBin|fnopRel)
  11062. //PTNODE(knopIn , "in" ,In ,Bin ,fnopBin|fnopRel)
  11063. //PTNODE(knopInstOf , "instanceof",InstOf ,Bin ,fnopBin|fnopRel)
  11064. //PTNODE(knopNEqv , "!==" ,NEqv ,Bin ,fnopBin|fnopRel)
  11065. //PTNODE(knopComma , "," ,None ,Bin ,fnopBin)
  11066. //PTNODE(knopLogOr , "||" ,None ,Bin ,fnopBin)
  11067. //PTNODE(knopLogAnd , "&&" ,None ,Bin ,fnopBin)
  11068. //PTNODE(knopLsh , "<<" ,Lsh ,Bin ,fnopBin)
  11069. //PTNODE(knopRsh , ">>" ,Rsh ,Bin ,fnopBin)
  11070. //PTNODE(knopRs2 , ">>>" ,Rs2 ,Bin ,fnopBin)
  11071. case knopAdd:
  11072. case knopSub:
  11073. case knopMul:
  11074. case knopExpo:
  11075. case knopDiv:
  11076. case knopMod:
  11077. case knopOr:
  11078. case knopXor:
  11079. case knopAnd:
  11080. case knopEq:
  11081. case knopNe:
  11082. case knopLt:
  11083. case knopLe:
  11084. case knopGe:
  11085. case knopGt:
  11086. case knopEqv:
  11087. case knopIn:
  11088. case knopInstOf:
  11089. case knopNEqv:
  11090. case knopComma:
  11091. case knopLogOr:
  11092. case knopLogAnd:
  11093. case knopCoalesce:
  11094. case knopLsh:
  11095. case knopRsh:
  11096. case knopRs2:
  11097. //PTNODE(knopAsg , "=" ,None ,Bin ,fnopBin|fnopAsg)
  11098. case knopAsg:
  11099. //PTNODE(knopDot , "." ,None ,Bin ,fnopBin)
  11100. case knopDot:
  11101. //PTNODE(knopAsgAdd , "+=" ,Add ,Bin ,fnopBin|fnopAsg)
  11102. case knopAsgAdd:
  11103. //PTNODE(knopAsgSub , "-=" ,Sub ,Bin ,fnopBin|fnopAsg)
  11104. case knopAsgSub:
  11105. //PTNODE(knopAsgMul , "*=" ,Mul ,Bin ,fnopBin|fnopAsg)
  11106. case knopAsgMul:
  11107. //PTNODE(knopAsgDiv , "/=" ,Div ,Bin ,fnopBin|fnopAsg)
  11108. case knopAsgExpo:
  11109. //PTNODE(knopAsgExpo , "**=" ,Expo ,Bin ,fnopBin|fnopAsg)
  11110. case knopAsgDiv:
  11111. //PTNODE(knopAsgMod , "%=" ,Mod ,Bin ,fnopBin|fnopAsg)
  11112. case knopAsgMod:
  11113. //PTNODE(knopAsgAnd , "&=" ,BitAnd ,Bin ,fnopBin|fnopAsg)
  11114. case knopAsgAnd:
  11115. //PTNODE(knopAsgXor , "^=" ,BitXor ,Bin ,fnopBin|fnopAsg)
  11116. case knopAsgXor:
  11117. //PTNODE(knopAsgOr , "|=" ,BitOr ,Bin ,fnopBin|fnopAsg)
  11118. case knopAsgOr:
  11119. //PTNODE(knopAsgLsh , "<<=" ,Lsh ,Bin ,fnopBin|fnopAsg)
  11120. case knopAsgLsh:
  11121. //PTNODE(knopAsgRsh , ">>=" ,Rsh ,Bin ,fnopBin|fnopAsg)
  11122. case knopAsgRsh:
  11123. //PTNODE(knopAsgRs2 , ">>>=" ,Rs2 ,Bin ,fnopBin|fnopAsg)
  11124. case knopAsgRs2:
  11125. //PTNODE(knopMember , ":" ,None ,Bin ,fnopBin)
  11126. case knopMember:
  11127. case knopMemberShort:
  11128. //PTNODE(knopIndex , "[]" ,None ,Bin ,fnopBin)
  11129. //PTNODE(knopList , "<list>" ,None ,Bin ,fnopNone)
  11130. case knopIndex:
  11131. case knopList:
  11132. return CreateBinNode(pnode->nop, CopyPnode(pnode->AsParseNodeBin()->pnode1),
  11133. CopyPnode(pnode->AsParseNodeBin()->pnode2), pnode->ichMin, pnode->ichLim);
  11134. //PTNODE(knopCall , "()" ,None ,Bin ,fnopBin)
  11135. //PTNODE(knopNew , "new" ,None ,Bin ,fnopBin)
  11136. case knopNew:
  11137. case knopCall:
  11138. return CreateCallNode(pnode->nop, CopyPnode(pnode->AsParseNodeCall()->pnodeTarget),
  11139. CopyPnode(pnode->AsParseNodeCall()->pnodeArgs), pnode->ichMin, pnode->ichLim);
  11140. //PTNODE(knopQmark , "?" ,None ,Tri ,fnopBin)
  11141. case knopQmark:
  11142. return CreateTriNode(pnode->nop, CopyPnode(pnode->AsParseNodeTri()->pnode1),
  11143. CopyPnode(pnode->AsParseNodeTri()->pnode2), CopyPnode(pnode->AsParseNodeTri()->pnode3),
  11144. pnode->ichMin, pnode->ichLim);
  11145. // General nodes.
  11146. //PTNODE(knopVarDecl , "varDcl" ,None ,Var ,fnopNone)
  11147. case knopVarDecl: {
  11148. ParseNodeVar* copyNode = Anew(&m_nodeAllocator, ParseNodeVar, knopVarDecl, pnode->ichMin, pnode->ichLim, nullptr);
  11149. copyNode->pnodeInit = CopyPnode(pnode->AsParseNodeVar()->pnodeInit);
  11150. copyNode->sym = pnode->AsParseNodeVar()->sym;
  11151. // TODO: mult-decl
  11152. Assert(pnode->AsParseNodeVar()->pnodeNext == NULL);
  11153. copyNode->pnodeNext = NULL;
  11154. return copyNode;
  11155. }
  11156. //PTNODE(knopFncDecl , "fncDcl" ,None ,Fnc ,fnopLeaf)
  11157. //PTNODE(knopProg , "program" ,None ,Fnc ,fnopNone)
  11158. case knopFncDecl:
  11159. case knopProg:
  11160. Assert(false);
  11161. break;
  11162. //PTNODE(knopEndCode , "<endcode>" ,None ,None ,fnopNone)
  11163. case knopEndCode:
  11164. break;
  11165. //PTNODE(knopDebugger , "debugger" ,None ,None ,fnopNone)
  11166. case knopDebugger:
  11167. break;
  11168. //PTNODE(knopFor , "for" ,None ,For ,fnopBreak|fnopContinue)
  11169. case knopFor: {
  11170. ParseNode* copyNode = CreateNodeForOpT<knopFor>(pnode->ichMin, pnode->ichLim);
  11171. copyNode->AsParseNodeFor()->pnodeInverted = NULL;
  11172. copyNode->AsParseNodeFor()->pnodeInit = CopyPnode(pnode->AsParseNodeFor()->pnodeInit);
  11173. copyNode->AsParseNodeFor()->pnodeCond = CopyPnode(pnode->AsParseNodeFor()->pnodeCond);
  11174. copyNode->AsParseNodeFor()->pnodeIncr = CopyPnode(pnode->AsParseNodeFor()->pnodeIncr);
  11175. copyNode->AsParseNodeFor()->pnodeBody = CopyPnode(pnode->AsParseNodeFor()->pnodeBody);
  11176. return copyNode;
  11177. }
  11178. //PTNODE(knopIf , "if" ,None ,If ,fnopNone)
  11179. case knopIf:
  11180. Assert(false);
  11181. break;
  11182. //PTNODE(knopWhile , "while" ,None ,While,fnopBreak|fnopContinue)
  11183. case knopWhile:
  11184. Assert(false);
  11185. break;
  11186. //PTNODE(knopDoWhile , "do-while" ,None ,While,fnopBreak|fnopContinue)
  11187. case knopDoWhile:
  11188. Assert(false);
  11189. break;
  11190. //PTNODE(knopForIn , "for in" ,None ,ForIn,fnopBreak|fnopContinue|fnopCleanup)
  11191. case knopForIn:
  11192. Assert(false);
  11193. break;
  11194. case knopForOf:
  11195. Assert(false);
  11196. break;
  11197. case knopForAwaitOf:
  11198. Assert(false);
  11199. break;
  11200. //PTNODE(knopReturn , "return" ,None ,Uni ,fnopNone)
  11201. case knopReturn: {
  11202. ParseNode* copyNode = CreateNodeForOpT<knopReturn>(pnode->ichMin, pnode->ichLim);
  11203. copyNode->AsParseNodeReturn()->pnodeExpr = CopyPnode(pnode->AsParseNodeReturn()->pnodeExpr);
  11204. return copyNode;
  11205. }
  11206. //PTNODE(knopBlock , "{}" ,None ,Block,fnopNone)
  11207. case knopBlock: {
  11208. ParseNodeBlock* copyNode = CreateBlockNode(pnode->ichMin, pnode->ichLim, pnode->AsParseNodeBlock()->blockType);
  11209. if (pnode->grfpn & PNodeFlags::fpnSyntheticNode) {
  11210. // fpnSyntheticNode is sometimes set on PnodeBlockType::Regular blocks which
  11211. // CreateBlockNode() will not automatically set for us, so set it here if it's
  11212. // specified on the source node.
  11213. copyNode->grfpn |= PNodeFlags::fpnSyntheticNode;
  11214. }
  11215. copyNode->pnodeStmt = CopyPnode(pnode->AsParseNodeBlock()->pnodeStmt);
  11216. return copyNode;
  11217. }
  11218. //PTNODE(knopWith , "with" ,None ,With ,fnopCleanup)
  11219. case knopWith:
  11220. Assert(false);
  11221. break;
  11222. //PTNODE(knopBreak , "break" ,None ,Jump ,fnopNone)
  11223. case knopBreak:
  11224. Assert(false);
  11225. break;
  11226. //PTNODE(knopContinue , "continue" ,None ,Jump ,fnopNone)
  11227. case knopContinue:
  11228. Assert(false);
  11229. break;
  11230. //PTNODE(knopSwitch , "switch" ,None ,Switch,fnopBreak)
  11231. case knopSwitch:
  11232. Assert(false);
  11233. break;
  11234. //PTNODE(knopCase , "case" ,None ,Case ,fnopNone)
  11235. case knopCase:
  11236. Assert(false);
  11237. break;
  11238. //PTNODE(knopTryFinally,"try-finally",None,TryFinally,fnopCleanup)
  11239. case knopTryFinally:
  11240. Assert(false);
  11241. break;
  11242. case knopFinally:
  11243. Assert(false);
  11244. break;
  11245. //PTNODE(knopCatch , "catch" ,None ,Catch,fnopNone)
  11246. case knopCatch:
  11247. Assert(false);
  11248. break;
  11249. //PTNODE(knopTryCatch , "try-catch" ,None ,TryCatch ,fnopCleanup)
  11250. case knopTryCatch:
  11251. Assert(false);
  11252. break;
  11253. //PTNODE(knopTry , "try" ,None ,Try ,fnopCleanup)
  11254. case knopTry:
  11255. Assert(false);
  11256. break;
  11257. //PTNODE(knopThrow , "throw" ,None ,Uni ,fnopNone)
  11258. case knopThrow:
  11259. Assert(false);
  11260. break;
  11261. default:
  11262. Assert(false);
  11263. break;
  11264. }
  11265. return NULL;
  11266. }
  11267. // Returns true when str is string for Nan, Infinity or -Infinity.
  11268. // Does not check for double number value being in NaN/Infinity range.
  11269. // static
  11270. template<bool CheckForNegativeInfinity>
  11271. inline bool Parser::IsNaNOrInfinityLiteral(LPCOLESTR str)
  11272. {
  11273. // Note: wcscmp crashes when one of the parameters is NULL.
  11274. return str &&
  11275. (wcscmp(_u("NaN"), str) == 0 ||
  11276. wcscmp(_u("Infinity"), str) == 0 ||
  11277. (CheckForNegativeInfinity && wcscmp(_u("-Infinity"), str) == 0));
  11278. }
  11279. template <bool buildAST>
  11280. IdentPtr Parser::ParseSuper(bool fAllowCall)
  11281. {
  11282. ParseNodeFnc * currentNodeFunc = GetCurrentFunctionNode();
  11283. ParseNodeFnc * currentNonLambdaFunc = GetCurrentNonLambdaFunctionNode();
  11284. IdentPtr superPid = nullptr;
  11285. switch (m_token.tk)
  11286. {
  11287. case tkDot: // super.prop
  11288. case tkLBrack: // super[foo]
  11289. superPid = wellKnownPropertyPids._super;
  11290. break;
  11291. case tkLParen: // super(args)
  11292. superPid = wellKnownPropertyPids._superConstructor;
  11293. break;
  11294. default:
  11295. Error(ERRInvalidSuper);
  11296. break;
  11297. }
  11298. currentNodeFunc->SetHasSuperReference(TRUE);
  11299. CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ES6, Super, m_scriptContext);
  11300. // If we are defer parsing, we can skip verifying that the super reference is valid.
  11301. // If it wasn't the parser would have thrown during upfront parsing and we wouldn't be defer parsing the function.
  11302. if (m_parseType == ParseType_Deferred)
  11303. {
  11304. return superPid;
  11305. }
  11306. if (!fAllowCall && (m_token.tk == tkLParen))
  11307. {
  11308. Error(ERRInvalidSuper); // new super() is not allowed
  11309. }
  11310. else if ((currentNodeFunc->IsConstructor() && currentNodeFunc->superRestrictionState == SuperRestrictionState::CallAndPropertyAllowed)
  11311. || (currentNonLambdaFunc != nullptr && currentNonLambdaFunc->superRestrictionState == SuperRestrictionState::CallAndPropertyAllowed))
  11312. {
  11313. // Any super access is good within a class constructor
  11314. }
  11315. else if ((this->m_grfscr & fscrEval) == fscrEval || (currentNonLambdaFunc != nullptr && currentNonLambdaFunc->superRestrictionState == SuperRestrictionState::PropertyAllowed))
  11316. {
  11317. // Currently for eval cases during compile time we use propertyallowed and throw during runtime for error cases
  11318. if (m_token.tk == tkLParen)
  11319. {
  11320. if ((this->m_grfscr & fscrEval) == fscrNil)
  11321. {
  11322. // Cannot call super within a class member
  11323. Error(ERRInvalidSuper);
  11324. }
  11325. else
  11326. {
  11327. Js::JavascriptFunction * caller = nullptr;
  11328. if (Js::JavascriptStackWalker::GetCaller(&caller, m_scriptContext))
  11329. {
  11330. Js::FunctionBody * callerBody = caller->GetFunctionBody();
  11331. Assert(callerBody);
  11332. if (!callerBody->GetFunctionInfo()->GetAllowDirectSuper())
  11333. {
  11334. Error(ERRInvalidSuper);
  11335. }
  11336. }
  11337. }
  11338. }
  11339. }
  11340. else
  11341. {
  11342. // Anything else is an error
  11343. Error(ERRInvalidSuper);
  11344. }
  11345. return superPid;
  11346. }
  11347. void Parser::AppendToList(ParseNodePtr *node, ParseNodePtr nodeToAppend)
  11348. {
  11349. Assert(nodeToAppend);
  11350. ParseNodePtr* lastPtr = node;
  11351. while ((*lastPtr) && (*lastPtr)->nop == knopList)
  11352. {
  11353. lastPtr = &(*lastPtr)->AsParseNodeBin()->pnode2;
  11354. }
  11355. auto last = (*lastPtr);
  11356. if (last)
  11357. {
  11358. *lastPtr = CreateBinNode(knopList, last, nodeToAppend, last->ichMin, nodeToAppend->ichLim);
  11359. }
  11360. else
  11361. {
  11362. *lastPtr = nodeToAppend;
  11363. }
  11364. }
  11365. ParseNodePtr Parser::ConvertArrayToArrayPattern(ParseNodePtr pnode)
  11366. {
  11367. Assert(pnode->nop == knopArray);
  11368. pnode->nop = knopArrayPattern;
  11369. ForEachItemRefInList(&pnode->AsParseNodeArrLit()->pnode1, [&](ParseNodePtr *itemRef) {
  11370. ParseNodePtr item = *itemRef;
  11371. if (item->nop == knopEllipsis)
  11372. {
  11373. itemRef = &item->AsParseNodeUni()->pnode1;
  11374. item = *itemRef;
  11375. if (!(item->nop == knopName
  11376. || item->nop == knopDot
  11377. || item->nop == knopIndex
  11378. || item->nop == knopArray
  11379. || item->nop == knopObject))
  11380. {
  11381. Error(ERRInvalidAssignmentTarget);
  11382. }
  11383. }
  11384. else if (item->nop == knopAsg)
  11385. {
  11386. itemRef = &item->AsParseNodeBin()->pnode1;
  11387. item = *itemRef;
  11388. }
  11389. if (item->nop == knopArray)
  11390. {
  11391. ConvertArrayToArrayPattern(item);
  11392. }
  11393. else if (item->nop == knopObject)
  11394. {
  11395. *itemRef = ConvertObjectToObjectPattern(item);
  11396. }
  11397. });
  11398. return pnode;
  11399. }
  11400. ParseNodeUni * Parser::ConvertObjectToObjectPattern(ParseNodePtr pnodeMemberList)
  11401. {
  11402. charcount_t ichMin = this->GetScanner()->IchMinTok();
  11403. charcount_t ichLim = this->GetScanner()->IchLimTok();
  11404. if (pnodeMemberList != nullptr && pnodeMemberList->nop == knopObject)
  11405. {
  11406. ichMin = pnodeMemberList->ichMin;
  11407. ichLim = pnodeMemberList->ichLim;
  11408. pnodeMemberList = pnodeMemberList->AsParseNodeUni()->pnode1;
  11409. }
  11410. ParseNodeObjLit * objectPatternNode = CreateObjectPatternNode(pnodeMemberList, ichMin, ichLim, true/*convertToPattern*/);
  11411. return objectPatternNode;
  11412. }
  11413. ParseNodePtr Parser::GetRightSideNodeFromPattern(ParseNodePtr pnode)
  11414. {
  11415. Assert(pnode != nullptr);
  11416. ParseNodePtr rightNode = nullptr;
  11417. OpCode op = pnode->nop;
  11418. if (op == knopObject)
  11419. {
  11420. rightNode = ConvertObjectToObjectPattern(pnode);
  11421. }
  11422. else if (op == knopArray)
  11423. {
  11424. rightNode = ConvertArrayToArrayPattern(pnode);
  11425. }
  11426. else
  11427. {
  11428. rightNode = pnode;
  11429. if (op == knopAsg)
  11430. {
  11431. TrackAssignment<true>(pnode->AsParseNodeBin()->pnode1, nullptr);
  11432. }
  11433. }
  11434. return rightNode;
  11435. }
  11436. ParseNodePtr Parser::ConvertMemberToMemberPattern(ParseNodePtr pnodeMember)
  11437. {
  11438. if (pnodeMember->nop == knopObjectPatternMember || pnodeMember->nop == knopEllipsis)
  11439. {
  11440. return pnodeMember;
  11441. }
  11442. Assert(pnodeMember->nop == knopMember || pnodeMember->nop == knopMemberShort);
  11443. ParseNodePtr rightNode = GetRightSideNodeFromPattern(pnodeMember->AsParseNodeBin()->pnode2);
  11444. ParseNodePtr resultNode = CreateBinNode(knopObjectPatternMember, pnodeMember->AsParseNodeBin()->pnode1, rightNode);
  11445. resultNode->ichMin = pnodeMember->ichMin;
  11446. resultNode->ichLim = pnodeMember->ichLim;
  11447. return resultNode;
  11448. }
  11449. ParseNodePtr Parser::ConvertToPattern(ParseNodePtr pnode)
  11450. {
  11451. if (pnode != nullptr)
  11452. {
  11453. if (pnode->nop == knopArray)
  11454. {
  11455. ConvertArrayToArrayPattern(pnode);
  11456. }
  11457. else if (pnode->nop == knopObject)
  11458. {
  11459. pnode = ConvertObjectToObjectPattern(pnode);
  11460. }
  11461. }
  11462. return pnode;
  11463. }
  11464. // This essentially be called for verifying the structure of the current tree with satisfying the destructuring grammar.
  11465. void Parser::ParseDestructuredLiteralWithScopeSave(tokens declarationType,
  11466. bool isDecl,
  11467. bool topLevel,
  11468. DestructuringInitializerContext initializerContext/* = DIC_None*/,
  11469. bool allowIn /*= true*/)
  11470. {
  11471. // We are going to parse the text again to validate the current grammar as Destructuring. Saving some scopes and
  11472. // AST related information before the validation parsing and later they will be restored.
  11473. ParseNodeFnc * pnodeFncSave = m_currentNodeFunc;
  11474. ParseNodeFnc * pnodeDeferredFncSave = m_currentNodeDeferredFunc;
  11475. if (m_currentNodeDeferredFunc == nullptr)
  11476. {
  11477. m_currentNodeDeferredFunc = m_currentNodeFunc;
  11478. }
  11479. int32 *pAstSizeSave = m_pCurrentAstSize;
  11480. uint *pNestedCountSave = m_pnestedCount;
  11481. ParseNodePtr *ppnodeScopeSave = m_ppnodeScope;
  11482. ParseNodePtr *ppnodeExprScopeSave = m_ppnodeExprScope;
  11483. ParseNodePtr newTempScope = nullptr;
  11484. m_ppnodeScope = &newTempScope;
  11485. int32 newTempAstSize = 0;
  11486. m_pCurrentAstSize = &newTempAstSize;
  11487. uint newTempNestedCount = 0;
  11488. m_pnestedCount = &newTempNestedCount;
  11489. m_ppnodeExprScope = nullptr;
  11490. charcount_t funcInArraySave = m_funcInArray;
  11491. uint funcInArrayDepthSave = m_funcInArrayDepth;
  11492. // we need to reset this as we are going to parse the grammar again.
  11493. m_hasDeferredShorthandInitError = false;
  11494. ParseDestructuredLiteral<false>(declarationType, isDecl, topLevel, initializerContext, allowIn);
  11495. m_currentNodeFunc = pnodeFncSave;
  11496. m_currentNodeDeferredFunc = pnodeDeferredFncSave;
  11497. m_pCurrentAstSize = pAstSizeSave;
  11498. m_pnestedCount = pNestedCountSave;
  11499. m_ppnodeScope = ppnodeScopeSave;
  11500. m_ppnodeExprScope = ppnodeExprScopeSave;
  11501. m_funcInArray = funcInArraySave;
  11502. m_funcInArrayDepth = funcInArrayDepthSave;
  11503. }
  11504. template <bool buildAST>
  11505. ParseNodePtr Parser::ParseDestructuredLiteral(tokens declarationType,
  11506. bool isDecl,
  11507. bool topLevel/* = true*/,
  11508. DestructuringInitializerContext initializerContext/* = DIC_None*/,
  11509. bool allowIn/* = true*/,
  11510. BOOL *forInOfOkay/* = nullptr*/,
  11511. BOOL *nativeForOkay/* = nullptr*/)
  11512. {
  11513. ParseNodeUni * pnode = nullptr;
  11514. Assert(IsPossiblePatternStart());
  11515. PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
  11516. if (m_token.tk == tkLCurly)
  11517. {
  11518. pnode = ParseDestructuredObjectLiteral<buildAST>(declarationType, isDecl, topLevel);
  11519. }
  11520. else
  11521. {
  11522. pnode = ParseDestructuredArrayLiteral<buildAST>(declarationType, isDecl, topLevel);
  11523. }
  11524. return ParseDestructuredInitializer<buildAST>(pnode, isDecl, topLevel, initializerContext, allowIn, forInOfOkay, nativeForOkay);
  11525. }
  11526. template <bool buildAST>
  11527. ParseNodePtr Parser::ParseDestructuredInitializer(ParseNodeUni * lhsNode,
  11528. bool isDecl,
  11529. bool topLevel,
  11530. DestructuringInitializerContext initializerContext,
  11531. bool allowIn,
  11532. BOOL *forInOfOkay,
  11533. BOOL *nativeForOkay)
  11534. {
  11535. this->GetScanner()->Scan();
  11536. if (topLevel && nativeForOkay == nullptr)
  11537. {
  11538. if (initializerContext != DIC_ForceErrorOnInitializer && m_token.tk != tkAsg)
  11539. {
  11540. // e.g. var {x};
  11541. Error(ERRDestructInit);
  11542. }
  11543. else if (initializerContext == DIC_ForceErrorOnInitializer && m_token.tk == tkAsg)
  11544. {
  11545. // e.g. catch([x] = [0])
  11546. Error(ERRDestructNotInit);
  11547. }
  11548. }
  11549. if (m_token.tk != tkAsg || initializerContext == DIC_ShouldNotParseInitializer)
  11550. {
  11551. if (topLevel && nativeForOkay != nullptr)
  11552. {
  11553. // Native loop should have destructuring initializer
  11554. *nativeForOkay = FALSE;
  11555. }
  11556. return lhsNode;
  11557. }
  11558. if (forInOfOkay)
  11559. {
  11560. *forInOfOkay = FALSE;
  11561. }
  11562. this->GetScanner()->Scan();
  11563. bool alreadyHasInitError = m_hasDeferredShorthandInitError;
  11564. ParseNodePtr pnodeDefault = ParseExpr<buildAST>(koplCma, nullptr, allowIn);
  11565. if (m_hasDeferredShorthandInitError && !alreadyHasInitError)
  11566. {
  11567. Error(ERRnoColon);
  11568. }
  11569. ParseNodeBin * pnodeDestructAsg = nullptr;
  11570. if (buildAST)
  11571. {
  11572. Assert(lhsNode != nullptr);
  11573. pnodeDestructAsg = CreateBinNode(knopAsg, lhsNode, pnodeDefault, lhsNode->ichMin, pnodeDefault->ichLim);
  11574. }
  11575. return pnodeDestructAsg;
  11576. }
  11577. template <bool buildAST>
  11578. ParseNodeUni * Parser::ParseDestructuredObjectLiteral(tokens declarationType, bool isDecl, bool topLevel/* = true*/)
  11579. {
  11580. Assert(m_token.tk == tkLCurly);
  11581. charcount_t ichMin = this->GetScanner()->IchMinTok();
  11582. this->GetScanner()->Scan();
  11583. if (!isDecl)
  11584. {
  11585. declarationType = tkLCurly;
  11586. }
  11587. ParseNodePtr pnodeMemberList = ParseMemberList<buildAST>(nullptr/*pNameHint*/, nullptr/*pHintLength*/, declarationType);
  11588. charcount_t ichLim = this->GetScanner()->IchLimTok();
  11589. ParseNodeObjLit * objectPatternNode = buildAST ? CreateObjectPatternNode(pnodeMemberList, ichMin, ichLim) : nullptr;
  11590. Assert(m_token.tk == tkRCurly);
  11591. return objectPatternNode;
  11592. }
  11593. template <bool buildAST>
  11594. ParseNodePtr Parser::ParseDestructuredVarDecl(tokens declarationType, bool isDecl, bool *hasSeenRest, bool topLevel/* = true*/, bool allowEmptyExpression/* = true*/, bool isObjectPattern/* =false*/)
  11595. {
  11596. ParseNodePtr pnodeElem = nullptr;
  11597. int parenCount = 0;
  11598. bool seenRest = false;
  11599. IdentToken token;
  11600. // Save the Block ID prior to the increments, so we can restore it back.
  11601. int originalCurrentBlockId = GetCurrentBlock()->blockId;
  11602. // Eat the left parentheses only when its not a declaration. This will make sure we throw syntax errors early.
  11603. if (!isDecl)
  11604. {
  11605. while (m_token.tk == tkLParen)
  11606. {
  11607. this->GetScanner()->Scan();
  11608. ++parenCount;
  11609. // Match the block increment we do upon entering parenthetical expressions
  11610. // so that the block ID's will match on reparsing of parameters.
  11611. GetCurrentBlock()->blockId = m_nextBlockId++;
  11612. }
  11613. }
  11614. if (m_token.tk == tkEllipsis)
  11615. {
  11616. // As per ES 2015 : Rest can have left-hand-side-expression when on assignment expression, but under declaration only binding identifier is allowed
  11617. // But spec is going to change for this one to allow LHS-expression both on expression and declaration - so making that happen early.
  11618. seenRest = true;
  11619. this->GetScanner()->Scan();
  11620. // Eat the left parentheses only when its not a declaration. This will make sure we throw syntax errors early.
  11621. if (!isDecl)
  11622. {
  11623. while (m_token.tk == tkLParen)
  11624. {
  11625. this->GetScanner()->Scan();
  11626. ++parenCount;
  11627. // Match the block increment we do upon entering parenthetical expressions
  11628. // so that the block ID's will match on reparsing of parameters.
  11629. GetCurrentBlock()->blockId = m_nextBlockId++;
  11630. }
  11631. }
  11632. if (m_token.tk != tkID && m_token.tk != tkTHIS && m_token.tk != tkSUPER)
  11633. {
  11634. bool nestedDestructuring = m_token.tk == tkLCurly || m_token.tk == tkLBrack;
  11635. if ((isObjectPattern && nestedDestructuring) || (!isObjectPattern && !nestedDestructuring))
  11636. {
  11637. if (isDecl)
  11638. {
  11639. Error(ERRnoIdent);
  11640. }
  11641. else
  11642. {
  11643. Error(ERRInvalidAssignmentTarget);
  11644. }
  11645. }
  11646. }
  11647. }
  11648. if (IsPossiblePatternStart())
  11649. {
  11650. // For the possible pattern start we do not allow the parens before
  11651. if (parenCount != 0)
  11652. {
  11653. Error(ERRDestructIDRef);
  11654. }
  11655. // Go recursively
  11656. pnodeElem = ParseDestructuredLiteral<buildAST>(declarationType, isDecl, false /*topLevel*/, seenRest ? DIC_ShouldNotParseInitializer : DIC_None);
  11657. if (!isDecl)
  11658. {
  11659. BOOL fCanAssign;
  11660. // Look for postfix operator
  11661. pnodeElem = ParsePostfixOperators<buildAST>(pnodeElem, TRUE, FALSE, FALSE, TRUE, &fCanAssign, &token);
  11662. }
  11663. }
  11664. else if (m_token.tk == tkSUPER || m_token.tk == tkID || m_token.tk == tkTHIS)
  11665. {
  11666. if (isDecl)
  11667. {
  11668. charcount_t ichMin = this->GetScanner()->IchMinTok();
  11669. pnodeElem = ParseVariableDeclaration<buildAST>(declarationType, ichMin
  11670. ,/* fAllowIn */false, /* pfForInOk */nullptr, /* singleDefOnly */true, /* allowInit */!seenRest, false /*topLevelParse*/);
  11671. }
  11672. else
  11673. {
  11674. BOOL fCanAssign;
  11675. // We aren't declaring anything, so scan the ID reference manually.
  11676. pnodeElem = ParseTerm<buildAST>(/* fAllowCall */ m_token.tk != tkSUPER, nullptr /*pNameHint*/, nullptr /*pHintLength*/, nullptr /*pShortNameOffset*/, &token, false,
  11677. FALSE, &fCanAssign);
  11678. // In this destructuring case we can force error here as we cannot assign.
  11679. if (!fCanAssign)
  11680. {
  11681. Error(ERRInvalidAssignmentTarget);
  11682. }
  11683. if (buildAST)
  11684. {
  11685. TrackAssignment<buildAST>(pnodeElem, nullptr);
  11686. }
  11687. if (buildAST)
  11688. {
  11689. if (IsStrictMode() && pnodeElem != nullptr && pnodeElem->nop == knopName)
  11690. {
  11691. CheckStrictModeEvalArgumentsUsage(pnodeElem->AsParseNodeName()->pid);
  11692. }
  11693. }
  11694. else
  11695. {
  11696. if (IsStrictMode() && token.tk == tkID)
  11697. {
  11698. CheckStrictModeEvalArgumentsUsage(token.pid);
  11699. }
  11700. }
  11701. }
  11702. }
  11703. else if (!((m_token.tk == tkComma || m_token.tk == tkRBrack || m_token.tk == tkRCurly) && allowEmptyExpression))
  11704. {
  11705. if (m_token.IsOperator())
  11706. {
  11707. Error(ERRDestructNoOper);
  11708. }
  11709. Error(ERRDestructIDRef);
  11710. }
  11711. // Swallow RParens before a default expression, if any.
  11712. // We eat the left parentheses only when its not a declaration. This will make sure we throw syntax errors early. We need to do the same for right parentheses.
  11713. if (!isDecl)
  11714. {
  11715. while (m_token.tk == tkRParen)
  11716. {
  11717. this->GetScanner()->Scan();
  11718. --parenCount;
  11719. }
  11720. // Restore the Block ID of the current block after the parsing of destructured variable declarations and initializers.
  11721. GetCurrentBlock()->blockId = originalCurrentBlockId;
  11722. }
  11723. if (parenCount != 0)
  11724. {
  11725. Error(ERRnoRparen);
  11726. }
  11727. if (hasSeenRest != nullptr)
  11728. {
  11729. *hasSeenRest = seenRest;
  11730. }
  11731. if (m_token.tk == tkAsg)
  11732. {
  11733. // Parse the initializer.
  11734. if (seenRest)
  11735. {
  11736. Error(ERRRestWithDefault);
  11737. }
  11738. this->GetScanner()->Scan();
  11739. bool alreadyHasInitError = m_hasDeferredShorthandInitError;
  11740. ParseNodePtr pnodeInit = ParseExpr<buildAST>(koplCma);
  11741. if (m_hasDeferredShorthandInitError && !alreadyHasInitError)
  11742. {
  11743. Error(ERRnoColon);
  11744. }
  11745. if (buildAST)
  11746. {
  11747. pnodeElem = CreateBinNode(knopAsg, pnodeElem, pnodeInit);
  11748. }
  11749. }
  11750. if (buildAST && seenRest)
  11751. {
  11752. ParseNodePtr pnodeRest = CreateUniNode(knopEllipsis, pnodeElem);
  11753. pnodeElem = pnodeRest;
  11754. }
  11755. if (!(m_token.tk == tkComma || m_token.tk == tkRBrack || m_token.tk == tkRCurly))
  11756. {
  11757. if (m_token.IsOperator())
  11758. {
  11759. Error(ERRDestructNoOper);
  11760. }
  11761. Error(ERRsyntax);
  11762. }
  11763. if (!buildAST && token.tk == tkID)
  11764. {
  11765. TrackAssignment<buildAST>(nullptr, &token);
  11766. }
  11767. return pnodeElem;
  11768. }
  11769. template <bool buildAST>
  11770. ParseNodeUni * Parser::ParseDestructuredArrayLiteral(tokens declarationType, bool isDecl, bool topLevel)
  11771. {
  11772. Assert(m_token.tk == tkLBrack);
  11773. charcount_t ichMin = this->GetScanner()->IchMinTok();
  11774. this->GetScanner()->Scan();
  11775. ParseNodeArrLit * pnodeDestructArr = nullptr;
  11776. ParseNodePtr pnodeList = nullptr;
  11777. ParseNodePtr *lastNodeRef = nullptr;
  11778. uint count = 0;
  11779. bool hasMissingValues = false;
  11780. bool seenRest = false;
  11781. if (m_token.tk != tkRBrack)
  11782. {
  11783. while (true)
  11784. {
  11785. ParseNodePtr pnodeElem = ParseDestructuredVarDecl<buildAST>(declarationType, isDecl, &seenRest, topLevel);
  11786. if (buildAST)
  11787. {
  11788. if (pnodeElem == nullptr && buildAST)
  11789. {
  11790. pnodeElem = CreateNodeForOpT<knopEmpty>();
  11791. hasMissingValues = true;
  11792. }
  11793. AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeElem);
  11794. }
  11795. count++;
  11796. if (m_token.tk == tkRBrack)
  11797. {
  11798. break;
  11799. }
  11800. if (m_token.tk != tkComma)
  11801. {
  11802. Error(ERRDestructNoOper);
  11803. }
  11804. if (seenRest) // Rest must be in the last position.
  11805. {
  11806. Error(ERRDestructRestLast);
  11807. }
  11808. this->GetScanner()->Scan();
  11809. // break if we have the trailing comma as well, eg. [a,]
  11810. if (m_token.tk == tkRBrack)
  11811. {
  11812. break;
  11813. }
  11814. }
  11815. }
  11816. if (buildAST)
  11817. {
  11818. pnodeDestructArr = CreateNodeForOpT<knopArrayPattern>(ichMin);
  11819. pnodeDestructArr->pnode1 = pnodeList;
  11820. pnodeDestructArr->arrayOfTaggedInts = false;
  11821. pnodeDestructArr->arrayOfInts = false;
  11822. pnodeDestructArr->arrayOfNumbers = false;
  11823. pnodeDestructArr->hasMissingValues = hasMissingValues;
  11824. pnodeDestructArr->count = count;
  11825. pnodeDestructArr->spreadCount = seenRest ? 1 : 0;
  11826. if (pnodeDestructArr->pnode1)
  11827. {
  11828. this->CheckArguments(pnodeDestructArr->pnode1);
  11829. }
  11830. }
  11831. return pnodeDestructArr;
  11832. }
  11833. void Parser::CaptureContext(ParseContext *parseContext) const
  11834. {
  11835. parseContext->pszSrc = this->GetScanner()->PchBase();
  11836. parseContext->length = this->m_originalLength;
  11837. parseContext->characterOffset = this->GetScanner()->IchMinTok();
  11838. parseContext->offset = parseContext->characterOffset + this->GetScanner()->m_cMultiUnits;
  11839. parseContext->grfscr = this->m_grfscr;
  11840. parseContext->lineNumber = this->GetScanner()->LineCur();
  11841. parseContext->pnodeProg = this->m_currentNodeProg;
  11842. parseContext->isUtf8 = this->GetScanner()->IsUtf8();
  11843. parseContext->strictMode = this->IsStrictMode();
  11844. parseContext->sourceContextInfo = this->m_sourceContextInfo;
  11845. parseContext->currentBlockInfo = this->m_currentBlockInfo;
  11846. parseContext->nextBlockId = this->m_nextBlockId;
  11847. }
  11848. void Parser::RestoreContext(ParseContext *const parseContext)
  11849. {
  11850. m_sourceContextInfo = parseContext->sourceContextInfo;
  11851. m_currentBlockInfo = parseContext->currentBlockInfo;
  11852. m_nextBlockId = parseContext->nextBlockId;
  11853. m_grfscr = parseContext->grfscr;
  11854. m_length = parseContext->length;
  11855. this->GetScanner()->SetText(parseContext->pszSrc, parseContext->offset, parseContext->length, parseContext->characterOffset, parseContext->isUtf8, parseContext->grfscr, parseContext->lineNumber);
  11856. m_currentNodeProg = parseContext->pnodeProg;
  11857. m_fUseStrictMode = parseContext->strictMode;
  11858. }
  11859. class ByteCodeGenerator;
  11860. #if DBG_DUMP
  11861. #define INDENT_SIZE 2
  11862. void PrintPnodeListWIndent(ParseNode *pnode, int indentAmt);
  11863. void PrintFormalsWIndent(ParseNode *pnode, int indentAmt);
  11864. void Indent(int indentAmt) {
  11865. for (int i = 0; i < indentAmt; i++) {
  11866. Output::Print(_u(" "));
  11867. }
  11868. }
  11869. void PrintBlockType(PnodeBlockType type)
  11870. {
  11871. switch (type)
  11872. {
  11873. case Global:
  11874. Output::Print(_u("(Global)"));
  11875. break;
  11876. case Function:
  11877. Output::Print(_u("(Function)"));
  11878. break;
  11879. case Regular:
  11880. Output::Print(_u("(Regular)"));
  11881. break;
  11882. case Parameter:
  11883. Output::Print(_u("(Parameter)"));
  11884. break;
  11885. default:
  11886. Output::Print(_u("(unknown blocktype)"));
  11887. break;
  11888. }
  11889. }
  11890. void PrintScopesWIndent(ParseNode *pnode, int indentAmt) {
  11891. ParseNode *scope = nullptr;
  11892. bool firstOnly = false;
  11893. switch (pnode->nop)
  11894. {
  11895. case knopProg:
  11896. case knopFncDecl: scope = pnode->AsParseNodeFnc()->pnodeScopes; break;
  11897. case knopBlock: scope = pnode->AsParseNodeBlock()->pnodeScopes; break;
  11898. case knopCatch: scope = pnode->AsParseNodeCatch()->pnodeScopes; break;
  11899. case knopWith: scope = pnode->AsParseNodeWith()->pnodeScopes; break;
  11900. case knopSwitch: scope = pnode->AsParseNodeSwitch()->pnodeBlock; firstOnly = true; break;
  11901. case knopFor: scope = pnode->AsParseNodeFor()->pnodeBlock; firstOnly = true; break;
  11902. case knopForIn: scope = pnode->AsParseNodeForInOrForOf()->pnodeBlock; firstOnly = true; break;
  11903. case knopForOf: scope = pnode->AsParseNodeForInOrForOf()->pnodeBlock; firstOnly = true; break;
  11904. case knopForAwaitOf: scope = pnode->AsParseNodeForInOrForOf()->pnodeBlock; firstOnly = true; break;
  11905. }
  11906. if (scope) {
  11907. Output::Print(_u("[%4d, %4d): "), scope->ichMin, scope->ichLim);
  11908. Indent(indentAmt);
  11909. Output::Print(_u("Scopes: "));
  11910. ParseNode *next = nullptr;
  11911. ParseNode *syntheticBlock = nullptr;
  11912. while (scope) {
  11913. switch (scope->nop) {
  11914. case knopFncDecl: Output::Print(_u("knopFncDecl")); next = scope->AsParseNodeFnc()->pnodeNext; break;
  11915. case knopBlock: Output::Print(_u("knopBlock")); PrintBlockType(scope->AsParseNodeBlock()->blockType); next = scope->AsParseNodeBlock()->pnodeNext; break;
  11916. case knopCatch: Output::Print(_u("knopCatch")); next = scope->AsParseNodeCatch()->pnodeNext; break;
  11917. case knopWith: Output::Print(_u("knopWith")); next = scope->AsParseNodeWith()->pnodeNext; break;
  11918. default: Output::Print(_u("unknown")); break;
  11919. }
  11920. if (firstOnly) {
  11921. next = nullptr;
  11922. syntheticBlock = scope;
  11923. }
  11924. if (scope->grfpn & fpnSyntheticNode) {
  11925. Output::Print(_u(" synthetic"));
  11926. if (scope->nop == knopBlock)
  11927. syntheticBlock = scope;
  11928. }
  11929. Output::Print(_u(" (%d-%d)"), scope->ichMin, scope->ichLim);
  11930. if (next) Output::Print(_u(", "));
  11931. scope = next;
  11932. }
  11933. Output::Print(_u("\n"));
  11934. if (syntheticBlock || firstOnly) {
  11935. PrintScopesWIndent(syntheticBlock, indentAmt + INDENT_SIZE);
  11936. }
  11937. }
  11938. }
  11939. void PrintPnodeWIndent(ParseNode *pnode, int indentAmt) {
  11940. if (pnode == NULL)
  11941. return;
  11942. Output::Print(_u("[%4d, %4d): "), pnode->ichMin, pnode->ichLim);
  11943. switch (pnode->nop) {
  11944. //PTNODE(knopName , "name" ,None ,Pid ,fnopLeaf)
  11945. case knopName:
  11946. Indent(indentAmt);
  11947. if (pnode->AsParseNodeName()->pid != NULL) {
  11948. Output::Print(_u("id: %s\n"), pnode->AsParseNodeName()->pid->Psz());
  11949. }
  11950. else {
  11951. Output::Print(_u("name node\n"));
  11952. }
  11953. break;
  11954. //PTNODE(knopInt , "int const" ,None ,Int ,fnopLeaf|fnopConst)
  11955. case knopInt:
  11956. Indent(indentAmt);
  11957. Output::Print(_u("%d\n"), pnode->AsParseNodeInt()->lw);
  11958. break;
  11959. //PTNODE(knopInt , "int const" ,None ,Int ,fnopLeaf|fnopConst)
  11960. case knopBigInt:
  11961. Indent(indentAmt);
  11962. Output::Print(_u("%s%s\n"), pnode->AsParseNodeBigInt()->isNegative? "-" : "", pnode->AsParseNodeBigInt()->pid->Psz());
  11963. break;
  11964. //PTNODE(knopFlt , "flt const" ,None ,Flt ,fnopLeaf|fnopConst)
  11965. case knopFlt:
  11966. Indent(indentAmt);
  11967. Output::Print(_u("%lf\n"), pnode->AsParseNodeFloat()->dbl);
  11968. break;
  11969. //PTNODE(knopStr , "str const" ,None ,Pid ,fnopLeaf|fnopConst)
  11970. case knopStr:
  11971. Indent(indentAmt);
  11972. Output::Print(_u("\"%s\"\n"), pnode->AsParseNodeStr()->pid->Psz());
  11973. break;
  11974. //PTNODE(knopRegExp , "reg expr" ,None ,Pid ,fnopLeaf|fnopConst)
  11975. case knopRegExp:
  11976. Indent(indentAmt);
  11977. Output::Print(_u("/%x/\n"), pnode->AsParseNodeRegExp()->regexPattern);
  11978. break;
  11979. //PTNODE(knopNull , "null" ,Null ,None ,fnopLeaf)
  11980. case knopNull:
  11981. Indent(indentAmt);
  11982. Output::Print(_u("null\n"));
  11983. break;
  11984. //PTNODE(knopFalse , "false" ,False ,None ,fnopLeaf)
  11985. case knopFalse:
  11986. Indent(indentAmt);
  11987. Output::Print(_u("false\n"));
  11988. break;
  11989. //PTNODE(knopTrue , "true" ,True ,None ,fnopLeaf)
  11990. case knopTrue:
  11991. Indent(indentAmt);
  11992. Output::Print(_u("true\n"));
  11993. break;
  11994. //PTNODE(knopEmpty , "empty" ,Empty ,None ,fnopLeaf)
  11995. case knopEmpty:
  11996. Indent(indentAmt);
  11997. Output::Print(_u("empty\n"));
  11998. break;
  11999. // Unary operators.
  12000. //PTNODE(knopNot , "~" ,BitNot ,Uni ,fnopUni)
  12001. case knopNot:
  12002. Indent(indentAmt);
  12003. Output::Print(_u("~\n"));
  12004. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12005. break;
  12006. //PTNODE(knopNeg , "unary -" ,Neg ,Uni ,fnopUni)
  12007. case knopNeg:
  12008. Indent(indentAmt);
  12009. Output::Print(_u("U-\n"));
  12010. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12011. break;
  12012. //PTNODE(knopPos , "unary +" ,Pos ,Uni ,fnopUni)
  12013. case knopPos:
  12014. Indent(indentAmt);
  12015. Output::Print(_u("U+\n"));
  12016. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12017. break;
  12018. //PTNODE(knopLogNot , "!" ,LogNot ,Uni ,fnopUni)
  12019. case knopLogNot:
  12020. Indent(indentAmt);
  12021. Output::Print(_u("!\n"));
  12022. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12023. break;
  12024. //PTNODE(knopEllipsis , "..." ,Spread ,Uni , fnopUni)
  12025. case knopEllipsis:
  12026. Indent(indentAmt);
  12027. Output::Print(_u("...<expr>\n"));
  12028. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12029. break;
  12030. //PTNODE(knopIncPost , "++ post" ,Inc ,Uni ,fnopUni|fnopAsg)
  12031. case knopIncPost:
  12032. Indent(indentAmt);
  12033. Output::Print(_u("<expr>++\n"));
  12034. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12035. break;
  12036. //PTNODE(knopDecPost , "-- post" ,Dec ,Uni ,fnopUni|fnopAsg)
  12037. case knopDecPost:
  12038. Indent(indentAmt);
  12039. Output::Print(_u("<expr>--\n"));
  12040. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12041. break;
  12042. //PTNODE(knopIncPre , "++ pre" ,Inc ,Uni ,fnopUni|fnopAsg)
  12043. case knopIncPre:
  12044. Indent(indentAmt);
  12045. Output::Print(_u("++<expr>\n"));
  12046. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12047. break;
  12048. //PTNODE(knopDecPre , "-- pre" ,Dec ,Uni ,fnopUni|fnopAsg)
  12049. case knopDecPre:
  12050. Indent(indentAmt);
  12051. Output::Print(_u("--<expr>\n"));
  12052. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12053. break;
  12054. //PTNODE(knopTypeof , "typeof" ,None ,Uni ,fnopUni)
  12055. case knopTypeof:
  12056. Indent(indentAmt);
  12057. Output::Print(_u("typeof\n"));
  12058. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12059. break;
  12060. //PTNODE(knopVoid , "void" ,Void ,Uni ,fnopUni)
  12061. case knopVoid:
  12062. Indent(indentAmt);
  12063. Output::Print(_u("void\n"));
  12064. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12065. break;
  12066. //PTNODE(knopDelete , "delete" ,None ,Uni ,fnopUni)
  12067. case knopDelete:
  12068. Indent(indentAmt);
  12069. Output::Print(_u("delete\n"));
  12070. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12071. break;
  12072. //PTNODE(knopArray , "arr cnst" ,None ,Uni ,fnopUni)
  12073. case knopArrayPattern:
  12074. Indent(indentAmt);
  12075. Output::Print(_u("Array Pattern\n"));
  12076. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12077. break;
  12078. case knopObjectPattern:
  12079. Indent(indentAmt);
  12080. Output::Print(_u("Object Pattern\n"));
  12081. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12082. break;
  12083. case knopArray:
  12084. Indent(indentAmt);
  12085. Output::Print(_u("Array Literal\n"));
  12086. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12087. break;
  12088. //PTNODE(knopObject , "obj cnst" ,None ,Uni ,fnopUni)
  12089. case knopObject:
  12090. Indent(indentAmt);
  12091. Output::Print(_u("Object Literal\n"));
  12092. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12093. break;
  12094. // Binary and Ternary Operators
  12095. //PTNODE(knopAdd , "+" ,Add ,Bin ,fnopBin)
  12096. case knopAdd:
  12097. Indent(indentAmt);
  12098. Output::Print(_u("+\n"));
  12099. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12100. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12101. break;
  12102. //PTNODE(knopSub , "-" ,Sub ,Bin ,fnopBin)
  12103. case knopSub:
  12104. Indent(indentAmt);
  12105. Output::Print(_u("-\n"));
  12106. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12107. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12108. break;
  12109. //PTNODE(knopMul , "*" ,Mul ,Bin ,fnopBin)
  12110. case knopMul:
  12111. Indent(indentAmt);
  12112. Output::Print(_u("*\n"));
  12113. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12114. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12115. break;
  12116. //PTNODE(knopDiv , "/" ,Div ,Bin ,fnopBin)
  12117. case knopExpo:
  12118. Indent(indentAmt);
  12119. Output::Print(_u("**\n"));
  12120. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12121. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12122. break;
  12123. //PTNODE(knopExpo , "**" ,Expo ,Bin ,fnopBin)
  12124. case knopDiv:
  12125. Indent(indentAmt);
  12126. Output::Print(_u("/\n"));
  12127. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12128. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12129. break;
  12130. //PTNODE(knopMod , "%" ,Mod ,Bin ,fnopBin)
  12131. case knopMod:
  12132. Indent(indentAmt);
  12133. Output::Print(_u("%%\n"));
  12134. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12135. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12136. break;
  12137. //PTNODE(knopOr , "|" ,BitOr ,Bin ,fnopBin)
  12138. case knopOr:
  12139. Indent(indentAmt);
  12140. Output::Print(_u("|\n"));
  12141. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12142. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12143. break;
  12144. //PTNODE(knopXor , "^" ,BitXor ,Bin ,fnopBin)
  12145. case knopXor:
  12146. Indent(indentAmt);
  12147. Output::Print(_u("^\n"));
  12148. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12149. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12150. break;
  12151. //PTNODE(knopAnd , "&" ,BitAnd ,Bin ,fnopBin)
  12152. case knopAnd:
  12153. Indent(indentAmt);
  12154. Output::Print(_u("&\n"));
  12155. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12156. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12157. break;
  12158. //PTNODE(knopEq , "==" ,EQ ,Bin ,fnopBin|fnopRel)
  12159. case knopEq:
  12160. Indent(indentAmt);
  12161. Output::Print(_u("==\n"));
  12162. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12163. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12164. break;
  12165. //PTNODE(knopNe , "!=" ,NE ,Bin ,fnopBin|fnopRel)
  12166. case knopNe:
  12167. Indent(indentAmt);
  12168. Output::Print(_u("!=\n"));
  12169. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12170. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12171. break;
  12172. //PTNODE(knopLt , "<" ,LT ,Bin ,fnopBin|fnopRel)
  12173. case knopLt:
  12174. Indent(indentAmt);
  12175. Output::Print(_u("<\n"));
  12176. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12177. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12178. break;
  12179. //PTNODE(knopLe , "<=" ,LE ,Bin ,fnopBin|fnopRel)
  12180. case knopLe:
  12181. Indent(indentAmt);
  12182. Output::Print(_u("<=\n"));
  12183. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12184. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12185. break;
  12186. //PTNODE(knopGe , ">=" ,GE ,Bin ,fnopBin|fnopRel)
  12187. case knopGe:
  12188. Indent(indentAmt);
  12189. Output::Print(_u(">=\n"));
  12190. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12191. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12192. break;
  12193. //PTNODE(knopGt , ">" ,GT ,Bin ,fnopBin|fnopRel)
  12194. case knopGt:
  12195. Indent(indentAmt);
  12196. Output::Print(_u(">\n"));
  12197. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12198. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12199. break;
  12200. //PTNODE(knopCall , "()" ,None ,Bin ,fnopBin)
  12201. case knopCall:
  12202. Indent(indentAmt);
  12203. Output::Print(_u("Call\n"));
  12204. PrintPnodeWIndent(pnode->AsParseNodeCall()->pnodeTarget, indentAmt + INDENT_SIZE);
  12205. PrintPnodeListWIndent(pnode->AsParseNodeCall()->pnodeArgs, indentAmt + INDENT_SIZE);
  12206. break;
  12207. //PTNODE(knopDot , "." ,None ,Bin ,fnopBin)
  12208. case knopDot:
  12209. Indent(indentAmt);
  12210. Output::Print(_u(".\n"));
  12211. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12212. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12213. break;
  12214. //PTNODE(knopAsg , "=" ,None ,Bin ,fnopBin|fnopAsg)
  12215. case knopAsg:
  12216. Indent(indentAmt);
  12217. Output::Print(_u("=\n"));
  12218. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12219. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12220. break;
  12221. //PTNODE(knopInstOf , "instanceof",InstOf ,Bin ,fnopBin|fnopRel)
  12222. case knopInstOf:
  12223. Indent(indentAmt);
  12224. Output::Print(_u("instanceof\n"));
  12225. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12226. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12227. break;
  12228. //PTNODE(knopIn , "in" ,In ,Bin ,fnopBin|fnopRel)
  12229. case knopIn:
  12230. Indent(indentAmt);
  12231. Output::Print(_u("in\n"));
  12232. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12233. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12234. break;
  12235. //PTNODE(knopEqv , "===" ,Eqv ,Bin ,fnopBin|fnopRel)
  12236. case knopEqv:
  12237. Indent(indentAmt);
  12238. Output::Print(_u("===\n"));
  12239. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12240. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12241. break;
  12242. //PTNODE(knopNEqv , "!==" ,NEqv ,Bin ,fnopBin|fnopRel)
  12243. case knopNEqv:
  12244. Indent(indentAmt);
  12245. Output::Print(_u("!==\n"));
  12246. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12247. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12248. break;
  12249. //PTNODE(knopComma , "," ,None ,Bin ,fnopBin)
  12250. case knopComma:
  12251. Indent(indentAmt);
  12252. Output::Print(_u(",\n"));
  12253. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12254. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12255. break;
  12256. //PTNODE(knopLogOr , "||" ,None ,Bin ,fnopBin)
  12257. case knopLogOr:
  12258. Indent(indentAmt);
  12259. Output::Print(_u("||\n"));
  12260. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12261. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12262. break;
  12263. //PTNODE(knopLogAnd , "&&" ,None ,Bin ,fnopBin)
  12264. case knopLogAnd:
  12265. Indent(indentAmt);
  12266. Output::Print(_u("&&\n"));
  12267. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12268. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12269. break;
  12270. case knopCoalesce:
  12271. Indent(indentAmt);
  12272. Output::Print(_u("??\n"));
  12273. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12274. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12275. break;
  12276. //PTNODE(knopLsh , "<<" ,Lsh ,Bin ,fnopBin)
  12277. case knopLsh:
  12278. Indent(indentAmt);
  12279. Output::Print(_u("<<\n"));
  12280. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12281. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12282. break;
  12283. //PTNODE(knopRsh , ">>" ,Rsh ,Bin ,fnopBin)
  12284. case knopRsh:
  12285. Indent(indentAmt);
  12286. Output::Print(_u(">>\n"));
  12287. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12288. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12289. break;
  12290. //PTNODE(knopRs2 , ">>>" ,Rs2 ,Bin ,fnopBin)
  12291. case knopRs2:
  12292. Indent(indentAmt);
  12293. Output::Print(_u(">>>\n"));
  12294. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12295. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12296. break;
  12297. //PTNODE(knopNew , "new" ,None ,Bin ,fnopBin)
  12298. case knopNew:
  12299. Indent(indentAmt);
  12300. Output::Print(_u("new\n"));
  12301. PrintPnodeWIndent(pnode->AsParseNodeCall()->pnodeTarget, indentAmt + INDENT_SIZE);
  12302. PrintPnodeListWIndent(pnode->AsParseNodeCall()->pnodeArgs, indentAmt + INDENT_SIZE);
  12303. break;
  12304. //PTNODE(knopIndex , "[]" ,None ,Bin ,fnopBin)
  12305. case knopIndex:
  12306. Indent(indentAmt);
  12307. Output::Print(_u("[]\n"));
  12308. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12309. PrintPnodeListWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12310. break;
  12311. //PTNODE(knopQmark , "?" ,None ,Tri ,fnopBin)
  12312. case knopQmark:
  12313. Indent(indentAmt);
  12314. Output::Print(_u("?:\n"));
  12315. PrintPnodeWIndent(pnode->AsParseNodeTri()->pnode1, indentAmt + INDENT_SIZE);
  12316. PrintPnodeWIndent(pnode->AsParseNodeTri()->pnode2, indentAmt + INDENT_SIZE);
  12317. PrintPnodeWIndent(pnode->AsParseNodeTri()->pnode3, indentAmt + INDENT_SIZE);
  12318. break;
  12319. //PTNODE(knopAsgAdd , "+=" ,Add ,Bin ,fnopBin|fnopAsg)
  12320. case knopAsgAdd:
  12321. Indent(indentAmt);
  12322. Output::Print(_u("+=\n"));
  12323. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12324. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12325. break;
  12326. //PTNODE(knopAsgSub , "-=" ,Sub ,Bin ,fnopBin|fnopAsg)
  12327. case knopAsgSub:
  12328. Indent(indentAmt);
  12329. Output::Print(_u("-=\n"));
  12330. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12331. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12332. break;
  12333. //PTNODE(knopAsgMul , "*=" ,Mul ,Bin ,fnopBin|fnopAsg)
  12334. case knopAsgMul:
  12335. Indent(indentAmt);
  12336. Output::Print(_u("*=\n"));
  12337. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12338. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12339. break;
  12340. //PTNODE(knopAsgDiv , "/=" ,Div ,Bin ,fnopBin|fnopAsg)
  12341. case knopAsgExpo:
  12342. Indent(indentAmt);
  12343. Output::Print(_u("**=\n"));
  12344. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12345. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12346. break;
  12347. //PTNODE(knopAsgExpo , "**=" ,Expo ,Bin ,fnopBin|fnopAsg)
  12348. case knopAsgDiv:
  12349. Indent(indentAmt);
  12350. Output::Print(_u("/=\n"));
  12351. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12352. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12353. break;
  12354. //PTNODE(knopAsgMod , "%=" ,Mod ,Bin ,fnopBin|fnopAsg)
  12355. case knopAsgMod:
  12356. Indent(indentAmt);
  12357. Output::Print(_u("%=\n"));
  12358. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12359. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12360. break;
  12361. //PTNODE(knopAsgAnd , "&=" ,BitAnd ,Bin ,fnopBin|fnopAsg)
  12362. case knopAsgAnd:
  12363. Indent(indentAmt);
  12364. Output::Print(_u("&=\n"));
  12365. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12366. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12367. break;
  12368. //PTNODE(knopAsgXor , "^=" ,BitXor ,Bin ,fnopBin|fnopAsg)
  12369. case knopAsgXor:
  12370. Indent(indentAmt);
  12371. Output::Print(_u("^=\n"));
  12372. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12373. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12374. break;
  12375. //PTNODE(knopAsgOr , "|=" ,BitOr ,Bin ,fnopBin|fnopAsg)
  12376. case knopAsgOr:
  12377. Indent(indentAmt);
  12378. Output::Print(_u("|=\n"));
  12379. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12380. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12381. break;
  12382. //PTNODE(knopAsgLsh , "<<=" ,Lsh ,Bin ,fnopBin|fnopAsg)
  12383. case knopAsgLsh:
  12384. Indent(indentAmt);
  12385. Output::Print(_u("<<=\n"));
  12386. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12387. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12388. break;
  12389. //PTNODE(knopAsgRsh , ">>=" ,Rsh ,Bin ,fnopBin|fnopAsg)
  12390. case knopAsgRsh:
  12391. Indent(indentAmt);
  12392. Output::Print(_u(">>=\n"));
  12393. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12394. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12395. break;
  12396. //PTNODE(knopAsgRs2 , ">>>=" ,Rs2 ,Bin ,fnopBin|fnopAsg)
  12397. case knopAsgRs2:
  12398. Indent(indentAmt);
  12399. Output::Print(_u(">>>=\n"));
  12400. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12401. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12402. break;
  12403. case knopComputedName:
  12404. Indent(indentAmt);
  12405. Output::Print(_u("ComputedProperty\n"));
  12406. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12407. break;
  12408. case knopParamPattern:
  12409. PrintPnodeWIndent(pnode->AsParseNodeParamPattern()->pnode1, indentAmt);
  12410. break;
  12411. //PTNODE(knopMember , ":" ,None ,Bin ,fnopBin)
  12412. case knopMember:
  12413. case knopMemberShort:
  12414. case knopObjectPatternMember:
  12415. Indent(indentAmt);
  12416. Output::Print(_u(":\n"));
  12417. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt + INDENT_SIZE);
  12418. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode2, indentAmt + INDENT_SIZE);
  12419. break;
  12420. // General nodes.
  12421. //PTNODE(knopList , "<list>" ,None ,Bin ,fnopNone)
  12422. case knopList:
  12423. Indent(indentAmt);
  12424. Output::Print(_u("List\n"));
  12425. PrintPnodeListWIndent(pnode, indentAmt + INDENT_SIZE);
  12426. break;
  12427. //PTNODE(knopVarDecl , "varDcl" ,None ,Var ,fnopNone)
  12428. case knopVarDecl:
  12429. Indent(indentAmt);
  12430. Output::Print(_u("var %s\n"), pnode->AsParseNodeVar()->pid->Psz());
  12431. if (pnode->AsParseNodeVar()->pnodeInit != NULL)
  12432. PrintPnodeWIndent(pnode->AsParseNodeVar()->pnodeInit, indentAmt + INDENT_SIZE);
  12433. break;
  12434. case knopConstDecl:
  12435. Indent(indentAmt);
  12436. Output::Print(_u("const %s\n"), pnode->AsParseNodeVar()->pid->Psz());
  12437. if (pnode->AsParseNodeVar()->pnodeInit != NULL)
  12438. PrintPnodeWIndent(pnode->AsParseNodeVar()->pnodeInit, indentAmt + INDENT_SIZE);
  12439. break;
  12440. case knopLetDecl:
  12441. Indent(indentAmt);
  12442. Output::Print(_u("let %s\n"), pnode->AsParseNodeVar()->pid->Psz());
  12443. if (pnode->AsParseNodeVar()->pnodeInit != NULL)
  12444. PrintPnodeWIndent(pnode->AsParseNodeVar()->pnodeInit, indentAmt + INDENT_SIZE);
  12445. break;
  12446. //PTNODE(knopFncDecl , "fncDcl" ,None ,Fnc ,fnopLeaf)
  12447. case knopFncDecl:
  12448. Indent(indentAmt);
  12449. if (pnode->AsParseNodeFnc()->pid != NULL)
  12450. {
  12451. Output::Print(_u("fn decl %d nested %d name %s (%d-%d)\n"), pnode->AsParseNodeFnc()->IsDeclaration(), pnode->AsParseNodeFnc()->IsNested(),
  12452. pnode->AsParseNodeFnc()->pid->Psz(), pnode->ichMin, pnode->ichLim);
  12453. }
  12454. else
  12455. {
  12456. Output::Print(_u("fn decl %d nested %d anonymous (%d-%d)\n"), pnode->AsParseNodeFnc()->IsDeclaration(), pnode->AsParseNodeFnc()->IsNested(), pnode->ichMin, pnode->ichLim);
  12457. }
  12458. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12459. PrintFormalsWIndent(pnode->AsParseNodeFnc()->pnodeParams, indentAmt + INDENT_SIZE);
  12460. PrintPnodeWIndent(pnode->AsParseNodeFnc()->pnodeRest, indentAmt + INDENT_SIZE);
  12461. PrintPnodeWIndent(pnode->AsParseNodeFnc()->pnodeBody, indentAmt + INDENT_SIZE);
  12462. if (pnode->AsParseNodeFnc()->pnodeBody == nullptr)
  12463. {
  12464. Output::Print(_u("[%4d, %4d): "), pnode->ichMin, pnode->ichLim);
  12465. Indent(indentAmt + INDENT_SIZE);
  12466. Output::Print(_u("<parse deferred body>\n"));
  12467. }
  12468. break;
  12469. //PTNODE(knopProg , "program" ,None ,Fnc ,fnopNone)
  12470. case knopProg:
  12471. Indent(indentAmt);
  12472. Output::Print(_u("program\n"));
  12473. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12474. PrintPnodeListWIndent(pnode->AsParseNodeFnc()->pnodeBody, indentAmt + INDENT_SIZE);
  12475. break;
  12476. //PTNODE(knopEndCode , "<endcode>" ,None ,None ,fnopNone)
  12477. case knopEndCode:
  12478. Indent(indentAmt);
  12479. Output::Print(_u("<endcode>\n"));
  12480. break;
  12481. //PTNODE(knopDebugger , "debugger" ,None ,None ,fnopNone)
  12482. case knopDebugger:
  12483. Indent(indentAmt);
  12484. Output::Print(_u("<debugger>\n"));
  12485. break;
  12486. //PTNODE(knopFor , "for" ,None ,For ,fnopBreak|fnopContinue)
  12487. case knopFor:
  12488. Indent(indentAmt);
  12489. Output::Print(_u("for\n"));
  12490. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12491. PrintPnodeWIndent(pnode->AsParseNodeFor()->pnodeInit, indentAmt + INDENT_SIZE);
  12492. PrintPnodeWIndent(pnode->AsParseNodeFor()->pnodeCond, indentAmt + INDENT_SIZE);
  12493. PrintPnodeWIndent(pnode->AsParseNodeFor()->pnodeIncr, indentAmt + INDENT_SIZE);
  12494. PrintPnodeWIndent(pnode->AsParseNodeFor()->pnodeBody, indentAmt + INDENT_SIZE);
  12495. break;
  12496. //PTNODE(knopIf , "if" ,None ,If ,fnopNone)
  12497. case knopIf:
  12498. Indent(indentAmt);
  12499. Output::Print(_u("if\n"));
  12500. PrintPnodeWIndent(pnode->AsParseNodeIf()->pnodeCond, indentAmt + INDENT_SIZE);
  12501. PrintPnodeWIndent(pnode->AsParseNodeIf()->pnodeTrue, indentAmt + INDENT_SIZE);
  12502. if (pnode->AsParseNodeIf()->pnodeFalse != NULL)
  12503. PrintPnodeWIndent(pnode->AsParseNodeIf()->pnodeFalse, indentAmt + INDENT_SIZE);
  12504. break;
  12505. //PTNODE(knopWhile , "while" ,None ,While,fnopBreak|fnopContinue)
  12506. case knopWhile:
  12507. Indent(indentAmt);
  12508. Output::Print(_u("while\n"));
  12509. PrintPnodeWIndent(pnode->AsParseNodeWhile()->pnodeCond, indentAmt + INDENT_SIZE);
  12510. PrintPnodeWIndent(pnode->AsParseNodeWhile()->pnodeBody, indentAmt + INDENT_SIZE);
  12511. break;
  12512. //PTNODE(knopDoWhile , "do-while" ,None ,While,fnopBreak|fnopContinue)
  12513. case knopDoWhile:
  12514. Indent(indentAmt);
  12515. Output::Print(_u("do\n"));
  12516. PrintPnodeWIndent(pnode->AsParseNodeWhile()->pnodeCond, indentAmt + INDENT_SIZE);
  12517. PrintPnodeWIndent(pnode->AsParseNodeWhile()->pnodeBody, indentAmt + INDENT_SIZE);
  12518. break;
  12519. //PTNODE(knopForIn , "for in" ,None ,ForIn,fnopBreak|fnopContinue|fnopCleanup)
  12520. case knopForIn:
  12521. Indent(indentAmt);
  12522. Output::Print(_u("forIn\n"));
  12523. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12524. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeLval, indentAmt + INDENT_SIZE);
  12525. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeObj, indentAmt + INDENT_SIZE);
  12526. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeBody, indentAmt + INDENT_SIZE);
  12527. break;
  12528. case knopForOf:
  12529. Indent(indentAmt);
  12530. Output::Print(_u("forOf\n"));
  12531. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12532. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeLval, indentAmt + INDENT_SIZE);
  12533. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeObj, indentAmt + INDENT_SIZE);
  12534. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeBody, indentAmt + INDENT_SIZE);
  12535. break;
  12536. case knopForAwaitOf:
  12537. Indent(indentAmt);
  12538. Output::Print(_u("forAwaitOf\n"));
  12539. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12540. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeLval, indentAmt + INDENT_SIZE);
  12541. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeObj, indentAmt + INDENT_SIZE);
  12542. PrintPnodeWIndent(pnode->AsParseNodeForInOrForOf()->pnodeBody, indentAmt + INDENT_SIZE);
  12543. break;
  12544. //PTNODE(knopReturn , "return" ,None ,Uni ,fnopNone)
  12545. case knopReturn:
  12546. Indent(indentAmt);
  12547. Output::Print(_u("return\n"));
  12548. if (pnode->AsParseNodeReturn()->pnodeExpr != NULL)
  12549. PrintPnodeWIndent(pnode->AsParseNodeReturn()->pnodeExpr, indentAmt + INDENT_SIZE);
  12550. break;
  12551. //PTNODE(knopBlock , "{}" ,None ,Block,fnopNone)
  12552. case knopBlock:
  12553. Indent(indentAmt);
  12554. Output::Print(_u("block "));
  12555. if (pnode->grfpn & fpnSyntheticNode)
  12556. Output::Print(_u("synthetic "));
  12557. PrintBlockType(pnode->AsParseNodeBlock()->blockType);
  12558. Output::Print(_u("(%d-%d)\n"), pnode->ichMin, pnode->ichLim);
  12559. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12560. if (pnode->AsParseNodeBlock()->pnodeStmt != NULL)
  12561. PrintPnodeWIndent(pnode->AsParseNodeBlock()->pnodeStmt, indentAmt + INDENT_SIZE);
  12562. break;
  12563. //PTNODE(knopWith , "with" ,None ,With ,fnopCleanup)
  12564. case knopWith:
  12565. Indent(indentAmt);
  12566. Output::Print(_u("with (%d-%d)\n"), pnode->ichMin, pnode->ichLim);
  12567. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12568. PrintPnodeWIndent(pnode->AsParseNodeWith()->pnodeObj, indentAmt + INDENT_SIZE);
  12569. PrintPnodeWIndent(pnode->AsParseNodeWith()->pnodeBody, indentAmt + INDENT_SIZE);
  12570. break;
  12571. //PTNODE(knopBreak , "break" ,None ,Jump ,fnopNone)
  12572. case knopBreak:
  12573. Indent(indentAmt);
  12574. Output::Print(_u("break\n"));
  12575. // TODO: some representation of target
  12576. break;
  12577. //PTNODE(knopContinue , "continue" ,None ,Jump ,fnopNone)
  12578. case knopContinue:
  12579. Indent(indentAmt);
  12580. Output::Print(_u("continue\n"));
  12581. // TODO: some representation of target
  12582. break;
  12583. //PTNODE(knopSwitch , "switch" ,None ,Switch,fnopBreak)
  12584. case knopSwitch:
  12585. Indent(indentAmt);
  12586. Output::Print(_u("switch\n"));
  12587. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12588. for (ParseNodeCase *pnodeT = pnode->AsParseNodeSwitch()->pnodeCases; NULL != pnodeT; pnodeT = pnodeT->pnodeNext) {
  12589. PrintPnodeWIndent(pnodeT, indentAmt + 2);
  12590. }
  12591. break;
  12592. //PTNODE(knopCase , "case" ,None ,Case ,fnopNone)
  12593. case knopCase:
  12594. Indent(indentAmt);
  12595. Output::Print(_u("case\n"));
  12596. PrintPnodeWIndent(pnode->AsParseNodeCase()->pnodeExpr, indentAmt + INDENT_SIZE);
  12597. PrintPnodeWIndent(pnode->AsParseNodeCase()->pnodeBody, indentAmt + INDENT_SIZE);
  12598. break;
  12599. //PTNODE(knopTryFinally,"try-finally",None,TryFinally,fnopCleanup)
  12600. case knopTryFinally:
  12601. PrintPnodeWIndent(pnode->AsParseNodeTryFinally()->pnodeTry, indentAmt);
  12602. PrintPnodeWIndent(pnode->AsParseNodeTryFinally()->pnodeFinally, indentAmt);
  12603. break;
  12604. case knopFinally:
  12605. Indent(indentAmt);
  12606. Output::Print(_u("finally\n"));
  12607. PrintPnodeWIndent(pnode->AsParseNodeFinally()->pnodeBody, indentAmt + INDENT_SIZE);
  12608. break;
  12609. //PTNODE(knopCatch , "catch" ,None ,Catch,fnopNone)
  12610. case knopCatch:
  12611. Indent(indentAmt);
  12612. Output::Print(_u("catch (%d-%d)\n"), pnode->ichMin, pnode->ichLim);
  12613. PrintScopesWIndent(pnode, indentAmt + INDENT_SIZE);
  12614. PrintPnodeWIndent(pnode->AsParseNodeCatch()->GetParam(), indentAmt + INDENT_SIZE);
  12615. // if (pnode->AsParseNodeCatch()->pnodeGuard!=NULL)
  12616. // PrintPnodeWIndent(pnode->AsParseNodeCatch()->pnodeGuard,indentAmt+INDENT_SIZE);
  12617. PrintPnodeWIndent(pnode->AsParseNodeCatch()->pnodeBody, indentAmt + INDENT_SIZE);
  12618. break;
  12619. //PTNODE(knopTryCatch , "try-catch" ,None ,TryCatch ,fnopCleanup)
  12620. case knopTryCatch:
  12621. PrintPnodeWIndent(pnode->AsParseNodeTryCatch()->pnodeTry, indentAmt);
  12622. PrintPnodeWIndent(pnode->AsParseNodeTryCatch()->pnodeCatch, indentAmt);
  12623. break;
  12624. //PTNODE(knopTry , "try" ,None ,Try ,fnopCleanup)
  12625. case knopTry:
  12626. Indent(indentAmt);
  12627. Output::Print(_u("try\n"));
  12628. PrintPnodeWIndent(pnode->AsParseNodeTry()->pnodeBody, indentAmt + INDENT_SIZE);
  12629. break;
  12630. //PTNODE(knopThrow , "throw" ,None ,Uni ,fnopNone)
  12631. case knopThrow:
  12632. Indent(indentAmt);
  12633. Output::Print(_u("throw\n"));
  12634. PrintPnodeWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12635. break;
  12636. //PTNODE(knopClassDecl, "classDecl", None , Class, fnopLeaf)
  12637. case knopClassDecl:
  12638. Indent(indentAmt);
  12639. Output::Print(_u("class %s"), pnode->AsParseNodeClass()->pnodeName->pid->Psz());
  12640. if (pnode->AsParseNodeClass()->pnodeExtends != nullptr)
  12641. {
  12642. Output::Print(_u(" extends "));
  12643. PrintPnodeWIndent(pnode->AsParseNodeClass()->pnodeExtends, 0);
  12644. }
  12645. else {
  12646. Output::Print(_u("\n"));
  12647. }
  12648. PrintPnodeWIndent(pnode->AsParseNodeClass()->pnodeConstructor, indentAmt + INDENT_SIZE);
  12649. PrintPnodeWIndent(pnode->AsParseNodeClass()->pnodeMembers, indentAmt + INDENT_SIZE);
  12650. break;
  12651. case knopStrTemplate:
  12652. Indent(indentAmt);
  12653. Output::Print(_u("string template\n"));
  12654. PrintPnodeListWIndent(pnode->AsParseNodeStrTemplate()->pnodeSubstitutionExpressions, indentAmt + INDENT_SIZE);
  12655. break;
  12656. case knopYieldStar:
  12657. Indent(indentAmt);
  12658. Output::Print(_u("yield*\n"));
  12659. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12660. break;
  12661. case knopYield:
  12662. case knopYieldLeaf:
  12663. Indent(indentAmt);
  12664. Output::Print(_u("yield\n"));
  12665. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12666. break;
  12667. case knopAwait:
  12668. Indent(indentAmt);
  12669. Output::Print(_u("await\n"));
  12670. PrintPnodeListWIndent(pnode->AsParseNodeUni()->pnode1, indentAmt + INDENT_SIZE);
  12671. break;
  12672. case knopExportDefault:
  12673. Indent(indentAmt);
  12674. Output::Print(_u("export default\n"));
  12675. PrintPnodeListWIndent(pnode->AsParseNodeExportDefault()->pnodeExpr, indentAmt + INDENT_SIZE);
  12676. break;
  12677. default:
  12678. Output::Print(_u("unhandled pnode op %d\n"), pnode->nop);
  12679. break;
  12680. }
  12681. }
  12682. void PrintPnodeListWIndent(ParseNode *pnode, int indentAmt) {
  12683. if (pnode != NULL) {
  12684. while (pnode->nop == knopList) {
  12685. PrintPnodeWIndent(pnode->AsParseNodeBin()->pnode1, indentAmt);
  12686. pnode = pnode->AsParseNodeBin()->pnode2;
  12687. }
  12688. PrintPnodeWIndent(pnode, indentAmt);
  12689. }
  12690. }
  12691. void PrintFormalsWIndent(ParseNode *pnodeArgs, int indentAmt)
  12692. {
  12693. for (ParseNode *pnode = pnodeArgs; pnode != nullptr; pnode = pnode->GetFormalNext())
  12694. {
  12695. PrintPnodeWIndent(pnode, indentAmt);
  12696. }
  12697. }
  12698. void PrintPnode(ParseNode *pnode) {
  12699. PrintPnodeWIndent(pnode, 0);
  12700. }
  12701. void ParseNode::Dump()
  12702. {
  12703. switch (nop)
  12704. {
  12705. case knopFncDecl:
  12706. case knopProg:
  12707. LPCOLESTR name = Js::Constants::AnonymousFunction;
  12708. if (this->AsParseNodeFnc()->pnodeName)
  12709. {
  12710. Assert(this->AsParseNodeFnc()->pnodeName->nop == knopVarDecl);
  12711. name = this->AsParseNodeFnc()->pnodeName->pid->Psz();
  12712. }
  12713. Output::Print(_u("%s (%d) [%d, %d]:\n"), name, this->AsParseNodeFnc()->functionId, this->AsParseNodeFnc()->lineNumber, this->AsParseNodeFnc()->columnNumber);
  12714. Output::Print(_u("hasArguments: %s callsEval:%s childCallsEval:%s HasReferenceableBuiltInArguments:%s ArgumentsObjectEscapes:%s HasWith:%s HasOnlyThis:%s \n"),
  12715. IsTrueOrFalse(this->AsParseNodeFnc()->HasHeapArguments()),
  12716. IsTrueOrFalse(this->AsParseNodeFnc()->CallsEval()),
  12717. IsTrueOrFalse(this->AsParseNodeFnc()->ChildCallsEval()),
  12718. IsTrueOrFalse(this->AsParseNodeFnc()->HasReferenceableBuiltInArguments()),
  12719. IsTrueOrFalse(this->AsParseNodeFnc()->GetArgumentsObjectEscapes()),
  12720. IsTrueOrFalse(this->AsParseNodeFnc()->HasWithStmt()),
  12721. IsTrueOrFalse(this->AsParseNodeFnc()->HasOnlyThisStmts()));
  12722. if (this->AsParseNodeFnc()->funcInfo)
  12723. {
  12724. this->AsParseNodeFnc()->funcInfo->Dump();
  12725. }
  12726. break;
  12727. }
  12728. }
  12729. void DumpCapturedNames(ParseNodeFnc* pnodeFnc, IdentPtrSet* capturedNames, ArenaAllocator* alloc)
  12730. {
  12731. auto sortedNames = JsUtil::List<IdentPtr, ArenaAllocator>::New(alloc);
  12732. capturedNames->Map([=](const IdentPtr& pid) -> void {
  12733. sortedNames->Add(pid);
  12734. });
  12735. sortedNames->Sort([](void* context, const void* left, const void* right) -> int {
  12736. const IdentPtr leftIdentPtr = *(const IdentPtr*)(left);
  12737. const IdentPtr rightIdentPtr = *(const IdentPtr*)(right);
  12738. return ::wcscmp(leftIdentPtr->Psz(), rightIdentPtr->Psz());
  12739. }, nullptr);
  12740. sortedNames->Map([=](int index, const IdentPtr pid) -> void {
  12741. OUTPUT_TRACE_DEBUGONLY(Js::CreateParserStatePhase, _u(" Function %u captured name \"%s\"\n"), pnodeFnc->functionId, pid->Psz());
  12742. });
  12743. }
  12744. #endif
  12745. void Parser::AddNestedCapturedNames(ParseNodeFnc* pnodeChildFnc)
  12746. {
  12747. if (m_currentNodeFunc && this->IsCreatingStateCache() && pnodeChildFnc->HasAnyCapturedNames())
  12748. {
  12749. IdentPtrSet* parentCapturedNames = GetCurrentFunctionNode()->EnsureCapturedNames(&m_nodeAllocator);
  12750. IdentPtrSet* childCaptureNames = pnodeChildFnc->GetCapturedNames();
  12751. auto iter = childCaptureNames->GetIterator();
  12752. while (iter.IsValid())
  12753. {
  12754. parentCapturedNames->AddNew(iter.CurrentValue());
  12755. iter.MoveNext();
  12756. }
  12757. }
  12758. }
  12759. void Parser::ProcessCapturedNames(ParseNodeFnc* pnodeFnc)
  12760. {
  12761. if (this->IsCreatingStateCache() && pnodeFnc->HasAnyCapturedNames())
  12762. {
  12763. IdentPtrSet* capturedNames = pnodeFnc->GetCapturedNames();
  12764. auto iter = capturedNames->GetIteratorWithRemovalSupport();
  12765. while (iter.IsValid())
  12766. {
  12767. const IdentPtr& pid = iter.CurrentValueReference();
  12768. PidRefStack* ref = pid->GetTopRef();
  12769. // If the pid has no refs left in our function's scope after binding, we didn't capture it.
  12770. if (!ref || ref->GetFuncScopeId() < pnodeFnc->functionId)
  12771. {
  12772. iter.RemoveCurrent();
  12773. }
  12774. iter.MoveNext();
  12775. }
  12776. #if DBG_DUMP
  12777. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::CreateParserStatePhase))
  12778. {
  12779. DumpCapturedNames(pnodeFnc, capturedNames, &this->m_nodeAllocator);
  12780. fflush(stdout);
  12781. }
  12782. #endif
  12783. }
  12784. }
  12785. void Parser::ReleaseTemporaryGuestArena()
  12786. {
  12787. // In case of modules the Parser lives longer than the temporary Guest Arena. We may have already released the arena explicitly.
  12788. if (!m_tempGuestArenaReleased)
  12789. {
  12790. // The regex patterns list has references to the temporary Guest Arena. Reset it first.
  12791. m_registeredRegexPatterns.Reset();
  12792. if (this->m_scriptContext != nullptr)
  12793. {
  12794. this->m_scriptContext->ReleaseTemporaryGuestAllocator(m_tempGuestArena);
  12795. m_tempGuestArena.Unroot();
  12796. }
  12797. m_tempGuestArenaReleased = true;
  12798. }
  12799. }
  12800. bool Parser::IsCreatingStateCache()
  12801. {
  12802. return (((this->m_grfscr & fscrCreateParserState) == fscrCreateParserState)
  12803. && this->m_functionBody == nullptr
  12804. && CONFIG_FLAG(ParserStateCache));
  12805. }
  12806. void Parser::ShiftCurrDeferredStubToChildFunction(ParseNodeFnc* pnodeFnc, ParseNodeFnc* pnodeFncParent)
  12807. {
  12808. // Goal here is to shift the current deferred stub to point to the stubs for pnodeFnc
  12809. // so we may continue parsing pnodeFnc using the correct set of stubs instead of the
  12810. // stubs for pnodeFncParent.
  12811. // This function assumes we are in the middle of parsing pnodeFnc which is a child
  12812. // nested in pnodeFncParent.
  12813. if (pnodeFnc->IsNested() && pnodeFncParent != nullptr && m_currDeferredStub != nullptr && pnodeFncParent->ichMin != pnodeFnc->ichMin)
  12814. {
  12815. AssertOrFailFast(pnodeFncParent->nestedCount > 0);
  12816. DeferredFunctionStub* childStub = m_currDeferredStub + (pnodeFncParent->nestedCount - 1);
  12817. m_currDeferredStubCount = childStub->nestedCount;
  12818. m_currDeferredStub = childStub->deferredStubs;
  12819. }
  12820. }
  12821. uint Parser::BuildDeferredStubTreeHelper(ParseNodeBlock* pnodeBlock, DeferredFunctionStub* deferredStubs, uint currentStubIndex, uint deferredStubCount, Recycler *recycler)
  12822. {
  12823. Assert(pnodeBlock != nullptr
  12824. && (pnodeBlock->blockType == PnodeBlockType::Function
  12825. || pnodeBlock->blockType == PnodeBlockType::Parameter));
  12826. ParseNodePtr pnodeChild = pnodeBlock->pnodeScopes;
  12827. while (pnodeChild != nullptr)
  12828. {
  12829. if (pnodeChild->nop != knopFncDecl)
  12830. {
  12831. // We only expect to find a function body block in a parameter scope block.
  12832. Assert(pnodeChild->nop == knopBlock
  12833. && (pnodeBlock->blockType == PnodeBlockType::Parameter
  12834. || pnodeChild->AsParseNodeBlock()->blockType == PnodeBlockType::Function));
  12835. pnodeChild = pnodeChild->AsParseNodeBlock()->pnodeNext;
  12836. continue;
  12837. }
  12838. ParseNodeFnc* pnodeFncChild = pnodeChild->AsParseNodeFnc();
  12839. AnalysisAssertOrFailFast(currentStubIndex < deferredStubCount);
  12840. Assert(pnodeFncChild->pnodeBody == nullptr);
  12841. if (pnodeFncChild->IsGeneratedDefault())
  12842. {
  12843. ++currentStubIndex;
  12844. pnodeChild = pnodeFncChild->pnodeNext;
  12845. continue;
  12846. }
  12847. deferredStubs[currentStubIndex].fncFlags = pnodeFncChild->fncFlags;
  12848. deferredStubs[currentStubIndex].nestedCount = pnodeFncChild->nestedCount;
  12849. deferredStubs[currentStubIndex].restorePoint = *pnodeFncChild->pRestorePoint;
  12850. deferredStubs[currentStubIndex].deferredStubs = BuildDeferredStubTree(pnodeFncChild, recycler);
  12851. deferredStubs[currentStubIndex].ichMin = pnodeChild->ichMin;
  12852. // Save the set of captured names onto the deferred stub.
  12853. // Since this set is allocated in the Parser arena, we'll have to convert these
  12854. // into indices in a string table which will survive when the parser goes away.
  12855. deferredStubs[currentStubIndex].capturedNamePointers = pnodeFncChild->GetCapturedNames();
  12856. ++currentStubIndex;
  12857. pnodeChild = pnodeFncChild->pnodeNext;
  12858. }
  12859. return currentStubIndex;
  12860. }
  12861. DeferredFunctionStub * Parser::BuildDeferredStubTree(ParseNodeFnc *pnodeFnc, Recycler *recycler)
  12862. {
  12863. Assert(CONFIG_FLAG(ParserStateCache));
  12864. uint nestedCount = pnodeFnc->nestedCount;
  12865. if (nestedCount == 0)
  12866. {
  12867. return nullptr;
  12868. }
  12869. if (pnodeFnc->deferredStub)
  12870. {
  12871. return pnodeFnc->deferredStub;
  12872. }
  12873. DeferredFunctionStub* deferredStubs = RecyclerNewArray(recycler, DeferredFunctionStub, nestedCount);
  12874. uint currentStubIndex = BuildDeferredStubTreeHelper(pnodeFnc->pnodeScopes, deferredStubs, 0, nestedCount, recycler);
  12875. currentStubIndex = BuildDeferredStubTreeHelper(pnodeFnc->pnodeBodyScope, deferredStubs, currentStubIndex, nestedCount, recycler);
  12876. Assert(currentStubIndex == nestedCount);
  12877. pnodeFnc->deferredStub = deferredStubs;
  12878. return deferredStubs;
  12879. }