as_compiler.cpp 517 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2021 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. // Modified for Urho3D
  24. //
  25. // as_compiler.cpp
  26. //
  27. // The class that does the actual compilation of the functions
  28. //
  29. #include <math.h> // fmodf() pow()
  30. #include "as_config.h"
  31. #ifndef AS_NO_COMPILER
  32. #include "as_compiler.h"
  33. #include "as_tokendef.h"
  34. #include "as_tokenizer.h"
  35. #include "as_string_util.h"
  36. #include "as_texts.h"
  37. #include "as_parser.h"
  38. #include "as_debug.h"
  39. #include "as_context.h" // as_powi()
  40. BEGIN_AS_NAMESPACE
  41. //
  42. // The calling convention rules for script functions:
  43. // - If a class method returns a reference, the caller must guarantee the object pointer stays alive until the function returns, and the reference is no longer going to be used
  44. // - If a class method doesn't return a reference, it must guarantee by itself that the this pointer stays alive during the function call. If no outside access is made, then the function is guaranteed to stay alive and nothing needs to be done
  45. // - The object pointer is always passed as the first argument, position 0
  46. // - If the function returns a value type the caller must reserve the memory for this and pass the pointer as the first argument after the object pointer
  47. //
  48. // TODO: I must correct the interpretation of a reference to objects in the compiler.
  49. // A reference should mean that a pointer to the object is on the stack.
  50. // No expression should end up as non-references to objects, as the actual object is
  51. // never put on the stack.
  52. // Local variables are declared as non-references, but the expression should be a reference to the variable.
  53. // Function parameters of called functions can also be non-references, but in that case it means the
  54. // object will be passed by value (currently on the heap, which will be moved to the application stack).
  55. //
  56. // The compiler shouldn't use the asCDataType::IsReference. The datatype should always be stored as non-references.
  57. // Instead the compiler should keep track of references in TypeInfo, where it should also state how the reference
  58. // is currently stored, i.e. in variable, in register, on stack, etc.
  59. asCCompiler::asCCompiler(asCScriptEngine *engine) : byteCode(engine)
  60. {
  61. builder = 0;
  62. script = 0;
  63. variables = 0;
  64. isProcessingDeferredParams = false;
  65. isCompilingDefaultArg = false;
  66. noCodeOutput = 0;
  67. }
  68. asCCompiler::~asCCompiler()
  69. {
  70. while( variables )
  71. {
  72. asCVariableScope *var = variables;
  73. variables = variables->parent;
  74. asDELETE(var,asCVariableScope);
  75. }
  76. // Clean up all the string constants that were allocated. By now the script
  77. // functions that were compiled successfully already holds their own references
  78. for (asUINT n = 0; n < usedStringConstants.GetLength(); n++)
  79. engine->stringFactory->ReleaseStringConstant(usedStringConstants[n]);
  80. usedStringConstants.SetLength(0);
  81. // Clean up the temporary script nodes that were allocated during compilation
  82. for (asUINT n = 0; n < nodesToFreeUponComplete.GetLength(); n++)
  83. nodesToFreeUponComplete[n]->Destroy(engine);
  84. }
  85. void asCCompiler::Reset(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptFunction *in_outFunc)
  86. {
  87. this->builder = in_builder;
  88. this->engine = in_builder->engine;
  89. this->script = in_script;
  90. this->outFunc = in_outFunc;
  91. hasCompileErrors = false;
  92. m_isConstructor = false;
  93. m_isConstructorCalled = false;
  94. m_classDecl = 0;
  95. m_globalVar = 0;
  96. nextLabel = 0;
  97. breakLabels.SetLength(0);
  98. continueLabels.SetLength(0);
  99. numLambdas = 0;
  100. byteCode.ClearAll();
  101. }
  102. int asCCompiler::CompileDefaultConstructor(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptNode *in_node, asCScriptFunction *in_outFunc, sClassDeclaration *in_classDecl)
  103. {
  104. Reset(in_builder, in_script, in_outFunc);
  105. m_classDecl = in_classDecl;
  106. // Insert a JitEntry at the start of the function for JIT compilers
  107. byteCode.InstrPTR(asBC_JitEntry, 0);
  108. // Add a variable scope that might be needed to declare dummy variables
  109. // in case the member initialization refers to undefined symbols.
  110. AddVariableScope();
  111. // Initialize the class members that have no explicit expression first. This will allow the
  112. // base class' constructor to access these members without worry they will be uninitialized.
  113. // This can happen if the base class' constructor calls a method that is overridden by the derived class
  114. CompileMemberInitialization(&byteCode, true);
  115. // If the class is derived from another, then the base class' default constructor must be called
  116. if( outFunc->objectType->derivedFrom )
  117. {
  118. // Make sure the base class really has a default constructor
  119. if( outFunc->objectType->derivedFrom->beh.construct == 0 )
  120. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, in_node);
  121. // Call the base class' default constructor
  122. byteCode.InstrSHORT(asBC_PSF, 0);
  123. byteCode.Instr(asBC_RDSPtr);
  124. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  125. }
  126. // Initialize the class members that explicit expressions afterwards. This allow the expressions
  127. // to access the base class members without worry they will be uninitialized
  128. CompileMemberInitialization(&byteCode, false);
  129. byteCode.OptimizeLocally(tempVariableOffsets);
  130. // If there are compile errors, there is no reason to build the final code
  131. if( hasCompileErrors )
  132. return -1;
  133. // Pop the object pointer from the stack
  134. byteCode.Ret(AS_PTR_SIZE);
  135. // Count total variable size
  136. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  137. outFunc->scriptData->variableSpace = varSize;
  138. FinalizeFunction();
  139. #ifdef AS_DEBUG
  140. // DEBUG: output byte code
  141. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + "__defconstr.txt").AddressOf(), in_outFunc);
  142. #endif
  143. return 0;
  144. }
  145. int asCCompiler::CompileFactory(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptFunction *in_outFunc)
  146. {
  147. Reset(in_builder, in_script, in_outFunc);
  148. // Insert a JitEntry at the start of the function for JIT compilers
  149. byteCode.InstrPTR(asBC_JitEntry, 0);
  150. // Find the corresponding constructor
  151. asCDataType dt = asCDataType::CreateType(outFunc->returnType.GetTypeInfo(), false);
  152. int constructor = 0;
  153. for( unsigned int n = 0; n < dt.GetBehaviour()->factories.GetLength(); n++ )
  154. {
  155. if( dt.GetBehaviour()->factories[n] == outFunc->id )
  156. {
  157. constructor = dt.GetBehaviour()->constructors[n];
  158. break;
  159. }
  160. }
  161. // Allocate the class and instantiate it with the constructor
  162. int varOffset = AllocateVariable(dt, true);
  163. outFunc->scriptData->variableSpace = AS_PTR_SIZE;
  164. byteCode.InstrSHORT(asBC_PSF, (short)varOffset);
  165. // Copy all arguments to the top of the stack
  166. // TODO: runtime optimize: Might be interesting to have a specific instruction for copying all arguments
  167. int offset = (int)outFunc->GetSpaceNeededForArguments();
  168. for( int a = int(outFunc->parameterTypes.GetLength()) - 1; a >= 0; a-- )
  169. {
  170. if( !outFunc->parameterTypes[a].IsPrimitive() ||
  171. outFunc->parameterTypes[a].IsReference() )
  172. {
  173. offset -= AS_PTR_SIZE;
  174. byteCode.InstrSHORT(asBC_PshVPtr, short(-offset));
  175. }
  176. else
  177. {
  178. if( outFunc->parameterTypes[a].GetSizeOnStackDWords() == 2 )
  179. {
  180. offset -= 2;
  181. byteCode.InstrSHORT(asBC_PshV8, short(-offset));
  182. }
  183. else
  184. {
  185. offset -= 1;
  186. byteCode.InstrSHORT(asBC_PshV4, short(-offset));
  187. }
  188. }
  189. }
  190. int argDwords = (int)outFunc->GetSpaceNeededForArguments();
  191. byteCode.Alloc(asBC_ALLOC, dt.GetTypeInfo(), constructor, argDwords + AS_PTR_SIZE);
  192. // Return a handle to the newly created object
  193. byteCode.InstrSHORT(asBC_LOADOBJ, (short)varOffset);
  194. byteCode.Ret(argDwords);
  195. FinalizeFunction();
  196. // Tell the virtual machine not to clean up parameters on exception
  197. outFunc->dontCleanUpOnException = true;
  198. /*
  199. #ifdef AS_DEBUG
  200. // DEBUG: output byte code
  201. asCString args;
  202. args.Format("%d", outFunc->parameterTypes.GetLength());
  203. byteCode.DebugOutput(("__" + outFunc->name + "__factory" + args + ".txt").AddressOf(), engine);
  204. #endif
  205. */
  206. return 0;
  207. }
  208. void asCCompiler::FinalizeFunction()
  209. {
  210. TimeIt("asCCompiler::FinalizeFunction");
  211. asASSERT( outFunc->scriptData );
  212. asUINT n;
  213. // Finalize the bytecode
  214. byteCode.Finalize(tempVariableOffsets);
  215. // extract the try/catch info before object variable info, as
  216. // some variable info is not needed if there are no try/catch blocks
  217. byteCode.ExtractTryCatchInfo(outFunc);
  218. byteCode.ExtractObjectVariableInfo(outFunc);
  219. // Compile the list of object variables for the exception handler
  220. // Start with the variables allocated on the heap, and then the ones allocated on the stack
  221. for( n = 0; n < variableAllocations.GetLength(); n++ )
  222. {
  223. if( (variableAllocations[n].IsObject() || variableAllocations[n].IsFuncdef()) && !variableAllocations[n].IsReference() )
  224. {
  225. if( variableIsOnHeap[n] )
  226. {
  227. outFunc->scriptData->objVariableTypes.PushLast(variableAllocations[n].GetTypeInfo());
  228. outFunc->scriptData->objVariablePos.PushLast(GetVariableOffset(n));
  229. }
  230. }
  231. }
  232. outFunc->scriptData->objVariablesOnHeap = asUINT(outFunc->scriptData->objVariablePos.GetLength());
  233. for( n = 0; n < variableAllocations.GetLength(); n++ )
  234. {
  235. if( (variableAllocations[n].IsObject() || variableAllocations[n].IsFuncdef()) && !variableAllocations[n].IsReference() )
  236. {
  237. if( !variableIsOnHeap[n] )
  238. {
  239. outFunc->scriptData->objVariableTypes.PushLast(variableAllocations[n].GetTypeInfo());
  240. outFunc->scriptData->objVariablePos.PushLast(GetVariableOffset(n));
  241. }
  242. }
  243. }
  244. // Copy byte code to the function
  245. asASSERT( outFunc->scriptData->byteCode.GetLength() == 0 );
  246. outFunc->scriptData->byteCode.SetLength(byteCode.GetSize());
  247. byteCode.Output(outFunc->scriptData->byteCode.AddressOf());
  248. outFunc->AddReferences();
  249. outFunc->scriptData->stackNeeded = byteCode.largestStackUsed + outFunc->scriptData->variableSpace;
  250. outFunc->scriptData->lineNumbers = byteCode.lineNumbers;
  251. // Extract the script section indexes too if there are any entries that are different from the function's script section
  252. int lastIdx = outFunc->scriptData->scriptSectionIdx;
  253. for( n = 0; n < byteCode.sectionIdxs.GetLength(); n++ )
  254. {
  255. if( byteCode.sectionIdxs[n] != lastIdx )
  256. {
  257. lastIdx = byteCode.sectionIdxs[n];
  258. outFunc->scriptData->sectionIdxs.PushLast(byteCode.lineNumbers[n*2]);
  259. outFunc->scriptData->sectionIdxs.PushLast(lastIdx);
  260. }
  261. }
  262. }
  263. // internal
  264. int asCCompiler::SetupParametersAndReturnVariable(asCArray<asCString> &parameterNames, asCScriptNode *func)
  265. {
  266. int stackPos = 0;
  267. if( outFunc->objectType )
  268. stackPos = -AS_PTR_SIZE; // The first parameter is the pointer to the object
  269. // Add the first variable scope, which the parameters and
  270. // variables declared in the outermost statement block is
  271. // part of.
  272. AddVariableScope();
  273. bool isDestructor = false;
  274. asCDataType returnType;
  275. // Examine return type
  276. returnType = outFunc->returnType;
  277. // Check if this is a constructor or destructor
  278. if( returnType.GetTokenType() == ttVoid && outFunc->objectType )
  279. {
  280. if( outFunc->name[0] == '~' )
  281. isDestructor = true;
  282. else if( outFunc->objectType->name == outFunc->name )
  283. m_isConstructor = true;
  284. }
  285. // Is the return type allowed?
  286. if( returnType != asCDataType::CreatePrimitive(ttVoid, false) &&
  287. !returnType.CanBeInstantiated() &&
  288. !returnType.IsReference() &&
  289. !returnType.IsObjectHandle() )
  290. {
  291. // TODO: Hasn't this been validated by the builder already?
  292. asCString str;
  293. str.Format(TXT_RETURN_CANT_BE_s, returnType.Format(outFunc->nameSpace).AddressOf());
  294. Error(str, func);
  295. }
  296. // If the return type is a value type returned by value the address of the
  297. // location where the value will be stored is pushed on the stack before
  298. // the arguments
  299. if( !(isDestructor || m_isConstructor) && outFunc->DoesReturnOnStack() )
  300. stackPos -= AS_PTR_SIZE;
  301. asCVariableScope vs(0);
  302. // Declare parameters
  303. asUINT n;
  304. for( n = 0; n < parameterNames.GetLength(); n++ )
  305. {
  306. // Get the parameter type
  307. asCDataType &type = outFunc->parameterTypes[n];
  308. asETypeModifiers inoutFlag = n < outFunc->inOutFlags.GetLength() ? outFunc->inOutFlags[n] : asTM_NONE;
  309. // Is the data type allowed?
  310. // TODO: Hasn't this been validated by the builder already?
  311. if( (type.IsReference() && inoutFlag != asTM_INOUTREF && !type.CanBeInstantiated()) ||
  312. (!type.IsReference() && !type.CanBeInstantiated()) )
  313. {
  314. asCString parm = type.Format(outFunc->nameSpace);
  315. if( inoutFlag == asTM_INREF )
  316. parm += "in";
  317. else if( inoutFlag == asTM_OUTREF )
  318. parm += "out";
  319. asCString str;
  320. str.Format(TXT_PARAMETER_CANT_BE_s, parm.AddressOf());
  321. Error(str, func);
  322. }
  323. // If the parameter has a name then declare it as variable
  324. if( parameterNames[n] != "" )
  325. {
  326. asCString &name = parameterNames[n];
  327. if( vs.DeclareVariable(name.AddressOf(), type, stackPos, true) < 0 )
  328. {
  329. // TODO: It might be an out-of-memory too
  330. Error(TXT_PARAMETER_ALREADY_DECLARED, func);
  331. }
  332. // Add marker for variable declaration
  333. byteCode.VarDecl((int)outFunc->scriptData->variables.GetLength());
  334. outFunc->AddVariable(name, type, stackPos);
  335. }
  336. else
  337. vs.DeclareVariable("", type, stackPos, true);
  338. // Move to next parameter
  339. stackPos -= type.GetSizeOnStackDWords();
  340. }
  341. for( n = asUINT(vs.variables.GetLength()); n-- > 0; )
  342. variables->DeclareVariable(vs.variables[n]->name.AddressOf(), vs.variables[n]->type, vs.variables[n]->stackOffset, vs.variables[n]->onHeap);
  343. variables->DeclareVariable("return", returnType, stackPos, true);
  344. return stackPos;
  345. }
  346. void asCCompiler::CompileMemberInitialization(asCByteCode *bc, bool onlyDefaults)
  347. {
  348. asASSERT( m_classDecl );
  349. // Initialize each member in the order they were declared
  350. for( asUINT n = 0; n < outFunc->objectType->properties.GetLength(); n++ )
  351. {
  352. asCObjectProperty *prop = outFunc->objectType->properties[n];
  353. // Check if the property has an initialization expression
  354. asCParser parser(builder);
  355. asCScriptNode *declNode = 0;
  356. asCScriptNode *initNode = 0;
  357. asCScriptCode *initScript = 0;
  358. for( asUINT m = 0; m < m_classDecl->propInits.GetLength(); m++ )
  359. {
  360. if( m_classDecl->propInits[m].name == prop->name )
  361. {
  362. declNode = m_classDecl->propInits[m].declNode;
  363. initNode = m_classDecl->propInits[m].initNode;
  364. initScript = m_classDecl->propInits[m].file;
  365. break;
  366. }
  367. }
  368. // If declNode is null, the property was inherited in which case
  369. // it was already initialized by the base class' constructor
  370. if( declNode )
  371. {
  372. if( initNode )
  373. {
  374. if( onlyDefaults )
  375. continue;
  376. #ifdef AS_NO_MEMBER_INIT
  377. // Give an error as the initialization in the declaration has been disabled
  378. asCScriptCode *origScript = script;
  379. script = initScript;
  380. Error("Initialization of members in declaration is not supported", initNode);
  381. script = origScript;
  382. // Clear the initialization node
  383. initNode = 0;
  384. initScript = script;
  385. #else
  386. // Re-parse the initialization expression as the parser now knows the types, which it didn't earlier
  387. int r = parser.ParseVarInit(initScript, initNode);
  388. if( r < 0 )
  389. continue;
  390. initNode = parser.GetScriptNode();
  391. #endif
  392. }
  393. else
  394. {
  395. if( !onlyDefaults )
  396. continue;
  397. }
  398. #ifdef AS_NO_MEMBER_INIT
  399. // The initialization will be done in the asCScriptObject constructor, so
  400. // here we should just validate that the member has a default constructor
  401. if( prop->type.IsObject() &&
  402. !prop->type.IsObjectHandle() &&
  403. (((prop->type.GetTypeInfo()->flags & asOBJ_REF) &&
  404. prop->type.GetBehaviour()->factory == 0) ||
  405. ((prop->type.GetTypeInfo()->flags & asOBJ_VALUE) &&
  406. prop->type.GetBehaviour()->construct == 0 &&
  407. !(prop->type.GetTypeInfo()->flags & asOBJ_POD))) )
  408. {
  409. // Class has no default factory/constructor.
  410. asCString str;
  411. // TODO: funcdef: asCDataType should have a GetTypeName()
  412. if( prop->type.GetFuncDef() )
  413. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, prop->type.GetFuncDef()->GetName());
  414. else
  415. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, prop->type.GetTypeInfo()->GetName());
  416. Error(str, declNode);
  417. }
  418. #else
  419. // Temporarily set the script that is being compiled to where the member initialization is declared.
  420. // The script can be different when including mixin classes from a different script section
  421. asCScriptCode *origScript = script;
  422. script = initScript;
  423. // Add a line instruction with the position of the declaration
  424. LineInstr(bc, declNode->tokenPos);
  425. // Compile the initialization
  426. asQWORD constantValue;
  427. asCByteCode bcInit(engine);
  428. CompileInitialization(initNode, &bcInit, prop->type, declNode, prop->byteOffset, &constantValue, 2);
  429. bcInit.OptimizeLocally(tempVariableOffsets);
  430. bc->AddCode(&bcInit);
  431. script = origScript;
  432. #endif
  433. }
  434. }
  435. }
  436. // Entry
  437. int asCCompiler::CompileFunction(asCBuilder *in_builder, asCScriptCode *in_script, asCArray<asCString> &in_parameterNames, asCScriptNode *in_func, asCScriptFunction *in_outFunc, sClassDeclaration *in_classDecl)
  438. {
  439. TimeIt("asCCompiler::CompileFunction");
  440. Reset(in_builder, in_script, in_outFunc);
  441. int buildErrors = builder->numErrors;
  442. int stackPos = SetupParametersAndReturnVariable(in_parameterNames, in_func);
  443. //--------------------------------------------
  444. // Compile the statement block
  445. if( m_isConstructor )
  446. m_classDecl = in_classDecl;
  447. // We need to parse the statement block now
  448. asCScriptNode *blockBegin;
  449. // If the function signature was implicit, e.g. virtual property accessor or
  450. // lambda function, then the received node already is the statement block
  451. if( in_func->nodeType != snStatementBlock )
  452. blockBegin = in_func->lastChild;
  453. else
  454. blockBegin = in_func;
  455. // TODO: memory: We can parse the statement block one statement at a time, thus save even more memory
  456. // TODO: optimize: For large functions, the parsing of the statement block can take a long time. Presumably because a lot of memory needs to be allocated
  457. asCParser parser(builder);
  458. int r = parser.ParseStatementBlock(script, blockBegin);
  459. if( r < 0 ) return -1;
  460. asCScriptNode *block = parser.GetScriptNode();
  461. // Reserve a label for the cleanup code
  462. nextLabel++;
  463. bool hasReturn;
  464. asCByteCode bc(engine);
  465. LineInstr(&bc, blockBegin->tokenPos);
  466. CompileStatementBlock(block, false, &hasReturn, &bc);
  467. LineInstr(&bc, blockBegin->tokenPos + blockBegin->tokenLength);
  468. // Make sure there is a return in all paths (if not return type is void)
  469. // Don't bother with this check if there are compiler errors, e.g. Unreachable code
  470. if( !hasCompileErrors && outFunc->returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  471. {
  472. if( hasReturn == false )
  473. Error(TXT_NOT_ALL_PATHS_RETURN, blockBegin);
  474. }
  475. //------------------------------------------------
  476. // Concatenate the bytecode
  477. // Insert a JitEntry at the start of the function for JIT compilers
  478. byteCode.InstrPTR(asBC_JitEntry, 0);
  479. if( outFunc->objectType )
  480. {
  481. if( m_isConstructor )
  482. {
  483. if( outFunc->objectType->derivedFrom )
  484. {
  485. // Call the base class' default constructor unless called manually in the code
  486. if( !m_isConstructorCalled )
  487. {
  488. if( outFunc->objectType->derivedFrom->beh.construct )
  489. {
  490. // Initialize members without explicit expression first
  491. CompileMemberInitialization(&byteCode, true);
  492. // Call base class' constructor
  493. asCByteCode tmpBC(engine);
  494. tmpBC.InstrSHORT(asBC_PSF, 0);
  495. tmpBC.Instr(asBC_RDSPtr);
  496. tmpBC.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  497. tmpBC.OptimizeLocally(tempVariableOffsets);
  498. byteCode.AddCode(&tmpBC);
  499. // Add the initialization of the members with explicit expressions
  500. CompileMemberInitialization(&byteCode, false);
  501. }
  502. else
  503. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, blockBegin);
  504. }
  505. else
  506. {
  507. // Only initialize members that don't have an explicit expression
  508. // The members that are explicitly initialized will be initialized after the call to base class' constructor
  509. CompileMemberInitialization(&byteCode, true);
  510. }
  511. }
  512. else
  513. {
  514. // Add the initialization of the members
  515. CompileMemberInitialization(&byteCode, true);
  516. CompileMemberInitialization(&byteCode, false);
  517. }
  518. }
  519. }
  520. // Add the code for the statement block
  521. byteCode.AddCode(&bc);
  522. // Count total variable size
  523. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  524. outFunc->scriptData->variableSpace = varSize;
  525. // Deallocate all local variables
  526. int n;
  527. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  528. {
  529. sVariable *v = variables->variables[n];
  530. if( v->stackOffset > 0 )
  531. {
  532. // Call variables destructors
  533. if( v->name != "return" && v->name != "return address" )
  534. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  535. DeallocateVariable(v->stackOffset);
  536. }
  537. }
  538. // This is the label that return statements jump to
  539. // in order to exit the function
  540. byteCode.Label(0);
  541. // Call destructors for function parameters
  542. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  543. {
  544. sVariable *v = variables->variables[n];
  545. if( v->stackOffset <= 0 )
  546. {
  547. // Call variable destructors here, for variables not yet destroyed
  548. if( v->name != "return" && v->name != "return address" )
  549. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  550. }
  551. // Do not deallocate parameters
  552. }
  553. // Check if the number of labels in the functions isn't too many to be handled
  554. if( nextLabel >= (1<<15) )
  555. Error(TXT_TOO_MANY_JUMP_LABELS, in_func);
  556. // If there are compile errors, there is no reason to build the final code
  557. if( hasCompileErrors || builder->numErrors != buildErrors )
  558. return -1;
  559. // At this point there should be no variables allocated
  560. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  561. // Remove the variable scope
  562. RemoveVariableScope();
  563. byteCode.Ret(-stackPos);
  564. FinalizeFunction();
  565. #ifdef AS_DEBUG
  566. // DEBUG: output byte code
  567. if( outFunc->objectType )
  568. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + ".txt").AddressOf(), in_outFunc);
  569. else
  570. byteCode.DebugOutput(("__" + outFunc->name + ".txt").AddressOf(), in_outFunc);
  571. #endif
  572. return 0;
  573. }
  574. int asCCompiler::CallCopyConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCExprContext *arg, asCScriptNode *node, bool isGlobalVar, bool derefDest)
  575. {
  576. if( !type.IsObject() )
  577. return 0;
  578. // CallCopyConstructor should not be called for object handles.
  579. asASSERT( !type.IsObjectHandle() );
  580. asCArray<asCExprContext*> args;
  581. args.PushLast(arg);
  582. // The reference parameter must be pushed on the stack
  583. asASSERT( arg->type.dataType.GetTypeInfo() == type.GetTypeInfo() );
  584. // Since we're calling the copy constructor, we have to trust the function to not do
  585. // anything stupid otherwise we will just enter a loop, as we try to make temporary
  586. // copies of the argument in order to guarantee safety.
  587. if( type.GetTypeInfo()->flags & asOBJ_REF )
  588. {
  589. asCExprContext ctx(engine);
  590. int func = 0;
  591. asSTypeBehaviour *beh = type.GetBehaviour();
  592. if( beh ) func = beh->copyfactory;
  593. if( func > 0 )
  594. {
  595. if( !isGlobalVar )
  596. {
  597. // Call factory and store the handle in the given variable
  598. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()), true, offset);
  599. // Pop the reference left by the function call
  600. ctx.bc.Instr(asBC_PopPtr);
  601. }
  602. else
  603. {
  604. // Call factory
  605. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()));
  606. // Store the returned handle in the global variable
  607. ctx.bc.Instr(asBC_RDSPtr);
  608. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  609. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  610. ctx.bc.Instr(asBC_PopPtr);
  611. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  612. }
  613. bc->AddCode(&ctx.bc);
  614. return 0;
  615. }
  616. }
  617. else
  618. {
  619. asSTypeBehaviour *beh = type.GetBehaviour();
  620. int func = beh ? beh->copyconstruct : 0;
  621. if( func > 0 )
  622. {
  623. // Push the address where the object will be stored on the stack, before the argument
  624. // TODO: When the context is serializable this probably has to be changed, since this
  625. // pointer can remain on the stack while the context is suspended. There is no
  626. // risk the pointer becomes invalid though, there is just no easy way to serialize it.
  627. asCByteCode tmp(engine);
  628. if( isGlobalVar )
  629. tmp.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  630. else if( isObjectOnHeap )
  631. tmp.InstrSHORT(asBC_PSF, (short)offset);
  632. tmp.AddCode(bc);
  633. bc->AddCode(&tmp);
  634. // When the object is allocated on the stack the object pointer
  635. // must be pushed on the stack after the arguments
  636. if( !isObjectOnHeap )
  637. {
  638. asASSERT( !isGlobalVar );
  639. bc->InstrSHORT(asBC_PSF, (short)offset);
  640. if( derefDest )
  641. {
  642. // The variable is a reference to the real location, so we need to dereference it
  643. bc->Instr(asBC_RDSPtr);
  644. }
  645. }
  646. asCExprContext ctx(engine);
  647. PerformFunctionCall(func, &ctx, isObjectOnHeap, &args, CastToObjectType(type.GetTypeInfo()));
  648. bc->AddCode(&ctx.bc);
  649. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  650. // Mark the object as initialized
  651. if( !isObjectOnHeap )
  652. bc->ObjInfo(offset, asOBJ_INIT);
  653. return 0;
  654. }
  655. }
  656. // Class has no copy constructor/factory.
  657. asCString str;
  658. str.Format(TXT_NO_COPY_CONSTRUCTOR_FOR_s, type.GetTypeInfo()->GetName());
  659. Error(str, node);
  660. return -1;
  661. }
  662. int asCCompiler::CallDefaultConstructor(const asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCScriptNode *node, int isVarGlobOrMem, bool derefDest)
  663. {
  664. if( !type.IsObject() || type.IsObjectHandle() )
  665. return 0;
  666. if( type.GetTypeInfo()->flags & asOBJ_REF )
  667. {
  668. asCExprContext ctx(engine);
  669. ctx.exprNode = node;
  670. int func = 0;
  671. asSTypeBehaviour *beh = type.GetBehaviour();
  672. if( beh )
  673. {
  674. func = beh->factory;
  675. // If no trivial default factory is found, look for a factory where all params have default args
  676. if( func == 0 )
  677. {
  678. for( asUINT n = 0; n < beh->factories.GetLength(); n++ )
  679. {
  680. asCScriptFunction *f = engine->scriptFunctions[beh->factories[n]];
  681. if( f->defaultArgs.GetLength() == f->parameterTypes.GetLength() &&
  682. f->defaultArgs[0] != 0 )
  683. {
  684. func = beh->factories[n];
  685. break;
  686. }
  687. }
  688. }
  689. }
  690. if( func > 0 )
  691. {
  692. asCArray<asCExprContext *> args;
  693. asCScriptFunction *f = engine->scriptFunctions[func];
  694. if( f->parameterTypes.GetLength() )
  695. {
  696. // Add the default values for arguments not explicitly supplied
  697. CompileDefaultAndNamedArgs(node, args, func, CastToObjectType(type.GetTypeInfo()));
  698. PrepareFunctionCall(func, &ctx.bc, args);
  699. MoveArgsToStack(func, &ctx.bc, args, false);
  700. }
  701. if( isVarGlobOrMem == 0 )
  702. {
  703. // Call factory and store the handle in the given variable
  704. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()), true, offset);
  705. // Pop the reference left by the function call
  706. ctx.bc.Instr(asBC_PopPtr);
  707. }
  708. else
  709. {
  710. // Call factory
  711. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()));
  712. // TODO: runtime optimize: Should have a way of storing the object pointer directly to the destination
  713. // instead of first storing it in a local variable and then copying it to the
  714. // destination.
  715. if( !(type.GetTypeInfo()->flags & asOBJ_SCOPED) )
  716. {
  717. // Only dereference the variable if not a scoped type
  718. ctx.bc.Instr(asBC_RDSPtr);
  719. }
  720. if( isVarGlobOrMem == 1 )
  721. {
  722. // Store the returned handle in the global variable
  723. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  724. }
  725. else
  726. {
  727. // Store the returned handle in the class member
  728. ctx.bc.InstrSHORT(asBC_PSF, 0);
  729. ctx.bc.Instr(asBC_RDSPtr);
  730. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  731. }
  732. if( type.GetTypeInfo()->flags & asOBJ_SCOPED )
  733. {
  734. // For scoped typed we must move the reference from the local
  735. // variable rather than copy it as there is no AddRef behaviour
  736. ctx.bc.InstrSHORT_DW(asBC_COPY, AS_PTR_SIZE, asTYPEID_OBJHANDLE | engine->GetTypeIdFromDataType(type));
  737. // Clear the local variable so the reference isn't released
  738. ctx.bc.InstrSHORT(asBC_ClrVPtr, ctx.type.stackOffset);
  739. }
  740. else
  741. {
  742. if( type.IsFuncdef() )
  743. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  744. else
  745. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  746. }
  747. ctx.bc.Instr(asBC_PopPtr);
  748. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  749. }
  750. bc->AddCode(&ctx.bc);
  751. // Cleanup
  752. for( asUINT n = 0; n < args.GetLength(); n++ )
  753. if( args[n] )
  754. {
  755. asDELETE(args[n], asCExprContext);
  756. }
  757. return 0;
  758. }
  759. }
  760. else
  761. {
  762. asCExprContext ctx(engine);
  763. ctx.exprNode = node;
  764. asSTypeBehaviour *beh = type.GetBehaviour();
  765. int func = 0;
  766. if( beh )
  767. {
  768. func = beh->construct;
  769. // If no trivial default constructor is found, look for a constructor where all params have default args
  770. if( func == 0 )
  771. {
  772. for( asUINT n = 0; n < beh->constructors.GetLength(); n++ )
  773. {
  774. asCScriptFunction *f = engine->scriptFunctions[beh->constructors[n]];
  775. if( f->defaultArgs.GetLength() == f->parameterTypes.GetLength() &&
  776. f->defaultArgs[0] != 0 )
  777. {
  778. func = beh->constructors[n];
  779. break;
  780. }
  781. }
  782. }
  783. }
  784. // Allocate and initialize with the default constructor
  785. if( func != 0 || (type.GetTypeInfo()->flags & asOBJ_POD) )
  786. {
  787. asCArray<asCExprContext *> args;
  788. asCScriptFunction *f = engine->scriptFunctions[func];
  789. if( f && f->parameterTypes.GetLength() )
  790. {
  791. // Add the default values for arguments not explicitly supplied
  792. CompileDefaultAndNamedArgs(node, args, func, CastToObjectType(type.GetTypeInfo()));
  793. PrepareFunctionCall(func, &ctx.bc, args);
  794. MoveArgsToStack(func, &ctx.bc, args, false);
  795. }
  796. if( !isObjectOnHeap )
  797. {
  798. if( isVarGlobOrMem == 0 )
  799. {
  800. // There is nothing to do if there is no function,
  801. // as the memory is already allocated on the stack
  802. if( func )
  803. {
  804. // Call the constructor as a normal function
  805. bc->InstrSHORT(asBC_PSF, (short)offset);
  806. if( derefDest )
  807. bc->Instr(asBC_RDSPtr);
  808. asCExprContext ctxCall(engine);
  809. PerformFunctionCall(func, &ctxCall, false, 0, CastToObjectType(type.GetTypeInfo()));
  810. bc->AddCode(&ctxCall.bc);
  811. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  812. // Mark the object as initialized
  813. bc->ObjInfo(offset, asOBJ_INIT);
  814. }
  815. }
  816. else if( isVarGlobOrMem == 2 )
  817. {
  818. // Only POD types can be allocated inline in script classes
  819. asASSERT( type.GetTypeInfo()->flags & asOBJ_POD );
  820. if( func )
  821. {
  822. // Call the constructor as a normal function
  823. bc->InstrSHORT(asBC_PSF, 0);
  824. bc->Instr(asBC_RDSPtr);
  825. bc->InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  826. asCExprContext ctxCall(engine);
  827. PerformFunctionCall(func, &ctxCall, false, 0, CastToObjectType(type.GetTypeInfo()));
  828. bc->AddCode(&ctxCall.bc);
  829. }
  830. }
  831. else
  832. {
  833. asASSERT( false );
  834. }
  835. }
  836. else
  837. {
  838. if( isVarGlobOrMem == 0 )
  839. bc->InstrSHORT(asBC_PSF, (short)offset);
  840. else if( isVarGlobOrMem == 1 )
  841. bc->InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  842. else
  843. {
  844. bc->InstrSHORT(asBC_PSF, 0);
  845. bc->Instr(asBC_RDSPtr);
  846. bc->InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  847. }
  848. if( (type.GetTypeInfo()->flags & asOBJ_TEMPLATE) )
  849. {
  850. asCScriptFunction *descr = engine->scriptFunctions[func];
  851. asASSERT( descr->funcType == asFUNC_SCRIPT );
  852. // Find the id of the real constructor and not the generated stub
  853. asUINT id = 0;
  854. asDWORD *funcBc = descr->scriptData->byteCode.AddressOf();
  855. while( funcBc )
  856. {
  857. if( (*(asBYTE*)funcBc) == asBC_CALLSYS )
  858. {
  859. id = asBC_INTARG(funcBc);
  860. break;
  861. }
  862. funcBc += asBCTypeSize[asBCInfo[*(asBYTE*)funcBc].type];
  863. }
  864. asASSERT( id );
  865. bc->InstrPTR(asBC_OBJTYPE, type.GetTypeInfo());
  866. bc->Alloc(asBC_ALLOC, type.GetTypeInfo(), id, AS_PTR_SIZE + AS_PTR_SIZE);
  867. }
  868. else
  869. bc->Alloc(asBC_ALLOC, type.GetTypeInfo(), func, AS_PTR_SIZE);
  870. }
  871. // Cleanup
  872. for( asUINT n = 0; n < args.GetLength(); n++ )
  873. if( args[n] )
  874. {
  875. asDELETE(args[n], asCExprContext);
  876. }
  877. return 0;
  878. }
  879. }
  880. // Class has no default factory/constructor.
  881. asCString str;
  882. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetTypeInfo()->GetName());
  883. Error(str, node);
  884. return -1;
  885. }
  886. void asCCompiler::CallDestructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc)
  887. {
  888. if( !type.IsReference() )
  889. {
  890. // Call destructor for the data type
  891. if( type.IsObject() || type.IsFuncdef() )
  892. {
  893. // The null pointer doesn't need to be destroyed
  894. if( type.IsNullHandle() )
  895. return;
  896. // Nothing is done for list pattern types, as this is taken care of by the CompileInitList method
  897. if( type.GetTypeInfo()->flags & asOBJ_LIST_PATTERN )
  898. return;
  899. if( isObjectOnHeap || type.IsObjectHandle() )
  900. {
  901. // Free the memory
  902. if (type.IsFuncdef())
  903. bc->InstrW_PTR(asBC_FREE, (short)offset, &engine->functionBehaviours);
  904. else
  905. bc->InstrW_PTR(asBC_FREE, (short)offset, type.GetTypeInfo());
  906. }
  907. else
  908. {
  909. asASSERT( type.GetTypeInfo()->GetFlags() & asOBJ_VALUE );
  910. if( type.GetBehaviour()->destruct )
  911. {
  912. // Call the destructor as a regular function
  913. asCExprContext ctx(engine);
  914. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  915. PerformFunctionCall(type.GetBehaviour()->destruct, &ctx);
  916. ctx.bc.OptimizeLocally(tempVariableOffsets);
  917. bc->AddCode(&ctx.bc);
  918. }
  919. // TODO: Value on stack: This probably needs to be done in PerformFunctionCall
  920. // Mark the object as destroyed
  921. bc->ObjInfo(offset, asOBJ_UNINIT);
  922. }
  923. }
  924. }
  925. }
  926. void asCCompiler::LineInstr(asCByteCode *bc, size_t pos)
  927. {
  928. int r, c;
  929. script->ConvertPosToRowCol(pos, &r, &c);
  930. bc->Line(r, c, script->idx);
  931. }
  932. void asCCompiler::CompileStatementBlock(asCScriptNode *block, bool ownVariableScope, bool *hasReturn, asCByteCode *bc)
  933. {
  934. *hasReturn = false;
  935. bool isFinished = false;
  936. bool hasUnreachableCode = false;
  937. bool hasReturnBefore = false;
  938. if( ownVariableScope )
  939. {
  940. bc->Block(true);
  941. AddVariableScope();
  942. }
  943. asCScriptNode *node = block->firstChild;
  944. while( node )
  945. {
  946. #ifdef AS_DEBUG
  947. // Keep the current line in a variable so it will be easier
  948. // to determine where in a script an assert is occurring.
  949. int currentLine = 0;
  950. script->ConvertPosToRowCol(node->tokenPos, &currentLine, 0);
  951. #endif
  952. if( !hasUnreachableCode && (*hasReturn || isFinished) )
  953. {
  954. // Empty statements don't count
  955. if( node->nodeType != snExpressionStatement || node->firstChild )
  956. {
  957. hasUnreachableCode = true;
  958. Warning(TXT_UNREACHABLE_CODE, node);
  959. }
  960. if( *hasReturn )
  961. hasReturnBefore = true;
  962. }
  963. if( node->nodeType == snBreak || node->nodeType == snContinue )
  964. isFinished = true;
  965. asCByteCode statement(engine);
  966. if( node->nodeType == snDeclaration )
  967. CompileDeclaration(node, &statement);
  968. else
  969. CompileStatement(node, hasReturn, &statement);
  970. // Ignore missing returns in unreachable code paths
  971. if( !(*hasReturn) && hasReturnBefore )
  972. *hasReturn = true;
  973. LineInstr(bc, node->tokenPos);
  974. bc->AddCode(&statement);
  975. if( !hasCompileErrors )
  976. {
  977. asASSERT( tempVariables.GetLength() == 0 );
  978. asASSERT( reservedVariables.GetLength() == 0 );
  979. }
  980. node = node->next;
  981. }
  982. if( ownVariableScope )
  983. {
  984. // Deallocate variables in this block, in reverse order
  985. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  986. {
  987. sVariable *v = variables->variables[n];
  988. // Call variable destructors here, for variables not yet destroyed
  989. // If the block is terminated with a break, continue, or
  990. // return the variables are already destroyed
  991. if( !isFinished && !*hasReturn )
  992. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  993. // Don't deallocate function parameters
  994. if( v->stackOffset > 0 )
  995. DeallocateVariable(v->stackOffset);
  996. }
  997. RemoveVariableScope();
  998. bc->Block(false);
  999. }
  1000. }
  1001. // Entry
  1002. int asCCompiler::CompileGlobalVariable(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptNode *in_node, sGlobalVariableDescription *in_gvar, asCScriptFunction *in_outFunc)
  1003. {
  1004. Reset(in_builder, in_script, in_outFunc);
  1005. m_globalVar = in_gvar;
  1006. // Add a variable scope (even though variables can't be declared)
  1007. AddVariableScope();
  1008. in_gvar->isPureConstant = false;
  1009. // Parse the initialization nodes
  1010. asCParser parser(builder);
  1011. if (in_node)
  1012. {
  1013. int r = parser.ParseVarInit(in_script, in_node);
  1014. if (r < 0)
  1015. return r;
  1016. in_node = parser.GetScriptNode();
  1017. }
  1018. asCExprContext compiledCtx(engine);
  1019. bool preCompiled = false;
  1020. if (in_gvar->datatype.IsAuto())
  1021. {
  1022. preCompiled = CompileAutoType(in_gvar->datatype, compiledCtx, in_node, in_gvar->declaredAtNode);
  1023. if (!preCompiled)
  1024. {
  1025. // If it wasn't possible to determine the type from the expression then there
  1026. // is no need to continue with the initialization. The error was already reported
  1027. // in CompileAutoType.
  1028. return -1;
  1029. }
  1030. }
  1031. if( in_gvar->property == 0 )
  1032. {
  1033. in_gvar->property = builder->module->AllocateGlobalProperty(in_gvar->name.AddressOf(), in_gvar->datatype, in_gvar->ns);
  1034. in_gvar->index = in_gvar->property->id;
  1035. }
  1036. // Compile the expression
  1037. asCExprContext ctx(engine);
  1038. asQWORD constantValue = 0;
  1039. if( CompileInitialization(in_node, &ctx.bc, in_gvar->datatype, in_gvar->declaredAtNode, in_gvar->index, &constantValue, 1, preCompiled ? &compiledCtx : 0) )
  1040. {
  1041. // Should the variable be marked as pure constant?
  1042. if( in_gvar->datatype.IsPrimitive() && in_gvar->datatype.IsReadOnly() )
  1043. {
  1044. in_gvar->isPureConstant = true;
  1045. in_gvar->constantValue = constantValue;
  1046. }
  1047. }
  1048. // Concatenate the bytecode
  1049. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  1050. // Add information on the line number for the global variable
  1051. size_t pos = 0;
  1052. if( in_gvar->declaredAtNode )
  1053. pos = in_gvar->declaredAtNode->tokenPos;
  1054. else if( in_gvar->initializationNode )
  1055. pos = in_gvar->initializationNode->tokenPos;
  1056. LineInstr(&byteCode, pos);
  1057. // Reserve space for all local variables
  1058. outFunc->scriptData->variableSpace = varSize;
  1059. ctx.bc.OptimizeLocally(tempVariableOffsets);
  1060. byteCode.AddCode(&ctx.bc);
  1061. // Deallocate variables in this block, in reverse order
  1062. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; --n )
  1063. {
  1064. sVariable *v = variables->variables[n];
  1065. // Call variable destructors here, for variables not yet destroyed
  1066. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  1067. DeallocateVariable(v->stackOffset);
  1068. }
  1069. if( hasCompileErrors ) return -1;
  1070. // At this point there should be no variables allocated
  1071. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  1072. // Remove the variable scope again
  1073. RemoveVariableScope();
  1074. byteCode.Ret(0);
  1075. FinalizeFunction();
  1076. #ifdef AS_DEBUG
  1077. // DEBUG: output byte code
  1078. byteCode.DebugOutput(("___init_" + in_gvar->name + ".txt").AddressOf(), outFunc);
  1079. #endif
  1080. return 0;
  1081. }
  1082. void asCCompiler::DetermineSingleFunc(asCExprContext *ctx, asCScriptNode *node)
  1083. {
  1084. // Don't do anything if this is not a deferred global function
  1085. if( !ctx->IsGlobalFunc() )
  1086. return;
  1087. // Determine the namespace
  1088. asSNameSpace *ns = 0;
  1089. asCString name = "";
  1090. int pos = ctx->methodName.FindLast("::");
  1091. if( pos >= 0 )
  1092. {
  1093. asCString nsName = ctx->methodName.SubString(0, pos+2);
  1094. // Cut off the ::
  1095. if( nsName.GetLength() > 2 )
  1096. nsName.SetLength(nsName.GetLength()-2);
  1097. ns = DetermineNameSpace(nsName);
  1098. name = ctx->methodName.SubString(pos+2);
  1099. }
  1100. else
  1101. {
  1102. DetermineNameSpace("");
  1103. name = ctx->methodName;
  1104. }
  1105. asCArray<int> funcs;
  1106. if( ns )
  1107. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  1108. // CompileVariableAccess should guarantee that at least one function is exists
  1109. asASSERT( funcs.GetLength() > 0 );
  1110. if( funcs.GetLength() > 1 )
  1111. {
  1112. asCString str;
  1113. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, ctx->methodName.AddressOf());
  1114. Error(str, node);
  1115. // Fall through so the compiler can continue as if only one function was matching
  1116. }
  1117. // A shared object may not access global functions unless they too are shared (e.g. registered functions)
  1118. if( !builder->GetFunctionDescription(funcs[0])->IsShared() &&
  1119. outFunc->IsShared() )
  1120. {
  1121. asCString msg;
  1122. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, builder->GetFunctionDescription(funcs[0])->GetDeclaration());
  1123. Error(msg, node);
  1124. // Fall through so the compiler can continue anyway
  1125. }
  1126. // Push the function pointer on the stack
  1127. ctx->bc.InstrPTR(asBC_FuncPtr, builder->GetFunctionDescription(funcs[0]));
  1128. ctx->type.Set(asCDataType::CreateType(engine->FindMatchingFuncdef(builder->GetFunctionDescription(funcs[0]), builder->module), false));
  1129. ctx->type.dataType.MakeHandle(true);
  1130. ctx->type.isExplicitHandle = true;
  1131. ctx->methodName = "";
  1132. }
  1133. void asCCompiler::CompileInitAsCopy(asCDataType &dt, int offset, asCByteCode *bc, asCExprContext *arg, asCScriptNode *node, bool derefDestination)
  1134. {
  1135. bool isObjectOnHeap = derefDestination ? false : IsVariableOnHeap(offset);
  1136. // Use copy constructor if available.
  1137. asCObjectType *ot = CastToObjectType(dt.GetTypeInfo());
  1138. if(!dt.IsObjectHandle() && ot && (ot->beh.copyconstruct || ot->beh.copyfactory))
  1139. {
  1140. PrepareForAssignment(&dt, arg, node, true);
  1141. int r = CallCopyConstructor(dt, offset, isObjectOnHeap, bc, arg, node, 0, derefDestination);
  1142. if( r < 0 && tempVariables.Exists(offset) )
  1143. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  1144. }
  1145. else
  1146. {
  1147. // TODO: Need to reserve variables, as the default constructor may need
  1148. // to allocate temporary variables to compute default args
  1149. // Allocate and construct the temporary object before whatever is already in the bytecode
  1150. asCByteCode tmpBC(engine);
  1151. int r = CallDefaultConstructor(dt, offset, isObjectOnHeap, &tmpBC, node, 0, derefDestination);
  1152. if( r < 0 )
  1153. {
  1154. if( tempVariables.Exists(offset) )
  1155. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  1156. return;
  1157. }
  1158. tmpBC.AddCode(bc);
  1159. bc->AddCode(&tmpBC);
  1160. // Assign the evaluated expression to the temporary variable
  1161. PrepareForAssignment(&dt, arg, node, true);
  1162. bc->AddCode(&arg->bc);
  1163. // Call the opAssign method to assign the value to the temporary object
  1164. dt.MakeReference(isObjectOnHeap);
  1165. asCExprValue type;
  1166. type.Set(dt);
  1167. type.isTemporary = true;
  1168. type.stackOffset = (short)offset;
  1169. if( dt.IsObjectHandle() )
  1170. type.isExplicitHandle = true;
  1171. bc->InstrSHORT(asBC_PSF, (short)offset);
  1172. if( derefDestination )
  1173. bc->Instr(asBC_RDSPtr);
  1174. r = PerformAssignment(&type, &arg->type, bc, node);
  1175. if( r < 0 )
  1176. {
  1177. if( tempVariables.Exists(offset) )
  1178. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  1179. return;
  1180. }
  1181. // Pop the reference that was pushed on the stack if the result is an object
  1182. if( type.dataType.IsObject() || type.dataType.IsFuncdef() )
  1183. bc->Instr(asBC_PopPtr);
  1184. // If the assignment operator returned an object by value it will
  1185. // be in a temporary variable which we need to destroy now
  1186. if( type.isTemporary && type.stackOffset != (short)offset )
  1187. ReleaseTemporaryVariable(type.stackOffset, bc);
  1188. // Release the original value too in case it is a temporary
  1189. ReleaseTemporaryVariable(arg->type, bc);
  1190. }
  1191. }
  1192. int asCCompiler::PrepareArgument(asCDataType *paramType, asCExprContext *ctx, asCScriptNode *node, bool isFunction, int refType, bool isMakingCopy)
  1193. {
  1194. asCDataType param = *paramType;
  1195. if( paramType->GetTokenType() == ttQuestion )
  1196. {
  1197. // The function is expecting a var type. If the argument is a function name, we must now decide which function it is
  1198. DetermineSingleFunc(ctx, node);
  1199. // Since the function is expecting a var type ?, then we don't want to convert the argument to anything else
  1200. param = ctx->type.dataType;
  1201. param.MakeHandle(ctx->type.isExplicitHandle || ctx->type.IsNullConstant());
  1202. // Treat the void expression like a null handle when working with var types
  1203. if( ctx->IsVoidExpression() )
  1204. param = asCDataType::CreateNullHandle();
  1205. // If value assign is disabled for reference types, then make
  1206. // sure to always pass the handle to ? parameters
  1207. if( builder->engine->ep.disallowValueAssignForRefType &&
  1208. ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCOPED) )
  1209. {
  1210. param.MakeHandle(true);
  1211. }
  1212. param.MakeReference(paramType->IsReference());
  1213. param.MakeReadOnly(paramType->IsReadOnly());
  1214. }
  1215. else
  1216. param = *paramType;
  1217. asCDataType dt = param;
  1218. // Need to protect arguments by reference
  1219. if( isFunction && dt.IsReference() )
  1220. {
  1221. // Allocate a temporary variable of the same type as the argument
  1222. dt.MakeReference(false);
  1223. int offset;
  1224. if( refType == asTM_INREF )
  1225. {
  1226. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  1227. return -1;
  1228. // Add the type id as hidden arg if the parameter is a ? type
  1229. if( paramType->GetTokenType() == ttQuestion )
  1230. {
  1231. asCByteCode tmpBC(engine);
  1232. // Place the type id on the stack as a hidden parameter
  1233. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1234. // Insert the code before the expression code
  1235. tmpBC.AddCode(&ctx->bc);
  1236. ctx->bc.AddCode(&tmpBC);
  1237. }
  1238. if( dt.IsPrimitive() )
  1239. {
  1240. // If the reference is const, then it is not necessary to make a copy if the value already is a variable
  1241. // Even if the same variable is passed in another argument as non-const then there is no problem
  1242. IsVariableInitialized(&ctx->type, node);
  1243. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1244. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  1245. if( !(param.IsReadOnly() && ctx->type.isVariable) )
  1246. ConvertToTempVariable(ctx);
  1247. PushVariableOnStack(ctx, true);
  1248. ctx->type.dataType.MakeReadOnly(param.IsReadOnly());
  1249. }
  1250. else if( ctx->type.dataType.IsNullHandle() )
  1251. {
  1252. // Make sure the argument type can support handles (or is itself a handle)
  1253. // Don't allow null handle to be converted to an object type of ASHANDLE here, that would require more logic to call the constructor (which should be handled in ImplicitConversion)
  1254. if( (!dt.SupportHandles() && !dt.IsObjectHandle()) || (dt.GetTypeInfo() && (dt.GetTypeInfo()->GetFlags() & asOBJ_ASHANDLE)) )
  1255. {
  1256. asCString str;
  1257. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), param.Format(outFunc->nameSpace).AddressOf());
  1258. Error(str, node);
  1259. ctx->type.Set(param);
  1260. return -1;
  1261. }
  1262. // Need to initialize a local temporary variable to
  1263. // represent the null handle when passed as reference
  1264. asASSERT( ctx->bc.GetLastInstr() == asBC_PshNull );
  1265. ctx->bc.Instr(asBC_PopPtr);
  1266. dt.MakeHandle(true);
  1267. dt.MakeReadOnly(false);
  1268. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1269. // Push the reference to the variable on the stack
  1270. ctx->bc.InstrWORD(asBC_PSF, (short)offset);
  1271. ctx->type.SetVariable(dt, offset, true);
  1272. ctx->type.isExplicitHandle = true;
  1273. }
  1274. else
  1275. {
  1276. IsVariableInitialized(&ctx->type, node);
  1277. if( !isMakingCopy )
  1278. {
  1279. // For parameters expecting a reference to a handle we need to make sure the argument
  1280. // is really a handle, and not just a reference to the object. Do this check before the
  1281. // implicit conversion so it can be treated correctly.
  1282. if (dt.IsObjectHandle() && !ctx->type.dataType.IsObjectHandle())
  1283. {
  1284. // Make a refCopy into a local handle variable
  1285. // Allocate a handle variable
  1286. dt.MakeHandle(true);
  1287. dt.MakeReadOnly(false);
  1288. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1289. // Copy the handle
  1290. Dereference(ctx, true);
  1291. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1292. if (ctx->type.dataType.IsFuncdef())
  1293. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  1294. else
  1295. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  1296. ctx->bc.Instr(asBC_PopPtr);
  1297. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1298. // Release the original temporary variable
  1299. if( ctx->type.isTemporary )
  1300. ReleaseTemporaryVariable(ctx->type.stackOffset, &ctx->bc);
  1301. ctx->type.SetVariable(dt, offset, true);
  1302. }
  1303. // Even though the parameter expects a reference, it is only meant to be
  1304. // used as input value and doesn't have to refer to the actual object, so it
  1305. // is OK to do an implicit conversion.
  1306. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  1307. if( !ctx->type.dataType.IsEqualExceptRefAndConst(param) )
  1308. {
  1309. asCString str;
  1310. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), param.Format(outFunc->nameSpace).AddressOf());
  1311. Error(str, node);
  1312. ctx->type.Set(param);
  1313. return -1;
  1314. }
  1315. // The compiler must guarantee that the object stays alive during the execution
  1316. // of the function, and it must also guarantee that the value isn't modified by
  1317. // the function.
  1318. // If the argument is a temporary local variable then it is safe to be passed to
  1319. // the function as it is, since the local variable will stay alive, and since it
  1320. // is temporary there is no side effect if the function modifies it.
  1321. // If the parameter is read-only and therefore guaranteed not to be modified by the
  1322. // function, then it is enough that the variable is local to guarantee the lifetime.
  1323. if( !ctx->type.isTemporary && !(param.IsReadOnly() && (ctx->type.isVariable || ctx->type.isRefSafe)) )
  1324. {
  1325. if( ctx->type.dataType.IsFuncdef() || ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && param.IsReadOnly() && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCOPED)) )
  1326. {
  1327. // Funcdefs only need an extra handle to guarantee the lifetime.
  1328. // If the object is a reference type (except scoped reference types), and the
  1329. // parameter is a const reference, then it is not necessary to make a copy of the
  1330. // object. The compiler just needs to hold a handle to guarantee the lifetime.
  1331. // Allocate a handle variable
  1332. dt.MakeHandle(true);
  1333. dt.MakeReadOnly(false);
  1334. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1335. // Copy the handle
  1336. Dereference(ctx, true);
  1337. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1338. if (ctx->type.dataType.IsFuncdef())
  1339. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  1340. else
  1341. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  1342. ctx->bc.Instr(asBC_PopPtr);
  1343. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1344. // The type should be set to the param type instead of dt to guarantee
  1345. // that the expression keeps the correct type for variable ? args. Otherwise
  1346. // MoveArgsToStack will use the wrong bytecode to move the arg to the stack
  1347. bool isExplicitHandle = ctx->type.isExplicitHandle;
  1348. ctx->type.SetVariable(param, offset, true);
  1349. ctx->type.dataType.MakeHandle(true);
  1350. ctx->type.isExplicitHandle = isExplicitHandle;
  1351. }
  1352. else
  1353. {
  1354. // Make a copy of the object to guarantee that the original isn't modified
  1355. asASSERT(!dt.IsFuncdef());
  1356. // Allocate and initialize a temporary local object
  1357. dt.MakeReadOnly(false);
  1358. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1359. CompileInitAsCopy(dt, offset, &ctx->bc, ctx, node, false);
  1360. // Push the object pointer on the stack
  1361. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1362. if( dt.IsObject() && !dt.IsObjectHandle() )
  1363. ctx->bc.Instr(asBC_RDSPtr);
  1364. // Set the resulting type
  1365. ctx->type.Set(dt);
  1366. ctx->type.isTemporary = true;
  1367. ctx->type.stackOffset = short(offset);
  1368. if( dt.IsObjectHandle() )
  1369. ctx->type.isExplicitHandle = true;
  1370. ctx->type.dataType.MakeReference(false);
  1371. if( paramType->IsReadOnly() )
  1372. ctx->type.dataType.MakeReadOnly(true);
  1373. }
  1374. }
  1375. // When calling a function expecting a var arg with a parameter received as reference to handle
  1376. // then it is necessary to copy the handle to a local variable, otherwise MoveArgsToStack will
  1377. // not be able to do the correct double dereference to put the reference to the object on the stack.
  1378. if (paramType->GetTokenType() == ttQuestion && !param.IsObjectHandle() && ctx->type.isVariable)
  1379. {
  1380. sVariable *var = variables->GetVariableByOffset(ctx->type.stackOffset);
  1381. if (var && var->type.IsReference() && var->type.IsObjectHandle())
  1382. {
  1383. // Copy the handle to local variable
  1384. // Allocate a handle variable
  1385. dt.MakeHandle(true);
  1386. dt.MakeReadOnly(false);
  1387. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1388. // Copy the handle
  1389. Dereference(ctx, true);
  1390. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1391. if (ctx->type.dataType.IsFuncdef())
  1392. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  1393. else
  1394. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  1395. ctx->bc.Instr(asBC_PopPtr);
  1396. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1397. // The type should be set to the param type instead of dt to guarantee
  1398. // that the expression keeps the correct type for variable ? args. Otherwise
  1399. // MoveArgsToStack will use the wrong bytecode to move the arg to the stack
  1400. ctx->type.SetVariable(param, offset, true);
  1401. }
  1402. }
  1403. }
  1404. else
  1405. {
  1406. // We must guarantee that the address to the value is on the stack
  1407. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) &&
  1408. !ctx->type.dataType.IsObjectHandle() &&
  1409. ctx->type.dataType.IsReference() )
  1410. Dereference(ctx, true);
  1411. }
  1412. }
  1413. }
  1414. else if( refType == asTM_OUTREF )
  1415. {
  1416. // Add the type id as hidden arg if the parameter is a ? type
  1417. if( paramType->GetTokenType() == ttQuestion )
  1418. {
  1419. asCByteCode tmpBC(engine);
  1420. // Place the type id on the stack as a hidden parameter
  1421. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1422. // Insert the code before the expression code
  1423. tmpBC.AddCode(&ctx->bc);
  1424. ctx->bc.AddCode(&tmpBC);
  1425. }
  1426. // If the expression is marked as clean, then it can be used directly
  1427. // without the need to allocate another temporary value as it is known
  1428. // that the argument has no other value than the default
  1429. if( ctx->isCleanArg )
  1430. {
  1431. // Must be a local variable
  1432. asASSERT( ctx->type.isVariable );
  1433. }
  1434. else
  1435. {
  1436. // Null handles and void expressions must be marked as explicit
  1437. // handles for correct treatement in MoveArgsToStack
  1438. if (dt.IsNullHandle())
  1439. ctx->type.isExplicitHandle = true;
  1440. // Make sure the variable is not used in the expression
  1441. dt.MakeReadOnly(false);
  1442. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1443. if( dt.IsPrimitive() )
  1444. {
  1445. ctx->type.SetVariable(dt, offset, true);
  1446. PushVariableOnStack(ctx, true);
  1447. }
  1448. else
  1449. {
  1450. // Allocate and construct the temporary object
  1451. asCByteCode tmpBC(engine);
  1452. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1453. // Insert the code before the expression code
  1454. tmpBC.AddCode(&ctx->bc);
  1455. ctx->bc.AddCode(&tmpBC);
  1456. dt.MakeReference(!(dt.IsObject() || dt.IsFuncdef()) || dt.IsObjectHandle());
  1457. asCExprValue type;
  1458. type.Set(dt);
  1459. type.isTemporary = true;
  1460. type.stackOffset = (short)offset;
  1461. type.isExplicitHandle = ctx->type.isExplicitHandle;
  1462. ctx->type = type;
  1463. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1464. if( (dt.IsObject() || dt.IsFuncdef()) && !dt.IsObjectHandle() )
  1465. ctx->bc.Instr(asBC_RDSPtr);
  1466. }
  1467. // After the function returns the temporary variable will
  1468. // be assigned to the expression, if it is a valid lvalue
  1469. }
  1470. }
  1471. else if( refType == asTM_INOUTREF )
  1472. {
  1473. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  1474. return -1;
  1475. // Add the type id as hidden arg if the parameter is a ? type
  1476. if( paramType->GetTokenType() == ttQuestion )
  1477. {
  1478. asCByteCode tmpBC(engine);
  1479. // Place the type id on the stack as a hidden parameter
  1480. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1481. // Insert the code before the expression code
  1482. tmpBC.AddCode(&ctx->bc);
  1483. ctx->bc.AddCode(&tmpBC);
  1484. }
  1485. // Literal constants cannot be passed to inout ref arguments
  1486. if( !ctx->type.isVariable &&
  1487. ctx->type.isConstant &&
  1488. !ctx->type.dataType.IsEqualExceptRefAndConst(engine->stringType) )
  1489. {
  1490. // Unless unsafe references are turned on and the reference is const
  1491. if( param.IsReadOnly() && engine->ep.allowUnsafeReferences )
  1492. {
  1493. // Since the parameter is a const & make a copy.
  1494. ConvertToTempVariable(ctx);
  1495. ctx->type.dataType.MakeReadOnly(true);
  1496. }
  1497. else
  1498. {
  1499. Error(TXT_NOT_VALID_REFERENCE, node);
  1500. return -1;
  1501. }
  1502. }
  1503. // Allow anonymous init lists to be converted to the arg type
  1504. if( ctx->IsAnonymousInitList() )
  1505. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true, true);
  1506. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && ctx->type.dataType.GetTypeInfo() != dt.GetTypeInfo() )
  1507. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true, false);
  1508. // Only objects that support object handles
  1509. // can be guaranteed to be safe. Local variables are
  1510. // already safe, so there is no need to add an extra
  1511. // references
  1512. if( !engine->ep.allowUnsafeReferences &&
  1513. !ctx->type.isVariable &&
  1514. (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) &&
  1515. !ctx->type.dataType.IsObjectHandle() &&
  1516. ((ctx->type.dataType.GetBehaviour()->addref &&
  1517. ctx->type.dataType.GetBehaviour()->release) ||
  1518. (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_NOCOUNT) ||
  1519. ctx->type.dataType.IsFuncdef()) )
  1520. {
  1521. // Store a handle to the object as local variable
  1522. asCExprContext tmp(engine);
  1523. dt = ctx->type.dataType;
  1524. dt.MakeHandle(true);
  1525. dt.MakeReference(false);
  1526. dt.MakeReadOnly(false);
  1527. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1528. // Copy the handle
  1529. if( !ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReference() )
  1530. ctx->bc.Instr(asBC_RDSPtr);
  1531. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1532. if( ctx->type.dataType.IsFuncdef() )
  1533. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  1534. else
  1535. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  1536. ctx->bc.Instr(asBC_PopPtr);
  1537. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1538. dt.MakeHandle(false);
  1539. dt.MakeReference(true);
  1540. // Release previous temporary variable stored in the context (if any)
  1541. if( ctx->type.isTemporary )
  1542. ReleaseTemporaryVariable(ctx->type.stackOffset, &ctx->bc);
  1543. ctx->type.SetVariable(dt, offset, true);
  1544. }
  1545. // Make sure the reference to the value is on the stack
  1546. // For objects, the reference needs to be dereferenced so the pointer on the stack is to the actual object
  1547. // For handles, the reference shouldn't be changed because the pointer on the stack should be to the handle
  1548. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && ctx->type.dataType.IsReference() && !param.IsObjectHandle() )
  1549. Dereference(ctx, true);
  1550. else if( ctx->type.isVariable && !(ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) )
  1551. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  1552. else if( ctx->type.dataType.IsPrimitive() )
  1553. ctx->bc.Instr(asBC_PshRPtr);
  1554. else if( ctx->type.dataType.IsObjectHandle() && !ctx->type.dataType.IsReference() )
  1555. ImplicitConversion(ctx, param, node, asIC_IMPLICIT_CONV, true, false);
  1556. }
  1557. }
  1558. else
  1559. {
  1560. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  1561. return -1;
  1562. if( dt.IsPrimitive() )
  1563. {
  1564. IsVariableInitialized(&ctx->type, node);
  1565. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1566. // Implicitly convert primitives to the parameter type
  1567. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1568. if( ctx->type.isVariable )
  1569. {
  1570. PushVariableOnStack(ctx, dt.IsReference());
  1571. }
  1572. else if( ctx->type.isConstant )
  1573. {
  1574. ConvertToVariable(ctx);
  1575. PushVariableOnStack(ctx, dt.IsReference());
  1576. }
  1577. }
  1578. else
  1579. {
  1580. IsVariableInitialized(&ctx->type, node);
  1581. // Implicitly convert primitives to the parameter type
  1582. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1583. // Was the conversion successful?
  1584. if( !ctx->type.dataType.IsEqualExceptRef(dt) )
  1585. {
  1586. asCString str;
  1587. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), dt.Format(outFunc->nameSpace).AddressOf());
  1588. Error(str, node);
  1589. ctx->type.Set(dt);
  1590. return -1;
  1591. }
  1592. if( dt.IsObjectHandle() )
  1593. ctx->type.isExplicitHandle = true;
  1594. if( (dt.IsObject() || dt.IsFuncdef()) && !dt.IsNullHandle() && !dt.IsReference() )
  1595. {
  1596. // Objects passed by value must be placed in temporary variables
  1597. // so that they are guaranteed to not be referenced anywhere else.
  1598. // The object must also be allocated on the heap, as the memory will
  1599. // be deleted by the called function.
  1600. // Handles passed by value must also be placed in a temporary variable
  1601. // to guarantee that the object referred to isn't freed too early.
  1602. // TODO: value on stack: How can we avoid this unnecessary allocation?
  1603. // Don't make temporary copies of handles if it is going to be used
  1604. // for handle assignment anyway, i.e. REFCPY.
  1605. if( !(!isFunction && isMakingCopy && ctx->type.dataType.IsObjectHandle() && ctx->type.isVariable) )
  1606. PrepareTemporaryVariable(node, ctx, true);
  1607. }
  1608. }
  1609. }
  1610. // Don't put any pointer on the stack yet
  1611. if( param.IsReference() || ((param.IsObject() || param.IsFuncdef()) && !param.IsNullHandle()) )
  1612. {
  1613. // &inout parameter may leave the reference on the stack already
  1614. // references considered safe too, i.e. when the life time is known
  1615. if( refType != asTM_INOUTREF && !ctx->type.isRefSafe )
  1616. {
  1617. asASSERT( ctx->type.isVariable || ctx->type.isRefSafe || ctx->type.isTemporary || isMakingCopy );
  1618. if( ctx->type.isVariable || ctx->type.isTemporary )
  1619. {
  1620. ctx->bc.Instr(asBC_PopPtr);
  1621. ctx->bc.InstrSHORT(asBC_VAR, ctx->type.stackOffset);
  1622. ProcessDeferredParams(ctx);
  1623. }
  1624. }
  1625. }
  1626. return 0;
  1627. }
  1628. int asCCompiler::PrepareFunctionCall(int funcId, asCByteCode *bc, asCArray<asCExprContext *> &args)
  1629. {
  1630. // When a match has been found, compile the final byte code using correct parameter types
  1631. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1632. asASSERT( descr->parameterTypes.GetLength() == args.GetLength() );
  1633. // If the function being called is the opAssign or copy constructor for the same type
  1634. // as the argument, then we should avoid making temporary copy of the argument
  1635. bool makingCopy = false;
  1636. if( descr->parameterTypes.GetLength() == 1 &&
  1637. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1638. (((descr->name == "opAssign" || descr->name == "$beh0") && descr->objectType && descr->objectType == args[0]->type.dataType.GetTypeInfo()) ||
  1639. (descr->objectType == 0 && args[0]->type.dataType.GetTypeInfo() && descr->name == args[0]->type.dataType.GetTypeInfo()->name)) )
  1640. makingCopy = true;
  1641. // Add code for arguments
  1642. asCExprContext e(engine);
  1643. for( int n = (int)args.GetLength()-1; n >= 0; n-- )
  1644. {
  1645. // Make sure PrepareArgument doesn't use any variable that is already
  1646. // being used by the argument or any of the following argument expressions
  1647. int l = int(reservedVariables.GetLength());
  1648. for( int m = n; m >= 0; m-- )
  1649. args[m]->bc.GetVarsUsed(reservedVariables);
  1650. int r = PrepareArgument2(&e, args[n], &descr->parameterTypes[n], true, descr->inOutFlags[n], makingCopy);
  1651. reservedVariables.SetLength(l);
  1652. if (r < 0)
  1653. return r;
  1654. }
  1655. bc->AddCode(&e.bc);
  1656. return 0;
  1657. }
  1658. void asCCompiler::MoveArgsToStack(int funcId, asCByteCode *bc, asCArray<asCExprContext *> &args, bool addOneToOffset)
  1659. {
  1660. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1661. int offset = 0;
  1662. if( addOneToOffset )
  1663. offset += AS_PTR_SIZE;
  1664. // The address of where the return value should be stored is push on top of the arguments
  1665. if( descr->DoesReturnOnStack() )
  1666. offset += AS_PTR_SIZE;
  1667. #ifdef AS_DEBUG
  1668. // If the function being called is the opAssign or copy constructor for the same type
  1669. // as the argument, then we should avoid making temporary copy of the argument
  1670. bool makingCopy = false;
  1671. if( descr->parameterTypes.GetLength() == 1 &&
  1672. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1673. (((descr->name == "opAssign" || descr->name == "$beh0") && descr->objectType && descr->objectType == args[0]->type.dataType.GetTypeInfo()) ||
  1674. (descr->objectType == 0 && args[0]->type.dataType.GetTypeInfo() && descr->name == args[0]->type.dataType.GetTypeInfo()->name)) )
  1675. makingCopy = true;
  1676. #endif
  1677. // Move the objects that are sent by value to the stack just before the call
  1678. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  1679. {
  1680. if( descr->parameterTypes[n].IsReference() )
  1681. {
  1682. if( (descr->parameterTypes[n].IsObject() || descr->parameterTypes[n].IsFuncdef()) && !descr->parameterTypes[n].IsObjectHandle() )
  1683. {
  1684. if( descr->inOutFlags[n] != asTM_INOUTREF && !args[n]->type.isRefSafe )
  1685. {
  1686. #ifdef AS_DEBUG
  1687. // This assert is inside AS_DEBUG because of the variable makingCopy which is only defined in debug mode
  1688. asASSERT( args[n]->type.isVariable || args[n]->type.isTemporary || makingCopy );
  1689. #endif
  1690. if( (args[n]->type.isVariable || args[n]->type.isTemporary) )
  1691. {
  1692. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1693. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1694. // as the value allocated on the stack is guaranteed to be safe
  1695. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1696. else
  1697. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1698. }
  1699. }
  1700. if( args[n]->type.dataType.IsObjectHandle() )
  1701. bc->InstrWORD(asBC_ChkNullS, (asWORD)offset);
  1702. }
  1703. else if( descr->inOutFlags[n] != asTM_INOUTREF )
  1704. {
  1705. // If the argument is already known to be safe, i.e. has a guaranteed lifetime,
  1706. // then the address on the stack is already pointing to the correct object so no
  1707. // need to do anything else
  1708. if (!args[n]->type.isRefSafe)
  1709. {
  1710. if (descr->parameterTypes[n].GetTokenType() == ttQuestion &&
  1711. (args[n]->type.dataType.IsObject() || args[n]->type.dataType.IsFuncdef()) &&
  1712. !args[n]->type.dataType.IsObjectHandle())
  1713. {
  1714. // Send the object as a reference to the object,
  1715. // and not to the variable holding the object
  1716. if (!IsVariableOnHeap(args[n]->type.stackOffset))
  1717. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1718. // as the value allocated on the stack is guaranteed to be safe
  1719. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1720. else
  1721. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1722. }
  1723. else if (descr->parameterTypes[n].GetTokenType() == ttQuestion &&
  1724. args[n]->type.dataType.IsObjectHandle() && !args[n]->type.isExplicitHandle)
  1725. {
  1726. // The object handle is being passed as an object, so dereference it before
  1727. // the call so the reference will be to the object rather than to the handle
  1728. if (engine->ep.disallowValueAssignForRefType)
  1729. {
  1730. // With disallow value assign all ref type objects are always passed by handle
  1731. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1732. }
  1733. else
  1734. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1735. }
  1736. else
  1737. {
  1738. // If the variable is really an argument of @& type, then it is necessary
  1739. // to use asBC_GETOBJREF so the pointer is correctly dereferenced.
  1740. sVariable *var = variables->GetVariableByOffset(args[n]->type.stackOffset);
  1741. if (var == 0 || !var->type.IsReference() || !var->type.IsObjectHandle())
  1742. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1743. else
  1744. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1745. }
  1746. }
  1747. }
  1748. }
  1749. else if( descr->parameterTypes[n].IsObject() || descr->parameterTypes[n].IsFuncdef() )
  1750. {
  1751. asASSERT(!args[n]->type.isRefSafe);
  1752. // TODO: value on stack: What can we do to avoid this unnecessary allocation?
  1753. // The object must be allocated on the heap, because this memory will be deleted in as_callfunc_xxx
  1754. asASSERT(IsVariableOnHeap(args[n]->type.stackOffset));
  1755. // The pointer in the variable will be moved to the stack
  1756. bc->InstrWORD(asBC_GETOBJ, (asWORD)offset);
  1757. // Deallocate the variable slot so it can be reused, but do not attempt to
  1758. // free the content of the variable since it was moved to the stack for the call
  1759. DeallocateVariable(args[n]->type.stackOffset);
  1760. args[n]->type.isTemporary = false;
  1761. }
  1762. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  1763. }
  1764. }
  1765. int asCCompiler::CompileArgumentList(asCScriptNode *node, asCArray<asCExprContext*> &args, asCArray<asSNamedArgument> &namedArgs)
  1766. {
  1767. asASSERT(node->nodeType == snArgList);
  1768. // Count arguments
  1769. asCScriptNode *arg = node->firstChild;
  1770. int argCount = 0;
  1771. while( arg )
  1772. {
  1773. if( arg->nodeType != snNamedArgument )
  1774. argCount++;
  1775. arg = arg->next;
  1776. }
  1777. // Prepare the arrays
  1778. args.SetLength(argCount);
  1779. int n;
  1780. for( n = 0; n < argCount; n++ )
  1781. args[n] = 0;
  1782. n = argCount-1;
  1783. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1784. bool anyErrors = false, inPositionalArguments = false;
  1785. arg = node->lastChild;
  1786. while( arg )
  1787. {
  1788. asCScriptNode *asgNode = arg, *namedNode = 0;
  1789. if( asgNode->nodeType == snNamedArgument )
  1790. {
  1791. if( inPositionalArguments )
  1792. {
  1793. Error(TXT_POS_ARG_AFTER_NAMED_ARG, node);
  1794. return -1;
  1795. }
  1796. asgNode = arg->firstChild->next;
  1797. namedNode = arg->firstChild;
  1798. asASSERT( namedNode->nodeType == snIdentifier );
  1799. }
  1800. else
  1801. inPositionalArguments = true;
  1802. asCExprContext expr(engine);
  1803. int r = CompileAssignment(asgNode, &expr);
  1804. if( r < 0 ) anyErrors = true;
  1805. asCExprContext *ctx = asNEW(asCExprContext)(engine);
  1806. if( ctx == 0 )
  1807. {
  1808. // Out of memory
  1809. return -1;
  1810. }
  1811. MergeExprBytecodeAndType(ctx, &expr);
  1812. if( inPositionalArguments )
  1813. {
  1814. args[n] = ctx;
  1815. n--;
  1816. }
  1817. else
  1818. {
  1819. asSNamedArgument namedArg;
  1820. namedArg.name = asCString(&script->code[namedNode->tokenPos], namedNode->tokenLength);
  1821. namedArg.ctx = ctx;
  1822. // Error out when multiple arguments with the same name are passed
  1823. for( asUINT a = 0; a < namedArgs.GetLength(); ++a )
  1824. {
  1825. if( namedArgs[a].name == namedArg.name )
  1826. {
  1827. Error(TXT_DUPLICATE_NAMED_ARG, asgNode);
  1828. anyErrors = true;
  1829. break;
  1830. }
  1831. }
  1832. namedArgs.PushLast(namedArg);
  1833. }
  1834. arg = arg->prev;
  1835. }
  1836. return anyErrors ? -1 : 0;
  1837. }
  1838. int asCCompiler::CompileDefaultAndNamedArgs(asCScriptNode *node, asCArray<asCExprContext*> &args, int funcId, asCObjectType *objectType, asCArray<asSNamedArgument> *namedArgs)
  1839. {
  1840. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  1841. if( func == 0 || args.GetLength() >= (asUINT)func->GetParamCount() )
  1842. return 0;
  1843. // Make sure to use the real function for virtual functions
  1844. if( func->funcType == asFUNC_VIRTUAL )
  1845. {
  1846. asASSERT( objectType );
  1847. func = objectType->virtualFunctionTable[func->vfTableIdx];
  1848. }
  1849. // Make sure none of the variables used in the previous arguments are reused in the default arguments
  1850. bool anyErrors = false;
  1851. int prevReservedVars = reservedVariables.GetLength();
  1852. int explicitArgs = (int)args.GetLength();
  1853. for( int p = 0; p < explicitArgs; p++ )
  1854. args[p]->bc.GetVarsUsed(reservedVariables);
  1855. // Make space for all the new arguments
  1856. args.SetLength(func->parameterTypes.GetLength());
  1857. for( asUINT c = explicitArgs; c < args.GetLength(); c++ )
  1858. args[c] = 0;
  1859. // Add the named arguments to the argument list in the right position
  1860. if( namedArgs )
  1861. {
  1862. for( asUINT n = 0; n < namedArgs->GetLength(); ++n )
  1863. {
  1864. asSNamedArgument &named = (*namedArgs)[n];
  1865. named.ctx->bc.GetVarsUsed(reservedVariables);
  1866. // Find the right spot to put it in
  1867. asUINT index = asUINT(-1);
  1868. for( asUINT j = 0; j < func->parameterTypes.GetLength(); ++j )
  1869. {
  1870. if( func->parameterNames[j] == (*namedArgs)[n].name )
  1871. {
  1872. index = j;
  1873. break;
  1874. }
  1875. }
  1876. asASSERT( index < args.GetLength() );
  1877. args[index] = named.ctx;
  1878. named.ctx = 0;
  1879. }
  1880. }
  1881. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1882. for( int n = (int)func->parameterTypes.GetLength() - 1; n >= explicitArgs; n-- )
  1883. {
  1884. if( args[n] != 0 ) continue;
  1885. if( func->defaultArgs[n] == 0 ) { anyErrors = true; continue; }
  1886. // Parse the default arg string
  1887. asCParser parser(builder);
  1888. asCScriptCode *code = builder->FindOrAddCode("default arg", func->defaultArgs[n]->AddressOf(), func->defaultArgs[n]->GetLength());
  1889. int r = parser.ParseExpression(code);
  1890. if( r < 0 )
  1891. {
  1892. asCString msg;
  1893. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1894. Error(msg, node);
  1895. anyErrors = true;
  1896. continue;
  1897. }
  1898. asCScriptNode *arg = parser.GetScriptNode();
  1899. // Temporarily set the script code to the default arg expression
  1900. asCScriptCode *origScript = script;
  1901. script = code;
  1902. // Don't allow the expression to access local variables
  1903. isCompilingDefaultArg = true;
  1904. // Temporarily set the namespace in the output function to the namespace of the called
  1905. // function so that the default arguments are evaluated in the correct namespace
  1906. asSNameSpace *origNameSpace = outFunc->nameSpace;
  1907. outFunc->nameSpace = func->nameSpace;
  1908. asCExprContext expr(engine);
  1909. r = CompileExpression(arg, &expr);
  1910. // Restore the namespace
  1911. outFunc->nameSpace = origNameSpace;
  1912. // Don't allow address of class method
  1913. if( expr.IsClassMethod() )
  1914. {
  1915. // TODO: Improve error message
  1916. Error(TXT_DEF_ARG_TYPE_DOESNT_MATCH, arg);
  1917. r = -1;
  1918. }
  1919. // Make sure the expression can be implicitly converted to the parameter type
  1920. if( r >= 0 )
  1921. {
  1922. asCArray<int> funcs;
  1923. funcs.PushLast(func->id);
  1924. asCArray<asSOverloadCandidate> matches;
  1925. if( MatchArgument(funcs, matches, &expr, n) == 0 )
  1926. {
  1927. Error(TXT_DEF_ARG_TYPE_DOESNT_MATCH, arg);
  1928. r = -1;
  1929. }
  1930. }
  1931. isCompilingDefaultArg = false;
  1932. script = origScript;
  1933. if( r < 0 )
  1934. {
  1935. asCString msg;
  1936. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1937. Error(msg, node);
  1938. anyErrors = true;
  1939. continue;
  1940. }
  1941. args[n] = asNEW(asCExprContext)(engine);
  1942. if( args[n] == 0 )
  1943. {
  1944. // Out of memory
  1945. reservedVariables.SetLength(prevReservedVars);
  1946. return -1;
  1947. }
  1948. MergeExprBytecodeAndType(args[n], &expr);
  1949. if (args[n]->exprNode)
  1950. {
  1951. // Disconnect the node from the parser, and tell the compiler to free it when complete
  1952. args[n]->exprNode->DisconnectParent();
  1953. nodesToFreeUponComplete.PushLast(args[n]->exprNode);
  1954. }
  1955. }
  1956. reservedVariables.SetLength(prevReservedVars);
  1957. return anyErrors ? -1 : 0;
  1958. }
  1959. asUINT asCCompiler::MatchFunctions(asCArray<int> &funcs, asCArray<asCExprContext*> &args, asCScriptNode *node, const char *name, asCArray<asSNamedArgument> *namedArgs, asCObjectType *objectType, bool isConstMethod, bool silent, bool allowObjectConstruct, const asCString &scope)
  1960. {
  1961. asCArray<int> origFuncs = funcs; // Keep the original list for error message
  1962. asUINT cost = 0;
  1963. asUINT n;
  1964. if( funcs.GetLength() > 0 )
  1965. {
  1966. // Check the number of parameters in the found functions
  1967. asUINT totalArgs = (asUINT)args.GetLength();
  1968. if( namedArgs != 0 )
  1969. totalArgs += (asUINT)namedArgs->GetLength();
  1970. for( n = 0; n < funcs.GetLength(); ++n )
  1971. {
  1972. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  1973. if( desc->parameterTypes.GetLength() != totalArgs )
  1974. {
  1975. bool noMatch = true;
  1976. if( totalArgs < desc->parameterTypes.GetLength() )
  1977. {
  1978. // For virtual functions, the default args are defined in the real function of the object
  1979. if( desc->funcType == asFUNC_VIRTUAL )
  1980. desc = objectType->virtualFunctionTable[desc->vfTableIdx];
  1981. // Count the number of default args
  1982. asUINT defaultArgs = 0;
  1983. for( asUINT d = 0; d < desc->defaultArgs.GetLength(); d++ )
  1984. if( desc->defaultArgs[d] )
  1985. defaultArgs++;
  1986. if( totalArgs >= desc->parameterTypes.GetLength() - defaultArgs )
  1987. noMatch = false;
  1988. }
  1989. if( noMatch )
  1990. {
  1991. // remove it from the list
  1992. if( n == funcs.GetLength()-1 )
  1993. funcs.PopLast();
  1994. else
  1995. funcs[n] = funcs.PopLast();
  1996. n--;
  1997. }
  1998. }
  1999. }
  2000. // Match functions with the parameters, and discard those that do not match
  2001. asCArray<asSOverloadCandidate> matchingFuncs;
  2002. matchingFuncs.SetLengthNoConstruct( funcs.GetLength() );
  2003. for ( n = 0; n < funcs.GetLength(); ++n )
  2004. {
  2005. matchingFuncs[n].funcId = funcs[n];
  2006. matchingFuncs[n].cost = 0;
  2007. }
  2008. // Match positionally passed arguments
  2009. for( n = 0; n < args.GetLength(); ++n )
  2010. {
  2011. asCArray<asSOverloadCandidate> tempFuncs;
  2012. MatchArgument(funcs, tempFuncs, args[n], n, allowObjectConstruct);
  2013. // Intersect the found functions with the list of matching functions
  2014. for( asUINT f = 0; f < matchingFuncs.GetLength(); f++ )
  2015. {
  2016. asUINT c;
  2017. for( c = 0; c < tempFuncs.GetLength(); c++ )
  2018. {
  2019. if( matchingFuncs[f].funcId == tempFuncs[c].funcId )
  2020. {
  2021. // Sum argument cost
  2022. matchingFuncs[f].cost += tempFuncs[c].cost;
  2023. break;
  2024. } // End if match
  2025. }
  2026. // Was the function a match?
  2027. if( c == tempFuncs.GetLength() )
  2028. {
  2029. // No, remove it from the list
  2030. if( f == matchingFuncs.GetLength()-1 )
  2031. matchingFuncs.PopLast();
  2032. else
  2033. matchingFuncs[f] = matchingFuncs.PopLast();
  2034. f--;
  2035. }
  2036. }
  2037. }
  2038. // Match named arguments
  2039. if( namedArgs != 0 )
  2040. {
  2041. for( asUINT i = 0; i < matchingFuncs.GetLength(); ++i )
  2042. {
  2043. asCScriptFunction *desc = builder->GetFunctionDescription(matchingFuncs[i].funcId);
  2044. if( desc->funcType == asFUNC_VIRTUAL )
  2045. desc = objectType->virtualFunctionTable[desc->vfTableIdx];
  2046. // Match every named argument to an argument in the function
  2047. for( n = 0; n < namedArgs->GetLength(); ++n )
  2048. (*namedArgs)[n].match = asUINT(-1);
  2049. bool matchedAll = true;
  2050. for( asUINT j = 0; j < desc->parameterTypes.GetLength(); ++j )
  2051. {
  2052. asUINT match = asUINT(-1);
  2053. for( n = 0; n < namedArgs->GetLength(); ++n )
  2054. {
  2055. asSNamedArgument &namedArg = (*namedArgs)[n];
  2056. if( desc->parameterNames[j] == namedArg.name )
  2057. {
  2058. namedArg.match = j;
  2059. match = n;
  2060. break;
  2061. }
  2062. }
  2063. // Check that every position is filled somehow
  2064. if( j >= args.GetLength() )
  2065. {
  2066. if( match == asUINT(-1) && !desc->defaultArgs[j] )
  2067. {
  2068. // No argument was found for this, and there is no
  2069. // default, so it doesn't work.
  2070. matchedAll = false;
  2071. break;
  2072. }
  2073. }
  2074. else
  2075. {
  2076. if( match != asUINT(-1) )
  2077. {
  2078. // Can't name an argument that was already passed
  2079. matchedAll = false;
  2080. break;
  2081. }
  2082. }
  2083. }
  2084. // Check that every named argument was matched
  2085. if( matchedAll )
  2086. {
  2087. for( n = 0; n < namedArgs->GetLength(); ++n )
  2088. {
  2089. asSNamedArgument &named = (*namedArgs)[n];
  2090. if( named.match == asUINT(-1) )
  2091. {
  2092. matchedAll = false;
  2093. break;
  2094. }
  2095. // Add to the cost
  2096. cost = MatchArgument(desc, named.ctx, named.match, allowObjectConstruct);
  2097. if( cost == asUINT(-1) )
  2098. {
  2099. matchedAll = false;
  2100. break;
  2101. }
  2102. matchingFuncs[i].cost += cost;
  2103. }
  2104. }
  2105. if( !matchedAll )
  2106. {
  2107. // Remove the function, we didn't match all the arguments.
  2108. if( i == matchingFuncs.GetLength()-1 )
  2109. matchingFuncs.PopLast();
  2110. else
  2111. matchingFuncs[i] = matchingFuncs.PopLast();
  2112. i--;
  2113. }
  2114. }
  2115. }
  2116. // Select the overload(s) with the lowest overall cost
  2117. funcs.SetLength(0);
  2118. asUINT bestCost = asUINT(-1);
  2119. for( n = 0; n < matchingFuncs.GetLength(); ++n )
  2120. {
  2121. cost = matchingFuncs[n].cost;
  2122. if( cost < bestCost )
  2123. {
  2124. funcs.SetLength(0);
  2125. bestCost = cost;
  2126. }
  2127. if( cost == bestCost )
  2128. funcs.PushLast( matchingFuncs[n].funcId );
  2129. }
  2130. // Cost returned is equivalent to the best cost discovered
  2131. cost = bestCost;
  2132. }
  2133. if( !isConstMethod )
  2134. FilterConst(funcs);
  2135. if( funcs.GetLength() != 1 && !silent )
  2136. {
  2137. // Build a readable string of the function with parameter types
  2138. bool attemptsPassingClassMethod = false;
  2139. asCString str;
  2140. if( scope != "" && scope != "::" )
  2141. str = scope + "::";
  2142. str += name;
  2143. str += "(";
  2144. for( n = 0; n < args.GetLength(); n++ )
  2145. {
  2146. if( n > 0 )
  2147. str += ", ";
  2148. if( args[n]->methodName != "" )
  2149. {
  2150. if( args[n]->IsClassMethod() )
  2151. {
  2152. attemptsPassingClassMethod = true;
  2153. str += args[n]->type.dataType.GetTypeInfo()->GetName();
  2154. str += "::";
  2155. }
  2156. str += args[n]->methodName;
  2157. }
  2158. else if (args[n]->IsAnonymousInitList())
  2159. {
  2160. str += "{...}";
  2161. }
  2162. else
  2163. str += args[n]->type.dataType.Format(outFunc->nameSpace);
  2164. }
  2165. if( namedArgs != 0 )
  2166. {
  2167. for( n = 0; n < namedArgs->GetLength(); n++ )
  2168. {
  2169. if( n > 0 || args.GetLength() )
  2170. str += ", ";
  2171. asSNamedArgument &named = (*namedArgs)[n];
  2172. str += named.name;
  2173. str += ": ";
  2174. if( named.ctx->methodName != "" )
  2175. str += named.ctx->methodName;
  2176. else
  2177. str += named.ctx->type.dataType.Format(outFunc->nameSpace);
  2178. }
  2179. }
  2180. str += ")";
  2181. if( isConstMethod )
  2182. str += " const";
  2183. if( objectType && scope == "" )
  2184. str = objectType->name + "::" + str;
  2185. if( funcs.GetLength() == 0 )
  2186. {
  2187. str.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  2188. Error(str, node);
  2189. if( attemptsPassingClassMethod )
  2190. {
  2191. // Class methods must use delegate objects
  2192. Error(TXT_CANNOT_PASS_CLASS_METHOD_AS_ARG, node);
  2193. }
  2194. else
  2195. {
  2196. // Print the list of candidates
  2197. if( origFuncs.GetLength() > 0 )
  2198. {
  2199. int r = 0, c = 0;
  2200. asASSERT( node );
  2201. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2202. builder->WriteInfo(script->name.AddressOf(), TXT_CANDIDATES_ARE, r, c, false);
  2203. PrintMatchingFuncs(origFuncs, node, objectType);
  2204. }
  2205. }
  2206. }
  2207. else
  2208. {
  2209. asASSERT( attemptsPassingClassMethod == false );
  2210. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  2211. Error(str, node);
  2212. PrintMatchingFuncs(funcs, node, objectType);
  2213. }
  2214. }
  2215. return cost;
  2216. }
  2217. bool asCCompiler::CompileAutoType(asCDataType &type, asCExprContext &compiledCtx, asCScriptNode *node, asCScriptNode *errNode)
  2218. {
  2219. if( node && node->nodeType == snAssignment )
  2220. {
  2221. int r = CompileAssignment(node, &compiledCtx);
  2222. if( r >= 0 )
  2223. {
  2224. // Must not have unused ambiguous names
  2225. if (compiledCtx.IsClassMethod() || compiledCtx.IsGlobalFunc())
  2226. {
  2227. // TODO: Should mention that the problem is the ambiguous name
  2228. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2229. return false;
  2230. }
  2231. // Must not have unused anonymous functions
  2232. if (compiledCtx.IsLambda())
  2233. {
  2234. // TODO: Should mention that the problem is the anonymous function
  2235. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2236. return false;
  2237. }
  2238. // Must not be a null handle
  2239. if (compiledCtx.type.dataType.IsNullHandle())
  2240. {
  2241. // TODO: Should mention that the problem is the null pointer
  2242. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2243. return false;
  2244. }
  2245. asCDataType newType = compiledCtx.type.dataType;
  2246. // Handle const qualifier on auto
  2247. if (type.IsReadOnly())
  2248. newType.MakeReadOnly(true);
  2249. else if (type.IsHandleToConst())
  2250. newType.MakeHandleToConst(true);
  2251. else if (newType.IsPrimitive())
  2252. newType.MakeReadOnly(false);
  2253. // Handle reference/value stuff
  2254. newType.MakeReference(false);
  2255. if (!newType.IsObjectHandle())
  2256. {
  2257. // We got a value object or an object reference.
  2258. // Turn the variable into a handle if specified
  2259. // as auto@, otherwise make it a 'value'.
  2260. if (type.IsHandleToAuto())
  2261. {
  2262. if (newType.MakeHandle(true) < 0)
  2263. {
  2264. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, errNode);
  2265. return false;
  2266. }
  2267. }
  2268. }
  2269. // Implicit handle types should always be handles
  2270. if (newType.GetTypeInfo() &&
  2271. (newType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE))
  2272. newType.MakeHandle(true);
  2273. // For types that support handles auto should prefer handle
  2274. // as it is more efficient than making a copy
  2275. if( newType.SupportHandles() )
  2276. newType.MakeHandle(true);
  2277. type = newType;
  2278. return true;
  2279. }
  2280. return false;
  2281. }
  2282. else
  2283. {
  2284. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2285. type = asCDataType::CreatePrimitive(ttInt, false);
  2286. return false;
  2287. }
  2288. }
  2289. void asCCompiler::CompileDeclaration(asCScriptNode *decl, asCByteCode *bc)
  2290. {
  2291. // Get the data type
  2292. asCDataType type = builder->CreateDataTypeFromNode(decl->firstChild, script, outFunc->nameSpace, false, outFunc->objectType);
  2293. // Declare all variables in this declaration
  2294. asCScriptNode *node = decl->firstChild->next;
  2295. while( node )
  2296. {
  2297. // If this is an auto type, we have to compile the assignment now to figure out the type
  2298. asCExprContext compiledCtx(engine);
  2299. bool preCompiled = false;
  2300. if (type.IsAuto())
  2301. {
  2302. preCompiled = CompileAutoType(type, compiledCtx, node->next, node);
  2303. if (!preCompiled)
  2304. {
  2305. // If it wasn't possible to determine the type from the expression then there
  2306. // is no need to continue with the initialization. The error was already reported
  2307. // in CompileAutoType.
  2308. return;
  2309. }
  2310. }
  2311. // Is the type allowed?
  2312. if( !type.CanBeInstantiated() )
  2313. {
  2314. asCString str;
  2315. if( type.IsAbstractClass() )
  2316. str.Format(TXT_ABSTRACT_CLASS_s_CANNOT_BE_INSTANTIATED, type.Format(outFunc->nameSpace).AddressOf());
  2317. else if( type.IsInterface() )
  2318. str.Format(TXT_INTERFACE_s_CANNOT_BE_INSTANTIATED, type.Format(outFunc->nameSpace).AddressOf());
  2319. else
  2320. // TODO: Improve error message to explain why
  2321. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format(outFunc->nameSpace).AddressOf());
  2322. Error(str, node);
  2323. // Don't continue, as it will most likely lead to further
  2324. // errors that may just mislead the script writer
  2325. return;
  2326. }
  2327. // A shared object may not declare variables of non-shared types
  2328. if( outFunc->IsShared() )
  2329. {
  2330. asCTypeInfo *ot = type.GetTypeInfo();
  2331. if( ot && !ot->IsShared() )
  2332. {
  2333. asCString msg;
  2334. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  2335. Error(msg, decl);
  2336. }
  2337. }
  2338. // Get the name of the identifier
  2339. asCString name(&script->code[node->tokenPos], node->tokenLength);
  2340. // Verify that the name isn't used by a dynamic data type
  2341. // TODO: Must check against registered funcdefs too
  2342. if( engine->GetRegisteredType(name.AddressOf(), outFunc->nameSpace) != 0 )
  2343. {
  2344. asCString str;
  2345. str.Format(TXT_ILLEGAL_VARIABLE_NAME_s, name.AddressOf());
  2346. Error(str, node);
  2347. }
  2348. int offset = AllocateVariable(type, false);
  2349. if( variables->DeclareVariable(name.AddressOf(), type, offset, IsVariableOnHeap(offset)) < 0 )
  2350. {
  2351. // TODO: It might be an out-of-memory too
  2352. asCString str;
  2353. str.Format(TXT_s_ALREADY_DECLARED, name.AddressOf());
  2354. Error(str, node);
  2355. // Don't continue after this error, as it will just
  2356. // lead to more errors that are likely false
  2357. return;
  2358. }
  2359. else
  2360. {
  2361. // Warn if this variable hides another variable in a higher scope
  2362. if( variables->parent && variables->parent->GetVariable(name.AddressOf()) )
  2363. {
  2364. asCString str;
  2365. str.Format(TXT_s_HIDES_VAR_IN_OUTER_SCOPE, name.AddressOf());
  2366. Warning(str, node);
  2367. }
  2368. }
  2369. // Add marker that the variable has been declared
  2370. bc->VarDecl((int)outFunc->scriptData->variables.GetLength());
  2371. outFunc->AddVariable(name, type, offset);
  2372. // Keep the node for the variable decl
  2373. asCScriptNode *varNode = node;
  2374. node = node->next;
  2375. if( node == 0 || node->nodeType == snIdentifier )
  2376. {
  2377. // Initialize with default constructor
  2378. CompileInitialization(0, bc, type, varNode, offset, 0, 0);
  2379. }
  2380. else
  2381. {
  2382. // Compile the initialization expression
  2383. asQWORD constantValue = 0;
  2384. if( CompileInitialization(node, bc, type, varNode, offset, &constantValue, 0, preCompiled ? &compiledCtx : 0) )
  2385. {
  2386. // Check if the variable should be marked as pure constant
  2387. if( type.IsPrimitive() && type.IsReadOnly() )
  2388. {
  2389. sVariable *v = variables->GetVariable(name.AddressOf());
  2390. v->isPureConstant = true;
  2391. v->constantValue = constantValue;
  2392. }
  2393. }
  2394. node = node->next;
  2395. }
  2396. }
  2397. bc->OptimizeLocally(tempVariableOffsets);
  2398. }
  2399. // Returns true if the initialization expression is a constant expression
  2400. bool asCCompiler::CompileInitialization(asCScriptNode *node, asCByteCode *bc, const asCDataType &type, asCScriptNode *errNode, int offset, asQWORD *constantValue, int isVarGlobOrMem, asCExprContext *preCompiled)
  2401. {
  2402. bool isConstantExpression = false;
  2403. if( node && node->nodeType == snArgList )
  2404. {
  2405. // Make sure it is an object and not a handle
  2406. if( type.GetTypeInfo() == 0 || type.IsObjectHandle() )
  2407. {
  2408. Error(TXT_MUST_BE_OBJECT, node);
  2409. }
  2410. else
  2411. {
  2412. // Compile the arguments
  2413. asCArray<asCExprContext *> args;
  2414. asCArray<asSNamedArgument> namedArgs;
  2415. if( CompileArgumentList(node, args, namedArgs) >= 0 )
  2416. {
  2417. // Find all constructors
  2418. asCArray<int> funcs;
  2419. asSTypeBehaviour *beh = type.GetBehaviour();
  2420. if( beh )
  2421. {
  2422. if( type.GetTypeInfo()->flags & asOBJ_REF )
  2423. funcs = beh->factories;
  2424. else
  2425. funcs = beh->constructors;
  2426. }
  2427. asCString str = type.Format(outFunc->nameSpace);
  2428. MatchFunctions(funcs, args, node, str.AddressOf(), &namedArgs);
  2429. if( funcs.GetLength() == 1 )
  2430. {
  2431. // Add the default values for arguments not explicitly supplied
  2432. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(type.GetTypeInfo()), &namedArgs);
  2433. if( r == asSUCCESS )
  2434. {
  2435. asCExprContext ctx(engine);
  2436. if( type.GetTypeInfo() && (type.GetTypeInfo()->flags & asOBJ_REF) )
  2437. {
  2438. if( isVarGlobOrMem == 0 )
  2439. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, offset);
  2440. else
  2441. {
  2442. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  2443. ctx.bc.Instr(asBC_RDSPtr);
  2444. if( isVarGlobOrMem == 1 )
  2445. {
  2446. // Store the returned handle in the global variable
  2447. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2448. }
  2449. else
  2450. {
  2451. // Store the returned handle in the member
  2452. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2453. ctx.bc.Instr(asBC_RDSPtr);
  2454. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2455. }
  2456. if( type.IsFuncdef())
  2457. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  2458. else
  2459. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  2460. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2461. }
  2462. // Pop the reference left by the function call
  2463. ctx.bc.Instr(asBC_PopPtr);
  2464. }
  2465. else
  2466. {
  2467. bool onHeap = false;
  2468. if( isVarGlobOrMem == 0 )
  2469. {
  2470. // When the object is allocated on the heap, the address where the
  2471. // reference will be stored must be pushed on the stack before the
  2472. // arguments. This reference on the stack is safe, even if the script
  2473. // is suspended during the evaluation of the arguments.
  2474. onHeap = IsVariableOnHeap(offset);
  2475. if( onHeap )
  2476. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2477. }
  2478. else if( isVarGlobOrMem == 1 )
  2479. {
  2480. // Push the address of the location where the variable will be stored on the stack.
  2481. // This reference is safe, because the addresses of the global variables cannot change.
  2482. onHeap = true;
  2483. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2484. }
  2485. else
  2486. {
  2487. // Value types may be allocated inline if they are POD types
  2488. onHeap = !(type.IsObject() || type.IsFuncdef()) || type.IsReference() || (type.GetTypeInfo()->flags & asOBJ_REF);
  2489. if( onHeap )
  2490. {
  2491. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2492. ctx.bc.Instr(asBC_RDSPtr);
  2493. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2494. }
  2495. }
  2496. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  2497. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  2498. // When the object is allocated on the stack, the address to the
  2499. // object is pushed on the stack after the arguments as the object pointer
  2500. if( !onHeap )
  2501. {
  2502. if( isVarGlobOrMem == 2 )
  2503. {
  2504. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2505. ctx.bc.Instr(asBC_RDSPtr);
  2506. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2507. }
  2508. else
  2509. {
  2510. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2511. }
  2512. }
  2513. PerformFunctionCall(funcs[0], &ctx, onHeap, &args, CastToObjectType(type.GetTypeInfo()));
  2514. if( isVarGlobOrMem == 0 )
  2515. {
  2516. // Mark the object in the local variable as initialized
  2517. ctx.bc.ObjInfo(offset, asOBJ_INIT);
  2518. }
  2519. }
  2520. bc->AddCode(&ctx.bc);
  2521. }
  2522. }
  2523. }
  2524. // Cleanup
  2525. for( asUINT n = 0; n < args.GetLength(); n++ )
  2526. if( args[n] )
  2527. {
  2528. asDELETE(args[n], asCExprContext);
  2529. }
  2530. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  2531. if( namedArgs[n].ctx )
  2532. {
  2533. asDELETE(namedArgs[n].ctx, asCExprContext);
  2534. }
  2535. }
  2536. }
  2537. else if( node && node->nodeType == snInitList )
  2538. {
  2539. asCExprValue ti;
  2540. ti.Set(type);
  2541. ti.isVariable = (isVarGlobOrMem == 0);
  2542. ti.isTemporary = false;
  2543. ti.stackOffset = (short)offset;
  2544. ti.isLValue = true;
  2545. CompileInitList(&ti, node, bc, isVarGlobOrMem);
  2546. }
  2547. else if( node && node->nodeType == snAssignment )
  2548. {
  2549. // Compile the expression
  2550. asCExprContext newExpr(engine);
  2551. asCExprContext* expr;
  2552. int r = 0;
  2553. if( preCompiled )
  2554. {
  2555. expr = preCompiled;
  2556. }
  2557. else
  2558. {
  2559. expr = &newExpr;
  2560. r = CompileAssignment(node, expr);
  2561. }
  2562. // handles initialized with null doesn't need any bytecode
  2563. // since handles will be initialized to null by default anyway
  2564. if (type.IsObjectHandle() && expr->type.IsNullConstant() && expr->bc.IsSimpleExpression() )
  2565. return false;
  2566. // Look for appropriate constructor
  2567. asCArray<int> funcs;
  2568. asCArray<asCExprContext *> args;
  2569. // Handles must use the handle assignment operation.
  2570. // Types that are ASHANDLE must not allow the use of the constructor in this case,
  2571. // because it is ambiguous whether a value assignment or handle assignment will be done.
  2572. // Only do this if the expression is of the same type, as the expression is an assignment
  2573. // and an initialization constructor may not have the same meaning.
  2574. // TODO: Should allow initialization constructor if it is declared as allowed for implicit conversions.
  2575. if( !type.IsObjectHandle() && !expr->type.isExplicitHandle &&
  2576. !(type.GetTypeInfo() && (type.GetTypeInfo()->GetFlags() & asOBJ_ASHANDLE)) &&
  2577. type.IsEqualExceptRefAndConst(expr->type.dataType) )
  2578. {
  2579. asSTypeBehaviour *beh = type.GetBehaviour();
  2580. if( beh )
  2581. {
  2582. if( type.GetTypeInfo()->flags & asOBJ_REF )
  2583. funcs = beh->factories;
  2584. else
  2585. funcs = beh->constructors;
  2586. }
  2587. asCString str = type.Format(outFunc->nameSpace);
  2588. args.PushLast(expr);
  2589. MatchFunctions(funcs, args, node, str.AddressOf(), 0, 0, 0, true);
  2590. // Make sure the argument is of the right type (and not just compatible with the expression)
  2591. if (funcs.GetLength() == 1)
  2592. {
  2593. asCScriptFunction *f = engine->scriptFunctions[funcs[0]];
  2594. if (!f->parameterTypes[0].IsEqualExceptRefAndConst(expr->type.dataType))
  2595. funcs.PopLast();
  2596. }
  2597. }
  2598. if( funcs.GetLength() == 1 )
  2599. {
  2600. // Use the constructor
  2601. // TODO: clean-up: A large part of this is identical to the initalization with argList above
  2602. // Add the default values for arguments not explicitly supplied
  2603. r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(type.GetTypeInfo()));
  2604. if( r == asSUCCESS )
  2605. {
  2606. asCExprContext ctx(engine);
  2607. if( type.GetTypeInfo() && (type.GetTypeInfo()->flags & asOBJ_REF) )
  2608. {
  2609. if( isVarGlobOrMem == 0 )
  2610. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, offset);
  2611. else
  2612. {
  2613. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  2614. ctx.bc.Instr(asBC_RDSPtr);
  2615. if( isVarGlobOrMem == 1 )
  2616. {
  2617. // Store the returned handle in the global variable
  2618. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2619. }
  2620. else
  2621. {
  2622. // Store the returned handle in the member
  2623. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2624. ctx.bc.Instr(asBC_RDSPtr);
  2625. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2626. }
  2627. if( type.IsFuncdef() )
  2628. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  2629. else
  2630. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  2631. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2632. }
  2633. // Pop the reference left by the function call
  2634. ctx.bc.Instr(asBC_PopPtr);
  2635. }
  2636. else
  2637. {
  2638. bool onHeap = false;
  2639. if( isVarGlobOrMem == 0 )
  2640. {
  2641. // When the object is allocated on the heap, the address where the
  2642. // reference will be stored must be pushed on the stack before the
  2643. // arguments. This reference on the stack is safe, even if the script
  2644. // is suspended during the evaluation of the arguments.
  2645. onHeap = IsVariableOnHeap(offset);
  2646. if( onHeap )
  2647. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2648. }
  2649. else if( isVarGlobOrMem == 1 )
  2650. {
  2651. // Push the address of the location where the variable will be stored on the stack.
  2652. // This reference is safe, because the addresses of the global variables cannot change.
  2653. onHeap = true;
  2654. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2655. }
  2656. else
  2657. {
  2658. // Value types may be allocated inline if they are POD types
  2659. onHeap = !(type.IsObject() || type.IsFuncdef()) || type.IsReference() || (type.GetTypeInfo()->flags & asOBJ_REF);
  2660. if( onHeap )
  2661. {
  2662. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2663. ctx.bc.Instr(asBC_RDSPtr);
  2664. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2665. }
  2666. }
  2667. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  2668. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  2669. // When the object is allocated on the stack, the address to the
  2670. // object is pushed on the stack after the arguments as the object pointer
  2671. if( !onHeap )
  2672. {
  2673. if( isVarGlobOrMem == 2 )
  2674. {
  2675. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2676. ctx.bc.Instr(asBC_RDSPtr);
  2677. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2678. }
  2679. else
  2680. {
  2681. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2682. }
  2683. }
  2684. PerformFunctionCall(funcs[0], &ctx, onHeap, &args, CastToObjectType(type.GetTypeInfo()));
  2685. if( isVarGlobOrMem == 0 )
  2686. {
  2687. // Mark the object in the local variable as initialized
  2688. ctx.bc.ObjInfo(offset, asOBJ_INIT);
  2689. }
  2690. }
  2691. bc->AddCode(&ctx.bc);
  2692. }
  2693. }
  2694. else
  2695. {
  2696. // Call the default constructor, then call the assignment operator
  2697. asCExprContext ctx(engine);
  2698. // Call the default constructor here
  2699. if( isVarGlobOrMem == 0 )
  2700. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), &ctx.bc, errNode);
  2701. else if( isVarGlobOrMem == 1 )
  2702. CallDefaultConstructor(type, offset, true, &ctx.bc, errNode, isVarGlobOrMem);
  2703. else if( isVarGlobOrMem == 2 )
  2704. CallDefaultConstructor(type, offset, type.IsReference(), &ctx.bc, errNode, isVarGlobOrMem);
  2705. if( r >= 0 )
  2706. {
  2707. if( type.IsPrimitive() )
  2708. {
  2709. if( type.IsReadOnly() && expr->type.isConstant )
  2710. {
  2711. ImplicitConversion(expr, type, node, asIC_IMPLICIT_CONV);
  2712. // Tell caller that the expression is a constant so it can mark the variable as pure constant
  2713. isConstantExpression = true;
  2714. *constantValue = expr->type.GetConstantData();
  2715. }
  2716. asCExprContext lctx(engine);
  2717. if( isVarGlobOrMem == 0 )
  2718. lctx.type.SetVariable(type, offset, false);
  2719. else if( isVarGlobOrMem == 1 )
  2720. {
  2721. lctx.type.Set(type);
  2722. lctx.type.dataType.MakeReference(true);
  2723. // If it is an enum value, i.e. offset is negative, that is being compiled then
  2724. // we skip this as the bytecode won't be used anyway, only the constant value
  2725. if( offset >= 0 )
  2726. lctx.bc.InstrPTR(asBC_LDG, engine->globalProperties[offset]->GetAddressOfValue());
  2727. }
  2728. else
  2729. {
  2730. asASSERT( isVarGlobOrMem == 2 );
  2731. lctx.type.Set(type);
  2732. lctx.type.dataType.MakeReference(true);
  2733. // Load the reference of the primitive member into the register
  2734. lctx.bc.InstrSHORT(asBC_PSF, 0);
  2735. lctx.bc.Instr(asBC_RDSPtr);
  2736. lctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2737. lctx.bc.Instr(asBC_PopRPtr);
  2738. }
  2739. lctx.type.dataType.MakeReadOnly(false);
  2740. lctx.type.isLValue = true;
  2741. DoAssignment(&ctx, &lctx, expr, node, node, ttAssignment, node);
  2742. ProcessDeferredParams(&ctx);
  2743. }
  2744. else
  2745. {
  2746. // TODO: runtime optimize: Here we should look for the best matching constructor, instead of
  2747. // just the copy constructor. Only if no appropriate constructor is
  2748. // available should the assignment operator be used.
  2749. asCExprContext lexpr(engine);
  2750. lexpr.type.Set(type);
  2751. if( isVarGlobOrMem == 0 )
  2752. lexpr.type.dataType.MakeReference(IsVariableOnHeap(offset));
  2753. else if( isVarGlobOrMem == 1 )
  2754. lexpr.type.dataType.MakeReference(true);
  2755. else if( isVarGlobOrMem == 2 )
  2756. {
  2757. if( !lexpr.type.dataType.IsObject() || lexpr.type.dataType.IsFuncdef() || (lexpr.type.dataType.GetTypeInfo()->flags & asOBJ_REF) )
  2758. lexpr.type.dataType.MakeReference(true);
  2759. }
  2760. // Allow initialization of constant variables
  2761. lexpr.type.dataType.MakeReadOnly(false);
  2762. if( type.IsObjectHandle() )
  2763. lexpr.type.isExplicitHandle = true;
  2764. if( isVarGlobOrMem == 0 )
  2765. {
  2766. lexpr.bc.InstrSHORT(asBC_PSF, (short)offset);
  2767. lexpr.type.stackOffset = (short)offset;
  2768. lexpr.type.isVariable = true;
  2769. }
  2770. else if( isVarGlobOrMem == 1 )
  2771. {
  2772. lexpr.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2773. }
  2774. else
  2775. {
  2776. lexpr.bc.InstrSHORT(asBC_PSF, 0);
  2777. lexpr.bc.Instr(asBC_RDSPtr);
  2778. lexpr.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2779. lexpr.type.stackOffset = -1;
  2780. }
  2781. lexpr.type.isLValue = true;
  2782. // If left expression resolves into a registered type
  2783. // check if the assignment operator is overloaded, and check
  2784. // the type of the right hand expression. If none is found
  2785. // the default action is a direct copy if it is the same type
  2786. // and a simple assignment.
  2787. bool assigned = false;
  2788. // Even though an ASHANDLE can be an explicit handle the overloaded operator needs to be called
  2789. if( (lexpr.type.dataType.IsObject() || lexpr.type.dataType.IsFuncdef()) && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetTypeInfo() && (lexpr.type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE))) )
  2790. {
  2791. bool useHndlAssign = false;
  2792. if (lexpr.type.dataType.IsHandleToAsHandleType())
  2793. {
  2794. useHndlAssign = true;
  2795. // Make sure the right hand expression is treated as a handle
  2796. if (!expr->type.isExplicitHandle && !expr->type.IsNullConstant() )
  2797. {
  2798. // TODO: Clean-up: This code is from CompileExpressionPreOp. Create a reusable function
  2799. // Convert the expression to a handle
  2800. if (!expr->type.dataType.IsObjectHandle() && expr->type.dataType.GetTypeInfo() && !(expr->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE))
  2801. {
  2802. asCDataType to = expr->type.dataType;
  2803. to.MakeHandle(true);
  2804. to.MakeReference(true);
  2805. to.MakeHandleToConst(expr->type.dataType.IsReadOnly());
  2806. ImplicitConversion(expr, to, node, asIC_IMPLICIT_CONV, true, false);
  2807. asASSERT(expr->type.dataType.IsObjectHandle());
  2808. }
  2809. else if (expr->type.dataType.GetTypeInfo() && expr->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE)
  2810. {
  2811. // For the ASHANDLE type we'll simply set the expression as a handle
  2812. expr->type.dataType.MakeHandle(true);
  2813. }
  2814. if( !expr->type.dataType.IsObjectHandle() && !expr->type.dataType.SupportHandles())
  2815. {
  2816. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  2817. }
  2818. expr->type.isExplicitHandle = true;
  2819. }
  2820. }
  2821. assigned = CompileOverloadedDualOperator(node, &lexpr, expr, false, &ctx, useHndlAssign);
  2822. if( assigned )
  2823. {
  2824. // Pop the resulting value
  2825. if( !ctx.type.dataType.IsPrimitive() )
  2826. ctx.bc.Instr(asBC_PopPtr);
  2827. // Release the argument
  2828. ProcessDeferredParams(&ctx);
  2829. // Release temporary variable that may be allocated by the overloaded operator
  2830. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  2831. }
  2832. }
  2833. if( !assigned )
  2834. {
  2835. PrepareForAssignment(&lexpr.type.dataType, expr, node, false);
  2836. // If the expression is constant and the variable also is constant
  2837. // then mark the variable as pure constant. This will allow the compiler
  2838. // to optimize expressions with this variable.
  2839. if( type.IsReadOnly() && expr->type.isConstant )
  2840. {
  2841. isConstantExpression = true;
  2842. *constantValue = expr->type.GetConstantQW();
  2843. }
  2844. // Add expression code to bytecode
  2845. MergeExprBytecode(&ctx, expr);
  2846. // Add byte code for storing value of expression in variable
  2847. ctx.bc.AddCode(&lexpr.bc);
  2848. PerformAssignment(&lexpr.type, &expr->type, &ctx.bc, errNode);
  2849. // Release temporary variables used by expression
  2850. ReleaseTemporaryVariable(expr->type, &ctx.bc);
  2851. ctx.bc.Instr(asBC_PopPtr);
  2852. ProcessDeferredParams(&ctx);
  2853. }
  2854. }
  2855. }
  2856. bc->AddCode(&ctx.bc);
  2857. }
  2858. }
  2859. else
  2860. {
  2861. asASSERT( node == 0 );
  2862. // Call the default constructor here, as no explicit initialization is done
  2863. if( isVarGlobOrMem == 0 )
  2864. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), bc, errNode);
  2865. else if( isVarGlobOrMem == 1 )
  2866. CallDefaultConstructor(type, offset, true, bc, errNode, isVarGlobOrMem);
  2867. else if( isVarGlobOrMem == 2 )
  2868. {
  2869. if( !(type.IsObject() || type.IsFuncdef()) || type.IsReference() || (type.GetTypeInfo()->flags & asOBJ_REF) )
  2870. CallDefaultConstructor(type, offset, true, bc, errNode, isVarGlobOrMem);
  2871. else
  2872. CallDefaultConstructor(type, offset, false, bc, errNode, isVarGlobOrMem);
  2873. }
  2874. }
  2875. return isConstantExpression;
  2876. }
  2877. void asCCompiler::CompileInitList(asCExprValue *var, asCScriptNode *node, asCByteCode *bc, int isVarGlobOrMem)
  2878. {
  2879. // Check if the type supports initialization lists
  2880. if( var->dataType.GetTypeInfo() == 0 ||
  2881. var->dataType.GetBehaviour()->listFactory == 0 )
  2882. {
  2883. asCString str;
  2884. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, var->dataType.Format(outFunc->nameSpace).AddressOf());
  2885. Error(str, node);
  2886. return;
  2887. }
  2888. // Construct the buffer with the elements
  2889. // Find the list factory
  2890. int funcId = var->dataType.GetBehaviour()->listFactory;
  2891. asASSERT( engine->scriptFunctions[funcId]->listPattern );
  2892. // TODO: runtime optimize: A future optimization should be to use the stack space directly
  2893. // for small buffers so that the dynamic allocation is skipped
  2894. // Create a new special object type for the lists. Both asCRestore and the
  2895. // context exception handler will need this to know how to parse the buffer.
  2896. asCObjectType *listPatternType = engine->GetListPatternType(funcId);
  2897. // Allocate a temporary variable to hold the pointer to the buffer
  2898. int bufferVar = AllocateVariable(asCDataType::CreateType(listPatternType, false), true);
  2899. asUINT bufferSize = 0;
  2900. // Evaluate all elements of the list
  2901. asCExprContext valueExpr(engine);
  2902. asCScriptNode *el = node;
  2903. asSListPatternNode *patternNode = engine->scriptFunctions[listPatternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
  2904. int elementsInSubList = -1;
  2905. int r = CompileInitListElement(patternNode, el, engine->GetTypeIdFromDataType(asCDataType::CreateType(listPatternType, false)), short(bufferVar), bufferSize, valueExpr.bc, elementsInSubList);
  2906. asASSERT( r || patternNode == 0 );
  2907. if (r < 0)
  2908. {
  2909. asCString msg;
  2910. msg.Format(TXT_PREV_ERROR_WHILE_COMP_LIST_FOR_TYPE_s, var->dataType.Format(outFunc->nameSpace).AddressOf());
  2911. Error(msg, node);
  2912. }
  2913. // After all values have been evaluated we know the final size of the buffer
  2914. asCExprContext allocExpr(engine);
  2915. allocExpr.bc.InstrSHORT_DW(asBC_AllocMem, short(bufferVar), bufferSize);
  2916. // Merge the bytecode into the final sequence
  2917. bc->AddCode(&allocExpr.bc);
  2918. bc->AddCode(&valueExpr.bc);
  2919. // The object itself is the last to be created and will receive the pointer to the buffer
  2920. asCArray<asCExprContext *> args;
  2921. asCExprContext arg1(engine);
  2922. arg1.type.Set(asCDataType::CreatePrimitive(ttUInt, false));
  2923. arg1.type.dataType.MakeReference(true);
  2924. arg1.bc.InstrSHORT(asBC_PshVPtr, short(bufferVar));
  2925. args.PushLast(&arg1);
  2926. asCExprContext ctx(engine);
  2927. if( var->isVariable )
  2928. {
  2929. asASSERT( isVarGlobOrMem == 0 );
  2930. if( var->dataType.GetTypeInfo()->GetFlags() & asOBJ_REF )
  2931. {
  2932. ctx.bc.AddCode(&arg1.bc);
  2933. // Call factory and store the handle in the given variable
  2934. PerformFunctionCall(funcId, &ctx, false, &args, 0, true, var->stackOffset);
  2935. ctx.bc.Instr(asBC_PopPtr);
  2936. }
  2937. else
  2938. {
  2939. // Call the constructor
  2940. // When the object is allocated on the heap, the address where the
  2941. // reference will be stored must be pushed on the stack before the
  2942. // arguments. This reference on the stack is safe, even if the script
  2943. // is suspended during the evaluation of the arguments.
  2944. bool onHeap = IsVariableOnHeap(var->stackOffset);
  2945. if( onHeap )
  2946. ctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  2947. ctx.bc.AddCode(&arg1.bc);
  2948. // When the object is allocated on the stack, the address to the
  2949. // object is pushed on the stack after the arguments as the object pointer
  2950. if( !onHeap )
  2951. ctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  2952. PerformFunctionCall(funcId, &ctx, onHeap, &args, CastToObjectType(var->dataType.GetTypeInfo()));
  2953. // Mark the object in the local variable as initialized
  2954. ctx.bc.ObjInfo(var->stackOffset, asOBJ_INIT);
  2955. }
  2956. }
  2957. else
  2958. {
  2959. if( var->dataType.GetTypeInfo()->GetFlags() & asOBJ_REF )
  2960. {
  2961. ctx.bc.AddCode(&arg1.bc);
  2962. PerformFunctionCall(funcId, &ctx, false, &args);
  2963. ctx.bc.Instr(asBC_RDSPtr);
  2964. if( isVarGlobOrMem == 1 )
  2965. {
  2966. // Store the returned handle in the global variable
  2967. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  2968. }
  2969. else
  2970. {
  2971. // Store the returned handle in the member
  2972. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2973. ctx.bc.Instr(asBC_RDSPtr);
  2974. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2975. }
  2976. if (var->dataType.IsFuncdef())
  2977. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  2978. else
  2979. ctx.bc.InstrPTR(asBC_REFCPY, var->dataType.GetTypeInfo());
  2980. ctx.bc.Instr(asBC_PopPtr);
  2981. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2982. }
  2983. else
  2984. {
  2985. bool onHeap = true;
  2986. // Put the address where the object pointer will be placed on the stack
  2987. if( isVarGlobOrMem == 1 )
  2988. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  2989. else
  2990. {
  2991. onHeap = !(var->dataType.IsObject() || var->dataType.IsFuncdef()) || var->dataType.IsReference() || (var->dataType.GetTypeInfo()->flags & asOBJ_REF);
  2992. if( onHeap )
  2993. {
  2994. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2995. ctx.bc.Instr(asBC_RDSPtr);
  2996. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2997. }
  2998. }
  2999. // Add the address of the list buffer as the argument
  3000. ctx.bc.AddCode(&arg1.bc);
  3001. if( !onHeap )
  3002. {
  3003. ctx.bc.InstrSHORT(asBC_PSF, 0);
  3004. ctx.bc.Instr(asBC_RDSPtr);
  3005. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  3006. }
  3007. // Call the ALLOC instruction to allocate memory and invoke constructor
  3008. PerformFunctionCall(funcId, &ctx, onHeap, &args, CastToObjectType(var->dataType.GetTypeInfo()));
  3009. }
  3010. }
  3011. bc->AddCode(&ctx.bc);
  3012. // Free the temporary buffer. The FREE instruction will make sure to destroy
  3013. // each element in the buffer so there is no need to do this manually
  3014. bc->InstrW_PTR(asBC_FREE, short(bufferVar), listPatternType);
  3015. ReleaseTemporaryVariable(bufferVar, bc);
  3016. }
  3017. int asCCompiler::CompileInitListElement(asSListPatternNode *&patternNode, asCScriptNode *&valueNode, int bufferTypeId, short bufferVar, asUINT &bufferSize, asCByteCode &bcInit, int &elementsInSubList)
  3018. {
  3019. if( patternNode->type == asLPT_START )
  3020. {
  3021. if( valueNode == 0 || valueNode->nodeType != snInitList )
  3022. {
  3023. Error(TXT_EXPECTED_LIST, valueNode);
  3024. return -1;
  3025. }
  3026. // Compile all values until asLPT_END
  3027. patternNode = patternNode->next;
  3028. asCScriptNode *node = valueNode->firstChild;
  3029. while( patternNode->type != asLPT_END )
  3030. {
  3031. // Check for missing value here, else the error reporting will not have a source position to report the error for
  3032. if( node == 0 && patternNode->type == asLPT_TYPE )
  3033. {
  3034. Error(TXT_NOT_ENOUGH_VALUES_FOR_LIST, valueNode);
  3035. return -1;
  3036. }
  3037. asCScriptNode *errNode = node;
  3038. int r = CompileInitListElement(patternNode, node, bufferTypeId, bufferVar, bufferSize, bcInit, elementsInSubList);
  3039. if( r < 0 ) return r;
  3040. if( r == 1 )
  3041. {
  3042. asASSERT( engine->ep.disallowEmptyListElements );
  3043. // Empty elements in the middle are not allowed
  3044. Error(TXT_EMPTY_LIST_ELEMENT_IS_NOT_ALLOWED, errNode);
  3045. }
  3046. asASSERT( patternNode );
  3047. }
  3048. if( node )
  3049. {
  3050. Error(TXT_TOO_MANY_VALUES_FOR_LIST, valueNode);
  3051. return -1;
  3052. }
  3053. // Move to the next node
  3054. valueNode = valueNode->next;
  3055. patternNode = patternNode->next;
  3056. }
  3057. else if( patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME )
  3058. {
  3059. // TODO: list: repeat_inner should make sure the list has the same size as the inner list, i.e. square area
  3060. // TODO: list: repeat_prev should make sure the list is the same size as the previous
  3061. asEListPatternNodeType repeatType = patternNode->type;
  3062. asCScriptNode *firstValue = valueNode;
  3063. // The following values will be repeated N times
  3064. patternNode = patternNode->next;
  3065. // Keep track of the patternNode so it can be reset
  3066. asSListPatternNode *nextNode = patternNode;
  3067. // Align the buffer size to 4 bytes in case previous value was smaller than 4 bytes
  3068. if( bufferSize & 0x3 )
  3069. bufferSize += 4 - (bufferSize & 0x3);
  3070. // The first dword will hold the number of elements in the list
  3071. asDWORD currSize = bufferSize;
  3072. bufferSize += 4;
  3073. asUINT countElements = 0;
  3074. int elementsInSubSubList = -1;
  3075. asCExprContext ctx(engine);
  3076. while( valueNode )
  3077. {
  3078. patternNode = nextNode;
  3079. asCScriptNode *errNode = valueNode;
  3080. int r = CompileInitListElement(patternNode, valueNode, bufferTypeId, bufferVar, bufferSize, ctx.bc, elementsInSubSubList);
  3081. if( r < 0 ) return r;
  3082. if( r == 0 )
  3083. countElements++;
  3084. else
  3085. {
  3086. asASSERT( r == 1 && engine->ep.disallowEmptyListElements );
  3087. if( valueNode )
  3088. {
  3089. // Empty elements in the middle are not allowed
  3090. Error(TXT_EMPTY_LIST_ELEMENT_IS_NOT_ALLOWED, errNode);
  3091. }
  3092. }
  3093. }
  3094. if( countElements == 0 )
  3095. {
  3096. // Skip the sub pattern that was expected to be repeated, otherwise the caller will try to match these when we return
  3097. patternNode = nextNode;
  3098. if( patternNode->type == asLPT_TYPE )
  3099. patternNode = patternNode->next;
  3100. else if( patternNode->type == asLPT_START )
  3101. {
  3102. int subCount = 1;
  3103. do
  3104. {
  3105. patternNode = patternNode->next;
  3106. if( patternNode->type == asLPT_START )
  3107. subCount++;
  3108. else if( patternNode->type == asLPT_END )
  3109. subCount--;
  3110. } while( subCount > 0 );
  3111. patternNode = patternNode->next;
  3112. }
  3113. }
  3114. // For repeat_same each repeated sublist must have the same size to form a rectangular array
  3115. if( repeatType == asLPT_REPEAT_SAME && elementsInSubList != -1 && asUINT(elementsInSubList) != countElements )
  3116. {
  3117. if( countElements < asUINT(elementsInSubList) )
  3118. Error(TXT_NOT_ENOUGH_VALUES_FOR_LIST, firstValue);
  3119. else
  3120. Error(TXT_TOO_MANY_VALUES_FOR_LIST, firstValue);
  3121. return -1;
  3122. }
  3123. else
  3124. {
  3125. // Return to caller the amount of elments in this sublist
  3126. elementsInSubList = countElements;
  3127. }
  3128. // The first dword in the buffer will hold the number of elements
  3129. bcInit.InstrSHORT_DW_DW(asBC_SetListSize, bufferVar, currSize, countElements);
  3130. // Add the values
  3131. bcInit.AddCode(&ctx.bc);
  3132. }
  3133. else if( patternNode->type == asLPT_TYPE )
  3134. {
  3135. bool isEmpty = false;
  3136. // Determine the size of the element
  3137. asUINT size = 0;
  3138. asCDataType dt = reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType;
  3139. if( valueNode->nodeType == snAssignment || valueNode->nodeType == snInitList )
  3140. {
  3141. asCExprContext lctx(engine);
  3142. asCExprContext rctx(engine);
  3143. if( valueNode->nodeType == snAssignment )
  3144. {
  3145. // Compile the assignment expression
  3146. CompileAssignment(valueNode, &rctx);
  3147. if( dt.GetTokenType() == ttQuestion )
  3148. {
  3149. // Make sure the type is not ambiguous
  3150. DetermineSingleFunc(&rctx, valueNode);
  3151. // We now know the type
  3152. dt = rctx.type.dataType;
  3153. dt.MakeReadOnly(false);
  3154. dt.MakeReference(false);
  3155. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3156. if( bufferSize & 0x3 )
  3157. bufferSize += 4 - (bufferSize & 0x3);
  3158. // When value assignment for reference types us disabled, make sure all ref types are passed in as handles
  3159. if (engine->ep.disallowValueAssignForRefType && dt.SupportHandles())
  3160. dt.MakeHandle(true);
  3161. // Place the type id in the buffer
  3162. bcInit.InstrSHORT_DW_DW(asBC_SetListType, bufferVar, bufferSize, engine->GetTypeIdFromDataType(dt));
  3163. bufferSize += 4;
  3164. }
  3165. }
  3166. else if( valueNode->nodeType == snInitList )
  3167. {
  3168. if( dt.GetTokenType() == ttQuestion )
  3169. {
  3170. // Can't use init lists with var type as it is not possible to determine what type should be allocated
  3171. asCString str;
  3172. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, "?");
  3173. Error(str.AddressOf(), valueNode);
  3174. rctx.type.SetDummy();
  3175. dt = rctx.type.dataType;
  3176. }
  3177. else
  3178. {
  3179. // Allocate a temporary variable that will be initialized with the list
  3180. int offset = AllocateVariable(dt, true);
  3181. rctx.type.Set(dt);
  3182. rctx.type.isVariable = true;
  3183. rctx.type.isTemporary = true;
  3184. rctx.type.stackOffset = (short)offset;
  3185. CompileInitList(&rctx.type, valueNode, &rctx.bc, 0);
  3186. // Put the object on the stack
  3187. rctx.bc.InstrSHORT(asBC_PSF, rctx.type.stackOffset);
  3188. // It is a reference that we place on the stack
  3189. rctx.type.dataType.MakeReference(true);
  3190. }
  3191. }
  3192. // Determine size of the element
  3193. if( dt.IsPrimitive() || (!dt.IsNullHandle() && (dt.GetTypeInfo()->flags & asOBJ_VALUE)) )
  3194. size = dt.GetSizeInMemoryBytes();
  3195. else
  3196. size = AS_PTR_SIZE*4;
  3197. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3198. if( size >= 4 && (bufferSize & 0x3) )
  3199. bufferSize += 4 - (bufferSize & 0x3);
  3200. // Compile the lvalue
  3201. lctx.bc.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3202. lctx.type.Set(dt);
  3203. lctx.type.isLValue = true;
  3204. if( dt.IsPrimitive() )
  3205. {
  3206. lctx.bc.Instr(asBC_PopRPtr);
  3207. lctx.type.dataType.MakeReference(true);
  3208. }
  3209. else if( dt.IsObjectHandle() ||
  3210. dt.GetTypeInfo()->flags & asOBJ_REF )
  3211. {
  3212. lctx.type.isExplicitHandle = true;
  3213. lctx.type.dataType.MakeReference(true);
  3214. }
  3215. else
  3216. {
  3217. asASSERT( dt.GetTypeInfo()->flags & asOBJ_VALUE );
  3218. // Make sure the object has been constructed before the assignment
  3219. // TODO: runtime optimize: Use copy constructor instead of assignment to initialize the objects
  3220. asSTypeBehaviour *beh = dt.GetBehaviour();
  3221. int func = 0;
  3222. if( beh ) func = beh->construct;
  3223. if( func == 0 && (dt.GetTypeInfo()->flags & asOBJ_POD) == 0 )
  3224. {
  3225. asCString str;
  3226. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetTypeInfo()->GetName());
  3227. Error(str, valueNode);
  3228. }
  3229. else if( func )
  3230. {
  3231. // Call the constructor as a normal function
  3232. bcInit.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3233. asCExprContext ctx(engine);
  3234. PerformFunctionCall(func, &ctx, false, 0, CastToObjectType(dt.GetTypeInfo()));
  3235. bcInit.AddCode(&ctx.bc);
  3236. }
  3237. }
  3238. if( lctx.type.dataType.IsNullHandle() )
  3239. {
  3240. // Don't add any code to assign a null handle. RefCpy doesn't work without a known type.
  3241. // The buffer is already initialized to zero in asBC_AllocMem anyway.
  3242. asASSERT( rctx.bc.GetLastInstr() == asBC_PshNull );
  3243. asASSERT( reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType.GetTokenType() == ttQuestion );
  3244. }
  3245. else
  3246. {
  3247. asCExprContext ctx(engine);
  3248. DoAssignment(&ctx, &lctx, &rctx, valueNode, valueNode, ttAssignment, valueNode);
  3249. if( !ctx.type.dataType.IsPrimitive() )
  3250. ctx.bc.Instr(asBC_PopPtr);
  3251. // Release temporary variables used by expression
  3252. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  3253. ProcessDeferredParams(&ctx);
  3254. bcInit.AddCode(&ctx.bc);
  3255. }
  3256. }
  3257. else
  3258. {
  3259. if( builder->engine->ep.disallowEmptyListElements )
  3260. {
  3261. // Empty elements are not allowed, except if it is the last in the list
  3262. isEmpty = true;
  3263. }
  3264. else
  3265. {
  3266. // There is no specific value so we need to fill it with a default value
  3267. if( dt.GetTokenType() == ttQuestion )
  3268. {
  3269. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3270. if( bufferSize & 0x3 )
  3271. bufferSize += 4 - (bufferSize & 0x3);
  3272. // Place the type id for a null handle in the buffer
  3273. bcInit.InstrSHORT_DW_DW(asBC_SetListType, bufferVar, bufferSize, 0);
  3274. bufferSize += 4;
  3275. dt = asCDataType::CreateNullHandle();
  3276. // No need to initialize the handle as the buffer is already initialized with zeroes
  3277. }
  3278. else if( dt.GetTypeInfo() && dt.GetTypeInfo()->flags & asOBJ_VALUE )
  3279. {
  3280. // For value types with default constructor we need to call the constructor
  3281. asSTypeBehaviour *beh = dt.GetBehaviour();
  3282. int func = 0;
  3283. if( beh ) func = beh->construct;
  3284. if( func == 0 && (dt.GetTypeInfo()->flags & asOBJ_POD) == 0 )
  3285. {
  3286. asCString str;
  3287. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetTypeInfo()->GetName());
  3288. Error(str, valueNode);
  3289. }
  3290. else if( func )
  3291. {
  3292. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3293. if( bufferSize & 0x3 )
  3294. bufferSize += 4 - (bufferSize & 0x3);
  3295. // Call the constructor as a normal function
  3296. bcInit.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3297. asCExprContext ctx(engine);
  3298. PerformFunctionCall(func, &ctx, false, 0, CastToObjectType(dt.GetTypeInfo()));
  3299. bcInit.AddCode(&ctx.bc);
  3300. }
  3301. }
  3302. else if( !dt.IsObjectHandle() && dt.GetTypeInfo() && dt.GetTypeInfo()->flags & asOBJ_REF )
  3303. {
  3304. // For ref types (not handles) we need to call the default factory
  3305. asSTypeBehaviour *beh = dt.GetBehaviour();
  3306. int func = 0;
  3307. if( beh ) func = beh->factory;
  3308. if( func == 0 )
  3309. {
  3310. asCString str;
  3311. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetTypeInfo()->GetName());
  3312. Error(str, valueNode);
  3313. }
  3314. else if( func )
  3315. {
  3316. asCExprContext rctx(engine);
  3317. PerformFunctionCall(func, &rctx, false, 0, CastToObjectType(dt.GetTypeInfo()));
  3318. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3319. if( bufferSize & 0x3 )
  3320. bufferSize += 4 - (bufferSize & 0x3);
  3321. asCExprContext lctx(engine);
  3322. lctx.bc.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3323. lctx.type.Set(dt);
  3324. lctx.type.isLValue = true;
  3325. lctx.type.isExplicitHandle = true;
  3326. lctx.type.dataType.MakeReference(true);
  3327. asCExprContext ctx(engine);
  3328. DoAssignment(&ctx, &lctx, &rctx, valueNode, valueNode, ttAssignment, valueNode);
  3329. if( !ctx.type.dataType.IsPrimitive() )
  3330. ctx.bc.Instr(asBC_PopPtr);
  3331. // Release temporary variables used by expression
  3332. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  3333. ProcessDeferredParams(&ctx);
  3334. bcInit.AddCode(&ctx.bc);
  3335. }
  3336. }
  3337. }
  3338. }
  3339. if( !isEmpty )
  3340. {
  3341. // Determine size of the element
  3342. if( dt.IsPrimitive() || (!dt.IsNullHandle() && (dt.GetTypeInfo()->flags & asOBJ_VALUE)) )
  3343. size = dt.GetSizeInMemoryBytes();
  3344. else
  3345. size = AS_PTR_SIZE*4;
  3346. asASSERT( size <= 4 || (size & 0x3) == 0 );
  3347. bufferSize += size;
  3348. }
  3349. // Move to the next element
  3350. patternNode = patternNode->next;
  3351. valueNode = valueNode->next;
  3352. if( isEmpty )
  3353. {
  3354. // The caller will determine if the empty element should be ignored or not
  3355. return 1;
  3356. }
  3357. }
  3358. else
  3359. asASSERT( false );
  3360. return 0;
  3361. }
  3362. void asCCompiler::CompileStatement(asCScriptNode *statement, bool *hasReturn, asCByteCode *bc)
  3363. {
  3364. // Don't clear the hasReturn flag if this is an empty statement
  3365. // to avoid false errors of 'not all paths return'
  3366. if( statement->nodeType != snExpressionStatement || statement->firstChild )
  3367. *hasReturn = false;
  3368. if (statement->nodeType == snStatementBlock)
  3369. CompileStatementBlock(statement, true, hasReturn, bc);
  3370. else if (statement->nodeType == snIf)
  3371. CompileIfStatement(statement, hasReturn, bc);
  3372. else if (statement->nodeType == snFor)
  3373. CompileForStatement(statement, bc);
  3374. else if (statement->nodeType == snWhile)
  3375. CompileWhileStatement(statement, bc);
  3376. else if (statement->nodeType == snDoWhile)
  3377. CompileDoWhileStatement(statement, bc);
  3378. else if (statement->nodeType == snExpressionStatement)
  3379. CompileExpressionStatement(statement, bc);
  3380. else if (statement->nodeType == snBreak)
  3381. CompileBreakStatement(statement, bc);
  3382. else if (statement->nodeType == snContinue)
  3383. CompileContinueStatement(statement, bc);
  3384. else if (statement->nodeType == snSwitch)
  3385. CompileSwitchStatement(statement, hasReturn, bc);
  3386. else if (statement->nodeType == snTryCatch)
  3387. CompileTryCatch(statement, hasReturn, bc);
  3388. else if (statement->nodeType == snReturn)
  3389. {
  3390. CompileReturnStatement(statement, bc);
  3391. *hasReturn = true;
  3392. }
  3393. else
  3394. asASSERT(false);
  3395. }
  3396. void asCCompiler::CompileSwitchStatement(asCScriptNode *snode, bool *, asCByteCode *bc)
  3397. {
  3398. // TODO: inheritance: Must guarantee that all options in the switch case call a constructor, or that none call it.
  3399. // Reserve label for break statements
  3400. int breakLabel = nextLabel++;
  3401. breakLabels.PushLast(breakLabel);
  3402. // Add a variable scope that will be used by CompileBreak
  3403. // to know where to stop deallocating variables
  3404. AddVariableScope(true, false);
  3405. //---------------------------
  3406. // Compile the switch expression
  3407. //-------------------------------
  3408. // Compile the switch expression
  3409. asCExprContext expr(engine);
  3410. CompileAssignment(snode->firstChild, &expr);
  3411. // Verify that the expression is a primitive type
  3412. if( !expr.type.dataType.IsIntegerType() && !expr.type.dataType.IsUnsignedType() )
  3413. {
  3414. Error(TXT_SWITCH_MUST_BE_INTEGRAL, snode->firstChild);
  3415. return;
  3416. }
  3417. if( ProcessPropertyGetAccessor(&expr, snode) < 0 )
  3418. return;
  3419. // TODO: Need to support 64bit integers
  3420. // Convert the expression to a 32bit variable
  3421. asCDataType to;
  3422. if( expr.type.dataType.IsIntegerType() )
  3423. to.SetTokenType(ttInt);
  3424. else if( expr.type.dataType.IsUnsignedType() )
  3425. to.SetTokenType(ttUInt);
  3426. // Make sure the value is in a variable
  3427. if( expr.type.dataType.IsReference() )
  3428. ConvertToVariable(&expr);
  3429. ImplicitConversion(&expr, to, snode->firstChild, asIC_IMPLICIT_CONV, true);
  3430. ConvertToVariable(&expr);
  3431. int offset = expr.type.stackOffset;
  3432. ProcessDeferredParams(&expr);
  3433. //-------------------------------
  3434. // Determine case values and labels
  3435. //--------------------------------
  3436. // Remember the first label so that we can later pass the
  3437. // correct label to each CompileCase()
  3438. int firstCaseLabel = nextLabel;
  3439. int defaultLabel = 0;
  3440. asCArray<int> caseValues;
  3441. asCArray<int> caseLabels;
  3442. // Compile all case comparisons and make them jump to the right label
  3443. asCScriptNode *cnode = snode->firstChild->next;
  3444. while( cnode )
  3445. {
  3446. // Each case should have a constant expression
  3447. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  3448. {
  3449. // Compile expression
  3450. asCExprContext c(engine);
  3451. CompileExpression(cnode->firstChild, &c);
  3452. // Verify that the result is a constant
  3453. if( !c.type.isConstant )
  3454. Error(TXT_SWITCH_CASE_MUST_BE_CONSTANT, cnode->firstChild);
  3455. // Verify that the result is an integral number
  3456. if (!c.type.dataType.IsIntegerType() && !c.type.dataType.IsUnsignedType())
  3457. Error(TXT_SWITCH_MUST_BE_INTEGRAL, cnode->firstChild);
  3458. else
  3459. {
  3460. ImplicitConversion(&c, to, cnode->firstChild, asIC_IMPLICIT_CONV, true);
  3461. // Has this case been declared already?
  3462. if (caseValues.IndexOf(c.type.GetConstantDW()) >= 0)
  3463. Error(TXT_DUPLICATE_SWITCH_CASE, cnode->firstChild);
  3464. // TODO: Optimize: We can insert the numbers sorted already
  3465. // Store constant for later use
  3466. caseValues.PushLast(c.type.GetConstantDW());
  3467. // Reserve label for this case
  3468. caseLabels.PushLast(nextLabel++);
  3469. }
  3470. }
  3471. else
  3472. {
  3473. // TODO: It shouldn't be necessary for the default case to be the last one.
  3474. // Is default the last case?
  3475. if( cnode->next )
  3476. {
  3477. Error(TXT_DEFAULT_MUST_BE_LAST, cnode);
  3478. break;
  3479. }
  3480. // Reserve label for this case
  3481. defaultLabel = nextLabel++;
  3482. }
  3483. cnode = cnode->next;
  3484. }
  3485. // check for empty switch
  3486. if (caseValues.GetLength() == 0)
  3487. {
  3488. Error(TXT_EMPTY_SWITCH, snode);
  3489. return;
  3490. }
  3491. if( defaultLabel == 0 )
  3492. defaultLabel = breakLabel;
  3493. //---------------------------------
  3494. // Output the optimized case comparisons
  3495. // with jumps to the case code
  3496. //------------------------------------
  3497. // Sort the case values by increasing value. Do the sort together with the labels
  3498. // A simple bubble sort is sufficient since we don't expect a huge number of values
  3499. for( asUINT fwd = 1; fwd < caseValues.GetLength(); fwd++ )
  3500. {
  3501. for( int bck = fwd - 1; bck >= 0; bck-- )
  3502. {
  3503. int bckp = bck + 1;
  3504. if( caseValues[bck] > caseValues[bckp] )
  3505. {
  3506. // Swap the values in both arrays
  3507. int swap = caseValues[bckp];
  3508. caseValues[bckp] = caseValues[bck];
  3509. caseValues[bck] = swap;
  3510. swap = caseLabels[bckp];
  3511. caseLabels[bckp] = caseLabels[bck];
  3512. caseLabels[bck] = swap;
  3513. }
  3514. else
  3515. break;
  3516. }
  3517. }
  3518. // Find ranges of consecutive numbers
  3519. asCArray<int> ranges;
  3520. ranges.PushLast(0);
  3521. asUINT n;
  3522. for( n = 1; n < caseValues.GetLength(); ++n )
  3523. {
  3524. // We can join numbers that are less than 5 numbers
  3525. // apart since the output code will still be smaller
  3526. if( caseValues[n] > caseValues[n-1] + 5 )
  3527. ranges.PushLast(n);
  3528. }
  3529. // If the value is larger than the largest case value, jump to default
  3530. int tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3531. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[caseValues.GetLength()-1]);
  3532. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3533. expr.bc.InstrDWORD(asBC_JP, defaultLabel);
  3534. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3535. // TODO: runtime optimize: We could possibly optimize this even more by doing a
  3536. // binary search instead of a linear search through the ranges
  3537. // For each range
  3538. int range;
  3539. for( range = 0; range < (int)ranges.GetLength(); range++ )
  3540. {
  3541. // Find the largest value in this range
  3542. int maxRange = caseValues[ranges[range]];
  3543. int index = ranges[range];
  3544. for( ; (index < (int)caseValues.GetLength()) && (caseValues[index] <= maxRange + 5); index++ )
  3545. maxRange = caseValues[index];
  3546. // If there are only 2 numbers then it is better to compare them directly
  3547. if( index - ranges[range] > 2 )
  3548. {
  3549. // If the value is smaller than the smallest case value in the range, jump to default
  3550. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3551. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  3552. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3553. expr.bc.InstrDWORD(asBC_JS, defaultLabel);
  3554. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3555. int nextRangeLabel = nextLabel++;
  3556. // If this is the last range we don't have to make this test
  3557. if( range < (int)ranges.GetLength() - 1 )
  3558. {
  3559. // If the value is larger than the largest case value in the range, jump to the next range
  3560. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3561. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, maxRange);
  3562. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3563. expr.bc.InstrDWORD(asBC_JP, nextRangeLabel);
  3564. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3565. }
  3566. // Jump forward according to the value
  3567. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3568. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  3569. expr.bc.InstrW_W_W(asBC_SUBi, tmpOffset, offset, tmpOffset);
  3570. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3571. expr.bc.JmpP(tmpOffset, maxRange - caseValues[ranges[range]]);
  3572. // Add the list of jumps to the correct labels (any holes, jump to default)
  3573. index = ranges[range];
  3574. for( int i = caseValues[index]; i <= maxRange; i++ )
  3575. {
  3576. if( caseValues[index] == i )
  3577. expr.bc.InstrINT(asBC_JMP, caseLabels[index++]);
  3578. else
  3579. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  3580. }
  3581. expr.bc.Label((short)nextRangeLabel);
  3582. }
  3583. else
  3584. {
  3585. // Simply make a comparison with each value
  3586. for( int i = ranges[range]; i < index; ++i )
  3587. {
  3588. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3589. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[i]);
  3590. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3591. expr.bc.InstrDWORD(asBC_JZ, caseLabels[i]);
  3592. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3593. }
  3594. }
  3595. }
  3596. // Catch any value that falls trough
  3597. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  3598. // Release the temporary variable previously stored
  3599. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3600. // TODO: optimize: Should optimize each piece individually
  3601. expr.bc.OptimizeLocally(tempVariableOffsets);
  3602. //----------------------------------
  3603. // Output case implementations
  3604. //----------------------------------
  3605. // Compile case implementations, each one with the label before it
  3606. cnode = snode->firstChild->next;
  3607. while( cnode )
  3608. {
  3609. // Each case should have a constant expression
  3610. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  3611. {
  3612. expr.bc.Label((short)firstCaseLabel++);
  3613. CompileCase(cnode->firstChild->next, &expr.bc);
  3614. }
  3615. else
  3616. {
  3617. expr.bc.Label((short)defaultLabel);
  3618. // Is default the last case?
  3619. if( cnode->next )
  3620. {
  3621. // We've already reported this error
  3622. break;
  3623. }
  3624. CompileCase(cnode->firstChild, &expr.bc);
  3625. }
  3626. cnode = cnode->next;
  3627. }
  3628. //--------------------------------
  3629. bc->AddCode(&expr.bc);
  3630. // Add break label
  3631. bc->Label((short)breakLabel);
  3632. breakLabels.PopLast();
  3633. RemoveVariableScope();
  3634. }
  3635. void asCCompiler::CompileCase(asCScriptNode *node, asCByteCode *bc)
  3636. {
  3637. bool isFinished = false;
  3638. bool hasReturn = false;
  3639. bool hasUnreachableCode = false;
  3640. while( node )
  3641. {
  3642. if( !hasUnreachableCode && (hasReturn || isFinished) )
  3643. {
  3644. hasUnreachableCode = true;
  3645. Warning(TXT_UNREACHABLE_CODE, node);
  3646. break;
  3647. }
  3648. if( node->nodeType == snBreak || node->nodeType == snContinue )
  3649. isFinished = true;
  3650. asCByteCode statement(engine);
  3651. if( node->nodeType == snDeclaration )
  3652. {
  3653. Error(TXT_DECL_IN_SWITCH, node);
  3654. // Compile it anyway to avoid further compiler errors
  3655. CompileDeclaration(node, &statement);
  3656. }
  3657. else
  3658. CompileStatement(node, &hasReturn, &statement);
  3659. LineInstr(bc, node->tokenPos);
  3660. bc->AddCode(&statement);
  3661. if( !hasCompileErrors )
  3662. asASSERT( tempVariables.GetLength() == 0 );
  3663. node = node->next;
  3664. }
  3665. }
  3666. void asCCompiler::CompileTryCatch(asCScriptNode *node, bool *hasReturn, asCByteCode *bc)
  3667. {
  3668. // We will use one label before and another after the catch statement
  3669. int beforeCatchLabel = nextLabel++;
  3670. int afterCatchLabel = nextLabel++;
  3671. // Compile the try block
  3672. bool hasReturnTry;
  3673. asCByteCode tryBC(engine);
  3674. CompileStatement(node->firstChild, &hasReturnTry, &tryBC);
  3675. // Add marker to unwind exception until here, then jump to catch block
  3676. bc->TryBlock((short)beforeCatchLabel);
  3677. // Add the byte code
  3678. LineInstr(bc, node->firstChild->tokenPos);
  3679. bc->AddCode(&tryBC);
  3680. // Add jump to after catch
  3681. bc->InstrINT(asBC_JMP, afterCatchLabel);
  3682. // Compile the catch block
  3683. bool hasReturnCatch;
  3684. asCByteCode catchBC(engine);
  3685. CompileStatement(node->firstChild->next, &hasReturnCatch, &catchBC);
  3686. // Add marker to tell bytecode optimizer that this is a catch
  3687. // block so the code is not removed as unreachable code
  3688. bc->Label((short)beforeCatchLabel);
  3689. // Add the byte code
  3690. LineInstr(bc, node->firstChild->next->tokenPos);
  3691. bc->AddCode(&catchBC);
  3692. // Add the label after catch
  3693. bc->Label((short)afterCatchLabel);
  3694. // The try/catch statement only has return (i.e. no code after
  3695. // the try/catch block will be executed) if both blocks have
  3696. *hasReturn = hasReturnTry && hasReturnCatch;
  3697. }
  3698. void asCCompiler::CompileIfStatement(asCScriptNode *inode, bool *hasReturn, asCByteCode *bc)
  3699. {
  3700. // We will use one label for the if statement
  3701. // and possibly another for the else statement
  3702. int afterLabel = nextLabel++;
  3703. // Compile the expression
  3704. asCExprContext expr(engine);
  3705. int r = CompileAssignment(inode->firstChild, &expr);
  3706. if( r == 0 )
  3707. {
  3708. // Allow value types to be converted to bool using 'bool opImplConv()'
  3709. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3710. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), inode, asIC_IMPLICIT_CONV);
  3711. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3712. Error(TXT_EXPR_MUST_BE_BOOL, inode->firstChild);
  3713. else
  3714. {
  3715. if( !expr.type.isConstant )
  3716. {
  3717. if( ProcessPropertyGetAccessor(&expr, inode) < 0 )
  3718. return;
  3719. ConvertToVariable(&expr);
  3720. ProcessDeferredParams(&expr);
  3721. // Add a test
  3722. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3723. expr.bc.Instr(asBC_ClrHi);
  3724. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  3725. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3726. expr.bc.OptimizeLocally(tempVariableOffsets);
  3727. bc->AddCode(&expr.bc);
  3728. }
  3729. #if AS_SIZEOF_BOOL == 1
  3730. else if( expr.type.GetConstantB() == 0 )
  3731. #else
  3732. else if (expr.type.GetConstantDW() == 0)
  3733. #endif
  3734. {
  3735. // Jump to the else case
  3736. bc->InstrINT(asBC_JMP, afterLabel);
  3737. // TODO: Should we warn that the expression will always go to the else?
  3738. }
  3739. }
  3740. }
  3741. // Compile the if statement
  3742. bool origIsConstructorCalled = m_isConstructorCalled;
  3743. bool hasReturn1;
  3744. asCByteCode ifBC(engine);
  3745. CompileStatement(inode->firstChild->next, &hasReturn1, &ifBC);
  3746. // Add the byte code
  3747. LineInstr(bc, inode->firstChild->next->tokenPos);
  3748. bc->AddCode(&ifBC);
  3749. if( inode->firstChild->next->nodeType == snExpressionStatement && inode->firstChild->next->firstChild == 0 )
  3750. {
  3751. // Don't allow if( expr );
  3752. Error(TXT_IF_WITH_EMPTY_STATEMENT, inode->firstChild->next);
  3753. }
  3754. // If one of the statements call the constructor, the other must as well
  3755. // otherwise it is possible the constructor is never called
  3756. bool constructorCall1 = false;
  3757. bool constructorCall2 = false;
  3758. if( !origIsConstructorCalled && m_isConstructorCalled )
  3759. constructorCall1 = true;
  3760. // Do we have an else statement?
  3761. if( inode->firstChild->next != inode->lastChild )
  3762. {
  3763. // Reset the constructor called flag so the else statement can call the constructor too
  3764. m_isConstructorCalled = origIsConstructorCalled;
  3765. int afterElse = 0;
  3766. if( !hasReturn1 )
  3767. {
  3768. afterElse = nextLabel++;
  3769. // Add jump to after the else statement
  3770. bc->InstrINT(asBC_JMP, afterElse);
  3771. }
  3772. // Add label for the else statement
  3773. bc->Label((short)afterLabel);
  3774. bool hasReturn2;
  3775. asCByteCode elseBC(engine);
  3776. CompileStatement(inode->lastChild, &hasReturn2, &elseBC);
  3777. // Add byte code for the else statement
  3778. LineInstr(bc, inode->lastChild->tokenPos);
  3779. bc->AddCode(&elseBC);
  3780. if( inode->lastChild->nodeType == snExpressionStatement && inode->lastChild->firstChild == 0 )
  3781. {
  3782. // Don't allow if( expr ) {} else;
  3783. Error(TXT_ELSE_WITH_EMPTY_STATEMENT, inode->lastChild);
  3784. }
  3785. if( !hasReturn1 )
  3786. {
  3787. // Add label for the end of else statement
  3788. bc->Label((short)afterElse);
  3789. }
  3790. // The if statement only has return if both alternatives have
  3791. *hasReturn = hasReturn1 && hasReturn2;
  3792. if( !origIsConstructorCalled && m_isConstructorCalled )
  3793. constructorCall2 = true;
  3794. }
  3795. else
  3796. {
  3797. // Add label for the end of if statement
  3798. bc->Label((short)afterLabel);
  3799. *hasReturn = false;
  3800. }
  3801. // Make sure both or neither conditions call a constructor
  3802. if( (constructorCall1 && !constructorCall2) ||
  3803. (constructorCall2 && !constructorCall1) )
  3804. {
  3805. Error(TXT_BOTH_CONDITIONS_MUST_CALL_CONSTRUCTOR, inode);
  3806. }
  3807. m_isConstructorCalled = origIsConstructorCalled || constructorCall1 || constructorCall2;
  3808. }
  3809. void asCCompiler::CompileForStatement(asCScriptNode *fnode, asCByteCode *bc)
  3810. {
  3811. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3812. AddVariableScope(true, true);
  3813. // We will use three labels for the for loop
  3814. int conditionLabel = nextLabel++;
  3815. int afterLabel = nextLabel++;
  3816. int continueLabel = nextLabel++;
  3817. int insideLabel = nextLabel++;
  3818. continueLabels.PushLast(continueLabel);
  3819. breakLabels.PushLast(afterLabel);
  3820. //---------------------------------------
  3821. // Compile the initialization statement
  3822. asCByteCode initBC(engine);
  3823. LineInstr(&initBC, fnode->firstChild->tokenPos);
  3824. if( fnode->firstChild->nodeType == snDeclaration )
  3825. CompileDeclaration(fnode->firstChild, &initBC);
  3826. else
  3827. CompileExpressionStatement(fnode->firstChild, &initBC);
  3828. //-----------------------------------
  3829. // Compile the condition statement
  3830. asCExprContext expr(engine);
  3831. asCScriptNode *second = fnode->firstChild->next;
  3832. if( second->firstChild )
  3833. {
  3834. int r = CompileAssignment(second->firstChild, &expr);
  3835. if( r >= 0 )
  3836. {
  3837. // Allow value types to be converted to bool using 'bool opImplConv()'
  3838. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3839. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), second->firstChild, asIC_IMPLICIT_CONV);
  3840. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3841. Error(TXT_EXPR_MUST_BE_BOOL, second);
  3842. else
  3843. {
  3844. if( ProcessPropertyGetAccessor(&expr, second) < 0 )
  3845. return;
  3846. ConvertToVariable(&expr);
  3847. ProcessDeferredParams(&expr);
  3848. // If expression is false exit the loop
  3849. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3850. expr.bc.Instr(asBC_ClrHi);
  3851. expr.bc.InstrDWORD(asBC_JNZ, insideLabel);
  3852. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3853. expr.bc.OptimizeLocally(tempVariableOffsets);
  3854. // Prepend the line instruction for the condition
  3855. asCByteCode tmp(engine);
  3856. LineInstr(&tmp, second->firstChild->tokenPos);
  3857. tmp.AddCode(&expr.bc);
  3858. expr.bc.AddCode(&tmp);
  3859. }
  3860. }
  3861. }
  3862. //---------------------------
  3863. // Compile the increment statement(s)
  3864. asCByteCode nextBC(engine);
  3865. asCScriptNode *cnode = second->next;
  3866. while( cnode && cnode->nodeType == snExpressionStatement && cnode != fnode->lastChild )
  3867. {
  3868. LineInstr(&nextBC, cnode->tokenPos);
  3869. CompileExpressionStatement(cnode, &nextBC);
  3870. cnode = cnode->next;
  3871. }
  3872. //------------------------------
  3873. // Compile loop statement
  3874. bool hasReturn;
  3875. asCByteCode forBC(engine);
  3876. CompileStatement(fnode->lastChild, &hasReturn, &forBC);
  3877. //-------------------------------
  3878. // Join the code pieces
  3879. bc->AddCode(&initBC);
  3880. bc->InstrDWORD(asBC_JMP, conditionLabel);
  3881. bc->Label((short)insideLabel);
  3882. // Add a suspend bytecode inside the loop to guarantee
  3883. // that the application can suspend the execution
  3884. bc->Instr(asBC_SUSPEND);
  3885. bc->InstrPTR(asBC_JitEntry, 0);
  3886. LineInstr(bc, fnode->lastChild->tokenPos);
  3887. bc->AddCode(&forBC);
  3888. bc->Label((short)continueLabel);
  3889. bc->AddCode(&nextBC);
  3890. bc->Label((short)conditionLabel);
  3891. if( expr.bc.GetLastInstr() == -1 )
  3892. // There is no condition, so we just always jump
  3893. bc->InstrDWORD(asBC_JMP, insideLabel);
  3894. else
  3895. bc->AddCode(&expr.bc);
  3896. bc->Label((short)afterLabel);
  3897. continueLabels.PopLast();
  3898. breakLabels.PopLast();
  3899. // Deallocate variables in this block, in reverse order
  3900. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  3901. {
  3902. sVariable *v = variables->variables[n];
  3903. // Call variable destructors here, for variables not yet destroyed
  3904. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  3905. // Don't deallocate function parameters
  3906. if( v->stackOffset > 0 )
  3907. DeallocateVariable(v->stackOffset);
  3908. }
  3909. RemoveVariableScope();
  3910. }
  3911. void asCCompiler::CompileWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  3912. {
  3913. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3914. AddVariableScope(true, true);
  3915. // We will use two labels for the while loop
  3916. int beforeLabel = nextLabel++;
  3917. int afterLabel = nextLabel++;
  3918. continueLabels.PushLast(beforeLabel);
  3919. breakLabels.PushLast(afterLabel);
  3920. // Add label before the expression
  3921. bc->Label((short)beforeLabel);
  3922. // Compile expression
  3923. asCExprContext expr(engine);
  3924. int r = CompileAssignment(wnode->firstChild, &expr);
  3925. if( r == 0 )
  3926. {
  3927. // Allow value types to be converted to bool using 'bool opImplConv()'
  3928. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3929. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), wnode->firstChild, asIC_IMPLICIT_CONV);
  3930. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3931. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  3932. else
  3933. {
  3934. if( ProcessPropertyGetAccessor(&expr, wnode) < 0 )
  3935. return;
  3936. ConvertToVariable(&expr);
  3937. ProcessDeferredParams(&expr);
  3938. // Jump to end of statement if expression is false
  3939. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3940. expr.bc.Instr(asBC_ClrHi);
  3941. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  3942. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3943. expr.bc.OptimizeLocally(tempVariableOffsets);
  3944. bc->AddCode(&expr.bc);
  3945. }
  3946. }
  3947. // Add a suspend bytecode inside the loop to guarantee
  3948. // that the application can suspend the execution
  3949. bc->Instr(asBC_SUSPEND);
  3950. bc->InstrPTR(asBC_JitEntry, 0);
  3951. // Compile statement
  3952. bool hasReturn;
  3953. asCByteCode whileBC(engine);
  3954. CompileStatement(wnode->lastChild, &hasReturn, &whileBC);
  3955. // Add byte code for the statement
  3956. LineInstr(bc, wnode->lastChild->tokenPos);
  3957. bc->AddCode(&whileBC);
  3958. // Jump to the expression
  3959. bc->InstrINT(asBC_JMP, beforeLabel);
  3960. // Add label after the statement
  3961. bc->Label((short)afterLabel);
  3962. continueLabels.PopLast();
  3963. breakLabels.PopLast();
  3964. RemoveVariableScope();
  3965. }
  3966. void asCCompiler::CompileDoWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  3967. {
  3968. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3969. AddVariableScope(true, true);
  3970. // We will use two labels for the while loop
  3971. int beforeLabel = nextLabel++;
  3972. int beforeTest = nextLabel++;
  3973. int afterLabel = nextLabel++;
  3974. continueLabels.PushLast(beforeTest);
  3975. breakLabels.PushLast(afterLabel);
  3976. // Add label before the statement
  3977. bc->Label((short)beforeLabel);
  3978. // Compile statement
  3979. bool hasReturn;
  3980. asCByteCode whileBC(engine);
  3981. CompileStatement(wnode->firstChild, &hasReturn, &whileBC);
  3982. // Add byte code for the statement
  3983. LineInstr(bc, wnode->firstChild->tokenPos);
  3984. bc->AddCode(&whileBC);
  3985. // Add label before the expression
  3986. bc->Label((short)beforeTest);
  3987. // Add a suspend bytecode inside the loop to guarantee
  3988. // that the application can suspend the execution
  3989. bc->Instr(asBC_SUSPEND);
  3990. bc->InstrPTR(asBC_JitEntry, 0);
  3991. // Add a line instruction
  3992. LineInstr(bc, wnode->lastChild->tokenPos);
  3993. // Compile expression
  3994. asCExprContext expr(engine);
  3995. CompileAssignment(wnode->lastChild, &expr);
  3996. // Allow value types to be converted to bool using 'bool opImplConv()'
  3997. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3998. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), wnode->lastChild, asIC_IMPLICIT_CONV);
  3999. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  4000. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  4001. else
  4002. {
  4003. if( ProcessPropertyGetAccessor(&expr, wnode) < 0 )
  4004. return;
  4005. ConvertToVariable(&expr);
  4006. ProcessDeferredParams(&expr);
  4007. // Jump to next iteration if expression is true
  4008. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  4009. expr.bc.Instr(asBC_ClrHi);
  4010. expr.bc.InstrDWORD(asBC_JNZ, beforeLabel);
  4011. ReleaseTemporaryVariable(expr.type, &expr.bc);
  4012. expr.bc.OptimizeLocally(tempVariableOffsets);
  4013. bc->AddCode(&expr.bc);
  4014. }
  4015. // Add label after the statement
  4016. bc->Label((short)afterLabel);
  4017. continueLabels.PopLast();
  4018. breakLabels.PopLast();
  4019. RemoveVariableScope();
  4020. }
  4021. void asCCompiler::CompileBreakStatement(asCScriptNode *node, asCByteCode *bc)
  4022. {
  4023. if( breakLabels.GetLength() == 0 )
  4024. {
  4025. Error(TXT_INVALID_BREAK, node);
  4026. return;
  4027. }
  4028. // Add destructor calls for all variables that will go out of scope
  4029. // Put this clean up in a block to allow exception handler to understand them
  4030. bc->Block(true);
  4031. asCVariableScope *vs = variables;
  4032. while( !vs->isBreakScope )
  4033. {
  4034. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  4035. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  4036. vs = vs->parent;
  4037. }
  4038. bc->Block(false);
  4039. bc->InstrINT(asBC_JMP, breakLabels[breakLabels.GetLength()-1]);
  4040. }
  4041. void asCCompiler::CompileContinueStatement(asCScriptNode *node, asCByteCode *bc)
  4042. {
  4043. if( continueLabels.GetLength() == 0 )
  4044. {
  4045. Error(TXT_INVALID_CONTINUE, node);
  4046. return;
  4047. }
  4048. // Add destructor calls for all variables that will go out of scope
  4049. // Put this clean up in a block to allow exception handler to understand them
  4050. bc->Block(true);
  4051. asCVariableScope *vs = variables;
  4052. while( !vs->isContinueScope )
  4053. {
  4054. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  4055. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  4056. vs = vs->parent;
  4057. }
  4058. bc->Block(false);
  4059. bc->InstrINT(asBC_JMP, continueLabels[continueLabels.GetLength()-1]);
  4060. }
  4061. void asCCompiler::CompileExpressionStatement(asCScriptNode *enode, asCByteCode *bc)
  4062. {
  4063. if( enode->firstChild )
  4064. {
  4065. // Compile the expression
  4066. asCExprContext expr(engine);
  4067. CompileAssignment(enode->firstChild, &expr);
  4068. // Must not have unused ambiguous names
  4069. if( expr.IsClassMethod() || expr.IsGlobalFunc() )
  4070. Error(TXT_INVALID_EXPRESSION_AMBIGUOUS_NAME, enode);
  4071. // Must not have unused anonymous functions
  4072. if( expr.IsLambda() )
  4073. Error(TXT_INVALID_EXPRESSION_LAMBDA, enode);
  4074. // If we get here and there is still an unprocessed property
  4075. // accessor, then process it as a get access. Don't call if there is
  4076. // already a compile error, or we might report an error that is not valid
  4077. if( !hasCompileErrors )
  4078. if( ProcessPropertyGetAccessor(&expr, enode) < 0 )
  4079. return;
  4080. // Pop the value from the stack
  4081. if( !expr.type.dataType.IsPrimitive() )
  4082. expr.bc.Instr(asBC_PopPtr);
  4083. // Release temporary variables used by expression
  4084. ReleaseTemporaryVariable(expr.type, &expr.bc);
  4085. ProcessDeferredParams(&expr);
  4086. expr.bc.OptimizeLocally(tempVariableOffsets);
  4087. bc->AddCode(&expr.bc);
  4088. }
  4089. }
  4090. void asCCompiler::PrepareTemporaryVariable(asCScriptNode *node, asCExprContext *ctx, bool forceOnHeap)
  4091. {
  4092. // The input can be either an object or funcdef, either as handle or reference
  4093. asASSERT(ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef());
  4094. // If the object already is stored in temporary variable then nothing needs to be done
  4095. // Note, a type can be temporary without being a variable, in which case it is holding off
  4096. // on releasing a previously used object.
  4097. if( ctx->type.isTemporary && ctx->type.isVariable &&
  4098. !(forceOnHeap && !IsVariableOnHeap(ctx->type.stackOffset)) )
  4099. {
  4100. // If the temporary object is currently not a reference
  4101. // the expression needs to be reevaluated to a reference
  4102. if( !ctx->type.dataType.IsReference() )
  4103. {
  4104. ctx->bc.Instr(asBC_PopPtr);
  4105. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4106. ctx->type.dataType.MakeReference(IsVariableOnHeap(ctx->type.stackOffset));
  4107. }
  4108. return;
  4109. }
  4110. // Allocate temporary variable
  4111. asCDataType dt = ctx->type.dataType;
  4112. dt.MakeReference(false);
  4113. dt.MakeReadOnly(false);
  4114. int offset = AllocateVariable(dt, true, forceOnHeap);
  4115. // Objects stored on the stack are not considered references
  4116. dt.MakeReference(IsVariableOnHeap(offset));
  4117. asCExprValue lvalue;
  4118. lvalue.Set(dt);
  4119. lvalue.isExplicitHandle = ctx->type.isExplicitHandle;
  4120. bool isExplicitHandle = ctx->type.isExplicitHandle;
  4121. bool prevIsTemp = ctx->type.isTemporary;
  4122. int prevStackOffset = ctx->type.stackOffset;
  4123. CompileInitAsCopy(dt, offset, &ctx->bc, ctx, node, false);
  4124. // Release the previous temporary variable if it hasn't already been released
  4125. if( prevIsTemp && tempVariables.Exists(prevStackOffset) )
  4126. ReleaseTemporaryVariable(prevStackOffset, &ctx->bc);
  4127. // Push the reference to the temporary variable on the stack
  4128. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  4129. ctx->type.Set(dt);
  4130. ctx->type.isTemporary = true;
  4131. ctx->type.stackOffset = (short)offset;
  4132. ctx->type.isVariable = true;
  4133. ctx->type.isExplicitHandle = isExplicitHandle;
  4134. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  4135. }
  4136. void asCCompiler::CompileReturnStatement(asCScriptNode *rnode, asCByteCode *bc)
  4137. {
  4138. // Get return type and location
  4139. sVariable *v = variables->GetVariable("return");
  4140. // Basic validations
  4141. if( v->type.GetSizeOnStackDWords() > 0 && !rnode->firstChild )
  4142. {
  4143. Error(TXT_MUST_RETURN_VALUE, rnode);
  4144. return;
  4145. }
  4146. else if( v->type.GetSizeOnStackDWords() == 0 && rnode->firstChild )
  4147. {
  4148. Error(TXT_CANT_RETURN_VALUE, rnode);
  4149. return;
  4150. }
  4151. // Compile the expression
  4152. if( rnode->firstChild )
  4153. {
  4154. // Compile the expression
  4155. asCExprContext expr(engine);
  4156. int r = CompileAssignment(rnode->firstChild, &expr);
  4157. if( r < 0 ) return;
  4158. if( v->type.IsReference() )
  4159. {
  4160. // The expression that gives the reference must not use any of the
  4161. // variables that must be destroyed upon exit, because then it means
  4162. // reference will stay alive while the clean-up is done, which could
  4163. // potentially mean that the reference is invalidated by the clean-up.
  4164. //
  4165. // When the function is returning a reference, the clean-up of the
  4166. // variables must be done before the evaluation of the expression.
  4167. //
  4168. // A reference to a global variable, or a class member for class methods
  4169. // should be allowed to be returned.
  4170. if( !(expr.type.dataType.IsReference() ||
  4171. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle())) )
  4172. {
  4173. // Clean up the potential deferred parameters
  4174. ProcessDeferredParams(&expr);
  4175. Error(TXT_NOT_VALID_REFERENCE, rnode);
  4176. return;
  4177. }
  4178. // No references to local variables, temporary variables, or parameters
  4179. // are allowed to be returned, since they go out of scope when the function
  4180. // returns. Even reference parameters are disallowed, since it is not possible
  4181. // to know the scope of them. The exception is the 'this' pointer, which
  4182. // is treated by the compiler as a local variable, but isn't really so.
  4183. if( (expr.type.isVariable && !(expr.type.stackOffset == 0 && outFunc->objectType)) || expr.type.isTemporary )
  4184. {
  4185. // Clean up the potential deferred parameters
  4186. ProcessDeferredParams(&expr);
  4187. Error(TXT_CANNOT_RETURN_REF_TO_LOCAL, rnode);
  4188. return;
  4189. }
  4190. // The type must match exactly as we cannot convert
  4191. // the reference without loosing the original value
  4192. if( !(v->type.IsEqualExceptConst(expr.type.dataType) ||
  4193. ((expr.type.dataType.IsObject() || expr.type.dataType.IsFuncdef()) &&
  4194. !expr.type.dataType.IsObjectHandle() &&
  4195. v->type.IsEqualExceptRefAndConst(expr.type.dataType))) ||
  4196. (!v->type.IsReadOnly() && expr.type.dataType.IsReadOnly()) )
  4197. {
  4198. // Clean up the potential deferred parameters
  4199. ProcessDeferredParams(&expr);
  4200. asCString str;
  4201. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format(outFunc->nameSpace).AddressOf(), v->type.Format(outFunc->nameSpace).AddressOf());
  4202. Error(str, rnode);
  4203. return;
  4204. }
  4205. // The expression must not have any deferred expressions, because the evaluation
  4206. // of these cannot be done without keeping the reference which is not safe
  4207. if( expr.deferredParams.GetLength() )
  4208. {
  4209. // Clean up the potential deferred parameters
  4210. ProcessDeferredParams(&expr);
  4211. Error(TXT_REF_CANT_BE_RETURNED_DEFERRED_PARAM, rnode);
  4212. return;
  4213. }
  4214. // Make sure the expression isn't using any local variables that
  4215. // will need to be cleaned up before the function completes
  4216. asCArray<int> usedVars;
  4217. expr.bc.GetVarsUsed(usedVars);
  4218. for( asUINT n = 0; n < usedVars.GetLength(); n++ )
  4219. {
  4220. int var = GetVariableSlot(usedVars[n]);
  4221. if( var != -1 )
  4222. {
  4223. asCDataType dt = variableAllocations[var];
  4224. if( dt.IsObject() )
  4225. {
  4226. ProcessDeferredParams(&expr);
  4227. Error(TXT_REF_CANT_BE_RETURNED_LOCAL_VARS, rnode);
  4228. return;
  4229. }
  4230. }
  4231. }
  4232. // Can't return the reference if could point to a local variable
  4233. if( expr.type.isRefToLocal )
  4234. {
  4235. ProcessDeferredParams(&expr);
  4236. Error(TXT_REF_CANT_BE_TO_LOCAL_VAR, rnode);
  4237. return;
  4238. }
  4239. // All objects in the function must be cleaned up before the expression
  4240. // is evaluated, otherwise there is a possibility that the cleanup will
  4241. // invalidate the reference.
  4242. // Destroy the local variables before loading
  4243. // the reference into the register. This will
  4244. // be done before the expression is evaluated.
  4245. DestroyVariables(bc);
  4246. // For primitives the reference is already in the register,
  4247. // but for non-primitives the reference is on the stack so we
  4248. // need to load it into the register
  4249. if( !expr.type.dataType.IsPrimitive() )
  4250. {
  4251. if( !expr.type.dataType.IsObjectHandle() &&
  4252. expr.type.dataType.IsReference() )
  4253. expr.bc.Instr(asBC_RDSPtr);
  4254. expr.bc.Instr(asBC_PopRPtr);
  4255. }
  4256. // There are no temporaries to release so we're done
  4257. }
  4258. else // if( !v->type.IsReference() )
  4259. {
  4260. if( ProcessPropertyGetAccessor(&expr, rnode) < 0 )
  4261. return;
  4262. // Prepare the value for assignment
  4263. IsVariableInitialized(&expr.type, rnode->firstChild);
  4264. if( v->type.IsPrimitive() )
  4265. {
  4266. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  4267. // Implicitly convert the value to the return type
  4268. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  4269. // Verify that the conversion was successful
  4270. if( expr.type.dataType != v->type )
  4271. {
  4272. asCString str;
  4273. str.Format(TXT_NO_CONVERSION_s_TO_s, expr.type.dataType.Format(outFunc->nameSpace).AddressOf(), v->type.Format(outFunc->nameSpace).AddressOf());
  4274. Error(str, rnode);
  4275. return;
  4276. }
  4277. else
  4278. {
  4279. ConvertToVariable(&expr);
  4280. // Clean up the local variables and process deferred parameters
  4281. DestroyVariables(&expr.bc);
  4282. ProcessDeferredParams(&expr);
  4283. ReleaseTemporaryVariable(expr.type, &expr.bc);
  4284. // Load the variable in the register
  4285. if( v->type.GetSizeOnStackDWords() == 1 )
  4286. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  4287. else
  4288. expr.bc.InstrSHORT(asBC_CpyVtoR8, expr.type.stackOffset);
  4289. }
  4290. }
  4291. else if( v->type.IsObject() || v->type.IsFuncdef() )
  4292. {
  4293. // Value types are returned on the stack, in a location
  4294. // that has been reserved by the calling function.
  4295. if( outFunc->DoesReturnOnStack() )
  4296. {
  4297. // TODO: runtime optimize: If the return type has a constructor that takes the type of the expression,
  4298. // it should be called directly instead of first converting the expression and
  4299. // then copy the value.
  4300. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  4301. {
  4302. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  4303. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  4304. {
  4305. asCString str;
  4306. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format(outFunc->nameSpace).AddressOf(), v->type.Format(outFunc->nameSpace).AddressOf());
  4307. Error(str, rnode->firstChild);
  4308. return;
  4309. }
  4310. }
  4311. int offset = outFunc->objectType ? -AS_PTR_SIZE : 0;
  4312. CompileInitAsCopy(v->type, offset, &expr.bc, &expr, rnode->firstChild, true);
  4313. // Clean up the local variables and process deferred parameters
  4314. DestroyVariables(&expr.bc);
  4315. ProcessDeferredParams(&expr);
  4316. }
  4317. else
  4318. {
  4319. asASSERT( (v->type.GetTypeInfo()->flags & asOBJ_REF) || v->type.IsFuncdef() );
  4320. // Prepare the expression to be loaded into the object
  4321. // register. This will place the reference in local variable
  4322. PrepareArgument(&v->type, &expr, rnode->firstChild, false, 0);
  4323. // Pop the reference to the temporary variable
  4324. expr.bc.Instr(asBC_PopPtr);
  4325. // Clean up the local variables and process deferred parameters
  4326. DestroyVariables(&expr.bc);
  4327. ProcessDeferredParams(&expr);
  4328. // Load the object pointer into the object register
  4329. // LOADOBJ also clears the address in the variable
  4330. expr.bc.InstrSHORT(asBC_LOADOBJ, expr.type.stackOffset);
  4331. // LOADOBJ cleared the address in the variable so the object will not be freed
  4332. // here, but the temporary variable must still be freed so the slot can be reused
  4333. // By releasing without the bytecode we do just that.
  4334. ReleaseTemporaryVariable(expr.type, 0);
  4335. }
  4336. }
  4337. }
  4338. expr.bc.OptimizeLocally(tempVariableOffsets);
  4339. bc->AddCode(&expr.bc);
  4340. }
  4341. else
  4342. {
  4343. // For functions that don't return anything
  4344. // we just detroy the local variables
  4345. DestroyVariables(bc);
  4346. }
  4347. // Jump to the end of the function
  4348. bc->InstrINT(asBC_JMP, 0);
  4349. }
  4350. void asCCompiler::DestroyVariables(asCByteCode *bc)
  4351. {
  4352. // Call destructor on all variables except for the function parameters
  4353. // Put the clean-up in a block to allow exception handler to understand this
  4354. bc->Block(true);
  4355. asCVariableScope *vs = variables;
  4356. while( vs )
  4357. {
  4358. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  4359. if( vs->variables[n]->stackOffset > 0 )
  4360. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  4361. vs = vs->parent;
  4362. }
  4363. bc->Block(false);
  4364. }
  4365. void asCCompiler::AddVariableScope(bool isBreakScope, bool isContinueScope)
  4366. {
  4367. variables = asNEW(asCVariableScope)(variables);
  4368. if( variables == 0 )
  4369. {
  4370. // Out of memory
  4371. return;
  4372. }
  4373. variables->isBreakScope = isBreakScope;
  4374. variables->isContinueScope = isContinueScope;
  4375. }
  4376. void asCCompiler::RemoveVariableScope()
  4377. {
  4378. if( variables )
  4379. {
  4380. asCVariableScope *var = variables;
  4381. variables = variables->parent;
  4382. asDELETE(var,asCVariableScope);
  4383. }
  4384. }
  4385. void asCCompiler::Error(const asCString &msg, asCScriptNode *node)
  4386. {
  4387. asCString str;
  4388. int r = 0, c = 0;
  4389. asASSERT( node );
  4390. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4391. builder->WriteError(script->name, msg, r, c);
  4392. hasCompileErrors = true;
  4393. }
  4394. void asCCompiler::Warning(const asCString &msg, asCScriptNode *node)
  4395. {
  4396. asCString str;
  4397. int r = 0, c = 0;
  4398. asASSERT( node );
  4399. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4400. builder->WriteWarning(script->name, msg, r, c);
  4401. }
  4402. void asCCompiler::Information(const asCString &msg, asCScriptNode *node)
  4403. {
  4404. asCString str;
  4405. int r = 0, c = 0;
  4406. asASSERT( node );
  4407. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4408. builder->WriteInfo(script->name, msg, r, c, false);
  4409. }
  4410. void asCCompiler::PrintMatchingFuncs(asCArray<int> &funcs, asCScriptNode *node, asCObjectType *inType)
  4411. {
  4412. int r = 0, c = 0;
  4413. asASSERT( node );
  4414. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4415. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  4416. {
  4417. asCScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  4418. if( inType && func->funcType == asFUNC_VIRTUAL )
  4419. func = inType->virtualFunctionTable[func->vfTableIdx];
  4420. builder->WriteInfo(script->name, func->GetDeclaration(true, false, true), r, c, false);
  4421. if (func->objectType && (func->objectType->flags & asOBJ_TEMPLATE))
  4422. {
  4423. // Check for funcdefs in the arguments that may have been generated by the template instance, so these can be shown to user
  4424. for (unsigned int p = 0; p < func->GetParamCount(); p++)
  4425. {
  4426. int typeId = 0;
  4427. func->GetParam(p, &typeId);
  4428. asITypeInfo *ti = engine->GetTypeInfoById(typeId);
  4429. if (ti && (ti->GetFlags() & asOBJ_FUNCDEF))
  4430. {
  4431. asCString msg;
  4432. msg.Format(TXT_WHERE_s_IS_s, ti->GetName(), ti->GetFuncdefSignature()->GetDeclaration());
  4433. builder->WriteInfo(script->name, msg.AddressOf(), r, c, false);
  4434. }
  4435. }
  4436. }
  4437. }
  4438. }
  4439. int asCCompiler::AllocateVariableNotIn(const asCDataType &type, bool isTemporary, bool forceOnHeap, asCExprContext *ctx)
  4440. {
  4441. int l = int(reservedVariables.GetLength());
  4442. ctx->bc.GetVarsUsed(reservedVariables);
  4443. int var = AllocateVariable(type, isTemporary, forceOnHeap);
  4444. reservedVariables.SetLength(l);
  4445. return var;
  4446. }
  4447. int asCCompiler::AllocateVariable(const asCDataType &type, bool isTemporary, bool forceOnHeap, bool asReference)
  4448. {
  4449. asCDataType t(type);
  4450. t.MakeReference(asReference);
  4451. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 1 )
  4452. t.SetTokenType(ttInt);
  4453. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 2 )
  4454. t.SetTokenType(ttDouble);
  4455. // Only null handles have the token type unrecognized token
  4456. asASSERT( t.IsObjectHandle() || t.GetTokenType() != ttUnrecognizedToken );
  4457. bool isOnHeap = true;
  4458. if( t.IsPrimitive() ||
  4459. (t.GetTypeInfo() && (t.GetTypeInfo()->GetFlags() & asOBJ_VALUE) && !forceOnHeap && !asReference) )
  4460. {
  4461. // Primitives and value types (unless overridden) are allocated on the stack
  4462. isOnHeap = false;
  4463. }
  4464. // Find a free location with the same type
  4465. for( asUINT n = 0; n < freeVariables.GetLength(); n++ )
  4466. {
  4467. int slot = freeVariables[n];
  4468. if( variableAllocations[slot].IsEqualExceptConst(t) &&
  4469. variableIsTemporary[slot] == isTemporary &&
  4470. variableIsOnHeap[slot] == isOnHeap )
  4471. {
  4472. // We can't return by slot, must count variable sizes
  4473. int offset = GetVariableOffset(slot);
  4474. // Verify that it is not in the list of reserved variables
  4475. bool isUsed = false;
  4476. if( reservedVariables.GetLength() )
  4477. isUsed = reservedVariables.Exists(offset);
  4478. if( !isUsed )
  4479. {
  4480. if( n != freeVariables.GetLength() - 1 )
  4481. freeVariables[n] = freeVariables.PopLast();
  4482. else
  4483. freeVariables.PopLast();
  4484. if( isTemporary )
  4485. tempVariables.PushLast(offset);
  4486. return offset;
  4487. }
  4488. }
  4489. }
  4490. variableAllocations.PushLast(t);
  4491. variableIsTemporary.PushLast(isTemporary);
  4492. variableIsOnHeap.PushLast(isOnHeap);
  4493. int offset = GetVariableOffset((int)variableAllocations.GetLength()-1);
  4494. if( isTemporary )
  4495. {
  4496. // Add offset to the currently allocated temporary variables
  4497. tempVariables.PushLast(offset);
  4498. // Add offset to all known offsets to temporary variables, whether allocated or not
  4499. tempVariableOffsets.PushLast(offset);
  4500. }
  4501. return offset;
  4502. }
  4503. int asCCompiler::GetVariableOffset(int varIndex)
  4504. {
  4505. // Return offset to the last dword on the stack
  4506. // Start at 1 as offset 0 is reserved for the this pointer (or first argument for global functions)
  4507. int varOffset = 1;
  4508. // Skip lower variables
  4509. for( int n = 0; n < varIndex; n++ )
  4510. {
  4511. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  4512. varOffset += variableAllocations[n].GetSizeInMemoryDWords();
  4513. else
  4514. varOffset += variableAllocations[n].GetSizeOnStackDWords();
  4515. }
  4516. if( varIndex < (int)variableAllocations.GetLength() )
  4517. {
  4518. // For variables larger than 1 dword the returned offset should be to the last dword
  4519. int size;
  4520. if( !variableIsOnHeap[varIndex] && variableAllocations[varIndex].IsObject() )
  4521. size = variableAllocations[varIndex].GetSizeInMemoryDWords();
  4522. else
  4523. size = variableAllocations[varIndex].GetSizeOnStackDWords();
  4524. if( size > 1 )
  4525. varOffset += size-1;
  4526. }
  4527. return varOffset;
  4528. }
  4529. int asCCompiler::GetVariableSlot(int offset)
  4530. {
  4531. int varOffset = 1;
  4532. for( asUINT n = 0; n < variableAllocations.GetLength(); n++ )
  4533. {
  4534. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  4535. varOffset += -1 + variableAllocations[n].GetSizeInMemoryDWords();
  4536. else
  4537. varOffset += -1 + variableAllocations[n].GetSizeOnStackDWords();
  4538. if( varOffset == offset )
  4539. return n;
  4540. varOffset++;
  4541. }
  4542. return -1;
  4543. }
  4544. bool asCCompiler::IsVariableOnHeap(int offset)
  4545. {
  4546. int varSlot = GetVariableSlot(offset);
  4547. if( varSlot < 0 )
  4548. {
  4549. // This happens for function arguments that are considered as on the heap
  4550. return true;
  4551. }
  4552. return variableIsOnHeap[varSlot];
  4553. }
  4554. void asCCompiler::DeallocateVariable(int offset)
  4555. {
  4556. // Remove temporary variable
  4557. int n;
  4558. for( n = 0; n < (int)tempVariables.GetLength(); n++ )
  4559. {
  4560. if( offset == tempVariables[n] )
  4561. {
  4562. if( n == (int)tempVariables.GetLength()-1 )
  4563. tempVariables.PopLast();
  4564. else
  4565. tempVariables[n] = tempVariables.PopLast();
  4566. break;
  4567. }
  4568. }
  4569. // Mark the variable slot available for new allocations
  4570. n = GetVariableSlot(offset);
  4571. if( n != -1 )
  4572. {
  4573. freeVariables.PushLast(n);
  4574. return;
  4575. }
  4576. // We might get here if the variable was implicitly declared
  4577. // because it was used before a formal declaration, in this case
  4578. // the offset is 0x7FFF
  4579. asASSERT(offset == 0x7FFF);
  4580. }
  4581. void asCCompiler::ReleaseTemporaryVariable(asCExprValue &t, asCByteCode *bc)
  4582. {
  4583. if( t.isTemporary )
  4584. {
  4585. ReleaseTemporaryVariable(t.stackOffset, bc);
  4586. t.isTemporary = false;
  4587. }
  4588. }
  4589. void asCCompiler::ReleaseTemporaryVariable(int offset, asCByteCode *bc)
  4590. {
  4591. asASSERT( tempVariables.Exists(offset) );
  4592. if( bc )
  4593. {
  4594. // We need to call the destructor on the true variable type
  4595. int n = GetVariableSlot(offset);
  4596. asASSERT( n >= 0 );
  4597. if( n >= 0 )
  4598. {
  4599. asCDataType dt = variableAllocations[n];
  4600. bool isOnHeap = variableIsOnHeap[n];
  4601. // Call destructor
  4602. CallDestructor(dt, offset, isOnHeap, bc);
  4603. }
  4604. }
  4605. DeallocateVariable(offset);
  4606. }
  4607. void asCCompiler::Dereference(asCExprContext *ctx, bool generateCode)
  4608. {
  4609. if( ctx->type.dataType.IsReference() )
  4610. {
  4611. if( ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef() )
  4612. {
  4613. ctx->type.dataType.MakeReference(false);
  4614. if( generateCode )
  4615. ctx->bc.Instr(asBC_RDSPtr);
  4616. }
  4617. else
  4618. {
  4619. // This should never happen as primitives are treated differently
  4620. asASSERT(false);
  4621. }
  4622. }
  4623. }
  4624. bool asCCompiler::IsVariableInitialized(asCExprValue *type, asCScriptNode *node)
  4625. {
  4626. // No need to check if there is no variable scope
  4627. if( variables == 0 ) return true;
  4628. // Temporary variables are assumed to be initialized
  4629. if( type->isTemporary ) return true;
  4630. // Verify that it is a variable
  4631. if( !type->isVariable ) return true;
  4632. // Find the variable
  4633. sVariable *v = variables->GetVariableByOffset(type->stackOffset);
  4634. // The variable isn't found if it is a constant, in which case it is guaranteed to be initialized
  4635. if( v == 0 ) return true;
  4636. if( v->isInitialized ) return true;
  4637. // Complex types don't need this test
  4638. if( v->type.IsObject() || v->type.IsFuncdef() ) return true;
  4639. // Mark as initialized so that the user will not be bothered again
  4640. v->isInitialized = true;
  4641. // Write warning
  4642. asCString str;
  4643. str.Format(TXT_s_NOT_INITIALIZED, (const char *)v->name.AddressOf());
  4644. Warning(str, node);
  4645. return false;
  4646. }
  4647. void asCCompiler::PrepareOperand(asCExprContext *ctx, asCScriptNode *node)
  4648. {
  4649. // Check if the variable is initialized (if it indeed is a variable)
  4650. IsVariableInitialized(&ctx->type, node);
  4651. asCDataType to = ctx->type.dataType;
  4652. to.MakeReference(false);
  4653. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  4654. ProcessDeferredParams(ctx);
  4655. }
  4656. void asCCompiler::PrepareForAssignment(asCDataType *lvalue, asCExprContext *rctx, asCScriptNode *node, bool toTemporary, asCExprContext *lvalueExpr)
  4657. {
  4658. // Reserve the temporary variables used in the lvalue expression so they won't end up being used by the rvalue too
  4659. int l = int(reservedVariables.GetLength());
  4660. if( lvalueExpr ) lvalueExpr->bc.GetVarsUsed(reservedVariables);
  4661. if( ProcessPropertyGetAccessor(rctx, node) < 0 )
  4662. return;
  4663. // Make sure the rvalue is initialized if it is a variable
  4664. IsVariableInitialized(&rctx->type, node);
  4665. if( lvalue->IsPrimitive() )
  4666. {
  4667. if( rctx->type.dataType.IsPrimitive() )
  4668. {
  4669. if( rctx->type.dataType.IsReference() )
  4670. {
  4671. // Cannot do implicit conversion of references so we first convert the reference to a variable
  4672. ConvertToVariableNotIn(rctx, lvalueExpr);
  4673. }
  4674. }
  4675. // Implicitly convert the value to the right type
  4676. ImplicitConversion(rctx, *lvalue, node, asIC_IMPLICIT_CONV);
  4677. // Check data type
  4678. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  4679. {
  4680. asCString str;
  4681. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lvalue->Format(outFunc->nameSpace).AddressOf());
  4682. Error(str, node);
  4683. rctx->type.SetDummy();
  4684. }
  4685. // Make sure the rvalue is a variable
  4686. if( !rctx->type.isVariable )
  4687. ConvertToVariableNotIn(rctx, lvalueExpr);
  4688. }
  4689. else
  4690. {
  4691. asCDataType to = *lvalue;
  4692. to.MakeReference(false);
  4693. // TODO: ImplicitConversion should know to do this by itself
  4694. // First convert to a handle which will do a reference cast
  4695. if( !lvalue->IsObjectHandle() &&
  4696. (lvalue->GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  4697. to.MakeHandle(true);
  4698. // Don't allow the implicit conversion to create an object
  4699. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  4700. if( !lvalue->IsObjectHandle() &&
  4701. (lvalue->GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  4702. {
  4703. // Then convert to a reference, which will validate the handle
  4704. to.MakeHandle(false);
  4705. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  4706. }
  4707. // Check data type
  4708. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  4709. {
  4710. asCString str;
  4711. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lvalue->Format(outFunc->nameSpace).AddressOf());
  4712. Error(str, node);
  4713. }
  4714. else
  4715. {
  4716. // If the assignment will be made with the copy behaviour then the rvalue must not be a reference
  4717. asASSERT(!lvalue->IsObject() || !rctx->type.dataType.IsReference());
  4718. }
  4719. }
  4720. // Unreserve variables
  4721. reservedVariables.SetLength(l);
  4722. }
  4723. bool asCCompiler::IsLValue(asCExprValue &type)
  4724. {
  4725. if( !type.isLValue ) return false;
  4726. if( type.dataType.IsReadOnly() ) return false;
  4727. if( !type.dataType.IsObject() && !type.isVariable && !type.dataType.IsReference() ) return false;
  4728. return true;
  4729. }
  4730. int asCCompiler::PerformAssignment(asCExprValue *lvalue, asCExprValue *rvalue, asCByteCode *bc, asCScriptNode *node)
  4731. {
  4732. if( lvalue->dataType.IsReadOnly() )
  4733. {
  4734. Error(TXT_REF_IS_READ_ONLY, node);
  4735. return -1;
  4736. }
  4737. if( lvalue->dataType.IsPrimitive() )
  4738. {
  4739. if( lvalue->isVariable )
  4740. {
  4741. // Copy the value between the variables directly
  4742. if( lvalue->dataType.GetSizeInMemoryDWords() == 1 )
  4743. bc->InstrW_W(asBC_CpyVtoV4, lvalue->stackOffset, rvalue->stackOffset);
  4744. else
  4745. bc->InstrW_W(asBC_CpyVtoV8, lvalue->stackOffset, rvalue->stackOffset);
  4746. // Mark variable as initialized
  4747. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  4748. if( v ) v->isInitialized = true;
  4749. }
  4750. else if( lvalue->dataType.IsReference() )
  4751. {
  4752. // Copy the value of the variable to the reference in the register
  4753. int s = lvalue->dataType.GetSizeInMemoryBytes();
  4754. if( s == 1 )
  4755. bc->InstrSHORT(asBC_WRTV1, rvalue->stackOffset);
  4756. else if( s == 2 )
  4757. bc->InstrSHORT(asBC_WRTV2, rvalue->stackOffset);
  4758. else if( s == 4 )
  4759. bc->InstrSHORT(asBC_WRTV4, rvalue->stackOffset);
  4760. else if( s == 8 )
  4761. bc->InstrSHORT(asBC_WRTV8, rvalue->stackOffset);
  4762. }
  4763. else
  4764. {
  4765. Error(TXT_NOT_VALID_LVALUE, node);
  4766. return -1;
  4767. }
  4768. }
  4769. else if( !lvalue->isExplicitHandle )
  4770. {
  4771. asCExprContext ctx(engine);
  4772. ctx.type = *lvalue;
  4773. Dereference(&ctx, true);
  4774. *lvalue = ctx.type;
  4775. bc->AddCode(&ctx.bc);
  4776. asSTypeBehaviour *beh = lvalue->dataType.GetBehaviour();
  4777. if( beh && beh->copy && beh->copy != engine->scriptTypeBehaviours.beh.copy )
  4778. {
  4779. asCExprContext res(engine);
  4780. PerformFunctionCall(beh->copy, &res, false, 0, CastToObjectType(lvalue->dataType.GetTypeInfo()));
  4781. bc->AddCode(&res.bc);
  4782. *lvalue = res.type;
  4783. }
  4784. else if( beh && beh->copy == engine->scriptTypeBehaviours.beh.copy )
  4785. {
  4786. // Call the default copy operator for script classes
  4787. // This is done differently because the default copy operator
  4788. // is registered as returning int&, but in reality it returns
  4789. // a reference to the object.
  4790. // TODO: Avoid this special case by implementing a copystub for
  4791. // script classes that uses the default copy operator
  4792. bc->Call(asBC_CALLSYS, beh->copy, 2*AS_PTR_SIZE);
  4793. bc->Instr(asBC_PshRPtr);
  4794. }
  4795. else
  4796. {
  4797. // Default copy operator
  4798. if( lvalue->dataType.GetSizeInMemoryDWords() == 0 ||
  4799. !(lvalue->dataType.GetTypeInfo()->flags & asOBJ_POD) )
  4800. {
  4801. asCString msg;
  4802. msg.Format(TXT_NO_DEFAULT_COPY_OP_FOR_s, lvalue->dataType.GetTypeInfo()->name.AddressOf());
  4803. Error(msg, node);
  4804. return -1;
  4805. }
  4806. // Copy larger data types from a reference
  4807. // TODO: runtime optimize: COPY should pop both arguments and store the reference in the register.
  4808. bc->InstrSHORT_DW(asBC_COPY, (short)lvalue->dataType.GetSizeInMemoryDWords(), engine->GetTypeIdFromDataType(lvalue->dataType));
  4809. }
  4810. }
  4811. else
  4812. {
  4813. // TODO: The object handle can be stored in a variable as well
  4814. if( !lvalue->dataType.IsReference() )
  4815. {
  4816. Error(TXT_NOT_VALID_REFERENCE, node);
  4817. return -1;
  4818. }
  4819. if( lvalue->dataType.IsFuncdef() )
  4820. bc->InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  4821. else
  4822. bc->InstrPTR(asBC_REFCPY, lvalue->dataType.GetTypeInfo());
  4823. // Mark variable as initialized
  4824. if( variables )
  4825. {
  4826. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  4827. if( v ) v->isInitialized = true;
  4828. }
  4829. }
  4830. return 0;
  4831. }
  4832. bool asCCompiler::CompileRefCast(asCExprContext *ctx, const asCDataType &to, bool isExplicit, asCScriptNode *node, bool generateCode)
  4833. {
  4834. bool conversionDone = false;
  4835. asCArray<int> ops;
  4836. // A ref cast must not remove the constness
  4837. bool isConst = ctx->type.dataType.IsObjectConst();
  4838. // Find a suitable opCast or opImplCast method
  4839. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  4840. for( asUINT n = 0; ot && n < ot->methods.GetLength(); n++ )
  4841. {
  4842. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  4843. if( (isExplicit && func->name == "opCast") ||
  4844. func->name == "opImplCast" )
  4845. {
  4846. // Is the operator for the output type?
  4847. if( func->returnType.GetTypeInfo() != to.GetTypeInfo() )
  4848. continue;
  4849. // Can't call a non-const function on a const object
  4850. if( isConst && !func->IsReadOnly() )
  4851. continue;
  4852. ops.PushLast(func->id);
  4853. }
  4854. }
  4855. // Filter the list by constness to remove const methods if there are matching non-const methods
  4856. FilterConst(ops, !isConst);
  4857. // If there is multiple matches, then pick the most appropriate one
  4858. if (ops.GetLength() > 1)
  4859. {
  4860. // This should only happen if an explicit cast is compiled
  4861. // and the type has both the opCast and opImplCast
  4862. asASSERT(isExplicit);
  4863. asASSERT(ops.GetLength() == 2);
  4864. for (asUINT n = 0; n < ops.GetLength(); n++)
  4865. {
  4866. asCScriptFunction *func = engine->scriptFunctions[ops[n]];
  4867. if (func->name == "opImplCast")
  4868. {
  4869. ops.RemoveIndex(n);
  4870. n--;
  4871. }
  4872. }
  4873. }
  4874. // Should only have one behaviour for each output type
  4875. if( ops.GetLength() == 1 )
  4876. {
  4877. conversionDone = true;
  4878. if( generateCode )
  4879. {
  4880. // TODO: runtime optimize: Instead of producing bytecode for checking if the handle is
  4881. // null, we can create a special CALLSYS instruction that checks
  4882. // if the object pointer is null and if so sets the object register
  4883. // to null directly without executing the function.
  4884. //
  4885. // Alternatively I could force the ref cast behaviours be global
  4886. // functions with 1 parameter, even though they should still be
  4887. // registered with RegisterObjectBehaviour()
  4888. if( (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_NOHANDLE))
  4889. {
  4890. // Add code to avoid calling the cast behaviour if the handle is already null,
  4891. // because that will raise a null pointer exception due to the cast behaviour
  4892. // being a class method, and the this pointer cannot be null.
  4893. if (!ctx->type.isVariable)
  4894. {
  4895. Dereference(ctx, true);
  4896. ConvertToVariable(ctx);
  4897. }
  4898. // The reference on the stack will not be used
  4899. ctx->bc.Instr(asBC_PopPtr);
  4900. // TODO: runtime optimize: should have immediate comparison for null pointer
  4901. int offset = AllocateVariable(asCDataType::CreateNullHandle(), true);
  4902. // TODO: runtime optimize: ClrVPtr is not necessary, because the VM should initialize the variable to null anyway (it is currently not done for null pointers though)
  4903. ctx->bc.InstrSHORT(asBC_ClrVPtr, (asWORD)offset);
  4904. ctx->bc.InstrW_W(asBC_CmpPtr, ctx->type.stackOffset, offset);
  4905. DeallocateVariable(offset);
  4906. int afterLabel = nextLabel++;
  4907. ctx->bc.InstrDWORD(asBC_JZ, afterLabel);
  4908. // Call the cast operator
  4909. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4910. ctx->bc.Instr(asBC_RDSPtr);
  4911. ctx->type.dataType.MakeReference(false);
  4912. asCArray<asCExprContext *> args;
  4913. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  4914. ctx->bc.Instr(asBC_PopPtr);
  4915. int endLabel = nextLabel++;
  4916. ctx->bc.InstrINT(asBC_JMP, endLabel);
  4917. ctx->bc.Label((short)afterLabel);
  4918. // Make a NULL pointer
  4919. ctx->bc.InstrSHORT(asBC_ClrVPtr, ctx->type.stackOffset);
  4920. ctx->bc.Label((short)endLabel);
  4921. // Push the reference to the handle on the stack
  4922. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4923. }
  4924. else
  4925. {
  4926. // Value types cannot be null, so there is no need to check for this.
  4927. // Likewise for reference types that are registered with asOBJ_NOHANDLE
  4928. // as those are only expected as registered global properties that cannot
  4929. // be modified anyway.
  4930. // Call the cast operator
  4931. asCArray<asCExprContext *> args;
  4932. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  4933. }
  4934. }
  4935. else
  4936. {
  4937. asCScriptFunction *func = engine->scriptFunctions[ops[0]];
  4938. ctx->type.Set(func->returnType);
  4939. }
  4940. }
  4941. else if( ops.GetLength() == 0 && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) && to.IsObjectHandle() )
  4942. {
  4943. // Check for the generic ref cast method: void opCast(?&out)
  4944. // This option only works if the expected type is a handle
  4945. for( asUINT n = 0; ot && n < ot->methods.GetLength(); n++ )
  4946. {
  4947. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  4948. if( (isExplicit && func->name == "opCast") ||
  4949. func->name == "opImplCast" )
  4950. {
  4951. // Does the operator take the ?&out parameter?
  4952. if( func->returnType.GetTokenType() != ttVoid ||
  4953. func->parameterTypes.GetLength() != 1 ||
  4954. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  4955. func->inOutFlags[0] != asTM_OUTREF )
  4956. continue;
  4957. ops.PushLast(func->id);
  4958. }
  4959. }
  4960. // Filter the list by constness to remove const methods if there are matching non-const methods
  4961. FilterConst(ops, !isConst);
  4962. // If there is multiple matches, then pick the most appropriate one
  4963. if (ops.GetLength() > 1)
  4964. {
  4965. // This should only happen if an explicit cast is compiled
  4966. // and the type has both the opCast and opImplCast
  4967. asASSERT(isExplicit);
  4968. asASSERT(ops.GetLength() == 2);
  4969. for (asUINT n = 0; n < ops.GetLength(); n++)
  4970. {
  4971. asCScriptFunction *func = engine->scriptFunctions[ops[n]];
  4972. if (func->name == "opImplCast")
  4973. {
  4974. ops.RemoveIndex(n);
  4975. n--;
  4976. }
  4977. }
  4978. }
  4979. if( ops.GetLength() == 1 )
  4980. {
  4981. conversionDone = true;
  4982. if( generateCode )
  4983. {
  4984. int afterLabel = 0;
  4985. bool doNullCheck = false;
  4986. bool releaseTempVariable = false;
  4987. asCExprContext tmp(engine);
  4988. if ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_NOHANDLE))
  4989. {
  4990. tmp.bc.AddCode(&ctx->bc);
  4991. tmp.Merge(ctx);
  4992. // Add code to avoid calling the cast behaviour if the handle is already null,
  4993. // because that will raise a null pointer exception due to the cast behaviour
  4994. // being a class method, and the this pointer cannot be null.
  4995. doNullCheck = true;
  4996. if (!ctx->type.isVariable)
  4997. {
  4998. Dereference(&tmp, true);
  4999. ConvertToVariable(&tmp);
  5000. releaseTempVariable = true;
  5001. }
  5002. // The reference on the stack will not be used
  5003. tmp.bc.Instr(asBC_PopPtr);
  5004. // TODO: runtime optimize: should have immediate comparison for null pointer
  5005. int offset = AllocateVariable(asCDataType::CreateNullHandle(), true);
  5006. // TODO: runtime optimize: ClrVPtr is not necessary, because the VM should initialize the variable to null anyway (it is currently not done for null pointers though)
  5007. tmp.bc.InstrSHORT(asBC_ClrVPtr, (asWORD)offset);
  5008. tmp.bc.InstrW_W(asBC_CmpPtr, tmp.type.stackOffset, offset);
  5009. DeallocateVariable(offset);
  5010. afterLabel = nextLabel++;
  5011. tmp.bc.InstrDWORD(asBC_JZ, afterLabel);
  5012. // Place the object pointer on the stack
  5013. ctx->bc.InstrSHORT(asBC_PSF, (short)tmp.type.stackOffset);
  5014. }
  5015. // Allocate a temporary variable of the requested handle type
  5016. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  5017. // Pass the reference of that variable to the function as output parameter
  5018. asCDataType toRef(to);
  5019. toRef.MakeReference(true);
  5020. asCArray<asCExprContext *> args;
  5021. asCExprContext arg(engine);
  5022. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  5023. // Don't mark the variable as temporary, so it won't be freed too early
  5024. arg.type.SetVariable(toRef, stackOffset, false);
  5025. arg.type.isLValue = true;
  5026. arg.type.isExplicitHandle = true;
  5027. args.PushLast(&arg);
  5028. // Call the behaviour method
  5029. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  5030. if (doNullCheck)
  5031. {
  5032. // Add the call after the null check
  5033. tmp.bc.AddCode(&ctx->bc);
  5034. ctx->bc.AddCode(&tmp.bc);
  5035. int endLabel = nextLabel++;
  5036. ctx->bc.InstrINT(asBC_JMP, endLabel);
  5037. ctx->bc.Label((short)afterLabel);
  5038. // Make a NULL pointer
  5039. ctx->bc.InstrSHORT(asBC_ClrVPtr, (short)stackOffset);
  5040. ctx->bc.Label((short)endLabel);
  5041. }
  5042. // If a temporary variable was allocated in the tmp to convert
  5043. // the input expression to a variable, it must be released here
  5044. if (releaseTempVariable && tmp.type.isTemporary)
  5045. ReleaseTemporaryVariable(tmp.type.stackOffset, &ctx->bc);
  5046. // Use the reference to the variable as the result of the expression
  5047. // Now we can mark the variable as temporary
  5048. ctx->type.SetVariable(toRef, stackOffset, true);
  5049. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  5050. }
  5051. else
  5052. {
  5053. // All casts are legal
  5054. ctx->type.Set(to);
  5055. }
  5056. }
  5057. }
  5058. // If the script object didn't implement a matching opCast or opImplCast
  5059. // then check if the desired type is part of the hierarchy
  5060. if( !conversionDone && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  5061. {
  5062. // We need it to be a reference
  5063. if( !ctx->type.dataType.IsReference() )
  5064. {
  5065. asCDataType toRef = ctx->type.dataType;
  5066. toRef.MakeReference(true);
  5067. ImplicitConversion(ctx, toRef, 0, isExplicit ? asIC_EXPLICIT_REF_CAST : asIC_IMPLICIT_CONV, generateCode);
  5068. }
  5069. if( isExplicit )
  5070. {
  5071. // Allow dynamic cast between object handles (only for script objects).
  5072. // At run time this may result in a null handle,
  5073. // which when used will throw an exception
  5074. conversionDone = true;
  5075. if( generateCode )
  5076. {
  5077. ctx->bc.InstrDWORD(asBC_Cast, engine->GetTypeIdFromDataType(to));
  5078. // Allocate a temporary variable for the returned object
  5079. int returnOffset = AllocateVariable(to, true);
  5080. // Move the pointer from the object register to the temporary variable
  5081. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  5082. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  5083. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5084. ctx->type.SetVariable(to, returnOffset, true);
  5085. ctx->type.dataType.MakeReference(true);
  5086. }
  5087. else
  5088. {
  5089. ctx->type.dataType = to;
  5090. ctx->type.dataType.MakeReference(true);
  5091. }
  5092. }
  5093. else
  5094. {
  5095. if( CastToObjectType(ctx->type.dataType.GetTypeInfo())->DerivesFrom(to.GetTypeInfo()) )
  5096. {
  5097. conversionDone = true;
  5098. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5099. }
  5100. }
  5101. // A ref cast must not remove the constness
  5102. if( isConst )
  5103. ctx->type.dataType.MakeHandleToConst(true);
  5104. }
  5105. return conversionDone;
  5106. }
  5107. asUINT asCCompiler::ImplicitConvPrimitiveToPrimitive(asCExprContext *ctx, const asCDataType &toOrig, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5108. {
  5109. asCDataType to = toOrig;
  5110. to.MakeReference(false);
  5111. asASSERT( !ctx->type.dataType.IsReference() );
  5112. // Maybe no conversion is needed
  5113. if( to.IsEqualExceptConst(ctx->type.dataType) )
  5114. {
  5115. // A primitive is const or not
  5116. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5117. return asCC_NO_CONV;
  5118. }
  5119. // Is the conversion an ambiguous enum value?
  5120. if( ctx->enumValue != "" )
  5121. {
  5122. if( to.IsEnumType() )
  5123. {
  5124. // Attempt to resolve an ambiguous enum value
  5125. asCDataType out;
  5126. asDWORD value;
  5127. if( builder->GetEnumValueFromType(CastToEnumType(to.GetTypeInfo()), ctx->enumValue.AddressOf(), out, value) )
  5128. {
  5129. ctx->type.SetConstantDW(out, value);
  5130. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5131. // Reset the enum value since we no longer need it
  5132. ctx->enumValue = "";
  5133. // It wasn't really a conversion. The compiler just resolved the ambiguity (or not)
  5134. return asCC_NO_CONV;
  5135. }
  5136. }
  5137. // The enum value is ambiguous
  5138. if( node && generateCode )
  5139. Error(TXT_FOUND_MULTIPLE_ENUM_VALUES, node);
  5140. // Set a dummy to allow the compiler to try to continue the conversion
  5141. ctx->type.SetDummy();
  5142. }
  5143. // Determine the cost of this conversion
  5144. asUINT cost = asCC_NO_CONV;
  5145. if( (to.IsIntegerType() || to.IsUnsignedType()) && (ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  5146. cost = asCC_INT_FLOAT_CONV;
  5147. else if ((to.IsFloatType() || to.IsDoubleType()) && (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType()))
  5148. cost = asCC_INT_FLOAT_CONV;
  5149. else if (ctx->type.dataType.IsEnumType() && to.IsIntegerType() && to.GetSizeInMemoryBytes() == ctx->type.dataType.GetSizeInMemoryBytes() )
  5150. cost = asCC_ENUM_SAME_SIZE_CONV;
  5151. else if (ctx->type.dataType.IsEnumType() && to.IsIntegerType() && to.GetSizeInMemoryBytes() != ctx->type.dataType.GetSizeInMemoryBytes())
  5152. cost = asCC_ENUM_DIFF_SIZE_CONV;
  5153. else if( to.IsUnsignedType() && ctx->type.dataType.IsIntegerType() )
  5154. cost = asCC_SIGNED_CONV;
  5155. else if( to.IsIntegerType() && ctx->type.dataType.IsUnsignedType() )
  5156. cost = asCC_SIGNED_CONV;
  5157. else if( to.GetSizeInMemoryBytes() != ctx->type.dataType.GetSizeInMemoryBytes() )
  5158. cost = asCC_PRIMITIVE_SIZE_CONV;
  5159. // Start by implicitly converting constant values
  5160. if( ctx->type.isConstant )
  5161. {
  5162. ImplicitConversionConstant(ctx, to, generateCode ? node : 0, convType);
  5163. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5164. return cost;
  5165. }
  5166. // Allow implicit conversion between numbers
  5167. if( generateCode )
  5168. {
  5169. // When generating the code the decision has already been made, so we don't bother determining the cost
  5170. // Convert smaller types to 32bit first
  5171. int s = ctx->type.dataType.GetSizeInMemoryBytes();
  5172. if( s < 4 )
  5173. {
  5174. ConvertToTempVariable(ctx);
  5175. if( ctx->type.dataType.IsIntegerType() )
  5176. {
  5177. if( s == 1 )
  5178. ctx->bc.InstrSHORT(asBC_sbTOi, ctx->type.stackOffset);
  5179. else if( s == 2 )
  5180. ctx->bc.InstrSHORT(asBC_swTOi, ctx->type.stackOffset);
  5181. ctx->type.dataType.SetTokenType(ttInt);
  5182. }
  5183. else if( ctx->type.dataType.IsUnsignedType() )
  5184. {
  5185. if( s == 1 )
  5186. ctx->bc.InstrSHORT(asBC_ubTOi, ctx->type.stackOffset);
  5187. else if( s == 2 )
  5188. ctx->bc.InstrSHORT(asBC_uwTOi, ctx->type.stackOffset);
  5189. ctx->type.dataType.SetTokenType(ttUInt);
  5190. }
  5191. }
  5192. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1 && !to.IsEnumType()) ||
  5193. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  5194. {
  5195. if( ctx->type.dataType.IsIntegerType() ||
  5196. ctx->type.dataType.IsUnsignedType() )
  5197. {
  5198. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5199. {
  5200. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5201. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5202. }
  5203. else
  5204. {
  5205. ConvertToTempVariable(ctx);
  5206. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5207. int offset = AllocateVariable(to, true);
  5208. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  5209. ctx->type.SetVariable(to, offset, true);
  5210. }
  5211. }
  5212. else if( ctx->type.dataType.IsFloatType() )
  5213. {
  5214. ConvertToTempVariable(ctx);
  5215. ctx->bc.InstrSHORT(asBC_fTOi, ctx->type.stackOffset);
  5216. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5217. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5218. if( convType != asIC_EXPLICIT_VAL_CAST )
  5219. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5220. }
  5221. else if( ctx->type.dataType.IsDoubleType() )
  5222. {
  5223. ConvertToTempVariable(ctx);
  5224. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5225. int offset = AllocateVariable(to, true);
  5226. ctx->bc.InstrW_W(asBC_dTOi, offset, ctx->type.stackOffset);
  5227. ctx->type.SetVariable(to, offset, true);
  5228. if( convType != asIC_EXPLICIT_VAL_CAST )
  5229. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5230. }
  5231. // Convert to smaller integer if necessary
  5232. s = to.GetSizeInMemoryBytes();
  5233. if( s < 4 )
  5234. {
  5235. ConvertToTempVariable(ctx);
  5236. if( s == 1 )
  5237. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  5238. else if( s == 2 )
  5239. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  5240. }
  5241. }
  5242. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  5243. {
  5244. if( ctx->type.dataType.IsIntegerType() ||
  5245. ctx->type.dataType.IsUnsignedType() )
  5246. {
  5247. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5248. {
  5249. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5250. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5251. }
  5252. else
  5253. {
  5254. ConvertToTempVariable(ctx);
  5255. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5256. int offset = AllocateVariable(to, true);
  5257. if( ctx->type.dataType.IsUnsignedType() )
  5258. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  5259. else
  5260. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  5261. ctx->type.SetVariable(to, offset, true);
  5262. }
  5263. }
  5264. else if( ctx->type.dataType.IsFloatType() )
  5265. {
  5266. ConvertToTempVariable(ctx);
  5267. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5268. int offset = AllocateVariable(to, true);
  5269. ctx->bc.InstrW_W(asBC_fTOi64, offset, ctx->type.stackOffset);
  5270. ctx->type.SetVariable(to, offset, true);
  5271. if( convType != asIC_EXPLICIT_VAL_CAST )
  5272. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5273. }
  5274. else if( ctx->type.dataType.IsDoubleType() )
  5275. {
  5276. ConvertToTempVariable(ctx);
  5277. ctx->bc.InstrSHORT(asBC_dTOi64, ctx->type.stackOffset);
  5278. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5279. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5280. if( convType != asIC_EXPLICIT_VAL_CAST )
  5281. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5282. }
  5283. }
  5284. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  5285. {
  5286. if( ctx->type.dataType.IsIntegerType() ||
  5287. ctx->type.dataType.IsUnsignedType() )
  5288. {
  5289. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5290. {
  5291. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5292. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5293. }
  5294. else
  5295. {
  5296. ConvertToTempVariable(ctx);
  5297. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5298. int offset = AllocateVariable(to, true);
  5299. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  5300. ctx->type.SetVariable(to, offset, true);
  5301. }
  5302. }
  5303. else if( ctx->type.dataType.IsFloatType() )
  5304. {
  5305. ConvertToTempVariable(ctx);
  5306. ctx->bc.InstrSHORT(asBC_fTOu, ctx->type.stackOffset);
  5307. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5308. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5309. if( convType != asIC_EXPLICIT_VAL_CAST )
  5310. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5311. }
  5312. else if( ctx->type.dataType.IsDoubleType() )
  5313. {
  5314. ConvertToTempVariable(ctx);
  5315. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5316. int offset = AllocateVariable(to, true);
  5317. ctx->bc.InstrW_W(asBC_dTOu, offset, ctx->type.stackOffset);
  5318. ctx->type.SetVariable(to, offset, true);
  5319. if( convType != asIC_EXPLICIT_VAL_CAST )
  5320. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5321. }
  5322. // Convert to smaller integer if necessary
  5323. s = to.GetSizeInMemoryBytes();
  5324. if( s < 4 )
  5325. {
  5326. ConvertToTempVariable(ctx);
  5327. if( s == 1 )
  5328. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  5329. else if( s == 2 )
  5330. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  5331. }
  5332. }
  5333. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  5334. {
  5335. if( ctx->type.dataType.IsIntegerType() ||
  5336. ctx->type.dataType.IsUnsignedType() )
  5337. {
  5338. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5339. {
  5340. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5341. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5342. }
  5343. else
  5344. {
  5345. ConvertToTempVariable(ctx);
  5346. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5347. int offset = AllocateVariable(to, true);
  5348. if( ctx->type.dataType.IsUnsignedType() )
  5349. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  5350. else
  5351. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  5352. ctx->type.SetVariable(to, offset, true);
  5353. }
  5354. }
  5355. else if( ctx->type.dataType.IsFloatType() )
  5356. {
  5357. ConvertToTempVariable(ctx);
  5358. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5359. int offset = AllocateVariable(to, true);
  5360. ctx->bc.InstrW_W(asBC_fTOu64, offset, ctx->type.stackOffset);
  5361. ctx->type.SetVariable(to, offset, true);
  5362. if( convType != asIC_EXPLICIT_VAL_CAST )
  5363. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5364. }
  5365. else if( ctx->type.dataType.IsDoubleType() )
  5366. {
  5367. ConvertToTempVariable(ctx);
  5368. ctx->bc.InstrSHORT(asBC_dTOu64, ctx->type.stackOffset);
  5369. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5370. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5371. if( convType != asIC_EXPLICIT_VAL_CAST )
  5372. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5373. }
  5374. }
  5375. else if( to.IsFloatType() )
  5376. {
  5377. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5378. {
  5379. ConvertToTempVariable(ctx);
  5380. ctx->bc.InstrSHORT(asBC_iTOf, ctx->type.stackOffset);
  5381. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5382. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5383. }
  5384. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5385. {
  5386. ConvertToTempVariable(ctx);
  5387. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5388. int offset = AllocateVariable(to, true);
  5389. ctx->bc.InstrW_W(asBC_i64TOf, offset, ctx->type.stackOffset);
  5390. ctx->type.SetVariable(to, offset, true);
  5391. }
  5392. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5393. {
  5394. ConvertToTempVariable(ctx);
  5395. ctx->bc.InstrSHORT(asBC_uTOf, ctx->type.stackOffset);
  5396. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5397. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5398. }
  5399. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5400. {
  5401. ConvertToTempVariable(ctx);
  5402. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5403. int offset = AllocateVariable(to, true);
  5404. ctx->bc.InstrW_W(asBC_u64TOf, offset, ctx->type.stackOffset);
  5405. ctx->type.SetVariable(to, offset, true);
  5406. }
  5407. else if( ctx->type.dataType.IsDoubleType() )
  5408. {
  5409. ConvertToTempVariable(ctx);
  5410. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5411. int offset = AllocateVariable(to, true);
  5412. ctx->bc.InstrW_W(asBC_dTOf, offset, ctx->type.stackOffset);
  5413. ctx->type.SetVariable(to, offset, true);
  5414. }
  5415. }
  5416. else if( to.IsDoubleType() )
  5417. {
  5418. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5419. {
  5420. ConvertToTempVariable(ctx);
  5421. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5422. int offset = AllocateVariable(to, true);
  5423. ctx->bc.InstrW_W(asBC_iTOd, offset, ctx->type.stackOffset);
  5424. ctx->type.SetVariable(to, offset, true);
  5425. }
  5426. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5427. {
  5428. ConvertToTempVariable(ctx);
  5429. ctx->bc.InstrSHORT(asBC_i64TOd, ctx->type.stackOffset);
  5430. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5431. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5432. }
  5433. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5434. {
  5435. ConvertToTempVariable(ctx);
  5436. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5437. int offset = AllocateVariable(to, true);
  5438. ctx->bc.InstrW_W(asBC_uTOd, offset, ctx->type.stackOffset);
  5439. ctx->type.SetVariable(to, offset, true);
  5440. }
  5441. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5442. {
  5443. ConvertToTempVariable(ctx);
  5444. ctx->bc.InstrSHORT(asBC_u64TOd, ctx->type.stackOffset);
  5445. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5446. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5447. }
  5448. else if( ctx->type.dataType.IsFloatType() )
  5449. {
  5450. ConvertToTempVariable(ctx);
  5451. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5452. int offset = AllocateVariable(to, true);
  5453. ctx->bc.InstrW_W(asBC_fTOd, offset, ctx->type.stackOffset);
  5454. ctx->type.SetVariable(to, offset, true);
  5455. }
  5456. }
  5457. }
  5458. else
  5459. {
  5460. if( ((to.IsIntegerType() && !to.IsEnumType()) || to.IsUnsignedType() ||
  5461. to.IsFloatType() || to.IsDoubleType() ||
  5462. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST)) &&
  5463. (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() ||
  5464. ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  5465. {
  5466. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5467. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5468. }
  5469. }
  5470. // Primitive types on the stack, can be const or non-const
  5471. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5472. return cost;
  5473. }
  5474. asUINT asCCompiler::ImplicitConvLambdaToFunc(asCExprContext *ctx, const asCDataType &to, asCScriptNode * /*node*/, EImplicitConv /*convType*/, bool generateCode)
  5475. {
  5476. asASSERT( to.IsFuncdef() && ctx->IsLambda() );
  5477. asCScriptFunction *funcDef = CastToFuncdefType(to.GetTypeInfo())->funcdef;
  5478. // Check that the lambda has the correct amount of arguments
  5479. asUINT count = 0;
  5480. asCScriptNode *argNode = ctx->exprNode->firstChild;
  5481. while( argNode->nodeType != snStatementBlock )
  5482. {
  5483. // Check if the specified parameter types match the funcdef
  5484. if (argNode->nodeType == snDataType)
  5485. {
  5486. asCDataType dt = builder->CreateDataTypeFromNode(argNode, script, outFunc->nameSpace, false, outFunc->objectType);
  5487. asETypeModifiers inOutFlag;
  5488. dt = builder->ModifyDataTypeFromNode(dt, argNode->next, script, &inOutFlag, 0);
  5489. if (count >= funcDef->parameterTypes.GetLength() ||
  5490. funcDef->parameterTypes[count] != dt ||
  5491. funcDef->inOutFlags[count] != inOutFlag)
  5492. return asCC_NO_CONV;
  5493. argNode = argNode->next;
  5494. }
  5495. if( argNode->nodeType == snIdentifier )
  5496. count++;
  5497. argNode = argNode->next;
  5498. }
  5499. if (funcDef->parameterTypes.GetLength() != count)
  5500. return asCC_NO_CONV;
  5501. asASSERT(argNode->nodeType == snStatementBlock);
  5502. // The Lambda can be used as this funcdef
  5503. ctx->type.dataType = to;
  5504. if( generateCode )
  5505. {
  5506. // Build a unique name for the anonymous function
  5507. asCString name;
  5508. if( m_globalVar )
  5509. name.Format("$%s$%d", m_globalVar->name.AddressOf(), numLambdas++);
  5510. else
  5511. name.Format("$%s$%d", outFunc->GetDeclaration(), numLambdas++);
  5512. // Register the lambda with the builder for later compilation
  5513. asCScriptFunction *func = builder->RegisterLambda(ctx->exprNode, script, funcDef, name, outFunc->nameSpace, outFunc->IsShared());
  5514. asASSERT( func == 0 || funcDef->IsSignatureExceptNameEqual(func) );
  5515. ctx->bc.InstrPTR(asBC_FuncPtr, func);
  5516. // Clear the expression node as it is no longer valid
  5517. ctx->exprNode = 0;
  5518. }
  5519. return asCC_CONST_CONV;
  5520. }
  5521. asUINT asCCompiler::ImplicitConversion(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  5522. {
  5523. asASSERT( ctx->type.dataType.GetTokenType() != ttUnrecognizedToken ||
  5524. ctx->type.dataType.IsNullHandle() ||
  5525. ctx->IsAnonymousInitList() );
  5526. if( to.IsFuncdef() && ctx->IsLambda() )
  5527. return ImplicitConvLambdaToFunc(ctx, to, node, convType, generateCode);
  5528. if (ctx->IsAnonymousInitList())
  5529. {
  5530. if (to.GetBehaviour() && to.GetBehaviour()->listFactory)
  5531. {
  5532. if (generateCode)
  5533. CompileAnonymousInitList(ctx->exprNode, ctx, to);
  5534. else
  5535. ctx->type.dataType = to;
  5536. }
  5537. return asCC_NO_CONV;
  5538. }
  5539. // No conversion from void to any other type
  5540. if( ctx->type.dataType.GetTokenType() == ttVoid )
  5541. return asCC_NO_CONV;
  5542. // No conversion from class method to any type (it requires delegate)
  5543. if( ctx->IsClassMethod() )
  5544. return asCC_NO_CONV;
  5545. // Do we want a var type?
  5546. if( to.GetTokenType() == ttQuestion )
  5547. {
  5548. // Any type can be converted to a var type, but only when not generating code
  5549. asASSERT( !generateCode );
  5550. ctx->type.dataType = to;
  5551. return asCC_VARIABLE_CONV;
  5552. }
  5553. // Do we want a primitive?
  5554. else if( to.IsPrimitive() )
  5555. {
  5556. if( !ctx->type.dataType.IsPrimitive() )
  5557. return ImplicitConvObjectToPrimitive(ctx, to, node, convType, generateCode);
  5558. else
  5559. return ImplicitConvPrimitiveToPrimitive(ctx, to, node, convType, generateCode);
  5560. }
  5561. else // The target is a complex type
  5562. {
  5563. if( ctx->type.dataType.IsPrimitive() )
  5564. return ImplicitConvPrimitiveToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  5565. else if( ctx->type.IsNullConstant() || ctx->type.dataType.GetTypeInfo() )
  5566. return ImplicitConvObjectToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  5567. }
  5568. return asCC_NO_CONV;
  5569. }
  5570. asUINT asCCompiler::ImplicitConvObjectToPrimitive(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5571. {
  5572. if( ctx->type.isExplicitHandle )
  5573. {
  5574. // An explicit handle cannot be converted to a primitive
  5575. if( convType != asIC_IMPLICIT_CONV && node )
  5576. {
  5577. asCString str;
  5578. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5579. Error(str, node);
  5580. }
  5581. return asCC_NO_CONV;
  5582. }
  5583. // Find matching value cast behaviours
  5584. // Here we're only interested in those that convert the type to a primitive type
  5585. asCArray<int> funcs;
  5586. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  5587. if( ot == 0 )
  5588. {
  5589. if( convType != asIC_IMPLICIT_CONV && node )
  5590. {
  5591. asCString str;
  5592. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5593. Error(str, node);
  5594. }
  5595. return asCC_NO_CONV;
  5596. }
  5597. if( convType == asIC_EXPLICIT_VAL_CAST )
  5598. {
  5599. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5600. {
  5601. // accept both implicit and explicit cast
  5602. asCScriptFunction *mthd = engine->scriptFunctions[ot->methods[n]];
  5603. if( (mthd->name == "opConv" || mthd->name == "opImplConv") &&
  5604. mthd->parameterTypes.GetLength() == 0 &&
  5605. mthd->returnType.IsPrimitive() )
  5606. funcs.PushLast(ot->methods[n]);
  5607. }
  5608. }
  5609. else
  5610. {
  5611. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5612. {
  5613. // accept only implicit cast
  5614. asCScriptFunction *mthd = engine->scriptFunctions[ot->methods[n]];
  5615. if( mthd->name == "opImplConv" &&
  5616. mthd->parameterTypes.GetLength() == 0 &&
  5617. mthd->returnType.IsPrimitive() )
  5618. funcs.PushLast(ot->methods[n]);
  5619. }
  5620. }
  5621. FilterConst(funcs, !ctx->type.dataType.IsReadOnly());
  5622. int funcId = 0;
  5623. if( to.IsMathType() )
  5624. {
  5625. // This matrix describes the priorities of the types to search for, for each target type
  5626. // The first column is the target type, the priorities goes from left to right
  5627. eTokenType matchMtx[10][10] =
  5628. {
  5629. {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  5630. {ttFloat, ttDouble, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  5631. {ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  5632. {ttUInt64, ttInt64, ttUInt, ttInt, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  5633. {ttInt, ttUInt, ttInt64, ttUInt64, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  5634. {ttUInt, ttInt, ttUInt64, ttInt64, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  5635. {ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttInt8, ttUInt8, ttDouble, ttFloat},
  5636. {ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttUInt8, ttInt8, ttDouble, ttFloat},
  5637. {ttInt8, ttUInt8, ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttDouble, ttFloat},
  5638. {ttUInt8, ttInt8, ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttDouble, ttFloat},
  5639. };
  5640. // Which row to use?
  5641. eTokenType *row = 0;
  5642. for( unsigned int type = 0; type < 10; type++ )
  5643. {
  5644. if( to.GetTokenType() == matchMtx[type][0] )
  5645. {
  5646. row = &matchMtx[type][0];
  5647. break;
  5648. }
  5649. }
  5650. // Find the best matching cast operator
  5651. if( row )
  5652. {
  5653. asCDataType target(to);
  5654. // Priority goes from left to right in the matrix
  5655. for( unsigned int attempt = 0; attempt < 10 && funcId == 0; attempt++ )
  5656. {
  5657. target.SetTokenType(row[attempt]);
  5658. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  5659. {
  5660. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  5661. if( descr->returnType.IsEqualExceptRefAndConst(target) )
  5662. {
  5663. funcId = funcs[n];
  5664. break;
  5665. }
  5666. }
  5667. }
  5668. }
  5669. }
  5670. else
  5671. {
  5672. // Only accept the exact conversion for non-math types
  5673. // Find the matching cast operator
  5674. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  5675. {
  5676. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  5677. if( descr->returnType.IsEqualExceptRefAndConst(to) )
  5678. {
  5679. funcId = funcs[n];
  5680. break;
  5681. }
  5682. }
  5683. }
  5684. // Did we find a suitable function?
  5685. if( funcId != 0 )
  5686. {
  5687. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  5688. if( generateCode )
  5689. {
  5690. Dereference(ctx, true);
  5691. PerformFunctionCall(funcId, ctx);
  5692. }
  5693. else
  5694. ctx->type.Set(descr->returnType);
  5695. // Allow one more implicit conversion to another primitive type
  5696. return asCC_OBJ_TO_PRIMITIVE_CONV + ImplicitConversion(ctx, to, node, convType, generateCode, false);
  5697. }
  5698. // TODO: clean-up: This part is similar to what is in ImplicitConvObjectValue
  5699. // If no direct conversion is found we should look for the generic form 'void opConv(?&out)'
  5700. funcs.SetLength(0);
  5701. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  5702. {
  5703. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5704. if( ((convType == asIC_EXPLICIT_VAL_CAST) && func->name == "opConv") ||
  5705. func->name == "opImplConv" )
  5706. {
  5707. // Does the operator take the ?&out parameter?
  5708. if( func->returnType != asCDataType::CreatePrimitive(ttVoid, false) ||
  5709. func->parameterTypes.GetLength() != 1 ||
  5710. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  5711. func->inOutFlags[0] != asTM_OUTREF )
  5712. continue;
  5713. funcs.PushLast(ot->methods[n]);
  5714. }
  5715. }
  5716. FilterConst(funcs, !ctx->type.dataType.IsReadOnly());
  5717. // If there are multiple valid value casts, then we must choose the most appropriate one
  5718. if (funcs.GetLength() > 1)
  5719. {
  5720. // This should only happen in case of explicit value cast and
  5721. // the application has registered both opImplConv and opConv
  5722. asASSERT(convType == asIC_EXPLICIT_VAL_CAST);
  5723. asASSERT(funcs.GetLength() == 2);
  5724. for (asUINT n = 0; n < funcs.GetLength(); n++)
  5725. {
  5726. asCScriptFunction *func = engine->scriptFunctions[funcs[n]];
  5727. if (func->name == "opImplConv")
  5728. {
  5729. funcs.RemoveIndex(n);
  5730. n--;
  5731. }
  5732. }
  5733. }
  5734. if( funcs.GetLength() == 1 )
  5735. {
  5736. if( generateCode )
  5737. {
  5738. // Allocate a temporary variable of the requested type
  5739. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  5740. CallDefaultConstructor(to, stackOffset, IsVariableOnHeap(stackOffset), &ctx->bc, node);
  5741. // Pass the reference of that variable to the function as output parameter
  5742. asCDataType toRef(to);
  5743. toRef.MakeReference(true);
  5744. toRef.MakeReadOnly(false);
  5745. asCArray<asCExprContext *> args;
  5746. asCExprContext arg(engine);
  5747. // Don't mark the variable as temporary, so it won't be freed too early
  5748. arg.type.SetVariable(toRef, stackOffset, false);
  5749. arg.type.isLValue = true;
  5750. arg.exprNode = node;
  5751. args.PushLast(&arg);
  5752. // Call the behaviour method
  5753. MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  5754. // Use the reference to the variable as the result of the expression
  5755. // Now we can mark the variable as temporary
  5756. toRef.MakeReference(false);
  5757. ctx->type.SetVariable(toRef, stackOffset, true);
  5758. }
  5759. else
  5760. ctx->type.Set(to);
  5761. return asCC_OBJ_TO_PRIMITIVE_CONV;
  5762. }
  5763. if( convType != asIC_IMPLICIT_CONV && node )
  5764. {
  5765. asCString str;
  5766. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5767. Error(str, node);
  5768. }
  5769. return asCC_NO_CONV;
  5770. }
  5771. asUINT asCCompiler::ImplicitConvObjectRef(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5772. {
  5773. // Convert null to any object type handle, but not to a non-handle type
  5774. if( ctx->type.IsNullConstant() && ctx->methodName == "" )
  5775. {
  5776. if( to.IsObjectHandle() )
  5777. {
  5778. ctx->type.dataType = to;
  5779. return asCC_REF_CONV;
  5780. }
  5781. return asCC_NO_CONV;
  5782. }
  5783. asASSERT(ctx->type.dataType.GetTypeInfo() || ctx->methodName != "");
  5784. // First attempt to convert the base type without instantiating another instance
  5785. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() && ctx->methodName == "" )
  5786. {
  5787. // If the to type is an interface and the from type implements it, then we can convert it immediately
  5788. if( ctx->type.dataType.GetTypeInfo()->Implements(to.GetTypeInfo()) )
  5789. {
  5790. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5791. return asCC_REF_CONV;
  5792. }
  5793. // If the to type is a class and the from type derives from it, then we can convert it immediately
  5794. else if( ctx->type.dataType.GetTypeInfo()->DerivesFrom(to.GetTypeInfo()) )
  5795. {
  5796. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5797. return asCC_REF_CONV;
  5798. }
  5799. // If the types are not equal yet, then we may still be able to find a reference cast
  5800. else if( ctx->type.dataType.GetTypeInfo() != to.GetTypeInfo() )
  5801. {
  5802. // We may still be able to find an implicit ref cast behaviour
  5803. CompileRefCast(ctx, to, convType == asIC_EXPLICIT_REF_CAST, node, generateCode);
  5804. // Was the conversion done?
  5805. if( ctx->type.dataType.GetTypeInfo() == to.GetTypeInfo() )
  5806. return asCC_REF_CONV;
  5807. }
  5808. }
  5809. // Convert matching function types
  5810. if( to.IsFuncdef() )
  5811. {
  5812. // If the input expression is already a funcdef, check if it can be converted
  5813. if( ctx->type.dataType.IsFuncdef() &&
  5814. to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() )
  5815. {
  5816. asCScriptFunction *toFunc = CastToFuncdefType(to.GetTypeInfo())->funcdef;
  5817. asCScriptFunction *fromFunc = CastToFuncdefType(ctx->type.dataType.GetTypeInfo())->funcdef;
  5818. if( toFunc->IsSignatureExceptNameEqual(fromFunc) )
  5819. {
  5820. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5821. return asCC_REF_CONV;
  5822. }
  5823. }
  5824. // If the input expression is a deferred function ref, check if there is a matching func
  5825. if( ctx->methodName != "" )
  5826. {
  5827. // Determine the namespace
  5828. asSNameSpace *ns = 0;
  5829. asCString name = "";
  5830. int pos = ctx->methodName.FindLast("::");
  5831. if( pos >= 0 )
  5832. {
  5833. asCString nsName = ctx->methodName.SubString(0, pos+2);
  5834. // Trim off the last ::
  5835. if( nsName.GetLength() > 2 )
  5836. nsName.SetLength(nsName.GetLength()-2);
  5837. ns = DetermineNameSpace(nsName);
  5838. name = ctx->methodName.SubString(pos+2);
  5839. }
  5840. else
  5841. {
  5842. DetermineNameSpace("");
  5843. name = ctx->methodName;
  5844. }
  5845. asCArray<int> funcs;
  5846. if( ns )
  5847. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  5848. // Check if any of the functions have perfect match
  5849. asCScriptFunction *toFunc = CastToFuncdefType(to.GetTypeInfo())->funcdef;
  5850. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  5851. {
  5852. asCScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  5853. if( toFunc->IsSignatureExceptNameEqual(func) )
  5854. {
  5855. if( generateCode )
  5856. {
  5857. ctx->bc.InstrPTR(asBC_FuncPtr, func);
  5858. // Make sure the identified function is shared if we're compiling a shared function
  5859. if( !func->IsShared() && outFunc->IsShared() )
  5860. {
  5861. asCString msg;
  5862. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, func->GetDeclaration());
  5863. Error(msg, node);
  5864. }
  5865. }
  5866. ctx->type.dataType = asCDataType::CreateType(to.GetTypeInfo(), false);
  5867. return asCC_REF_CONV;
  5868. }
  5869. }
  5870. }
  5871. }
  5872. return asCC_NO_CONV;
  5873. }
  5874. asUINT asCCompiler::ImplicitConvObjectValue(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5875. {
  5876. asUINT cost = asCC_NO_CONV;
  5877. // If the base type is still different, and we are allowed to instance
  5878. // another object then we can try an implicit value cast
  5879. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() )
  5880. {
  5881. // TODO: Implement support for implicit constructor/factory
  5882. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  5883. if( ot == 0 )
  5884. return cost;
  5885. asCArray<int> funcs;
  5886. if( convType == asIC_EXPLICIT_VAL_CAST )
  5887. {
  5888. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5889. {
  5890. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5891. // accept both implicit and explicit cast
  5892. if( (func->name == "opConv" ||
  5893. func->name == "opImplConv") &&
  5894. func->returnType.GetTypeInfo() == to.GetTypeInfo() &&
  5895. func->parameterTypes.GetLength() == 0 )
  5896. funcs.PushLast(ot->methods[n]);
  5897. }
  5898. }
  5899. else
  5900. {
  5901. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5902. {
  5903. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5904. // accept only implicit cast
  5905. if( func->name == "opImplConv" &&
  5906. func->returnType.GetTypeInfo() == to.GetTypeInfo() &&
  5907. func->parameterTypes.GetLength() == 0 )
  5908. funcs.PushLast(ot->methods[n]);
  5909. }
  5910. }
  5911. FilterConst(funcs, !ctx->type.dataType.IsReadOnly());
  5912. // If there are multiple valid value casts, then we must choose the most appropriate one
  5913. if (funcs.GetLength() > 1)
  5914. {
  5915. // This should only happen in case of explicit value cast and
  5916. // the application has registered both opImplConv and opConv
  5917. asASSERT(convType == asIC_EXPLICIT_VAL_CAST);
  5918. asASSERT(funcs.GetLength() == 2);
  5919. for (asUINT n = 0; n < funcs.GetLength(); n++)
  5920. {
  5921. asCScriptFunction *func = engine->scriptFunctions[funcs[n]];
  5922. if (func->name == "opImplConv")
  5923. {
  5924. funcs.RemoveIndex(n);
  5925. n--;
  5926. }
  5927. }
  5928. }
  5929. if( funcs.GetLength() == 1 )
  5930. {
  5931. asCScriptFunction *f = builder->GetFunctionDescription(funcs[0]);
  5932. if( generateCode )
  5933. {
  5934. Dereference(ctx, true);
  5935. bool useVariable = false;
  5936. int stackOffset = 0;
  5937. if( f->DoesReturnOnStack() )
  5938. {
  5939. useVariable = true;
  5940. stackOffset = AllocateVariable(f->returnType, true);
  5941. // Push the pointer to the pre-allocated space for the return value
  5942. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  5943. // The object pointer is already on the stack, but should be the top
  5944. // one, so we need to swap the pointers in order to get the correct
  5945. ctx->bc.Instr(asBC_SwapPtr);
  5946. }
  5947. PerformFunctionCall(funcs[0], ctx, false, 0, 0, useVariable, stackOffset);
  5948. }
  5949. else
  5950. ctx->type.Set(f->returnType);
  5951. cost = asCC_TO_OBJECT_CONV;
  5952. }
  5953. else
  5954. {
  5955. // TODO: cleanup: This part is similar to the second half of ImplicitConvObjectToPrimitive
  5956. // Look for a value cast with variable type
  5957. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  5958. {
  5959. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5960. if( ((convType == asIC_EXPLICIT_VAL_CAST) && func->name == "opConv") ||
  5961. func->name == "opImplConv" )
  5962. {
  5963. // Does the operator take the ?&out parameter?
  5964. if( func->returnType != asCDataType::CreatePrimitive(ttVoid, false) ||
  5965. func->parameterTypes.GetLength() != 1 ||
  5966. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  5967. func->inOutFlags[0] != asTM_OUTREF )
  5968. continue;
  5969. funcs.PushLast(ot->methods[n]);
  5970. }
  5971. }
  5972. FilterConst(funcs, !ctx->type.dataType.IsReadOnly());
  5973. // If there are multiple valid value casts, then we must choose the most appropriate one
  5974. if (funcs.GetLength() > 1)
  5975. {
  5976. // This should only happen in case of explicit value cast and
  5977. // the application has registered both opImplConv and opConv
  5978. asASSERT(convType == asIC_EXPLICIT_VAL_CAST);
  5979. asASSERT(funcs.GetLength() == 2);
  5980. for (asUINT n = 0; n < funcs.GetLength(); n++)
  5981. {
  5982. asCScriptFunction *func = engine->scriptFunctions[funcs[n]];
  5983. if (func->name == "opImplConv")
  5984. {
  5985. funcs.RemoveIndex(n);
  5986. n--;
  5987. }
  5988. }
  5989. }
  5990. if( funcs.GetLength() == 1 )
  5991. {
  5992. cost = asCC_TO_OBJECT_CONV;
  5993. if( generateCode )
  5994. {
  5995. // Allocate a temporary variable of the requested type
  5996. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  5997. CallDefaultConstructor(to, stackOffset, IsVariableOnHeap(stackOffset), &ctx->bc, node);
  5998. // Pass the reference of that variable to the function as output parameter
  5999. asCDataType toRef(to);
  6000. toRef.MakeReference(false);
  6001. asCExprContext arg(engine);
  6002. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  6003. // If this an object on the heap, the pointer must be dereferenced
  6004. if( IsVariableOnHeap(stackOffset) )
  6005. arg.bc.Instr(asBC_RDSPtr);
  6006. // Don't mark the variable as temporary, so it won't be freed too early
  6007. arg.type.SetVariable(toRef, stackOffset, false);
  6008. arg.type.isLValue = true;
  6009. arg.exprNode = node;
  6010. // Mark the argument as clean, so that MakeFunctionCall knows it
  6011. // doesn't have to make a copy of it in order to protect the value
  6012. arg.isCleanArg = true;
  6013. // Call the behaviour method
  6014. asCArray<asCExprContext *> args;
  6015. args.PushLast(&arg);
  6016. MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  6017. // Use the reference to the variable as the result of the expression
  6018. // Now we can mark the variable as temporary
  6019. ctx->type.SetVariable(toRef, stackOffset, true);
  6020. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  6021. }
  6022. else
  6023. {
  6024. // All casts are legal
  6025. ctx->type.Set(to);
  6026. }
  6027. }
  6028. else if( CastToObjectType(to.GetTypeInfo()) )
  6029. {
  6030. // If no opConv/opImplConv methods were found on the object, then try to find a conversion constructor on the target type
  6031. if( to.GetTypeInfo()->flags & asOBJ_REF )
  6032. funcs = CastToObjectType(to.GetTypeInfo())->beh.factories;
  6033. else
  6034. funcs = CastToObjectType(to.GetTypeInfo())->beh.constructors;
  6035. // If not explicit cast, remove any explicit conversion constructors
  6036. for (asUINT n = 0; n < funcs.GetLength(); n++)
  6037. {
  6038. asCScriptFunction *f = engine->scriptFunctions[funcs[n]];
  6039. if( f == 0 || f->parameterTypes.GetLength() != 1 || (convType != asIC_EXPLICIT_VAL_CAST && f->IsExplicit()) )
  6040. funcs.RemoveIndex(n--);
  6041. }
  6042. asCArray<asCExprContext *> args;
  6043. args.PushLast(ctx);
  6044. cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, node, 0, 0, 0, false, true, false);
  6045. // Did we find a matching constructor?
  6046. if (funcs.GetLength() == 1)
  6047. {
  6048. if (generateCode)
  6049. {
  6050. // TODO: This should really reuse the code from CompileConstructCall
  6051. // Allocate the new object
  6052. asCExprValue tempObj;
  6053. asCExprContext e(engine);
  6054. bool onHeap = false;
  6055. if (to.GetTypeInfo()->flags & asOBJ_VALUE)
  6056. {
  6057. tempObj.dataType = to;
  6058. tempObj.dataType.MakeReference(false);
  6059. tempObj.stackOffset = (short)AllocateVariable(tempObj.dataType, true);
  6060. tempObj.dataType.MakeReference(true);
  6061. tempObj.isTemporary = true;
  6062. tempObj.isVariable = true;
  6063. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  6064. // Push the address of the object on the stack
  6065. if (onHeap)
  6066. e.bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  6067. }
  6068. PrepareFunctionCall(funcs[0], &e.bc, args);
  6069. MoveArgsToStack(funcs[0], &e.bc, args, false);
  6070. if (to.GetTypeInfo()->flags & asOBJ_VALUE)
  6071. {
  6072. // If the object is allocated on the stack, then call the constructor as a normal function
  6073. if (onHeap)
  6074. {
  6075. int offset = 0;
  6076. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  6077. offset = descr->parameterTypes[0].GetSizeOnStackDWords();
  6078. e.bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  6079. }
  6080. else
  6081. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6082. }
  6083. PerformFunctionCall(funcs[0], &e, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  6084. if (to.GetTypeInfo()->flags & asOBJ_VALUE)
  6085. {
  6086. // Add tag that the object has been initialized
  6087. e.bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  6088. // The constructor doesn't return anything,
  6089. // so we have to manually inform the type of
  6090. // the return value
  6091. e.type = tempObj;
  6092. if (!onHeap)
  6093. e.type.dataType.MakeReference(false);
  6094. // Push the address of the object on the stack again
  6095. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6096. }
  6097. MergeExprBytecodeAndType(ctx, &e);
  6098. }
  6099. else
  6100. {
  6101. ctx->type.Set(asCDataType::CreateType(to.GetTypeInfo(), false));
  6102. }
  6103. }
  6104. }
  6105. }
  6106. }
  6107. return cost;
  6108. }
  6109. asUINT asCCompiler::ImplicitConvObjectToObject(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  6110. {
  6111. // First try a ref cast
  6112. asUINT cost = ImplicitConvObjectRef(ctx, to, node, convType, generateCode);
  6113. // If the desired type is an asOBJ_ASHANDLE then we'll assume it is allowed to implicitly
  6114. // construct the object through any of the available constructors (except those marked as explicit)
  6115. if( to.GetTypeInfo() && (to.GetTypeInfo()->flags & asOBJ_ASHANDLE) && to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() && allowObjectConstruct )
  6116. {
  6117. asCArray<int> funcs;
  6118. funcs = CastToObjectType(to.GetTypeInfo())->beh.constructors;
  6119. // Don't allow use of explicit constructors/factories in implicit conversions
  6120. if (convType == asIC_IMPLICIT_CONV)
  6121. {
  6122. for (asUINT n = 0; n < funcs.GetLength(); n++)
  6123. {
  6124. asCScriptFunction* desc = builder->GetFunctionDescription(funcs[n]);
  6125. if (desc->IsExplicit())
  6126. funcs.RemoveIndex(n--);
  6127. }
  6128. }
  6129. asCArray<asCExprContext *> args;
  6130. args.PushLast(ctx);
  6131. cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, node, 0, 0, 0, false, true, false);
  6132. // Did we find a matching constructor?
  6133. if( funcs.GetLength() == 1 )
  6134. {
  6135. if( generateCode )
  6136. {
  6137. // If the ASHANDLE receives a variable type parameter, then we need to
  6138. // make sure the expression is treated as a handle and not as a value
  6139. asCScriptFunction *func = engine->scriptFunctions[funcs[0]];
  6140. if( func->parameterTypes[0].GetTokenType() == ttQuestion )
  6141. {
  6142. if( !ctx->type.isExplicitHandle )
  6143. {
  6144. asCDataType toHandle = ctx->type.dataType;
  6145. toHandle.MakeHandle(true);
  6146. toHandle.MakeReference(true);
  6147. toHandle.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  6148. ImplicitConversion(ctx, toHandle, node, asIC_IMPLICIT_CONV, true, false);
  6149. asASSERT( ctx->type.dataType.IsObjectHandle() );
  6150. }
  6151. ctx->type.isExplicitHandle = true;
  6152. }
  6153. // TODO: This should really reuse the code from CompileConstructCall
  6154. // Allocate the new object
  6155. asCExprValue tempObj;
  6156. tempObj.dataType = to;
  6157. tempObj.dataType.MakeReference(false);
  6158. tempObj.stackOffset = (short)AllocateVariable(tempObj.dataType, true);
  6159. tempObj.dataType.MakeReference(true);
  6160. tempObj.isTemporary = true;
  6161. tempObj.isVariable = true;
  6162. bool onHeap = IsVariableOnHeap(tempObj.stackOffset);
  6163. // Push the address of the object on the stack
  6164. asCExprContext e(engine);
  6165. if( onHeap )
  6166. e.bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  6167. PrepareFunctionCall(funcs[0], &e.bc, args);
  6168. MoveArgsToStack(funcs[0], &e.bc, args, false);
  6169. // If the object is allocated on the stack, then call the constructor as a normal function
  6170. if( onHeap )
  6171. {
  6172. int offset = 0;
  6173. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  6174. offset = descr->parameterTypes[0].GetSizeOnStackDWords();
  6175. e.bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  6176. }
  6177. else
  6178. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6179. PerformFunctionCall(funcs[0], &e, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  6180. // Add tag that the object has been initialized
  6181. e.bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  6182. // The constructor doesn't return anything,
  6183. // so we have to manually inform the type of
  6184. // the return value
  6185. e.type = tempObj;
  6186. if( !onHeap )
  6187. e.type.dataType.MakeReference(false);
  6188. // Push the address of the object on the stack again
  6189. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6190. MergeExprBytecodeAndType(ctx, &e);
  6191. }
  6192. else
  6193. {
  6194. ctx->type.Set(asCDataType::CreateType(to.GetTypeInfo(), false));
  6195. }
  6196. }
  6197. }
  6198. // If the base type is still different, and we are allowed to instance
  6199. // another object then we can try an implicit value cast
  6200. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() && allowObjectConstruct )
  6201. {
  6202. // Attempt implicit value cast
  6203. cost = ImplicitConvObjectValue(ctx, to, node, convType, generateCode);
  6204. }
  6205. // If we still haven't converted the base type to the correct type, then there is
  6206. // no need to continue as it is not possible to do the conversion
  6207. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() )
  6208. return asCC_NO_CONV;
  6209. if( to.IsObjectHandle() )
  6210. {
  6211. // There is no extra cost in converting to a handle
  6212. // reference to handle -> handle
  6213. // reference -> handle
  6214. // object -> handle
  6215. // handle -> reference to handle
  6216. // reference -> reference to handle
  6217. // object -> reference to handle
  6218. if( (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly() && !to.IsHandleToConst()) ||
  6219. (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst() && !to.IsHandleToConst()) )
  6220. {
  6221. // String literals can be implicitly converted to temporary local variables in order to pass them to functions expecting non-const
  6222. // TODO: NEWSTRING: Should have an engine property to warn or error on this
  6223. if (ctx->type.isConstant && ctx->type.dataType.IsEqualExceptRefAndConst(engine->stringType))
  6224. {
  6225. if (generateCode)
  6226. PrepareTemporaryVariable(node, ctx);
  6227. else
  6228. {
  6229. ctx->type.dataType.MakeReadOnly(false);
  6230. ctx->type.isConstant = false;
  6231. }
  6232. // Add the cost for the copy
  6233. cost += asCC_TO_OBJECT_CONV;
  6234. }
  6235. else if( convType != asIC_IMPLICIT_CONV )
  6236. {
  6237. asASSERT(node);
  6238. asCString str;
  6239. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  6240. Error(str, node);
  6241. }
  6242. }
  6243. if( !ctx->type.dataType.IsObjectHandle() )
  6244. {
  6245. // An object type can be directly converted to a handle of the
  6246. // same type by doing a ref copy to a new variable
  6247. if( ctx->type.dataType.SupportHandles() )
  6248. {
  6249. asCDataType dt = ctx->type.dataType;
  6250. dt.MakeHandle(true);
  6251. dt.MakeReference(false);
  6252. if( generateCode )
  6253. {
  6254. // If the expression is already a local variable, then it is not
  6255. // necessary to do a ref copy, as the ref objects on the stack are
  6256. // really handles, only the handles cannot be modified.
  6257. if( ctx->type.isVariable )
  6258. {
  6259. bool isHandleToConst = ctx->type.dataType.IsReadOnly();
  6260. ctx->type.dataType.MakeReadOnly(false);
  6261. ctx->type.dataType.MakeHandle(true);
  6262. ctx->type.dataType.MakeReadOnly(true);
  6263. ctx->type.dataType.MakeHandleToConst(isHandleToConst);
  6264. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  6265. {
  6266. ctx->bc.Instr(asBC_PopPtr);
  6267. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  6268. ctx->type.dataType.MakeReference(true);
  6269. }
  6270. else if( ctx->type.dataType.IsReference() )
  6271. {
  6272. ctx->bc.Instr(asBC_RDSPtr);
  6273. ctx->type.dataType.MakeReference(false);
  6274. }
  6275. }
  6276. else
  6277. {
  6278. int offset = AllocateVariable(dt, true);
  6279. if( ctx->type.dataType.IsReference() )
  6280. ctx->bc.Instr(asBC_RDSPtr);
  6281. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  6282. if (dt.IsFuncdef())
  6283. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  6284. else
  6285. ctx->bc.InstrPTR(asBC_REFCPY, dt.GetTypeInfo());
  6286. ctx->bc.Instr(asBC_PopPtr);
  6287. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  6288. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  6289. if( to.IsReference() )
  6290. dt.MakeReference(true);
  6291. else
  6292. ctx->bc.Instr(asBC_RDSPtr);
  6293. ctx->type.SetVariable(dt, offset, true);
  6294. }
  6295. }
  6296. else
  6297. ctx->type.dataType = dt;
  6298. // When this conversion is done the expression is no longer an lvalue
  6299. ctx->type.isLValue = false;
  6300. }
  6301. }
  6302. if( ctx->type.dataType.IsObjectHandle() )
  6303. {
  6304. // A handle to non-const can be converted to a
  6305. // handle to const, but not the other way
  6306. if( to.IsHandleToConst() )
  6307. ctx->type.dataType.MakeHandleToConst(true);
  6308. // A const handle can be converted to a non-const
  6309. // handle and vice versa as the handle is just a value
  6310. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  6311. }
  6312. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  6313. {
  6314. if( generateCode )
  6315. {
  6316. asASSERT( ctx->type.dataType.IsObjectHandle() );
  6317. // If the input type is a handle, then a simple ref copy is enough
  6318. bool isExplicitHandle = ctx->type.isExplicitHandle;
  6319. ctx->type.isExplicitHandle = ctx->type.dataType.IsObjectHandle();
  6320. // If the input type is read-only we'll need to temporarily
  6321. // remove this constness, otherwise the assignment will fail
  6322. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  6323. ctx->type.dataType.MakeReadOnly(false);
  6324. // If the object already is a temporary variable, then the copy
  6325. // doesn't have to be made as it is already a unique object
  6326. PrepareTemporaryVariable(node, ctx);
  6327. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  6328. ctx->type.isExplicitHandle = isExplicitHandle;
  6329. }
  6330. // A non-reference can be converted to a reference,
  6331. // by putting the value in a temporary variable
  6332. ctx->type.dataType.MakeReference(true);
  6333. // Since it is a new temporary variable it doesn't have to be const
  6334. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  6335. }
  6336. else if( !to.IsReference() && ctx->type.dataType.IsReference() )
  6337. {
  6338. Dereference(ctx, generateCode);
  6339. }
  6340. }
  6341. else // if( !to.IsObjectHandle() )
  6342. {
  6343. if( !to.IsReference() )
  6344. {
  6345. // reference to handle -> object
  6346. // handle -> object
  6347. // reference -> object
  6348. // An implicit handle can be converted to an object by adding a check for null pointer
  6349. if( ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  6350. {
  6351. if( generateCode )
  6352. {
  6353. if( ctx->type.dataType.IsReference() )
  6354. {
  6355. // The pointer on the stack refers to the handle
  6356. ctx->bc.Instr(asBC_ChkRefS);
  6357. }
  6358. else
  6359. {
  6360. // The pointer on the stack refers to the object
  6361. ctx->bc.Instr(asBC_CHKREF);
  6362. }
  6363. }
  6364. ctx->type.dataType.MakeHandle(false);
  6365. }
  6366. // A const object can be converted to a non-const object through a copy
  6367. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() &&
  6368. allowObjectConstruct )
  6369. {
  6370. // Does the object type allow a copy to be made?
  6371. if( ctx->type.dataType.CanBeCopied() )
  6372. {
  6373. if( generateCode )
  6374. {
  6375. // Make a temporary object with the copy
  6376. PrepareTemporaryVariable(node, ctx);
  6377. }
  6378. // In case the object was already in a temporary variable, then the function
  6379. // didn't really do anything so we need to remove the constness here
  6380. ctx->type.dataType.MakeReadOnly(false);
  6381. // Add the cost for the copy
  6382. cost += asCC_TO_OBJECT_CONV;
  6383. }
  6384. }
  6385. if( ctx->type.dataType.IsReference() )
  6386. {
  6387. // This may look strange, but a value type allocated on the stack is already
  6388. // correct, so nothing should be done other than remove the mark as reference.
  6389. // For types allocated on the heap, it is necessary to dereference the pointer
  6390. // that is currently on the stack
  6391. if( IsVariableOnHeap(ctx->type.stackOffset) )
  6392. Dereference(ctx, generateCode);
  6393. else
  6394. ctx->type.dataType.MakeReference(false);
  6395. }
  6396. // A non-const object can be converted to a const object directly
  6397. if( !ctx->type.dataType.IsReadOnly() && to.IsReadOnly() )
  6398. {
  6399. ctx->type.dataType.MakeReadOnly(true);
  6400. }
  6401. }
  6402. else // if( to.IsReference() )
  6403. {
  6404. // reference to handle -> reference
  6405. // handle -> reference
  6406. // object -> reference
  6407. if( ctx->type.dataType.IsReference() )
  6408. {
  6409. if( ctx->type.isExplicitHandle && ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  6410. {
  6411. // ASHANDLE objects are really value types, so explicit handle can be removed
  6412. ctx->type.isExplicitHandle = false;
  6413. ctx->type.dataType.MakeHandle(false);
  6414. }
  6415. // A reference to a handle can be converted to a reference to an object
  6416. // by first reading the address, then verifying that it is not null
  6417. if( !to.IsObjectHandle() && ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  6418. {
  6419. ctx->type.dataType.MakeHandle(false);
  6420. if( generateCode )
  6421. ctx->bc.Instr(asBC_ChkRefS);
  6422. }
  6423. // A reference to a non-const can be converted to a reference to a const
  6424. if( to.IsReadOnly() )
  6425. ctx->type.dataType.MakeReadOnly(true);
  6426. else if( ctx->type.dataType.IsReadOnly() && allowObjectConstruct )
  6427. {
  6428. // A reference to a const can be converted to a reference to a
  6429. // non-const by copying the object to a temporary variable
  6430. ctx->type.dataType.MakeReadOnly(false);
  6431. if( generateCode )
  6432. {
  6433. // If the object already is a temporary variable, then the copy
  6434. // doesn't have to be made as it is already a unique object
  6435. PrepareTemporaryVariable(node, ctx);
  6436. }
  6437. // Add the cost for the copy
  6438. cost += asCC_TO_OBJECT_CONV;
  6439. }
  6440. }
  6441. else // if( !ctx->type.dataType.IsReference() )
  6442. {
  6443. // A non-reference handle can be converted to a non-handle reference by checking against null handle
  6444. if( ctx->type.dataType.IsObjectHandle() )
  6445. {
  6446. bool readOnly = false;
  6447. if( ctx->type.dataType.IsHandleToConst() )
  6448. readOnly = true;
  6449. if( generateCode )
  6450. {
  6451. if( ctx->type.isVariable )
  6452. ctx->bc.InstrSHORT(asBC_ChkNullV, ctx->type.stackOffset);
  6453. else
  6454. ctx->bc.Instr(asBC_CHKREF);
  6455. }
  6456. ctx->type.dataType.MakeHandle(false);
  6457. ctx->type.dataType.MakeReference(true);
  6458. // Make sure a handle to const isn't converted to non-const reference
  6459. if( readOnly )
  6460. ctx->type.dataType.MakeReadOnly(true);
  6461. }
  6462. else
  6463. {
  6464. // A value type allocated on the stack is differentiated
  6465. // by it not being a reference. But it can be handled as
  6466. // reference by pushing the pointer on the stack
  6467. if( (ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) &&
  6468. (ctx->type.isVariable || ctx->type.isTemporary) &&
  6469. !IsVariableOnHeap(ctx->type.stackOffset) )
  6470. {
  6471. // Actually the pointer is already pushed on the stack in
  6472. // CompileVariableAccess, so we don't need to do anything else
  6473. }
  6474. else if( generateCode )
  6475. {
  6476. // A non-reference can be converted to a reference,
  6477. // by putting the value in a temporary variable
  6478. // If the input type is read-only we'll need to temporarily
  6479. // remove this constness, otherwise the assignment will fail
  6480. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  6481. ctx->type.dataType.MakeReadOnly(false);
  6482. // If the object already is a temporary variable, then the copy
  6483. // doesn't have to be made as it is already a unique object
  6484. PrepareTemporaryVariable(node, ctx);
  6485. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  6486. // Add the cost for the copy
  6487. cost += asCC_TO_OBJECT_CONV;
  6488. }
  6489. // This may look strange as the conversion was to make the expression a reference
  6490. // but a value type allocated on the stack is a reference even without the type
  6491. // being marked as such.
  6492. ctx->type.dataType.MakeReference(IsVariableOnHeap(ctx->type.stackOffset));
  6493. }
  6494. if (to.IsReadOnly())
  6495. {
  6496. // This doesn't cost anything
  6497. ctx->type.dataType.MakeReadOnly(true);
  6498. }
  6499. if (!to.IsReadOnly() && ctx->type.dataType.IsReadOnly())
  6500. {
  6501. // A const object can be converted to a non-const object through a copy
  6502. if (allowObjectConstruct || convType == asIC_EXPLICIT_VAL_CAST)
  6503. {
  6504. ctx->type.dataType.MakeReadOnly(false);
  6505. if (generateCode)
  6506. {
  6507. // Make a temporary copy of the object in order to make it non-const
  6508. PrepareTemporaryVariable(node, ctx);
  6509. }
  6510. // Add the cost for the copy
  6511. cost += asCC_TO_OBJECT_CONV;
  6512. }
  6513. // String literals can be implicitly converted to temporary local variables in order to pass them to functions expecting non-const
  6514. // TODO: NEWSTRING: Should have an engine property to warn or error on this
  6515. if (ctx->type.isConstant && ctx->type.dataType.IsEqualExceptRefAndConst(engine->stringType))
  6516. {
  6517. if (generateCode)
  6518. PrepareTemporaryVariable(node, ctx);
  6519. else
  6520. {
  6521. ctx->type.dataType.MakeReadOnly(false);
  6522. ctx->type.isConstant = false;
  6523. }
  6524. // Add the cost for the copy
  6525. cost += asCC_TO_OBJECT_CONV;
  6526. }
  6527. }
  6528. }
  6529. }
  6530. }
  6531. return cost;
  6532. }
  6533. asUINT asCCompiler::ImplicitConvPrimitiveToObject(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv isExplicit, bool generateCode, bool allowObjectConstruct)
  6534. {
  6535. asCObjectType *objType = CastToObjectType(to.GetTypeInfo());
  6536. asASSERT( objType || CastToFuncdefType(to.GetTypeInfo()) );
  6537. if( !objType )
  6538. return asCC_NO_CONV;
  6539. asCArray<int> funcs;
  6540. if (objType->flags & asOBJ_VALUE)
  6541. {
  6542. // For value types the object must have a constructor that takes a single primitive argument either by value or as input reference
  6543. for (asUINT n = 0; n < objType->beh.constructors.GetLength(); n++)
  6544. {
  6545. asCScriptFunction *func = engine->scriptFunctions[objType->beh.constructors[n]];
  6546. if (func->parameterTypes.GetLength() == 1 &&
  6547. func->parameterTypes[0].IsPrimitive() &&
  6548. !(func->inOutFlags[0] & asTM_OUTREF) &&
  6549. (isExplicit == asIC_EXPLICIT_VAL_CAST || !func->IsExplicit()) )
  6550. {
  6551. funcs.PushLast(func->id);
  6552. }
  6553. }
  6554. }
  6555. else if (objType->flags & asOBJ_REF)
  6556. {
  6557. // For ref types the object must have a factory that takes a single primitive argument either by value or as input reference
  6558. for (asUINT n = 0; n < objType->beh.factories.GetLength(); n++)
  6559. {
  6560. asCScriptFunction *func = engine->scriptFunctions[objType->beh.factories[n]];
  6561. if (func->parameterTypes.GetLength() == 1 &&
  6562. func->parameterTypes[0].IsPrimitive() &&
  6563. !(func->inOutFlags[0] & asTM_OUTREF) &&
  6564. (isExplicit == asIC_EXPLICIT_VAL_CAST || !func->IsExplicit()))
  6565. {
  6566. funcs.PushLast(func->id);
  6567. }
  6568. }
  6569. }
  6570. if( funcs.GetLength() == 0 )
  6571. return asCC_NO_CONV;
  6572. // Check if it is possible to choose a best match
  6573. asCExprContext arg(engine);
  6574. arg.type = ctx->type;
  6575. arg.exprNode = ctx->exprNode; // Use the same node for compiler messages
  6576. asCArray<asCExprContext*> args;
  6577. args.PushLast(&arg);
  6578. asUINT cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, 0, 0, 0, objType, false, true, false);
  6579. if( funcs.GetLength() != 1 )
  6580. return asCC_NO_CONV;
  6581. if( !generateCode )
  6582. {
  6583. ctx->type.Set(to);
  6584. return cost;
  6585. }
  6586. // TODO: clean up: This part is similar to CompileConstructCall(). It should be put in a common function
  6587. // Clear the type of ctx, as the type is moved to the arg
  6588. ctx->type.SetDummy();
  6589. // Value types and script types are allocated through the constructor
  6590. asCExprValue tempObj;
  6591. bool onHeap = false;
  6592. if (!(objType->flags & asOBJ_REF))
  6593. {
  6594. tempObj.dataType = to;
  6595. tempObj.stackOffset = (short)AllocateVariable(to, true);
  6596. tempObj.dataType.MakeReference(true);
  6597. tempObj.isTemporary = true;
  6598. tempObj.isVariable = true;
  6599. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  6600. // Push the address of the object on the stack
  6601. if (onHeap)
  6602. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  6603. }
  6604. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  6605. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  6606. if( !(objType->flags & asOBJ_REF) )
  6607. {
  6608. // If the object is allocated on the stack, then call the constructor as a normal function
  6609. if( onHeap )
  6610. {
  6611. int offset = 0;
  6612. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  6613. for( asUINT n = 0; n < args.GetLength(); n++ )
  6614. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  6615. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  6616. }
  6617. else
  6618. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6619. PerformFunctionCall(funcs[0], ctx, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  6620. // Add tag that the object has been initialized
  6621. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  6622. // The constructor doesn't return anything,
  6623. // so we have to manually inform the type of
  6624. // the return value
  6625. ctx->type = tempObj;
  6626. if( !onHeap )
  6627. ctx->type.dataType.MakeReference(false);
  6628. // Push the address of the object on the stack again
  6629. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6630. }
  6631. else
  6632. {
  6633. // Call the factory to create the reference type
  6634. PerformFunctionCall(funcs[0], ctx, false, &args);
  6635. // Make another pass to make sure the result has the correct handle and reference settings
  6636. ImplicitConversion(ctx, to, node, isExplicit, generateCode, allowObjectConstruct);
  6637. }
  6638. return cost;
  6639. }
  6640. void asCCompiler::ImplicitConversionConstant(asCExprContext *from, const asCDataType &to, asCScriptNode *node, EImplicitConv convType)
  6641. {
  6642. asASSERT(from->type.isConstant);
  6643. // TODO: node should be the node of the value that is
  6644. // converted (not the operator that provokes the implicit
  6645. // conversion)
  6646. // If the base type is correct there is no more to do
  6647. if( to.IsEqualExceptRefAndConst(from->type.dataType) ) return;
  6648. // References cannot be constants
  6649. if( from->type.dataType.IsReference() ) return;
  6650. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1 && !to.IsEnumType()) ||
  6651. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  6652. {
  6653. if( from->type.dataType.IsFloatType() ||
  6654. from->type.dataType.IsDoubleType() ||
  6655. from->type.dataType.IsUnsignedType() ||
  6656. from->type.dataType.IsIntegerType() )
  6657. {
  6658. asCDataType targetDt;
  6659. if (to.IsEnumType())
  6660. targetDt = to;
  6661. else
  6662. targetDt = asCDataType::CreatePrimitive(ttInt, true);
  6663. // Transform the value
  6664. // Float constants can be implicitly converted to int
  6665. if( from->type.dataType.IsFloatType() )
  6666. {
  6667. float fc = from->type.GetConstantF();
  6668. int ic = int(fc);
  6669. if( float(ic) != fc )
  6670. {
  6671. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6672. }
  6673. from->type.SetConstantDW(targetDt, ic);
  6674. }
  6675. // Double constants can be implicitly converted to int
  6676. else if( from->type.dataType.IsDoubleType() )
  6677. {
  6678. double fc = from->type.GetConstantD();
  6679. int ic = int(fc);
  6680. if( double(ic) != fc )
  6681. {
  6682. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6683. }
  6684. from->type.SetConstantDW(targetDt, ic);
  6685. }
  6686. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6687. {
  6688. // Verify that it is possible to convert to signed without getting negative
  6689. if( from->type.dataType.GetSizeInMemoryBytes() == 4 &&
  6690. int(from->type.GetConstantDW()) < 0 &&
  6691. convType != asIC_EXPLICIT_VAL_CAST &&
  6692. node != 0 )
  6693. Warning(TXT_CHANGE_SIGN, node);
  6694. // Convert to 32bit
  6695. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6696. from->type.SetConstantDW(targetDt, from->type.GetConstantB());
  6697. else if (from->type.dataType.GetSizeInMemoryBytes() == 2)
  6698. from->type.SetConstantDW(targetDt, from->type.GetConstantW());
  6699. else
  6700. from->type.dataType = targetDt;
  6701. }
  6702. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6703. {
  6704. if (asQWORD(from->type.GetConstantQW()) >> 31)
  6705. if (convType != asIC_EXPLICIT_VAL_CAST && node) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6706. // Convert to 32bit
  6707. from->type.SetConstantDW(targetDt, int(from->type.GetConstantQW()));
  6708. }
  6709. else if (from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2)
  6710. {
  6711. if (int(from->type.GetConstantQW()) != asINT64(from->type.GetConstantQW()))
  6712. if (convType != asIC_EXPLICIT_VAL_CAST && node) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6713. // Convert to 32bit
  6714. from->type.SetConstantDW(targetDt, int(from->type.GetConstantQW()));
  6715. }
  6716. else if (from->type.dataType.IsIntegerType() &&
  6717. from->type.dataType.GetSizeInMemoryBytes() < 4)
  6718. {
  6719. // Convert to 32bit
  6720. if (from->type.dataType.GetSizeInMemoryBytes() == 1)
  6721. from->type.SetConstantDW(targetDt, (asINT8)from->type.GetConstantB());
  6722. else if (from->type.dataType.GetSizeInMemoryBytes() == 2)
  6723. from->type.SetConstantDW(targetDt, (asINT16)from->type.GetConstantW());
  6724. }
  6725. else
  6726. {
  6727. // Only int32 and enums should come here and as these are 32bit
  6728. // already nothing needs to be done except set the target type
  6729. asASSERT((from->type.dataType.GetTokenType() == ttInt ||
  6730. from->type.dataType.IsEnumType()) &&
  6731. from->type.dataType.GetSizeInMemoryBytes() == 4);
  6732. from->type.dataType = targetDt;
  6733. }
  6734. }
  6735. // Check if a downsize is necessary
  6736. if( to.IsIntegerType() &&
  6737. from->type.dataType.IsIntegerType() &&
  6738. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  6739. {
  6740. // Verify if it is possible
  6741. if( to.GetSizeInMemoryBytes() == 1 )
  6742. {
  6743. if( asINT8(from->type.GetConstantDW()) != int(from->type.GetConstantDW()) )
  6744. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6745. from->type.SetConstantB(asCDataType::CreatePrimitive(to.GetTokenType(), true), asINT8(from->type.GetConstantDW()));
  6746. }
  6747. else if( to.GetSizeInMemoryBytes() == 2 )
  6748. {
  6749. if( asINT16(from->type.GetConstantDW()) != int(from->type.GetConstantDW()) )
  6750. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6751. from->type.SetConstantW(asCDataType::CreatePrimitive(to.GetTokenType(), true), asINT16(from->type.GetConstantDW()));
  6752. }
  6753. }
  6754. }
  6755. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  6756. {
  6757. // Float constants can be implicitly converted to int
  6758. if( from->type.dataType.IsFloatType() )
  6759. {
  6760. float fc = from->type.GetConstantF();
  6761. asINT64 ic = asINT64(fc);
  6762. if( float(ic) != fc )
  6763. {
  6764. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6765. }
  6766. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), ic);
  6767. }
  6768. // Double constants can be implicitly converted to int
  6769. else if( from->type.dataType.IsDoubleType() )
  6770. {
  6771. double fc = from->type.GetConstantD();
  6772. asINT64 ic = asINT64(fc);
  6773. if( double(ic) != fc )
  6774. {
  6775. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6776. }
  6777. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), ic);
  6778. }
  6779. else if( from->type.dataType.IsUnsignedType() )
  6780. {
  6781. // Convert to 64bit
  6782. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6783. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), from->type.GetConstantB());
  6784. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6785. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), from->type.GetConstantW());
  6786. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6787. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), from->type.GetConstantDW());
  6788. else if( from->type.dataType.GetSizeInMemoryBytes() == 8 )
  6789. {
  6790. if( asINT64(from->type.GetConstantQW()) < 0 )
  6791. {
  6792. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6793. }
  6794. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  6795. }
  6796. }
  6797. else if( from->type.dataType.IsIntegerType() )
  6798. {
  6799. // Convert to 64bit
  6800. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6801. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), (asINT8)from->type.GetConstantB());
  6802. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6803. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), (asINT16)from->type.GetConstantW());
  6804. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6805. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), (int)from->type.GetConstantDW());
  6806. }
  6807. }
  6808. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  6809. {
  6810. if( from->type.dataType.IsFloatType() )
  6811. {
  6812. float fc = from->type.GetConstantF();
  6813. // Some compilers set the value to 0 when converting a negative float to unsigned int.
  6814. // To maintain a consistent behaviour across compilers we convert to int first.
  6815. asUINT uic = asUINT(int(fc));
  6816. if( float(uic) != fc )
  6817. {
  6818. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6819. }
  6820. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), uic);
  6821. // Try once more, in case of a smaller type
  6822. ImplicitConversionConstant(from, to, node, convType);
  6823. }
  6824. else if( from->type.dataType.IsDoubleType() )
  6825. {
  6826. double fc = from->type.GetConstantD();
  6827. // Some compilers set the value to 0 when converting a negative double to unsigned int.
  6828. // To maintain a consistent behaviour across compilers we convert to int first.
  6829. asUINT uic = asUINT(int(fc));
  6830. if( double(uic) != fc )
  6831. {
  6832. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6833. }
  6834. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), uic);
  6835. // Try once more, in case of a smaller type
  6836. ImplicitConversionConstant(from, to, node, convType);
  6837. }
  6838. else if( from->type.dataType.IsIntegerType() )
  6839. {
  6840. // Verify that it is possible to convert to unsigned without loosing negative
  6841. if( (from->type.dataType.GetSizeInMemoryBytes() > 4 && asINT64(from->type.GetConstantQW()) < 0) ||
  6842. (from->type.dataType.GetSizeInMemoryBytes() == 4 && int(from->type.GetConstantDW()) < 0) ||
  6843. (from->type.dataType.GetSizeInMemoryBytes() == 2 && asINT16(from->type.GetConstantW()) < 0) ||
  6844. (from->type.dataType.GetSizeInMemoryBytes() == 1 && asINT8(from->type.GetConstantB()) < 0))
  6845. {
  6846. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6847. }
  6848. // Check if any data is lost
  6849. if( from->type.dataType.GetSizeInMemoryBytes() > 4 && (from->type.GetConstantQW() >> 32) != 0 && (from->type.GetConstantQW() >> 32) != 0xFFFFFFFF )
  6850. {
  6851. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6852. }
  6853. // Convert to 32bit
  6854. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6855. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (asINT8)from->type.GetConstantB());
  6856. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6857. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (asINT16)from->type.GetConstantW());
  6858. else if (from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6859. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (int)from->type.GetConstantDW());
  6860. else
  6861. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (int)(asINT64)from->type.GetConstantQW());
  6862. // Try once more, in case of a smaller type
  6863. ImplicitConversionConstant(from, to, node, convType);
  6864. }
  6865. else if( from->type.dataType.IsUnsignedType() &&
  6866. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  6867. {
  6868. // Convert to 32bit
  6869. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6870. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), from->type.GetConstantB());
  6871. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6872. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), from->type.GetConstantW());
  6873. // Try once more, in case of a smaller type
  6874. ImplicitConversionConstant(from, to, node, convType);
  6875. }
  6876. else if( from->type.dataType.IsUnsignedType() &&
  6877. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  6878. {
  6879. // Verify if it is possible
  6880. if( to.GetSizeInMemoryBytes() == 1 )
  6881. {
  6882. if( (from->type.dataType.GetSizeInMemoryBytes() == 2 && asBYTE(from->type.GetConstantW()) != from->type.GetConstantW()) ||
  6883. (from->type.dataType.GetSizeInMemoryBytes() == 4 && asBYTE(from->type.GetConstantDW()) != from->type.GetConstantDW()) ||
  6884. (from->type.dataType.GetSizeInMemoryBytes() == 8 && asBYTE(from->type.GetConstantQW()) != from->type.GetConstantQW()) )
  6885. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6886. if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6887. from->type.SetConstantB(asCDataType::CreatePrimitive(to.GetTokenType(), true), asBYTE(from->type.GetConstantW()));
  6888. else if (from->type.dataType.GetSizeInMemoryBytes() == 4)
  6889. from->type.SetConstantB(asCDataType::CreatePrimitive(to.GetTokenType(), true), asBYTE(from->type.GetConstantDW()));
  6890. else if (from->type.dataType.GetSizeInMemoryBytes() == 8)
  6891. from->type.SetConstantB(asCDataType::CreatePrimitive(to.GetTokenType(), true), asBYTE(from->type.GetConstantQW()));
  6892. }
  6893. else if( to.GetSizeInMemoryBytes() == 2 )
  6894. {
  6895. if( (from->type.dataType.GetSizeInMemoryBytes() == 4 && asWORD(from->type.GetConstantDW()) != from->type.GetConstantDW()) ||
  6896. (from->type.dataType.GetSizeInMemoryBytes() == 8 && asWORD(from->type.GetConstantQW()) != from->type.GetConstantQW()) )
  6897. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6898. if (from->type.dataType.GetSizeInMemoryBytes() == 4)
  6899. from->type.SetConstantW(asCDataType::CreatePrimitive(to.GetTokenType(), true), asWORD(from->type.GetConstantDW()));
  6900. else if (from->type.dataType.GetSizeInMemoryBytes() == 8)
  6901. from->type.SetConstantW(asCDataType::CreatePrimitive(to.GetTokenType(), true), asWORD(from->type.GetConstantQW()));
  6902. }
  6903. else if (to.GetSizeInMemoryBytes() == 4)
  6904. {
  6905. if( asDWORD(from->type.GetConstantQW()) != from->type.GetConstantQW())
  6906. if (convType != asIC_EXPLICIT_VAL_CAST && node) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6907. from->type.SetConstantDW(asCDataType::CreatePrimitive(to.GetTokenType(), true), asDWORD(from->type.GetConstantQW()));
  6908. }
  6909. }
  6910. }
  6911. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  6912. {
  6913. if( from->type.dataType.IsFloatType() )
  6914. {
  6915. float fc = from->type.GetConstantF();
  6916. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  6917. asQWORD uic = asQWORD(asINT64(fc));
  6918. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  6919. // MSVC6 doesn't support this conversion
  6920. if( float(uic) != fc )
  6921. {
  6922. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6923. }
  6924. #endif
  6925. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), uic);
  6926. }
  6927. else if( from->type.dataType.IsDoubleType() )
  6928. {
  6929. double fc = from->type.GetConstantD();
  6930. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  6931. asQWORD uic = asQWORD(asINT64(fc));
  6932. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  6933. // MSVC6 doesn't support this conversion
  6934. if( double(uic) != fc )
  6935. {
  6936. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6937. }
  6938. #endif
  6939. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), uic);
  6940. }
  6941. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6942. {
  6943. // Convert to 64bit
  6944. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6945. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), (asINT64)(asINT8)from->type.GetConstantB());
  6946. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6947. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), (asINT64)(asINT16)from->type.GetConstantW());
  6948. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6949. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), (asINT64)(int)from->type.GetConstantDW());
  6950. // Verify that it is possible to convert to unsigned without loosing negative
  6951. if( asINT64(from->type.GetConstantQW()) < 0 )
  6952. {
  6953. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6954. }
  6955. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  6956. }
  6957. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6958. {
  6959. // Verify that it is possible to convert to unsigned without loosing negative
  6960. if( asINT64(from->type.GetConstantQW()) < 0 )
  6961. {
  6962. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6963. }
  6964. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  6965. }
  6966. else if( from->type.dataType.IsUnsignedType() )
  6967. {
  6968. // Convert to 64bit
  6969. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6970. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), from->type.GetConstantB());
  6971. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6972. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), from->type.GetConstantW());
  6973. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6974. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), from->type.GetConstantDW());
  6975. }
  6976. }
  6977. else if( to.IsFloatType() )
  6978. {
  6979. if( from->type.dataType.IsDoubleType() )
  6980. {
  6981. double ic = from->type.GetConstantD();
  6982. float fc = float(ic);
  6983. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6984. }
  6985. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6986. {
  6987. // Must properly convert value in case the from value is smaller
  6988. int ic;
  6989. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6990. ic = (asINT8)from->type.GetConstantB();
  6991. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6992. ic = (asINT16)from->type.GetConstantW();
  6993. else
  6994. ic = (int)from->type.GetConstantDW();
  6995. float fc = float(ic);
  6996. if( int(fc) != ic )
  6997. {
  6998. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6999. }
  7000. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7001. }
  7002. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  7003. {
  7004. float fc = float(asINT64(from->type.GetConstantQW()));
  7005. if( asINT64(fc) != asINT64(from->type.GetConstantQW()) )
  7006. {
  7007. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7008. }
  7009. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7010. }
  7011. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  7012. {
  7013. // Must properly convert value in case the from value is smaller
  7014. unsigned int uic;
  7015. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  7016. uic = from->type.GetConstantB();
  7017. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  7018. uic = from->type.GetConstantW();
  7019. else
  7020. uic = from->type.GetConstantDW();
  7021. float fc = float(uic);
  7022. if( (unsigned int)(fc) != uic )
  7023. {
  7024. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7025. }
  7026. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7027. }
  7028. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  7029. {
  7030. float fc = float((asINT64)from->type.GetConstantQW());
  7031. if( asQWORD(fc) != from->type.GetConstantQW())
  7032. {
  7033. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7034. }
  7035. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7036. }
  7037. }
  7038. else if( to.IsDoubleType() )
  7039. {
  7040. if( from->type.dataType.IsFloatType() )
  7041. {
  7042. float ic = from->type.GetConstantF();
  7043. double fc = double(ic);
  7044. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7045. }
  7046. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  7047. {
  7048. // Must properly convert value in case the from value is smaller
  7049. int ic;
  7050. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  7051. ic = (asINT8)from->type.GetConstantB();
  7052. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  7053. ic = (asINT16)from->type.GetConstantW();
  7054. else
  7055. ic = (int)from->type.GetConstantDW();
  7056. double fc = double(ic);
  7057. if( int(fc) != ic )
  7058. {
  7059. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7060. }
  7061. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7062. }
  7063. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  7064. {
  7065. double fc = double(asINT64(from->type.GetConstantQW()));
  7066. if( asINT64(fc) != asINT64(from->type.GetConstantQW()) )
  7067. {
  7068. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7069. }
  7070. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7071. }
  7072. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  7073. {
  7074. // Must properly convert value in case the from value is smaller
  7075. unsigned int uic;
  7076. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  7077. uic = from->type.GetConstantB();
  7078. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  7079. uic = from->type.GetConstantW();
  7080. else
  7081. uic = from->type.GetConstantDW();
  7082. double fc = double(uic);
  7083. if( (unsigned int)(fc) != uic )
  7084. {
  7085. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7086. }
  7087. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7088. }
  7089. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  7090. {
  7091. double fc = double((asINT64)from->type.GetConstantQW());
  7092. if( asQWORD(fc) != from->type.GetConstantQW())
  7093. {
  7094. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  7095. }
  7096. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  7097. }
  7098. }
  7099. }
  7100. int asCCompiler::DoAssignment(asCExprContext *ctx, asCExprContext *lctx, asCExprContext *rctx, asCScriptNode *lexpr, asCScriptNode *rexpr, eTokenType op, asCScriptNode *opNode)
  7101. {
  7102. // Don't allow any operators on expressions that take address of class method
  7103. // If methodName is set but the type is not an object, then it is a global function
  7104. if( lctx->methodName != "" || rctx->IsClassMethod() )
  7105. {
  7106. Error(TXT_INVALID_OP_ON_METHOD, opNode);
  7107. return -1;
  7108. }
  7109. // Implicit handle types should always be treated as handles in assignments
  7110. if (lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE) )
  7111. {
  7112. lctx->type.dataType.MakeHandle(true);
  7113. lctx->type.isExplicitHandle = true;
  7114. }
  7115. // Urho3D: if there is a handle type, and it does not have an overloaded assignment operator, convert to an explicit handle
  7116. // for scripting convenience. (For the Urho3D handle types, value assignment is not supported)
  7117. if (lctx->type.dataType.IsObjectHandle() && !lctx->type.dataType.IsTemplate() && !lctx->type.isExplicitHandle &&
  7118. (!lctx->type.dataType.GetBehaviour() || !lctx->type.dataType.GetBehaviour()->copy))
  7119. lctx->type.isExplicitHandle = true;
  7120. // If the left hand expression is a property accessor, then that should be used
  7121. // to do the assignment instead of the ordinary operator. The exception is when
  7122. // the property accessor is for a handle property, and the operation is a value
  7123. // assignment.
  7124. if( (lctx->property_get || lctx->property_set) &&
  7125. !(lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle) )
  7126. {
  7127. if( op != ttAssignment )
  7128. {
  7129. // Generate the code for the compound assignment, i.e. get the value, apply operator, then set the value
  7130. return ProcessPropertyGetSetAccessor(ctx, lctx, rctx, op, opNode);
  7131. }
  7132. // It is not allowed to do a handle assignment on a property
  7133. // accessor that doesn't take a handle in the set accessor.
  7134. if( lctx->property_set && lctx->type.isExplicitHandle )
  7135. {
  7136. // set_opIndex has 2 arguments, where as normal setters have only 1
  7137. asCArray<asCDataType>& parameterTypes =
  7138. builder->GetFunctionDescription(lctx->property_set)->parameterTypes;
  7139. if( !parameterTypes[parameterTypes.GetLength() - 1].IsObjectHandle() )
  7140. {
  7141. // Process the property to free the memory
  7142. ProcessPropertySetAccessor(lctx, rctx, opNode);
  7143. Error(TXT_HANDLE_ASSIGN_ON_NON_HANDLE_PROP, opNode);
  7144. return -1;
  7145. }
  7146. }
  7147. MergeExprBytecodeAndType(ctx, lctx);
  7148. return ProcessPropertySetAccessor(ctx, rctx, opNode);
  7149. }
  7150. else if( lctx->property_get && lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  7151. {
  7152. // Get the handle to the object that will be used for the value assignment
  7153. if( ProcessPropertyGetAccessor(lctx, opNode) < 0 )
  7154. return -1;
  7155. }
  7156. if( lctx->type.dataType.IsPrimitive() )
  7157. {
  7158. if( !lctx->type.isLValue )
  7159. {
  7160. Error(TXT_NOT_LVALUE, lexpr);
  7161. return -1;
  7162. }
  7163. if( op != ttAssignment )
  7164. {
  7165. // Compute the operator before the assignment
  7166. asCExprValue lvalue = lctx->type;
  7167. if( lctx->type.isTemporary && !lctx->type.isVariable )
  7168. {
  7169. // The temporary variable must not be freed until the
  7170. // assignment has been performed. lvalue still holds
  7171. // the information about the temporary variable
  7172. lctx->type.isTemporary = false;
  7173. }
  7174. asCExprContext o(engine);
  7175. CompileOperator(opNode, lctx, rctx, &o);
  7176. MergeExprBytecode(rctx, &o);
  7177. rctx->type = o.type;
  7178. // Convert the rvalue to the right type and validate it
  7179. PrepareForAssignment(&lvalue.dataType, rctx, rexpr, false);
  7180. MergeExprBytecode(ctx, rctx);
  7181. lctx->type = lvalue;
  7182. // The lvalue continues the same, either it was a variable, or a reference in the register
  7183. }
  7184. else
  7185. {
  7186. // Convert the rvalue to the right type and validate it
  7187. PrepareForAssignment(&lctx->type.dataType, rctx, rexpr, false, lctx);
  7188. MergeExprBytecode(ctx, rctx);
  7189. MergeExprBytecode(ctx, lctx);
  7190. }
  7191. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  7192. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  7193. ctx->type = lctx->type;
  7194. }
  7195. else if( lctx->type.isExplicitHandle )
  7196. {
  7197. if( !lctx->type.isLValue )
  7198. {
  7199. Error(TXT_NOT_LVALUE, lexpr);
  7200. return -1;
  7201. }
  7202. // Object handles don't have any compound assignment operators
  7203. if( op != ttAssignment )
  7204. {
  7205. asCString str;
  7206. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  7207. Error(str, lexpr);
  7208. return -1;
  7209. }
  7210. if( lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  7211. {
  7212. // The object is a value type but that should be treated as a handle
  7213. // Make sure the right hand value is a handle
  7214. if( !rctx->type.isExplicitHandle &&
  7215. !(rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE)) )
  7216. {
  7217. // Function names can be considered handles already
  7218. if( rctx->methodName == "" )
  7219. {
  7220. asCDataType dt = rctx->type.dataType;
  7221. dt.MakeHandle(true);
  7222. dt.MakeReference(false);
  7223. PrepareArgument(&dt, rctx, rexpr, true, asTM_INREF);
  7224. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  7225. {
  7226. asCString str;
  7227. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  7228. Error(str, rexpr);
  7229. return -1;
  7230. }
  7231. }
  7232. if (!rctx->type.dataType.IsObjectHandle() && !rctx->type.dataType.SupportHandles())
  7233. {
  7234. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, rexpr);
  7235. return -1;
  7236. }
  7237. // Mark the right hand expression as explicit handle even if the user didn't do it, otherwise
  7238. // the code for moving the argument to the stack may not know to correctly handle the argument type
  7239. // in case of variable parameter type.
  7240. rctx->type.isExplicitHandle = true;
  7241. }
  7242. if( CompileOverloadedDualOperator(opNode, lctx, rctx, false, ctx, true) )
  7243. {
  7244. // An overloaded assignment operator was found (or a compilation error occured)
  7245. return 0;
  7246. }
  7247. // The object must implement the opAssign method
  7248. asCString msg;
  7249. msg.Format(TXT_NO_APPROPRIATE_OPHNDLASSIGN_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  7250. Error(msg.AddressOf(), opNode);
  7251. return -1;
  7252. }
  7253. else
  7254. {
  7255. asCDataType dt = lctx->type.dataType;
  7256. dt.MakeReference(false);
  7257. PrepareArgument(&dt, rctx, rexpr, false, asTM_INREF , true);
  7258. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  7259. {
  7260. asCString str;
  7261. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  7262. Error(str, rexpr);
  7263. return -1;
  7264. }
  7265. MergeExprBytecode(ctx, rctx);
  7266. MergeExprBytecode(ctx, lctx);
  7267. if(!rctx->type.isRefSafe)
  7268. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  7269. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  7270. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  7271. ctx->type = lctx->type;
  7272. // After the handle assignment the original handle is left on the stack
  7273. ctx->type.dataType.MakeReference(false);
  7274. }
  7275. }
  7276. else // if( lctx->type.dataType.IsObject() )
  7277. {
  7278. // The lvalue reference may be marked as a temporary, if for example
  7279. // it was originated as a handle returned from a function. In such
  7280. // cases it must be possible to assign values to it anyway.
  7281. if( lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  7282. {
  7283. // Convert the handle to a object reference
  7284. asCDataType to;
  7285. to = lctx->type.dataType;
  7286. to.MakeHandle(false);
  7287. ImplicitConversion(lctx, to, lexpr, asIC_IMPLICIT_CONV);
  7288. lctx->type.isLValue = true; // Handle may not have been an lvalue, but the dereferenced object is
  7289. }
  7290. // Check for overloaded assignment operator
  7291. if( CompileOverloadedDualOperator(opNode, lctx, rctx, false, ctx) )
  7292. {
  7293. // An overloaded assignment operator was found (or a compilation error occured)
  7294. return 0;
  7295. }
  7296. // No registered operator was found. In case the operation is a direct
  7297. // assignment and the rvalue is the same type as the lvalue, then we can
  7298. // still use the byte-for-byte copy to do the assignment
  7299. if( op != ttAssignment )
  7300. {
  7301. asCString str;
  7302. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  7303. Error(str, lexpr);
  7304. return -1;
  7305. }
  7306. // If the left hand expression is simple, i.e. without any
  7307. // function calls or allocations of memory, then we can avoid
  7308. // doing a copy of the right hand expression (done by PrepareArgument).
  7309. // Instead the reference to the value can be placed directly on the
  7310. // stack.
  7311. //
  7312. // This optimization should only be done for value types, where
  7313. // the application developer is responsible for making the
  7314. // implementation safe against unwanted destruction of the input
  7315. // reference before the time.
  7316. bool simpleExpr = (lctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) && lctx->bc.IsSimpleExpression();
  7317. // Implicitly convert the rvalue to the type of the lvalue
  7318. bool needConversion = false;
  7319. if( !lctx->type.dataType.IsEqualExceptRefAndConst(rctx->type.dataType) )
  7320. needConversion = true;
  7321. if( !simpleExpr || needConversion )
  7322. {
  7323. if( rctx->type.dataType.IsObjectHandle() && !rctx->type.isExplicitHandle &&
  7324. !lctx->type.dataType.IsObjectHandle() && rctx->type.dataType.GetTypeInfo() == lctx->type.dataType.GetTypeInfo() )
  7325. {
  7326. // Make the conversion from handle to non-handle without creating
  7327. // a copy of the object (otherwise done by PrepareArgument)
  7328. asCDataType dt = rctx->type.dataType;
  7329. dt.MakeHandle(false);
  7330. ImplicitConversion(rctx, dt, rexpr, asIC_IMPLICIT_CONV);
  7331. needConversion = false;
  7332. }
  7333. asCDataType dt = lctx->type.dataType;
  7334. dt.MakeReference(true);
  7335. // A funcdef can be accessed by ref, but only as read-only
  7336. if( dt.IsFuncdef() && !dt.IsObjectHandle() )
  7337. dt.MakeReadOnly(true);
  7338. int r = PrepareArgument(&dt, rctx, rexpr, true, 1, !needConversion);
  7339. if( r < 0 )
  7340. return -1;
  7341. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  7342. {
  7343. asCString str;
  7344. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  7345. Error(str, rexpr);
  7346. return -1;
  7347. }
  7348. }
  7349. else
  7350. {
  7351. // Process any property accessor first, before placing the final reference on the stack
  7352. if( ProcessPropertyGetAccessor(rctx, rexpr) < 0 )
  7353. return -1;
  7354. if( rctx->type.dataType.IsReference() && (!(rctx->type.isVariable || rctx->type.isTemporary) || IsVariableOnHeap(rctx->type.stackOffset)) )
  7355. rctx->bc.Instr(asBC_RDSPtr);
  7356. }
  7357. MergeExprBytecode(ctx, rctx);
  7358. MergeExprBytecode(ctx, lctx);
  7359. if( !simpleExpr || needConversion )
  7360. {
  7361. if( !rctx->type.isRefSafe && (rctx->type.isVariable || rctx->type.isTemporary) )
  7362. {
  7363. if( !IsVariableOnHeap(rctx->type.stackOffset) )
  7364. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  7365. // as the value allocated on the stack is guaranteed to be safe.
  7366. // The bytecode optimizer should be able to determine this and optimize away the VAR + GETREF
  7367. ctx->bc.InstrWORD(asBC_GETREF, AS_PTR_SIZE);
  7368. else
  7369. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  7370. }
  7371. }
  7372. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  7373. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  7374. ctx->type = lctx->type;
  7375. }
  7376. return 0;
  7377. }
  7378. int asCCompiler::CompileAssignment(asCScriptNode *expr, asCExprContext *ctx)
  7379. {
  7380. asASSERT(expr->nodeType == snAssignment);
  7381. asCScriptNode *lexpr = expr->firstChild;
  7382. if( lexpr->next )
  7383. {
  7384. // Compile the two expression terms
  7385. asCExprContext lctx(engine), rctx(engine);
  7386. int rr = CompileAssignment(lexpr->next->next, &rctx);
  7387. int lr = CompileCondition(lexpr, &lctx);
  7388. if( lr >= 0 && rr >= 0 )
  7389. return DoAssignment(ctx, &lctx, &rctx, lexpr, lexpr->next->next, lexpr->next->tokenType, lexpr->next);
  7390. // Since the operands failed, the assignment was not computed
  7391. ctx->type.SetDummy();
  7392. return -1;
  7393. }
  7394. return CompileCondition(lexpr, ctx);
  7395. }
  7396. int asCCompiler::CompileCondition(asCScriptNode *expr, asCExprContext *ctx)
  7397. {
  7398. asCExprValue ctype;
  7399. // Compile the conditional expression
  7400. asCScriptNode *cexpr = expr->firstChild;
  7401. if( cexpr->next )
  7402. {
  7403. //-------------------------------
  7404. // Compile the condition
  7405. asCExprContext e(engine);
  7406. int r = CompileExpression(cexpr, &e);
  7407. if( r < 0 )
  7408. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  7409. // Allow value types to be converted to bool using 'bool opImplConv()'
  7410. if( e.type.dataType.GetTypeInfo() && (e.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  7411. ImplicitConversion(&e, asCDataType::CreatePrimitive(ttBool, false), cexpr, asIC_IMPLICIT_CONV);
  7412. if( r >= 0 && !e.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  7413. {
  7414. Error(TXT_EXPR_MUST_BE_BOOL, cexpr);
  7415. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  7416. }
  7417. ctype = e.type;
  7418. if( ProcessPropertyGetAccessor(&e, cexpr) < 0)
  7419. return -1;
  7420. if( e.type.dataType.IsReference() ) ConvertToVariable(&e);
  7421. ProcessDeferredParams(&e);
  7422. //-------------------------------
  7423. // Compile the left expression
  7424. asCExprContext le(engine);
  7425. int lr = CompileAssignment(cexpr->next, &le);
  7426. // Resolve any function names already
  7427. DetermineSingleFunc(&le, cexpr->next);
  7428. //-------------------------------
  7429. // Compile the right expression
  7430. asCExprContext re(engine);
  7431. int rr = CompileAssignment(cexpr->next->next, &re);
  7432. DetermineSingleFunc(&re, cexpr->next->next);
  7433. if (lr >= 0 && rr >= 0)
  7434. {
  7435. // Don't allow any operators on expressions that take address of class method
  7436. if (le.IsClassMethod() || re.IsClassMethod())
  7437. {
  7438. Error(TXT_INVALID_OP_ON_METHOD, expr);
  7439. return -1;
  7440. }
  7441. if (ProcessPropertyGetAccessor(&le, cexpr->next) < 0)
  7442. return -1;
  7443. if (ProcessPropertyGetAccessor(&re, cexpr->next->next) < 0)
  7444. return -1;
  7445. bool isExplicitHandle = le.type.isExplicitHandle || re.type.isExplicitHandle;
  7446. // Allow an anonymous initialization list to be converted to the type in the other condition
  7447. if (le.IsAnonymousInitList() && re.type.dataType.GetBehaviour() && re.type.dataType.GetBehaviour()->listFactory)
  7448. {
  7449. asCDataType to = re.type.dataType;
  7450. to.MakeReference(false);
  7451. to.MakeReadOnly(false);
  7452. ImplicitConversion(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  7453. }
  7454. else if (re.IsAnonymousInitList() && le.type.dataType.GetBehaviour() && le.type.dataType.GetBehaviour()->listFactory)
  7455. {
  7456. asCDataType to = le.type.dataType;
  7457. to.MakeReference(false);
  7458. to.MakeReadOnly(false);
  7459. ImplicitConversion(&re, to, cexpr->next->next, asIC_IMPLICIT_CONV);
  7460. }
  7461. if (le.IsAnonymousInitList())
  7462. {
  7463. Error(TXT_CANNOT_RESOLVE_AUTO, cexpr->next);
  7464. return -1;
  7465. }
  7466. else if (re.IsAnonymousInitList())
  7467. {
  7468. Error(TXT_CANNOT_RESOLVE_AUTO, cexpr->next->next);
  7469. return -1;
  7470. }
  7471. // Try to perform an implicit cast to make the two operands of the same type
  7472. // Choose the conversion that is the least costly
  7473. if (le.type.dataType != re.type.dataType)
  7474. {
  7475. asCExprContext tmp(engine);
  7476. tmp.type = le.type;
  7477. tmp.type.dataType.MakeReference(false);
  7478. asUINT costAtoB = ImplicitConversion(&tmp, re.type.dataType, cexpr->next, asIC_IMPLICIT_CONV, false);
  7479. if (!tmp.type.dataType.IsEqualExceptRef(re.type.dataType))
  7480. costAtoB = 0xFFFFFFFF;
  7481. tmp.type = re.type;
  7482. tmp.type.dataType.MakeReference(false);
  7483. asUINT costBtoA = ImplicitConversion(&tmp, le.type.dataType, cexpr->next->next, asIC_IMPLICIT_CONV, false);
  7484. if (!tmp.type.dataType.IsEqualExceptRef(le.type.dataType))
  7485. costBtoA = 0xFFFFFFFF;
  7486. if (costAtoB < costBtoA && costAtoB != 0xFFFFFFFF)
  7487. {
  7488. Dereference(&le, true);
  7489. ImplicitConversion(&le, re.type.dataType, cexpr->next, asIC_IMPLICIT_CONV, true);
  7490. }
  7491. else if (costAtoB > costBtoA && costBtoA != 0xFFFFFFFF)
  7492. {
  7493. Dereference(&re, true);
  7494. ImplicitConversion(&re, le.type.dataType, cexpr->next->next, asIC_IMPLICIT_CONV, true);
  7495. }
  7496. // If the cost for conversion is the same in both directions we have an ambigious situation,
  7497. // which we do not resolve. In that case the script need to perform an explicit conversion
  7498. }
  7499. // Allow a 0 to be implicitly converted to the other type
  7500. if (le.type.isConstant && le.type.GetConstantData() == 0 && le.type.dataType.IsIntegerType())
  7501. {
  7502. asCDataType to = re.type.dataType;
  7503. to.MakeReference(false);
  7504. to.MakeReadOnly(true);
  7505. ImplicitConversionConstant(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  7506. }
  7507. else if( re.type.isConstant && re.type.GetConstantData() == 0 && re.type.dataType.IsIntegerType())
  7508. {
  7509. asCDataType to = le.type.dataType;
  7510. to.MakeReference(false);
  7511. to.MakeReadOnly(true);
  7512. ImplicitConversionConstant(&re, to, cexpr->next->next, asIC_IMPLICIT_CONV);
  7513. }
  7514. // Allow expression to be converted to handle if the other is handle
  7515. if (!le.type.dataType.IsObjectHandle() && re.type.dataType.IsObjectHandle() && le.type.dataType.GetTypeInfo() == re.type.dataType.GetTypeInfo() )
  7516. {
  7517. asCDataType dt = le.type.dataType;
  7518. dt.MakeHandle(true);
  7519. ImplicitConversion(&le, dt, cexpr->next, asIC_IMPLICIT_CONV);
  7520. }
  7521. if (!re.type.dataType.IsObjectHandle() && le.type.dataType.IsObjectHandle() && le.type.dataType.GetTypeInfo() == re.type.dataType.GetTypeInfo())
  7522. {
  7523. asCDataType dt = re.type.dataType;
  7524. dt.MakeHandle(true);
  7525. ImplicitConversion(&re, dt, cexpr->next->next, asIC_IMPLICIT_CONV);
  7526. }
  7527. // Allow either case to be converted to const @ if the other is const @
  7528. if( (le.type.dataType.IsHandleToConst() && !le.type.IsNullConstant()) || (re.type.dataType.IsHandleToConst() && !re.type.dataType.IsNullHandle()) )
  7529. {
  7530. le.type.dataType.MakeHandleToConst(true);
  7531. re.type.dataType.MakeHandleToConst(true);
  7532. }
  7533. // Make sure both expressions have the same type
  7534. if (!le.type.dataType.IsEqualExceptRefAndConst(re.type.dataType))
  7535. {
  7536. Error(TXT_BOTH_MUST_BE_SAME, expr);
  7537. return -1;
  7538. }
  7539. //---------------------------------
  7540. // Output the byte code
  7541. int afterLabel = nextLabel++;
  7542. int elseLabel = nextLabel++;
  7543. // If left expression is void, then we don't need to store the result
  7544. if( le.type.dataType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  7545. {
  7546. // Put the code for the condition expression on the output
  7547. MergeExprBytecode(ctx, &e);
  7548. // Added the branch decision
  7549. ctx->type = e.type;
  7550. ConvertToVariable(ctx);
  7551. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  7552. ctx->bc.Instr(asBC_ClrHi);
  7553. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  7554. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7555. // Add the left expression
  7556. MergeExprBytecode(ctx, &le);
  7557. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  7558. // Add the right expression
  7559. ctx->bc.Label((short)elseLabel);
  7560. MergeExprBytecode(ctx, &re);
  7561. ctx->bc.Label((short)afterLabel);
  7562. // Set the type of the result
  7563. ctx->type = le.type;
  7564. }
  7565. else if (le.type.IsNullConstant() && re.type.IsNullConstant())
  7566. {
  7567. // Special case for when both results are 'null'
  7568. // TODO: Other expressions where both results are identical literal constants can probably also be handled this way
  7569. // Put the code for the condition expression on the output
  7570. MergeExprBytecode(ctx, &e);
  7571. // Load the result into the register, but ignore the value since both paths give the same response
  7572. ctx->type = e.type;
  7573. ConvertToVariable(ctx);
  7574. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  7575. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7576. // Return a null constant
  7577. ctx->bc.Instr(asBC_PshNull);
  7578. ctx->type.SetNullConstant();
  7579. }
  7580. else
  7581. {
  7582. // Allow "(a ? b : c) = d;" and "return (a ? b : c);" (where the latter returns the reference)
  7583. //
  7584. // Restrictions for the condition to be used as lvalue:
  7585. // 1. both b and c must be of the same type and be lvalue references
  7586. // 2. neither of the expressions can have any deferred arguments
  7587. // that would have to be cleaned up after the reference
  7588. // 3. neither expression can be temporary
  7589. //
  7590. // If either expression is local, the resulting lvalue is not valid
  7591. // for return since it is not allowed to return references to local
  7592. // variables.
  7593. //
  7594. // The reference to the local variable must be loaded into the register,
  7595. // the resulting expression must not be considered as a local variable
  7596. // with a stack offset (i.e. it will not be allowed to use asBC_VAR)
  7597. if( le.type.isLValue && re.type.isLValue &&
  7598. le.deferredParams.GetLength() == 0 && re.deferredParams.GetLength() == 0 &&
  7599. !le.type.isTemporary && !re.type.isTemporary &&
  7600. le.type.dataType == re.type.dataType )
  7601. {
  7602. // Put the code for the condition expression on the output
  7603. MergeExprBytecode(ctx, &e);
  7604. // Add the branch decision
  7605. ctx->type = e.type;
  7606. ConvertToVariable(ctx);
  7607. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  7608. ctx->bc.Instr(asBC_ClrHi);
  7609. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  7610. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7611. // Start of the left expression
  7612. MergeExprBytecode(ctx, &le);
  7613. if( !le.type.dataType.IsReference() && le.type.isVariable )
  7614. {
  7615. // Load the address of the variable into the register
  7616. ctx->bc.InstrSHORT(asBC_LDV, le.type.stackOffset);
  7617. }
  7618. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  7619. // Start of the right expression
  7620. ctx->bc.Label((short)elseLabel);
  7621. MergeExprBytecode(ctx, &re);
  7622. if( !re.type.dataType.IsReference() && re.type.isVariable )
  7623. {
  7624. // Load the address of the variable into the register
  7625. ctx->bc.InstrSHORT(asBC_LDV, re.type.stackOffset);
  7626. }
  7627. ctx->bc.Label((short)afterLabel);
  7628. // In case the options were to objects, it is necessary to dereference the pointer on
  7629. // the stack so it will point to the actual object, instead of the variable
  7630. if( le.type.dataType.IsReference() && le.type.dataType.IsObject() && !le.type.dataType.IsObjectHandle() )
  7631. {
  7632. asASSERT( re.type.dataType.IsReference() && re.type.dataType.IsObject() && !re.type.dataType.IsObjectHandle() );
  7633. ctx->bc.Instr(asBC_RDSPtr);
  7634. }
  7635. // The result is an lvalue
  7636. ctx->type.isLValue = true;
  7637. ctx->type.dataType = le.type.dataType;
  7638. if( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsObjectHandle() )
  7639. ctx->type.dataType.MakeReference(true);
  7640. else
  7641. ctx->type.dataType.MakeReference(false);
  7642. // It can't be a treated as a variable, since we don't know which one was used
  7643. ctx->type.isVariable = false;
  7644. ctx->type.isTemporary = false;
  7645. // Must remember if the reference was to a local variable, since it must not be allowed to be returned
  7646. ctx->type.isRefToLocal = le.type.isVariable || le.type.isRefToLocal || re.type.isVariable || re.type.isRefToLocal;
  7647. }
  7648. else
  7649. {
  7650. // Allocate temporary variable and copy the result to that one
  7651. asCExprValue temp;
  7652. temp = le.type;
  7653. temp.dataType.MakeReference(false);
  7654. temp.dataType.MakeReadOnly(false);
  7655. // Make sure the variable isn't used in any of the expressions,
  7656. // as it would be overwritten which may cause crashes or less visible bugs
  7657. int l = int(reservedVariables.GetLength());
  7658. e.bc.GetVarsUsed(reservedVariables);
  7659. le.bc.GetVarsUsed(reservedVariables);
  7660. re.bc.GetVarsUsed(reservedVariables);
  7661. int offset = AllocateVariable(temp.dataType, true, false);
  7662. reservedVariables.SetLength(l);
  7663. temp.SetVariable(temp.dataType, offset, true);
  7664. // TODO: copy: Use copy constructor if available. See PrepareTemporaryVariable()
  7665. CallDefaultConstructor(temp.dataType, offset, IsVariableOnHeap(offset), &ctx->bc, expr);
  7666. // Put the code for the condition expression on the output
  7667. MergeExprBytecode(ctx, &e);
  7668. // Add the branch decision
  7669. ctx->type = e.type;
  7670. ConvertToVariable(ctx);
  7671. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  7672. ctx->bc.Instr(asBC_ClrHi);
  7673. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  7674. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7675. // Assign the result of the left expression to the temporary variable
  7676. asCExprValue rtemp;
  7677. rtemp = temp;
  7678. if( rtemp.dataType.IsObjectHandle() )
  7679. rtemp.isExplicitHandle = true;
  7680. PrepareForAssignment(&rtemp.dataType, &le, cexpr->next, true);
  7681. MergeExprBytecode(ctx, &le);
  7682. if( !rtemp.dataType.IsPrimitive() )
  7683. {
  7684. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7685. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  7686. }
  7687. asCExprValue result;
  7688. result = rtemp;
  7689. PerformAssignment(&result, &le.type, &ctx->bc, cexpr->next);
  7690. if( !result.dataType.IsPrimitive() )
  7691. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  7692. // Release the old temporary variable
  7693. ReleaseTemporaryVariable(le.type, &ctx->bc);
  7694. // Process any deferred arguments in the expressions as these must not survive until after the condition returns
  7695. ProcessDeferredParams(ctx);
  7696. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  7697. // Start of the right expression
  7698. ctx->bc.Label((short)elseLabel);
  7699. // Copy the result to the same temporary variable
  7700. PrepareForAssignment(&rtemp.dataType, &re, cexpr->next, true);
  7701. MergeExprBytecode(ctx, &re);
  7702. if( !rtemp.dataType.IsPrimitive() )
  7703. {
  7704. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7705. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  7706. }
  7707. result = rtemp;
  7708. PerformAssignment(&result, &re.type, &ctx->bc, cexpr->next);
  7709. if( !result.dataType.IsPrimitive() )
  7710. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  7711. // Release the old temporary variable
  7712. ReleaseTemporaryVariable(re.type, &ctx->bc);
  7713. // Process any deferred arguments in the expressions as these must not survive until after the condition returns
  7714. ProcessDeferredParams(ctx);
  7715. ctx->bc.Label((short)afterLabel);
  7716. // Set the temporary variable as output
  7717. ctx->type = rtemp;
  7718. ctx->type.isExplicitHandle = isExplicitHandle;
  7719. if( !ctx->type.dataType.IsPrimitive() )
  7720. {
  7721. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7722. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  7723. }
  7724. // Make sure the output isn't marked as being a literal constant
  7725. ctx->type.isConstant = false;
  7726. }
  7727. }
  7728. }
  7729. else
  7730. {
  7731. ctx->type.SetDummy();
  7732. return -1;
  7733. }
  7734. }
  7735. else
  7736. return CompileExpression(cexpr, ctx);
  7737. return 0;
  7738. }
  7739. int asCCompiler::CompileExpression(asCScriptNode *expr, asCExprContext *ctx)
  7740. {
  7741. asASSERT(expr->nodeType == snExpression);
  7742. // Convert to polish post fix, i.e: a+b => ab+
  7743. asCArray<asCScriptNode *> postfix;
  7744. ConvertToPostFix(expr, postfix);
  7745. // Compile the postfix formatted expression
  7746. return CompilePostFixExpression(&postfix, ctx);
  7747. }
  7748. void asCCompiler::ConvertToPostFix(asCScriptNode *expr, asCArray<asCScriptNode *> &postfix)
  7749. {
  7750. // The algorithm that I've implemented here is similar to
  7751. // Djikstra's Shunting Yard algorithm, though I didn't know it at the time.
  7752. // ref: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
  7753. // Count the nodes in order to preallocate the buffers
  7754. int count = 0;
  7755. asCScriptNode *node = expr->firstChild;
  7756. while( node )
  7757. {
  7758. count++;
  7759. node = node->next;
  7760. }
  7761. asCArray<asCScriptNode *> stackA(count);
  7762. asCArray<asCScriptNode *> &stackB = postfix;
  7763. stackB.Allocate(count, false);
  7764. node = expr->firstChild;
  7765. while( node )
  7766. {
  7767. int precedence = GetPrecedence(node);
  7768. while( stackA.GetLength() > 0 &&
  7769. precedence <= GetPrecedence(stackA[stackA.GetLength()-1]) )
  7770. stackB.PushLast(stackA.PopLast());
  7771. stackA.PushLast(node);
  7772. node = node->next;
  7773. }
  7774. while( stackA.GetLength() > 0 )
  7775. stackB.PushLast(stackA.PopLast());
  7776. }
  7777. int asCCompiler::CompilePostFixExpression(asCArray<asCScriptNode *> *postfix, asCExprContext *ctx)
  7778. {
  7779. // Shouldn't send any byte code
  7780. asASSERT(ctx->bc.GetLastInstr() == -1);
  7781. // Set the context to a dummy type to avoid further
  7782. // errors in case the expression fails to compile
  7783. ctx->type.SetDummy();
  7784. // Evaluate the operands and operators
  7785. asCArray<asCExprContext*> free;
  7786. asCArray<asCExprContext*> expr;
  7787. int ret = 0;
  7788. for( asUINT n = 0; ret == 0 && n < postfix->GetLength(); n++ )
  7789. {
  7790. asCScriptNode *node = (*postfix)[n];
  7791. if( node->nodeType == snExprTerm )
  7792. {
  7793. asCExprContext *e = free.GetLength() ? free.PopLast() : asNEW(asCExprContext)(engine);
  7794. expr.PushLast(e);
  7795. e->exprNode = node;
  7796. ret = CompileExpressionTerm(node, e);
  7797. }
  7798. else
  7799. {
  7800. asCExprContext *r = expr.PopLast();
  7801. asCExprContext *l = expr.PopLast();
  7802. // Now compile the operator
  7803. asCExprContext *e = free.GetLength() ? free.PopLast() : asNEW(asCExprContext)(engine);
  7804. ret = CompileOperator(node, l, r, e);
  7805. expr.PushLast(e);
  7806. // Free the operands
  7807. l->Clear();
  7808. free.PushLast(l);
  7809. r->Clear();
  7810. free.PushLast(r);
  7811. }
  7812. }
  7813. if( ret == 0 )
  7814. {
  7815. asASSERT(expr.GetLength() == 1);
  7816. // The final result should be moved to the output context
  7817. MergeExprBytecodeAndType(ctx, expr[0]);
  7818. }
  7819. // Clean up
  7820. for( asUINT e = 0; e < expr.GetLength(); e++ )
  7821. asDELETE(expr[e], asCExprContext);
  7822. for( asUINT f = 0; f < free.GetLength(); f++ )
  7823. asDELETE(free[f], asCExprContext);
  7824. return ret;
  7825. }
  7826. int asCCompiler::CompileAnonymousInitList(asCScriptNode *node, asCExprContext *ctx, const asCDataType &dt)
  7827. {
  7828. asASSERT(node->nodeType == snInitList);
  7829. // Do not allow constructing non-shared types in shared functions
  7830. if (outFunc->IsShared() &&
  7831. dt.GetTypeInfo() && !dt.GetTypeInfo()->IsShared())
  7832. {
  7833. asCString msg;
  7834. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetTypeInfo()->name.AddressOf());
  7835. Error(msg, node);
  7836. }
  7837. // If this is compiled from a default arg, then use the script code for the default arg
  7838. asCScriptCode *origCode = script;
  7839. if (ctx->origCode)
  7840. script = ctx->origCode;
  7841. // Allocate and initialize the temporary object
  7842. int offset = AllocateVariable(dt, true);
  7843. CompileInitialization(node, &ctx->bc, dt, node, offset, 0, 0);
  7844. // Push the reference to the object on the stack
  7845. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7846. ctx->type.SetVariable(dt, offset, true);
  7847. ctx->type.isLValue = false;
  7848. // If the variable is allocated on the heap we have a reference,
  7849. // otherwise the actual object pointer is pushed on the stack.
  7850. if (IsVariableOnHeap(offset))
  7851. ctx->type.dataType.MakeReference(true);
  7852. // Clear the flag for anonymous initalization list as it is no
  7853. // longer true now that the object has been initialized.
  7854. ctx->isAnonymousInitList = false;
  7855. ctx->origCode = 0;
  7856. script = origCode;
  7857. return 0;
  7858. }
  7859. int asCCompiler::CompileExpressionTerm(asCScriptNode *node, asCExprContext *ctx)
  7860. {
  7861. // Shouldn't send any byte code
  7862. asASSERT(ctx->bc.GetLastInstr() == -1);
  7863. // Check if this is an initialization of a temp object with an initialization list
  7864. if (node->firstChild )
  7865. {
  7866. if (node->firstChild->nodeType == snDataType)
  7867. {
  7868. // Determine the type of the temporary object
  7869. asCDataType dt = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  7870. return CompileAnonymousInitList(node->lastChild, ctx, dt);
  7871. }
  7872. else if (node->firstChild->nodeType == snInitList)
  7873. {
  7874. // As the type is not yet known, the init list will be compiled at a
  7875. // later time when the type can be determined from the destination
  7876. ctx->SetAnonymousInitList(node->firstChild, script);
  7877. return 0;
  7878. }
  7879. }
  7880. // Set the type as a dummy by default, in case of any compiler errors
  7881. ctx->type.SetDummy();
  7882. // Compile the value node
  7883. asCScriptNode *vnode = node->firstChild;
  7884. while( vnode->nodeType != snExprValue )
  7885. vnode = vnode->next;
  7886. asCExprContext v(engine);
  7887. int r = CompileExpressionValue(vnode, &v);
  7888. if( r < 0 )
  7889. return r;
  7890. // Compile post fix operators
  7891. asCScriptNode *pnode = vnode->next;
  7892. while( pnode )
  7893. {
  7894. r = CompileExpressionPostOp(pnode, &v);
  7895. if( r < 0 )
  7896. return r;
  7897. pnode = pnode->next;
  7898. }
  7899. // Compile pre fix operators
  7900. pnode = vnode->prev;
  7901. while( pnode )
  7902. {
  7903. r = CompileExpressionPreOp(pnode, &v);
  7904. if( r < 0 )
  7905. return r;
  7906. pnode = pnode->prev;
  7907. }
  7908. // Return the byte code and final type description
  7909. MergeExprBytecodeAndType(ctx, &v);
  7910. return 0;
  7911. }
  7912. // returns:
  7913. // SL_LOCALCONST = local constant
  7914. // SL_LOCALVAR = local variable
  7915. // SL_NOMATCH = no match
  7916. asCCompiler::SYMBOLTYPE asCCompiler::SymbolLookupLocalVar(const asCString &name, asCExprContext *outResult)
  7917. {
  7918. sVariable *v = 0;
  7919. if (variables)
  7920. v = variables->GetVariable(name.AddressOf());
  7921. if (v)
  7922. {
  7923. if (v->isPureConstant)
  7924. {
  7925. outResult->type.SetConstantData(v->type, v->constantValue);
  7926. return SL_LOCALCONST;
  7927. }
  7928. outResult->type.SetVariable(v->type, v->stackOffset, false);
  7929. return SL_LOCALVAR;
  7930. }
  7931. return SL_NOMATCH;
  7932. }
  7933. // returns:
  7934. // SL_CLASSPROPACCESS = class property accessor
  7935. // SL_CLASSPROP = class property
  7936. // SL_CLASSMETHOD = class method
  7937. // SL_CLASSTYPE = class child type
  7938. // SL_NOMATCH = no match
  7939. // SL_ERROR = error
  7940. asCCompiler::SYMBOLTYPE asCCompiler::SymbolLookupMember(const asCString &name, asCObjectType *objType, asCExprContext *outResult)
  7941. {
  7942. // See if there are any matching property accessors
  7943. asCExprContext access(engine);
  7944. access.type.Set(asCDataType::CreateType(objType, false));
  7945. access.type.dataType.MakeReference(true);
  7946. int r = 0;
  7947. // Indexed property access
  7948. asCExprContext dummyArg(engine);
  7949. r = FindPropertyAccessor(name, &access, &dummyArg, 0, 0, true);
  7950. if (r == 0)
  7951. {
  7952. // Normal property access
  7953. r = FindPropertyAccessor(name, &access, 0, 0, true);
  7954. }
  7955. if (r <= -3) return SL_ERROR;
  7956. if (r != 0)
  7957. {
  7958. // The symbol matches getters/setters (though not necessarily a compilable match)
  7959. MergeExprBytecodeAndType(outResult, &access);
  7960. outResult->type.dataType.SetTypeInfo(objType);
  7961. return SL_CLASSPROPACCESS;
  7962. }
  7963. // Look for matching properties
  7964. asCDataType dt;
  7965. dt = asCDataType::CreateType(objType, false);
  7966. asCObjectProperty *prop = builder->GetObjectProperty(dt, name.AddressOf());
  7967. if (prop)
  7968. {
  7969. outResult->type.dataType.SetTypeInfo(objType);
  7970. return SL_CLASSPROP;
  7971. }
  7972. // If it is not a property, it may still be the name of a method
  7973. asCObjectType *ot = objType;
  7974. for (asUINT n = 0; n < ot->methods.GetLength(); n++)
  7975. {
  7976. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  7977. if (f->name == name &&
  7978. (builder->module->m_accessMask & f->accessMask))
  7979. {
  7980. outResult->type.dataType.SetTypeInfo(objType);
  7981. return SL_CLASSMETHOD;
  7982. }
  7983. }
  7984. // If it is not a method, then it can still be a child type
  7985. for (asUINT n = 0; n < ot->childFuncDefs.GetLength(); n++)
  7986. {
  7987. if (ot->childFuncDefs[n]->name == name)
  7988. {
  7989. outResult->type.dataType.SetTypeInfo(objType);
  7990. return SL_CLASSTYPE;
  7991. }
  7992. }
  7993. return SL_NOMATCH;
  7994. }
  7995. // The purpose of this function is to find the entity that matches the symbol name respecting the scope and visibility hierarchy
  7996. // The 'outResult' will be used to return info on what was identified, but no code will be produced by this function
  7997. // input:
  7998. // name = the name of the symbol to look for
  7999. // scope = explicit scope informed
  8000. // objType = used to look for symbols within object type (e.g. when compiling post op), in this case no local or global symbols will be looked up
  8001. // returns:
  8002. // SL_NOMATCH = no matching symbol
  8003. // SL_LOCALCONST = local constant
  8004. // SL_LOCALVAR = local variable
  8005. // SL_THISPTR = this pointer
  8006. // SL_CLASSPROPACCESS = class property accessor, lookupResult->dataType holds the object type in which the member was found
  8007. // SL_CLASSPROP = class property, lookupResult->dataType holds the object type in which the member was found
  8008. // SL_CLASSMETHOD = class method, lookupResult->dataType holds the object type in which the member was found
  8009. // SL_CLASSTYPE = class child type, lookupResult->dataType holds the object type in which the member was found
  8010. // SL_GLOBALPROPACCESS = global property accessor, lookupResult->symbolNamespace holds the namespace where the symbol was identified
  8011. // SL_GLOBALCONST = global constant, lookupResult->symbolNamespace holds the namespace where the symbol was identified
  8012. // SL_GLOBALVAR = global variable, lookupResult->symbolNamespace holds the namespace where the symbol was identified
  8013. // SL_GLOBALFUNC = global function, lookupResult->symbolNamespace holds the namespace where the symbol was identified
  8014. // SL_GLOBALTYPE = type, lookupResult->dataType holds the type
  8015. // SL_ENUMVAL = enum value, lookupResult->dataType holds the enum type, unless ambigious. lookupResult->symbolNamespace holds the namespace where the symbol was identified
  8016. // SL_ERROR = error
  8017. asCCompiler::SYMBOLTYPE asCCompiler::SymbolLookup(const asCString &name, const asCString &scope, asCObjectType *objType, asCExprContext *outResult)
  8018. {
  8019. asASSERT(outResult);
  8020. // It is a local variable or parameter?
  8021. // This is not accessible by default arg expressions
  8022. if (!isCompilingDefaultArg && scope == "" && !objType )
  8023. {
  8024. SYMBOLTYPE r = SymbolLookupLocalVar(name, outResult);
  8025. if (r != 0)
  8026. return r;
  8027. }
  8028. // Is it a class member?
  8029. if (scope == "" && ((objType) || (outFunc && outFunc->objectType)))
  8030. {
  8031. // 'this' is not accessible by default arg expressions
  8032. if (name == THIS_TOKEN && !objType && !isCompilingDefaultArg)
  8033. {
  8034. asCDataType dt = asCDataType::CreateType(outFunc->objectType, outFunc->IsReadOnly());
  8035. // The object pointer is located at stack position 0
  8036. outResult->type.SetVariable(dt, 0, false);
  8037. return SL_THISPTR;
  8038. }
  8039. // 'super' is not accessible by default arg expressions
  8040. if (m_isConstructor && name == SUPER_TOKEN && !objType && !isCompilingDefaultArg)
  8041. {
  8042. // If the class is derived from another class, then super can be used to call the base' class constructor
  8043. if (outFunc && outFunc->objectType->derivedFrom)
  8044. {
  8045. outResult->type.dataType.SetTypeInfo(outFunc->objectType->derivedFrom);
  8046. return SL_CLASSMETHOD;
  8047. }
  8048. }
  8049. // Look for members in the type
  8050. // class members are only accessible in default arg expressions as post op '.'
  8051. if( !isCompilingDefaultArg || (isCompilingDefaultArg && objType) )
  8052. {
  8053. SYMBOLTYPE r = SymbolLookupMember(name, objType ? objType : outFunc->objectType, outResult);
  8054. if (r != 0)
  8055. return r;
  8056. }
  8057. }
  8058. // Recursively search parent namespaces for global entities
  8059. asSNameSpace *currNamespace = DetermineNameSpace("");
  8060. while( !objType && currNamespace )
  8061. {
  8062. asCString currScope = scope;
  8063. // If the scope contains ::identifier, then use the last identifier as the class name and the rest of it as the namespace
  8064. // TODO: child funcdef: A scope can include a template type, e.g. array<ns::type>
  8065. int n = currScope.FindLast("::");
  8066. asCString typeName = n >= 0 ? currScope.SubString(n + 2) : currScope;
  8067. asCString nsName = n >= 0 ? currScope.SubString(0, n) : asCString("");
  8068. // If the scope represents a type that the current class inherits
  8069. // from then that should be used instead of going through the namespaces
  8070. if (nsName == "" && (outFunc && outFunc->objectType))
  8071. {
  8072. asCObjectType *ot = outFunc->objectType;
  8073. while (ot)
  8074. {
  8075. if (ot->name == typeName)
  8076. {
  8077. SYMBOLTYPE r = SymbolLookupMember(name, ot, outResult);
  8078. if (r != 0)
  8079. return r;
  8080. }
  8081. ot = ot->derivedFrom;
  8082. }
  8083. }
  8084. // If the scope starts with :: then search from the global scope
  8085. if (currScope.GetLength() < 2 || currScope[0] != ':')
  8086. {
  8087. if (nsName != "")
  8088. {
  8089. if (currNamespace->name != "")
  8090. nsName = currNamespace->name + "::" + nsName;
  8091. }
  8092. else
  8093. nsName = currNamespace->name;
  8094. }
  8095. else
  8096. nsName = nsName.SubString(2);
  8097. // Get the namespace for this scope
  8098. asSNameSpace *ns = engine->FindNameSpace(nsName.AddressOf());
  8099. if (ns)
  8100. {
  8101. // Is there a type with typeName in the namespace?
  8102. asCTypeInfo *scopeType = builder->GetType(typeName.AddressOf(), ns, 0);
  8103. // Check if the symbol is a member of that type
  8104. if (scopeType)
  8105. {
  8106. // Is it an object type?
  8107. if (CastToObjectType(scopeType))
  8108. {
  8109. SYMBOLTYPE r = SymbolLookupMember(name, CastToObjectType(scopeType), outResult);
  8110. if (r != 0)
  8111. return r;
  8112. }
  8113. // Is it an enum type?
  8114. if (CastToEnumType(scopeType))
  8115. {
  8116. asDWORD value = 0;
  8117. asCDataType dt;
  8118. if (builder->GetEnumValueFromType(CastToEnumType(scopeType), name.AddressOf(), dt, value))
  8119. {
  8120. // an enum value was resolved
  8121. outResult->type.SetConstantDW(dt, value);
  8122. outResult->symbolNamespace = ns;
  8123. return SL_ENUMVAL;
  8124. }
  8125. }
  8126. }
  8127. }
  8128. // Get the namespace for this scope. This may return null if the scope is an enum
  8129. nsName = currScope;
  8130. // If the scope starts with :: then search from the global scope
  8131. if (currScope.GetLength() < 2 || currScope[0] != ':')
  8132. {
  8133. if (nsName != "")
  8134. {
  8135. if (currNamespace->name != "")
  8136. nsName = currNamespace->name + "::" + nsName;
  8137. }
  8138. else
  8139. nsName = currNamespace->name;
  8140. }
  8141. else
  8142. nsName = nsName.SubString(2);
  8143. ns = engine->FindNameSpace(nsName.AddressOf());
  8144. // Is it a global property?
  8145. if (ns)
  8146. {
  8147. // See if there are any matching global property accessors
  8148. asCExprContext access(engine);
  8149. int r = 0;
  8150. // Indexed property access
  8151. asCExprContext dummyArg(engine);
  8152. r = FindPropertyAccessor(name, &access, &dummyArg, 0, ns);
  8153. if (r == 0)
  8154. {
  8155. // Normal property access
  8156. r = FindPropertyAccessor(name, &access, 0, ns);
  8157. }
  8158. if (r <= -3) return SL_ERROR;
  8159. if (r != 0)
  8160. {
  8161. // The symbol matches getters/setters (though not necessarily a compilable match)
  8162. MergeExprBytecodeAndType(outResult, &access);
  8163. outResult->symbolNamespace = ns;
  8164. return SL_GLOBALPROPACCESS;
  8165. }
  8166. // See if there is any matching global property
  8167. bool isCompiled = true;
  8168. bool isPureConstant = false;
  8169. bool isAppProp = false;
  8170. asQWORD constantValue = 0;
  8171. asCGlobalProperty *prop = builder->GetGlobalProperty(name.AddressOf(), ns, &isCompiled, &isPureConstant, &constantValue, &isAppProp);
  8172. if (prop)
  8173. {
  8174. // If the global property is a pure constant
  8175. // we can allow the compiler to optimize it. Pure
  8176. // constants are global constant variables that were
  8177. // initialized by literal constants.
  8178. if (isPureConstant)
  8179. {
  8180. outResult->type.SetConstantData(prop->type, constantValue);
  8181. outResult->symbolNamespace = ns;
  8182. return SL_GLOBALCONST;
  8183. }
  8184. else
  8185. {
  8186. outResult->type.Set(prop->type);
  8187. outResult->symbolNamespace = ns;
  8188. return SL_GLOBALVAR;
  8189. }
  8190. }
  8191. }
  8192. // Is it the name of a global function?
  8193. if (ns)
  8194. {
  8195. asCArray<int> funcs;
  8196. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  8197. if (funcs.GetLength() > 0)
  8198. {
  8199. // Defer the evaluation of which function until it is actually used
  8200. // Store the namespace and name of the function for later
  8201. outResult->type.SetUndefinedFuncHandle(engine);
  8202. outResult->methodName = ns ? ns->name + "::" + name : name;
  8203. outResult->symbolNamespace = ns;
  8204. return SL_GLOBALFUNC;
  8205. }
  8206. }
  8207. // Check for type names
  8208. if (ns)
  8209. {
  8210. asCTypeInfo *type = builder->GetType(name.AddressOf(), ns, 0);
  8211. if (type)
  8212. {
  8213. outResult->type.dataType = asCDataType::CreateType(type, false);
  8214. return SL_GLOBALTYPE;
  8215. }
  8216. }
  8217. // Is it an enum value?
  8218. if (ns && !engine->ep.requireEnumScope)
  8219. {
  8220. // Look for the enum value without explicitly informing the enum type
  8221. asDWORD value = 0;
  8222. asCDataType dt;
  8223. int e = builder->GetEnumValue(name.AddressOf(), dt, value, ns);
  8224. if (e)
  8225. {
  8226. if (e == 2)
  8227. {
  8228. // Ambiguous enum value: Save the name for resolution later.
  8229. // The ambiguity could be resolved now, but I hesitate
  8230. // to store too much information in the context.
  8231. outResult->enumValue = name.AddressOf();
  8232. // We cannot set a dummy value because it will pass through
  8233. // cleanly as an integer.
  8234. outResult->type.SetConstantDW(asCDataType::CreatePrimitive(ttIdentifier, true), 0);
  8235. outResult->symbolNamespace = ns;
  8236. return SL_ENUMVAL;
  8237. }
  8238. else
  8239. {
  8240. // an enum value was resolved
  8241. outResult->type.SetConstantDW(dt, value);
  8242. outResult->symbolNamespace = ns;
  8243. return SL_ENUMVAL;
  8244. }
  8245. }
  8246. }
  8247. // If the given scope starts with '::' then the search starts from global scope
  8248. if (scope.GetLength() >= 2 && scope[0] == ':')
  8249. break;
  8250. // Move up to parent namespace
  8251. currNamespace = engine->GetParentNameSpace(currNamespace);
  8252. }
  8253. // The name doesn't match any symbol
  8254. return SL_NOMATCH;
  8255. }
  8256. int asCCompiler::CompileVariableAccess(const asCString &name, const asCString &scope, asCExprContext *ctx, asCScriptNode *errNode, bool isOptional, asCObjectType *objType)
  8257. {
  8258. asCExprContext lookupResult(engine);
  8259. SYMBOLTYPE symbolType = SymbolLookup(name, scope, objType, &lookupResult);
  8260. if (symbolType < 0)
  8261. {
  8262. // Give dummy value
  8263. ctx->type.SetDummy();
  8264. return -1;
  8265. }
  8266. if (symbolType == SL_NOMATCH)
  8267. {
  8268. // Give dummy value
  8269. ctx->type.SetDummy();
  8270. if (!isOptional)
  8271. {
  8272. // No matching symbol
  8273. asCString msg;
  8274. asCString smbl;
  8275. if (scope == "::")
  8276. smbl = scope;
  8277. else if (scope != "")
  8278. smbl = scope + "::";
  8279. smbl += name;
  8280. msg.Format(TXT_NO_MATCHING_SYMBOL_s, smbl.AddressOf());
  8281. Error(msg, errNode);
  8282. }
  8283. return -1;
  8284. }
  8285. // It is a local variable or parameter?
  8286. if( symbolType == SL_LOCALCONST || symbolType == SL_LOCALVAR )
  8287. {
  8288. // This is not accessible by default arg expressions
  8289. asASSERT(!isCompilingDefaultArg && scope == "" && !objType && variables);
  8290. sVariable *v = variables->GetVariable(name.AddressOf());
  8291. asASSERT(v);
  8292. if( v->isPureConstant )
  8293. ctx->type.SetConstantData(v->type, v->constantValue);
  8294. else if( v->type.IsPrimitive() )
  8295. {
  8296. if( v->type.IsReference() )
  8297. {
  8298. // Copy the reference into the register
  8299. ctx->bc.InstrSHORT(asBC_PshVPtr, (short)v->stackOffset);
  8300. ctx->bc.Instr(asBC_PopRPtr);
  8301. ctx->type.Set(v->type);
  8302. }
  8303. else
  8304. ctx->type.SetVariable(v->type, v->stackOffset, false);
  8305. // Set as lvalue unless it is a const variable
  8306. if( !v->type.IsReadOnly() )
  8307. ctx->type.isLValue = true;
  8308. }
  8309. else
  8310. {
  8311. ctx->bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  8312. ctx->type.SetVariable(v->type, v->stackOffset, false);
  8313. // If the variable is allocated on the heap we have a reference,
  8314. // otherwise the actual object pointer is pushed on the stack.
  8315. if( v->onHeap || v->type.IsObjectHandle() ) ctx->type.dataType.MakeReference(true);
  8316. // Implicitly dereference handle parameters sent by reference
  8317. if( v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()) )
  8318. ctx->bc.Instr(asBC_RDSPtr);
  8319. // Mark the object as safe for access unless it is a handle, as the
  8320. // life time of the object is guaranteed throughout the scope.
  8321. if( !v->type.IsObjectHandle() )
  8322. ctx->type.isRefSafe = true;
  8323. // Set as lvalue unless it is a const variable
  8324. if (!v->type.IsReadOnly())
  8325. ctx->type.isLValue = true;
  8326. }
  8327. return 0;
  8328. }
  8329. // Is it a class member?
  8330. if (symbolType == SL_CLASSPROPACCESS || symbolType == SL_CLASSPROP || symbolType == SL_CLASSMETHOD || symbolType == SL_THISPTR)
  8331. {
  8332. // This is not accessible by default arg expressions
  8333. asASSERT(!isCompilingDefaultArg);
  8334. if (symbolType == SL_THISPTR)
  8335. {
  8336. asASSERT(name == THIS_TOKEN && !objType && scope == "");
  8337. asCDataType dt = asCDataType::CreateType(outFunc->objectType, outFunc->IsReadOnly());
  8338. // The object pointer is located at stack position 0
  8339. ctx->bc.InstrSHORT(asBC_PSF, 0);
  8340. ctx->type.SetVariable(dt, 0, false);
  8341. ctx->type.dataType.MakeReference(true);
  8342. ctx->type.isLValue = true;
  8343. // The 'this' handle is always considered safe (i.e. life time guaranteed)
  8344. ctx->type.isRefSafe = true;
  8345. return 0;
  8346. }
  8347. if (symbolType == SL_CLASSPROPACCESS)
  8348. {
  8349. if (scope != "")
  8350. {
  8351. // Cannot access non-static members like this
  8352. asCString msg;
  8353. msg.Format(TXT_CANNOT_ACCESS_NON_STATIC_MEMBER_s, name.AddressOf());
  8354. Error(msg, errNode);
  8355. return -1;
  8356. }
  8357. // See if there are any matching property accessors
  8358. asCExprContext access(engine);
  8359. if (objType)
  8360. access.type.Set(asCDataType::CreateType(objType, false));
  8361. else
  8362. access.type.Set(asCDataType::CreateType(outFunc->objectType, outFunc->IsReadOnly()));
  8363. access.type.dataType.MakeReference(true);
  8364. int r = 0;
  8365. if (errNode->next && errNode->next->tokenType == ttOpenBracket)
  8366. {
  8367. // This is an index access, check if there is a property accessor that takes an index arg
  8368. asCExprContext dummyArg(engine);
  8369. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, 0, true);
  8370. }
  8371. if (r == 0)
  8372. {
  8373. // Normal property access
  8374. r = FindPropertyAccessor(name, &access, errNode, 0, true);
  8375. }
  8376. if (r < 0) return -1;
  8377. if (access.property_get == 0 && access.property_set == 0)
  8378. {
  8379. // Even though the symbol was identified in SymbolLookup, it doesn't match the arguments
  8380. asCString msg;
  8381. if (errNode->next && errNode->next->tokenType == ttOpenBracket)
  8382. msg.Format(TXT_PROP_ACCESS_s_DOES_NOT_EXPECT_INDEX, name.AddressOf());
  8383. else
  8384. msg.Format(TXT_PROP_ACCESS_s_EXPECTS_INDEX, name.AddressOf());
  8385. Error(msg, errNode);
  8386. return -1;
  8387. }
  8388. if (!objType)
  8389. {
  8390. // Prepare the bytecode for the member access
  8391. // This is only done when accessing through the implicit this pointer
  8392. ctx->bc.InstrSHORT(asBC_PSF, 0);
  8393. }
  8394. MergeExprBytecodeAndType(ctx, &access);
  8395. return 0;
  8396. }
  8397. if (symbolType == SL_CLASSPROP)
  8398. {
  8399. if (scope != "")
  8400. {
  8401. // Cannot access non-static members like this
  8402. asCString msg;
  8403. msg.Format(TXT_CANNOT_ACCESS_NON_STATIC_MEMBER_s, name.AddressOf());
  8404. Error(msg, errNode);
  8405. return -1;
  8406. }
  8407. asCDataType dt;
  8408. if (objType)
  8409. dt = asCDataType::CreateType(objType, false);
  8410. else
  8411. dt = asCDataType::CreateType(outFunc->objectType, false);
  8412. asCObjectProperty *prop = builder->GetObjectProperty(dt, name.AddressOf());
  8413. asASSERT(prop);
  8414. // Is the property access allowed?
  8415. if (prop->isPrivate && prop->isInherited)
  8416. {
  8417. if (engine->ep.privatePropAsProtected)
  8418. {
  8419. // The application is allowing inherited classes to access private properties of the parent
  8420. // class. This option is allowed to provide backwards compatibility with pre-2.30.0 versions
  8421. // as it was how the compiler behaved earlier.
  8422. asCString msg;
  8423. msg.Format(TXT_ACCESSING_PRIVATE_PROP_s, name.AddressOf());
  8424. Warning(msg, errNode);
  8425. }
  8426. else
  8427. {
  8428. asCString msg;
  8429. msg.Format(TXT_INHERITED_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  8430. Error(msg, errNode);
  8431. }
  8432. }
  8433. if (!objType)
  8434. {
  8435. // The object pointer is located at stack position 0
  8436. // This is only done when accessing through the implicit this pointer
  8437. ctx->bc.InstrSHORT(asBC_PSF, 0);
  8438. ctx->type.SetVariable(dt, 0, false);
  8439. ctx->type.dataType.MakeReference(true);
  8440. Dereference(ctx, true);
  8441. }
  8442. // TODO: This is the same as what is in CompileExpressionPostOp
  8443. // Put the offset on the stack
  8444. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(dt));
  8445. if (prop->type.IsReference())
  8446. ctx->bc.Instr(asBC_RDSPtr);
  8447. // Reference to primitive must be stored in the temp register
  8448. if (prop->type.IsPrimitive())
  8449. {
  8450. // TODO: runtime optimize: The ADD offset command should store the reference in the register directly
  8451. ctx->bc.Instr(asBC_PopRPtr);
  8452. }
  8453. // Set the new type (keeping info about temp variable)
  8454. ctx->type.dataType = prop->type;
  8455. ctx->type.dataType.MakeReference(true);
  8456. ctx->type.isVariable = false;
  8457. ctx->type.isLValue = true;
  8458. if (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())
  8459. {
  8460. // Objects that are members are not references
  8461. ctx->type.dataType.MakeReference(false);
  8462. // Objects that are members but not handles are safe as long as the parent object is safe
  8463. if (!objType || ctx->type.isRefSafe)
  8464. ctx->type.isRefSafe = true;
  8465. }
  8466. else if (ctx->type.dataType.IsObjectHandle())
  8467. {
  8468. // Objects accessed through handles cannot be considered safe
  8469. // as the handle can be cleared at any time
  8470. ctx->type.isRefSafe = false;
  8471. }
  8472. // If the object reference is const, the property will also be const
  8473. ctx->type.dataType.MakeReadOnly(outFunc->IsReadOnly());
  8474. return 0;
  8475. }
  8476. if (symbolType == SL_CLASSMETHOD)
  8477. {
  8478. if (scope != "")
  8479. {
  8480. // Cannot access non-static members like this
  8481. asCString msg;
  8482. msg.Format(TXT_CANNOT_ACCESS_NON_STATIC_MEMBER_s, name.AddressOf());
  8483. Error(msg, errNode);
  8484. return -1;
  8485. }
  8486. #if AS_DEBUG
  8487. // If it is not a property, it may still be the name of a method which can be used to create delegates
  8488. asCObjectType *ot = outFunc->objectType;
  8489. asCScriptFunction *func = 0;
  8490. for (asUINT n = 0; n < ot->methods.GetLength(); n++)
  8491. {
  8492. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  8493. if (f->name == name &&
  8494. (builder->module->m_accessMask & f->accessMask))
  8495. {
  8496. func = f;
  8497. break;
  8498. }
  8499. }
  8500. asASSERT(func);
  8501. #endif
  8502. // An object method was found. Keep the name of the method in the expression, but
  8503. // don't actually modify the bytecode at this point since it is not yet known what
  8504. // the method will be used for, or even what overloaded method should be used.
  8505. ctx->methodName = name;
  8506. // Place the object pointer on the stack, as if the expression was this.func
  8507. if (!objType)
  8508. {
  8509. // The object pointer is located at stack position 0
  8510. // This is only done when accessing through the implicit this pointer
  8511. ctx->bc.InstrSHORT(asBC_PSF, 0);
  8512. ctx->type.SetVariable(asCDataType::CreateType(outFunc->objectType, false), 0, false);
  8513. ctx->type.dataType.MakeReference(true);
  8514. Dereference(ctx, true);
  8515. }
  8516. return 0;
  8517. }
  8518. }
  8519. if (symbolType == SL_GLOBALCONST || symbolType == SL_GLOBALPROPACCESS || symbolType == SL_GLOBALVAR || symbolType == SL_GLOBALFUNC || symbolType == SL_ENUMVAL)
  8520. {
  8521. // Get the namespace from SymbolLookup
  8522. asSNameSpace *ns = lookupResult.symbolNamespace;
  8523. if (symbolType == SL_GLOBALPROPACCESS)
  8524. {
  8525. // See if there are any matching global property accessors
  8526. asCExprContext access(engine);
  8527. int r = 0;
  8528. if (errNode->next && errNode->next->tokenType == ttOpenBracket)
  8529. {
  8530. // This is an index access, check if there is a property accessor that takes an index arg
  8531. asCExprContext dummyArg(engine);
  8532. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, ns);
  8533. }
  8534. if (r == 0)
  8535. {
  8536. // Normal property access
  8537. r = FindPropertyAccessor(name, &access, errNode, ns);
  8538. }
  8539. if (r < 0) return -1;
  8540. if (access.property_get == 0 && access.property_set == 0)
  8541. {
  8542. // Even though the symbol was identified in SymbolLookup, it doesn't match the arguments
  8543. asCString msg;
  8544. if (errNode->next && errNode->next->tokenType == ttOpenBracket)
  8545. msg.Format(TXT_PROP_ACCESS_s_DOES_NOT_EXPECT_INDEX, name.AddressOf());
  8546. else
  8547. msg.Format(TXT_PROP_ACCESS_s_EXPECTS_INDEX, name.AddressOf());
  8548. Error(msg, errNode);
  8549. return -1;
  8550. }
  8551. // Prepare the bytecode for the function call
  8552. MergeExprBytecodeAndType(ctx, &access);
  8553. return 0;
  8554. }
  8555. if (symbolType == SL_GLOBALCONST || symbolType == SL_GLOBALVAR)
  8556. {
  8557. bool isCompiled = true;
  8558. bool isPureConstant = false;
  8559. bool isAppProp = false;
  8560. asQWORD constantValue = 0;
  8561. asCGlobalProperty *prop = builder->GetGlobalProperty(name.AddressOf(), ns, &isCompiled, &isPureConstant, &constantValue, &isAppProp);
  8562. asASSERT(prop);
  8563. // Verify that the global property has been compiled already
  8564. if (!isCompiled)
  8565. {
  8566. asCString str;
  8567. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, prop->name.AddressOf());
  8568. Error(str, errNode);
  8569. return -1;
  8570. }
  8571. // If the global property is a pure constant
  8572. // we can allow the compiler to optimize it. Pure
  8573. // constants are global constant variables that were
  8574. // initialized by literal constants.
  8575. if (isPureConstant)
  8576. ctx->type.SetConstantData(prop->type, constantValue);
  8577. else
  8578. {
  8579. // A shared type must not access global vars, unless they
  8580. // too are shared, e.g. application registered vars
  8581. if (outFunc->IsShared())
  8582. {
  8583. if (!isAppProp)
  8584. {
  8585. asCString str;
  8586. str.Format(TXT_SHARED_CANNOT_ACCESS_NON_SHARED_VAR_s, prop->name.AddressOf());
  8587. Error(str, errNode);
  8588. // Allow the compilation to continue to catch other problems
  8589. }
  8590. }
  8591. ctx->type.Set(prop->type);
  8592. ctx->type.isLValue = true;
  8593. if (ctx->type.dataType.IsPrimitive())
  8594. {
  8595. // Load the address of the variable into the register
  8596. ctx->bc.InstrPTR(asBC_LDG, prop->GetAddressOfValue());
  8597. ctx->type.dataType.MakeReference(true);
  8598. }
  8599. else
  8600. {
  8601. // Push the address of the variable on the stack
  8602. ctx->bc.InstrPTR(asBC_PGA, prop->GetAddressOfValue());
  8603. // If the object is a value type or a non-handle variable to a reference type,
  8604. // then we must validate the existance as it could potentially be accessed
  8605. // before it is initialized.
  8606. // This check is not needed for application registered properties, since they
  8607. // are guaranteed to be valid by the application itself.
  8608. if (!isAppProp &&
  8609. ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_VALUE) ||
  8610. !ctx->type.dataType.IsObjectHandle()))
  8611. {
  8612. ctx->bc.Instr(asBC_ChkRefS);
  8613. }
  8614. // If the address pushed on the stack is to a value type or an object
  8615. // handle, then mark the expression as a reference. Addresses to a reference
  8616. // type aren't marked as references to get correct behaviour
  8617. if ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_VALUE) ||
  8618. ctx->type.dataType.IsObjectHandle())
  8619. {
  8620. ctx->type.dataType.MakeReference(true);
  8621. }
  8622. else
  8623. {
  8624. asASSERT((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !ctx->type.dataType.IsObjectHandle());
  8625. // It's necessary to dereference the pointer so the pointer on the stack will point to the actual object
  8626. ctx->bc.Instr(asBC_RDSPtr);
  8627. }
  8628. }
  8629. }
  8630. return 0;
  8631. }
  8632. if (symbolType == SL_GLOBALFUNC)
  8633. {
  8634. asCArray<int> funcs;
  8635. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  8636. asASSERT(funcs.GetLength() > 0);
  8637. if (funcs.GetLength() > 0)
  8638. {
  8639. // Defer the evaluation of which function until it is actually used
  8640. // Store the namespace and name of the function for later
  8641. ctx->type.SetUndefinedFuncHandle(engine);
  8642. ctx->methodName = ns ? ns->name + "::" + name : name;
  8643. }
  8644. return 0;
  8645. }
  8646. if (symbolType == SL_ENUMVAL)
  8647. {
  8648. // The enum type and namespace must be returned from SymbolLookup
  8649. asCDataType dt = lookupResult.type.dataType;
  8650. if (!dt.IsEnumType())
  8651. {
  8652. asASSERT(!engine->ep.requireEnumScope);
  8653. // It is an ambigious enum value. The evaluation needs to be deferred for when the type is known
  8654. ctx->enumValue = name.AddressOf();
  8655. ctx->symbolNamespace = lookupResult.symbolNamespace;
  8656. // We cannot set a dummy value because it will pass through
  8657. // cleanly as an integer.
  8658. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttIdentifier, true), 0);
  8659. return 0;
  8660. }
  8661. asDWORD value = 0;
  8662. builder->GetEnumValueFromType(CastToEnumType(lookupResult.type.dataType.GetTypeInfo()), name.AddressOf(), dt, value);
  8663. // Even if the enum type is not shared, and we're compiling a shared object,
  8664. // the use of the values are still allowed, since they are treated as constants.
  8665. // an enum value was resolved
  8666. ctx->type.SetConstantDW(dt, value);
  8667. return 0;
  8668. }
  8669. }
  8670. // The result must have been identified above
  8671. if (symbolType == SL_GLOBALTYPE || symbolType == SL_CLASSTYPE)
  8672. {
  8673. // Give dummy value
  8674. ctx->type.SetDummy();
  8675. // The symbol matches a type
  8676. asCString msg;
  8677. asCString smbl;
  8678. if (scope == "::")
  8679. smbl = scope;
  8680. else if (scope != "")
  8681. smbl = scope + "::";
  8682. smbl += name;
  8683. msg.Format(TXT_EXPR_s_IS_DATA_TYPE, smbl.AddressOf());
  8684. Error(msg, errNode);
  8685. return -1;
  8686. }
  8687. // Should not come here
  8688. asASSERT(false);
  8689. return 0;
  8690. }
  8691. int asCCompiler::CompileExpressionValue(asCScriptNode *node, asCExprContext *ctx)
  8692. {
  8693. // Shouldn't receive any byte code
  8694. asASSERT(ctx->bc.GetLastInstr() == -1);
  8695. asCScriptNode *vnode = node->firstChild;
  8696. ctx->exprNode = vnode;
  8697. if( vnode->nodeType == snVariableAccess )
  8698. {
  8699. // Determine the scope resolution of the variable
  8700. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script, &vnode);
  8701. // Determine the name of the variable
  8702. asASSERT(vnode->nodeType == snIdentifier );
  8703. asCString name(&script->code[vnode->tokenPos], vnode->tokenLength);
  8704. return CompileVariableAccess(name, scope, ctx, node);
  8705. }
  8706. else if( vnode->nodeType == snConstant )
  8707. {
  8708. if( vnode->tokenType == ttIntConstant )
  8709. {
  8710. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  8711. bool overflow = false;
  8712. asQWORD val = asStringScanUInt64(value.AddressOf(), 10, 0, &overflow);
  8713. // Is the number bigger than a 64bit word?
  8714. if (overflow)
  8715. {
  8716. Error(TXT_VALUE_TOO_LARGE_FOR_TYPE, vnode);
  8717. // Set the value to zero to avoid further warnings
  8718. val = 0;
  8719. }
  8720. // Do we need 64 bits?
  8721. // If the 31st bit is set we'll treat the value as a signed 64bit number to avoid
  8722. // incorrect warnings about changing signs if the value is assigned to a 64bit variable
  8723. if( val>>31 )
  8724. {
  8725. // Only if the value uses the last bit of a 64bit word do we consider the number unsigned
  8726. if( val>>63 )
  8727. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  8728. else
  8729. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), val);
  8730. }
  8731. else
  8732. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), asDWORD(val));
  8733. }
  8734. else if( vnode->tokenType == ttBitsConstant )
  8735. {
  8736. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  8737. // Let the function determine the radix from the prefix 0x = 16, 0d = 10, 0o = 8, or 0b = 2
  8738. bool overflow = false;
  8739. asQWORD val = asStringScanUInt64(value.AddressOf(), 0, 0, &overflow);
  8740. // Is the number bigger than a 64bit word?
  8741. if (overflow)
  8742. {
  8743. Error(TXT_VALUE_TOO_LARGE_FOR_TYPE, vnode);
  8744. // Set the value to zero to avoid further warnings
  8745. val = 0;
  8746. }
  8747. // Do we need 64 bits?
  8748. if( val>>32 )
  8749. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  8750. else
  8751. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  8752. }
  8753. else if( vnode->tokenType == ttFloatConstant )
  8754. {
  8755. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  8756. // TODO: Check for overflow
  8757. size_t numScanned;
  8758. float v = float(asStringScanDouble(value.AddressOf(), &numScanned));
  8759. ctx->type.SetConstantF(asCDataType::CreatePrimitive(ttFloat, true), v);
  8760. #ifndef AS_USE_DOUBLE_AS_FLOAT
  8761. // Don't check this if we have double as float, because then the whole token would be scanned (i.e. no f suffix)
  8762. asASSERT(numScanned == vnode->tokenLength - 1);
  8763. #endif
  8764. }
  8765. else if( vnode->tokenType == ttDoubleConstant )
  8766. {
  8767. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  8768. // TODO: Check for overflow
  8769. size_t numScanned;
  8770. double v = asStringScanDouble(value.AddressOf(), &numScanned);
  8771. ctx->type.SetConstantD(asCDataType::CreatePrimitive(ttDouble, true), v);
  8772. asASSERT(numScanned == vnode->tokenLength);
  8773. }
  8774. else if( vnode->tokenType == ttTrue ||
  8775. vnode->tokenType == ttFalse )
  8776. {
  8777. #if AS_SIZEOF_BOOL == 1
  8778. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  8779. #else
  8780. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  8781. #endif
  8782. }
  8783. else if( vnode->tokenType == ttStringConstant ||
  8784. vnode->tokenType == ttMultilineStringConstant ||
  8785. vnode->tokenType == ttHeredocStringConstant )
  8786. {
  8787. asCString str;
  8788. asCScriptNode *snode = vnode->firstChild;
  8789. if( script->code[snode->tokenPos] == '\'' && engine->ep.useCharacterLiterals )
  8790. {
  8791. // Treat the single quoted string as a single character literal
  8792. str.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  8793. asDWORD val = 0;
  8794. if( str.GetLength() && (asBYTE)str[0] > 127 && engine->ep.scanner == 1 )
  8795. {
  8796. // This is the start of a UTF8 encoded character. We need to decode it
  8797. val = asStringDecodeUTF8(str.AddressOf(), 0);
  8798. if( val == (asDWORD)-1 )
  8799. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  8800. }
  8801. else
  8802. {
  8803. val = ProcessStringConstant(str, snode);
  8804. if( val == (asDWORD)-1 )
  8805. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  8806. }
  8807. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), val);
  8808. }
  8809. else
  8810. {
  8811. // Process the string constants
  8812. while( snode )
  8813. {
  8814. asCString cat;
  8815. if( snode->tokenType == ttStringConstant )
  8816. {
  8817. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  8818. ProcessStringConstant(cat, snode);
  8819. }
  8820. else if( snode->tokenType == ttMultilineStringConstant )
  8821. {
  8822. if( !engine->ep.allowMultilineStrings )
  8823. Error(TXT_MULTILINE_STRINGS_NOT_ALLOWED, snode);
  8824. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  8825. ProcessStringConstant(cat, snode);
  8826. }
  8827. else if( snode->tokenType == ttHeredocStringConstant )
  8828. {
  8829. cat.Assign(&script->code[snode->tokenPos+3], snode->tokenLength-6);
  8830. ProcessHeredocStringConstant(cat, snode);
  8831. }
  8832. str += cat;
  8833. snode = snode->next;
  8834. }
  8835. // Call the string factory function to create a string object
  8836. if(engine->stringFactory == 0 )
  8837. {
  8838. // Error
  8839. Error(TXT_STRINGS_NOT_RECOGNIZED, vnode);
  8840. // Give dummy value
  8841. ctx->type.SetDummy();
  8842. return -1;
  8843. }
  8844. else
  8845. {
  8846. void *strPtr = const_cast<void*>(engine->stringFactory->GetStringConstant(str.AddressOf(), (asUINT)str.GetLength()));
  8847. if (strPtr == 0)
  8848. {
  8849. // TODO: A better message is needed
  8850. Error(TXT_NULL_POINTER_ACCESS, vnode);
  8851. ctx->type.SetDummy();
  8852. return -1;
  8853. }
  8854. // Keep the pointer in the list for clean up at exit
  8855. usedStringConstants.PushLast(strPtr);
  8856. // Push the pointer on the stack. The string factory already guarantees that the
  8857. // string object is valid throughout the lifetime of the script so no need to add
  8858. // reference count or make local copy.
  8859. ctx->bc.InstrPTR(asBC_PGA, strPtr);
  8860. ctx->type.Set(engine->stringType);
  8861. // Mark the string as literal constant so the compiler knows it is allowed
  8862. // to treat it differently than an ordinary constant string variable
  8863. ctx->type.isConstant = true;
  8864. // Mark the reference to the string constant as safe, so the compiler can
  8865. // avoid making unnecessary temporary copies when passing the reference to
  8866. // functions.
  8867. ctx->type.isRefSafe = true;
  8868. }
  8869. }
  8870. }
  8871. else if( vnode->tokenType == ttNull )
  8872. {
  8873. ctx->bc.Instr(asBC_PshNull);
  8874. ctx->type.SetNullConstant();
  8875. }
  8876. else
  8877. asASSERT(false);
  8878. }
  8879. else if( vnode->nodeType == snFunctionCall )
  8880. {
  8881. // Determine the scope resolution
  8882. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script);
  8883. return CompileFunctionCall(vnode, ctx, 0, false, scope);
  8884. }
  8885. else if( vnode->nodeType == snConstructCall )
  8886. {
  8887. return CompileConstructCall(vnode, ctx);
  8888. }
  8889. else if( vnode->nodeType == snAssignment )
  8890. {
  8891. asCExprContext e(engine);
  8892. int r = CompileAssignment(vnode, &e);
  8893. if( r < 0 )
  8894. {
  8895. ctx->type.SetDummy();
  8896. return r;
  8897. }
  8898. MergeExprBytecodeAndType(ctx, &e);
  8899. }
  8900. else if( vnode->nodeType == snCast )
  8901. {
  8902. // Implement the cast operator
  8903. return CompileConversion(vnode, ctx);
  8904. }
  8905. else if( vnode->nodeType == snUndefined && vnode->tokenType == ttVoid )
  8906. {
  8907. // This is a void expression
  8908. ctx->SetVoidExpression();
  8909. }
  8910. else if( vnode->nodeType == snFunction )
  8911. {
  8912. // This is an anonymous function
  8913. // Defer the evaluation of the function until it is known where it
  8914. // will be used, which is where the signature will be defined
  8915. ctx->SetLambda(vnode);
  8916. }
  8917. else
  8918. asASSERT(false);
  8919. return 0;
  8920. }
  8921. asUINT asCCompiler::ProcessStringConstant(asCString &cstr, asCScriptNode *node, bool processEscapeSequences)
  8922. {
  8923. int charLiteral = -1;
  8924. // Process escape sequences
  8925. asCArray<char> str((int)cstr.GetLength());
  8926. for( asUINT n = 0; n < cstr.GetLength(); n++ )
  8927. {
  8928. #ifdef AS_DOUBLEBYTE_CHARSET
  8929. // Double-byte charset is only allowed for ASCII and not UTF16 encoded strings
  8930. if( (cstr[n] & 0x80) && engine->ep.scanner == 0 && engine->ep.stringEncoding != 1 )
  8931. {
  8932. // This is the lead character of a double byte character
  8933. // include the trail character without checking it's value.
  8934. str.PushLast(cstr[n]);
  8935. n++;
  8936. str.PushLast(cstr[n]);
  8937. continue;
  8938. }
  8939. #endif
  8940. asUINT val;
  8941. if( processEscapeSequences && cstr[n] == '\\' )
  8942. {
  8943. ++n;
  8944. if( n == cstr.GetLength() )
  8945. {
  8946. if( charLiteral == -1 ) charLiteral = 0;
  8947. return charLiteral;
  8948. }
  8949. // Hexadecimal escape sequences will allow the construction of
  8950. // invalid unicode sequences, but the string should also work as
  8951. // a bytearray so we must support this. The code for working with
  8952. // unicode text must be prepared to handle invalid unicode sequences
  8953. if( cstr[n] == 'x' || cstr[n] == 'X' )
  8954. {
  8955. ++n;
  8956. if( n == cstr.GetLength() ) break;
  8957. val = 0;
  8958. int c = engine->ep.stringEncoding == 1 ? 4 : 2;
  8959. for( ; c > 0 && n < cstr.GetLength(); c--, n++ )
  8960. {
  8961. if( cstr[n] >= '0' && cstr[n] <= '9' )
  8962. val = val*16 + cstr[n] - '0';
  8963. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  8964. val = val*16 + cstr[n] - 'a' + 10;
  8965. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  8966. val = val*16 + cstr[n] - 'A' + 10;
  8967. else
  8968. break;
  8969. }
  8970. // Rewind one, since the loop will increment it again
  8971. n--;
  8972. // Hexadecimal escape sequences produce exact value, even if it is not proper unicode chars
  8973. if( engine->ep.stringEncoding == 0 )
  8974. {
  8975. str.PushLast((asBYTE)val);
  8976. }
  8977. else
  8978. {
  8979. #ifndef AS_BIG_ENDIAN
  8980. str.PushLast((asBYTE)val);
  8981. str.PushLast((asBYTE)(val>>8));
  8982. #else
  8983. str.PushLast((asBYTE)(val>>8));
  8984. str.PushLast((asBYTE)val);
  8985. #endif
  8986. }
  8987. if( charLiteral == -1 ) charLiteral = val;
  8988. continue;
  8989. }
  8990. else if( cstr[n] == 'u' || cstr[n] == 'U' )
  8991. {
  8992. // \u expects 4 hex digits
  8993. // \U expects 8 hex digits
  8994. bool expect2 = cstr[n] == 'u';
  8995. int c = expect2 ? 4 : 8;
  8996. val = 0;
  8997. for( ; c > 0; c-- )
  8998. {
  8999. ++n;
  9000. if( n == cstr.GetLength() ) break;
  9001. if( cstr[n] >= '0' && cstr[n] <= '9' )
  9002. val = val*16 + cstr[n] - '0';
  9003. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  9004. val = val*16 + cstr[n] - 'a' + 10;
  9005. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  9006. val = val*16 + cstr[n] - 'A' + 10;
  9007. else
  9008. break;
  9009. }
  9010. if( c != 0 )
  9011. {
  9012. // Give warning about invalid code point
  9013. // TODO: Need code position for warning
  9014. asCString msg;
  9015. msg.Format(TXT_INVALID_UNICODE_FORMAT_EXPECTED_d, expect2 ? 4 : 8);
  9016. Warning(msg, node);
  9017. continue;
  9018. }
  9019. }
  9020. else
  9021. {
  9022. if( cstr[n] == '"' )
  9023. val = '"';
  9024. else if( cstr[n] == '\'' )
  9025. val = '\'';
  9026. else if( cstr[n] == 'n' )
  9027. val = '\n';
  9028. else if( cstr[n] == 'r' )
  9029. val = '\r';
  9030. else if( cstr[n] == 't' )
  9031. val = '\t';
  9032. else if( cstr[n] == '0' )
  9033. val = '\0';
  9034. else if( cstr[n] == '\\' )
  9035. val = '\\';
  9036. else
  9037. {
  9038. // Invalid escape sequence
  9039. Warning(TXT_INVALID_ESCAPE_SEQUENCE, node);
  9040. continue;
  9041. }
  9042. }
  9043. }
  9044. else
  9045. {
  9046. if( engine->ep.scanner == 1 && (cstr[n] & 0x80) )
  9047. {
  9048. unsigned int len;
  9049. val = asStringDecodeUTF8(&cstr[n], &len);
  9050. if( val == 0xFFFFFFFF )
  9051. {
  9052. // Incorrect UTF8 encoding. Use only the first byte
  9053. // TODO: Need code position for warning
  9054. Warning(TXT_INVALID_UNICODE_SEQUENCE_IN_SRC, node);
  9055. val = (unsigned char)cstr[n];
  9056. }
  9057. else
  9058. n += len-1;
  9059. }
  9060. else
  9061. val = (unsigned char)cstr[n];
  9062. }
  9063. // Add the character to the final string
  9064. char encodedValue[5];
  9065. int len;
  9066. if( engine->ep.scanner == 1 && engine->ep.stringEncoding == 0 )
  9067. {
  9068. // Convert to UTF8 encoded
  9069. len = asStringEncodeUTF8(val, encodedValue);
  9070. }
  9071. else if( engine->ep.stringEncoding == 1 )
  9072. {
  9073. // Convert to 16bit wide character string (even if the script is scanned as ASCII)
  9074. len = asStringEncodeUTF16(val, encodedValue);
  9075. }
  9076. else
  9077. {
  9078. // Do not convert ASCII characters
  9079. encodedValue[0] = (asBYTE)val;
  9080. len = 1;
  9081. }
  9082. if( len < 0 )
  9083. {
  9084. // Give warning about invalid code point
  9085. // TODO: Need code position for warning
  9086. Warning(TXT_INVALID_UNICODE_VALUE, node);
  9087. }
  9088. else
  9089. {
  9090. // Add the encoded value to the final string
  9091. str.Concatenate(encodedValue, len);
  9092. if( charLiteral == -1 ) charLiteral = val;
  9093. }
  9094. }
  9095. cstr.Assign(str.AddressOf(), str.GetLength());
  9096. return charLiteral;
  9097. }
  9098. void asCCompiler::ProcessHeredocStringConstant(asCString &str, asCScriptNode *node)
  9099. {
  9100. // Remove first line if it only contains whitespace
  9101. bool isMultiline = false;
  9102. int start;
  9103. for( start = 0; start < (int)str.GetLength(); start++ )
  9104. {
  9105. if( str[start] == '\n' )
  9106. {
  9107. isMultiline = true;
  9108. // Remove the linebreak as well
  9109. start++;
  9110. break;
  9111. }
  9112. if( str[start] != ' ' &&
  9113. str[start] != '\t' &&
  9114. str[start] != '\r' )
  9115. {
  9116. // Don't remove anything
  9117. start = 0;
  9118. break;
  9119. }
  9120. }
  9121. // Remove the line after the last line break if it only contains whitespaces
  9122. int end;
  9123. for( end = (int)str.GetLength() - 1; end >= 0; end-- )
  9124. {
  9125. if( str[end] == '\n' )
  9126. {
  9127. // Don't remove the last line break
  9128. end++;
  9129. break;
  9130. }
  9131. if( str[end] != ' ' &&
  9132. str[end] != '\t' &&
  9133. str[end] != '\r' )
  9134. {
  9135. // Don't remove anything
  9136. end = (int)str.GetLength();
  9137. break;
  9138. }
  9139. }
  9140. if( end < 0 ) end = 0;
  9141. asCString tmp;
  9142. if (end > start || engine->ep.heredocTrimMode != 2 )
  9143. {
  9144. // if heredocTrimMode == 0 the string shouldn't be trimmed
  9145. // if heredocTrimMode == 1 the string should only be trimmed if it is multiline
  9146. // if heredocTrimMode == 2 the string should always be trimmed
  9147. if (engine->ep.heredocTrimMode == 2 || (isMultiline && engine->ep.heredocTrimMode == 1))
  9148. tmp.Assign(&str[start], end - start);
  9149. else
  9150. tmp = str;
  9151. }
  9152. ProcessStringConstant(tmp, node, false);
  9153. str = tmp;
  9154. }
  9155. int asCCompiler::CompileConversion(asCScriptNode *node, asCExprContext *ctx)
  9156. {
  9157. asCExprContext expr(engine);
  9158. asCDataType to;
  9159. bool anyErrors = false;
  9160. EImplicitConv convType;
  9161. if( node->nodeType == snConstructCall || node->nodeType == snFunctionCall )
  9162. {
  9163. convType = asIC_EXPLICIT_VAL_CAST;
  9164. // Verify that there is only one argument
  9165. if( node->lastChild->firstChild == 0 ||
  9166. node->lastChild->firstChild != node->lastChild->lastChild )
  9167. {
  9168. Error(TXT_ONLY_ONE_ARGUMENT_IN_CAST, node->lastChild);
  9169. expr.type.SetDummy();
  9170. anyErrors = true;
  9171. }
  9172. else if (node->lastChild->firstChild &&
  9173. node->lastChild->firstChild->nodeType == snNamedArgument)
  9174. {
  9175. Error(TXT_INVALID_USE_OF_NAMED_ARGS, node->lastChild);
  9176. expr.type.SetDummy();
  9177. anyErrors = true;
  9178. }
  9179. else
  9180. {
  9181. // Compile the expression
  9182. int r = CompileAssignment(node->lastChild->firstChild, &expr);
  9183. if( r < 0 )
  9184. anyErrors = true;
  9185. }
  9186. // Determine the requested type
  9187. to = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  9188. to.MakeReadOnly(true); // Default to const
  9189. asASSERT(to.IsPrimitive());
  9190. }
  9191. else
  9192. {
  9193. convType = asIC_EXPLICIT_REF_CAST;
  9194. // Compile the expression
  9195. int r = CompileAssignment(node->lastChild, &expr);
  9196. if( r < 0 )
  9197. anyErrors = true;
  9198. // Determine the requested type
  9199. to = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  9200. // If the type support object handles, then use it
  9201. if( to.SupportHandles() )
  9202. {
  9203. to.MakeHandle(true);
  9204. if( expr.type.dataType.IsObjectConst() )
  9205. to.MakeHandleToConst(true);
  9206. }
  9207. else if( !to.IsObjectHandle() )
  9208. {
  9209. // The cast<type> operator can only be used for reference casts
  9210. Error(TXT_ILLEGAL_TARGET_TYPE_FOR_REF_CAST, node->firstChild);
  9211. anyErrors = true;
  9212. }
  9213. }
  9214. // Do not allow casting to non shared type if we're compiling a shared method
  9215. if( outFunc->IsShared() &&
  9216. to.GetTypeInfo() && !to.GetTypeInfo()->IsShared() )
  9217. {
  9218. asCString msg;
  9219. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, to.GetTypeInfo()->name.AddressOf());
  9220. Error(msg, node);
  9221. anyErrors = true;
  9222. }
  9223. if( anyErrors )
  9224. {
  9225. // Assume that the error can be fixed and allow the compilation to continue
  9226. ctx->type.Set(to);
  9227. return -1;
  9228. }
  9229. if( ProcessPropertyGetAccessor(&expr, node) < 0 )
  9230. return -1;
  9231. // Don't allow any operators on expressions that take address of class method
  9232. if( expr.IsClassMethod() )
  9233. {
  9234. Error(TXT_INVALID_OP_ON_METHOD, node);
  9235. return -1;
  9236. }
  9237. // We don't want a reference for conversion casts
  9238. if( convType == asIC_EXPLICIT_VAL_CAST && expr.type.dataType.IsReference() )
  9239. {
  9240. if( expr.type.dataType.IsObject() )
  9241. Dereference(&expr, true);
  9242. else
  9243. ConvertToVariable(&expr);
  9244. }
  9245. ImplicitConversion(&expr, to, node, convType);
  9246. IsVariableInitialized(&expr.type, node);
  9247. // If no type conversion is really tried ignore it
  9248. if( to == expr.type.dataType )
  9249. {
  9250. // This will keep information about constant type
  9251. MergeExprBytecode(ctx, &expr);
  9252. ctx->type = expr.type;
  9253. return 0;
  9254. }
  9255. if( to.IsEqualExceptRefAndConst(expr.type.dataType) && to.IsPrimitive() )
  9256. {
  9257. MergeExprBytecode(ctx, &expr);
  9258. ctx->type = expr.type;
  9259. ctx->type.dataType.MakeReadOnly(true);
  9260. return 0;
  9261. }
  9262. // The implicit conversion already does most of the conversions permitted,
  9263. // here we'll only treat those conversions that require an explicit cast.
  9264. bool conversionOK = false;
  9265. if( !expr.type.isConstant && expr.type.dataType != asCDataType::CreatePrimitive(ttVoid, false) )
  9266. {
  9267. if( !expr.type.dataType.IsObject() )
  9268. ConvertToTempVariable(&expr);
  9269. if( to.IsObjectHandle() &&
  9270. expr.type.dataType.IsObjectHandle() &&
  9271. !(!to.IsHandleToConst() && expr.type.dataType.IsHandleToConst()) )
  9272. {
  9273. conversionOK = CompileRefCast(&expr, to, true, node);
  9274. MergeExprBytecode(ctx, &expr);
  9275. ctx->type = expr.type;
  9276. }
  9277. }
  9278. if( conversionOK )
  9279. return 0;
  9280. // Conversion not available
  9281. ctx->type.SetDummy();
  9282. asCString strTo, strFrom;
  9283. strTo = to.Format(outFunc->nameSpace);
  9284. strFrom = expr.type.dataType.Format(outFunc->nameSpace);
  9285. asCString msg;
  9286. msg.Format(TXT_NO_CONVERSION_s_TO_s, strFrom.AddressOf(), strTo.AddressOf());
  9287. Error(msg, node);
  9288. return -1;
  9289. }
  9290. void asCCompiler::AfterFunctionCall(int funcID, asCArray<asCExprContext*> &args, asCExprContext *ctx, bool deferAll)
  9291. {
  9292. // deferAll is set to true if for example the function returns a reference, since in
  9293. // this case the function might be returning a reference to one of the arguments.
  9294. asCScriptFunction *descr = builder->GetFunctionDescription(funcID);
  9295. // Parameters that are sent by reference should be assigned
  9296. // to the evaluated expression if it is an lvalue
  9297. // Evaluate the arguments from last to first
  9298. int n = (int)descr->parameterTypes.GetLength() - 1;
  9299. for( ; n >= 0; n-- )
  9300. {
  9301. // All &out arguments must be deferred, except if the argument is clean, in which case the actual reference was passed in to the function
  9302. // If deferAll is set all objects passed by reference or handle must be deferred
  9303. if( (descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] & asTM_OUTREF) && !args[n]->isCleanArg) ||
  9304. (descr->parameterTypes[n].IsObject() && deferAll && (descr->parameterTypes[n].IsReference() || descr->parameterTypes[n].IsObjectHandle())) )
  9305. {
  9306. asASSERT( !(descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] == asTM_OUTREF) && !args[n]->isCleanArg) || args[n]->origExpr );
  9307. // For &inout, only store the argument if it is for a temporary variable
  9308. if( engine->ep.allowUnsafeReferences ||
  9309. descr->inOutFlags[n] != asTM_INOUTREF || args[n]->type.isTemporary )
  9310. {
  9311. // Store the argument for later processing
  9312. asSDeferredParam outParam;
  9313. outParam.argNode = args[n]->exprNode;
  9314. outParam.argType = args[n]->type;
  9315. outParam.argInOutFlags = descr->inOutFlags[n];
  9316. outParam.origExpr = args[n]->origExpr;
  9317. ctx->deferredParams.PushLast(outParam);
  9318. }
  9319. }
  9320. else
  9321. {
  9322. // Release the temporary variable now
  9323. ReleaseTemporaryVariable(args[n]->type, &ctx->bc);
  9324. }
  9325. // Move the argument's deferred expressions over to the final expression
  9326. for( asUINT m = 0; m < args[n]->deferredParams.GetLength(); m++ )
  9327. {
  9328. ctx->deferredParams.PushLast(args[n]->deferredParams[m]);
  9329. args[n]->deferredParams[m].origExpr = 0;
  9330. }
  9331. args[n]->deferredParams.SetLength(0);
  9332. }
  9333. }
  9334. void asCCompiler::ProcessDeferredParams(asCExprContext *ctx)
  9335. {
  9336. if( isProcessingDeferredParams ) return;
  9337. isProcessingDeferredParams = true;
  9338. for( asUINT n = 0; n < ctx->deferredParams.GetLength(); n++ )
  9339. {
  9340. asSDeferredParam outParam = ctx->deferredParams[n];
  9341. if( outParam.argInOutFlags < asTM_OUTREF ) // &in, or not reference
  9342. {
  9343. // Just release the variable
  9344. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  9345. }
  9346. else if( outParam.argInOutFlags == asTM_OUTREF )
  9347. {
  9348. asCExprContext *expr = outParam.origExpr;
  9349. outParam.origExpr = 0;
  9350. if( outParam.argType.dataType.IsObjectHandle() )
  9351. {
  9352. // Implicitly convert the value to a handle
  9353. if( expr->type.dataType.IsObjectHandle() )
  9354. expr->type.isExplicitHandle = true;
  9355. }
  9356. // Verify that the expression result in a lvalue, or a property accessor
  9357. if( IsLValue(expr->type) || expr->property_get || expr->property_set )
  9358. {
  9359. asCExprContext rctx(engine);
  9360. rctx.type = outParam.argType;
  9361. if( rctx.type.dataType.IsPrimitive() )
  9362. rctx.type.dataType.MakeReference(false);
  9363. else
  9364. {
  9365. rctx.bc.InstrSHORT(asBC_PSF, outParam.argType.stackOffset);
  9366. rctx.type.dataType.MakeReference(IsVariableOnHeap(outParam.argType.stackOffset));
  9367. if( expr->type.isExplicitHandle )
  9368. rctx.type.isExplicitHandle = true;
  9369. }
  9370. asCExprContext o(engine);
  9371. DoAssignment(&o, expr, &rctx, outParam.argNode, outParam.argNode, ttAssignment, outParam.argNode);
  9372. if( !o.type.dataType.IsPrimitive() ) o.bc.Instr(asBC_PopPtr);
  9373. // The assignment may itself have resulted in a new temporary variable, e.g. if
  9374. // the opAssign returns a non-reference. We must release this temporary variable
  9375. // since it won't be used
  9376. ReleaseTemporaryVariable(o.type, &o.bc);
  9377. MergeExprBytecode(ctx, &o);
  9378. }
  9379. else
  9380. {
  9381. // We must still evaluate the expression
  9382. MergeExprBytecode(ctx, expr);
  9383. if( !expr->IsVoidExpression() && (!expr->type.isConstant || expr->type.IsNullConstant()) )
  9384. ctx->bc.Instr(asBC_PopPtr);
  9385. // Give an error, except if the argument is void, null or 0 which indicate the argument is explicitly to be ignored
  9386. if( !expr->IsVoidExpression() && !expr->type.IsNullConstant() &&
  9387. !(expr->type.isConstant && expr->type.dataType.IsPrimitive() && expr->type.GetConstantData() == 0) )
  9388. Error(TXT_ARG_NOT_LVALUE, outParam.argNode);
  9389. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  9390. }
  9391. ReleaseTemporaryVariable(expr->type, &ctx->bc);
  9392. // Delete the original expression context
  9393. asDELETE(expr, asCExprContext);
  9394. }
  9395. else // &inout
  9396. {
  9397. if( outParam.argType.isTemporary )
  9398. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  9399. else if( !outParam.argType.isVariable )
  9400. {
  9401. if( outParam.argType.dataType.IsObject() &&
  9402. ((outParam.argType.dataType.GetBehaviour()->addref &&
  9403. outParam.argType.dataType.GetBehaviour()->release) ||
  9404. (outParam.argType.dataType.GetTypeInfo()->flags & asOBJ_NOCOUNT)) )
  9405. {
  9406. // Release the object handle that was taken to guarantee the reference
  9407. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  9408. }
  9409. }
  9410. }
  9411. }
  9412. ctx->deferredParams.SetLength(0);
  9413. isProcessingDeferredParams = false;
  9414. }
  9415. int asCCompiler::CompileConstructCall(asCScriptNode *node, asCExprContext *ctx)
  9416. {
  9417. // The first node is a datatype node
  9418. asCString name;
  9419. asCExprValue tempObj;
  9420. bool onHeap = true;
  9421. asCArray<int> funcs;
  9422. bool error = false;
  9423. // It is possible that the name is really a constructor
  9424. asCDataType dt;
  9425. dt = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace, false, outFunc->objectType);
  9426. if( dt.IsPrimitive() )
  9427. {
  9428. // This is a cast to a primitive type
  9429. return CompileConversion(node, ctx);
  9430. }
  9431. if( dt.GetTypeInfo() && (dt.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE) )
  9432. {
  9433. // Types declared as implicit handle must not attempt to construct a handle
  9434. dt.MakeHandle(false);
  9435. }
  9436. // Don't accept syntax like object@(expr)
  9437. if( dt.IsObjectHandle() )
  9438. {
  9439. asCString str;
  9440. str.Format(TXT_CANT_CONSTRUCT_s_USE_REF_CAST, dt.Format(outFunc->nameSpace).AddressOf());
  9441. Error(str, node);
  9442. ctx->type.SetDummy();
  9443. return -1;
  9444. }
  9445. // Make sure the desired type can actually be instantiated
  9446. // Delegates are allowed to be created through construct calls,
  9447. // even though they cannot be instantiated as variables
  9448. if( !dt.CanBeInstantiated() && !dt.IsFuncdef() )
  9449. {
  9450. asCString str;
  9451. if( dt.IsAbstractClass() )
  9452. str.Format(TXT_ABSTRACT_CLASS_s_CANNOT_BE_INSTANTIATED, dt.Format(outFunc->nameSpace).AddressOf());
  9453. else if( dt.IsInterface() )
  9454. str.Format(TXT_INTERFACE_s_CANNOT_BE_INSTANTIATED, dt.Format(outFunc->nameSpace).AddressOf());
  9455. else
  9456. // TODO: Improve error message to explain why
  9457. str.Format(TXT_DATA_TYPE_CANT_BE_s, dt.Format(outFunc->nameSpace).AddressOf());
  9458. Error(str, node);
  9459. ctx->type.SetDummy();
  9460. return -1;
  9461. }
  9462. // Do not allow constructing non-shared types in shared functions
  9463. if( outFunc->IsShared() &&
  9464. dt.GetTypeInfo() && !dt.GetTypeInfo()->IsShared() )
  9465. {
  9466. asCString msg;
  9467. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetTypeInfo()->name.AddressOf());
  9468. Error(msg, node);
  9469. return -1;
  9470. }
  9471. // Compile the arguments
  9472. asCArray<asCExprContext *> args;
  9473. asCArray<asSNamedArgument> namedArgs;
  9474. asCArray<asCExprValue> temporaryVariables;
  9475. if( CompileArgumentList(node->lastChild, args, namedArgs) >= 0 )
  9476. {
  9477. // Check for a value cast behaviour
  9478. if( args.GetLength() == 1 )
  9479. {
  9480. asCExprContext conv(engine);
  9481. conv.Copy(args[0]);
  9482. asUINT cost = ImplicitConversion(&conv, dt, node->lastChild, asIC_EXPLICIT_VAL_CAST, false);
  9483. // Clean the property_arg in the temporary copy so
  9484. // it isn't deleted when conv goes out of scope
  9485. conv.property_arg = 0;
  9486. // Don't use this if the cost is 0 because it would mean that nothing
  9487. // is done and the script wants a new value to be constructed
  9488. if( conv.type.dataType.IsEqualExceptRef(dt) && cost > 0 )
  9489. {
  9490. // Make sure the result is a reference, just as if to a local variable
  9491. if( !dt.IsFuncdef() )
  9492. dt.MakeReference(true);
  9493. // Make sure any property accessor is already evaluated
  9494. if( ProcessPropertyGetAccessor(args[0], args[0]->exprNode) < 0 )
  9495. return -1;
  9496. ImplicitConversion(args[0], dt, node->lastChild, asIC_EXPLICIT_VAL_CAST);
  9497. ctx->bc.AddCode(&args[0]->bc);
  9498. ctx->type = args[0]->type;
  9499. asDELETE(args[0], asCExprContext);
  9500. return 0;
  9501. }
  9502. }
  9503. // Check for possible constructor/factory
  9504. name = dt.Format(outFunc->nameSpace);
  9505. asSTypeBehaviour *beh = dt.GetBehaviour();
  9506. if( !(dt.GetTypeInfo()->flags & asOBJ_REF) && !dt.IsFuncdef() )
  9507. {
  9508. funcs = beh->constructors;
  9509. // Value types and script types are allocated through the constructor
  9510. tempObj.dataType = dt;
  9511. tempObj.stackOffset = (short)AllocateVariable(dt, true);
  9512. tempObj.dataType.MakeReference(true);
  9513. tempObj.isTemporary = true;
  9514. tempObj.isVariable = true;
  9515. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  9516. // Push the address of the object on the stack
  9517. if( onHeap )
  9518. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  9519. }
  9520. else if( beh )
  9521. funcs = beh->factories;
  9522. // Special case: Allow calling func(void) with a void expression.
  9523. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  9524. {
  9525. // Evaluate the expression before the function call
  9526. MergeExprBytecode(ctx, args[0]);
  9527. asDELETE(args[0], asCExprContext);
  9528. args.SetLength(0);
  9529. }
  9530. // Special case: If this is an object constructor and there are no arguments use the default constructor.
  9531. // If none has been registered, just allocate the variable and push it on the stack.
  9532. if( args.GetLength() == 0 )
  9533. {
  9534. beh = tempObj.dataType.GetBehaviour();
  9535. if( beh && beh->construct == 0 && !(dt.GetTypeInfo()->flags & asOBJ_REF) )
  9536. {
  9537. // Call the default constructor
  9538. ctx->type = tempObj;
  9539. if( onHeap )
  9540. {
  9541. asASSERT(ctx->bc.GetLastInstr() == asBC_VAR);
  9542. ctx->bc.RemoveLastInstr();
  9543. }
  9544. CallDefaultConstructor(tempObj.dataType, tempObj.stackOffset, IsVariableOnHeap(tempObj.stackOffset), &ctx->bc, node);
  9545. // Push the reference on the stack
  9546. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  9547. return 0;
  9548. }
  9549. }
  9550. // Special case: If this is a construction of a delegate and the expression names an object method
  9551. if( dt.IsFuncdef() && args.GetLength() == 1 && args[0]->methodName != "" )
  9552. {
  9553. // TODO: delegate: It is possible that the argument returns a function pointer already, in which
  9554. // case no object delegate will be created, but instead a delegate for a function pointer
  9555. // In theory a simple cast would be good in this case, but this is a construct call so it
  9556. // is expected that a new object is created.
  9557. dt.MakeHandle(true);
  9558. ctx->type.Set(dt);
  9559. // The delegate must be able to hold on to a reference to the object
  9560. if( !args[0]->type.dataType.SupportHandles() )
  9561. {
  9562. Error(TXT_CANNOT_CREATE_DELEGATE_FOR_NOREF_TYPES, node);
  9563. error = true;
  9564. }
  9565. else
  9566. {
  9567. // Filter the available object methods to find the one that matches the func def
  9568. asCObjectType *type = CastToObjectType(args[0]->type.dataType.GetTypeInfo());
  9569. asCScriptFunction *bestMethod = 0;
  9570. for( asUINT n = 0; n < type->methods.GetLength(); n++ )
  9571. {
  9572. asCScriptFunction *func = engine->scriptFunctions[type->methods[n]];
  9573. if( func->name != args[0]->methodName )
  9574. continue;
  9575. // If the expression is for a const object, then only const methods should be accepted
  9576. if( args[0]->type.dataType.IsReadOnly() && !func->IsReadOnly() )
  9577. continue;
  9578. if( func->IsSignatureExceptNameAndObjectTypeEqual(CastToFuncdefType(dt.GetTypeInfo())->funcdef) )
  9579. {
  9580. bestMethod = func;
  9581. // If the expression is non-const the non-const overloaded method has priority
  9582. if( args[0]->type.dataType.IsReadOnly() == func->IsReadOnly() )
  9583. break;
  9584. }
  9585. }
  9586. if( bestMethod )
  9587. {
  9588. // The object pointer is already on the stack
  9589. MergeExprBytecode(ctx, args[0]);
  9590. // Push the function pointer as an additional argument
  9591. ctx->bc.InstrPTR(asBC_FuncPtr, bestMethod);
  9592. // Call the factory function for the delegate
  9593. asCArray<int> delegateFuncs;
  9594. builder->GetFunctionDescriptions(DELEGATE_FACTORY, delegateFuncs, engine->nameSpaces[0]);
  9595. asASSERT(delegateFuncs.GetLength() == 1 );
  9596. ctx->bc.Call(asBC_CALLSYS , delegateFuncs[0], 2*AS_PTR_SIZE);
  9597. // Store the returned delegate in a temporary variable
  9598. int returnOffset = AllocateVariable(dt, true, false);
  9599. dt.MakeReference(true);
  9600. ctx->type.SetVariable(dt, returnOffset, true);
  9601. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  9602. // Push a reference to the temporary variable on the stack
  9603. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  9604. // Clean up arguments
  9605. ReleaseTemporaryVariable(args[0]->type, &ctx->bc);
  9606. }
  9607. else
  9608. {
  9609. asCString msg;
  9610. msg.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, CastToFuncdefType(dt.GetTypeInfo())->funcdef->GetDeclaration());
  9611. Error(msg.AddressOf(), node);
  9612. error = true;
  9613. }
  9614. }
  9615. // Clean-up arg
  9616. asDELETE(args[0], asCExprContext);
  9617. return error ? -1 : 0;
  9618. }
  9619. MatchFunctions(funcs, args, node, name.AddressOf(), &namedArgs, 0, false);
  9620. if( funcs.GetLength() != 1 )
  9621. {
  9622. // The error was reported by MatchFunctions()
  9623. error = true;
  9624. // Dummy value
  9625. ctx->type.SetDummy();
  9626. }
  9627. else
  9628. {
  9629. // TODO: Clean up: Merge this with MakeFunctionCall
  9630. // Add the default values for arguments not explicitly supplied
  9631. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(dt.GetTypeInfo()), &namedArgs);
  9632. if( r == asSUCCESS )
  9633. {
  9634. asCByteCode objBC(engine);
  9635. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  9636. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  9637. if( !(dt.GetTypeInfo()->flags & asOBJ_REF) )
  9638. {
  9639. // If the object is allocated on the stack, then call the constructor as a normal function
  9640. if( onHeap )
  9641. {
  9642. int offset = 0;
  9643. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  9644. for( asUINT n = 0; n < args.GetLength(); n++ )
  9645. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  9646. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  9647. }
  9648. else
  9649. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  9650. PerformFunctionCall(funcs[0], ctx, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  9651. // Add tag that the object has been initialized
  9652. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  9653. // The constructor doesn't return anything,
  9654. // so we have to manually inform the type of
  9655. // the return value
  9656. ctx->type = tempObj;
  9657. if( !onHeap )
  9658. ctx->type.dataType.MakeReference(false);
  9659. // Push the address of the object on the stack again
  9660. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  9661. }
  9662. else
  9663. {
  9664. // Call the factory to create the reference type
  9665. PerformFunctionCall(funcs[0], ctx, false, &args);
  9666. }
  9667. }
  9668. else
  9669. error = true;
  9670. }
  9671. }
  9672. else
  9673. {
  9674. // Failed to compile the argument list, set the result to the dummy type
  9675. ctx->type.SetDummy();
  9676. error = true;
  9677. }
  9678. // Cleanup
  9679. for( asUINT n = 0; n < args.GetLength(); n++ )
  9680. if( args[n] )
  9681. {
  9682. asDELETE(args[n], asCExprContext);
  9683. }
  9684. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  9685. if( namedArgs[n].ctx )
  9686. {
  9687. asDELETE(namedArgs[n].ctx, asCExprContext);
  9688. }
  9689. return error ? -1 : 0;
  9690. }
  9691. int asCCompiler::CompileFunctionCall(asCScriptNode *node, asCExprContext *ctx, asCObjectType *objectType, bool objIsConst, const asCString &scope)
  9692. {
  9693. asCExprValue tempObj;
  9694. asCArray<int> funcs;
  9695. int localVar = -1;
  9696. bool initializeMembers = false;
  9697. asCExprContext funcExpr(engine);
  9698. asCScriptNode *nm = node->lastChild->prev;
  9699. asCString name(&script->code[nm->tokenPos], nm->tokenLength);
  9700. // Find the matching entities
  9701. // If objectType is set then this is a post op expression and we shouldn't look for local variables
  9702. asCExprContext lookupResult(engine);
  9703. SYMBOLTYPE symbolType = SymbolLookup(name, scope, objectType, &lookupResult);
  9704. if (symbolType < 0)
  9705. return -1;
  9706. if (symbolType == SL_NOMATCH)
  9707. {
  9708. // No matching symbol
  9709. asCString msg;
  9710. asCString smbl;
  9711. if (scope == "::")
  9712. smbl = scope;
  9713. else if (scope != "")
  9714. smbl = scope + "::";
  9715. smbl += name;
  9716. msg.Format(TXT_NO_MATCHING_SYMBOL_s, smbl.AddressOf());
  9717. Error(msg, node);
  9718. return -1;
  9719. }
  9720. // Is the symbol matching a variable/property?
  9721. if (symbolType == SL_LOCALCONST || symbolType == SL_LOCALVAR ||
  9722. symbolType == SL_THISPTR || symbolType == SL_CLASSPROPACCESS || symbolType == SL_CLASSPROP ||
  9723. symbolType == SL_GLOBALPROPACCESS || symbolType == SL_GLOBALCONST || symbolType == SL_GLOBALVAR || symbolType == SL_ENUMVAL)
  9724. {
  9725. // Variables/properties can be used as functions if they have the opCall
  9726. // Compile the variable
  9727. // TODO: Take advantage of the known symbol, so it doesn't have to be looked up again
  9728. localVar = CompileVariableAccess(name, scope, &funcExpr, node, false, objectType);
  9729. if( localVar < 0 )
  9730. return -1;
  9731. if (funcExpr.type.dataType.IsFuncdef())
  9732. {
  9733. funcs.PushLast(CastToFuncdefType(funcExpr.type.dataType.GetTypeInfo())->funcdef->id);
  9734. }
  9735. else if (funcExpr.type.dataType.IsObject())
  9736. {
  9737. // Keep information about temporary variables as deferred expression so it can be properly cleaned up after the call
  9738. if (ctx->type.isTemporary)
  9739. {
  9740. asASSERT(objectType);
  9741. asSDeferredParam deferred;
  9742. deferred.origExpr = 0;
  9743. deferred.argInOutFlags = asTM_INREF;
  9744. deferred.argNode = 0;
  9745. deferred.argType.SetVariable(ctx->type.dataType, ctx->type.stackOffset, true);
  9746. ctx->deferredParams.PushLast(deferred);
  9747. }
  9748. if (funcExpr.property_get == 0)
  9749. Dereference(ctx, true);
  9750. // Add the bytecode for accessing the object on which opCall will be called
  9751. if (ctx->type.dataType.IsObject())
  9752. {
  9753. // Make sure the ProcessPropertyGetAccess knows whether or not to
  9754. // dereference the original object before calling the get accessor
  9755. funcExpr.property_ref = ctx->type.dataType.IsReference();
  9756. }
  9757. MergeExprBytecodeAndType(ctx, &funcExpr);
  9758. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  9759. return -1;
  9760. Dereference(ctx, true);
  9761. objectType = CastToObjectType(funcExpr.type.dataType.GetTypeInfo());
  9762. // Get the opCall methods from the object type
  9763. if (funcExpr.type.dataType.IsObjectHandle())
  9764. objIsConst = funcExpr.type.dataType.IsHandleToConst();
  9765. else
  9766. objIsConst = funcExpr.type.dataType.IsReadOnly();
  9767. builder->GetObjectMethodDescriptions("opCall", CastToObjectType(funcExpr.type.dataType.GetTypeInfo()), funcs, objIsConst);
  9768. }
  9769. else
  9770. {
  9771. // The variable is not a function or object with opCall
  9772. asCString msg;
  9773. msg.Format(TXT_NOT_A_FUNC_s_IS_TYPE_s, name.AddressOf(), lookupResult.type.dataType.Format(outFunc->nameSpace).AddressOf());
  9774. Error(msg, node);
  9775. return -1;
  9776. }
  9777. }
  9778. // Is the symbol matching a class method?
  9779. if (symbolType == SL_CLASSMETHOD)
  9780. {
  9781. // If we're compiling a constructor and the name of the function is super then
  9782. // the constructor of the base class is being called.
  9783. // super cannot be prefixed with a scope operator
  9784. if (scope == "" && m_isConstructor && name == SUPER_TOKEN)
  9785. {
  9786. // If the class is not derived from anyone else, calling super should give an error
  9787. if (outFunc && outFunc->objectType->derivedFrom)
  9788. funcs = outFunc->objectType->derivedFrom->beh.constructors;
  9789. // Must not allow calling base class' constructor multiple times
  9790. if (continueLabels.GetLength() > 0)
  9791. {
  9792. // If a continue label is set we are in a loop
  9793. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_LOOPS, node);
  9794. }
  9795. else if (breakLabels.GetLength() > 0)
  9796. {
  9797. // TODO: inheritance: Should eventually allow constructors in switch statements
  9798. // If a break label is set we are either in a loop or a switch statements
  9799. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_SWITCH, node);
  9800. }
  9801. else if (m_isConstructorCalled)
  9802. {
  9803. Error(TXT_CANNOT_CALL_CONSTRUCTOR_TWICE, node);
  9804. }
  9805. m_isConstructorCalled = true;
  9806. // We need to initialize the class members, but only after all the deferred arguments have been completed
  9807. initializeMembers = true;
  9808. }
  9809. else
  9810. {
  9811. // The scope can be used to specify the base class
  9812. builder->GetObjectMethodDescriptions(name.AddressOf(), CastToObjectType(lookupResult.type.dataType.GetTypeInfo()), funcs, objIsConst, scope, node, script);
  9813. }
  9814. // If a class method is being called implicitly, then add the this pointer for the call
  9815. if (funcs.GetLength() && !objectType && outFunc->objectType)
  9816. {
  9817. // Verify that the identified function is actually part of the class hierarchy
  9818. if (!outFunc->objectType->DerivesFrom(lookupResult.type.dataType.GetTypeInfo()))
  9819. {
  9820. asCString msg;
  9821. asCString mthd;
  9822. if (scope == "")
  9823. mthd = name;
  9824. else if (scope == "::")
  9825. mthd = scope + name;
  9826. else
  9827. mthd = scope + "::" + name;
  9828. msg.Format(TXT_METHOD_s_NOT_PART_OF_OBJECT_s, mthd.AddressOf(), outFunc->objectType->name.AddressOf());
  9829. Error(msg, node);
  9830. return -1;
  9831. }
  9832. objectType = outFunc->objectType;
  9833. asCDataType dt = asCDataType::CreateType(objectType, false);
  9834. // The object pointer is located at stack position 0
  9835. ctx->bc.InstrSHORT(asBC_PSF, 0);
  9836. ctx->type.SetVariable(dt, 0, false);
  9837. ctx->type.dataType.MakeReference(true);
  9838. Dereference(ctx, true);
  9839. }
  9840. else if (funcs.GetLength() && !objectType && !outFunc->objectType)
  9841. {
  9842. // Cannot call class methods directly without the object
  9843. asCString msg;
  9844. msg.Format(TXT_CANNOT_ACCESS_NON_STATIC_MEMBER_s, name.AddressOf());
  9845. Error(msg, node);
  9846. return -1;
  9847. }
  9848. }
  9849. // Is it a global function?
  9850. if (symbolType == SL_GLOBALFUNC)
  9851. {
  9852. // The symbol lookup identified the namespace to use
  9853. int n = lookupResult.methodName.FindLast("::");
  9854. asSNameSpace *ns = engine->FindNameSpace(lookupResult.methodName.SubString(0, n).AddressOf());
  9855. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  9856. }
  9857. // Is it a type?
  9858. if (symbolType == SL_CLASSTYPE || symbolType == SL_GLOBALTYPE)
  9859. {
  9860. bool isValid = false;
  9861. asCDataType dt = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace, false, outFunc->objectType, false, &isValid);
  9862. if (isValid)
  9863. return CompileConstructCall(node, ctx);
  9864. }
  9865. // Compile the arguments
  9866. asCArray<asCExprContext *> args;
  9867. asCArray<asSNamedArgument> namedArgs;
  9868. bool isOK = true;
  9869. if( CompileArgumentList(node->lastChild, args, namedArgs) >= 0 )
  9870. {
  9871. // Special case: Allow calling func(void) with an expression that evaluates to no datatype, but isn't exactly 'void'
  9872. if( args.GetLength() == 1 && args[0]->type.IsVoid() && !args[0]->IsVoidExpression() )
  9873. {
  9874. // Evaluate the expression before the function call
  9875. MergeExprBytecode(ctx, args[0]);
  9876. asDELETE(args[0], asCExprContext);
  9877. args.SetLength(0);
  9878. }
  9879. MatchFunctions(funcs, args, node, name.AddressOf(), &namedArgs, objectType, objIsConst, false, true, scope);
  9880. if( funcs.GetLength() != 1 )
  9881. {
  9882. // The error was reported by MatchFunctions()
  9883. // Dummy value
  9884. ctx->type.SetDummy();
  9885. isOK = false;
  9886. }
  9887. else
  9888. {
  9889. // Add the default values for arguments not explicitly supplied
  9890. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], objectType, &namedArgs);
  9891. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  9892. // is it enough to make sure it is in a local variable?
  9893. // For function pointer we must guarantee that the function is safe, i.e.
  9894. // by first storing the function pointer in a local variable (if it isn't already in one)
  9895. if( r == asSUCCESS )
  9896. {
  9897. asCScriptFunction *func = builder->GetFunctionDescription(funcs[0]);
  9898. if( func->funcType == asFUNC_FUNCDEF )
  9899. {
  9900. if( objectType && funcExpr.property_get <= 0 )
  9901. {
  9902. // Dereference the object pointer to access the member
  9903. Dereference(ctx, true);
  9904. }
  9905. if( funcExpr.property_get > 0 )
  9906. {
  9907. if( ProcessPropertyGetAccessor(&funcExpr, node) < 0 )
  9908. return -1;
  9909. Dereference(&funcExpr, true);
  9910. }
  9911. else
  9912. {
  9913. Dereference(&funcExpr, true);
  9914. ConvertToVariable(&funcExpr);
  9915. }
  9916. // The actual function should be called as if a global function
  9917. objectType = 0;
  9918. // The function call will be made directly from the local variable so the function pointer shouldn't be on the stack
  9919. funcExpr.bc.Instr(asBC_PopPtr);
  9920. asCExprValue tmp = ctx->type;
  9921. MergeExprBytecodeAndType(ctx, &funcExpr);
  9922. ReleaseTemporaryVariable(tmp, &ctx->bc);
  9923. }
  9924. r = MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, funcExpr.type.stackOffset);
  9925. if( r < 0 )
  9926. {
  9927. ctx->type.SetDummy();
  9928. isOK = false;
  9929. }
  9930. }
  9931. else
  9932. isOK = false;
  9933. }
  9934. }
  9935. else
  9936. {
  9937. // Failed to compile the argument list, set the dummy type and continue compilation
  9938. ctx->type.SetDummy();
  9939. isOK = false;
  9940. }
  9941. // Cleanup
  9942. for( asUINT n = 0; n < args.GetLength(); n++ )
  9943. if( args[n] )
  9944. {
  9945. asDELETE(args[n], asCExprContext);
  9946. }
  9947. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  9948. if( namedArgs[n].ctx )
  9949. {
  9950. asDELETE(namedArgs[n].ctx, asCExprContext);
  9951. }
  9952. if( initializeMembers )
  9953. {
  9954. asASSERT( m_isConstructor );
  9955. // Need to initialize members here, as they may use the properties of the base class
  9956. // If there are multiple paths that call super(), then there will also be multiple
  9957. // locations with initializations of the members. It is not possible to consolidate
  9958. // these in one place, as the expressions for the initialization are evaluated where
  9959. // they are compiled, which means that they may access different variables depending
  9960. // on the scope where super() is called.
  9961. // Members that don't have an explicit initialization expression will be initialized
  9962. // beginning of the constructor as they are guaranteed not to use at the any
  9963. // members of the base class.
  9964. CompileMemberInitialization(&ctx->bc, false);
  9965. }
  9966. return isOK ? 0 : -1;
  9967. }
  9968. asSNameSpace *asCCompiler::DetermineNameSpace(const asCString &scope)
  9969. {
  9970. asSNameSpace *ns;
  9971. if( scope == "" )
  9972. {
  9973. // When compiling default argument expression the correct namespace is stored in the outFunc even for objects
  9974. if( outFunc->nameSpace->name != "" || isCompilingDefaultArg )
  9975. ns = outFunc->nameSpace;
  9976. else if( outFunc->objectType && outFunc->objectType->nameSpace->name != "" )
  9977. ns = outFunc->objectType->nameSpace;
  9978. else
  9979. ns = engine->nameSpaces[0];
  9980. }
  9981. else if( scope == "::" )
  9982. ns = engine->nameSpaces[0];
  9983. else
  9984. ns = engine->FindNameSpace(scope.AddressOf());
  9985. return ns;
  9986. }
  9987. int asCCompiler::CompileExpressionPreOp(asCScriptNode *node, asCExprContext *ctx)
  9988. {
  9989. int op = node->tokenType;
  9990. // Don't allow any prefix operators except handle on expressions that take address of class method
  9991. if( ctx->IsClassMethod() && op != ttHandle )
  9992. {
  9993. Error(TXT_INVALID_OP_ON_METHOD, node);
  9994. return -1;
  9995. }
  9996. // Don't allow any operators on void expressions
  9997. if( ctx->IsVoidExpression() )
  9998. {
  9999. Error(TXT_VOID_CANT_BE_OPERAND, node);
  10000. return -1;
  10001. }
  10002. IsVariableInitialized(&ctx->type, node);
  10003. if( op == ttHandle )
  10004. {
  10005. if( ctx->methodName != "" )
  10006. {
  10007. // Don't allow taking the handle of a handle
  10008. if( ctx->type.isExplicitHandle )
  10009. {
  10010. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  10011. return -1;
  10012. }
  10013. }
  10014. else
  10015. {
  10016. // Don't allow taking handle of a handle, i.e. @@
  10017. if( ctx->type.isExplicitHandle )
  10018. {
  10019. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  10020. return -1;
  10021. }
  10022. // @null is allowed even though it is implicit
  10023. if( !ctx->type.IsNullConstant() )
  10024. {
  10025. // Verify that the type allow its handle to be taken
  10026. if( !ctx->type.dataType.SupportHandles() && !ctx->type.dataType.IsObjectHandle() )
  10027. {
  10028. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  10029. return -1;
  10030. }
  10031. // Objects that are not local variables are not references
  10032. // Objects allocated on the stack are also not marked as references
  10033. if( !ctx->type.dataType.IsReference() &&
  10034. !((ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && !ctx->type.isVariable) &&
  10035. !(ctx->type.isVariable && !IsVariableOnHeap(ctx->type.stackOffset)) )
  10036. {
  10037. Error(TXT_NOT_VALID_REFERENCE, node);
  10038. return -1;
  10039. }
  10040. // Convert the expression to a handle
  10041. if( !ctx->type.dataType.IsObjectHandle() && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  10042. {
  10043. asCDataType to = ctx->type.dataType;
  10044. to.MakeHandle(true);
  10045. to.MakeReference(true);
  10046. to.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  10047. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV, true, false);
  10048. asASSERT( ctx->type.dataType.IsObjectHandle() );
  10049. }
  10050. else if( ctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE )
  10051. {
  10052. // For the ASHANDLE type we'll simply set the expression as a handle
  10053. ctx->type.dataType.MakeHandle(true);
  10054. }
  10055. }
  10056. }
  10057. // Mark the expression as an explicit handle to avoid implicit conversions to non-handle expressions
  10058. ctx->type.isExplicitHandle = true;
  10059. }
  10060. else if( (op == ttMinus || op == ttPlus || op == ttBitNot || op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  10061. {
  10062. // Look for the appropriate method
  10063. // There is no overloadable operator for unary plus
  10064. const char *opName = 0;
  10065. switch( op )
  10066. {
  10067. case ttMinus: opName = "opNeg"; break;
  10068. case ttBitNot: opName = "opCom"; break;
  10069. case ttInc: opName = "opPreInc"; break;
  10070. case ttDec: opName = "opPreDec"; break;
  10071. }
  10072. if( opName )
  10073. {
  10074. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  10075. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  10076. return -1;
  10077. // TODO: If the value isn't const, then first try to find the non const method, and if not found try to find the const method
  10078. // Find the correct method
  10079. bool isConst = ctx->type.dataType.IsObjectConst();
  10080. asCArray<int> funcs;
  10081. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  10082. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  10083. {
  10084. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  10085. if( func->name == opName &&
  10086. func->parameterTypes.GetLength() == 0 &&
  10087. (!isConst || func->IsReadOnly()) )
  10088. {
  10089. funcs.PushLast(func->id);
  10090. }
  10091. }
  10092. // Did we find the method?
  10093. if( funcs.GetLength() == 1 )
  10094. {
  10095. asCArray<asCExprContext *> args;
  10096. return MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  10097. }
  10098. else if( funcs.GetLength() == 0 )
  10099. {
  10100. asCString str;
  10101. str = asCString(opName) + "()";
  10102. if( isConst )
  10103. str += " const";
  10104. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  10105. Error(str, node);
  10106. ctx->type.SetDummy();
  10107. return -1;
  10108. }
  10109. else if( funcs.GetLength() > 1 )
  10110. {
  10111. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  10112. PrintMatchingFuncs(funcs, node);
  10113. ctx->type.SetDummy();
  10114. return -1;
  10115. }
  10116. }
  10117. else if( op == ttPlus )
  10118. {
  10119. Error(TXT_ILLEGAL_OPERATION, node);
  10120. ctx->type.SetDummy();
  10121. return -1;
  10122. }
  10123. }
  10124. else if( op == ttPlus || op == ttMinus )
  10125. {
  10126. // This is only for primitives. Objects are treated in the above block
  10127. // Make sure the type is a math type
  10128. if( !(ctx->type.dataType.IsIntegerType() ||
  10129. ctx->type.dataType.IsUnsignedType() ||
  10130. ctx->type.dataType.IsFloatType() ||
  10131. ctx->type.dataType.IsDoubleType() ) )
  10132. {
  10133. Error(TXT_ILLEGAL_OPERATION, node);
  10134. return -1;
  10135. }
  10136. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  10137. return -1;
  10138. asCDataType to = ctx->type.dataType;
  10139. if( ctx->type.dataType.IsUnsignedType() )
  10140. {
  10141. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  10142. to = asCDataType::CreatePrimitive(ttInt8, false);
  10143. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  10144. to = asCDataType::CreatePrimitive(ttInt16, false);
  10145. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  10146. to = asCDataType::CreatePrimitive(ttInt, false);
  10147. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  10148. to = asCDataType::CreatePrimitive(ttInt64, false);
  10149. else
  10150. {
  10151. Error(TXT_INVALID_TYPE, node);
  10152. return -1;
  10153. }
  10154. }
  10155. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  10156. // Use an explicit conversion in case of constants to avoid unnecessary warning about change of sign
  10157. ImplicitConversion(ctx, to, node, ctx->type.isConstant ? asIC_EXPLICIT_VAL_CAST : asIC_IMPLICIT_CONV);
  10158. if( !ctx->type.isConstant )
  10159. {
  10160. ConvertToTempVariable(ctx);
  10161. asASSERT(!ctx->type.isLValue);
  10162. if( op == ttMinus )
  10163. {
  10164. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10165. ctx->bc.InstrSHORT(asBC_NEGi, ctx->type.stackOffset);
  10166. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10167. ctx->bc.InstrSHORT(asBC_NEGi64, ctx->type.stackOffset);
  10168. else if( ctx->type.dataType.IsFloatType() )
  10169. ctx->bc.InstrSHORT(asBC_NEGf, ctx->type.stackOffset);
  10170. else if( ctx->type.dataType.IsDoubleType() )
  10171. ctx->bc.InstrSHORT(asBC_NEGd, ctx->type.stackOffset);
  10172. else
  10173. {
  10174. Error(TXT_ILLEGAL_OPERATION, node);
  10175. return -1;
  10176. }
  10177. return 0;
  10178. }
  10179. }
  10180. else
  10181. {
  10182. if( op == ttMinus )
  10183. {
  10184. if (ctx->type.dataType.IsIntegerType())
  10185. {
  10186. if (ctx->type.dataType.GetSizeInMemoryBytes() == 4)
  10187. ctx->type.SetConstantDW(-(int)ctx->type.GetConstantDW());
  10188. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 2)
  10189. ctx->type.SetConstantW(-(asINT16)ctx->type.GetConstantW());
  10190. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 1)
  10191. ctx->type.SetConstantB(-(asINT8)ctx->type.GetConstantB());
  10192. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 8)
  10193. ctx->type.SetConstantQW(-(asINT64)ctx->type.GetConstantQW());
  10194. }
  10195. else if( ctx->type.dataType.IsFloatType() )
  10196. ctx->type.SetConstantF(-ctx->type.GetConstantF());
  10197. else if( ctx->type.dataType.IsDoubleType() )
  10198. ctx->type.SetConstantD(-ctx->type.GetConstantD());
  10199. else
  10200. {
  10201. Error(TXT_ILLEGAL_OPERATION, node);
  10202. return -1;
  10203. }
  10204. return 0;
  10205. }
  10206. }
  10207. }
  10208. else if( op == ttNot )
  10209. {
  10210. // Allow value types to be converted to bool using 'bool opImplConv()'
  10211. if( ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  10212. ImplicitConversion(ctx, asCDataType::CreatePrimitive(ttBool, false), node, asIC_IMPLICIT_CONV);
  10213. if( ctx->type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  10214. {
  10215. if( ctx->type.isConstant )
  10216. {
  10217. #if AS_SIZEOF_BOOL == 1
  10218. ctx->type.SetConstantB(ctx->type.GetConstantB() == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10219. #else
  10220. ctx->type.SetConstantDW(ctx->type.GetConstantDW() == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10221. #endif
  10222. return 0;
  10223. }
  10224. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  10225. return -1;
  10226. ConvertToTempVariable(ctx);
  10227. asASSERT(!ctx->type.isLValue);
  10228. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  10229. }
  10230. else
  10231. {
  10232. Error(TXT_ILLEGAL_OPERATION, node);
  10233. return -1;
  10234. }
  10235. }
  10236. else if( op == ttBitNot )
  10237. {
  10238. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  10239. return -1;
  10240. asCDataType to = ctx->type.dataType;
  10241. if( ctx->type.dataType.IsIntegerType() )
  10242. {
  10243. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  10244. to = asCDataType::CreatePrimitive(ttUInt8, false);
  10245. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  10246. to = asCDataType::CreatePrimitive(ttUInt16, false);
  10247. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  10248. to = asCDataType::CreatePrimitive(ttUInt, false);
  10249. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  10250. to = asCDataType::CreatePrimitive(ttUInt64, false);
  10251. else
  10252. {
  10253. Error(TXT_INVALID_TYPE, node);
  10254. return -1;
  10255. }
  10256. }
  10257. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  10258. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  10259. if( ctx->type.dataType.IsUnsignedType() )
  10260. {
  10261. if( ctx->type.isConstant )
  10262. {
  10263. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  10264. ctx->type.SetConstantB(~ctx->type.GetConstantB());
  10265. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 2)
  10266. ctx->type.SetConstantW(~ctx->type.GetConstantW());
  10267. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 4)
  10268. ctx->type.SetConstantDW(~ctx->type.GetConstantDW());
  10269. else
  10270. ctx->type.SetConstantQW(~ctx->type.GetConstantQW());
  10271. return 0;
  10272. }
  10273. ConvertToTempVariable(ctx);
  10274. asASSERT(!ctx->type.isLValue);
  10275. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10276. ctx->bc.InstrSHORT(asBC_BNOT, ctx->type.stackOffset);
  10277. else
  10278. ctx->bc.InstrSHORT(asBC_BNOT64, ctx->type.stackOffset);
  10279. }
  10280. else
  10281. {
  10282. Error(TXT_ILLEGAL_OPERATION, node);
  10283. return -1;
  10284. }
  10285. }
  10286. else if( op == ttInc || op == ttDec )
  10287. {
  10288. // Need a reference to the primitive that will be updated
  10289. // The result of this expression is the same reference as before
  10290. // Make sure the reference isn't a temporary variable
  10291. if( ctx->type.isTemporary )
  10292. {
  10293. Error(TXT_REF_IS_TEMP, node);
  10294. return -1;
  10295. }
  10296. if( ctx->type.dataType.IsReadOnly() )
  10297. {
  10298. Error(TXT_REF_IS_READ_ONLY, node);
  10299. return -1;
  10300. }
  10301. if( ctx->property_get || ctx->property_set )
  10302. {
  10303. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  10304. return -1;
  10305. }
  10306. if( !ctx->type.isLValue )
  10307. {
  10308. Error(TXT_NOT_LVALUE, node);
  10309. return -1;
  10310. }
  10311. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  10312. ConvertToReference(ctx);
  10313. else if( !ctx->type.dataType.IsReference() )
  10314. {
  10315. Error(TXT_NOT_VALID_REFERENCE, node);
  10316. return -1;
  10317. }
  10318. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  10319. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  10320. {
  10321. if( op == ttInc )
  10322. ctx->bc.Instr(asBC_INCi64);
  10323. else
  10324. ctx->bc.Instr(asBC_DECi64);
  10325. }
  10326. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt, false)) ||
  10327. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt, false)) )
  10328. {
  10329. if( op == ttInc )
  10330. ctx->bc.Instr(asBC_INCi);
  10331. else
  10332. ctx->bc.Instr(asBC_DECi);
  10333. }
  10334. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  10335. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  10336. {
  10337. if( op == ttInc )
  10338. ctx->bc.Instr(asBC_INCi16);
  10339. else
  10340. ctx->bc.Instr(asBC_DECi16);
  10341. }
  10342. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  10343. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  10344. {
  10345. if( op == ttInc )
  10346. ctx->bc.Instr(asBC_INCi8);
  10347. else
  10348. ctx->bc.Instr(asBC_DECi8);
  10349. }
  10350. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttFloat, false)) )
  10351. {
  10352. if( op == ttInc )
  10353. ctx->bc.Instr(asBC_INCf);
  10354. else
  10355. ctx->bc.Instr(asBC_DECf);
  10356. }
  10357. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttDouble, false)) )
  10358. {
  10359. if( op == ttInc )
  10360. ctx->bc.Instr(asBC_INCd);
  10361. else
  10362. ctx->bc.Instr(asBC_DECd);
  10363. }
  10364. else
  10365. {
  10366. Error(TXT_ILLEGAL_OPERATION, node);
  10367. return -1;
  10368. }
  10369. }
  10370. else
  10371. {
  10372. // Unknown operator
  10373. asASSERT(false);
  10374. return -1;
  10375. }
  10376. return 0;
  10377. }
  10378. void asCCompiler::ConvertToReference(asCExprContext *ctx)
  10379. {
  10380. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  10381. {
  10382. ctx->bc.InstrSHORT(asBC_LDV, ctx->type.stackOffset);
  10383. ctx->type.dataType.MakeReference(true);
  10384. ctx->type.SetVariable(ctx->type.dataType, ctx->type.stackOffset, ctx->type.isTemporary);
  10385. }
  10386. }
  10387. int asCCompiler::FindPropertyAccessor(const asCString &name, asCExprContext *ctx, asCScriptNode *node, asSNameSpace *ns, bool isThisAccess)
  10388. {
  10389. return FindPropertyAccessor(name, ctx, 0, node, ns, isThisAccess);
  10390. }
  10391. // Returns:
  10392. // 1 = a valid match was found
  10393. // 0 = no matching symbols (or feature disabled)
  10394. // -1 = ambiguous getters or setters, i.e. multiple methods match symbol name and signature
  10395. // -2 = mismatching type for getter and setter
  10396. // -3 = processing error, e.g. out of memory
  10397. int asCCompiler::FindPropertyAccessor(const asCString &name, asCExprContext *ctx, asCExprContext *arg, asCScriptNode *node, asSNameSpace *ns, bool isThisAccess)
  10398. {
  10399. // TODO: With asEP_PROPERTY_ACCESSOR_MODE == 3 this method doesn't need to validate the
  10400. // getter/setter as it is done at the time of declaration. Should deprecate the other options
  10401. if( engine->ep.propertyAccessorMode == 0 )
  10402. {
  10403. // Property accessors have been disabled by the application
  10404. return 0;
  10405. }
  10406. int getId = 0, setId = 0;
  10407. asCString getName = "get_" + name;
  10408. asCString setName = "set_" + name;
  10409. asCArray<int> multipleGetFuncs, multipleSetFuncs;
  10410. if( ctx->type.dataType.IsObject() )
  10411. {
  10412. asASSERT( ns == 0 );
  10413. // Don't look for property accessors in script classes if the script
  10414. // property accessors have been disabled by the application
  10415. if( !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) ||
  10416. engine->ep.propertyAccessorMode >= 2 )
  10417. {
  10418. // Check if the object has any methods with the corresponding accessor name(s)
  10419. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  10420. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  10421. {
  10422. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  10423. if( engine->ep.propertyAccessorMode == 3 && !f->IsProperty() )
  10424. continue;
  10425. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  10426. if( f->name == getName && (int)f->parameterTypes.GetLength() == (arg?1:0) )
  10427. {
  10428. if( getId == 0 )
  10429. getId = ot->methods[n];
  10430. else
  10431. {
  10432. if( multipleGetFuncs.GetLength() == 0 )
  10433. multipleGetFuncs.PushLast(getId);
  10434. multipleGetFuncs.PushLast(ot->methods[n]);
  10435. }
  10436. }
  10437. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  10438. if( f->name == setName && (int)f->parameterTypes.GetLength() == (arg?2:1) )
  10439. {
  10440. if( setId == 0 )
  10441. setId = ot->methods[n];
  10442. else
  10443. {
  10444. if( multipleSetFuncs.GetLength() == 0 )
  10445. multipleSetFuncs.PushLast(setId);
  10446. multipleSetFuncs.PushLast(ot->methods[n]);
  10447. }
  10448. }
  10449. }
  10450. }
  10451. }
  10452. else
  10453. {
  10454. asASSERT( ns != 0 );
  10455. // Look for appropriate global functions.
  10456. asCArray<int> funcs;
  10457. asUINT n;
  10458. builder->GetFunctionDescriptions(getName.AddressOf(), funcs, ns);
  10459. for( n = 0; n < funcs.GetLength(); n++ )
  10460. {
  10461. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  10462. if( engine->ep.propertyAccessorMode == 3 && !f->IsProperty() )
  10463. continue;
  10464. // Ignore script functions, if the application has disabled script defined property accessors
  10465. if( engine->ep.propertyAccessorMode == 1 && f->funcType == asFUNC_SCRIPT )
  10466. continue;
  10467. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  10468. if( (int)f->parameterTypes.GetLength() == (arg?1:0) )
  10469. {
  10470. if( getId == 0 )
  10471. getId = funcs[n];
  10472. else
  10473. {
  10474. if( multipleGetFuncs.GetLength() == 0 )
  10475. multipleGetFuncs.PushLast(getId);
  10476. multipleGetFuncs.PushLast(funcs[n]);
  10477. }
  10478. }
  10479. }
  10480. funcs.SetLength(0);
  10481. builder->GetFunctionDescriptions(setName.AddressOf(), funcs, ns);
  10482. for( n = 0; n < funcs.GetLength(); n++ )
  10483. {
  10484. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  10485. if( engine->ep.propertyAccessorMode == 3 && !f->IsProperty() )
  10486. continue;
  10487. // Ignore script functions, if the application has disabled script defined property accessors
  10488. if( engine->ep.propertyAccessorMode == 1 && f->funcType == asFUNC_SCRIPT )
  10489. continue;
  10490. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  10491. if( (int)f->parameterTypes.GetLength() == (arg?2:1) )
  10492. {
  10493. if( setId == 0 )
  10494. setId = funcs[n];
  10495. else
  10496. {
  10497. if( multipleSetFuncs.GetLength() == 0 )
  10498. multipleSetFuncs.PushLast(setId);
  10499. multipleSetFuncs.PushLast(funcs[n]);
  10500. }
  10501. }
  10502. }
  10503. }
  10504. bool isConst = ctx->type.dataType.IsObjectConst();
  10505. // Check for multiple matches
  10506. if( multipleGetFuncs.GetLength() > 0 )
  10507. {
  10508. // Filter the list by constness
  10509. FilterConst(multipleGetFuncs, !isConst);
  10510. if( multipleGetFuncs.GetLength() > 1 )
  10511. {
  10512. if (node)
  10513. {
  10514. asCString str;
  10515. str.Format(TXT_MULTIPLE_PROP_GET_ACCESSOR_FOR_s, name.AddressOf());
  10516. Error(str, node);
  10517. PrintMatchingFuncs(multipleGetFuncs, node);
  10518. }
  10519. return -1;
  10520. }
  10521. else
  10522. {
  10523. // The id may have changed
  10524. getId = multipleGetFuncs[0];
  10525. }
  10526. }
  10527. if( multipleSetFuncs.GetLength() > 0 )
  10528. {
  10529. // Filter the list by constness
  10530. FilterConst(multipleSetFuncs, !isConst);
  10531. if( multipleSetFuncs.GetLength() > 1 )
  10532. {
  10533. if (node)
  10534. {
  10535. asCString str;
  10536. str.Format(TXT_MULTIPLE_PROP_SET_ACCESSOR_FOR_s, name.AddressOf());
  10537. Error(str, node);
  10538. PrintMatchingFuncs(multipleSetFuncs, node);
  10539. }
  10540. return -1;
  10541. }
  10542. else
  10543. {
  10544. // The id may have changed
  10545. setId = multipleSetFuncs[0];
  10546. }
  10547. }
  10548. // Check for type compatibility between get and set accessor
  10549. if( getId && setId )
  10550. {
  10551. asCScriptFunction *getFunc = builder->GetFunctionDescription(getId);
  10552. asCScriptFunction *setFunc = builder->GetFunctionDescription(setId);
  10553. // It is permitted for a getter to return a handle and the setter to take a reference
  10554. int idx = (arg?1:0);
  10555. if( !getFunc->returnType.IsEqualExceptRefAndConst(setFunc->parameterTypes[idx]) &&
  10556. !((getFunc->returnType.IsObjectHandle() && !setFunc->parameterTypes[idx].IsObjectHandle()) &&
  10557. (getFunc->returnType.GetTypeInfo() == setFunc->parameterTypes[idx].GetTypeInfo())) )
  10558. {
  10559. if (node)
  10560. {
  10561. asCString str;
  10562. str.Format(TXT_GET_SET_ACCESSOR_TYPE_MISMATCH_FOR_s, name.AddressOf());
  10563. Error(str, node);
  10564. asCArray<int> funcs;
  10565. funcs.PushLast(getId);
  10566. funcs.PushLast(setId);
  10567. PrintMatchingFuncs(funcs, node);
  10568. }
  10569. return -2;
  10570. }
  10571. }
  10572. // Check if we are within one of the accessors
  10573. int realGetId = getId;
  10574. int realSetId = setId;
  10575. if( outFunc->objectType && isThisAccess )
  10576. {
  10577. // The property accessors would be virtual functions, so we need to find the real implementation
  10578. asCScriptFunction *getFunc = getId ? builder->GetFunctionDescription(getId) : 0;
  10579. if( getFunc &&
  10580. getFunc->funcType == asFUNC_VIRTUAL &&
  10581. outFunc->objectType->DerivesFrom(getFunc->objectType) )
  10582. realGetId = outFunc->objectType->virtualFunctionTable[getFunc->vfTableIdx]->id;
  10583. asCScriptFunction *setFunc = setId ? builder->GetFunctionDescription(setId) : 0;
  10584. if( setFunc &&
  10585. setFunc->funcType == asFUNC_VIRTUAL &&
  10586. outFunc->objectType->DerivesFrom(setFunc->objectType) )
  10587. realSetId = outFunc->objectType->virtualFunctionTable[setFunc->vfTableIdx]->id;
  10588. }
  10589. // Avoid recursive call by not treating this as a property accessor call.
  10590. // This will also allow having the real property with the same name as the accessors.
  10591. if( (isThisAccess || outFunc->objectType == 0) &&
  10592. ((realGetId && realGetId == outFunc->id) ||
  10593. (realSetId && realSetId == outFunc->id)) )
  10594. {
  10595. getId = 0;
  10596. setId = 0;
  10597. }
  10598. if( getId || setId )
  10599. {
  10600. // Property accessors were found, but we don't know which is to be used yet, so
  10601. // we just prepare the bytecode for the method call, and then store the function ids
  10602. // so that the right one can be used when we get there.
  10603. ctx->property_get = getId;
  10604. ctx->property_set = setId;
  10605. bool isRefSafe = ctx->type.isRefSafe;
  10606. if( ctx->type.dataType.IsObject() )
  10607. {
  10608. // If the object is read-only then we need to remember that
  10609. if( (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) ||
  10610. (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) )
  10611. ctx->property_const = true;
  10612. else
  10613. ctx->property_const = false;
  10614. // If the object is a handle then we need to remember that
  10615. ctx->property_handle = ctx->type.dataType.IsObjectHandle();
  10616. ctx->property_ref = ctx->type.dataType.IsReference();
  10617. }
  10618. // The setter's parameter type is used as the property type,
  10619. // unless only the getter is available
  10620. asCDataType dt;
  10621. if( setId )
  10622. dt = builder->GetFunctionDescription(setId)->parameterTypes[(arg?1:0)];
  10623. else
  10624. dt = builder->GetFunctionDescription(getId)->returnType;
  10625. // Just change the type, the context must still maintain information
  10626. // about previous variable offset and the indicator of temporary variable.
  10627. int offset = ctx->type.stackOffset;
  10628. bool isTemp = ctx->type.isTemporary;
  10629. ctx->type.Set(dt);
  10630. ctx->type.stackOffset = (short)offset;
  10631. ctx->type.isTemporary = isTemp;
  10632. ctx->exprNode = node;
  10633. // Remember if the object is safe, so the invocation of the property
  10634. // accessor doesn't needlessly make a safe copy of the handle
  10635. ctx->type.isRefSafe = isRefSafe;
  10636. // Store the argument for later use
  10637. if( arg )
  10638. {
  10639. ctx->property_arg = asNEW(asCExprContext)(engine);
  10640. if( ctx->property_arg == 0 )
  10641. {
  10642. // Out of memory
  10643. return -3;
  10644. }
  10645. MergeExprBytecodeAndType(ctx->property_arg, arg);
  10646. }
  10647. return 1;
  10648. }
  10649. // No accessor was found
  10650. return 0;
  10651. }
  10652. int asCCompiler::ProcessPropertySetAccessor(asCExprContext *ctx, asCExprContext *arg, asCScriptNode *node)
  10653. {
  10654. // TODO: A lot of this code is similar to ProcessPropertyGetAccessor. Can we unify them?
  10655. if( !ctx->property_set )
  10656. {
  10657. Error(TXT_PROPERTY_HAS_NO_SET_ACCESSOR, node);
  10658. return -1;
  10659. }
  10660. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_set);
  10661. // Make sure the arg match the property
  10662. asCArray<int> funcs;
  10663. funcs.PushLast(ctx->property_set);
  10664. asCArray<asCExprContext *> args;
  10665. if( ctx->property_arg )
  10666. args.PushLast(ctx->property_arg);
  10667. args.PushLast(arg);
  10668. MatchFunctions(funcs, args, node, func->GetName(), 0, func->objectType, ctx->property_const);
  10669. if( funcs.GetLength() == 0 )
  10670. {
  10671. // MatchFunctions already reported the error
  10672. if( ctx->property_arg )
  10673. {
  10674. asDELETE(ctx->property_arg, asCExprContext);
  10675. ctx->property_arg = 0;
  10676. }
  10677. return -1;
  10678. }
  10679. if( func->objectType )
  10680. {
  10681. // Setup the context with the original type so the method call gets built correctly
  10682. ctx->type.dataType = asCDataType::CreateType(func->objectType, ctx->property_const);
  10683. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  10684. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  10685. // Don't allow the call if the object is read-only and the property accessor is not const
  10686. if( ctx->property_const && !func->IsReadOnly() )
  10687. {
  10688. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  10689. asCArray<int> funcCandidates;
  10690. funcCandidates.PushLast(ctx->property_set);
  10691. PrintMatchingFuncs(funcCandidates, node);
  10692. }
  10693. }
  10694. // Call the accessor
  10695. int r = MakeFunctionCall(ctx, ctx->property_set, func->objectType, args, node);
  10696. ctx->property_get = 0;
  10697. ctx->property_set = 0;
  10698. if( ctx->property_arg )
  10699. {
  10700. asDELETE(ctx->property_arg, asCExprContext);
  10701. ctx->property_arg = 0;
  10702. }
  10703. return r;
  10704. }
  10705. int asCCompiler::ProcessPropertyGetSetAccessor(asCExprContext *ctx, asCExprContext *lctx, asCExprContext *rctx, eTokenType op, asCScriptNode *errNode)
  10706. {
  10707. // TODO: Perhaps it might be interesting to allow the definition of compound setters for better
  10708. // performance, e.g. set_add_prop, set_mul_prop, etc. With these it would also be possible
  10709. // to support value types, since it would be a single call
  10710. // Compound assignment for indexed property accessors is not supported yet
  10711. if( lctx->property_arg != 0 )
  10712. {
  10713. // Process the property to free the memory
  10714. ProcessPropertySetAccessor(lctx, rctx, errNode);
  10715. Error(TXT_COMPOUND_ASGN_WITH_IDX_PROP, errNode);
  10716. return -1;
  10717. }
  10718. // Compound assignments require both get and set accessors
  10719. if( lctx->property_set == 0 || lctx->property_get == 0 )
  10720. {
  10721. // Process the property to free the memory
  10722. ProcessPropertySetAccessor(lctx, rctx, errNode);
  10723. Error(TXT_COMPOUND_ASGN_REQUIRE_GET_SET, errNode);
  10724. return -1;
  10725. }
  10726. // Property accessors on value types (or scoped references types) are not supported since
  10727. // it is not possible to guarantee that the object will stay alive between the two calls
  10728. asCScriptFunction *func = engine->scriptFunctions[lctx->property_set];
  10729. if( func->objectType && (func->objectType->flags & (asOBJ_VALUE | asOBJ_SCOPED)) )
  10730. {
  10731. // Process the property to free the memory
  10732. ProcessPropertySetAccessor(lctx, rctx, errNode);
  10733. Error(TXT_COMPOUND_ASGN_ON_VALUE_TYPE, errNode);
  10734. return -1;
  10735. }
  10736. // Translate the compound assignment to the corresponding dual operator
  10737. switch( op )
  10738. {
  10739. case ttAddAssign: op = ttPlus; break;
  10740. case ttSubAssign: op = ttMinus; break;
  10741. case ttMulAssign: op = ttStar; break;
  10742. case ttDivAssign: op = ttSlash; break;
  10743. case ttModAssign: op = ttPercent; break;
  10744. case ttPowAssign: op = ttStarStar; break;
  10745. case ttAndAssign: op = ttAmp; break;
  10746. case ttOrAssign: op = ttBitOr; break;
  10747. case ttXorAssign: op = ttBitXor; break;
  10748. case ttShiftLeftAssign: op = ttBitShiftLeft; break;
  10749. case ttShiftRightAAssign: op = ttBitShiftRightArith; break;
  10750. case ttShiftRightLAssign: op = ttBitShiftRight; break;
  10751. default: op = ttUnrecognizedToken; break;
  10752. }
  10753. if( op == ttUnrecognizedToken )
  10754. {
  10755. // Shouldn't happen
  10756. asASSERT(false);
  10757. // Process the property to free the memory
  10758. ProcessPropertySetAccessor(lctx, rctx, errNode);
  10759. return -1;
  10760. }
  10761. asCExprContext before(engine);
  10762. if( func->objectType && (func->objectType->flags & (asOBJ_REF|asOBJ_SCOPED)) == asOBJ_REF )
  10763. {
  10764. // Keep a reference to the object in a local variable
  10765. before.bc.AddCode(&lctx->bc);
  10766. asUINT len = reservedVariables.GetLength();
  10767. rctx->bc.GetVarsUsed(reservedVariables);
  10768. before.bc.GetVarsUsed(reservedVariables);
  10769. asCDataType dt = asCDataType::CreateObjectHandle(func->objectType, false);
  10770. int offset = AllocateVariable(dt, true);
  10771. reservedVariables.SetLength(len);
  10772. before.type.SetVariable(dt, offset, true);
  10773. if( lctx->property_ref )
  10774. before.bc.Instr(asBC_RDSPtr);
  10775. before.bc.InstrSHORT(asBC_PSF, (short)offset);
  10776. before.bc.InstrPTR(asBC_REFCPY, func->objectType);
  10777. before.bc.Instr(asBC_PopPtr);
  10778. if( lctx->type.isTemporary )
  10779. {
  10780. // Add the release of the temporary variable as a deferred expression
  10781. asSDeferredParam deferred;
  10782. deferred.origExpr = 0;
  10783. deferred.argInOutFlags = asTM_INREF;
  10784. deferred.argNode = 0;
  10785. deferred.argType.SetVariable(ctx->type.dataType, lctx->type.stackOffset, true);
  10786. before.deferredParams.PushLast(deferred);
  10787. }
  10788. // Update the left expression to use the local variable
  10789. lctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  10790. lctx->type.stackOffset = (short)offset;
  10791. lctx->property_ref = true;
  10792. // Don't release the temporary variable too early
  10793. lctx->type.isTemporary = false;
  10794. ctx->bc.AddCode(&before.bc);
  10795. }
  10796. // Keep the original information on the property
  10797. asCExprContext llctx(engine);
  10798. llctx.type = lctx->type;
  10799. llctx.property_arg = lctx->property_arg;
  10800. llctx.property_const = lctx->property_const;
  10801. llctx.property_get = lctx->property_get;
  10802. llctx.property_handle = lctx->property_handle;
  10803. llctx.property_ref = lctx->property_ref;
  10804. llctx.property_set = lctx->property_set;
  10805. // Compile the dual operator using the get accessor
  10806. CompileOperator(errNode, lctx, rctx, ctx, op, false);
  10807. // If we made a local variable to hold the reference it must be reused
  10808. if( before.type.stackOffset )
  10809. llctx.bc.InstrSHORT(asBC_PSF, before.type.stackOffset);
  10810. // Compile the assignment using the set accessor
  10811. ProcessPropertySetAccessor(&llctx, ctx, errNode);
  10812. MergeExprBytecodeAndType(ctx, &llctx);
  10813. if( before.type.stackOffset )
  10814. ReleaseTemporaryVariable(before.type.stackOffset, &ctx->bc);
  10815. asASSERT( ctx->deferredParams.GetLength() == 0 );
  10816. ctx->deferredParams = before.deferredParams;
  10817. ProcessDeferredParams(ctx);
  10818. return 0;
  10819. }
  10820. int asCCompiler::ProcessPropertyGetAccessor(asCExprContext *ctx, asCScriptNode *node)
  10821. {
  10822. // If no property accessor has been prepared then don't do anything
  10823. if( !ctx->property_get && !ctx->property_set )
  10824. return 0;
  10825. if( !ctx->property_get )
  10826. {
  10827. // Raise error on missing accessor
  10828. Error(TXT_PROPERTY_HAS_NO_GET_ACCESSOR, node);
  10829. return -1;
  10830. }
  10831. asCExprValue objType = ctx->type;
  10832. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_get);
  10833. // Make sure the arg match the property
  10834. asCArray<int> funcs;
  10835. funcs.PushLast(ctx->property_get);
  10836. asCArray<asCExprContext *> args;
  10837. if( ctx->property_arg )
  10838. args.PushLast(ctx->property_arg);
  10839. MatchFunctions(funcs, args, node, func->GetName(), 0, func->objectType, ctx->property_const);
  10840. if( funcs.GetLength() == 0 )
  10841. {
  10842. // MatchFunctions already reported the error
  10843. if( ctx->property_arg )
  10844. {
  10845. asDELETE(ctx->property_arg, asCExprContext);
  10846. ctx->property_arg = 0;
  10847. }
  10848. return -1;
  10849. }
  10850. if( func->objectType )
  10851. {
  10852. // Setup the context with the original type so the method call gets built correctly
  10853. ctx->type.dataType = asCDataType::CreateType(func->objectType, ctx->property_const);
  10854. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  10855. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  10856. // Don't allow the call if the object is read-only and the property accessor is not const
  10857. if( ctx->property_const && !func->IsReadOnly() )
  10858. {
  10859. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  10860. asCArray<int> funcCandidates;
  10861. funcCandidates.PushLast(ctx->property_get);
  10862. PrintMatchingFuncs(funcCandidates, node);
  10863. return -1;
  10864. }
  10865. }
  10866. // The explicit handle flag must be remembered
  10867. bool isExplicitHandle = ctx->type.isExplicitHandle;
  10868. // Call the accessor
  10869. int r = MakeFunctionCall(ctx, ctx->property_get, func->objectType, args, node);
  10870. if( isExplicitHandle )
  10871. ctx->type.isExplicitHandle = true;
  10872. // Clear the property get/set ids
  10873. ctx->property_get = 0;
  10874. ctx->property_set = 0;
  10875. if( ctx->property_arg )
  10876. {
  10877. asDELETE(ctx->property_arg, asCExprContext);
  10878. ctx->property_arg = 0;
  10879. }
  10880. return r;
  10881. }
  10882. int asCCompiler::CompileExpressionPostOp(asCScriptNode *node, asCExprContext *ctx)
  10883. {
  10884. // Don't allow any postfix operators on expressions that take address of class method
  10885. if( ctx->IsClassMethod() )
  10886. {
  10887. Error(TXT_INVALID_OP_ON_METHOD, node);
  10888. return -1;
  10889. }
  10890. // Don't allow any operators on void expressions
  10891. if( ctx->IsVoidExpression() )
  10892. {
  10893. Error(TXT_VOID_CANT_BE_OPERAND, node);
  10894. return -1;
  10895. }
  10896. // Check if the variable is initialized (if it indeed is a variable)
  10897. IsVariableInitialized(&ctx->type, node);
  10898. int op = node->tokenType;
  10899. if( (op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  10900. {
  10901. const char *opName = 0;
  10902. switch( op )
  10903. {
  10904. case ttInc: opName = "opPostInc"; break;
  10905. case ttDec: opName = "opPostDec"; break;
  10906. }
  10907. if( opName )
  10908. {
  10909. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  10910. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  10911. return -1;
  10912. // TODO: If the value isn't const, then first try to find the non const method, and if not found try to find the const method
  10913. // Find the correct method
  10914. bool isConst = ctx->type.dataType.IsObjectConst();
  10915. asCArray<int> funcs;
  10916. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  10917. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  10918. {
  10919. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  10920. if( func->name == opName &&
  10921. func->parameterTypes.GetLength() == 0 &&
  10922. (!isConst || func->IsReadOnly()) )
  10923. {
  10924. funcs.PushLast(func->id);
  10925. }
  10926. }
  10927. // Did we find the method?
  10928. if( funcs.GetLength() == 1 )
  10929. {
  10930. asCArray<asCExprContext *> args;
  10931. return MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  10932. }
  10933. else if( funcs.GetLength() == 0 )
  10934. {
  10935. asCString str;
  10936. str = asCString(opName) + "()";
  10937. if( isConst )
  10938. str += " const";
  10939. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  10940. Error(str, node);
  10941. ctx->type.SetDummy();
  10942. return -1;
  10943. }
  10944. else if( funcs.GetLength() > 1 )
  10945. {
  10946. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  10947. PrintMatchingFuncs(funcs, node);
  10948. ctx->type.SetDummy();
  10949. return -1;
  10950. }
  10951. }
  10952. }
  10953. else if( op == ttInc || op == ttDec )
  10954. {
  10955. // Make sure the reference isn't a temporary variable
  10956. if( ctx->type.isTemporary )
  10957. {
  10958. Error(TXT_REF_IS_TEMP, node);
  10959. return -1;
  10960. }
  10961. if( ctx->type.dataType.IsReadOnly() )
  10962. {
  10963. Error(TXT_REF_IS_READ_ONLY, node);
  10964. return -1;
  10965. }
  10966. if( ctx->property_get || ctx->property_set )
  10967. {
  10968. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  10969. return -1;
  10970. }
  10971. if( !ctx->type.isLValue )
  10972. {
  10973. Error(TXT_NOT_LVALUE, node);
  10974. return -1;
  10975. }
  10976. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  10977. ConvertToReference(ctx);
  10978. else if( !ctx->type.dataType.IsReference() )
  10979. {
  10980. Error(TXT_NOT_VALID_REFERENCE, node);
  10981. return -1;
  10982. }
  10983. // Copy the value to a temp before changing it
  10984. ConvertToTempVariable(ctx);
  10985. asASSERT(!ctx->type.isLValue);
  10986. // Increment the value pointed to by the reference still in the register
  10987. asEBCInstr iInc = asBC_INCi, iDec = asBC_DECi;
  10988. if( ctx->type.dataType.IsDoubleType() )
  10989. {
  10990. iInc = asBC_INCd;
  10991. iDec = asBC_DECd;
  10992. }
  10993. else if( ctx->type.dataType.IsFloatType() )
  10994. {
  10995. iInc = asBC_INCf;
  10996. iDec = asBC_DECf;
  10997. }
  10998. else if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() )
  10999. {
  11000. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  11001. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  11002. {
  11003. iInc = asBC_INCi16;
  11004. iDec = asBC_DECi16;
  11005. }
  11006. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  11007. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  11008. {
  11009. iInc = asBC_INCi8;
  11010. iDec = asBC_DECi8;
  11011. }
  11012. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  11013. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  11014. {
  11015. iInc = asBC_INCi64;
  11016. iDec = asBC_DECi64;
  11017. }
  11018. }
  11019. else
  11020. {
  11021. Error(TXT_ILLEGAL_OPERATION, node);
  11022. return -1;
  11023. }
  11024. if( op == ttInc ) ctx->bc.Instr(iInc); else ctx->bc.Instr(iDec);
  11025. }
  11026. else if( op == ttDot )
  11027. {
  11028. if( node->firstChild->nodeType == snIdentifier )
  11029. {
  11030. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  11031. return -1;
  11032. // Get the property name
  11033. asCString name(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  11034. if( ctx->type.dataType.IsObject() )
  11035. {
  11036. // We need to look for get/set property accessors.
  11037. // If found, the context stores information on the get/set accessors
  11038. // until it is known which is to be used.
  11039. int r = 0;
  11040. if( node->next && node->next->tokenType == ttOpenBracket )
  11041. {
  11042. // The property accessor should take an index arg
  11043. asCExprContext dummyArg(engine);
  11044. r = FindPropertyAccessor(name, ctx, &dummyArg, node, 0);
  11045. }
  11046. if( r == 0 )
  11047. r = FindPropertyAccessor(name, ctx, node, 0);
  11048. if( r != 0 )
  11049. return r;
  11050. if( !ctx->type.dataType.IsPrimitive() )
  11051. Dereference(ctx, true);
  11052. if( ctx->type.dataType.IsObjectHandle() )
  11053. {
  11054. // Convert the handle to a normal object
  11055. asCDataType dt = ctx->type.dataType;
  11056. dt.MakeHandle(false);
  11057. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  11058. // The handle may not have been an lvalue, but the dereferenced object is
  11059. ctx->type.isLValue = true;
  11060. }
  11061. bool isConst = ctx->type.dataType.IsObjectConst();
  11062. asCObjectProperty *prop = builder->GetObjectProperty(ctx->type.dataType, name.AddressOf());
  11063. if( prop )
  11064. {
  11065. // Is the property access allowed?
  11066. if( (prop->isPrivate || prop->isProtected) && (!outFunc || outFunc->objectType != ctx->type.dataType.GetTypeInfo()) )
  11067. {
  11068. asCString msg;
  11069. if( prop->isPrivate )
  11070. msg.Format(TXT_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  11071. else
  11072. msg.Format(TXT_PROTECTED_PROP_ACCESS_s, name.AddressOf());
  11073. Error(msg, node);
  11074. }
  11075. // Adjust the pointer for composite member
  11076. // This must always be done even if the offset is 0 because the asCWriter needs the meta data in ADDSi to identify the composite property
  11077. if( prop->compositeOffset || prop->isCompositeIndirect )
  11078. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->compositeOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(ctx->type.dataType.GetTypeInfo(), false)));
  11079. if (prop->isCompositeIndirect)
  11080. ctx->bc.Instr(asBC_RDSPtr);
  11081. // Put the offset on the stack
  11082. // This must always be done even if the offset is 0 so the type info is stored
  11083. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(ctx->type.dataType.GetTypeInfo(), false)));
  11084. if( prop->type.IsReference() )
  11085. ctx->bc.Instr(asBC_RDSPtr);
  11086. // Reference to primitive must be stored in the temp register
  11087. if( prop->type.IsPrimitive() )
  11088. {
  11089. ctx->bc.Instr(asBC_PopRPtr);
  11090. }
  11091. // Keep information about temporary variables as deferred expression
  11092. if( ctx->type.isTemporary )
  11093. {
  11094. // Add the release of this reference, as a deferred expression
  11095. asSDeferredParam deferred;
  11096. deferred.origExpr = 0;
  11097. deferred.argInOutFlags = asTM_INREF;
  11098. deferred.argNode = 0;
  11099. deferred.argType.SetVariable(ctx->type.dataType, ctx->type.stackOffset, true);
  11100. ctx->deferredParams.PushLast(deferred);
  11101. }
  11102. // Set the new type and make sure it is not treated as a variable anymore
  11103. ctx->type.dataType = prop->type;
  11104. ctx->type.dataType.MakeReference(true);
  11105. ctx->type.isVariable = false;
  11106. ctx->type.isTemporary = false;
  11107. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && !ctx->type.dataType.IsObjectHandle() )
  11108. {
  11109. // Objects that are members are not references
  11110. ctx->type.dataType.MakeReference(false);
  11111. // The object is safe (life time guaranteed) if the parent object is also safe
  11112. }
  11113. else if (ctx->type.dataType.IsObjectHandle())
  11114. {
  11115. // A object accessed through a handle cannot be considered safe,
  11116. // as it can be cleared at any time
  11117. ctx->type.isRefSafe = false;
  11118. }
  11119. ctx->type.dataType.MakeReadOnly(isConst ? true : prop->type.IsReadOnly());
  11120. }
  11121. else
  11122. {
  11123. // If the name is not a property, the compiler must check if the name matches
  11124. // a method, which can be used for constructing delegates
  11125. asIScriptFunction *func = 0;
  11126. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  11127. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  11128. {
  11129. if( engine->scriptFunctions[ot->methods[n]]->name == name )
  11130. {
  11131. func = engine->scriptFunctions[ot->methods[n]];
  11132. break;
  11133. }
  11134. }
  11135. if( func )
  11136. {
  11137. // An object method was found. Keep the name of the method in the expression, but
  11138. // don't actually modify the bytecode at this point since it is not yet known what
  11139. // the method will be used for, or even what overloaded method should be used.
  11140. ctx->methodName = name;
  11141. }
  11142. else
  11143. {
  11144. asCString str;
  11145. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11146. Error(str, node);
  11147. return -1;
  11148. }
  11149. }
  11150. }
  11151. else
  11152. {
  11153. asCString str;
  11154. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11155. Error(str, node);
  11156. return -1;
  11157. }
  11158. }
  11159. else
  11160. {
  11161. // Make sure it is an object we are accessing
  11162. if( !ctx->type.dataType.IsObject() )
  11163. {
  11164. asCString str;
  11165. str.Format(TXT_ILLEGAL_OPERATION_ON_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11166. Error(str, node);
  11167. return -1;
  11168. }
  11169. // Process the get property accessor
  11170. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  11171. return -1;
  11172. // Compile function call
  11173. int r = CompileFunctionCall(node->firstChild, ctx, CastToObjectType(ctx->type.dataType.GetTypeInfo()), ctx->type.dataType.IsObjectConst());
  11174. if( r < 0 ) return r;
  11175. }
  11176. }
  11177. else if( op == ttOpenBracket )
  11178. {
  11179. // If the property access takes an index arg and the argument hasn't been evaluated yet,
  11180. // then we should use that instead of processing it now. If the argument has already been
  11181. // evaluated, then we should process the property accessor as a get access now as the new
  11182. // index operator is on the result of that accessor.
  11183. asCString propertyName;
  11184. asSNameSpace *ns = 0;
  11185. if( ((ctx->property_get && builder->GetFunctionDescription(ctx->property_get)->GetParamCount() == 1) ||
  11186. (ctx->property_set && builder->GetFunctionDescription(ctx->property_set)->GetParamCount() == 2)) &&
  11187. (ctx->property_arg && ctx->property_arg->type.dataType.GetTokenType() == ttUnrecognizedToken) )
  11188. {
  11189. // Determine the name of the property accessor
  11190. asCScriptFunction *func = 0;
  11191. if( ctx->property_get )
  11192. func = builder->GetFunctionDescription(ctx->property_get);
  11193. else
  11194. func = builder->GetFunctionDescription(ctx->property_set);
  11195. propertyName = func->GetName();
  11196. propertyName = propertyName.SubString(4);
  11197. // Set the original type of the expression so we can re-evaluate the property accessor
  11198. if( func->objectType )
  11199. {
  11200. ctx->type.dataType = asCDataType::CreateType(func->objectType, ctx->property_const);
  11201. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  11202. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  11203. }
  11204. else
  11205. {
  11206. // Store the namespace where the function is declared
  11207. // so the same function can be found later
  11208. ctx->type.SetDummy();
  11209. ns = func->nameSpace;
  11210. }
  11211. ctx->property_get = ctx->property_set = 0;
  11212. if( ctx->property_arg )
  11213. {
  11214. asDELETE(ctx->property_arg, asCExprContext);
  11215. ctx->property_arg = 0;
  11216. }
  11217. }
  11218. else
  11219. {
  11220. if( !ctx->type.dataType.IsObject() )
  11221. {
  11222. asCString str;
  11223. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11224. Error(str, node);
  11225. return -1;
  11226. }
  11227. if( ProcessPropertyGetAccessor(ctx, node) < 0 )
  11228. return -1;
  11229. }
  11230. // Compile the expression
  11231. bool isOK = true;
  11232. asCArray<asCExprContext *> args;
  11233. asCArray<asSNamedArgument> namedArgs;
  11234. asASSERT( node->firstChild->nodeType == snArgList );
  11235. if( CompileArgumentList(node->firstChild, args, namedArgs) >= 0 )
  11236. {
  11237. // Check for the existence of the opIndex method
  11238. bool lookForProperty = true;
  11239. if( propertyName == "" )
  11240. {
  11241. bool isConst = ctx->type.dataType.IsObjectConst();
  11242. asCObjectType *objectType = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  11243. asCArray<int> funcs;
  11244. builder->GetObjectMethodDescriptions("opIndex", objectType, funcs, isConst);
  11245. if( funcs.GetLength() > 0 )
  11246. {
  11247. // Since there are opIndex methods, the compiler should not look for get/set_opIndex accessors
  11248. lookForProperty = false;
  11249. // Determine which of opIndex methods that match
  11250. MatchFunctions(funcs, args, node, "opIndex", 0, objectType, isConst);
  11251. if( funcs.GetLength() != 1 )
  11252. {
  11253. // The error has already been reported by MatchFunctions
  11254. isOK = false;
  11255. }
  11256. else
  11257. {
  11258. // Add the default values for arguments not explicitly supplied
  11259. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], objectType);
  11260. if( r < 0 )
  11261. isOK = false;
  11262. else if( MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, ctx->type.stackOffset) < 0 )
  11263. isOK = false;
  11264. }
  11265. }
  11266. }
  11267. if( lookForProperty && isOK )
  11268. {
  11269. if( args.GetLength() != 1 )
  11270. {
  11271. // TODO: opIndex: Implement support for multiple index arguments in set_opIndex too
  11272. Error(TXT_PROP_ACCESS_WITH_INDEX_ONE_ARG, node);
  11273. isOK = false;
  11274. }
  11275. else
  11276. {
  11277. Dereference(ctx, true);
  11278. asCExprContext lctx(engine);
  11279. MergeExprBytecodeAndType(&lctx, ctx);
  11280. // Check for accessors methods for the opIndex, either as get/set_opIndex or as get/set with the property name
  11281. int r = FindPropertyAccessor(propertyName == "" ? "opIndex" : propertyName.AddressOf(), &lctx, args[0], node, ns);
  11282. if (r == 0)
  11283. {
  11284. asCString str;
  11285. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11286. Error(str, node);
  11287. isOK = false;
  11288. }
  11289. else if (r < 0)
  11290. isOK = false;
  11291. if (isOK)
  11292. MergeExprBytecodeAndType(ctx, &lctx);
  11293. }
  11294. }
  11295. }
  11296. else
  11297. isOK = false;
  11298. // Cleanup
  11299. for( asUINT n = 0; n < args.GetLength(); n++ )
  11300. if( args[n] )
  11301. {
  11302. asDELETE(args[n], asCExprContext);
  11303. }
  11304. if( !isOK )
  11305. return -1;
  11306. }
  11307. else if( op == ttOpenParanthesis )
  11308. {
  11309. // TODO: Most of this is already done by CompileFunctionCall(). Can we share the code?
  11310. // Make sure the expression is a funcdef or an object that may have opCall methods
  11311. if( !ctx->type.dataType.GetTypeInfo() || (!ctx->type.dataType.IsFuncdef() && !ctx->type.dataType.IsObject()) )
  11312. {
  11313. Error(TXT_EXPR_DOESNT_EVAL_TO_FUNC, node);
  11314. return -1;
  11315. }
  11316. // Compile arguments
  11317. bool isOK = true;
  11318. asCArray<asCExprContext *> args;
  11319. asCArray<asSNamedArgument> namedArgs;
  11320. if( CompileArgumentList(node->lastChild, args, namedArgs) >= 0 )
  11321. {
  11322. // Match arguments with the funcdef
  11323. asCArray<int> funcs;
  11324. if( ctx->type.dataType.IsFuncdef() )
  11325. {
  11326. funcs.PushLast(CastToFuncdefType(ctx->type.dataType.GetTypeInfo())->funcdef->id);
  11327. MatchFunctions(funcs, args, node, ctx->type.dataType.GetTypeInfo()->name.AddressOf(), &namedArgs);
  11328. }
  11329. else
  11330. {
  11331. bool isConst = ctx->type.dataType.IsObjectConst();
  11332. builder->GetObjectMethodDescriptions("opCall", CastToObjectType(ctx->type.dataType.GetTypeInfo()), funcs, isConst);
  11333. MatchFunctions(funcs, args, node, "opCall", &namedArgs, CastToObjectType(ctx->type.dataType.GetTypeInfo()), isConst);
  11334. }
  11335. if( funcs.GetLength() != 1 )
  11336. {
  11337. // The error was reported by MatchFunctions()
  11338. // Dummy value
  11339. ctx->type.SetDummy();
  11340. }
  11341. else
  11342. {
  11343. // Add the default values for arguments not explicitly supplied
  11344. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), &namedArgs);
  11345. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  11346. // is it enough to make sure it is in a local variable?
  11347. // For function pointer we must guarantee that the function is safe, i.e.
  11348. // by first storing the function pointer in a local variable (if it isn't already in one)
  11349. if( r == asSUCCESS )
  11350. {
  11351. Dereference(ctx, true);
  11352. if( ctx->type.dataType.IsFuncdef() )
  11353. {
  11354. if( !ctx->type.isVariable )
  11355. ConvertToVariable(ctx);
  11356. // Remove the reference from the stack as the asBC_CALLPTR instruction takes the variable as argument
  11357. ctx->bc.Instr(asBC_PopPtr);
  11358. }
  11359. r = MakeFunctionCall(ctx, funcs[0], ctx->type.dataType.IsFuncdef() ? 0 : CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node, false, 0, ctx->type.stackOffset);
  11360. if( r < 0 )
  11361. isOK = false;
  11362. }
  11363. else
  11364. isOK = false;
  11365. }
  11366. }
  11367. else
  11368. ctx->type.SetDummy();
  11369. // Cleanup
  11370. for( asUINT n = 0; n < args.GetLength(); n++ )
  11371. if( args[n] )
  11372. {
  11373. asDELETE(args[n], asCExprContext);
  11374. }
  11375. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  11376. if( namedArgs[n].ctx )
  11377. {
  11378. asDELETE(namedArgs[n].ctx, asCExprContext);
  11379. }
  11380. if( !isOK )
  11381. return -1;
  11382. }
  11383. return 0;
  11384. }
  11385. int asCCompiler::GetPrecedence(asCScriptNode *op)
  11386. {
  11387. // x ** y
  11388. // x * y, x / y, x % y
  11389. // x + y, x - y
  11390. // x <= y, x < y, x >= y, x > y
  11391. // x = =y, x != y, x xor y, x is y, x !is y
  11392. // x and y
  11393. // x or y
  11394. // The following are not used in this function,
  11395. // but should have lower precedence than the above
  11396. // x ? y : z
  11397. // x = y
  11398. // The expression term have the highest precedence
  11399. if( op->nodeType == snExprTerm )
  11400. return 1;
  11401. // Evaluate operators by token
  11402. int tokenType = op->tokenType;
  11403. if( tokenType == ttStarStar )
  11404. return 0;
  11405. if( tokenType == ttStar || tokenType == ttSlash || tokenType == ttPercent )
  11406. return -1;
  11407. if( tokenType == ttPlus || tokenType == ttMinus )
  11408. return -2;
  11409. if( tokenType == ttBitShiftLeft ||
  11410. tokenType == ttBitShiftRight ||
  11411. tokenType == ttBitShiftRightArith )
  11412. return -3;
  11413. if( tokenType == ttAmp )
  11414. return -4;
  11415. if( tokenType == ttBitXor )
  11416. return -5;
  11417. if( tokenType == ttBitOr )
  11418. return -6;
  11419. if( tokenType == ttLessThanOrEqual ||
  11420. tokenType == ttLessThan ||
  11421. tokenType == ttGreaterThanOrEqual ||
  11422. tokenType == ttGreaterThan )
  11423. return -7;
  11424. if( tokenType == ttEqual || tokenType == ttNotEqual || tokenType == ttXor || tokenType == ttIs || tokenType == ttNotIs )
  11425. return -8;
  11426. if( tokenType == ttAnd )
  11427. return -9;
  11428. if( tokenType == ttOr )
  11429. return -10;
  11430. // Unknown operator
  11431. asASSERT(false);
  11432. return 0;
  11433. }
  11434. asUINT asCCompiler::MatchArgument(asCArray<int> &funcs, asCArray<asSOverloadCandidate> &matches, const asCExprContext *argExpr, int paramNum, bool allowObjectConstruct)
  11435. {
  11436. matches.SetLength(0);
  11437. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  11438. {
  11439. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  11440. // Does the function have arguments enough?
  11441. if( (int)desc->parameterTypes.GetLength() <= paramNum )
  11442. continue;
  11443. int cost = MatchArgument(desc, argExpr, paramNum, allowObjectConstruct);
  11444. if( cost != -1 )
  11445. matches.PushLast(asSOverloadCandidate(funcs[n], asUINT(cost)));
  11446. }
  11447. return (asUINT)matches.GetLength();
  11448. }
  11449. int asCCompiler::MatchArgument(asCScriptFunction *desc, const asCExprContext *argExpr, int paramNum, bool allowObjectConstruct)
  11450. {
  11451. // void expressions can match any out parameter, but nothing else
  11452. if( argExpr->IsVoidExpression() )
  11453. {
  11454. if( desc->inOutFlags[paramNum] == asTM_OUTREF )
  11455. return 0;
  11456. return -1;
  11457. }
  11458. // Anonymous init lists can only match parameters that can be initialized with a list
  11459. if (argExpr->IsAnonymousInitList())
  11460. {
  11461. if( (desc->parameterTypes[paramNum].IsReference() && (desc->inOutFlags[paramNum] & asTM_INREF) == 0) ||
  11462. desc->parameterTypes[paramNum].GetBehaviour() == 0 ||
  11463. desc->parameterTypes[paramNum].GetBehaviour()->listFactory == 0 )
  11464. {
  11465. return -1;
  11466. }
  11467. return 0;
  11468. }
  11469. // Can we make the match by implicit conversion?
  11470. asCExprContext ti(engine);
  11471. ti.type = argExpr->type;
  11472. ti.methodName = argExpr->methodName;
  11473. ti.enumValue = argExpr->enumValue;
  11474. ti.exprNode = argExpr->exprNode;
  11475. if( argExpr->type.dataType.IsPrimitive() )
  11476. ti.type.dataType.MakeReference(false);
  11477. // Don't allow the implicit conversion to make a copy in case the argument is expecting a reference to the true value
  11478. if (desc->parameterTypes[paramNum].IsReference() && desc->inOutFlags[paramNum] == asTM_INOUTREF)
  11479. allowObjectConstruct = false;
  11480. int cost = ImplicitConversion(&ti, desc->parameterTypes[paramNum], 0, asIC_IMPLICIT_CONV, false, allowObjectConstruct);
  11481. // If the function parameter is an inout-reference then it must not be possible to call the
  11482. // function with an incorrect argument type, even though the type can normally be converted.
  11483. if( desc->parameterTypes[paramNum].IsReference() &&
  11484. desc->inOutFlags[paramNum] == asTM_INOUTREF &&
  11485. desc->parameterTypes[paramNum].GetTokenType() != ttQuestion )
  11486. {
  11487. // Observe, that the below checks are only necessary for when unsafe references have been
  11488. // enabled by the application. Without this the &inout reference form wouldn't be allowed
  11489. // for these value types.
  11490. // Don't allow a primitive to be converted to a reference of another primitive type
  11491. if( desc->parameterTypes[paramNum].IsPrimitive() &&
  11492. desc->parameterTypes[paramNum].GetTokenType() != argExpr->type.dataType.GetTokenType() )
  11493. {
  11494. asASSERT( engine->ep.allowUnsafeReferences );
  11495. return -1;
  11496. }
  11497. // Don't allow an enum to be converted to a reference of another enum type
  11498. if( desc->parameterTypes[paramNum].IsEnumType() &&
  11499. desc->parameterTypes[paramNum].GetTypeInfo() != argExpr->type.dataType.GetTypeInfo() )
  11500. {
  11501. asASSERT( engine->ep.allowUnsafeReferences );
  11502. return -1;
  11503. }
  11504. // Don't allow a non-handle expression to be converted to a reference to a handle
  11505. if( desc->parameterTypes[paramNum].IsObjectHandle() &&
  11506. !argExpr->type.dataType.IsObjectHandle() )
  11507. {
  11508. asASSERT( engine->ep.allowUnsafeReferences );
  11509. return -1;
  11510. }
  11511. // Don't allow a value type to be converted
  11512. if( (desc->parameterTypes[paramNum].GetTypeInfo() && (desc->parameterTypes[paramNum].GetTypeInfo()->GetFlags() & asOBJ_VALUE)) &&
  11513. (desc->parameterTypes[paramNum].GetTypeInfo() != argExpr->type.dataType.GetTypeInfo()) )
  11514. {
  11515. asASSERT( engine->ep.allowUnsafeReferences );
  11516. return -1;
  11517. }
  11518. }
  11519. // How well does the argument match the function parameter?
  11520. if( desc->parameterTypes[paramNum].IsEqualExceptRef(ti.type.dataType) )
  11521. return cost;
  11522. // No match is available
  11523. return -1;
  11524. }
  11525. int asCCompiler::PrepareArgument2(asCExprContext *ctx, asCExprContext *arg, asCDataType *paramType, bool isFunction, int refType, bool isMakingCopy)
  11526. {
  11527. // Reference parameters whose value won't be used don't evaluate the expression
  11528. // Clean arguments (i.e. default value) will be passed in directly as there is nothing to protect
  11529. if( paramType->IsReference() && !(refType & asTM_INREF) && !arg->isCleanArg )
  11530. {
  11531. // Store the original bytecode so that it can be reused when processing the deferred output parameter
  11532. asCExprContext *orig = asNEW(asCExprContext)(engine);
  11533. if( orig == 0 )
  11534. {
  11535. // Out of memory
  11536. return -1;
  11537. }
  11538. MergeExprBytecodeAndType(orig, arg);
  11539. arg->origExpr = orig;
  11540. }
  11541. int r = PrepareArgument(paramType, arg, arg->exprNode, isFunction, refType, isMakingCopy);
  11542. if (r < 0)
  11543. return r;
  11544. // arg still holds the original expression for output parameters
  11545. ctx->bc.AddCode(&arg->bc);
  11546. return 0;
  11547. }
  11548. bool asCCompiler::CompileOverloadedDualOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, bool leftToRight, asCExprContext *ctx, bool isHandle, eTokenType token)
  11549. {
  11550. DetermineSingleFunc(lctx, node);
  11551. DetermineSingleFunc(rctx, node);
  11552. ctx->exprNode = node;
  11553. // What type of operator is it?
  11554. if( token == ttUnrecognizedToken )
  11555. token = node->tokenType;
  11556. if( token == ttUnrecognizedToken )
  11557. {
  11558. // This happens when the compiler is inferring an assignment
  11559. // operation from another action, for example in preparing a value
  11560. // as a function argument
  11561. token = ttAssignment;
  11562. }
  11563. // boolean operators are not overloadable
  11564. if( token == ttAnd ||
  11565. token == ttOr ||
  11566. token == ttXor )
  11567. return false;
  11568. // Dual operators can also be implemented as class methods
  11569. if( token == ttEqual ||
  11570. token == ttNotEqual )
  11571. {
  11572. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  11573. // Find the matching opEquals method
  11574. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, leftToRight, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  11575. if( r == 0 )
  11576. {
  11577. // Try again by switching the order of the operands
  11578. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, !leftToRight, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  11579. }
  11580. if( r == 1 )
  11581. {
  11582. if( token == ttNotEqual )
  11583. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  11584. // Success, don't continue
  11585. return true;
  11586. }
  11587. else if( r < 0 )
  11588. {
  11589. // Compiler error, don't continue
  11590. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  11591. return true;
  11592. }
  11593. }
  11594. if( token == ttEqual ||
  11595. token == ttNotEqual ||
  11596. token == ttLessThan ||
  11597. token == ttLessThanOrEqual ||
  11598. token == ttGreaterThan ||
  11599. token == ttGreaterThanOrEqual )
  11600. {
  11601. bool swappedOrder = false;
  11602. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  11603. // Find the matching opCmp method
  11604. int r = CompileOverloadedDualOperator2(node, "opCmp", lctx, rctx, leftToRight, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  11605. if( r == 0 )
  11606. {
  11607. // Try again by switching the order of the operands
  11608. swappedOrder = true;
  11609. r = CompileOverloadedDualOperator2(node, "opCmp", rctx, lctx, !leftToRight, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  11610. }
  11611. if( r == 1 )
  11612. {
  11613. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  11614. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  11615. ctx->bc.InstrW_DW(asBC_CMPIi, ctx->type.stackOffset, 0);
  11616. if( token == ttEqual )
  11617. ctx->bc.Instr(asBC_TZ);
  11618. else if( token == ttNotEqual )
  11619. ctx->bc.Instr(asBC_TNZ);
  11620. else if( (token == ttLessThan && !swappedOrder) ||
  11621. (token == ttGreaterThan && swappedOrder) )
  11622. ctx->bc.Instr(asBC_TS);
  11623. else if( (token == ttLessThanOrEqual && !swappedOrder) ||
  11624. (token == ttGreaterThanOrEqual && swappedOrder) )
  11625. ctx->bc.Instr(asBC_TNP);
  11626. else if( (token == ttGreaterThan && !swappedOrder) ||
  11627. (token == ttLessThan && swappedOrder) )
  11628. ctx->bc.Instr(asBC_TP);
  11629. else if( (token == ttGreaterThanOrEqual && !swappedOrder) ||
  11630. (token == ttLessThanOrEqual && swappedOrder) )
  11631. ctx->bc.Instr(asBC_TNS);
  11632. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  11633. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), a, true);
  11634. // Success, don't continue
  11635. return true;
  11636. }
  11637. else if( r < 0 )
  11638. {
  11639. // Compiler error, don't continue
  11640. #if AS_SIZEOF_BOOL == 1
  11641. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  11642. #else
  11643. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  11644. #endif
  11645. return true;
  11646. }
  11647. }
  11648. // The rest of the operators are not commutative, and doesn't require specific return type
  11649. const char *op = 0, *op_r = 0;
  11650. switch( int(token) ) // convert to int to avoid warning in gnuc that not all values are tested
  11651. {
  11652. case ttPlus: op = "opAdd"; op_r = "opAdd_r"; break;
  11653. case ttMinus: op = "opSub"; op_r = "opSub_r"; break;
  11654. case ttStar: op = "opMul"; op_r = "opMul_r"; break;
  11655. case ttSlash: op = "opDiv"; op_r = "opDiv_r"; break;
  11656. case ttPercent: op = "opMod"; op_r = "opMod_r"; break;
  11657. case ttStarStar: op = "opPow"; op_r = "opPow_r"; break;
  11658. case ttBitOr: op = "opOr"; op_r = "opOr_r"; break;
  11659. case ttAmp: op = "opAnd"; op_r = "opAnd_r"; break;
  11660. case ttBitXor: op = "opXor"; op_r = "opXor_r"; break;
  11661. case ttBitShiftLeft: op = "opShl"; op_r = "opShl_r"; break;
  11662. case ttBitShiftRight: op = "opShr"; op_r = "opShr_r"; break;
  11663. case ttBitShiftRightArith: op = "opUShr"; op_r = "opUShr_r"; break;
  11664. }
  11665. // TODO: Might be interesting to support a concatenation operator, e.g. ~
  11666. if( op && op_r )
  11667. {
  11668. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  11669. // Find the matching operator method
  11670. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, leftToRight, ctx);
  11671. if( r == 0 )
  11672. {
  11673. // Try again by switching the order of the operands, and using the reversed operator
  11674. r = CompileOverloadedDualOperator2(node, op_r, rctx, lctx, !leftToRight, ctx);
  11675. }
  11676. if( r == 1 )
  11677. {
  11678. // Success, don't continue
  11679. return true;
  11680. }
  11681. else if( r < 0 )
  11682. {
  11683. // Compiler error, don't continue
  11684. ctx->type.SetDummy();
  11685. return true;
  11686. }
  11687. }
  11688. // Assignment operators
  11689. op = 0;
  11690. if( isHandle )
  11691. {
  11692. // Only asOBJ_ASHANDLE types can get here
  11693. asASSERT( lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) );
  11694. asASSERT( token == ttAssignment );
  11695. if( token == ttAssignment )
  11696. op = "opHndlAssign";
  11697. }
  11698. else
  11699. {
  11700. switch( int(token) ) // convert to int to avoid warning in gnuc that not all values are tested
  11701. {
  11702. case ttAssignment: op = "opAssign"; break;
  11703. case ttAddAssign: op = "opAddAssign"; break;
  11704. case ttSubAssign: op = "opSubAssign"; break;
  11705. case ttMulAssign: op = "opMulAssign"; break;
  11706. case ttDivAssign: op = "opDivAssign"; break;
  11707. case ttModAssign: op = "opModAssign"; break;
  11708. case ttPowAssign: op = "opPowAssign"; break;
  11709. case ttOrAssign: op = "opOrAssign"; break;
  11710. case ttAndAssign: op = "opAndAssign"; break;
  11711. case ttXorAssign: op = "opXorAssign"; break;
  11712. case ttShiftLeftAssign: op = "opShlAssign"; break;
  11713. case ttShiftRightLAssign: op = "opShrAssign"; break;
  11714. case ttShiftRightAAssign: op = "opUShrAssign"; break;
  11715. }
  11716. }
  11717. if( op )
  11718. {
  11719. if( builder->engine->ep.disallowValueAssignForRefType &&
  11720. lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(lctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCOPED) )
  11721. {
  11722. if( token == ttAssignment )
  11723. Error(TXT_DISALLOW_ASSIGN_ON_REF_TYPE, node);
  11724. else
  11725. Error(TXT_DISALLOW_COMPOUND_ASSIGN_ON_REF_TYPE, node);
  11726. // Set a dummy output
  11727. ctx->type.Set(lctx->type.dataType);
  11728. return true;
  11729. }
  11730. // TODO: Shouldn't accept const lvalue with the assignment operators
  11731. // Find the matching operator method
  11732. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, false, ctx);
  11733. if( r == 1 )
  11734. {
  11735. // Success, don't continue
  11736. return true;
  11737. }
  11738. else if( r < 0 )
  11739. {
  11740. // Compiler error, don't continue
  11741. ctx->type.SetDummy();
  11742. return true;
  11743. }
  11744. }
  11745. // No suitable operator was found
  11746. return false;
  11747. }
  11748. // Returns negative on compile error
  11749. // zero on no matching operator
  11750. // one on matching operator
  11751. int asCCompiler::CompileOverloadedDualOperator2(asCScriptNode *node, const char *methodName, asCExprContext *lctx, asCExprContext *rctx, bool leftToRight, asCExprContext *ctx, bool specificReturn, const asCDataType &returnType)
  11752. {
  11753. // Find the matching method
  11754. if( lctx->type.dataType.IsObject() &&
  11755. (!lctx->type.isExplicitHandle ||
  11756. lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) &&
  11757. !lctx->type.IsNullConstant() )
  11758. {
  11759. asUINT n;
  11760. // Is the left value a const?
  11761. bool isConst = lctx->type.dataType.IsObjectConst();
  11762. asCArray<int> funcs;
  11763. asCObjectType *ot = CastToObjectType(lctx->type.dataType.GetTypeInfo());
  11764. asASSERT(ot);
  11765. for( n = 0; ot && n < ot->methods.GetLength(); n++ )
  11766. {
  11767. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  11768. asASSERT( func );
  11769. if( func && func->name == methodName &&
  11770. (!specificReturn || func->returnType == returnType) &&
  11771. func->parameterTypes.GetLength() == 1 &&
  11772. (!isConst || func->IsReadOnly()) )
  11773. {
  11774. // Make sure the method is accessible by the module
  11775. if( builder->module->m_accessMask & func->accessMask )
  11776. {
  11777. funcs.PushLast(func->id);
  11778. }
  11779. }
  11780. }
  11781. // Which is the best matching function?
  11782. asCArray<asSOverloadCandidate> tempFuncs;
  11783. MatchArgument(funcs, tempFuncs, rctx, 0);
  11784. // Find the lowest cost operator(s)
  11785. asCArray<int> ops;
  11786. asUINT bestCost = asUINT(-1);
  11787. for( n = 0; n < tempFuncs.GetLength(); ++n )
  11788. {
  11789. asUINT cost = tempFuncs[n].cost;
  11790. if( cost < bestCost )
  11791. {
  11792. ops.SetLength(0);
  11793. bestCost = cost;
  11794. }
  11795. if( cost == bestCost )
  11796. ops.PushLast(tempFuncs[n].funcId);
  11797. }
  11798. // If the object is not const, then we need to prioritize non-const methods
  11799. if( !isConst )
  11800. FilterConst(ops);
  11801. // Did we find an operator?
  11802. if( ops.GetLength() == 1 )
  11803. {
  11804. // Reserve the variables used in the right expression so the new temporary
  11805. // variable allocated for the left operand isn't accidentally overwritten.
  11806. int l = int(reservedVariables.GetLength());
  11807. rctx->bc.GetVarsUsed(reservedVariables);
  11808. // Process the lctx expression as get accessor
  11809. if( ProcessPropertyGetAccessor(lctx, node) < 0 )
  11810. return -1;
  11811. reservedVariables.SetLength(l);
  11812. asCExprContext tmpCtx(engine);
  11813. if (leftToRight)
  11814. {
  11815. // Make sure lctx is in fact a variable. If it is a reference there is no
  11816. // guarantee that the reference will stay alive throughout the evaluation of rctx
  11817. if (!lctx->type.isVariable)
  11818. {
  11819. // Reserve the variables used in the right expression so the new temporary
  11820. // variable allocated for the left operand isn't accidentally overwritten.
  11821. l = int(reservedVariables.GetLength());
  11822. rctx->bc.GetVarsUsed(reservedVariables);
  11823. if (engine->ep.allowUnsafeReferences && lctx->type.dataType.IsObject() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_VALUE))
  11824. {
  11825. // If the application allows unsafe references, then it is not necessary to
  11826. // make a copy of the object, just store the reference as a local variable
  11827. // Allocate a temporary variable as reference to the type
  11828. asCDataType dt = lctx->type.dataType;
  11829. dt.MakeReference(true);
  11830. int offset = AllocateVariable(dt, true, false, true);
  11831. Dereference(lctx, true);
  11832. // Copy the pointer to the temporary variable
  11833. lctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  11834. if (lctx->type.dataType.IsFuncdef())
  11835. lctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  11836. else
  11837. lctx->bc.InstrPTR(asBC_REFCPY, lctx->type.dataType.GetTypeInfo());
  11838. lctx->type.SetVariable(dt, offset, true);
  11839. }
  11840. else
  11841. {
  11842. if (lctx->type.dataType.SupportHandles())
  11843. lctx->type.dataType.MakeHandle(true);
  11844. PrepareTemporaryVariable(node, lctx);
  11845. }
  11846. reservedVariables.SetLength(l);
  11847. }
  11848. // Move the bytecode for the left operand to a temporary context
  11849. // so we can later make sure this is computed first
  11850. tmpCtx.bc.AddCode(&lctx->bc);
  11851. tmpCtx.bc.Instr(asBC_PopPtr);
  11852. // Add bytecode to push the object pointer computed in the left operand on the stack as the this pointer
  11853. // This will be placed after rctx by MakeFunctionCall below
  11854. lctx->bc.InstrWORD(asBC_PSF, lctx->type.stackOffset);
  11855. // Implicitly dereference handle parameters sent by reference
  11856. sVariable *v = variables->GetVariableByOffset(lctx->type.stackOffset);
  11857. if (v && v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()))
  11858. lctx->bc.Instr(asBC_RDSPtr);
  11859. }
  11860. else
  11861. {
  11862. // Make sure the rvalue doesn't have deferred temporary variables that are also used in the lvalue,
  11863. // since that would cause the VM to overwrite the variable while executing the bytecode for the lvalue.
  11864. asCArray<int> usedVars;
  11865. lctx->bc.GetVarsUsed(usedVars);
  11866. asUINT oldReservedVars = reservedVariables.GetLength();
  11867. for (n = 0; n < rctx->deferredParams.GetLength(); n++)
  11868. {
  11869. if (rctx->deferredParams[n].argType.isTemporary &&
  11870. usedVars.Exists(rctx->deferredParams[n].argType.stackOffset))
  11871. {
  11872. if (reservedVariables.GetLength() == oldReservedVars)
  11873. reservedVariables.Concatenate(usedVars);
  11874. // Allocate a new variable for the deferred argument
  11875. int offset = AllocateVariableNotIn(rctx->deferredParams[n].argType.dataType, true, false, rctx);
  11876. int oldVar = rctx->deferredParams[n].argType.stackOffset;
  11877. rctx->deferredParams[n].argType.stackOffset = short(offset);
  11878. rctx->bc.ExchangeVar(oldVar, offset);
  11879. ReleaseTemporaryVariable(oldVar, 0);
  11880. }
  11881. }
  11882. reservedVariables.SetLength(oldReservedVars);
  11883. }
  11884. // Merge the bytecode so that it forms lvalue.methodName(rvalue)
  11885. asCArray<asCExprContext *> args;
  11886. args.PushLast(rctx);
  11887. MergeExprBytecode(ctx, lctx);
  11888. ctx->type = lctx->type;
  11889. if( MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node) < 0 )
  11890. return -1;
  11891. // Rearrange the bytecode so the left argument is computed first
  11892. if (leftToRight)
  11893. {
  11894. tmpCtx.bc.AddCode(&ctx->bc);
  11895. ctx->bc.AddCode(&tmpCtx.bc);
  11896. }
  11897. // Found matching operator
  11898. return 1;
  11899. }
  11900. else if( ops.GetLength() > 1 )
  11901. {
  11902. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  11903. PrintMatchingFuncs(ops, node);
  11904. ctx->type.SetDummy();
  11905. // Compiler error
  11906. return -1;
  11907. }
  11908. }
  11909. // No matching operator
  11910. return 0;
  11911. }
  11912. int asCCompiler::MakeFunctionCall(asCExprContext *ctx, int funcId, asCObjectType *objectType, asCArray<asCExprContext*> &args, asCScriptNode *node, bool useVariable, int stackOffset, int funcPtrVar)
  11913. {
  11914. if( objectType )
  11915. Dereference(ctx, true);
  11916. // Store the expression node for error reporting
  11917. if( ctx->exprNode == 0 )
  11918. ctx->exprNode = node;
  11919. asCByteCode objBC(engine);
  11920. objBC.AddCode(&ctx->bc);
  11921. int r = PrepareFunctionCall(funcId, &ctx->bc, args);
  11922. if (r < 0)
  11923. return r;
  11924. // Verify if any of the args variable offsets are used in the other code.
  11925. // If they are exchange the offset for a new one
  11926. asUINT n;
  11927. for( n = 0; n < args.GetLength(); n++ )
  11928. {
  11929. if( args[n]->type.isTemporary && objBC.IsVarUsed(args[n]->type.stackOffset) )
  11930. {
  11931. // Release the current temporary variable
  11932. ReleaseTemporaryVariable(args[n]->type, 0);
  11933. asCDataType dt = args[n]->type.dataType;
  11934. dt.MakeReference(false);
  11935. int l = int(reservedVariables.GetLength());
  11936. objBC.GetVarsUsed(reservedVariables);
  11937. ctx->bc.GetVarsUsed(reservedVariables);
  11938. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(args[n]->type.stackOffset));
  11939. reservedVariables.SetLength(l);
  11940. asASSERT( IsVariableOnHeap(args[n]->type.stackOffset) == IsVariableOnHeap(newOffset) );
  11941. ctx->bc.ExchangeVar(args[n]->type.stackOffset, newOffset);
  11942. args[n]->type.stackOffset = (short)newOffset;
  11943. args[n]->type.isTemporary = true;
  11944. args[n]->type.isVariable = true;
  11945. }
  11946. }
  11947. // If the function will return a value type on the stack, then we must allocate space
  11948. // for that here and push the address on the stack as a hidden argument to the function
  11949. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  11950. if( func->DoesReturnOnStack() )
  11951. {
  11952. asASSERT(!useVariable);
  11953. useVariable = true;
  11954. stackOffset = AllocateVariable(func->returnType, true);
  11955. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  11956. }
  11957. ctx->bc.AddCode(&objBC);
  11958. MoveArgsToStack(funcId, &ctx->bc, args, objectType ? true : false);
  11959. PerformFunctionCall(funcId, ctx, false, &args, 0, useVariable, stackOffset, funcPtrVar);
  11960. return 0;
  11961. }
  11962. int asCCompiler::CompileOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op, bool leftToRight)
  11963. {
  11964. // Don't allow any operators on expressions that take address of class method, but allow it on global functions
  11965. if( (lctx->IsClassMethod()) || (rctx->IsClassMethod()) )
  11966. {
  11967. Error(TXT_INVALID_OP_ON_METHOD, node);
  11968. return -1;
  11969. }
  11970. // Don't allow any operators on void expressions
  11971. if( lctx->IsVoidExpression() || rctx->IsVoidExpression() )
  11972. {
  11973. Error(TXT_VOID_CANT_BE_OPERAND, node);
  11974. return -1;
  11975. }
  11976. if( op == ttUnrecognizedToken )
  11977. op = node->tokenType;
  11978. IsVariableInitialized(&lctx->type, node);
  11979. IsVariableInitialized(&rctx->type, node);
  11980. if( lctx->type.isExplicitHandle || rctx->type.isExplicitHandle ||
  11981. lctx->type.IsNullConstant() || rctx->type.IsNullConstant() ||
  11982. op == ttIs || op == ttNotIs )
  11983. {
  11984. CompileOperatorOnHandles(node, lctx, rctx, ctx, op);
  11985. return 0;
  11986. }
  11987. else
  11988. {
  11989. // Compile an overloaded operator for the two operands
  11990. if( CompileOverloadedDualOperator(node, lctx, rctx, leftToRight, ctx, false, op) )
  11991. return 0;
  11992. // If both operands are objects, then we shouldn't continue
  11993. if( lctx->type.dataType.IsObject() && rctx->type.dataType.IsObject() )
  11994. {
  11995. asCString str;
  11996. str.Format(TXT_NO_MATCHING_OP_FOUND_FOR_TYPES_s_AND_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), rctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11997. Error(str, node);
  11998. ctx->type.SetDummy();
  11999. return -1;
  12000. }
  12001. // Process the property get accessors (if any)
  12002. if( ProcessPropertyGetAccessor(lctx, node) < 0 )
  12003. return -1;
  12004. if( ProcessPropertyGetAccessor(rctx, node) < 0 )
  12005. return -1;
  12006. // Make sure we have two variables or constants
  12007. if( lctx->type.dataType.IsReference() ) ConvertToVariableNotIn(lctx, rctx);
  12008. if( rctx->type.dataType.IsReference() ) ConvertToVariableNotIn(rctx, lctx);
  12009. // Make sure lctx doesn't end up with a variable used in rctx
  12010. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  12011. {
  12012. int offset = AllocateVariableNotIn(lctx->type.dataType, true, false, rctx);
  12013. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  12014. ReleaseTemporaryVariable(offset, 0);
  12015. }
  12016. // Math operators
  12017. // + - * / % ** += -= *= /= %= **=
  12018. if( op == ttPlus || op == ttAddAssign ||
  12019. op == ttMinus || op == ttSubAssign ||
  12020. op == ttStar || op == ttMulAssign ||
  12021. op == ttSlash || op == ttDivAssign ||
  12022. op == ttPercent || op == ttModAssign ||
  12023. op == ttStarStar || op == ttPowAssign )
  12024. {
  12025. CompileMathOperator(node, lctx, rctx, ctx, op);
  12026. return 0;
  12027. }
  12028. // Bitwise operators
  12029. // << >> >>> & | ^ <<= >>= >>>= &= |= ^=
  12030. if( op == ttAmp || op == ttAndAssign ||
  12031. op == ttBitOr || op == ttOrAssign ||
  12032. op == ttBitXor || op == ttXorAssign ||
  12033. op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  12034. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  12035. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  12036. {
  12037. CompileBitwiseOperator(node, lctx, rctx, ctx, op);
  12038. return 0;
  12039. }
  12040. // Comparison operators
  12041. // == != < > <= >=
  12042. if( op == ttEqual || op == ttNotEqual ||
  12043. op == ttLessThan || op == ttLessThanOrEqual ||
  12044. op == ttGreaterThan || op == ttGreaterThanOrEqual )
  12045. {
  12046. CompileComparisonOperator(node, lctx, rctx, ctx, op);
  12047. return 0;
  12048. }
  12049. // Boolean operators
  12050. // && || ^^
  12051. if( op == ttAnd || op == ttOr || op == ttXor )
  12052. {
  12053. CompileBooleanOperator(node, lctx, rctx, ctx, op);
  12054. return 0;
  12055. }
  12056. }
  12057. asASSERT(false);
  12058. return -1;
  12059. }
  12060. void asCCompiler::ConvertToTempVariableNotIn(asCExprContext *ctx, asCExprContext *exclude)
  12061. {
  12062. int l = int(reservedVariables.GetLength());
  12063. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  12064. ConvertToTempVariable(ctx);
  12065. reservedVariables.SetLength(l);
  12066. }
  12067. void asCCompiler::ConvertToTempVariable(asCExprContext *ctx)
  12068. {
  12069. // This is only used for primitive types and null handles
  12070. asASSERT( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsNullHandle() );
  12071. ConvertToVariable(ctx);
  12072. if( !ctx->type.isTemporary )
  12073. {
  12074. if( ctx->type.dataType.IsPrimitive() )
  12075. {
  12076. // Copy the variable to a temporary variable
  12077. int offset = AllocateVariable(ctx->type.dataType, true);
  12078. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12079. ctx->bc.InstrW_W(asBC_CpyVtoV4, offset, ctx->type.stackOffset);
  12080. else
  12081. ctx->bc.InstrW_W(asBC_CpyVtoV8, offset, ctx->type.stackOffset);
  12082. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  12083. }
  12084. else
  12085. {
  12086. // We should never get here
  12087. asASSERT(false);
  12088. }
  12089. }
  12090. }
  12091. void asCCompiler::ConvertToVariable(asCExprContext *ctx)
  12092. {
  12093. // We should never get here while the context is still an unprocessed property accessor
  12094. asASSERT(ctx->property_get == 0 && ctx->property_set == 0);
  12095. int offset;
  12096. if( !ctx->type.isVariable &&
  12097. (ctx->type.dataType.IsObjectHandle() ||
  12098. (ctx->type.dataType.IsObject() && ctx->type.dataType.SupportHandles())) )
  12099. {
  12100. offset = AllocateVariable(ctx->type.dataType, true);
  12101. if( ctx->type.IsNullConstant() )
  12102. {
  12103. if( ctx->bc.GetLastInstr() == asBC_PshNull )
  12104. ctx->bc.Instr(asBC_PopPtr); // Pop the null constant pushed onto the stack
  12105. ctx->bc.InstrSHORT(asBC_ClrVPtr, (short)offset);
  12106. }
  12107. else
  12108. {
  12109. Dereference(ctx, true);
  12110. // Copy the object handle to a variable
  12111. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  12112. if( ctx->type.dataType.IsFuncdef() )
  12113. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  12114. else
  12115. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  12116. ctx->bc.Instr(asBC_PopPtr);
  12117. }
  12118. // As this is an object the reference must be placed on the stack
  12119. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  12120. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  12121. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  12122. ctx->type.dataType.MakeHandle(true);
  12123. ctx->type.dataType.MakeReference(true);
  12124. }
  12125. else if( (!ctx->type.isVariable || ctx->type.dataType.IsReference()) &&
  12126. ctx->type.dataType.IsPrimitive() )
  12127. {
  12128. if( ctx->type.isConstant )
  12129. {
  12130. offset = AllocateVariable(ctx->type.dataType, true);
  12131. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  12132. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, ctx->type.GetConstantB());
  12133. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  12134. ctx->bc.InstrSHORT_W(asBC_SetV2, (short)offset, ctx->type.GetConstantW());
  12135. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  12136. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, ctx->type.GetConstantDW());
  12137. else
  12138. ctx->bc.InstrSHORT_QW(asBC_SetV8, (short)offset, ctx->type.GetConstantQW());
  12139. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  12140. return;
  12141. }
  12142. else
  12143. {
  12144. asASSERT(ctx->type.dataType.IsPrimitive());
  12145. asASSERT(ctx->type.dataType.IsReference());
  12146. ctx->type.dataType.MakeReference(false);
  12147. offset = AllocateVariable(ctx->type.dataType, true);
  12148. // Read the value from the address in the register directly into the variable
  12149. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  12150. ctx->bc.InstrSHORT(asBC_RDR1, (short)offset);
  12151. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  12152. ctx->bc.InstrSHORT(asBC_RDR2, (short)offset);
  12153. else if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12154. ctx->bc.InstrSHORT(asBC_RDR4, (short)offset);
  12155. else
  12156. ctx->bc.InstrSHORT(asBC_RDR8, (short)offset);
  12157. }
  12158. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  12159. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  12160. }
  12161. }
  12162. void asCCompiler::ConvertToVariableNotIn(asCExprContext *ctx, asCExprContext *exclude)
  12163. {
  12164. int l = int(reservedVariables.GetLength());
  12165. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  12166. ConvertToVariable(ctx);
  12167. reservedVariables.SetLength(l);
  12168. }
  12169. void asCCompiler::ImplicitConvObjectToBestMathType(asCExprContext *ctx, asCScriptNode *node)
  12170. {
  12171. asCArray<int> funcs;
  12172. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  12173. if( ot )
  12174. {
  12175. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  12176. {
  12177. // Consider only implicit casts
  12178. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  12179. if( func->name == "opImplConv" &&
  12180. func->returnType.IsPrimitive() &&
  12181. func->parameterTypes.GetLength() == 0 )
  12182. funcs.PushLast(ot->methods[n]);
  12183. }
  12184. // Use the one with the highest precision
  12185. const eTokenType match[10] = {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8};
  12186. while( funcs.GetLength() > 1 )
  12187. {
  12188. eTokenType returnType = builder->GetFunctionDescription(funcs[0])->returnType.GetTokenType();
  12189. int value1 = 11, value2 = 11;
  12190. for( asUINT i = 0; i < 10; i++ )
  12191. {
  12192. if( returnType == match[i] )
  12193. {
  12194. value1 = i;
  12195. break;
  12196. }
  12197. }
  12198. for( asUINT n = 1; n < funcs.GetLength(); n++ )
  12199. {
  12200. returnType = builder->GetFunctionDescription(funcs[n])->returnType.GetTokenType();
  12201. for( asUINT i = 0; i < 10; i++ )
  12202. {
  12203. if( returnType == match[i] )
  12204. {
  12205. value2 = i;
  12206. break;
  12207. }
  12208. }
  12209. if( value2 >= value1 )
  12210. {
  12211. // Remove this and continue searching
  12212. funcs.RemoveIndexUnordered(n--);
  12213. }
  12214. else
  12215. {
  12216. // Remove the first, and start over
  12217. funcs.RemoveIndexUnordered(0);
  12218. break;
  12219. }
  12220. }
  12221. }
  12222. // Do the conversion
  12223. if( funcs.GetLength() )
  12224. ImplicitConvObjectToPrimitive(ctx, builder->GetFunctionDescription(funcs[0])->returnType, node, asIC_IMPLICIT_CONV);
  12225. }
  12226. }
  12227. void asCCompiler::CompileMathOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  12228. {
  12229. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  12230. // TODO: clean up: This initial part is identical to CompileComparisonOperator. Make a common function out of it
  12231. // If either operand is a non-primitive then use the primitive type
  12232. if( !lctx->type.dataType.IsPrimitive() )
  12233. {
  12234. int l = int(reservedVariables.GetLength());
  12235. rctx->bc.GetVarsUsed(reservedVariables);
  12236. ImplicitConvObjectToBestMathType(lctx, node);
  12237. reservedVariables.SetLength(l);
  12238. }
  12239. if( !rctx->type.dataType.IsPrimitive() )
  12240. {
  12241. int l = int(reservedVariables.GetLength());
  12242. lctx->bc.GetVarsUsed(reservedVariables);
  12243. ImplicitConvObjectToBestMathType(rctx, node);
  12244. reservedVariables.SetLength(l);
  12245. }
  12246. // Both types must now be primitives. Implicitly convert them so they match
  12247. asCDataType to;
  12248. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  12249. to.SetTokenType(ttDouble);
  12250. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  12251. to.SetTokenType(ttFloat);
  12252. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12253. {
  12254. // Convert to int64 if both are signed or if one is non-constant and signed
  12255. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  12256. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  12257. to.SetTokenType(ttInt64);
  12258. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  12259. to.SetTokenType(ttUInt64);
  12260. else
  12261. to.SetTokenType(ttInt64);
  12262. }
  12263. else
  12264. {
  12265. // Convert to int32 if both are signed or if one is non-constant and signed
  12266. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  12267. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  12268. to.SetTokenType(ttInt);
  12269. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  12270. to.SetTokenType(ttUInt);
  12271. else
  12272. to.SetTokenType(ttInt);
  12273. }
  12274. // If doing an operation with double constant and float variable, the constant should be converted to float
  12275. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  12276. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  12277. to.SetTokenType(ttFloat);
  12278. if( op == ttUnrecognizedToken )
  12279. op = node->tokenType;
  12280. // If integer division is disabled, convert to floating-point
  12281. if( engine->ep.disableIntegerDivision &&
  12282. (op == ttSlash || op == ttDivAssign) &&
  12283. (to.IsIntegerType() || to.IsUnsignedType()) )
  12284. {
  12285. // Use double to avoid losing precision when dividing with 32bit ints
  12286. // For 64bit ints there is unfortunately no greater type so with those
  12287. // there is still a risk of loosing precision
  12288. to.SetTokenType(ttDouble);
  12289. }
  12290. // Do the actual conversion
  12291. int l = int(reservedVariables.GetLength());
  12292. rctx->bc.GetVarsUsed(reservedVariables);
  12293. lctx->bc.GetVarsUsed(reservedVariables);
  12294. if( lctx->type.dataType.IsReference() )
  12295. ConvertToVariable(lctx);
  12296. if( rctx->type.dataType.IsReference() )
  12297. ConvertToVariable(rctx);
  12298. if( to.IsPrimitive() )
  12299. {
  12300. // ttStarStar allows an integer, right-hand operand and a double
  12301. // left-hand operand.
  12302. if( (op == ttStarStar || op == ttPowAssign) &&
  12303. lctx->type.dataType.IsDoubleType() &&
  12304. (rctx->type.dataType.IsIntegerType() ||
  12305. rctx->type.dataType.IsUnsignedType()) )
  12306. {
  12307. to.SetTokenType(ttInt);
  12308. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  12309. to.SetTokenType(ttDouble);
  12310. }
  12311. else
  12312. {
  12313. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  12314. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  12315. }
  12316. }
  12317. reservedVariables.SetLength(l);
  12318. // Verify that the conversion was successful
  12319. if( !lctx->type.dataType.IsIntegerType() &&
  12320. !lctx->type.dataType.IsUnsignedType() &&
  12321. !lctx->type.dataType.IsFloatType() &&
  12322. !lctx->type.dataType.IsDoubleType() )
  12323. {
  12324. asCString str;
  12325. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  12326. Error(str, node);
  12327. ctx->type.SetDummy();
  12328. return;
  12329. }
  12330. if( !rctx->type.dataType.IsIntegerType() &&
  12331. !rctx->type.dataType.IsUnsignedType() &&
  12332. !rctx->type.dataType.IsFloatType() &&
  12333. !rctx->type.dataType.IsDoubleType() )
  12334. {
  12335. asCString str;
  12336. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  12337. Error(str, node);
  12338. ctx->type.SetDummy();
  12339. return;
  12340. }
  12341. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  12342. // Verify if we are dividing with a constant zero
  12343. if( rctx->type.isConstant &&
  12344. (op == ttSlash || op == ttDivAssign ||
  12345. op == ttPercent || op == ttModAssign) &&
  12346. ((rctx->type.dataType.GetSizeInMemoryBytes() == 4 && rctx->type.GetConstantDW() == 0) ||
  12347. (rctx->type.dataType.GetSizeInMemoryBytes() == 8 && rctx->type.GetConstantQW() == 0) ||
  12348. (rctx->type.dataType.GetSizeInMemoryBytes() == 1 && rctx->type.GetConstantB() == 0) ||
  12349. (rctx->type.dataType.GetSizeInMemoryBytes() == 2 && rctx->type.GetConstantW() == 0)) )
  12350. {
  12351. Error(TXT_DIVIDE_BY_ZERO, node);
  12352. }
  12353. if( !isConstant )
  12354. {
  12355. ConvertToVariableNotIn(lctx, rctx);
  12356. ConvertToVariableNotIn(rctx, lctx);
  12357. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  12358. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  12359. if( op == ttAddAssign || op == ttSubAssign ||
  12360. op == ttMulAssign || op == ttDivAssign ||
  12361. op == ttModAssign || op == ttPowAssign )
  12362. {
  12363. // Merge the operands in the different order so that they are evaluated correctly
  12364. MergeExprBytecode(ctx, rctx);
  12365. MergeExprBytecode(ctx, lctx);
  12366. // We must not process the deferred parameters yet, as
  12367. // it may overwrite the lvalue kept in the register
  12368. }
  12369. else
  12370. {
  12371. MergeExprBytecode(ctx, lctx);
  12372. MergeExprBytecode(ctx, rctx);
  12373. ProcessDeferredParams(ctx);
  12374. }
  12375. asEBCInstr instruction = asBC_ADDi;
  12376. if( lctx->type.dataType.IsIntegerType() ||
  12377. lctx->type.dataType.IsUnsignedType() )
  12378. {
  12379. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12380. {
  12381. if( op == ttPlus || op == ttAddAssign )
  12382. instruction = asBC_ADDi;
  12383. else if( op == ttMinus || op == ttSubAssign )
  12384. instruction = asBC_SUBi;
  12385. else if( op == ttStar || op == ttMulAssign )
  12386. instruction = asBC_MULi;
  12387. else if( op == ttSlash || op == ttDivAssign )
  12388. {
  12389. if( lctx->type.dataType.IsIntegerType() )
  12390. instruction = asBC_DIVi;
  12391. else
  12392. instruction = asBC_DIVu;
  12393. }
  12394. else if( op == ttPercent || op == ttModAssign )
  12395. {
  12396. if( lctx->type.dataType.IsIntegerType() )
  12397. instruction = asBC_MODi;
  12398. else
  12399. instruction = asBC_MODu;
  12400. }
  12401. else if( op == ttStarStar || op == ttPowAssign )
  12402. {
  12403. if( lctx->type.dataType.IsIntegerType() )
  12404. instruction = asBC_POWi;
  12405. else
  12406. instruction = asBC_POWu;
  12407. }
  12408. }
  12409. else
  12410. {
  12411. if( op == ttPlus || op == ttAddAssign )
  12412. instruction = asBC_ADDi64;
  12413. else if( op == ttMinus || op == ttSubAssign )
  12414. instruction = asBC_SUBi64;
  12415. else if( op == ttStar || op == ttMulAssign )
  12416. instruction = asBC_MULi64;
  12417. else if( op == ttSlash || op == ttDivAssign )
  12418. {
  12419. if( lctx->type.dataType.IsIntegerType() )
  12420. instruction = asBC_DIVi64;
  12421. else
  12422. instruction = asBC_DIVu64;
  12423. }
  12424. else if( op == ttPercent || op == ttModAssign )
  12425. {
  12426. if( lctx->type.dataType.IsIntegerType() )
  12427. instruction = asBC_MODi64;
  12428. else
  12429. instruction = asBC_MODu64;
  12430. }
  12431. else if( op == ttStarStar || op == ttPowAssign )
  12432. {
  12433. if( lctx->type.dataType.IsIntegerType() )
  12434. instruction = asBC_POWi64;
  12435. else
  12436. instruction = asBC_POWu64;
  12437. }
  12438. }
  12439. }
  12440. else if( lctx->type.dataType.IsFloatType() )
  12441. {
  12442. if( op == ttPlus || op == ttAddAssign )
  12443. instruction = asBC_ADDf;
  12444. else if( op == ttMinus || op == ttSubAssign )
  12445. instruction = asBC_SUBf;
  12446. else if( op == ttStar || op == ttMulAssign )
  12447. instruction = asBC_MULf;
  12448. else if( op == ttSlash || op == ttDivAssign )
  12449. instruction = asBC_DIVf;
  12450. else if( op == ttPercent || op == ttModAssign )
  12451. instruction = asBC_MODf;
  12452. else if( op == ttStarStar || op == ttPowAssign )
  12453. instruction = asBC_POWf;
  12454. }
  12455. else if( lctx->type.dataType.IsDoubleType() )
  12456. {
  12457. if( rctx->type.dataType.IsIntegerType() )
  12458. {
  12459. asASSERT(rctx->type.dataType.GetSizeInMemoryDWords() == 1);
  12460. if( op == ttStarStar || op == ttPowAssign )
  12461. instruction = asBC_POWdi;
  12462. else
  12463. asASSERT(false); // Should not be possible
  12464. }
  12465. else
  12466. {
  12467. if( op == ttPlus || op == ttAddAssign )
  12468. instruction = asBC_ADDd;
  12469. else if( op == ttMinus || op == ttSubAssign )
  12470. instruction = asBC_SUBd;
  12471. else if( op == ttStar || op == ttMulAssign )
  12472. instruction = asBC_MULd;
  12473. else if( op == ttSlash || op == ttDivAssign )
  12474. instruction = asBC_DIVd;
  12475. else if( op == ttPercent || op == ttModAssign )
  12476. instruction = asBC_MODd;
  12477. else if( op == ttStarStar || op == ttPowAssign )
  12478. instruction = asBC_POWd;
  12479. }
  12480. }
  12481. else
  12482. {
  12483. // Shouldn't be possible
  12484. asASSERT(false);
  12485. }
  12486. // Do the operation
  12487. int a = AllocateVariable(lctx->type.dataType, true);
  12488. int b = lctx->type.stackOffset;
  12489. int c = rctx->type.stackOffset;
  12490. ctx->bc.InstrW_W_W(instruction, a, b, c);
  12491. ctx->type.SetVariable(lctx->type.dataType, a, true);
  12492. }
  12493. else
  12494. {
  12495. // Both values are constants
  12496. if( lctx->type.dataType.IsIntegerType() ||
  12497. lctx->type.dataType.IsUnsignedType() )
  12498. {
  12499. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12500. {
  12501. int v = 0;
  12502. if( op == ttPlus )
  12503. v = int(lctx->type.GetConstantDW()) + int(rctx->type.GetConstantDW());
  12504. else if( op == ttMinus )
  12505. v = int(lctx->type.GetConstantDW()) - int(rctx->type.GetConstantDW());
  12506. else if( op == ttStar )
  12507. v = int(lctx->type.GetConstantDW()) * int(rctx->type.GetConstantDW());
  12508. else if( op == ttSlash )
  12509. {
  12510. // TODO: Should probably report an error, rather than silently convert the value to 0
  12511. if( rctx->type.GetConstantDW() == 0 || (int(rctx->type.GetConstantDW()) == -1 && lctx->type.GetConstantDW() == 0x80000000) )
  12512. v = 0;
  12513. else
  12514. if( lctx->type.dataType.IsIntegerType() )
  12515. v = int(lctx->type.GetConstantDW()) / int(rctx->type.GetConstantDW());
  12516. else
  12517. v = lctx->type.GetConstantDW() / rctx->type.GetConstantDW();
  12518. }
  12519. else if( op == ttPercent )
  12520. {
  12521. // TODO: Should probably report an error, rather than silently convert the value to 0
  12522. if( rctx->type.GetConstantDW() == 0 || (int(rctx->type.GetConstantDW()) == -1 && lctx->type.GetConstantDW() == 0x80000000) )
  12523. v = 0;
  12524. else
  12525. if( lctx->type.dataType.IsIntegerType() )
  12526. v = int(lctx->type.GetConstantDW()) % int(rctx->type.GetConstantDW());
  12527. else
  12528. v = lctx->type.GetConstantDW() % rctx->type.GetConstantDW();
  12529. }
  12530. else if( op == ttStarStar )
  12531. {
  12532. bool isOverflow;
  12533. if( lctx->type.dataType.IsIntegerType() )
  12534. v = as_powi(int(lctx->type.GetConstantDW()), int(rctx->type.GetConstantDW()), isOverflow);
  12535. else
  12536. v = as_powu(lctx->type.GetConstantDW(), rctx->type.GetConstantDW(), isOverflow);
  12537. if( isOverflow )
  12538. Error(TXT_POW_OVERFLOW, node);
  12539. }
  12540. ctx->type.SetConstantDW(lctx->type.dataType, v);
  12541. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  12542. if( lctx->type.dataType.GetTokenType() == ttUInt && op == ttMinus && lctx->type.GetConstantDW() < rctx->type.GetConstantDW())
  12543. ctx->type.dataType.SetTokenType(ttInt);
  12544. }
  12545. else
  12546. {
  12547. asQWORD v = 0;
  12548. if( op == ttPlus )
  12549. v = asINT64(lctx->type.GetConstantQW()) + asINT64(rctx->type.GetConstantQW());
  12550. else if( op == ttMinus )
  12551. v = asINT64(lctx->type.GetConstantQW()) - asINT64(rctx->type.GetConstantQW());
  12552. else if( op == ttStar )
  12553. v = asINT64(lctx->type.GetConstantQW()) * asINT64(rctx->type.GetConstantQW());
  12554. else if( op == ttSlash )
  12555. {
  12556. // TODO: Should probably report an error, rather than silently convert the value to 0
  12557. if( rctx->type.GetConstantQW() == 0 || (rctx->type.GetConstantQW() == asQWORD(-1) && lctx->type.GetConstantQW() == (asQWORD(1)<<63)) )
  12558. v = 0;
  12559. else
  12560. if( lctx->type.dataType.IsIntegerType() )
  12561. v = asINT64(lctx->type.GetConstantQW()) / asINT64(rctx->type.GetConstantQW());
  12562. else
  12563. v = lctx->type.GetConstantQW() / rctx->type.GetConstantQW();
  12564. }
  12565. else if( op == ttPercent )
  12566. {
  12567. // TODO: Should probably report an error, rather than silently convert the value to 0
  12568. if( rctx->type.GetConstantQW() == 0 || (rctx->type.GetConstantQW() == asQWORD(-1) && lctx->type.GetConstantQW() == (asQWORD(1)<<63)) )
  12569. v = 0;
  12570. else
  12571. if( lctx->type.dataType.IsIntegerType() )
  12572. v = asINT64(lctx->type.GetConstantQW()) % asINT64(rctx->type.GetConstantQW());
  12573. else
  12574. v = lctx->type.GetConstantQW() % rctx->type.GetConstantQW();
  12575. }
  12576. else if( op == ttStarStar )
  12577. {
  12578. bool isOverflow;
  12579. if( lctx->type.dataType.IsIntegerType() )
  12580. v = as_powi64(asINT64(lctx->type.GetConstantQW()), asINT64(rctx->type.GetConstantQW()), isOverflow);
  12581. else
  12582. v = as_powu64(lctx->type.GetConstantQW(), rctx->type.GetConstantQW(), isOverflow);
  12583. if( isOverflow )
  12584. Error(TXT_POW_OVERFLOW, node);
  12585. }
  12586. ctx->type.SetConstantQW(lctx->type.dataType, v);
  12587. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  12588. if( lctx->type.dataType.GetTokenType() == ttUInt64 && op == ttMinus && lctx->type.GetConstantQW() < rctx->type.GetConstantQW())
  12589. ctx->type.dataType.SetTokenType(ttInt64);
  12590. }
  12591. }
  12592. else if( lctx->type.dataType.IsFloatType() )
  12593. {
  12594. float v = 0.0f;
  12595. if( op == ttPlus )
  12596. v = lctx->type.GetConstantF() + rctx->type.GetConstantF();
  12597. else if( op == ttMinus )
  12598. v = lctx->type.GetConstantF() - rctx->type.GetConstantF();
  12599. else if( op == ttStar )
  12600. v = lctx->type.GetConstantF() * rctx->type.GetConstantF();
  12601. else if( op == ttSlash )
  12602. {
  12603. if( rctx->type.GetConstantF() == 0 )
  12604. v = 0;
  12605. else
  12606. v = lctx->type.GetConstantF() / rctx->type.GetConstantF();
  12607. }
  12608. else if( op == ttPercent )
  12609. {
  12610. if( rctx->type.GetConstantF() == 0 )
  12611. v = 0;
  12612. else
  12613. v = fmodf(lctx->type.GetConstantF(), rctx->type.GetConstantF());
  12614. }
  12615. else if( op == ttStarStar )
  12616. {
  12617. v = powf(lctx->type.GetConstantF(), rctx->type.GetConstantF());
  12618. if( v == HUGE_VAL )
  12619. Error(TXT_POW_OVERFLOW, node);
  12620. }
  12621. ctx->type.SetConstantF(lctx->type.dataType, v);
  12622. }
  12623. else if( lctx->type.dataType.IsDoubleType() )
  12624. {
  12625. double v = 0.0;
  12626. if( rctx->type.dataType.IsIntegerType() )
  12627. {
  12628. asASSERT(rctx->type.dataType.GetSizeInMemoryDWords() == 1);
  12629. if( op == ttStarStar || op == ttPowAssign )
  12630. {
  12631. v = pow(lctx->type.GetConstantD(), int(rctx->type.GetConstantDW()));
  12632. if( v == HUGE_VAL )
  12633. Error(TXT_POW_OVERFLOW, node);
  12634. }
  12635. else
  12636. asASSERT(false); // Should not be possible
  12637. }
  12638. else
  12639. {
  12640. if( op == ttPlus )
  12641. v = lctx->type.GetConstantD() + rctx->type.GetConstantD();
  12642. else if( op == ttMinus )
  12643. v = lctx->type.GetConstantD() - rctx->type.GetConstantD();
  12644. else if( op == ttStar )
  12645. v = lctx->type.GetConstantD() * rctx->type.GetConstantD();
  12646. else if( op == ttSlash )
  12647. {
  12648. if( rctx->type.GetConstantD() == 0 )
  12649. v = 0;
  12650. else
  12651. v = lctx->type.GetConstantD() / rctx->type.GetConstantD();
  12652. }
  12653. else if( op == ttPercent )
  12654. {
  12655. if( rctx->type.GetConstantD() == 0 )
  12656. v = 0;
  12657. else
  12658. v = fmod(lctx->type.GetConstantD(), rctx->type.GetConstantD());
  12659. }
  12660. else if( op == ttStarStar )
  12661. {
  12662. v = pow(lctx->type.GetConstantD(), rctx->type.GetConstantD());
  12663. if( v == HUGE_VAL )
  12664. Error(TXT_POW_OVERFLOW, node);
  12665. }
  12666. }
  12667. ctx->type.SetConstantD(lctx->type.dataType, v);
  12668. }
  12669. else
  12670. {
  12671. // Shouldn't be possible
  12672. asASSERT(false);
  12673. }
  12674. }
  12675. }
  12676. void asCCompiler::CompileBitwiseOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  12677. {
  12678. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  12679. if( op == ttUnrecognizedToken )
  12680. op = node->tokenType;
  12681. if( op == ttAmp || op == ttAndAssign ||
  12682. op == ttBitOr || op == ttOrAssign ||
  12683. op == ttBitXor || op == ttXorAssign )
  12684. {
  12685. // Also do not permit float/double to be implicitly converted to integer in this case
  12686. // as the user may think the result is a bitwise operation on the float value but it's not
  12687. if (lctx->type.dataType.IsFloatType() || lctx->type.dataType.IsDoubleType())
  12688. {
  12689. asCString str;
  12690. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  12691. Error(str, node);
  12692. // Set an integer value and allow the compiler to continue
  12693. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  12694. return;
  12695. }
  12696. if (rctx->type.dataType.IsFloatType() || rctx->type.dataType.IsDoubleType())
  12697. {
  12698. asCString str;
  12699. str.Format(TXT_ILLEGAL_OPERATION_ON_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  12700. Error(str, node);
  12701. // Set an integer value and allow the compiler to continue
  12702. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  12703. return;
  12704. }
  12705. // Convert left hand operand to integer if it's not already one
  12706. asCDataType to;
  12707. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 ||
  12708. rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12709. to.SetTokenType(ttInt64);
  12710. else
  12711. to.SetTokenType(ttInt);
  12712. // Do the actual conversion (keep sign/unsigned if possible)
  12713. int l = int(reservedVariables.GetLength());
  12714. rctx->bc.GetVarsUsed(reservedVariables);
  12715. if( lctx->type.dataType.IsUnsignedType() )
  12716. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttUInt : ttUInt64 );
  12717. else
  12718. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttInt : ttInt64 );
  12719. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  12720. reservedVariables.SetLength(l);
  12721. // Verify that the conversion was successful
  12722. if( lctx->type.dataType != to )
  12723. {
  12724. asCString str;
  12725. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  12726. Error(str, node);
  12727. }
  12728. // Convert right hand operand to same size as left hand
  12729. l = int(reservedVariables.GetLength());
  12730. lctx->bc.GetVarsUsed(reservedVariables);
  12731. if( rctx->type.dataType.IsUnsignedType() )
  12732. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttUInt : ttUInt64 );
  12733. else
  12734. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttInt : ttInt64 );
  12735. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  12736. reservedVariables.SetLength(l);
  12737. if( rctx->type.dataType != to )
  12738. {
  12739. asCString str;
  12740. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  12741. Error(str, node);
  12742. }
  12743. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  12744. if( !isConstant )
  12745. {
  12746. ConvertToVariableNotIn(lctx, rctx);
  12747. ConvertToVariableNotIn(rctx, lctx);
  12748. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  12749. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  12750. if( op == ttAndAssign || op == ttOrAssign || op == ttXorAssign )
  12751. {
  12752. // Compound assignments execute the right hand value first
  12753. MergeExprBytecode(ctx, rctx);
  12754. MergeExprBytecode(ctx, lctx);
  12755. }
  12756. else
  12757. {
  12758. MergeExprBytecode(ctx, lctx);
  12759. MergeExprBytecode(ctx, rctx);
  12760. }
  12761. ProcessDeferredParams(ctx);
  12762. asEBCInstr instruction = asBC_BAND;
  12763. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12764. {
  12765. if( op == ttAmp || op == ttAndAssign )
  12766. instruction = asBC_BAND;
  12767. else if( op == ttBitOr || op == ttOrAssign )
  12768. instruction = asBC_BOR;
  12769. else if( op == ttBitXor || op == ttXorAssign )
  12770. instruction = asBC_BXOR;
  12771. }
  12772. else
  12773. {
  12774. if( op == ttAmp || op == ttAndAssign )
  12775. instruction = asBC_BAND64;
  12776. else if( op == ttBitOr || op == ttOrAssign )
  12777. instruction = asBC_BOR64;
  12778. else if( op == ttBitXor || op == ttXorAssign )
  12779. instruction = asBC_BXOR64;
  12780. }
  12781. // Do the operation
  12782. int a = AllocateVariable(lctx->type.dataType, true);
  12783. int b = lctx->type.stackOffset;
  12784. int c = rctx->type.stackOffset;
  12785. ctx->bc.InstrW_W_W(instruction, a, b, c);
  12786. ctx->type.SetVariable(lctx->type.dataType, a, true);
  12787. }
  12788. else
  12789. {
  12790. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12791. {
  12792. asQWORD v = 0;
  12793. if( op == ttAmp )
  12794. v = lctx->type.GetConstantQW() & rctx->type.GetConstantQW();
  12795. else if( op == ttBitOr )
  12796. v = lctx->type.GetConstantQW() | rctx->type.GetConstantQW();
  12797. else if( op == ttBitXor )
  12798. v = lctx->type.GetConstantQW() ^ rctx->type.GetConstantQW();
  12799. // Remember the result
  12800. ctx->type.SetConstantQW(lctx->type.dataType, v);
  12801. }
  12802. else
  12803. {
  12804. asDWORD v = 0;
  12805. if( op == ttAmp )
  12806. v = lctx->type.GetConstantDW() & rctx->type.GetConstantDW();
  12807. else if( op == ttBitOr )
  12808. v = lctx->type.GetConstantDW() | rctx->type.GetConstantDW();
  12809. else if( op == ttBitXor )
  12810. v = lctx->type.GetConstantDW() ^ rctx->type.GetConstantDW();
  12811. // Remember the result
  12812. ctx->type.SetConstantDW(lctx->type.dataType, v);
  12813. }
  12814. }
  12815. }
  12816. else if( op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  12817. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  12818. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  12819. {
  12820. // Don't permit object to primitive conversion, since we don't know which integer type is the correct one
  12821. // Also do not permit float/double to be implicitly converted to integer in this case
  12822. if( lctx->type.dataType.IsObject() || lctx->type.dataType.IsFloatType() || lctx->type.dataType.IsDoubleType() )
  12823. {
  12824. asCString str;
  12825. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  12826. Error(str, node);
  12827. // Set an integer value and allow the compiler to continue
  12828. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  12829. return;
  12830. }
  12831. // Convert left hand operand to integer if it's not already one
  12832. asCDataType to = lctx->type.dataType;
  12833. if( lctx->type.dataType.IsUnsignedType() &&
  12834. lctx->type.dataType.GetSizeInMemoryBytes() < 4 )
  12835. {
  12836. // Upgrade to 32bit
  12837. to = asCDataType::CreatePrimitive(ttUInt, false);
  12838. }
  12839. else if( !lctx->type.dataType.IsUnsignedType() )
  12840. {
  12841. if (lctx->type.dataType.GetSizeInMemoryDWords() == 2)
  12842. to = asCDataType::CreatePrimitive(ttInt64, false);
  12843. else
  12844. to = asCDataType::CreatePrimitive(ttInt, false);
  12845. }
  12846. // Do the actual conversion
  12847. int l = int(reservedVariables.GetLength());
  12848. rctx->bc.GetVarsUsed(reservedVariables);
  12849. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  12850. reservedVariables.SetLength(l);
  12851. // Verify that the conversion was successful
  12852. if( lctx->type.dataType != to )
  12853. {
  12854. asCString str;
  12855. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  12856. Error(str, node);
  12857. }
  12858. // Right operand must be 32bit uint
  12859. l = int(reservedVariables.GetLength());
  12860. lctx->bc.GetVarsUsed(reservedVariables);
  12861. ImplicitConversion(rctx, asCDataType::CreatePrimitive(ttUInt, true), node, asIC_IMPLICIT_CONV, true);
  12862. reservedVariables.SetLength(l);
  12863. if( !rctx->type.dataType.IsUnsignedType() )
  12864. {
  12865. asCString str;
  12866. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), "uint");
  12867. Error(str, node);
  12868. }
  12869. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  12870. if( !isConstant )
  12871. {
  12872. ConvertToVariableNotIn(lctx, rctx);
  12873. ConvertToVariableNotIn(rctx, lctx);
  12874. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  12875. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  12876. if( op == ttShiftLeftAssign || op == ttShiftRightLAssign || op == ttShiftRightAAssign )
  12877. {
  12878. // Compound assignments execute the right hand value first
  12879. MergeExprBytecode(ctx, rctx);
  12880. MergeExprBytecode(ctx, lctx);
  12881. }
  12882. else
  12883. {
  12884. MergeExprBytecode(ctx, lctx);
  12885. MergeExprBytecode(ctx, rctx);
  12886. }
  12887. ProcessDeferredParams(ctx);
  12888. asEBCInstr instruction = asBC_BSLL;
  12889. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12890. {
  12891. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  12892. instruction = asBC_BSLL;
  12893. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  12894. instruction = asBC_BSRL;
  12895. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  12896. instruction = asBC_BSRA;
  12897. }
  12898. else
  12899. {
  12900. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  12901. instruction = asBC_BSLL64;
  12902. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  12903. instruction = asBC_BSRL64;
  12904. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  12905. instruction = asBC_BSRA64;
  12906. }
  12907. // Do the operation
  12908. int a = AllocateVariable(lctx->type.dataType, true);
  12909. int b = lctx->type.stackOffset;
  12910. int c = rctx->type.stackOffset;
  12911. ctx->bc.InstrW_W_W(instruction, a, b, c);
  12912. ctx->type.SetVariable(lctx->type.dataType, a, true);
  12913. }
  12914. else
  12915. {
  12916. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12917. {
  12918. asDWORD v = 0;
  12919. if( op == ttBitShiftLeft )
  12920. v = lctx->type.GetConstantDW() << rctx->type.GetConstantDW();
  12921. else if( op == ttBitShiftRight )
  12922. v = lctx->type.GetConstantDW() >> rctx->type.GetConstantDW();
  12923. else if( op == ttBitShiftRightArith )
  12924. v = int(lctx->type.GetConstantDW()) >> rctx->type.GetConstantDW();
  12925. ctx->type.SetConstantDW(lctx->type.dataType, v);
  12926. }
  12927. else
  12928. {
  12929. asQWORD v = 0;
  12930. if( op == ttBitShiftLeft )
  12931. v = lctx->type.GetConstantQW() << rctx->type.GetConstantDW();
  12932. else if( op == ttBitShiftRight )
  12933. v = lctx->type.GetConstantQW() >> rctx->type.GetConstantDW();
  12934. else if( op == ttBitShiftRightArith )
  12935. v = asINT64(lctx->type.GetConstantQW()) >> rctx->type.GetConstantDW();
  12936. ctx->type.SetConstantQW(lctx->type.dataType, v);
  12937. }
  12938. }
  12939. }
  12940. }
  12941. void asCCompiler::CompileComparisonOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  12942. {
  12943. // Both operands must be of the same type
  12944. // If either operand is a non-primitive then first convert them to the best number type
  12945. if( !lctx->type.dataType.IsPrimitive() )
  12946. {
  12947. int l = int(reservedVariables.GetLength());
  12948. rctx->bc.GetVarsUsed(reservedVariables);
  12949. ImplicitConvObjectToBestMathType(lctx, node);
  12950. reservedVariables.SetLength(l);
  12951. }
  12952. if( !rctx->type.dataType.IsPrimitive() )
  12953. {
  12954. int l = int(reservedVariables.GetLength());
  12955. lctx->bc.GetVarsUsed(reservedVariables);
  12956. ImplicitConvObjectToBestMathType(rctx, node);
  12957. reservedVariables.SetLength(l);
  12958. }
  12959. // Implicitly convert the operands to matching types
  12960. asCDataType to;
  12961. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  12962. to.SetTokenType(ttDouble);
  12963. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  12964. to.SetTokenType(ttFloat);
  12965. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12966. {
  12967. // Convert to int64 if both are signed or if one is non-constant and signed
  12968. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  12969. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  12970. to.SetTokenType(ttInt64);
  12971. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  12972. to.SetTokenType(ttUInt64);
  12973. else
  12974. to.SetTokenType(ttInt64);
  12975. }
  12976. else
  12977. {
  12978. // Convert to int32 if both are signed or if one is non-constant and signed
  12979. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  12980. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  12981. to.SetTokenType(ttInt);
  12982. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  12983. to.SetTokenType(ttUInt);
  12984. else if( lctx->type.dataType.IsBooleanType() || rctx->type.dataType.IsBooleanType() )
  12985. to.SetTokenType(ttBool);
  12986. else
  12987. to.SetTokenType(ttInt);
  12988. }
  12989. // If doing an operation with double constant and float variable, the constant should be converted to float
  12990. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  12991. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  12992. to.SetTokenType(ttFloat);
  12993. asASSERT( to.GetTokenType() != ttUnrecognizedToken );
  12994. // Do we have a mismatch between the sign of the operand?
  12995. bool signMismatch = false;
  12996. for( int n = 0; !signMismatch && n < 2; n++ )
  12997. {
  12998. asCExprContext *opCtx = n ? rctx : lctx;
  12999. if( opCtx->type.dataType.IsUnsignedType() != to.IsUnsignedType() )
  13000. {
  13001. // We have a mismatch, unless the value is a literal constant and the conversion won't affect its value
  13002. signMismatch = true;
  13003. if( opCtx->type.isConstant )
  13004. {
  13005. if( opCtx->type.dataType.GetTokenType() == ttUInt64 || opCtx->type.dataType.GetTokenType() == ttInt64 )
  13006. {
  13007. if( !(opCtx->type.GetConstantQW() & (asQWORD(1)<<63)) )
  13008. signMismatch = false;
  13009. }
  13010. else if(opCtx->type.dataType.GetTokenType() == ttUInt || opCtx->type.dataType.GetTokenType() == ttInt || opCtx->type.dataType.IsEnumType() )
  13011. {
  13012. if( !(opCtx->type.GetConstantDW() & (1<<31)) )
  13013. signMismatch = false;
  13014. }
  13015. else if (opCtx->type.dataType.GetTokenType() == ttUInt16 || opCtx->type.dataType.GetTokenType() == ttInt16)
  13016. {
  13017. if (!(opCtx->type.GetConstantW() & (1 << 15)))
  13018. signMismatch = false;
  13019. }
  13020. else if (opCtx->type.dataType.GetTokenType() == ttUInt8 || opCtx->type.dataType.GetTokenType() == ttInt8)
  13021. {
  13022. if (!(opCtx->type.GetConstantB() & (1 << 7)))
  13023. signMismatch = false;
  13024. }
  13025. // It's not necessary to check for floats or double, because if
  13026. // it was then the types for the conversion will never be unsigned
  13027. }
  13028. }
  13029. }
  13030. // Check for signed/unsigned mismatch
  13031. if( signMismatch )
  13032. Warning(TXT_SIGNED_UNSIGNED_MISMATCH, node);
  13033. // Attempt to resolve ambiguous enumerations
  13034. if( lctx->type.dataType.IsEnumType() && rctx->enumValue != "" )
  13035. ImplicitConversion(rctx, lctx->type.dataType, node, asIC_IMPLICIT_CONV);
  13036. else if( rctx->type.dataType.IsEnumType() && lctx->enumValue != "" )
  13037. ImplicitConversion(lctx, rctx->type.dataType, node, asIC_IMPLICIT_CONV);
  13038. // Do the actual conversion
  13039. int l = int(reservedVariables.GetLength());
  13040. rctx->bc.GetVarsUsed(reservedVariables);
  13041. if( lctx->type.dataType.IsReference() )
  13042. ConvertToVariable(lctx);
  13043. if( rctx->type.dataType.IsReference() )
  13044. ConvertToVariable(rctx);
  13045. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  13046. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  13047. reservedVariables.SetLength(l);
  13048. // Verify that the conversion was successful
  13049. bool ok = true;
  13050. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  13051. {
  13052. asCString str;
  13053. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  13054. Error(str, node);
  13055. ok = false;
  13056. }
  13057. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  13058. {
  13059. asCString str;
  13060. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  13061. Error(str, node);
  13062. ok = false;
  13063. }
  13064. if( !ok )
  13065. {
  13066. // It wasn't possible to get two valid operands, so we just return
  13067. // a boolean result and let the compiler continue.
  13068. #if AS_SIZEOF_BOOL == 1
  13069. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  13070. #else
  13071. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  13072. #endif
  13073. return;
  13074. }
  13075. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  13076. if( op == ttUnrecognizedToken )
  13077. op = node->tokenType;
  13078. if( !isConstant )
  13079. {
  13080. if( to.IsBooleanType() )
  13081. {
  13082. if( op == ttEqual || op == ttNotEqual )
  13083. {
  13084. // Must convert to temporary variable, because we are changing the value before comparison
  13085. ConvertToTempVariableNotIn(lctx, rctx);
  13086. ConvertToTempVariableNotIn(rctx, lctx);
  13087. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  13088. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  13089. // Make sure they are equal if not false
  13090. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  13091. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  13092. MergeExprBytecode(ctx, lctx);
  13093. MergeExprBytecode(ctx, rctx);
  13094. ProcessDeferredParams(ctx);
  13095. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  13096. int b = lctx->type.stackOffset;
  13097. int c = rctx->type.stackOffset;
  13098. if( op == ttEqual )
  13099. {
  13100. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  13101. ctx->bc.Instr(asBC_TZ);
  13102. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  13103. }
  13104. else if( op == ttNotEqual )
  13105. {
  13106. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  13107. ctx->bc.Instr(asBC_TNZ);
  13108. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  13109. }
  13110. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  13111. }
  13112. else
  13113. {
  13114. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  13115. Error(TXT_ILLEGAL_OPERATION, node);
  13116. #if AS_SIZEOF_BOOL == 1
  13117. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), 0);
  13118. #else
  13119. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 0);
  13120. #endif
  13121. }
  13122. }
  13123. else
  13124. {
  13125. ConvertToVariableNotIn(lctx, rctx);
  13126. ConvertToVariableNotIn(rctx, lctx);
  13127. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  13128. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  13129. MergeExprBytecode(ctx, lctx);
  13130. MergeExprBytecode(ctx, rctx);
  13131. ProcessDeferredParams(ctx);
  13132. asEBCInstr iCmp = asBC_CMPi, iT = asBC_TZ;
  13133. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  13134. iCmp = asBC_CMPi;
  13135. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  13136. iCmp = asBC_CMPu;
  13137. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  13138. iCmp = asBC_CMPi64;
  13139. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  13140. iCmp = asBC_CMPu64;
  13141. else if( lctx->type.dataType.IsFloatType() )
  13142. iCmp = asBC_CMPf;
  13143. else if( lctx->type.dataType.IsDoubleType() )
  13144. iCmp = asBC_CMPd;
  13145. else
  13146. asASSERT(false);
  13147. if( op == ttEqual )
  13148. iT = asBC_TZ;
  13149. else if( op == ttNotEqual )
  13150. iT = asBC_TNZ;
  13151. else if( op == ttLessThan )
  13152. iT = asBC_TS;
  13153. else if( op == ttLessThanOrEqual )
  13154. iT = asBC_TNP;
  13155. else if( op == ttGreaterThan )
  13156. iT = asBC_TP;
  13157. else if( op == ttGreaterThanOrEqual )
  13158. iT = asBC_TNS;
  13159. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  13160. int b = lctx->type.stackOffset;
  13161. int c = rctx->type.stackOffset;
  13162. ctx->bc.InstrW_W(iCmp, b, c);
  13163. ctx->bc.Instr(iT);
  13164. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  13165. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  13166. }
  13167. }
  13168. else
  13169. {
  13170. if( to.IsBooleanType() )
  13171. {
  13172. if( op == ttEqual || op == ttNotEqual )
  13173. {
  13174. asDWORD lv, rv;
  13175. #if AS_SIZEOF_BOOL == 1
  13176. lv = lctx->type.GetConstantB();
  13177. rv = rctx->type.GetConstantB();
  13178. #else
  13179. lv = lctx->type.GetConstantDW();
  13180. rv = rctx->type.GetConstantDW();
  13181. #endif
  13182. // Make sure they are equal if not false
  13183. if (lv != 0) lv = VALUE_OF_BOOLEAN_TRUE;
  13184. if (rv != 0) rv = VALUE_OF_BOOLEAN_TRUE;
  13185. asDWORD v = 0;
  13186. if (op == ttEqual)
  13187. v = (lv == rv) ? VALUE_OF_BOOLEAN_TRUE : 0;
  13188. else if (op == ttNotEqual)
  13189. v = (lv != rv) ? VALUE_OF_BOOLEAN_TRUE : 0;
  13190. #if AS_SIZEOF_BOOL == 1
  13191. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), (asBYTE)v);
  13192. #else
  13193. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), v);
  13194. #endif
  13195. }
  13196. else
  13197. {
  13198. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  13199. Error(TXT_ILLEGAL_OPERATION, node);
  13200. }
  13201. }
  13202. else
  13203. {
  13204. int i = 0;
  13205. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  13206. {
  13207. int v = int(lctx->type.GetConstantDW()) - int(rctx->type.GetConstantDW());
  13208. if( v < 0 ) i = -1;
  13209. if( v > 0 ) i = 1;
  13210. }
  13211. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  13212. {
  13213. asDWORD v1 = lctx->type.GetConstantDW();
  13214. asDWORD v2 = rctx->type.GetConstantDW();
  13215. if( v1 < v2 ) i = -1;
  13216. if( v1 > v2 ) i = 1;
  13217. }
  13218. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  13219. {
  13220. asINT64 v = asINT64(lctx->type.GetConstantQW()) - asINT64(rctx->type.GetConstantQW());
  13221. if( v < 0 ) i = -1;
  13222. if( v > 0 ) i = 1;
  13223. }
  13224. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  13225. {
  13226. asQWORD v1 = lctx->type.GetConstantQW();
  13227. asQWORD v2 = rctx->type.GetConstantQW();
  13228. if( v1 < v2 ) i = -1;
  13229. if( v1 > v2 ) i = 1;
  13230. }
  13231. else if( lctx->type.dataType.IsFloatType() )
  13232. {
  13233. float v = lctx->type.GetConstantF() - rctx->type.GetConstantF();
  13234. if( v < 0 ) i = -1;
  13235. if( v > 0 ) i = 1;
  13236. }
  13237. else if( lctx->type.dataType.IsDoubleType() )
  13238. {
  13239. double v = lctx->type.GetConstantD() - rctx->type.GetConstantD();
  13240. if( v < 0 ) i = -1;
  13241. if( v > 0 ) i = 1;
  13242. }
  13243. if( op == ttEqual )
  13244. i = (i == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  13245. else if( op == ttNotEqual )
  13246. i = (i != 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  13247. else if( op == ttLessThan )
  13248. i = (i < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  13249. else if( op == ttLessThanOrEqual )
  13250. i = (i <= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  13251. else if( op == ttGreaterThan )
  13252. i = (i > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  13253. else if( op == ttGreaterThanOrEqual )
  13254. i = (i >= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  13255. #if AS_SIZEOF_BOOL == 1
  13256. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), (asBYTE)i);
  13257. #else
  13258. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), i);
  13259. #endif
  13260. }
  13261. }
  13262. }
  13263. void asCCompiler::PushVariableOnStack(asCExprContext *ctx, bool asReference)
  13264. {
  13265. // Put the result on the stack
  13266. if( asReference )
  13267. {
  13268. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  13269. ctx->type.dataType.MakeReference(true);
  13270. }
  13271. else
  13272. {
  13273. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  13274. ctx->bc.InstrSHORT(asBC_PshV4, ctx->type.stackOffset);
  13275. else
  13276. ctx->bc.InstrSHORT(asBC_PshV8, ctx->type.stackOffset);
  13277. }
  13278. }
  13279. void asCCompiler::CompileBooleanOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  13280. {
  13281. // Both operands must be booleans
  13282. asCDataType to;
  13283. to.SetTokenType(ttBool);
  13284. // Do the actual conversion
  13285. int l = int(reservedVariables.GetLength());
  13286. rctx->bc.GetVarsUsed(reservedVariables);
  13287. lctx->bc.GetVarsUsed(reservedVariables);
  13288. // Allow value types to be converted to bool using 'bool opImplConv()'
  13289. if( lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  13290. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  13291. if( rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  13292. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  13293. reservedVariables.SetLength(l);
  13294. // Verify that the conversion was successful
  13295. if( !lctx->type.dataType.IsBooleanType() )
  13296. {
  13297. asCString str;
  13298. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), "bool");
  13299. Error(str, node);
  13300. // Force the conversion to allow compilation to proceed
  13301. lctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  13302. }
  13303. if( !rctx->type.dataType.IsBooleanType() )
  13304. {
  13305. asCString str;
  13306. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), "bool");
  13307. Error(str, node);
  13308. // Force the conversion to allow compilation to proceed
  13309. rctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  13310. }
  13311. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  13312. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  13313. // What kind of operator is it?
  13314. if( op == ttUnrecognizedToken )
  13315. op = node->tokenType;
  13316. if( op == ttXor )
  13317. {
  13318. if( !isConstant )
  13319. {
  13320. // Must convert to temporary variable, because we are changing the value before comparison
  13321. ConvertToTempVariableNotIn(lctx, rctx);
  13322. ConvertToTempVariableNotIn(rctx, lctx);
  13323. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  13324. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  13325. // Make sure they are equal if not false
  13326. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  13327. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  13328. MergeExprBytecode(ctx, lctx);
  13329. MergeExprBytecode(ctx, rctx);
  13330. ProcessDeferredParams(ctx);
  13331. int a = AllocateVariable(ctx->type.dataType, true);
  13332. int b = lctx->type.stackOffset;
  13333. int c = rctx->type.stackOffset;
  13334. ctx->bc.InstrW_W_W(asBC_BXOR,a,b,c);
  13335. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  13336. }
  13337. else
  13338. {
  13339. // Make sure they are equal if not false
  13340. #if AS_SIZEOF_BOOL == 1
  13341. if( lctx->type.GetConstantB() != 0 ) lctx->type.SetConstantB(VALUE_OF_BOOLEAN_TRUE);
  13342. if( rctx->type.GetConstantB() != 0 ) rctx->type.SetConstantB(VALUE_OF_BOOLEAN_TRUE);
  13343. asBYTE v = 0;
  13344. v = lctx->type.GetConstantB() - rctx->type.GetConstantB();
  13345. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  13346. ctx->type.isConstant = true;
  13347. ctx->type.SetConstantB(v);
  13348. #else
  13349. if( lctx->type.GetConstantDW() != 0 ) lctx->type.SetConstantDW(VALUE_OF_BOOLEAN_TRUE);
  13350. if( rctx->type.GetConstantDW() != 0 ) rctx->type.SetConstantDW(VALUE_OF_BOOLEAN_TRUE);
  13351. asDWORD v = 0;
  13352. v = lctx->type.GetConstantDW() - rctx->type.GetConstantDW();
  13353. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  13354. ctx->type.isConstant = true;
  13355. ctx->type.SetConstantDW(v);
  13356. #endif
  13357. }
  13358. }
  13359. else if( op == ttAnd ||
  13360. op == ttOr )
  13361. {
  13362. if( !isConstant )
  13363. {
  13364. // If or-operator and first value is 1 the second value shouldn't be calculated
  13365. // if and-operator and first value is 0 the second value shouldn't be calculated
  13366. ConvertToVariable(lctx);
  13367. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  13368. MergeExprBytecode(ctx, lctx);
  13369. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  13370. int label1 = nextLabel++;
  13371. int label2 = nextLabel++;
  13372. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  13373. ctx->bc.Instr(asBC_ClrHi);
  13374. if( op == ttAnd )
  13375. {
  13376. ctx->bc.InstrDWORD(asBC_JNZ, label1);
  13377. ctx->bc.InstrW_DW(asBC_SetV4, (asWORD)offset, 0);
  13378. ctx->bc.InstrINT(asBC_JMP, label2);
  13379. }
  13380. else if( op == ttOr )
  13381. {
  13382. ctx->bc.InstrDWORD(asBC_JZ, label1);
  13383. #if AS_SIZEOF_BOOL == 1
  13384. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  13385. #else
  13386. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  13387. #endif
  13388. ctx->bc.InstrINT(asBC_JMP, label2);
  13389. }
  13390. ctx->bc.Label((short)label1);
  13391. ConvertToVariable(rctx);
  13392. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  13393. rctx->bc.InstrW_W(asBC_CpyVtoV4, offset, rctx->type.stackOffset);
  13394. MergeExprBytecode(ctx, rctx);
  13395. ctx->bc.Label((short)label2);
  13396. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), offset, true);
  13397. }
  13398. else
  13399. {
  13400. #if AS_SIZEOF_BOOL == 1
  13401. asBYTE v = 0;
  13402. if( op == ttAnd )
  13403. v = lctx->type.GetConstantB() && rctx->type.GetConstantB();
  13404. else if( op == ttOr )
  13405. v = lctx->type.GetConstantB() || rctx->type.GetConstantB();
  13406. // Remember the result
  13407. ctx->type.isConstant = true;
  13408. ctx->type.SetConstantB(v);
  13409. #else
  13410. asDWORD v = 0;
  13411. if( op == ttAnd )
  13412. v = lctx->type.GetConstantDW() && rctx->type.GetConstantDW();
  13413. else if( op == ttOr )
  13414. v = lctx->type.GetConstantDW() || rctx->type.GetConstantDW();
  13415. // Remember the result
  13416. ctx->type.isConstant = true;
  13417. ctx->type.SetConstantDW(v);
  13418. #endif
  13419. }
  13420. }
  13421. }
  13422. void asCCompiler::CompileOperatorOnHandles(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType opToken)
  13423. {
  13424. // Process the property accessor as get
  13425. if( ProcessPropertyGetAccessor(lctx, node) < 0 )
  13426. return;
  13427. if( ProcessPropertyGetAccessor(rctx, node) < 0 )
  13428. return;
  13429. DetermineSingleFunc(lctx, node);
  13430. DetermineSingleFunc(rctx, node);
  13431. // Make sure lctx doesn't end up with a variable used in rctx
  13432. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  13433. {
  13434. asCArray<int> vars;
  13435. rctx->bc.GetVarsUsed(vars);
  13436. int offset = AllocateVariable(lctx->type.dataType, true);
  13437. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  13438. ReleaseTemporaryVariable(offset, 0);
  13439. }
  13440. if( opToken == ttUnrecognizedToken )
  13441. opToken = node->tokenType;
  13442. // Warn if not both operands are explicit handles or null handles
  13443. if( (opToken == ttEqual || opToken == ttNotEqual) &&
  13444. ((!(lctx->type.isExplicitHandle || lctx->type.IsNullConstant()) && !(lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE))) ||
  13445. (!(rctx->type.isExplicitHandle || rctx->type.IsNullConstant()) && !(rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE)))) )
  13446. {
  13447. Warning(TXT_HANDLE_COMPARISON, node);
  13448. }
  13449. // If one of the operands is a value type used as handle, we should look for the opEquals method
  13450. if( ((lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE)) ||
  13451. (rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE))) &&
  13452. (opToken == ttEqual || opToken == ttIs ||
  13453. opToken == ttNotEqual || opToken == ttNotIs) )
  13454. {
  13455. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  13456. // Find the matching opEquals method
  13457. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, true, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  13458. if( r == 0 )
  13459. {
  13460. // Try again by switching the order of the operands
  13461. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, false, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  13462. }
  13463. if( r == 1 )
  13464. {
  13465. if( opToken == ttNotEqual || opToken == ttNotIs )
  13466. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  13467. // Success, don't continue
  13468. return;
  13469. }
  13470. else if( r == 0 )
  13471. {
  13472. // Couldn't find opEquals method
  13473. Error(TXT_NO_APPROPRIATE_OPEQUALS, node);
  13474. }
  13475. // Compiler error, don't continue
  13476. #if AS_SIZEOF_BOOL == 1
  13477. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  13478. #else
  13479. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  13480. #endif
  13481. return;
  13482. }
  13483. // Implicitly convert null to the other type
  13484. asCDataType to;
  13485. if( lctx->type.IsNullConstant() )
  13486. to = rctx->type.dataType;
  13487. else if( rctx->type.IsNullConstant() )
  13488. to = lctx->type.dataType;
  13489. else
  13490. {
  13491. // Find a common base type
  13492. asCExprContext tmp(engine);
  13493. tmp.type = rctx->type;
  13494. ImplicitConversion(&tmp, lctx->type.dataType, 0, asIC_IMPLICIT_CONV, false);
  13495. if( tmp.type.dataType.GetTypeInfo() == lctx->type.dataType.GetTypeInfo() )
  13496. to = lctx->type.dataType;
  13497. else
  13498. to = rctx->type.dataType;
  13499. // Assume handle-to-const as it is not possible to convert handle-to-const to handle-to-non-const
  13500. to.MakeHandleToConst(true);
  13501. }
  13502. // Need to pop the value if it is a null constant
  13503. if( lctx->type.IsNullConstant() )
  13504. lctx->bc.Instr(asBC_PopPtr);
  13505. if( rctx->type.IsNullConstant() )
  13506. rctx->bc.Instr(asBC_PopPtr);
  13507. // Convert both sides to explicit handles
  13508. to.MakeHandle(true);
  13509. to.MakeReference(false);
  13510. if( !to.IsObjectHandle() )
  13511. {
  13512. // Compiler error, don't continue
  13513. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  13514. #if AS_SIZEOF_BOOL == 1
  13515. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  13516. #else
  13517. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  13518. #endif
  13519. return;
  13520. }
  13521. // Do the conversion
  13522. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  13523. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  13524. // Both operands must be of the same type
  13525. // Verify that the conversion was successful
  13526. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  13527. {
  13528. asCString str;
  13529. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  13530. Error(str, node);
  13531. }
  13532. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  13533. {
  13534. asCString str;
  13535. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  13536. Error(str, node);
  13537. }
  13538. // Make sure it really is handles that are being compared
  13539. if( !lctx->type.dataType.IsObjectHandle() )
  13540. {
  13541. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  13542. }
  13543. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  13544. if( opToken == ttEqual || opToken == ttNotEqual || opToken == ttIs || opToken == ttNotIs )
  13545. {
  13546. // Make sure handles received as parameters by reference are copied to a local variable before the
  13547. // asBC_CmpPtr, so we don't end up comparing the reference to the handle instead of the handle itself
  13548. if( lctx->type.isVariable && !lctx->type.isTemporary && lctx->type.stackOffset <= 0 )
  13549. lctx->type.isVariable = false;
  13550. if( rctx->type.isVariable && !rctx->type.isTemporary && rctx->type.stackOffset <= 0 )
  13551. rctx->type.isVariable = false;
  13552. // TODO: runtime optimize: don't do REFCPY if not necessary
  13553. ConvertToVariableNotIn(lctx, rctx);
  13554. ConvertToVariable(rctx);
  13555. // Pop the pointers from the stack as they will not be used
  13556. lctx->bc.Instr(asBC_PopPtr);
  13557. rctx->bc.Instr(asBC_PopPtr);
  13558. MergeExprBytecode(ctx, lctx);
  13559. MergeExprBytecode(ctx, rctx);
  13560. int a = AllocateVariable(ctx->type.dataType, true);
  13561. int b = lctx->type.stackOffset;
  13562. int c = rctx->type.stackOffset;
  13563. ctx->bc.InstrW_W(asBC_CmpPtr, b, c);
  13564. if( opToken == ttEqual || opToken == ttIs )
  13565. ctx->bc.Instr(asBC_TZ);
  13566. else if( opToken == ttNotEqual || opToken == ttNotIs )
  13567. ctx->bc.Instr(asBC_TNZ);
  13568. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  13569. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  13570. ReleaseTemporaryVariable(lctx->type, &ctx->bc);
  13571. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  13572. ProcessDeferredParams(ctx);
  13573. }
  13574. else
  13575. {
  13576. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  13577. Error(TXT_ILLEGAL_OPERATION, node);
  13578. }
  13579. }
  13580. void asCCompiler::PerformFunctionCall(int funcId, asCExprContext *ctx, bool isConstructor, asCArray<asCExprContext*> *args, asCObjectType *objType, bool useVariable, int varOffset, int funcPtrVar)
  13581. {
  13582. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  13583. // A shared object may not call non-shared functions
  13584. if( outFunc->IsShared() && !descr->IsShared() )
  13585. {
  13586. asCString msg;
  13587. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, descr->GetDeclarationStr().AddressOf());
  13588. Error(msg, ctx->exprNode);
  13589. }
  13590. // Check if the function is private or protected
  13591. if (descr->IsPrivate())
  13592. {
  13593. asCObjectType *type = descr->objectType;
  13594. if (type == 0 && descr->traits.GetTrait(asTRAIT_CONSTRUCTOR))
  13595. type = CastToObjectType(descr->returnType.GetTypeInfo());
  13596. asASSERT(type);
  13597. if( (type != outFunc->GetObjectType()) )
  13598. {
  13599. asCString msg;
  13600. msg.Format(TXT_PRIVATE_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  13601. Error(msg, ctx->exprNode);
  13602. }
  13603. }
  13604. else if (descr->IsProtected())
  13605. {
  13606. asCObjectType *type = descr->objectType;
  13607. if (type == 0 && descr->traits.GetTrait(asTRAIT_CONSTRUCTOR))
  13608. type = CastToObjectType(descr->returnType.GetTypeInfo());
  13609. asASSERT(type);
  13610. if (!(type == outFunc->objectType || (outFunc->objectType && outFunc->objectType->DerivesFrom(type))))
  13611. {
  13612. asCString msg;
  13613. msg.Format(TXT_PROTECTED_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  13614. Error(msg, ctx->exprNode);
  13615. }
  13616. }
  13617. int argSize = descr->GetSpaceNeededForArguments();
  13618. // If we're calling a class method we must make sure the object is guaranteed to stay
  13619. // alive throughout the call by holding on to a reference in a local variable. This must
  13620. // be done for any methods that return references, and any calls on script objects.
  13621. // Application registered objects are assumed to know to keep themselves alive even
  13622. // if the method doesn't return a reference.
  13623. if( !ctx->type.isRefSafe &&
  13624. descr->objectType &&
  13625. (ctx->type.dataType.IsObjectHandle() || ctx->type.dataType.SupportHandles()) &&
  13626. (descr->returnType.IsReference() || (ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_SCRIPT_OBJECT)) &&
  13627. !(ctx->type.isVariable || ctx->type.isTemporary) &&
  13628. !(ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_SCOPED) &&
  13629. !(ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_ASHANDLE) )
  13630. {
  13631. // TODO: runtime optimize: Avoid this for global variables, by storing a reference to the global variable once in a
  13632. // local variable and then refer to the same for each call. An alias for the global variable
  13633. // should be stored in the variable scope so that the compiler can find it. For loops and
  13634. // scopes that will always be executed, i.e. non-if scopes the alias should be stored in the
  13635. // higher scope to increase the probability of re-use.
  13636. int tempRef = AllocateVariable(ctx->type.dataType, true);
  13637. ctx->bc.InstrSHORT(asBC_PSF, (short)tempRef);
  13638. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  13639. // Add the release of this reference as a deferred expression
  13640. asSDeferredParam deferred;
  13641. deferred.origExpr = 0;
  13642. deferred.argInOutFlags = asTM_INREF;
  13643. deferred.argNode = 0;
  13644. deferred.argType.SetVariable(ctx->type.dataType, tempRef, true);
  13645. ctx->deferredParams.PushLast(deferred);
  13646. // Forget the current type
  13647. ctx->type.SetDummy();
  13648. }
  13649. // Check if there is a need to add a hidden pointer for when the function returns an object by value
  13650. if( descr->DoesReturnOnStack() && !useVariable )
  13651. {
  13652. useVariable = true;
  13653. varOffset = AllocateVariable(descr->returnType, true);
  13654. // Push the pointer to the pre-allocated space for the return value
  13655. ctx->bc.InstrSHORT(asBC_PSF, short(varOffset));
  13656. if( descr->objectType )
  13657. {
  13658. // The object pointer is already on the stack, but should be the top
  13659. // one, so we need to swap the pointers in order to get the correct
  13660. ctx->bc.Instr(asBC_SwapPtr);
  13661. }
  13662. }
  13663. if( isConstructor )
  13664. {
  13665. // Sometimes the value types are allocated on the heap,
  13666. // which is when this way of constructing them is used.
  13667. asASSERT(useVariable == false);
  13668. if( (objType->flags & asOBJ_TEMPLATE) )
  13669. {
  13670. asASSERT( descr->funcType == asFUNC_SCRIPT );
  13671. // Find the id of the real constructor and not the generated stub
  13672. asUINT id = 0;
  13673. asDWORD *bc = descr->scriptData->byteCode.AddressOf();
  13674. while( bc )
  13675. {
  13676. if( (*(asBYTE*)bc) == asBC_CALLSYS )
  13677. {
  13678. id = asBC_INTARG(bc);
  13679. break;
  13680. }
  13681. bc += asBCTypeSize[asBCInfo[*(asBYTE*)bc].type];
  13682. }
  13683. asASSERT( id );
  13684. ctx->bc.InstrPTR(asBC_OBJTYPE, objType);
  13685. ctx->bc.Alloc(asBC_ALLOC, objType, id, argSize + AS_PTR_SIZE + AS_PTR_SIZE);
  13686. }
  13687. else
  13688. ctx->bc.Alloc(asBC_ALLOC, objType, descr->id, argSize+AS_PTR_SIZE);
  13689. // The instruction has already moved the returned object to the variable
  13690. ctx->type.Set(asCDataType::CreatePrimitive(ttVoid, false));
  13691. ctx->type.isLValue = false;
  13692. // Clean up arguments
  13693. if( args )
  13694. AfterFunctionCall(funcId, *args, ctx, false);
  13695. ProcessDeferredParams(ctx);
  13696. return;
  13697. }
  13698. else
  13699. {
  13700. if( descr->objectType )
  13701. argSize += AS_PTR_SIZE;
  13702. // If the function returns an object by value the address of the location
  13703. // where the value should be stored is passed as an argument too
  13704. if( descr->DoesReturnOnStack() )
  13705. argSize += AS_PTR_SIZE;
  13706. // TODO: runtime optimize: If it is known that a class method cannot be overridden the call
  13707. // should be made with asBC_CALL as it is faster. Examples where this
  13708. // is known is for example finalled methods where the class doesn't derive
  13709. // from any other, or even non-finalled methods but where it is known
  13710. // at compile time the true type of the object. The first should be
  13711. // quite easy to determine, but the latter will be quite complex and possibly
  13712. // not worth it.
  13713. if( descr->funcType == asFUNC_IMPORTED )
  13714. ctx->bc.Call(asBC_CALLBND , descr->id, argSize);
  13715. // TODO: Maybe we need two different byte codes
  13716. else if( descr->funcType == asFUNC_INTERFACE || descr->funcType == asFUNC_VIRTUAL )
  13717. ctx->bc.Call(asBC_CALLINTF, descr->id, argSize);
  13718. else if( descr->funcType == asFUNC_SCRIPT )
  13719. ctx->bc.Call(asBC_CALL , descr->id, argSize);
  13720. else if( descr->funcType == asFUNC_SYSTEM )
  13721. {
  13722. // Check if we can use the faster asBC_Thiscall1 instruction, i.e. one of
  13723. // type &obj::func(int)
  13724. // type &obj::func(uint)
  13725. if( descr->GetObjectType() && descr->returnType.IsReference() &&
  13726. descr->parameterTypes.GetLength() == 1 &&
  13727. (descr->parameterTypes[0].IsIntegerType() || descr->parameterTypes[0].IsUnsignedType()) &&
  13728. descr->parameterTypes[0].GetSizeInMemoryBytes() == 4 &&
  13729. !descr->parameterTypes[0].IsReference() )
  13730. ctx->bc.Call(asBC_Thiscall1, descr->id, argSize);
  13731. else
  13732. ctx->bc.Call(asBC_CALLSYS , descr->id, argSize);
  13733. }
  13734. else if( descr->funcType == asFUNC_FUNCDEF )
  13735. ctx->bc.CallPtr(asBC_CallPtr, funcPtrVar, argSize);
  13736. }
  13737. if( (descr->returnType.IsObject() || descr->returnType.IsFuncdef()) && !descr->returnType.IsReference() )
  13738. {
  13739. int returnOffset = 0;
  13740. asCExprValue tmpExpr = ctx->type;
  13741. if( descr->DoesReturnOnStack() )
  13742. {
  13743. asASSERT( useVariable );
  13744. // The variable was allocated before the function was called
  13745. returnOffset = varOffset;
  13746. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  13747. // The variable was initialized by the function, so we need to mark it as initialized here
  13748. ctx->bc.ObjInfo(varOffset, asOBJ_INIT);
  13749. }
  13750. else
  13751. {
  13752. if( useVariable )
  13753. {
  13754. // Use the given variable
  13755. returnOffset = varOffset;
  13756. ctx->type.SetVariable(descr->returnType, returnOffset, false);
  13757. }
  13758. else
  13759. {
  13760. // Allocate a temporary variable for the returned object
  13761. // The returned object will actually be allocated on the heap, so
  13762. // we must force the allocation of the variable to do the same
  13763. returnOffset = AllocateVariable(descr->returnType, true, !descr->returnType.IsObjectHandle());
  13764. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  13765. }
  13766. // Move the pointer from the object register to the temporary variable
  13767. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  13768. }
  13769. ReleaseTemporaryVariable(tmpExpr, &ctx->bc);
  13770. ctx->type.dataType.MakeReference(IsVariableOnHeap(returnOffset));
  13771. ctx->type.isLValue = false; // It is a reference, but not an lvalue
  13772. // Clean up arguments
  13773. if( args )
  13774. AfterFunctionCall(funcId, *args, ctx, false);
  13775. ProcessDeferredParams(ctx);
  13776. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  13777. }
  13778. else if( descr->returnType.IsReference() )
  13779. {
  13780. asASSERT(useVariable == false);
  13781. // We cannot clean up the arguments yet, because the
  13782. // reference might be pointing to one of them.
  13783. if( args )
  13784. AfterFunctionCall(funcId, *args, ctx, true);
  13785. // Do not process the output parameters yet, because it
  13786. // might invalidate the returned reference
  13787. // If the context holds a variable that needs cleanup
  13788. // store it as a deferred parameter so it will be cleaned up
  13789. // afterwards.
  13790. if( ctx->type.isTemporary )
  13791. {
  13792. asSDeferredParam defer;
  13793. defer.argNode = 0;
  13794. defer.argType = ctx->type;
  13795. defer.argInOutFlags = asTM_INOUTREF;
  13796. defer.origExpr = 0;
  13797. ctx->deferredParams.PushLast(defer);
  13798. }
  13799. ctx->type.Set(descr->returnType);
  13800. if( !descr->returnType.IsPrimitive() )
  13801. {
  13802. ctx->bc.Instr(asBC_PshRPtr);
  13803. if( descr->returnType.IsObject() &&
  13804. !descr->returnType.IsObjectHandle() )
  13805. {
  13806. // We are getting the pointer to the object
  13807. // not a pointer to a object variable
  13808. ctx->type.dataType.MakeReference(false);
  13809. }
  13810. }
  13811. // A returned reference can be used as lvalue
  13812. ctx->type.isLValue = true;
  13813. }
  13814. else
  13815. {
  13816. asCExprValue tmpExpr = ctx->type;
  13817. if( descr->returnType.GetSizeInMemoryBytes() )
  13818. {
  13819. int offset;
  13820. if (useVariable)
  13821. offset = varOffset;
  13822. else
  13823. {
  13824. // Allocate a temporary variable to hold the value, but make sure
  13825. // the temporary variable isn't used in any of the deferred arguments
  13826. int l = int(reservedVariables.GetLength());
  13827. for (asUINT n = 0; args && n < args->GetLength(); n++)
  13828. {
  13829. asCExprContext *expr = (*args)[n]->origExpr;
  13830. if (expr)
  13831. expr->bc.GetVarsUsed(reservedVariables);
  13832. }
  13833. offset = AllocateVariable(descr->returnType, true);
  13834. reservedVariables.SetLength(l);
  13835. }
  13836. ctx->type.SetVariable(descr->returnType, offset, true);
  13837. // Move the value from the return register to the variable
  13838. if( descr->returnType.GetSizeOnStackDWords() == 1 )
  13839. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)offset);
  13840. else if( descr->returnType.GetSizeOnStackDWords() == 2 )
  13841. ctx->bc.InstrSHORT(asBC_CpyRtoV8, (short)offset);
  13842. }
  13843. else
  13844. ctx->type.Set(descr->returnType);
  13845. ReleaseTemporaryVariable(tmpExpr, &ctx->bc);
  13846. ctx->type.isLValue = false;
  13847. // Clean up arguments
  13848. if( args )
  13849. AfterFunctionCall(funcId, *args, ctx, false);
  13850. ProcessDeferredParams(ctx);
  13851. }
  13852. }
  13853. // This only merges the bytecode, but doesn't modify the type of the final context
  13854. void asCCompiler::MergeExprBytecode(asCExprContext *before, asCExprContext *after)
  13855. {
  13856. before->bc.AddCode(&after->bc);
  13857. for( asUINT n = 0; n < after->deferredParams.GetLength(); n++ )
  13858. {
  13859. before->deferredParams.PushLast(after->deferredParams[n]);
  13860. after->deferredParams[n].origExpr = 0;
  13861. }
  13862. after->deferredParams.SetLength(0);
  13863. }
  13864. // This merges both bytecode and the type of the final context
  13865. void asCCompiler::MergeExprBytecodeAndType(asCExprContext *before, asCExprContext *after)
  13866. {
  13867. MergeExprBytecode(before, after);
  13868. before->Merge(after);
  13869. }
  13870. void asCCompiler::FilterConst(asCArray<int> &funcs, bool removeConst)
  13871. {
  13872. if( funcs.GetLength() == 0 ) return;
  13873. // This is only done for object methods
  13874. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[0]);
  13875. if( !desc || desc->objectType == 0 ) return;
  13876. // Check if there are any non-const matches
  13877. asUINT n;
  13878. bool foundNonConst = false;
  13879. for( n = 0; n < funcs.GetLength(); n++ )
  13880. {
  13881. desc = builder->GetFunctionDescription(funcs[n]);
  13882. if( desc && desc->IsReadOnly() != removeConst )
  13883. {
  13884. foundNonConst = true;
  13885. break;
  13886. }
  13887. }
  13888. if( foundNonConst )
  13889. {
  13890. // Remove all const methods
  13891. for( n = 0; n < funcs.GetLength(); n++ )
  13892. {
  13893. desc = builder->GetFunctionDescription(funcs[n]);
  13894. if( desc && desc->IsReadOnly() == removeConst )
  13895. {
  13896. if( n == funcs.GetLength() - 1 )
  13897. funcs.PopLast();
  13898. else
  13899. funcs[n] = funcs.PopLast();
  13900. n--;
  13901. }
  13902. }
  13903. }
  13904. }
  13905. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13906. asCExprValue::asCExprValue()
  13907. {
  13908. isTemporary = false;
  13909. stackOffset = 0;
  13910. isConstant = false;
  13911. isVariable = false;
  13912. isExplicitHandle = false;
  13913. qwordValue = 0;
  13914. isLValue = false;
  13915. isRefToLocal = false;
  13916. isRefSafe = false;
  13917. }
  13918. void asCExprValue::Set(const asCDataType &dt)
  13919. {
  13920. dataType = dt;
  13921. isTemporary = false;
  13922. stackOffset = 0;
  13923. isConstant = false;
  13924. isVariable = false;
  13925. isExplicitHandle = false;
  13926. qwordValue = 0;
  13927. isLValue = false;
  13928. isRefToLocal = false;
  13929. isRefSafe = false;
  13930. }
  13931. void asCExprValue::SetVariable(const asCDataType &in_dt, int in_stackOffset, bool in_isTemporary)
  13932. {
  13933. Set(in_dt);
  13934. this->isVariable = true;
  13935. this->isTemporary = in_isTemporary;
  13936. this->stackOffset = (short)in_stackOffset;
  13937. }
  13938. void asCExprValue::SetConstantQW(const asCDataType &dt, asQWORD value)
  13939. {
  13940. Set(dt);
  13941. isConstant = true;
  13942. SetConstantQW(value);
  13943. }
  13944. void asCExprValue::SetConstantDW(const asCDataType &dt, asDWORD value)
  13945. {
  13946. Set(dt);
  13947. isConstant = true;
  13948. SetConstantDW(value);
  13949. }
  13950. void asCExprValue::SetConstantB(const asCDataType &dt, asBYTE value)
  13951. {
  13952. Set(dt);
  13953. isConstant = true;
  13954. SetConstantB(value);
  13955. }
  13956. void asCExprValue::SetConstantW(const asCDataType &dt, asWORD value)
  13957. {
  13958. Set(dt);
  13959. isConstant = true;
  13960. SetConstantW(value);
  13961. }
  13962. void asCExprValue::SetConstantF(const asCDataType &dt, float value)
  13963. {
  13964. Set(dt);
  13965. isConstant = true;
  13966. SetConstantF(value);
  13967. }
  13968. void asCExprValue::SetConstantD(const asCDataType &dt, double value)
  13969. {
  13970. Set(dt);
  13971. isConstant = true;
  13972. SetConstantD(value);
  13973. }
  13974. void asCExprValue::SetConstantQW(asQWORD value)
  13975. {
  13976. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  13977. qwordValue = value;
  13978. }
  13979. void asCExprValue::SetConstantDW(asDWORD value)
  13980. {
  13981. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  13982. dwordValue = value;
  13983. }
  13984. void asCExprValue::SetConstantW(asWORD value)
  13985. {
  13986. asASSERT(dataType.GetSizeInMemoryBytes() == 2);
  13987. wordValue = value;
  13988. }
  13989. void asCExprValue::SetConstantB(asBYTE value)
  13990. {
  13991. asASSERT(dataType.GetSizeInMemoryBytes() == 1);
  13992. byteValue = value;
  13993. }
  13994. void asCExprValue::SetConstantF(float value)
  13995. {
  13996. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  13997. floatValue = value;
  13998. }
  13999. void asCExprValue::SetConstantD(double value)
  14000. {
  14001. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  14002. doubleValue = value;
  14003. }
  14004. asQWORD asCExprValue::GetConstantQW()
  14005. {
  14006. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  14007. return qwordValue;
  14008. }
  14009. asDWORD asCExprValue::GetConstantDW()
  14010. {
  14011. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  14012. return dwordValue;
  14013. }
  14014. asWORD asCExprValue::GetConstantW()
  14015. {
  14016. asASSERT(dataType.GetSizeInMemoryBytes() == 2);
  14017. return wordValue;
  14018. }
  14019. asBYTE asCExprValue::GetConstantB()
  14020. {
  14021. asASSERT(dataType.GetSizeInMemoryBytes() == 1);
  14022. return byteValue;
  14023. }
  14024. float asCExprValue::GetConstantF()
  14025. {
  14026. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  14027. return floatValue;
  14028. }
  14029. double asCExprValue::GetConstantD()
  14030. {
  14031. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  14032. return doubleValue;
  14033. }
  14034. void asCExprValue::SetConstantData(const asCDataType &dt, asQWORD qw)
  14035. {
  14036. Set(dt);
  14037. isConstant = true;
  14038. // This code is necessary to guarantee that the code
  14039. // works on both big endian and little endian CPUs.
  14040. if (dataType.GetSizeInMemoryBytes() == 1)
  14041. byteValue = (asBYTE)qw;
  14042. if (dataType.GetSizeInMemoryBytes() == 2)
  14043. wordValue = (asWORD)qw;
  14044. if (dataType.GetSizeInMemoryBytes() == 4)
  14045. dwordValue = (asDWORD)qw;
  14046. else
  14047. qwordValue = qw;
  14048. }
  14049. asQWORD asCExprValue::GetConstantData()
  14050. {
  14051. asQWORD qw = 0;
  14052. // This code is necessary to guarantee that the code
  14053. // works on both big endian and little endian CPUs.
  14054. if (dataType.GetSizeInMemoryBytes() == 1)
  14055. qw = byteValue;
  14056. if (dataType.GetSizeInMemoryBytes() == 2)
  14057. qw = wordValue;
  14058. if (dataType.GetSizeInMemoryBytes() == 4)
  14059. qw = dwordValue;
  14060. else
  14061. qw = qwordValue;
  14062. return qw;
  14063. }
  14064. void asCExprValue::SetUndefinedFuncHandle(asCScriptEngine *engine)
  14065. {
  14066. // This is used for when the expression evaluates to a
  14067. // function, but it is not yet known exactly which. The
  14068. // owner expression will hold the name of the function
  14069. // to determine the exact function when the signature is
  14070. // known.
  14071. Set(asCDataType::CreateObjectHandle(&engine->functionBehaviours, true));
  14072. isConstant = true;
  14073. isExplicitHandle = false;
  14074. qwordValue = 1; // Set to a different value than 0 to differentiate from null constant
  14075. isLValue = false;
  14076. }
  14077. bool asCExprValue::IsUndefinedFuncHandle() const
  14078. {
  14079. if (isConstant == false) return false;
  14080. if (qwordValue == 0) return false;
  14081. if (isLValue) return false;
  14082. if (dataType.GetTypeInfo() == 0) return false;
  14083. if (dataType.GetTypeInfo()->name != "$func") return false;
  14084. if (dataType.IsFuncdef()) return false;
  14085. return true;
  14086. }
  14087. void asCExprValue::SetNullConstant()
  14088. {
  14089. Set(asCDataType::CreateNullHandle());
  14090. isConstant = true;
  14091. isExplicitHandle = false;
  14092. qwordValue = 0;
  14093. isLValue = false;
  14094. }
  14095. bool asCExprValue::IsNullConstant() const
  14096. {
  14097. // We can't check the actual object type, because the null constant may have been cast to another type
  14098. if (isConstant && dataType.IsObjectHandle() && qwordValue == 0)
  14099. return true;
  14100. return false;
  14101. }
  14102. void asCExprValue::SetVoid()
  14103. {
  14104. Set(asCDataType::CreatePrimitive(ttVoid, false));
  14105. isLValue = false;
  14106. isConstant = true;
  14107. }
  14108. bool asCExprValue::IsVoid() const
  14109. {
  14110. if (dataType.GetTokenType() == ttVoid)
  14111. return true;
  14112. return false;
  14113. }
  14114. void asCExprValue::SetDummy()
  14115. {
  14116. SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  14117. }
  14118. ////////////////////////////////////////////////////////////////////////////////////////////////
  14119. asCExprContext::asCExprContext(asCScriptEngine *engine) : bc(engine)
  14120. {
  14121. property_arg = 0;
  14122. Clear();
  14123. }
  14124. asCExprContext::~asCExprContext()
  14125. {
  14126. if (property_arg)
  14127. asDELETE(property_arg, asCExprContext);
  14128. }
  14129. void asCExprContext::Clear()
  14130. {
  14131. bc.ClearAll();
  14132. type.Set(asCDataType());
  14133. deferredParams.SetLength(0);
  14134. if (property_arg)
  14135. asDELETE(property_arg, asCExprContext);
  14136. property_arg = 0;
  14137. exprNode = 0;
  14138. origExpr = 0;
  14139. property_get = 0;
  14140. property_set = 0;
  14141. property_const = false;
  14142. property_handle = false;
  14143. property_ref = false;
  14144. methodName = "";
  14145. enumValue = "";
  14146. symbolNamespace = 0;
  14147. isVoidExpression = false;
  14148. isCleanArg = false;
  14149. isAnonymousInitList = false;
  14150. origCode = 0;
  14151. }
  14152. bool asCExprContext::IsClassMethod() const
  14153. {
  14154. if (type.dataType.GetTypeInfo() == 0) return false;
  14155. if (methodName == "") return false;
  14156. if (type.dataType.GetTypeInfo() == &type.dataType.GetTypeInfo()->engine->functionBehaviours) return false;
  14157. if (isAnonymousInitList) return false;
  14158. return true;
  14159. }
  14160. bool asCExprContext::IsGlobalFunc() const
  14161. {
  14162. if (type.dataType.GetTypeInfo() == 0) return false;
  14163. if (methodName == "") return false;
  14164. if (type.dataType.GetTypeInfo() != &type.dataType.GetTypeInfo()->engine->functionBehaviours) return false;
  14165. if (isAnonymousInitList) return false;
  14166. return true;
  14167. }
  14168. void asCExprContext::SetLambda(asCScriptNode *funcDecl)
  14169. {
  14170. asASSERT(funcDecl && funcDecl->nodeType == snFunction);
  14171. asASSERT(bc.GetLastInstr() == -1);
  14172. Clear();
  14173. type.SetUndefinedFuncHandle(bc.GetEngine());
  14174. exprNode = funcDecl;
  14175. }
  14176. bool asCExprContext::IsLambda() const
  14177. {
  14178. if (type.IsUndefinedFuncHandle() && exprNode && exprNode->nodeType == snFunction)
  14179. return true;
  14180. return false;
  14181. }
  14182. void asCExprContext::SetVoidExpression()
  14183. {
  14184. Clear();
  14185. type.SetVoid();
  14186. isVoidExpression = true;
  14187. }
  14188. bool asCExprContext::IsVoidExpression() const
  14189. {
  14190. if (isVoidExpression && type.IsVoid() && exprNode == 0)
  14191. return true;
  14192. return false;
  14193. }
  14194. void asCExprContext::SetAnonymousInitList(asCScriptNode *initList, asCScriptCode *script)
  14195. {
  14196. Clear();
  14197. exprNode = initList;
  14198. origCode = script;
  14199. isAnonymousInitList = true;
  14200. }
  14201. bool asCExprContext::IsAnonymousInitList() const
  14202. {
  14203. if (isAnonymousInitList && exprNode && exprNode->nodeType == snInitList)
  14204. return true;
  14205. return false;
  14206. }
  14207. void asCExprContext::Copy(asCExprContext *other)
  14208. {
  14209. type = other->type;
  14210. property_get = other->property_get;
  14211. property_set = other->property_set;
  14212. property_const = other->property_const;
  14213. property_handle = other->property_handle;
  14214. property_ref = other->property_ref;
  14215. property_arg = other->property_arg;
  14216. exprNode = other->exprNode;
  14217. methodName = other->methodName;
  14218. enumValue = other->enumValue;
  14219. isVoidExpression = other->isVoidExpression;
  14220. isCleanArg = other->isCleanArg;
  14221. isAnonymousInitList = other->isAnonymousInitList;
  14222. origCode = other->origCode;
  14223. // Do not copy the origExpr member
  14224. }
  14225. void asCExprContext::Merge(asCExprContext *after)
  14226. {
  14227. // Overwrite properties with the expression that comes after
  14228. Copy(after);
  14229. // Clean the properties in 'after' that have now moved into
  14230. // this structure so they are not cleaned up accidentally
  14231. after->property_arg = 0;
  14232. }
  14233. END_AS_NAMESPACE
  14234. #endif // AS_NO_COMPILER