zend_compile.c 311 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Nikita Popov <nikic@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <zend_language_parser.h>
  21. #include "zend.h"
  22. #include "zend_attributes.h"
  23. #include "zend_compile.h"
  24. #include "zend_constants.h"
  25. #include "zend_llist.h"
  26. #include "zend_API.h"
  27. #include "zend_exceptions.h"
  28. #include "zend_interfaces.h"
  29. #include "zend_virtual_cwd.h"
  30. #include "zend_multibyte.h"
  31. #include "zend_language_scanner.h"
  32. #include "zend_inheritance.h"
  33. #include "zend_vm.h"
  34. #include "zend_enum.h"
  35. #define SET_NODE(target, src) do { \
  36. target ## _type = (src)->op_type; \
  37. if ((src)->op_type == IS_CONST) { \
  38. target.constant = zend_add_literal(&(src)->u.constant); \
  39. } else { \
  40. target = (src)->u.op; \
  41. } \
  42. } while (0)
  43. #define GET_NODE(target, src) do { \
  44. (target)->op_type = src ## _type; \
  45. if ((target)->op_type == IS_CONST) { \
  46. ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
  47. } else { \
  48. (target)->u.op = src; \
  49. } \
  50. } while (0)
  51. #define FC(member) (CG(file_context).member)
  52. typedef struct _zend_loop_var {
  53. zend_uchar opcode;
  54. zend_uchar var_type;
  55. uint32_t var_num;
  56. uint32_t try_catch_offset;
  57. } zend_loop_var;
  58. static inline uint32_t zend_alloc_cache_slots(unsigned count) {
  59. if (count == 0) {
  60. /* Even if no cache slots are desired, the VM handler may still want to acquire
  61. * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
  62. * calculation is still legal and ubsan does not complain. */
  63. return 0;
  64. }
  65. zend_op_array *op_array = CG(active_op_array);
  66. uint32_t ret = op_array->cache_size;
  67. op_array->cache_size += count * sizeof(void*);
  68. return ret;
  69. }
  70. static inline uint32_t zend_alloc_cache_slot(void) {
  71. return zend_alloc_cache_slots(1);
  72. }
  73. ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
  74. ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
  75. #ifndef ZTS
  76. ZEND_API zend_compiler_globals compiler_globals;
  77. ZEND_API zend_executor_globals executor_globals;
  78. #endif
  79. static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2);
  80. static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
  81. static void zend_eval_const_expr(zend_ast **ast_ptr);
  82. static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
  83. static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
  84. static void zend_compile_expr(znode *result, zend_ast *ast);
  85. static void zend_compile_stmt(zend_ast *ast);
  86. static void zend_compile_assign(znode *result, zend_ast *ast);
  87. static void init_op(zend_op *op)
  88. {
  89. MAKE_NOP(op);
  90. op->extended_value = 0;
  91. op->lineno = CG(zend_lineno);
  92. }
  93. static zend_always_inline uint32_t get_next_op_number(void)
  94. {
  95. return CG(active_op_array)->last;
  96. }
  97. static zend_op *get_next_op(void)
  98. {
  99. zend_op_array *op_array = CG(active_op_array);
  100. uint32_t next_op_num = op_array->last++;
  101. zend_op *next_op;
  102. if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
  103. CG(context).opcodes_size *= 4;
  104. op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
  105. }
  106. next_op = &(op_array->opcodes[next_op_num]);
  107. init_op(next_op);
  108. return next_op;
  109. }
  110. static zend_brk_cont_element *get_next_brk_cont_element(void)
  111. {
  112. CG(context).last_brk_cont++;
  113. CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
  114. return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
  115. }
  116. static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
  117. {
  118. zend_string *filename = CG(active_op_array)->filename;
  119. zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
  120. '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
  121. return zend_new_interned_string(result);
  122. }
  123. /* }}} */
  124. static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
  125. {
  126. const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  127. if (ns_separator != NULL) {
  128. *result = ns_separator + 1;
  129. *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. /* }}} */
  135. struct reserved_class_name {
  136. const char *name;
  137. size_t len;
  138. };
  139. static const struct reserved_class_name reserved_class_names[] = {
  140. {ZEND_STRL("bool")},
  141. {ZEND_STRL("false")},
  142. {ZEND_STRL("float")},
  143. {ZEND_STRL("int")},
  144. {ZEND_STRL("null")},
  145. {ZEND_STRL("parent")},
  146. {ZEND_STRL("self")},
  147. {ZEND_STRL("static")},
  148. {ZEND_STRL("string")},
  149. {ZEND_STRL("true")},
  150. {ZEND_STRL("void")},
  151. {ZEND_STRL("never")},
  152. {ZEND_STRL("iterable")},
  153. {ZEND_STRL("object")},
  154. {ZEND_STRL("mixed")},
  155. {NULL, 0}
  156. };
  157. static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
  158. {
  159. const struct reserved_class_name *reserved = reserved_class_names;
  160. const char *uqname = ZSTR_VAL(name);
  161. size_t uqname_len = ZSTR_LEN(name);
  162. zend_get_unqualified_name(name, &uqname, &uqname_len);
  163. for (; reserved->name; ++reserved) {
  164. if (uqname_len == reserved->len
  165. && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
  166. ) {
  167. return 1;
  168. }
  169. }
  170. return 0;
  171. }
  172. /* }}} */
  173. void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
  174. {
  175. if (zend_is_reserved_class_name(name)) {
  176. zend_error_noreturn(E_COMPILE_ERROR,
  177. "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
  178. }
  179. }
  180. /* }}} */
  181. typedef struct _builtin_type_info {
  182. const char* name;
  183. const size_t name_len;
  184. const zend_uchar type;
  185. } builtin_type_info;
  186. static const builtin_type_info builtin_types[] = {
  187. {ZEND_STRL("null"), IS_NULL},
  188. {ZEND_STRL("false"), IS_FALSE},
  189. {ZEND_STRL("int"), IS_LONG},
  190. {ZEND_STRL("float"), IS_DOUBLE},
  191. {ZEND_STRL("string"), IS_STRING},
  192. {ZEND_STRL("bool"), _IS_BOOL},
  193. {ZEND_STRL("void"), IS_VOID},
  194. {ZEND_STRL("never"), IS_NEVER},
  195. {ZEND_STRL("iterable"), IS_ITERABLE},
  196. {ZEND_STRL("object"), IS_OBJECT},
  197. {ZEND_STRL("mixed"), IS_MIXED},
  198. {NULL, 0, IS_UNDEF}
  199. };
  200. typedef struct {
  201. const char *name;
  202. size_t name_len;
  203. const char *correct_name;
  204. } confusable_type_info;
  205. static const confusable_type_info confusable_types[] = {
  206. {ZEND_STRL("boolean"), "bool"},
  207. {ZEND_STRL("integer"), "int"},
  208. {ZEND_STRL("double"), "float"},
  209. {ZEND_STRL("resource"), NULL},
  210. {NULL, 0, NULL},
  211. };
  212. static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
  213. {
  214. const builtin_type_info *info = &builtin_types[0];
  215. for (; info->name; ++info) {
  216. if (ZSTR_LEN(name) == info->name_len
  217. && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
  218. ) {
  219. return info->type;
  220. }
  221. }
  222. return 0;
  223. }
  224. /* }}} */
  225. static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
  226. {
  227. const confusable_type_info *info = confusable_types;
  228. /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
  229. * as a scalar type, while "Integer" is likely a class type. */
  230. for (; info->name; ++info) {
  231. if (ZSTR_LEN(name) == info->name_len
  232. && memcmp(ZSTR_VAL(name), info->name, info->name_len) == 0
  233. ) {
  234. *correct_name = info->correct_name;
  235. return 1;
  236. }
  237. }
  238. return 0;
  239. }
  240. /* }}} */
  241. static bool zend_is_not_imported(zend_string *name) {
  242. /* Assuming "name" is unqualified here. */
  243. return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
  244. }
  245. void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
  246. {
  247. *prev_context = CG(context);
  248. CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
  249. CG(context).vars_size = 0;
  250. CG(context).literals_size = 0;
  251. CG(context).fast_call_var = -1;
  252. CG(context).try_catch_offset = -1;
  253. CG(context).current_brk_cont = -1;
  254. CG(context).last_brk_cont = 0;
  255. CG(context).brk_cont_array = NULL;
  256. CG(context).labels = NULL;
  257. }
  258. /* }}} */
  259. void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
  260. {
  261. if (CG(context).brk_cont_array) {
  262. efree(CG(context).brk_cont_array);
  263. CG(context).brk_cont_array = NULL;
  264. }
  265. if (CG(context).labels) {
  266. zend_hash_destroy(CG(context).labels);
  267. FREE_HASHTABLE(CG(context).labels);
  268. CG(context).labels = NULL;
  269. }
  270. CG(context) = *prev_context;
  271. }
  272. /* }}} */
  273. static void zend_reset_import_tables(void) /* {{{ */
  274. {
  275. if (FC(imports)) {
  276. zend_hash_destroy(FC(imports));
  277. efree(FC(imports));
  278. FC(imports) = NULL;
  279. }
  280. if (FC(imports_function)) {
  281. zend_hash_destroy(FC(imports_function));
  282. efree(FC(imports_function));
  283. FC(imports_function) = NULL;
  284. }
  285. if (FC(imports_const)) {
  286. zend_hash_destroy(FC(imports_const));
  287. efree(FC(imports_const));
  288. FC(imports_const) = NULL;
  289. }
  290. }
  291. /* }}} */
  292. static void zend_end_namespace(void) /* {{{ */ {
  293. FC(in_namespace) = 0;
  294. zend_reset_import_tables();
  295. if (FC(current_namespace)) {
  296. zend_string_release_ex(FC(current_namespace), 0);
  297. FC(current_namespace) = NULL;
  298. }
  299. }
  300. /* }}} */
  301. void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
  302. {
  303. *prev_context = CG(file_context);
  304. FC(imports) = NULL;
  305. FC(imports_function) = NULL;
  306. FC(imports_const) = NULL;
  307. FC(current_namespace) = NULL;
  308. FC(in_namespace) = 0;
  309. FC(has_bracketed_namespaces) = 0;
  310. FC(declarables).ticks = 0;
  311. zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
  312. }
  313. /* }}} */
  314. void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
  315. {
  316. zend_end_namespace();
  317. zend_hash_destroy(&FC(seen_symbols));
  318. CG(file_context) = *prev_context;
  319. }
  320. /* }}} */
  321. void zend_init_compiler_data_structures(void) /* {{{ */
  322. {
  323. zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
  324. zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
  325. zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
  326. CG(active_class_entry) = NULL;
  327. CG(in_compilation) = 0;
  328. CG(skip_shebang) = 0;
  329. CG(encoding_declared) = 0;
  330. CG(memoized_exprs) = NULL;
  331. CG(memoize_mode) = 0;
  332. }
  333. /* }}} */
  334. static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
  335. zval *zv = zend_hash_find(&FC(seen_symbols), name);
  336. if (zv) {
  337. Z_LVAL_P(zv) |= kind;
  338. } else {
  339. zval tmp;
  340. ZVAL_LONG(&tmp, kind);
  341. zend_hash_add_new(&FC(seen_symbols), name, &tmp);
  342. }
  343. }
  344. static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
  345. zval *zv = zend_hash_find(&FC(seen_symbols), name);
  346. return zv && (Z_LVAL_P(zv) & kind) != 0;
  347. }
  348. void init_compiler(void) /* {{{ */
  349. {
  350. CG(arena) = zend_arena_create(64 * 1024);
  351. CG(active_op_array) = NULL;
  352. memset(&CG(context), 0, sizeof(CG(context)));
  353. zend_init_compiler_data_structures();
  354. zend_init_rsrc_list();
  355. zend_stream_init();
  356. CG(unclean_shutdown) = 0;
  357. CG(delayed_variance_obligations) = NULL;
  358. CG(delayed_autoloads) = NULL;
  359. CG(unlinked_uses) = NULL;
  360. CG(current_linking_class) = NULL;
  361. }
  362. /* }}} */
  363. void shutdown_compiler(void) /* {{{ */
  364. {
  365. /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
  366. zend_restore_compiled_filename(NULL);
  367. zend_stack_destroy(&CG(loop_var_stack));
  368. zend_stack_destroy(&CG(delayed_oplines_stack));
  369. zend_stack_destroy(&CG(short_circuiting_opnums));
  370. if (CG(delayed_variance_obligations)) {
  371. zend_hash_destroy(CG(delayed_variance_obligations));
  372. FREE_HASHTABLE(CG(delayed_variance_obligations));
  373. CG(delayed_variance_obligations) = NULL;
  374. }
  375. if (CG(delayed_autoloads)) {
  376. zend_hash_destroy(CG(delayed_autoloads));
  377. FREE_HASHTABLE(CG(delayed_autoloads));
  378. CG(delayed_autoloads) = NULL;
  379. }
  380. if (CG(unlinked_uses)) {
  381. zend_hash_destroy(CG(unlinked_uses));
  382. FREE_HASHTABLE(CG(unlinked_uses));
  383. CG(unlinked_uses) = NULL;
  384. }
  385. CG(current_linking_class) = NULL;
  386. }
  387. /* }}} */
  388. ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
  389. {
  390. CG(compiled_filename) = zend_string_copy(new_compiled_filename);
  391. return new_compiled_filename;
  392. }
  393. /* }}} */
  394. ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
  395. {
  396. if (CG(compiled_filename)) {
  397. zend_string_release(CG(compiled_filename));
  398. CG(compiled_filename) = NULL;
  399. }
  400. CG(compiled_filename) = original_compiled_filename;
  401. }
  402. /* }}} */
  403. ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
  404. {
  405. return CG(compiled_filename);
  406. }
  407. /* }}} */
  408. ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
  409. {
  410. return CG(zend_lineno);
  411. }
  412. /* }}} */
  413. ZEND_API bool zend_is_compiling(void) /* {{{ */
  414. {
  415. return CG(in_compilation);
  416. }
  417. /* }}} */
  418. static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
  419. {
  420. return (uint32_t)CG(active_op_array)->T++;
  421. }
  422. /* }}} */
  423. static int lookup_cv(zend_string *name) /* {{{ */{
  424. zend_op_array *op_array = CG(active_op_array);
  425. int i = 0;
  426. zend_ulong hash_value = zend_string_hash_val(name);
  427. while (i < op_array->last_var) {
  428. if (ZSTR_H(op_array->vars[i]) == hash_value
  429. && zend_string_equals(op_array->vars[i], name)) {
  430. return EX_NUM_TO_VAR(i);
  431. }
  432. i++;
  433. }
  434. i = op_array->last_var;
  435. op_array->last_var++;
  436. if (op_array->last_var > CG(context).vars_size) {
  437. CG(context).vars_size += 16; /* FIXME */
  438. op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
  439. }
  440. op_array->vars[i] = zend_string_copy(name);
  441. return EX_NUM_TO_VAR(i);
  442. }
  443. /* }}} */
  444. zend_string *zval_make_interned_string(zval *zv)
  445. {
  446. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  447. Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
  448. if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
  449. Z_TYPE_FLAGS_P(zv) = 0;
  450. }
  451. return Z_STR_P(zv);
  452. }
  453. /* Common part of zend_add_literal and zend_append_individual_literal */
  454. static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
  455. {
  456. zval *lit = CT_CONSTANT_EX(op_array, literal_position);
  457. if (Z_TYPE_P(zv) == IS_STRING) {
  458. zval_make_interned_string(zv);
  459. }
  460. ZVAL_COPY_VALUE(lit, zv);
  461. Z_EXTRA_P(lit) = 0;
  462. }
  463. /* }}} */
  464. /* Is used while compiling a function, using the context to keep track
  465. of an approximate size to avoid to relocate to often.
  466. Literals are truncated to actual size in the second compiler pass (pass_two()). */
  467. static int zend_add_literal(zval *zv) /* {{{ */
  468. {
  469. zend_op_array *op_array = CG(active_op_array);
  470. int i = op_array->last_literal;
  471. op_array->last_literal++;
  472. if (i >= CG(context).literals_size) {
  473. while (i >= CG(context).literals_size) {
  474. CG(context).literals_size += 16; /* FIXME */
  475. }
  476. op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
  477. }
  478. zend_insert_literal(op_array, zv, i);
  479. return i;
  480. }
  481. /* }}} */
  482. static inline int zend_add_literal_string(zend_string **str) /* {{{ */
  483. {
  484. int ret;
  485. zval zv;
  486. ZVAL_STR(&zv, *str);
  487. ret = zend_add_literal(&zv);
  488. *str = Z_STR(zv);
  489. return ret;
  490. }
  491. /* }}} */
  492. static int zend_add_func_name_literal(zend_string *name) /* {{{ */
  493. {
  494. /* Original name */
  495. int ret = zend_add_literal_string(&name);
  496. /* Lowercased name */
  497. zend_string *lc_name = zend_string_tolower(name);
  498. zend_add_literal_string(&lc_name);
  499. return ret;
  500. }
  501. /* }}} */
  502. static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
  503. {
  504. const char *unqualified_name;
  505. size_t unqualified_name_len;
  506. /* Original name */
  507. int ret = zend_add_literal_string(&name);
  508. /* Lowercased name */
  509. zend_string *lc_name = zend_string_tolower(name);
  510. zend_add_literal_string(&lc_name);
  511. /* Lowercased unqualified name */
  512. if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
  513. lc_name = zend_string_alloc(unqualified_name_len, 0);
  514. zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
  515. zend_add_literal_string(&lc_name);
  516. }
  517. return ret;
  518. }
  519. /* }}} */
  520. static int zend_add_class_name_literal(zend_string *name) /* {{{ */
  521. {
  522. /* Original name */
  523. int ret = zend_add_literal_string(&name);
  524. /* Lowercased name */
  525. zend_string *lc_name = zend_string_tolower(name);
  526. zend_add_literal_string(&lc_name);
  527. return ret;
  528. }
  529. /* }}} */
  530. static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
  531. {
  532. zend_string *tmp_name;
  533. int ret = zend_add_literal_string(&name);
  534. size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
  535. const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  536. if (after_ns) {
  537. after_ns += 1;
  538. ns_len = after_ns - ZSTR_VAL(name) - 1;
  539. after_ns_len = ZSTR_LEN(name) - ns_len - 1;
  540. /* lowercased namespace name & original constant name */
  541. tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
  542. zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
  543. zend_add_literal_string(&tmp_name);
  544. if (!unqualified) {
  545. return ret;
  546. }
  547. } else {
  548. after_ns = ZSTR_VAL(name);
  549. }
  550. /* original unqualified constant name */
  551. tmp_name = zend_string_init(after_ns, after_ns_len, 0);
  552. zend_add_literal_string(&tmp_name);
  553. return ret;
  554. }
  555. /* }}} */
  556. #define LITERAL_STR(op, str) do { \
  557. zval _c; \
  558. ZVAL_STR(&_c, str); \
  559. op.constant = zend_add_literal(&_c); \
  560. } while (0)
  561. void zend_stop_lexing(void)
  562. {
  563. if (LANG_SCNG(on_event)) {
  564. LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
  565. }
  566. LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
  567. }
  568. static inline void zend_begin_loop(
  569. zend_uchar free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
  570. {
  571. zend_brk_cont_element *brk_cont_element;
  572. int parent = CG(context).current_brk_cont;
  573. zend_loop_var info = {0};
  574. CG(context).current_brk_cont = CG(context).last_brk_cont;
  575. brk_cont_element = get_next_brk_cont_element();
  576. brk_cont_element->parent = parent;
  577. brk_cont_element->is_switch = is_switch;
  578. if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
  579. uint32_t start = get_next_op_number();
  580. info.opcode = free_opcode;
  581. info.var_type = loop_var->op_type;
  582. info.var_num = loop_var->u.op.var;
  583. brk_cont_element->start = start;
  584. } else {
  585. info.opcode = ZEND_NOP;
  586. /* The start field is used to free temporary variables in case of exceptions.
  587. * We won't try to free something of we don't have loop variable. */
  588. brk_cont_element->start = -1;
  589. }
  590. zend_stack_push(&CG(loop_var_stack), &info);
  591. }
  592. /* }}} */
  593. static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
  594. {
  595. uint32_t end = get_next_op_number();
  596. zend_brk_cont_element *brk_cont_element
  597. = &CG(context).brk_cont_array[CG(context).current_brk_cont];
  598. brk_cont_element->cont = cont_addr;
  599. brk_cont_element->brk = end;
  600. CG(context).current_brk_cont = brk_cont_element->parent;
  601. zend_stack_del_top(&CG(loop_var_stack));
  602. }
  603. /* }}} */
  604. static void zend_do_free(znode *op1) /* {{{ */
  605. {
  606. if (op1->op_type == IS_TMP_VAR) {
  607. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  608. while (opline->opcode == ZEND_END_SILENCE ||
  609. opline->opcode == ZEND_OP_DATA) {
  610. opline--;
  611. }
  612. if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
  613. switch (opline->opcode) {
  614. case ZEND_BOOL:
  615. case ZEND_BOOL_NOT:
  616. /* boolean results don't have to be freed */
  617. return;
  618. case ZEND_POST_INC_STATIC_PROP:
  619. case ZEND_POST_DEC_STATIC_PROP:
  620. case ZEND_POST_INC_OBJ:
  621. case ZEND_POST_DEC_OBJ:
  622. case ZEND_POST_INC:
  623. case ZEND_POST_DEC:
  624. /* convert $i++ to ++$i */
  625. opline->opcode -= 2;
  626. opline->result_type = IS_UNUSED;
  627. return;
  628. case ZEND_ASSIGN:
  629. case ZEND_ASSIGN_DIM:
  630. case ZEND_ASSIGN_OBJ:
  631. case ZEND_ASSIGN_STATIC_PROP:
  632. case ZEND_ASSIGN_OP:
  633. case ZEND_ASSIGN_DIM_OP:
  634. case ZEND_ASSIGN_OBJ_OP:
  635. case ZEND_ASSIGN_STATIC_PROP_OP:
  636. case ZEND_PRE_INC_STATIC_PROP:
  637. case ZEND_PRE_DEC_STATIC_PROP:
  638. case ZEND_PRE_INC_OBJ:
  639. case ZEND_PRE_DEC_OBJ:
  640. case ZEND_PRE_INC:
  641. case ZEND_PRE_DEC:
  642. opline->result_type = IS_UNUSED;
  643. return;
  644. }
  645. }
  646. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  647. } else if (op1->op_type == IS_VAR) {
  648. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  649. while (opline->opcode == ZEND_END_SILENCE ||
  650. opline->opcode == ZEND_EXT_FCALL_END ||
  651. opline->opcode == ZEND_OP_DATA) {
  652. opline--;
  653. }
  654. if (opline->result_type == IS_VAR
  655. && opline->result.var == op1->u.op.var) {
  656. if (opline->opcode == ZEND_FETCH_THIS) {
  657. opline->opcode = ZEND_NOP;
  658. opline->result_type = IS_UNUSED;
  659. } else {
  660. opline->result_type = IS_UNUSED;
  661. }
  662. } else {
  663. while (opline >= CG(active_op_array)->opcodes) {
  664. if ((opline->opcode == ZEND_FETCH_LIST_R ||
  665. opline->opcode == ZEND_FETCH_LIST_W) &&
  666. opline->op1_type == IS_VAR &&
  667. opline->op1.var == op1->u.op.var) {
  668. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  669. return;
  670. }
  671. if (opline->result_type == IS_VAR
  672. && opline->result.var == op1->u.op.var) {
  673. if (opline->opcode == ZEND_NEW) {
  674. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  675. }
  676. break;
  677. }
  678. opline--;
  679. }
  680. }
  681. } else if (op1->op_type == IS_CONST) {
  682. /* Destroy value without using GC: When opcache moves arrays into SHM it will
  683. * free the zend_array structure, so references to it from outside the op array
  684. * become invalid. GC would cause such a reference in the root buffer. */
  685. zval_ptr_dtor_nogc(&op1->u.constant);
  686. }
  687. }
  688. /* }}} */
  689. uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
  690. {
  691. uint32_t new_flags = flags | new_flag;
  692. if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  693. zend_throw_exception(zend_ce_compile_error,
  694. "Multiple abstract modifiers are not allowed", 0);
  695. return 0;
  696. }
  697. if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
  698. zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
  699. return 0;
  700. }
  701. if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
  702. zend_throw_exception(zend_ce_compile_error,
  703. "Cannot use the final modifier on an abstract class", 0);
  704. return 0;
  705. }
  706. return new_flags;
  707. }
  708. /* }}} */
  709. uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
  710. {
  711. uint32_t new_flags = flags | new_flag;
  712. if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
  713. zend_throw_exception(zend_ce_compile_error,
  714. "Multiple access type modifiers are not allowed", 0);
  715. return 0;
  716. }
  717. if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
  718. zend_throw_exception(zend_ce_compile_error, "Multiple abstract modifiers are not allowed", 0);
  719. return 0;
  720. }
  721. if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
  722. zend_throw_exception(zend_ce_compile_error, "Multiple static modifiers are not allowed", 0);
  723. return 0;
  724. }
  725. if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
  726. zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
  727. return 0;
  728. }
  729. if ((flags & ZEND_ACC_READONLY) && (new_flag & ZEND_ACC_READONLY)) {
  730. zend_throw_exception(zend_ce_compile_error,
  731. "Multiple readonly modifiers are not allowed", 0);
  732. return 0;
  733. }
  734. if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
  735. zend_throw_exception(zend_ce_compile_error,
  736. "Cannot use the final modifier on an abstract class member", 0);
  737. return 0;
  738. }
  739. return new_flags;
  740. }
  741. /* }}} */
  742. ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name) {
  743. return zend_string_concat3(
  744. ZSTR_VAL(class_name), ZSTR_LEN(class_name),
  745. "::", sizeof("::") - 1,
  746. ZSTR_VAL(member_name), ZSTR_LEN(member_name));
  747. }
  748. static zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
  749. return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
  750. }
  751. static zend_string *zend_prefix_with_ns(zend_string *name) {
  752. if (FC(current_namespace)) {
  753. zend_string *ns = FC(current_namespace);
  754. return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
  755. } else {
  756. return zend_string_copy(name);
  757. }
  758. }
  759. static zend_string *zend_resolve_non_class_name(
  760. zend_string *name, uint32_t type, bool *is_fully_qualified,
  761. bool case_sensitive, HashTable *current_import_sub
  762. ) {
  763. char *compound;
  764. *is_fully_qualified = 0;
  765. if (ZSTR_VAL(name)[0] == '\\') {
  766. /* Remove \ prefix (only relevant if this is a string rather than a label) */
  767. *is_fully_qualified = 1;
  768. return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
  769. }
  770. if (type == ZEND_NAME_FQ) {
  771. *is_fully_qualified = 1;
  772. return zend_string_copy(name);
  773. }
  774. if (type == ZEND_NAME_RELATIVE) {
  775. *is_fully_qualified = 1;
  776. return zend_prefix_with_ns(name);
  777. }
  778. if (current_import_sub) {
  779. /* If an unqualified name is a function/const alias, replace it. */
  780. zend_string *import_name;
  781. if (case_sensitive) {
  782. import_name = zend_hash_find_ptr(current_import_sub, name);
  783. } else {
  784. import_name = zend_hash_find_ptr_lc(current_import_sub, name);
  785. }
  786. if (import_name) {
  787. *is_fully_qualified = 1;
  788. return zend_string_copy(import_name);
  789. }
  790. }
  791. compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  792. if (compound) {
  793. *is_fully_qualified = 1;
  794. }
  795. if (compound && FC(imports)) {
  796. /* If the first part of a qualified name is an alias, substitute it. */
  797. size_t len = compound - ZSTR_VAL(name);
  798. zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
  799. if (import_name) {
  800. return zend_concat_names(
  801. ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
  802. }
  803. }
  804. return zend_prefix_with_ns(name);
  805. }
  806. /* }}} */
  807. static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
  808. {
  809. return zend_resolve_non_class_name(
  810. name, type, is_fully_qualified, 0, FC(imports_function));
  811. }
  812. static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
  813. {
  814. return zend_resolve_non_class_name(
  815. name, type, is_fully_qualified, 1, FC(imports_const));
  816. }
  817. static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
  818. {
  819. char *compound;
  820. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
  821. if (type == ZEND_NAME_FQ) {
  822. zend_error_noreturn(E_COMPILE_ERROR,
  823. "'\\%s' is an invalid class name", ZSTR_VAL(name));
  824. }
  825. if (type == ZEND_NAME_RELATIVE) {
  826. zend_error_noreturn(E_COMPILE_ERROR,
  827. "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
  828. }
  829. ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
  830. return zend_string_copy(name);
  831. }
  832. if (type == ZEND_NAME_RELATIVE) {
  833. return zend_prefix_with_ns(name);
  834. }
  835. if (type == ZEND_NAME_FQ) {
  836. if (ZSTR_VAL(name)[0] == '\\') {
  837. /* Remove \ prefix (only relevant if this is a string rather than a label) */
  838. name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
  839. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
  840. zend_error_noreturn(E_COMPILE_ERROR,
  841. "'\\%s' is an invalid class name", ZSTR_VAL(name));
  842. }
  843. return name;
  844. }
  845. return zend_string_copy(name);
  846. }
  847. if (FC(imports)) {
  848. compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  849. if (compound) {
  850. /* If the first part of a qualified name is an alias, substitute it. */
  851. size_t len = compound - ZSTR_VAL(name);
  852. zend_string *import_name =
  853. zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
  854. if (import_name) {
  855. return zend_concat_names(
  856. ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
  857. }
  858. } else {
  859. /* If an unqualified name is an alias, replace it. */
  860. zend_string *import_name
  861. = zend_hash_find_ptr_lc(FC(imports), name);
  862. if (import_name) {
  863. return zend_string_copy(import_name);
  864. }
  865. }
  866. }
  867. /* If not fully qualified and not an alias, prepend the current namespace */
  868. return zend_prefix_with_ns(name);
  869. }
  870. /* }}} */
  871. zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
  872. {
  873. zval *class_name = zend_ast_get_zval(ast);
  874. if (Z_TYPE_P(class_name) != IS_STRING) {
  875. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  876. }
  877. return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
  878. }
  879. /* }}} */
  880. static void label_ptr_dtor(zval *zv) /* {{{ */
  881. {
  882. efree_size(Z_PTR_P(zv), sizeof(zend_label));
  883. }
  884. /* }}} */
  885. static void str_dtor(zval *zv) /* {{{ */ {
  886. zend_string_release_ex(Z_STR_P(zv), 0);
  887. }
  888. /* }}} */
  889. static bool zend_is_call(zend_ast *ast);
  890. static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
  891. {
  892. zend_op_array *op_array = CG(active_op_array);
  893. uint32_t try_catch_offset = op_array->last_try_catch++;
  894. zend_try_catch_element *elem;
  895. op_array->try_catch_array = safe_erealloc(
  896. op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
  897. elem = &op_array->try_catch_array[try_catch_offset];
  898. elem->try_op = try_op;
  899. elem->catch_op = 0;
  900. elem->finally_op = 0;
  901. elem->finally_end = 0;
  902. return try_catch_offset;
  903. }
  904. /* }}} */
  905. void zend_init_static_variables_map_ptr(zend_op_array *op_array)
  906. {
  907. if (op_array->static_variables) {
  908. ZEND_MAP_PTR_INIT(op_array->static_variables_ptr,
  909. zend_arena_alloc(&CG(arena), sizeof(HashTable *)));
  910. ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
  911. }
  912. }
  913. ZEND_API void function_add_ref(zend_function *function) /* {{{ */
  914. {
  915. if (function->type == ZEND_USER_FUNCTION) {
  916. zend_op_array *op_array = &function->op_array;
  917. if (op_array->refcount) {
  918. (*op_array->refcount)++;
  919. }
  920. ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void *)));
  921. ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
  922. zend_init_static_variables_map_ptr(op_array);
  923. }
  924. if (function->common.function_name) {
  925. zend_string_addref(function->common.function_name);
  926. }
  927. }
  928. /* }}} */
  929. static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(zend_string *lcname, zend_op_array *op_array, bool compile_time) /* {{{ */
  930. {
  931. zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
  932. int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
  933. zend_function *old_function;
  934. ZEND_ASSERT(zv != NULL);
  935. old_function = (zend_function*)Z_PTR_P(zv);
  936. if (old_function->type == ZEND_USER_FUNCTION
  937. && old_function->op_array.last > 0) {
  938. zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
  939. op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
  940. ZSTR_VAL(old_function->op_array.filename),
  941. old_function->op_array.opcodes[0].lineno);
  942. } else {
  943. zend_error_noreturn(error_level, "Cannot redeclare %s()",
  944. op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
  945. }
  946. }
  947. ZEND_API zend_result do_bind_function(zend_function *func, zval *lcname) /* {{{ */
  948. {
  949. zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
  950. if (UNEXPECTED(!added_func)) {
  951. do_bind_function_error(Z_STR_P(lcname), &func->op_array, 0);
  952. return FAILURE;
  953. }
  954. if (func->op_array.refcount) {
  955. ++*func->op_array.refcount;
  956. }
  957. if (func->common.function_name) {
  958. zend_string_addref(func->common.function_name);
  959. }
  960. return SUCCESS;
  961. }
  962. /* }}} */
  963. ZEND_API zend_class_entry *zend_bind_class_in_slot(
  964. zval *class_table_slot, zval *lcname, zend_string *lc_parent_name)
  965. {
  966. zend_class_entry *ce = Z_PTR_P(class_table_slot);
  967. bool is_preloaded =
  968. (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
  969. bool success;
  970. if (EXPECTED(!is_preloaded)) {
  971. success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
  972. } else {
  973. /* If preloading is used, don't replace the existing bucket, add a new one. */
  974. success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
  975. }
  976. if (UNEXPECTED(!success)) {
  977. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
  978. return NULL;
  979. }
  980. if (ce->ce_flags & ZEND_ACC_LINKED) {
  981. return ce;
  982. }
  983. ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
  984. if (ce) {
  985. ZEND_ASSERT(!EG(exception));
  986. return ce;
  987. }
  988. if (!is_preloaded) {
  989. /* Reload bucket pointer, the hash table may have been reallocated */
  990. zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
  991. zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
  992. } else {
  993. zend_hash_del(EG(class_table), Z_STR_P(lcname));
  994. }
  995. return NULL;
  996. }
  997. ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
  998. {
  999. zend_class_entry *ce;
  1000. zval *rtd_key, *zv;
  1001. rtd_key = lcname + 1;
  1002. zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
  1003. if (UNEXPECTED(!zv)) {
  1004. ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
  1005. ZEND_ASSERT(ce);
  1006. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
  1007. return FAILURE;
  1008. }
  1009. /* Register the derived class */
  1010. return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
  1011. }
  1012. /* }}} */
  1013. static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
  1014. zend_string *result;
  1015. if (type == NULL) {
  1016. return zend_string_copy(new_type);
  1017. }
  1018. if (is_intersection) {
  1019. result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
  1020. "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
  1021. zend_string_release(type);
  1022. } else {
  1023. result = zend_string_concat3(
  1024. ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
  1025. zend_string_release(type);
  1026. }
  1027. return result;
  1028. }
  1029. static zend_string *resolve_class_name(zend_string *name, zend_class_entry *scope) {
  1030. if (scope) {
  1031. if (zend_string_equals_literal_ci(name, "self")) {
  1032. name = scope->name;
  1033. } else if (zend_string_equals_literal_ci(name, "parent") && scope->parent) {
  1034. name = scope->parent->name;
  1035. }
  1036. }
  1037. /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
  1038. * null byte here, to avoid larger parts of the type being omitted by printing code later. */
  1039. size_t len = strlen(ZSTR_VAL(name));
  1040. if (len != ZSTR_LEN(name)) {
  1041. ZEND_ASSERT(scope && "This should only happen with resolved types");
  1042. return zend_string_init(ZSTR_VAL(name), len, 0);
  1043. }
  1044. return zend_string_copy(name);
  1045. }
  1046. zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope) {
  1047. zend_string *str = NULL;
  1048. if (ZEND_TYPE_HAS_LIST(type)) {
  1049. zend_type *list_type;
  1050. bool is_intersection = ZEND_TYPE_IS_INTERSECTION(type);
  1051. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
  1052. zend_string *name = ZEND_TYPE_NAME(*list_type);
  1053. zend_string *resolved = resolve_class_name(name, scope);
  1054. str = add_type_string(str, resolved, is_intersection);
  1055. zend_string_release(resolved);
  1056. } ZEND_TYPE_LIST_FOREACH_END();
  1057. } else if (ZEND_TYPE_HAS_NAME(type)) {
  1058. str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
  1059. }
  1060. uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
  1061. if (type_mask == MAY_BE_ANY) {
  1062. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
  1063. return str;
  1064. }
  1065. if (type_mask & MAY_BE_STATIC) {
  1066. zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
  1067. // During compilation of eval'd code the called scope refers to the scope calling the eval
  1068. if (scope && !zend_is_compiling()) {
  1069. zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
  1070. if (called_scope) {
  1071. name = called_scope->name;
  1072. }
  1073. }
  1074. str = add_type_string(str, name, /* is_intersection */ false);
  1075. }
  1076. if (type_mask & MAY_BE_CALLABLE) {
  1077. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
  1078. }
  1079. if (type_mask & MAY_BE_ITERABLE) {
  1080. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ITERABLE), /* is_intersection */ false);
  1081. }
  1082. if (type_mask & MAY_BE_OBJECT) {
  1083. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
  1084. }
  1085. if (type_mask & MAY_BE_ARRAY) {
  1086. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
  1087. }
  1088. if (type_mask & MAY_BE_STRING) {
  1089. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
  1090. }
  1091. if (type_mask & MAY_BE_LONG) {
  1092. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
  1093. }
  1094. if (type_mask & MAY_BE_DOUBLE) {
  1095. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
  1096. }
  1097. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
  1098. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
  1099. } else if (type_mask & MAY_BE_FALSE) {
  1100. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
  1101. }
  1102. if (type_mask & MAY_BE_VOID) {
  1103. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
  1104. }
  1105. if (type_mask & MAY_BE_NEVER) {
  1106. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
  1107. }
  1108. if (type_mask & MAY_BE_NULL) {
  1109. bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
  1110. if (!is_union) {
  1111. zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
  1112. zend_string_release(str);
  1113. return nullable_str;
  1114. }
  1115. str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
  1116. }
  1117. return str;
  1118. }
  1119. ZEND_API zend_string *zend_type_to_string(zend_type type) {
  1120. return zend_type_to_string_resolved(type, NULL);
  1121. }
  1122. static bool is_generator_compatible_class_type(zend_string *name) {
  1123. return zend_string_equals_literal_ci(name, "Traversable")
  1124. || zend_string_equals_literal_ci(name, "Iterator")
  1125. || zend_string_equals_literal_ci(name, "Generator");
  1126. }
  1127. static void zend_mark_function_as_generator(void) /* {{{ */
  1128. {
  1129. if (!CG(active_op_array)->function_name) {
  1130. zend_error_noreturn(E_COMPILE_ERROR,
  1131. "The \"yield\" expression can only be used inside a function");
  1132. }
  1133. if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  1134. zend_type return_type = CG(active_op_array)->arg_info[-1].type;
  1135. bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & (MAY_BE_ITERABLE | MAY_BE_OBJECT)) != 0;
  1136. if (!valid_type) {
  1137. zend_type *single_type;
  1138. ZEND_TYPE_FOREACH(return_type, single_type) {
  1139. if (ZEND_TYPE_HAS_NAME(*single_type)
  1140. && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
  1141. valid_type = 1;
  1142. break;
  1143. }
  1144. } ZEND_TYPE_FOREACH_END();
  1145. }
  1146. if (!valid_type) {
  1147. zend_string *str = zend_type_to_string(return_type);
  1148. zend_error_noreturn(E_COMPILE_ERROR,
  1149. "Generator return type must be a supertype of Generator, %s given",
  1150. ZSTR_VAL(str));
  1151. }
  1152. }
  1153. CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
  1154. }
  1155. /* }}} */
  1156. ZEND_API uint32_t zend_build_delayed_early_binding_list(const zend_op_array *op_array) /* {{{ */
  1157. {
  1158. if (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) {
  1159. uint32_t first_early_binding_opline = (uint32_t)-1;
  1160. uint32_t *prev_opline_num = &first_early_binding_opline;
  1161. zend_op *opline = op_array->opcodes;
  1162. zend_op *end = opline + op_array->last;
  1163. while (opline < end) {
  1164. if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
  1165. *prev_opline_num = opline - op_array->opcodes;
  1166. prev_opline_num = &opline->result.opline_num;
  1167. }
  1168. ++opline;
  1169. }
  1170. *prev_opline_num = -1;
  1171. return first_early_binding_opline;
  1172. }
  1173. return (uint32_t)-1;
  1174. }
  1175. /* }}} */
  1176. ZEND_API void zend_do_delayed_early_binding(zend_op_array *op_array, uint32_t first_early_binding_opline) /* {{{ */
  1177. {
  1178. if (first_early_binding_opline != (uint32_t)-1) {
  1179. bool orig_in_compilation = CG(in_compilation);
  1180. uint32_t opline_num = first_early_binding_opline;
  1181. void **run_time_cache;
  1182. if (!ZEND_MAP_PTR(op_array->run_time_cache)) {
  1183. void *ptr;
  1184. ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
  1185. ptr = emalloc(op_array->cache_size + sizeof(void*));
  1186. ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
  1187. ptr = (char*)ptr + sizeof(void*);
  1188. ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
  1189. memset(ptr, 0, op_array->cache_size);
  1190. }
  1191. run_time_cache = RUN_TIME_CACHE(op_array);
  1192. CG(in_compilation) = 1;
  1193. while (opline_num != (uint32_t)-1) {
  1194. const zend_op *opline = &op_array->opcodes[opline_num];
  1195. zval *lcname = RT_CONSTANT(opline, opline->op1);
  1196. zval *zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(lcname + 1));
  1197. if (zv) {
  1198. zend_class_entry *ce = Z_CE_P(zv);
  1199. zend_string *lc_parent_name = Z_STR_P(RT_CONSTANT(opline, opline->op2));
  1200. zend_class_entry *parent_ce = zend_hash_find_ex_ptr(EG(class_table), lc_parent_name, 1);
  1201. if (parent_ce) {
  1202. ce = zend_try_early_bind(ce, parent_ce, Z_STR_P(lcname), zv);
  1203. if (ce) {
  1204. /* Store in run-time cache */
  1205. ((void**)((char*)run_time_cache + opline->extended_value))[0] = ce;
  1206. }
  1207. }
  1208. }
  1209. opline_num = op_array->opcodes[opline_num].result.opline_num;
  1210. }
  1211. CG(in_compilation) = orig_in_compilation;
  1212. }
  1213. }
  1214. /* }}} */
  1215. ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
  1216. {
  1217. size_t prop_name_length = 1 + src1_length + 1 + src2_length;
  1218. zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
  1219. ZSTR_VAL(prop_name)[0] = '\0';
  1220. memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
  1221. memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
  1222. return prop_name;
  1223. }
  1224. /* }}} */
  1225. static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
  1226. {
  1227. size_t len = 0;
  1228. while (*s++ && maxlen--) len++;
  1229. return len;
  1230. }
  1231. /* }}} */
  1232. ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
  1233. {
  1234. size_t class_name_len;
  1235. size_t anonclass_src_len;
  1236. *class_name = NULL;
  1237. if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
  1238. *prop_name = ZSTR_VAL(name);
  1239. if (prop_len) {
  1240. *prop_len = ZSTR_LEN(name);
  1241. }
  1242. return SUCCESS;
  1243. }
  1244. if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
  1245. zend_error(E_NOTICE, "Illegal member variable name");
  1246. *prop_name = ZSTR_VAL(name);
  1247. if (prop_len) {
  1248. *prop_len = ZSTR_LEN(name);
  1249. }
  1250. return FAILURE;
  1251. }
  1252. class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
  1253. if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
  1254. zend_error(E_NOTICE, "Corrupt member variable name");
  1255. *prop_name = ZSTR_VAL(name);
  1256. if (prop_len) {
  1257. *prop_len = ZSTR_LEN(name);
  1258. }
  1259. return FAILURE;
  1260. }
  1261. *class_name = ZSTR_VAL(name) + 1;
  1262. anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
  1263. if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
  1264. class_name_len += anonclass_src_len + 1;
  1265. }
  1266. *prop_name = ZSTR_VAL(name) + class_name_len + 2;
  1267. if (prop_len) {
  1268. *prop_len = ZSTR_LEN(name) - class_name_len - 2;
  1269. }
  1270. return SUCCESS;
  1271. }
  1272. /* }}} */
  1273. static bool can_ct_eval_const(zend_constant *c) {
  1274. if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
  1275. return 0;
  1276. }
  1277. if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
  1278. && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
  1279. && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
  1280. && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
  1281. return 1;
  1282. }
  1283. if (Z_TYPE(c->value) < IS_OBJECT
  1284. && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
  1285. return 1;
  1286. }
  1287. return 0;
  1288. }
  1289. static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
  1290. {
  1291. /* Substitute true, false and null (including unqualified usage in namespaces)
  1292. * before looking up the possibly namespaced name. */
  1293. const char *lookup_name = ZSTR_VAL(name);
  1294. size_t lookup_len = ZSTR_LEN(name);
  1295. if (!is_fully_qualified) {
  1296. zend_get_unqualified_name(name, &lookup_name, &lookup_len);
  1297. }
  1298. zend_constant *c;
  1299. if ((c = zend_get_special_const(lookup_name, lookup_len))) {
  1300. ZVAL_COPY_VALUE(zv, &c->value);
  1301. return 1;
  1302. }
  1303. c = zend_hash_find_ptr(EG(zend_constants), name);
  1304. if (c && can_ct_eval_const(c)) {
  1305. ZVAL_COPY_OR_DUP(zv, &c->value);
  1306. return 1;
  1307. }
  1308. return 0;
  1309. }
  1310. /* }}} */
  1311. static inline bool zend_is_scope_known(void) /* {{{ */
  1312. {
  1313. if (!CG(active_op_array)) {
  1314. /* This can only happen when evaluating a default value string. */
  1315. return 0;
  1316. }
  1317. if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
  1318. /* Closures can be rebound to a different scope */
  1319. return 0;
  1320. }
  1321. if (!CG(active_class_entry)) {
  1322. /* The scope is known if we're in a free function (no scope), but not if we're in
  1323. * a file/eval (which inherits including/eval'ing scope). */
  1324. return CG(active_op_array)->function_name != NULL;
  1325. }
  1326. /* For traits self etc refers to the using class, not the trait itself */
  1327. return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
  1328. }
  1329. /* }}} */
  1330. static inline bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
  1331. {
  1332. if (!CG(active_class_entry)) {
  1333. return 0;
  1334. }
  1335. if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
  1336. return 1;
  1337. }
  1338. return fetch_type == ZEND_FETCH_CLASS_DEFAULT
  1339. && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
  1340. }
  1341. /* }}} */
  1342. uint32_t zend_get_class_fetch_type(zend_string *name) /* {{{ */
  1343. {
  1344. if (zend_string_equals_literal_ci(name, "self")) {
  1345. return ZEND_FETCH_CLASS_SELF;
  1346. } else if (zend_string_equals_literal_ci(name, "parent")) {
  1347. return ZEND_FETCH_CLASS_PARENT;
  1348. } else if (zend_string_equals_literal_ci(name, "static")) {
  1349. return ZEND_FETCH_CLASS_STATIC;
  1350. } else {
  1351. return ZEND_FETCH_CLASS_DEFAULT;
  1352. }
  1353. }
  1354. /* }}} */
  1355. static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
  1356. {
  1357. /* Fully qualified names are always default refs */
  1358. if (name_ast->attr == ZEND_NAME_FQ) {
  1359. return ZEND_FETCH_CLASS_DEFAULT;
  1360. }
  1361. return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  1362. }
  1363. /* }}} */
  1364. static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
  1365. {
  1366. zend_string *class_name = zend_ast_get_str(ast);
  1367. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
  1368. zend_error_noreturn(E_COMPILE_ERROR,
  1369. "Cannot use '%s' as %s, as it is reserved",
  1370. ZSTR_VAL(class_name), type);
  1371. }
  1372. return zend_resolve_class_name(class_name, ast->attr);
  1373. }
  1374. static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
  1375. {
  1376. if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
  1377. zend_class_entry *ce = CG(active_class_entry);
  1378. if (!ce) {
  1379. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
  1380. fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
  1381. fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
  1382. } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
  1383. zend_error_noreturn(E_COMPILE_ERROR,
  1384. "Cannot use \"parent\" when current class scope has no parent");
  1385. }
  1386. }
  1387. }
  1388. /* }}} */
  1389. static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
  1390. {
  1391. uint32_t fetch_type;
  1392. zval *class_name;
  1393. if (class_ast->kind != ZEND_AST_ZVAL) {
  1394. return 0;
  1395. }
  1396. class_name = zend_ast_get_zval(class_ast);
  1397. if (Z_TYPE_P(class_name) != IS_STRING) {
  1398. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  1399. }
  1400. fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
  1401. zend_ensure_valid_class_fetch_type(fetch_type);
  1402. switch (fetch_type) {
  1403. case ZEND_FETCH_CLASS_SELF:
  1404. if (CG(active_class_entry) && zend_is_scope_known()) {
  1405. ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
  1406. return 1;
  1407. }
  1408. return 0;
  1409. case ZEND_FETCH_CLASS_PARENT:
  1410. if (CG(active_class_entry) && CG(active_class_entry)->parent_name
  1411. && zend_is_scope_known()) {
  1412. ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
  1413. return 1;
  1414. }
  1415. return 0;
  1416. case ZEND_FETCH_CLASS_STATIC:
  1417. return 0;
  1418. case ZEND_FETCH_CLASS_DEFAULT:
  1419. ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
  1420. return 1;
  1421. EMPTY_SWITCH_DEFAULT_CASE()
  1422. }
  1423. }
  1424. /* }}} */
  1425. /* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
  1426. static bool zend_verify_ct_const_access(zend_class_constant *c, zend_class_entry *scope)
  1427. {
  1428. if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
  1429. return 1;
  1430. } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
  1431. return c->ce == scope;
  1432. } else {
  1433. zend_class_entry *ce = c->ce;
  1434. while (1) {
  1435. if (ce == scope) {
  1436. return 1;
  1437. }
  1438. if (!ce->parent) {
  1439. break;
  1440. }
  1441. if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
  1442. ce = ce->parent;
  1443. } else {
  1444. ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
  1445. if (!ce) {
  1446. break;
  1447. }
  1448. }
  1449. }
  1450. /* Reverse case cannot be true during compilation */
  1451. return 0;
  1452. }
  1453. }
  1454. static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
  1455. {
  1456. uint32_t fetch_type = zend_get_class_fetch_type(class_name);
  1457. zend_class_constant *cc;
  1458. zval *c;
  1459. if (class_name_refers_to_active_ce(class_name, fetch_type)) {
  1460. cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
  1461. } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
  1462. zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
  1463. if (ce) {
  1464. cc = zend_hash_find_ptr(&ce->constants_table, name);
  1465. } else {
  1466. return 0;
  1467. }
  1468. } else {
  1469. return 0;
  1470. }
  1471. if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
  1472. return 0;
  1473. }
  1474. if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
  1475. return 0;
  1476. }
  1477. c = &cc->value;
  1478. /* Substitute case-sensitive (or lowercase) persistent class constants */
  1479. if (Z_TYPE_P(c) < IS_OBJECT) {
  1480. ZVAL_COPY_OR_DUP(zv, c);
  1481. return 1;
  1482. }
  1483. return 0;
  1484. }
  1485. /* }}} */
  1486. static void zend_add_to_list(void *result, void *item) /* {{{ */
  1487. {
  1488. void** list = *(void**)result;
  1489. size_t n = 0;
  1490. if (list) {
  1491. while (list[n]) {
  1492. n++;
  1493. }
  1494. }
  1495. list = erealloc(list, sizeof(void*) * (n+2));
  1496. list[n] = item;
  1497. list[n+1] = NULL;
  1498. *(void**)result = list;
  1499. }
  1500. /* }}} */
  1501. static void zend_do_extended_stmt(void) /* {{{ */
  1502. {
  1503. zend_op *opline;
  1504. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
  1505. return;
  1506. }
  1507. opline = get_next_op();
  1508. opline->opcode = ZEND_EXT_STMT;
  1509. }
  1510. /* }}} */
  1511. static void zend_do_extended_fcall_begin(void) /* {{{ */
  1512. {
  1513. zend_op *opline;
  1514. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
  1515. return;
  1516. }
  1517. opline = get_next_op();
  1518. opline->opcode = ZEND_EXT_FCALL_BEGIN;
  1519. }
  1520. /* }}} */
  1521. static void zend_do_extended_fcall_end(void) /* {{{ */
  1522. {
  1523. zend_op *opline;
  1524. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
  1525. return;
  1526. }
  1527. opline = get_next_op();
  1528. opline->opcode = ZEND_EXT_FCALL_END;
  1529. }
  1530. /* }}} */
  1531. ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
  1532. zend_auto_global *auto_global;
  1533. if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
  1534. if (auto_global->armed) {
  1535. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1536. }
  1537. return 1;
  1538. }
  1539. return 0;
  1540. }
  1541. /* }}} */
  1542. ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
  1543. {
  1544. zend_auto_global *auto_global;
  1545. if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
  1546. if (auto_global->armed) {
  1547. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1548. }
  1549. return 1;
  1550. }
  1551. return 0;
  1552. }
  1553. /* }}} */
  1554. ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
  1555. {
  1556. zend_auto_global auto_global;
  1557. zend_result retval;
  1558. auto_global.name = name;
  1559. auto_global.auto_global_callback = auto_global_callback;
  1560. auto_global.jit = jit;
  1561. retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
  1562. return retval;
  1563. }
  1564. /* }}} */
  1565. ZEND_API void zend_activate_auto_globals(void) /* {{{ */
  1566. {
  1567. zend_auto_global *auto_global;
  1568. ZEND_HASH_FOREACH_PTR(CG(auto_globals), auto_global) {
  1569. if (auto_global->jit) {
  1570. auto_global->armed = 1;
  1571. } else if (auto_global->auto_global_callback) {
  1572. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1573. } else {
  1574. auto_global->armed = 0;
  1575. }
  1576. } ZEND_HASH_FOREACH_END();
  1577. }
  1578. /* }}} */
  1579. int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
  1580. {
  1581. zval zv;
  1582. int ret;
  1583. if (CG(increment_lineno)) {
  1584. CG(zend_lineno)++;
  1585. CG(increment_lineno) = 0;
  1586. }
  1587. ret = lex_scan(&zv, elem);
  1588. ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
  1589. return ret;
  1590. }
  1591. /* }}} */
  1592. ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
  1593. {
  1594. bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
  1595. ce->refcount = 1;
  1596. ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
  1597. if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
  1598. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  1599. }
  1600. ce->default_properties_table = NULL;
  1601. ce->default_static_members_table = NULL;
  1602. zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
  1603. zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
  1604. zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
  1605. if (ce->type == ZEND_USER_CLASS) {
  1606. ce->info.user.doc_comment = NULL;
  1607. }
  1608. ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
  1609. ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
  1610. ce->default_properties_count = 0;
  1611. ce->default_static_members_count = 0;
  1612. ce->properties_info_table = NULL;
  1613. ce->attributes = NULL;
  1614. ce->enum_backing_type = IS_UNDEF;
  1615. ce->backed_enum_table = NULL;
  1616. if (nullify_handlers) {
  1617. ce->constructor = NULL;
  1618. ce->destructor = NULL;
  1619. ce->clone = NULL;
  1620. ce->__get = NULL;
  1621. ce->__set = NULL;
  1622. ce->__unset = NULL;
  1623. ce->__isset = NULL;
  1624. ce->__call = NULL;
  1625. ce->__callstatic = NULL;
  1626. ce->__tostring = NULL;
  1627. ce->__serialize = NULL;
  1628. ce->__unserialize = NULL;
  1629. ce->__debugInfo = NULL;
  1630. ce->create_object = NULL;
  1631. ce->get_iterator = NULL;
  1632. ce->iterator_funcs_ptr = NULL;
  1633. ce->get_static_method = NULL;
  1634. ce->parent = NULL;
  1635. ce->parent_name = NULL;
  1636. ce->num_interfaces = 0;
  1637. ce->interfaces = NULL;
  1638. ce->num_traits = 0;
  1639. ce->trait_names = NULL;
  1640. ce->trait_aliases = NULL;
  1641. ce->trait_precedences = NULL;
  1642. ce->serialize = NULL;
  1643. ce->unserialize = NULL;
  1644. if (ce->type == ZEND_INTERNAL_CLASS) {
  1645. ce->info.internal.module = NULL;
  1646. ce->info.internal.builtin_functions = NULL;
  1647. }
  1648. }
  1649. }
  1650. /* }}} */
  1651. ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
  1652. {
  1653. return op_array->vars[EX_VAR_TO_NUM(var)];
  1654. }
  1655. /* }}} */
  1656. zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
  1657. {
  1658. zval *left_zv = zend_ast_get_zval(left_ast);
  1659. zend_string *left = Z_STR_P(left_zv);
  1660. zend_string *right = zend_ast_get_str(right_ast);
  1661. zend_string *result;
  1662. size_t left_len = ZSTR_LEN(left);
  1663. size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
  1664. result = zend_string_extend(left, len, 0);
  1665. ZSTR_VAL(result)[left_len] = '\\';
  1666. memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
  1667. ZSTR_VAL(result)[len] = '\0';
  1668. zend_string_release_ex(right, 0);
  1669. ZVAL_STR(left_zv, result);
  1670. return left_ast;
  1671. }
  1672. /* }}} */
  1673. zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
  1674. {
  1675. zval *zv = zend_ast_get_zval(ast);
  1676. if (Z_TYPE_P(zv) == IS_LONG) {
  1677. if (Z_LVAL_P(zv) == 0) {
  1678. ZVAL_NEW_STR(zv, zend_string_init("-0", sizeof("-0")-1, 0));
  1679. } else {
  1680. ZEND_ASSERT(Z_LVAL_P(zv) > 0);
  1681. Z_LVAL_P(zv) *= -1;
  1682. }
  1683. } else if (Z_TYPE_P(zv) == IS_STRING) {
  1684. size_t orig_len = Z_STRLEN_P(zv);
  1685. Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
  1686. memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
  1687. Z_STRVAL_P(zv)[0] = '-';
  1688. } else {
  1689. ZEND_UNREACHABLE();
  1690. }
  1691. return ast;
  1692. }
  1693. /* }}} */
  1694. static void zend_verify_namespace(void) /* {{{ */
  1695. {
  1696. if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
  1697. zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
  1698. }
  1699. }
  1700. /* }}} */
  1701. /* {{{ zend_dirname
  1702. Returns directory name component of path */
  1703. ZEND_API size_t zend_dirname(char *path, size_t len)
  1704. {
  1705. char *end = path + len - 1;
  1706. unsigned int len_adjust = 0;
  1707. #ifdef ZEND_WIN32
  1708. /* Note that on Win32 CWD is per drive (heritage from CP/M).
  1709. * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
  1710. */
  1711. if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
  1712. /* Skip over the drive spec (if any) so as not to change */
  1713. path += 2;
  1714. len_adjust += 2;
  1715. if (2 == len) {
  1716. /* Return "c:" on Win32 for dirname("c:").
  1717. * It would be more consistent to return "c:."
  1718. * but that would require making the string *longer*.
  1719. */
  1720. return len;
  1721. }
  1722. }
  1723. #endif
  1724. if (len == 0) {
  1725. /* Illegal use of this function */
  1726. return 0;
  1727. }
  1728. /* Strip trailing slashes */
  1729. while (end >= path && IS_SLASH_P(end)) {
  1730. end--;
  1731. }
  1732. if (end < path) {
  1733. /* The path only contained slashes */
  1734. path[0] = DEFAULT_SLASH;
  1735. path[1] = '\0';
  1736. return 1 + len_adjust;
  1737. }
  1738. /* Strip filename */
  1739. while (end >= path && !IS_SLASH_P(end)) {
  1740. end--;
  1741. }
  1742. if (end < path) {
  1743. /* No slash found, therefore return '.' */
  1744. path[0] = '.';
  1745. path[1] = '\0';
  1746. return 1 + len_adjust;
  1747. }
  1748. /* Strip slashes which came before the file name */
  1749. while (end >= path && IS_SLASH_P(end)) {
  1750. end--;
  1751. }
  1752. if (end < path) {
  1753. path[0] = DEFAULT_SLASH;
  1754. path[1] = '\0';
  1755. return 1 + len_adjust;
  1756. }
  1757. *(end+1) = '\0';
  1758. return (size_t)(end + 1 - path) + len_adjust;
  1759. }
  1760. /* }}} */
  1761. static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
  1762. {
  1763. zend_uchar factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
  1764. switch (type) {
  1765. case BP_VAR_R:
  1766. opline->result_type = IS_TMP_VAR;
  1767. result->op_type = IS_TMP_VAR;
  1768. return;
  1769. case BP_VAR_W:
  1770. opline->opcode += 1 * factor;
  1771. return;
  1772. case BP_VAR_RW:
  1773. opline->opcode += 2 * factor;
  1774. return;
  1775. case BP_VAR_IS:
  1776. opline->result_type = IS_TMP_VAR;
  1777. result->op_type = IS_TMP_VAR;
  1778. opline->opcode += 3 * factor;
  1779. return;
  1780. case BP_VAR_FUNC_ARG:
  1781. opline->opcode += 4 * factor;
  1782. return;
  1783. case BP_VAR_UNSET:
  1784. opline->opcode += 5 * factor;
  1785. return;
  1786. EMPTY_SWITCH_DEFAULT_CASE()
  1787. }
  1788. }
  1789. /* }}} */
  1790. static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
  1791. {
  1792. opline->result_type = IS_VAR;
  1793. opline->result.var = get_temporary_variable();
  1794. GET_NODE(result, opline->result);
  1795. }
  1796. /* }}} */
  1797. static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
  1798. {
  1799. opline->result_type = IS_TMP_VAR;
  1800. opline->result.var = get_temporary_variable();
  1801. GET_NODE(result, opline->result);
  1802. }
  1803. /* }}} */
  1804. static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1805. {
  1806. zend_op *opline = get_next_op();
  1807. opline->opcode = opcode;
  1808. if (op1 != NULL) {
  1809. SET_NODE(opline->op1, op1);
  1810. }
  1811. if (op2 != NULL) {
  1812. SET_NODE(opline->op2, op2);
  1813. }
  1814. if (result) {
  1815. zend_make_var_result(result, opline);
  1816. }
  1817. return opline;
  1818. }
  1819. /* }}} */
  1820. static zend_op *zend_emit_op_tmp(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1821. {
  1822. zend_op *opline = get_next_op();
  1823. opline->opcode = opcode;
  1824. if (op1 != NULL) {
  1825. SET_NODE(opline->op1, op1);
  1826. }
  1827. if (op2 != NULL) {
  1828. SET_NODE(opline->op2, op2);
  1829. }
  1830. if (result) {
  1831. zend_make_tmp_result(result, opline);
  1832. }
  1833. return opline;
  1834. }
  1835. /* }}} */
  1836. static void zend_emit_tick(void) /* {{{ */
  1837. {
  1838. zend_op *opline;
  1839. /* This prevents a double TICK generated by the parser statement of "declare()" */
  1840. if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
  1841. return;
  1842. }
  1843. opline = get_next_op();
  1844. opline->opcode = ZEND_TICKS;
  1845. opline->extended_value = FC(declarables).ticks;
  1846. }
  1847. /* }}} */
  1848. static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
  1849. {
  1850. return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
  1851. }
  1852. /* }}} */
  1853. static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
  1854. {
  1855. uint32_t opnum = get_next_op_number();
  1856. zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
  1857. opline->op1.opline_num = opnum_target;
  1858. return opnum;
  1859. }
  1860. /* }}} */
  1861. ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
  1862. {
  1863. switch (opline->opcode) {
  1864. case ZEND_IS_IDENTICAL:
  1865. case ZEND_IS_NOT_IDENTICAL:
  1866. case ZEND_IS_EQUAL:
  1867. case ZEND_IS_NOT_EQUAL:
  1868. case ZEND_IS_SMALLER:
  1869. case ZEND_IS_SMALLER_OR_EQUAL:
  1870. case ZEND_CASE:
  1871. case ZEND_CASE_STRICT:
  1872. case ZEND_ISSET_ISEMPTY_CV:
  1873. case ZEND_ISSET_ISEMPTY_VAR:
  1874. case ZEND_ISSET_ISEMPTY_DIM_OBJ:
  1875. case ZEND_ISSET_ISEMPTY_PROP_OBJ:
  1876. case ZEND_ISSET_ISEMPTY_STATIC_PROP:
  1877. case ZEND_INSTANCEOF:
  1878. case ZEND_TYPE_CHECK:
  1879. case ZEND_DEFINED:
  1880. case ZEND_IN_ARRAY:
  1881. case ZEND_ARRAY_KEY_EXISTS:
  1882. return 1;
  1883. default:
  1884. return 0;
  1885. }
  1886. }
  1887. /* }}} */
  1888. static inline uint32_t zend_emit_cond_jump(zend_uchar opcode, znode *cond, uint32_t opnum_target) /* {{{ */
  1889. {
  1890. uint32_t opnum = get_next_op_number();
  1891. zend_op *opline;
  1892. if (cond->op_type == IS_TMP_VAR && opnum > 0) {
  1893. opline = CG(active_op_array)->opcodes + opnum - 1;
  1894. if (opline->result_type == IS_TMP_VAR
  1895. && opline->result.var == cond->u.op.var
  1896. && zend_is_smart_branch(opline)) {
  1897. if (opcode == ZEND_JMPZ) {
  1898. opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
  1899. } else {
  1900. ZEND_ASSERT(opcode == ZEND_JMPNZ);
  1901. opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
  1902. }
  1903. }
  1904. }
  1905. opline = zend_emit_op(NULL, opcode, cond, NULL);
  1906. opline->op2.opline_num = opnum_target;
  1907. return opnum;
  1908. }
  1909. /* }}} */
  1910. static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
  1911. {
  1912. zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
  1913. switch (opline->opcode) {
  1914. case ZEND_JMP:
  1915. opline->op1.opline_num = opnum_target;
  1916. break;
  1917. case ZEND_JMPZ:
  1918. case ZEND_JMPNZ:
  1919. case ZEND_JMPZ_EX:
  1920. case ZEND_JMPNZ_EX:
  1921. case ZEND_JMP_SET:
  1922. case ZEND_COALESCE:
  1923. case ZEND_JMP_NULL:
  1924. opline->op2.opline_num = opnum_target;
  1925. break;
  1926. EMPTY_SWITCH_DEFAULT_CASE()
  1927. }
  1928. }
  1929. /* }}} */
  1930. static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
  1931. {
  1932. zend_update_jump_target(opnum_jump, get_next_op_number());
  1933. }
  1934. /* }}} */
  1935. static inline zend_op *zend_delayed_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1936. {
  1937. zend_op tmp_opline;
  1938. init_op(&tmp_opline);
  1939. tmp_opline.opcode = opcode;
  1940. if (op1 != NULL) {
  1941. SET_NODE(tmp_opline.op1, op1);
  1942. }
  1943. if (op2 != NULL) {
  1944. SET_NODE(tmp_opline.op2, op2);
  1945. }
  1946. if (result) {
  1947. zend_make_var_result(result, &tmp_opline);
  1948. }
  1949. zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
  1950. return zend_stack_top(&CG(delayed_oplines_stack));
  1951. }
  1952. /* }}} */
  1953. static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
  1954. {
  1955. return zend_stack_count(&CG(delayed_oplines_stack));
  1956. }
  1957. /* }}} */
  1958. static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
  1959. {
  1960. zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
  1961. uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
  1962. ZEND_ASSERT(count >= offset);
  1963. for (i = offset; i < count; ++i) {
  1964. if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
  1965. opline = get_next_op();
  1966. memcpy(opline, &oplines[i], sizeof(zend_op));
  1967. } else {
  1968. opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
  1969. }
  1970. }
  1971. CG(delayed_oplines_stack).top = offset;
  1972. return opline;
  1973. }
  1974. /* }}} */
  1975. static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
  1976. {
  1977. switch (ast_kind) {
  1978. case ZEND_AST_DIM:
  1979. case ZEND_AST_PROP:
  1980. case ZEND_AST_NULLSAFE_PROP:
  1981. case ZEND_AST_STATIC_PROP:
  1982. case ZEND_AST_METHOD_CALL:
  1983. case ZEND_AST_NULLSAFE_METHOD_CALL:
  1984. case ZEND_AST_STATIC_CALL:
  1985. return 1;
  1986. default:
  1987. return 0;
  1988. }
  1989. }
  1990. static bool zend_ast_is_short_circuited(const zend_ast *ast)
  1991. {
  1992. switch (ast->kind) {
  1993. case ZEND_AST_DIM:
  1994. case ZEND_AST_PROP:
  1995. case ZEND_AST_STATIC_PROP:
  1996. case ZEND_AST_METHOD_CALL:
  1997. case ZEND_AST_STATIC_CALL:
  1998. return zend_ast_is_short_circuited(ast->child[0]);
  1999. case ZEND_AST_NULLSAFE_PROP:
  2000. case ZEND_AST_NULLSAFE_METHOD_CALL:
  2001. return 1;
  2002. default:
  2003. return 0;
  2004. }
  2005. }
  2006. /* Mark nodes that are an inner part of a short-circuiting chain.
  2007. * We should not perform a "commit" on them, as it will be performed by the outer-most node.
  2008. * We do this to avoid passing down an argument in various compile functions. */
  2009. #define ZEND_SHORT_CIRCUITING_INNER 0x8000
  2010. static void zend_short_circuiting_mark_inner(zend_ast *ast) {
  2011. if (zend_ast_kind_is_short_circuited(ast->kind)) {
  2012. ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
  2013. }
  2014. }
  2015. static uint32_t zend_short_circuiting_checkpoint(void)
  2016. {
  2017. return zend_stack_count(&CG(short_circuiting_opnums));
  2018. }
  2019. static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, zend_ast *ast)
  2020. {
  2021. bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
  2022. || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
  2023. if (!is_short_circuited) {
  2024. ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
  2025. && "Short circuiting stack should be empty");
  2026. return;
  2027. }
  2028. if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
  2029. /* Outer-most node will commit. */
  2030. return;
  2031. }
  2032. while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
  2033. uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
  2034. zend_op *opline = &CG(active_op_array)->opcodes[opnum];
  2035. opline->op2.opline_num = get_next_op_number();
  2036. SET_NODE(opline->result, result);
  2037. opline->extended_value =
  2038. ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
  2039. ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
  2040. ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
  2041. zend_stack_del_top(&CG(short_circuiting_opnums));
  2042. }
  2043. }
  2044. static void zend_emit_jmp_null(znode *obj_node)
  2045. {
  2046. uint32_t jmp_null_opnum = get_next_op_number();
  2047. zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
  2048. if (opline->op1_type == IS_CONST) {
  2049. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  2050. }
  2051. zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
  2052. }
  2053. #define ZEND_MEMOIZE_NONE 0
  2054. #define ZEND_MEMOIZE_COMPILE 1
  2055. #define ZEND_MEMOIZE_FETCH 2
  2056. static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
  2057. {
  2058. int memoize_mode = CG(memoize_mode);
  2059. if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
  2060. znode memoized_result;
  2061. /* Go through normal compilation */
  2062. CG(memoize_mode) = ZEND_MEMOIZE_NONE;
  2063. zend_compile_expr(result, expr);
  2064. CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
  2065. if (result->op_type == IS_VAR) {
  2066. zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
  2067. } else if (result->op_type == IS_TMP_VAR) {
  2068. zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
  2069. } else {
  2070. if (result->op_type == IS_CONST) {
  2071. Z_TRY_ADDREF(result->u.constant);
  2072. }
  2073. memoized_result = *result;
  2074. }
  2075. zend_hash_index_update_mem(
  2076. CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
  2077. } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
  2078. znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
  2079. *result = *memoized_result;
  2080. if (result->op_type == IS_CONST) {
  2081. Z_TRY_ADDREF(result->u.constant);
  2082. }
  2083. } else {
  2084. ZEND_UNREACHABLE();
  2085. }
  2086. }
  2087. /* }}} */
  2088. static size_t zend_type_get_num_classes(zend_type type) {
  2089. if (!ZEND_TYPE_IS_COMPLEX(type)) {
  2090. return 0;
  2091. }
  2092. if (ZEND_TYPE_HAS_LIST(type)) {
  2093. return ZEND_TYPE_LIST(type)->num_types;
  2094. }
  2095. return 1;
  2096. }
  2097. static void zend_emit_return_type_check(
  2098. znode *expr, zend_arg_info *return_info, bool implicit) /* {{{ */
  2099. {
  2100. zend_type type = return_info->type;
  2101. if (ZEND_TYPE_IS_SET(type)) {
  2102. zend_op *opline;
  2103. /* `return ...;` is illegal in a void function (but `return;` isn't) */
  2104. if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
  2105. if (expr) {
  2106. if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
  2107. zend_error_noreturn(E_COMPILE_ERROR,
  2108. "A void function must not return a value "
  2109. "(did you mean \"return;\" instead of \"return null;\"?)");
  2110. } else {
  2111. zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
  2112. }
  2113. }
  2114. /* we don't need run-time check */
  2115. return;
  2116. }
  2117. /* `return` is illegal in a never-returning function */
  2118. if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
  2119. /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
  2120. ZEND_ASSERT(!implicit);
  2121. zend_error_noreturn(E_COMPILE_ERROR, "A never-returning function must not return");
  2122. return;
  2123. }
  2124. if (!expr && !implicit) {
  2125. if (ZEND_TYPE_ALLOW_NULL(type)) {
  2126. zend_error_noreturn(E_COMPILE_ERROR,
  2127. "A function with return type must return a value "
  2128. "(did you mean \"return null;\" instead of \"return;\"?)");
  2129. } else {
  2130. zend_error_noreturn(E_COMPILE_ERROR,
  2131. "A function with return type must return a value");
  2132. }
  2133. }
  2134. if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
  2135. /* we don't need run-time check for mixed return type */
  2136. return;
  2137. }
  2138. if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
  2139. /* we don't need run-time check */
  2140. return;
  2141. }
  2142. opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
  2143. if (expr && expr->op_type == IS_CONST) {
  2144. opline->result_type = expr->op_type = IS_TMP_VAR;
  2145. opline->result.var = expr->u.op.var = get_temporary_variable();
  2146. }
  2147. opline->op2.num = zend_alloc_cache_slots(zend_type_get_num_classes(return_info->type));
  2148. }
  2149. }
  2150. /* }}} */
  2151. void zend_emit_final_return(bool return_one) /* {{{ */
  2152. {
  2153. znode zn;
  2154. zend_op *ret;
  2155. bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  2156. if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
  2157. && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
  2158. zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
  2159. if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
  2160. zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
  2161. return;
  2162. }
  2163. zend_emit_return_type_check(NULL, return_info, 1);
  2164. }
  2165. zn.op_type = IS_CONST;
  2166. if (return_one) {
  2167. ZVAL_LONG(&zn.u.constant, 1);
  2168. } else {
  2169. ZVAL_NULL(&zn.u.constant);
  2170. }
  2171. ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
  2172. ret->extended_value = -1;
  2173. }
  2174. /* }}} */
  2175. static inline bool zend_is_variable(zend_ast *ast) /* {{{ */
  2176. {
  2177. return ast->kind == ZEND_AST_VAR
  2178. || ast->kind == ZEND_AST_DIM
  2179. || ast->kind == ZEND_AST_PROP
  2180. || ast->kind == ZEND_AST_NULLSAFE_PROP
  2181. || ast->kind == ZEND_AST_STATIC_PROP;
  2182. }
  2183. /* }}} */
  2184. static inline bool zend_is_call(zend_ast *ast) /* {{{ */
  2185. {
  2186. return ast->kind == ZEND_AST_CALL
  2187. || ast->kind == ZEND_AST_METHOD_CALL
  2188. || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
  2189. || ast->kind == ZEND_AST_STATIC_CALL;
  2190. }
  2191. /* }}} */
  2192. static inline bool zend_is_variable_or_call(zend_ast *ast) /* {{{ */
  2193. {
  2194. return zend_is_variable(ast) || zend_is_call(ast);
  2195. }
  2196. /* }}} */
  2197. static inline bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
  2198. {
  2199. return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
  2200. || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
  2201. || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
  2202. }
  2203. /* }}} */
  2204. static inline bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
  2205. {
  2206. while (
  2207. ast->kind == ZEND_AST_DIM
  2208. || ast->kind == ZEND_AST_PROP
  2209. ) {
  2210. ast = ast->child[0];
  2211. }
  2212. return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
  2213. }
  2214. /* }}} */
  2215. static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
  2216. {
  2217. if (name_ast->kind != ZEND_AST_ZVAL) {
  2218. return 0;
  2219. }
  2220. return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
  2221. }
  2222. /* }}} */
  2223. static inline void zend_handle_numeric_op(znode *node) /* {{{ */
  2224. {
  2225. if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
  2226. zend_ulong index;
  2227. if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
  2228. zval_ptr_dtor(&node->u.constant);
  2229. ZVAL_LONG(&node->u.constant, index);
  2230. }
  2231. }
  2232. }
  2233. /* }}} */
  2234. static inline void zend_handle_numeric_dim(zend_op *opline, znode *dim_node) /* {{{ */
  2235. {
  2236. if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
  2237. zend_ulong index;
  2238. if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
  2239. /* For numeric indexes we also keep the original value to use by ArrayAccess
  2240. * See bug #63217
  2241. */
  2242. int c = zend_add_literal(&dim_node->u.constant);
  2243. ZEND_ASSERT(opline->op2.constant + 1 == c);
  2244. ZVAL_LONG(CT_CONSTANT(opline->op2), index);
  2245. Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
  2246. return;
  2247. }
  2248. }
  2249. }
  2250. /* }}} */
  2251. static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
  2252. {
  2253. if (class_node->op_type == IS_CONST) {
  2254. opline->op1_type = IS_CONST;
  2255. opline->op1.constant = zend_add_class_name_literal(
  2256. Z_STR(class_node->u.constant));
  2257. } else {
  2258. SET_NODE(opline->op1, class_node);
  2259. }
  2260. }
  2261. /* }}} */
  2262. static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
  2263. {
  2264. uint32_t fetch_type;
  2265. if (name_ast->kind != ZEND_AST_ZVAL) {
  2266. znode name_node;
  2267. zend_compile_expr(&name_node, name_ast);
  2268. if (name_node.op_type == IS_CONST) {
  2269. zend_string *name;
  2270. if (Z_TYPE(name_node.u.constant) != IS_STRING) {
  2271. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  2272. }
  2273. name = Z_STR(name_node.u.constant);
  2274. fetch_type = zend_get_class_fetch_type(name);
  2275. if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
  2276. result->op_type = IS_CONST;
  2277. ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
  2278. } else {
  2279. zend_ensure_valid_class_fetch_type(fetch_type);
  2280. result->op_type = IS_UNUSED;
  2281. result->u.op.num = fetch_type | fetch_flags;
  2282. }
  2283. zend_string_release_ex(name, 0);
  2284. } else {
  2285. zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
  2286. opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
  2287. }
  2288. return;
  2289. }
  2290. /* Fully qualified names are always default refs */
  2291. if (name_ast->attr == ZEND_NAME_FQ) {
  2292. result->op_type = IS_CONST;
  2293. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  2294. return;
  2295. }
  2296. fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  2297. if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
  2298. result->op_type = IS_CONST;
  2299. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  2300. } else {
  2301. zend_ensure_valid_class_fetch_type(fetch_type);
  2302. result->op_type = IS_UNUSED;
  2303. result->u.op.num = fetch_type | fetch_flags;
  2304. }
  2305. }
  2306. /* }}} */
  2307. static zend_result zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
  2308. {
  2309. zend_ast *name_ast = ast->child[0];
  2310. if (name_ast->kind == ZEND_AST_ZVAL) {
  2311. zval *zv = zend_ast_get_zval(name_ast);
  2312. zend_string *name;
  2313. if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
  2314. name = zval_make_interned_string(zv);
  2315. } else {
  2316. name = zend_new_interned_string(zval_get_string_func(zv));
  2317. }
  2318. if (zend_is_auto_global(name)) {
  2319. return FAILURE;
  2320. }
  2321. result->op_type = IS_CV;
  2322. result->u.op.var = lookup_cv(name);
  2323. if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
  2324. zend_string_release_ex(name, 0);
  2325. }
  2326. return SUCCESS;
  2327. }
  2328. return FAILURE;
  2329. }
  2330. /* }}} */
  2331. static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
  2332. {
  2333. zend_ast *name_ast = ast->child[0];
  2334. znode name_node;
  2335. zend_op *opline;
  2336. zend_compile_expr(&name_node, name_ast);
  2337. if (name_node.op_type == IS_CONST) {
  2338. convert_to_string(&name_node.u.constant);
  2339. }
  2340. if (delayed) {
  2341. opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
  2342. } else {
  2343. opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
  2344. }
  2345. if (name_node.op_type == IS_CONST &&
  2346. zend_is_auto_global(Z_STR(name_node.u.constant))) {
  2347. opline->extended_value = ZEND_FETCH_GLOBAL;
  2348. } else {
  2349. opline->extended_value = ZEND_FETCH_LOCAL;
  2350. }
  2351. zend_adjust_for_fetch_type(opline, result, type);
  2352. return opline;
  2353. }
  2354. /* }}} */
  2355. static bool is_this_fetch(zend_ast *ast) /* {{{ */
  2356. {
  2357. if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
  2358. zval *name = zend_ast_get_zval(ast->child[0]);
  2359. return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "this");
  2360. }
  2361. return 0;
  2362. }
  2363. /* }}} */
  2364. static bool is_globals_fetch(const zend_ast *ast)
  2365. {
  2366. if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
  2367. zval *name = zend_ast_get_zval(ast->child[0]);
  2368. return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
  2369. }
  2370. return 0;
  2371. }
  2372. static bool is_global_var_fetch(zend_ast *ast)
  2373. {
  2374. return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
  2375. }
  2376. static bool this_guaranteed_exists(void) /* {{{ */
  2377. {
  2378. zend_op_array *op_array = CG(active_op_array);
  2379. /* Instance methods always have a $this.
  2380. * This also includes closures that have a scope and use $this. */
  2381. return op_array->scope != NULL
  2382. && (op_array->fn_flags & ZEND_ACC_STATIC) == 0;
  2383. }
  2384. /* }}} */
  2385. static zend_op *zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
  2386. {
  2387. if (is_this_fetch(ast)) {
  2388. zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
  2389. if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
  2390. opline->result_type = IS_TMP_VAR;
  2391. result->op_type = IS_TMP_VAR;
  2392. }
  2393. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  2394. return opline;
  2395. } else if (is_globals_fetch(ast)) {
  2396. zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
  2397. if (type == BP_VAR_R || type == BP_VAR_IS) {
  2398. opline->result_type = IS_TMP_VAR;
  2399. result->op_type = IS_TMP_VAR;
  2400. }
  2401. return opline;
  2402. } else if (zend_try_compile_cv(result, ast) == FAILURE) {
  2403. return zend_compile_simple_var_no_cv(result, ast, type, delayed);
  2404. }
  2405. return NULL;
  2406. }
  2407. /* }}} */
  2408. static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
  2409. {
  2410. if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
  2411. if (node->op_type == IS_VAR) {
  2412. zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
  2413. opline->result_type = IS_VAR;
  2414. opline->result.var = opline->op1.var;
  2415. } else {
  2416. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
  2417. }
  2418. }
  2419. }
  2420. /* }}} */
  2421. static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
  2422. {
  2423. znode dummy_node;
  2424. zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
  2425. zend_ast_create_znode(value_node));
  2426. zend_compile_expr(&dummy_node, assign_ast);
  2427. zend_do_free(&dummy_node);
  2428. }
  2429. /* }}} */
  2430. static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2431. {
  2432. if (ast->attr == ZEND_DIM_ALTERNATIVE_SYNTAX) {
  2433. zend_error(E_COMPILE_ERROR, "Array and string offset access syntax with curly braces is no longer supported");
  2434. }
  2435. zend_ast *var_ast = ast->child[0];
  2436. zend_ast *dim_ast = ast->child[1];
  2437. zend_op *opline;
  2438. znode var_node, dim_node;
  2439. if (is_globals_fetch(var_ast)) {
  2440. if (dim_ast == NULL) {
  2441. zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
  2442. }
  2443. zend_compile_expr(&dim_node, dim_ast);
  2444. if (dim_node.op_type == IS_CONST) {
  2445. convert_to_string(&dim_node.u.constant);
  2446. }
  2447. opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
  2448. opline->extended_value = ZEND_FETCH_GLOBAL;
  2449. zend_adjust_for_fetch_type(opline, result, type);
  2450. return opline;
  2451. } else {
  2452. zend_short_circuiting_mark_inner(var_ast);
  2453. opline = zend_delayed_compile_var(&var_node, var_ast, type, 0);
  2454. if (opline && type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
  2455. opline->extended_value |= ZEND_FETCH_DIM_WRITE;
  2456. }
  2457. }
  2458. zend_separate_if_call_and_write(&var_node, var_ast, type);
  2459. if (dim_ast == NULL) {
  2460. if (type == BP_VAR_R || type == BP_VAR_IS) {
  2461. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  2462. }
  2463. if (type == BP_VAR_UNSET) {
  2464. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
  2465. }
  2466. dim_node.op_type = IS_UNUSED;
  2467. } else {
  2468. zend_compile_expr(&dim_node, dim_ast);
  2469. }
  2470. opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
  2471. zend_adjust_for_fetch_type(opline, result, type);
  2472. if (dim_node.op_type == IS_CONST) {
  2473. zend_handle_numeric_dim(opline, &dim_node);
  2474. }
  2475. return opline;
  2476. }
  2477. /* }}} */
  2478. static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2479. {
  2480. uint32_t offset = zend_delayed_compile_begin();
  2481. zend_delayed_compile_dim(result, ast, type);
  2482. return zend_delayed_compile_end(offset);
  2483. }
  2484. /* }}} */
  2485. static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2486. {
  2487. zend_ast *obj_ast = ast->child[0];
  2488. zend_ast *prop_ast = ast->child[1];
  2489. znode obj_node, prop_node;
  2490. zend_op *opline;
  2491. bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
  2492. if (is_this_fetch(obj_ast)) {
  2493. if (this_guaranteed_exists()) {
  2494. obj_node.op_type = IS_UNUSED;
  2495. } else {
  2496. zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
  2497. }
  2498. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  2499. /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
  2500. * check for a nullsafe access. */
  2501. } else {
  2502. zend_short_circuiting_mark_inner(obj_ast);
  2503. opline = zend_delayed_compile_var(&obj_node, obj_ast, type, 0);
  2504. zend_separate_if_call_and_write(&obj_node, obj_ast, type);
  2505. if (nullsafe) {
  2506. if (obj_node.op_type == IS_TMP_VAR) {
  2507. /* Flush delayed oplines */
  2508. zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
  2509. uint32_t var = obj_node.u.op.var;
  2510. uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
  2511. uint32_t i = count;
  2512. while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
  2513. i--;
  2514. if (oplines[i].op1_type == IS_TMP_VAR) {
  2515. var = oplines[i].op1.var;
  2516. } else {
  2517. break;
  2518. }
  2519. }
  2520. for (; i < count; ++i) {
  2521. if (oplines[i].opcode != ZEND_NOP) {
  2522. opline = get_next_op();
  2523. memcpy(opline, &oplines[i], sizeof(zend_op));
  2524. oplines[i].opcode = ZEND_NOP;
  2525. oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
  2526. }
  2527. }
  2528. }
  2529. zend_emit_jmp_null(&obj_node);
  2530. }
  2531. }
  2532. zend_compile_expr(&prop_node, prop_ast);
  2533. opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
  2534. if (opline->op2_type == IS_CONST) {
  2535. convert_to_string(CT_CONSTANT(opline->op2));
  2536. opline->extended_value = zend_alloc_cache_slots(3);
  2537. }
  2538. zend_adjust_for_fetch_type(opline, result, type);
  2539. return opline;
  2540. }
  2541. /* }}} */
  2542. static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
  2543. {
  2544. uint32_t offset = zend_delayed_compile_begin();
  2545. zend_op *opline = zend_delayed_compile_prop(result, ast, type);
  2546. if (by_ref) { /* shared with cache_slot */
  2547. opline->extended_value |= ZEND_FETCH_REF;
  2548. }
  2549. return zend_delayed_compile_end(offset);
  2550. }
  2551. /* }}} */
  2552. static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
  2553. {
  2554. zend_ast *class_ast = ast->child[0];
  2555. zend_ast *prop_ast = ast->child[1];
  2556. znode class_node, prop_node;
  2557. zend_op *opline;
  2558. zend_short_circuiting_mark_inner(class_ast);
  2559. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  2560. zend_compile_expr(&prop_node, prop_ast);
  2561. if (delayed) {
  2562. opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
  2563. } else {
  2564. opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
  2565. }
  2566. if (opline->op1_type == IS_CONST) {
  2567. convert_to_string(CT_CONSTANT(opline->op1));
  2568. opline->extended_value = zend_alloc_cache_slots(3);
  2569. }
  2570. if (class_node.op_type == IS_CONST) {
  2571. opline->op2_type = IS_CONST;
  2572. opline->op2.constant = zend_add_class_name_literal(
  2573. Z_STR(class_node.u.constant));
  2574. if (opline->op1_type != IS_CONST) {
  2575. opline->extended_value = zend_alloc_cache_slot();
  2576. }
  2577. } else {
  2578. SET_NODE(opline->op2, &class_node);
  2579. }
  2580. if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
  2581. opline->extended_value |= ZEND_FETCH_REF;
  2582. }
  2583. zend_adjust_for_fetch_type(opline, result, type);
  2584. return opline;
  2585. }
  2586. /* }}} */
  2587. static void zend_verify_list_assign_target(zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
  2588. if (var_ast->kind == ZEND_AST_ARRAY) {
  2589. if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
  2590. zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
  2591. }
  2592. if (array_style != var_ast->attr) {
  2593. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
  2594. }
  2595. } else if (!zend_can_write_to_variable(var_ast)) {
  2596. zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
  2597. }
  2598. }
  2599. /* }}} */
  2600. static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node);
  2601. /* Propagate refs used on leaf elements to the surrounding list() structures. */
  2602. static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
  2603. zend_ast_list *list = zend_ast_get_list(ast);
  2604. bool has_refs = 0;
  2605. uint32_t i;
  2606. for (i = 0; i < list->children; ++i) {
  2607. zend_ast *elem_ast = list->child[i];
  2608. if (elem_ast) {
  2609. zend_ast *var_ast = elem_ast->child[0];
  2610. if (var_ast->kind == ZEND_AST_ARRAY) {
  2611. elem_ast->attr = zend_propagate_list_refs(var_ast);
  2612. }
  2613. has_refs |= elem_ast->attr;
  2614. }
  2615. }
  2616. return has_refs;
  2617. }
  2618. /* }}} */
  2619. static bool list_is_keyed(zend_ast_list *list)
  2620. {
  2621. for (uint32_t i = 0; i < list->children; i++) {
  2622. zend_ast *child = list->child[i];
  2623. if (child) {
  2624. return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
  2625. }
  2626. }
  2627. return false;
  2628. }
  2629. static void zend_compile_list_assign(
  2630. znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
  2631. {
  2632. zend_ast_list *list = zend_ast_get_list(ast);
  2633. uint32_t i;
  2634. bool has_elems = 0;
  2635. bool is_keyed = list_is_keyed(list);
  2636. if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
  2637. zval_make_interned_string(&expr_node->u.constant);
  2638. }
  2639. for (i = 0; i < list->children; ++i) {
  2640. zend_ast *elem_ast = list->child[i];
  2641. zend_ast *var_ast, *key_ast;
  2642. znode fetch_result, dim_node;
  2643. zend_op *opline;
  2644. if (elem_ast == NULL) {
  2645. if (is_keyed) {
  2646. zend_error(E_COMPILE_ERROR,
  2647. "Cannot use empty array entries in keyed array assignment");
  2648. } else {
  2649. continue;
  2650. }
  2651. }
  2652. if (elem_ast->kind == ZEND_AST_UNPACK) {
  2653. zend_error(E_COMPILE_ERROR,
  2654. "Spread operator is not supported in assignments");
  2655. }
  2656. var_ast = elem_ast->child[0];
  2657. key_ast = elem_ast->child[1];
  2658. has_elems = 1;
  2659. if (is_keyed) {
  2660. if (key_ast == NULL) {
  2661. zend_error(E_COMPILE_ERROR,
  2662. "Cannot mix keyed and unkeyed array entries in assignments");
  2663. }
  2664. zend_compile_expr(&dim_node, key_ast);
  2665. } else {
  2666. if (key_ast != NULL) {
  2667. zend_error(E_COMPILE_ERROR,
  2668. "Cannot mix keyed and unkeyed array entries in assignments");
  2669. }
  2670. dim_node.op_type = IS_CONST;
  2671. ZVAL_LONG(&dim_node.u.constant, i);
  2672. }
  2673. if (expr_node->op_type == IS_CONST) {
  2674. Z_TRY_ADDREF(expr_node->u.constant);
  2675. }
  2676. zend_verify_list_assign_target(var_ast, array_style);
  2677. opline = zend_emit_op(&fetch_result,
  2678. elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node);
  2679. if (dim_node.op_type == IS_CONST) {
  2680. zend_handle_numeric_dim(opline, &dim_node);
  2681. }
  2682. if (elem_ast->attr) {
  2683. zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
  2684. }
  2685. if (var_ast->kind == ZEND_AST_ARRAY) {
  2686. zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
  2687. } else if (elem_ast->attr) {
  2688. zend_emit_assign_ref_znode(var_ast, &fetch_result);
  2689. } else {
  2690. zend_emit_assign_znode(var_ast, &fetch_result);
  2691. }
  2692. }
  2693. if (has_elems == 0) {
  2694. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
  2695. }
  2696. if (result) {
  2697. *result = *expr_node;
  2698. } else {
  2699. zend_do_free(expr_node);
  2700. }
  2701. }
  2702. /* }}} */
  2703. static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
  2704. {
  2705. if (ast->kind == ZEND_AST_CALL) {
  2706. zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
  2707. }
  2708. if (
  2709. ast->kind == ZEND_AST_METHOD_CALL
  2710. || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
  2711. || ast->kind == ZEND_AST_STATIC_CALL
  2712. ) {
  2713. zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
  2714. }
  2715. if (zend_ast_is_short_circuited(ast)) {
  2716. zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
  2717. }
  2718. if (is_globals_fetch(ast)) {
  2719. zend_error_noreturn(E_COMPILE_ERROR,
  2720. "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
  2721. }
  2722. }
  2723. /* }}} */
  2724. /* Detects $a... = $a pattern */
  2725. static bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
  2726. {
  2727. if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
  2728. return 0;
  2729. }
  2730. while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
  2731. var_ast = var_ast->child[0];
  2732. }
  2733. if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
  2734. return 0;
  2735. }
  2736. {
  2737. zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
  2738. zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
  2739. bool result = zend_string_equals(name1, name2);
  2740. zend_string_release_ex(name1, 0);
  2741. zend_string_release_ex(name2, 0);
  2742. return result;
  2743. }
  2744. }
  2745. /* }}} */
  2746. static void zend_compile_expr_with_potential_assign_to_self(
  2747. znode *expr_node, zend_ast *expr_ast, zend_ast *var_ast) {
  2748. if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
  2749. /* $a[0] = $a should evaluate the right $a first */
  2750. znode cv_node;
  2751. if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
  2752. zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, 0);
  2753. } else {
  2754. zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
  2755. }
  2756. } else {
  2757. zend_compile_expr(expr_node, expr_ast);
  2758. }
  2759. }
  2760. static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
  2761. {
  2762. zend_ast *var_ast = ast->child[0];
  2763. zend_ast *expr_ast = ast->child[1];
  2764. znode var_node, expr_node;
  2765. zend_op *opline;
  2766. uint32_t offset;
  2767. if (is_this_fetch(var_ast)) {
  2768. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  2769. }
  2770. zend_ensure_writable_variable(var_ast);
  2771. /* Treat $GLOBALS['x'] assignment like assignment to variable. */
  2772. zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
  2773. switch (kind) {
  2774. case ZEND_AST_VAR:
  2775. offset = zend_delayed_compile_begin();
  2776. zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, 0);
  2777. zend_compile_expr(&expr_node, expr_ast);
  2778. zend_delayed_compile_end(offset);
  2779. CG(zend_lineno) = zend_ast_get_lineno(var_ast);
  2780. zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
  2781. return;
  2782. case ZEND_AST_STATIC_PROP:
  2783. offset = zend_delayed_compile_begin();
  2784. zend_delayed_compile_var(result, var_ast, BP_VAR_W, 0);
  2785. zend_compile_expr(&expr_node, expr_ast);
  2786. opline = zend_delayed_compile_end(offset);
  2787. opline->opcode = ZEND_ASSIGN_STATIC_PROP;
  2788. opline->result_type = IS_TMP_VAR;
  2789. result->op_type = IS_TMP_VAR;
  2790. zend_emit_op_data(&expr_node);
  2791. return;
  2792. case ZEND_AST_DIM:
  2793. offset = zend_delayed_compile_begin();
  2794. zend_delayed_compile_dim(result, var_ast, BP_VAR_W);
  2795. zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
  2796. opline = zend_delayed_compile_end(offset);
  2797. opline->opcode = ZEND_ASSIGN_DIM;
  2798. opline->result_type = IS_TMP_VAR;
  2799. result->op_type = IS_TMP_VAR;
  2800. opline = zend_emit_op_data(&expr_node);
  2801. return;
  2802. case ZEND_AST_PROP:
  2803. case ZEND_AST_NULLSAFE_PROP:
  2804. offset = zend_delayed_compile_begin();
  2805. zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
  2806. zend_compile_expr(&expr_node, expr_ast);
  2807. opline = zend_delayed_compile_end(offset);
  2808. opline->opcode = ZEND_ASSIGN_OBJ;
  2809. opline->result_type = IS_TMP_VAR;
  2810. result->op_type = IS_TMP_VAR;
  2811. zend_emit_op_data(&expr_node);
  2812. return;
  2813. case ZEND_AST_ARRAY:
  2814. if (zend_propagate_list_refs(var_ast)) {
  2815. if (!zend_is_variable_or_call(expr_ast)) {
  2816. zend_error_noreturn(E_COMPILE_ERROR,
  2817. "Cannot assign reference to non referenceable value");
  2818. }
  2819. zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
  2820. /* MAKE_REF is usually not necessary for CVs. However, if there are
  2821. * self-assignments, this forces the RHS to evaluate first. */
  2822. zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
  2823. } else {
  2824. if (expr_ast->kind == ZEND_AST_VAR) {
  2825. /* list($a, $b) = $a should evaluate the right $a first */
  2826. znode cv_node;
  2827. if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
  2828. zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
  2829. } else {
  2830. zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
  2831. }
  2832. } else {
  2833. zend_compile_expr(&expr_node, expr_ast);
  2834. }
  2835. }
  2836. zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
  2837. return;
  2838. EMPTY_SWITCH_DEFAULT_CASE();
  2839. }
  2840. }
  2841. /* }}} */
  2842. static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
  2843. {
  2844. zend_ast *target_ast = ast->child[0];
  2845. zend_ast *source_ast = ast->child[1];
  2846. znode target_node, source_node;
  2847. zend_op *opline;
  2848. uint32_t offset, flags;
  2849. if (is_this_fetch(target_ast)) {
  2850. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  2851. }
  2852. zend_ensure_writable_variable(target_ast);
  2853. if (zend_ast_is_short_circuited(source_ast)) {
  2854. zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
  2855. }
  2856. if (is_globals_fetch(source_ast)) {
  2857. zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
  2858. }
  2859. offset = zend_delayed_compile_begin();
  2860. zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, 1);
  2861. zend_compile_var(&source_node, source_ast, BP_VAR_W, 1);
  2862. if ((target_ast->kind != ZEND_AST_VAR
  2863. || target_ast->child[0]->kind != ZEND_AST_ZVAL)
  2864. && source_ast->kind != ZEND_AST_ZNODE
  2865. && source_node.op_type != IS_CV) {
  2866. /* Both LHS and RHS expressions may modify the same data structure,
  2867. * and the modification during RHS evaluation may dangle the pointer
  2868. * to the result of the LHS evaluation.
  2869. * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
  2870. * See: Bug #71539
  2871. */
  2872. zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
  2873. }
  2874. opline = zend_delayed_compile_end(offset);
  2875. if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
  2876. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
  2877. }
  2878. flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
  2879. if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
  2880. opline->opcode = ZEND_ASSIGN_OBJ_REF;
  2881. opline->extended_value &= ~ZEND_FETCH_REF;
  2882. opline->extended_value |= flags;
  2883. zend_emit_op_data(&source_node);
  2884. *result = target_node;
  2885. } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
  2886. opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
  2887. opline->extended_value &= ~ZEND_FETCH_REF;
  2888. opline->extended_value |= flags;
  2889. zend_emit_op_data(&source_node);
  2890. *result = target_node;
  2891. } else {
  2892. opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
  2893. opline->extended_value = flags;
  2894. }
  2895. }
  2896. /* }}} */
  2897. static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
  2898. {
  2899. znode dummy_node;
  2900. zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
  2901. zend_ast_create_znode(value_node));
  2902. zend_compile_expr(&dummy_node, assign_ast);
  2903. zend_do_free(&dummy_node);
  2904. }
  2905. /* }}} */
  2906. static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
  2907. {
  2908. zend_ast *var_ast = ast->child[0];
  2909. zend_ast *expr_ast = ast->child[1];
  2910. uint32_t opcode = ast->attr;
  2911. znode var_node, expr_node;
  2912. zend_op *opline;
  2913. uint32_t offset, cache_slot;
  2914. zend_ensure_writable_variable(var_ast);
  2915. /* Treat $GLOBALS['x'] assignment like assignment to variable. */
  2916. zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
  2917. switch (kind) {
  2918. case ZEND_AST_VAR:
  2919. offset = zend_delayed_compile_begin();
  2920. zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
  2921. zend_compile_expr(&expr_node, expr_ast);
  2922. zend_delayed_compile_end(offset);
  2923. opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
  2924. opline->extended_value = opcode;
  2925. return;
  2926. case ZEND_AST_STATIC_PROP:
  2927. offset = zend_delayed_compile_begin();
  2928. zend_delayed_compile_var(result, var_ast, BP_VAR_RW, 0);
  2929. zend_compile_expr(&expr_node, expr_ast);
  2930. opline = zend_delayed_compile_end(offset);
  2931. cache_slot = opline->extended_value;
  2932. opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
  2933. opline->extended_value = opcode;
  2934. opline->result_type = IS_TMP_VAR;
  2935. result->op_type = IS_TMP_VAR;
  2936. opline = zend_emit_op_data(&expr_node);
  2937. opline->extended_value = cache_slot;
  2938. return;
  2939. case ZEND_AST_DIM:
  2940. offset = zend_delayed_compile_begin();
  2941. zend_delayed_compile_dim(result, var_ast, BP_VAR_RW);
  2942. zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
  2943. opline = zend_delayed_compile_end(offset);
  2944. opline->opcode = ZEND_ASSIGN_DIM_OP;
  2945. opline->extended_value = opcode;
  2946. opline->result_type = IS_TMP_VAR;
  2947. result->op_type = IS_TMP_VAR;
  2948. zend_emit_op_data(&expr_node);
  2949. return;
  2950. case ZEND_AST_PROP:
  2951. case ZEND_AST_NULLSAFE_PROP:
  2952. offset = zend_delayed_compile_begin();
  2953. zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
  2954. zend_compile_expr(&expr_node, expr_ast);
  2955. opline = zend_delayed_compile_end(offset);
  2956. cache_slot = opline->extended_value;
  2957. opline->opcode = ZEND_ASSIGN_OBJ_OP;
  2958. opline->extended_value = opcode;
  2959. opline->result_type = IS_TMP_VAR;
  2960. result->op_type = IS_TMP_VAR;
  2961. opline = zend_emit_op_data(&expr_node);
  2962. opline->extended_value = cache_slot;
  2963. return;
  2964. EMPTY_SWITCH_DEFAULT_CASE()
  2965. }
  2966. }
  2967. /* }}} */
  2968. static uint32_t zend_get_arg_num(zend_function *fn, zend_string *arg_name) {
  2969. // TODO: Caching?
  2970. if (fn->type == ZEND_USER_FUNCTION) {
  2971. for (uint32_t i = 0; i < fn->common.num_args; i++) {
  2972. zend_arg_info *arg_info = &fn->op_array.arg_info[i];
  2973. if (zend_string_equals(arg_info->name, arg_name)) {
  2974. return i + 1;
  2975. }
  2976. }
  2977. } else {
  2978. for (uint32_t i = 0; i < fn->common.num_args; i++) {
  2979. zend_internal_arg_info *arg_info = &fn->internal_function.arg_info[i];
  2980. size_t len = strlen(arg_info->name);
  2981. if (len == ZSTR_LEN(arg_name) && !memcmp(arg_info->name, ZSTR_VAL(arg_name), len)) {
  2982. return i + 1;
  2983. }
  2984. }
  2985. }
  2986. /* Either an invalid argument name, or collected into a variadic argument. */
  2987. return (uint32_t) -1;
  2988. }
  2989. static uint32_t zend_compile_args(
  2990. zend_ast *ast, zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
  2991. {
  2992. zend_ast_list *args = zend_ast_get_list(ast);
  2993. uint32_t i;
  2994. bool uses_arg_unpack = 0;
  2995. uint32_t arg_count = 0; /* number of arguments not including unpacks */
  2996. /* Whether named arguments are used syntactically, to enforce language level limitations.
  2997. * May not actually use named argument passing. */
  2998. bool uses_named_args = 0;
  2999. /* Whether there may be any undef arguments due to the use of named arguments. */
  3000. bool may_have_undef = 0;
  3001. /* Whether there may be any extra named arguments collected into a variadic. */
  3002. *may_have_extra_named_args = 0;
  3003. for (i = 0; i < args->children; ++i) {
  3004. zend_ast *arg = args->child[i];
  3005. zend_string *arg_name = NULL;
  3006. uint32_t arg_num = i + 1;
  3007. znode arg_node;
  3008. zend_op *opline;
  3009. zend_uchar opcode;
  3010. if (arg->kind == ZEND_AST_UNPACK) {
  3011. if (uses_named_args) {
  3012. zend_error_noreturn(E_COMPILE_ERROR,
  3013. "Cannot use argument unpacking after named arguments");
  3014. }
  3015. uses_arg_unpack = 1;
  3016. fbc = NULL;
  3017. zend_compile_expr(&arg_node, arg->child[0]);
  3018. opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
  3019. opline->op2.num = arg_count;
  3020. opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
  3021. /* Unpack may contain named arguments. */
  3022. may_have_undef = 1;
  3023. if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
  3024. *may_have_extra_named_args = 1;
  3025. }
  3026. continue;
  3027. }
  3028. if (arg->kind == ZEND_AST_NAMED_ARG) {
  3029. uses_named_args = 1;
  3030. arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
  3031. arg = arg->child[1];
  3032. if (fbc && !uses_arg_unpack) {
  3033. arg_num = zend_get_arg_num(fbc, arg_name);
  3034. if (arg_num == arg_count + 1 && !may_have_undef) {
  3035. /* Using named arguments, but passing in order. */
  3036. arg_name = NULL;
  3037. arg_count++;
  3038. } else {
  3039. // TODO: We could track which arguments were passed, even if out of order.
  3040. may_have_undef = 1;
  3041. if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
  3042. *may_have_extra_named_args = 1;
  3043. }
  3044. }
  3045. } else {
  3046. arg_num = (uint32_t) -1;
  3047. may_have_undef = 1;
  3048. *may_have_extra_named_args = 1;
  3049. }
  3050. } else {
  3051. if (uses_arg_unpack) {
  3052. zend_error_noreturn(E_COMPILE_ERROR,
  3053. "Cannot use positional argument after argument unpacking");
  3054. }
  3055. if (uses_named_args) {
  3056. zend_error_noreturn(E_COMPILE_ERROR,
  3057. "Cannot use positional argument after named argument");
  3058. }
  3059. arg_count++;
  3060. }
  3061. /* Treat passing of $GLOBALS the same as passing a call.
  3062. * This will error at runtime if the argument is by-ref. */
  3063. if (zend_is_call(arg) || is_globals_fetch(arg)) {
  3064. zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
  3065. if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
  3066. /* Function call was converted into builtin instruction */
  3067. if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  3068. opcode = ZEND_SEND_VAL_EX;
  3069. } else {
  3070. opcode = ZEND_SEND_VAL;
  3071. }
  3072. } else {
  3073. if (fbc && arg_num != (uint32_t) -1) {
  3074. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  3075. opcode = ZEND_SEND_VAR_NO_REF;
  3076. } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
  3077. opcode = ZEND_SEND_VAL;
  3078. } else {
  3079. opcode = ZEND_SEND_VAR;
  3080. }
  3081. } else {
  3082. opcode = ZEND_SEND_VAR_NO_REF_EX;
  3083. }
  3084. }
  3085. } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
  3086. if (fbc && arg_num != (uint32_t) -1) {
  3087. if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
  3088. zend_compile_var(&arg_node, arg, BP_VAR_W, 1);
  3089. opcode = ZEND_SEND_REF;
  3090. } else {
  3091. zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
  3092. opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
  3093. }
  3094. } else {
  3095. do {
  3096. if (arg->kind == ZEND_AST_VAR) {
  3097. CG(zend_lineno) = zend_ast_get_lineno(ast);
  3098. if (is_this_fetch(arg)) {
  3099. zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
  3100. opcode = ZEND_SEND_VAR_EX;
  3101. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  3102. break;
  3103. } else if (zend_try_compile_cv(&arg_node, arg) == SUCCESS) {
  3104. opcode = ZEND_SEND_VAR_EX;
  3105. break;
  3106. }
  3107. }
  3108. opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
  3109. if (arg_name) {
  3110. opline->op2_type = IS_CONST;
  3111. zend_string_addref(arg_name);
  3112. opline->op2.constant = zend_add_literal_string(&arg_name);
  3113. opline->result.num = zend_alloc_cache_slots(2);
  3114. } else {
  3115. opline->op2.num = arg_num;
  3116. }
  3117. zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, 1);
  3118. opcode = ZEND_SEND_FUNC_ARG;
  3119. } while (0);
  3120. }
  3121. } else {
  3122. zend_compile_expr(&arg_node, arg);
  3123. if (arg_node.op_type == IS_VAR) {
  3124. /* pass ++$a or something similar */
  3125. if (fbc && arg_num != (uint32_t) -1) {
  3126. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  3127. opcode = ZEND_SEND_VAR_NO_REF;
  3128. } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
  3129. opcode = ZEND_SEND_VAL;
  3130. } else {
  3131. opcode = ZEND_SEND_VAR;
  3132. }
  3133. } else {
  3134. opcode = ZEND_SEND_VAR_NO_REF_EX;
  3135. }
  3136. } else if (arg_node.op_type == IS_CV) {
  3137. if (fbc && arg_num != (uint32_t) -1) {
  3138. if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
  3139. opcode = ZEND_SEND_REF;
  3140. } else {
  3141. opcode = ZEND_SEND_VAR;
  3142. }
  3143. } else {
  3144. opcode = ZEND_SEND_VAR_EX;
  3145. }
  3146. } else {
  3147. /* Delay "Only variables can be passed by reference" error to execution */
  3148. if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  3149. opcode = ZEND_SEND_VAL;
  3150. } else {
  3151. opcode = ZEND_SEND_VAL_EX;
  3152. }
  3153. }
  3154. }
  3155. opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
  3156. if (arg_name) {
  3157. opline->op2_type = IS_CONST;
  3158. zend_string_addref(arg_name);
  3159. opline->op2.constant = zend_add_literal_string(&arg_name);
  3160. opline->result.num = zend_alloc_cache_slots(2);
  3161. } else {
  3162. opline->op2.opline_num = arg_num;
  3163. opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
  3164. }
  3165. }
  3166. if (may_have_undef) {
  3167. zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
  3168. }
  3169. return arg_count;
  3170. }
  3171. /* }}} */
  3172. ZEND_API zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc) /* {{{ */
  3173. {
  3174. if (fbc) {
  3175. if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
  3176. if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
  3177. if (!(fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED))) {
  3178. return ZEND_DO_ICALL;
  3179. } else {
  3180. return ZEND_DO_FCALL_BY_NAME;
  3181. }
  3182. }
  3183. } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
  3184. if (zend_execute_ex == execute_ex && !(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
  3185. return ZEND_DO_UCALL;
  3186. }
  3187. }
  3188. } else if (zend_execute_ex == execute_ex &&
  3189. !zend_execute_internal &&
  3190. (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
  3191. init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
  3192. return ZEND_DO_FCALL_BY_NAME;
  3193. }
  3194. return ZEND_DO_FCALL;
  3195. }
  3196. /* }}} */
  3197. static bool zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc) /* {{{ */
  3198. {
  3199. zend_op *opline;
  3200. uint32_t opnum_init = get_next_op_number() - 1;
  3201. if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
  3202. opline = &CG(active_op_array)->opcodes[opnum_init];
  3203. opline->extended_value = 0;
  3204. if (opline->opcode == ZEND_NEW) {
  3205. zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
  3206. }
  3207. if (opline->opcode == ZEND_INIT_FCALL) {
  3208. opline->op1.num = zend_vm_calc_used_stack(0, fbc);
  3209. }
  3210. zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
  3211. return true;
  3212. }
  3213. bool may_have_extra_named_args;
  3214. uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
  3215. zend_do_extended_fcall_begin();
  3216. opline = &CG(active_op_array)->opcodes[opnum_init];
  3217. opline->extended_value = arg_count;
  3218. if (opline->opcode == ZEND_INIT_FCALL) {
  3219. opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
  3220. }
  3221. opline = zend_emit_op(result, zend_get_call_op(opline, fbc), NULL, NULL);
  3222. if (may_have_extra_named_args) {
  3223. opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
  3224. }
  3225. zend_do_extended_fcall_end();
  3226. return false;
  3227. }
  3228. /* }}} */
  3229. static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
  3230. {
  3231. zend_string *orig_name = zend_ast_get_str(name_ast);
  3232. bool is_fully_qualified;
  3233. name_node->op_type = IS_CONST;
  3234. ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
  3235. orig_name, name_ast->attr, &is_fully_qualified));
  3236. return !is_fully_qualified && FC(current_namespace);
  3237. }
  3238. /* }}} */
  3239. static void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
  3240. {
  3241. zend_op *opline = get_next_op();
  3242. opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
  3243. opline->op2_type = IS_CONST;
  3244. opline->op2.constant = zend_add_ns_func_name_literal(
  3245. Z_STR(name_node->u.constant));
  3246. opline->result.num = zend_alloc_cache_slot();
  3247. zend_compile_call_common(result, args_ast, NULL);
  3248. }
  3249. /* }}} */
  3250. static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
  3251. {
  3252. if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
  3253. const char *colon;
  3254. zend_string *str = Z_STR(name_node->u.constant);
  3255. if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
  3256. zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
  3257. zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
  3258. zend_op *opline = get_next_op();
  3259. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  3260. opline->op1_type = IS_CONST;
  3261. opline->op1.constant = zend_add_class_name_literal(class);
  3262. opline->op2_type = IS_CONST;
  3263. opline->op2.constant = zend_add_func_name_literal(method);
  3264. /* 2 slots, for class and method */
  3265. opline->result.num = zend_alloc_cache_slots(2);
  3266. zval_ptr_dtor(&name_node->u.constant);
  3267. } else {
  3268. zend_op *opline = get_next_op();
  3269. opline->opcode = ZEND_INIT_FCALL_BY_NAME;
  3270. opline->op2_type = IS_CONST;
  3271. opline->op2.constant = zend_add_func_name_literal(str);
  3272. opline->result.num = zend_alloc_cache_slot();
  3273. }
  3274. } else {
  3275. zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
  3276. }
  3277. zend_compile_call_common(result, args_ast, NULL);
  3278. }
  3279. /* }}} */
  3280. static inline bool zend_args_contain_unpack_or_named(zend_ast_list *args) /* {{{ */
  3281. {
  3282. uint32_t i;
  3283. for (i = 0; i < args->children; ++i) {
  3284. zend_ast *arg = args->child[i];
  3285. if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
  3286. return 1;
  3287. }
  3288. }
  3289. return 0;
  3290. }
  3291. /* }}} */
  3292. static zend_result zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
  3293. {
  3294. znode arg_node;
  3295. if (args->children != 1) {
  3296. return FAILURE;
  3297. }
  3298. zend_compile_expr(&arg_node, args->child[0]);
  3299. if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
  3300. result->op_type = IS_CONST;
  3301. ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
  3302. zval_ptr_dtor_str(&arg_node.u.constant);
  3303. } else {
  3304. zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
  3305. }
  3306. return SUCCESS;
  3307. }
  3308. /* }}} */
  3309. static zend_result zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
  3310. {
  3311. znode arg_node;
  3312. zend_op *opline;
  3313. if (args->children != 1) {
  3314. return FAILURE;
  3315. }
  3316. zend_compile_expr(&arg_node, args->child[0]);
  3317. opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
  3318. if (type != _IS_BOOL) {
  3319. opline->extended_value = (1 << type);
  3320. } else {
  3321. opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
  3322. }
  3323. return SUCCESS;
  3324. }
  3325. /* }}} */
  3326. static zend_result zend_compile_func_is_scalar(znode *result, zend_ast_list *args) /* {{{ */
  3327. {
  3328. znode arg_node;
  3329. zend_op *opline;
  3330. if (args->children != 1) {
  3331. return FAILURE;
  3332. }
  3333. zend_compile_expr(&arg_node, args->child[0]);
  3334. opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
  3335. opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
  3336. return SUCCESS;
  3337. }
  3338. static zend_result zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
  3339. {
  3340. znode arg_node;
  3341. zend_op *opline;
  3342. if (args->children != 1) {
  3343. return FAILURE;
  3344. }
  3345. zend_compile_expr(&arg_node, args->child[0]);
  3346. if (type == _IS_BOOL) {
  3347. opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
  3348. } else {
  3349. opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
  3350. opline->extended_value = type;
  3351. }
  3352. return SUCCESS;
  3353. }
  3354. /* }}} */
  3355. static zend_result zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
  3356. {
  3357. zend_string *name;
  3358. zend_op *opline;
  3359. if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
  3360. return FAILURE;
  3361. }
  3362. name = zval_get_string(zend_ast_get_zval(args->child[0]));
  3363. if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
  3364. zend_string_release_ex(name, 0);
  3365. return FAILURE;
  3366. }
  3367. if (zend_try_ct_eval_const(&result->u.constant, name, 0)) {
  3368. zend_string_release_ex(name, 0);
  3369. zval_ptr_dtor(&result->u.constant);
  3370. ZVAL_TRUE(&result->u.constant);
  3371. result->op_type = IS_CONST;
  3372. return SUCCESS;
  3373. }
  3374. opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
  3375. opline->op1_type = IS_CONST;
  3376. LITERAL_STR(opline->op1, name);
  3377. opline->extended_value = zend_alloc_cache_slot();
  3378. return SUCCESS;
  3379. }
  3380. /* }}} */
  3381. static zend_result zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
  3382. {
  3383. if (args->children == 1 &&
  3384. args->child[0]->kind == ZEND_AST_ZVAL &&
  3385. Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_LONG) {
  3386. zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;
  3387. result->op_type = IS_CONST;
  3388. ZVAL_CHAR(&result->u.constant, c);
  3389. return SUCCESS;
  3390. } else {
  3391. return FAILURE;
  3392. }
  3393. }
  3394. /* }}} */
  3395. static zend_result zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
  3396. {
  3397. if (args->children == 1 &&
  3398. args->child[0]->kind == ZEND_AST_ZVAL &&
  3399. Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
  3400. result->op_type = IS_CONST;
  3401. ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
  3402. return SUCCESS;
  3403. } else {
  3404. return FAILURE;
  3405. }
  3406. }
  3407. /* }}} */
  3408. /* We can only calculate the stack size for functions that have been fully compiled, otherwise
  3409. * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
  3410. * directly or indirectly recursive function calls. */
  3411. static bool fbc_is_finalized(zend_function *fbc) {
  3412. return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
  3413. }
  3414. static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
  3415. {
  3416. zend_string *name, *lcname;
  3417. zend_function *fbc;
  3418. zend_op *opline;
  3419. if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
  3420. return FAILURE;
  3421. }
  3422. name = zend_ast_get_str(name_ast);
  3423. lcname = zend_string_tolower(name);
  3424. fbc = zend_hash_find_ptr(CG(function_table), lcname);
  3425. if (!fbc || !fbc_is_finalized(fbc)
  3426. || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
  3427. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
  3428. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) && fbc->op_array.filename != CG(active_op_array)->filename)
  3429. ) {
  3430. zend_string_release_ex(lcname, 0);
  3431. return FAILURE;
  3432. }
  3433. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
  3434. opline->extended_value = num_args;
  3435. opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
  3436. opline->op2_type = IS_CONST;
  3437. LITERAL_STR(opline->op2, lcname);
  3438. opline->result.num = zend_alloc_cache_slot();
  3439. return SUCCESS;
  3440. }
  3441. /* }}} */
  3442. static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
  3443. {
  3444. zend_op *opline;
  3445. znode name_node;
  3446. if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
  3447. return;
  3448. }
  3449. zend_compile_expr(&name_node, name_ast);
  3450. opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
  3451. opline->op1_type = IS_CONST;
  3452. LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
  3453. opline->extended_value = num_args;
  3454. }
  3455. /* }}} */
  3456. /* cufa = call_user_func_array */
  3457. static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3458. {
  3459. znode arg_node;
  3460. zend_op *opline;
  3461. if (args->children != 2) {
  3462. return FAILURE;
  3463. }
  3464. zend_compile_init_user_func(args->child[0], 0, lcname);
  3465. if (args->child[1]->kind == ZEND_AST_CALL
  3466. && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
  3467. && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
  3468. && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
  3469. zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
  3470. zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
  3471. bool is_fully_qualified;
  3472. zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
  3473. if (zend_string_equals_literal_ci(name, "array_slice")
  3474. && !zend_args_contain_unpack_or_named(list)
  3475. && list->children == 3
  3476. && list->child[1]->kind == ZEND_AST_ZVAL) {
  3477. zval *zv = zend_ast_get_zval(list->child[1]);
  3478. if (Z_TYPE_P(zv) == IS_LONG
  3479. && Z_LVAL_P(zv) >= 0
  3480. && Z_LVAL_P(zv) <= 0x7fffffff) {
  3481. zend_op *opline;
  3482. znode len_node;
  3483. zend_compile_expr(&arg_node, list->child[0]);
  3484. zend_compile_expr(&len_node, list->child[2]);
  3485. opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
  3486. opline->extended_value = Z_LVAL_P(zv);
  3487. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3488. zend_string_release_ex(name, 0);
  3489. return SUCCESS;
  3490. }
  3491. }
  3492. zend_string_release_ex(name, 0);
  3493. }
  3494. zend_compile_expr(&arg_node, args->child[1]);
  3495. zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
  3496. zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
  3497. opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3498. opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
  3499. return SUCCESS;
  3500. }
  3501. /* }}} */
  3502. /* cuf = call_user_func */
  3503. static zend_result zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3504. {
  3505. uint32_t i;
  3506. if (args->children < 1) {
  3507. return FAILURE;
  3508. }
  3509. zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
  3510. for (i = 1; i < args->children; ++i) {
  3511. zend_ast *arg_ast = args->child[i];
  3512. znode arg_node;
  3513. zend_op *opline;
  3514. zend_compile_expr(&arg_node, arg_ast);
  3515. opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
  3516. opline->op2.num = i;
  3517. opline->result.var = EX_NUM_TO_VAR(i - 1);
  3518. }
  3519. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3520. return SUCCESS;
  3521. }
  3522. /* }}} */
  3523. static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
  3524. {
  3525. if (EG(assertions) >= 0) {
  3526. znode name_node;
  3527. zend_op *opline;
  3528. uint32_t check_op_number = get_next_op_number();
  3529. zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
  3530. if (fbc && fbc_is_finalized(fbc)) {
  3531. name_node.op_type = IS_CONST;
  3532. ZVAL_STR_COPY(&name_node.u.constant, name);
  3533. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
  3534. } else {
  3535. opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
  3536. opline->op2_type = IS_CONST;
  3537. opline->op2.constant = zend_add_ns_func_name_literal(name);
  3538. }
  3539. opline->result.num = zend_alloc_cache_slot();
  3540. if (args->children == 1) {
  3541. /* add "assert(condition) as assertion message */
  3542. zend_ast *arg = zend_ast_create_zval_from_str(
  3543. zend_ast_export("assert(", args->child[0], ")"));
  3544. if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
  3545. /* If the original argument was named, add the new argument as named as well,
  3546. * as mixing named and positional is not allowed. */
  3547. zend_ast *name = zend_ast_create_zval_from_str(
  3548. zend_string_init("description", sizeof("description") - 1, 0));
  3549. arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
  3550. }
  3551. zend_ast_list_add((zend_ast *) args, arg);
  3552. }
  3553. zend_compile_call_common(result, (zend_ast*)args, fbc);
  3554. opline = &CG(active_op_array)->opcodes[check_op_number];
  3555. opline->op2.opline_num = get_next_op_number();
  3556. SET_NODE(opline->result, result);
  3557. } else {
  3558. if (!fbc) {
  3559. zend_string_release_ex(name, 0);
  3560. }
  3561. result->op_type = IS_CONST;
  3562. ZVAL_TRUE(&result->u.constant);
  3563. }
  3564. }
  3565. /* }}} */
  3566. static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
  3567. {
  3568. bool strict = 0;
  3569. znode array, needly;
  3570. zend_op *opline;
  3571. if (args->children == 3) {
  3572. if (args->child[2]->kind == ZEND_AST_ZVAL) {
  3573. strict = zend_is_true(zend_ast_get_zval(args->child[2]));
  3574. } else if (args->child[2]->kind == ZEND_AST_CONST) {
  3575. zval value;
  3576. zend_ast *name_ast = args->child[2]->child[0];
  3577. bool is_fully_qualified;
  3578. zend_string *resolved_name = zend_resolve_const_name(
  3579. zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
  3580. if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
  3581. zend_string_release_ex(resolved_name, 0);
  3582. return FAILURE;
  3583. }
  3584. zend_string_release_ex(resolved_name, 0);
  3585. strict = zend_is_true(&value);
  3586. zval_ptr_dtor(&value);
  3587. } else {
  3588. return FAILURE;
  3589. }
  3590. } else if (args->children != 2) {
  3591. return FAILURE;
  3592. }
  3593. if (args->child[1]->kind != ZEND_AST_ARRAY
  3594. || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
  3595. return FAILURE;
  3596. }
  3597. if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
  3598. bool ok = 1;
  3599. zval *val, tmp;
  3600. HashTable *src = Z_ARRVAL(array.u.constant);
  3601. HashTable *dst = zend_new_array(zend_hash_num_elements(src));
  3602. ZVAL_TRUE(&tmp);
  3603. if (strict) {
  3604. ZEND_HASH_FOREACH_VAL(src, val) {
  3605. if (Z_TYPE_P(val) == IS_STRING) {
  3606. zend_hash_add(dst, Z_STR_P(val), &tmp);
  3607. } else if (Z_TYPE_P(val) == IS_LONG) {
  3608. zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
  3609. } else {
  3610. zend_array_destroy(dst);
  3611. ok = 0;
  3612. break;
  3613. }
  3614. } ZEND_HASH_FOREACH_END();
  3615. } else {
  3616. ZEND_HASH_FOREACH_VAL(src, val) {
  3617. if (Z_TYPE_P(val) != IS_STRING
  3618. || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
  3619. zend_array_destroy(dst);
  3620. ok = 0;
  3621. break;
  3622. }
  3623. zend_hash_add(dst, Z_STR_P(val), &tmp);
  3624. } ZEND_HASH_FOREACH_END();
  3625. }
  3626. zend_array_destroy(src);
  3627. if (!ok) {
  3628. return FAILURE;
  3629. }
  3630. Z_ARRVAL(array.u.constant) = dst;
  3631. }
  3632. array.op_type = IS_CONST;
  3633. zend_compile_expr(&needly, args->child[0]);
  3634. opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
  3635. opline->extended_value = strict;
  3636. return SUCCESS;
  3637. }
  3638. /* }}} */
  3639. static zend_result zend_compile_func_count(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3640. {
  3641. znode arg_node;
  3642. zend_op *opline;
  3643. if (args->children != 1) {
  3644. return FAILURE;
  3645. }
  3646. zend_compile_expr(&arg_node, args->child[0]);
  3647. opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
  3648. opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
  3649. return SUCCESS;
  3650. }
  3651. /* }}} */
  3652. static zend_result zend_compile_func_get_class(znode *result, zend_ast_list *args) /* {{{ */
  3653. {
  3654. if (args->children == 0) {
  3655. zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
  3656. } else {
  3657. znode arg_node;
  3658. if (args->children != 1) {
  3659. return FAILURE;
  3660. }
  3661. zend_compile_expr(&arg_node, args->child[0]);
  3662. zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
  3663. }
  3664. return SUCCESS;
  3665. }
  3666. /* }}} */
  3667. static zend_result zend_compile_func_get_called_class(znode *result, zend_ast_list *args) /* {{{ */
  3668. {
  3669. if (args->children != 0) {
  3670. return FAILURE;
  3671. }
  3672. zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
  3673. return SUCCESS;
  3674. }
  3675. /* }}} */
  3676. static zend_result zend_compile_func_gettype(znode *result, zend_ast_list *args) /* {{{ */
  3677. {
  3678. znode arg_node;
  3679. if (args->children != 1) {
  3680. return FAILURE;
  3681. }
  3682. zend_compile_expr(&arg_node, args->child[0]);
  3683. zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
  3684. return SUCCESS;
  3685. }
  3686. /* }}} */
  3687. static zend_result zend_compile_func_num_args(znode *result, zend_ast_list *args) /* {{{ */
  3688. {
  3689. if (CG(active_op_array)->function_name && args->children == 0) {
  3690. zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
  3691. return SUCCESS;
  3692. } else {
  3693. return FAILURE;
  3694. }
  3695. }
  3696. /* }}} */
  3697. static zend_result zend_compile_func_get_args(znode *result, zend_ast_list *args) /* {{{ */
  3698. {
  3699. if (CG(active_op_array)->function_name && args->children == 0) {
  3700. zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
  3701. return SUCCESS;
  3702. } else {
  3703. return FAILURE;
  3704. }
  3705. }
  3706. /* }}} */
  3707. static zend_result zend_compile_func_array_key_exists(znode *result, zend_ast_list *args) /* {{{ */
  3708. {
  3709. znode subject, needle;
  3710. if (args->children != 2) {
  3711. return FAILURE;
  3712. }
  3713. zend_compile_expr(&needle, args->child[0]);
  3714. zend_compile_expr(&subject, args->child[1]);
  3715. zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
  3716. return SUCCESS;
  3717. }
  3718. /* }}} */
  3719. static zend_result zend_compile_func_array_slice(znode *result, zend_ast_list *args) /* {{{ */
  3720. {
  3721. if (CG(active_op_array)->function_name
  3722. && args->children == 2
  3723. && args->child[0]->kind == ZEND_AST_CALL
  3724. && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
  3725. && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
  3726. && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
  3727. && args->child[1]->kind == ZEND_AST_ZVAL) {
  3728. zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
  3729. bool is_fully_qualified;
  3730. zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
  3731. zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
  3732. zval *zv = zend_ast_get_zval(args->child[1]);
  3733. znode first;
  3734. if (zend_string_equals_literal_ci(name, "func_get_args")
  3735. && list->children == 0
  3736. && Z_TYPE_P(zv) == IS_LONG
  3737. && Z_LVAL_P(zv) >= 0) {
  3738. first.op_type = IS_CONST;
  3739. ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
  3740. zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
  3741. zend_string_release_ex(name, 0);
  3742. return SUCCESS;
  3743. }
  3744. zend_string_release_ex(name, 0);
  3745. }
  3746. return FAILURE;
  3747. }
  3748. /* }}} */
  3749. static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
  3750. {
  3751. if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
  3752. return FAILURE;
  3753. }
  3754. if (fbc->type != ZEND_INTERNAL_FUNCTION) {
  3755. /* If the function is part of disabled_functions, it may be redeclared as a userland
  3756. * function with a different implementation. Don't use the VM builtin in that case. */
  3757. return FAILURE;
  3758. }
  3759. if (zend_args_contain_unpack_or_named(args)) {
  3760. return FAILURE;
  3761. }
  3762. if (zend_string_equals_literal(lcname, "strlen")) {
  3763. return zend_compile_func_strlen(result, args);
  3764. } else if (zend_string_equals_literal(lcname, "is_null")) {
  3765. return zend_compile_func_typecheck(result, args, IS_NULL);
  3766. } else if (zend_string_equals_literal(lcname, "is_bool")) {
  3767. return zend_compile_func_typecheck(result, args, _IS_BOOL);
  3768. } else if (zend_string_equals_literal(lcname, "is_long")
  3769. || zend_string_equals_literal(lcname, "is_int")
  3770. || zend_string_equals_literal(lcname, "is_integer")
  3771. ) {
  3772. return zend_compile_func_typecheck(result, args, IS_LONG);
  3773. } else if (zend_string_equals_literal(lcname, "is_float")
  3774. || zend_string_equals_literal(lcname, "is_double")
  3775. ) {
  3776. return zend_compile_func_typecheck(result, args, IS_DOUBLE);
  3777. } else if (zend_string_equals_literal(lcname, "is_string")) {
  3778. return zend_compile_func_typecheck(result, args, IS_STRING);
  3779. } else if (zend_string_equals_literal(lcname, "is_array")) {
  3780. return zend_compile_func_typecheck(result, args, IS_ARRAY);
  3781. } else if (zend_string_equals_literal(lcname, "is_object")) {
  3782. return zend_compile_func_typecheck(result, args, IS_OBJECT);
  3783. } else if (zend_string_equals_literal(lcname, "is_resource")) {
  3784. return zend_compile_func_typecheck(result, args, IS_RESOURCE);
  3785. } else if (zend_string_equals_literal(lcname, "is_scalar")) {
  3786. return zend_compile_func_is_scalar(result, args);
  3787. } else if (zend_string_equals_literal(lcname, "boolval")) {
  3788. return zend_compile_func_cast(result, args, _IS_BOOL);
  3789. } else if (zend_string_equals_literal(lcname, "intval")) {
  3790. return zend_compile_func_cast(result, args, IS_LONG);
  3791. } else if (zend_string_equals_literal(lcname, "floatval")
  3792. || zend_string_equals_literal(lcname, "doubleval")
  3793. ) {
  3794. return zend_compile_func_cast(result, args, IS_DOUBLE);
  3795. } else if (zend_string_equals_literal(lcname, "strval")) {
  3796. return zend_compile_func_cast(result, args, IS_STRING);
  3797. } else if (zend_string_equals_literal(lcname, "defined")) {
  3798. return zend_compile_func_defined(result, args);
  3799. } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
  3800. return zend_compile_func_chr(result, args);
  3801. } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
  3802. return zend_compile_func_ord(result, args);
  3803. } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
  3804. return zend_compile_func_cufa(result, args, lcname);
  3805. } else if (zend_string_equals_literal(lcname, "call_user_func")) {
  3806. return zend_compile_func_cuf(result, args, lcname);
  3807. } else if (zend_string_equals_literal(lcname, "in_array")) {
  3808. return zend_compile_func_in_array(result, args);
  3809. } else if (zend_string_equals_literal(lcname, "count")
  3810. || zend_string_equals_literal(lcname, "sizeof")) {
  3811. return zend_compile_func_count(result, args, lcname);
  3812. } else if (zend_string_equals_literal(lcname, "get_class")) {
  3813. return zend_compile_func_get_class(result, args);
  3814. } else if (zend_string_equals_literal(lcname, "get_called_class")) {
  3815. return zend_compile_func_get_called_class(result, args);
  3816. } else if (zend_string_equals_literal(lcname, "gettype")) {
  3817. return zend_compile_func_gettype(result, args);
  3818. } else if (zend_string_equals_literal(lcname, "func_num_args")) {
  3819. return zend_compile_func_num_args(result, args);
  3820. } else if (zend_string_equals_literal(lcname, "func_get_args")) {
  3821. return zend_compile_func_get_args(result, args);
  3822. } else if (zend_string_equals_literal(lcname, "array_slice")) {
  3823. return zend_compile_func_array_slice(result, args);
  3824. } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
  3825. return zend_compile_func_array_key_exists(result, args);
  3826. } else {
  3827. return FAILURE;
  3828. }
  3829. }
  3830. /* }}} */
  3831. static void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3832. {
  3833. zend_ast *name_ast = ast->child[0];
  3834. zend_ast *args_ast = ast->child[1];
  3835. bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
  3836. znode name_node;
  3837. if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
  3838. zend_compile_expr(&name_node, name_ast);
  3839. zend_compile_dynamic_call(result, &name_node, args_ast);
  3840. return;
  3841. }
  3842. {
  3843. bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
  3844. if (runtime_resolution) {
  3845. if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
  3846. && !is_callable_convert) {
  3847. zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
  3848. } else {
  3849. zend_compile_ns_call(result, &name_node, args_ast);
  3850. }
  3851. return;
  3852. }
  3853. }
  3854. {
  3855. zval *name = &name_node.u.constant;
  3856. zend_string *lcname;
  3857. zend_function *fbc;
  3858. zend_op *opline;
  3859. lcname = zend_string_tolower(Z_STR_P(name));
  3860. fbc = zend_hash_find_ptr(CG(function_table), lcname);
  3861. /* Special assert() handling should apply independently of compiler flags. */
  3862. if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
  3863. zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc);
  3864. zend_string_release(lcname);
  3865. zval_ptr_dtor(&name_node.u.constant);
  3866. return;
  3867. }
  3868. if (!fbc || !fbc_is_finalized(fbc)
  3869. || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
  3870. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
  3871. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) && fbc->op_array.filename != CG(active_op_array)->filename)
  3872. ) {
  3873. zend_string_release_ex(lcname, 0);
  3874. zend_compile_dynamic_call(result, &name_node, args_ast);
  3875. return;
  3876. }
  3877. if (!is_callable_convert &&
  3878. zend_try_compile_special_func(result, lcname,
  3879. zend_ast_get_list(args_ast), fbc, type) == SUCCESS
  3880. ) {
  3881. zend_string_release_ex(lcname, 0);
  3882. zval_ptr_dtor(&name_node.u.constant);
  3883. return;
  3884. }
  3885. zval_ptr_dtor(&name_node.u.constant);
  3886. ZVAL_NEW_STR(&name_node.u.constant, lcname);
  3887. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
  3888. opline->result.num = zend_alloc_cache_slot();
  3889. zend_compile_call_common(result, args_ast, fbc);
  3890. }
  3891. }
  3892. /* }}} */
  3893. static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3894. {
  3895. zend_ast *obj_ast = ast->child[0];
  3896. zend_ast *method_ast = ast->child[1];
  3897. zend_ast *args_ast = ast->child[2];
  3898. znode obj_node, method_node;
  3899. zend_op *opline;
  3900. zend_function *fbc = NULL;
  3901. bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
  3902. uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
  3903. if (is_this_fetch(obj_ast)) {
  3904. if (this_guaranteed_exists()) {
  3905. obj_node.op_type = IS_UNUSED;
  3906. } else {
  3907. zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
  3908. }
  3909. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  3910. /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
  3911. * check for a nullsafe access. */
  3912. } else {
  3913. zend_short_circuiting_mark_inner(obj_ast);
  3914. zend_compile_expr(&obj_node, obj_ast);
  3915. if (nullsafe) {
  3916. zend_emit_jmp_null(&obj_node);
  3917. }
  3918. }
  3919. zend_compile_expr(&method_node, method_ast);
  3920. opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
  3921. if (method_node.op_type == IS_CONST) {
  3922. if (Z_TYPE(method_node.u.constant) != IS_STRING) {
  3923. zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
  3924. }
  3925. opline->op2_type = IS_CONST;
  3926. opline->op2.constant = zend_add_func_name_literal(
  3927. Z_STR(method_node.u.constant));
  3928. opline->result.num = zend_alloc_cache_slots(2);
  3929. } else {
  3930. SET_NODE(opline->op2, &method_node);
  3931. }
  3932. /* Check if this calls a known method on $this */
  3933. if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
  3934. CG(active_class_entry) && zend_is_scope_known()) {
  3935. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
  3936. fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
  3937. /* We only know the exact method that is being called if it is either private or final.
  3938. * Otherwise an overriding method in a child class may be called. */
  3939. if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
  3940. fbc = NULL;
  3941. }
  3942. }
  3943. if (zend_compile_call_common(result, args_ast, fbc)) {
  3944. if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
  3945. zend_error_noreturn(E_COMPILE_ERROR,
  3946. "Cannot combine nullsafe operator with Closure creation");
  3947. }
  3948. }
  3949. }
  3950. /* }}} */
  3951. static bool zend_is_constructor(zend_string *name) /* {{{ */
  3952. {
  3953. return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
  3954. }
  3955. /* }}} */
  3956. static zend_function *zend_get_compatible_func_or_null(zend_class_entry *ce, zend_string *lcname) /* {{{ */
  3957. {
  3958. zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
  3959. if (!fbc || (fbc->common.fn_flags & ZEND_ACC_PUBLIC) || ce == CG(active_class_entry)) {
  3960. return fbc;
  3961. }
  3962. if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
  3963. && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
  3964. && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
  3965. && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
  3966. return fbc;
  3967. }
  3968. return NULL;
  3969. }
  3970. /* }}} */
  3971. static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3972. {
  3973. zend_ast *class_ast = ast->child[0];
  3974. zend_ast *method_ast = ast->child[1];
  3975. zend_ast *args_ast = ast->child[2];
  3976. znode class_node, method_node;
  3977. zend_op *opline;
  3978. zend_function *fbc = NULL;
  3979. zend_short_circuiting_mark_inner(class_ast);
  3980. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  3981. zend_compile_expr(&method_node, method_ast);
  3982. if (method_node.op_type == IS_CONST) {
  3983. zval *name = &method_node.u.constant;
  3984. if (Z_TYPE_P(name) != IS_STRING) {
  3985. zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
  3986. }
  3987. if (zend_is_constructor(Z_STR_P(name))) {
  3988. zval_ptr_dtor(name);
  3989. method_node.op_type = IS_UNUSED;
  3990. }
  3991. }
  3992. opline = get_next_op();
  3993. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  3994. zend_set_class_name_op1(opline, &class_node);
  3995. if (method_node.op_type == IS_CONST) {
  3996. opline->op2_type = IS_CONST;
  3997. opline->op2.constant = zend_add_func_name_literal(
  3998. Z_STR(method_node.u.constant));
  3999. opline->result.num = zend_alloc_cache_slots(2);
  4000. } else {
  4001. if (opline->op1_type == IS_CONST) {
  4002. opline->result.num = zend_alloc_cache_slot();
  4003. }
  4004. SET_NODE(opline->op2, &method_node);
  4005. }
  4006. /* Check if we already know which method we're calling */
  4007. if (opline->op2_type == IS_CONST) {
  4008. zend_class_entry *ce = NULL;
  4009. if (opline->op1_type == IS_CONST) {
  4010. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
  4011. ce = zend_hash_find_ptr(CG(class_table), lcname);
  4012. if (!ce && CG(active_class_entry)
  4013. && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
  4014. ce = CG(active_class_entry);
  4015. }
  4016. } else if (opline->op1_type == IS_UNUSED
  4017. && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
  4018. && zend_is_scope_known()) {
  4019. ce = CG(active_class_entry);
  4020. }
  4021. if (ce) {
  4022. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
  4023. fbc = zend_get_compatible_func_or_null(ce, lcname);
  4024. }
  4025. }
  4026. zend_compile_call_common(result, args_ast, fbc);
  4027. }
  4028. /* }}} */
  4029. static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel);
  4030. static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
  4031. {
  4032. zend_ast *class_ast = ast->child[0];
  4033. zend_ast *args_ast = ast->child[1];
  4034. znode class_node, ctor_result;
  4035. zend_op *opline;
  4036. if (class_ast->kind == ZEND_AST_CLASS) {
  4037. /* anon class declaration */
  4038. zend_compile_class_decl(&class_node, class_ast, 0);
  4039. } else {
  4040. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  4041. }
  4042. opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
  4043. if (class_node.op_type == IS_CONST) {
  4044. opline->op1_type = IS_CONST;
  4045. opline->op1.constant = zend_add_class_name_literal(
  4046. Z_STR(class_node.u.constant));
  4047. opline->op2.num = zend_alloc_cache_slot();
  4048. } else {
  4049. SET_NODE(opline->op1, &class_node);
  4050. }
  4051. zend_compile_call_common(&ctor_result, args_ast, NULL);
  4052. zend_do_free(&ctor_result);
  4053. }
  4054. /* }}} */
  4055. static void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
  4056. {
  4057. zend_ast *obj_ast = ast->child[0];
  4058. znode obj_node;
  4059. zend_compile_expr(&obj_node, obj_ast);
  4060. zend_emit_op_tmp(result, ZEND_CLONE, &obj_node, NULL);
  4061. }
  4062. /* }}} */
  4063. static void zend_compile_global_var(zend_ast *ast) /* {{{ */
  4064. {
  4065. zend_ast *var_ast = ast->child[0];
  4066. zend_ast *name_ast = var_ast->child[0];
  4067. znode name_node, result;
  4068. zend_compile_expr(&name_node, name_ast);
  4069. if (name_node.op_type == IS_CONST) {
  4070. convert_to_string(&name_node.u.constant);
  4071. }
  4072. // TODO(GLOBALS) Forbid "global $GLOBALS"?
  4073. if (is_this_fetch(var_ast)) {
  4074. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
  4075. } else if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
  4076. zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
  4077. opline->extended_value = zend_alloc_cache_slot();
  4078. } else {
  4079. /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
  4080. * to not free the name_node operand, so it can be reused in the following
  4081. * ASSIGN_REF, which then frees it. */
  4082. zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
  4083. opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
  4084. if (name_node.op_type == IS_CONST) {
  4085. zend_string_addref(Z_STR(name_node.u.constant));
  4086. }
  4087. zend_emit_assign_ref_znode(
  4088. zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
  4089. &result
  4090. );
  4091. }
  4092. }
  4093. /* }}} */
  4094. static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
  4095. {
  4096. zend_op *opline;
  4097. if (!CG(active_op_array)->static_variables) {
  4098. if (CG(active_op_array)->scope) {
  4099. CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
  4100. }
  4101. CG(active_op_array)->static_variables = zend_new_array(8);
  4102. }
  4103. value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
  4104. if (zend_string_equals_literal(var_name, "this")) {
  4105. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
  4106. }
  4107. opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
  4108. opline->op1_type = IS_CV;
  4109. opline->op1.var = lookup_cv(var_name);
  4110. opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
  4111. }
  4112. /* }}} */
  4113. static void zend_compile_static_var(zend_ast *ast) /* {{{ */
  4114. {
  4115. zend_ast *var_ast = ast->child[0];
  4116. zend_ast **value_ast_ptr = &ast->child[1];
  4117. zval value_zv;
  4118. if (*value_ast_ptr) {
  4119. zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ true);
  4120. } else {
  4121. ZVAL_NULL(&value_zv);
  4122. }
  4123. zend_compile_static_var_common(zend_ast_get_str(var_ast), &value_zv, ZEND_BIND_REF);
  4124. }
  4125. /* }}} */
  4126. static void zend_compile_unset(zend_ast *ast) /* {{{ */
  4127. {
  4128. zend_ast *var_ast = ast->child[0];
  4129. znode var_node;
  4130. zend_op *opline;
  4131. zend_ensure_writable_variable(var_ast);
  4132. if (is_global_var_fetch(var_ast)) {
  4133. if (!var_ast->child[1]) {
  4134. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
  4135. }
  4136. zend_compile_expr(&var_node, var_ast->child[1]);
  4137. if (var_node.op_type == IS_CONST) {
  4138. convert_to_string(&var_node.u.constant);
  4139. }
  4140. opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
  4141. opline->extended_value = ZEND_FETCH_GLOBAL;
  4142. return;
  4143. }
  4144. switch (var_ast->kind) {
  4145. case ZEND_AST_VAR:
  4146. if (is_this_fetch(var_ast)) {
  4147. zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
  4148. } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
  4149. opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
  4150. } else {
  4151. opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
  4152. opline->opcode = ZEND_UNSET_VAR;
  4153. }
  4154. return;
  4155. case ZEND_AST_DIM:
  4156. opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET);
  4157. opline->opcode = ZEND_UNSET_DIM;
  4158. return;
  4159. case ZEND_AST_PROP:
  4160. case ZEND_AST_NULLSAFE_PROP:
  4161. opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, 0);
  4162. opline->opcode = ZEND_UNSET_OBJ;
  4163. return;
  4164. case ZEND_AST_STATIC_PROP:
  4165. opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, 0, 0);
  4166. opline->opcode = ZEND_UNSET_STATIC_PROP;
  4167. return;
  4168. EMPTY_SWITCH_DEFAULT_CASE()
  4169. }
  4170. }
  4171. /* }}} */
  4172. static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
  4173. {
  4174. zend_loop_var *base;
  4175. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  4176. if (!loop_var) {
  4177. return 1;
  4178. }
  4179. base = zend_stack_base(&CG(loop_var_stack));
  4180. for (; loop_var >= base; loop_var--) {
  4181. if (loop_var->opcode == ZEND_FAST_CALL) {
  4182. zend_op *opline = get_next_op();
  4183. opline->opcode = ZEND_FAST_CALL;
  4184. opline->result_type = IS_TMP_VAR;
  4185. opline->result.var = loop_var->var_num;
  4186. if (return_value) {
  4187. SET_NODE(opline->op2, return_value);
  4188. }
  4189. opline->op1.num = loop_var->try_catch_offset;
  4190. } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
  4191. zend_op *opline = get_next_op();
  4192. opline->opcode = ZEND_DISCARD_EXCEPTION;
  4193. opline->op1_type = IS_TMP_VAR;
  4194. opline->op1.var = loop_var->var_num;
  4195. } else if (loop_var->opcode == ZEND_RETURN) {
  4196. /* Stack separator */
  4197. break;
  4198. } else if (depth <= 1) {
  4199. return 1;
  4200. } else if (loop_var->opcode == ZEND_NOP) {
  4201. /* Loop doesn't have freeable variable */
  4202. depth--;
  4203. } else {
  4204. zend_op *opline;
  4205. ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
  4206. opline = get_next_op();
  4207. opline->opcode = loop_var->opcode;
  4208. opline->op1_type = loop_var->var_type;
  4209. opline->op1.var = loop_var->var_num;
  4210. opline->extended_value = ZEND_FREE_ON_RETURN;
  4211. depth--;
  4212. }
  4213. }
  4214. return (depth == 0);
  4215. }
  4216. /* }}} */
  4217. static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
  4218. {
  4219. return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
  4220. }
  4221. /* }}} */
  4222. static bool zend_has_finally_ex(zend_long depth) /* {{{ */
  4223. {
  4224. zend_loop_var *base;
  4225. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  4226. if (!loop_var) {
  4227. return 0;
  4228. }
  4229. base = zend_stack_base(&CG(loop_var_stack));
  4230. for (; loop_var >= base; loop_var--) {
  4231. if (loop_var->opcode == ZEND_FAST_CALL) {
  4232. return 1;
  4233. } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
  4234. } else if (loop_var->opcode == ZEND_RETURN) {
  4235. /* Stack separator */
  4236. return 0;
  4237. } else if (depth <= 1) {
  4238. return 0;
  4239. } else {
  4240. depth--;
  4241. }
  4242. }
  4243. return 0;
  4244. }
  4245. /* }}} */
  4246. static bool zend_has_finally(void) /* {{{ */
  4247. {
  4248. return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
  4249. }
  4250. /* }}} */
  4251. static void zend_compile_return(zend_ast *ast) /* {{{ */
  4252. {
  4253. zend_ast *expr_ast = ast->child[0];
  4254. bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
  4255. bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  4256. znode expr_node;
  4257. zend_op *opline;
  4258. if (is_generator) {
  4259. /* For generators the by-ref flag refers to yields, not returns */
  4260. by_ref = 0;
  4261. }
  4262. if (!expr_ast) {
  4263. expr_node.op_type = IS_CONST;
  4264. ZVAL_NULL(&expr_node.u.constant);
  4265. } else if (by_ref && zend_is_variable(expr_ast)) {
  4266. if (zend_ast_is_short_circuited(expr_ast)) {
  4267. zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
  4268. }
  4269. zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
  4270. } else {
  4271. zend_compile_expr(&expr_node, expr_ast);
  4272. }
  4273. if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
  4274. && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
  4275. && zend_has_finally()) {
  4276. /* Copy return value into temporary VAR to avoid modification in finally code */
  4277. if (by_ref) {
  4278. zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
  4279. } else {
  4280. zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
  4281. }
  4282. }
  4283. /* Generator return types are handled separately */
  4284. if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  4285. zend_emit_return_type_check(
  4286. expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
  4287. }
  4288. zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
  4289. opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
  4290. &expr_node, NULL);
  4291. if (by_ref && expr_ast) {
  4292. if (zend_is_call(expr_ast)) {
  4293. opline->extended_value = ZEND_RETURNS_FUNCTION;
  4294. } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
  4295. opline->extended_value = ZEND_RETURNS_VALUE;
  4296. }
  4297. }
  4298. }
  4299. /* }}} */
  4300. static void zend_compile_echo(zend_ast *ast) /* {{{ */
  4301. {
  4302. zend_op *opline;
  4303. zend_ast *expr_ast = ast->child[0];
  4304. znode expr_node;
  4305. zend_compile_expr(&expr_node, expr_ast);
  4306. opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
  4307. opline->extended_value = 0;
  4308. }
  4309. /* }}} */
  4310. static void zend_compile_throw(znode *result, zend_ast *ast) /* {{{ */
  4311. {
  4312. zend_ast *expr_ast = ast->child[0];
  4313. znode expr_node;
  4314. zend_compile_expr(&expr_node, expr_ast);
  4315. zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
  4316. if (result) {
  4317. /* Mark this as an "expression throw" for opcache. */
  4318. opline->extended_value = ZEND_THROW_IS_EXPR;
  4319. result->op_type = IS_CONST;
  4320. ZVAL_TRUE(&result->u.constant);
  4321. }
  4322. }
  4323. /* }}} */
  4324. static void zend_compile_break_continue(zend_ast *ast) /* {{{ */
  4325. {
  4326. zend_ast *depth_ast = ast->child[0];
  4327. zend_op *opline;
  4328. zend_long depth;
  4329. ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
  4330. if (depth_ast) {
  4331. zval *depth_zv;
  4332. if (depth_ast->kind != ZEND_AST_ZVAL) {
  4333. zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
  4334. "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  4335. }
  4336. depth_zv = zend_ast_get_zval(depth_ast);
  4337. if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
  4338. zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
  4339. ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  4340. }
  4341. depth = Z_LVAL_P(depth_zv);
  4342. } else {
  4343. depth = 1;
  4344. }
  4345. if (CG(context).current_brk_cont == -1) {
  4346. zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
  4347. ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  4348. } else {
  4349. if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
  4350. zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
  4351. ast->kind == ZEND_AST_BREAK ? "break" : "continue",
  4352. depth, depth == 1 ? "" : "s");
  4353. }
  4354. }
  4355. if (ast->kind == ZEND_AST_CONTINUE) {
  4356. int d, cur = CG(context).current_brk_cont;
  4357. for (d = depth - 1; d > 0; d--) {
  4358. cur = CG(context).brk_cont_array[cur].parent;
  4359. ZEND_ASSERT(cur != -1);
  4360. }
  4361. if (CG(context).brk_cont_array[cur].is_switch) {
  4362. if (depth == 1) {
  4363. if (CG(context).brk_cont_array[cur].parent == -1) {
  4364. zend_error(E_WARNING,
  4365. "\"continue\" targeting switch is equivalent to \"break\"");
  4366. } else {
  4367. zend_error(E_WARNING,
  4368. "\"continue\" targeting switch is equivalent to \"break\". " \
  4369. "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
  4370. depth + 1);
  4371. }
  4372. } else {
  4373. if (CG(context).brk_cont_array[cur].parent == -1) {
  4374. zend_error(E_WARNING,
  4375. "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
  4376. depth, depth);
  4377. } else {
  4378. zend_error(E_WARNING,
  4379. "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
  4380. "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
  4381. depth, depth, depth + 1);
  4382. }
  4383. }
  4384. }
  4385. }
  4386. opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
  4387. opline->op1.num = CG(context).current_brk_cont;
  4388. opline->op2.num = depth;
  4389. }
  4390. /* }}} */
  4391. void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
  4392. {
  4393. zend_label *dest;
  4394. int current, remove_oplines = opline->op1.num;
  4395. zval *label;
  4396. uint32_t opnum = opline - op_array->opcodes;
  4397. label = CT_CONSTANT_EX(op_array, opline->op2.constant);
  4398. if (CG(context).labels == NULL ||
  4399. (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
  4400. ) {
  4401. CG(in_compilation) = 1;
  4402. CG(active_op_array) = op_array;
  4403. CG(zend_lineno) = opline->lineno;
  4404. zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
  4405. }
  4406. zval_ptr_dtor_str(label);
  4407. ZVAL_NULL(label);
  4408. current = opline->extended_value;
  4409. for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
  4410. if (current == -1) {
  4411. CG(in_compilation) = 1;
  4412. CG(active_op_array) = op_array;
  4413. CG(zend_lineno) = opline->lineno;
  4414. zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
  4415. }
  4416. if (CG(context).brk_cont_array[current].start >= 0) {
  4417. remove_oplines--;
  4418. }
  4419. }
  4420. for (current = 0; current < op_array->last_try_catch; ++current) {
  4421. zend_try_catch_element *elem = &op_array->try_catch_array[current];
  4422. if (elem->try_op > opnum) {
  4423. break;
  4424. }
  4425. if (elem->finally_op && opnum < elem->finally_op - 1
  4426. && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
  4427. ) {
  4428. remove_oplines--;
  4429. }
  4430. }
  4431. opline->opcode = ZEND_JMP;
  4432. opline->op1.opline_num = dest->opline_num;
  4433. opline->extended_value = 0;
  4434. SET_UNUSED(opline->op1);
  4435. SET_UNUSED(opline->op2);
  4436. SET_UNUSED(opline->result);
  4437. ZEND_ASSERT(remove_oplines >= 0);
  4438. while (remove_oplines--) {
  4439. opline--;
  4440. MAKE_NOP(opline);
  4441. ZEND_VM_SET_OPCODE_HANDLER(opline);
  4442. }
  4443. }
  4444. /* }}} */
  4445. static void zend_compile_goto(zend_ast *ast) /* {{{ */
  4446. {
  4447. zend_ast *label_ast = ast->child[0];
  4448. znode label_node;
  4449. zend_op *opline;
  4450. uint32_t opnum_start = get_next_op_number();
  4451. zend_compile_expr(&label_node, label_ast);
  4452. /* Label resolution and unwinding adjustments happen in pass two. */
  4453. zend_handle_loops_and_finally(NULL);
  4454. opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
  4455. opline->op1.num = get_next_op_number() - opnum_start - 1;
  4456. opline->extended_value = CG(context).current_brk_cont;
  4457. }
  4458. /* }}} */
  4459. static void zend_compile_label(zend_ast *ast) /* {{{ */
  4460. {
  4461. zend_string *label = zend_ast_get_str(ast->child[0]);
  4462. zend_label dest;
  4463. if (!CG(context).labels) {
  4464. ALLOC_HASHTABLE(CG(context).labels);
  4465. zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
  4466. }
  4467. dest.brk_cont = CG(context).current_brk_cont;
  4468. dest.opline_num = get_next_op_number();
  4469. if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
  4470. zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
  4471. }
  4472. }
  4473. /* }}} */
  4474. static void zend_compile_while(zend_ast *ast) /* {{{ */
  4475. {
  4476. zend_ast *cond_ast = ast->child[0];
  4477. zend_ast *stmt_ast = ast->child[1];
  4478. znode cond_node;
  4479. uint32_t opnum_start, opnum_jmp, opnum_cond;
  4480. opnum_jmp = zend_emit_jump(0);
  4481. zend_begin_loop(ZEND_NOP, NULL, 0);
  4482. opnum_start = get_next_op_number();
  4483. zend_compile_stmt(stmt_ast);
  4484. opnum_cond = get_next_op_number();
  4485. zend_update_jump_target(opnum_jmp, opnum_cond);
  4486. zend_compile_expr(&cond_node, cond_ast);
  4487. zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
  4488. zend_end_loop(opnum_cond, NULL);
  4489. }
  4490. /* }}} */
  4491. static void zend_compile_do_while(zend_ast *ast) /* {{{ */
  4492. {
  4493. zend_ast *stmt_ast = ast->child[0];
  4494. zend_ast *cond_ast = ast->child[1];
  4495. znode cond_node;
  4496. uint32_t opnum_start, opnum_cond;
  4497. zend_begin_loop(ZEND_NOP, NULL, 0);
  4498. opnum_start = get_next_op_number();
  4499. zend_compile_stmt(stmt_ast);
  4500. opnum_cond = get_next_op_number();
  4501. zend_compile_expr(&cond_node, cond_ast);
  4502. zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
  4503. zend_end_loop(opnum_cond, NULL);
  4504. }
  4505. /* }}} */
  4506. static void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
  4507. {
  4508. zend_ast_list *list;
  4509. uint32_t i;
  4510. result->op_type = IS_CONST;
  4511. ZVAL_TRUE(&result->u.constant);
  4512. if (!ast) {
  4513. return;
  4514. }
  4515. list = zend_ast_get_list(ast);
  4516. for (i = 0; i < list->children; ++i) {
  4517. zend_ast *expr_ast = list->child[i];
  4518. zend_do_free(result);
  4519. zend_compile_expr(result, expr_ast);
  4520. }
  4521. }
  4522. /* }}} */
  4523. static void zend_compile_for(zend_ast *ast) /* {{{ */
  4524. {
  4525. zend_ast *init_ast = ast->child[0];
  4526. zend_ast *cond_ast = ast->child[1];
  4527. zend_ast *loop_ast = ast->child[2];
  4528. zend_ast *stmt_ast = ast->child[3];
  4529. znode result;
  4530. uint32_t opnum_start, opnum_jmp, opnum_loop;
  4531. zend_compile_expr_list(&result, init_ast);
  4532. zend_do_free(&result);
  4533. opnum_jmp = zend_emit_jump(0);
  4534. zend_begin_loop(ZEND_NOP, NULL, 0);
  4535. opnum_start = get_next_op_number();
  4536. zend_compile_stmt(stmt_ast);
  4537. opnum_loop = get_next_op_number();
  4538. zend_compile_expr_list(&result, loop_ast);
  4539. zend_do_free(&result);
  4540. zend_update_jump_target_to_next(opnum_jmp);
  4541. zend_compile_expr_list(&result, cond_ast);
  4542. zend_do_extended_stmt();
  4543. zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
  4544. zend_end_loop(opnum_loop, NULL);
  4545. }
  4546. /* }}} */
  4547. static void zend_compile_foreach(zend_ast *ast) /* {{{ */
  4548. {
  4549. zend_ast *expr_ast = ast->child[0];
  4550. zend_ast *value_ast = ast->child[1];
  4551. zend_ast *key_ast = ast->child[2];
  4552. zend_ast *stmt_ast = ast->child[3];
  4553. bool by_ref = value_ast->kind == ZEND_AST_REF;
  4554. bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
  4555. znode expr_node, reset_node, value_node, key_node;
  4556. zend_op *opline;
  4557. uint32_t opnum_reset, opnum_fetch;
  4558. if (key_ast) {
  4559. if (key_ast->kind == ZEND_AST_REF) {
  4560. zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
  4561. }
  4562. if (key_ast->kind == ZEND_AST_ARRAY) {
  4563. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
  4564. }
  4565. }
  4566. if (by_ref) {
  4567. value_ast = value_ast->child[0];
  4568. }
  4569. if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
  4570. by_ref = 1;
  4571. }
  4572. if (by_ref && is_variable) {
  4573. zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
  4574. } else {
  4575. zend_compile_expr(&expr_node, expr_ast);
  4576. }
  4577. if (by_ref) {
  4578. zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
  4579. }
  4580. opnum_reset = get_next_op_number();
  4581. opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
  4582. zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);
  4583. opnum_fetch = get_next_op_number();
  4584. opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
  4585. if (is_this_fetch(value_ast)) {
  4586. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  4587. } else if (value_ast->kind == ZEND_AST_VAR &&
  4588. zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
  4589. SET_NODE(opline->op2, &value_node);
  4590. } else {
  4591. opline->op2_type = IS_VAR;
  4592. opline->op2.var = get_temporary_variable();
  4593. GET_NODE(&value_node, opline->op2);
  4594. if (value_ast->kind == ZEND_AST_ARRAY) {
  4595. zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
  4596. } else if (by_ref) {
  4597. zend_emit_assign_ref_znode(value_ast, &value_node);
  4598. } else {
  4599. zend_emit_assign_znode(value_ast, &value_node);
  4600. }
  4601. }
  4602. if (key_ast) {
  4603. opline = &CG(active_op_array)->opcodes[opnum_fetch];
  4604. zend_make_tmp_result(&key_node, opline);
  4605. zend_emit_assign_znode(key_ast, &key_node);
  4606. }
  4607. zend_compile_stmt(stmt_ast);
  4608. /* Place JMP and FE_FREE on the line where foreach starts. It would be
  4609. * better to use the end line, but this information is not available
  4610. * currently. */
  4611. CG(zend_lineno) = ast->lineno;
  4612. zend_emit_jump(opnum_fetch);
  4613. opline = &CG(active_op_array)->opcodes[opnum_reset];
  4614. opline->op2.opline_num = get_next_op_number();
  4615. opline = &CG(active_op_array)->opcodes[opnum_fetch];
  4616. opline->extended_value = get_next_op_number();
  4617. zend_end_loop(opnum_fetch, &reset_node);
  4618. opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
  4619. }
  4620. /* }}} */
  4621. static void zend_compile_if(zend_ast *ast) /* {{{ */
  4622. {
  4623. zend_ast_list *list = zend_ast_get_list(ast);
  4624. uint32_t i;
  4625. uint32_t *jmp_opnums = NULL;
  4626. if (list->children > 1) {
  4627. jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
  4628. }
  4629. for (i = 0; i < list->children; ++i) {
  4630. zend_ast *elem_ast = list->child[i];
  4631. zend_ast *cond_ast = elem_ast->child[0];
  4632. zend_ast *stmt_ast = elem_ast->child[1];
  4633. if (cond_ast) {
  4634. znode cond_node;
  4635. uint32_t opnum_jmpz;
  4636. if (i > 0) {
  4637. CG(zend_lineno) = cond_ast->lineno;
  4638. zend_do_extended_stmt();
  4639. }
  4640. zend_compile_expr(&cond_node, cond_ast);
  4641. opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  4642. zend_compile_stmt(stmt_ast);
  4643. if (i != list->children - 1) {
  4644. jmp_opnums[i] = zend_emit_jump(0);
  4645. }
  4646. zend_update_jump_target_to_next(opnum_jmpz);
  4647. } else {
  4648. /* "else" can only occur as last element. */
  4649. ZEND_ASSERT(i == list->children - 1);
  4650. zend_compile_stmt(stmt_ast);
  4651. }
  4652. }
  4653. if (list->children > 1) {
  4654. for (i = 0; i < list->children - 1; ++i) {
  4655. zend_update_jump_target_to_next(jmp_opnums[i]);
  4656. }
  4657. efree(jmp_opnums);
  4658. }
  4659. }
  4660. /* }}} */
  4661. static zend_uchar determine_switch_jumptable_type(zend_ast_list *cases) {
  4662. uint32_t i;
  4663. zend_uchar common_type = IS_UNDEF;
  4664. for (i = 0; i < cases->children; i++) {
  4665. zend_ast *case_ast = cases->child[i];
  4666. zend_ast **cond_ast = &case_ast->child[0];
  4667. zval *cond_zv;
  4668. if (!case_ast->child[0]) {
  4669. /* Skip default clause */
  4670. continue;
  4671. }
  4672. zend_eval_const_expr(cond_ast);
  4673. if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
  4674. /* Non-constant case */
  4675. return IS_UNDEF;
  4676. }
  4677. cond_zv = zend_ast_get_zval(case_ast->child[0]);
  4678. if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
  4679. /* We only optimize switched on integers and strings */
  4680. return IS_UNDEF;
  4681. }
  4682. if (common_type == IS_UNDEF) {
  4683. common_type = Z_TYPE_P(cond_zv);
  4684. } else if (common_type != Z_TYPE_P(cond_zv)) {
  4685. /* Non-uniform case types */
  4686. return IS_UNDEF;
  4687. }
  4688. if (Z_TYPE_P(cond_zv) == IS_STRING
  4689. && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
  4690. /* Numeric strings cannot be compared with a simple hash lookup */
  4691. return IS_UNDEF;
  4692. }
  4693. }
  4694. return common_type;
  4695. }
  4696. static bool should_use_jumptable(zend_ast_list *cases, zend_uchar jumptable_type) {
  4697. if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
  4698. return 0;
  4699. }
  4700. /* Thresholds are chosen based on when the average switch time for equidistributed
  4701. * input becomes smaller when using the jumptable optimization. */
  4702. if (jumptable_type == IS_LONG) {
  4703. return cases->children >= 5;
  4704. } else {
  4705. ZEND_ASSERT(jumptable_type == IS_STRING);
  4706. return cases->children >= 2;
  4707. }
  4708. }
  4709. static void zend_compile_switch(zend_ast *ast) /* {{{ */
  4710. {
  4711. zend_ast *expr_ast = ast->child[0];
  4712. zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
  4713. uint32_t i;
  4714. bool has_default_case = 0;
  4715. znode expr_node, case_node;
  4716. zend_op *opline;
  4717. uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
  4718. zend_uchar jumptable_type;
  4719. HashTable *jumptable = NULL;
  4720. zend_compile_expr(&expr_node, expr_ast);
  4721. zend_begin_loop(ZEND_FREE, &expr_node, 1);
  4722. case_node.op_type = IS_TMP_VAR;
  4723. case_node.u.op.var = get_temporary_variable();
  4724. jumptable_type = determine_switch_jumptable_type(cases);
  4725. if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
  4726. znode jumptable_op;
  4727. ALLOC_HASHTABLE(jumptable);
  4728. zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
  4729. jumptable_op.op_type = IS_CONST;
  4730. ZVAL_ARR(&jumptable_op.u.constant, jumptable);
  4731. opline = zend_emit_op(NULL,
  4732. jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
  4733. &expr_node, &jumptable_op);
  4734. if (opline->op1_type == IS_CONST) {
  4735. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4736. }
  4737. opnum_switch = opline - CG(active_op_array)->opcodes;
  4738. }
  4739. jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
  4740. for (i = 0; i < cases->children; ++i) {
  4741. zend_ast *case_ast = cases->child[i];
  4742. zend_ast *cond_ast = case_ast->child[0];
  4743. znode cond_node;
  4744. if (!cond_ast) {
  4745. if (has_default_case) {
  4746. CG(zend_lineno) = case_ast->lineno;
  4747. zend_error_noreturn(E_COMPILE_ERROR,
  4748. "Switch statements may only contain one default clause");
  4749. }
  4750. has_default_case = 1;
  4751. continue;
  4752. }
  4753. zend_compile_expr(&cond_node, cond_ast);
  4754. if (expr_node.op_type == IS_CONST
  4755. && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
  4756. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  4757. } else if (expr_node.op_type == IS_CONST
  4758. && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
  4759. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
  4760. } else {
  4761. opline = zend_emit_op(NULL,
  4762. (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
  4763. &expr_node, &cond_node);
  4764. SET_NODE(opline->result, &case_node);
  4765. if (opline->op1_type == IS_CONST) {
  4766. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4767. }
  4768. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
  4769. }
  4770. }
  4771. opnum_default_jmp = zend_emit_jump(0);
  4772. for (i = 0; i < cases->children; ++i) {
  4773. zend_ast *case_ast = cases->child[i];
  4774. zend_ast *cond_ast = case_ast->child[0];
  4775. zend_ast *stmt_ast = case_ast->child[1];
  4776. if (cond_ast) {
  4777. zend_update_jump_target_to_next(jmpnz_opnums[i]);
  4778. if (jumptable) {
  4779. zval *cond_zv = zend_ast_get_zval(cond_ast);
  4780. zval jmp_target;
  4781. ZVAL_LONG(&jmp_target, get_next_op_number());
  4782. ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
  4783. if (Z_TYPE_P(cond_zv) == IS_LONG) {
  4784. zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
  4785. } else {
  4786. ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
  4787. zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
  4788. }
  4789. }
  4790. } else {
  4791. zend_update_jump_target_to_next(opnum_default_jmp);
  4792. if (jumptable) {
  4793. ZEND_ASSERT(opnum_switch != (uint32_t)-1);
  4794. opline = &CG(active_op_array)->opcodes[opnum_switch];
  4795. opline->extended_value = get_next_op_number();
  4796. }
  4797. }
  4798. zend_compile_stmt(stmt_ast);
  4799. }
  4800. if (!has_default_case) {
  4801. zend_update_jump_target_to_next(opnum_default_jmp);
  4802. if (jumptable) {
  4803. opline = &CG(active_op_array)->opcodes[opnum_switch];
  4804. opline->extended_value = get_next_op_number();
  4805. }
  4806. }
  4807. zend_end_loop(get_next_op_number(), &expr_node);
  4808. if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
  4809. opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
  4810. opline->extended_value = ZEND_FREE_SWITCH;
  4811. } else if (expr_node.op_type == IS_CONST) {
  4812. zval_ptr_dtor_nogc(&expr_node.u.constant);
  4813. }
  4814. efree(jmpnz_opnums);
  4815. }
  4816. /* }}} */
  4817. static uint32_t count_match_conds(zend_ast_list *arms)
  4818. {
  4819. uint32_t num_conds = 0;
  4820. for (uint32_t i = 0; i < arms->children; i++) {
  4821. zend_ast *arm_ast = arms->child[i];
  4822. if (arm_ast->child[0] == NULL) {
  4823. continue;
  4824. }
  4825. zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
  4826. num_conds += conds->children;
  4827. }
  4828. return num_conds;
  4829. }
  4830. static bool can_match_use_jumptable(zend_ast_list *arms) {
  4831. for (uint32_t i = 0; i < arms->children; i++) {
  4832. zend_ast *arm_ast = arms->child[i];
  4833. if (!arm_ast->child[0]) {
  4834. /* Skip default arm */
  4835. continue;
  4836. }
  4837. zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
  4838. for (uint32_t j = 0; j < conds->children; j++) {
  4839. zend_ast **cond_ast = &conds->child[j];
  4840. zend_eval_const_expr(cond_ast);
  4841. if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
  4842. return 0;
  4843. }
  4844. zval *cond_zv = zend_ast_get_zval(*cond_ast);
  4845. if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
  4846. return 0;
  4847. }
  4848. }
  4849. }
  4850. return 1;
  4851. }
  4852. static void zend_compile_match(znode *result, zend_ast *ast)
  4853. {
  4854. zend_ast *expr_ast = ast->child[0];
  4855. zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
  4856. bool has_default_arm = 0;
  4857. uint32_t opnum_match = (uint32_t)-1;
  4858. znode expr_node;
  4859. zend_compile_expr(&expr_node, expr_ast);
  4860. znode case_node;
  4861. case_node.op_type = IS_TMP_VAR;
  4862. case_node.u.op.var = get_temporary_variable();
  4863. uint32_t num_conds = count_match_conds(arms);
  4864. zend_uchar can_use_jumptable = can_match_use_jumptable(arms);
  4865. bool uses_jumptable = can_use_jumptable && num_conds >= 2;
  4866. HashTable *jumptable = NULL;
  4867. uint32_t *jmpnz_opnums = NULL;
  4868. for (uint32_t i = 0; i < arms->children; ++i) {
  4869. zend_ast *arm_ast = arms->child[i];
  4870. if (!arm_ast->child[0]) {
  4871. if (has_default_arm) {
  4872. CG(zend_lineno) = arm_ast->lineno;
  4873. zend_error_noreturn(E_COMPILE_ERROR,
  4874. "Match expressions may only contain one default arm");
  4875. }
  4876. has_default_arm = 1;
  4877. }
  4878. }
  4879. if (uses_jumptable) {
  4880. znode jumptable_op;
  4881. ALLOC_HASHTABLE(jumptable);
  4882. zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
  4883. jumptable_op.op_type = IS_CONST;
  4884. ZVAL_ARR(&jumptable_op.u.constant, jumptable);
  4885. zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
  4886. if (opline->op1_type == IS_CONST) {
  4887. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4888. }
  4889. opnum_match = opline - CG(active_op_array)->opcodes;
  4890. } else {
  4891. jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
  4892. uint32_t cond_count = 0;
  4893. for (uint32_t i = 0; i < arms->children; ++i) {
  4894. zend_ast *arm_ast = arms->child[i];
  4895. if (!arm_ast->child[0]) {
  4896. continue;
  4897. }
  4898. zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
  4899. for (uint32_t j = 0; j < conds->children; j++) {
  4900. zend_ast *cond_ast = conds->child[j];
  4901. znode cond_node;
  4902. zend_compile_expr(&cond_node, cond_ast);
  4903. uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
  4904. zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
  4905. SET_NODE(opline->result, &case_node);
  4906. if (opline->op1_type == IS_CONST) {
  4907. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4908. }
  4909. jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
  4910. cond_count++;
  4911. }
  4912. }
  4913. }
  4914. uint32_t opnum_default_jmp = 0;
  4915. if (!uses_jumptable) {
  4916. opnum_default_jmp = zend_emit_jump(0);
  4917. }
  4918. bool is_first_case = 1;
  4919. uint32_t cond_count = 0;
  4920. uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
  4921. // The generated default arm is emitted first to avoid live range issues where the tmpvar
  4922. // for the arm result is freed even though it has not been initialized yet.
  4923. if (!has_default_arm) {
  4924. if (!uses_jumptable) {
  4925. zend_update_jump_target_to_next(opnum_default_jmp);
  4926. }
  4927. if (jumptable) {
  4928. zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
  4929. opline->extended_value = get_next_op_number();
  4930. }
  4931. zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
  4932. if (opline->op1_type == IS_CONST) {
  4933. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4934. }
  4935. }
  4936. for (uint32_t i = 0; i < arms->children; ++i) {
  4937. zend_ast *arm_ast = arms->child[i];
  4938. zend_ast *body_ast = arm_ast->child[1];
  4939. if (arm_ast->child[0] != NULL) {
  4940. zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
  4941. for (uint32_t j = 0; j < conds->children; j++) {
  4942. zend_ast *cond_ast = conds->child[j];
  4943. if (jmpnz_opnums != NULL) {
  4944. zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
  4945. }
  4946. if (jumptable) {
  4947. zval *cond_zv = zend_ast_get_zval(cond_ast);
  4948. zval jmp_target;
  4949. ZVAL_LONG(&jmp_target, get_next_op_number());
  4950. if (Z_TYPE_P(cond_zv) == IS_LONG) {
  4951. zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
  4952. } else {
  4953. ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
  4954. zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
  4955. }
  4956. }
  4957. cond_count++;
  4958. }
  4959. } else {
  4960. if (!uses_jumptable) {
  4961. zend_update_jump_target_to_next(opnum_default_jmp);
  4962. }
  4963. if (jumptable) {
  4964. ZEND_ASSERT(opnum_match != (uint32_t)-1);
  4965. zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
  4966. opline->extended_value = get_next_op_number();
  4967. }
  4968. }
  4969. znode body_node;
  4970. zend_compile_expr(&body_node, body_ast);
  4971. if (is_first_case) {
  4972. zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
  4973. is_first_case = 0;
  4974. } else {
  4975. zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
  4976. SET_NODE(opline_qm_assign->result, result);
  4977. }
  4978. jmp_end_opnums[i] = zend_emit_jump(0);
  4979. }
  4980. // Initialize result in case there is no arm
  4981. if (arms->children == 0) {
  4982. result->op_type = IS_CONST;
  4983. ZVAL_NULL(&result->u.constant);
  4984. }
  4985. for (uint32_t i = 0; i < arms->children; ++i) {
  4986. zend_update_jump_target_to_next(jmp_end_opnums[i]);
  4987. }
  4988. if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
  4989. zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
  4990. opline->extended_value = ZEND_FREE_SWITCH;
  4991. } else if (expr_node.op_type == IS_CONST) {
  4992. zval_ptr_dtor_nogc(&expr_node.u.constant);
  4993. }
  4994. if (jmpnz_opnums != NULL) {
  4995. efree(jmpnz_opnums);
  4996. }
  4997. efree(jmp_end_opnums);
  4998. }
  4999. static void zend_compile_try(zend_ast *ast) /* {{{ */
  5000. {
  5001. zend_ast *try_ast = ast->child[0];
  5002. zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
  5003. zend_ast *finally_ast = ast->child[2];
  5004. uint32_t i, j;
  5005. zend_op *opline;
  5006. uint32_t try_catch_offset;
  5007. uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
  5008. uint32_t orig_fast_call_var = CG(context).fast_call_var;
  5009. uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
  5010. if (catches->children == 0 && !finally_ast) {
  5011. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
  5012. }
  5013. /* label: try { } must not be equal to try { label: } */
  5014. if (CG(context).labels) {
  5015. zend_label *label;
  5016. ZEND_HASH_REVERSE_FOREACH_PTR(CG(context).labels, label) {
  5017. if (label->opline_num == get_next_op_number()) {
  5018. zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
  5019. }
  5020. break;
  5021. } ZEND_HASH_FOREACH_END();
  5022. }
  5023. try_catch_offset = zend_add_try_element(get_next_op_number());
  5024. if (finally_ast) {
  5025. zend_loop_var fast_call;
  5026. if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
  5027. CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
  5028. }
  5029. CG(context).fast_call_var = get_temporary_variable();
  5030. /* Push FAST_CALL on unwind stack */
  5031. fast_call.opcode = ZEND_FAST_CALL;
  5032. fast_call.var_type = IS_TMP_VAR;
  5033. fast_call.var_num = CG(context).fast_call_var;
  5034. fast_call.try_catch_offset = try_catch_offset;
  5035. zend_stack_push(&CG(loop_var_stack), &fast_call);
  5036. }
  5037. CG(context).try_catch_offset = try_catch_offset;
  5038. zend_compile_stmt(try_ast);
  5039. if (catches->children != 0) {
  5040. jmp_opnums[0] = zend_emit_jump(0);
  5041. }
  5042. for (i = 0; i < catches->children; ++i) {
  5043. zend_ast *catch_ast = catches->child[i];
  5044. zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
  5045. zend_ast *var_ast = catch_ast->child[1];
  5046. zend_ast *stmt_ast = catch_ast->child[2];
  5047. zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
  5048. bool is_last_catch = (i + 1 == catches->children);
  5049. uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
  5050. uint32_t opnum_catch = (uint32_t)-1;
  5051. CG(zend_lineno) = catch_ast->lineno;
  5052. for (j = 0; j < classes->children; j++) {
  5053. zend_ast *class_ast = classes->child[j];
  5054. bool is_last_class = (j + 1 == classes->children);
  5055. if (!zend_is_const_default_class_ref(class_ast)) {
  5056. zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
  5057. }
  5058. opnum_catch = get_next_op_number();
  5059. if (i == 0 && j == 0) {
  5060. CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
  5061. }
  5062. opline = get_next_op();
  5063. opline->opcode = ZEND_CATCH;
  5064. opline->op1_type = IS_CONST;
  5065. opline->op1.constant = zend_add_class_name_literal(
  5066. zend_resolve_class_name_ast(class_ast));
  5067. opline->extended_value = zend_alloc_cache_slot();
  5068. if (var_name && zend_string_equals_literal(var_name, "this")) {
  5069. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  5070. }
  5071. opline->result_type = var_name ? IS_CV : IS_UNUSED;
  5072. opline->result.var = var_name ? lookup_cv(var_name) : -1;
  5073. if (is_last_catch && is_last_class) {
  5074. opline->extended_value |= ZEND_LAST_CATCH;
  5075. }
  5076. if (!is_last_class) {
  5077. jmp_multicatch[j] = zend_emit_jump(0);
  5078. opline = &CG(active_op_array)->opcodes[opnum_catch];
  5079. opline->op2.opline_num = get_next_op_number();
  5080. }
  5081. }
  5082. for (j = 0; j < classes->children - 1; j++) {
  5083. zend_update_jump_target_to_next(jmp_multicatch[j]);
  5084. }
  5085. efree(jmp_multicatch);
  5086. zend_compile_stmt(stmt_ast);
  5087. if (!is_last_catch) {
  5088. jmp_opnums[i + 1] = zend_emit_jump(0);
  5089. }
  5090. ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
  5091. opline = &CG(active_op_array)->opcodes[opnum_catch];
  5092. if (!is_last_catch) {
  5093. opline->op2.opline_num = get_next_op_number();
  5094. }
  5095. }
  5096. for (i = 0; i < catches->children; ++i) {
  5097. zend_update_jump_target_to_next(jmp_opnums[i]);
  5098. }
  5099. if (finally_ast) {
  5100. zend_loop_var discard_exception;
  5101. uint32_t opnum_jmp = get_next_op_number() + 1;
  5102. /* Pop FAST_CALL from unwind stack */
  5103. zend_stack_del_top(&CG(loop_var_stack));
  5104. /* Push DISCARD_EXCEPTION on unwind stack */
  5105. discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
  5106. discard_exception.var_type = IS_TMP_VAR;
  5107. discard_exception.var_num = CG(context).fast_call_var;
  5108. zend_stack_push(&CG(loop_var_stack), &discard_exception);
  5109. CG(zend_lineno) = finally_ast->lineno;
  5110. opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
  5111. opline->op1.num = try_catch_offset;
  5112. opline->result_type = IS_TMP_VAR;
  5113. opline->result.var = CG(context).fast_call_var;
  5114. zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
  5115. zend_compile_stmt(finally_ast);
  5116. CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
  5117. CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
  5118. = get_next_op_number();
  5119. opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
  5120. opline->op1_type = IS_TMP_VAR;
  5121. opline->op1.var = CG(context).fast_call_var;
  5122. opline->op2.num = orig_try_catch_offset;
  5123. zend_update_jump_target_to_next(opnum_jmp);
  5124. CG(context).fast_call_var = orig_fast_call_var;
  5125. /* Pop DISCARD_EXCEPTION from unwind stack */
  5126. zend_stack_del_top(&CG(loop_var_stack));
  5127. }
  5128. CG(context).try_catch_offset = orig_try_catch_offset;
  5129. efree(jmp_opnums);
  5130. }
  5131. /* }}} */
  5132. /* Encoding declarations must already be handled during parsing */
  5133. bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
  5134. {
  5135. zend_ast_list *declares = zend_ast_get_list(ast);
  5136. uint32_t i;
  5137. for (i = 0; i < declares->children; ++i) {
  5138. zend_ast *declare_ast = declares->child[i];
  5139. zend_ast *name_ast = declare_ast->child[0];
  5140. zend_ast *value_ast = declare_ast->child[1];
  5141. zend_string *name = zend_ast_get_str(name_ast);
  5142. if (zend_string_equals_literal_ci(name, "encoding")) {
  5143. if (value_ast->kind != ZEND_AST_ZVAL) {
  5144. zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
  5145. return 0;
  5146. }
  5147. if (CG(multibyte)) {
  5148. zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
  5149. const zend_encoding *new_encoding, *old_encoding;
  5150. zend_encoding_filter old_input_filter;
  5151. CG(encoding_declared) = 1;
  5152. new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
  5153. if (!new_encoding) {
  5154. zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
  5155. } else {
  5156. old_input_filter = LANG_SCNG(input_filter);
  5157. old_encoding = LANG_SCNG(script_encoding);
  5158. zend_multibyte_set_filter(new_encoding);
  5159. /* need to re-scan if input filter changed */
  5160. if (old_input_filter != LANG_SCNG(input_filter) ||
  5161. (old_input_filter && new_encoding != old_encoding)) {
  5162. zend_multibyte_yyinput_again(old_input_filter, old_encoding);
  5163. }
  5164. }
  5165. zend_string_release_ex(encoding_name, 0);
  5166. } else {
  5167. zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
  5168. "Zend multibyte feature is turned off by settings");
  5169. }
  5170. }
  5171. }
  5172. return 1;
  5173. }
  5174. /* }}} */
  5175. /* Check whether this is the first statement, not counting declares. */
  5176. static zend_result zend_is_first_statement(zend_ast *ast, bool allow_nop) /* {{{ */
  5177. {
  5178. uint32_t i = 0;
  5179. zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
  5180. while (i < file_ast->children) {
  5181. if (file_ast->child[i] == ast) {
  5182. return SUCCESS;
  5183. } else if (file_ast->child[i] == NULL) {
  5184. if (!allow_nop) {
  5185. return FAILURE;
  5186. }
  5187. } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
  5188. return FAILURE;
  5189. }
  5190. i++;
  5191. }
  5192. return FAILURE;
  5193. }
  5194. /* }}} */
  5195. static void zend_compile_declare(zend_ast *ast) /* {{{ */
  5196. {
  5197. zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
  5198. zend_ast *stmt_ast = ast->child[1];
  5199. zend_declarables orig_declarables = FC(declarables);
  5200. uint32_t i;
  5201. for (i = 0; i < declares->children; ++i) {
  5202. zend_ast *declare_ast = declares->child[i];
  5203. zend_ast *name_ast = declare_ast->child[0];
  5204. zend_ast **value_ast_ptr = &declare_ast->child[1];
  5205. zend_string *name = zend_ast_get_str(name_ast);
  5206. if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
  5207. zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
  5208. }
  5209. if (zend_string_equals_literal_ci(name, "ticks")) {
  5210. zval value_zv;
  5211. zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
  5212. FC(declarables).ticks = zval_get_long(&value_zv);
  5213. zval_ptr_dtor_nogc(&value_zv);
  5214. } else if (zend_string_equals_literal_ci(name, "encoding")) {
  5215. if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
  5216. zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
  5217. "the very first statement in the script");
  5218. }
  5219. } else if (zend_string_equals_literal_ci(name, "strict_types")) {
  5220. zval value_zv;
  5221. if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ 0)) {
  5222. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
  5223. "the very first statement in the script");
  5224. }
  5225. if (ast->child[1] != NULL) {
  5226. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
  5227. "use block mode");
  5228. }
  5229. zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
  5230. if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
  5231. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
  5232. }
  5233. if (Z_LVAL(value_zv) == 1) {
  5234. CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
  5235. }
  5236. } else {
  5237. zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
  5238. }
  5239. }
  5240. if (stmt_ast) {
  5241. zend_compile_stmt(stmt_ast);
  5242. FC(declarables) = orig_declarables;
  5243. }
  5244. }
  5245. /* }}} */
  5246. static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
  5247. {
  5248. zend_ast_list *list = zend_ast_get_list(ast);
  5249. uint32_t i;
  5250. for (i = 0; i < list->children; ++i) {
  5251. zend_compile_stmt(list->child[i]);
  5252. }
  5253. }
  5254. /* }}} */
  5255. ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
  5256. {
  5257. uint32_t i, n;
  5258. func->common.arg_flags[0] = 0;
  5259. func->common.arg_flags[1] = 0;
  5260. func->common.arg_flags[2] = 0;
  5261. if (func->common.arg_info) {
  5262. n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
  5263. i = 0;
  5264. while (i < n) {
  5265. ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
  5266. i++;
  5267. }
  5268. if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
  5269. uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
  5270. while (i < MAX_ARG_FLAG_NUM) {
  5271. ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
  5272. i++;
  5273. }
  5274. }
  5275. }
  5276. }
  5277. /* }}} */
  5278. static zend_type zend_compile_single_typename(zend_ast *ast)
  5279. {
  5280. ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
  5281. if (ast->kind == ZEND_AST_TYPE) {
  5282. if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
  5283. zend_error_noreturn(E_COMPILE_ERROR,
  5284. "Cannot use \"static\" when no class scope is active");
  5285. }
  5286. return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
  5287. } else {
  5288. zend_string *class_name = zend_ast_get_str(ast);
  5289. zend_uchar type_code = zend_lookup_builtin_type_by_name(class_name);
  5290. if (type_code != 0) {
  5291. if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
  5292. zend_error_noreturn(E_COMPILE_ERROR,
  5293. "Type declaration '%s' must be unqualified",
  5294. ZSTR_VAL(zend_string_tolower(class_name)));
  5295. }
  5296. return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
  5297. } else {
  5298. const char *correct_name;
  5299. zend_string *orig_name = zend_ast_get_str(ast);
  5300. uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
  5301. if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
  5302. class_name = zend_resolve_class_name_ast(ast);
  5303. zend_assert_valid_class_name(class_name);
  5304. } else {
  5305. zend_ensure_valid_class_fetch_type(fetch_type);
  5306. zend_string_addref(class_name);
  5307. }
  5308. if (ast->attr == ZEND_NAME_NOT_FQ
  5309. && zend_is_confusable_type(orig_name, &correct_name)
  5310. && zend_is_not_imported(orig_name)) {
  5311. const char *extra =
  5312. FC(current_namespace) ? " or import the class with \"use\"" : "";
  5313. if (correct_name) {
  5314. zend_error(E_COMPILE_WARNING,
  5315. "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
  5316. "Write \"\\%s\"%s to suppress this warning",
  5317. ZSTR_VAL(orig_name), correct_name, ZSTR_VAL(class_name), extra);
  5318. } else {
  5319. zend_error(E_COMPILE_WARNING,
  5320. "\"%s\" is not a supported builtin type "
  5321. "and will be interpreted as a class name. "
  5322. "Write \"\\%s\"%s to suppress this warning",
  5323. ZSTR_VAL(orig_name), ZSTR_VAL(class_name), extra);
  5324. }
  5325. }
  5326. class_name = zend_new_interned_string(class_name);
  5327. zend_alloc_ce_cache(class_name);
  5328. return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, 0, 0);
  5329. }
  5330. }
  5331. }
  5332. static bool zend_type_contains_traversable(zend_type type) {
  5333. zend_type *single_type;
  5334. ZEND_TYPE_FOREACH(type, single_type) {
  5335. if (ZEND_TYPE_HAS_NAME(*single_type)
  5336. && zend_string_equals_literal_ci(ZEND_TYPE_NAME(*single_type), "Traversable")) {
  5337. return 1;
  5338. }
  5339. } ZEND_TYPE_FOREACH_END();
  5340. return 0;
  5341. }
  5342. // TODO: Ideally we'd canonicalize "iterable" into "array|Traversable" and essentially
  5343. // treat it as a built-in type alias.
  5344. static zend_type zend_compile_typename(
  5345. zend_ast *ast, bool force_allow_null) /* {{{ */
  5346. {
  5347. bool allow_null = force_allow_null;
  5348. zend_ast_attr orig_ast_attr = ast->attr;
  5349. zend_type type = ZEND_TYPE_INIT_NONE(0);
  5350. if (ast->attr & ZEND_TYPE_NULLABLE) {
  5351. allow_null = 1;
  5352. ast->attr &= ~ZEND_TYPE_NULLABLE;
  5353. }
  5354. if (ast->kind == ZEND_AST_TYPE_UNION) {
  5355. zend_ast_list *list = zend_ast_get_list(ast);
  5356. zend_type_list *type_list;
  5357. ALLOCA_FLAG(use_heap)
  5358. type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
  5359. type_list->num_types = 0;
  5360. for (uint32_t i = 0; i < list->children; i++) {
  5361. zend_ast *type_ast = list->child[i];
  5362. zend_type single_type = zend_compile_single_typename(type_ast);
  5363. uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
  5364. if (single_type_mask == MAY_BE_ANY) {
  5365. zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
  5366. }
  5367. uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
  5368. if (type_mask_overlap) {
  5369. zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
  5370. zend_string *overlap_type_str = zend_type_to_string(overlap_type);
  5371. zend_error_noreturn(E_COMPILE_ERROR,
  5372. "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
  5373. }
  5374. ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
  5375. ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
  5376. if (ZEND_TYPE_IS_COMPLEX(single_type)) {
  5377. if (!ZEND_TYPE_IS_COMPLEX(type)) {
  5378. /* The first class type can be stored directly as the type ptr payload. */
  5379. ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
  5380. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
  5381. } else {
  5382. if (type_list->num_types == 0) {
  5383. /* Switch from single name to name list. */
  5384. type_list->num_types = 1;
  5385. type_list->types[0] = type;
  5386. ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
  5387. ZEND_TYPE_SET_LIST(type, type_list);
  5388. }
  5389. type_list->types[type_list->num_types++] = single_type;
  5390. /* Check for trivially redundant class types */
  5391. for (size_t i = 0; i < type_list->num_types - 1; i++) {
  5392. if (zend_string_equals_ci(
  5393. ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(single_type))) {
  5394. zend_string *single_type_str = zend_type_to_string(single_type);
  5395. zend_error_noreturn(E_COMPILE_ERROR,
  5396. "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
  5397. }
  5398. }
  5399. }
  5400. }
  5401. }
  5402. if (type_list->num_types) {
  5403. zend_type_list *list = zend_arena_alloc(
  5404. &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
  5405. memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
  5406. ZEND_TYPE_SET_LIST(type, list);
  5407. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
  5408. /* Inform that the type list is a union type */
  5409. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
  5410. }
  5411. free_alloca(type_list, use_heap);
  5412. } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
  5413. zend_ast_list *list = zend_ast_get_list(ast);
  5414. zend_type_list *type_list;
  5415. /* Allocate the type list directly on the arena as it must be a type
  5416. * list of the same number of elements as the AST list has children */
  5417. type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
  5418. type_list->num_types = 0;
  5419. ZEND_ASSERT(list->children > 1);
  5420. for (uint32_t i = 0; i < list->children; i++) {
  5421. zend_ast *type_ast = list->child[i];
  5422. zend_type single_type = zend_compile_single_typename(type_ast);
  5423. /* An intersection of standard types cannot exist so invalidate it */
  5424. if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
  5425. zend_string *standard_type_str = zend_type_to_string(single_type);
  5426. zend_error_noreturn(E_COMPILE_ERROR,
  5427. "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
  5428. zend_string_release_ex(standard_type_str, false);
  5429. }
  5430. /* Check for "self" and "parent" too */
  5431. if (zend_string_equals_literal_ci(ZEND_TYPE_NAME(single_type), "self")
  5432. || zend_string_equals_literal_ci(ZEND_TYPE_NAME(single_type), "parent")) {
  5433. zend_error_noreturn(E_COMPILE_ERROR,
  5434. "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
  5435. }
  5436. /* Add type to the type list */
  5437. type_list->types[type_list->num_types++] = single_type;
  5438. /* Check for trivially redundant class types */
  5439. for (size_t i = 0; i < type_list->num_types - 1; i++) {
  5440. if (zend_string_equals_ci(
  5441. ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(single_type))) {
  5442. zend_string *single_type_str = zend_type_to_string(single_type);
  5443. zend_error_noreturn(E_COMPILE_ERROR,
  5444. "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
  5445. }
  5446. }
  5447. }
  5448. ZEND_ASSERT(list->children == type_list->num_types);
  5449. ZEND_TYPE_SET_LIST(type, type_list);
  5450. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
  5451. /* Inform that the type list is an intersection type */
  5452. ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
  5453. } else {
  5454. type = zend_compile_single_typename(ast);
  5455. }
  5456. if (allow_null) {
  5457. ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
  5458. }
  5459. uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
  5460. if ((type_mask & (MAY_BE_ARRAY|MAY_BE_ITERABLE)) == (MAY_BE_ARRAY|MAY_BE_ITERABLE)) {
  5461. zend_string *type_str = zend_type_to_string(type);
  5462. zend_error_noreturn(E_COMPILE_ERROR,
  5463. "Type %s contains both iterable and array, which is redundant", ZSTR_VAL(type_str));
  5464. }
  5465. if ((type_mask & MAY_BE_ITERABLE) && zend_type_contains_traversable(type)) {
  5466. zend_string *type_str = zend_type_to_string(type);
  5467. zend_error_noreturn(E_COMPILE_ERROR,
  5468. "Type %s contains both iterable and Traversable, which is redundant",
  5469. ZSTR_VAL(type_str));
  5470. }
  5471. if (type_mask == MAY_BE_ANY && (orig_ast_attr & ZEND_TYPE_NULLABLE)) {
  5472. zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
  5473. }
  5474. if ((type_mask & MAY_BE_OBJECT) && (ZEND_TYPE_IS_COMPLEX(type) || (type_mask & MAY_BE_STATIC))) {
  5475. zend_string *type_str = zend_type_to_string(type);
  5476. zend_error_noreturn(E_COMPILE_ERROR,
  5477. "Type %s contains both object and a class type, which is redundant",
  5478. ZSTR_VAL(type_str));
  5479. }
  5480. if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
  5481. zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
  5482. }
  5483. if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
  5484. zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
  5485. }
  5486. if ((type_mask & (MAY_BE_NULL|MAY_BE_FALSE))
  5487. && !ZEND_TYPE_IS_COMPLEX(type) && !(type_mask & ~(MAY_BE_NULL|MAY_BE_FALSE))) {
  5488. if (type_mask == MAY_BE_NULL) {
  5489. zend_error_noreturn(E_COMPILE_ERROR, "Null can not be used as a standalone type");
  5490. } else {
  5491. zend_error_noreturn(E_COMPILE_ERROR, "False can not be used as a standalone type");
  5492. }
  5493. }
  5494. ast->attr = orig_ast_attr;
  5495. return type;
  5496. }
  5497. /* }}} */
  5498. /* May convert value from int to float. */
  5499. static bool zend_is_valid_default_value(zend_type type, zval *value)
  5500. {
  5501. ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
  5502. if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
  5503. return 1;
  5504. }
  5505. if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
  5506. /* Integers are allowed as initializers for floating-point values. */
  5507. convert_to_double(value);
  5508. return 1;
  5509. }
  5510. if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_ITERABLE) && Z_TYPE_P(value) == IS_ARRAY) {
  5511. return 1;
  5512. }
  5513. return 0;
  5514. }
  5515. static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target) /* {{{ */
  5516. {
  5517. zend_attribute *attr;
  5518. zend_internal_attribute *config;
  5519. zend_ast_list *list = zend_ast_get_list(ast);
  5520. uint32_t g, i, j;
  5521. ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
  5522. for (g = 0; g < list->children; g++) {
  5523. zend_ast_list *group = zend_ast_get_list(list->child[g]);
  5524. ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
  5525. for (i = 0; i < group->children; i++) {
  5526. ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
  5527. zend_ast *el = group->child[i];
  5528. if (el->child[1] &&
  5529. el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
  5530. zend_error_noreturn(E_COMPILE_ERROR,
  5531. "Cannot create Closure as attribute argument");
  5532. }
  5533. zend_string *name = zend_resolve_class_name_ast(el->child[0]);
  5534. zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
  5535. uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
  5536. ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
  5537. attr = zend_add_attribute(
  5538. attributes, name, args ? args->children : 0, flags, offset, el->lineno);
  5539. zend_string_release(name);
  5540. /* Populate arguments */
  5541. if (args) {
  5542. ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
  5543. bool uses_named_args = 0;
  5544. for (j = 0; j < args->children; j++) {
  5545. zend_ast **arg_ast_ptr = &args->child[j];
  5546. zend_ast *arg_ast = *arg_ast_ptr;
  5547. if (arg_ast->kind == ZEND_AST_UNPACK) {
  5548. zend_error_noreturn(E_COMPILE_ERROR,
  5549. "Cannot use unpacking in attribute argument list");
  5550. }
  5551. if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
  5552. attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
  5553. arg_ast_ptr = &arg_ast->child[1];
  5554. uses_named_args = 1;
  5555. for (uint32_t k = 0; k < j; k++) {
  5556. if (attr->args[k].name &&
  5557. zend_string_equals(attr->args[k].name, attr->args[j].name)) {
  5558. zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
  5559. ZSTR_VAL(attr->args[j].name));
  5560. }
  5561. }
  5562. } else if (uses_named_args) {
  5563. zend_error_noreturn(E_COMPILE_ERROR,
  5564. "Cannot use positional argument after named argument");
  5565. }
  5566. zend_const_expr_to_zval(
  5567. &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
  5568. }
  5569. }
  5570. }
  5571. }
  5572. /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
  5573. ZEND_HASH_FOREACH_PTR(*attributes, attr) {
  5574. if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
  5575. continue;
  5576. }
  5577. if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
  5578. zend_string *location = zend_get_attribute_target_names(target);
  5579. zend_string *allowed = zend_get_attribute_target_names(config->flags);
  5580. zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
  5581. ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
  5582. );
  5583. }
  5584. if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
  5585. if (zend_is_attribute_repeated(*attributes, attr)) {
  5586. zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
  5587. }
  5588. }
  5589. if (config->validator != NULL) {
  5590. config->validator(attr, target, CG(active_class_entry));
  5591. }
  5592. } ZEND_HASH_FOREACH_END();
  5593. }
  5594. /* }}} */
  5595. static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
  5596. {
  5597. zend_ast_list *list = zend_ast_get_list(ast);
  5598. uint32_t i;
  5599. zend_op_array *op_array = CG(active_op_array);
  5600. zend_arg_info *arg_infos;
  5601. if (return_type_ast || fallback_return_type) {
  5602. /* Use op_array->arg_info[-1] for return type */
  5603. arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
  5604. arg_infos->name = NULL;
  5605. if (return_type_ast) {
  5606. arg_infos->type = zend_compile_typename(
  5607. return_type_ast, /* force_allow_null */ 0);
  5608. ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
  5609. (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
  5610. } else {
  5611. arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
  5612. }
  5613. arg_infos++;
  5614. op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  5615. if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
  5616. && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
  5617. zend_error(E_DEPRECATED, "Returning by reference from a void function is deprecated");
  5618. }
  5619. } else {
  5620. if (list->children == 0) {
  5621. return;
  5622. }
  5623. arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
  5624. }
  5625. /* Find last required parameter number for deprecation message. */
  5626. uint32_t last_required_param = (uint32_t) -1;
  5627. for (i = 0; i < list->children; ++i) {
  5628. zend_ast *param_ast = list->child[i];
  5629. zend_ast *default_ast_ptr = param_ast->child[2];
  5630. bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
  5631. if (!default_ast_ptr && !is_variadic) {
  5632. last_required_param = i;
  5633. }
  5634. }
  5635. for (i = 0; i < list->children; ++i) {
  5636. zend_ast *param_ast = list->child[i];
  5637. zend_ast *type_ast = param_ast->child[0];
  5638. zend_ast *var_ast = param_ast->child[1];
  5639. zend_ast **default_ast_ptr = &param_ast->child[2];
  5640. zend_ast *attributes_ast = param_ast->child[3];
  5641. zend_ast *doc_comment_ast = param_ast->child[4];
  5642. zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
  5643. bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
  5644. bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
  5645. uint32_t property_flags = param_ast->attr & (ZEND_ACC_PPP_MASK | ZEND_ACC_READONLY);
  5646. znode var_node, default_node;
  5647. zend_uchar opcode;
  5648. zend_op *opline;
  5649. zend_arg_info *arg_info;
  5650. if (zend_is_auto_global(name)) {
  5651. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
  5652. ZSTR_VAL(name));
  5653. }
  5654. var_node.op_type = IS_CV;
  5655. var_node.u.op.var = lookup_cv(name);
  5656. if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
  5657. zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
  5658. ZSTR_VAL(name));
  5659. } else if (zend_string_equals_literal(name, "this")) {
  5660. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
  5661. }
  5662. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  5663. zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
  5664. }
  5665. if (is_variadic) {
  5666. opcode = ZEND_RECV_VARIADIC;
  5667. default_node.op_type = IS_UNUSED;
  5668. op_array->fn_flags |= ZEND_ACC_VARIADIC;
  5669. if (*default_ast_ptr) {
  5670. zend_error_noreturn(E_COMPILE_ERROR,
  5671. "Variadic parameter cannot have a default value");
  5672. }
  5673. } else if (*default_ast_ptr) {
  5674. /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
  5675. uint32_t cops = CG(compiler_options);
  5676. CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
  5677. opcode = ZEND_RECV_INIT;
  5678. default_node.op_type = IS_CONST;
  5679. zend_const_expr_to_zval(
  5680. &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
  5681. CG(compiler_options) = cops;
  5682. if (last_required_param != (uint32_t) -1 && i < last_required_param) {
  5683. /* Ignore parameters of the form "Type $param = null".
  5684. * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
  5685. bool is_implicit_nullable =
  5686. type_ast && !(type_ast->attr & ZEND_TYPE_NULLABLE)
  5687. && Z_TYPE(default_node.u.constant) == IS_NULL;
  5688. if (!is_implicit_nullable) {
  5689. zend_ast *required_param_ast = list->child[last_required_param];
  5690. zend_error(E_DEPRECATED,
  5691. "Optional parameter $%s declared before required parameter $%s "
  5692. "is implicitly treated as a required parameter",
  5693. ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
  5694. }
  5695. /* Regardless of whether we issue a deprecation, convert this parameter into
  5696. * a required parameter without a default value. This ensures that it cannot be
  5697. * used as an optional parameter even with named parameters. */
  5698. opcode = ZEND_RECV;
  5699. default_node.op_type = IS_UNUSED;
  5700. zval_ptr_dtor(&default_node.u.constant);
  5701. }
  5702. } else {
  5703. opcode = ZEND_RECV;
  5704. default_node.op_type = IS_UNUSED;
  5705. op_array->required_num_args = i + 1;
  5706. }
  5707. arg_info = &arg_infos[i];
  5708. arg_info->name = zend_string_copy(name);
  5709. arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
  5710. if (attributes_ast) {
  5711. zend_compile_attributes(&op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER);
  5712. }
  5713. if (type_ast) {
  5714. uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
  5715. bool force_nullable = default_type == IS_NULL && !property_flags;
  5716. op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  5717. arg_info->type = zend_compile_typename(type_ast, force_nullable);
  5718. if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
  5719. zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
  5720. }
  5721. if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
  5722. zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
  5723. }
  5724. if (force_nullable && ZEND_TYPE_IS_INTERSECTION(arg_info->type)) {
  5725. zend_string *type_str = zend_type_to_string(arg_info->type);
  5726. zend_error_noreturn(E_COMPILE_ERROR,
  5727. "Cannot use null as default value for parameter $%s of type %s",
  5728. /* We move type_str pointer one char forward to skip the '?' generated by
  5729. * the call to zend_compile_typename() */
  5730. ZSTR_VAL(name), ZSTR_VAL(type_str)+1);
  5731. }
  5732. if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
  5733. && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
  5734. zend_string *type_str = zend_type_to_string(arg_info->type);
  5735. zend_error_noreturn(E_COMPILE_ERROR,
  5736. "Cannot use %s as default value for parameter $%s of type %s",
  5737. zend_get_type_by_const(default_type),
  5738. ZSTR_VAL(name), ZSTR_VAL(type_str));
  5739. }
  5740. }
  5741. opline = zend_emit_op(NULL, opcode, NULL, &default_node);
  5742. SET_NODE(opline->result, &var_node);
  5743. opline->op1.num = i + 1;
  5744. if (type_ast) {
  5745. /* Allocate cache slot to speed-up run-time class resolution */
  5746. opline->extended_value =
  5747. zend_alloc_cache_slots(zend_type_get_num_classes(arg_info->type));
  5748. }
  5749. uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
  5750. | (property_flags ? _ZEND_IS_PROMOTED_BIT : 0);
  5751. ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
  5752. if (opcode == ZEND_RECV) {
  5753. opline->op2.num = type_ast ?
  5754. ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
  5755. }
  5756. if (property_flags) {
  5757. zend_op_array *op_array = CG(active_op_array);
  5758. zend_class_entry *scope = op_array->scope;
  5759. bool is_ctor =
  5760. scope && zend_is_constructor(op_array->function_name);
  5761. if (!is_ctor) {
  5762. zend_error_noreturn(E_COMPILE_ERROR,
  5763. "Cannot declare promoted property outside a constructor");
  5764. }
  5765. if ((op_array->fn_flags & ZEND_ACC_ABSTRACT)
  5766. || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
  5767. zend_error_noreturn(E_COMPILE_ERROR,
  5768. "Cannot declare promoted property in an abstract constructor");
  5769. }
  5770. if (is_variadic) {
  5771. zend_error_noreturn(E_COMPILE_ERROR,
  5772. "Cannot declare variadic promoted property");
  5773. }
  5774. if (zend_hash_exists(&scope->properties_info, name)) {
  5775. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
  5776. ZSTR_VAL(scope->name), ZSTR_VAL(name));
  5777. }
  5778. if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
  5779. zend_string *str = zend_type_to_string(arg_info->type);
  5780. zend_error_noreturn(E_COMPILE_ERROR,
  5781. "Property %s::$%s cannot have type %s",
  5782. ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
  5783. }
  5784. /* Recompile the type, as it has different memory management requirements. */
  5785. zend_type type = ZEND_TYPE_INIT_NONE(0);
  5786. if (type_ast) {
  5787. type = zend_compile_typename(type_ast, /* force_allow_null */ 0);
  5788. }
  5789. /* Don't give the property an explicit default value. For typed properties this means
  5790. * uninitialized, for untyped properties it means an implicit null default value. */
  5791. zval default_value;
  5792. if (ZEND_TYPE_IS_SET(type)) {
  5793. ZVAL_UNDEF(&default_value);
  5794. } else {
  5795. if (property_flags & ZEND_ACC_READONLY) {
  5796. zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
  5797. ZSTR_VAL(scope->name), ZSTR_VAL(name));
  5798. }
  5799. ZVAL_NULL(&default_value);
  5800. }
  5801. zend_string *doc_comment =
  5802. doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
  5803. zend_property_info *prop = zend_declare_typed_property(
  5804. scope, name, &default_value, property_flags | ZEND_ACC_PROMOTED, doc_comment, type);
  5805. if (attributes_ast) {
  5806. zend_compile_attributes(
  5807. &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY);
  5808. }
  5809. }
  5810. }
  5811. /* These are assigned at the end to avoid uninitialized memory in case of an error */
  5812. op_array->num_args = list->children;
  5813. op_array->arg_info = arg_infos;
  5814. /* Don't count the variadic argument */
  5815. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  5816. op_array->num_args--;
  5817. }
  5818. zend_set_function_arg_flags((zend_function*)op_array);
  5819. for (i = 0; i < list->children; i++) {
  5820. zend_ast *param_ast = list->child[i];
  5821. bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
  5822. uint32_t flags = param_ast->attr & (ZEND_ACC_PPP_MASK | ZEND_ACC_READONLY);
  5823. if (!flags) {
  5824. continue;
  5825. }
  5826. /* Emit $this->prop = $prop for promoted properties. */
  5827. zend_string *name = zend_ast_get_str(param_ast->child[1]);
  5828. znode name_node, value_node;
  5829. name_node.op_type = IS_CONST;
  5830. ZVAL_STR_COPY(&name_node.u.constant, name);
  5831. value_node.op_type = IS_CV;
  5832. value_node.u.op.var = lookup_cv(name);
  5833. zend_op *opline = zend_emit_op(NULL,
  5834. is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
  5835. opline->extended_value = zend_alloc_cache_slots(3);
  5836. zend_emit_op_data(&value_node);
  5837. }
  5838. }
  5839. /* }}} */
  5840. static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
  5841. {
  5842. zend_ast_list *list = zend_ast_get_list(uses_ast);
  5843. uint32_t i;
  5844. if (!list->children) {
  5845. return;
  5846. }
  5847. if (!op_array->static_variables) {
  5848. op_array->static_variables = zend_new_array(8);
  5849. }
  5850. for (i = 0; i < list->children; ++i) {
  5851. zend_ast *var_name_ast = list->child[i];
  5852. zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
  5853. uint32_t mode = var_name_ast->attr;
  5854. zend_op *opline;
  5855. zval *value;
  5856. if (zend_string_equals_literal(var_name, "this")) {
  5857. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
  5858. }
  5859. if (zend_is_auto_global(var_name)) {
  5860. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
  5861. }
  5862. value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
  5863. if (!value) {
  5864. zend_error_noreturn(E_COMPILE_ERROR,
  5865. "Cannot use variable $%s twice", ZSTR_VAL(var_name));
  5866. }
  5867. CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
  5868. opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
  5869. opline->op2_type = IS_CV;
  5870. opline->op2.var = lookup_cv(var_name);
  5871. opline->extended_value =
  5872. (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
  5873. }
  5874. }
  5875. /* }}} */
  5876. typedef struct {
  5877. HashTable uses;
  5878. bool varvars_used;
  5879. } closure_info;
  5880. static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
  5881. if (!ast) {
  5882. return;
  5883. }
  5884. if (ast->kind == ZEND_AST_VAR) {
  5885. zend_ast *name_ast = ast->child[0];
  5886. if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
  5887. zend_string *name = zend_ast_get_str(name_ast);
  5888. if (zend_is_auto_global(name)) {
  5889. /* These is no need to explicitly import auto-globals. */
  5890. return;
  5891. }
  5892. if (zend_string_equals_literal(name, "this")) {
  5893. /* $this does not need to be explicitly imported. */
  5894. return;
  5895. }
  5896. zend_hash_add_empty_element(&info->uses, name);
  5897. } else {
  5898. info->varvars_used = 1;
  5899. find_implicit_binds_recursively(info, name_ast);
  5900. }
  5901. } else if (zend_ast_is_list(ast)) {
  5902. zend_ast_list *list = zend_ast_get_list(ast);
  5903. uint32_t i;
  5904. for (i = 0; i < list->children; i++) {
  5905. find_implicit_binds_recursively(info, list->child[i]);
  5906. }
  5907. } else if (ast->kind == ZEND_AST_CLOSURE) {
  5908. /* For normal closures add the use() list. */
  5909. zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
  5910. zend_ast *uses_ast = closure_ast->child[1];
  5911. if (uses_ast) {
  5912. zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
  5913. uint32_t i;
  5914. for (i = 0; i < uses_list->children; i++) {
  5915. zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
  5916. }
  5917. }
  5918. } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
  5919. /* For arrow functions recursively check the expression. */
  5920. zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
  5921. find_implicit_binds_recursively(info, closure_ast->child[2]);
  5922. } else if (!zend_ast_is_special(ast)) {
  5923. uint32_t i, children = zend_ast_get_num_children(ast);
  5924. for (i = 0; i < children; i++) {
  5925. find_implicit_binds_recursively(info, ast->child[i]);
  5926. }
  5927. }
  5928. }
  5929. static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
  5930. {
  5931. zend_ast_list *param_list = zend_ast_get_list(params_ast);
  5932. uint32_t i;
  5933. zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
  5934. find_implicit_binds_recursively(info, stmt_ast);
  5935. /* Remove variables that are parameters */
  5936. for (i = 0; i < param_list->children; i++) {
  5937. zend_ast *param_ast = param_list->child[i];
  5938. zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
  5939. }
  5940. }
  5941. static void compile_implicit_lexical_binds(
  5942. closure_info *info, znode *closure, zend_op_array *op_array)
  5943. {
  5944. zend_string *var_name;
  5945. zend_op *opline;
  5946. /* TODO We might want to use a special binding mode if varvars_used is set. */
  5947. if (zend_hash_num_elements(&info->uses) == 0) {
  5948. return;
  5949. }
  5950. if (!op_array->static_variables) {
  5951. op_array->static_variables = zend_new_array(8);
  5952. }
  5953. ZEND_HASH_FOREACH_STR_KEY(&info->uses, var_name)
  5954. zval *value = zend_hash_add(
  5955. op_array->static_variables, var_name, &EG(uninitialized_zval));
  5956. uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
  5957. opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
  5958. opline->op2_type = IS_CV;
  5959. opline->op2.var = lookup_cv(var_name);
  5960. opline->extended_value = offset | ZEND_BIND_IMPLICIT;
  5961. ZEND_HASH_FOREACH_END();
  5962. }
  5963. static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
  5964. {
  5965. zend_op_array *op_array = CG(active_op_array);
  5966. zend_ast_list *list = zend_ast_get_list(ast);
  5967. uint32_t i;
  5968. for (i = 0; i < list->children; ++i) {
  5969. uint32_t mode = ZEND_BIND_EXPLICIT;
  5970. zend_ast *var_ast = list->child[i];
  5971. zend_string *var_name = zend_ast_get_str(var_ast);
  5972. zval zv;
  5973. ZVAL_NULL(&zv);
  5974. {
  5975. int i;
  5976. for (i = 0; i < op_array->last_var; i++) {
  5977. if (zend_string_equals(op_array->vars[i], var_name)) {
  5978. zend_error_noreturn(E_COMPILE_ERROR,
  5979. "Cannot use lexical variable $%s as a parameter name", ZSTR_VAL(var_name));
  5980. }
  5981. }
  5982. }
  5983. CG(zend_lineno) = zend_ast_get_lineno(var_ast);
  5984. if (var_ast->attr) {
  5985. mode |= ZEND_BIND_REF;
  5986. }
  5987. zend_compile_static_var_common(var_name, &zv, mode);
  5988. }
  5989. }
  5990. /* }}} */
  5991. static void zend_compile_implicit_closure_uses(closure_info *info)
  5992. {
  5993. zend_string *var_name;
  5994. ZEND_HASH_FOREACH_STR_KEY(&info->uses, var_name)
  5995. zval zv;
  5996. ZVAL_NULL(&zv);
  5997. zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
  5998. ZEND_HASH_FOREACH_END();
  5999. }
  6000. static void add_stringable_interface(zend_class_entry *ce) {
  6001. for (uint32_t i = 0; i < ce->num_interfaces; i++) {
  6002. if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
  6003. /* Interface already explicitly implemented */
  6004. return;
  6005. }
  6006. }
  6007. ce->num_interfaces++;
  6008. ce->interface_names =
  6009. erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
  6010. // TODO: Add known interned strings instead?
  6011. ce->interface_names[ce->num_interfaces - 1].name =
  6012. zend_string_init("Stringable", sizeof("Stringable") - 1, 0);
  6013. ce->interface_names[ce->num_interfaces - 1].lc_name =
  6014. zend_string_init("stringable", sizeof("stringable") - 1, 0);
  6015. }
  6016. static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
  6017. {
  6018. zend_class_entry *ce = CG(active_class_entry);
  6019. bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
  6020. uint32_t fn_flags = op_array->fn_flags;
  6021. zend_string *lcname;
  6022. if (fn_flags & ZEND_ACC_READONLY) {
  6023. zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
  6024. }
  6025. if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
  6026. zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
  6027. }
  6028. if (in_interface) {
  6029. if (!(fn_flags & ZEND_ACC_PUBLIC)) {
  6030. zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
  6031. "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6032. }
  6033. if (fn_flags & ZEND_ACC_FINAL) {
  6034. zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
  6035. "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6036. }
  6037. if (fn_flags & ZEND_ACC_ABSTRACT) {
  6038. zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
  6039. "%s::%s() must not be abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6040. }
  6041. op_array->fn_flags |= ZEND_ACC_ABSTRACT;
  6042. }
  6043. if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
  6044. if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
  6045. zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
  6046. in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6047. }
  6048. if (has_body) {
  6049. zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
  6050. in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6051. }
  6052. ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  6053. } else if (!has_body) {
  6054. zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
  6055. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6056. }
  6057. op_array->scope = ce;
  6058. op_array->function_name = zend_string_copy(name);
  6059. lcname = zend_string_tolower(name);
  6060. lcname = zend_new_interned_string(lcname);
  6061. if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
  6062. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
  6063. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6064. }
  6065. zend_add_magic_method(ce, (zend_function *) op_array, lcname);
  6066. if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
  6067. && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
  6068. add_stringable_interface(ce);
  6069. }
  6070. return lcname;
  6071. }
  6072. /* }}} */
  6073. static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
  6074. zend_op_array *op_array = CG(active_op_array);
  6075. uint32_t def_offset = op_array->num_dynamic_func_defs++;
  6076. op_array->dynamic_func_defs = erealloc(
  6077. op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
  6078. op_array->dynamic_func_defs[def_offset] = def;
  6079. return def_offset;
  6080. }
  6081. static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl, bool toplevel) /* {{{ */
  6082. {
  6083. zend_string *unqualified_name, *name, *lcname;
  6084. zend_op *opline;
  6085. unqualified_name = decl->name;
  6086. op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
  6087. lcname = zend_string_tolower(name);
  6088. if (FC(imports_function)) {
  6089. zend_string *import_name =
  6090. zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
  6091. if (import_name && !zend_string_equals_ci(lcname, import_name)) {
  6092. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
  6093. "because the name is already in use", ZSTR_VAL(name));
  6094. }
  6095. }
  6096. if (zend_string_equals_literal(lcname, "__autoload")) {
  6097. zend_error_noreturn(E_COMPILE_ERROR,
  6098. "__autoload() is no longer supported, use spl_autoload_register() instead");
  6099. }
  6100. if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
  6101. zend_error(E_COMPILE_ERROR,
  6102. "Defining a custom assert() function is not allowed, "
  6103. "as the function has special semantics");
  6104. }
  6105. zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
  6106. if (toplevel) {
  6107. if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
  6108. do_bind_function_error(lcname, op_array, 1);
  6109. }
  6110. zend_string_release_ex(lcname, 0);
  6111. return;
  6112. }
  6113. uint32_t func_ref = zend_add_dynamic_func_def(op_array);
  6114. if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
  6115. opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
  6116. opline->op2.num = func_ref;
  6117. } else {
  6118. opline = get_next_op();
  6119. opline->opcode = ZEND_DECLARE_FUNCTION;
  6120. opline->op1_type = IS_CONST;
  6121. LITERAL_STR(opline->op1, zend_string_copy(lcname));
  6122. opline->op2.num = func_ref;
  6123. }
  6124. zend_string_release_ex(lcname, 0);
  6125. }
  6126. /* }}} */
  6127. static void zend_compile_func_decl(znode *result, zend_ast *ast, bool toplevel) /* {{{ */
  6128. {
  6129. zend_ast_decl *decl = (zend_ast_decl *) ast;
  6130. zend_ast *params_ast = decl->child[0];
  6131. zend_ast *uses_ast = decl->child[1];
  6132. zend_ast *stmt_ast = decl->child[2];
  6133. zend_ast *return_type_ast = decl->child[3];
  6134. bool is_method = decl->kind == ZEND_AST_METHOD;
  6135. zend_string *method_lcname = NULL;
  6136. zend_class_entry *orig_class_entry = CG(active_class_entry);
  6137. zend_op_array *orig_op_array = CG(active_op_array);
  6138. zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
  6139. zend_oparray_context orig_oparray_context;
  6140. closure_info info;
  6141. memset(&info, 0, sizeof(closure_info));
  6142. init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
  6143. if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
  6144. op_array->fn_flags |= ZEND_ACC_PRELOADED;
  6145. }
  6146. ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void *)));
  6147. ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
  6148. op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
  6149. op_array->fn_flags |= decl->flags;
  6150. op_array->line_start = decl->start_lineno;
  6151. op_array->line_end = decl->end_lineno;
  6152. if (decl->doc_comment) {
  6153. op_array->doc_comment = zend_string_copy(decl->doc_comment);
  6154. }
  6155. if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
  6156. op_array->fn_flags |= ZEND_ACC_CLOSURE;
  6157. }
  6158. if (is_method) {
  6159. bool has_body = stmt_ast != NULL;
  6160. method_lcname = zend_begin_method_decl(op_array, decl->name, has_body);
  6161. } else {
  6162. zend_begin_func_decl(result, op_array, decl, toplevel);
  6163. if (decl->kind == ZEND_AST_ARROW_FUNC) {
  6164. find_implicit_binds(&info, params_ast, stmt_ast);
  6165. compile_implicit_lexical_binds(&info, result, op_array);
  6166. } else if (uses_ast) {
  6167. zend_compile_closure_binding(result, op_array, uses_ast);
  6168. }
  6169. }
  6170. CG(active_op_array) = op_array;
  6171. if (decl->child[4]) {
  6172. int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
  6173. if (is_method) {
  6174. target = ZEND_ATTRIBUTE_TARGET_METHOD;
  6175. }
  6176. zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target);
  6177. }
  6178. /* Do not leak the class scope into free standing functions, even if they are dynamically
  6179. * defined inside a class method. This is necessary for correct handling of magic constants.
  6180. * For example __CLASS__ should always be "" inside a free standing function. */
  6181. if (decl->kind == ZEND_AST_FUNC_DECL) {
  6182. CG(active_class_entry) = NULL;
  6183. }
  6184. if (toplevel) {
  6185. op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
  6186. }
  6187. zend_oparray_context_begin(&orig_oparray_context);
  6188. {
  6189. /* Push a separator to the loop variable stack */
  6190. zend_loop_var dummy_var;
  6191. dummy_var.opcode = ZEND_RETURN;
  6192. zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
  6193. }
  6194. zend_compile_params(params_ast, return_type_ast,
  6195. is_method && zend_string_equals_literal(method_lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
  6196. if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
  6197. zend_mark_function_as_generator();
  6198. zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
  6199. }
  6200. if (decl->kind == ZEND_AST_ARROW_FUNC) {
  6201. zend_compile_implicit_closure_uses(&info);
  6202. zend_hash_destroy(&info.uses);
  6203. } else if (uses_ast) {
  6204. zend_compile_closure_uses(uses_ast);
  6205. }
  6206. zend_compile_stmt(stmt_ast);
  6207. if (is_method) {
  6208. CG(zend_lineno) = decl->start_lineno;
  6209. zend_check_magic_method_implementation(
  6210. CG(active_class_entry), (zend_function *) op_array, method_lcname, E_COMPILE_ERROR);
  6211. zend_string_release_ex(method_lcname, 0);
  6212. }
  6213. /* put the implicit return on the really last line */
  6214. CG(zend_lineno) = decl->end_lineno;
  6215. zend_do_extended_stmt();
  6216. zend_emit_final_return(0);
  6217. zend_init_static_variables_map_ptr(op_array);
  6218. pass_two(CG(active_op_array));
  6219. zend_oparray_context_end(&orig_oparray_context);
  6220. /* Pop the loop variable stack separator */
  6221. zend_stack_del_top(&CG(loop_var_stack));
  6222. CG(active_op_array) = orig_op_array;
  6223. CG(active_class_entry) = orig_class_entry;
  6224. }
  6225. /* }}} */
  6226. static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
  6227. {
  6228. zend_ast_list *list = zend_ast_get_list(ast);
  6229. zend_class_entry *ce = CG(active_class_entry);
  6230. uint32_t i, children = list->children;
  6231. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  6232. zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include properties");
  6233. }
  6234. if (ce->ce_flags & ZEND_ACC_ENUM) {
  6235. zend_error_noreturn(E_COMPILE_ERROR, "Enums may not include properties");
  6236. }
  6237. if (flags & ZEND_ACC_ABSTRACT) {
  6238. zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
  6239. }
  6240. for (i = 0; i < children; ++i) {
  6241. zend_property_info *info;
  6242. zend_ast *prop_ast = list->child[i];
  6243. zend_ast *name_ast = prop_ast->child[0];
  6244. zend_ast **value_ast_ptr = &prop_ast->child[1];
  6245. zend_ast *doc_comment_ast = prop_ast->child[2];
  6246. zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
  6247. zend_string *doc_comment = NULL;
  6248. zval value_zv;
  6249. zend_type type = ZEND_TYPE_INIT_NONE(0);
  6250. if (type_ast) {
  6251. type = zend_compile_typename(type_ast, /* force_allow_null */ 0);
  6252. if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
  6253. zend_string *str = zend_type_to_string(type);
  6254. zend_error_noreturn(E_COMPILE_ERROR,
  6255. "Property %s::$%s cannot have type %s",
  6256. ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
  6257. }
  6258. }
  6259. /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
  6260. if (doc_comment_ast) {
  6261. doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
  6262. }
  6263. if (flags & ZEND_ACC_FINAL) {
  6264. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
  6265. "the final modifier is allowed only for methods, classes, and class constants",
  6266. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6267. }
  6268. if (zend_hash_exists(&ce->properties_info, name)) {
  6269. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
  6270. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6271. }
  6272. if (*value_ast_ptr) {
  6273. zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
  6274. if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
  6275. && !zend_is_valid_default_value(type, &value_zv)) {
  6276. zend_string *str = zend_type_to_string(type);
  6277. if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
  6278. ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
  6279. zend_string *nullable_str = zend_type_to_string(type);
  6280. zend_error_noreturn(E_COMPILE_ERROR,
  6281. "Default value for property of type %s may not be null. "
  6282. "Use the nullable type %s to allow null default value",
  6283. ZSTR_VAL(str), ZSTR_VAL(nullable_str));
  6284. } else {
  6285. zend_error_noreturn(E_COMPILE_ERROR,
  6286. "Cannot use %s as default value for property %s::$%s of type %s",
  6287. zend_zval_type_name(&value_zv),
  6288. ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
  6289. }
  6290. }
  6291. } else if (!ZEND_TYPE_IS_SET(type)) {
  6292. ZVAL_NULL(&value_zv);
  6293. } else {
  6294. ZVAL_UNDEF(&value_zv);
  6295. }
  6296. if (flags & ZEND_ACC_READONLY) {
  6297. if (!ZEND_TYPE_IS_SET(type)) {
  6298. zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
  6299. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6300. }
  6301. if (!Z_ISUNDEF(value_zv)) {
  6302. zend_error_noreturn(E_COMPILE_ERROR,
  6303. "Readonly property %s::$%s cannot have default value",
  6304. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6305. }
  6306. if (flags & ZEND_ACC_STATIC) {
  6307. zend_error_noreturn(E_COMPILE_ERROR,
  6308. "Static property %s::$%s cannot be readonly",
  6309. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  6310. }
  6311. }
  6312. info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
  6313. if (attr_ast) {
  6314. zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY);
  6315. }
  6316. }
  6317. }
  6318. /* }}} */
  6319. static void zend_compile_prop_group(zend_ast *ast) /* {{{ */
  6320. {
  6321. zend_ast *type_ast = ast->child[0];
  6322. zend_ast *prop_ast = ast->child[1];
  6323. zend_ast *attr_ast = ast->child[2];
  6324. zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
  6325. }
  6326. /* }}} */
  6327. static void zend_check_const_and_trait_alias_attr(uint32_t attr, const char* entity) /* {{{ */
  6328. {
  6329. if (attr & ZEND_ACC_STATIC) {
  6330. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as %s modifier", entity);
  6331. } else if (attr & ZEND_ACC_ABSTRACT) {
  6332. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as %s modifier", entity);
  6333. } else if (attr & ZEND_ACC_FINAL) {
  6334. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as %s modifier", entity);
  6335. } else if (attr & ZEND_ACC_READONLY) {
  6336. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'readonly' as %s modifier", entity);
  6337. }
  6338. }
  6339. /* }}} */
  6340. static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
  6341. {
  6342. zend_ast_list *list = zend_ast_get_list(ast);
  6343. zend_class_entry *ce = CG(active_class_entry);
  6344. uint32_t i, children = list->children;
  6345. if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  6346. zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
  6347. return;
  6348. }
  6349. for (i = 0; i < children; ++i) {
  6350. zend_class_constant *c;
  6351. zend_ast *const_ast = list->child[i];
  6352. zend_ast *name_ast = const_ast->child[0];
  6353. zend_ast **value_ast_ptr = &const_ast->child[1];
  6354. zend_ast *doc_comment_ast = const_ast->child[2];
  6355. zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
  6356. zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
  6357. zval value_zv;
  6358. if (UNEXPECTED(flags & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_READONLY))) {
  6359. zend_check_const_and_trait_alias_attr(flags, "constant");
  6360. }
  6361. if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
  6362. zend_error_noreturn(
  6363. E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
  6364. ZSTR_VAL(ce->name), ZSTR_VAL(name)
  6365. );
  6366. }
  6367. zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
  6368. c = zend_declare_class_constant_ex(ce, name, &value_zv, flags, doc_comment);
  6369. if (attr_ast) {
  6370. zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
  6371. }
  6372. }
  6373. }
  6374. /* }}} */
  6375. static void zend_compile_class_const_group(zend_ast *ast) /* {{{ */
  6376. {
  6377. zend_ast *const_ast = ast->child[0];
  6378. zend_ast *attr_ast = ast->child[1];
  6379. zend_compile_class_const_decl(const_ast, ast->attr, attr_ast);
  6380. }
  6381. /* }}} */
  6382. static void zend_compile_method_ref(zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
  6383. {
  6384. zend_ast *class_ast = ast->child[0];
  6385. zend_ast *method_ast = ast->child[1];
  6386. method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
  6387. if (class_ast) {
  6388. method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
  6389. } else {
  6390. method_ref->class_name = NULL;
  6391. }
  6392. }
  6393. /* }}} */
  6394. static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
  6395. {
  6396. zend_ast *method_ref_ast = ast->child[0];
  6397. zend_ast *insteadof_ast = ast->child[1];
  6398. zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
  6399. uint32_t i;
  6400. zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
  6401. zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
  6402. precedence->num_excludes = insteadof_list->children;
  6403. for (i = 0; i < insteadof_list->children; ++i) {
  6404. zend_ast *name_ast = insteadof_list->child[i];
  6405. precedence->exclude_class_names[i] =
  6406. zend_resolve_const_class_name_reference(name_ast, "trait name");
  6407. }
  6408. zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
  6409. }
  6410. /* }}} */
  6411. static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
  6412. {
  6413. zend_ast *method_ref_ast = ast->child[0];
  6414. zend_ast *alias_ast = ast->child[1];
  6415. uint32_t modifiers = ast->attr;
  6416. zend_trait_alias *alias;
  6417. zend_check_const_and_trait_alias_attr(modifiers, "method");
  6418. alias = emalloc(sizeof(zend_trait_alias));
  6419. zend_compile_method_ref(method_ref_ast, &alias->trait_method);
  6420. alias->modifiers = modifiers;
  6421. if (alias_ast) {
  6422. alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
  6423. } else {
  6424. alias->alias = NULL;
  6425. }
  6426. zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
  6427. }
  6428. /* }}} */
  6429. static void zend_compile_use_trait(zend_ast *ast) /* {{{ */
  6430. {
  6431. zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
  6432. zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
  6433. zend_class_entry *ce = CG(active_class_entry);
  6434. uint32_t i;
  6435. ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
  6436. for (i = 0; i < traits->children; ++i) {
  6437. zend_ast *trait_ast = traits->child[i];
  6438. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  6439. zend_string *name = zend_ast_get_str(trait_ast);
  6440. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
  6441. "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
  6442. }
  6443. ce->trait_names[ce->num_traits].name =
  6444. zend_resolve_const_class_name_reference(trait_ast, "trait name");
  6445. ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
  6446. ce->num_traits++;
  6447. }
  6448. if (!adaptations) {
  6449. return;
  6450. }
  6451. for (i = 0; i < adaptations->children; ++i) {
  6452. zend_ast *adaptation_ast = adaptations->child[i];
  6453. switch (adaptation_ast->kind) {
  6454. case ZEND_AST_TRAIT_PRECEDENCE:
  6455. zend_compile_trait_precedence(adaptation_ast);
  6456. break;
  6457. case ZEND_AST_TRAIT_ALIAS:
  6458. zend_compile_trait_alias(adaptation_ast);
  6459. break;
  6460. EMPTY_SWITCH_DEFAULT_CASE()
  6461. }
  6462. }
  6463. }
  6464. /* }}} */
  6465. static void zend_compile_implements(zend_ast *ast) /* {{{ */
  6466. {
  6467. zend_ast_list *list = zend_ast_get_list(ast);
  6468. zend_class_entry *ce = CG(active_class_entry);
  6469. zend_class_name *interface_names;
  6470. uint32_t i;
  6471. interface_names = emalloc(sizeof(zend_class_name) * list->children);
  6472. for (i = 0; i < list->children; ++i) {
  6473. zend_ast *class_ast = list->child[i];
  6474. interface_names[i].name =
  6475. zend_resolve_const_class_name_reference(class_ast, "interface name");
  6476. interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
  6477. }
  6478. ce->num_interfaces = list->children;
  6479. ce->interface_names = interface_names;
  6480. }
  6481. /* }}} */
  6482. static zend_string *zend_generate_anon_class_name(zend_ast_decl *decl)
  6483. {
  6484. zend_string *filename = CG(active_op_array)->filename;
  6485. uint32_t start_lineno = decl->start_lineno;
  6486. /* Use parent or first interface as prefix. */
  6487. zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
  6488. if (decl->child[0]) {
  6489. prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
  6490. } else if (decl->child[1]) {
  6491. zend_ast_list *list = zend_ast_get_list(decl->child[1]);
  6492. prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
  6493. }
  6494. zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
  6495. ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
  6496. zend_string_release(prefix);
  6497. return zend_new_interned_string(result);
  6498. }
  6499. static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
  6500. {
  6501. ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
  6502. zend_type type = zend_compile_typename(enum_backing_type_ast, 0);
  6503. uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
  6504. if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
  6505. zend_string *type_string = zend_type_to_string(type);
  6506. zend_error_noreturn(E_COMPILE_ERROR,
  6507. "Enum backing type must be int or string, %s given",
  6508. ZSTR_VAL(type_string));
  6509. }
  6510. if (type_mask == MAY_BE_LONG) {
  6511. ce->enum_backing_type = IS_LONG;
  6512. } else {
  6513. ZEND_ASSERT(type_mask == MAY_BE_STRING);
  6514. ce->enum_backing_type = IS_STRING;
  6515. }
  6516. zend_type_release(type, 0);
  6517. ce->backed_enum_table = emalloc(sizeof(HashTable));
  6518. zend_hash_init(ce->backed_enum_table, 0, NULL, ZVAL_PTR_DTOR, 0);
  6519. }
  6520. static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel) /* {{{ */
  6521. {
  6522. zend_ast_decl *decl = (zend_ast_decl *) ast;
  6523. zend_ast *extends_ast = decl->child[0];
  6524. zend_ast *implements_ast = decl->child[1];
  6525. zend_ast *stmt_ast = decl->child[2];
  6526. zend_ast *enum_backing_type_ast = decl->child[4];
  6527. zend_string *name, *lcname;
  6528. zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
  6529. zend_op *opline;
  6530. zend_class_entry *original_ce = CG(active_class_entry);
  6531. if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
  6532. zend_string *unqualified_name = decl->name;
  6533. if (CG(active_class_entry)) {
  6534. zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
  6535. }
  6536. zend_assert_valid_class_name(unqualified_name);
  6537. name = zend_prefix_with_ns(unqualified_name);
  6538. name = zend_new_interned_string(name);
  6539. lcname = zend_string_tolower(name);
  6540. if (FC(imports)) {
  6541. zend_string *import_name =
  6542. zend_hash_find_ptr_lc(FC(imports), unqualified_name);
  6543. if (import_name && !zend_string_equals_ci(lcname, import_name)) {
  6544. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
  6545. "because the name is already in use", ZSTR_VAL(name));
  6546. }
  6547. }
  6548. zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
  6549. } else {
  6550. /* Find an anon class name that is not in use yet. */
  6551. name = NULL;
  6552. lcname = NULL;
  6553. do {
  6554. zend_tmp_string_release(name);
  6555. zend_tmp_string_release(lcname);
  6556. name = zend_generate_anon_class_name(decl);
  6557. lcname = zend_string_tolower(name);
  6558. } while (zend_hash_exists(CG(class_table), lcname));
  6559. }
  6560. lcname = zend_new_interned_string(lcname);
  6561. ce->type = ZEND_USER_CLASS;
  6562. ce->name = name;
  6563. zend_initialize_class_data(ce, 1);
  6564. if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
  6565. zend_alloc_ce_cache(ce->name);
  6566. }
  6567. if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
  6568. ce->ce_flags |= ZEND_ACC_PRELOADED;
  6569. ZEND_MAP_PTR_NEW(ce->static_members_table);
  6570. ZEND_MAP_PTR_NEW(ce->mutable_data);
  6571. }
  6572. ce->ce_flags |= decl->flags;
  6573. ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
  6574. ce->info.user.line_start = decl->start_lineno;
  6575. ce->info.user.line_end = decl->end_lineno;
  6576. if (decl->doc_comment) {
  6577. ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
  6578. }
  6579. if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
  6580. /* Serialization is not supported for anonymous classes */
  6581. ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
  6582. }
  6583. if (extends_ast) {
  6584. ce->parent_name =
  6585. zend_resolve_const_class_name_reference(extends_ast, "class name");
  6586. }
  6587. CG(active_class_entry) = ce;
  6588. if (decl->child[3]) {
  6589. zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS);
  6590. }
  6591. if (implements_ast) {
  6592. zend_compile_implements(implements_ast);
  6593. }
  6594. if (ce->ce_flags & ZEND_ACC_ENUM) {
  6595. if (enum_backing_type_ast != NULL) {
  6596. zend_compile_enum_backing_type(ce, enum_backing_type_ast);
  6597. }
  6598. zend_enum_add_interfaces(ce);
  6599. zend_enum_register_props(ce);
  6600. }
  6601. zend_compile_stmt(stmt_ast);
  6602. /* Reset lineno for final opcodes and errors */
  6603. CG(zend_lineno) = ast->lineno;
  6604. if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
  6605. zend_verify_abstract_class(ce);
  6606. }
  6607. CG(active_class_entry) = original_ce;
  6608. if (toplevel) {
  6609. ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
  6610. }
  6611. /* We currently don't early-bind classes that implement interfaces or use traits */
  6612. if (!ce->num_interfaces && !ce->num_traits
  6613. && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
  6614. if (toplevel) {
  6615. if (extends_ast) {
  6616. zend_class_entry *parent_ce = zend_lookup_class_ex(
  6617. ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  6618. if (parent_ce
  6619. && ((parent_ce->type != ZEND_INTERNAL_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES))
  6620. && ((parent_ce->type != ZEND_USER_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) || (parent_ce->info.user.filename == ce->info.user.filename))) {
  6621. if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
  6622. zend_string_release(lcname);
  6623. return;
  6624. }
  6625. }
  6626. } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
  6627. zend_string_release(lcname);
  6628. zend_build_properties_info_table(ce);
  6629. ce->ce_flags |= ZEND_ACC_LINKED;
  6630. return;
  6631. }
  6632. } else if (!extends_ast) {
  6633. /* Link unbound simple class */
  6634. zend_build_properties_info_table(ce);
  6635. ce->ce_flags |= ZEND_ACC_LINKED;
  6636. }
  6637. }
  6638. opline = get_next_op();
  6639. if (ce->parent_name) {
  6640. /* Lowercased parent name */
  6641. zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
  6642. opline->op2_type = IS_CONST;
  6643. LITERAL_STR(opline->op2, lc_parent_name);
  6644. }
  6645. opline->op1_type = IS_CONST;
  6646. LITERAL_STR(opline->op1, lcname);
  6647. if (decl->flags & ZEND_ACC_ANON_CLASS) {
  6648. opline->opcode = ZEND_DECLARE_ANON_CLASS;
  6649. opline->extended_value = zend_alloc_cache_slot();
  6650. zend_make_var_result(result, opline);
  6651. if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
  6652. /* We checked above that the class name is not used. This really shouldn't happen. */
  6653. zend_error_noreturn(E_ERROR,
  6654. "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
  6655. }
  6656. } else {
  6657. /* Generate RTD keys until we find one that isn't in use yet. */
  6658. zend_string *key = NULL;
  6659. do {
  6660. zend_tmp_string_release(key);
  6661. key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
  6662. } while (!zend_hash_add_ptr(CG(class_table), key, ce));
  6663. /* RTD key is placed after lcname literal in op1 */
  6664. zend_add_literal_string(&key);
  6665. opline->opcode = ZEND_DECLARE_CLASS;
  6666. if (extends_ast && toplevel
  6667. && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
  6668. /* We currently don't early-bind classes that implement interfaces or use traits */
  6669. && !ce->num_interfaces && !ce->num_traits
  6670. ) {
  6671. CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
  6672. opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
  6673. opline->extended_value = zend_alloc_cache_slot();
  6674. opline->result_type = IS_UNUSED;
  6675. opline->result.opline_num = -1;
  6676. }
  6677. }
  6678. }
  6679. /* }}} */
  6680. static void zend_compile_enum_case(zend_ast *ast)
  6681. {
  6682. zend_class_entry *enum_class = CG(active_class_entry);
  6683. if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
  6684. zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
  6685. }
  6686. zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
  6687. zend_string *enum_class_name = enum_class->name;
  6688. zval class_name_zval;
  6689. ZVAL_STR_COPY(&class_name_zval, enum_class_name);
  6690. zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
  6691. zval case_name_zval;
  6692. ZVAL_STR_COPY(&case_name_zval, enum_case_name);
  6693. zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
  6694. zend_ast *case_value_zval_ast = NULL;
  6695. zend_ast *case_value_ast = ast->child[1];
  6696. if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
  6697. zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
  6698. ZSTR_VAL(enum_case_name),
  6699. ZSTR_VAL(enum_class_name));
  6700. }
  6701. if (case_value_ast != NULL) {
  6702. zend_eval_const_expr(&ast->child[1]);
  6703. case_value_ast = ast->child[1];
  6704. if (case_value_ast->kind != ZEND_AST_ZVAL) {
  6705. zend_error_noreturn(
  6706. E_COMPILE_ERROR, "Enum case value must be compile-time evaluatable");
  6707. }
  6708. zval case_value_zv;
  6709. ZVAL_COPY(&case_value_zv, zend_ast_get_zval(case_value_ast));
  6710. if (enum_class->enum_backing_type == IS_UNDEF) {
  6711. if (Z_TYPE(case_value_zv) == IS_LONG || Z_TYPE(case_value_zv) == IS_STRING) {
  6712. zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value, try adding \": %s\" to the enum declaration",
  6713. ZSTR_VAL(enum_case_name),
  6714. ZSTR_VAL(enum_class_name),
  6715. zend_zval_type_name(&case_value_zv));
  6716. } else {
  6717. zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
  6718. ZSTR_VAL(enum_case_name),
  6719. ZSTR_VAL(enum_class_name));
  6720. }
  6721. }
  6722. if (enum_class->enum_backing_type != Z_TYPE(case_value_zv)) {
  6723. zend_error_noreturn(E_COMPILE_ERROR, "Enum case type %s does not match enum backing type %s",
  6724. zend_get_type_by_const(Z_TYPE(case_value_zv)),
  6725. zend_get_type_by_const(enum_class->enum_backing_type));
  6726. }
  6727. case_value_zval_ast = zend_ast_create_zval(&case_value_zv);
  6728. Z_TRY_ADDREF(case_name_zval);
  6729. if (enum_class->enum_backing_type == IS_LONG) {
  6730. zend_long long_key = Z_LVAL(case_value_zv);
  6731. zval *existing_case_name = zend_hash_index_find(enum_class->backed_enum_table, long_key);
  6732. if (existing_case_name) {
  6733. zend_error_noreturn(E_COMPILE_ERROR, "Duplicate value in enum %s for cases %s and %s",
  6734. ZSTR_VAL(enum_class_name),
  6735. Z_STRVAL_P(existing_case_name),
  6736. ZSTR_VAL(enum_case_name));
  6737. }
  6738. zend_hash_index_add_new(enum_class->backed_enum_table, long_key, &case_name_zval);
  6739. } else {
  6740. ZEND_ASSERT(enum_class->enum_backing_type == IS_STRING);
  6741. zend_string *string_key = Z_STR(case_value_zv);
  6742. zval *existing_case_name = zend_hash_find(enum_class->backed_enum_table, string_key);
  6743. if (existing_case_name != NULL) {
  6744. zend_error_noreturn(E_COMPILE_ERROR, "Duplicate value in enum %s for cases %s and %s",
  6745. ZSTR_VAL(enum_class_name),
  6746. Z_STRVAL_P(existing_case_name),
  6747. ZSTR_VAL(enum_case_name));
  6748. }
  6749. zend_hash_add_new(enum_class->backed_enum_table, string_key, &case_name_zval);
  6750. }
  6751. }
  6752. zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_zval_ast);
  6753. zval value_zv;
  6754. zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
  6755. /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
  6756. zend_ast *doc_comment_ast = ast->child[2];
  6757. zend_string *doc_comment = NULL;
  6758. if (doc_comment_ast) {
  6759. doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
  6760. }
  6761. zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
  6762. ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
  6763. zend_ast_destroy(const_enum_init_ast);
  6764. zend_ast *attr_ast = ast->child[3];
  6765. if (attr_ast) {
  6766. zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
  6767. }
  6768. }
  6769. static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
  6770. {
  6771. switch (type) {
  6772. case ZEND_SYMBOL_CLASS:
  6773. if (!FC(imports)) {
  6774. FC(imports) = emalloc(sizeof(HashTable));
  6775. zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
  6776. }
  6777. return FC(imports);
  6778. case ZEND_SYMBOL_FUNCTION:
  6779. if (!FC(imports_function)) {
  6780. FC(imports_function) = emalloc(sizeof(HashTable));
  6781. zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
  6782. }
  6783. return FC(imports_function);
  6784. case ZEND_SYMBOL_CONST:
  6785. if (!FC(imports_const)) {
  6786. FC(imports_const) = emalloc(sizeof(HashTable));
  6787. zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
  6788. }
  6789. return FC(imports_const);
  6790. EMPTY_SWITCH_DEFAULT_CASE()
  6791. }
  6792. return NULL;
  6793. }
  6794. /* }}} */
  6795. static char *zend_get_use_type_str(uint32_t type) /* {{{ */
  6796. {
  6797. switch (type) {
  6798. case ZEND_SYMBOL_CLASS:
  6799. return "";
  6800. case ZEND_SYMBOL_FUNCTION:
  6801. return " function";
  6802. case ZEND_SYMBOL_CONST:
  6803. return " const";
  6804. EMPTY_SWITCH_DEFAULT_CASE()
  6805. }
  6806. return " unknown";
  6807. }
  6808. /* }}} */
  6809. static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
  6810. {
  6811. if (zend_string_equals_ci(old_name, check_name)) {
  6812. return;
  6813. }
  6814. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
  6815. "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
  6816. }
  6817. /* }}} */
  6818. static void zend_compile_use(zend_ast *ast) /* {{{ */
  6819. {
  6820. zend_ast_list *list = zend_ast_get_list(ast);
  6821. uint32_t i;
  6822. zend_string *current_ns = FC(current_namespace);
  6823. uint32_t type = ast->attr;
  6824. HashTable *current_import = zend_get_import_ht(type);
  6825. bool case_sensitive = type == ZEND_SYMBOL_CONST;
  6826. for (i = 0; i < list->children; ++i) {
  6827. zend_ast *use_ast = list->child[i];
  6828. zend_ast *old_name_ast = use_ast->child[0];
  6829. zend_ast *new_name_ast = use_ast->child[1];
  6830. zend_string *old_name = zend_ast_get_str(old_name_ast);
  6831. zend_string *new_name, *lookup_name;
  6832. if (new_name_ast) {
  6833. new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
  6834. } else {
  6835. const char *unqualified_name;
  6836. size_t unqualified_name_len;
  6837. if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
  6838. /* The form "use A\B" is equivalent to "use A\B as B" */
  6839. new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
  6840. } else {
  6841. new_name = zend_string_copy(old_name);
  6842. if (!current_ns) {
  6843. zend_error(E_WARNING, "The use statement with non-compound name '%s' "
  6844. "has no effect", ZSTR_VAL(new_name));
  6845. }
  6846. }
  6847. }
  6848. if (case_sensitive) {
  6849. lookup_name = zend_string_copy(new_name);
  6850. } else {
  6851. lookup_name = zend_string_tolower(new_name);
  6852. }
  6853. if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
  6854. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
  6855. "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
  6856. }
  6857. if (current_ns) {
  6858. zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
  6859. zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
  6860. ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
  6861. memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
  6862. if (zend_have_seen_symbol(ns_name, type)) {
  6863. zend_check_already_in_use(type, old_name, new_name, ns_name);
  6864. }
  6865. zend_string_efree(ns_name);
  6866. } else if (zend_have_seen_symbol(lookup_name, type)) {
  6867. zend_check_already_in_use(type, old_name, new_name, lookup_name);
  6868. }
  6869. zend_string_addref(old_name);
  6870. old_name = zend_new_interned_string(old_name);
  6871. if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
  6872. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
  6873. "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
  6874. }
  6875. zend_string_release_ex(lookup_name, 0);
  6876. zend_string_release_ex(new_name, 0);
  6877. }
  6878. }
  6879. /* }}} */
  6880. static void zend_compile_group_use(zend_ast *ast) /* {{{ */
  6881. {
  6882. uint32_t i;
  6883. zend_string *ns = zend_ast_get_str(ast->child[0]);
  6884. zend_ast_list *list = zend_ast_get_list(ast->child[1]);
  6885. for (i = 0; i < list->children; i++) {
  6886. zend_ast *inline_use, *use = list->child[i];
  6887. zval *name_zval = zend_ast_get_zval(use->child[0]);
  6888. zend_string *name = Z_STR_P(name_zval);
  6889. zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
  6890. zend_string_release_ex(name, 0);
  6891. ZVAL_STR(name_zval, compound_ns);
  6892. inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
  6893. inline_use->attr = ast->attr ? ast->attr : use->attr;
  6894. zend_compile_use(inline_use);
  6895. }
  6896. }
  6897. /* }}} */
  6898. static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
  6899. {
  6900. zend_ast_list *list = zend_ast_get_list(ast);
  6901. uint32_t i;
  6902. for (i = 0; i < list->children; ++i) {
  6903. zend_ast *const_ast = list->child[i];
  6904. zend_ast *name_ast = const_ast->child[0];
  6905. zend_ast **value_ast_ptr = &const_ast->child[1];
  6906. zend_string *unqualified_name = zend_ast_get_str(name_ast);
  6907. zend_string *name;
  6908. znode name_node, value_node;
  6909. zval *value_zv = &value_node.u.constant;
  6910. value_node.op_type = IS_CONST;
  6911. zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
  6912. if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
  6913. zend_error_noreturn(E_COMPILE_ERROR,
  6914. "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
  6915. }
  6916. name = zend_prefix_with_ns(unqualified_name);
  6917. name = zend_new_interned_string(name);
  6918. if (FC(imports_const)) {
  6919. zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
  6920. if (import_name && !zend_string_equals(import_name, name)) {
  6921. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
  6922. "the name is already in use", ZSTR_VAL(name));
  6923. }
  6924. }
  6925. name_node.op_type = IS_CONST;
  6926. ZVAL_STR(&name_node.u.constant, name);
  6927. zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
  6928. zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
  6929. }
  6930. }
  6931. /* }}}*/
  6932. static void zend_compile_namespace(zend_ast *ast) /* {{{ */
  6933. {
  6934. zend_ast *name_ast = ast->child[0];
  6935. zend_ast *stmt_ast = ast->child[1];
  6936. zend_string *name;
  6937. bool with_bracket = stmt_ast != NULL;
  6938. /* handle mixed syntax declaration or nested namespaces */
  6939. if (!FC(has_bracketed_namespaces)) {
  6940. if (FC(current_namespace)) {
  6941. /* previous namespace declarations were unbracketed */
  6942. if (with_bracket) {
  6943. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
  6944. "with unbracketed namespace declarations");
  6945. }
  6946. }
  6947. } else {
  6948. /* previous namespace declarations were bracketed */
  6949. if (!with_bracket) {
  6950. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
  6951. "with unbracketed namespace declarations");
  6952. } else if (FC(current_namespace) || FC(in_namespace)) {
  6953. zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
  6954. }
  6955. }
  6956. bool is_first_namespace = (!with_bracket && !FC(current_namespace))
  6957. || (with_bracket && !FC(has_bracketed_namespaces));
  6958. if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ 1)) {
  6959. zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
  6960. "the very first statement or after any declare call in the script");
  6961. }
  6962. if (FC(current_namespace)) {
  6963. zend_string_release_ex(FC(current_namespace), 0);
  6964. }
  6965. if (name_ast) {
  6966. name = zend_ast_get_str(name_ast);
  6967. if (zend_string_equals_literal_ci(name, "namespace")) {
  6968. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
  6969. }
  6970. FC(current_namespace) = zend_string_copy(name);
  6971. } else {
  6972. FC(current_namespace) = NULL;
  6973. }
  6974. zend_reset_import_tables();
  6975. FC(in_namespace) = 1;
  6976. if (with_bracket) {
  6977. FC(has_bracketed_namespaces) = 1;
  6978. }
  6979. if (stmt_ast) {
  6980. zend_compile_top_stmt(stmt_ast);
  6981. zend_end_namespace();
  6982. }
  6983. }
  6984. /* }}} */
  6985. static void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
  6986. {
  6987. zend_ast *offset_ast = ast->child[0];
  6988. zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
  6989. zend_string *filename, *name;
  6990. const char const_name[] = "__COMPILER_HALT_OFFSET__";
  6991. if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
  6992. zend_error_noreturn(E_COMPILE_ERROR,
  6993. "__HALT_COMPILER() can only be used from the outermost scope");
  6994. }
  6995. filename = zend_get_compiled_filename();
  6996. name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
  6997. ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
  6998. zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, CONST_CS, 0);
  6999. zend_string_release_ex(name, 0);
  7000. }
  7001. /* }}} */
  7002. static bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
  7003. {
  7004. zend_op_array *op_array = CG(active_op_array);
  7005. zend_class_entry *ce = CG(active_class_entry);
  7006. switch (ast->attr) {
  7007. case T_LINE:
  7008. ZVAL_LONG(zv, ast->lineno);
  7009. break;
  7010. case T_FILE:
  7011. ZVAL_STR_COPY(zv, CG(compiled_filename));
  7012. break;
  7013. case T_DIR:
  7014. {
  7015. zend_string *filename = CG(compiled_filename);
  7016. zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
  7017. #ifdef ZEND_WIN32
  7018. ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
  7019. #else
  7020. ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
  7021. #endif
  7022. if (zend_string_equals_literal(dirname, ".")) {
  7023. dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
  7024. #if HAVE_GETCWD
  7025. ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
  7026. #elif HAVE_GETWD
  7027. ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
  7028. #endif
  7029. ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
  7030. }
  7031. ZVAL_STR(zv, dirname);
  7032. break;
  7033. }
  7034. case T_FUNC_C:
  7035. if (op_array && op_array->function_name) {
  7036. ZVAL_STR_COPY(zv, op_array->function_name);
  7037. } else {
  7038. ZVAL_EMPTY_STRING(zv);
  7039. }
  7040. break;
  7041. case T_METHOD_C:
  7042. /* Detect whether we are directly inside a class (e.g. a class constant) and treat
  7043. * this as not being inside a function. */
  7044. if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
  7045. op_array = NULL;
  7046. }
  7047. if (op_array && op_array->function_name) {
  7048. if (op_array->scope) {
  7049. ZVAL_NEW_STR(zv,
  7050. zend_create_member_string(op_array->scope->name, op_array->function_name));
  7051. } else {
  7052. ZVAL_STR_COPY(zv, op_array->function_name);
  7053. }
  7054. } else {
  7055. ZVAL_EMPTY_STRING(zv);
  7056. }
  7057. break;
  7058. case T_CLASS_C:
  7059. if (ce) {
  7060. if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  7061. return 0;
  7062. } else {
  7063. ZVAL_STR_COPY(zv, ce->name);
  7064. }
  7065. } else {
  7066. ZVAL_EMPTY_STRING(zv);
  7067. }
  7068. break;
  7069. case T_TRAIT_C:
  7070. if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  7071. ZVAL_STR_COPY(zv, ce->name);
  7072. } else {
  7073. ZVAL_EMPTY_STRING(zv);
  7074. }
  7075. break;
  7076. case T_NS_C:
  7077. if (FC(current_namespace)) {
  7078. ZVAL_STR_COPY(zv, FC(current_namespace));
  7079. } else {
  7080. ZVAL_EMPTY_STRING(zv);
  7081. }
  7082. break;
  7083. EMPTY_SWITCH_DEFAULT_CASE()
  7084. }
  7085. return 1;
  7086. }
  7087. /* }}} */
  7088. ZEND_API bool zend_is_op_long_compatible(zval *op)
  7089. {
  7090. if (Z_TYPE_P(op) == IS_ARRAY) {
  7091. return false;
  7092. }
  7093. if (Z_TYPE_P(op) == IS_DOUBLE
  7094. && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval(Z_DVAL_P(op)))) {
  7095. return false;
  7096. }
  7097. if (Z_TYPE_P(op) == IS_STRING) {
  7098. double dval = 0;
  7099. zend_uchar is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
  7100. if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval(dval)))) {
  7101. return false;
  7102. }
  7103. }
  7104. return true;
  7105. }
  7106. ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, zval *op1, zval *op2) /* {{{ */
  7107. {
  7108. if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
  7109. /* Array to string warning. */
  7110. return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
  7111. }
  7112. if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
  7113. || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
  7114. || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
  7115. /* Only the numeric operations throw errors. */
  7116. return 0;
  7117. }
  7118. if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
  7119. if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
  7120. /* Adding two arrays is allowed. */
  7121. return 0;
  7122. }
  7123. /* Numeric operators throw when one of the operands is an array. */
  7124. return 1;
  7125. }
  7126. /* While basic arithmetic operators always produce numeric string errors,
  7127. * bitwise operators don't produce errors if both operands are strings */
  7128. if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
  7129. && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
  7130. return 0;
  7131. }
  7132. if (Z_TYPE_P(op1) == IS_STRING
  7133. && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
  7134. return 1;
  7135. }
  7136. if (Z_TYPE_P(op2) == IS_STRING
  7137. && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
  7138. return 1;
  7139. }
  7140. if ((opcode == ZEND_MOD && zval_get_long(op2) == 0)
  7141. || (opcode == ZEND_DIV && zval_get_double(op2) == 0.0)) {
  7142. /* Division by zero throws an error. */
  7143. return 1;
  7144. }
  7145. if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
  7146. /* Shift by negative number throws an error. */
  7147. return 1;
  7148. }
  7149. /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
  7150. if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
  7151. || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR || opcode == ZEND_MOD) {
  7152. return !zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2);
  7153. }
  7154. return 0;
  7155. }
  7156. /* }}} */
  7157. static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
  7158. {
  7159. if (zend_binary_op_produces_error(opcode, op1, op2)) {
  7160. return 0;
  7161. }
  7162. binary_op_type fn = get_binary_op(opcode);
  7163. fn(result, op1, op2);
  7164. return 1;
  7165. }
  7166. /* }}} */
  7167. ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, zval *op)
  7168. {
  7169. if (opcode == ZEND_BW_NOT) {
  7170. /* BW_NOT on string does not convert the string into an integer. */
  7171. if (Z_TYPE_P(op) == IS_STRING) {
  7172. return 0;
  7173. }
  7174. return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
  7175. }
  7176. return 0;
  7177. }
  7178. static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
  7179. {
  7180. if (zend_unary_op_produces_error(opcode, op)) {
  7181. return 0;
  7182. }
  7183. unary_op_type fn = get_unary_op(opcode);
  7184. fn(result, op);
  7185. return 1;
  7186. }
  7187. /* }}} */
  7188. static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
  7189. {
  7190. zval right;
  7191. ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
  7192. return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
  7193. }
  7194. /* }}} */
  7195. static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
  7196. {
  7197. binary_op_type fn = kind == ZEND_AST_GREATER
  7198. ? is_smaller_function : is_smaller_or_equal_function;
  7199. fn(result, op2, op1);
  7200. }
  7201. /* }}} */
  7202. static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
  7203. {
  7204. zend_ast_list *list = zend_ast_get_list(ast);
  7205. zend_ast *last_elem_ast = NULL;
  7206. uint32_t i;
  7207. bool is_constant = 1;
  7208. if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
  7209. zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
  7210. }
  7211. /* First ensure that *all* child nodes are constant and by-val */
  7212. for (i = 0; i < list->children; ++i) {
  7213. zend_ast *elem_ast = list->child[i];
  7214. if (elem_ast == NULL) {
  7215. /* Report error at line of last non-empty element */
  7216. if (last_elem_ast) {
  7217. CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
  7218. }
  7219. zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
  7220. }
  7221. if (elem_ast->kind != ZEND_AST_UNPACK) {
  7222. zend_eval_const_expr(&elem_ast->child[0]);
  7223. zend_eval_const_expr(&elem_ast->child[1]);
  7224. if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
  7225. || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
  7226. ) {
  7227. is_constant = 0;
  7228. }
  7229. } else {
  7230. zend_eval_const_expr(&elem_ast->child[0]);
  7231. if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
  7232. is_constant = 0;
  7233. }
  7234. }
  7235. last_elem_ast = elem_ast;
  7236. }
  7237. if (!is_constant) {
  7238. return 0;
  7239. }
  7240. if (!list->children) {
  7241. ZVAL_EMPTY_ARRAY(result);
  7242. return 1;
  7243. }
  7244. array_init_size(result, list->children);
  7245. for (i = 0; i < list->children; ++i) {
  7246. zend_ast *elem_ast = list->child[i];
  7247. zend_ast *value_ast = elem_ast->child[0];
  7248. zend_ast *key_ast;
  7249. zval *value = zend_ast_get_zval(value_ast);
  7250. if (elem_ast->kind == ZEND_AST_UNPACK) {
  7251. if (Z_TYPE_P(value) == IS_ARRAY) {
  7252. HashTable *ht = Z_ARRVAL_P(value);
  7253. zval *val;
  7254. zend_string *key;
  7255. ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
  7256. if (key) {
  7257. zend_hash_update(Z_ARRVAL_P(result), key, val);
  7258. } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
  7259. zval_ptr_dtor(result);
  7260. return 0;
  7261. }
  7262. Z_TRY_ADDREF_P(val);
  7263. } ZEND_HASH_FOREACH_END();
  7264. continue;
  7265. } else {
  7266. zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked");
  7267. }
  7268. }
  7269. Z_TRY_ADDREF_P(value);
  7270. key_ast = elem_ast->child[1];
  7271. if (key_ast) {
  7272. zval *key = zend_ast_get_zval(key_ast);
  7273. switch (Z_TYPE_P(key)) {
  7274. case IS_LONG:
  7275. zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
  7276. break;
  7277. case IS_STRING:
  7278. zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
  7279. break;
  7280. case IS_DOUBLE: {
  7281. zend_long lval = zend_dval_to_lval(Z_DVAL_P(key));
  7282. /* Incompatible float will generate an error, leave this to run-time */
  7283. if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
  7284. zval_ptr_dtor_nogc(value);
  7285. zval_ptr_dtor(result);
  7286. return 0;
  7287. }
  7288. zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
  7289. break;
  7290. }
  7291. case IS_FALSE:
  7292. zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
  7293. break;
  7294. case IS_TRUE:
  7295. zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
  7296. break;
  7297. case IS_NULL:
  7298. zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value);
  7299. break;
  7300. default:
  7301. zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
  7302. break;
  7303. }
  7304. } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
  7305. zval_ptr_dtor_nogc(value);
  7306. zval_ptr_dtor(result);
  7307. return 0;
  7308. }
  7309. }
  7310. return 1;
  7311. }
  7312. /* }}} */
  7313. static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
  7314. {
  7315. zend_ast *left_ast = ast->child[0];
  7316. zend_ast *right_ast = ast->child[1];
  7317. uint32_t opcode = ast->attr;
  7318. znode left_node, right_node;
  7319. zend_compile_expr(&left_node, left_ast);
  7320. zend_compile_expr(&right_node, right_ast);
  7321. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  7322. if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
  7323. &left_node.u.constant, &right_node.u.constant)
  7324. ) {
  7325. result->op_type = IS_CONST;
  7326. zval_ptr_dtor(&left_node.u.constant);
  7327. zval_ptr_dtor(&right_node.u.constant);
  7328. return;
  7329. }
  7330. }
  7331. do {
  7332. if (opcode == ZEND_IS_EQUAL || opcode == ZEND_IS_NOT_EQUAL) {
  7333. if (left_node.op_type == IS_CONST) {
  7334. if (Z_TYPE(left_node.u.constant) == IS_FALSE) {
  7335. opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  7336. zend_emit_op_tmp(result, opcode, &right_node, NULL);
  7337. break;
  7338. } else if (Z_TYPE(left_node.u.constant) == IS_TRUE) {
  7339. opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  7340. zend_emit_op_tmp(result, opcode, &right_node, NULL);
  7341. break;
  7342. }
  7343. } else if (right_node.op_type == IS_CONST) {
  7344. if (Z_TYPE(right_node.u.constant) == IS_FALSE) {
  7345. opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  7346. zend_emit_op_tmp(result, opcode, &left_node, NULL);
  7347. break;
  7348. } else if (Z_TYPE(right_node.u.constant) == IS_TRUE) {
  7349. opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  7350. zend_emit_op_tmp(result, opcode, &left_node, NULL);
  7351. break;
  7352. }
  7353. }
  7354. } else if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
  7355. /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
  7356. if (left_node.op_type == IS_CONST) {
  7357. if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
  7358. zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
  7359. opline->extended_value =
  7360. (opcode == ZEND_IS_IDENTICAL) ?
  7361. (1 << Z_TYPE(left_node.u.constant)) :
  7362. (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
  7363. return;
  7364. }
  7365. } else if (right_node.op_type == IS_CONST) {
  7366. if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
  7367. zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
  7368. opline->extended_value =
  7369. (opcode == ZEND_IS_IDENTICAL) ?
  7370. (1 << Z_TYPE(right_node.u.constant)) :
  7371. (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
  7372. return;
  7373. }
  7374. }
  7375. } else if (opcode == ZEND_CONCAT) {
  7376. /* convert constant operands to strings at compile-time */
  7377. if (left_node.op_type == IS_CONST) {
  7378. if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
  7379. zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
  7380. } else {
  7381. convert_to_string(&left_node.u.constant);
  7382. }
  7383. }
  7384. if (right_node.op_type == IS_CONST) {
  7385. if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
  7386. zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
  7387. } else {
  7388. convert_to_string(&right_node.u.constant);
  7389. }
  7390. }
  7391. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  7392. opcode = ZEND_FAST_CONCAT;
  7393. }
  7394. }
  7395. zend_emit_op_tmp(result, opcode, &left_node, &right_node);
  7396. } while (0);
  7397. }
  7398. /* }}} */
  7399. /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
  7400. * evaluation order. */
  7401. static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
  7402. {
  7403. zend_ast *left_ast = ast->child[0];
  7404. zend_ast *right_ast = ast->child[1];
  7405. znode left_node, right_node;
  7406. ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
  7407. zend_compile_expr(&left_node, left_ast);
  7408. zend_compile_expr(&right_node, right_ast);
  7409. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  7410. result->op_type = IS_CONST;
  7411. zend_ct_eval_greater(&result->u.constant, ast->kind,
  7412. &left_node.u.constant, &right_node.u.constant);
  7413. zval_ptr_dtor(&left_node.u.constant);
  7414. zval_ptr_dtor(&right_node.u.constant);
  7415. return;
  7416. }
  7417. zend_emit_op_tmp(result,
  7418. ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
  7419. &right_node, &left_node);
  7420. }
  7421. /* }}} */
  7422. static void zend_compile_unary_op(znode *result, zend_ast *ast) /* {{{ */
  7423. {
  7424. zend_ast *expr_ast = ast->child[0];
  7425. uint32_t opcode = ast->attr;
  7426. znode expr_node;
  7427. zend_compile_expr(&expr_node, expr_ast);
  7428. if (expr_node.op_type == IS_CONST
  7429. && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
  7430. result->op_type = IS_CONST;
  7431. zval_ptr_dtor(&expr_node.u.constant);
  7432. return;
  7433. }
  7434. zend_emit_op_tmp(result, opcode, &expr_node, NULL);
  7435. }
  7436. /* }}} */
  7437. static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
  7438. {
  7439. zend_ast *expr_ast = ast->child[0];
  7440. znode expr_node, right_node;
  7441. ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
  7442. zend_compile_expr(&expr_node, expr_ast);
  7443. if (expr_node.op_type == IS_CONST
  7444. && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
  7445. result->op_type = IS_CONST;
  7446. zval_ptr_dtor(&expr_node.u.constant);
  7447. return;
  7448. }
  7449. right_node.op_type = IS_CONST;
  7450. ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
  7451. zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
  7452. }
  7453. /* }}} */
  7454. static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
  7455. {
  7456. zend_ast *left_ast = ast->child[0];
  7457. zend_ast *right_ast = ast->child[1];
  7458. znode left_node, right_node;
  7459. zend_op *opline_jmpz, *opline_bool;
  7460. uint32_t opnum_jmpz;
  7461. ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
  7462. zend_compile_expr(&left_node, left_ast);
  7463. if (left_node.op_type == IS_CONST) {
  7464. if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
  7465. || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
  7466. result->op_type = IS_CONST;
  7467. ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
  7468. } else {
  7469. zend_compile_expr(&right_node, right_ast);
  7470. if (right_node.op_type == IS_CONST) {
  7471. result->op_type = IS_CONST;
  7472. ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
  7473. zval_ptr_dtor(&right_node.u.constant);
  7474. } else {
  7475. zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
  7476. }
  7477. }
  7478. zval_ptr_dtor(&left_node.u.constant);
  7479. return;
  7480. }
  7481. opnum_jmpz = get_next_op_number();
  7482. opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
  7483. &left_node, NULL);
  7484. if (left_node.op_type == IS_TMP_VAR) {
  7485. SET_NODE(opline_jmpz->result, &left_node);
  7486. GET_NODE(result, opline_jmpz->result);
  7487. } else {
  7488. zend_make_tmp_result(result, opline_jmpz);
  7489. }
  7490. zend_compile_expr(&right_node, right_ast);
  7491. opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
  7492. SET_NODE(opline_bool->result, result);
  7493. zend_update_jump_target_to_next(opnum_jmpz);
  7494. }
  7495. /* }}} */
  7496. static void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
  7497. {
  7498. zend_ast *var_ast = ast->child[0];
  7499. ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
  7500. zend_ensure_writable_variable(var_ast);
  7501. if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
  7502. zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, 0);
  7503. opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
  7504. zend_make_tmp_result(result, opline);
  7505. } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
  7506. zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, 0, 0);
  7507. opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
  7508. zend_make_tmp_result(result, opline);
  7509. } else {
  7510. znode var_node;
  7511. zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
  7512. zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
  7513. &var_node, NULL);
  7514. }
  7515. }
  7516. /* }}} */
  7517. static void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
  7518. {
  7519. zend_ast *var_ast = ast->child[0];
  7520. ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
  7521. zend_ensure_writable_variable(var_ast);
  7522. if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
  7523. zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, 0);
  7524. opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
  7525. opline->result_type = IS_TMP_VAR;
  7526. result->op_type = IS_TMP_VAR;
  7527. } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
  7528. zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, 0, 0);
  7529. opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
  7530. opline->result_type = IS_TMP_VAR;
  7531. result->op_type = IS_TMP_VAR;
  7532. } else {
  7533. znode var_node;
  7534. zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
  7535. zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
  7536. &var_node, NULL);
  7537. }
  7538. }
  7539. /* }}} */
  7540. static void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
  7541. {
  7542. zend_ast *expr_ast = ast->child[0];
  7543. znode expr_node;
  7544. zend_op *opline;
  7545. zend_compile_expr(&expr_node, expr_ast);
  7546. if (ast->attr == _IS_BOOL) {
  7547. opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
  7548. } else if (ast->attr == IS_NULL) {
  7549. zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
  7550. } else {
  7551. opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
  7552. opline->extended_value = ast->attr;
  7553. }
  7554. }
  7555. /* }}} */
  7556. static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
  7557. {
  7558. zend_ast *cond_ast = ast->child[0];
  7559. zend_ast *false_ast = ast->child[2];
  7560. znode cond_node, false_node;
  7561. zend_op *opline_qm_assign;
  7562. uint32_t opnum_jmp_set;
  7563. ZEND_ASSERT(ast->child[1] == NULL);
  7564. zend_compile_expr(&cond_node, cond_ast);
  7565. opnum_jmp_set = get_next_op_number();
  7566. zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
  7567. zend_compile_expr(&false_node, false_ast);
  7568. opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
  7569. SET_NODE(opline_qm_assign->result, result);
  7570. zend_update_jump_target_to_next(opnum_jmp_set);
  7571. }
  7572. /* }}} */
  7573. static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
  7574. {
  7575. zend_ast *cond_ast = ast->child[0];
  7576. zend_ast *true_ast = ast->child[1];
  7577. zend_ast *false_ast = ast->child[2];
  7578. znode cond_node, true_node, false_node;
  7579. zend_op *opline_qm_assign2;
  7580. uint32_t opnum_jmpz, opnum_jmp;
  7581. if (cond_ast->kind == ZEND_AST_CONDITIONAL
  7582. && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
  7583. if (cond_ast->child[1]) {
  7584. if (true_ast) {
  7585. zend_error(E_COMPILE_ERROR,
  7586. "Unparenthesized `a ? b : c ? d : e` is not supported. "
  7587. "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
  7588. } else {
  7589. zend_error(E_COMPILE_ERROR,
  7590. "Unparenthesized `a ? b : c ?: d` is not supported. "
  7591. "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
  7592. }
  7593. } else {
  7594. if (true_ast) {
  7595. zend_error(E_COMPILE_ERROR,
  7596. "Unparenthesized `a ?: b ? c : d` is not supported. "
  7597. "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
  7598. } else {
  7599. /* This case is harmless: (a ?: b) ?: c always produces the same result
  7600. * as a ?: (b ?: c). */
  7601. }
  7602. }
  7603. }
  7604. if (!true_ast) {
  7605. zend_compile_shorthand_conditional(result, ast);
  7606. return;
  7607. }
  7608. zend_compile_expr(&cond_node, cond_ast);
  7609. opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  7610. zend_compile_expr(&true_node, true_ast);
  7611. zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
  7612. opnum_jmp = zend_emit_jump(0);
  7613. zend_update_jump_target_to_next(opnum_jmpz);
  7614. zend_compile_expr(&false_node, false_ast);
  7615. opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
  7616. SET_NODE(opline_qm_assign2->result, result);
  7617. zend_update_jump_target_to_next(opnum_jmp);
  7618. }
  7619. /* }}} */
  7620. static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
  7621. {
  7622. zend_ast *expr_ast = ast->child[0];
  7623. zend_ast *default_ast = ast->child[1];
  7624. znode expr_node, default_node;
  7625. zend_op *opline;
  7626. uint32_t opnum;
  7627. zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, 0);
  7628. opnum = get_next_op_number();
  7629. zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
  7630. zend_compile_expr(&default_node, default_ast);
  7631. opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
  7632. SET_NODE(opline->result, result);
  7633. opline = &CG(active_op_array)->opcodes[opnum];
  7634. opline->op2.opline_num = get_next_op_number();
  7635. }
  7636. /* }}} */
  7637. static void znode_dtor(zval *zv) {
  7638. znode *node = Z_PTR_P(zv);
  7639. if (node->op_type == IS_CONST) {
  7640. zval_ptr_dtor_nogc(&node->u.constant);
  7641. }
  7642. efree(node);
  7643. }
  7644. static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
  7645. {
  7646. zend_ast *var_ast = ast->child[0];
  7647. zend_ast *default_ast = ast->child[1];
  7648. znode var_node_is, var_node_w, default_node, assign_node, *node;
  7649. zend_op *opline;
  7650. uint32_t coalesce_opnum;
  7651. bool need_frees = 0;
  7652. /* Remember expressions compiled during the initial BP_VAR_IS lookup,
  7653. * to avoid double-evaluation when we compile again with BP_VAR_W. */
  7654. HashTable *orig_memoized_exprs = CG(memoized_exprs);
  7655. int orig_memoize_mode = CG(memoize_mode);
  7656. zend_ensure_writable_variable(var_ast);
  7657. if (is_this_fetch(var_ast)) {
  7658. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  7659. }
  7660. ALLOC_HASHTABLE(CG(memoized_exprs));
  7661. zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
  7662. CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
  7663. zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, 0);
  7664. coalesce_opnum = get_next_op_number();
  7665. zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
  7666. CG(memoize_mode) = ZEND_MEMOIZE_NONE;
  7667. if (var_ast->kind == ZEND_AST_DIM) {
  7668. zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
  7669. } else {
  7670. zend_compile_expr(&default_node, default_ast);
  7671. }
  7672. CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
  7673. zend_compile_var(&var_node_w, var_ast, BP_VAR_W, 0);
  7674. /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
  7675. opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  7676. /* Treat $GLOBALS['x'] assignment like assignment to variable. */
  7677. zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
  7678. switch (kind) {
  7679. case ZEND_AST_VAR:
  7680. zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
  7681. break;
  7682. case ZEND_AST_STATIC_PROP:
  7683. opline->opcode = ZEND_ASSIGN_STATIC_PROP;
  7684. opline->result_type = IS_TMP_VAR;
  7685. var_node_w.op_type = IS_TMP_VAR;
  7686. zend_emit_op_data(&default_node);
  7687. assign_node = var_node_w;
  7688. break;
  7689. case ZEND_AST_DIM:
  7690. opline->opcode = ZEND_ASSIGN_DIM;
  7691. opline->result_type = IS_TMP_VAR;
  7692. var_node_w.op_type = IS_TMP_VAR;
  7693. zend_emit_op_data(&default_node);
  7694. assign_node = var_node_w;
  7695. break;
  7696. case ZEND_AST_PROP:
  7697. case ZEND_AST_NULLSAFE_PROP:
  7698. opline->opcode = ZEND_ASSIGN_OBJ;
  7699. opline->result_type = IS_TMP_VAR;
  7700. var_node_w.op_type = IS_TMP_VAR;
  7701. zend_emit_op_data(&default_node);
  7702. assign_node = var_node_w;
  7703. break;
  7704. EMPTY_SWITCH_DEFAULT_CASE();
  7705. }
  7706. opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
  7707. SET_NODE(opline->result, result);
  7708. ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
  7709. if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
  7710. need_frees = 1;
  7711. break;
  7712. }
  7713. } ZEND_HASH_FOREACH_END();
  7714. /* Free DUPed expressions if there are any */
  7715. if (need_frees) {
  7716. uint32_t jump_opnum = zend_emit_jump(0);
  7717. zend_update_jump_target_to_next(coalesce_opnum);
  7718. ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
  7719. if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
  7720. zend_emit_op(NULL, ZEND_FREE, node, NULL);
  7721. }
  7722. } ZEND_HASH_FOREACH_END();
  7723. zend_update_jump_target_to_next(jump_opnum);
  7724. } else {
  7725. zend_update_jump_target_to_next(coalesce_opnum);
  7726. }
  7727. zend_hash_destroy(CG(memoized_exprs));
  7728. FREE_HASHTABLE(CG(memoized_exprs));
  7729. CG(memoized_exprs) = orig_memoized_exprs;
  7730. CG(memoize_mode) = orig_memoize_mode;
  7731. }
  7732. /* }}} */
  7733. static void zend_compile_print(znode *result, zend_ast *ast) /* {{{ */
  7734. {
  7735. zend_op *opline;
  7736. zend_ast *expr_ast = ast->child[0];
  7737. znode expr_node;
  7738. zend_compile_expr(&expr_node, expr_ast);
  7739. opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
  7740. opline->extended_value = 1;
  7741. result->op_type = IS_CONST;
  7742. ZVAL_LONG(&result->u.constant, 1);
  7743. }
  7744. /* }}} */
  7745. static void zend_compile_exit(znode *result, zend_ast *ast) /* {{{ */
  7746. {
  7747. zend_ast *expr_ast = ast->child[0];
  7748. znode expr_node;
  7749. if (expr_ast) {
  7750. zend_compile_expr(&expr_node, expr_ast);
  7751. } else {
  7752. expr_node.op_type = IS_UNUSED;
  7753. }
  7754. zend_op *opline = zend_emit_op(NULL, ZEND_EXIT, &expr_node, NULL);
  7755. if (result) {
  7756. /* Mark this as an "expression throw" for opcache. */
  7757. opline->extended_value = ZEND_THROW_IS_EXPR;
  7758. result->op_type = IS_CONST;
  7759. ZVAL_TRUE(&result->u.constant);
  7760. }
  7761. }
  7762. /* }}} */
  7763. static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
  7764. {
  7765. zend_ast *value_ast = ast->child[0];
  7766. zend_ast *key_ast = ast->child[1];
  7767. znode value_node, key_node;
  7768. znode *value_node_ptr = NULL, *key_node_ptr = NULL;
  7769. zend_op *opline;
  7770. bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  7771. zend_mark_function_as_generator();
  7772. if (key_ast) {
  7773. zend_compile_expr(&key_node, key_ast);
  7774. key_node_ptr = &key_node;
  7775. }
  7776. if (value_ast) {
  7777. if (returns_by_ref && zend_is_variable(value_ast)) {
  7778. zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
  7779. } else {
  7780. zend_compile_expr(&value_node, value_ast);
  7781. }
  7782. value_node_ptr = &value_node;
  7783. }
  7784. opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
  7785. if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
  7786. opline->extended_value = ZEND_RETURNS_FUNCTION;
  7787. }
  7788. }
  7789. /* }}} */
  7790. static void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
  7791. {
  7792. zend_ast *expr_ast = ast->child[0];
  7793. znode expr_node;
  7794. zend_mark_function_as_generator();
  7795. if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
  7796. zend_error_noreturn(E_COMPILE_ERROR,
  7797. "Cannot use \"yield from\" inside a by-reference generator");
  7798. }
  7799. zend_compile_expr(&expr_node, expr_ast);
  7800. zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
  7801. }
  7802. /* }}} */
  7803. static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
  7804. {
  7805. zend_ast *obj_ast = ast->child[0];
  7806. zend_ast *class_ast = ast->child[1];
  7807. znode obj_node, class_node;
  7808. zend_op *opline;
  7809. zend_compile_expr(&obj_node, obj_ast);
  7810. if (obj_node.op_type == IS_CONST) {
  7811. zend_do_free(&obj_node);
  7812. result->op_type = IS_CONST;
  7813. ZVAL_FALSE(&result->u.constant);
  7814. return;
  7815. }
  7816. zend_compile_class_ref(&class_node, class_ast,
  7817. ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
  7818. opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
  7819. if (class_node.op_type == IS_CONST) {
  7820. opline->op2_type = IS_CONST;
  7821. opline->op2.constant = zend_add_class_name_literal(
  7822. Z_STR(class_node.u.constant));
  7823. opline->extended_value = zend_alloc_cache_slot();
  7824. } else {
  7825. SET_NODE(opline->op2, &class_node);
  7826. }
  7827. }
  7828. /* }}} */
  7829. static void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */
  7830. {
  7831. zend_ast *expr_ast = ast->child[0];
  7832. znode expr_node;
  7833. zend_op *opline;
  7834. zend_do_extended_fcall_begin();
  7835. zend_compile_expr(&expr_node, expr_ast);
  7836. opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
  7837. opline->extended_value = ast->attr;
  7838. zend_do_extended_fcall_end();
  7839. }
  7840. /* }}} */
  7841. static void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */
  7842. {
  7843. zend_ast *var_ast = ast->child[0];
  7844. znode var_node;
  7845. zend_op *opline = NULL;
  7846. ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
  7847. if (!zend_is_variable(var_ast)) {
  7848. if (ast->kind == ZEND_AST_EMPTY) {
  7849. /* empty(expr) can be transformed to !expr */
  7850. zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
  7851. zend_compile_expr(result, not_ast);
  7852. return;
  7853. } else {
  7854. zend_error_noreturn(E_COMPILE_ERROR,
  7855. "Cannot use isset() on the result of an expression "
  7856. "(you can use \"null !== expression\" instead)");
  7857. }
  7858. }
  7859. if (is_globals_fetch(var_ast)) {
  7860. result->op_type = IS_CONST;
  7861. ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
  7862. return;
  7863. }
  7864. if (is_global_var_fetch(var_ast)) {
  7865. if (!var_ast->child[1]) {
  7866. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  7867. }
  7868. zend_compile_expr(&var_node, var_ast->child[1]);
  7869. if (var_node.op_type == IS_CONST) {
  7870. convert_to_string(&var_node.u.constant);
  7871. }
  7872. opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
  7873. opline->extended_value =
  7874. ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
  7875. return;
  7876. }
  7877. zend_short_circuiting_mark_inner(var_ast);
  7878. switch (var_ast->kind) {
  7879. case ZEND_AST_VAR:
  7880. if (is_this_fetch(var_ast)) {
  7881. opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
  7882. CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
  7883. } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
  7884. opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
  7885. } else {
  7886. opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, 0);
  7887. opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
  7888. }
  7889. break;
  7890. case ZEND_AST_DIM:
  7891. opline = zend_compile_dim(result, var_ast, BP_VAR_IS);
  7892. opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
  7893. break;
  7894. case ZEND_AST_PROP:
  7895. case ZEND_AST_NULLSAFE_PROP:
  7896. opline = zend_compile_prop(result, var_ast, BP_VAR_IS, 0);
  7897. opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
  7898. break;
  7899. case ZEND_AST_STATIC_PROP:
  7900. opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, 0, 0);
  7901. opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
  7902. break;
  7903. EMPTY_SWITCH_DEFAULT_CASE()
  7904. }
  7905. result->op_type = opline->result_type = IS_TMP_VAR;
  7906. if (!(ast->kind == ZEND_AST_ISSET)) {
  7907. opline->extended_value |= ZEND_ISEMPTY;
  7908. }
  7909. }
  7910. /* }}} */
  7911. static void zend_compile_silence(znode *result, zend_ast *ast) /* {{{ */
  7912. {
  7913. zend_ast *expr_ast = ast->child[0];
  7914. znode silence_node;
  7915. zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
  7916. if (expr_ast->kind == ZEND_AST_VAR) {
  7917. /* For @$var we need to force a FETCH instruction, otherwise the CV access will
  7918. * happen outside the silenced section. */
  7919. zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, 0 );
  7920. } else {
  7921. zend_compile_expr(result, expr_ast);
  7922. }
  7923. zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
  7924. }
  7925. /* }}} */
  7926. static void zend_compile_shell_exec(znode *result, zend_ast *ast) /* {{{ */
  7927. {
  7928. zend_ast *expr_ast = ast->child[0];
  7929. zval fn_name;
  7930. zend_ast *name_ast, *args_ast, *call_ast;
  7931. ZVAL_STRING(&fn_name, "shell_exec");
  7932. name_ast = zend_ast_create_zval(&fn_name);
  7933. args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
  7934. call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
  7935. zend_compile_expr(result, call_ast);
  7936. zval_ptr_dtor(&fn_name);
  7937. }
  7938. /* }}} */
  7939. static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
  7940. {
  7941. zend_ast_list *list = zend_ast_get_list(ast);
  7942. zend_op *opline;
  7943. uint32_t i, opnum_init = -1;
  7944. bool packed = 1;
  7945. if (zend_try_ct_eval_array(&result->u.constant, ast)) {
  7946. result->op_type = IS_CONST;
  7947. return;
  7948. }
  7949. /* Empty arrays are handled at compile-time */
  7950. ZEND_ASSERT(list->children > 0);
  7951. for (i = 0; i < list->children; ++i) {
  7952. zend_ast *elem_ast = list->child[i];
  7953. zend_ast *value_ast, *key_ast;
  7954. bool by_ref;
  7955. znode value_node, key_node, *key_node_ptr = NULL;
  7956. if (elem_ast == NULL) {
  7957. zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
  7958. }
  7959. value_ast = elem_ast->child[0];
  7960. if (elem_ast->kind == ZEND_AST_UNPACK) {
  7961. zend_compile_expr(&value_node, value_ast);
  7962. if (i == 0) {
  7963. opnum_init = get_next_op_number();
  7964. opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
  7965. }
  7966. opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
  7967. SET_NODE(opline->result, result);
  7968. continue;
  7969. }
  7970. key_ast = elem_ast->child[1];
  7971. by_ref = elem_ast->attr;
  7972. if (key_ast) {
  7973. zend_compile_expr(&key_node, key_ast);
  7974. zend_handle_numeric_op(&key_node);
  7975. key_node_ptr = &key_node;
  7976. }
  7977. if (by_ref) {
  7978. zend_ensure_writable_variable(value_ast);
  7979. zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
  7980. } else {
  7981. zend_compile_expr(&value_node, value_ast);
  7982. }
  7983. if (i == 0) {
  7984. opnum_init = get_next_op_number();
  7985. opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
  7986. opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
  7987. } else {
  7988. opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
  7989. &value_node, key_node_ptr);
  7990. SET_NODE(opline->result, result);
  7991. }
  7992. opline->extended_value |= by_ref;
  7993. if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
  7994. packed = 0;
  7995. }
  7996. }
  7997. /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
  7998. if (!packed) {
  7999. ZEND_ASSERT(opnum_init != (uint32_t)-1);
  8000. opline = &CG(active_op_array)->opcodes[opnum_init];
  8001. opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
  8002. }
  8003. }
  8004. /* }}} */
  8005. static void zend_compile_const(znode *result, zend_ast *ast) /* {{{ */
  8006. {
  8007. zend_ast *name_ast = ast->child[0];
  8008. zend_op *opline;
  8009. bool is_fully_qualified;
  8010. zend_string *orig_name = zend_ast_get_str(name_ast);
  8011. zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
  8012. if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
  8013. zend_ast *last = CG(ast);
  8014. while (last && last->kind == ZEND_AST_STMT_LIST) {
  8015. zend_ast_list *list = zend_ast_get_list(last);
  8016. if (list->children == 0) {
  8017. break;
  8018. }
  8019. last = list->child[list->children-1];
  8020. }
  8021. if (last && last->kind == ZEND_AST_HALT_COMPILER) {
  8022. result->op_type = IS_CONST;
  8023. ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
  8024. zend_string_release_ex(resolved_name, 0);
  8025. return;
  8026. }
  8027. }
  8028. if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
  8029. result->op_type = IS_CONST;
  8030. zend_string_release_ex(resolved_name, 0);
  8031. return;
  8032. }
  8033. opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
  8034. opline->op2_type = IS_CONST;
  8035. if (is_fully_qualified || !FC(current_namespace)) {
  8036. opline->op2.constant = zend_add_const_name_literal(
  8037. resolved_name, 0);
  8038. } else {
  8039. opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
  8040. opline->op2.constant = zend_add_const_name_literal(
  8041. resolved_name, 1);
  8042. }
  8043. opline->extended_value = zend_alloc_cache_slot();
  8044. }
  8045. /* }}} */
  8046. static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
  8047. {
  8048. zend_ast *class_ast;
  8049. zend_ast *const_ast;
  8050. znode class_node, const_node;
  8051. zend_op *opline;
  8052. zend_eval_const_expr(&ast->child[0]);
  8053. zend_eval_const_expr(&ast->child[1]);
  8054. class_ast = ast->child[0];
  8055. const_ast = ast->child[1];
  8056. if (class_ast->kind == ZEND_AST_ZVAL) {
  8057. zend_string *resolved_name;
  8058. resolved_name = zend_resolve_class_name_ast(class_ast);
  8059. if (const_ast->kind == ZEND_AST_ZVAL && zend_try_ct_eval_class_const(&result->u.constant, resolved_name, zend_ast_get_str(const_ast))) {
  8060. result->op_type = IS_CONST;
  8061. zend_string_release_ex(resolved_name, 0);
  8062. return;
  8063. }
  8064. zend_string_release_ex(resolved_name, 0);
  8065. }
  8066. zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  8067. zend_compile_expr(&const_node, const_ast);
  8068. opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
  8069. zend_set_class_name_op1(opline, &class_node);
  8070. opline->extended_value = zend_alloc_cache_slots(2);
  8071. }
  8072. /* }}} */
  8073. static void zend_compile_class_name(znode *result, zend_ast *ast) /* {{{ */
  8074. {
  8075. zend_ast *class_ast = ast->child[0];
  8076. if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
  8077. result->op_type = IS_CONST;
  8078. return;
  8079. }
  8080. if (class_ast->kind == ZEND_AST_ZVAL) {
  8081. zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  8082. opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
  8083. } else {
  8084. znode expr_node;
  8085. zend_compile_expr(&expr_node, class_ast);
  8086. if (expr_node.op_type == IS_CONST) {
  8087. /* Unlikely case that happen if class_ast is constant folded.
  8088. * Handle it here, to avoid needing a CONST specialization in the VM. */
  8089. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on value of type %s",
  8090. zend_zval_type_name(&expr_node.u.constant));
  8091. }
  8092. zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
  8093. }
  8094. }
  8095. /* }}} */
  8096. static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
  8097. {
  8098. if (num == 0) {
  8099. result->op_type = IS_TMP_VAR;
  8100. result->u.op.var = -1;
  8101. opline->opcode = ZEND_ROPE_INIT;
  8102. } else {
  8103. opline->opcode = ZEND_ROPE_ADD;
  8104. SET_NODE(opline->op1, result);
  8105. }
  8106. SET_NODE(opline->op2, elem_node);
  8107. SET_NODE(opline->result, result);
  8108. opline->extended_value = num;
  8109. return opline;
  8110. }
  8111. /* }}} */
  8112. static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
  8113. {
  8114. zend_op *opline = get_next_op();
  8115. if (num == 0) {
  8116. result->op_type = IS_TMP_VAR;
  8117. result->u.op.var = -1;
  8118. opline->opcode = ZEND_ROPE_INIT;
  8119. } else {
  8120. opline->opcode = ZEND_ROPE_ADD;
  8121. SET_NODE(opline->op1, result);
  8122. }
  8123. SET_NODE(opline->op2, elem_node);
  8124. SET_NODE(opline->result, result);
  8125. opline->extended_value = num;
  8126. return opline;
  8127. }
  8128. /* }}} */
  8129. static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
  8130. {
  8131. uint32_t i, j;
  8132. uint32_t rope_init_lineno = -1;
  8133. zend_op *opline = NULL, *init_opline;
  8134. znode elem_node, last_const_node;
  8135. zend_ast_list *list = zend_ast_get_list(ast);
  8136. uint32_t reserved_op_number = -1;
  8137. ZEND_ASSERT(list->children > 0);
  8138. j = 0;
  8139. last_const_node.op_type = IS_UNUSED;
  8140. for (i = 0; i < list->children; i++) {
  8141. zend_compile_expr(&elem_node, list->child[i]);
  8142. if (elem_node.op_type == IS_CONST) {
  8143. convert_to_string(&elem_node.u.constant);
  8144. if (Z_STRLEN(elem_node.u.constant) == 0) {
  8145. zval_ptr_dtor(&elem_node.u.constant);
  8146. } else if (last_const_node.op_type == IS_CONST) {
  8147. concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
  8148. zval_ptr_dtor(&elem_node.u.constant);
  8149. } else {
  8150. last_const_node.op_type = IS_CONST;
  8151. ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
  8152. /* Reserve place for ZEND_ROPE_ADD instruction */
  8153. reserved_op_number = get_next_op_number();
  8154. opline = get_next_op();
  8155. opline->opcode = ZEND_NOP;
  8156. }
  8157. continue;
  8158. } else {
  8159. if (j == 0) {
  8160. if (last_const_node.op_type == IS_CONST) {
  8161. rope_init_lineno = reserved_op_number;
  8162. } else {
  8163. rope_init_lineno = get_next_op_number();
  8164. }
  8165. }
  8166. if (last_const_node.op_type == IS_CONST) {
  8167. opline = &CG(active_op_array)->opcodes[reserved_op_number];
  8168. zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
  8169. last_const_node.op_type = IS_UNUSED;
  8170. }
  8171. opline = zend_compile_rope_add(result, j++, &elem_node);
  8172. }
  8173. }
  8174. if (j == 0) {
  8175. result->op_type = IS_CONST;
  8176. if (last_const_node.op_type == IS_CONST) {
  8177. ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
  8178. } else {
  8179. ZVAL_EMPTY_STRING(&result->u.constant);
  8180. /* empty string */
  8181. }
  8182. CG(active_op_array)->last = reserved_op_number - 1;
  8183. return;
  8184. } else if (last_const_node.op_type == IS_CONST) {
  8185. opline = &CG(active_op_array)->opcodes[reserved_op_number];
  8186. opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
  8187. }
  8188. init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
  8189. if (j == 1) {
  8190. if (opline->op2_type == IS_CONST) {
  8191. GET_NODE(result, opline->op2);
  8192. MAKE_NOP(opline);
  8193. } else {
  8194. opline->opcode = ZEND_CAST;
  8195. opline->extended_value = IS_STRING;
  8196. opline->op1_type = opline->op2_type;
  8197. opline->op1 = opline->op2;
  8198. SET_UNUSED(opline->op2);
  8199. zend_make_tmp_result(result, opline);
  8200. }
  8201. } else if (j == 2) {
  8202. opline->opcode = ZEND_FAST_CONCAT;
  8203. opline->extended_value = 0;
  8204. opline->op1_type = init_opline->op2_type;
  8205. opline->op1 = init_opline->op2;
  8206. zend_make_tmp_result(result, opline);
  8207. MAKE_NOP(init_opline);
  8208. } else {
  8209. uint32_t var;
  8210. init_opline->extended_value = j;
  8211. opline->opcode = ZEND_ROPE_END;
  8212. zend_make_tmp_result(result, opline);
  8213. var = opline->op1.var = get_temporary_variable();
  8214. /* Allocates the necessary number of zval slots to keep the rope */
  8215. i = ((j * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
  8216. while (i > 1) {
  8217. get_temporary_variable();
  8218. i--;
  8219. }
  8220. /* Update all the previous opcodes to use the same variable */
  8221. while (opline != init_opline) {
  8222. opline--;
  8223. if (opline->opcode == ZEND_ROPE_ADD &&
  8224. opline->result.var == (uint32_t)-1) {
  8225. opline->op1.var = var;
  8226. opline->result.var = var;
  8227. } else if (opline->opcode == ZEND_ROPE_INIT &&
  8228. opline->result.var == (uint32_t)-1) {
  8229. opline->result.var = var;
  8230. }
  8231. }
  8232. }
  8233. }
  8234. /* }}} */
  8235. static void zend_compile_magic_const(znode *result, zend_ast *ast) /* {{{ */
  8236. {
  8237. zend_op *opline;
  8238. if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
  8239. result->op_type = IS_CONST;
  8240. return;
  8241. }
  8242. ZEND_ASSERT(ast->attr == T_CLASS_C &&
  8243. CG(active_class_entry) &&
  8244. (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
  8245. opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  8246. opline->op1.num = ZEND_FETCH_CLASS_SELF;
  8247. }
  8248. /* }}} */
  8249. static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
  8250. {
  8251. return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
  8252. || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
  8253. || kind == ZEND_AST_AND || kind == ZEND_AST_OR
  8254. || kind == ZEND_AST_UNARY_OP
  8255. || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
  8256. || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
  8257. || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
  8258. || kind == ZEND_AST_UNPACK
  8259. || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
  8260. || kind == ZEND_AST_CLASS_NAME
  8261. || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
  8262. || kind == ZEND_AST_CONST_ENUM_INIT
  8263. || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
  8264. || kind == ZEND_AST_NAMED_ARG;
  8265. }
  8266. /* }}} */
  8267. static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
  8268. {
  8269. zend_ast *ast = *ast_ptr;
  8270. zend_ast *class_ast = ast->child[0];
  8271. zend_string *class_name;
  8272. int fetch_type;
  8273. if (class_ast->kind != ZEND_AST_ZVAL) {
  8274. zend_error_noreturn(E_COMPILE_ERROR,
  8275. "Dynamic class names are not allowed in compile-time class constant references");
  8276. }
  8277. class_name = zend_ast_get_str(class_ast);
  8278. fetch_type = zend_get_class_fetch_type(class_name);
  8279. if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
  8280. zend_error_noreturn(E_COMPILE_ERROR,
  8281. "\"static::\" is not allowed in compile-time constants");
  8282. }
  8283. if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
  8284. zend_string *tmp = zend_resolve_class_name_ast(class_ast);
  8285. zend_string_release_ex(class_name, 0);
  8286. if (tmp != class_name) {
  8287. zval *zv = zend_ast_get_zval(class_ast);
  8288. ZVAL_STR(zv, tmp);
  8289. class_ast->attr = ZEND_NAME_FQ;
  8290. }
  8291. }
  8292. ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
  8293. }
  8294. /* }}} */
  8295. static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
  8296. {
  8297. zend_ast *ast = *ast_ptr;
  8298. zend_ast *class_ast = ast->child[0];
  8299. if (class_ast->kind != ZEND_AST_ZVAL) {
  8300. zend_error_noreturn(E_COMPILE_ERROR,
  8301. "(expression)::class cannot be used in constant expressions");
  8302. }
  8303. zend_string *class_name = zend_ast_get_str(class_ast);
  8304. uint32_t fetch_type = zend_get_class_fetch_type(class_name);
  8305. switch (fetch_type) {
  8306. case ZEND_FETCH_CLASS_SELF:
  8307. case ZEND_FETCH_CLASS_PARENT:
  8308. /* For the const-eval representation store the fetch type instead of the name. */
  8309. zend_string_release(class_name);
  8310. ast->child[0] = NULL;
  8311. ast->attr = fetch_type;
  8312. return;
  8313. case ZEND_FETCH_CLASS_STATIC:
  8314. zend_error_noreturn(E_COMPILE_ERROR,
  8315. "static::class cannot be used for compile-time class name resolution");
  8316. return;
  8317. EMPTY_SWITCH_DEFAULT_CASE()
  8318. }
  8319. }
  8320. static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
  8321. {
  8322. zend_ast *ast = *ast_ptr;
  8323. zend_ast *name_ast = ast->child[0];
  8324. zend_string *orig_name = zend_ast_get_str(name_ast);
  8325. bool is_fully_qualified;
  8326. zval result;
  8327. zend_string *resolved_name;
  8328. resolved_name = zend_resolve_const_name(
  8329. orig_name, name_ast->attr, &is_fully_qualified);
  8330. if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
  8331. zend_string_release_ex(resolved_name, 0);
  8332. zend_ast_destroy(ast);
  8333. *ast_ptr = zend_ast_create_zval(&result);
  8334. return;
  8335. }
  8336. zend_ast_destroy(ast);
  8337. *ast_ptr = zend_ast_create_constant(resolved_name,
  8338. !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
  8339. }
  8340. /* }}} */
  8341. static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
  8342. {
  8343. zend_ast *ast = *ast_ptr;
  8344. /* Other cases already resolved by constant folding */
  8345. ZEND_ASSERT(ast->attr == T_CLASS_C);
  8346. zend_ast_destroy(ast);
  8347. *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
  8348. }
  8349. /* }}} */
  8350. static void zend_compile_const_expr_new(zend_ast **ast_ptr)
  8351. {
  8352. zend_ast *class_ast = (*ast_ptr)->child[0];
  8353. if (class_ast->kind == ZEND_AST_CLASS) {
  8354. zend_error_noreturn(E_COMPILE_ERROR,
  8355. "Cannot use anonymous class in constant expression");
  8356. }
  8357. if (class_ast->kind != ZEND_AST_ZVAL) {
  8358. zend_error_noreturn(E_COMPILE_ERROR,
  8359. "Cannot use dynamic class name in constant expression");
  8360. }
  8361. zend_string *class_name = zend_resolve_class_name_ast(class_ast);
  8362. int fetch_type = zend_get_class_fetch_type(class_name);
  8363. if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
  8364. zend_error_noreturn(E_COMPILE_ERROR,
  8365. "\"static\" is not allowed in compile-time constants");
  8366. }
  8367. zval *class_ast_zv = zend_ast_get_zval(class_ast);
  8368. zval_ptr_dtor_nogc(class_ast_zv);
  8369. ZVAL_STR(class_ast_zv, class_name);
  8370. class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
  8371. }
  8372. static void zend_compile_const_expr_args(zend_ast **ast_ptr)
  8373. {
  8374. zend_ast_list *list = zend_ast_get_list(*ast_ptr);
  8375. bool uses_named_args = false;
  8376. for (uint32_t i = 0; i < list->children; i++) {
  8377. zend_ast *arg = list->child[i];
  8378. if (arg->kind == ZEND_AST_UNPACK) {
  8379. zend_error_noreturn(E_COMPILE_ERROR,
  8380. "Argument unpacking in constant expressions is not supported");
  8381. }
  8382. if (arg->kind == ZEND_AST_NAMED_ARG) {
  8383. uses_named_args = true;
  8384. } else if (uses_named_args) {
  8385. zend_error_noreturn(E_COMPILE_ERROR,
  8386. "Cannot use positional argument after named argument");
  8387. }
  8388. }
  8389. if (uses_named_args) {
  8390. list->attr = 1;
  8391. }
  8392. }
  8393. typedef struct {
  8394. /* Whether the value of this expression may differ on each evaluation. */
  8395. bool allow_dynamic;
  8396. } const_expr_context;
  8397. static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
  8398. {
  8399. const_expr_context *ctx = (const_expr_context *) context;
  8400. zend_ast *ast = *ast_ptr;
  8401. if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
  8402. return;
  8403. }
  8404. if (!zend_is_allowed_in_const_expr(ast->kind)) {
  8405. zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
  8406. }
  8407. switch (ast->kind) {
  8408. case ZEND_AST_CLASS_CONST:
  8409. zend_compile_const_expr_class_const(ast_ptr);
  8410. break;
  8411. case ZEND_AST_CLASS_NAME:
  8412. zend_compile_const_expr_class_name(ast_ptr);
  8413. break;
  8414. case ZEND_AST_CONST:
  8415. zend_compile_const_expr_const(ast_ptr);
  8416. break;
  8417. case ZEND_AST_MAGIC_CONST:
  8418. zend_compile_const_expr_magic_const(ast_ptr);
  8419. break;
  8420. case ZEND_AST_NEW:
  8421. if (!ctx->allow_dynamic) {
  8422. zend_error_noreturn(E_COMPILE_ERROR,
  8423. "New expressions are not supported in this context");
  8424. }
  8425. zend_compile_const_expr_new(ast_ptr);
  8426. break;
  8427. case ZEND_AST_ARG_LIST:
  8428. zend_compile_const_expr_args(ast_ptr);
  8429. break;
  8430. }
  8431. zend_ast_apply(ast, zend_compile_const_expr, context);
  8432. }
  8433. /* }}} */
  8434. void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
  8435. {
  8436. const_expr_context context;
  8437. context.allow_dynamic = allow_dynamic;
  8438. zend_eval_const_expr(ast_ptr);
  8439. zend_compile_const_expr(ast_ptr, &context);
  8440. if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
  8441. /* Replace with compiled AST zval representation. */
  8442. zval ast_zv;
  8443. ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
  8444. zend_ast_destroy(*ast_ptr);
  8445. *ast_ptr = zend_ast_create_zval(&ast_zv);
  8446. }
  8447. ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
  8448. }
  8449. /* }}} */
  8450. /* Same as compile_stmt, but with early binding */
  8451. void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
  8452. {
  8453. if (!ast) {
  8454. return;
  8455. }
  8456. if (ast->kind == ZEND_AST_STMT_LIST) {
  8457. zend_ast_list *list = zend_ast_get_list(ast);
  8458. uint32_t i;
  8459. for (i = 0; i < list->children; ++i) {
  8460. zend_compile_top_stmt(list->child[i]);
  8461. }
  8462. return;
  8463. }
  8464. if (ast->kind == ZEND_AST_FUNC_DECL) {
  8465. CG(zend_lineno) = ast->lineno;
  8466. zend_compile_func_decl(NULL, ast, 1);
  8467. CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
  8468. } else if (ast->kind == ZEND_AST_CLASS) {
  8469. CG(zend_lineno) = ast->lineno;
  8470. zend_compile_class_decl(NULL, ast, 1);
  8471. CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
  8472. } else {
  8473. zend_compile_stmt(ast);
  8474. }
  8475. if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
  8476. zend_verify_namespace();
  8477. }
  8478. }
  8479. /* }}} */
  8480. static void zend_compile_stmt(zend_ast *ast) /* {{{ */
  8481. {
  8482. if (!ast) {
  8483. return;
  8484. }
  8485. CG(zend_lineno) = ast->lineno;
  8486. if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
  8487. zend_do_extended_stmt();
  8488. }
  8489. switch (ast->kind) {
  8490. case ZEND_AST_STMT_LIST:
  8491. zend_compile_stmt_list(ast);
  8492. break;
  8493. case ZEND_AST_GLOBAL:
  8494. zend_compile_global_var(ast);
  8495. break;
  8496. case ZEND_AST_STATIC:
  8497. zend_compile_static_var(ast);
  8498. break;
  8499. case ZEND_AST_UNSET:
  8500. zend_compile_unset(ast);
  8501. break;
  8502. case ZEND_AST_RETURN:
  8503. zend_compile_return(ast);
  8504. break;
  8505. case ZEND_AST_ECHO:
  8506. zend_compile_echo(ast);
  8507. break;
  8508. case ZEND_AST_BREAK:
  8509. case ZEND_AST_CONTINUE:
  8510. zend_compile_break_continue(ast);
  8511. break;
  8512. case ZEND_AST_GOTO:
  8513. zend_compile_goto(ast);
  8514. break;
  8515. case ZEND_AST_LABEL:
  8516. zend_compile_label(ast);
  8517. break;
  8518. case ZEND_AST_WHILE:
  8519. zend_compile_while(ast);
  8520. break;
  8521. case ZEND_AST_DO_WHILE:
  8522. zend_compile_do_while(ast);
  8523. break;
  8524. case ZEND_AST_FOR:
  8525. zend_compile_for(ast);
  8526. break;
  8527. case ZEND_AST_FOREACH:
  8528. zend_compile_foreach(ast);
  8529. break;
  8530. case ZEND_AST_IF:
  8531. zend_compile_if(ast);
  8532. break;
  8533. case ZEND_AST_SWITCH:
  8534. zend_compile_switch(ast);
  8535. break;
  8536. case ZEND_AST_TRY:
  8537. zend_compile_try(ast);
  8538. break;
  8539. case ZEND_AST_DECLARE:
  8540. zend_compile_declare(ast);
  8541. break;
  8542. case ZEND_AST_FUNC_DECL:
  8543. case ZEND_AST_METHOD:
  8544. zend_compile_func_decl(NULL, ast, 0);
  8545. break;
  8546. case ZEND_AST_ENUM_CASE:
  8547. zend_compile_enum_case(ast);
  8548. break;
  8549. case ZEND_AST_PROP_GROUP:
  8550. zend_compile_prop_group(ast);
  8551. break;
  8552. case ZEND_AST_CLASS_CONST_GROUP:
  8553. zend_compile_class_const_group(ast);
  8554. break;
  8555. case ZEND_AST_USE_TRAIT:
  8556. zend_compile_use_trait(ast);
  8557. break;
  8558. case ZEND_AST_CLASS:
  8559. zend_compile_class_decl(NULL, ast, 0);
  8560. break;
  8561. case ZEND_AST_GROUP_USE:
  8562. zend_compile_group_use(ast);
  8563. break;
  8564. case ZEND_AST_USE:
  8565. zend_compile_use(ast);
  8566. break;
  8567. case ZEND_AST_CONST_DECL:
  8568. zend_compile_const_decl(ast);
  8569. break;
  8570. case ZEND_AST_NAMESPACE:
  8571. zend_compile_namespace(ast);
  8572. break;
  8573. case ZEND_AST_HALT_COMPILER:
  8574. zend_compile_halt_compiler(ast);
  8575. break;
  8576. case ZEND_AST_THROW:
  8577. case ZEND_AST_EXIT:
  8578. zend_compile_expr(NULL, ast);
  8579. break;
  8580. default:
  8581. {
  8582. znode result;
  8583. zend_compile_expr(&result, ast);
  8584. zend_do_free(&result);
  8585. }
  8586. }
  8587. if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
  8588. zend_emit_tick();
  8589. }
  8590. }
  8591. /* }}} */
  8592. static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
  8593. {
  8594. /* CG(zend_lineno) = ast->lineno; */
  8595. CG(zend_lineno) = zend_ast_get_lineno(ast);
  8596. if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
  8597. zend_compile_memoized_expr(result, ast);
  8598. return;
  8599. }
  8600. switch (ast->kind) {
  8601. case ZEND_AST_ZVAL:
  8602. ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
  8603. result->op_type = IS_CONST;
  8604. return;
  8605. case ZEND_AST_ZNODE:
  8606. *result = *zend_ast_get_znode(ast);
  8607. return;
  8608. case ZEND_AST_VAR:
  8609. case ZEND_AST_DIM:
  8610. case ZEND_AST_PROP:
  8611. case ZEND_AST_NULLSAFE_PROP:
  8612. case ZEND_AST_STATIC_PROP:
  8613. case ZEND_AST_CALL:
  8614. case ZEND_AST_METHOD_CALL:
  8615. case ZEND_AST_NULLSAFE_METHOD_CALL:
  8616. case ZEND_AST_STATIC_CALL:
  8617. zend_compile_var(result, ast, BP_VAR_R, 0);
  8618. return;
  8619. case ZEND_AST_ASSIGN:
  8620. zend_compile_assign(result, ast);
  8621. return;
  8622. case ZEND_AST_ASSIGN_REF:
  8623. zend_compile_assign_ref(result, ast);
  8624. return;
  8625. case ZEND_AST_NEW:
  8626. zend_compile_new(result, ast);
  8627. return;
  8628. case ZEND_AST_CLONE:
  8629. zend_compile_clone(result, ast);
  8630. return;
  8631. case ZEND_AST_ASSIGN_OP:
  8632. zend_compile_compound_assign(result, ast);
  8633. return;
  8634. case ZEND_AST_BINARY_OP:
  8635. zend_compile_binary_op(result, ast);
  8636. return;
  8637. case ZEND_AST_GREATER:
  8638. case ZEND_AST_GREATER_EQUAL:
  8639. zend_compile_greater(result, ast);
  8640. return;
  8641. case ZEND_AST_UNARY_OP:
  8642. zend_compile_unary_op(result, ast);
  8643. return;
  8644. case ZEND_AST_UNARY_PLUS:
  8645. case ZEND_AST_UNARY_MINUS:
  8646. zend_compile_unary_pm(result, ast);
  8647. return;
  8648. case ZEND_AST_AND:
  8649. case ZEND_AST_OR:
  8650. zend_compile_short_circuiting(result, ast);
  8651. return;
  8652. case ZEND_AST_POST_INC:
  8653. case ZEND_AST_POST_DEC:
  8654. zend_compile_post_incdec(result, ast);
  8655. return;
  8656. case ZEND_AST_PRE_INC:
  8657. case ZEND_AST_PRE_DEC:
  8658. zend_compile_pre_incdec(result, ast);
  8659. return;
  8660. case ZEND_AST_CAST:
  8661. zend_compile_cast(result, ast);
  8662. return;
  8663. case ZEND_AST_CONDITIONAL:
  8664. zend_compile_conditional(result, ast);
  8665. return;
  8666. case ZEND_AST_COALESCE:
  8667. zend_compile_coalesce(result, ast);
  8668. return;
  8669. case ZEND_AST_ASSIGN_COALESCE:
  8670. zend_compile_assign_coalesce(result, ast);
  8671. return;
  8672. case ZEND_AST_PRINT:
  8673. zend_compile_print(result, ast);
  8674. return;
  8675. case ZEND_AST_EXIT:
  8676. zend_compile_exit(result, ast);
  8677. return;
  8678. case ZEND_AST_YIELD:
  8679. zend_compile_yield(result, ast);
  8680. return;
  8681. case ZEND_AST_YIELD_FROM:
  8682. zend_compile_yield_from(result, ast);
  8683. return;
  8684. case ZEND_AST_INSTANCEOF:
  8685. zend_compile_instanceof(result, ast);
  8686. return;
  8687. case ZEND_AST_INCLUDE_OR_EVAL:
  8688. zend_compile_include_or_eval(result, ast);
  8689. return;
  8690. case ZEND_AST_ISSET:
  8691. case ZEND_AST_EMPTY:
  8692. zend_compile_isset_or_empty(result, ast);
  8693. return;
  8694. case ZEND_AST_SILENCE:
  8695. zend_compile_silence(result, ast);
  8696. return;
  8697. case ZEND_AST_SHELL_EXEC:
  8698. zend_compile_shell_exec(result, ast);
  8699. return;
  8700. case ZEND_AST_ARRAY:
  8701. zend_compile_array(result, ast);
  8702. return;
  8703. case ZEND_AST_CONST:
  8704. zend_compile_const(result, ast);
  8705. return;
  8706. case ZEND_AST_CLASS_CONST:
  8707. zend_compile_class_const(result, ast);
  8708. return;
  8709. case ZEND_AST_CLASS_NAME:
  8710. zend_compile_class_name(result, ast);
  8711. return;
  8712. case ZEND_AST_ENCAPS_LIST:
  8713. zend_compile_encaps_list(result, ast);
  8714. return;
  8715. case ZEND_AST_MAGIC_CONST:
  8716. zend_compile_magic_const(result, ast);
  8717. return;
  8718. case ZEND_AST_CLOSURE:
  8719. case ZEND_AST_ARROW_FUNC:
  8720. zend_compile_func_decl(result, ast, 0);
  8721. return;
  8722. case ZEND_AST_THROW:
  8723. zend_compile_throw(result, ast);
  8724. return;
  8725. case ZEND_AST_MATCH:
  8726. zend_compile_match(result, ast);
  8727. return;
  8728. default:
  8729. ZEND_ASSERT(0 /* not supported */);
  8730. }
  8731. }
  8732. /* }}} */
  8733. static void zend_compile_expr(znode *result, zend_ast *ast)
  8734. {
  8735. uint32_t checkpoint = zend_short_circuiting_checkpoint();
  8736. zend_compile_expr_inner(result, ast);
  8737. zend_short_circuiting_commit(checkpoint, result, ast);
  8738. }
  8739. static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
  8740. {
  8741. CG(zend_lineno) = zend_ast_get_lineno(ast);
  8742. switch (ast->kind) {
  8743. case ZEND_AST_VAR:
  8744. return zend_compile_simple_var(result, ast, type, 0);
  8745. case ZEND_AST_DIM:
  8746. return zend_compile_dim(result, ast, type);
  8747. case ZEND_AST_PROP:
  8748. case ZEND_AST_NULLSAFE_PROP:
  8749. return zend_compile_prop(result, ast, type, by_ref);
  8750. case ZEND_AST_STATIC_PROP:
  8751. return zend_compile_static_prop(result, ast, type, by_ref, 0);
  8752. case ZEND_AST_CALL:
  8753. zend_compile_call(result, ast, type);
  8754. return NULL;
  8755. case ZEND_AST_METHOD_CALL:
  8756. case ZEND_AST_NULLSAFE_METHOD_CALL:
  8757. zend_compile_method_call(result, ast, type);
  8758. return NULL;
  8759. case ZEND_AST_STATIC_CALL:
  8760. zend_compile_static_call(result, ast, type);
  8761. return NULL;
  8762. case ZEND_AST_ZNODE:
  8763. *result = *zend_ast_get_znode(ast);
  8764. return NULL;
  8765. default:
  8766. if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
  8767. zend_error_noreturn(E_COMPILE_ERROR,
  8768. "Cannot use temporary expression in write context");
  8769. }
  8770. zend_compile_expr(result, ast);
  8771. return NULL;
  8772. }
  8773. }
  8774. static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
  8775. {
  8776. uint32_t checkpoint = zend_short_circuiting_checkpoint();
  8777. zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
  8778. zend_short_circuiting_commit(checkpoint, result, ast);
  8779. return opcode;
  8780. }
  8781. static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
  8782. {
  8783. switch (ast->kind) {
  8784. case ZEND_AST_VAR:
  8785. return zend_compile_simple_var(result, ast, type, 1);
  8786. case ZEND_AST_DIM:
  8787. return zend_delayed_compile_dim(result, ast, type);
  8788. case ZEND_AST_PROP:
  8789. case ZEND_AST_NULLSAFE_PROP:
  8790. {
  8791. zend_op *opline = zend_delayed_compile_prop(result, ast, type);
  8792. if (by_ref) {
  8793. opline->extended_value |= ZEND_FETCH_REF;
  8794. }
  8795. return opline;
  8796. }
  8797. case ZEND_AST_STATIC_PROP:
  8798. return zend_compile_static_prop(result, ast, type, by_ref, 1);
  8799. default:
  8800. return zend_compile_var(result, ast, type, 0);
  8801. }
  8802. }
  8803. /* }}} */
  8804. static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
  8805. {
  8806. zend_ast *ast = *ast_ptr;
  8807. zval result;
  8808. if (!ast) {
  8809. return;
  8810. }
  8811. switch (ast->kind) {
  8812. case ZEND_AST_BINARY_OP:
  8813. zend_eval_const_expr(&ast->child[0]);
  8814. zend_eval_const_expr(&ast->child[1]);
  8815. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  8816. return;
  8817. }
  8818. if (!zend_try_ct_eval_binary_op(&result, ast->attr,
  8819. zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
  8820. ) {
  8821. return;
  8822. }
  8823. break;
  8824. case ZEND_AST_GREATER:
  8825. case ZEND_AST_GREATER_EQUAL:
  8826. zend_eval_const_expr(&ast->child[0]);
  8827. zend_eval_const_expr(&ast->child[1]);
  8828. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  8829. return;
  8830. }
  8831. zend_ct_eval_greater(&result, ast->kind,
  8832. zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
  8833. break;
  8834. case ZEND_AST_AND:
  8835. case ZEND_AST_OR:
  8836. {
  8837. bool child0_is_true, child1_is_true;
  8838. zend_eval_const_expr(&ast->child[0]);
  8839. zend_eval_const_expr(&ast->child[1]);
  8840. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  8841. return;
  8842. }
  8843. child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
  8844. if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
  8845. ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
  8846. break;
  8847. }
  8848. if (ast->child[1]->kind != ZEND_AST_ZVAL) {
  8849. return;
  8850. }
  8851. child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
  8852. if (ast->kind == ZEND_AST_OR) {
  8853. ZVAL_BOOL(&result, child0_is_true || child1_is_true);
  8854. } else {
  8855. ZVAL_BOOL(&result, child0_is_true && child1_is_true);
  8856. }
  8857. break;
  8858. }
  8859. case ZEND_AST_UNARY_OP:
  8860. zend_eval_const_expr(&ast->child[0]);
  8861. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  8862. return;
  8863. }
  8864. if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
  8865. return;
  8866. }
  8867. break;
  8868. case ZEND_AST_UNARY_PLUS:
  8869. case ZEND_AST_UNARY_MINUS:
  8870. zend_eval_const_expr(&ast->child[0]);
  8871. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  8872. return;
  8873. }
  8874. if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
  8875. return;
  8876. }
  8877. break;
  8878. case ZEND_AST_COALESCE:
  8879. /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
  8880. if (ast->child[0]->kind == ZEND_AST_DIM) {
  8881. ast->child[0]->attr |= ZEND_DIM_IS;
  8882. }
  8883. zend_eval_const_expr(&ast->child[0]);
  8884. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  8885. /* ensure everything was compile-time evaluated at least once */
  8886. zend_eval_const_expr(&ast->child[1]);
  8887. return;
  8888. }
  8889. if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
  8890. zend_eval_const_expr(&ast->child[1]);
  8891. *ast_ptr = ast->child[1];
  8892. ast->child[1] = NULL;
  8893. zend_ast_destroy(ast);
  8894. } else {
  8895. *ast_ptr = ast->child[0];
  8896. ast->child[0] = NULL;
  8897. zend_ast_destroy(ast);
  8898. }
  8899. return;
  8900. case ZEND_AST_CONDITIONAL:
  8901. {
  8902. zend_ast **child, *child_ast;
  8903. zend_eval_const_expr(&ast->child[0]);
  8904. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  8905. /* ensure everything was compile-time evaluated at least once */
  8906. if (ast->child[1]) {
  8907. zend_eval_const_expr(&ast->child[1]);
  8908. }
  8909. zend_eval_const_expr(&ast->child[2]);
  8910. return;
  8911. }
  8912. child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
  8913. if (*child == NULL) {
  8914. child--;
  8915. }
  8916. child_ast = *child;
  8917. *child = NULL;
  8918. zend_ast_destroy(ast);
  8919. *ast_ptr = child_ast;
  8920. zend_eval_const_expr(ast_ptr);
  8921. return;
  8922. }
  8923. case ZEND_AST_DIM:
  8924. {
  8925. /* constant expression should be always read context ... */
  8926. zval *container, *dim;
  8927. if (ast->child[1] == NULL) {
  8928. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  8929. }
  8930. if (ast->attr & ZEND_DIM_ALTERNATIVE_SYNTAX) {
  8931. ast->attr &= ~ZEND_DIM_ALTERNATIVE_SYNTAX; /* remove flag to avoid duplicate warning */
  8932. zend_error(E_COMPILE_ERROR, "Array and string offset access syntax with curly braces is no longer supported");
  8933. }
  8934. /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
  8935. if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
  8936. ast->child[0]->attr |= ZEND_DIM_IS;
  8937. }
  8938. zend_eval_const_expr(&ast->child[0]);
  8939. zend_eval_const_expr(&ast->child[1]);
  8940. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  8941. return;
  8942. }
  8943. container = zend_ast_get_zval(ast->child[0]);
  8944. dim = zend_ast_get_zval(ast->child[1]);
  8945. if (Z_TYPE_P(container) == IS_ARRAY) {
  8946. zval *el;
  8947. if (Z_TYPE_P(dim) == IS_LONG) {
  8948. el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
  8949. if (el) {
  8950. ZVAL_COPY(&result, el);
  8951. } else {
  8952. return;
  8953. }
  8954. } else if (Z_TYPE_P(dim) == IS_STRING) {
  8955. el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
  8956. if (el) {
  8957. ZVAL_COPY(&result, el);
  8958. } else {
  8959. return;
  8960. }
  8961. } else {
  8962. return; /* warning... handle at runtime */
  8963. }
  8964. } else if (Z_TYPE_P(container) == IS_STRING) {
  8965. zend_long offset;
  8966. zend_uchar c;
  8967. if (Z_TYPE_P(dim) == IS_LONG) {
  8968. offset = Z_LVAL_P(dim);
  8969. } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
  8970. return;
  8971. }
  8972. if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
  8973. return;
  8974. }
  8975. c = (zend_uchar) Z_STRVAL_P(container)[offset];
  8976. ZVAL_CHAR(&result, c);
  8977. } else if (Z_TYPE_P(container) <= IS_FALSE) {
  8978. ZVAL_NULL(&result);
  8979. } else {
  8980. return;
  8981. }
  8982. break;
  8983. }
  8984. case ZEND_AST_ARRAY:
  8985. if (!zend_try_ct_eval_array(&result, ast)) {
  8986. return;
  8987. }
  8988. break;
  8989. case ZEND_AST_MAGIC_CONST:
  8990. if (!zend_try_ct_eval_magic_const(&result, ast)) {
  8991. return;
  8992. }
  8993. break;
  8994. case ZEND_AST_CONST:
  8995. {
  8996. zend_ast *name_ast = ast->child[0];
  8997. bool is_fully_qualified;
  8998. zend_string *resolved_name = zend_resolve_const_name(
  8999. zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
  9000. if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
  9001. zend_string_release_ex(resolved_name, 0);
  9002. return;
  9003. }
  9004. zend_string_release_ex(resolved_name, 0);
  9005. break;
  9006. }
  9007. case ZEND_AST_CLASS_CONST:
  9008. {
  9009. zend_ast *class_ast;
  9010. zend_ast *name_ast;
  9011. zend_string *resolved_name;
  9012. zend_eval_const_expr(&ast->child[0]);
  9013. zend_eval_const_expr(&ast->child[1]);
  9014. class_ast = ast->child[0];
  9015. name_ast = ast->child[1];
  9016. if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
  9017. return;
  9018. }
  9019. resolved_name = zend_resolve_class_name_ast(class_ast);
  9020. if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
  9021. zend_string_release_ex(resolved_name, 0);
  9022. return;
  9023. }
  9024. zend_string_release_ex(resolved_name, 0);
  9025. break;
  9026. }
  9027. case ZEND_AST_CLASS_NAME:
  9028. {
  9029. zend_ast *class_ast = ast->child[0];
  9030. if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
  9031. return;
  9032. }
  9033. break;
  9034. }
  9035. // TODO: We should probably use zend_ast_apply to recursively walk nodes without
  9036. // special handling. It is required that all nodes that are part of a const expr
  9037. // are visited. Probably we should be distinguishing evaluation of const expr and
  9038. // normal exprs here.
  9039. case ZEND_AST_ARG_LIST:
  9040. {
  9041. zend_ast_list *list = zend_ast_get_list(ast);
  9042. for (uint32_t i = 0; i < list->children; i++) {
  9043. zend_eval_const_expr(&list->child[i]);
  9044. }
  9045. return;
  9046. }
  9047. case ZEND_AST_NEW:
  9048. zend_eval_const_expr(&ast->child[0]);
  9049. zend_eval_const_expr(&ast->child[1]);
  9050. return;
  9051. case ZEND_AST_NAMED_ARG:
  9052. zend_eval_const_expr(&ast->child[1]);
  9053. return;
  9054. default:
  9055. return;
  9056. }
  9057. zend_ast_destroy(ast);
  9058. *ast_ptr = zend_ast_create_zval(&result);
  9059. }
  9060. /* }}} */