| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #include "ParserPch.h"
- #include "FormalsUtil.h"
- #include "../Runtime/Language/SourceDynamicProfileManager.h"
- #if DBG_DUMP
- void PrintPnodeWIndent(ParseNode *pnode,int indentAmt);
- #endif
- const char* const nopNames[knopLim]= {
- #define PTNODE(nop,sn,pc,nk,grfnop,json) sn,
- #include "ptlist.h"
- };
- void printNop(int nop) {
- printf("%s\n",nopNames[nop]);
- }
- const uint ParseNode::mpnopgrfnop[knopLim] =
- {
- #define PTNODE(nop,sn,pc,nk,grfnop,json) grfnop,
- #include "ptlist.h"
- };
- bool Parser::IsES6DestructuringEnabled() const
- {
- return m_scriptContext->GetConfig()->IsES6DestructuringEnabled();
- }
- struct DeferredFunctionStub
- {
- RestorePoint restorePoint;
- uint fncFlags;
- uint nestedCount;
- DeferredFunctionStub *deferredStubs;
- charcount_t ichMin;
- };
- struct StmtNest
- {
- union
- {
- struct
- {
- ParseNodePtr pnodeStmt; // This statement node.
- ParseNodePtr pnodeLab; // Labels for this statement.
- };
- struct
- {
- bool isDeferred : 1;
- OpCode op; // This statement operation.
- LabelId* pLabelId; // Labels for this statement.
- };
- };
- StmtNest *pstmtOuter; // Enclosing statement.
- };
- struct BlockInfoStack
- {
- StmtNest pstmt;
- ParseNode *pnodeBlock;
- ParseNodePtr *m_ppnodeLex; // lexical variable list tail
- BlockInfoStack *pBlockInfoOuter; // containing block's BlockInfoStack
- BlockInfoStack *pBlockInfoFunction; // nearest function's BlockInfoStack (if pnodeBlock is a function, this points to itself)
- };
- #if DEBUG
- Parser::Parser(Js::ScriptContext* scriptContext, BOOL strictMode, PageAllocator *alloc, bool isBackground, size_t size)
- #else
- Parser::Parser(Js::ScriptContext* scriptContext, BOOL strictMode, PageAllocator *alloc, bool isBackground)
- #endif
- : m_nodeAllocator(_u("Parser"), alloc ? alloc : scriptContext->GetThreadContext()->GetPageAllocator(), Parser::OutOfMemory),
- // use the GuestArena directly for keeping the RegexPattern* alive during byte code generation
- m_registeredRegexPatterns(scriptContext->GetGuestArena())
- {
- AssertMsg(size == sizeof(Parser), "verify conditionals affecting the size of Parser agree");
- Assert(scriptContext != nullptr);
- m_isInBackground = isBackground;
- m_phtbl = nullptr;
- m_pscan = nullptr;
- m_deferringAST = FALSE;
- m_stoppedDeferredParse = FALSE;
- m_hasParallelJob = false;
- m_doingFastScan = false;
- m_scriptContext = scriptContext;
- m_pCurrentAstSize = nullptr;
- m_arrayDepth = 0;
- m_funcInArrayDepth = 0;
- m_parenDepth = 0;
- m_funcInArray = 0;
- m_tryCatchOrFinallyDepth = 0;
- m_UsesArgumentsAtGlobal = false;
- m_currentNodeFunc = nullptr;
- m_currentNodeDeferredFunc = nullptr;
- m_currentNodeNonLambdaFunc = nullptr;
- m_currentNodeNonLambdaDeferredFunc = nullptr;
- m_currentNodeProg = nullptr;
- m_currDeferredStub = nullptr;
- m_prevSiblingDeferredStub = nullptr;
- m_pstmtCur = nullptr;
- m_currentBlockInfo = nullptr;
- m_currentScope = nullptr;
- m_currentDynamicBlock = nullptr;
- m_grfscr = fscrNil;
- m_length = 0;
- m_originalLength = 0;
- m_nextFunctionId = nullptr;
- m_errorCallback = nullptr;
- m_uncertainStructure = FALSE;
- currBackgroundParseItem = nullptr;
- backgroundParseItems = nullptr;
- fastScannedRegExpNodes = nullptr;
- m_fUseStrictMode = strictMode;
- m_InAsmMode = false;
- m_deferAsmJs = true;
- m_scopeCountNoAst = 0;
- m_fExpectExternalSource = 0;
- m_parseType = ParseType_Upfront;
- m_deferEllipsisError = false;
- m_hasDeferredShorthandInitError = false;
- m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperDisallowed;
- }
- Parser::~Parser(void)
- {
- if (m_scriptContext == nullptr || m_scriptContext->GetGuestArena() == nullptr)
- {
- // If the scriptContext or guestArena have gone away, there is no point clearing each item of this list.
- // Just reset it so that destructor of the SList will be no-op
- m_registeredRegexPatterns.Reset();
- }
- if (this->m_hasParallelJob)
- {
- #if ENABLE_BACKGROUND_PARSING
- // Let the background threads know that they can decommit their arena pages.
- BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
- Assert(bgp);
- if (bgp->Processor()->ProcessesInBackground())
- {
- JsUtil::BackgroundJobProcessor *processor = static_cast<JsUtil::BackgroundJobProcessor*>(bgp->Processor());
- bool result = processor->IterateBackgroundThreads([&](JsUtil::ParallelThreadData *threadData)->bool {
- threadData->canDecommit = true;
- return false;
- });
- Assert(result);
- }
- #endif
- }
- Release();
- }
- void Parser::OutOfMemory()
- {
- throw ParseExceptionObject(ERRnoMemory);
- }
- void Parser::Error(HRESULT hr)
- {
- Assert(FAILED(hr));
- m_err.Throw(hr);
- }
- void Parser::Error(HRESULT hr, ParseNodePtr pnode)
- {
- if (pnode && pnode->ichLim)
- {
- Error(hr, pnode->ichMin, pnode->ichLim);
- }
- else
- {
- Error(hr);
- }
- }
- void Parser::Error(HRESULT hr, charcount_t ichMin, charcount_t ichLim)
- {
- m_pscan->SetErrorPosition(ichMin, ichLim);
- Error(hr);
- }
- void Parser::IdentifierExpectedError(const Token& token)
- {
- Assert(token.tk != tkID);
- HRESULT hr;
- if (token.IsReservedWord())
- {
- if (token.IsKeyword())
- {
- hr = ERRKeywordNotId;
- }
- else
- {
- Assert(token.IsFutureReservedWord(true));
- if (token.IsFutureReservedWord(false))
- {
- // Future reserved word in strict and non-strict modes
- hr = ERRFutureReservedWordNotId;
- }
- else
- {
- // Future reserved word only in strict mode. The token would have been converted to tkID by the scanner if not
- // in strict mode.
- Assert(IsStrictMode());
- hr = ERRFutureReservedWordInStrictModeNotId;
- }
- }
- }
- else
- {
- hr = ERRnoIdent;
- }
- Error(hr);
- }
- HRESULT Parser::ValidateSyntax(LPCUTF8 pszSrc, size_t encodedCharCount, bool isGenerator, bool isAsync, CompileScriptException *pse, void (Parser::*validateFunction)())
- {
- AssertPsz(pszSrc);
- AssertMemN(pse);
- if (this->IsBackgroundParser())
- {
- PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
- }
- else
- {
- PROBE_STACK(m_scriptContext, Js::Constants::MinStackDefault);
- }
- HRESULT hr;
- SmartFPUControl smartFpuControl;
- DebugOnly( m_err.fInited = TRUE; )
- BOOL fDeferSave = m_deferringAST;
- try
- {
- hr = NOERROR;
- this->PrepareScanner(false);
- m_length = encodedCharCount;
- m_originalLength = encodedCharCount;
- // make sure deferred parsing is turned off
- ULONG grfscr = fscrNil;
- // Give the scanner the source and get the first token
- m_pscan->SetText(pszSrc, 0, encodedCharCount, 0, grfscr);
- m_pscan->SetYieldIsKeyword(isGenerator);
- m_pscan->SetAwaitIsKeyword(isAsync);
- m_pscan->Scan();
- uint nestedCount = 0;
- m_pnestedCount = &nestedCount;
- ParseNodePtr pnodeScope = nullptr;
- m_ppnodeScope = &pnodeScope;
- m_ppnodeExprScope = nullptr;
- uint nextFunctionId = 0;
- m_nextFunctionId = &nextFunctionId;
- m_inDeferredNestedFunc = false;
- m_deferringAST = true;
- m_nextBlockId = 0;
- ParseNode *pnodeFnc = CreateNode(knopFncDecl);
- pnodeFnc->sxFnc.ClearFlags();
- pnodeFnc->sxFnc.SetDeclaration(false);
- pnodeFnc->sxFnc.astSize = 0;
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- pnodeFnc->sxFnc.pnodeParams = nullptr;
- pnodeFnc->sxFnc.pnodeBody = nullptr;
- pnodeFnc->sxFnc.pnodeName = nullptr;
- pnodeFnc->sxFnc.pnodeRest = nullptr;
- pnodeFnc->sxFnc.deferredStub = nullptr;
- pnodeFnc->sxFnc.SetIsGenerator(isGenerator);
- pnodeFnc->sxFnc.SetIsAsync(isAsync);
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- m_currentNodeFunc = pnodeFnc;
- m_currentNodeDeferredFunc = NULL;
- AssertMsg(m_pstmtCur == NULL, "Statement stack should be empty when we start parse function body");
- ParseNodePtr block = StartParseBlock<false>(PnodeBlockType::Function, ScopeType_FunctionBody);
- (this->*validateFunction)();
- FinishParseBlock(block);
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- // there should be nothing after successful parsing for a given construct
- if (m_token.tk != tkEOF)
- Error(ERRsyntax);
- RELEASEPTR(m_pscan);
- m_deferringAST = fDeferSave;
- }
- catch(ParseExceptionObject& e)
- {
- m_deferringAST = fDeferSave;
- m_err.m_hr = e.GetError();
- hr = pse->ProcessError( m_pscan, m_err.m_hr, /* pnodeBase */ NULL);
- }
- return hr;
- }
- HRESULT Parser::ParseSourceInternal(
- __out ParseNodePtr* parseTree, LPCUTF8 pszSrc, size_t offsetInBytes, size_t encodedCharCount, charcount_t offsetInChars,
- bool fromExternal, ULONG grfscr, CompileScriptException *pse, Js::LocalFunctionId * nextFunctionId, ULONG lineNumber, SourceContextInfo * sourceContextInfo)
- {
- AssertMem(parseTree);
- AssertPsz(pszSrc);
- AssertMemN(pse);
- #ifdef ENABLE_BASIC_TELEMETRY
- double startTime = m_scriptContext->GetThreadContext()->ParserTelemetry.Now();
- #endif
-
- if (this->IsBackgroundParser())
- {
- PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackDefault);
- }
- else
- {
- PROBE_STACK(m_scriptContext, Js::Constants::MinStackDefault);
- }
- #ifdef PROFILE_EXEC
- m_scriptContext->ProfileBegin(Js::ParsePhase);
- #endif
- JS_ETW(EventWriteJSCRIPT_PARSE_START(m_scriptContext,0));
- *parseTree = NULL;
- m_sourceLim = 0;
- m_grfscr = grfscr;
- m_sourceContextInfo = sourceContextInfo;
- ParseNodePtr pnodeBase = NULL;
- HRESULT hr;
- SmartFPUControl smartFpuControl;
- DebugOnly( m_err.fInited = TRUE; )
- try
- {
- this->PrepareScanner(fromExternal);
- if ((grfscr & fscrEvalCode) != 0)
- {
- // This makes the parser to believe when eval() is called, it accept any super access in global scope.
- this->m_parsingSuperRestrictionState = Parser::ParsingSuperRestrictionState_SuperCallAndPropertyAllowed;
- }
- if ((grfscr & fscrIsModuleCode) != 0)
- {
- // Module source flag should not be enabled unless module is enabled
- Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
- // Module code is always strict mode code.
- this->m_fUseStrictMode = TRUE;
- }
- // parse the source
- pnodeBase = Parse(pszSrc, offsetInBytes, encodedCharCount, offsetInChars, grfscr, lineNumber, nextFunctionId, pse);
- AssertNodeMem(pnodeBase);
- // Record the actual number of words parsed.
- m_sourceLim = pnodeBase->ichLim - offsetInChars;
- // TODO: The assert can be false positive in some scenarios and chuckj to fix it later
- // Assert(utf8::ByteIndexIntoCharacterIndex(pszSrc + offsetInBytes, encodedCharCount, fromExternal ? utf8::doDefault : utf8::doAllowThreeByteSurrogates) == m_sourceLim);
- #if DBG_DUMP
- if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::ParsePhase))
- {
- PrintPnodeWIndent(pnodeBase,4);
- fflush(stdout);
- }
- #endif
- *parseTree = pnodeBase;
- hr = NOERROR;
- }
- catch(ParseExceptionObject& e)
- {
- m_err.m_hr = e.GetError();
- hr = pse->ProcessError( m_pscan, m_err.m_hr, pnodeBase);
- }
- if (this->m_hasParallelJob)
- {
- #if ENABLE_BACKGROUND_PARSING
- ///// Wait here for remaining jobs to finish. Then look for errors, do final const bindings.
- // pleath TODO: If there are remaining jobs, let the main thread help finish them.
- BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
- Assert(bgp);
- CompileScriptException se;
- this->WaitForBackgroundJobs(bgp, &se);
- BackgroundParseItem *failedItem = bgp->GetFailedBackgroundParseItem();
- if (failedItem)
- {
- CompileScriptException *bgPse = failedItem->GetPSE();
- Assert(bgPse);
- *pse = *bgPse;
- hr = failedItem->GetHR();
- bgp->SetFailedBackgroundParseItem(nullptr);
- }
- if (this->fastScannedRegExpNodes != nullptr)
- {
- this->FinishBackgroundRegExpNodes();
- }
- for (BackgroundParseItem *item = this->backgroundParseItems; item; item = item->GetNext())
- {
- Parser *parser = item->GetParser();
- parser->FinishBackgroundPidRefs(item, this != parser);
- }
- #endif
- }
- // done with the scanner
- RELEASEPTR(m_pscan);
- #ifdef PROFILE_EXEC
- m_scriptContext->ProfileEnd(Js::ParsePhase);
- #endif
- JS_ETW(EventWriteJSCRIPT_PARSE_STOP(m_scriptContext, 0));
- #ifdef ENABLE_BASIC_TELEMETRY
- ThreadContext *threadContext = m_scriptContext->GetThreadContext();
- threadContext->ParserTelemetry.LogTime(threadContext->ParserTelemetry.Now() - startTime);
- #endif
-
- return hr;
- }
- #if ENABLE_BACKGROUND_PARSING
- void Parser::WaitForBackgroundJobs(BackgroundParser *bgp, CompileScriptException *pse)
- {
- // The scan of the script is done, but there may be unfinished background jobs in the queue.
- // Enlist the main thread to help with those.
- BackgroundParseItem *item;
- if (!*bgp->GetPendingBackgroundItemsPtr())
- {
- // We're done.
- return;
- }
- // Save parser state, since we'll need to restore it in order to bind references correctly later.
- this->m_isInBackground = true;
- this->SetCurrBackgroundParseItem(nullptr);
- uint blockIdSave = this->m_nextBlockId;
- uint functionIdSave = *this->m_nextFunctionId;
- StmtNest *pstmtSave = this->m_pstmtCur;
- if (!bgp->Processor()->ProcessesInBackground())
- {
- // No background thread. Just walk the jobs with no locking and process them.
- for (item = bgp->GetNextUnprocessedItem(); item; item = bgp->GetNextUnprocessedItem())
- {
- bgp->Processor()->RemoveJob(item);
- bool succeeded = bgp->Process(item, this, pse);
- bgp->JobProcessed(item, succeeded);
- }
- Assert(!*bgp->GetPendingBackgroundItemsPtr());
- }
- else
- {
- // Background threads. We need to have the critical section in order to:
- // - Check for unprocessed jobs;
- // - Remove jobs from the processor queue;
- // - Do JobsProcessed work (such as removing jobs from the BackgroundParser's unprocessed list).
- CriticalSection *pcs = static_cast<JsUtil::BackgroundJobProcessor*>(bgp->Processor())->GetCriticalSection();
- pcs->Enter();
- for (;;)
- {
- // Grab a job (in lock)
- item = bgp->GetNextUnprocessedItem();
- if (item == nullptr)
- {
- break;
- }
- bgp->Processor()->RemoveJob(item);
- pcs->Leave();
- // Process job (if there is one) (outside lock)
- bool succeeded = bgp->Process(item, this, pse);
- pcs->Enter();
- bgp->JobProcessed(item, succeeded);
- }
- pcs->Leave();
- // Wait for the background threads to finish jobs they're already processing (if any).
- // TODO: Replace with a proper semaphore.
- while(*bgp->GetPendingBackgroundItemsPtr());
- }
- Assert(!*bgp->GetPendingBackgroundItemsPtr());
- // Restore parser state.
- this->m_pstmtCur = pstmtSave;
- this->m_isInBackground = false;
- this->m_nextBlockId = blockIdSave;
- *this->m_nextFunctionId = functionIdSave;
- }
- void Parser::FinishBackgroundPidRefs(BackgroundParseItem *item, bool isOtherParser)
- {
- for (BlockInfoStack *blockInfo = item->GetParseContext()->currentBlockInfo; blockInfo; blockInfo = blockInfo->pBlockInfoOuter)
- {
- if (isOtherParser)
- {
- this->BindPidRefs<true>(blockInfo, item->GetMaxBlockId());
- }
- else
- {
- this->BindPidRefs<false>(blockInfo, item->GetMaxBlockId());
- }
- }
- }
- void Parser::FinishBackgroundRegExpNodes()
- {
- // We have a list of RegExp nodes that we saw on the UI thread in functions we're parallel parsing,
- // and for each background job we have a list of RegExp nodes for which we couldn't allocate patterns.
- // We need to copy the pattern pointers from the UI thread nodes to the corresponding nodes on the
- // background nodes.
- // There may be UI thread nodes for which there are no background thread equivalents, because the UI thread
- // has to assume that the background thread won't defer anything.
- // Note that because these lists (and the list of background jobs) are SList's built by prepending, they are
- // all in reverse lexical order.
- Assert(!this->IsBackgroundParser());
- Assert(this->fastScannedRegExpNodes);
- Assert(this->backgroundParseItems != nullptr);
- BackgroundParseItem *currBackgroundItem;
- #if DBG
- for (currBackgroundItem = this->backgroundParseItems;
- currBackgroundItem;
- currBackgroundItem = currBackgroundItem->GetNext())
- {
- if (currBackgroundItem->RegExpNodeList())
- {
- FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnode, currBackgroundItem->RegExpNodeList())
- {
- Assert(pnode->sxPid.regexPattern == nullptr);
- }
- NEXT_DLIST_ENTRY;
- }
- }
- #endif
- // Hook up the patterns allocated on the main thread to the nodes created on the background thread.
- // Walk the list of foreground nodes, advancing through the work items and looking up each item.
- // Note that the background thread may have chosen to defer a given RegEx literal, so not every foreground
- // node will have a matching background node. Doesn't matter for correctness.
- // (It's inefficient, of course, to have to restart the inner loop from the beginning of the work item's
- // list, but it should be unusual to have many RegExes in a single work item's chunk of code. Figure out how
- // to start the inner loop from a known internal node within the list if that turns out to be important.)
- currBackgroundItem = this->backgroundParseItems;
- FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnodeFgnd, this->fastScannedRegExpNodes)
- {
- Assert(pnodeFgnd->nop == knopRegExp);
- Assert(pnodeFgnd->sxPid.regexPattern != nullptr);
- bool quit = false;
- while (!quit)
- {
- // Find the next work item with a RegEx in it.
- while (currBackgroundItem && currBackgroundItem->RegExpNodeList() == nullptr)
- {
- currBackgroundItem = currBackgroundItem->GetNext();
- }
- if (!currBackgroundItem)
- {
- break;
- }
- // Walk the RegExps in the work item.
- FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnodeBgnd, currBackgroundItem->RegExpNodeList())
- {
- Assert(pnodeBgnd->nop == knopRegExp);
- if (pnodeFgnd->ichMin <= pnodeBgnd->ichMin)
- {
- // Either we found a match, or the next background node is past the foreground node.
- // In any case, we can stop searching.
- if (pnodeFgnd->ichMin == pnodeBgnd->ichMin)
- {
- Assert(pnodeFgnd->ichLim == pnodeBgnd->ichLim);
- pnodeBgnd->sxPid.regexPattern = pnodeFgnd->sxPid.regexPattern;
- }
- quit = true;
- break;
- }
- }
- NEXT_DLIST_ENTRY;
- if (!quit)
- {
- // Need to advance to the next work item.
- currBackgroundItem = currBackgroundItem->GetNext();
- }
- }
- }
- NEXT_DLIST_ENTRY;
- #if DBG
- for (currBackgroundItem = this->backgroundParseItems;
- currBackgroundItem;
- currBackgroundItem = currBackgroundItem->GetNext())
- {
- if (currBackgroundItem->RegExpNodeList())
- {
- FOREACH_DLIST_ENTRY(ParseNodePtr, ArenaAllocator, pnode, currBackgroundItem->RegExpNodeList())
- {
- Assert(pnode->sxPid.regexPattern != nullptr);
- }
- NEXT_DLIST_ENTRY;
- }
- }
- #endif
- }
- #endif
- LabelId* Parser::CreateLabelId(IdentToken* pToken)
- {
- LabelId* pLabelId;
- pLabelId = (LabelId*)m_nodeAllocator.Alloc(sizeof(LabelId));
- if (NULL == pLabelId)
- Error(ERRnoMemory);
- pLabelId->pid = pToken->pid;
- pLabelId->next = NULL;
- return pLabelId;
- }
- /*****************************************************************************
- The following set of routines allocate parse tree nodes of various kinds.
- They catch an exception on out of memory.
- *****************************************************************************/
- static const int g_mpnopcbNode[] =
- {
- #define PTNODE(nop,sn,pc,nk,ok,json) kcbPn##nk,
- #include "ptlist.h"
- };
- const Js::RegSlot NoRegister = (Js::RegSlot)-1;
- const Js::RegSlot OneByteRegister = (Js::RegSlot_OneByte)-1;
- void Parser::InitNode(OpCode nop,ParseNodePtr pnode) {
- pnode->nop = nop;
- pnode->grfpn = PNodeFlags::fpnNone;
- pnode->location = NoRegister;
- pnode->emitLabels = false;
- pnode->isUsed = true;
- pnode->notEscapedUse = false;
- pnode->isInList = false;
- pnode->isCallApplyTargetLoad = false;
- }
- // Create nodes using Arena
- ParseNodePtr
- Parser::StaticCreateBlockNode(ArenaAllocator* alloc, charcount_t ichMin , charcount_t ichLim, int blockId, PnodeBlockType blockType)
- {
- ParseNodePtr pnode = StaticCreateNodeT<knopBlock>(alloc, ichMin, ichLim);
- InitBlockNode(pnode, blockId, blockType);
- return pnode;
- }
- void Parser::InitBlockNode(ParseNodePtr pnode, int blockId, PnodeBlockType blockType)
- {
- Assert(pnode->nop == knopBlock);
- pnode->sxBlock.pnodeScopes = nullptr;
- pnode->sxBlock.pnodeNext = nullptr;
- pnode->sxBlock.scope = nullptr;
- pnode->sxBlock.enclosingBlock = nullptr;
- pnode->sxBlock.pnodeLexVars = nullptr;
- pnode->sxBlock.pnodeStmt = nullptr;
- pnode->sxBlock.pnodeLastValStmt = nullptr;
- pnode->sxBlock.callsEval = false;
- pnode->sxBlock.childCallsEval = false;
- pnode->sxBlock.blockType = blockType;
- pnode->sxBlock.blockId = blockId;
- if (blockType != PnodeBlockType::Regular)
- {
- pnode->grfpn |= PNodeFlags::fpnSyntheticNode;
- }
- }
- // Create Node with limit
- template <OpCode nop>
- ParseNodePtr Parser::CreateNodeT(charcount_t ichMin,charcount_t ichLim)
- {
- Assert(!this->m_deferringAST);
- ParseNodePtr pnode = StaticCreateNodeT<nop>(&m_nodeAllocator, ichMin, ichLim);
- Assert(m_pCurrentAstSize != NULL);
- *m_pCurrentAstSize += GetNodeSize<nop>();
- return pnode;
- }
- ParseNodePtr Parser::CreateDeclNode(OpCode nop, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl)
- {
- ParseNodePtr pnode = CreateNode(nop);
- pnode->sxVar.InitDeclNode(pid, NULL);
- if (symbolType != STUnknown)
- {
- pnode->sxVar.sym = AddDeclForPid(pnode, pid, symbolType, errorOnRedecl);
- }
- return pnode;
- }
- Symbol* Parser::AddDeclForPid(ParseNodePtr pnode, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl)
- {
- Assert(pnode->IsVarLetOrConst());
- PidRefStack *refForUse = nullptr, *refForDecl = nullptr;
- BlockInfoStack *blockInfo;
- bool fBlockScope = false;
- if (pnode->nop != knopVarDecl || symbolType == STFunction)
- {
- Assert(m_pstmtCur);
- if (m_pstmtCur->isDeferred)
- {
- // Deferred parsing: there's no pnodeStmt node, only an opcode on the Stmt struct.
- if (m_pstmtCur->op != knopBlock)
- {
- // Let/const declared in a bare statement context.
- Error(ERRDeclOutOfStmt);
- }
- if (m_pstmtCur->pstmtOuter && m_pstmtCur->pstmtOuter->op == knopSwitch)
- {
- // Let/const declared inside a switch block (requiring conservative use-before-decl check).
- pnode->sxVar.isSwitchStmtDecl = true;
- }
- }
- else
- {
- if (m_pstmtCur->pnodeStmt->nop != knopBlock)
- {
- // Let/const declared in a bare statement context.
- Error(ERRDeclOutOfStmt);
- }
- if (m_pstmtCur->pstmtOuter && m_pstmtCur->pstmtOuter->pnodeStmt->nop == knopSwitch)
- {
- // Let/const declared inside a switch block (requiring conservative use-before-decl check).
- pnode->sxVar.isSwitchStmtDecl = true;
- }
- }
- fBlockScope = pnode->nop != knopVarDecl ||
- (
- !GetCurrentBlockInfo()->pnodeBlock->sxBlock.scope ||
- GetCurrentBlockInfo()->pnodeBlock->sxBlock.scope->GetScopeType() != ScopeType_GlobalEvalBlock
- );
- }
- if (fBlockScope)
- {
- blockInfo = GetCurrentBlockInfo();
- }
- else
- {
- blockInfo = GetCurrentFunctionBlockInfo();
- }
- // If we are creating an 'arguments' Sym at function block scope, create it in
- // the parameter scope instead. That way, if we need to reuse the Sym for the
- // actual arguments object at the end of the function, we don't need to move it
- // into the parameter scope.
- if (pid == wellKnownPropertyPids.arguments
- && pnode->nop == knopVarDecl
- && blockInfo->pnodeBlock->sxBlock.blockType == PnodeBlockType::Function
- && blockInfo->pBlockInfoOuter != nullptr
- && blockInfo->pBlockInfoOuter->pnodeBlock->sxBlock.blockType == PnodeBlockType::Parameter)
- {
- blockInfo = blockInfo->pBlockInfoOuter;
- }
- refForDecl = this->FindOrAddPidRef(pid, blockInfo->pnodeBlock->sxBlock.blockId);
- if (refForDecl == nullptr)
- {
- Error(ERRnoMemory);
- }
- if (blockInfo == GetCurrentBlockInfo())
- {
- refForUse = refForDecl;
- }
- else
- {
- refForUse = this->PushPidRef(pid);
- }
- pnode->sxVar.symRef = refForUse->GetSymRef();
- Symbol *sym = refForDecl->GetSym();
- if (sym != nullptr)
- {
- // Multiple declarations in the same scope. 3 possibilities: error, existing one wins, new one wins.
- switch (pnode->nop)
- {
- case knopLetDecl:
- case knopConstDecl:
- if (!sym->GetDecl()->sxVar.isBlockScopeFncDeclVar)
- {
- Assert(errorOnRedecl);
- // Redeclaration error.
- Error(ERRRedeclaration);
- }
- else
- {
- // (New) let/const hides the (old) var
- sym->SetSymbolType(symbolType);
- sym->SetDecl(pnode);
- }
- break;
- case knopVarDecl:
- if (m_currentScope->GetScopeType() == ScopeType_Parameter)
- {
- // If this is a parameter list, mark the scope to indicate that it has duplicate definition.
- // 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.
- m_currentScope->SetHasDuplicateFormals();
- }
- if (sym->GetDecl() == nullptr)
- {
- Assert(symbolType == STFunction);
- sym->SetDecl(pnode);
- break;
- }
- switch (sym->GetDecl()->nop)
- {
- case knopLetDecl:
- case knopConstDecl:
- // Destructuring made possible to have the formals to be the let bind. But that shouldn't throw the error.
- if (errorOnRedecl && (!IsES6DestructuringEnabled() || sym->GetSymbolType() != STFormal))
- {
- Error(ERRRedeclaration);
- }
- // If !errorOnRedecl, (old) let/const hides the (new) var, so do nothing.
- break;
- case knopVarDecl:
- // Legal redeclaration. Who wins?
- if (errorOnRedecl || sym->GetDecl()->sxVar.isBlockScopeFncDeclVar)
- {
- if (symbolType == STFormal ||
- (symbolType == STFunction && sym->GetSymbolType() != STFormal) ||
- sym->GetSymbolType() == STVariable)
- {
- // New decl wins.
- sym->SetSymbolType(symbolType);
- sym->SetDecl(pnode);
- }
- }
- break;
- }
- break;
- }
- }
- else
- {
- Scope *scope = blockInfo->pnodeBlock->sxBlock.scope;
- if (scope == nullptr)
- {
- Assert(blockInfo->pnodeBlock->sxBlock.blockType == PnodeBlockType::Regular);
- scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, ScopeType_Block);
- blockInfo->pnodeBlock->sxBlock.scope = scope;
- PushScope(scope);
- }
- if (scope->GetScopeType() == ScopeType_GlobalEvalBlock)
- {
- Assert(fBlockScope);
- Assert(scope->GetEnclosingScope() == m_currentNodeProg->sxProg.scope);
- // Check for same-named decl in Global scope.
- PidRefStack *pidRefOld = pid->GetPidRefForScopeId(0);
- if (pidRefOld && pidRefOld->GetSym())
- {
- Error(ERRRedeclaration);
- }
- }
- else if (scope->GetScopeType() == ScopeType_Global && (this->m_grfscr & fscrEvalCode) &&
- !(m_functionBody && m_functionBody->GetScopeInfo()))
- {
- // Check for same-named decl in GlobalEvalBlock scope. Note that this is not necessary
- // if we're compiling a deferred nested function and the global scope was restored from cached info,
- // because in that case we don't need a GlobalEvalScope.
- Assert(!fBlockScope || (this->m_grfscr & fscrConsoleScopeEval) == fscrConsoleScopeEval);
- PidRefStack *pidRefOld = pid->GetPidRefForScopeId(1);
- if (pidRefOld && pidRefOld->GetSym())
- {
- Error(ERRRedeclaration);
- }
- }
- if ((scope->GetScopeType() == ScopeType_FunctionBody || scope->GetScopeType() == ScopeType_Parameter) && symbolType != STFunction)
- {
- ParseNodePtr pnodeFnc = GetCurrentFunctionNode();
- AnalysisAssert(pnodeFnc);
- if (pnodeFnc->sxFnc.pnodeName &&
- pnodeFnc->sxFnc.pnodeName->nop == knopVarDecl &&
- pnodeFnc->sxFnc.pnodeName->sxVar.pid == pid)
- {
- // Named function expression has its name hidden by a local declaration.
- // This is important to know if we don't know whether nested deferred functions refer to it,
- // because if the name has a non-local reference then we have to create a scope object.
- m_currentNodeFunc->sxFnc.SetNameIsHidden();
- }
- }
- if (!sym)
- {
- const char16 *name = reinterpret_cast<const char16*>(pid->Psz());
- int nameLength = pid->Cch();
- SymbolName const symName(name, nameLength);
- Assert(!scope->FindLocalSymbol(symName));
- sym = Anew(&m_nodeAllocator, Symbol, symName, pnode, symbolType);
- scope->AddNewSymbol(sym);
- sym->SetPid(pid);
- }
- refForDecl->SetSym(sym);
- }
- return sym;
- }
- void Parser::RestorePidRefForSym(Symbol *sym)
- {
- IdentPtr pid = m_pscan->m_phtbl->PidHashNameLen(sym->GetName().GetBuffer(), sym->GetName().GetLength());
- Assert(pid);
- sym->SetPid(pid);
- PidRefStack *ref = this->PushPidRef(pid);
- ref->SetSym(sym);
- }
- IdentPtr Parser::GenerateIdentPtr(__ecount(len) char16* name, long len)
- {
- return m_phtbl->PidHashNameLen(name,len);
- }
- IdentPtr Parser::PidFromNode(ParseNodePtr pnode)
- {
- for (;;)
- {
- switch (pnode->nop)
- {
- case knopName:
- return pnode->sxPid.pid;
- case knopVarDecl:
- return pnode->sxVar.pid;
- case knopDot:
- Assert(pnode->sxBin.pnode2->nop == knopName);
- return pnode->sxBin.pnode2->sxPid.pid;
- case knopComma:
- // Advance to the RHS and iterate.
- pnode = pnode->sxBin.pnode2;
- break;
- default:
- return nullptr;
- }
- }
- }
- #if DBG
- void VerifyNodeSize(OpCode nop, int size)
- {
- Assert(nop >= 0 && nop < knopLim);
- __analysis_assume(nop < knopLim);
- Assert(g_mpnopcbNode[nop] == size);
- }
- #endif
- ParseNodePtr Parser::StaticCreateBinNode(OpCode nop, ParseNodePtr pnode1,
- ParseNodePtr pnode2,ArenaAllocator* alloc)
- {
- DebugOnly(VerifyNodeSize(nop, kcbPnBin));
- ParseNodePtr pnode = (ParseNodePtr)alloc->Alloc(kcbPnBin);
- InitNode(nop, pnode);
- pnode->sxBin.pnodeNext = nullptr;
- pnode->sxBin.pnode1 = pnode1;
- pnode->sxBin.pnode2 = pnode2;
- // Statically detect if the add is a concat
- if (!PHASE_OFF1(Js::ByteCodeConcatExprOptPhase))
- {
- // We can't flatten the concat expression if the LHS is not a flatten concat already
- // e.g. a + (<str> + b)
- // Side effect of ToStr(b) need to happen first before ToStr(a)
- // If we flatten the concat expression, we will do ToStr(a) before ToStr(b)
- if ((nop == knopAdd) && (pnode1->CanFlattenConcatExpr() || pnode2->nop == knopStr))
- {
- pnode->grfpn |= fpnCanFlattenConcatExpr;
- }
- }
- return pnode;
- }
- // Create nodes using parser allocator
- ParseNodePtr Parser::CreateNode(OpCode nop, charcount_t ichMin)
- {
- bool nodeAllowed = IsNodeAllowedForDeferParse(nop);
- Assert(nodeAllowed);
- Assert(nop >= 0 && nop < knopLim);
- ParseNodePtr pnode;
- int cb = (nop >= knopNone && nop < knopLim) ? g_mpnopcbNode[nop] : g_mpnopcbNode[knopEmpty];
- pnode = (ParseNodePtr)m_nodeAllocator.Alloc(cb);
- Assert(pnode != nullptr);
- if (!m_deferringAST)
- {
- Assert(m_pCurrentAstSize != nullptr);
- *m_pCurrentAstSize += cb;
- }
- InitNode(nop,pnode);
- // default - may be changed
- pnode->ichMin = ichMin;
- if (m_pscan!= nullptr) {
- pnode->ichLim = m_pscan->IchLimTok();
- }
- else pnode->ichLim=0;
- return pnode;
- }
- ParseNodePtr Parser::CreateUniNode(OpCode nop, ParseNodePtr pnode1)
- {
- Assert(!this->m_deferringAST);
- DebugOnly(VerifyNodeSize(nop, kcbPnUni));
- ParseNodePtr pnode = (ParseNodePtr)m_nodeAllocator.Alloc(kcbPnUni);
- Assert(m_pCurrentAstSize != nullptr);
- *m_pCurrentAstSize += kcbPnUni;
- InitNode(nop, pnode);
- pnode->sxUni.pnode1 = pnode1;
- if (nullptr == pnode1)
- {
- // no ops
- pnode->ichMin = m_pscan->IchMinTok();
- pnode->ichLim = m_pscan->IchLimTok();
- }
- else
- {
- // 1 op
- pnode->ichMin = pnode1->ichMin;
- pnode->ichLim = pnode1->ichLim;
- this->CheckArguments(pnode);
- }
- return pnode;
- }
- ParseNodePtr Parser::CreateBinNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2)
- {
- Assert(!this->m_deferringAST);
- charcount_t ichMin;
- charcount_t ichLim;
- if (nullptr == pnode1)
- {
- // no ops
- Assert(nullptr == pnode2);
- ichMin = m_pscan->IchMinTok();
- ichLim = m_pscan->IchLimTok();
- }
- else
- {
- if (nullptr == pnode2)
- {
- // 1 op
- ichMin = pnode1->ichMin;
- ichLim = pnode1->ichLim;
- }
- else
- {
- // 2 ops
- ichMin = pnode1->ichMin;
- ichLim = pnode2->ichLim;
- if (nop != knopDot && nop != knopIndex)
- {
- this->CheckArguments(pnode2);
- }
- }
- if (nop != knopDot && nop != knopIndex)
- {
- this->CheckArguments(pnode1);
- }
- }
- return CreateBinNode(nop, pnode1, pnode2, ichMin, ichLim);
- }
- ParseNodePtr Parser::CreateTriNode(OpCode nop, ParseNodePtr pnode1,
- ParseNodePtr pnode2, ParseNodePtr pnode3)
- {
- charcount_t ichMin;
- charcount_t ichLim;
- if (nullptr == pnode1)
- {
- // no ops
- Assert(nullptr == pnode2);
- Assert(nullptr == pnode3);
- ichMin = m_pscan->IchMinTok();
- ichLim = m_pscan->IchLimTok();
- }
- else if (nullptr == pnode2)
- {
- // 1 op
- Assert(nullptr == pnode3);
- ichMin = pnode1->ichMin;
- ichLim = pnode1->ichLim;
- }
- else if (nullptr == pnode3)
- {
- // 2 op
- ichMin = pnode1->ichMin;
- ichLim = pnode2->ichLim;
- }
- else
- {
- // 3 ops
- ichMin = pnode1->ichMin;
- ichLim = pnode3->ichLim;
- }
- return CreateTriNode(nop, pnode1, pnode2, pnode3, ichMin, ichLim);
- }
- ParseNodePtr Parser::CreateBlockNode(charcount_t ichMin,charcount_t ichLim, PnodeBlockType blockType)
- {
- return StaticCreateBlockNode(&m_nodeAllocator, ichMin, ichLim, this->m_nextBlockId++, blockType);
- }
- ParseNodePtr
- Parser::CreateCallNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2,charcount_t ichMin,charcount_t ichLim)
- {
- Assert(!this->m_deferringAST);
- DebugOnly(VerifyNodeSize(nop, kcbPnCall));
- ParseNodePtr pnode = (ParseNodePtr)m_nodeAllocator.Alloc(kcbPnCall);
- Assert(m_pCurrentAstSize != nullptr);
- *m_pCurrentAstSize += kcbPnCall;
- InitNode(nop, pnode);
- pnode->sxCall.pnodeTarget = pnode1;
- pnode->sxCall.pnodeArgs = pnode2;
- pnode->sxCall.argCount = 0;
- pnode->sxCall.spreadArgCount = 0;
- pnode->sxCall.callOfConstants = false;
- pnode->sxCall.isApplyCall = false;
- pnode->sxCall.isEvalCall = false;
- pnode->ichMin = ichMin;
- pnode->ichLim = ichLim;
- return pnode;
- }
- ParseNodePtr Parser::CreateStrNode(IdentPtr pid)
- {
- Assert(!this->m_deferringAST);
- ParseNodePtr pnode = CreateNode(knopStr);
- pnode->sxPid.pid=pid;
- pnode->grfpn |= PNodeFlags::fpnCanFlattenConcatExpr;
- return pnode;
- }
- ParseNodePtr Parser::CreateIntNode(long lw)
- {
- ParseNodePtr pnode = CreateNode(knopInt);
- pnode->sxInt.lw = lw;
- return pnode;
- }
- // Create Node with scanner limit
- template <OpCode nop>
- ParseNodePtr Parser::CreateNodeWithScanner()
- {
- Assert(m_pscan != nullptr);
- return CreateNodeWithScanner<nop>(m_pscan->IchMinTok());
- }
- template <OpCode nop>
- ParseNodePtr Parser::CreateNodeWithScanner(charcount_t ichMin)
- {
- Assert(m_pscan != nullptr);
- return CreateNodeT<nop>(ichMin, m_pscan->IchLimTok());
- }
- ParseNodePtr Parser::CreateProgNodeWithScanner(bool isModuleSource)
- {
- ParseNodePtr pnodeProg;
- if (isModuleSource)
- {
- pnodeProg = CreateNodeWithScanner<knopModule>();
- // knopModule is not actually handled anywhere since we would need to handle it everywhere we could
- // have knopProg and it would be treated exactly the same except for import/export statements.
- // We are only using it as a way to get the correct size for PnModule.
- // Consider: Should we add a flag to PnProg which is false but set to true in PnModule?
- // If we do, it can't be a virtual method since the parse nodes are all in a union.
- pnodeProg->nop = knopProg;
- }
- else
- {
- pnodeProg = CreateNodeWithScanner<knopProg>();
- }
- return pnodeProg;
- }
- ParseNodePtr Parser::CreateCallNode(OpCode nop, ParseNodePtr pnode1, ParseNodePtr pnode2)
- {
- charcount_t ichMin;
- charcount_t ichLim;
- if (nullptr == pnode1)
- {
- Assert(nullptr == pnode2);
- ichMin = m_pscan->IchMinTok();
- ichLim = m_pscan->IchLimTok();
- }
- else
- {
- if (nullptr == pnode2)
- {
- ichMin = pnode1->ichMin;
- ichLim = pnode1->ichLim;
- }
- else
- {
- ichMin = pnode1->ichMin;
- ichLim = pnode2->ichLim;
- }
- if (pnode1->nop == knopDot || pnode1->nop == knopIndex)
- {
- this->CheckArguments(pnode1->sxBin.pnode1);
- }
- }
- return CreateCallNode(nop, pnode1, pnode2, ichMin, ichLim);
- }
- ParseNodePtr Parser::CreateStrNodeWithScanner(IdentPtr pid)
- {
- Assert(!this->m_deferringAST);
- ParseNodePtr pnode = CreateNodeWithScanner<knopStr>();
- pnode->sxPid.pid=pid;
- pnode->grfpn |= PNodeFlags::fpnCanFlattenConcatExpr;
- return pnode;
- }
- ParseNodePtr Parser::CreateIntNodeWithScanner(long lw)
- {
- Assert(!this->m_deferringAST);
- ParseNodePtr pnode = CreateNodeWithScanner<knopInt>();
- pnode->sxInt.lw = lw;
- return pnode;
- }
- ParseNodePtr Parser::CreateTempNode(ParseNode* initExpr)
- {
- ParseNodePtr pnode = CreateNode(knopTemp, (charcount_t)0);
- pnode->sxVar.pnodeInit =initExpr;
- pnode->sxVar.pnodeNext = nullptr;
- return pnode;
- }
- ParseNodePtr Parser::CreateTempRef(ParseNode* tempNode)
- {
- ParseNodePtr pnode = CreateUniNode(knopTempRef, tempNode);
- return pnode;
- }
- void Parser::CheckPidIsValid(IdentPtr pid, bool autoArgumentsObject)
- {
- if (IsStrictMode())
- {
- // in strict mode, variable named 'eval' cannot be created
- if (pid == wellKnownPropertyPids.eval)
- {
- Error(ERREvalUsage);
- }
- else if (pid == wellKnownPropertyPids.arguments && !autoArgumentsObject)
- {
- Error(ERRArgsUsage);
- }
- }
- }
- // CreateVarDecl needs m_ppnodeVar to be pointing to the right function.
- // Post-parsing rewriting during bytecode gen may have m_ppnodeVar pointing to the last parsed function.
- // This function sets up m_ppnodeVar to point to the given pnodeFnc and creates the new var declaration.
- // This prevents accidentally adding var declarations to the last parsed function.
- ParseNodePtr Parser::AddVarDeclNode(IdentPtr pid, ParseNodePtr pnodeFnc)
- {
- AnalysisAssert(pnodeFnc);
- ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- while (*m_ppnodeVar != nullptr)
- {
- m_ppnodeVar = &(*m_ppnodeVar)->sxVar.pnodeNext;
- }
- ParseNodePtr pnode = CreateVarDeclNode(pid, STUnknown, false, 0, /* checkReDecl = */ false);
- m_ppnodeVar = ppnodeVarSave;
- return pnode;
- }
- ParseNodePtr Parser::CreateModuleImportDeclNode(IdentPtr localName)
- {
- ParseNodePtr declNode = CreateBlockScopedDeclNode(localName, knopConstDecl);
- Symbol* sym = declNode->sxVar.sym;
- sym->SetIsModuleExportStorage(true);
- sym->SetIsModuleImport(true);
- return declNode;
- }
- void Parser::MarkIdentifierReferenceIsModuleExport(IdentPtr localName)
- {
- PidRefStack* pidRef = this->PushPidRef(localName);
- Assert(pidRef != nullptr);
- pidRef->SetModuleExport();
- }
- ParseNodePtr Parser::CreateVarDeclNode(IdentPtr pid, SymbolType symbolType, bool autoArgumentsObject, ParseNodePtr pnodeFnc, bool errorOnRedecl)
- {
- ParseNodePtr pnode = CreateDeclNode(knopVarDecl, pid, symbolType, errorOnRedecl);
- // Append the variable to the end of the current variable list.
- AssertMem(m_ppnodeVar);
- pnode->sxVar.pnodeNext = *m_ppnodeVar;
- *m_ppnodeVar = pnode;
- if (nullptr != pid)
- {
- // this is not a temp - make sure temps go after this node
- AssertMem(pid);
- m_ppnodeVar = &pnode->sxVar.pnodeNext;
- CheckPidIsValid(pid, autoArgumentsObject);
- }
- return pnode;
- }
- ParseNodePtr Parser::CreateBlockScopedDeclNode(IdentPtr pid, OpCode nodeType)
- {
- Assert(nodeType == knopConstDecl || nodeType == knopLetDecl);
- ParseNodePtr pnode = CreateDeclNode(nodeType, pid, STVariable, true);
- if (nullptr != pid)
- {
- AssertMem(pid);
- pid->SetIsLetOrConst();
- AddVarDeclToBlock(pnode);
- CheckPidIsValid(pid);
- }
- return pnode;
- }
- void Parser::AddVarDeclToBlock(ParseNode *pnode)
- {
- Assert(pnode->nop == knopConstDecl || pnode->nop == knopLetDecl);
- // Maintain a combined list of let and const declarations to keep
- // track of declaration order.
- AssertMem(m_currentBlockInfo->m_ppnodeLex);
- *m_currentBlockInfo->m_ppnodeLex = pnode;
- m_currentBlockInfo->m_ppnodeLex = &pnode->sxVar.pnodeNext;
- pnode->sxVar.pnodeNext = nullptr;
- }
- void Parser::SetCurrentStatement(StmtNest *stmt)
- {
- m_pstmtCur = stmt;
- }
- template<bool buildAST>
- ParseNodePtr Parser::StartParseBlockWithCapacity(PnodeBlockType blockType, ScopeType scopeType, int capacity)
- {
- Scope *scope = nullptr;
- // Block scopes are created lazily when we discover block-scoped content.
- if (scopeType != ScopeType_Unknown && scopeType != ScopeType_Block)
- {
- scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, scopeType, PHASE_OFF1(Js::ParserBindPhase), capacity);
- PushScope(scope);
- }
- return StartParseBlockHelper<buildAST>(blockType, scope, nullptr, nullptr);
- }
- template<bool buildAST>
- ParseNodePtr Parser::StartParseBlock(PnodeBlockType blockType, ScopeType scopeType, ParseNodePtr pnodeLabel, LabelId* pLabelId)
- {
- Scope *scope = nullptr;
- // Block scopes are created lazily when we discover block-scoped content.
- if (scopeType != ScopeType_Unknown && scopeType != ScopeType_Block)
- {
- scope = Anew(&m_nodeAllocator, Scope, &m_nodeAllocator, scopeType);
- PushScope(scope);
- }
- return StartParseBlockHelper<buildAST>(blockType, scope, pnodeLabel, pLabelId);
- }
- template<bool buildAST>
- ParseNodePtr Parser::StartParseBlockHelper(PnodeBlockType blockType, Scope *scope, ParseNodePtr pnodeLabel, LabelId* pLabelId)
- {
- ParseNodePtr pnodeBlock = CreateBlockNode(blockType);
- pnodeBlock->sxBlock.scope = scope;
- BlockInfoStack *newBlockInfo = PushBlockInfo(pnodeBlock);
- PushStmt<buildAST>(&newBlockInfo->pstmt, pnodeBlock, knopBlock, pnodeLabel, pLabelId);
- return pnodeBlock;
- }
- void Parser::PushScope(Scope *scope)
- {
- Assert(scope);
- scope->SetEnclosingScope(m_currentScope);
- m_currentScope = scope;
- }
- void Parser::PopScope(Scope *scope)
- {
- Assert(scope == m_currentScope);
- m_currentScope = scope->GetEnclosingScope();
- scope->SetEnclosingScope(nullptr);
- }
- void Parser::PushFuncBlockScope(ParseNodePtr pnodeBlock, ParseNodePtr **ppnodeScopeSave, ParseNodePtr **ppnodeExprScopeSave)
- {
- // Maintain the scope tree.
- pnodeBlock->sxBlock.pnodeScopes = nullptr;
- pnodeBlock->sxBlock.pnodeNext = nullptr;
- // Insert this block into the active list of scopes (m_ppnodeExprScope or m_ppnodeScope).
- // Save the current block's "next" pointer as the new endpoint of that list.
- if (m_ppnodeExprScope)
- {
- *ppnodeScopeSave = m_ppnodeScope;
- Assert(*m_ppnodeExprScope == nullptr);
- *m_ppnodeExprScope = pnodeBlock;
- *ppnodeExprScopeSave = &pnodeBlock->sxBlock.pnodeNext;
- }
- else
- {
- Assert(m_ppnodeScope);
- Assert(*m_ppnodeScope == nullptr);
- *m_ppnodeScope = pnodeBlock;
- *ppnodeScopeSave = &pnodeBlock->sxBlock.pnodeNext;
- *ppnodeExprScopeSave = m_ppnodeExprScope;
- }
- // Advance the global scope list pointer to the new block's child list.
- m_ppnodeScope = &pnodeBlock->sxBlock.pnodeScopes;
- // Set m_ppnodeExprScope to NULL to make that list inactive.
- m_ppnodeExprScope = nullptr;
- }
- void Parser::PopFuncBlockScope(ParseNodePtr *ppnodeScopeSave, ParseNodePtr *ppnodeExprScopeSave)
- {
- Assert(m_ppnodeExprScope == nullptr || *m_ppnodeExprScope == nullptr);
- m_ppnodeExprScope = ppnodeExprScopeSave;
- AssertMem(m_ppnodeScope);
- Assert(nullptr == *m_ppnodeScope);
- m_ppnodeScope = ppnodeScopeSave;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseBlock(ParseNodePtr pnodeLabel, LabelId* pLabelId)
- {
- ParseNodePtr pnodeBlock = nullptr;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block, pnodeLabel, pLabelId);
- BlockInfoStack* outerBlockInfo = m_currentBlockInfo->pBlockInfoOuter;
- if (outerBlockInfo != nullptr && outerBlockInfo->pnodeBlock != nullptr
- && outerBlockInfo->pnodeBlock->sxBlock.scope != nullptr
- && outerBlockInfo->pnodeBlock->sxBlock.scope->GetScopeType() == ScopeType_CatchParamPattern)
- {
- // If we are parsing the catch block then destructured params can have let declrations. Let's add them to the new block.
- for (ParseNodePtr pnode = m_currentBlockInfo->pBlockInfoOuter->pnodeBlock->sxBlock.pnodeLexVars; pnode; pnode = pnode->sxVar.pnodeNext)
- {
- PidRefStack* ref = PushPidRef(pnode->sxVar.sym->GetPid());
- ref->SetSym(pnode->sxVar.sym);
- }
- }
- ChkCurTok(tkLCurly, ERRnoLcurly);
- ParseNodePtr * ppnodeList = nullptr;
- if (buildAST)
- {
- PushFuncBlockScope(pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
- ppnodeList = &pnodeBlock->sxBlock.pnodeStmt;
- }
- ParseStmtList<buildAST>(ppnodeList);
- if (buildAST)
- {
- PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
- }
- FinishParseBlock(pnodeBlock);
- ChkCurTok(tkRCurly, ERRnoRcurly);
- return pnodeBlock;
- }
- void Parser::FinishParseBlock(ParseNode *pnodeBlock, bool needScanRCurly)
- {
- Assert(m_currentBlockInfo != nullptr && pnodeBlock == m_currentBlockInfo->pnodeBlock);
- if (needScanRCurly)
- {
- // Only update the ichLim if we were expecting an RCurly. If there is an
- // expression body without a necessary RCurly, the correct ichLim will
- // have been set already.
- pnodeBlock->ichLim = m_pscan->IchLimTok();
- }
- BindPidRefs<false>(GetCurrentBlockInfo(), m_nextBlockId - 1);
- PopStmt(&m_currentBlockInfo->pstmt);
- PopBlockInfo();
- Scope *scope = pnodeBlock->sxBlock.scope;
- if (scope)
- {
- PopScope(scope);
- }
- }
- void Parser::FinishParseFncExprScope(ParseNodePtr pnodeFnc, ParseNodePtr pnodeFncExprScope)
- {
- int fncExprScopeId = pnodeFncExprScope->sxBlock.blockId;
- ParseNodePtr pnodeName = pnodeFnc->sxFnc.pnodeName;
- if (pnodeName)
- {
- Assert(pnodeName->nop == knopVarDecl);
- BindPidRefsInScope(pnodeName->sxVar.pid, pnodeName->sxVar.sym, fncExprScopeId);
- }
- FinishParseBlock(pnodeFncExprScope);
- }
- template <const bool backgroundPidRef>
- void Parser::BindPidRefs(BlockInfoStack *blockInfo, uint maxBlockId)
- {
- // We need to bind all assignments in order to emit assignment to 'const' error
- int blockId = blockInfo->pnodeBlock->sxBlock.blockId;
- Scope *scope = blockInfo->pnodeBlock->sxBlock.scope;
- if (scope)
- {
- auto bindPidRefs = [blockId, maxBlockId, this](Symbol *sym)
- {
- ParseNodePtr pnode = sym->GetDecl();
- IdentPtr pid;
- #if PROFILE_DICTIONARY
- int depth = 0;
- #endif
- Assert(pnode);
- switch (pnode->nop)
- {
- case knopLetDecl:
- case knopVarDecl:
- pid = pnode->sxVar.pid;
- if (backgroundPidRef)
- {
- pid = this->m_pscan->m_phtbl->FindExistingPid(pid->Psz(), pid->Cch(), pid->Hash(), nullptr, nullptr
- #if PROFILE_DICTIONARY
- , depth
- #endif
- );
- if (pid == nullptr)
- {
- break;
- }
- }
- this->BindPidRefsInScope(pid, sym, blockId, maxBlockId);
- break;
- case knopConstDecl:
- pid = pnode->sxVar.pid;
- if (backgroundPidRef)
- {
- pid = this->m_pscan->m_phtbl->FindExistingPid(pid->Psz(), pid->Cch(), pid->Hash(), nullptr, nullptr
- #if PROFILE_DICTIONARY
- , depth
- #endif
- );
- if (pid == nullptr)
- {
- break;
- }
- }
- this->BindConstPidRefsInScope(pid, sym, blockId, maxBlockId);
- break;
- case knopName:
- pid = pnode->sxPid.pid;
- if (backgroundPidRef)
- {
- pid = this->m_pscan->m_phtbl->FindExistingPid(pid->Psz(), pid->Cch(), pid->Hash(), nullptr, nullptr
- #if PROFILE_DICTIONARY
- , depth
- #endif
- );
- if (pid == nullptr)
- {
- break;
- }
- }
- this->BindPidRefsInScope(pid, sym, blockId, maxBlockId);
- break;
- default:
- Assert(0);
- break;
- }
- };
- scope->ForEachSymbol(bindPidRefs);
- }
- }
- void Parser::BindPidRefsInScope(IdentPtr pid, Symbol *sym, int blockId, uint maxBlockId)
- {
- this->BindPidRefsInScopeImpl<false>(pid, sym, blockId, maxBlockId);
- }
- void Parser::BindConstPidRefsInScope(IdentPtr pid, Symbol *sym, int blockId, uint maxBlockId)
- {
- this->BindPidRefsInScopeImpl<true>(pid, sym, blockId, maxBlockId);
- }
- template<const bool isConstBinding>
- void Parser::BindPidRefsInScopeImpl(IdentPtr pid, Symbol *sym, int blockId, uint maxBlockId)
- {
- PidRefStack *ref, *nextRef, *lastRef = nullptr;
- Assert(sym);
- for (ref = pid->GetTopRef(); ref && ref->GetScopeId() >= blockId; ref = nextRef)
- {
- // Fix up sym* on PID ref.
- Assert(!ref->GetSym() || ref->GetSym() == sym);
- nextRef = ref->prev;
- Assert(ref->GetScopeId() >= 0);
- if ((uint)ref->GetScopeId() > maxBlockId)
- {
- lastRef = ref;
- continue;
- }
- ref->SetSym(sym);
- this->RemovePrevPidRef(pid, lastRef);
- if (ref->IsModuleExport())
- {
- Assert(sym->GetIsGlobal());
- sym->SetIsModuleExportStorage(true);
- }
- if (ref->GetScopeId() == blockId)
- {
- break;
- }
- }
- }
- void Parser::PopStmt(StmtNest *pStmt)
- {
- Assert(pStmt == m_pstmtCur);
- SetCurrentStatement(m_pstmtCur->pstmtOuter);
- }
- BlockInfoStack *Parser::PushBlockInfo(ParseNodePtr pnodeBlock)
- {
- BlockInfoStack *newBlockInfo = (BlockInfoStack *)m_nodeAllocator.Alloc(sizeof(BlockInfoStack));
- Assert(nullptr != newBlockInfo);
- newBlockInfo->pnodeBlock = pnodeBlock;
- newBlockInfo->pBlockInfoOuter = m_currentBlockInfo;
- newBlockInfo->m_ppnodeLex = &pnodeBlock->sxBlock.pnodeLexVars;
- if (pnodeBlock->sxBlock.blockType != PnodeBlockType::Regular)
- {
- newBlockInfo->pBlockInfoFunction = newBlockInfo;
- }
- else
- {
- Assert(m_currentBlockInfo);
- newBlockInfo->pBlockInfoFunction = m_currentBlockInfo->pBlockInfoFunction;
- }
- m_currentBlockInfo = newBlockInfo;
- return newBlockInfo;
- }
- void Parser::PopBlockInfo()
- {
- Assert(m_currentBlockInfo);
- PopDynamicBlock();
- m_currentBlockInfo = m_currentBlockInfo->pBlockInfoOuter;
- }
- void Parser::PushDynamicBlock()
- {
- Assert(GetCurrentBlock());
- int blockId = GetCurrentBlock()->sxBlock.blockId;
- if (m_currentDynamicBlock && m_currentDynamicBlock->id == blockId)
- {
- return;
- }
- BlockIdsStack *info = (BlockIdsStack *)m_nodeAllocator.Alloc(sizeof(BlockIdsStack));
- if (nullptr == info)
- {
- Error(ERRnoMemory);
- }
- info->id = blockId;
- info->prev = m_currentDynamicBlock;
- m_currentDynamicBlock = info;
- }
- void Parser::PopDynamicBlock()
- {
- int blockId = GetCurrentDynamicBlockId();
- if (GetCurrentBlock()->sxBlock.blockId != blockId || blockId == -1)
- {
- return;
- }
- Assert(m_currentDynamicBlock);
- for (BlockInfoStack *blockInfo = m_currentBlockInfo; blockInfo; blockInfo = blockInfo->pBlockInfoOuter)
- {
- for (ParseNodePtr pnodeDecl = blockInfo->pnodeBlock->sxBlock.pnodeLexVars;
- pnodeDecl;
- pnodeDecl = pnodeDecl->sxVar.pnodeNext)
- {
- this->SetPidRefsInScopeDynamic(pnodeDecl->sxVar.pid, blockId);
- }
- }
- m_currentDynamicBlock = m_currentDynamicBlock->prev;
- }
- int Parser::GetCurrentDynamicBlockId() const
- {
- return m_currentDynamicBlock ? m_currentDynamicBlock->id : -1;
- }
- ParseNode *Parser::GetCurrentFunctionNode()
- {
- if (m_currentNodeDeferredFunc != nullptr)
- {
- return m_currentNodeDeferredFunc;
- }
- else if (m_currentNodeFunc != nullptr)
- {
- return m_currentNodeFunc;
- }
- else
- {
- AssertMsg(GetFunctionBlock()->sxBlock.blockType == PnodeBlockType::Global,
- "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'");
- return m_currentNodeProg;
- }
- }
- ParseNode *Parser::GetCurrentNonLamdaFunctionNode()
- {
- if (m_currentNodeNonLambdaDeferredFunc != nullptr)
- {
- return m_currentNodeNonLambdaDeferredFunc;
- }
- return m_currentNodeNonLambdaFunc;
- }
- void Parser::RegisterRegexPattern(UnifiedRegex::RegexPattern *const regexPattern)
- {
- Assert(regexPattern);
- // ensure a no-throw add behavior here, to catch out of memory exceptions, using the guest arena allocator
- if (!m_registeredRegexPatterns.PrependNoThrow(m_scriptContext->GetGuestArena(), regexPattern))
- {
- Parser::Error(ERRnoMemory);
- }
- }
- void Parser::CaptureState(ParserState *state)
- {
- Assert(state != nullptr);
- state->m_funcInArraySave = m_funcInArray;
- state->m_funcInArrayDepthSave = m_funcInArrayDepth;
- state->m_nestedCountSave = *m_pnestedCount;
- state->m_ppnodeScopeSave = m_ppnodeScope;
- state->m_ppnodeExprScopeSave = m_ppnodeExprScope;
- state->m_pCurrentAstSizeSave = m_pCurrentAstSize;
- Assert(state->m_ppnodeScopeSave == nullptr || *state->m_ppnodeScopeSave == nullptr);
- Assert(state->m_ppnodeExprScopeSave == nullptr || *state->m_ppnodeExprScopeSave == nullptr);
- #if DEBUG
- state->m_currentBlockInfo = m_currentBlockInfo;
- #endif
- }
- void Parser::RestoreStateFrom(ParserState *state)
- {
- Assert(state != nullptr);
- Assert(state->m_currentBlockInfo == m_currentBlockInfo);
- m_funcInArray = state->m_funcInArraySave;
- m_funcInArrayDepth = state->m_funcInArrayDepthSave;
- *m_pnestedCount = state->m_nestedCountSave;
- m_pCurrentAstSize = state->m_pCurrentAstSizeSave;
- if (state->m_ppnodeScopeSave != nullptr)
- {
- *state->m_ppnodeScopeSave = nullptr;
- }
- if (state->m_ppnodeExprScopeSave != nullptr)
- {
- *state->m_ppnodeExprScopeSave = nullptr;
- }
- m_ppnodeScope = state->m_ppnodeScopeSave;
- m_ppnodeExprScope = state->m_ppnodeExprScopeSave;
- }
- void Parser::AddToNodeListEscapedUse(ParseNode ** ppnodeList, ParseNode *** pppnodeLast,
- ParseNode * pnodeAdd)
- {
- AddToNodeList(ppnodeList, pppnodeLast, pnodeAdd);
- pnodeAdd->SetIsInList();
- }
- void Parser::AddToNodeList(ParseNode ** ppnodeList, ParseNode *** pppnodeLast,
- ParseNode * pnodeAdd)
- {
- Assert(!this->m_deferringAST);
- if (nullptr == *pppnodeLast)
- {
- // should be an empty list
- Assert(nullptr == *ppnodeList);
- *ppnodeList = pnodeAdd;
- *pppnodeLast = ppnodeList;
- }
- else
- {
- //
- AssertNodeMem(*ppnodeList);
- AssertNodeMem(**pppnodeLast);
- ParseNode *pnodeT = CreateBinNode(knopList, **pppnodeLast, pnodeAdd);
- **pppnodeLast = pnodeT;
- *pppnodeLast = &pnodeT->sxBin.pnode2;
- }
- }
- // Check reference to "arguments" that indicates the object may escape.
- void Parser::CheckArguments(ParseNodePtr pnode)
- {
- if (m_currentNodeFunc && this->NodeIsIdent(pnode, wellKnownPropertyPids.arguments))
- {
- m_currentNodeFunc->sxFnc.SetHasHeapArguments();
- }
- }
- // Check use of "arguments" that requires instantiation of the object.
- void Parser::CheckArgumentsUse(IdentPtr pid, ParseNodePtr pnodeFnc)
- {
- if (pid == wellKnownPropertyPids.arguments)
- {
- if (pnodeFnc != nullptr)
- {
- pnodeFnc->sxFnc.SetUsesArguments(TRUE);
- }
- else
- {
- m_UsesArgumentsAtGlobal = true;
- }
- }
- }
- void Parser::CheckStrictModeEvalArgumentsUsage(IdentPtr pid, ParseNodePtr pnode)
- {
- if (pid != nullptr)
- {
- // In strict mode, 'eval' / 'arguments' cannot be assigned to.
- if ( pid == wellKnownPropertyPids.eval)
- {
- Error(ERREvalUsage, pnode);
- }
- if (pid == wellKnownPropertyPids.arguments)
- {
- Error(ERRArgsUsage, pnode);
- }
- }
- }
- void Parser::ReduceDeferredScriptLength(size_t chars)
- {
- // If we're in deferred mode, subtract the given char count from the total length,
- // and see if this puts us under the deferral threshold.
- if ((m_grfscr & fscrDeferFncParse) &&
- (
- PHASE_OFF1(Js::DeferEventHandlersPhase) ||
- (m_grfscr & fscrGlobalCode)
- )
- )
- {
- if (m_length > chars)
- {
- m_length -= chars;
- }
- else
- {
- m_length = 0;
- }
- if (m_length < Parser::GetDeferralThreshold(this->m_sourceContextInfo->IsSourceProfileLoaded()))
- {
- // Stop deferring.
- m_grfscr &= ~fscrDeferFncParse;
- m_stoppedDeferredParse = TRUE;
- }
- }
- }
- /***************************************************************************
- Look for an existing label with the given name.
- ***************************************************************************/
- BOOL Parser::PnodeLabelNoAST(IdentToken* pToken, LabelId* pLabelIdList)
- {
- StmtNest* pStmt;
- LabelId* pLabelId;
- // Look in the label stack.
- for (pStmt = m_pstmtCur; pStmt != nullptr; pStmt = pStmt->pstmtOuter)
- {
- for (pLabelId = pStmt->pLabelId; pLabelId != nullptr; pLabelId = pLabelId->next)
- {
- if (pLabelId->pid == pToken->pid)
- return TRUE;
- }
- }
- // Also look in the pnodeLabels list.
- for (pLabelId = pLabelIdList; pLabelId != nullptr; pLabelId = pLabelId->next)
- {
- if (pLabelId->pid == pToken->pid)
- return TRUE;
- }
- return FALSE;
- }
- void Parser::EnsureStackAvailable()
- {
- if (!m_scriptContext->GetThreadContext()->IsStackAvailable(Js::Constants::MinStackCompile))
- {
- Error(ERRnoMemory);
- }
- }
- void Parser::ThrowNewTargetSyntaxErrForGlobalScope()
- {
- //TODO: (falotfi) we need reliably distinguish eval in global scope vs in a function
- // The rule for this syntax error is any time new.target is called at global scope
- // we are excluding new.target in eval at global scope for now.
- if(GetCurrentNonLamdaFunctionNode() == nullptr && (this->m_grfscr & fscrEvalCode) == 0)
- {
- Error(ERRInvalidNewTarget);
- }
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseMetaProperty(tokens metaParentKeyword, charcount_t ichMin, _Out_opt_ BOOL* pfCanAssign)
- {
- AssertMsg(metaParentKeyword == tkNEW, "Only supported for tkNEW parent keywords");
- AssertMsg(this->m_token.tk == tkDot, "We must be currently sitting on the dot after the parent keyword");
- m_pscan->Scan();
- if (this->m_token.tk == tkID && this->m_token.GetIdentifier(m_phtbl) == this->GetTargetPid())
- {
- ThrowNewTargetSyntaxErrForGlobalScope();
- if (pfCanAssign)
- {
- *pfCanAssign = FALSE;
- }
- if (buildAST)
- {
- return CreateNodeWithScanner<knopNewTarget>(ichMin);
- }
- }
- else
- {
- Error(ERRsyntax);
- }
- return nullptr;
- }
- template<bool buildAST>
- void Parser::ParseNamedImportOrExportClause(ModuleImportOrExportEntryList* importOrExportEntryList, bool isExportClause)
- {
- Assert(m_token.tk == tkLCurly);
- Assert(importOrExportEntryList != nullptr);
- m_pscan->Scan();
- while (m_token.tk != tkRCurly && m_token.tk != tkEOF)
- {
- tokens firstToken = m_token.tk;
- if (!(m_token.IsIdentifier() || m_token.IsReservedWord()))
- {
- Error(ERRsyntax);
- }
- IdentPtr identifierName = m_token.GetIdentifier(m_phtbl);
- IdentPtr identifierAs = identifierName;
- m_pscan->Scan();
- if (m_token.tk == tkID)
- {
- // We have the pattern "IdentifierName as"
- if (wellKnownPropertyPids.as != m_token.GetIdentifier(m_phtbl))
- {
- Error(ERRsyntax);
- }
- m_pscan->Scan();
- // If we are parsing an import statement, the token after 'as' must be a BindingIdentifier.
- if (!isExportClause)
- {
- ChkCurTokNoScan(tkID, ERRsyntax);
- }
- if (!(m_token.IsIdentifier() || m_token.IsReservedWord()))
- {
- Error(ERRsyntax);
- }
- identifierAs = m_token.GetIdentifier(m_phtbl);
- // Scan to the next token.
- m_pscan->Scan();
- }
- else if (!isExportClause && firstToken != tkID)
- {
- // If we are parsing an import statement and this ImportSpecifier clause did not have
- // 'as ImportedBinding' at the end of it, identifierName must be a BindingIdentifier.
- Error(ERRsyntax);
- }
- if (m_token.tk == tkComma)
- {
- // Consume a trailing comma
- m_pscan->Scan();
- }
- if (buildAST)
- {
- // The name we will use 'as' this import/export is a binding identifier in import statements.
- if (!isExportClause)
- {
- CreateModuleImportDeclNode(identifierAs);
- AddModuleImportOrExportEntry(importOrExportEntryList, identifierName, identifierAs, nullptr, nullptr);
- }
- else
- {
- MarkIdentifierReferenceIsModuleExport(identifierName);
- AddModuleImportOrExportEntry(importOrExportEntryList, nullptr, identifierName, identifierAs, nullptr);
- }
- }
- }
- // Final token in a named import or export clause must be a '}'
- ChkCurTokNoScan(tkRCurly, ERRsyntax);
- }
- IdentPtrList* Parser::GetRequestedModulesList()
- {
- return m_currentNodeProg->sxModule.requestedModules;
- }
- ModuleImportOrExportEntryList* Parser::GetModuleImportEntryList()
- {
- return m_currentNodeProg->sxModule.importEntries;
- }
- ModuleImportOrExportEntryList* Parser::GetModuleLocalExportEntryList()
- {
- return m_currentNodeProg->sxModule.localExportEntries;
- }
- ModuleImportOrExportEntryList* Parser::GetModuleIndirectExportEntryList()
- {
- return m_currentNodeProg->sxModule.indirectExportEntries;
- }
- ModuleImportOrExportEntryList* Parser::GetModuleStarExportEntryList()
- {
- return m_currentNodeProg->sxModule.starExportEntries;
- }
- IdentPtrList* Parser::EnsureRequestedModulesList()
- {
- if (m_currentNodeProg->sxModule.requestedModules == nullptr)
- {
- m_currentNodeProg->sxModule.requestedModules = Anew(&m_nodeAllocator, IdentPtrList, &m_nodeAllocator);
- }
- return m_currentNodeProg->sxModule.requestedModules;
- }
- ModuleImportOrExportEntryList* Parser::EnsureModuleImportEntryList()
- {
- if (m_currentNodeProg->sxModule.importEntries == nullptr)
- {
- m_currentNodeProg->sxModule.importEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
- }
- return m_currentNodeProg->sxModule.importEntries;
- }
- ModuleImportOrExportEntryList* Parser::EnsureModuleLocalExportEntryList()
- {
- if (m_currentNodeProg->sxModule.localExportEntries == nullptr)
- {
- m_currentNodeProg->sxModule.localExportEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
- }
- return m_currentNodeProg->sxModule.localExportEntries;
- }
- ModuleImportOrExportEntryList* Parser::EnsureModuleIndirectExportEntryList()
- {
- if (m_currentNodeProg->sxModule.indirectExportEntries == nullptr)
- {
- m_currentNodeProg->sxModule.indirectExportEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
- }
- return m_currentNodeProg->sxModule.indirectExportEntries;
- }
- ModuleImportOrExportEntryList* Parser::EnsureModuleStarExportEntryList()
- {
- if (m_currentNodeProg->sxModule.starExportEntries == nullptr)
- {
- m_currentNodeProg->sxModule.starExportEntries = Anew(&m_nodeAllocator, ModuleImportOrExportEntryList, &m_nodeAllocator);
- }
- return m_currentNodeProg->sxModule.starExportEntries;
- }
- void Parser::AddModuleSpecifier(IdentPtr moduleRequest)
- {
- IdentPtrList* requestedModulesList = EnsureRequestedModulesList();
- if (!requestedModulesList->Has(moduleRequest))
- {
- requestedModulesList->Prepend(moduleRequest);
- }
- }
- ModuleImportOrExportEntry* Parser::AddModuleImportOrExportEntry(ModuleImportOrExportEntryList* importOrExportEntryList, ModuleImportOrExportEntry* importOrExportEntry)
- {
- if (importOrExportEntry->exportName != nullptr)
- {
- CheckForDuplicateExportEntry(importOrExportEntryList, importOrExportEntry->exportName);
- }
- importOrExportEntryList->Prepend(*importOrExportEntry);
- return importOrExportEntry;
- }
- ModuleImportOrExportEntry* Parser::AddModuleImportOrExportEntry(ModuleImportOrExportEntryList* importOrExportEntryList, IdentPtr importName, IdentPtr localName, IdentPtr exportName, IdentPtr moduleRequest)
- {
- ModuleImportOrExportEntry* importOrExportEntry = Anew(&m_nodeAllocator, ModuleImportOrExportEntry);
- importOrExportEntry->importName = importName;
- importOrExportEntry->localName = localName;
- importOrExportEntry->exportName = exportName;
- importOrExportEntry->moduleRequest = moduleRequest;
- return AddModuleImportOrExportEntry(importOrExportEntryList, importOrExportEntry);
- }
- void Parser::AddModuleLocalExportEntry(ParseNodePtr varDeclNode)
- {
- Assert(varDeclNode->nop == knopVarDecl || varDeclNode->nop == knopLetDecl || varDeclNode->nop == knopConstDecl);
- IdentPtr localName = varDeclNode->sxVar.pid;
- varDeclNode->sxVar.sym->SetIsModuleExportStorage(true);
- AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), nullptr, localName, localName, nullptr);
- }
- void Parser::CheckForDuplicateExportEntry(ModuleImportOrExportEntryList* exportEntryList, IdentPtr exportName)
- {
- ModuleImportOrExportEntry* findResult = exportEntryList->Find([&](ModuleImportOrExportEntry exportEntry)
- {
- if (exportName == exportEntry.exportName)
- {
- return true;
- }
- return false;
- });
- if (findResult != nullptr)
- {
- Error(ERRsyntax);
- }
- }
- template<bool buildAST>
- void Parser::ParseImportClause(ModuleImportOrExportEntryList* importEntryList, bool parsingAfterComma)
- {
- bool parsedNamespaceOrNamedImport = false;
- switch (m_token.tk)
- {
- case tkID:
- // This is the default binding identifier.
- // If we already saw a comma in the import clause, this is a syntax error.
- if (parsingAfterComma)
- {
- Error(ERRsyntax);
- }
- if (buildAST)
- {
- IdentPtr localName = m_token.GetIdentifier(m_phtbl);
- IdentPtr importName = wellKnownPropertyPids._default;
- CreateModuleImportDeclNode(localName);
- AddModuleImportOrExportEntry(importEntryList, importName, localName, nullptr, nullptr);
- }
- break;
- case tkLCurly:
- // This begins a list of named imports.
- ParseNamedImportOrExportClause<buildAST>(importEntryList, false);
- parsedNamespaceOrNamedImport = true;
- break;
- case tkStar:
- // This begins a namespace import clause.
- // "* as ImportedBinding"
- // Token following * must be the identifier 'as'
- m_pscan->Scan();
- if (m_token.tk != tkID || wellKnownPropertyPids.as != m_token.GetIdentifier(m_phtbl))
- {
- Error(ERRsyntax);
- }
- // Token following 'as' must be a binding identifier.
- m_pscan->Scan();
- ChkCurTokNoScan(tkID, ERRsyntax);
- if (buildAST)
- {
- IdentPtr localName = m_token.GetIdentifier(m_phtbl);
- IdentPtr importName = wellKnownPropertyPids._star;
- CreateModuleImportDeclNode(localName);
- AddModuleImportOrExportEntry(importEntryList, importName, localName, nullptr, nullptr);
- }
- parsedNamespaceOrNamedImport = true;
- break;
- default:
- Error(ERRsyntax);
- }
- m_pscan->Scan();
- if (m_token.tk == tkComma)
- {
- // There cannot be more than one comma in a module import clause.
- // There cannot be a namespace import or named imports list on the left of the comma in a module import clause.
- if (parsingAfterComma || parsedNamespaceOrNamedImport)
- {
- Error(ERRsyntax);
- }
- m_pscan->Scan();
- ParseImportClause<buildAST>(importEntryList, true);
- }
- }
- bool Parser::IsImportOrExportStatementValidHere()
- {
- // Import must be located in the global scope of the module.
- return GetCurrentFunctionNode()->nop == knopProg
- && (this->m_grfscr & fscrEvalCode) != fscrEvalCode
- && this->m_tryCatchOrFinallyDepth == 0
- && this->m_currentBlockInfo->pBlockInfoOuter == nullptr;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseImportDeclaration()
- {
- Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
- Assert(m_token.tk == tkIMPORT);
- if (!IsImportOrExportStatementValidHere())
- {
- Error(ERRInvalidModuleImportOrExport);
- }
- // We just parsed an import token. Next valid token is *, {, string constant, or binding identifier.
- m_pscan->Scan();
- if (m_token.tk == tkStrCon)
- {
- // This import declaration has no import clause.
- // "import ModuleSpecifier;"
- if (buildAST)
- {
- AddModuleSpecifier(m_token.GetStr());
- }
- // Scan past the module identifier.
- m_pscan->Scan();
- }
- else
- {
- ModuleImportOrExportEntryList importEntryList(&m_nodeAllocator);
- // Parse the import clause (default binding can only exist before the comma).
- ParseImportClause<buildAST>(&importEntryList);
- // Token following import clause must be the identifier 'from'
- IdentPtr moduleSpecifier = ParseImportOrExportFromClause<buildAST>(true);
- if (buildAST)
- {
- Assert(moduleSpecifier != nullptr);
- AddModuleSpecifier(moduleSpecifier);
- importEntryList.Map([this, moduleSpecifier](ModuleImportOrExportEntry& importEntry) {
- importEntry.moduleRequest = moduleSpecifier;
- AddModuleImportOrExportEntry(EnsureModuleImportEntryList(), &importEntry);
- });
- }
- importEntryList.Clear();
- }
- // Import statement is actually a nop, we hoist all the imported bindings to the top of the module.
- return nullptr;
- }
- template<bool buildAST>
- IdentPtr Parser::ParseImportOrExportFromClause(bool throwIfNotFound)
- {
- IdentPtr moduleSpecifier = nullptr;
- if (m_token.tk == tkID && wellKnownPropertyPids.from == m_token.GetIdentifier(m_phtbl))
- {
- m_pscan->Scan();
- // Token following the 'from' token must be a string constant - the module specifier.
- ChkCurTokNoScan(tkStrCon, ERRsyntax);
- if (buildAST)
- {
- moduleSpecifier = m_token.GetStr();
- }
- m_pscan->Scan();
- }
- else if (throwIfNotFound)
- {
- Error(ERRsyntax);
- }
- return moduleSpecifier;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseDefaultExportClause()
- {
- Assert(m_token.tk == tkDEFAULT);
- m_pscan->Scan();
- ParseNodePtr pnode = nullptr;
- ushort flags = fFncNoFlgs;
- switch (m_token.tk)
- {
- case tkCLASS:
- {
- if (!m_scriptContext->GetConfig()->IsES6ClassAndExtendsEnabled())
- {
- goto LDefault;
- }
- // Before we parse the class itself we need to know if the class has an identifier name.
- // If it does, we'll treat this class as an ordinary class declaration which will bind
- // it to that name. Otherwise the class should parse as a nameless class expression and
- // bind only to the export binding.
- BOOL classHasName = false;
- RestorePoint parsedClass;
- m_pscan->Capture(&parsedClass);
- m_pscan->Scan();
- if (m_token.tk == tkID)
- {
- classHasName = true;
- }
- m_pscan->SeekTo(parsedClass);
- pnode = ParseClassDecl<buildAST>(classHasName, nullptr, nullptr, nullptr);
- if (buildAST)
- {
- AnalysisAssert(pnode != nullptr);
- Assert(pnode->nop == knopClassDecl);
- pnode->sxClass.SetIsDefaultModuleExport(true);
- }
- break;
- }
- case tkID:
- // If we parsed an async token, it could either modify the next token (if it is a
- // function token) or it could be an identifier (let async = 0; export default async;).
- // To handle both cases, when we parse an async token we need to keep the parser state
- // and rewind if the next token is not function.
- if (wellKnownPropertyPids.async == m_token.GetIdentifier(m_phtbl))
- {
- RestorePoint parsedAsync;
- m_pscan->Capture(&parsedAsync);
- m_pscan->Scan();
- if (m_token.tk == tkFUNCTION)
- {
- // Token after async is function, consume the async token and continue to parse the
- // function as an async function.
- flags |= fFncAsync;
- goto LFunction;
- }
- // Token after async is not function, no idea what the async token is supposed to mean
- // so rewind and let the default case handle it.
- m_pscan->SeekTo(parsedAsync);
- }
- goto LDefault;
- break;
- case tkFUNCTION:
- {
- LFunction:
- // We just parsed a function token but we need to figure out if the function
- // has an identifier name or not before we call the helper.
- RestorePoint parsedFunction;
- m_pscan->Capture(&parsedFunction);
- m_pscan->Scan();
- if (m_token.tk == tkStar)
- {
- // If we saw 'function*' that indicates we are going to parse a generator,
- // but doesn't tell us if the generator has an identifier or not.
- // Skip the '*' token for now as it doesn't matter yet.
- m_pscan->Scan();
- }
- // We say that if the function has an identifier name, it is a 'normal' declaration
- // and should create a binding to that identifier as well as one for our default export.
- if (m_token.tk == tkID)
- {
- flags |= fFncDeclaration;
- }
- else
- {
- flags |= fFncNoName;
- }
- // Rewind back to the function token and let the helper handle the parsing.
- m_pscan->SeekTo(parsedFunction);
- pnode = ParseFncDecl<buildAST>(flags);
- if (buildAST)
- {
- AnalysisAssert(pnode != nullptr);
- Assert(pnode->nop == knopFncDecl);
- pnode->sxFnc.SetIsDefaultModuleExport(true);
- }
- break;
- }
- default:
- LDefault:
- {
- ParseNodePtr pnodeExpression = ParseExpr<buildAST>();
- // Consider: Can we detect this syntax error earlier?
- if (pnodeExpression && pnodeExpression->nop == knopComma)
- {
- Error(ERRsyntax);
- }
- if (buildAST)
- {
- AnalysisAssert(pnodeExpression != nullptr);
- // Mark this node as the default module export. We need to make sure it is put into the correct
- // module export slot when we emit the node.
- pnode = CreateNode(knopExportDefault);
- pnode->sxExportDefault.pnodeExpr = pnodeExpression;
- }
- break;
- }
- }
- IdentPtr exportName = wellKnownPropertyPids._default;
- IdentPtr localName = wellKnownPropertyPids._starDefaultStar;
- AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), nullptr, localName, exportName, nullptr);
- return pnode;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseExportDeclaration()
- {
- Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
- Assert(m_token.tk == tkEXPORT);
- if (!IsImportOrExportStatementValidHere())
- {
- Error(ERRInvalidModuleImportOrExport);
- }
- ParseNodePtr pnode = nullptr;
- IdentPtr moduleIdentifier = nullptr;
- tokens declarationType;
- // We just parsed an export token. Next valid tokens are *, {, var, let, const, async, function, class, default.
- m_pscan->Scan();
- switch (m_token.tk)
- {
- case tkStar:
- m_pscan->Scan();
- // A star token in an export declaration must be followed by a from clause which begins with a token 'from'.
- moduleIdentifier = ParseImportOrExportFromClause<buildAST>(true);
- if (buildAST)
- {
- Assert(moduleIdentifier != nullptr);
- AddModuleSpecifier(moduleIdentifier);
- IdentPtr importName = wellKnownPropertyPids._star;
- AddModuleImportOrExportEntry(EnsureModuleStarExportEntryList(), importName, nullptr, nullptr, moduleIdentifier);
- }
- break;
- case tkLCurly:
- {
- ModuleImportOrExportEntryList exportEntryList(&m_nodeAllocator);
- ParseNamedImportOrExportClause<buildAST>(&exportEntryList, true);
- m_pscan->Scan();
- // Export clause may be followed by a from clause.
- moduleIdentifier = ParseImportOrExportFromClause<buildAST>(false);
- if (buildAST)
- {
- if (moduleIdentifier != nullptr)
- {
- AddModuleSpecifier(moduleIdentifier);
- }
- exportEntryList.Map([this, moduleIdentifier](ModuleImportOrExportEntry& exportEntry) {
- if (moduleIdentifier != nullptr)
- {
- exportEntry.moduleRequest = moduleIdentifier;
- // We need to swap localname and importname when this is a re-export.
- exportEntry.importName = exportEntry.localName;
- exportEntry.localName = nullptr;
- AddModuleImportOrExportEntry(EnsureModuleIndirectExportEntryList(), &exportEntry);
- }
- else
- {
- AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), &exportEntry);
- }
- });
- exportEntryList.Clear();
- }
- }
- break;
- case tkID:
- {
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- if (wellKnownPropertyPids.let == pid)
- {
- declarationType = tkLET;
- goto ParseVarDecl;
- }
- if (wellKnownPropertyPids.async == pid && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
- {
- // In module export statements, async token is only valid if it's followed by function.
- // We need to check here because ParseStatement would think 'async = 20' is a var decl.
- RestorePoint parsedAsync;
- m_pscan->Capture(&parsedAsync);
- m_pscan->Scan();
- if (m_token.tk == tkFUNCTION)
- {
- // Token after async is function, rewind to the async token and let ParseStatement handle it.
- m_pscan->SeekTo(parsedAsync);
- goto ParseFunctionDecl;
- }
- // Token after async is not function, it's a syntax error.
- }
- goto ErrorToken;
- }
- case tkVAR:
- case tkLET:
- case tkCONST:
- {
- declarationType = m_token.tk;
- ParseVarDecl:
- m_pscan->Scan();
- pnode = ParseVariableDeclaration<buildAST>(declarationType, m_pscan->IchMinTok());
- if (buildAST)
- {
- ParseNodePtr temp = pnode;
- while (temp->nop == knopList)
- {
- ParseNodePtr varDeclNode = temp->sxBin.pnode1;
- temp = temp->sxBin.pnode2;
- AddModuleLocalExportEntry(varDeclNode);
- }
- AddModuleLocalExportEntry(temp);
- }
- }
- break;
- case tkFUNCTION:
- case tkCLASS:
- {
- ParseFunctionDecl:
- pnode = ParseStatement<buildAST>();
- if (buildAST)
- {
- IdentPtr localName;
- if (pnode->nop == knopClassDecl)
- {
- pnode->sxClass.pnodeName->sxVar.sym->SetIsModuleExportStorage(true);
- pnode->sxClass.pnodeDeclName->sxVar.sym->SetIsModuleExportStorage(true);
- localName = pnode->sxClass.pnodeName->sxVar.pid;
- }
- else
- {
- Assert(pnode->nop == knopFncDecl);
- pnode->sxFnc.GetFuncSymbol()->SetIsModuleExportStorage(true);
- localName = pnode->sxFnc.pid;
- }
- Assert(localName != nullptr);
- AddModuleImportOrExportEntry(EnsureModuleLocalExportEntryList(), nullptr, localName, localName, nullptr);
- }
- }
- break;
- case tkDEFAULT:
- {
- pnode = ParseDefaultExportClause<buildAST>();
- }
- break;
- default:
- {
- ErrorToken:
- Error(ERRsyntax);
- }
- }
- return pnode;
- }
- /***************************************************************************
- Parse an expression term.
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseTerm(BOOL fAllowCall,
- LPCOLESTR pNameHint,
- ulong *pHintLength,
- ulong *pShortNameOffset,
- _Inout_opt_ IdentToken* pToken /*= nullptr*/,
- bool fUnaryOrParen /*= false*/,
- _Out_opt_ BOOL* pfCanAssign /*= nullptr*/,
- _Inout_opt_ BOOL* pfLikelyPattern /*= nullptr*/)
- {
- ParseNodePtr pnode = nullptr;
- charcount_t ichMin = 0;
- size_t iecpMin = 0;
- size_t iuMin;
- IdentToken term;
- BOOL fInNew = FALSE;
- BOOL fCanAssign = TRUE;
- bool isAsyncExpr = false;
- bool isLambdaExpr = false;
- Assert(pToken == nullptr || pToken->tk == tkNone); // Must be empty initially
- if (this->IsBackgroundParser())
- {
- PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackParseOneTerm);
- }
- else
- {
- PROBE_STACK(m_scriptContext, Js::Constants::MinStackParseOneTerm);
- }
- switch (m_token.tk)
- {
- case tkID:
- {
- PidRefStack *ref = nullptr;
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- charcount_t ichLim = m_pscan->IchLimTok();
- size_t iecpLim = m_pscan->IecpLimTok();
- ichMin = m_pscan->IchMinTok();
- iecpMin = m_pscan->IecpMinTok();
- m_pscan->Scan();
- // We search for an Async expression (a function declaration or an async lambda expression)
- if (pid == wellKnownPropertyPids.async &&
- !m_pscan->FHadNewLine() &&
- m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
- {
- if (m_token.tk == tkFUNCTION)
- {
- isAsyncExpr = true;
- goto LFunction;
- }
- else if (m_token.tk == tkID)
- {
- isLambdaExpr = true;
- isAsyncExpr = true;
- goto LFunction;
- }
- }
- ref = this->PushPidRef(pid);
- if (buildAST)
- {
- pnode = CreateNameNode(pid);
- pnode->ichMin = ichMin;
- pnode->ichLim = ichLim;
- pnode->sxPid.SetSymRef(ref);
- CheckArgumentsUse(pid, m_currentNodeFunc);
- }
- else
- {
- // Remember the identifier start and end in case it turns out to be a statement label.
- term.tk = tkID;
- term.pid = pid; // Record the identifier for detection of eval
- term.ichMin = static_cast<charcount_t>(iecpMin);
- term.ichLim = static_cast<charcount_t>(iecpLim);
- }
- break;
- }
- case tkTHIS:
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopThis>();
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkLParen:
- ichMin = m_pscan->IchMinTok();
- iuMin = m_pscan->IecpMinTok();
- m_pscan->Scan();
- if (m_token.tk == tkRParen)
- {
- // Empty parens can only be legal as an empty parameter list to a lambda declaration.
- // We're in a lambda if the next token is =>.
- fAllowCall = FALSE;
- m_pscan->Scan();
- // If the token after the right paren is not => or if there was a newline between () and => this is a syntax error
- if (!m_doingFastScan && (m_token.tk != tkDArrow || m_pscan->FHadNewLine()))
- {
- Error(ERRsyntax);
- }
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopEmpty>();
- }
- break;
- }
- this->m_parenDepth++;
- pnode = ParseExpr<buildAST>(koplNo, &fCanAssign, TRUE, FALSE, nullptr, nullptr /*nameLength*/, nullptr /*pShortNameOffset*/, &term, true);
- this->m_parenDepth--;
- ChkCurTok(tkRParen, ERRnoRparen);
- // Emit a deferred ... error if one was parsed.
- if (m_deferEllipsisError && m_token.tk != tkDArrow)
- {
- m_pscan->SeekTo(m_EllipsisErrLoc);
- Error(ERRInvalidSpreadUse);
- }
- else
- {
- m_deferEllipsisError = false;
- }
- break;
- case tkIntCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- if (buildAST)
- {
- pnode = CreateIntNodeWithScanner(m_token.GetLong());
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkFltCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopFlt>();
- pnode->sxFlt.dbl = m_token.GetDouble();
- pnode->sxFlt.maybeInt = m_token.GetDoubleMayBeInt();
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkStrCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- if (buildAST)
- {
- pnode = CreateStrNodeWithScanner(m_token.GetStr());
- }
- else
- {
- // Subtract the string literal length from the total char count for the purpose
- // of deciding whether to defer parsing and byte code generation.
- this->ReduceDeferredScriptLength(m_pscan->IchLimTok() - m_pscan->IchMinTok());
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkTRUE:
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopTrue>();
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkFALSE:
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopFalse>();
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkNULL:
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopNull>();
- }
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkDiv:
- case tkAsgDiv:
- pnode = ParseRegExp<buildAST>();
- fCanAssign = FALSE;
- m_pscan->Scan();
- break;
- case tkNEW:
- {
- ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- if (m_token.tk == tkDot && m_scriptContext->GetConfig()->IsES6ClassAndExtendsEnabled())
- {
- pnode = ParseMetaProperty<buildAST>(tkNEW, ichMin, &fCanAssign);
- m_pscan->Scan();
- }
- else
- {
- ParseNodePtr pnodeExpr = ParseTerm<buildAST>(FALSE, pNameHint, pHintLength, pShortNameOffset);
- if (buildAST)
- {
- pnode = CreateCallNode(knopNew, pnodeExpr, nullptr);
- pnode->ichMin = ichMin;
- }
- fInNew = TRUE;
- fCanAssign = FALSE;
- }
- break;
- }
- case tkLBrack:
- {
- ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- pnode = ParseArrayLiteral<buildAST>();
- if (buildAST)
- {
- pnode->ichMin = ichMin;
- pnode->ichLim = m_pscan->IchLimTok();
- }
- if (this->m_arrayDepth == 0)
- {
- Assert(m_pscan->IchLimTok() - ichMin > m_funcInArray);
- this->ReduceDeferredScriptLength(m_pscan->IchLimTok() - ichMin - this->m_funcInArray);
- this->m_funcInArray = 0;
- this->m_funcInArrayDepth = 0;
- }
- ChkCurTok(tkRBrack, ERRnoRbrack);
- if (!IsES6DestructuringEnabled())
- {
- fCanAssign = FALSE;
- }
- else if (pfLikelyPattern != nullptr && !IsPostFixOperators())
- {
- *pfLikelyPattern = TRUE;
- }
- break;
- }
- case tkLCurly:
- {
- ichMin = m_pscan->IchMinTok();
- m_pscan->ScanForcingPid();
- ParseNodePtr pnodeMemberList = ParseMemberList<buildAST>(pNameHint, pHintLength);
- if (buildAST)
- {
- pnode = CreateUniNode(knopObject, pnodeMemberList);
- pnode->ichMin = ichMin;
- pnode->ichLim = m_pscan->IchLimTok();
- }
- ChkCurTok(tkRCurly, ERRnoRcurly);
- if (!IsES6DestructuringEnabled())
- {
- fCanAssign = FALSE;
- }
- else if (pfLikelyPattern != nullptr && !IsPostFixOperators())
- {
- *pfLikelyPattern = TRUE;
- }
- break;
- }
- case tkFUNCTION:
- {
- LFunction :
- if (m_grfscr & fscrDeferredFncExpression)
- {
- // The top-level deferred function body was defined by a function expression whose parsing was deferred. We are now
- // parsing it, so unset the flag so that any nested functions are parsed normally. This flag is only applicable the
- // first time we see it.
- //
- // Normally, deferred functions will be parsed in ParseStatement upon encountering the 'function' token. The first
- // token of the source code of the function may not a 'function' token though, so we still need to reset this flag
- // for the first function we parse. This can happen in compat modes, for instance, for a function expression enclosed
- // in parentheses, where the legacy behavior was to include the parentheses in the function's source code.
- m_grfscr &= ~fscrDeferredFncExpression;
- }
- ushort flags = fFncNoFlgs;
- if (isLambdaExpr)
- {
- flags |= fFncLambda;
- }
- if (isAsyncExpr)
- {
- flags |= fFncAsync;
- }
- pnode = ParseFncDecl<buildAST>(flags, pNameHint, false, true, fUnaryOrParen);
- if (isAsyncExpr)
- {
- pnode->sxFnc.cbMin = iecpMin;
- pnode->ichMin = ichMin;
- }
- fCanAssign = FALSE;
- break;
- }
- case tkCLASS:
- fAllowCall = FALSE;
- if (m_scriptContext->GetConfig()->IsES6ClassAndExtendsEnabled())
- {
- pnode = ParseClassDecl<buildAST>(FALSE, pNameHint, pHintLength, pShortNameOffset);
- }
- else
- {
- goto LUnknown;
- }
- fCanAssign = FALSE;
- break;
- case tkStrTmplBasic:
- case tkStrTmplBegin:
- pnode = ParseStringTemplateDecl<buildAST>(nullptr);
- fCanAssign = FALSE;
- break;
- case tkSUPER:
- if (m_scriptContext->GetConfig()->IsES6ClassAndExtendsEnabled())
- {
- pnode = ParseSuper<buildAST>(pnode, !!fAllowCall);
- }
- else
- {
- goto LUnknown;
- }
- break;
- case tkCASE:
- {
- if (!m_doingFastScan)
- {
- goto LUnknown;
- }
- ParseNodePtr pnodeUnused;
- pnode = ParseCase<buildAST>(&pnodeUnused);
- break;
- }
- case tkELSE:
- if (!m_doingFastScan)
- {
- goto LUnknown;
- }
- m_pscan->Scan();
- ParseStatement<buildAST>();
- break;
- default:
- LUnknown :
- Error(ERRsyntax);
- break;
- }
- pnode = ParsePostfixOperators<buildAST>(pnode, fAllowCall, fInNew, &fCanAssign, &term);
- // Pass back identifier if requested
- if (pToken && term.tk == tkID)
- {
- *pToken = term;
- }
- if (pfCanAssign)
- {
- *pfCanAssign = fCanAssign;
- }
- return pnode;
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseRegExp()
- {
- ParseNodePtr pnode = nullptr;
- if (buildAST || m_doingFastScan)
- {
- m_pscan->RescanRegExp();
- BOOL saveDeferringAST = this->m_deferringAST;
- if (m_doingFastScan)
- {
- this->m_deferringAST = false;
- }
- pnode = CreateNodeWithScanner<knopRegExp>();
- pnode->sxPid.regexPattern = m_token.GetRegex();
- if (m_doingFastScan)
- {
- this->m_deferringAST = saveDeferringAST;
- this->AddFastScannedRegExpNode(pnode);
- if (!buildAST)
- {
- pnode = nullptr;
- }
- }
- #if ENABLE_BACKGROUND_PARSING
- else if (this->IsBackgroundParser())
- {
- Assert(pnode->sxPid.regexPattern == nullptr);
- this->AddBackgroundRegExpNode(pnode);
- }
- #endif
- }
- else
- {
- m_pscan->RescanRegExpNoAST();
- }
- Assert(m_token.tk == tkRegExp);
- return pnode;
- }
- BOOL Parser::NodeIsEvalName(ParseNodePtr pnode)
- {
- //WOOB 1107758 Special case of indirect eval binds to local scope in standards mode
- return pnode->nop == knopName && (pnode->sxPid.pid == wellKnownPropertyPids.eval);
- }
- BOOL Parser::NodeEqualsName(ParseNodePtr pnode, LPCOLESTR sz, ulong cch)
- {
- return pnode->nop == knopName &&
- pnode->sxPid.pid->Cch() == cch &&
- !wmemcmp(pnode->sxPid.pid->Psz(), sz, cch);
- }
- BOOL Parser::NodeIsIdent(ParseNodePtr pnode, IdentPtr pid)
- {
- for (;;)
- {
- switch (pnode->nop)
- {
- case knopName:
- return (pnode->sxPid.pid == pid);
- case knopComma:
- pnode = pnode->sxBin.pnode2;
- break;
- default:
- return FALSE;
- }
- }
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParsePostfixOperators(
- ParseNodePtr pnode,
- BOOL fAllowCall,
- BOOL fInNew,
- BOOL *pfCanAssign,
- _Inout_ IdentToken* pToken)
- {
- uint16 count = 0;
- bool callOfConstants = false;
- for (;;)
- {
- uint16 spreadArgCount = 0;
- switch (m_token.tk)
- {
- case tkLParen:
- {
- if (fInNew)
- {
- ParseNodePtr pnodeArgs = ParseArgList<buildAST>(&callOfConstants, &spreadArgCount, &count);
- if (buildAST)
- {
- Assert(pnode->nop == knopNew);
- Assert(pnode->sxCall.pnodeArgs == nullptr);
- pnode->sxCall.pnodeArgs = pnodeArgs;
- pnode->sxCall.callOfConstants = callOfConstants;
- pnode->sxCall.isApplyCall = false;
- pnode->sxCall.isEvalCall = false;
- pnode->sxCall.argCount = count;
- pnode->sxCall.spreadArgCount = spreadArgCount;
- pnode->ichLim = m_pscan->IchLimTok();
- }
- else
- {
- pToken->tk = tkNone; // This is no longer an identifier
- }
- fInNew = FALSE;
- }
- else
- {
- bool fCallIsEval = false;
- if (!fAllowCall)
- {
- return pnode;
- }
- ParseNodePtr pnodeArgs = ParseArgList<buildAST>(&callOfConstants, &spreadArgCount, &count);
- // We used to un-defer a deferred function body here if it was called as part of the expression that declared it.
- // We now detect this case up front in ParseFncDecl, which is cheaper and simpler.
- if (buildAST)
- {
- pnode = CreateCallNode(knopCall, pnode, pnodeArgs);
- Assert(pnode);
- // Detect call to "eval" and record it on the function.
- // Note: we used to leave it up to the byte code generator to detect eval calls
- // at global scope, but now it relies on the flag the parser sets, so set it here.
- if (count > 0 && this->NodeIsEvalName(pnode->sxCall.pnodeTarget))
- {
- this->MarkEvalCaller();
- fCallIsEval = true;
- }
- pnode->sxCall.callOfConstants = callOfConstants;
- pnode->sxCall.spreadArgCount = spreadArgCount;
- pnode->sxCall.isApplyCall = false;
- pnode->sxCall.isEvalCall = fCallIsEval;
- pnode->sxCall.argCount = count;
- pnode->ichLim = m_pscan->IchLimTok();
- }
- else
- {
- if (pToken->tk == tkID && pToken->pid == wellKnownPropertyPids.eval && count > 0) // Detect eval
- {
- this->MarkEvalCaller();
- }
- pToken->tk = tkNone; // This is no longer an identifier
- }
- }
- ChkCurTok(tkRParen, ERRnoRparen);
- if (pfCanAssign)
- {
- *pfCanAssign = FALSE;
- }
- break;
- }
- case tkLBrack:
- {
- m_pscan->Scan();
- ParseNodePtr pnodeExpr = ParseExpr<buildAST>();
- if (buildAST)
- {
- pnode = CreateBinNode(knopIndex, pnode, pnodeExpr);
- pnode->ichLim = m_pscan->IchLimTok();
- }
- else
- {
- pToken->tk = tkNone; // This is no longer an identifier
- }
- ChkCurTok(tkRBrack, ERRnoRbrack);
- if (pfCanAssign)
- {
- *pfCanAssign = TRUE;
- }
- if (!buildAST)
- {
- break;
- }
- bool shouldConvertToDot = false;
- if (pnode->sxBin.pnode2->nop == knopStr)
- {
- // if the string is empty or contains escape character, we will not convert them to dot node
- shouldConvertToDot = pnode->sxBin.pnode2->sxPid.pid->Cch() > 0 && !m_pscan->IsEscapeOnLastTkStrCon();
- }
- if (shouldConvertToDot)
- {
- LPCOLESTR str = pnode->sxBin.pnode2->sxPid.pid->Psz();
- // See if we can convert o["p"] into o.p and o["0"] into o[0] since they're equivalent and the latter forms
- // are faster
- uint32 uintValue;
- if(Js::JavascriptOperators::TryConvertToUInt32(
- str,
- pnode->sxBin.pnode2->sxPid.pid->Cch(),
- &uintValue) &&
- !Js::TaggedInt::IsOverflow(uintValue)) // the optimization is not very useful if the number can't be represented as a TaggedInt
- {
- // No need to verify that uintValue != JavascriptArray::InvalidIndex since all nonnegative TaggedInts are valid indexes
- auto intNode = CreateIntNodeWithScanner(uintValue); // implicit conversion from uint32 to long
- pnode->sxBin.pnode2 = intNode;
- }
- // Field optimization (see GlobOpt::KillLiveElems) checks for value being a Number,
- // and since NaN/Infinity is a number it won't kill o.NaN/o.Infinity which would cause a problem
- // if we decide to hoist o.NaN/o.Infinity.
- // We need to keep o["NaN"] and o["+/-Infinity"] as array element access (we don't hoist that but we may hoist field access),
- // so no matter if it's killed by o[x] inside a loop, we make sure that we never hoist these.
- // We need to follow same logic for strings that convert to a floating point number.
- else
- {
- bool doConvertToProperty = false; // Convert a["x"] -> a.x.
- if (!Parser::IsNaNOrInfinityLiteral<true>(str))
- {
- const OLECHAR* terminalChar;
- double dbl = Js::NumberUtilities::StrToDbl(str, &terminalChar, m_scriptContext);
- bool convertsToFloat = !Js::NumberUtilities::IsNan(dbl);
- doConvertToProperty = !convertsToFloat;
- }
- if (doConvertToProperty)
- {
- pnode->sxBin.pnode2->nop = knopName;
- pnode->nop = knopDot;
- pnode->grfpn |= PNodeFlags::fpnIndexOperator;
- }
- }
- }
- }
- break;
- case tkDot:
- {
- ParseNodePtr name = nullptr;
- OpCode opCode = knopDot;
- m_pscan->Scan();
- if (!m_token.IsIdentifier())
- {
- //allow reserved words in ES5 mode
- if (!(m_token.IsReservedWord()))
- {
- IdentifierExpectedError(m_token);
- }
- }
- // Note: see comment above about field optimization WRT NaN/Infinity/-Infinity.
- // Convert a.Nan, a.Infinity into a["NaN"], a["Infinity"].
- // We don't care about -Infinity case here because x.-Infinity is invalid in JavaScript.
- // Both NaN and Infinity are identifiers.
- else if (buildAST && Parser::IsNaNOrInfinityLiteral<false>(m_token.GetIdentifier(m_phtbl)->Psz()))
- {
- opCode = knopIndex;
- }
- if (buildAST)
- {
- if (opCode == knopDot)
- {
- name = CreateNameNode(m_token.GetIdentifier(m_phtbl));
- }
- else
- {
- Assert(opCode == knopIndex);
- name = CreateStrNodeWithScanner(m_token.GetIdentifier(m_phtbl));
- }
- pnode = CreateBinNode(opCode, pnode, name);
- }
- else
- {
- pToken->tk = tkNone;
- }
- if (pfCanAssign)
- {
- *pfCanAssign = TRUE;
- }
- m_pscan->Scan();
- break;
- }
- case tkStrTmplBasic:
- case tkStrTmplBegin:
- {
- ParseNode* templateNode = ParseStringTemplateDecl<buildAST>(pnode);
- if (!buildAST)
- {
- pToken->tk = tkNone; // This is no longer an identifier
- }
- pnode = templateNode;
- if (pfCanAssign)
- {
- *pfCanAssign = FALSE;
- }
- break;
- }
- default:
- return pnode;
- }
- }
- }
- /***************************************************************************
- Look for an existing label with the given name.
- ***************************************************************************/
- ParseNodePtr Parser::PnodeLabel(IdentPtr pid, ParseNodePtr pnodeLabels)
- {
- AssertMem(pid);
- AssertNodeMemN(pnodeLabels);
- StmtNest *pstmt;
- ParseNodePtr pnodeT;
- // Look in the statement stack.
- for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
- {
- AssertNodeMem(pstmt->pnodeStmt);
- AssertNodeMemN(pstmt->pnodeLab);
- for (pnodeT = pstmt->pnodeLab; nullptr != pnodeT;
- pnodeT = pnodeT->sxLabel.pnodeNext)
- {
- Assert(knopLabel == pnodeT->nop);
- if (pid == pnodeT->sxLabel.pid)
- return pnodeT;
- }
- }
- // Also look in the pnodeLabels list.
- for (pnodeT = pnodeLabels; nullptr != pnodeT;
- pnodeT = pnodeT->sxLabel.pnodeNext)
- {
- Assert(knopLabel == pnodeT->nop);
- if (pid == pnodeT->sxLabel.pid)
- return pnodeT;
- }
- return nullptr;
- }
- // Currently only ints and floats are treated as constants in function call
- // TODO: Check if we need for other constants as well
- BOOL Parser::IsConstantInFunctionCall(ParseNodePtr pnode)
- {
- if (pnode->nop == knopInt && !Js::TaggedInt::IsOverflow(pnode->sxInt.lw))
- {
- return TRUE;
- }
- if (pnode->nop == knopFlt)
- {
- return TRUE;
- }
- return FALSE;
- }
- /***************************************************************************
- Parse a list of arguments.
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseArgList( bool *pCallOfConstants, uint16 *pSpreadArgCount, uint16 * pCount)
- {
- ParseNodePtr pnodeArg;
- ParseNodePtr pnodeList = nullptr;
- ParseNodePtr *lastNodeRef = nullptr;
- // Check for an empty list
- Assert(m_token.tk == tkLParen);
- if (m_pscan->Scan() == tkRParen)
- {
- return nullptr;
- }
- *pCallOfConstants = true;
- *pSpreadArgCount = 0;
- int count=0;
- while (true)
- {
- // the count of arguments has to fit in an unsigned short
- if (count > 0xffffU)
- Error(ERRnoMemory);
- // Allow spread in argument lists.
- pnodeArg = ParseExpr<buildAST>(koplCma, nullptr, TRUE, /* fAllowEllipsis */TRUE);
- ++count;
- if (buildAST)
- {
- this->CheckArguments(pnodeArg);
- if (*pCallOfConstants && !IsConstantInFunctionCall(pnodeArg))
- {
- *pCallOfConstants = false;
- }
- if (pnodeArg->nop == knopEllipsis)
- {
- (*pSpreadArgCount)++;
- }
- AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeArg);
- }
- if (m_token.tk != tkComma)
- {
- break;
- }
- m_pscan->Scan();
- if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
- {
- break;
- }
- }
- if (pSpreadArgCount!=nullptr && (*pSpreadArgCount) > 0){
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(SpreadFeatureCount, m_scriptContext);
- }
- *pCount = static_cast<uint16>(count);
- if (buildAST)
- {
- AssertMem(lastNodeRef);
- AssertNodeMem(*lastNodeRef);
- pnodeList->ichLim = (*lastNodeRef)->ichLim;
- }
- return pnodeList;
- }
- // Currently only ints are treated as constants in ArrayLiterals
- BOOL Parser::IsConstantInArrayLiteral(ParseNodePtr pnode)
- {
- if (pnode->nop == knopInt && !Js::TaggedInt::IsOverflow(pnode->sxInt.lw))
- {
- return TRUE;
- }
- return FALSE;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseArrayLiteral()
- {
- ParseNodePtr pnode = nullptr;
- bool arrayOfTaggedInts = false;
- bool arrayOfInts = false;
- bool arrayOfNumbers = false;
- bool hasMissingValues = false;
- uint count = 0;
- uint spreadCount = 0;
- ParseNodePtr pnode1 = ParseArrayList<buildAST>(&arrayOfTaggedInts, &arrayOfInts, &arrayOfNumbers, &hasMissingValues, &count, &spreadCount);
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopArray>();
- pnode->sxArrLit.pnode1 = pnode1;
- pnode->sxArrLit.arrayOfTaggedInts = arrayOfTaggedInts;
- pnode->sxArrLit.arrayOfInts = arrayOfInts;
- pnode->sxArrLit.arrayOfNumbers = arrayOfNumbers;
- pnode->sxArrLit.hasMissingValues = hasMissingValues;
- pnode->sxArrLit.count = count;
- pnode->sxArrLit.spreadCount = spreadCount;
- if (pnode->sxArrLit.pnode1)
- {
- this->CheckArguments(pnode->sxArrLit.pnode1);
- }
- }
- return pnode;
- }
- /***************************************************************************
- Create an ArrayLiteral node
- Parse a list of array elements. [ a, b, , c, ]
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseArrayList(bool *pArrayOfTaggedInts, bool *pArrayOfInts, bool *pArrayOfNumbers, bool *pHasMissingValues, uint *count, uint *spreadCount)
- {
- ParseNodePtr pnodeArg = nullptr;
- ParseNodePtr pnodeList = nullptr;
- ParseNodePtr *lastNodeRef = nullptr;
- *count = 0;
- // Check for an empty list
- if (tkRBrack == m_token.tk)
- {
- return nullptr;
- }
- this->m_arrayDepth++;
- bool arrayOfTaggedInts = buildAST;
- bool arrayOfInts = buildAST;
- bool arrayOfNumbers = buildAST;
- bool arrayOfVarInts = false;
- bool hasMissingValues = false;
- for (;;)
- {
- (*count)++;
- if (tkComma == m_token.tk || tkRBrack == m_token.tk)
- {
- hasMissingValues = true;
- arrayOfTaggedInts = false;
- arrayOfInts = false;
- arrayOfNumbers = false;
- if (buildAST)
- {
- pnodeArg = CreateNodeWithScanner<knopEmpty>();
- }
- }
- else
- {
- // Allow Spread in array literals.
- pnodeArg = ParseExpr<buildAST>(koplCma, nullptr, TRUE, /* fAllowEllipsis */ TRUE);
- if (buildAST)
- {
- if (pnodeArg->nop == knopEllipsis)
- {
- (*spreadCount)++;
- }
- this->CheckArguments(pnodeArg);
- }
- }
- #if DEBUG
- if(m_grfscr & fscrEnforceJSON && !IsJSONValid(pnodeArg))
- {
- Error(ERRsyntax);
- }
- #endif
- if (buildAST)
- {
- if (arrayOfNumbers)
- {
- if (pnodeArg->nop != knopInt)
- {
- arrayOfTaggedInts = false;
- if (pnodeArg->nop != knopFlt)
- {
- // Not an array of constants.
- arrayOfInts = false;
- arrayOfNumbers = false;
- }
- else if (arrayOfInts && Js::JavascriptNumber::IsInt32OrUInt32(pnodeArg->sxFlt.dbl) && (!Js::JavascriptNumber::IsInt32(pnodeArg->sxFlt.dbl) || pnodeArg->sxFlt.dbl == -2147483648.0))
- {
- // We've seen nothing but ints, and this is a uint32 but not an int32.
- // Unless we see an actual float at some point, we want an array of vars
- // so we can work with tagged ints.
- arrayOfVarInts = true;
- }
- else
- {
- // Not an int array, but it may still be a float array.
- arrayOfInts = false;
- }
- }
- else
- {
- if (Js::SparseArraySegment<int32>::IsMissingItem((int32*)&pnodeArg->sxInt.lw))
- {
- arrayOfInts = false;
- }
- if (Js::TaggedInt::IsOverflow(pnodeArg->sxInt.lw))
- {
- arrayOfTaggedInts = false;
- }
- }
- }
- AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeArg);
- }
- if (tkComma != m_token.tk)
- {
- break;
- }
- m_pscan->Scan();
- if (tkRBrack == m_token.tk)
- {
- break;
- }
- }
- if (spreadCount != nullptr && *spreadCount > 0){
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(SpreadFeatureCount, m_scriptContext);
- }
- if (buildAST)
- {
- AssertMem(lastNodeRef);
- AssertNodeMem(*lastNodeRef);
- pnodeList->ichLim = (*lastNodeRef)->ichLim;
- if (arrayOfVarInts && arrayOfInts)
- {
- arrayOfInts = false;
- arrayOfNumbers = false;
- }
- *pArrayOfTaggedInts = arrayOfTaggedInts;
- *pArrayOfInts = arrayOfInts;
- *pArrayOfNumbers = arrayOfNumbers;
- *pHasMissingValues = hasMissingValues;
- }
- this->m_arrayDepth--;
- return pnodeList;
- }
- Parser::MemberNameToTypeMap* Parser::CreateMemberNameMap(ArenaAllocator* pAllocator)
- {
- Assert(pAllocator);
- return Anew(pAllocator, MemberNameToTypeMap, pAllocator, 5);
- }
- template<bool buildAST> void Parser::ParseComputedName(ParseNodePtr* ppnodeName, LPCOLESTR* ppNameHint, LPCOLESTR* ppFullNameHint, ulong *pNameLength, ulong *pShortNameOffset)
- {
- m_pscan->Scan();
- ParseNodePtr pnodeNameExpr = ParseExpr<buildAST>(koplCma, nullptr, TRUE, FALSE, *ppNameHint, pNameLength, pShortNameOffset);
- if (buildAST)
- {
- *ppnodeName = CreateNodeT<knopComputedName>(pnodeNameExpr->ichMin, pnodeNameExpr->ichLim);
- (*ppnodeName)->sxUni.pnode1 = pnodeNameExpr;
- }
- if (ppFullNameHint && buildAST && CONFIG_FLAG(UseFullName))
- {
- *ppFullNameHint = FormatPropertyString(*ppNameHint, pnodeNameExpr, pNameLength, pShortNameOffset);
- }
- ChkCurTokNoScan(tkRBrack, ERRnoRbrack);
- }
- /***************************************************************************
- Parse a list of object set/get members, e.g.:
- { get foo(){ ... }, set bar(arg) { ... } }
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseMemberGetSet(OpCode nop, LPCOLESTR* ppNameHint)
- {
- ParseNodePtr pnodeName = nullptr;
- Assert(nop == knopGetMember || nop == knopSetMember);
- AssertMem(ppNameHint);
- IdentPtr pid = nullptr;
- bool isComputedName = false;
- *ppNameHint=nullptr;
- switch(m_token.tk)
- {
- default:
- if (!m_token.IsReservedWord())
- {
- Error(ERRnoMemberIdent);
- }
- // fall through
- case tkID:
- pid = m_token.GetIdentifier(m_phtbl);
- *ppNameHint = pid->Psz();
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pid);
- }
- break;
- case tkStrCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- pid = m_token.GetStr();
- *ppNameHint = pid->Psz();
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pid);
- }
- break;
- case tkIntCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- pid = m_pscan->PidFromLong(m_token.GetLong());
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pid);
- }
- break;
- case tkFltCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- pid = m_pscan->PidFromDbl(m_token.GetDouble());
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pid);
- }
- break;
- case tkLBrack:
- // Computed property name: get|set [expr] () { }
- if (!m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
- {
- Error(ERRnoMemberIdent);
- }
- LPCOLESTR emptyHint = nullptr;
- ulong offset = 0;
- ParseComputedName<buildAST>(&pnodeName, &emptyHint, ppNameHint, &offset);
- isComputedName = true;
- break;
- }
- MemberType memberType;
- ushort flags = fFncMethod | fFncNoName;
- if (nop == knopGetMember)
- {
- memberType = MemberTypeGetter;
- flags |= fFncNoArg;
- }
- else
- {
- Assert(nop == knopSetMember);
- memberType = MemberTypeSetter;
- flags |= fFncOneArg;
- }
- this->m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperPropertyAllowed;
- ParseNodePtr pnodeFnc = ParseFncDecl<buildAST>(flags, *ppNameHint,
- /*needsPIDOnRCurlyScan*/ false, /*resetParsingSuperRestrictionState*/ false);
- if (buildAST)
- {
- pnodeFnc->sxFnc.SetIsAccessor();
- return CreateBinNode(nop, pnodeName, pnodeFnc);
- }
- else
- {
- return nullptr;
- }
- }
- /***************************************************************************
- Parse a list of object members. e.g. { x:foo, 'y me':bar }
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseMemberList(LPCOLESTR pNameHint, ulong* pNameHintLength, tokens declarationType)
- {
- ParseNodePtr pnodeArg = nullptr;
- ParseNodePtr pnodeName = nullptr;
- ParseNodePtr pnodeList = nullptr;
- ParseNodePtr *lastNodeRef = nullptr;
- LPCOLESTR pFullNameHint = nullptr; // A calculated full name
- ulong fullNameHintLength = pNameHintLength ? *pNameHintLength : 0;
- ulong shortNameOffset = 0;
- bool isProtoDeclared = false;
- // we get declaration tkLCurly - when the possible object pattern found under the expression.
- bool isObjectPattern = (declarationType == tkVAR || declarationType == tkLET || declarationType == tkCONST || declarationType == tkLCurly) && IsES6DestructuringEnabled();
- // Check for an empty list
- if (tkRCurly == m_token.tk)
- {
- return nullptr;
- }
- ArenaAllocator tempAllocator(_u("MemberNames"), m_nodeAllocator.GetPageAllocator(), Parser::OutOfMemory);
- bool hasDeferredInitError = false;
- for (;;)
- {
- bool isComputedName = false;
- #if DEBUG
- if((m_grfscr & fscrEnforceJSON) && (tkStrCon != m_token.tk || !(m_pscan->IsDoubleQuoteOnLastTkStrCon())))
- {
- Error(ERRsyntax);
- }
- #endif
- bool isAsyncMethod = false;
- charcount_t ichMin = 0;
- size_t iecpMin = 0;
- if (m_token.tk == tkID && m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.async && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
- {
- RestorePoint parsedAsync;
- m_pscan->Capture(&parsedAsync);
- ichMin = m_pscan->IchMinTok();
- iecpMin = m_pscan->IecpMinTok();
- m_pscan->ScanForcingPid();
- if (m_token.tk == tkLParen || m_token.tk == tkColon || m_token.tk == tkRCurly || m_pscan->FHadNewLine())
- {
- m_pscan->SeekTo(parsedAsync);
- }
- else
- {
- isAsyncMethod = true;
- }
- }
- bool isGenerator = m_scriptContext->GetConfig()->IsES6GeneratorsEnabled() &&
- m_token.tk == tkStar;
- ushort fncDeclFlags = fFncNoName | fFncMethod;
- if (isGenerator)
- {
- if (isAsyncMethod)
- {
- Error(ERRsyntax);
- }
- m_pscan->ScanForcingPid();
- fncDeclFlags |= fFncGenerator;
- }
- IdentPtr pidHint = nullptr; // A name scoped to current expression
- Token tkHint = m_token;
- charcount_t idHintIchMin = static_cast<charcount_t>(m_pscan->IecpMinTok());
- charcount_t idHintIchLim = static_cast< charcount_t >(m_pscan->IecpLimTok());
- bool wrapInBrackets = false;
- switch (m_token.tk)
- {
- default:
- if (!m_token.IsReservedWord())
- {
- Error(ERRnoMemberIdent);
- }
- // allow reserved words
- wrapInBrackets = true;
- // fall-through
- case tkID:
- pidHint = m_token.GetIdentifier(m_phtbl);
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pidHint);
- }
- break;
- case tkStrCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- wrapInBrackets = true;
- pidHint = m_token.GetStr();
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pidHint);
- }
- break;
- case tkIntCon:
- // Object initializers with numeric labels allowed in JS6
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- pidHint = m_pscan->PidFromLong(m_token.GetLong());
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pidHint);
- }
- break;
- case tkFltCon:
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- pidHint = m_pscan->PidFromDbl(m_token.GetDouble());
- if (buildAST)
- {
- pnodeName = CreateStrNodeWithScanner(pidHint);
- }
- wrapInBrackets = true;
- break;
- case tkLBrack:
- // Computed property name: [expr] : value
- if (!m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
- {
- Error(ERRnoMemberIdent);
- }
- ParseComputedName<buildAST>(&pnodeName, &pNameHint, &pFullNameHint, &fullNameHintLength, &shortNameOffset);
- isComputedName = true;
- break;
- }
- if (pFullNameHint == nullptr)
- {
- if (CONFIG_FLAG(UseFullName))
- {
- pFullNameHint = AppendNameHints(pNameHint, pidHint, &fullNameHintLength, &shortNameOffset, false, wrapInBrackets);
- }
- else
- {
- pFullNameHint = pidHint? pidHint->Psz() : nullptr;
- fullNameHintLength = pidHint ? pidHint->Cch() : 0;
- shortNameOffset = 0;
- }
- }
- RestorePoint atPid;
- m_pscan->Capture(&atPid);
- m_pscan->ScanForcingPid();
- if (isGenerator && m_token.tk != tkLParen)
- {
- Error(ERRnoLparen);
- }
- if (tkColon == m_token.tk)
- {
- // It is a syntax error is the production of the form __proto__ : <> occurs more than once. From B.3.1 in spec.
- // Note that previous scan is important because only after that we can determine we have a variable.
- if (!isComputedName && pidHint == wellKnownPropertyPids.__proto__)
- {
- if (isProtoDeclared)
- {
- Error(ERRsyntax);
- }
- else
- {
- isProtoDeclared = true;
- }
- }
- m_pscan->Scan();
- ParseNodePtr pnodeExpr = nullptr;
- if (isObjectPattern)
- {
- if (m_token.tk == tkEllipsis)
- {
- Error(ERRUnexpectedEllipsis);
- }
- pnodeExpr = ParseDestructuredVarDecl<buildAST>(declarationType, declarationType != tkLCurly, nullptr/* *hasSeenRest*/, false /*topLevel*/, false /*allowEmptyExpression*/);
- if (m_token.tk != tkComma && m_token.tk != tkRCurly)
- {
- if (m_token.IsOperator())
- {
- Error(ERRDestructNoOper);
- }
- Error(ERRsyntax);
- }
- }
- else
- {
- pnodeExpr = ParseExpr<buildAST>(koplCma, nullptr, TRUE, FALSE, pFullNameHint, &fullNameHintLength, &shortNameOffset);
- }
- #if DEBUG
- if((m_grfscr & fscrEnforceJSON) && !IsJSONValid(pnodeExpr))
- {
- Error(ERRsyntax);
- }
- #endif
- if (buildAST)
- {
- pnodeArg = CreateBinNode(isObjectPattern ? knopObjectPatternMember : knopMember, pnodeName, pnodeExpr);
- if (pnodeArg->sxBin.pnode1->nop == knopStr)
- {
- pnodeArg->sxBin.pnode1->sxPid.pid->PromoteAssignmentState();
- }
- }
- }
- else if (m_token.tk == tkLParen && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
- {
- if (isObjectPattern)
- {
- Error(ERRInvalidAssignmentTarget);
- }
- // Shorthand syntax: foo() {} -> foo: function() {}
- // Rewind to the PID and parse a function expression.
- m_pscan->SeekTo(atPid);
- this->m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperPropertyAllowed;
- ParseNodePtr pnodeFunc = ParseFncDecl<buildAST>(fncDeclFlags | (isAsyncMethod ? fFncAsync : fFncNoFlgs), pFullNameHint,
- /*needsPIDOnRCurlyScan*/ false, /*resetParsingSuperRestrictionState*/ false);
- if (isAsyncMethod)
- {
- pnodeFunc->sxFnc.cbMin = iecpMin;
- pnodeFunc->ichMin = ichMin;
- }
- if (buildAST)
- {
- pnodeArg = CreateBinNode(knopMember, pnodeName, pnodeFunc);
- }
- }
- else if (nullptr != pidHint) //Its either tkID/tkStrCon/tkFloatCon/tkIntCon
- {
- Assert(pidHint->Psz() != nullptr);
- if ((pidHint == wellKnownPropertyPids.get || pidHint == wellKnownPropertyPids.set) &&
- // get/set are only pseudo keywords when they are identifiers (i.e. not strings)
- tkHint.tk == tkID && NextTokenIsPropertyNameStart())
- {
- if (isObjectPattern)
- {
- Error(ERRInvalidAssignmentTarget);
- }
- LPCOLESTR pNameGetOrSet = nullptr;
- OpCode op = pidHint == wellKnownPropertyPids.get ? knopGetMember : knopSetMember;
- pnodeArg = ParseMemberGetSet<buildAST>(op, &pNameGetOrSet);
- if (CONFIG_FLAG(UseFullName) && buildAST && pnodeArg->sxBin.pnode2->nop == knopFncDecl)
- {
- if (m_scriptContext->GetConfig()->IsES6FunctionNameEnabled())
- {
- // displays as "get object.funcname" or "set object.funcname"
- ulong getOrSetOffset = 0;
- LPCOLESTR intermediateHint = AppendNameHints(pNameHint, pNameGetOrSet, &fullNameHintLength, &shortNameOffset);
- pFullNameHint = AppendNameHints(pidHint, intermediateHint, &fullNameHintLength, &getOrSetOffset, true);
- shortNameOffset += getOrSetOffset;
- }
- else
- {
- // displays as "object.funcname.get" or "object.funcname.set"
- LPCOLESTR intermediateHint = AppendNameHints(pNameGetOrSet, pidHint, &fullNameHintLength, &shortNameOffset);
- pFullNameHint = AppendNameHints(pNameHint, intermediateHint, &fullNameHintLength, &shortNameOffset);
- }
- }
- }
- else if ((m_token.tk == tkRCurly || m_token.tk == tkComma || m_token.tk == tkAsg) && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
- {
- // Shorthand {foo} -> {foo:foo} syntax.
- // {foo = <initializer>} supported only when on object pattern rules are being applied
- if (tkHint.tk != tkID)
- {
- Assert(tkHint.IsReservedWord()
- || tkHint.tk == tkIntCon || tkHint.tk == tkFltCon || tkHint.tk == tkStrCon);
- // All keywords are banned in non-strict mode.
- // Future reserved words are banned in strict mode.
- if (IsStrictMode() || !tkHint.IsFutureReservedWord(true))
- {
- IdentifierExpectedError(tkHint);
- }
- }
- if (buildAST)
- {
- CheckArgumentsUse(pidHint, GetCurrentFunctionNode());
- }
- bool couldBeObjectPattern = !isObjectPattern && m_token.tk == tkAsg;
- if (couldBeObjectPattern)
- {
- declarationType = tkLCurly;
- isObjectPattern = true;
- // This may be an error but we are deferring for favouring destructuring.
- hasDeferredInitError = true;
- }
- ParseNodePtr pnodeIdent = nullptr;
- if (isObjectPattern)
- {
- m_pscan->SeekTo(atPid);
- pnodeIdent = ParseDestructuredVarDecl<buildAST>(declarationType, declarationType != tkLCurly, nullptr/* *hasSeenRest*/, false /*topLevel*/, false /*allowEmptyExpression*/);
- if (m_token.tk != tkComma && m_token.tk != tkRCurly)
- {
- if (m_token.IsOperator())
- {
- Error(ERRDestructNoOper);
- }
- Error(ERRsyntax);
- }
- }
- if (buildAST)
- {
- if (!isObjectPattern)
- {
- pnodeIdent = CreateNameNode(pidHint, idHintIchMin, idHintIchLim);
- PidRefStack *ref = PushPidRef(pidHint);
- pnodeIdent->sxPid.SetSymRef(ref);
- }
- pnodeArg = CreateBinNode(isObjectPattern && !couldBeObjectPattern ? knopObjectPatternMember : knopMemberShort, pnodeName, pnodeIdent);
- }
- }
- else
- {
- Error(ERRnoColon);
- }
- }
- else
- {
- Error(ERRnoColon);
- }
- if (buildAST)
- {
- Assert(pnodeArg->sxBin.pnode2 != nullptr);
- if (pnodeArg->sxBin.pnode2->nop == knopFncDecl)
- {
- Assert(fullNameHintLength >= shortNameOffset);
- pnodeArg->sxBin.pnode2->sxFnc.hint = pFullNameHint;
- pnodeArg->sxBin.pnode2->sxFnc.hintLength = fullNameHintLength;
- pnodeArg->sxBin.pnode2->sxFnc.hintOffset = shortNameOffset;
- }
- AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeArg);
- }
- pidHint = nullptr;
- pFullNameHint = nullptr;
- if (tkComma != m_token.tk)
- {
- break;
- }
- m_pscan->ScanForcingPid();
- if (tkRCurly == m_token.tk)
- {
- break;
- }
- }
- m_hasDeferredShorthandInitError = m_hasDeferredShorthandInitError || hasDeferredInitError;
- if (buildAST)
- {
- AssertMem(lastNodeRef);
- AssertNodeMem(*lastNodeRef);
- pnodeList->ichLim = (*lastNodeRef)->ichLim;
- }
- return pnodeList;
- }
- BOOL Parser::DeferredParse(Js::LocalFunctionId functionId)
- {
- if ((m_grfscr & fscrDeferFncParse) != 0)
- {
- if (m_stoppedDeferredParse)
- {
- return false;
- }
- if (PHASE_OFF_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, functionId))
- {
- return false;
- }
- if (PHASE_FORCE_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, functionId))
- {
- return true;
- }
- #if ENABLE_PROFILE_INFO
- #ifndef DISABLE_DYNAMIC_PROFILE_DEFER_PARSE
- if (m_sourceContextInfo->sourceDynamicProfileManager != nullptr)
- {
- Js::ExecutionFlags flags = m_sourceContextInfo->sourceDynamicProfileManager->IsFunctionExecuted(functionId);
- return flags != Js::ExecutionFlags_Executed;
- }
- #endif
- #endif
- return true;
- }
- return false;
- }
- //
- // Call this in ParseFncDecl only to check (and reset) if ParseFncDecl is re-parsing a deferred
- // function body. If a deferred function is called and being re-parsed, it shouldn't be deferred again.
- //
- BOOL Parser::IsDeferredFnc()
- {
- if (m_grfscr & fscrDeferredFnc)
- {
- m_grfscr &= ~fscrDeferredFnc;
- return true;
- }
- return false;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseFncDecl(ushort flags, LPCOLESTR pNameHint, const bool needsPIDOnRCurlyScan, bool resetParsingSuperRestrictionState, bool fUnaryOrParen)
- {
- AutoParsingSuperRestrictionStateRestorer restorer(this);
- if (resetParsingSuperRestrictionState)
- {
- // ParseFncDecl will always reset m_parsingSuperRestrictionState to super disallowed unless explicitly disabled
- this->m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperDisallowed;
- }
- ParseNodePtr pnodeFnc = nullptr;
- ParseNodePtr *ppnodeVarSave = nullptr;
- ParseNodePtr pnodeFncSave = nullptr;
- ParseNodePtr pnodeFncSaveNonLambda = nullptr;
- ParseNodePtr pnodeFncBlockScope = nullptr;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- bool funcHasName = false;
- bool fDeclaration = flags & fFncDeclaration;
- bool fLambda = (flags & fFncLambda) != 0;
- charcount_t ichMin = this->m_pscan->IchMinTok();
- bool wasInDeferredNestedFunc = false;
- uint tryCatchOrFinallyDepthSave = this->m_tryCatchOrFinallyDepth;
- this->m_tryCatchOrFinallyDepth = 0;
- if (this->m_arrayDepth)
- {
- this->m_funcInArrayDepth++; // Count function depth within array literal
- }
- // Update the count of functions nested in the current parent.
- Assert(m_pnestedCount || !buildAST);
- uint *pnestedCountSave = m_pnestedCount;
- if (buildAST || m_pnestedCount)
- {
- (*m_pnestedCount)++;
- }
- uint scopeCountNoAstSave = m_scopeCountNoAst;
- m_scopeCountNoAst = 0;
- long* pAstSizeSave = m_pCurrentAstSize;
- bool noStmtContext = false;
- if (fDeclaration)
- {
- AnalysisAssert(m_pstmtCur->isDeferred || m_pstmtCur->pnodeStmt != nullptr);
- noStmtContext =
- (m_pstmtCur->isDeferred && m_pstmtCur->op != knopBlock) ||
- (!m_pstmtCur->isDeferred && m_pstmtCur->pnodeStmt->nop != knopBlock);
- if (noStmtContext)
- {
- // We have a function declaration like "if (a) function f() {}". We didn't see
- // a block scope on the way in, so we need to pretend we did. Note that this is a syntax error
- // in strict mode.
- if (!this->FncDeclAllowedWithoutContext(flags))
- {
- Error(ERRsyntax);
- }
- pnodeFncBlockScope = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
- if (buildAST)
- {
- PushFuncBlockScope(pnodeFncBlockScope, &ppnodeScopeSave, &ppnodeExprScopeSave);
- }
- }
- }
- // Create the node.
- pnodeFnc = CreateNode(knopFncDecl);
- pnodeFnc->sxFnc.ClearFlags();
- pnodeFnc->sxFnc.SetDeclaration(fDeclaration);
- pnodeFnc->sxFnc.astSize = 0;
- pnodeFnc->sxFnc.pnodeName = nullptr;
- pnodeFnc->sxFnc.pnodeScopes = nullptr;
- pnodeFnc->sxFnc.pnodeRest = nullptr;
- pnodeFnc->sxFnc.pid = nullptr;
- pnodeFnc->sxFnc.hint = nullptr;
- pnodeFnc->sxFnc.hintOffset = 0;
- pnodeFnc->sxFnc.hintLength = 0;
- pnodeFnc->sxFnc.isNameIdentifierRef = true;
- pnodeFnc->sxFnc.pnodeNext = nullptr;
- pnodeFnc->sxFnc.pnodeParams = nullptr;
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- pnodeFnc->sxFnc.funcInfo = nullptr;
- pnodeFnc->sxFnc.deferredStub = nullptr;
- pnodeFnc->sxFnc.nestedCount = 0;
- pnodeFnc->sxFnc.cbMin = m_pscan->IecpMinTok();
- pnodeFnc->sxFnc.functionId = (*m_nextFunctionId)++;
- // Push new parser state with this new function node
- AppendFunctionToScopeList(fDeclaration, pnodeFnc);
- // Start the argument list.
- ppnodeVarSave = m_ppnodeVar;
- if (buildAST)
- {
- pnodeFnc->sxFnc.lineNumber = m_pscan->LineCur();
- pnodeFnc->sxFnc.columnNumber = CalculateFunctionColumnNumber();
- pnodeFnc->sxFnc.SetNested(m_currentNodeFunc != nullptr); // If there is a current function, then we're a nested function.
- pnodeFnc->sxFnc.SetStrictMode(IsStrictMode()); // Inherit current strict mode -- may be overridden by the function itself if it contains a strict mode directive.
- pnodeFnc->sxFnc.firstDefaultArg = 0;
- m_pCurrentAstSize = &pnodeFnc->sxFnc.astSize;
- // Make this the current function and start its sub-function list.
- pnodeFncSave = m_currentNodeFunc;
- m_currentNodeFunc = pnodeFnc;
- Assert(m_currentNodeDeferredFunc == nullptr);
- if (!fLambda)
- {
- pnodeFncSaveNonLambda = m_currentNodeNonLambdaFunc;
- m_currentNodeNonLambdaFunc = pnodeFnc;
- }
- m_pnestedCount = &pnodeFnc->sxFnc.nestedCount;
- }
- else // if !buildAST
- {
- wasInDeferredNestedFunc = m_inDeferredNestedFunc;
- m_inDeferredNestedFunc = true;
- AnalysisAssert(pnodeFnc);
- if (!fLambda)
- {
- pnodeFncSaveNonLambda = m_currentNodeNonLambdaDeferredFunc;
- m_currentNodeNonLambdaDeferredFunc = pnodeFnc;
- }
- pnodeFncSave = m_currentNodeDeferredFunc;
- m_currentNodeDeferredFunc = pnodeFnc;
- m_pnestedCount = &pnodeFnc->sxFnc.nestedCount;
- }
- AnalysisAssert(pnodeFnc);
- pnodeFnc->sxFnc.SetIsAsync((flags & fFncAsync) != 0);
- pnodeFnc->sxFnc.SetIsLambda(fLambda);
- pnodeFnc->sxFnc.SetIsMethod((flags & fFncMethod) != 0);
- pnodeFnc->sxFnc.SetIsClassMember((flags & fFncClassMember) != 0);
- bool needScanRCurly = true;
- bool result = ParseFncDeclHelper<buildAST>(pnodeFnc, pnodeFncSave, pNameHint, flags, &funcHasName, fUnaryOrParen, noStmtContext, &needScanRCurly);
- if (!result)
- {
- Assert(!pnodeFncBlockScope);
- return pnodeFnc;
- }
- AnalysisAssert(pnodeFnc);
- *m_ppnodeVar = nullptr;
- m_ppnodeVar = ppnodeVarSave;
- // Restore the current function.
- if (buildAST)
- {
- Assert(pnodeFnc == m_currentNodeFunc);
- m_currentNodeFunc = pnodeFncSave;
- m_pCurrentAstSize = pAstSizeSave;
- if (!fLambda)
- {
- Assert(pnodeFnc == m_currentNodeNonLambdaFunc);
- m_currentNodeNonLambdaFunc = pnodeFncSaveNonLambda;
- }
- }
- else
- {
- Assert(pnodeFnc == m_currentNodeDeferredFunc);
- if (!fLambda)
- {
- Assert(pnodeFnc == m_currentNodeNonLambdaDeferredFunc);
- m_currentNodeNonLambdaDeferredFunc = pnodeFncSaveNonLambda;
- }
- m_currentNodeDeferredFunc = pnodeFncSave;
- if (m_currentNodeFunc && pnodeFnc->sxFnc.HasWithStmt())
- {
- GetCurrentFunctionNode()->sxFnc.SetHasWithStmt(true);
- }
- }
- if (m_currentNodeFunc && (pnodeFnc->sxFnc.CallsEval() || pnodeFnc->sxFnc.ChildCallsEval()))
- {
- GetCurrentFunctionNode()->sxFnc.SetChildCallsEval(true);
- }
- // Lambdas do not have "arguments" and instead capture their parent's
- // binding of "arguments. To ensure the arguments object of the enclosing
- // non-lambda function is loaded propagate the UsesArguments flag up to
- // the parent function
- if ((flags & fFncLambda) != 0 && pnodeFnc->sxFnc.UsesArguments())
- {
- if (pnodeFncSave != nullptr)
- {
- pnodeFncSave->sxFnc.SetUsesArguments();
- }
- else
- {
- m_UsesArgumentsAtGlobal = true;
- }
- }
- if (needScanRCurly)
- {
- // Consume the next token now that we're back in the enclosing function (whose strictness may be
- // different from the function we just finished).
- #if DBG
- bool expectedTokenValid = m_token.tk == tkRCurly;
- AssertMsg(expectedTokenValid, "Invalid token expected for RCurly match");
- #endif
- // The next token may need to have a PID created in !buildAST mode, as we may be parsing a method with a string name.
- if (needsPIDOnRCurlyScan)
- {
- m_pscan->ScanForcingPid();
- }
- else
- {
- m_pscan->Scan();
- }
- }
- m_pnestedCount = pnestedCountSave;
- Assert(!buildAST || !wasInDeferredNestedFunc);
- m_inDeferredNestedFunc = wasInDeferredNestedFunc;
- if (this->m_arrayDepth)
- {
- this->m_funcInArrayDepth--;
- if (this->m_funcInArrayDepth == 0)
- {
- // We disable deferred parsing if array literals dominate.
- // But don't do this if the array literal is dominated by function bodies.
- if (flags & (fFncMethod | fFncClassMember) && m_token.tk != tkSColon)
- {
- // Class member methods have optional separators. We need to check whether we are
- // getting the IchLim of the correct token.
- Assert(m_pscan->m_tkPrevious == tkRCurly && needScanRCurly);
- this->m_funcInArray += m_pscan->IchMinTok() - /*tkRCurly*/ 1 - ichMin;
- }
- else
- {
- this->m_funcInArray += m_pscan->IchLimTok() - ichMin;
- }
- }
- }
- m_scopeCountNoAst = scopeCountNoAstSave;
- if (buildAST && fDeclaration && !IsStrictMode())
- {
- if (pnodeFnc->sxFnc.pnodeName != nullptr && pnodeFnc->sxFnc.pnodeName->nop == knopVarDecl &&
- GetCurrentBlock()->sxBlock.blockType == PnodeBlockType::Regular)
- {
- // Add a function-scoped VarDecl with the same name as the function for
- // back compat with pre-ES6 code that declares functions in blocks. The
- // idea is that the last executed declaration wins at the function scope
- // level and we accomplish this by having each block scoped function
- // declaration assign to both the block scoped "let" binding, as well
- // as the function scoped "var" binding.
- ParseNodePtr vardecl = CreateVarDeclNode(pnodeFnc->sxFnc.pnodeName->sxVar.pid, STVariable, false, nullptr, false);
- vardecl->sxVar.isBlockScopeFncDeclVar = true;
- }
- }
- if (pnodeFncBlockScope)
- {
- Assert(pnodeFncBlockScope->sxBlock.pnodeStmt == nullptr);
- pnodeFncBlockScope->sxBlock.pnodeStmt = pnodeFnc;
- if (buildAST)
- {
- PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
- }
- FinishParseBlock(pnodeFncBlockScope);
- return pnodeFncBlockScope;
- }
- this->m_tryCatchOrFinallyDepth = tryCatchOrFinallyDepthSave;
- return pnodeFnc;
- }
- bool Parser::FncDeclAllowedWithoutContext(ushort flags)
- {
- // Statement context required for strict mode, async functions, and generators.
- // Note that generators aren't detected yet when this method is called; they're checked elsewhere.
- return !IsStrictMode() && !(flags & fFncAsync);
- }
- uint Parser::CalculateFunctionColumnNumber()
- {
- uint columnNumber;
- if (m_pscan->IchMinTok() >= m_pscan->IchMinLine())
- {
- // In scenarios involving defer parse IchMinLine() can be incorrect for the first line after defer parse
- columnNumber = m_pscan->IchMinTok() - m_pscan->IchMinLine();
- if (m_functionBody != nullptr && m_functionBody->GetRelativeLineNumber() == m_pscan->LineCur())
- {
- // Adjust the column if it falls on the first line, where the re-parse is happening.
- columnNumber += m_functionBody->GetRelativeColumnNumber();
- }
- }
- else if (m_currentNodeFunc)
- {
- // For the first line after defer parse, compute the column relative to the column number
- // of the lexically parent function.
- ULONG offsetFromCurrentFunction = m_pscan->IchMinTok() - m_currentNodeFunc->ichMin;
- columnNumber = m_currentNodeFunc->sxFnc.columnNumber + offsetFromCurrentFunction ;
- }
- else
- {
- // if there is no current function, lets give a default of 0.
- columnNumber = 0;
- }
- return columnNumber;
- }
- void Parser::AppendFunctionToScopeList(bool fDeclaration, ParseNodePtr pnodeFnc)
- {
- if (!fDeclaration && m_ppnodeExprScope)
- {
- // We're tracking function expressions separately from declarations in this scope
- // (e.g., inside a catch scope in standards mode).
- Assert(*m_ppnodeExprScope == nullptr);
- *m_ppnodeExprScope = pnodeFnc;
- m_ppnodeExprScope = &pnodeFnc->sxFnc.pnodeNext;
- }
- else
- {
- Assert(*m_ppnodeScope == nullptr);
- *m_ppnodeScope = pnodeFnc;
- m_ppnodeScope = &pnodeFnc->sxFnc.pnodeNext;
- }
- }
- /***************************************************************************
- Parse a function definition.
- ***************************************************************************/
- template<bool buildAST>
- bool Parser::ParseFncDeclHelper(ParseNodePtr pnodeFnc, ParseNodePtr pnodeFncParent, LPCOLESTR pNameHint, ushort flags, bool *pHasName, bool fUnaryOrParen, bool noStmtContext, bool *pNeedScanRCurly)
- {
- bool fDeclaration = (flags & fFncDeclaration) != 0;
- bool fLambda = (flags & fFncLambda) != 0;
- bool fAsync = (flags & fFncAsync) != 0;
- bool fDeferred = false;
- StmtNest *pstmtSave;
- ParseNodePtr *lastNodeRef = nullptr;
- bool fFunctionInBlock = false;
- if (buildAST)
- {
- fFunctionInBlock = GetCurrentBlockInfo() != GetCurrentFunctionBlockInfo() &&
- (GetCurrentBlockInfo()->pnodeBlock->sxBlock.scope == nullptr ||
- GetCurrentBlockInfo()->pnodeBlock->sxBlock.scope->GetScopeType() != ScopeType_GlobalEvalBlock);
- }
- // Save the position of the scanner in case we need to inspect the name hint later
- RestorePoint beginNameHint;
- m_pscan->Capture(&beginNameHint);
- ParseNodePtr pnodeFncExprScope = nullptr;
- Scope *fncExprScope = nullptr;
- if (!fDeclaration)
- {
- pnodeFncExprScope = StartParseBlock<buildAST>(PnodeBlockType::Function, ScopeType_FuncExpr);
- fncExprScope = pnodeFncExprScope->sxBlock.scope;
- }
- *pHasName = !fLambda && this->ParseFncNames<buildAST>(pnodeFnc, pnodeFncParent, flags, &lastNodeRef);
- if (noStmtContext && pnodeFnc->sxFnc.IsGenerator())
- {
- // Generator decl not allowed outside stmt context. (We have to wait until we've parsed the '*' to
- // detect generator.)
- Error(ERRsyntax, pnodeFnc);
- }
- // switch scanner to treat 'yield' as keyword in generator functions
- // or as an identifier in non-generator functions
- bool fPreviousYieldIsKeyword = m_pscan->SetYieldIsKeyword(pnodeFnc && pnodeFnc->sxFnc.IsGenerator());
- bool fPreviousAwaitIsKeyword = m_pscan->SetAwaitIsKeyword(fAsync);
- if (pnodeFnc && pnodeFnc->sxFnc.IsGenerator())
- {
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(GeneratorCount, m_scriptContext);
- }
- if (fncExprScope && !*pHasName)
- {
- FinishParseBlock(pnodeFncExprScope);
- m_nextBlockId--;
- Adelete(&m_nodeAllocator, fncExprScope);
- fncExprScope = nullptr;
- pnodeFncExprScope = nullptr;
- }
- if (pnodeFnc)
- {
- pnodeFnc->sxFnc.scope = fncExprScope;
- }
- // Start a new statement stack.
- bool topLevelStmt =
- buildAST &&
- !fFunctionInBlock &&
- (this->m_pstmtCur == nullptr || this->m_pstmtCur->pnodeStmt->nop == knopBlock);
- pstmtSave = m_pstmtCur;
- SetCurrentStatement(nullptr);
- // Function definition is inside the parent function's parameter scope
- bool isEnclosedInParamScope = this->m_currentScope->GetScopeType() == ScopeType_Parameter;
- if (this->m_currentScope->GetScopeType() == ScopeType_FuncExpr || this->m_currentScope->GetScopeType() == ScopeType_Block)
- {
- // Or this is a function expression or class enclosed in a parameter scope
- isEnclosedInParamScope = this->m_currentScope->GetEnclosingScope() && this->m_currentScope->GetEnclosingScope()->GetScopeType() == ScopeType_Parameter;
- }
- Assert(!isEnclosedInParamScope || pnodeFncParent->sxFnc.HasNonSimpleParameterList());
- RestorePoint beginFormals;
- m_pscan->Capture(&beginFormals);
- BOOL fWasAlreadyStrictMode = IsStrictMode();
- BOOL oldStrictMode = this->m_fUseStrictMode;
- if (fLambda)
- {
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(LambdaCount, m_scriptContext);
- }
- uint uDeferSave = m_grfscr & fscrDeferFncParse;
- if ((!fDeclaration && m_ppnodeExprScope) ||
- fFunctionInBlock ||
- isEnclosedInParamScope ||
- (flags & (fFncNoName | fFncLambda)))
- {
- // NOTE: Don't defer if this is a function expression inside a construct that induces
- // a scope nested within the current function (like a with, or a catch in ES5 mode, or
- // any function declared inside a nested lexical block or param scope in ES6 mode).
- // We won't be able to reconstruct the scope chain properly when we come back and
- // try to compile just the function expression.
- // Also shut off deferring on getter/setter or other construct with unusual text bounds
- // (fFncNoName|fFncLambda) as these are usually trivial, and re-parsing is problematic.
- m_grfscr &= ~fscrDeferFncParse;
- }
- bool isTopLevelDeferredFunc = false;
- struct AutoFastScanFlag {
- bool savedDoingFastScan;
- AutoFastScanFlag(Parser *parser) : m_parser(parser) { savedDoingFastScan = m_parser->m_doingFastScan; }
- ~AutoFastScanFlag() { m_parser->m_doingFastScan = savedDoingFastScan; }
- Parser *m_parser;
- } flag(this);
- bool doParallel = false;
- bool parallelJobStarted = false;
- if (buildAST)
- {
- bool isLikelyModulePattern =
- !fDeclaration && pnodeFnc && pnodeFnc->sxFnc.pnodeName == nullptr && fUnaryOrParen;
- BOOL isDeferredFnc = IsDeferredFnc();
- AnalysisAssert(isDeferredFnc || pnodeFnc);
- isTopLevelDeferredFunc =
- (!isDeferredFnc
- && DeferredParse(pnodeFnc->sxFnc.functionId)
- && (!pnodeFnc->sxFnc.IsNested() || CONFIG_FLAG(DeferNested))
- // Don't defer if this is a function expression not contained in a statement or other expression.
- // Assume it will be called as part of this expression.
- && (!isLikelyModulePattern || !topLevelStmt || PHASE_FORCE_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, pnodeFnc->sxFnc.functionId))
- && !m_InAsmMode
- );
- if (!fLambda &&
- !isDeferredFnc &&
- !isLikelyModulePattern &&
- !this->IsBackgroundParser() &&
- !this->m_doingFastScan &&
- !(pnodeFncParent && m_currDeferredStub) &&
- !(this->m_parseType == ParseType_Deferred && this->m_functionBody && this->m_functionBody->GetScopeInfo() && !isTopLevelDeferredFunc))
- {
- doParallel = DoParallelParse(pnodeFnc);
- #if ENABLE_BACKGROUND_PARSING
- if (doParallel)
- {
- BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
- Assert(bgp);
- if (bgp->HasFailedBackgroundParseItem())
- {
- Error(ERRsyntax);
- }
- doParallel = bgp->ParseBackgroundItem(this, pnodeFnc, isTopLevelDeferredFunc);
- if (doParallel)
- {
- parallelJobStarted = true;
- this->m_hasParallelJob = true;
- this->m_doingFastScan = true;
- doParallel = FastScanFormalsAndBody();
- if (doParallel)
- {
- // Let the foreground thread take care of marking the limit on the function node,
- // because in some cases this function's caller will want to change that limit,
- // so we don't want the background thread to try and touch it.
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- }
- }
- }
- #endif
- }
- }
- if (!doParallel)
- {
- // We don't want to, or couldn't, let the main thread scan past this function body, so parse
- // it for real.
- ParseNodePtr pnodeRealFnc = pnodeFnc;
- if (parallelJobStarted)
- {
- // We have to deal with a failure to fast-scan the function (due to syntax error? "/"?) when
- // a background thread may already have begun to work on the job. Both threads can't be allowed to
- // operate on the same node.
- pnodeFnc = CreateDummyFuncNode(fDeclaration);
- }
- AnalysisAssert(pnodeFnc);
- ParseNodePtr pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Parameter, ScopeType_Parameter);
- AnalysisAssert(pnodeBlock != nullptr);
- pnodeFnc->sxFnc.pnodeScopes = pnodeBlock;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeParams;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- ppnodeScopeSave = m_ppnodeScope;
- if (pnodeBlock)
- {
- // This synthetic block scope will contain all the nested scopes.
- m_ppnodeScope = &pnodeBlock->sxBlock.pnodeScopes;
- pnodeBlock->sxBlock.pnodeStmt = pnodeFnc;
- }
- // Keep nested function declarations and expressions in the same list at function scope.
- // (Indicate this by nulling out the current function expressions list.)
- ppnodeExprScopeSave = m_ppnodeExprScope;
- m_ppnodeExprScope = nullptr;
- this->ParseFncFormals<buildAST>(pnodeFnc, flags);
- // Create function body scope
- ParseNodePtr pnodeInnerBlock = StartParseBlock<buildAST>(PnodeBlockType::Function, ScopeType_FunctionBody);
- // Set the parameter block's child to the function body block.
- // The pnodeFnc->sxFnc.pnodeScopes list is constructed in such a way that it includes all the scopes in this list.
- // For example if the param scope has one function and body scope has one function then the list will look like below,
- // param scope block -> function decl from param scope -> body socpe block -> function decl from body scope.
- *m_ppnodeScope = pnodeInnerBlock;
- pnodeFnc->sxFnc.pnodeBodyScope = pnodeInnerBlock;
- // This synthetic block scope will contain all the nested scopes.
- m_ppnodeScope = &pnodeInnerBlock->sxBlock.pnodeScopes;
- pnodeInnerBlock->sxBlock.pnodeStmt = pnodeFnc;
- // DEFER: Begin deferral here (after names are parsed and name nodes created).
- // Create no more AST nodes until we're done.
- // Try to defer this func if all these are true:
- // 0. We are not already in deferred parsing (i.e. buildAST is true)
- // 1. We are not re-parsing a deferred func which is being invoked.
- // 2. Dynamic profile suggests this func can be deferred (and deferred parse is on).
- // 3. This func is top level or defer nested func is on.
- // 4. Optionally, the function is non-nested and not in eval, or the deferral decision was based on cached profile info,
- // or the function is sufficiently long. (I.e., don't defer little nested functions unless we're
- // confident they'll never be executed, because un-deferring nested functions is more expensive.)
- // NOTE: I'm disabling #4 by default, because we've found other ways to reduce the cost of un-deferral,
- // and we don't want to create function bodies aggressively for little functions.
- // We will also temporarily defer all asm.js functions, except for the asm.js
- // module itself, which we will never defer
- bool strictModeTurnedOn = false;
- if (isTopLevelDeferredFunc &&
- !(this->m_grfscr & fscrEvalCode) &&
- pnodeFnc->sxFnc.IsNested() &&
- #ifndef DISABLE_DYNAMIC_PROFILE_DEFER_PARSE
- m_sourceContextInfo->sourceDynamicProfileManager == nullptr &&
- #endif
- PHASE_ON_RAW(Js::ScanAheadPhase, m_sourceContextInfo->sourceContextId, pnodeFnc->sxFnc.functionId) &&
- (
- !PHASE_FORCE_RAW(Js::DeferParsePhase, m_sourceContextInfo->sourceContextId, pnodeFnc->sxFnc.functionId) ||
- PHASE_FORCE_RAW(Js::ScanAheadPhase, m_sourceContextInfo->sourceContextId, pnodeFnc->sxFnc.functionId)
- ))
- {
- // Try to scan ahead to the end of the function. If we get there before we've scanned a minimum
- // number of tokens, don't bother deferring, because it's too small.
- if (this->ScanAheadToFunctionEnd(CONFIG_FLAG(MinDeferredFuncTokenCount)))
- {
- isTopLevelDeferredFunc = false;
- }
- }
- if (fAsync)
- {
- if (!buildAST || isTopLevelDeferredFunc)
- {
- // We increment m_nextFunctionId when there is an Async function to counterbalance the functionId because of the added generator to the AST with an async function that we use to keep deferred parsing in sync with non-deferred parsing
- (*m_nextFunctionId)++;
- }
- // Same than before, we increment the nestedCount because we will have a Generator inside any async function.
- pnodeFnc->sxFnc.nestedCount++;
- }
- Scope* paramScope = pnodeFnc->sxFnc.pnodeScopes ? pnodeFnc->sxFnc.pnodeScopes->sxBlock.scope : nullptr;
- if (paramScope != nullptr && pnodeFnc->sxFnc.HasNonSimpleParameterList() && !fAsync)
- {
- Assert(paramScope != nullptr);
- if (paramScope->GetCanMergeWithBodyScope())
- {
- paramScope->ForEachSymbolUntil([this, paramScope](Symbol* sym) {
- if (sym->GetPid()->GetTopRef()->sym == nullptr)
- {
- // One of the symbol has non local reference. Mark the param scope as we can't merge it with body scope.
- paramScope->SetCannotMergeWithBodyScope();
- return true;
- }
- else
- {
- // If no non-local references are there then the top of the ref stack should point to the same symbol.
- Assert(sym->GetPid()->GetTopRef()->sym == sym);
- }
- return false;
- });
- }
- }
- // If the param scope is merged with the body scope we want to use the param scope symbols in the body scope.
- // So add a pid ref for the body using the param scope symbol. Note that in this case the same symbol will occur twice
- // in the same pid ref stack.
- if (paramScope != nullptr && paramScope->GetCanMergeWithBodyScope() && (isTopLevelDeferredFunc || !fAsync))
- {
- paramScope->ForEachSymbol([this](Symbol* paramSym)
- {
- Symbol* sym = paramSym->GetPid()->GetTopRef()->GetSym();
- PidRefStack* ref = PushPidRef(paramSym->GetPid());
- ref->SetSym(sym);
- });
- }
- if (isTopLevelDeferredFunc || (m_InAsmMode && m_deferAsmJs))
- {
- AssertMsg(!fLambda, "Deferring function parsing of a function does not handle lambda syntax");
- fDeferred = true;
- this->ParseTopLevelDeferredFunc(pnodeFnc, pnodeFncParent, pNameHint);
- }
- else
- {
- if (m_token.tk == tkRParen) // This might be false due to error recovery or lambda.
- {
- m_pscan->Scan();
- }
- if (fLambda)
- {
- BOOL hadNewLine = m_pscan->FHadNewLine();
- // it can be the case we do not have a fat arrow here if there is a valid expression on the left hand side
- // of the fat arrow, but that expression does not parse as a parameter list. E.g.
- // a.x => { }
- // Therefore check for it and error if not found.
- // LS Mode : since this is a lambda we supposed to get the fat arrow, if not we will skip till we get that fat arrow.
- ChkCurTok(tkDArrow, ERRnoDArrow);
- // Newline character between arrow parameters and fat arrow is a syntax error but we want to check for
- // this after verifying there was a => token. Otherwise we would throw the wrong error.
- if (hadNewLine)
- {
- Error(ERRsyntax);
- }
- }
- AnalysisAssert(pnodeFnc);
- // Shouldn't be any temps in the arg list.
- Assert(*m_ppnodeVar == nullptr);
- // Start the var list.
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- if (paramScope != nullptr && !paramScope->GetCanMergeWithBodyScope())
- {
- OUTPUT_TRACE_DEBUGONLY(Js::ParsePhase, _u("The param and body scope of the function %s cannot be merged\n"), pnodeFnc->sxFnc.pnodeName ? pnodeFnc->sxFnc.pnodeName->sxVar.pid->Psz() : _u("Anonymous function"));
- // Add a new symbol reference for each formal in the param scope to the body scope.
- paramScope->ForEachSymbol([this](Symbol* param) {
- OUTPUT_TRACE_DEBUGONLY(Js::ParsePhase, _u("Creating a duplicate symbol for the parameter %s in the body scope\n"), param->GetPid()->Psz());
- ParseNodePtr paramNode = this->CreateVarDeclNode(param->GetPid(), STVariable, false, nullptr, false);
- Assert(paramNode && paramNode->sxVar.sym->GetScope()->GetScopeType() == ScopeType_FunctionBody);
- paramNode->sxVar.sym->SetHasInit(true);
- });
- }
- // Keep nested function declarations and expressions in the same list at function scope.
- // (Indicate this by nulling out the current function expressions list.)
- m_ppnodeExprScope = nullptr;
- if (buildAST)
- {
- DeferredFunctionStub *saveCurrentStub = m_currDeferredStub;
- if (isEnclosedInParamScope)
- {
- // if the enclosed scope is the param scope we would not have created the deferred stub.
- m_currDeferredStub = nullptr;
- }
- else if (pnodeFncParent && m_currDeferredStub)
- {
- // the Deferred stub will not match for the function which are defined on lambda formals.
- // Since this is not determined upfront that the current function is a part of outer function or part of lambda formal until we have seen the Arrow token.
- // Due to that the current function may be fetching stubs from the outer function (outer of the lambda) - rather then the lambda function. The way to fix is to match
- // the function start with the stub. Because they should match. We need to have previous sibling concept as the lambda formals can have more than one
- // functions and we want to avoid getting wrong stub.
- if (pnodeFncParent->sxFnc.nestedCount == 1)
- {
- m_prevSiblingDeferredStub = nullptr;
- }
- if (m_prevSiblingDeferredStub == nullptr)
- {
- m_prevSiblingDeferredStub = (m_currDeferredStub + (pnodeFncParent->sxFnc.nestedCount - 1));
- }
- if (m_prevSiblingDeferredStub->ichMin == pnodeFnc->ichMin)
- {
- m_currDeferredStub = m_prevSiblingDeferredStub->deferredStubs;
- m_prevSiblingDeferredStub = nullptr;
- }
- else
- {
- m_currDeferredStub = nullptr;
- }
- }
- if (m_token.tk != tkLCurly && fLambda)
- {
- if (fAsync)
- {
- TransformAsyncFncDeclAST(&pnodeFnc, true);
- }
- else
- {
- ParseExpressionLambdaBody<true>(pnodeFnc);
- }
- *pNeedScanRCurly = false;
- }
- else
- {
- this->FinishFncDecl(pnodeFnc, pNameHint, lastNodeRef);
- }
- m_currDeferredStub = saveCurrentStub;
- }
- else
- {
- this->ParseNestedDeferredFunc(pnodeFnc, fLambda, pNeedScanRCurly, &strictModeTurnedOn);
- }
- }
- if (pnodeInnerBlock)
- {
- FinishParseBlock(pnodeInnerBlock, *pNeedScanRCurly);
- }
- if (m_token.tk == tkLCurly || !fLambda)
- {
- this->AddArgumentsNodeToVars(pnodeFnc);
- }
- // Restore the lists of scopes that contain function expressions.
- Assert(m_ppnodeExprScope == nullptr || *m_ppnodeExprScope == nullptr);
- m_ppnodeExprScope = ppnodeExprScopeSave;
- AssertMem(m_ppnodeScope);
- Assert(nullptr == *m_ppnodeScope);
- m_ppnodeScope = ppnodeScopeSave;
- if (pnodeBlock)
- {
- FinishParseBlock(pnodeBlock, *pNeedScanRCurly);
- }
- if (IsStrictMode() || strictModeTurnedOn)
- {
- this->m_fUseStrictMode = TRUE; // Now we know this function is in strict mode
- if (!fWasAlreadyStrictMode)
- {
- // If this function turned on strict mode then we didn't check the formal
- // parameters or function name hint for future reserved word usage. So do that now.
- RestorePoint afterFnc;
- m_pscan->Capture(&afterFnc);
- if (*pHasName)
- {
- // Rewind to the function name hint and check if the token is a reserved word.
- m_pscan->SeekTo(beginNameHint);
- m_pscan->Scan();
- if (pnodeFnc->sxFnc.IsGenerator())
- {
- Assert(m_token.tk == tkStar);
- Assert(m_scriptContext->GetConfig()->IsES6GeneratorsEnabled());
- Assert(!(flags & fFncClassMember));
- m_pscan->Scan();
- }
- if (m_token.IsReservedWord())
- {
- IdentifierExpectedError(m_token);
- }
- CheckStrictModeEvalArgumentsUsage(m_token.GetIdentifier(m_phtbl));
- }
- // Fast forward to formal parameter list, check for future reserved words,
- // then restore scanner as it was.
- m_pscan->SeekToForcingPid(beginFormals);
- CheckStrictFormalParameters();
- m_pscan->SeekTo(afterFnc);
- }
- if (buildAST)
- {
- if (pnodeFnc->sxFnc.pnodeName != nullptr && knopVarDecl == pnodeFnc->sxFnc.pnodeName->nop)
- {
- CheckStrictModeEvalArgumentsUsage(pnodeFnc->sxFnc.pnodeName->sxVar.pid, pnodeFnc->sxFnc.pnodeName);
- }
- }
- this->m_fUseStrictMode = oldStrictMode;
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(StrictModeFunctionCount, m_scriptContext);
- }
- if (fDeferred)
- {
- AnalysisAssert(pnodeFnc);
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- }
- if (parallelJobStarted)
- {
- pnodeFnc = pnodeRealFnc;
- m_currentNodeFunc = pnodeRealFnc;
- // Let the foreground thread take care of marking the limit on the function node,
- // because in some cases this function's caller will want to change that limit,
- // so we don't want the background thread to try and touch it.
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- }
- }
- // after parsing asm.js module, we want to reset asm.js state before continuing
- AnalysisAssert(pnodeFnc);
- if (pnodeFnc->sxFnc.GetAsmjsMode())
- {
- m_InAsmMode = false;
- }
- // Restore the statement stack.
- Assert(nullptr == m_pstmtCur);
- SetCurrentStatement(pstmtSave);
- if (pnodeFncExprScope)
- {
- FinishParseFncExprScope(pnodeFnc, pnodeFncExprScope);
- }
- if (!m_stoppedDeferredParse)
- {
- m_grfscr |= uDeferSave;
- }
- m_pscan->SetYieldIsKeyword(fPreviousYieldIsKeyword);
- m_pscan->SetAwaitIsKeyword(fPreviousAwaitIsKeyword);
- return true;
- }
- void Parser::ParseTopLevelDeferredFunc(ParseNodePtr pnodeFnc, ParseNodePtr pnodeFncParent, LPCOLESTR pNameHint)
- {
- // Parse a function body that is a transition point from building AST to doing fast syntax check.
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- pnodeFnc->sxFnc.pnodeBody = nullptr;
- this->m_deferringAST = TRUE;
- // Put the scanner into "no hashing" mode.
- BYTE deferFlags = m_pscan->SetDeferredParse(TRUE);
- m_pscan->Scan();
- ChkCurTok(tkLCurly, ERRnoLcurly);
- ParseNodePtr *ppnodeVarSave = m_ppnodeVar;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- if (pnodeFncParent != nullptr
- && m_currDeferredStub != nullptr
- // We don't create stubs for function bodies in parameter scope.
- && pnodeFnc->sxFnc.pnodeScopes->sxBlock.blockType != PnodeBlockType::Parameter)
- {
- // We've already parsed this function body for syntax errors on the initial parse of the script.
- // We have information that allows us to skip it, so do so.
- DeferredFunctionStub *stub = m_currDeferredStub + (pnodeFncParent->sxFnc.nestedCount - 1);
- Assert(pnodeFnc->ichMin == stub->ichMin);
- if (stub->fncFlags & kFunctionCallsEval)
- {
- this->MarkEvalCaller();
- }
- if (stub->fncFlags & kFunctionChildCallsEval)
- {
- pnodeFnc->sxFnc.SetChildCallsEval(true);
- }
- if (stub->fncFlags & kFunctionHasWithStmt)
- {
- pnodeFnc->sxFnc.SetHasWithStmt(true);
- }
- PHASE_PRINT_TRACE1(
- Js::SkipNestedDeferredPhase,
- _u("Skipping nested deferred function %d. %s: %d...%d\n"),
- pnodeFnc->sxFnc.functionId, GetFunctionName(pnodeFnc, pNameHint), pnodeFnc->ichMin, stub->restorePoint.m_ichMinTok);
- m_pscan->SeekTo(stub->restorePoint, m_nextFunctionId);
- pnodeFnc->sxFnc.nestedCount = stub->nestedCount;
- pnodeFnc->sxFnc.deferredStub = stub->deferredStubs;
- if (stub->fncFlags & kFunctionStrictMode)
- {
- pnodeFnc->sxFnc.SetStrictMode(true);
- }
- }
- else
- {
- ParseStmtList<false>(nullptr, nullptr, SM_DeferredParse, true /* isSourceElementList */);
- }
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- m_ppnodeVar = ppnodeVarSave;
- // Restore the scanner's default hashing mode.
- // Do this before we consume the next token.
- m_pscan->SetDeferredParseFlags(deferFlags);
- ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
- #if DBG
- pnodeFnc->sxFnc.deferredParseNextFunctionId = *this->m_nextFunctionId;
- #endif
- this->m_deferringAST = FALSE;
- }
- bool Parser::DoParallelParse(ParseNodePtr pnodeFnc) const
- {
- #if ENABLE_BACKGROUND_PARSING
- if (!PHASE_ON_RAW(Js::ParallelParsePhase, m_sourceContextInfo->sourceContextId, pnodeFnc->sxFnc.functionId))
- {
- return false;
- }
- BackgroundParser *bgp = m_scriptContext->GetBackgroundParser();
- return bgp != nullptr;
- #else
- return false;
- #endif
- }
- bool Parser::ScanAheadToFunctionEnd(uint count)
- {
- bool found = false;
- uint curlyDepth = 0;
- RestorePoint funcStart;
- m_pscan->Capture(&funcStart);
- for (uint i = 0; i < count; i++)
- {
- switch (m_token.tk)
- {
- case tkStrTmplBegin:
- case tkStrTmplMid:
- case tkStrTmplEnd:
- case tkDiv:
- case tkAsgDiv:
- case tkScanError:
- case tkEOF:
- goto LEnd;
- case tkLCurly:
- UInt32Math::Inc(curlyDepth, Parser::OutOfMemory);
- break;
- case tkRCurly:
- if (curlyDepth == 1)
- {
- found = true;
- goto LEnd;
- }
- if (curlyDepth == 0)
- {
- goto LEnd;
- }
- curlyDepth--;
- break;
- }
- m_pscan->ScanAhead();
- }
- LEnd:
- m_pscan->SeekTo(funcStart);
- return found;
- }
- bool Parser::FastScanFormalsAndBody()
- {
- // The scanner is currently pointing just past the name of a function.
- // The idea here is to find the end of the function body as quickly as possible,
- // by tokenizing and tracking {}'s if possible.
- // String templates require some extra logic but can be handled.
- // The real wrinkle is "/" and "/=", which may indicate either a RegExp literal or a division, depending
- // on the context.
- // To handle this with minimal work, keep track of the last ";" seen at each {} depth. If we see one of the
- // difficult tokens, rewind to the last ";" at the current {} depth and parse statements until we pass the
- // point where we had to rewind. This will process the "/" as required.
- RestorePoint funcStart;
- m_pscan->Capture(&funcStart);
- const int maxRestorePointDepth = 16;
- struct FastScanRestorePoint
- {
- RestorePoint restorePoint;
- uint parenDepth;
- Js::LocalFunctionId functionId;
- int blockId;
- FastScanRestorePoint() : restorePoint(), parenDepth(0) {};
- };
- FastScanRestorePoint lastSColonAtCurlyDepth[maxRestorePointDepth];
- charcount_t ichStart = m_pscan->IchMinTok();
- uint blockIdSave = m_nextBlockId;
- uint functionIdSave = *m_nextFunctionId;
- uint curlyDepth = 0;
- uint strTmplDepth = 0;
- for (;;)
- {
- switch (m_token.tk)
- {
- case tkStrTmplBegin:
- UInt32Math::Inc(strTmplDepth, Parser::OutOfMemory);
- // Fall through
- case tkStrTmplMid:
- case tkLCurly:
- UInt32Math::Inc(curlyDepth, Parser::OutOfMemory);
- Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
- break;
- case tkStrTmplEnd:
- // We can assert here, because the scanner will only return this token if we've told it we're
- // in a string template.
- Assert(strTmplDepth > 0);
- strTmplDepth--;
- break;
- case tkRCurly:
- if (curlyDepth == 1)
- {
- Assert(strTmplDepth == 0);
- if (PHASE_TRACE1(Js::ParallelParsePhase))
- {
- Output::Print(_u("Finished fast seek: %d. %s -- %d...%d\n"),
- m_currentNodeFunc->sxFnc.functionId,
- GetFunctionName(m_currentNodeFunc, m_currentNodeFunc->sxFnc.hint),
- ichStart, m_pscan->IchLimTok());
- }
- return true;
- }
- if (curlyDepth < maxRestorePointDepth)
- {
- lastSColonAtCurlyDepth[curlyDepth].restorePoint.m_ichMinTok = (uint)-1;
- }
- curlyDepth--;
- if (strTmplDepth > 0)
- {
- m_pscan->SetScanState(Scanner_t::ScanState::ScanStateStringTemplateMiddleOrEnd);
- }
- break;
- case tkSColon:
- // Track the location of the ";" (if it's outside parens, as we don't, for instance, want
- // to track the ";"'s in a for-loop header. If we find it's important to rewind within a paren
- // expression, we can do something more sophisticated.)
- if (curlyDepth < maxRestorePointDepth && lastSColonAtCurlyDepth[curlyDepth].parenDepth == 0)
- {
- m_pscan->Capture(&lastSColonAtCurlyDepth[curlyDepth].restorePoint);
- lastSColonAtCurlyDepth[curlyDepth].functionId = *this->m_nextFunctionId;
- lastSColonAtCurlyDepth[curlyDepth].blockId = m_nextBlockId;
- }
- break;
- case tkLParen:
- if (curlyDepth < maxRestorePointDepth)
- {
- UInt32Math::Inc(lastSColonAtCurlyDepth[curlyDepth].parenDepth);
- }
- break;
- case tkRParen:
- if (curlyDepth < maxRestorePointDepth)
- {
- Assert(lastSColonAtCurlyDepth[curlyDepth].parenDepth != 0);
- lastSColonAtCurlyDepth[curlyDepth].parenDepth--;
- }
- break;
- case tkID:
- {
- charcount_t tokLength = m_pscan->IchLimTok() - m_pscan->IchMinTok();
- // Detect the function and class keywords so we can track function ID's.
- // (In fast mode, the scanner doesn't distinguish keywords and doesn't point the token
- // to a PID.)
- // Detect try/catch/for to increment block count for them.
- switch (tokLength)
- {
- case 3:
- if (!memcmp(m_pscan->PchMinTok(), "try", 3) || !memcmp(m_pscan->PchMinTok(), "for", 3))
- {
- Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
- }
- break;
- case 5:
- if (!memcmp(m_pscan->PchMinTok(), "catch", 5))
- {
- Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
- }
- else if (!memcmp(m_pscan->PchMinTok(), "class", 5))
- {
- Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
- Int32Math::Inc(*this->m_nextFunctionId, (int*)this->m_nextFunctionId);
- }
- break;
- case 8:
- if (!memcmp(m_pscan->PchMinTok(), "function", 8))
- {
- // Account for the possible func expr scope or dummy block for missing {}'s around a declaration
- Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
- Int32Math::Inc(*this->m_nextFunctionId, (int*)this->m_nextFunctionId);
- }
- break;
- }
- break;
- }
- case tkDArrow:
- Int32Math::Inc(m_nextBlockId, &m_nextBlockId);
- Int32Math::Inc(*this->m_nextFunctionId, (int*)this->m_nextFunctionId);
- break;
- case tkDiv:
- case tkAsgDiv:
- {
- int opl;
- OpCode nop;
- tokens tkPrev = m_pscan->m_tkPrevious;
- if ((m_pscan->m_phtbl->TokIsBinop(tkPrev, &opl, &nop) && nop != knopNone) ||
- (m_pscan->m_phtbl->TokIsUnop(tkPrev, &opl, &nop) &&
- nop != knopNone &&
- tkPrev != tkInc &&
- tkPrev != tkDec) ||
- tkPrev == tkColon ||
- tkPrev == tkLParen ||
- tkPrev == tkLBrack ||
- tkPrev == tkRETURN)
- {
- // Previous token indicates that we're starting an expression here and can't have a
- // binary operator now.
- // Assume this is a RegExp.
- ParseRegExp<false>();
- break;
- }
- uint tempCurlyDepth = curlyDepth < maxRestorePointDepth ? curlyDepth : maxRestorePointDepth - 1;
- for (; tempCurlyDepth != (uint)-1; tempCurlyDepth--)
- {
- // We don't know whether we've got a RegExp or a divide. Rewind to the last safe ";"
- // if we can and parse statements until we pass this point.
- if (lastSColonAtCurlyDepth[tempCurlyDepth].restorePoint.m_ichMinTok != -1)
- {
- break;
- }
- }
- if (tempCurlyDepth != (uint)-1)
- {
- ParseNodePtr pnodeFncSave = m_currentNodeFunc;
- long *pastSizeSave = m_pCurrentAstSize;
- uint *pnestedCountSave = m_pnestedCount;
- ParseNodePtr *ppnodeScopeSave = m_ppnodeScope;
- ParseNodePtr *ppnodeExprScopeSave = m_ppnodeExprScope;
- ParseNodePtr pnodeFnc = CreateDummyFuncNode(true);
- m_ppnodeScope = &pnodeFnc->sxFnc.pnodeScopes;
- m_ppnodeExprScope = nullptr;
- charcount_t ichStop = m_pscan->IchLimTok();
- curlyDepth = tempCurlyDepth;
- m_pscan->SeekTo(lastSColonAtCurlyDepth[tempCurlyDepth].restorePoint);
- m_nextBlockId = lastSColonAtCurlyDepth[tempCurlyDepth].blockId;
- *this->m_nextFunctionId = lastSColonAtCurlyDepth[tempCurlyDepth].functionId;
- ParseNodePtr pnodeBlock = StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
- m_pscan->Scan();
- do
- {
- ParseStatement<false>();
- }
- while(m_pscan->IchMinTok() < ichStop);
- FinishParseBlock(pnodeBlock);
- m_currentNodeFunc = pnodeFncSave;
- m_pCurrentAstSize = pastSizeSave;
- m_pnestedCount = pnestedCountSave;
- m_ppnodeScope = ppnodeScopeSave;
- m_ppnodeExprScope = ppnodeExprScopeSave;
- // We've already consumed the first token of the next statement, so just continue
- // without a further scan.
- continue;
- }
- }
- // fall through to rewind to function start
- case tkScanError:
- case tkEOF:
- // Unexpected token.
- if (PHASE_TRACE1(Js::ParallelParsePhase))
- {
- Output::Print(_u("Failed fast seek: %d. %s -- %d...%d\n"),
- m_currentNodeFunc->sxFnc.functionId,
- GetFunctionName(m_currentNodeFunc, m_currentNodeFunc->sxFnc.hint),
- ichStart, m_pscan->IchLimTok());
- }
- m_nextBlockId = blockIdSave;
- *m_nextFunctionId = functionIdSave;
- m_pscan->SeekTo(funcStart);
- return false;
- }
- m_pscan->ScanNoKeywords();
- }
- }
- ParseNodePtr Parser::CreateDummyFuncNode(bool fDeclaration)
- {
- // Create a dummy node and make it look like the current function declaration.
- // Do this in situations where we want to parse statements without impacting
- // the state of the "real" AST.
- ParseNodePtr pnodeFnc = CreateNode(knopFncDecl);
- pnodeFnc->sxFnc.ClearFlags();
- pnodeFnc->sxFnc.SetDeclaration(fDeclaration);
- pnodeFnc->sxFnc.astSize = 0;
- pnodeFnc->sxFnc.pnodeName = nullptr;
- pnodeFnc->sxFnc.pnodeScopes = nullptr;
- pnodeFnc->sxFnc.pnodeRest = nullptr;
- pnodeFnc->sxFnc.pid = nullptr;
- pnodeFnc->sxFnc.hint = nullptr;
- pnodeFnc->sxFnc.hintOffset = 0;
- pnodeFnc->sxFnc.hintLength = 0;
- pnodeFnc->sxFnc.isNameIdentifierRef = true;
- pnodeFnc->sxFnc.pnodeNext = nullptr;
- pnodeFnc->sxFnc.pnodeParams = nullptr;
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- pnodeFnc->sxFnc.funcInfo = nullptr;
- pnodeFnc->sxFnc.deferredStub = nullptr;
- pnodeFnc->sxFnc.nestedCount = 0;
- pnodeFnc->sxFnc.SetNested(m_currentNodeFunc != nullptr); // If there is a current function, then we're a nested function.
- pnodeFnc->sxFnc.SetStrictMode(IsStrictMode()); // Inherit current strict mode -- may be overridden by the function itself if it contains a strict mode directive.
- pnodeFnc->sxFnc.firstDefaultArg = 0;
- m_pCurrentAstSize = &pnodeFnc->sxFnc.astSize;
- m_currentNodeFunc = pnodeFnc;
- m_pnestedCount = &pnodeFnc->sxFnc.nestedCount;
- return pnodeFnc;
- }
- void Parser::ParseNestedDeferredFunc(ParseNodePtr pnodeFnc, bool fLambda, bool *pNeedScanRCurly, bool *pStrictModeTurnedOn)
- {
- // Parse a function nested inside another deferred function.
- size_t lengthBeforeBody = this->GetSourceLength();
- if (m_token.tk != tkLCurly && fLambda)
- {
- ParseExpressionLambdaBody<false>(pnodeFnc);
- *pNeedScanRCurly = false;
- }
- else
- {
- ChkCurTok(tkLCurly, ERRnoLcurly);
- bool* detectStrictModeOn = IsStrictMode() ? nullptr : pStrictModeTurnedOn;
- m_ppnodeVar = &m_currentNodeDeferredFunc->sxFnc.pnodeVars;
- ParseStmtList<false>(nullptr, nullptr, SM_DeferredParse, true /* isSourceElementList */, detectStrictModeOn);
- ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
- }
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- if (*pStrictModeTurnedOn)
- {
- pnodeFnc->sxFnc.SetStrictMode(true);
- }
- if (!PHASE_OFF1(Js::SkipNestedDeferredPhase))
- {
- // Record the end of the function and the function ID increment that happens inside the function.
- // Byte code gen will use this to build stub information to allow us to skip this function when the
- // enclosing function is fully parsed.
- RestorePoint *restorePoint = Anew(&m_nodeAllocator, RestorePoint);
- m_pscan->Capture(restorePoint,
- *m_nextFunctionId - pnodeFnc->sxFnc.functionId - 1,
- lengthBeforeBody - this->GetSourceLength());
- pnodeFnc->sxFnc.pRestorePoint = restorePoint;
- }
- }
- template<bool buildAST>
- bool Parser::ParseFncNames(ParseNodePtr pnodeFnc, ParseNodePtr pnodeFncParent, ushort flags, ParseNodePtr **pLastNodeRef)
- {
- BOOL fDeclaration = flags & fFncDeclaration;
- BOOL fIsAsync = flags & fFncAsync;
- ParseNodePtr pnodeT;
- charcount_t ichMinNames, ichLimNames;
- // Get the names to bind to.
- /*
- * KaushiS [5/15/08]:
- * ECMAScript defines a FunctionExpression as follows:
- *
- * "function" [Identifier] ( [FormalParameterList] ) { FunctionBody }
- *
- * The function name being optional is omitted by most real world
- * code that uses a FunctionExpression to define a function. This however
- * is problematic for tools because there isn't a function name that
- * the runtime can provide.
- *
- * To fix this (primarily for the profiler), I'm adding simple, static
- * name inferencing logic to the parser. When it encounters the following
- * productions
- *
- * "var" Identifier "=" FunctionExpression
- * "var" IdentifierA.IdentifierB...Identifier "=" FunctionExpression
- * Identifier = FunctionExpression
- * "{" Identifier: FunctionExpression "}"
- *
- * it associates Identifier with the function created by the
- * FunctionExpression. This identifier is *not* the function's name. It
- * is ignored by the runtime and is only an additional piece of information
- * about the function (function name hint) that tools could opt to
- * surface.
- */
- m_pscan->Scan();
- // If generators are enabled then we are in a recent enough version
- // that deferred parsing will create a parse node for pnodeFnc and
- // it is safe to assume it is not null.
- if (flags & fFncGenerator)
- {
- Assert(m_scriptContext->GetConfig()->IsES6GeneratorsEnabled());
- pnodeFnc->sxFnc.SetIsGenerator();
- }
- else if (m_scriptContext->GetConfig()->IsES6GeneratorsEnabled() &&
- m_token.tk == tkStar &&
- !(flags & fFncClassMember))
- {
- if (!fDeclaration)
- {
- bool fPreviousYieldIsKeyword = m_pscan->SetYieldIsKeyword(!fDeclaration);
- m_pscan->Scan();
- m_pscan->SetYieldIsKeyword(fPreviousYieldIsKeyword);
- }
- else
- {
- m_pscan->Scan();
- }
- pnodeFnc->sxFnc.SetIsGenerator();
- }
- if (fIsAsync)
- {
- if (pnodeFnc->sxFnc.IsGenerator())
- {
- Error(ERRsyntax);
- }
- pnodeFnc->sxFnc.SetIsAsync();
- }
- if (pnodeFnc)
- {
- pnodeFnc->sxFnc.pnodeName = nullptr;
- }
- if ((m_token.tk != tkID || flags & fFncNoName)
- && (IsStrictMode() || (pnodeFnc && pnodeFnc->sxFnc.IsGenerator()) || m_token.tk != tkYIELD || fDeclaration)) // Function expressions can have the name yield even inside generator functions
- {
- if (fDeclaration ||
- m_token.IsReservedWord()) // For example: var x = (function break(){});
- {
- IdentifierExpectedError(m_token);
- }
- return false;
- }
- ichMinNames = m_pscan->IchMinTok();
- Assert(m_token.tk == tkID || (m_token.tk == tkYIELD && !fDeclaration));
- if (IsStrictMode())
- {
- CheckStrictModeEvalArgumentsUsage(m_token.GetIdentifier(m_phtbl));
- }
- Token tokenBase = m_token;
- charcount_t ichMinBase = m_pscan->IchMinTok();
- charcount_t ichLimBase = m_pscan->IchLimTok();
- m_pscan->Scan();
- IdentPtr pidBase = tokenBase.GetIdentifier(m_phtbl);
- pnodeT = CreateDeclNode(knopVarDecl, pidBase, STFunction);
- pnodeT->ichMin = ichMinBase;
- pnodeT->ichLim = ichLimBase;
- if (fDeclaration &&
- pnodeFncParent &&
- pnodeFncParent->sxFnc.pnodeName &&
- pnodeFncParent->sxFnc.pnodeName->nop == knopVarDecl &&
- pnodeFncParent->sxFnc.pnodeName->sxVar.pid == pidBase)
- {
- pnodeFncParent->sxFnc.SetNameIsHidden();
- }
- if (buildAST)
- {
- AnalysisAssert(pnodeFnc);
- ichLimNames = pnodeT->ichLim;
- AddToNodeList(&pnodeFnc->sxFnc.pnodeName, pLastNodeRef, pnodeT);
- pnodeFnc->sxFnc.pnodeName->ichMin = ichMinNames;
- pnodeFnc->sxFnc.pnodeName->ichLim = ichLimNames;
- if (knopVarDecl == pnodeFnc->sxFnc.pnodeName->nop)
- {
- // Only one name (the common case).
- pnodeFnc->sxFnc.pid = pnodeFnc->sxFnc.pnodeName->sxVar.pid;
- }
- else
- {
- // Multiple names. Turn the source into an IdentPtr.
- pnodeFnc->sxFnc.pid = m_phtbl->PidHashNameLen(
- m_pscan->PchBase() + ichMinNames, ichLimNames - ichMinNames);
- }
- if(pnodeFnc->sxFnc.pid == wellKnownPropertyPids.arguments && fDeclaration && pnodeFncParent)
- {
- // This function declaration (or function expression in compat modes) overrides the built-in arguments object of the
- // parent function
- pnodeFncParent->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
- }
- }
- return true;
- }
- void Parser::ValidateFormals()
- {
- ParseFncFormals<false>(NULL, fFncNoFlgs);
- // Eat the tkRParen. The ParseFncDeclHelper caller expects to see it.
- m_pscan->Scan();
- }
- void Parser::ValidateSourceElementList()
- {
- ParseStmtList<false>(NULL, NULL, SM_NotUsed, true);
- }
- void Parser::UpdateOrCheckForDuplicateInFormals(IdentPtr pid, SList<IdentPtr> *formals)
- {
- bool isStrictMode = IsStrictMode();
- if (isStrictMode)
- {
- CheckStrictModeEvalArgumentsUsage(pid);
- }
- if (formals->Has(pid))
- {
- if (isStrictMode)
- {
- Error(ERRES5ArgSame);
- }
- else
- {
- Error(ERRFormalSame);
- }
- }
- else
- {
- formals->Prepend(pid);
- }
- }
- template<bool buildAST>
- void Parser::ParseFncFormals(ParseNodePtr pnodeFnc, ushort flags)
- {
- bool fLambda = (flags & fFncLambda) != 0;
- bool fMethod = (flags & fFncMethod) != 0;
- bool fNoArg = (flags & fFncNoArg) != 0;
- bool fOneArg = (flags & fFncOneArg) != 0;
- Assert(!fNoArg || !fOneArg); // fNoArg and fOneArg can never be true at the same time.
- // strictFormals corresponds to the StrictFormalParameters grammar production
- // in the ES spec which just means duplicate names are not allowed
- bool fStrictFormals = IsStrictMode() || fLambda || fMethod;
- // When detecting duplicated formals pids are needed so force PID creation (unless the function should take 0 or 1 arg).
- bool forcePid = fStrictFormals && !fNoArg && !fOneArg;
- AutoTempForcePid autoForcePid(m_pscan, forcePid);
- // Lambda's allow single formal specified by a single binding identifier without parentheses, special case it.
- if (fLambda && m_token.tk == tkID)
- {
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- CreateVarDeclNode(pid, STFormal, false, nullptr, false);
- CheckPidIsValid(pid);
- m_pscan->Scan();
- if (m_token.tk != tkDArrow)
- {
- Error(ERRsyntax, m_pscan->IchMinTok(), m_pscan->IchLimTok());
- }
- return;
- }
- // Otherwise, must have a parameter list within parens.
- ChkCurTok(tkLParen, ERRnoLparen);
- // Now parse the list of arguments, if present
- if (m_token.tk == tkRParen)
- {
- if (fOneArg)
- {
- Error(ERRSetterMustHaveOneParameter);
- }
- }
- else
- {
- if (fNoArg)
- {
- Error(ERRGetterMustHaveNoParameters);
- }
- SList<IdentPtr> formals(&m_nodeAllocator);
- ParseNodePtr pnodeT = nullptr;
- bool seenRestParameter = false;
- bool isNonSimpleParameterList = false;
- for (Js::ArgSlot argPos = 0; ; ++argPos)
- {
- bool isBindingPattern = false;
- if (m_scriptContext->GetConfig()->IsES6RestEnabled() && m_token.tk == tkEllipsis)
- {
- // Possible rest parameter
- m_pscan->Scan();
- seenRestParameter = true;
- }
- if (m_token.tk != tkID)
- {
- if (IsES6DestructuringEnabled() && IsPossiblePatternStart())
- {
- // Mark that the function has a non simple parameter list before parsing the pattern since the pattern can have function definitions.
- this->GetCurrentFunctionNode()->sxFnc.SetHasNonSimpleParameterList();
- ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- ParseNodePtr * ppNodeLex = m_currentBlockInfo->m_ppnodeLex;
- Assert(ppNodeLex != nullptr);
- ParseNodePtr paramPattern = nullptr;
- ParseNodePtr pnodePattern = ParseDestructuredLiteral<buildAST>(tkLET, true /*isDecl*/, false /*topLevel*/);
- // Instead of passing the STFormal all the way on many methods, it seems it is better to change the symbol type afterward.
- for (ParseNodePtr lexNode = *ppNodeLex; lexNode != nullptr; lexNode = lexNode->sxVar.pnodeNext)
- {
- Assert(lexNode->IsVarLetOrConst());
- UpdateOrCheckForDuplicateInFormals(lexNode->sxVar.pid, &formals);
- lexNode->sxVar.sym->SetSymbolType(STFormal);
- if (m_currentNodeFunc != nullptr && lexNode->sxVar.pid == wellKnownPropertyPids.arguments)
- {
- m_currentNodeFunc->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
- }
- }
- m_ppnodeVar = ppnodeVarSave;
- if (buildAST)
- {
- paramPattern = CreateParamPatternNode(pnodePattern);
- // Linking the current formal parameter (which is pattern parameter) with other formals.
- *m_ppnodeVar = paramPattern;
- paramPattern->sxParamPattern.pnodeNext = nullptr;
- m_ppnodeVar = ¶mPattern->sxParamPattern.pnodeNext;
- }
- isBindingPattern = true;
- isNonSimpleParameterList = true;
- }
- else
- {
- IdentifierExpectedError(m_token);
- }
- }
- if (!isBindingPattern)
- {
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- LPCOLESTR pNameHint = pid->Psz();
- ulong nameHintLength = pid->Cch();
- ulong nameHintOffset = 0;
- if (seenRestParameter)
- {
- this->GetCurrentFunctionNode()->sxFnc.SetHasNonSimpleParameterList();
- if (flags & fFncOneArg)
- {
- // The parameter of a setter cannot be a rest parameter.
- Error(ERRUnexpectedEllipsis);
- }
- pnodeT = CreateDeclNode(knopVarDecl, pid, STFormal, false);
- pnodeT->sxVar.sym->SetIsNonSimpleParameter(true);
- if (buildAST)
- {
- // When only validating formals, we won't have a function node.
- pnodeFnc->sxFnc.pnodeRest = pnodeT;
- if (!isNonSimpleParameterList)
- {
- // This is the first non-simple parameter we've seen. We need to go back
- // and set the Symbols of all previous parameters.
- MapFormalsWithoutRest(m_currentNodeFunc, [&](ParseNodePtr pnodeArg) { pnodeArg->sxVar.sym->SetIsNonSimpleParameter(true); });
- }
- }
- isNonSimpleParameterList = true;
- }
- else
- {
- pnodeT = CreateVarDeclNode(pid, STFormal, false, nullptr, false);
- if (isNonSimpleParameterList)
- {
- pnodeT->sxVar.sym->SetIsNonSimpleParameter(true);
- }
- }
- if (buildAST && pid == wellKnownPropertyPids.arguments)
- {
- // This formal parameter overrides the built-in 'arguments' object
- m_currentNodeFunc->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
- }
- if (fStrictFormals)
- {
- UpdateOrCheckForDuplicateInFormals(pid, &formals);
- }
- m_pscan->Scan();
- if (seenRestParameter && m_token.tk != tkRParen && m_token.tk != tkAsg)
- {
- Error(ERRRestLastArg);
- }
- if (m_token.tk == tkAsg && m_scriptContext->GetConfig()->IsES6DefaultArgsEnabled())
- {
- if (seenRestParameter && m_scriptContext->GetConfig()->IsES6RestEnabled())
- {
- Error(ERRRestWithDefault);
- }
- // In defer parse mode we have to flag the function node to indicate that it has default arguments
- // so that it will be considered for any syntax error scenario.
- // Also mark it before parsing the expression as it may contain functions.
- ParseNode* currentFncNode = GetCurrentFunctionNode();
- if (!currentFncNode->sxFnc.HasDefaultArguments())
- {
- currentFncNode->sxFnc.SetHasDefaultArguments();
- currentFncNode->sxFnc.SetHasNonSimpleParameterList();
- currentFncNode->sxFnc.firstDefaultArg = argPos;
- }
- m_pscan->Scan();
- ParseNodePtr pnodeInit = ParseExpr<buildAST>(koplCma, nullptr, TRUE, FALSE, pNameHint, &nameHintLength, &nameHintOffset);
- if (buildAST && pnodeInit->nop == knopFncDecl)
- {
- Assert(nameHintLength >= nameHintOffset);
- pnodeInit->sxFnc.hint = pNameHint;
- pnodeInit->sxFnc.hintLength = nameHintLength;
- pnodeInit->sxFnc.hintOffset = nameHintOffset;
- }
- AnalysisAssert(pnodeT);
- pnodeT->sxVar.sym->SetIsNonSimpleParameter(true);
- if (!isNonSimpleParameterList)
- {
- if (buildAST)
- {
- // This is the first non-simple parameter we've seen. We need to go back
- // and set the Symbols of all previous parameters.
- MapFormalsWithoutRest(m_currentNodeFunc, [&](ParseNodePtr pnodeArg) { pnodeArg->sxVar.sym->SetIsNonSimpleParameter(true); });
- }
- // There may be previous parameters that need to be checked for duplicates.
- isNonSimpleParameterList = true;
- }
- if (buildAST)
- {
- if (!m_currentNodeFunc->sxFnc.HasDefaultArguments())
- {
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(DefaultArgFunctionCount, m_scriptContext);
- }
- pnodeT->sxVar.pnodeInit = pnodeInit;
- pnodeT->ichLim = m_pscan->IchLimTok();
- }
- }
- }
- if (isNonSimpleParameterList && m_currentScope->GetHasDuplicateFormals())
- {
- Error(ERRFormalSame);
- }
- if (flags & fFncOneArg)
- {
- if (m_token.tk != tkRParen)
- {
- Error(ERRSetterMustHaveOneParameter);
- }
- break; //enforce only one arg
- }
- if (m_token.tk != tkComma)
- {
- break;
- }
- m_pscan->Scan();
- if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
- {
- break;
- }
- }
- if (seenRestParameter)
- {
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(RestCount, m_scriptContext);
- }
- if (m_token.tk != tkRParen)
- {
- Error(ERRnoRparen);
- }
- if ((this->GetCurrentFunctionNode()->sxFnc.CallsEval() || this->GetCurrentFunctionNode()->sxFnc.ChildCallsEval())
- && !m_scriptContext->GetConfig()->IsES6DefaultArgsSplitScopeEnabled())
- {
- Error(ERREvalNotSupportedInParamScope);
- }
- }
- Assert(m_token.tk == tkRParen);
- }
- template<bool buildAST>
- ParseNodePtr Parser::GenerateEmptyConstructor(bool extends)
- {
- ParseNodePtr pnodeFnc;
- // Create the node.
- pnodeFnc = CreateNode(knopFncDecl);
- pnodeFnc->sxFnc.ClearFlags();
- pnodeFnc->sxFnc.SetNested(NULL != m_currentNodeFunc);
- pnodeFnc->sxFnc.SetStrictMode();
- pnodeFnc->sxFnc.SetDeclaration(TRUE);
- pnodeFnc->sxFnc.SetIsMethod(TRUE);
- pnodeFnc->sxFnc.SetIsClassMember(TRUE);
- pnodeFnc->sxFnc.SetIsClassConstructor(TRUE);
- pnodeFnc->sxFnc.SetIsBaseClassConstructor(!extends);
- pnodeFnc->sxFnc.SetHasNonThisStmt(extends);
- pnodeFnc->sxFnc.SetIsGeneratedDefault(TRUE);
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->ichMin = m_pscan->IchMinTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- pnodeFnc->sxFnc.cbMin = m_pscan->IecpMinTok();
- pnodeFnc->sxFnc.astSize = 0;
- pnodeFnc->sxFnc.lineNumber = m_pscan->LineCur();
- pnodeFnc->sxFnc.functionId = (*m_nextFunctionId);
- pnodeFnc->sxFnc.pid = nullptr;
- pnodeFnc->sxFnc.hint = nullptr;
- pnodeFnc->sxFnc.hintOffset = 0;
- pnodeFnc->sxFnc.hintLength = 0;
- pnodeFnc->sxFnc.isNameIdentifierRef = true;
- pnodeFnc->sxFnc.pnodeName = nullptr;
- pnodeFnc->sxFnc.pnodeScopes = nullptr;
- pnodeFnc->sxFnc.pnodeParams = nullptr;
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- pnodeFnc->sxFnc.pnodeBody = nullptr;
- pnodeFnc->sxFnc.nestedCount = 0;
- pnodeFnc->sxFnc.pnodeNext = nullptr;
- pnodeFnc->sxFnc.pnodeRest = nullptr;
- pnodeFnc->sxFnc.deferredStub = nullptr;
- pnodeFnc->sxFnc.funcInfo = nullptr;
- #ifdef DBG
- pnodeFnc->sxFnc.deferredParseNextFunctionId = *(this->m_nextFunctionId);
- #endif
- AppendFunctionToScopeList(true, pnodeFnc);
- if (m_nextFunctionId)
- {
- (*m_nextFunctionId)++;
- }
- // Update the count of functions nested in the current parent.
- if (m_pnestedCount)
- {
- (*m_pnestedCount)++;
- }
- if (!buildAST)
- {
- return NULL;
- }
- if (m_pscan->IchMinTok() >= m_pscan->IchMinLine())
- {
- // In scenarios involving defer parse IchMinLine() can be incorrect for the first line after defer parse
- pnodeFnc->sxFnc.columnNumber = m_pscan->IchMinTok() - m_pscan->IchMinLine();
- }
- else if (m_currentNodeFunc)
- {
- // For the first line after defer parse, compute the column relative to the column number
- // of the lexically parent function.
- ULONG offsetFromCurrentFunction = m_pscan->IchMinTok() - m_currentNodeFunc->ichMin;
- pnodeFnc->sxFnc.columnNumber = m_currentNodeFunc->sxFnc.columnNumber + offsetFromCurrentFunction;
- }
- else
- {
- // if there is no current function, lets give a default of 0.
- pnodeFnc->sxFnc.columnNumber = 0;
- }
- long * pAstSizeSave = m_pCurrentAstSize;
- m_pCurrentAstSize = &(pnodeFnc->sxFnc.astSize);
- // Make this the current function.
- ParseNodePtr pnodeFncSave = m_currentNodeFunc;
- m_currentNodeFunc = pnodeFnc;
- ParseNodePtr pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Parameter, ScopeType_Parameter);
- ParseNodePtr pnodeInnerBlock = StartParseBlock<buildAST>(PnodeBlockType::Function, ScopeType_FunctionBody);
- pnodeBlock->sxBlock.pnodeScopes = pnodeInnerBlock;
- pnodeFnc->sxFnc.pnodeBodyScope = pnodeInnerBlock;
- pnodeFnc->sxFnc.pnodeScopes = pnodeBlock;
- ParseNodePtr *lastNodeRef = nullptr;
- if (extends)
- {
- // constructor() { super(...arguments); } (equivalent to constructor(...args) { super(...args); } )
- PidRefStack *ref = this->PushPidRef(wellKnownPropertyPids.arguments);
- ParseNodePtr argumentsId = CreateNameNode(wellKnownPropertyPids.arguments, pnodeFnc->ichMin, pnodeFnc->ichLim);
- argumentsId->sxPid.symRef = ref->GetSymRef();
- pnodeFnc->sxFnc.SetUsesArguments(true);
- pnodeFnc->sxFnc.SetHasReferenceableBuiltInArguments(true);
- ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- CreateVarDeclNode(wellKnownPropertyPids.arguments, STVariable, true, pnodeFnc)->grfpn |= PNodeFlags::fpnArguments;
- m_ppnodeVar = ppnodeVarSave;
- ParseNodePtr spreadArg = CreateUniNode(knopEllipsis, argumentsId, pnodeFnc->ichMin, pnodeFnc->ichLim);
- ParseNodePtr superRef = CreateNodeWithScanner<knopSuper>();
- pnodeFnc->sxFnc.SetHasSuperReference(TRUE);
- ParseNodePtr callNode = CreateCallNode(knopCall, superRef, spreadArg);
- callNode->sxCall.spreadArgCount = 1;
- AddToNodeList(&pnodeFnc->sxFnc.pnodeBody, &lastNodeRef, callNode);
- }
- AddToNodeList(&pnodeFnc->sxFnc.pnodeBody, &lastNodeRef, CreateNodeWithScanner<knopEndCode>());
- FinishParseBlock(pnodeInnerBlock);
- FinishParseBlock(pnodeBlock);
- m_currentNodeFunc = pnodeFncSave;
- m_pCurrentAstSize = pAstSizeSave;
- return pnodeFnc;
- }
- template<bool buildAST>
- void Parser::ParseExpressionLambdaBody(ParseNodePtr pnodeLambda)
- {
- ParseNodePtr *lastNodeRef = nullptr;
- // The lambda body is a single expression, the result of which is the return value.
- ParseNodePtr pnodeRet = nullptr;
- if (buildAST)
- {
- pnodeRet = CreateNodeWithScanner<knopReturn>();
- pnodeRet->grfpn |= PNodeFlags::fpnSyntheticNode;
- pnodeLambda->sxFnc.pnodeScopes->sxBlock.pnodeStmt = pnodeRet;
- }
- ParseNodePtr result = ParseExpr<buildAST>(koplAsg, nullptr, TRUE, FALSE, nullptr);
- if (buildAST)
- {
- pnodeRet->sxReturn.pnodeExpr = result;
- pnodeRet->ichMin = pnodeRet->sxReturn.pnodeExpr->ichMin;
- pnodeRet->ichLim = pnodeRet->sxReturn.pnodeExpr->ichLim;
- // Pushing a statement node with PushStmt<>() normally does this initialization
- // but do it here manually since we know there is no outer statement node.
- pnodeRet->sxStmt.grfnop = 0;
- pnodeRet->sxStmt.pnodeOuter = nullptr;
- pnodeLambda->ichLim = pnodeRet->ichLim;
- pnodeLambda->sxFnc.cbLim = m_pscan->IecpLimTokPrevious();
- pnodeLambda->sxFnc.pnodeScopes->ichLim = pnodeRet->ichLim;
- pnodeLambda->sxFnc.pnodeBody = nullptr;
- AddToNodeList(&pnodeLambda->sxFnc.pnodeBody, &lastNodeRef, pnodeLambda->sxFnc.pnodeScopes);
- // Append an EndCode node.
- ParseNodePtr end = CreateNodeWithScanner<knopEndCode>(pnodeRet->ichLim);
- end->ichLim = end->ichMin; // make end code zero width at the immediate end of lambda body
- AddToNodeList(&pnodeLambda->sxFnc.pnodeBody, &lastNodeRef, end);
- // Lambda's do not have arguments binding
- pnodeLambda->sxFnc.SetHasReferenceableBuiltInArguments(false);
- }
- }
- void Parser::CheckStrictFormalParameters()
- {
- if (m_token.tk == tkID)
- {
- // single parameter arrow function case
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- CheckStrictModeEvalArgumentsUsage(pid);
- return;
- }
- Assert(m_token.tk == tkLParen);
- m_pscan->ScanForcingPid();
- if (m_token.tk != tkRParen)
- {
- SList<IdentPtr> formals(&m_nodeAllocator);
- for (;;)
- {
- if (m_token.tk != tkID)
- {
- IdentifierExpectedError(m_token);
- }
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- CheckStrictModeEvalArgumentsUsage(pid);
- if (formals.Has(pid))
- {
- Error(ERRES5ArgSame, m_pscan->IchMinTok(), m_pscan->IchLimTok());
- }
- else
- {
- formals.Prepend(pid);
- }
- m_pscan->Scan();
- if (m_token.tk == tkAsg && m_scriptContext->GetConfig()->IsES6DefaultArgsEnabled())
- {
- m_pscan->Scan();
- // We can avoid building the AST since we are just checking the default expression.
- ParseNodePtr pnodeInit = ParseExpr<false>(koplCma);
- Assert(pnodeInit == nullptr);
- }
- if (m_token.tk != tkComma)
- {
- break;
- }
- m_pscan->ScanForcingPid();
- if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
- {
- break;
- }
- }
- }
- Assert(m_token.tk == tkRParen);
- }
- void Parser::FinishFncNode(ParseNodePtr pnodeFnc)
- {
- AnalysisAssert(pnodeFnc);
- // Finish the AST for a function that was deferred earlier, but which we decided
- // to finish after the fact.
- // We assume that the name(s) and arg(s) have already got parse nodes, so
- // we just have to do the function body.
- // Save the current next function Id, and resume from the old one.
- Js::LocalFunctionId * nextFunctionIdSave = m_nextFunctionId;
- Js::LocalFunctionId tempNextFunctionId = pnodeFnc->sxFnc.functionId + 1;
- this->m_nextFunctionId = &tempNextFunctionId;
- ParseNodePtr pnodeFncSave = m_currentNodeFunc;
- uint *pnestedCountSave = m_pnestedCount;
- long* pAstSizeSave = m_pCurrentAstSize;
- m_currentNodeFunc = pnodeFnc;
- m_pCurrentAstSize = & (pnodeFnc->sxFnc.astSize);
- pnodeFnc->sxFnc.nestedCount = 0;
- m_pnestedCount = &pnodeFnc->sxFnc.nestedCount;
- // Cue up the parser to the start of the function body.
- if (pnodeFnc->sxFnc.pnodeName)
- {
- // Skip the name(s).
- m_pscan->SetCurrentCharacter(pnodeFnc->sxFnc.pnodeName->ichLim, pnodeFnc->sxFnc.lineNumber);
- }
- else
- {
- m_pscan->SetCurrentCharacter(pnodeFnc->ichMin, pnodeFnc->sxFnc.lineNumber);
- if (pnodeFnc->sxFnc.IsAccessor())
- {
- // Getter/setter. The node text starts with the name, so eat that.
- m_pscan->ScanNoKeywords();
- }
- else
- {
- // Anonymous function. Skip any leading "("'s and "function".
- for (;;)
- {
- m_pscan->Scan();
- if (m_token.tk == tkFUNCTION)
- {
- break;
- }
- Assert(m_token.tk == tkLParen || m_token.tk == tkStar);
- }
- }
- }
- // switch scanner to treat 'yield' as keyword in generator functions
- // or as an identifier in non-generator functions
- bool fPreviousYieldIsKeyword = m_pscan->SetYieldIsKeyword(pnodeFnc && pnodeFnc->sxFnc.IsGenerator());
- bool fPreviousAwaitIsKeyword = m_pscan->SetAwaitIsKeyword(pnodeFnc && pnodeFnc->sxFnc.IsAsync());
- // Skip the arg list.
- m_pscan->ScanNoKeywords();
- if (m_token.tk == tkStar)
- {
- Assert(pnodeFnc->sxFnc.IsGenerator());
- m_pscan->ScanNoKeywords();
- }
- Assert(m_token.tk == tkLParen);
- m_pscan->ScanNoKeywords();
- if (m_token.tk != tkRParen)
- {
- for (;;)
- {
- if (m_token.tk == tkEllipsis)
- {
- m_pscan->ScanNoKeywords();
- }
- if (m_token.tk == tkID)
- {
- m_pscan->ScanNoKeywords();
- if (m_token.tk == tkAsg)
- {
- // Eat the default expression
- m_pscan->Scan();
- ParseExpr<false>(koplCma);
- }
- }
- else if (IsPossiblePatternStart())
- {
- ParseDestructuredLiteralWithScopeSave(tkLET, false/*isDecl*/, false /*topLevel*/);
- }
- else
- {
- AssertMsg(false, "Unexpected identifier prefix while fast-scanning formals");
- }
- if (m_token.tk != tkComma)
- {
- break;
- }
- m_pscan->ScanNoKeywords();
- if (m_token.tk == tkRParen && m_scriptContext->GetConfig()->IsES7TrailingCommaEnabled())
- {
- break;
- }
- }
- }
- if (m_token.tk == tkRParen) // This might be false due to a lambda => token.
- {
- m_pscan->Scan();
- }
- // Finish the function body.
- {
- // Note that in IE8- modes, surrounding parentheses are considered part of function body. e.g. "( function x(){} )".
- // We lose that context here since we start from middle of function body. So save and restore source range info.
- ParseNodePtr* lastNodeRef = NULL;
- const charcount_t ichLim = pnodeFnc->ichLim;
- const size_t cbLim = pnodeFnc->sxFnc.cbLim;
- this->FinishFncDecl(pnodeFnc, NULL, lastNodeRef);
- #if DBG
- // The pnode extent may not match the original extent.
- // We expect this to happen only when there are trailing ")"'s.
- // Consume them and make sure that's all we've got.
- if (pnodeFnc->ichLim != ichLim)
- {
- Assert(pnodeFnc->ichLim < ichLim);
- m_pscan->SetCurrentCharacter(pnodeFnc->ichLim);
- while (m_pscan->IchLimTok() != ichLim)
- {
- m_pscan->ScanNoKeywords();
- Assert(m_token.tk == tkRParen);
- }
- }
- #endif
- pnodeFnc->ichLim = ichLim;
- pnodeFnc->sxFnc.cbLim = cbLim;
- }
- m_currentNodeFunc = pnodeFncSave;
- m_pCurrentAstSize = pAstSizeSave;
- m_pnestedCount = pnestedCountSave;
- Assert(m_pnestedCount);
- Assert(tempNextFunctionId == pnodeFnc->sxFnc.deferredParseNextFunctionId);
- this->m_nextFunctionId = nextFunctionIdSave;
- m_pscan->SetYieldIsKeyword(fPreviousYieldIsKeyword);
- m_pscan->SetAwaitIsKeyword(fPreviousAwaitIsKeyword);
- }
- void Parser::FinishFncDecl(ParseNodePtr pnodeFnc, LPCOLESTR pNameHint, ParseNodePtr *lastNodeRef)
- {
- LPCOLESTR name = NULL;
- JS_ETW(long startAstSize = *m_pCurrentAstSize);
- if(IS_JS_ETW(EventEnabledJSCRIPT_PARSE_METHOD_START()) || PHASE_TRACE1(Js::DeferParsePhase))
- {
- name = GetFunctionName(pnodeFnc, pNameHint);
- m_functionBody = NULL; // for nested functions we do not want to get the name of the top deferred function return name;
- JS_ETW(EventWriteJSCRIPT_PARSE_METHOD_START(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), pnodeFnc->sxFnc.functionId, 0, m_parseType, name));
- OUTPUT_TRACE(Js::DeferParsePhase, _u("Parsing function (%s) : %s (%d)\n"), GetParseType(), name, pnodeFnc->sxFnc.functionId);
- }
- JS_ETW(EventWriteJSCRIPT_PARSE_FUNC(GetScriptContext(), pnodeFnc->sxFnc.functionId, /*Undefer*/FALSE));
- // Do the work of creating an AST for a function body.
- // This is common to the un-deferred case and the case in which we un-defer late in the game.
- Assert(pnodeFnc->nop == knopFncDecl);
- ChkCurTok(tkLCurly, ERRnoLcurly);
- if (pnodeFnc->sxFnc.IsAsync())
- {
- TransformAsyncFncDeclAST(&pnodeFnc->sxFnc.pnodeBody, false);
- }
- else
- {
- ParseStmtList<true>(&pnodeFnc->sxFnc.pnodeBody, &lastNodeRef, SM_OnFunctionCode, true /* isSourceElementList */);
- // Append an EndCode node.
- AddToNodeList(&pnodeFnc->sxFnc.pnodeBody, &lastNodeRef, CreateNodeWithScanner<knopEndCode>());
- }
- ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
- pnodeFnc->ichLim = m_pscan->IchLimTok();
- pnodeFnc->sxFnc.cbLim = m_pscan->IecpLimTok();
- // Restore the lists of scopes that contain function expressions.
- // Save the temps and restore the outer scope's list.
- // NOTE: Eze makes no use of this.
- //pnodeFnc->sxFnc.pnodeTmps = *m_ppnodeVar;
- #ifdef ENABLE_JS_ETW
- long astSize = *m_pCurrentAstSize - startAstSize;
- EventWriteJSCRIPT_PARSE_METHOD_STOP(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), pnodeFnc->sxFnc.functionId, astSize, m_parseType, name);
- #endif
- }
- void Parser::AddArgumentsNodeToVars(ParseNodePtr pnodeFnc)
- {
- if((pnodeFnc->grfpn & PNodeFlags::fpnArguments_overriddenByDecl) || pnodeFnc->sxFnc.IsLambda())
- {
- // In any of the following cases, there is no way to reference the built-in 'arguments' variable (in the order of checks
- // above):
- // - A function parameter is named 'arguments'
- // - There is a nested function declaration (or named function expression in compat modes) named 'arguments'
- // - In compat modes, the function is named arguments, does not have a var declaration named 'arguments', and does
- // not call 'eval'
- pnodeFnc->sxFnc.SetHasReferenceableBuiltInArguments(false);
- }
- else
- {
- if(m_ppnodeVar == &pnodeFnc->sxFnc.pnodeVars)
- {
- // There were no var declarations in the function
- CreateVarDeclNode(wellKnownPropertyPids.arguments, STVariable, true, pnodeFnc)->grfpn |= PNodeFlags::fpnArguments;
- }
- else
- {
- // There were var declarations in the function, so insert an 'arguments' local at the beginning of the var list.
- // This is done because the built-in 'arguments' variable overrides an 'arguments' var declaration until the
- // 'arguments' variable is assigned. By putting our built-in var declaration at the beginning, an 'arguments'
- // identifier will resolve to this symbol, which has the fpnArguments flag set, and will be the built-in arguments
- // object until it is replaced with something else.
- ParseNodePtr *const ppnodeVarSave = m_ppnodeVar;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- CreateVarDeclNode(wellKnownPropertyPids.arguments, STVariable, true, pnodeFnc)->grfpn |= PNodeFlags::fpnArguments;
- m_ppnodeVar = ppnodeVarSave;
- }
- pnodeFnc->sxFnc.SetHasReferenceableBuiltInArguments(true);
- }
- }
- LPCOLESTR Parser::GetFunctionName(ParseNodePtr pnodeFnc, LPCOLESTR pNameHint)
- {
- LPCOLESTR name = nullptr;
- if(pnodeFnc->sxFnc.pnodeName != nullptr && knopVarDecl == pnodeFnc->sxFnc.pnodeName->nop)
- {
- name = pnodeFnc->sxFnc.pnodeName->sxVar.pid->Psz();
- }
- if(name == nullptr && pNameHint != nullptr)
- {
- name = pNameHint;
- }
- if(name == nullptr && m_functionBody != nullptr)
- {
- name = m_functionBody->GetExternalDisplayName();
- }
- else if(name == nullptr)
- {
- name = Js::Constants::AnonymousFunction;
- }
- return name;
- }
- IdentPtr Parser::ParseClassPropertyName(IdentPtr * pidHint)
- {
- if (m_token.tk == tkID || m_token.tk == tkStrCon || m_token.IsReservedWord())
- {
- IdentPtr pid;
- if (m_token.tk == tkStrCon)
- {
- if (m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- pid = m_token.GetStr();
- }
- else
- {
- pid = m_token.GetIdentifier(m_phtbl);
- }
- *pidHint = pid;
- return pid;
- }
- else if (m_token.tk == tkIntCon)
- {
- if (m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- return m_pscan->PidFromLong(m_token.GetLong());
- }
- else if (m_token.tk == tkFltCon)
- {
- if (m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- return m_pscan->PidFromDbl(m_token.GetDouble());
- }
- Error(ERRnoMemberIdent);
- }
- LPCOLESTR Parser::ConstructFinalHintNode(IdentPtr pClassName, IdentPtr pMemberName, IdentPtr pGetSet, bool isStatic, ulong* nameLength, ulong* pShortNameOffset, bool isComputedName, LPCOLESTR pMemberNameHint)
- {
- if ((pMemberName == nullptr && !isComputedName) ||
- (pMemberNameHint == nullptr && isComputedName) ||
- !CONFIG_FLAG(UseFullName))
- {
- return nullptr;
- }
- LPCOLESTR pFinalName = isComputedName? pMemberNameHint : pMemberName->Psz();
- ulong fullNameHintLength = 0;
- ulong shortNameOffset = 0;
- if (!isStatic)
- {
- // Add prototype.
- pFinalName = AppendNameHints(wellKnownPropertyPids.prototype, pFinalName, &fullNameHintLength, &shortNameOffset);
- }
- if (pClassName)
- {
- ulong classNameOffset = 0;
- pFinalName = AppendNameHints(pClassName, pFinalName, &fullNameHintLength, &classNameOffset);
- shortNameOffset += classNameOffset;
- }
- if (pGetSet)
- {
- if (m_scriptContext->GetConfig()->IsES6FunctionNameEnabled())
- {
- // displays as get/set prototype.funcname
- ulong getSetOffset = 0;
- pFinalName = AppendNameHints(pGetSet, pFinalName, &fullNameHintLength, &getSetOffset, true);
- shortNameOffset += getSetOffset;
- }
- else
- {
- pFinalName = AppendNameHints(pFinalName, pGetSet, &fullNameHintLength, &shortNameOffset);
- }
- }
- if (fullNameHintLength > *nameLength)
- {
- *nameLength = fullNameHintLength;
- }
- if (shortNameOffset > *pShortNameOffset)
- {
- *pShortNameOffset = shortNameOffset;
- }
- return pFinalName;
- }
- class AutoParsingSuperRestrictionStateRestorer
- {
- public:
- AutoParsingSuperRestrictionStateRestorer(Parser* parser) : m_parser(parser)
- {
- AssertMsg(this->m_parser != nullptr, "This just should not happen");
- this->m_originalParsingSuperRestrictionState = this->m_parser->m_parsingSuperRestrictionState;
- }
- ~AutoParsingSuperRestrictionStateRestorer()
- {
- AssertMsg(this->m_parser != nullptr, "This just should not happen");
- this->m_parser->m_parsingSuperRestrictionState = m_originalParsingSuperRestrictionState;
- }
- private:
- Parser* m_parser;
- int m_originalParsingSuperRestrictionState;
- };
- template<bool buildAST>
- ParseNodePtr Parser::ParseClassDecl(BOOL isDeclaration, LPCOLESTR pNameHint, ulong *pHintLength, ulong *pShortNameOffset)
- {
- bool hasConstructor = false;
- bool hasExtends = false;
- IdentPtr name = nullptr;
- ParseNodePtr pnodeName = nullptr;
- ParseNodePtr pnodeConstructor = nullptr;
- ParseNodePtr pnodeExtends = nullptr;
- ParseNodePtr pnodeMembers = nullptr;
- ParseNodePtr *lastMemberNodeRef = nullptr;
- ParseNodePtr pnodeStaticMembers = nullptr;
- ParseNodePtr *lastStaticMemberNodeRef = nullptr;
- ulong nameHintLength = pHintLength ? *pHintLength : 0;
- ulong nameHintOffset = pShortNameOffset ? *pShortNameOffset : 0;
- ArenaAllocator tempAllocator(_u("ClassMemberNames"), m_nodeAllocator.GetPageAllocator(), Parser::OutOfMemory);
- ParseNodePtr pnodeClass = nullptr;
- if (buildAST)
- {
- pnodeClass = CreateNode(knopClassDecl);
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ClassCount, m_scriptContext);
- }
- m_pscan->Scan();
- if (m_token.tk == tkID)
- {
- name = m_token.GetIdentifier(m_phtbl);
- m_pscan->Scan();
- }
- else if (isDeclaration)
- {
- IdentifierExpectedError(m_token);
- }
- if (isDeclaration && name == wellKnownPropertyPids.arguments && GetCurrentBlockInfo()->pnodeBlock->sxBlock.blockType == Function)
- {
- GetCurrentFunctionNode()->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
- }
- BOOL strictSave = m_fUseStrictMode;
- m_fUseStrictMode = TRUE;
- if (m_token.tk == tkEXTENDS)
- {
- m_pscan->Scan();
- pnodeExtends = ParseExpr<buildAST>();
- hasExtends = true;
- }
- if (m_token.tk != tkLCurly)
- {
- Error(ERRnoLcurly);
- }
- OUTPUT_TRACE_DEBUGONLY(Js::ES6VerboseFlag, _u("Parsing class (%s) : %s\n"), GetParseType(), name ? name->Psz() : _u("anonymous class"));
- ParseNodePtr pnodeDeclName = nullptr;
- if (isDeclaration)
- {
- pnodeDeclName = CreateBlockScopedDeclNode(name, knopLetDecl);
- }
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- ParseNodePtr pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
- if (buildAST)
- {
- PushFuncBlockScope(pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
- pnodeClass->sxClass.pnodeBlock = pnodeBlock;
- }
- if (name)
- {
- pnodeName = CreateBlockScopedDeclNode(name, knopConstDecl);
- }
- RestorePoint beginClass;
- m_pscan->Capture(&beginClass);
- m_pscan->ScanForcingPid();
- IdentPtr pClassNamePid = pnodeName ? pnodeName->sxVar.pid : nullptr;
- for (;;)
- {
- if (m_token.tk == tkSColon)
- {
- m_pscan->ScanForcingPid();
- continue;
- }
- if (m_token.tk == tkRCurly)
- {
- break;
- }
- bool isStatic = m_token.tk == tkSTATIC;
- if (isStatic)
- {
- m_pscan->ScanForcingPid();
- }
- ushort fncDeclFlags = fFncNoName | fFncMethod | fFncClassMember;
- charcount_t ichMin = 0;
- size_t iecpMin = 0;
- ParseNodePtr pnodeMemberName = nullptr;
- IdentPtr pidHint = nullptr;
- IdentPtr memberPid = nullptr;
- LPCOLESTR pMemberNameHint = nullptr;
- ulong memberNameHintLength = 0;
- ulong memberNameOffset = 0;
- bool isComputedName = false;
- bool isAsyncMethod = false;
- if (m_token.tk == tkID && m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.async && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
- {
- RestorePoint parsedAsync;
- m_pscan->Capture(&parsedAsync);
- ichMin = m_pscan->IchMinTok();
- iecpMin = m_pscan->IecpMinTok();
- m_pscan->Scan();
- if (m_token.tk == tkLParen || m_pscan->FHadNewLine())
- {
- m_pscan->SeekTo(parsedAsync);
- }
- else
- {
- isAsyncMethod = true;
- }
- }
- bool isGenerator = m_scriptContext->GetConfig()->IsES6GeneratorsEnabled() &&
- m_token.tk == tkStar;
- if (isGenerator)
- {
- fncDeclFlags |= fFncGenerator;
- m_pscan->ScanForcingPid();
- }
- if (m_token.tk == tkLBrack && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
- {
- // Computed member name: [expr] () { }
- LPCOLESTR emptyHint = nullptr;
- ParseComputedName<buildAST>(&pnodeMemberName, &emptyHint, &pMemberNameHint, &memberNameHintLength, &memberNameOffset);
- isComputedName = true;
- }
- else // not computed name
- {
- memberPid = this->ParseClassPropertyName(&pidHint);
- if (pidHint)
- {
- pMemberNameHint = pidHint->Psz();
- memberNameHintLength = pidHint->Cch();
- }
- }
- if (buildAST && memberPid)
- {
- pnodeMemberName = CreateStrNodeWithScanner(memberPid);
- }
- if (!isStatic && memberPid == wellKnownPropertyPids.constructor)
- {
- if (hasConstructor || isAsyncMethod)
- {
- Error(ERRsyntax);
- }
- hasConstructor = true;
- LPCOLESTR pConstructorName = nullptr;
- ulong constructorNameLength = 0;
- ulong constructorShortNameHintOffset = 0;
- if (pnodeName && pnodeName->sxVar.pid)
- {
- pConstructorName = pnodeName->sxVar.pid->Psz();
- constructorNameLength = pnodeName->sxVar.pid->Cch();
- }
- else
- {
- pConstructorName = pNameHint;
- constructorNameLength = nameHintLength;
- constructorShortNameHintOffset = nameHintOffset;
- }
- {
- AutoParsingSuperRestrictionStateRestorer restorer(this);
- this->m_parsingSuperRestrictionState = hasExtends ? ParsingSuperRestrictionState_SuperCallAndPropertyAllowed : ParsingSuperRestrictionState_SuperPropertyAllowed;
- pnodeConstructor = ParseFncDecl<buildAST>(fncDeclFlags, pConstructorName, /* needsPIDOnRCurlyScan */ true, /* resetParsingSuperRestrictionState = */false);
- }
- if (pnodeConstructor->sxFnc.IsGenerator())
- {
- Error(ERRConstructorCannotBeGenerator);
- }
- Assert(constructorNameLength >= constructorShortNameHintOffset);
- // The constructor function will get the same name as class.
- pnodeConstructor->sxFnc.hint = pConstructorName;
- pnodeConstructor->sxFnc.hintLength = constructorNameLength;
- pnodeConstructor->sxFnc.hintOffset = constructorShortNameHintOffset;
- pnodeConstructor->sxFnc.pid = pnodeName && pnodeName->sxVar.pid ? pnodeName->sxVar.pid : wellKnownPropertyPids.constructor;
- pnodeConstructor->sxFnc.SetIsClassConstructor(TRUE);
- pnodeConstructor->sxFnc.SetIsBaseClassConstructor(pnodeExtends == nullptr);
- }
- else
- {
- ParseNodePtr pnodeMember = nullptr;
- bool isMemberNamedGetOrSet = false;
- RestorePoint beginMethodName;
- m_pscan->Capture(&beginMethodName);
- if (memberPid == wellKnownPropertyPids.get || memberPid == wellKnownPropertyPids.set)
- {
- m_pscan->ScanForcingPid();
- }
- if (m_token.tk == tkLParen)
- {
- m_pscan->SeekTo(beginMethodName);
- isMemberNamedGetOrSet = true;
- }
- if ((memberPid == wellKnownPropertyPids.get || memberPid == wellKnownPropertyPids.set) && !isMemberNamedGetOrSet)
- {
- bool isGetter = (memberPid == wellKnownPropertyPids.get);
- if (m_token.tk == tkLBrack && m_scriptContext->GetConfig()->IsES6ObjectLiteralsEnabled())
- {
- // Computed get/set member name: get|set [expr] () { }
- LPCOLESTR emptyHint = nullptr;
- ParseComputedName<buildAST>(&pnodeMemberName, &emptyHint, &pMemberNameHint, &memberNameHintLength, &memberNameOffset);
- isComputedName = true;
- }
- else // not computed name
- {
- memberPid = this->ParseClassPropertyName(&pidHint);
- }
- if ((isStatic ? (memberPid == wellKnownPropertyPids.prototype) : (memberPid == wellKnownPropertyPids.constructor)) || isAsyncMethod)
- {
- Error(ERRsyntax);
- }
- if (buildAST && memberPid && !isComputedName)
- {
- pnodeMemberName = CreateStrNodeWithScanner(memberPid);
- }
- ParseNodePtr pnodeFnc = nullptr;
- {
- AutoParsingSuperRestrictionStateRestorer restorer(this);
- this->m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperPropertyAllowed;
- pnodeFnc = ParseFncDecl<buildAST>(fncDeclFlags | (isGetter ? fFncNoArg : fFncOneArg),
- pidHint ? pidHint->Psz() : nullptr, /* needsPIDOnRCurlyScan */ true,
- /* resetParsingSuperRestrictionState */false);
- }
- pnodeFnc->sxFnc.SetIsStaticMember(isStatic);
- if (buildAST)
- {
- pnodeFnc->sxFnc.SetIsAccessor();
- pnodeMember = CreateBinNode(isGetter ? knopGetMember : knopSetMember, pnodeMemberName, pnodeFnc);
- pMemberNameHint = ConstructFinalHintNode(pClassNamePid, pidHint,
- isGetter ? wellKnownPropertyPids.get : wellKnownPropertyPids.set, isStatic,
- &memberNameHintLength, &memberNameOffset, isComputedName, pMemberNameHint);
- }
- }
- else
- {
- if (isStatic && (memberPid == wellKnownPropertyPids.prototype))
- {
- Error(ERRsyntax);
- }
- ParseNodePtr pnodeFnc = nullptr;
- {
- AutoParsingSuperRestrictionStateRestorer restorer(this);
- this->m_parsingSuperRestrictionState = ParsingSuperRestrictionState_SuperPropertyAllowed;
- if (isAsyncMethod)
- {
- fncDeclFlags |= fFncAsync;
- }
- pnodeFnc = ParseFncDecl<buildAST>(fncDeclFlags, pidHint ? pidHint->Psz() : nullptr, /* needsPIDOnRCurlyScan */ true, /* resetParsingSuperRestrictionState */false);
- if (isAsyncMethod)
- {
- pnodeFnc->sxFnc.cbMin = iecpMin;
- pnodeFnc->ichMin = ichMin;
- }
- }
- pnodeFnc->sxFnc.SetIsStaticMember(isStatic);
- if (buildAST)
- {
- pnodeMember = CreateBinNode(knopMember, pnodeMemberName, pnodeFnc);
- pMemberNameHint = ConstructFinalHintNode(pClassNamePid, pidHint, nullptr /*pgetset*/, isStatic, &memberNameHintLength, &memberNameOffset, isComputedName, pMemberNameHint);
- }
- }
- if (buildAST)
- {
- Assert(memberNameHintLength >= memberNameOffset);
- pnodeMember->sxBin.pnode2->sxFnc.hint = pMemberNameHint; // Fully qualified name
- pnodeMember->sxBin.pnode2->sxFnc.hintLength = memberNameHintLength;
- pnodeMember->sxBin.pnode2->sxFnc.hintOffset = memberNameOffset;
- pnodeMember->sxBin.pnode2->sxFnc.pid = memberPid; // Short name
- AddToNodeList(isStatic ? &pnodeStaticMembers : &pnodeMembers, isStatic ? &lastStaticMemberNodeRef : &lastMemberNodeRef, pnodeMember);
- }
- }
- }
- if (buildAST)
- {
- pnodeClass->ichLim = m_pscan->IchLimTok();
- }
- if (!hasConstructor)
- {
- OUTPUT_TRACE_DEBUGONLY(Js::ES6VerboseFlag, _u("Generating constructor (%s) : %s\n"), GetParseType(), name ? name->Psz() : _u("anonymous class"));
- RestorePoint endClass;
- m_pscan->Capture(&endClass);
- m_pscan->SeekTo(beginClass);
- pnodeConstructor = GenerateEmptyConstructor<buildAST>(pnodeExtends != nullptr);
- if (buildAST)
- {
- if (pClassNamePid)
- {
- pnodeConstructor->sxFnc.hint = pClassNamePid->Psz();
- pnodeConstructor->sxFnc.hintLength = pClassNamePid->Cch();
- pnodeConstructor->sxFnc.hintOffset = 0;
- }
- else
- {
- Assert(nameHintLength >= nameHintOffset);
- pnodeConstructor->sxFnc.hint = pNameHint;
- pnodeConstructor->sxFnc.hintLength = nameHintLength;
- pnodeConstructor->sxFnc.hintOffset = nameHintOffset;
- }
- pnodeConstructor->sxFnc.pid = pClassNamePid;
- }
- m_pscan->SeekTo(endClass);
- }
- if (buildAST)
- {
- pnodeConstructor->sxFnc.cbMin = pnodeClass->ichMin;
- pnodeConstructor->sxFnc.cbLim = pnodeClass->ichLim;
- pnodeConstructor->ichMin = pnodeClass->ichMin;
- pnodeConstructor->ichLim = pnodeClass->ichLim;
- PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
- pnodeClass->sxClass.pnodeDeclName = pnodeDeclName;
- pnodeClass->sxClass.pnodeName = pnodeName;
- pnodeClass->sxClass.pnodeConstructor = pnodeConstructor;
- pnodeClass->sxClass.pnodeExtends = pnodeExtends;
- pnodeClass->sxClass.pnodeMembers = pnodeMembers;
- pnodeClass->sxClass.pnodeStaticMembers = pnodeStaticMembers;
- pnodeClass->sxClass.isDefaultModuleExport = false;
- }
- FinishParseBlock(pnodeBlock);
- m_fUseStrictMode = strictSave;
- m_pscan->Scan();
- return pnodeClass;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseStringTemplateDecl(ParseNodePtr pnodeTagFnc)
- {
- ParseNodePtr pnodeStringLiterals = nullptr;
- ParseNodePtr* lastStringLiteralNodeRef = nullptr;
- ParseNodePtr pnodeRawStringLiterals = nullptr;
- ParseNodePtr* lastRawStringLiteralNodeRef = nullptr;
- ParseNodePtr pnodeSubstitutionExpressions = nullptr;
- ParseNodePtr* lastSubstitutionExpressionNodeRef = nullptr;
- ParseNodePtr pnodeTagFncArgs = nullptr;
- ParseNodePtr* lastTagFncArgNodeRef = nullptr;
- ParseNodePtr stringLiteral = nullptr;
- ParseNodePtr stringLiteralRaw = nullptr;
- ParseNodePtr pnodeStringTemplate = nullptr;
- bool templateClosed = false;
- const bool isTagged = pnodeTagFnc != nullptr;
- uint16 stringConstantCount = 0;
- charcount_t ichMin = 0;
- Assert(m_token.tk == tkStrTmplBasic || m_token.tk == tkStrTmplBegin);
- if (buildAST)
- {
- pnodeStringTemplate = CreateNode(knopStrTemplate);
- pnodeStringTemplate->sxStrTemplate.countStringLiterals = 0;
- pnodeStringTemplate->sxStrTemplate.isTaggedTemplate = isTagged ? TRUE : FALSE;
- // If this is a tagged string template, we need to start building the arg list for the call
- if (isTagged)
- {
- ichMin = pnodeTagFnc->ichMin;
- AddToNodeListEscapedUse(&pnodeTagFncArgs, &lastTagFncArgNodeRef, pnodeStringTemplate);
- }
- }
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(StringTemplatesCount, m_scriptContext);
- OUTPUT_TRACE_DEBUGONLY(
- Js::StringTemplateParsePhase,
- _u("Starting to parse a string template (%s)...\n\tis tagged = %s\n"),
- GetParseType(),
- isTagged ? _u("true") : _u("false (Raw and cooked strings will not differ!)"));
- // String template grammar
- // `...` Simple string template
- // `...${ String template beginning
- // }...${ String template middle
- // }...` String template end
- while (!templateClosed)
- {
- // First, extract the string constant part - we always have one
- if (IsStrictMode() && m_pscan->IsOctOrLeadingZeroOnLastTKNumber())
- {
- Error(ERRES5NoOctal);
- }
- // We are not able to pass more than a ushort worth of arguments to the tag
- // so use that as a logical limit on the number of string constant pieces.
- if (stringConstantCount >= USHRT_MAX)
- {
- Error(ERRnoMemory);
- }
- // Keep track of the string literal count (must be the same for raw strings)
- // We use this in code gen so we don't need to count the string literals list
- stringConstantCount++;
- // If we are not creating parse nodes, there is no need to create strings
- if (buildAST)
- {
- stringLiteral = CreateStrNodeWithScanner(m_token.GetStr());
- AddToNodeList(&pnodeStringLiterals, &lastStringLiteralNodeRef, stringLiteral);
- // We only need to collect a raw string when we are going to pass the string template to a tag
- if (isTagged)
- {
- // Make the scanner create a PID for the raw string constant for the preceding scan
- IdentPtr pid = m_pscan->GetSecondaryBufferAsPid();
- stringLiteralRaw = CreateStrNodeWithScanner(pid);
- // Should have gotten a raw string literal above
- AddToNodeList(&pnodeRawStringLiterals, &lastRawStringLiteralNodeRef, stringLiteralRaw);
- }
- else
- {
- #if DBG
- // Assign the raw string for debug tracing below
- stringLiteralRaw = stringLiteral;
- #endif
- }
- OUTPUT_TRACE_DEBUGONLY(
- Js::StringTemplateParsePhase,
- _u("Parsed string constant: \n\tcooked = \"%s\" \n\traw = \"%s\" \n\tdiffer = %d\n"),
- stringLiteral->sxPid.pid->Psz(),
- stringLiteralRaw->sxPid.pid->Psz(),
- stringLiteral->sxPid.pid->Psz() == stringLiteralRaw->sxPid.pid->Psz() ? 0 : 1);
- }
- switch (m_token.tk)
- {
- case tkStrTmplEnd:
- case tkStrTmplBasic:
- // We do not need to parse an expression for either the end or basic string template tokens
- templateClosed = true;
- break;
- case tkStrTmplBegin:
- case tkStrTmplMid:
- {
- // In the middle or begin string template token case, we need to parse an expression next
- m_pscan->Scan();
- // Parse the contents of the curly braces as an expression
- ParseNodePtr expression = ParseExpr<buildAST>(0);
- // After parsing expression, scan should leave us with an RCurly token.
- // Use the NoScan version so we do not automatically perform a scan - we need to
- // set the scan state before next scan but we don't want to set that state if
- // the token is not as expected since we'll error in that case.
- ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
- // Notify the scanner that it should scan for a middle or end string template token
- m_pscan->SetScanState(Scanner_t::ScanState::ScanStateStringTemplateMiddleOrEnd);
- m_pscan->Scan();
- if (buildAST)
- {
- // If we are going to call the tag function, add this expression into the list of args
- if (isTagged)
- {
- AddToNodeListEscapedUse(&pnodeTagFncArgs, &lastTagFncArgNodeRef, expression);
- }
- else
- {
- // Otherwise add it to the substitution expression list
- // TODO: Store the arguments and substitution expressions in a single list?
- AddToNodeList(&pnodeSubstitutionExpressions, &lastSubstitutionExpressionNodeRef, expression);
- }
- }
- if (!(m_token.tk == tkStrTmplMid || m_token.tk == tkStrTmplEnd))
- {
- // Scan with ScanState ScanStateStringTemplateMiddleOrEnd should only return
- // tkStrTmpMid/End unless it is EOF or tkScanError
- Assert(m_token.tk == tkEOF || m_token.tk == tkScanError);
- Error(ERRsyntax);
- }
- OUTPUT_TRACE_DEBUGONLY(Js::StringTemplateParsePhase, _u("Parsed expression\n"));
- }
- break;
- default:
- Assert(false);
- break;
- }
- }
- if (buildAST)
- {
- pnodeStringTemplate->sxStrTemplate.pnodeStringLiterals = pnodeStringLiterals;
- pnodeStringTemplate->sxStrTemplate.pnodeStringRawLiterals = pnodeRawStringLiterals;
- pnodeStringTemplate->sxStrTemplate.pnodeSubstitutionExpressions = pnodeSubstitutionExpressions;
- pnodeStringTemplate->sxStrTemplate.countStringLiterals = stringConstantCount;
- // We should still have the last string literal.
- // Use the char offset of the end of that constant as the end of the string template.
- pnodeStringTemplate->ichLim = stringLiteral->ichLim;
- // If this is a tagged template, we now have the argument list and can construct a call node
- if (isTagged)
- {
- // Return the call node here and let the byte code generator Emit the string template automagically
- pnodeStringTemplate = CreateCallNode(knopCall, pnodeTagFnc, pnodeTagFncArgs, ichMin, pnodeStringTemplate->ichLim);
- // We need to set the arg count explicitly
- pnodeStringTemplate->sxCall.argCount = stringConstantCount;
- }
- }
- m_pscan->Scan();
- return pnodeStringTemplate;
- }
- void Parser::TransformAsyncFncDeclAST(ParseNodePtr *pnodeBody, bool fLambda)
- {
- StmtNest *pstmtSave;
- ParseNodePtr pnodeReturn;
- ParseNodePtr pnodeAsyncSpawn;
- ParseNodePtr pnodeFncGenerator = nullptr;
- ParseNodePtr pnodeFncSave = nullptr;
- ParseNodePtr pnodeDeferredFncSave = nullptr;
- ParseNodePtr pnodeInnerBlock = nullptr;
- ParseNodePtr pnodeBlock = nullptr;
- ParseNodePtr *lastNodeRef = nullptr;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- AutoParsingSuperRestrictionStateRestorer restorer(this);
- // Create the generator : function*() {}
- uint tryCatchOrFinallyDepthSave = this->m_tryCatchOrFinallyDepth;
- this->m_tryCatchOrFinallyDepth = 0;
- uint scopeCountNoAstSave = m_scopeCountNoAst;
- m_scopeCountNoAst = 0;
- long* pAstSizeSave = m_pCurrentAstSize;
- pnodeFncSave = m_currentNodeFunc;
- pnodeDeferredFncSave = m_currentNodeDeferredFunc;
- bool hasNonSimpleParameterList = m_currentNodeFunc->sxFnc.HasNonSimpleParameterList();
- pnodeFncGenerator = CreateAsyncSpawnGenerator();
- pstmtSave = m_pstmtCur;
- SetCurrentStatement(nullptr);
- bool fPreviousYieldIsKeyword = m_pscan->SetYieldIsKeyword(FALSE);
- uint uDeferSave = m_grfscr & fscrDeferFncParse;
- pnodeBlock = StartParseBlock<true>(PnodeBlockType::Parameter, ScopeType_Parameter);
- pnodeFncGenerator->sxFnc.pnodeScopes = pnodeBlock;
- m_ppnodeVar = &pnodeFncGenerator->sxFnc.pnodeParams;
- ppnodeScopeSave = m_ppnodeScope;
- m_ppnodeScope = &pnodeBlock->sxBlock.pnodeScopes;
- pnodeBlock->sxBlock.pnodeStmt = pnodeFncGenerator;
- ppnodeExprScopeSave = m_ppnodeExprScope;
- m_ppnodeExprScope = nullptr;
- // Push the formal parameter symbols again for the inner generator to get proper
- // redeclaration semantics (error for let/const locals, merge for var locals)
- Scope* paramScope = pnodeFncSave->sxFnc.pnodeScopes->sxBlock.scope;
- paramScope->ForEachSymbol([this](Symbol* paramSym)
- {
- Symbol* sym = paramSym->GetPid()->GetTopRef()->GetSym();
- PidRefStack* ref = PushPidRef(paramSym->GetPid());
- ref->SetSym(sym);
- });
- pnodeInnerBlock = StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
- *m_ppnodeScope = pnodeInnerBlock;
- pnodeFncGenerator->sxFnc.pnodeBodyScope = pnodeInnerBlock;
- m_ppnodeScope = &pnodeInnerBlock->sxBlock.pnodeScopes;
- pnodeInnerBlock->sxBlock.pnodeStmt = pnodeFncGenerator;
- Assert(*m_ppnodeVar == nullptr);
- pnodeFncGenerator->sxFnc.pnodeVars = nullptr;
- m_ppnodeVar = &pnodeFncGenerator->sxFnc.pnodeVars;
- DeferredFunctionStub *saveCurrentStub = m_currDeferredStub;
- if (pnodeFncSave && m_currDeferredStub)
- {
- m_currDeferredStub = (m_currDeferredStub + (pnodeFncSave->sxFnc.nestedCount - 1))->deferredStubs;
- }
- // It is an error if the async function contains a "use strict" directive and has
- // a non simple parameter list. Since we split the body from the parameters by the
- // synthetic inner generator function, temporarily set the HasNonSimpleParameterList
- // flag on the inner generator for the duration of parsing the body so that "use strict"
- // will trigger the corresponding syntax error. Unset it afterwards since it has
- // meaning post-parsing that won't match the actual parameter list of the generator.
- pnodeFncGenerator->sxFnc.SetHasNonSimpleParameterList(hasNonSimpleParameterList);
- // We always merge the param scope and body scope for async methods right now.
- // So adding an additional reference for the param symbols to the body.
- paramScope->ForEachSymbol([this] (Symbol* param)
- {
- Symbol* sym = param->GetPid()->GetTopRef()->GetSym();
- PidRefStack* ref = PushPidRef(param->GetPid());
- ref->SetSym(sym);
- });
- pnodeFncGenerator->sxFnc.pnodeBody = nullptr;
- if (fLambda)
- {
- // Parse and set the function body
- ParseExpressionLambdaBody<true>(*pnodeBody);
- AddToNodeList(&pnodeFncGenerator->sxFnc.pnodeBody, &lastNodeRef, (*pnodeBody)->sxFnc.pnodeScopes->sxBlock.pnodeStmt);
- }
- else
- {
- // Parse the function body
- ParseStmtList<true>(&pnodeFncGenerator->sxFnc.pnodeBody, &lastNodeRef, SM_OnFunctionCode, true);
- ChkCurTokNoScan(tkRCurly, ERRnoRcurly);
- }
- AddToNodeList(&pnodeFncGenerator->sxFnc.pnodeBody, &lastNodeRef, CreateNodeWithScanner<knopEndCode>());
- lastNodeRef = NULL;
- pnodeFncGenerator->sxFnc.SetHasNonSimpleParameterList(false);
- pnodeFncGenerator->ichLim = m_pscan->IchLimTok();
- pnodeFncGenerator->sxFnc.cbLim = m_pscan->IecpLimTok();
- m_currDeferredStub = saveCurrentStub;
- FinishParseBlock(pnodeInnerBlock, true);
- this->AddArgumentsNodeToVars(pnodeFncGenerator);
- Assert(m_ppnodeExprScope == nullptr || *m_ppnodeExprScope == nullptr);
- m_ppnodeExprScope = ppnodeExprScopeSave;
- AssertMem(m_ppnodeScope);
- Assert(nullptr == *m_ppnodeScope);
- m_ppnodeScope = ppnodeScopeSave;
- FinishParseBlock(pnodeBlock, true);
- Assert(nullptr == m_pstmtCur);
- SetCurrentStatement(pstmtSave);
- if (!m_stoppedDeferredParse)
- {
- m_grfscr |= uDeferSave;
- }
- m_pscan->SetYieldIsKeyword(fPreviousYieldIsKeyword);
- Assert(pnodeFncGenerator == m_currentNodeFunc);
- m_currentNodeFunc = pnodeFncSave;
- m_currentNodeDeferredFunc = pnodeDeferredFncSave;
- m_pCurrentAstSize = pAstSizeSave;
- m_inDeferredNestedFunc = false;
- m_scopeCountNoAst = scopeCountNoAstSave;
- this->m_tryCatchOrFinallyDepth = tryCatchOrFinallyDepthSave;
- // Create the call : spawn(function*() {}, this)
- pnodeAsyncSpawn = CreateBinNode(knopAsyncSpawn, pnodeFncGenerator, CreateNodeWithScanner<knopThis>());
- // Create the return : return spawn(function*() {}, this)
- pnodeReturn = CreateNodeWithScanner<knopReturn>();
- pnodeReturn->sxStmt.grfnop = 0;
- pnodeReturn->sxStmt.pnodeOuter = nullptr;
- pnodeReturn->sxReturn.pnodeExpr = pnodeAsyncSpawn;
- if (fLambda)
- {
- (*pnodeBody)->sxFnc.pnodeScopes->sxBlock.pnodeStmt = nullptr;
- AddToNodeList(&(*pnodeBody)->sxFnc.pnodeScopes->sxBlock.pnodeStmt, &lastNodeRef, pnodeReturn);
- }
- else
- {
- *pnodeBody = nullptr;
- AddToNodeList(pnodeBody, &lastNodeRef, pnodeReturn);
- AddToNodeList(pnodeBody, &lastNodeRef, CreateNodeWithScanner<knopEndCode>());
- }
- if (pnodeFncGenerator->sxFnc.GetStrictMode())
- {
- GetCurrentFunctionNode()->sxFnc.SetStrictMode();
- }
- if (pnodeFncGenerator->sxFnc.UsesArguments())
- {
- GetCurrentFunctionNode()->sxFnc.SetUsesArguments();
- }
- if (pnodeFncGenerator->sxFnc.CallsEval() || pnodeFncGenerator->sxFnc.ChildCallsEval())
- {
- GetCurrentFunctionNode()->sxFnc.SetChildCallsEval();
- }
- lastNodeRef = NULL;
- }
- ParseNodePtr Parser::CreateAsyncSpawnGenerator()
- {
- ParseNodePtr pnodeFncGenerator = nullptr;
- pnodeFncGenerator = CreateDummyFuncNode(false);
- pnodeFncGenerator->sxFnc.functionId = (*m_nextFunctionId)++;
- pnodeFncGenerator->sxFnc.cbMin = m_pscan->IecpMinTok();
- pnodeFncGenerator->sxFnc.cbLim = m_pscan->IecpLimTok();
- pnodeFncGenerator->sxFnc.lineNumber = m_pscan->LineCur();
- pnodeFncGenerator->sxFnc.columnNumber = CalculateFunctionColumnNumber();
- pnodeFncGenerator->sxFnc.SetNested(m_currentNodeFunc != nullptr);
- pnodeFncGenerator->sxFnc.SetStrictMode(IsStrictMode());
- pnodeFncGenerator->sxFnc.SetIsGenerator();
- pnodeFncGenerator->sxFnc.SetIsLambda();
- pnodeFncGenerator->sxFnc.scope = nullptr;
- AppendFunctionToScopeList(false, pnodeFncGenerator);
- return pnodeFncGenerator;
- }
- LPCOLESTR Parser::FormatPropertyString(LPCOLESTR propertyString, ParseNodePtr pNode, ulong *fullNameHintLength, ulong *pShortNameOffset)
- {
- // propertyString could be null, such as 'this.foo' =
- // propertyString could be empty, found in pattern as in (-1)[""][(x = z)]
- OpCode op = pNode->nop;
- LPCOLESTR rightNode = nullptr;
- if (propertyString == nullptr)
- {
- propertyString = _u("");
- }
- if (op != knopInt && op != knopFlt && op != knopName && op != knopStr)
- {
- rightNode = _u("");
- }
- else if (op == knopStr)
- {
- return AppendNameHints(propertyString, pNode->sxPid.pid, fullNameHintLength, pShortNameOffset, false, true/*add brackets*/);
- }
- else if(op == knopFlt)
- {
- rightNode = m_pscan->StringFromDbl(pNode->sxFlt.dbl);
- }
- else
- {
- rightNode = op == knopInt ? m_pscan->StringFromLong(pNode->sxInt.lw)
- : pNode->sxPid.pid->Psz();
- }
- return AppendNameHints(propertyString, rightNode, fullNameHintLength, pShortNameOffset, false, true/*add brackets*/);
- }
- LPCOLESTR Parser::ConstructNameHint(ParseNodePtr pNode, ulong* fullNameHintLength, ulong *pShortNameOffset)
- {
- Assert(pNode != nullptr);
- Assert(pNode->nop == knopDot || pNode->nop == knopIndex);
- LPCOLESTR leftNode = nullptr;
- if (pNode->sxBin.pnode1->nop == knopDot || pNode->sxBin.pnode1->nop == knopIndex)
- {
- leftNode = ConstructNameHint(pNode->sxBin.pnode1, fullNameHintLength, pShortNameOffset);
- }
- else if (pNode->sxBin.pnode1->nop == knopName)
- {
- leftNode = pNode->sxBin.pnode1->sxPid.pid->Psz();
- *fullNameHintLength = pNode->sxBin.pnode1->sxPid.pid->Cch();
- *pShortNameOffset = 0;
- }
- if (pNode->nop == knopIndex)
- {
- return FormatPropertyString(
- leftNode ? leftNode : Js::Constants::AnonymousFunction, // e.g. f()[0] = function () {}
- pNode->sxBin.pnode2, fullNameHintLength, pShortNameOffset);
- }
- Assert(pNode->sxBin.pnode2->nop == knopDot || pNode->sxBin.pnode2->nop == knopName);
- LPCOLESTR rightNode = nullptr;
- bool wrapWithBrackets = false;
- if (pNode->sxBin.pnode2->nop == knopDot)
- {
- rightNode = ConstructNameHint(pNode->sxBin.pnode2, fullNameHintLength, pShortNameOffset);
- }
- else
- {
- rightNode = pNode->sxBin.pnode2->sxPid.pid->Psz();
- wrapWithBrackets = PNodeFlags::fpnIndexOperator == (pNode->grfpn & PNodeFlags::fpnIndexOperator);
- }
- Assert(rightNode != nullptr);
- return AppendNameHints(leftNode, rightNode, fullNameHintLength, pShortNameOffset, false, wrapWithBrackets);
- }
- LPCOLESTR Parser::AppendNameHints(LPCOLESTR leftStr, ulong leftLen, LPCOLESTR rightStr, ulong rightLen, ulong *pNameLength, ulong *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
- {
- Assert(rightStr != nullptr);
- Assert(leftLen != 0 || wrapInBrackets);
- Assert(rightLen != 0 || wrapInBrackets);
- 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
- ulong totalLength = leftLen + rightLen + ((ignoreDot) ? 1 : 2); // 1 (for dot or [) + 1 (for null termination)
- if (wrapInBrackets)
- {
- totalLength++; //1 for ']';
- }
- WCHAR * finalName = AllocateStringOfLength(totalLength);
- if (leftStr != nullptr && leftLen != 0)
- {
- wcscpy_s(finalName, leftLen + 1, leftStr);
- }
- if (ignoreAddDotWithSpace)
- {
- finalName[leftLen++] = (OLECHAR)_u(' ');
- }
- // mutually exclusive from ignoreAddDotWithSpace which is used for getters/setters
- else if (wrapInBrackets)
- {
- finalName[leftLen++] = (OLECHAR)_u('[');
- finalName[totalLength-2] = (OLECHAR)_u(']');
- }
- else if (!ignoreDot)
- {
- finalName[leftLen++] = (OLECHAR)_u('.');
- }
- //ignore case falls through
- js_wmemcpy_s(finalName + leftLen, rightLen, rightStr, rightLen);
- finalName[totalLength-1] = (OLECHAR)_u('\0');
- if (pNameLength != nullptr)
- {
- *pNameLength = totalLength - 1;
- }
- if (pShortNameOffset != nullptr)
- {
- *pShortNameOffset = leftLen;
- }
- return finalName;
- }
- WCHAR * Parser::AllocateStringOfLength(ulong length)
- {
- Assert(length > 0);
- ULONG totalBytes;
- if (ULongMult(length, sizeof(OLECHAR), &totalBytes) != S_OK)
- {
- Error(ERRnoMemory);
- }
- WCHAR* finalName = (WCHAR*)m_phtbl->GetAllocator()->Alloc(totalBytes);
- if (finalName == nullptr)
- {
- Error(ERRnoMemory);
- }
- return finalName;
- }
- LPCOLESTR Parser::AppendNameHints(IdentPtr left, IdentPtr right, ulong *pNameLength, ulong *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
- {
- if (pShortNameOffset != nullptr)
- {
- *pShortNameOffset = 0;
- }
- if (left == nullptr && !wrapInBrackets)
- {
- if (right)
- {
- *pNameLength = right->Cch();
- return right->Psz();
- }
- return nullptr;
- }
- ulong leftLen = 0;
- LPCOLESTR leftStr = _u("");
- if (left != nullptr) // if wrapInBrackets is true
- {
- leftStr = left->Psz();
- leftLen = left->Cch();
- }
- if (right == nullptr)
- {
- *pNameLength = leftLen;
- return left->Psz();
- }
- ulong rightLen = right->Cch();
- return AppendNameHints(leftStr, leftLen, right->Psz(), rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
- }
- LPCOLESTR Parser::AppendNameHints(IdentPtr left, LPCOLESTR right, ulong *pNameLength, ulong *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
- {
- ulong rightLen = (right == nullptr) ? 0 : (ulong) wcslen(right);
- if (pShortNameOffset != nullptr)
- {
- *pShortNameOffset = 0;
- }
- Assert(rightLen <= ULONG_MAX); // name hints should not exceed ULONG_MAX characters
- if (left == nullptr && !wrapInBrackets)
- {
- *pNameLength = rightLen;
- return right;
- }
- LPCOLESTR leftStr = _u("");
- ulong leftLen = 0;
- if (left != nullptr) // if wrapInBrackets is true
- {
- leftStr = left->Psz();
- leftLen = left->Cch();
- }
- if (rightLen == 0 && !wrapInBrackets)
- {
- *pNameLength = leftLen;
- return left->Psz();
- }
- return AppendNameHints(leftStr, leftLen, right, rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
- }
- LPCOLESTR Parser::AppendNameHints(LPCOLESTR left, IdentPtr right, ulong *pNameLength, ulong *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
- {
- ulong leftLen = (left == nullptr) ? 0 : (ulong) wcslen(left);
- if (pShortNameOffset != nullptr)
- {
- *pShortNameOffset = 0;
- }
- Assert(leftLen <= ULONG_MAX); // name hints should not exceed ULONG_MAX characters
- if (left == nullptr || (leftLen == 0 && !wrapInBrackets))
- {
- if (right != nullptr)
- {
- *pNameLength = right->Cch();
- return right->Psz();
- }
- return nullptr;
- }
- if (right == nullptr)
- {
- *pNameLength = leftLen;
- return left;
- }
- ulong rightLen = right->Cch();
- return AppendNameHints(left, leftLen, right->Psz(), rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
- }
- LPCOLESTR Parser::AppendNameHints(LPCOLESTR left, LPCOLESTR right, ulong *pNameLength, ulong *pShortNameOffset, bool ignoreAddDotWithSpace, bool wrapInBrackets)
- {
- ulong leftLen = (left == nullptr) ? 0 : (ulong) wcslen(left);
- ulong rightLen = (right == nullptr) ? 0 : (ulong) wcslen(right);
- if (pShortNameOffset != nullptr)
- {
- *pShortNameOffset = 0;
- }
- Assert(rightLen <= ULONG_MAX && leftLen <= ULONG_MAX); // name hints should not exceed ULONG_MAX characters
- if (leftLen == 0 && !wrapInBrackets)
- {
- *pNameLength = right ? rightLen : 0;
- return right;
- }
- if (rightLen == 0 && !wrapInBrackets)
- {
- *pNameLength = leftLen;
- return left;
- }
- return AppendNameHints(left, leftLen, right, rightLen, pNameLength, pShortNameOffset, ignoreAddDotWithSpace, wrapInBrackets);
- }
- /**
- * Emits a spread error if there is no ambiguity, or marks defers the error for
- * when we can determine if it is a rest error or a spread error.
- *
- * The ambiguity arises when we are parsing a lambda parameter list but we have
- * not seen the => token. At this point, we are either in a parenthesized
- * expression or a parameter list, and cannot issue an error until the matching
- * RParen has been scanned.
- *
- * The actual emission of the error happens in ParseExpr, when we first know if
- * the expression is a lambda parameter list or not.
- *
- */
- void Parser::DeferOrEmitPotentialSpreadError(ParseNodePtr pnodeT)
- {
- if (m_parenDepth > 0)
- {
- if (m_token.tk == tkRParen)
- {
- if (!m_deferEllipsisError)
- {
- // Capture only the first error instance.
- m_pscan->Capture(&m_EllipsisErrLoc);
- m_deferEllipsisError = true;
- }
- }
- else
- {
- Error(ERRUnexpectedEllipsis);
- }
- }
- else
- {
- Error(ERRInvalidSpreadUse);
- }
- }
- /***************************************************************************
- Parse an optional sub expression returning null if there was no expression.
- Checks for no expression by looking for a token that can follow an
- Expression grammar production.
- ***************************************************************************/
- template<bool buildAST>
- bool Parser::ParseOptionalExpr(ParseNodePtr* pnode, bool fUnaryOrParen, int oplMin, BOOL *pfCanAssign, BOOL fAllowIn, BOOL fAllowEllipsis, _Inout_opt_ IdentToken* pToken)
- {
- *pnode = nullptr;
- if (m_token.tk == tkRCurly ||
- m_token.tk == tkRBrack ||
- m_token.tk == tkRParen ||
- m_token.tk == tkSColon ||
- m_token.tk == tkColon ||
- m_token.tk == tkComma ||
- m_token.tk == tkLimKwd ||
- m_pscan->FHadNewLine())
- {
- return false;
- }
- *pnode = ParseExpr<buildAST>(oplMin, pfCanAssign, fAllowIn, fAllowEllipsis, nullptr /*pNameHint*/, nullptr /*pHintLength*/, nullptr /*pShortNameOffset*/, pToken, fUnaryOrParen);
- return true;
- }
- /***************************************************************************
- Parse a sub expression.
- 'fAllowIn' indicates if the 'in' operator should be allowed in the initializing
- expression ( it is not allowed in the context of the first expression in a 'for' loop).
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseExpr(int oplMin,
- BOOL *pfCanAssign,
- BOOL fAllowIn,
- BOOL fAllowEllipsis,
- LPCOLESTR pNameHint,
- ulong *pHintLength,
- ulong *pShortNameOffset,
- _Inout_opt_ IdentToken* pToken,
- bool fUnaryOrParen,
- _Inout_opt_ bool* pfLikelyPattern)
- {
- Assert(pToken == nullptr || pToken->tk == tkNone); // Must be empty initially
- int opl;
- OpCode nop;
- charcount_t ichMin;
- ParseNodePtr pnode = nullptr;
- ParseNodePtr pnodeT = nullptr;
- BOOL fCanAssign = TRUE;
- bool assignmentStmt = false;
- IdentToken term;
- RestorePoint termStart;
- ulong hintLength = 0;
- ulong hintOffset = 0;
- ParserState parserState;
- if (pHintLength != nullptr)
- {
- hintLength = *pHintLength;
- }
- if (pShortNameOffset != nullptr)
- {
- hintOffset = *pShortNameOffset;
- }
- EnsureStackAvailable();
- // Storing the state here as we need to restore this state back when we need to reparse the grammar under lambda syntax.
- CaptureState(&parserState);
- m_pscan->Capture(&termStart);
- bool deferredErrorFoundOnLeftSide = false;
- bool savedDeferredInitError = m_hasDeferredShorthandInitError;
- m_hasDeferredShorthandInitError = false;
- // Is the current token a unary operator?
- if (m_phtbl->TokIsUnop(m_token.tk, &opl, &nop) && nop != knopNone)
- {
- IdentToken operandToken;
- ichMin = m_pscan->IchMinTok();
- if (nop == knopYield)
- {
- if (!m_pscan->YieldIsKeyword() || oplMin > opl)
- {
- // The case where 'yield' is scanned as a keyword (tkYIELD) but the scanner
- // is not treating yield as a keyword (!m_pscan->YieldIsKeyword()) occurs
- // in strict mode non-generator function contexts.
- //
- // That is, 'yield' is a keyword because of strict mode, but YieldExpression
- // is not a grammar production outside of generator functions.
- //
- // Otherwise it is an error for a yield to appear in the context of a higher level
- // binding operator, be it unary or binary.
- Error(ERRsyntax);
- }
- if (GetCurrentFunctionNode()->sxFnc.IsGenerator()
- && m_currentBlockInfo->pnodeBlock->sxBlock.blockType == PnodeBlockType::Parameter)
- {
- Error(ERRsyntax);
- }
- }
- else if (nop == knopAwait)
- {
- if (!m_pscan->AwaitIsKeyword() ||
- (GetCurrentFunctionNode()->sxFnc.IsAsync() && m_currentScope->GetScopeType() == ScopeType_Parameter))
- {
- // As with the 'yield' keyword, the case where 'await' is scanned as a keyword (tkAWAIT)
- // but the scanner is not treating await as a keyword (!m_pscan->AwaitIsKeyword())
- // occurs in strict mode non-async function contexts.
- //
- // That is, 'await' is a keyword because of strict mode, but AwaitExpression
- // is not a grammar production outside of async functions.
- //
- // Further, await expressions are disallowed within parameter scopes.
- Error(ERRBadAwait);
- }
- }
- m_pscan->Scan();
- if (nop == knopYield && !m_pscan->FHadNewLine() && m_token.tk == tkStar)
- {
- m_pscan->Scan();
- nop = knopYieldStar;
- }
- if (nop == knopYield)
- {
- if (!ParseOptionalExpr<buildAST>(&pnodeT, false, opl, NULL, TRUE, fAllowEllipsis))
- {
- nop = knopYieldLeaf;
- if (buildAST)
- {
- pnode = CreateNodeT<knopYieldLeaf>(ichMin, m_pscan->IchLimTok());
- }
- }
- }
- else
- {
- // Disallow spread after an Ellipsis token. This prevents chaining, and ensures spread is the top level expression.
- pnodeT = ParseExpr<buildAST>(opl, &fCanAssign, TRUE, nop != knopEllipsis && fAllowEllipsis, nullptr /*hint*/, nullptr /*hintLength*/, nullptr /*hintOffset*/, &operandToken, true);
- }
- if (nop != knopYieldLeaf)
- {
- if (nop == knopIncPre || nop == knopDecPre)
- {
- if (!fCanAssign && PHASE_ON1(Js::EarlyReferenceErrorsPhase))
- {
- Error(JSERR_CantAssignTo);
- }
- if (buildAST)
- {
- if (IsStrictMode() && pnodeT->nop == knopName)
- {
- CheckStrictModeEvalArgumentsUsage(pnodeT->sxPid.pid);
- }
- }
- else
- {
- if (IsStrictMode() && operandToken.tk == tkID)
- {
- CheckStrictModeEvalArgumentsUsage(operandToken.pid);
- }
- }
- }
- else if (nop == knopEllipsis)
- {
- if (!fAllowEllipsis)
- {
- DeferOrEmitPotentialSpreadError(pnodeT);
- }
- }
- else if (m_token.tk == tkExpo)
- {
- //Unary operator on the left hand-side of ** is unexpected, except ++, -- or ...
- Error(ERRInvalidUseofExponentiationOperator);
- }
- if (buildAST)
- {
- //Do not do the folding for Asm in case of KnopPos as we need this to determine the type
- if (nop == knopPos && (pnodeT->nop == knopInt || pnodeT->nop == knopFlt) && !this->m_InAsmMode)
- {
- // Fold away a unary '+' on a number.
- pnode = pnodeT;
- }
- else if (nop == knopNeg &&
- ((pnodeT->nop == knopInt && pnodeT->sxInt.lw != 0) ||
- (pnodeT->nop == knopFlt && (pnodeT->sxFlt.dbl != 0 || this->m_InAsmMode))))
- {
- // Fold a unary '-' on a number into the value of the number itself.
- pnode = pnodeT;
- if (pnode->nop == knopInt)
- {
- pnode->sxInt.lw = -pnode->sxInt.lw;
- }
- else
- {
- pnode->sxFlt.dbl = -pnode->sxFlt.dbl;
- }
- }
- else
- {
- pnode = CreateUniNode(nop, pnodeT);
- this->CheckArguments(pnode->sxUni.pnode1);
- }
- pnode->ichMin = ichMin;
- }
- if (nop == knopDelete)
- {
- if (IsStrictMode())
- {
- if ((buildAST && pnode->sxUni.pnode1->nop == knopName) ||
- (!buildAST && operandToken.tk == tkID))
- {
- Error(ERRInvalidDelete);
- }
- }
- if (buildAST)
- {
- ParseNodePtr pnode1 = pnode->sxUni.pnode1;
- if (m_currentNodeFunc)
- {
- if (pnode1->nop == knopDot || pnode1->nop == knopIndex)
- {
- // If we delete an arguments property, use the conservative,
- // heap-allocated arguments object.
- this->CheckArguments(pnode1->sxBin.pnode1);
- }
- }
- }
- }
- }
- fCanAssign = FALSE;
- }
- else
- {
- ichMin = m_pscan->IchMinTok();
- BOOL fLikelyPattern = FALSE;
- pnode = ParseTerm<buildAST>(TRUE, pNameHint, &hintLength, &hintOffset, &term, fUnaryOrParen, &fCanAssign, IsES6DestructuringEnabled() ? &fLikelyPattern : nullptr);
- if (pfLikelyPattern != nullptr)
- {
- *pfLikelyPattern = !!fLikelyPattern;
- }
- if (m_token.tk == tkAsg && oplMin <= koplAsg && fLikelyPattern)
- {
- m_pscan->SeekTo(termStart);
- ParseDestructuredLiteralWithScopeSave(tkLCurly, false/*isDecl*/, false /*topLevel*/, DIC_ShouldNotParseInitializer);
- if (buildAST)
- {
- pnode = ConvertToPattern(pnode);
- }
- // The left-hand side is found to be destructuring pattern - so the shorthand can have initializer.
- m_hasDeferredShorthandInitError = false;
- }
- if (buildAST)
- {
- pNameHint = NULL;
- if (pnode->nop == knopName)
- {
- pNameHint = pnode->sxPid.pid->Psz();
- hintLength = pnode->sxPid.pid->Cch();
- hintOffset = 0;
- }
- else if (pnode->nop == knopDot || pnode->nop == knopIndex)
- {
- if (CONFIG_FLAG(UseFullName))
- {
- pNameHint = ConstructNameHint(pnode, &hintLength, &hintOffset);
- }
- else
- {
- ParseNodePtr pnodeName = pnode;
- while (pnodeName->nop == knopDot)
- {
- pnodeName = pnodeName->sxBin.pnode2;
- }
- if (pnodeName->nop == knopName)
- {
- pNameHint = pnodeName->sxPid.pid->Psz();
- hintLength = pnodeName->sxPid.pid->Cch();
- hintOffset = 0;
- }
- }
- }
- }
- // Check for postfix unary operators.
- if (!m_pscan->FHadNewLine() &&
- (tkInc == m_token.tk || tkDec == m_token.tk))
- {
- if (!fCanAssign && PHASE_ON1(Js::EarlyReferenceErrorsPhase))
- {
- Error(JSERR_CantAssignTo);
- }
- fCanAssign = FALSE;
- if (buildAST)
- {
- if (IsStrictMode() && pnode->nop == knopName)
- {
- CheckStrictModeEvalArgumentsUsage(pnode->sxPid.pid);
- }
- this->CheckArguments(pnode);
- pnode = CreateUniNode(tkInc == m_token.tk ? knopIncPost : knopDecPost, pnode);
- pnode->ichLim = m_pscan->IchLimTok();
- }
- else
- {
- if (IsStrictMode() && term.tk == tkID)
- {
- CheckStrictModeEvalArgumentsUsage(term.pid);
- }
- // This expression is not an identifier
- term.tk = tkNone;
- }
- m_pscan->Scan();
- }
- }
- deferredErrorFoundOnLeftSide = m_hasDeferredShorthandInitError;
- // Process a sequence of operators and operands.
- for (;;)
- {
- if (!m_phtbl->TokIsBinop(m_token.tk, &opl, &nop) || nop == knopNone)
- {
- break;
- }
- if ( ! fAllowIn && nop == knopIn )
- {
- break;
- }
- Assert(opl != koplNo);
- if (opl == koplAsg)
- {
- if (m_token.tk != tkDArrow)
- {
- // Assignment operator. These are the only right associative
- // binary operators. We also need to special case the left
- // operand - it should only be a LeftHandSideExpression.
- Assert(ParseNode::Grfnop(nop) & fnopAsg || nop == knopFncDecl);
- if (buildAST)
- {
- if (IsStrictMode() && pnode->nop == knopName)
- {
- CheckStrictModeEvalArgumentsUsage(pnode->sxPid.pid);
- }
- // Assignment stmt of the form "this.<id> = <expr>"
- if (nop == knopAsg && pnode->nop == knopDot && pnode->sxBin.pnode1->nop == knopThis && pnode->sxBin.pnode2->nop == knopName)
- {
- if (pnode->sxBin.pnode2->sxPid.pid != wellKnownPropertyPids.__proto__)
- {
- assignmentStmt = true;
- }
- }
- }
- else
- {
- if (IsStrictMode() && term.tk == tkID)
- {
- CheckStrictModeEvalArgumentsUsage(term.pid);
- }
- }
- }
- if (opl < oplMin)
- {
- break;
- }
- if (m_token.tk != tkDArrow && !fCanAssign && PHASE_ON1(Js::EarlyReferenceErrorsPhase))
- {
- Error(JSERR_CantAssignTo);
- // No recovery necessary since this is a semantic, not structural, error.
- }
- }
- else if (opl == koplExpo)
- {
- // ** operator is right associative
- if (opl < oplMin)
- {
- break;
- }
- }
- else if (opl <= oplMin)
- {
- break;
- }
- // This expression is not an identifier
- term.tk = tkNone;
- // Precedence is high enough. Consume the operator token.
- m_pscan->Scan();
- fCanAssign = FALSE;
- // Special case the "?:" operator
- if (nop == knopQmark)
- {
- pnodeT = ParseExpr<buildAST>(koplAsg, NULL, fAllowIn);
- ChkCurTok(tkColon, ERRnoColon);
- ParseNodePtr pnodeT2 = ParseExpr<buildAST>(koplAsg, NULL, fAllowIn);
- if (buildAST)
- {
- pnode = CreateTriNode(nop, pnode, pnodeT, pnodeT2);
- this->CheckArguments(pnode->sxTri.pnode2);
- this->CheckArguments(pnode->sxTri.pnode3);
- }
- }
- else if (nop == knopFncDecl)
- {
- ushort flags = fFncLambda;
- size_t iecpMin = 0;
- bool isAsyncMethod = false;
- RestoreStateFrom(&parserState);
- m_pscan->SeekTo(termStart);
- if (m_token.tk == tkID && m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.async && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
- {
- ichMin = m_pscan->IchMinTok();
- iecpMin = m_pscan->IecpMinTok();
- m_pscan->Scan();
- if ((m_token.tk == tkID || m_token.tk == tkLParen) && !m_pscan->FHadNewLine())
- {
- flags |= fFncAsync;
- isAsyncMethod = true;
- }
- else
- {
- m_pscan->SeekTo(termStart);
- }
- }
- pnode = ParseFncDecl<buildAST>(flags, nullptr, /* needsPIDOnRCurlyScan = */false, /* resetParsingSuperRestrictionState = */false);
- if (isAsyncMethod)
- {
- pnode->sxFnc.cbMin = iecpMin;
- pnode->ichMin = ichMin;
- }
- }
- else
- {
- // Parse the operand, make a new node, and look for more
- pnodeT = ParseExpr<buildAST>(opl, NULL, fAllowIn, FALSE, pNameHint, &hintLength, &hintOffset, nullptr);
- if (buildAST)
- {
- pnode = CreateBinNode(nop, pnode, pnodeT);
- Assert(pnode->sxBin.pnode2 != NULL);
- if (pnode->sxBin.pnode2->nop == knopFncDecl)
- {
- Assert(hintLength >= hintOffset);
- pnode->sxBin.pnode2->sxFnc.hint = pNameHint;
- pnode->sxBin.pnode2->sxFnc.hintLength = hintLength;
- pnode->sxBin.pnode2->sxFnc.hintOffset = hintOffset;
- if (pnode->sxBin.pnode1->nop == knopDot)
- {
- pnode->sxBin.pnode2->sxFnc.isNameIdentifierRef = false;
- }
- }
- if (pnode->sxBin.pnode2->nop == knopClassDecl && pnode->sxBin.pnode1->nop == knopDot)
- {
- Assert(pnode->sxBin.pnode2->sxClass.pnodeConstructor);
- pnode->sxBin.pnode2->sxClass.pnodeConstructor->sxFnc.isNameIdentifierRef = false;
- }
- }
- pNameHint = NULL;
- }
- }
- if (buildAST)
- {
- if (!assignmentStmt)
- {
- // Don't set the flag for following nodes
- switch (pnode->nop)
- {
- case knopName:
- case knopInt:
- case knopFlt:
- case knopStr:
- case knopRegExp:
- case knopNull:
- case knopFalse:
- case knopTrue:
- break;
- default:
- if (m_currentNodeFunc)
- {
- m_currentNodeFunc->sxFnc.SetHasNonThisStmt();
- }
- else if (m_currentNodeProg)
- {
- m_currentNodeProg->sxFnc.SetHasNonThisStmt();
- }
- }
- }
- }
- if (m_hasDeferredShorthandInitError && !deferredErrorFoundOnLeftSide)
- {
- // Raise error only if it is found not on the right side of the expression.
- // such as <expr> = {x = 1}
- Error(ERRnoColon);
- }
- m_hasDeferredShorthandInitError = m_hasDeferredShorthandInitError || savedDeferredInitError;
- if (NULL != pfCanAssign)
- {
- *pfCanAssign = fCanAssign;
- }
- // Pass back identifier if requested
- if (pToken && term.tk == tkID)
- {
- *pToken = term;
- }
- //Track "obj.a" assignment patterns here - Promote the Assignment state for the property's PID.
- // This includes =, += etc.
- if (pnode != NULL)
- {
- uint nodeType = ParseNode::Grfnop(pnode->nop);
- if (nodeType & fnopAsg)
- {
- if (nodeType & fnopBin)
- {
- ParseNodePtr lhs = pnode->sxBin.pnode1;
- Assert(lhs);
- if (lhs->nop == knopDot)
- {
- ParseNodePtr propertyNode = lhs->sxBin.pnode2;
- if (propertyNode->nop == knopName)
- {
- propertyNode->sxPid.pid->PromoteAssignmentState();
- }
- }
- }
- else if (nodeType & fnopUni)
- {
- // cases like obj.a++, ++obj.a
- ParseNodePtr lhs = pnode->sxUni.pnode1;
- if (lhs->nop == knopDot)
- {
- ParseNodePtr propertyNode = lhs->sxBin.pnode2;
- if (propertyNode->nop == knopName)
- {
- propertyNode->sxPid.pid->PromoteAssignmentState();
- }
- }
- }
- }
- }
- return pnode;
- }
- void PnPid::SetSymRef(PidRefStack *ref)
- {
- Assert(symRef == nullptr);
- this->symRef = ref->GetSymRef();
- }
- Js::PropertyId PnPid::PropertyIdFromNameNode() const
- {
- Js::PropertyId propertyId;
- Symbol *sym = this->sym;
- if (sym)
- {
- propertyId = sym->GetPosition();
- }
- else
- {
- propertyId = this->pid->GetPropertyId();
- }
- return propertyId;
- }
- PidRefStack* Parser::PushPidRef(IdentPtr pid)
- {
- if (PHASE_ON1(Js::ParallelParsePhase))
- {
- // NOTE: the phase check is here to protect perf. See OSG 1020424.
- // In some LS AST-rewrite cases we lose a lot of perf searching the PID ref stack rather
- // than just pushing on the top. This hasn't shown up as a perf issue in non-LS benchmarks.
- return pid->FindOrAddPidRef(&m_nodeAllocator, GetCurrentBlock()->sxBlock.blockId);
- }
- Assert(GetCurrentBlock() != nullptr);
- AssertMsg(pid != nullptr, "PID should be created");
- PidRefStack *ref = pid->GetTopRef();
- if (!ref || (ref->GetScopeId() < GetCurrentBlock()->sxBlock.blockId))
- {
- ref = Anew(&m_nodeAllocator, PidRefStack);
- if (ref == nullptr)
- {
- Error(ERRnoMemory);
- }
- pid->PushPidRef(GetCurrentBlock()->sxBlock.blockId, ref);
- }
- return ref;
- }
- PidRefStack* Parser::FindOrAddPidRef(IdentPtr pid, int scopeId)
- {
- PidRefStack *ref = pid->FindOrAddPidRef(&m_nodeAllocator, scopeId);
- if (ref == NULL)
- {
- Error(ERRnoMemory);
- }
- return ref;
- }
- void Parser::RemovePrevPidRef(IdentPtr pid, PidRefStack *ref)
- {
- PidRefStack *prevRef = pid->RemovePrevPidRef(ref);
- Assert(prevRef);
- if (prevRef->GetSym() == nullptr)
- {
- AllocatorDelete(ArenaAllocator, &m_nodeAllocator, prevRef);
- }
- }
- void Parser::SetPidRefsInScopeDynamic(IdentPtr pid, int blockId)
- {
- PidRefStack *ref = pid->GetTopRef();
- while (ref && ref->GetScopeId() >= blockId)
- {
- ref->SetDynamicBinding();
- ref = ref->prev;
- }
- }
- ParseNode* Parser::GetFunctionBlock()
- {
- Assert(m_currentBlockInfo != nullptr);
- return m_currentBlockInfo->pBlockInfoFunction->pnodeBlock;
- }
- ParseNode* Parser::GetCurrentBlock()
- {
- return m_currentBlockInfo != nullptr ? m_currentBlockInfo->pnodeBlock : nullptr;
- }
- BlockInfoStack* Parser::GetCurrentBlockInfo()
- {
- return m_currentBlockInfo;
- }
- BlockInfoStack* Parser::GetCurrentFunctionBlockInfo()
- {
- return m_currentBlockInfo->pBlockInfoFunction;
- }
- /***************************************************************************
- Parse a variable declaration.
- 'fAllowIn' indicates if the 'in' operator should be allowed in the initializing
- expression ( it is not allowed in the context of the first expression in a 'for' loop).
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseVariableDeclaration(
- tokens declarationType, charcount_t ichMin,
- BOOL fAllowIn/* = TRUE*/,
- BOOL* pfForInOk/* = nullptr*/,
- BOOL singleDefOnly/* = FALSE*/,
- BOOL allowInit/* = TRUE*/,
- BOOL isTopVarParse/* = TRUE*/,
- BOOL isFor/* = FALSE*/,
- BOOL* nativeForOk /*= nullptr*/)
- {
- ParseNodePtr pnodeThis = nullptr;
- ParseNodePtr pnodeInit;
- ParseNodePtr pnodeList = nullptr;
- ParseNodePtr *lastNodeRef = nullptr;
- LPCOLESTR pNameHint = nullptr;
- ulong nameHintLength = 0;
- ulong nameHintOffset = 0;
- Assert(declarationType == tkVAR || declarationType == tkCONST || declarationType == tkLET);
- for (;;)
- {
- if (IsES6DestructuringEnabled() && IsPossiblePatternStart())
- {
- pnodeThis = ParseDestructuredLiteral<buildAST>(declarationType, true, !!isTopVarParse, DIC_None, !!fAllowIn, pfForInOk, nativeForOk);
- if (pnodeThis != nullptr)
- {
- pnodeThis->ichMin = ichMin;
- }
- }
- else
- {
- if (m_token.tk != tkID)
- {
- IdentifierExpectedError(m_token);
- }
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- Assert(pid);
- pNameHint = pid->Psz();
- nameHintLength = pid->Cch();
- nameHintOffset = 0;
- if (pid == wellKnownPropertyPids.let && (declarationType == tkCONST || declarationType == tkLET))
- {
- Error(ERRLetIDInLexicalDecl, pnodeThis);
- }
- if (declarationType == tkVAR)
- {
- pnodeThis = CreateVarDeclNode(pid, STVariable);
- }
- else if (declarationType == tkCONST)
- {
- pnodeThis = CreateBlockScopedDeclNode(pid, knopConstDecl);
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(ConstCount, m_scriptContext);
- }
- else
- {
- pnodeThis = CreateBlockScopedDeclNode(pid, knopLetDecl);
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(LetCount, m_scriptContext);
- }
- if (pid == wellKnownPropertyPids.arguments && m_currentNodeFunc)
- {
- // This var declaration may change the way an 'arguments' identifier in the function is resolved
- if (declarationType == tkVAR)
- {
- m_currentNodeFunc->grfpn |= PNodeFlags::fpnArguments_varDeclaration;
- }
- else
- {
- if (GetCurrentBlockInfo()->pnodeBlock->sxBlock.blockType == Function)
- {
- // Only override arguments if we are at the function block level.
- m_currentNodeFunc->grfpn |= PNodeFlags::fpnArguments_overriddenByDecl;
- }
- }
- }
- if (pnodeThis)
- {
- pnodeThis->ichMin = ichMin;
- }
- m_pscan->Scan();
- if (m_token.tk == tkAsg)
- {
- if (!allowInit)
- {
- Error(ERRUnexpectedDefault);
- }
- if (pfForInOk && (declarationType == tkLET || declarationType == tkCONST))
- {
- *pfForInOk = FALSE;
- }
- m_pscan->Scan();
- pnodeInit = ParseExpr<buildAST>(koplCma, nullptr, fAllowIn, FALSE, pNameHint, &nameHintLength, &nameHintOffset);
- if (buildAST)
- {
- AnalysisAssert(pnodeThis);
- pnodeThis->sxVar.pnodeInit = pnodeInit;
- pnodeThis->ichLim = pnodeInit->ichLim;
- if (pnodeInit->nop == knopFncDecl)
- {
- Assert(nameHintLength >= nameHintOffset);
- pnodeInit->sxFnc.hint = pNameHint;
- pnodeInit->sxFnc.hintLength = nameHintLength;
- pnodeInit->sxFnc.hintOffset = nameHintOffset;
- }
- else
- {
- this->CheckArguments(pnodeInit);
- }
- pNameHint = nullptr;
- }
- //Track var a =, let a= , const a =
- // This is for FixedFields Constant Heuristics
- if (pnodeThis && pnodeThis->sxVar.pnodeInit != nullptr)
- {
- pnodeThis->sxVar.sym->PromoteAssignmentState();
- }
- }
- else if (declarationType == tkCONST /*pnodeThis->nop == knopConstDecl*/
- && !singleDefOnly
- && !(isFor && TokIsForInOrForOf()))
- {
- Error(ERRUninitializedConst);
- }
- }
- if (singleDefOnly)
- {
- return pnodeThis;
- }
- if (buildAST)
- {
- AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeThis);
- }
- if (m_token.tk != tkComma)
- {
- return pnodeList;
- }
- if (pfForInOk)
- {
- // don't allow "for (var a, b in c)"
- *pfForInOk = FALSE;
- }
- m_pscan->Scan();
- ichMin = m_pscan->IchMinTok();
- }
- }
- /***************************************************************************
- Parse try-catch-finally statement
- ***************************************************************************/
- // Eze try-catch-finally tree nests the try-catch within a try-finally.
- // This matches the new runtime implementation.
- template<bool buildAST>
- ParseNodePtr Parser::ParseTryCatchFinally()
- {
- this->m_tryCatchOrFinallyDepth++;
- ParseNodePtr pnodeT = ParseTry<buildAST>();
- ParseNodePtr pnodeTC = nullptr;
- StmtNest stmt;
- bool hasCatch = false;
- if (tkCATCH == m_token.tk)
- {
- hasCatch = true;
- if (buildAST)
- {
- pnodeTC = CreateNodeWithScanner<knopTryCatch>();
- pnodeT->sxStmt.pnodeOuter = pnodeTC;
- pnodeTC->sxTryCatch.pnodeTry = pnodeT;
- }
- PushStmt<buildAST>(&stmt, pnodeTC, knopTryCatch, nullptr, nullptr);
- ParseNodePtr pnodeCatch = ParseCatch<buildAST>();
- if (buildAST)
- {
- pnodeTC->sxTryCatch.pnodeCatch = pnodeCatch;
- }
- PopStmt(&stmt);
- }
- if (tkFINALLY != m_token.tk)
- {
- if (!hasCatch)
- {
- Error(ERRnoCatch);
- }
- Assert(!buildAST || pnodeTC);
- return pnodeTC;
- }
- ParseNodePtr pnodeTF = nullptr;
- if (buildAST)
- {
- pnodeTF = CreateNode(knopTryFinally);
- }
- PushStmt<buildAST>(&stmt, pnodeTF, knopTryFinally, nullptr, nullptr);
- ParseNodePtr pnodeFinally = ParseFinally<buildAST>();
- if (buildAST)
- {
- if (!hasCatch)
- {
- pnodeTF->sxTryFinally.pnodeTry = pnodeT;
- pnodeT->sxStmt.pnodeOuter = pnodeTF;
- }
- else
- {
- pnodeTF->sxTryFinally.pnodeTry = CreateNode(knopTry);
- pnodeTF->sxTryFinally.pnodeTry->sxStmt.pnodeOuter = pnodeTF;
- pnodeTF->sxTryFinally.pnodeTry->sxTry.pnodeBody = pnodeTC;
- pnodeTC->sxStmt.pnodeOuter = pnodeTF->sxTryFinally.pnodeTry;
- }
- pnodeTF->sxTryFinally.pnodeFinally = pnodeFinally;
- }
- PopStmt(&stmt);
- this->m_tryCatchOrFinallyDepth--;
- return pnodeTF;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseTry()
- {
- ParseNodePtr pnode = nullptr;
- StmtNest stmt;
- Assert(tkTRY == m_token.tk);
- if (buildAST)
- {
- pnode = CreateNode(knopTry);
- }
- m_pscan->Scan();
- if (tkLCurly != m_token.tk)
- {
- Error(ERRnoLcurly);
- }
- PushStmt<buildAST>(&stmt, pnode, knopTry, nullptr, nullptr);
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- if (buildAST)
- {
- pnode->sxTry.pnodeBody = pnodeBody;
- if (pnode->sxTry.pnodeBody)
- pnode->ichLim = pnode->sxTry.pnodeBody->ichLim;
- }
- PopStmt(&stmt);
- return pnode;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseFinally()
- {
- ParseNodePtr pnode = nullptr;
- StmtNest stmt;
- Assert(tkFINALLY == m_token.tk);
- if (buildAST)
- {
- pnode = CreateNode(knopFinally);
- }
- m_pscan->Scan();
- if (tkLCurly != m_token.tk)
- {
- Error(ERRnoLcurly);
- }
- PushStmt<buildAST>(&stmt, pnode, knopFinally, nullptr, nullptr);
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- if (buildAST)
- {
- pnode->sxFinally.pnodeBody = pnodeBody;
- if (!pnode->sxFinally.pnodeBody)
- // Will only occur due to error correction.
- pnode->sxFinally.pnodeBody = CreateNodeWithScanner<knopEmpty>();
- else
- pnode->ichLim = pnode->sxFinally.pnodeBody->ichLim;
- }
- PopStmt(&stmt);
- return pnode;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseCatch()
- {
- ParseNodePtr rootNode = nullptr;
- ParseNodePtr* ppnode = &rootNode;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- ParseNodePtr pnode = nullptr;
- ParseNodePtr pnodeCatchScope = nullptr;
- StmtNest stmt;
- IdentPtr pidCatch = nullptr;
- //while (tkCATCH == m_token.tk)
- if (tkCATCH == m_token.tk)
- {
- charcount_t ichMin;
- if (buildAST)
- {
- ichMin = m_pscan->IchMinTok();
- }
- m_pscan->Scan(); //catch
- ChkCurTok(tkLParen, ERRnoLparen); //catch(
- bool isPattern = false;
- if (tkID != m_token.tk)
- {
- isPattern = IsES6DestructuringEnabled() && IsPossiblePatternStart();
- if (!isPattern)
- {
- IdentifierExpectedError(m_token);
- }
- }
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopCatch>(ichMin);
- PushStmt<buildAST>(&stmt, pnode, knopCatch, nullptr, nullptr);
- *ppnode = pnode;
- ppnode = &pnode->sxCatch.pnodeNext;
- *ppnode = nullptr;
- }
- pnodeCatchScope = StartParseBlock<buildAST>(PnodeBlockType::Regular, isPattern ? ScopeType_CatchParamPattern : ScopeType_Catch);
- if (isPattern)
- {
- ParseNodePtr pnodePattern = ParseDestructuredLiteral<buildAST>(tkLET, true /*isDecl*/, true /*topLevel*/, DIC_ForceErrorOnInitializer);
- if (buildAST)
- {
- pnode->sxCatch.pnodeParam = CreateParamPatternNode(pnodePattern);
- Scope *scope = pnodeCatchScope->sxBlock.scope;
- pnode->sxCatch.scope = scope;
- }
- }
- else
- {
- if (IsStrictMode())
- {
- IdentPtr pid = m_token.GetIdentifier(m_phtbl);
- if (pid == wellKnownPropertyPids.eval)
- {
- Error(ERREvalUsage);
- }
- else if (pid == wellKnownPropertyPids.arguments)
- {
- Error(ERRArgsUsage);
- }
- }
- if (buildAST)
- {
- pidCatch = m_token.GetIdentifier(m_phtbl);
- PidRefStack *ref = this->PushPidRef(pidCatch);
- ParseNodePtr pnodeParam = CreateNameNode(pidCatch);
- pnodeParam->sxPid.symRef = ref->GetSymRef();
- pnode->sxCatch.pnodeParam = pnodeParam;
- const char16 *name = reinterpret_cast<const char16*>(pidCatch->Psz());
- int nameLength = pidCatch->Cch();
- SymbolName const symName(name, nameLength);
- Symbol *sym = Anew(&m_nodeAllocator, Symbol, symName, pnodeParam, STVariable);
- sym->SetPid(pidCatch);
- if (sym == nullptr)
- {
- Error(ERRnoMemory);
- }
- Assert(ref->GetSym() == nullptr);
- ref->SetSym(sym);
- Scope *scope = pnodeCatchScope->sxBlock.scope;
- scope->AddNewSymbol(sym);
- pnode->sxCatch.scope = scope;
- }
- m_pscan->Scan();
- }
- if (buildAST)
- {
- // Add this catch to the current scope list.
- if (m_ppnodeExprScope)
- {
- Assert(*m_ppnodeExprScope == nullptr);
- *m_ppnodeExprScope = pnode;
- m_ppnodeExprScope = &pnode->sxCatch.pnodeNext;
- }
- else
- {
- Assert(m_ppnodeScope);
- Assert(*m_ppnodeScope == nullptr);
- *m_ppnodeScope = pnode;
- m_ppnodeScope = &pnode->sxCatch.pnodeNext;
- }
- // Keep a list of function expressions (not declarations) at this scope.
- ppnodeExprScopeSave = m_ppnodeExprScope;
- m_ppnodeExprScope = &pnode->sxCatch.pnodeScopes;
- pnode->sxCatch.pnodeScopes = nullptr;
- }
- charcount_t ichLim;
- if (buildAST)
- {
- ichLim = m_pscan->IchLimTok();
- }
- ChkCurTok(tkRParen, ERRnoRparen); //catch(id[:expr])
- if (tkLCurly != m_token.tk)
- {
- Error(ERRnoLcurly);
- }
- ParseNodePtr pnodeBody = ParseStatement<buildAST>(); //catch(id[:expr]) {block}
- if (buildAST)
- {
- pnode->sxCatch.pnodeBody = pnodeBody;
- pnode->ichLim = ichLim;
- }
- if (pnodeCatchScope != nullptr)
- {
- FinishParseBlock(pnodeCatchScope);
- }
- if (buildAST)
- {
- PopStmt(&stmt);
- // Restore the lists of function expression scopes.
- AssertMem(m_ppnodeExprScope);
- Assert(*m_ppnodeExprScope == nullptr);
- m_ppnodeExprScope = ppnodeExprScopeSave;
- }
- }
- return rootNode;
- }
- template<bool buildAST>
- ParseNodePtr Parser::ParseCase(ParseNodePtr *ppnodeBody)
- {
- ParseNodePtr pnodeT = nullptr;
- charcount_t ichMinT = m_pscan->IchMinTok();
- m_pscan->Scan();
- ParseNodePtr pnodeExpr = ParseExpr<buildAST>();
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkColon, ERRnoColon);
- if (buildAST)
- {
- pnodeT = CreateNodeWithScanner<knopCase>(ichMinT);
- pnodeT->sxCase.pnodeExpr = pnodeExpr;
- pnodeT->ichLim = ichLim;
- }
- ParseStmtList<buildAST>(ppnodeBody);
- return pnodeT;
- }
- /***************************************************************************
- Parse a single statement. Digest a trailing semicolon.
- ***************************************************************************/
- template<bool buildAST>
- ParseNodePtr Parser::ParseStatement()
- {
- ParseNodePtr *ppnodeT;
- ParseNodePtr pnodeT;
- ParseNodePtr pnode = nullptr;
- LabelId* pLabelIdList = nullptr;
- charcount_t ichMin = 0;
- size_t iecpMin = 0;
- StmtNest stmt;
- StmtNest *pstmt;
- BOOL fForInOrOfOkay;
- BOOL fCanAssign;
- IdentPtr pid;
- uint fnop;
- ParseNodePtr pnodeLabel = nullptr;
- bool expressionStmt = false;
- bool isAsyncMethod = false;
- tokens tok;
- #if EXCEPTION_RECOVERY
- ParseNodePtr pParentTryCatch = nullptr;
- ParseNodePtr pTryBlock = nullptr;
- ParseNodePtr pTry = nullptr;
- ParseNodePtr pParentTryCatchBlock = nullptr;
- StmtNest stmtTryCatchBlock;
- StmtNest stmtTryCatch;
- StmtNest stmtTry;
- StmtNest stmtTryBlock;
- #endif
- if (buildAST)
- {
- #if EXCEPTION_RECOVERY
- if(Js::Configuration::Global.flags.SwallowExceptions)
- {
- // If we're swallowing exceptions, surround this statement with a try/catch block:
- //
- // Before: x.y = 3;
- // After: try { x.y = 3; } catch(__ehobj) { }
- //
- // This is done to force the runtime to recover from exceptions at the most granular
- // possible point. Recovering from EH dramatically improves coverage of testing via
- // fault injection.
- // create and push the try-catch node
- pParentTryCatchBlock = CreateBlockNode();
- PushStmt<buildAST>(&stmtTryCatchBlock, pParentTryCatchBlock, knopBlock, nullptr, nullptr);
- pParentTryCatch = CreateNodeWithScanner<knopTryCatch>();
- PushStmt<buildAST>(&stmtTryCatch, pParentTryCatch, knopTryCatch, nullptr, nullptr);
- // create and push a try node
- pTry = CreateNodeWithScanner<knopTry>();
- PushStmt<buildAST>(&stmtTry, pTry, knopTry, nullptr, nullptr);
- pTryBlock = CreateBlockNode();
- PushStmt<buildAST>(&stmtTryBlock, pTryBlock, knopBlock, nullptr, nullptr);
- // these nodes will be closed after the statement is parsed.
- }
- #endif // EXCEPTION_RECOVERY
- }
- EnsureStackAvailable();
- LRestart:
- tok = m_token.tk;
- switch (tok)
- {
- case tkEOF:
- if (buildAST)
- {
- pnode = nullptr;
- }
- break;
- case tkFUNCTION:
- {
- LFunctionStatement:
- if (m_grfscr & fscrDeferredFncExpression)
- {
- // The top-level deferred function body was defined by a function expression whose parsing was deferred. We are now
- // parsing it, so unset the flag so that any nested functions are parsed normally. This flag is only applicable the
- // first time we see it.
- m_grfscr &= ~fscrDeferredFncExpression;
- pnode = ParseFncDecl<buildAST>(isAsyncMethod ? fFncAsync : fFncNoFlgs, nullptr);
- }
- else
- {
- pnode = ParseFncDecl<buildAST>(fFncDeclaration | (isAsyncMethod ? fFncAsync : fFncNoFlgs), nullptr);
- }
- if (isAsyncMethod)
- {
- pnode->sxFnc.cbMin = iecpMin;
- pnode->ichMin = ichMin;
- }
- break;
- }
- case tkCLASS:
- if (m_scriptContext->GetConfig()->IsES6ClassAndExtendsEnabled())
- {
- pnode = ParseClassDecl<buildAST>(TRUE, nullptr, nullptr, nullptr);
- }
- else
- {
- goto LDefaultToken;
- }
- break;
- case tkID:
- if (m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.let)
- {
- // We see "let" at the start of a statement. This could either be a declaration or an identifier
- // reference. The next token determines which.
- RestorePoint parsedLet;
- m_pscan->Capture(&parsedLet);
- ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- if (this->NextTokenConfirmsLetDecl())
- {
- pnode = ParseVariableDeclaration<buildAST>(tkLET, ichMin);
- goto LNeedTerminator;
- }
- m_pscan->SeekTo(parsedLet);
- }
- else if (m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.async && m_scriptContext->GetConfig()->IsES7AsyncAndAwaitEnabled())
- {
- RestorePoint parsedAsync;
- m_pscan->Capture(&parsedAsync);
- ichMin = m_pscan->IchMinTok();
- iecpMin = m_pscan->IecpMinTok();
- m_pscan->Scan();
- if (m_token.tk == tkFUNCTION && !m_pscan->FHadNewLine())
- {
- isAsyncMethod = true;
- goto LFunctionStatement;
- }
- m_pscan->SeekTo(parsedAsync);
- }
- goto LDefaultToken;
- case tkCONST:
- case tkLET:
- ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- pnode = ParseVariableDeclaration<buildAST>(tok, ichMin);
- goto LNeedTerminator;
- case tkVAR:
- ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- pnode = ParseVariableDeclaration<buildAST>(tok, ichMin);
- goto LNeedTerminator;
- case tkFOR:
- {
- ParseNodePtr pnodeBlock = nullptr;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- ichMin = m_pscan->IchMinTok();
- ChkNxtTok(tkLParen, ERRnoLparen);
- pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block);
- if (buildAST)
- {
- PushFuncBlockScope(pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
- }
- RestorePoint startExprOrIdentifier;
- fForInOrOfOkay = TRUE;
- fCanAssign = TRUE;
- tok = m_token.tk;
- BOOL nativeForOkay = TRUE;
- switch (tok)
- {
- case tkID:
- if (m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.let)
- {
- // We see "let" in the init part of a for loop. This could either be a declaration or an identifier
- // reference. The next token determines which.
- RestorePoint parsedLet;
- m_pscan->Capture(&parsedLet);
- auto ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- if (IsPossiblePatternStart())
- {
- m_pscan->Capture(&startExprOrIdentifier);
- }
- if (this->NextTokenConfirmsLetDecl() && m_token.tk != tkIN)
- {
- pnodeT = ParseVariableDeclaration<buildAST>(tkLET, ichMin
- , /*fAllowIn = */FALSE
- , /*pfForInOk = */&fForInOrOfOkay
- , /*singleDefOnly*/FALSE
- , /*allowInit*/TRUE
- , /*isTopVarParse*/TRUE
- , /*isFor*/TRUE
- , &nativeForOkay);
- break;
- }
- m_pscan->SeekTo(parsedLet);
- }
- goto LDefaultTokenFor;
- case tkLET:
- case tkCONST:
- case tkVAR:
- {
- auto ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- if (IsPossiblePatternStart())
- {
- m_pscan->Capture(&startExprOrIdentifier);
- }
- pnodeT = ParseVariableDeclaration<buildAST>(tok, ichMin
- , /*fAllowIn = */FALSE
- , /*pfForInOk = */&fForInOrOfOkay
- , /*singleDefOnly*/FALSE
- , /*allowInit*/TRUE
- , /*isTopVarParse*/TRUE
- , /*isFor*/TRUE
- , &nativeForOkay);
- }
- break;
- case tkSColon:
- pnodeT = nullptr;
- fForInOrOfOkay = FALSE;
- break;
- default:
- {
- LDefaultTokenFor:
- RestorePoint exprStart;
- tokens beforeToken = tok;
- m_pscan->Capture(&exprStart);
- if (IsPossiblePatternStart())
- {
- m_pscan->Capture(&startExprOrIdentifier);
- }
- bool fLikelyPattern = false;
- if (IsES6DestructuringEnabled() && (beforeToken == tkLBrack || beforeToken == tkLCurly))
- {
- pnodeT = ParseExpr<buildAST>(koplNo,
- &fCanAssign,
- /*fAllowIn = */FALSE,
- /*fAllowEllipsis*/FALSE,
- /*pHint*/nullptr,
- /*pHintLength*/nullptr,
- /*pShortNameOffset*/nullptr,
- /*pToken*/nullptr,
- /**fUnaryOrParen*/false,
- &fLikelyPattern);
- }
- else
- {
- pnodeT = ParseExpr<buildAST>(koplNo, &fCanAssign, /*fAllowIn = */FALSE);
- }
- // 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
- // has already converted them appropriately.
- if (fLikelyPattern && TokIsForInOrForOf())
- {
- m_pscan->SeekTo(exprStart);
- ParseDestructuredLiteralWithScopeSave(tkNone, false/*isDecl*/, false /*topLevel*/, DIC_None, false /*allowIn*/);
- if (buildAST)
- {
- pnodeT = ConvertToPattern(pnodeT);
- }
- }
- if (buildAST)
- {
- Assert(pnodeT);
- pnodeT->isUsed = false;
- }
- }
- break;
- }
- if (TokIsForInOrForOf())
- {
- bool isForOf = (m_token.tk != tkIN);
- Assert(!isForOf || (m_token.tk == tkID && m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.of));
- if ((buildAST && nullptr == pnodeT) || !fForInOrOfOkay)
- {
- if (isForOf)
- {
- Error(ERRForOfNoInitAllowed);
- }
- else
- {
- Error(ERRForInNoInitAllowed);
- }
- }
- if (!fCanAssign && PHASE_ON1(Js::EarlyReferenceErrorsPhase))
- {
- Error(JSERR_CantAssignTo);
- }
- m_pscan->Scan();
- ParseNodePtr pnodeObj = ParseExpr<buildAST>(isForOf ? koplCma : koplNo);
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkRParen, ERRnoRparen);
- if (buildAST)
- {
- if (isForOf)
- {
- pnode = CreateNodeWithScanner<knopForOf>(ichMin);
- }
- else
- {
- pnode = CreateNodeWithScanner<knopForIn>(ichMin);
- }
- pnode->sxForInOrForOf.pnodeBlock = pnodeBlock;
- pnode->sxForInOrForOf.pnodeLval = pnodeT;
- pnode->sxForInOrForOf.pnodeObj = pnodeObj;
- pnode->ichLim = ichLim;
- }
- PushStmt<buildAST>(&stmt, pnode, isForOf ? knopForOf : knopForIn, pnodeLabel, pLabelIdList);
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- if (buildAST)
- {
- pnode->sxForInOrForOf.pnodeBody = pnodeBody;
- }
- PopStmt(&stmt);
- }
- else
- {
- if (!nativeForOkay)
- {
- Error(ERRDestructInit);
- }
- ChkCurTok(tkSColon, ERRnoSemic);
- ParseNodePtr pnodeCond = nullptr;
- if (m_token.tk != tkSColon)
- {
- pnodeCond = ParseExpr<buildAST>();
- if (m_token.tk != tkSColon)
- {
- Error(ERRnoSemic);
- }
- }
- tokens tk;
- tk = m_pscan->Scan();
- ParseNodePtr pnodeIncr = nullptr;
- if (tk != tkRParen)
- {
- pnodeIncr = ParseExpr<buildAST>();
- if(pnodeIncr)
- {
- pnodeIncr->isUsed = false;
- }
- }
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkRParen, ERRnoRparen);
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopFor>(ichMin);
- pnode->sxFor.pnodeBlock = pnodeBlock;
- pnode->sxFor.pnodeInverted= nullptr;
- pnode->sxFor.pnodeInit = pnodeT;
- pnode->sxFor.pnodeCond = pnodeCond;
- pnode->sxFor.pnodeIncr = pnodeIncr;
- pnode->ichLim = ichLim;
- }
- PushStmt<buildAST>(&stmt, pnode, knopFor, pnodeLabel, pLabelIdList);
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- if (buildAST)
- {
- pnode->sxFor.pnodeBody = pnodeBody;
- }
- PopStmt(&stmt);
- }
- if (buildAST)
- {
- PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
- }
- FinishParseBlock(pnodeBlock);
- break;
- }
- case tkSWITCH:
- {
- BOOL fSeenDefault = FALSE;
- ParseNodePtr pnodeBlock = nullptr;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- ichMin = m_pscan->IchMinTok();
- ChkNxtTok(tkLParen, ERRnoLparen);
- ParseNodePtr pnodeVal = ParseExpr<buildAST>();
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkRParen, ERRnoRparen);
- ChkCurTok(tkLCurly, ERRnoLcurly);
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopSwitch>(ichMin);
- }
- PushStmt<buildAST>(&stmt, pnode, knopSwitch, pnodeLabel, pLabelIdList);
- pnodeBlock = StartParseBlock<buildAST>(PnodeBlockType::Regular, ScopeType_Block, nullptr, pLabelIdList);
- if (buildAST)
- {
- pnode->sxSwitch.pnodeVal = pnodeVal;
- pnode->sxSwitch.pnodeBlock = pnodeBlock;
- pnode->ichLim = ichLim;
- PushFuncBlockScope(pnode->sxSwitch.pnodeBlock, &ppnodeScopeSave, &ppnodeExprScopeSave);
- pnode->sxSwitch.pnodeDefault = nullptr;
- ppnodeT = &pnode->sxSwitch.pnodeCases;
- }
- for (;;)
- {
- ParseNodePtr pnodeBody = nullptr;
- switch (m_token.tk)
- {
- default:
- goto LEndSwitch;
- case tkCASE:
- {
- pnodeT = this->ParseCase<buildAST>(&pnodeBody);
- break;
- }
- case tkDEFAULT:
- if (fSeenDefault)
- {
- Error(ERRdupDefault);
- // No recovery necessary since this is a semantic, not structural, error
- }
- fSeenDefault = TRUE;
- charcount_t ichMinT = m_pscan->IchMinTok();
- m_pscan->Scan();
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkColon, ERRnoColon);
- if (buildAST)
- {
- pnodeT = CreateNodeWithScanner<knopCase>(ichMinT);
- pnode->sxSwitch.pnodeDefault = pnodeT;
- pnodeT->ichLim = ichLim;
- pnodeT->sxCase.pnodeExpr = nullptr;
- }
- ParseStmtList<buildAST>(&pnodeBody);
- break;
- }
- if (buildAST)
- {
- if (pnodeBody)
- {
- // Create a block node to contain the statement list for this case.
- // This helps us insert byte code to return the right value from
- // global/eval code.
- pnodeT->sxCase.pnodeBody = CreateBlockNode(pnodeT->ichMin, pnodeT->ichLim);
- pnodeT->sxCase.pnodeBody->grfpn |= PNodeFlags::fpnSyntheticNode; // block is not a user specifier block
- pnodeT->sxCase.pnodeBody->sxBlock.pnodeStmt = pnodeBody;
- }
- else
- {
- pnodeT->sxCase.pnodeBody = nullptr;
- }
- *ppnodeT = pnodeT;
- ppnodeT = &pnodeT->sxCase.pnodeNext;
- }
- }
- LEndSwitch:
- ChkCurTok(tkRCurly, ERRnoRcurly);
- if (buildAST)
- {
- *ppnodeT = nullptr;
- PopFuncBlockScope(ppnodeScopeSave, ppnodeExprScopeSave);
- FinishParseBlock(pnode->sxSwitch.pnodeBlock);
- }
- else
- {
- FinishParseBlock(pnodeBlock);
- }
- PopStmt(&stmt);
- break;
- }
- case tkWHILE:
- {
- ichMin = m_pscan->IchMinTok();
- ChkNxtTok(tkLParen, ERRnoLparen);
- ParseNodePtr pnodeCond = ParseExpr<buildAST>();
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkRParen, ERRnoRparen);
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopWhile>(ichMin);
- pnode->sxWhile.pnodeCond = pnodeCond;
- pnode->ichLim = ichLim;
- }
- PushStmt<buildAST>(&stmt, pnode, knopWhile, pnodeLabel, pLabelIdList);
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- PopStmt(&stmt);
- if (buildAST)
- {
- pnode->sxWhile.pnodeBody = pnodeBody;
- }
- break;
- }
- case tkDO:
- {
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopDoWhile>();
- }
- PushStmt<buildAST>(&stmt, pnode, knopDoWhile, pnodeLabel, pLabelIdList);
- m_pscan->Scan();
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- PopStmt(&stmt);
- charcount_t ichMinT = m_pscan->IchMinTok();
- ChkCurTok(tkWHILE, ERRnoWhile);
- ChkCurTok(tkLParen, ERRnoLparen);
- ParseNodePtr pnodeCond = ParseExpr<buildAST>();
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkRParen, ERRnoRparen);
- if (buildAST)
- {
- pnode->sxWhile.pnodeBody = pnodeBody;
- pnode->sxWhile.pnodeCond = pnodeCond;
- pnode->ichLim = ichLim;
- pnode->ichMin = ichMinT;
- }
- // REVIEW: Allow do...while statements to be embedded in other compound statements like if..else, or do..while?
- // goto LNeedTerminator;
- // For now just eat the trailing semicolon if present.
- if (m_token.tk == tkSColon)
- {
- if (pnode)
- {
- pnode->grfpn |= PNodeFlags::fpnExplicitSemicolon;
- }
- m_pscan->Scan();
- }
- else if (pnode)
- {
- pnode->grfpn |= PNodeFlags::fpnAutomaticSemicolon;
- }
- break;
- }
- case tkIF:
- {
- ichMin = m_pscan->IchMinTok();
- ChkNxtTok(tkLParen, ERRnoLparen);
- ParseNodePtr pnodeCond = ParseExpr<buildAST>();
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopIf>(ichMin);
- pnode->ichLim = m_pscan->IchLimTok();
- pnode->sxIf.pnodeCond = pnodeCond;
- }
- ChkCurTok(tkRParen, ERRnoRparen);
- PushStmt<buildAST>(&stmt, pnode, knopIf, pnodeLabel, pLabelIdList);
- ParseNodePtr pnodeTrue = ParseStatement<buildAST>();
- ParseNodePtr pnodeFalse = nullptr;
- if (m_token.tk == tkELSE)
- {
- m_pscan->Scan();
- pnodeFalse = ParseStatement<buildAST>();
- }
- if (buildAST)
- {
- pnode->sxIf.pnodeTrue = pnodeTrue;
- pnode->sxIf.pnodeFalse = pnodeFalse;
- }
- PopStmt(&stmt);
- break;
- }
- case tkTRY:
- {
- if (buildAST)
- {
- pnode = CreateBlockNode();
- pnode->grfpn |= PNodeFlags::fpnSyntheticNode; // block is not a user specifier block
- }
- PushStmt<buildAST>(&stmt, pnode, knopBlock, pnodeLabel, pLabelIdList);
- ParseNodePtr pnodeStmt = ParseTryCatchFinally<buildAST>();
- if (buildAST)
- {
- pnode->sxBlock.pnodeStmt = pnodeStmt;
- }
- PopStmt(&stmt);
- break;
- }
- case tkWITH:
- {
- if ( IsStrictMode() )
- {
- Error(ERRES5NoWith);
- }
- if (m_currentNodeFunc)
- {
- GetCurrentFunctionNode()->sxFnc.SetHasWithStmt(); // Used by DeferNested
- }
- ichMin = m_pscan->IchMinTok();
- ChkNxtTok(tkLParen, ERRnoLparen);
- ParseNodePtr pnodeObj = ParseExpr<buildAST>();
- if (!buildAST)
- {
- m_scopeCountNoAst++;
- }
- charcount_t ichLim = m_pscan->IchLimTok();
- ChkCurTok(tkRParen, ERRnoRparen);
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopWith>(ichMin);
- }
- PushStmt<buildAST>(&stmt, pnode, knopWith, pnodeLabel, pLabelIdList);
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- if (buildAST)
- {
- pnode->sxWith.pnodeObj = pnodeObj;
- this->CheckArguments(pnode->sxWith.pnodeObj);
- if (m_ppnodeExprScope)
- {
- Assert(*m_ppnodeExprScope == nullptr);
- *m_ppnodeExprScope = pnode;
- m_ppnodeExprScope = &pnode->sxWith.pnodeNext;
- }
- else
- {
- Assert(m_ppnodeScope);
- Assert(*m_ppnodeScope == nullptr);
- *m_ppnodeScope = pnode;
- m_ppnodeScope = &pnode->sxWith.pnodeNext;
- }
- pnode->sxWith.pnodeNext = nullptr;
- pnode->sxWith.scope = nullptr;
- ppnodeExprScopeSave = m_ppnodeExprScope;
- m_ppnodeExprScope = &pnode->sxWith.pnodeScopes;
- pnode->sxWith.pnodeScopes = nullptr;
- pnode->ichLim = ichLim;
- }
- PushBlockInfo(CreateBlockNode());
- PushDynamicBlock();
- ParseNodePtr pnodeBody = ParseStatement<buildAST>();
- if (buildAST)
- {
- pnode->sxWith.pnodeBody = pnodeBody;
- m_ppnodeExprScope = ppnodeExprScopeSave;
- }
- else
- {
- m_scopeCountNoAst--;
- }
- // The dynamic block is not stored in the actual parse tree and so will not
- // be visited by the byte code generator. Grab the callsEval flag off it and
- // pass on to outer block in case of:
- // with (...) eval(...); // i.e. blockless form of with
- bool callsEval = GetCurrentBlock()->sxBlock.GetCallsEval();
- PopBlockInfo();
- if (callsEval)
- {
- // be careful not to overwrite an existing true with false
- GetCurrentBlock()->sxBlock.SetCallsEval(true);
- }
- PopStmt(&stmt);
- break;
- }
- case tkLCurly:
- pnode = ParseBlock<buildAST>(pnodeLabel, pLabelIdList);
- break;
- case tkSColon:
- pnode = nullptr;
- m_pscan->Scan();
- break;
- case tkBREAK:
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopBreak>();
- }
- fnop = fnopBreak;
- goto LGetJumpStatement;
- case tkCONTINUE:
- if (buildAST)
- {
- pnode = CreateNode(knopContinue);
- }
- fnop = fnopContinue;
- LGetJumpStatement:
- m_pscan->ScanForcingPid();
- if (tkID == m_token.tk && !m_pscan->FHadNewLine())
- {
- // Labeled break or continue.
- pid = m_token.GetIdentifier(m_phtbl);
- AssertMem(pid);
- if (buildAST)
- {
- pnode->sxJump.hasExplicitTarget=true;
- pnode->ichLim = m_pscan->IchLimTok();
- m_pscan->Scan();
- PushStmt<buildAST>(&stmt, pnode, pnode->nop, pnodeLabel, nullptr);
- Assert(pnode->sxStmt.grfnop == 0);
- for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
- {
- AssertNodeMem(pstmt->pnodeStmt);
- AssertNodeMemN(pstmt->pnodeLab);
- for (pnodeT = pstmt->pnodeLab; nullptr != pnodeT;
- pnodeT = pnodeT->sxLabel.pnodeNext)
- {
- Assert(knopLabel == pnodeT->nop);
- if (pid == pnodeT->sxLabel.pid)
- {
- // Found the label. Make sure we can use it. We can
- // break out of any statement, but we can only
- // continue loops.
- if (fnop == fnopContinue &&
- !(pstmt->pnodeStmt->Grfnop() & fnop))
- {
- Error(ERRbadContinue);
- }
- else
- {
- pstmt->pnodeStmt->sxStmt.grfnop |= fnop;
- pnode->sxJump.pnodeTarget = pstmt->pnodeStmt;
- }
- PopStmt(&stmt);
- goto LNeedTerminator;
- }
- }
- pnode->sxStmt.grfnop |=
- (pstmt->pnodeStmt->Grfnop() & fnopCleanup);
- }
- }
- else
- {
- m_pscan->Scan();
- for (pstmt = m_pstmtCur; pstmt; pstmt = pstmt->pstmtOuter)
- {
- LabelId* pLabelId;
- for (pLabelId = pstmt->pLabelId; pLabelId; pLabelId = pLabelId->next)
- {
- if (pid == pLabelId->pid)
- {
- // Found the label. Make sure we can use it. We can
- // break out of any statement, but we can only
- // continue loops.
- if (fnop == fnopContinue &&
- !(ParseNode::Grfnop(pstmt->op) & fnop))
- {
- Error(ERRbadContinue);
- }
- goto LNeedTerminator;
- }
- }
- }
- }
- Error(ERRnoLabel);
- }
- else
- {
- // If we're doing a fast scan, we're not tracking labels, so we can't accurately do this analysis.
- // Let the thread that's doing the full parse detect the error, if there is one.
- if (!this->m_doingFastScan)
- {
- // Unlabeled break or continue.
- if (buildAST)
- {
- pnode->sxJump.hasExplicitTarget=false;
- PushStmt<buildAST>(&stmt, pnode, pnode->nop, pnodeLabel, nullptr);
- Assert(pnode->sxStmt.grfnop == 0);
- }
- for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
- {
- if (buildAST)
- {
- AnalysisAssert(pstmt->pnodeStmt);
- if (pstmt->pnodeStmt->Grfnop() & fnop)
- {
- pstmt->pnodeStmt->sxStmt.grfnop |= fnop;
- pnode->sxJump.pnodeTarget = pstmt->pnodeStmt;
- PopStmt(&stmt);
- goto LNeedTerminator;
- }
- pnode->sxStmt.grfnop |=
- (pstmt->pnodeStmt->Grfnop() & fnopCleanup);
- }
- else
- {
- if (pstmt->isDeferred)
- {
- if (ParseNode::Grfnop(pstmt->op) & fnop)
- {
- goto LNeedTerminator;
- }
- }
- else
- {
- AnalysisAssert(pstmt->pnodeStmt);
- if (pstmt->pnodeStmt->Grfnop() & fnop)
- {
- pstmt->pnodeStmt->sxStmt.grfnop |= fnop;
- goto LNeedTerminator;
- }
- }
- }
- }
- Error(fnop == fnopBreak ? ERRbadBreak : ERRbadContinue);
- }
- goto LNeedTerminator;
- }
- case tkRETURN:
- {
- if (buildAST)
- {
- if (nullptr == m_currentNodeFunc)
- {
- Error(ERRbadReturn);
- }
- pnode = CreateNodeWithScanner<knopReturn>();
- }
- m_pscan->Scan();
- ParseNodePtr pnodeExpr = nullptr;
- ParseOptionalExpr<buildAST>(&pnodeExpr, true);
- if (buildAST)
- {
- pnode->sxReturn.pnodeExpr = pnodeExpr;
- if (pnodeExpr)
- {
- this->CheckArguments(pnode->sxReturn.pnodeExpr);
- pnode->ichLim = pnode->sxReturn.pnodeExpr->ichLim;
- }
- // See if return should call finally
- PushStmt<buildAST>(&stmt, pnode, knopReturn, pnodeLabel, nullptr);
- Assert(pnode->sxStmt.grfnop == 0);
- for (pstmt = m_pstmtCur; nullptr != pstmt; pstmt = pstmt->pstmtOuter)
- {
- AssertNodeMem(pstmt->pnodeStmt);
- AssertNodeMemN(pstmt->pnodeLab);
- if (pstmt->pnodeStmt->Grfnop() & fnopCleanup)
- {
- pnode->sxStmt.grfnop |= fnopCleanup;
- break;
- }
- }
- PopStmt(&stmt);
- }
- goto LNeedTerminator;
- }
- case tkTHROW:
- {
- if (buildAST)
- {
- pnode = CreateUniNode(knopThrow, nullptr);
- }
- m_pscan->Scan();
- ParseNodePtr pnode1 = nullptr;
- if (m_token.tk != tkSColon &&
- m_token.tk != tkRCurly &&
- !m_pscan->FHadNewLine())
- {
- pnode1 = ParseExpr<buildAST>();
- }
- else
- {
- Error(ERRdanglingThrow);
- }
- if (buildAST)
- {
- pnode->sxUni.pnode1 = pnode1;
- if (pnode1)
- {
- this->CheckArguments(pnode->sxUni.pnode1);
- pnode->ichLim = pnode->sxUni.pnode1->ichLim;
- }
- }
- goto LNeedTerminator;
- }
- case tkDEBUGGER:
- if (buildAST)
- {
- pnode = CreateNodeWithScanner<knopDebugger>();
- }
- m_pscan->Scan();
- goto LNeedTerminator;
- case tkIMPORT:
- if (!(m_grfscr & fscrIsModuleCode))
- {
- goto LDefaultToken;
- }
- pnode = ParseImportDeclaration<buildAST>();
- goto LNeedTerminator;
- case tkEXPORT:
- if (!(m_grfscr & fscrIsModuleCode))
- {
- goto LDefaultToken;
- }
- pnode = ParseExportDeclaration<buildAST>();
- goto LNeedTerminator;
- LDefaultToken:
- default:
- {
- // An expression statement or a label.
- IdentToken tok;
- pnode = ParseExpr<buildAST>(koplNo, nullptr, TRUE, FALSE, nullptr, nullptr /*hintLength*/, nullptr /*hintOffset*/, &tok);
- if (m_hasDeferredShorthandInitError)
- {
- Error(ERRnoColon);
- }
- if (buildAST)
- {
- // Check for a label.
- if (tkColon == m_token.tk &&
- nullptr != pnode && knopName == pnode->nop)
- {
- // We have a label. See if it is already defined.
- if (nullptr != PnodeLabel(pnode->sxPid.pid, pnodeLabel))
- {
- Error(ERRbadLabel);
- // No recovery is necessary since this is a semantic, not structural, error
- }
- pnodeT = CreateNodeWithScanner<knopLabel>();
- pnodeT->sxLabel.pid = pnode->sxPid.pid;
- pnodeT->sxLabel.pnodeNext = pnodeLabel;
- pnodeLabel = pnodeT;
- m_pscan->Scan();
- goto LRestart;
- }
- expressionStmt = true;
- AnalysisAssert(pnode);
- pnode->isUsed = false;
- }
- else
- {
- // Check for a label.
- if (tkColon == m_token.tk && tok.tk == tkID)
- {
- tok.pid = m_pscan->PidAt(tok.ichMin, tok.ichLim);
- if (PnodeLabelNoAST(&tok, pLabelIdList))
- {
- Error(ERRbadLabel);
- }
- LabelId* pLabelId = CreateLabelId(&tok);
- pLabelId->next = pLabelIdList;
- pLabelIdList = pLabelId;
- m_pscan->Scan();
- goto LRestart;
- }
- }
- }
- LNeedTerminator:
- // Need a semicolon, new-line, } or end-of-file.
- // We digest a semicolon if it's there.
- switch (m_token.tk)
- {
- case tkSColon:
- m_pscan->Scan();
- if (pnode!= nullptr) pnode->grfpn |= PNodeFlags::fpnExplicitSemicolon;
- break;
- case tkEOF:
- case tkRCurly:
- if (pnode!= nullptr) pnode->grfpn |= PNodeFlags::fpnAutomaticSemicolon;
- break;
- default:
- if (!m_pscan->FHadNewLine())
- {
- Error(ERRnoSemic);
- }
- else
- {
- if (pnode!= nullptr) pnode->grfpn |= PNodeFlags::fpnAutomaticSemicolon;
- }
- break;
- }
- break;
- }
- if (m_hasDeferredShorthandInitError)
- {
- Error(ERRnoColon);
- }
- if (buildAST)
- {
- // All non expression statements excluded from the "this.x" optimization
- // Another check while parsing expressions
- if (!expressionStmt)
- {
- if (m_currentNodeFunc)
- {
- m_currentNodeFunc->sxFnc.SetHasNonThisStmt();
- }
- else if (m_currentNodeProg)
- {
- m_currentNodeProg->sxFnc.SetHasNonThisStmt();
- }
- }
- #if EXCEPTION_RECOVERY
- // close the try/catch block
- if(Js::Configuration::Global.flags.SwallowExceptions)
- {
- // pop the try block and fill in the body
- PopStmt(&stmtTryBlock);
- pTryBlock->sxBlock.pnodeStmt = pnode;
- PopStmt(&stmtTry);
- if(pnode != nullptr)
- {
- pTry->ichLim = pnode->ichLim;
- }
- pTry->sxTry.pnodeBody = pTryBlock;
- // create a catch block with an empty body
- StmtNest stmtCatch;
- ParseNodePtr pCatch;
- pCatch = CreateNodeWithScanner<knopCatch>();
- PushStmt<buildAST>(&stmtCatch, pCatch, knopCatch, nullptr, nullptr);
- pCatch->sxCatch.pnodeBody = nullptr;
- if(pnode != nullptr)
- {
- pCatch->ichLim = pnode->ichLim;
- }
- pCatch->sxCatch.grfnop = 0;
- pCatch->sxCatch.pnodeNext = nullptr;
- // create a fake name for the catch var.
- const WCHAR *uniqueNameStr = _u("__ehobj");
- IdentPtr uniqueName = m_phtbl->PidHashNameLen(uniqueNameStr, static_cast<long>(wcslen(uniqueNameStr)));
- pCatch->sxCatch.pnodeParam = CreateNameNode(uniqueName);
- // Add this catch to the current list. We don't bother adjusting the catch and function expression
- // lists here because the catch is just an empty statement.
- if (m_ppnodeExprScope)
- {
- Assert(*m_ppnodeExprScope == nullptr);
- *m_ppnodeExprScope = pCatch;
- m_ppnodeExprScope = &pCatch->sxCatch.pnodeNext;
- }
- else
- {
- Assert(m_ppnodeScope);
- Assert(*m_ppnodeScope == nullptr);
- *m_ppnodeScope = pCatch;
- m_ppnodeScope = &pCatch->sxCatch.pnodeNext;
- }
- pCatch->sxCatch.pnodeScopes = nullptr;
- PopStmt(&stmtCatch);
- // fill in and pop the try-catch
- pParentTryCatch->sxTryCatch.pnodeTry = pTry;
- pParentTryCatch->sxTryCatch.pnodeCatch = pCatch;
- PopStmt(&stmtTryCatch);
- PopStmt(&stmtTryCatchBlock);
- // replace the node that's being returned
- pParentTryCatchBlock->sxBlock.pnodeStmt = pParentTryCatch;
- pnode = pParentTryCatchBlock;
- }
- #endif // EXCEPTION_RECOVERY
- }
- return pnode;
- }
- BOOL
- Parser::TokIsForInOrForOf()
- {
- return m_token.tk == tkIN ||
- (m_token.tk == tkID &&
- m_token.GetIdentifier(m_phtbl) == wellKnownPropertyPids.of);
- }
- /***************************************************************************
- Parse a sequence of statements.
- ***************************************************************************/
- template<bool buildAST>
- void Parser::ParseStmtList(ParseNodePtr *ppnodeList, ParseNodePtr **pppnodeLast, StrictModeEnvironment smEnvironment, const bool isSourceElementList, bool* strictModeOn)
- {
- BOOL doneDirectives = !isSourceElementList; // directives may only exist in a SourceElementList, not a StatementList
- BOOL seenDirectiveContainingOctal = false; // Have we seen an octal directive before a use strict directive?
- BOOL old_UseStrictMode = m_fUseStrictMode;
- ParseNodePtr pnodeStmt;
- ParseNodePtr *lastNodeRef = nullptr;
- if (buildAST)
- {
- AssertMem(ppnodeList);
- AssertMemN(pppnodeLast);
- *ppnodeList = nullptr;
- }
- if(CONFIG_FLAG(ForceStrictMode))
- {
- m_fUseStrictMode = TRUE;
- }
- for (;;)
- {
- switch (m_token.tk)
- {
- case tkCASE:
- case tkDEFAULT:
- case tkRCurly:
- case tkEOF:
- if (buildAST && nullptr != pppnodeLast)
- {
- *pppnodeLast = lastNodeRef;
- }
- if (!buildAST)
- {
- m_fUseStrictMode = old_UseStrictMode;
- }
- return;
- }
- if (doneDirectives == FALSE)
- {
- bool isOctalInString = false;
- bool isUseStrictDirective = false;
- bool isUseAsmDirective = false;
- if (smEnvironment != SM_NotUsed && CheckForDirective(&isUseStrictDirective, &isUseAsmDirective, &isOctalInString))
- {
- // Ignore "use asm" statement when not building the AST
- isUseAsmDirective &= buildAST;
- if (isUseStrictDirective)
- {
- // Functions with non-simple parameter list cannot be made strict mode
- if (GetCurrentFunctionNode()->sxFnc.HasNonSimpleParameterList())
- {
- Error(ERRNonSimpleParamListInStrictMode);
- }
- if (seenDirectiveContainingOctal)
- {
- // Directives seen before a "use strict" cannot contain an octal.
- Error(ERRES5NoOctal);
- }
- if (!buildAST)
- {
- // Turning on strict mode in deferred code.
- m_fUseStrictMode = TRUE;
- if (!m_inDeferredNestedFunc)
- {
- // Top-level deferred function, so there's a parse node
- Assert(m_currentNodeFunc != nullptr);
- m_currentNodeFunc->sxFnc.SetStrictMode();
- }
- else if (strictModeOn)
- {
- // This turns on strict mode in a deferred function, we need to go back
- // and re-check duplicated formals.
- *strictModeOn = true;
- }
- }
- else
- {
- if (smEnvironment == SM_OnGlobalCode)
- {
- // Turning on strict mode at the top level
- m_fUseStrictMode = TRUE;
- }
- else
- {
- // i.e. smEnvironment == SM_OnFunctionCode
- Assert(m_currentNodeFunc != nullptr);
- m_currentNodeFunc->sxFnc.SetStrictMode();
- }
- }
- }
- else if (isUseAsmDirective)
- {
- if (smEnvironment != SM_OnGlobalCode) //Top level use asm doesn't mean anything.
- {
- // i.e. smEnvironment == SM_OnFunctionCode
- Assert(m_currentNodeFunc != nullptr);
- m_currentNodeFunc->sxFnc.SetAsmjsMode();
- m_InAsmMode = true;
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(AsmJSFunctionCount, m_scriptContext);
- }
- }
- else if (isOctalInString)
- {
- seenDirectiveContainingOctal = TRUE;
- }
- }
- else
- {
- // The first time we see anything other than a directive we can have no more directives.
- doneDirectives = TRUE;
- }
- }
- if (nullptr != (pnodeStmt = ParseStatement<buildAST>()))
- {
- if (buildAST)
- {
- AddToNodeList(ppnodeList, &lastNodeRef, pnodeStmt);
- }
- }
- }
- }
- template <class Fn>
- void Parser::VisitFunctionsInScope(ParseNodePtr pnodeScopeList, Fn fn)
- {
- ParseNodePtr pnodeScope;
- for (pnodeScope = pnodeScopeList; pnodeScope;)
- {
- switch (pnodeScope->nop)
- {
- case knopBlock:
- VisitFunctionsInScope(pnodeScope->sxBlock.pnodeScopes, fn);
- pnodeScope = pnodeScope->sxBlock.pnodeNext;
- break;
- case knopFncDecl:
- fn(pnodeScope);
- pnodeScope = pnodeScope->sxFnc.pnodeNext;
- break;
- case knopCatch:
- VisitFunctionsInScope(pnodeScope->sxCatch.pnodeScopes, fn);
- pnodeScope = pnodeScope->sxCatch.pnodeNext;
- break;
- case knopWith:
- VisitFunctionsInScope(pnodeScope->sxWith.pnodeScopes, fn);
- pnodeScope = pnodeScope->sxWith.pnodeNext;
- break;
- default:
- AssertMsg(false, "Unexpected node with scope list");
- return;
- }
- }
- }
- // Scripts above this size (minus string literals and comments) will have parsing of
- // function bodies deferred.
- ULONG Parser::GetDeferralThreshold(bool isProfileLoaded)
- {
- #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
- if (CONFIG_FLAG(ForceDeferParse) ||
- PHASE_FORCE1(Js::DeferParsePhase) ||
- Js::Configuration::Global.flags.IsEnabled(Js::ForceUndoDeferFlag))
- {
- return 0;
- }
- else if (Js::Configuration::Global.flags.IsEnabled(Js::DeferParseFlag))
- {
- return Js::Configuration::Global.flags.DeferParse;
- }
- else
- #endif
- {
- if (isProfileLoaded)
- {
- return DEFAULT_CONFIG_ProfileBasedDeferParseThreshold;
- }
- return DEFAULT_CONFIG_DeferParseThreshold;
- }
- }
- void Parser::FinishDeferredFunction(ParseNodePtr pnodeScopeList)
- {
- VisitFunctionsInScope(pnodeScopeList,
- [this](ParseNodePtr pnodeFnc)
- {
- Assert(pnodeFnc->nop == knopFncDecl);
- // Non-simple params (such as default) require a good amount of logic to put vars on appriopriate scopes. ParseFncDecl handles it
- // properly (both on defer and non-defer case). This is to avoid write duplicated logic here as well. Function with non-simple-param
- // will remain deferred untill they are called.
- if (pnodeFnc->sxFnc.pnodeBody == nullptr && !pnodeFnc->sxFnc.HasNonSimpleParameterList())
- {
- // Go back and generate an AST for this function.
- JS_ETW(EventWriteJSCRIPT_PARSE_FUNC(this->GetScriptContext(), pnodeFnc->sxFnc.functionId, /*Undefer*/TRUE));
- ParseNodePtr pnodeFncSave = this->m_currentNodeFunc;
- this->m_currentNodeFunc = pnodeFnc;
- ParseNodePtr pnodeFncExprBlock = nullptr;
- if (pnodeFnc->sxFnc.pnodeName &&
- !pnodeFnc->sxFnc.IsDeclaration())
- {
- // Set up the named function expression symbol so references inside the function can be bound.
- ParseNodePtr pnodeName = pnodeFnc->sxFnc.pnodeName;
- Assert(pnodeName->nop == knopVarDecl);
- Assert(pnodeName->sxVar.pnodeNext == nullptr);
- pnodeFncExprBlock = this->StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FuncExpr);
- PidRefStack *ref = this->PushPidRef(pnodeName->sxVar.pid);
- pnodeName->sxVar.symRef = ref->GetSymRef();
- ref->SetSym(pnodeName->sxVar.sym);
- Scope *fncExprScope = pnodeFncExprBlock->sxBlock.scope;
- fncExprScope->AddNewSymbol(pnodeName->sxVar.sym);
- pnodeFnc->sxFnc.scope = fncExprScope;
- }
- ParseNodePtr pnodeBlock = this->StartParseBlock<true>(PnodeBlockType::Parameter, ScopeType_Parameter);
- pnodeFnc->sxFnc.pnodeScopes = pnodeBlock;
- m_ppnodeScope = &pnodeBlock->sxBlock.pnodeScopes;
- pnodeBlock->sxBlock.pnodeStmt = pnodeFnc;
- // Add the args to the scope, since we won't re-parse those.
- Scope *scope = pnodeBlock->sxBlock.scope;
- auto addArgsToScope = [&](ParseNodePtr pnodeArg) {
- if (pnodeArg->IsVarLetOrConst())
- {
- PidRefStack *ref = this->PushPidRef(pnodeArg->sxVar.pid);
- pnodeArg->sxVar.symRef = ref->GetSymRef();
- if (ref->GetSym() != nullptr)
- {
- // Duplicate parameter in a configuration that allows them.
- // The symbol is already in the scope, just point it to the right declaration.
- Assert(ref->GetSym() == pnodeArg->sxVar.sym);
- ref->GetSym()->SetDecl(pnodeArg);
- }
- else
- {
- ref->SetSym(pnodeArg->sxVar.sym);
- scope->AddNewSymbol(pnodeArg->sxVar.sym);
- }
- }
- };
- MapFormals(pnodeFnc, addArgsToScope);
- MapFormalsFromPattern(pnodeFnc, addArgsToScope);
- ParseNodePtr pnodeInnerBlock = this->StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
- pnodeFnc->sxFnc.pnodeBodyScope = pnodeInnerBlock;
- // Set the parameter block's child to the function body block.
- *m_ppnodeScope = pnodeInnerBlock;
- ParseNodePtr *ppnodeScopeSave = nullptr;
- ParseNodePtr *ppnodeExprScopeSave = nullptr;
- ppnodeScopeSave = m_ppnodeScope;
- // This synthetic block scope will contain all the nested scopes.
- m_ppnodeScope = &pnodeInnerBlock->sxBlock.pnodeScopes;
- pnodeInnerBlock->sxBlock.pnodeStmt = pnodeFnc;
- // Keep nested function declarations and expressions in the same list at function scope.
- // (Indicate this by nulling out the current function expressions list.)
- ppnodeExprScopeSave = m_ppnodeExprScope;
- m_ppnodeExprScope = nullptr;
- // Shouldn't be any temps in the arg list.
- Assert(*m_ppnodeVar == nullptr);
- // Start the var list.
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- Assert(m_currentNodeNonLambdaFunc == nullptr);
- m_currentNodeNonLambdaFunc = pnodeFnc;
- this->FinishFncNode(pnodeFnc);
- Assert(pnodeFnc == m_currentNodeNonLambdaFunc);
- m_currentNodeNonLambdaFunc = nullptr;
- m_ppnodeExprScope = ppnodeExprScopeSave;
- AssertMem(m_ppnodeScope);
- Assert(nullptr == *m_ppnodeScope);
- m_ppnodeScope = ppnodeScopeSave;
- this->FinishParseBlock(pnodeInnerBlock);
- this->AddArgumentsNodeToVars(pnodeFnc);
- this->FinishParseBlock(pnodeBlock);
- if (pnodeFncExprBlock)
- {
- this->FinishParseBlock(pnodeFncExprBlock);
- }
- this->m_currentNodeFunc = pnodeFncSave;
- }
- });
- }
- void Parser::InitPids()
- {
- AssertMemN(m_phtbl);
- wellKnownPropertyPids.arguments = m_phtbl->PidHashNameLen(g_ssym_arguments.sz, g_ssym_arguments.cch);
- wellKnownPropertyPids.async = m_phtbl->PidHashNameLen(g_ssym_async.sz, g_ssym_async.cch);
- wellKnownPropertyPids.eval = m_phtbl->PidHashNameLen(g_ssym_eval.sz, g_ssym_eval.cch);
- wellKnownPropertyPids.get = m_phtbl->PidHashNameLen(g_ssym_get.sz, g_ssym_get.cch);
- wellKnownPropertyPids.set = m_phtbl->PidHashNameLen(g_ssym_set.sz, g_ssym_set.cch);
- wellKnownPropertyPids.let = m_phtbl->PidHashNameLen(g_ssym_let.sz, g_ssym_let.cch);
- wellKnownPropertyPids.constructor = m_phtbl->PidHashNameLen(g_ssym_constructor.sz, g_ssym_constructor.cch);
- wellKnownPropertyPids.prototype = m_phtbl->PidHashNameLen(g_ssym_prototype.sz, g_ssym_prototype.cch);
- wellKnownPropertyPids.__proto__ = m_phtbl->PidHashNameLen(_u("__proto__"), sizeof("__proto__") - 1);
- wellKnownPropertyPids.of = m_phtbl->PidHashNameLen(_u("of"), sizeof("of") - 1);
- wellKnownPropertyPids.target = m_phtbl->PidHashNameLen(_u("target"), sizeof("target") - 1);
- wellKnownPropertyPids.as = m_phtbl->PidHashNameLen(_u("as"), sizeof("as") - 1);
- wellKnownPropertyPids.from = m_phtbl->PidHashNameLen(_u("from"), sizeof("from") - 1);
- wellKnownPropertyPids._default = m_phtbl->PidHashNameLen(_u("default"), sizeof("default") - 1);
- wellKnownPropertyPids._starDefaultStar = m_phtbl->PidHashNameLen(_u("*default*"), sizeof("*default*") - 1);
- wellKnownPropertyPids._star = m_phtbl->PidHashNameLen(_u("*"), sizeof("*") - 1);
- }
- void Parser::RestoreScopeInfo(Js::FunctionBody* functionBody)
- {
- if (!functionBody)
- {
- return;
- }
- Js::ScopeInfo* scopeInfo = functionBody->GetScopeInfo();
- if (!scopeInfo)
- {
- return;
- }
- if (this->IsBackgroundParser())
- {
- PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
- }
- else
- {
- PROBE_STACK(m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
- }
- RestoreScopeInfo(scopeInfo->GetParent()); // Recursively restore outer func scope info
- Js::ScopeInfo* funcExprScopeInfo = scopeInfo->GetFuncExprScopeInfo();
- if (funcExprScopeInfo)
- {
- funcExprScopeInfo->SetScopeId(m_nextBlockId);
- ParseNodePtr pnodeFncExprScope = StartParseBlockWithCapacity<true>(PnodeBlockType::Function, ScopeType_FuncExpr, funcExprScopeInfo->GetSymbolCount());
- Scope *scope = pnodeFncExprScope->sxBlock.scope;
- funcExprScopeInfo->GetScopeInfo(this, nullptr, nullptr, scope);
- }
- Js::ScopeInfo* paramScopeInfo = scopeInfo->GetParamScopeInfo();
- if (paramScopeInfo)
- {
- paramScopeInfo->SetScopeId(m_nextBlockId);
- ParseNodePtr pnodeFncExprScope = StartParseBlockWithCapacity<true>(PnodeBlockType::Parameter, ScopeType_Parameter, paramScopeInfo->GetSymbolCount());
- Scope *scope = pnodeFncExprScope->sxBlock.scope;
- paramScopeInfo->GetScopeInfo(this, nullptr, nullptr, scope);
- }
- scopeInfo->SetScopeId(m_nextBlockId);
- ParseNodePtr pnodeFncScope = nullptr;
- if (scopeInfo->IsGlobalEval())
- {
- pnodeFncScope = StartParseBlockWithCapacity<true>(PnodeBlockType::Regular, ScopeType_GlobalEvalBlock, scopeInfo->GetSymbolCount());
- }
- else
- {
- pnodeFncScope = StartParseBlockWithCapacity<true>(PnodeBlockType::Function, ScopeType_FunctionBody, scopeInfo->GetSymbolCount());
- }
- Scope *scope = pnodeFncScope->sxBlock.scope;
- scopeInfo->GetScopeInfo(this, nullptr, nullptr, scope);
- }
- void Parser::FinishScopeInfo(Js::FunctionBody *functionBody)
- {
- if (!functionBody)
- {
- return;
- }
- Js::ScopeInfo* scopeInfo = functionBody->GetScopeInfo();
- if (!scopeInfo)
- {
- return;
- }
- if (this->IsBackgroundParser())
- {
- PROBE_STACK_NO_DISPOSE(m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
- }
- else
- {
- PROBE_STACK(m_scriptContext, Js::Constants::MinStackByteCodeVisitor);
- }
- int scopeId = scopeInfo->GetScopeId();
- scopeInfo->GetScope()->ForEachSymbol([this, scopeId](Symbol *sym)
- {
- this->BindPidRefsInScope(sym->GetPid(), sym, scopeId);
- });
- PopScope(scopeInfo->GetScope());
- PopStmt(&m_currentBlockInfo->pstmt);
- PopBlockInfo();
- Js::ScopeInfo *paramScopeInfo = scopeInfo->GetParamScopeInfo();
- if (paramScopeInfo)
- {
- scopeId = paramScopeInfo->GetScopeId();
- paramScopeInfo->GetScope()->ForEachSymbol([this, scopeId](Symbol *sym)
- {
- this->BindPidRefsInScope(sym->GetPid(), sym, scopeId);
- });
- PopScope(paramScopeInfo->GetScope());
- PopStmt(&m_currentBlockInfo->pstmt);
- PopBlockInfo();
- }
- Js::ScopeInfo *funcExprScopeInfo = scopeInfo->GetFuncExprScopeInfo();
- if (funcExprScopeInfo)
- {
- scopeId = funcExprScopeInfo->GetScopeId();
- funcExprScopeInfo->GetScope()->ForEachSymbol([this, scopeId](Symbol *sym)
- {
- this->BindPidRefsInScope(sym->GetPid(), sym, scopeId);
- });
- PopScope(funcExprScopeInfo->GetScope());
- PopStmt(&m_currentBlockInfo->pstmt);
- PopBlockInfo();
- }
- FinishScopeInfo(scopeInfo->GetParent());
- }
- /***************************************************************************
- Parse the code.
- ***************************************************************************/
- ParseNodePtr Parser::Parse(LPCUTF8 pszSrc, size_t offset, size_t length, charcount_t charOffset, ULONG grfscr, ULONG lineNumber, Js::LocalFunctionId * nextFunctionId, CompileScriptException *pse)
- {
- ParseNodePtr pnodeProg;
- ParseNodePtr *lastNodeRef = nullptr;
- m_nextBlockId = 0;
- // Scanner should run in Running mode and not syntax coloring mode
- grfscr &= ~fscrSyntaxColor;
- if (this->m_scriptContext->IsScriptContextInDebugMode()
- #ifdef ENABLE_PREJIT
- || Js::Configuration::Global.flags.Prejit
- #endif
- || ((grfscr & fscrNoDeferParse) != 0)
- )
- {
- // Don't do deferred parsing if debugger is attached or feature is disabled
- // by command-line switch.
- grfscr &= ~fscrDeferFncParse;
- }
- else if (!(grfscr & fscrGlobalCode) &&
- (
- PHASE_OFF1(Js::Phase::DeferEventHandlersPhase) ||
- this->m_scriptContext->IsScriptContextInSourceRundownOrDebugMode()
- )
- )
- {
- // Don't defer event handlers in debug/rundown mode, because we need to register the document,
- // so we need to create a full FunctionBody for the script body.
- grfscr &= ~fscrDeferFncParse;
- }
- bool isDeferred = (grfscr & fscrDeferredFnc) != 0;
- bool isModuleSource = (grfscr & fscrIsModuleCode) != 0;
- m_grfscr = grfscr;
- m_length = length;
- m_originalLength = length;
- m_nextFunctionId = nextFunctionId;
- if(m_parseType != ParseType_Deferred)
- {
- JS_ETW(EventWriteJSCRIPT_PARSE_METHOD_START(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), *m_nextFunctionId, 0, m_parseType, Js::Constants::GlobalFunction));
- OUTPUT_TRACE(Js::DeferParsePhase, _u("Parsing function (%s) : %s (%d)\n"), GetParseType(), Js::Constants::GlobalFunction, *m_nextFunctionId);
- }
- // Give the scanner the source and get the first token
- m_pscan->SetText(pszSrc, offset, length, charOffset, grfscr, lineNumber);
- m_pscan->Scan();
- // Make the main 'knopProg' node
- long initSize = 0;
- m_pCurrentAstSize = &initSize;
- pnodeProg = CreateProgNodeWithScanner(isModuleSource);
- pnodeProg->grfpn = PNodeFlags::fpnNone;
- pnodeProg->sxFnc.pid = nullptr;
- pnodeProg->sxFnc.pnodeName = nullptr;
- pnodeProg->sxFnc.pnodeRest = nullptr;
- pnodeProg->sxFnc.ClearFlags();
- pnodeProg->sxFnc.SetNested(FALSE);
- pnodeProg->sxFnc.astSize = 0;
- pnodeProg->sxFnc.cbMin = m_pscan->IecpMinTok();
- pnodeProg->sxFnc.lineNumber = lineNumber;
- pnodeProg->sxFnc.columnNumber = 0;
- if (!isDeferred || (isDeferred && grfscr & fscrGlobalCode))
- {
- // In the deferred case, if the global function is deferred parse (which is in no-refresh case),
- // we will re-use the same function body, so start with the correct functionId.
- pnodeProg->sxFnc.functionId = (*m_nextFunctionId)++;
- }
- else
- {
- pnodeProg->sxFnc.functionId = Js::Constants::NoFunctionId;
- }
- if (isModuleSource)
- {
- Assert(m_scriptContext->GetConfig()->IsES6ModuleEnabled());
- pnodeProg->sxModule.localExportEntries = nullptr;
- pnodeProg->sxModule.indirectExportEntries = nullptr;
- pnodeProg->sxModule.starExportEntries = nullptr;
- pnodeProg->sxModule.importEntries = nullptr;
- pnodeProg->sxModule.requestedModules = nullptr;
- }
- m_pCurrentAstSize = & (pnodeProg->sxFnc.astSize);
- pnodeProg->sxFnc.hint = nullptr;
- pnodeProg->sxFnc.hintLength = 0;
- pnodeProg->sxFnc.hintOffset = 0;
- pnodeProg->sxFnc.isNameIdentifierRef = true;
- // initialize parsing variables
- pnodeProg->sxFnc.pnodeNext = nullptr;
- m_currentNodeFunc = nullptr;
- m_currentNodeDeferredFunc = nullptr;
- m_currentNodeProg = pnodeProg;
- m_cactIdentToNodeLookup = 1;
- pnodeProg->sxFnc.nestedCount = 0;
- m_pnestedCount = &pnodeProg->sxFnc.nestedCount;
- m_inDeferredNestedFunc = false;
- pnodeProg->sxFnc.pnodeParams = nullptr;
- pnodeProg->sxFnc.pnodeVars = nullptr;
- pnodeProg->sxFnc.pnodeRest = nullptr;
- m_ppnodeVar = &pnodeProg->sxFnc.pnodeVars;
- SetCurrentStatement(nullptr);
- AssertMsg(m_pstmtCur == nullptr, "Statement stack should be empty when we start parse global code");
- // Create block for const's and let's
- ParseNodePtr pnodeGlobalBlock = StartParseBlock<true>(PnodeBlockType::Global, ScopeType_Global);
- pnodeProg->sxProg.scope = pnodeGlobalBlock->sxBlock.scope;
- ParseNodePtr pnodeGlobalEvalBlock = nullptr;
- // Don't track function expressions separately from declarations at global scope.
- m_ppnodeExprScope = nullptr;
- // This synthetic block scope will contain all the nested scopes.
- pnodeProg->sxFnc.pnodeBodyScope = nullptr;
- pnodeProg->sxFnc.pnodeScopes = pnodeGlobalBlock;
- m_ppnodeScope = &pnodeGlobalBlock->sxBlock.pnodeScopes;
- if ((this->m_grfscr & fscrEvalCode) &&
- !(this->m_functionBody && this->m_functionBody->GetScopeInfo()))
- {
- pnodeGlobalEvalBlock = StartParseBlock<true>(PnodeBlockType::Regular, ScopeType_GlobalEvalBlock);
- pnodeProg->sxFnc.pnodeScopes = pnodeGlobalEvalBlock;
- m_ppnodeScope = &pnodeGlobalEvalBlock->sxBlock.pnodeScopes;
- }
- Js::ScopeInfo *scopeInfo = nullptr;
- if (m_parseType == ParseType_Deferred && m_functionBody)
- {
- // this->m_functionBody can be cleared during parsing, but we need access to the scope info later.
- scopeInfo = m_functionBody->GetScopeInfo();
- if (scopeInfo)
- {
- this->RestoreScopeInfo(scopeInfo->GetParent());
- }
- }
- // Process a sequence of statements/declarations
- ParseStmtList<true>(
- &pnodeProg->sxFnc.pnodeBody,
- &lastNodeRef,
- SM_OnGlobalCode,
- !(m_grfscr & fscrDeferredFncExpression) /* isSourceElementList */);
- if (m_parseType == ParseType_Deferred)
- {
- if (scopeInfo)
- {
- this->FinishScopeInfo(scopeInfo->GetParent());
- }
- }
- pnodeProg->sxProg.m_UsesArgumentsAtGlobal = m_UsesArgumentsAtGlobal;
- if (IsStrictMode())
- {
- pnodeProg->sxFnc.SetStrictMode();
- }
- #if DEBUG
- if(m_grfscr & fscrEnforceJSON && !IsJSONValid(pnodeProg->sxFnc.pnodeBody))
- {
- Error(ERRsyntax);
- }
- #endif
- if (tkEOF != m_token.tk)
- Error(ERRsyntax);
- // Append an EndCode node.
- AddToNodeList(&pnodeProg->sxFnc.pnodeBody, &lastNodeRef,
- CreateNodeWithScanner<knopEndCode>());
- AssertMem(lastNodeRef);
- AssertNodeMem(*lastNodeRef);
- Assert((*lastNodeRef)->nop == knopEndCode);
- (*lastNodeRef)->ichMin = 0;
- (*lastNodeRef)->ichLim = 0;
- // Get the extent of the code.
- pnodeProg->ichLim = m_pscan->IchLimTok();
- pnodeProg->sxFnc.cbLim = m_pscan->IecpLimTok();
- // save the temps and terminate the local list
- // NOTE: Eze makes no use of this.
- //pnodeProg->sxFnc.pnodeTmps = *m_ppnodeVar;
- *m_ppnodeVar = nullptr;
- Assert(nullptr == *m_ppnodeScope);
- Assert(nullptr == pnodeProg->sxFnc.pnodeNext);
- #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
- if (Js::Configuration::Global.flags.IsEnabled(Js::ForceUndoDeferFlag))
- {
- m_stoppedDeferredParse = true;
- }
- #endif
- if (m_stoppedDeferredParse)
- {
- if (this->m_hasParallelJob)
- {
- #if ENABLE_BACKGROUND_PARSING
- BackgroundParser *bgp = static_cast<BackgroundParser*>(m_scriptContext->GetBackgroundParser());
- Assert(bgp);
- this->WaitForBackgroundJobs(bgp, pse);
- #endif
- }
- // Finally, see if there are any function bodies we now want to generate because we
- // decided to stop deferring.
- FinishDeferredFunction(pnodeProg->sxFnc.pnodeScopes);
- }
- if (pnodeGlobalEvalBlock)
- {
- FinishParseBlock(pnodeGlobalEvalBlock);
- }
- // Append block as body of pnodeProg
- FinishParseBlock(pnodeGlobalBlock);
- m_scriptContext->AddSourceSize(m_length);
- if (m_parseType != ParseType_Deferred)
- {
- JS_ETW(EventWriteJSCRIPT_PARSE_METHOD_STOP(m_sourceContextInfo->dwHostSourceContext, GetScriptContext(), pnodeProg->sxFnc.functionId, *m_pCurrentAstSize, false, Js::Constants::GlobalFunction));
- }
- return pnodeProg;
- }
- bool Parser::CheckForDirective(bool* pIsUseStrict, bool *pIsUseAsm, bool* pIsOctalInString)
- {
- // A directive is a string constant followed by a statement terminating token
- if (m_token.tk != tkStrCon)
- return false;
- // Careful, need to check for octal before calling m_pscan->Scan()
- // because Scan() clears the "had octal" flag on the scanner and
- // m_pscan->Restore() does not restore this flag.
- if (pIsOctalInString != nullptr)
- {
- *pIsOctalInString = m_pscan->IsOctOrLeadingZeroOnLastTKNumber();
- }
- Ident* pidDirective = m_token.GetStr();
- RestorePoint start;
- m_pscan->Capture(&start);
- m_pscan->Scan();
- bool isDirective = true;
- switch (m_token.tk)
- {
- case tkSColon:
- case tkEOF:
- case tkLCurly:
- case tkRCurly:
- break;
- default:
- if (!m_pscan->FHadNewLine())
- {
- isDirective = false;
- }
- break;
- }
- if (isDirective)
- {
- if (pIsUseStrict != nullptr)
- {
- *pIsUseStrict = CheckStrictModeStrPid(pidDirective);
- }
- if (pIsUseAsm != nullptr)
- {
- *pIsUseAsm = CheckAsmjsModeStrPid(pidDirective);
- }
- }
- m_pscan->SeekTo(start);
- return isDirective;
- }
- bool Parser::CheckStrictModeStrPid(IdentPtr pid)
- {
- #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
- if (Js::Configuration::Global.flags.NoStrictMode)
- return false;
- #endif
- return pid != nullptr &&
- pid->Cch() == 10 &&
- !m_pscan->IsEscapeOnLastTkStrCon() &&
- wcsncmp(pid->Psz(), _u("use strict"), 10) == 0;
- }
- bool Parser::CheckAsmjsModeStrPid(IdentPtr pid)
- {
- #ifdef ASMJS_PLAT
- if (!CONFIG_FLAG_RELEASE(Asmjs))
- {
- return false;
- }
- bool isAsmCandidate = (pid != nullptr &&
- AutoSystemInfo::Data.SSE2Available() &&
- pid->Cch() == 7 &&
- !m_pscan->IsEscapeOnLastTkStrCon() &&
- wcsncmp(pid->Psz(), _u("use asm"), 10) == 0);
- if (isAsmCandidate && m_scriptContext->IsScriptContextInDebugMode())
- {
- // We would like to report this to debugger - they may choose to disable debugging.
- // TODO : localization of the string?
- m_scriptContext->RaiseMessageToDebugger(DEIT_ASMJS_IN_DEBUGGING, _u("AsmJs initialization error - AsmJs disabled due to script debugger"), !m_sourceContextInfo->IsDynamic() ? m_sourceContextInfo->url : nullptr);
- return false;
- }
- return isAsmCandidate && !(m_grfscr & fscrNoAsmJs);
- #else
- return false;
- #endif
- }
- HRESULT Parser::ParseUtf8Source(__out ParseNodePtr* parseTree, LPCUTF8 pSrc, size_t length, ULONG grfsrc, CompileScriptException *pse,
- Js::LocalFunctionId * nextFunctionId, SourceContextInfo * sourceContextInfo)
- {
- m_functionBody = nullptr;
- m_parseType = ParseType_Upfront;
- return ParseSourceInternal( parseTree, pSrc, 0, length, 0, true, grfsrc, pse, nextFunctionId, 0, sourceContextInfo);
- }
- HRESULT Parser::ParseCesu8Source(__out ParseNodePtr* parseTree, LPCUTF8 pSrc, size_t length, ULONG grfsrc, CompileScriptException *pse,
- Js::LocalFunctionId * nextFunctionId, SourceContextInfo * sourceContextInfo)
- {
- m_functionBody = nullptr;
- m_parseType = ParseType_Upfront;
- return ParseSourceInternal( parseTree, pSrc, 0, length, 0, false, grfsrc, pse, nextFunctionId, 0, sourceContextInfo);
- }
- void Parser::PrepareScanner(bool fromExternal)
- {
- // NOTE: HashTbl and Scanner are currently allocated from the CRT heap. If we want to allocate them from the
- // parser arena, then we also need to change the way the HashTbl allocates PID's from its underlying
- // allocator (which also currently uses the CRT heap). This is not trivial, because we still need to support
- // heap allocation for the colorizer interface.
- // create the hash table and init PID members
- if (nullptr == (m_phtbl = HashTbl::Create(HASH_TABLE_SIZE, &m_err)))
- Error(ERRnoMemory);
- InitPids();
- // create the scanner
- if (nullptr == (m_pscan = Scanner_t::Create(this, m_phtbl, &m_token, &m_err, m_scriptContext)))
- Error(ERRnoMemory);
- if (fromExternal)
- m_pscan->FromExternalSource();
- }
- #if ENABLE_BACKGROUND_PARSING
- void Parser::PrepareForBackgroundParse()
- {
- m_pscan->PrepareForBackgroundParse(m_scriptContext);
- }
- void Parser::AddBackgroundParseItem(BackgroundParseItem *const item)
- {
- if (currBackgroundParseItem == nullptr)
- {
- backgroundParseItems = item;
- }
- else
- {
- currBackgroundParseItem->SetNext(item);
- }
- currBackgroundParseItem = item;
- }
- #endif
- void Parser::AddFastScannedRegExpNode(ParseNodePtr const pnode)
- {
- Assert(!IsBackgroundParser());
- Assert(m_doingFastScan);
- if (fastScannedRegExpNodes == nullptr)
- {
- fastScannedRegExpNodes = Anew(&m_nodeAllocator, NodeDList, &m_nodeAllocator);
- }
- fastScannedRegExpNodes->Append(pnode);
- }
- #if ENABLE_BACKGROUND_PARSING
- void Parser::AddBackgroundRegExpNode(ParseNodePtr const pnode)
- {
- Assert(IsBackgroundParser());
- Assert(currBackgroundParseItem != nullptr);
- currBackgroundParseItem->AddRegExpNode(pnode, &m_nodeAllocator);
- }
- #endif
- HRESULT Parser::ParseFunctionInBackground(ParseNodePtr pnodeFnc, ParseContext *parseContext, bool topLevelDeferred, CompileScriptException *pse)
- {
- m_functionBody = nullptr;
- m_parseType = ParseType_Upfront;
- HRESULT hr = S_OK;
- SmartFPUControl smartFpuControl;
- uint nextFunctionId = pnodeFnc->sxFnc.functionId + 1;
- this->RestoreContext(parseContext);
- DebugOnly( m_err.fInited = TRUE; )
- m_nextFunctionId = &nextFunctionId;
- m_deferringAST = topLevelDeferred;
- m_inDeferredNestedFunc = false;
- m_scopeCountNoAst = 0;
- SetCurrentStatement(nullptr);
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- pnodeFnc->sxFnc.pnodeParams = nullptr;
- pnodeFnc->sxFnc.pnodeBody = nullptr;
- pnodeFnc->sxFnc.nestedCount = 0;
- m_currentNodeFunc = pnodeFnc;
- m_currentNodeDeferredFunc = nullptr;
- m_ppnodeScope = nullptr;
- m_ppnodeExprScope = nullptr;
- m_pnestedCount = &pnodeFnc->sxFnc.nestedCount;
- m_pCurrentAstSize = &pnodeFnc->sxFnc.astSize;
- ParseNodePtr pnodeBlock = StartParseBlock<true>(PnodeBlockType::Function, ScopeType_FunctionBody);
- pnodeFnc->sxFnc.pnodeScopes = pnodeBlock;
- m_ppnodeScope = &pnodeBlock->sxBlock.pnodeScopes;
- uint uDeferSave = m_grfscr & fscrDeferFncParse;
- try
- {
- m_pscan->Scan();
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeParams;
- this->ParseFncFormals<true>(pnodeFnc, fFncNoFlgs);
- if (m_token.tk == tkRParen)
- {
- m_pscan->Scan();
- }
- ChkCurTok(tkLCurly, ERRnoLcurly);
- m_ppnodeVar = &pnodeFnc->sxFnc.pnodeVars;
- // Put the scanner into "no hashing" mode.
- BYTE deferFlags = m_pscan->SetDeferredParse(topLevelDeferred);
- // Process a sequence of statements/declarations
- if (topLevelDeferred)
- {
- ParseStmtList<false>(nullptr, nullptr, SM_DeferredParse, true);
- }
- else
- {
- ParseNodePtr *lastNodeRef = nullptr;
- ParseStmtList<true>(&pnodeFnc->sxFnc.pnodeBody, &lastNodeRef, SM_OnFunctionCode, true);
- AddArgumentsNodeToVars(pnodeFnc);
- // Append an EndCode node.
- AddToNodeList(&pnodeFnc->sxFnc.pnodeBody, &lastNodeRef, CreateNodeWithScanner<knopEndCode>());
- }
- // Restore the scanner's default hashing mode.
- m_pscan->SetDeferredParseFlags(deferFlags);
- #if DBG
- pnodeFnc->sxFnc.deferredParseNextFunctionId = *this->m_nextFunctionId;
- #endif
- this->m_deferringAST = FALSE;
- // Append block as body of pnodeProg
- FinishParseBlock(pnodeBlock);
- }
- catch(ParseExceptionObject& e)
- {
- m_err.m_hr = e.GetError();
- hr = pse->ProcessError( m_pscan, m_err.m_hr, nullptr);
- }
- if (IsStrictMode())
- {
- pnodeFnc->sxFnc.SetStrictMode();
- }
- if (topLevelDeferred)
- {
- pnodeFnc->sxFnc.pnodeVars = nullptr;
- }
- m_grfscr |= uDeferSave;
- Assert(nullptr == *m_ppnodeScope);
- return hr;
- }
- HRESULT Parser::ParseSourceWithOffset(__out ParseNodePtr* parseTree, LPCUTF8 pSrc, size_t offset, size_t cbLength, charcount_t cchOffset,
- bool isCesu8, ULONG grfscr, CompileScriptException *pse, Js::LocalFunctionId * nextFunctionId, ULONG lineNumber, SourceContextInfo * sourceContextInfo,
- Js::ParseableFunctionInfo* functionInfo)
- {
- m_functionBody = functionInfo;
- if (m_functionBody)
- {
- m_currDeferredStub = m_functionBody->GetDeferredStubs();
- m_InAsmMode = grfscr & fscrNoAsmJs ? false : m_functionBody->GetIsAsmjsMode();
- }
- m_deferAsmJs = !m_InAsmMode;
- m_parseType = ParseType_Deferred;
- return ParseSourceInternal( parseTree, pSrc, offset, cbLength, cchOffset, !isCesu8, grfscr, pse, nextFunctionId, lineNumber, sourceContextInfo);
- }
- bool Parser::IsStrictMode() const
- {
- return (m_fUseStrictMode ||
- (m_currentNodeFunc != nullptr && m_currentNodeFunc->sxFnc.GetStrictMode()));
- }
- BOOL Parser::ExpectingExternalSource()
- {
- return m_fExpectExternalSource;
- }
- Symbol *PnFnc::GetFuncSymbol()
- {
- if (pnodeName &&
- pnodeName->nop == knopVarDecl)
- {
- return pnodeName->sxVar.sym;
- }
- return nullptr;
- }
- void PnFnc::SetFuncSymbol(Symbol *sym)
- {
- Assert(pnodeName &&
- pnodeName->nop == knopVarDecl);
- pnodeName->sxVar.sym = sym;
- }
- ParseNodePtr PnFnc::GetParamScope() const
- {
- if (this->pnodeScopes == nullptr)
- {
- return nullptr;
- }
- Assert(this->pnodeScopes->nop == knopBlock &&
- this->pnodeScopes->sxBlock.pnodeNext == nullptr);
- return this->pnodeScopes->sxBlock.pnodeScopes;
- }
- ParseNodePtr PnFnc::GetBodyScope() const
- {
- if (this->pnodeBodyScope == nullptr)
- {
- return nullptr;
- }
- Assert(this->pnodeBodyScope->nop == knopBlock &&
- this->pnodeBodyScope->sxBlock.pnodeNext == nullptr);
- return this->pnodeBodyScope->sxBlock.pnodeScopes;
- }
- // Create node versions with explicit token limits
- ParseNodePtr Parser::CreateNode(OpCode nop, charcount_t ichMin, charcount_t ichLim)
- {
- Assert(!this->m_deferringAST);
- Assert(nop >= 0 && nop < knopLim);
- ParseNodePtr pnode;
- __analysis_assume(nop < knopLim);
- int cb = nop >= 0 && nop < knopLim ? g_mpnopcbNode[nop] : kcbPnNone;
- pnode = (ParseNodePtr)m_nodeAllocator.Alloc(cb);
- Assert(pnode);
- Assert(m_pCurrentAstSize != NULL);
- *m_pCurrentAstSize += cb;
- InitNode(nop,pnode);
- pnode->ichMin = ichMin;
- pnode->ichLim = ichLim;
- return pnode;
- }
- ParseNodePtr Parser::CreateNameNode(IdentPtr pid,charcount_t ichMin,charcount_t ichLim) {
- ParseNodePtr pnode = CreateNodeT<knopName>(ichMin,ichLim);
- pnode->sxPid.pid = pid;
- pnode->sxPid.sym=NULL;
- pnode->sxPid.symRef=NULL;
- return pnode;
- }
- ParseNodePtr Parser::CreateUniNode(OpCode nop, ParseNodePtr pnode1, charcount_t ichMin,charcount_t ichLim)
- {
- Assert(!this->m_deferringAST);
- DebugOnly(VerifyNodeSize(nop, kcbPnUni));
- ParseNodePtr pnode = (ParseNodePtr)m_nodeAllocator.Alloc(kcbPnUni);
- Assert(m_pCurrentAstSize != NULL);
- *m_pCurrentAstSize += kcbPnUni;
- InitNode(nop, pnode);
- pnode->sxUni.pnode1 = pnode1;
- pnode->ichMin = ichMin;
- pnode->ichLim = ichLim;
- return pnode;
- }
- ParseNodePtr Parser::CreateBinNode(OpCode nop, ParseNodePtr pnode1,
- ParseNodePtr pnode2,charcount_t ichMin,charcount_t ichLim)
- {
- Assert(!this->m_deferringAST);
- ParseNodePtr pnode = StaticCreateBinNode(nop, pnode1, pnode2, &m_nodeAllocator);
- Assert(m_pCurrentAstSize != NULL);
- *m_pCurrentAstSize += kcbPnBin;
- pnode->ichMin = ichMin;
- pnode->ichLim = ichLim;
- return pnode;
- }
- ParseNodePtr Parser::CreateTriNode(OpCode nop, ParseNodePtr pnode1,
- ParseNodePtr pnode2, ParseNodePtr pnode3,
- charcount_t ichMin,charcount_t ichLim)
- {
- Assert(!this->m_deferringAST);
- DebugOnly(VerifyNodeSize(nop, kcbPnTri));
- ParseNodePtr pnode = (ParseNodePtr)m_nodeAllocator.Alloc(kcbPnTri);
- Assert(m_pCurrentAstSize != NULL);
- *m_pCurrentAstSize += kcbPnTri;
- InitNode(nop, pnode);
- pnode->sxTri.pnodeNext = NULL;
- pnode->sxTri.pnode1 = pnode1;
- pnode->sxTri.pnode2 = pnode2;
- pnode->sxTri.pnode3 = pnode3;
- pnode->ichMin = ichMin;
- pnode->ichLim = ichLim;
- return pnode;
- }
- bool PnBlock::HasBlockScopedContent() const
- {
- // A block has its own content if a let, const, or function is declared there.
- if (this->pnodeLexVars != nullptr || this->blockType == Parameter)
- {
- return true;
- }
- // The enclosing scopes can contain functions and other things, so walk the list
- // looking specifically for functions.
- for (ParseNodePtr pnode = this->pnodeScopes; pnode;)
- {
- switch (pnode->nop) {
- case knopFncDecl:
- return true;
- case knopBlock:
- pnode = pnode->sxBlock.pnodeNext;
- break;
- case knopCatch:
- pnode = pnode->sxCatch.pnodeNext;
- break;
- case knopWith:
- pnode = pnode->sxWith.pnodeNext;
- break;
- default:
- Assert(UNREACHED);
- return true;
- }
- }
- return false;
- }
- class ByteCodeGenerator;
- // Copy AST; this works mostly on expressions for now
- ParseNode* Parser::CopyPnode(ParseNode *pnode) {
- if (pnode==NULL)
- return NULL;
- switch (pnode->nop) {
- //PTNODE(knopName , "name" ,None ,Pid ,fnopLeaf)
- case knopName: {
- ParseNode* nameNode=CreateNameNode(pnode->sxPid.pid,pnode->ichMin,pnode->ichLim);
- nameNode->sxPid.sym=pnode->sxPid.sym;
- return nameNode;
- }
- //PTNODE(knopInt , "int const" ,None ,Int ,fnopLeaf|fnopConst)
- case knopInt:
- return pnode;
- //PTNODE(knopFlt , "flt const" ,None ,Flt ,fnopLeaf|fnopConst)
- case knopFlt:
- return pnode;
- //PTNODE(knopStr , "str const" ,None ,Pid ,fnopLeaf|fnopConst)
- case knopStr:
- return pnode;
- //PTNODE(knopRegExp , "reg expr" ,None ,Pid ,fnopLeaf|fnopConst)
- case knopRegExp:
- return pnode;
- break;
- //PTNODE(knopThis , "this" ,None ,None ,fnopLeaf)
- case knopThis:
- return CreateNodeT<knopThis>(pnode->ichMin,pnode->ichLim);
- //PTNODE(knopNull , "null" ,Null ,None ,fnopLeaf)
- case knopNull:
- return pnode;
- //PTNODE(knopFalse , "false" ,False ,None ,fnopLeaf)
- case knopFalse:
- return CreateNodeT<knopFalse>(pnode->ichMin,pnode->ichLim);
- break;
- //PTNODE(knopTrue , "true" ,True ,None ,fnopLeaf)
- case knopTrue:
- return CreateNodeT<knopTrue>(pnode->ichMin,pnode->ichLim);
- //PTNODE(knopEmpty , "empty" ,Empty ,None ,fnopLeaf)
- case knopEmpty:
- return CreateNodeT<knopEmpty>(pnode->ichMin,pnode->ichLim);
- // Unary operators.
- //PTNODE(knopNot , "~" ,BitNot ,Uni ,fnopUni)
- //PTNODE(knopNeg , "unary -" ,Neg ,Uni ,fnopUni)
- //PTNODE(knopPos , "unary +" ,Pos ,Uni ,fnopUni)
- //PTNODE(knopLogNot , "!" ,LogNot ,Uni ,fnopUni)
- //PTNODE(knopEllipsis , "..." ,Spread ,Uni , fnopUni)
- //PTNODE(knopDecPost , "-- post" ,Dec ,Uni ,fnopUni|fnopAsg)
- //PTNODE(knopIncPre , "++ pre" ,Inc ,Uni ,fnopUni|fnopAsg)
- //PTNODE(knopDecPre , "-- pre" ,Dec ,Uni ,fnopUni|fnopAsg)
- //PTNODE(knopTypeof , "typeof" ,None ,Uni ,fnopUni)
- //PTNODE(knopVoid , "void" ,Void ,Uni ,fnopUni)
- //PTNODE(knopDelete , "delete" ,None ,Uni ,fnopUni)
- case knopNot:
- case knopNeg:
- case knopPos:
- case knopLogNot:
- case knopEllipsis:
- case knopIncPost:
- case knopDecPost:
- case knopIncPre:
- case knopDecPre:
- case knopTypeof:
- case knopVoid:
- case knopDelete:
- return CreateUniNode(pnode->nop,CopyPnode(pnode->sxUni.pnode1),pnode->ichMin,pnode->ichLim);
- //PTNODE(knopArray , "arr cnst" ,None ,Uni ,fnopUni)
- //PTNODE(knopObject , "obj cnst" ,None ,Uni ,fnopUni)
- case knopArray:
- case knopObject:
- // TODO: need to copy arr
- Assert(false);
- break;
- // Binary operators
- //PTNODE(knopAdd , "+" ,Add ,Bin ,fnopBin)
- //PTNODE(knopSub , "-" ,Sub ,Bin ,fnopBin)
- //PTNODE(knopMul , "*" ,Mul ,Bin ,fnopBin)
- //PTNODE(knopExpo , "**" ,Expo ,Bin ,fnopBin)
- //PTNODE(knopDiv , "/" ,Div ,Bin ,fnopBin)
- //PTNODE(knopMod , "%" ,Mod ,Bin ,fnopBin)
- //PTNODE(knopOr , "|" ,BitOr ,Bin ,fnopBin)
- //PTNODE(knopXor , "^" ,BitXor ,Bin ,fnopBin)
- //PTNODE(knopAnd , "&" ,BitAnd ,Bin ,fnopBin)
- //PTNODE(knopEq , "==" ,EQ ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopNe , "!=" ,NE ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopLt , "<" ,LT ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopLe , "<=" ,LE ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopGe , ">=" ,GE ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopGt , ">" ,GT ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopEqv , "===" ,Eqv ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopIn , "in" ,In ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopInstOf , "instanceof",InstOf ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopNEqv , "!==" ,NEqv ,Bin ,fnopBin|fnopRel)
- //PTNODE(knopComma , "," ,None ,Bin ,fnopBin)
- //PTNODE(knopLogOr , "||" ,None ,Bin ,fnopBin)
- //PTNODE(knopLogAnd , "&&" ,None ,Bin ,fnopBin)
- //PTNODE(knopLsh , "<<" ,Lsh ,Bin ,fnopBin)
- //PTNODE(knopRsh , ">>" ,Rsh ,Bin ,fnopBin)
- //PTNODE(knopRs2 , ">>>" ,Rs2 ,Bin ,fnopBin)
- case knopAdd:
- case knopSub:
- case knopMul:
- case knopExpo:
- case knopDiv:
- case knopMod:
- case knopOr:
- case knopXor:
- case knopAnd:
- case knopEq:
- case knopNe:
- case knopLt:
- case knopLe:
- case knopGe:
- case knopGt:
- case knopEqv:
- case knopIn:
- case knopInstOf:
- case knopNEqv:
- case knopComma:
- case knopLogOr:
- case knopLogAnd:
- case knopLsh:
- case knopRsh:
- case knopRs2:
- //PTNODE(knopAsg , "=" ,None ,Bin ,fnopBin|fnopAsg)
- case knopAsg:
- //PTNODE(knopDot , "." ,None ,Bin ,fnopBin)
- case knopDot:
- //PTNODE(knopAsgAdd , "+=" ,Add ,Bin ,fnopBin|fnopAsg)
- case knopAsgAdd:
- //PTNODE(knopAsgSub , "-=" ,Sub ,Bin ,fnopBin|fnopAsg)
- case knopAsgSub:
- //PTNODE(knopAsgMul , "*=" ,Mul ,Bin ,fnopBin|fnopAsg)
- case knopAsgMul:
- //PTNODE(knopAsgDiv , "/=" ,Div ,Bin ,fnopBin|fnopAsg)
- case knopAsgExpo:
- //PTNODE(knopAsgExpo , "**=" ,Expo ,Bin ,fnopBin|fnopAsg)
- case knopAsgDiv:
- //PTNODE(knopAsgMod , "%=" ,Mod ,Bin ,fnopBin|fnopAsg)
- case knopAsgMod:
- //PTNODE(knopAsgAnd , "&=" ,BitAnd ,Bin ,fnopBin|fnopAsg)
- case knopAsgAnd:
- //PTNODE(knopAsgXor , "^=" ,BitXor ,Bin ,fnopBin|fnopAsg)
- case knopAsgXor:
- //PTNODE(knopAsgOr , "|=" ,BitOr ,Bin ,fnopBin|fnopAsg)
- case knopAsgOr:
- //PTNODE(knopAsgLsh , "<<=" ,Lsh ,Bin ,fnopBin|fnopAsg)
- case knopAsgLsh:
- //PTNODE(knopAsgRsh , ">>=" ,Rsh ,Bin ,fnopBin|fnopAsg)
- case knopAsgRsh:
- //PTNODE(knopAsgRs2 , ">>>=" ,Rs2 ,Bin ,fnopBin|fnopAsg)
- case knopAsgRs2:
- //PTNODE(knopMember , ":" ,None ,Bin ,fnopBin)
- case knopMember:
- case knopMemberShort:
- //PTNODE(knopIndex , "[]" ,None ,Bin ,fnopBin)
- //PTNODE(knopList , "<list>" ,None ,Bin ,fnopNone)
- case knopIndex:
- case knopList:
- return CreateBinNode(pnode->nop,CopyPnode(pnode->sxBin.pnode1),
- CopyPnode(pnode->sxBin.pnode2),pnode->ichMin,pnode->ichLim);
- //PTNODE(knopCall , "()" ,None ,Bin ,fnopBin)
- //PTNODE(knopNew , "new" ,None ,Bin ,fnopBin)
- case knopNew:
- case knopCall:
- return CreateCallNode(pnode->nop,CopyPnode(pnode->sxBin.pnode1),
- CopyPnode(pnode->sxBin.pnode2),pnode->ichMin,pnode->ichLim);
- //PTNODE(knopQmark , "?" ,None ,Tri ,fnopBin)
- case knopQmark:
- return CreateTriNode(pnode->nop,CopyPnode(pnode->sxTri.pnode1),
- CopyPnode(pnode->sxTri.pnode2),CopyPnode(pnode->sxTri.pnode3),
- pnode->ichMin,pnode->ichLim);
- // General nodes.
- //PTNODE(knopVarDecl , "varDcl" ,None ,Var ,fnopNone)
- case knopVarDecl: {
- ParseNode* copyNode=CreateNodeT<knopVarDecl>(pnode->ichMin,pnode->ichLim);
- copyNode->sxVar.pnodeInit=CopyPnode(pnode->sxVar.pnodeInit);
- copyNode->sxVar.sym=pnode->sxVar.sym;
- // TODO: mult-decl
- Assert(pnode->sxVar.pnodeNext==NULL);
- copyNode->sxVar.pnodeNext=NULL;
- return copyNode;
- }
- //PTNODE(knopFncDecl , "fncDcl" ,None ,Fnc ,fnopLeaf)
- //PTNODE(knopProg , "program" ,None ,Fnc ,fnopNone)
- case knopFncDecl:
- case knopProg:
- Assert(false);
- break;
- //PTNODE(knopEndCode , "<endcode>" ,None ,None ,fnopNone)
- case knopEndCode:
- break;
- //PTNODE(knopDebugger , "debugger" ,None ,None ,fnopNone)
- case knopDebugger:
- break;
- //PTNODE(knopFor , "for" ,None ,For ,fnopBreak|fnopContinue)
- case knopFor: {
- ParseNode* copyNode=CreateNodeT<knopFor>(pnode->ichMin,pnode->ichLim);
- copyNode->sxFor.pnodeInverted=NULL;
- copyNode->sxFor.pnodeInit=CopyPnode(pnode->sxFor.pnodeInit);
- copyNode->sxFor.pnodeCond=CopyPnode(pnode->sxFor.pnodeCond);
- copyNode->sxFor.pnodeIncr=CopyPnode(pnode->sxFor.pnodeIncr);
- copyNode->sxFor.pnodeBody=CopyPnode(pnode->sxFor.pnodeBody);
- return copyNode;
- }
- //PTNODE(knopIf , "if" ,None ,If ,fnopNone)
- case knopIf:
- Assert(false);
- break;
- //PTNODE(knopWhile , "while" ,None ,While,fnopBreak|fnopContinue)
- case knopWhile:
- Assert(false);
- break;
- //PTNODE(knopDoWhile , "do-while" ,None ,While,fnopBreak|fnopContinue)
- case knopDoWhile:
- Assert(false);
- break;
- //PTNODE(knopForIn , "for in" ,None ,ForIn,fnopBreak|fnopContinue|fnopCleanup)
- case knopForIn:
- Assert(false);
- break;
- case knopForOf:
- Assert(false);
- break;
- //PTNODE(knopReturn , "return" ,None ,Uni ,fnopNone)
- case knopReturn: {
- ParseNode* copyNode=CreateNodeT<knopReturn>(pnode->ichMin,pnode->ichLim);
- copyNode->sxReturn.pnodeExpr=CopyPnode(pnode->sxReturn.pnodeExpr);
- return copyNode;
- }
- //PTNODE(knopBlock , "{}" ,None ,Block,fnopNone)
- case knopBlock: {
- ParseNode* copyNode=CreateBlockNode(pnode->ichMin,pnode->ichLim,pnode->sxBlock.blockType);
- if (pnode->grfpn & PNodeFlags::fpnSyntheticNode) {
- // fpnSyntheticNode is sometimes set on PnodeBlockType::Regular blocks which
- // CreateBlockNode() will not automatically set for us, so set it here if it's
- // specified on the source node.
- copyNode->grfpn |= PNodeFlags::fpnSyntheticNode;
- }
- copyNode->sxBlock.pnodeStmt=CopyPnode(pnode->sxBlock.pnodeStmt);
- return copyNode;
- }
- //PTNODE(knopWith , "with" ,None ,With ,fnopCleanup)
- case knopWith:
- Assert(false);
- break;
- //PTNODE(knopBreak , "break" ,None ,Jump ,fnopNone)
- case knopBreak:
- Assert(false);
- break;
- //PTNODE(knopContinue , "continue" ,None ,Jump ,fnopNone)
- case knopContinue:
- Assert(false);
- break;
- //PTNODE(knopLabel , "label" ,None ,Label,fnopNone)
- case knopLabel:
- Assert(false);
- break;
- //PTNODE(knopSwitch , "switch" ,None ,Switch,fnopBreak)
- case knopSwitch:
- Assert(false);
- break;
- //PTNODE(knopCase , "case" ,None ,Case ,fnopNone)
- case knopCase:
- Assert(false);
- break;
- //PTNODE(knopTryFinally,"try-finally",None,TryFinally,fnopCleanup)
- case knopTryFinally:
- Assert(false);
- break;
- case knopFinally:
- Assert(false);
- break;
- //PTNODE(knopCatch , "catch" ,None ,Catch,fnopNone)
- case knopCatch:
- Assert(false);
- break;
- //PTNODE(knopTryCatch , "try-catch" ,None ,TryCatch ,fnopCleanup)
- case knopTryCatch:
- Assert(false);
- break;
- //PTNODE(knopTry , "try" ,None ,Try ,fnopCleanup)
- case knopTry:
- Assert(false);
- break;
- //PTNODE(knopThrow , "throw" ,None ,Uni ,fnopNone)
- case knopThrow:
- Assert(false);
- break;
- default:
- Assert(false);
- break;
- }
- return NULL;
- }
- // Returns true when str is string for Nan, Infinity or -Infinity.
- // Does not check for double number value being in NaN/Infinity range.
- // static
- template<bool CheckForNegativeInfinity>
- inline bool Parser::IsNaNOrInfinityLiteral(LPCOLESTR str)
- {
- // Note: wcscmp crashes when one of the parameters is NULL.
- return str &&
- (wcscmp(_u("NaN"), str) == 0 ||
- wcscmp(_u("Infinity"), str) == 0 ||
- (CheckForNegativeInfinity && wcscmp(_u("-Infinity"), str) == 0));
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseSuper(ParseNodePtr pnode, bool fAllowCall)
- {
- ParseNodePtr currentNodeFunc = GetCurrentFunctionNode();
- if (buildAST) {
- pnode = CreateNodeWithScanner<knopSuper>();
- }
- m_pscan->ScanForcingPid();
- switch (m_token.tk)
- {
- case tkDot: // super.prop
- case tkLBrack: // super[foo]
- case tkLParen: // super(args)
- break;
- default:
- Error(ERRInvalidSuper);
- break;
- }
- if (!fAllowCall && (m_token.tk == tkLParen))
- {
- Error(ERRInvalidSuper); // new super() is not allowed
- }
- else if (this->m_parsingSuperRestrictionState == ParsingSuperRestrictionState_SuperCallAndPropertyAllowed)
- {
- // Any super access is good within a class constructor
- }
- else if (this->m_parsingSuperRestrictionState == ParsingSuperRestrictionState_SuperPropertyAllowed)
- {
- // Cannot call super within a class member
- if (m_token.tk == tkLParen)
- {
- Error(ERRInvalidSuper);
- }
- }
- else
- {
- // Anything else is an error
- Error(ERRInvalidSuper);
- }
- currentNodeFunc->sxFnc.SetHasSuperReference(TRUE);
- CHAKRATEL_LANGSTATS_INC_LANGFEATURECOUNT(SuperCount, m_scriptContext);
- return pnode;
- }
- void Parser::AppendToList(ParseNodePtr *node, ParseNodePtr nodeToAppend)
- {
- Assert(nodeToAppend);
- ParseNodePtr* lastPtr = node;
- while ((*lastPtr) && (*lastPtr)->nop == knopList)
- {
- lastPtr = &(*lastPtr)->sxBin.pnode2;
- }
- auto last = (*lastPtr);
- if (last)
- {
- *lastPtr = CreateBinNode(knopList, last, nodeToAppend, last->ichMin, nodeToAppend->ichLim);
- }
- else
- {
- *lastPtr = nodeToAppend;
- }
- }
- ParseNodePtr Parser::ConvertArrayToArrayPattern(ParseNodePtr pnode)
- {
- Assert(pnode->nop == knopArray);
- pnode->nop = knopArrayPattern;
- ForEachItemRefInList(&pnode->sxArrLit.pnode1, [&](ParseNodePtr *itemRef) {
- ParseNodePtr item = *itemRef;
- if (item->nop == knopEllipsis)
- {
- itemRef = &item->sxUni.pnode1;
- item = *itemRef;
- if (!(item->nop == knopName
- || item->nop == knopDot
- || item->nop == knopIndex
- || item->nop == knopArray
- || item->nop == knopObject))
- {
- Error(ERRInvalidAssignmentTarget);
- }
- }
- else if (item->nop == knopAsg)
- {
- itemRef = &item->sxBin.pnode1;
- item = *itemRef;
- }
- if (item->nop == knopArray)
- {
- ConvertArrayToArrayPattern(item);
- }
- else if (item->nop == knopObject)
- {
- *itemRef = ConvertObjectToObjectPattern(item);
- }
- });
- return pnode;
- }
- ParseNodePtr Parser::CreateParamPatternNode(ParseNodePtr pnode1)
- {
- ParseNodePtr paramPatternNode = CreateNode(knopParamPattern, pnode1->ichMin, pnode1->ichLim);
- paramPatternNode->sxParamPattern.pnode1 = pnode1;
- paramPatternNode->sxParamPattern.pnodeNext = nullptr;
- paramPatternNode->sxParamPattern.location = Js::Constants::NoRegister;
- return paramPatternNode;
- }
- ParseNodePtr Parser::ConvertObjectToObjectPattern(ParseNodePtr pnodeMemberList)
- {
- charcount_t ichMin = m_pscan->IchMinTok();
- charcount_t ichLim = m_pscan->IchLimTok();
- ParseNodePtr pnodeMemberNodeList = nullptr;
- if (pnodeMemberList != nullptr && pnodeMemberList->nop == knopObject)
- {
- ichMin = pnodeMemberList->ichMin;
- ichLim = pnodeMemberList->ichLim;
- pnodeMemberList = pnodeMemberList->sxUni.pnode1;
- }
- ForEachItemInList(pnodeMemberList, [&](ParseNodePtr item) {
- ParseNodePtr memberNode = ConvertMemberToMemberPattern(item);
- AppendToList(&pnodeMemberNodeList, memberNode);
- });
- return CreateUniNode(knopObjectPattern, pnodeMemberNodeList, ichMin, ichLim);
- }
- ParseNodePtr Parser::GetRightSideNodeFromPattern(ParseNodePtr pnode)
- {
- Assert(pnode != nullptr);
- ParseNodePtr rightNode = nullptr;
- OpCode op = pnode->nop;
- if (op == knopObject)
- {
- rightNode = ConvertObjectToObjectPattern(pnode);
- }
- else if (op == knopArray)
- {
- rightNode = ConvertArrayToArrayPattern(pnode);
- }
- else
- {
- rightNode = pnode;
- }
- return rightNode;
- }
- ParseNodePtr Parser::ConvertMemberToMemberPattern(ParseNodePtr pnodeMember)
- {
- if (pnodeMember->nop == knopObjectPatternMember)
- {
- return pnodeMember;
- }
- Assert(pnodeMember->nop == knopMember || pnodeMember->nop == knopMemberShort);
- ParseNodePtr rightNode = GetRightSideNodeFromPattern(pnodeMember->sxBin.pnode2);
- ParseNodePtr resultNode = CreateBinNode(knopObjectPatternMember, pnodeMember->sxBin.pnode1, rightNode);
- resultNode->ichMin = pnodeMember->ichMin;
- resultNode->ichLim = pnodeMember->ichLim;
- return resultNode;
- }
- ParseNodePtr Parser::ConvertToPattern(ParseNodePtr pnode)
- {
- if (pnode != nullptr)
- {
- if (pnode->nop == knopArray)
- {
- ConvertArrayToArrayPattern(pnode);
- }
- else if (pnode->nop == knopObject)
- {
- pnode = ConvertObjectToObjectPattern(pnode);
- }
- }
- return pnode;
- }
- // This essentially be called for verifying the structure of the current tree with satisfying the destructuring grammar.
- void Parser::ParseDestructuredLiteralWithScopeSave(tokens declarationType,
- bool isDecl,
- bool topLevel,
- DestructuringInitializerContext initializerContext/* = DIC_None*/,
- bool allowIn /*= true*/)
- {
- // We are going to parse the text again to validate the current grammar as Destructuring. Saving some scopes and
- // AST related information before the validation parsing and later they will be restored.
- ParseNodePtr pnodeFncSave = m_currentNodeFunc;
- ParseNodePtr pnodeDeferredFncSave = m_currentNodeDeferredFunc;
- if (m_currentNodeDeferredFunc == nullptr)
- {
- m_currentNodeDeferredFunc = m_currentNodeFunc;
- }
- long *pAstSizeSave = m_pCurrentAstSize;
- uint *pNestedCountSave = m_pnestedCount;
- ParseNodePtr *ppnodeScopeSave = m_ppnodeScope;
- ParseNodePtr *ppnodeExprScopeSave = m_ppnodeExprScope;
- ParseNodePtr newTempScope = nullptr;
- m_ppnodeScope = &newTempScope;
- long newTempAstSize = 0;
- m_pCurrentAstSize = &newTempAstSize;
- uint newTempNestedCount = 0;
- m_pnestedCount = &newTempNestedCount;
- m_ppnodeExprScope = nullptr;
- charcount_t funcInArraySave = m_funcInArray;
- uint funcInArrayDepthSave = m_funcInArrayDepth;
- // we need to reset this as we are going to parse the grammar again.
- m_hasDeferredShorthandInitError = false;
- ParseDestructuredLiteral<false>(declarationType, isDecl, topLevel, initializerContext, allowIn);
- m_currentNodeFunc = pnodeFncSave;
- m_currentNodeDeferredFunc = pnodeDeferredFncSave;
- m_pCurrentAstSize = pAstSizeSave;
- m_pnestedCount = pNestedCountSave;
- m_ppnodeScope = ppnodeScopeSave;
- m_ppnodeExprScope = ppnodeExprScopeSave;
- m_funcInArray = funcInArraySave;
- m_funcInArrayDepth = funcInArrayDepthSave;
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseDestructuredLiteral(tokens declarationType,
- bool isDecl,
- bool topLevel/* = true*/,
- DestructuringInitializerContext initializerContext/* = DIC_None*/,
- bool allowIn/* = true*/,
- BOOL *forInOfOkay/* = nullptr*/,
- BOOL *nativeForOkay/* = nullptr*/)
- {
- ParseNodePtr pnode = nullptr;
- Assert(IsPossiblePatternStart());
- if (m_token.tk == tkLCurly)
- {
- pnode = ParseDestructuredObjectLiteral<buildAST>(declarationType, isDecl, topLevel);
- }
- else
- {
- pnode = ParseDestructuredArrayLiteral<buildAST>(declarationType, isDecl, topLevel);
- }
- return ParseDestructuredInitializer<buildAST>(pnode, isDecl, topLevel, initializerContext, allowIn, forInOfOkay, nativeForOkay);
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseDestructuredInitializer(ParseNodePtr lhsNode,
- bool isDecl,
- bool topLevel,
- DestructuringInitializerContext initializerContext,
- bool allowIn,
- BOOL *forInOfOkay,
- BOOL *nativeForOkay)
- {
- m_pscan->Scan();
- if (topLevel && nativeForOkay == nullptr)
- {
- if (initializerContext != DIC_ForceErrorOnInitializer && m_token.tk != tkAsg)
- {
- // e.g. var {x};
- Error(ERRDestructInit);
- }
- else if (initializerContext == DIC_ForceErrorOnInitializer && m_token.tk == tkAsg)
- {
- // e.g. catch([x] = [0])
- Error(ERRDestructNotInit);
- }
- }
- if (m_token.tk != tkAsg || initializerContext == DIC_ShouldNotParseInitializer)
- {
- if (topLevel && nativeForOkay != nullptr)
- {
- // Native loop should have destructuring initializer
- *nativeForOkay = FALSE;
- }
- return lhsNode;
- }
- if (forInOfOkay)
- {
- *forInOfOkay = FALSE;
- }
- m_pscan->Scan();
- bool alreadyHasInitError = m_hasDeferredShorthandInitError;
- ParseNodePtr pnodeDefault = ParseExpr<buildAST>(koplCma, nullptr, allowIn);
- if (m_hasDeferredShorthandInitError && !alreadyHasInitError)
- {
- Error(ERRnoColon);
- }
- ParseNodePtr pnodeDestructAsg = nullptr;
- if (buildAST)
- {
- Assert(lhsNode != nullptr);
- pnodeDestructAsg = CreateNodeWithScanner<knopAsg>();
- pnodeDestructAsg->sxBin.pnode1 = lhsNode;
- pnodeDestructAsg->sxBin.pnode2 = pnodeDefault;
- pnodeDestructAsg->ichMin = lhsNode->ichMin;
- pnodeDestructAsg->ichLim = pnodeDefault->ichLim;
- }
- return pnodeDestructAsg;
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseDestructuredObjectLiteral(tokens declarationType, bool isDecl, bool topLevel/* = true*/)
- {
- Assert(m_token.tk == tkLCurly);
- charcount_t ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- if (!isDecl)
- {
- declarationType = tkLCurly;
- }
- ParseNodePtr pnodeMemberList = ParseMemberList<buildAST>(nullptr/*pNameHint*/, nullptr/*pHintLength*/, declarationType);
- Assert(m_token.tk == tkRCurly);
- ParseNodePtr objectPatternNode = nullptr;
- if (buildAST)
- {
- charcount_t ichLim = m_pscan->IchLimTok();
- objectPatternNode = CreateUniNode(knopObjectPattern, pnodeMemberList, ichMin, ichLim);
- }
- return objectPatternNode;
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseDestructuredVarDecl(tokens declarationType, bool isDecl, bool *hasSeenRest, bool topLevel/* = true*/, bool allowEmptyExpression/* = true*/)
- {
- ParseNodePtr pnodeElem = nullptr;
- int parenCount = 0;
- bool seenRest = false;
- while (m_token.tk == tkLParen)
- {
- m_pscan->Scan();
- ++parenCount;
- }
- if (m_token.tk == tkEllipsis)
- {
- // As per ES 2015 : Rest can have left-hand-side-expression when on assignment expression, but under declaration only binding identifier is allowed
- // But spec is going to change for this one to allow LHS-expression both on expression and declaration - so making that happen early.
- seenRest = true;
- m_pscan->Scan();
- while (m_token.tk == tkLParen)
- {
- m_pscan->Scan();
- ++parenCount;
- }
- if (m_token.tk != tkID && m_token.tk != tkSUPER && m_token.tk != tkLCurly && m_token.tk != tkLBrack)
- {
- if (isDecl)
- {
- Error(ERRnoIdent);
- }
- else
- {
- Error(ERRInvalidAssignmentTarget);
- }
- }
- }
- if (IsPossiblePatternStart())
- {
- // Go recursively
- pnodeElem = ParseDestructuredLiteral<buildAST>(declarationType, isDecl, false /*topLevel*/, seenRest ? DIC_ShouldNotParseInitializer : DIC_None);
- }
- else if (m_token.tk == tkSUPER || m_token.tk == tkID)
- {
- if (isDecl)
- {
- charcount_t ichMin = m_pscan->IchMinTok();
- pnodeElem = ParseVariableDeclaration<buildAST>(declarationType, ichMin
- ,/* fAllowIn */false, /* pfForInOk */nullptr, /* singleDefOnly */true, /* allowInit */!seenRest, false /*topLevelParse*/);
- }
- else
- {
- BOOL fCanAssign;
- IdentToken token;
- // We aren't declaring anything, so scan the ID reference manually.
- pnodeElem = ParseTerm<buildAST>(/* fAllowCall */ m_token.tk != tkSUPER, nullptr /*pNameHint*/, nullptr /*pHintLength*/, nullptr /*pShortNameOffset*/, &token, false,
- &fCanAssign);
- // In this destructuring case we can force error here as we cannot assign.
- if (!fCanAssign)
- {
- Error(ERRInvalidAssignmentTarget);
- }
- if (buildAST)
- {
- if (IsStrictMode() && pnodeElem != nullptr && pnodeElem->nop == knopName)
- {
- CheckStrictModeEvalArgumentsUsage(pnodeElem->sxPid.pid);
- }
- }
- else
- {
- if (IsStrictMode() && token.tk == tkID)
- {
- CheckStrictModeEvalArgumentsUsage(token.pid);
- }
- token.tk = tkNone;
- }
- }
- }
- else if (!((m_token.tk == tkComma || m_token.tk == tkRBrack || m_token.tk == tkRCurly) && allowEmptyExpression))
- {
- if (m_token.IsOperator())
- {
- Error(ERRDestructNoOper);
- }
- Error(ERRDestructIDRef);
- }
- // Swallow RParens before a default expression, if any.
- while (m_token.tk == tkRParen)
- {
- m_pscan->Scan();
- --parenCount;
- }
- if (hasSeenRest != nullptr)
- {
- *hasSeenRest = seenRest;
- }
- if (m_token.tk == tkAsg)
- {
- // Parse the initializer.
- if (seenRest)
- {
- Error(ERRRestWithDefault);
- }
- m_pscan->Scan();
- bool alreadyHasInitError = m_hasDeferredShorthandInitError;
- ParseNodePtr pnodeInit = ParseExpr<buildAST>(koplCma);
- if (m_hasDeferredShorthandInitError && !alreadyHasInitError)
- {
- Error(ERRnoColon);
- }
- if (buildAST)
- {
- pnodeElem = CreateBinNode(knopAsg, pnodeElem, pnodeInit);
- }
- }
- if (buildAST && seenRest)
- {
- ParseNodePtr pnodeRest = CreateNodeWithScanner<knopEllipsis>();
- pnodeRest->sxUni.pnode1 = pnodeElem;
- pnodeElem = pnodeRest;
- }
- while (m_token.tk == tkRParen)
- {
- m_pscan->Scan();
- --parenCount;
- }
- if (!(m_token.tk == tkComma || m_token.tk == tkRBrack || m_token.tk == tkRCurly))
- {
- if (m_token.IsOperator())
- {
- Error(ERRDestructNoOper);
- }
- Error(ERRsyntax);
- }
- if (parenCount != 0)
- {
- Error(ERRnoRparen);
- }
- return pnodeElem;
- }
- template <bool buildAST>
- ParseNodePtr Parser::ParseDestructuredArrayLiteral(tokens declarationType, bool isDecl, bool topLevel)
- {
- Assert(m_token.tk == tkLBrack);
- charcount_t ichMin = m_pscan->IchMinTok();
- m_pscan->Scan();
- ParseNodePtr pnodeDestructArr = nullptr;
- ParseNodePtr pnodeList = nullptr;
- ParseNodePtr *lastNodeRef = nullptr;
- uint count = 0;
- bool hasMissingValues = false;
- bool seenRest = false;
- while (true)
- {
- if (seenRest) // Rest must be in the last position.
- {
- Error(ERRDestructRestLast);
- }
- ParseNodePtr pnodeElem = ParseDestructuredVarDecl<buildAST>(declarationType, isDecl, &seenRest, topLevel);
- if (buildAST)
- {
- if (pnodeElem == nullptr && buildAST)
- {
- pnodeElem = CreateNodeWithScanner<knopEmpty>();
- hasMissingValues = true;
- }
- AddToNodeListEscapedUse(&pnodeList, &lastNodeRef, pnodeElem);
- }
- count++;
- if (m_token.tk == tkRBrack)
- {
- break;
- }
- if (m_token.tk != tkComma)
- {
- Error(ERRDestructNoOper);
- }
- m_pscan->Scan();
- }
- if (buildAST)
- {
- pnodeDestructArr = CreateNodeWithScanner<knopArrayPattern>();
- pnodeDestructArr->sxArrLit.pnode1 = pnodeList;
- pnodeDestructArr->sxArrLit.arrayOfTaggedInts = false;
- pnodeDestructArr->sxArrLit.arrayOfInts = false;
- pnodeDestructArr->sxArrLit.arrayOfNumbers = false;
- pnodeDestructArr->sxArrLit.hasMissingValues = hasMissingValues;
- pnodeDestructArr->sxArrLit.count = count;
- pnodeDestructArr->sxArrLit.spreadCount = seenRest ? 1 : 0;
- pnodeDestructArr->ichMin = ichMin;
- pnodeDestructArr->ichLim = m_pscan->IchLimTok();
- if (pnodeDestructArr->sxArrLit.pnode1)
- {
- this->CheckArguments(pnodeDestructArr->sxArrLit.pnode1);
- }
- }
- return pnodeDestructArr;
- }
- void Parser::CaptureContext(ParseContext *parseContext) const
- {
- parseContext->pszSrc = m_pscan->PchBase();
- parseContext->length = this->m_originalLength;
- parseContext->characterOffset = m_pscan->IchMinTok();
- parseContext->offset = parseContext->characterOffset + m_pscan->m_cMultiUnits;
- parseContext->grfscr = this->m_grfscr;
- parseContext->lineNumber = m_pscan->LineCur();
- parseContext->pnodeProg = this->m_currentNodeProg;
- parseContext->fromExternal = m_pscan->IsFromExternalSource();
- parseContext->strictMode = this->IsStrictMode();
- parseContext->sourceContextInfo = this->m_sourceContextInfo;
- parseContext->currentBlockInfo = this->m_currentBlockInfo;
- parseContext->nextBlockId = this->m_nextBlockId;
- }
- void Parser::RestoreContext(ParseContext *const parseContext)
- {
- m_sourceContextInfo = parseContext->sourceContextInfo;
- m_currentBlockInfo = parseContext->currentBlockInfo;
- m_nextBlockId = parseContext->nextBlockId;
- m_grfscr = parseContext->grfscr;
- m_length = parseContext->length;
- m_pscan->SetText(parseContext->pszSrc, parseContext->offset, parseContext->length, parseContext->characterOffset, parseContext->grfscr, parseContext->lineNumber);
- m_currentNodeProg = parseContext->pnodeProg;
- m_fUseStrictMode = parseContext->strictMode;
- }
- class ByteCodeGenerator;
- #if DBG_DUMP
- #define INDENT_SIZE 2
- void PrintPnodeListWIndent(ParseNode *pnode,int indentAmt);
- void PrintFormalsWIndent(ParseNode *pnode, int indentAmt);
- void Indent(int indentAmt) {
- for (int i=0;i<indentAmt;i++) {
- Output::Print(_u(" "));
- }
- }
- void PrintBlockType(PnodeBlockType type)
- {
- switch (type)
- {
- case Global:
- Output::Print(_u("(Global)"));
- break;
- case Function:
- Output::Print(_u("(Function)"));
- break;
- case Regular:
- Output::Print(_u("(Regular)"));
- break;
- case Parameter:
- Output::Print(_u("(Parameter)"));
- break;
- default:
- Output::Print(_u("(unknown blocktype)"));
- break;
- }
- }
- void PrintScopesWIndent(ParseNode *pnode,int indentAmt) {
- ParseNode *scope = nullptr;
- bool firstOnly = false;
- switch(pnode->nop)
- {
- case knopProg:
- case knopFncDecl: scope = pnode->sxFnc.pnodeScopes; break;
- case knopBlock: scope = pnode->sxBlock.pnodeScopes; break;
- case knopCatch: scope = pnode->sxCatch.pnodeScopes; break;
- case knopWith: scope = pnode->sxWith.pnodeScopes; break;
- case knopSwitch: scope = pnode->sxSwitch.pnodeBlock; firstOnly = true; break;
- case knopFor: scope = pnode->sxFor.pnodeBlock; firstOnly = true; break;
- case knopForIn: scope = pnode->sxForInOrForOf.pnodeBlock; firstOnly = true; break;
- case knopForOf: scope = pnode->sxForInOrForOf.pnodeBlock; firstOnly = true; break;
- }
- if (scope) {
- Output::Print(_u("[%4d, %4d): "), scope->ichMin, scope->ichLim);
- Indent(indentAmt);
- Output::Print(_u("Scopes: "));
- ParseNode *next = nullptr;
- ParseNode *syntheticBlock = nullptr;
- while (scope) {
- switch (scope->nop) {
- case knopFncDecl: Output::Print(_u("knopFncDecl")); next = scope->sxFnc.pnodeNext; break;
- case knopBlock: Output::Print(_u("knopBlock")); PrintBlockType(scope->sxBlock.blockType); next = scope->sxBlock.pnodeNext; break;
- case knopCatch: Output::Print(_u("knopCatch")); next = scope->sxCatch.pnodeNext; break;
- case knopWith: Output::Print(_u("knopWith")); next = scope->sxWith.pnodeNext; break;
- default: Output::Print(_u("unknown")); break;
- }
- if (firstOnly) {
- next = nullptr;
- syntheticBlock = scope;
- }
- if (scope->grfpn & fpnSyntheticNode) {
- Output::Print(_u(" synthetic"));
- if (scope->nop == knopBlock)
- syntheticBlock = scope;
- }
- Output::Print(_u(" (%d-%d)"), scope->ichMin, scope->ichLim);
- if (next) Output::Print(_u(", "));
- scope = next;
- }
- Output::Print(_u("\n"));
- if (syntheticBlock || firstOnly) {
- PrintScopesWIndent(syntheticBlock, indentAmt + INDENT_SIZE);
- }
- }
- }
- void PrintPnodeWIndent(ParseNode *pnode,int indentAmt) {
- if (pnode==NULL)
- return;
- Output::Print(_u("[%4d, %4d): "), pnode->ichMin, pnode->ichLim);
- switch (pnode->nop) {
- //PTNODE(knopName , "name" ,None ,Pid ,fnopLeaf)
- case knopName:
- Indent(indentAmt);
- if (pnode->sxPid.pid!=NULL) {
- Output::Print(_u("id: %s\n"),pnode->sxPid.pid->Psz());
- }
- else {
- Output::Print(_u("name node\n"));
- }
- break;
- //PTNODE(knopInt , "int const" ,None ,Int ,fnopLeaf|fnopConst)
- case knopInt:
- Indent(indentAmt);
- Output::Print(_u("%d\n"),pnode->sxInt.lw);
- break;
- //PTNODE(knopFlt , "flt const" ,None ,Flt ,fnopLeaf|fnopConst)
- case knopFlt:
- Indent(indentAmt);
- Output::Print(_u("%lf\n"),pnode->sxFlt.dbl);
- break;
- //PTNODE(knopStr , "str const" ,None ,Pid ,fnopLeaf|fnopConst)
- case knopStr:
- Indent(indentAmt);
- Output::Print(_u("\"%s\"\n"),pnode->sxPid.pid->Psz());
- break;
- //PTNODE(knopRegExp , "reg expr" ,None ,Pid ,fnopLeaf|fnopConst)
- case knopRegExp:
- Indent(indentAmt);
- Output::Print(_u("/%x/\n"),pnode->sxPid.regexPattern);
- break;
- //PTNODE(knopThis , "this" ,None ,None ,fnopLeaf)
- case knopThis:
- Indent(indentAmt);
- Output::Print(_u("this\n"));
- break;
- //PTNODE(knopSuper , "super" ,None ,None ,fnopLeaf)
- case knopSuper:
- Indent(indentAmt);
- Output::Print(_u("super\n"));
- break;
- //PTNODE(knopNewTarget , "new.target" ,None ,None ,fnopLeaf)
- case knopNewTarget:
- Indent(indentAmt);
- Output::Print(_u("new.target\n"));
- break;
- //PTNODE(knopNull , "null" ,Null ,None ,fnopLeaf)
- case knopNull:
- Indent(indentAmt);
- Output::Print(_u("null\n"));
- break;
- //PTNODE(knopFalse , "false" ,False ,None ,fnopLeaf)
- case knopFalse:
- Indent(indentAmt);
- Output::Print(_u("false\n"));
- break;
- //PTNODE(knopTrue , "true" ,True ,None ,fnopLeaf)
- case knopTrue:
- Indent(indentAmt);
- Output::Print(_u("true\n"));
- break;
- //PTNODE(knopEmpty , "empty" ,Empty ,None ,fnopLeaf)
- case knopEmpty:
- Indent(indentAmt);
- Output::Print(_u("empty\n"));
- break;
- // Unary operators.
- //PTNODE(knopNot , "~" ,BitNot ,Uni ,fnopUni)
- case knopNot:
- Indent(indentAmt);
- Output::Print(_u("~\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopNeg , "unary -" ,Neg ,Uni ,fnopUni)
- case knopNeg:
- Indent(indentAmt);
- Output::Print(_u("U-\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopPos , "unary +" ,Pos ,Uni ,fnopUni)
- case knopPos:
- Indent(indentAmt);
- Output::Print(_u("U+\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopLogNot , "!" ,LogNot ,Uni ,fnopUni)
- case knopLogNot:
- Indent(indentAmt);
- Output::Print(_u("!\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopEllipsis , "..." ,Spread ,Uni , fnopUni)
- case knopEllipsis:
- Indent(indentAmt);
- Output::Print(_u("...<expr>\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopIncPost , "++ post" ,Inc ,Uni ,fnopUni|fnopAsg)
- case knopIncPost:
- Indent(indentAmt);
- Output::Print(_u("<expr>++\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopDecPost , "-- post" ,Dec ,Uni ,fnopUni|fnopAsg)
- case knopDecPost:
- Indent(indentAmt);
- Output::Print(_u("<expr>--\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopIncPre , "++ pre" ,Inc ,Uni ,fnopUni|fnopAsg)
- case knopIncPre:
- Indent(indentAmt);
- Output::Print(_u("++<expr>\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopDecPre , "-- pre" ,Dec ,Uni ,fnopUni|fnopAsg)
- case knopDecPre:
- Indent(indentAmt);
- Output::Print(_u("--<expr>\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopTypeof , "typeof" ,None ,Uni ,fnopUni)
- case knopTypeof:
- Indent(indentAmt);
- Output::Print(_u("typeof\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopVoid , "void" ,Void ,Uni ,fnopUni)
- case knopVoid:
- Indent(indentAmt);
- Output::Print(_u("void\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopDelete , "delete" ,None ,Uni ,fnopUni)
- case knopDelete:
- Indent(indentAmt);
- Output::Print(_u("delete\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopArray , "arr cnst" ,None ,Uni ,fnopUni)
- case knopArrayPattern:
- Indent(indentAmt);
- Output::Print(_u("Array Pattern\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1, indentAmt + INDENT_SIZE);
- break;
- case knopObjectPattern:
- Indent(indentAmt);
- Output::Print(_u("Object Pattern\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1, indentAmt + INDENT_SIZE);
- break;
- case knopArray:
- Indent(indentAmt);
- Output::Print(_u("Array Literal\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopObject , "obj cnst" ,None ,Uni ,fnopUni)
- case knopObject:
- Indent(indentAmt);
- Output::Print(_u("Object Literal\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- // Binary and Ternary Operators
- //PTNODE(knopAdd , "+" ,Add ,Bin ,fnopBin)
- case knopAdd:
- Indent(indentAmt);
- Output::Print(_u("+\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopSub , "-" ,Sub ,Bin ,fnopBin)
- case knopSub:
- Indent(indentAmt);
- Output::Print(_u("-\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopMul , "*" ,Mul ,Bin ,fnopBin)
- case knopMul:
- Indent(indentAmt);
- Output::Print(_u("*\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopDiv , "/" ,Div ,Bin ,fnopBin)
- case knopExpo:
- Indent(indentAmt);
- Output::Print(_u("**\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1, indentAmt + INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2, indentAmt + INDENT_SIZE);
- break;
- //PTNODE(knopExpo , "**" ,Expo ,Bin ,fnopBin)
- case knopDiv:
- Indent(indentAmt);
- Output::Print(_u("/\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopMod , "%" ,Mod ,Bin ,fnopBin)
- case knopMod:
- Indent(indentAmt);
- Output::Print(_u("%\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopOr , "|" ,BitOr ,Bin ,fnopBin)
- case knopOr:
- Indent(indentAmt);
- Output::Print(_u("|\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopXor , "^" ,BitXor ,Bin ,fnopBin)
- case knopXor:
- Indent(indentAmt);
- Output::Print(_u("^\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAnd , "&" ,BitAnd ,Bin ,fnopBin)
- case knopAnd:
- Indent(indentAmt);
- Output::Print(_u("&\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopEq , "==" ,EQ ,Bin ,fnopBin|fnopRel)
- case knopEq:
- Indent(indentAmt);
- Output::Print(_u("==\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopNe , "!=" ,NE ,Bin ,fnopBin|fnopRel)
- case knopNe:
- Indent(indentAmt);
- Output::Print(_u("!=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopLt , "<" ,LT ,Bin ,fnopBin|fnopRel)
- case knopLt:
- Indent(indentAmt);
- Output::Print(_u("<\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopLe , "<=" ,LE ,Bin ,fnopBin|fnopRel)
- case knopLe:
- Indent(indentAmt);
- Output::Print(_u("<=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopGe , ">=" ,GE ,Bin ,fnopBin|fnopRel)
- case knopGe:
- Indent(indentAmt);
- Output::Print(_u(">=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopGt , ">" ,GT ,Bin ,fnopBin|fnopRel)
- case knopGt:
- Indent(indentAmt);
- Output::Print(_u(">\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopCall , "()" ,None ,Bin ,fnopBin)
- case knopCall:
- Indent(indentAmt);
- Output::Print(_u("Call\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeListWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopDot , "." ,None ,Bin ,fnopBin)
- case knopDot:
- Indent(indentAmt);
- Output::Print(_u(".\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsg , "=" ,None ,Bin ,fnopBin|fnopAsg)
- case knopAsg:
- Indent(indentAmt);
- Output::Print(_u("=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopInstOf , "instanceof",InstOf ,Bin ,fnopBin|fnopRel)
- case knopInstOf:
- Indent(indentAmt);
- Output::Print(_u("instanceof\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopIn , "in" ,In ,Bin ,fnopBin|fnopRel)
- case knopIn:
- Indent(indentAmt);
- Output::Print(_u("in\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopEqv , "===" ,Eqv ,Bin ,fnopBin|fnopRel)
- case knopEqv:
- Indent(indentAmt);
- Output::Print(_u("===\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopNEqv , "!==" ,NEqv ,Bin ,fnopBin|fnopRel)
- case knopNEqv:
- Indent(indentAmt);
- Output::Print(_u("!==\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopComma , "," ,None ,Bin ,fnopBin)
- case knopComma:
- Indent(indentAmt);
- Output::Print(_u(",\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopLogOr , "||" ,None ,Bin ,fnopBin)
- case knopLogOr:
- Indent(indentAmt);
- Output::Print(_u("||\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopLogAnd , "&&" ,None ,Bin ,fnopBin)
- case knopLogAnd:
- Indent(indentAmt);
- Output::Print(_u("&&\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopLsh , "<<" ,Lsh ,Bin ,fnopBin)
- case knopLsh:
- Indent(indentAmt);
- Output::Print(_u("<<\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopRsh , ">>" ,Rsh ,Bin ,fnopBin)
- case knopRsh:
- Indent(indentAmt);
- Output::Print(_u(">>\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopRs2 , ">>>" ,Rs2 ,Bin ,fnopBin)
- case knopRs2:
- Indent(indentAmt);
- Output::Print(_u(">>>\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopNew , "new" ,None ,Bin ,fnopBin)
- case knopNew:
- Indent(indentAmt);
- Output::Print(_u("new\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeListWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopIndex , "[]" ,None ,Bin ,fnopBin)
- case knopIndex:
- Indent(indentAmt);
- Output::Print(_u("[]\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeListWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopQmark , "?" ,None ,Tri ,fnopBin)
- case knopQmark:
- Indent(indentAmt);
- Output::Print(_u("?:\n"));
- PrintPnodeWIndent(pnode->sxTri.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxTri.pnode2,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxTri.pnode3,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgAdd , "+=" ,Add ,Bin ,fnopBin|fnopAsg)
- case knopAsgAdd:
- Indent(indentAmt);
- Output::Print(_u("+=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgSub , "-=" ,Sub ,Bin ,fnopBin|fnopAsg)
- case knopAsgSub:
- Indent(indentAmt);
- Output::Print(_u("-=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgMul , "*=" ,Mul ,Bin ,fnopBin|fnopAsg)
- case knopAsgMul:
- Indent(indentAmt);
- Output::Print(_u("*=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgDiv , "/=" ,Div ,Bin ,fnopBin|fnopAsg)
- case knopAsgExpo:
- Indent(indentAmt);
- Output::Print(_u("**=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1, indentAmt + INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2, indentAmt + INDENT_SIZE);
- break;
- //PTNODE(knopAsgExpo , "**=" ,Expo ,Bin ,fnopBin|fnopAsg)
- case knopAsgDiv:
- Indent(indentAmt);
- Output::Print(_u("/=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgMod , "%=" ,Mod ,Bin ,fnopBin|fnopAsg)
- case knopAsgMod:
- Indent(indentAmt);
- Output::Print(_u("%=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgAnd , "&=" ,BitAnd ,Bin ,fnopBin|fnopAsg)
- case knopAsgAnd:
- Indent(indentAmt);
- Output::Print(_u("&=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgXor , "^=" ,BitXor ,Bin ,fnopBin|fnopAsg)
- case knopAsgXor:
- Indent(indentAmt);
- Output::Print(_u("^=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgOr , "|=" ,BitOr ,Bin ,fnopBin|fnopAsg)
- case knopAsgOr:
- Indent(indentAmt);
- Output::Print(_u("|=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgLsh , "<<=" ,Lsh ,Bin ,fnopBin|fnopAsg)
- case knopAsgLsh:
- Indent(indentAmt);
- Output::Print(_u("<<=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgRsh , ">>=" ,Rsh ,Bin ,fnopBin|fnopAsg)
- case knopAsgRsh:
- Indent(indentAmt);
- Output::Print(_u(">>=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopAsgRs2 , ">>>=" ,Rs2 ,Bin ,fnopBin|fnopAsg)
- case knopAsgRs2:
- Indent(indentAmt);
- Output::Print(_u(">>>=\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- case knopComputedName:
- Indent(indentAmt);
- Output::Print(_u("ComputedProperty\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1, indentAmt + INDENT_SIZE);
- break;
- //PTNODE(knopMember , ":" ,None ,Bin ,fnopBin)
- case knopMember:
- case knopMemberShort:
- case knopObjectPatternMember:
- Indent(indentAmt);
- Output::Print(_u(":\n"));
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxBin.pnode2,indentAmt+INDENT_SIZE);
- break;
- // General nodes.
- //PTNODE(knopList , "<list>" ,None ,Bin ,fnopNone)
- case knopList:
- Indent(indentAmt);
- Output::Print(_u("List\n"));
- PrintPnodeListWIndent(pnode,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopVarDecl , "varDcl" ,None ,Var ,fnopNone)
- case knopVarDecl:
- Indent(indentAmt);
- Output::Print(_u("var %s\n"),pnode->sxVar.pid->Psz());
- if (pnode->sxVar.pnodeInit!=NULL)
- PrintPnodeWIndent(pnode->sxVar.pnodeInit,indentAmt+INDENT_SIZE);
- break;
- case knopConstDecl:
- Indent(indentAmt);
- Output::Print(_u("const %s\n"),pnode->sxVar.pid->Psz());
- if (pnode->sxVar.pnodeInit!=NULL)
- PrintPnodeWIndent(pnode->sxVar.pnodeInit,indentAmt+INDENT_SIZE);
- break;
- case knopLetDecl:
- Indent(indentAmt);
- Output::Print(_u("let %s\n"),pnode->sxVar.pid->Psz());
- if (pnode->sxVar.pnodeInit!=NULL)
- PrintPnodeWIndent(pnode->sxVar.pnodeInit,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopFncDecl , "fncDcl" ,None ,Fnc ,fnopLeaf)
- case knopFncDecl:
- Indent(indentAmt);
- if (pnode->sxFnc.pid!=NULL)
- {
- Output::Print(_u("fn decl %d nested %d name %s (%d-%d)\n"),pnode->sxFnc.IsDeclaration(),pnode->sxFnc.IsNested(),
- pnode->sxFnc.pid->Psz(), pnode->ichMin, pnode->ichLim);
- }
- else
- {
- Output::Print(_u("fn decl %d nested %d anonymous (%d-%d)\n"),pnode->sxFnc.IsDeclaration(),pnode->sxFnc.IsNested(),pnode->ichMin,pnode->ichLim);
- }
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintFormalsWIndent(pnode->sxFnc.pnodeParams, indentAmt + INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxFnc.pnodeRest, indentAmt + INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxFnc.pnodeBody, indentAmt + INDENT_SIZE);
- break;
- //PTNODE(knopProg , "program" ,None ,Fnc ,fnopNone)
- case knopProg:
- Indent(indentAmt);
- Output::Print(_u("program\n"));
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintPnodeListWIndent(pnode->sxFnc.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopEndCode , "<endcode>" ,None ,None ,fnopNone)
- case knopEndCode:
- Indent(indentAmt);
- Output::Print(_u("<endcode>\n"));
- break;
- //PTNODE(knopDebugger , "debugger" ,None ,None ,fnopNone)
- case knopDebugger:
- Indent(indentAmt);
- Output::Print(_u("<debugger>\n"));
- break;
- //PTNODE(knopFor , "for" ,None ,For ,fnopBreak|fnopContinue)
- case knopFor:
- Indent(indentAmt);
- Output::Print(_u("for\n"));
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxFor.pnodeInit,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxFor.pnodeCond,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxFor.pnodeIncr,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxFor.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopIf , "if" ,None ,If ,fnopNone)
- case knopIf:
- Indent(indentAmt);
- Output::Print(_u("if\n"));
- PrintPnodeWIndent(pnode->sxIf.pnodeCond,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxIf.pnodeTrue,indentAmt+INDENT_SIZE);
- if (pnode->sxIf.pnodeFalse!=NULL)
- PrintPnodeWIndent(pnode->sxIf.pnodeFalse,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopWhile , "while" ,None ,While,fnopBreak|fnopContinue)
- case knopWhile:
- Indent(indentAmt);
- Output::Print(_u("while\n"));
- PrintPnodeWIndent(pnode->sxWhile.pnodeCond,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxWhile.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopDoWhile , "do-while" ,None ,While,fnopBreak|fnopContinue)
- case knopDoWhile:
- Indent(indentAmt);
- Output::Print(_u("do\n"));
- PrintPnodeWIndent(pnode->sxWhile.pnodeCond,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxWhile.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopForIn , "for in" ,None ,ForIn,fnopBreak|fnopContinue|fnopCleanup)
- case knopForIn:
- Indent(indentAmt);
- Output::Print(_u("forIn\n"));
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxForInOrForOf.pnodeLval,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxForInOrForOf.pnodeObj,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxForInOrForOf.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- case knopForOf:
- Indent(indentAmt);
- Output::Print(_u("forOf\n"));
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxForInOrForOf.pnodeLval,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxForInOrForOf.pnodeObj,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxForInOrForOf.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopReturn , "return" ,None ,Uni ,fnopNone)
- case knopReturn:
- Indent(indentAmt);
- Output::Print(_u("return\n"));
- if (pnode->sxReturn.pnodeExpr!=NULL)
- PrintPnodeWIndent(pnode->sxReturn.pnodeExpr,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopBlock , "{}" ,None ,Block,fnopNone)
- case knopBlock:
- Indent(indentAmt);
- Output::Print(_u("block "));
- if (pnode->grfpn & fpnSyntheticNode)
- Output::Print(_u("synthetic "));
- PrintBlockType(pnode->sxBlock.blockType);
- Output::Print(_u("(%d-%d)\n"),pnode->ichMin,pnode->ichLim);
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- if (pnode->sxBlock.pnodeStmt!=NULL)
- PrintPnodeWIndent(pnode->sxBlock.pnodeStmt,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopWith , "with" ,None ,With ,fnopCleanup)
- case knopWith:
- Indent(indentAmt);
- Output::Print(_u("with (%d-%d)\n"), pnode->ichMin,pnode->ichLim);
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxWith.pnodeObj,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxWith.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopBreak , "break" ,None ,Jump ,fnopNone)
- case knopBreak:
- Indent(indentAmt);
- Output::Print(_u("break\n"));
- // TODO: some representation of target
- break;
- //PTNODE(knopContinue , "continue" ,None ,Jump ,fnopNone)
- case knopContinue:
- Indent(indentAmt);
- Output::Print(_u("continue\n"));
- // TODO: some representation of target
- break;
- //PTNODE(knopLabel , "label" ,None ,Label,fnopNone)
- case knopLabel:
- Indent(indentAmt);
- Output::Print(_u("label %s"),pnode->sxLabel.pid->Psz());
- // TODO: print labeled statement
- break;
- //PTNODE(knopSwitch , "switch" ,None ,Switch,fnopBreak)
- case knopSwitch:
- Indent(indentAmt);
- Output::Print(_u("switch\n"));
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- for (ParseNode *pnodeT = pnode->sxSwitch.pnodeCases; NULL != pnodeT;pnodeT = pnodeT->sxCase.pnodeNext) {
- PrintPnodeWIndent(pnodeT,indentAmt+2);
- }
- break;
- //PTNODE(knopCase , "case" ,None ,Case ,fnopNone)
- case knopCase:
- Indent(indentAmt);
- Output::Print(_u("case\n"));
- PrintPnodeWIndent(pnode->sxCase.pnodeExpr,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxCase.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopTryFinally,"try-finally",None,TryFinally,fnopCleanup)
- case knopTryFinally:
- PrintPnodeWIndent(pnode->sxTryFinally.pnodeTry,indentAmt);
- PrintPnodeWIndent(pnode->sxTryFinally.pnodeFinally,indentAmt);
- break;
- case knopFinally:
- Indent(indentAmt);
- Output::Print(_u("finally\n"));
- PrintPnodeWIndent(pnode->sxFinally.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopCatch , "catch" ,None ,Catch,fnopNone)
- case knopCatch:
- Indent(indentAmt);
- Output::Print(_u("catch (%d-%d)\n"), pnode->ichMin,pnode->ichLim);
- PrintScopesWIndent(pnode, indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxCatch.pnodeParam,indentAmt+INDENT_SIZE);
- // if (pnode->sxCatch.pnodeGuard!=NULL)
- // PrintPnodeWIndent(pnode->sxCatch.pnodeGuard,indentAmt+INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxCatch.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopTryCatch , "try-catch" ,None ,TryCatch ,fnopCleanup)
- case knopTryCatch:
- PrintPnodeWIndent(pnode->sxTryCatch.pnodeTry,indentAmt);
- PrintPnodeWIndent(pnode->sxTryCatch.pnodeCatch,indentAmt);
- break;
- //PTNODE(knopTry , "try" ,None ,Try ,fnopCleanup)
- case knopTry:
- Indent(indentAmt);
- Output::Print(_u("try\n"));
- PrintPnodeWIndent(pnode->sxTry.pnodeBody,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopThrow , "throw" ,None ,Uni ,fnopNone)
- case knopThrow:
- Indent(indentAmt);
- Output::Print(_u("throw\n"));
- PrintPnodeWIndent(pnode->sxUni.pnode1,indentAmt+INDENT_SIZE);
- break;
- //PTNODE(knopClassDecl, "classDecl", None , Class, fnopLeaf)
- case knopClassDecl:
- Indent(indentAmt);
- Output::Print(_u("class %s"), pnode->sxClass.pnodeName->sxVar.pid->Psz());
- if (pnode->sxClass.pnodeExtends != nullptr)
- {
- Output::Print(_u(" extends "));
- PrintPnodeWIndent(pnode->sxClass.pnodeExtends, 0);
- }
- else {
- Output::Print(_u("\n"));
- }
- PrintPnodeWIndent(pnode->sxClass.pnodeConstructor, indentAmt + INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxClass.pnodeMembers, indentAmt + INDENT_SIZE);
- PrintPnodeWIndent(pnode->sxClass.pnodeStaticMembers, indentAmt + INDENT_SIZE);
- break;
- case knopStrTemplate:
- Indent(indentAmt);
- Output::Print(_u("string template\n"));
- PrintPnodeListWIndent(pnode->sxStrTemplate.pnodeSubstitutionExpressions, indentAmt + INDENT_SIZE);
- break;
- case knopYieldStar:
- Indent(indentAmt);
- Output::Print(_u("yield*\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1, indentAmt + INDENT_SIZE);
- break;
- case knopYield:
- case knopYieldLeaf:
- Indent(indentAmt);
- Output::Print(_u("yield\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1, indentAmt + INDENT_SIZE);
- break;
- case knopAwait:
- Indent(indentAmt);
- Output::Print(_u("await\n"));
- PrintPnodeListWIndent(pnode->sxUni.pnode1, indentAmt + INDENT_SIZE);
- break;
- case knopExportDefault:
- Indent(indentAmt);
- Output::Print(_u("export default\n"));
- PrintPnodeListWIndent(pnode->sxExportDefault.pnodeExpr, indentAmt + INDENT_SIZE);
- break;
- default:
- Output::Print(_u("unhandled pnode op %d\n"),pnode->nop);
- break;
- }
- }
- void PrintPnodeListWIndent(ParseNode *pnode,int indentAmt) {
- if (pnode!=NULL) {
- while(pnode->nop==knopList) {
- PrintPnodeWIndent(pnode->sxBin.pnode1,indentAmt);
- pnode = pnode->sxBin.pnode2;
- }
- PrintPnodeWIndent(pnode,indentAmt);
- }
- }
- void PrintFormalsWIndent(ParseNode *pnodeArgs, int indentAmt)
- {
- for (ParseNode *pnode = pnodeArgs; pnode != nullptr; pnode = pnode->GetFormalNext())
- {
- PrintPnodeWIndent(pnode->nop == knopParamPattern ? pnode->sxParamPattern.pnode1 : pnode, indentAmt);
- }
- }
- void PrintPnode(ParseNode *pnode) {
- PrintPnodeWIndent(pnode,0);
- }
- void ParseNode::Dump()
- {
- switch(nop)
- {
- case knopFncDecl:
- case knopProg:
- LPCOLESTR name = Js::Constants::AnonymousFunction;
- if(this->sxFnc.pnodeName)
- {
- name = this->sxFnc.pnodeName->sxVar.pid->Psz();
- }
- Output::Print(_u("%s (%d) [%d, %d]:\n"), name, this->sxFnc.functionId, this->sxFnc.lineNumber, this->sxFnc.columnNumber);
- Output::Print(_u("hasArguments: %s callsEval:%s childCallsEval:%s HasReferenceableBuiltInArguments:%s ArgumentsObjectEscapes:%s HasWith:%s HasThis:%s HasOnlyThis:%s \n"),
- IsTrueOrFalse(this->sxFnc.HasHeapArguments()),
- IsTrueOrFalse(this->sxFnc.CallsEval()),
- IsTrueOrFalse(this->sxFnc.ChildCallsEval()),
- IsTrueOrFalse(this->sxFnc.HasReferenceableBuiltInArguments()),
- IsTrueOrFalse(this->sxFnc.GetArgumentsObjectEscapes()),
- IsTrueOrFalse(this->sxFnc.HasWithStmt()),
- IsTrueOrFalse(this->sxFnc.HasThisStmt()),
- IsTrueOrFalse(this->sxFnc.HasOnlyThisStmts()));
- if(this->sxFnc.funcInfo)
- {
- this->sxFnc.funcInfo->Dump();
- }
- break;
- }
- }
- #endif
- DeferredFunctionStub * BuildDeferredStubTree(ParseNode *pnodeFnc, Recycler *recycler)
- {
- Assert(pnodeFnc->nop == knopFncDecl);
- uint nestedCount = pnodeFnc->sxFnc.nestedCount;
- if (nestedCount == 0)
- {
- return nullptr;
- }
- if (pnodeFnc->sxFnc.deferredStub)
- {
- return pnodeFnc->sxFnc.deferredStub;
- }
- DeferredFunctionStub *deferredStubs = RecyclerNewArray(recycler, DeferredFunctionStub, nestedCount);
- uint i = 0;
- ParseNode *pnodeBlock = pnodeFnc->sxFnc.pnodeBodyScope;
- Assert(pnodeBlock != nullptr
- && pnodeBlock->nop == knopBlock
- && (pnodeBlock->sxBlock.blockType == PnodeBlockType::Function
- || pnodeBlock->sxBlock.blockType == PnodeBlockType::Parameter));
- for (ParseNode *pnodeChild = pnodeBlock->sxBlock.pnodeScopes; pnodeChild != nullptr;)
- {
- if (pnodeChild->nop != knopFncDecl)
- {
- // We only expect to find a function body block in a parameter scope block.
- Assert(pnodeChild->nop == knopBlock
- && (pnodeBlock->sxBlock.blockType == PnodeBlockType::Parameter
- || pnodeChild->sxBlock.blockType == PnodeBlockType::Function));
- pnodeChild = pnodeChild->sxBlock.pnodeNext;
- continue;
- }
- Assert(i < nestedCount);
- if (pnodeChild->sxFnc.IsGeneratedDefault())
- {
- ++i;
- pnodeChild = pnodeChild->sxFnc.pnodeNext;
- continue;
- }
- __analysis_assume(i < nestedCount);
- deferredStubs[i].fncFlags = pnodeChild->sxFnc.fncFlags;
- deferredStubs[i].nestedCount = pnodeChild->sxFnc.nestedCount;
- deferredStubs[i].restorePoint = *pnodeChild->sxFnc.pRestorePoint;
- deferredStubs[i].deferredStubs = BuildDeferredStubTree(pnodeChild, recycler);
- deferredStubs[i].ichMin = pnodeChild->ichMin;
- ++i;
- pnodeChild = pnodeChild->sxFnc.pnodeNext;
- }
- return deferredStubs;
- }
|