as_compiler.cpp 359 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2012 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 by Lasse Öörni 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()
  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. BEGIN_AS_NAMESPACE
  39. //
  40. // The calling convention rules for script functions:
  41. // - 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
  42. // - 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
  43. // - The object pointer is always passed as the first argument, position 0
  44. // - 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
  45. //
  46. // TODO: I must correct the interpretation of a references to objects in the compiler.
  47. // A reference should mean that a pointer to the object is on the stack.
  48. // No expression should end up as non-references to objects, as the actual object is
  49. // never put on the stack.
  50. // Local variables are declared as non-references, but the expression should be a reference to the variable.
  51. // Function parameters of called functions can also be non-references, but in that case it means the
  52. // object will be passed by value (currently on the heap, which will be moved to the application stack).
  53. //
  54. // The compiler shouldn't use the asCDataType::IsReference. The datatype should always be stored as non-references.
  55. // Instead the compiler should keep track of references in TypeInfo, where it should also state how the reference
  56. // is currently stored, i.e. in variable, in register, on stack, etc.
  57. asCCompiler::asCCompiler(asCScriptEngine *engine) : byteCode(engine)
  58. {
  59. builder = 0;
  60. script = 0;
  61. variables = 0;
  62. isProcessingDeferredParams = false;
  63. isCompilingDefaultArg = false;
  64. noCodeOutput = 0;
  65. }
  66. asCCompiler::~asCCompiler()
  67. {
  68. while( variables )
  69. {
  70. asCVariableScope *var = variables;
  71. variables = variables->parent;
  72. asDELETE(var,asCVariableScope);
  73. }
  74. }
  75. void asCCompiler::Reset(asCBuilder *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  76. {
  77. this->builder = builder;
  78. this->engine = builder->engine;
  79. this->script = script;
  80. this->outFunc = outFunc;
  81. hasCompileErrors = false;
  82. m_isConstructor = false;
  83. m_isConstructorCalled = false;
  84. nextLabel = 0;
  85. breakLabels.SetLength(0);
  86. continueLabels.SetLength(0);
  87. byteCode.ClearAll();
  88. }
  89. int asCCompiler::CompileDefaultConstructor(asCBuilder *builder, asCScriptCode *script, asCScriptNode *node, asCScriptFunction *outFunc)
  90. {
  91. Reset(builder, script, outFunc);
  92. // Make sure all the class members can be initialized with default constructors
  93. for( asUINT n = 0; n < outFunc->objectType->properties.GetLength(); n++ )
  94. {
  95. asCDataType &dt = outFunc->objectType->properties[n]->type;
  96. if( dt.IsObject() && !dt.IsObjectHandle() &&
  97. (((dt.GetObjectType()->flags & asOBJ_REF) && dt.GetObjectType()->beh.factory == 0) ||
  98. ((dt.GetObjectType()->flags & asOBJ_VALUE) && !(dt.GetObjectType()->flags & asOBJ_POD) && dt.GetObjectType()->beh.construct == 0)) )
  99. {
  100. asCString str;
  101. if( dt.GetFuncDef() )
  102. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetFuncDef()->GetName());
  103. else
  104. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetObjectType()->GetName());
  105. Error(str.AddressOf(), node);
  106. }
  107. }
  108. // If the class is derived from another, then the base class' default constructor must be called
  109. if( outFunc->objectType->derivedFrom )
  110. {
  111. // Call the base class' default constructor
  112. byteCode.InstrSHORT(asBC_PSF, 0);
  113. byteCode.Instr(asBC_RDSPtr);
  114. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  115. }
  116. // Pop the object pointer from the stack
  117. byteCode.Ret(AS_PTR_SIZE);
  118. FinalizeFunction();
  119. #ifdef AS_DEBUG
  120. // DEBUG: output byte code
  121. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + "__dc.txt").AddressOf(), engine, outFunc);
  122. #endif
  123. return 0;
  124. }
  125. int asCCompiler::CompileFactory(asCBuilder *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  126. {
  127. Reset(builder, script, outFunc);
  128. unsigned int n;
  129. // Find the corresponding constructor
  130. asCDataType dt = asCDataType::CreateObject(outFunc->returnType.GetObjectType(), false);
  131. int constructor = 0;
  132. for( n = 0; n < dt.GetBehaviour()->factories.GetLength(); n++ )
  133. {
  134. if( dt.GetBehaviour()->factories[n] == outFunc->id )
  135. {
  136. constructor = dt.GetBehaviour()->constructors[n];
  137. break;
  138. }
  139. }
  140. // Allocate the class and instanciate it with the constructor
  141. int varOffset = AllocateVariable(dt, true);
  142. outFunc->variableSpace = AS_PTR_SIZE;
  143. byteCode.InstrSHORT(asBC_PSF, (short)varOffset);
  144. // Copy all arguments to the top of the stack
  145. // TODO: runtime optimize: Might be interesting to have a specific instruction for copying all arguments
  146. int offset = (int)outFunc->GetSpaceNeededForArguments();
  147. for( int a = int(outFunc->parameterTypes.GetLength()) - 1; a >= 0; a-- )
  148. {
  149. if( !outFunc->parameterTypes[a].IsPrimitive() ||
  150. outFunc->parameterTypes[a].IsReference() )
  151. {
  152. offset -= AS_PTR_SIZE;
  153. byteCode.InstrSHORT(asBC_PshVPtr, short(-offset));
  154. }
  155. else
  156. {
  157. if( outFunc->parameterTypes[a].GetSizeOnStackDWords() == 2 )
  158. {
  159. offset -= 2;
  160. byteCode.InstrSHORT(asBC_PshV8, short(-offset));
  161. }
  162. else
  163. {
  164. offset -= 1;
  165. byteCode.InstrSHORT(asBC_PshV4, short(-offset));
  166. }
  167. }
  168. }
  169. int argDwords = (int)outFunc->GetSpaceNeededForArguments();
  170. byteCode.Alloc(asBC_ALLOC, dt.GetObjectType(), constructor, argDwords + AS_PTR_SIZE);
  171. // Return a handle to the newly created object
  172. byteCode.InstrSHORT(asBC_LOADOBJ, (short)varOffset);
  173. byteCode.Ret(argDwords);
  174. FinalizeFunction();
  175. // Tell the virtual machine not to clean up parameters on exception
  176. outFunc->dontCleanUpOnException = true;
  177. /*
  178. #ifdef AS_DEBUG
  179. // DEBUG: output byte code
  180. asCString args;
  181. args.Format("%d", outFunc->parameterTypes.GetLength());
  182. byteCode.DebugOutput(("__" + outFunc->name + "__factory" + args + ".txt").AddressOf(), engine);
  183. #endif
  184. */
  185. return 0;
  186. }
  187. void asCCompiler::FinalizeFunction()
  188. {
  189. asUINT n;
  190. // Tell the bytecode which variables are temporary
  191. for( n = 0; n < variableIsTemporary.GetLength(); n++ )
  192. {
  193. if( variableIsTemporary[n] )
  194. byteCode.DefineTemporaryVariable(GetVariableOffset(n));
  195. }
  196. // Finalize the bytecode
  197. byteCode.Finalize();
  198. byteCode.ExtractObjectVariableInfo(outFunc);
  199. // Compile the list of object variables for the exception handler
  200. // Start with the variables allocated on the heap, and then the ones allocated on the stack
  201. for( n = 0; n < variableAllocations.GetLength(); n++ )
  202. {
  203. if( variableAllocations[n].IsObject() && !variableAllocations[n].IsReference() )
  204. {
  205. if( variableIsOnHeap[n] )
  206. {
  207. outFunc->objVariableTypes.PushLast(variableAllocations[n].GetObjectType());
  208. outFunc->funcVariableTypes.PushLast(variableAllocations[n].GetFuncDef());
  209. outFunc->objVariablePos.PushLast(GetVariableOffset(n));
  210. }
  211. }
  212. }
  213. outFunc->objVariablesOnHeap = outFunc->objVariablePos.GetLength();
  214. for( n = 0; n < variableAllocations.GetLength(); n++ )
  215. {
  216. if( variableAllocations[n].IsObject() && !variableAllocations[n].IsReference() )
  217. {
  218. if( !variableIsOnHeap[n] )
  219. {
  220. outFunc->objVariableTypes.PushLast(variableAllocations[n].GetObjectType());
  221. outFunc->funcVariableTypes.PushLast(variableAllocations[n].GetFuncDef());
  222. outFunc->objVariablePos.PushLast(GetVariableOffset(n));
  223. }
  224. }
  225. }
  226. // Copy byte code to the function
  227. outFunc->byteCode.SetLength(byteCode.GetSize());
  228. byteCode.Output(outFunc->byteCode.AddressOf());
  229. outFunc->AddReferences();
  230. outFunc->stackNeeded = byteCode.largestStackUsed + outFunc->variableSpace;
  231. outFunc->lineNumbers = byteCode.lineNumbers;
  232. }
  233. // Entry
  234. int asCCompiler::CompileFunction(asCBuilder *builder, asCScriptCode *script, sExplicitSignature *signature, asCScriptNode *func, asCScriptFunction *outFunc)
  235. {
  236. // TODO: The compiler should take the return type and parameter types from the
  237. // outFunc, instead of interpreting the script nodes again. The builder
  238. // must pass the list of parameter names. Making this change we can
  239. // eliminate large parts of this function and the sExplicitSignature structure
  240. Reset(builder, script, outFunc);
  241. int buildErrors = builder->numErrors;
  242. int stackPos = 0;
  243. if( outFunc->objectType )
  244. stackPos = -AS_PTR_SIZE; // The first parameter is the pointer to the object
  245. // Reserve a label for the cleanup code
  246. nextLabel++;
  247. // Add the first variable scope, which the parameters and
  248. // variables declared in the outermost statement block is
  249. // part of.
  250. AddVariableScope();
  251. asCScriptNode *node;
  252. bool isDestructor = false;
  253. asCDataType returnType;
  254. if( !signature )
  255. {
  256. node = func->firstChild;
  257. if( outFunc->objectType )
  258. {
  259. // Skip the private keyword if it is there
  260. if( node->nodeType == snUndefined && node->tokenType == ttPrivate )
  261. node = node->next;
  262. }
  263. else if( outFunc->IsShared() )
  264. {
  265. // Skip the shared keyword
  266. asASSERT( node->nodeType == snIdentifier );
  267. node = node->next;
  268. }
  269. //----------------------------------------------
  270. // Examine return type
  271. if( node->nodeType == snDataType )
  272. {
  273. // TODO: namespace: Use correct implicit namespace from function
  274. returnType = builder->CreateDataTypeFromNode(node, script, "");
  275. returnType = builder->ModifyDataTypeFromNode(returnType, node->next, script, 0, 0);
  276. // Make sure the return type is instanciable or is void
  277. if( !returnType.CanBeInstanciated() &&
  278. returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  279. {
  280. asCString str;
  281. str.Format(TXT_DATA_TYPE_CANT_BE_s, returnType.Format().AddressOf());
  282. Error(str.AddressOf(), func->firstChild);
  283. }
  284. }
  285. else
  286. {
  287. returnType = asCDataType::CreatePrimitive(ttVoid, false);
  288. if( node->tokenType == ttBitNot )
  289. isDestructor = true;
  290. else
  291. m_isConstructor = true;
  292. }
  293. }
  294. else
  295. {
  296. node = func;
  297. returnType = signature->returnType;
  298. }
  299. // If the return type is a value type returned by value the address of the
  300. // location where the value will be stored is pushed on the stack before
  301. // the arguments
  302. if( !(isDestructor || m_isConstructor) && outFunc->DoesReturnOnStack() )
  303. stackPos -= AS_PTR_SIZE;
  304. asCVariableScope vs(0);
  305. if( !signature )
  306. {
  307. //----------------------------------------------
  308. // Declare parameters
  309. // Find first parameter
  310. while( node && node->nodeType != snParameterList )
  311. node = node->next;
  312. // Register parameters from last to first, otherwise they will be destroyed in the wrong order
  313. if( node ) node = node->firstChild;
  314. while( node )
  315. {
  316. // Get the parameter type
  317. // TODO: namespace: Use correct implicit namespace from function
  318. asCDataType type = builder->CreateDataTypeFromNode(node, script, "");
  319. asETypeModifiers inoutFlag = asTM_NONE;
  320. type = builder->ModifyDataTypeFromNode(type, node->next, script, &inoutFlag, 0);
  321. // Is the data type allowed?
  322. if( (type.IsReference() && inoutFlag != asTM_INOUTREF && !type.CanBeInstanciated()) ||
  323. (!type.IsReference() && !type.CanBeInstanciated()) )
  324. {
  325. asCString parm = type.Format();
  326. if( inoutFlag == asTM_INREF )
  327. parm += "in";
  328. else if( inoutFlag == asTM_OUTREF )
  329. parm += "out";
  330. asCString str;
  331. str.Format(TXT_PARAMETER_CANT_BE_s, parm.AddressOf());
  332. Error(str.AddressOf(), node);
  333. }
  334. // If the parameter has a name then declare it as variable
  335. node = node->next->next;
  336. if( node && node->nodeType == snIdentifier )
  337. {
  338. asCString name(&script->code[node->tokenPos], node->tokenLength);
  339. if( vs.DeclareVariable(name.AddressOf(), type, stackPos, true) < 0 )
  340. {
  341. // TODO: It might be an out-of-memory too
  342. Error(TXT_PARAMETER_ALREADY_DECLARED, node);
  343. }
  344. // Add marker for variable declaration
  345. byteCode.VarDecl((int)outFunc->variables.GetLength());
  346. outFunc->AddVariable(name, type, stackPos);
  347. node = node->next;
  348. // Skip the default arg
  349. if( node && node->nodeType == snExpression )
  350. node = node->next;
  351. }
  352. else
  353. vs.DeclareVariable("", type, stackPos, true);
  354. // Move to next parameter
  355. stackPos -= type.GetSizeOnStackDWords();
  356. }
  357. }
  358. else
  359. {
  360. asCArray<asCDataType> &args = signature->argTypes;
  361. asCArray<asETypeModifiers> &inoutFlags = signature->argModifiers;
  362. asCArray<asCString> &argNames = signature->argNames;
  363. asASSERT(args.GetLength() == argNames.GetLength());
  364. for( int k = 0; k < (int)args.GetLength(); k++ )
  365. {
  366. asCDataType type = args[k];
  367. asETypeModifiers inoutFlag = inoutFlags[k];
  368. if( (type.IsReference() && inoutFlag != asTM_INOUTREF && !type.CanBeInstanciated()) ||
  369. (!type.IsReference() && !type.CanBeInstanciated()) )
  370. {
  371. asCString str;
  372. str.Format(TXT_PARAMETER_CANT_BE_s, type.Format().AddressOf());
  373. Error(str.AddressOf(), node);
  374. }
  375. if( 0 != argNames[k].Compare("") )
  376. {
  377. if( vs.DeclareVariable(argNames[k].AddressOf(), type, stackPos, true) < 0 )
  378. Error(TXT_PARAMETER_ALREADY_DECLARED, node);
  379. // Add marker for variable declaration
  380. byteCode.VarDecl((int)outFunc->variables.GetLength());
  381. outFunc->AddVariable(argNames[k], type, stackPos);
  382. }
  383. else
  384. vs.DeclareVariable("", type, stackPos, true);
  385. // Move to next parameter
  386. stackPos -= type.GetSizeOnStackDWords();
  387. }
  388. }
  389. int n;
  390. for( n = (int)vs.variables.GetLength() - 1; n >= 0; n-- )
  391. {
  392. variables->DeclareVariable(vs.variables[n]->name.AddressOf(), vs.variables[n]->type, vs.variables[n]->stackOffset, vs.variables[n]->onHeap);
  393. }
  394. // Is the return type allowed?
  395. if( (returnType.GetSizeOnStackDWords() == 0 && returnType != asCDataType::CreatePrimitive(ttVoid, false)) ||
  396. (returnType.IsReference() && !returnType.CanBeInstanciated()) )
  397. {
  398. asCString str;
  399. str.Format(TXT_RETURN_CANT_BE_s, returnType.Format().AddressOf());
  400. Error(str.AddressOf(), func);
  401. }
  402. variables->DeclareVariable("return", returnType, stackPos, true);
  403. //--------------------------------------------
  404. // Compile the statement block
  405. // We need to parse the statement block now
  406. asCScriptNode *blockBegin;
  407. if( !signature )
  408. blockBegin = func->lastChild;
  409. else
  410. blockBegin = func;
  411. // TODO: memory: We can parse the statement block one statement at a time, thus save even more memory
  412. asCParser parser(builder);
  413. int r = parser.ParseStatementBlock(script, blockBegin);
  414. if( r < 0 ) return -1;
  415. asCScriptNode *block = parser.GetScriptNode();
  416. bool hasReturn;
  417. asCByteCode bc(engine);
  418. LineInstr(&bc, blockBegin->tokenPos);
  419. CompileStatementBlock(block, false, &hasReturn, &bc);
  420. LineInstr(&bc, blockBegin->tokenPos + blockBegin->tokenLength);
  421. // Make sure there is a return in all paths (if not return type is void)
  422. if( returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  423. {
  424. if( hasReturn == false )
  425. Error(TXT_NOT_ALL_PATHS_RETURN, blockBegin);
  426. }
  427. //------------------------------------------------
  428. // Concatenate the bytecode
  429. // Insert a JitEntry at the start of the function for JIT compilers
  430. byteCode.InstrPTR(asBC_JitEntry, 0);
  431. // Count total variable size
  432. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  433. outFunc->variableSpace = varSize;
  434. if( outFunc->objectType )
  435. {
  436. // Call the base class' default constructor unless called manually in the code
  437. if( m_isConstructor && !m_isConstructorCalled && outFunc->objectType->derivedFrom )
  438. {
  439. if( outFunc->objectType->derivedFrom->beh.construct )
  440. {
  441. byteCode.InstrSHORT(asBC_PSF, 0);
  442. byteCode.Instr(asBC_RDSPtr);
  443. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  444. }
  445. else
  446. {
  447. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, blockBegin);
  448. }
  449. }
  450. // Increase the reference for the object pointer, so that it is guaranteed to live during the entire call
  451. if( !m_isConstructor && !outFunc->returnType.IsReference() )
  452. {
  453. // TODO: runtime optimize: If the function is trivial, i.e. doesn't access any outside functions,
  454. // then this is not necessary. If I implement this, then the function needs
  455. // to set a flag so the exception handler doesn't try to release the handle.
  456. // It is not necessary to do this for constructors, as they have no outside references that can be released anyway
  457. // It is not necessary to do this for methods that return references, as the caller is guaranteed to hold a reference to the object
  458. byteCode.InstrSHORT(asBC_PSF, 0);
  459. byteCode.Instr(asBC_RDSPtr);
  460. byteCode.Call(asBC_CALLSYS, outFunc->objectType->beh.addref, AS_PTR_SIZE);
  461. }
  462. }
  463. // Add the code for the statement block
  464. byteCode.AddCode(&bc);
  465. // Deallocate all local variables
  466. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  467. {
  468. sVariable *v = variables->variables[n];
  469. if( v->stackOffset > 0 )
  470. {
  471. // Call variables destructors
  472. if( v->name != "return" && v->name != "return address" )
  473. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  474. DeallocateVariable(v->stackOffset);
  475. }
  476. }
  477. // This is the label that return statements jump to
  478. // in order to exit the function
  479. byteCode.Label(0);
  480. // Call destructors for function parameters
  481. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  482. {
  483. sVariable *v = variables->variables[n];
  484. if( v->stackOffset <= 0 )
  485. {
  486. // Call variable destructors here, for variables not yet destroyed
  487. if( v->name != "return" && v->name != "return address" )
  488. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  489. }
  490. // Do not deallocate parameters
  491. }
  492. // Release the object pointer again
  493. if( outFunc->objectType && !m_isConstructor && !outFunc->returnType.IsReference() )
  494. {
  495. byteCode.InstrW_PTR(asBC_FREE, 0, outFunc->objectType);
  496. }
  497. // If there are compile errors, there is no reason to build the final code
  498. if( hasCompileErrors || builder->numErrors != buildErrors )
  499. return -1;
  500. // At this point there should be no variables allocated
  501. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  502. // Remove the variable scope
  503. RemoveVariableScope();
  504. // This POP is not necessary as the return will clean up the stack frame anyway.
  505. // The bytecode optimizer would remove this POP, however by not including it here
  506. // it is guaranteed it doesn't have to be adjusted by the asCRestore class when
  507. // a types are of a different size than originally compiled for.
  508. // byteCode.Pop(varSize);
  509. byteCode.Ret(-stackPos);
  510. FinalizeFunction();
  511. #ifdef AS_DEBUG
  512. // DEBUG: output byte code
  513. if( outFunc->objectType )
  514. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + ".txt").AddressOf(), engine, outFunc);
  515. else
  516. byteCode.DebugOutput(("__" + outFunc->name + ".txt").AddressOf(), engine, outFunc);
  517. #endif
  518. return 0;
  519. }
  520. int asCCompiler::CallCopyConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asSExprContext *arg, asCScriptNode *node, bool isGlobalVar, bool derefDest)
  521. {
  522. if( !type.IsObject() )
  523. return 0;
  524. // CallCopyConstructor should not be called for object handles.
  525. asASSERT( !type.IsObjectHandle() );
  526. asCArray<asSExprContext*> args;
  527. args.PushLast(arg);
  528. // The reference parameter must be pushed on the stack
  529. asASSERT( arg->type.dataType.GetObjectType() == type.GetObjectType() );
  530. // Since we're calling the copy constructor, we have to trust the function to not do
  531. // anything stupid otherwise we will just enter a loop, as we try to make temporary
  532. // copies of the argument in order to guarantee safety.
  533. if( type.GetObjectType()->flags & asOBJ_REF )
  534. {
  535. asSExprContext ctx(engine);
  536. int func = 0;
  537. asSTypeBehaviour *beh = type.GetBehaviour();
  538. if( beh ) func = beh->copyfactory;
  539. if( func > 0 )
  540. {
  541. if( !isGlobalVar )
  542. {
  543. // Call factory and store the handle in the given variable
  544. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType(), true, offset);
  545. // Pop the reference left by the function call
  546. ctx.bc.Instr(asBC_PopPtr);
  547. }
  548. else
  549. {
  550. // Call factory
  551. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType());
  552. // Store the returned handle in the global variable
  553. ctx.bc.Instr(asBC_RDSPtr);
  554. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  555. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  556. ctx.bc.Instr(asBC_PopPtr);
  557. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  558. }
  559. bc->AddCode(&ctx.bc);
  560. return 0;
  561. }
  562. }
  563. else
  564. {
  565. asSTypeBehaviour *beh = type.GetBehaviour();
  566. int func = beh ? beh->copyconstruct : 0;
  567. if( func > 0 )
  568. {
  569. // Push the address where the object will be stored on the stack, before the argument
  570. // TODO: When the context is serializable this probably has to be changed, since this
  571. // pointer can remain on the stack while the context is suspended. There is no
  572. // risk the pointer becomes invalid though, there is just no easy way to serialize it.
  573. asCByteCode tmp(engine);
  574. if( isGlobalVar )
  575. tmp.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  576. else if( isObjectOnHeap )
  577. tmp.InstrSHORT(asBC_PSF, (short)offset);
  578. tmp.AddCode(bc);
  579. bc->AddCode(&tmp);
  580. // When the object is allocated on the stack the object pointer
  581. // must be pushed on the stack after the arguments
  582. if( !isObjectOnHeap )
  583. {
  584. asASSERT( !isGlobalVar );
  585. bc->InstrSHORT(asBC_PSF, (short)offset);
  586. if( derefDest )
  587. {
  588. // The variable is a reference to the real location, so we need to dereference it
  589. bc->Instr(asBC_RDSPtr);
  590. }
  591. }
  592. asSExprContext ctx(engine);
  593. PerformFunctionCall(func, &ctx, isObjectOnHeap, &args, type.GetObjectType());
  594. bc->AddCode(&ctx.bc);
  595. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  596. // Mark the object as initialized
  597. if( !isObjectOnHeap )
  598. bc->ObjInfo(offset, asOBJ_INIT);
  599. return 0;
  600. }
  601. }
  602. // Class has no copy constructor/factory.
  603. asCString str;
  604. str.Format(TXT_NO_COPY_CONSTRUCTOR_FOR_s, type.GetObjectType()->GetName());
  605. Error(str.AddressOf(), node);
  606. return -1;
  607. }
  608. int asCCompiler::CallDefaultConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCScriptNode *node, bool isGlobalVar, bool deferDest)
  609. {
  610. if( !type.IsObject() || type.IsObjectHandle() )
  611. return 0;
  612. if( type.GetObjectType()->flags & asOBJ_REF )
  613. {
  614. asSExprContext ctx(engine);
  615. ctx.exprNode = node;
  616. int func = 0;
  617. asSTypeBehaviour *beh = type.GetBehaviour();
  618. if( beh ) func = beh->factory;
  619. if( func > 0 )
  620. {
  621. if( !isGlobalVar )
  622. {
  623. // Call factory and store the handle in the given variable
  624. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType(), true, offset);
  625. // Pop the reference left by the function call
  626. ctx.bc.Instr(asBC_PopPtr);
  627. }
  628. else
  629. {
  630. // Call factory
  631. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType());
  632. // Store the returned handle in the global variable
  633. ctx.bc.Instr(asBC_RDSPtr);
  634. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  635. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  636. ctx.bc.Instr(asBC_PopPtr);
  637. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  638. }
  639. bc->AddCode(&ctx.bc);
  640. return 0;
  641. }
  642. }
  643. else
  644. {
  645. asSTypeBehaviour *beh = type.GetBehaviour();
  646. int func = 0;
  647. if( beh ) func = beh->construct;
  648. // Allocate and initialize with the default constructor
  649. if( func != 0 || (type.GetObjectType()->flags & asOBJ_POD) )
  650. {
  651. if( !isObjectOnHeap )
  652. {
  653. asASSERT( !isGlobalVar );
  654. // There is nothing to do if there is no function,
  655. // as the memory is already allocated on the stack
  656. if( func )
  657. {
  658. // Call the constructor as a normal function
  659. bc->InstrSHORT(asBC_PSF, (short)offset);
  660. if( deferDest )
  661. bc->Instr(asBC_RDSPtr);
  662. asSExprContext ctx(engine);
  663. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType());
  664. bc->AddCode(&ctx.bc);
  665. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  666. // Mark the object as initialized
  667. bc->ObjInfo(offset, asOBJ_INIT);
  668. }
  669. }
  670. else
  671. {
  672. if( isGlobalVar )
  673. bc->InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  674. else
  675. bc->InstrSHORT(asBC_PSF, (short)offset);
  676. bc->Alloc(asBC_ALLOC, type.GetObjectType(), func, AS_PTR_SIZE);
  677. }
  678. return 0;
  679. }
  680. }
  681. // Class has no default factory/constructor.
  682. asCString str;
  683. // TODO: funcdef: asCDataType should have a GetTypeName()
  684. if( type.GetFuncDef() )
  685. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetFuncDef()->GetName());
  686. else
  687. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetObjectType()->GetName());
  688. Error(str.AddressOf(), node);
  689. return -1;
  690. }
  691. void asCCompiler::CallDestructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc)
  692. {
  693. if( !type.IsReference() )
  694. {
  695. // Call destructor for the data type
  696. if( type.IsObject() )
  697. {
  698. if( isObjectOnHeap || type.IsObjectHandle() )
  699. {
  700. // Free the memory
  701. bc->InstrW_PTR(asBC_FREE, (short)offset, type.GetObjectType());
  702. }
  703. else
  704. {
  705. asASSERT( type.GetObjectType()->GetFlags() & asOBJ_VALUE );
  706. if( type.GetBehaviour()->destruct )
  707. {
  708. // Call the destructor as a regular function
  709. bc->InstrSHORT(asBC_PSF, (short)offset);
  710. asSExprContext ctx(engine);
  711. PerformFunctionCall(type.GetBehaviour()->destruct, &ctx);
  712. bc->AddCode(&ctx.bc);
  713. }
  714. // TODO: Value on stack: This probably needs to be done in PerformFunctionCall
  715. // Mark the object as destroyed
  716. bc->ObjInfo(offset, asOBJ_UNINIT);
  717. }
  718. }
  719. }
  720. }
  721. void asCCompiler::LineInstr(asCByteCode *bc, size_t pos)
  722. {
  723. int r, c;
  724. script->ConvertPosToRowCol(pos, &r, &c);
  725. bc->Line(r, c);
  726. }
  727. void asCCompiler::CompileStatementBlock(asCScriptNode *block, bool ownVariableScope, bool *hasReturn, asCByteCode *bc)
  728. {
  729. *hasReturn = false;
  730. bool isFinished = false;
  731. bool hasWarned = false;
  732. if( ownVariableScope )
  733. {
  734. bc->Block(true);
  735. AddVariableScope();
  736. }
  737. asCScriptNode *node = block->firstChild;
  738. while( node )
  739. {
  740. if( !hasWarned && (*hasReturn || isFinished) )
  741. {
  742. hasWarned = true;
  743. Warning(TXT_UNREACHABLE_CODE, node);
  744. }
  745. if( node->nodeType == snBreak || node->nodeType == snContinue )
  746. isFinished = true;
  747. asCByteCode statement(engine);
  748. if( node->nodeType == snDeclaration )
  749. CompileDeclaration(node, &statement);
  750. else
  751. CompileStatement(node, hasReturn, &statement);
  752. LineInstr(bc, node->tokenPos);
  753. bc->AddCode(&statement);
  754. if( !hasCompileErrors )
  755. {
  756. asASSERT( tempVariables.GetLength() == 0 );
  757. asASSERT( reservedVariables.GetLength() == 0 );
  758. }
  759. node = node->next;
  760. }
  761. if( ownVariableScope )
  762. {
  763. // Deallocate variables in this block, in reverse order
  764. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  765. {
  766. sVariable *v = variables->variables[n];
  767. // Call variable destructors here, for variables not yet destroyed
  768. // If the block is terminated with a break, continue, or
  769. // return the variables are already destroyed
  770. if( !isFinished && !*hasReturn )
  771. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  772. // Don't deallocate function parameters
  773. if( v->stackOffset > 0 )
  774. DeallocateVariable(v->stackOffset);
  775. }
  776. RemoveVariableScope();
  777. bc->Block(false);
  778. }
  779. }
  780. // Entry
  781. int asCCompiler::CompileGlobalVariable(asCBuilder *builder, asCScriptCode *script, asCScriptNode *node, sGlobalVariableDescription *gvar, asCScriptFunction *outFunc)
  782. {
  783. Reset(builder, script, outFunc);
  784. // Add a variable scope (even though variables can't be declared)
  785. AddVariableScope();
  786. asSExprContext ctx(engine);
  787. gvar->isPureConstant = false;
  788. // Parse the initialization nodes
  789. asCParser parser(builder);
  790. if( node )
  791. {
  792. int r = parser.ParseGlobalVarInit(script, node);
  793. if( r < 0 )
  794. return r;
  795. node = parser.GetScriptNode();
  796. }
  797. // Compile the expression
  798. if( node && node->nodeType == snArgList )
  799. {
  800. // Make sure that it is a registered type, and that it isn't a pointer
  801. if( gvar->datatype.GetObjectType() == 0 || gvar->datatype.IsObjectHandle() )
  802. {
  803. Error(TXT_MUST_BE_OBJECT, node);
  804. }
  805. else
  806. {
  807. // Compile the arguments
  808. asCArray<asSExprContext *> args;
  809. if( CompileArgumentList(node, args) >= 0 )
  810. {
  811. // Find all constructors
  812. asCArray<int> funcs;
  813. asSTypeBehaviour *beh = gvar->datatype.GetBehaviour();
  814. if( beh )
  815. {
  816. if( gvar->datatype.GetObjectType()->flags & asOBJ_REF )
  817. funcs = beh->factories;
  818. else
  819. funcs = beh->constructors;
  820. }
  821. asCString str = gvar->datatype.Format();
  822. MatchFunctions(funcs, args, node, str.AddressOf());
  823. if( funcs.GetLength() == 1 )
  824. {
  825. int r = asSUCCESS;
  826. // Add the default values for arguments not explicitly supplied
  827. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  828. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  829. r = CompileDefaultArgs(node, args, func);
  830. if( r == asSUCCESS )
  831. {
  832. if( gvar->datatype.GetObjectType()->flags & asOBJ_REF )
  833. {
  834. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  835. // Store the returned handle in the global variable
  836. ctx.bc.Instr(asBC_RDSPtr);
  837. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  838. ctx.bc.InstrPTR(asBC_REFCPY, gvar->datatype.GetObjectType());
  839. ctx.bc.Instr(asBC_PopPtr);
  840. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  841. }
  842. else
  843. {
  844. // Push the address of the location where the variable will be stored on the stack.
  845. // This reference is safe, because the addresses of the global variables cannot change.
  846. // TODO: When serialization of the context is implemented this will probably have to change,
  847. // because this pointer may be on the stack while the context is suspended, and may
  848. // be difficult to serialize as the context doesn't know that the value represents a
  849. // pointer.
  850. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  851. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  852. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  853. PerformFunctionCall(funcs[0], &ctx, true, &args, gvar->datatype.GetObjectType());
  854. }
  855. }
  856. }
  857. }
  858. // Cleanup
  859. for( asUINT n = 0; n < args.GetLength(); n++ )
  860. if( args[n] )
  861. {
  862. asDELETE(args[n],asSExprContext);
  863. }
  864. }
  865. }
  866. else if( node && node->nodeType == snInitList )
  867. {
  868. asCTypeInfo ti;
  869. ti.Set(gvar->datatype);
  870. ti.isVariable = false;
  871. ti.isTemporary = false;
  872. ti.stackOffset = (short)gvar->index;
  873. ti.isLValue = true;
  874. CompileInitList(&ti, node, &ctx.bc);
  875. node = node->next;
  876. }
  877. else if( node )
  878. {
  879. // Compile the right hand expression
  880. asSExprContext expr(engine);
  881. int r = CompileAssignment(node, &expr); if( r < 0 ) return r;
  882. // Assign the value to the variable
  883. if( gvar->datatype.IsPrimitive() )
  884. {
  885. if( gvar->datatype.IsReadOnly() && expr.type.isConstant )
  886. {
  887. ImplicitConversion(&expr, gvar->datatype, node, asIC_IMPLICIT_CONV);
  888. gvar->isPureConstant = true;
  889. gvar->constantValue = expr.type.qwordValue;
  890. }
  891. asSExprContext lctx(engine);
  892. lctx.type.Set(gvar->datatype);
  893. lctx.type.dataType.MakeReference(true);
  894. lctx.type.dataType.MakeReadOnly(false);
  895. lctx.type.isLValue = true;
  896. // If it is an enum value that is being compiled, then
  897. // we skip this, as the bytecode won't be used anyway
  898. if( !gvar->isEnumValue )
  899. lctx.bc.InstrPTR(asBC_LDG, engine->globalProperties[gvar->index]->GetAddressOfValue());
  900. DoAssignment(&ctx, &lctx, &expr, node, node, ttAssignment, node);
  901. }
  902. else
  903. {
  904. // TODO: runtime optimize: Here we should look for the best matching constructor, instead of
  905. // just the copy constructor. Only if no appropriate constructor is
  906. // available should the assignment operator be used.
  907. if( !gvar->datatype.IsObjectHandle() )
  908. {
  909. // Call the default constructor to have a valid object for the assignment
  910. CallDefaultConstructor(gvar->datatype, gvar->index, true, &ctx.bc, gvar->idNode, true);
  911. }
  912. asSExprContext lexpr(engine);
  913. lexpr.type.Set(gvar->datatype);
  914. lexpr.type.dataType.MakeReference(true);
  915. lexpr.type.dataType.MakeReadOnly(false);
  916. lexpr.type.stackOffset = -1;
  917. lexpr.type.isLValue = true;
  918. if( gvar->datatype.IsObjectHandle() )
  919. lexpr.type.isExplicitHandle = true;
  920. lexpr.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  921. // If left expression resolves into a registered type
  922. // check if the assignment operator is overloaded, and check
  923. // the type of the right hand expression. If none is found
  924. // the default action is a direct copy if it is the same type
  925. // and a simple assignment.
  926. bool assigned = false;
  927. // Even though an ASHANDLE can be an explicit handle the assignment needs to be treated by the overloaded operator
  928. if( lexpr.type.dataType.IsObject() && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  929. {
  930. assigned = CompileOverloadedDualOperator(node, &lexpr, &expr, &ctx);
  931. if( assigned )
  932. {
  933. // Pop the resulting value
  934. ctx.bc.Instr(asBC_PopPtr);
  935. // Release the argument
  936. ProcessDeferredParams(&ctx);
  937. }
  938. }
  939. if( !assigned )
  940. {
  941. PrepareForAssignment(&lexpr.type.dataType, &expr, node, false);
  942. // If the expression is constant and the variable also is constant
  943. // then mark the variable as pure constant. This will allow the compiler
  944. // to optimize expressions with this variable.
  945. if( gvar->datatype.IsReadOnly() && expr.type.isConstant )
  946. {
  947. gvar->isPureConstant = true;
  948. gvar->constantValue = expr.type.qwordValue;
  949. }
  950. // Add expression code to bytecode
  951. MergeExprBytecode(&ctx, &expr);
  952. // Add byte code for storing value of expression in variable
  953. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[gvar->index]->GetAddressOfValue());
  954. PerformAssignment(&lexpr.type, &expr.type, &ctx.bc, node);
  955. // Release temporary variables used by expression
  956. ReleaseTemporaryVariable(expr.type, &ctx.bc);
  957. ctx.bc.Instr(asBC_PopPtr);
  958. }
  959. }
  960. }
  961. else if( gvar->datatype.IsObject() && !gvar->datatype.IsObjectHandle() )
  962. {
  963. // Call the default constructor in case no explicit initialization is given
  964. CallDefaultConstructor(gvar->datatype, gvar->index, true, &ctx.bc, gvar->idNode, true);
  965. }
  966. // Concatenate the bytecode
  967. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  968. // Add information on the line number for the global variable
  969. size_t pos = 0;
  970. if( gvar->idNode )
  971. pos = gvar->idNode->tokenPos;
  972. else if( gvar->nextNode )
  973. pos = gvar->nextNode->tokenPos;
  974. LineInstr(&byteCode, pos);
  975. // Reserve space for all local variables
  976. outFunc->variableSpace = varSize;
  977. byteCode.AddCode(&ctx.bc);
  978. // Deallocate variables in this block, in reverse order
  979. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; --n )
  980. {
  981. sVariable *v = variables->variables[n];
  982. // Call variable destructors here, for variables not yet destroyed
  983. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  984. DeallocateVariable(v->stackOffset);
  985. }
  986. if( hasCompileErrors ) return -1;
  987. // At this point there should be no variables allocated
  988. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  989. // Remove the variable scope again
  990. RemoveVariableScope();
  991. byteCode.Ret(0);
  992. FinalizeFunction();
  993. #ifdef AS_DEBUG
  994. // DEBUG: output byte code
  995. byteCode.DebugOutput(("___init_" + gvar->name + ".txt").AddressOf(), engine, outFunc);
  996. #endif
  997. return 0;
  998. }
  999. void asCCompiler::PrepareArgument(asCDataType *paramType, asSExprContext *ctx, asCScriptNode *node, bool isFunction, int refType, bool isMakingCopy)
  1000. {
  1001. asCDataType param = *paramType;
  1002. if( paramType->GetTokenType() == ttQuestion )
  1003. {
  1004. // Since the function is expecting a var type ?, then we don't want to convert the argument to anything else
  1005. param = ctx->type.dataType;
  1006. param.MakeHandle(ctx->type.isExplicitHandle);
  1007. param.MakeReference(paramType->IsReference());
  1008. param.MakeReadOnly(paramType->IsReadOnly());
  1009. }
  1010. else
  1011. param = *paramType;
  1012. asCDataType dt = param;
  1013. // Need to protect arguments by reference
  1014. if( isFunction && dt.IsReference() )
  1015. {
  1016. // Allocate a temporary variable of the same type as the argument
  1017. dt.MakeReference(false);
  1018. dt.MakeReadOnly(false);
  1019. int offset;
  1020. if( refType == 1 ) // &in
  1021. {
  1022. ProcessPropertyGetAccessor(ctx, node);
  1023. // Add the type id as hidden arg if the parameter is a ? type
  1024. if( paramType->GetTokenType() == ttQuestion )
  1025. {
  1026. asCByteCode tmpBC(engine);
  1027. // Place the type id on the stack as a hidden parameter
  1028. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1029. // Insert the code before the expression code
  1030. tmpBC.AddCode(&ctx->bc);
  1031. ctx->bc.AddCode(&tmpBC);
  1032. }
  1033. // If the reference is const, then it is not necessary to make a copy if the value already is a variable
  1034. // Even if the same variable is passed in another argument as non-const then there is no problem
  1035. if( dt.IsPrimitive() || dt.IsNullHandle() )
  1036. {
  1037. IsVariableInitialized(&ctx->type, node);
  1038. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1039. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  1040. if( !(param.IsReadOnly() && ctx->type.isVariable) )
  1041. ConvertToTempVariable(ctx);
  1042. PushVariableOnStack(ctx, true);
  1043. ctx->type.dataType.MakeReadOnly(param.IsReadOnly());
  1044. }
  1045. else
  1046. {
  1047. IsVariableInitialized(&ctx->type, node);
  1048. if( !isMakingCopy )
  1049. {
  1050. ImplicitConversion(ctx, param, node, asIC_IMPLICIT_CONV, true);
  1051. if( !ctx->type.dataType.IsEqualExceptRef(param) )
  1052. {
  1053. asCString str;
  1054. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), param.Format().AddressOf());
  1055. Error(str.AddressOf(), node);
  1056. ctx->type.Set(param);
  1057. }
  1058. }
  1059. // If the argument already is a temporary
  1060. // variable we don't need to allocate another
  1061. // If the parameter is read-only and the object already is a local
  1062. // variable then it is not necessary to make a copy either
  1063. if( !ctx->type.isTemporary && !(param.IsReadOnly() && ctx->type.isVariable) && !isMakingCopy )
  1064. {
  1065. // Make sure the variable is not used in the expression
  1066. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1067. // TODO: copy: Use copy constructor if available. See PrepareTemporaryObject()
  1068. // Allocate and construct the temporary object
  1069. asCByteCode tmpBC(engine);
  1070. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1071. // Insert the code before the expression code
  1072. tmpBC.AddCode(&ctx->bc);
  1073. ctx->bc.AddCode(&tmpBC);
  1074. // Assign the evaluated expression to the temporary variable
  1075. PrepareForAssignment(&dt, ctx, node, true);
  1076. dt.MakeReference(IsVariableOnHeap(offset));
  1077. asCTypeInfo type;
  1078. type.Set(dt);
  1079. type.isTemporary = true;
  1080. type.stackOffset = (short)offset;
  1081. if( dt.IsObjectHandle() )
  1082. type.isExplicitHandle = true;
  1083. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1084. PerformAssignment(&type, &ctx->type, &ctx->bc, node);
  1085. ctx->bc.Instr(asBC_PopPtr);
  1086. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  1087. ctx->type = type;
  1088. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1089. if( dt.IsObject() && !dt.IsObjectHandle() )
  1090. ctx->bc.Instr(asBC_RDSPtr);
  1091. if( paramType->IsReadOnly() )
  1092. ctx->type.dataType.MakeReadOnly(true);
  1093. }
  1094. else if( isMakingCopy )
  1095. {
  1096. // We must guarantee that the address to the value is on the stack
  1097. if( ctx->type.dataType.IsObject() &&
  1098. !ctx->type.dataType.IsObjectHandle() &&
  1099. ctx->type.dataType.IsReference() )
  1100. Dereference(ctx, true);
  1101. }
  1102. }
  1103. }
  1104. else if( refType == 2 ) // &out
  1105. {
  1106. // Add the type id as hidden arg if the parameter is a ? type
  1107. if( paramType->GetTokenType() == ttQuestion )
  1108. {
  1109. asCByteCode tmpBC(engine);
  1110. // Place the type id on the stack as a hidden parameter
  1111. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1112. // Insert the code before the expression code
  1113. tmpBC.AddCode(&ctx->bc);
  1114. ctx->bc.AddCode(&tmpBC);
  1115. }
  1116. // Make sure the variable is not used in the expression
  1117. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1118. if( dt.IsPrimitive() )
  1119. {
  1120. ctx->type.SetVariable(dt, offset, true);
  1121. PushVariableOnStack(ctx, true);
  1122. }
  1123. else
  1124. {
  1125. // Allocate and construct the temporary object
  1126. asCByteCode tmpBC(engine);
  1127. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1128. // Insert the code before the expression code
  1129. tmpBC.AddCode(&ctx->bc);
  1130. ctx->bc.AddCode(&tmpBC);
  1131. dt.MakeReference((!dt.IsObject() || dt.IsObjectHandle()));
  1132. asCTypeInfo type;
  1133. type.Set(dt);
  1134. type.isTemporary = true;
  1135. type.stackOffset = (short)offset;
  1136. ctx->type = type;
  1137. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1138. if( dt.IsObject() && !dt.IsObjectHandle() )
  1139. ctx->bc.Instr(asBC_RDSPtr);
  1140. }
  1141. // After the function returns the temporary variable will
  1142. // be assigned to the expression, if it is a valid lvalue
  1143. }
  1144. else if( refType == asTM_INOUTREF )
  1145. {
  1146. ProcessPropertyGetAccessor(ctx, node);
  1147. // Add the type id as hidden arg if the parameter is a ? type
  1148. if( paramType->GetTokenType() == ttQuestion )
  1149. {
  1150. asCByteCode tmpBC(engine);
  1151. // Place the type id on the stack as a hidden parameter
  1152. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1153. // Insert the code before the expression code
  1154. tmpBC.AddCode(&ctx->bc);
  1155. ctx->bc.AddCode(&tmpBC);
  1156. }
  1157. // Literal constants cannot be passed to inout ref arguments
  1158. if( !ctx->type.isVariable && ctx->type.isConstant )
  1159. {
  1160. Error(TXT_NOT_VALID_REFERENCE, node);
  1161. }
  1162. // Only objects that support object handles
  1163. // can be guaranteed to be safe. Local variables are
  1164. // already safe, so there is no need to add an extra
  1165. // references
  1166. if( !engine->ep.allowUnsafeReferences &&
  1167. !ctx->type.isVariable &&
  1168. ctx->type.dataType.IsObject() &&
  1169. !ctx->type.dataType.IsObjectHandle() &&
  1170. ((ctx->type.dataType.GetBehaviour()->addref &&
  1171. ctx->type.dataType.GetBehaviour()->release) ||
  1172. (ctx->type.dataType.GetObjectType()->flags & asOBJ_NOCOUNT)) )
  1173. {
  1174. // Store a handle to the object as local variable
  1175. asSExprContext tmp(engine);
  1176. asCDataType dt = ctx->type.dataType;
  1177. dt.MakeHandle(true);
  1178. dt.MakeReference(false);
  1179. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1180. // Copy the handle
  1181. if( !ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReference() )
  1182. ctx->bc.Instr(asBC_RDSPtr);
  1183. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1184. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  1185. ctx->bc.Instr(asBC_PopPtr);
  1186. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1187. dt.MakeHandle(false);
  1188. dt.MakeReference(true);
  1189. // Release previous temporary variable stored in the context (if any)
  1190. if( ctx->type.isTemporary )
  1191. {
  1192. ReleaseTemporaryVariable(ctx->type.stackOffset, &ctx->bc);
  1193. }
  1194. ctx->type.SetVariable(dt, offset, true);
  1195. }
  1196. // Make sure the reference to the value is on the stack
  1197. // For objects, the reference needs to be dereferenced so the pointer on the stack is to the actual object
  1198. // For handles, the reference shouldn't be changed because the pointer on the stack should be to the handle
  1199. if( ctx->type.dataType.IsObject() && ctx->type.dataType.IsReference() && !paramType->IsObjectHandle() )
  1200. Dereference(ctx, true);
  1201. else if( ctx->type.isVariable && !ctx->type.dataType.IsObject() )
  1202. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  1203. else if( ctx->type.dataType.IsPrimitive() )
  1204. ctx->bc.Instr(asBC_PshRPtr);
  1205. else if( ctx->type.dataType.IsObjectHandle() && !ctx->type.dataType.IsReference() )
  1206. {
  1207. asCDataType dt = ctx->type.dataType;
  1208. dt.MakeReference(true);
  1209. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true, false);
  1210. }
  1211. }
  1212. }
  1213. else
  1214. {
  1215. ProcessPropertyGetAccessor(ctx, node);
  1216. if( dt.IsPrimitive() )
  1217. {
  1218. IsVariableInitialized(&ctx->type, node);
  1219. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1220. // Implicitly convert primitives to the parameter type
  1221. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1222. if( ctx->type.isVariable )
  1223. {
  1224. PushVariableOnStack(ctx, dt.IsReference());
  1225. }
  1226. else if( ctx->type.isConstant )
  1227. {
  1228. ConvertToVariable(ctx);
  1229. PushVariableOnStack(ctx, dt.IsReference());
  1230. }
  1231. }
  1232. else
  1233. {
  1234. IsVariableInitialized(&ctx->type, node);
  1235. // Implicitly convert primitives to the parameter type
  1236. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1237. // Was the conversion successful?
  1238. if( !ctx->type.dataType.IsEqualExceptRef(dt) )
  1239. {
  1240. asCString str;
  1241. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), dt.Format().AddressOf());
  1242. Error(str.AddressOf(), node);
  1243. ctx->type.Set(dt);
  1244. }
  1245. if( dt.IsObjectHandle() )
  1246. ctx->type.isExplicitHandle = true;
  1247. if( dt.IsObject() )
  1248. {
  1249. if( !dt.IsReference() )
  1250. {
  1251. // Objects passed by value must be placed in temporary variables
  1252. // so that they are guaranteed to not be referenced anywhere else.
  1253. // The object must also be allocated on the heap, as the memory will
  1254. // be deleted by in as_callfunc_xxx.
  1255. // TODO: value on stack: How can we avoid this unnecessary allocation?
  1256. PrepareTemporaryObject(node, ctx, true);
  1257. // The implicit conversion shouldn't convert the object to
  1258. // non-reference yet. It will be dereferenced just before the call.
  1259. // Otherwise the object might be missed by the exception handler.
  1260. dt.MakeReference(true);
  1261. }
  1262. else
  1263. {
  1264. // An object passed by reference should place the pointer to
  1265. // the object on the stack.
  1266. dt.MakeReference(false);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. // Don't put any pointer on the stack yet
  1272. if( param.IsReference() || param.IsObject() )
  1273. {
  1274. // &inout parameter may leave the reference on the stack already
  1275. if( refType != 3 )
  1276. {
  1277. asASSERT( ctx->type.isVariable || ctx->type.isTemporary || isMakingCopy );
  1278. if( ctx->type.isVariable || ctx->type.isTemporary )
  1279. {
  1280. ctx->bc.Instr(asBC_PopPtr);
  1281. ctx->bc.InstrSHORT(asBC_VAR, ctx->type.stackOffset);
  1282. ProcessDeferredParams(ctx);
  1283. }
  1284. }
  1285. }
  1286. }
  1287. void asCCompiler::PrepareFunctionCall(int funcId, asCByteCode *bc, asCArray<asSExprContext *> &args)
  1288. {
  1289. // When a match has been found, compile the final byte code using correct parameter types
  1290. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1291. // If the function being called is the opAssign or copy constructor for the same type
  1292. // as the argument, then we should avoid making temporary copy of the argument
  1293. bool makingCopy = false;
  1294. if( descr->parameterTypes.GetLength() == 1 &&
  1295. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1296. ((descr->name == "opAssign" && descr->objectType && descr->objectType == args[0]->type.dataType.GetObjectType()) ||
  1297. (args[0]->type.dataType.GetObjectType() && descr->name == args[0]->type.dataType.GetObjectType()->name)) )
  1298. makingCopy = true;
  1299. // Add code for arguments
  1300. asSExprContext e(engine);
  1301. for( int n = (int)args.GetLength()-1; n >= 0; n-- )
  1302. {
  1303. // Make sure PrepareArgument doesn't use any variable that is already
  1304. // being used by any of the following argument expressions
  1305. int l = int(reservedVariables.GetLength());
  1306. for( int m = n-1; m >= 0; m-- )
  1307. args[m]->bc.GetVarsUsed(reservedVariables);
  1308. PrepareArgument2(&e, args[n], &descr->parameterTypes[n], true, descr->inOutFlags[n], makingCopy);
  1309. reservedVariables.SetLength(l);
  1310. }
  1311. bc->AddCode(&e.bc);
  1312. }
  1313. void asCCompiler::MoveArgsToStack(int funcId, asCByteCode *bc, asCArray<asSExprContext *> &args, bool addOneToOffset)
  1314. {
  1315. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1316. int offset = 0;
  1317. if( addOneToOffset )
  1318. offset += AS_PTR_SIZE;
  1319. // The address of where the return value should be stored is push on top of the arguments
  1320. if( descr->DoesReturnOnStack() )
  1321. offset += AS_PTR_SIZE;
  1322. #ifdef AS_DEBUG
  1323. // If the function being called is the opAssign or copy constructor for the same type
  1324. // as the argument, then we should avoid making temporary copy of the argument
  1325. bool makingCopy = false;
  1326. if( descr->parameterTypes.GetLength() == 1 &&
  1327. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1328. ((descr->name == "opAssign" && descr->objectType && descr->objectType == args[0]->type.dataType.GetObjectType()) ||
  1329. (args[0]->type.dataType.GetObjectType() && descr->name == args[0]->type.dataType.GetObjectType()->name)) )
  1330. makingCopy = true;
  1331. #endif
  1332. // Move the objects that are sent by value to the stack just before the call
  1333. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  1334. {
  1335. if( descr->parameterTypes[n].IsReference() )
  1336. {
  1337. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() )
  1338. {
  1339. if( descr->inOutFlags[n] != asTM_INOUTREF )
  1340. {
  1341. #ifdef AS_DEBUG
  1342. asASSERT( args[n]->type.isVariable || args[n]->type.isTemporary || makingCopy );
  1343. #endif
  1344. if( (args[n]->type.isVariable || args[n]->type.isTemporary) )
  1345. {
  1346. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1347. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1348. // as the value allocated on the stack is guaranteed to be safe
  1349. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1350. else
  1351. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1352. }
  1353. }
  1354. if( args[n]->type.dataType.IsObjectHandle() )
  1355. bc->InstrWORD(asBC_ChkNullS, (asWORD)offset);
  1356. }
  1357. else if( descr->inOutFlags[n] != asTM_INOUTREF )
  1358. {
  1359. if( descr->parameterTypes[n].GetTokenType() == ttQuestion &&
  1360. args[n]->type.dataType.IsObject() && !args[n]->type.dataType.IsObjectHandle() )
  1361. {
  1362. // Send the object as a reference to the object,
  1363. // and not to the variable holding the object
  1364. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1365. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1366. // as the value allocated on the stack is guaranteed to be safe
  1367. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1368. else
  1369. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1370. }
  1371. else
  1372. {
  1373. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1374. }
  1375. }
  1376. }
  1377. else if( descr->parameterTypes[n].IsObject() )
  1378. {
  1379. // TODO: value on stack: What can we do to avoid this unnecessary allocation?
  1380. // The object must be allocated on the heap, because this memory will be deleted in as_callfunc_xxx
  1381. asASSERT(IsVariableOnHeap(args[n]->type.stackOffset));
  1382. bc->InstrWORD(asBC_GETOBJ, (asWORD)offset);
  1383. // The temporary variable must not be freed as it will no longer hold an object
  1384. DeallocateVariable(args[n]->type.stackOffset);
  1385. args[n]->type.isTemporary = false;
  1386. }
  1387. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  1388. }
  1389. }
  1390. int asCCompiler::CompileArgumentList(asCScriptNode *node, asCArray<asSExprContext*> &args)
  1391. {
  1392. asASSERT(node->nodeType == snArgList);
  1393. // Count arguments
  1394. asCScriptNode *arg = node->firstChild;
  1395. int argCount = 0;
  1396. while( arg )
  1397. {
  1398. argCount++;
  1399. arg = arg->next;
  1400. }
  1401. // Prepare the arrays
  1402. args.SetLength(argCount);
  1403. int n;
  1404. for( n = 0; n < argCount; n++ )
  1405. args[n] = 0;
  1406. n = argCount-1;
  1407. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1408. bool anyErrors = false;
  1409. arg = node->lastChild;
  1410. while( arg )
  1411. {
  1412. asSExprContext expr(engine);
  1413. int r = CompileAssignment(arg, &expr);
  1414. if( r < 0 ) anyErrors = true;
  1415. args[n] = asNEW(asSExprContext)(engine);
  1416. if( args[n] == 0 )
  1417. {
  1418. // Out of memory
  1419. return -1;
  1420. }
  1421. MergeExprBytecodeAndType(args[n], &expr);
  1422. n--;
  1423. arg = arg->prev;
  1424. }
  1425. return anyErrors ? -1 : 0;
  1426. }
  1427. int asCCompiler::CompileDefaultArgs(asCScriptNode *node, asCArray<asSExprContext*> &args, asCScriptFunction *func)
  1428. {
  1429. bool anyErrors = false;
  1430. asCArray<int> varsUsed;
  1431. int explicitArgs = (int)args.GetLength();
  1432. for( int p = 0; p < explicitArgs; p++ )
  1433. args[p]->bc.GetVarsUsed(varsUsed);
  1434. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1435. args.SetLength(func->parameterTypes.GetLength());
  1436. for( asUINT c = explicitArgs; c < args.GetLength(); c++ )
  1437. args[c] = 0;
  1438. for( int n = (int)func->parameterTypes.GetLength() - 1; n >= explicitArgs; n-- )
  1439. {
  1440. if( func->defaultArgs[n] == 0 ) { anyErrors = true; continue; }
  1441. // Parse the default arg string
  1442. asCParser parser(builder);
  1443. asCScriptCode code;
  1444. code.SetCode("default arg", func->defaultArgs[n]->AddressOf(), false);
  1445. int r = parser.ParseExpression(&code);
  1446. if( r < 0 ) { anyErrors = true; continue; }
  1447. asCScriptNode *arg = parser.GetScriptNode();
  1448. // Temporarily set the script code to the default arg expression
  1449. asCScriptCode *origScript = script;
  1450. script = &code;
  1451. // Don't allow the expression to access local variables
  1452. // TODO: namespace: The default arg should see the symbols declared in the same scope as the function
  1453. isCompilingDefaultArg = true;
  1454. asSExprContext expr(engine);
  1455. r = CompileExpression(arg, &expr);
  1456. isCompilingDefaultArg = false;
  1457. script = origScript;
  1458. if( r < 0 )
  1459. {
  1460. asCString msg;
  1461. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1462. Error(msg.AddressOf(), node);
  1463. anyErrors = true;
  1464. continue;
  1465. }
  1466. args[n] = asNEW(asSExprContext)(engine);
  1467. if( args[n] == 0 )
  1468. {
  1469. // Out of memory
  1470. return -1;
  1471. }
  1472. MergeExprBytecodeAndType(args[n], &expr);
  1473. // Make sure the default arg expression doesn't end up
  1474. // with a variable that is used in a previous expression
  1475. if( args[n]->type.isVariable )
  1476. {
  1477. int offset = args[n]->type.stackOffset;
  1478. if( varsUsed.Exists(offset) )
  1479. {
  1480. // Release the current temporary variable
  1481. ReleaseTemporaryVariable(args[n]->type, 0);
  1482. asCDataType dt = args[n]->type.dataType;
  1483. dt.MakeReference(false);
  1484. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(offset));
  1485. asASSERT( IsVariableOnHeap(offset) == IsVariableOnHeap(newOffset) );
  1486. args[n]->bc.ExchangeVar(offset, newOffset);
  1487. args[n]->type.stackOffset = (short)newOffset;
  1488. args[n]->type.isTemporary = true;
  1489. args[n]->type.isVariable = true;
  1490. }
  1491. }
  1492. }
  1493. return anyErrors ? -1 : 0;
  1494. }
  1495. asUINT asCCompiler::MatchFunctions(asCArray<int> &funcs, asCArray<asSExprContext*> &args, asCScriptNode *node, const char *name, asCObjectType *objectType, bool isConstMethod, bool silent, bool allowObjectConstruct, const asCString &scope)
  1496. {
  1497. asCArray<int> origFuncs = funcs; // Keep the original list for error message
  1498. asUINT cost = 0;
  1499. asUINT n;
  1500. if( funcs.GetLength() > 0 )
  1501. {
  1502. // Check the number of parameters in the found functions
  1503. for( n = 0; n < funcs.GetLength(); ++n )
  1504. {
  1505. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  1506. if( desc->parameterTypes.GetLength() != args.GetLength() )
  1507. {
  1508. bool noMatch = true;
  1509. if( args.GetLength() < desc->parameterTypes.GetLength() )
  1510. {
  1511. // Count the number of default args
  1512. asUINT defaultArgs = 0;
  1513. for( asUINT d = 0; d < desc->defaultArgs.GetLength(); d++ )
  1514. if( desc->defaultArgs[d] )
  1515. defaultArgs++;
  1516. if( args.GetLength() >= desc->parameterTypes.GetLength() - defaultArgs )
  1517. noMatch = false;
  1518. }
  1519. if( noMatch )
  1520. {
  1521. // remove it from the list
  1522. if( n == funcs.GetLength()-1 )
  1523. funcs.PopLast();
  1524. else
  1525. funcs[n] = funcs.PopLast();
  1526. n--;
  1527. }
  1528. }
  1529. }
  1530. // Match functions with the parameters, and discard those that do not match
  1531. asCArray<int> matchingFuncs = funcs;
  1532. for( n = 0; n < args.GetLength(); ++n )
  1533. {
  1534. asCArray<int> tempFuncs;
  1535. cost += MatchArgument(funcs, tempFuncs, &args[n]->type, n, allowObjectConstruct);
  1536. // Intersect the found functions with the list of matching functions
  1537. for( asUINT f = 0; f < matchingFuncs.GetLength(); f++ )
  1538. {
  1539. asUINT c;
  1540. for( c = 0; c < tempFuncs.GetLength(); c++ )
  1541. {
  1542. if( matchingFuncs[f] == tempFuncs[c] )
  1543. break;
  1544. }
  1545. // Was the function a match?
  1546. if( c == tempFuncs.GetLength() )
  1547. {
  1548. // No, remove it from the list
  1549. if( f == matchingFuncs.GetLength()-1 )
  1550. matchingFuncs.PopLast();
  1551. else
  1552. matchingFuncs[f] = matchingFuncs.PopLast();
  1553. f--;
  1554. }
  1555. }
  1556. }
  1557. funcs = matchingFuncs;
  1558. }
  1559. if( !isConstMethod )
  1560. FilterConst(funcs);
  1561. if( funcs.GetLength() != 1 && !silent )
  1562. {
  1563. // Build a readable string of the function with parameter types
  1564. asCString str;
  1565. if( scope != "" )
  1566. {
  1567. if( scope == "::" )
  1568. str = scope;
  1569. else
  1570. str = scope + "::";
  1571. }
  1572. str += name;
  1573. str += "(";
  1574. if( args.GetLength() )
  1575. str += args[0]->type.dataType.Format();
  1576. for( n = 1; n < args.GetLength(); n++ )
  1577. str += ", " + args[n]->type.dataType.Format();
  1578. str += ")";
  1579. if( isConstMethod )
  1580. str += " const";
  1581. if( objectType && scope == "" )
  1582. str = objectType->name + "::" + str;
  1583. if( funcs.GetLength() == 0 )
  1584. {
  1585. str.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  1586. Error(str.AddressOf(), node);
  1587. // Print the list of candidates
  1588. if( origFuncs.GetLength() > 0 )
  1589. {
  1590. int r = 0, c = 0;
  1591. asASSERT( node );
  1592. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1593. builder->WriteInfo(script->name.AddressOf(), TXT_CANDIDATES_ARE, r, c, false);
  1594. PrintMatchingFuncs(origFuncs, node);
  1595. }
  1596. }
  1597. else
  1598. {
  1599. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  1600. Error(str.AddressOf(), node);
  1601. PrintMatchingFuncs(funcs, node);
  1602. }
  1603. }
  1604. return cost;
  1605. }
  1606. void asCCompiler::CompileDeclaration(asCScriptNode *decl, asCByteCode *bc)
  1607. {
  1608. // Get the data type
  1609. // TODO: namespace: Use correct implicit namespace from function
  1610. asCDataType type = builder->CreateDataTypeFromNode(decl->firstChild, script, "");
  1611. // Declare all variables in this declaration
  1612. asCScriptNode *node = decl->firstChild->next;
  1613. while( node )
  1614. {
  1615. // Is the type allowed?
  1616. if( !type.CanBeInstanciated() )
  1617. {
  1618. asCString str;
  1619. // TODO: Change to "'type' cannot be declared as variable"
  1620. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format().AddressOf());
  1621. Error(str.AddressOf(), node);
  1622. // Use int instead to avoid further problems
  1623. type = asCDataType::CreatePrimitive(ttInt, false);
  1624. }
  1625. // A shared object may not declare variables of non-shared types
  1626. if( outFunc->IsShared() )
  1627. {
  1628. asCObjectType *ot = type.GetObjectType();
  1629. if( ot && !ot->IsShared() )
  1630. {
  1631. asCString msg;
  1632. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  1633. Error(msg.AddressOf(), decl);
  1634. }
  1635. }
  1636. // Get the name of the identifier
  1637. asCString name(&script->code[node->tokenPos], node->tokenLength);
  1638. // Verify that the name isn't used by a dynamic data type
  1639. if( engine->GetObjectType(name.AddressOf(), outFunc->nameSpace) != 0 )
  1640. {
  1641. asCString str;
  1642. str.Format(TXT_ILLEGAL_VARIABLE_NAME_s, name.AddressOf());
  1643. Error(str.AddressOf(), node);
  1644. }
  1645. int offset = AllocateVariable(type, false);
  1646. if( variables->DeclareVariable(name.AddressOf(), type, offset, IsVariableOnHeap(offset)) < 0 )
  1647. {
  1648. // TODO: It might be an out-of-memory too
  1649. asCString str;
  1650. str.Format(TXT_s_ALREADY_DECLARED, name.AddressOf());
  1651. Error(str.AddressOf(), node);
  1652. // Don't continue after this error, as it will just
  1653. // lead to more errors that are likely false
  1654. return;
  1655. }
  1656. // Add marker that the variable has been declared
  1657. bc->VarDecl((int)outFunc->variables.GetLength());
  1658. outFunc->AddVariable(name, type, offset);
  1659. // Keep the node for the variable decl
  1660. asCScriptNode *varNode = node;
  1661. node = node->next;
  1662. if( node && node->nodeType == snArgList )
  1663. {
  1664. // Make sure that it is a registered type, and that is isn't a pointer
  1665. if( type.GetObjectType() == 0 || type.IsObjectHandle() )
  1666. {
  1667. Error(TXT_MUST_BE_OBJECT, node);
  1668. }
  1669. else
  1670. {
  1671. // Compile the arguments
  1672. asCArray<asSExprContext *> args;
  1673. if( CompileArgumentList(node, args) >= 0 )
  1674. {
  1675. // Find all constructors
  1676. asCArray<int> funcs;
  1677. asSTypeBehaviour *beh = type.GetBehaviour();
  1678. if( beh )
  1679. {
  1680. if( type.GetObjectType()->flags & asOBJ_REF )
  1681. funcs = beh->factories;
  1682. else
  1683. funcs = beh->constructors;
  1684. }
  1685. asCString str = type.Format();
  1686. MatchFunctions(funcs, args, node, str.AddressOf());
  1687. if( funcs.GetLength() == 1 )
  1688. {
  1689. int r = asSUCCESS;
  1690. // Add the default values for arguments not explicitly supplied
  1691. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  1692. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  1693. r = CompileDefaultArgs(node, args, func);
  1694. if( r == asSUCCESS )
  1695. {
  1696. sVariable *v = variables->GetVariable(name.AddressOf());
  1697. asSExprContext ctx(engine);
  1698. if( v->type.GetObjectType() && (v->type.GetObjectType()->flags & asOBJ_REF) )
  1699. {
  1700. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, v->stackOffset);
  1701. // Pop the reference left by the function call
  1702. ctx.bc.Instr(asBC_PopPtr);
  1703. }
  1704. else
  1705. {
  1706. // When the object is allocated on the heap, the address where the
  1707. // reference will be stored must be pushed on the stack before the
  1708. // arguments. This reference on the stack is safe, even if the script
  1709. // is suspended during the evaluation of the arguments.
  1710. if( v->onHeap )
  1711. ctx.bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  1712. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  1713. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  1714. // When the object is allocated on the stack, the address to the
  1715. // object is pushed on the stack after the arguments as the object pointer
  1716. if( !v->onHeap )
  1717. ctx.bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  1718. PerformFunctionCall(funcs[0], &ctx, v->onHeap, &args, type.GetObjectType());
  1719. // TODO: value on stack: This probably has to be done in PerformFunctionCall
  1720. // Mark the object as initialized
  1721. ctx.bc.ObjInfo(v->stackOffset, asOBJ_INIT);
  1722. }
  1723. bc->AddCode(&ctx.bc);
  1724. }
  1725. }
  1726. }
  1727. // Cleanup
  1728. for( asUINT n = 0; n < args.GetLength(); n++ )
  1729. if( args[n] )
  1730. {
  1731. asDELETE(args[n],asSExprContext);
  1732. }
  1733. }
  1734. node = node->next;
  1735. }
  1736. else if( node && node->nodeType == snInitList )
  1737. {
  1738. sVariable *v = variables->GetVariable(name.AddressOf());
  1739. asCTypeInfo ti;
  1740. ti.Set(type);
  1741. ti.isVariable = true;
  1742. ti.isTemporary = false;
  1743. ti.stackOffset = (short)v->stackOffset;
  1744. ti.isLValue = true;
  1745. CompileInitList(&ti, node, bc);
  1746. node = node->next;
  1747. }
  1748. else if( node && node->nodeType == snAssignment )
  1749. {
  1750. asSExprContext ctx(engine);
  1751. // TODO: copy: Here we should look for the best matching constructor, instead of
  1752. // just the copy constructor. Only if no appropriate constructor is
  1753. // available should the assignment operator be used.
  1754. // Call the default constructor here
  1755. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), &ctx.bc, varNode);
  1756. // Compile the expression
  1757. asSExprContext expr(engine);
  1758. int r = CompileAssignment(node, &expr);
  1759. if( r >= 0 )
  1760. {
  1761. if( type.IsPrimitive() )
  1762. {
  1763. if( type.IsReadOnly() && expr.type.isConstant )
  1764. {
  1765. ImplicitConversion(&expr, type, node, asIC_IMPLICIT_CONV);
  1766. sVariable *v = variables->GetVariable(name.AddressOf());
  1767. v->isPureConstant = true;
  1768. v->constantValue = expr.type.qwordValue;
  1769. }
  1770. asSExprContext lctx(engine);
  1771. lctx.type.SetVariable(type, offset, false);
  1772. lctx.type.dataType.MakeReadOnly(false);
  1773. lctx.type.isLValue = true;
  1774. DoAssignment(&ctx, &lctx, &expr, node, node, ttAssignment, node);
  1775. ProcessDeferredParams(&ctx);
  1776. }
  1777. else
  1778. {
  1779. // TODO: runtime optimize: We can use a copy constructor here
  1780. sVariable *v = variables->GetVariable(name.AddressOf());
  1781. asSExprContext lexpr(engine);
  1782. lexpr.type.Set(type);
  1783. lexpr.type.dataType.MakeReference(v->onHeap);
  1784. // Allow initialization of constant variables
  1785. lexpr.type.dataType.MakeReadOnly(false);
  1786. if( type.IsObjectHandle() )
  1787. lexpr.type.isExplicitHandle = true;
  1788. lexpr.bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  1789. lexpr.type.stackOffset = (short)v->stackOffset;
  1790. lexpr.type.isVariable = true;
  1791. lexpr.type.isLValue = true;
  1792. // If left expression resolves into a registered type
  1793. // check if the assignment operator is overloaded, and check
  1794. // the type of the right hand expression. If none is found
  1795. // the default action is a direct copy if it is the same type
  1796. // and a simple assignment.
  1797. bool assigned = false;
  1798. // Even though an ASHANDLE can be an explicit handle the overloaded operator needs to be called
  1799. if( lexpr.type.dataType.IsObject() && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  1800. {
  1801. assigned = CompileOverloadedDualOperator(node, &lexpr, &expr, &ctx);
  1802. if( assigned )
  1803. {
  1804. // Pop the resulting value
  1805. ctx.bc.Instr(asBC_PopPtr);
  1806. // Release the argument
  1807. ProcessDeferredParams(&ctx);
  1808. // Release temporary variable that may be allocated by the overloaded operator
  1809. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  1810. }
  1811. }
  1812. if( !assigned )
  1813. {
  1814. PrepareForAssignment(&lexpr.type.dataType, &expr, node, false);
  1815. // If the expression is constant and the variable also is constant
  1816. // then mark the variable as pure constant. This will allow the compiler
  1817. // to optimize expressions with this variable.
  1818. if( v->type.IsReadOnly() && expr.type.isConstant )
  1819. {
  1820. v->isPureConstant = true;
  1821. v->constantValue = expr.type.qwordValue;
  1822. }
  1823. // Add expression code to bytecode
  1824. MergeExprBytecode(&ctx, &expr);
  1825. // Add byte code for storing value of expression in variable
  1826. ctx.bc.AddCode(&lexpr.bc);
  1827. lexpr.type.stackOffset = (short)v->stackOffset;
  1828. PerformAssignment(&lexpr.type, &expr.type, &ctx.bc, node->prev);
  1829. // Release temporary variables used by expression
  1830. ReleaseTemporaryVariable(expr.type, &ctx.bc);
  1831. ctx.bc.Instr(asBC_PopPtr);
  1832. ProcessDeferredParams(&ctx);
  1833. }
  1834. }
  1835. }
  1836. node = node->next;
  1837. bc->AddCode(&ctx.bc);
  1838. }
  1839. else
  1840. {
  1841. // Call the default constructor here if no explicit initialization is done
  1842. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), bc, varNode);
  1843. }
  1844. }
  1845. }
  1846. void asCCompiler::CompileInitList(asCTypeInfo *var, asCScriptNode *node, asCByteCode *bc)
  1847. {
  1848. // Check if the type supports initialization lists
  1849. if( var->dataType.GetObjectType() == 0 ||
  1850. var->dataType.GetBehaviour()->listFactory == 0 ||
  1851. var->dataType.IsObjectHandle() )
  1852. {
  1853. asCString str;
  1854. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, var->dataType.Format().AddressOf());
  1855. Error(str.AddressOf(), node);
  1856. return;
  1857. }
  1858. // Count the number of elements and initialize the array with the correct size
  1859. int countElements = 0;
  1860. asCScriptNode *el = node->firstChild;
  1861. while( el )
  1862. {
  1863. countElements++;
  1864. el = el->next;
  1865. }
  1866. // Construct the array with the size elements
  1867. // TODO: value on stack: This needs to support value types on the stack as well
  1868. // Find the list factory
  1869. // TODO: initlist: Add support for value types as well
  1870. int funcId = var->dataType.GetBehaviour()->listFactory;
  1871. asCArray<asSExprContext *> args;
  1872. asSExprContext arg1(engine);
  1873. arg1.bc.InstrDWORD(asBC_PshC4, countElements);
  1874. arg1.type.Set(asCDataType::CreatePrimitive(ttUInt, false));
  1875. args.PushLast(&arg1);
  1876. asSExprContext ctx(engine);
  1877. PrepareFunctionCall(funcId, &ctx.bc, args);
  1878. MoveArgsToStack(funcId, &ctx.bc, args, false);
  1879. if( var->isVariable )
  1880. {
  1881. // Call factory and store the handle in the given variable
  1882. PerformFunctionCall(funcId, &ctx, false, &args, 0, true, var->stackOffset);
  1883. ctx.bc.Instr(asBC_PopPtr);
  1884. }
  1885. else
  1886. {
  1887. PerformFunctionCall(funcId, &ctx, false, &args);
  1888. // Store the returned handle in the global variable
  1889. ctx.bc.Instr(asBC_RDSPtr);
  1890. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  1891. ctx.bc.InstrPTR(asBC_REFCPY, var->dataType.GetObjectType());
  1892. ctx.bc.Instr(asBC_PopPtr);
  1893. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  1894. }
  1895. bc->AddCode(&ctx.bc);
  1896. // TODO: initlist: Should we have a special indexing operator for this? How can we support
  1897. // initialization lists with different types for different elements? Maybe
  1898. // by using the variable arguments the initialization can be done with one
  1899. // call, passing all the elements as arguments. The registered function can
  1900. // then traverse them however it wants.
  1901. // Find the indexing operator that is not read-only that will be used for all elements
  1902. asCDataType retType;
  1903. retType = var->dataType.GetSubType();
  1904. retType.MakeReference(true);
  1905. retType.MakeReadOnly(false);
  1906. funcId = 0;
  1907. for( asUINT n = 0; n < var->dataType.GetObjectType()->methods.GetLength(); n++ )
  1908. {
  1909. asCScriptFunction *desc = builder->GetFunctionDescription(var->dataType.GetObjectType()->methods[n]);
  1910. if( !desc->isReadOnly &&
  1911. desc->parameterTypes.GetLength() == 1 &&
  1912. (desc->parameterTypes[0] == asCDataType::CreatePrimitive(ttUInt, false) ||
  1913. desc->parameterTypes[0] == asCDataType::CreatePrimitive(ttInt, false)) &&
  1914. desc->returnType == retType &&
  1915. desc->name == "opIndex" )
  1916. {
  1917. funcId = var->dataType.GetObjectType()->methods[n];
  1918. break;
  1919. }
  1920. }
  1921. if( funcId == 0 )
  1922. {
  1923. Error(TXT_NO_APPROPRIATE_INDEX_OPERATOR, node);
  1924. return;
  1925. }
  1926. asUINT index = 0;
  1927. el = node->firstChild;
  1928. while( el )
  1929. {
  1930. if( el->nodeType == snAssignment || el->nodeType == snInitList )
  1931. {
  1932. asSExprContext lctx(engine);
  1933. asSExprContext rctx(engine);
  1934. if( el->nodeType == snAssignment )
  1935. {
  1936. // Compile the assignment expression
  1937. CompileAssignment(el, &rctx);
  1938. }
  1939. else if( el->nodeType == snInitList )
  1940. {
  1941. int offset = AllocateVariable(var->dataType.GetSubType(), true);
  1942. rctx.type.Set(var->dataType.GetSubType());
  1943. rctx.type.isVariable = true;
  1944. rctx.type.isTemporary = true;
  1945. rctx.type.stackOffset = (short)offset;
  1946. CompileInitList(&rctx.type, el, &rctx.bc);
  1947. // Put the object on the stack
  1948. rctx.bc.InstrSHORT(asBC_PSF, rctx.type.stackOffset);
  1949. // It is a reference that we place on the stack
  1950. rctx.type.dataType.MakeReference(true);
  1951. }
  1952. // Compile the lvalue
  1953. lctx.bc.InstrDWORD(asBC_PshC4, index);
  1954. if( var->isVariable )
  1955. lctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  1956. else
  1957. lctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  1958. lctx.bc.Instr(asBC_RDSPtr);
  1959. lctx.bc.Call(asBC_CALLSYS, funcId, 1+AS_PTR_SIZE);
  1960. if( !var->dataType.GetSubType().IsPrimitive() )
  1961. lctx.bc.Instr(asBC_PshRPtr);
  1962. lctx.type.Set(var->dataType.GetSubType());
  1963. if( !lctx.type.dataType.IsObject() || lctx.type.dataType.IsObjectHandle() )
  1964. lctx.type.dataType.MakeReference(true);
  1965. // If the element type is handles, then we're expected to do handle assignments
  1966. if( lctx.type.dataType.IsObjectHandle() )
  1967. lctx.type.isExplicitHandle = true;
  1968. lctx.type.isLValue = true;
  1969. asSExprContext ctx(engine);
  1970. DoAssignment(&ctx, &lctx, &rctx, el, el, ttAssignment, el);
  1971. if( !lctx.type.dataType.IsPrimitive() )
  1972. ctx.bc.Instr(asBC_PopPtr);
  1973. // Release temporary variables used by expression
  1974. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  1975. ProcessDeferredParams(&ctx);
  1976. bc->AddCode(&ctx.bc);
  1977. }
  1978. el = el->next;
  1979. index++;
  1980. }
  1981. }
  1982. void asCCompiler::CompileStatement(asCScriptNode *statement, bool *hasReturn, asCByteCode *bc)
  1983. {
  1984. *hasReturn = false;
  1985. if( statement->nodeType == snStatementBlock )
  1986. CompileStatementBlock(statement, true, hasReturn, bc);
  1987. else if( statement->nodeType == snIf )
  1988. CompileIfStatement(statement, hasReturn, bc);
  1989. else if( statement->nodeType == snFor )
  1990. CompileForStatement(statement, bc);
  1991. else if( statement->nodeType == snWhile )
  1992. CompileWhileStatement(statement, bc);
  1993. else if( statement->nodeType == snDoWhile )
  1994. CompileDoWhileStatement(statement, bc);
  1995. else if( statement->nodeType == snExpressionStatement )
  1996. CompileExpressionStatement(statement, bc);
  1997. else if( statement->nodeType == snBreak )
  1998. CompileBreakStatement(statement, bc);
  1999. else if( statement->nodeType == snContinue )
  2000. CompileContinueStatement(statement, bc);
  2001. else if( statement->nodeType == snSwitch )
  2002. CompileSwitchStatement(statement, hasReturn, bc);
  2003. else if( statement->nodeType == snReturn )
  2004. {
  2005. CompileReturnStatement(statement, bc);
  2006. *hasReturn = true;
  2007. }
  2008. }
  2009. void asCCompiler::CompileSwitchStatement(asCScriptNode *snode, bool *, asCByteCode *bc)
  2010. {
  2011. // TODO: inheritance: Must guarantee that all options in the switch case call a constructor, or that none call it.
  2012. // Reserve label for break statements
  2013. int breakLabel = nextLabel++;
  2014. breakLabels.PushLast(breakLabel);
  2015. // Add a variable scope that will be used by CompileBreak
  2016. // to know where to stop deallocating variables
  2017. AddVariableScope(true, false);
  2018. //---------------------------
  2019. // Compile the switch expression
  2020. //-------------------------------
  2021. // Compile the switch expression
  2022. asSExprContext expr(engine);
  2023. CompileAssignment(snode->firstChild, &expr);
  2024. // Verify that the expression is a primitive type
  2025. if( !expr.type.dataType.IsIntegerType() && !expr.type.dataType.IsUnsignedType() && !expr.type.dataType.IsEnumType() )
  2026. {
  2027. Error(TXT_SWITCH_MUST_BE_INTEGRAL, snode->firstChild);
  2028. return;
  2029. }
  2030. ProcessPropertyGetAccessor(&expr, snode);
  2031. // TODO: Need to support 64bit integers
  2032. // Convert the expression to a 32bit variable
  2033. asCDataType to;
  2034. if( expr.type.dataType.IsIntegerType() || expr.type.dataType.IsEnumType() )
  2035. to.SetTokenType(ttInt);
  2036. else if( expr.type.dataType.IsUnsignedType() )
  2037. to.SetTokenType(ttUInt);
  2038. // Make sure the value is in a variable
  2039. if( expr.type.dataType.IsReference() )
  2040. ConvertToVariable(&expr);
  2041. ImplicitConversion(&expr, to, snode->firstChild, asIC_IMPLICIT_CONV, true);
  2042. ConvertToVariable(&expr);
  2043. int offset = expr.type.stackOffset;
  2044. ProcessDeferredParams(&expr);
  2045. //-------------------------------
  2046. // Determine case values and labels
  2047. //--------------------------------
  2048. // Remember the first label so that we can later pass the
  2049. // correct label to each CompileCase()
  2050. int firstCaseLabel = nextLabel;
  2051. int defaultLabel = 0;
  2052. asCArray<int> caseValues;
  2053. asCArray<int> caseLabels;
  2054. // Compile all case comparisons and make them jump to the right label
  2055. asCScriptNode *cnode = snode->firstChild->next;
  2056. while( cnode )
  2057. {
  2058. // Each case should have a constant expression
  2059. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  2060. {
  2061. // Compile expression
  2062. asSExprContext c(engine);
  2063. CompileExpression(cnode->firstChild, &c);
  2064. // Verify that the result is a constant
  2065. if( !c.type.isConstant )
  2066. Error(TXT_SWITCH_CASE_MUST_BE_CONSTANT, cnode->firstChild);
  2067. // Verify that the result is an integral number
  2068. if( !c.type.dataType.IsIntegerType() && !c.type.dataType.IsUnsignedType() && !c.type.dataType.IsEnumType() )
  2069. Error(TXT_SWITCH_MUST_BE_INTEGRAL, cnode->firstChild);
  2070. ImplicitConversion(&c, to, cnode->firstChild, asIC_IMPLICIT_CONV, true);
  2071. // Has this case been declared already?
  2072. if( caseValues.IndexOf(c.type.intValue) >= 0 )
  2073. {
  2074. Error(TXT_DUPLICATE_SWITCH_CASE, cnode->firstChild);
  2075. }
  2076. // TODO: Optimize: We can insert the numbers sorted already
  2077. // Store constant for later use
  2078. caseValues.PushLast(c.type.intValue);
  2079. // Reserve label for this case
  2080. caseLabels.PushLast(nextLabel++);
  2081. }
  2082. else
  2083. {
  2084. // Is default the last case?
  2085. if( cnode->next )
  2086. {
  2087. Error(TXT_DEFAULT_MUST_BE_LAST, cnode);
  2088. break;
  2089. }
  2090. // Reserve label for this case
  2091. defaultLabel = nextLabel++;
  2092. }
  2093. cnode = cnode->next;
  2094. }
  2095. // check for empty switch
  2096. if (caseValues.GetLength() == 0)
  2097. {
  2098. Error(TXT_EMPTY_SWITCH, snode);
  2099. return;
  2100. }
  2101. if( defaultLabel == 0 )
  2102. defaultLabel = breakLabel;
  2103. //---------------------------------
  2104. // Output the optimized case comparisons
  2105. // with jumps to the case code
  2106. //------------------------------------
  2107. // Sort the case values by increasing value. Do the sort together with the labels
  2108. // A simple bubble sort is sufficient since we don't expect a huge number of values
  2109. for( asUINT fwd = 1; fwd < caseValues.GetLength(); fwd++ )
  2110. {
  2111. for( int bck = fwd - 1; bck >= 0; bck-- )
  2112. {
  2113. int bckp = bck + 1;
  2114. if( caseValues[bck] > caseValues[bckp] )
  2115. {
  2116. // Swap the values in both arrays
  2117. int swap = caseValues[bckp];
  2118. caseValues[bckp] = caseValues[bck];
  2119. caseValues[bck] = swap;
  2120. swap = caseLabels[bckp];
  2121. caseLabels[bckp] = caseLabels[bck];
  2122. caseLabels[bck] = swap;
  2123. }
  2124. else
  2125. break;
  2126. }
  2127. }
  2128. // Find ranges of consecutive numbers
  2129. asCArray<int> ranges;
  2130. ranges.PushLast(0);
  2131. asUINT n;
  2132. for( n = 1; n < caseValues.GetLength(); ++n )
  2133. {
  2134. // We can join numbers that are less than 5 numbers
  2135. // apart since the output code will still be smaller
  2136. if( caseValues[n] > caseValues[n-1] + 5 )
  2137. ranges.PushLast(n);
  2138. }
  2139. // If the value is larger than the largest case value, jump to default
  2140. int tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2141. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[caseValues.GetLength()-1]);
  2142. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2143. expr.bc.InstrDWORD(asBC_JP, defaultLabel);
  2144. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2145. // TODO: runtime optimize: We could possibly optimize this even more by doing a
  2146. // binary search instead of a linear search through the ranges
  2147. // For each range
  2148. int range;
  2149. for( range = 0; range < (int)ranges.GetLength(); range++ )
  2150. {
  2151. // Find the largest value in this range
  2152. int maxRange = caseValues[ranges[range]];
  2153. int index = ranges[range];
  2154. for( ; (index < (int)caseValues.GetLength()) && (caseValues[index] <= maxRange + 5); index++ )
  2155. maxRange = caseValues[index];
  2156. // If there are only 2 numbers then it is better to compare them directly
  2157. if( index - ranges[range] > 2 )
  2158. {
  2159. // If the value is smaller than the smallest case value in the range, jump to default
  2160. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2161. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  2162. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2163. expr.bc.InstrDWORD(asBC_JS, defaultLabel);
  2164. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2165. int nextRangeLabel = nextLabel++;
  2166. // If this is the last range we don't have to make this test
  2167. if( range < (int)ranges.GetLength() - 1 )
  2168. {
  2169. // If the value is larger than the largest case value in the range, jump to the next range
  2170. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2171. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, maxRange);
  2172. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2173. expr.bc.InstrDWORD(asBC_JP, nextRangeLabel);
  2174. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2175. }
  2176. // Jump forward according to the value
  2177. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2178. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  2179. expr.bc.InstrW_W_W(asBC_SUBi, tmpOffset, offset, tmpOffset);
  2180. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2181. expr.bc.JmpP(tmpOffset, maxRange - caseValues[ranges[range]]);
  2182. // Add the list of jumps to the correct labels (any holes, jump to default)
  2183. index = ranges[range];
  2184. for( int n = caseValues[index]; n <= maxRange; n++ )
  2185. {
  2186. if( caseValues[index] == n )
  2187. expr.bc.InstrINT(asBC_JMP, caseLabels[index++]);
  2188. else
  2189. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  2190. }
  2191. expr.bc.Label((short)nextRangeLabel);
  2192. }
  2193. else
  2194. {
  2195. // Simply make a comparison with each value
  2196. int n;
  2197. for( n = ranges[range]; n < index; ++n )
  2198. {
  2199. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2200. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[n]);
  2201. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2202. expr.bc.InstrDWORD(asBC_JZ, caseLabels[n]);
  2203. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2204. }
  2205. }
  2206. }
  2207. // Catch any value that falls trough
  2208. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  2209. // Release the temporary variable previously stored
  2210. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2211. //----------------------------------
  2212. // Output case implementations
  2213. //----------------------------------
  2214. // Compile case implementations, each one with the label before it
  2215. cnode = snode->firstChild->next;
  2216. while( cnode )
  2217. {
  2218. // Each case should have a constant expression
  2219. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  2220. {
  2221. expr.bc.Label((short)firstCaseLabel++);
  2222. CompileCase(cnode->firstChild->next, &expr.bc);
  2223. }
  2224. else
  2225. {
  2226. expr.bc.Label((short)defaultLabel);
  2227. // Is default the last case?
  2228. if( cnode->next )
  2229. {
  2230. // We've already reported this error
  2231. break;
  2232. }
  2233. CompileCase(cnode->firstChild, &expr.bc);
  2234. }
  2235. cnode = cnode->next;
  2236. }
  2237. //--------------------------------
  2238. bc->AddCode(&expr.bc);
  2239. // Add break label
  2240. bc->Label((short)breakLabel);
  2241. breakLabels.PopLast();
  2242. RemoveVariableScope();
  2243. }
  2244. void asCCompiler::CompileCase(asCScriptNode *node, asCByteCode *bc)
  2245. {
  2246. bool isFinished = false;
  2247. bool hasReturn = false;
  2248. while( node )
  2249. {
  2250. if( hasReturn || isFinished )
  2251. {
  2252. Warning(TXT_UNREACHABLE_CODE, node);
  2253. break;
  2254. }
  2255. if( node->nodeType == snBreak || node->nodeType == snContinue )
  2256. isFinished = true;
  2257. asCByteCode statement(engine);
  2258. if( node->nodeType == snDeclaration )
  2259. {
  2260. Error(TXT_DECL_IN_SWITCH, node);
  2261. // Compile it anyway to avoid further compiler errors
  2262. CompileDeclaration(node, &statement);
  2263. }
  2264. else
  2265. CompileStatement(node, &hasReturn, &statement);
  2266. LineInstr(bc, node->tokenPos);
  2267. bc->AddCode(&statement);
  2268. if( !hasCompileErrors )
  2269. asASSERT( tempVariables.GetLength() == 0 );
  2270. node = node->next;
  2271. }
  2272. }
  2273. void asCCompiler::CompileIfStatement(asCScriptNode *inode, bool *hasReturn, asCByteCode *bc)
  2274. {
  2275. // We will use one label for the if statement
  2276. // and possibly another for the else statement
  2277. int afterLabel = nextLabel++;
  2278. // Compile the expression
  2279. asSExprContext expr(engine);
  2280. CompileAssignment(inode->firstChild, &expr);
  2281. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2282. {
  2283. Error(TXT_EXPR_MUST_BE_BOOL, inode->firstChild);
  2284. expr.type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 1);
  2285. }
  2286. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2287. ProcessDeferredParams(&expr);
  2288. if( !expr.type.isConstant )
  2289. {
  2290. ProcessPropertyGetAccessor(&expr, inode);
  2291. ConvertToVariable(&expr);
  2292. // Add byte code from the expression
  2293. bc->AddCode(&expr.bc);
  2294. // Add a test
  2295. bc->InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2296. bc->Instr(asBC_ClrHi);
  2297. bc->InstrDWORD(asBC_JZ, afterLabel);
  2298. ReleaseTemporaryVariable(expr.type, bc);
  2299. }
  2300. else if( expr.type.dwordValue == 0 )
  2301. {
  2302. // Jump to the else case
  2303. bc->InstrINT(asBC_JMP, afterLabel);
  2304. // TODO: Should we warn that the expression will always go to the else?
  2305. }
  2306. // Compile the if statement
  2307. bool origIsConstructorCalled = m_isConstructorCalled;
  2308. bool hasReturn1;
  2309. asCByteCode ifBC(engine);
  2310. CompileStatement(inode->firstChild->next, &hasReturn1, &ifBC);
  2311. // Add the byte code
  2312. LineInstr(bc, inode->firstChild->next->tokenPos);
  2313. bc->AddCode(&ifBC);
  2314. if( inode->firstChild->next->nodeType == snExpressionStatement && inode->firstChild->next->firstChild == 0 )
  2315. {
  2316. // Don't allow if( expr );
  2317. Error(TXT_IF_WITH_EMPTY_STATEMENT, inode->firstChild->next);
  2318. }
  2319. // If one of the statements call the constructor, the other must as well
  2320. // otherwise it is possible the constructor is never called
  2321. bool constructorCall1 = false;
  2322. bool constructorCall2 = false;
  2323. if( !origIsConstructorCalled && m_isConstructorCalled )
  2324. constructorCall1 = true;
  2325. // Do we have an else statement?
  2326. if( inode->firstChild->next != inode->lastChild )
  2327. {
  2328. // Reset the constructor called flag so the else statement can call the constructor too
  2329. m_isConstructorCalled = origIsConstructorCalled;
  2330. int afterElse = 0;
  2331. if( !hasReturn1 )
  2332. {
  2333. afterElse = nextLabel++;
  2334. // Add jump to after the else statement
  2335. bc->InstrINT(asBC_JMP, afterElse);
  2336. }
  2337. // Add label for the else statement
  2338. bc->Label((short)afterLabel);
  2339. bool hasReturn2;
  2340. asCByteCode elseBC(engine);
  2341. CompileStatement(inode->lastChild, &hasReturn2, &elseBC);
  2342. // Add byte code for the else statement
  2343. LineInstr(bc, inode->lastChild->tokenPos);
  2344. bc->AddCode(&elseBC);
  2345. if( inode->lastChild->nodeType == snExpressionStatement && inode->lastChild->firstChild == 0 )
  2346. {
  2347. // Don't allow if( expr ) {} else;
  2348. Error(TXT_ELSE_WITH_EMPTY_STATEMENT, inode->lastChild);
  2349. }
  2350. if( !hasReturn1 )
  2351. {
  2352. // Add label for the end of else statement
  2353. bc->Label((short)afterElse);
  2354. }
  2355. // The if statement only has return if both alternatives have
  2356. *hasReturn = hasReturn1 && hasReturn2;
  2357. if( !origIsConstructorCalled && m_isConstructorCalled )
  2358. constructorCall2 = true;
  2359. }
  2360. else
  2361. {
  2362. // Add label for the end of if statement
  2363. bc->Label((short)afterLabel);
  2364. *hasReturn = false;
  2365. }
  2366. // Make sure both or neither conditions call a constructor
  2367. if( (constructorCall1 && !constructorCall2) ||
  2368. (constructorCall2 && !constructorCall1) )
  2369. {
  2370. Error(TXT_BOTH_CONDITIONS_MUST_CALL_CONSTRUCTOR, inode);
  2371. }
  2372. m_isConstructorCalled = origIsConstructorCalled || constructorCall1 || constructorCall2;
  2373. }
  2374. void asCCompiler::CompileForStatement(asCScriptNode *fnode, asCByteCode *bc)
  2375. {
  2376. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2377. AddVariableScope(true, true);
  2378. // We will use three labels for the for loop
  2379. int conditionLabel = nextLabel++;
  2380. int afterLabel = nextLabel++;
  2381. int continueLabel = nextLabel++;
  2382. int insideLabel = nextLabel++;
  2383. continueLabels.PushLast(continueLabel);
  2384. breakLabels.PushLast(afterLabel);
  2385. //---------------------------------------
  2386. // Compile the initialization statement
  2387. asCByteCode initBC(engine);
  2388. if( fnode->firstChild->nodeType == snDeclaration )
  2389. CompileDeclaration(fnode->firstChild, &initBC);
  2390. else
  2391. CompileExpressionStatement(fnode->firstChild, &initBC);
  2392. //-----------------------------------
  2393. // Compile the condition statement
  2394. asSExprContext expr(engine);
  2395. asCScriptNode *second = fnode->firstChild->next;
  2396. if( second->firstChild )
  2397. {
  2398. int r = CompileAssignment(second->firstChild, &expr);
  2399. if( r >= 0 )
  2400. {
  2401. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2402. Error(TXT_EXPR_MUST_BE_BOOL, second);
  2403. else
  2404. {
  2405. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2406. ProcessDeferredParams(&expr);
  2407. ProcessPropertyGetAccessor(&expr, second);
  2408. // If expression is false exit the loop
  2409. ConvertToVariable(&expr);
  2410. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2411. expr.bc.Instr(asBC_ClrHi);
  2412. expr.bc.InstrDWORD(asBC_JNZ, insideLabel);
  2413. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2414. }
  2415. }
  2416. }
  2417. //---------------------------
  2418. // Compile the increment statement
  2419. asCByteCode nextBC(engine);
  2420. asCScriptNode *third = second->next;
  2421. if( third->nodeType == snExpressionStatement )
  2422. CompileExpressionStatement(third, &nextBC);
  2423. //------------------------------
  2424. // Compile loop statement
  2425. bool hasReturn;
  2426. asCByteCode forBC(engine);
  2427. CompileStatement(fnode->lastChild, &hasReturn, &forBC);
  2428. //-------------------------------
  2429. // Join the code pieces
  2430. bc->AddCode(&initBC);
  2431. bc->InstrDWORD(asBC_JMP, conditionLabel);
  2432. bc->Label((short)insideLabel);
  2433. // Add a suspend bytecode inside the loop to guarantee
  2434. // that the application can suspend the execution
  2435. bc->Instr(asBC_SUSPEND);
  2436. bc->InstrPTR(asBC_JitEntry, 0);
  2437. LineInstr(bc, fnode->lastChild->tokenPos);
  2438. bc->AddCode(&forBC);
  2439. bc->Label((short)continueLabel);
  2440. bc->AddCode(&nextBC);
  2441. bc->Label((short)conditionLabel);
  2442. if( expr.bc.GetLastInstr() == -1 )
  2443. // There is no condition, so we just always jump
  2444. bc->InstrDWORD(asBC_JMP, insideLabel);
  2445. else
  2446. bc->AddCode(&expr.bc);
  2447. bc->Label((short)afterLabel);
  2448. continueLabels.PopLast();
  2449. breakLabels.PopLast();
  2450. // Deallocate variables in this block, in reverse order
  2451. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  2452. {
  2453. sVariable *v = variables->variables[n];
  2454. // Call variable destructors here, for variables not yet destroyed
  2455. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  2456. // Don't deallocate function parameters
  2457. if( v->stackOffset > 0 )
  2458. DeallocateVariable(v->stackOffset);
  2459. }
  2460. RemoveVariableScope();
  2461. }
  2462. void asCCompiler::CompileWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  2463. {
  2464. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2465. AddVariableScope(true, true);
  2466. // We will use two labels for the while loop
  2467. int beforeLabel = nextLabel++;
  2468. int afterLabel = nextLabel++;
  2469. continueLabels.PushLast(beforeLabel);
  2470. breakLabels.PushLast(afterLabel);
  2471. // Add label before the expression
  2472. bc->Label((short)beforeLabel);
  2473. // Compile expression
  2474. asSExprContext expr(engine);
  2475. CompileAssignment(wnode->firstChild, &expr);
  2476. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2477. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  2478. else
  2479. {
  2480. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2481. ProcessDeferredParams(&expr);
  2482. ProcessPropertyGetAccessor(&expr, wnode);
  2483. // Add byte code for the expression
  2484. ConvertToVariable(&expr);
  2485. bc->AddCode(&expr.bc);
  2486. // Jump to end of statement if expression is false
  2487. bc->InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2488. bc->Instr(asBC_ClrHi);
  2489. bc->InstrDWORD(asBC_JZ, afterLabel);
  2490. ReleaseTemporaryVariable(expr.type, bc);
  2491. }
  2492. // Add a suspend bytecode inside the loop to guarantee
  2493. // that the application can suspend the execution
  2494. bc->Instr(asBC_SUSPEND);
  2495. bc->InstrPTR(asBC_JitEntry, 0);
  2496. // Compile statement
  2497. bool hasReturn;
  2498. asCByteCode whileBC(engine);
  2499. CompileStatement(wnode->lastChild, &hasReturn, &whileBC);
  2500. // Add byte code for the statement
  2501. LineInstr(bc, wnode->lastChild->tokenPos);
  2502. bc->AddCode(&whileBC);
  2503. // Jump to the expression
  2504. bc->InstrINT(asBC_JMP, beforeLabel);
  2505. // Add label after the statement
  2506. bc->Label((short)afterLabel);
  2507. continueLabels.PopLast();
  2508. breakLabels.PopLast();
  2509. RemoveVariableScope();
  2510. }
  2511. void asCCompiler::CompileDoWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  2512. {
  2513. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2514. AddVariableScope(true, true);
  2515. // We will use two labels for the while loop
  2516. int beforeLabel = nextLabel++;
  2517. int beforeTest = nextLabel++;
  2518. int afterLabel = nextLabel++;
  2519. continueLabels.PushLast(beforeTest);
  2520. breakLabels.PushLast(afterLabel);
  2521. // Add label before the statement
  2522. bc->Label((short)beforeLabel);
  2523. // Compile statement
  2524. bool hasReturn;
  2525. asCByteCode whileBC(engine);
  2526. CompileStatement(wnode->firstChild, &hasReturn, &whileBC);
  2527. // Add byte code for the statement
  2528. LineInstr(bc, wnode->firstChild->tokenPos);
  2529. bc->AddCode(&whileBC);
  2530. // Add label before the expression
  2531. bc->Label((short)beforeTest);
  2532. // Add a suspend bytecode inside the loop to guarantee
  2533. // that the application can suspend the execution
  2534. bc->Instr(asBC_SUSPEND);
  2535. bc->InstrPTR(asBC_JitEntry, 0);
  2536. // Add a line instruction
  2537. LineInstr(bc, wnode->lastChild->tokenPos);
  2538. // Compile expression
  2539. asSExprContext expr(engine);
  2540. CompileAssignment(wnode->lastChild, &expr);
  2541. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2542. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  2543. else
  2544. {
  2545. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2546. ProcessDeferredParams(&expr);
  2547. ProcessPropertyGetAccessor(&expr, wnode);
  2548. // Add byte code for the expression
  2549. ConvertToVariable(&expr);
  2550. bc->AddCode(&expr.bc);
  2551. // Jump to next iteration if expression is true
  2552. bc->InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2553. bc->Instr(asBC_ClrHi);
  2554. bc->InstrDWORD(asBC_JNZ, beforeLabel);
  2555. ReleaseTemporaryVariable(expr.type, bc);
  2556. }
  2557. // Add label after the statement
  2558. bc->Label((short)afterLabel);
  2559. continueLabels.PopLast();
  2560. breakLabels.PopLast();
  2561. RemoveVariableScope();
  2562. }
  2563. void asCCompiler::CompileBreakStatement(asCScriptNode *node, asCByteCode *bc)
  2564. {
  2565. if( breakLabels.GetLength() == 0 )
  2566. {
  2567. Error(TXT_INVALID_BREAK, node);
  2568. return;
  2569. }
  2570. // Add destructor calls for all variables that will go out of scope
  2571. // Put this clean up in a block to allow exception handler to understand them
  2572. bc->Block(true);
  2573. asCVariableScope *vs = variables;
  2574. while( !vs->isBreakScope )
  2575. {
  2576. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  2577. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  2578. vs = vs->parent;
  2579. }
  2580. bc->Block(false);
  2581. bc->InstrINT(asBC_JMP, breakLabels[breakLabels.GetLength()-1]);
  2582. }
  2583. void asCCompiler::CompileContinueStatement(asCScriptNode *node, asCByteCode *bc)
  2584. {
  2585. if( continueLabels.GetLength() == 0 )
  2586. {
  2587. Error(TXT_INVALID_CONTINUE, node);
  2588. return;
  2589. }
  2590. // Add destructor calls for all variables that will go out of scope
  2591. // Put this clean up in a block to allow exception handler to understand them
  2592. bc->Block(true);
  2593. asCVariableScope *vs = variables;
  2594. while( !vs->isContinueScope )
  2595. {
  2596. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  2597. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  2598. vs = vs->parent;
  2599. }
  2600. bc->Block(false);
  2601. bc->InstrINT(asBC_JMP, continueLabels[continueLabels.GetLength()-1]);
  2602. }
  2603. void asCCompiler::CompileExpressionStatement(asCScriptNode *enode, asCByteCode *bc)
  2604. {
  2605. if( enode->firstChild )
  2606. {
  2607. // Compile the expression
  2608. asSExprContext expr(engine);
  2609. CompileAssignment(enode->firstChild, &expr);
  2610. // If we get here and there is still an unprocessed property
  2611. // accessor, then process it as a get access. Don't call if there is
  2612. // already a compile error, or we might report an error that is not valid
  2613. if( !hasCompileErrors )
  2614. ProcessPropertyGetAccessor(&expr, enode);
  2615. // Pop the value from the stack
  2616. if( !expr.type.dataType.IsPrimitive() )
  2617. expr.bc.Instr(asBC_PopPtr);
  2618. // Release temporary variables used by expression
  2619. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2620. ProcessDeferredParams(&expr);
  2621. bc->AddCode(&expr.bc);
  2622. }
  2623. }
  2624. void asCCompiler::PrepareTemporaryObject(asCScriptNode *node, asSExprContext *ctx, bool forceOnHeap)
  2625. {
  2626. // If the object already is stored in temporary variable then nothing needs to be done
  2627. // Note, a type can be temporary without being a variable, in which case it is holding off
  2628. // on releasing a previously used object.
  2629. if( ctx->type.isTemporary && ctx->type.isVariable &&
  2630. !(forceOnHeap && !IsVariableOnHeap(ctx->type.stackOffset)) )
  2631. {
  2632. // If the temporary object is currently not a reference
  2633. // the expression needs to be reevaluated to a reference
  2634. if( !ctx->type.dataType.IsReference() )
  2635. {
  2636. ctx->bc.Instr(asBC_PopPtr);
  2637. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  2638. ctx->type.dataType.MakeReference(true);
  2639. }
  2640. return;
  2641. }
  2642. // Allocate temporary variable
  2643. asCDataType dt = ctx->type.dataType;
  2644. dt.MakeReference(false);
  2645. dt.MakeReadOnly(false);
  2646. int offset = AllocateVariable(dt, true, forceOnHeap);
  2647. // Objects stored on the stack are not considered references
  2648. dt.MakeReference(IsVariableOnHeap(offset));
  2649. asCTypeInfo lvalue;
  2650. lvalue.Set(dt);
  2651. lvalue.isTemporary = true;
  2652. lvalue.stackOffset = (short)offset;
  2653. lvalue.isVariable = true;
  2654. lvalue.isExplicitHandle = ctx->type.isExplicitHandle;
  2655. if( !dt.IsObjectHandle() &&
  2656. dt.GetObjectType() && (dt.GetBehaviour()->copyconstruct || dt.GetBehaviour()->copyfactory) )
  2657. {
  2658. PrepareForAssignment(&lvalue.dataType, ctx, node, true);
  2659. // Use the copy constructor/factory when available
  2660. CallCopyConstructor(dt, offset, IsVariableOnHeap(offset), &ctx->bc, ctx, node);
  2661. }
  2662. else
  2663. {
  2664. // Allocate and construct the temporary object
  2665. int r = CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &ctx->bc, node);
  2666. if( r < 0 )
  2667. {
  2668. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  2669. }
  2670. else
  2671. {
  2672. // Assign the object to the temporary variable
  2673. PrepareForAssignment(&lvalue.dataType, ctx, node, true);
  2674. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  2675. r = PerformAssignment(&lvalue, &ctx->type, &ctx->bc, node);
  2676. if( r < 0 )
  2677. {
  2678. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  2679. }
  2680. // Pop the original reference
  2681. ctx->bc.Instr(asBC_PopPtr);
  2682. }
  2683. }
  2684. // If the expression was holding off on releasing a
  2685. // previously used object, we need to release it now
  2686. if( ctx->type.isTemporary )
  2687. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  2688. // Push the reference to the temporary variable on the stack
  2689. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  2690. lvalue.dataType.MakeReference(IsVariableOnHeap(offset));
  2691. ctx->type = lvalue;
  2692. }
  2693. void asCCompiler::CompileReturnStatement(asCScriptNode *rnode, asCByteCode *bc)
  2694. {
  2695. // Get return type and location
  2696. sVariable *v = variables->GetVariable("return");
  2697. // Basic validations
  2698. if( v->type.GetSizeOnStackDWords() > 0 && !rnode->firstChild )
  2699. {
  2700. Error(TXT_MUST_RETURN_VALUE, rnode);
  2701. return;
  2702. }
  2703. else if( v->type.GetSizeOnStackDWords() == 0 && rnode->firstChild )
  2704. {
  2705. Error(TXT_CANT_RETURN_VALUE, rnode);
  2706. return;
  2707. }
  2708. // Compile the expression
  2709. if( rnode->firstChild )
  2710. {
  2711. // Compile the expression
  2712. asSExprContext expr(engine);
  2713. int r = CompileAssignment(rnode->firstChild, &expr);
  2714. if( r < 0 ) return;
  2715. if( v->type.IsReference() )
  2716. {
  2717. // The expression that gives the reference must not use any of the
  2718. // variables that must be destroyed upon exit, because then it means
  2719. // reference will stay alive while the clean-up is done, which could
  2720. // potentially mean that the reference is invalidated by the clean-up.
  2721. //
  2722. // When the function is returning a reference, the clean-up of the
  2723. // variables must be done before the evaluation of the expression.
  2724. //
  2725. // A reference to a global variable, or a class member for class methods
  2726. // should be allowed to be returned.
  2727. if( !(expr.type.dataType.IsReference() ||
  2728. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle())) )
  2729. {
  2730. // Clean up the potential deferred parameters
  2731. ProcessDeferredParams(&expr);
  2732. Error(TXT_NOT_VALID_REFERENCE, rnode);
  2733. return;
  2734. }
  2735. // No references to local variables, temporary variables, or parameters
  2736. // are allowed to be returned, since they go out of scope when the function
  2737. // returns. Even reference parameters are disallowed, since it is not possible
  2738. // to know the scope of them. The exception is the 'this' pointer, which
  2739. // is treated by the compiler as a local variable, but isn't really so.
  2740. if( (expr.type.isVariable && !(expr.type.stackOffset == 0 && outFunc->objectType)) || expr.type.isTemporary )
  2741. {
  2742. // Clean up the potential deferred parameters
  2743. ProcessDeferredParams(&expr);
  2744. Error(TXT_CANNOT_RETURN_REF_TO_LOCAL, rnode);
  2745. return;
  2746. }
  2747. // The type must match exactly as we cannot convert
  2748. // the reference without loosing the original value
  2749. if( !(v->type == expr.type.dataType ||
  2750. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle() && v->type.IsEqualExceptRef(expr.type.dataType))) )
  2751. {
  2752. // Clean up the potential deferred parameters
  2753. ProcessDeferredParams(&expr);
  2754. asCString str;
  2755. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  2756. Error(str.AddressOf(), rnode);
  2757. return;
  2758. }
  2759. // The expression must not have any deferred expressions, because the evaluation
  2760. // of these cannot be done without keeping the reference which is not safe
  2761. if( expr.deferredParams.GetLength() )
  2762. {
  2763. // Clean up the potential deferred parameters
  2764. ProcessDeferredParams(&expr);
  2765. Error(TXT_REF_CANT_BE_RETURNED_DEFERRED_PARAM, rnode);
  2766. return;
  2767. }
  2768. // Make sure the expression isn't using any local variables that
  2769. // will need to be cleaned up before the function completes
  2770. asCArray<int> usedVars;
  2771. expr.bc.GetVarsUsed(usedVars);
  2772. for( asUINT n = 0; n < usedVars.GetLength(); n++ )
  2773. {
  2774. int var = GetVariableSlot(usedVars[n]);
  2775. if( var != -1 )
  2776. {
  2777. asCDataType dt = variableAllocations[var];
  2778. if( dt.IsObject() )
  2779. {
  2780. ProcessDeferredParams(&expr);
  2781. Error(TXT_REF_CANT_BE_RETURNED_LOCAL_VARS, rnode);
  2782. return;
  2783. }
  2784. }
  2785. }
  2786. // All objects in the function must be cleaned up before the expression
  2787. // is evaluated, otherwise there is a possibility that the cleanup will
  2788. // invalidate the reference.
  2789. // Destroy the local variables before loading
  2790. // the reference into the register. This will
  2791. // be done before the expression is evaluated.
  2792. DestroyVariables(bc);
  2793. // For primitives the reference is already in the register,
  2794. // but for non-primitives the reference is on the stack so we
  2795. // need to load it into the register
  2796. if( !expr.type.dataType.IsPrimitive() )
  2797. {
  2798. if( !expr.type.dataType.IsObjectHandle() &&
  2799. expr.type.dataType.IsReference() )
  2800. expr.bc.Instr(asBC_RDSPtr);
  2801. expr.bc.Instr(asBC_PopRPtr);
  2802. }
  2803. // There are no temporaries to release so we're done
  2804. }
  2805. else // if( !v->type.IsReference() )
  2806. {
  2807. ProcessPropertyGetAccessor(&expr, rnode);
  2808. // Prepare the value for assignment
  2809. IsVariableInitialized(&expr.type, rnode->firstChild);
  2810. if( v->type.IsPrimitive() )
  2811. {
  2812. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2813. // Implicitly convert the value to the return type
  2814. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  2815. // Verify that the conversion was successful
  2816. if( expr.type.dataType != v->type )
  2817. {
  2818. asCString str;
  2819. str.Format(TXT_NO_CONVERSION_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  2820. Error(str.AddressOf(), rnode);
  2821. return;
  2822. }
  2823. else
  2824. {
  2825. ConvertToVariable(&expr);
  2826. // Clean up the local variables and process deferred parameters
  2827. DestroyVariables(&expr.bc);
  2828. ProcessDeferredParams(&expr);
  2829. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2830. // Load the variable in the register
  2831. if( v->type.GetSizeOnStackDWords() == 1 )
  2832. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2833. else
  2834. expr.bc.InstrSHORT(asBC_CpyVtoR8, expr.type.stackOffset);
  2835. }
  2836. }
  2837. else if( v->type.IsObject() )
  2838. {
  2839. // Value types are returned on the stack, in a location
  2840. // that has been reserved by the calling function.
  2841. if( outFunc->DoesReturnOnStack() )
  2842. {
  2843. // TODO: runtime optimize: If the return type has a constructor that takes the type of the expression,
  2844. // it should be called directly instead of first converting the expression and
  2845. // then copy the value.
  2846. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  2847. {
  2848. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  2849. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  2850. {
  2851. asCString str;
  2852. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  2853. Error(str.AddressOf(), rnode->firstChild);
  2854. return;
  2855. }
  2856. }
  2857. int offset = outFunc->objectType ? -AS_PTR_SIZE : 0;
  2858. if( v->type.GetObjectType()->beh.copyconstruct )
  2859. {
  2860. PrepareForAssignment(&v->type, &expr, rnode->firstChild, false);
  2861. CallCopyConstructor(v->type, offset, false, &expr.bc, &expr, rnode->firstChild, false, true);
  2862. }
  2863. else
  2864. {
  2865. // If the copy constructor doesn't exist, then a manual assignment needs to be done instead.
  2866. CallDefaultConstructor(v->type, offset, false, &expr.bc, rnode->firstChild, false, true);
  2867. PrepareForAssignment(&v->type, &expr, rnode->firstChild, false);
  2868. expr.bc.InstrSHORT(asBC_PSF, (short)offset);
  2869. expr.bc.Instr(asBC_RDSPtr);
  2870. asSExprContext lexpr(engine);
  2871. lexpr.type.Set(v->type);
  2872. lexpr.type.isLValue = true;
  2873. PerformAssignment(&lexpr.type, &expr.type, &expr.bc, rnode->firstChild);
  2874. expr.bc.Instr(asBC_PopPtr);
  2875. // Release any temporary variable
  2876. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2877. }
  2878. // Clean up the local variables and process deferred parameters
  2879. DestroyVariables(&expr.bc);
  2880. ProcessDeferredParams(&expr);
  2881. }
  2882. else
  2883. {
  2884. asASSERT( v->type.GetObjectType()->flags & asOBJ_REF );
  2885. // Prepare the expression to be loaded into the object
  2886. // register. This will place the reference in local variable
  2887. PrepareArgument(&v->type, &expr, rnode->firstChild, false, 0);
  2888. // Pop the reference to the temporary variable
  2889. expr.bc.Instr(asBC_PopPtr);
  2890. // Clean up the local variables and process deferred parameters
  2891. DestroyVariables(&expr.bc);
  2892. ProcessDeferredParams(&expr);
  2893. // Load the object pointer into the object register
  2894. // LOADOBJ also clears the address in the variable
  2895. expr.bc.InstrSHORT(asBC_LOADOBJ, expr.type.stackOffset);
  2896. // LOADOBJ cleared the address in the variable so the object will not be freed
  2897. // here, but the temporary variable must still be freed so the slot can be reused
  2898. // By releasing without the bytecode we do just that.
  2899. ReleaseTemporaryVariable(expr.type, 0);
  2900. }
  2901. }
  2902. }
  2903. bc->AddCode(&expr.bc);
  2904. }
  2905. else
  2906. {
  2907. // For functions that don't return anything
  2908. // we just detroy the local variables
  2909. DestroyVariables(bc);
  2910. }
  2911. // Jump to the end of the function
  2912. bc->InstrINT(asBC_JMP, 0);
  2913. }
  2914. void asCCompiler::DestroyVariables(asCByteCode *bc)
  2915. {
  2916. // Call destructor on all variables except for the function parameters
  2917. // Put the clean-up in a block to allow exception handler to understand this
  2918. bc->Block(true);
  2919. asCVariableScope *vs = variables;
  2920. while( vs )
  2921. {
  2922. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  2923. if( vs->variables[n]->stackOffset > 0 )
  2924. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  2925. vs = vs->parent;
  2926. }
  2927. bc->Block(false);
  2928. }
  2929. void asCCompiler::AddVariableScope(bool isBreakScope, bool isContinueScope)
  2930. {
  2931. variables = asNEW(asCVariableScope)(variables);
  2932. if( variables == 0 )
  2933. {
  2934. // Out of memory
  2935. return;
  2936. }
  2937. variables->isBreakScope = isBreakScope;
  2938. variables->isContinueScope = isContinueScope;
  2939. }
  2940. void asCCompiler::RemoveVariableScope()
  2941. {
  2942. if( variables )
  2943. {
  2944. asCVariableScope *var = variables;
  2945. variables = variables->parent;
  2946. asDELETE(var,asCVariableScope);
  2947. }
  2948. }
  2949. void asCCompiler::Error(const char *msg, asCScriptNode *node)
  2950. {
  2951. asCString str;
  2952. int r = 0, c = 0;
  2953. asASSERT( node );
  2954. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2955. builder->WriteError(script->name.AddressOf(), msg, r, c);
  2956. hasCompileErrors = true;
  2957. }
  2958. void asCCompiler::Warning(const char *msg, asCScriptNode *node)
  2959. {
  2960. asCString str;
  2961. int r = 0, c = 0;
  2962. asASSERT( node );
  2963. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2964. builder->WriteWarning(script->name.AddressOf(), msg, r, c);
  2965. }
  2966. void asCCompiler::Information(const char *msg, asCScriptNode *node)
  2967. {
  2968. asCString str;
  2969. int r = 0, c = 0;
  2970. asASSERT( node );
  2971. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2972. builder->WriteInfo(script->name.AddressOf(), msg, r, c, false);
  2973. }
  2974. void asCCompiler::PrintMatchingFuncs(asCArray<int> &funcs, asCScriptNode *node)
  2975. {
  2976. int r = 0, c = 0;
  2977. asASSERT( node );
  2978. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2979. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  2980. {
  2981. asIScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  2982. builder->WriteInfo(script->name.AddressOf(), func->GetDeclaration(true), r, c, false);
  2983. }
  2984. }
  2985. int asCCompiler::AllocateVariableNotIn(const asCDataType &type, bool isTemporary, bool forceOnHeap, asSExprContext *ctx)
  2986. {
  2987. int l = int(reservedVariables.GetLength());
  2988. ctx->bc.GetVarsUsed(reservedVariables);
  2989. int var = AllocateVariable(type, isTemporary, forceOnHeap);
  2990. reservedVariables.SetLength(l);
  2991. return var;
  2992. }
  2993. int asCCompiler::AllocateVariable(const asCDataType &type, bool isTemporary, bool forceOnHeap)
  2994. {
  2995. asCDataType t(type);
  2996. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 1 )
  2997. t.SetTokenType(ttInt);
  2998. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 2 )
  2999. t.SetTokenType(ttDouble);
  3000. // Only null handles have the token type unrecognized token
  3001. asASSERT( t.IsObjectHandle() || t.GetTokenType() != ttUnrecognizedToken );
  3002. bool isOnHeap = true;
  3003. if( t.IsPrimitive() ||
  3004. (t.GetObjectType() && (t.GetObjectType()->GetFlags() & asOBJ_VALUE) && !forceOnHeap) )
  3005. {
  3006. // Primitives and value types (unless overridden) are allocated on the stack
  3007. isOnHeap = false;
  3008. }
  3009. // Find a free location with the same type
  3010. for( asUINT n = 0; n < freeVariables.GetLength(); n++ )
  3011. {
  3012. int slot = freeVariables[n];
  3013. if( variableAllocations[slot].IsEqualExceptConst(t) &&
  3014. variableIsTemporary[slot] == isTemporary &&
  3015. variableIsOnHeap[slot] == isOnHeap )
  3016. {
  3017. // We can't return by slot, must count variable sizes
  3018. int offset = GetVariableOffset(slot);
  3019. // Verify that it is not in the list of reserved variables
  3020. bool isUsed = false;
  3021. if( reservedVariables.GetLength() )
  3022. isUsed = reservedVariables.Exists(offset);
  3023. if( !isUsed )
  3024. {
  3025. if( n != freeVariables.GetLength() - 1 )
  3026. freeVariables[n] = freeVariables.PopLast();
  3027. else
  3028. freeVariables.PopLast();
  3029. if( isTemporary )
  3030. tempVariables.PushLast(offset);
  3031. return offset;
  3032. }
  3033. }
  3034. }
  3035. variableAllocations.PushLast(t);
  3036. variableIsTemporary.PushLast(isTemporary);
  3037. variableIsOnHeap.PushLast(isOnHeap);
  3038. int offset = GetVariableOffset((int)variableAllocations.GetLength()-1);
  3039. if( isTemporary )
  3040. tempVariables.PushLast(offset);
  3041. return offset;
  3042. }
  3043. int asCCompiler::GetVariableOffset(int varIndex)
  3044. {
  3045. // Return offset to the last dword on the stack
  3046. int varOffset = 1;
  3047. for( int n = 0; n < varIndex; n++ )
  3048. {
  3049. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  3050. varOffset += variableAllocations[n].GetSizeInMemoryDWords();
  3051. else
  3052. varOffset += variableAllocations[n].GetSizeOnStackDWords();
  3053. }
  3054. if( varIndex < (int)variableAllocations.GetLength() )
  3055. {
  3056. int size;
  3057. if( !variableIsOnHeap[varIndex] && variableAllocations[varIndex].IsObject() )
  3058. size = variableAllocations[varIndex].GetSizeInMemoryDWords();
  3059. else
  3060. size = variableAllocations[varIndex].GetSizeOnStackDWords();
  3061. if( size > 1 )
  3062. varOffset += size-1;
  3063. }
  3064. return varOffset;
  3065. }
  3066. int asCCompiler::GetVariableSlot(int offset)
  3067. {
  3068. int varOffset = 1;
  3069. for( asUINT n = 0; n < variableAllocations.GetLength(); n++ )
  3070. {
  3071. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  3072. varOffset += -1 + variableAllocations[n].GetSizeInMemoryDWords();
  3073. else
  3074. varOffset += -1 + variableAllocations[n].GetSizeOnStackDWords();
  3075. if( varOffset == offset )
  3076. return n;
  3077. varOffset++;
  3078. }
  3079. return -1;
  3080. }
  3081. bool asCCompiler::IsVariableOnHeap(int offset)
  3082. {
  3083. int varSlot = GetVariableSlot(offset);
  3084. if( varSlot < 0 )
  3085. {
  3086. // This happens for function arguments that are considered as on the heap
  3087. return true;
  3088. }
  3089. return variableIsOnHeap[varSlot];
  3090. }
  3091. void asCCompiler::DeallocateVariable(int offset)
  3092. {
  3093. // Remove temporary variable
  3094. int n;
  3095. for( n = 0; n < (int)tempVariables.GetLength(); n++ )
  3096. {
  3097. if( offset == tempVariables[n] )
  3098. {
  3099. if( n == (int)tempVariables.GetLength()-1 )
  3100. tempVariables.PopLast();
  3101. else
  3102. tempVariables[n] = tempVariables.PopLast();
  3103. break;
  3104. }
  3105. }
  3106. n = GetVariableSlot(offset);
  3107. if( n != -1 )
  3108. {
  3109. freeVariables.PushLast(n);
  3110. return;
  3111. }
  3112. // We might get here if the variable was implicitly declared
  3113. // because it was use before a formal declaration, in this case
  3114. // the offset is 0x7FFF
  3115. asASSERT(offset == 0x7FFF);
  3116. }
  3117. void asCCompiler::ReleaseTemporaryVariable(asCTypeInfo &t, asCByteCode *bc)
  3118. {
  3119. if( t.isTemporary )
  3120. {
  3121. ReleaseTemporaryVariable(t.stackOffset, bc);
  3122. t.isTemporary = false;
  3123. }
  3124. }
  3125. void asCCompiler::ReleaseTemporaryVariable(int offset, asCByteCode *bc)
  3126. {
  3127. if( bc )
  3128. {
  3129. // We need to call the destructor on the true variable type
  3130. int n = GetVariableSlot(offset);
  3131. asASSERT( n >= 0 );
  3132. if( n >= 0 )
  3133. {
  3134. asCDataType dt = variableAllocations[n];
  3135. bool isOnHeap = variableIsOnHeap[n];
  3136. // Call destructor
  3137. CallDestructor(dt, offset, isOnHeap, bc);
  3138. }
  3139. }
  3140. DeallocateVariable(offset);
  3141. }
  3142. void asCCompiler::Dereference(asSExprContext *ctx, bool generateCode)
  3143. {
  3144. if( ctx->type.dataType.IsReference() )
  3145. {
  3146. if( ctx->type.dataType.IsObject() )
  3147. {
  3148. ctx->type.dataType.MakeReference(false);
  3149. if( generateCode )
  3150. ctx->bc.Instr(asBC_RDSPtr);
  3151. }
  3152. else
  3153. {
  3154. // This should never happen as primitives are treated differently
  3155. asASSERT(false);
  3156. }
  3157. }
  3158. }
  3159. bool asCCompiler::IsVariableInitialized(asCTypeInfo *type, asCScriptNode *node)
  3160. {
  3161. // Temporary variables are assumed to be initialized
  3162. if( type->isTemporary ) return true;
  3163. // Verify that it is a variable
  3164. if( !type->isVariable ) return true;
  3165. // Find the variable
  3166. sVariable *v = variables->GetVariableByOffset(type->stackOffset);
  3167. // The variable isn't found if it is a constant, in which case it is guaranteed to be initialized
  3168. if( v == 0 ) return true;
  3169. if( v->isInitialized ) return true;
  3170. // Complex types don't need this test
  3171. if( v->type.IsObject() ) return true;
  3172. // Mark as initialized so that the user will not be bothered again
  3173. v->isInitialized = true;
  3174. // Write warning
  3175. asCString str;
  3176. str.Format(TXT_s_NOT_INITIALIZED, (const char *)v->name.AddressOf());
  3177. Warning(str.AddressOf(), node);
  3178. return false;
  3179. }
  3180. void asCCompiler::PrepareOperand(asSExprContext *ctx, asCScriptNode *node)
  3181. {
  3182. // Check if the variable is initialized (if it indeed is a variable)
  3183. IsVariableInitialized(&ctx->type, node);
  3184. asCDataType to = ctx->type.dataType;
  3185. to.MakeReference(false);
  3186. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  3187. ProcessDeferredParams(ctx);
  3188. }
  3189. void asCCompiler::PrepareForAssignment(asCDataType *lvalue, asSExprContext *rctx, asCScriptNode *node, bool toTemporary, asSExprContext *lvalueExpr)
  3190. {
  3191. ProcessPropertyGetAccessor(rctx, node);
  3192. // Make sure the rvalue is initialized if it is a variable
  3193. IsVariableInitialized(&rctx->type, node);
  3194. if( lvalue->IsPrimitive() )
  3195. {
  3196. if( rctx->type.dataType.IsPrimitive() )
  3197. {
  3198. if( rctx->type.dataType.IsReference() )
  3199. {
  3200. // Cannot do implicit conversion of references so we first convert the reference to a variable
  3201. ConvertToVariableNotIn(rctx, lvalueExpr);
  3202. }
  3203. }
  3204. // Implicitly convert the value to the right type
  3205. int l = int(reservedVariables.GetLength());
  3206. if( lvalueExpr ) lvalueExpr->bc.GetVarsUsed(reservedVariables);
  3207. ImplicitConversion(rctx, *lvalue, node, asIC_IMPLICIT_CONV);
  3208. reservedVariables.SetLength(l);
  3209. // Check data type
  3210. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  3211. {
  3212. asCString str;
  3213. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lvalue->Format().AddressOf());
  3214. Error(str.AddressOf(), node);
  3215. rctx->type.SetDummy();
  3216. }
  3217. // Make sure the rvalue is a variable
  3218. if( !rctx->type.isVariable )
  3219. ConvertToVariableNotIn(rctx, lvalueExpr);
  3220. }
  3221. else
  3222. {
  3223. asCDataType to = *lvalue;
  3224. to.MakeReference(false);
  3225. // TODO: ImplicitConversion should know to do this by itself
  3226. // First convert to a handle which will do a reference cast
  3227. if( !lvalue->IsObjectHandle() &&
  3228. (lvalue->GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) )
  3229. to.MakeHandle(true);
  3230. // Don't allow the implicit conversion to create an object
  3231. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  3232. if( !lvalue->IsObjectHandle() &&
  3233. (lvalue->GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) )
  3234. {
  3235. // Then convert to a reference, which will validate the handle
  3236. to.MakeHandle(false);
  3237. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  3238. }
  3239. // Check data type
  3240. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  3241. {
  3242. asCString str;
  3243. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lvalue->Format().AddressOf());
  3244. Error(str.AddressOf(), node);
  3245. }
  3246. else
  3247. {
  3248. // If the assignment will be made with the copy behaviour then the rvalue must not be a reference
  3249. if( lvalue->IsObject() )
  3250. asASSERT(!rctx->type.dataType.IsReference());
  3251. }
  3252. }
  3253. }
  3254. bool asCCompiler::IsLValue(asCTypeInfo &type)
  3255. {
  3256. if( !type.isLValue ) return false;
  3257. if( type.dataType.IsReadOnly() ) return false;
  3258. if( !type.dataType.IsObject() && !type.isVariable && !type.dataType.IsReference() ) return false;
  3259. return true;
  3260. }
  3261. int asCCompiler::PerformAssignment(asCTypeInfo *lvalue, asCTypeInfo *rvalue, asCByteCode *bc, asCScriptNode *node)
  3262. {
  3263. if( lvalue->dataType.IsReadOnly() )
  3264. {
  3265. Error(TXT_REF_IS_READ_ONLY, node);
  3266. return -1;
  3267. }
  3268. if( lvalue->dataType.IsPrimitive() )
  3269. {
  3270. if( lvalue->isVariable )
  3271. {
  3272. // Copy the value between the variables directly
  3273. if( lvalue->dataType.GetSizeInMemoryDWords() == 1 )
  3274. bc->InstrW_W(asBC_CpyVtoV4, lvalue->stackOffset, rvalue->stackOffset);
  3275. else
  3276. bc->InstrW_W(asBC_CpyVtoV8, lvalue->stackOffset, rvalue->stackOffset);
  3277. // Mark variable as initialized
  3278. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  3279. if( v ) v->isInitialized = true;
  3280. }
  3281. else if( lvalue->dataType.IsReference() )
  3282. {
  3283. // Copy the value of the variable to the reference in the register
  3284. int s = lvalue->dataType.GetSizeInMemoryBytes();
  3285. if( s == 1 )
  3286. bc->InstrSHORT(asBC_WRTV1, rvalue->stackOffset);
  3287. else if( s == 2 )
  3288. bc->InstrSHORT(asBC_WRTV2, rvalue->stackOffset);
  3289. else if( s == 4 )
  3290. bc->InstrSHORT(asBC_WRTV4, rvalue->stackOffset);
  3291. else if( s == 8 )
  3292. bc->InstrSHORT(asBC_WRTV8, rvalue->stackOffset);
  3293. }
  3294. else
  3295. {
  3296. Error(TXT_NOT_VALID_LVALUE, node);
  3297. return -1;
  3298. }
  3299. }
  3300. else if( !lvalue->isExplicitHandle )
  3301. {
  3302. asSExprContext ctx(engine);
  3303. ctx.type = *lvalue;
  3304. Dereference(&ctx, true);
  3305. *lvalue = ctx.type;
  3306. bc->AddCode(&ctx.bc);
  3307. // TODO: Should find the opAssign method that implements the default copy behaviour.
  3308. // The beh->copy member will be removed.
  3309. asSTypeBehaviour *beh = lvalue->dataType.GetBehaviour();
  3310. if( beh->copy )
  3311. {
  3312. // Call the copy operator
  3313. bc->Call(asBC_CALLSYS, (asDWORD)beh->copy, 2*AS_PTR_SIZE);
  3314. bc->Instr(asBC_PshRPtr);
  3315. }
  3316. else
  3317. {
  3318. // Default copy operator
  3319. if( lvalue->dataType.GetSizeInMemoryDWords() == 0 ||
  3320. !(lvalue->dataType.GetObjectType()->flags & asOBJ_POD) )
  3321. {
  3322. asCString msg;
  3323. msg.Format(TXT_NO_DEFAULT_COPY_OP_FOR_s, lvalue->dataType.GetObjectType()->name.AddressOf());
  3324. Error(msg.AddressOf(), node);
  3325. return -1;
  3326. }
  3327. // Copy larger data types from a reference
  3328. // TODO: runtime optimize: COPY should pop both arguments and store the reference in the register.
  3329. bc->InstrSHORT_DW(asBC_COPY, (short)lvalue->dataType.GetSizeInMemoryDWords(), engine->GetTypeIdFromDataType(lvalue->dataType));
  3330. }
  3331. }
  3332. else
  3333. {
  3334. // TODO: The object handle can be stored in a variable as well
  3335. if( !lvalue->dataType.IsReference() )
  3336. {
  3337. Error(TXT_NOT_VALID_REFERENCE, node);
  3338. return -1;
  3339. }
  3340. bc->InstrPTR(asBC_REFCPY, lvalue->dataType.GetObjectType());
  3341. // Mark variable as initialized
  3342. if( variables )
  3343. {
  3344. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  3345. if( v ) v->isInitialized = true;
  3346. }
  3347. }
  3348. return 0;
  3349. }
  3350. bool asCCompiler::CompileRefCast(asSExprContext *ctx, const asCDataType &to, bool isExplicit, asCScriptNode *node, bool generateCode)
  3351. {
  3352. bool conversionDone = false;
  3353. asCArray<int> ops;
  3354. asUINT n;
  3355. if( ctx->type.dataType.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT )
  3356. {
  3357. // We need it to be a reference
  3358. if( !ctx->type.dataType.IsReference() )
  3359. {
  3360. asCDataType to = ctx->type.dataType;
  3361. to.MakeReference(true);
  3362. ImplicitConversion(ctx, to, 0, isExplicit ? asIC_EXPLICIT_REF_CAST : asIC_IMPLICIT_CONV, generateCode);
  3363. }
  3364. if( isExplicit )
  3365. {
  3366. // Allow dynamic cast between object handles (only for script objects).
  3367. // At run time this may result in a null handle,
  3368. // which when used will throw an exception
  3369. conversionDone = true;
  3370. if( generateCode )
  3371. {
  3372. ctx->bc.InstrDWORD(asBC_Cast, engine->GetTypeIdFromDataType(to));
  3373. // Allocate a temporary variable for the returned object
  3374. int returnOffset = AllocateVariable(to, true);
  3375. // Move the pointer from the object register to the temporary variable
  3376. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  3377. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  3378. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3379. ctx->type.SetVariable(to, returnOffset, true);
  3380. ctx->type.dataType.MakeReference(true);
  3381. }
  3382. else
  3383. {
  3384. ctx->type.dataType = to;
  3385. ctx->type.dataType.MakeReference(true);
  3386. }
  3387. }
  3388. else
  3389. {
  3390. if( ctx->type.dataType.GetObjectType()->DerivesFrom(to.GetObjectType()) )
  3391. {
  3392. conversionDone = true;
  3393. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3394. }
  3395. }
  3396. }
  3397. else
  3398. {
  3399. // Find a suitable registered behaviour
  3400. asSTypeBehaviour *beh = &ctx->type.dataType.GetObjectType()->beh;
  3401. for( n = 0; n < beh->operators.GetLength(); n+= 2 )
  3402. {
  3403. if( (isExplicit && asBEHAVE_REF_CAST == beh->operators[n]) ||
  3404. asBEHAVE_IMPLICIT_REF_CAST == beh->operators[n] )
  3405. {
  3406. int funcId = beh->operators[n+1];
  3407. // Is the operator for the output type?
  3408. asCScriptFunction *func = engine->scriptFunctions[funcId];
  3409. if( func->returnType.GetObjectType() != to.GetObjectType() )
  3410. continue;
  3411. ops.PushLast(funcId);
  3412. }
  3413. }
  3414. // It shouldn't be possible to have more than one
  3415. asASSERT( ops.GetLength() <= 1 );
  3416. // Should only have one behaviour for each output type
  3417. if( ops.GetLength() == 1 )
  3418. {
  3419. if( generateCode )
  3420. {
  3421. // TODO: runtime optimize: Instead of producing bytecode for checking if the handle is
  3422. // null, we can create a special CALLSYS instruction that checks
  3423. // if the object pointer is null and if so sets the object register
  3424. // to null directly without executing the function.
  3425. //
  3426. // Alternatively I could force the ref cast behaviours be global
  3427. // functions with 1 parameter, even though they should still be
  3428. // registered with RegisterObjectBehaviour()
  3429. // Add code to avoid calling the cast behaviour if the handle is already null,
  3430. // because that will raise a null pointer exception due to the cast behaviour
  3431. // being a class method, and the this pointer cannot be null.
  3432. if( ctx->type.isVariable )
  3433. ctx->bc.Instr(asBC_PopPtr);
  3434. else
  3435. {
  3436. Dereference(ctx, true);
  3437. ConvertToVariable(ctx);
  3438. }
  3439. // TODO: runtime optimize: should have immediate comparison for null pointer
  3440. int offset = AllocateVariable(asCDataType::CreateNullHandle(), true);
  3441. // 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)
  3442. ctx->bc.InstrSHORT(asBC_ClrVPtr, (asWORD)offset);
  3443. ctx->bc.InstrW_W(asBC_CmpPtr, ctx->type.stackOffset, offset);
  3444. DeallocateVariable(offset);
  3445. int afterLabel = nextLabel++;
  3446. ctx->bc.InstrDWORD(asBC_JZ, afterLabel);
  3447. // Call the cast operator
  3448. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  3449. ctx->bc.Instr(asBC_RDSPtr);
  3450. ctx->type.dataType.MakeReference(false);
  3451. asCTypeInfo objType = ctx->type;
  3452. asCArray<asSExprContext *> args;
  3453. MakeFunctionCall(ctx, ops[0], objType.dataType.GetObjectType(), args, node);
  3454. ctx->bc.Instr(asBC_PopPtr);
  3455. int endLabel = nextLabel++;
  3456. ctx->bc.InstrINT(asBC_JMP, endLabel);
  3457. ctx->bc.Label((short)afterLabel);
  3458. // Make a NULL pointer
  3459. ctx->bc.InstrSHORT(asBC_ClrVPtr, ctx->type.stackOffset);
  3460. ctx->bc.Label((short)endLabel);
  3461. // Since we're receiving a handle, we can release the original variable
  3462. ReleaseTemporaryVariable(objType, &ctx->bc);
  3463. // Push the reference to the handle on the stack
  3464. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  3465. }
  3466. else
  3467. {
  3468. asCScriptFunction *func = engine->scriptFunctions[ops[0]];
  3469. ctx->type.Set(func->returnType);
  3470. }
  3471. }
  3472. else if( ops.GetLength() == 0 )
  3473. {
  3474. // Check for the generic ref cast behaviour
  3475. for( n = 0; n < beh->operators.GetLength(); n+= 2 )
  3476. {
  3477. if( (isExplicit && asBEHAVE_REF_CAST == beh->operators[n]) ||
  3478. asBEHAVE_IMPLICIT_REF_CAST == beh->operators[n] )
  3479. {
  3480. int funcId = beh->operators[n+1];
  3481. // Does the operator take the ?&out parameter?
  3482. asCScriptFunction *func = engine->scriptFunctions[funcId];
  3483. if( func->parameterTypes.GetLength() != 1 ||
  3484. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  3485. func->inOutFlags[0] != asTM_OUTREF )
  3486. continue;
  3487. ops.PushLast(funcId);
  3488. }
  3489. }
  3490. // It shouldn't be possible to have more than one
  3491. asASSERT( ops.GetLength() <= 1 );
  3492. if( ops.GetLength() == 1 )
  3493. {
  3494. if( generateCode )
  3495. {
  3496. asASSERT(to.IsObjectHandle());
  3497. // Allocate a temporary variable of the requested handle type
  3498. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  3499. // Pass the reference of that variable to the function as output parameter
  3500. asCDataType toRef(to);
  3501. toRef.MakeReference(true);
  3502. asCArray<asSExprContext *> args;
  3503. asSExprContext arg(engine);
  3504. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  3505. // Don't mark the variable as temporary, so it won't be freed too early
  3506. arg.type.SetVariable(toRef, stackOffset, false);
  3507. arg.type.isLValue = true;
  3508. arg.type.isExplicitHandle = true;
  3509. args.PushLast(&arg);
  3510. asCTypeInfo prev = ctx->type;
  3511. // Call the behaviour method
  3512. MakeFunctionCall(ctx, ops[0], ctx->type.dataType.GetObjectType(), args, node);
  3513. // Release previous temporary variable
  3514. ReleaseTemporaryVariable(prev, &ctx->bc);
  3515. // Use the reference to the variable as the result of the expression
  3516. // Now we can mark the variable as temporary
  3517. ctx->type.SetVariable(toRef, stackOffset, true);
  3518. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  3519. }
  3520. else
  3521. {
  3522. // All casts are legal
  3523. ctx->type.Set(to);
  3524. }
  3525. }
  3526. }
  3527. }
  3528. return conversionDone;
  3529. }
  3530. asUINT asCCompiler::ImplicitConvPrimitiveToPrimitive(asSExprContext *ctx, const asCDataType &toOrig, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  3531. {
  3532. asCDataType to = toOrig;
  3533. to.MakeReference(false);
  3534. asASSERT( !ctx->type.dataType.IsReference() );
  3535. // Maybe no conversion is needed
  3536. if( to.IsEqualExceptConst(ctx->type.dataType) )
  3537. {
  3538. // A primitive is const or not
  3539. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  3540. return asCC_NO_CONV;
  3541. }
  3542. // Determine the cost of this conversion
  3543. asUINT cost = asCC_NO_CONV;
  3544. if( (to.IsIntegerType() || to.IsUnsignedType()) && (ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  3545. cost = asCC_INT_FLOAT_CONV;
  3546. else if( (to.IsFloatType() || to.IsDoubleType()) && (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() || ctx->type.dataType.IsEnumType()) )
  3547. cost = asCC_INT_FLOAT_CONV;
  3548. else if( to.IsUnsignedType() && ctx->type.dataType.IsIntegerType() )
  3549. cost = asCC_SIGNED_CONV;
  3550. else if( to.IsIntegerType() && (ctx->type.dataType.IsUnsignedType() || ctx->type.dataType.IsEnumType()) )
  3551. cost = asCC_SIGNED_CONV;
  3552. else if( to.GetSizeInMemoryBytes() || ctx->type.dataType.GetSizeInMemoryBytes() )
  3553. cost = asCC_PRIMITIVE_SIZE_CONV;
  3554. // Start by implicitly converting constant values
  3555. if( ctx->type.isConstant )
  3556. {
  3557. ImplicitConversionConstant(ctx, to, node, convType);
  3558. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  3559. return cost;
  3560. }
  3561. // Allow implicit conversion between numbers
  3562. if( generateCode )
  3563. {
  3564. // When generating the code the decision has already been made, so we don't bother determining the cost
  3565. // Convert smaller types to 32bit first
  3566. int s = ctx->type.dataType.GetSizeInMemoryBytes();
  3567. if( s < 4 )
  3568. {
  3569. ConvertToTempVariable(ctx);
  3570. if( ctx->type.dataType.IsIntegerType() )
  3571. {
  3572. if( s == 1 )
  3573. ctx->bc.InstrSHORT(asBC_sbTOi, ctx->type.stackOffset);
  3574. else if( s == 2 )
  3575. ctx->bc.InstrSHORT(asBC_swTOi, ctx->type.stackOffset);
  3576. ctx->type.dataType.SetTokenType(ttInt);
  3577. }
  3578. else if( ctx->type.dataType.IsUnsignedType() )
  3579. {
  3580. if( s == 1 )
  3581. ctx->bc.InstrSHORT(asBC_ubTOi, ctx->type.stackOffset);
  3582. else if( s == 2 )
  3583. ctx->bc.InstrSHORT(asBC_uwTOi, ctx->type.stackOffset);
  3584. ctx->type.dataType.SetTokenType(ttUInt);
  3585. }
  3586. }
  3587. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1) ||
  3588. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  3589. {
  3590. if( ctx->type.dataType.IsIntegerType() ||
  3591. ctx->type.dataType.IsUnsignedType() ||
  3592. ctx->type.dataType.IsEnumType() )
  3593. {
  3594. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3595. {
  3596. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3597. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3598. }
  3599. else
  3600. {
  3601. ConvertToTempVariable(ctx);
  3602. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3603. int offset = AllocateVariable(to, true);
  3604. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  3605. ctx->type.SetVariable(to, offset, true);
  3606. }
  3607. }
  3608. else if( ctx->type.dataType.IsFloatType() )
  3609. {
  3610. ConvertToTempVariable(ctx);
  3611. ctx->bc.InstrSHORT(asBC_fTOi, ctx->type.stackOffset);
  3612. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3613. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3614. }
  3615. else if( ctx->type.dataType.IsDoubleType() )
  3616. {
  3617. ConvertToTempVariable(ctx);
  3618. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3619. int offset = AllocateVariable(to, true);
  3620. ctx->bc.InstrW_W(asBC_dTOi, offset, ctx->type.stackOffset);
  3621. ctx->type.SetVariable(to, offset, true);
  3622. }
  3623. // Convert to smaller integer if necessary
  3624. int s = to.GetSizeInMemoryBytes();
  3625. if( s < 4 )
  3626. {
  3627. ConvertToTempVariable(ctx);
  3628. if( s == 1 )
  3629. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  3630. else if( s == 2 )
  3631. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  3632. }
  3633. }
  3634. if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  3635. {
  3636. if( ctx->type.dataType.IsIntegerType() ||
  3637. ctx->type.dataType.IsUnsignedType() ||
  3638. ctx->type.dataType.IsEnumType() )
  3639. {
  3640. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3641. {
  3642. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3643. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3644. }
  3645. else
  3646. {
  3647. ConvertToTempVariable(ctx);
  3648. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3649. int offset = AllocateVariable(to, true);
  3650. if( ctx->type.dataType.IsUnsignedType() )
  3651. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  3652. else
  3653. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  3654. ctx->type.SetVariable(to, offset, true);
  3655. }
  3656. }
  3657. else if( ctx->type.dataType.IsFloatType() )
  3658. {
  3659. ConvertToTempVariable(ctx);
  3660. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3661. int offset = AllocateVariable(to, true);
  3662. ctx->bc.InstrW_W(asBC_fTOi64, offset, ctx->type.stackOffset);
  3663. ctx->type.SetVariable(to, offset, true);
  3664. }
  3665. else if( ctx->type.dataType.IsDoubleType() )
  3666. {
  3667. ConvertToTempVariable(ctx);
  3668. ctx->bc.InstrSHORT(asBC_dTOi64, ctx->type.stackOffset);
  3669. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3670. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3671. }
  3672. }
  3673. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  3674. {
  3675. if( ctx->type.dataType.IsIntegerType() ||
  3676. ctx->type.dataType.IsUnsignedType() ||
  3677. ctx->type.dataType.IsEnumType() )
  3678. {
  3679. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3680. {
  3681. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3682. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3683. }
  3684. else
  3685. {
  3686. ConvertToTempVariable(ctx);
  3687. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3688. int offset = AllocateVariable(to, true);
  3689. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  3690. ctx->type.SetVariable(to, offset, true);
  3691. }
  3692. }
  3693. else if( ctx->type.dataType.IsFloatType() )
  3694. {
  3695. ConvertToTempVariable(ctx);
  3696. ctx->bc.InstrSHORT(asBC_fTOu, ctx->type.stackOffset);
  3697. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3698. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3699. }
  3700. else if( ctx->type.dataType.IsDoubleType() )
  3701. {
  3702. ConvertToTempVariable(ctx);
  3703. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3704. int offset = AllocateVariable(to, true);
  3705. ctx->bc.InstrW_W(asBC_dTOu, offset, ctx->type.stackOffset);
  3706. ctx->type.SetVariable(to, offset, true);
  3707. }
  3708. // Convert to smaller integer if necessary
  3709. int s = to.GetSizeInMemoryBytes();
  3710. if( s < 4 )
  3711. {
  3712. ConvertToTempVariable(ctx);
  3713. if( s == 1 )
  3714. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  3715. else if( s == 2 )
  3716. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  3717. }
  3718. }
  3719. if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  3720. {
  3721. if( ctx->type.dataType.IsIntegerType() ||
  3722. ctx->type.dataType.IsUnsignedType() ||
  3723. ctx->type.dataType.IsEnumType() )
  3724. {
  3725. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3726. {
  3727. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3728. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3729. }
  3730. else
  3731. {
  3732. ConvertToTempVariable(ctx);
  3733. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3734. int offset = AllocateVariable(to, true);
  3735. if( ctx->type.dataType.IsUnsignedType() )
  3736. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  3737. else
  3738. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  3739. ctx->type.SetVariable(to, offset, true);
  3740. }
  3741. }
  3742. else if( ctx->type.dataType.IsFloatType() )
  3743. {
  3744. ConvertToTempVariable(ctx);
  3745. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3746. int offset = AllocateVariable(to, true);
  3747. ctx->bc.InstrW_W(asBC_fTOu64, offset, ctx->type.stackOffset);
  3748. ctx->type.SetVariable(to, offset, true);
  3749. }
  3750. else if( ctx->type.dataType.IsDoubleType() )
  3751. {
  3752. ConvertToTempVariable(ctx);
  3753. ctx->bc.InstrSHORT(asBC_dTOu64, ctx->type.stackOffset);
  3754. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3755. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3756. }
  3757. }
  3758. else if( to.IsFloatType() )
  3759. {
  3760. if( (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsEnumType()) && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3761. {
  3762. ConvertToTempVariable(ctx);
  3763. ctx->bc.InstrSHORT(asBC_iTOf, ctx->type.stackOffset);
  3764. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3765. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3766. }
  3767. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3768. {
  3769. ConvertToTempVariable(ctx);
  3770. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3771. int offset = AllocateVariable(to, true);
  3772. ctx->bc.InstrW_W(asBC_i64TOf, offset, ctx->type.stackOffset);
  3773. ctx->type.SetVariable(to, offset, true);
  3774. }
  3775. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3776. {
  3777. ConvertToTempVariable(ctx);
  3778. ctx->bc.InstrSHORT(asBC_uTOf, ctx->type.stackOffset);
  3779. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3780. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3781. }
  3782. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3783. {
  3784. ConvertToTempVariable(ctx);
  3785. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3786. int offset = AllocateVariable(to, true);
  3787. ctx->bc.InstrW_W(asBC_u64TOf, offset, ctx->type.stackOffset);
  3788. ctx->type.SetVariable(to, offset, true);
  3789. }
  3790. else if( ctx->type.dataType.IsDoubleType() )
  3791. {
  3792. ConvertToTempVariable(ctx);
  3793. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3794. int offset = AllocateVariable(to, true);
  3795. ctx->bc.InstrW_W(asBC_dTOf, offset, ctx->type.stackOffset);
  3796. ctx->type.SetVariable(to, offset, true);
  3797. }
  3798. }
  3799. else if( to.IsDoubleType() )
  3800. {
  3801. if( (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsEnumType()) && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3802. {
  3803. ConvertToTempVariable(ctx);
  3804. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3805. int offset = AllocateVariable(to, true);
  3806. ctx->bc.InstrW_W(asBC_iTOd, offset, ctx->type.stackOffset);
  3807. ctx->type.SetVariable(to, offset, true);
  3808. }
  3809. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3810. {
  3811. ConvertToTempVariable(ctx);
  3812. ctx->bc.InstrSHORT(asBC_i64TOd, ctx->type.stackOffset);
  3813. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3814. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3815. }
  3816. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  3817. {
  3818. ConvertToTempVariable(ctx);
  3819. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3820. int offset = AllocateVariable(to, true);
  3821. ctx->bc.InstrW_W(asBC_uTOd, offset, ctx->type.stackOffset);
  3822. ctx->type.SetVariable(to, offset, true);
  3823. }
  3824. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  3825. {
  3826. ConvertToTempVariable(ctx);
  3827. ctx->bc.InstrSHORT(asBC_u64TOd, ctx->type.stackOffset);
  3828. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3829. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3830. }
  3831. else if( ctx->type.dataType.IsFloatType() )
  3832. {
  3833. ConvertToTempVariable(ctx);
  3834. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3835. int offset = AllocateVariable(to, true);
  3836. ctx->bc.InstrW_W(asBC_fTOd, offset, ctx->type.stackOffset);
  3837. ctx->type.SetVariable(to, offset, true);
  3838. }
  3839. }
  3840. }
  3841. else
  3842. {
  3843. if( (to.IsIntegerType() || to.IsUnsignedType() ||
  3844. to.IsFloatType() || to.IsDoubleType() ||
  3845. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST)) &&
  3846. (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() ||
  3847. ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType() ||
  3848. ctx->type.dataType.IsEnumType()) )
  3849. {
  3850. ctx->type.dataType.SetTokenType(to.GetTokenType());
  3851. ctx->type.dataType.SetObjectType(to.GetObjectType());
  3852. }
  3853. }
  3854. // Primitive types on the stack, can be const or non-const
  3855. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  3856. return cost;
  3857. }
  3858. asUINT asCCompiler::ImplicitConversion(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  3859. {
  3860. asASSERT( ctx->type.dataType.GetTokenType() != ttUnrecognizedToken ||
  3861. ctx->type.dataType.IsNullHandle() );
  3862. // No conversion from void to any other type
  3863. if( ctx->type.dataType.GetTokenType() == ttVoid )
  3864. return asCC_NO_CONV;
  3865. // Do we want a var type?
  3866. if( to.GetTokenType() == ttQuestion )
  3867. {
  3868. // Any type can be converted to a var type, but only when not generating code
  3869. asASSERT( !generateCode );
  3870. ctx->type.dataType = to;
  3871. return asCC_VARIABLE_CONV;
  3872. }
  3873. // Do we want a primitive?
  3874. else if( to.IsPrimitive() )
  3875. {
  3876. if( !ctx->type.dataType.IsPrimitive() )
  3877. return ImplicitConvObjectToPrimitive(ctx, to, node, convType, generateCode);
  3878. else
  3879. return ImplicitConvPrimitiveToPrimitive(ctx, to, node, convType, generateCode);
  3880. }
  3881. else // The target is a complex type
  3882. {
  3883. if( ctx->type.dataType.IsPrimitive() )
  3884. return ImplicitConvPrimitiveToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  3885. else if( ctx->type.IsNullConstant() || ctx->type.dataType.GetObjectType() )
  3886. return ImplicitConvObjectToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  3887. }
  3888. return asCC_NO_CONV;
  3889. }
  3890. asUINT asCCompiler::ImplicitConvObjectToPrimitive(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  3891. {
  3892. if( ctx->type.isExplicitHandle )
  3893. {
  3894. // An explicit handle cannot be converted to a primitive
  3895. if( convType != asIC_IMPLICIT_CONV && node )
  3896. {
  3897. asCString str;
  3898. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  3899. Error(str.AddressOf(), node);
  3900. }
  3901. return asCC_NO_CONV;
  3902. }
  3903. // TODO: Must use the const cast behaviour if the object is read-only
  3904. // Find matching value cast behaviours
  3905. // Here we're only interested in those that convert the type to a primitive type
  3906. asCArray<int> funcs;
  3907. asSTypeBehaviour *beh = ctx->type.dataType.GetBehaviour();
  3908. if( beh )
  3909. {
  3910. if( convType == asIC_EXPLICIT_VAL_CAST )
  3911. {
  3912. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  3913. {
  3914. // accept both implicit and explicit cast
  3915. if( (beh->operators[n] == asBEHAVE_VALUE_CAST ||
  3916. beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST) &&
  3917. builder->GetFunctionDescription(beh->operators[n+1])->returnType.IsPrimitive() )
  3918. funcs.PushLast(beh->operators[n+1]);
  3919. }
  3920. }
  3921. else
  3922. {
  3923. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  3924. {
  3925. // accept only implicit cast
  3926. if( beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST &&
  3927. builder->GetFunctionDescription(beh->operators[n+1])->returnType.IsPrimitive() )
  3928. funcs.PushLast(beh->operators[n+1]);
  3929. }
  3930. }
  3931. }
  3932. // This matrix describes the priorities of the types to search for, for each target type
  3933. // The first column is the target type, the priorities goes from left to right
  3934. eTokenType matchMtx[10][10] =
  3935. {
  3936. {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  3937. {ttFloat, ttDouble, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  3938. {ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  3939. {ttUInt64, ttInt64, ttUInt, ttInt, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  3940. {ttInt, ttUInt, ttInt64, ttUInt64, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  3941. {ttUInt, ttInt, ttUInt64, ttInt64, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  3942. {ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttInt8, ttUInt8, ttDouble, ttFloat},
  3943. {ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttUInt8, ttInt8, ttDouble, ttFloat},
  3944. {ttInt8, ttUInt8, ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttDouble, ttFloat},
  3945. {ttUInt8, ttInt8, ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttDouble, ttFloat},
  3946. };
  3947. // Which row to use?
  3948. eTokenType *row = 0;
  3949. for( unsigned int type = 0; type < 10; type++ )
  3950. {
  3951. if( to.GetTokenType() == matchMtx[type][0] )
  3952. {
  3953. row = &matchMtx[type][0];
  3954. break;
  3955. }
  3956. }
  3957. // Find the best matching cast operator
  3958. int funcId = 0;
  3959. if( row )
  3960. {
  3961. asCDataType target(to);
  3962. // Priority goes from left to right in the matrix
  3963. for( unsigned int attempt = 0; attempt < 10 && funcId == 0; attempt++ )
  3964. {
  3965. target.SetTokenType(row[attempt]);
  3966. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  3967. {
  3968. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  3969. if( descr->returnType.IsEqualExceptConst(target) )
  3970. {
  3971. funcId = funcs[n];
  3972. break;
  3973. }
  3974. }
  3975. }
  3976. }
  3977. // Did we find a suitable function?
  3978. if( funcId != 0 )
  3979. {
  3980. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  3981. if( generateCode )
  3982. {
  3983. asCTypeInfo objType = ctx->type;
  3984. Dereference(ctx, true);
  3985. PerformFunctionCall(funcId, ctx);
  3986. ReleaseTemporaryVariable(objType, &ctx->bc);
  3987. }
  3988. else
  3989. ctx->type.Set(descr->returnType);
  3990. // Allow one more implicit conversion to another primitive type
  3991. return asCC_OBJ_TO_PRIMITIVE_CONV + ImplicitConversion(ctx, to, node, convType, generateCode, false);
  3992. }
  3993. else
  3994. {
  3995. if( convType != asIC_IMPLICIT_CONV && node )
  3996. {
  3997. asCString str;
  3998. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  3999. Error(str.AddressOf(), node);
  4000. }
  4001. }
  4002. return asCC_NO_CONV;
  4003. }
  4004. asUINT asCCompiler::ImplicitConvObjectRef(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  4005. {
  4006. // Convert null to any object type handle, but not to a non-handle type
  4007. if( ctx->type.IsNullConstant() )
  4008. {
  4009. if( to.IsObjectHandle() )
  4010. {
  4011. ctx->type.dataType = to;
  4012. return asCC_REF_CONV;
  4013. }
  4014. return asCC_NO_CONV;
  4015. }
  4016. asASSERT(ctx->type.dataType.GetObjectType());
  4017. // First attempt to convert the base type without instanciating another instance
  4018. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  4019. {
  4020. // If the to type is an interface and the from type implements it, then we can convert it immediately
  4021. if( ctx->type.dataType.GetObjectType()->Implements(to.GetObjectType()) )
  4022. {
  4023. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4024. return asCC_REF_CONV;
  4025. }
  4026. // If the to type is a class and the from type derives from it, then we can convert it immediately
  4027. else if( ctx->type.dataType.GetObjectType()->DerivesFrom(to.GetObjectType()) )
  4028. {
  4029. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4030. return asCC_REF_CONV;
  4031. }
  4032. // If the types are not equal yet, then we may still be able to find a reference cast
  4033. else if( ctx->type.dataType.GetObjectType() != to.GetObjectType() )
  4034. {
  4035. // A ref cast must not remove the constness
  4036. bool isConst = false;
  4037. if( (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) ||
  4038. (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) )
  4039. isConst = true;
  4040. // We may still be able to find an implicit ref cast behaviour
  4041. CompileRefCast(ctx, to, convType == asIC_EXPLICIT_REF_CAST, node, generateCode);
  4042. ctx->type.dataType.MakeHandleToConst(isConst);
  4043. // Was the conversion done?
  4044. if( ctx->type.dataType.GetObjectType() == to.GetObjectType() )
  4045. return asCC_REF_CONV;
  4046. }
  4047. }
  4048. // Convert matching function types
  4049. if( to.GetFuncDef() && ctx->type.dataType.GetFuncDef() &&
  4050. to.GetFuncDef() != ctx->type.dataType.GetFuncDef() )
  4051. {
  4052. asCScriptFunction *toFunc = to.GetFuncDef();
  4053. asCScriptFunction *fromFunc = ctx->type.dataType.GetFuncDef();
  4054. if( toFunc->IsSignatureExceptNameEqual(fromFunc) )
  4055. {
  4056. ctx->type.dataType.SetFuncDef(toFunc);
  4057. return asCC_REF_CONV;
  4058. }
  4059. }
  4060. return asCC_NO_CONV;
  4061. }
  4062. asUINT asCCompiler::ImplicitConvObjectValue(asSExprContext *ctx, const asCDataType &to, asCScriptNode * /*node*/, EImplicitConv convType, bool generateCode)
  4063. {
  4064. asUINT cost = asCC_NO_CONV;
  4065. // If the base type is still different, and we are allowed to instance
  4066. // another object then we can try an implicit value cast
  4067. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  4068. {
  4069. // TODO: Implement support for implicit constructor/factory
  4070. asCArray<int> funcs;
  4071. asSTypeBehaviour *beh = ctx->type.dataType.GetBehaviour();
  4072. if( beh )
  4073. {
  4074. if( convType == asIC_EXPLICIT_VAL_CAST )
  4075. {
  4076. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  4077. {
  4078. // accept both implicit and explicit cast
  4079. if( (beh->operators[n] == asBEHAVE_VALUE_CAST ||
  4080. beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST) &&
  4081. builder->GetFunctionDescription(beh->operators[n+1])->returnType.GetObjectType() == to.GetObjectType() )
  4082. funcs.PushLast(beh->operators[n+1]);
  4083. }
  4084. }
  4085. else
  4086. {
  4087. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  4088. {
  4089. // accept only implicit cast
  4090. if( beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST &&
  4091. builder->GetFunctionDescription(beh->operators[n+1])->returnType.GetObjectType() == to.GetObjectType() )
  4092. funcs.PushLast(beh->operators[n+1]);
  4093. }
  4094. }
  4095. }
  4096. // TODO: If there are multiple valid value casts, then we must choose the most appropriate one
  4097. asASSERT( funcs.GetLength() <= 1 );
  4098. if( funcs.GetLength() == 1 )
  4099. {
  4100. asCScriptFunction *f = builder->GetFunctionDescription(funcs[0]);
  4101. if( generateCode )
  4102. {
  4103. asCTypeInfo objType = ctx->type;
  4104. Dereference(ctx, true);
  4105. bool useVariable = false;
  4106. int stackOffset = 0;
  4107. if( f->DoesReturnOnStack() )
  4108. {
  4109. useVariable = true;
  4110. stackOffset = AllocateVariable(f->returnType, true);
  4111. // Push the pointer to the pre-allocated space for the return value
  4112. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  4113. // The object pointer is already on the stack, but should be the top
  4114. // one, so we need to swap the pointers in order to get the correct
  4115. ctx->bc.Instr(asBC_SwapPtr);
  4116. }
  4117. PerformFunctionCall(funcs[0], ctx, false, 0, 0, useVariable, stackOffset);
  4118. ReleaseTemporaryVariable(objType, &ctx->bc);
  4119. }
  4120. else
  4121. ctx->type.Set(f->returnType);
  4122. cost = asCC_TO_OBJECT_CONV;
  4123. }
  4124. }
  4125. return cost;
  4126. }
  4127. asUINT asCCompiler::ImplicitConvObjectToObject(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  4128. {
  4129. // First try a ref cast
  4130. asUINT cost = ImplicitConvObjectRef(ctx, to, node, convType, generateCode);
  4131. // If the desired type is an asOBJ_ASHANDLE then we'll assume it is allowed to implicitly
  4132. // construct the object through any of the available constructors
  4133. if( to.GetObjectType() && (to.GetObjectType()->flags & asOBJ_ASHANDLE) && to.GetObjectType() != ctx->type.dataType.GetObjectType() && allowObjectConstruct )
  4134. {
  4135. asCArray<int> funcs;
  4136. funcs = to.GetObjectType()->beh.constructors;
  4137. asCArray<asSExprContext *> args;
  4138. args.PushLast(ctx);
  4139. cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, node, 0, 0, false, true, false);
  4140. // Did we find a matching constructor?
  4141. if( funcs.GetLength() == 1 )
  4142. {
  4143. if( generateCode )
  4144. {
  4145. // TODO: This should really reuse the code from CompileConstructCall
  4146. // Allocate the new object
  4147. asCTypeInfo tempObj;
  4148. tempObj.dataType = to;
  4149. tempObj.dataType.MakeReference(false);
  4150. tempObj.stackOffset = (short)AllocateVariable(tempObj.dataType, true);
  4151. tempObj.dataType.MakeReference(true);
  4152. tempObj.isTemporary = true;
  4153. tempObj.isVariable = true;
  4154. bool onHeap = IsVariableOnHeap(tempObj.stackOffset);
  4155. // Push the address of the object on the stack
  4156. asSExprContext e(engine);
  4157. if( onHeap )
  4158. e.bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  4159. PrepareFunctionCall(funcs[0], &e.bc, args);
  4160. MoveArgsToStack(funcs[0], &e.bc, args, false);
  4161. // If the object is allocated on the stack, then call the constructor as a normal function
  4162. if( onHeap )
  4163. {
  4164. int offset = 0;
  4165. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  4166. offset = descr->parameterTypes[0].GetSizeOnStackDWords();
  4167. e.bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  4168. }
  4169. else
  4170. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  4171. PerformFunctionCall(funcs[0], &e, onHeap, &args, tempObj.dataType.GetObjectType());
  4172. // Add tag that the object has been initialized
  4173. e.bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  4174. // The constructor doesn't return anything,
  4175. // so we have to manually inform the type of
  4176. // the return value
  4177. e.type = tempObj;
  4178. if( !onHeap )
  4179. e.type.dataType.MakeReference(false);
  4180. // Push the address of the object on the stack again
  4181. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  4182. MergeExprBytecodeAndType(ctx, &e);
  4183. }
  4184. else
  4185. {
  4186. ctx->type.Set(asCDataType::CreateObject(to.GetObjectType(), false));
  4187. }
  4188. }
  4189. }
  4190. // If the base type is still different, and we are allowed to instance
  4191. // another object then we can try an implicit value cast
  4192. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() && allowObjectConstruct )
  4193. {
  4194. // Attempt implicit value cast
  4195. cost = ImplicitConvObjectValue(ctx, to, node, convType, generateCode);
  4196. }
  4197. // If we still haven't converted the base type to the correct type, then there is
  4198. // no need to continue as it is not possible to do the conversion
  4199. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  4200. return asCC_NO_CONV;
  4201. if( to.IsObjectHandle() )
  4202. {
  4203. // There is no extra cost in converting to a handle
  4204. // reference to handle -> handle
  4205. // reference -> handle
  4206. // object -> handle
  4207. // handle -> reference to handle
  4208. // reference -> reference to handle
  4209. // object -> reference to handle
  4210. // TODO: If the type is handle, then we can't use IsReadOnly to determine the constness of the basetype
  4211. // If the rvalue is a handle to a const object, then
  4212. // the lvalue must also be a handle to a const object
  4213. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() )
  4214. {
  4215. if( convType != asIC_IMPLICIT_CONV )
  4216. {
  4217. asASSERT(node);
  4218. asCString str;
  4219. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  4220. Error(str.AddressOf(), node);
  4221. }
  4222. }
  4223. if( !ctx->type.dataType.IsObjectHandle() )
  4224. {
  4225. // An object type can be directly converted to a handle of the
  4226. // same type by doing a ref copy to a new variable
  4227. if( ctx->type.dataType.SupportHandles() )
  4228. {
  4229. asCDataType dt = ctx->type.dataType;
  4230. dt.MakeHandle(true);
  4231. dt.MakeReference(false);
  4232. if( generateCode )
  4233. {
  4234. // TODO: runtime optimize: This copy is not always necessary.
  4235. // How to determine when not to do it?
  4236. int offset = AllocateVariable(dt, true);
  4237. if( ctx->type.dataType.IsReference() )
  4238. ctx->bc.Instr(asBC_RDSPtr);
  4239. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  4240. ctx->bc.InstrPTR(asBC_REFCPY, dt.GetObjectType());
  4241. ctx->bc.Instr(asBC_PopPtr);
  4242. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  4243. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4244. if( to.IsReference() )
  4245. dt.MakeReference(true);
  4246. else
  4247. ctx->bc.Instr(asBC_RDSPtr);
  4248. ctx->type.SetVariable(dt, offset, true);
  4249. }
  4250. else
  4251. ctx->type.dataType = dt;
  4252. // When this conversion is done the expression is no longer an lvalue
  4253. ctx->type.isLValue = false;
  4254. }
  4255. if( ctx->type.dataType.IsObjectHandle() )
  4256. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4257. if( to.IsHandleToConst() && ctx->type.dataType.IsObjectHandle() )
  4258. ctx->type.dataType.MakeHandleToConst(true);
  4259. }
  4260. else
  4261. {
  4262. // A handle to non-const can be converted to a
  4263. // handle to const, but not the other way
  4264. if( to.IsHandleToConst() )
  4265. ctx->type.dataType.MakeHandleToConst(true);
  4266. // A const handle can be converted to a non-const
  4267. // handle and vice versa as the handle is just a value
  4268. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4269. }
  4270. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  4271. {
  4272. if( generateCode )
  4273. {
  4274. asASSERT( ctx->type.dataType.IsObjectHandle() );
  4275. // If the input type is a handle, then a simple ref copy is enough
  4276. bool isExplicitHandle = ctx->type.isExplicitHandle;
  4277. ctx->type.isExplicitHandle = ctx->type.dataType.IsObjectHandle();
  4278. // If the input type is read-only we'll need to temporarily
  4279. // remove this constness, otherwise the assignment will fail
  4280. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  4281. ctx->type.dataType.MakeReadOnly(false);
  4282. // If the object already is a temporary variable, then the copy
  4283. // doesn't have to be made as it is already a unique object
  4284. PrepareTemporaryObject(node, ctx);
  4285. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  4286. ctx->type.isExplicitHandle = isExplicitHandle;
  4287. }
  4288. // A non-reference can be converted to a reference,
  4289. // by putting the value in a temporary variable
  4290. ctx->type.dataType.MakeReference(true);
  4291. // Since it is a new temporary variable it doesn't have to be const
  4292. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4293. }
  4294. else if( !to.IsReference() && ctx->type.dataType.IsReference() )
  4295. {
  4296. Dereference(ctx, generateCode);
  4297. }
  4298. }
  4299. else
  4300. {
  4301. if( !to.IsReference() )
  4302. {
  4303. // reference to handle -> object
  4304. // handle -> object
  4305. // reference -> object
  4306. // An implicit handle can be converted to an object by adding a check for null pointer
  4307. if( ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  4308. {
  4309. if( generateCode )
  4310. ctx->bc.Instr(asBC_CHKREF);
  4311. ctx->type.dataType.MakeHandle(false);
  4312. }
  4313. // A const object can be converted to a non-const object through a copy
  4314. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() &&
  4315. allowObjectConstruct )
  4316. {
  4317. // Does the object type allow a copy to be made?
  4318. if( ctx->type.dataType.CanBeCopied() )
  4319. {
  4320. if( generateCode )
  4321. {
  4322. // Make a temporary object with the copy
  4323. PrepareTemporaryObject(node, ctx);
  4324. }
  4325. // In case the object was already in a temporary variable, then the function
  4326. // didn't really do anything so we need to remove the constness here
  4327. ctx->type.dataType.MakeReadOnly(false);
  4328. // Add the cost for the copy
  4329. cost += asCC_TO_OBJECT_CONV;
  4330. }
  4331. }
  4332. if( ctx->type.dataType.IsReference() )
  4333. {
  4334. // This may look strange, but a value type allocated on the stack is already
  4335. // correct, so nothing should be done other than remove the mark as reference.
  4336. // For types allocated on the heap, it is necessary to dereference the pointer
  4337. // that is currently on the stack
  4338. if( IsVariableOnHeap(ctx->type.stackOffset) )
  4339. Dereference(ctx, generateCode);
  4340. else
  4341. ctx->type.dataType.MakeReference(false);
  4342. }
  4343. // A non-const object can be converted to a const object directly
  4344. if( !ctx->type.dataType.IsReadOnly() && to.IsReadOnly() )
  4345. {
  4346. ctx->type.dataType.MakeReadOnly(true);
  4347. }
  4348. }
  4349. else
  4350. {
  4351. // reference to handle -> reference
  4352. // handle -> reference
  4353. // object -> reference
  4354. if( ctx->type.dataType.IsReference() )
  4355. {
  4356. if( ctx->type.isExplicitHandle && ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  4357. {
  4358. // ASHANDLE objects are really value types, so explicit handle can be removed
  4359. ctx->type.isExplicitHandle = false;
  4360. ctx->type.dataType.MakeHandle(false);
  4361. }
  4362. // A reference to a handle can be converted to a reference to an object
  4363. // by first reading the address, then verifying that it is not null
  4364. if( !to.IsObjectHandle() && ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  4365. {
  4366. ctx->type.dataType.MakeHandle(false);
  4367. if( generateCode )
  4368. ctx->bc.Instr(asBC_ChkRefS);
  4369. }
  4370. // A reference to a non-const can be converted to a reference to a const
  4371. if( to.IsReadOnly() )
  4372. ctx->type.dataType.MakeReadOnly(true);
  4373. else if( ctx->type.dataType.IsReadOnly() )
  4374. {
  4375. // A reference to a const can be converted to a reference to a
  4376. // non-const by copying the object to a temporary variable
  4377. ctx->type.dataType.MakeReadOnly(false);
  4378. if( generateCode )
  4379. {
  4380. // If the object already is a temporary variable, then the copy
  4381. // doesn't have to be made as it is already a unique object
  4382. PrepareTemporaryObject(node, ctx);
  4383. }
  4384. // Add the cost for the copy
  4385. cost += asCC_TO_OBJECT_CONV;
  4386. }
  4387. }
  4388. else
  4389. {
  4390. // A value type allocated on the stack is differentiated
  4391. // by it not being a reference. But it can be handled as
  4392. // reference by pushing the pointer on the stack
  4393. if( (ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_VALUE) &&
  4394. (ctx->type.isVariable || ctx->type.isTemporary) &&
  4395. !IsVariableOnHeap(ctx->type.stackOffset) )
  4396. {
  4397. // Actually the pointer is already pushed on the stack in
  4398. // CompileVariableAccess, so we don't need to do anything else
  4399. }
  4400. else if( generateCode )
  4401. {
  4402. // A non-reference can be converted to a reference,
  4403. // by putting the value in a temporary variable
  4404. // If the input type is read-only we'll need to temporarily
  4405. // remove this constness, otherwise the assignment will fail
  4406. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  4407. ctx->type.dataType.MakeReadOnly(false);
  4408. // If the object already is a temporary variable, then the copy
  4409. // doesn't have to be made as it is already a unique object
  4410. PrepareTemporaryObject(node, ctx);
  4411. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  4412. // Add the cost for the copy
  4413. cost += asCC_TO_OBJECT_CONV;
  4414. }
  4415. // A handle can be converted to a reference, by checking for a null pointer
  4416. if( ctx->type.dataType.IsObjectHandle() )
  4417. {
  4418. if( generateCode )
  4419. ctx->bc.InstrSHORT(asBC_ChkNullV, ctx->type.stackOffset);
  4420. ctx->type.dataType.MakeHandle(false);
  4421. ctx->type.dataType.MakeReference(true);
  4422. // TODO: Make sure a handle to const isn't converted to non-const reference
  4423. }
  4424. else
  4425. {
  4426. // This may look strange as the conversion was to make the expression a reference
  4427. // but a value type allocated on the stack is a reference even without the type
  4428. // being marked as such.
  4429. ctx->type.dataType.MakeReference(IsVariableOnHeap(ctx->type.stackOffset));
  4430. }
  4431. // TODO: If the variable is an object allocated on the stack the following is not true as the copy may not have been made
  4432. // Since it is a new temporary variable it doesn't have to be const
  4433. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4434. }
  4435. }
  4436. }
  4437. return cost;
  4438. }
  4439. asUINT asCCompiler::ImplicitConvPrimitiveToObject(asSExprContext * /*ctx*/, const asCDataType & /*to*/, asCScriptNode * /*node*/, EImplicitConv /*isExplicit*/, bool /*generateCode*/, bool /*allowObjectConstruct*/)
  4440. {
  4441. // TODO: This function should call the constructor/factory that has been marked as available
  4442. // for implicit conversions. The code will likely be similar to CallCopyConstructor()
  4443. return asCC_NO_CONV;
  4444. }
  4445. void asCCompiler::ImplicitConversionConstant(asSExprContext *from, const asCDataType &to, asCScriptNode *node, EImplicitConv convType)
  4446. {
  4447. asASSERT(from->type.isConstant);
  4448. // TODO: node should be the node of the value that is
  4449. // converted (not the operator that provokes the implicit
  4450. // conversion)
  4451. // If the base type is correct there is no more to do
  4452. if( to.IsEqualExceptRefAndConst(from->type.dataType) ) return;
  4453. // References cannot be constants
  4454. if( from->type.dataType.IsReference() ) return;
  4455. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1) ||
  4456. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  4457. {
  4458. if( from->type.dataType.IsFloatType() ||
  4459. from->type.dataType.IsDoubleType() ||
  4460. from->type.dataType.IsUnsignedType() ||
  4461. from->type.dataType.IsIntegerType() ||
  4462. from->type.dataType.IsEnumType() )
  4463. {
  4464. // Transform the value
  4465. // Float constants can be implicitly converted to int
  4466. if( from->type.dataType.IsFloatType() )
  4467. {
  4468. float fc = from->type.floatValue;
  4469. int ic = int(fc);
  4470. if( float(ic) != fc )
  4471. {
  4472. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4473. }
  4474. from->type.intValue = ic;
  4475. }
  4476. // Double constants can be implicitly converted to int
  4477. else if( from->type.dataType.IsDoubleType() )
  4478. {
  4479. double fc = from->type.doubleValue;
  4480. int ic = int(fc);
  4481. if( double(ic) != fc )
  4482. {
  4483. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4484. }
  4485. from->type.intValue = ic;
  4486. }
  4487. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4488. {
  4489. // Verify that it is possible to convert to signed without getting negative
  4490. if( from->type.intValue < 0 )
  4491. {
  4492. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4493. }
  4494. // Convert to 32bit
  4495. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4496. from->type.intValue = from->type.byteValue;
  4497. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4498. from->type.intValue = from->type.wordValue;
  4499. }
  4500. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4501. {
  4502. // Convert to 32bit
  4503. from->type.intValue = int(from->type.qwordValue);
  4504. }
  4505. else if( from->type.dataType.IsIntegerType() &&
  4506. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  4507. {
  4508. // Convert to 32bit
  4509. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4510. from->type.intValue = (signed char)from->type.byteValue;
  4511. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4512. from->type.intValue = (short)from->type.wordValue;
  4513. }
  4514. else if( from->type.dataType.IsEnumType() )
  4515. {
  4516. // Enum type is already an integer type
  4517. }
  4518. // Set the resulting type
  4519. if( to.IsEnumType() )
  4520. from->type.dataType = to;
  4521. else
  4522. from->type.dataType = asCDataType::CreatePrimitive(ttInt, true);
  4523. }
  4524. // Check if a downsize is necessary
  4525. if( to.IsIntegerType() &&
  4526. from->type.dataType.IsIntegerType() &&
  4527. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  4528. {
  4529. // Verify if it is possible
  4530. if( to.GetSizeInMemoryBytes() == 1 )
  4531. {
  4532. if( char(from->type.intValue) != from->type.intValue )
  4533. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4534. from->type.byteValue = char(from->type.intValue);
  4535. }
  4536. else if( to.GetSizeInMemoryBytes() == 2 )
  4537. {
  4538. if( short(from->type.intValue) != from->type.intValue )
  4539. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4540. from->type.wordValue = short(from->type.intValue);
  4541. }
  4542. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4543. }
  4544. }
  4545. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  4546. {
  4547. // Float constants can be implicitly converted to int
  4548. if( from->type.dataType.IsFloatType() )
  4549. {
  4550. float fc = from->type.floatValue;
  4551. asINT64 ic = asINT64(fc);
  4552. if( float(ic) != fc )
  4553. {
  4554. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4555. }
  4556. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4557. from->type.qwordValue = ic;
  4558. }
  4559. // Double constants can be implicitly converted to int
  4560. else if( from->type.dataType.IsDoubleType() )
  4561. {
  4562. double fc = from->type.doubleValue;
  4563. asINT64 ic = asINT64(fc);
  4564. if( double(ic) != fc )
  4565. {
  4566. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4567. }
  4568. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4569. from->type.qwordValue = ic;
  4570. }
  4571. else if( from->type.dataType.IsUnsignedType() )
  4572. {
  4573. // Convert to 64bit
  4574. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4575. from->type.qwordValue = from->type.byteValue;
  4576. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4577. from->type.qwordValue = from->type.wordValue;
  4578. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4579. from->type.qwordValue = from->type.dwordValue;
  4580. else if( from->type.dataType.GetSizeInMemoryBytes() == 8 )
  4581. {
  4582. if( asINT64(from->type.qwordValue) < 0 )
  4583. {
  4584. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4585. }
  4586. }
  4587. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4588. }
  4589. else if( from->type.dataType.IsEnumType() )
  4590. {
  4591. from->type.qwordValue = from->type.intValue;
  4592. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4593. }
  4594. else if( from->type.dataType.IsIntegerType() )
  4595. {
  4596. // Convert to 64bit
  4597. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4598. from->type.qwordValue = (signed char)from->type.byteValue;
  4599. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4600. from->type.qwordValue = (short)from->type.wordValue;
  4601. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4602. from->type.qwordValue = from->type.intValue;
  4603. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  4604. }
  4605. }
  4606. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  4607. {
  4608. if( from->type.dataType.IsFloatType() )
  4609. {
  4610. float fc = from->type.floatValue;
  4611. // Some compilers set the value to 0 when converting a negative float to unsigned int.
  4612. // To maintain a consistent behaviour across compilers we convert to int first.
  4613. asUINT uic = asUINT(int(fc));
  4614. if( float(uic) != fc )
  4615. {
  4616. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4617. }
  4618. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4619. from->type.intValue = uic;
  4620. // Try once more, in case of a smaller type
  4621. ImplicitConversionConstant(from, to, node, convType);
  4622. }
  4623. else if( from->type.dataType.IsDoubleType() )
  4624. {
  4625. double fc = from->type.doubleValue;
  4626. // Some compilers set the value to 0 when converting a negative double to unsigned int.
  4627. // To maintain a consistent behaviour across compilers we convert to int first.
  4628. asUINT uic = asUINT(int(fc));
  4629. if( double(uic) != fc )
  4630. {
  4631. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4632. }
  4633. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4634. from->type.intValue = uic;
  4635. // Try once more, in case of a smaller type
  4636. ImplicitConversionConstant(from, to, node, convType);
  4637. }
  4638. else if( from->type.dataType.IsEnumType() )
  4639. {
  4640. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4641. // Try once more, in case of a smaller type
  4642. ImplicitConversionConstant(from, to, node, convType);
  4643. }
  4644. else if( from->type.dataType.IsIntegerType() )
  4645. {
  4646. // Verify that it is possible to convert to unsigned without loosing negative
  4647. if( from->type.intValue < 0 )
  4648. {
  4649. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4650. }
  4651. // Convert to 32bit
  4652. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4653. from->type.intValue = (signed char)from->type.byteValue;
  4654. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4655. from->type.intValue = (short)from->type.wordValue;
  4656. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4657. // Try once more, in case of a smaller type
  4658. ImplicitConversionConstant(from, to, node, convType);
  4659. }
  4660. else if( from->type.dataType.IsUnsignedType() &&
  4661. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  4662. {
  4663. // Convert to 32bit
  4664. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4665. from->type.dwordValue = from->type.byteValue;
  4666. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4667. from->type.dwordValue = from->type.wordValue;
  4668. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  4669. // Try once more, in case of a smaller type
  4670. ImplicitConversionConstant(from, to, node, convType);
  4671. }
  4672. else if( from->type.dataType.IsUnsignedType() &&
  4673. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  4674. {
  4675. // Verify if it is possible
  4676. if( to.GetSizeInMemoryBytes() == 1 )
  4677. {
  4678. if( asBYTE(from->type.dwordValue) != from->type.dwordValue )
  4679. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4680. from->type.byteValue = asBYTE(from->type.dwordValue);
  4681. }
  4682. else if( to.GetSizeInMemoryBytes() == 2 )
  4683. {
  4684. if( asWORD(from->type.dwordValue) != from->type.dwordValue )
  4685. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  4686. from->type.wordValue = asWORD(from->type.dwordValue);
  4687. }
  4688. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4689. }
  4690. }
  4691. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  4692. {
  4693. if( from->type.dataType.IsFloatType() )
  4694. {
  4695. float fc = from->type.floatValue;
  4696. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  4697. asQWORD uic = asQWORD(asINT64(fc));
  4698. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  4699. // MSVC6 doesn't support this conversion
  4700. if( float(uic) != fc )
  4701. {
  4702. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4703. }
  4704. #endif
  4705. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4706. from->type.qwordValue = uic;
  4707. }
  4708. else if( from->type.dataType.IsDoubleType() )
  4709. {
  4710. double fc = from->type.doubleValue;
  4711. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  4712. asQWORD uic = asQWORD(asINT64(fc));
  4713. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  4714. // MSVC6 doesn't support this conversion
  4715. if( double(uic) != fc )
  4716. {
  4717. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4718. }
  4719. #endif
  4720. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4721. from->type.qwordValue = uic;
  4722. }
  4723. else if( from->type.dataType.IsEnumType() )
  4724. {
  4725. from->type.qwordValue = (asINT64)from->type.intValue;
  4726. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4727. }
  4728. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4729. {
  4730. // Convert to 64bit
  4731. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4732. from->type.qwordValue = (asINT64)(signed char)from->type.byteValue;
  4733. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4734. from->type.qwordValue = (asINT64)(short)from->type.wordValue;
  4735. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4736. from->type.qwordValue = (asINT64)from->type.intValue;
  4737. // Verify that it is possible to convert to unsigned without loosing negative
  4738. if( asINT64(from->type.qwordValue) < 0 )
  4739. {
  4740. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4741. }
  4742. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4743. }
  4744. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4745. {
  4746. // Verify that it is possible to convert to unsigned without loosing negative
  4747. if( asINT64(from->type.qwordValue) < 0 )
  4748. {
  4749. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  4750. }
  4751. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4752. }
  4753. else if( from->type.dataType.IsUnsignedType() )
  4754. {
  4755. // Convert to 64bit
  4756. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4757. from->type.qwordValue = from->type.byteValue;
  4758. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4759. from->type.qwordValue = from->type.wordValue;
  4760. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  4761. from->type.qwordValue = from->type.dwordValue;
  4762. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  4763. }
  4764. }
  4765. else if( to.IsFloatType() )
  4766. {
  4767. if( from->type.dataType.IsDoubleType() )
  4768. {
  4769. double ic = from->type.doubleValue;
  4770. float fc = float(ic);
  4771. // Don't bother warning about this
  4772. // if( double(fc) != ic )
  4773. // {
  4774. // asCString str;
  4775. // str.Format(TXT_POSSIBLE_LOSS_OF_PRECISION);
  4776. // if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(str.AddressOf(), node);
  4777. // }
  4778. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4779. from->type.floatValue = fc;
  4780. }
  4781. else if( from->type.dataType.IsEnumType() )
  4782. {
  4783. float fc = float(from->type.intValue);
  4784. if( int(fc) != from->type.intValue )
  4785. {
  4786. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4787. }
  4788. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4789. from->type.floatValue = fc;
  4790. }
  4791. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4792. {
  4793. // Must properly convert value in case the from value is smaller
  4794. int ic;
  4795. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4796. ic = (signed char)from->type.byteValue;
  4797. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4798. ic = (short)from->type.wordValue;
  4799. else
  4800. ic = from->type.intValue;
  4801. float fc = float(ic);
  4802. if( int(fc) != ic )
  4803. {
  4804. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4805. }
  4806. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4807. from->type.floatValue = fc;
  4808. }
  4809. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4810. {
  4811. float fc = float(asINT64(from->type.qwordValue));
  4812. if( asINT64(fc) != asINT64(from->type.qwordValue) )
  4813. {
  4814. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4815. }
  4816. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4817. from->type.floatValue = fc;
  4818. }
  4819. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4820. {
  4821. // Must properly convert value in case the from value is smaller
  4822. unsigned int uic;
  4823. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4824. uic = from->type.byteValue;
  4825. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4826. uic = from->type.wordValue;
  4827. else
  4828. uic = from->type.dwordValue;
  4829. float fc = float(uic);
  4830. if( (unsigned int)(fc) != uic )
  4831. {
  4832. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4833. }
  4834. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4835. from->type.floatValue = fc;
  4836. }
  4837. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4838. {
  4839. float fc = float((asINT64)from->type.qwordValue);
  4840. if( asQWORD(fc) != from->type.qwordValue )
  4841. {
  4842. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4843. }
  4844. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4845. from->type.floatValue = fc;
  4846. }
  4847. }
  4848. else if( to.IsDoubleType() )
  4849. {
  4850. if( from->type.dataType.IsFloatType() )
  4851. {
  4852. float ic = from->type.floatValue;
  4853. double fc = double(ic);
  4854. // Don't check for float->double
  4855. // if( float(fc) != ic )
  4856. // {
  4857. // acCString str;
  4858. // str.Format(TXT_NOT_EXACT_g_g_g, ic, fc, float(fc));
  4859. // if( !isExplicit ) Warning(str, node);
  4860. // }
  4861. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4862. from->type.doubleValue = fc;
  4863. }
  4864. else if( from->type.dataType.IsEnumType() )
  4865. {
  4866. double fc = double(from->type.intValue);
  4867. if( int(fc) != from->type.intValue )
  4868. {
  4869. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4870. }
  4871. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4872. from->type.doubleValue = fc;
  4873. }
  4874. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4875. {
  4876. // Must properly convert value in case the from value is smaller
  4877. int ic;
  4878. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4879. ic = (signed char)from->type.byteValue;
  4880. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4881. ic = (short)from->type.wordValue;
  4882. else
  4883. ic = from->type.intValue;
  4884. double fc = double(ic);
  4885. if( int(fc) != ic )
  4886. {
  4887. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4888. }
  4889. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4890. from->type.doubleValue = fc;
  4891. }
  4892. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4893. {
  4894. double fc = double(asINT64(from->type.qwordValue));
  4895. if( asINT64(fc) != asINT64(from->type.qwordValue) )
  4896. {
  4897. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4898. }
  4899. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4900. from->type.doubleValue = fc;
  4901. }
  4902. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  4903. {
  4904. // Must properly convert value in case the from value is smaller
  4905. unsigned int uic;
  4906. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  4907. uic = from->type.byteValue;
  4908. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  4909. uic = from->type.wordValue;
  4910. else
  4911. uic = from->type.dwordValue;
  4912. double fc = double(uic);
  4913. if( (unsigned int)(fc) != uic )
  4914. {
  4915. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4916. }
  4917. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4918. from->type.doubleValue = fc;
  4919. }
  4920. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  4921. {
  4922. double fc = double((asINT64)from->type.qwordValue);
  4923. if( asQWORD(fc) != from->type.qwordValue )
  4924. {
  4925. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  4926. }
  4927. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  4928. from->type.doubleValue = fc;
  4929. }
  4930. }
  4931. }
  4932. int asCCompiler::DoAssignment(asSExprContext *ctx, asSExprContext *lctx, asSExprContext *rctx, asCScriptNode *lexpr, asCScriptNode *rexpr, int op, asCScriptNode *opNode)
  4933. {
  4934. // Implicit handle types should always be treated as handles in assignments
  4935. if (lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE) )
  4936. {
  4937. lctx->type.dataType.MakeHandle(true);
  4938. lctx->type.isExplicitHandle = true;
  4939. }
  4940. // Urho3D: if there is a handle type, and it does not have an overloaded assignment operator, convert to an explicit handle
  4941. // for scripting convenience. (For the Urho3D handle types, value assignment is not supported)
  4942. if (lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle && !lctx->type.dataType.GetBehaviour()->copy)
  4943. lctx->type.isExplicitHandle = true;
  4944. // If the left hand expression is a property accessor, then that should be used
  4945. // to do the assignment instead of the ordinary operator. The exception is when
  4946. // the property accessor is for a handle property, and the operation is a value
  4947. // assignment.
  4948. if( (lctx->property_get || lctx->property_set) &&
  4949. !(lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle) )
  4950. {
  4951. if( op != ttAssignment )
  4952. {
  4953. // TODO: getset: We may actually be able to support this, if we can
  4954. // guarantee that the object reference will stay valid
  4955. // between the calls to the get and set accessors.
  4956. // Process the property to free the memory
  4957. ProcessPropertySetAccessor(lctx, rctx, opNode);
  4958. // Compound assignments are not allowed for properties
  4959. Error(TXT_COMPOUND_ASGN_WITH_PROP, opNode);
  4960. return -1;
  4961. }
  4962. // It is not allowed to do a handle assignment on a property
  4963. // accessor that doesn't take a handle in the set accessor.
  4964. if( lctx->property_set && lctx->type.isExplicitHandle )
  4965. {
  4966. // set_opIndex has 2 arguments, where as normal setters have only 1
  4967. asCArray<asCDataType>& parameterTypes =
  4968. builder->GetFunctionDescription(lctx->property_set)->parameterTypes;
  4969. if( !parameterTypes[parameterTypes.GetLength() - 1].IsObjectHandle() )
  4970. {
  4971. // Process the property to free the memory
  4972. ProcessPropertySetAccessor(lctx, rctx, opNode);
  4973. Error(TXT_HANDLE_ASSIGN_ON_NON_HANDLE_PROP, opNode);
  4974. return -1;
  4975. }
  4976. }
  4977. MergeExprBytecodeAndType(ctx, lctx);
  4978. return ProcessPropertySetAccessor(ctx, rctx, opNode);
  4979. }
  4980. else if( lctx->property_get && lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  4981. {
  4982. // Get the handle to the object that will be used for the value assignment
  4983. ProcessPropertyGetAccessor(lctx, opNode);
  4984. }
  4985. if( lctx->type.dataType.IsPrimitive() )
  4986. {
  4987. if( !lctx->type.isLValue )
  4988. {
  4989. Error(TXT_NOT_LVALUE, lexpr);
  4990. return -1;
  4991. }
  4992. if( op != ttAssignment )
  4993. {
  4994. // Compute the operator before the assignment
  4995. asCTypeInfo lvalue = lctx->type;
  4996. if( lctx->type.isTemporary && !lctx->type.isVariable )
  4997. {
  4998. // The temporary variable must not be freed until the
  4999. // assignment has been performed. lvalue still holds
  5000. // the information about the temporary variable
  5001. lctx->type.isTemporary = false;
  5002. }
  5003. asSExprContext o(engine);
  5004. CompileOperator(opNode, lctx, rctx, &o);
  5005. MergeExprBytecode(rctx, &o);
  5006. rctx->type = o.type;
  5007. // Convert the rvalue to the right type and validate it
  5008. PrepareForAssignment(&lvalue.dataType, rctx, rexpr, false);
  5009. MergeExprBytecode(ctx, rctx);
  5010. lctx->type = lvalue;
  5011. // The lvalue continues the same, either it was a variable, or a reference in the register
  5012. }
  5013. else
  5014. {
  5015. // Convert the rvalue to the right type and validate it
  5016. PrepareForAssignment(&lctx->type.dataType, rctx, rexpr, false, lctx);
  5017. MergeExprBytecode(ctx, rctx);
  5018. MergeExprBytecode(ctx, lctx);
  5019. }
  5020. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  5021. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  5022. ctx->type = lctx->type;
  5023. }
  5024. else if( lctx->type.isExplicitHandle )
  5025. {
  5026. if( !lctx->type.isLValue )
  5027. {
  5028. Error(TXT_NOT_LVALUE, lexpr);
  5029. return -1;
  5030. }
  5031. // Object handles don't have any compound assignment operators
  5032. if( op != ttAssignment )
  5033. {
  5034. asCString str;
  5035. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  5036. Error(str.AddressOf(), lexpr);
  5037. return -1;
  5038. }
  5039. if( lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  5040. {
  5041. // The object is a value type but that should be treated as a handle
  5042. // TODO: handle: Make sure the right hand value is a handle
  5043. if( CompileOverloadedDualOperator(opNode, lctx, rctx, ctx) )
  5044. {
  5045. // An overloaded assignment operator was found (or a compilation error occured)
  5046. return 0;
  5047. }
  5048. // The object must implement the opAssign method
  5049. Error(TXT_NO_APPROPRIATE_OPASSIGN, opNode);
  5050. return -1;
  5051. }
  5052. else
  5053. {
  5054. asCDataType dt = lctx->type.dataType;
  5055. dt.MakeReference(false);
  5056. PrepareArgument(&dt, rctx, rexpr, true, 1);
  5057. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5058. {
  5059. asCString str;
  5060. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  5061. Error(str.AddressOf(), rexpr);
  5062. return -1;
  5063. }
  5064. MergeExprBytecode(ctx, rctx);
  5065. MergeExprBytecode(ctx, lctx);
  5066. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  5067. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  5068. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  5069. ctx->type = rctx->type;
  5070. }
  5071. }
  5072. else // if( lctx->type.dataType.IsObject() )
  5073. {
  5074. // An ASHANDLE type must not allow a value assignment, as
  5075. // the opAssign operator is used for the handle assignment
  5076. if( lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  5077. {
  5078. asCString str;
  5079. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  5080. Error(str.AddressOf(), lexpr);
  5081. return -1;
  5082. }
  5083. // The lvalue reference may be marked as a temporary, if for example
  5084. // it was originated as a handle returned from a function. In such
  5085. // cases it must be possible to assign values to it anyway.
  5086. if( lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  5087. {
  5088. // Convert the handle to a object reference
  5089. asCDataType to;
  5090. to = lctx->type.dataType;
  5091. to.MakeHandle(false);
  5092. ImplicitConversion(lctx, to, lexpr, asIC_IMPLICIT_CONV);
  5093. lctx->type.isLValue = true; // Handle may not have been an lvalue, but the dereferenced object is
  5094. }
  5095. // Check for overloaded assignment operator
  5096. if( CompileOverloadedDualOperator(opNode, lctx, rctx, ctx) )
  5097. {
  5098. // An overloaded assignment operator was found (or a compilation error occured)
  5099. return 0;
  5100. }
  5101. // No registered operator was found. In case the operation is a direct
  5102. // assignment and the rvalue is the same type as the lvalue, then we can
  5103. // still use the byte-for-byte copy to do the assignment
  5104. if( op != ttAssignment )
  5105. {
  5106. asCString str;
  5107. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  5108. Error(str.AddressOf(), lexpr);
  5109. return -1;
  5110. }
  5111. // If the left hand expression is simple, i.e. without any
  5112. // function calls or allocations of memory, then we can avoid
  5113. // doing a copy of the right hand expression (done by PrepareArgument).
  5114. // Instead the reference to the value can be placed directly on the
  5115. // stack.
  5116. //
  5117. // This optimization should only be done for value types, where
  5118. // the application developer is responsible for making the
  5119. // implementation safe against unwanted destruction of the input
  5120. // reference before the time.
  5121. bool simpleExpr = (lctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_VALUE) && lctx->bc.IsSimpleExpression();
  5122. // Implicitly convert the rvalue to the type of the lvalue
  5123. if( !lctx->type.dataType.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5124. simpleExpr = false;
  5125. if( !simpleExpr )
  5126. {
  5127. asCDataType dt = lctx->type.dataType;
  5128. dt.MakeReference(true);
  5129. dt.MakeReadOnly(true);
  5130. PrepareArgument(&dt, rctx, rexpr, true, 1);
  5131. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5132. {
  5133. asCString str;
  5134. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  5135. Error(str.AddressOf(), rexpr);
  5136. return -1;
  5137. }
  5138. }
  5139. else
  5140. {
  5141. // Process any property accessor first, before placing the final reference on the stack
  5142. ProcessPropertyGetAccessor(rctx, rexpr);
  5143. if( rctx->type.dataType.IsReference() && (!(rctx->type.isVariable || rctx->type.isTemporary) || IsVariableOnHeap(rctx->type.stackOffset)) )
  5144. rctx->bc.Instr(asBC_RDSPtr);
  5145. }
  5146. MergeExprBytecode(ctx, rctx);
  5147. MergeExprBytecode(ctx, lctx);
  5148. if( !simpleExpr )
  5149. {
  5150. if( (rctx->type.isVariable || rctx->type.isTemporary) && !IsVariableOnHeap(rctx->type.stackOffset) )
  5151. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  5152. // as the value allocated on the stack is guaranteed to be safe.
  5153. // The bytecode optimizer should be able to determine this and optimize away the VAR + GETREF
  5154. ctx->bc.InstrWORD(asBC_GETREF, AS_PTR_SIZE);
  5155. else
  5156. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  5157. }
  5158. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  5159. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  5160. ctx->type = lctx->type;
  5161. }
  5162. return 0;
  5163. }
  5164. int asCCompiler::CompileAssignment(asCScriptNode *expr, asSExprContext *ctx)
  5165. {
  5166. asCScriptNode *lexpr = expr->firstChild;
  5167. if( lexpr->next )
  5168. {
  5169. // Compile the two expression terms
  5170. asSExprContext lctx(engine), rctx(engine);
  5171. int rr = CompileAssignment(lexpr->next->next, &rctx);
  5172. int lr = CompileCondition(lexpr, &lctx);
  5173. if( lr >= 0 && rr >= 0 )
  5174. return DoAssignment(ctx, &lctx, &rctx, lexpr, lexpr->next->next, lexpr->next->tokenType, lexpr->next);
  5175. // Since the operands failed, the assignment was not computed
  5176. ctx->type.SetDummy();
  5177. return -1;
  5178. }
  5179. return CompileCondition(lexpr, ctx);
  5180. }
  5181. int asCCompiler::CompileCondition(asCScriptNode *expr, asSExprContext *ctx)
  5182. {
  5183. asCTypeInfo ctype;
  5184. // Compile the conditional expression
  5185. asCScriptNode *cexpr = expr->firstChild;
  5186. if( cexpr->next )
  5187. {
  5188. //-------------------------------
  5189. // Compile the condition
  5190. asSExprContext e(engine);
  5191. int r = CompileExpression(cexpr, &e);
  5192. if( r < 0 )
  5193. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  5194. if( r >= 0 && !e.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  5195. {
  5196. Error(TXT_EXPR_MUST_BE_BOOL, cexpr);
  5197. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  5198. }
  5199. ctype = e.type;
  5200. ProcessPropertyGetAccessor(&e, cexpr);
  5201. if( e.type.dataType.IsReference() ) ConvertToVariable(&e);
  5202. ProcessDeferredParams(&e);
  5203. //-------------------------------
  5204. // Compile the left expression
  5205. asSExprContext le(engine);
  5206. int lr = CompileAssignment(cexpr->next, &le);
  5207. //-------------------------------
  5208. // Compile the right expression
  5209. asSExprContext re(engine);
  5210. int rr = CompileAssignment(cexpr->next->next, &re);
  5211. if( lr >= 0 && rr >= 0 )
  5212. {
  5213. ProcessPropertyGetAccessor(&le, cexpr->next);
  5214. ProcessPropertyGetAccessor(&re, cexpr->next->next);
  5215. bool isExplicitHandle = le.type.isExplicitHandle || re.type.isExplicitHandle;
  5216. // Allow a 0 or null in the first case to be implicitly converted to the second type
  5217. if( le.type.isConstant && le.type.intValue == 0 && le.type.dataType.IsUnsignedType() )
  5218. {
  5219. asCDataType to = re.type.dataType;
  5220. to.MakeReference(false);
  5221. to.MakeReadOnly(true);
  5222. ImplicitConversionConstant(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  5223. }
  5224. else if( le.type.IsNullConstant() )
  5225. {
  5226. asCDataType to = re.type.dataType;
  5227. to.MakeHandle(true);
  5228. ImplicitConversion(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  5229. }
  5230. //---------------------------------
  5231. // Output the byte code
  5232. int afterLabel = nextLabel++;
  5233. int elseLabel = nextLabel++;
  5234. // If left expression is void, then we don't need to store the result
  5235. if( le.type.dataType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  5236. {
  5237. // Put the code for the condition expression on the output
  5238. MergeExprBytecode(ctx, &e);
  5239. // Added the branch decision
  5240. ctx->type = e.type;
  5241. ConvertToVariable(ctx);
  5242. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  5243. ctx->bc.Instr(asBC_ClrHi);
  5244. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  5245. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5246. // Add the left expression
  5247. MergeExprBytecode(ctx, &le);
  5248. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  5249. // Add the right expression
  5250. ctx->bc.Label((short)elseLabel);
  5251. MergeExprBytecode(ctx, &re);
  5252. ctx->bc.Label((short)afterLabel);
  5253. // Make sure both expressions have the same type
  5254. if( le.type.dataType != re.type.dataType )
  5255. Error(TXT_BOTH_MUST_BE_SAME, expr);
  5256. // Set the type of the result
  5257. ctx->type = le.type;
  5258. }
  5259. else
  5260. {
  5261. // Allocate temporary variable and copy the result to that one
  5262. asCTypeInfo temp;
  5263. temp = le.type;
  5264. temp.dataType.MakeReference(false);
  5265. temp.dataType.MakeReadOnly(false);
  5266. // Make sure the variable isn't used in the initial expression
  5267. int offset = AllocateVariableNotIn(temp.dataType, true, false, &e);
  5268. temp.SetVariable(temp.dataType, offset, true);
  5269. // TODO: copy: Use copy constructor if available. See PrepareTemporaryObject()
  5270. CallDefaultConstructor(temp.dataType, offset, IsVariableOnHeap(offset), &ctx->bc, expr);
  5271. // Put the code for the condition expression on the output
  5272. MergeExprBytecode(ctx, &e);
  5273. // Add the branch decision
  5274. ctx->type = e.type;
  5275. ConvertToVariable(ctx);
  5276. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  5277. ctx->bc.Instr(asBC_ClrHi);
  5278. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  5279. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5280. // Assign the result of the left expression to the temporary variable
  5281. asCTypeInfo rtemp;
  5282. rtemp = temp;
  5283. if( rtemp.dataType.IsObjectHandle() )
  5284. rtemp.isExplicitHandle = true;
  5285. PrepareForAssignment(&rtemp.dataType, &le, cexpr->next, true);
  5286. MergeExprBytecode(ctx, &le);
  5287. if( !rtemp.dataType.IsPrimitive() )
  5288. {
  5289. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5290. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  5291. }
  5292. PerformAssignment(&rtemp, &le.type, &ctx->bc, cexpr->next);
  5293. if( !rtemp.dataType.IsPrimitive() )
  5294. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  5295. // Release the old temporary variable
  5296. ReleaseTemporaryVariable(le.type, &ctx->bc);
  5297. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  5298. // Start of the right expression
  5299. ctx->bc.Label((short)elseLabel);
  5300. // Copy the result to the same temporary variable
  5301. PrepareForAssignment(&rtemp.dataType, &re, cexpr->next, true);
  5302. MergeExprBytecode(ctx, &re);
  5303. if( !rtemp.dataType.IsPrimitive() )
  5304. {
  5305. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5306. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  5307. }
  5308. PerformAssignment(&rtemp, &re.type, &ctx->bc, cexpr->next);
  5309. if( !rtemp.dataType.IsPrimitive() )
  5310. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  5311. // Release the old temporary variable
  5312. ReleaseTemporaryVariable(re.type, &ctx->bc);
  5313. ctx->bc.Label((short)afterLabel);
  5314. // Make sure both expressions have the same type
  5315. if( !le.type.dataType.IsEqualExceptConst(re.type.dataType) )
  5316. Error(TXT_BOTH_MUST_BE_SAME, expr);
  5317. // Set the temporary variable as output
  5318. ctx->type = rtemp;
  5319. ctx->type.isExplicitHandle = isExplicitHandle;
  5320. if( !ctx->type.dataType.IsPrimitive() )
  5321. {
  5322. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5323. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  5324. }
  5325. // Make sure the output isn't marked as being a literal constant
  5326. ctx->type.isConstant = false;
  5327. }
  5328. }
  5329. else
  5330. {
  5331. ctx->type.SetDummy();
  5332. return -1;
  5333. }
  5334. }
  5335. else
  5336. return CompileExpression(cexpr, ctx);
  5337. return 0;
  5338. }
  5339. int asCCompiler::CompileExpression(asCScriptNode *expr, asSExprContext *ctx)
  5340. {
  5341. asASSERT(expr->nodeType == snExpression);
  5342. // Convert to polish post fix, i.e: a+b => ab+
  5343. // The algorithm that I've implemented here is similar to
  5344. // Djikstra's Shunting Yard algorithm, though I didn't know it at the time.
  5345. // ref: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
  5346. // Count the nodes in order to preallocate the buffers
  5347. int count = 0;
  5348. asCScriptNode *node = expr->firstChild;
  5349. while( node )
  5350. {
  5351. count++;
  5352. node = node->next;
  5353. }
  5354. asCArray<asCScriptNode *> stack(count);
  5355. asCArray<asCScriptNode *> stack2(count);
  5356. asCArray<asCScriptNode *> postfix(count);
  5357. node = expr->firstChild;
  5358. while( node )
  5359. {
  5360. int precedence = GetPrecedence(node);
  5361. while( stack.GetLength() > 0 &&
  5362. precedence <= GetPrecedence(stack[stack.GetLength()-1]) )
  5363. stack2.PushLast(stack.PopLast());
  5364. stack.PushLast(node);
  5365. node = node->next;
  5366. }
  5367. while( stack.GetLength() > 0 )
  5368. stack2.PushLast(stack.PopLast());
  5369. // We need to swap operands so that the left
  5370. // operand is always computed before the right
  5371. SwapPostFixOperands(stack2, postfix);
  5372. // Compile the postfix formatted expression
  5373. return CompilePostFixExpression(&postfix, ctx);
  5374. }
  5375. void asCCompiler::SwapPostFixOperands(asCArray<asCScriptNode *> &postfix, asCArray<asCScriptNode *> &target)
  5376. {
  5377. if( postfix.GetLength() == 0 ) return;
  5378. asCScriptNode *node = postfix.PopLast();
  5379. if( node->nodeType == snExprTerm )
  5380. {
  5381. target.PushLast(node);
  5382. return;
  5383. }
  5384. SwapPostFixOperands(postfix, target);
  5385. SwapPostFixOperands(postfix, target);
  5386. target.PushLast(node);
  5387. }
  5388. int asCCompiler::CompilePostFixExpression(asCArray<asCScriptNode *> *postfix, asSExprContext *ctx)
  5389. {
  5390. // Shouldn't send any byte code
  5391. asASSERT(ctx->bc.GetLastInstr() == -1);
  5392. // Set the context to a dummy type to avoid further
  5393. // errors in case the expression fails to compile
  5394. ctx->type.SetDummy();
  5395. // Pop the last node
  5396. asCScriptNode *node = postfix->PopLast();
  5397. ctx->exprNode = node;
  5398. // If term, compile the term
  5399. if( node->nodeType == snExprTerm )
  5400. return CompileExpressionTerm(node, ctx);
  5401. // Compile the two expression terms
  5402. asSExprContext r(engine), l(engine);
  5403. int ret;
  5404. ret = CompilePostFixExpression(postfix, &l); if( ret < 0 ) return ret;
  5405. ret = CompilePostFixExpression(postfix, &r); if( ret < 0 ) return ret;
  5406. // Compile the operation
  5407. return CompileOperator(node, &l, &r, ctx);
  5408. }
  5409. int asCCompiler::CompileExpressionTerm(asCScriptNode *node, asSExprContext *ctx)
  5410. {
  5411. // Shouldn't send any byte code
  5412. asASSERT(ctx->bc.GetLastInstr() == -1);
  5413. // Set the type as a dummy by default, in case of any compiler errors
  5414. ctx->type.SetDummy();
  5415. // Compile the value node
  5416. asCScriptNode *vnode = node->firstChild;
  5417. while( vnode->nodeType != snExprValue )
  5418. vnode = vnode->next;
  5419. asSExprContext v(engine);
  5420. int r = CompileExpressionValue(vnode, &v); if( r < 0 ) return r;
  5421. // Compile post fix operators
  5422. asCScriptNode *pnode = vnode->next;
  5423. while( pnode )
  5424. {
  5425. r = CompileExpressionPostOp(pnode, &v); if( r < 0 ) return r;
  5426. pnode = pnode->next;
  5427. }
  5428. // Compile pre fix operators
  5429. pnode = vnode->prev;
  5430. while( pnode )
  5431. {
  5432. r = CompileExpressionPreOp(pnode, &v); if( r < 0 ) return r;
  5433. pnode = pnode->prev;
  5434. }
  5435. // Return the byte code and final type description
  5436. MergeExprBytecodeAndType(ctx, &v);
  5437. return 0;
  5438. }
  5439. int asCCompiler::CompileVariableAccess(const asCString &name, const asCString &scope, asSExprContext *ctx, asCScriptNode *errNode, bool isOptional, bool noFunction, asCObjectType *objType)
  5440. {
  5441. bool found = false;
  5442. // It is a local variable or parameter?
  5443. // This is not accessible by default arg expressions
  5444. sVariable *v = 0;
  5445. if( !isCompilingDefaultArg && scope == "" && !objType )
  5446. v = variables->GetVariable(name.AddressOf());
  5447. if( v )
  5448. {
  5449. found = true;
  5450. if( v->isPureConstant )
  5451. ctx->type.SetConstantQW(v->type, v->constantValue);
  5452. else if( v->type.IsPrimitive() )
  5453. {
  5454. if( v->type.IsReference() )
  5455. {
  5456. // Copy the reference into the register
  5457. ctx->bc.InstrSHORT(asBC_PshVPtr, (short)v->stackOffset);
  5458. ctx->bc.Instr(asBC_PopRPtr);
  5459. ctx->type.Set(v->type);
  5460. }
  5461. else
  5462. ctx->type.SetVariable(v->type, v->stackOffset, false);
  5463. ctx->type.isLValue = true;
  5464. }
  5465. else
  5466. {
  5467. ctx->bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  5468. ctx->type.SetVariable(v->type, v->stackOffset, false);
  5469. // If the variable is allocated on the heap we have a reference,
  5470. // otherwise the actual object pointer is pushed on the stack.
  5471. if( v->onHeap || v->type.IsObjectHandle() ) ctx->type.dataType.MakeReference(true);
  5472. // Implicitly dereference handle parameters sent by reference
  5473. if( v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()) )
  5474. ctx->bc.Instr(asBC_RDSPtr);
  5475. ctx->type.isLValue = true;
  5476. }
  5477. }
  5478. // Is it a class member?
  5479. // This is not accessible by default arg expressions
  5480. if( !isCompilingDefaultArg && !found && ((objType) || (outFunc && outFunc->objectType && scope == "")) )
  5481. {
  5482. if( name == THIS_TOKEN && !objType )
  5483. {
  5484. asCDataType dt = asCDataType::CreateObject(outFunc->objectType, outFunc->isReadOnly);
  5485. // The object pointer is located at stack position 0
  5486. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5487. ctx->type.SetVariable(dt, 0, false);
  5488. ctx->type.dataType.MakeReference(true);
  5489. ctx->type.isLValue = true;
  5490. found = true;
  5491. }
  5492. if( !found )
  5493. {
  5494. // See if there are any matching property accessors
  5495. asSExprContext access(engine);
  5496. if( objType )
  5497. access.type.Set(asCDataType::CreateObject(objType, false));
  5498. else
  5499. access.type.Set(asCDataType::CreateObject(outFunc->objectType, outFunc->isReadOnly));
  5500. access.type.dataType.MakeReference(true);
  5501. int r = 0;
  5502. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  5503. {
  5504. // This is an index access, check if there is a property accessor that takes an index arg
  5505. asSExprContext dummyArg(engine);
  5506. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, true);
  5507. }
  5508. if( r == 0 )
  5509. {
  5510. // Normal property access
  5511. r = FindPropertyAccessor(name, &access, errNode, true);
  5512. }
  5513. if( r < 0 ) return -1;
  5514. if( access.property_get || access.property_set )
  5515. {
  5516. if( !objType )
  5517. {
  5518. // Prepare the bytecode for the member access
  5519. // This is only done when accessing through the implicit this pointer
  5520. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5521. }
  5522. MergeExprBytecodeAndType(ctx, &access);
  5523. found = true;
  5524. }
  5525. }
  5526. if( !found )
  5527. {
  5528. asCDataType dt;
  5529. if( objType )
  5530. dt = asCDataType::CreateObject(objType, false);
  5531. else
  5532. dt = asCDataType::CreateObject(outFunc->objectType, false);
  5533. asCObjectProperty *prop = builder->GetObjectProperty(dt, name.AddressOf());
  5534. if( prop )
  5535. {
  5536. if( !objType )
  5537. {
  5538. // The object pointer is located at stack position 0
  5539. // This is only done when accessing through the implicit this pointer
  5540. ctx->bc.InstrSHORT(asBC_PSF, 0);
  5541. ctx->type.SetVariable(dt, 0, false);
  5542. ctx->type.dataType.MakeReference(true);
  5543. Dereference(ctx, true);
  5544. }
  5545. // TODO: This is the same as what is in CompileExpressionPostOp
  5546. // Put the offset on the stack
  5547. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(dt));
  5548. if( prop->type.IsReference() )
  5549. ctx->bc.Instr(asBC_RDSPtr);
  5550. // Reference to primitive must be stored in the temp register
  5551. if( prop->type.IsPrimitive() )
  5552. {
  5553. // TODO: runtime optimize: The ADD offset command should store the reference in the register directly
  5554. ctx->bc.Instr(asBC_PopRPtr);
  5555. }
  5556. // Set the new type (keeping info about temp variable)
  5557. ctx->type.dataType = prop->type;
  5558. ctx->type.dataType.MakeReference(true);
  5559. ctx->type.isVariable = false;
  5560. ctx->type.isLValue = true;
  5561. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  5562. {
  5563. // Objects that are members are not references
  5564. ctx->type.dataType.MakeReference(false);
  5565. }
  5566. // If the object reference is const, the property will also be const
  5567. ctx->type.dataType.MakeReadOnly(outFunc->isReadOnly);
  5568. found = true;
  5569. }
  5570. }
  5571. }
  5572. // Is it a global property?
  5573. if( !found && !objType )
  5574. {
  5575. // See if there are any matching global property accessors
  5576. // TODO: namespace: Support namespaces for global property accessors too
  5577. asSExprContext access(engine);
  5578. int r = 0;
  5579. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  5580. {
  5581. // This is an index access, check if there is a property accessor that takes an index arg
  5582. asSExprContext dummyArg(engine);
  5583. r = FindPropertyAccessor(name, &access, &dummyArg, errNode);
  5584. }
  5585. if( r == 0 )
  5586. {
  5587. // Normal property access
  5588. r = FindPropertyAccessor(name, &access, errNode);
  5589. }
  5590. if( r < 0 ) return -1;
  5591. if( access.property_get || access.property_set )
  5592. {
  5593. // Prepare the bytecode for the function call
  5594. MergeExprBytecodeAndType(ctx, &access);
  5595. found = true;
  5596. }
  5597. // See if there is any matching global property
  5598. if( !found )
  5599. {
  5600. bool isCompiled = true;
  5601. bool isPureConstant = false;
  5602. bool isAppProp = false;
  5603. asQWORD constantValue;
  5604. asCString ns = scope;
  5605. if( ns == "" )
  5606. {
  5607. if( outFunc->nameSpace != "" )
  5608. ns = outFunc->nameSpace;
  5609. else if( outFunc->objectType && outFunc->objectType->nameSpace != "" )
  5610. ns = outFunc->objectType->nameSpace;
  5611. }
  5612. else if( ns == "::" )
  5613. ns = "";
  5614. asCGlobalProperty *prop = builder->GetGlobalProperty(name.AddressOf(), ns, &isCompiled, &isPureConstant, &constantValue, &isAppProp);
  5615. if( prop )
  5616. {
  5617. found = true;
  5618. // Verify that the global property has been compiled already
  5619. if( isCompiled )
  5620. {
  5621. if( ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE) )
  5622. {
  5623. ctx->type.dataType.MakeHandle(true);
  5624. ctx->type.isExplicitHandle = true;
  5625. }
  5626. // If the global property is a pure constant
  5627. // we can allow the compiler to optimize it. Pure
  5628. // constants are global constant variables that were
  5629. // initialized by literal constants.
  5630. if( isPureConstant )
  5631. ctx->type.SetConstantQW(prop->type, constantValue);
  5632. else
  5633. {
  5634. // A shared type must not access global vars, unless they
  5635. // too are shared, e.g. application registered vars
  5636. if( outFunc->IsShared() )
  5637. {
  5638. if( !isAppProp )
  5639. {
  5640. asCString str;
  5641. str.Format(TXT_SHARED_CANNOT_ACCESS_NON_SHARED_VAR_s, prop->name.AddressOf());
  5642. Error(str.AddressOf(), errNode);
  5643. // Allow the compilation to continue to catch other problems
  5644. }
  5645. }
  5646. ctx->type.Set(prop->type);
  5647. ctx->type.dataType.MakeReference(true);
  5648. ctx->type.isLValue = true;
  5649. if( ctx->type.dataType.IsPrimitive() )
  5650. {
  5651. // Load the address of the variable into the register
  5652. ctx->bc.InstrPTR(asBC_LDG, prop->GetAddressOfValue());
  5653. }
  5654. else
  5655. {
  5656. // Push the address of the variable on the stack
  5657. ctx->bc.InstrPTR(asBC_PGA, prop->GetAddressOfValue());
  5658. // If the object is a value type, then we must validate the existance,
  5659. // as it could potentially be accessed before it is initialized.
  5660. if( ctx->type.dataType.GetObjectType()->flags & asOBJ_VALUE ||
  5661. !ctx->type.dataType.IsObjectHandle() )
  5662. {
  5663. // TODO: runtime optimize: This is not necessary for application registered properties
  5664. ctx->bc.Instr(asBC_ChkRefS);
  5665. }
  5666. }
  5667. }
  5668. }
  5669. else
  5670. {
  5671. asCString str;
  5672. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, prop->name.AddressOf());
  5673. Error(str.AddressOf(), errNode);
  5674. return -1;
  5675. }
  5676. }
  5677. }
  5678. }
  5679. // Is it the name of a global function?
  5680. if( !noFunction && !found && !objType )
  5681. {
  5682. asCArray<int> funcs;
  5683. asCString ns = scope;
  5684. if( ns == "" )
  5685. {
  5686. if( outFunc->nameSpace != "" )
  5687. ns = outFunc->nameSpace;
  5688. else if( outFunc->objectType && outFunc->objectType->nameSpace != "" )
  5689. ns = outFunc->objectType->nameSpace;
  5690. }
  5691. else if( ns == "::" )
  5692. ns = "";
  5693. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  5694. if( funcs.GetLength() > 1 )
  5695. {
  5696. // TODO: funcdef: If multiple functions are found, then the compiler should defer the decision
  5697. // to which one it should use until the value is actually used.
  5698. //
  5699. // - assigning the function pointer to a variable
  5700. // - performing an explicit cast
  5701. // - passing the function pointer to a function as parameter
  5702. asCString str;
  5703. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, name.AddressOf());
  5704. Error(str.AddressOf(), errNode);
  5705. return -1;
  5706. }
  5707. else if( funcs.GetLength() == 1 )
  5708. {
  5709. found = true;
  5710. // A shared object may not access global functions unless they too are shared (e.g. registered functions)
  5711. if( !builder->GetFunctionDescription(funcs[0])->IsShared() &&
  5712. outFunc->IsShared() )
  5713. {
  5714. asCString msg;
  5715. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, builder->GetFunctionDescription(funcs[0])->GetDeclaration());
  5716. Error(msg.AddressOf(), errNode);
  5717. return -1;
  5718. }
  5719. // Push the function pointer on the stack
  5720. ctx->bc.InstrPTR(asBC_FuncPtr, builder->GetFunctionDescription(funcs[0]));
  5721. ctx->type.Set(asCDataType::CreateFuncDef(builder->GetFunctionDescription(funcs[0])));
  5722. }
  5723. }
  5724. // Is it an enum value?
  5725. if( !found && !objType )
  5726. {
  5727. // The enum type may be declared in a namespace too
  5728. asCObjectType *scopeType = 0;
  5729. if( scope != "" && scope != "::" )
  5730. {
  5731. // Use the last scope name as the enum type
  5732. asCString enumType = scope;
  5733. asCString ns;
  5734. int p = scope.FindLast("::");
  5735. if( p != -1 )
  5736. {
  5737. enumType = scope.SubString(p+2);
  5738. ns = scope.SubString(0, p);
  5739. }
  5740. // resolve the type before the scope
  5741. scopeType = builder->GetObjectType(enumType.AddressOf(), ns);
  5742. }
  5743. asDWORD value = 0;
  5744. asCDataType dt;
  5745. if( scopeType && builder->GetEnumValueFromObjectType(scopeType, name.AddressOf(), dt, value) )
  5746. {
  5747. // scoped enum value found
  5748. found = true;
  5749. }
  5750. else if( !engine->ep.requireEnumScope )
  5751. {
  5752. // Look for the enum value without explicitly informing the enum type
  5753. asCString ns = scope;
  5754. if( ns == "" )
  5755. {
  5756. // Use implicit scope from the current function that is being compiled
  5757. // TODO: cleanup: This is repeated in a lot of places. Should use function for it
  5758. if( outFunc->nameSpace != "" )
  5759. ns = outFunc->nameSpace;
  5760. else if( outFunc->objectType && outFunc->objectType->nameSpace != "" )
  5761. ns = outFunc->objectType->nameSpace;
  5762. }
  5763. else if( ns == "::" )
  5764. ns = "";
  5765. int e = builder->GetEnumValue(name.AddressOf(), dt, value, ns);
  5766. if( e )
  5767. {
  5768. found = true;
  5769. if( e == 2 )
  5770. {
  5771. Error(TXT_FOUND_MULTIPLE_ENUM_VALUES, errNode);
  5772. }
  5773. }
  5774. }
  5775. if( found )
  5776. {
  5777. // Even if the enum type is not shared, and we're compiling a shared object,
  5778. // the use of the values are still allowed, since they are treated as constants.
  5779. // an enum value was resolved
  5780. ctx->type.SetConstantDW(dt, value);
  5781. }
  5782. }
  5783. // The name doesn't match any variable
  5784. if( !found )
  5785. {
  5786. // Give dummy value
  5787. ctx->type.SetDummy();
  5788. if( !isOptional )
  5789. {
  5790. // Prepend the scope to the name for the error message
  5791. asCString ename;
  5792. if( scope != "" && scope != "::" )
  5793. ename = scope + "::";
  5794. else
  5795. ename = scope;
  5796. ename += name;
  5797. asCString str;
  5798. str.Format(TXT_s_NOT_DECLARED, ename.AddressOf());
  5799. Error(str.AddressOf(), errNode);
  5800. // Declare the variable now so that it will not be reported again
  5801. variables->DeclareVariable(name.AddressOf(), asCDataType::CreatePrimitive(ttInt, false), 0x7FFF, true);
  5802. // Mark the variable as initialized so that the user will not be bother by it again
  5803. sVariable *v = variables->GetVariable(name.AddressOf());
  5804. asASSERT(v);
  5805. if( v ) v->isInitialized = true;
  5806. }
  5807. // Return -1 to signal that the variable wasn't found
  5808. return -1;
  5809. }
  5810. return 0;
  5811. }
  5812. int asCCompiler::CompileExpressionValue(asCScriptNode *node, asSExprContext *ctx)
  5813. {
  5814. // Shouldn't receive any byte code
  5815. asASSERT(ctx->bc.GetLastInstr() == -1);
  5816. asCScriptNode *vnode = node->firstChild;
  5817. ctx->exprNode = vnode;
  5818. if( vnode->nodeType == snVariableAccess )
  5819. {
  5820. // Determine the scope resolution of the variable
  5821. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script, &vnode);
  5822. // Determine the name of the variable
  5823. asASSERT(vnode->nodeType == snIdentifier );
  5824. asCString name(&script->code[vnode->tokenPos], vnode->tokenLength);
  5825. return CompileVariableAccess(name, scope, ctx, node);
  5826. }
  5827. else if( vnode->nodeType == snConstant )
  5828. {
  5829. if( vnode->tokenType == ttIntConstant )
  5830. {
  5831. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  5832. asQWORD val = asStringScanUInt64(value.AddressOf(), 10, 0);
  5833. // Do we need 64 bits?
  5834. if( val>>32 )
  5835. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  5836. else
  5837. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  5838. }
  5839. else if( vnode->tokenType == ttBitsConstant )
  5840. {
  5841. asCString value(&script->code[vnode->tokenPos+2], vnode->tokenLength-2);
  5842. // TODO: Check for overflow
  5843. asQWORD val = asStringScanUInt64(value.AddressOf(), 16, 0);
  5844. // Do we need 64 bits?
  5845. if( val>>32 )
  5846. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  5847. else
  5848. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  5849. }
  5850. else if( vnode->tokenType == ttFloatConstant )
  5851. {
  5852. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  5853. // TODO: Check for overflow
  5854. size_t numScanned;
  5855. float v = float(asStringScanDouble(value.AddressOf(), &numScanned));
  5856. ctx->type.SetConstantF(asCDataType::CreatePrimitive(ttFloat, true), v);
  5857. #ifndef AS_USE_DOUBLE_AS_FLOAT
  5858. // Don't check this if we have double as float, because then the whole token would be scanned (i.e. no f suffix)
  5859. asASSERT(numScanned == vnode->tokenLength - 1);
  5860. #endif
  5861. }
  5862. else if( vnode->tokenType == ttDoubleConstant )
  5863. {
  5864. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  5865. // TODO: Check for overflow
  5866. size_t numScanned;
  5867. double v = asStringScanDouble(value.AddressOf(), &numScanned);
  5868. ctx->type.SetConstantD(asCDataType::CreatePrimitive(ttDouble, true), v);
  5869. asASSERT(numScanned == vnode->tokenLength);
  5870. }
  5871. else if( vnode->tokenType == ttTrue ||
  5872. vnode->tokenType == ttFalse )
  5873. {
  5874. #if AS_SIZEOF_BOOL == 1
  5875. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  5876. #else
  5877. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  5878. #endif
  5879. }
  5880. else if( vnode->tokenType == ttStringConstant ||
  5881. vnode->tokenType == ttMultilineStringConstant ||
  5882. vnode->tokenType == ttHeredocStringConstant )
  5883. {
  5884. asCString str;
  5885. asCScriptNode *snode = vnode->firstChild;
  5886. if( script->code[snode->tokenPos] == '\'' && engine->ep.useCharacterLiterals )
  5887. {
  5888. // Treat the single quoted string as a single character literal
  5889. str.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  5890. asDWORD val = 0;
  5891. if( str.GetLength() && (unsigned char)str[0] > 127 && engine->ep.scanner == 1 )
  5892. {
  5893. // This is the start of a UTF8 encoded character. We need to decode it
  5894. val = asStringDecodeUTF8(str.AddressOf(), 0);
  5895. if( val == (asDWORD)-1 )
  5896. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  5897. }
  5898. else
  5899. {
  5900. val = ProcessStringConstant(str, snode);
  5901. if( val == (asDWORD)-1 )
  5902. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  5903. }
  5904. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), val);
  5905. }
  5906. else
  5907. {
  5908. // Process the string constants
  5909. while( snode )
  5910. {
  5911. asCString cat;
  5912. if( snode->tokenType == ttStringConstant )
  5913. {
  5914. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  5915. ProcessStringConstant(cat, snode);
  5916. }
  5917. else if( snode->tokenType == ttMultilineStringConstant )
  5918. {
  5919. if( !engine->ep.allowMultilineStrings )
  5920. Error(TXT_MULTILINE_STRINGS_NOT_ALLOWED, snode);
  5921. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  5922. ProcessStringConstant(cat, snode);
  5923. }
  5924. else if( snode->tokenType == ttHeredocStringConstant )
  5925. {
  5926. cat.Assign(&script->code[snode->tokenPos+3], snode->tokenLength-6);
  5927. ProcessHeredocStringConstant(cat, snode);
  5928. }
  5929. str += cat;
  5930. snode = snode->next;
  5931. }
  5932. // Call the string factory function to create a string object
  5933. asCScriptFunction *descr = engine->stringFactory;
  5934. if( descr == 0 )
  5935. {
  5936. // Error
  5937. Error(TXT_STRINGS_NOT_RECOGNIZED, vnode);
  5938. // Give dummy value
  5939. ctx->type.SetDummy();
  5940. return -1;
  5941. }
  5942. else
  5943. {
  5944. // Register the constant string with the engine
  5945. int id = engine->AddConstantString(str.AddressOf(), str.GetLength());
  5946. ctx->bc.InstrWORD(asBC_STR, (asWORD)id);
  5947. bool useVariable = false;
  5948. int stackOffset = 0;
  5949. if( descr->DoesReturnOnStack() )
  5950. {
  5951. useVariable = true;
  5952. stackOffset = AllocateVariable(descr->returnType, true);
  5953. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  5954. }
  5955. PerformFunctionCall(descr->id, ctx, false, 0, 0, useVariable, stackOffset);
  5956. }
  5957. }
  5958. }
  5959. else if( vnode->tokenType == ttNull )
  5960. {
  5961. ctx->bc.Instr(asBC_PshNull);
  5962. ctx->type.SetNullConstant();
  5963. }
  5964. else
  5965. asASSERT(false);
  5966. }
  5967. else if( vnode->nodeType == snFunctionCall )
  5968. {
  5969. // Determine the scope resolution
  5970. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script);
  5971. return CompileFunctionCall(vnode, ctx, 0, false, scope);
  5972. }
  5973. else if( vnode->nodeType == snConstructCall )
  5974. {
  5975. CompileConstructCall(vnode, ctx);
  5976. }
  5977. else if( vnode->nodeType == snAssignment )
  5978. {
  5979. asSExprContext e(engine);
  5980. int r = CompileAssignment(vnode, &e);
  5981. if( r < 0 )
  5982. {
  5983. ctx->type.SetDummy();
  5984. return r;
  5985. }
  5986. MergeExprBytecodeAndType(ctx, &e);
  5987. }
  5988. else if( vnode->nodeType == snCast )
  5989. {
  5990. // Implement the cast operator
  5991. CompileConversion(vnode, ctx);
  5992. }
  5993. else
  5994. asASSERT(false);
  5995. return 0;
  5996. }
  5997. asUINT asCCompiler::ProcessStringConstant(asCString &cstr, asCScriptNode *node, bool processEscapeSequences)
  5998. {
  5999. int charLiteral = -1;
  6000. // Process escape sequences
  6001. asCArray<char> str((int)cstr.GetLength());
  6002. for( asUINT n = 0; n < cstr.GetLength(); n++ )
  6003. {
  6004. #ifdef AS_DOUBLEBYTE_CHARSET
  6005. // Double-byte charset is only allowed for ASCII and not UTF16 encoded strings
  6006. if( (cstr[n] & 0x80) && engine->ep.scanner == 0 && engine->ep.stringEncoding != 1 )
  6007. {
  6008. // This is the lead character of a double byte character
  6009. // include the trail character without checking it's value.
  6010. str.PushLast(cstr[n]);
  6011. n++;
  6012. str.PushLast(cstr[n]);
  6013. continue;
  6014. }
  6015. #endif
  6016. asUINT val;
  6017. if( processEscapeSequences && cstr[n] == '\\' )
  6018. {
  6019. ++n;
  6020. if( n == cstr.GetLength() )
  6021. {
  6022. if( charLiteral == -1 ) charLiteral = 0;
  6023. return charLiteral;
  6024. }
  6025. // Hexadecimal escape sequences will allow the construction of
  6026. // invalid unicode sequences, but the string should also work as
  6027. // a bytearray so we must support this. The code for working with
  6028. // unicode text must be prepared to handle invalid unicode sequences
  6029. if( cstr[n] == 'x' || cstr[n] == 'X' )
  6030. {
  6031. ++n;
  6032. if( n == cstr.GetLength() ) break;
  6033. val = 0;
  6034. int c = engine->ep.stringEncoding == 1 ? 4 : 2;
  6035. for( ; c > 0 && n < cstr.GetLength(); c--, n++ )
  6036. {
  6037. if( cstr[n] >= '0' && cstr[n] <= '9' )
  6038. val = val*16 + cstr[n] - '0';
  6039. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  6040. val = val*16 + cstr[n] - 'a' + 10;
  6041. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  6042. val = val*16 + cstr[n] - 'A' + 10;
  6043. else
  6044. break;
  6045. }
  6046. // Rewind one, since the loop will increment it again
  6047. n--;
  6048. // Hexadecimal escape sequences produce exact value, even if it is not proper unicode chars
  6049. if( engine->ep.stringEncoding == 0 )
  6050. {
  6051. str.PushLast((asBYTE)val);
  6052. }
  6053. else
  6054. {
  6055. #ifndef AS_BIG_ENDIAN
  6056. str.PushLast((asBYTE)val);
  6057. str.PushLast((asBYTE)(val>>8));
  6058. #else
  6059. str.PushLast((asBYTE)(val>>8));
  6060. str.PushLast((asBYTE)val);
  6061. #endif
  6062. }
  6063. if( charLiteral == -1 ) charLiteral = val;
  6064. continue;
  6065. }
  6066. else if( cstr[n] == 'u' || cstr[n] == 'U' )
  6067. {
  6068. // \u expects 4 hex digits
  6069. // \U expects 8 hex digits
  6070. bool expect2 = cstr[n] == 'u';
  6071. int c = expect2 ? 4 : 8;
  6072. val = 0;
  6073. for( ; c > 0; c-- )
  6074. {
  6075. ++n;
  6076. if( n == cstr.GetLength() ) break;
  6077. if( cstr[n] >= '0' && cstr[n] <= '9' )
  6078. val = val*16 + cstr[n] - '0';
  6079. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  6080. val = val*16 + cstr[n] - 'a' + 10;
  6081. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  6082. val = val*16 + cstr[n] - 'A' + 10;
  6083. else
  6084. break;
  6085. }
  6086. if( c != 0 )
  6087. {
  6088. // Give warning about invalid code point
  6089. // TODO: Need code position for warning
  6090. asCString msg;
  6091. msg.Format(TXT_INVALID_UNICODE_FORMAT_EXPECTED_d, expect2 ? 4 : 8);
  6092. Warning(msg.AddressOf(), node);
  6093. continue;
  6094. }
  6095. }
  6096. else
  6097. {
  6098. if( cstr[n] == '"' )
  6099. val = '"';
  6100. else if( cstr[n] == '\'' )
  6101. val = '\'';
  6102. else if( cstr[n] == 'n' )
  6103. val = '\n';
  6104. else if( cstr[n] == 'r' )
  6105. val = '\r';
  6106. else if( cstr[n] == 't' )
  6107. val = '\t';
  6108. else if( cstr[n] == '0' )
  6109. val = '\0';
  6110. else if( cstr[n] == '\\' )
  6111. val = '\\';
  6112. else
  6113. {
  6114. // Invalid escape sequence
  6115. Warning(TXT_INVALID_ESCAPE_SEQUENCE, node);
  6116. continue;
  6117. }
  6118. }
  6119. }
  6120. else
  6121. {
  6122. if( engine->ep.scanner == 1 && (cstr[n] & 0x80) )
  6123. {
  6124. unsigned int len;
  6125. val = asStringDecodeUTF8(&cstr[n], &len);
  6126. if( val == 0xFFFFFFFF )
  6127. {
  6128. // Incorrect UTF8 encoding. Use only the first byte
  6129. // TODO: Need code position for warning
  6130. Warning(TXT_INVALID_UNICODE_SEQUENCE_IN_SRC, node);
  6131. val = (unsigned char)cstr[n];
  6132. }
  6133. else
  6134. n += len-1;
  6135. }
  6136. else
  6137. val = (unsigned char)cstr[n];
  6138. }
  6139. // Add the character to the final string
  6140. char encodedValue[5];
  6141. int len;
  6142. if( engine->ep.scanner == 1 && engine->ep.stringEncoding == 0 )
  6143. {
  6144. // Convert to UTF8 encoded
  6145. len = asStringEncodeUTF8(val, encodedValue);
  6146. }
  6147. else if( engine->ep.stringEncoding == 1 )
  6148. {
  6149. // Convert to 16bit wide character string (even if the script is scanned as ASCII)
  6150. len = asStringEncodeUTF16(val, encodedValue);
  6151. }
  6152. else
  6153. {
  6154. // Do not convert ASCII characters
  6155. encodedValue[0] = (asBYTE)val;
  6156. len = 1;
  6157. }
  6158. if( len < 0 )
  6159. {
  6160. // Give warning about invalid code point
  6161. // TODO: Need code position for warning
  6162. Warning(TXT_INVALID_UNICODE_VALUE, node);
  6163. }
  6164. else
  6165. {
  6166. // Add the encoded value to the final string
  6167. str.Concatenate(encodedValue, len);
  6168. if( charLiteral == -1 ) charLiteral = val;
  6169. }
  6170. }
  6171. cstr.Assign(str.AddressOf(), str.GetLength());
  6172. return charLiteral;
  6173. }
  6174. void asCCompiler::ProcessHeredocStringConstant(asCString &str, asCScriptNode *node)
  6175. {
  6176. // Remove first line if it only contains whitespace
  6177. int start;
  6178. for( start = 0; start < (int)str.GetLength(); start++ )
  6179. {
  6180. if( str[start] == '\n' )
  6181. {
  6182. // Remove the linebreak as well
  6183. start++;
  6184. break;
  6185. }
  6186. if( str[start] != ' ' &&
  6187. str[start] != '\t' &&
  6188. str[start] != '\r' )
  6189. {
  6190. // Don't remove anything
  6191. start = 0;
  6192. break;
  6193. }
  6194. }
  6195. // Remove last line break and the line after that if it only contains whitespaces
  6196. int end;
  6197. for( end = (int)str.GetLength() - 1; end >= 0; end-- )
  6198. {
  6199. if( str[end] == '\n' )
  6200. break;
  6201. if( str[end] != ' ' &&
  6202. str[end] != '\t' &&
  6203. str[end] != '\r' )
  6204. {
  6205. // Don't remove anything
  6206. end = (int)str.GetLength();
  6207. break;
  6208. }
  6209. }
  6210. if( end < 0 ) end = 0;
  6211. asCString tmp;
  6212. if( end > start )
  6213. tmp.Assign(&str[start], end-start);
  6214. ProcessStringConstant(tmp, node, false);
  6215. str = tmp;
  6216. }
  6217. void asCCompiler::CompileConversion(asCScriptNode *node, asSExprContext *ctx)
  6218. {
  6219. asSExprContext expr(engine);
  6220. asCDataType to;
  6221. bool anyErrors = false;
  6222. EImplicitConv convType;
  6223. if( node->nodeType == snConstructCall )
  6224. {
  6225. convType = asIC_EXPLICIT_VAL_CAST;
  6226. // Verify that there is only one argument
  6227. if( node->lastChild->firstChild == 0 ||
  6228. node->lastChild->firstChild != node->lastChild->lastChild )
  6229. {
  6230. Error(TXT_ONLY_ONE_ARGUMENT_IN_CAST, node->lastChild);
  6231. expr.type.SetDummy();
  6232. anyErrors = true;
  6233. }
  6234. else
  6235. {
  6236. // Compile the expression
  6237. int r = CompileAssignment(node->lastChild->firstChild, &expr);
  6238. if( r < 0 )
  6239. anyErrors = true;
  6240. }
  6241. // Determine the requested type
  6242. // TODO: namespace: Use correct implicit namespace from function
  6243. to = builder->CreateDataTypeFromNode(node->firstChild, script, "");
  6244. to.MakeReadOnly(true); // Default to const
  6245. asASSERT(to.IsPrimitive());
  6246. }
  6247. else
  6248. {
  6249. convType = asIC_EXPLICIT_REF_CAST;
  6250. // Compile the expression
  6251. int r = CompileAssignment(node->lastChild, &expr);
  6252. if( r < 0 )
  6253. anyErrors = true;
  6254. // Determine the requested type
  6255. // TODO: namespace: Use correct implicit namespace from function
  6256. to = builder->CreateDataTypeFromNode(node->firstChild, script, "");
  6257. to = builder->ModifyDataTypeFromNode(to, node->firstChild->next, script, 0, 0);
  6258. // If the type support object handles, then use it
  6259. if( to.SupportHandles() )
  6260. {
  6261. to.MakeHandle(true);
  6262. }
  6263. else if( !to.IsObjectHandle() )
  6264. {
  6265. // The cast<type> operator can only be used for reference casts
  6266. Error(TXT_ILLEGAL_TARGET_TYPE_FOR_REF_CAST, node->firstChild);
  6267. anyErrors = true;
  6268. }
  6269. }
  6270. // Do not allow casting to non shared type if we're compiling a shared method
  6271. if( outFunc->IsShared() &&
  6272. to.GetObjectType() && !to.GetObjectType()->IsShared() )
  6273. {
  6274. asCString msg;
  6275. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, to.GetObjectType()->name.AddressOf());
  6276. Error(msg.AddressOf(), node);
  6277. anyErrors = true;
  6278. }
  6279. if( anyErrors )
  6280. {
  6281. // Assume that the error can be fixed and allow the compilation to continue
  6282. ctx->type.SetConstantDW(to, 0);
  6283. return;
  6284. }
  6285. ProcessPropertyGetAccessor(&expr, node);
  6286. // We don't want a reference
  6287. if( expr.type.dataType.IsReference() )
  6288. {
  6289. if( expr.type.dataType.IsObject() )
  6290. Dereference(&expr, true);
  6291. else
  6292. ConvertToVariable(&expr);
  6293. }
  6294. ImplicitConversion(&expr, to, node, convType);
  6295. IsVariableInitialized(&expr.type, node);
  6296. // If no type conversion is really tried ignore it
  6297. if( to == expr.type.dataType )
  6298. {
  6299. // This will keep information about constant type
  6300. MergeExprBytecode(ctx, &expr);
  6301. ctx->type = expr.type;
  6302. return;
  6303. }
  6304. if( to.IsEqualExceptConst(expr.type.dataType) && to.IsPrimitive() )
  6305. {
  6306. MergeExprBytecode(ctx, &expr);
  6307. ctx->type = expr.type;
  6308. ctx->type.dataType.MakeReadOnly(true);
  6309. return;
  6310. }
  6311. // The implicit conversion already does most of the conversions permitted,
  6312. // here we'll only treat those conversions that require an explicit cast.
  6313. bool conversionOK = false;
  6314. if( !expr.type.isConstant )
  6315. {
  6316. if( !expr.type.dataType.IsObject() )
  6317. ConvertToTempVariable(&expr);
  6318. if( to.IsObjectHandle() &&
  6319. expr.type.dataType.IsObjectHandle() &&
  6320. !(!to.IsHandleToConst() && expr.type.dataType.IsHandleToConst()) )
  6321. {
  6322. conversionOK = CompileRefCast(&expr, to, true, node);
  6323. MergeExprBytecode(ctx, &expr);
  6324. ctx->type = expr.type;
  6325. }
  6326. }
  6327. if( conversionOK )
  6328. return;
  6329. // Conversion not available
  6330. ctx->type.SetDummy();
  6331. asCString strTo, strFrom;
  6332. strTo = to.Format();
  6333. strFrom = expr.type.dataType.Format();
  6334. asCString msg;
  6335. msg.Format(TXT_NO_CONVERSION_s_TO_s, strFrom.AddressOf(), strTo.AddressOf());
  6336. Error(msg.AddressOf(), node);
  6337. }
  6338. void asCCompiler::AfterFunctionCall(int funcID, asCArray<asSExprContext*> &args, asSExprContext *ctx, bool deferAll)
  6339. {
  6340. asCScriptFunction *descr = builder->GetFunctionDescription(funcID);
  6341. // Parameters that are sent by reference should be assigned
  6342. // to the evaluated expression if it is an lvalue
  6343. // Evaluate the arguments from last to first
  6344. int n = (int)descr->parameterTypes.GetLength() - 1;
  6345. for( ; n >= 0; n-- )
  6346. {
  6347. if( (descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] & asTM_OUTREF)) ||
  6348. (descr->parameterTypes[n].IsObject() && deferAll) )
  6349. {
  6350. asASSERT( !(descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] == asTM_OUTREF)) || args[n]->origExpr );
  6351. // For &inout, only store the argument if it is for a temporary variable
  6352. if( engine->ep.allowUnsafeReferences ||
  6353. descr->inOutFlags[n] != asTM_INOUTREF || args[n]->type.isTemporary )
  6354. {
  6355. // Store the argument for later processing
  6356. asSDeferredParam outParam;
  6357. outParam.argNode = args[n]->exprNode;
  6358. outParam.argType = args[n]->type;
  6359. outParam.argInOutFlags = descr->inOutFlags[n];
  6360. outParam.origExpr = args[n]->origExpr;
  6361. ctx->deferredParams.PushLast(outParam);
  6362. }
  6363. }
  6364. else
  6365. {
  6366. // Release the temporary variable now
  6367. ReleaseTemporaryVariable(args[n]->type, &ctx->bc);
  6368. }
  6369. // Move the argument's deferred expressions over to the final expression
  6370. for( asUINT m = 0; m < args[n]->deferredParams.GetLength(); m++ )
  6371. {
  6372. ctx->deferredParams.PushLast(args[n]->deferredParams[m]);
  6373. args[n]->deferredParams[m].origExpr = 0;
  6374. }
  6375. args[n]->deferredParams.SetLength(0);
  6376. }
  6377. }
  6378. void asCCompiler::ProcessDeferredParams(asSExprContext *ctx)
  6379. {
  6380. if( isProcessingDeferredParams ) return;
  6381. isProcessingDeferredParams = true;
  6382. for( asUINT n = 0; n < ctx->deferredParams.GetLength(); n++ )
  6383. {
  6384. asSDeferredParam outParam = ctx->deferredParams[n];
  6385. if( outParam.argInOutFlags < asTM_OUTREF ) // &in, or not reference
  6386. {
  6387. // Just release the variable
  6388. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6389. }
  6390. else if( outParam.argInOutFlags == asTM_OUTREF )
  6391. {
  6392. asSExprContext *expr = outParam.origExpr;
  6393. outParam.origExpr = 0;
  6394. if( outParam.argType.dataType.IsObjectHandle() )
  6395. {
  6396. // Implicitly convert the value to a handle
  6397. if( expr->type.dataType.IsObjectHandle() )
  6398. expr->type.isExplicitHandle = true;
  6399. }
  6400. // Verify that the expression result in a lvalue, or a property accessor
  6401. if( IsLValue(expr->type) || expr->property_get || expr->property_set )
  6402. {
  6403. asSExprContext rctx(engine);
  6404. rctx.type = outParam.argType;
  6405. if( rctx.type.dataType.IsPrimitive() )
  6406. rctx.type.dataType.MakeReference(false);
  6407. else
  6408. {
  6409. rctx.bc.InstrSHORT(asBC_PSF, outParam.argType.stackOffset);
  6410. rctx.type.dataType.MakeReference(IsVariableOnHeap(outParam.argType.stackOffset));
  6411. if( expr->type.isExplicitHandle )
  6412. rctx.type.isExplicitHandle = true;
  6413. }
  6414. asSExprContext o(engine);
  6415. DoAssignment(&o, expr, &rctx, outParam.argNode, outParam.argNode, ttAssignment, outParam.argNode);
  6416. if( !o.type.dataType.IsPrimitive() ) o.bc.Instr(asBC_PopPtr);
  6417. MergeExprBytecode(ctx, &o);
  6418. }
  6419. else
  6420. {
  6421. // We must still evaluate the expression
  6422. MergeExprBytecode(ctx, expr);
  6423. if( !expr->type.isConstant || expr->type.IsNullConstant() )
  6424. ctx->bc.Instr(asBC_PopPtr);
  6425. // Give a warning, except if the argument is null or 0 which indicate the argument is really to be ignored
  6426. if( !expr->type.IsNullConstant() && !(expr->type.isConstant && expr->type.qwordValue == 0) )
  6427. Warning(TXT_ARG_NOT_LVALUE, outParam.argNode);
  6428. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6429. }
  6430. ReleaseTemporaryVariable(expr->type, &ctx->bc);
  6431. // Delete the original expression context
  6432. asDELETE(expr,asSExprContext);
  6433. }
  6434. else // &inout
  6435. {
  6436. if( outParam.argType.isTemporary )
  6437. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6438. else if( !outParam.argType.isVariable )
  6439. {
  6440. if( outParam.argType.dataType.IsObject() &&
  6441. ((outParam.argType.dataType.GetBehaviour()->addref &&
  6442. outParam.argType.dataType.GetBehaviour()->release) ||
  6443. (outParam.argType.dataType.GetObjectType()->flags & asOBJ_NOCOUNT)) )
  6444. {
  6445. // Release the object handle that was taken to guarantee the reference
  6446. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  6447. }
  6448. }
  6449. }
  6450. }
  6451. ctx->deferredParams.SetLength(0);
  6452. isProcessingDeferredParams = false;
  6453. }
  6454. void asCCompiler::CompileConstructCall(asCScriptNode *node, asSExprContext *ctx)
  6455. {
  6456. // The first node is a datatype node
  6457. asCString name;
  6458. asCTypeInfo tempObj;
  6459. bool onHeap = true;
  6460. asCArray<int> funcs;
  6461. // It is possible that the name is really a constructor
  6462. asCDataType dt;
  6463. // TODO: namespace: Use correct implicit namespace from function
  6464. dt = builder->CreateDataTypeFromNode(node->firstChild, script, "");
  6465. if( dt.IsPrimitive() )
  6466. {
  6467. // This is a cast to a primitive type
  6468. CompileConversion(node, ctx);
  6469. return;
  6470. }
  6471. // Do not allow constructing non-shared types in shared functions
  6472. if( outFunc->IsShared() &&
  6473. dt.GetObjectType() && !dt.GetObjectType()->IsShared() )
  6474. {
  6475. asCString msg;
  6476. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetObjectType()->name.AddressOf());
  6477. Error(msg.AddressOf(), node);
  6478. }
  6479. // Compile the arguments
  6480. asCArray<asSExprContext *> args;
  6481. asCArray<asCTypeInfo> temporaryVariables;
  6482. if( CompileArgumentList(node->lastChild, args) >= 0 )
  6483. {
  6484. // Check for a value cast behaviour
  6485. if( args.GetLength() == 1 && args[0]->type.dataType.GetObjectType() )
  6486. {
  6487. asSExprContext conv(engine);
  6488. conv.type = args[0]->type;
  6489. ImplicitConversion(&conv, dt, node->lastChild, asIC_EXPLICIT_VAL_CAST, false);
  6490. if( conv.type.dataType.IsEqualExceptRef(dt) )
  6491. {
  6492. ImplicitConversion(args[0], dt, node->lastChild, asIC_EXPLICIT_VAL_CAST);
  6493. ctx->bc.AddCode(&args[0]->bc);
  6494. ctx->type = args[0]->type;
  6495. asDELETE(args[0],asSExprContext);
  6496. return;
  6497. }
  6498. }
  6499. // Check for possible constructor/factory
  6500. name = dt.Format();
  6501. asSTypeBehaviour *beh = dt.GetBehaviour();
  6502. if( !(dt.GetObjectType()->flags & asOBJ_REF) )
  6503. {
  6504. funcs = beh->constructors;
  6505. // Value types and script types are allocated through the constructor
  6506. tempObj.dataType = dt;
  6507. tempObj.stackOffset = (short)AllocateVariable(dt, true);
  6508. tempObj.dataType.MakeReference(true);
  6509. tempObj.isTemporary = true;
  6510. tempObj.isVariable = true;
  6511. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  6512. // Push the address of the object on the stack
  6513. if( onHeap )
  6514. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  6515. }
  6516. else
  6517. {
  6518. funcs = beh->factories;
  6519. }
  6520. // Special case: Allow calling func(void) with a void expression.
  6521. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  6522. {
  6523. // Evaluate the expression before the function call
  6524. MergeExprBytecode(ctx, args[0]);
  6525. asDELETE(args[0],asSExprContext);
  6526. args.SetLength(0);
  6527. }
  6528. // Special case: If this is an object constructor and there are no arguments use the default constructor.
  6529. // If none has been registered, just allocate the variable and push it on the stack.
  6530. if( args.GetLength() == 0 )
  6531. {
  6532. asSTypeBehaviour *beh = tempObj.dataType.GetBehaviour();
  6533. if( beh && beh->construct == 0 && !(dt.GetObjectType()->flags & asOBJ_REF) )
  6534. {
  6535. // Call the default constructor
  6536. ctx->type = tempObj;
  6537. if( onHeap )
  6538. {
  6539. asASSERT(ctx->bc.GetLastInstr() == asBC_VAR);
  6540. ctx->bc.RemoveLastInstr();
  6541. }
  6542. CallDefaultConstructor(tempObj.dataType, tempObj.stackOffset, IsVariableOnHeap(tempObj.stackOffset), &ctx->bc, node);
  6543. // Push the reference on the stack
  6544. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6545. return;
  6546. }
  6547. }
  6548. MatchFunctions(funcs, args, node, name.AddressOf(), NULL, false);
  6549. if( funcs.GetLength() != 1 )
  6550. {
  6551. // The error was reported by MatchFunctions()
  6552. // Dummy value
  6553. ctx->type.SetDummy();
  6554. }
  6555. else
  6556. {
  6557. int r = asSUCCESS;
  6558. // Add the default values for arguments not explicitly supplied
  6559. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  6560. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  6561. r = CompileDefaultArgs(node, args, func);
  6562. if( r == asSUCCESS )
  6563. {
  6564. asCByteCode objBC(engine);
  6565. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  6566. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  6567. if( !(dt.GetObjectType()->flags & asOBJ_REF) )
  6568. {
  6569. // If the object is allocated on the stack, then call the constructor as a normal function
  6570. if( onHeap )
  6571. {
  6572. int offset = 0;
  6573. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  6574. for( asUINT n = 0; n < args.GetLength(); n++ )
  6575. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  6576. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  6577. }
  6578. else
  6579. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6580. PerformFunctionCall(funcs[0], ctx, onHeap, &args, tempObj.dataType.GetObjectType());
  6581. // Add tag that the object has been initialized
  6582. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  6583. // The constructor doesn't return anything,
  6584. // so we have to manually inform the type of
  6585. // the return value
  6586. ctx->type = tempObj;
  6587. if( !onHeap )
  6588. ctx->type.dataType.MakeReference(false);
  6589. // Push the address of the object on the stack again
  6590. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6591. }
  6592. else
  6593. {
  6594. // Call the factory to create the reference type
  6595. PerformFunctionCall(funcs[0], ctx, false, &args);
  6596. }
  6597. }
  6598. }
  6599. }
  6600. else
  6601. {
  6602. // Failed to compile the argument list, set the result to the dummy type
  6603. ctx->type.SetDummy();
  6604. }
  6605. // Cleanup
  6606. for( asUINT n = 0; n < args.GetLength(); n++ )
  6607. if( args[n] )
  6608. {
  6609. asDELETE(args[n],asSExprContext);
  6610. }
  6611. }
  6612. int asCCompiler::CompileFunctionCall(asCScriptNode *node, asSExprContext *ctx, asCObjectType *objectType, bool objIsConst, const asCString &scope)
  6613. {
  6614. asCString name;
  6615. asCTypeInfo tempObj;
  6616. asCArray<int> funcs;
  6617. int r = -1;
  6618. asCScriptNode *nm = node->lastChild->prev;
  6619. name.Assign(&script->code[nm->tokenPos], nm->tokenLength);
  6620. // If we're compiling a class method, then the call may be to a class method
  6621. // even though it looks like an ordinary call to a global function. If it is
  6622. // to a class method it is necessary to implicitly add the this pointer.
  6623. if( objectType == 0 && outFunc && outFunc->objectType && scope != "::" )
  6624. {
  6625. // The special keyword 'super' may be used in constructors to invoke the base
  6626. // class' constructor. This can only be used without any scoping operator
  6627. if( m_isConstructor && name == SUPER_TOKEN && scope == "" )
  6628. {
  6629. // We are calling the base class' constructor, so set the objectType
  6630. objectType = outFunc->objectType;
  6631. }
  6632. else
  6633. {
  6634. // Are there any class methods that may match?
  6635. // TODO: namespace: Should really make sure the scope also match. Because the scope
  6636. // may match a base class, or it may match a global namespace. If it is
  6637. // matching a global scope then we're not calling a class method even
  6638. // if there is a method with the same name.
  6639. asCArray<int> funcs;
  6640. builder->GetObjectMethodDescriptions(name.AddressOf(), outFunc->objectType, funcs, false);
  6641. if( funcs.GetLength() )
  6642. {
  6643. // We're calling a class method, so set the objectType
  6644. objectType = outFunc->objectType;
  6645. }
  6646. }
  6647. // If a class method is being called then implicitly add the this pointer for the call
  6648. if( objectType )
  6649. {
  6650. asCDataType dt = asCDataType::CreateObject(objectType, false);
  6651. // The object pointer is located at stack position 0
  6652. ctx->bc.InstrSHORT(asBC_PSF, 0);
  6653. ctx->type.SetVariable(dt, 0, false);
  6654. ctx->type.dataType.MakeReference(true);
  6655. Dereference(ctx, true);
  6656. }
  6657. }
  6658. // First check for a local variable of a function type
  6659. // Must not allow function names, nor global variables to be returned in this instance
  6660. asSExprContext funcPtr(engine);
  6661. if( objectType == 0 )
  6662. r = CompileVariableAccess(name, scope, &funcPtr, node, true, true);
  6663. if( r < 0 )
  6664. {
  6665. if( objectType )
  6666. {
  6667. // If we're compiling a constructor and the name of the function is super then
  6668. // the constructor of the base class is being called.
  6669. // super cannot be prefixed with a scope operator
  6670. if( scope == "" && m_isConstructor && name == SUPER_TOKEN )
  6671. {
  6672. // If the class is not derived from anyone else, calling super should give an error
  6673. if( objectType->derivedFrom )
  6674. funcs = objectType->derivedFrom->beh.constructors;
  6675. // Must not allow calling base class' constructor multiple times
  6676. if( continueLabels.GetLength() > 0 )
  6677. {
  6678. // If a continue label is set we are in a loop
  6679. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_LOOPS, node);
  6680. }
  6681. else if( breakLabels.GetLength() > 0 )
  6682. {
  6683. // TODO: inheritance: Should eventually allow constructors in switch statements
  6684. // If a break label is set we are either in a loop or a switch statements
  6685. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_SWITCH, node);
  6686. }
  6687. else if( m_isConstructorCalled )
  6688. {
  6689. Error(TXT_CANNOT_CALL_CONSTRUCTOR_TWICE, node);
  6690. }
  6691. m_isConstructorCalled = true;
  6692. }
  6693. else
  6694. {
  6695. // The scope is can be used to specify the base class
  6696. builder->GetObjectMethodDescriptions(name.AddressOf(), objectType, funcs, objIsConst, scope);
  6697. }
  6698. // It is still possible that there is a class member of a function type
  6699. if( funcs.GetLength() == 0 )
  6700. CompileVariableAccess(name, scope, &funcPtr, node, true, true, objectType);
  6701. }
  6702. else
  6703. {
  6704. // The scope is used to define the namespace
  6705. asCString ns = scope;
  6706. if( ns == "" )
  6707. {
  6708. if( outFunc->nameSpace != "" )
  6709. ns = outFunc->nameSpace;
  6710. else if( outFunc->objectType && outFunc->objectType->nameSpace != "" )
  6711. ns = outFunc->objectType->nameSpace;
  6712. }
  6713. else if( ns == "::" )
  6714. ns = "";
  6715. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  6716. // TODO: funcdef: It is still possible that there is a global variable of a function type
  6717. }
  6718. }
  6719. else if( !funcPtr.type.dataType.GetFuncDef() )
  6720. {
  6721. // The variable is not a function
  6722. asCString msg;
  6723. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  6724. Error(msg.AddressOf(), node);
  6725. return -1;
  6726. }
  6727. if( funcs.GetLength() == 0 && funcPtr.type.dataType.GetFuncDef() )
  6728. {
  6729. funcs.PushLast(funcPtr.type.dataType.GetFuncDef()->id);
  6730. }
  6731. // Compile the arguments
  6732. asCArray<asSExprContext *> args;
  6733. asCArray<asCTypeInfo> temporaryVariables;
  6734. if( CompileArgumentList(node->lastChild, args) >= 0 )
  6735. {
  6736. // Special case: Allow calling func(void) with a void expression.
  6737. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  6738. {
  6739. // Evaluate the expression before the function call
  6740. MergeExprBytecode(ctx, args[0]);
  6741. asDELETE(args[0],asSExprContext);
  6742. args.SetLength(0);
  6743. }
  6744. MatchFunctions(funcs, args, node, name.AddressOf(), objectType, objIsConst, false, true, scope);
  6745. if( funcs.GetLength() != 1 )
  6746. {
  6747. // The error was reported by MatchFunctions()
  6748. // Dummy value
  6749. ctx->type.SetDummy();
  6750. }
  6751. else
  6752. {
  6753. int r = asSUCCESS;
  6754. // Add the default values for arguments not explicitly supplied
  6755. asCScriptFunction *func = (funcs[0] & 0xFFFF0000) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  6756. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  6757. r = CompileDefaultArgs(node, args, func);
  6758. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  6759. // is it enough to make sure it is in a local variable?
  6760. // For function pointer we must guarantee that the function is safe, i.e.
  6761. // by first storing the function pointer in a local variable (if it isn't already in one)
  6762. if( r == asSUCCESS )
  6763. {
  6764. if( (funcs[0] & 0xFFFF0000) == 0 && engine->scriptFunctions[funcs[0]]->funcType == asFUNC_FUNCDEF )
  6765. {
  6766. if( objectType )
  6767. {
  6768. Dereference(ctx, true); // Dereference the object pointer to access the member
  6769. // The actual function should be called as if a global function
  6770. objectType = 0;
  6771. }
  6772. Dereference(&funcPtr, true);
  6773. ConvertToVariable(&funcPtr);
  6774. ctx->bc.AddCode(&funcPtr.bc);
  6775. if( !funcPtr.type.isTemporary )
  6776. ctx->bc.Instr(asBC_PopPtr);
  6777. }
  6778. MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, funcPtr.type.stackOffset);
  6779. // If the function pointer was copied to a local variable for the call, then
  6780. // release it again (temporary local variable)
  6781. if( (funcs[0] & 0xFFFF0000) == 0 && engine->scriptFunctions[funcs[0]]->funcType == asFUNC_FUNCDEF )
  6782. {
  6783. ReleaseTemporaryVariable(funcPtr.type, &ctx->bc);
  6784. }
  6785. }
  6786. }
  6787. }
  6788. else
  6789. {
  6790. // Failed to compile the argument list, set the dummy type and continue compilation
  6791. ctx->type.SetDummy();
  6792. }
  6793. // Cleanup
  6794. for( asUINT n = 0; n < args.GetLength(); n++ )
  6795. if( args[n] )
  6796. {
  6797. asDELETE(args[n],asSExprContext);
  6798. }
  6799. return 0;
  6800. }
  6801. int asCCompiler::CompileExpressionPreOp(asCScriptNode *node, asSExprContext *ctx)
  6802. {
  6803. int op = node->tokenType;
  6804. IsVariableInitialized(&ctx->type, node);
  6805. if( op == ttHandle )
  6806. {
  6807. // Verify that the type allow its handle to be taken
  6808. if( ctx->type.isExplicitHandle ||
  6809. !ctx->type.dataType.IsObject() ||
  6810. !(((ctx->type.dataType.GetObjectType()->beh.addref && ctx->type.dataType.GetObjectType()->beh.release) || (ctx->type.dataType.GetObjectType()->flags & asOBJ_NOCOUNT)) ||
  6811. (ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  6812. {
  6813. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  6814. return -1;
  6815. }
  6816. // Objects that are not local variables are not references
  6817. // Objects allocated on the stack are also not marked as references
  6818. if( !ctx->type.dataType.IsReference() &&
  6819. !(ctx->type.dataType.IsObject() && !ctx->type.isVariable) &&
  6820. !(ctx->type.isVariable && !IsVariableOnHeap(ctx->type.stackOffset)) )
  6821. {
  6822. Error(TXT_NOT_VALID_REFERENCE, node);
  6823. return -1;
  6824. }
  6825. // Convert the expression to a handle
  6826. if( !ctx->type.dataType.IsObjectHandle() && !(ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  6827. {
  6828. asCDataType to = ctx->type.dataType;
  6829. to.MakeHandle(true);
  6830. to.MakeReference(true);
  6831. to.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  6832. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV, true, false);
  6833. asASSERT( ctx->type.dataType.IsObjectHandle() );
  6834. }
  6835. else if( ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  6836. {
  6837. // For the ASHANDLE type we'll simply set the expression as a handle
  6838. ctx->type.dataType.MakeHandle(true);
  6839. }
  6840. // Mark the expression as an explicit handle to avoid implicit conversions to non-handle expressions
  6841. ctx->type.isExplicitHandle = true;
  6842. }
  6843. else if( (op == ttMinus || op == ttPlus || op == ttBitNot || op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  6844. {
  6845. // Look for the appropriate method
  6846. // There is no overloadable operator for unary plus
  6847. const char *opName = 0;
  6848. switch( op )
  6849. {
  6850. case ttMinus: opName = "opNeg"; break;
  6851. case ttBitNot: opName = "opCom"; break;
  6852. case ttInc: opName = "opPreInc"; break;
  6853. case ttDec: opName = "opPreDec"; break;
  6854. }
  6855. if( opName )
  6856. {
  6857. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  6858. ProcessPropertyGetAccessor(ctx, node);
  6859. // Is it a const value?
  6860. bool isConst = false;
  6861. if( ctx->type.dataType.IsObjectHandle() )
  6862. isConst = ctx->type.dataType.IsHandleToConst();
  6863. else
  6864. isConst = ctx->type.dataType.IsReadOnly();
  6865. // 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
  6866. // Find the correct method
  6867. asCArray<int> funcs;
  6868. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  6869. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  6870. {
  6871. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  6872. if( func->name == opName &&
  6873. func->parameterTypes.GetLength() == 0 &&
  6874. (!isConst || func->isReadOnly) )
  6875. {
  6876. funcs.PushLast(func->id);
  6877. }
  6878. }
  6879. // Did we find the method?
  6880. if( funcs.GetLength() == 1 )
  6881. {
  6882. asCTypeInfo objType = ctx->type;
  6883. asCArray<asSExprContext *> args;
  6884. MakeFunctionCall(ctx, funcs[0], objType.dataType.GetObjectType(), args, node);
  6885. ReleaseTemporaryVariable(objType, &ctx->bc);
  6886. return 0;
  6887. }
  6888. else if( funcs.GetLength() == 0 )
  6889. {
  6890. asCString str;
  6891. str = asCString(opName) + "()";
  6892. if( isConst )
  6893. str += " const";
  6894. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  6895. Error(str.AddressOf(), node);
  6896. ctx->type.SetDummy();
  6897. return -1;
  6898. }
  6899. else if( funcs.GetLength() > 1 )
  6900. {
  6901. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  6902. PrintMatchingFuncs(funcs, node);
  6903. ctx->type.SetDummy();
  6904. return -1;
  6905. }
  6906. }
  6907. else if( op == ttPlus )
  6908. {
  6909. Error(TXT_ILLEGAL_OPERATION, node);
  6910. ctx->type.SetDummy();
  6911. return -1;
  6912. }
  6913. }
  6914. else if( op == ttPlus || op == ttMinus )
  6915. {
  6916. ProcessPropertyGetAccessor(ctx, node);
  6917. asCDataType to = ctx->type.dataType;
  6918. // TODO: The case -2147483648 gives an unecessary warning of changed sign for implicit conversion
  6919. if( ctx->type.dataType.IsUnsignedType() || ctx->type.dataType.IsEnumType() )
  6920. {
  6921. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  6922. to = asCDataType::CreatePrimitive(ttInt8, false);
  6923. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  6924. to = asCDataType::CreatePrimitive(ttInt16, false);
  6925. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  6926. to = asCDataType::CreatePrimitive(ttInt, false);
  6927. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  6928. to = asCDataType::CreatePrimitive(ttInt64, false);
  6929. else
  6930. {
  6931. Error(TXT_INVALID_TYPE, node);
  6932. return -1;
  6933. }
  6934. }
  6935. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  6936. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  6937. if( !ctx->type.isConstant )
  6938. {
  6939. ConvertToTempVariable(ctx);
  6940. asASSERT(!ctx->type.isLValue);
  6941. if( op == ttMinus )
  6942. {
  6943. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  6944. ctx->bc.InstrSHORT(asBC_NEGi, ctx->type.stackOffset);
  6945. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  6946. ctx->bc.InstrSHORT(asBC_NEGi64, ctx->type.stackOffset);
  6947. else if( ctx->type.dataType.IsFloatType() )
  6948. ctx->bc.InstrSHORT(asBC_NEGf, ctx->type.stackOffset);
  6949. else if( ctx->type.dataType.IsDoubleType() )
  6950. ctx->bc.InstrSHORT(asBC_NEGd, ctx->type.stackOffset);
  6951. else
  6952. {
  6953. Error(TXT_ILLEGAL_OPERATION, node);
  6954. return -1;
  6955. }
  6956. return 0;
  6957. }
  6958. }
  6959. else
  6960. {
  6961. if( op == ttMinus )
  6962. {
  6963. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  6964. ctx->type.intValue = -ctx->type.intValue;
  6965. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  6966. ctx->type.qwordValue = -(asINT64)ctx->type.qwordValue;
  6967. else if( ctx->type.dataType.IsFloatType() )
  6968. ctx->type.floatValue = -ctx->type.floatValue;
  6969. else if( ctx->type.dataType.IsDoubleType() )
  6970. ctx->type.doubleValue = -ctx->type.doubleValue;
  6971. else
  6972. {
  6973. Error(TXT_ILLEGAL_OPERATION, node);
  6974. return -1;
  6975. }
  6976. return 0;
  6977. }
  6978. }
  6979. if( op == ttPlus )
  6980. {
  6981. if( !ctx->type.dataType.IsIntegerType() &&
  6982. !ctx->type.dataType.IsFloatType() &&
  6983. !ctx->type.dataType.IsDoubleType() )
  6984. {
  6985. Error(TXT_ILLEGAL_OPERATION, node);
  6986. return -1;
  6987. }
  6988. }
  6989. }
  6990. else if( op == ttNot )
  6991. {
  6992. if( ctx->type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  6993. {
  6994. if( ctx->type.isConstant )
  6995. {
  6996. ctx->type.dwordValue = (ctx->type.dwordValue == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  6997. return 0;
  6998. }
  6999. ProcessPropertyGetAccessor(ctx, node);
  7000. ConvertToTempVariable(ctx);
  7001. asASSERT(!ctx->type.isLValue);
  7002. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  7003. }
  7004. else
  7005. {
  7006. Error(TXT_ILLEGAL_OPERATION, node);
  7007. return -1;
  7008. }
  7009. }
  7010. else if( op == ttBitNot )
  7011. {
  7012. ProcessPropertyGetAccessor(ctx, node);
  7013. asCDataType to = ctx->type.dataType;
  7014. if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsEnumType() )
  7015. {
  7016. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  7017. to = asCDataType::CreatePrimitive(ttUInt8, false);
  7018. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  7019. to = asCDataType::CreatePrimitive(ttUInt16, false);
  7020. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  7021. to = asCDataType::CreatePrimitive(ttUInt, false);
  7022. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  7023. to = asCDataType::CreatePrimitive(ttUInt64, false);
  7024. else
  7025. {
  7026. Error(TXT_INVALID_TYPE, node);
  7027. return -1;
  7028. }
  7029. }
  7030. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  7031. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  7032. if( ctx->type.dataType.IsUnsignedType() )
  7033. {
  7034. if( ctx->type.isConstant )
  7035. {
  7036. ctx->type.qwordValue = ~ctx->type.qwordValue;
  7037. return 0;
  7038. }
  7039. ConvertToTempVariable(ctx);
  7040. asASSERT(!ctx->type.isLValue);
  7041. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  7042. ctx->bc.InstrSHORT(asBC_BNOT, ctx->type.stackOffset);
  7043. else
  7044. ctx->bc.InstrSHORT(asBC_BNOT64, ctx->type.stackOffset);
  7045. }
  7046. else
  7047. {
  7048. Error(TXT_ILLEGAL_OPERATION, node);
  7049. return -1;
  7050. }
  7051. }
  7052. else if( op == ttInc || op == ttDec )
  7053. {
  7054. // Need a reference to the primitive that will be updated
  7055. // The result of this expression is the same reference as before
  7056. // Make sure the reference isn't a temporary variable
  7057. if( ctx->type.isTemporary )
  7058. {
  7059. Error(TXT_REF_IS_TEMP, node);
  7060. return -1;
  7061. }
  7062. if( ctx->type.dataType.IsReadOnly() )
  7063. {
  7064. Error(TXT_REF_IS_READ_ONLY, node);
  7065. return -1;
  7066. }
  7067. if( ctx->property_get || ctx->property_set )
  7068. {
  7069. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  7070. return -1;
  7071. }
  7072. if( !ctx->type.isLValue )
  7073. {
  7074. Error(TXT_NOT_LVALUE, node);
  7075. return -1;
  7076. }
  7077. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  7078. ConvertToReference(ctx);
  7079. else if( !ctx->type.dataType.IsReference() )
  7080. {
  7081. Error(TXT_NOT_VALID_REFERENCE, node);
  7082. return -1;
  7083. }
  7084. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  7085. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  7086. {
  7087. if( op == ttInc )
  7088. ctx->bc.Instr(asBC_INCi64);
  7089. else
  7090. ctx->bc.Instr(asBC_DECi64);
  7091. }
  7092. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt, false)) ||
  7093. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt, false)) )
  7094. {
  7095. if( op == ttInc )
  7096. ctx->bc.Instr(asBC_INCi);
  7097. else
  7098. ctx->bc.Instr(asBC_DECi);
  7099. }
  7100. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  7101. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  7102. {
  7103. if( op == ttInc )
  7104. ctx->bc.Instr(asBC_INCi16);
  7105. else
  7106. ctx->bc.Instr(asBC_DECi16);
  7107. }
  7108. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  7109. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  7110. {
  7111. if( op == ttInc )
  7112. ctx->bc.Instr(asBC_INCi8);
  7113. else
  7114. ctx->bc.Instr(asBC_DECi8);
  7115. }
  7116. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttFloat, false)) )
  7117. {
  7118. if( op == ttInc )
  7119. ctx->bc.Instr(asBC_INCf);
  7120. else
  7121. ctx->bc.Instr(asBC_DECf);
  7122. }
  7123. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttDouble, false)) )
  7124. {
  7125. if( op == ttInc )
  7126. ctx->bc.Instr(asBC_INCd);
  7127. else
  7128. ctx->bc.Instr(asBC_DECd);
  7129. }
  7130. else
  7131. {
  7132. Error(TXT_ILLEGAL_OPERATION, node);
  7133. return -1;
  7134. }
  7135. }
  7136. else
  7137. {
  7138. // Unknown operator
  7139. asASSERT(false);
  7140. return -1;
  7141. }
  7142. return 0;
  7143. }
  7144. void asCCompiler::ConvertToReference(asSExprContext *ctx)
  7145. {
  7146. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  7147. {
  7148. ctx->bc.InstrSHORT(asBC_LDV, ctx->type.stackOffset);
  7149. ctx->type.dataType.MakeReference(true);
  7150. ctx->type.SetVariable(ctx->type.dataType, ctx->type.stackOffset, ctx->type.isTemporary);
  7151. }
  7152. }
  7153. int asCCompiler::FindPropertyAccessor(const asCString &name, asSExprContext *ctx, asCScriptNode *node, bool isThisAccess)
  7154. {
  7155. return FindPropertyAccessor(name, ctx, 0, node, isThisAccess);
  7156. }
  7157. int asCCompiler::FindPropertyAccessor(const asCString &name, asSExprContext *ctx, asSExprContext *arg, asCScriptNode *node, bool isThisAccess)
  7158. {
  7159. if( engine->ep.propertyAccessorMode == 0 )
  7160. {
  7161. // Property accessors have been disabled by the application
  7162. return 0;
  7163. }
  7164. int getId = 0, setId = 0;
  7165. asCString getName = "get_" + name;
  7166. asCString setName = "set_" + name;
  7167. asCArray<int> multipleGetFuncs, multipleSetFuncs;
  7168. if( ctx->type.dataType.IsObject() )
  7169. {
  7170. // Check if the object has any methods with the corresponding accessor name(s)
  7171. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  7172. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  7173. {
  7174. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  7175. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  7176. if( f->name == getName && (int)f->parameterTypes.GetLength() == (arg?1:0) )
  7177. {
  7178. if( getId == 0 )
  7179. getId = ot->methods[n];
  7180. else
  7181. {
  7182. if( multipleGetFuncs.GetLength() == 0 )
  7183. multipleGetFuncs.PushLast(getId);
  7184. multipleGetFuncs.PushLast(ot->methods[n]);
  7185. }
  7186. }
  7187. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  7188. if( f->name == setName && (int)f->parameterTypes.GetLength() == (arg?2:1) )
  7189. {
  7190. if( setId == 0 )
  7191. setId = ot->methods[n];
  7192. else
  7193. {
  7194. if( multipleSetFuncs.GetLength() == 0 )
  7195. multipleSetFuncs.PushLast(setId);
  7196. multipleSetFuncs.PushLast(ot->methods[n]);
  7197. }
  7198. }
  7199. }
  7200. }
  7201. else
  7202. {
  7203. // Look for appropriate global functions.
  7204. asCArray<int> funcs;
  7205. asUINT n;
  7206. // TODO: namespace: use the proper namespace
  7207. builder->GetFunctionDescriptions(getName.AddressOf(), funcs, "");
  7208. for( n = 0; n < funcs.GetLength(); n++ )
  7209. {
  7210. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  7211. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  7212. if( (int)f->parameterTypes.GetLength() == (arg?1:0) )
  7213. {
  7214. if( getId == 0 )
  7215. getId = funcs[n];
  7216. else
  7217. {
  7218. if( multipleGetFuncs.GetLength() == 0 )
  7219. multipleGetFuncs.PushLast(getId);
  7220. multipleGetFuncs.PushLast(funcs[n]);
  7221. }
  7222. }
  7223. }
  7224. funcs.SetLength(0);
  7225. // TODO: namespace: use the proper namespace
  7226. builder->GetFunctionDescriptions(setName.AddressOf(), funcs, "");
  7227. for( n = 0; n < funcs.GetLength(); n++ )
  7228. {
  7229. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  7230. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  7231. if( (int)f->parameterTypes.GetLength() == (arg?2:1) )
  7232. {
  7233. if( setId == 0 )
  7234. setId = funcs[n];
  7235. else
  7236. {
  7237. if( multipleSetFuncs.GetLength() == 0 )
  7238. multipleSetFuncs.PushLast(getId);
  7239. multipleSetFuncs.PushLast(funcs[n]);
  7240. }
  7241. }
  7242. }
  7243. }
  7244. bool isConst = false;
  7245. if( ctx->type.dataType.IsObjectHandle() )
  7246. isConst = ctx->type.dataType.IsHandleToConst();
  7247. else
  7248. isConst = ctx->type.dataType.IsReadOnly();
  7249. // Check for multiple matches
  7250. if( multipleGetFuncs.GetLength() > 0 )
  7251. {
  7252. // Filter the list by constness
  7253. FilterConst(multipleGetFuncs, !isConst);
  7254. if( multipleGetFuncs.GetLength() > 1 )
  7255. {
  7256. asCString str;
  7257. str.Format(TXT_MULTIPLE_PROP_GET_ACCESSOR_FOR_s, name.AddressOf());
  7258. Error(str.AddressOf(), node);
  7259. PrintMatchingFuncs(multipleGetFuncs, node);
  7260. return -1;
  7261. }
  7262. else
  7263. {
  7264. // The id may have changed
  7265. getId = multipleGetFuncs[0];
  7266. }
  7267. }
  7268. if( multipleSetFuncs.GetLength() > 0 )
  7269. {
  7270. // Filter the list by constness
  7271. FilterConst(multipleSetFuncs, !isConst);
  7272. if( multipleSetFuncs.GetLength() > 1 )
  7273. {
  7274. asCString str;
  7275. str.Format(TXT_MULTIPLE_PROP_SET_ACCESSOR_FOR_s, name.AddressOf());
  7276. Error(str.AddressOf(), node);
  7277. PrintMatchingFuncs(multipleSetFuncs, node);
  7278. return -1;
  7279. }
  7280. else
  7281. {
  7282. // The id may have changed
  7283. setId = multipleSetFuncs[0];
  7284. }
  7285. }
  7286. // Check for type compatibility between get and set accessor
  7287. if( getId && setId )
  7288. {
  7289. asCScriptFunction *getFunc = builder->GetFunctionDescription(getId);
  7290. asCScriptFunction *setFunc = builder->GetFunctionDescription(setId);
  7291. // It is permitted for a getter to return a handle and the setter to take a reference
  7292. int idx = (arg?1:0);
  7293. if( !getFunc->returnType.IsEqualExceptRefAndConst(setFunc->parameterTypes[idx]) &&
  7294. !((getFunc->returnType.IsObjectHandle() && !setFunc->parameterTypes[idx].IsObjectHandle()) &&
  7295. (getFunc->returnType.GetObjectType() == setFunc->parameterTypes[idx].GetObjectType())) )
  7296. {
  7297. asCString str;
  7298. str.Format(TXT_GET_SET_ACCESSOR_TYPE_MISMATCH_FOR_s, name.AddressOf());
  7299. Error(str.AddressOf(), node);
  7300. asCArray<int> funcs;
  7301. funcs.PushLast(getId);
  7302. funcs.PushLast(setId);
  7303. PrintMatchingFuncs(funcs, node);
  7304. return -1;
  7305. }
  7306. }
  7307. // Check if we are within one of the accessors
  7308. int realGetId = getId;
  7309. int realSetId = setId;
  7310. if( outFunc->objectType && isThisAccess )
  7311. {
  7312. // The property accessors would be virtual functions, so we need to find the real implementation
  7313. asCScriptFunction *getFunc = getId ? builder->GetFunctionDescription(getId) : 0;
  7314. if( getFunc &&
  7315. getFunc->funcType == asFUNC_VIRTUAL &&
  7316. outFunc->objectType->DerivesFrom(getFunc->objectType) )
  7317. realGetId = outFunc->objectType->virtualFunctionTable[getFunc->vfTableIdx]->id;
  7318. asCScriptFunction *setFunc = setId ? builder->GetFunctionDescription(setId) : 0;
  7319. if( setFunc &&
  7320. setFunc->funcType == asFUNC_VIRTUAL &&
  7321. outFunc->objectType->DerivesFrom(setFunc->objectType) )
  7322. realSetId = outFunc->objectType->virtualFunctionTable[setFunc->vfTableIdx]->id;
  7323. }
  7324. // Avoid recursive call, by not treating this as a property accessor call.
  7325. // This will also allow having the real property with the same name as the accessors.
  7326. if( (isThisAccess || outFunc->objectType == 0) &&
  7327. ((realGetId && realGetId == outFunc->id) ||
  7328. (realSetId && realSetId == outFunc->id)) )
  7329. {
  7330. getId = 0;
  7331. setId = 0;
  7332. }
  7333. // Check if the application has disabled script written property accessors
  7334. if( engine->ep.propertyAccessorMode == 1 )
  7335. {
  7336. if( getId && builder->GetFunctionDescription(getId)->funcType != asFUNC_SYSTEM )
  7337. getId = 0;
  7338. if( setId && builder->GetFunctionDescription(setId)->funcType != asFUNC_SYSTEM )
  7339. setId = 0;
  7340. }
  7341. if( getId || setId )
  7342. {
  7343. // Property accessors were found, but we don't know which is to be used yet, so
  7344. // we just prepare the bytecode for the method call, and then store the function ids
  7345. // so that the right one can be used when we get there.
  7346. ctx->property_get = getId;
  7347. ctx->property_set = setId;
  7348. if( ctx->type.dataType.IsObject() )
  7349. {
  7350. // If the object is read-only then we need to remember that
  7351. if( (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) ||
  7352. (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) )
  7353. ctx->property_const = true;
  7354. else
  7355. ctx->property_const = false;
  7356. // If the object is a handle then we need to remember that
  7357. ctx->property_handle = ctx->type.dataType.IsObjectHandle();
  7358. ctx->property_ref = ctx->type.dataType.IsReference();
  7359. }
  7360. // The setter's parameter type is used as the property type,
  7361. // unless only the getter is available
  7362. asCDataType dt;
  7363. if( setId )
  7364. dt = builder->GetFunctionDescription(setId)->parameterTypes[(arg?1:0)];
  7365. else
  7366. dt = builder->GetFunctionDescription(getId)->returnType;
  7367. // Just change the type, the context must still maintain information
  7368. // about previous variable offset and the indicator of temporary variable.
  7369. int offset = ctx->type.stackOffset;
  7370. bool isTemp = ctx->type.isTemporary;
  7371. ctx->type.Set(dt);
  7372. ctx->type.stackOffset = (short)offset;
  7373. ctx->type.isTemporary = isTemp;
  7374. ctx->exprNode = node;
  7375. // Store the argument for later use
  7376. if( arg )
  7377. {
  7378. ctx->property_arg = asNEW(asSExprContext)(engine);
  7379. if( ctx->property_arg == 0 )
  7380. {
  7381. // Out of memory
  7382. return -1;
  7383. }
  7384. MergeExprBytecodeAndType(ctx->property_arg, arg);
  7385. }
  7386. return 1;
  7387. }
  7388. // No accessor was found
  7389. return 0;
  7390. }
  7391. int asCCompiler::ProcessPropertySetAccessor(asSExprContext *ctx, asSExprContext *arg, asCScriptNode *node)
  7392. {
  7393. // TODO: A lot of this code is similar to ProcessPropertyGetAccessor. Can we unify them?
  7394. if( !ctx->property_set )
  7395. {
  7396. Error(TXT_PROPERTY_HAS_NO_SET_ACCESSOR, node);
  7397. return -1;
  7398. }
  7399. asCTypeInfo objType = ctx->type;
  7400. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_set);
  7401. // Make sure the arg match the property
  7402. asCArray<int> funcs;
  7403. funcs.PushLast(ctx->property_set);
  7404. asCArray<asSExprContext *> args;
  7405. if( ctx->property_arg )
  7406. args.PushLast(ctx->property_arg);
  7407. args.PushLast(arg);
  7408. MatchFunctions(funcs, args, node, func->GetName(), func->objectType, ctx->property_const);
  7409. if( funcs.GetLength() == 0 )
  7410. {
  7411. // MatchFunctions already reported the error
  7412. if( ctx->property_arg )
  7413. {
  7414. asDELETE(ctx->property_arg, asSExprContext);
  7415. ctx->property_arg = 0;
  7416. }
  7417. return -1;
  7418. }
  7419. if( func->objectType )
  7420. {
  7421. // Setup the context with the original type so the method call gets built correctly
  7422. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  7423. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  7424. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  7425. // Don't allow the call if the object is read-only and the property accessor is not const
  7426. if( ctx->property_const && !func->isReadOnly )
  7427. {
  7428. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  7429. asCArray<int> funcs;
  7430. funcs.PushLast(ctx->property_set);
  7431. PrintMatchingFuncs(funcs, node);
  7432. }
  7433. }
  7434. // Call the accessor
  7435. MakeFunctionCall(ctx, ctx->property_set, func->objectType, args, node);
  7436. if( func->objectType )
  7437. {
  7438. // TODO: This is from CompileExpressionPostOp, can we unify the code?
  7439. if( !objType.isTemporary ||
  7440. !ctx->type.dataType.IsReference() ||
  7441. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  7442. {
  7443. // As the method didn't return a reference to a member
  7444. // we can safely release the original object now
  7445. ReleaseTemporaryVariable(objType, &ctx->bc);
  7446. }
  7447. }
  7448. ctx->property_get = 0;
  7449. ctx->property_set = 0;
  7450. if( ctx->property_arg )
  7451. {
  7452. asDELETE(ctx->property_arg, asSExprContext);
  7453. ctx->property_arg = 0;
  7454. }
  7455. return 0;
  7456. }
  7457. void asCCompiler::ProcessPropertyGetAccessor(asSExprContext *ctx, asCScriptNode *node)
  7458. {
  7459. // If no property accessor has been prepared then don't do anything
  7460. if( !ctx->property_get && !ctx->property_set )
  7461. return;
  7462. if( !ctx->property_get )
  7463. {
  7464. // Raise error on missing accessor
  7465. Error(TXT_PROPERTY_HAS_NO_GET_ACCESSOR, node);
  7466. ctx->type.SetDummy();
  7467. return;
  7468. }
  7469. asCTypeInfo objType = ctx->type;
  7470. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_get);
  7471. // Make sure the arg match the property
  7472. asCArray<int> funcs;
  7473. funcs.PushLast(ctx->property_get);
  7474. asCArray<asSExprContext *> args;
  7475. if( ctx->property_arg )
  7476. args.PushLast(ctx->property_arg);
  7477. MatchFunctions(funcs, args, node, func->GetName(), func->objectType, ctx->property_const);
  7478. if( funcs.GetLength() == 0 )
  7479. {
  7480. // MatchFunctions already reported the error
  7481. if( ctx->property_arg )
  7482. {
  7483. asDELETE(ctx->property_arg, asSExprContext);
  7484. ctx->property_arg = 0;
  7485. }
  7486. ctx->type.SetDummy();
  7487. return;
  7488. }
  7489. if( func->objectType )
  7490. {
  7491. // Setup the context with the original type so the method call gets built correctly
  7492. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  7493. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  7494. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  7495. // Don't allow the call if the object is read-only and the property accessor is not const
  7496. if( ctx->property_const && !func->isReadOnly )
  7497. {
  7498. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  7499. asCArray<int> funcs;
  7500. funcs.PushLast(ctx->property_get);
  7501. PrintMatchingFuncs(funcs, node);
  7502. }
  7503. }
  7504. // Call the accessor
  7505. MakeFunctionCall(ctx, ctx->property_get, func->objectType, args, node);
  7506. if( func->objectType )
  7507. {
  7508. // TODO: This is from CompileExpressionPostOp, can we unify the code?
  7509. if( !objType.isTemporary ||
  7510. !ctx->type.dataType.IsReference() ||
  7511. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  7512. {
  7513. // As the method didn't return a reference to a member
  7514. // we can safely release the original object now
  7515. ReleaseTemporaryVariable(objType, &ctx->bc);
  7516. }
  7517. }
  7518. ctx->property_get = 0;
  7519. ctx->property_set = 0;
  7520. if( ctx->property_arg )
  7521. {
  7522. asDELETE(ctx->property_arg, asSExprContext);
  7523. ctx->property_arg = 0;
  7524. }
  7525. }
  7526. int asCCompiler::CompileExpressionPostOp(asCScriptNode *node, asSExprContext *ctx)
  7527. {
  7528. int op = node->tokenType;
  7529. // Check if the variable is initialized (if it indeed is a variable)
  7530. IsVariableInitialized(&ctx->type, node);
  7531. if( (op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  7532. {
  7533. const char *opName = 0;
  7534. switch( op )
  7535. {
  7536. case ttInc: opName = "opPostInc"; break;
  7537. case ttDec: opName = "opPostDec"; break;
  7538. }
  7539. if( opName )
  7540. {
  7541. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  7542. ProcessPropertyGetAccessor(ctx, node);
  7543. // Is it a const value?
  7544. bool isConst = false;
  7545. if( ctx->type.dataType.IsObjectHandle() )
  7546. isConst = ctx->type.dataType.IsHandleToConst();
  7547. else
  7548. isConst = ctx->type.dataType.IsReadOnly();
  7549. // 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
  7550. // Find the correct method
  7551. asCArray<int> funcs;
  7552. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  7553. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  7554. {
  7555. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  7556. if( func->name == opName &&
  7557. func->parameterTypes.GetLength() == 0 &&
  7558. (!isConst || func->isReadOnly) )
  7559. {
  7560. funcs.PushLast(func->id);
  7561. }
  7562. }
  7563. // Did we find the method?
  7564. if( funcs.GetLength() == 1 )
  7565. {
  7566. asCTypeInfo objType = ctx->type;
  7567. asCArray<asSExprContext *> args;
  7568. MakeFunctionCall(ctx, funcs[0], objType.dataType.GetObjectType(), args, node);
  7569. ReleaseTemporaryVariable(objType, &ctx->bc);
  7570. return 0;
  7571. }
  7572. else if( funcs.GetLength() == 0 )
  7573. {
  7574. asCString str;
  7575. str = asCString(opName) + "()";
  7576. if( isConst )
  7577. str += " const";
  7578. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  7579. Error(str.AddressOf(), node);
  7580. ctx->type.SetDummy();
  7581. return -1;
  7582. }
  7583. else if( funcs.GetLength() > 1 )
  7584. {
  7585. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  7586. PrintMatchingFuncs(funcs, node);
  7587. ctx->type.SetDummy();
  7588. return -1;
  7589. }
  7590. }
  7591. }
  7592. else if( op == ttInc || op == ttDec )
  7593. {
  7594. // Make sure the reference isn't a temporary variable
  7595. if( ctx->type.isTemporary )
  7596. {
  7597. Error(TXT_REF_IS_TEMP, node);
  7598. return -1;
  7599. }
  7600. if( ctx->type.dataType.IsReadOnly() )
  7601. {
  7602. Error(TXT_REF_IS_READ_ONLY, node);
  7603. return -1;
  7604. }
  7605. if( ctx->property_get || ctx->property_set )
  7606. {
  7607. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  7608. return -1;
  7609. }
  7610. if( !ctx->type.isLValue )
  7611. {
  7612. Error(TXT_NOT_LVALUE, node);
  7613. return -1;
  7614. }
  7615. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  7616. ConvertToReference(ctx);
  7617. else if( !ctx->type.dataType.IsReference() )
  7618. {
  7619. Error(TXT_NOT_VALID_REFERENCE, node);
  7620. return -1;
  7621. }
  7622. // Copy the value to a temp before changing it
  7623. ConvertToTempVariable(ctx);
  7624. asASSERT(!ctx->type.isLValue);
  7625. // Increment the value pointed to by the reference still in the register
  7626. asEBCInstr iInc = asBC_INCi, iDec = asBC_DECi;
  7627. if( ctx->type.dataType.IsDoubleType() )
  7628. {
  7629. iInc = asBC_INCd;
  7630. iDec = asBC_DECd;
  7631. }
  7632. else if( ctx->type.dataType.IsFloatType() )
  7633. {
  7634. iInc = asBC_INCf;
  7635. iDec = asBC_DECf;
  7636. }
  7637. else if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() )
  7638. {
  7639. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  7640. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  7641. {
  7642. iInc = asBC_INCi16;
  7643. iDec = asBC_DECi16;
  7644. }
  7645. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  7646. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  7647. {
  7648. iInc = asBC_INCi8;
  7649. iDec = asBC_DECi8;
  7650. }
  7651. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  7652. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  7653. {
  7654. iInc = asBC_INCi64;
  7655. iDec = asBC_DECi64;
  7656. }
  7657. }
  7658. else
  7659. {
  7660. Error(TXT_ILLEGAL_OPERATION, node);
  7661. return -1;
  7662. }
  7663. if( op == ttInc ) ctx->bc.Instr(iInc); else ctx->bc.Instr(iDec);
  7664. }
  7665. else if( op == ttDot )
  7666. {
  7667. if( node->firstChild->nodeType == snIdentifier )
  7668. {
  7669. ProcessPropertyGetAccessor(ctx, node);
  7670. // Get the property name
  7671. asCString name(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  7672. // We need to look for get/set property accessors.
  7673. // If found, the context stores information on the get/set accessors
  7674. // until it is known which is to be used.
  7675. int r = 0;
  7676. if( node->next && node->next->tokenType == ttOpenBracket )
  7677. {
  7678. // The property accessor should take an index arg
  7679. asSExprContext dummyArg(engine);
  7680. r = FindPropertyAccessor(name, ctx, &dummyArg, node);
  7681. }
  7682. if( r == 0 )
  7683. r = FindPropertyAccessor(name, ctx, node);
  7684. if( r != 0 )
  7685. return r;
  7686. if( !ctx->type.dataType.IsPrimitive() )
  7687. Dereference(ctx, true);
  7688. if( ctx->type.dataType.IsObjectHandle() )
  7689. {
  7690. // Convert the handle to a normal object
  7691. asCDataType dt = ctx->type.dataType;
  7692. dt.MakeHandle(false);
  7693. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  7694. // The handle may not have been an lvalue, but the dereferenced object is
  7695. ctx->type.isLValue = true;
  7696. }
  7697. // Find the property offset and type
  7698. if( ctx->type.dataType.IsObject() )
  7699. {
  7700. bool isConst = ctx->type.dataType.IsReadOnly();
  7701. asCObjectProperty *prop = builder->GetObjectProperty(ctx->type.dataType, name.AddressOf());
  7702. if( prop )
  7703. {
  7704. // Is the property access allowed?
  7705. if( prop->isPrivate && (!outFunc || outFunc->objectType != ctx->type.dataType.GetObjectType()) )
  7706. {
  7707. asCString msg;
  7708. msg.Format(TXT_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  7709. Error(msg.AddressOf(), node);
  7710. }
  7711. // Put the offset on the stack
  7712. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(ctx->type.dataType.GetObjectType(), false)));
  7713. if( prop->type.IsReference() )
  7714. ctx->bc.Instr(asBC_RDSPtr);
  7715. // Reference to primitive must be stored in the temp register
  7716. if( prop->type.IsPrimitive() )
  7717. {
  7718. ctx->bc.Instr(asBC_PopRPtr);
  7719. }
  7720. // Keep information about temporary variables as deferred expression
  7721. if( ctx->type.isTemporary )
  7722. {
  7723. // Add the release of this reference, as a deferred expression
  7724. asSDeferredParam deferred;
  7725. deferred.origExpr = 0;
  7726. deferred.argInOutFlags = asTM_INREF;
  7727. deferred.argNode = 0;
  7728. deferred.argType.SetVariable(ctx->type.dataType, ctx->type.stackOffset, true);
  7729. ctx->deferredParams.PushLast(deferred);
  7730. }
  7731. // Set the new type and make sure it is not treated as a variable anymore
  7732. ctx->type.dataType = prop->type;
  7733. ctx->type.dataType.MakeReference(true);
  7734. ctx->type.isVariable = false;
  7735. ctx->type.isTemporary = false;
  7736. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  7737. {
  7738. // Objects that are members are not references
  7739. ctx->type.dataType.MakeReference(false);
  7740. }
  7741. ctx->type.dataType.MakeReadOnly(isConst ? true : prop->type.IsReadOnly());
  7742. }
  7743. else
  7744. {
  7745. asCString str;
  7746. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format().AddressOf());
  7747. Error(str.AddressOf(), node);
  7748. return -1;
  7749. }
  7750. }
  7751. else
  7752. {
  7753. asCString str;
  7754. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format().AddressOf());
  7755. Error(str.AddressOf(), node);
  7756. return -1;
  7757. }
  7758. }
  7759. else
  7760. {
  7761. // Make sure it is an object we are accessing
  7762. if( !ctx->type.dataType.IsObject() )
  7763. {
  7764. asCString str;
  7765. str.Format(TXT_ILLEGAL_OPERATION_ON_s, ctx->type.dataType.Format().AddressOf());
  7766. Error(str.AddressOf(), node);
  7767. return -1;
  7768. }
  7769. // Process the get property accessor
  7770. ProcessPropertyGetAccessor(ctx, node);
  7771. bool isConst = false;
  7772. if( ctx->type.dataType.IsObjectHandle() )
  7773. isConst = ctx->type.dataType.IsHandleToConst();
  7774. else
  7775. isConst = ctx->type.dataType.IsReadOnly();
  7776. asCObjectType *trueObj = ctx->type.dataType.GetObjectType();
  7777. asCTypeInfo objType = ctx->type;
  7778. // Compile function call
  7779. int r = CompileFunctionCall(node->firstChild, ctx, trueObj, isConst);
  7780. if( r < 0 ) return r;
  7781. // If the method returned a reference, then we can't release the original
  7782. // object yet, because the reference may be to a member of it
  7783. if( !objType.isTemporary ||
  7784. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  7785. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  7786. {
  7787. // As the method didn't return a reference to a member
  7788. // we can safely release the original object now
  7789. ReleaseTemporaryVariable(objType, &ctx->bc);
  7790. }
  7791. }
  7792. }
  7793. else if( op == ttOpenBracket )
  7794. {
  7795. // If the property access takes an index arg and the argument hasn't been evaluated yet,
  7796. // then we should use that instead of processing it now. If the argument has already been
  7797. // evaluated, then we should process the property accessor as a get access now as the new
  7798. // index operator is on the result of that accessor.
  7799. asCString propertyName;
  7800. if( ((ctx->property_get && builder->GetFunctionDescription(ctx->property_get)->GetParamCount() == 1) ||
  7801. (ctx->property_set && builder->GetFunctionDescription(ctx->property_set)->GetParamCount() == 2)) &&
  7802. (ctx->property_arg && ctx->property_arg->type.dataType.GetTokenType() == ttUnrecognizedToken) )
  7803. {
  7804. // Determine the name of the property accessor
  7805. asCScriptFunction *func = 0;
  7806. if( ctx->property_get )
  7807. func = builder->GetFunctionDescription(ctx->property_get);
  7808. else
  7809. func = builder->GetFunctionDescription(ctx->property_set);
  7810. propertyName = func->GetName();
  7811. propertyName = propertyName.SubString(4);
  7812. // Set the original type of the expression so we can re-evaluate the property accessor
  7813. if( func->objectType )
  7814. {
  7815. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  7816. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  7817. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  7818. }
  7819. else
  7820. {
  7821. ctx->type.SetDummy();
  7822. }
  7823. ctx->property_get = ctx->property_set = 0;
  7824. if( ctx->property_arg )
  7825. {
  7826. asDELETE(ctx->property_arg, asSExprContext);
  7827. ctx->property_arg = 0;
  7828. }
  7829. }
  7830. else
  7831. {
  7832. if( !ctx->type.dataType.IsObject() )
  7833. {
  7834. asCString str;
  7835. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format().AddressOf());
  7836. Error(str.AddressOf(), node);
  7837. return -1;
  7838. }
  7839. ProcessPropertyGetAccessor(ctx, node);
  7840. }
  7841. Dereference(ctx, true);
  7842. // Compile the expression
  7843. asSExprContext expr(engine);
  7844. CompileAssignment(node->firstChild, &expr);
  7845. // Check for the existence of the opIndex method
  7846. asSExprContext lctx(engine);
  7847. MergeExprBytecodeAndType(&lctx, ctx);
  7848. int r = 0;
  7849. if( propertyName == "" )
  7850. r = CompileOverloadedDualOperator2(node, "opIndex", &lctx, &expr, ctx);
  7851. if( r == 0 )
  7852. {
  7853. // Check for accessors methods for the opIndex
  7854. r = FindPropertyAccessor(propertyName == "" ? "opIndex" : propertyName.AddressOf(), &lctx, &expr, node);
  7855. if( r == 0 )
  7856. {
  7857. asCString str;
  7858. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format().AddressOf());
  7859. Error(str.AddressOf(), node);
  7860. return -1;
  7861. }
  7862. else if( r < 0 )
  7863. return -1;
  7864. MergeExprBytecodeAndType(ctx, &lctx);
  7865. }
  7866. }
  7867. return 0;
  7868. }
  7869. int asCCompiler::GetPrecedence(asCScriptNode *op)
  7870. {
  7871. // x * y, x / y, x % y
  7872. // x + y, x - y
  7873. // x <= y, x < y, x >= y, x > y
  7874. // x = =y, x != y, x xor y, x is y, x !is y
  7875. // x and y
  7876. // x or y
  7877. // The following are not used in this function,
  7878. // but should have lower precedence than the above
  7879. // x ? y : z
  7880. // x = y
  7881. // The expression term have the highest precedence
  7882. if( op->nodeType == snExprTerm )
  7883. return 1;
  7884. // Evaluate operators by token
  7885. int tokenType = op->tokenType;
  7886. if( tokenType == ttStar || tokenType == ttSlash || tokenType == ttPercent )
  7887. return 0;
  7888. if( tokenType == ttPlus || tokenType == ttMinus )
  7889. return -1;
  7890. if( tokenType == ttBitShiftLeft ||
  7891. tokenType == ttBitShiftRight ||
  7892. tokenType == ttBitShiftRightArith )
  7893. return -2;
  7894. if( tokenType == ttAmp )
  7895. return -3;
  7896. if( tokenType == ttBitXor )
  7897. return -4;
  7898. if( tokenType == ttBitOr )
  7899. return -5;
  7900. if( tokenType == ttLessThanOrEqual ||
  7901. tokenType == ttLessThan ||
  7902. tokenType == ttGreaterThanOrEqual ||
  7903. tokenType == ttGreaterThan )
  7904. return -6;
  7905. if( tokenType == ttEqual || tokenType == ttNotEqual || tokenType == ttXor || tokenType == ttIs || tokenType == ttNotIs )
  7906. return -7;
  7907. if( tokenType == ttAnd )
  7908. return -8;
  7909. if( tokenType == ttOr )
  7910. return -9;
  7911. // Unknown operator
  7912. asASSERT(false);
  7913. return 0;
  7914. }
  7915. asUINT asCCompiler::MatchArgument(asCArray<int> &funcs, asCArray<int> &matches, const asCTypeInfo *argType, int paramNum, bool allowObjectConstruct)
  7916. {
  7917. asUINT bestCost = asUINT(-1);
  7918. matches.SetLength(0);
  7919. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  7920. {
  7921. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  7922. // Does the function have arguments enough?
  7923. if( (int)desc->parameterTypes.GetLength() <= paramNum )
  7924. continue;
  7925. // Can we make the match by implicit conversion?
  7926. asSExprContext ti(engine);
  7927. ti.type = *argType;
  7928. if( argType->dataType.IsPrimitive() ) ti.type.dataType.MakeReference(false);
  7929. asUINT cost = ImplicitConversion(&ti, desc->parameterTypes[paramNum], 0, asIC_IMPLICIT_CONV, false, allowObjectConstruct);
  7930. // If the function parameter is an inout-reference then it must not be possible to call the
  7931. // function with an incorrect argument type, even though the type can normally be converted.
  7932. if( desc->parameterTypes[paramNum].IsReference() &&
  7933. desc->inOutFlags[paramNum] == asTM_INOUTREF &&
  7934. desc->parameterTypes[paramNum].GetTokenType() != ttQuestion )
  7935. {
  7936. // Observe, that the below checks are only necessary for when unsafe references have been
  7937. // enabled by the application. Without this the &inout reference form wouldn't be allowed
  7938. // for these value types.
  7939. // Don't allow a primitive to be converted to a reference of another primitive type
  7940. if( desc->parameterTypes[paramNum].IsPrimitive() &&
  7941. desc->parameterTypes[paramNum].GetTokenType() != argType->dataType.GetTokenType() )
  7942. {
  7943. asASSERT( engine->ep.allowUnsafeReferences );
  7944. continue;
  7945. }
  7946. // Don't allow an enum to be converted to a reference of another enum type
  7947. if( desc->parameterTypes[paramNum].IsEnumType() &&
  7948. desc->parameterTypes[paramNum].GetObjectType() != argType->dataType.GetObjectType() )
  7949. {
  7950. asASSERT( engine->ep.allowUnsafeReferences );
  7951. continue;
  7952. }
  7953. // Don't allow a non-handle expression to be converted to a reference to a handle
  7954. if( desc->parameterTypes[paramNum].IsObjectHandle() &&
  7955. !argType->dataType.IsObjectHandle() )
  7956. {
  7957. asASSERT( engine->ep.allowUnsafeReferences );
  7958. continue;
  7959. }
  7960. // Don't allow a value type to be converted
  7961. if( (desc->parameterTypes[paramNum].GetObjectType() && (desc->parameterTypes[paramNum].GetObjectType()->GetFlags() & asOBJ_VALUE)) &&
  7962. (desc->parameterTypes[paramNum].GetObjectType() != argType->dataType.GetObjectType()) )
  7963. {
  7964. asASSERT( engine->ep.allowUnsafeReferences );
  7965. continue;
  7966. }
  7967. }
  7968. // How well does the argument match the function parameter?
  7969. if( desc->parameterTypes[paramNum].IsEqualExceptRef(ti.type.dataType) )
  7970. {
  7971. if( cost < bestCost )
  7972. {
  7973. matches.SetLength(0);
  7974. bestCost = cost;
  7975. }
  7976. if( cost == bestCost )
  7977. matches.PushLast(funcs[n]);
  7978. }
  7979. }
  7980. return bestCost;
  7981. }
  7982. void asCCompiler::PrepareArgument2(asSExprContext *ctx, asSExprContext *arg, asCDataType *paramType, bool isFunction, int refType, bool isMakingCopy)
  7983. {
  7984. // Reference parameters whose value won't be used don't evaluate the expression
  7985. if( paramType->IsReference() && !(refType & asTM_INREF) )
  7986. {
  7987. // Store the original bytecode so that it can be reused when processing the deferred output parameter
  7988. asSExprContext *orig = asNEW(asSExprContext)(engine);
  7989. if( orig == 0 )
  7990. {
  7991. // Out of memory
  7992. return;
  7993. }
  7994. MergeExprBytecodeAndType(orig, arg);
  7995. arg->origExpr = orig;
  7996. }
  7997. PrepareArgument(paramType, arg, arg->exprNode, isFunction, refType, isMakingCopy);
  7998. // arg still holds the original expression for output parameters
  7999. ctx->bc.AddCode(&arg->bc);
  8000. }
  8001. bool asCCompiler::CompileOverloadedDualOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8002. {
  8003. ctx->exprNode = node;
  8004. // What type of operator is it?
  8005. int token = node->tokenType;
  8006. if( token == ttUnrecognizedToken )
  8007. {
  8008. // This happens when the compiler is inferring an assignment
  8009. // operation from another action, for example in preparing a value
  8010. // as a function argument
  8011. token = ttAssignment;
  8012. }
  8013. // boolean operators are not overloadable
  8014. if( token == ttAnd ||
  8015. token == ttOr ||
  8016. token == ttXor )
  8017. return false;
  8018. // Dual operators can also be implemented as class methods
  8019. if( token == ttEqual ||
  8020. token == ttNotEqual )
  8021. {
  8022. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  8023. // Find the matching opEquals method
  8024. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  8025. if( r == 0 )
  8026. {
  8027. // Try again by switching the order of the operands
  8028. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  8029. }
  8030. if( r == 1 )
  8031. {
  8032. if( token == ttNotEqual )
  8033. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  8034. // Success, don't continue
  8035. return true;
  8036. }
  8037. else if( r < 0 )
  8038. {
  8039. // Compiler error, don't continue
  8040. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  8041. return true;
  8042. }
  8043. }
  8044. if( token == ttEqual ||
  8045. token == ttNotEqual ||
  8046. token == ttLessThan ||
  8047. token == ttLessThanOrEqual ||
  8048. token == ttGreaterThan ||
  8049. token == ttGreaterThanOrEqual )
  8050. {
  8051. bool swappedOrder = false;
  8052. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  8053. // Find the matching opCmp method
  8054. int r = CompileOverloadedDualOperator2(node, "opCmp", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  8055. if( r == 0 )
  8056. {
  8057. // Try again by switching the order of the operands
  8058. swappedOrder = true;
  8059. r = CompileOverloadedDualOperator2(node, "opCmp", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  8060. }
  8061. if( r == 1 )
  8062. {
  8063. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  8064. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  8065. ctx->bc.InstrW_DW(asBC_CMPIi, ctx->type.stackOffset, 0);
  8066. if( token == ttEqual )
  8067. ctx->bc.Instr(asBC_TZ);
  8068. else if( token == ttNotEqual )
  8069. ctx->bc.Instr(asBC_TNZ);
  8070. else if( (token == ttLessThan && !swappedOrder) ||
  8071. (token == ttGreaterThan && swappedOrder) )
  8072. ctx->bc.Instr(asBC_TS);
  8073. else if( (token == ttLessThanOrEqual && !swappedOrder) ||
  8074. (token == ttGreaterThanOrEqual && swappedOrder) )
  8075. ctx->bc.Instr(asBC_TNP);
  8076. else if( (token == ttGreaterThan && !swappedOrder) ||
  8077. (token == ttLessThan && swappedOrder) )
  8078. ctx->bc.Instr(asBC_TP);
  8079. else if( (token == ttGreaterThanOrEqual && !swappedOrder) ||
  8080. (token == ttLessThanOrEqual && swappedOrder) )
  8081. ctx->bc.Instr(asBC_TNS);
  8082. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  8083. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), a, true);
  8084. // Success, don't continue
  8085. return true;
  8086. }
  8087. else if( r < 0 )
  8088. {
  8089. // Compiler error, don't continue
  8090. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  8091. return true;
  8092. }
  8093. }
  8094. // The rest of the operators are not commutative, and doesn't require specific return type
  8095. const char *op = 0, *op_r = 0;
  8096. switch( token )
  8097. {
  8098. case ttPlus: op = "opAdd"; op_r = "opAdd_r"; break;
  8099. case ttMinus: op = "opSub"; op_r = "opSub_r"; break;
  8100. case ttStar: op = "opMul"; op_r = "opMul_r"; break;
  8101. case ttSlash: op = "opDiv"; op_r = "opDiv_r"; break;
  8102. case ttPercent: op = "opMod"; op_r = "opMod_r"; break;
  8103. case ttBitOr: op = "opOr"; op_r = "opOr_r"; break;
  8104. case ttAmp: op = "opAnd"; op_r = "opAnd_r"; break;
  8105. case ttBitXor: op = "opXor"; op_r = "opXor_r"; break;
  8106. case ttBitShiftLeft: op = "opShl"; op_r = "opShl_r"; break;
  8107. case ttBitShiftRight: op = "opShr"; op_r = "opShr_r"; break;
  8108. case ttBitShiftRightArith: op = "opUShr"; op_r = "opUShr_r"; break;
  8109. }
  8110. // TODO: Might be interesting to support a concatenation operator, e.g. ~
  8111. if( op && op_r )
  8112. {
  8113. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  8114. // Find the matching operator method
  8115. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, ctx);
  8116. if( r == 0 )
  8117. {
  8118. // Try again by switching the order of the operands, and using the reversed operator
  8119. r = CompileOverloadedDualOperator2(node, op_r, rctx, lctx, ctx);
  8120. }
  8121. if( r == 1 )
  8122. {
  8123. // Success, don't continue
  8124. return true;
  8125. }
  8126. else if( r < 0 )
  8127. {
  8128. // Compiler error, don't continue
  8129. ctx->type.SetDummy();
  8130. return true;
  8131. }
  8132. }
  8133. // Assignment operators
  8134. op = 0;
  8135. switch( token )
  8136. {
  8137. case ttAssignment: op = "opAssign"; break;
  8138. case ttAddAssign: op = "opAddAssign"; break;
  8139. case ttSubAssign: op = "opSubAssign"; break;
  8140. case ttMulAssign: op = "opMulAssign"; break;
  8141. case ttDivAssign: op = "opDivAssign"; break;
  8142. case ttModAssign: op = "opModAssign"; break;
  8143. case ttOrAssign: op = "opOrAssign"; break;
  8144. case ttAndAssign: op = "opAndAssign"; break;
  8145. case ttXorAssign: op = "opXorAssign"; break;
  8146. case ttShiftLeftAssign: op = "opShlAssign"; break;
  8147. case ttShiftRightLAssign: op = "opShrAssign"; break;
  8148. case ttShiftRightAAssign: op = "opUShrAssign"; break;
  8149. }
  8150. if( op )
  8151. {
  8152. // TODO: Shouldn't accept const lvalue with the assignment operators
  8153. // Find the matching operator method
  8154. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, ctx);
  8155. if( r == 1 )
  8156. {
  8157. // Success, don't continue
  8158. return true;
  8159. }
  8160. else if( r < 0 )
  8161. {
  8162. // Compiler error, don't continue
  8163. ctx->type.SetDummy();
  8164. return true;
  8165. }
  8166. }
  8167. // No suitable operator was found
  8168. return false;
  8169. }
  8170. // Returns negative on compile error
  8171. // zero on no matching operator
  8172. // one on matching operator
  8173. int asCCompiler::CompileOverloadedDualOperator2(asCScriptNode *node, const char *methodName, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx, bool specificReturn, const asCDataType &returnType)
  8174. {
  8175. // Find the matching method
  8176. if( lctx->type.dataType.IsObject() &&
  8177. (!lctx->type.isExplicitHandle ||
  8178. lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  8179. {
  8180. // Is the left value a const?
  8181. bool isConst = false;
  8182. if( lctx->type.dataType.IsObjectHandle() )
  8183. isConst = lctx->type.dataType.IsHandleToConst();
  8184. else
  8185. isConst = lctx->type.dataType.IsReadOnly();
  8186. asCArray<int> funcs;
  8187. asCObjectType *ot = lctx->type.dataType.GetObjectType();
  8188. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  8189. {
  8190. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  8191. if( func->name == methodName &&
  8192. (!specificReturn || func->returnType == returnType) &&
  8193. func->parameterTypes.GetLength() == 1 &&
  8194. (!isConst || func->isReadOnly) )
  8195. {
  8196. // Make sure the method is accessible by the module
  8197. if( builder->module->accessMask & func->accessMask )
  8198. {
  8199. funcs.PushLast(func->id);
  8200. }
  8201. }
  8202. }
  8203. // Which is the best matching function?
  8204. asCArray<int> ops;
  8205. MatchArgument(funcs, ops, &rctx->type, 0);
  8206. // If the object is not const, then we need to prioritize non-const methods
  8207. if( !isConst )
  8208. FilterConst(ops);
  8209. // Did we find an operator?
  8210. if( ops.GetLength() == 1 )
  8211. {
  8212. // Process the lctx expression as get accessor
  8213. ProcessPropertyGetAccessor(lctx, node);
  8214. // Merge the bytecode so that it forms lvalue.methodName(rvalue)
  8215. asCTypeInfo objType = lctx->type;
  8216. asCArray<asSExprContext *> args;
  8217. args.PushLast(rctx);
  8218. MergeExprBytecode(ctx, lctx);
  8219. ctx->type = lctx->type;
  8220. MakeFunctionCall(ctx, ops[0], objType.dataType.GetObjectType(), args, node);
  8221. // If the method returned a reference, then we can't release the original
  8222. // object yet, because the reference may be to a member of it
  8223. if( !objType.isTemporary ||
  8224. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  8225. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not to a member
  8226. {
  8227. // As the index operator didn't return a reference to a
  8228. // member we can release the original object now
  8229. ReleaseTemporaryVariable(objType, &ctx->bc);
  8230. }
  8231. // Found matching operator
  8232. return 1;
  8233. }
  8234. else if( ops.GetLength() > 1 )
  8235. {
  8236. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  8237. PrintMatchingFuncs(ops, node);
  8238. ctx->type.SetDummy();
  8239. // Compiler error
  8240. return -1;
  8241. }
  8242. }
  8243. // No matching operator
  8244. return 0;
  8245. }
  8246. void asCCompiler::MakeFunctionCall(asSExprContext *ctx, int funcId, asCObjectType *objectType, asCArray<asSExprContext*> &args, asCScriptNode * /*node*/, bool useVariable, int stackOffset, int funcPtrVar)
  8247. {
  8248. if( objectType )
  8249. {
  8250. Dereference(ctx, true);
  8251. // This following warning was removed as there may be valid reasons
  8252. // for calling non-const methods on temporary objects, and we shouldn't
  8253. // warn when there is no way of removing the warning.
  8254. /*
  8255. // Warn if the method is non-const and the object is temporary
  8256. // since the changes will be lost when the object is destroyed.
  8257. // If the object is accessed through a handle, then it is assumed
  8258. // the object is not temporary, even though the handle is.
  8259. if( ctx->type.isTemporary &&
  8260. !ctx->type.dataType.IsObjectHandle() &&
  8261. !engine->scriptFunctions[funcId]->isReadOnly )
  8262. {
  8263. Warning("A non-const method is called on temporary object. Changes to the object may be lost.", node);
  8264. Information(engine->scriptFunctions[funcId]->GetDeclaration(), node);
  8265. }
  8266. */ }
  8267. asCByteCode objBC(engine);
  8268. objBC.AddCode(&ctx->bc);
  8269. PrepareFunctionCall(funcId, &ctx->bc, args);
  8270. // Verify if any of the args variable offsets are used in the other code.
  8271. // If they are exchange the offset for a new one
  8272. asUINT n;
  8273. for( n = 0; n < args.GetLength(); n++ )
  8274. {
  8275. if( args[n]->type.isTemporary && objBC.IsVarUsed(args[n]->type.stackOffset) )
  8276. {
  8277. // Release the current temporary variable
  8278. ReleaseTemporaryVariable(args[n]->type, 0);
  8279. asCDataType dt = args[n]->type.dataType;
  8280. dt.MakeReference(false);
  8281. int l = int(reservedVariables.GetLength());
  8282. objBC.GetVarsUsed(reservedVariables);
  8283. ctx->bc.GetVarsUsed(reservedVariables);
  8284. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(args[n]->type.stackOffset));
  8285. reservedVariables.SetLength(l);
  8286. asASSERT( IsVariableOnHeap(args[n]->type.stackOffset) == IsVariableOnHeap(newOffset) );
  8287. ctx->bc.ExchangeVar(args[n]->type.stackOffset, newOffset);
  8288. args[n]->type.stackOffset = (short)newOffset;
  8289. args[n]->type.isTemporary = true;
  8290. args[n]->type.isVariable = true;
  8291. }
  8292. }
  8293. // If the function will return a value type on the stack, then we must allocate space
  8294. // for that here and push the address on the stack as a hidden argument to the function
  8295. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  8296. if( func->DoesReturnOnStack() )
  8297. {
  8298. asASSERT(!useVariable);
  8299. useVariable = true;
  8300. stackOffset = AllocateVariable(func->returnType, true);
  8301. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  8302. }
  8303. ctx->bc.AddCode(&objBC);
  8304. MoveArgsToStack(funcId, &ctx->bc, args, objectType ? true : false);
  8305. PerformFunctionCall(funcId, ctx, false, &args, 0, useVariable, stackOffset, funcPtrVar);
  8306. }
  8307. int asCCompiler::CompileOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8308. {
  8309. IsVariableInitialized(&lctx->type, node);
  8310. IsVariableInitialized(&rctx->type, node);
  8311. if( lctx->type.isExplicitHandle || rctx->type.isExplicitHandle ||
  8312. node->tokenType == ttIs || node->tokenType == ttNotIs )
  8313. {
  8314. CompileOperatorOnHandles(node, lctx, rctx, ctx);
  8315. return 0;
  8316. }
  8317. else
  8318. {
  8319. // Compile an overloaded operator for the two operands
  8320. if( CompileOverloadedDualOperator(node, lctx, rctx, ctx) )
  8321. return 0;
  8322. // If both operands are objects, then we shouldn't continue
  8323. if( lctx->type.dataType.IsObject() && rctx->type.dataType.IsObject() )
  8324. {
  8325. asCString str;
  8326. str.Format(TXT_NO_MATCHING_OP_FOUND_FOR_TYPES_s_AND_s, lctx->type.dataType.Format().AddressOf(), rctx->type.dataType.Format().AddressOf());
  8327. Error(str.AddressOf(), node);
  8328. ctx->type.SetDummy();
  8329. return -1;
  8330. }
  8331. // Process the property get accessors (if any)
  8332. ProcessPropertyGetAccessor(lctx, node);
  8333. ProcessPropertyGetAccessor(rctx, node);
  8334. // Make sure we have two variables or constants
  8335. if( lctx->type.dataType.IsReference() ) ConvertToVariableNotIn(lctx, rctx);
  8336. if( rctx->type.dataType.IsReference() ) ConvertToVariableNotIn(rctx, lctx);
  8337. // Make sure lctx doesn't end up with a variable used in rctx
  8338. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  8339. {
  8340. int offset = AllocateVariableNotIn(lctx->type.dataType, true, false, rctx);
  8341. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  8342. ReleaseTemporaryVariable(offset, 0);
  8343. }
  8344. // Math operators
  8345. // + - * / % += -= *= /= %=
  8346. int op = node->tokenType;
  8347. if( op == ttPlus || op == ttAddAssign ||
  8348. op == ttMinus || op == ttSubAssign ||
  8349. op == ttStar || op == ttMulAssign ||
  8350. op == ttSlash || op == ttDivAssign ||
  8351. op == ttPercent || op == ttModAssign )
  8352. {
  8353. CompileMathOperator(node, lctx, rctx, ctx);
  8354. return 0;
  8355. }
  8356. // Bitwise operators
  8357. // << >> >>> & | ^ <<= >>= >>>= &= |= ^=
  8358. if( op == ttAmp || op == ttAndAssign ||
  8359. op == ttBitOr || op == ttOrAssign ||
  8360. op == ttBitXor || op == ttXorAssign ||
  8361. op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  8362. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  8363. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8364. {
  8365. CompileBitwiseOperator(node, lctx, rctx, ctx);
  8366. return 0;
  8367. }
  8368. // Comparison operators
  8369. // == != < > <= >=
  8370. if( op == ttEqual || op == ttNotEqual ||
  8371. op == ttLessThan || op == ttLessThanOrEqual ||
  8372. op == ttGreaterThan || op == ttGreaterThanOrEqual )
  8373. {
  8374. CompileComparisonOperator(node, lctx, rctx, ctx);
  8375. return 0;
  8376. }
  8377. // Boolean operators
  8378. // && || ^^
  8379. if( op == ttAnd || op == ttOr || op == ttXor )
  8380. {
  8381. CompileBooleanOperator(node, lctx, rctx, ctx);
  8382. return 0;
  8383. }
  8384. }
  8385. asASSERT(false);
  8386. return -1;
  8387. }
  8388. void asCCompiler::ConvertToTempVariableNotIn(asSExprContext *ctx, asSExprContext *exclude)
  8389. {
  8390. int l = int(reservedVariables.GetLength());
  8391. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  8392. ConvertToTempVariable(ctx);
  8393. reservedVariables.SetLength(l);
  8394. }
  8395. void asCCompiler::ConvertToTempVariable(asSExprContext *ctx)
  8396. {
  8397. // This is only used for primitive types and null handles
  8398. asASSERT( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsNullHandle() );
  8399. ConvertToVariable(ctx);
  8400. if( !ctx->type.isTemporary )
  8401. {
  8402. if( ctx->type.dataType.IsPrimitive() )
  8403. {
  8404. // Copy the variable to a temporary variable
  8405. int offset = AllocateVariable(ctx->type.dataType, true);
  8406. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8407. ctx->bc.InstrW_W(asBC_CpyVtoV4, offset, ctx->type.stackOffset);
  8408. else
  8409. ctx->bc.InstrW_W(asBC_CpyVtoV8, offset, ctx->type.stackOffset);
  8410. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8411. }
  8412. else
  8413. {
  8414. // We should never get here
  8415. asASSERT(false);
  8416. }
  8417. }
  8418. }
  8419. void asCCompiler::ConvertToVariable(asSExprContext *ctx)
  8420. {
  8421. // We should never get here while the context is still an unprocessed property accessor
  8422. asASSERT(ctx->property_get == 0 && ctx->property_set == 0);
  8423. int offset;
  8424. if( !ctx->type.isVariable &&
  8425. (ctx->type.dataType.IsObjectHandle() ||
  8426. (ctx->type.dataType.IsObject() && ctx->type.dataType.SupportHandles())) )
  8427. {
  8428. offset = AllocateVariable(ctx->type.dataType, true);
  8429. if( ctx->type.IsNullConstant() )
  8430. {
  8431. if( ctx->bc.GetLastInstr() == asBC_PshNull )
  8432. ctx->bc.Instr(asBC_PopPtr); // Pop the null constant pushed onto the stack
  8433. ctx->bc.InstrSHORT(asBC_ClrVPtr, (short)offset);
  8434. }
  8435. else
  8436. {
  8437. // Copy the object handle to a variable
  8438. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  8439. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  8440. ctx->bc.Instr(asBC_PopPtr);
  8441. }
  8442. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  8443. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8444. ctx->type.dataType.MakeHandle(true);
  8445. }
  8446. else if( (!ctx->type.isVariable || ctx->type.dataType.IsReference()) &&
  8447. ctx->type.dataType.IsPrimitive() )
  8448. {
  8449. if( ctx->type.isConstant )
  8450. {
  8451. offset = AllocateVariable(ctx->type.dataType, true);
  8452. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  8453. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, ctx->type.byteValue);
  8454. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  8455. ctx->bc.InstrSHORT_W(asBC_SetV2, (short)offset, ctx->type.wordValue);
  8456. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  8457. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, ctx->type.dwordValue);
  8458. else
  8459. ctx->bc.InstrSHORT_QW(asBC_SetV8, (short)offset, ctx->type.qwordValue);
  8460. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8461. return;
  8462. }
  8463. else
  8464. {
  8465. asASSERT(ctx->type.dataType.IsPrimitive());
  8466. asASSERT(ctx->type.dataType.IsReference());
  8467. ctx->type.dataType.MakeReference(false);
  8468. offset = AllocateVariable(ctx->type.dataType, true);
  8469. // Read the value from the address in the register directly into the variable
  8470. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  8471. ctx->bc.InstrSHORT(asBC_RDR1, (short)offset);
  8472. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  8473. ctx->bc.InstrSHORT(asBC_RDR2, (short)offset);
  8474. else if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8475. ctx->bc.InstrSHORT(asBC_RDR4, (short)offset);
  8476. else
  8477. ctx->bc.InstrSHORT(asBC_RDR8, (short)offset);
  8478. }
  8479. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  8480. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  8481. }
  8482. }
  8483. void asCCompiler::ConvertToVariableNotIn(asSExprContext *ctx, asSExprContext *exclude)
  8484. {
  8485. int l = int(reservedVariables.GetLength());
  8486. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  8487. ConvertToVariable(ctx);
  8488. reservedVariables.SetLength(l);
  8489. }
  8490. void asCCompiler::CompileMathOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8491. {
  8492. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  8493. // Implicitly convert the operands to a number type
  8494. asCDataType to;
  8495. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  8496. to.SetTokenType(ttDouble);
  8497. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  8498. to.SetTokenType(ttFloat);
  8499. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8500. {
  8501. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() )
  8502. to.SetTokenType(ttInt64);
  8503. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  8504. to.SetTokenType(ttUInt64);
  8505. }
  8506. else
  8507. {
  8508. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() ||
  8509. lctx->type.dataType.IsEnumType() || rctx->type.dataType.IsEnumType() )
  8510. to.SetTokenType(ttInt);
  8511. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  8512. to.SetTokenType(ttUInt);
  8513. }
  8514. // If doing an operation with double constant and float variable, the constant should be converted to float
  8515. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  8516. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  8517. to.SetTokenType(ttFloat);
  8518. // Do the actual conversion
  8519. int l = int(reservedVariables.GetLength());
  8520. rctx->bc.GetVarsUsed(reservedVariables);
  8521. lctx->bc.GetVarsUsed(reservedVariables);
  8522. if( lctx->type.dataType.IsReference() )
  8523. ConvertToVariable(lctx);
  8524. if( rctx->type.dataType.IsReference() )
  8525. ConvertToVariable(rctx);
  8526. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  8527. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  8528. reservedVariables.SetLength(l);
  8529. // Verify that the conversion was successful
  8530. if( !lctx->type.dataType.IsIntegerType() &&
  8531. !lctx->type.dataType.IsUnsignedType() &&
  8532. !lctx->type.dataType.IsFloatType() &&
  8533. !lctx->type.dataType.IsDoubleType() )
  8534. {
  8535. asCString str;
  8536. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, lctx->type.dataType.Format().AddressOf());
  8537. Error(str.AddressOf(), node);
  8538. ctx->type.SetDummy();
  8539. return;
  8540. }
  8541. if( !rctx->type.dataType.IsIntegerType() &&
  8542. !rctx->type.dataType.IsUnsignedType() &&
  8543. !rctx->type.dataType.IsFloatType() &&
  8544. !rctx->type.dataType.IsDoubleType() )
  8545. {
  8546. asCString str;
  8547. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, rctx->type.dataType.Format().AddressOf());
  8548. Error(str.AddressOf(), node);
  8549. ctx->type.SetDummy();
  8550. return;
  8551. }
  8552. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8553. // Verify if we are dividing with a constant zero
  8554. int op = node->tokenType;
  8555. if( rctx->type.isConstant && rctx->type.qwordValue == 0 &&
  8556. (op == ttSlash || op == ttDivAssign ||
  8557. op == ttPercent || op == ttModAssign) )
  8558. {
  8559. Error(TXT_DIVIDE_BY_ZERO, node);
  8560. }
  8561. if( !isConstant )
  8562. {
  8563. ConvertToVariableNotIn(lctx, rctx);
  8564. ConvertToVariableNotIn(rctx, lctx);
  8565. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8566. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8567. if( op == ttAddAssign || op == ttSubAssign ||
  8568. op == ttMulAssign || op == ttDivAssign ||
  8569. op == ttModAssign )
  8570. {
  8571. // Merge the operands in the different order so that they are evaluated correctly
  8572. MergeExprBytecode(ctx, rctx);
  8573. MergeExprBytecode(ctx, lctx);
  8574. }
  8575. else
  8576. {
  8577. MergeExprBytecode(ctx, lctx);
  8578. MergeExprBytecode(ctx, rctx);
  8579. }
  8580. ProcessDeferredParams(ctx);
  8581. asEBCInstr instruction = asBC_ADDi;
  8582. if( lctx->type.dataType.IsIntegerType() ||
  8583. lctx->type.dataType.IsUnsignedType() )
  8584. {
  8585. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8586. {
  8587. if( op == ttPlus || op == ttAddAssign )
  8588. instruction = asBC_ADDi;
  8589. else if( op == ttMinus || op == ttSubAssign )
  8590. instruction = asBC_SUBi;
  8591. else if( op == ttStar || op == ttMulAssign )
  8592. instruction = asBC_MULi;
  8593. else if( op == ttSlash || op == ttDivAssign )
  8594. {
  8595. if( lctx->type.dataType.IsIntegerType() )
  8596. instruction = asBC_DIVi;
  8597. else
  8598. instruction = asBC_DIVu;
  8599. }
  8600. else if( op == ttPercent || op == ttModAssign )
  8601. {
  8602. if( lctx->type.dataType.IsIntegerType() )
  8603. instruction = asBC_MODi;
  8604. else
  8605. instruction = asBC_MODu;
  8606. }
  8607. }
  8608. else
  8609. {
  8610. if( op == ttPlus || op == ttAddAssign )
  8611. instruction = asBC_ADDi64;
  8612. else if( op == ttMinus || op == ttSubAssign )
  8613. instruction = asBC_SUBi64;
  8614. else if( op == ttStar || op == ttMulAssign )
  8615. instruction = asBC_MULi64;
  8616. else if( op == ttSlash || op == ttDivAssign )
  8617. {
  8618. if( lctx->type.dataType.IsIntegerType() )
  8619. instruction = asBC_DIVi64;
  8620. else
  8621. instruction = asBC_DIVu64;
  8622. }
  8623. else if( op == ttPercent || op == ttModAssign )
  8624. {
  8625. if( lctx->type.dataType.IsIntegerType() )
  8626. instruction = asBC_MODi64;
  8627. else
  8628. instruction = asBC_MODu64;
  8629. }
  8630. }
  8631. }
  8632. else if( lctx->type.dataType.IsFloatType() )
  8633. {
  8634. if( op == ttPlus || op == ttAddAssign )
  8635. instruction = asBC_ADDf;
  8636. else if( op == ttMinus || op == ttSubAssign )
  8637. instruction = asBC_SUBf;
  8638. else if( op == ttStar || op == ttMulAssign )
  8639. instruction = asBC_MULf;
  8640. else if( op == ttSlash || op == ttDivAssign )
  8641. instruction = asBC_DIVf;
  8642. else if( op == ttPercent || op == ttModAssign )
  8643. instruction = asBC_MODf;
  8644. }
  8645. else if( lctx->type.dataType.IsDoubleType() )
  8646. {
  8647. if( op == ttPlus || op == ttAddAssign )
  8648. instruction = asBC_ADDd;
  8649. else if( op == ttMinus || op == ttSubAssign )
  8650. instruction = asBC_SUBd;
  8651. else if( op == ttStar || op == ttMulAssign )
  8652. instruction = asBC_MULd;
  8653. else if( op == ttSlash || op == ttDivAssign )
  8654. instruction = asBC_DIVd;
  8655. else if( op == ttPercent || op == ttModAssign )
  8656. instruction = asBC_MODd;
  8657. }
  8658. else
  8659. {
  8660. // Shouldn't be possible
  8661. asASSERT(false);
  8662. }
  8663. // Do the operation
  8664. int a = AllocateVariable(lctx->type.dataType, true);
  8665. int b = lctx->type.stackOffset;
  8666. int c = rctx->type.stackOffset;
  8667. ctx->bc.InstrW_W_W(instruction, a, b, c);
  8668. ctx->type.SetVariable(lctx->type.dataType, a, true);
  8669. }
  8670. else
  8671. {
  8672. // Both values are constants
  8673. if( lctx->type.dataType.IsIntegerType() ||
  8674. lctx->type.dataType.IsUnsignedType() )
  8675. {
  8676. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8677. {
  8678. int v = 0;
  8679. if( op == ttPlus )
  8680. v = lctx->type.intValue + rctx->type.intValue;
  8681. else if( op == ttMinus )
  8682. v = lctx->type.intValue - rctx->type.intValue;
  8683. else if( op == ttStar )
  8684. v = lctx->type.intValue * rctx->type.intValue;
  8685. else if( op == ttSlash )
  8686. {
  8687. if( rctx->type.intValue == 0 )
  8688. v = 0;
  8689. else
  8690. if( lctx->type.dataType.IsIntegerType() )
  8691. v = lctx->type.intValue / rctx->type.intValue;
  8692. else
  8693. v = lctx->type.dwordValue / rctx->type.dwordValue;
  8694. }
  8695. else if( op == ttPercent )
  8696. {
  8697. if( rctx->type.intValue == 0 )
  8698. v = 0;
  8699. else
  8700. if( lctx->type.dataType.IsIntegerType() )
  8701. v = lctx->type.intValue % rctx->type.intValue;
  8702. else
  8703. v = lctx->type.dwordValue % rctx->type.dwordValue;
  8704. }
  8705. ctx->type.SetConstantDW(lctx->type.dataType, v);
  8706. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  8707. if( lctx->type.dataType.GetTokenType() == ttUInt && op == ttMinus && lctx->type.intValue < rctx->type.intValue )
  8708. ctx->type.dataType.SetTokenType(ttInt);
  8709. }
  8710. else
  8711. {
  8712. asQWORD v = 0;
  8713. if( op == ttPlus )
  8714. v = lctx->type.qwordValue + rctx->type.qwordValue;
  8715. else if( op == ttMinus )
  8716. v = lctx->type.qwordValue - rctx->type.qwordValue;
  8717. else if( op == ttStar )
  8718. v = lctx->type.qwordValue * rctx->type.qwordValue;
  8719. else if( op == ttSlash )
  8720. {
  8721. if( rctx->type.qwordValue == 0 )
  8722. v = 0;
  8723. else
  8724. if( lctx->type.dataType.IsIntegerType() )
  8725. v = asINT64(lctx->type.qwordValue) / asINT64(rctx->type.qwordValue);
  8726. else
  8727. v = lctx->type.qwordValue / rctx->type.qwordValue;
  8728. }
  8729. else if( op == ttPercent )
  8730. {
  8731. if( rctx->type.qwordValue == 0 )
  8732. v = 0;
  8733. else
  8734. if( lctx->type.dataType.IsIntegerType() )
  8735. v = asINT64(lctx->type.qwordValue) % asINT64(rctx->type.qwordValue);
  8736. else
  8737. v = lctx->type.qwordValue % rctx->type.qwordValue;
  8738. }
  8739. ctx->type.SetConstantQW(lctx->type.dataType, v);
  8740. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  8741. if( lctx->type.dataType.GetTokenType() == ttUInt64 && op == ttMinus && lctx->type.qwordValue < rctx->type.qwordValue )
  8742. ctx->type.dataType.SetTokenType(ttInt64);
  8743. }
  8744. }
  8745. else if( lctx->type.dataType.IsFloatType() )
  8746. {
  8747. float v = 0.0f;
  8748. if( op == ttPlus )
  8749. v = lctx->type.floatValue + rctx->type.floatValue;
  8750. else if( op == ttMinus )
  8751. v = lctx->type.floatValue - rctx->type.floatValue;
  8752. else if( op == ttStar )
  8753. v = lctx->type.floatValue * rctx->type.floatValue;
  8754. else if( op == ttSlash )
  8755. {
  8756. if( rctx->type.floatValue == 0 )
  8757. v = 0;
  8758. else
  8759. v = lctx->type.floatValue / rctx->type.floatValue;
  8760. }
  8761. else if( op == ttPercent )
  8762. {
  8763. if( rctx->type.floatValue == 0 )
  8764. v = 0;
  8765. else
  8766. v = fmodf(lctx->type.floatValue, rctx->type.floatValue);
  8767. }
  8768. ctx->type.SetConstantF(lctx->type.dataType, v);
  8769. }
  8770. else if( lctx->type.dataType.IsDoubleType() )
  8771. {
  8772. double v = 0.0;
  8773. if( op == ttPlus )
  8774. v = lctx->type.doubleValue + rctx->type.doubleValue;
  8775. else if( op == ttMinus )
  8776. v = lctx->type.doubleValue - rctx->type.doubleValue;
  8777. else if( op == ttStar )
  8778. v = lctx->type.doubleValue * rctx->type.doubleValue;
  8779. else if( op == ttSlash )
  8780. {
  8781. if( rctx->type.doubleValue == 0 )
  8782. v = 0;
  8783. else
  8784. v = lctx->type.doubleValue / rctx->type.doubleValue;
  8785. }
  8786. else if( op == ttPercent )
  8787. {
  8788. if( rctx->type.doubleValue == 0 )
  8789. v = 0;
  8790. else
  8791. v = fmod(lctx->type.doubleValue, rctx->type.doubleValue);
  8792. }
  8793. ctx->type.SetConstantD(lctx->type.dataType, v);
  8794. }
  8795. else
  8796. {
  8797. // Shouldn't be possible
  8798. asASSERT(false);
  8799. }
  8800. }
  8801. }
  8802. void asCCompiler::CompileBitwiseOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  8803. {
  8804. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  8805. int op = node->tokenType;
  8806. if( op == ttAmp || op == ttAndAssign ||
  8807. op == ttBitOr || op == ttOrAssign ||
  8808. op == ttBitXor || op == ttXorAssign )
  8809. {
  8810. // Convert left hand operand to integer if it's not already one
  8811. asCDataType to;
  8812. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 ||
  8813. rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8814. to.SetTokenType(ttUInt64);
  8815. else
  8816. to.SetTokenType(ttUInt);
  8817. // Do the actual conversion
  8818. int l = int(reservedVariables.GetLength());
  8819. rctx->bc.GetVarsUsed(reservedVariables);
  8820. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  8821. reservedVariables.SetLength(l);
  8822. // Verify that the conversion was successful
  8823. if( !lctx->type.dataType.IsUnsignedType() )
  8824. {
  8825. asCString str;
  8826. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  8827. Error(str.AddressOf(), node);
  8828. }
  8829. // Convert right hand operand to same type as left hand operand
  8830. l = int(reservedVariables.GetLength());
  8831. lctx->bc.GetVarsUsed(reservedVariables);
  8832. ImplicitConversion(rctx, lctx->type.dataType, node, asIC_IMPLICIT_CONV, true);
  8833. reservedVariables.SetLength(l);
  8834. if( !rctx->type.dataType.IsEqualExceptRef(lctx->type.dataType) )
  8835. {
  8836. asCString str;
  8837. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  8838. Error(str.AddressOf(), node);
  8839. }
  8840. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8841. if( !isConstant )
  8842. {
  8843. ConvertToVariableNotIn(lctx, rctx);
  8844. ConvertToVariableNotIn(rctx, lctx);
  8845. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8846. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8847. if( op == ttAndAssign || op == ttOrAssign || op == ttXorAssign )
  8848. {
  8849. // Compound assignments execute the right hand value first
  8850. MergeExprBytecode(ctx, rctx);
  8851. MergeExprBytecode(ctx, lctx);
  8852. }
  8853. else
  8854. {
  8855. MergeExprBytecode(ctx, lctx);
  8856. MergeExprBytecode(ctx, rctx);
  8857. }
  8858. ProcessDeferredParams(ctx);
  8859. asEBCInstr instruction = asBC_BAND;
  8860. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8861. {
  8862. if( op == ttAmp || op == ttAndAssign )
  8863. instruction = asBC_BAND;
  8864. else if( op == ttBitOr || op == ttOrAssign )
  8865. instruction = asBC_BOR;
  8866. else if( op == ttBitXor || op == ttXorAssign )
  8867. instruction = asBC_BXOR;
  8868. }
  8869. else
  8870. {
  8871. if( op == ttAmp || op == ttAndAssign )
  8872. instruction = asBC_BAND64;
  8873. else if( op == ttBitOr || op == ttOrAssign )
  8874. instruction = asBC_BOR64;
  8875. else if( op == ttBitXor || op == ttXorAssign )
  8876. instruction = asBC_BXOR64;
  8877. }
  8878. // Do the operation
  8879. int a = AllocateVariable(lctx->type.dataType, true);
  8880. int b = lctx->type.stackOffset;
  8881. int c = rctx->type.stackOffset;
  8882. ctx->bc.InstrW_W_W(instruction, a, b, c);
  8883. ctx->type.SetVariable(lctx->type.dataType, a, true);
  8884. }
  8885. else
  8886. {
  8887. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8888. {
  8889. asQWORD v = 0;
  8890. if( op == ttAmp )
  8891. v = lctx->type.qwordValue & rctx->type.qwordValue;
  8892. else if( op == ttBitOr )
  8893. v = lctx->type.qwordValue | rctx->type.qwordValue;
  8894. else if( op == ttBitXor )
  8895. v = lctx->type.qwordValue ^ rctx->type.qwordValue;
  8896. // Remember the result
  8897. ctx->type.SetConstantQW(lctx->type.dataType, v);
  8898. }
  8899. else
  8900. {
  8901. asDWORD v = 0;
  8902. if( op == ttAmp )
  8903. v = lctx->type.dwordValue & rctx->type.dwordValue;
  8904. else if( op == ttBitOr )
  8905. v = lctx->type.dwordValue | rctx->type.dwordValue;
  8906. else if( op == ttBitXor )
  8907. v = lctx->type.dwordValue ^ rctx->type.dwordValue;
  8908. // Remember the result
  8909. ctx->type.SetConstantDW(lctx->type.dataType, v);
  8910. }
  8911. }
  8912. }
  8913. else if( op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  8914. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  8915. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8916. {
  8917. // Don't permit object to primitive conversion, since we don't know which integer type is the correct one
  8918. if( lctx->type.dataType.IsObject() )
  8919. {
  8920. asCString str;
  8921. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  8922. Error(str.AddressOf(), node);
  8923. // Set an integer value and allow the compiler to continue
  8924. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  8925. return;
  8926. }
  8927. // Convert left hand operand to integer if it's not already one
  8928. asCDataType to = lctx->type.dataType;
  8929. if( lctx->type.dataType.IsUnsignedType() &&
  8930. lctx->type.dataType.GetSizeInMemoryBytes() < 4 )
  8931. {
  8932. to = asCDataType::CreatePrimitive(ttUInt, false);
  8933. }
  8934. else if( !lctx->type.dataType.IsUnsignedType() )
  8935. {
  8936. asCDataType to;
  8937. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8938. to.SetTokenType(ttInt64);
  8939. else
  8940. to.SetTokenType(ttInt);
  8941. }
  8942. // Do the actual conversion
  8943. int l = int(reservedVariables.GetLength());
  8944. rctx->bc.GetVarsUsed(reservedVariables);
  8945. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  8946. reservedVariables.SetLength(l);
  8947. // Verify that the conversion was successful
  8948. if( lctx->type.dataType != to )
  8949. {
  8950. asCString str;
  8951. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  8952. Error(str.AddressOf(), node);
  8953. }
  8954. // Right operand must be 32bit uint
  8955. l = int(reservedVariables.GetLength());
  8956. lctx->bc.GetVarsUsed(reservedVariables);
  8957. ImplicitConversion(rctx, asCDataType::CreatePrimitive(ttUInt, true), node, asIC_IMPLICIT_CONV, true);
  8958. reservedVariables.SetLength(l);
  8959. if( !rctx->type.dataType.IsUnsignedType() )
  8960. {
  8961. asCString str;
  8962. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), "uint");
  8963. Error(str.AddressOf(), node);
  8964. }
  8965. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  8966. if( !isConstant )
  8967. {
  8968. ConvertToVariableNotIn(lctx, rctx);
  8969. ConvertToVariableNotIn(rctx, lctx);
  8970. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  8971. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  8972. if( op == ttShiftLeftAssign || op == ttShiftRightLAssign || op == ttShiftRightAAssign )
  8973. {
  8974. // Compound assignments execute the right hand value first
  8975. MergeExprBytecode(ctx, rctx);
  8976. MergeExprBytecode(ctx, lctx);
  8977. }
  8978. else
  8979. {
  8980. MergeExprBytecode(ctx, lctx);
  8981. MergeExprBytecode(ctx, rctx);
  8982. }
  8983. ProcessDeferredParams(ctx);
  8984. asEBCInstr instruction = asBC_BSLL;
  8985. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8986. {
  8987. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  8988. instruction = asBC_BSLL;
  8989. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  8990. instruction = asBC_BSRL;
  8991. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  8992. instruction = asBC_BSRA;
  8993. }
  8994. else
  8995. {
  8996. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  8997. instruction = asBC_BSLL64;
  8998. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  8999. instruction = asBC_BSRL64;
  9000. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  9001. instruction = asBC_BSRA64;
  9002. }
  9003. // Do the operation
  9004. int a = AllocateVariable(lctx->type.dataType, true);
  9005. int b = lctx->type.stackOffset;
  9006. int c = rctx->type.stackOffset;
  9007. ctx->bc.InstrW_W_W(instruction, a, b, c);
  9008. ctx->type.SetVariable(lctx->type.dataType, a, true);
  9009. }
  9010. else
  9011. {
  9012. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9013. {
  9014. asDWORD v = 0;
  9015. if( op == ttBitShiftLeft )
  9016. v = lctx->type.dwordValue << rctx->type.dwordValue;
  9017. else if( op == ttBitShiftRight )
  9018. v = lctx->type.dwordValue >> rctx->type.dwordValue;
  9019. else if( op == ttBitShiftRightArith )
  9020. v = lctx->type.intValue >> rctx->type.dwordValue;
  9021. ctx->type.SetConstantDW(lctx->type.dataType, v);
  9022. }
  9023. else
  9024. {
  9025. asQWORD v = 0;
  9026. if( op == ttBitShiftLeft )
  9027. v = lctx->type.qwordValue << rctx->type.dwordValue;
  9028. else if( op == ttBitShiftRight )
  9029. v = lctx->type.qwordValue >> rctx->type.dwordValue;
  9030. else if( op == ttBitShiftRightArith )
  9031. v = asINT64(lctx->type.qwordValue) >> rctx->type.dwordValue;
  9032. ctx->type.SetConstantQW(lctx->type.dataType, v);
  9033. }
  9034. }
  9035. }
  9036. }
  9037. void asCCompiler::CompileComparisonOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9038. {
  9039. // Both operands must be of the same type
  9040. // Implicitly convert the operands to a number type
  9041. asCDataType to;
  9042. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  9043. to.SetTokenType(ttDouble);
  9044. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  9045. to.SetTokenType(ttFloat);
  9046. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9047. {
  9048. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() )
  9049. to.SetTokenType(ttInt64);
  9050. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  9051. to.SetTokenType(ttUInt64);
  9052. }
  9053. else
  9054. {
  9055. if( lctx->type.dataType.IsIntegerType() || rctx->type.dataType.IsIntegerType() ||
  9056. lctx->type.dataType.IsEnumType() || rctx->type.dataType.IsEnumType() )
  9057. to.SetTokenType(ttInt);
  9058. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  9059. to.SetTokenType(ttUInt);
  9060. else if( lctx->type.dataType.IsBooleanType() || rctx->type.dataType.IsBooleanType() )
  9061. to.SetTokenType(ttBool);
  9062. }
  9063. // If doing an operation with double constant and float variable, the constant should be converted to float
  9064. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  9065. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  9066. to.SetTokenType(ttFloat);
  9067. // Is it an operation on signed values?
  9068. bool signMismatch = false;
  9069. if( !lctx->type.dataType.IsUnsignedType() || !rctx->type.dataType.IsUnsignedType() )
  9070. {
  9071. if( lctx->type.dataType.GetTokenType() == ttUInt64 )
  9072. {
  9073. if( !lctx->type.isConstant )
  9074. signMismatch = true;
  9075. else if( lctx->type.qwordValue & (asQWORD(1)<<63) )
  9076. signMismatch = true;
  9077. }
  9078. if( lctx->type.dataType.GetTokenType() == ttUInt )
  9079. {
  9080. if( !lctx->type.isConstant )
  9081. signMismatch = true;
  9082. else if( lctx->type.dwordValue & (asDWORD(1)<<31) )
  9083. signMismatch = true;
  9084. }
  9085. if( rctx->type.dataType.GetTokenType() == ttUInt64 )
  9086. {
  9087. if( !rctx->type.isConstant )
  9088. signMismatch = true;
  9089. else if( rctx->type.qwordValue & (asQWORD(1)<<63) )
  9090. signMismatch = true;
  9091. }
  9092. if( rctx->type.dataType.GetTokenType() == ttUInt )
  9093. {
  9094. if( !rctx->type.isConstant )
  9095. signMismatch = true;
  9096. else if( rctx->type.dwordValue & (asDWORD(1)<<31) )
  9097. signMismatch = true;
  9098. }
  9099. }
  9100. // Check for signed/unsigned mismatch
  9101. if( signMismatch )
  9102. Warning(TXT_SIGNED_UNSIGNED_MISMATCH, node);
  9103. // Do the actual conversion
  9104. int l = int(reservedVariables.GetLength());
  9105. rctx->bc.GetVarsUsed(reservedVariables);
  9106. if( lctx->type.dataType.IsReference() )
  9107. ConvertToVariable(lctx);
  9108. if( rctx->type.dataType.IsReference() )
  9109. ConvertToVariable(rctx);
  9110. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  9111. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  9112. reservedVariables.SetLength(l);
  9113. // Verify that the conversion was successful
  9114. bool ok = true;
  9115. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  9116. {
  9117. asCString str;
  9118. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  9119. Error(str.AddressOf(), node);
  9120. ok = false;
  9121. }
  9122. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  9123. {
  9124. asCString str;
  9125. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  9126. Error(str.AddressOf(), node);
  9127. ok = false;
  9128. }
  9129. if( !ok )
  9130. {
  9131. // It wasn't possible to get two valid operands, so we just return
  9132. // a boolean result and let the compiler continue.
  9133. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  9134. return;
  9135. }
  9136. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  9137. int op = node->tokenType;
  9138. if( !isConstant )
  9139. {
  9140. if( to.IsBooleanType() )
  9141. {
  9142. int op = node->tokenType;
  9143. if( op == ttEqual || op == ttNotEqual )
  9144. {
  9145. // Must convert to temporary variable, because we are changing the value before comparison
  9146. ConvertToTempVariableNotIn(lctx, rctx);
  9147. ConvertToTempVariableNotIn(rctx, lctx);
  9148. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9149. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9150. // Make sure they are equal if not false
  9151. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  9152. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  9153. MergeExprBytecode(ctx, lctx);
  9154. MergeExprBytecode(ctx, rctx);
  9155. ProcessDeferredParams(ctx);
  9156. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  9157. int b = lctx->type.stackOffset;
  9158. int c = rctx->type.stackOffset;
  9159. if( op == ttEqual )
  9160. {
  9161. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  9162. ctx->bc.Instr(asBC_TZ);
  9163. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  9164. }
  9165. else if( op == ttNotEqual )
  9166. {
  9167. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  9168. ctx->bc.Instr(asBC_TNZ);
  9169. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  9170. }
  9171. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  9172. }
  9173. else
  9174. {
  9175. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  9176. Error(TXT_ILLEGAL_OPERATION, node);
  9177. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 0);
  9178. }
  9179. }
  9180. else
  9181. {
  9182. ConvertToVariableNotIn(lctx, rctx);
  9183. ConvertToVariableNotIn(rctx, lctx);
  9184. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9185. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9186. MergeExprBytecode(ctx, lctx);
  9187. MergeExprBytecode(ctx, rctx);
  9188. ProcessDeferredParams(ctx);
  9189. asEBCInstr iCmp = asBC_CMPi, iT = asBC_TZ;
  9190. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9191. iCmp = asBC_CMPi;
  9192. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9193. iCmp = asBC_CMPu;
  9194. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9195. iCmp = asBC_CMPi64;
  9196. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9197. iCmp = asBC_CMPu64;
  9198. else if( lctx->type.dataType.IsFloatType() )
  9199. iCmp = asBC_CMPf;
  9200. else if( lctx->type.dataType.IsDoubleType() )
  9201. iCmp = asBC_CMPd;
  9202. else
  9203. asASSERT(false);
  9204. if( op == ttEqual )
  9205. iT = asBC_TZ;
  9206. else if( op == ttNotEqual )
  9207. iT = asBC_TNZ;
  9208. else if( op == ttLessThan )
  9209. iT = asBC_TS;
  9210. else if( op == ttLessThanOrEqual )
  9211. iT = asBC_TNP;
  9212. else if( op == ttGreaterThan )
  9213. iT = asBC_TP;
  9214. else if( op == ttGreaterThanOrEqual )
  9215. iT = asBC_TNS;
  9216. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  9217. int b = lctx->type.stackOffset;
  9218. int c = rctx->type.stackOffset;
  9219. ctx->bc.InstrW_W(iCmp, b, c);
  9220. ctx->bc.Instr(iT);
  9221. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  9222. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  9223. }
  9224. }
  9225. else
  9226. {
  9227. if( to.IsBooleanType() )
  9228. {
  9229. int op = node->tokenType;
  9230. if( op == ttEqual || op == ttNotEqual )
  9231. {
  9232. // Make sure they are equal if not false
  9233. if( lctx->type.dwordValue != 0 ) lctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  9234. if( rctx->type.dwordValue != 0 ) rctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  9235. asDWORD v = 0;
  9236. if( op == ttEqual )
  9237. {
  9238. v = lctx->type.intValue - rctx->type.intValue;
  9239. if( v == 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  9240. }
  9241. else if( op == ttNotEqual )
  9242. {
  9243. v = lctx->type.intValue - rctx->type.intValue;
  9244. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  9245. }
  9246. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), v);
  9247. }
  9248. else
  9249. {
  9250. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  9251. Error(TXT_ILLEGAL_OPERATION, node);
  9252. }
  9253. }
  9254. else
  9255. {
  9256. int i = 0;
  9257. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9258. {
  9259. int v = lctx->type.intValue - rctx->type.intValue;
  9260. if( v < 0 ) i = -1;
  9261. if( v > 0 ) i = 1;
  9262. }
  9263. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9264. {
  9265. asDWORD v1 = lctx->type.dwordValue;
  9266. asDWORD v2 = rctx->type.dwordValue;
  9267. if( v1 < v2 ) i = -1;
  9268. if( v1 > v2 ) i = 1;
  9269. }
  9270. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9271. {
  9272. asINT64 v = asINT64(lctx->type.qwordValue) - asINT64(rctx->type.qwordValue);
  9273. if( v < 0 ) i = -1;
  9274. if( v > 0 ) i = 1;
  9275. }
  9276. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9277. {
  9278. asQWORD v1 = lctx->type.qwordValue;
  9279. asQWORD v2 = rctx->type.qwordValue;
  9280. if( v1 < v2 ) i = -1;
  9281. if( v1 > v2 ) i = 1;
  9282. }
  9283. else if( lctx->type.dataType.IsFloatType() )
  9284. {
  9285. float v = lctx->type.floatValue - rctx->type.floatValue;
  9286. if( v < 0 ) i = -1;
  9287. if( v > 0 ) i = 1;
  9288. }
  9289. else if( lctx->type.dataType.IsDoubleType() )
  9290. {
  9291. double v = lctx->type.doubleValue - rctx->type.doubleValue;
  9292. if( v < 0 ) i = -1;
  9293. if( v > 0 ) i = 1;
  9294. }
  9295. if( op == ttEqual )
  9296. i = (i == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9297. else if( op == ttNotEqual )
  9298. i = (i != 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9299. else if( op == ttLessThan )
  9300. i = (i < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9301. else if( op == ttLessThanOrEqual )
  9302. i = (i <= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9303. else if( op == ttGreaterThan )
  9304. i = (i > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9305. else if( op == ttGreaterThanOrEqual )
  9306. i = (i >= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9307. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), i);
  9308. }
  9309. }
  9310. }
  9311. void asCCompiler::PushVariableOnStack(asSExprContext *ctx, bool asReference)
  9312. {
  9313. // Put the result on the stack
  9314. if( asReference )
  9315. {
  9316. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  9317. ctx->type.dataType.MakeReference(true);
  9318. }
  9319. else
  9320. {
  9321. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9322. ctx->bc.InstrSHORT(asBC_PshV4, ctx->type.stackOffset);
  9323. else
  9324. ctx->bc.InstrSHORT(asBC_PshV8, ctx->type.stackOffset);
  9325. }
  9326. }
  9327. void asCCompiler::CompileBooleanOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9328. {
  9329. // Both operands must be booleans
  9330. asCDataType to;
  9331. to.SetTokenType(ttBool);
  9332. // Do the actual conversion
  9333. int l = int(reservedVariables.GetLength());
  9334. rctx->bc.GetVarsUsed(reservedVariables);
  9335. lctx->bc.GetVarsUsed(reservedVariables);
  9336. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  9337. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  9338. reservedVariables.SetLength(l);
  9339. // Verify that the conversion was successful
  9340. if( !lctx->type.dataType.IsBooleanType() )
  9341. {
  9342. asCString str;
  9343. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), "bool");
  9344. Error(str.AddressOf(), node);
  9345. // Force the conversion to allow compilation to proceed
  9346. lctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  9347. }
  9348. if( !rctx->type.dataType.IsBooleanType() )
  9349. {
  9350. asCString str;
  9351. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), "bool");
  9352. Error(str.AddressOf(), node);
  9353. // Force the conversion to allow compilation to proceed
  9354. rctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  9355. }
  9356. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  9357. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  9358. // What kind of operator is it?
  9359. int op = node->tokenType;
  9360. if( op == ttXor )
  9361. {
  9362. if( !isConstant )
  9363. {
  9364. // Must convert to temporary variable, because we are changing the value before comparison
  9365. ConvertToTempVariableNotIn(lctx, rctx);
  9366. ConvertToTempVariableNotIn(rctx, lctx);
  9367. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9368. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9369. // Make sure they are equal if not false
  9370. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  9371. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  9372. MergeExprBytecode(ctx, lctx);
  9373. MergeExprBytecode(ctx, rctx);
  9374. ProcessDeferredParams(ctx);
  9375. int a = AllocateVariable(ctx->type.dataType, true);
  9376. int b = lctx->type.stackOffset;
  9377. int c = rctx->type.stackOffset;
  9378. ctx->bc.InstrW_W_W(asBC_BXOR,a,b,c);
  9379. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  9380. }
  9381. else
  9382. {
  9383. // Make sure they are equal if not false
  9384. #if AS_SIZEOF_BOOL == 1
  9385. if( lctx->type.byteValue != 0 ) lctx->type.byteValue = VALUE_OF_BOOLEAN_TRUE;
  9386. if( rctx->type.byteValue != 0 ) rctx->type.byteValue = VALUE_OF_BOOLEAN_TRUE;
  9387. asBYTE v = 0;
  9388. v = lctx->type.byteValue - rctx->type.byteValue;
  9389. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  9390. ctx->type.isConstant = true;
  9391. ctx->type.byteValue = v;
  9392. #else
  9393. if( lctx->type.dwordValue != 0 ) lctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  9394. if( rctx->type.dwordValue != 0 ) rctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  9395. asDWORD v = 0;
  9396. v = lctx->type.intValue - rctx->type.intValue;
  9397. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  9398. ctx->type.isConstant = true;
  9399. ctx->type.dwordValue = v;
  9400. #endif
  9401. }
  9402. }
  9403. else if( op == ttAnd ||
  9404. op == ttOr )
  9405. {
  9406. if( !isConstant )
  9407. {
  9408. // If or-operator and first value is 1 the second value shouldn't be calculated
  9409. // if and-operator and first value is 0 the second value shouldn't be calculated
  9410. ConvertToVariable(lctx);
  9411. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9412. MergeExprBytecode(ctx, lctx);
  9413. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  9414. int label1 = nextLabel++;
  9415. int label2 = nextLabel++;
  9416. if( op == ttAnd )
  9417. {
  9418. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  9419. ctx->bc.Instr(asBC_ClrHi);
  9420. ctx->bc.InstrDWORD(asBC_JNZ, label1);
  9421. ctx->bc.InstrW_DW(asBC_SetV4, (asWORD)offset, 0);
  9422. ctx->bc.InstrINT(asBC_JMP, label2);
  9423. }
  9424. else if( op == ttOr )
  9425. {
  9426. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  9427. ctx->bc.Instr(asBC_ClrHi);
  9428. ctx->bc.InstrDWORD(asBC_JZ, label1);
  9429. #if AS_SIZEOF_BOOL == 1
  9430. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  9431. #else
  9432. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  9433. #endif
  9434. ctx->bc.InstrINT(asBC_JMP, label2);
  9435. }
  9436. ctx->bc.Label((short)label1);
  9437. ConvertToVariable(rctx);
  9438. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9439. rctx->bc.InstrW_W(asBC_CpyVtoV4, offset, rctx->type.stackOffset);
  9440. MergeExprBytecode(ctx, rctx);
  9441. ctx->bc.Label((short)label2);
  9442. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), offset, true);
  9443. }
  9444. else
  9445. {
  9446. #if AS_SIZEOF_BOOL == 1
  9447. asBYTE v = 0;
  9448. if( op == ttAnd )
  9449. v = lctx->type.byteValue && rctx->type.byteValue;
  9450. else if( op == ttOr )
  9451. v = lctx->type.byteValue || rctx->type.byteValue;
  9452. // Remember the result
  9453. ctx->type.isConstant = true;
  9454. ctx->type.byteValue = v;
  9455. #else
  9456. asDWORD v = 0;
  9457. if( op == ttAnd )
  9458. v = lctx->type.dwordValue && rctx->type.dwordValue;
  9459. else if( op == ttOr )
  9460. v = lctx->type.dwordValue || rctx->type.dwordValue;
  9461. // Remember the result
  9462. ctx->type.isConstant = true;
  9463. ctx->type.dwordValue = v;
  9464. #endif
  9465. }
  9466. }
  9467. }
  9468. void asCCompiler::CompileOperatorOnHandles(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9469. {
  9470. // Process the property accessor as get
  9471. ProcessPropertyGetAccessor(lctx, node);
  9472. ProcessPropertyGetAccessor(rctx, node);
  9473. // Make sure lctx doesn't end up with a variable used in rctx
  9474. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  9475. {
  9476. asCArray<int> vars;
  9477. rctx->bc.GetVarsUsed(vars);
  9478. int offset = AllocateVariable(lctx->type.dataType, true);
  9479. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  9480. ReleaseTemporaryVariable(offset, 0);
  9481. }
  9482. // Warn if not both operands are explicit handles
  9483. if( (node->tokenType == ttEqual || node->tokenType == ttNotEqual) &&
  9484. ((!lctx->type.isExplicitHandle && !(lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE))) ||
  9485. (!rctx->type.isExplicitHandle && !(rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE)))) )
  9486. {
  9487. Warning(TXT_HANDLE_COMPARISON, node);
  9488. }
  9489. // If one of the operands is a value type used as handle, we should look for the opEquals method
  9490. if( ((lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) ||
  9491. (rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE))) &&
  9492. (node->tokenType == ttEqual || node->tokenType == ttIs ||
  9493. node->tokenType == ttNotEqual || node->tokenType == ttNotIs) )
  9494. {
  9495. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  9496. // Find the matching opEquals method
  9497. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  9498. if( r == 0 )
  9499. {
  9500. // Try again by switching the order of the operands
  9501. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  9502. }
  9503. if( r == 1 )
  9504. {
  9505. if( node->tokenType == ttNotEqual || node->tokenType == ttNotIs )
  9506. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  9507. // Success, don't continue
  9508. return;
  9509. }
  9510. else if( r == 0 )
  9511. {
  9512. // Couldn't find opEquals method
  9513. Error(TXT_NO_APPROPRIATE_OPEQUALS, node);
  9514. }
  9515. // Compiler error, don't continue
  9516. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  9517. return;
  9518. }
  9519. // Implicitly convert null to the other type
  9520. asCDataType to;
  9521. if( lctx->type.IsNullConstant() )
  9522. to = rctx->type.dataType;
  9523. else if( rctx->type.IsNullConstant() )
  9524. to = lctx->type.dataType;
  9525. else
  9526. {
  9527. // TODO: Use the common base type
  9528. to = lctx->type.dataType;
  9529. }
  9530. // Need to pop the value if it is a null constant
  9531. if( lctx->type.IsNullConstant() )
  9532. lctx->bc.Instr(asBC_PopPtr);
  9533. if( rctx->type.IsNullConstant() )
  9534. rctx->bc.Instr(asBC_PopPtr);
  9535. // Convert both sides to explicit handles
  9536. to.MakeHandle(true);
  9537. to.MakeReference(false);
  9538. if( !to.IsObjectHandle() )
  9539. {
  9540. // Compiler error, don't continue
  9541. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  9542. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  9543. return;
  9544. }
  9545. // Do the conversion
  9546. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  9547. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  9548. // Both operands must be of the same type
  9549. // Verify that the conversion was successful
  9550. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  9551. {
  9552. asCString str;
  9553. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  9554. Error(str.AddressOf(), node);
  9555. }
  9556. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  9557. {
  9558. asCString str;
  9559. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  9560. Error(str.AddressOf(), node);
  9561. }
  9562. // Make sure it really is handles that are being compared
  9563. if( !lctx->type.dataType.IsObjectHandle() )
  9564. {
  9565. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  9566. }
  9567. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  9568. int op = node->tokenType;
  9569. if( op == ttEqual || op == ttNotEqual || op == ttIs || op == ttNotIs )
  9570. {
  9571. // If the object handle already is in a variable we must manually pop it from the stack
  9572. if( lctx->type.isVariable )
  9573. lctx->bc.Instr(asBC_PopPtr);
  9574. if( rctx->type.isVariable )
  9575. rctx->bc.Instr(asBC_PopPtr);
  9576. // TODO: runtime optimize: don't do REFCPY
  9577. ConvertToVariableNotIn(lctx, rctx);
  9578. ConvertToVariable(rctx);
  9579. MergeExprBytecode(ctx, lctx);
  9580. MergeExprBytecode(ctx, rctx);
  9581. int a = AllocateVariable(ctx->type.dataType, true);
  9582. int b = lctx->type.stackOffset;
  9583. int c = rctx->type.stackOffset;
  9584. ctx->bc.InstrW_W(asBC_CmpPtr, b, c);
  9585. if( op == ttEqual || op == ttIs )
  9586. ctx->bc.Instr(asBC_TZ);
  9587. else if( op == ttNotEqual || op == ttNotIs )
  9588. ctx->bc.Instr(asBC_TNZ);
  9589. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  9590. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  9591. ReleaseTemporaryVariable(lctx->type, &ctx->bc);
  9592. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  9593. ProcessDeferredParams(ctx);
  9594. }
  9595. else
  9596. {
  9597. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  9598. Error(TXT_ILLEGAL_OPERATION, node);
  9599. }
  9600. }
  9601. void asCCompiler::PerformFunctionCall(int funcId, asSExprContext *ctx, bool isConstructor, asCArray<asSExprContext*> *args, asCObjectType *objType, bool useVariable, int varOffset, int funcPtrVar)
  9602. {
  9603. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  9604. // A shared object may not call non-shared functions
  9605. if( outFunc->IsShared() && !descr->IsShared() )
  9606. {
  9607. asCString msg;
  9608. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, descr->GetDeclarationStr().AddressOf());
  9609. Error(msg.AddressOf(), ctx->exprNode);
  9610. }
  9611. // Check if the function is private
  9612. if( descr->isPrivate && descr->GetObjectType() != outFunc->GetObjectType() )
  9613. {
  9614. asCString msg;
  9615. msg.Format(TXT_PRIVATE_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  9616. Error(msg.AddressOf(), ctx->exprNode);
  9617. }
  9618. int argSize = descr->GetSpaceNeededForArguments();
  9619. if( descr->objectType && descr->returnType.IsReference() &&
  9620. !ctx->type.isVariable && (ctx->type.dataType.IsObjectHandle() || ctx->type.dataType.SupportHandles()) &&
  9621. !(ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_SCOPED) &&
  9622. !(ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_ASHANDLE) )
  9623. {
  9624. // The class method we're calling is returning a reference, which may be to a member of the object.
  9625. // In order to guarantee the lifetime of the reference, we must hold a local reference to the object.
  9626. // TODO: runtime optimize: This can be avoided for local variables (non-handles) as they have a well defined life time
  9627. int tempRef = AllocateVariable(ctx->type.dataType, true);
  9628. ctx->bc.InstrSHORT(asBC_PSF, (short)tempRef);
  9629. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  9630. // Add the release of this reference, as a deferred expression
  9631. asSDeferredParam deferred;
  9632. deferred.origExpr = 0;
  9633. deferred.argInOutFlags = asTM_INREF;
  9634. deferred.argNode = 0;
  9635. deferred.argType.SetVariable(ctx->type.dataType, tempRef, true);
  9636. ctx->deferredParams.PushLast(deferred);
  9637. // Forget the current type
  9638. ctx->type.SetDummy();
  9639. }
  9640. if( isConstructor )
  9641. {
  9642. // Sometimes the value types are allocated on the heap,
  9643. // which is when this way of constructing them is used.
  9644. asASSERT(useVariable == false);
  9645. ctx->bc.Alloc(asBC_ALLOC, objType, descr->id, argSize+AS_PTR_SIZE);
  9646. // The instruction has already moved the returned object to the variable
  9647. ctx->type.Set(asCDataType::CreatePrimitive(ttVoid, false));
  9648. ctx->type.isLValue = false;
  9649. // Clean up arguments
  9650. if( args )
  9651. AfterFunctionCall(funcId, *args, ctx, false);
  9652. ProcessDeferredParams(ctx);
  9653. return;
  9654. }
  9655. else
  9656. {
  9657. if( descr->objectType )
  9658. argSize += AS_PTR_SIZE;
  9659. // If the function returns an object by value the address of the location
  9660. // where the value should be stored is passed as an argument too
  9661. if( descr->DoesReturnOnStack() )
  9662. {
  9663. argSize += AS_PTR_SIZE;
  9664. }
  9665. // TODO: runtime optimize: If it is known that a class method cannot be overridden the call
  9666. // should be made with asBC_CALL as it is faster. Examples where this
  9667. // is known is for example finalled methods where the class doesn't derive
  9668. // from any other, or even non-finalled methods but where it is known
  9669. // at compile time the true type of the object. The first should be
  9670. // quite easy to determine, but the latter will be quite complex and possibly
  9671. // not worth it.
  9672. if( descr->funcType == asFUNC_IMPORTED )
  9673. ctx->bc.Call(asBC_CALLBND , descr->id, argSize);
  9674. // TODO: Maybe we need two different byte codes
  9675. else if( descr->funcType == asFUNC_INTERFACE || descr->funcType == asFUNC_VIRTUAL )
  9676. ctx->bc.Call(asBC_CALLINTF, descr->id, argSize);
  9677. else if( descr->funcType == asFUNC_SCRIPT )
  9678. ctx->bc.Call(asBC_CALL , descr->id, argSize);
  9679. else if( descr->funcType == asFUNC_SYSTEM )
  9680. ctx->bc.Call(asBC_CALLSYS , descr->id, argSize);
  9681. else if( descr->funcType == asFUNC_FUNCDEF )
  9682. ctx->bc.CallPtr(asBC_CallPtr, funcPtrVar, argSize);
  9683. }
  9684. if( descr->returnType.IsObject() && !descr->returnType.IsReference() )
  9685. {
  9686. int returnOffset = 0;
  9687. if( descr->DoesReturnOnStack() )
  9688. {
  9689. asASSERT( useVariable );
  9690. // The variable was allocated before the function was called
  9691. returnOffset = varOffset;
  9692. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  9693. // The variable was initialized by the function, so we need to mark it as initialized here
  9694. ctx->bc.ObjInfo(varOffset, asOBJ_INIT);
  9695. }
  9696. else
  9697. {
  9698. if( useVariable )
  9699. {
  9700. // Use the given variable
  9701. returnOffset = varOffset;
  9702. ctx->type.SetVariable(descr->returnType, returnOffset, false);
  9703. }
  9704. else
  9705. {
  9706. // Allocate a temporary variable for the returned object
  9707. // The returned object will actually be allocated on the heap, so
  9708. // we must force the allocation of the variable to do the same
  9709. returnOffset = AllocateVariable(descr->returnType, true, true);
  9710. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  9711. }
  9712. // Move the pointer from the object register to the temporary variable
  9713. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  9714. }
  9715. ctx->type.dataType.MakeReference(IsVariableOnHeap(returnOffset));
  9716. ctx->type.isLValue = false; // It is a reference, but not an lvalue
  9717. // Clean up arguments
  9718. if( args )
  9719. AfterFunctionCall(funcId, *args, ctx, false);
  9720. ProcessDeferredParams(ctx);
  9721. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  9722. }
  9723. else if( descr->returnType.IsReference() )
  9724. {
  9725. asASSERT(useVariable == false);
  9726. // We cannot clean up the arguments yet, because the
  9727. // reference might be pointing to one of them.
  9728. if( args )
  9729. AfterFunctionCall(funcId, *args, ctx, true);
  9730. // Do not process the output parameters yet, because it
  9731. // might invalidate the returned reference
  9732. // If the context holds a variable that needs cleanup
  9733. // store it as a deferred parameter so it will be cleaned up
  9734. // afterwards.
  9735. if( ctx->type.isTemporary )
  9736. {
  9737. asSDeferredParam defer;
  9738. defer.argNode = 0;
  9739. defer.argType = ctx->type;
  9740. defer.argInOutFlags = asTM_INOUTREF;
  9741. defer.origExpr = 0;
  9742. ctx->deferredParams.PushLast(defer);
  9743. }
  9744. ctx->type.Set(descr->returnType);
  9745. if( !descr->returnType.IsPrimitive() )
  9746. {
  9747. ctx->bc.Instr(asBC_PshRPtr);
  9748. if( descr->returnType.IsObject() &&
  9749. !descr->returnType.IsObjectHandle() )
  9750. {
  9751. // We are getting the pointer to the object
  9752. // not a pointer to a object variable
  9753. ctx->type.dataType.MakeReference(false);
  9754. }
  9755. }
  9756. // A returned reference can be used as lvalue
  9757. ctx->type.isLValue = true;
  9758. }
  9759. else
  9760. {
  9761. asASSERT(useVariable == false);
  9762. if( descr->returnType.GetSizeInMemoryBytes() )
  9763. {
  9764. // Allocate a temporary variable to hold the value, but make sure
  9765. // the temporary variable isn't used in any of the deferred arguments
  9766. int l = int(reservedVariables.GetLength());
  9767. for( asUINT n = 0; args && n < args->GetLength(); n++ )
  9768. {
  9769. asSExprContext *expr = (*args)[n]->origExpr;
  9770. if( expr )
  9771. expr->bc.GetVarsUsed(reservedVariables);
  9772. }
  9773. int offset = AllocateVariable(descr->returnType, true);
  9774. reservedVariables.SetLength(l);
  9775. ctx->type.SetVariable(descr->returnType, offset, true);
  9776. // Move the value from the return register to the variable
  9777. if( descr->returnType.GetSizeOnStackDWords() == 1 )
  9778. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)offset);
  9779. else if( descr->returnType.GetSizeOnStackDWords() == 2 )
  9780. ctx->bc.InstrSHORT(asBC_CpyRtoV8, (short)offset);
  9781. }
  9782. else
  9783. ctx->type.Set(descr->returnType);
  9784. ctx->type.isLValue = false;
  9785. // Clean up arguments
  9786. if( args )
  9787. AfterFunctionCall(funcId, *args, ctx, false);
  9788. ProcessDeferredParams(ctx);
  9789. }
  9790. }
  9791. // This only merges the bytecode, but doesn't modify the type of the final context
  9792. void asCCompiler::MergeExprBytecode(asSExprContext *before, asSExprContext *after)
  9793. {
  9794. before->bc.AddCode(&after->bc);
  9795. for( asUINT n = 0; n < after->deferredParams.GetLength(); n++ )
  9796. {
  9797. before->deferredParams.PushLast(after->deferredParams[n]);
  9798. after->deferredParams[n].origExpr = 0;
  9799. }
  9800. after->deferredParams.SetLength(0);
  9801. }
  9802. // This merges both bytecode and the type of the final context
  9803. void asCCompiler::MergeExprBytecodeAndType(asSExprContext *before, asSExprContext *after)
  9804. {
  9805. MergeExprBytecode(before, after);
  9806. before->type = after->type;
  9807. before->property_get = after->property_get;
  9808. before->property_set = after->property_set;
  9809. before->property_const = after->property_const;
  9810. before->property_handle = after->property_handle;
  9811. before->property_ref = after->property_ref;
  9812. before->property_arg = after->property_arg;
  9813. before->exprNode = after->exprNode;
  9814. after->property_arg = 0;
  9815. // Do not copy the origExpr member
  9816. }
  9817. void asCCompiler::FilterConst(asCArray<int> &funcs, bool removeConst)
  9818. {
  9819. if( funcs.GetLength() == 0 ) return;
  9820. // This is only done for object methods
  9821. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[0]);
  9822. if( desc->objectType == 0 ) return;
  9823. // Check if there are any non-const matches
  9824. asUINT n;
  9825. bool foundNonConst = false;
  9826. for( n = 0; n < funcs.GetLength(); n++ )
  9827. {
  9828. desc = builder->GetFunctionDescription(funcs[n]);
  9829. if( desc->isReadOnly != removeConst )
  9830. {
  9831. foundNonConst = true;
  9832. break;
  9833. }
  9834. }
  9835. if( foundNonConst )
  9836. {
  9837. // Remove all const methods
  9838. for( n = 0; n < funcs.GetLength(); n++ )
  9839. {
  9840. desc = builder->GetFunctionDescription(funcs[n]);
  9841. if( desc->isReadOnly == removeConst )
  9842. {
  9843. if( n == funcs.GetLength() - 1 )
  9844. funcs.PopLast();
  9845. else
  9846. funcs[n] = funcs.PopLast();
  9847. n--;
  9848. }
  9849. }
  9850. }
  9851. }
  9852. END_AS_NAMESPACE
  9853. #endif // AS_NO_COMPILER