zend_compile.c 255 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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_compile.h"
  23. #include "zend_constants.h"
  24. #include "zend_llist.h"
  25. #include "zend_API.h"
  26. #include "zend_exceptions.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_virtual_cwd.h"
  29. #include "zend_multibyte.h"
  30. #include "zend_language_scanner.h"
  31. #include "zend_inheritance.h"
  32. #include "zend_vm.h"
  33. #define SET_NODE(target, src) do { \
  34. target ## _type = (src)->op_type; \
  35. if ((src)->op_type == IS_CONST) { \
  36. target.constant = zend_add_literal(CG(active_op_array), &(src)->u.constant); \
  37. } else { \
  38. target = (src)->u.op; \
  39. } \
  40. } while (0)
  41. #define GET_NODE(target, src) do { \
  42. (target)->op_type = src ## _type; \
  43. if ((target)->op_type == IS_CONST) { \
  44. ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
  45. } else { \
  46. (target)->u.op = src; \
  47. } \
  48. } while (0)
  49. #define FC(member) (CG(file_context).member)
  50. typedef struct _zend_loop_var {
  51. zend_uchar opcode;
  52. zend_uchar var_type;
  53. uint32_t var_num;
  54. union {
  55. uint32_t try_catch_offset;
  56. uint32_t live_range_offset;
  57. } u;
  58. } zend_loop_var;
  59. static inline uint32_t zend_alloc_cache_slot(void) {
  60. zend_op_array *op_array = CG(active_op_array);
  61. uint32_t ret = op_array->cache_size;
  62. op_array->cache_size += sizeof(void*);
  63. return ret;
  64. }
  65. #define POLYMORPHIC_CACHE_SLOT_SIZE 2
  66. static inline uint32_t zend_alloc_polymorphic_cache_slot(void) {
  67. zend_op_array *op_array = CG(active_op_array);
  68. uint32_t ret = op_array->cache_size;
  69. op_array->cache_size += POLYMORPHIC_CACHE_SLOT_SIZE * sizeof(void*);
  70. return ret;
  71. }
  72. ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
  73. ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);
  74. #ifndef ZTS
  75. ZEND_API zend_compiler_globals compiler_globals;
  76. ZEND_API zend_executor_globals executor_globals;
  77. #endif
  78. static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2);
  79. static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
  80. static void init_op(zend_op *op)
  81. {
  82. MAKE_NOP(op);
  83. op->extended_value = 0;
  84. op->lineno = CG(zend_lineno);
  85. }
  86. static zend_op *get_next_op(zend_op_array *op_array)
  87. {
  88. uint32_t next_op_num = op_array->last++;
  89. zend_op *next_op;
  90. if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
  91. CG(context).opcodes_size *= 4;
  92. op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
  93. }
  94. next_op = &(op_array->opcodes[next_op_num]);
  95. init_op(next_op);
  96. return next_op;
  97. }
  98. static zend_brk_cont_element *get_next_brk_cont_element(void)
  99. {
  100. CG(context).last_brk_cont++;
  101. CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
  102. return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
  103. }
  104. static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
  105. {
  106. zend_property_info *property_info = Z_PTR_P(zv);
  107. zend_string_release_ex(property_info->name, 1);
  108. free(property_info);
  109. }
  110. /* }}} */
  111. static zend_string *zend_build_runtime_definition_key(zend_string *name, unsigned char *lex_pos) /* {{{ */
  112. {
  113. zend_string *result;
  114. char char_pos_buf[32];
  115. size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
  116. zend_string *filename = CG(active_op_array)->filename;
  117. /* NULL, name length, filename length, last accepting char position length */
  118. result = zend_string_alloc(1 + ZSTR_LEN(name) + ZSTR_LEN(filename) + char_pos_len, 0);
  119. sprintf(ZSTR_VAL(result), "%c%s%s%s", '\0', ZSTR_VAL(name), ZSTR_VAL(filename), char_pos_buf);
  120. return zend_new_interned_string(result);
  121. }
  122. /* }}} */
  123. static zend_bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
  124. {
  125. const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  126. if (ns_separator != NULL) {
  127. *result = ns_separator + 1;
  128. *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. /* }}} */
  134. struct reserved_class_name {
  135. const char *name;
  136. size_t len;
  137. };
  138. static const struct reserved_class_name reserved_class_names[] = {
  139. {ZEND_STRL("bool")},
  140. {ZEND_STRL("false")},
  141. {ZEND_STRL("float")},
  142. {ZEND_STRL("int")},
  143. {ZEND_STRL("null")},
  144. {ZEND_STRL("parent")},
  145. {ZEND_STRL("self")},
  146. {ZEND_STRL("static")},
  147. {ZEND_STRL("string")},
  148. {ZEND_STRL("true")},
  149. {ZEND_STRL("void")},
  150. {ZEND_STRL("iterable")},
  151. {ZEND_STRL("object")},
  152. {NULL, 0}
  153. };
  154. static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
  155. {
  156. const struct reserved_class_name *reserved = reserved_class_names;
  157. const char *uqname = ZSTR_VAL(name);
  158. size_t uqname_len = ZSTR_LEN(name);
  159. zend_get_unqualified_name(name, &uqname, &uqname_len);
  160. for (; reserved->name; ++reserved) {
  161. if (uqname_len == reserved->len
  162. && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
  163. ) {
  164. return 1;
  165. }
  166. }
  167. return 0;
  168. }
  169. /* }}} */
  170. void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
  171. {
  172. if (zend_is_reserved_class_name(name)) {
  173. zend_error_noreturn(E_COMPILE_ERROR,
  174. "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
  175. }
  176. }
  177. /* }}} */
  178. typedef struct _builtin_type_info {
  179. const char* name;
  180. const size_t name_len;
  181. const zend_uchar type;
  182. } builtin_type_info;
  183. static const builtin_type_info builtin_types[] = {
  184. {ZEND_STRL("int"), IS_LONG},
  185. {ZEND_STRL("float"), IS_DOUBLE},
  186. {ZEND_STRL("string"), IS_STRING},
  187. {ZEND_STRL("bool"), _IS_BOOL},
  188. {ZEND_STRL("void"), IS_VOID},
  189. {ZEND_STRL("iterable"), IS_ITERABLE},
  190. {ZEND_STRL("object"), IS_OBJECT},
  191. {NULL, 0, IS_UNDEF}
  192. };
  193. static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
  194. {
  195. const builtin_type_info *info = &builtin_types[0];
  196. for (; info->name; ++info) {
  197. if (ZSTR_LEN(name) == info->name_len
  198. && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
  199. ) {
  200. return info->type;
  201. }
  202. }
  203. return 0;
  204. }
  205. /* }}} */
  206. void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
  207. {
  208. *prev_context = CG(context);
  209. CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
  210. CG(context).vars_size = 0;
  211. CG(context).literals_size = 0;
  212. CG(context).backpatch_count = 0;
  213. CG(context).fast_call_var = -1;
  214. CG(context).try_catch_offset = -1;
  215. CG(context).current_brk_cont = -1;
  216. CG(context).last_brk_cont = 0;
  217. CG(context).brk_cont_array = NULL;
  218. CG(context).labels = NULL;
  219. }
  220. /* }}} */
  221. void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
  222. {
  223. if (CG(context).brk_cont_array) {
  224. efree(CG(context).brk_cont_array);
  225. CG(context).brk_cont_array = NULL;
  226. }
  227. if (CG(context).labels) {
  228. zend_hash_destroy(CG(context).labels);
  229. FREE_HASHTABLE(CG(context).labels);
  230. CG(context).labels = NULL;
  231. }
  232. CG(context) = *prev_context;
  233. }
  234. /* }}} */
  235. static void zend_reset_import_tables(void) /* {{{ */
  236. {
  237. if (FC(imports)) {
  238. zend_hash_destroy(FC(imports));
  239. efree(FC(imports));
  240. FC(imports) = NULL;
  241. }
  242. if (FC(imports_function)) {
  243. zend_hash_destroy(FC(imports_function));
  244. efree(FC(imports_function));
  245. FC(imports_function) = NULL;
  246. }
  247. if (FC(imports_const)) {
  248. zend_hash_destroy(FC(imports_const));
  249. efree(FC(imports_const));
  250. FC(imports_const) = NULL;
  251. }
  252. }
  253. /* }}} */
  254. static void zend_end_namespace(void) /* {{{ */ {
  255. FC(in_namespace) = 0;
  256. zend_reset_import_tables();
  257. if (FC(current_namespace)) {
  258. zend_string_release_ex(FC(current_namespace), 0);
  259. FC(current_namespace) = NULL;
  260. }
  261. }
  262. /* }}} */
  263. void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
  264. {
  265. *prev_context = CG(file_context);
  266. FC(imports) = NULL;
  267. FC(imports_function) = NULL;
  268. FC(imports_const) = NULL;
  269. FC(current_namespace) = NULL;
  270. FC(in_namespace) = 0;
  271. FC(has_bracketed_namespaces) = 0;
  272. FC(declarables).ticks = 0;
  273. zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
  274. }
  275. /* }}} */
  276. void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
  277. {
  278. zend_end_namespace();
  279. zend_hash_destroy(&FC(seen_symbols));
  280. CG(file_context) = *prev_context;
  281. }
  282. /* }}} */
  283. void zend_init_compiler_data_structures(void) /* {{{ */
  284. {
  285. zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
  286. zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
  287. CG(active_class_entry) = NULL;
  288. CG(in_compilation) = 0;
  289. CG(start_lineno) = 0;
  290. CG(encoding_declared) = 0;
  291. }
  292. /* }}} */
  293. static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
  294. zval *zv = zend_hash_find(&FC(seen_symbols), name);
  295. if (zv) {
  296. Z_LVAL_P(zv) |= kind;
  297. } else {
  298. zval tmp;
  299. ZVAL_LONG(&tmp, kind);
  300. zend_hash_add_new(&FC(seen_symbols), name, &tmp);
  301. }
  302. }
  303. static zend_bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
  304. zval *zv = zend_hash_find(&FC(seen_symbols), name);
  305. return zv && (Z_LVAL_P(zv) & kind) != 0;
  306. }
  307. ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
  308. {
  309. zend_file_handle_dtor(fh);
  310. }
  311. /* }}} */
  312. void init_compiler(void) /* {{{ */
  313. {
  314. CG(arena) = zend_arena_create(64 * 1024);
  315. CG(active_op_array) = NULL;
  316. memset(&CG(context), 0, sizeof(CG(context)));
  317. zend_init_compiler_data_structures();
  318. zend_init_rsrc_list();
  319. zend_hash_init(&CG(filenames_table), 8, NULL, ZVAL_PTR_DTOR, 0);
  320. zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
  321. CG(unclean_shutdown) = 0;
  322. }
  323. /* }}} */
  324. void shutdown_compiler(void) /* {{{ */
  325. {
  326. zend_stack_destroy(&CG(loop_var_stack));
  327. zend_stack_destroy(&CG(delayed_oplines_stack));
  328. zend_hash_destroy(&CG(filenames_table));
  329. zend_arena_destroy(CG(arena));
  330. }
  331. /* }}} */
  332. ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
  333. {
  334. zval *p, rv;
  335. if ((p = zend_hash_find(&CG(filenames_table), new_compiled_filename))) {
  336. ZEND_ASSERT(Z_TYPE_P(p) == IS_STRING);
  337. CG(compiled_filename) = Z_STR_P(p);
  338. return Z_STR_P(p);
  339. }
  340. new_compiled_filename = zend_new_interned_string(zend_string_copy(new_compiled_filename));
  341. ZVAL_STR(&rv, new_compiled_filename);
  342. zend_hash_add_new(&CG(filenames_table), new_compiled_filename, &rv);
  343. CG(compiled_filename) = new_compiled_filename;
  344. return new_compiled_filename;
  345. }
  346. /* }}} */
  347. ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
  348. {
  349. CG(compiled_filename) = original_compiled_filename;
  350. }
  351. /* }}} */
  352. ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
  353. {
  354. return CG(compiled_filename);
  355. }
  356. /* }}} */
  357. ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
  358. {
  359. return CG(zend_lineno);
  360. }
  361. /* }}} */
  362. ZEND_API zend_bool zend_is_compiling(void) /* {{{ */
  363. {
  364. return CG(in_compilation);
  365. }
  366. /* }}} */
  367. static uint32_t get_temporary_variable(zend_op_array *op_array) /* {{{ */
  368. {
  369. return (uint32_t)op_array->T++;
  370. }
  371. /* }}} */
  372. static int lookup_cv(zend_op_array *op_array, zend_string *name) /* {{{ */{
  373. int i = 0;
  374. zend_ulong hash_value = zend_string_hash_val(name);
  375. while (i < op_array->last_var) {
  376. if (ZSTR_VAL(op_array->vars[i]) == ZSTR_VAL(name) ||
  377. (ZSTR_H(op_array->vars[i]) == hash_value &&
  378. zend_string_equal_content(op_array->vars[i], name))) {
  379. return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
  380. }
  381. i++;
  382. }
  383. i = op_array->last_var;
  384. op_array->last_var++;
  385. if (op_array->last_var > CG(context).vars_size) {
  386. CG(context).vars_size += 16; /* FIXME */
  387. op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
  388. }
  389. op_array->vars[i] = zend_string_copy(name);
  390. return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
  391. }
  392. /* }}} */
  393. void zend_del_literal(zend_op_array *op_array, int n) /* {{{ */
  394. {
  395. zval_ptr_dtor_nogc(CT_CONSTANT_EX(op_array, n));
  396. if (n + 1 == op_array->last_literal) {
  397. op_array->last_literal--;
  398. } else {
  399. ZVAL_UNDEF(CT_CONSTANT_EX(op_array, n));
  400. }
  401. }
  402. /* }}} */
  403. static inline zend_string *zval_make_interned_string(zval *zv) /* {{{ */
  404. {
  405. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  406. Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
  407. if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
  408. Z_TYPE_FLAGS_P(zv) = 0;
  409. }
  410. return Z_STR_P(zv);
  411. }
  412. /* Common part of zend_add_literal and zend_append_individual_literal */
  413. static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
  414. {
  415. zval *lit = CT_CONSTANT_EX(op_array, literal_position);
  416. if (Z_TYPE_P(zv) == IS_STRING) {
  417. zval_make_interned_string(zv);
  418. }
  419. ZVAL_COPY_VALUE(lit, zv);
  420. Z_EXTRA_P(lit) = 0;
  421. }
  422. /* }}} */
  423. /* Is used while compiling a function, using the context to keep track
  424. of an approximate size to avoid to relocate to often.
  425. Literals are truncated to actual size in the second compiler pass (pass_two()). */
  426. int zend_add_literal(zend_op_array *op_array, zval *zv) /* {{{ */
  427. {
  428. int i = op_array->last_literal;
  429. op_array->last_literal++;
  430. if (i >= CG(context).literals_size) {
  431. while (i >= CG(context).literals_size) {
  432. CG(context).literals_size += 16; /* FIXME */
  433. }
  434. op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
  435. }
  436. zend_insert_literal(op_array, zv, i);
  437. return i;
  438. }
  439. /* }}} */
  440. static inline int zend_add_literal_string(zend_op_array *op_array, zend_string **str) /* {{{ */
  441. {
  442. int ret;
  443. zval zv;
  444. ZVAL_STR(&zv, *str);
  445. ret = zend_add_literal(op_array, &zv);
  446. *str = Z_STR(zv);
  447. return ret;
  448. }
  449. /* }}} */
  450. static int zend_add_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
  451. {
  452. /* Original name */
  453. int ret = zend_add_literal_string(op_array, &name);
  454. /* Lowercased name */
  455. zend_string *lc_name = zend_string_tolower(name);
  456. zend_add_literal_string(op_array, &lc_name);
  457. return ret;
  458. }
  459. /* }}} */
  460. static int zend_add_ns_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
  461. {
  462. const char *unqualified_name;
  463. size_t unqualified_name_len;
  464. /* Original name */
  465. int ret = zend_add_literal_string(op_array, &name);
  466. /* Lowercased name */
  467. zend_string *lc_name = zend_string_tolower(name);
  468. zend_add_literal_string(op_array, &lc_name);
  469. /* Lowercased unqualfied name */
  470. if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
  471. lc_name = zend_string_alloc(unqualified_name_len, 0);
  472. zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
  473. zend_add_literal_string(op_array, &lc_name);
  474. }
  475. return ret;
  476. }
  477. /* }}} */
  478. static int zend_add_class_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
  479. {
  480. /* Original name */
  481. int ret = zend_add_literal_string(op_array, &name);
  482. /* Lowercased name */
  483. zend_string *lc_name = zend_string_tolower(name);
  484. zend_add_literal_string(op_array, &lc_name);
  485. return ret;
  486. }
  487. /* }}} */
  488. static int zend_add_const_name_literal(zend_op_array *op_array, zend_string *name, zend_bool unqualified) /* {{{ */
  489. {
  490. zend_string *tmp_name;
  491. int ret = zend_add_literal_string(op_array, &name);
  492. size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
  493. const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  494. if (after_ns) {
  495. after_ns += 1;
  496. ns_len = after_ns - ZSTR_VAL(name) - 1;
  497. after_ns_len = ZSTR_LEN(name) - ns_len - 1;
  498. /* lowercased namespace name & original constant name */
  499. tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
  500. zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
  501. zend_add_literal_string(op_array, &tmp_name);
  502. /* lowercased namespace name & lowercased constant name */
  503. tmp_name = zend_string_tolower(name);
  504. zend_add_literal_string(op_array, &tmp_name);
  505. if (!unqualified) {
  506. return ret;
  507. }
  508. } else {
  509. after_ns = ZSTR_VAL(name);
  510. }
  511. /* original unqualified constant name */
  512. tmp_name = zend_string_init(after_ns, after_ns_len, 0);
  513. zend_add_literal_string(op_array, &tmp_name);
  514. /* lowercased unqualified constant name */
  515. tmp_name = zend_string_alloc(after_ns_len, 0);
  516. zend_str_tolower_copy(ZSTR_VAL(tmp_name), after_ns, after_ns_len);
  517. zend_add_literal_string(op_array, &tmp_name);
  518. return ret;
  519. }
  520. /* }}} */
  521. #define LITERAL_STR(op, str) do { \
  522. zval _c; \
  523. ZVAL_STR(&_c, str); \
  524. op.constant = zend_add_literal(CG(active_op_array), &_c); \
  525. } while (0)
  526. void zend_stop_lexing(void)
  527. {
  528. if (LANG_SCNG(on_event)) {
  529. LANG_SCNG(on_event)(ON_STOP, END, 0, LANG_SCNG(on_event_context));
  530. }
  531. LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
  532. }
  533. static uint32_t zend_start_live_range(zend_op_array *op_array, uint32_t start) /* {{{ */
  534. {
  535. zend_live_range *range;
  536. op_array->last_live_range++;
  537. op_array->live_range = erealloc(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
  538. range = op_array->live_range + op_array->last_live_range - 1;
  539. range->start = start;
  540. return op_array->last_live_range - 1;
  541. }
  542. /* }}} */
  543. static void zend_end_live_range(zend_op_array *op_array, uint32_t offset, uint32_t end, uint32_t kind, uint32_t var) /* {{{ */
  544. {
  545. zend_live_range *range = op_array->live_range + offset;
  546. if (range->start == end && offset == (uint32_t)op_array->last_live_range - 1) {
  547. op_array->last_live_range--;
  548. } else {
  549. range->end = end;
  550. range->var = (var * sizeof(zval)) | kind;
  551. }
  552. }
  553. /* }}} */
  554. static inline void zend_begin_loop(
  555. zend_uchar free_opcode, const znode *loop_var, zend_bool is_switch) /* {{{ */
  556. {
  557. zend_brk_cont_element *brk_cont_element;
  558. int parent = CG(context).current_brk_cont;
  559. zend_loop_var info = {0};
  560. CG(context).current_brk_cont = CG(context).last_brk_cont;
  561. brk_cont_element = get_next_brk_cont_element();
  562. brk_cont_element->parent = parent;
  563. brk_cont_element->is_switch = is_switch;
  564. if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
  565. uint32_t start = get_next_op_number(CG(active_op_array));
  566. info.opcode = free_opcode;
  567. info.var_type = loop_var->op_type;
  568. info.var_num = loop_var->u.op.var;
  569. info.u.live_range_offset = zend_start_live_range(CG(active_op_array), start);
  570. brk_cont_element->start = start;
  571. } else {
  572. info.opcode = ZEND_NOP;
  573. /* The start field is used to free temporary variables in case of exceptions.
  574. * We won't try to free something of we don't have loop variable. */
  575. brk_cont_element->start = -1;
  576. }
  577. zend_stack_push(&CG(loop_var_stack), &info);
  578. }
  579. /* }}} */
  580. static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
  581. {
  582. uint32_t end = get_next_op_number(CG(active_op_array));
  583. zend_brk_cont_element *brk_cont_element
  584. = &CG(context).brk_cont_array[CG(context).current_brk_cont];
  585. brk_cont_element->cont = cont_addr;
  586. brk_cont_element->brk = end;
  587. CG(context).current_brk_cont = brk_cont_element->parent;
  588. if (brk_cont_element->start != -1) {
  589. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  590. zend_end_live_range(CG(active_op_array), loop_var->u.live_range_offset, end,
  591. loop_var->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR,
  592. var_node->u.op.var);
  593. }
  594. zend_stack_del_top(&CG(loop_var_stack));
  595. }
  596. /* }}} */
  597. void zend_do_free(znode *op1) /* {{{ */
  598. {
  599. if (op1->op_type == IS_TMP_VAR) {
  600. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  601. while (opline->opcode == ZEND_END_SILENCE) {
  602. opline--;
  603. }
  604. if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
  605. if (opline->opcode == ZEND_BOOL || opline->opcode == ZEND_BOOL_NOT) {
  606. return;
  607. }
  608. }
  609. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  610. } else if (op1->op_type == IS_VAR) {
  611. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  612. while (opline->opcode == ZEND_END_SILENCE ||
  613. opline->opcode == ZEND_EXT_FCALL_END ||
  614. opline->opcode == ZEND_OP_DATA) {
  615. opline--;
  616. }
  617. if (opline->result_type == IS_VAR
  618. && opline->result.var == op1->u.op.var) {
  619. if (opline->opcode == ZEND_FETCH_THIS) {
  620. opline->opcode = ZEND_NOP;
  621. opline->result_type = IS_UNUSED;
  622. } else {
  623. opline->result_type = IS_UNUSED;
  624. }
  625. } else {
  626. while (opline >= CG(active_op_array)->opcodes) {
  627. if ((opline->opcode == ZEND_FETCH_LIST_R ||
  628. opline->opcode == ZEND_FETCH_LIST_W) &&
  629. opline->op1_type == IS_VAR &&
  630. opline->op1.var == op1->u.op.var) {
  631. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  632. return;
  633. }
  634. if (opline->result_type == IS_VAR
  635. && opline->result.var == op1->u.op.var) {
  636. if (opline->opcode == ZEND_NEW) {
  637. zend_emit_op(NULL, ZEND_FREE, op1, NULL);
  638. }
  639. break;
  640. }
  641. opline--;
  642. }
  643. }
  644. } else if (op1->op_type == IS_CONST) {
  645. /* Destroy value without using GC: When opcache moves arrays into SHM it will
  646. * free the zend_array structure, so references to it from outside the op array
  647. * become invalid. GC would cause such a reference in the root buffer. */
  648. zval_ptr_dtor_nogc(&op1->u.constant);
  649. }
  650. }
  651. /* }}} */
  652. uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
  653. {
  654. uint32_t new_flags = flags | new_flag;
  655. if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  656. zend_throw_exception(zend_ce_compile_error,
  657. "Multiple abstract modifiers are not allowed", 0);
  658. return 0;
  659. }
  660. if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
  661. zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
  662. return 0;
  663. }
  664. if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
  665. zend_throw_exception(zend_ce_compile_error,
  666. "Cannot use the final modifier on an abstract class", 0);
  667. return 0;
  668. }
  669. return new_flags;
  670. }
  671. /* }}} */
  672. uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
  673. {
  674. uint32_t new_flags = flags | new_flag;
  675. if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
  676. zend_throw_exception(zend_ce_compile_error,
  677. "Multiple access type modifiers are not allowed", 0);
  678. return 0;
  679. }
  680. if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
  681. zend_throw_exception(zend_ce_compile_error, "Multiple abstract modifiers are not allowed", 0);
  682. return 0;
  683. }
  684. if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
  685. zend_throw_exception(zend_ce_compile_error, "Multiple static modifiers are not allowed", 0);
  686. return 0;
  687. }
  688. if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
  689. zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
  690. return 0;
  691. }
  692. if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
  693. zend_throw_exception(zend_ce_compile_error,
  694. "Cannot use the final modifier on an abstract class member", 0);
  695. return 0;
  696. }
  697. return new_flags;
  698. }
  699. /* }}} */
  700. zend_string *zend_concat3(char *str1, size_t str1_len, char *str2, size_t str2_len, char *str3, size_t str3_len) /* {{{ */
  701. {
  702. size_t len = str1_len + str2_len + str3_len;
  703. zend_string *res = zend_string_alloc(len, 0);
  704. memcpy(ZSTR_VAL(res), str1, str1_len);
  705. memcpy(ZSTR_VAL(res) + str1_len, str2, str2_len);
  706. memcpy(ZSTR_VAL(res) + str1_len + str2_len, str3, str3_len);
  707. ZSTR_VAL(res)[len] = '\0';
  708. return res;
  709. }
  710. zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
  711. return zend_concat3(name1, name1_len, "\\", 1, name2, name2_len);
  712. }
  713. zend_string *zend_prefix_with_ns(zend_string *name) {
  714. if (FC(current_namespace)) {
  715. zend_string *ns = FC(current_namespace);
  716. return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
  717. } else {
  718. return zend_string_copy(name);
  719. }
  720. }
  721. void *zend_hash_find_ptr_lc(HashTable *ht, const char *str, size_t len) {
  722. void *result;
  723. zend_string *lcname;
  724. ALLOCA_FLAG(use_heap);
  725. ZSTR_ALLOCA_ALLOC(lcname, len, use_heap);
  726. zend_str_tolower_copy(ZSTR_VAL(lcname), str, len);
  727. result = zend_hash_find_ptr(ht, lcname);
  728. ZSTR_ALLOCA_FREE(lcname, use_heap);
  729. return result;
  730. }
  731. zend_string *zend_resolve_non_class_name(
  732. zend_string *name, uint32_t type, zend_bool *is_fully_qualified,
  733. zend_bool case_sensitive, HashTable *current_import_sub
  734. ) {
  735. char *compound;
  736. *is_fully_qualified = 0;
  737. if (ZSTR_VAL(name)[0] == '\\') {
  738. /* Remove \ prefix (only relevant if this is a string rather than a label) */
  739. *is_fully_qualified = 1;
  740. return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
  741. }
  742. if (type == ZEND_NAME_FQ) {
  743. *is_fully_qualified = 1;
  744. return zend_string_copy(name);
  745. }
  746. if (type == ZEND_NAME_RELATIVE) {
  747. *is_fully_qualified = 1;
  748. return zend_prefix_with_ns(name);
  749. }
  750. if (current_import_sub) {
  751. /* If an unqualified name is a function/const alias, replace it. */
  752. zend_string *import_name;
  753. if (case_sensitive) {
  754. import_name = zend_hash_find_ptr(current_import_sub, name);
  755. } else {
  756. import_name = zend_hash_find_ptr_lc(current_import_sub, ZSTR_VAL(name), ZSTR_LEN(name));
  757. }
  758. if (import_name) {
  759. *is_fully_qualified = 1;
  760. return zend_string_copy(import_name);
  761. }
  762. }
  763. compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  764. if (compound) {
  765. *is_fully_qualified = 1;
  766. }
  767. if (compound && FC(imports)) {
  768. /* If the first part of a qualified name is an alias, substitute it. */
  769. size_t len = compound - ZSTR_VAL(name);
  770. zend_string *import_name = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
  771. if (import_name) {
  772. return zend_concat_names(
  773. ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
  774. }
  775. }
  776. return zend_prefix_with_ns(name);
  777. }
  778. /* }}} */
  779. zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */
  780. {
  781. return zend_resolve_non_class_name(
  782. name, type, is_fully_qualified, 0, FC(imports_function));
  783. }
  784. /* }}} */
  785. zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */ {
  786. return zend_resolve_non_class_name(
  787. name, type, is_fully_qualified, 1, FC(imports_const));
  788. }
  789. /* }}} */
  790. zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
  791. {
  792. char *compound;
  793. if (type == ZEND_NAME_RELATIVE) {
  794. return zend_prefix_with_ns(name);
  795. }
  796. if (type == ZEND_NAME_FQ || ZSTR_VAL(name)[0] == '\\') {
  797. /* Remove \ prefix (only relevant if this is a string rather than a label) */
  798. if (ZSTR_VAL(name)[0] == '\\') {
  799. name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
  800. } else {
  801. zend_string_addref(name);
  802. }
  803. /* Ensure that \self, \parent and \static are not used */
  804. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
  805. zend_error_noreturn(E_COMPILE_ERROR, "'\\%s' is an invalid class name", ZSTR_VAL(name));
  806. }
  807. return name;
  808. }
  809. if (FC(imports)) {
  810. compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
  811. if (compound) {
  812. /* If the first part of a qualified name is an alias, substitute it. */
  813. size_t len = compound - ZSTR_VAL(name);
  814. zend_string *import_name =
  815. zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
  816. if (import_name) {
  817. return zend_concat_names(
  818. ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
  819. }
  820. } else {
  821. /* If an unqualified name is an alias, replace it. */
  822. zend_string *import_name
  823. = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), ZSTR_LEN(name));
  824. if (import_name) {
  825. return zend_string_copy(import_name);
  826. }
  827. }
  828. }
  829. /* If not fully qualified and not an alias, prepend the current namespace */
  830. return zend_prefix_with_ns(name);
  831. }
  832. /* }}} */
  833. zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
  834. {
  835. zval *class_name = zend_ast_get_zval(ast);
  836. if (Z_TYPE_P(class_name) != IS_STRING) {
  837. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  838. }
  839. return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
  840. }
  841. /* }}} */
  842. static void label_ptr_dtor(zval *zv) /* {{{ */
  843. {
  844. efree_size(Z_PTR_P(zv), sizeof(zend_label));
  845. }
  846. /* }}} */
  847. static void str_dtor(zval *zv) /* {{{ */ {
  848. zend_string_release_ex(Z_STR_P(zv), 0);
  849. }
  850. /* }}} */
  851. static zend_bool zend_is_call(zend_ast *ast);
  852. static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
  853. {
  854. zend_op_array *op_array = CG(active_op_array);
  855. uint32_t try_catch_offset = op_array->last_try_catch++;
  856. zend_try_catch_element *elem;
  857. op_array->try_catch_array = safe_erealloc(
  858. op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
  859. elem = &op_array->try_catch_array[try_catch_offset];
  860. elem->try_op = try_op;
  861. elem->catch_op = 0;
  862. elem->finally_op = 0;
  863. elem->finally_end = 0;
  864. return try_catch_offset;
  865. }
  866. /* }}} */
  867. ZEND_API void function_add_ref(zend_function *function) /* {{{ */
  868. {
  869. if (function->type == ZEND_USER_FUNCTION) {
  870. zend_op_array *op_array = &function->op_array;
  871. if (op_array->refcount) {
  872. (*op_array->refcount)++;
  873. }
  874. if (op_array->static_variables) {
  875. if (!(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
  876. GC_ADDREF(op_array->static_variables);
  877. }
  878. }
  879. op_array->run_time_cache = NULL;
  880. } else if (function->type == ZEND_INTERNAL_FUNCTION) {
  881. if (function->common.function_name) {
  882. zend_string_addref(function->common.function_name);
  883. }
  884. }
  885. }
  886. /* }}} */
  887. ZEND_API int do_bind_function(const zend_op_array *op_array, const zend_op *opline, HashTable *function_table, zend_bool compile_time) /* {{{ */
  888. {
  889. zend_function *function, *new_function;
  890. zval *lcname, *rtd_key, *zv;
  891. if (compile_time) {
  892. lcname = CT_CONSTANT_EX(op_array, opline->op1.constant);
  893. rtd_key = lcname + 1;
  894. } else {
  895. lcname = RT_CONSTANT(opline, opline->op1);
  896. rtd_key = lcname + 1;
  897. }
  898. zv = zend_hash_find_ex(function_table, Z_STR_P(rtd_key), 1);
  899. function = (zend_function*)Z_PTR_P(zv);
  900. new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
  901. memcpy(new_function, function, sizeof(zend_op_array));
  902. if (zend_hash_add_ptr(function_table, Z_STR_P(lcname), new_function) == NULL) {
  903. int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
  904. zend_function *old_function;
  905. zv = zend_hash_find_ex(function_table, Z_STR_P(lcname), 1);
  906. ZEND_ASSERT(zv != NULL);
  907. old_function = (zend_function*)Z_PTR_P(zv);
  908. if (old_function->type == ZEND_USER_FUNCTION
  909. && old_function->op_array.last > 0) {
  910. zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
  911. ZSTR_VAL(function->common.function_name),
  912. ZSTR_VAL(old_function->op_array.filename),
  913. old_function->op_array.opcodes[0].lineno);
  914. } else {
  915. zend_error_noreturn(error_level, "Cannot redeclare %s()", ZSTR_VAL(function->common.function_name));
  916. }
  917. return FAILURE;
  918. } else {
  919. if (function->op_array.refcount) {
  920. (*function->op_array.refcount)++;
  921. }
  922. if (!(function->op_array.fn_flags & ZEND_ACC_IMMUTABLE)) {
  923. function->op_array.static_variables = NULL; /* NULL out the unbound function */
  924. }
  925. return SUCCESS;
  926. }
  927. }
  928. /* }}} */
  929. ZEND_API zend_class_entry *do_bind_class(const zend_op_array* op_array, const zend_op *opline, HashTable *class_table, zend_bool compile_time) /* {{{ */
  930. {
  931. zend_class_entry *ce;
  932. zval *lcname, *rtd_key, *zv;
  933. if (compile_time) {
  934. lcname = CT_CONSTANT_EX(op_array, opline->op1.constant);
  935. rtd_key = lcname + 1;
  936. } else {
  937. lcname = RT_CONSTANT(opline, opline->op1);
  938. rtd_key = lcname + 1;
  939. }
  940. zv = zend_hash_find_ex(class_table, Z_STR_P(rtd_key), 1);
  941. ZEND_ASSERT(zv);
  942. ce = (zend_class_entry*)Z_PTR_P(zv);
  943. ce->refcount++;
  944. if (zend_hash_add_ptr(class_table, Z_STR_P(lcname), ce) == NULL) {
  945. ce->refcount--;
  946. if (!compile_time) {
  947. /* If we're in compile time, in practice, it's quite possible
  948. * that we'll never reach this class declaration at runtime,
  949. * so we shut up about it. This allows the if (!defined('FOO')) { return; }
  950. * approach to work.
  951. */
  952. 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));
  953. }
  954. return NULL;
  955. } else {
  956. if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLEMENT_INTERFACES|ZEND_ACC_IMPLEMENT_TRAITS))) {
  957. zend_verify_abstract_class(ce);
  958. }
  959. return ce;
  960. }
  961. }
  962. /* }}} */
  963. ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op_array *op_array, const zend_op *opline, HashTable *class_table, zend_class_entry *parent_ce, zend_bool compile_time) /* {{{ */
  964. {
  965. zend_class_entry *ce;
  966. zval *lcname, *rtd_key, *zv;
  967. if (compile_time) {
  968. lcname = CT_CONSTANT_EX(op_array, opline->op1.constant);
  969. rtd_key = lcname + 1;
  970. } else {
  971. lcname = RT_CONSTANT(opline, opline->op1);
  972. rtd_key = lcname + 1;
  973. }
  974. zv = zend_hash_find_ex(class_table, Z_STR_P(rtd_key), 1);
  975. if (!zv) {
  976. if (!compile_time) {
  977. /* If we're in compile time, in practice, it's quite possible
  978. * that we'll never reach this class declaration at runtime,
  979. * so we shut up about it. This allows the if (!defined('FOO')) { return; }
  980. * approach to work.
  981. */
  982. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s, because the name is already in use", zend_get_object_type(Z_OBJCE_P(lcname)));
  983. }
  984. return NULL;
  985. }
  986. ce = (zend_class_entry*)Z_PTR_P(zv);
  987. if (zend_hash_exists(class_table, Z_STR_P(lcname))) {
  988. 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));
  989. }
  990. zend_do_inheritance(ce, parent_ce);
  991. ce->refcount++;
  992. /* Register the derived class */
  993. if (zend_hash_add_ptr(class_table, Z_STR_P(lcname), ce) == NULL) {
  994. 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));
  995. }
  996. return ce;
  997. }
  998. /* }}} */
  999. void zend_do_early_binding(void) /* {{{ */
  1000. {
  1001. zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
  1002. HashTable *table;
  1003. while (opline->opcode == ZEND_TICKS && opline > CG(active_op_array)->opcodes) {
  1004. opline--;
  1005. }
  1006. switch (opline->opcode) {
  1007. case ZEND_DECLARE_FUNCTION:
  1008. if (do_bind_function(CG(active_op_array), opline, CG(function_table), 1) == FAILURE) {
  1009. return;
  1010. }
  1011. table = CG(function_table);
  1012. break;
  1013. case ZEND_DECLARE_CLASS:
  1014. if (do_bind_class(CG(active_op_array), opline, CG(class_table), 1) == NULL) {
  1015. return;
  1016. }
  1017. table = CG(class_table);
  1018. break;
  1019. case ZEND_DECLARE_INHERITED_CLASS:
  1020. {
  1021. zval *parent_name;
  1022. zend_class_entry *ce;
  1023. parent_name = CT_CONSTANT(opline->op2);
  1024. if (((ce = zend_lookup_class_ex(Z_STR_P(parent_name), parent_name + 1, 0)) == NULL) ||
  1025. ((CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES) &&
  1026. (ce->type == ZEND_INTERNAL_CLASS))) {
  1027. if (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) {
  1028. CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
  1029. opline->opcode = ZEND_DECLARE_INHERITED_CLASS_DELAYED;
  1030. opline->result_type = IS_UNUSED;
  1031. opline->result.opline_num = -1;
  1032. }
  1033. return;
  1034. }
  1035. if (do_bind_inherited_class(CG(active_op_array), opline, CG(class_table), ce, 1) == NULL) {
  1036. return;
  1037. }
  1038. zend_del_literal(CG(active_op_array), opline->op2.constant+1);
  1039. zend_del_literal(CG(active_op_array), opline->op2.constant);
  1040. table = CG(class_table);
  1041. break;
  1042. }
  1043. case ZEND_VERIFY_ABSTRACT_CLASS:
  1044. case ZEND_ADD_INTERFACE:
  1045. case ZEND_ADD_TRAIT:
  1046. case ZEND_BIND_TRAITS:
  1047. /* We currently don't early-bind classes that implement interfaces */
  1048. /* Classes with traits are handled exactly the same, no early-bind here */
  1049. return;
  1050. default:
  1051. zend_error_noreturn(E_COMPILE_ERROR, "Invalid binding type");
  1052. return;
  1053. }
  1054. zend_hash_del(table, Z_STR_P(CT_CONSTANT(opline->op1)+1));
  1055. zend_del_literal(CG(active_op_array), opline->op1.constant+1);
  1056. zend_del_literal(CG(active_op_array), opline->op1.constant);
  1057. MAKE_NOP(opline);
  1058. }
  1059. /* }}} */
  1060. static void zend_mark_function_as_generator() /* {{{ */
  1061. {
  1062. if (!CG(active_op_array)->function_name) {
  1063. zend_error_noreturn(E_COMPILE_ERROR,
  1064. "The \"yield\" expression can only be used inside a function");
  1065. }
  1066. if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  1067. zend_arg_info return_info = CG(active_op_array)->arg_info[-1];
  1068. if (ZEND_TYPE_CODE(return_info.type) != IS_ITERABLE) {
  1069. const char *msg = "Generators may only declare a return type of Generator, Iterator, Traversable, or iterable, %s is not permitted";
  1070. if (!ZEND_TYPE_IS_CLASS(return_info.type)) {
  1071. zend_error_noreturn(E_COMPILE_ERROR, msg, zend_get_type_by_const(ZEND_TYPE_CODE(return_info.type)));
  1072. }
  1073. if (!zend_string_equals_literal_ci(ZEND_TYPE_NAME(return_info.type), "Traversable")
  1074. && !zend_string_equals_literal_ci(ZEND_TYPE_NAME(return_info.type), "Iterator")
  1075. && !zend_string_equals_literal_ci(ZEND_TYPE_NAME(return_info.type), "Generator")) {
  1076. zend_error_noreturn(E_COMPILE_ERROR, msg, ZSTR_VAL(ZEND_TYPE_NAME(return_info.type)));
  1077. }
  1078. }
  1079. }
  1080. CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
  1081. }
  1082. /* }}} */
  1083. ZEND_API uint32_t zend_build_delayed_early_binding_list(const zend_op_array *op_array) /* {{{ */
  1084. {
  1085. if (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) {
  1086. uint32_t first_early_binding_opline = (uint32_t)-1;
  1087. uint32_t *prev_opline_num = &first_early_binding_opline;
  1088. zend_op *opline = op_array->opcodes;
  1089. zend_op *end = opline + op_array->last;
  1090. while (opline < end) {
  1091. if (opline->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED) {
  1092. *prev_opline_num = opline - op_array->opcodes;
  1093. prev_opline_num = &opline->result.opline_num;
  1094. }
  1095. ++opline;
  1096. }
  1097. *prev_opline_num = -1;
  1098. return first_early_binding_opline;
  1099. }
  1100. return (uint32_t)-1;
  1101. }
  1102. /* }}} */
  1103. ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array, uint32_t first_early_binding_opline) /* {{{ */
  1104. {
  1105. if (first_early_binding_opline != (uint32_t)-1) {
  1106. zend_bool orig_in_compilation = CG(in_compilation);
  1107. uint32_t opline_num = first_early_binding_opline;
  1108. zend_class_entry *ce;
  1109. CG(in_compilation) = 1;
  1110. while (opline_num != (uint32_t)-1) {
  1111. const zend_op *opline = &op_array->opcodes[opline_num];
  1112. zval *parent_name = RT_CONSTANT(opline, opline->op2);
  1113. if ((ce = zend_lookup_class_ex(Z_STR_P(parent_name), parent_name + 1, 0)) != NULL) {
  1114. do_bind_inherited_class(op_array, &op_array->opcodes[opline_num], EG(class_table), ce, 0);
  1115. }
  1116. opline_num = op_array->opcodes[opline_num].result.opline_num;
  1117. }
  1118. CG(in_compilation) = orig_in_compilation;
  1119. }
  1120. }
  1121. /* }}} */
  1122. ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, int internal) /* {{{ */
  1123. {
  1124. size_t prop_name_length = 1 + src1_length + 1 + src2_length;
  1125. zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
  1126. ZSTR_VAL(prop_name)[0] = '\0';
  1127. memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
  1128. memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
  1129. return prop_name;
  1130. }
  1131. /* }}} */
  1132. static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
  1133. {
  1134. size_t len = 0;
  1135. while (*s++ && maxlen--) len++;
  1136. return len;
  1137. }
  1138. /* }}} */
  1139. ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
  1140. {
  1141. size_t class_name_len;
  1142. size_t anonclass_src_len;
  1143. *class_name = NULL;
  1144. if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
  1145. *prop_name = ZSTR_VAL(name);
  1146. if (prop_len) {
  1147. *prop_len = ZSTR_LEN(name);
  1148. }
  1149. return SUCCESS;
  1150. }
  1151. if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
  1152. zend_error(E_NOTICE, "Illegal member variable name");
  1153. *prop_name = ZSTR_VAL(name);
  1154. if (prop_len) {
  1155. *prop_len = ZSTR_LEN(name);
  1156. }
  1157. return FAILURE;
  1158. }
  1159. class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
  1160. if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
  1161. zend_error(E_NOTICE, "Corrupt member variable name");
  1162. *prop_name = ZSTR_VAL(name);
  1163. if (prop_len) {
  1164. *prop_len = ZSTR_LEN(name);
  1165. }
  1166. return FAILURE;
  1167. }
  1168. *class_name = ZSTR_VAL(name) + 1;
  1169. anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
  1170. if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
  1171. class_name_len += anonclass_src_len + 1;
  1172. }
  1173. *prop_name = ZSTR_VAL(name) + class_name_len + 2;
  1174. if (prop_len) {
  1175. *prop_len = ZSTR_LEN(name) - class_name_len - 2;
  1176. }
  1177. return SUCCESS;
  1178. }
  1179. /* }}} */
  1180. static zend_constant *zend_lookup_reserved_const(const char *name, size_t len) /* {{{ */
  1181. {
  1182. zend_constant *c = zend_hash_find_ptr_lc(EG(zend_constants), name, len);
  1183. if (c && !(ZEND_CONSTANT_FLAGS(c) & CONST_CS) && (ZEND_CONSTANT_FLAGS(c) & CONST_CT_SUBST)) {
  1184. return c;
  1185. }
  1186. return NULL;
  1187. }
  1188. /* }}} */
  1189. static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
  1190. {
  1191. zend_constant *c;
  1192. /* Substitute case-sensitive (or lowercase) constants */
  1193. c = zend_hash_find_ptr(EG(zend_constants), name);
  1194. if (c && (
  1195. ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
  1196. && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
  1197. && (!(ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) || !(CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE)))
  1198. || (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
  1199. )) {
  1200. ZVAL_COPY_OR_DUP(zv, &c->value);
  1201. return 1;
  1202. }
  1203. {
  1204. /* Substitute true, false and null (including unqualified usage in namespaces) */
  1205. const char *lookup_name = ZSTR_VAL(name);
  1206. size_t lookup_len = ZSTR_LEN(name);
  1207. if (!is_fully_qualified) {
  1208. zend_get_unqualified_name(name, &lookup_name, &lookup_len);
  1209. }
  1210. c = zend_lookup_reserved_const(lookup_name, lookup_len);
  1211. if (c) {
  1212. ZVAL_COPY_OR_DUP(zv, &c->value);
  1213. return 1;
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. /* }}} */
  1219. static inline zend_bool zend_is_scope_known() /* {{{ */
  1220. {
  1221. if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
  1222. /* Closures can be rebound to a different scope */
  1223. return 0;
  1224. }
  1225. if (!CG(active_class_entry)) {
  1226. /* The scope is known if we're in a free function (no scope), but not if we're in
  1227. * a file/eval (which inherits including/eval'ing scope). */
  1228. return CG(active_op_array)->function_name != NULL;
  1229. }
  1230. /* For traits self etc refers to the using class, not the trait itself */
  1231. return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
  1232. }
  1233. /* }}} */
  1234. static inline zend_bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
  1235. {
  1236. if (!CG(active_class_entry)) {
  1237. return 0;
  1238. }
  1239. if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
  1240. return 1;
  1241. }
  1242. return fetch_type == ZEND_FETCH_CLASS_DEFAULT
  1243. && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
  1244. }
  1245. /* }}} */
  1246. uint32_t zend_get_class_fetch_type(zend_string *name) /* {{{ */
  1247. {
  1248. if (zend_string_equals_literal_ci(name, "self")) {
  1249. return ZEND_FETCH_CLASS_SELF;
  1250. } else if (zend_string_equals_literal_ci(name, "parent")) {
  1251. return ZEND_FETCH_CLASS_PARENT;
  1252. } else if (zend_string_equals_literal_ci(name, "static")) {
  1253. return ZEND_FETCH_CLASS_STATIC;
  1254. } else {
  1255. return ZEND_FETCH_CLASS_DEFAULT;
  1256. }
  1257. }
  1258. /* }}} */
  1259. static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
  1260. {
  1261. /* Fully qualified names are always default refs */
  1262. if (name_ast->attr == ZEND_NAME_FQ) {
  1263. return ZEND_FETCH_CLASS_DEFAULT;
  1264. }
  1265. return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  1266. }
  1267. /* }}} */
  1268. static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
  1269. {
  1270. if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && !CG(active_class_entry) && zend_is_scope_known()) {
  1271. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
  1272. fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
  1273. fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
  1274. }
  1275. }
  1276. /* }}} */
  1277. static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast, zend_ast *name_ast, zend_bool constant) /* {{{ */
  1278. {
  1279. uint32_t fetch_type;
  1280. zval *class_name;
  1281. if (name_ast->kind != ZEND_AST_ZVAL) {
  1282. return 0;
  1283. }
  1284. if (!zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "class")) {
  1285. return 0;
  1286. }
  1287. if (class_ast->kind != ZEND_AST_ZVAL) {
  1288. zend_error_noreturn(E_COMPILE_ERROR,
  1289. "Dynamic class names are not allowed in compile-time ::class fetch");
  1290. }
  1291. class_name = zend_ast_get_zval(class_ast);
  1292. if (Z_TYPE_P(class_name) != IS_STRING) {
  1293. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  1294. }
  1295. fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
  1296. zend_ensure_valid_class_fetch_type(fetch_type);
  1297. switch (fetch_type) {
  1298. case ZEND_FETCH_CLASS_SELF:
  1299. if (CG(active_class_entry) && zend_is_scope_known()) {
  1300. ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
  1301. } else {
  1302. ZVAL_NULL(zv);
  1303. }
  1304. return 1;
  1305. case ZEND_FETCH_CLASS_STATIC:
  1306. case ZEND_FETCH_CLASS_PARENT:
  1307. if (constant) {
  1308. zend_error_noreturn(E_COMPILE_ERROR,
  1309. "%s::class cannot be used for compile-time class name resolution",
  1310. fetch_type == ZEND_FETCH_CLASS_STATIC ? "static" : "parent"
  1311. );
  1312. } else {
  1313. ZVAL_NULL(zv);
  1314. }
  1315. return 1;
  1316. case ZEND_FETCH_CLASS_DEFAULT:
  1317. ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
  1318. return 1;
  1319. EMPTY_SWITCH_DEFAULT_CASE()
  1320. }
  1321. }
  1322. /* }}} */
  1323. static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
  1324. {
  1325. uint32_t fetch_type = zend_get_class_fetch_type(class_name);
  1326. zend_class_constant *cc;
  1327. zval *c;
  1328. if (class_name_refers_to_active_ce(class_name, fetch_type)) {
  1329. cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
  1330. } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
  1331. zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), ZSTR_VAL(class_name), ZSTR_LEN(class_name));
  1332. if (ce) {
  1333. cc = zend_hash_find_ptr(&ce->constants_table, name);
  1334. } else {
  1335. return 0;
  1336. }
  1337. } else {
  1338. return 0;
  1339. }
  1340. if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
  1341. return 0;
  1342. }
  1343. if (!cc || !zend_verify_const_access(cc, CG(active_class_entry))) {
  1344. return 0;
  1345. }
  1346. c = &cc->value;
  1347. /* Substitute case-sensitive (or lowercase) persistent class constants */
  1348. if (Z_TYPE_P(c) < IS_OBJECT) {
  1349. ZVAL_COPY_OR_DUP(zv, c);
  1350. return 1;
  1351. }
  1352. return 0;
  1353. }
  1354. /* }}} */
  1355. static void zend_add_to_list(void *result, void *item) /* {{{ */
  1356. {
  1357. void** list = *(void**)result;
  1358. size_t n = 0;
  1359. if (list) {
  1360. while (list[n]) {
  1361. n++;
  1362. }
  1363. }
  1364. list = erealloc(list, sizeof(void*) * (n+2));
  1365. list[n] = item;
  1366. list[n+1] = NULL;
  1367. *(void**)result = list;
  1368. }
  1369. /* }}} */
  1370. void zend_do_extended_info(void) /* {{{ */
  1371. {
  1372. zend_op *opline;
  1373. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
  1374. return;
  1375. }
  1376. opline = get_next_op(CG(active_op_array));
  1377. opline->opcode = ZEND_EXT_STMT;
  1378. }
  1379. /* }}} */
  1380. void zend_do_extended_fcall_begin(void) /* {{{ */
  1381. {
  1382. zend_op *opline;
  1383. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
  1384. return;
  1385. }
  1386. opline = get_next_op(CG(active_op_array));
  1387. opline->opcode = ZEND_EXT_FCALL_BEGIN;
  1388. }
  1389. /* }}} */
  1390. void zend_do_extended_fcall_end(void) /* {{{ */
  1391. {
  1392. zend_op *opline;
  1393. if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
  1394. return;
  1395. }
  1396. opline = get_next_op(CG(active_op_array));
  1397. opline->opcode = ZEND_EXT_FCALL_END;
  1398. }
  1399. /* }}} */
  1400. zend_bool zend_is_auto_global_str(char *name, size_t len) /* {{{ */ {
  1401. zend_auto_global *auto_global;
  1402. if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
  1403. if (auto_global->armed) {
  1404. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1405. }
  1406. return 1;
  1407. }
  1408. return 0;
  1409. }
  1410. /* }}} */
  1411. zend_bool zend_is_auto_global(zend_string *name) /* {{{ */
  1412. {
  1413. zend_auto_global *auto_global;
  1414. if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
  1415. if (auto_global->armed) {
  1416. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1417. }
  1418. return 1;
  1419. }
  1420. return 0;
  1421. }
  1422. /* }}} */
  1423. int zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
  1424. {
  1425. zend_auto_global auto_global;
  1426. int retval;
  1427. auto_global.name = name;
  1428. auto_global.auto_global_callback = auto_global_callback;
  1429. auto_global.jit = jit;
  1430. retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
  1431. return retval;
  1432. }
  1433. /* }}} */
  1434. ZEND_API void zend_activate_auto_globals(void) /* {{{ */
  1435. {
  1436. zend_auto_global *auto_global;
  1437. ZEND_HASH_FOREACH_PTR(CG(auto_globals), auto_global) {
  1438. if (auto_global->jit) {
  1439. auto_global->armed = 1;
  1440. } else if (auto_global->auto_global_callback) {
  1441. auto_global->armed = auto_global->auto_global_callback(auto_global->name);
  1442. } else {
  1443. auto_global->armed = 0;
  1444. }
  1445. } ZEND_HASH_FOREACH_END();
  1446. }
  1447. /* }}} */
  1448. int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
  1449. {
  1450. zval zv;
  1451. int ret;
  1452. if (CG(increment_lineno)) {
  1453. CG(zend_lineno)++;
  1454. CG(increment_lineno) = 0;
  1455. }
  1456. ret = lex_scan(&zv, elem);
  1457. ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
  1458. return ret;
  1459. }
  1460. /* }}} */
  1461. ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
  1462. {
  1463. zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0;
  1464. ce->refcount = 1;
  1465. ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
  1466. if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
  1467. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  1468. }
  1469. ce->default_properties_table = NULL;
  1470. ce->default_static_members_table = NULL;
  1471. zend_hash_init_ex(&ce->properties_info, 8, NULL, (persistent_hashes ? zend_destroy_property_info_internal : NULL), persistent_hashes, 0);
  1472. zend_hash_init_ex(&ce->constants_table, 8, NULL, NULL, persistent_hashes, 0);
  1473. zend_hash_init_ex(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes, 0);
  1474. if (ce->type == ZEND_INTERNAL_CLASS) {
  1475. #ifdef ZTS
  1476. int n = zend_hash_num_elements(CG(class_table));
  1477. if (CG(static_members_table) && n >= CG(last_static_member)) {
  1478. /* Support for run-time declaration: dl() */
  1479. CG(last_static_member) = n+1;
  1480. CG(static_members_table) = realloc(CG(static_members_table), (n+1)*sizeof(zval*));
  1481. CG(static_members_table)[n] = NULL;
  1482. }
  1483. ce->static_members_table = (zval*)(zend_intptr_t)n;
  1484. #else
  1485. ce->static_members_table = NULL;
  1486. #endif
  1487. } else {
  1488. ce->static_members_table = ce->default_static_members_table;
  1489. ce->info.user.doc_comment = NULL;
  1490. }
  1491. ce->default_properties_count = 0;
  1492. ce->default_static_members_count = 0;
  1493. if (nullify_handlers) {
  1494. ce->constructor = NULL;
  1495. ce->destructor = NULL;
  1496. ce->clone = NULL;
  1497. ce->__get = NULL;
  1498. ce->__set = NULL;
  1499. ce->__unset = NULL;
  1500. ce->__isset = NULL;
  1501. ce->__call = NULL;
  1502. ce->__callstatic = NULL;
  1503. ce->__tostring = NULL;
  1504. ce->create_object = NULL;
  1505. ce->get_iterator = NULL;
  1506. ce->iterator_funcs_ptr = NULL;
  1507. ce->get_static_method = NULL;
  1508. ce->parent = NULL;
  1509. ce->num_interfaces = 0;
  1510. ce->interfaces = NULL;
  1511. ce->num_traits = 0;
  1512. ce->traits = NULL;
  1513. ce->trait_aliases = NULL;
  1514. ce->trait_precedences = NULL;
  1515. ce->serialize = NULL;
  1516. ce->unserialize = NULL;
  1517. ce->serialize_func = NULL;
  1518. ce->unserialize_func = NULL;
  1519. ce->__debugInfo = NULL;
  1520. if (ce->type == ZEND_INTERNAL_CLASS) {
  1521. ce->info.internal.module = NULL;
  1522. ce->info.internal.builtin_functions = NULL;
  1523. }
  1524. }
  1525. }
  1526. /* }}} */
  1527. ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
  1528. {
  1529. return op_array->vars[EX_VAR_TO_NUM(var)];
  1530. }
  1531. /* }}} */
  1532. zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
  1533. {
  1534. zval *left_zv = zend_ast_get_zval(left_ast);
  1535. zend_string *left = Z_STR_P(left_zv);
  1536. zend_string *right = zend_ast_get_str(right_ast);
  1537. zend_string *result;
  1538. size_t left_len = ZSTR_LEN(left);
  1539. size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
  1540. result = zend_string_extend(left, len, 0);
  1541. ZSTR_VAL(result)[left_len] = '\\';
  1542. memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
  1543. ZSTR_VAL(result)[len] = '\0';
  1544. zend_string_release_ex(right, 0);
  1545. ZVAL_STR(left_zv, result);
  1546. return left_ast;
  1547. }
  1548. /* }}} */
  1549. zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
  1550. {
  1551. zval *zv = zend_ast_get_zval(ast);
  1552. if (Z_TYPE_P(zv) == IS_LONG) {
  1553. if (Z_LVAL_P(zv) == 0) {
  1554. ZVAL_NEW_STR(zv, zend_string_init("-0", sizeof("-0")-1, 0));
  1555. } else {
  1556. ZEND_ASSERT(Z_LVAL_P(zv) > 0);
  1557. Z_LVAL_P(zv) *= -1;
  1558. }
  1559. } else if (Z_TYPE_P(zv) == IS_STRING) {
  1560. size_t orig_len = Z_STRLEN_P(zv);
  1561. Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
  1562. memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
  1563. Z_STRVAL_P(zv)[0] = '-';
  1564. } else {
  1565. ZEND_ASSERT(0);
  1566. }
  1567. return ast;
  1568. }
  1569. /* }}} */
  1570. void zend_verify_namespace(void) /* {{{ */
  1571. {
  1572. if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
  1573. zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
  1574. }
  1575. }
  1576. /* }}} */
  1577. /* {{{ zend_dirname
  1578. Returns directory name component of path */
  1579. ZEND_API size_t zend_dirname(char *path, size_t len)
  1580. {
  1581. register char *end = path + len - 1;
  1582. unsigned int len_adjust = 0;
  1583. #ifdef ZEND_WIN32
  1584. /* Note that on Win32 CWD is per drive (heritage from CP/M).
  1585. * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
  1586. */
  1587. if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
  1588. /* Skip over the drive spec (if any) so as not to change */
  1589. path += 2;
  1590. len_adjust += 2;
  1591. if (2 == len) {
  1592. /* Return "c:" on Win32 for dirname("c:").
  1593. * It would be more consistent to return "c:."
  1594. * but that would require making the string *longer*.
  1595. */
  1596. return len;
  1597. }
  1598. }
  1599. #endif
  1600. if (len == 0) {
  1601. /* Illegal use of this function */
  1602. return 0;
  1603. }
  1604. /* Strip trailing slashes */
  1605. while (end >= path && IS_SLASH_P(end)) {
  1606. end--;
  1607. }
  1608. if (end < path) {
  1609. /* The path only contained slashes */
  1610. path[0] = DEFAULT_SLASH;
  1611. path[1] = '\0';
  1612. return 1 + len_adjust;
  1613. }
  1614. /* Strip filename */
  1615. while (end >= path && !IS_SLASH_P(end)) {
  1616. end--;
  1617. }
  1618. if (end < path) {
  1619. /* No slash found, therefore return '.' */
  1620. path[0] = '.';
  1621. path[1] = '\0';
  1622. return 1 + len_adjust;
  1623. }
  1624. /* Strip slashes which came before the file name */
  1625. while (end >= path && IS_SLASH_P(end)) {
  1626. end--;
  1627. }
  1628. if (end < path) {
  1629. path[0] = DEFAULT_SLASH;
  1630. path[1] = '\0';
  1631. return 1 + len_adjust;
  1632. }
  1633. *(end+1) = '\0';
  1634. return (size_t)(end + 1 - path) + len_adjust;
  1635. }
  1636. /* }}} */
  1637. static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
  1638. {
  1639. zend_uchar factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
  1640. switch (type) {
  1641. case BP_VAR_R:
  1642. opline->result_type = IS_TMP_VAR;
  1643. result->op_type = IS_TMP_VAR;
  1644. return;
  1645. case BP_VAR_W:
  1646. opline->opcode += 1 * factor;
  1647. return;
  1648. case BP_VAR_RW:
  1649. opline->opcode += 2 * factor;
  1650. return;
  1651. case BP_VAR_IS:
  1652. opline->result_type = IS_TMP_VAR;
  1653. result->op_type = IS_TMP_VAR;
  1654. opline->opcode += 3 * factor;
  1655. return;
  1656. case BP_VAR_FUNC_ARG:
  1657. opline->opcode += 4 * factor;
  1658. return;
  1659. case BP_VAR_UNSET:
  1660. opline->opcode += 5 * factor;
  1661. return;
  1662. EMPTY_SWITCH_DEFAULT_CASE()
  1663. }
  1664. }
  1665. /* }}} */
  1666. static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
  1667. {
  1668. opline->result_type = IS_VAR;
  1669. opline->result.var = get_temporary_variable(CG(active_op_array));
  1670. GET_NODE(result, opline->result);
  1671. }
  1672. /* }}} */
  1673. static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
  1674. {
  1675. opline->result_type = IS_TMP_VAR;
  1676. opline->result.var = get_temporary_variable(CG(active_op_array));
  1677. GET_NODE(result, opline->result);
  1678. }
  1679. /* }}} */
  1680. static void zend_find_live_range(zend_op *opline, zend_uchar type, uint32_t var) /* {{{ */
  1681. {
  1682. zend_op *def = opline;
  1683. while (def != CG(active_op_array)->opcodes) {
  1684. def--;
  1685. if (def->result_type == type && def->result.var == var) {
  1686. if (def->opcode == ZEND_ADD_ARRAY_ELEMENT ||
  1687. def->opcode == ZEND_ROPE_ADD) {
  1688. /* not a real definition */
  1689. continue;
  1690. } else if (def->opcode == ZEND_JMPZ_EX ||
  1691. def->opcode == ZEND_JMPNZ_EX ||
  1692. def->opcode == ZEND_BOOL ||
  1693. def->opcode == ZEND_BOOL_NOT) {
  1694. /* result IS_BOOL, it does't have to be destroyed */
  1695. break;
  1696. } else if (def->opcode == ZEND_DECLARE_CLASS ||
  1697. def->opcode == ZEND_DECLARE_INHERITED_CLASS ||
  1698. def->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED ||
  1699. def->opcode == ZEND_DECLARE_ANON_CLASS ||
  1700. def->opcode == ZEND_DECLARE_ANON_INHERITED_CLASS) {
  1701. /* classes don't have to be destroyed */
  1702. break;
  1703. } else if (def->opcode == ZEND_FAST_CALL) {
  1704. /* fast_calls don't have to be destroyed */
  1705. break;
  1706. } else if (def->opcode == ZEND_NEW) {
  1707. /* Objects created via ZEND_NEW are only fully initialized
  1708. * after the DO_FCALL (constructor call) */
  1709. int level = 0;
  1710. while (def + 1 != opline) {
  1711. def++;
  1712. if (def->opcode == ZEND_DO_FCALL) {
  1713. if (level == 0) {
  1714. break;
  1715. }
  1716. level--;
  1717. } else {
  1718. switch(def->opcode) {
  1719. case ZEND_INIT_FCALL:
  1720. case ZEND_INIT_FCALL_BY_NAME:
  1721. case ZEND_INIT_NS_FCALL_BY_NAME:
  1722. case ZEND_INIT_DYNAMIC_CALL:
  1723. case ZEND_INIT_USER_CALL:
  1724. case ZEND_INIT_METHOD_CALL:
  1725. case ZEND_INIT_STATIC_METHOD_CALL:
  1726. case ZEND_NEW:
  1727. level++;
  1728. break;
  1729. case ZEND_DO_ICALL:
  1730. case ZEND_DO_UCALL:
  1731. case ZEND_DO_FCALL_BY_NAME:
  1732. level--;
  1733. break;
  1734. }
  1735. }
  1736. }
  1737. if (def + 1 == opline) {
  1738. break;
  1739. }
  1740. }
  1741. zend_end_live_range(CG(active_op_array),
  1742. zend_start_live_range(CG(active_op_array),
  1743. def + 1 - CG(active_op_array)->opcodes),
  1744. opline - CG(active_op_array)->opcodes,
  1745. ZEND_LIVE_TMPVAR, var);
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. /* }}} */
  1751. static zend_always_inline int zend_is_def_range(zend_op *opline, zend_uchar type, uint32_t var) /* {{{ */
  1752. {
  1753. while (1) {
  1754. if (opline->result_type == type && opline->result.var == var) {
  1755. return opline->opcode != ZEND_ADD_ARRAY_ELEMENT &&
  1756. opline->opcode != ZEND_ROPE_ADD;
  1757. } else if (opline->opcode == ZEND_OP_DATA) {
  1758. return (opline-1)->result_type == type &&
  1759. (opline-1)->result.var == var;
  1760. } else if (opline->opcode == ZEND_END_SILENCE ||
  1761. opline->opcode == ZEND_NOP ||
  1762. opline->opcode == ZEND_EXT_NOP ||
  1763. opline->opcode == ZEND_EXT_STMT ||
  1764. opline->opcode == ZEND_EXT_FCALL_BEGIN ||
  1765. opline->opcode == ZEND_EXT_FCALL_END ||
  1766. opline->opcode == ZEND_TICKS) {
  1767. opline--;
  1768. } else {
  1769. return 0;
  1770. }
  1771. }
  1772. }
  1773. /* }}} */
  1774. static void zend_check_live_ranges(zend_op *opline) /* {{{ */
  1775. {
  1776. if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) &&
  1777. !zend_is_def_range(opline - 1, opline->op1_type, opline->op1.var)) {
  1778. if (opline->opcode == ZEND_OP_DATA) {
  1779. if (!zend_is_def_range(opline - 2, opline->op1_type, opline->op1.var)) {
  1780. zend_find_live_range(opline - 1, opline->op1_type, opline->op1.var);
  1781. }
  1782. } else if (opline->opcode == ZEND_INIT_STATIC_METHOD_CALL ||
  1783. opline->opcode == ZEND_NEW ||
  1784. opline->opcode == ZEND_FETCH_CLASS_CONSTANT ||
  1785. opline->opcode == ZEND_ADD_INTERFACE ||
  1786. opline->opcode == ZEND_ADD_TRAIT ||
  1787. opline->opcode == ZEND_BIND_TRAITS ||
  1788. opline->opcode == ZEND_VERIFY_ABSTRACT_CLASS) {
  1789. /* classes don't have to be destroyed */
  1790. } else if (opline->opcode == ZEND_FAST_RET) {
  1791. /* fast_calls don't have to be destroyed */
  1792. } else if (opline->opcode == ZEND_CASE ||
  1793. opline->opcode == ZEND_SWITCH_LONG ||
  1794. opline->opcode == ZEND_SWITCH_STRING ||
  1795. opline->opcode == ZEND_FE_FETCH_R ||
  1796. opline->opcode == ZEND_FE_FETCH_RW ||
  1797. opline->opcode == ZEND_FE_FREE ||
  1798. opline->opcode == ZEND_ROPE_ADD ||
  1799. opline->opcode == ZEND_ROPE_END ||
  1800. opline->opcode == ZEND_END_SILENCE ||
  1801. opline->opcode == ZEND_FETCH_LIST_R ||
  1802. opline->opcode == ZEND_FETCH_LIST_W ||
  1803. opline->opcode == ZEND_VERIFY_RETURN_TYPE ||
  1804. opline->opcode == ZEND_BIND_LEXICAL) {
  1805. /* these opcodes are handled separately */
  1806. } else {
  1807. zend_find_live_range(opline, opline->op1_type, opline->op1.var);
  1808. }
  1809. }
  1810. if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) &&
  1811. !zend_is_def_range(opline - 1, opline->op2_type, opline->op2.var)) {
  1812. if (opline->opcode == ZEND_OP_DATA) {
  1813. if (!zend_is_def_range(opline - 2, opline->op2_type, opline->op2.var)) {
  1814. zend_find_live_range(opline-1, opline->op2_type, opline->op2.var);
  1815. }
  1816. } else if (opline->opcode == ZEND_FETCH_STATIC_PROP_R ||
  1817. opline->opcode == ZEND_FETCH_STATIC_PROP_W ||
  1818. opline->opcode == ZEND_FETCH_STATIC_PROP_RW ||
  1819. opline->opcode == ZEND_FETCH_STATIC_PROP_IS ||
  1820. opline->opcode == ZEND_FETCH_STATIC_PROP_FUNC_ARG ||
  1821. opline->opcode == ZEND_FETCH_STATIC_PROP_UNSET ||
  1822. opline->opcode == ZEND_UNSET_STATIC_PROP ||
  1823. opline->opcode == ZEND_ISSET_ISEMPTY_STATIC_PROP ||
  1824. opline->opcode == ZEND_INSTANCEOF) {
  1825. /* classes don't have to be destroyed */
  1826. } else {
  1827. zend_find_live_range(opline, opline->op2_type, opline->op2.var);
  1828. }
  1829. }
  1830. }
  1831. /* }}} */
  1832. static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1833. {
  1834. zend_op *opline = get_next_op(CG(active_op_array));
  1835. opline->opcode = opcode;
  1836. if (op1 != NULL) {
  1837. SET_NODE(opline->op1, op1);
  1838. }
  1839. if (op2 != NULL) {
  1840. SET_NODE(opline->op2, op2);
  1841. }
  1842. zend_check_live_ranges(opline);
  1843. if (result) {
  1844. zend_make_var_result(result, opline);
  1845. }
  1846. return opline;
  1847. }
  1848. /* }}} */
  1849. static zend_op *zend_emit_op_tmp(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1850. {
  1851. zend_op *opline = get_next_op(CG(active_op_array));
  1852. opline->opcode = opcode;
  1853. if (op1 != NULL) {
  1854. SET_NODE(opline->op1, op1);
  1855. }
  1856. if (op2 != NULL) {
  1857. SET_NODE(opline->op2, op2);
  1858. }
  1859. zend_check_live_ranges(opline);
  1860. if (result) {
  1861. zend_make_tmp_result(result, opline);
  1862. }
  1863. return opline;
  1864. }
  1865. /* }}} */
  1866. static void zend_emit_tick(void) /* {{{ */
  1867. {
  1868. zend_op *opline;
  1869. /* This prevents a double TICK generated by the parser statement of "declare()" */
  1870. if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
  1871. return;
  1872. }
  1873. opline = get_next_op(CG(active_op_array));
  1874. opline->opcode = ZEND_TICKS;
  1875. opline->extended_value = FC(declarables).ticks;
  1876. }
  1877. /* }}} */
  1878. static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
  1879. {
  1880. return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
  1881. }
  1882. /* }}} */
  1883. static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
  1884. {
  1885. uint32_t opnum = get_next_op_number(CG(active_op_array));
  1886. zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
  1887. opline->op1.opline_num = opnum_target;
  1888. return opnum;
  1889. }
  1890. /* }}} */
  1891. ZEND_API int zend_is_smart_branch(zend_op *opline) /* {{{ */
  1892. {
  1893. switch (opline->opcode) {
  1894. case ZEND_IS_IDENTICAL:
  1895. case ZEND_IS_NOT_IDENTICAL:
  1896. case ZEND_IS_EQUAL:
  1897. case ZEND_IS_NOT_EQUAL:
  1898. case ZEND_IS_SMALLER:
  1899. case ZEND_IS_SMALLER_OR_EQUAL:
  1900. case ZEND_CASE:
  1901. case ZEND_ISSET_ISEMPTY_CV:
  1902. case ZEND_ISSET_ISEMPTY_VAR:
  1903. case ZEND_ISSET_ISEMPTY_DIM_OBJ:
  1904. case ZEND_ISSET_ISEMPTY_PROP_OBJ:
  1905. case ZEND_ISSET_ISEMPTY_STATIC_PROP:
  1906. case ZEND_INSTANCEOF:
  1907. case ZEND_TYPE_CHECK:
  1908. case ZEND_DEFINED:
  1909. case ZEND_IN_ARRAY:
  1910. return 1;
  1911. default:
  1912. return 0;
  1913. }
  1914. }
  1915. /* }}} */
  1916. static inline uint32_t zend_emit_cond_jump(zend_uchar opcode, znode *cond, uint32_t opnum_target) /* {{{ */
  1917. {
  1918. uint32_t opnum = get_next_op_number(CG(active_op_array));
  1919. zend_op *opline;
  1920. if ((cond->op_type & (IS_CV|IS_CONST))
  1921. && opnum > 0
  1922. && zend_is_smart_branch(CG(active_op_array)->opcodes + opnum - 1)) {
  1923. /* emit extra NOP to avoid incorrect SMART_BRANCH in very rare cases */
  1924. zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
  1925. opnum = get_next_op_number(CG(active_op_array));
  1926. }
  1927. opline = zend_emit_op(NULL, opcode, cond, NULL);
  1928. opline->op2.opline_num = opnum_target;
  1929. return opnum;
  1930. }
  1931. /* }}} */
  1932. static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
  1933. {
  1934. zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
  1935. switch (opline->opcode) {
  1936. case ZEND_JMP:
  1937. opline->op1.opline_num = opnum_target;
  1938. break;
  1939. case ZEND_JMPZ:
  1940. case ZEND_JMPNZ:
  1941. case ZEND_JMPZ_EX:
  1942. case ZEND_JMPNZ_EX:
  1943. case ZEND_JMP_SET:
  1944. opline->op2.opline_num = opnum_target;
  1945. break;
  1946. EMPTY_SWITCH_DEFAULT_CASE()
  1947. }
  1948. }
  1949. /* }}} */
  1950. static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
  1951. {
  1952. zend_update_jump_target(opnum_jump, get_next_op_number(CG(active_op_array)));
  1953. }
  1954. /* }}} */
  1955. static inline zend_op *zend_delayed_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
  1956. {
  1957. zend_op tmp_opline;
  1958. init_op(&tmp_opline);
  1959. tmp_opline.opcode = opcode;
  1960. if (op1 != NULL) {
  1961. SET_NODE(tmp_opline.op1, op1);
  1962. }
  1963. if (op2 != NULL) {
  1964. SET_NODE(tmp_opline.op2, op2);
  1965. }
  1966. if (result) {
  1967. zend_make_var_result(result, &tmp_opline);
  1968. }
  1969. zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
  1970. return zend_stack_top(&CG(delayed_oplines_stack));
  1971. }
  1972. /* }}} */
  1973. static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
  1974. {
  1975. return zend_stack_count(&CG(delayed_oplines_stack));
  1976. }
  1977. /* }}} */
  1978. static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
  1979. {
  1980. zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
  1981. uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
  1982. ZEND_ASSERT(count >= offset);
  1983. for (i = offset; i < count; ++i) {
  1984. opline = get_next_op(CG(active_op_array));
  1985. memcpy(opline, &oplines[i], sizeof(zend_op));
  1986. zend_check_live_ranges(opline);
  1987. }
  1988. CG(delayed_oplines_stack).top = offset;
  1989. return opline;
  1990. }
  1991. /* }}} */
  1992. static void zend_emit_return_type_check(
  1993. znode *expr, zend_arg_info *return_info, zend_bool implicit) /* {{{ */
  1994. {
  1995. if (ZEND_TYPE_IS_SET(return_info->type)) {
  1996. zend_op *opline;
  1997. /* `return ...;` is illegal in a void function (but `return;` isn't) */
  1998. if (ZEND_TYPE_CODE(return_info->type) == IS_VOID) {
  1999. if (expr) {
  2000. if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
  2001. zend_error_noreturn(E_COMPILE_ERROR,
  2002. "A void function must not return a value "
  2003. "(did you mean \"return;\" instead of \"return null;\"?)");
  2004. } else {
  2005. zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
  2006. }
  2007. }
  2008. /* we don't need run-time check */
  2009. return;
  2010. }
  2011. if (!expr && !implicit) {
  2012. if (ZEND_TYPE_ALLOW_NULL(return_info->type)) {
  2013. zend_error_noreturn(E_COMPILE_ERROR,
  2014. "A function with return type must return a value "
  2015. "(did you mean \"return null;\" instead of \"return;\"?)");
  2016. } else {
  2017. zend_error_noreturn(E_COMPILE_ERROR,
  2018. "A function with return type must return a value");
  2019. }
  2020. }
  2021. if (expr && expr->op_type == IS_CONST) {
  2022. if ((ZEND_TYPE_CODE(return_info->type) == Z_TYPE(expr->u.constant))
  2023. ||((ZEND_TYPE_CODE(return_info->type) == _IS_BOOL)
  2024. && (Z_TYPE(expr->u.constant) == IS_FALSE
  2025. || Z_TYPE(expr->u.constant) == IS_TRUE))
  2026. || (ZEND_TYPE_ALLOW_NULL(return_info->type)
  2027. && Z_TYPE(expr->u.constant) == IS_NULL)) {
  2028. /* we don't need run-time check */
  2029. return;
  2030. }
  2031. }
  2032. opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
  2033. if (expr && expr->op_type == IS_CONST) {
  2034. opline->result_type = expr->op_type = IS_TMP_VAR;
  2035. opline->result.var = expr->u.op.var = get_temporary_variable(CG(active_op_array));
  2036. }
  2037. if (ZEND_TYPE_IS_CLASS(return_info->type)) {
  2038. opline->op2.num = CG(active_op_array)->cache_size;
  2039. CG(active_op_array)->cache_size += sizeof(void*);
  2040. } else {
  2041. opline->op2.num = -1;
  2042. }
  2043. }
  2044. }
  2045. /* }}} */
  2046. void zend_emit_final_return(int return_one) /* {{{ */
  2047. {
  2048. znode zn;
  2049. zend_op *ret;
  2050. zend_bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  2051. if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE
  2052. && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
  2053. zend_emit_return_type_check(NULL, CG(active_op_array)->arg_info - 1, 1);
  2054. }
  2055. zn.op_type = IS_CONST;
  2056. if (return_one) {
  2057. ZVAL_LONG(&zn.u.constant, 1);
  2058. } else {
  2059. ZVAL_NULL(&zn.u.constant);
  2060. }
  2061. ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
  2062. ret->extended_value = -1;
  2063. }
  2064. /* }}} */
  2065. static inline zend_bool zend_is_variable(zend_ast *ast) /* {{{ */
  2066. {
  2067. return ast->kind == ZEND_AST_VAR || ast->kind == ZEND_AST_DIM
  2068. || ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_STATIC_PROP
  2069. || ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_METHOD_CALL
  2070. || ast->kind == ZEND_AST_STATIC_CALL;
  2071. }
  2072. /* }}} */
  2073. static inline zend_bool zend_is_call(zend_ast *ast) /* {{{ */
  2074. {
  2075. return ast->kind == ZEND_AST_CALL
  2076. || ast->kind == ZEND_AST_METHOD_CALL
  2077. || ast->kind == ZEND_AST_STATIC_CALL;
  2078. }
  2079. /* }}} */
  2080. static inline zend_bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
  2081. {
  2082. return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
  2083. || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_DECL
  2084. || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
  2085. }
  2086. /* }}} */
  2087. static inline zend_bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
  2088. {
  2089. while (ast->kind == ZEND_AST_DIM || ast->kind == ZEND_AST_PROP) {
  2090. ast = ast->child[0];
  2091. }
  2092. return zend_is_variable(ast);
  2093. }
  2094. /* }}} */
  2095. static inline zend_bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
  2096. {
  2097. if (name_ast->kind != ZEND_AST_ZVAL) {
  2098. return 0;
  2099. }
  2100. return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
  2101. }
  2102. /* }}} */
  2103. static inline void zend_handle_numeric_op(znode *node) /* {{{ */
  2104. {
  2105. if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
  2106. zend_ulong index;
  2107. if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
  2108. zval_ptr_dtor(&node->u.constant);
  2109. ZVAL_LONG(&node->u.constant, index);
  2110. }
  2111. }
  2112. }
  2113. /* }}} */
  2114. static inline void zend_handle_numeric_dim(zend_op *opline, znode *dim_node) /* {{{ */
  2115. {
  2116. if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
  2117. zend_ulong index;
  2118. if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
  2119. /* For numeric indexs we also keep the original value to use by ArrayAccess
  2120. * See bug #63217
  2121. */
  2122. int c = zend_add_literal(CG(active_op_array), &dim_node->u.constant);
  2123. ZEND_ASSERT(opline->op2.constant + 1 == c);
  2124. ZVAL_LONG(CT_CONSTANT(opline->op2), index);
  2125. Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
  2126. return;
  2127. }
  2128. }
  2129. }
  2130. /* }}} */
  2131. static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
  2132. {
  2133. if (class_node->op_type == IS_CONST) {
  2134. opline->op1_type = IS_CONST;
  2135. opline->op1.constant = zend_add_class_name_literal(
  2136. CG(active_op_array), Z_STR(class_node->u.constant));
  2137. } else {
  2138. SET_NODE(opline->op1, class_node);
  2139. }
  2140. }
  2141. /* }}} */
  2142. static void zend_compile_class_ref_ex(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
  2143. {
  2144. uint32_t fetch_type;
  2145. if (name_ast->kind != ZEND_AST_ZVAL) {
  2146. znode name_node;
  2147. zend_compile_expr(&name_node, name_ast);
  2148. if (name_node.op_type == IS_CONST) {
  2149. zend_string *name;
  2150. if (Z_TYPE(name_node.u.constant) != IS_STRING) {
  2151. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  2152. }
  2153. name = Z_STR(name_node.u.constant);
  2154. fetch_type = zend_get_class_fetch_type(name);
  2155. if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
  2156. result->op_type = IS_CONST;
  2157. ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
  2158. } else {
  2159. zend_ensure_valid_class_fetch_type(fetch_type);
  2160. result->op_type = IS_UNUSED;
  2161. result->u.op.num = fetch_type | fetch_flags;
  2162. }
  2163. zend_string_release_ex(name, 0);
  2164. } else {
  2165. zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
  2166. opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
  2167. }
  2168. return;
  2169. }
  2170. /* Fully qualified names are always default refs */
  2171. if (name_ast->attr == ZEND_NAME_FQ) {
  2172. result->op_type = IS_CONST;
  2173. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  2174. return;
  2175. }
  2176. fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  2177. if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
  2178. result->op_type = IS_CONST;
  2179. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  2180. } else {
  2181. zend_ensure_valid_class_fetch_type(fetch_type);
  2182. result->op_type = IS_UNUSED;
  2183. result->u.op.num = fetch_type | fetch_flags;
  2184. }
  2185. }
  2186. /* }}} */
  2187. static int zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
  2188. {
  2189. zend_ast *name_ast = ast->child[0];
  2190. if (name_ast->kind == ZEND_AST_ZVAL) {
  2191. zval *zv = zend_ast_get_zval(name_ast);
  2192. zend_string *name;
  2193. if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
  2194. name = zval_make_interned_string(zv);
  2195. } else {
  2196. name = zend_new_interned_string(zval_get_string_func(zv));
  2197. }
  2198. if (zend_is_auto_global(name)) {
  2199. return FAILURE;
  2200. }
  2201. result->op_type = IS_CV;
  2202. result->u.op.var = lookup_cv(CG(active_op_array), name);
  2203. if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
  2204. zend_string_release_ex(name, 0);
  2205. }
  2206. return SUCCESS;
  2207. }
  2208. return FAILURE;
  2209. }
  2210. /* }}} */
  2211. static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
  2212. {
  2213. zend_ast *name_ast = ast->child[0];
  2214. znode name_node;
  2215. zend_op *opline;
  2216. zend_compile_expr(&name_node, name_ast);
  2217. if (name_node.op_type == IS_CONST) {
  2218. convert_to_string(&name_node.u.constant);
  2219. }
  2220. if (delayed) {
  2221. opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
  2222. } else {
  2223. opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
  2224. }
  2225. if (name_node.op_type == IS_CONST &&
  2226. zend_is_auto_global(Z_STR(name_node.u.constant))) {
  2227. opline->extended_value = ZEND_FETCH_GLOBAL;
  2228. } else {
  2229. opline->extended_value = ZEND_FETCH_LOCAL;
  2230. }
  2231. zend_adjust_for_fetch_type(opline, result, type);
  2232. return opline;
  2233. }
  2234. /* }}} */
  2235. static zend_bool is_this_fetch(zend_ast *ast) /* {{{ */
  2236. {
  2237. if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
  2238. zval *name = zend_ast_get_zval(ast->child[0]);
  2239. return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "this");
  2240. }
  2241. return 0;
  2242. }
  2243. /* }}} */
  2244. static void zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
  2245. {
  2246. if (is_this_fetch(ast)) {
  2247. zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
  2248. if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
  2249. opline->result_type = IS_TMP_VAR;
  2250. result->op_type = IS_TMP_VAR;
  2251. }
  2252. } else if (zend_try_compile_cv(result, ast) == FAILURE) {
  2253. zend_compile_simple_var_no_cv(result, ast, type, delayed);
  2254. }
  2255. }
  2256. /* }}} */
  2257. static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
  2258. {
  2259. if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
  2260. if (node->op_type == IS_VAR) {
  2261. zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
  2262. opline->result_type = IS_VAR;
  2263. opline->result.var = opline->op1.var;
  2264. } else {
  2265. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
  2266. }
  2267. }
  2268. }
  2269. /* }}} */
  2270. void zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type);
  2271. void zend_compile_assign(znode *result, zend_ast *ast);
  2272. static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
  2273. {
  2274. znode dummy_node;
  2275. zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
  2276. zend_ast_create_znode(value_node));
  2277. zend_compile_assign(&dummy_node, assign_ast);
  2278. zend_do_free(&dummy_node);
  2279. }
  2280. /* }}} */
  2281. static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2282. {
  2283. zend_ast *var_ast = ast->child[0];
  2284. zend_ast *dim_ast = ast->child[1];
  2285. zend_op *opline;
  2286. znode var_node, dim_node;
  2287. zend_delayed_compile_var(&var_node, var_ast, type);
  2288. zend_separate_if_call_and_write(&var_node, var_ast, type);
  2289. if (dim_ast == NULL) {
  2290. if (type == BP_VAR_R || type == BP_VAR_IS) {
  2291. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  2292. }
  2293. if (type == BP_VAR_UNSET) {
  2294. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
  2295. }
  2296. dim_node.op_type = IS_UNUSED;
  2297. } else {
  2298. zend_compile_expr(&dim_node, dim_ast);
  2299. }
  2300. opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
  2301. zend_adjust_for_fetch_type(opline, result, type);
  2302. if (dim_node.op_type == IS_CONST) {
  2303. zend_handle_numeric_dim(opline, &dim_node);
  2304. }
  2305. return opline;
  2306. }
  2307. /* }}} */
  2308. static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2309. {
  2310. uint32_t offset = zend_delayed_compile_begin();
  2311. zend_delayed_compile_dim(result, ast, type);
  2312. return zend_delayed_compile_end(offset);
  2313. }
  2314. /* }}} */
  2315. static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2316. {
  2317. zend_ast *obj_ast = ast->child[0];
  2318. zend_ast *prop_ast = ast->child[1];
  2319. znode obj_node, prop_node;
  2320. zend_op *opline;
  2321. if (is_this_fetch(obj_ast)) {
  2322. obj_node.op_type = IS_UNUSED;
  2323. } else {
  2324. zend_delayed_compile_var(&obj_node, obj_ast, type);
  2325. zend_separate_if_call_and_write(&obj_node, obj_ast, type);
  2326. }
  2327. zend_compile_expr(&prop_node, prop_ast);
  2328. opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
  2329. if (opline->op2_type == IS_CONST) {
  2330. convert_to_string(CT_CONSTANT(opline->op2));
  2331. opline->extended_value = zend_alloc_polymorphic_cache_slot();
  2332. }
  2333. zend_adjust_for_fetch_type(opline, result, type);
  2334. return opline;
  2335. }
  2336. /* }}} */
  2337. static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  2338. {
  2339. uint32_t offset = zend_delayed_compile_begin();
  2340. zend_delayed_compile_prop(result, ast, type);
  2341. return zend_delayed_compile_end(offset);
  2342. }
  2343. /* }}} */
  2344. zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
  2345. {
  2346. zend_ast *class_ast = ast->child[0];
  2347. zend_ast *prop_ast = ast->child[1];
  2348. znode class_node, prop_node;
  2349. zend_op *opline;
  2350. zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  2351. zend_compile_expr(&prop_node, prop_ast);
  2352. if (delayed) {
  2353. opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
  2354. } else {
  2355. opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
  2356. }
  2357. if (opline->op1_type == IS_CONST) {
  2358. convert_to_string(CT_CONSTANT(opline->op1));
  2359. opline->extended_value = zend_alloc_polymorphic_cache_slot();
  2360. }
  2361. if (class_node.op_type == IS_CONST) {
  2362. opline->op2_type = IS_CONST;
  2363. opline->op2.constant = zend_add_class_name_literal(
  2364. CG(active_op_array), Z_STR(class_node.u.constant));
  2365. if (opline->op1_type != IS_CONST) {
  2366. opline->extended_value = zend_alloc_cache_slot();
  2367. }
  2368. } else {
  2369. SET_NODE(opline->op2, &class_node);
  2370. }
  2371. zend_adjust_for_fetch_type(opline, result, type);
  2372. return opline;
  2373. }
  2374. /* }}} */
  2375. static void zend_verify_list_assign_target(zend_ast *var_ast, zend_bool old_style) /* {{{ */ {
  2376. if (var_ast->kind == ZEND_AST_ARRAY) {
  2377. if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
  2378. zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
  2379. }
  2380. if (old_style != var_ast->attr) {
  2381. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
  2382. }
  2383. } else if (!zend_can_write_to_variable(var_ast)) {
  2384. zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
  2385. }
  2386. }
  2387. /* }}} */
  2388. static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node);
  2389. /* Propagate refs used on leaf elements to the surrounding list() structures. */
  2390. static zend_bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
  2391. zend_ast_list *list = zend_ast_get_list(ast);
  2392. zend_bool has_refs = 0;
  2393. uint32_t i;
  2394. for (i = 0; i < list->children; ++i) {
  2395. zend_ast *elem_ast = list->child[i];
  2396. if (elem_ast) {
  2397. zend_ast *var_ast = elem_ast->child[0];
  2398. if (var_ast->kind == ZEND_AST_ARRAY) {
  2399. elem_ast->attr = zend_propagate_list_refs(var_ast);
  2400. }
  2401. has_refs |= elem_ast->attr;
  2402. }
  2403. }
  2404. return has_refs;
  2405. }
  2406. /* }}} */
  2407. static void zend_compile_list_assign(
  2408. znode *result, zend_ast *ast, znode *expr_node, zend_bool old_style) /* {{{ */
  2409. {
  2410. zend_ast_list *list = zend_ast_get_list(ast);
  2411. uint32_t i;
  2412. zend_bool has_elems = 0;
  2413. zend_bool is_keyed =
  2414. list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL;
  2415. if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
  2416. zval_make_interned_string(&expr_node->u.constant);
  2417. }
  2418. for (i = 0; i < list->children; ++i) {
  2419. zend_ast *elem_ast = list->child[i];
  2420. zend_ast *var_ast, *key_ast;
  2421. znode fetch_result, dim_node;
  2422. zend_op *opline;
  2423. if (elem_ast == NULL) {
  2424. if (is_keyed) {
  2425. zend_error(E_COMPILE_ERROR,
  2426. "Cannot use empty array entries in keyed array assignment");
  2427. } else {
  2428. continue;
  2429. }
  2430. }
  2431. var_ast = elem_ast->child[0];
  2432. key_ast = elem_ast->child[1];
  2433. has_elems = 1;
  2434. if (is_keyed) {
  2435. if (key_ast == NULL) {
  2436. zend_error(E_COMPILE_ERROR,
  2437. "Cannot mix keyed and unkeyed array entries in assignments");
  2438. }
  2439. zend_compile_expr(&dim_node, key_ast);
  2440. } else {
  2441. if (key_ast != NULL) {
  2442. zend_error(E_COMPILE_ERROR,
  2443. "Cannot mix keyed and unkeyed array entries in assignments");
  2444. }
  2445. dim_node.op_type = IS_CONST;
  2446. ZVAL_LONG(&dim_node.u.constant, i);
  2447. }
  2448. if (expr_node->op_type == IS_CONST) {
  2449. Z_TRY_ADDREF(expr_node->u.constant);
  2450. }
  2451. zend_verify_list_assign_target(var_ast, old_style);
  2452. opline = zend_emit_op(&fetch_result,
  2453. 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);
  2454. if (dim_node.op_type == IS_CONST) {
  2455. zend_handle_numeric_dim(opline, &dim_node);
  2456. }
  2457. if (var_ast->kind == ZEND_AST_ARRAY) {
  2458. if (elem_ast->attr) {
  2459. zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
  2460. }
  2461. zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
  2462. } else if (elem_ast->attr) {
  2463. zend_emit_assign_ref_znode(var_ast, &fetch_result);
  2464. } else {
  2465. zend_emit_assign_znode(var_ast, &fetch_result);
  2466. }
  2467. }
  2468. if (has_elems == 0) {
  2469. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
  2470. }
  2471. if (result) {
  2472. *result = *expr_node;
  2473. } else {
  2474. zend_do_free(expr_node);
  2475. }
  2476. }
  2477. /* }}} */
  2478. static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
  2479. {
  2480. if (ast->kind == ZEND_AST_CALL) {
  2481. zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
  2482. }
  2483. if (ast->kind == ZEND_AST_METHOD_CALL || ast->kind == ZEND_AST_STATIC_CALL) {
  2484. zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
  2485. }
  2486. }
  2487. /* }}} */
  2488. /* Detects $a... = $a pattern */
  2489. zend_bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
  2490. {
  2491. if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
  2492. return 0;
  2493. }
  2494. while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
  2495. var_ast = var_ast->child[0];
  2496. }
  2497. if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
  2498. return 0;
  2499. }
  2500. {
  2501. zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
  2502. zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
  2503. zend_bool result = zend_string_equals(name1, name2);
  2504. zend_string_release_ex(name1, 0);
  2505. zend_string_release_ex(name2, 0);
  2506. return result;
  2507. }
  2508. }
  2509. /* }}} */
  2510. /* Detects if list($a, $b, $c) contains variable with given name */
  2511. zend_bool zend_list_has_assign_to(zend_ast *list_ast, zend_string *name) /* {{{ */
  2512. {
  2513. zend_ast_list *list = zend_ast_get_list(list_ast);
  2514. uint32_t i;
  2515. for (i = 0; i < list->children; i++) {
  2516. zend_ast *elem_ast = list->child[i];
  2517. zend_ast *var_ast;
  2518. if (!elem_ast) {
  2519. continue;
  2520. }
  2521. var_ast = elem_ast->child[0];
  2522. /* Recursively check nested list()s */
  2523. if (var_ast->kind == ZEND_AST_ARRAY && zend_list_has_assign_to(var_ast, name)) {
  2524. return 1;
  2525. }
  2526. if (var_ast->kind == ZEND_AST_VAR && var_ast->child[0]->kind == ZEND_AST_ZVAL) {
  2527. zend_string *var_name = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
  2528. zend_bool result = zend_string_equals(var_name, name);
  2529. zend_string_release_ex(var_name, 0);
  2530. if (result) {
  2531. return 1;
  2532. }
  2533. }
  2534. }
  2535. return 0;
  2536. }
  2537. /* }}} */
  2538. /* Detects patterns like list($a, $b, $c) = $a */
  2539. zend_bool zend_list_has_assign_to_self(zend_ast *list_ast, zend_ast *expr_ast) /* {{{ */
  2540. {
  2541. /* Only check simple variables on the RHS, as only CVs cause issues with this. */
  2542. if (expr_ast->kind == ZEND_AST_VAR && expr_ast->child[0]->kind == ZEND_AST_ZVAL) {
  2543. zend_string *name = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
  2544. zend_bool result = zend_list_has_assign_to(list_ast, name);
  2545. zend_string_release_ex(name, 0);
  2546. return result;
  2547. }
  2548. return 0;
  2549. }
  2550. /* }}} */
  2551. void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
  2552. {
  2553. zend_ast *var_ast = ast->child[0];
  2554. zend_ast *expr_ast = ast->child[1];
  2555. znode var_node, expr_node;
  2556. zend_op *opline;
  2557. uint32_t offset;
  2558. if (is_this_fetch(var_ast)) {
  2559. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  2560. }
  2561. zend_ensure_writable_variable(var_ast);
  2562. switch (var_ast->kind) {
  2563. case ZEND_AST_VAR:
  2564. case ZEND_AST_STATIC_PROP:
  2565. offset = zend_delayed_compile_begin();
  2566. zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W);
  2567. zend_compile_expr(&expr_node, expr_ast);
  2568. zend_delayed_compile_end(offset);
  2569. zend_emit_op(result, ZEND_ASSIGN, &var_node, &expr_node);
  2570. return;
  2571. case ZEND_AST_DIM:
  2572. offset = zend_delayed_compile_begin();
  2573. zend_delayed_compile_dim(result, var_ast, BP_VAR_W);
  2574. if (zend_is_assign_to_self(var_ast, expr_ast)
  2575. && !is_this_fetch(expr_ast)) {
  2576. /* $a[0] = $a should evaluate the right $a first */
  2577. znode cv_node;
  2578. if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
  2579. zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
  2580. } else {
  2581. zend_emit_op(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
  2582. }
  2583. } else {
  2584. zend_compile_expr(&expr_node, expr_ast);
  2585. }
  2586. opline = zend_delayed_compile_end(offset);
  2587. opline->opcode = ZEND_ASSIGN_DIM;
  2588. opline = zend_emit_op_data(&expr_node);
  2589. return;
  2590. case ZEND_AST_PROP:
  2591. offset = zend_delayed_compile_begin();
  2592. zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
  2593. zend_compile_expr(&expr_node, expr_ast);
  2594. opline = zend_delayed_compile_end(offset);
  2595. opline->opcode = ZEND_ASSIGN_OBJ;
  2596. zend_emit_op_data(&expr_node);
  2597. return;
  2598. case ZEND_AST_ARRAY:
  2599. if (zend_propagate_list_refs(var_ast)) {
  2600. if (!zend_is_variable(expr_ast)) {
  2601. zend_error_noreturn(E_COMPILE_ERROR,
  2602. "Cannot assign reference to non referencable value");
  2603. }
  2604. zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
  2605. /* MAKE_REF is usually not necessary for CVs. However, if there are
  2606. * self-assignments, this forces the RHS to evaluate first. */
  2607. if (expr_node.op_type != IS_CV
  2608. || zend_list_has_assign_to_self(var_ast, expr_ast)) {
  2609. zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
  2610. }
  2611. } else {
  2612. if (zend_list_has_assign_to_self(var_ast, expr_ast)) {
  2613. /* list($a, $b) = $a should evaluate the right $a first */
  2614. znode cv_node;
  2615. if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
  2616. zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
  2617. } else {
  2618. zend_emit_op(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
  2619. }
  2620. } else {
  2621. zend_compile_expr(&expr_node, expr_ast);
  2622. }
  2623. }
  2624. zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
  2625. return;
  2626. EMPTY_SWITCH_DEFAULT_CASE();
  2627. }
  2628. }
  2629. /* }}} */
  2630. void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
  2631. {
  2632. zend_ast *target_ast = ast->child[0];
  2633. zend_ast *source_ast = ast->child[1];
  2634. znode target_node, source_node;
  2635. zend_op *opline;
  2636. uint32_t offset;
  2637. if (is_this_fetch(target_ast)) {
  2638. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  2639. }
  2640. zend_ensure_writable_variable(target_ast);
  2641. offset = zend_delayed_compile_begin();
  2642. zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W);
  2643. zend_compile_var(&source_node, source_ast, BP_VAR_W);
  2644. if ((target_ast->kind != ZEND_AST_VAR
  2645. || target_ast->child[0]->kind != ZEND_AST_ZVAL)
  2646. && source_node.op_type != IS_CV) {
  2647. /* Both LHS and RHS expressions may modify the same data structure,
  2648. * and the modification during RHS evaluation may dangle the pointer
  2649. * to the result of the LHS evaluation.
  2650. * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
  2651. * See: Bug #71539
  2652. */
  2653. zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
  2654. }
  2655. zend_delayed_compile_end(offset);
  2656. if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
  2657. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
  2658. }
  2659. opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
  2660. if (zend_is_call(source_ast)) {
  2661. opline->extended_value = ZEND_RETURNS_FUNCTION;
  2662. }
  2663. }
  2664. /* }}} */
  2665. static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
  2666. {
  2667. zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
  2668. zend_ast_create_znode(value_node));
  2669. zend_compile_assign_ref(NULL, assign_ast);
  2670. }
  2671. /* }}} */
  2672. void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
  2673. {
  2674. zend_ast *var_ast = ast->child[0];
  2675. zend_ast *expr_ast = ast->child[1];
  2676. uint32_t opcode = ast->attr;
  2677. znode var_node, expr_node;
  2678. zend_op *opline;
  2679. uint32_t offset, cache_slot;
  2680. zend_ensure_writable_variable(var_ast);
  2681. switch (var_ast->kind) {
  2682. case ZEND_AST_VAR:
  2683. case ZEND_AST_STATIC_PROP:
  2684. offset = zend_delayed_compile_begin();
  2685. zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW);
  2686. zend_compile_expr(&expr_node, expr_ast);
  2687. zend_delayed_compile_end(offset);
  2688. zend_emit_op(result, opcode, &var_node, &expr_node);
  2689. return;
  2690. case ZEND_AST_DIM:
  2691. offset = zend_delayed_compile_begin();
  2692. zend_delayed_compile_dim(result, var_ast, BP_VAR_RW);
  2693. zend_compile_expr(&expr_node, expr_ast);
  2694. opline = zend_delayed_compile_end(offset);
  2695. opline->opcode = opcode;
  2696. opline->extended_value = ZEND_ASSIGN_DIM;
  2697. opline = zend_emit_op_data(&expr_node);
  2698. return;
  2699. case ZEND_AST_PROP:
  2700. offset = zend_delayed_compile_begin();
  2701. zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
  2702. zend_compile_expr(&expr_node, expr_ast);
  2703. opline = zend_delayed_compile_end(offset);
  2704. cache_slot = opline->extended_value;
  2705. opline->opcode = opcode;
  2706. opline->extended_value = ZEND_ASSIGN_OBJ;
  2707. opline = zend_emit_op_data(&expr_node);
  2708. opline->extended_value = cache_slot;
  2709. return;
  2710. EMPTY_SWITCH_DEFAULT_CASE()
  2711. }
  2712. }
  2713. /* }}} */
  2714. uint32_t zend_compile_args(zend_ast *ast, zend_function *fbc) /* {{{ */
  2715. {
  2716. zend_ast_list *args = zend_ast_get_list(ast);
  2717. uint32_t i;
  2718. zend_bool uses_arg_unpack = 0;
  2719. uint32_t arg_count = 0; /* number of arguments not including unpacks */
  2720. for (i = 0; i < args->children; ++i) {
  2721. zend_ast *arg = args->child[i];
  2722. uint32_t arg_num = i + 1;
  2723. znode arg_node;
  2724. zend_op *opline;
  2725. zend_uchar opcode;
  2726. if (arg->kind == ZEND_AST_UNPACK) {
  2727. uses_arg_unpack = 1;
  2728. fbc = NULL;
  2729. zend_compile_expr(&arg_node, arg->child[0]);
  2730. opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
  2731. opline->op2.num = arg_count;
  2732. opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, arg_count);
  2733. continue;
  2734. }
  2735. if (uses_arg_unpack) {
  2736. zend_error_noreturn(E_COMPILE_ERROR,
  2737. "Cannot use positional argument after argument unpacking");
  2738. }
  2739. arg_count++;
  2740. if (zend_is_variable(arg)) {
  2741. if (zend_is_call(arg)) {
  2742. zend_compile_var(&arg_node, arg, BP_VAR_R);
  2743. if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
  2744. /* Function call was converted into builtin instruction */
  2745. opcode = ZEND_SEND_VAL;
  2746. } else {
  2747. if (fbc) {
  2748. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  2749. opcode = ZEND_SEND_VAR_NO_REF;
  2750. } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
  2751. opcode = ZEND_SEND_VAL;
  2752. } else {
  2753. opcode = ZEND_SEND_VAR;
  2754. }
  2755. } else {
  2756. opcode = ZEND_SEND_VAR_NO_REF_EX;
  2757. }
  2758. }
  2759. } else if (fbc) {
  2760. if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
  2761. zend_compile_var(&arg_node, arg, BP_VAR_W);
  2762. opcode = ZEND_SEND_REF;
  2763. } else {
  2764. zend_compile_var(&arg_node, arg, BP_VAR_R);
  2765. opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
  2766. }
  2767. } else {
  2768. do {
  2769. if (arg->kind == ZEND_AST_VAR) {
  2770. CG(zend_lineno) = zend_ast_get_lineno(ast);
  2771. if (is_this_fetch(arg)) {
  2772. zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
  2773. opcode = ZEND_SEND_VAR_EX;
  2774. break;
  2775. } else if (zend_try_compile_cv(&arg_node, arg) == SUCCESS) {
  2776. opcode = ZEND_SEND_VAR_EX;
  2777. break;
  2778. }
  2779. }
  2780. opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
  2781. opline->op2.num = arg_num;
  2782. zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG);
  2783. opcode = ZEND_SEND_FUNC_ARG;
  2784. } while (0);
  2785. }
  2786. } else {
  2787. zend_compile_expr(&arg_node, arg);
  2788. if (arg_node.op_type == IS_VAR) {
  2789. /* pass ++$a or something similar */
  2790. if (fbc) {
  2791. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  2792. opcode = ZEND_SEND_VAR_NO_REF;
  2793. } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
  2794. opcode = ZEND_SEND_VAL;
  2795. } else {
  2796. opcode = ZEND_SEND_VAR;
  2797. }
  2798. } else {
  2799. opcode = ZEND_SEND_VAR_NO_REF_EX;
  2800. }
  2801. } else if (arg_node.op_type == IS_CV) {
  2802. if (fbc) {
  2803. if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
  2804. opcode = ZEND_SEND_REF;
  2805. } else {
  2806. opcode = ZEND_SEND_VAR;
  2807. }
  2808. } else {
  2809. opcode = ZEND_SEND_VAR_EX;
  2810. }
  2811. } else {
  2812. if (fbc) {
  2813. opcode = ZEND_SEND_VAL;
  2814. if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
  2815. zend_error_noreturn(E_COMPILE_ERROR, "Only variables can be passed by reference");
  2816. }
  2817. } else {
  2818. opcode = ZEND_SEND_VAL_EX;
  2819. }
  2820. }
  2821. }
  2822. opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
  2823. opline->op2.opline_num = arg_num;
  2824. opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, arg_num);
  2825. }
  2826. return arg_count;
  2827. }
  2828. /* }}} */
  2829. ZEND_API zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc) /* {{{ */
  2830. {
  2831. if (fbc) {
  2832. if (fbc->type == ZEND_INTERNAL_FUNCTION) {
  2833. if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
  2834. if (!(fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED|ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_RETURN_REFERENCE))) {
  2835. return ZEND_DO_ICALL;
  2836. } else {
  2837. return ZEND_DO_FCALL_BY_NAME;
  2838. }
  2839. }
  2840. } else {
  2841. if (zend_execute_ex == execute_ex && !(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
  2842. return ZEND_DO_UCALL;
  2843. }
  2844. }
  2845. } else if (zend_execute_ex == execute_ex &&
  2846. !zend_execute_internal &&
  2847. (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
  2848. init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
  2849. return ZEND_DO_FCALL_BY_NAME;
  2850. }
  2851. return ZEND_DO_FCALL;
  2852. }
  2853. /* }}} */
  2854. void zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc) /* {{{ */
  2855. {
  2856. zend_op *opline;
  2857. uint32_t opnum_init = get_next_op_number(CG(active_op_array)) - 1;
  2858. uint32_t arg_count;
  2859. uint32_t call_flags;
  2860. zend_do_extended_fcall_begin();
  2861. arg_count = zend_compile_args(args_ast, fbc);
  2862. opline = &CG(active_op_array)->opcodes[opnum_init];
  2863. opline->extended_value = arg_count;
  2864. if (opline->opcode == ZEND_INIT_FCALL) {
  2865. opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
  2866. }
  2867. call_flags = (opline->opcode == ZEND_NEW ? ZEND_CALL_CTOR : 0);
  2868. opline = zend_emit_op(result, zend_get_call_op(opline, fbc), NULL, NULL);
  2869. opline->op1.num = call_flags;
  2870. zend_do_extended_fcall_end();
  2871. }
  2872. /* }}} */
  2873. zend_bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
  2874. {
  2875. zend_string *orig_name = zend_ast_get_str(name_ast);
  2876. zend_bool is_fully_qualified;
  2877. name_node->op_type = IS_CONST;
  2878. ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
  2879. orig_name, name_ast->attr, &is_fully_qualified));
  2880. return !is_fully_qualified && FC(current_namespace);
  2881. }
  2882. /* }}} */
  2883. void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
  2884. {
  2885. zend_op *opline = get_next_op(CG(active_op_array));
  2886. opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
  2887. opline->op2_type = IS_CONST;
  2888. opline->op2.constant = zend_add_ns_func_name_literal(
  2889. CG(active_op_array), Z_STR(name_node->u.constant));
  2890. opline->result.num = zend_alloc_cache_slot();
  2891. zend_compile_call_common(result, args_ast, NULL);
  2892. }
  2893. /* }}} */
  2894. void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
  2895. {
  2896. if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
  2897. const char *colon;
  2898. zend_string *str = Z_STR(name_node->u.constant);
  2899. if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
  2900. zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
  2901. zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
  2902. zend_op *opline = get_next_op(CG(active_op_array));
  2903. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  2904. opline->op1_type = IS_CONST;
  2905. opline->op1.constant = zend_add_class_name_literal(CG(active_op_array), class);
  2906. opline->op2_type = IS_CONST;
  2907. opline->op2.constant = zend_add_func_name_literal(CG(active_op_array), method);
  2908. /* 2 slots, for class and method */
  2909. opline->result.num = zend_alloc_polymorphic_cache_slot();
  2910. zval_ptr_dtor(&name_node->u.constant);
  2911. } else {
  2912. zend_op *opline = get_next_op(CG(active_op_array));
  2913. opline->opcode = ZEND_INIT_FCALL_BY_NAME;
  2914. opline->op2_type = IS_CONST;
  2915. opline->op2.constant = zend_add_func_name_literal(CG(active_op_array), str);
  2916. opline->result.num = zend_alloc_cache_slot();
  2917. }
  2918. } else {
  2919. zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
  2920. }
  2921. zend_compile_call_common(result, args_ast, NULL);
  2922. }
  2923. /* }}} */
  2924. static zend_bool zend_args_contain_unpack(zend_ast_list *args) /* {{{ */
  2925. {
  2926. uint32_t i;
  2927. for (i = 0; i < args->children; ++i) {
  2928. if (args->child[i]->kind == ZEND_AST_UNPACK) {
  2929. return 1;
  2930. }
  2931. }
  2932. return 0;
  2933. }
  2934. /* }}} */
  2935. int zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
  2936. {
  2937. znode arg_node;
  2938. if ((CG(compiler_options) & ZEND_COMPILE_NO_BUILTIN_STRLEN)
  2939. || args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK
  2940. ) {
  2941. return FAILURE;
  2942. }
  2943. zend_compile_expr(&arg_node, args->child[0]);
  2944. if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
  2945. result->op_type = IS_CONST;
  2946. ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
  2947. zval_ptr_dtor_str(&arg_node.u.constant);
  2948. } else {
  2949. zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
  2950. }
  2951. return SUCCESS;
  2952. }
  2953. /* }}} */
  2954. int zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
  2955. {
  2956. znode arg_node;
  2957. zend_op *opline;
  2958. if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
  2959. return FAILURE;
  2960. }
  2961. zend_compile_expr(&arg_node, args->child[0]);
  2962. opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
  2963. if (type != _IS_BOOL) {
  2964. opline->extended_value = (1 << type);
  2965. } else {
  2966. opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
  2967. }
  2968. return SUCCESS;
  2969. }
  2970. /* }}} */
  2971. int zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
  2972. {
  2973. znode arg_node;
  2974. zend_op *opline;
  2975. if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
  2976. return FAILURE;
  2977. }
  2978. zend_compile_expr(&arg_node, args->child[0]);
  2979. opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
  2980. opline->extended_value = type;
  2981. return SUCCESS;
  2982. }
  2983. /* }}} */
  2984. int zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
  2985. {
  2986. zend_string *name;
  2987. zend_op *opline;
  2988. if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
  2989. return FAILURE;
  2990. }
  2991. name = zval_get_string(zend_ast_get_zval(args->child[0]));
  2992. if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
  2993. zend_string_release_ex(name, 0);
  2994. return FAILURE;
  2995. }
  2996. if (zend_try_ct_eval_const(&result->u.constant, name, 0)) {
  2997. zend_string_release_ex(name, 0);
  2998. zval_ptr_dtor(&result->u.constant);
  2999. ZVAL_TRUE(&result->u.constant);
  3000. result->op_type = IS_CONST;
  3001. return SUCCESS;
  3002. }
  3003. opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
  3004. opline->op1_type = IS_CONST;
  3005. LITERAL_STR(opline->op1, name);
  3006. opline->extended_value = zend_alloc_cache_slot();
  3007. /* Lowercase constant name in a separate literal */
  3008. {
  3009. zval c;
  3010. zend_string *lcname = zend_string_tolower(name);
  3011. ZVAL_NEW_STR(&c, lcname);
  3012. zend_add_literal(CG(active_op_array), &c);
  3013. }
  3014. return SUCCESS;
  3015. }
  3016. /* }}} */
  3017. int zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
  3018. {
  3019. if (args->children == 1 &&
  3020. args->child[0]->kind == ZEND_AST_ZVAL &&
  3021. Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_LONG) {
  3022. zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;
  3023. result->op_type = IS_CONST;
  3024. ZVAL_INTERNED_STR(&result->u.constant, ZSTR_CHAR(c));
  3025. return SUCCESS;
  3026. } else {
  3027. return FAILURE;
  3028. }
  3029. }
  3030. /* }}} */
  3031. int zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
  3032. {
  3033. if (args->children == 1 &&
  3034. args->child[0]->kind == ZEND_AST_ZVAL &&
  3035. Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
  3036. result->op_type = IS_CONST;
  3037. ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
  3038. return SUCCESS;
  3039. } else {
  3040. return FAILURE;
  3041. }
  3042. }
  3043. /* }}} */
  3044. static int zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
  3045. {
  3046. zend_string *name, *lcname;
  3047. zend_function *fbc;
  3048. zend_op *opline;
  3049. if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
  3050. return FAILURE;
  3051. }
  3052. name = zend_ast_get_str(name_ast);
  3053. lcname = zend_string_tolower(name);
  3054. fbc = zend_hash_find_ptr(CG(function_table), lcname);
  3055. if (!fbc
  3056. || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
  3057. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
  3058. ) {
  3059. zend_string_release_ex(lcname, 0);
  3060. return FAILURE;
  3061. }
  3062. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
  3063. opline->extended_value = num_args;
  3064. opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
  3065. opline->op2_type = IS_CONST;
  3066. LITERAL_STR(opline->op2, lcname);
  3067. opline->result.num = zend_alloc_cache_slot();
  3068. return SUCCESS;
  3069. }
  3070. /* }}} */
  3071. static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
  3072. {
  3073. zend_op *opline;
  3074. znode name_node;
  3075. if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
  3076. return;
  3077. }
  3078. zend_compile_expr(&name_node, name_ast);
  3079. opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
  3080. opline->op1_type = IS_CONST;
  3081. LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
  3082. opline->extended_value = num_args;
  3083. }
  3084. /* }}} */
  3085. /* cufa = call_user_func_array */
  3086. int zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3087. {
  3088. znode arg_node;
  3089. if (args->children != 2 || zend_args_contain_unpack(args)) {
  3090. return FAILURE;
  3091. }
  3092. zend_compile_init_user_func(args->child[0], 0, lcname);
  3093. if (args->child[1]->kind == ZEND_AST_CALL
  3094. && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
  3095. && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
  3096. && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
  3097. zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
  3098. zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
  3099. zend_bool is_fully_qualified;
  3100. zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
  3101. if (zend_string_equals_literal_ci(name, "array_slice")
  3102. && list->children == 3
  3103. && list->child[1]->kind == ZEND_AST_ZVAL) {
  3104. zval *zv = zend_ast_get_zval(list->child[1]);
  3105. if (Z_TYPE_P(zv) == IS_LONG
  3106. && Z_LVAL_P(zv) >= 0
  3107. && Z_LVAL_P(zv) <= 0x7fffffff) {
  3108. zend_op *opline;
  3109. znode len_node;
  3110. zend_compile_expr(&arg_node, list->child[0]);
  3111. zend_compile_expr(&len_node, list->child[2]);
  3112. opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
  3113. opline->extended_value = Z_LVAL_P(zv);
  3114. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3115. zend_string_release_ex(name, 0);
  3116. return SUCCESS;
  3117. }
  3118. }
  3119. zend_string_release_ex(name, 0);
  3120. }
  3121. zend_compile_expr(&arg_node, args->child[1]);
  3122. zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
  3123. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3124. return SUCCESS;
  3125. }
  3126. /* }}} */
  3127. /* cuf = call_user_func */
  3128. int zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
  3129. {
  3130. uint32_t i;
  3131. if (args->children < 1 || zend_args_contain_unpack(args)) {
  3132. return FAILURE;
  3133. }
  3134. zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
  3135. for (i = 1; i < args->children; ++i) {
  3136. zend_ast *arg_ast = args->child[i];
  3137. znode arg_node;
  3138. zend_op *opline;
  3139. zend_compile_expr(&arg_node, arg_ast);
  3140. opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
  3141. opline->op2.num = i;
  3142. opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, i);
  3143. }
  3144. zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
  3145. return SUCCESS;
  3146. }
  3147. /* }}} */
  3148. static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
  3149. {
  3150. if (EG(assertions) >= 0) {
  3151. znode name_node;
  3152. zend_op *opline;
  3153. uint32_t check_op_number = get_next_op_number(CG(active_op_array));
  3154. zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
  3155. if (fbc) {
  3156. name_node.op_type = IS_CONST;
  3157. ZVAL_STR_COPY(&name_node.u.constant, name);
  3158. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
  3159. } else {
  3160. opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
  3161. opline->op2_type = IS_CONST;
  3162. opline->op2.constant = zend_add_ns_func_name_literal(
  3163. CG(active_op_array), name);
  3164. }
  3165. opline->result.num = zend_alloc_cache_slot();
  3166. if (args->children == 1 &&
  3167. (args->child[0]->kind != ZEND_AST_ZVAL ||
  3168. Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
  3169. /* add "assert(condition) as assertion message */
  3170. zend_ast_list_add((zend_ast*)args,
  3171. zend_ast_create_zval_from_str(
  3172. zend_ast_export("assert(", args->child[0], ")")));
  3173. }
  3174. zend_compile_call_common(result, (zend_ast*)args, fbc);
  3175. opline = &CG(active_op_array)->opcodes[check_op_number];
  3176. opline->op2.opline_num = get_next_op_number(CG(active_op_array));
  3177. SET_NODE(opline->result, result);
  3178. } else {
  3179. if (!fbc) {
  3180. zend_string_release_ex(name, 0);
  3181. }
  3182. result->op_type = IS_CONST;
  3183. ZVAL_TRUE(&result->u.constant);
  3184. }
  3185. }
  3186. /* }}} */
  3187. static int zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
  3188. {
  3189. zend_bool strict = 0;
  3190. znode array, needly;
  3191. zend_op *opline;
  3192. if (args->children == 3) {
  3193. if (args->child[2]->kind == ZEND_AST_ZVAL) {
  3194. strict = zend_is_true(zend_ast_get_zval(args->child[2]));
  3195. } else if (args->child[2]->kind == ZEND_AST_CONST) {
  3196. zval value;
  3197. zend_ast *name_ast = args->child[2]->child[0];
  3198. zend_bool is_fully_qualified;
  3199. zend_string *resolved_name = zend_resolve_const_name(
  3200. zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
  3201. if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
  3202. zend_string_release_ex(resolved_name, 0);
  3203. return FAILURE;
  3204. }
  3205. zend_string_release_ex(resolved_name, 0);
  3206. strict = zend_is_true(&value);
  3207. zval_ptr_dtor(&value);
  3208. } else {
  3209. return FAILURE;
  3210. }
  3211. } else if (args->children != 2) {
  3212. return FAILURE;
  3213. }
  3214. if (args->child[1]->kind != ZEND_AST_ARRAY
  3215. || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
  3216. return FAILURE;
  3217. }
  3218. if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
  3219. zend_bool ok = 1;
  3220. zval *val, tmp;
  3221. HashTable *src = Z_ARRVAL(array.u.constant);
  3222. HashTable *dst = zend_new_array(zend_hash_num_elements(src));
  3223. ZVAL_TRUE(&tmp);
  3224. if (strict) {
  3225. ZEND_HASH_FOREACH_VAL(src, val) {
  3226. if (Z_TYPE_P(val) == IS_STRING) {
  3227. zend_hash_add(dst, Z_STR_P(val), &tmp);
  3228. } else if (Z_TYPE_P(val) == IS_LONG) {
  3229. zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
  3230. } else {
  3231. zend_array_destroy(dst);
  3232. ok = 0;
  3233. break;
  3234. }
  3235. } ZEND_HASH_FOREACH_END();
  3236. } else {
  3237. ZEND_HASH_FOREACH_VAL(src, val) {
  3238. if (Z_TYPE_P(val) != IS_STRING
  3239. || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
  3240. zend_array_destroy(dst);
  3241. ok = 0;
  3242. break;
  3243. }
  3244. zend_hash_add(dst, Z_STR_P(val), &tmp);
  3245. } ZEND_HASH_FOREACH_END();
  3246. }
  3247. zend_array_destroy(src);
  3248. if (!ok) {
  3249. return FAILURE;
  3250. }
  3251. Z_ARRVAL(array.u.constant) = dst;
  3252. }
  3253. array.op_type = IS_CONST;
  3254. zend_compile_expr(&needly, args->child[0]);
  3255. opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
  3256. opline->extended_value = strict;
  3257. return SUCCESS;
  3258. }
  3259. /* }}} */
  3260. int zend_compile_func_count(znode *result, zend_ast_list *args) /* {{{ */
  3261. {
  3262. znode arg_node;
  3263. if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
  3264. return FAILURE;
  3265. }
  3266. zend_compile_expr(&arg_node, args->child[0]);
  3267. zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
  3268. return SUCCESS;
  3269. }
  3270. /* }}} */
  3271. int zend_compile_func_get_class(znode *result, zend_ast_list *args) /* {{{ */
  3272. {
  3273. if (args->children == 0) {
  3274. zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
  3275. } else {
  3276. znode arg_node;
  3277. if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
  3278. return FAILURE;
  3279. }
  3280. zend_compile_expr(&arg_node, args->child[0]);
  3281. zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
  3282. }
  3283. return SUCCESS;
  3284. }
  3285. /* }}} */
  3286. int zend_compile_func_get_called_class(znode *result, zend_ast_list *args) /* {{{ */
  3287. {
  3288. if (args->children != 0) {
  3289. return FAILURE;
  3290. }
  3291. zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
  3292. return SUCCESS;
  3293. }
  3294. /* }}} */
  3295. int zend_compile_func_gettype(znode *result, zend_ast_list *args) /* {{{ */
  3296. {
  3297. znode arg_node;
  3298. if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
  3299. return FAILURE;
  3300. }
  3301. zend_compile_expr(&arg_node, args->child[0]);
  3302. zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
  3303. return SUCCESS;
  3304. }
  3305. /* }}} */
  3306. int zend_compile_func_num_args(znode *result, zend_ast_list *args) /* {{{ */
  3307. {
  3308. if (CG(active_op_array)->function_name && args->children == 0) {
  3309. zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
  3310. return SUCCESS;
  3311. } else {
  3312. return FAILURE;
  3313. }
  3314. }
  3315. /* }}} */
  3316. int zend_compile_func_get_args(znode *result, zend_ast_list *args) /* {{{ */
  3317. {
  3318. if (CG(active_op_array)->function_name && args->children == 0) {
  3319. zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
  3320. return SUCCESS;
  3321. } else {
  3322. return FAILURE;
  3323. }
  3324. }
  3325. /* }}} */
  3326. int zend_compile_func_array_slice(znode *result, zend_ast_list *args) /* {{{ */
  3327. {
  3328. if (CG(active_op_array)->function_name
  3329. && args->children == 2
  3330. && args->child[0]->kind == ZEND_AST_CALL
  3331. && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
  3332. && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
  3333. && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
  3334. && args->child[1]->kind == ZEND_AST_ZVAL) {
  3335. zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
  3336. zend_bool is_fully_qualified;
  3337. zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
  3338. zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
  3339. zval *zv = zend_ast_get_zval(args->child[1]);
  3340. znode first;
  3341. if (zend_string_equals_literal_ci(name, "func_get_args")
  3342. && list->children == 0
  3343. && Z_TYPE_P(zv) == IS_LONG
  3344. && Z_LVAL_P(zv) >= 0) {
  3345. first.op_type = IS_CONST;
  3346. ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
  3347. zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
  3348. zend_string_release_ex(name, 0);
  3349. return SUCCESS;
  3350. }
  3351. zend_string_release_ex(name, 0);
  3352. }
  3353. return FAILURE;
  3354. }
  3355. /* }}} */
  3356. int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
  3357. {
  3358. if (fbc->internal_function.handler == ZEND_FN(display_disabled_function)) {
  3359. return FAILURE;
  3360. }
  3361. if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
  3362. return FAILURE;
  3363. }
  3364. if (zend_string_equals_literal(lcname, "strlen")) {
  3365. return zend_compile_func_strlen(result, args);
  3366. } else if (zend_string_equals_literal(lcname, "is_null")) {
  3367. return zend_compile_func_typecheck(result, args, IS_NULL);
  3368. } else if (zend_string_equals_literal(lcname, "is_bool")) {
  3369. return zend_compile_func_typecheck(result, args, _IS_BOOL);
  3370. } else if (zend_string_equals_literal(lcname, "is_long")
  3371. || zend_string_equals_literal(lcname, "is_int")
  3372. || zend_string_equals_literal(lcname, "is_integer")
  3373. ) {
  3374. return zend_compile_func_typecheck(result, args, IS_LONG);
  3375. } else if (zend_string_equals_literal(lcname, "is_float")
  3376. || zend_string_equals_literal(lcname, "is_double")
  3377. || zend_string_equals_literal(lcname, "is_real")
  3378. ) {
  3379. return zend_compile_func_typecheck(result, args, IS_DOUBLE);
  3380. } else if (zend_string_equals_literal(lcname, "is_string")) {
  3381. return zend_compile_func_typecheck(result, args, IS_STRING);
  3382. } else if (zend_string_equals_literal(lcname, "is_array")) {
  3383. return zend_compile_func_typecheck(result, args, IS_ARRAY);
  3384. } else if (zend_string_equals_literal(lcname, "is_object")) {
  3385. return zend_compile_func_typecheck(result, args, IS_OBJECT);
  3386. } else if (zend_string_equals_literal(lcname, "is_resource")) {
  3387. return zend_compile_func_typecheck(result, args, IS_RESOURCE);
  3388. } else if (zend_string_equals_literal(lcname, "boolval")) {
  3389. return zend_compile_func_cast(result, args, _IS_BOOL);
  3390. } else if (zend_string_equals_literal(lcname, "intval")) {
  3391. return zend_compile_func_cast(result, args, IS_LONG);
  3392. } else if (zend_string_equals_literal(lcname, "floatval")
  3393. || zend_string_equals_literal(lcname, "doubleval")
  3394. ) {
  3395. return zend_compile_func_cast(result, args, IS_DOUBLE);
  3396. } else if (zend_string_equals_literal(lcname, "strval")) {
  3397. return zend_compile_func_cast(result, args, IS_STRING);
  3398. } else if (zend_string_equals_literal(lcname, "defined")) {
  3399. return zend_compile_func_defined(result, args);
  3400. } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
  3401. return zend_compile_func_chr(result, args);
  3402. } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
  3403. return zend_compile_func_ord(result, args);
  3404. } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
  3405. return zend_compile_func_cufa(result, args, lcname);
  3406. } else if (zend_string_equals_literal(lcname, "call_user_func")) {
  3407. return zend_compile_func_cuf(result, args, lcname);
  3408. } else if (zend_string_equals_literal(lcname, "in_array")) {
  3409. return zend_compile_func_in_array(result, args);
  3410. } else if (zend_string_equals_literal(lcname, "count")) {
  3411. return zend_compile_func_count(result, args);
  3412. } else if (zend_string_equals_literal(lcname, "get_class")) {
  3413. return zend_compile_func_get_class(result, args);
  3414. } else if (zend_string_equals_literal(lcname, "get_called_class")) {
  3415. return zend_compile_func_get_called_class(result, args);
  3416. } else if (zend_string_equals_literal(lcname, "gettype")) {
  3417. return zend_compile_func_gettype(result, args);
  3418. } else if (zend_string_equals_literal(lcname, "func_num_args")) {
  3419. return zend_compile_func_num_args(result, args);
  3420. } else if (zend_string_equals_literal(lcname, "func_get_args")) {
  3421. return zend_compile_func_get_args(result, args);
  3422. } else if (zend_string_equals_literal(lcname, "array_slice")) {
  3423. return zend_compile_func_array_slice(result, args);
  3424. } else {
  3425. return FAILURE;
  3426. }
  3427. }
  3428. /* }}} */
  3429. void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3430. {
  3431. zend_ast *name_ast = ast->child[0];
  3432. zend_ast *args_ast = ast->child[1];
  3433. znode name_node;
  3434. if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
  3435. zend_compile_expr(&name_node, name_ast);
  3436. zend_compile_dynamic_call(result, &name_node, args_ast);
  3437. return;
  3438. }
  3439. {
  3440. zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
  3441. if (runtime_resolution) {
  3442. if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
  3443. zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
  3444. } else {
  3445. zend_compile_ns_call(result, &name_node, args_ast);
  3446. }
  3447. return;
  3448. }
  3449. }
  3450. {
  3451. zval *name = &name_node.u.constant;
  3452. zend_string *lcname;
  3453. zend_function *fbc;
  3454. zend_op *opline;
  3455. lcname = zend_string_tolower(Z_STR_P(name));
  3456. fbc = zend_hash_find_ptr(CG(function_table), lcname);
  3457. /* Special assert() handling should apply independently of compiler flags. */
  3458. if (fbc && zend_string_equals_literal(lcname, "assert")) {
  3459. zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc);
  3460. zend_string_release(lcname);
  3461. zval_ptr_dtor(&name_node.u.constant);
  3462. return;
  3463. }
  3464. if (!fbc
  3465. || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
  3466. || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
  3467. ) {
  3468. zend_string_release_ex(lcname, 0);
  3469. zend_compile_dynamic_call(result, &name_node, args_ast);
  3470. return;
  3471. }
  3472. if (zend_try_compile_special_func(result, lcname,
  3473. zend_ast_get_list(args_ast), fbc, type) == SUCCESS
  3474. ) {
  3475. zend_string_release_ex(lcname, 0);
  3476. zval_ptr_dtor(&name_node.u.constant);
  3477. return;
  3478. }
  3479. zval_ptr_dtor(&name_node.u.constant);
  3480. ZVAL_NEW_STR(&name_node.u.constant, lcname);
  3481. opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
  3482. opline->result.num = zend_alloc_cache_slot();
  3483. zend_compile_call_common(result, args_ast, fbc);
  3484. }
  3485. }
  3486. /* }}} */
  3487. void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3488. {
  3489. zend_ast *obj_ast = ast->child[0];
  3490. zend_ast *method_ast = ast->child[1];
  3491. zend_ast *args_ast = ast->child[2];
  3492. znode obj_node, method_node;
  3493. zend_op *opline;
  3494. zend_function *fbc = NULL;
  3495. if (is_this_fetch(obj_ast)) {
  3496. obj_node.op_type = IS_UNUSED;
  3497. } else {
  3498. zend_compile_expr(&obj_node, obj_ast);
  3499. }
  3500. zend_compile_expr(&method_node, method_ast);
  3501. opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
  3502. if (method_node.op_type == IS_CONST) {
  3503. if (Z_TYPE(method_node.u.constant) != IS_STRING) {
  3504. zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
  3505. }
  3506. opline->op2_type = IS_CONST;
  3507. opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
  3508. Z_STR(method_node.u.constant));
  3509. opline->result.num = zend_alloc_polymorphic_cache_slot();
  3510. } else {
  3511. SET_NODE(opline->op2, &method_node);
  3512. }
  3513. /* Check if this calls a known method on $this */
  3514. if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
  3515. CG(active_class_entry) && zend_is_scope_known()) {
  3516. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
  3517. fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
  3518. /* We only know the exact method that is being called if it is either private or final.
  3519. * Otherwise an overriding method in a child class may be called. */
  3520. if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
  3521. fbc = NULL;
  3522. }
  3523. }
  3524. zend_compile_call_common(result, args_ast, fbc);
  3525. }
  3526. /* }}} */
  3527. static zend_bool zend_is_constructor(zend_string *name) /* {{{ */
  3528. {
  3529. return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
  3530. }
  3531. /* }}} */
  3532. void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  3533. {
  3534. zend_ast *class_ast = ast->child[0];
  3535. zend_ast *method_ast = ast->child[1];
  3536. zend_ast *args_ast = ast->child[2];
  3537. znode class_node, method_node;
  3538. zend_op *opline;
  3539. zend_function *fbc = NULL;
  3540. zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  3541. zend_compile_expr(&method_node, method_ast);
  3542. if (method_node.op_type == IS_CONST) {
  3543. zval *name = &method_node.u.constant;
  3544. if (Z_TYPE_P(name) != IS_STRING) {
  3545. zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
  3546. }
  3547. if (zend_is_constructor(Z_STR_P(name))) {
  3548. zval_ptr_dtor(name);
  3549. method_node.op_type = IS_UNUSED;
  3550. }
  3551. }
  3552. opline = get_next_op(CG(active_op_array));
  3553. opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
  3554. zend_set_class_name_op1(opline, &class_node);
  3555. if (method_node.op_type == IS_CONST) {
  3556. opline->op2_type = IS_CONST;
  3557. opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
  3558. Z_STR(method_node.u.constant));
  3559. opline->result.num = zend_alloc_polymorphic_cache_slot();
  3560. } else {
  3561. if (opline->op1_type == IS_CONST) {
  3562. opline->result.num = zend_alloc_cache_slot();
  3563. }
  3564. SET_NODE(opline->op2, &method_node);
  3565. }
  3566. zend_check_live_ranges(opline);
  3567. /* Check if we already know which method we're calling */
  3568. if (opline->op2_type == IS_CONST) {
  3569. zend_class_entry *ce = NULL;
  3570. if (opline->op1_type == IS_CONST) {
  3571. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
  3572. ce = zend_hash_find_ptr(CG(class_table), lcname);
  3573. if (!ce && CG(active_class_entry)
  3574. && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
  3575. ce = CG(active_class_entry);
  3576. }
  3577. } else if (opline->op1_type == IS_UNUSED
  3578. && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
  3579. && zend_is_scope_known()) {
  3580. ce = CG(active_class_entry);
  3581. }
  3582. if (ce) {
  3583. zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
  3584. fbc = zend_hash_find_ptr(&ce->function_table, lcname);
  3585. if (fbc && !(fbc->common.fn_flags & ZEND_ACC_PUBLIC)) {
  3586. if (ce != CG(active_class_entry)
  3587. &&((fbc->common.fn_flags & ZEND_ACC_PRIVATE)
  3588. || !zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry)))) {
  3589. /* incompatibe function */
  3590. fbc = NULL;
  3591. }
  3592. }
  3593. }
  3594. }
  3595. zend_compile_call_common(result, args_ast, fbc);
  3596. }
  3597. /* }}} */
  3598. void zend_compile_class_decl(zend_ast *ast);
  3599. void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
  3600. {
  3601. zend_ast *class_ast = ast->child[0];
  3602. zend_ast *args_ast = ast->child[1];
  3603. znode class_node, ctor_result;
  3604. zend_op *opline;
  3605. if (class_ast->kind == ZEND_AST_CLASS) {
  3606. uint32_t dcl_opnum = get_next_op_number(CG(active_op_array));
  3607. zend_compile_class_decl(class_ast);
  3608. /* jump over anon class declaration */
  3609. opline = &CG(active_op_array)->opcodes[dcl_opnum];
  3610. if (opline->opcode == ZEND_FETCH_CLASS) {
  3611. opline++;
  3612. }
  3613. class_node.op_type = opline->result_type;
  3614. class_node.u.op.var = opline->result.var;
  3615. opline->extended_value = get_next_op_number(CG(active_op_array));
  3616. } else {
  3617. zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  3618. }
  3619. opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
  3620. if (class_node.op_type == IS_CONST) {
  3621. opline->op1_type = IS_CONST;
  3622. opline->op1.constant = zend_add_class_name_literal(
  3623. CG(active_op_array), Z_STR(class_node.u.constant));
  3624. opline->op2.num = zend_alloc_cache_slot();
  3625. } else {
  3626. SET_NODE(opline->op1, &class_node);
  3627. }
  3628. zend_compile_call_common(&ctor_result, args_ast, NULL);
  3629. zend_do_free(&ctor_result);
  3630. }
  3631. /* }}} */
  3632. void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
  3633. {
  3634. zend_ast *obj_ast = ast->child[0];
  3635. znode obj_node;
  3636. zend_compile_expr(&obj_node, obj_ast);
  3637. zend_emit_op_tmp(result, ZEND_CLONE, &obj_node, NULL);
  3638. }
  3639. /* }}} */
  3640. void zend_compile_global_var(zend_ast *ast) /* {{{ */
  3641. {
  3642. zend_ast *var_ast = ast->child[0];
  3643. zend_ast *name_ast = var_ast->child[0];
  3644. znode name_node, result;
  3645. zend_compile_expr(&name_node, name_ast);
  3646. if (name_node.op_type == IS_CONST) {
  3647. convert_to_string(&name_node.u.constant);
  3648. }
  3649. if (is_this_fetch(var_ast)) {
  3650. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
  3651. } else if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
  3652. zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
  3653. opline->extended_value = zend_alloc_cache_slot();
  3654. } else {
  3655. /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
  3656. * to not free the name_node operand, so it can be reused in the following
  3657. * ASSIGN_REF, which then frees it. */
  3658. zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
  3659. opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
  3660. if (name_node.op_type == IS_CONST) {
  3661. zend_string_addref(Z_STR(name_node.u.constant));
  3662. }
  3663. zend_emit_assign_ref_znode(
  3664. zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
  3665. &result
  3666. );
  3667. }
  3668. }
  3669. /* }}} */
  3670. static void zend_compile_static_var_common(zend_ast *var_ast, zval *value, uint32_t by_ref) /* {{{ */
  3671. {
  3672. znode var_node;
  3673. zend_op *opline;
  3674. zend_string *var_name;
  3675. if (var_ast->kind == ZEND_AST_ZVAL) {
  3676. var_name = zval_make_interned_string(zend_ast_get_zval(var_ast));
  3677. zend_compile_expr(&var_node, var_ast);
  3678. } else {
  3679. zend_compile_expr(&var_node, var_ast);
  3680. var_name = zval_make_interned_string(&var_node.u.constant);
  3681. }
  3682. if (!CG(active_op_array)->static_variables) {
  3683. if (CG(active_op_array)->scope) {
  3684. CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
  3685. }
  3686. CG(active_op_array)->static_variables = zend_new_array(8);
  3687. }
  3688. if (GC_REFCOUNT(CG(active_op_array)->static_variables) > 1) {
  3689. if (!(GC_FLAGS(CG(active_op_array)->static_variables) & IS_ARRAY_IMMUTABLE)) {
  3690. GC_DELREF(CG(active_op_array)->static_variables);
  3691. }
  3692. CG(active_op_array)->static_variables = zend_array_dup(CG(active_op_array)->static_variables);
  3693. }
  3694. value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
  3695. if (zend_string_equals_literal(var_name, "this")) {
  3696. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
  3697. }
  3698. opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &var_node);
  3699. opline->op1_type = IS_CV;
  3700. opline->op1.var = lookup_cv(CG(active_op_array), var_name);
  3701. opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | by_ref;
  3702. }
  3703. /* }}} */
  3704. void zend_compile_static_var(zend_ast *ast) /* {{{ */
  3705. {
  3706. zend_ast *var_ast = ast->child[0];
  3707. zend_ast *value_ast = ast->child[1];
  3708. zval value_zv;
  3709. if (value_ast) {
  3710. zend_const_expr_to_zval(&value_zv, value_ast);
  3711. } else {
  3712. ZVAL_NULL(&value_zv);
  3713. }
  3714. zend_compile_static_var_common(var_ast, &value_zv, ZEND_BIND_REF);
  3715. }
  3716. /* }}} */
  3717. void zend_compile_unset(zend_ast *ast) /* {{{ */
  3718. {
  3719. zend_ast *var_ast = ast->child[0];
  3720. znode var_node;
  3721. zend_op *opline;
  3722. zend_ensure_writable_variable(var_ast);
  3723. switch (var_ast->kind) {
  3724. case ZEND_AST_VAR:
  3725. if (is_this_fetch(var_ast)) {
  3726. zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
  3727. } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
  3728. opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
  3729. } else {
  3730. opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
  3731. opline->opcode = ZEND_UNSET_VAR;
  3732. }
  3733. return;
  3734. case ZEND_AST_DIM:
  3735. opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET);
  3736. opline->opcode = ZEND_UNSET_DIM;
  3737. return;
  3738. case ZEND_AST_PROP:
  3739. opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET);
  3740. opline->opcode = ZEND_UNSET_OBJ;
  3741. return;
  3742. case ZEND_AST_STATIC_PROP:
  3743. opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, 0);
  3744. opline->opcode = ZEND_UNSET_STATIC_PROP;
  3745. return;
  3746. EMPTY_SWITCH_DEFAULT_CASE()
  3747. }
  3748. }
  3749. /* }}} */
  3750. static int zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
  3751. {
  3752. zend_loop_var *base;
  3753. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  3754. if (!loop_var) {
  3755. return 1;
  3756. }
  3757. base = zend_stack_base(&CG(loop_var_stack));
  3758. for (; loop_var >= base; loop_var--) {
  3759. if (loop_var->opcode == ZEND_FAST_CALL) {
  3760. zend_op *opline = get_next_op(CG(active_op_array));
  3761. opline->opcode = ZEND_FAST_CALL;
  3762. opline->result_type = IS_TMP_VAR;
  3763. opline->result.var = loop_var->var_num;
  3764. if (return_value) {
  3765. SET_NODE(opline->op2, return_value);
  3766. }
  3767. opline->op1.num = loop_var->u.try_catch_offset;
  3768. } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
  3769. zend_op *opline = get_next_op(CG(active_op_array));
  3770. opline->opcode = ZEND_DISCARD_EXCEPTION;
  3771. opline->op1_type = IS_TMP_VAR;
  3772. opline->op1.var = loop_var->var_num;
  3773. } else if (loop_var->opcode == ZEND_RETURN) {
  3774. /* Stack separator */
  3775. break;
  3776. } else if (depth <= 1) {
  3777. return 1;
  3778. } else if (loop_var->opcode == ZEND_NOP) {
  3779. /* Loop doesn't have freeable variable */
  3780. depth--;
  3781. } else {
  3782. zend_op *opline;
  3783. ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
  3784. opline = get_next_op(CG(active_op_array));
  3785. opline->opcode = loop_var->opcode;
  3786. opline->op1_type = loop_var->var_type;
  3787. opline->op1.var = loop_var->var_num;
  3788. opline->extended_value = ZEND_FREE_ON_RETURN;
  3789. depth--;
  3790. }
  3791. }
  3792. return (depth == 0);
  3793. }
  3794. /* }}} */
  3795. static int zend_handle_loops_and_finally(znode *return_value) /* {{{ */
  3796. {
  3797. return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
  3798. }
  3799. /* }}} */
  3800. static int zend_has_finally_ex(zend_long depth) /* {{{ */
  3801. {
  3802. zend_loop_var *base;
  3803. zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
  3804. if (!loop_var) {
  3805. return 0;
  3806. }
  3807. base = zend_stack_base(&CG(loop_var_stack));
  3808. for (; loop_var >= base; loop_var--) {
  3809. if (loop_var->opcode == ZEND_FAST_CALL) {
  3810. return 1;
  3811. } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
  3812. } else if (loop_var->opcode == ZEND_RETURN) {
  3813. /* Stack separator */
  3814. return 0;
  3815. } else if (depth <= 1) {
  3816. return 0;
  3817. } else {
  3818. depth--;
  3819. }
  3820. }
  3821. return 0;
  3822. }
  3823. /* }}} */
  3824. static int zend_has_finally(void) /* {{{ */
  3825. {
  3826. return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
  3827. }
  3828. /* }}} */
  3829. void zend_compile_return(zend_ast *ast) /* {{{ */
  3830. {
  3831. zend_ast *expr_ast = ast->child[0];
  3832. zend_bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
  3833. zend_bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  3834. znode expr_node;
  3835. zend_op *opline;
  3836. if (is_generator) {
  3837. /* For generators the by-ref flag refers to yields, not returns */
  3838. by_ref = 0;
  3839. }
  3840. if (!expr_ast) {
  3841. expr_node.op_type = IS_CONST;
  3842. ZVAL_NULL(&expr_node.u.constant);
  3843. } else if (by_ref && zend_is_variable(expr_ast) && !zend_is_call(expr_ast)) {
  3844. zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
  3845. } else {
  3846. zend_compile_expr(&expr_node, expr_ast);
  3847. }
  3848. if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
  3849. && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
  3850. && zend_has_finally()) {
  3851. /* Copy return value into temporary VAR to avoid modification in finally code */
  3852. if (by_ref) {
  3853. zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
  3854. } else {
  3855. zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
  3856. }
  3857. }
  3858. /* Generator return types are handled separately */
  3859. if (!is_generator && CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  3860. zend_emit_return_type_check(
  3861. expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
  3862. }
  3863. zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
  3864. opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
  3865. &expr_node, NULL);
  3866. if (by_ref && expr_ast) {
  3867. if (zend_is_call(expr_ast)) {
  3868. opline->extended_value = ZEND_RETURNS_FUNCTION;
  3869. } else if (!zend_is_variable(expr_ast)) {
  3870. opline->extended_value = ZEND_RETURNS_VALUE;
  3871. }
  3872. }
  3873. }
  3874. /* }}} */
  3875. void zend_compile_echo(zend_ast *ast) /* {{{ */
  3876. {
  3877. zend_op *opline;
  3878. zend_ast *expr_ast = ast->child[0];
  3879. znode expr_node;
  3880. zend_compile_expr(&expr_node, expr_ast);
  3881. opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
  3882. opline->extended_value = 0;
  3883. }
  3884. /* }}} */
  3885. void zend_compile_throw(zend_ast *ast) /* {{{ */
  3886. {
  3887. zend_ast *expr_ast = ast->child[0];
  3888. znode expr_node;
  3889. zend_compile_expr(&expr_node, expr_ast);
  3890. zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
  3891. }
  3892. /* }}} */
  3893. void zend_compile_break_continue(zend_ast *ast) /* {{{ */
  3894. {
  3895. zend_ast *depth_ast = ast->child[0];
  3896. zend_op *opline;
  3897. zend_long depth;
  3898. ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
  3899. if (depth_ast) {
  3900. zval *depth_zv;
  3901. if (depth_ast->kind != ZEND_AST_ZVAL) {
  3902. zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
  3903. "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  3904. }
  3905. depth_zv = zend_ast_get_zval(depth_ast);
  3906. if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
  3907. zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
  3908. ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  3909. }
  3910. depth = Z_LVAL_P(depth_zv);
  3911. } else {
  3912. depth = 1;
  3913. }
  3914. if (CG(context).current_brk_cont == -1) {
  3915. zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
  3916. ast->kind == ZEND_AST_BREAK ? "break" : "continue");
  3917. } else {
  3918. if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
  3919. zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
  3920. ast->kind == ZEND_AST_BREAK ? "break" : "continue",
  3921. depth, depth == 1 ? "" : "s");
  3922. }
  3923. }
  3924. if (ast->kind == ZEND_AST_CONTINUE) {
  3925. int d, cur = CG(context).current_brk_cont;
  3926. for (d = depth - 1; d > 0; d--) {
  3927. cur = CG(context).brk_cont_array[cur].parent;
  3928. ZEND_ASSERT(cur != -1);
  3929. }
  3930. if (CG(context).brk_cont_array[cur].is_switch) {
  3931. if (depth == 1) {
  3932. zend_error(E_WARNING,
  3933. "\"continue\" targeting switch is equivalent to \"break\". " \
  3934. "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
  3935. depth + 1);
  3936. } else {
  3937. zend_error(E_WARNING,
  3938. "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
  3939. "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
  3940. depth, depth, depth + 1);
  3941. }
  3942. }
  3943. }
  3944. opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
  3945. opline->op1.num = CG(context).current_brk_cont;
  3946. opline->op2.num = depth;
  3947. }
  3948. /* }}} */
  3949. void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
  3950. {
  3951. zend_label *dest;
  3952. int current, remove_oplines = opline->op1.num;
  3953. zval *label;
  3954. uint32_t opnum = opline - op_array->opcodes;
  3955. label = CT_CONSTANT_EX(op_array, opline->op2.constant);
  3956. if (CG(context).labels == NULL ||
  3957. (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
  3958. ) {
  3959. CG(in_compilation) = 1;
  3960. CG(active_op_array) = op_array;
  3961. CG(zend_lineno) = opline->lineno;
  3962. zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
  3963. }
  3964. zval_ptr_dtor_str(label);
  3965. ZVAL_NULL(label);
  3966. current = opline->extended_value;
  3967. for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
  3968. if (current == -1) {
  3969. CG(in_compilation) = 1;
  3970. CG(active_op_array) = op_array;
  3971. CG(zend_lineno) = opline->lineno;
  3972. zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
  3973. }
  3974. if (CG(context).brk_cont_array[current].start >= 0) {
  3975. remove_oplines--;
  3976. }
  3977. }
  3978. for (current = 0; current < op_array->last_try_catch; ++current) {
  3979. zend_try_catch_element *elem = &op_array->try_catch_array[current];
  3980. if (elem->try_op > opnum) {
  3981. break;
  3982. }
  3983. if (elem->finally_op && opnum < elem->finally_op - 1
  3984. && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
  3985. ) {
  3986. remove_oplines--;
  3987. }
  3988. }
  3989. opline->opcode = ZEND_JMP;
  3990. opline->op1.opline_num = dest->opline_num;
  3991. opline->extended_value = 0;
  3992. SET_UNUSED(opline->op1);
  3993. SET_UNUSED(opline->op2);
  3994. SET_UNUSED(opline->result);
  3995. ZEND_ASSERT(remove_oplines >= 0);
  3996. while (remove_oplines--) {
  3997. opline--;
  3998. MAKE_NOP(opline);
  3999. ZEND_VM_SET_OPCODE_HANDLER(opline);
  4000. }
  4001. }
  4002. /* }}} */
  4003. void zend_compile_goto(zend_ast *ast) /* {{{ */
  4004. {
  4005. zend_ast *label_ast = ast->child[0];
  4006. znode label_node;
  4007. zend_op *opline;
  4008. uint32_t opnum_start = get_next_op_number(CG(active_op_array));
  4009. zend_compile_expr(&label_node, label_ast);
  4010. /* Label resolution and unwinding adjustments happen in pass two. */
  4011. zend_handle_loops_and_finally(NULL);
  4012. opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
  4013. opline->op1.num = get_next_op_number(CG(active_op_array)) - opnum_start - 1;
  4014. opline->extended_value = CG(context).current_brk_cont;
  4015. }
  4016. /* }}} */
  4017. void zend_compile_label(zend_ast *ast) /* {{{ */
  4018. {
  4019. zend_string *label = zend_ast_get_str(ast->child[0]);
  4020. zend_label dest;
  4021. if (!CG(context).labels) {
  4022. ALLOC_HASHTABLE(CG(context).labels);
  4023. zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
  4024. }
  4025. dest.brk_cont = CG(context).current_brk_cont;
  4026. dest.opline_num = get_next_op_number(CG(active_op_array));
  4027. if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
  4028. zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
  4029. }
  4030. }
  4031. /* }}} */
  4032. void zend_compile_while(zend_ast *ast) /* {{{ */
  4033. {
  4034. zend_ast *cond_ast = ast->child[0];
  4035. zend_ast *stmt_ast = ast->child[1];
  4036. znode cond_node;
  4037. uint32_t opnum_start, opnum_jmp, opnum_cond;
  4038. opnum_jmp = zend_emit_jump(0);
  4039. zend_begin_loop(ZEND_NOP, NULL, 0);
  4040. opnum_start = get_next_op_number(CG(active_op_array));
  4041. zend_compile_stmt(stmt_ast);
  4042. opnum_cond = get_next_op_number(CG(active_op_array));
  4043. zend_update_jump_target(opnum_jmp, opnum_cond);
  4044. zend_compile_expr(&cond_node, cond_ast);
  4045. zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
  4046. zend_end_loop(opnum_cond, NULL);
  4047. }
  4048. /* }}} */
  4049. void zend_compile_do_while(zend_ast *ast) /* {{{ */
  4050. {
  4051. zend_ast *stmt_ast = ast->child[0];
  4052. zend_ast *cond_ast = ast->child[1];
  4053. znode cond_node;
  4054. uint32_t opnum_start, opnum_cond;
  4055. zend_begin_loop(ZEND_NOP, NULL, 0);
  4056. opnum_start = get_next_op_number(CG(active_op_array));
  4057. zend_compile_stmt(stmt_ast);
  4058. opnum_cond = get_next_op_number(CG(active_op_array));
  4059. zend_compile_expr(&cond_node, cond_ast);
  4060. zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
  4061. zend_end_loop(opnum_cond, NULL);
  4062. }
  4063. /* }}} */
  4064. void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
  4065. {
  4066. zend_ast_list *list;
  4067. uint32_t i;
  4068. result->op_type = IS_CONST;
  4069. ZVAL_TRUE(&result->u.constant);
  4070. if (!ast) {
  4071. return;
  4072. }
  4073. list = zend_ast_get_list(ast);
  4074. for (i = 0; i < list->children; ++i) {
  4075. zend_ast *expr_ast = list->child[i];
  4076. zend_do_free(result);
  4077. zend_compile_expr(result, expr_ast);
  4078. }
  4079. }
  4080. /* }}} */
  4081. void zend_compile_for(zend_ast *ast) /* {{{ */
  4082. {
  4083. zend_ast *init_ast = ast->child[0];
  4084. zend_ast *cond_ast = ast->child[1];
  4085. zend_ast *loop_ast = ast->child[2];
  4086. zend_ast *stmt_ast = ast->child[3];
  4087. znode result;
  4088. uint32_t opnum_start, opnum_jmp, opnum_loop;
  4089. zend_compile_expr_list(&result, init_ast);
  4090. zend_do_free(&result);
  4091. opnum_jmp = zend_emit_jump(0);
  4092. zend_begin_loop(ZEND_NOP, NULL, 0);
  4093. opnum_start = get_next_op_number(CG(active_op_array));
  4094. zend_compile_stmt(stmt_ast);
  4095. opnum_loop = get_next_op_number(CG(active_op_array));
  4096. zend_compile_expr_list(&result, loop_ast);
  4097. zend_do_free(&result);
  4098. zend_update_jump_target_to_next(opnum_jmp);
  4099. zend_compile_expr_list(&result, cond_ast);
  4100. zend_do_extended_info();
  4101. zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
  4102. zend_end_loop(opnum_loop, NULL);
  4103. }
  4104. /* }}} */
  4105. void zend_compile_foreach(zend_ast *ast) /* {{{ */
  4106. {
  4107. zend_ast *expr_ast = ast->child[0];
  4108. zend_ast *value_ast = ast->child[1];
  4109. zend_ast *key_ast = ast->child[2];
  4110. zend_ast *stmt_ast = ast->child[3];
  4111. zend_bool by_ref = value_ast->kind == ZEND_AST_REF;
  4112. zend_bool is_variable = zend_is_variable(expr_ast) && !zend_is_call(expr_ast)
  4113. && zend_can_write_to_variable(expr_ast);
  4114. znode expr_node, reset_node, value_node, key_node;
  4115. zend_op *opline;
  4116. uint32_t opnum_reset, opnum_fetch;
  4117. if (key_ast) {
  4118. if (key_ast->kind == ZEND_AST_REF) {
  4119. zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
  4120. }
  4121. if (key_ast->kind == ZEND_AST_ARRAY) {
  4122. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
  4123. }
  4124. }
  4125. if (by_ref) {
  4126. value_ast = value_ast->child[0];
  4127. }
  4128. if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
  4129. by_ref = 1;
  4130. }
  4131. if (by_ref && is_variable) {
  4132. zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
  4133. } else {
  4134. zend_compile_expr(&expr_node, expr_ast);
  4135. }
  4136. if (by_ref) {
  4137. zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
  4138. }
  4139. opnum_reset = get_next_op_number(CG(active_op_array));
  4140. opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
  4141. zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);
  4142. opnum_fetch = get_next_op_number(CG(active_op_array));
  4143. opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
  4144. if (is_this_fetch(value_ast)) {
  4145. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  4146. } else if (value_ast->kind == ZEND_AST_VAR &&
  4147. zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
  4148. SET_NODE(opline->op2, &value_node);
  4149. } else {
  4150. opline->op2_type = IS_VAR;
  4151. opline->op2.var = get_temporary_variable(CG(active_op_array));
  4152. GET_NODE(&value_node, opline->op2);
  4153. if (value_ast->kind == ZEND_AST_ARRAY) {
  4154. zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
  4155. } else if (by_ref) {
  4156. zend_emit_assign_ref_znode(value_ast, &value_node);
  4157. } else {
  4158. zend_emit_assign_znode(value_ast, &value_node);
  4159. }
  4160. }
  4161. if (key_ast) {
  4162. opline = &CG(active_op_array)->opcodes[opnum_fetch];
  4163. zend_make_tmp_result(&key_node, opline);
  4164. zend_emit_assign_znode(key_ast, &key_node);
  4165. }
  4166. zend_compile_stmt(stmt_ast);
  4167. /* Place JMP and FE_FREE on the line where foreach starts. It would be
  4168. * better to use the end line, but this information is not available
  4169. * currently. */
  4170. CG(zend_lineno) = ast->lineno;
  4171. zend_emit_jump(opnum_fetch);
  4172. opline = &CG(active_op_array)->opcodes[opnum_reset];
  4173. opline->op2.opline_num = get_next_op_number(CG(active_op_array));
  4174. opline = &CG(active_op_array)->opcodes[opnum_fetch];
  4175. opline->extended_value = get_next_op_number(CG(active_op_array));
  4176. zend_end_loop(opnum_fetch, &reset_node);
  4177. opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
  4178. }
  4179. /* }}} */
  4180. void zend_compile_if(zend_ast *ast) /* {{{ */
  4181. {
  4182. zend_ast_list *list = zend_ast_get_list(ast);
  4183. uint32_t i;
  4184. uint32_t *jmp_opnums = NULL;
  4185. if (list->children > 1) {
  4186. jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
  4187. }
  4188. for (i = 0; i < list->children; ++i) {
  4189. zend_ast *elem_ast = list->child[i];
  4190. zend_ast *cond_ast = elem_ast->child[0];
  4191. zend_ast *stmt_ast = elem_ast->child[1];
  4192. znode cond_node;
  4193. uint32_t opnum_jmpz;
  4194. if (cond_ast) {
  4195. zend_compile_expr(&cond_node, cond_ast);
  4196. opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  4197. }
  4198. zend_compile_stmt(stmt_ast);
  4199. if (i != list->children - 1) {
  4200. jmp_opnums[i] = zend_emit_jump(0);
  4201. }
  4202. if (cond_ast) {
  4203. zend_update_jump_target_to_next(opnum_jmpz);
  4204. }
  4205. }
  4206. if (list->children > 1) {
  4207. for (i = 0; i < list->children - 1; ++i) {
  4208. zend_update_jump_target_to_next(jmp_opnums[i]);
  4209. }
  4210. efree(jmp_opnums);
  4211. }
  4212. }
  4213. /* }}} */
  4214. static zend_uchar determine_switch_jumptable_type(zend_ast_list *cases) {
  4215. uint32_t i;
  4216. zend_uchar common_type = IS_UNDEF;
  4217. for (i = 0; i < cases->children; i++) {
  4218. zend_ast *case_ast = cases->child[i];
  4219. zend_ast **cond_ast = &case_ast->child[0];
  4220. zval *cond_zv;
  4221. if (!case_ast->child[0]) {
  4222. /* Skip default clause */
  4223. continue;
  4224. }
  4225. zend_eval_const_expr(cond_ast);
  4226. if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
  4227. /* Non-constant case */
  4228. return IS_UNDEF;
  4229. }
  4230. cond_zv = zend_ast_get_zval(case_ast->child[0]);
  4231. if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
  4232. /* We only optimize switched on integers and strings */
  4233. return IS_UNDEF;
  4234. }
  4235. if (common_type == IS_UNDEF) {
  4236. common_type = Z_TYPE_P(cond_zv);
  4237. } else if (common_type != Z_TYPE_P(cond_zv)) {
  4238. /* Non-uniform case types */
  4239. return IS_UNDEF;
  4240. }
  4241. if (Z_TYPE_P(cond_zv) == IS_STRING
  4242. && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
  4243. /* Numeric strings cannot be compared with a simple hash lookup */
  4244. return IS_UNDEF;
  4245. }
  4246. }
  4247. return common_type;
  4248. }
  4249. static zend_bool should_use_jumptable(zend_ast_list *cases, zend_uchar jumptable_type) {
  4250. if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
  4251. return 0;
  4252. }
  4253. /* Thresholds are chosen based on when the average switch time for equidistributed
  4254. * input becomes smaller when using the jumptable optimization. */
  4255. if (jumptable_type == IS_LONG) {
  4256. return cases->children >= 5;
  4257. } else {
  4258. ZEND_ASSERT(jumptable_type == IS_STRING);
  4259. return cases->children >= 2;
  4260. }
  4261. }
  4262. void zend_compile_switch(zend_ast *ast) /* {{{ */
  4263. {
  4264. zend_ast *expr_ast = ast->child[0];
  4265. zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
  4266. uint32_t i;
  4267. zend_bool has_default_case = 0;
  4268. znode expr_node, case_node;
  4269. zend_op *opline;
  4270. uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch;
  4271. zend_uchar jumptable_type;
  4272. HashTable *jumptable = NULL;
  4273. zend_compile_expr(&expr_node, expr_ast);
  4274. zend_begin_loop(ZEND_FREE, &expr_node, 1);
  4275. case_node.op_type = IS_TMP_VAR;
  4276. case_node.u.op.var = get_temporary_variable(CG(active_op_array));
  4277. jumptable_type = determine_switch_jumptable_type(cases);
  4278. if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
  4279. znode jumptable_op;
  4280. ALLOC_HASHTABLE(jumptable);
  4281. zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
  4282. jumptable_op.op_type = IS_CONST;
  4283. ZVAL_ARR(&jumptable_op.u.constant, jumptable);
  4284. opline = zend_emit_op(NULL,
  4285. jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
  4286. &expr_node, &jumptable_op);
  4287. if (opline->op1_type == IS_CONST) {
  4288. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4289. }
  4290. opnum_switch = opline - CG(active_op_array)->opcodes;
  4291. }
  4292. jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
  4293. for (i = 0; i < cases->children; ++i) {
  4294. zend_ast *case_ast = cases->child[i];
  4295. zend_ast *cond_ast = case_ast->child[0];
  4296. znode cond_node;
  4297. if (!cond_ast) {
  4298. if (has_default_case) {
  4299. CG(zend_lineno) = case_ast->lineno;
  4300. zend_error_noreturn(E_COMPILE_ERROR,
  4301. "Switch statements may only contain one default clause");
  4302. }
  4303. has_default_case = 1;
  4304. continue;
  4305. }
  4306. zend_compile_expr(&cond_node, cond_ast);
  4307. if (expr_node.op_type == IS_CONST
  4308. && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
  4309. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  4310. } else if (expr_node.op_type == IS_CONST
  4311. && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
  4312. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
  4313. } else {
  4314. opline = zend_emit_op(NULL,
  4315. (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
  4316. &expr_node, &cond_node);
  4317. SET_NODE(opline->result, &case_node);
  4318. if (opline->op1_type == IS_CONST) {
  4319. Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
  4320. }
  4321. jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
  4322. }
  4323. }
  4324. opnum_default_jmp = zend_emit_jump(0);
  4325. for (i = 0; i < cases->children; ++i) {
  4326. zend_ast *case_ast = cases->child[i];
  4327. zend_ast *cond_ast = case_ast->child[0];
  4328. zend_ast *stmt_ast = case_ast->child[1];
  4329. if (cond_ast) {
  4330. zend_update_jump_target_to_next(jmpnz_opnums[i]);
  4331. if (jumptable) {
  4332. zval *cond_zv = zend_ast_get_zval(cond_ast);
  4333. zval jmp_target;
  4334. ZVAL_LONG(&jmp_target, get_next_op_number(CG(active_op_array)));
  4335. ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
  4336. if (Z_TYPE_P(cond_zv) == IS_LONG) {
  4337. zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
  4338. } else {
  4339. ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
  4340. zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
  4341. }
  4342. }
  4343. } else {
  4344. zend_update_jump_target_to_next(opnum_default_jmp);
  4345. if (jumptable) {
  4346. opline = &CG(active_op_array)->opcodes[opnum_switch];
  4347. opline->extended_value = get_next_op_number(CG(active_op_array));
  4348. }
  4349. }
  4350. zend_compile_stmt(stmt_ast);
  4351. }
  4352. if (!has_default_case) {
  4353. zend_update_jump_target_to_next(opnum_default_jmp);
  4354. if (jumptable) {
  4355. opline = &CG(active_op_array)->opcodes[opnum_switch];
  4356. opline->extended_value = get_next_op_number(CG(active_op_array));
  4357. }
  4358. }
  4359. zend_end_loop(get_next_op_number(CG(active_op_array)), &expr_node);
  4360. if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
  4361. /* don't use emit_op() to prevent automatic live-range construction */
  4362. opline = get_next_op(CG(active_op_array));
  4363. opline->opcode = ZEND_FREE;
  4364. SET_NODE(opline->op1, &expr_node);
  4365. } else if (expr_node.op_type == IS_CONST) {
  4366. zval_ptr_dtor_nogc(&expr_node.u.constant);
  4367. }
  4368. efree(jmpnz_opnums);
  4369. }
  4370. /* }}} */
  4371. void zend_compile_try(zend_ast *ast) /* {{{ */
  4372. {
  4373. zend_ast *try_ast = ast->child[0];
  4374. zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
  4375. zend_ast *finally_ast = ast->child[2];
  4376. uint32_t i, j;
  4377. zend_op *opline;
  4378. uint32_t try_catch_offset;
  4379. uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
  4380. uint32_t orig_fast_call_var = CG(context).fast_call_var;
  4381. uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
  4382. if (catches->children == 0 && !finally_ast) {
  4383. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
  4384. }
  4385. /* label: try { } must not be equal to try { label: } */
  4386. if (CG(context).labels) {
  4387. zend_label *label;
  4388. ZEND_HASH_REVERSE_FOREACH_PTR(CG(context).labels, label) {
  4389. if (label->opline_num == get_next_op_number(CG(active_op_array))) {
  4390. zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
  4391. }
  4392. break;
  4393. } ZEND_HASH_FOREACH_END();
  4394. }
  4395. try_catch_offset = zend_add_try_element(get_next_op_number(CG(active_op_array)));
  4396. if (finally_ast) {
  4397. zend_loop_var fast_call;
  4398. if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
  4399. CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
  4400. }
  4401. CG(context).fast_call_var = get_temporary_variable(CG(active_op_array));
  4402. /* Push FAST_CALL on unwind stack */
  4403. fast_call.opcode = ZEND_FAST_CALL;
  4404. fast_call.var_type = IS_TMP_VAR;
  4405. fast_call.var_num = CG(context).fast_call_var;
  4406. fast_call.u.try_catch_offset = try_catch_offset;
  4407. zend_stack_push(&CG(loop_var_stack), &fast_call);
  4408. }
  4409. CG(context).try_catch_offset = try_catch_offset;
  4410. zend_compile_stmt(try_ast);
  4411. if (catches->children != 0) {
  4412. jmp_opnums[0] = zend_emit_jump(0);
  4413. }
  4414. for (i = 0; i < catches->children; ++i) {
  4415. zend_ast *catch_ast = catches->child[i];
  4416. zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
  4417. zend_ast *var_ast = catch_ast->child[1];
  4418. zend_ast *stmt_ast = catch_ast->child[2];
  4419. zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_ast));
  4420. zend_bool is_last_catch = (i + 1 == catches->children);
  4421. uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
  4422. uint32_t opnum_catch;
  4423. CG(zend_lineno) = catch_ast->lineno;
  4424. for (j = 0; j < classes->children; j++) {
  4425. zend_ast *class_ast = classes->child[j];
  4426. zend_bool is_last_class = (j + 1 == classes->children);
  4427. if (!zend_is_const_default_class_ref(class_ast)) {
  4428. zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
  4429. }
  4430. opnum_catch = get_next_op_number(CG(active_op_array));
  4431. if (i == 0 && j == 0) {
  4432. CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
  4433. }
  4434. opline = get_next_op(CG(active_op_array));
  4435. opline->opcode = ZEND_CATCH;
  4436. opline->op1_type = IS_CONST;
  4437. opline->op1.constant = zend_add_class_name_literal(CG(active_op_array),
  4438. zend_resolve_class_name_ast(class_ast));
  4439. opline->extended_value = zend_alloc_cache_slot();
  4440. if (zend_string_equals_literal(var_name, "this")) {
  4441. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
  4442. }
  4443. opline->result_type = IS_CV;
  4444. opline->result.var = lookup_cv(CG(active_op_array), var_name);
  4445. if (is_last_catch && is_last_class) {
  4446. opline->extended_value |= ZEND_LAST_CATCH;
  4447. }
  4448. if (!is_last_class) {
  4449. jmp_multicatch[j] = zend_emit_jump(0);
  4450. opline = &CG(active_op_array)->opcodes[opnum_catch];
  4451. opline->op2.opline_num = get_next_op_number(CG(active_op_array));
  4452. }
  4453. }
  4454. for (j = 0; j < classes->children - 1; j++) {
  4455. zend_update_jump_target_to_next(jmp_multicatch[j]);
  4456. }
  4457. efree(jmp_multicatch);
  4458. zend_compile_stmt(stmt_ast);
  4459. if (!is_last_catch) {
  4460. jmp_opnums[i + 1] = zend_emit_jump(0);
  4461. }
  4462. opline = &CG(active_op_array)->opcodes[opnum_catch];
  4463. if (!is_last_catch) {
  4464. opline->op2.opline_num = get_next_op_number(CG(active_op_array));
  4465. }
  4466. }
  4467. for (i = 0; i < catches->children; ++i) {
  4468. zend_update_jump_target_to_next(jmp_opnums[i]);
  4469. }
  4470. if (finally_ast) {
  4471. zend_loop_var discard_exception;
  4472. uint32_t opnum_jmp = get_next_op_number(CG(active_op_array)) + 1;
  4473. /* Pop FAST_CALL from unwind stack */
  4474. zend_stack_del_top(&CG(loop_var_stack));
  4475. /* Push DISCARD_EXCEPTION on unwind stack */
  4476. discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
  4477. discard_exception.var_type = IS_TMP_VAR;
  4478. discard_exception.var_num = CG(context).fast_call_var;
  4479. zend_stack_push(&CG(loop_var_stack), &discard_exception);
  4480. CG(zend_lineno) = finally_ast->lineno;
  4481. opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
  4482. opline->op1.num = try_catch_offset;
  4483. opline->result_type = IS_TMP_VAR;
  4484. opline->result.var = CG(context).fast_call_var;
  4485. zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
  4486. zend_compile_stmt(finally_ast);
  4487. CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
  4488. CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
  4489. = get_next_op_number(CG(active_op_array));
  4490. opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
  4491. opline->op1_type = IS_TMP_VAR;
  4492. opline->op1.var = CG(context).fast_call_var;
  4493. opline->op2.num = orig_try_catch_offset;
  4494. zend_update_jump_target_to_next(opnum_jmp);
  4495. CG(context).fast_call_var = orig_fast_call_var;
  4496. /* Pop DISCARD_EXCEPTION from unwind stack */
  4497. zend_stack_del_top(&CG(loop_var_stack));
  4498. }
  4499. CG(context).try_catch_offset = orig_try_catch_offset;
  4500. efree(jmp_opnums);
  4501. }
  4502. /* }}} */
  4503. /* Encoding declarations must already be handled during parsing */
  4504. zend_bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
  4505. {
  4506. zend_ast_list *declares = zend_ast_get_list(ast);
  4507. uint32_t i;
  4508. for (i = 0; i < declares->children; ++i) {
  4509. zend_ast *declare_ast = declares->child[i];
  4510. zend_ast *name_ast = declare_ast->child[0];
  4511. zend_ast *value_ast = declare_ast->child[1];
  4512. zend_string *name = zend_ast_get_str(name_ast);
  4513. if (zend_string_equals_literal_ci(name, "encoding")) {
  4514. if (value_ast->kind != ZEND_AST_ZVAL) {
  4515. zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
  4516. return 0;
  4517. }
  4518. if (CG(multibyte)) {
  4519. zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
  4520. const zend_encoding *new_encoding, *old_encoding;
  4521. zend_encoding_filter old_input_filter;
  4522. CG(encoding_declared) = 1;
  4523. new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
  4524. if (!new_encoding) {
  4525. zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
  4526. } else {
  4527. old_input_filter = LANG_SCNG(input_filter);
  4528. old_encoding = LANG_SCNG(script_encoding);
  4529. zend_multibyte_set_filter(new_encoding);
  4530. /* need to re-scan if input filter changed */
  4531. if (old_input_filter != LANG_SCNG(input_filter) ||
  4532. (old_input_filter && new_encoding != old_encoding)) {
  4533. zend_multibyte_yyinput_again(old_input_filter, old_encoding);
  4534. }
  4535. }
  4536. zend_string_release_ex(encoding_name, 0);
  4537. } else {
  4538. zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
  4539. "Zend multibyte feature is turned off by settings");
  4540. }
  4541. }
  4542. }
  4543. return 1;
  4544. }
  4545. /* }}} */
  4546. static int zend_declare_is_first_statement(zend_ast *ast) /* {{{ */
  4547. {
  4548. uint32_t i = 0;
  4549. zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
  4550. /* Check to see if this declare is preceded only by declare statements */
  4551. while (i < file_ast->children) {
  4552. if (file_ast->child[i] == ast) {
  4553. return SUCCESS;
  4554. } else if (file_ast->child[i] == NULL) {
  4555. /* Empty statements are not allowed prior to a declare */
  4556. return FAILURE;
  4557. } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
  4558. /* declares can only be preceded by other declares */
  4559. return FAILURE;
  4560. }
  4561. i++;
  4562. }
  4563. return FAILURE;
  4564. }
  4565. /* }}} */
  4566. void zend_compile_declare(zend_ast *ast) /* {{{ */
  4567. {
  4568. zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
  4569. zend_ast *stmt_ast = ast->child[1];
  4570. zend_declarables orig_declarables = FC(declarables);
  4571. uint32_t i;
  4572. for (i = 0; i < declares->children; ++i) {
  4573. zend_ast *declare_ast = declares->child[i];
  4574. zend_ast *name_ast = declare_ast->child[0];
  4575. zend_ast *value_ast = declare_ast->child[1];
  4576. zend_string *name = zend_ast_get_str(name_ast);
  4577. if (value_ast->kind != ZEND_AST_ZVAL) {
  4578. zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
  4579. }
  4580. if (zend_string_equals_literal_ci(name, "ticks")) {
  4581. zval value_zv;
  4582. zend_const_expr_to_zval(&value_zv, value_ast);
  4583. FC(declarables).ticks = zval_get_long(&value_zv);
  4584. zval_ptr_dtor_nogc(&value_zv);
  4585. } else if (zend_string_equals_literal_ci(name, "encoding")) {
  4586. if (FAILURE == zend_declare_is_first_statement(ast)) {
  4587. zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
  4588. "the very first statement in the script");
  4589. }
  4590. } else if (zend_string_equals_literal_ci(name, "strict_types")) {
  4591. zval value_zv;
  4592. if (FAILURE == zend_declare_is_first_statement(ast)) {
  4593. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
  4594. "the very first statement in the script");
  4595. }
  4596. if (ast->child[1] != NULL) {
  4597. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
  4598. "use block mode");
  4599. }
  4600. zend_const_expr_to_zval(&value_zv, value_ast);
  4601. if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
  4602. zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
  4603. }
  4604. if (Z_LVAL(value_zv) == 1) {
  4605. CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
  4606. }
  4607. } else {
  4608. zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
  4609. }
  4610. }
  4611. if (stmt_ast) {
  4612. zend_compile_stmt(stmt_ast);
  4613. FC(declarables) = orig_declarables;
  4614. }
  4615. }
  4616. /* }}} */
  4617. void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
  4618. {
  4619. zend_ast_list *list = zend_ast_get_list(ast);
  4620. uint32_t i;
  4621. for (i = 0; i < list->children; ++i) {
  4622. zend_compile_stmt(list->child[i]);
  4623. }
  4624. }
  4625. /* }}} */
  4626. ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
  4627. {
  4628. uint32_t i, n;
  4629. func->common.arg_flags[0] = 0;
  4630. func->common.arg_flags[1] = 0;
  4631. func->common.arg_flags[2] = 0;
  4632. if (func->common.arg_info) {
  4633. n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
  4634. i = 0;
  4635. while (i < n) {
  4636. ZEND_SET_ARG_FLAG(func, i + 1, func->common.arg_info[i].pass_by_reference);
  4637. i++;
  4638. }
  4639. if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_VARIADIC && func->common.arg_info[i].pass_by_reference)) {
  4640. uint32_t pass_by_reference = func->common.arg_info[i].pass_by_reference;
  4641. while (i < MAX_ARG_FLAG_NUM) {
  4642. ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
  4643. i++;
  4644. }
  4645. }
  4646. }
  4647. }
  4648. /* }}} */
  4649. static void zend_compile_typename(zend_ast *ast, zend_arg_info *arg_info, zend_bool allow_null) /* {{{ */
  4650. {
  4651. if (ast->kind == ZEND_AST_TYPE) {
  4652. arg_info->type = ZEND_TYPE_ENCODE(ast->attr, allow_null);
  4653. } else {
  4654. zend_string *class_name = zend_ast_get_str(ast);
  4655. zend_uchar type = zend_lookup_builtin_type_by_name(class_name);
  4656. if (type != 0) {
  4657. if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
  4658. zend_error_noreturn(E_COMPILE_ERROR,
  4659. "Type declaration '%s' must be unqualified",
  4660. ZSTR_VAL(zend_string_tolower(class_name)));
  4661. }
  4662. arg_info->type = ZEND_TYPE_ENCODE(type, allow_null);
  4663. } else {
  4664. uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
  4665. if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
  4666. class_name = zend_resolve_class_name_ast(ast);
  4667. zend_assert_valid_class_name(class_name);
  4668. } else {
  4669. zend_ensure_valid_class_fetch_type(fetch_type);
  4670. zend_string_addref(class_name);
  4671. }
  4672. arg_info->type = ZEND_TYPE_ENCODE_CLASS(class_name, allow_null);
  4673. }
  4674. }
  4675. }
  4676. /* }}} */
  4677. void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
  4678. {
  4679. zend_ast_list *list = zend_ast_get_list(ast);
  4680. uint32_t i;
  4681. zend_op_array *op_array = CG(active_op_array);
  4682. zend_arg_info *arg_infos;
  4683. if (return_type_ast) {
  4684. zend_bool allow_null = 0;
  4685. /* Use op_array->arg_info[-1] for return type */
  4686. arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
  4687. arg_infos->name = NULL;
  4688. arg_infos->pass_by_reference = (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  4689. arg_infos->is_variadic = 0;
  4690. arg_infos->type = 0;
  4691. if (return_type_ast->attr & ZEND_TYPE_NULLABLE) {
  4692. allow_null = 1;
  4693. return_type_ast->attr &= ~ZEND_TYPE_NULLABLE;
  4694. }
  4695. zend_compile_typename(return_type_ast, arg_infos, allow_null);
  4696. if (ZEND_TYPE_CODE(arg_infos->type) == IS_VOID && ZEND_TYPE_ALLOW_NULL(arg_infos->type)) {
  4697. zend_error_noreturn(E_COMPILE_ERROR, "Void type cannot be nullable");
  4698. }
  4699. arg_infos++;
  4700. op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  4701. } else {
  4702. if (list->children == 0) {
  4703. return;
  4704. }
  4705. arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
  4706. }
  4707. for (i = 0; i < list->children; ++i) {
  4708. zend_ast *param_ast = list->child[i];
  4709. zend_ast *type_ast = param_ast->child[0];
  4710. zend_ast *var_ast = param_ast->child[1];
  4711. zend_ast *default_ast = param_ast->child[2];
  4712. zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
  4713. zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
  4714. zend_bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
  4715. znode var_node, default_node;
  4716. zend_uchar opcode;
  4717. zend_op *opline;
  4718. zend_arg_info *arg_info;
  4719. if (zend_is_auto_global(name)) {
  4720. zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
  4721. ZSTR_VAL(name));
  4722. }
  4723. var_node.op_type = IS_CV;
  4724. var_node.u.op.var = lookup_cv(CG(active_op_array), name);
  4725. if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
  4726. zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
  4727. ZSTR_VAL(name));
  4728. } else if (zend_string_equals_literal(name, "this")) {
  4729. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
  4730. }
  4731. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  4732. zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
  4733. }
  4734. if (is_variadic) {
  4735. opcode = ZEND_RECV_VARIADIC;
  4736. default_node.op_type = IS_UNUSED;
  4737. op_array->fn_flags |= ZEND_ACC_VARIADIC;
  4738. if (default_ast) {
  4739. zend_error_noreturn(E_COMPILE_ERROR,
  4740. "Variadic parameter cannot have a default value");
  4741. }
  4742. } else if (default_ast) {
  4743. /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
  4744. uint32_t cops = CG(compiler_options);
  4745. CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
  4746. opcode = ZEND_RECV_INIT;
  4747. default_node.op_type = IS_CONST;
  4748. zend_const_expr_to_zval(&default_node.u.constant, default_ast);
  4749. CG(compiler_options) = cops;
  4750. } else {
  4751. opcode = ZEND_RECV;
  4752. default_node.op_type = IS_UNUSED;
  4753. op_array->required_num_args = i + 1;
  4754. }
  4755. opline = zend_emit_op(NULL, opcode, NULL, &default_node);
  4756. SET_NODE(opline->result, &var_node);
  4757. opline->op1.num = i + 1;
  4758. arg_info = &arg_infos[i];
  4759. arg_info->name = zend_string_copy(name);
  4760. arg_info->pass_by_reference = is_ref;
  4761. arg_info->is_variadic = is_variadic;
  4762. /* TODO: Keep compatibility, but may be better reset "allow_null" ??? */
  4763. arg_info->type = ZEND_TYPE_ENCODE(0, 1);
  4764. if (type_ast) {
  4765. zend_bool allow_null;
  4766. zend_bool has_null_default = default_ast
  4767. && (Z_TYPE(default_node.u.constant) == IS_NULL
  4768. || (Z_TYPE(default_node.u.constant) == IS_CONSTANT_AST
  4769. && Z_ASTVAL(default_node.u.constant)->kind == ZEND_AST_CONSTANT
  4770. && strcasecmp(ZSTR_VAL(zend_ast_get_constant_name(Z_ASTVAL(default_node.u.constant))), "NULL") == 0));
  4771. zend_bool is_explicitly_nullable = (type_ast->attr & ZEND_TYPE_NULLABLE) == ZEND_TYPE_NULLABLE;
  4772. op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  4773. allow_null = has_null_default || is_explicitly_nullable;
  4774. type_ast->attr &= ~ZEND_TYPE_NULLABLE;
  4775. zend_compile_typename(type_ast, arg_info, allow_null);
  4776. if (ZEND_TYPE_CODE(arg_info->type) == IS_VOID) {
  4777. zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
  4778. }
  4779. if (type_ast->kind == ZEND_AST_TYPE) {
  4780. if (ZEND_TYPE_CODE(arg_info->type) == IS_ARRAY) {
  4781. if (default_ast && !has_null_default
  4782. && Z_TYPE(default_node.u.constant) != IS_ARRAY
  4783. && Z_TYPE(default_node.u.constant) != IS_CONSTANT_AST
  4784. ) {
  4785. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4786. "with array type can only be an array or NULL");
  4787. }
  4788. } else if (ZEND_TYPE_CODE(arg_info->type) == IS_CALLABLE && default_ast) {
  4789. if (!has_null_default && Z_TYPE(default_node.u.constant) != IS_CONSTANT_AST) {
  4790. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4791. "with callable type can only be NULL");
  4792. }
  4793. }
  4794. } else {
  4795. if (default_ast && !has_null_default && Z_TYPE(default_node.u.constant) != IS_CONSTANT_AST) {
  4796. if (ZEND_TYPE_IS_CLASS(arg_info->type)) {
  4797. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4798. "with a class type can only be NULL");
  4799. } else switch (ZEND_TYPE_CODE(arg_info->type)) {
  4800. case IS_DOUBLE:
  4801. if (Z_TYPE(default_node.u.constant) != IS_DOUBLE && Z_TYPE(default_node.u.constant) != IS_LONG) {
  4802. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4803. "with a float type can only be float, integer, or NULL");
  4804. }
  4805. break;
  4806. case IS_ITERABLE:
  4807. if (Z_TYPE(default_node.u.constant) != IS_ARRAY) {
  4808. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4809. "with iterable type can only be an array or NULL");
  4810. }
  4811. break;
  4812. case IS_OBJECT:
  4813. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4814. "with an object type can only be NULL");
  4815. break;
  4816. default:
  4817. if (!ZEND_SAME_FAKE_TYPE(ZEND_TYPE_CODE(arg_info->type), Z_TYPE(default_node.u.constant))) {
  4818. zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
  4819. "with a %s type can only be %s or NULL",
  4820. zend_get_type_by_const(ZEND_TYPE_CODE(arg_info->type)), zend_get_type_by_const(ZEND_TYPE_CODE(arg_info->type)));
  4821. }
  4822. break;
  4823. }
  4824. }
  4825. }
  4826. /* Allocate cache slot to speed-up run-time class resolution */
  4827. if (opline->opcode == ZEND_RECV_INIT) {
  4828. if (ZEND_TYPE_IS_CLASS(arg_info->type)) {
  4829. opline->extended_value = zend_alloc_cache_slot();
  4830. }
  4831. } else {
  4832. if (ZEND_TYPE_IS_CLASS(arg_info->type)) {
  4833. opline->op2.num = op_array->cache_size;
  4834. op_array->cache_size += sizeof(void*);
  4835. } else {
  4836. opline->op2.num = -1;
  4837. }
  4838. }
  4839. } else {
  4840. if (opline->opcode != ZEND_RECV_INIT) {
  4841. opline->op2.num = -1;
  4842. }
  4843. }
  4844. }
  4845. /* These are assigned at the end to avoid uninitialized memory in case of an error */
  4846. op_array->num_args = list->children;
  4847. op_array->arg_info = arg_infos;
  4848. /* Don't count the variadic argument */
  4849. if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
  4850. op_array->num_args--;
  4851. }
  4852. zend_set_function_arg_flags((zend_function*)op_array);
  4853. }
  4854. /* }}} */
  4855. static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
  4856. {
  4857. zend_ast_list *list = zend_ast_get_list(uses_ast);
  4858. uint32_t i;
  4859. if (!list->children) {
  4860. return;
  4861. }
  4862. if (!op_array->static_variables) {
  4863. op_array->static_variables = zend_new_array(8);
  4864. }
  4865. if (GC_REFCOUNT(op_array->static_variables) > 1) {
  4866. if (!(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
  4867. GC_DELREF(op_array->static_variables);
  4868. }
  4869. op_array->static_variables = zend_array_dup(op_array->static_variables);
  4870. }
  4871. for (i = 0; i < list->children; ++i) {
  4872. zend_ast *var_name_ast = list->child[i];
  4873. zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
  4874. uint32_t by_ref = var_name_ast->attr;
  4875. zend_op *opline;
  4876. zval *value;
  4877. if (zend_string_equals_literal(var_name, "this")) {
  4878. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
  4879. }
  4880. if (zend_is_auto_global(var_name)) {
  4881. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
  4882. }
  4883. value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
  4884. if (!value) {
  4885. zend_error_noreturn(E_COMPILE_ERROR,
  4886. "Cannot use variable $%s twice", ZSTR_VAL(var_name));
  4887. }
  4888. opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
  4889. opline->op2_type = IS_CV;
  4890. opline->op2.var = lookup_cv(CG(active_op_array), var_name);
  4891. opline->extended_value = (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | by_ref;
  4892. }
  4893. }
  4894. /* }}} */
  4895. void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
  4896. {
  4897. zend_op_array *op_array = CG(active_op_array);
  4898. zend_ast_list *list = zend_ast_get_list(ast);
  4899. uint32_t i;
  4900. for (i = 0; i < list->children; ++i) {
  4901. zend_ast *var_ast = list->child[i];
  4902. zend_string *var_name = zend_ast_get_str(var_ast);
  4903. uint32_t by_ref = var_ast->attr;
  4904. zval zv;
  4905. ZVAL_NULL(&zv);
  4906. {
  4907. int i;
  4908. for (i = 0; i < op_array->last_var; i++) {
  4909. if (zend_string_equals(op_array->vars[i], var_name)) {
  4910. zend_error_noreturn(E_COMPILE_ERROR,
  4911. "Cannot use lexical variable $%s as a parameter name", ZSTR_VAL(var_name));
  4912. }
  4913. }
  4914. }
  4915. zend_compile_static_var_common(var_ast, &zv, by_ref);
  4916. }
  4917. }
  4918. /* }}} */
  4919. void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_bool has_body) /* {{{ */
  4920. {
  4921. zend_class_entry *ce = CG(active_class_entry);
  4922. zend_bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
  4923. zend_bool in_trait = (ce->ce_flags & ZEND_ACC_TRAIT) != 0;
  4924. zend_bool is_public = (op_array->fn_flags & ZEND_ACC_PUBLIC) != 0;
  4925. zend_bool is_static = (op_array->fn_flags & ZEND_ACC_STATIC) != 0;
  4926. zend_string *lcname;
  4927. if (in_interface) {
  4928. if (!is_public || (op_array->fn_flags & (ZEND_ACC_FINAL|ZEND_ACC_ABSTRACT))) {
  4929. zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
  4930. "%s::%s() must be omitted", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4931. }
  4932. op_array->fn_flags |= ZEND_ACC_ABSTRACT;
  4933. }
  4934. if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
  4935. if (op_array->fn_flags & ZEND_ACC_PRIVATE) {
  4936. zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
  4937. in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4938. }
  4939. if (has_body) {
  4940. zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
  4941. in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4942. }
  4943. ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  4944. } else if (!has_body) {
  4945. zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
  4946. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4947. }
  4948. op_array->scope = ce;
  4949. op_array->function_name = zend_string_copy(name);
  4950. lcname = zend_string_tolower(name);
  4951. lcname = zend_new_interned_string(lcname);
  4952. if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
  4953. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
  4954. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  4955. }
  4956. if (in_interface) {
  4957. if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
  4958. /* pass */
  4959. } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
  4960. if (!is_public || is_static) {
  4961. zend_error(E_WARNING, "The magic method __call() must have "
  4962. "public visibility and cannot be static");
  4963. }
  4964. } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
  4965. if (!is_public || !is_static) {
  4966. zend_error(E_WARNING, "The magic method __callStatic() must have "
  4967. "public visibility and be static");
  4968. }
  4969. } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
  4970. if (!is_public || is_static) {
  4971. zend_error(E_WARNING, "The magic method __get() must have "
  4972. "public visibility and cannot be static");
  4973. }
  4974. } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
  4975. if (!is_public || is_static) {
  4976. zend_error(E_WARNING, "The magic method __set() must have "
  4977. "public visibility and cannot be static");
  4978. }
  4979. } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
  4980. if (!is_public || is_static) {
  4981. zend_error(E_WARNING, "The magic method __unset() must have "
  4982. "public visibility and cannot be static");
  4983. }
  4984. } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
  4985. if (!is_public || is_static) {
  4986. zend_error(E_WARNING, "The magic method __isset() must have "
  4987. "public visibility and cannot be static");
  4988. }
  4989. } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
  4990. if (!is_public || is_static) {
  4991. zend_error(E_WARNING, "The magic method __toString() must have "
  4992. "public visibility and cannot be static");
  4993. }
  4994. } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
  4995. if (!is_public || is_static) {
  4996. zend_error(E_WARNING, "The magic method __invoke() must have "
  4997. "public visibility and cannot be static");
  4998. }
  4999. } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
  5000. if (!is_public || is_static) {
  5001. zend_error(E_WARNING, "The magic method __debugInfo() must have "
  5002. "public visibility and cannot be static");
  5003. }
  5004. }
  5005. } else {
  5006. if (!in_trait && zend_string_equals_ci(lcname, ce->name)) {
  5007. if (!ce->constructor) {
  5008. ce->constructor = (zend_function *) op_array;
  5009. }
  5010. } else if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
  5011. if (!is_static) {
  5012. op_array->fn_flags |= ZEND_ACC_ALLOW_STATIC;
  5013. }
  5014. } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  5015. ce->constructor = (zend_function *) op_array;
  5016. } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
  5017. ce->destructor = (zend_function *) op_array;
  5018. } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
  5019. ce->clone = (zend_function *) op_array;
  5020. } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
  5021. if (!is_public || is_static) {
  5022. zend_error(E_WARNING, "The magic method __call() must have "
  5023. "public visibility and cannot be static");
  5024. }
  5025. ce->__call = (zend_function *) op_array;
  5026. } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
  5027. if (!is_public || !is_static) {
  5028. zend_error(E_WARNING, "The magic method __callStatic() must have "
  5029. "public visibility and be static");
  5030. }
  5031. ce->__callstatic = (zend_function *) op_array;
  5032. } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
  5033. if (!is_public || is_static) {
  5034. zend_error(E_WARNING, "The magic method __get() must have "
  5035. "public visibility and cannot be static");
  5036. }
  5037. ce->__get = (zend_function *) op_array;
  5038. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5039. } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
  5040. if (!is_public || is_static) {
  5041. zend_error(E_WARNING, "The magic method __set() must have "
  5042. "public visibility and cannot be static");
  5043. }
  5044. ce->__set = (zend_function *) op_array;
  5045. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5046. } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
  5047. if (!is_public || is_static) {
  5048. zend_error(E_WARNING, "The magic method __unset() must have "
  5049. "public visibility and cannot be static");
  5050. }
  5051. ce->__unset = (zend_function *) op_array;
  5052. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5053. } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
  5054. if (!is_public || is_static) {
  5055. zend_error(E_WARNING, "The magic method __isset() must have "
  5056. "public visibility and cannot be static");
  5057. }
  5058. ce->__isset = (zend_function *) op_array;
  5059. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  5060. } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
  5061. if (!is_public || is_static) {
  5062. zend_error(E_WARNING, "The magic method __toString() must have "
  5063. "public visibility and cannot be static");
  5064. }
  5065. ce->__tostring = (zend_function *) op_array;
  5066. } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
  5067. if (!is_public || is_static) {
  5068. zend_error(E_WARNING, "The magic method __invoke() must have "
  5069. "public visibility and cannot be static");
  5070. }
  5071. } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
  5072. if (!is_public || is_static) {
  5073. zend_error(E_WARNING, "The magic method __debugInfo() must have "
  5074. "public visibility and cannot be static");
  5075. }
  5076. ce->__debugInfo = (zend_function *) op_array;
  5077. } else if (!is_static) {
  5078. op_array->fn_flags |= ZEND_ACC_ALLOW_STATIC;
  5079. }
  5080. }
  5081. zend_string_release_ex(lcname, 0);
  5082. }
  5083. /* }}} */
  5084. static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl) /* {{{ */
  5085. {
  5086. zend_ast *params_ast = decl->child[0];
  5087. zend_string *unqualified_name, *name, *lcname, *key;
  5088. zend_op *opline;
  5089. unqualified_name = decl->name;
  5090. op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
  5091. lcname = zend_string_tolower(name);
  5092. if (FC(imports_function)) {
  5093. zend_string *import_name = zend_hash_find_ptr_lc(
  5094. FC(imports_function), ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name));
  5095. if (import_name && !zend_string_equals_ci(lcname, import_name)) {
  5096. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
  5097. "because the name is already in use", ZSTR_VAL(name));
  5098. }
  5099. }
  5100. if (zend_string_equals_literal(lcname, ZEND_AUTOLOAD_FUNC_NAME)) {
  5101. if (zend_ast_get_list(params_ast)->children != 1) {
  5102. zend_error_noreturn(E_COMPILE_ERROR, "%s() must take exactly 1 argument",
  5103. ZEND_AUTOLOAD_FUNC_NAME);
  5104. }
  5105. zend_error(E_DEPRECATED, "__autoload() is deprecated, use spl_autoload_register() instead");
  5106. }
  5107. if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
  5108. zend_error(E_DEPRECATED,
  5109. "Defining a custom assert() function is deprecated, "
  5110. "as the function has special semantics");
  5111. }
  5112. key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
  5113. zend_hash_update_ptr(CG(function_table), key, op_array);
  5114. zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
  5115. if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
  5116. opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
  5117. opline->op1_type = IS_CONST;
  5118. LITERAL_STR(opline->op1, key);
  5119. } else {
  5120. opline = get_next_op(CG(active_op_array));
  5121. opline->opcode = ZEND_DECLARE_FUNCTION;
  5122. opline->op1_type = IS_CONST;
  5123. LITERAL_STR(opline->op1, zend_string_copy(lcname));
  5124. /* RTD key is placed after lcname literal in op1 */
  5125. zend_add_literal_string(CG(active_op_array), &key);
  5126. }
  5127. zend_string_release_ex(lcname, 0);
  5128. }
  5129. /* }}} */
  5130. void zend_compile_func_decl(znode *result, zend_ast *ast) /* {{{ */
  5131. {
  5132. zend_ast_decl *decl = (zend_ast_decl *) ast;
  5133. zend_ast *params_ast = decl->child[0];
  5134. zend_ast *uses_ast = decl->child[1];
  5135. zend_ast *stmt_ast = decl->child[2];
  5136. zend_ast *return_type_ast = decl->child[3];
  5137. zend_bool is_method = decl->kind == ZEND_AST_METHOD;
  5138. zend_op_array *orig_op_array = CG(active_op_array);
  5139. zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
  5140. zend_oparray_context orig_oparray_context;
  5141. init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
  5142. op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
  5143. op_array->fn_flags |= decl->flags;
  5144. op_array->line_start = decl->start_lineno;
  5145. op_array->line_end = decl->end_lineno;
  5146. if (decl->doc_comment) {
  5147. op_array->doc_comment = zend_string_copy(decl->doc_comment);
  5148. }
  5149. if (decl->kind == ZEND_AST_CLOSURE) {
  5150. op_array->fn_flags |= ZEND_ACC_CLOSURE;
  5151. }
  5152. if (is_method) {
  5153. zend_bool has_body = stmt_ast != NULL;
  5154. zend_begin_method_decl(op_array, decl->name, has_body);
  5155. } else {
  5156. zend_begin_func_decl(result, op_array, decl);
  5157. if (uses_ast) {
  5158. zend_compile_closure_binding(result, op_array, uses_ast);
  5159. }
  5160. }
  5161. CG(active_op_array) = op_array;
  5162. zend_oparray_context_begin(&orig_oparray_context);
  5163. if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
  5164. zend_op *opline_ext = zend_emit_op(NULL, ZEND_EXT_NOP, NULL, NULL);
  5165. opline_ext->lineno = decl->start_lineno;
  5166. }
  5167. {
  5168. /* Push a separator to the loop variable stack */
  5169. zend_loop_var dummy_var;
  5170. dummy_var.opcode = ZEND_RETURN;
  5171. zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
  5172. }
  5173. zend_compile_params(params_ast, return_type_ast);
  5174. if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
  5175. zend_mark_function_as_generator();
  5176. zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
  5177. }
  5178. if (uses_ast) {
  5179. zend_compile_closure_uses(uses_ast);
  5180. }
  5181. zend_compile_stmt(stmt_ast);
  5182. if (is_method) {
  5183. zend_check_magic_method_implementation(
  5184. CG(active_class_entry), (zend_function *) op_array, E_COMPILE_ERROR);
  5185. }
  5186. /* put the implicit return on the really last line */
  5187. CG(zend_lineno) = decl->end_lineno;
  5188. zend_do_extended_info();
  5189. zend_emit_final_return(0);
  5190. pass_two(CG(active_op_array));
  5191. zend_oparray_context_end(&orig_oparray_context);
  5192. /* Pop the loop variable stack separator */
  5193. zend_stack_del_top(&CG(loop_var_stack));
  5194. CG(active_op_array) = orig_op_array;
  5195. }
  5196. /* }}} */
  5197. void zend_compile_prop_decl(zend_ast *ast) /* {{{ */
  5198. {
  5199. zend_ast_list *list = zend_ast_get_list(ast);
  5200. uint32_t flags = list->attr;
  5201. zend_class_entry *ce = CG(active_class_entry);
  5202. uint32_t i, children = list->children;
  5203. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  5204. zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include member variables");
  5205. }
  5206. if (flags & ZEND_ACC_ABSTRACT) {
  5207. zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
  5208. }
  5209. for (i = 0; i < children; ++i) {
  5210. zend_ast *prop_ast = list->child[i];
  5211. zend_ast *name_ast = prop_ast->child[0];
  5212. zend_ast *value_ast = prop_ast->child[1];
  5213. zend_ast *doc_comment_ast = prop_ast->child[2];
  5214. zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
  5215. zend_string *doc_comment = NULL;
  5216. zval value_zv;
  5217. /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
  5218. if (doc_comment_ast) {
  5219. doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
  5220. }
  5221. if (flags & ZEND_ACC_FINAL) {
  5222. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
  5223. "the final modifier is allowed only for methods and classes",
  5224. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5225. }
  5226. if (zend_hash_exists(&ce->properties_info, name)) {
  5227. zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
  5228. ZSTR_VAL(ce->name), ZSTR_VAL(name));
  5229. }
  5230. if (value_ast) {
  5231. zend_const_expr_to_zval(&value_zv, value_ast);
  5232. } else {
  5233. ZVAL_NULL(&value_zv);
  5234. }
  5235. zend_declare_property_ex(ce, name, &value_zv, flags, doc_comment);
  5236. }
  5237. }
  5238. /* }}} */
  5239. void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
  5240. {
  5241. zend_ast_list *list = zend_ast_get_list(ast);
  5242. zend_class_entry *ce = CG(active_class_entry);
  5243. uint32_t i;
  5244. if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  5245. zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
  5246. return;
  5247. }
  5248. for (i = 0; i < list->children; ++i) {
  5249. zend_ast *const_ast = list->child[i];
  5250. zend_ast *name_ast = const_ast->child[0];
  5251. zend_ast *value_ast = const_ast->child[1];
  5252. zend_ast *doc_comment_ast = const_ast->child[2];
  5253. zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
  5254. zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
  5255. zval value_zv;
  5256. if (UNEXPECTED(ast->attr & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
  5257. if (ast->attr & ZEND_ACC_STATIC) {
  5258. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as constant modifier");
  5259. } else if (ast->attr & ZEND_ACC_ABSTRACT) {
  5260. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as constant modifier");
  5261. } else if (ast->attr & ZEND_ACC_FINAL) {
  5262. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as constant modifier");
  5263. }
  5264. }
  5265. zend_const_expr_to_zval(&value_zv, value_ast);
  5266. zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr, doc_comment);
  5267. }
  5268. }
  5269. /* }}} */
  5270. static void zend_compile_method_ref(zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
  5271. {
  5272. zend_ast *class_ast = ast->child[0];
  5273. zend_ast *method_ast = ast->child[1];
  5274. method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
  5275. if (class_ast) {
  5276. method_ref->class_name = zend_resolve_class_name_ast(class_ast);
  5277. } else {
  5278. method_ref->class_name = NULL;
  5279. }
  5280. }
  5281. /* }}} */
  5282. static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
  5283. {
  5284. zend_ast *method_ref_ast = ast->child[0];
  5285. zend_ast *insteadof_ast = ast->child[1];
  5286. zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
  5287. uint32_t i;
  5288. zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
  5289. zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
  5290. precedence->num_excludes = insteadof_list->children;
  5291. for (i = 0; i < insteadof_list->children; ++i) {
  5292. zend_ast *name_ast = insteadof_list->child[i];
  5293. precedence->exclude_class_names[i] = zend_resolve_class_name_ast(name_ast);
  5294. }
  5295. zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
  5296. }
  5297. /* }}} */
  5298. static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
  5299. {
  5300. zend_ast *method_ref_ast = ast->child[0];
  5301. zend_ast *alias_ast = ast->child[1];
  5302. uint32_t modifiers = ast->attr;
  5303. zend_trait_alias *alias;
  5304. if (modifiers == ZEND_ACC_STATIC) {
  5305. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as method modifier");
  5306. } else if (modifiers == ZEND_ACC_ABSTRACT) {
  5307. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as method modifier");
  5308. } else if (modifiers == ZEND_ACC_FINAL) {
  5309. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as method modifier");
  5310. }
  5311. alias = emalloc(sizeof(zend_trait_alias));
  5312. zend_compile_method_ref(method_ref_ast, &alias->trait_method);
  5313. alias->modifiers = modifiers;
  5314. if (alias_ast) {
  5315. alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
  5316. } else {
  5317. alias->alias = NULL;
  5318. }
  5319. zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
  5320. }
  5321. /* }}} */
  5322. void zend_compile_use_trait(zend_ast *ast) /* {{{ */
  5323. {
  5324. zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
  5325. zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
  5326. zend_class_entry *ce = CG(active_class_entry);
  5327. zend_op *opline;
  5328. uint32_t i;
  5329. for (i = 0; i < traits->children; ++i) {
  5330. zend_ast *trait_ast = traits->child[i];
  5331. zend_string *name = zend_ast_get_str(trait_ast);
  5332. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  5333. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
  5334. "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
  5335. }
  5336. switch (zend_get_class_fetch_type(name)) {
  5337. case ZEND_FETCH_CLASS_SELF:
  5338. case ZEND_FETCH_CLASS_PARENT:
  5339. case ZEND_FETCH_CLASS_STATIC:
  5340. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as trait name "
  5341. "as it is reserved", ZSTR_VAL(name));
  5342. break;
  5343. }
  5344. opline = get_next_op(CG(active_op_array));
  5345. opline->opcode = ZEND_ADD_TRAIT;
  5346. SET_NODE(opline->op1, &FC(implementing_class));
  5347. opline->op2_type = IS_CONST;
  5348. opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
  5349. zend_resolve_class_name_ast(trait_ast));
  5350. ce->num_traits++;
  5351. }
  5352. if (!adaptations) {
  5353. return;
  5354. }
  5355. for (i = 0; i < adaptations->children; ++i) {
  5356. zend_ast *adaptation_ast = adaptations->child[i];
  5357. switch (adaptation_ast->kind) {
  5358. case ZEND_AST_TRAIT_PRECEDENCE:
  5359. zend_compile_trait_precedence(adaptation_ast);
  5360. break;
  5361. case ZEND_AST_TRAIT_ALIAS:
  5362. zend_compile_trait_alias(adaptation_ast);
  5363. break;
  5364. EMPTY_SWITCH_DEFAULT_CASE()
  5365. }
  5366. }
  5367. }
  5368. /* }}} */
  5369. void zend_compile_implements(znode *class_node, zend_ast *ast) /* {{{ */
  5370. {
  5371. zend_ast_list *list = zend_ast_get_list(ast);
  5372. uint32_t i;
  5373. for (i = 0; i < list->children; ++i) {
  5374. zend_ast *class_ast = list->child[i];
  5375. zend_string *name = zend_ast_get_str(class_ast);
  5376. zend_op *opline;
  5377. if (!zend_is_const_default_class_ref(class_ast)) {
  5378. zend_error_noreturn(E_COMPILE_ERROR,
  5379. "Cannot use '%s' as interface name as it is reserved", ZSTR_VAL(name));
  5380. }
  5381. opline = zend_emit_op(NULL, ZEND_ADD_INTERFACE, class_node, NULL);
  5382. opline->op2_type = IS_CONST;
  5383. opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
  5384. zend_resolve_class_name_ast(class_ast));
  5385. CG(active_class_entry)->num_interfaces++;
  5386. }
  5387. }
  5388. /* }}} */
  5389. static zend_string *zend_generate_anon_class_name(unsigned char *lex_pos) /* {{{ */
  5390. {
  5391. zend_string *result;
  5392. char char_pos_buf[32];
  5393. size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
  5394. zend_string *filename = CG(active_op_array)->filename;
  5395. /* NULL, name length, filename length, last accepting char position length */
  5396. result = zend_string_alloc(sizeof("class@anonymous") + ZSTR_LEN(filename) + char_pos_len, 0);
  5397. sprintf(ZSTR_VAL(result), "class@anonymous%c%s%s", '\0', ZSTR_VAL(filename), char_pos_buf);
  5398. return zend_new_interned_string(result);
  5399. }
  5400. /* }}} */
  5401. void zend_compile_class_decl(zend_ast *ast) /* {{{ */
  5402. {
  5403. zend_ast_decl *decl = (zend_ast_decl *) ast;
  5404. zend_ast *extends_ast = decl->child[0];
  5405. zend_ast *implements_ast = decl->child[1];
  5406. zend_ast *stmt_ast = decl->child[2];
  5407. zend_string *name, *lcname;
  5408. zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
  5409. zend_op *opline;
  5410. znode declare_node;
  5411. int extends_const;
  5412. zend_class_entry *original_ce = CG(active_class_entry);
  5413. znode original_implementing_class = FC(implementing_class);
  5414. if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
  5415. zend_string *unqualified_name = decl->name;
  5416. if (CG(active_class_entry)) {
  5417. zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
  5418. }
  5419. zend_assert_valid_class_name(unqualified_name);
  5420. name = zend_prefix_with_ns(unqualified_name);
  5421. name = zend_new_interned_string(name);
  5422. lcname = zend_string_tolower(name);
  5423. if (FC(imports)) {
  5424. zend_string *import_name = zend_hash_find_ptr_lc(
  5425. FC(imports), ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name));
  5426. if (import_name && !zend_string_equals_ci(lcname, import_name)) {
  5427. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
  5428. "because the name is already in use", ZSTR_VAL(name));
  5429. }
  5430. }
  5431. zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
  5432. } else {
  5433. name = zend_generate_anon_class_name(decl->lex_pos);
  5434. lcname = zend_string_tolower(name);
  5435. }
  5436. lcname = zend_new_interned_string(lcname);
  5437. ce->type = ZEND_USER_CLASS;
  5438. ce->name = name;
  5439. zend_initialize_class_data(ce, 1);
  5440. ce->ce_flags |= decl->flags;
  5441. ce->info.user.filename = zend_get_compiled_filename();
  5442. ce->info.user.line_start = decl->start_lineno;
  5443. ce->info.user.line_end = decl->end_lineno;
  5444. if (decl->doc_comment) {
  5445. ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
  5446. }
  5447. if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
  5448. /* Serialization is not supported for anonymous classes */
  5449. ce->serialize = zend_class_serialize_deny;
  5450. ce->unserialize = zend_class_unserialize_deny;
  5451. }
  5452. if (extends_ast) {
  5453. znode extends_node;
  5454. zend_string *extends_name;
  5455. if (!zend_is_const_default_class_ref(extends_ast)) {
  5456. extends_name = zend_ast_get_str(extends_ast);
  5457. zend_error_noreturn(E_COMPILE_ERROR,
  5458. "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(extends_name));
  5459. }
  5460. zend_compile_expr(&extends_node, extends_ast);
  5461. if (extends_node.op_type != IS_CONST || Z_TYPE(extends_node.u.constant) != IS_STRING) {
  5462. zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
  5463. }
  5464. extends_name = Z_STR(extends_node.u.constant);
  5465. extends_const = zend_add_class_name_literal(CG(active_op_array),
  5466. zend_resolve_class_name(extends_name,
  5467. extends_ast->kind == ZEND_AST_ZVAL ? extends_ast->attr : ZEND_NAME_FQ));
  5468. zend_string_release_ex(extends_name, 0);
  5469. ce->ce_flags |= ZEND_ACC_INHERITED;
  5470. }
  5471. opline = get_next_op(CG(active_op_array));
  5472. zend_make_var_result(&declare_node, opline);
  5473. GET_NODE(&FC(implementing_class), opline->result);
  5474. opline->op1_type = IS_CONST;
  5475. LITERAL_STR(opline->op1, lcname);
  5476. if (decl->flags & ZEND_ACC_ANON_CLASS) {
  5477. if (extends_ast) {
  5478. opline->opcode = ZEND_DECLARE_ANON_INHERITED_CLASS;
  5479. opline->op2_type = IS_CONST;
  5480. opline->op2.constant = extends_const;
  5481. } else {
  5482. opline->opcode = ZEND_DECLARE_ANON_CLASS;
  5483. }
  5484. if (!zend_hash_exists(CG(class_table), lcname)) {
  5485. zend_hash_add_ptr(CG(class_table), lcname, ce);
  5486. } else {
  5487. /* This anonymous class has been included, reuse the existing definition.
  5488. * NB: This behavior is buggy, and this should always result in a separate
  5489. * class declaration. However, until the problem of RTD key collisions is
  5490. * solved, this gives a behavior close to what is expected. */
  5491. zval zv;
  5492. ZVAL_PTR(&zv, ce);
  5493. destroy_zend_class(&zv);
  5494. ce = zend_hash_find_ptr(CG(class_table), lcname);
  5495. /* Manually replicate emission of necessary inheritance opcodes here. We cannot
  5496. * reuse the general code, as we only want to emit the opcodes, without modifying
  5497. * the reused class definition. */
  5498. if (ce->ce_flags & ZEND_ACC_IMPLEMENT_TRAITS) {
  5499. zend_emit_op(NULL, ZEND_BIND_TRAITS, &declare_node, NULL);
  5500. }
  5501. if (implements_ast) {
  5502. zend_ast_list *iface_list = zend_ast_get_list(implements_ast);
  5503. uint32_t i;
  5504. for (i = 0; i < iface_list->children; i++) {
  5505. opline = zend_emit_op(NULL, ZEND_ADD_INTERFACE, &declare_node, NULL);
  5506. opline->op2_type = IS_CONST;
  5507. opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
  5508. zend_resolve_class_name_ast(iface_list->child[i]));
  5509. }
  5510. zend_emit_op(NULL, ZEND_VERIFY_ABSTRACT_CLASS, &declare_node, NULL);
  5511. }
  5512. return;
  5513. }
  5514. } else {
  5515. zend_string *key;
  5516. if (extends_ast) {
  5517. opline->opcode = ZEND_DECLARE_INHERITED_CLASS;
  5518. opline->op2_type = IS_CONST;
  5519. opline->op2.constant = extends_const;
  5520. } else {
  5521. opline->opcode = ZEND_DECLARE_CLASS;
  5522. }
  5523. key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
  5524. /* RTD key is placed after lcname literal in op1 */
  5525. zend_add_literal_string(CG(active_op_array), &key);
  5526. zend_hash_update_ptr(CG(class_table), key, ce);
  5527. }
  5528. CG(active_class_entry) = ce;
  5529. zend_compile_stmt(stmt_ast);
  5530. /* Reset lineno for final opcodes and errors */
  5531. CG(zend_lineno) = ast->lineno;
  5532. if (ce->num_traits == 0) {
  5533. /* For traits this check is delayed until after trait binding */
  5534. zend_check_deprecated_constructor(ce);
  5535. }
  5536. if (ce->constructor) {
  5537. ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
  5538. if (ce->constructor->common.fn_flags & ZEND_ACC_STATIC) {
  5539. zend_error_noreturn(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static",
  5540. ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
  5541. }
  5542. if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  5543. zend_error_noreturn(E_COMPILE_ERROR,
  5544. "Constructor %s::%s() cannot declare a return type",
  5545. ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
  5546. }
  5547. }
  5548. if (ce->destructor) {
  5549. ce->destructor->common.fn_flags |= ZEND_ACC_DTOR;
  5550. if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
  5551. zend_error_noreturn(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static",
  5552. ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
  5553. } else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  5554. zend_error_noreturn(E_COMPILE_ERROR,
  5555. "Destructor %s::%s() cannot declare a return type",
  5556. ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
  5557. }
  5558. }
  5559. if (ce->clone) {
  5560. if (ce->clone->common.fn_flags & ZEND_ACC_STATIC) {
  5561. zend_error_noreturn(E_COMPILE_ERROR, "Clone method %s::%s() cannot be static",
  5562. ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
  5563. } else if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  5564. zend_error_noreturn(E_COMPILE_ERROR,
  5565. "%s::%s() cannot declare a return type",
  5566. ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
  5567. }
  5568. }
  5569. /* Check for traits and proceed like with interfaces.
  5570. * The only difference will be a combined handling of them in the end.
  5571. * Thus, we need another opcode here. */
  5572. if (ce->num_traits > 0) {
  5573. ce->traits = NULL;
  5574. ce->num_traits = 0;
  5575. ce->ce_flags |= ZEND_ACC_IMPLEMENT_TRAITS;
  5576. zend_emit_op(NULL, ZEND_BIND_TRAITS, &declare_node, NULL);
  5577. }
  5578. if (implements_ast) {
  5579. zend_compile_implements(&declare_node, implements_ast);
  5580. }
  5581. if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
  5582. && (extends_ast || implements_ast)
  5583. ) {
  5584. zend_verify_abstract_class(ce);
  5585. if (implements_ast) {
  5586. zend_emit_op(NULL, ZEND_VERIFY_ABSTRACT_CLASS, &declare_node, NULL);
  5587. }
  5588. }
  5589. /* Inherit interfaces; reset number to zero, we need it for above check and
  5590. * will restore it during actual implementation.
  5591. * The ZEND_ACC_IMPLEMENT_INTERFACES flag disables double call to
  5592. * zend_verify_abstract_class() */
  5593. if (ce->num_interfaces > 0) {
  5594. ce->interfaces = NULL;
  5595. ce->num_interfaces = 0;
  5596. ce->ce_flags |= ZEND_ACC_IMPLEMENT_INTERFACES;
  5597. }
  5598. FC(implementing_class) = original_implementing_class;
  5599. CG(active_class_entry) = original_ce;
  5600. }
  5601. /* }}} */
  5602. static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
  5603. {
  5604. switch (type) {
  5605. case ZEND_SYMBOL_CLASS:
  5606. if (!FC(imports)) {
  5607. FC(imports) = emalloc(sizeof(HashTable));
  5608. zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
  5609. }
  5610. return FC(imports);
  5611. case ZEND_SYMBOL_FUNCTION:
  5612. if (!FC(imports_function)) {
  5613. FC(imports_function) = emalloc(sizeof(HashTable));
  5614. zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
  5615. }
  5616. return FC(imports_function);
  5617. case ZEND_SYMBOL_CONST:
  5618. if (!FC(imports_const)) {
  5619. FC(imports_const) = emalloc(sizeof(HashTable));
  5620. zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
  5621. }
  5622. return FC(imports_const);
  5623. EMPTY_SWITCH_DEFAULT_CASE()
  5624. }
  5625. return NULL;
  5626. }
  5627. /* }}} */
  5628. static char *zend_get_use_type_str(uint32_t type) /* {{{ */
  5629. {
  5630. switch (type) {
  5631. case ZEND_SYMBOL_CLASS:
  5632. return "";
  5633. case ZEND_SYMBOL_FUNCTION:
  5634. return " function";
  5635. case ZEND_SYMBOL_CONST:
  5636. return " const";
  5637. EMPTY_SWITCH_DEFAULT_CASE()
  5638. }
  5639. return " unknown";
  5640. }
  5641. /* }}} */
  5642. static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
  5643. {
  5644. if (zend_string_equals_ci(old_name, check_name)) {
  5645. return;
  5646. }
  5647. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
  5648. "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
  5649. }
  5650. /* }}} */
  5651. void zend_compile_use(zend_ast *ast) /* {{{ */
  5652. {
  5653. zend_ast_list *list = zend_ast_get_list(ast);
  5654. uint32_t i;
  5655. zend_string *current_ns = FC(current_namespace);
  5656. uint32_t type = ast->attr;
  5657. HashTable *current_import = zend_get_import_ht(type);
  5658. zend_bool case_sensitive = type == ZEND_SYMBOL_CONST;
  5659. for (i = 0; i < list->children; ++i) {
  5660. zend_ast *use_ast = list->child[i];
  5661. zend_ast *old_name_ast = use_ast->child[0];
  5662. zend_ast *new_name_ast = use_ast->child[1];
  5663. zend_string *old_name = zend_ast_get_str(old_name_ast);
  5664. zend_string *new_name, *lookup_name;
  5665. if (new_name_ast) {
  5666. new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
  5667. } else {
  5668. const char *unqualified_name;
  5669. size_t unqualified_name_len;
  5670. if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
  5671. /* The form "use A\B" is equivalent to "use A\B as B" */
  5672. new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
  5673. } else {
  5674. new_name = zend_string_copy(old_name);
  5675. if (!current_ns) {
  5676. if (type == T_CLASS && zend_string_equals_literal(new_name, "strict")) {
  5677. zend_error_noreturn(E_COMPILE_ERROR,
  5678. "You seem to be trying to use a different language...");
  5679. }
  5680. zend_error(E_WARNING, "The use statement with non-compound name '%s' "
  5681. "has no effect", ZSTR_VAL(new_name));
  5682. }
  5683. }
  5684. }
  5685. if (case_sensitive) {
  5686. lookup_name = zend_string_copy(new_name);
  5687. } else {
  5688. lookup_name = zend_string_tolower(new_name);
  5689. }
  5690. if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
  5691. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
  5692. "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
  5693. }
  5694. if (current_ns) {
  5695. zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
  5696. zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
  5697. ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
  5698. memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
  5699. if (zend_have_seen_symbol(ns_name, type)) {
  5700. zend_check_already_in_use(type, old_name, new_name, ns_name);
  5701. }
  5702. zend_string_efree(ns_name);
  5703. } else {
  5704. if (zend_have_seen_symbol(lookup_name, type)) {
  5705. zend_check_already_in_use(type, old_name, new_name, lookup_name);
  5706. }
  5707. }
  5708. zend_string_addref(old_name);
  5709. old_name = zend_new_interned_string(old_name);
  5710. if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
  5711. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
  5712. "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
  5713. }
  5714. zend_string_release_ex(lookup_name, 0);
  5715. zend_string_release_ex(new_name, 0);
  5716. }
  5717. }
  5718. /* }}} */
  5719. void zend_compile_group_use(zend_ast *ast) /* {{{ */
  5720. {
  5721. uint32_t i;
  5722. zend_string *ns = zend_ast_get_str(ast->child[0]);
  5723. zend_ast_list *list = zend_ast_get_list(ast->child[1]);
  5724. for (i = 0; i < list->children; i++) {
  5725. zend_ast *inline_use, *use = list->child[i];
  5726. zval *name_zval = zend_ast_get_zval(use->child[0]);
  5727. zend_string *name = Z_STR_P(name_zval);
  5728. zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
  5729. zend_string_release_ex(name, 0);
  5730. ZVAL_STR(name_zval, compound_ns);
  5731. inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
  5732. inline_use->attr = ast->attr ? ast->attr : use->attr;
  5733. zend_compile_use(inline_use);
  5734. }
  5735. }
  5736. /* }}} */
  5737. void zend_compile_const_decl(zend_ast *ast) /* {{{ */
  5738. {
  5739. zend_ast_list *list = zend_ast_get_list(ast);
  5740. uint32_t i;
  5741. for (i = 0; i < list->children; ++i) {
  5742. zend_ast *const_ast = list->child[i];
  5743. zend_ast *name_ast = const_ast->child[0];
  5744. zend_ast *value_ast = const_ast->child[1];
  5745. zend_string *unqualified_name = zend_ast_get_str(name_ast);
  5746. zend_string *name;
  5747. znode name_node, value_node;
  5748. zval *value_zv = &value_node.u.constant;
  5749. value_node.op_type = IS_CONST;
  5750. zend_const_expr_to_zval(value_zv, value_ast);
  5751. if (zend_lookup_reserved_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
  5752. zend_error_noreturn(E_COMPILE_ERROR,
  5753. "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
  5754. }
  5755. name = zend_prefix_with_ns(unqualified_name);
  5756. name = zend_new_interned_string(name);
  5757. if (FC(imports_const)) {
  5758. zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
  5759. if (import_name && !zend_string_equals(import_name, name)) {
  5760. zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
  5761. "the name is already in use", ZSTR_VAL(name));
  5762. }
  5763. }
  5764. name_node.op_type = IS_CONST;
  5765. ZVAL_STR(&name_node.u.constant, name);
  5766. zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
  5767. zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
  5768. }
  5769. }
  5770. /* }}}*/
  5771. void zend_compile_namespace(zend_ast *ast) /* {{{ */
  5772. {
  5773. zend_ast *name_ast = ast->child[0];
  5774. zend_ast *stmt_ast = ast->child[1];
  5775. zend_string *name;
  5776. zend_bool with_bracket = stmt_ast != NULL;
  5777. /* handle mixed syntax declaration or nested namespaces */
  5778. if (!FC(has_bracketed_namespaces)) {
  5779. if (FC(current_namespace)) {
  5780. /* previous namespace declarations were unbracketed */
  5781. if (with_bracket) {
  5782. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
  5783. "with unbracketed namespace declarations");
  5784. }
  5785. }
  5786. } else {
  5787. /* previous namespace declarations were bracketed */
  5788. if (!with_bracket) {
  5789. zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
  5790. "with unbracketed namespace declarations");
  5791. } else if (FC(current_namespace) || FC(in_namespace)) {
  5792. zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
  5793. }
  5794. }
  5795. if (((!with_bracket && !FC(current_namespace))
  5796. || (with_bracket && !FC(has_bracketed_namespaces))) && CG(active_op_array)->last > 0
  5797. ) {
  5798. /* ignore ZEND_EXT_STMT and ZEND_TICKS */
  5799. uint32_t num = CG(active_op_array)->last;
  5800. while (num > 0 &&
  5801. (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
  5802. CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
  5803. --num;
  5804. }
  5805. if (num > 0) {
  5806. zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
  5807. "the very first statement or after any declare call in the script");
  5808. }
  5809. }
  5810. if (FC(current_namespace)) {
  5811. zend_string_release_ex(FC(current_namespace), 0);
  5812. }
  5813. if (name_ast) {
  5814. name = zend_ast_get_str(name_ast);
  5815. if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
  5816. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
  5817. }
  5818. FC(current_namespace) = zend_string_copy(name);
  5819. } else {
  5820. FC(current_namespace) = NULL;
  5821. }
  5822. zend_reset_import_tables();
  5823. FC(in_namespace) = 1;
  5824. if (with_bracket) {
  5825. FC(has_bracketed_namespaces) = 1;
  5826. }
  5827. if (stmt_ast) {
  5828. zend_compile_top_stmt(stmt_ast);
  5829. zend_end_namespace();
  5830. }
  5831. }
  5832. /* }}} */
  5833. void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
  5834. {
  5835. zend_ast *offset_ast = ast->child[0];
  5836. zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
  5837. zend_string *filename, *name;
  5838. const char const_name[] = "__COMPILER_HALT_OFFSET__";
  5839. if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
  5840. zend_error_noreturn(E_COMPILE_ERROR,
  5841. "__HALT_COMPILER() can only be used from the outermost scope");
  5842. }
  5843. filename = zend_get_compiled_filename();
  5844. name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
  5845. ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
  5846. zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, CONST_CS, 0);
  5847. zend_string_release_ex(name, 0);
  5848. }
  5849. /* }}} */
  5850. static zend_bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
  5851. {
  5852. zend_op_array *op_array = CG(active_op_array);
  5853. zend_class_entry *ce = CG(active_class_entry);
  5854. switch (ast->attr) {
  5855. case T_LINE:
  5856. ZVAL_LONG(zv, ast->lineno);
  5857. break;
  5858. case T_FILE:
  5859. ZVAL_STR_COPY(zv, CG(compiled_filename));
  5860. break;
  5861. case T_DIR:
  5862. {
  5863. zend_string *filename = CG(compiled_filename);
  5864. zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
  5865. #ifdef ZEND_WIN32
  5866. ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
  5867. #else
  5868. ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
  5869. #endif
  5870. if (strcmp(ZSTR_VAL(dirname), ".") == 0) {
  5871. dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
  5872. #if HAVE_GETCWD
  5873. ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
  5874. #elif HAVE_GETWD
  5875. ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
  5876. #endif
  5877. ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
  5878. }
  5879. ZVAL_STR(zv, dirname);
  5880. break;
  5881. }
  5882. case T_FUNC_C:
  5883. if (op_array && op_array->function_name) {
  5884. ZVAL_STR_COPY(zv, op_array->function_name);
  5885. } else {
  5886. ZVAL_EMPTY_STRING(zv);
  5887. }
  5888. break;
  5889. case T_METHOD_C:
  5890. if ((op_array && !op_array->scope && op_array->function_name) || (op_array->fn_flags & ZEND_ACC_CLOSURE)) {
  5891. ZVAL_STR_COPY(zv, op_array->function_name);
  5892. } else if (ce) {
  5893. if (op_array && op_array->function_name) {
  5894. ZVAL_NEW_STR(zv, zend_concat3(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), "::", 2,
  5895. ZSTR_VAL(op_array->function_name), ZSTR_LEN(op_array->function_name)));
  5896. } else {
  5897. ZVAL_STR_COPY(zv, ce->name);
  5898. }
  5899. } else if (op_array && op_array->function_name) {
  5900. ZVAL_STR_COPY(zv, op_array->function_name);
  5901. } else {
  5902. ZVAL_EMPTY_STRING(zv);
  5903. }
  5904. break;
  5905. case T_CLASS_C:
  5906. if (ce) {
  5907. if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  5908. return 0;
  5909. } else {
  5910. ZVAL_STR_COPY(zv, ce->name);
  5911. }
  5912. } else {
  5913. ZVAL_EMPTY_STRING(zv);
  5914. }
  5915. break;
  5916. case T_TRAIT_C:
  5917. if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
  5918. ZVAL_STR_COPY(zv, ce->name);
  5919. } else {
  5920. ZVAL_EMPTY_STRING(zv);
  5921. }
  5922. break;
  5923. case T_NS_C:
  5924. if (FC(current_namespace)) {
  5925. ZVAL_STR_COPY(zv, FC(current_namespace));
  5926. } else {
  5927. ZVAL_EMPTY_STRING(zv);
  5928. }
  5929. break;
  5930. EMPTY_SWITCH_DEFAULT_CASE()
  5931. }
  5932. return 1;
  5933. }
  5934. /* }}} */
  5935. ZEND_API zend_bool zend_binary_op_produces_numeric_string_error(uint32_t opcode, zval *op1, zval *op2) /* {{{ */
  5936. {
  5937. if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
  5938. || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
  5939. || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
  5940. return 0;
  5941. }
  5942. /* While basic arithmetic operators always produce numeric string errors,
  5943. * bitwise operators don't produce errors if both operands are strings */
  5944. if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
  5945. && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
  5946. return 0;
  5947. }
  5948. if (Z_TYPE_P(op1) == IS_STRING
  5949. && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
  5950. return 1;
  5951. }
  5952. if (Z_TYPE_P(op2) == IS_STRING
  5953. && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
  5954. return 1;
  5955. }
  5956. return 0;
  5957. }
  5958. /* }}} */
  5959. static inline zend_bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
  5960. {
  5961. binary_op_type fn = get_binary_op(opcode);
  5962. /* don't evaluate division by zero at compile-time */
  5963. if ((opcode == ZEND_DIV || opcode == ZEND_MOD) &&
  5964. zval_get_long(op2) == 0) {
  5965. return 0;
  5966. } else if ((opcode == ZEND_SL || opcode == ZEND_SR) &&
  5967. zval_get_long(op2) < 0) {
  5968. return 0;
  5969. }
  5970. /* don't evaluate numeric string error-producing operations at compile-time */
  5971. if (zend_binary_op_produces_numeric_string_error(opcode, op1, op2)) {
  5972. return 0;
  5973. }
  5974. fn(result, op1, op2);
  5975. return 1;
  5976. }
  5977. /* }}} */
  5978. static inline void zend_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
  5979. {
  5980. unary_op_type fn = get_unary_op(opcode);
  5981. fn(result, op);
  5982. }
  5983. /* }}} */
  5984. static inline zend_bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
  5985. {
  5986. zval left;
  5987. ZVAL_LONG(&left, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
  5988. return zend_try_ct_eval_binary_op(result, ZEND_MUL, &left, op);
  5989. }
  5990. /* }}} */
  5991. static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
  5992. {
  5993. binary_op_type fn = kind == ZEND_AST_GREATER
  5994. ? is_smaller_function : is_smaller_or_equal_function;
  5995. fn(result, op2, op1);
  5996. }
  5997. /* }}} */
  5998. static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
  5999. {
  6000. zend_ast_list *list = zend_ast_get_list(ast);
  6001. zend_ast *last_elem_ast = NULL;
  6002. uint32_t i;
  6003. zend_bool is_constant = 1;
  6004. if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
  6005. zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
  6006. }
  6007. /* First ensure that *all* child nodes are constant and by-val */
  6008. for (i = 0; i < list->children; ++i) {
  6009. zend_ast *elem_ast = list->child[i];
  6010. if (elem_ast == NULL) {
  6011. /* Report error at line of last non-empty element */
  6012. if (last_elem_ast) {
  6013. CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
  6014. }
  6015. zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
  6016. }
  6017. zend_eval_const_expr(&elem_ast->child[0]);
  6018. zend_eval_const_expr(&elem_ast->child[1]);
  6019. if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
  6020. || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
  6021. ) {
  6022. is_constant = 0;
  6023. }
  6024. last_elem_ast = elem_ast;
  6025. }
  6026. if (!is_constant) {
  6027. return 0;
  6028. }
  6029. if (!list->children) {
  6030. ZVAL_EMPTY_ARRAY(result);
  6031. return 1;
  6032. }
  6033. array_init_size(result, list->children);
  6034. for (i = 0; i < list->children; ++i) {
  6035. zend_ast *elem_ast = list->child[i];
  6036. zend_ast *value_ast = elem_ast->child[0];
  6037. zend_ast *key_ast = elem_ast->child[1];
  6038. zval *value = zend_ast_get_zval(value_ast);
  6039. Z_TRY_ADDREF_P(value);
  6040. if (key_ast) {
  6041. zval *key = zend_ast_get_zval(key_ast);
  6042. switch (Z_TYPE_P(key)) {
  6043. case IS_LONG:
  6044. zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
  6045. break;
  6046. case IS_STRING:
  6047. zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
  6048. break;
  6049. case IS_DOUBLE:
  6050. zend_hash_index_update(Z_ARRVAL_P(result),
  6051. zend_dval_to_lval(Z_DVAL_P(key)), value);
  6052. break;
  6053. case IS_FALSE:
  6054. zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
  6055. break;
  6056. case IS_TRUE:
  6057. zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
  6058. break;
  6059. case IS_NULL:
  6060. zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value);
  6061. break;
  6062. default:
  6063. zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
  6064. break;
  6065. }
  6066. } else {
  6067. if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
  6068. zval_ptr_dtor_nogc(value);
  6069. zval_ptr_dtor(result);
  6070. return 0;
  6071. }
  6072. }
  6073. }
  6074. return 1;
  6075. }
  6076. /* }}} */
  6077. void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
  6078. {
  6079. zend_ast *left_ast = ast->child[0];
  6080. zend_ast *right_ast = ast->child[1];
  6081. uint32_t opcode = ast->attr;
  6082. znode left_node, right_node;
  6083. zend_compile_expr(&left_node, left_ast);
  6084. zend_compile_expr(&right_node, right_ast);
  6085. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  6086. if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
  6087. &left_node.u.constant, &right_node.u.constant)
  6088. ) {
  6089. result->op_type = IS_CONST;
  6090. zval_ptr_dtor(&left_node.u.constant);
  6091. zval_ptr_dtor(&right_node.u.constant);
  6092. return;
  6093. }
  6094. }
  6095. do {
  6096. if (opcode == ZEND_IS_EQUAL || opcode == ZEND_IS_NOT_EQUAL) {
  6097. if (left_node.op_type == IS_CONST) {
  6098. if (Z_TYPE(left_node.u.constant) == IS_FALSE) {
  6099. opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6100. zend_emit_op_tmp(result, opcode, &right_node, NULL);
  6101. break;
  6102. } else if (Z_TYPE(left_node.u.constant) == IS_TRUE) {
  6103. opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6104. zend_emit_op_tmp(result, opcode, &right_node, NULL);
  6105. break;
  6106. }
  6107. } else if (right_node.op_type == IS_CONST) {
  6108. if (Z_TYPE(right_node.u.constant) == IS_FALSE) {
  6109. opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6110. zend_emit_op_tmp(result, opcode, &left_node, NULL);
  6111. break;
  6112. } else if (Z_TYPE(right_node.u.constant) == IS_TRUE) {
  6113. opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
  6114. zend_emit_op_tmp(result, opcode, &left_node, NULL);
  6115. break;
  6116. }
  6117. }
  6118. }
  6119. if (opcode == ZEND_CONCAT) {
  6120. /* convert constant operands to strings at compile-time */
  6121. if (left_node.op_type == IS_CONST) {
  6122. convert_to_string(&left_node.u.constant);
  6123. }
  6124. if (right_node.op_type == IS_CONST) {
  6125. convert_to_string(&right_node.u.constant);
  6126. }
  6127. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  6128. opcode = ZEND_FAST_CONCAT;
  6129. }
  6130. }
  6131. zend_emit_op_tmp(result, opcode, &left_node, &right_node);
  6132. } while (0);
  6133. }
  6134. /* }}} */
  6135. /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
  6136. * evaluation order. */
  6137. void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
  6138. {
  6139. zend_ast *left_ast = ast->child[0];
  6140. zend_ast *right_ast = ast->child[1];
  6141. znode left_node, right_node;
  6142. ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
  6143. zend_compile_expr(&left_node, left_ast);
  6144. zend_compile_expr(&right_node, right_ast);
  6145. if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
  6146. result->op_type = IS_CONST;
  6147. zend_ct_eval_greater(&result->u.constant, ast->kind,
  6148. &left_node.u.constant, &right_node.u.constant);
  6149. zval_ptr_dtor(&left_node.u.constant);
  6150. zval_ptr_dtor(&right_node.u.constant);
  6151. return;
  6152. }
  6153. zend_emit_op_tmp(result,
  6154. ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
  6155. &right_node, &left_node);
  6156. }
  6157. /* }}} */
  6158. void zend_compile_unary_op(znode *result, zend_ast *ast) /* {{{ */
  6159. {
  6160. zend_ast *expr_ast = ast->child[0];
  6161. uint32_t opcode = ast->attr;
  6162. znode expr_node;
  6163. zend_compile_expr(&expr_node, expr_ast);
  6164. if (expr_node.op_type == IS_CONST) {
  6165. result->op_type = IS_CONST;
  6166. zend_ct_eval_unary_op(&result->u.constant, opcode,
  6167. &expr_node.u.constant);
  6168. zval_ptr_dtor(&expr_node.u.constant);
  6169. return;
  6170. }
  6171. zend_emit_op_tmp(result, opcode, &expr_node, NULL);
  6172. }
  6173. /* }}} */
  6174. void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
  6175. {
  6176. zend_ast *expr_ast = ast->child[0];
  6177. znode expr_node;
  6178. znode lefthand_node;
  6179. ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
  6180. zend_compile_expr(&expr_node, expr_ast);
  6181. if (expr_node.op_type == IS_CONST) {
  6182. if (zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
  6183. result->op_type = IS_CONST;
  6184. zval_ptr_dtor(&expr_node.u.constant);
  6185. return;
  6186. }
  6187. }
  6188. lefthand_node.op_type = IS_CONST;
  6189. ZVAL_LONG(&lefthand_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
  6190. zend_emit_op_tmp(result, ZEND_MUL, &lefthand_node, &expr_node);
  6191. }
  6192. /* }}} */
  6193. void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
  6194. {
  6195. zend_ast *left_ast = ast->child[0];
  6196. zend_ast *right_ast = ast->child[1];
  6197. znode left_node, right_node;
  6198. zend_op *opline_jmpz, *opline_bool;
  6199. uint32_t opnum_jmpz;
  6200. ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
  6201. zend_compile_expr(&left_node, left_ast);
  6202. if (left_node.op_type == IS_CONST) {
  6203. if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
  6204. || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
  6205. result->op_type = IS_CONST;
  6206. ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
  6207. } else {
  6208. zend_compile_expr(&right_node, right_ast);
  6209. if (right_node.op_type == IS_CONST) {
  6210. result->op_type = IS_CONST;
  6211. ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
  6212. zval_ptr_dtor(&right_node.u.constant);
  6213. } else {
  6214. zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
  6215. }
  6216. }
  6217. zval_ptr_dtor(&left_node.u.constant);
  6218. return;
  6219. }
  6220. opnum_jmpz = get_next_op_number(CG(active_op_array));
  6221. opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
  6222. &left_node, NULL);
  6223. if (left_node.op_type == IS_TMP_VAR) {
  6224. SET_NODE(opline_jmpz->result, &left_node);
  6225. } else {
  6226. opline_jmpz->result.var = get_temporary_variable(CG(active_op_array));
  6227. opline_jmpz->result_type = IS_TMP_VAR;
  6228. }
  6229. GET_NODE(result, opline_jmpz->result);
  6230. zend_compile_expr(&right_node, right_ast);
  6231. opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
  6232. SET_NODE(opline_bool->result, result);
  6233. zend_update_jump_target_to_next(opnum_jmpz);
  6234. }
  6235. /* }}} */
  6236. void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
  6237. {
  6238. zend_ast *var_ast = ast->child[0];
  6239. ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
  6240. zend_ensure_writable_variable(var_ast);
  6241. if (var_ast->kind == ZEND_AST_PROP) {
  6242. zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW);
  6243. opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
  6244. zend_make_tmp_result(result, opline);
  6245. } else {
  6246. znode var_node;
  6247. zend_compile_var(&var_node, var_ast, BP_VAR_RW);
  6248. zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
  6249. &var_node, NULL);
  6250. }
  6251. }
  6252. /* }}} */
  6253. void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
  6254. {
  6255. zend_ast *var_ast = ast->child[0];
  6256. ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
  6257. zend_ensure_writable_variable(var_ast);
  6258. if (var_ast->kind == ZEND_AST_PROP) {
  6259. zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW);
  6260. opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
  6261. } else {
  6262. znode var_node;
  6263. zend_compile_var(&var_node, var_ast, BP_VAR_RW);
  6264. zend_emit_op(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
  6265. &var_node, NULL);
  6266. }
  6267. }
  6268. /* }}} */
  6269. void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
  6270. {
  6271. zend_ast *expr_ast = ast->child[0];
  6272. znode expr_node;
  6273. zend_op *opline;
  6274. zend_compile_expr(&expr_node, expr_ast);
  6275. opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
  6276. opline->extended_value = ast->attr;
  6277. if (ast->attr == IS_NULL) {
  6278. zend_error(E_DEPRECATED, "The (unset) cast is deprecated");
  6279. }
  6280. }
  6281. /* }}} */
  6282. static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
  6283. {
  6284. zend_ast *cond_ast = ast->child[0];
  6285. zend_ast *false_ast = ast->child[2];
  6286. znode cond_node, false_node;
  6287. zend_op *opline_qm_assign;
  6288. uint32_t opnum_jmp_set;
  6289. ZEND_ASSERT(ast->child[1] == NULL);
  6290. zend_compile_expr(&cond_node, cond_ast);
  6291. opnum_jmp_set = get_next_op_number(CG(active_op_array));
  6292. zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
  6293. zend_compile_expr(&false_node, false_ast);
  6294. opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
  6295. SET_NODE(opline_qm_assign->result, result);
  6296. zend_update_jump_target_to_next(opnum_jmp_set);
  6297. }
  6298. /* }}} */
  6299. void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
  6300. {
  6301. zend_ast *cond_ast = ast->child[0];
  6302. zend_ast *true_ast = ast->child[1];
  6303. zend_ast *false_ast = ast->child[2];
  6304. znode cond_node, true_node, false_node;
  6305. zend_op *opline_qm_assign2;
  6306. uint32_t opnum_jmpz, opnum_jmp;
  6307. if (!true_ast) {
  6308. zend_compile_shorthand_conditional(result, ast);
  6309. return;
  6310. }
  6311. zend_compile_expr(&cond_node, cond_ast);
  6312. opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
  6313. zend_compile_expr(&true_node, true_ast);
  6314. zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
  6315. opnum_jmp = zend_emit_jump(0);
  6316. zend_update_jump_target_to_next(opnum_jmpz);
  6317. zend_compile_expr(&false_node, false_ast);
  6318. opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
  6319. SET_NODE(opline_qm_assign2->result, result);
  6320. zend_update_jump_target_to_next(opnum_jmp);
  6321. }
  6322. /* }}} */
  6323. void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
  6324. {
  6325. zend_ast *expr_ast = ast->child[0];
  6326. zend_ast *default_ast = ast->child[1];
  6327. znode expr_node, default_node;
  6328. zend_op *opline;
  6329. uint32_t opnum;
  6330. zend_compile_var(&expr_node, expr_ast, BP_VAR_IS);
  6331. opnum = get_next_op_number(CG(active_op_array));
  6332. zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
  6333. zend_compile_expr(&default_node, default_ast);
  6334. opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
  6335. SET_NODE(opline->result, result);
  6336. opline = &CG(active_op_array)->opcodes[opnum];
  6337. opline->op2.opline_num = get_next_op_number(CG(active_op_array));
  6338. }
  6339. /* }}} */
  6340. void zend_compile_print(znode *result, zend_ast *ast) /* {{{ */
  6341. {
  6342. zend_op *opline;
  6343. zend_ast *expr_ast = ast->child[0];
  6344. znode expr_node;
  6345. zend_compile_expr(&expr_node, expr_ast);
  6346. opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
  6347. opline->extended_value = 1;
  6348. result->op_type = IS_CONST;
  6349. ZVAL_LONG(&result->u.constant, 1);
  6350. }
  6351. /* }}} */
  6352. void zend_compile_exit(znode *result, zend_ast *ast) /* {{{ */
  6353. {
  6354. zend_ast *expr_ast = ast->child[0];
  6355. if (expr_ast) {
  6356. znode expr_node;
  6357. zend_compile_expr(&expr_node, expr_ast);
  6358. zend_emit_op(NULL, ZEND_EXIT, &expr_node, NULL);
  6359. } else {
  6360. zend_emit_op(NULL, ZEND_EXIT, NULL, NULL);
  6361. }
  6362. result->op_type = IS_CONST;
  6363. ZVAL_BOOL(&result->u.constant, 1);
  6364. }
  6365. /* }}} */
  6366. void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
  6367. {
  6368. zend_ast *value_ast = ast->child[0];
  6369. zend_ast *key_ast = ast->child[1];
  6370. znode value_node, key_node;
  6371. znode *value_node_ptr = NULL, *key_node_ptr = NULL;
  6372. zend_op *opline;
  6373. zend_bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
  6374. zend_mark_function_as_generator();
  6375. if (key_ast) {
  6376. zend_compile_expr(&key_node, key_ast);
  6377. key_node_ptr = &key_node;
  6378. }
  6379. if (value_ast) {
  6380. if (returns_by_ref && zend_is_variable(value_ast) && !zend_is_call(value_ast)) {
  6381. zend_compile_var(&value_node, value_ast, BP_VAR_W);
  6382. } else {
  6383. zend_compile_expr(&value_node, value_ast);
  6384. }
  6385. value_node_ptr = &value_node;
  6386. }
  6387. opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
  6388. if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
  6389. opline->extended_value = ZEND_RETURNS_FUNCTION;
  6390. }
  6391. }
  6392. /* }}} */
  6393. void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
  6394. {
  6395. zend_ast *expr_ast = ast->child[0];
  6396. znode expr_node;
  6397. zend_mark_function_as_generator();
  6398. if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
  6399. zend_error_noreturn(E_COMPILE_ERROR,
  6400. "Cannot use \"yield from\" inside a by-reference generator");
  6401. }
  6402. zend_compile_expr(&expr_node, expr_ast);
  6403. zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
  6404. }
  6405. /* }}} */
  6406. void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
  6407. {
  6408. zend_ast *obj_ast = ast->child[0];
  6409. zend_ast *class_ast = ast->child[1];
  6410. znode obj_node, class_node;
  6411. zend_op *opline;
  6412. zend_compile_expr(&obj_node, obj_ast);
  6413. if (obj_node.op_type == IS_CONST) {
  6414. zend_do_free(&obj_node);
  6415. result->op_type = IS_CONST;
  6416. ZVAL_FALSE(&result->u.constant);
  6417. return;
  6418. }
  6419. zend_compile_class_ref_ex(&class_node, class_ast,
  6420. ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION);
  6421. opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
  6422. if (class_node.op_type == IS_CONST) {
  6423. opline->op2_type = IS_CONST;
  6424. opline->op2.constant = zend_add_class_name_literal(
  6425. CG(active_op_array), Z_STR(class_node.u.constant));
  6426. opline->extended_value = zend_alloc_cache_slot();
  6427. } else {
  6428. SET_NODE(opline->op2, &class_node);
  6429. }
  6430. }
  6431. /* }}} */
  6432. void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */
  6433. {
  6434. zend_ast *expr_ast = ast->child[0];
  6435. znode expr_node;
  6436. zend_op *opline;
  6437. zend_do_extended_fcall_begin();
  6438. zend_compile_expr(&expr_node, expr_ast);
  6439. opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
  6440. opline->extended_value = ast->attr;
  6441. zend_do_extended_fcall_end();
  6442. }
  6443. /* }}} */
  6444. void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */
  6445. {
  6446. zend_ast *var_ast = ast->child[0];
  6447. znode var_node;
  6448. zend_op *opline = NULL;
  6449. ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
  6450. if (!zend_is_variable(var_ast) || zend_is_call(var_ast)) {
  6451. if (ast->kind == ZEND_AST_EMPTY) {
  6452. /* empty(expr) can be transformed to !expr */
  6453. zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
  6454. zend_compile_expr(result, not_ast);
  6455. return;
  6456. } else {
  6457. zend_error_noreturn(E_COMPILE_ERROR,
  6458. "Cannot use isset() on the result of an expression "
  6459. "(you can use \"null !== expression\" instead)");
  6460. }
  6461. }
  6462. switch (var_ast->kind) {
  6463. case ZEND_AST_VAR:
  6464. if (is_this_fetch(var_ast)) {
  6465. opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
  6466. } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
  6467. opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
  6468. } else {
  6469. opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, 0);
  6470. opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
  6471. }
  6472. break;
  6473. case ZEND_AST_DIM:
  6474. opline = zend_compile_dim(result, var_ast, BP_VAR_IS);
  6475. opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
  6476. break;
  6477. case ZEND_AST_PROP:
  6478. opline = zend_compile_prop(result, var_ast, BP_VAR_IS);
  6479. opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
  6480. break;
  6481. case ZEND_AST_STATIC_PROP:
  6482. opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, 0);
  6483. opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
  6484. break;
  6485. EMPTY_SWITCH_DEFAULT_CASE()
  6486. }
  6487. result->op_type = opline->result_type = IS_TMP_VAR;
  6488. if (!(ast->kind == ZEND_AST_ISSET)) {
  6489. opline->extended_value |= ZEND_ISEMPTY;
  6490. }
  6491. }
  6492. /* }}} */
  6493. void zend_compile_silence(znode *result, zend_ast *ast) /* {{{ */
  6494. {
  6495. zend_ast *expr_ast = ast->child[0];
  6496. znode silence_node;
  6497. uint32_t range;
  6498. range = zend_start_live_range(CG(active_op_array), get_next_op_number(CG(active_op_array)));
  6499. zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
  6500. if (expr_ast->kind == ZEND_AST_VAR) {
  6501. /* For @$var we need to force a FETCH instruction, otherwise the CV access will
  6502. * happen outside the silenced section. */
  6503. zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, 0 );
  6504. } else {
  6505. zend_compile_expr(result, expr_ast);
  6506. }
  6507. /* Store BEGIN_SILENCE/END_SILENCE pair to restore previous
  6508. * EG(error_reporting) value on exception */
  6509. zend_end_live_range(CG(active_op_array), range, get_next_op_number(CG(active_op_array)),
  6510. ZEND_LIVE_SILENCE, silence_node.u.op.var);
  6511. zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
  6512. }
  6513. /* }}} */
  6514. void zend_compile_shell_exec(znode *result, zend_ast *ast) /* {{{ */
  6515. {
  6516. zend_ast *expr_ast = ast->child[0];
  6517. zval fn_name;
  6518. zend_ast *name_ast, *args_ast, *call_ast;
  6519. ZVAL_STRING(&fn_name, "shell_exec");
  6520. name_ast = zend_ast_create_zval(&fn_name);
  6521. args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
  6522. call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
  6523. zend_compile_expr(result, call_ast);
  6524. zval_ptr_dtor(&fn_name);
  6525. }
  6526. /* }}} */
  6527. void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
  6528. {
  6529. zend_ast_list *list = zend_ast_get_list(ast);
  6530. zend_op *opline;
  6531. uint32_t i, opnum_init = -1;
  6532. zend_bool packed = 1;
  6533. if (zend_try_ct_eval_array(&result->u.constant, ast)) {
  6534. result->op_type = IS_CONST;
  6535. return;
  6536. }
  6537. /* Empty arrays are handled at compile-time */
  6538. ZEND_ASSERT(list->children > 0);
  6539. for (i = 0; i < list->children; ++i) {
  6540. zend_ast *elem_ast = list->child[i];
  6541. zend_ast *value_ast, *key_ast;
  6542. zend_bool by_ref;
  6543. znode value_node, key_node, *key_node_ptr = NULL;
  6544. if (elem_ast == NULL) {
  6545. zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
  6546. }
  6547. value_ast = elem_ast->child[0];
  6548. key_ast = elem_ast->child[1];
  6549. by_ref = elem_ast->attr;
  6550. if (key_ast) {
  6551. zend_compile_expr(&key_node, key_ast);
  6552. zend_handle_numeric_op(&key_node);
  6553. key_node_ptr = &key_node;
  6554. }
  6555. if (by_ref) {
  6556. zend_ensure_writable_variable(value_ast);
  6557. zend_compile_var(&value_node, value_ast, BP_VAR_W);
  6558. } else {
  6559. zend_compile_expr(&value_node, value_ast);
  6560. }
  6561. if (i == 0) {
  6562. opnum_init = get_next_op_number(CG(active_op_array));
  6563. opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
  6564. opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
  6565. } else {
  6566. opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
  6567. &value_node, key_node_ptr);
  6568. SET_NODE(opline->result, result);
  6569. }
  6570. opline->extended_value |= by_ref;
  6571. if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
  6572. packed = 0;
  6573. }
  6574. }
  6575. /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
  6576. if (!packed) {
  6577. ZEND_ASSERT(opnum_init != (uint32_t)-1);
  6578. opline = &CG(active_op_array)->opcodes[opnum_init];
  6579. opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
  6580. }
  6581. }
  6582. /* }}} */
  6583. void zend_compile_const(znode *result, zend_ast *ast) /* {{{ */
  6584. {
  6585. zend_ast *name_ast = ast->child[0];
  6586. zend_op *opline;
  6587. zend_bool is_fully_qualified;
  6588. zend_string *orig_name = zend_ast_get_str(name_ast);
  6589. zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
  6590. 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__"))) {
  6591. zend_ast *last = CG(ast);
  6592. while (last && last->kind == ZEND_AST_STMT_LIST) {
  6593. zend_ast_list *list = zend_ast_get_list(last);
  6594. if (list->children == 0) {
  6595. break;
  6596. }
  6597. last = list->child[list->children-1];
  6598. }
  6599. if (last && last->kind == ZEND_AST_HALT_COMPILER) {
  6600. result->op_type = IS_CONST;
  6601. ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
  6602. zend_string_release_ex(resolved_name, 0);
  6603. return;
  6604. }
  6605. }
  6606. if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
  6607. result->op_type = IS_CONST;
  6608. zend_string_release_ex(resolved_name, 0);
  6609. return;
  6610. }
  6611. opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
  6612. opline->op2_type = IS_CONST;
  6613. if (is_fully_qualified) {
  6614. opline->op2.constant = zend_add_const_name_literal(
  6615. CG(active_op_array), resolved_name, 0);
  6616. } else {
  6617. opline->op1.num = IS_CONSTANT_UNQUALIFIED;
  6618. if (FC(current_namespace)) {
  6619. opline->op1.num |= IS_CONSTANT_IN_NAMESPACE;
  6620. opline->op2.constant = zend_add_const_name_literal(
  6621. CG(active_op_array), resolved_name, 1);
  6622. } else {
  6623. opline->op2.constant = zend_add_const_name_literal(
  6624. CG(active_op_array), resolved_name, 0);
  6625. }
  6626. }
  6627. opline->extended_value = zend_alloc_cache_slot();
  6628. }
  6629. /* }}} */
  6630. void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
  6631. {
  6632. zend_ast *class_ast = ast->child[0];
  6633. zend_ast *const_ast = ast->child[1];
  6634. znode class_node, const_node;
  6635. zend_op *opline;
  6636. if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast, const_ast, 0)) {
  6637. if (Z_TYPE(result->u.constant) == IS_NULL) {
  6638. zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  6639. opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
  6640. } else {
  6641. result->op_type = IS_CONST;
  6642. }
  6643. return;
  6644. }
  6645. zend_eval_const_expr(&ast->child[0]);
  6646. zend_eval_const_expr(&ast->child[1]);
  6647. class_ast = ast->child[0];
  6648. const_ast = ast->child[1];
  6649. if (class_ast->kind == ZEND_AST_ZVAL) {
  6650. zend_string *resolved_name;
  6651. resolved_name = zend_resolve_class_name_ast(class_ast);
  6652. if (const_ast->kind == ZEND_AST_ZVAL && zend_try_ct_eval_class_const(&result->u.constant, resolved_name, zend_ast_get_str(const_ast))) {
  6653. result->op_type = IS_CONST;
  6654. zend_string_release_ex(resolved_name, 0);
  6655. return;
  6656. }
  6657. zend_string_release_ex(resolved_name, 0);
  6658. }
  6659. if (const_ast->kind == ZEND_AST_ZVAL && zend_string_equals_literal_ci(zend_ast_get_str(const_ast), "class")) {
  6660. zend_error_noreturn(E_COMPILE_ERROR,
  6661. "Dynamic class names are not allowed in compile-time ::class fetch");
  6662. }
  6663. zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
  6664. zend_compile_expr(&const_node, const_ast);
  6665. opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
  6666. zend_set_class_name_op1(opline, &class_node);
  6667. opline->extended_value = zend_alloc_polymorphic_cache_slot();
  6668. }
  6669. /* }}} */
  6670. void zend_compile_resolve_class_name(znode *result, zend_ast *ast) /* {{{ */
  6671. {
  6672. zend_ast *name_ast = ast->child[0];
  6673. uint32_t fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
  6674. zend_ensure_valid_class_fetch_type(fetch_type);
  6675. switch (fetch_type) {
  6676. case ZEND_FETCH_CLASS_SELF:
  6677. if (CG(active_class_entry) && zend_is_scope_known()) {
  6678. result->op_type = IS_CONST;
  6679. ZVAL_STR_COPY(&result->u.constant, CG(active_class_entry)->name);
  6680. } else {
  6681. zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  6682. opline->op1.num = fetch_type;
  6683. }
  6684. break;
  6685. case ZEND_FETCH_CLASS_STATIC:
  6686. case ZEND_FETCH_CLASS_PARENT:
  6687. {
  6688. zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  6689. opline->op1.num = fetch_type;
  6690. }
  6691. break;
  6692. case ZEND_FETCH_CLASS_DEFAULT:
  6693. result->op_type = IS_CONST;
  6694. ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
  6695. break;
  6696. EMPTY_SWITCH_DEFAULT_CASE()
  6697. }
  6698. }
  6699. /* }}} */
  6700. static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
  6701. {
  6702. zend_op *opline = get_next_op(CG(active_op_array));
  6703. if (num == 0) {
  6704. result->op_type = IS_TMP_VAR;
  6705. result->u.op.var = -1;
  6706. opline->opcode = ZEND_ROPE_INIT;
  6707. } else {
  6708. opline->opcode = ZEND_ROPE_ADD;
  6709. SET_NODE(opline->op1, result);
  6710. }
  6711. SET_NODE(opline->op2, elem_node);
  6712. SET_NODE(opline->result, result);
  6713. opline->extended_value = num;
  6714. return opline;
  6715. }
  6716. /* }}} */
  6717. static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
  6718. {
  6719. uint32_t i, j;
  6720. uint32_t rope_init_lineno = -1;
  6721. zend_op *opline = NULL, *init_opline;
  6722. znode elem_node, last_const_node;
  6723. zend_ast_list *list = zend_ast_get_list(ast);
  6724. ZEND_ASSERT(list->children > 0);
  6725. j = 0;
  6726. last_const_node.op_type = IS_UNUSED;
  6727. for (i = 0; i < list->children; i++) {
  6728. zend_compile_expr(&elem_node, list->child[i]);
  6729. if (elem_node.op_type == IS_CONST) {
  6730. convert_to_string(&elem_node.u.constant);
  6731. if (Z_STRLEN(elem_node.u.constant) == 0) {
  6732. zval_ptr_dtor(&elem_node.u.constant);
  6733. } else if (last_const_node.op_type == IS_CONST) {
  6734. concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
  6735. zval_ptr_dtor(&elem_node.u.constant);
  6736. } else {
  6737. last_const_node.op_type = IS_CONST;
  6738. ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
  6739. }
  6740. continue;
  6741. } else {
  6742. if (j == 0) {
  6743. rope_init_lineno = get_next_op_number(CG(active_op_array));
  6744. }
  6745. if (last_const_node.op_type == IS_CONST) {
  6746. zend_compile_rope_add(result, j++, &last_const_node);
  6747. last_const_node.op_type = IS_UNUSED;
  6748. }
  6749. opline = zend_compile_rope_add(result, j++, &elem_node);
  6750. }
  6751. }
  6752. if (j == 0) {
  6753. result->op_type = IS_CONST;
  6754. if (last_const_node.op_type == IS_CONST) {
  6755. ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
  6756. } else {
  6757. ZVAL_EMPTY_STRING(&result->u.constant);
  6758. /* empty string */
  6759. }
  6760. return;
  6761. } else if (last_const_node.op_type == IS_CONST) {
  6762. opline = zend_compile_rope_add(result, j++, &last_const_node);
  6763. }
  6764. init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
  6765. if (j == 1) {
  6766. if (opline->op2_type == IS_CONST) {
  6767. GET_NODE(result, opline->op2);
  6768. MAKE_NOP(opline);
  6769. } else {
  6770. opline->opcode = ZEND_CAST;
  6771. opline->extended_value = IS_STRING;
  6772. opline->op1_type = opline->op2_type;
  6773. opline->op1 = opline->op2;
  6774. opline->result_type = IS_TMP_VAR;
  6775. opline->result.var = get_temporary_variable(CG(active_op_array));
  6776. SET_UNUSED(opline->op2);
  6777. GET_NODE(result, opline->result);
  6778. }
  6779. } else if (j == 2) {
  6780. opline->opcode = ZEND_FAST_CONCAT;
  6781. opline->extended_value = 0;
  6782. opline->op1_type = init_opline->op2_type;
  6783. opline->op1 = init_opline->op2;
  6784. opline->result_type = IS_TMP_VAR;
  6785. opline->result.var = get_temporary_variable(CG(active_op_array));
  6786. MAKE_NOP(init_opline);
  6787. GET_NODE(result, opline->result);
  6788. } else {
  6789. uint32_t var;
  6790. uint32_t range = zend_start_live_range(CG(active_op_array), rope_init_lineno);
  6791. init_opline->extended_value = j;
  6792. opline->opcode = ZEND_ROPE_END;
  6793. opline->result.var = get_temporary_variable(CG(active_op_array));
  6794. var = opline->op1.var = get_temporary_variable(CG(active_op_array));
  6795. GET_NODE(result, opline->result);
  6796. /* Allocates the necessary number of zval slots to keep the rope */
  6797. i = ((j * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
  6798. while (i > 1) {
  6799. get_temporary_variable(CG(active_op_array));
  6800. i--;
  6801. }
  6802. zend_end_live_range(CG(active_op_array), range, opline - CG(active_op_array)->opcodes,
  6803. ZEND_LIVE_ROPE, var);
  6804. /* Update all the previous opcodes to use the same variable */
  6805. while (opline != init_opline) {
  6806. opline--;
  6807. if (opline->opcode == ZEND_ROPE_ADD &&
  6808. opline->result.var == (uint32_t)-1) {
  6809. opline->op1.var = var;
  6810. opline->result.var = var;
  6811. } else if (opline->opcode == ZEND_ROPE_INIT &&
  6812. opline->result.var == (uint32_t)-1) {
  6813. opline->result.var = var;
  6814. }
  6815. }
  6816. }
  6817. }
  6818. /* }}} */
  6819. void zend_compile_magic_const(znode *result, zend_ast *ast) /* {{{ */
  6820. {
  6821. zend_op *opline;
  6822. if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
  6823. result->op_type = IS_CONST;
  6824. return;
  6825. }
  6826. ZEND_ASSERT(ast->attr == T_CLASS_C &&
  6827. CG(active_class_entry) &&
  6828. (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
  6829. opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
  6830. opline->op1.num = ZEND_FETCH_CLASS_SELF;
  6831. }
  6832. /* }}} */
  6833. zend_bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
  6834. {
  6835. return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
  6836. || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
  6837. || kind == ZEND_AST_AND || kind == ZEND_AST_OR
  6838. || kind == ZEND_AST_UNARY_OP
  6839. || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
  6840. || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
  6841. || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
  6842. || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
  6843. || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE;
  6844. }
  6845. /* }}} */
  6846. void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
  6847. {
  6848. zend_ast *ast = *ast_ptr;
  6849. zend_ast *class_ast = ast->child[0];
  6850. zend_ast *const_ast = ast->child[1];
  6851. zend_string *class_name;
  6852. zend_string *const_name = zend_ast_get_str(const_ast);
  6853. zend_string *name;
  6854. zval result;
  6855. int fetch_type;
  6856. if (class_ast->kind != ZEND_AST_ZVAL) {
  6857. zend_error_noreturn(E_COMPILE_ERROR,
  6858. "Dynamic class names are not allowed in compile-time class constant references");
  6859. }
  6860. if (zend_try_compile_const_expr_resolve_class_name(&result, class_ast, const_ast, 1)) {
  6861. *ast_ptr = zend_ast_create_zval(&result);
  6862. return;
  6863. }
  6864. class_name = zend_ast_get_str(class_ast);
  6865. fetch_type = zend_get_class_fetch_type(class_name);
  6866. if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
  6867. zend_error_noreturn(E_COMPILE_ERROR,
  6868. "\"static::\" is not allowed in compile-time constants");
  6869. }
  6870. if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
  6871. class_name = zend_resolve_class_name_ast(class_ast);
  6872. } else {
  6873. zend_string_addref(class_name);
  6874. }
  6875. name = zend_concat3(
  6876. ZSTR_VAL(class_name), ZSTR_LEN(class_name), "::", 2, ZSTR_VAL(const_name), ZSTR_LEN(const_name));
  6877. zend_ast_destroy(ast);
  6878. zend_string_release_ex(class_name, 0);
  6879. *ast_ptr = zend_ast_create_constant(name, fetch_type | ZEND_FETCH_CLASS_EXCEPTION);
  6880. }
  6881. /* }}} */
  6882. void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
  6883. {
  6884. zend_ast *ast = *ast_ptr;
  6885. zend_ast *name_ast = ast->child[0];
  6886. zend_string *orig_name = zend_ast_get_str(name_ast);
  6887. zend_bool is_fully_qualified;
  6888. zval result;
  6889. zend_string *resolved_name;
  6890. resolved_name = zend_resolve_const_name(
  6891. orig_name, name_ast->attr, &is_fully_qualified);
  6892. if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
  6893. zend_string_release_ex(resolved_name, 0);
  6894. zend_ast_destroy(ast);
  6895. *ast_ptr = zend_ast_create_zval(&result);
  6896. return;
  6897. }
  6898. zend_ast_destroy(ast);
  6899. *ast_ptr = zend_ast_create_constant(resolved_name, !is_fully_qualified ? IS_CONSTANT_UNQUALIFIED : 0);
  6900. }
  6901. /* }}} */
  6902. void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
  6903. {
  6904. zend_ast *ast = *ast_ptr;
  6905. /* Other cases already resolved by constant folding */
  6906. ZEND_ASSERT(ast->attr == T_CLASS_C);
  6907. zend_ast_destroy(ast);
  6908. *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
  6909. }
  6910. /* }}} */
  6911. void zend_compile_const_expr(zend_ast **ast_ptr) /* {{{ */
  6912. {
  6913. zend_ast *ast = *ast_ptr;
  6914. if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
  6915. return;
  6916. }
  6917. if (!zend_is_allowed_in_const_expr(ast->kind)) {
  6918. zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
  6919. }
  6920. switch (ast->kind) {
  6921. case ZEND_AST_CLASS_CONST:
  6922. zend_compile_const_expr_class_const(ast_ptr);
  6923. break;
  6924. case ZEND_AST_CONST:
  6925. zend_compile_const_expr_const(ast_ptr);
  6926. break;
  6927. case ZEND_AST_MAGIC_CONST:
  6928. zend_compile_const_expr_magic_const(ast_ptr);
  6929. break;
  6930. default:
  6931. zend_ast_apply(ast, zend_compile_const_expr);
  6932. break;
  6933. }
  6934. }
  6935. /* }}} */
  6936. void zend_const_expr_to_zval(zval *result, zend_ast *ast) /* {{{ */
  6937. {
  6938. zend_ast *orig_ast = ast;
  6939. zend_eval_const_expr(&ast);
  6940. zend_compile_const_expr(&ast);
  6941. if (ast->kind == ZEND_AST_ZVAL) {
  6942. ZVAL_COPY_VALUE(result, zend_ast_get_zval(ast));
  6943. } else {
  6944. ZVAL_AST(result, zend_ast_copy(ast));
  6945. /* destroy the ast here, it might have been replaced */
  6946. zend_ast_destroy(ast);
  6947. }
  6948. /* Kill this branch of the original AST, as it was already destroyed.
  6949. * It would be nice to find a better solution to this problem in the
  6950. * future. */
  6951. orig_ast->kind = 0;
  6952. }
  6953. /* }}} */
  6954. /* Same as compile_stmt, but with early binding */
  6955. void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
  6956. {
  6957. if (!ast) {
  6958. return;
  6959. }
  6960. if (ast->kind == ZEND_AST_STMT_LIST) {
  6961. zend_ast_list *list = zend_ast_get_list(ast);
  6962. uint32_t i;
  6963. for (i = 0; i < list->children; ++i) {
  6964. zend_compile_top_stmt(list->child[i]);
  6965. }
  6966. return;
  6967. }
  6968. zend_compile_stmt(ast);
  6969. if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
  6970. zend_verify_namespace();
  6971. }
  6972. if (ast->kind == ZEND_AST_FUNC_DECL || ast->kind == ZEND_AST_CLASS) {
  6973. CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
  6974. zend_do_early_binding();
  6975. }
  6976. }
  6977. /* }}} */
  6978. void zend_compile_stmt(zend_ast *ast) /* {{{ */
  6979. {
  6980. if (!ast) {
  6981. return;
  6982. }
  6983. CG(zend_lineno) = ast->lineno;
  6984. if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) && !zend_is_unticked_stmt(ast)) {
  6985. zend_do_extended_info();
  6986. }
  6987. switch (ast->kind) {
  6988. case ZEND_AST_STMT_LIST:
  6989. zend_compile_stmt_list(ast);
  6990. break;
  6991. case ZEND_AST_GLOBAL:
  6992. zend_compile_global_var(ast);
  6993. break;
  6994. case ZEND_AST_STATIC:
  6995. zend_compile_static_var(ast);
  6996. break;
  6997. case ZEND_AST_UNSET:
  6998. zend_compile_unset(ast);
  6999. break;
  7000. case ZEND_AST_RETURN:
  7001. zend_compile_return(ast);
  7002. break;
  7003. case ZEND_AST_ECHO:
  7004. zend_compile_echo(ast);
  7005. break;
  7006. case ZEND_AST_THROW:
  7007. zend_compile_throw(ast);
  7008. break;
  7009. case ZEND_AST_BREAK:
  7010. case ZEND_AST_CONTINUE:
  7011. zend_compile_break_continue(ast);
  7012. break;
  7013. case ZEND_AST_GOTO:
  7014. zend_compile_goto(ast);
  7015. break;
  7016. case ZEND_AST_LABEL:
  7017. zend_compile_label(ast);
  7018. break;
  7019. case ZEND_AST_WHILE:
  7020. zend_compile_while(ast);
  7021. break;
  7022. case ZEND_AST_DO_WHILE:
  7023. zend_compile_do_while(ast);
  7024. break;
  7025. case ZEND_AST_FOR:
  7026. zend_compile_for(ast);
  7027. break;
  7028. case ZEND_AST_FOREACH:
  7029. zend_compile_foreach(ast);
  7030. break;
  7031. case ZEND_AST_IF:
  7032. zend_compile_if(ast);
  7033. break;
  7034. case ZEND_AST_SWITCH:
  7035. zend_compile_switch(ast);
  7036. break;
  7037. case ZEND_AST_TRY:
  7038. zend_compile_try(ast);
  7039. break;
  7040. case ZEND_AST_DECLARE:
  7041. zend_compile_declare(ast);
  7042. break;
  7043. case ZEND_AST_FUNC_DECL:
  7044. case ZEND_AST_METHOD:
  7045. zend_compile_func_decl(NULL, ast);
  7046. break;
  7047. case ZEND_AST_PROP_DECL:
  7048. zend_compile_prop_decl(ast);
  7049. break;
  7050. case ZEND_AST_CLASS_CONST_DECL:
  7051. zend_compile_class_const_decl(ast);
  7052. break;
  7053. case ZEND_AST_USE_TRAIT:
  7054. zend_compile_use_trait(ast);
  7055. break;
  7056. case ZEND_AST_CLASS:
  7057. zend_compile_class_decl(ast);
  7058. break;
  7059. case ZEND_AST_GROUP_USE:
  7060. zend_compile_group_use(ast);
  7061. break;
  7062. case ZEND_AST_USE:
  7063. zend_compile_use(ast);
  7064. break;
  7065. case ZEND_AST_CONST_DECL:
  7066. zend_compile_const_decl(ast);
  7067. break;
  7068. case ZEND_AST_NAMESPACE:
  7069. zend_compile_namespace(ast);
  7070. break;
  7071. case ZEND_AST_HALT_COMPILER:
  7072. zend_compile_halt_compiler(ast);
  7073. break;
  7074. default:
  7075. {
  7076. znode result;
  7077. zend_compile_expr(&result, ast);
  7078. zend_do_free(&result);
  7079. }
  7080. }
  7081. if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
  7082. zend_emit_tick();
  7083. }
  7084. }
  7085. /* }}} */
  7086. void zend_compile_expr(znode *result, zend_ast *ast) /* {{{ */
  7087. {
  7088. /* CG(zend_lineno) = ast->lineno; */
  7089. CG(zend_lineno) = zend_ast_get_lineno(ast);
  7090. switch (ast->kind) {
  7091. case ZEND_AST_ZVAL:
  7092. ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
  7093. result->op_type = IS_CONST;
  7094. return;
  7095. case ZEND_AST_ZNODE:
  7096. *result = *zend_ast_get_znode(ast);
  7097. return;
  7098. case ZEND_AST_VAR:
  7099. case ZEND_AST_DIM:
  7100. case ZEND_AST_PROP:
  7101. case ZEND_AST_STATIC_PROP:
  7102. case ZEND_AST_CALL:
  7103. case ZEND_AST_METHOD_CALL:
  7104. case ZEND_AST_STATIC_CALL:
  7105. zend_compile_var(result, ast, BP_VAR_R);
  7106. return;
  7107. case ZEND_AST_ASSIGN:
  7108. zend_compile_assign(result, ast);
  7109. return;
  7110. case ZEND_AST_ASSIGN_REF:
  7111. zend_compile_assign_ref(result, ast);
  7112. return;
  7113. case ZEND_AST_NEW:
  7114. zend_compile_new(result, ast);
  7115. return;
  7116. case ZEND_AST_CLONE:
  7117. zend_compile_clone(result, ast);
  7118. return;
  7119. case ZEND_AST_ASSIGN_OP:
  7120. zend_compile_compound_assign(result, ast);
  7121. return;
  7122. case ZEND_AST_BINARY_OP:
  7123. zend_compile_binary_op(result, ast);
  7124. return;
  7125. case ZEND_AST_GREATER:
  7126. case ZEND_AST_GREATER_EQUAL:
  7127. zend_compile_greater(result, ast);
  7128. return;
  7129. case ZEND_AST_UNARY_OP:
  7130. zend_compile_unary_op(result, ast);
  7131. return;
  7132. case ZEND_AST_UNARY_PLUS:
  7133. case ZEND_AST_UNARY_MINUS:
  7134. zend_compile_unary_pm(result, ast);
  7135. return;
  7136. case ZEND_AST_AND:
  7137. case ZEND_AST_OR:
  7138. zend_compile_short_circuiting(result, ast);
  7139. return;
  7140. case ZEND_AST_POST_INC:
  7141. case ZEND_AST_POST_DEC:
  7142. zend_compile_post_incdec(result, ast);
  7143. return;
  7144. case ZEND_AST_PRE_INC:
  7145. case ZEND_AST_PRE_DEC:
  7146. zend_compile_pre_incdec(result, ast);
  7147. return;
  7148. case ZEND_AST_CAST:
  7149. zend_compile_cast(result, ast);
  7150. return;
  7151. case ZEND_AST_CONDITIONAL:
  7152. zend_compile_conditional(result, ast);
  7153. return;
  7154. case ZEND_AST_COALESCE:
  7155. zend_compile_coalesce(result, ast);
  7156. return;
  7157. case ZEND_AST_PRINT:
  7158. zend_compile_print(result, ast);
  7159. return;
  7160. case ZEND_AST_EXIT:
  7161. zend_compile_exit(result, ast);
  7162. return;
  7163. case ZEND_AST_YIELD:
  7164. zend_compile_yield(result, ast);
  7165. return;
  7166. case ZEND_AST_YIELD_FROM:
  7167. zend_compile_yield_from(result, ast);
  7168. return;
  7169. case ZEND_AST_INSTANCEOF:
  7170. zend_compile_instanceof(result, ast);
  7171. return;
  7172. case ZEND_AST_INCLUDE_OR_EVAL:
  7173. zend_compile_include_or_eval(result, ast);
  7174. return;
  7175. case ZEND_AST_ISSET:
  7176. case ZEND_AST_EMPTY:
  7177. zend_compile_isset_or_empty(result, ast);
  7178. return;
  7179. case ZEND_AST_SILENCE:
  7180. zend_compile_silence(result, ast);
  7181. return;
  7182. case ZEND_AST_SHELL_EXEC:
  7183. zend_compile_shell_exec(result, ast);
  7184. return;
  7185. case ZEND_AST_ARRAY:
  7186. zend_compile_array(result, ast);
  7187. return;
  7188. case ZEND_AST_CONST:
  7189. zend_compile_const(result, ast);
  7190. return;
  7191. case ZEND_AST_CLASS_CONST:
  7192. zend_compile_class_const(result, ast);
  7193. return;
  7194. case ZEND_AST_ENCAPS_LIST:
  7195. zend_compile_encaps_list(result, ast);
  7196. return;
  7197. case ZEND_AST_MAGIC_CONST:
  7198. zend_compile_magic_const(result, ast);
  7199. return;
  7200. case ZEND_AST_CLOSURE:
  7201. zend_compile_func_decl(result, ast);
  7202. return;
  7203. default:
  7204. ZEND_ASSERT(0 /* not supported */);
  7205. }
  7206. }
  7207. /* }}} */
  7208. void zend_compile_var(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  7209. {
  7210. CG(zend_lineno) = zend_ast_get_lineno(ast);
  7211. switch (ast->kind) {
  7212. case ZEND_AST_VAR:
  7213. zend_compile_simple_var(result, ast, type, 0);
  7214. return;
  7215. case ZEND_AST_DIM:
  7216. zend_compile_dim(result, ast, type);
  7217. return;
  7218. case ZEND_AST_PROP:
  7219. zend_compile_prop(result, ast, type);
  7220. return;
  7221. case ZEND_AST_STATIC_PROP:
  7222. zend_compile_static_prop(result, ast, type, 0);
  7223. return;
  7224. case ZEND_AST_CALL:
  7225. zend_compile_call(result, ast, type);
  7226. return;
  7227. case ZEND_AST_METHOD_CALL:
  7228. zend_compile_method_call(result, ast, type);
  7229. return;
  7230. case ZEND_AST_STATIC_CALL:
  7231. zend_compile_static_call(result, ast, type);
  7232. return;
  7233. case ZEND_AST_ZNODE:
  7234. *result = *zend_ast_get_znode(ast);
  7235. return;
  7236. default:
  7237. if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
  7238. zend_error_noreturn(E_COMPILE_ERROR,
  7239. "Cannot use temporary expression in write context");
  7240. }
  7241. zend_compile_expr(result, ast);
  7242. return;
  7243. }
  7244. }
  7245. /* }}} */
  7246. void zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
  7247. {
  7248. switch (ast->kind) {
  7249. case ZEND_AST_VAR:
  7250. zend_compile_simple_var(result, ast, type, 1);
  7251. return;
  7252. case ZEND_AST_DIM:
  7253. zend_delayed_compile_dim(result, ast, type);
  7254. return;
  7255. case ZEND_AST_PROP:
  7256. zend_delayed_compile_prop(result, ast, type);
  7257. return;
  7258. case ZEND_AST_STATIC_PROP:
  7259. zend_compile_static_prop(result, ast, type, 1);
  7260. return;
  7261. default:
  7262. zend_compile_var(result, ast, type);
  7263. return;
  7264. }
  7265. }
  7266. /* }}} */
  7267. void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
  7268. {
  7269. zend_ast *ast = *ast_ptr;
  7270. zval result;
  7271. if (!ast) {
  7272. return;
  7273. }
  7274. switch (ast->kind) {
  7275. case ZEND_AST_BINARY_OP:
  7276. zend_eval_const_expr(&ast->child[0]);
  7277. zend_eval_const_expr(&ast->child[1]);
  7278. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  7279. return;
  7280. }
  7281. if (!zend_try_ct_eval_binary_op(&result, ast->attr,
  7282. zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
  7283. ) {
  7284. return;
  7285. }
  7286. break;
  7287. case ZEND_AST_GREATER:
  7288. case ZEND_AST_GREATER_EQUAL:
  7289. zend_eval_const_expr(&ast->child[0]);
  7290. zend_eval_const_expr(&ast->child[1]);
  7291. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  7292. return;
  7293. }
  7294. zend_ct_eval_greater(&result, ast->kind,
  7295. zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
  7296. break;
  7297. case ZEND_AST_AND:
  7298. case ZEND_AST_OR:
  7299. {
  7300. zend_bool child0_is_true, child1_is_true;
  7301. zend_eval_const_expr(&ast->child[0]);
  7302. zend_eval_const_expr(&ast->child[1]);
  7303. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7304. return;
  7305. }
  7306. child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
  7307. if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
  7308. ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
  7309. break;
  7310. }
  7311. if (ast->child[1]->kind != ZEND_AST_ZVAL) {
  7312. return;
  7313. }
  7314. child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
  7315. if (ast->kind == ZEND_AST_OR) {
  7316. ZVAL_BOOL(&result, child0_is_true || child1_is_true);
  7317. } else {
  7318. ZVAL_BOOL(&result, child0_is_true && child1_is_true);
  7319. }
  7320. break;
  7321. }
  7322. case ZEND_AST_UNARY_OP:
  7323. zend_eval_const_expr(&ast->child[0]);
  7324. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7325. return;
  7326. }
  7327. zend_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]));
  7328. break;
  7329. case ZEND_AST_UNARY_PLUS:
  7330. case ZEND_AST_UNARY_MINUS:
  7331. zend_eval_const_expr(&ast->child[0]);
  7332. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7333. return;
  7334. }
  7335. if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
  7336. return;
  7337. }
  7338. break;
  7339. case ZEND_AST_COALESCE:
  7340. /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
  7341. if (ast->child[0]->kind == ZEND_AST_DIM) {
  7342. ast->child[0]->attr = ZEND_DIM_IS;
  7343. }
  7344. zend_eval_const_expr(&ast->child[0]);
  7345. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7346. /* ensure everything was compile-time evaluated at least once */
  7347. zend_eval_const_expr(&ast->child[1]);
  7348. return;
  7349. }
  7350. if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
  7351. zend_eval_const_expr(&ast->child[1]);
  7352. *ast_ptr = ast->child[1];
  7353. ast->child[1] = NULL;
  7354. zend_ast_destroy(ast);
  7355. } else {
  7356. *ast_ptr = ast->child[0];
  7357. ast->child[0] = NULL;
  7358. zend_ast_destroy(ast);
  7359. }
  7360. return;
  7361. case ZEND_AST_CONDITIONAL:
  7362. {
  7363. zend_ast **child, *child_ast;
  7364. zend_eval_const_expr(&ast->child[0]);
  7365. if (ast->child[0]->kind != ZEND_AST_ZVAL) {
  7366. /* ensure everything was compile-time evaluated at least once */
  7367. if (ast->child[1]) {
  7368. zend_eval_const_expr(&ast->child[1]);
  7369. }
  7370. zend_eval_const_expr(&ast->child[2]);
  7371. return;
  7372. }
  7373. child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
  7374. if (*child == NULL) {
  7375. child--;
  7376. }
  7377. child_ast = *child;
  7378. *child = NULL;
  7379. zend_ast_destroy(ast);
  7380. *ast_ptr = child_ast;
  7381. zend_eval_const_expr(ast_ptr);
  7382. return;
  7383. }
  7384. case ZEND_AST_DIM:
  7385. {
  7386. /* constant expression should be always read context ... */
  7387. zval *container, *dim;
  7388. if (ast->child[1] == NULL) {
  7389. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  7390. }
  7391. /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
  7392. if (ast->attr == ZEND_DIM_IS && ast->child[0]->kind == ZEND_AST_DIM) {
  7393. ast->child[0]->attr = ZEND_DIM_IS;
  7394. }
  7395. zend_eval_const_expr(&ast->child[0]);
  7396. zend_eval_const_expr(&ast->child[1]);
  7397. if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
  7398. return;
  7399. }
  7400. container = zend_ast_get_zval(ast->child[0]);
  7401. dim = zend_ast_get_zval(ast->child[1]);
  7402. if (Z_TYPE_P(container) == IS_ARRAY) {
  7403. zval *el;
  7404. if (Z_TYPE_P(dim) == IS_LONG) {
  7405. el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
  7406. if (el) {
  7407. ZVAL_COPY(&result, el);
  7408. } else {
  7409. return;
  7410. }
  7411. } else if (Z_TYPE_P(dim) == IS_STRING) {
  7412. el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
  7413. if (el) {
  7414. ZVAL_COPY(&result, el);
  7415. } else {
  7416. return;
  7417. }
  7418. } else {
  7419. return; /* warning... handle at runtime */
  7420. }
  7421. } else if (Z_TYPE_P(container) == IS_STRING) {
  7422. zend_long offset;
  7423. zend_uchar c;
  7424. if (Z_TYPE_P(dim) == IS_LONG) {
  7425. offset = Z_LVAL_P(dim);
  7426. } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
  7427. return;
  7428. }
  7429. if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
  7430. return;
  7431. }
  7432. c = (zend_uchar) Z_STRVAL_P(container)[offset];
  7433. ZVAL_INTERNED_STR(&result, ZSTR_CHAR(c));
  7434. } else if (Z_TYPE_P(container) <= IS_FALSE) {
  7435. ZVAL_NULL(&result);
  7436. } else {
  7437. return;
  7438. }
  7439. break;
  7440. }
  7441. case ZEND_AST_ARRAY:
  7442. if (!zend_try_ct_eval_array(&result, ast)) {
  7443. return;
  7444. }
  7445. break;
  7446. case ZEND_AST_MAGIC_CONST:
  7447. if (!zend_try_ct_eval_magic_const(&result, ast)) {
  7448. return;
  7449. }
  7450. break;
  7451. case ZEND_AST_CONST:
  7452. {
  7453. zend_ast *name_ast = ast->child[0];
  7454. zend_bool is_fully_qualified;
  7455. zend_string *resolved_name = zend_resolve_const_name(
  7456. zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
  7457. if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
  7458. zend_string_release_ex(resolved_name, 0);
  7459. return;
  7460. }
  7461. zend_string_release_ex(resolved_name, 0);
  7462. break;
  7463. }
  7464. case ZEND_AST_CLASS_CONST:
  7465. {
  7466. zend_ast *class_ast = ast->child[0];
  7467. zend_ast *name_ast = ast->child[1];
  7468. zend_string *resolved_name;
  7469. if (zend_try_compile_const_expr_resolve_class_name(&result, class_ast, name_ast, 0)) {
  7470. if (Z_TYPE(result) == IS_NULL) {
  7471. if (zend_get_class_fetch_type(zend_ast_get_str(class_ast)) == ZEND_FETCH_CLASS_SELF) {
  7472. zend_ast_destroy(ast);
  7473. *ast_ptr = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_CLASS_C);
  7474. }
  7475. return;
  7476. }
  7477. break;
  7478. }
  7479. zend_eval_const_expr(&ast->child[0]);
  7480. zend_eval_const_expr(&ast->child[1]);
  7481. class_ast = ast->child[0];
  7482. name_ast = ast->child[1];
  7483. if (name_ast->kind == ZEND_AST_ZVAL && zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "class")) {
  7484. zend_error_noreturn(E_COMPILE_ERROR,
  7485. "Dynamic class names are not allowed in compile-time ::class fetch");
  7486. }
  7487. if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
  7488. return;
  7489. }
  7490. resolved_name = zend_resolve_class_name_ast(class_ast);
  7491. if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
  7492. zend_string_release_ex(resolved_name, 0);
  7493. return;
  7494. }
  7495. zend_string_release_ex(resolved_name, 0);
  7496. break;
  7497. }
  7498. default:
  7499. return;
  7500. }
  7501. zend_ast_destroy(ast);
  7502. *ast_ptr = zend_ast_create_zval(&result);
  7503. }
  7504. /* }}} */
  7505. /*
  7506. * Local variables:
  7507. * tab-width: 4
  7508. * c-basic-offset: 4
  7509. * indent-tabs-mode: t
  7510. * End:
  7511. * vim600: sw=4 ts=4 fdm=marker
  7512. * vim<600: sw=4 ts=4
  7513. */