as_compiler.cpp 488 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2017 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 Oorni for Urho3D
  24. //
  25. // as_compiler.cpp
  26. //
  27. // The class that does the actual compilation of the functions
  28. //
  29. #include <math.h> // fmodf() pow()
  30. #include "as_config.h"
  31. #ifndef AS_NO_COMPILER
  32. #include "as_compiler.h"
  33. #include "as_tokendef.h"
  34. #include "as_tokenizer.h"
  35. #include "as_string_util.h"
  36. #include "as_texts.h"
  37. #include "as_parser.h"
  38. #include "as_debug.h"
  39. #include "as_context.h" // as_powi()
  40. BEGIN_AS_NAMESPACE
  41. //
  42. // The calling convention rules for script functions:
  43. // - If a class method returns a reference, the caller must guarantee the object pointer stays alive until the function returns, and the reference is no longer going to be used
  44. // - If a class method doesn't return a reference, it must guarantee by itself that the this pointer stays alive during the function call. If no outside access is made, then the function is guaranteed to stay alive and nothing needs to be done
  45. // - The object pointer is always passed as the first argument, position 0
  46. // - If the function returns a value type the caller must reserve the memory for this and pass the pointer as the first argument after the object pointer
  47. //
  48. // TODO: I must correct the interpretation of a reference to objects in the compiler.
  49. // A reference should mean that a pointer to the object is on the stack.
  50. // No expression should end up as non-references to objects, as the actual object is
  51. // never put on the stack.
  52. // Local variables are declared as non-references, but the expression should be a reference to the variable.
  53. // Function parameters of called functions can also be non-references, but in that case it means the
  54. // object will be passed by value (currently on the heap, which will be moved to the application stack).
  55. //
  56. // The compiler shouldn't use the asCDataType::IsReference. The datatype should always be stored as non-references.
  57. // Instead the compiler should keep track of references in TypeInfo, where it should also state how the reference
  58. // is currently stored, i.e. in variable, in register, on stack, etc.
  59. asCCompiler::asCCompiler(asCScriptEngine *engine) : byteCode(engine)
  60. {
  61. builder = 0;
  62. script = 0;
  63. variables = 0;
  64. isProcessingDeferredParams = false;
  65. isCompilingDefaultArg = false;
  66. noCodeOutput = 0;
  67. }
  68. asCCompiler::~asCCompiler()
  69. {
  70. while( variables )
  71. {
  72. asCVariableScope *var = variables;
  73. variables = variables->parent;
  74. asDELETE(var,asCVariableScope);
  75. }
  76. }
  77. void asCCompiler::Reset(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptFunction *in_outFunc)
  78. {
  79. this->builder = in_builder;
  80. this->engine = in_builder->engine;
  81. this->script = in_script;
  82. this->outFunc = in_outFunc;
  83. hasCompileErrors = false;
  84. m_isConstructor = false;
  85. m_isConstructorCalled = false;
  86. m_classDecl = 0;
  87. m_globalVar = 0;
  88. nextLabel = 0;
  89. breakLabels.SetLength(0);
  90. continueLabels.SetLength(0);
  91. numLambdas = 0;
  92. byteCode.ClearAll();
  93. }
  94. int asCCompiler::CompileDefaultConstructor(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptNode *in_node, asCScriptFunction *in_outFunc, sClassDeclaration *in_classDecl)
  95. {
  96. Reset(in_builder, in_script, in_outFunc);
  97. m_classDecl = in_classDecl;
  98. // Insert a JitEntry at the start of the function for JIT compilers
  99. byteCode.InstrPTR(asBC_JitEntry, 0);
  100. // Add a variable scope that might be needed to declare dummy variables
  101. // in case the member initialization refers to undefined symbols.
  102. AddVariableScope();
  103. // Initialize the class members that have no explicit expression first. This will allow the
  104. // base class' constructor to access these members without worry they will be uninitialized.
  105. // This can happen if the base class' constructor calls a method that is overridden by the derived class
  106. CompileMemberInitialization(&byteCode, true);
  107. // If the class is derived from another, then the base class' default constructor must be called
  108. if( outFunc->objectType->derivedFrom )
  109. {
  110. // Make sure the base class really has a default constructor
  111. if( outFunc->objectType->derivedFrom->beh.construct == 0 )
  112. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, in_node);
  113. // Call the base class' default constructor
  114. byteCode.InstrSHORT(asBC_PSF, 0);
  115. byteCode.Instr(asBC_RDSPtr);
  116. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  117. }
  118. // Initialize the class members that explicit expressions afterwards. This allow the expressions
  119. // to access the base class members without worry they will be uninitialized
  120. CompileMemberInitialization(&byteCode, false);
  121. byteCode.OptimizeLocally(tempVariableOffsets);
  122. // If there are compile errors, there is no reason to build the final code
  123. if( hasCompileErrors )
  124. return -1;
  125. // Pop the object pointer from the stack
  126. byteCode.Ret(AS_PTR_SIZE);
  127. // Count total variable size
  128. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  129. outFunc->scriptData->variableSpace = varSize;
  130. FinalizeFunction();
  131. #ifdef AS_DEBUG
  132. // DEBUG: output byte code
  133. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + "__defconstr.txt").AddressOf(), in_outFunc);
  134. #endif
  135. return 0;
  136. }
  137. int asCCompiler::CompileFactory(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptFunction *in_outFunc)
  138. {
  139. Reset(in_builder, in_script, in_outFunc);
  140. // Insert a JitEntry at the start of the function for JIT compilers
  141. byteCode.InstrPTR(asBC_JitEntry, 0);
  142. // Find the corresponding constructor
  143. asCDataType dt = asCDataType::CreateType(outFunc->returnType.GetTypeInfo(), false);
  144. int constructor = 0;
  145. for( unsigned int n = 0; n < dt.GetBehaviour()->factories.GetLength(); n++ )
  146. {
  147. if( dt.GetBehaviour()->factories[n] == outFunc->id )
  148. {
  149. constructor = dt.GetBehaviour()->constructors[n];
  150. break;
  151. }
  152. }
  153. // Allocate the class and instantiate it with the constructor
  154. int varOffset = AllocateVariable(dt, true);
  155. outFunc->scriptData->variableSpace = AS_PTR_SIZE;
  156. byteCode.InstrSHORT(asBC_PSF, (short)varOffset);
  157. // Copy all arguments to the top of the stack
  158. // TODO: runtime optimize: Might be interesting to have a specific instruction for copying all arguments
  159. int offset = (int)outFunc->GetSpaceNeededForArguments();
  160. for( int a = int(outFunc->parameterTypes.GetLength()) - 1; a >= 0; a-- )
  161. {
  162. if( !outFunc->parameterTypes[a].IsPrimitive() ||
  163. outFunc->parameterTypes[a].IsReference() )
  164. {
  165. offset -= AS_PTR_SIZE;
  166. byteCode.InstrSHORT(asBC_PshVPtr, short(-offset));
  167. }
  168. else
  169. {
  170. if( outFunc->parameterTypes[a].GetSizeOnStackDWords() == 2 )
  171. {
  172. offset -= 2;
  173. byteCode.InstrSHORT(asBC_PshV8, short(-offset));
  174. }
  175. else
  176. {
  177. offset -= 1;
  178. byteCode.InstrSHORT(asBC_PshV4, short(-offset));
  179. }
  180. }
  181. }
  182. int argDwords = (int)outFunc->GetSpaceNeededForArguments();
  183. byteCode.Alloc(asBC_ALLOC, dt.GetTypeInfo(), constructor, argDwords + AS_PTR_SIZE);
  184. // Return a handle to the newly created object
  185. byteCode.InstrSHORT(asBC_LOADOBJ, (short)varOffset);
  186. byteCode.Ret(argDwords);
  187. FinalizeFunction();
  188. // Tell the virtual machine not to clean up parameters on exception
  189. outFunc->dontCleanUpOnException = true;
  190. /*
  191. #ifdef AS_DEBUG
  192. // DEBUG: output byte code
  193. asCString args;
  194. args.Format("%d", outFunc->parameterTypes.GetLength());
  195. byteCode.DebugOutput(("__" + outFunc->name + "__factory" + args + ".txt").AddressOf(), engine);
  196. #endif
  197. */
  198. return 0;
  199. }
  200. void asCCompiler::FinalizeFunction()
  201. {
  202. TimeIt("asCCompiler::FinalizeFunction");
  203. asASSERT( outFunc->scriptData );
  204. asUINT n;
  205. // Finalize the bytecode
  206. byteCode.Finalize(tempVariableOffsets);
  207. byteCode.ExtractObjectVariableInfo(outFunc);
  208. // Compile the list of object variables for the exception handler
  209. // Start with the variables allocated on the heap, and then the ones allocated on the stack
  210. for( n = 0; n < variableAllocations.GetLength(); n++ )
  211. {
  212. if( (variableAllocations[n].IsObject() || variableAllocations[n].IsFuncdef()) && !variableAllocations[n].IsReference() )
  213. {
  214. if( variableIsOnHeap[n] )
  215. {
  216. outFunc->scriptData->objVariableTypes.PushLast(variableAllocations[n].GetTypeInfo());
  217. outFunc->scriptData->objVariablePos.PushLast(GetVariableOffset(n));
  218. }
  219. }
  220. }
  221. outFunc->scriptData->objVariablesOnHeap = asUINT(outFunc->scriptData->objVariablePos.GetLength());
  222. for( n = 0; n < variableAllocations.GetLength(); n++ )
  223. {
  224. if( (variableAllocations[n].IsObject() || variableAllocations[n].IsFuncdef()) && !variableAllocations[n].IsReference() )
  225. {
  226. if( !variableIsOnHeap[n] )
  227. {
  228. outFunc->scriptData->objVariableTypes.PushLast(variableAllocations[n].GetTypeInfo());
  229. outFunc->scriptData->objVariablePos.PushLast(GetVariableOffset(n));
  230. }
  231. }
  232. }
  233. // Copy byte code to the function
  234. asASSERT( outFunc->scriptData->byteCode.GetLength() == 0 );
  235. outFunc->scriptData->byteCode.SetLength(byteCode.GetSize());
  236. byteCode.Output(outFunc->scriptData->byteCode.AddressOf());
  237. outFunc->AddReferences();
  238. outFunc->scriptData->stackNeeded = byteCode.largestStackUsed + outFunc->scriptData->variableSpace;
  239. outFunc->scriptData->lineNumbers = byteCode.lineNumbers;
  240. // Extract the script section indexes too if there are any entries that are different from the function's script section
  241. int lastIdx = outFunc->scriptData->scriptSectionIdx;
  242. for( n = 0; n < byteCode.sectionIdxs.GetLength(); n++ )
  243. {
  244. if( byteCode.sectionIdxs[n] != lastIdx )
  245. {
  246. lastIdx = byteCode.sectionIdxs[n];
  247. outFunc->scriptData->sectionIdxs.PushLast(byteCode.lineNumbers[n*2]);
  248. outFunc->scriptData->sectionIdxs.PushLast(lastIdx);
  249. }
  250. }
  251. }
  252. // internal
  253. int asCCompiler::SetupParametersAndReturnVariable(asCArray<asCString> &parameterNames, asCScriptNode *func)
  254. {
  255. int stackPos = 0;
  256. if( outFunc->objectType )
  257. stackPos = -AS_PTR_SIZE; // The first parameter is the pointer to the object
  258. // Add the first variable scope, which the parameters and
  259. // variables declared in the outermost statement block is
  260. // part of.
  261. AddVariableScope();
  262. bool isDestructor = false;
  263. asCDataType returnType;
  264. // Examine return type
  265. returnType = outFunc->returnType;
  266. // Check if this is a constructor or destructor
  267. if( returnType.GetTokenType() == ttVoid && outFunc->objectType )
  268. {
  269. if( outFunc->name[0] == '~' )
  270. isDestructor = true;
  271. else if( outFunc->objectType->name == outFunc->name )
  272. m_isConstructor = true;
  273. }
  274. // Is the return type allowed?
  275. if( returnType != asCDataType::CreatePrimitive(ttVoid, false) &&
  276. !returnType.CanBeInstantiated() )
  277. {
  278. // TODO: Hasn't this been validated by the builder already?
  279. asCString str;
  280. str.Format(TXT_RETURN_CANT_BE_s, returnType.Format(outFunc->nameSpace).AddressOf());
  281. Error(str, func);
  282. }
  283. // If the return type is a value type returned by value the address of the
  284. // location where the value will be stored is pushed on the stack before
  285. // the arguments
  286. if( !(isDestructor || m_isConstructor) && outFunc->DoesReturnOnStack() )
  287. stackPos -= AS_PTR_SIZE;
  288. asCVariableScope vs(0);
  289. // Declare parameters
  290. asUINT n;
  291. for( n = 0; n < parameterNames.GetLength(); n++ )
  292. {
  293. // Get the parameter type
  294. asCDataType &type = outFunc->parameterTypes[n];
  295. asETypeModifiers inoutFlag = n < outFunc->inOutFlags.GetLength() ? outFunc->inOutFlags[n] : asTM_NONE;
  296. // Is the data type allowed?
  297. // TODO: Hasn't this been validated by the builder already?
  298. if( (type.IsReference() && inoutFlag != asTM_INOUTREF && !type.CanBeInstantiated()) ||
  299. (!type.IsReference() && !type.CanBeInstantiated()) )
  300. {
  301. asCString parm = type.Format(outFunc->nameSpace);
  302. if( inoutFlag == asTM_INREF )
  303. parm += "in";
  304. else if( inoutFlag == asTM_OUTREF )
  305. parm += "out";
  306. asCString str;
  307. str.Format(TXT_PARAMETER_CANT_BE_s, parm.AddressOf());
  308. Error(str, func);
  309. }
  310. // If the parameter has a name then declare it as variable
  311. if( parameterNames[n] != "" )
  312. {
  313. asCString &name = parameterNames[n];
  314. if( vs.DeclareVariable(name.AddressOf(), type, stackPos, true) < 0 )
  315. {
  316. // TODO: It might be an out-of-memory too
  317. Error(TXT_PARAMETER_ALREADY_DECLARED, func);
  318. }
  319. // Add marker for variable declaration
  320. byteCode.VarDecl((int)outFunc->scriptData->variables.GetLength());
  321. outFunc->AddVariable(name, type, stackPos);
  322. }
  323. else
  324. vs.DeclareVariable("", type, stackPos, true);
  325. // Move to next parameter
  326. stackPos -= type.GetSizeOnStackDWords();
  327. }
  328. for( n = asUINT(vs.variables.GetLength()); n-- > 0; )
  329. variables->DeclareVariable(vs.variables[n]->name.AddressOf(), vs.variables[n]->type, vs.variables[n]->stackOffset, vs.variables[n]->onHeap);
  330. variables->DeclareVariable("return", returnType, stackPos, true);
  331. return stackPos;
  332. }
  333. void asCCompiler::CompileMemberInitialization(asCByteCode *bc, bool onlyDefaults)
  334. {
  335. asASSERT( m_classDecl );
  336. // Initialize each member in the order they were declared
  337. for( asUINT n = 0; n < outFunc->objectType->properties.GetLength(); n++ )
  338. {
  339. asCObjectProperty *prop = outFunc->objectType->properties[n];
  340. // Check if the property has an initialization expression
  341. asCScriptNode *declNode = 0;
  342. asCScriptNode *initNode = 0;
  343. asCScriptCode *initScript = 0;
  344. for( asUINT m = 0; m < m_classDecl->propInits.GetLength(); m++ )
  345. {
  346. if( m_classDecl->propInits[m].name == prop->name )
  347. {
  348. declNode = m_classDecl->propInits[m].declNode;
  349. initNode = m_classDecl->propInits[m].initNode;
  350. initScript = m_classDecl->propInits[m].file;
  351. break;
  352. }
  353. }
  354. // If declNode is null, the property was inherited in which case
  355. // it was already initialized by the base class' constructor
  356. if( declNode )
  357. {
  358. if( initNode )
  359. {
  360. if( onlyDefaults )
  361. continue;
  362. #ifdef AS_NO_MEMBER_INIT
  363. // Give an error as the initialization in the declaration has been disabled
  364. asCScriptCode *origScript = script;
  365. script = initScript;
  366. Error("Initialization of members in declaration is not supported", initNode);
  367. script = origScript;
  368. // Clear the initialization node
  369. initNode = 0;
  370. initScript = script;
  371. #else
  372. // Re-parse the initialization expression as the parser now knows the types, which it didn't earlier
  373. asCParser parser(builder);
  374. int r = parser.ParseVarInit(initScript, initNode);
  375. if( r < 0 )
  376. continue;
  377. initNode = parser.GetScriptNode();
  378. #endif
  379. }
  380. else
  381. {
  382. if( !onlyDefaults )
  383. continue;
  384. }
  385. #ifdef AS_NO_MEMBER_INIT
  386. // The initialization will be done in the asCScriptObject constructor, so
  387. // here we should just validate that the member has a default constructor
  388. if( prop->type.IsObject() &&
  389. !prop->type.IsObjectHandle() &&
  390. (((prop->type.GetTypeInfo()->flags & asOBJ_REF) &&
  391. prop->type.GetBehaviour()->factory == 0) ||
  392. ((prop->type.GetTypeInfo()->flags & asOBJ_VALUE) &&
  393. prop->type.GetBehaviour()->construct == 0 &&
  394. !(prop->type.GetTypeInfo()->flags & asOBJ_POD))) )
  395. {
  396. // Class has no default factory/constructor.
  397. asCString str;
  398. // TODO: funcdef: asCDataType should have a GetTypeName()
  399. if( prop->type.GetFuncDef() )
  400. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, prop->type.GetFuncDef()->GetName());
  401. else
  402. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, prop->type.GetTypeInfo()->GetName());
  403. Error(str, declNode);
  404. }
  405. #else
  406. // Temporarily set the script that is being compiled to where the member initialization is declared.
  407. // The script can be different when including mixin classes from a different script section
  408. asCScriptCode *origScript = script;
  409. script = initScript;
  410. // Add a line instruction with the position of the declaration
  411. LineInstr(bc, declNode->tokenPos);
  412. // Compile the initialization
  413. asQWORD constantValue;
  414. asCByteCode bcInit(engine);
  415. CompileInitialization(initNode, &bcInit, prop->type, declNode, prop->byteOffset, &constantValue, 2);
  416. bcInit.OptimizeLocally(tempVariableOffsets);
  417. bc->AddCode(&bcInit);
  418. script = origScript;
  419. #endif
  420. }
  421. }
  422. }
  423. // Entry
  424. int asCCompiler::CompileFunction(asCBuilder *in_builder, asCScriptCode *in_script, asCArray<asCString> &in_parameterNames, asCScriptNode *in_func, asCScriptFunction *in_outFunc, sClassDeclaration *in_classDecl)
  425. {
  426. TimeIt("asCCompiler::CompileFunction");
  427. Reset(in_builder, in_script, in_outFunc);
  428. int buildErrors = builder->numErrors;
  429. int stackPos = SetupParametersAndReturnVariable(in_parameterNames, in_func);
  430. //--------------------------------------------
  431. // Compile the statement block
  432. if( m_isConstructor )
  433. m_classDecl = in_classDecl;
  434. // We need to parse the statement block now
  435. asCScriptNode *blockBegin;
  436. // If the function signature was implicit, e.g. virtual property accessor or
  437. // lambda function, then the received node already is the statement block
  438. if( in_func->nodeType != snStatementBlock )
  439. blockBegin = in_func->lastChild;
  440. else
  441. blockBegin = in_func;
  442. // TODO: memory: We can parse the statement block one statement at a time, thus save even more memory
  443. // TODO: optimize: For large functions, the parsing of the statement block can take a long time. Presumably because a lot of memory needs to be allocated
  444. asCParser parser(builder);
  445. int r = parser.ParseStatementBlock(script, blockBegin);
  446. if( r < 0 ) return -1;
  447. asCScriptNode *block = parser.GetScriptNode();
  448. // Reserve a label for the cleanup code
  449. nextLabel++;
  450. bool hasReturn;
  451. asCByteCode bc(engine);
  452. LineInstr(&bc, blockBegin->tokenPos);
  453. CompileStatementBlock(block, false, &hasReturn, &bc);
  454. LineInstr(&bc, blockBegin->tokenPos + blockBegin->tokenLength);
  455. // Make sure there is a return in all paths (if not return type is void)
  456. // Don't bother with this check if there are compiler errors, e.g. Unreachable code
  457. if( !hasCompileErrors && outFunc->returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  458. {
  459. if( hasReturn == false )
  460. Error(TXT_NOT_ALL_PATHS_RETURN, blockBegin);
  461. }
  462. //------------------------------------------------
  463. // Concatenate the bytecode
  464. // Insert a JitEntry at the start of the function for JIT compilers
  465. byteCode.InstrPTR(asBC_JitEntry, 0);
  466. if( outFunc->objectType )
  467. {
  468. if( m_isConstructor )
  469. {
  470. if( outFunc->objectType->derivedFrom )
  471. {
  472. // Call the base class' default constructor unless called manually in the code
  473. if( !m_isConstructorCalled )
  474. {
  475. if( outFunc->objectType->derivedFrom->beh.construct )
  476. {
  477. // Initialize members without explicit expression first
  478. CompileMemberInitialization(&byteCode, true);
  479. // Call base class' constructor
  480. asCByteCode tmpBC(engine);
  481. tmpBC.InstrSHORT(asBC_PSF, 0);
  482. tmpBC.Instr(asBC_RDSPtr);
  483. tmpBC.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  484. tmpBC.OptimizeLocally(tempVariableOffsets);
  485. byteCode.AddCode(&tmpBC);
  486. // Add the initialization of the members with explicit expressions
  487. CompileMemberInitialization(&byteCode, false);
  488. }
  489. else
  490. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, blockBegin);
  491. }
  492. else
  493. {
  494. // Only initialize members that don't have an explicit expression
  495. // The members that are explicitly initialized will be initialized after the call to base class' constructor
  496. CompileMemberInitialization(&byteCode, true);
  497. }
  498. }
  499. else
  500. {
  501. // Add the initialization of the members
  502. CompileMemberInitialization(&byteCode, true);
  503. CompileMemberInitialization(&byteCode, false);
  504. }
  505. }
  506. }
  507. // Add the code for the statement block
  508. byteCode.AddCode(&bc);
  509. // Count total variable size
  510. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  511. outFunc->scriptData->variableSpace = varSize;
  512. // Deallocate all local variables
  513. int n;
  514. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  515. {
  516. sVariable *v = variables->variables[n];
  517. if( v->stackOffset > 0 )
  518. {
  519. // Call variables destructors
  520. if( v->name != "return" && v->name != "return address" )
  521. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  522. DeallocateVariable(v->stackOffset);
  523. }
  524. }
  525. // This is the label that return statements jump to
  526. // in order to exit the function
  527. byteCode.Label(0);
  528. // Call destructors for function parameters
  529. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  530. {
  531. sVariable *v = variables->variables[n];
  532. if( v->stackOffset <= 0 )
  533. {
  534. // Call variable destructors here, for variables not yet destroyed
  535. if( v->name != "return" && v->name != "return address" )
  536. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  537. }
  538. // Do not deallocate parameters
  539. }
  540. // Check if the number of labels in the functions isn't too many to be handled
  541. if( nextLabel >= (1<<15) )
  542. Error(TXT_TOO_MANY_JUMP_LABELS, in_func);
  543. // If there are compile errors, there is no reason to build the final code
  544. if( hasCompileErrors || builder->numErrors != buildErrors )
  545. return -1;
  546. // At this point there should be no variables allocated
  547. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  548. // Remove the variable scope
  549. RemoveVariableScope();
  550. byteCode.Ret(-stackPos);
  551. FinalizeFunction();
  552. #ifdef AS_DEBUG
  553. // DEBUG: output byte code
  554. if( outFunc->objectType )
  555. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + ".txt").AddressOf(), in_outFunc);
  556. else
  557. byteCode.DebugOutput(("__" + outFunc->name + ".txt").AddressOf(), in_outFunc);
  558. #endif
  559. return 0;
  560. }
  561. int asCCompiler::CallCopyConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCExprContext *arg, asCScriptNode *node, bool isGlobalVar, bool derefDest)
  562. {
  563. if( !type.IsObject() )
  564. return 0;
  565. // CallCopyConstructor should not be called for object handles.
  566. asASSERT( !type.IsObjectHandle() );
  567. asCArray<asCExprContext*> args;
  568. args.PushLast(arg);
  569. // The reference parameter must be pushed on the stack
  570. asASSERT( arg->type.dataType.GetTypeInfo() == type.GetTypeInfo() );
  571. // Since we're calling the copy constructor, we have to trust the function to not do
  572. // anything stupid otherwise we will just enter a loop, as we try to make temporary
  573. // copies of the argument in order to guarantee safety.
  574. if( type.GetTypeInfo()->flags & asOBJ_REF )
  575. {
  576. asCExprContext ctx(engine);
  577. int func = 0;
  578. asSTypeBehaviour *beh = type.GetBehaviour();
  579. if( beh ) func = beh->copyfactory;
  580. if( func > 0 )
  581. {
  582. if( !isGlobalVar )
  583. {
  584. // Call factory and store the handle in the given variable
  585. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()), true, offset);
  586. // Pop the reference left by the function call
  587. ctx.bc.Instr(asBC_PopPtr);
  588. }
  589. else
  590. {
  591. // Call factory
  592. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()));
  593. // Store the returned handle in the global variable
  594. ctx.bc.Instr(asBC_RDSPtr);
  595. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  596. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  597. ctx.bc.Instr(asBC_PopPtr);
  598. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  599. }
  600. bc->AddCode(&ctx.bc);
  601. return 0;
  602. }
  603. }
  604. else
  605. {
  606. asSTypeBehaviour *beh = type.GetBehaviour();
  607. int func = beh ? beh->copyconstruct : 0;
  608. if( func > 0 )
  609. {
  610. // Push the address where the object will be stored on the stack, before the argument
  611. // TODO: When the context is serializable this probably has to be changed, since this
  612. // pointer can remain on the stack while the context is suspended. There is no
  613. // risk the pointer becomes invalid though, there is just no easy way to serialize it.
  614. asCByteCode tmp(engine);
  615. if( isGlobalVar )
  616. tmp.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  617. else if( isObjectOnHeap )
  618. tmp.InstrSHORT(asBC_PSF, (short)offset);
  619. tmp.AddCode(bc);
  620. bc->AddCode(&tmp);
  621. // When the object is allocated on the stack the object pointer
  622. // must be pushed on the stack after the arguments
  623. if( !isObjectOnHeap )
  624. {
  625. asASSERT( !isGlobalVar );
  626. bc->InstrSHORT(asBC_PSF, (short)offset);
  627. if( derefDest )
  628. {
  629. // The variable is a reference to the real location, so we need to dereference it
  630. bc->Instr(asBC_RDSPtr);
  631. }
  632. }
  633. asCExprContext ctx(engine);
  634. PerformFunctionCall(func, &ctx, isObjectOnHeap, &args, CastToObjectType(type.GetTypeInfo()));
  635. bc->AddCode(&ctx.bc);
  636. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  637. // Mark the object as initialized
  638. if( !isObjectOnHeap )
  639. bc->ObjInfo(offset, asOBJ_INIT);
  640. return 0;
  641. }
  642. }
  643. // Class has no copy constructor/factory.
  644. asCString str;
  645. str.Format(TXT_NO_COPY_CONSTRUCTOR_FOR_s, type.GetTypeInfo()->GetName());
  646. Error(str, node);
  647. return -1;
  648. }
  649. int asCCompiler::CallDefaultConstructor(const asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCScriptNode *node, int isVarGlobOrMem, bool derefDest)
  650. {
  651. if( !type.IsObject() || type.IsObjectHandle() )
  652. return 0;
  653. if( type.GetTypeInfo()->flags & asOBJ_REF )
  654. {
  655. asCExprContext ctx(engine);
  656. ctx.exprNode = node;
  657. int func = 0;
  658. asSTypeBehaviour *beh = type.GetBehaviour();
  659. if( beh )
  660. {
  661. func = beh->factory;
  662. // If no trivial default factory is found, look for a factory where all params have default args
  663. if( func == 0 )
  664. {
  665. for( asUINT n = 0; n < beh->factories.GetLength(); n++ )
  666. {
  667. asCScriptFunction *f = engine->scriptFunctions[beh->factories[n]];
  668. if( f->defaultArgs.GetLength() == f->parameterTypes.GetLength() &&
  669. f->defaultArgs[0] != 0 )
  670. {
  671. func = beh->factories[n];
  672. break;
  673. }
  674. }
  675. }
  676. }
  677. if( func > 0 )
  678. {
  679. asCArray<asCExprContext *> args;
  680. asCScriptFunction *f = engine->scriptFunctions[func];
  681. if( f->parameterTypes.GetLength() )
  682. {
  683. // Add the default values for arguments not explicitly supplied
  684. CompileDefaultAndNamedArgs(node, args, func, CastToObjectType(type.GetTypeInfo()));
  685. PrepareFunctionCall(func, &ctx.bc, args);
  686. MoveArgsToStack(func, &ctx.bc, args, false);
  687. }
  688. if( isVarGlobOrMem == 0 )
  689. {
  690. // Call factory and store the handle in the given variable
  691. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()), true, offset);
  692. // Pop the reference left by the function call
  693. ctx.bc.Instr(asBC_PopPtr);
  694. }
  695. else
  696. {
  697. // Call factory
  698. PerformFunctionCall(func, &ctx, false, &args, CastToObjectType(type.GetTypeInfo()));
  699. // TODO: runtime optimize: Should have a way of storing the object pointer directly to the destination
  700. // instead of first storing it in a local variable and then copying it to the
  701. // destination.
  702. if( !(type.GetTypeInfo()->flags & asOBJ_SCOPED) )
  703. {
  704. // Only dereference the variable if not a scoped type
  705. ctx.bc.Instr(asBC_RDSPtr);
  706. }
  707. if( isVarGlobOrMem == 1 )
  708. {
  709. // Store the returned handle in the global variable
  710. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  711. }
  712. else
  713. {
  714. // Store the returned handle in the class member
  715. ctx.bc.InstrSHORT(asBC_PSF, 0);
  716. ctx.bc.Instr(asBC_RDSPtr);
  717. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  718. }
  719. if( type.GetTypeInfo()->flags & asOBJ_SCOPED )
  720. {
  721. // For scoped typed we must move the reference from the local
  722. // variable rather than copy it as there is no AddRef behaviour
  723. ctx.bc.InstrSHORT_DW(asBC_COPY, AS_PTR_SIZE, asTYPEID_OBJHANDLE | engine->GetTypeIdFromDataType(type));
  724. // Clear the local variable so the reference isn't released
  725. ctx.bc.InstrSHORT(asBC_ClrVPtr, ctx.type.stackOffset);
  726. }
  727. else
  728. {
  729. if( type.IsFuncdef() )
  730. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  731. else
  732. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  733. }
  734. ctx.bc.Instr(asBC_PopPtr);
  735. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  736. }
  737. bc->AddCode(&ctx.bc);
  738. // Cleanup
  739. for( asUINT n = 0; n < args.GetLength(); n++ )
  740. if( args[n] )
  741. {
  742. asDELETE(args[n], asCExprContext);
  743. }
  744. return 0;
  745. }
  746. }
  747. else
  748. {
  749. asCExprContext ctx(engine);
  750. ctx.exprNode = node;
  751. asSTypeBehaviour *beh = type.GetBehaviour();
  752. int func = 0;
  753. if( beh )
  754. {
  755. func = beh->construct;
  756. // If no trivial default constructor is found, look for a constructor where all params have default args
  757. if( func == 0 )
  758. {
  759. for( asUINT n = 0; n < beh->constructors.GetLength(); n++ )
  760. {
  761. asCScriptFunction *f = engine->scriptFunctions[beh->constructors[n]];
  762. if( f->defaultArgs.GetLength() == f->parameterTypes.GetLength() &&
  763. f->defaultArgs[0] != 0 )
  764. {
  765. func = beh->constructors[n];
  766. break;
  767. }
  768. }
  769. }
  770. }
  771. // Allocate and initialize with the default constructor
  772. if( func != 0 || (type.GetTypeInfo()->flags & asOBJ_POD) )
  773. {
  774. asCArray<asCExprContext *> args;
  775. asCScriptFunction *f = engine->scriptFunctions[func];
  776. if( f && f->parameterTypes.GetLength() )
  777. {
  778. // Add the default values for arguments not explicitly supplied
  779. CompileDefaultAndNamedArgs(node, args, func, CastToObjectType(type.GetTypeInfo()));
  780. PrepareFunctionCall(func, &ctx.bc, args);
  781. MoveArgsToStack(func, &ctx.bc, args, false);
  782. }
  783. if( !isObjectOnHeap )
  784. {
  785. if( isVarGlobOrMem == 0 )
  786. {
  787. // There is nothing to do if there is no function,
  788. // as the memory is already allocated on the stack
  789. if( func )
  790. {
  791. // Call the constructor as a normal function
  792. bc->InstrSHORT(asBC_PSF, (short)offset);
  793. if( derefDest )
  794. bc->Instr(asBC_RDSPtr);
  795. asCExprContext ctxCall(engine);
  796. PerformFunctionCall(func, &ctxCall, false, 0, CastToObjectType(type.GetTypeInfo()));
  797. bc->AddCode(&ctxCall.bc);
  798. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  799. // Mark the object as initialized
  800. bc->ObjInfo(offset, asOBJ_INIT);
  801. }
  802. }
  803. else if( isVarGlobOrMem == 2 )
  804. {
  805. // Only POD types can be allocated inline in script classes
  806. asASSERT( type.GetTypeInfo()->flags & asOBJ_POD );
  807. if( func )
  808. {
  809. // Call the constructor as a normal function
  810. bc->InstrSHORT(asBC_PSF, 0);
  811. bc->Instr(asBC_RDSPtr);
  812. bc->InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  813. asCExprContext ctxCall(engine);
  814. PerformFunctionCall(func, &ctxCall, false, 0, CastToObjectType(type.GetTypeInfo()));
  815. bc->AddCode(&ctxCall.bc);
  816. }
  817. }
  818. else
  819. {
  820. asASSERT( false );
  821. }
  822. }
  823. else
  824. {
  825. if( isVarGlobOrMem == 0 )
  826. bc->InstrSHORT(asBC_PSF, (short)offset);
  827. else if( isVarGlobOrMem == 1 )
  828. bc->InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  829. else
  830. {
  831. bc->InstrSHORT(asBC_PSF, 0);
  832. bc->Instr(asBC_RDSPtr);
  833. bc->InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  834. }
  835. if( (type.GetTypeInfo()->flags & asOBJ_TEMPLATE) )
  836. {
  837. asCScriptFunction *descr = engine->scriptFunctions[func];
  838. asASSERT( descr->funcType == asFUNC_SCRIPT );
  839. // Find the id of the real constructor and not the generated stub
  840. asUINT id = 0;
  841. asDWORD *funcBc = descr->scriptData->byteCode.AddressOf();
  842. while( funcBc )
  843. {
  844. if( (*(asBYTE*)funcBc) == asBC_CALLSYS )
  845. {
  846. id = asBC_INTARG(funcBc);
  847. break;
  848. }
  849. funcBc += asBCTypeSize[asBCInfo[*(asBYTE*)funcBc].type];
  850. }
  851. asASSERT( id );
  852. bc->InstrPTR(asBC_OBJTYPE, type.GetTypeInfo());
  853. bc->Alloc(asBC_ALLOC, type.GetTypeInfo(), id, AS_PTR_SIZE + AS_PTR_SIZE);
  854. }
  855. else
  856. bc->Alloc(asBC_ALLOC, type.GetTypeInfo(), func, AS_PTR_SIZE);
  857. }
  858. // Cleanup
  859. for( asUINT n = 0; n < args.GetLength(); n++ )
  860. if( args[n] )
  861. {
  862. asDELETE(args[n], asCExprContext);
  863. }
  864. return 0;
  865. }
  866. }
  867. // Class has no default factory/constructor.
  868. asCString str;
  869. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetTypeInfo()->GetName());
  870. Error(str, node);
  871. return -1;
  872. }
  873. void asCCompiler::CallDestructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc)
  874. {
  875. if( !type.IsReference() )
  876. {
  877. // Call destructor for the data type
  878. if( type.IsObject() || type.IsFuncdef() )
  879. {
  880. // The null pointer doesn't need to be destroyed
  881. if( type.IsNullHandle() )
  882. return;
  883. // Nothing is done for list pattern types, as this is taken care of by the CompileInitList method
  884. if( type.GetTypeInfo()->flags & asOBJ_LIST_PATTERN )
  885. return;
  886. if( isObjectOnHeap || type.IsObjectHandle() )
  887. {
  888. // Free the memory
  889. if (type.IsFuncdef())
  890. bc->InstrW_PTR(asBC_FREE, (short)offset, &engine->functionBehaviours);
  891. else
  892. bc->InstrW_PTR(asBC_FREE, (short)offset, type.GetTypeInfo());
  893. }
  894. else
  895. {
  896. asASSERT( type.GetTypeInfo()->GetFlags() & asOBJ_VALUE );
  897. if( type.GetBehaviour()->destruct )
  898. {
  899. // Call the destructor as a regular function
  900. asCExprContext ctx(engine);
  901. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  902. PerformFunctionCall(type.GetBehaviour()->destruct, &ctx);
  903. ctx.bc.OptimizeLocally(tempVariableOffsets);
  904. bc->AddCode(&ctx.bc);
  905. }
  906. // TODO: Value on stack: This probably needs to be done in PerformFunctionCall
  907. // Mark the object as destroyed
  908. bc->ObjInfo(offset, asOBJ_UNINIT);
  909. }
  910. }
  911. }
  912. }
  913. void asCCompiler::LineInstr(asCByteCode *bc, size_t pos)
  914. {
  915. int r, c;
  916. script->ConvertPosToRowCol(pos, &r, &c);
  917. bc->Line(r, c, script->idx);
  918. }
  919. void asCCompiler::CompileStatementBlock(asCScriptNode *block, bool ownVariableScope, bool *hasReturn, asCByteCode *bc)
  920. {
  921. *hasReturn = false;
  922. bool isFinished = false;
  923. bool hasUnreachableCode = false;
  924. bool hasReturnBefore = false;
  925. if( ownVariableScope )
  926. {
  927. bc->Block(true);
  928. AddVariableScope();
  929. }
  930. asCScriptNode *node = block->firstChild;
  931. while( node )
  932. {
  933. #ifdef AS_DEBUG
  934. // Keep the current line in a variable so it will be easier
  935. // to determine where in a script an assert is occurring.
  936. int currentLine = 0;
  937. script->ConvertPosToRowCol(node->tokenPos, &currentLine, 0);
  938. #endif
  939. if( !hasUnreachableCode && (*hasReturn || isFinished) )
  940. {
  941. // Empty statements don't count
  942. if( node->nodeType != snExpressionStatement || node->firstChild )
  943. {
  944. hasUnreachableCode = true;
  945. Warning(TXT_UNREACHABLE_CODE, node);
  946. }
  947. if( *hasReturn )
  948. hasReturnBefore = true;
  949. }
  950. if( node->nodeType == snBreak || node->nodeType == snContinue )
  951. isFinished = true;
  952. asCByteCode statement(engine);
  953. if( node->nodeType == snDeclaration )
  954. CompileDeclaration(node, &statement);
  955. else
  956. CompileStatement(node, hasReturn, &statement);
  957. // Ignore missing returns in unreachable code paths
  958. if( !(*hasReturn) && hasReturnBefore )
  959. *hasReturn = true;
  960. LineInstr(bc, node->tokenPos);
  961. bc->AddCode(&statement);
  962. if( !hasCompileErrors )
  963. {
  964. asASSERT( tempVariables.GetLength() == 0 );
  965. asASSERT( reservedVariables.GetLength() == 0 );
  966. }
  967. node = node->next;
  968. }
  969. if( ownVariableScope )
  970. {
  971. // Deallocate variables in this block, in reverse order
  972. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  973. {
  974. sVariable *v = variables->variables[n];
  975. // Call variable destructors here, for variables not yet destroyed
  976. // If the block is terminated with a break, continue, or
  977. // return the variables are already destroyed
  978. if( !isFinished && !*hasReturn )
  979. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  980. // Don't deallocate function parameters
  981. if( v->stackOffset > 0 )
  982. DeallocateVariable(v->stackOffset);
  983. }
  984. RemoveVariableScope();
  985. bc->Block(false);
  986. }
  987. }
  988. // Entry
  989. int asCCompiler::CompileGlobalVariable(asCBuilder *in_builder, asCScriptCode *in_script, asCScriptNode *in_node, sGlobalVariableDescription *in_gvar, asCScriptFunction *in_outFunc)
  990. {
  991. Reset(in_builder, in_script, in_outFunc);
  992. m_globalVar = in_gvar;
  993. // Add a variable scope (even though variables can't be declared)
  994. AddVariableScope();
  995. in_gvar->isPureConstant = false;
  996. // Parse the initialization nodes
  997. asCParser parser(builder);
  998. if (in_node)
  999. {
  1000. int r = parser.ParseVarInit(in_script, in_node);
  1001. if (r < 0)
  1002. return r;
  1003. in_node = parser.GetScriptNode();
  1004. }
  1005. asCExprContext compiledCtx(engine);
  1006. bool preCompiled = false;
  1007. if (in_gvar->datatype.IsAuto())
  1008. {
  1009. preCompiled = CompileAutoType(in_gvar->datatype, compiledCtx, in_node, in_gvar->declaredAtNode);
  1010. if (!preCompiled)
  1011. {
  1012. // If it wasn't possible to determine the type from the expression then there
  1013. // is no need to continue with the initialization. The error was already reported
  1014. // in CompileAutoType.
  1015. return -1;
  1016. }
  1017. }
  1018. if( in_gvar->property == 0 )
  1019. {
  1020. in_gvar->property = builder->module->AllocateGlobalProperty(in_gvar->name.AddressOf(), in_gvar->datatype, in_gvar->ns);
  1021. in_gvar->index = in_gvar->property->id;
  1022. }
  1023. // Compile the expression
  1024. asCExprContext ctx(engine);
  1025. asQWORD constantValue = 0;
  1026. if( CompileInitialization(in_node, &ctx.bc, in_gvar->datatype, in_gvar->declaredAtNode, in_gvar->index, &constantValue, 1, preCompiled ? &compiledCtx : 0) )
  1027. {
  1028. // Should the variable be marked as pure constant?
  1029. if( in_gvar->datatype.IsPrimitive() && in_gvar->datatype.IsReadOnly() )
  1030. {
  1031. in_gvar->isPureConstant = true;
  1032. in_gvar->constantValue = constantValue;
  1033. }
  1034. }
  1035. // Concatenate the bytecode
  1036. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  1037. // Add information on the line number for the global variable
  1038. size_t pos = 0;
  1039. if( in_gvar->declaredAtNode )
  1040. pos = in_gvar->declaredAtNode->tokenPos;
  1041. else if( in_gvar->initializationNode )
  1042. pos = in_gvar->initializationNode->tokenPos;
  1043. LineInstr(&byteCode, pos);
  1044. // Reserve space for all local variables
  1045. outFunc->scriptData->variableSpace = varSize;
  1046. ctx.bc.OptimizeLocally(tempVariableOffsets);
  1047. byteCode.AddCode(&ctx.bc);
  1048. // Deallocate variables in this block, in reverse order
  1049. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; --n )
  1050. {
  1051. sVariable *v = variables->variables[n];
  1052. // Call variable destructors here, for variables not yet destroyed
  1053. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  1054. DeallocateVariable(v->stackOffset);
  1055. }
  1056. if( hasCompileErrors ) return -1;
  1057. // At this point there should be no variables allocated
  1058. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  1059. // Remove the variable scope again
  1060. RemoveVariableScope();
  1061. byteCode.Ret(0);
  1062. FinalizeFunction();
  1063. #ifdef AS_DEBUG
  1064. // DEBUG: output byte code
  1065. byteCode.DebugOutput(("___init_" + in_gvar->name + ".txt").AddressOf(), outFunc);
  1066. #endif
  1067. return 0;
  1068. }
  1069. void asCCompiler::DetermineSingleFunc(asCExprContext *ctx, asCScriptNode *node)
  1070. {
  1071. // Don't do anything if this is not a deferred global function
  1072. if( !ctx->IsGlobalFunc() )
  1073. return;
  1074. // Determine the namespace
  1075. asSNameSpace *ns = 0;
  1076. asCString name = "";
  1077. int pos = ctx->methodName.FindLast("::");
  1078. if( pos >= 0 )
  1079. {
  1080. asCString nsName = ctx->methodName.SubString(0, pos+2);
  1081. // Cut off the ::
  1082. if( nsName.GetLength() > 2 )
  1083. nsName.SetLength(nsName.GetLength()-2);
  1084. ns = DetermineNameSpace(nsName);
  1085. name = ctx->methodName.SubString(pos+2);
  1086. }
  1087. else
  1088. {
  1089. DetermineNameSpace("");
  1090. name = ctx->methodName;
  1091. }
  1092. asCArray<int> funcs;
  1093. if( ns )
  1094. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  1095. // CompileVariableAccess should guarantee that at least one function is exists
  1096. asASSERT( funcs.GetLength() > 0 );
  1097. if( funcs.GetLength() > 1 )
  1098. {
  1099. asCString str;
  1100. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, ctx->methodName.AddressOf());
  1101. Error(str, node);
  1102. // Fall through so the compiler can continue as if only one function was matching
  1103. }
  1104. // A shared object may not access global functions unless they too are shared (e.g. registered functions)
  1105. if( !builder->GetFunctionDescription(funcs[0])->IsShared() &&
  1106. outFunc->IsShared() )
  1107. {
  1108. asCString msg;
  1109. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, builder->GetFunctionDescription(funcs[0])->GetDeclaration());
  1110. Error(msg, node);
  1111. // Fall through so the compiler can continue anyway
  1112. }
  1113. // Push the function pointer on the stack
  1114. ctx->bc.InstrPTR(asBC_FuncPtr, builder->GetFunctionDescription(funcs[0]));
  1115. ctx->type.Set(asCDataType::CreateType(engine->FindMatchingFuncdef(builder->GetFunctionDescription(funcs[0]), builder->module), false));
  1116. ctx->type.dataType.MakeHandle(true);
  1117. ctx->type.isExplicitHandle = true;
  1118. ctx->methodName = "";
  1119. }
  1120. void asCCompiler::CompileInitAsCopy(asCDataType &dt, int offset, asCByteCode *bc, asCExprContext *arg, asCScriptNode *node, bool derefDestination)
  1121. {
  1122. bool isObjectOnHeap = derefDestination ? false : IsVariableOnHeap(offset);
  1123. // Use copy constructor if available.
  1124. if(CastToObjectType(dt.GetTypeInfo()) && CastToObjectType(dt.GetTypeInfo())->beh.copyconstruct )
  1125. {
  1126. PrepareForAssignment(&dt, arg, node, true);
  1127. int r = CallCopyConstructor(dt, offset, isObjectOnHeap, bc, arg, node, 0, derefDestination);
  1128. if( r < 0 && tempVariables.Exists(offset) )
  1129. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  1130. }
  1131. else
  1132. {
  1133. // TODO: Need to reserve variables, as the default constructor may need
  1134. // to allocate temporary variables to compute default args
  1135. // Allocate and construct the temporary object before whatever is already in the bytecode
  1136. asCByteCode tmpBC(engine);
  1137. int r = CallDefaultConstructor(dt, offset, isObjectOnHeap, &tmpBC, node, 0, derefDestination);
  1138. if( r < 0 )
  1139. {
  1140. if( tempVariables.Exists(offset) )
  1141. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  1142. return;
  1143. }
  1144. tmpBC.AddCode(bc);
  1145. bc->AddCode(&tmpBC);
  1146. // Assign the evaluated expression to the temporary variable
  1147. PrepareForAssignment(&dt, arg, node, true);
  1148. bc->AddCode(&arg->bc);
  1149. // Call the opAssign method to assign the value to the temporary object
  1150. dt.MakeReference(isObjectOnHeap);
  1151. asCExprValue type;
  1152. type.Set(dt);
  1153. type.isTemporary = true;
  1154. type.stackOffset = (short)offset;
  1155. if( dt.IsObjectHandle() )
  1156. type.isExplicitHandle = true;
  1157. bc->InstrSHORT(asBC_PSF, (short)offset);
  1158. if( derefDestination )
  1159. bc->Instr(asBC_RDSPtr);
  1160. r = PerformAssignment(&type, &arg->type, bc, node);
  1161. if( r < 0 )
  1162. {
  1163. if( tempVariables.Exists(offset) )
  1164. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  1165. return;
  1166. }
  1167. // Pop the reference that was pushed on the stack if the result is an object
  1168. if( type.dataType.IsObject() || type.dataType.IsFuncdef() )
  1169. bc->Instr(asBC_PopPtr);
  1170. // If the assignment operator returned an object by value it will
  1171. // be in a temporary variable which we need to destroy now
  1172. if( type.isTemporary && type.stackOffset != (short)offset )
  1173. ReleaseTemporaryVariable(type.stackOffset, bc);
  1174. // Release the original value too in case it is a temporary
  1175. ReleaseTemporaryVariable(arg->type, bc);
  1176. }
  1177. }
  1178. int asCCompiler::PrepareArgument(asCDataType *paramType, asCExprContext *ctx, asCScriptNode *node, bool isFunction, int refType, bool isMakingCopy)
  1179. {
  1180. asCDataType param = *paramType;
  1181. if( paramType->GetTokenType() == ttQuestion )
  1182. {
  1183. // The function is expecting a var type. If the argument is a function name, we must now decide which function it is
  1184. DetermineSingleFunc(ctx, node);
  1185. // Since the function is expecting a var type ?, then we don't want to convert the argument to anything else
  1186. param = ctx->type.dataType;
  1187. param.MakeHandle(ctx->type.isExplicitHandle || ctx->type.IsNullConstant());
  1188. // Treat the void expression like a null handle when working with var types
  1189. if( ctx->IsVoidExpression() )
  1190. param = asCDataType::CreateNullHandle();
  1191. // If value assign is disabled for reference types, then make
  1192. // sure to always pass the handle to ? parameters
  1193. if( builder->engine->ep.disallowValueAssignForRefType &&
  1194. ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCOPED) )
  1195. {
  1196. param.MakeHandle(true);
  1197. }
  1198. param.MakeReference(paramType->IsReference());
  1199. param.MakeReadOnly(paramType->IsReadOnly());
  1200. }
  1201. else
  1202. param = *paramType;
  1203. asCDataType dt = param;
  1204. // Need to protect arguments by reference
  1205. if( isFunction && dt.IsReference() )
  1206. {
  1207. // Allocate a temporary variable of the same type as the argument
  1208. dt.MakeReference(false);
  1209. dt.MakeReadOnly(false);
  1210. int offset;
  1211. if( refType == asTM_INREF )
  1212. {
  1213. ProcessPropertyGetAccessor(ctx, node);
  1214. // Add the type id as hidden arg if the parameter is a ? type
  1215. if( paramType->GetTokenType() == ttQuestion )
  1216. {
  1217. asCByteCode tmpBC(engine);
  1218. // Place the type id on the stack as a hidden parameter
  1219. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1220. // Insert the code before the expression code
  1221. tmpBC.AddCode(&ctx->bc);
  1222. ctx->bc.AddCode(&tmpBC);
  1223. }
  1224. if( dt.IsPrimitive() )
  1225. {
  1226. // If the reference is const, then it is not necessary to make a copy if the value already is a variable
  1227. // Even if the same variable is passed in another argument as non-const then there is no problem
  1228. IsVariableInitialized(&ctx->type, node);
  1229. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1230. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  1231. if( !(param.IsReadOnly() && ctx->type.isVariable) )
  1232. ConvertToTempVariable(ctx);
  1233. PushVariableOnStack(ctx, true);
  1234. ctx->type.dataType.MakeReadOnly(param.IsReadOnly());
  1235. }
  1236. else if( ctx->type.dataType.IsNullHandle() )
  1237. {
  1238. // Make sure the argument type can support handles (or is itself a handle)
  1239. if( !dt.SupportHandles() && !dt.IsObjectHandle() )
  1240. {
  1241. asCString str;
  1242. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), param.Format(outFunc->nameSpace).AddressOf());
  1243. Error(str, node);
  1244. ctx->type.Set(param);
  1245. return -1;
  1246. }
  1247. // Need to initialize a local temporary variable to
  1248. // represent the null handle when passed as reference
  1249. asASSERT( ctx->bc.GetLastInstr() == asBC_PshNull );
  1250. ctx->bc.Instr(asBC_PopPtr);
  1251. dt.MakeHandle(true);
  1252. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1253. // Push the reference to the variable on the stack
  1254. ctx->bc.InstrWORD(asBC_PSF, (short)offset);
  1255. ctx->type.SetVariable(dt, offset, true);
  1256. }
  1257. else
  1258. {
  1259. IsVariableInitialized(&ctx->type, node);
  1260. if( !isMakingCopy )
  1261. {
  1262. // Even though the parameter expects a reference, it is only meant to be
  1263. // used as input value and doesn't have to refer to the actual object, so it
  1264. // is OK to do an implicit conversion.
  1265. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  1266. if( !ctx->type.dataType.IsEqualExceptRefAndConst(param) )
  1267. {
  1268. asCString str;
  1269. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), param.Format(outFunc->nameSpace).AddressOf());
  1270. Error(str, node);
  1271. ctx->type.Set(param);
  1272. return -1;
  1273. }
  1274. // The compiler must guarantee that the object stays alive during the execution
  1275. // of the function, and it must also guarantee that the value isn't modified by
  1276. // the function.
  1277. // If the argument is a temporary local variable then it is safe to be passed to
  1278. // the function as it is, since the local variable will stay alive, and since it
  1279. // is temporary there is no side effect if the function modifies it.
  1280. // If the parameter is read-only and therefore guaranteed not to be modified by the
  1281. // function, then it is enough that the variable is local to guarantee the lifetime.
  1282. if( !ctx->type.isTemporary && !(param.IsReadOnly() && ctx->type.isVariable) )
  1283. {
  1284. if( ctx->type.dataType.IsFuncdef() || ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && param.IsReadOnly() && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCOPED)) )
  1285. {
  1286. // Funcdefs only need an extra handle to guarantee the lifetime.
  1287. // If the object is a reference type (except scoped reference types), and the
  1288. // parameter is a const reference, then it is not necessary to make a copy of the
  1289. // object. The compiler just needs to hold a handle to guarantee the lifetime.
  1290. // Allocate a handle variable
  1291. dt.MakeHandle(true);
  1292. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1293. // Copy the handle
  1294. Dereference(ctx, true);
  1295. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1296. if (ctx->type.dataType.IsFuncdef())
  1297. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  1298. else
  1299. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  1300. ctx->bc.Instr(asBC_PopPtr);
  1301. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1302. // The type should be set to the param type instead of dt to guarantee
  1303. // that the expression keeps the correct type for variable ? args. Otherwise
  1304. // MoveArgsToStack will use the wrong bytecode to move the arg to the stack
  1305. ctx->type.SetVariable(param, offset, true);
  1306. }
  1307. else
  1308. {
  1309. // Make a copy of the object to guarantee that the original isn't modified
  1310. asASSERT(!dt.IsFuncdef());
  1311. // Allocate and initialize a temporary local object
  1312. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1313. CompileInitAsCopy(dt, offset, &ctx->bc, ctx, node, false);
  1314. // Push the object pointer on the stack
  1315. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1316. if( dt.IsObject() && !dt.IsObjectHandle() )
  1317. ctx->bc.Instr(asBC_RDSPtr);
  1318. // Set the resulting type
  1319. ctx->type.Set(dt);
  1320. ctx->type.isTemporary = true;
  1321. ctx->type.stackOffset = short(offset);
  1322. if( dt.IsObjectHandle() )
  1323. ctx->type.isExplicitHandle = true;
  1324. ctx->type.dataType.MakeReference(false);
  1325. if( paramType->IsReadOnly() )
  1326. ctx->type.dataType.MakeReadOnly(true);
  1327. }
  1328. }
  1329. }
  1330. else
  1331. {
  1332. // We must guarantee that the address to the value is on the stack
  1333. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) &&
  1334. !ctx->type.dataType.IsObjectHandle() &&
  1335. ctx->type.dataType.IsReference() )
  1336. Dereference(ctx, true);
  1337. }
  1338. }
  1339. }
  1340. else if( refType == asTM_OUTREF )
  1341. {
  1342. // Add the type id as hidden arg if the parameter is a ? type
  1343. if( paramType->GetTokenType() == ttQuestion )
  1344. {
  1345. asCByteCode tmpBC(engine);
  1346. // Place the type id on the stack as a hidden parameter
  1347. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1348. // Insert the code before the expression code
  1349. tmpBC.AddCode(&ctx->bc);
  1350. ctx->bc.AddCode(&tmpBC);
  1351. }
  1352. // If the expression is marked as clean, then it can be used directly
  1353. // without the need to allocate another temporary value as it is known
  1354. // that the argument has no other value than the default
  1355. if( ctx->isCleanArg )
  1356. {
  1357. // Must be a local variable
  1358. asASSERT( ctx->type.isVariable );
  1359. }
  1360. else
  1361. {
  1362. // Make sure the variable is not used in the expression
  1363. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1364. if( dt.IsPrimitive() )
  1365. {
  1366. ctx->type.SetVariable(dt, offset, true);
  1367. PushVariableOnStack(ctx, true);
  1368. }
  1369. else
  1370. {
  1371. // TODO: Need to reserve variables, as the default constructor may need
  1372. // to allocate temporary variables to compute default args
  1373. // Allocate and construct the temporary object
  1374. asCByteCode tmpBC(engine);
  1375. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1376. // Insert the code before the expression code
  1377. tmpBC.AddCode(&ctx->bc);
  1378. ctx->bc.AddCode(&tmpBC);
  1379. dt.MakeReference(!(dt.IsObject() || dt.IsFuncdef()) || dt.IsObjectHandle());
  1380. asCExprValue type;
  1381. type.Set(dt);
  1382. type.isTemporary = true;
  1383. type.stackOffset = (short)offset;
  1384. ctx->type = type;
  1385. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1386. if( (dt.IsObject() || dt.IsFuncdef()) && !dt.IsObjectHandle() )
  1387. ctx->bc.Instr(asBC_RDSPtr);
  1388. }
  1389. // After the function returns the temporary variable will
  1390. // be assigned to the expression, if it is a valid lvalue
  1391. }
  1392. }
  1393. else if( refType == asTM_INOUTREF )
  1394. {
  1395. ProcessPropertyGetAccessor(ctx, node);
  1396. // Add the type id as hidden arg if the parameter is a ? type
  1397. if( paramType->GetTokenType() == ttQuestion )
  1398. {
  1399. asCByteCode tmpBC(engine);
  1400. // Place the type id on the stack as a hidden parameter
  1401. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1402. // Insert the code before the expression code
  1403. tmpBC.AddCode(&ctx->bc);
  1404. ctx->bc.AddCode(&tmpBC);
  1405. }
  1406. // Literal constants cannot be passed to inout ref arguments
  1407. if( !ctx->type.isVariable && ctx->type.isConstant )
  1408. {
  1409. // Unless unsafe references are turned on and the reference is const
  1410. if( param.IsReadOnly() && engine->ep.allowUnsafeReferences )
  1411. {
  1412. // Since the parameter is a const & make a copy.
  1413. ConvertToTempVariable(ctx);
  1414. ctx->type.dataType.MakeReadOnly(true);
  1415. }
  1416. else
  1417. {
  1418. Error(TXT_NOT_VALID_REFERENCE, node);
  1419. return -1;
  1420. }
  1421. }
  1422. // Perform implicit ref cast if necessary, but don't allow the implicit conversion to create new objects
  1423. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && ctx->type.dataType.GetTypeInfo() != dt.GetTypeInfo() )
  1424. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true, false);
  1425. // Only objects that support object handles
  1426. // can be guaranteed to be safe. Local variables are
  1427. // already safe, so there is no need to add an extra
  1428. // references
  1429. if( !engine->ep.allowUnsafeReferences &&
  1430. !ctx->type.isVariable &&
  1431. (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) &&
  1432. !ctx->type.dataType.IsObjectHandle() &&
  1433. ((ctx->type.dataType.GetBehaviour()->addref &&
  1434. ctx->type.dataType.GetBehaviour()->release) ||
  1435. (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_NOCOUNT) ||
  1436. ctx->type.dataType.IsFuncdef()) )
  1437. {
  1438. // Store a handle to the object as local variable
  1439. asCExprContext tmp(engine);
  1440. dt = ctx->type.dataType;
  1441. dt.MakeHandle(true);
  1442. dt.MakeReference(false);
  1443. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1444. // Copy the handle
  1445. if( !ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReference() )
  1446. ctx->bc.Instr(asBC_RDSPtr);
  1447. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1448. if( ctx->type.dataType.IsFuncdef() )
  1449. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  1450. else
  1451. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  1452. ctx->bc.Instr(asBC_PopPtr);
  1453. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1454. dt.MakeHandle(false);
  1455. dt.MakeReference(true);
  1456. // Release previous temporary variable stored in the context (if any)
  1457. if( ctx->type.isTemporary )
  1458. ReleaseTemporaryVariable(ctx->type.stackOffset, &ctx->bc);
  1459. ctx->type.SetVariable(dt, offset, true);
  1460. }
  1461. // Make sure the reference to the value is on the stack
  1462. // For objects, the reference needs to be dereferenced so the pointer on the stack is to the actual object
  1463. // For handles, the reference shouldn't be changed because the pointer on the stack should be to the handle
  1464. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && ctx->type.dataType.IsReference() && !param.IsObjectHandle() )
  1465. Dereference(ctx, true);
  1466. else if( ctx->type.isVariable && !(ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) )
  1467. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  1468. else if( ctx->type.dataType.IsPrimitive() )
  1469. ctx->bc.Instr(asBC_PshRPtr);
  1470. else if( ctx->type.dataType.IsObjectHandle() && !ctx->type.dataType.IsReference() )
  1471. ImplicitConversion(ctx, param, node, asIC_IMPLICIT_CONV, true, false);
  1472. }
  1473. }
  1474. else
  1475. {
  1476. ProcessPropertyGetAccessor(ctx, node);
  1477. if( dt.IsPrimitive() )
  1478. {
  1479. IsVariableInitialized(&ctx->type, node);
  1480. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1481. // Implicitly convert primitives to the parameter type
  1482. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1483. if( ctx->type.isVariable )
  1484. {
  1485. PushVariableOnStack(ctx, dt.IsReference());
  1486. }
  1487. else if( ctx->type.isConstant )
  1488. {
  1489. ConvertToVariable(ctx);
  1490. PushVariableOnStack(ctx, dt.IsReference());
  1491. }
  1492. }
  1493. else
  1494. {
  1495. IsVariableInitialized(&ctx->type, node);
  1496. // Implicitly convert primitives to the parameter type
  1497. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1498. // Was the conversion successful?
  1499. if( !ctx->type.dataType.IsEqualExceptRef(dt) )
  1500. {
  1501. asCString str;
  1502. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), dt.Format(outFunc->nameSpace).AddressOf());
  1503. Error(str, node);
  1504. ctx->type.Set(dt);
  1505. return -1;
  1506. }
  1507. if( dt.IsObjectHandle() )
  1508. ctx->type.isExplicitHandle = true;
  1509. if( (dt.IsObject() || dt.IsFuncdef()) && !dt.IsNullHandle() && !dt.IsReference() )
  1510. {
  1511. // Objects passed by value must be placed in temporary variables
  1512. // so that they are guaranteed to not be referenced anywhere else.
  1513. // The object must also be allocated on the heap, as the memory will
  1514. // be deleted by the called function.
  1515. // Handles passed by value must also be placed in a temporary variable
  1516. // to guarantee that the object referred to isn't freed too early.
  1517. // TODO: value on stack: How can we avoid this unnecessary allocation?
  1518. // Don't make temporary copies of handles if it is going to be used
  1519. // for handle assignment anyway, i.e. REFCPY.
  1520. if( !(!isFunction && isMakingCopy && ctx->type.dataType.IsObjectHandle() && ctx->type.isVariable) )
  1521. PrepareTemporaryVariable(node, ctx, true);
  1522. }
  1523. }
  1524. }
  1525. // Don't put any pointer on the stack yet
  1526. if( param.IsReference() || ((param.IsObject() || param.IsFuncdef()) && !param.IsNullHandle()) )
  1527. {
  1528. // &inout parameter may leave the reference on the stack already
  1529. if( refType != asTM_INOUTREF )
  1530. {
  1531. asASSERT( ctx->type.isVariable || ctx->type.isTemporary || isMakingCopy );
  1532. if( ctx->type.isVariable || ctx->type.isTemporary )
  1533. {
  1534. ctx->bc.Instr(asBC_PopPtr);
  1535. ctx->bc.InstrSHORT(asBC_VAR, ctx->type.stackOffset);
  1536. ProcessDeferredParams(ctx);
  1537. }
  1538. }
  1539. }
  1540. return 0;
  1541. }
  1542. void asCCompiler::PrepareFunctionCall(int funcId, asCByteCode *bc, asCArray<asCExprContext *> &args)
  1543. {
  1544. // When a match has been found, compile the final byte code using correct parameter types
  1545. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1546. asASSERT( descr->parameterTypes.GetLength() == args.GetLength() );
  1547. // If the function being called is the opAssign or copy constructor for the same type
  1548. // as the argument, then we should avoid making temporary copy of the argument
  1549. bool makingCopy = false;
  1550. if( descr->parameterTypes.GetLength() == 1 &&
  1551. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1552. (((descr->name == "opAssign" || descr->name == "$beh0") && descr->objectType && descr->objectType == args[0]->type.dataType.GetTypeInfo()) ||
  1553. (descr->objectType == 0 && args[0]->type.dataType.GetTypeInfo() && descr->name == args[0]->type.dataType.GetTypeInfo()->name)) )
  1554. makingCopy = true;
  1555. // Add code for arguments
  1556. asCExprContext e(engine);
  1557. for( int n = (int)args.GetLength()-1; n >= 0; n-- )
  1558. {
  1559. // Make sure PrepareArgument doesn't use any variable that is already
  1560. // being used by any of the following argument expressions
  1561. int l = int(reservedVariables.GetLength());
  1562. for( int m = n-1; m >= 0; m-- )
  1563. args[m]->bc.GetVarsUsed(reservedVariables);
  1564. PrepareArgument2(&e, args[n], &descr->parameterTypes[n], true, descr->inOutFlags[n], makingCopy);
  1565. reservedVariables.SetLength(l);
  1566. }
  1567. bc->AddCode(&e.bc);
  1568. }
  1569. void asCCompiler::MoveArgsToStack(int funcId, asCByteCode *bc, asCArray<asCExprContext *> &args, bool addOneToOffset)
  1570. {
  1571. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1572. int offset = 0;
  1573. if( addOneToOffset )
  1574. offset += AS_PTR_SIZE;
  1575. // The address of where the return value should be stored is push on top of the arguments
  1576. if( descr->DoesReturnOnStack() )
  1577. offset += AS_PTR_SIZE;
  1578. #ifdef AS_DEBUG
  1579. // If the function being called is the opAssign or copy constructor for the same type
  1580. // as the argument, then we should avoid making temporary copy of the argument
  1581. bool makingCopy = false;
  1582. if( descr->parameterTypes.GetLength() == 1 &&
  1583. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1584. (((descr->name == "opAssign" || descr->name == "$beh0") && descr->objectType && descr->objectType == args[0]->type.dataType.GetTypeInfo()) ||
  1585. (descr->objectType == 0 && args[0]->type.dataType.GetTypeInfo() && descr->name == args[0]->type.dataType.GetTypeInfo()->name)) )
  1586. makingCopy = true;
  1587. #endif
  1588. // Move the objects that are sent by value to the stack just before the call
  1589. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  1590. {
  1591. if( descr->parameterTypes[n].IsReference() )
  1592. {
  1593. if( (descr->parameterTypes[n].IsObject() || descr->parameterTypes[n].IsFuncdef()) && !descr->parameterTypes[n].IsObjectHandle() )
  1594. {
  1595. if( descr->inOutFlags[n] != asTM_INOUTREF )
  1596. {
  1597. #ifdef AS_DEBUG
  1598. // This assert is inside AS_DEBUG because of the variable makingCopy which is only defined in debug mode
  1599. asASSERT( args[n]->type.isVariable || args[n]->type.isTemporary || makingCopy );
  1600. #endif
  1601. if( (args[n]->type.isVariable || args[n]->type.isTemporary) )
  1602. {
  1603. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1604. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1605. // as the value allocated on the stack is guaranteed to be safe
  1606. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1607. else
  1608. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1609. }
  1610. }
  1611. if( args[n]->type.dataType.IsObjectHandle() )
  1612. bc->InstrWORD(asBC_ChkNullS, (asWORD)offset);
  1613. }
  1614. else if( descr->inOutFlags[n] != asTM_INOUTREF )
  1615. {
  1616. if( descr->parameterTypes[n].GetTokenType() == ttQuestion &&
  1617. (args[n]->type.dataType.IsObject() || args[n]->type.dataType.IsFuncdef()) &&
  1618. !args[n]->type.dataType.IsObjectHandle() )
  1619. {
  1620. // Send the object as a reference to the object,
  1621. // and not to the variable holding the object
  1622. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1623. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1624. // as the value allocated on the stack is guaranteed to be safe
  1625. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1626. else
  1627. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1628. }
  1629. else
  1630. {
  1631. // If the variable is really an argument of @& type, then it is necessary
  1632. // to use asBC_GETOBJREF so the pointer is correctly dereferenced.
  1633. sVariable *var = variables->GetVariableByOffset(args[n]->type.stackOffset);
  1634. if (var == 0 || !var->type.IsReference() || !var->type.IsObjectHandle())
  1635. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1636. else
  1637. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1638. }
  1639. }
  1640. }
  1641. else if( descr->parameterTypes[n].IsObject() || descr->parameterTypes[n].IsFuncdef() )
  1642. {
  1643. // TODO: value on stack: What can we do to avoid this unnecessary allocation?
  1644. // The object must be allocated on the heap, because this memory will be deleted in as_callfunc_xxx
  1645. asASSERT(IsVariableOnHeap(args[n]->type.stackOffset));
  1646. // The pointer in the variable will be moved to the stack
  1647. bc->InstrWORD(asBC_GETOBJ, (asWORD)offset);
  1648. // Deallocate the variable slot so it can be reused, but do not attempt to
  1649. // free the content of the variable since it was moved to the stack for the call
  1650. DeallocateVariable(args[n]->type.stackOffset);
  1651. args[n]->type.isTemporary = false;
  1652. }
  1653. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  1654. }
  1655. }
  1656. int asCCompiler::CompileArgumentList(asCScriptNode *node, asCArray<asCExprContext*> &args, asCArray<asSNamedArgument> &namedArgs)
  1657. {
  1658. asASSERT(node->nodeType == snArgList);
  1659. // Count arguments
  1660. asCScriptNode *arg = node->firstChild;
  1661. int argCount = 0;
  1662. while( arg )
  1663. {
  1664. if( arg->nodeType != snNamedArgument )
  1665. argCount++;
  1666. arg = arg->next;
  1667. }
  1668. // Prepare the arrays
  1669. args.SetLength(argCount);
  1670. int n;
  1671. for( n = 0; n < argCount; n++ )
  1672. args[n] = 0;
  1673. n = argCount-1;
  1674. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1675. bool anyErrors = false, inPositionalArguments = false;
  1676. arg = node->lastChild;
  1677. while( arg )
  1678. {
  1679. asCScriptNode *asgNode = arg, *namedNode = 0;
  1680. if( asgNode->nodeType == snNamedArgument )
  1681. {
  1682. if( inPositionalArguments )
  1683. {
  1684. Error(TXT_POS_ARG_AFTER_NAMED_ARG, node);
  1685. return -1;
  1686. }
  1687. asgNode = arg->firstChild->next;
  1688. namedNode = arg->firstChild;
  1689. asASSERT( namedNode->nodeType == snIdentifier );
  1690. }
  1691. else
  1692. inPositionalArguments = true;
  1693. asCExprContext expr(engine);
  1694. int r = CompileAssignment(asgNode, &expr);
  1695. if( r < 0 ) anyErrors = true;
  1696. asCExprContext *ctx = asNEW(asCExprContext)(engine);
  1697. if( ctx == 0 )
  1698. {
  1699. // Out of memory
  1700. return -1;
  1701. }
  1702. MergeExprBytecodeAndType(ctx, &expr);
  1703. if( inPositionalArguments )
  1704. {
  1705. args[n] = ctx;
  1706. n--;
  1707. }
  1708. else
  1709. {
  1710. asSNamedArgument namedArg;
  1711. namedArg.name = asCString(&script->code[namedNode->tokenPos], namedNode->tokenLength);
  1712. namedArg.ctx = ctx;
  1713. // Error out when multiple arguments with the same name are passed
  1714. for( asUINT a = 0; a < namedArgs.GetLength(); ++a )
  1715. {
  1716. if( namedArgs[a].name == namedArg.name )
  1717. {
  1718. Error(TXT_DUPLICATE_NAMED_ARG, asgNode);
  1719. anyErrors = true;
  1720. break;
  1721. }
  1722. }
  1723. namedArgs.PushLast(namedArg);
  1724. }
  1725. arg = arg->prev;
  1726. }
  1727. return anyErrors ? -1 : 0;
  1728. }
  1729. int asCCompiler::CompileDefaultAndNamedArgs(asCScriptNode *node, asCArray<asCExprContext*> &args, int funcId, asCObjectType *objectType, asCArray<asSNamedArgument> *namedArgs)
  1730. {
  1731. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  1732. if( func == 0 || args.GetLength() >= (asUINT)func->GetParamCount() )
  1733. return 0;
  1734. // Make sure to use the real function for virtual functions
  1735. if( func->funcType == asFUNC_VIRTUAL )
  1736. {
  1737. asASSERT( objectType );
  1738. func = objectType->virtualFunctionTable[func->vfTableIdx];
  1739. }
  1740. // Make sure none of the variables used in the previous arguments are reused in the default arguments
  1741. bool anyErrors = false;
  1742. int prevReservedVars = reservedVariables.GetLength();
  1743. int explicitArgs = (int)args.GetLength();
  1744. for( int p = 0; p < explicitArgs; p++ )
  1745. args[p]->bc.GetVarsUsed(reservedVariables);
  1746. // Make space for all the new arguments
  1747. args.SetLength(func->parameterTypes.GetLength());
  1748. for( asUINT c = explicitArgs; c < args.GetLength(); c++ )
  1749. args[c] = 0;
  1750. // Add the named arguments to the argument list in the right position
  1751. if( namedArgs )
  1752. {
  1753. for( asUINT n = 0; n < namedArgs->GetLength(); ++n )
  1754. {
  1755. asSNamedArgument &named = (*namedArgs)[n];
  1756. named.ctx->bc.GetVarsUsed(reservedVariables);
  1757. // Find the right spot to put it in
  1758. asUINT index = asUINT(-1);
  1759. for( asUINT j = 0; j < func->parameterTypes.GetLength(); ++j )
  1760. {
  1761. if( func->parameterNames[j] == (*namedArgs)[n].name )
  1762. {
  1763. index = j;
  1764. break;
  1765. }
  1766. }
  1767. asASSERT( index < args.GetLength() );
  1768. args[index] = named.ctx;
  1769. named.ctx = 0;
  1770. }
  1771. }
  1772. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1773. for( int n = (int)func->parameterTypes.GetLength() - 1; n >= explicitArgs; n-- )
  1774. {
  1775. if( args[n] != 0 ) continue;
  1776. if( func->defaultArgs[n] == 0 ) { anyErrors = true; continue; }
  1777. // Parse the default arg string
  1778. asCParser parser(builder);
  1779. asCScriptCode code;
  1780. code.SetCode("default arg", func->defaultArgs[n]->AddressOf(), false);
  1781. int r = parser.ParseExpression(&code);
  1782. if( r < 0 )
  1783. {
  1784. asCString msg;
  1785. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1786. Error(msg, node);
  1787. anyErrors = true;
  1788. continue;
  1789. }
  1790. asCScriptNode *arg = parser.GetScriptNode();
  1791. // Temporarily set the script code to the default arg expression
  1792. asCScriptCode *origScript = script;
  1793. script = &code;
  1794. // Don't allow the expression to access local variables
  1795. isCompilingDefaultArg = true;
  1796. // Temporarily set the namespace in the output function to the namespace of the called
  1797. // function so that the default arguments are evaluated in the correct namespace
  1798. asSNameSpace *origNameSpace = outFunc->nameSpace;
  1799. outFunc->nameSpace = func->nameSpace;
  1800. asCExprContext expr(engine);
  1801. r = CompileExpression(arg, &expr);
  1802. // Restore the namespace
  1803. outFunc->nameSpace = origNameSpace;
  1804. // Don't allow address of class method
  1805. if( expr.methodName != "" )
  1806. {
  1807. // TODO: Improve error message
  1808. Error(TXT_DEF_ARG_TYPE_DOESNT_MATCH, arg);
  1809. r = -1;
  1810. }
  1811. // Make sure the expression can be implicitly converted to the parameter type
  1812. if( r >= 0 )
  1813. {
  1814. asCArray<int> funcs;
  1815. funcs.PushLast(func->id);
  1816. asCArray<asSOverloadCandidate> matches;
  1817. if( MatchArgument(funcs, matches, &expr, n) == 0 )
  1818. {
  1819. Error(TXT_DEF_ARG_TYPE_DOESNT_MATCH, arg);
  1820. r = -1;
  1821. }
  1822. }
  1823. isCompilingDefaultArg = false;
  1824. script = origScript;
  1825. if( r < 0 )
  1826. {
  1827. asCString msg;
  1828. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1829. Error(msg, node);
  1830. anyErrors = true;
  1831. continue;
  1832. }
  1833. args[n] = asNEW(asCExprContext)(engine);
  1834. if( args[n] == 0 )
  1835. {
  1836. // Out of memory
  1837. reservedVariables.SetLength(prevReservedVars);
  1838. return -1;
  1839. }
  1840. MergeExprBytecodeAndType(args[n], &expr);
  1841. }
  1842. reservedVariables.SetLength(prevReservedVars);
  1843. return anyErrors ? -1 : 0;
  1844. }
  1845. asUINT asCCompiler::MatchFunctions(asCArray<int> &funcs, asCArray<asCExprContext*> &args, asCScriptNode *node, const char *name, asCArray<asSNamedArgument> *namedArgs, asCObjectType *objectType, bool isConstMethod, bool silent, bool allowObjectConstruct, const asCString &scope)
  1846. {
  1847. asCArray<int> origFuncs = funcs; // Keep the original list for error message
  1848. asUINT cost = 0;
  1849. asUINT n;
  1850. if( funcs.GetLength() > 0 )
  1851. {
  1852. // Check the number of parameters in the found functions
  1853. asUINT totalArgs = (asUINT)args.GetLength();
  1854. if( namedArgs != 0 )
  1855. totalArgs += (asUINT)namedArgs->GetLength();
  1856. for( n = 0; n < funcs.GetLength(); ++n )
  1857. {
  1858. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  1859. if( desc->parameterTypes.GetLength() != totalArgs )
  1860. {
  1861. bool noMatch = true;
  1862. if( totalArgs < desc->parameterTypes.GetLength() )
  1863. {
  1864. // For virtual functions, the default args are defined in the real function of the object
  1865. if( desc->funcType == asFUNC_VIRTUAL )
  1866. desc = objectType->virtualFunctionTable[desc->vfTableIdx];
  1867. // Count the number of default args
  1868. asUINT defaultArgs = 0;
  1869. for( asUINT d = 0; d < desc->defaultArgs.GetLength(); d++ )
  1870. if( desc->defaultArgs[d] )
  1871. defaultArgs++;
  1872. if( totalArgs >= desc->parameterTypes.GetLength() - defaultArgs )
  1873. noMatch = false;
  1874. }
  1875. if( noMatch )
  1876. {
  1877. // remove it from the list
  1878. if( n == funcs.GetLength()-1 )
  1879. funcs.PopLast();
  1880. else
  1881. funcs[n] = funcs.PopLast();
  1882. n--;
  1883. }
  1884. }
  1885. }
  1886. // Match functions with the parameters, and discard those that do not match
  1887. asCArray<asSOverloadCandidate> matchingFuncs;
  1888. matchingFuncs.SetLengthNoConstruct( funcs.GetLength() );
  1889. for ( n = 0; n < funcs.GetLength(); ++n )
  1890. {
  1891. matchingFuncs[n].funcId = funcs[n];
  1892. matchingFuncs[n].cost = 0;
  1893. }
  1894. // Match positionally passed arguments
  1895. for( n = 0; n < args.GetLength(); ++n )
  1896. {
  1897. asCArray<asSOverloadCandidate> tempFuncs;
  1898. MatchArgument(funcs, tempFuncs, args[n], n, allowObjectConstruct);
  1899. // Intersect the found functions with the list of matching functions
  1900. for( asUINT f = 0; f < matchingFuncs.GetLength(); f++ )
  1901. {
  1902. asUINT c;
  1903. for( c = 0; c < tempFuncs.GetLength(); c++ )
  1904. {
  1905. if( matchingFuncs[f].funcId == tempFuncs[c].funcId )
  1906. {
  1907. // Sum argument cost
  1908. matchingFuncs[f].cost += tempFuncs[c].cost;
  1909. break;
  1910. } // End if match
  1911. }
  1912. // Was the function a match?
  1913. if( c == tempFuncs.GetLength() )
  1914. {
  1915. // No, remove it from the list
  1916. if( f == matchingFuncs.GetLength()-1 )
  1917. matchingFuncs.PopLast();
  1918. else
  1919. matchingFuncs[f] = matchingFuncs.PopLast();
  1920. f--;
  1921. }
  1922. }
  1923. }
  1924. // Match named arguments
  1925. if( namedArgs != 0 )
  1926. {
  1927. for( asUINT i = 0; i < matchingFuncs.GetLength(); ++i )
  1928. {
  1929. asCScriptFunction *desc = builder->GetFunctionDescription(matchingFuncs[i].funcId);
  1930. if( desc->funcType == asFUNC_VIRTUAL )
  1931. desc = objectType->virtualFunctionTable[desc->vfTableIdx];
  1932. // Match every named argument to an argument in the function
  1933. for( n = 0; n < namedArgs->GetLength(); ++n )
  1934. (*namedArgs)[n].match = asUINT(-1);
  1935. bool matchedAll = true;
  1936. for( asUINT j = 0; j < desc->parameterTypes.GetLength(); ++j )
  1937. {
  1938. asUINT match = asUINT(-1);
  1939. for( n = 0; n < namedArgs->GetLength(); ++n )
  1940. {
  1941. asSNamedArgument &namedArg = (*namedArgs)[n];
  1942. if( desc->parameterNames[j] == namedArg.name )
  1943. {
  1944. namedArg.match = j;
  1945. match = n;
  1946. break;
  1947. }
  1948. }
  1949. // Check that every position is filled somehow
  1950. if( j >= args.GetLength() )
  1951. {
  1952. if( match == asUINT(-1) && !desc->defaultArgs[j] )
  1953. {
  1954. // No argument was found for this, and there is no
  1955. // default, so it doesn't work.
  1956. matchedAll = false;
  1957. break;
  1958. }
  1959. }
  1960. else
  1961. {
  1962. if( match != asUINT(-1) )
  1963. {
  1964. // Can't name an argument that was already passed
  1965. matchedAll = false;
  1966. break;
  1967. }
  1968. }
  1969. }
  1970. // Check that every named argument was matched
  1971. if( matchedAll )
  1972. {
  1973. for( n = 0; n < namedArgs->GetLength(); ++n )
  1974. {
  1975. asSNamedArgument &named = (*namedArgs)[n];
  1976. if( named.match == asUINT(-1) )
  1977. {
  1978. matchedAll = false;
  1979. break;
  1980. }
  1981. // Add to the cost
  1982. cost = MatchArgument(desc, named.ctx, named.match, allowObjectConstruct);
  1983. if( cost == asUINT(-1) )
  1984. {
  1985. matchedAll = false;
  1986. break;
  1987. }
  1988. matchingFuncs[i].cost += cost;
  1989. }
  1990. }
  1991. if( !matchedAll )
  1992. {
  1993. // Remove the function, we didn't match all the arguments.
  1994. if( i == matchingFuncs.GetLength()-1 )
  1995. matchingFuncs.PopLast();
  1996. else
  1997. matchingFuncs[i] = matchingFuncs.PopLast();
  1998. i--;
  1999. }
  2000. }
  2001. }
  2002. // Select the overload(s) with the lowest overall cost
  2003. funcs.SetLength(0);
  2004. asUINT bestCost = asUINT(-1);
  2005. for( n = 0; n < matchingFuncs.GetLength(); ++n )
  2006. {
  2007. cost = matchingFuncs[n].cost;
  2008. if( cost < bestCost )
  2009. {
  2010. funcs.SetLength(0);
  2011. bestCost = cost;
  2012. }
  2013. if( cost == bestCost )
  2014. funcs.PushLast( matchingFuncs[n].funcId );
  2015. }
  2016. // Cost returned is equivalent to the best cost discovered
  2017. cost = bestCost;
  2018. }
  2019. if( !isConstMethod )
  2020. FilterConst(funcs);
  2021. if( funcs.GetLength() != 1 && !silent )
  2022. {
  2023. // Build a readable string of the function with parameter types
  2024. bool attemptsPassingClassMethod = false;
  2025. asCString str;
  2026. if( scope != "" && scope != "::" )
  2027. str = scope + "::";
  2028. str += name;
  2029. str += "(";
  2030. for( n = 0; n < args.GetLength(); n++ )
  2031. {
  2032. if( n > 0 )
  2033. str += ", ";
  2034. if( args[n]->methodName != "" )
  2035. {
  2036. if( args[n]->IsClassMethod() )
  2037. {
  2038. attemptsPassingClassMethod = true;
  2039. str += args[n]->type.dataType.GetTypeInfo()->GetName();
  2040. str += "::";
  2041. }
  2042. str += args[n]->methodName;
  2043. }
  2044. else
  2045. str += args[n]->type.dataType.Format(outFunc->nameSpace);
  2046. }
  2047. if( namedArgs != 0 )
  2048. {
  2049. for( n = 0; n < namedArgs->GetLength(); n++ )
  2050. {
  2051. if( n > 0 || args.GetLength() )
  2052. str += ", ";
  2053. asSNamedArgument &named = (*namedArgs)[n];
  2054. str += named.name;
  2055. str += "=";
  2056. if( named.ctx->methodName != "" )
  2057. str += named.ctx->methodName;
  2058. else
  2059. str += named.ctx->type.dataType.Format(outFunc->nameSpace);
  2060. }
  2061. }
  2062. str += ")";
  2063. if( isConstMethod )
  2064. str += " const";
  2065. if( objectType && scope == "" )
  2066. str = objectType->name + "::" + str;
  2067. if( funcs.GetLength() == 0 )
  2068. {
  2069. str.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  2070. Error(str, node);
  2071. if( attemptsPassingClassMethod )
  2072. {
  2073. // Class methods must use delegate objects
  2074. Error(TXT_CANNOT_PASS_CLASS_METHOD_AS_ARG, node);
  2075. }
  2076. else
  2077. {
  2078. // Print the list of candidates
  2079. if( origFuncs.GetLength() > 0 )
  2080. {
  2081. int r = 0, c = 0;
  2082. asASSERT( node );
  2083. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  2084. builder->WriteInfo(script->name.AddressOf(), TXT_CANDIDATES_ARE, r, c, false);
  2085. PrintMatchingFuncs(origFuncs, node, objectType);
  2086. }
  2087. }
  2088. }
  2089. else
  2090. {
  2091. asASSERT( attemptsPassingClassMethod == false );
  2092. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  2093. Error(str, node);
  2094. PrintMatchingFuncs(funcs, node, objectType);
  2095. }
  2096. }
  2097. return cost;
  2098. }
  2099. bool asCCompiler::CompileAutoType(asCDataType &type, asCExprContext &compiledCtx, asCScriptNode *node, asCScriptNode *errNode)
  2100. {
  2101. if( node && node->nodeType == snAssignment )
  2102. {
  2103. int r = CompileAssignment(node, &compiledCtx);
  2104. if( r >= 0 )
  2105. {
  2106. // Must not have unused ambiguous names
  2107. if (compiledCtx.IsClassMethod() || compiledCtx.IsGlobalFunc())
  2108. {
  2109. // TODO: Should mention that the problem is the ambiguous name
  2110. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2111. return false;
  2112. }
  2113. // Must not have unused anonymous functions
  2114. if (compiledCtx.IsLambda())
  2115. {
  2116. // TODO: Should mention that the problem is the anonymous function
  2117. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2118. return false;
  2119. }
  2120. // Must not be a null handle
  2121. if (compiledCtx.type.dataType.IsNullHandle())
  2122. {
  2123. // TODO: Should mention that the problem is the null pointer
  2124. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2125. return false;
  2126. }
  2127. asCDataType newType = compiledCtx.type.dataType;
  2128. // Handle const qualifier on auto
  2129. if (type.IsReadOnly())
  2130. newType.MakeReadOnly(true);
  2131. else if (newType.IsPrimitive())
  2132. newType.MakeReadOnly(false);
  2133. // Handle reference/value stuff
  2134. newType.MakeReference(false);
  2135. if (!newType.IsObjectHandle())
  2136. {
  2137. // We got a value object or an object reference.
  2138. // Turn the variable into a handle if specified
  2139. // as auto@, otherwise make it a 'value'.
  2140. if (type.IsHandleToAuto())
  2141. {
  2142. if (newType.MakeHandle(true) < 0)
  2143. {
  2144. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, errNode);
  2145. return false;
  2146. }
  2147. }
  2148. }
  2149. type = newType;
  2150. return true;
  2151. }
  2152. return false;
  2153. }
  2154. else
  2155. {
  2156. Error(TXT_CANNOT_RESOLVE_AUTO, errNode);
  2157. type = asCDataType::CreatePrimitive(ttInt, false);
  2158. return false;
  2159. }
  2160. }
  2161. void asCCompiler::CompileDeclaration(asCScriptNode *decl, asCByteCode *bc)
  2162. {
  2163. // Get the data type
  2164. asCDataType type = builder->CreateDataTypeFromNode(decl->firstChild, script, outFunc->nameSpace, false, outFunc->objectType);
  2165. // Declare all variables in this declaration
  2166. asCScriptNode *node = decl->firstChild->next;
  2167. while( node )
  2168. {
  2169. // If this is an auto type, we have to compile the assignment now to figure out the type
  2170. asCExprContext compiledCtx(engine);
  2171. bool preCompiled = false;
  2172. if (type.IsAuto())
  2173. {
  2174. preCompiled = CompileAutoType(type, compiledCtx, node->next, node);
  2175. if (!preCompiled)
  2176. {
  2177. // If it wasn't possible to determine the type from the expression then there
  2178. // is no need to continue with the initialization. The error was already reported
  2179. // in CompileAutoType.
  2180. return;
  2181. }
  2182. }
  2183. // Is the type allowed?
  2184. if( !type.CanBeInstantiated() )
  2185. {
  2186. asCString str;
  2187. if( type.IsAbstractClass() )
  2188. str.Format(TXT_ABSTRACT_CLASS_s_CANNOT_BE_INSTANTIATED, type.Format(outFunc->nameSpace).AddressOf());
  2189. else if( type.IsInterface() )
  2190. str.Format(TXT_INTERFACE_s_CANNOT_BE_INSTANTIATED, type.Format(outFunc->nameSpace).AddressOf());
  2191. else
  2192. // TODO: Improve error message to explain why
  2193. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format(outFunc->nameSpace).AddressOf());
  2194. Error(str, node);
  2195. // Don't continue, as it will most likely lead to further
  2196. // errors that may just mislead the script writer
  2197. return;
  2198. }
  2199. // A shared object may not declare variables of non-shared types
  2200. if( outFunc->IsShared() )
  2201. {
  2202. asCTypeInfo *ot = type.GetTypeInfo();
  2203. if( ot && !ot->IsShared() )
  2204. {
  2205. asCString msg;
  2206. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  2207. Error(msg, decl);
  2208. }
  2209. }
  2210. // Get the name of the identifier
  2211. asCString name(&script->code[node->tokenPos], node->tokenLength);
  2212. // Verify that the name isn't used by a dynamic data type
  2213. // TODO: Must check against registered funcdefs too
  2214. if( engine->GetRegisteredType(name.AddressOf(), outFunc->nameSpace) != 0 )
  2215. {
  2216. asCString str;
  2217. str.Format(TXT_ILLEGAL_VARIABLE_NAME_s, name.AddressOf());
  2218. Error(str, node);
  2219. }
  2220. int offset = AllocateVariable(type, false);
  2221. if( variables->DeclareVariable(name.AddressOf(), type, offset, IsVariableOnHeap(offset)) < 0 )
  2222. {
  2223. // TODO: It might be an out-of-memory too
  2224. asCString str;
  2225. str.Format(TXT_s_ALREADY_DECLARED, name.AddressOf());
  2226. Error(str, node);
  2227. // Don't continue after this error, as it will just
  2228. // lead to more errors that are likely false
  2229. return;
  2230. }
  2231. else
  2232. {
  2233. // Warn if this variable hides another variable in a higher scope
  2234. if( variables->parent && variables->parent->GetVariable(name.AddressOf()) )
  2235. {
  2236. asCString str;
  2237. str.Format(TXT_s_HIDES_VAR_IN_OUTER_SCOPE, name.AddressOf());
  2238. Warning(str, node);
  2239. }
  2240. }
  2241. // Add marker that the variable has been declared
  2242. bc->VarDecl((int)outFunc->scriptData->variables.GetLength());
  2243. outFunc->AddVariable(name, type, offset);
  2244. // Keep the node for the variable decl
  2245. asCScriptNode *varNode = node;
  2246. node = node->next;
  2247. if( node == 0 || node->nodeType == snIdentifier )
  2248. {
  2249. // Initialize with default constructor
  2250. CompileInitialization(0, bc, type, varNode, offset, 0, 0);
  2251. }
  2252. else
  2253. {
  2254. // Compile the initialization expression
  2255. asQWORD constantValue = 0;
  2256. if( CompileInitialization(node, bc, type, varNode, offset, &constantValue, 0, preCompiled ? &compiledCtx : 0) )
  2257. {
  2258. // Check if the variable should be marked as pure constant
  2259. if( type.IsPrimitive() && type.IsReadOnly() )
  2260. {
  2261. sVariable *v = variables->GetVariable(name.AddressOf());
  2262. v->isPureConstant = true;
  2263. v->constantValue = constantValue;
  2264. }
  2265. }
  2266. node = node->next;
  2267. }
  2268. }
  2269. bc->OptimizeLocally(tempVariableOffsets);
  2270. }
  2271. // Returns true if the initialization expression is a constant expression
  2272. bool asCCompiler::CompileInitialization(asCScriptNode *node, asCByteCode *bc, asCDataType &type, asCScriptNode *errNode, int offset, asQWORD *constantValue, int isVarGlobOrMem, asCExprContext *preCompiled)
  2273. {
  2274. bool isConstantExpression = false;
  2275. if( node && node->nodeType == snArgList )
  2276. {
  2277. // Make sure it is an object and not a handle
  2278. if( type.GetTypeInfo() == 0 || type.IsObjectHandle() )
  2279. {
  2280. Error(TXT_MUST_BE_OBJECT, node);
  2281. }
  2282. else
  2283. {
  2284. // Compile the arguments
  2285. asCArray<asCExprContext *> args;
  2286. asCArray<asSNamedArgument> namedArgs;
  2287. if( CompileArgumentList(node, args, namedArgs) >= 0 )
  2288. {
  2289. // Find all constructors
  2290. asCArray<int> funcs;
  2291. asSTypeBehaviour *beh = type.GetBehaviour();
  2292. if( beh )
  2293. {
  2294. if( type.GetTypeInfo()->flags & asOBJ_REF )
  2295. funcs = beh->factories;
  2296. else
  2297. funcs = beh->constructors;
  2298. }
  2299. asCString str = type.Format(outFunc->nameSpace);
  2300. MatchFunctions(funcs, args, node, str.AddressOf(), &namedArgs);
  2301. if( funcs.GetLength() == 1 )
  2302. {
  2303. // Add the default values for arguments not explicitly supplied
  2304. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(type.GetTypeInfo()), &namedArgs);
  2305. if( r == asSUCCESS )
  2306. {
  2307. asCExprContext ctx(engine);
  2308. if( type.GetTypeInfo() && (type.GetTypeInfo()->flags & asOBJ_REF) )
  2309. {
  2310. if( isVarGlobOrMem == 0 )
  2311. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, offset);
  2312. else
  2313. {
  2314. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  2315. ctx.bc.Instr(asBC_RDSPtr);
  2316. if( isVarGlobOrMem == 1 )
  2317. {
  2318. // Store the returned handle in the global variable
  2319. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2320. }
  2321. else
  2322. {
  2323. // Store the returned handle in the member
  2324. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2325. ctx.bc.Instr(asBC_RDSPtr);
  2326. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2327. }
  2328. if( type.IsFuncdef())
  2329. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  2330. else
  2331. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  2332. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2333. }
  2334. // Pop the reference left by the function call
  2335. ctx.bc.Instr(asBC_PopPtr);
  2336. }
  2337. else
  2338. {
  2339. bool onHeap = false;
  2340. if( isVarGlobOrMem == 0 )
  2341. {
  2342. // When the object is allocated on the heap, the address where the
  2343. // reference will be stored must be pushed on the stack before the
  2344. // arguments. This reference on the stack is safe, even if the script
  2345. // is suspended during the evaluation of the arguments.
  2346. onHeap = IsVariableOnHeap(offset);
  2347. if( onHeap )
  2348. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2349. }
  2350. else if( isVarGlobOrMem == 1 )
  2351. {
  2352. // Push the address of the location where the variable will be stored on the stack.
  2353. // This reference is safe, because the addresses of the global variables cannot change.
  2354. onHeap = true;
  2355. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2356. }
  2357. else
  2358. {
  2359. // Value types may be allocated inline if they are POD types
  2360. onHeap = !(type.IsObject() || type.IsFuncdef()) || type.IsReference() || (type.GetTypeInfo()->flags & asOBJ_REF);
  2361. if( onHeap )
  2362. {
  2363. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2364. ctx.bc.Instr(asBC_RDSPtr);
  2365. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2366. }
  2367. }
  2368. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  2369. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  2370. // When the object is allocated on the stack, the address to the
  2371. // object is pushed on the stack after the arguments as the object pointer
  2372. if( !onHeap )
  2373. {
  2374. if( isVarGlobOrMem == 2 )
  2375. {
  2376. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2377. ctx.bc.Instr(asBC_RDSPtr);
  2378. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2379. }
  2380. else
  2381. {
  2382. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2383. }
  2384. }
  2385. PerformFunctionCall(funcs[0], &ctx, onHeap, &args, CastToObjectType(type.GetTypeInfo()));
  2386. if( isVarGlobOrMem == 0 )
  2387. {
  2388. // Mark the object in the local variable as initialized
  2389. ctx.bc.ObjInfo(offset, asOBJ_INIT);
  2390. }
  2391. }
  2392. bc->AddCode(&ctx.bc);
  2393. }
  2394. }
  2395. }
  2396. // Cleanup
  2397. for( asUINT n = 0; n < args.GetLength(); n++ )
  2398. if( args[n] )
  2399. {
  2400. asDELETE(args[n], asCExprContext);
  2401. }
  2402. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  2403. if( namedArgs[n].ctx )
  2404. {
  2405. asDELETE(namedArgs[n].ctx, asCExprContext);
  2406. }
  2407. }
  2408. }
  2409. else if( node && node->nodeType == snInitList )
  2410. {
  2411. asCExprValue ti;
  2412. ti.Set(type);
  2413. ti.isVariable = (isVarGlobOrMem == 0);
  2414. ti.isTemporary = false;
  2415. ti.stackOffset = (short)offset;
  2416. ti.isLValue = true;
  2417. CompileInitList(&ti, node, bc, isVarGlobOrMem);
  2418. }
  2419. else if( node && node->nodeType == snAssignment )
  2420. {
  2421. // Compile the expression
  2422. asCExprContext newExpr(engine);
  2423. asCExprContext* expr;
  2424. int r = 0;
  2425. if( preCompiled )
  2426. {
  2427. expr = preCompiled;
  2428. }
  2429. else
  2430. {
  2431. expr = &newExpr;
  2432. r = CompileAssignment(node, expr);
  2433. }
  2434. // Look for appropriate constructor
  2435. asCArray<int> funcs;
  2436. asCArray<asCExprContext *> args;
  2437. // Handles must use the handle assignment operation.
  2438. // Types that are ASHANDLE must not allow the use of the constructor in this case,
  2439. // because it is ambiguous whether a value assignment or handle assignment will be done.
  2440. // Only do this if the expression is of the same type, as the expression is an assignment
  2441. // and an initialization constructor may not have the same meaning.
  2442. // TODO: Should allow initialization constructor if it is declared as allowed for implicit conversions.
  2443. if( !type.IsObjectHandle() && !expr->type.isExplicitHandle &&
  2444. !(type.GetTypeInfo() && (type.GetTypeInfo()->GetFlags() & asOBJ_ASHANDLE)) &&
  2445. type.IsEqualExceptRefAndConst(expr->type.dataType) )
  2446. {
  2447. asSTypeBehaviour *beh = type.GetBehaviour();
  2448. if( beh )
  2449. {
  2450. if( type.GetTypeInfo()->flags & asOBJ_REF )
  2451. funcs = beh->factories;
  2452. else
  2453. funcs = beh->constructors;
  2454. }
  2455. asCString str = type.Format(outFunc->nameSpace);
  2456. args.PushLast(expr);
  2457. MatchFunctions(funcs, args, node, str.AddressOf(), 0, 0, 0, true);
  2458. // Make sure the argument is of the right type (and not just compatible with the expression)
  2459. if (funcs.GetLength() == 1)
  2460. {
  2461. asCScriptFunction *f = engine->scriptFunctions[funcs[0]];
  2462. if (!f->parameterTypes[0].IsEqualExceptRefAndConst(expr->type.dataType))
  2463. funcs.PopLast();
  2464. }
  2465. }
  2466. if( funcs.GetLength() == 1 )
  2467. {
  2468. // Use the constructor
  2469. // TODO: clean-up: A large part of this is identical to the initalization with argList above
  2470. // Add the default values for arguments not explicitly supplied
  2471. r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(type.GetTypeInfo()));
  2472. if( r == asSUCCESS )
  2473. {
  2474. asCExprContext ctx(engine);
  2475. if( type.GetTypeInfo() && (type.GetTypeInfo()->flags & asOBJ_REF) )
  2476. {
  2477. if( isVarGlobOrMem == 0 )
  2478. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, offset);
  2479. else
  2480. {
  2481. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  2482. ctx.bc.Instr(asBC_RDSPtr);
  2483. if( isVarGlobOrMem == 1 )
  2484. {
  2485. // Store the returned handle in the global variable
  2486. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2487. }
  2488. else
  2489. {
  2490. // Store the returned handle in the member
  2491. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2492. ctx.bc.Instr(asBC_RDSPtr);
  2493. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2494. }
  2495. if( type.IsFuncdef() )
  2496. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  2497. else
  2498. ctx.bc.InstrPTR(asBC_REFCPY, type.GetTypeInfo());
  2499. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2500. }
  2501. // Pop the reference left by the function call
  2502. ctx.bc.Instr(asBC_PopPtr);
  2503. }
  2504. else
  2505. {
  2506. bool onHeap = false;
  2507. if( isVarGlobOrMem == 0 )
  2508. {
  2509. // When the object is allocated on the heap, the address where the
  2510. // reference will be stored must be pushed on the stack before the
  2511. // arguments. This reference on the stack is safe, even if the script
  2512. // is suspended during the evaluation of the arguments.
  2513. onHeap = IsVariableOnHeap(offset);
  2514. if( onHeap )
  2515. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2516. }
  2517. else if( isVarGlobOrMem == 1 )
  2518. {
  2519. // Push the address of the location where the variable will be stored on the stack.
  2520. // This reference is safe, because the addresses of the global variables cannot change.
  2521. onHeap = true;
  2522. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2523. }
  2524. else
  2525. {
  2526. // Value types may be allocated inline if they are POD types
  2527. onHeap = !(type.IsObject() || type.IsFuncdef()) || type.IsReference() || (type.GetTypeInfo()->flags & asOBJ_REF);
  2528. if( onHeap )
  2529. {
  2530. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2531. ctx.bc.Instr(asBC_RDSPtr);
  2532. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2533. }
  2534. }
  2535. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  2536. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  2537. // When the object is allocated on the stack, the address to the
  2538. // object is pushed on the stack after the arguments as the object pointer
  2539. if( !onHeap )
  2540. {
  2541. if( isVarGlobOrMem == 2 )
  2542. {
  2543. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2544. ctx.bc.Instr(asBC_RDSPtr);
  2545. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2546. }
  2547. else
  2548. {
  2549. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  2550. }
  2551. }
  2552. PerformFunctionCall(funcs[0], &ctx, onHeap, &args, CastToObjectType(type.GetTypeInfo()));
  2553. if( isVarGlobOrMem == 0 )
  2554. {
  2555. // Mark the object in the local variable as initialized
  2556. ctx.bc.ObjInfo(offset, asOBJ_INIT);
  2557. }
  2558. }
  2559. bc->AddCode(&ctx.bc);
  2560. }
  2561. }
  2562. else
  2563. {
  2564. // Call the default constructur, then call the assignment operator
  2565. asCExprContext ctx(engine);
  2566. // Call the default constructor here
  2567. if( isVarGlobOrMem == 0 )
  2568. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), &ctx.bc, errNode);
  2569. else if( isVarGlobOrMem == 1 )
  2570. CallDefaultConstructor(type, offset, true, &ctx.bc, errNode, isVarGlobOrMem);
  2571. else if( isVarGlobOrMem == 2 )
  2572. CallDefaultConstructor(type, offset, type.IsReference(), &ctx.bc, errNode, isVarGlobOrMem);
  2573. if( r >= 0 )
  2574. {
  2575. if( type.IsPrimitive() )
  2576. {
  2577. if( type.IsReadOnly() && expr->type.isConstant )
  2578. {
  2579. ImplicitConversion(expr, type, node, asIC_IMPLICIT_CONV);
  2580. // Tell caller that the expression is a constant so it can mark the variable as pure constant
  2581. isConstantExpression = true;
  2582. *constantValue = expr->type.GetConstantData();
  2583. }
  2584. asCExprContext lctx(engine);
  2585. if( isVarGlobOrMem == 0 )
  2586. lctx.type.SetVariable(type, offset, false);
  2587. else if( isVarGlobOrMem == 1 )
  2588. {
  2589. lctx.type.Set(type);
  2590. lctx.type.dataType.MakeReference(true);
  2591. // If it is an enum value, i.e. offset is negative, that is being compiled then
  2592. // we skip this as the bytecode won't be used anyway, only the constant value
  2593. if( offset >= 0 )
  2594. lctx.bc.InstrPTR(asBC_LDG, engine->globalProperties[offset]->GetAddressOfValue());
  2595. }
  2596. else
  2597. {
  2598. asASSERT( isVarGlobOrMem == 2 );
  2599. lctx.type.Set(type);
  2600. lctx.type.dataType.MakeReference(true);
  2601. // Load the reference of the primitive member into the register
  2602. lctx.bc.InstrSHORT(asBC_PSF, 0);
  2603. lctx.bc.Instr(asBC_RDSPtr);
  2604. lctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2605. lctx.bc.Instr(asBC_PopRPtr);
  2606. }
  2607. lctx.type.dataType.MakeReadOnly(false);
  2608. lctx.type.isLValue = true;
  2609. DoAssignment(&ctx, &lctx, expr, node, node, ttAssignment, node);
  2610. ProcessDeferredParams(&ctx);
  2611. }
  2612. else
  2613. {
  2614. // TODO: runtime optimize: Here we should look for the best matching constructor, instead of
  2615. // just the copy constructor. Only if no appropriate constructor is
  2616. // available should the assignment operator be used.
  2617. asCExprContext lexpr(engine);
  2618. lexpr.type.Set(type);
  2619. if( isVarGlobOrMem == 0 )
  2620. lexpr.type.dataType.MakeReference(IsVariableOnHeap(offset));
  2621. else if( isVarGlobOrMem == 1 )
  2622. lexpr.type.dataType.MakeReference(true);
  2623. else if( isVarGlobOrMem == 2 )
  2624. {
  2625. if( !lexpr.type.dataType.IsObject() || lexpr.type.dataType.IsFuncdef() || (lexpr.type.dataType.GetTypeInfo()->flags & asOBJ_REF) )
  2626. lexpr.type.dataType.MakeReference(true);
  2627. }
  2628. // Allow initialization of constant variables
  2629. lexpr.type.dataType.MakeReadOnly(false);
  2630. if( type.IsObjectHandle() )
  2631. lexpr.type.isExplicitHandle = true;
  2632. if( isVarGlobOrMem == 0 )
  2633. {
  2634. lexpr.bc.InstrSHORT(asBC_PSF, (short)offset);
  2635. lexpr.type.stackOffset = (short)offset;
  2636. lexpr.type.isVariable = true;
  2637. }
  2638. else if( isVarGlobOrMem == 1 )
  2639. {
  2640. lexpr.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2641. }
  2642. else
  2643. {
  2644. lexpr.bc.InstrSHORT(asBC_PSF, 0);
  2645. lexpr.bc.Instr(asBC_RDSPtr);
  2646. lexpr.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2647. lexpr.type.stackOffset = -1;
  2648. }
  2649. lexpr.type.isLValue = true;
  2650. // If left expression resolves into a registered type
  2651. // check if the assignment operator is overloaded, and check
  2652. // the type of the right hand expression. If none is found
  2653. // the default action is a direct copy if it is the same type
  2654. // and a simple assignment.
  2655. bool assigned = false;
  2656. // Even though an ASHANDLE can be an explicit handle the overloaded operator needs to be called
  2657. if( (lexpr.type.dataType.IsObject() || lexpr.type.dataType.IsFuncdef()) && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetTypeInfo() && (lexpr.type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE))) )
  2658. {
  2659. bool useHndlAssign = lexpr.type.dataType.IsHandleToAsHandleType();
  2660. assigned = CompileOverloadedDualOperator(node, &lexpr, expr, false, &ctx, useHndlAssign);
  2661. if( assigned )
  2662. {
  2663. // Pop the resulting value
  2664. if( !ctx.type.dataType.IsPrimitive() )
  2665. ctx.bc.Instr(asBC_PopPtr);
  2666. // Release the argument
  2667. ProcessDeferredParams(&ctx);
  2668. // Release temporary variable that may be allocated by the overloaded operator
  2669. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  2670. }
  2671. }
  2672. if( !assigned )
  2673. {
  2674. PrepareForAssignment(&lexpr.type.dataType, expr, node, false);
  2675. // If the expression is constant and the variable also is constant
  2676. // then mark the variable as pure constant. This will allow the compiler
  2677. // to optimize expressions with this variable.
  2678. if( type.IsReadOnly() && expr->type.isConstant )
  2679. {
  2680. isConstantExpression = true;
  2681. *constantValue = expr->type.GetConstantQW();
  2682. }
  2683. // Add expression code to bytecode
  2684. MergeExprBytecode(&ctx, expr);
  2685. // Add byte code for storing value of expression in variable
  2686. ctx.bc.AddCode(&lexpr.bc);
  2687. PerformAssignment(&lexpr.type, &expr->type, &ctx.bc, errNode);
  2688. // Release temporary variables used by expression
  2689. ReleaseTemporaryVariable(expr->type, &ctx.bc);
  2690. ctx.bc.Instr(asBC_PopPtr);
  2691. ProcessDeferredParams(&ctx);
  2692. }
  2693. }
  2694. }
  2695. bc->AddCode(&ctx.bc);
  2696. }
  2697. }
  2698. else
  2699. {
  2700. asASSERT( node == 0 );
  2701. // Call the default constructor here, as no explicit initialization is done
  2702. if( isVarGlobOrMem == 0 )
  2703. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), bc, errNode);
  2704. else if( isVarGlobOrMem == 1 )
  2705. CallDefaultConstructor(type, offset, true, bc, errNode, isVarGlobOrMem);
  2706. else if( isVarGlobOrMem == 2 )
  2707. {
  2708. if( !(type.IsObject() || type.IsFuncdef()) || type.IsReference() || (type.GetTypeInfo()->flags & asOBJ_REF) )
  2709. CallDefaultConstructor(type, offset, true, bc, errNode, isVarGlobOrMem);
  2710. else
  2711. CallDefaultConstructor(type, offset, false, bc, errNode, isVarGlobOrMem);
  2712. }
  2713. }
  2714. return isConstantExpression;
  2715. }
  2716. void asCCompiler::CompileInitList(asCExprValue *var, asCScriptNode *node, asCByteCode *bc, int isVarGlobOrMem)
  2717. {
  2718. // Check if the type supports initialization lists
  2719. if( var->dataType.GetTypeInfo() == 0 ||
  2720. var->dataType.GetBehaviour()->listFactory == 0 )
  2721. {
  2722. asCString str;
  2723. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, var->dataType.Format(outFunc->nameSpace).AddressOf());
  2724. Error(str, node);
  2725. return;
  2726. }
  2727. // Construct the buffer with the elements
  2728. // Find the list factory
  2729. int funcId = var->dataType.GetBehaviour()->listFactory;
  2730. asASSERT( engine->scriptFunctions[funcId]->listPattern );
  2731. // TODO: runtime optimize: A future optimization should be to use the stack space directly
  2732. // for small buffers so that the dynamic allocation is skipped
  2733. // Create a new special object type for the lists. Both asCRestore and the
  2734. // context exception handler will need this to know how to parse the buffer.
  2735. asCObjectType *listPatternType = engine->GetListPatternType(funcId);
  2736. // Allocate a temporary variable to hold the pointer to the buffer
  2737. int bufferVar = AllocateVariable(asCDataType::CreateType(listPatternType, false), true);
  2738. asUINT bufferSize = 0;
  2739. // Evaluate all elements of the list
  2740. asCExprContext valueExpr(engine);
  2741. asCScriptNode *el = node;
  2742. asSListPatternNode *patternNode = engine->scriptFunctions[listPatternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
  2743. int elementsInSubList = -1;
  2744. int r = CompileInitListElement(patternNode, el, engine->GetTypeIdFromDataType(asCDataType::CreateType(listPatternType, false)), short(bufferVar), bufferSize, valueExpr.bc, elementsInSubList);
  2745. asASSERT( r || patternNode == 0 );
  2746. UNUSED_VAR(r);
  2747. // After all values have been evaluated we know the final size of the buffer
  2748. asCExprContext allocExpr(engine);
  2749. allocExpr.bc.InstrSHORT_DW(asBC_AllocMem, short(bufferVar), bufferSize);
  2750. // Merge the bytecode into the final sequence
  2751. bc->AddCode(&allocExpr.bc);
  2752. bc->AddCode(&valueExpr.bc);
  2753. // The object itself is the last to be created and will receive the pointer to the buffer
  2754. asCArray<asCExprContext *> args;
  2755. asCExprContext arg1(engine);
  2756. arg1.type.Set(asCDataType::CreatePrimitive(ttUInt, false));
  2757. arg1.type.dataType.MakeReference(true);
  2758. arg1.bc.InstrSHORT(asBC_PshVPtr, short(bufferVar));
  2759. args.PushLast(&arg1);
  2760. asCExprContext ctx(engine);
  2761. if( var->isVariable )
  2762. {
  2763. asASSERT( isVarGlobOrMem == 0 );
  2764. if( var->dataType.GetTypeInfo()->GetFlags() & asOBJ_REF )
  2765. {
  2766. ctx.bc.AddCode(&arg1.bc);
  2767. // Call factory and store the handle in the given variable
  2768. PerformFunctionCall(funcId, &ctx, false, &args, 0, true, var->stackOffset);
  2769. ctx.bc.Instr(asBC_PopPtr);
  2770. }
  2771. else
  2772. {
  2773. // Call the constructor
  2774. // When the object is allocated on the heap, the address where the
  2775. // reference will be stored must be pushed on the stack before the
  2776. // arguments. This reference on the stack is safe, even if the script
  2777. // is suspended during the evaluation of the arguments.
  2778. bool onHeap = IsVariableOnHeap(var->stackOffset);
  2779. if( onHeap )
  2780. ctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  2781. ctx.bc.AddCode(&arg1.bc);
  2782. // When the object is allocated on the stack, the address to the
  2783. // object is pushed on the stack after the arguments as the object pointer
  2784. if( !onHeap )
  2785. ctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  2786. PerformFunctionCall(funcId, &ctx, onHeap, &args, CastToObjectType(var->dataType.GetTypeInfo()));
  2787. // Mark the object in the local variable as initialized
  2788. ctx.bc.ObjInfo(var->stackOffset, asOBJ_INIT);
  2789. }
  2790. }
  2791. else
  2792. {
  2793. if( var->dataType.GetTypeInfo()->GetFlags() & asOBJ_REF )
  2794. {
  2795. ctx.bc.AddCode(&arg1.bc);
  2796. PerformFunctionCall(funcId, &ctx, false, &args);
  2797. ctx.bc.Instr(asBC_RDSPtr);
  2798. if( isVarGlobOrMem == 1 )
  2799. {
  2800. // Store the returned handle in the global variable
  2801. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  2802. }
  2803. else
  2804. {
  2805. // Store the returned handle in the member
  2806. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2807. ctx.bc.Instr(asBC_RDSPtr);
  2808. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2809. }
  2810. if (var->dataType.IsFuncdef())
  2811. ctx.bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  2812. else
  2813. ctx.bc.InstrPTR(asBC_REFCPY, var->dataType.GetTypeInfo());
  2814. ctx.bc.Instr(asBC_PopPtr);
  2815. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2816. }
  2817. else
  2818. {
  2819. bool onHeap = true;
  2820. // Put the address where the object pointer will be placed on the stack
  2821. if( isVarGlobOrMem == 1 )
  2822. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  2823. else
  2824. {
  2825. onHeap = !(var->dataType.IsObject() || var->dataType.IsFuncdef()) || var->dataType.IsReference() || (var->dataType.GetTypeInfo()->flags & asOBJ_REF);
  2826. if( onHeap )
  2827. {
  2828. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2829. ctx.bc.Instr(asBC_RDSPtr);
  2830. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2831. }
  2832. }
  2833. // Add the address of the list buffer as the argument
  2834. ctx.bc.AddCode(&arg1.bc);
  2835. if( !onHeap )
  2836. {
  2837. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2838. ctx.bc.Instr(asBC_RDSPtr);
  2839. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(outFunc->objectType, false)));
  2840. }
  2841. // Call the ALLOC instruction to allocate memory and invoke constructor
  2842. PerformFunctionCall(funcId, &ctx, onHeap, &args, CastToObjectType(var->dataType.GetTypeInfo()));
  2843. }
  2844. }
  2845. bc->AddCode(&ctx.bc);
  2846. // Free the temporary buffer. The FREE instruction will make sure to destroy
  2847. // each element in the buffer so there is no need to do this manually
  2848. bc->InstrW_PTR(asBC_FREE, short(bufferVar), listPatternType);
  2849. ReleaseTemporaryVariable(bufferVar, bc);
  2850. }
  2851. int asCCompiler::CompileInitListElement(asSListPatternNode *&patternNode, asCScriptNode *&valueNode, int bufferTypeId, short bufferVar, asUINT &bufferSize, asCByteCode &bcInit, int &elementsInSubList)
  2852. {
  2853. if( patternNode->type == asLPT_START )
  2854. {
  2855. if( valueNode == 0 || valueNode->nodeType != snInitList )
  2856. {
  2857. Error(TXT_EXPECTED_LIST, valueNode);
  2858. return -1;
  2859. }
  2860. // Compile all values until asLPT_END
  2861. patternNode = patternNode->next;
  2862. asCScriptNode *node = valueNode->firstChild;
  2863. while( patternNode->type != asLPT_END )
  2864. {
  2865. // Check for missing value here, else the error reporting will not have a source position to report the error for
  2866. if( node == 0 && patternNode->type == asLPT_TYPE )
  2867. {
  2868. Error(TXT_NOT_ENOUGH_VALUES_FOR_LIST, valueNode);
  2869. return -1;
  2870. }
  2871. asCScriptNode *errNode = node;
  2872. int r = CompileInitListElement(patternNode, node, bufferTypeId, bufferVar, bufferSize, bcInit, elementsInSubList);
  2873. if( r < 0 ) return r;
  2874. if( r == 1 )
  2875. {
  2876. asASSERT( engine->ep.disallowEmptyListElements );
  2877. // Empty elements in the middle are not allowed
  2878. Error(TXT_EMPTY_LIST_ELEMENT_IS_NOT_ALLOWED, errNode);
  2879. }
  2880. asASSERT( patternNode );
  2881. }
  2882. if( node )
  2883. {
  2884. Error(TXT_TOO_MANY_VALUES_FOR_LIST, valueNode);
  2885. return -1;
  2886. }
  2887. // Move to the next node
  2888. valueNode = valueNode->next;
  2889. patternNode = patternNode->next;
  2890. }
  2891. else if( patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME )
  2892. {
  2893. // TODO: list: repeat_inner should make sure the list has the same size as the inner list, i.e. square area
  2894. // TODO: list: repeat_prev should make sure the list is the same size as the previous
  2895. asEListPatternNodeType repeatType = patternNode->type;
  2896. asCScriptNode *firstValue = valueNode;
  2897. // The following values will be repeated N times
  2898. patternNode = patternNode->next;
  2899. // Keep track of the patternNode so it can be reset
  2900. asSListPatternNode *nextNode = patternNode;
  2901. // Align the buffer size to 4 bytes in case previous value was smaller than 4 bytes
  2902. if( bufferSize & 0x3 )
  2903. bufferSize += 4 - (bufferSize & 0x3);
  2904. // The first dword will hold the number of elements in the list
  2905. asDWORD currSize = bufferSize;
  2906. bufferSize += 4;
  2907. asUINT countElements = 0;
  2908. int elementsInSubSubList = -1;
  2909. asCExprContext ctx(engine);
  2910. while( valueNode )
  2911. {
  2912. patternNode = nextNode;
  2913. asCScriptNode *errNode = valueNode;
  2914. int r = CompileInitListElement(patternNode, valueNode, bufferTypeId, bufferVar, bufferSize, ctx.bc, elementsInSubSubList);
  2915. if( r < 0 ) return r;
  2916. if( r == 0 )
  2917. countElements++;
  2918. else
  2919. {
  2920. asASSERT( r == 1 && engine->ep.disallowEmptyListElements );
  2921. if( valueNode )
  2922. {
  2923. // Empty elements in the middle are not allowed
  2924. Error(TXT_EMPTY_LIST_ELEMENT_IS_NOT_ALLOWED, errNode);
  2925. }
  2926. }
  2927. }
  2928. if( countElements == 0 )
  2929. {
  2930. // Skip the sub pattern that was expected to be repeated, otherwise the caller will try to match these when we return
  2931. patternNode = nextNode;
  2932. if( patternNode->type == asLPT_TYPE )
  2933. patternNode = patternNode->next;
  2934. else if( patternNode->type == asLPT_START )
  2935. {
  2936. int subCount = 1;
  2937. do
  2938. {
  2939. patternNode = patternNode->next;
  2940. if( patternNode->type == asLPT_START )
  2941. subCount++;
  2942. else if( patternNode->type == asLPT_END )
  2943. subCount--;
  2944. } while( subCount > 0 );
  2945. patternNode = patternNode->next;
  2946. }
  2947. }
  2948. // For repeat_same each repeated sublist must have the same size to form a rectangular array
  2949. if( repeatType == asLPT_REPEAT_SAME && elementsInSubList != -1 && asUINT(elementsInSubList) != countElements )
  2950. {
  2951. if( countElements < asUINT(elementsInSubList) )
  2952. Error(TXT_NOT_ENOUGH_VALUES_FOR_LIST, firstValue);
  2953. else
  2954. Error(TXT_TOO_MANY_VALUES_FOR_LIST, firstValue);
  2955. return -1;
  2956. }
  2957. else
  2958. {
  2959. // Return to caller the amount of elments in this sublist
  2960. elementsInSubList = countElements;
  2961. }
  2962. // The first dword in the buffer will hold the number of elements
  2963. bcInit.InstrSHORT_DW_DW(asBC_SetListSize, bufferVar, currSize, countElements);
  2964. // Add the values
  2965. bcInit.AddCode(&ctx.bc);
  2966. }
  2967. else if( patternNode->type == asLPT_TYPE )
  2968. {
  2969. bool isEmpty = false;
  2970. // Determine the size of the element
  2971. asUINT size = 0;
  2972. asCDataType dt = reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType;
  2973. if( valueNode->nodeType == snAssignment || valueNode->nodeType == snInitList )
  2974. {
  2975. asCExprContext lctx(engine);
  2976. asCExprContext rctx(engine);
  2977. if( valueNode->nodeType == snAssignment )
  2978. {
  2979. // Compile the assignment expression
  2980. CompileAssignment(valueNode, &rctx);
  2981. if( dt.GetTokenType() == ttQuestion )
  2982. {
  2983. // Make sure the type is not ambiguous
  2984. DetermineSingleFunc(&rctx, valueNode);
  2985. // We now know the type
  2986. dt = rctx.type.dataType;
  2987. dt.MakeReadOnly(false);
  2988. dt.MakeReference(false);
  2989. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  2990. if( bufferSize & 0x3 )
  2991. bufferSize += 4 - (bufferSize & 0x3);
  2992. // Place the type id in the buffer
  2993. bcInit.InstrSHORT_DW_DW(asBC_SetListType, bufferVar, bufferSize, engine->GetTypeIdFromDataType(dt));
  2994. bufferSize += 4;
  2995. }
  2996. }
  2997. else if( valueNode->nodeType == snInitList )
  2998. {
  2999. if( dt.GetTokenType() == ttQuestion )
  3000. {
  3001. // Can't use init lists with var type as it is not possible to determine what type should be allocated
  3002. asCString str;
  3003. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, "?");
  3004. Error(str.AddressOf(), valueNode);
  3005. rctx.type.SetDummy();
  3006. dt = rctx.type.dataType;
  3007. }
  3008. else
  3009. {
  3010. // Allocate a temporary variable that will be initialized with the list
  3011. int offset = AllocateVariable(dt, true);
  3012. rctx.type.Set(dt);
  3013. rctx.type.isVariable = true;
  3014. rctx.type.isTemporary = true;
  3015. rctx.type.stackOffset = (short)offset;
  3016. CompileInitList(&rctx.type, valueNode, &rctx.bc, 0);
  3017. // Put the object on the stack
  3018. rctx.bc.InstrSHORT(asBC_PSF, rctx.type.stackOffset);
  3019. // It is a reference that we place on the stack
  3020. rctx.type.dataType.MakeReference(true);
  3021. }
  3022. }
  3023. // Determine size of the element
  3024. if( dt.IsPrimitive() || (!dt.IsNullHandle() && (dt.GetTypeInfo()->flags & asOBJ_VALUE)) )
  3025. size = dt.GetSizeInMemoryBytes();
  3026. else
  3027. size = AS_PTR_SIZE*4;
  3028. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3029. if( size >= 4 && (bufferSize & 0x3) )
  3030. bufferSize += 4 - (bufferSize & 0x3);
  3031. // Compile the lvalue
  3032. lctx.bc.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3033. lctx.type.Set(dt);
  3034. lctx.type.isLValue = true;
  3035. if( dt.IsPrimitive() )
  3036. {
  3037. lctx.bc.Instr(asBC_PopRPtr);
  3038. lctx.type.dataType.MakeReference(true);
  3039. }
  3040. else if( dt.IsObjectHandle() ||
  3041. dt.GetTypeInfo()->flags & asOBJ_REF )
  3042. {
  3043. lctx.type.isExplicitHandle = true;
  3044. lctx.type.dataType.MakeReference(true);
  3045. }
  3046. else
  3047. {
  3048. asASSERT( dt.GetTypeInfo()->flags & asOBJ_VALUE );
  3049. // Make sure the object has been constructed before the assignment
  3050. // TODO: runtime optimize: Use copy constructor instead of assignment to initialize the objects
  3051. asSTypeBehaviour *beh = dt.GetBehaviour();
  3052. int func = 0;
  3053. if( beh ) func = beh->construct;
  3054. if( func == 0 && (dt.GetTypeInfo()->flags & asOBJ_POD) == 0 )
  3055. {
  3056. asCString str;
  3057. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetTypeInfo()->GetName());
  3058. Error(str, valueNode);
  3059. }
  3060. else if( func )
  3061. {
  3062. // Call the constructor as a normal function
  3063. bcInit.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3064. asCExprContext ctx(engine);
  3065. PerformFunctionCall(func, &ctx, false, 0, CastToObjectType(dt.GetTypeInfo()));
  3066. bcInit.AddCode(&ctx.bc);
  3067. }
  3068. }
  3069. if( lctx.type.dataType.IsNullHandle() )
  3070. {
  3071. // Don't add any code to assign a null handle. RefCpy doesn't work without a known type.
  3072. // The buffer is already initialized to zero in asBC_AllocMem anyway.
  3073. asASSERT( rctx.bc.GetLastInstr() == asBC_PshNull );
  3074. asASSERT( reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType.GetTokenType() == ttQuestion );
  3075. }
  3076. else
  3077. {
  3078. asCExprContext ctx(engine);
  3079. DoAssignment(&ctx, &lctx, &rctx, valueNode, valueNode, ttAssignment, valueNode);
  3080. if( !lctx.type.dataType.IsPrimitive() )
  3081. ctx.bc.Instr(asBC_PopPtr);
  3082. // Release temporary variables used by expression
  3083. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  3084. ProcessDeferredParams(&ctx);
  3085. bcInit.AddCode(&ctx.bc);
  3086. }
  3087. }
  3088. else
  3089. {
  3090. if( builder->engine->ep.disallowEmptyListElements )
  3091. {
  3092. // Empty elements are not allowed, except if it is the last in the list
  3093. isEmpty = true;
  3094. }
  3095. else
  3096. {
  3097. // There is no specific value so we need to fill it with a default value
  3098. if( dt.GetTokenType() == ttQuestion )
  3099. {
  3100. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3101. if( bufferSize & 0x3 )
  3102. bufferSize += 4 - (bufferSize & 0x3);
  3103. // Place the type id for a null handle in the buffer
  3104. bcInit.InstrSHORT_DW_DW(asBC_SetListType, bufferVar, bufferSize, 0);
  3105. bufferSize += 4;
  3106. dt = asCDataType::CreateNullHandle();
  3107. // No need to initialize the handle as the buffer is already initialized with zeroes
  3108. }
  3109. else if( dt.GetTypeInfo() && dt.GetTypeInfo()->flags & asOBJ_VALUE )
  3110. {
  3111. // For value types with default constructor we need to call the constructor
  3112. asSTypeBehaviour *beh = dt.GetBehaviour();
  3113. int func = 0;
  3114. if( beh ) func = beh->construct;
  3115. if( func == 0 && (dt.GetTypeInfo()->flags & asOBJ_POD) == 0 )
  3116. {
  3117. asCString str;
  3118. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetTypeInfo()->GetName());
  3119. Error(str, valueNode);
  3120. }
  3121. else if( func )
  3122. {
  3123. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3124. if( bufferSize & 0x3 )
  3125. bufferSize += 4 - (bufferSize & 0x3);
  3126. // Call the constructor as a normal function
  3127. bcInit.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3128. asCExprContext ctx(engine);
  3129. PerformFunctionCall(func, &ctx, false, 0, CastToObjectType(dt.GetTypeInfo()));
  3130. bcInit.AddCode(&ctx.bc);
  3131. }
  3132. }
  3133. else if( !dt.IsObjectHandle() && dt.GetTypeInfo() && dt.GetTypeInfo()->flags & asOBJ_REF )
  3134. {
  3135. // For ref types (not handles) we need to call the default factory
  3136. asSTypeBehaviour *beh = dt.GetBehaviour();
  3137. int func = 0;
  3138. if( beh ) func = beh->factory;
  3139. if( func == 0 )
  3140. {
  3141. asCString str;
  3142. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetTypeInfo()->GetName());
  3143. Error(str, valueNode);
  3144. }
  3145. else if( func )
  3146. {
  3147. asCExprContext rctx(engine);
  3148. PerformFunctionCall(func, &rctx, false, 0, CastToObjectType(dt.GetTypeInfo()));
  3149. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  3150. if( bufferSize & 0x3 )
  3151. bufferSize += 4 - (bufferSize & 0x3);
  3152. asCExprContext lctx(engine);
  3153. lctx.bc.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  3154. lctx.type.Set(dt);
  3155. lctx.type.isLValue = true;
  3156. lctx.type.isExplicitHandle = true;
  3157. lctx.type.dataType.MakeReference(true);
  3158. asCExprContext ctx(engine);
  3159. DoAssignment(&ctx, &lctx, &rctx, valueNode, valueNode, ttAssignment, valueNode);
  3160. if( !lctx.type.dataType.IsPrimitive() )
  3161. ctx.bc.Instr(asBC_PopPtr);
  3162. // Release temporary variables used by expression
  3163. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  3164. ProcessDeferredParams(&ctx);
  3165. bcInit.AddCode(&ctx.bc);
  3166. }
  3167. }
  3168. }
  3169. }
  3170. if( !isEmpty )
  3171. {
  3172. // Determine size of the element
  3173. if( dt.IsPrimitive() || (!dt.IsNullHandle() && (dt.GetTypeInfo()->flags & asOBJ_VALUE)) )
  3174. size = dt.GetSizeInMemoryBytes();
  3175. else
  3176. size = AS_PTR_SIZE*4;
  3177. asASSERT( size <= 4 || (size & 0x3) == 0 );
  3178. bufferSize += size;
  3179. }
  3180. // Move to the next element
  3181. patternNode = patternNode->next;
  3182. valueNode = valueNode->next;
  3183. if( isEmpty )
  3184. {
  3185. // The caller will determine if the empty element should be ignored or not
  3186. return 1;
  3187. }
  3188. }
  3189. else
  3190. asASSERT( false );
  3191. return 0;
  3192. }
  3193. void asCCompiler::CompileStatement(asCScriptNode *statement, bool *hasReturn, asCByteCode *bc)
  3194. {
  3195. // Don't clear the hasReturn flag if this is an empty statement
  3196. // to avoid false errors of 'not all paths return'
  3197. if( statement->nodeType != snExpressionStatement || statement->firstChild )
  3198. *hasReturn = false;
  3199. if( statement->nodeType == snStatementBlock )
  3200. CompileStatementBlock(statement, true, hasReturn, bc);
  3201. else if( statement->nodeType == snIf )
  3202. CompileIfStatement(statement, hasReturn, bc);
  3203. else if( statement->nodeType == snFor )
  3204. CompileForStatement(statement, bc);
  3205. else if( statement->nodeType == snWhile )
  3206. CompileWhileStatement(statement, bc);
  3207. else if( statement->nodeType == snDoWhile )
  3208. CompileDoWhileStatement(statement, bc);
  3209. else if( statement->nodeType == snExpressionStatement )
  3210. CompileExpressionStatement(statement, bc);
  3211. else if( statement->nodeType == snBreak )
  3212. CompileBreakStatement(statement, bc);
  3213. else if( statement->nodeType == snContinue )
  3214. CompileContinueStatement(statement, bc);
  3215. else if( statement->nodeType == snSwitch )
  3216. CompileSwitchStatement(statement, hasReturn, bc);
  3217. else if( statement->nodeType == snReturn )
  3218. {
  3219. CompileReturnStatement(statement, bc);
  3220. *hasReturn = true;
  3221. }
  3222. }
  3223. void asCCompiler::CompileSwitchStatement(asCScriptNode *snode, bool *, asCByteCode *bc)
  3224. {
  3225. // TODO: inheritance: Must guarantee that all options in the switch case call a constructor, or that none call it.
  3226. // Reserve label for break statements
  3227. int breakLabel = nextLabel++;
  3228. breakLabels.PushLast(breakLabel);
  3229. // Add a variable scope that will be used by CompileBreak
  3230. // to know where to stop deallocating variables
  3231. AddVariableScope(true, false);
  3232. //---------------------------
  3233. // Compile the switch expression
  3234. //-------------------------------
  3235. // Compile the switch expression
  3236. asCExprContext expr(engine);
  3237. CompileAssignment(snode->firstChild, &expr);
  3238. // Verify that the expression is a primitive type
  3239. if( !expr.type.dataType.IsIntegerType() && !expr.type.dataType.IsUnsignedType() )
  3240. {
  3241. Error(TXT_SWITCH_MUST_BE_INTEGRAL, snode->firstChild);
  3242. return;
  3243. }
  3244. ProcessPropertyGetAccessor(&expr, snode);
  3245. // TODO: Need to support 64bit integers
  3246. // Convert the expression to a 32bit variable
  3247. asCDataType to;
  3248. if( expr.type.dataType.IsIntegerType() )
  3249. to.SetTokenType(ttInt);
  3250. else if( expr.type.dataType.IsUnsignedType() )
  3251. to.SetTokenType(ttUInt);
  3252. // Make sure the value is in a variable
  3253. if( expr.type.dataType.IsReference() )
  3254. ConvertToVariable(&expr);
  3255. ImplicitConversion(&expr, to, snode->firstChild, asIC_IMPLICIT_CONV, true);
  3256. ConvertToVariable(&expr);
  3257. int offset = expr.type.stackOffset;
  3258. ProcessDeferredParams(&expr);
  3259. //-------------------------------
  3260. // Determine case values and labels
  3261. //--------------------------------
  3262. // Remember the first label so that we can later pass the
  3263. // correct label to each CompileCase()
  3264. int firstCaseLabel = nextLabel;
  3265. int defaultLabel = 0;
  3266. asCArray<int> caseValues;
  3267. asCArray<int> caseLabels;
  3268. // Compile all case comparisons and make them jump to the right label
  3269. asCScriptNode *cnode = snode->firstChild->next;
  3270. while( cnode )
  3271. {
  3272. // Each case should have a constant expression
  3273. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  3274. {
  3275. // Compile expression
  3276. asCExprContext c(engine);
  3277. CompileExpression(cnode->firstChild, &c);
  3278. // Verify that the result is a constant
  3279. if( !c.type.isConstant )
  3280. Error(TXT_SWITCH_CASE_MUST_BE_CONSTANT, cnode->firstChild);
  3281. // Verify that the result is an integral number
  3282. if (!c.type.dataType.IsIntegerType() && !c.type.dataType.IsUnsignedType())
  3283. Error(TXT_SWITCH_MUST_BE_INTEGRAL, cnode->firstChild);
  3284. else
  3285. {
  3286. ImplicitConversion(&c, to, cnode->firstChild, asIC_IMPLICIT_CONV, true);
  3287. // Has this case been declared already?
  3288. if (caseValues.IndexOf(c.type.GetConstantDW()) >= 0)
  3289. Error(TXT_DUPLICATE_SWITCH_CASE, cnode->firstChild);
  3290. // TODO: Optimize: We can insert the numbers sorted already
  3291. // Store constant for later use
  3292. caseValues.PushLast(c.type.GetConstantDW());
  3293. // Reserve label for this case
  3294. caseLabels.PushLast(nextLabel++);
  3295. }
  3296. }
  3297. else
  3298. {
  3299. // TODO: It shouldn't be necessary for the default case to be the last one.
  3300. // Is default the last case?
  3301. if( cnode->next )
  3302. {
  3303. Error(TXT_DEFAULT_MUST_BE_LAST, cnode);
  3304. break;
  3305. }
  3306. // Reserve label for this case
  3307. defaultLabel = nextLabel++;
  3308. }
  3309. cnode = cnode->next;
  3310. }
  3311. // check for empty switch
  3312. if (caseValues.GetLength() == 0)
  3313. {
  3314. Error(TXT_EMPTY_SWITCH, snode);
  3315. return;
  3316. }
  3317. if( defaultLabel == 0 )
  3318. defaultLabel = breakLabel;
  3319. //---------------------------------
  3320. // Output the optimized case comparisons
  3321. // with jumps to the case code
  3322. //------------------------------------
  3323. // Sort the case values by increasing value. Do the sort together with the labels
  3324. // A simple bubble sort is sufficient since we don't expect a huge number of values
  3325. for( asUINT fwd = 1; fwd < caseValues.GetLength(); fwd++ )
  3326. {
  3327. for( int bck = fwd - 1; bck >= 0; bck-- )
  3328. {
  3329. int bckp = bck + 1;
  3330. if( caseValues[bck] > caseValues[bckp] )
  3331. {
  3332. // Swap the values in both arrays
  3333. int swap = caseValues[bckp];
  3334. caseValues[bckp] = caseValues[bck];
  3335. caseValues[bck] = swap;
  3336. swap = caseLabels[bckp];
  3337. caseLabels[bckp] = caseLabels[bck];
  3338. caseLabels[bck] = swap;
  3339. }
  3340. else
  3341. break;
  3342. }
  3343. }
  3344. // Find ranges of consecutive numbers
  3345. asCArray<int> ranges;
  3346. ranges.PushLast(0);
  3347. asUINT n;
  3348. for( n = 1; n < caseValues.GetLength(); ++n )
  3349. {
  3350. // We can join numbers that are less than 5 numbers
  3351. // apart since the output code will still be smaller
  3352. if( caseValues[n] > caseValues[n-1] + 5 )
  3353. ranges.PushLast(n);
  3354. }
  3355. // If the value is larger than the largest case value, jump to default
  3356. int tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3357. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[caseValues.GetLength()-1]);
  3358. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3359. expr.bc.InstrDWORD(asBC_JP, defaultLabel);
  3360. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3361. // TODO: runtime optimize: We could possibly optimize this even more by doing a
  3362. // binary search instead of a linear search through the ranges
  3363. // For each range
  3364. int range;
  3365. for( range = 0; range < (int)ranges.GetLength(); range++ )
  3366. {
  3367. // Find the largest value in this range
  3368. int maxRange = caseValues[ranges[range]];
  3369. int index = ranges[range];
  3370. for( ; (index < (int)caseValues.GetLength()) && (caseValues[index] <= maxRange + 5); index++ )
  3371. maxRange = caseValues[index];
  3372. // If there are only 2 numbers then it is better to compare them directly
  3373. if( index - ranges[range] > 2 )
  3374. {
  3375. // If the value is smaller than the smallest case value in the range, jump to default
  3376. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3377. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  3378. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3379. expr.bc.InstrDWORD(asBC_JS, defaultLabel);
  3380. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3381. int nextRangeLabel = nextLabel++;
  3382. // If this is the last range we don't have to make this test
  3383. if( range < (int)ranges.GetLength() - 1 )
  3384. {
  3385. // If the value is larger than the largest case value in the range, jump to the next range
  3386. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3387. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, maxRange);
  3388. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3389. expr.bc.InstrDWORD(asBC_JP, nextRangeLabel);
  3390. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3391. }
  3392. // Jump forward according to the value
  3393. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3394. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  3395. expr.bc.InstrW_W_W(asBC_SUBi, tmpOffset, offset, tmpOffset);
  3396. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3397. expr.bc.JmpP(tmpOffset, maxRange - caseValues[ranges[range]]);
  3398. // Add the list of jumps to the correct labels (any holes, jump to default)
  3399. index = ranges[range];
  3400. for( int i = caseValues[index]; i <= maxRange; i++ )
  3401. {
  3402. if( caseValues[index] == i )
  3403. expr.bc.InstrINT(asBC_JMP, caseLabels[index++]);
  3404. else
  3405. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  3406. }
  3407. expr.bc.Label((short)nextRangeLabel);
  3408. }
  3409. else
  3410. {
  3411. // Simply make a comparison with each value
  3412. for( int i = ranges[range]; i < index; ++i )
  3413. {
  3414. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  3415. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[i]);
  3416. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  3417. expr.bc.InstrDWORD(asBC_JZ, caseLabels[i]);
  3418. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  3419. }
  3420. }
  3421. }
  3422. // Catch any value that falls trough
  3423. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  3424. // Release the temporary variable previously stored
  3425. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3426. // TODO: optimize: Should optimize each piece individually
  3427. expr.bc.OptimizeLocally(tempVariableOffsets);
  3428. //----------------------------------
  3429. // Output case implementations
  3430. //----------------------------------
  3431. // Compile case implementations, each one with the label before it
  3432. cnode = snode->firstChild->next;
  3433. while( cnode )
  3434. {
  3435. // Each case should have a constant expression
  3436. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  3437. {
  3438. expr.bc.Label((short)firstCaseLabel++);
  3439. CompileCase(cnode->firstChild->next, &expr.bc);
  3440. }
  3441. else
  3442. {
  3443. expr.bc.Label((short)defaultLabel);
  3444. // Is default the last case?
  3445. if( cnode->next )
  3446. {
  3447. // We've already reported this error
  3448. break;
  3449. }
  3450. CompileCase(cnode->firstChild, &expr.bc);
  3451. }
  3452. cnode = cnode->next;
  3453. }
  3454. //--------------------------------
  3455. bc->AddCode(&expr.bc);
  3456. // Add break label
  3457. bc->Label((short)breakLabel);
  3458. breakLabels.PopLast();
  3459. RemoveVariableScope();
  3460. }
  3461. void asCCompiler::CompileCase(asCScriptNode *node, asCByteCode *bc)
  3462. {
  3463. bool isFinished = false;
  3464. bool hasReturn = false;
  3465. bool hasUnreachableCode = false;
  3466. while( node )
  3467. {
  3468. if( !hasUnreachableCode && (hasReturn || isFinished) )
  3469. {
  3470. hasUnreachableCode = true;
  3471. Warning(TXT_UNREACHABLE_CODE, node);
  3472. break;
  3473. }
  3474. if( node->nodeType == snBreak || node->nodeType == snContinue )
  3475. isFinished = true;
  3476. asCByteCode statement(engine);
  3477. if( node->nodeType == snDeclaration )
  3478. {
  3479. Error(TXT_DECL_IN_SWITCH, node);
  3480. // Compile it anyway to avoid further compiler errors
  3481. CompileDeclaration(node, &statement);
  3482. }
  3483. else
  3484. CompileStatement(node, &hasReturn, &statement);
  3485. LineInstr(bc, node->tokenPos);
  3486. bc->AddCode(&statement);
  3487. if( !hasCompileErrors )
  3488. asASSERT( tempVariables.GetLength() == 0 );
  3489. node = node->next;
  3490. }
  3491. }
  3492. void asCCompiler::CompileIfStatement(asCScriptNode *inode, bool *hasReturn, asCByteCode *bc)
  3493. {
  3494. // We will use one label for the if statement
  3495. // and possibly another for the else statement
  3496. int afterLabel = nextLabel++;
  3497. // Compile the expression
  3498. asCExprContext expr(engine);
  3499. int r = CompileAssignment(inode->firstChild, &expr);
  3500. if( r == 0 )
  3501. {
  3502. // Allow value types to be converted to bool using 'bool opImplConv()'
  3503. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3504. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), inode, asIC_IMPLICIT_CONV);
  3505. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3506. Error(TXT_EXPR_MUST_BE_BOOL, inode->firstChild);
  3507. else
  3508. {
  3509. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3510. ProcessDeferredParams(&expr);
  3511. if( !expr.type.isConstant )
  3512. {
  3513. ProcessPropertyGetAccessor(&expr, inode);
  3514. ConvertToVariable(&expr);
  3515. // Add a test
  3516. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3517. expr.bc.Instr(asBC_ClrHi);
  3518. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  3519. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3520. expr.bc.OptimizeLocally(tempVariableOffsets);
  3521. bc->AddCode(&expr.bc);
  3522. }
  3523. #if AS_SIZEOF_BOOL == 1
  3524. else if( expr.type.GetConstantB() == 0 )
  3525. #else
  3526. else if (expr.type.GetConstantDW() == 0)
  3527. #endif
  3528. {
  3529. // Jump to the else case
  3530. bc->InstrINT(asBC_JMP, afterLabel);
  3531. // TODO: Should we warn that the expression will always go to the else?
  3532. }
  3533. }
  3534. }
  3535. // Compile the if statement
  3536. bool origIsConstructorCalled = m_isConstructorCalled;
  3537. bool hasReturn1;
  3538. asCByteCode ifBC(engine);
  3539. CompileStatement(inode->firstChild->next, &hasReturn1, &ifBC);
  3540. // Add the byte code
  3541. LineInstr(bc, inode->firstChild->next->tokenPos);
  3542. bc->AddCode(&ifBC);
  3543. if( inode->firstChild->next->nodeType == snExpressionStatement && inode->firstChild->next->firstChild == 0 )
  3544. {
  3545. // Don't allow if( expr );
  3546. Error(TXT_IF_WITH_EMPTY_STATEMENT, inode->firstChild->next);
  3547. }
  3548. // If one of the statements call the constructor, the other must as well
  3549. // otherwise it is possible the constructor is never called
  3550. bool constructorCall1 = false;
  3551. bool constructorCall2 = false;
  3552. if( !origIsConstructorCalled && m_isConstructorCalled )
  3553. constructorCall1 = true;
  3554. // Do we have an else statement?
  3555. if( inode->firstChild->next != inode->lastChild )
  3556. {
  3557. // Reset the constructor called flag so the else statement can call the constructor too
  3558. m_isConstructorCalled = origIsConstructorCalled;
  3559. int afterElse = 0;
  3560. if( !hasReturn1 )
  3561. {
  3562. afterElse = nextLabel++;
  3563. // Add jump to after the else statement
  3564. bc->InstrINT(asBC_JMP, afterElse);
  3565. }
  3566. // Add label for the else statement
  3567. bc->Label((short)afterLabel);
  3568. bool hasReturn2;
  3569. asCByteCode elseBC(engine);
  3570. CompileStatement(inode->lastChild, &hasReturn2, &elseBC);
  3571. // Add byte code for the else statement
  3572. LineInstr(bc, inode->lastChild->tokenPos);
  3573. bc->AddCode(&elseBC);
  3574. if( inode->lastChild->nodeType == snExpressionStatement && inode->lastChild->firstChild == 0 )
  3575. {
  3576. // Don't allow if( expr ) {} else;
  3577. Error(TXT_ELSE_WITH_EMPTY_STATEMENT, inode->lastChild);
  3578. }
  3579. if( !hasReturn1 )
  3580. {
  3581. // Add label for the end of else statement
  3582. bc->Label((short)afterElse);
  3583. }
  3584. // The if statement only has return if both alternatives have
  3585. *hasReturn = hasReturn1 && hasReturn2;
  3586. if( !origIsConstructorCalled && m_isConstructorCalled )
  3587. constructorCall2 = true;
  3588. }
  3589. else
  3590. {
  3591. // Add label for the end of if statement
  3592. bc->Label((short)afterLabel);
  3593. *hasReturn = false;
  3594. }
  3595. // Make sure both or neither conditions call a constructor
  3596. if( (constructorCall1 && !constructorCall2) ||
  3597. (constructorCall2 && !constructorCall1) )
  3598. {
  3599. Error(TXT_BOTH_CONDITIONS_MUST_CALL_CONSTRUCTOR, inode);
  3600. }
  3601. m_isConstructorCalled = origIsConstructorCalled || constructorCall1 || constructorCall2;
  3602. }
  3603. void asCCompiler::CompileForStatement(asCScriptNode *fnode, asCByteCode *bc)
  3604. {
  3605. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3606. AddVariableScope(true, true);
  3607. // We will use three labels for the for loop
  3608. int conditionLabel = nextLabel++;
  3609. int afterLabel = nextLabel++;
  3610. int continueLabel = nextLabel++;
  3611. int insideLabel = nextLabel++;
  3612. continueLabels.PushLast(continueLabel);
  3613. breakLabels.PushLast(afterLabel);
  3614. //---------------------------------------
  3615. // Compile the initialization statement
  3616. asCByteCode initBC(engine);
  3617. LineInstr(&initBC, fnode->firstChild->tokenPos);
  3618. if( fnode->firstChild->nodeType == snDeclaration )
  3619. CompileDeclaration(fnode->firstChild, &initBC);
  3620. else
  3621. CompileExpressionStatement(fnode->firstChild, &initBC);
  3622. //-----------------------------------
  3623. // Compile the condition statement
  3624. asCExprContext expr(engine);
  3625. asCScriptNode *second = fnode->firstChild->next;
  3626. if( second->firstChild )
  3627. {
  3628. int r = CompileAssignment(second->firstChild, &expr);
  3629. if( r >= 0 )
  3630. {
  3631. // Allow value types to be converted to bool using 'bool opImplConv()'
  3632. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3633. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), second->firstChild, asIC_IMPLICIT_CONV);
  3634. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3635. Error(TXT_EXPR_MUST_BE_BOOL, second);
  3636. else
  3637. {
  3638. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3639. ProcessDeferredParams(&expr);
  3640. ProcessPropertyGetAccessor(&expr, second);
  3641. // If expression is false exit the loop
  3642. ConvertToVariable(&expr);
  3643. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3644. expr.bc.Instr(asBC_ClrHi);
  3645. expr.bc.InstrDWORD(asBC_JNZ, insideLabel);
  3646. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3647. expr.bc.OptimizeLocally(tempVariableOffsets);
  3648. // Prepend the line instruction for the condition
  3649. asCByteCode tmp(engine);
  3650. LineInstr(&tmp, second->firstChild->tokenPos);
  3651. tmp.AddCode(&expr.bc);
  3652. expr.bc.AddCode(&tmp);
  3653. }
  3654. }
  3655. }
  3656. //---------------------------
  3657. // Compile the increment statement(s)
  3658. asCByteCode nextBC(engine);
  3659. asCScriptNode *cnode = second->next;
  3660. while( cnode && cnode->nodeType == snExpressionStatement && cnode != fnode->lastChild )
  3661. {
  3662. LineInstr(&nextBC, cnode->tokenPos);
  3663. CompileExpressionStatement(cnode, &nextBC);
  3664. cnode = cnode->next;
  3665. }
  3666. //------------------------------
  3667. // Compile loop statement
  3668. bool hasReturn;
  3669. asCByteCode forBC(engine);
  3670. CompileStatement(fnode->lastChild, &hasReturn, &forBC);
  3671. //-------------------------------
  3672. // Join the code pieces
  3673. bc->AddCode(&initBC);
  3674. bc->InstrDWORD(asBC_JMP, conditionLabel);
  3675. bc->Label((short)insideLabel);
  3676. // Add a suspend bytecode inside the loop to guarantee
  3677. // that the application can suspend the execution
  3678. bc->Instr(asBC_SUSPEND);
  3679. bc->InstrPTR(asBC_JitEntry, 0);
  3680. LineInstr(bc, fnode->lastChild->tokenPos);
  3681. bc->AddCode(&forBC);
  3682. bc->Label((short)continueLabel);
  3683. bc->AddCode(&nextBC);
  3684. bc->Label((short)conditionLabel);
  3685. if( expr.bc.GetLastInstr() == -1 )
  3686. // There is no condition, so we just always jump
  3687. bc->InstrDWORD(asBC_JMP, insideLabel);
  3688. else
  3689. bc->AddCode(&expr.bc);
  3690. bc->Label((short)afterLabel);
  3691. continueLabels.PopLast();
  3692. breakLabels.PopLast();
  3693. // Deallocate variables in this block, in reverse order
  3694. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  3695. {
  3696. sVariable *v = variables->variables[n];
  3697. // Call variable destructors here, for variables not yet destroyed
  3698. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  3699. // Don't deallocate function parameters
  3700. if( v->stackOffset > 0 )
  3701. DeallocateVariable(v->stackOffset);
  3702. }
  3703. RemoveVariableScope();
  3704. }
  3705. void asCCompiler::CompileWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  3706. {
  3707. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3708. AddVariableScope(true, true);
  3709. // We will use two labels for the while loop
  3710. int beforeLabel = nextLabel++;
  3711. int afterLabel = nextLabel++;
  3712. continueLabels.PushLast(beforeLabel);
  3713. breakLabels.PushLast(afterLabel);
  3714. // Add label before the expression
  3715. bc->Label((short)beforeLabel);
  3716. // Compile expression
  3717. asCExprContext expr(engine);
  3718. int r = CompileAssignment(wnode->firstChild, &expr);
  3719. if( r == 0 )
  3720. {
  3721. // Allow value types to be converted to bool using 'bool opImplConv()'
  3722. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3723. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), wnode->firstChild, asIC_IMPLICIT_CONV);
  3724. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3725. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  3726. else
  3727. {
  3728. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3729. ProcessDeferredParams(&expr);
  3730. ProcessPropertyGetAccessor(&expr, wnode);
  3731. // Add byte code for the expression
  3732. ConvertToVariable(&expr);
  3733. // Jump to end of statement if expression is false
  3734. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3735. expr.bc.Instr(asBC_ClrHi);
  3736. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  3737. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3738. expr.bc.OptimizeLocally(tempVariableOffsets);
  3739. bc->AddCode(&expr.bc);
  3740. }
  3741. }
  3742. // Add a suspend bytecode inside the loop to guarantee
  3743. // that the application can suspend the execution
  3744. bc->Instr(asBC_SUSPEND);
  3745. bc->InstrPTR(asBC_JitEntry, 0);
  3746. // Compile statement
  3747. bool hasReturn;
  3748. asCByteCode whileBC(engine);
  3749. CompileStatement(wnode->lastChild, &hasReturn, &whileBC);
  3750. // Add byte code for the statement
  3751. LineInstr(bc, wnode->lastChild->tokenPos);
  3752. bc->AddCode(&whileBC);
  3753. // Jump to the expression
  3754. bc->InstrINT(asBC_JMP, beforeLabel);
  3755. // Add label after the statement
  3756. bc->Label((short)afterLabel);
  3757. continueLabels.PopLast();
  3758. breakLabels.PopLast();
  3759. RemoveVariableScope();
  3760. }
  3761. void asCCompiler::CompileDoWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  3762. {
  3763. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3764. AddVariableScope(true, true);
  3765. // We will use two labels for the while loop
  3766. int beforeLabel = nextLabel++;
  3767. int beforeTest = nextLabel++;
  3768. int afterLabel = nextLabel++;
  3769. continueLabels.PushLast(beforeTest);
  3770. breakLabels.PushLast(afterLabel);
  3771. // Add label before the statement
  3772. bc->Label((short)beforeLabel);
  3773. // Compile statement
  3774. bool hasReturn;
  3775. asCByteCode whileBC(engine);
  3776. CompileStatement(wnode->firstChild, &hasReturn, &whileBC);
  3777. // Add byte code for the statement
  3778. LineInstr(bc, wnode->firstChild->tokenPos);
  3779. bc->AddCode(&whileBC);
  3780. // Add label before the expression
  3781. bc->Label((short)beforeTest);
  3782. // Add a suspend bytecode inside the loop to guarantee
  3783. // that the application can suspend the execution
  3784. bc->Instr(asBC_SUSPEND);
  3785. bc->InstrPTR(asBC_JitEntry, 0);
  3786. // Add a line instruction
  3787. LineInstr(bc, wnode->lastChild->tokenPos);
  3788. // Compile expression
  3789. asCExprContext expr(engine);
  3790. CompileAssignment(wnode->lastChild, &expr);
  3791. // Allow value types to be converted to bool using 'bool opImplConv()'
  3792. if( expr.type.dataType.GetTypeInfo() && (expr.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  3793. ImplicitConversion(&expr, asCDataType::CreatePrimitive(ttBool, false), wnode->lastChild, asIC_IMPLICIT_CONV);
  3794. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3795. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  3796. else
  3797. {
  3798. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3799. ProcessDeferredParams(&expr);
  3800. ProcessPropertyGetAccessor(&expr, wnode);
  3801. // Add byte code for the expression
  3802. ConvertToVariable(&expr);
  3803. // Jump to next iteration if expression is true
  3804. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3805. expr.bc.Instr(asBC_ClrHi);
  3806. expr.bc.InstrDWORD(asBC_JNZ, beforeLabel);
  3807. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3808. expr.bc.OptimizeLocally(tempVariableOffsets);
  3809. bc->AddCode(&expr.bc);
  3810. }
  3811. // Add label after the statement
  3812. bc->Label((short)afterLabel);
  3813. continueLabels.PopLast();
  3814. breakLabels.PopLast();
  3815. RemoveVariableScope();
  3816. }
  3817. void asCCompiler::CompileBreakStatement(asCScriptNode *node, asCByteCode *bc)
  3818. {
  3819. if( breakLabels.GetLength() == 0 )
  3820. {
  3821. Error(TXT_INVALID_BREAK, node);
  3822. return;
  3823. }
  3824. // Add destructor calls for all variables that will go out of scope
  3825. // Put this clean up in a block to allow exception handler to understand them
  3826. bc->Block(true);
  3827. asCVariableScope *vs = variables;
  3828. while( !vs->isBreakScope )
  3829. {
  3830. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  3831. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  3832. vs = vs->parent;
  3833. }
  3834. bc->Block(false);
  3835. bc->InstrINT(asBC_JMP, breakLabels[breakLabels.GetLength()-1]);
  3836. }
  3837. void asCCompiler::CompileContinueStatement(asCScriptNode *node, asCByteCode *bc)
  3838. {
  3839. if( continueLabels.GetLength() == 0 )
  3840. {
  3841. Error(TXT_INVALID_CONTINUE, node);
  3842. return;
  3843. }
  3844. // Add destructor calls for all variables that will go out of scope
  3845. // Put this clean up in a block to allow exception handler to understand them
  3846. bc->Block(true);
  3847. asCVariableScope *vs = variables;
  3848. while( !vs->isContinueScope )
  3849. {
  3850. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  3851. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  3852. vs = vs->parent;
  3853. }
  3854. bc->Block(false);
  3855. bc->InstrINT(asBC_JMP, continueLabels[continueLabels.GetLength()-1]);
  3856. }
  3857. void asCCompiler::CompileExpressionStatement(asCScriptNode *enode, asCByteCode *bc)
  3858. {
  3859. if( enode->firstChild )
  3860. {
  3861. // Compile the expression
  3862. asCExprContext expr(engine);
  3863. CompileAssignment(enode->firstChild, &expr);
  3864. // Must not have unused ambiguous names
  3865. if( expr.IsClassMethod() || expr.IsGlobalFunc() )
  3866. Error(TXT_INVALID_EXPRESSION_AMBIGUOUS_NAME, enode);
  3867. // Must not have unused anonymous functions
  3868. if( expr.IsLambda() )
  3869. Error(TXT_INVALID_EXPRESSION_LAMBDA, enode);
  3870. // If we get here and there is still an unprocessed property
  3871. // accessor, then process it as a get access. Don't call if there is
  3872. // already a compile error, or we might report an error that is not valid
  3873. if( !hasCompileErrors )
  3874. ProcessPropertyGetAccessor(&expr, enode);
  3875. // Pop the value from the stack
  3876. if( !expr.type.dataType.IsPrimitive() )
  3877. expr.bc.Instr(asBC_PopPtr);
  3878. // Release temporary variables used by expression
  3879. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3880. ProcessDeferredParams(&expr);
  3881. expr.bc.OptimizeLocally(tempVariableOffsets);
  3882. bc->AddCode(&expr.bc);
  3883. }
  3884. }
  3885. void asCCompiler::PrepareTemporaryVariable(asCScriptNode *node, asCExprContext *ctx, bool forceOnHeap)
  3886. {
  3887. // The input can be either an object or funcdef, either as handle or reference
  3888. asASSERT(ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef());
  3889. // If the object already is stored in temporary variable then nothing needs to be done
  3890. // Note, a type can be temporary without being a variable, in which case it is holding off
  3891. // on releasing a previously used object.
  3892. if( ctx->type.isTemporary && ctx->type.isVariable &&
  3893. !(forceOnHeap && !IsVariableOnHeap(ctx->type.stackOffset)) )
  3894. {
  3895. // If the temporary object is currently not a reference
  3896. // the expression needs to be reevaluated to a reference
  3897. if( !ctx->type.dataType.IsReference() )
  3898. {
  3899. ctx->bc.Instr(asBC_PopPtr);
  3900. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  3901. ctx->type.dataType.MakeReference(true);
  3902. }
  3903. return;
  3904. }
  3905. // Allocate temporary variable
  3906. asCDataType dt = ctx->type.dataType;
  3907. dt.MakeReference(false);
  3908. dt.MakeReadOnly(false);
  3909. int offset = AllocateVariable(dt, true, forceOnHeap);
  3910. // Objects stored on the stack are not considered references
  3911. dt.MakeReference(IsVariableOnHeap(offset));
  3912. asCExprValue lvalue;
  3913. lvalue.Set(dt);
  3914. lvalue.isExplicitHandle = ctx->type.isExplicitHandle;
  3915. bool isExplicitHandle = ctx->type.isExplicitHandle;
  3916. CompileInitAsCopy(dt, offset, &ctx->bc, ctx, node, false);
  3917. // Push the reference to the temporary variable on the stack
  3918. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  3919. ctx->type.Set(dt);
  3920. ctx->type.isTemporary = true;
  3921. ctx->type.stackOffset = (short)offset;
  3922. ctx->type.isVariable = true;
  3923. ctx->type.isExplicitHandle = isExplicitHandle;
  3924. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  3925. }
  3926. void asCCompiler::CompileReturnStatement(asCScriptNode *rnode, asCByteCode *bc)
  3927. {
  3928. // Get return type and location
  3929. sVariable *v = variables->GetVariable("return");
  3930. // Basic validations
  3931. if( v->type.GetSizeOnStackDWords() > 0 && !rnode->firstChild )
  3932. {
  3933. Error(TXT_MUST_RETURN_VALUE, rnode);
  3934. return;
  3935. }
  3936. else if( v->type.GetSizeOnStackDWords() == 0 && rnode->firstChild )
  3937. {
  3938. Error(TXT_CANT_RETURN_VALUE, rnode);
  3939. return;
  3940. }
  3941. // Compile the expression
  3942. if( rnode->firstChild )
  3943. {
  3944. // Compile the expression
  3945. asCExprContext expr(engine);
  3946. int r = CompileAssignment(rnode->firstChild, &expr);
  3947. if( r < 0 ) return;
  3948. if( v->type.IsReference() )
  3949. {
  3950. // The expression that gives the reference must not use any of the
  3951. // variables that must be destroyed upon exit, because then it means
  3952. // reference will stay alive while the clean-up is done, which could
  3953. // potentially mean that the reference is invalidated by the clean-up.
  3954. //
  3955. // When the function is returning a reference, the clean-up of the
  3956. // variables must be done before the evaluation of the expression.
  3957. //
  3958. // A reference to a global variable, or a class member for class methods
  3959. // should be allowed to be returned.
  3960. if( !(expr.type.dataType.IsReference() ||
  3961. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle())) )
  3962. {
  3963. // Clean up the potential deferred parameters
  3964. ProcessDeferredParams(&expr);
  3965. Error(TXT_NOT_VALID_REFERENCE, rnode);
  3966. return;
  3967. }
  3968. // No references to local variables, temporary variables, or parameters
  3969. // are allowed to be returned, since they go out of scope when the function
  3970. // returns. Even reference parameters are disallowed, since it is not possible
  3971. // to know the scope of them. The exception is the 'this' pointer, which
  3972. // is treated by the compiler as a local variable, but isn't really so.
  3973. if( (expr.type.isVariable && !(expr.type.stackOffset == 0 && outFunc->objectType)) || expr.type.isTemporary )
  3974. {
  3975. // Clean up the potential deferred parameters
  3976. ProcessDeferredParams(&expr);
  3977. Error(TXT_CANNOT_RETURN_REF_TO_LOCAL, rnode);
  3978. return;
  3979. }
  3980. // The type must match exactly as we cannot convert
  3981. // the reference without loosing the original value
  3982. if( !(v->type.IsEqualExceptConst(expr.type.dataType) ||
  3983. ((expr.type.dataType.IsObject() || expr.type.dataType.IsFuncdef()) &&
  3984. !expr.type.dataType.IsObjectHandle() &&
  3985. v->type.IsEqualExceptRefAndConst(expr.type.dataType))) ||
  3986. (!v->type.IsReadOnly() && expr.type.dataType.IsReadOnly()) )
  3987. {
  3988. // Clean up the potential deferred parameters
  3989. ProcessDeferredParams(&expr);
  3990. asCString str;
  3991. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format(outFunc->nameSpace).AddressOf(), v->type.Format(outFunc->nameSpace).AddressOf());
  3992. Error(str, rnode);
  3993. return;
  3994. }
  3995. // The expression must not have any deferred expressions, because the evaluation
  3996. // of these cannot be done without keeping the reference which is not safe
  3997. if( expr.deferredParams.GetLength() )
  3998. {
  3999. // Clean up the potential deferred parameters
  4000. ProcessDeferredParams(&expr);
  4001. Error(TXT_REF_CANT_BE_RETURNED_DEFERRED_PARAM, rnode);
  4002. return;
  4003. }
  4004. // Make sure the expression isn't using any local variables that
  4005. // will need to be cleaned up before the function completes
  4006. asCArray<int> usedVars;
  4007. expr.bc.GetVarsUsed(usedVars);
  4008. for( asUINT n = 0; n < usedVars.GetLength(); n++ )
  4009. {
  4010. int var = GetVariableSlot(usedVars[n]);
  4011. if( var != -1 )
  4012. {
  4013. asCDataType dt = variableAllocations[var];
  4014. if( dt.IsObject() )
  4015. {
  4016. ProcessDeferredParams(&expr);
  4017. Error(TXT_REF_CANT_BE_RETURNED_LOCAL_VARS, rnode);
  4018. return;
  4019. }
  4020. }
  4021. }
  4022. // Can't return the reference if could point to a local variable
  4023. if( expr.type.isRefToLocal )
  4024. {
  4025. ProcessDeferredParams(&expr);
  4026. Error(TXT_REF_CANT_BE_TO_LOCAL_VAR, rnode);
  4027. return;
  4028. }
  4029. // All objects in the function must be cleaned up before the expression
  4030. // is evaluated, otherwise there is a possibility that the cleanup will
  4031. // invalidate the reference.
  4032. // Destroy the local variables before loading
  4033. // the reference into the register. This will
  4034. // be done before the expression is evaluated.
  4035. DestroyVariables(bc);
  4036. // For primitives the reference is already in the register,
  4037. // but for non-primitives the reference is on the stack so we
  4038. // need to load it into the register
  4039. if( !expr.type.dataType.IsPrimitive() )
  4040. {
  4041. if( !expr.type.dataType.IsObjectHandle() &&
  4042. expr.type.dataType.IsReference() )
  4043. expr.bc.Instr(asBC_RDSPtr);
  4044. expr.bc.Instr(asBC_PopRPtr);
  4045. }
  4046. // There are no temporaries to release so we're done
  4047. }
  4048. else // if( !v->type.IsReference() )
  4049. {
  4050. ProcessPropertyGetAccessor(&expr, rnode);
  4051. // Prepare the value for assignment
  4052. IsVariableInitialized(&expr.type, rnode->firstChild);
  4053. if( v->type.IsPrimitive() )
  4054. {
  4055. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  4056. // Implicitly convert the value to the return type
  4057. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  4058. // Verify that the conversion was successful
  4059. if( expr.type.dataType != v->type )
  4060. {
  4061. asCString str;
  4062. str.Format(TXT_NO_CONVERSION_s_TO_s, expr.type.dataType.Format(outFunc->nameSpace).AddressOf(), v->type.Format(outFunc->nameSpace).AddressOf());
  4063. Error(str, rnode);
  4064. return;
  4065. }
  4066. else
  4067. {
  4068. ConvertToVariable(&expr);
  4069. // Clean up the local variables and process deferred parameters
  4070. DestroyVariables(&expr.bc);
  4071. ProcessDeferredParams(&expr);
  4072. ReleaseTemporaryVariable(expr.type, &expr.bc);
  4073. // Load the variable in the register
  4074. if( v->type.GetSizeOnStackDWords() == 1 )
  4075. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  4076. else
  4077. expr.bc.InstrSHORT(asBC_CpyVtoR8, expr.type.stackOffset);
  4078. }
  4079. }
  4080. else if( v->type.IsObject() || v->type.IsFuncdef() )
  4081. {
  4082. // Value types are returned on the stack, in a location
  4083. // that has been reserved by the calling function.
  4084. if( outFunc->DoesReturnOnStack() )
  4085. {
  4086. // TODO: runtime optimize: If the return type has a constructor that takes the type of the expression,
  4087. // it should be called directly instead of first converting the expression and
  4088. // then copy the value.
  4089. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  4090. {
  4091. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  4092. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  4093. {
  4094. asCString str;
  4095. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format(outFunc->nameSpace).AddressOf(), v->type.Format(outFunc->nameSpace).AddressOf());
  4096. Error(str, rnode->firstChild);
  4097. return;
  4098. }
  4099. }
  4100. int offset = outFunc->objectType ? -AS_PTR_SIZE : 0;
  4101. CompileInitAsCopy(v->type, offset, &expr.bc, &expr, rnode->firstChild, true);
  4102. // Clean up the local variables and process deferred parameters
  4103. DestroyVariables(&expr.bc);
  4104. ProcessDeferredParams(&expr);
  4105. }
  4106. else
  4107. {
  4108. asASSERT( (v->type.GetTypeInfo()->flags & asOBJ_REF) || v->type.IsFuncdef() );
  4109. // Prepare the expression to be loaded into the object
  4110. // register. This will place the reference in local variable
  4111. PrepareArgument(&v->type, &expr, rnode->firstChild, false, 0);
  4112. // Pop the reference to the temporary variable
  4113. expr.bc.Instr(asBC_PopPtr);
  4114. // Clean up the local variables and process deferred parameters
  4115. DestroyVariables(&expr.bc);
  4116. ProcessDeferredParams(&expr);
  4117. // Load the object pointer into the object register
  4118. // LOADOBJ also clears the address in the variable
  4119. expr.bc.InstrSHORT(asBC_LOADOBJ, expr.type.stackOffset);
  4120. // LOADOBJ cleared the address in the variable so the object will not be freed
  4121. // here, but the temporary variable must still be freed so the slot can be reused
  4122. // By releasing without the bytecode we do just that.
  4123. ReleaseTemporaryVariable(expr.type, 0);
  4124. }
  4125. }
  4126. }
  4127. expr.bc.OptimizeLocally(tempVariableOffsets);
  4128. bc->AddCode(&expr.bc);
  4129. }
  4130. else
  4131. {
  4132. // For functions that don't return anything
  4133. // we just detroy the local variables
  4134. DestroyVariables(bc);
  4135. }
  4136. // Jump to the end of the function
  4137. bc->InstrINT(asBC_JMP, 0);
  4138. }
  4139. void asCCompiler::DestroyVariables(asCByteCode *bc)
  4140. {
  4141. // Call destructor on all variables except for the function parameters
  4142. // Put the clean-up in a block to allow exception handler to understand this
  4143. bc->Block(true);
  4144. asCVariableScope *vs = variables;
  4145. while( vs )
  4146. {
  4147. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  4148. if( vs->variables[n]->stackOffset > 0 )
  4149. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  4150. vs = vs->parent;
  4151. }
  4152. bc->Block(false);
  4153. }
  4154. void asCCompiler::AddVariableScope(bool isBreakScope, bool isContinueScope)
  4155. {
  4156. variables = asNEW(asCVariableScope)(variables);
  4157. if( variables == 0 )
  4158. {
  4159. // Out of memory
  4160. return;
  4161. }
  4162. variables->isBreakScope = isBreakScope;
  4163. variables->isContinueScope = isContinueScope;
  4164. }
  4165. void asCCompiler::RemoveVariableScope()
  4166. {
  4167. if( variables )
  4168. {
  4169. asCVariableScope *var = variables;
  4170. variables = variables->parent;
  4171. asDELETE(var,asCVariableScope);
  4172. }
  4173. }
  4174. void asCCompiler::Error(const asCString &msg, asCScriptNode *node)
  4175. {
  4176. asCString str;
  4177. int r = 0, c = 0;
  4178. asASSERT( node );
  4179. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4180. builder->WriteError(script->name, msg, r, c);
  4181. hasCompileErrors = true;
  4182. }
  4183. void asCCompiler::Warning(const asCString &msg, asCScriptNode *node)
  4184. {
  4185. asCString str;
  4186. int r = 0, c = 0;
  4187. asASSERT( node );
  4188. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4189. builder->WriteWarning(script->name, msg, r, c);
  4190. }
  4191. void asCCompiler::Information(const asCString &msg, asCScriptNode *node)
  4192. {
  4193. asCString str;
  4194. int r = 0, c = 0;
  4195. asASSERT( node );
  4196. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4197. builder->WriteInfo(script->name, msg, r, c, false);
  4198. }
  4199. void asCCompiler::PrintMatchingFuncs(asCArray<int> &funcs, asCScriptNode *node, asCObjectType *inType)
  4200. {
  4201. int r = 0, c = 0;
  4202. asASSERT( node );
  4203. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  4204. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  4205. {
  4206. asCScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  4207. if( inType && func->funcType == asFUNC_VIRTUAL )
  4208. func = inType->virtualFunctionTable[func->vfTableIdx];
  4209. builder->WriteInfo(script->name, func->GetDeclaration(true, false, true), r, c, false);
  4210. }
  4211. }
  4212. int asCCompiler::AllocateVariableNotIn(const asCDataType &type, bool isTemporary, bool forceOnHeap, asCExprContext *ctx)
  4213. {
  4214. int l = int(reservedVariables.GetLength());
  4215. ctx->bc.GetVarsUsed(reservedVariables);
  4216. int var = AllocateVariable(type, isTemporary, forceOnHeap);
  4217. reservedVariables.SetLength(l);
  4218. return var;
  4219. }
  4220. int asCCompiler::AllocateVariable(const asCDataType &type, bool isTemporary, bool forceOnHeap)
  4221. {
  4222. asCDataType t(type);
  4223. t.MakeReference(false);
  4224. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 1 )
  4225. t.SetTokenType(ttInt);
  4226. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 2 )
  4227. t.SetTokenType(ttDouble);
  4228. // Only null handles have the token type unrecognized token
  4229. asASSERT( t.IsObjectHandle() || t.GetTokenType() != ttUnrecognizedToken );
  4230. bool isOnHeap = true;
  4231. if( t.IsPrimitive() ||
  4232. (t.GetTypeInfo() && (t.GetTypeInfo()->GetFlags() & asOBJ_VALUE) && !forceOnHeap) )
  4233. {
  4234. // Primitives and value types (unless overridden) are allocated on the stack
  4235. isOnHeap = false;
  4236. }
  4237. // Find a free location with the same type
  4238. for( asUINT n = 0; n < freeVariables.GetLength(); n++ )
  4239. {
  4240. int slot = freeVariables[n];
  4241. if( variableAllocations[slot].IsEqualExceptConst(t) &&
  4242. variableIsTemporary[slot] == isTemporary &&
  4243. variableIsOnHeap[slot] == isOnHeap )
  4244. {
  4245. // We can't return by slot, must count variable sizes
  4246. int offset = GetVariableOffset(slot);
  4247. // Verify that it is not in the list of reserved variables
  4248. bool isUsed = false;
  4249. if( reservedVariables.GetLength() )
  4250. isUsed = reservedVariables.Exists(offset);
  4251. if( !isUsed )
  4252. {
  4253. if( n != freeVariables.GetLength() - 1 )
  4254. freeVariables[n] = freeVariables.PopLast();
  4255. else
  4256. freeVariables.PopLast();
  4257. if( isTemporary )
  4258. tempVariables.PushLast(offset);
  4259. return offset;
  4260. }
  4261. }
  4262. }
  4263. variableAllocations.PushLast(t);
  4264. variableIsTemporary.PushLast(isTemporary);
  4265. variableIsOnHeap.PushLast(isOnHeap);
  4266. int offset = GetVariableOffset((int)variableAllocations.GetLength()-1);
  4267. if( isTemporary )
  4268. {
  4269. // Add offset to the currently allocated temporary variables
  4270. tempVariables.PushLast(offset);
  4271. // Add offset to all known offsets to temporary variables, whether allocated or not
  4272. tempVariableOffsets.PushLast(offset);
  4273. }
  4274. return offset;
  4275. }
  4276. int asCCompiler::GetVariableOffset(int varIndex)
  4277. {
  4278. // Return offset to the last dword on the stack
  4279. // Start at 1 as offset 0 is reserved for the this pointer (or first argument for global functions)
  4280. int varOffset = 1;
  4281. // Skip lower variables
  4282. for( int n = 0; n < varIndex; n++ )
  4283. {
  4284. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  4285. varOffset += variableAllocations[n].GetSizeInMemoryDWords();
  4286. else
  4287. varOffset += variableAllocations[n].GetSizeOnStackDWords();
  4288. }
  4289. if( varIndex < (int)variableAllocations.GetLength() )
  4290. {
  4291. // For variables larger than 1 dword the returned offset should be to the last dword
  4292. int size;
  4293. if( !variableIsOnHeap[varIndex] && variableAllocations[varIndex].IsObject() )
  4294. size = variableAllocations[varIndex].GetSizeInMemoryDWords();
  4295. else
  4296. size = variableAllocations[varIndex].GetSizeOnStackDWords();
  4297. if( size > 1 )
  4298. varOffset += size-1;
  4299. }
  4300. return varOffset;
  4301. }
  4302. int asCCompiler::GetVariableSlot(int offset)
  4303. {
  4304. int varOffset = 1;
  4305. for( asUINT n = 0; n < variableAllocations.GetLength(); n++ )
  4306. {
  4307. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  4308. varOffset += -1 + variableAllocations[n].GetSizeInMemoryDWords();
  4309. else
  4310. varOffset += -1 + variableAllocations[n].GetSizeOnStackDWords();
  4311. if( varOffset == offset )
  4312. return n;
  4313. varOffset++;
  4314. }
  4315. return -1;
  4316. }
  4317. bool asCCompiler::IsVariableOnHeap(int offset)
  4318. {
  4319. int varSlot = GetVariableSlot(offset);
  4320. if( varSlot < 0 )
  4321. {
  4322. // This happens for function arguments that are considered as on the heap
  4323. return true;
  4324. }
  4325. return variableIsOnHeap[varSlot];
  4326. }
  4327. void asCCompiler::DeallocateVariable(int offset)
  4328. {
  4329. // Remove temporary variable
  4330. int n;
  4331. for( n = 0; n < (int)tempVariables.GetLength(); n++ )
  4332. {
  4333. if( offset == tempVariables[n] )
  4334. {
  4335. if( n == (int)tempVariables.GetLength()-1 )
  4336. tempVariables.PopLast();
  4337. else
  4338. tempVariables[n] = tempVariables.PopLast();
  4339. break;
  4340. }
  4341. }
  4342. // Mark the variable slot available for new allocations
  4343. n = GetVariableSlot(offset);
  4344. if( n != -1 )
  4345. {
  4346. freeVariables.PushLast(n);
  4347. return;
  4348. }
  4349. // We might get here if the variable was implicitly declared
  4350. // because it was used before a formal declaration, in this case
  4351. // the offset is 0x7FFF
  4352. asASSERT(offset == 0x7FFF);
  4353. }
  4354. void asCCompiler::ReleaseTemporaryVariable(asCExprValue &t, asCByteCode *bc)
  4355. {
  4356. if( t.isTemporary )
  4357. {
  4358. ReleaseTemporaryVariable(t.stackOffset, bc);
  4359. t.isTemporary = false;
  4360. }
  4361. }
  4362. void asCCompiler::ReleaseTemporaryVariable(int offset, asCByteCode *bc)
  4363. {
  4364. asASSERT( tempVariables.Exists(offset) );
  4365. if( bc )
  4366. {
  4367. // We need to call the destructor on the true variable type
  4368. int n = GetVariableSlot(offset);
  4369. asASSERT( n >= 0 );
  4370. if( n >= 0 )
  4371. {
  4372. asCDataType dt = variableAllocations[n];
  4373. bool isOnHeap = variableIsOnHeap[n];
  4374. // Call destructor
  4375. CallDestructor(dt, offset, isOnHeap, bc);
  4376. }
  4377. }
  4378. DeallocateVariable(offset);
  4379. }
  4380. void asCCompiler::Dereference(asCExprContext *ctx, bool generateCode)
  4381. {
  4382. if( ctx->type.dataType.IsReference() )
  4383. {
  4384. if( ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef() )
  4385. {
  4386. ctx->type.dataType.MakeReference(false);
  4387. if( generateCode )
  4388. ctx->bc.Instr(asBC_RDSPtr);
  4389. }
  4390. else
  4391. {
  4392. // This should never happen as primitives are treated differently
  4393. asASSERT(false);
  4394. }
  4395. }
  4396. }
  4397. bool asCCompiler::IsVariableInitialized(asCExprValue *type, asCScriptNode *node)
  4398. {
  4399. // No need to check if there is no variable scope
  4400. if( variables == 0 ) return true;
  4401. // Temporary variables are assumed to be initialized
  4402. if( type->isTemporary ) return true;
  4403. // Verify that it is a variable
  4404. if( !type->isVariable ) return true;
  4405. // Find the variable
  4406. sVariable *v = variables->GetVariableByOffset(type->stackOffset);
  4407. // The variable isn't found if it is a constant, in which case it is guaranteed to be initialized
  4408. if( v == 0 ) return true;
  4409. if( v->isInitialized ) return true;
  4410. // Complex types don't need this test
  4411. if( v->type.IsObject() || v->type.IsFuncdef() ) return true;
  4412. // Mark as initialized so that the user will not be bothered again
  4413. v->isInitialized = true;
  4414. // Write warning
  4415. asCString str;
  4416. str.Format(TXT_s_NOT_INITIALIZED, (const char *)v->name.AddressOf());
  4417. Warning(str, node);
  4418. return false;
  4419. }
  4420. void asCCompiler::PrepareOperand(asCExprContext *ctx, asCScriptNode *node)
  4421. {
  4422. // Check if the variable is initialized (if it indeed is a variable)
  4423. IsVariableInitialized(&ctx->type, node);
  4424. asCDataType to = ctx->type.dataType;
  4425. to.MakeReference(false);
  4426. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  4427. ProcessDeferredParams(ctx);
  4428. }
  4429. void asCCompiler::PrepareForAssignment(asCDataType *lvalue, asCExprContext *rctx, asCScriptNode *node, bool toTemporary, asCExprContext *lvalueExpr)
  4430. {
  4431. // Reserve the temporary variables used in the lvalue expression so they won't end up being used by the rvalue too
  4432. int l = int(reservedVariables.GetLength());
  4433. if( lvalueExpr ) lvalueExpr->bc.GetVarsUsed(reservedVariables);
  4434. ProcessPropertyGetAccessor(rctx, node);
  4435. // Make sure the rvalue is initialized if it is a variable
  4436. IsVariableInitialized(&rctx->type, node);
  4437. if( lvalue->IsPrimitive() )
  4438. {
  4439. if( rctx->type.dataType.IsPrimitive() )
  4440. {
  4441. if( rctx->type.dataType.IsReference() )
  4442. {
  4443. // Cannot do implicit conversion of references so we first convert the reference to a variable
  4444. ConvertToVariableNotIn(rctx, lvalueExpr);
  4445. }
  4446. }
  4447. // Implicitly convert the value to the right type
  4448. ImplicitConversion(rctx, *lvalue, node, asIC_IMPLICIT_CONV);
  4449. // Check data type
  4450. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  4451. {
  4452. asCString str;
  4453. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lvalue->Format(outFunc->nameSpace).AddressOf());
  4454. Error(str, node);
  4455. rctx->type.SetDummy();
  4456. }
  4457. // Make sure the rvalue is a variable
  4458. if( !rctx->type.isVariable )
  4459. ConvertToVariableNotIn(rctx, lvalueExpr);
  4460. }
  4461. else
  4462. {
  4463. asCDataType to = *lvalue;
  4464. to.MakeReference(false);
  4465. // TODO: ImplicitConversion should know to do this by itself
  4466. // First convert to a handle which will do a reference cast
  4467. if( !lvalue->IsObjectHandle() &&
  4468. (lvalue->GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  4469. to.MakeHandle(true);
  4470. // Don't allow the implicit conversion to create an object
  4471. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  4472. if( !lvalue->IsObjectHandle() &&
  4473. (lvalue->GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  4474. {
  4475. // Then convert to a reference, which will validate the handle
  4476. to.MakeHandle(false);
  4477. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  4478. }
  4479. // Check data type
  4480. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  4481. {
  4482. asCString str;
  4483. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lvalue->Format(outFunc->nameSpace).AddressOf());
  4484. Error(str, node);
  4485. }
  4486. else
  4487. {
  4488. // If the assignment will be made with the copy behaviour then the rvalue must not be a reference
  4489. asASSERT(!lvalue->IsObject() || !rctx->type.dataType.IsReference());
  4490. }
  4491. }
  4492. // Unreserve variables
  4493. reservedVariables.SetLength(l);
  4494. }
  4495. bool asCCompiler::IsLValue(asCExprValue &type)
  4496. {
  4497. if( !type.isLValue ) return false;
  4498. if( type.dataType.IsReadOnly() ) return false;
  4499. if( !type.dataType.IsObject() && !type.isVariable && !type.dataType.IsReference() ) return false;
  4500. return true;
  4501. }
  4502. int asCCompiler::PerformAssignment(asCExprValue *lvalue, asCExprValue *rvalue, asCByteCode *bc, asCScriptNode *node)
  4503. {
  4504. if( lvalue->dataType.IsReadOnly() )
  4505. {
  4506. Error(TXT_REF_IS_READ_ONLY, node);
  4507. return -1;
  4508. }
  4509. if( lvalue->dataType.IsPrimitive() )
  4510. {
  4511. if( lvalue->isVariable )
  4512. {
  4513. // Copy the value between the variables directly
  4514. if( lvalue->dataType.GetSizeInMemoryDWords() == 1 )
  4515. bc->InstrW_W(asBC_CpyVtoV4, lvalue->stackOffset, rvalue->stackOffset);
  4516. else
  4517. bc->InstrW_W(asBC_CpyVtoV8, lvalue->stackOffset, rvalue->stackOffset);
  4518. // Mark variable as initialized
  4519. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  4520. if( v ) v->isInitialized = true;
  4521. }
  4522. else if( lvalue->dataType.IsReference() )
  4523. {
  4524. // Copy the value of the variable to the reference in the register
  4525. int s = lvalue->dataType.GetSizeInMemoryBytes();
  4526. if( s == 1 )
  4527. bc->InstrSHORT(asBC_WRTV1, rvalue->stackOffset);
  4528. else if( s == 2 )
  4529. bc->InstrSHORT(asBC_WRTV2, rvalue->stackOffset);
  4530. else if( s == 4 )
  4531. bc->InstrSHORT(asBC_WRTV4, rvalue->stackOffset);
  4532. else if( s == 8 )
  4533. bc->InstrSHORT(asBC_WRTV8, rvalue->stackOffset);
  4534. }
  4535. else
  4536. {
  4537. Error(TXT_NOT_VALID_LVALUE, node);
  4538. return -1;
  4539. }
  4540. }
  4541. else if( !lvalue->isExplicitHandle )
  4542. {
  4543. asCExprContext ctx(engine);
  4544. ctx.type = *lvalue;
  4545. Dereference(&ctx, true);
  4546. *lvalue = ctx.type;
  4547. bc->AddCode(&ctx.bc);
  4548. asSTypeBehaviour *beh = lvalue->dataType.GetBehaviour();
  4549. if( beh && beh->copy && beh->copy != engine->scriptTypeBehaviours.beh.copy )
  4550. {
  4551. asCExprContext res(engine);
  4552. PerformFunctionCall(beh->copy, &res, false, 0, CastToObjectType(lvalue->dataType.GetTypeInfo()));
  4553. bc->AddCode(&res.bc);
  4554. *lvalue = res.type;
  4555. }
  4556. else if( beh && beh->copy == engine->scriptTypeBehaviours.beh.copy )
  4557. {
  4558. // Call the default copy operator for script classes
  4559. // This is done differently because the default copy operator
  4560. // is registered as returning int&, but in reality it returns
  4561. // a reference to the object.
  4562. // TODO: Avoid this special case by implementing a copystub for
  4563. // script classes that uses the default copy operator
  4564. bc->Call(asBC_CALLSYS, beh->copy, 2*AS_PTR_SIZE);
  4565. bc->Instr(asBC_PshRPtr);
  4566. }
  4567. else
  4568. {
  4569. // Default copy operator
  4570. if( lvalue->dataType.GetSizeInMemoryDWords() == 0 ||
  4571. !(lvalue->dataType.GetTypeInfo()->flags & asOBJ_POD) )
  4572. {
  4573. asCString msg;
  4574. msg.Format(TXT_NO_DEFAULT_COPY_OP_FOR_s, lvalue->dataType.GetTypeInfo()->name.AddressOf());
  4575. Error(msg, node);
  4576. return -1;
  4577. }
  4578. // Copy larger data types from a reference
  4579. // TODO: runtime optimize: COPY should pop both arguments and store the reference in the register.
  4580. bc->InstrSHORT_DW(asBC_COPY, (short)lvalue->dataType.GetSizeInMemoryDWords(), engine->GetTypeIdFromDataType(lvalue->dataType));
  4581. }
  4582. }
  4583. else
  4584. {
  4585. // TODO: The object handle can be stored in a variable as well
  4586. if( !lvalue->dataType.IsReference() )
  4587. {
  4588. Error(TXT_NOT_VALID_REFERENCE, node);
  4589. return -1;
  4590. }
  4591. if( lvalue->dataType.IsFuncdef() )
  4592. bc->InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  4593. else
  4594. bc->InstrPTR(asBC_REFCPY, lvalue->dataType.GetTypeInfo());
  4595. // Mark variable as initialized
  4596. if( variables )
  4597. {
  4598. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  4599. if( v ) v->isInitialized = true;
  4600. }
  4601. }
  4602. return 0;
  4603. }
  4604. bool asCCompiler::CompileRefCast(asCExprContext *ctx, const asCDataType &to, bool isExplicit, asCScriptNode *node, bool generateCode)
  4605. {
  4606. bool conversionDone = false;
  4607. asCArray<int> ops;
  4608. asUINT n;
  4609. // A ref cast must not remove the constness
  4610. bool isConst = ctx->type.dataType.IsObjectConst();
  4611. // Find a suitable opCast or opImplCast method
  4612. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  4613. for( n = 0; ot && n < ot->methods.GetLength(); n++ )
  4614. {
  4615. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  4616. if( (isExplicit && func->name == "opCast") ||
  4617. func->name == "opImplCast" )
  4618. {
  4619. // Is the operator for the output type?
  4620. if( func->returnType.GetTypeInfo() != to.GetTypeInfo() )
  4621. continue;
  4622. // Can't call a non-const function on a const object
  4623. if( isConst && !func->IsReadOnly() )
  4624. continue;
  4625. ops.PushLast(func->id);
  4626. }
  4627. }
  4628. // Filter the list by constness to remove const methods if there are matching non-const methods
  4629. FilterConst(ops, !isConst);
  4630. // It shouldn't be possible to have more than one
  4631. // TODO: Should be allowed to have different behaviours for const and non-const references
  4632. asASSERT( ops.GetLength() <= 1 );
  4633. // Should only have one behaviour for each output type
  4634. if( ops.GetLength() == 1 )
  4635. {
  4636. conversionDone = true;
  4637. if( generateCode )
  4638. {
  4639. // TODO: runtime optimize: Instead of producing bytecode for checking if the handle is
  4640. // null, we can create a special CALLSYS instruction that checks
  4641. // if the object pointer is null and if so sets the object register
  4642. // to null directly without executing the function.
  4643. //
  4644. // Alternatively I could force the ref cast behaviours be global
  4645. // functions with 1 parameter, even though they should still be
  4646. // registered with RegisterObjectBehaviour()
  4647. if( (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_NOHANDLE))
  4648. {
  4649. // Add code to avoid calling the cast behaviour if the handle is already null,
  4650. // because that will raise a null pointer exception due to the cast behaviour
  4651. // being a class method, and the this pointer cannot be null.
  4652. if (!ctx->type.isVariable)
  4653. {
  4654. Dereference(ctx, true);
  4655. ConvertToVariable(ctx);
  4656. }
  4657. // The reference on the stack will not be used
  4658. ctx->bc.Instr(asBC_PopPtr);
  4659. // TODO: runtime optimize: should have immediate comparison for null pointer
  4660. int offset = AllocateVariable(asCDataType::CreateNullHandle(), true);
  4661. // 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)
  4662. ctx->bc.InstrSHORT(asBC_ClrVPtr, (asWORD)offset);
  4663. ctx->bc.InstrW_W(asBC_CmpPtr, ctx->type.stackOffset, offset);
  4664. DeallocateVariable(offset);
  4665. int afterLabel = nextLabel++;
  4666. ctx->bc.InstrDWORD(asBC_JZ, afterLabel);
  4667. // Call the cast operator
  4668. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4669. ctx->bc.Instr(asBC_RDSPtr);
  4670. ctx->type.dataType.MakeReference(false);
  4671. asCArray<asCExprContext *> args;
  4672. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  4673. ctx->bc.Instr(asBC_PopPtr);
  4674. int endLabel = nextLabel++;
  4675. ctx->bc.InstrINT(asBC_JMP, endLabel);
  4676. ctx->bc.Label((short)afterLabel);
  4677. // Make a NULL pointer
  4678. ctx->bc.InstrSHORT(asBC_ClrVPtr, ctx->type.stackOffset);
  4679. ctx->bc.Label((short)endLabel);
  4680. // Push the reference to the handle on the stack
  4681. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4682. }
  4683. else
  4684. {
  4685. // Value types cannot be null, so there is no need to check for this.
  4686. // Likewise for reference types that are registered with asOBJ_NOHANDLE
  4687. // as those are only expected as registered global properties that cannot
  4688. // be modified anyway.
  4689. // Call the cast operator
  4690. asCArray<asCExprContext *> args;
  4691. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  4692. }
  4693. }
  4694. else
  4695. {
  4696. asCScriptFunction *func = engine->scriptFunctions[ops[0]];
  4697. ctx->type.Set(func->returnType);
  4698. }
  4699. }
  4700. else if( ops.GetLength() == 0 && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  4701. {
  4702. // Check for the generic ref cast method: void opCast(?&out)
  4703. for( n = 0; ot && n < ot->methods.GetLength(); n++ )
  4704. {
  4705. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  4706. if( (isExplicit && func->name == "opCast") ||
  4707. func->name == "opImplCast" )
  4708. {
  4709. // Does the operator take the ?&out parameter?
  4710. if( func->returnType.GetTokenType() != ttVoid ||
  4711. func->parameterTypes.GetLength() != 1 ||
  4712. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  4713. func->inOutFlags[0] != asTM_OUTREF )
  4714. continue;
  4715. ops.PushLast(func->id);
  4716. }
  4717. }
  4718. // It shouldn't be possible to have more than one
  4719. // TODO: Should be allowed to have different implementations for const and non-const references
  4720. asASSERT( ops.GetLength() <= 1 );
  4721. if( ops.GetLength() == 1 )
  4722. {
  4723. conversionDone = true;
  4724. if( generateCode )
  4725. {
  4726. asASSERT(to.IsObjectHandle());
  4727. int afterLabel = 0;
  4728. bool doNullCheck = false;
  4729. asCExprContext tmp(engine);
  4730. if ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_NOHANDLE))
  4731. {
  4732. tmp.bc.AddCode(&ctx->bc);
  4733. tmp.Merge(ctx);
  4734. // Add code to avoid calling the cast behaviour if the handle is already null,
  4735. // because that will raise a null pointer exception due to the cast behaviour
  4736. // being a class method, and the this pointer cannot be null.
  4737. doNullCheck = true;
  4738. if (!ctx->type.isVariable)
  4739. {
  4740. Dereference(&tmp, true);
  4741. ConvertToVariable(&tmp);
  4742. }
  4743. // The reference on the stack will not be used
  4744. tmp.bc.Instr(asBC_PopPtr);
  4745. // TODO: runtime optimize: should have immediate comparison for null pointer
  4746. int offset = AllocateVariable(asCDataType::CreateNullHandle(), true);
  4747. // 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)
  4748. tmp.bc.InstrSHORT(asBC_ClrVPtr, (asWORD)offset);
  4749. tmp.bc.InstrW_W(asBC_CmpPtr, tmp.type.stackOffset, offset);
  4750. DeallocateVariable(offset);
  4751. afterLabel = nextLabel++;
  4752. tmp.bc.InstrDWORD(asBC_JZ, afterLabel);
  4753. // Place the object pointer on the stack
  4754. ctx->bc.InstrSHORT(asBC_PSF, (short)tmp.type.stackOffset);
  4755. }
  4756. // Allocate a temporary variable of the requested handle type
  4757. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  4758. // Pass the reference of that variable to the function as output parameter
  4759. asCDataType toRef(to);
  4760. toRef.MakeReference(true);
  4761. asCArray<asCExprContext *> args;
  4762. asCExprContext arg(engine);
  4763. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  4764. // Don't mark the variable as temporary, so it won't be freed too early
  4765. arg.type.SetVariable(toRef, stackOffset, false);
  4766. arg.type.isLValue = true;
  4767. arg.type.isExplicitHandle = true;
  4768. args.PushLast(&arg);
  4769. // Call the behaviour method
  4770. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  4771. if (doNullCheck)
  4772. {
  4773. // Add the call after the null check
  4774. tmp.bc.AddCode(&ctx->bc);
  4775. ctx->bc.AddCode(&tmp.bc);
  4776. int endLabel = nextLabel++;
  4777. ctx->bc.InstrINT(asBC_JMP, endLabel);
  4778. ctx->bc.Label((short)afterLabel);
  4779. // Make a NULL pointer
  4780. ctx->bc.InstrSHORT(asBC_ClrVPtr, (short)stackOffset);
  4781. ctx->bc.Label((short)endLabel);
  4782. }
  4783. // Use the reference to the variable as the result of the expression
  4784. // Now we can mark the variable as temporary
  4785. ctx->type.SetVariable(toRef, stackOffset, true);
  4786. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  4787. }
  4788. else
  4789. {
  4790. // All casts are legal
  4791. ctx->type.Set(to);
  4792. }
  4793. }
  4794. }
  4795. // If the script object didn't implement a matching opCast or opImplCast
  4796. // then check if the desired type is part of the hierarchy
  4797. if( !conversionDone && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) )
  4798. {
  4799. // We need it to be a reference
  4800. if( !ctx->type.dataType.IsReference() )
  4801. {
  4802. asCDataType toRef = ctx->type.dataType;
  4803. toRef.MakeReference(true);
  4804. ImplicitConversion(ctx, toRef, 0, isExplicit ? asIC_EXPLICIT_REF_CAST : asIC_IMPLICIT_CONV, generateCode);
  4805. }
  4806. if( isExplicit )
  4807. {
  4808. // Allow dynamic cast between object handles (only for script objects).
  4809. // At run time this may result in a null handle,
  4810. // which when used will throw an exception
  4811. conversionDone = true;
  4812. if( generateCode )
  4813. {
  4814. ctx->bc.InstrDWORD(asBC_Cast, engine->GetTypeIdFromDataType(to));
  4815. // Allocate a temporary variable for the returned object
  4816. int returnOffset = AllocateVariable(to, true);
  4817. // Move the pointer from the object register to the temporary variable
  4818. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  4819. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  4820. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4821. ctx->type.SetVariable(to, returnOffset, true);
  4822. ctx->type.dataType.MakeReference(true);
  4823. }
  4824. else
  4825. {
  4826. ctx->type.dataType = to;
  4827. ctx->type.dataType.MakeReference(true);
  4828. }
  4829. }
  4830. else
  4831. {
  4832. if( CastToObjectType(ctx->type.dataType.GetTypeInfo())->DerivesFrom(to.GetTypeInfo()) )
  4833. {
  4834. conversionDone = true;
  4835. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  4836. }
  4837. }
  4838. // A ref cast must not remove the constness
  4839. if( isConst )
  4840. ctx->type.dataType.MakeHandleToConst(true);
  4841. }
  4842. return conversionDone;
  4843. }
  4844. asUINT asCCompiler::ImplicitConvPrimitiveToPrimitive(asCExprContext *ctx, const asCDataType &toOrig, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  4845. {
  4846. asCDataType to = toOrig;
  4847. to.MakeReference(false);
  4848. asASSERT( !ctx->type.dataType.IsReference() );
  4849. // Maybe no conversion is needed
  4850. if( to.IsEqualExceptConst(ctx->type.dataType) )
  4851. {
  4852. // A primitive is const or not
  4853. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4854. return asCC_NO_CONV;
  4855. }
  4856. // Is the conversion an ambiguous enum value?
  4857. if( ctx->enumValue != "" )
  4858. {
  4859. if( to.IsEnumType() )
  4860. {
  4861. // Attempt to resolve an ambiguous enum value
  4862. asCDataType out;
  4863. asDWORD value;
  4864. if( builder->GetEnumValueFromType(CastToEnumType(to.GetTypeInfo()), ctx->enumValue.AddressOf(), out, value) )
  4865. {
  4866. ctx->type.SetConstantDW(out, value);
  4867. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4868. // Reset the enum value since we no longer need it
  4869. ctx->enumValue = "";
  4870. // It wasn't really a conversion. The compiler just resolved the ambiguity (or not)
  4871. return asCC_NO_CONV;
  4872. }
  4873. }
  4874. // The enum value is ambiguous
  4875. if( node && generateCode )
  4876. Error(TXT_FOUND_MULTIPLE_ENUM_VALUES, node);
  4877. // Set a dummy to allow the compiler to try to continue the conversion
  4878. ctx->type.SetDummy();
  4879. }
  4880. // Determine the cost of this conversion
  4881. asUINT cost = asCC_NO_CONV;
  4882. if( (to.IsIntegerType() || to.IsUnsignedType()) && (ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  4883. cost = asCC_INT_FLOAT_CONV;
  4884. else if ((to.IsFloatType() || to.IsDoubleType()) && (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType()))
  4885. cost = asCC_INT_FLOAT_CONV;
  4886. else if (ctx->type.dataType.IsEnumType() && to.IsIntegerType() && to.GetSizeInMemoryBytes() == ctx->type.dataType.GetSizeInMemoryBytes() )
  4887. cost = asCC_ENUM_SAME_SIZE_CONV;
  4888. else if (ctx->type.dataType.IsEnumType() && to.IsIntegerType() && to.GetSizeInMemoryBytes() != ctx->type.dataType.GetSizeInMemoryBytes())
  4889. cost = asCC_ENUM_DIFF_SIZE_CONV;
  4890. else if( to.IsUnsignedType() && ctx->type.dataType.IsIntegerType() )
  4891. cost = asCC_SIGNED_CONV;
  4892. else if( to.IsIntegerType() && ctx->type.dataType.IsUnsignedType() )
  4893. cost = asCC_SIGNED_CONV;
  4894. else if( to.GetSizeInMemoryBytes() != ctx->type.dataType.GetSizeInMemoryBytes() )
  4895. cost = asCC_PRIMITIVE_SIZE_CONV;
  4896. // Start by implicitly converting constant values
  4897. if( ctx->type.isConstant )
  4898. {
  4899. ImplicitConversionConstant(ctx, to, node, convType);
  4900. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4901. return cost;
  4902. }
  4903. // Allow implicit conversion between numbers
  4904. if( generateCode )
  4905. {
  4906. // When generating the code the decision has already been made, so we don't bother determining the cost
  4907. // Convert smaller types to 32bit first
  4908. int s = ctx->type.dataType.GetSizeInMemoryBytes();
  4909. if( s < 4 )
  4910. {
  4911. ConvertToTempVariable(ctx);
  4912. if( ctx->type.dataType.IsIntegerType() )
  4913. {
  4914. if( s == 1 )
  4915. ctx->bc.InstrSHORT(asBC_sbTOi, ctx->type.stackOffset);
  4916. else if( s == 2 )
  4917. ctx->bc.InstrSHORT(asBC_swTOi, ctx->type.stackOffset);
  4918. ctx->type.dataType.SetTokenType(ttInt);
  4919. }
  4920. else if( ctx->type.dataType.IsUnsignedType() )
  4921. {
  4922. if( s == 1 )
  4923. ctx->bc.InstrSHORT(asBC_ubTOi, ctx->type.stackOffset);
  4924. else if( s == 2 )
  4925. ctx->bc.InstrSHORT(asBC_uwTOi, ctx->type.stackOffset);
  4926. ctx->type.dataType.SetTokenType(ttUInt);
  4927. }
  4928. }
  4929. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1 && !to.IsEnumType()) ||
  4930. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  4931. {
  4932. if( ctx->type.dataType.IsIntegerType() ||
  4933. ctx->type.dataType.IsUnsignedType() )
  4934. {
  4935. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4936. {
  4937. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4938. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  4939. }
  4940. else
  4941. {
  4942. ConvertToTempVariable(ctx);
  4943. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4944. int offset = AllocateVariable(to, true);
  4945. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  4946. ctx->type.SetVariable(to, offset, true);
  4947. }
  4948. }
  4949. else if( ctx->type.dataType.IsFloatType() )
  4950. {
  4951. ConvertToTempVariable(ctx);
  4952. ctx->bc.InstrSHORT(asBC_fTOi, ctx->type.stackOffset);
  4953. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4954. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  4955. if( convType != asIC_EXPLICIT_VAL_CAST )
  4956. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4957. }
  4958. else if( ctx->type.dataType.IsDoubleType() )
  4959. {
  4960. ConvertToTempVariable(ctx);
  4961. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4962. int offset = AllocateVariable(to, true);
  4963. ctx->bc.InstrW_W(asBC_dTOi, offset, ctx->type.stackOffset);
  4964. ctx->type.SetVariable(to, offset, true);
  4965. if( convType != asIC_EXPLICIT_VAL_CAST )
  4966. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4967. }
  4968. // Convert to smaller integer if necessary
  4969. s = to.GetSizeInMemoryBytes();
  4970. if( s < 4 )
  4971. {
  4972. ConvertToTempVariable(ctx);
  4973. if( s == 1 )
  4974. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  4975. else if( s == 2 )
  4976. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  4977. }
  4978. }
  4979. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  4980. {
  4981. if( ctx->type.dataType.IsIntegerType() ||
  4982. ctx->type.dataType.IsUnsignedType() )
  4983. {
  4984. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4985. {
  4986. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4987. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  4988. }
  4989. else
  4990. {
  4991. ConvertToTempVariable(ctx);
  4992. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4993. int offset = AllocateVariable(to, true);
  4994. if( ctx->type.dataType.IsUnsignedType() )
  4995. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  4996. else
  4997. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  4998. ctx->type.SetVariable(to, offset, true);
  4999. }
  5000. }
  5001. else if( ctx->type.dataType.IsFloatType() )
  5002. {
  5003. ConvertToTempVariable(ctx);
  5004. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5005. int offset = AllocateVariable(to, true);
  5006. ctx->bc.InstrW_W(asBC_fTOi64, offset, ctx->type.stackOffset);
  5007. ctx->type.SetVariable(to, offset, true);
  5008. if( convType != asIC_EXPLICIT_VAL_CAST )
  5009. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5010. }
  5011. else if( ctx->type.dataType.IsDoubleType() )
  5012. {
  5013. ConvertToTempVariable(ctx);
  5014. ctx->bc.InstrSHORT(asBC_dTOi64, ctx->type.stackOffset);
  5015. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5016. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5017. if( convType != asIC_EXPLICIT_VAL_CAST )
  5018. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5019. }
  5020. }
  5021. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  5022. {
  5023. if( ctx->type.dataType.IsIntegerType() ||
  5024. ctx->type.dataType.IsUnsignedType() )
  5025. {
  5026. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5027. {
  5028. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5029. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5030. }
  5031. else
  5032. {
  5033. ConvertToTempVariable(ctx);
  5034. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5035. int offset = AllocateVariable(to, true);
  5036. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  5037. ctx->type.SetVariable(to, offset, true);
  5038. }
  5039. }
  5040. else if( ctx->type.dataType.IsFloatType() )
  5041. {
  5042. ConvertToTempVariable(ctx);
  5043. ctx->bc.InstrSHORT(asBC_fTOu, ctx->type.stackOffset);
  5044. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5045. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5046. if( convType != asIC_EXPLICIT_VAL_CAST )
  5047. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5048. }
  5049. else if( ctx->type.dataType.IsDoubleType() )
  5050. {
  5051. ConvertToTempVariable(ctx);
  5052. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5053. int offset = AllocateVariable(to, true);
  5054. ctx->bc.InstrW_W(asBC_dTOu, offset, ctx->type.stackOffset);
  5055. ctx->type.SetVariable(to, offset, true);
  5056. if( convType != asIC_EXPLICIT_VAL_CAST )
  5057. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5058. }
  5059. // Convert to smaller integer if necessary
  5060. s = to.GetSizeInMemoryBytes();
  5061. if( s < 4 )
  5062. {
  5063. ConvertToTempVariable(ctx);
  5064. if( s == 1 )
  5065. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  5066. else if( s == 2 )
  5067. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  5068. }
  5069. }
  5070. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  5071. {
  5072. if( ctx->type.dataType.IsIntegerType() ||
  5073. ctx->type.dataType.IsUnsignedType() )
  5074. {
  5075. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5076. {
  5077. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5078. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5079. }
  5080. else
  5081. {
  5082. ConvertToTempVariable(ctx);
  5083. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5084. int offset = AllocateVariable(to, true);
  5085. if( ctx->type.dataType.IsUnsignedType() )
  5086. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  5087. else
  5088. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  5089. ctx->type.SetVariable(to, offset, true);
  5090. }
  5091. }
  5092. else if( ctx->type.dataType.IsFloatType() )
  5093. {
  5094. ConvertToTempVariable(ctx);
  5095. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5096. int offset = AllocateVariable(to, true);
  5097. ctx->bc.InstrW_W(asBC_fTOu64, offset, ctx->type.stackOffset);
  5098. ctx->type.SetVariable(to, offset, true);
  5099. if( convType != asIC_EXPLICIT_VAL_CAST )
  5100. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5101. }
  5102. else if( ctx->type.dataType.IsDoubleType() )
  5103. {
  5104. ConvertToTempVariable(ctx);
  5105. ctx->bc.InstrSHORT(asBC_dTOu64, ctx->type.stackOffset);
  5106. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5107. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5108. if( convType != asIC_EXPLICIT_VAL_CAST )
  5109. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  5110. }
  5111. }
  5112. else if( to.IsFloatType() )
  5113. {
  5114. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5115. {
  5116. ConvertToTempVariable(ctx);
  5117. ctx->bc.InstrSHORT(asBC_iTOf, ctx->type.stackOffset);
  5118. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5119. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5120. }
  5121. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5122. {
  5123. ConvertToTempVariable(ctx);
  5124. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5125. int offset = AllocateVariable(to, true);
  5126. ctx->bc.InstrW_W(asBC_i64TOf, offset, ctx->type.stackOffset);
  5127. ctx->type.SetVariable(to, offset, true);
  5128. }
  5129. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5130. {
  5131. ConvertToTempVariable(ctx);
  5132. ctx->bc.InstrSHORT(asBC_uTOf, ctx->type.stackOffset);
  5133. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5134. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5135. }
  5136. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5137. {
  5138. ConvertToTempVariable(ctx);
  5139. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5140. int offset = AllocateVariable(to, true);
  5141. ctx->bc.InstrW_W(asBC_u64TOf, offset, ctx->type.stackOffset);
  5142. ctx->type.SetVariable(to, offset, true);
  5143. }
  5144. else if( ctx->type.dataType.IsDoubleType() )
  5145. {
  5146. ConvertToTempVariable(ctx);
  5147. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5148. int offset = AllocateVariable(to, true);
  5149. ctx->bc.InstrW_W(asBC_dTOf, offset, ctx->type.stackOffset);
  5150. ctx->type.SetVariable(to, offset, true);
  5151. }
  5152. }
  5153. else if( to.IsDoubleType() )
  5154. {
  5155. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5156. {
  5157. ConvertToTempVariable(ctx);
  5158. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5159. int offset = AllocateVariable(to, true);
  5160. ctx->bc.InstrW_W(asBC_iTOd, offset, ctx->type.stackOffset);
  5161. ctx->type.SetVariable(to, offset, true);
  5162. }
  5163. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5164. {
  5165. ConvertToTempVariable(ctx);
  5166. ctx->bc.InstrSHORT(asBC_i64TOd, ctx->type.stackOffset);
  5167. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5168. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5169. }
  5170. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  5171. {
  5172. ConvertToTempVariable(ctx);
  5173. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5174. int offset = AllocateVariable(to, true);
  5175. ctx->bc.InstrW_W(asBC_uTOd, offset, ctx->type.stackOffset);
  5176. ctx->type.SetVariable(to, offset, true);
  5177. }
  5178. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  5179. {
  5180. ConvertToTempVariable(ctx);
  5181. ctx->bc.InstrSHORT(asBC_u64TOd, ctx->type.stackOffset);
  5182. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5183. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5184. }
  5185. else if( ctx->type.dataType.IsFloatType() )
  5186. {
  5187. ConvertToTempVariable(ctx);
  5188. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5189. int offset = AllocateVariable(to, true);
  5190. ctx->bc.InstrW_W(asBC_fTOd, offset, ctx->type.stackOffset);
  5191. ctx->type.SetVariable(to, offset, true);
  5192. }
  5193. }
  5194. }
  5195. else
  5196. {
  5197. if( ((to.IsIntegerType() && !to.IsEnumType()) || to.IsUnsignedType() ||
  5198. to.IsFloatType() || to.IsDoubleType() ||
  5199. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST)) &&
  5200. (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() ||
  5201. ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  5202. {
  5203. ctx->type.dataType.SetTokenType(to.GetTokenType());
  5204. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5205. }
  5206. }
  5207. // Primitive types on the stack, can be const or non-const
  5208. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5209. return cost;
  5210. }
  5211. asUINT asCCompiler::ImplicitConvLambdaToFunc(asCExprContext *ctx, const asCDataType &to, asCScriptNode * /*node*/, EImplicitConv /*convType*/, bool generateCode)
  5212. {
  5213. asASSERT( to.IsFuncdef() && ctx->IsLambda() );
  5214. // Check that the lambda has the correct amount of arguments
  5215. asUINT count = 0;
  5216. asCScriptNode *argNode = ctx->exprNode->firstChild;
  5217. while( argNode->nodeType == snIdentifier )
  5218. {
  5219. count++;
  5220. argNode = argNode->next;
  5221. }
  5222. asASSERT( argNode->nodeType == snStatementBlock );
  5223. asCScriptFunction *funcDef = CastToFuncdefType(to.GetTypeInfo())->funcdef;
  5224. if( funcDef->parameterTypes.GetLength() != count )
  5225. return asCC_NO_CONV;
  5226. // The Lambda can be used as this funcdef
  5227. ctx->type.dataType = to;
  5228. if( generateCode )
  5229. {
  5230. // Build a unique name for the anonymous function
  5231. asCString name;
  5232. if( m_globalVar )
  5233. name.Format("$%s$%d", m_globalVar->name.AddressOf(), numLambdas++);
  5234. else
  5235. name.Format("$%s$%d", outFunc->GetDeclaration(), numLambdas++);
  5236. // Register the lambda with the builder for later compilation
  5237. asCScriptFunction *func = builder->RegisterLambda(ctx->exprNode, script, funcDef, name, outFunc->nameSpace);
  5238. asASSERT( func == 0 || funcDef->IsSignatureExceptNameEqual(func) );
  5239. ctx->bc.InstrPTR(asBC_FuncPtr, func);
  5240. // Clear the expression node as it is no longer valid
  5241. ctx->exprNode = 0;
  5242. }
  5243. return asCC_CONST_CONV;
  5244. }
  5245. asUINT asCCompiler::ImplicitConversion(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  5246. {
  5247. asASSERT( ctx->type.dataType.GetTokenType() != ttUnrecognizedToken ||
  5248. ctx->type.dataType.IsNullHandle() );
  5249. if( to.IsFuncdef() && ctx->IsLambda() )
  5250. return ImplicitConvLambdaToFunc(ctx, to, node, convType, generateCode);
  5251. // No conversion from void to any other type
  5252. if( ctx->type.dataType.GetTokenType() == ttVoid )
  5253. return asCC_NO_CONV;
  5254. // No conversion from class method to any type (it requires delegate)
  5255. if( ctx->IsClassMethod() )
  5256. return asCC_NO_CONV;
  5257. // Do we want a var type?
  5258. if( to.GetTokenType() == ttQuestion )
  5259. {
  5260. // Any type can be converted to a var type, but only when not generating code
  5261. asASSERT( !generateCode );
  5262. ctx->type.dataType = to;
  5263. return asCC_VARIABLE_CONV;
  5264. }
  5265. // Do we want a primitive?
  5266. else if( to.IsPrimitive() )
  5267. {
  5268. if( !ctx->type.dataType.IsPrimitive() )
  5269. return ImplicitConvObjectToPrimitive(ctx, to, node, convType, generateCode);
  5270. else
  5271. return ImplicitConvPrimitiveToPrimitive(ctx, to, node, convType, generateCode);
  5272. }
  5273. else // The target is a complex type
  5274. {
  5275. if( ctx->type.dataType.IsPrimitive() )
  5276. return ImplicitConvPrimitiveToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  5277. else if( ctx->type.IsNullConstant() || ctx->type.dataType.GetTypeInfo() )
  5278. return ImplicitConvObjectToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  5279. }
  5280. return asCC_NO_CONV;
  5281. }
  5282. asUINT asCCompiler::ImplicitConvObjectToPrimitive(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5283. {
  5284. if( ctx->type.isExplicitHandle )
  5285. {
  5286. // An explicit handle cannot be converted to a primitive
  5287. if( convType != asIC_IMPLICIT_CONV && node )
  5288. {
  5289. asCString str;
  5290. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5291. Error(str, node);
  5292. }
  5293. return asCC_NO_CONV;
  5294. }
  5295. // TODO: Must use the const cast behaviour if the object is read-only
  5296. // Find matching value cast behaviours
  5297. // Here we're only interested in those that convert the type to a primitive type
  5298. asCArray<int> funcs;
  5299. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  5300. if( ot == 0 )
  5301. {
  5302. if( convType != asIC_IMPLICIT_CONV && node )
  5303. {
  5304. asCString str;
  5305. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5306. Error(str, node);
  5307. }
  5308. return asCC_NO_CONV;
  5309. }
  5310. if( convType == asIC_EXPLICIT_VAL_CAST )
  5311. {
  5312. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5313. {
  5314. // accept both implicit and explicit cast
  5315. asCScriptFunction *mthd = engine->scriptFunctions[ot->methods[n]];
  5316. if( (mthd->name == "opConv" || mthd->name == "opImplConv") &&
  5317. mthd->parameterTypes.GetLength() == 0 &&
  5318. mthd->returnType.IsPrimitive() )
  5319. funcs.PushLast(ot->methods[n]);
  5320. }
  5321. }
  5322. else
  5323. {
  5324. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5325. {
  5326. // accept only implicit cast
  5327. asCScriptFunction *mthd = engine->scriptFunctions[ot->methods[n]];
  5328. if( mthd->name == "opImplConv" &&
  5329. mthd->parameterTypes.GetLength() == 0 &&
  5330. mthd->returnType.IsPrimitive() )
  5331. funcs.PushLast(ot->methods[n]);
  5332. }
  5333. }
  5334. int funcId = 0;
  5335. if( to.IsMathType() )
  5336. {
  5337. // This matrix describes the priorities of the types to search for, for each target type
  5338. // The first column is the target type, the priorities goes from left to right
  5339. eTokenType matchMtx[10][10] =
  5340. {
  5341. {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  5342. {ttFloat, ttDouble, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  5343. {ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  5344. {ttUInt64, ttInt64, ttUInt, ttInt, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  5345. {ttInt, ttUInt, ttInt64, ttUInt64, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  5346. {ttUInt, ttInt, ttUInt64, ttInt64, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  5347. {ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttInt8, ttUInt8, ttDouble, ttFloat},
  5348. {ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttUInt8, ttInt8, ttDouble, ttFloat},
  5349. {ttInt8, ttUInt8, ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttDouble, ttFloat},
  5350. {ttUInt8, ttInt8, ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttDouble, ttFloat},
  5351. };
  5352. // Which row to use?
  5353. eTokenType *row = 0;
  5354. for( unsigned int type = 0; type < 10; type++ )
  5355. {
  5356. if( to.GetTokenType() == matchMtx[type][0] )
  5357. {
  5358. row = &matchMtx[type][0];
  5359. break;
  5360. }
  5361. }
  5362. // Find the best matching cast operator
  5363. if( row )
  5364. {
  5365. asCDataType target(to);
  5366. // Priority goes from left to right in the matrix
  5367. for( unsigned int attempt = 0; attempt < 10 && funcId == 0; attempt++ )
  5368. {
  5369. target.SetTokenType(row[attempt]);
  5370. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  5371. {
  5372. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  5373. if( descr->returnType.IsEqualExceptRefAndConst(target) )
  5374. {
  5375. funcId = funcs[n];
  5376. break;
  5377. }
  5378. }
  5379. }
  5380. }
  5381. }
  5382. else
  5383. {
  5384. // Only accept the exact conversion for non-math types
  5385. // Find the matching cast operator
  5386. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  5387. {
  5388. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  5389. if( descr->returnType.IsEqualExceptRefAndConst(to) )
  5390. {
  5391. funcId = funcs[n];
  5392. break;
  5393. }
  5394. }
  5395. }
  5396. // Did we find a suitable function?
  5397. if( funcId != 0 )
  5398. {
  5399. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  5400. if( generateCode )
  5401. {
  5402. Dereference(ctx, true);
  5403. PerformFunctionCall(funcId, ctx);
  5404. }
  5405. else
  5406. ctx->type.Set(descr->returnType);
  5407. // Allow one more implicit conversion to another primitive type
  5408. return asCC_OBJ_TO_PRIMITIVE_CONV + ImplicitConversion(ctx, to, node, convType, generateCode, false);
  5409. }
  5410. // TODO: clean-up: This part is similar to what is in ImplicitConvObjectValue
  5411. // If no direct conversion is found we should look for the generic form 'void opConv(?&out)'
  5412. funcs.SetLength(0);
  5413. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  5414. {
  5415. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5416. if( ((convType == asIC_EXPLICIT_VAL_CAST) && func->name == "opConv") ||
  5417. func->name == "opImplConv" )
  5418. {
  5419. // Does the operator take the ?&out parameter?
  5420. if( func->returnType != asCDataType::CreatePrimitive(ttVoid, false) ||
  5421. func->parameterTypes.GetLength() != 1 ||
  5422. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  5423. func->inOutFlags[0] != asTM_OUTREF )
  5424. continue;
  5425. funcs.PushLast(ot->methods[n]);
  5426. }
  5427. }
  5428. // TODO: If there are multiple valid value casts, then we must choose the most appropriate one
  5429. asASSERT( funcs.GetLength() <= 1 );
  5430. if( funcs.GetLength() == 1 )
  5431. {
  5432. if( generateCode )
  5433. {
  5434. // Allocate a temporary variable of the requested type
  5435. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  5436. CallDefaultConstructor(to, stackOffset, IsVariableOnHeap(stackOffset), &ctx->bc, node);
  5437. // Pass the reference of that variable to the function as output parameter
  5438. asCDataType toRef(to);
  5439. toRef.MakeReference(true);
  5440. toRef.MakeReadOnly(false);
  5441. asCArray<asCExprContext *> args;
  5442. asCExprContext arg(engine);
  5443. // Don't mark the variable as temporary, so it won't be freed too early
  5444. arg.type.SetVariable(toRef, stackOffset, false);
  5445. arg.type.isLValue = true;
  5446. arg.exprNode = node;
  5447. args.PushLast(&arg);
  5448. // Call the behaviour method
  5449. MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  5450. // Use the reference to the variable as the result of the expression
  5451. // Now we can mark the variable as temporary
  5452. toRef.MakeReference(false);
  5453. ctx->type.SetVariable(toRef, stackOffset, true);
  5454. }
  5455. else
  5456. ctx->type.Set(to);
  5457. return asCC_OBJ_TO_PRIMITIVE_CONV;
  5458. }
  5459. if( convType != asIC_IMPLICIT_CONV && node )
  5460. {
  5461. asCString str;
  5462. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5463. Error(str, node);
  5464. }
  5465. return asCC_NO_CONV;
  5466. }
  5467. asUINT asCCompiler::ImplicitConvObjectRef(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5468. {
  5469. // Convert null to any object type handle, but not to a non-handle type
  5470. if( ctx->type.IsNullConstant() && ctx->methodName == "" )
  5471. {
  5472. if( to.IsObjectHandle() )
  5473. {
  5474. ctx->type.dataType = to;
  5475. return asCC_REF_CONV;
  5476. }
  5477. return asCC_NO_CONV;
  5478. }
  5479. asASSERT(ctx->type.dataType.GetTypeInfo() || ctx->methodName != "");
  5480. // First attempt to convert the base type without instantiating another instance
  5481. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() && ctx->methodName == "" )
  5482. {
  5483. // If the to type is an interface and the from type implements it, then we can convert it immediately
  5484. if( ctx->type.dataType.GetTypeInfo()->Implements(to.GetTypeInfo()) )
  5485. {
  5486. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5487. return asCC_REF_CONV;
  5488. }
  5489. // If the to type is a class and the from type derives from it, then we can convert it immediately
  5490. else if( ctx->type.dataType.GetTypeInfo()->DerivesFrom(to.GetTypeInfo()) )
  5491. {
  5492. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5493. return asCC_REF_CONV;
  5494. }
  5495. // If the types are not equal yet, then we may still be able to find a reference cast
  5496. else if( ctx->type.dataType.GetTypeInfo() != to.GetTypeInfo() )
  5497. {
  5498. // We may still be able to find an implicit ref cast behaviour
  5499. CompileRefCast(ctx, to, convType == asIC_EXPLICIT_REF_CAST, node, generateCode);
  5500. // Was the conversion done?
  5501. if( ctx->type.dataType.GetTypeInfo() == to.GetTypeInfo() )
  5502. return asCC_REF_CONV;
  5503. }
  5504. }
  5505. // Convert matching function types
  5506. if( to.IsFuncdef() )
  5507. {
  5508. // If the input expression is already a funcdef, check if it can be converted
  5509. if( ctx->type.dataType.IsFuncdef() &&
  5510. to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() )
  5511. {
  5512. asCScriptFunction *toFunc = CastToFuncdefType(to.GetTypeInfo())->funcdef;
  5513. asCScriptFunction *fromFunc = CastToFuncdefType(ctx->type.dataType.GetTypeInfo())->funcdef;
  5514. if( toFunc->IsSignatureExceptNameEqual(fromFunc) )
  5515. {
  5516. ctx->type.dataType.SetTypeInfo(to.GetTypeInfo());
  5517. return asCC_REF_CONV;
  5518. }
  5519. }
  5520. // If the input expression is a deferred function ref, check if there is a matching func
  5521. if( ctx->methodName != "" )
  5522. {
  5523. // Determine the namespace
  5524. asSNameSpace *ns = 0;
  5525. asCString name = "";
  5526. int pos = ctx->methodName.FindLast("::");
  5527. if( pos >= 0 )
  5528. {
  5529. asCString nsName = ctx->methodName.SubString(0, pos+2);
  5530. // Trim off the last ::
  5531. if( nsName.GetLength() > 2 )
  5532. nsName.SetLength(nsName.GetLength()-2);
  5533. ns = DetermineNameSpace(nsName);
  5534. name = ctx->methodName.SubString(pos+2);
  5535. }
  5536. else
  5537. {
  5538. DetermineNameSpace("");
  5539. name = ctx->methodName;
  5540. }
  5541. asCArray<int> funcs;
  5542. if( ns )
  5543. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  5544. // Check if any of the functions have perfect match
  5545. asCScriptFunction *toFunc = CastToFuncdefType(to.GetTypeInfo())->funcdef;
  5546. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  5547. {
  5548. asCScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  5549. if( toFunc->IsSignatureExceptNameEqual(func) )
  5550. {
  5551. if( generateCode )
  5552. {
  5553. ctx->bc.InstrPTR(asBC_FuncPtr, func);
  5554. // Make sure the identified function is shared if we're compiling a shared function
  5555. if( !func->IsShared() && outFunc->IsShared() )
  5556. {
  5557. asCString msg;
  5558. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, func->GetDeclaration());
  5559. Error(msg, node);
  5560. }
  5561. }
  5562. ctx->type.dataType = asCDataType::CreateType(to.GetTypeInfo(), false);
  5563. return asCC_REF_CONV;
  5564. }
  5565. }
  5566. }
  5567. }
  5568. return asCC_NO_CONV;
  5569. }
  5570. asUINT asCCompiler::ImplicitConvObjectValue(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  5571. {
  5572. asUINT cost = asCC_NO_CONV;
  5573. // If the base type is still different, and we are allowed to instance
  5574. // another object then we can try an implicit value cast
  5575. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() )
  5576. {
  5577. // TODO: Implement support for implicit constructor/factory
  5578. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  5579. if( ot == 0 )
  5580. return cost;
  5581. asCArray<int> funcs;
  5582. if( convType == asIC_EXPLICIT_VAL_CAST )
  5583. {
  5584. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5585. {
  5586. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5587. // accept both implicit and explicit cast
  5588. if( (func->name == "opConv" ||
  5589. func->name == "opImplConv") &&
  5590. func->returnType.GetTypeInfo() == to.GetTypeInfo() &&
  5591. func->parameterTypes.GetLength() == 0 )
  5592. funcs.PushLast(ot->methods[n]);
  5593. }
  5594. }
  5595. else
  5596. {
  5597. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  5598. {
  5599. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5600. // accept only implicit cast
  5601. if( func->name == "opImplConv" &&
  5602. func->returnType.GetTypeInfo() == to.GetTypeInfo() &&
  5603. func->parameterTypes.GetLength() == 0 )
  5604. funcs.PushLast(ot->methods[n]);
  5605. }
  5606. }
  5607. // TODO: If there are multiple valid value casts, then we must choose the most appropriate one
  5608. asASSERT( funcs.GetLength() <= 1 );
  5609. if( funcs.GetLength() == 1 )
  5610. {
  5611. asCScriptFunction *f = builder->GetFunctionDescription(funcs[0]);
  5612. if( generateCode )
  5613. {
  5614. Dereference(ctx, true);
  5615. bool useVariable = false;
  5616. int stackOffset = 0;
  5617. if( f->DoesReturnOnStack() )
  5618. {
  5619. useVariable = true;
  5620. stackOffset = AllocateVariable(f->returnType, true);
  5621. // Push the pointer to the pre-allocated space for the return value
  5622. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  5623. // The object pointer is already on the stack, but should be the top
  5624. // one, so we need to swap the pointers in order to get the correct
  5625. ctx->bc.Instr(asBC_SwapPtr);
  5626. }
  5627. PerformFunctionCall(funcs[0], ctx, false, 0, 0, useVariable, stackOffset);
  5628. }
  5629. else
  5630. ctx->type.Set(f->returnType);
  5631. cost = asCC_TO_OBJECT_CONV;
  5632. }
  5633. else
  5634. {
  5635. // TODO: cleanup: This part is similar to the second half of ImplicitConvObjectToPrimitive
  5636. // Look for a value cast with variable type
  5637. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  5638. {
  5639. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  5640. if( ((convType == asIC_EXPLICIT_VAL_CAST) && func->name == "opConv") ||
  5641. func->name == "opImplConv" )
  5642. {
  5643. // Does the operator take the ?&out parameter?
  5644. if( func->returnType != asCDataType::CreatePrimitive(ttVoid, false) ||
  5645. func->parameterTypes.GetLength() != 1 ||
  5646. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  5647. func->inOutFlags[0] != asTM_OUTREF )
  5648. continue;
  5649. funcs.PushLast(ot->methods[n]);
  5650. }
  5651. }
  5652. // TODO: If there are multiple valid value casts, then we must choose the most appropriate one
  5653. asASSERT( funcs.GetLength() <= 1 );
  5654. if( funcs.GetLength() == 1 )
  5655. {
  5656. cost = asCC_TO_OBJECT_CONV;
  5657. if( generateCode )
  5658. {
  5659. // Allocate a temporary variable of the requested type
  5660. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  5661. CallDefaultConstructor(to, stackOffset, IsVariableOnHeap(stackOffset), &ctx->bc, node);
  5662. // Pass the reference of that variable to the function as output parameter
  5663. asCDataType toRef(to);
  5664. toRef.MakeReference(false);
  5665. asCExprContext arg(engine);
  5666. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  5667. // If this an object on the heap, the pointer must be dereferenced
  5668. if( IsVariableOnHeap(stackOffset) )
  5669. arg.bc.Instr(asBC_RDSPtr);
  5670. // Don't mark the variable as temporary, so it won't be freed too early
  5671. arg.type.SetVariable(toRef, stackOffset, false);
  5672. arg.type.isLValue = true;
  5673. arg.exprNode = node;
  5674. // Mark the argument as clean, so that MakeFunctionCall knows it
  5675. // doesn't have to make a copy of it in order to protect the value
  5676. arg.isCleanArg = true;
  5677. // Call the behaviour method
  5678. asCArray<asCExprContext *> args;
  5679. args.PushLast(&arg);
  5680. MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  5681. // Use the reference to the variable as the result of the expression
  5682. // Now we can mark the variable as temporary
  5683. ctx->type.SetVariable(toRef, stackOffset, true);
  5684. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  5685. }
  5686. else
  5687. {
  5688. // All casts are legal
  5689. ctx->type.Set(to);
  5690. }
  5691. }
  5692. }
  5693. }
  5694. return cost;
  5695. }
  5696. asUINT asCCompiler::ImplicitConvObjectToObject(asCExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  5697. {
  5698. // First try a ref cast
  5699. asUINT cost = ImplicitConvObjectRef(ctx, to, node, convType, generateCode);
  5700. // If the desired type is an asOBJ_ASHANDLE then we'll assume it is allowed to implicitly
  5701. // construct the object through any of the available constructors
  5702. if( to.GetTypeInfo() && (to.GetTypeInfo()->flags & asOBJ_ASHANDLE) && to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() && allowObjectConstruct )
  5703. {
  5704. asCArray<int> funcs;
  5705. funcs = CastToObjectType(to.GetTypeInfo())->beh.constructors;
  5706. asCArray<asCExprContext *> args;
  5707. args.PushLast(ctx);
  5708. cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, node, 0, 0, 0, false, true, false);
  5709. // Did we find a matching constructor?
  5710. if( funcs.GetLength() == 1 )
  5711. {
  5712. if( generateCode )
  5713. {
  5714. // If the ASHANDLE receives a variable type parameter, then we need to
  5715. // make sure the expression is treated as a handle and not as a value
  5716. asCScriptFunction *func = engine->scriptFunctions[funcs[0]];
  5717. if( func->parameterTypes[0].GetTokenType() == ttQuestion )
  5718. {
  5719. if( !ctx->type.isExplicitHandle )
  5720. {
  5721. asCDataType toHandle = ctx->type.dataType;
  5722. toHandle.MakeHandle(true);
  5723. toHandle.MakeReference(true);
  5724. toHandle.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  5725. ImplicitConversion(ctx, toHandle, node, asIC_IMPLICIT_CONV, true, false);
  5726. asASSERT( ctx->type.dataType.IsObjectHandle() );
  5727. }
  5728. ctx->type.isExplicitHandle = true;
  5729. }
  5730. // TODO: This should really reuse the code from CompileConstructCall
  5731. // Allocate the new object
  5732. asCExprValue tempObj;
  5733. tempObj.dataType = to;
  5734. tempObj.dataType.MakeReference(false);
  5735. tempObj.stackOffset = (short)AllocateVariable(tempObj.dataType, true);
  5736. tempObj.dataType.MakeReference(true);
  5737. tempObj.isTemporary = true;
  5738. tempObj.isVariable = true;
  5739. bool onHeap = IsVariableOnHeap(tempObj.stackOffset);
  5740. // Push the address of the object on the stack
  5741. asCExprContext e(engine);
  5742. if( onHeap )
  5743. e.bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  5744. PrepareFunctionCall(funcs[0], &e.bc, args);
  5745. MoveArgsToStack(funcs[0], &e.bc, args, false);
  5746. // If the object is allocated on the stack, then call the constructor as a normal function
  5747. if( onHeap )
  5748. {
  5749. int offset = 0;
  5750. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  5751. offset = descr->parameterTypes[0].GetSizeOnStackDWords();
  5752. e.bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  5753. }
  5754. else
  5755. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  5756. PerformFunctionCall(funcs[0], &e, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  5757. // Add tag that the object has been initialized
  5758. e.bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  5759. // The constructor doesn't return anything,
  5760. // so we have to manually inform the type of
  5761. // the return value
  5762. e.type = tempObj;
  5763. if( !onHeap )
  5764. e.type.dataType.MakeReference(false);
  5765. // Push the address of the object on the stack again
  5766. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  5767. MergeExprBytecodeAndType(ctx, &e);
  5768. }
  5769. else
  5770. {
  5771. ctx->type.Set(asCDataType::CreateType(to.GetTypeInfo(), false));
  5772. }
  5773. }
  5774. }
  5775. // If the base type is still different, and we are allowed to instance
  5776. // another object then we can try an implicit value cast
  5777. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() && allowObjectConstruct )
  5778. {
  5779. // Attempt implicit value cast
  5780. cost = ImplicitConvObjectValue(ctx, to, node, convType, generateCode);
  5781. }
  5782. // If we still haven't converted the base type to the correct type, then there is
  5783. // no need to continue as it is not possible to do the conversion
  5784. if( to.GetTypeInfo() != ctx->type.dataType.GetTypeInfo() )
  5785. return asCC_NO_CONV;
  5786. if( to.IsObjectHandle() )
  5787. {
  5788. // There is no extra cost in converting to a handle
  5789. // reference to handle -> handle
  5790. // reference -> handle
  5791. // object -> handle
  5792. // handle -> reference to handle
  5793. // reference -> reference to handle
  5794. // object -> reference to handle
  5795. // TODO: If the type is handle, then we can't use IsReadOnly to determine the constness of the basetype
  5796. // If the rvalue is a handle to a const object, then
  5797. // the lvalue must also be a handle to a const object
  5798. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() )
  5799. {
  5800. if( convType != asIC_IMPLICIT_CONV )
  5801. {
  5802. asASSERT(node);
  5803. asCString str;
  5804. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  5805. Error(str, node);
  5806. }
  5807. }
  5808. if( !ctx->type.dataType.IsObjectHandle() )
  5809. {
  5810. // An object type can be directly converted to a handle of the
  5811. // same type by doing a ref copy to a new variable
  5812. if( ctx->type.dataType.SupportHandles() )
  5813. {
  5814. asCDataType dt = ctx->type.dataType;
  5815. dt.MakeHandle(true);
  5816. dt.MakeReference(false);
  5817. if( generateCode )
  5818. {
  5819. // If the expression is already a local variable, then it is not
  5820. // necessary to do a ref copy, as the ref objects on the stack are
  5821. // really handles, only the handles cannot be modified.
  5822. if( ctx->type.isVariable )
  5823. {
  5824. bool isHandleToConst = ctx->type.dataType.IsReadOnly();
  5825. ctx->type.dataType.MakeReadOnly(false);
  5826. ctx->type.dataType.MakeHandle(true);
  5827. ctx->type.dataType.MakeReadOnly(true);
  5828. ctx->type.dataType.MakeHandleToConst(isHandleToConst);
  5829. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  5830. {
  5831. ctx->bc.Instr(asBC_PopPtr);
  5832. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  5833. ctx->type.dataType.MakeReference(true);
  5834. }
  5835. else if( ctx->type.dataType.IsReference() )
  5836. {
  5837. ctx->bc.Instr(asBC_RDSPtr);
  5838. ctx->type.dataType.MakeReference(false);
  5839. }
  5840. }
  5841. else
  5842. {
  5843. int offset = AllocateVariable(dt, true);
  5844. if( ctx->type.dataType.IsReference() )
  5845. ctx->bc.Instr(asBC_RDSPtr);
  5846. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5847. if (dt.IsFuncdef())
  5848. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  5849. else
  5850. ctx->bc.InstrPTR(asBC_REFCPY, dt.GetTypeInfo());
  5851. ctx->bc.Instr(asBC_PopPtr);
  5852. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  5853. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  5854. if( to.IsReference() )
  5855. dt.MakeReference(true);
  5856. else
  5857. ctx->bc.Instr(asBC_RDSPtr);
  5858. ctx->type.SetVariable(dt, offset, true);
  5859. }
  5860. }
  5861. else
  5862. ctx->type.dataType = dt;
  5863. // When this conversion is done the expression is no longer an lvalue
  5864. ctx->type.isLValue = false;
  5865. }
  5866. }
  5867. if( ctx->type.dataType.IsObjectHandle() )
  5868. {
  5869. // A handle to non-const can be converted to a
  5870. // handle to const, but not the other way
  5871. if( to.IsHandleToConst() )
  5872. ctx->type.dataType.MakeHandleToConst(true);
  5873. // A const handle can be converted to a non-const
  5874. // handle and vice versa as the handle is just a value
  5875. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5876. }
  5877. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  5878. {
  5879. if( generateCode )
  5880. {
  5881. asASSERT( ctx->type.dataType.IsObjectHandle() );
  5882. // If the input type is a handle, then a simple ref copy is enough
  5883. bool isExplicitHandle = ctx->type.isExplicitHandle;
  5884. ctx->type.isExplicitHandle = ctx->type.dataType.IsObjectHandle();
  5885. // If the input type is read-only we'll need to temporarily
  5886. // remove this constness, otherwise the assignment will fail
  5887. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  5888. ctx->type.dataType.MakeReadOnly(false);
  5889. // If the object already is a temporary variable, then the copy
  5890. // doesn't have to be made as it is already a unique object
  5891. PrepareTemporaryVariable(node, ctx);
  5892. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  5893. ctx->type.isExplicitHandle = isExplicitHandle;
  5894. }
  5895. // A non-reference can be converted to a reference,
  5896. // by putting the value in a temporary variable
  5897. ctx->type.dataType.MakeReference(true);
  5898. // Since it is a new temporary variable it doesn't have to be const
  5899. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5900. }
  5901. else if( !to.IsReference() && ctx->type.dataType.IsReference() )
  5902. {
  5903. Dereference(ctx, generateCode);
  5904. }
  5905. }
  5906. else // if( !to.IsObjectHandle() )
  5907. {
  5908. if( !to.IsReference() )
  5909. {
  5910. // reference to handle -> object
  5911. // handle -> object
  5912. // reference -> object
  5913. // An implicit handle can be converted to an object by adding a check for null pointer
  5914. if( ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  5915. {
  5916. if( generateCode )
  5917. {
  5918. if( ctx->type.dataType.IsReference() )
  5919. {
  5920. // The pointer on the stack refers to the handle
  5921. ctx->bc.Instr(asBC_ChkRefS);
  5922. }
  5923. else
  5924. {
  5925. // The pointer on the stack refers to the object
  5926. ctx->bc.Instr(asBC_CHKREF);
  5927. }
  5928. }
  5929. ctx->type.dataType.MakeHandle(false);
  5930. }
  5931. // A const object can be converted to a non-const object through a copy
  5932. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() &&
  5933. allowObjectConstruct )
  5934. {
  5935. // Does the object type allow a copy to be made?
  5936. if( ctx->type.dataType.CanBeCopied() )
  5937. {
  5938. if( generateCode )
  5939. {
  5940. // Make a temporary object with the copy
  5941. PrepareTemporaryVariable(node, ctx);
  5942. }
  5943. // In case the object was already in a temporary variable, then the function
  5944. // didn't really do anything so we need to remove the constness here
  5945. ctx->type.dataType.MakeReadOnly(false);
  5946. // Add the cost for the copy
  5947. cost += asCC_TO_OBJECT_CONV;
  5948. }
  5949. }
  5950. if( ctx->type.dataType.IsReference() )
  5951. {
  5952. // This may look strange, but a value type allocated on the stack is already
  5953. // correct, so nothing should be done other than remove the mark as reference.
  5954. // For types allocated on the heap, it is necessary to dereference the pointer
  5955. // that is currently on the stack
  5956. if( IsVariableOnHeap(ctx->type.stackOffset) )
  5957. Dereference(ctx, generateCode);
  5958. else
  5959. ctx->type.dataType.MakeReference(false);
  5960. }
  5961. // A non-const object can be converted to a const object directly
  5962. if( !ctx->type.dataType.IsReadOnly() && to.IsReadOnly() )
  5963. {
  5964. ctx->type.dataType.MakeReadOnly(true);
  5965. }
  5966. }
  5967. else // if( to.IsReference() )
  5968. {
  5969. // reference to handle -> reference
  5970. // handle -> reference
  5971. // object -> reference
  5972. if( ctx->type.dataType.IsReference() )
  5973. {
  5974. if( ctx->type.isExplicitHandle && ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  5975. {
  5976. // ASHANDLE objects are really value types, so explicit handle can be removed
  5977. ctx->type.isExplicitHandle = false;
  5978. ctx->type.dataType.MakeHandle(false);
  5979. }
  5980. // A reference to a handle can be converted to a reference to an object
  5981. // by first reading the address, then verifying that it is not null
  5982. if( !to.IsObjectHandle() && ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  5983. {
  5984. ctx->type.dataType.MakeHandle(false);
  5985. if( generateCode )
  5986. ctx->bc.Instr(asBC_ChkRefS);
  5987. }
  5988. // A reference to a non-const can be converted to a reference to a const
  5989. if( to.IsReadOnly() )
  5990. ctx->type.dataType.MakeReadOnly(true);
  5991. else if( ctx->type.dataType.IsReadOnly() && allowObjectConstruct )
  5992. {
  5993. // A reference to a const can be converted to a reference to a
  5994. // non-const by copying the object to a temporary variable
  5995. ctx->type.dataType.MakeReadOnly(false);
  5996. if( generateCode )
  5997. {
  5998. // If the object already is a temporary variable, then the copy
  5999. // doesn't have to be made as it is already a unique object
  6000. PrepareTemporaryVariable(node, ctx);
  6001. }
  6002. // Add the cost for the copy
  6003. cost += asCC_TO_OBJECT_CONV;
  6004. }
  6005. }
  6006. else // if( !ctx->type.dataType.IsReference() )
  6007. {
  6008. // A non-reference handle can be converted to a non-handle reference by checking against null handle
  6009. if( ctx->type.dataType.IsObjectHandle() )
  6010. {
  6011. bool readOnly = false;
  6012. if( ctx->type.dataType.IsHandleToConst() )
  6013. readOnly = true;
  6014. if( generateCode )
  6015. {
  6016. if( ctx->type.isVariable )
  6017. ctx->bc.InstrSHORT(asBC_ChkNullV, ctx->type.stackOffset);
  6018. else
  6019. ctx->bc.Instr(asBC_CHKREF);
  6020. }
  6021. ctx->type.dataType.MakeHandle(false);
  6022. ctx->type.dataType.MakeReference(true);
  6023. // Make sure a handle to const isn't converted to non-const reference
  6024. if( readOnly )
  6025. ctx->type.dataType.MakeReadOnly(true);
  6026. }
  6027. else
  6028. {
  6029. // A value type allocated on the stack is differentiated
  6030. // by it not being a reference. But it can be handled as
  6031. // reference by pushing the pointer on the stack
  6032. if( (ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) &&
  6033. (ctx->type.isVariable || ctx->type.isTemporary) &&
  6034. !IsVariableOnHeap(ctx->type.stackOffset) )
  6035. {
  6036. // Actually the pointer is already pushed on the stack in
  6037. // CompileVariableAccess, so we don't need to do anything else
  6038. }
  6039. else if( generateCode )
  6040. {
  6041. // A non-reference can be converted to a reference,
  6042. // by putting the value in a temporary variable
  6043. // If the input type is read-only we'll need to temporarily
  6044. // remove this constness, otherwise the assignment will fail
  6045. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  6046. ctx->type.dataType.MakeReadOnly(false);
  6047. // If the object already is a temporary variable, then the copy
  6048. // doesn't have to be made as it is already a unique object
  6049. PrepareTemporaryVariable(node, ctx);
  6050. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  6051. // Add the cost for the copy
  6052. cost += asCC_TO_OBJECT_CONV;
  6053. }
  6054. // This may look strange as the conversion was to make the expression a reference
  6055. // but a value type allocated on the stack is a reference even without the type
  6056. // being marked as such.
  6057. ctx->type.dataType.MakeReference(IsVariableOnHeap(ctx->type.stackOffset));
  6058. }
  6059. // TODO: If the variable is an object allocated on the stack the following is not true as the copy may not have been made
  6060. // Since it is a new temporary variable it doesn't have to be const
  6061. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  6062. }
  6063. }
  6064. }
  6065. return cost;
  6066. }
  6067. asUINT asCCompiler::ImplicitConvPrimitiveToObject(asCExprContext *ctx, const asCDataType &to, asCScriptNode * /*node*/, EImplicitConv /*isExplicit*/, bool generateCode, bool /*allowObjectConstruct*/)
  6068. {
  6069. // Reference types currently don't allow implicit conversion from primitive to object
  6070. // TODO: Allow implicit conversion to scoped reference types as they are supposed to appear like ordinary value types
  6071. asCObjectType *objType = CastToObjectType(to.GetTypeInfo());
  6072. asASSERT( objType || CastToFuncdefType(to.GetTypeInfo()) );
  6073. if( !objType || (objType->flags & asOBJ_REF) )
  6074. return asCC_NO_CONV;
  6075. // For value types the object must have a constructor that takes a single primitive argument either by value or as input reference
  6076. asCArray<int> funcs;
  6077. for( asUINT n = 0; n < objType->beh.constructors.GetLength(); n++ )
  6078. {
  6079. asCScriptFunction *func = engine->scriptFunctions[objType->beh.constructors[n]];
  6080. if( func->parameterTypes.GetLength() == 1 &&
  6081. func->parameterTypes[0].IsPrimitive() &&
  6082. !(func->inOutFlags[0] & asTM_OUTREF) )
  6083. {
  6084. funcs.PushLast(func->id);
  6085. }
  6086. }
  6087. if( funcs.GetLength() == 0 )
  6088. return asCC_NO_CONV;
  6089. // Check if it is possible to choose a best match
  6090. asCExprContext arg(engine);
  6091. arg.type = ctx->type;
  6092. arg.exprNode = ctx->exprNode; // Use the same node for compiler messages
  6093. asCArray<asCExprContext*> args;
  6094. args.PushLast(&arg);
  6095. asUINT cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, 0, 0, 0, objType, false, true, false);
  6096. if( funcs.GetLength() != 1 )
  6097. return asCC_NO_CONV;
  6098. if( !generateCode )
  6099. {
  6100. ctx->type.Set(to);
  6101. return cost;
  6102. }
  6103. // TODO: clean up: This part is similar to CompileConstructCall(). It should be put in a common function
  6104. // Clear the type of ctx, as the type is moved to the arg
  6105. ctx->type.SetDummy();
  6106. // Value types and script types are allocated through the constructor
  6107. asCExprValue tempObj;
  6108. tempObj.dataType = to;
  6109. tempObj.stackOffset = (short)AllocateVariable(to, true);
  6110. tempObj.dataType.MakeReference(true);
  6111. tempObj.isTemporary = true;
  6112. tempObj.isVariable = true;
  6113. bool onHeap = IsVariableOnHeap(tempObj.stackOffset);
  6114. // Push the address of the object on the stack
  6115. if( onHeap )
  6116. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  6117. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  6118. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  6119. if( !(objType->flags & asOBJ_REF) )
  6120. {
  6121. // If the object is allocated on the stack, then call the constructor as a normal function
  6122. if( onHeap )
  6123. {
  6124. int offset = 0;
  6125. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  6126. for( asUINT n = 0; n < args.GetLength(); n++ )
  6127. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  6128. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  6129. }
  6130. else
  6131. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6132. PerformFunctionCall(funcs[0], ctx, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  6133. // Add tag that the object has been initialized
  6134. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  6135. // The constructor doesn't return anything,
  6136. // so we have to manually inform the type of
  6137. // the return value
  6138. ctx->type = tempObj;
  6139. if( !onHeap )
  6140. ctx->type.dataType.MakeReference(false);
  6141. // Push the address of the object on the stack again
  6142. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  6143. }
  6144. else
  6145. {
  6146. asASSERT( objType->flags & asOBJ_SCOPED );
  6147. // Call the factory to create the reference type
  6148. PerformFunctionCall(funcs[0], ctx, false, &args);
  6149. }
  6150. return cost;
  6151. }
  6152. void asCCompiler::ImplicitConversionConstant(asCExprContext *from, const asCDataType &to, asCScriptNode *node, EImplicitConv convType)
  6153. {
  6154. asASSERT(from->type.isConstant);
  6155. // TODO: node should be the node of the value that is
  6156. // converted (not the operator that provokes the implicit
  6157. // conversion)
  6158. // If the base type is correct there is no more to do
  6159. if( to.IsEqualExceptRefAndConst(from->type.dataType) ) return;
  6160. // References cannot be constants
  6161. if( from->type.dataType.IsReference() ) return;
  6162. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1 && !to.IsEnumType()) ||
  6163. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  6164. {
  6165. if( from->type.dataType.IsFloatType() ||
  6166. from->type.dataType.IsDoubleType() ||
  6167. from->type.dataType.IsUnsignedType() ||
  6168. from->type.dataType.IsIntegerType() )
  6169. {
  6170. asCDataType targetDt;
  6171. if (to.IsEnumType())
  6172. targetDt = to;
  6173. else
  6174. targetDt = asCDataType::CreatePrimitive(ttInt, true);
  6175. // Transform the value
  6176. // Float constants can be implicitly converted to int
  6177. if( from->type.dataType.IsFloatType() )
  6178. {
  6179. float fc = from->type.GetConstantF();
  6180. int ic = int(fc);
  6181. if( float(ic) != fc )
  6182. {
  6183. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6184. }
  6185. from->type.SetConstantDW(targetDt, ic);
  6186. }
  6187. // Double constants can be implicitly converted to int
  6188. else if( from->type.dataType.IsDoubleType() )
  6189. {
  6190. double fc = from->type.GetConstantD();
  6191. int ic = int(fc);
  6192. if( double(ic) != fc )
  6193. {
  6194. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6195. }
  6196. from->type.SetConstantDW(targetDt, ic);
  6197. }
  6198. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6199. {
  6200. // Verify that it is possible to convert to signed without getting negative
  6201. if( from->type.dataType.GetSizeInMemoryBytes() == 4 &&
  6202. int(from->type.GetConstantDW()) < 0 &&
  6203. convType != asIC_EXPLICIT_VAL_CAST &&
  6204. node != 0 )
  6205. Warning(TXT_CHANGE_SIGN, node);
  6206. // Convert to 32bit
  6207. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6208. from->type.SetConstantDW(targetDt, from->type.GetConstantB());
  6209. else if (from->type.dataType.GetSizeInMemoryBytes() == 2)
  6210. from->type.SetConstantDW(targetDt, from->type.GetConstantW());
  6211. else
  6212. from->type.dataType = targetDt;
  6213. }
  6214. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6215. {
  6216. if (asQWORD(from->type.GetConstantQW()) >> 31)
  6217. if (convType != asIC_EXPLICIT_VAL_CAST && node) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6218. // Convert to 32bit
  6219. from->type.SetConstantDW(targetDt, int(from->type.GetConstantQW()));
  6220. }
  6221. else if (from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2)
  6222. {
  6223. if (int(from->type.GetConstantQW()) != asINT64(from->type.GetConstantQW()))
  6224. if (convType != asIC_EXPLICIT_VAL_CAST && node) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6225. // Convert to 32bit
  6226. from->type.SetConstantDW(targetDt, int(from->type.GetConstantQW()));
  6227. }
  6228. else if (from->type.dataType.IsIntegerType() &&
  6229. from->type.dataType.GetSizeInMemoryBytes() < 4)
  6230. {
  6231. // Convert to 32bit
  6232. if (from->type.dataType.GetSizeInMemoryBytes() == 1)
  6233. from->type.SetConstantDW(targetDt, (signed char)from->type.GetConstantB());
  6234. else if (from->type.dataType.GetSizeInMemoryBytes() == 2)
  6235. from->type.SetConstantDW(targetDt, (short)from->type.GetConstantW());
  6236. }
  6237. else
  6238. {
  6239. // Only int32 and enums should come here and as these are 32bit
  6240. // already nothing needs to be done except set the target type
  6241. asASSERT((from->type.dataType.GetTokenType() == ttInt ||
  6242. from->type.dataType.IsEnumType()) &&
  6243. from->type.dataType.GetSizeInMemoryBytes() == 4);
  6244. from->type.dataType = targetDt;
  6245. }
  6246. }
  6247. // Check if a downsize is necessary
  6248. if( to.IsIntegerType() &&
  6249. from->type.dataType.IsIntegerType() &&
  6250. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  6251. {
  6252. // Verify if it is possible
  6253. if( to.GetSizeInMemoryBytes() == 1 )
  6254. {
  6255. if( char(from->type.GetConstantDW()) != int(from->type.GetConstantDW()) )
  6256. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6257. from->type.SetConstantB(asCDataType::CreatePrimitive(to.GetTokenType(), true), char(from->type.GetConstantDW()));
  6258. }
  6259. else if( to.GetSizeInMemoryBytes() == 2 )
  6260. {
  6261. if( short(from->type.GetConstantDW()) != int(from->type.GetConstantDW()) )
  6262. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6263. from->type.SetConstantW(asCDataType::CreatePrimitive(to.GetTokenType(), true), short(from->type.GetConstantDW()));
  6264. }
  6265. }
  6266. }
  6267. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  6268. {
  6269. // Float constants can be implicitly converted to int
  6270. if( from->type.dataType.IsFloatType() )
  6271. {
  6272. float fc = from->type.GetConstantF();
  6273. asINT64 ic = asINT64(fc);
  6274. if( float(ic) != fc )
  6275. {
  6276. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6277. }
  6278. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), ic);
  6279. }
  6280. // Double constants can be implicitly converted to int
  6281. else if( from->type.dataType.IsDoubleType() )
  6282. {
  6283. double fc = from->type.GetConstantD();
  6284. asINT64 ic = asINT64(fc);
  6285. if( double(ic) != fc )
  6286. {
  6287. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6288. }
  6289. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), ic);
  6290. }
  6291. else if( from->type.dataType.IsUnsignedType() )
  6292. {
  6293. // Convert to 64bit
  6294. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6295. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), from->type.GetConstantB());
  6296. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6297. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), from->type.GetConstantW());
  6298. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6299. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), from->type.GetConstantDW());
  6300. else if( from->type.dataType.GetSizeInMemoryBytes() == 8 )
  6301. {
  6302. if( asINT64(from->type.GetConstantQW()) < 0 )
  6303. {
  6304. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6305. }
  6306. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  6307. }
  6308. }
  6309. else if( from->type.dataType.IsIntegerType() )
  6310. {
  6311. // Convert to 64bit
  6312. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6313. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), (signed char)from->type.GetConstantB());
  6314. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6315. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), (short)from->type.GetConstantW());
  6316. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6317. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), (int)from->type.GetConstantDW());
  6318. }
  6319. }
  6320. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  6321. {
  6322. if( from->type.dataType.IsFloatType() )
  6323. {
  6324. float fc = from->type.GetConstantF();
  6325. // Some compilers set the value to 0 when converting a negative float to unsigned int.
  6326. // To maintain a consistent behaviour across compilers we convert to int first.
  6327. asUINT uic = asUINT(int(fc));
  6328. if( float(uic) != fc )
  6329. {
  6330. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6331. }
  6332. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), uic);
  6333. // Try once more, in case of a smaller type
  6334. ImplicitConversionConstant(from, to, node, convType);
  6335. }
  6336. else if( from->type.dataType.IsDoubleType() )
  6337. {
  6338. double fc = from->type.GetConstantD();
  6339. // Some compilers set the value to 0 when converting a negative double to unsigned int.
  6340. // To maintain a consistent behaviour across compilers we convert to int first.
  6341. asUINT uic = asUINT(int(fc));
  6342. if( double(uic) != fc )
  6343. {
  6344. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6345. }
  6346. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), uic);
  6347. // Try once more, in case of a smaller type
  6348. ImplicitConversionConstant(from, to, node, convType);
  6349. }
  6350. else if( from->type.dataType.IsIntegerType() )
  6351. {
  6352. // Verify that it is possible to convert to unsigned without loosing negative
  6353. if( (from->type.dataType.GetSizeInMemoryBytes() > 4 && asINT64(from->type.GetConstantQW()) < 0) ||
  6354. (from->type.dataType.GetSizeInMemoryBytes() == 4 && int(from->type.GetConstantDW()) < 0) ||
  6355. (from->type.dataType.GetSizeInMemoryBytes() == 2 && short(from->type.GetConstantW()) < 0) ||
  6356. (from->type.dataType.GetSizeInMemoryBytes() == 1 && char(from->type.GetConstantB()) < 0))
  6357. {
  6358. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6359. }
  6360. // Check if any data is lost
  6361. if( from->type.dataType.GetSizeInMemoryBytes() > 4 && (from->type.GetConstantQW() >> 32) != 0 && (from->type.GetConstantQW() >> 32) != 0xFFFFFFFF )
  6362. {
  6363. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6364. }
  6365. // Convert to 32bit
  6366. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6367. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (signed char)from->type.GetConstantB());
  6368. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6369. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (short)from->type.GetConstantW());
  6370. else if (from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6371. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (int)from->type.GetConstantDW());
  6372. else
  6373. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), (int)(asINT64)from->type.GetConstantQW());
  6374. // Try once more, in case of a smaller type
  6375. ImplicitConversionConstant(from, to, node, convType);
  6376. }
  6377. else if( from->type.dataType.IsUnsignedType() &&
  6378. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  6379. {
  6380. // Convert to 32bit
  6381. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6382. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), from->type.GetConstantB());
  6383. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6384. from->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), from->type.GetConstantW());
  6385. // Try once more, in case of a smaller type
  6386. ImplicitConversionConstant(from, to, node, convType);
  6387. }
  6388. else if( from->type.dataType.IsUnsignedType() &&
  6389. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  6390. {
  6391. // Verify if it is possible
  6392. if( to.GetSizeInMemoryBytes() == 1 )
  6393. {
  6394. if( asBYTE(from->type.GetConstantDW()) != from->type.GetConstantDW() )
  6395. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6396. from->type.SetConstantB(asCDataType::CreatePrimitive(to.GetTokenType(), true), asBYTE(from->type.GetConstantDW()));
  6397. }
  6398. else if( to.GetSizeInMemoryBytes() == 2 )
  6399. {
  6400. if( asWORD(from->type.GetConstantDW()) != from->type.GetConstantDW())
  6401. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6402. from->type.SetConstantW(asCDataType::CreatePrimitive(to.GetTokenType(), true), asWORD(from->type.GetConstantDW()));
  6403. }
  6404. else if (to.GetSizeInMemoryBytes() == 4)
  6405. {
  6406. if( asDWORD(from->type.GetConstantQW()) != from->type.GetConstantQW())
  6407. if (convType != asIC_EXPLICIT_VAL_CAST && node) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  6408. from->type.SetConstantDW(asCDataType::CreatePrimitive(to.GetTokenType(), true), asDWORD(from->type.GetConstantQW()));
  6409. }
  6410. }
  6411. }
  6412. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  6413. {
  6414. if( from->type.dataType.IsFloatType() )
  6415. {
  6416. float fc = from->type.GetConstantF();
  6417. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  6418. asQWORD uic = asQWORD(asINT64(fc));
  6419. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  6420. // MSVC6 doesn't support this conversion
  6421. if( float(uic) != fc )
  6422. {
  6423. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6424. }
  6425. #endif
  6426. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), uic);
  6427. }
  6428. else if( from->type.dataType.IsDoubleType() )
  6429. {
  6430. double fc = from->type.GetConstantD();
  6431. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  6432. asQWORD uic = asQWORD(asINT64(fc));
  6433. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  6434. // MSVC6 doesn't support this conversion
  6435. if( double(uic) != fc )
  6436. {
  6437. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6438. }
  6439. #endif
  6440. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), uic);
  6441. }
  6442. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6443. {
  6444. // Convert to 64bit
  6445. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6446. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), (asINT64)(signed char)from->type.GetConstantB());
  6447. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6448. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), (asINT64)(short)from->type.GetConstantW());
  6449. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6450. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), (asINT64)(int)from->type.GetConstantDW());
  6451. // Verify that it is possible to convert to unsigned without loosing negative
  6452. if( asINT64(from->type.GetConstantQW()) < 0 )
  6453. {
  6454. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6455. }
  6456. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  6457. }
  6458. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6459. {
  6460. // Verify that it is possible to convert to unsigned without loosing negative
  6461. if( asINT64(from->type.GetConstantQW()) < 0 )
  6462. {
  6463. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  6464. }
  6465. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  6466. }
  6467. else if( from->type.dataType.IsUnsignedType() )
  6468. {
  6469. // Convert to 64bit
  6470. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6471. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), from->type.GetConstantB());
  6472. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6473. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), from->type.GetConstantW());
  6474. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  6475. from->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), from->type.GetConstantDW());
  6476. }
  6477. }
  6478. else if( to.IsFloatType() )
  6479. {
  6480. if( from->type.dataType.IsDoubleType() )
  6481. {
  6482. double ic = from->type.GetConstantD();
  6483. float fc = float(ic);
  6484. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6485. }
  6486. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6487. {
  6488. // Must properly convert value in case the from value is smaller
  6489. int ic;
  6490. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6491. ic = (signed char)from->type.GetConstantB();
  6492. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6493. ic = (short)from->type.GetConstantW();
  6494. else
  6495. ic = (int)from->type.GetConstantDW();
  6496. float fc = float(ic);
  6497. if( int(fc) != ic )
  6498. {
  6499. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6500. }
  6501. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6502. }
  6503. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6504. {
  6505. float fc = float(asINT64(from->type.GetConstantQW()));
  6506. if( asINT64(fc) != asINT64(from->type.GetConstantQW()) )
  6507. {
  6508. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6509. }
  6510. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6511. }
  6512. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6513. {
  6514. // Must properly convert value in case the from value is smaller
  6515. unsigned int uic;
  6516. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6517. uic = from->type.GetConstantB();
  6518. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6519. uic = from->type.GetConstantW();
  6520. else
  6521. uic = from->type.GetConstantDW();
  6522. float fc = float(uic);
  6523. if( (unsigned int)(fc) != uic )
  6524. {
  6525. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6526. }
  6527. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6528. }
  6529. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6530. {
  6531. float fc = float((asINT64)from->type.GetConstantQW());
  6532. if( asQWORD(fc) != from->type.GetConstantQW())
  6533. {
  6534. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6535. }
  6536. from->type.SetConstantF(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6537. }
  6538. }
  6539. else if( to.IsDoubleType() )
  6540. {
  6541. if( from->type.dataType.IsFloatType() )
  6542. {
  6543. float ic = from->type.GetConstantF();
  6544. double fc = double(ic);
  6545. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6546. }
  6547. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6548. {
  6549. // Must properly convert value in case the from value is smaller
  6550. int ic;
  6551. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6552. ic = (signed char)from->type.GetConstantB();
  6553. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6554. ic = (short)from->type.GetConstantW();
  6555. else
  6556. ic = (int)from->type.GetConstantDW();
  6557. double fc = double(ic);
  6558. if( int(fc) != ic )
  6559. {
  6560. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6561. }
  6562. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6563. }
  6564. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6565. {
  6566. double fc = double(asINT64(from->type.GetConstantQW()));
  6567. if( asINT64(fc) != asINT64(from->type.GetConstantQW()) )
  6568. {
  6569. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6570. }
  6571. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6572. }
  6573. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  6574. {
  6575. // Must properly convert value in case the from value is smaller
  6576. unsigned int uic;
  6577. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  6578. uic = from->type.GetConstantB();
  6579. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  6580. uic = from->type.GetConstantW();
  6581. else
  6582. uic = from->type.GetConstantDW();
  6583. double fc = double(uic);
  6584. if( (unsigned int)(fc) != uic )
  6585. {
  6586. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6587. }
  6588. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6589. }
  6590. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  6591. {
  6592. double fc = double((asINT64)from->type.GetConstantQW());
  6593. if( asQWORD(fc) != from->type.GetConstantQW())
  6594. {
  6595. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  6596. }
  6597. from->type.SetConstantD(asCDataType::CreatePrimitive(to.GetTokenType(), true), fc);
  6598. }
  6599. }
  6600. }
  6601. int asCCompiler::DoAssignment(asCExprContext *ctx, asCExprContext *lctx, asCExprContext *rctx, asCScriptNode *lexpr, asCScriptNode *rexpr, eTokenType op, asCScriptNode *opNode)
  6602. {
  6603. // Don't allow any operators on expressions that take address of class method
  6604. // If methodName is set but the type is not an object, then it is a global function
  6605. if( lctx->methodName != "" || rctx->IsClassMethod() )
  6606. {
  6607. Error(TXT_INVALID_OP_ON_METHOD, opNode);
  6608. return -1;
  6609. }
  6610. // Implicit handle types should always be treated as handles in assignments
  6611. if (lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE) )
  6612. {
  6613. lctx->type.dataType.MakeHandle(true);
  6614. lctx->type.isExplicitHandle = true;
  6615. }
  6616. // Urho3D: if there is a handle type, and it does not have an overloaded assignment operator, convert to an explicit handle
  6617. // for scripting convenience. (For the Urho3D handle types, value assignment is not supported)
  6618. if (lctx->type.dataType.IsObjectHandle() && !lctx->type.dataType.IsTemplate() && !lctx->type.isExplicitHandle &&
  6619. (!lctx->type.dataType.GetBehaviour() || !lctx->type.dataType.GetBehaviour()->copy))
  6620. lctx->type.isExplicitHandle = true;
  6621. // If the left hand expression is a property accessor, then that should be used
  6622. // to do the assignment instead of the ordinary operator. The exception is when
  6623. // the property accessor is for a handle property, and the operation is a value
  6624. // assignment.
  6625. if( (lctx->property_get || lctx->property_set) &&
  6626. !(lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle) )
  6627. {
  6628. if( op != ttAssignment )
  6629. {
  6630. // Generate the code for the compound assignment, i.e. get the value, apply operator, then set the value
  6631. return ProcessPropertyGetSetAccessor(ctx, lctx, rctx, op, opNode);
  6632. }
  6633. // It is not allowed to do a handle assignment on a property
  6634. // accessor that doesn't take a handle in the set accessor.
  6635. if( lctx->property_set && lctx->type.isExplicitHandle )
  6636. {
  6637. // set_opIndex has 2 arguments, where as normal setters have only 1
  6638. asCArray<asCDataType>& parameterTypes =
  6639. builder->GetFunctionDescription(lctx->property_set)->parameterTypes;
  6640. if( !parameterTypes[parameterTypes.GetLength() - 1].IsObjectHandle() )
  6641. {
  6642. // Process the property to free the memory
  6643. ProcessPropertySetAccessor(lctx, rctx, opNode);
  6644. Error(TXT_HANDLE_ASSIGN_ON_NON_HANDLE_PROP, opNode);
  6645. return -1;
  6646. }
  6647. }
  6648. MergeExprBytecodeAndType(ctx, lctx);
  6649. return ProcessPropertySetAccessor(ctx, rctx, opNode);
  6650. }
  6651. else if( lctx->property_get && lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  6652. {
  6653. // Get the handle to the object that will be used for the value assignment
  6654. ProcessPropertyGetAccessor(lctx, opNode);
  6655. }
  6656. if( lctx->type.dataType.IsPrimitive() )
  6657. {
  6658. if( !lctx->type.isLValue )
  6659. {
  6660. Error(TXT_NOT_LVALUE, lexpr);
  6661. return -1;
  6662. }
  6663. if( op != ttAssignment )
  6664. {
  6665. // Compute the operator before the assignment
  6666. asCExprValue lvalue = lctx->type;
  6667. if( lctx->type.isTemporary && !lctx->type.isVariable )
  6668. {
  6669. // The temporary variable must not be freed until the
  6670. // assignment has been performed. lvalue still holds
  6671. // the information about the temporary variable
  6672. lctx->type.isTemporary = false;
  6673. }
  6674. asCExprContext o(engine);
  6675. CompileOperator(opNode, lctx, rctx, &o);
  6676. MergeExprBytecode(rctx, &o);
  6677. rctx->type = o.type;
  6678. // Convert the rvalue to the right type and validate it
  6679. PrepareForAssignment(&lvalue.dataType, rctx, rexpr, false);
  6680. MergeExprBytecode(ctx, rctx);
  6681. lctx->type = lvalue;
  6682. // The lvalue continues the same, either it was a variable, or a reference in the register
  6683. }
  6684. else
  6685. {
  6686. // Convert the rvalue to the right type and validate it
  6687. PrepareForAssignment(&lctx->type.dataType, rctx, rexpr, false, lctx);
  6688. MergeExprBytecode(ctx, rctx);
  6689. MergeExprBytecode(ctx, lctx);
  6690. }
  6691. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  6692. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  6693. ctx->type = lctx->type;
  6694. }
  6695. else if( lctx->type.isExplicitHandle )
  6696. {
  6697. if( !lctx->type.isLValue )
  6698. {
  6699. Error(TXT_NOT_LVALUE, lexpr);
  6700. return -1;
  6701. }
  6702. // Object handles don't have any compound assignment operators
  6703. if( op != ttAssignment )
  6704. {
  6705. asCString str;
  6706. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  6707. Error(str, lexpr);
  6708. return -1;
  6709. }
  6710. if( lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  6711. {
  6712. // The object is a value type but that should be treated as a handle
  6713. // Make sure the right hand value is a handle
  6714. if( !rctx->type.isExplicitHandle &&
  6715. !(rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE)) )
  6716. {
  6717. // Function names can be considered handles already
  6718. if( rctx->methodName == "" )
  6719. {
  6720. asCDataType dt = rctx->type.dataType;
  6721. dt.MakeHandle(true);
  6722. dt.MakeReference(false);
  6723. PrepareArgument(&dt, rctx, rexpr, true, asTM_INREF);
  6724. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  6725. {
  6726. asCString str;
  6727. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  6728. Error(str, rexpr);
  6729. return -1;
  6730. }
  6731. }
  6732. }
  6733. if( CompileOverloadedDualOperator(opNode, lctx, rctx, false, ctx, true) )
  6734. {
  6735. // An overloaded assignment operator was found (or a compilation error occured)
  6736. return 0;
  6737. }
  6738. // The object must implement the opAssign method
  6739. asCString msg;
  6740. msg.Format(TXT_NO_APPROPRIATE_OPHNDLASSIGN_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  6741. Error(msg.AddressOf(), opNode);
  6742. return -1;
  6743. }
  6744. else
  6745. {
  6746. asCDataType dt = lctx->type.dataType;
  6747. dt.MakeReference(false);
  6748. PrepareArgument(&dt, rctx, rexpr, false, asTM_INREF , true);
  6749. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  6750. {
  6751. asCString str;
  6752. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  6753. Error(str, rexpr);
  6754. return -1;
  6755. }
  6756. MergeExprBytecode(ctx, rctx);
  6757. MergeExprBytecode(ctx, lctx);
  6758. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  6759. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  6760. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  6761. ctx->type = lctx->type;
  6762. // After the handle assignment the original handle is left on the stack
  6763. ctx->type.dataType.MakeReference(false);
  6764. }
  6765. }
  6766. else // if( lctx->type.dataType.IsObject() )
  6767. {
  6768. // The lvalue reference may be marked as a temporary, if for example
  6769. // it was originated as a handle returned from a function. In such
  6770. // cases it must be possible to assign values to it anyway.
  6771. if( lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  6772. {
  6773. // Convert the handle to a object reference
  6774. asCDataType to;
  6775. to = lctx->type.dataType;
  6776. to.MakeHandle(false);
  6777. ImplicitConversion(lctx, to, lexpr, asIC_IMPLICIT_CONV);
  6778. lctx->type.isLValue = true; // Handle may not have been an lvalue, but the dereferenced object is
  6779. }
  6780. // Check for overloaded assignment operator
  6781. if( CompileOverloadedDualOperator(opNode, lctx, rctx, false, ctx) )
  6782. {
  6783. // An overloaded assignment operator was found (or a compilation error occured)
  6784. return 0;
  6785. }
  6786. // No registered operator was found. In case the operation is a direct
  6787. // assignment and the rvalue is the same type as the lvalue, then we can
  6788. // still use the byte-for-byte copy to do the assignment
  6789. if( op != ttAssignment )
  6790. {
  6791. asCString str;
  6792. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  6793. Error(str, lexpr);
  6794. return -1;
  6795. }
  6796. // If the left hand expression is simple, i.e. without any
  6797. // function calls or allocations of memory, then we can avoid
  6798. // doing a copy of the right hand expression (done by PrepareArgument).
  6799. // Instead the reference to the value can be placed directly on the
  6800. // stack.
  6801. //
  6802. // This optimization should only be done for value types, where
  6803. // the application developer is responsible for making the
  6804. // implementation safe against unwanted destruction of the input
  6805. // reference before the time.
  6806. bool simpleExpr = (lctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) && lctx->bc.IsSimpleExpression();
  6807. // Implicitly convert the rvalue to the type of the lvalue
  6808. bool needConversion = false;
  6809. if( !lctx->type.dataType.IsEqualExceptRefAndConst(rctx->type.dataType) )
  6810. needConversion = true;
  6811. if( !simpleExpr || needConversion )
  6812. {
  6813. asCDataType dt = lctx->type.dataType;
  6814. dt.MakeReference(true);
  6815. dt.MakeReadOnly(true);
  6816. int r = PrepareArgument(&dt, rctx, rexpr, true, 1, !needConversion);
  6817. if( r < 0 )
  6818. return -1;
  6819. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  6820. {
  6821. asCString str;
  6822. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  6823. Error(str, rexpr);
  6824. return -1;
  6825. }
  6826. }
  6827. else
  6828. {
  6829. // Process any property accessor first, before placing the final reference on the stack
  6830. ProcessPropertyGetAccessor(rctx, rexpr);
  6831. if( rctx->type.dataType.IsReference() && (!(rctx->type.isVariable || rctx->type.isTemporary) || IsVariableOnHeap(rctx->type.stackOffset)) )
  6832. rctx->bc.Instr(asBC_RDSPtr);
  6833. }
  6834. MergeExprBytecode(ctx, rctx);
  6835. MergeExprBytecode(ctx, lctx);
  6836. if( !simpleExpr || needConversion )
  6837. {
  6838. if( (rctx->type.isVariable || rctx->type.isTemporary) )
  6839. {
  6840. if( !IsVariableOnHeap(rctx->type.stackOffset) )
  6841. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  6842. // as the value allocated on the stack is guaranteed to be safe.
  6843. // The bytecode optimizer should be able to determine this and optimize away the VAR + GETREF
  6844. ctx->bc.InstrWORD(asBC_GETREF, AS_PTR_SIZE);
  6845. else
  6846. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  6847. }
  6848. }
  6849. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  6850. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  6851. ctx->type = lctx->type;
  6852. }
  6853. return 0;
  6854. }
  6855. int asCCompiler::CompileAssignment(asCScriptNode *expr, asCExprContext *ctx)
  6856. {
  6857. asCScriptNode *lexpr = expr->firstChild;
  6858. if( lexpr->next )
  6859. {
  6860. // Compile the two expression terms
  6861. asCExprContext lctx(engine), rctx(engine);
  6862. int rr = CompileAssignment(lexpr->next->next, &rctx);
  6863. int lr = CompileCondition(lexpr, &lctx);
  6864. if( lr >= 0 && rr >= 0 )
  6865. return DoAssignment(ctx, &lctx, &rctx, lexpr, lexpr->next->next, lexpr->next->tokenType, lexpr->next);
  6866. // Since the operands failed, the assignment was not computed
  6867. ctx->type.SetDummy();
  6868. return -1;
  6869. }
  6870. return CompileCondition(lexpr, ctx);
  6871. }
  6872. int asCCompiler::CompileCondition(asCScriptNode *expr, asCExprContext *ctx)
  6873. {
  6874. asCExprValue ctype;
  6875. // Compile the conditional expression
  6876. asCScriptNode *cexpr = expr->firstChild;
  6877. if( cexpr->next )
  6878. {
  6879. //-------------------------------
  6880. // Compile the condition
  6881. asCExprContext e(engine);
  6882. int r = CompileExpression(cexpr, &e);
  6883. if( r < 0 )
  6884. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  6885. // Allow value types to be converted to bool using 'bool opImplConv()'
  6886. if( e.type.dataType.GetTypeInfo() && (e.type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  6887. ImplicitConversion(&e, asCDataType::CreatePrimitive(ttBool, false), cexpr, asIC_IMPLICIT_CONV);
  6888. if( r >= 0 && !e.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  6889. {
  6890. Error(TXT_EXPR_MUST_BE_BOOL, cexpr);
  6891. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  6892. }
  6893. ctype = e.type;
  6894. ProcessPropertyGetAccessor(&e, cexpr);
  6895. if( e.type.dataType.IsReference() ) ConvertToVariable(&e);
  6896. ProcessDeferredParams(&e);
  6897. //-------------------------------
  6898. // Compile the left expression
  6899. asCExprContext le(engine);
  6900. int lr = CompileAssignment(cexpr->next, &le);
  6901. // Resolve any function names already
  6902. DetermineSingleFunc(&le, cexpr->next);
  6903. //-------------------------------
  6904. // Compile the right expression
  6905. asCExprContext re(engine);
  6906. int rr = CompileAssignment(cexpr->next->next, &re);
  6907. DetermineSingleFunc(&re, cexpr->next->next);
  6908. if( lr >= 0 && rr >= 0 )
  6909. {
  6910. // Don't allow any operators on expressions that take address of class method
  6911. if( le.IsClassMethod() || re.IsClassMethod() )
  6912. {
  6913. Error(TXT_INVALID_OP_ON_METHOD, expr);
  6914. return -1;
  6915. }
  6916. ProcessPropertyGetAccessor(&le, cexpr->next);
  6917. ProcessPropertyGetAccessor(&re, cexpr->next->next);
  6918. bool isExplicitHandle = le.type.isExplicitHandle || re.type.isExplicitHandle;
  6919. // Allow a 0 or null in the first case to be implicitly converted to the second type
  6920. if( le.type.isConstant && le.type.GetConstantData() == 0 && le.type.dataType.IsIntegerType() )
  6921. {
  6922. asCDataType to = re.type.dataType;
  6923. to.MakeReference(false);
  6924. to.MakeReadOnly(true);
  6925. ImplicitConversionConstant(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  6926. }
  6927. else if( le.type.IsNullConstant() )
  6928. {
  6929. asCDataType to = re.type.dataType;
  6930. to.MakeHandle(true);
  6931. ImplicitConversion(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  6932. }
  6933. // Allow either case to be converted to const @ if the other is const @
  6934. if( (le.type.dataType.IsHandleToConst() && !le.type.IsNullConstant()) || (re.type.dataType.IsHandleToConst() && !re.type.dataType.IsNullHandle()) )
  6935. {
  6936. le.type.dataType.MakeHandleToConst(true);
  6937. re.type.dataType.MakeHandleToConst(true);
  6938. }
  6939. //---------------------------------
  6940. // Output the byte code
  6941. int afterLabel = nextLabel++;
  6942. int elseLabel = nextLabel++;
  6943. // If left expression is void, then we don't need to store the result
  6944. if( le.type.dataType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  6945. {
  6946. // Put the code for the condition expression on the output
  6947. MergeExprBytecode(ctx, &e);
  6948. // Added the branch decision
  6949. ctx->type = e.type;
  6950. ConvertToVariable(ctx);
  6951. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  6952. ctx->bc.Instr(asBC_ClrHi);
  6953. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  6954. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  6955. // Add the left expression
  6956. MergeExprBytecode(ctx, &le);
  6957. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  6958. // Add the right expression
  6959. ctx->bc.Label((short)elseLabel);
  6960. MergeExprBytecode(ctx, &re);
  6961. ctx->bc.Label((short)afterLabel);
  6962. // Make sure both expressions have the same type
  6963. if( le.type.dataType != re.type.dataType )
  6964. Error(TXT_BOTH_MUST_BE_SAME, expr);
  6965. // Set the type of the result
  6966. ctx->type = le.type;
  6967. }
  6968. else if (le.type.IsNullConstant() && re.type.IsNullConstant())
  6969. {
  6970. // Special case for when both results are 'null'
  6971. // TODO: Other expressions where both results are identical literal constants can probably also be handled this way
  6972. // Put the code for the condition expression on the output
  6973. MergeExprBytecode(ctx, &e);
  6974. // Load the result into the register, but ignore the value since both paths give the same response
  6975. ctx->type = e.type;
  6976. ConvertToVariable(ctx);
  6977. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  6978. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  6979. // Return a null constant
  6980. ctx->bc.Instr(asBC_PshNull);
  6981. ctx->type.SetNullConstant();
  6982. }
  6983. else
  6984. {
  6985. // Allow "(a ? b : c) = d;" and "return (a ? b : c);" (where the latter returns the reference)
  6986. //
  6987. // Restrictions for the condition to be used as lvalue:
  6988. // 1. both b and c must be of the same type and be lvalue references
  6989. // 2. neither of the expressions can have any deferred arguments
  6990. // that would have to be cleaned up after the reference
  6991. // 3. neither expression can be temporary
  6992. //
  6993. // If either expression is local, the resulting lvalue is not valid
  6994. // for return since it is not allowed to return references to local
  6995. // variables.
  6996. //
  6997. // The reference to the local variable must be loaded into the register,
  6998. // the resulting expression must not be considered as a local variable
  6999. // with a stack offset (i.e. it will not be allowed to use asBC_VAR)
  7000. if( le.type.isLValue && re.type.isLValue &&
  7001. le.deferredParams.GetLength() == 0 && re.deferredParams.GetLength() ==0 &&
  7002. !le.type.isTemporary && !re.type.isTemporary &&
  7003. le.type.dataType == re.type.dataType )
  7004. {
  7005. // Put the code for the condition expression on the output
  7006. MergeExprBytecode(ctx, &e);
  7007. // Add the branch decision
  7008. ctx->type = e.type;
  7009. ConvertToVariable(ctx);
  7010. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  7011. ctx->bc.Instr(asBC_ClrHi);
  7012. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  7013. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7014. // Start of the left expression
  7015. MergeExprBytecode(ctx, &le);
  7016. if( !le.type.dataType.IsReference() && le.type.isVariable )
  7017. {
  7018. // Load the address of the variable into the register
  7019. ctx->bc.InstrSHORT(asBC_LDV, le.type.stackOffset);
  7020. }
  7021. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  7022. // Start of the right expression
  7023. ctx->bc.Label((short)elseLabel);
  7024. MergeExprBytecode(ctx, &re);
  7025. if( !re.type.dataType.IsReference() && re.type.isVariable )
  7026. {
  7027. // Load the address of the variable into the register
  7028. ctx->bc.InstrSHORT(asBC_LDV, re.type.stackOffset);
  7029. }
  7030. ctx->bc.Label((short)afterLabel);
  7031. // In case the options were to objects, it is necessary to dereference the pointer on
  7032. // the stack so it will point to the actual object, instead of the variable
  7033. if( le.type.dataType.IsReference() && le.type.dataType.IsObject() && !le.type.dataType.IsObjectHandle() )
  7034. {
  7035. asASSERT( re.type.dataType.IsReference() && re.type.dataType.IsObject() && !re.type.dataType.IsObjectHandle() );
  7036. ctx->bc.Instr(asBC_RDSPtr);
  7037. }
  7038. // The result is an lvalue
  7039. ctx->type.isLValue = true;
  7040. ctx->type.dataType = le.type.dataType;
  7041. if( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsObjectHandle() )
  7042. ctx->type.dataType.MakeReference(true);
  7043. else
  7044. ctx->type.dataType.MakeReference(false);
  7045. // It can't be a treated as a variable, since we don't know which one was used
  7046. ctx->type.isVariable = false;
  7047. ctx->type.isTemporary = false;
  7048. // Must remember if the reference was to a local variable, since it must not be allowed to be returned
  7049. ctx->type.isRefToLocal = le.type.isVariable || le.type.isRefToLocal || re.type.isVariable || re.type.isRefToLocal;
  7050. }
  7051. else
  7052. {
  7053. // Allocate temporary variable and copy the result to that one
  7054. asCExprValue temp;
  7055. temp = le.type;
  7056. temp.dataType.MakeReference(false);
  7057. temp.dataType.MakeReadOnly(false);
  7058. // Make sure the variable isn't used in any of the expressions,
  7059. // as it would be overwritten which may cause crashes or less visible bugs
  7060. int l = int(reservedVariables.GetLength());
  7061. e.bc.GetVarsUsed(reservedVariables);
  7062. le.bc.GetVarsUsed(reservedVariables);
  7063. re.bc.GetVarsUsed(reservedVariables);
  7064. int offset = AllocateVariable(temp.dataType, true, false);
  7065. reservedVariables.SetLength(l);
  7066. temp.SetVariable(temp.dataType, offset, true);
  7067. // TODO: copy: Use copy constructor if available. See PrepareTemporaryVariable()
  7068. CallDefaultConstructor(temp.dataType, offset, IsVariableOnHeap(offset), &ctx->bc, expr);
  7069. // Put the code for the condition expression on the output
  7070. MergeExprBytecode(ctx, &e);
  7071. // Add the branch decision
  7072. ctx->type = e.type;
  7073. ConvertToVariable(ctx);
  7074. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  7075. ctx->bc.Instr(asBC_ClrHi);
  7076. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  7077. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  7078. // Assign the result of the left expression to the temporary variable
  7079. asCExprValue rtemp;
  7080. rtemp = temp;
  7081. if( rtemp.dataType.IsObjectHandle() )
  7082. rtemp.isExplicitHandle = true;
  7083. PrepareForAssignment(&rtemp.dataType, &le, cexpr->next, true);
  7084. MergeExprBytecode(ctx, &le);
  7085. if( !rtemp.dataType.IsPrimitive() )
  7086. {
  7087. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7088. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  7089. }
  7090. asCExprValue result;
  7091. result = rtemp;
  7092. PerformAssignment(&result, &le.type, &ctx->bc, cexpr->next);
  7093. if( !result.dataType.IsPrimitive() )
  7094. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  7095. // Release the old temporary variable
  7096. ReleaseTemporaryVariable(le.type, &ctx->bc);
  7097. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  7098. // Start of the right expression
  7099. ctx->bc.Label((short)elseLabel);
  7100. // Copy the result to the same temporary variable
  7101. PrepareForAssignment(&rtemp.dataType, &re, cexpr->next, true);
  7102. MergeExprBytecode(ctx, &re);
  7103. if( !rtemp.dataType.IsPrimitive() )
  7104. {
  7105. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7106. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  7107. }
  7108. result = rtemp;
  7109. PerformAssignment(&result, &re.type, &ctx->bc, cexpr->next);
  7110. if( !result.dataType.IsPrimitive() )
  7111. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  7112. // Release the old temporary variable
  7113. ReleaseTemporaryVariable(re.type, &ctx->bc);
  7114. ctx->bc.Label((short)afterLabel);
  7115. // Make sure both expressions have the same type
  7116. if( !le.type.dataType.IsEqualExceptConst(re.type.dataType) )
  7117. Error(TXT_BOTH_MUST_BE_SAME, expr);
  7118. // Set the temporary variable as output
  7119. ctx->type = rtemp;
  7120. ctx->type.isExplicitHandle = isExplicitHandle;
  7121. if( !ctx->type.dataType.IsPrimitive() )
  7122. {
  7123. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7124. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  7125. }
  7126. // Make sure the output isn't marked as being a literal constant
  7127. ctx->type.isConstant = false;
  7128. }
  7129. }
  7130. }
  7131. else
  7132. {
  7133. ctx->type.SetDummy();
  7134. return -1;
  7135. }
  7136. }
  7137. else
  7138. return CompileExpression(cexpr, ctx);
  7139. return 0;
  7140. }
  7141. int asCCompiler::CompileExpression(asCScriptNode *expr, asCExprContext *ctx)
  7142. {
  7143. asASSERT(expr->nodeType == snExpression);
  7144. // Convert to polish post fix, i.e: a+b => ab+
  7145. asCArray<asCScriptNode *> postfix;
  7146. ConvertToPostFix(expr, postfix);
  7147. // Compile the postfix formatted expression
  7148. return CompilePostFixExpression(&postfix, ctx);
  7149. }
  7150. void asCCompiler::ConvertToPostFix(asCScriptNode *expr, asCArray<asCScriptNode *> &postfix)
  7151. {
  7152. // The algorithm that I've implemented here is similar to
  7153. // Djikstra's Shunting Yard algorithm, though I didn't know it at the time.
  7154. // ref: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
  7155. // Count the nodes in order to preallocate the buffers
  7156. int count = 0;
  7157. asCScriptNode *node = expr->firstChild;
  7158. while( node )
  7159. {
  7160. count++;
  7161. node = node->next;
  7162. }
  7163. asCArray<asCScriptNode *> stackA(count);
  7164. asCArray<asCScriptNode *> &stackB = postfix;
  7165. stackB.Allocate(count, false);
  7166. node = expr->firstChild;
  7167. while( node )
  7168. {
  7169. int precedence = GetPrecedence(node);
  7170. while( stackA.GetLength() > 0 &&
  7171. precedence <= GetPrecedence(stackA[stackA.GetLength()-1]) )
  7172. stackB.PushLast(stackA.PopLast());
  7173. stackA.PushLast(node);
  7174. node = node->next;
  7175. }
  7176. while( stackA.GetLength() > 0 )
  7177. stackB.PushLast(stackA.PopLast());
  7178. }
  7179. int asCCompiler::CompilePostFixExpression(asCArray<asCScriptNode *> *postfix, asCExprContext *ctx)
  7180. {
  7181. // Shouldn't send any byte code
  7182. asASSERT(ctx->bc.GetLastInstr() == -1);
  7183. // Set the context to a dummy type to avoid further
  7184. // errors in case the expression fails to compile
  7185. ctx->type.SetDummy();
  7186. // Evaluate the operands and operators
  7187. asCArray<asCExprContext*> free;
  7188. asCArray<asCExprContext*> expr;
  7189. int ret = 0;
  7190. for( asUINT n = 0; ret == 0 && n < postfix->GetLength(); n++ )
  7191. {
  7192. asCScriptNode *node = (*postfix)[n];
  7193. if( node->nodeType == snExprTerm )
  7194. {
  7195. asCExprContext *e = free.GetLength() ? free.PopLast() : asNEW(asCExprContext)(engine);
  7196. expr.PushLast(e);
  7197. e->exprNode = node;
  7198. ret = CompileExpressionTerm(node, e);
  7199. }
  7200. else
  7201. {
  7202. asCExprContext *r = expr.PopLast();
  7203. asCExprContext *l = expr.PopLast();
  7204. // Now compile the operator
  7205. asCExprContext *e = free.GetLength() ? free.PopLast() : asNEW(asCExprContext)(engine);
  7206. ret = CompileOperator(node, l, r, e);
  7207. expr.PushLast(e);
  7208. // Free the operands
  7209. l->Clear();
  7210. free.PushLast(l);
  7211. r->Clear();
  7212. free.PushLast(r);
  7213. }
  7214. }
  7215. if( ret == 0 )
  7216. {
  7217. asASSERT(expr.GetLength() == 1);
  7218. // The final result should be moved to the output context
  7219. MergeExprBytecodeAndType(ctx, expr[0]);
  7220. }
  7221. // Clean up
  7222. for( asUINT e = 0; e < expr.GetLength(); e++ )
  7223. asDELETE(expr[e], asCExprContext);
  7224. for( asUINT f = 0; f < free.GetLength(); f++ )
  7225. asDELETE(free[f], asCExprContext);
  7226. return ret;
  7227. }
  7228. int asCCompiler::CompileExpressionTerm(asCScriptNode *node, asCExprContext *ctx)
  7229. {
  7230. // Shouldn't send any byte code
  7231. asASSERT(ctx->bc.GetLastInstr() == -1);
  7232. // Check if this is an initialization of a temp object with an initialization list
  7233. if (node->firstChild && node->firstChild->nodeType == snDataType)
  7234. {
  7235. // TODO: It should be possible to infer the type of the object from where the
  7236. // expression will be used. The compilation of the initialization list
  7237. // should be deferred until it is known for what it will be used. It will
  7238. // then for example be possible to write expressions like:
  7239. //
  7240. // @dict = {{'key', 'value'}};
  7241. // funcTakingArrayOfInt({1,2,3,4});
  7242. // Determine the type of the temporary object
  7243. asCDataType dt = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  7244. // Do not allow constructing non-shared types in shared functions
  7245. if (outFunc->IsShared() &&
  7246. dt.GetTypeInfo() && !dt.GetTypeInfo()->IsShared())
  7247. {
  7248. asCString msg;
  7249. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetTypeInfo()->name.AddressOf());
  7250. Error(msg, node);
  7251. }
  7252. // Allocate and initialize the temporary object
  7253. int offset = AllocateVariable(dt, true);
  7254. CompileInitialization(node->lastChild, &ctx->bc, dt, node, offset, 0, 0);
  7255. // Push the reference to the object on the stack
  7256. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  7257. ctx->type.SetVariable(dt, offset, true);
  7258. ctx->type.isLValue = false;
  7259. // If the variable is allocated on the heap we have a reference,
  7260. // otherwise the actual object pointer is pushed on the stack.
  7261. if (IsVariableOnHeap(offset))
  7262. ctx->type.dataType.MakeReference(true);
  7263. return 0;
  7264. }
  7265. // Set the type as a dummy by default, in case of any compiler errors
  7266. ctx->type.SetDummy();
  7267. // Compile the value node
  7268. asCScriptNode *vnode = node->firstChild;
  7269. while( vnode->nodeType != snExprValue )
  7270. vnode = vnode->next;
  7271. asCExprContext v(engine);
  7272. int r = CompileExpressionValue(vnode, &v); if( r < 0 ) return r;
  7273. // Compile post fix operators
  7274. asCScriptNode *pnode = vnode->next;
  7275. while( pnode )
  7276. {
  7277. r = CompileExpressionPostOp(pnode, &v); if( r < 0 ) return r;
  7278. pnode = pnode->next;
  7279. }
  7280. // Compile pre fix operators
  7281. pnode = vnode->prev;
  7282. while( pnode )
  7283. {
  7284. r = CompileExpressionPreOp(pnode, &v); if( r < 0 ) return r;
  7285. pnode = pnode->prev;
  7286. }
  7287. // Return the byte code and final type description
  7288. MergeExprBytecodeAndType(ctx, &v);
  7289. return 0;
  7290. }
  7291. int asCCompiler::CompileVariableAccess(const asCString &name, const asCString &scope, asCExprContext *ctx, asCScriptNode *errNode, bool isOptional, bool noFunction, bool noGlobal, asCObjectType *objType)
  7292. {
  7293. bool found = false;
  7294. // It is a local variable or parameter?
  7295. // This is not accessible by default arg expressions
  7296. sVariable *v = 0;
  7297. if( !isCompilingDefaultArg && scope == "" && !objType && variables )
  7298. v = variables->GetVariable(name.AddressOf());
  7299. if( v )
  7300. {
  7301. found = true;
  7302. if( v->isPureConstant )
  7303. ctx->type.SetConstantData(v->type, v->constantValue);
  7304. else if( v->type.IsPrimitive() )
  7305. {
  7306. if( v->type.IsReference() )
  7307. {
  7308. // Copy the reference into the register
  7309. ctx->bc.InstrSHORT(asBC_PshVPtr, (short)v->stackOffset);
  7310. ctx->bc.Instr(asBC_PopRPtr);
  7311. ctx->type.Set(v->type);
  7312. }
  7313. else
  7314. ctx->type.SetVariable(v->type, v->stackOffset, false);
  7315. // Set as lvalue unless it is a const variable
  7316. if( !v->type.IsReadOnly() )
  7317. ctx->type.isLValue = true;
  7318. }
  7319. else
  7320. {
  7321. ctx->bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  7322. ctx->type.SetVariable(v->type, v->stackOffset, false);
  7323. // If the variable is allocated on the heap we have a reference,
  7324. // otherwise the actual object pointer is pushed on the stack.
  7325. if( v->onHeap || v->type.IsObjectHandle() ) ctx->type.dataType.MakeReference(true);
  7326. // Implicitly dereference handle parameters sent by reference
  7327. if( v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()) )
  7328. ctx->bc.Instr(asBC_RDSPtr);
  7329. // Mark the object as safe for access unless it is a handle, as the
  7330. // life time of the object is guaranteed throughout the scope.
  7331. if( !v->type.IsObjectHandle() )
  7332. ctx->type.isHandleSafe = true;
  7333. // Set as lvalue unless it is a const variable
  7334. if (!v->type.IsReadOnly())
  7335. ctx->type.isLValue = true;
  7336. }
  7337. }
  7338. // Is it a class member?
  7339. // This is not accessible by default arg expressions
  7340. if( !isCompilingDefaultArg && !found && ((objType) || (outFunc && outFunc->objectType && scope == "")) )
  7341. {
  7342. if( name == THIS_TOKEN && !objType )
  7343. {
  7344. asCDataType dt = asCDataType::CreateType(outFunc->objectType, outFunc->isReadOnly);
  7345. // The object pointer is located at stack position 0
  7346. ctx->bc.InstrSHORT(asBC_PSF, 0);
  7347. ctx->type.SetVariable(dt, 0, false);
  7348. ctx->type.dataType.MakeReference(true);
  7349. ctx->type.isLValue = true;
  7350. // The 'this' handle is always considered safe (i.e. life time guaranteed)
  7351. ctx->type.isHandleSafe = true;
  7352. found = true;
  7353. }
  7354. if( !found )
  7355. {
  7356. // See if there are any matching property accessors
  7357. asCExprContext access(engine);
  7358. if( objType )
  7359. access.type.Set(asCDataType::CreateType(objType, false));
  7360. else
  7361. access.type.Set(asCDataType::CreateType(outFunc->objectType, outFunc->isReadOnly));
  7362. access.type.dataType.MakeReference(true);
  7363. int r = 0;
  7364. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  7365. {
  7366. // This is an index access, check if there is a property accessor that takes an index arg
  7367. asCExprContext dummyArg(engine);
  7368. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, 0, true);
  7369. }
  7370. if( r == 0 )
  7371. {
  7372. // Normal property access
  7373. r = FindPropertyAccessor(name, &access, errNode, 0, true);
  7374. }
  7375. if( r < 0 ) return -1;
  7376. if( access.property_get || access.property_set )
  7377. {
  7378. if( !objType )
  7379. {
  7380. // Prepare the bytecode for the member access
  7381. // This is only done when accessing through the implicit this pointer
  7382. ctx->bc.InstrSHORT(asBC_PSF, 0);
  7383. }
  7384. MergeExprBytecodeAndType(ctx, &access);
  7385. found = true;
  7386. }
  7387. }
  7388. if( !found )
  7389. {
  7390. asCDataType dt;
  7391. if( objType )
  7392. dt = asCDataType::CreateType(objType, false);
  7393. else
  7394. dt = asCDataType::CreateType(outFunc->objectType, false);
  7395. asCObjectProperty *prop = builder->GetObjectProperty(dt, name.AddressOf());
  7396. if( prop )
  7397. {
  7398. // Is the property access allowed?
  7399. if( prop->isPrivate && prop->isInherited )
  7400. {
  7401. if( engine->ep.privatePropAsProtected )
  7402. {
  7403. // The application is allowing inherited classes to access private properties of the parent
  7404. // class. This option is allowed to provide backwards compatibility with pre-2.30.0 versions
  7405. // as it was how the compiler behaved earlier.
  7406. asCString msg;
  7407. msg.Format(TXT_ACCESSING_PRIVATE_PROP_s, name.AddressOf());
  7408. Warning(msg, errNode);
  7409. }
  7410. else
  7411. {
  7412. asCString msg;
  7413. msg.Format(TXT_INHERITED_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  7414. Error(msg, errNode);
  7415. }
  7416. }
  7417. if( !objType )
  7418. {
  7419. // The object pointer is located at stack position 0
  7420. // This is only done when accessing through the implicit this pointer
  7421. ctx->bc.InstrSHORT(asBC_PSF, 0);
  7422. ctx->type.SetVariable(dt, 0, false);
  7423. ctx->type.dataType.MakeReference(true);
  7424. Dereference(ctx, true);
  7425. }
  7426. // TODO: This is the same as what is in CompileExpressionPostOp
  7427. // Put the offset on the stack
  7428. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(dt));
  7429. if( prop->type.IsReference() )
  7430. ctx->bc.Instr(asBC_RDSPtr);
  7431. // Reference to primitive must be stored in the temp register
  7432. if( prop->type.IsPrimitive() )
  7433. {
  7434. // TODO: runtime optimize: The ADD offset command should store the reference in the register directly
  7435. ctx->bc.Instr(asBC_PopRPtr);
  7436. }
  7437. // Set the new type (keeping info about temp variable)
  7438. ctx->type.dataType = prop->type;
  7439. ctx->type.dataType.MakeReference(true);
  7440. ctx->type.isVariable = false;
  7441. ctx->type.isLValue = true;
  7442. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  7443. {
  7444. // Objects that are members are not references
  7445. ctx->type.dataType.MakeReference(false);
  7446. // Objects that are members but not handles are safe as long as the parent object is safe
  7447. if (!objType || ctx->type.isHandleSafe)
  7448. ctx->type.isHandleSafe = true;
  7449. }
  7450. else if (ctx->type.dataType.IsObjectHandle())
  7451. {
  7452. // Objects accessed through handles cannot be considered safe
  7453. // as the handle can be cleared at any time
  7454. ctx->type.isHandleSafe = false;
  7455. }
  7456. // If the object reference is const, the property will also be const
  7457. ctx->type.dataType.MakeReadOnly(outFunc->isReadOnly);
  7458. found = true;
  7459. }
  7460. else if( outFunc->objectType )
  7461. {
  7462. // If it is not a property, it may still be the name of a method which can be used to create delegates
  7463. asCObjectType *ot = outFunc->objectType;
  7464. asCScriptFunction *func = 0;
  7465. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  7466. {
  7467. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  7468. if( f->name == name &&
  7469. (builder->module->accessMask & f->accessMask) )
  7470. {
  7471. func = f;
  7472. break;
  7473. }
  7474. }
  7475. if( func )
  7476. {
  7477. // An object method was found. Keep the name of the method in the expression, but
  7478. // don't actually modify the bytecode at this point since it is not yet known what
  7479. // the method will be used for, or even what overloaded method should be used.
  7480. ctx->methodName = name;
  7481. // Place the object pointer on the stack, as if the expression was this.func
  7482. if( !objType )
  7483. {
  7484. // The object pointer is located at stack position 0
  7485. // This is only done when accessing through the implicit this pointer
  7486. ctx->bc.InstrSHORT(asBC_PSF, 0);
  7487. ctx->type.SetVariable(asCDataType::CreateType(outFunc->objectType, false), 0, false);
  7488. ctx->type.dataType.MakeReference(true);
  7489. Dereference(ctx, true);
  7490. }
  7491. found = true;
  7492. }
  7493. }
  7494. }
  7495. }
  7496. // Recursively search parent namespaces for global entities
  7497. asCString currScope = scope;
  7498. // Get the namespace for this scope. This may return null if the scope is an enum
  7499. asSNameSpace *ns = DetermineNameSpace(currScope);
  7500. if( ns && currScope != "::" )
  7501. currScope = ns->name;
  7502. while( !found && !noGlobal && !objType )
  7503. {
  7504. // Is it a global property?
  7505. if( !found && ns )
  7506. {
  7507. // See if there are any matching global property accessors
  7508. asCExprContext access(engine);
  7509. int r = 0;
  7510. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  7511. {
  7512. // This is an index access, check if there is a property accessor that takes an index arg
  7513. asCExprContext dummyArg(engine);
  7514. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, ns);
  7515. }
  7516. if( r == 0 )
  7517. {
  7518. // Normal property access
  7519. r = FindPropertyAccessor(name, &access, errNode, ns);
  7520. }
  7521. if( r < 0 ) return -1;
  7522. if( access.property_get || access.property_set )
  7523. {
  7524. // Prepare the bytecode for the function call
  7525. MergeExprBytecodeAndType(ctx, &access);
  7526. found = true;
  7527. }
  7528. // See if there is any matching global property
  7529. if( !found )
  7530. {
  7531. bool isCompiled = true;
  7532. bool isPureConstant = false;
  7533. bool isAppProp = false;
  7534. asQWORD constantValue = 0;
  7535. asCGlobalProperty *prop = builder->GetGlobalProperty(name.AddressOf(), ns, &isCompiled, &isPureConstant, &constantValue, &isAppProp);
  7536. if( prop )
  7537. {
  7538. found = true;
  7539. // Verify that the global property has been compiled already
  7540. if( isCompiled )
  7541. {
  7542. if( ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE) )
  7543. {
  7544. ctx->type.dataType.MakeHandle(true);
  7545. ctx->type.isExplicitHandle = true;
  7546. }
  7547. // If the global property is a pure constant
  7548. // we can allow the compiler to optimize it. Pure
  7549. // constants are global constant variables that were
  7550. // initialized by literal constants.
  7551. if (isPureConstant)
  7552. ctx->type.SetConstantData(prop->type, constantValue);
  7553. else
  7554. {
  7555. // A shared type must not access global vars, unless they
  7556. // too are shared, e.g. application registered vars
  7557. if( outFunc->IsShared() )
  7558. {
  7559. if( !isAppProp )
  7560. {
  7561. asCString str;
  7562. str.Format(TXT_SHARED_CANNOT_ACCESS_NON_SHARED_VAR_s, prop->name.AddressOf());
  7563. Error(str, errNode);
  7564. // Allow the compilation to continue to catch other problems
  7565. }
  7566. }
  7567. ctx->type.Set(prop->type);
  7568. ctx->type.isLValue = true;
  7569. if( ctx->type.dataType.IsPrimitive() )
  7570. {
  7571. // Load the address of the variable into the register
  7572. ctx->bc.InstrPTR(asBC_LDG, prop->GetAddressOfValue());
  7573. ctx->type.dataType.MakeReference(true);
  7574. }
  7575. else
  7576. {
  7577. // Push the address of the variable on the stack
  7578. ctx->bc.InstrPTR(asBC_PGA, prop->GetAddressOfValue());
  7579. // If the object is a value type or a non-handle variable to a reference type,
  7580. // then we must validate the existance as it could potentially be accessed
  7581. // before it is initialized.
  7582. // This check is not needed for application registered properties, since they
  7583. // are guaranteed to be valid by the application itself.
  7584. if( !isAppProp &&
  7585. ((ctx->type.dataType.GetTypeInfo()->flags & asOBJ_VALUE) ||
  7586. !ctx->type.dataType.IsObjectHandle()) )
  7587. {
  7588. ctx->bc.Instr(asBC_ChkRefS);
  7589. }
  7590. // If the address pushed on the stack is to a value type or an object
  7591. // handle, then mark the expression as a reference. Addresses to a reference
  7592. // type aren't marked as references to get correct behaviour
  7593. if( (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_VALUE) ||
  7594. ctx->type.dataType.IsObjectHandle() )
  7595. {
  7596. ctx->type.dataType.MakeReference(true);
  7597. }
  7598. else
  7599. {
  7600. asASSERT( (ctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !ctx->type.dataType.IsObjectHandle() );
  7601. // It's necessary to dereference the pointer so the pointer on the stack will point to the actual object
  7602. ctx->bc.Instr(asBC_RDSPtr);
  7603. }
  7604. }
  7605. }
  7606. }
  7607. else
  7608. {
  7609. asCString str;
  7610. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, prop->name.AddressOf());
  7611. Error(str, errNode);
  7612. return -1;
  7613. }
  7614. }
  7615. }
  7616. }
  7617. // Is it the name of a global function?
  7618. if( !noFunction && !found && ns )
  7619. {
  7620. asCArray<int> funcs;
  7621. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  7622. if( funcs.GetLength() > 0 )
  7623. {
  7624. found = true;
  7625. // Defer the evaluation of which function until it is actually used
  7626. // Store the namespace and name of the function for later
  7627. ctx->type.SetUndefinedFuncHandle(engine);
  7628. ctx->methodName = ns ? ns->name + "::" + name : name;
  7629. }
  7630. }
  7631. // Is it an enum value?
  7632. if( !found )
  7633. {
  7634. // The enum type may be declared in a namespace too
  7635. asCTypeInfo *scopeType = 0;
  7636. if( currScope != "" && currScope != "::" )
  7637. {
  7638. builder->GetNameSpaceByString(currScope, outFunc->objectType ? outFunc->objectType->nameSpace : outFunc->nameSpace, errNode, script, &scopeType, false);
  7639. if (CastToEnumType(scopeType) == 0)
  7640. scopeType = 0;
  7641. }
  7642. asDWORD value = 0;
  7643. asCDataType dt;
  7644. if( scopeType && builder->GetEnumValueFromType(CastToEnumType(scopeType), name.AddressOf(), dt, value) )
  7645. {
  7646. // scoped enum value found
  7647. found = true;
  7648. }
  7649. else if( !engine->ep.requireEnumScope )
  7650. {
  7651. // Look for the enum value without explicitly informing the enum type
  7652. asSNameSpace *nsEnum = DetermineNameSpace(currScope);
  7653. int e = 0;
  7654. if(nsEnum)
  7655. e = builder->GetEnumValue(name.AddressOf(), dt, value, nsEnum);
  7656. if( e )
  7657. {
  7658. found = true;
  7659. if( e == 2 )
  7660. {
  7661. // Ambiguous enum value: Save the name for resolution later.
  7662. // The ambiguity could be resolved now, but I hesitate
  7663. // to store too much information in the context.
  7664. ctx->enumValue = name.AddressOf();
  7665. // We cannot set a dummy value because it will pass through
  7666. // cleanly as an integer.
  7667. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttIdentifier, true), 0);
  7668. return 0;
  7669. }
  7670. }
  7671. }
  7672. if( found )
  7673. {
  7674. // Even if the enum type is not shared, and we're compiling a shared object,
  7675. // the use of the values are still allowed, since they are treated as constants.
  7676. // an enum value was resolved
  7677. ctx->type.SetConstantDW(dt, value);
  7678. }
  7679. else
  7680. {
  7681. // If nothing was found because the scope doesn't match a namespace or an enum
  7682. // then this should be reported as an error and the search interrupted
  7683. if( !ns && !scopeType )
  7684. {
  7685. ctx->type.SetDummy();
  7686. asCString str;
  7687. str.Format(TXT_UNKNOWN_SCOPE_s, scope.AddressOf());
  7688. Error(str, errNode);
  7689. return -1;
  7690. }
  7691. }
  7692. }
  7693. if( !found )
  7694. {
  7695. if( currScope == "" || currScope == "::" )
  7696. break;
  7697. // Move up to parent namespace
  7698. int pos = currScope.FindLast("::");
  7699. if( pos >= 0 )
  7700. currScope = currScope.SubString(0, pos);
  7701. else
  7702. currScope = "::";
  7703. if( ns )
  7704. ns = engine->GetParentNameSpace(ns);
  7705. }
  7706. }
  7707. // The name doesn't match any variable
  7708. if( !found )
  7709. {
  7710. // Give dummy value
  7711. ctx->type.SetDummy();
  7712. if( !isOptional )
  7713. {
  7714. // Prepend the scope to the name for the error message
  7715. asCString ename;
  7716. if( scope != "" && scope != "::" )
  7717. ename = scope + "::";
  7718. else
  7719. ename = scope;
  7720. ename += name;
  7721. asCString str;
  7722. str.Format(TXT_s_NOT_DECLARED, ename.AddressOf());
  7723. Error(str, errNode);
  7724. // Declare the variable now so that it will not be reported again
  7725. variables->DeclareVariable(name.AddressOf(), asCDataType::CreatePrimitive(ttInt, false), 0x7FFF, true);
  7726. // Mark the variable as initialized so that the user will not be bother by it again
  7727. v = variables->GetVariable(name.AddressOf());
  7728. asASSERT(v);
  7729. if( v ) v->isInitialized = true;
  7730. }
  7731. // Return -1 to signal that the variable wasn't found
  7732. return -1;
  7733. }
  7734. return 0;
  7735. }
  7736. int asCCompiler::CompileExpressionValue(asCScriptNode *node, asCExprContext *ctx)
  7737. {
  7738. // Shouldn't receive any byte code
  7739. asASSERT(ctx->bc.GetLastInstr() == -1);
  7740. asCScriptNode *vnode = node->firstChild;
  7741. ctx->exprNode = vnode;
  7742. if( vnode->nodeType == snVariableAccess )
  7743. {
  7744. // Determine the scope resolution of the variable
  7745. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script, &vnode);
  7746. // Determine the name of the variable
  7747. asASSERT(vnode->nodeType == snIdentifier );
  7748. asCString name(&script->code[vnode->tokenPos], vnode->tokenLength);
  7749. return CompileVariableAccess(name, scope, ctx, node);
  7750. }
  7751. else if( vnode->nodeType == snConstant )
  7752. {
  7753. if( vnode->tokenType == ttIntConstant )
  7754. {
  7755. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  7756. bool overflow = false;
  7757. asQWORD val = asStringScanUInt64(value.AddressOf(), 10, 0, &overflow);
  7758. // Is the number bigger than a 64bit word?
  7759. if (overflow)
  7760. {
  7761. Error(TXT_VALUE_TOO_LARGE_FOR_TYPE, vnode);
  7762. // Set the value to zero to avoid further warnings
  7763. val = 0;
  7764. }
  7765. // Do we need 64 bits?
  7766. // If the 31st bit is set we'll treat the value as a signed 64bit number to avoid
  7767. // incorrect warnings about changing signs if the value is assigned to a 64bit variable
  7768. if( val>>31 )
  7769. {
  7770. // Only if the value uses the last bit of a 64bit word do we consider the number unsigned
  7771. if( val>>63 )
  7772. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  7773. else
  7774. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), val);
  7775. }
  7776. else
  7777. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), asDWORD(val));
  7778. }
  7779. else if( vnode->tokenType == ttBitsConstant )
  7780. {
  7781. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  7782. // Let the function determine the radix from the prefix 0x = 16, 0d = 10, 0o = 8, or 0b = 2
  7783. bool overflow = false;
  7784. asQWORD val = asStringScanUInt64(value.AddressOf(), 0, 0, &overflow);
  7785. // Is the number bigger than a 64bit word?
  7786. if (overflow)
  7787. {
  7788. Error(TXT_VALUE_TOO_LARGE_FOR_TYPE, vnode);
  7789. // Set the value to zero to avoid further warnings
  7790. val = 0;
  7791. }
  7792. // Do we need 64 bits?
  7793. if( val>>32 )
  7794. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  7795. else
  7796. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  7797. }
  7798. else if( vnode->tokenType == ttFloatConstant )
  7799. {
  7800. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  7801. // TODO: Check for overflow
  7802. size_t numScanned;
  7803. float v = float(asStringScanDouble(value.AddressOf(), &numScanned));
  7804. ctx->type.SetConstantF(asCDataType::CreatePrimitive(ttFloat, true), v);
  7805. #ifndef AS_USE_DOUBLE_AS_FLOAT
  7806. // Don't check this if we have double as float, because then the whole token would be scanned (i.e. no f suffix)
  7807. asASSERT(numScanned == vnode->tokenLength - 1);
  7808. #endif
  7809. }
  7810. else if( vnode->tokenType == ttDoubleConstant )
  7811. {
  7812. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  7813. // TODO: Check for overflow
  7814. size_t numScanned;
  7815. double v = asStringScanDouble(value.AddressOf(), &numScanned);
  7816. ctx->type.SetConstantD(asCDataType::CreatePrimitive(ttDouble, true), v);
  7817. asASSERT(numScanned == vnode->tokenLength);
  7818. }
  7819. else if( vnode->tokenType == ttTrue ||
  7820. vnode->tokenType == ttFalse )
  7821. {
  7822. #if AS_SIZEOF_BOOL == 1
  7823. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  7824. #else
  7825. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  7826. #endif
  7827. }
  7828. else if( vnode->tokenType == ttStringConstant ||
  7829. vnode->tokenType == ttMultilineStringConstant ||
  7830. vnode->tokenType == ttHeredocStringConstant )
  7831. {
  7832. asCString str;
  7833. asCScriptNode *snode = vnode->firstChild;
  7834. if( script->code[snode->tokenPos] == '\'' && engine->ep.useCharacterLiterals )
  7835. {
  7836. // Treat the single quoted string as a single character literal
  7837. str.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  7838. asDWORD val = 0;
  7839. if( str.GetLength() && (unsigned char)str[0] > 127 && engine->ep.scanner == 1 )
  7840. {
  7841. // This is the start of a UTF8 encoded character. We need to decode it
  7842. val = asStringDecodeUTF8(str.AddressOf(), 0);
  7843. if( val == (asDWORD)-1 )
  7844. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  7845. }
  7846. else
  7847. {
  7848. val = ProcessStringConstant(str, snode);
  7849. if( val == (asDWORD)-1 )
  7850. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  7851. }
  7852. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), val);
  7853. }
  7854. else
  7855. {
  7856. // Process the string constants
  7857. while( snode )
  7858. {
  7859. asCString cat;
  7860. if( snode->tokenType == ttStringConstant )
  7861. {
  7862. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  7863. ProcessStringConstant(cat, snode);
  7864. }
  7865. else if( snode->tokenType == ttMultilineStringConstant )
  7866. {
  7867. if( !engine->ep.allowMultilineStrings )
  7868. Error(TXT_MULTILINE_STRINGS_NOT_ALLOWED, snode);
  7869. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  7870. ProcessStringConstant(cat, snode);
  7871. }
  7872. else if( snode->tokenType == ttHeredocStringConstant )
  7873. {
  7874. cat.Assign(&script->code[snode->tokenPos+3], snode->tokenLength-6);
  7875. ProcessHeredocStringConstant(cat, snode);
  7876. }
  7877. str += cat;
  7878. snode = snode->next;
  7879. }
  7880. // Call the string factory function to create a string object
  7881. asCScriptFunction *descr = engine->stringFactory;
  7882. if( descr == 0 )
  7883. {
  7884. // Error
  7885. Error(TXT_STRINGS_NOT_RECOGNIZED, vnode);
  7886. // Give dummy value
  7887. ctx->type.SetDummy();
  7888. return -1;
  7889. }
  7890. else
  7891. {
  7892. // Register the constant string with the engine
  7893. int id = engine->AddConstantString(str.AddressOf(), str.GetLength());
  7894. ctx->bc.InstrWORD(asBC_STR, (asWORD)id);
  7895. bool useVariable = false;
  7896. int stackOffset = 0;
  7897. if( descr->DoesReturnOnStack() )
  7898. {
  7899. useVariable = true;
  7900. stackOffset = AllocateVariable(descr->returnType, true);
  7901. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  7902. }
  7903. PerformFunctionCall(descr->id, ctx, false, 0, 0, useVariable, stackOffset);
  7904. }
  7905. }
  7906. }
  7907. else if( vnode->tokenType == ttNull )
  7908. {
  7909. ctx->bc.Instr(asBC_PshNull);
  7910. ctx->type.SetNullConstant();
  7911. }
  7912. else
  7913. asASSERT(false);
  7914. }
  7915. else if( vnode->nodeType == snFunctionCall )
  7916. {
  7917. // Determine the scope resolution
  7918. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script);
  7919. return CompileFunctionCall(vnode, ctx, 0, false, scope);
  7920. }
  7921. else if( vnode->nodeType == snConstructCall )
  7922. {
  7923. return CompileConstructCall(vnode, ctx);
  7924. }
  7925. else if( vnode->nodeType == snAssignment )
  7926. {
  7927. asCExprContext e(engine);
  7928. int r = CompileAssignment(vnode, &e);
  7929. if( r < 0 )
  7930. {
  7931. ctx->type.SetDummy();
  7932. return r;
  7933. }
  7934. MergeExprBytecodeAndType(ctx, &e);
  7935. }
  7936. else if( vnode->nodeType == snCast )
  7937. {
  7938. // Implement the cast operator
  7939. return CompileConversion(vnode, ctx);
  7940. }
  7941. else if( vnode->nodeType == snUndefined && vnode->tokenType == ttVoid )
  7942. {
  7943. // This is a void expression
  7944. ctx->SetVoidExpression();
  7945. }
  7946. else if( vnode->nodeType == snFunction )
  7947. {
  7948. // This is an anonymous function
  7949. // Defer the evaluation of the function until it known where it
  7950. // will be used, which is where the signature will be defined
  7951. ctx->SetLambda(vnode);
  7952. }
  7953. else
  7954. asASSERT(false);
  7955. return 0;
  7956. }
  7957. asUINT asCCompiler::ProcessStringConstant(asCString &cstr, asCScriptNode *node, bool processEscapeSequences)
  7958. {
  7959. int charLiteral = -1;
  7960. // Process escape sequences
  7961. asCArray<char> str((int)cstr.GetLength());
  7962. for( asUINT n = 0; n < cstr.GetLength(); n++ )
  7963. {
  7964. #ifdef AS_DOUBLEBYTE_CHARSET
  7965. // Double-byte charset is only allowed for ASCII and not UTF16 encoded strings
  7966. if( (cstr[n] & 0x80) && engine->ep.scanner == 0 && engine->ep.stringEncoding != 1 )
  7967. {
  7968. // This is the lead character of a double byte character
  7969. // include the trail character without checking it's value.
  7970. str.PushLast(cstr[n]);
  7971. n++;
  7972. str.PushLast(cstr[n]);
  7973. continue;
  7974. }
  7975. #endif
  7976. asUINT val;
  7977. if( processEscapeSequences && cstr[n] == '\\' )
  7978. {
  7979. ++n;
  7980. if( n == cstr.GetLength() )
  7981. {
  7982. if( charLiteral == -1 ) charLiteral = 0;
  7983. return charLiteral;
  7984. }
  7985. // Hexadecimal escape sequences will allow the construction of
  7986. // invalid unicode sequences, but the string should also work as
  7987. // a bytearray so we must support this. The code for working with
  7988. // unicode text must be prepared to handle invalid unicode sequences
  7989. if( cstr[n] == 'x' || cstr[n] == 'X' )
  7990. {
  7991. ++n;
  7992. if( n == cstr.GetLength() ) break;
  7993. val = 0;
  7994. int c = engine->ep.stringEncoding == 1 ? 4 : 2;
  7995. for( ; c > 0 && n < cstr.GetLength(); c--, n++ )
  7996. {
  7997. if( cstr[n] >= '0' && cstr[n] <= '9' )
  7998. val = val*16 + cstr[n] - '0';
  7999. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  8000. val = val*16 + cstr[n] - 'a' + 10;
  8001. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  8002. val = val*16 + cstr[n] - 'A' + 10;
  8003. else
  8004. break;
  8005. }
  8006. // Rewind one, since the loop will increment it again
  8007. n--;
  8008. // Hexadecimal escape sequences produce exact value, even if it is not proper unicode chars
  8009. if( engine->ep.stringEncoding == 0 )
  8010. {
  8011. str.PushLast((asBYTE)val);
  8012. }
  8013. else
  8014. {
  8015. #ifndef AS_BIG_ENDIAN
  8016. str.PushLast((asBYTE)val);
  8017. str.PushLast((asBYTE)(val>>8));
  8018. #else
  8019. str.PushLast((asBYTE)(val>>8));
  8020. str.PushLast((asBYTE)val);
  8021. #endif
  8022. }
  8023. if( charLiteral == -1 ) charLiteral = val;
  8024. continue;
  8025. }
  8026. else if( cstr[n] == 'u' || cstr[n] == 'U' )
  8027. {
  8028. // \u expects 4 hex digits
  8029. // \U expects 8 hex digits
  8030. bool expect2 = cstr[n] == 'u';
  8031. int c = expect2 ? 4 : 8;
  8032. val = 0;
  8033. for( ; c > 0; c-- )
  8034. {
  8035. ++n;
  8036. if( n == cstr.GetLength() ) break;
  8037. if( cstr[n] >= '0' && cstr[n] <= '9' )
  8038. val = val*16 + cstr[n] - '0';
  8039. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  8040. val = val*16 + cstr[n] - 'a' + 10;
  8041. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  8042. val = val*16 + cstr[n] - 'A' + 10;
  8043. else
  8044. break;
  8045. }
  8046. if( c != 0 )
  8047. {
  8048. // Give warning about invalid code point
  8049. // TODO: Need code position for warning
  8050. asCString msg;
  8051. msg.Format(TXT_INVALID_UNICODE_FORMAT_EXPECTED_d, expect2 ? 4 : 8);
  8052. Warning(msg, node);
  8053. continue;
  8054. }
  8055. }
  8056. else
  8057. {
  8058. if( cstr[n] == '"' )
  8059. val = '"';
  8060. else if( cstr[n] == '\'' )
  8061. val = '\'';
  8062. else if( cstr[n] == 'n' )
  8063. val = '\n';
  8064. else if( cstr[n] == 'r' )
  8065. val = '\r';
  8066. else if( cstr[n] == 't' )
  8067. val = '\t';
  8068. else if( cstr[n] == '0' )
  8069. val = '\0';
  8070. else if( cstr[n] == '\\' )
  8071. val = '\\';
  8072. else
  8073. {
  8074. // Invalid escape sequence
  8075. Warning(TXT_INVALID_ESCAPE_SEQUENCE, node);
  8076. continue;
  8077. }
  8078. }
  8079. }
  8080. else
  8081. {
  8082. if( engine->ep.scanner == 1 && (cstr[n] & 0x80) )
  8083. {
  8084. unsigned int len;
  8085. val = asStringDecodeUTF8(&cstr[n], &len);
  8086. if( val == 0xFFFFFFFF )
  8087. {
  8088. // Incorrect UTF8 encoding. Use only the first byte
  8089. // TODO: Need code position for warning
  8090. Warning(TXT_INVALID_UNICODE_SEQUENCE_IN_SRC, node);
  8091. val = (unsigned char)cstr[n];
  8092. }
  8093. else
  8094. n += len-1;
  8095. }
  8096. else
  8097. val = (unsigned char)cstr[n];
  8098. }
  8099. // Add the character to the final string
  8100. char encodedValue[5];
  8101. int len;
  8102. if( engine->ep.scanner == 1 && engine->ep.stringEncoding == 0 )
  8103. {
  8104. // Convert to UTF8 encoded
  8105. len = asStringEncodeUTF8(val, encodedValue);
  8106. }
  8107. else if( engine->ep.stringEncoding == 1 )
  8108. {
  8109. // Convert to 16bit wide character string (even if the script is scanned as ASCII)
  8110. len = asStringEncodeUTF16(val, encodedValue);
  8111. }
  8112. else
  8113. {
  8114. // Do not convert ASCII characters
  8115. encodedValue[0] = (asBYTE)val;
  8116. len = 1;
  8117. }
  8118. if( len < 0 )
  8119. {
  8120. // Give warning about invalid code point
  8121. // TODO: Need code position for warning
  8122. Warning(TXT_INVALID_UNICODE_VALUE, node);
  8123. }
  8124. else
  8125. {
  8126. // Add the encoded value to the final string
  8127. str.Concatenate(encodedValue, len);
  8128. if( charLiteral == -1 ) charLiteral = val;
  8129. }
  8130. }
  8131. cstr.Assign(str.AddressOf(), str.GetLength());
  8132. return charLiteral;
  8133. }
  8134. void asCCompiler::ProcessHeredocStringConstant(asCString &str, asCScriptNode *node)
  8135. {
  8136. // Remove first line if it only contains whitespace
  8137. bool isMultiline = false;
  8138. int start;
  8139. for( start = 0; start < (int)str.GetLength(); start++ )
  8140. {
  8141. if( str[start] == '\n' )
  8142. {
  8143. isMultiline = true;
  8144. // Remove the linebreak as well
  8145. start++;
  8146. break;
  8147. }
  8148. if( str[start] != ' ' &&
  8149. str[start] != '\t' &&
  8150. str[start] != '\r' )
  8151. {
  8152. // Don't remove anything
  8153. start = 0;
  8154. break;
  8155. }
  8156. }
  8157. // Remove the line after the last line break if it only contains whitespaces
  8158. int end;
  8159. for( end = (int)str.GetLength() - 1; end >= 0; end-- )
  8160. {
  8161. if( str[end] == '\n' )
  8162. {
  8163. // Don't remove the last line break
  8164. end++;
  8165. break;
  8166. }
  8167. if( str[end] != ' ' &&
  8168. str[end] != '\t' &&
  8169. str[end] != '\r' )
  8170. {
  8171. // Don't remove anything
  8172. end = (int)str.GetLength();
  8173. break;
  8174. }
  8175. }
  8176. if( end < 0 ) end = 0;
  8177. asCString tmp;
  8178. if (end > start || engine->ep.heredocTrimMode != 2 )
  8179. {
  8180. // if heredocTrimMode == 0 the string shouldn't be trimmed
  8181. // if heredocTrimMode == 1 the string should only be trimmed if it is multiline
  8182. // if heredocTrimMode == 2 the string should always be trimmed
  8183. if (engine->ep.heredocTrimMode == 2 || (isMultiline && engine->ep.heredocTrimMode == 1))
  8184. tmp.Assign(&str[start], end - start);
  8185. else
  8186. tmp = str;
  8187. }
  8188. ProcessStringConstant(tmp, node, false);
  8189. str = tmp;
  8190. }
  8191. int asCCompiler::CompileConversion(asCScriptNode *node, asCExprContext *ctx)
  8192. {
  8193. asCExprContext expr(engine);
  8194. asCDataType to;
  8195. bool anyErrors = false;
  8196. EImplicitConv convType;
  8197. if( node->nodeType == snConstructCall )
  8198. {
  8199. convType = asIC_EXPLICIT_VAL_CAST;
  8200. // Verify that there is only one argument
  8201. if( node->lastChild->firstChild == 0 ||
  8202. node->lastChild->firstChild != node->lastChild->lastChild )
  8203. {
  8204. Error(TXT_ONLY_ONE_ARGUMENT_IN_CAST, node->lastChild);
  8205. expr.type.SetDummy();
  8206. anyErrors = true;
  8207. }
  8208. else
  8209. {
  8210. // Compile the expression
  8211. int r = CompileAssignment(node->lastChild->firstChild, &expr);
  8212. if( r < 0 )
  8213. anyErrors = true;
  8214. }
  8215. // Determine the requested type
  8216. to = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  8217. to.MakeReadOnly(true); // Default to const
  8218. asASSERT(to.IsPrimitive());
  8219. }
  8220. else
  8221. {
  8222. convType = asIC_EXPLICIT_REF_CAST;
  8223. // Compile the expression
  8224. int r = CompileAssignment(node->lastChild, &expr);
  8225. if( r < 0 )
  8226. anyErrors = true;
  8227. // Determine the requested type
  8228. to = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  8229. // If the type support object handles, then use it
  8230. if( to.SupportHandles() )
  8231. {
  8232. to.MakeHandle(true);
  8233. if( expr.type.dataType.IsObjectConst() )
  8234. to.MakeHandleToConst(true);
  8235. }
  8236. else if( !to.IsObjectHandle() )
  8237. {
  8238. // The cast<type> operator can only be used for reference casts
  8239. Error(TXT_ILLEGAL_TARGET_TYPE_FOR_REF_CAST, node->firstChild);
  8240. anyErrors = true;
  8241. }
  8242. }
  8243. // Do not allow casting to non shared type if we're compiling a shared method
  8244. if( outFunc->IsShared() &&
  8245. to.GetTypeInfo() && !to.GetTypeInfo()->IsShared() )
  8246. {
  8247. asCString msg;
  8248. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, to.GetTypeInfo()->name.AddressOf());
  8249. Error(msg, node);
  8250. anyErrors = true;
  8251. }
  8252. if( anyErrors )
  8253. {
  8254. // Assume that the error can be fixed and allow the compilation to continue
  8255. ctx->type.Set(to);
  8256. return -1;
  8257. }
  8258. ProcessPropertyGetAccessor(&expr, node);
  8259. // Don't allow any operators on expressions that take address of class method
  8260. if( expr.IsClassMethod() )
  8261. {
  8262. Error(TXT_INVALID_OP_ON_METHOD, node);
  8263. return -1;
  8264. }
  8265. // We don't want a reference for conversion casts
  8266. if( convType == asIC_EXPLICIT_VAL_CAST && expr.type.dataType.IsReference() )
  8267. {
  8268. if( expr.type.dataType.IsObject() )
  8269. Dereference(&expr, true);
  8270. else
  8271. ConvertToVariable(&expr);
  8272. }
  8273. ImplicitConversion(&expr, to, node, convType);
  8274. IsVariableInitialized(&expr.type, node);
  8275. // If no type conversion is really tried ignore it
  8276. if( to == expr.type.dataType )
  8277. {
  8278. // This will keep information about constant type
  8279. MergeExprBytecode(ctx, &expr);
  8280. ctx->type = expr.type;
  8281. return 0;
  8282. }
  8283. if( to.IsEqualExceptRefAndConst(expr.type.dataType) && to.IsPrimitive() )
  8284. {
  8285. MergeExprBytecode(ctx, &expr);
  8286. ctx->type = expr.type;
  8287. ctx->type.dataType.MakeReadOnly(true);
  8288. return 0;
  8289. }
  8290. // The implicit conversion already does most of the conversions permitted,
  8291. // here we'll only treat those conversions that require an explicit cast.
  8292. bool conversionOK = false;
  8293. if( !expr.type.isConstant && expr.type.dataType != asCDataType::CreatePrimitive(ttVoid, false) )
  8294. {
  8295. if( !expr.type.dataType.IsObject() )
  8296. ConvertToTempVariable(&expr);
  8297. if( to.IsObjectHandle() &&
  8298. expr.type.dataType.IsObjectHandle() &&
  8299. !(!to.IsHandleToConst() && expr.type.dataType.IsHandleToConst()) )
  8300. {
  8301. conversionOK = CompileRefCast(&expr, to, true, node);
  8302. MergeExprBytecode(ctx, &expr);
  8303. ctx->type = expr.type;
  8304. }
  8305. }
  8306. if( conversionOK )
  8307. return 0;
  8308. // Conversion not available
  8309. ctx->type.SetDummy();
  8310. asCString strTo, strFrom;
  8311. strTo = to.Format(outFunc->nameSpace);
  8312. strFrom = expr.type.dataType.Format(outFunc->nameSpace);
  8313. asCString msg;
  8314. msg.Format(TXT_NO_CONVERSION_s_TO_s, strFrom.AddressOf(), strTo.AddressOf());
  8315. Error(msg, node);
  8316. return -1;
  8317. }
  8318. void asCCompiler::AfterFunctionCall(int funcID, asCArray<asCExprContext*> &args, asCExprContext *ctx, bool deferAll)
  8319. {
  8320. // deferAll is set to true if for example the function returns a reference, since in
  8321. // this case the function might be returning a reference to one of the arguments.
  8322. asCScriptFunction *descr = builder->GetFunctionDescription(funcID);
  8323. // Parameters that are sent by reference should be assigned
  8324. // to the evaluated expression if it is an lvalue
  8325. // Evaluate the arguments from last to first
  8326. int n = (int)descr->parameterTypes.GetLength() - 1;
  8327. for( ; n >= 0; n-- )
  8328. {
  8329. // All &out arguments must be deferred, except if the argument is clean, in which case the actual reference was passed in to the function
  8330. // If deferAll is set all objects passed by reference or handle must be deferred
  8331. if( (descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] & asTM_OUTREF) && !args[n]->isCleanArg) ||
  8332. (descr->parameterTypes[n].IsObject() && deferAll && (descr->parameterTypes[n].IsReference() || descr->parameterTypes[n].IsObjectHandle())) )
  8333. {
  8334. asASSERT( !(descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] == asTM_OUTREF) && !args[n]->isCleanArg) || args[n]->origExpr );
  8335. // For &inout, only store the argument if it is for a temporary variable
  8336. if( engine->ep.allowUnsafeReferences ||
  8337. descr->inOutFlags[n] != asTM_INOUTREF || args[n]->type.isTemporary )
  8338. {
  8339. // Store the argument for later processing
  8340. asSDeferredParam outParam;
  8341. outParam.argNode = args[n]->exprNode;
  8342. outParam.argType = args[n]->type;
  8343. outParam.argInOutFlags = descr->inOutFlags[n];
  8344. outParam.origExpr = args[n]->origExpr;
  8345. ctx->deferredParams.PushLast(outParam);
  8346. }
  8347. }
  8348. else
  8349. {
  8350. // Release the temporary variable now
  8351. ReleaseTemporaryVariable(args[n]->type, &ctx->bc);
  8352. }
  8353. // Move the argument's deferred expressions over to the final expression
  8354. for( asUINT m = 0; m < args[n]->deferredParams.GetLength(); m++ )
  8355. {
  8356. ctx->deferredParams.PushLast(args[n]->deferredParams[m]);
  8357. args[n]->deferredParams[m].origExpr = 0;
  8358. }
  8359. args[n]->deferredParams.SetLength(0);
  8360. }
  8361. }
  8362. void asCCompiler::ProcessDeferredParams(asCExprContext *ctx)
  8363. {
  8364. if( isProcessingDeferredParams ) return;
  8365. isProcessingDeferredParams = true;
  8366. for( asUINT n = 0; n < ctx->deferredParams.GetLength(); n++ )
  8367. {
  8368. asSDeferredParam outParam = ctx->deferredParams[n];
  8369. if( outParam.argInOutFlags < asTM_OUTREF ) // &in, or not reference
  8370. {
  8371. // Just release the variable
  8372. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  8373. }
  8374. else if( outParam.argInOutFlags == asTM_OUTREF )
  8375. {
  8376. asCExprContext *expr = outParam.origExpr;
  8377. outParam.origExpr = 0;
  8378. if( outParam.argType.dataType.IsObjectHandle() )
  8379. {
  8380. // Implicitly convert the value to a handle
  8381. if( expr->type.dataType.IsObjectHandle() )
  8382. expr->type.isExplicitHandle = true;
  8383. }
  8384. // Verify that the expression result in a lvalue, or a property accessor
  8385. if( IsLValue(expr->type) || expr->property_get || expr->property_set )
  8386. {
  8387. asCExprContext rctx(engine);
  8388. rctx.type = outParam.argType;
  8389. if( rctx.type.dataType.IsPrimitive() )
  8390. rctx.type.dataType.MakeReference(false);
  8391. else
  8392. {
  8393. rctx.bc.InstrSHORT(asBC_PSF, outParam.argType.stackOffset);
  8394. rctx.type.dataType.MakeReference(IsVariableOnHeap(outParam.argType.stackOffset));
  8395. if( expr->type.isExplicitHandle )
  8396. rctx.type.isExplicitHandle = true;
  8397. }
  8398. asCExprContext o(engine);
  8399. DoAssignment(&o, expr, &rctx, outParam.argNode, outParam.argNode, ttAssignment, outParam.argNode);
  8400. if( !o.type.dataType.IsPrimitive() ) o.bc.Instr(asBC_PopPtr);
  8401. // The assignment may itself have resulted in a new temporary variable, e.g. if
  8402. // the opAssign returns a non-reference. We must release this temporary variable
  8403. // since it won't be used
  8404. ReleaseTemporaryVariable(o.type, &o.bc);
  8405. MergeExprBytecode(ctx, &o);
  8406. }
  8407. else
  8408. {
  8409. // We must still evaluate the expression
  8410. MergeExprBytecode(ctx, expr);
  8411. if( !expr->IsVoidExpression() && (!expr->type.isConstant || expr->type.IsNullConstant()) )
  8412. ctx->bc.Instr(asBC_PopPtr);
  8413. // Give an error, except if the argument is void, null or 0 which indicate the argument is explicitly to be ignored
  8414. if( !expr->IsVoidExpression() && !expr->type.IsNullConstant() && !(expr->type.isConstant && expr->type.GetConstantData() == 0) )
  8415. Error(TXT_ARG_NOT_LVALUE, outParam.argNode);
  8416. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  8417. }
  8418. ReleaseTemporaryVariable(expr->type, &ctx->bc);
  8419. // Delete the original expression context
  8420. asDELETE(expr, asCExprContext);
  8421. }
  8422. else // &inout
  8423. {
  8424. if( outParam.argType.isTemporary )
  8425. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  8426. else if( !outParam.argType.isVariable )
  8427. {
  8428. if( outParam.argType.dataType.IsObject() &&
  8429. ((outParam.argType.dataType.GetBehaviour()->addref &&
  8430. outParam.argType.dataType.GetBehaviour()->release) ||
  8431. (outParam.argType.dataType.GetTypeInfo()->flags & asOBJ_NOCOUNT)) )
  8432. {
  8433. // Release the object handle that was taken to guarantee the reference
  8434. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  8435. }
  8436. }
  8437. }
  8438. }
  8439. ctx->deferredParams.SetLength(0);
  8440. isProcessingDeferredParams = false;
  8441. }
  8442. int asCCompiler::CompileConstructCall(asCScriptNode *node, asCExprContext *ctx)
  8443. {
  8444. // The first node is a datatype node
  8445. asCString name;
  8446. asCExprValue tempObj;
  8447. bool onHeap = true;
  8448. asCArray<int> funcs;
  8449. bool error = false;
  8450. // It is possible that the name is really a constructor
  8451. asCDataType dt;
  8452. dt = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  8453. if( dt.IsPrimitive() )
  8454. {
  8455. // This is a cast to a primitive type
  8456. return CompileConversion(node, ctx);
  8457. }
  8458. if( dt.GetTypeInfo() && (dt.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE) )
  8459. {
  8460. // Types declared as implicit handle must not attempt to construct a handle
  8461. dt.MakeHandle(false);
  8462. }
  8463. // Don't accept syntax like object@(expr)
  8464. if( dt.IsObjectHandle() )
  8465. {
  8466. asCString str;
  8467. str.Format(TXT_CANT_CONSTRUCT_s_USE_REF_CAST, dt.Format(outFunc->nameSpace).AddressOf());
  8468. Error(str, node);
  8469. ctx->type.SetDummy();
  8470. return -1;
  8471. }
  8472. // Make sure the desired type can actually be instantiated
  8473. // Delegates are allowed to be created through construct calls,
  8474. // even though they cannot be instantiated as variables
  8475. if( !dt.CanBeInstantiated() && !dt.IsFuncdef() )
  8476. {
  8477. asCString str;
  8478. if( dt.IsAbstractClass() )
  8479. str.Format(TXT_ABSTRACT_CLASS_s_CANNOT_BE_INSTANTIATED, dt.Format(outFunc->nameSpace).AddressOf());
  8480. else if( dt.IsInterface() )
  8481. str.Format(TXT_INTERFACE_s_CANNOT_BE_INSTANTIATED, dt.Format(outFunc->nameSpace).AddressOf());
  8482. else
  8483. // TODO: Improve error message to explain why
  8484. str.Format(TXT_DATA_TYPE_CANT_BE_s, dt.Format(outFunc->nameSpace).AddressOf());
  8485. Error(str, node);
  8486. ctx->type.SetDummy();
  8487. return -1;
  8488. }
  8489. // Do not allow constructing non-shared types in shared functions
  8490. if( outFunc->IsShared() &&
  8491. dt.GetTypeInfo() && !dt.GetTypeInfo()->IsShared() )
  8492. {
  8493. asCString msg;
  8494. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetTypeInfo()->name.AddressOf());
  8495. Error(msg, node);
  8496. return -1;
  8497. }
  8498. // Compile the arguments
  8499. asCArray<asCExprContext *> args;
  8500. asCArray<asSNamedArgument> namedArgs;
  8501. asCArray<asCExprValue> temporaryVariables;
  8502. if( CompileArgumentList(node->lastChild, args, namedArgs) >= 0 )
  8503. {
  8504. // Check for a value cast behaviour
  8505. if( args.GetLength() == 1 && args[0]->type.dataType.GetTypeInfo() )
  8506. {
  8507. asCExprContext conv(engine);
  8508. conv.type = args[0]->type;
  8509. asUINT cost = ImplicitConversion(&conv, dt, node->lastChild, asIC_EXPLICIT_VAL_CAST, false);
  8510. // Don't use this if the cost is 0 because it would mean that nothing
  8511. // is done and the scipt wants a new value to be constructed
  8512. if( conv.type.dataType.IsEqualExceptRef(dt) && cost > 0 )
  8513. {
  8514. // Make sure any property accessor is already evaluated
  8515. ProcessPropertyGetAccessor(args[0], args[0]->exprNode);
  8516. ImplicitConversion(args[0], dt, node->lastChild, asIC_EXPLICIT_VAL_CAST);
  8517. ctx->bc.AddCode(&args[0]->bc);
  8518. ctx->type = args[0]->type;
  8519. asDELETE(args[0], asCExprContext);
  8520. return 0;
  8521. }
  8522. }
  8523. // Check for possible constructor/factory
  8524. name = dt.Format(outFunc->nameSpace);
  8525. asSTypeBehaviour *beh = dt.GetBehaviour();
  8526. if( !(dt.GetTypeInfo()->flags & asOBJ_REF) && !dt.IsFuncdef() )
  8527. {
  8528. funcs = beh->constructors;
  8529. // Value types and script types are allocated through the constructor
  8530. tempObj.dataType = dt;
  8531. tempObj.stackOffset = (short)AllocateVariable(dt, true);
  8532. tempObj.dataType.MakeReference(true);
  8533. tempObj.isTemporary = true;
  8534. tempObj.isVariable = true;
  8535. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  8536. // Push the address of the object on the stack
  8537. if( onHeap )
  8538. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  8539. }
  8540. else if( beh )
  8541. funcs = beh->factories;
  8542. // Special case: Allow calling func(void) with a void expression.
  8543. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  8544. {
  8545. // Evaluate the expression before the function call
  8546. MergeExprBytecode(ctx, args[0]);
  8547. asDELETE(args[0], asCExprContext);
  8548. args.SetLength(0);
  8549. }
  8550. // Special case: If this is an object constructor and there are no arguments use the default constructor.
  8551. // If none has been registered, just allocate the variable and push it on the stack.
  8552. if( args.GetLength() == 0 )
  8553. {
  8554. beh = tempObj.dataType.GetBehaviour();
  8555. if( beh && beh->construct == 0 && !(dt.GetTypeInfo()->flags & asOBJ_REF) )
  8556. {
  8557. // Call the default constructor
  8558. ctx->type = tempObj;
  8559. if( onHeap )
  8560. {
  8561. asASSERT(ctx->bc.GetLastInstr() == asBC_VAR);
  8562. ctx->bc.RemoveLastInstr();
  8563. }
  8564. CallDefaultConstructor(tempObj.dataType, tempObj.stackOffset, IsVariableOnHeap(tempObj.stackOffset), &ctx->bc, node);
  8565. // Push the reference on the stack
  8566. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  8567. return 0;
  8568. }
  8569. }
  8570. // Special case: If this is a construction of a delegate and the expression names an object method
  8571. if( dt.IsFuncdef() && args.GetLength() == 1 && args[0]->methodName != "" )
  8572. {
  8573. // TODO: delegate: It is possible that the argument returns a function pointer already, in which
  8574. // case no object delegate will be created, but instead a delegate for a function pointer
  8575. // In theory a simple cast would be good in this case, but this is a construct call so it
  8576. // is expected that a new object is created.
  8577. dt.MakeHandle(true);
  8578. ctx->type.Set(dt);
  8579. // The delegate must be able to hold on to a reference to the object
  8580. if( !args[0]->type.dataType.SupportHandles() )
  8581. {
  8582. Error(TXT_CANNOT_CREATE_DELEGATE_FOR_NOREF_TYPES, node);
  8583. error = true;
  8584. }
  8585. else
  8586. {
  8587. // Filter the available object methods to find the one that matches the func def
  8588. asCObjectType *type = CastToObjectType(args[0]->type.dataType.GetTypeInfo());
  8589. asCScriptFunction *bestMethod = 0;
  8590. for( asUINT n = 0; n < type->methods.GetLength(); n++ )
  8591. {
  8592. asCScriptFunction *func = engine->scriptFunctions[type->methods[n]];
  8593. if( func->name != args[0]->methodName )
  8594. continue;
  8595. // If the expression is for a const object, then only const methods should be accepted
  8596. if( args[0]->type.dataType.IsReadOnly() && !func->IsReadOnly() )
  8597. continue;
  8598. if( func->IsSignatureExceptNameAndObjectTypeEqual(CastToFuncdefType(dt.GetTypeInfo())->funcdef) )
  8599. {
  8600. bestMethod = func;
  8601. // If the expression is non-const the non-const overloaded method has priority
  8602. if( args[0]->type.dataType.IsReadOnly() == func->IsReadOnly() )
  8603. break;
  8604. }
  8605. }
  8606. if( bestMethod )
  8607. {
  8608. // The object pointer is already on the stack
  8609. MergeExprBytecode(ctx, args[0]);
  8610. // Push the function pointer as an additional argument
  8611. ctx->bc.InstrPTR(asBC_FuncPtr, bestMethod);
  8612. // Call the factory function for the delegate
  8613. asCArray<int> delegateFuncs;
  8614. builder->GetFunctionDescriptions(DELEGATE_FACTORY, delegateFuncs, engine->nameSpaces[0]);
  8615. asASSERT(delegateFuncs.GetLength() == 1 );
  8616. ctx->bc.Call(asBC_CALLSYS , delegateFuncs[0], 2*AS_PTR_SIZE);
  8617. // Store the returned delegate in a temporary variable
  8618. int returnOffset = AllocateVariable(dt, true, false);
  8619. dt.MakeReference(true);
  8620. ctx->type.SetVariable(dt, returnOffset, true);
  8621. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  8622. // Push a reference to the temporary variable on the stack
  8623. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  8624. // Clean up arguments
  8625. ReleaseTemporaryVariable(args[0]->type, &ctx->bc);
  8626. }
  8627. else
  8628. {
  8629. asCString msg;
  8630. msg.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, CastToFuncdefType(dt.GetTypeInfo())->funcdef->GetDeclaration());
  8631. Error(msg.AddressOf(), node);
  8632. error = true;
  8633. }
  8634. }
  8635. // Clean-up arg
  8636. asDELETE(args[0], asCExprContext);
  8637. return error ? -1 : 0;
  8638. }
  8639. MatchFunctions(funcs, args, node, name.AddressOf(), &namedArgs, 0, false);
  8640. if( funcs.GetLength() != 1 )
  8641. {
  8642. // The error was reported by MatchFunctions()
  8643. error = true;
  8644. // Dummy value
  8645. ctx->type.SetDummy();
  8646. }
  8647. else
  8648. {
  8649. // TODO: Clean up: Merge this with MakeFunctionCall
  8650. // Add the default values for arguments not explicitly supplied
  8651. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(dt.GetTypeInfo()), &namedArgs);
  8652. if( r == asSUCCESS )
  8653. {
  8654. asCByteCode objBC(engine);
  8655. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  8656. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  8657. if( !(dt.GetTypeInfo()->flags & asOBJ_REF) )
  8658. {
  8659. // If the object is allocated on the stack, then call the constructor as a normal function
  8660. if( onHeap )
  8661. {
  8662. int offset = 0;
  8663. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  8664. for( asUINT n = 0; n < args.GetLength(); n++ )
  8665. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  8666. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  8667. }
  8668. else
  8669. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  8670. PerformFunctionCall(funcs[0], ctx, onHeap, &args, CastToObjectType(tempObj.dataType.GetTypeInfo()));
  8671. // Add tag that the object has been initialized
  8672. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  8673. // The constructor doesn't return anything,
  8674. // so we have to manually inform the type of
  8675. // the return value
  8676. ctx->type = tempObj;
  8677. if( !onHeap )
  8678. ctx->type.dataType.MakeReference(false);
  8679. // Push the address of the object on the stack again
  8680. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  8681. }
  8682. else
  8683. {
  8684. // Call the factory to create the reference type
  8685. PerformFunctionCall(funcs[0], ctx, false, &args);
  8686. }
  8687. }
  8688. else
  8689. error = true;
  8690. }
  8691. }
  8692. else
  8693. {
  8694. // Failed to compile the argument list, set the result to the dummy type
  8695. ctx->type.SetDummy();
  8696. error = true;
  8697. }
  8698. // Cleanup
  8699. for( asUINT n = 0; n < args.GetLength(); n++ )
  8700. if( args[n] )
  8701. {
  8702. asDELETE(args[n], asCExprContext);
  8703. }
  8704. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  8705. if( namedArgs[n].ctx )
  8706. {
  8707. asDELETE(namedArgs[n].ctx, asCExprContext);
  8708. }
  8709. return error ? -1 : 0;
  8710. }
  8711. int asCCompiler::CompileFunctionCall(asCScriptNode *node, asCExprContext *ctx, asCObjectType *objectType, bool objIsConst, const asCString &scope)
  8712. {
  8713. asCExprValue tempObj;
  8714. asCArray<int> funcs;
  8715. int localVar = -1;
  8716. bool initializeMembers = false;
  8717. asCExprContext funcExpr(engine);
  8718. asCScriptNode *nm = node->lastChild->prev;
  8719. asCString name(&script->code[nm->tokenPos], nm->tokenLength);
  8720. // First check for a local variable as it would take precedence
  8721. // Must not allow function names, nor global variables to be returned in this instance
  8722. // If objectType is set then this is a post op expression and we shouldn't look for local variables
  8723. if( objectType == 0 )
  8724. {
  8725. localVar = CompileVariableAccess(name, scope, &funcExpr, node, true, true, true);
  8726. if( localVar >= 0 &&
  8727. !(funcExpr.type.dataType.IsFuncdef() || funcExpr.type.dataType.IsObject()) &&
  8728. funcExpr.methodName == "" )
  8729. {
  8730. // The variable is not a function or object with opCall
  8731. asCString msg;
  8732. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  8733. Error(msg, node);
  8734. return -1;
  8735. }
  8736. // If the name matches a method name, then reset the indicator that nothing was found
  8737. if( funcExpr.methodName != "" )
  8738. localVar = -1;
  8739. }
  8740. if( localVar < 0 )
  8741. {
  8742. // If this is an expression post op, or if a class method is
  8743. // being compiled, then we should look for matching class methods
  8744. if( objectType || (outFunc && outFunc->objectType && scope != "::") )
  8745. {
  8746. // If we're compiling a constructor and the name of the function is super then
  8747. // the constructor of the base class is being called.
  8748. // super cannot be prefixed with a scope operator
  8749. if( scope == "" && m_isConstructor && name == SUPER_TOKEN )
  8750. {
  8751. // If the class is not derived from anyone else, calling super should give an error
  8752. if( outFunc && outFunc->objectType->derivedFrom )
  8753. funcs = outFunc->objectType->derivedFrom->beh.constructors;
  8754. // Must not allow calling base class' constructor multiple times
  8755. if( continueLabels.GetLength() > 0 )
  8756. {
  8757. // If a continue label is set we are in a loop
  8758. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_LOOPS, node);
  8759. }
  8760. else if( breakLabels.GetLength() > 0 )
  8761. {
  8762. // TODO: inheritance: Should eventually allow constructors in switch statements
  8763. // If a break label is set we are either in a loop or a switch statements
  8764. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_SWITCH, node);
  8765. }
  8766. else if( m_isConstructorCalled )
  8767. {
  8768. Error(TXT_CANNOT_CALL_CONSTRUCTOR_TWICE, node);
  8769. }
  8770. m_isConstructorCalled = true;
  8771. // We need to initialize the class members, but only after all the deferred arguments have been completed
  8772. initializeMembers = true;
  8773. }
  8774. else
  8775. {
  8776. // The scope can be used to specify the base class
  8777. builder->GetObjectMethodDescriptions(name.AddressOf(), objectType ? objectType : outFunc->objectType, funcs, objIsConst, scope, node, script);
  8778. }
  8779. // It is still possible that there is a class member of a function type or a type with opCall methods
  8780. if( funcs.GetLength() == 0 )
  8781. {
  8782. int r = CompileVariableAccess(name, scope, &funcExpr, node, true, true, true, objectType);
  8783. if( r >= 0 &&
  8784. !(funcExpr.type.dataType.IsFuncdef() || funcExpr.type.dataType.IsObject()) &&
  8785. funcExpr.methodName == "" )
  8786. {
  8787. // The variable is not a function
  8788. asCString msg;
  8789. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  8790. Error(msg, node);
  8791. return -1;
  8792. }
  8793. // If the name is an access property, make sure the original value isn't
  8794. // dereferenced when calling the access property as part a dot post operator
  8795. if( objectType && (funcExpr.property_get || funcExpr.property_set) && !ctx->type.dataType.IsReference() )
  8796. funcExpr.property_ref = false;
  8797. }
  8798. // If a class method is being called implicitly, then add the this pointer for the call
  8799. if( funcs.GetLength() && !objectType )
  8800. {
  8801. objectType = outFunc->objectType;
  8802. asCDataType dt = asCDataType::CreateType(objectType, false);
  8803. // The object pointer is located at stack position 0
  8804. ctx->bc.InstrSHORT(asBC_PSF, 0);
  8805. ctx->type.SetVariable(dt, 0, false);
  8806. ctx->type.dataType.MakeReference(true);
  8807. Dereference(ctx, true);
  8808. }
  8809. }
  8810. // If it is not a class method or member function pointer,
  8811. // then look for global functions or global function pointers,
  8812. // unless this is an expression post op, incase only member
  8813. // functions are expected
  8814. if( objectType == 0 && funcs.GetLength() == 0 && (!funcExpr.type.dataType.IsFuncdef() || funcExpr.type.dataType.IsObject()) )
  8815. {
  8816. // The scope is used to define the namespace
  8817. asSNameSpace *ns = DetermineNameSpace(scope);
  8818. if( ns )
  8819. {
  8820. // Search recursively in parent namespaces
  8821. while( ns && funcs.GetLength() == 0 && !funcExpr.type.dataType.IsFuncdef() )
  8822. {
  8823. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  8824. if( funcs.GetLength() == 0 )
  8825. {
  8826. int r = CompileVariableAccess(name, scope, &funcExpr, node, true, true);
  8827. if( r >= 0 &&
  8828. !(funcExpr.type.dataType.IsFuncdef() || funcExpr.type.dataType.IsObject()) &&
  8829. funcExpr.methodName == "" )
  8830. {
  8831. // The variable is not a function
  8832. asCString msg;
  8833. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  8834. Error(msg, node);
  8835. return -1;
  8836. }
  8837. }
  8838. ns = engine->GetParentNameSpace(ns);
  8839. }
  8840. }
  8841. else
  8842. {
  8843. asCString msg;
  8844. msg.Format(TXT_NAMESPACE_s_DOESNT_EXIST, scope.AddressOf());
  8845. Error(msg, node);
  8846. return -1;
  8847. }
  8848. }
  8849. }
  8850. if( funcs.GetLength() == 0 )
  8851. {
  8852. if( funcExpr.type.dataType.IsFuncdef() )
  8853. {
  8854. funcs.PushLast(CastToFuncdefType(funcExpr.type.dataType.GetTypeInfo())->funcdef->id);
  8855. }
  8856. else if( funcExpr.type.dataType.IsObject() )
  8857. {
  8858. // Keep information about temporary variables as deferred expression so it can be properly cleaned up after the call
  8859. if( ctx->type.isTemporary )
  8860. {
  8861. asASSERT( objectType );
  8862. asSDeferredParam deferred;
  8863. deferred.origExpr = 0;
  8864. deferred.argInOutFlags = asTM_INREF;
  8865. deferred.argNode = 0;
  8866. deferred.argType.SetVariable(ctx->type.dataType, ctx->type.stackOffset, true);
  8867. ctx->deferredParams.PushLast(deferred);
  8868. }
  8869. if( funcExpr.property_get == 0 )
  8870. Dereference(ctx, true);
  8871. // Add the bytecode for accessing the object on which opCall will be called
  8872. MergeExprBytecodeAndType(ctx, &funcExpr);
  8873. ProcessPropertyGetAccessor(ctx, node);
  8874. Dereference(ctx, true);
  8875. objectType = CastToObjectType(funcExpr.type.dataType.GetTypeInfo());
  8876. // Get the opCall methods from the object type
  8877. if( funcExpr.type.dataType.IsObjectHandle() )
  8878. objIsConst = funcExpr.type.dataType.IsHandleToConst();
  8879. else
  8880. objIsConst = funcExpr.type.dataType.IsReadOnly();
  8881. builder->GetObjectMethodDescriptions("opCall", CastToObjectType(funcExpr.type.dataType.GetTypeInfo()), funcs, objIsConst);
  8882. }
  8883. }
  8884. // Compile the arguments
  8885. asCArray<asCExprContext *> args;
  8886. asCArray<asSNamedArgument> namedArgs;
  8887. bool isOK = true;
  8888. if( CompileArgumentList(node->lastChild, args, namedArgs) >= 0 )
  8889. {
  8890. // Special case: Allow calling func(void) with an expression that evaluates to no datatype, but isn't exactly 'void'
  8891. if( args.GetLength() == 1 && args[0]->type.IsVoid() && !args[0]->IsVoidExpression() )
  8892. {
  8893. // Evaluate the expression before the function call
  8894. MergeExprBytecode(ctx, args[0]);
  8895. asDELETE(args[0], asCExprContext);
  8896. args.SetLength(0);
  8897. }
  8898. MatchFunctions(funcs, args, node, name.AddressOf(), &namedArgs, objectType, objIsConst, false, true, scope);
  8899. if( funcs.GetLength() != 1 )
  8900. {
  8901. // The error was reported by MatchFunctions()
  8902. // Dummy value
  8903. ctx->type.SetDummy();
  8904. isOK = false;
  8905. }
  8906. else
  8907. {
  8908. // Add the default values for arguments not explicitly supplied
  8909. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], objectType, &namedArgs);
  8910. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  8911. // is it enough to make sure it is in a local variable?
  8912. // For function pointer we must guarantee that the function is safe, i.e.
  8913. // by first storing the function pointer in a local variable (if it isn't already in one)
  8914. if( r == asSUCCESS )
  8915. {
  8916. asCScriptFunction *func = builder->GetFunctionDescription(funcs[0]);
  8917. if( func->funcType == asFUNC_FUNCDEF )
  8918. {
  8919. if( objectType && funcExpr.property_get <= 0 )
  8920. {
  8921. // Dereference the object pointer to access the member
  8922. Dereference(ctx, true);
  8923. }
  8924. if( funcExpr.property_get > 0 )
  8925. {
  8926. ProcessPropertyGetAccessor(&funcExpr, node);
  8927. Dereference(&funcExpr, true);
  8928. }
  8929. else
  8930. {
  8931. Dereference(&funcExpr, true);
  8932. ConvertToVariable(&funcExpr);
  8933. }
  8934. // The actual function should be called as if a global function
  8935. objectType = 0;
  8936. // The function call will be made directly from the local variable so the function pointer shouldn't be on the stack
  8937. funcExpr.bc.Instr(asBC_PopPtr);
  8938. asCExprValue tmp = ctx->type;
  8939. MergeExprBytecodeAndType(ctx, &funcExpr);
  8940. ReleaseTemporaryVariable(tmp, &ctx->bc);
  8941. }
  8942. MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, funcExpr.type.stackOffset);
  8943. }
  8944. else
  8945. isOK = false;
  8946. }
  8947. }
  8948. else
  8949. {
  8950. // Failed to compile the argument list, set the dummy type and continue compilation
  8951. ctx->type.SetDummy();
  8952. isOK = false;
  8953. }
  8954. // Cleanup
  8955. for( asUINT n = 0; n < args.GetLength(); n++ )
  8956. if( args[n] )
  8957. {
  8958. asDELETE(args[n], asCExprContext);
  8959. }
  8960. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  8961. if( namedArgs[n].ctx )
  8962. {
  8963. asDELETE(namedArgs[n].ctx, asCExprContext);
  8964. }
  8965. if( initializeMembers )
  8966. {
  8967. asASSERT( m_isConstructor );
  8968. // Need to initialize members here, as they may use the properties of the base class
  8969. // If there are multiple paths that call super(), then there will also be multiple
  8970. // locations with initializations of the members. It is not possible to consolidate
  8971. // these in one place, as the expressions for the initialization are evaluated where
  8972. // they are compiled, which means that they may access different variables depending
  8973. // on the scope where super() is called.
  8974. // Members that don't have an explicit initialization expression will be initialized
  8975. // beginning of the constructor as they are guaranteed not to use at the any
  8976. // members of the base class.
  8977. CompileMemberInitialization(&ctx->bc, false);
  8978. }
  8979. return isOK ? 0 : -1;
  8980. }
  8981. asSNameSpace *asCCompiler::DetermineNameSpace(const asCString &scope)
  8982. {
  8983. asSNameSpace *ns;
  8984. if( scope == "" )
  8985. {
  8986. // When compiling default argument expression the correct namespace is stored in the outFunc even for objects
  8987. if( outFunc->nameSpace->name != "" || isCompilingDefaultArg )
  8988. ns = outFunc->nameSpace;
  8989. else if( outFunc->objectType && outFunc->objectType->nameSpace->name != "" )
  8990. ns = outFunc->objectType->nameSpace;
  8991. else
  8992. ns = engine->nameSpaces[0];
  8993. }
  8994. else if( scope == "::" )
  8995. ns = engine->nameSpaces[0];
  8996. else
  8997. ns = engine->FindNameSpace(scope.AddressOf());
  8998. return ns;
  8999. }
  9000. int asCCompiler::CompileExpressionPreOp(asCScriptNode *node, asCExprContext *ctx)
  9001. {
  9002. int op = node->tokenType;
  9003. // Don't allow any prefix operators except handle on expressions that take address of class method
  9004. if( ctx->IsClassMethod() && op != ttHandle )
  9005. {
  9006. Error(TXT_INVALID_OP_ON_METHOD, node);
  9007. return -1;
  9008. }
  9009. // Don't allow any operators on void expressions
  9010. if( ctx->IsVoidExpression() )
  9011. {
  9012. Error(TXT_VOID_CANT_BE_OPERAND, node);
  9013. return -1;
  9014. }
  9015. IsVariableInitialized(&ctx->type, node);
  9016. if( op == ttHandle )
  9017. {
  9018. if( ctx->methodName != "" )
  9019. {
  9020. // Don't allow taking the handle of a handle
  9021. if( ctx->type.isExplicitHandle )
  9022. {
  9023. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  9024. return -1;
  9025. }
  9026. }
  9027. else
  9028. {
  9029. // Don't allow taking handle of a handle, i.e. @@
  9030. if( ctx->type.isExplicitHandle )
  9031. {
  9032. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  9033. return -1;
  9034. }
  9035. // @null is allowed even though it is implicit
  9036. if( !ctx->type.IsNullConstant() )
  9037. {
  9038. // Verify that the type allow its handle to be taken
  9039. if( !ctx->type.dataType.SupportHandles() && !ctx->type.dataType.IsObjectHandle() )
  9040. {
  9041. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  9042. return -1;
  9043. }
  9044. // Objects that are not local variables are not references
  9045. // Objects allocated on the stack are also not marked as references
  9046. if( !ctx->type.dataType.IsReference() &&
  9047. !((ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && !ctx->type.isVariable) &&
  9048. !(ctx->type.isVariable && !IsVariableOnHeap(ctx->type.stackOffset)) )
  9049. {
  9050. Error(TXT_NOT_VALID_REFERENCE, node);
  9051. return -1;
  9052. }
  9053. // Convert the expression to a handle
  9054. if( !ctx->type.dataType.IsObjectHandle() && !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  9055. {
  9056. asCDataType to = ctx->type.dataType;
  9057. to.MakeHandle(true);
  9058. to.MakeReference(true);
  9059. to.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  9060. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV, true, false);
  9061. asASSERT( ctx->type.dataType.IsObjectHandle() );
  9062. }
  9063. else if( ctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE )
  9064. {
  9065. // For the ASHANDLE type we'll simply set the expression as a handle
  9066. ctx->type.dataType.MakeHandle(true);
  9067. }
  9068. }
  9069. }
  9070. // Mark the expression as an explicit handle to avoid implicit conversions to non-handle expressions
  9071. ctx->type.isExplicitHandle = true;
  9072. }
  9073. else if( (op == ttMinus || op == ttPlus || op == ttBitNot || op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  9074. {
  9075. // Look for the appropriate method
  9076. // There is no overloadable operator for unary plus
  9077. const char *opName = 0;
  9078. switch( op )
  9079. {
  9080. case ttMinus: opName = "opNeg"; break;
  9081. case ttBitNot: opName = "opCom"; break;
  9082. case ttInc: opName = "opPreInc"; break;
  9083. case ttDec: opName = "opPreDec"; break;
  9084. }
  9085. if( opName )
  9086. {
  9087. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  9088. ProcessPropertyGetAccessor(ctx, node);
  9089. // 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
  9090. // Find the correct method
  9091. bool isConst = ctx->type.dataType.IsObjectConst();
  9092. asCArray<int> funcs;
  9093. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  9094. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  9095. {
  9096. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  9097. if( func->name == opName &&
  9098. func->parameterTypes.GetLength() == 0 &&
  9099. (!isConst || func->isReadOnly) )
  9100. {
  9101. funcs.PushLast(func->id);
  9102. }
  9103. }
  9104. // Did we find the method?
  9105. if( funcs.GetLength() == 1 )
  9106. {
  9107. asCArray<asCExprContext *> args;
  9108. MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  9109. return 0;
  9110. }
  9111. else if( funcs.GetLength() == 0 )
  9112. {
  9113. asCString str;
  9114. str = asCString(opName) + "()";
  9115. if( isConst )
  9116. str += " const";
  9117. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  9118. Error(str, node);
  9119. ctx->type.SetDummy();
  9120. return -1;
  9121. }
  9122. else if( funcs.GetLength() > 1 )
  9123. {
  9124. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  9125. PrintMatchingFuncs(funcs, node);
  9126. ctx->type.SetDummy();
  9127. return -1;
  9128. }
  9129. }
  9130. else if( op == ttPlus )
  9131. {
  9132. Error(TXT_ILLEGAL_OPERATION, node);
  9133. ctx->type.SetDummy();
  9134. return -1;
  9135. }
  9136. }
  9137. else if( op == ttPlus || op == ttMinus )
  9138. {
  9139. // This is only for primitives. Objects are treated in the above block
  9140. // Make sure the type is a math type
  9141. if( !(ctx->type.dataType.IsIntegerType() ||
  9142. ctx->type.dataType.IsUnsignedType() ||
  9143. ctx->type.dataType.IsFloatType() ||
  9144. ctx->type.dataType.IsDoubleType() ) )
  9145. {
  9146. Error(TXT_ILLEGAL_OPERATION, node);
  9147. return -1;
  9148. }
  9149. ProcessPropertyGetAccessor(ctx, node);
  9150. asCDataType to = ctx->type.dataType;
  9151. if( ctx->type.dataType.IsUnsignedType() )
  9152. {
  9153. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  9154. to = asCDataType::CreatePrimitive(ttInt8, false);
  9155. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  9156. to = asCDataType::CreatePrimitive(ttInt16, false);
  9157. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  9158. to = asCDataType::CreatePrimitive(ttInt, false);
  9159. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  9160. to = asCDataType::CreatePrimitive(ttInt64, false);
  9161. else
  9162. {
  9163. Error(TXT_INVALID_TYPE, node);
  9164. return -1;
  9165. }
  9166. }
  9167. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  9168. // Use an explicit conversion in case of constants to avoid unnecessary warning about change of sign
  9169. ImplicitConversion(ctx, to, node, ctx->type.isConstant ? asIC_EXPLICIT_VAL_CAST : asIC_IMPLICIT_CONV);
  9170. if( !ctx->type.isConstant )
  9171. {
  9172. ConvertToTempVariable(ctx);
  9173. asASSERT(!ctx->type.isLValue);
  9174. if( op == ttMinus )
  9175. {
  9176. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9177. ctx->bc.InstrSHORT(asBC_NEGi, ctx->type.stackOffset);
  9178. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9179. ctx->bc.InstrSHORT(asBC_NEGi64, ctx->type.stackOffset);
  9180. else if( ctx->type.dataType.IsFloatType() )
  9181. ctx->bc.InstrSHORT(asBC_NEGf, ctx->type.stackOffset);
  9182. else if( ctx->type.dataType.IsDoubleType() )
  9183. ctx->bc.InstrSHORT(asBC_NEGd, ctx->type.stackOffset);
  9184. else
  9185. {
  9186. Error(TXT_ILLEGAL_OPERATION, node);
  9187. return -1;
  9188. }
  9189. return 0;
  9190. }
  9191. }
  9192. else
  9193. {
  9194. if( op == ttMinus )
  9195. {
  9196. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9197. ctx->type.SetConstantDW(-(int)ctx->type.GetConstantDW());
  9198. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9199. ctx->type.SetConstantQW(-(asINT64)ctx->type.GetConstantQW());
  9200. else if( ctx->type.dataType.IsFloatType() )
  9201. ctx->type.SetConstantF(-ctx->type.GetConstantF());
  9202. else if( ctx->type.dataType.IsDoubleType() )
  9203. ctx->type.SetConstantD(-ctx->type.GetConstantD());
  9204. else
  9205. {
  9206. Error(TXT_ILLEGAL_OPERATION, node);
  9207. return -1;
  9208. }
  9209. return 0;
  9210. }
  9211. }
  9212. }
  9213. else if( op == ttNot )
  9214. {
  9215. // Allow value types to be converted to bool using 'bool opImplConv()'
  9216. if( ctx->type.dataType.GetTypeInfo() && (ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  9217. ImplicitConversion(ctx, asCDataType::CreatePrimitive(ttBool, false), node, asIC_IMPLICIT_CONV);
  9218. if( ctx->type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  9219. {
  9220. if( ctx->type.isConstant )
  9221. {
  9222. #if AS_SIZEOF_BOOL == 1
  9223. ctx->type.SetConstantB(ctx->type.GetConstantB() == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9224. #else
  9225. ctx->type.SetConstantDW(ctx->type.GetConstantDW() == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  9226. #endif
  9227. return 0;
  9228. }
  9229. ProcessPropertyGetAccessor(ctx, node);
  9230. ConvertToTempVariable(ctx);
  9231. asASSERT(!ctx->type.isLValue);
  9232. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  9233. }
  9234. else
  9235. {
  9236. Error(TXT_ILLEGAL_OPERATION, node);
  9237. return -1;
  9238. }
  9239. }
  9240. else if( op == ttBitNot )
  9241. {
  9242. ProcessPropertyGetAccessor(ctx, node);
  9243. asCDataType to = ctx->type.dataType;
  9244. if( ctx->type.dataType.IsIntegerType() )
  9245. {
  9246. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  9247. to = asCDataType::CreatePrimitive(ttUInt8, false);
  9248. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  9249. to = asCDataType::CreatePrimitive(ttUInt16, false);
  9250. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  9251. to = asCDataType::CreatePrimitive(ttUInt, false);
  9252. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  9253. to = asCDataType::CreatePrimitive(ttUInt64, false);
  9254. else
  9255. {
  9256. Error(TXT_INVALID_TYPE, node);
  9257. return -1;
  9258. }
  9259. }
  9260. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  9261. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  9262. if( ctx->type.dataType.IsUnsignedType() )
  9263. {
  9264. if( ctx->type.isConstant )
  9265. {
  9266. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  9267. ctx->type.SetConstantB(~ctx->type.GetConstantB());
  9268. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 2)
  9269. ctx->type.SetConstantW(~ctx->type.GetConstantW());
  9270. else if (ctx->type.dataType.GetSizeInMemoryBytes() == 4)
  9271. ctx->type.SetConstantDW(~ctx->type.GetConstantDW());
  9272. else
  9273. ctx->type.SetConstantQW(~ctx->type.GetConstantQW());
  9274. return 0;
  9275. }
  9276. ConvertToTempVariable(ctx);
  9277. asASSERT(!ctx->type.isLValue);
  9278. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9279. ctx->bc.InstrSHORT(asBC_BNOT, ctx->type.stackOffset);
  9280. else
  9281. ctx->bc.InstrSHORT(asBC_BNOT64, ctx->type.stackOffset);
  9282. }
  9283. else
  9284. {
  9285. Error(TXT_ILLEGAL_OPERATION, node);
  9286. return -1;
  9287. }
  9288. }
  9289. else if( op == ttInc || op == ttDec )
  9290. {
  9291. // Need a reference to the primitive that will be updated
  9292. // The result of this expression is the same reference as before
  9293. // Make sure the reference isn't a temporary variable
  9294. if( ctx->type.isTemporary )
  9295. {
  9296. Error(TXT_REF_IS_TEMP, node);
  9297. return -1;
  9298. }
  9299. if( ctx->type.dataType.IsReadOnly() )
  9300. {
  9301. Error(TXT_REF_IS_READ_ONLY, node);
  9302. return -1;
  9303. }
  9304. if( ctx->property_get || ctx->property_set )
  9305. {
  9306. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  9307. return -1;
  9308. }
  9309. if( !ctx->type.isLValue )
  9310. {
  9311. Error(TXT_NOT_LVALUE, node);
  9312. return -1;
  9313. }
  9314. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  9315. ConvertToReference(ctx);
  9316. else if( !ctx->type.dataType.IsReference() )
  9317. {
  9318. Error(TXT_NOT_VALID_REFERENCE, node);
  9319. return -1;
  9320. }
  9321. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  9322. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  9323. {
  9324. if( op == ttInc )
  9325. ctx->bc.Instr(asBC_INCi64);
  9326. else
  9327. ctx->bc.Instr(asBC_DECi64);
  9328. }
  9329. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt, false)) ||
  9330. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt, false)) )
  9331. {
  9332. if( op == ttInc )
  9333. ctx->bc.Instr(asBC_INCi);
  9334. else
  9335. ctx->bc.Instr(asBC_DECi);
  9336. }
  9337. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  9338. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  9339. {
  9340. if( op == ttInc )
  9341. ctx->bc.Instr(asBC_INCi16);
  9342. else
  9343. ctx->bc.Instr(asBC_DECi16);
  9344. }
  9345. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  9346. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  9347. {
  9348. if( op == ttInc )
  9349. ctx->bc.Instr(asBC_INCi8);
  9350. else
  9351. ctx->bc.Instr(asBC_DECi8);
  9352. }
  9353. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttFloat, false)) )
  9354. {
  9355. if( op == ttInc )
  9356. ctx->bc.Instr(asBC_INCf);
  9357. else
  9358. ctx->bc.Instr(asBC_DECf);
  9359. }
  9360. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttDouble, false)) )
  9361. {
  9362. if( op == ttInc )
  9363. ctx->bc.Instr(asBC_INCd);
  9364. else
  9365. ctx->bc.Instr(asBC_DECd);
  9366. }
  9367. else
  9368. {
  9369. Error(TXT_ILLEGAL_OPERATION, node);
  9370. return -1;
  9371. }
  9372. }
  9373. else
  9374. {
  9375. // Unknown operator
  9376. asASSERT(false);
  9377. return -1;
  9378. }
  9379. return 0;
  9380. }
  9381. void asCCompiler::ConvertToReference(asCExprContext *ctx)
  9382. {
  9383. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  9384. {
  9385. ctx->bc.InstrSHORT(asBC_LDV, ctx->type.stackOffset);
  9386. ctx->type.dataType.MakeReference(true);
  9387. ctx->type.SetVariable(ctx->type.dataType, ctx->type.stackOffset, ctx->type.isTemporary);
  9388. }
  9389. }
  9390. int asCCompiler::FindPropertyAccessor(const asCString &name, asCExprContext *ctx, asCScriptNode *node, asSNameSpace *ns, bool isThisAccess)
  9391. {
  9392. return FindPropertyAccessor(name, ctx, 0, node, ns, isThisAccess);
  9393. }
  9394. int asCCompiler::FindPropertyAccessor(const asCString &name, asCExprContext *ctx, asCExprContext *arg, asCScriptNode *node, asSNameSpace *ns, bool isThisAccess)
  9395. {
  9396. if( engine->ep.propertyAccessorMode == 0 )
  9397. {
  9398. // Property accessors have been disabled by the application
  9399. return 0;
  9400. }
  9401. int getId = 0, setId = 0;
  9402. asCString getName = "get_" + name;
  9403. asCString setName = "set_" + name;
  9404. asCArray<int> multipleGetFuncs, multipleSetFuncs;
  9405. if( ctx->type.dataType.IsObject() )
  9406. {
  9407. asASSERT( ns == 0 );
  9408. // Don't look for property accessors in script classes if the script
  9409. // property accessors have been disabled by the application
  9410. if( !(ctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCRIPT_OBJECT) ||
  9411. engine->ep.propertyAccessorMode == 2 )
  9412. {
  9413. // Check if the object has any methods with the corresponding accessor name(s)
  9414. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  9415. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  9416. {
  9417. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  9418. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  9419. if( f->name == getName && (int)f->parameterTypes.GetLength() == (arg?1:0) )
  9420. {
  9421. if( getId == 0 )
  9422. getId = ot->methods[n];
  9423. else
  9424. {
  9425. if( multipleGetFuncs.GetLength() == 0 )
  9426. multipleGetFuncs.PushLast(getId);
  9427. multipleGetFuncs.PushLast(ot->methods[n]);
  9428. }
  9429. }
  9430. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  9431. if( f->name == setName && (int)f->parameterTypes.GetLength() == (arg?2:1) )
  9432. {
  9433. if( setId == 0 )
  9434. setId = ot->methods[n];
  9435. else
  9436. {
  9437. if( multipleSetFuncs.GetLength() == 0 )
  9438. multipleSetFuncs.PushLast(setId);
  9439. multipleSetFuncs.PushLast(ot->methods[n]);
  9440. }
  9441. }
  9442. }
  9443. }
  9444. }
  9445. else
  9446. {
  9447. asASSERT( ns != 0 );
  9448. // Look for appropriate global functions.
  9449. asCArray<int> funcs;
  9450. asUINT n;
  9451. builder->GetFunctionDescriptions(getName.AddressOf(), funcs, ns);
  9452. for( n = 0; n < funcs.GetLength(); n++ )
  9453. {
  9454. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  9455. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  9456. if( (int)f->parameterTypes.GetLength() == (arg?1:0) )
  9457. {
  9458. if( getId == 0 )
  9459. getId = funcs[n];
  9460. else
  9461. {
  9462. if( multipleGetFuncs.GetLength() == 0 )
  9463. multipleGetFuncs.PushLast(getId);
  9464. multipleGetFuncs.PushLast(funcs[n]);
  9465. }
  9466. }
  9467. }
  9468. funcs.SetLength(0);
  9469. builder->GetFunctionDescriptions(setName.AddressOf(), funcs, ns);
  9470. for( n = 0; n < funcs.GetLength(); n++ )
  9471. {
  9472. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  9473. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  9474. if( (int)f->parameterTypes.GetLength() == (arg?2:1) )
  9475. {
  9476. if( setId == 0 )
  9477. setId = funcs[n];
  9478. else
  9479. {
  9480. if( multipleSetFuncs.GetLength() == 0 )
  9481. multipleSetFuncs.PushLast(getId);
  9482. multipleSetFuncs.PushLast(funcs[n]);
  9483. }
  9484. }
  9485. }
  9486. }
  9487. bool isConst = ctx->type.dataType.IsObjectConst();
  9488. // Check for multiple matches
  9489. if( multipleGetFuncs.GetLength() > 0 )
  9490. {
  9491. // Filter the list by constness
  9492. FilterConst(multipleGetFuncs, !isConst);
  9493. if( multipleGetFuncs.GetLength() > 1 )
  9494. {
  9495. asCString str;
  9496. str.Format(TXT_MULTIPLE_PROP_GET_ACCESSOR_FOR_s, name.AddressOf());
  9497. Error(str, node);
  9498. PrintMatchingFuncs(multipleGetFuncs, node);
  9499. return -1;
  9500. }
  9501. else
  9502. {
  9503. // The id may have changed
  9504. getId = multipleGetFuncs[0];
  9505. }
  9506. }
  9507. if( multipleSetFuncs.GetLength() > 0 )
  9508. {
  9509. // Filter the list by constness
  9510. FilterConst(multipleSetFuncs, !isConst);
  9511. if( multipleSetFuncs.GetLength() > 1 )
  9512. {
  9513. asCString str;
  9514. str.Format(TXT_MULTIPLE_PROP_SET_ACCESSOR_FOR_s, name.AddressOf());
  9515. Error(str, node);
  9516. PrintMatchingFuncs(multipleSetFuncs, node);
  9517. return -1;
  9518. }
  9519. else
  9520. {
  9521. // The id may have changed
  9522. setId = multipleSetFuncs[0];
  9523. }
  9524. }
  9525. // Check for type compatibility between get and set accessor
  9526. if( getId && setId )
  9527. {
  9528. asCScriptFunction *getFunc = builder->GetFunctionDescription(getId);
  9529. asCScriptFunction *setFunc = builder->GetFunctionDescription(setId);
  9530. // It is permitted for a getter to return a handle and the setter to take a reference
  9531. int idx = (arg?1:0);
  9532. if( !getFunc->returnType.IsEqualExceptRefAndConst(setFunc->parameterTypes[idx]) &&
  9533. !((getFunc->returnType.IsObjectHandle() && !setFunc->parameterTypes[idx].IsObjectHandle()) &&
  9534. (getFunc->returnType.GetTypeInfo() == setFunc->parameterTypes[idx].GetTypeInfo())) )
  9535. {
  9536. asCString str;
  9537. str.Format(TXT_GET_SET_ACCESSOR_TYPE_MISMATCH_FOR_s, name.AddressOf());
  9538. Error(str, node);
  9539. asCArray<int> funcs;
  9540. funcs.PushLast(getId);
  9541. funcs.PushLast(setId);
  9542. PrintMatchingFuncs(funcs, node);
  9543. return -1;
  9544. }
  9545. }
  9546. // Check if we are within one of the accessors
  9547. int realGetId = getId;
  9548. int realSetId = setId;
  9549. if( outFunc->objectType && isThisAccess )
  9550. {
  9551. // The property accessors would be virtual functions, so we need to find the real implementation
  9552. asCScriptFunction *getFunc = getId ? builder->GetFunctionDescription(getId) : 0;
  9553. if( getFunc &&
  9554. getFunc->funcType == asFUNC_VIRTUAL &&
  9555. outFunc->objectType->DerivesFrom(getFunc->objectType) )
  9556. realGetId = outFunc->objectType->virtualFunctionTable[getFunc->vfTableIdx]->id;
  9557. asCScriptFunction *setFunc = setId ? builder->GetFunctionDescription(setId) : 0;
  9558. if( setFunc &&
  9559. setFunc->funcType == asFUNC_VIRTUAL &&
  9560. outFunc->objectType->DerivesFrom(setFunc->objectType) )
  9561. realSetId = outFunc->objectType->virtualFunctionTable[setFunc->vfTableIdx]->id;
  9562. }
  9563. // Avoid recursive call, by not treating this as a property accessor call.
  9564. // This will also allow having the real property with the same name as the accessors.
  9565. if( (isThisAccess || outFunc->objectType == 0) &&
  9566. ((realGetId && realGetId == outFunc->id) ||
  9567. (realSetId && realSetId == outFunc->id)) )
  9568. {
  9569. getId = 0;
  9570. setId = 0;
  9571. }
  9572. // Check if the application has disabled script written property accessors
  9573. if( engine->ep.propertyAccessorMode == 1 )
  9574. {
  9575. if( getId && builder->GetFunctionDescription(getId)->funcType != asFUNC_SYSTEM )
  9576. getId = 0;
  9577. if( setId && builder->GetFunctionDescription(setId)->funcType != asFUNC_SYSTEM )
  9578. setId = 0;
  9579. }
  9580. if( getId || setId )
  9581. {
  9582. // Property accessors were found, but we don't know which is to be used yet, so
  9583. // we just prepare the bytecode for the method call, and then store the function ids
  9584. // so that the right one can be used when we get there.
  9585. ctx->property_get = getId;
  9586. ctx->property_set = setId;
  9587. bool isHandleSafe = ctx->type.isHandleSafe;
  9588. if( ctx->type.dataType.IsObject() )
  9589. {
  9590. // If the object is read-only then we need to remember that
  9591. if( (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) ||
  9592. (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) )
  9593. ctx->property_const = true;
  9594. else
  9595. ctx->property_const = false;
  9596. // If the object is a handle then we need to remember that
  9597. ctx->property_handle = ctx->type.dataType.IsObjectHandle();
  9598. ctx->property_ref = ctx->type.dataType.IsReference();
  9599. }
  9600. // The setter's parameter type is used as the property type,
  9601. // unless only the getter is available
  9602. asCDataType dt;
  9603. if( setId )
  9604. dt = builder->GetFunctionDescription(setId)->parameterTypes[(arg?1:0)];
  9605. else
  9606. dt = builder->GetFunctionDescription(getId)->returnType;
  9607. // Just change the type, the context must still maintain information
  9608. // about previous variable offset and the indicator of temporary variable.
  9609. int offset = ctx->type.stackOffset;
  9610. bool isTemp = ctx->type.isTemporary;
  9611. ctx->type.Set(dt);
  9612. ctx->type.stackOffset = (short)offset;
  9613. ctx->type.isTemporary = isTemp;
  9614. ctx->exprNode = node;
  9615. // Remember if the object is safe, so the invocation of the property
  9616. // accessor doesn't needlessly make a safe copy of the handle
  9617. ctx->type.isHandleSafe = isHandleSafe;
  9618. // Store the argument for later use
  9619. if( arg )
  9620. {
  9621. ctx->property_arg = asNEW(asCExprContext)(engine);
  9622. if( ctx->property_arg == 0 )
  9623. {
  9624. // Out of memory
  9625. return -1;
  9626. }
  9627. MergeExprBytecodeAndType(ctx->property_arg, arg);
  9628. }
  9629. return 1;
  9630. }
  9631. // No accessor was found
  9632. return 0;
  9633. }
  9634. int asCCompiler::ProcessPropertySetAccessor(asCExprContext *ctx, asCExprContext *arg, asCScriptNode *node)
  9635. {
  9636. // TODO: A lot of this code is similar to ProcessPropertyGetAccessor. Can we unify them?
  9637. if( !ctx->property_set )
  9638. {
  9639. Error(TXT_PROPERTY_HAS_NO_SET_ACCESSOR, node);
  9640. return -1;
  9641. }
  9642. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_set);
  9643. // Make sure the arg match the property
  9644. asCArray<int> funcs;
  9645. funcs.PushLast(ctx->property_set);
  9646. asCArray<asCExprContext *> args;
  9647. if( ctx->property_arg )
  9648. args.PushLast(ctx->property_arg);
  9649. args.PushLast(arg);
  9650. MatchFunctions(funcs, args, node, func->GetName(), 0, func->objectType, ctx->property_const);
  9651. if( funcs.GetLength() == 0 )
  9652. {
  9653. // MatchFunctions already reported the error
  9654. if( ctx->property_arg )
  9655. {
  9656. asDELETE(ctx->property_arg, asCExprContext);
  9657. ctx->property_arg = 0;
  9658. }
  9659. return -1;
  9660. }
  9661. if( func->objectType )
  9662. {
  9663. // Setup the context with the original type so the method call gets built correctly
  9664. ctx->type.dataType = asCDataType::CreateType(func->objectType, ctx->property_const);
  9665. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  9666. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  9667. // Don't allow the call if the object is read-only and the property accessor is not const
  9668. if( ctx->property_const && !func->isReadOnly )
  9669. {
  9670. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  9671. asCArray<int> funcCandidates;
  9672. funcCandidates.PushLast(ctx->property_set);
  9673. PrintMatchingFuncs(funcCandidates, node);
  9674. }
  9675. }
  9676. // Call the accessor
  9677. MakeFunctionCall(ctx, ctx->property_set, func->objectType, args, node);
  9678. ctx->property_get = 0;
  9679. ctx->property_set = 0;
  9680. if( ctx->property_arg )
  9681. {
  9682. asDELETE(ctx->property_arg, asCExprContext);
  9683. ctx->property_arg = 0;
  9684. }
  9685. return 0;
  9686. }
  9687. int asCCompiler::ProcessPropertyGetSetAccessor(asCExprContext *ctx, asCExprContext *lctx, asCExprContext *rctx, eTokenType op, asCScriptNode *errNode)
  9688. {
  9689. // TODO: Perhaps it might be interesting to allow the definition of compound setters for better
  9690. // performance, e.g. set_add_prop, set_mul_prop, etc. With these it would also be possible
  9691. // to support value types, since it would be a single call
  9692. // Compound assignment for indexed property accessors is not supported yet
  9693. if( lctx->property_arg != 0 )
  9694. {
  9695. // Process the property to free the memory
  9696. ProcessPropertySetAccessor(lctx, rctx, errNode);
  9697. Error(TXT_COMPOUND_ASGN_WITH_IDX_PROP, errNode);
  9698. return -1;
  9699. }
  9700. // Compound assignments require both get and set accessors
  9701. if( lctx->property_set == 0 || lctx->property_get == 0 )
  9702. {
  9703. // Process the property to free the memory
  9704. ProcessPropertySetAccessor(lctx, rctx, errNode);
  9705. Error(TXT_COMPOUND_ASGN_REQUIRE_GET_SET, errNode);
  9706. return -1;
  9707. }
  9708. // Property accessors on value types (or scoped references types) are not supported since
  9709. // it is not possible to guarantee that the object will stay alive between the two calls
  9710. asCScriptFunction *func = engine->scriptFunctions[lctx->property_set];
  9711. if( func->objectType && (func->objectType->flags & (asOBJ_VALUE | asOBJ_SCOPED)) )
  9712. {
  9713. // Process the property to free the memory
  9714. ProcessPropertySetAccessor(lctx, rctx, errNode);
  9715. Error(TXT_COMPOUND_ASGN_ON_VALUE_TYPE, errNode);
  9716. return -1;
  9717. }
  9718. // Translate the compound assignment to the corresponding dual operator
  9719. switch( op )
  9720. {
  9721. case ttAddAssign: op = ttPlus; break;
  9722. case ttSubAssign: op = ttMinus; break;
  9723. case ttMulAssign: op = ttStar; break;
  9724. case ttDivAssign: op = ttSlash; break;
  9725. case ttModAssign: op = ttPercent; break;
  9726. case ttPowAssign: op = ttStarStar; break;
  9727. case ttAndAssign: op = ttAmp; break;
  9728. case ttOrAssign: op = ttBitOr; break;
  9729. case ttXorAssign: op = ttBitXor; break;
  9730. case ttShiftLeftAssign: op = ttBitShiftLeft; break;
  9731. case ttShiftRightAAssign: op = ttBitShiftRightArith; break;
  9732. case ttShiftRightLAssign: op = ttBitShiftRight; break;
  9733. default: op = ttUnrecognizedToken; break;
  9734. }
  9735. if( op == ttUnrecognizedToken )
  9736. {
  9737. // Shouldn't happen
  9738. asASSERT(false);
  9739. // Process the property to free the memory
  9740. ProcessPropertySetAccessor(lctx, rctx, errNode);
  9741. return -1;
  9742. }
  9743. asCExprContext before(engine);
  9744. if( func->objectType && (func->objectType->flags & (asOBJ_REF|asOBJ_SCOPED)) == asOBJ_REF )
  9745. {
  9746. // Keep a reference to the object in a local variable
  9747. before.bc.AddCode(&lctx->bc);
  9748. asUINT len = reservedVariables.GetLength();
  9749. rctx->bc.GetVarsUsed(reservedVariables);
  9750. before.bc.GetVarsUsed(reservedVariables);
  9751. asCDataType dt = asCDataType::CreateObjectHandle(func->objectType, false);
  9752. int offset = AllocateVariable(dt, true);
  9753. reservedVariables.SetLength(len);
  9754. before.type.SetVariable(dt, offset, true);
  9755. if( lctx->property_ref )
  9756. before.bc.Instr(asBC_RDSPtr);
  9757. before.bc.InstrSHORT(asBC_PSF, (short)offset);
  9758. before.bc.InstrPTR(asBC_REFCPY, func->objectType);
  9759. before.bc.Instr(asBC_PopPtr);
  9760. if( lctx->type.isTemporary )
  9761. {
  9762. // Add the release of the temporary variable as a deferred expression
  9763. asSDeferredParam deferred;
  9764. deferred.origExpr = 0;
  9765. deferred.argInOutFlags = asTM_INREF;
  9766. deferred.argNode = 0;
  9767. deferred.argType.SetVariable(ctx->type.dataType, lctx->type.stackOffset, true);
  9768. before.deferredParams.PushLast(deferred);
  9769. }
  9770. // Update the left expression to use the local variable
  9771. lctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  9772. lctx->type.stackOffset = (short)offset;
  9773. lctx->property_ref = true;
  9774. // Don't release the temporary variable too early
  9775. lctx->type.isTemporary = false;
  9776. ctx->bc.AddCode(&before.bc);
  9777. }
  9778. // Keep the original information on the property
  9779. asCExprContext llctx(engine);
  9780. llctx.type = lctx->type;
  9781. llctx.property_arg = lctx->property_arg;
  9782. llctx.property_const = lctx->property_const;
  9783. llctx.property_get = lctx->property_get;
  9784. llctx.property_handle = lctx->property_handle;
  9785. llctx.property_ref = lctx->property_ref;
  9786. llctx.property_set = lctx->property_set;
  9787. // Compile the dual operator using the get accessor
  9788. CompileOperator(errNode, lctx, rctx, ctx, op, false);
  9789. // If we made a local variable to hold the reference it must be reused
  9790. if( before.type.stackOffset )
  9791. llctx.bc.InstrSHORT(asBC_PSF, before.type.stackOffset);
  9792. // Compile the assignment using the set accessor
  9793. ProcessPropertySetAccessor(&llctx, ctx, errNode);
  9794. MergeExprBytecodeAndType(ctx, &llctx);
  9795. if( before.type.stackOffset )
  9796. ReleaseTemporaryVariable(before.type.stackOffset, &ctx->bc);
  9797. asASSERT( ctx->deferredParams.GetLength() == 0 );
  9798. ctx->deferredParams = before.deferredParams;
  9799. ProcessDeferredParams(ctx);
  9800. return 0;
  9801. }
  9802. void asCCompiler::ProcessPropertyGetAccessor(asCExprContext *ctx, asCScriptNode *node)
  9803. {
  9804. // If no property accessor has been prepared then don't do anything
  9805. if( !ctx->property_get && !ctx->property_set )
  9806. return;
  9807. if( !ctx->property_get )
  9808. {
  9809. // Raise error on missing accessor
  9810. Error(TXT_PROPERTY_HAS_NO_GET_ACCESSOR, node);
  9811. ctx->type.SetDummy();
  9812. return;
  9813. }
  9814. asCExprValue objType = ctx->type;
  9815. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_get);
  9816. // Make sure the arg match the property
  9817. asCArray<int> funcs;
  9818. funcs.PushLast(ctx->property_get);
  9819. asCArray<asCExprContext *> args;
  9820. if( ctx->property_arg )
  9821. args.PushLast(ctx->property_arg);
  9822. MatchFunctions(funcs, args, node, func->GetName(), 0, func->objectType, ctx->property_const);
  9823. if( funcs.GetLength() == 0 )
  9824. {
  9825. // MatchFunctions already reported the error
  9826. if( ctx->property_arg )
  9827. {
  9828. asDELETE(ctx->property_arg, asCExprContext);
  9829. ctx->property_arg = 0;
  9830. }
  9831. ctx->type.SetDummy();
  9832. return;
  9833. }
  9834. if( func->objectType )
  9835. {
  9836. // Setup the context with the original type so the method call gets built correctly
  9837. ctx->type.dataType = asCDataType::CreateType(func->objectType, ctx->property_const);
  9838. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  9839. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  9840. // Don't allow the call if the object is read-only and the property accessor is not const
  9841. if( ctx->property_const && !func->isReadOnly )
  9842. {
  9843. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  9844. asCArray<int> funcCandidates;
  9845. funcCandidates.PushLast(ctx->property_get);
  9846. PrintMatchingFuncs(funcCandidates, node);
  9847. }
  9848. }
  9849. // The explicit handle flag must be remembered
  9850. bool isExplicitHandle = ctx->type.isExplicitHandle;
  9851. // Call the accessor
  9852. MakeFunctionCall(ctx, ctx->property_get, func->objectType, args, node);
  9853. if( isExplicitHandle )
  9854. ctx->type.isExplicitHandle = true;
  9855. // Clear the property get/set ids
  9856. ctx->property_get = 0;
  9857. ctx->property_set = 0;
  9858. if( ctx->property_arg )
  9859. {
  9860. asDELETE(ctx->property_arg, asCExprContext);
  9861. ctx->property_arg = 0;
  9862. }
  9863. }
  9864. int asCCompiler::CompileExpressionPostOp(asCScriptNode *node, asCExprContext *ctx)
  9865. {
  9866. // Don't allow any postfix operators on expressions that take address of class method
  9867. if( ctx->IsClassMethod() )
  9868. {
  9869. Error(TXT_INVALID_OP_ON_METHOD, node);
  9870. return -1;
  9871. }
  9872. // Don't allow any operators on void expressions
  9873. if( ctx->IsVoidExpression() )
  9874. {
  9875. Error(TXT_VOID_CANT_BE_OPERAND, node);
  9876. return -1;
  9877. }
  9878. // Check if the variable is initialized (if it indeed is a variable)
  9879. IsVariableInitialized(&ctx->type, node);
  9880. int op = node->tokenType;
  9881. if( (op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  9882. {
  9883. const char *opName = 0;
  9884. switch( op )
  9885. {
  9886. case ttInc: opName = "opPostInc"; break;
  9887. case ttDec: opName = "opPostDec"; break;
  9888. }
  9889. if( opName )
  9890. {
  9891. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  9892. ProcessPropertyGetAccessor(ctx, node);
  9893. // 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
  9894. // Find the correct method
  9895. bool isConst = ctx->type.dataType.IsObjectConst();
  9896. asCArray<int> funcs;
  9897. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  9898. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  9899. {
  9900. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  9901. if( func->name == opName &&
  9902. func->parameterTypes.GetLength() == 0 &&
  9903. (!isConst || func->isReadOnly) )
  9904. {
  9905. funcs.PushLast(func->id);
  9906. }
  9907. }
  9908. // Did we find the method?
  9909. if( funcs.GetLength() == 1 )
  9910. {
  9911. asCArray<asCExprContext *> args;
  9912. MakeFunctionCall(ctx, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  9913. return 0;
  9914. }
  9915. else if( funcs.GetLength() == 0 )
  9916. {
  9917. asCString str;
  9918. str = asCString(opName) + "()";
  9919. if( isConst )
  9920. str += " const";
  9921. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  9922. Error(str, node);
  9923. ctx->type.SetDummy();
  9924. return -1;
  9925. }
  9926. else if( funcs.GetLength() > 1 )
  9927. {
  9928. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  9929. PrintMatchingFuncs(funcs, node);
  9930. ctx->type.SetDummy();
  9931. return -1;
  9932. }
  9933. }
  9934. }
  9935. else if( op == ttInc || op == ttDec )
  9936. {
  9937. // Make sure the reference isn't a temporary variable
  9938. if( ctx->type.isTemporary )
  9939. {
  9940. Error(TXT_REF_IS_TEMP, node);
  9941. return -1;
  9942. }
  9943. if( ctx->type.dataType.IsReadOnly() )
  9944. {
  9945. Error(TXT_REF_IS_READ_ONLY, node);
  9946. return -1;
  9947. }
  9948. if( ctx->property_get || ctx->property_set )
  9949. {
  9950. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  9951. return -1;
  9952. }
  9953. if( !ctx->type.isLValue )
  9954. {
  9955. Error(TXT_NOT_LVALUE, node);
  9956. return -1;
  9957. }
  9958. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  9959. ConvertToReference(ctx);
  9960. else if( !ctx->type.dataType.IsReference() )
  9961. {
  9962. Error(TXT_NOT_VALID_REFERENCE, node);
  9963. return -1;
  9964. }
  9965. // Copy the value to a temp before changing it
  9966. ConvertToTempVariable(ctx);
  9967. asASSERT(!ctx->type.isLValue);
  9968. // Increment the value pointed to by the reference still in the register
  9969. asEBCInstr iInc = asBC_INCi, iDec = asBC_DECi;
  9970. if( ctx->type.dataType.IsDoubleType() )
  9971. {
  9972. iInc = asBC_INCd;
  9973. iDec = asBC_DECd;
  9974. }
  9975. else if( ctx->type.dataType.IsFloatType() )
  9976. {
  9977. iInc = asBC_INCf;
  9978. iDec = asBC_DECf;
  9979. }
  9980. else if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() )
  9981. {
  9982. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  9983. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  9984. {
  9985. iInc = asBC_INCi16;
  9986. iDec = asBC_DECi16;
  9987. }
  9988. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  9989. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  9990. {
  9991. iInc = asBC_INCi8;
  9992. iDec = asBC_DECi8;
  9993. }
  9994. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  9995. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  9996. {
  9997. iInc = asBC_INCi64;
  9998. iDec = asBC_DECi64;
  9999. }
  10000. }
  10001. else
  10002. {
  10003. Error(TXT_ILLEGAL_OPERATION, node);
  10004. return -1;
  10005. }
  10006. if( op == ttInc ) ctx->bc.Instr(iInc); else ctx->bc.Instr(iDec);
  10007. }
  10008. else if( op == ttDot )
  10009. {
  10010. if( node->firstChild->nodeType == snIdentifier )
  10011. {
  10012. ProcessPropertyGetAccessor(ctx, node);
  10013. // Get the property name
  10014. asCString name(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  10015. if( ctx->type.dataType.IsObject() )
  10016. {
  10017. // We need to look for get/set property accessors.
  10018. // If found, the context stores information on the get/set accessors
  10019. // until it is known which is to be used.
  10020. int r = 0;
  10021. if( node->next && node->next->tokenType == ttOpenBracket )
  10022. {
  10023. // The property accessor should take an index arg
  10024. asCExprContext dummyArg(engine);
  10025. r = FindPropertyAccessor(name, ctx, &dummyArg, node, 0);
  10026. }
  10027. if( r == 0 )
  10028. r = FindPropertyAccessor(name, ctx, node, 0);
  10029. if( r != 0 )
  10030. return r;
  10031. if( !ctx->type.dataType.IsPrimitive() )
  10032. Dereference(ctx, true);
  10033. if( ctx->type.dataType.IsObjectHandle() )
  10034. {
  10035. // Convert the handle to a normal object
  10036. asCDataType dt = ctx->type.dataType;
  10037. dt.MakeHandle(false);
  10038. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  10039. // The handle may not have been an lvalue, but the dereferenced object is
  10040. ctx->type.isLValue = true;
  10041. }
  10042. bool isConst = ctx->type.dataType.IsObjectConst();
  10043. asCObjectProperty *prop = builder->GetObjectProperty(ctx->type.dataType, name.AddressOf());
  10044. if( prop )
  10045. {
  10046. // Is the property access allowed?
  10047. if( (prop->isPrivate || prop->isProtected) && (!outFunc || outFunc->objectType != ctx->type.dataType.GetTypeInfo()) )
  10048. {
  10049. asCString msg;
  10050. if( prop->isPrivate )
  10051. msg.Format(TXT_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  10052. else
  10053. msg.Format(TXT_PROTECTED_PROP_ACCESS_s, name.AddressOf());
  10054. Error(msg, node);
  10055. }
  10056. // Put the offset on the stack
  10057. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(asCDataType::CreateType(ctx->type.dataType.GetTypeInfo(), false)));
  10058. if( prop->type.IsReference() )
  10059. ctx->bc.Instr(asBC_RDSPtr);
  10060. // Reference to primitive must be stored in the temp register
  10061. if( prop->type.IsPrimitive() )
  10062. {
  10063. ctx->bc.Instr(asBC_PopRPtr);
  10064. }
  10065. // Keep information about temporary variables as deferred expression
  10066. if( ctx->type.isTemporary )
  10067. {
  10068. // Add the release of this reference, as a deferred expression
  10069. asSDeferredParam deferred;
  10070. deferred.origExpr = 0;
  10071. deferred.argInOutFlags = asTM_INREF;
  10072. deferred.argNode = 0;
  10073. deferred.argType.SetVariable(ctx->type.dataType, ctx->type.stackOffset, true);
  10074. ctx->deferredParams.PushLast(deferred);
  10075. }
  10076. // Set the new type and make sure it is not treated as a variable anymore
  10077. ctx->type.dataType = prop->type;
  10078. ctx->type.dataType.MakeReference(true);
  10079. ctx->type.isVariable = false;
  10080. ctx->type.isTemporary = false;
  10081. if( (ctx->type.dataType.IsObject() || ctx->type.dataType.IsFuncdef()) && !ctx->type.dataType.IsObjectHandle() )
  10082. {
  10083. // Objects that are members are not references
  10084. ctx->type.dataType.MakeReference(false);
  10085. // The object is safe (life time guaranteed) if the parent object is also safe
  10086. }
  10087. else if (ctx->type.dataType.IsObjectHandle())
  10088. {
  10089. // A object accessed through a handle cannot be considered safe,
  10090. // as it can be cleared at any time
  10091. ctx->type.isHandleSafe = false;
  10092. }
  10093. ctx->type.dataType.MakeReadOnly(isConst ? true : prop->type.IsReadOnly());
  10094. }
  10095. else
  10096. {
  10097. // If the name is not a property, the compiler must check if the name matches
  10098. // a method, which can be used for constructing delegates
  10099. asIScriptFunction *func = 0;
  10100. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  10101. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  10102. {
  10103. if( engine->scriptFunctions[ot->methods[n]]->name == name )
  10104. {
  10105. func = engine->scriptFunctions[ot->methods[n]];
  10106. break;
  10107. }
  10108. }
  10109. if( func )
  10110. {
  10111. // An object method was found. Keep the name of the method in the expression, but
  10112. // don't actually modify the bytecode at this point since it is not yet known what
  10113. // the method will be used for, or even what overloaded method should be used.
  10114. ctx->methodName = name;
  10115. }
  10116. else
  10117. {
  10118. asCString str;
  10119. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  10120. Error(str, node);
  10121. return -1;
  10122. }
  10123. }
  10124. }
  10125. else
  10126. {
  10127. asCString str;
  10128. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  10129. Error(str, node);
  10130. return -1;
  10131. }
  10132. }
  10133. else
  10134. {
  10135. // Make sure it is an object we are accessing
  10136. if( !ctx->type.dataType.IsObject() )
  10137. {
  10138. asCString str;
  10139. str.Format(TXT_ILLEGAL_OPERATION_ON_s, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  10140. Error(str, node);
  10141. return -1;
  10142. }
  10143. // Process the get property accessor
  10144. ProcessPropertyGetAccessor(ctx, node);
  10145. // Compile function call
  10146. int r = CompileFunctionCall(node->firstChild, ctx, CastToObjectType(ctx->type.dataType.GetTypeInfo()), ctx->type.dataType.IsObjectConst());
  10147. if( r < 0 ) return r;
  10148. }
  10149. }
  10150. else if( op == ttOpenBracket )
  10151. {
  10152. // If the property access takes an index arg and the argument hasn't been evaluated yet,
  10153. // then we should use that instead of processing it now. If the argument has already been
  10154. // evaluated, then we should process the property accessor as a get access now as the new
  10155. // index operator is on the result of that accessor.
  10156. asCString propertyName;
  10157. asSNameSpace *ns = 0;
  10158. if( ((ctx->property_get && builder->GetFunctionDescription(ctx->property_get)->GetParamCount() == 1) ||
  10159. (ctx->property_set && builder->GetFunctionDescription(ctx->property_set)->GetParamCount() == 2)) &&
  10160. (ctx->property_arg && ctx->property_arg->type.dataType.GetTokenType() == ttUnrecognizedToken) )
  10161. {
  10162. // Determine the name of the property accessor
  10163. asCScriptFunction *func = 0;
  10164. if( ctx->property_get )
  10165. func = builder->GetFunctionDescription(ctx->property_get);
  10166. else
  10167. func = builder->GetFunctionDescription(ctx->property_set);
  10168. propertyName = func->GetName();
  10169. propertyName = propertyName.SubString(4);
  10170. // Set the original type of the expression so we can re-evaluate the property accessor
  10171. if( func->objectType )
  10172. {
  10173. ctx->type.dataType = asCDataType::CreateType(func->objectType, ctx->property_const);
  10174. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  10175. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  10176. }
  10177. else
  10178. {
  10179. // Store the namespace where the function is declared
  10180. // so the same function can be found later
  10181. ctx->type.SetDummy();
  10182. ns = func->nameSpace;
  10183. }
  10184. ctx->property_get = ctx->property_set = 0;
  10185. if( ctx->property_arg )
  10186. {
  10187. asDELETE(ctx->property_arg, asCExprContext);
  10188. ctx->property_arg = 0;
  10189. }
  10190. }
  10191. else
  10192. {
  10193. if( !ctx->type.dataType.IsObject() )
  10194. {
  10195. asCString str;
  10196. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  10197. Error(str, node);
  10198. return -1;
  10199. }
  10200. ProcessPropertyGetAccessor(ctx, node);
  10201. }
  10202. // Compile the expression
  10203. bool isOK = true;
  10204. asCArray<asCExprContext *> args;
  10205. asCArray<asSNamedArgument> namedArgs;
  10206. asASSERT( node->firstChild->nodeType == snArgList );
  10207. if( CompileArgumentList(node->firstChild, args, namedArgs) >= 0 )
  10208. {
  10209. // Check for the existence of the opIndex method
  10210. bool lookForProperty = true;
  10211. if( propertyName == "" )
  10212. {
  10213. bool isConst = ctx->type.dataType.IsObjectConst();
  10214. asCObjectType *objectType = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  10215. asCArray<int> funcs;
  10216. builder->GetObjectMethodDescriptions("opIndex", objectType, funcs, isConst);
  10217. if( funcs.GetLength() > 0 )
  10218. {
  10219. // Since there are opIndex methods, the compiler should not look for get/set_opIndex accessors
  10220. lookForProperty = false;
  10221. // Determine which of opIndex methods that match
  10222. MatchFunctions(funcs, args, node, "opIndex", 0, objectType, isConst);
  10223. if( funcs.GetLength() != 1 )
  10224. {
  10225. // The error has already been reported by MatchFunctions
  10226. isOK = false;
  10227. }
  10228. else
  10229. {
  10230. // Add the default values for arguments not explicitly supplied
  10231. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], objectType);
  10232. if( r == 0 )
  10233. MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, ctx->type.stackOffset);
  10234. else
  10235. isOK = false;
  10236. }
  10237. }
  10238. }
  10239. if( lookForProperty && isOK )
  10240. {
  10241. if( args.GetLength() != 1 )
  10242. {
  10243. // TODO: opIndex: Implement this
  10244. Error("Property accessor with index only support 1 index argument for now", node);
  10245. isOK = false;
  10246. }
  10247. Dereference(ctx, true);
  10248. asCExprContext lctx(engine);
  10249. MergeExprBytecodeAndType(&lctx, ctx);
  10250. // Check for accessors methods for the opIndex, either as get/set_opIndex or as get/set with the property name
  10251. int r = FindPropertyAccessor(propertyName == "" ? "opIndex" : propertyName.AddressOf(), &lctx, args[0], node, ns);
  10252. if( r == 0 )
  10253. {
  10254. asCString str;
  10255. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  10256. Error(str, node);
  10257. isOK = false;
  10258. }
  10259. else if( r < 0 )
  10260. isOK = false;
  10261. if( isOK )
  10262. MergeExprBytecodeAndType(ctx, &lctx);
  10263. }
  10264. }
  10265. else
  10266. isOK = false;
  10267. // Cleanup
  10268. for( asUINT n = 0; n < args.GetLength(); n++ )
  10269. if( args[n] )
  10270. {
  10271. asDELETE(args[n], asCExprContext);
  10272. }
  10273. if( !isOK )
  10274. return -1;
  10275. }
  10276. else if( op == ttOpenParanthesis )
  10277. {
  10278. // TODO: Most of this is already done by CompileFunctionCall(). Can we share the code?
  10279. // Make sure the expression is a funcdef or an object that may have opCall methods
  10280. if( !ctx->type.dataType.GetTypeInfo() || (!ctx->type.dataType.IsFuncdef() && !ctx->type.dataType.IsObject()) )
  10281. {
  10282. Error(TXT_EXPR_DOESNT_EVAL_TO_FUNC, node);
  10283. return -1;
  10284. }
  10285. // Compile arguments
  10286. asCArray<asCExprContext *> args;
  10287. asCArray<asSNamedArgument> namedArgs;
  10288. if( CompileArgumentList(node->lastChild, args, namedArgs) >= 0 )
  10289. {
  10290. // Match arguments with the funcdef
  10291. asCArray<int> funcs;
  10292. if( ctx->type.dataType.IsFuncdef() )
  10293. {
  10294. funcs.PushLast(CastToFuncdefType(ctx->type.dataType.GetTypeInfo())->funcdef->id);
  10295. MatchFunctions(funcs, args, node, ctx->type.dataType.GetTypeInfo()->name.AddressOf(), &namedArgs);
  10296. }
  10297. else
  10298. {
  10299. bool isConst = ctx->type.dataType.IsObjectConst();
  10300. builder->GetObjectMethodDescriptions("opCall", CastToObjectType(ctx->type.dataType.GetTypeInfo()), funcs, isConst);
  10301. MatchFunctions(funcs, args, node, "opCall", &namedArgs, CastToObjectType(ctx->type.dataType.GetTypeInfo()), isConst);
  10302. }
  10303. if( funcs.GetLength() != 1 )
  10304. {
  10305. // The error was reported by MatchFunctions()
  10306. // Dummy value
  10307. ctx->type.SetDummy();
  10308. }
  10309. else
  10310. {
  10311. // Add the default values for arguments not explicitly supplied
  10312. int r = CompileDefaultAndNamedArgs(node, args, funcs[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), &namedArgs);
  10313. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  10314. // is it enough to make sure it is in a local variable?
  10315. // For function pointer we must guarantee that the function is safe, i.e.
  10316. // by first storing the function pointer in a local variable (if it isn't already in one)
  10317. if( r == asSUCCESS )
  10318. {
  10319. Dereference(ctx, true);
  10320. if( ctx->type.dataType.IsFuncdef() )
  10321. {
  10322. if( !ctx->type.isVariable )
  10323. ConvertToVariable(ctx);
  10324. // Remove the reference from the stack as the asBC_CALLPTR instruction takes the variable as argument
  10325. ctx->bc.Instr(asBC_PopPtr);
  10326. }
  10327. MakeFunctionCall(ctx, funcs[0], ctx->type.dataType.IsFuncdef() ? 0 : CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node, false, 0, ctx->type.stackOffset);
  10328. }
  10329. }
  10330. }
  10331. else
  10332. ctx->type.SetDummy();
  10333. // Cleanup
  10334. for( asUINT n = 0; n < args.GetLength(); n++ )
  10335. if( args[n] )
  10336. {
  10337. asDELETE(args[n], asCExprContext);
  10338. }
  10339. for( asUINT n = 0; n < namedArgs.GetLength(); n++ )
  10340. if( namedArgs[n].ctx )
  10341. {
  10342. asDELETE(namedArgs[n].ctx, asCExprContext);
  10343. }
  10344. }
  10345. return 0;
  10346. }
  10347. int asCCompiler::GetPrecedence(asCScriptNode *op)
  10348. {
  10349. // x ** y
  10350. // x * y, x / y, x % y
  10351. // x + y, x - y
  10352. // x <= y, x < y, x >= y, x > y
  10353. // x = =y, x != y, x xor y, x is y, x !is y
  10354. // x and y
  10355. // x or y
  10356. // The following are not used in this function,
  10357. // but should have lower precedence than the above
  10358. // x ? y : z
  10359. // x = y
  10360. // The expression term have the highest precedence
  10361. if( op->nodeType == snExprTerm )
  10362. return 1;
  10363. // Evaluate operators by token
  10364. int tokenType = op->tokenType;
  10365. if( tokenType == ttStarStar )
  10366. return 0;
  10367. if( tokenType == ttStar || tokenType == ttSlash || tokenType == ttPercent )
  10368. return -1;
  10369. if( tokenType == ttPlus || tokenType == ttMinus )
  10370. return -2;
  10371. if( tokenType == ttBitShiftLeft ||
  10372. tokenType == ttBitShiftRight ||
  10373. tokenType == ttBitShiftRightArith )
  10374. return -3;
  10375. if( tokenType == ttAmp )
  10376. return -4;
  10377. if( tokenType == ttBitXor )
  10378. return -5;
  10379. if( tokenType == ttBitOr )
  10380. return -6;
  10381. if( tokenType == ttLessThanOrEqual ||
  10382. tokenType == ttLessThan ||
  10383. tokenType == ttGreaterThanOrEqual ||
  10384. tokenType == ttGreaterThan )
  10385. return -7;
  10386. if( tokenType == ttEqual || tokenType == ttNotEqual || tokenType == ttXor || tokenType == ttIs || tokenType == ttNotIs )
  10387. return -8;
  10388. if( tokenType == ttAnd )
  10389. return -9;
  10390. if( tokenType == ttOr )
  10391. return -10;
  10392. // Unknown operator
  10393. asASSERT(false);
  10394. return 0;
  10395. }
  10396. asUINT asCCompiler::MatchArgument(asCArray<int> &funcs, asCArray<asSOverloadCandidate> &matches, const asCExprContext *argExpr, int paramNum, bool allowObjectConstruct)
  10397. {
  10398. matches.SetLength(0);
  10399. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  10400. {
  10401. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  10402. // Does the function have arguments enough?
  10403. if( (int)desc->parameterTypes.GetLength() <= paramNum )
  10404. continue;
  10405. int cost = MatchArgument(desc, argExpr, paramNum, allowObjectConstruct);
  10406. if( cost != -1 )
  10407. matches.PushLast(asSOverloadCandidate(funcs[n], asUINT(cost)));
  10408. }
  10409. return (asUINT)matches.GetLength();
  10410. }
  10411. int asCCompiler::MatchArgument(asCScriptFunction *desc, const asCExprContext *argExpr, int paramNum, bool allowObjectConstruct)
  10412. {
  10413. // void expressions can match any out parameter, but nothing else
  10414. if( argExpr->IsVoidExpression() )
  10415. {
  10416. if( desc->inOutFlags[paramNum] == asTM_OUTREF )
  10417. return 0;
  10418. return -1;
  10419. }
  10420. // Can we make the match by implicit conversion?
  10421. asCExprContext ti(engine);
  10422. ti.type = argExpr->type;
  10423. ti.methodName = argExpr->methodName;
  10424. ti.enumValue = argExpr->enumValue;
  10425. ti.exprNode = argExpr->exprNode;
  10426. if( argExpr->type.dataType.IsPrimitive() )
  10427. ti.type.dataType.MakeReference(false);
  10428. // Don't allow the implicit conversion to make a copy in case the argument is expecting a reference to the true value
  10429. if (desc->parameterTypes[paramNum].IsReference() && desc->inOutFlags[paramNum] == asTM_INOUTREF)
  10430. allowObjectConstruct = false;
  10431. int cost = ImplicitConversion(&ti, desc->parameterTypes[paramNum], 0, asIC_IMPLICIT_CONV, false, allowObjectConstruct);
  10432. // If the function parameter is an inout-reference then it must not be possible to call the
  10433. // function with an incorrect argument type, even though the type can normally be converted.
  10434. if( desc->parameterTypes[paramNum].IsReference() &&
  10435. desc->inOutFlags[paramNum] == asTM_INOUTREF &&
  10436. desc->parameterTypes[paramNum].GetTokenType() != ttQuestion )
  10437. {
  10438. // Observe, that the below checks are only necessary for when unsafe references have been
  10439. // enabled by the application. Without this the &inout reference form wouldn't be allowed
  10440. // for these value types.
  10441. // Don't allow a primitive to be converted to a reference of another primitive type
  10442. if( desc->parameterTypes[paramNum].IsPrimitive() &&
  10443. desc->parameterTypes[paramNum].GetTokenType() != argExpr->type.dataType.GetTokenType() )
  10444. {
  10445. asASSERT( engine->ep.allowUnsafeReferences );
  10446. return -1;
  10447. }
  10448. // Don't allow an enum to be converted to a reference of another enum type
  10449. if( desc->parameterTypes[paramNum].IsEnumType() &&
  10450. desc->parameterTypes[paramNum].GetTypeInfo() != argExpr->type.dataType.GetTypeInfo() )
  10451. {
  10452. asASSERT( engine->ep.allowUnsafeReferences );
  10453. return -1;
  10454. }
  10455. // Don't allow a non-handle expression to be converted to a reference to a handle
  10456. if( desc->parameterTypes[paramNum].IsObjectHandle() &&
  10457. !argExpr->type.dataType.IsObjectHandle() )
  10458. {
  10459. asASSERT( engine->ep.allowUnsafeReferences );
  10460. return -1;
  10461. }
  10462. // Don't allow a value type to be converted
  10463. if( (desc->parameterTypes[paramNum].GetTypeInfo() && (desc->parameterTypes[paramNum].GetTypeInfo()->GetFlags() & asOBJ_VALUE)) &&
  10464. (desc->parameterTypes[paramNum].GetTypeInfo() != argExpr->type.dataType.GetTypeInfo()) )
  10465. {
  10466. asASSERT( engine->ep.allowUnsafeReferences );
  10467. return -1;
  10468. }
  10469. }
  10470. // How well does the argument match the function parameter?
  10471. if( desc->parameterTypes[paramNum].IsEqualExceptRef(ti.type.dataType) )
  10472. return cost;
  10473. // No match is available
  10474. return -1;
  10475. }
  10476. void asCCompiler::PrepareArgument2(asCExprContext *ctx, asCExprContext *arg, asCDataType *paramType, bool isFunction, int refType, bool isMakingCopy)
  10477. {
  10478. // Reference parameters whose value won't be used don't evaluate the expression
  10479. // Clean arguments (i.e. default value) will be passed in directly as there is nothing to protect
  10480. if( paramType->IsReference() && !(refType & asTM_INREF) && !arg->isCleanArg )
  10481. {
  10482. // Store the original bytecode so that it can be reused when processing the deferred output parameter
  10483. asCExprContext *orig = asNEW(asCExprContext)(engine);
  10484. if( orig == 0 )
  10485. {
  10486. // Out of memory
  10487. return;
  10488. }
  10489. MergeExprBytecodeAndType(orig, arg);
  10490. arg->origExpr = orig;
  10491. }
  10492. PrepareArgument(paramType, arg, arg->exprNode, isFunction, refType, isMakingCopy);
  10493. // arg still holds the original expression for output parameters
  10494. ctx->bc.AddCode(&arg->bc);
  10495. }
  10496. bool asCCompiler::CompileOverloadedDualOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, bool leftToRight, asCExprContext *ctx, bool isHandle, eTokenType token)
  10497. {
  10498. DetermineSingleFunc(lctx, node);
  10499. DetermineSingleFunc(rctx, node);
  10500. ctx->exprNode = node;
  10501. // What type of operator is it?
  10502. if( token == ttUnrecognizedToken )
  10503. token = node->tokenType;
  10504. if( token == ttUnrecognizedToken )
  10505. {
  10506. // This happens when the compiler is inferring an assignment
  10507. // operation from another action, for example in preparing a value
  10508. // as a function argument
  10509. token = ttAssignment;
  10510. }
  10511. // boolean operators are not overloadable
  10512. if( token == ttAnd ||
  10513. token == ttOr ||
  10514. token == ttXor )
  10515. return false;
  10516. // Dual operators can also be implemented as class methods
  10517. if( token == ttEqual ||
  10518. token == ttNotEqual )
  10519. {
  10520. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  10521. // Find the matching opEquals method
  10522. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, leftToRight, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  10523. if( r == 0 )
  10524. {
  10525. // Try again by switching the order of the operands
  10526. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, !leftToRight, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  10527. }
  10528. if( r == 1 )
  10529. {
  10530. if( token == ttNotEqual )
  10531. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  10532. // Success, don't continue
  10533. return true;
  10534. }
  10535. else if( r < 0 )
  10536. {
  10537. // Compiler error, don't continue
  10538. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  10539. return true;
  10540. }
  10541. }
  10542. if( token == ttEqual ||
  10543. token == ttNotEqual ||
  10544. token == ttLessThan ||
  10545. token == ttLessThanOrEqual ||
  10546. token == ttGreaterThan ||
  10547. token == ttGreaterThanOrEqual )
  10548. {
  10549. bool swappedOrder = false;
  10550. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  10551. // Find the matching opCmp method
  10552. int r = CompileOverloadedDualOperator2(node, "opCmp", lctx, rctx, leftToRight, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  10553. if( r == 0 )
  10554. {
  10555. // Try again by switching the order of the operands
  10556. swappedOrder = true;
  10557. r = CompileOverloadedDualOperator2(node, "opCmp", rctx, lctx, !leftToRight, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  10558. }
  10559. if( r == 1 )
  10560. {
  10561. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  10562. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  10563. ctx->bc.InstrW_DW(asBC_CMPIi, ctx->type.stackOffset, 0);
  10564. if( token == ttEqual )
  10565. ctx->bc.Instr(asBC_TZ);
  10566. else if( token == ttNotEqual )
  10567. ctx->bc.Instr(asBC_TNZ);
  10568. else if( (token == ttLessThan && !swappedOrder) ||
  10569. (token == ttGreaterThan && swappedOrder) )
  10570. ctx->bc.Instr(asBC_TS);
  10571. else if( (token == ttLessThanOrEqual && !swappedOrder) ||
  10572. (token == ttGreaterThanOrEqual && swappedOrder) )
  10573. ctx->bc.Instr(asBC_TNP);
  10574. else if( (token == ttGreaterThan && !swappedOrder) ||
  10575. (token == ttLessThan && swappedOrder) )
  10576. ctx->bc.Instr(asBC_TP);
  10577. else if( (token == ttGreaterThanOrEqual && !swappedOrder) ||
  10578. (token == ttLessThanOrEqual && swappedOrder) )
  10579. ctx->bc.Instr(asBC_TNS);
  10580. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  10581. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), a, true);
  10582. // Success, don't continue
  10583. return true;
  10584. }
  10585. else if( r < 0 )
  10586. {
  10587. // Compiler error, don't continue
  10588. #if AS_SIZEOF_BOOL == 1
  10589. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  10590. #else
  10591. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  10592. #endif
  10593. return true;
  10594. }
  10595. }
  10596. // The rest of the operators are not commutative, and doesn't require specific return type
  10597. const char *op = 0, *op_r = 0;
  10598. switch( int(token) ) // convert to int to avoid warning in gnuc that not all values are tested
  10599. {
  10600. case ttPlus: op = "opAdd"; op_r = "opAdd_r"; break;
  10601. case ttMinus: op = "opSub"; op_r = "opSub_r"; break;
  10602. case ttStar: op = "opMul"; op_r = "opMul_r"; break;
  10603. case ttSlash: op = "opDiv"; op_r = "opDiv_r"; break;
  10604. case ttPercent: op = "opMod"; op_r = "opMod_r"; break;
  10605. case ttStarStar: op = "opPow"; op_r = "opPow_r"; break;
  10606. case ttBitOr: op = "opOr"; op_r = "opOr_r"; break;
  10607. case ttAmp: op = "opAnd"; op_r = "opAnd_r"; break;
  10608. case ttBitXor: op = "opXor"; op_r = "opXor_r"; break;
  10609. case ttBitShiftLeft: op = "opShl"; op_r = "opShl_r"; break;
  10610. case ttBitShiftRight: op = "opShr"; op_r = "opShr_r"; break;
  10611. case ttBitShiftRightArith: op = "opUShr"; op_r = "opUShr_r"; break;
  10612. }
  10613. // TODO: Might be interesting to support a concatenation operator, e.g. ~
  10614. if( op && op_r )
  10615. {
  10616. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  10617. // Find the matching operator method
  10618. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, leftToRight, ctx);
  10619. if( r == 0 )
  10620. {
  10621. // Try again by switching the order of the operands, and using the reversed operator
  10622. r = CompileOverloadedDualOperator2(node, op_r, rctx, lctx, !leftToRight, ctx);
  10623. }
  10624. if( r == 1 )
  10625. {
  10626. // Success, don't continue
  10627. return true;
  10628. }
  10629. else if( r < 0 )
  10630. {
  10631. // Compiler error, don't continue
  10632. ctx->type.SetDummy();
  10633. return true;
  10634. }
  10635. }
  10636. // Assignment operators
  10637. op = 0;
  10638. if( isHandle )
  10639. {
  10640. // Only asOBJ_ASHANDLE types can get here
  10641. asASSERT( lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) );
  10642. asASSERT( token == ttAssignment );
  10643. if( token == ttAssignment )
  10644. op = "opHndlAssign";
  10645. }
  10646. else
  10647. {
  10648. switch( int(token) ) // convert to int to avoid warning in gnuc that not all values are tested
  10649. {
  10650. case ttAssignment: op = "opAssign"; break;
  10651. case ttAddAssign: op = "opAddAssign"; break;
  10652. case ttSubAssign: op = "opSubAssign"; break;
  10653. case ttMulAssign: op = "opMulAssign"; break;
  10654. case ttDivAssign: op = "opDivAssign"; break;
  10655. case ttModAssign: op = "opModAssign"; break;
  10656. case ttPowAssign: op = "opPowAssign"; break;
  10657. case ttOrAssign: op = "opOrAssign"; break;
  10658. case ttAndAssign: op = "opAndAssign"; break;
  10659. case ttXorAssign: op = "opXorAssign"; break;
  10660. case ttShiftLeftAssign: op = "opShlAssign"; break;
  10661. case ttShiftRightLAssign: op = "opShrAssign"; break;
  10662. case ttShiftRightAAssign: op = "opUShrAssign"; break;
  10663. }
  10664. }
  10665. if( op )
  10666. {
  10667. if( builder->engine->ep.disallowValueAssignForRefType &&
  10668. lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_REF) && !(lctx->type.dataType.GetTypeInfo()->flags & asOBJ_SCOPED) )
  10669. {
  10670. if( token == ttAssignment )
  10671. Error(TXT_DISALLOW_ASSIGN_ON_REF_TYPE, node);
  10672. else
  10673. Error(TXT_DISALLOW_COMPOUND_ASSIGN_ON_REF_TYPE, node);
  10674. // Set a dummy output
  10675. ctx->type.Set(lctx->type.dataType);
  10676. return true;
  10677. }
  10678. // TODO: Shouldn't accept const lvalue with the assignment operators
  10679. // Find the matching operator method
  10680. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, false, ctx);
  10681. if( r == 1 )
  10682. {
  10683. // Success, don't continue
  10684. return true;
  10685. }
  10686. else if( r < 0 )
  10687. {
  10688. // Compiler error, don't continue
  10689. ctx->type.SetDummy();
  10690. return true;
  10691. }
  10692. }
  10693. // No suitable operator was found
  10694. return false;
  10695. }
  10696. // Returns negative on compile error
  10697. // zero on no matching operator
  10698. // one on matching operator
  10699. int asCCompiler::CompileOverloadedDualOperator2(asCScriptNode *node, const char *methodName, asCExprContext *lctx, asCExprContext *rctx, bool leftToRight, asCExprContext *ctx, bool specificReturn, const asCDataType &returnType)
  10700. {
  10701. // Find the matching method
  10702. if( lctx->type.dataType.IsObject() &&
  10703. (!lctx->type.isExplicitHandle ||
  10704. lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE) )
  10705. {
  10706. asUINT n;
  10707. // Is the left value a const?
  10708. bool isConst = lctx->type.dataType.IsObjectConst();
  10709. asCArray<int> funcs;
  10710. asCObjectType *ot = CastToObjectType(lctx->type.dataType.GetTypeInfo());
  10711. for( n = 0; n < ot->methods.GetLength(); n++ )
  10712. {
  10713. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  10714. asASSERT( func );
  10715. if( func && func->name == methodName &&
  10716. (!specificReturn || func->returnType == returnType) &&
  10717. func->parameterTypes.GetLength() == 1 &&
  10718. (!isConst || func->isReadOnly) )
  10719. {
  10720. // Make sure the method is accessible by the module
  10721. if( builder->module->accessMask & func->accessMask )
  10722. {
  10723. funcs.PushLast(func->id);
  10724. }
  10725. }
  10726. }
  10727. // Which is the best matching function?
  10728. asCArray<asSOverloadCandidate> tempFuncs;
  10729. MatchArgument(funcs, tempFuncs, rctx, 0);
  10730. // Find the lowest cost operator(s)
  10731. asCArray<int> ops;
  10732. asUINT bestCost = asUINT(-1);
  10733. for( n = 0; n < tempFuncs.GetLength(); ++n )
  10734. {
  10735. asUINT cost = tempFuncs[n].cost;
  10736. if( cost < bestCost )
  10737. {
  10738. ops.SetLength(0);
  10739. bestCost = cost;
  10740. }
  10741. if( cost == bestCost )
  10742. ops.PushLast(tempFuncs[n].funcId);
  10743. }
  10744. // If the object is not const, then we need to prioritize non-const methods
  10745. if( !isConst )
  10746. FilterConst(ops);
  10747. // Did we find an operator?
  10748. if( ops.GetLength() == 1 )
  10749. {
  10750. // Reserve the variables used in the right expression so the new temporary
  10751. // variable allocated for the left operand isn't accidentally overwritten.
  10752. int l = int(reservedVariables.GetLength());
  10753. rctx->bc.GetVarsUsed(reservedVariables);
  10754. // Process the lctx expression as get accessor
  10755. ProcessPropertyGetAccessor(lctx, node);
  10756. reservedVariables.SetLength(l);
  10757. asCExprContext tmpCtx(engine);
  10758. if (leftToRight)
  10759. {
  10760. // Make sure lctx is in fact a variable. If it is a reference there is no
  10761. // guarantee that the reference will stay alive throughout the evaluation of rctx
  10762. if (!lctx->type.isVariable)
  10763. {
  10764. // Reserve the variables used in the right expression so the new temporary
  10765. // variable allocated for the left operand isn't accidentally overwritten.
  10766. l = int(reservedVariables.GetLength());
  10767. rctx->bc.GetVarsUsed(reservedVariables);
  10768. if (lctx->type.dataType.SupportHandles())
  10769. lctx->type.dataType.MakeHandle(true);
  10770. PrepareTemporaryVariable(node, lctx);
  10771. reservedVariables.SetLength(l);
  10772. }
  10773. // Move the bytecode for the left operand to a temporary context
  10774. // so we can later make sure this is computed first
  10775. tmpCtx.bc.AddCode(&lctx->bc);
  10776. tmpCtx.bc.Instr(asBC_PopPtr);
  10777. // Add bytecode to push the object pointer computed in the left operand on the stack as the this pointer
  10778. // This will be placed after rctx by MakeFunctionCall below
  10779. lctx->bc.InstrWORD(asBC_PSF, lctx->type.stackOffset);
  10780. // Implicitly dereference handle parameters sent by reference
  10781. sVariable *v = variables->GetVariableByOffset(lctx->type.stackOffset);
  10782. if (v && v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()))
  10783. lctx->bc.Instr(asBC_RDSPtr);
  10784. }
  10785. else
  10786. {
  10787. // Make sure the rvalue doesn't have deferred temporary variables that are also used in the lvalue,
  10788. // since that would cause the VM to overwrite the variable while executing the bytecode for the lvalue.
  10789. asCArray<int> usedVars;
  10790. lctx->bc.GetVarsUsed(usedVars);
  10791. asUINT oldReservedVars = reservedVariables.GetLength();
  10792. for (n = 0; n < rctx->deferredParams.GetLength(); n++)
  10793. {
  10794. if (rctx->deferredParams[n].argType.isTemporary &&
  10795. usedVars.Exists(rctx->deferredParams[n].argType.stackOffset))
  10796. {
  10797. if (reservedVariables.GetLength() == oldReservedVars)
  10798. reservedVariables.Concatenate(usedVars);
  10799. // Allocate a new variable for the deferred argument
  10800. int offset = AllocateVariableNotIn(rctx->deferredParams[n].argType.dataType, true, false, rctx);
  10801. int oldVar = rctx->deferredParams[n].argType.stackOffset;
  10802. rctx->deferredParams[n].argType.stackOffset = short(offset);
  10803. rctx->bc.ExchangeVar(oldVar, offset);
  10804. ReleaseTemporaryVariable(oldVar, 0);
  10805. }
  10806. }
  10807. reservedVariables.SetLength(oldReservedVars);
  10808. }
  10809. // Merge the bytecode so that it forms lvalue.methodName(rvalue)
  10810. asCArray<asCExprContext *> args;
  10811. args.PushLast(rctx);
  10812. MergeExprBytecode(ctx, lctx);
  10813. ctx->type = lctx->type;
  10814. MakeFunctionCall(ctx, ops[0], CastToObjectType(ctx->type.dataType.GetTypeInfo()), args, node);
  10815. // Rearrange the bytecode so the left argument is computed first
  10816. if (leftToRight)
  10817. {
  10818. tmpCtx.bc.AddCode(&ctx->bc);
  10819. ctx->bc.AddCode(&tmpCtx.bc);
  10820. }
  10821. // Found matching operator
  10822. return 1;
  10823. }
  10824. else if( ops.GetLength() > 1 )
  10825. {
  10826. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  10827. PrintMatchingFuncs(ops, node);
  10828. ctx->type.SetDummy();
  10829. // Compiler error
  10830. return -1;
  10831. }
  10832. }
  10833. // No matching operator
  10834. return 0;
  10835. }
  10836. void asCCompiler::MakeFunctionCall(asCExprContext *ctx, int funcId, asCObjectType *objectType, asCArray<asCExprContext*> &args, asCScriptNode *node, bool useVariable, int stackOffset, int funcPtrVar)
  10837. {
  10838. if( objectType )
  10839. Dereference(ctx, true);
  10840. // Store the expression node for error reporting
  10841. if( ctx->exprNode == 0 )
  10842. ctx->exprNode = node;
  10843. asCByteCode objBC(engine);
  10844. objBC.AddCode(&ctx->bc);
  10845. PrepareFunctionCall(funcId, &ctx->bc, args);
  10846. // Verify if any of the args variable offsets are used in the other code.
  10847. // If they are exchange the offset for a new one
  10848. asUINT n;
  10849. for( n = 0; n < args.GetLength(); n++ )
  10850. {
  10851. if( args[n]->type.isTemporary && objBC.IsVarUsed(args[n]->type.stackOffset) )
  10852. {
  10853. // Release the current temporary variable
  10854. ReleaseTemporaryVariable(args[n]->type, 0);
  10855. asCDataType dt = args[n]->type.dataType;
  10856. dt.MakeReference(false);
  10857. int l = int(reservedVariables.GetLength());
  10858. objBC.GetVarsUsed(reservedVariables);
  10859. ctx->bc.GetVarsUsed(reservedVariables);
  10860. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(args[n]->type.stackOffset));
  10861. reservedVariables.SetLength(l);
  10862. asASSERT( IsVariableOnHeap(args[n]->type.stackOffset) == IsVariableOnHeap(newOffset) );
  10863. ctx->bc.ExchangeVar(args[n]->type.stackOffset, newOffset);
  10864. args[n]->type.stackOffset = (short)newOffset;
  10865. args[n]->type.isTemporary = true;
  10866. args[n]->type.isVariable = true;
  10867. }
  10868. }
  10869. // If the function will return a value type on the stack, then we must allocate space
  10870. // for that here and push the address on the stack as a hidden argument to the function
  10871. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  10872. if( func->DoesReturnOnStack() )
  10873. {
  10874. asASSERT(!useVariable);
  10875. useVariable = true;
  10876. stackOffset = AllocateVariable(func->returnType, true);
  10877. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  10878. }
  10879. ctx->bc.AddCode(&objBC);
  10880. MoveArgsToStack(funcId, &ctx->bc, args, objectType ? true : false);
  10881. PerformFunctionCall(funcId, ctx, false, &args, 0, useVariable, stackOffset, funcPtrVar);
  10882. }
  10883. int asCCompiler::CompileOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op, bool leftToRight)
  10884. {
  10885. // Don't allow any operators on expressions that take address of class method, but allow it on global functions
  10886. if( (lctx->IsClassMethod()) || (rctx->IsClassMethod()) )
  10887. {
  10888. Error(TXT_INVALID_OP_ON_METHOD, node);
  10889. return -1;
  10890. }
  10891. // Don't allow any operators on void expressions
  10892. if( lctx->IsVoidExpression() || rctx->IsVoidExpression() )
  10893. {
  10894. Error(TXT_VOID_CANT_BE_OPERAND, node);
  10895. return -1;
  10896. }
  10897. if( op == ttUnrecognizedToken )
  10898. op = node->tokenType;
  10899. IsVariableInitialized(&lctx->type, node);
  10900. IsVariableInitialized(&rctx->type, node);
  10901. if( lctx->type.isExplicitHandle || rctx->type.isExplicitHandle ||
  10902. lctx->type.IsNullConstant() || rctx->type.IsNullConstant() ||
  10903. op == ttIs || op == ttNotIs )
  10904. {
  10905. CompileOperatorOnHandles(node, lctx, rctx, ctx, op);
  10906. return 0;
  10907. }
  10908. else
  10909. {
  10910. // Compile an overloaded operator for the two operands
  10911. if( CompileOverloadedDualOperator(node, lctx, rctx, leftToRight, ctx, false, op) )
  10912. return 0;
  10913. // If both operands are objects, then we shouldn't continue
  10914. if( lctx->type.dataType.IsObject() && rctx->type.dataType.IsObject() )
  10915. {
  10916. asCString str;
  10917. str.Format(TXT_NO_MATCHING_OP_FOUND_FOR_TYPES_s_AND_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), rctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  10918. Error(str, node);
  10919. ctx->type.SetDummy();
  10920. return -1;
  10921. }
  10922. // Process the property get accessors (if any)
  10923. ProcessPropertyGetAccessor(lctx, node);
  10924. ProcessPropertyGetAccessor(rctx, node);
  10925. // Make sure we have two variables or constants
  10926. if( lctx->type.dataType.IsReference() ) ConvertToVariableNotIn(lctx, rctx);
  10927. if( rctx->type.dataType.IsReference() ) ConvertToVariableNotIn(rctx, lctx);
  10928. // Make sure lctx doesn't end up with a variable used in rctx
  10929. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  10930. {
  10931. int offset = AllocateVariableNotIn(lctx->type.dataType, true, false, rctx);
  10932. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  10933. ReleaseTemporaryVariable(offset, 0);
  10934. }
  10935. // Math operators
  10936. // + - * / % ** += -= *= /= %= **=
  10937. if( op == ttPlus || op == ttAddAssign ||
  10938. op == ttMinus || op == ttSubAssign ||
  10939. op == ttStar || op == ttMulAssign ||
  10940. op == ttSlash || op == ttDivAssign ||
  10941. op == ttPercent || op == ttModAssign ||
  10942. op == ttStarStar || op == ttPowAssign )
  10943. {
  10944. CompileMathOperator(node, lctx, rctx, ctx, op);
  10945. return 0;
  10946. }
  10947. // Bitwise operators
  10948. // << >> >>> & | ^ <<= >>= >>>= &= |= ^=
  10949. if( op == ttAmp || op == ttAndAssign ||
  10950. op == ttBitOr || op == ttOrAssign ||
  10951. op == ttBitXor || op == ttXorAssign ||
  10952. op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  10953. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  10954. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  10955. {
  10956. CompileBitwiseOperator(node, lctx, rctx, ctx, op);
  10957. return 0;
  10958. }
  10959. // Comparison operators
  10960. // == != < > <= >=
  10961. if( op == ttEqual || op == ttNotEqual ||
  10962. op == ttLessThan || op == ttLessThanOrEqual ||
  10963. op == ttGreaterThan || op == ttGreaterThanOrEqual )
  10964. {
  10965. CompileComparisonOperator(node, lctx, rctx, ctx, op);
  10966. return 0;
  10967. }
  10968. // Boolean operators
  10969. // && || ^^
  10970. if( op == ttAnd || op == ttOr || op == ttXor )
  10971. {
  10972. CompileBooleanOperator(node, lctx, rctx, ctx, op);
  10973. return 0;
  10974. }
  10975. }
  10976. asASSERT(false);
  10977. return -1;
  10978. }
  10979. void asCCompiler::ConvertToTempVariableNotIn(asCExprContext *ctx, asCExprContext *exclude)
  10980. {
  10981. int l = int(reservedVariables.GetLength());
  10982. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  10983. ConvertToTempVariable(ctx);
  10984. reservedVariables.SetLength(l);
  10985. }
  10986. void asCCompiler::ConvertToTempVariable(asCExprContext *ctx)
  10987. {
  10988. // This is only used for primitive types and null handles
  10989. asASSERT( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsNullHandle() );
  10990. ConvertToVariable(ctx);
  10991. if( !ctx->type.isTemporary )
  10992. {
  10993. if( ctx->type.dataType.IsPrimitive() )
  10994. {
  10995. // Copy the variable to a temporary variable
  10996. int offset = AllocateVariable(ctx->type.dataType, true);
  10997. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10998. ctx->bc.InstrW_W(asBC_CpyVtoV4, offset, ctx->type.stackOffset);
  10999. else
  11000. ctx->bc.InstrW_W(asBC_CpyVtoV8, offset, ctx->type.stackOffset);
  11001. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  11002. }
  11003. else
  11004. {
  11005. // We should never get here
  11006. asASSERT(false);
  11007. }
  11008. }
  11009. }
  11010. void asCCompiler::ConvertToVariable(asCExprContext *ctx)
  11011. {
  11012. // We should never get here while the context is still an unprocessed property accessor
  11013. asASSERT(ctx->property_get == 0 && ctx->property_set == 0);
  11014. int offset;
  11015. if( !ctx->type.isVariable &&
  11016. (ctx->type.dataType.IsObjectHandle() ||
  11017. (ctx->type.dataType.IsObject() && ctx->type.dataType.SupportHandles())) )
  11018. {
  11019. offset = AllocateVariable(ctx->type.dataType, true);
  11020. if( ctx->type.IsNullConstant() )
  11021. {
  11022. if( ctx->bc.GetLastInstr() == asBC_PshNull )
  11023. ctx->bc.Instr(asBC_PopPtr); // Pop the null constant pushed onto the stack
  11024. ctx->bc.InstrSHORT(asBC_ClrVPtr, (short)offset);
  11025. }
  11026. else
  11027. {
  11028. Dereference(ctx, true);
  11029. // Copy the object handle to a variable
  11030. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  11031. if( ctx->type.dataType.IsFuncdef() )
  11032. ctx->bc.InstrPTR(asBC_REFCPY, &engine->functionBehaviours);
  11033. else
  11034. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  11035. ctx->bc.Instr(asBC_PopPtr);
  11036. }
  11037. // As this is an object the reference must be placed on the stack
  11038. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  11039. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  11040. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  11041. ctx->type.dataType.MakeHandle(true);
  11042. ctx->type.dataType.MakeReference(true);
  11043. }
  11044. else if( (!ctx->type.isVariable || ctx->type.dataType.IsReference()) &&
  11045. ctx->type.dataType.IsPrimitive() )
  11046. {
  11047. if( ctx->type.isConstant )
  11048. {
  11049. offset = AllocateVariable(ctx->type.dataType, true);
  11050. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  11051. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, ctx->type.GetConstantB());
  11052. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  11053. ctx->bc.InstrSHORT_W(asBC_SetV2, (short)offset, ctx->type.GetConstantW());
  11054. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  11055. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, ctx->type.GetConstantDW());
  11056. else
  11057. ctx->bc.InstrSHORT_QW(asBC_SetV8, (short)offset, ctx->type.GetConstantQW());
  11058. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  11059. return;
  11060. }
  11061. else
  11062. {
  11063. asASSERT(ctx->type.dataType.IsPrimitive());
  11064. asASSERT(ctx->type.dataType.IsReference());
  11065. ctx->type.dataType.MakeReference(false);
  11066. offset = AllocateVariable(ctx->type.dataType, true);
  11067. // Read the value from the address in the register directly into the variable
  11068. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  11069. ctx->bc.InstrSHORT(asBC_RDR1, (short)offset);
  11070. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  11071. ctx->bc.InstrSHORT(asBC_RDR2, (short)offset);
  11072. else if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  11073. ctx->bc.InstrSHORT(asBC_RDR4, (short)offset);
  11074. else
  11075. ctx->bc.InstrSHORT(asBC_RDR8, (short)offset);
  11076. }
  11077. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  11078. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  11079. }
  11080. }
  11081. void asCCompiler::ConvertToVariableNotIn(asCExprContext *ctx, asCExprContext *exclude)
  11082. {
  11083. int l = int(reservedVariables.GetLength());
  11084. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  11085. ConvertToVariable(ctx);
  11086. reservedVariables.SetLength(l);
  11087. }
  11088. void asCCompiler::ImplicitConvObjectToBestMathType(asCExprContext *ctx, asCScriptNode *node)
  11089. {
  11090. asCArray<int> funcs;
  11091. asCObjectType *ot = CastToObjectType(ctx->type.dataType.GetTypeInfo());
  11092. if( ot )
  11093. {
  11094. for( unsigned int n = 0; n < ot->methods.GetLength(); n++ )
  11095. {
  11096. // Consider only implicit casts
  11097. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  11098. if( func->name == "opImplConv" &&
  11099. func->returnType.IsPrimitive() &&
  11100. func->parameterTypes.GetLength() == 0 )
  11101. funcs.PushLast(ot->methods[n]);
  11102. }
  11103. // Use the one with the highest precision
  11104. const eTokenType match[10] = {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8};
  11105. while( funcs.GetLength() > 1 )
  11106. {
  11107. eTokenType returnType = builder->GetFunctionDescription(funcs[0])->returnType.GetTokenType();
  11108. int value1 = 11, value2 = 11;
  11109. for( asUINT i = 0; i < 10; i++ )
  11110. {
  11111. if( returnType == match[i] )
  11112. {
  11113. value1 = i;
  11114. break;
  11115. }
  11116. }
  11117. for( asUINT n = 1; n < funcs.GetLength(); n++ )
  11118. {
  11119. returnType = builder->GetFunctionDescription(funcs[n])->returnType.GetTokenType();
  11120. for( asUINT i = 0; i < 10; i++ )
  11121. {
  11122. if( returnType == match[i] )
  11123. {
  11124. value2 = i;
  11125. break;
  11126. }
  11127. }
  11128. if( value2 >= value1 )
  11129. {
  11130. // Remove this and continue searching
  11131. funcs.RemoveIndexUnordered(n--);
  11132. }
  11133. else
  11134. {
  11135. // Remove the first, and start over
  11136. funcs.RemoveIndexUnordered(0);
  11137. break;
  11138. }
  11139. }
  11140. }
  11141. // Do the conversion
  11142. if( funcs.GetLength() )
  11143. ImplicitConvObjectToPrimitive(ctx, builder->GetFunctionDescription(funcs[0])->returnType, node, asIC_IMPLICIT_CONV);
  11144. }
  11145. }
  11146. void asCCompiler::CompileMathOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  11147. {
  11148. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  11149. // TODO: clean up: This initial part is identical to CompileComparisonOperator. Make a common function out of it
  11150. // If either operand is a non-primitive then use the primitive type
  11151. if( !lctx->type.dataType.IsPrimitive() )
  11152. {
  11153. int l = int(reservedVariables.GetLength());
  11154. rctx->bc.GetVarsUsed(reservedVariables);
  11155. ImplicitConvObjectToBestMathType(lctx, node);
  11156. reservedVariables.SetLength(l);
  11157. }
  11158. if( !rctx->type.dataType.IsPrimitive() )
  11159. {
  11160. int l = int(reservedVariables.GetLength());
  11161. lctx->bc.GetVarsUsed(reservedVariables);
  11162. ImplicitConvObjectToBestMathType(rctx, node);
  11163. reservedVariables.SetLength(l);
  11164. }
  11165. // Both types must now be primitives. Implicitly convert them so they match
  11166. asCDataType to;
  11167. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  11168. to.SetTokenType(ttDouble);
  11169. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  11170. to.SetTokenType(ttFloat);
  11171. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  11172. {
  11173. // Convert to int64 if both are signed or if one is non-constant and signed
  11174. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  11175. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  11176. to.SetTokenType(ttInt64);
  11177. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  11178. to.SetTokenType(ttUInt64);
  11179. else
  11180. to.SetTokenType(ttInt64);
  11181. }
  11182. else
  11183. {
  11184. // Convert to int32 if both are signed or if one is non-constant and signed
  11185. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  11186. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  11187. to.SetTokenType(ttInt);
  11188. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  11189. to.SetTokenType(ttUInt);
  11190. else
  11191. to.SetTokenType(ttInt);
  11192. }
  11193. // If doing an operation with double constant and float variable, the constant should be converted to float
  11194. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  11195. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  11196. to.SetTokenType(ttFloat);
  11197. if( op == ttUnrecognizedToken )
  11198. op = node->tokenType;
  11199. // If integer division is disabled, convert to floating-point
  11200. if( engine->ep.disableIntegerDivision &&
  11201. (op == ttSlash || op == ttDivAssign) &&
  11202. (to.IsIntegerType() || to.IsUnsignedType()) )
  11203. {
  11204. // Use double to avoid losing precision when dividing with 32bit ints
  11205. // For 64bit ints there is unfortunately no greater type so with those
  11206. // there is still a risk of loosing precision
  11207. to.SetTokenType(ttDouble);
  11208. }
  11209. // Do the actual conversion
  11210. int l = int(reservedVariables.GetLength());
  11211. rctx->bc.GetVarsUsed(reservedVariables);
  11212. lctx->bc.GetVarsUsed(reservedVariables);
  11213. if( lctx->type.dataType.IsReference() )
  11214. ConvertToVariable(lctx);
  11215. if( rctx->type.dataType.IsReference() )
  11216. ConvertToVariable(rctx);
  11217. if( to.IsPrimitive() )
  11218. {
  11219. // ttStarStar allows an integer, right-hand operand and a double
  11220. // left-hand operand.
  11221. if( (op == ttStarStar || op == ttPowAssign) &&
  11222. lctx->type.dataType.IsDoubleType() &&
  11223. (rctx->type.dataType.IsIntegerType() ||
  11224. rctx->type.dataType.IsUnsignedType()) )
  11225. {
  11226. to.SetTokenType(ttInt);
  11227. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  11228. to.SetTokenType(ttDouble);
  11229. }
  11230. else
  11231. {
  11232. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  11233. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  11234. }
  11235. }
  11236. reservedVariables.SetLength(l);
  11237. // Verify that the conversion was successful
  11238. if( !lctx->type.dataType.IsIntegerType() &&
  11239. !lctx->type.dataType.IsUnsignedType() &&
  11240. !lctx->type.dataType.IsFloatType() &&
  11241. !lctx->type.dataType.IsDoubleType() )
  11242. {
  11243. asCString str;
  11244. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11245. Error(str, node);
  11246. ctx->type.SetDummy();
  11247. return;
  11248. }
  11249. if( !rctx->type.dataType.IsIntegerType() &&
  11250. !rctx->type.dataType.IsUnsignedType() &&
  11251. !rctx->type.dataType.IsFloatType() &&
  11252. !rctx->type.dataType.IsDoubleType() )
  11253. {
  11254. asCString str;
  11255. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11256. Error(str, node);
  11257. ctx->type.SetDummy();
  11258. return;
  11259. }
  11260. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  11261. // Verify if we are dividing with a constant zero
  11262. if( rctx->type.isConstant &&
  11263. (op == ttSlash || op == ttDivAssign ||
  11264. op == ttPercent || op == ttModAssign) &&
  11265. ((rctx->type.dataType.GetSizeInMemoryBytes() == 4 && rctx->type.GetConstantDW() == 0) ||
  11266. (rctx->type.dataType.GetSizeInMemoryBytes() == 8 && rctx->type.GetConstantQW() == 0) ||
  11267. (rctx->type.dataType.GetSizeInMemoryBytes() == 1 && rctx->type.GetConstantB() == 0) ||
  11268. (rctx->type.dataType.GetSizeInMemoryBytes() == 2 && rctx->type.GetConstantW() == 0)) )
  11269. {
  11270. Error(TXT_DIVIDE_BY_ZERO, node);
  11271. }
  11272. if( !isConstant )
  11273. {
  11274. ConvertToVariableNotIn(lctx, rctx);
  11275. ConvertToVariableNotIn(rctx, lctx);
  11276. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  11277. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  11278. if( op == ttAddAssign || op == ttSubAssign ||
  11279. op == ttMulAssign || op == ttDivAssign ||
  11280. op == ttModAssign || op == ttPowAssign )
  11281. {
  11282. // Merge the operands in the different order so that they are evaluated correctly
  11283. MergeExprBytecode(ctx, rctx);
  11284. MergeExprBytecode(ctx, lctx);
  11285. // We must not process the deferred parameters yet, as
  11286. // it may overwrite the lvalue kept in the register
  11287. }
  11288. else
  11289. {
  11290. MergeExprBytecode(ctx, lctx);
  11291. MergeExprBytecode(ctx, rctx);
  11292. ProcessDeferredParams(ctx);
  11293. }
  11294. asEBCInstr instruction = asBC_ADDi;
  11295. if( lctx->type.dataType.IsIntegerType() ||
  11296. lctx->type.dataType.IsUnsignedType() )
  11297. {
  11298. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  11299. {
  11300. if( op == ttPlus || op == ttAddAssign )
  11301. instruction = asBC_ADDi;
  11302. else if( op == ttMinus || op == ttSubAssign )
  11303. instruction = asBC_SUBi;
  11304. else if( op == ttStar || op == ttMulAssign )
  11305. instruction = asBC_MULi;
  11306. else if( op == ttSlash || op == ttDivAssign )
  11307. {
  11308. if( lctx->type.dataType.IsIntegerType() )
  11309. instruction = asBC_DIVi;
  11310. else
  11311. instruction = asBC_DIVu;
  11312. }
  11313. else if( op == ttPercent || op == ttModAssign )
  11314. {
  11315. if( lctx->type.dataType.IsIntegerType() )
  11316. instruction = asBC_MODi;
  11317. else
  11318. instruction = asBC_MODu;
  11319. }
  11320. else if( op == ttStarStar || op == ttPowAssign )
  11321. {
  11322. if( lctx->type.dataType.IsIntegerType() )
  11323. instruction = asBC_POWi;
  11324. else
  11325. instruction = asBC_POWu;
  11326. }
  11327. }
  11328. else
  11329. {
  11330. if( op == ttPlus || op == ttAddAssign )
  11331. instruction = asBC_ADDi64;
  11332. else if( op == ttMinus || op == ttSubAssign )
  11333. instruction = asBC_SUBi64;
  11334. else if( op == ttStar || op == ttMulAssign )
  11335. instruction = asBC_MULi64;
  11336. else if( op == ttSlash || op == ttDivAssign )
  11337. {
  11338. if( lctx->type.dataType.IsIntegerType() )
  11339. instruction = asBC_DIVi64;
  11340. else
  11341. instruction = asBC_DIVu64;
  11342. }
  11343. else if( op == ttPercent || op == ttModAssign )
  11344. {
  11345. if( lctx->type.dataType.IsIntegerType() )
  11346. instruction = asBC_MODi64;
  11347. else
  11348. instruction = asBC_MODu64;
  11349. }
  11350. else if( op == ttStarStar || op == ttPowAssign )
  11351. {
  11352. if( lctx->type.dataType.IsIntegerType() )
  11353. instruction = asBC_POWi64;
  11354. else
  11355. instruction = asBC_POWu64;
  11356. }
  11357. }
  11358. }
  11359. else if( lctx->type.dataType.IsFloatType() )
  11360. {
  11361. if( op == ttPlus || op == ttAddAssign )
  11362. instruction = asBC_ADDf;
  11363. else if( op == ttMinus || op == ttSubAssign )
  11364. instruction = asBC_SUBf;
  11365. else if( op == ttStar || op == ttMulAssign )
  11366. instruction = asBC_MULf;
  11367. else if( op == ttSlash || op == ttDivAssign )
  11368. instruction = asBC_DIVf;
  11369. else if( op == ttPercent || op == ttModAssign )
  11370. instruction = asBC_MODf;
  11371. else if( op == ttStarStar || op == ttPowAssign )
  11372. instruction = asBC_POWf;
  11373. }
  11374. else if( lctx->type.dataType.IsDoubleType() )
  11375. {
  11376. if( rctx->type.dataType.IsIntegerType() )
  11377. {
  11378. asASSERT(rctx->type.dataType.GetSizeInMemoryDWords() == 1);
  11379. if( op == ttStarStar || op == ttPowAssign )
  11380. instruction = asBC_POWdi;
  11381. else
  11382. asASSERT(false); // Should not be possible
  11383. }
  11384. else
  11385. {
  11386. if( op == ttPlus || op == ttAddAssign )
  11387. instruction = asBC_ADDd;
  11388. else if( op == ttMinus || op == ttSubAssign )
  11389. instruction = asBC_SUBd;
  11390. else if( op == ttStar || op == ttMulAssign )
  11391. instruction = asBC_MULd;
  11392. else if( op == ttSlash || op == ttDivAssign )
  11393. instruction = asBC_DIVd;
  11394. else if( op == ttPercent || op == ttModAssign )
  11395. instruction = asBC_MODd;
  11396. else if( op == ttStarStar || op == ttPowAssign )
  11397. instruction = asBC_POWd;
  11398. }
  11399. }
  11400. else
  11401. {
  11402. // Shouldn't be possible
  11403. asASSERT(false);
  11404. }
  11405. // Do the operation
  11406. int a = AllocateVariable(lctx->type.dataType, true);
  11407. int b = lctx->type.stackOffset;
  11408. int c = rctx->type.stackOffset;
  11409. ctx->bc.InstrW_W_W(instruction, a, b, c);
  11410. ctx->type.SetVariable(lctx->type.dataType, a, true);
  11411. }
  11412. else
  11413. {
  11414. // Both values are constants
  11415. if( lctx->type.dataType.IsIntegerType() ||
  11416. lctx->type.dataType.IsUnsignedType() )
  11417. {
  11418. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  11419. {
  11420. int v = 0;
  11421. if( op == ttPlus )
  11422. v = int(lctx->type.GetConstantDW()) + int(rctx->type.GetConstantDW());
  11423. else if( op == ttMinus )
  11424. v = int(lctx->type.GetConstantDW()) - int(rctx->type.GetConstantDW());
  11425. else if( op == ttStar )
  11426. v = int(lctx->type.GetConstantDW()) * int(rctx->type.GetConstantDW());
  11427. else if( op == ttSlash )
  11428. {
  11429. // TODO: Should probably report an error, rather than silently convert the value to 0
  11430. if( rctx->type.GetConstantDW() == 0 || (int(rctx->type.GetConstantDW()) == -1 && lctx->type.GetConstantDW() == 0x80000000) )
  11431. v = 0;
  11432. else
  11433. if( lctx->type.dataType.IsIntegerType() )
  11434. v = int(lctx->type.GetConstantDW()) / int(rctx->type.GetConstantDW());
  11435. else
  11436. v = lctx->type.GetConstantDW() / rctx->type.GetConstantDW();
  11437. }
  11438. else if( op == ttPercent )
  11439. {
  11440. // TODO: Should probably report an error, rather than silently convert the value to 0
  11441. if( rctx->type.GetConstantDW() == 0 || (int(rctx->type.GetConstantDW()) == -1 && lctx->type.GetConstantDW() == 0x80000000) )
  11442. v = 0;
  11443. else
  11444. if( lctx->type.dataType.IsIntegerType() )
  11445. v = int(lctx->type.GetConstantDW()) % int(rctx->type.GetConstantDW());
  11446. else
  11447. v = lctx->type.GetConstantDW() % rctx->type.GetConstantDW();
  11448. }
  11449. else if( op == ttStarStar )
  11450. {
  11451. bool isOverflow;
  11452. if( lctx->type.dataType.IsIntegerType() )
  11453. v = as_powi(int(lctx->type.GetConstantDW()), int(rctx->type.GetConstantDW()), isOverflow);
  11454. else
  11455. v = as_powu(lctx->type.GetConstantDW(), rctx->type.GetConstantDW(), isOverflow);
  11456. if( isOverflow )
  11457. Error(TXT_POW_OVERFLOW, node);
  11458. }
  11459. ctx->type.SetConstantDW(lctx->type.dataType, v);
  11460. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  11461. if( lctx->type.dataType.GetTokenType() == ttUInt && op == ttMinus && lctx->type.GetConstantDW() < rctx->type.GetConstantDW())
  11462. ctx->type.dataType.SetTokenType(ttInt);
  11463. }
  11464. else
  11465. {
  11466. asQWORD v = 0;
  11467. if( op == ttPlus )
  11468. v = asINT64(lctx->type.GetConstantQW()) + asINT64(rctx->type.GetConstantQW());
  11469. else if( op == ttMinus )
  11470. v = asINT64(lctx->type.GetConstantQW()) - asINT64(rctx->type.GetConstantQW());
  11471. else if( op == ttStar )
  11472. v = asINT64(lctx->type.GetConstantQW()) * asINT64(rctx->type.GetConstantQW());
  11473. else if( op == ttSlash )
  11474. {
  11475. // TODO: Should probably report an error, rather than silently convert the value to 0
  11476. if( rctx->type.GetConstantQW() == 0 || (rctx->type.GetConstantQW() == asQWORD(-1) && lctx->type.GetConstantQW() == (asQWORD(1)<<63)) )
  11477. v = 0;
  11478. else
  11479. if( lctx->type.dataType.IsIntegerType() )
  11480. v = asINT64(lctx->type.GetConstantQW()) / asINT64(rctx->type.GetConstantQW());
  11481. else
  11482. v = lctx->type.GetConstantQW() / rctx->type.GetConstantQW();
  11483. }
  11484. else if( op == ttPercent )
  11485. {
  11486. // TODO: Should probably report an error, rather than silently convert the value to 0
  11487. if( rctx->type.GetConstantQW() == 0 || (rctx->type.GetConstantQW() == asQWORD(-1) && lctx->type.GetConstantQW() == (asQWORD(1)<<63)) )
  11488. v = 0;
  11489. else
  11490. if( lctx->type.dataType.IsIntegerType() )
  11491. v = asINT64(lctx->type.GetConstantQW()) % asINT64(rctx->type.GetConstantQW());
  11492. else
  11493. v = lctx->type.GetConstantQW() % rctx->type.GetConstantQW();
  11494. }
  11495. else if( op == ttStarStar )
  11496. {
  11497. bool isOverflow;
  11498. if( lctx->type.dataType.IsIntegerType() )
  11499. v = as_powi64(asINT64(lctx->type.GetConstantQW()), asINT64(rctx->type.GetConstantQW()), isOverflow);
  11500. else
  11501. v = as_powu64(lctx->type.GetConstantQW(), rctx->type.GetConstantQW(), isOverflow);
  11502. if( isOverflow )
  11503. Error(TXT_POW_OVERFLOW, node);
  11504. }
  11505. ctx->type.SetConstantQW(lctx->type.dataType, v);
  11506. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  11507. if( lctx->type.dataType.GetTokenType() == ttUInt64 && op == ttMinus && lctx->type.GetConstantQW() < rctx->type.GetConstantQW())
  11508. ctx->type.dataType.SetTokenType(ttInt64);
  11509. }
  11510. }
  11511. else if( lctx->type.dataType.IsFloatType() )
  11512. {
  11513. float v = 0.0f;
  11514. if( op == ttPlus )
  11515. v = lctx->type.GetConstantF() + rctx->type.GetConstantF();
  11516. else if( op == ttMinus )
  11517. v = lctx->type.GetConstantF() - rctx->type.GetConstantF();
  11518. else if( op == ttStar )
  11519. v = lctx->type.GetConstantF() * rctx->type.GetConstantF();
  11520. else if( op == ttSlash )
  11521. {
  11522. if( rctx->type.GetConstantF() == 0 )
  11523. v = 0;
  11524. else
  11525. v = lctx->type.GetConstantF() / rctx->type.GetConstantF();
  11526. }
  11527. else if( op == ttPercent )
  11528. {
  11529. if( rctx->type.GetConstantF() == 0 )
  11530. v = 0;
  11531. else
  11532. v = fmodf(lctx->type.GetConstantF(), rctx->type.GetConstantF());
  11533. }
  11534. else if( op == ttStarStar )
  11535. {
  11536. v = powf(lctx->type.GetConstantF(), rctx->type.GetConstantF());
  11537. if( v == HUGE_VAL )
  11538. Error(TXT_POW_OVERFLOW, node);
  11539. }
  11540. ctx->type.SetConstantF(lctx->type.dataType, v);
  11541. }
  11542. else if( lctx->type.dataType.IsDoubleType() )
  11543. {
  11544. double v = 0.0;
  11545. if( rctx->type.dataType.IsIntegerType() )
  11546. {
  11547. asASSERT(rctx->type.dataType.GetSizeInMemoryDWords() == 1);
  11548. if( op == ttStarStar || op == ttPowAssign )
  11549. {
  11550. v = pow(lctx->type.GetConstantD(), int(rctx->type.GetConstantDW()));
  11551. if( v == HUGE_VAL )
  11552. Error(TXT_POW_OVERFLOW, node);
  11553. }
  11554. else
  11555. asASSERT(false); // Should not be possible
  11556. }
  11557. else
  11558. {
  11559. if( op == ttPlus )
  11560. v = lctx->type.GetConstantD() + rctx->type.GetConstantD();
  11561. else if( op == ttMinus )
  11562. v = lctx->type.GetConstantD() - rctx->type.GetConstantD();
  11563. else if( op == ttStar )
  11564. v = lctx->type.GetConstantD() * rctx->type.GetConstantD();
  11565. else if( op == ttSlash )
  11566. {
  11567. if( rctx->type.GetConstantD() == 0 )
  11568. v = 0;
  11569. else
  11570. v = lctx->type.GetConstantD() / rctx->type.GetConstantD();
  11571. }
  11572. else if( op == ttPercent )
  11573. {
  11574. if( rctx->type.GetConstantD() == 0 )
  11575. v = 0;
  11576. else
  11577. v = fmod(lctx->type.GetConstantD(), rctx->type.GetConstantD());
  11578. }
  11579. else if( op == ttStarStar )
  11580. {
  11581. v = pow(lctx->type.GetConstantD(), rctx->type.GetConstantD());
  11582. if( v == HUGE_VAL )
  11583. Error(TXT_POW_OVERFLOW, node);
  11584. }
  11585. }
  11586. ctx->type.SetConstantD(lctx->type.dataType, v);
  11587. }
  11588. else
  11589. {
  11590. // Shouldn't be possible
  11591. asASSERT(false);
  11592. }
  11593. }
  11594. }
  11595. void asCCompiler::CompileBitwiseOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  11596. {
  11597. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  11598. if( op == ttUnrecognizedToken )
  11599. op = node->tokenType;
  11600. if( op == ttAmp || op == ttAndAssign ||
  11601. op == ttBitOr || op == ttOrAssign ||
  11602. op == ttBitXor || op == ttXorAssign )
  11603. {
  11604. // Convert left hand operand to integer if it's not already one
  11605. asCDataType to;
  11606. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 ||
  11607. rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  11608. to.SetTokenType(ttInt64);
  11609. else
  11610. to.SetTokenType(ttInt);
  11611. // Do the actual conversion (keep sign/unsigned if possible)
  11612. int l = int(reservedVariables.GetLength());
  11613. rctx->bc.GetVarsUsed(reservedVariables);
  11614. if( lctx->type.dataType.IsUnsignedType() )
  11615. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttUInt : ttUInt64 );
  11616. else
  11617. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttInt : ttInt64 );
  11618. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  11619. reservedVariables.SetLength(l);
  11620. // Verify that the conversion was successful
  11621. if( lctx->type.dataType != to )
  11622. {
  11623. asCString str;
  11624. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  11625. Error(str, node);
  11626. }
  11627. // Convert right hand operand to same size as left hand
  11628. l = int(reservedVariables.GetLength());
  11629. lctx->bc.GetVarsUsed(reservedVariables);
  11630. if( rctx->type.dataType.IsUnsignedType() )
  11631. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttUInt : ttUInt64 );
  11632. else
  11633. to.SetTokenType( to.GetSizeOnStackDWords() == 1 ? ttInt : ttInt64 );
  11634. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  11635. reservedVariables.SetLength(l);
  11636. if( rctx->type.dataType != to )
  11637. {
  11638. asCString str;
  11639. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11640. Error(str, node);
  11641. }
  11642. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  11643. if( !isConstant )
  11644. {
  11645. ConvertToVariableNotIn(lctx, rctx);
  11646. ConvertToVariableNotIn(rctx, lctx);
  11647. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  11648. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  11649. if( op == ttAndAssign || op == ttOrAssign || op == ttXorAssign )
  11650. {
  11651. // Compound assignments execute the right hand value first
  11652. MergeExprBytecode(ctx, rctx);
  11653. MergeExprBytecode(ctx, lctx);
  11654. }
  11655. else
  11656. {
  11657. MergeExprBytecode(ctx, lctx);
  11658. MergeExprBytecode(ctx, rctx);
  11659. }
  11660. ProcessDeferredParams(ctx);
  11661. asEBCInstr instruction = asBC_BAND;
  11662. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  11663. {
  11664. if( op == ttAmp || op == ttAndAssign )
  11665. instruction = asBC_BAND;
  11666. else if( op == ttBitOr || op == ttOrAssign )
  11667. instruction = asBC_BOR;
  11668. else if( op == ttBitXor || op == ttXorAssign )
  11669. instruction = asBC_BXOR;
  11670. }
  11671. else
  11672. {
  11673. if( op == ttAmp || op == ttAndAssign )
  11674. instruction = asBC_BAND64;
  11675. else if( op == ttBitOr || op == ttOrAssign )
  11676. instruction = asBC_BOR64;
  11677. else if( op == ttBitXor || op == ttXorAssign )
  11678. instruction = asBC_BXOR64;
  11679. }
  11680. // Do the operation
  11681. int a = AllocateVariable(lctx->type.dataType, true);
  11682. int b = lctx->type.stackOffset;
  11683. int c = rctx->type.stackOffset;
  11684. ctx->bc.InstrW_W_W(instruction, a, b, c);
  11685. ctx->type.SetVariable(lctx->type.dataType, a, true);
  11686. }
  11687. else
  11688. {
  11689. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  11690. {
  11691. asQWORD v = 0;
  11692. if( op == ttAmp )
  11693. v = lctx->type.GetConstantQW() & rctx->type.GetConstantQW();
  11694. else if( op == ttBitOr )
  11695. v = lctx->type.GetConstantQW() | rctx->type.GetConstantQW();
  11696. else if( op == ttBitXor )
  11697. v = lctx->type.GetConstantQW() ^ rctx->type.GetConstantQW();
  11698. // Remember the result
  11699. ctx->type.SetConstantQW(lctx->type.dataType, v);
  11700. }
  11701. else
  11702. {
  11703. asDWORD v = 0;
  11704. if( op == ttAmp )
  11705. v = lctx->type.GetConstantDW() & rctx->type.GetConstantDW();
  11706. else if( op == ttBitOr )
  11707. v = lctx->type.GetConstantDW() | rctx->type.GetConstantDW();
  11708. else if( op == ttBitXor )
  11709. v = lctx->type.GetConstantDW() ^ rctx->type.GetConstantDW();
  11710. // Remember the result
  11711. ctx->type.SetConstantDW(lctx->type.dataType, v);
  11712. }
  11713. }
  11714. }
  11715. else if( op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  11716. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  11717. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  11718. {
  11719. // Don't permit object to primitive conversion, since we don't know which integer type is the correct one
  11720. if( lctx->type.dataType.IsObject() )
  11721. {
  11722. asCString str;
  11723. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
  11724. Error(str, node);
  11725. // Set an integer value and allow the compiler to continue
  11726. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  11727. return;
  11728. }
  11729. // Convert left hand operand to integer if it's not already one
  11730. asCDataType to = lctx->type.dataType;
  11731. if( lctx->type.dataType.IsUnsignedType() &&
  11732. lctx->type.dataType.GetSizeInMemoryBytes() < 4 )
  11733. {
  11734. // Upgrade to 32bit
  11735. to = asCDataType::CreatePrimitive(ttUInt, false);
  11736. }
  11737. else if( !lctx->type.dataType.IsUnsignedType() )
  11738. {
  11739. if (lctx->type.dataType.GetSizeInMemoryDWords() == 2)
  11740. to = asCDataType::CreatePrimitive(ttInt64, false);
  11741. else
  11742. to = asCDataType::CreatePrimitive(ttInt, false);
  11743. }
  11744. // Do the actual conversion
  11745. int l = int(reservedVariables.GetLength());
  11746. rctx->bc.GetVarsUsed(reservedVariables);
  11747. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  11748. reservedVariables.SetLength(l);
  11749. // Verify that the conversion was successful
  11750. if( lctx->type.dataType != to )
  11751. {
  11752. asCString str;
  11753. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  11754. Error(str, node);
  11755. }
  11756. // Right operand must be 32bit uint
  11757. l = int(reservedVariables.GetLength());
  11758. lctx->bc.GetVarsUsed(reservedVariables);
  11759. ImplicitConversion(rctx, asCDataType::CreatePrimitive(ttUInt, true), node, asIC_IMPLICIT_CONV, true);
  11760. reservedVariables.SetLength(l);
  11761. if( !rctx->type.dataType.IsUnsignedType() )
  11762. {
  11763. asCString str;
  11764. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), "uint");
  11765. Error(str, node);
  11766. }
  11767. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  11768. if( !isConstant )
  11769. {
  11770. ConvertToVariableNotIn(lctx, rctx);
  11771. ConvertToVariableNotIn(rctx, lctx);
  11772. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  11773. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  11774. if( op == ttShiftLeftAssign || op == ttShiftRightLAssign || op == ttShiftRightAAssign )
  11775. {
  11776. // Compound assignments execute the right hand value first
  11777. MergeExprBytecode(ctx, rctx);
  11778. MergeExprBytecode(ctx, lctx);
  11779. }
  11780. else
  11781. {
  11782. MergeExprBytecode(ctx, lctx);
  11783. MergeExprBytecode(ctx, rctx);
  11784. }
  11785. ProcessDeferredParams(ctx);
  11786. asEBCInstr instruction = asBC_BSLL;
  11787. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  11788. {
  11789. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  11790. instruction = asBC_BSLL;
  11791. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  11792. instruction = asBC_BSRL;
  11793. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  11794. instruction = asBC_BSRA;
  11795. }
  11796. else
  11797. {
  11798. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  11799. instruction = asBC_BSLL64;
  11800. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  11801. instruction = asBC_BSRL64;
  11802. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  11803. instruction = asBC_BSRA64;
  11804. }
  11805. // Do the operation
  11806. int a = AllocateVariable(lctx->type.dataType, true);
  11807. int b = lctx->type.stackOffset;
  11808. int c = rctx->type.stackOffset;
  11809. ctx->bc.InstrW_W_W(instruction, a, b, c);
  11810. ctx->type.SetVariable(lctx->type.dataType, a, true);
  11811. }
  11812. else
  11813. {
  11814. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  11815. {
  11816. asDWORD v = 0;
  11817. if( op == ttBitShiftLeft )
  11818. v = lctx->type.GetConstantDW() << rctx->type.GetConstantDW();
  11819. else if( op == ttBitShiftRight )
  11820. v = lctx->type.GetConstantDW() >> rctx->type.GetConstantDW();
  11821. else if( op == ttBitShiftRightArith )
  11822. v = int(lctx->type.GetConstantDW()) >> rctx->type.GetConstantDW();
  11823. ctx->type.SetConstantDW(lctx->type.dataType, v);
  11824. }
  11825. else
  11826. {
  11827. asQWORD v = 0;
  11828. if( op == ttBitShiftLeft )
  11829. v = lctx->type.GetConstantQW() << rctx->type.GetConstantDW();
  11830. else if( op == ttBitShiftRight )
  11831. v = lctx->type.GetConstantQW() >> rctx->type.GetConstantDW();
  11832. else if( op == ttBitShiftRightArith )
  11833. v = asINT64(lctx->type.GetConstantQW()) >> rctx->type.GetConstantDW();
  11834. ctx->type.SetConstantQW(lctx->type.dataType, v);
  11835. }
  11836. }
  11837. }
  11838. }
  11839. void asCCompiler::CompileComparisonOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  11840. {
  11841. // Both operands must be of the same type
  11842. // If either operand is a non-primitive then first convert them to the best number type
  11843. if( !lctx->type.dataType.IsPrimitive() )
  11844. {
  11845. int l = int(reservedVariables.GetLength());
  11846. rctx->bc.GetVarsUsed(reservedVariables);
  11847. ImplicitConvObjectToBestMathType(lctx, node);
  11848. reservedVariables.SetLength(l);
  11849. }
  11850. if( !rctx->type.dataType.IsPrimitive() )
  11851. {
  11852. int l = int(reservedVariables.GetLength());
  11853. lctx->bc.GetVarsUsed(reservedVariables);
  11854. ImplicitConvObjectToBestMathType(rctx, node);
  11855. reservedVariables.SetLength(l);
  11856. }
  11857. // Implicitly convert the operands to matching types
  11858. asCDataType to;
  11859. if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  11860. to.SetTokenType(ttDouble);
  11861. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  11862. to.SetTokenType(ttFloat);
  11863. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  11864. {
  11865. // Convert to int64 if both are signed or if one is non-constant and signed
  11866. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  11867. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  11868. to.SetTokenType(ttInt64);
  11869. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  11870. to.SetTokenType(ttUInt64);
  11871. else
  11872. to.SetTokenType(ttInt64);
  11873. }
  11874. else
  11875. {
  11876. // Convert to int32 if both are signed or if one is non-constant and signed
  11877. if( (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  11878. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  11879. to.SetTokenType(ttInt);
  11880. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  11881. to.SetTokenType(ttUInt);
  11882. else if( lctx->type.dataType.IsBooleanType() || rctx->type.dataType.IsBooleanType() )
  11883. to.SetTokenType(ttBool);
  11884. else
  11885. to.SetTokenType(ttInt);
  11886. }
  11887. // If doing an operation with double constant and float variable, the constant should be converted to float
  11888. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  11889. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  11890. to.SetTokenType(ttFloat);
  11891. asASSERT( to.GetTokenType() != ttUnrecognizedToken );
  11892. // Do we have a mismatch between the sign of the operand?
  11893. bool signMismatch = false;
  11894. for( int n = 0; !signMismatch && n < 2; n++ )
  11895. {
  11896. asCExprContext *opCtx = n ? rctx : lctx;
  11897. if( opCtx->type.dataType.IsUnsignedType() != to.IsUnsignedType() )
  11898. {
  11899. // We have a mismatch, unless the value is a literal constant and the conversion won't affect its value
  11900. signMismatch = true;
  11901. if( opCtx->type.isConstant )
  11902. {
  11903. if( opCtx->type.dataType.GetTokenType() == ttUInt64 || opCtx->type.dataType.GetTokenType() == ttInt64 )
  11904. {
  11905. if( !(opCtx->type.GetConstantQW() & (asQWORD(1)<<63)) )
  11906. signMismatch = false;
  11907. }
  11908. else
  11909. {
  11910. if( !(opCtx->type.GetConstantDW() & (1<<31)) )
  11911. signMismatch = false;
  11912. }
  11913. // It's not necessary to check for floats or double, because if
  11914. // it was then the types for the conversion will never be unsigned
  11915. }
  11916. }
  11917. }
  11918. // Check for signed/unsigned mismatch
  11919. if( signMismatch )
  11920. Warning(TXT_SIGNED_UNSIGNED_MISMATCH, node);
  11921. // Attempt to resolve ambiguous enumerations
  11922. if( lctx->type.dataType.IsEnumType() && rctx->enumValue != "" )
  11923. ImplicitConversion(rctx, lctx->type.dataType, node, asIC_IMPLICIT_CONV);
  11924. else if( rctx->type.dataType.IsEnumType() && lctx->enumValue != "" )
  11925. ImplicitConversion(lctx, rctx->type.dataType, node, asIC_IMPLICIT_CONV);
  11926. // Do the actual conversion
  11927. int l = int(reservedVariables.GetLength());
  11928. rctx->bc.GetVarsUsed(reservedVariables);
  11929. if( lctx->type.dataType.IsReference() )
  11930. ConvertToVariable(lctx);
  11931. if( rctx->type.dataType.IsReference() )
  11932. ConvertToVariable(rctx);
  11933. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  11934. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  11935. reservedVariables.SetLength(l);
  11936. // Verify that the conversion was successful
  11937. bool ok = true;
  11938. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  11939. {
  11940. asCString str;
  11941. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  11942. Error(str, node);
  11943. ok = false;
  11944. }
  11945. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  11946. {
  11947. asCString str;
  11948. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  11949. Error(str, node);
  11950. ok = false;
  11951. }
  11952. if( !ok )
  11953. {
  11954. // It wasn't possible to get two valid operands, so we just return
  11955. // a boolean result and let the compiler continue.
  11956. #if AS_SIZEOF_BOOL == 1
  11957. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  11958. #else
  11959. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  11960. #endif
  11961. return;
  11962. }
  11963. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  11964. if( op == ttUnrecognizedToken )
  11965. op = node->tokenType;
  11966. if( !isConstant )
  11967. {
  11968. if( to.IsBooleanType() )
  11969. {
  11970. if( op == ttEqual || op == ttNotEqual )
  11971. {
  11972. // Must convert to temporary variable, because we are changing the value before comparison
  11973. ConvertToTempVariableNotIn(lctx, rctx);
  11974. ConvertToTempVariableNotIn(rctx, lctx);
  11975. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  11976. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  11977. // Make sure they are equal if not false
  11978. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  11979. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  11980. MergeExprBytecode(ctx, lctx);
  11981. MergeExprBytecode(ctx, rctx);
  11982. ProcessDeferredParams(ctx);
  11983. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  11984. int b = lctx->type.stackOffset;
  11985. int c = rctx->type.stackOffset;
  11986. if( op == ttEqual )
  11987. {
  11988. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  11989. ctx->bc.Instr(asBC_TZ);
  11990. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  11991. }
  11992. else if( op == ttNotEqual )
  11993. {
  11994. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  11995. ctx->bc.Instr(asBC_TNZ);
  11996. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  11997. }
  11998. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  11999. }
  12000. else
  12001. {
  12002. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  12003. Error(TXT_ILLEGAL_OPERATION, node);
  12004. #if AS_SIZEOF_BOOL == 1
  12005. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), 0);
  12006. #else
  12007. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 0);
  12008. #endif
  12009. }
  12010. }
  12011. else
  12012. {
  12013. ConvertToVariableNotIn(lctx, rctx);
  12014. ConvertToVariableNotIn(rctx, lctx);
  12015. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  12016. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  12017. MergeExprBytecode(ctx, lctx);
  12018. MergeExprBytecode(ctx, rctx);
  12019. ProcessDeferredParams(ctx);
  12020. asEBCInstr iCmp = asBC_CMPi, iT = asBC_TZ;
  12021. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12022. iCmp = asBC_CMPi;
  12023. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12024. iCmp = asBC_CMPu;
  12025. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12026. iCmp = asBC_CMPi64;
  12027. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12028. iCmp = asBC_CMPu64;
  12029. else if( lctx->type.dataType.IsFloatType() )
  12030. iCmp = asBC_CMPf;
  12031. else if( lctx->type.dataType.IsDoubleType() )
  12032. iCmp = asBC_CMPd;
  12033. else
  12034. asASSERT(false);
  12035. if( op == ttEqual )
  12036. iT = asBC_TZ;
  12037. else if( op == ttNotEqual )
  12038. iT = asBC_TNZ;
  12039. else if( op == ttLessThan )
  12040. iT = asBC_TS;
  12041. else if( op == ttLessThanOrEqual )
  12042. iT = asBC_TNP;
  12043. else if( op == ttGreaterThan )
  12044. iT = asBC_TP;
  12045. else if( op == ttGreaterThanOrEqual )
  12046. iT = asBC_TNS;
  12047. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  12048. int b = lctx->type.stackOffset;
  12049. int c = rctx->type.stackOffset;
  12050. ctx->bc.InstrW_W(iCmp, b, c);
  12051. ctx->bc.Instr(iT);
  12052. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  12053. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  12054. }
  12055. }
  12056. else
  12057. {
  12058. if( to.IsBooleanType() )
  12059. {
  12060. if( op == ttEqual || op == ttNotEqual )
  12061. {
  12062. asDWORD lv, rv;
  12063. #if AS_SIZEOF_BOOL == 1
  12064. lv = lctx->type.GetConstantB();
  12065. rv = rctx->type.GetConstantB();
  12066. #else
  12067. lv = lctx->type.GetConstantDW();
  12068. rv = rctx->type.GetConstantDW();
  12069. #endif
  12070. // Make sure they are equal if not false
  12071. if (lv != 0) lv = VALUE_OF_BOOLEAN_TRUE;
  12072. if (rv != 0) rv = VALUE_OF_BOOLEAN_TRUE;
  12073. asDWORD v = 0;
  12074. if (op == ttEqual)
  12075. v = (lv == rv) ? VALUE_OF_BOOLEAN_TRUE : 0;
  12076. else if (op == ttNotEqual)
  12077. v = (lv != rv) ? VALUE_OF_BOOLEAN_TRUE : 0;
  12078. #if AS_SIZEOF_BOOL == 1
  12079. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), (asBYTE)v);
  12080. #else
  12081. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), v);
  12082. #endif
  12083. }
  12084. else
  12085. {
  12086. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  12087. Error(TXT_ILLEGAL_OPERATION, node);
  12088. }
  12089. }
  12090. else
  12091. {
  12092. int i = 0;
  12093. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12094. {
  12095. int v = int(lctx->type.GetConstantDW()) - int(rctx->type.GetConstantDW());
  12096. if( v < 0 ) i = -1;
  12097. if( v > 0 ) i = 1;
  12098. }
  12099. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12100. {
  12101. asDWORD v1 = lctx->type.GetConstantDW();
  12102. asDWORD v2 = rctx->type.GetConstantDW();
  12103. if( v1 < v2 ) i = -1;
  12104. if( v1 > v2 ) i = 1;
  12105. }
  12106. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12107. {
  12108. asINT64 v = asINT64(lctx->type.GetConstantQW()) - asINT64(rctx->type.GetConstantQW());
  12109. if( v < 0 ) i = -1;
  12110. if( v > 0 ) i = 1;
  12111. }
  12112. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  12113. {
  12114. asQWORD v1 = lctx->type.GetConstantQW();
  12115. asQWORD v2 = rctx->type.GetConstantQW();
  12116. if( v1 < v2 ) i = -1;
  12117. if( v1 > v2 ) i = 1;
  12118. }
  12119. else if( lctx->type.dataType.IsFloatType() )
  12120. {
  12121. float v = lctx->type.GetConstantF() - rctx->type.GetConstantF();
  12122. if( v < 0 ) i = -1;
  12123. if( v > 0 ) i = 1;
  12124. }
  12125. else if( lctx->type.dataType.IsDoubleType() )
  12126. {
  12127. double v = lctx->type.GetConstantD() - rctx->type.GetConstantD();
  12128. if( v < 0 ) i = -1;
  12129. if( v > 0 ) i = 1;
  12130. }
  12131. if( op == ttEqual )
  12132. i = (i == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  12133. else if( op == ttNotEqual )
  12134. i = (i != 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  12135. else if( op == ttLessThan )
  12136. i = (i < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  12137. else if( op == ttLessThanOrEqual )
  12138. i = (i <= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  12139. else if( op == ttGreaterThan )
  12140. i = (i > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  12141. else if( op == ttGreaterThanOrEqual )
  12142. i = (i >= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  12143. #if AS_SIZEOF_BOOL == 1
  12144. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), (asBYTE)i);
  12145. #else
  12146. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), i);
  12147. #endif
  12148. }
  12149. }
  12150. }
  12151. void asCCompiler::PushVariableOnStack(asCExprContext *ctx, bool asReference)
  12152. {
  12153. // Put the result on the stack
  12154. if( asReference )
  12155. {
  12156. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  12157. ctx->type.dataType.MakeReference(true);
  12158. }
  12159. else
  12160. {
  12161. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  12162. ctx->bc.InstrSHORT(asBC_PshV4, ctx->type.stackOffset);
  12163. else
  12164. ctx->bc.InstrSHORT(asBC_PshV8, ctx->type.stackOffset);
  12165. }
  12166. }
  12167. void asCCompiler::CompileBooleanOperator(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType op)
  12168. {
  12169. // Both operands must be booleans
  12170. asCDataType to;
  12171. to.SetTokenType(ttBool);
  12172. // Do the actual conversion
  12173. int l = int(reservedVariables.GetLength());
  12174. rctx->bc.GetVarsUsed(reservedVariables);
  12175. lctx->bc.GetVarsUsed(reservedVariables);
  12176. // Allow value types to be converted to bool using 'bool opImplConv()'
  12177. if( lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  12178. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  12179. if( rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_VALUE) )
  12180. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  12181. reservedVariables.SetLength(l);
  12182. // Verify that the conversion was successful
  12183. if( !lctx->type.dataType.IsBooleanType() )
  12184. {
  12185. asCString str;
  12186. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), "bool");
  12187. Error(str, node);
  12188. // Force the conversion to allow compilation to proceed
  12189. lctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  12190. }
  12191. if( !rctx->type.dataType.IsBooleanType() )
  12192. {
  12193. asCString str;
  12194. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), "bool");
  12195. Error(str, node);
  12196. // Force the conversion to allow compilation to proceed
  12197. rctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  12198. }
  12199. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  12200. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  12201. // What kind of operator is it?
  12202. if( op == ttUnrecognizedToken )
  12203. op = node->tokenType;
  12204. if( op == ttXor )
  12205. {
  12206. if( !isConstant )
  12207. {
  12208. // Must convert to temporary variable, because we are changing the value before comparison
  12209. ConvertToTempVariableNotIn(lctx, rctx);
  12210. ConvertToTempVariableNotIn(rctx, lctx);
  12211. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  12212. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  12213. // Make sure they are equal if not false
  12214. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  12215. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  12216. MergeExprBytecode(ctx, lctx);
  12217. MergeExprBytecode(ctx, rctx);
  12218. ProcessDeferredParams(ctx);
  12219. int a = AllocateVariable(ctx->type.dataType, true);
  12220. int b = lctx->type.stackOffset;
  12221. int c = rctx->type.stackOffset;
  12222. ctx->bc.InstrW_W_W(asBC_BXOR,a,b,c);
  12223. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  12224. }
  12225. else
  12226. {
  12227. // Make sure they are equal if not false
  12228. #if AS_SIZEOF_BOOL == 1
  12229. if( lctx->type.GetConstantB() != 0 ) lctx->type.SetConstantB(VALUE_OF_BOOLEAN_TRUE);
  12230. if( rctx->type.GetConstantB() != 0 ) rctx->type.SetConstantB(VALUE_OF_BOOLEAN_TRUE);
  12231. asBYTE v = 0;
  12232. v = lctx->type.GetConstantB() - rctx->type.GetConstantB();
  12233. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  12234. ctx->type.isConstant = true;
  12235. ctx->type.SetConstantB(v);
  12236. #else
  12237. if( lctx->type.GetConstantDW() != 0 ) lctx->type.SetConstantDW(VALUE_OF_BOOLEAN_TRUE);
  12238. if( rctx->type.GetConstantDW() != 0 ) rctx->type.GetConstantDW(VALUE_OF_BOOLEAN_TRUE);
  12239. asDWORD v = 0;
  12240. v = lctx->type.GetConstantDW() - rctx->type.GetConstantDW();
  12241. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  12242. ctx->type.isConstant = true;
  12243. ctx->type.SetConstantDW(v);
  12244. #endif
  12245. }
  12246. }
  12247. else if( op == ttAnd ||
  12248. op == ttOr )
  12249. {
  12250. if( !isConstant )
  12251. {
  12252. // If or-operator and first value is 1 the second value shouldn't be calculated
  12253. // if and-operator and first value is 0 the second value shouldn't be calculated
  12254. ConvertToVariable(lctx);
  12255. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  12256. MergeExprBytecode(ctx, lctx);
  12257. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  12258. int label1 = nextLabel++;
  12259. int label2 = nextLabel++;
  12260. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  12261. ctx->bc.Instr(asBC_ClrHi);
  12262. if( op == ttAnd )
  12263. {
  12264. ctx->bc.InstrDWORD(asBC_JNZ, label1);
  12265. ctx->bc.InstrW_DW(asBC_SetV4, (asWORD)offset, 0);
  12266. ctx->bc.InstrINT(asBC_JMP, label2);
  12267. }
  12268. else if( op == ttOr )
  12269. {
  12270. ctx->bc.InstrDWORD(asBC_JZ, label1);
  12271. #if AS_SIZEOF_BOOL == 1
  12272. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  12273. #else
  12274. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  12275. #endif
  12276. ctx->bc.InstrINT(asBC_JMP, label2);
  12277. }
  12278. ctx->bc.Label((short)label1);
  12279. ConvertToVariable(rctx);
  12280. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  12281. rctx->bc.InstrW_W(asBC_CpyVtoV4, offset, rctx->type.stackOffset);
  12282. MergeExprBytecode(ctx, rctx);
  12283. ctx->bc.Label((short)label2);
  12284. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), offset, true);
  12285. }
  12286. else
  12287. {
  12288. #if AS_SIZEOF_BOOL == 1
  12289. asBYTE v = 0;
  12290. if( op == ttAnd )
  12291. v = lctx->type.GetConstantB() && rctx->type.GetConstantB();
  12292. else if( op == ttOr )
  12293. v = lctx->type.GetConstantB() || rctx->type.GetConstantB();
  12294. // Remember the result
  12295. ctx->type.isConstant = true;
  12296. ctx->type.SetConstantB(v);
  12297. #else
  12298. asDWORD v = 0;
  12299. if( op == ttAnd )
  12300. v = lctx->type.GetConstantDW() && rctx->type.GetConstantDW();
  12301. else if( op == ttOr )
  12302. v = lctx->type.GetConstantDW() || rctx->type.GetConstantDW();
  12303. // Remember the result
  12304. ctx->type.isConstant = true;
  12305. ctx->type.SetConstantDW(v);
  12306. #endif
  12307. }
  12308. }
  12309. }
  12310. void asCCompiler::CompileOperatorOnHandles(asCScriptNode *node, asCExprContext *lctx, asCExprContext *rctx, asCExprContext *ctx, eTokenType opToken)
  12311. {
  12312. // Process the property accessor as get
  12313. ProcessPropertyGetAccessor(lctx, node);
  12314. ProcessPropertyGetAccessor(rctx, node);
  12315. DetermineSingleFunc(lctx, node);
  12316. DetermineSingleFunc(rctx, node);
  12317. // Make sure lctx doesn't end up with a variable used in rctx
  12318. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  12319. {
  12320. asCArray<int> vars;
  12321. rctx->bc.GetVarsUsed(vars);
  12322. int offset = AllocateVariable(lctx->type.dataType, true);
  12323. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  12324. ReleaseTemporaryVariable(offset, 0);
  12325. }
  12326. if( opToken == ttUnrecognizedToken )
  12327. opToken = node->tokenType;
  12328. // Warn if not both operands are explicit handles or null handles
  12329. if( (opToken == ttEqual || opToken == ttNotEqual) &&
  12330. ((!(lctx->type.isExplicitHandle || lctx->type.IsNullConstant()) && !(lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE))) ||
  12331. (!(rctx->type.isExplicitHandle || rctx->type.IsNullConstant()) && !(rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->flags & asOBJ_IMPLICIT_HANDLE)))) )
  12332. {
  12333. Warning(TXT_HANDLE_COMPARISON, node);
  12334. }
  12335. // If one of the operands is a value type used as handle, we should look for the opEquals method
  12336. if( ((lctx->type.dataType.GetTypeInfo() && (lctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE)) ||
  12337. (rctx->type.dataType.GetTypeInfo() && (rctx->type.dataType.GetTypeInfo()->flags & asOBJ_ASHANDLE))) &&
  12338. (opToken == ttEqual || opToken == ttIs ||
  12339. opToken == ttNotEqual || opToken == ttNotIs) )
  12340. {
  12341. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  12342. // Find the matching opEquals method
  12343. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, true, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  12344. if( r == 0 )
  12345. {
  12346. // Try again by switching the order of the operands
  12347. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, false, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  12348. }
  12349. if( r == 1 )
  12350. {
  12351. if( opToken == ttNotEqual || opToken == ttNotIs )
  12352. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  12353. // Success, don't continue
  12354. return;
  12355. }
  12356. else if( r == 0 )
  12357. {
  12358. // Couldn't find opEquals method
  12359. Error(TXT_NO_APPROPRIATE_OPEQUALS, node);
  12360. }
  12361. // Compiler error, don't continue
  12362. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  12363. return;
  12364. }
  12365. // Implicitly convert null to the other type
  12366. asCDataType to;
  12367. if( lctx->type.IsNullConstant() )
  12368. to = rctx->type.dataType;
  12369. else if( rctx->type.IsNullConstant() )
  12370. to = lctx->type.dataType;
  12371. else
  12372. {
  12373. // Find a common base type
  12374. asCExprContext tmp(engine);
  12375. tmp.type = rctx->type;
  12376. ImplicitConversion(&tmp, lctx->type.dataType, 0, asIC_IMPLICIT_CONV, false);
  12377. if( tmp.type.dataType.GetTypeInfo() == lctx->type.dataType.GetTypeInfo() )
  12378. to = lctx->type.dataType;
  12379. else
  12380. to = rctx->type.dataType;
  12381. // Assume handle-to-const as it is not possible to convert handle-to-const to handle-to-non-const
  12382. to.MakeHandleToConst(true);
  12383. }
  12384. // Need to pop the value if it is a null constant
  12385. if( lctx->type.IsNullConstant() )
  12386. lctx->bc.Instr(asBC_PopPtr);
  12387. if( rctx->type.IsNullConstant() )
  12388. rctx->bc.Instr(asBC_PopPtr);
  12389. // Convert both sides to explicit handles
  12390. to.MakeHandle(true);
  12391. to.MakeReference(false);
  12392. if( !to.IsObjectHandle() )
  12393. {
  12394. // Compiler error, don't continue
  12395. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  12396. #if AS_SIZEOF_BOOL == 1
  12397. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  12398. #else
  12399. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  12400. #endif
  12401. return;
  12402. }
  12403. // Do the conversion
  12404. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  12405. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  12406. // Both operands must be of the same type
  12407. // Verify that the conversion was successful
  12408. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  12409. {
  12410. asCString str;
  12411. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  12412. Error(str, node);
  12413. }
  12414. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  12415. {
  12416. asCString str;
  12417. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf(), to.Format(outFunc->nameSpace).AddressOf());
  12418. Error(str, node);
  12419. }
  12420. // Make sure it really is handles that are being compared
  12421. if( !lctx->type.dataType.IsObjectHandle() )
  12422. {
  12423. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  12424. }
  12425. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  12426. if( opToken == ttEqual || opToken == ttNotEqual || opToken == ttIs || opToken == ttNotIs )
  12427. {
  12428. // Make sure handles received as parameters by reference are copied to a local variable before the
  12429. // asBC_CmpPtr, so we don't end up comparing the reference to the handle instead of the handle itself
  12430. if( lctx->type.isVariable && !lctx->type.isTemporary && lctx->type.stackOffset <= 0 )
  12431. lctx->type.isVariable = false;
  12432. if( rctx->type.isVariable && !rctx->type.isTemporary && rctx->type.stackOffset <= 0 )
  12433. rctx->type.isVariable = false;
  12434. // TODO: runtime optimize: don't do REFCPY if not necessary
  12435. ConvertToVariableNotIn(lctx, rctx);
  12436. ConvertToVariable(rctx);
  12437. // Pop the pointers from the stack as they will not be used
  12438. lctx->bc.Instr(asBC_PopPtr);
  12439. rctx->bc.Instr(asBC_PopPtr);
  12440. MergeExprBytecode(ctx, lctx);
  12441. MergeExprBytecode(ctx, rctx);
  12442. int a = AllocateVariable(ctx->type.dataType, true);
  12443. int b = lctx->type.stackOffset;
  12444. int c = rctx->type.stackOffset;
  12445. ctx->bc.InstrW_W(asBC_CmpPtr, b, c);
  12446. if( opToken == ttEqual || opToken == ttIs )
  12447. ctx->bc.Instr(asBC_TZ);
  12448. else if( opToken == ttNotEqual || opToken == ttNotIs )
  12449. ctx->bc.Instr(asBC_TNZ);
  12450. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  12451. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  12452. ReleaseTemporaryVariable(lctx->type, &ctx->bc);
  12453. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  12454. ProcessDeferredParams(ctx);
  12455. }
  12456. else
  12457. {
  12458. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  12459. Error(TXT_ILLEGAL_OPERATION, node);
  12460. }
  12461. }
  12462. void asCCompiler::PerformFunctionCall(int funcId, asCExprContext *ctx, bool isConstructor, asCArray<asCExprContext*> *args, asCObjectType *objType, bool useVariable, int varOffset, int funcPtrVar)
  12463. {
  12464. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  12465. // A shared object may not call non-shared functions
  12466. if( outFunc->IsShared() && !descr->IsShared() )
  12467. {
  12468. asCString msg;
  12469. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, descr->GetDeclarationStr().AddressOf());
  12470. Error(msg, ctx->exprNode);
  12471. }
  12472. // Check if the function is private or protected
  12473. if( descr->isPrivate && descr->GetObjectType() != outFunc->GetObjectType() )
  12474. {
  12475. asCString msg;
  12476. msg.Format(TXT_PRIVATE_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  12477. Error(msg, ctx->exprNode);
  12478. }
  12479. else if( descr->isProtected &&
  12480. !(descr->GetObjectType() == outFunc->GetObjectType() ||
  12481. (outFunc->GetObjectType() && outFunc->GetObjectType()->DerivesFrom(descr->GetObjectType()))) )
  12482. {
  12483. asCString msg;
  12484. msg.Format(TXT_PROTECTED_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  12485. Error(msg, ctx->exprNode);
  12486. }
  12487. int argSize = descr->GetSpaceNeededForArguments();
  12488. // If we're calling a class method we must make sure the object is guaranteed to stay
  12489. // alive throughout the call by holding on to a reference in a local variable. This must
  12490. // be done for any methods that return references, and any calls on script objects.
  12491. // Application registered objects are assumed to know to keep themselves alive even
  12492. // if the method doesn't return a reference.
  12493. if( !ctx->type.isHandleSafe &&
  12494. descr->objectType &&
  12495. (ctx->type.dataType.IsObjectHandle() || ctx->type.dataType.SupportHandles()) &&
  12496. (descr->returnType.IsReference() || (ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_SCRIPT_OBJECT)) &&
  12497. !(ctx->type.isVariable || ctx->type.isTemporary) &&
  12498. !(ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_SCOPED) &&
  12499. !(ctx->type.dataType.GetTypeInfo()->GetFlags() & asOBJ_ASHANDLE) )
  12500. {
  12501. // TODO: runtime optimize: Avoid this for global variables, by storing a reference to the global variable once in a
  12502. // local variable and then refer to the same for each call. An alias for the global variable
  12503. // should be stored in the variable scope so that the compiler can find it. For loops and
  12504. // scopes that will always be executed, i.e. non-if scopes the alias should be stored in the
  12505. // higher scope to increase the probability of re-use.
  12506. int tempRef = AllocateVariable(ctx->type.dataType, true);
  12507. ctx->bc.InstrSHORT(asBC_PSF, (short)tempRef);
  12508. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetTypeInfo());
  12509. // Add the release of this reference as a deferred expression
  12510. asSDeferredParam deferred;
  12511. deferred.origExpr = 0;
  12512. deferred.argInOutFlags = asTM_INREF;
  12513. deferred.argNode = 0;
  12514. deferred.argType.SetVariable(ctx->type.dataType, tempRef, true);
  12515. ctx->deferredParams.PushLast(deferred);
  12516. // Forget the current type
  12517. ctx->type.SetDummy();
  12518. }
  12519. // Check if there is a need to add a hidden pointer for when the function returns an object by value
  12520. if( descr->DoesReturnOnStack() && !useVariable )
  12521. {
  12522. useVariable = true;
  12523. varOffset = AllocateVariable(descr->returnType, true);
  12524. // Push the pointer to the pre-allocated space for the return value
  12525. ctx->bc.InstrSHORT(asBC_PSF, short(varOffset));
  12526. if( descr->objectType )
  12527. {
  12528. // The object pointer is already on the stack, but should be the top
  12529. // one, so we need to swap the pointers in order to get the correct
  12530. ctx->bc.Instr(asBC_SwapPtr);
  12531. }
  12532. }
  12533. if( isConstructor )
  12534. {
  12535. // Sometimes the value types are allocated on the heap,
  12536. // which is when this way of constructing them is used.
  12537. asASSERT(useVariable == false);
  12538. if( (objType->flags & asOBJ_TEMPLATE) )
  12539. {
  12540. asASSERT( descr->funcType == asFUNC_SCRIPT );
  12541. // Find the id of the real constructor and not the generated stub
  12542. asUINT id = 0;
  12543. asDWORD *bc = descr->scriptData->byteCode.AddressOf();
  12544. while( bc )
  12545. {
  12546. if( (*(asBYTE*)bc) == asBC_CALLSYS )
  12547. {
  12548. id = asBC_INTARG(bc);
  12549. break;
  12550. }
  12551. bc += asBCTypeSize[asBCInfo[*(asBYTE*)bc].type];
  12552. }
  12553. asASSERT( id );
  12554. ctx->bc.InstrPTR(asBC_OBJTYPE, objType);
  12555. ctx->bc.Alloc(asBC_ALLOC, objType, id, argSize + AS_PTR_SIZE + AS_PTR_SIZE);
  12556. }
  12557. else
  12558. ctx->bc.Alloc(asBC_ALLOC, objType, descr->id, argSize+AS_PTR_SIZE);
  12559. // The instruction has already moved the returned object to the variable
  12560. ctx->type.Set(asCDataType::CreatePrimitive(ttVoid, false));
  12561. ctx->type.isLValue = false;
  12562. // Clean up arguments
  12563. if( args )
  12564. AfterFunctionCall(funcId, *args, ctx, false);
  12565. ProcessDeferredParams(ctx);
  12566. return;
  12567. }
  12568. else
  12569. {
  12570. if( descr->objectType )
  12571. argSize += AS_PTR_SIZE;
  12572. // If the function returns an object by value the address of the location
  12573. // where the value should be stored is passed as an argument too
  12574. if( descr->DoesReturnOnStack() )
  12575. argSize += AS_PTR_SIZE;
  12576. // TODO: runtime optimize: If it is known that a class method cannot be overridden the call
  12577. // should be made with asBC_CALL as it is faster. Examples where this
  12578. // is known is for example finalled methods where the class doesn't derive
  12579. // from any other, or even non-finalled methods but where it is known
  12580. // at compile time the true type of the object. The first should be
  12581. // quite easy to determine, but the latter will be quite complex and possibly
  12582. // not worth it.
  12583. if( descr->funcType == asFUNC_IMPORTED )
  12584. ctx->bc.Call(asBC_CALLBND , descr->id, argSize);
  12585. // TODO: Maybe we need two different byte codes
  12586. else if( descr->funcType == asFUNC_INTERFACE || descr->funcType == asFUNC_VIRTUAL )
  12587. ctx->bc.Call(asBC_CALLINTF, descr->id, argSize);
  12588. else if( descr->funcType == asFUNC_SCRIPT )
  12589. ctx->bc.Call(asBC_CALL , descr->id, argSize);
  12590. else if( descr->funcType == asFUNC_SYSTEM )
  12591. {
  12592. // Check if we can use the faster asBC_Thiscall1 instruction, i.e. one of
  12593. // type &obj::func(int)
  12594. // type &obj::func(uint)
  12595. if( descr->GetObjectType() && descr->returnType.IsReference() &&
  12596. descr->parameterTypes.GetLength() == 1 &&
  12597. (descr->parameterTypes[0].IsIntegerType() || descr->parameterTypes[0].IsUnsignedType()) &&
  12598. descr->parameterTypes[0].GetSizeInMemoryBytes() == 4 &&
  12599. !descr->parameterTypes[0].IsReference() )
  12600. ctx->bc.Call(asBC_Thiscall1, descr->id, argSize);
  12601. else
  12602. ctx->bc.Call(asBC_CALLSYS , descr->id, argSize);
  12603. }
  12604. else if( descr->funcType == asFUNC_FUNCDEF )
  12605. ctx->bc.CallPtr(asBC_CallPtr, funcPtrVar, argSize);
  12606. }
  12607. if( (descr->returnType.IsObject() || descr->returnType.IsFuncdef()) && !descr->returnType.IsReference() )
  12608. {
  12609. int returnOffset = 0;
  12610. asCExprValue tmpExpr = ctx->type;
  12611. if( descr->DoesReturnOnStack() )
  12612. {
  12613. asASSERT( useVariable );
  12614. // The variable was allocated before the function was called
  12615. returnOffset = varOffset;
  12616. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  12617. // The variable was initialized by the function, so we need to mark it as initialized here
  12618. ctx->bc.ObjInfo(varOffset, asOBJ_INIT);
  12619. }
  12620. else
  12621. {
  12622. if( useVariable )
  12623. {
  12624. // Use the given variable
  12625. returnOffset = varOffset;
  12626. ctx->type.SetVariable(descr->returnType, returnOffset, false);
  12627. }
  12628. else
  12629. {
  12630. // Allocate a temporary variable for the returned object
  12631. // The returned object will actually be allocated on the heap, so
  12632. // we must force the allocation of the variable to do the same
  12633. returnOffset = AllocateVariable(descr->returnType, true, !descr->returnType.IsObjectHandle());
  12634. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  12635. }
  12636. // Move the pointer from the object register to the temporary variable
  12637. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  12638. }
  12639. ReleaseTemporaryVariable(tmpExpr, &ctx->bc);
  12640. ctx->type.dataType.MakeReference(IsVariableOnHeap(returnOffset));
  12641. ctx->type.isLValue = false; // It is a reference, but not an lvalue
  12642. // Clean up arguments
  12643. if( args )
  12644. AfterFunctionCall(funcId, *args, ctx, false);
  12645. ProcessDeferredParams(ctx);
  12646. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  12647. }
  12648. else if( descr->returnType.IsReference() )
  12649. {
  12650. asASSERT(useVariable == false);
  12651. // We cannot clean up the arguments yet, because the
  12652. // reference might be pointing to one of them.
  12653. if( args )
  12654. AfterFunctionCall(funcId, *args, ctx, true);
  12655. // Do not process the output parameters yet, because it
  12656. // might invalidate the returned reference
  12657. // If the context holds a variable that needs cleanup
  12658. // store it as a deferred parameter so it will be cleaned up
  12659. // afterwards.
  12660. if( ctx->type.isTemporary )
  12661. {
  12662. asSDeferredParam defer;
  12663. defer.argNode = 0;
  12664. defer.argType = ctx->type;
  12665. defer.argInOutFlags = asTM_INOUTREF;
  12666. defer.origExpr = 0;
  12667. ctx->deferredParams.PushLast(defer);
  12668. }
  12669. ctx->type.Set(descr->returnType);
  12670. if( !descr->returnType.IsPrimitive() )
  12671. {
  12672. ctx->bc.Instr(asBC_PshRPtr);
  12673. if( descr->returnType.IsObject() &&
  12674. !descr->returnType.IsObjectHandle() )
  12675. {
  12676. // We are getting the pointer to the object
  12677. // not a pointer to a object variable
  12678. ctx->type.dataType.MakeReference(false);
  12679. }
  12680. }
  12681. // A returned reference can be used as lvalue
  12682. ctx->type.isLValue = true;
  12683. }
  12684. else
  12685. {
  12686. asASSERT(useVariable == false);
  12687. asCExprValue tmpExpr = ctx->type;
  12688. if( descr->returnType.GetSizeInMemoryBytes() )
  12689. {
  12690. // Allocate a temporary variable to hold the value, but make sure
  12691. // the temporary variable isn't used in any of the deferred arguments
  12692. int l = int(reservedVariables.GetLength());
  12693. for( asUINT n = 0; args && n < args->GetLength(); n++ )
  12694. {
  12695. asCExprContext *expr = (*args)[n]->origExpr;
  12696. if( expr )
  12697. expr->bc.GetVarsUsed(reservedVariables);
  12698. }
  12699. int offset = AllocateVariable(descr->returnType, true);
  12700. reservedVariables.SetLength(l);
  12701. ctx->type.SetVariable(descr->returnType, offset, true);
  12702. // Move the value from the return register to the variable
  12703. if( descr->returnType.GetSizeOnStackDWords() == 1 )
  12704. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)offset);
  12705. else if( descr->returnType.GetSizeOnStackDWords() == 2 )
  12706. ctx->bc.InstrSHORT(asBC_CpyRtoV8, (short)offset);
  12707. }
  12708. else
  12709. ctx->type.Set(descr->returnType);
  12710. ReleaseTemporaryVariable(tmpExpr, &ctx->bc);
  12711. ctx->type.isLValue = false;
  12712. // Clean up arguments
  12713. if( args )
  12714. AfterFunctionCall(funcId, *args, ctx, false);
  12715. ProcessDeferredParams(ctx);
  12716. }
  12717. }
  12718. // This only merges the bytecode, but doesn't modify the type of the final context
  12719. void asCCompiler::MergeExprBytecode(asCExprContext *before, asCExprContext *after)
  12720. {
  12721. before->bc.AddCode(&after->bc);
  12722. for( asUINT n = 0; n < after->deferredParams.GetLength(); n++ )
  12723. {
  12724. before->deferredParams.PushLast(after->deferredParams[n]);
  12725. after->deferredParams[n].origExpr = 0;
  12726. }
  12727. after->deferredParams.SetLength(0);
  12728. }
  12729. // This merges both bytecode and the type of the final context
  12730. void asCCompiler::MergeExprBytecodeAndType(asCExprContext *before, asCExprContext *after)
  12731. {
  12732. MergeExprBytecode(before, after);
  12733. before->Merge(after);
  12734. }
  12735. void asCCompiler::FilterConst(asCArray<int> &funcs, bool removeConst)
  12736. {
  12737. if( funcs.GetLength() == 0 ) return;
  12738. // This is only done for object methods
  12739. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[0]);
  12740. if( desc->objectType == 0 ) return;
  12741. // Check if there are any non-const matches
  12742. asUINT n;
  12743. bool foundNonConst = false;
  12744. for( n = 0; n < funcs.GetLength(); n++ )
  12745. {
  12746. desc = builder->GetFunctionDescription(funcs[n]);
  12747. if( desc->isReadOnly != removeConst )
  12748. {
  12749. foundNonConst = true;
  12750. break;
  12751. }
  12752. }
  12753. if( foundNonConst )
  12754. {
  12755. // Remove all const methods
  12756. for( n = 0; n < funcs.GetLength(); n++ )
  12757. {
  12758. desc = builder->GetFunctionDescription(funcs[n]);
  12759. if( desc->isReadOnly == removeConst )
  12760. {
  12761. if( n == funcs.GetLength() - 1 )
  12762. funcs.PopLast();
  12763. else
  12764. funcs[n] = funcs.PopLast();
  12765. n--;
  12766. }
  12767. }
  12768. }
  12769. }
  12770. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12771. asCExprValue::asCExprValue()
  12772. {
  12773. isTemporary = false;
  12774. stackOffset = 0;
  12775. isConstant = false;
  12776. isVariable = false;
  12777. isExplicitHandle = false;
  12778. qwordValue = 0;
  12779. isLValue = false;
  12780. isRefToLocal = false;
  12781. isHandleSafe = false;
  12782. }
  12783. void asCExprValue::Set(const asCDataType &dt)
  12784. {
  12785. dataType = dt;
  12786. isTemporary = false;
  12787. stackOffset = 0;
  12788. isConstant = false;
  12789. isVariable = false;
  12790. isExplicitHandle = false;
  12791. qwordValue = 0;
  12792. isLValue = false;
  12793. isRefToLocal = false;
  12794. isHandleSafe = false;
  12795. }
  12796. void asCExprValue::SetVariable(const asCDataType &in_dt, int in_stackOffset, bool in_isTemporary)
  12797. {
  12798. Set(in_dt);
  12799. this->isVariable = true;
  12800. this->isTemporary = in_isTemporary;
  12801. this->stackOffset = (short)in_stackOffset;
  12802. }
  12803. void asCExprValue::SetConstantQW(const asCDataType &dt, asQWORD value)
  12804. {
  12805. Set(dt);
  12806. isConstant = true;
  12807. SetConstantQW(value);
  12808. }
  12809. void asCExprValue::SetConstantDW(const asCDataType &dt, asDWORD value)
  12810. {
  12811. Set(dt);
  12812. isConstant = true;
  12813. SetConstantDW(value);
  12814. }
  12815. void asCExprValue::SetConstantB(const asCDataType &dt, asBYTE value)
  12816. {
  12817. Set(dt);
  12818. isConstant = true;
  12819. SetConstantB(value);
  12820. }
  12821. void asCExprValue::SetConstantW(const asCDataType &dt, asWORD value)
  12822. {
  12823. Set(dt);
  12824. isConstant = true;
  12825. SetConstantW(value);
  12826. }
  12827. void asCExprValue::SetConstantF(const asCDataType &dt, float value)
  12828. {
  12829. Set(dt);
  12830. isConstant = true;
  12831. SetConstantF(value);
  12832. }
  12833. void asCExprValue::SetConstantD(const asCDataType &dt, double value)
  12834. {
  12835. Set(dt);
  12836. isConstant = true;
  12837. SetConstantD(value);
  12838. }
  12839. void asCExprValue::SetConstantQW(asQWORD value)
  12840. {
  12841. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  12842. qwordValue = value;
  12843. }
  12844. void asCExprValue::SetConstantDW(asDWORD value)
  12845. {
  12846. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  12847. dwordValue = value;
  12848. }
  12849. void asCExprValue::SetConstantW(asWORD value)
  12850. {
  12851. asASSERT(dataType.GetSizeInMemoryBytes() == 2);
  12852. wordValue = value;
  12853. }
  12854. void asCExprValue::SetConstantB(asBYTE value)
  12855. {
  12856. asASSERT(dataType.GetSizeInMemoryBytes() == 1);
  12857. byteValue = value;
  12858. }
  12859. void asCExprValue::SetConstantF(float value)
  12860. {
  12861. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  12862. floatValue = value;
  12863. }
  12864. void asCExprValue::SetConstantD(double value)
  12865. {
  12866. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  12867. doubleValue = value;
  12868. }
  12869. asQWORD asCExprValue::GetConstantQW()
  12870. {
  12871. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  12872. return qwordValue;
  12873. }
  12874. asDWORD asCExprValue::GetConstantDW()
  12875. {
  12876. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  12877. return dwordValue;
  12878. }
  12879. asWORD asCExprValue::GetConstantW()
  12880. {
  12881. asASSERT(dataType.GetSizeInMemoryBytes() == 2);
  12882. return wordValue;
  12883. }
  12884. asBYTE asCExprValue::GetConstantB()
  12885. {
  12886. asASSERT(dataType.GetSizeInMemoryBytes() == 1);
  12887. return byteValue;
  12888. }
  12889. float asCExprValue::GetConstantF()
  12890. {
  12891. asASSERT(dataType.GetSizeInMemoryBytes() == 4);
  12892. return floatValue;
  12893. }
  12894. double asCExprValue::GetConstantD()
  12895. {
  12896. asASSERT(dataType.GetSizeInMemoryBytes() == 8);
  12897. return doubleValue;
  12898. }
  12899. void asCExprValue::SetConstantData(const asCDataType &dt, asQWORD qw)
  12900. {
  12901. Set(dt);
  12902. isConstant = true;
  12903. // This code is necessary to guarantee that the code
  12904. // works on both big endian and little endian CPUs.
  12905. if (dataType.GetSizeInMemoryBytes() == 1)
  12906. byteValue = (asBYTE)qw;
  12907. if (dataType.GetSizeInMemoryBytes() == 2)
  12908. wordValue = (asWORD)qw;
  12909. if (dataType.GetSizeInMemoryBytes() == 4)
  12910. dwordValue = (asDWORD)qw;
  12911. else
  12912. qwordValue = qw;
  12913. }
  12914. asQWORD asCExprValue::GetConstantData()
  12915. {
  12916. asQWORD qw = 0;
  12917. // This code is necessary to guarantee that the code
  12918. // works on both big endian and little endian CPUs.
  12919. if (dataType.GetSizeInMemoryBytes() == 1)
  12920. qw = byteValue;
  12921. if (dataType.GetSizeInMemoryBytes() == 2)
  12922. qw = wordValue;
  12923. if (dataType.GetSizeInMemoryBytes() == 4)
  12924. qw = dwordValue;
  12925. else
  12926. qw = qwordValue;
  12927. return qw;
  12928. }
  12929. void asCExprValue::SetUndefinedFuncHandle(asCScriptEngine *engine)
  12930. {
  12931. // This is used for when the expression evaluates to a
  12932. // function, but it is not yet known exactly which. The
  12933. // owner expression will hold the name of the function
  12934. // to determine the exact function when the signature is
  12935. // known.
  12936. Set(asCDataType::CreateObjectHandle(&engine->functionBehaviours, true));
  12937. isConstant = true;
  12938. isExplicitHandle = false;
  12939. qwordValue = 1; // Set to a different value than 0 to differentiate from null constant
  12940. isLValue = false;
  12941. }
  12942. bool asCExprValue::IsUndefinedFuncHandle() const
  12943. {
  12944. if (isConstant == false) return false;
  12945. if (qwordValue == 0) return false;
  12946. if (isLValue) return false;
  12947. if (dataType.GetTypeInfo() == 0) return false;
  12948. if (dataType.GetTypeInfo()->name != "$func") return false;
  12949. if (dataType.IsFuncdef()) return false;
  12950. return true;
  12951. }
  12952. void asCExprValue::SetNullConstant()
  12953. {
  12954. Set(asCDataType::CreateNullHandle());
  12955. isConstant = true;
  12956. isExplicitHandle = false;
  12957. qwordValue = 0;
  12958. isLValue = false;
  12959. }
  12960. bool asCExprValue::IsNullConstant() const
  12961. {
  12962. // We can't check the actual object type, because the null constant may have been cast to another type
  12963. if (isConstant && dataType.IsObjectHandle() && qwordValue == 0)
  12964. return true;
  12965. return false;
  12966. }
  12967. void asCExprValue::SetVoid()
  12968. {
  12969. Set(asCDataType::CreatePrimitive(ttVoid, false));
  12970. isLValue = false;
  12971. isConstant = true;
  12972. }
  12973. bool asCExprValue::IsVoid() const
  12974. {
  12975. if (dataType.GetTokenType() == ttVoid)
  12976. return true;
  12977. return false;
  12978. }
  12979. void asCExprValue::SetDummy()
  12980. {
  12981. SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  12982. }
  12983. ////////////////////////////////////////////////////////////////////////////////////////////////
  12984. asCExprContext::asCExprContext(asCScriptEngine *engine) : bc(engine)
  12985. {
  12986. property_arg = 0;
  12987. Clear();
  12988. }
  12989. asCExprContext::~asCExprContext()
  12990. {
  12991. if (property_arg)
  12992. asDELETE(property_arg, asCExprContext);
  12993. }
  12994. void asCExprContext::Clear()
  12995. {
  12996. bc.ClearAll();
  12997. type.Set(asCDataType());
  12998. deferredParams.SetLength(0);
  12999. if (property_arg)
  13000. asDELETE(property_arg, asCExprContext);
  13001. property_arg = 0;
  13002. exprNode = 0;
  13003. origExpr = 0;
  13004. property_get = 0;
  13005. property_set = 0;
  13006. property_const = false;
  13007. property_handle = false;
  13008. property_ref = false;
  13009. methodName = "";
  13010. enumValue = "";
  13011. isVoidExpression = false;
  13012. isCleanArg = false;
  13013. }
  13014. bool asCExprContext::IsClassMethod() const
  13015. {
  13016. if (type.dataType.GetTypeInfo() == 0) return false;
  13017. if (methodName == "") return false;
  13018. if (type.dataType.GetTypeInfo() == &type.dataType.GetTypeInfo()->engine->functionBehaviours) return false;
  13019. return true;
  13020. }
  13021. bool asCExprContext::IsGlobalFunc() const
  13022. {
  13023. if (type.dataType.GetTypeInfo() == 0) return false;
  13024. if (methodName == "") return false;
  13025. if (type.dataType.GetTypeInfo() != &type.dataType.GetTypeInfo()->engine->functionBehaviours) return false;
  13026. return true;
  13027. }
  13028. void asCExprContext::SetLambda(asCScriptNode *funcDecl)
  13029. {
  13030. asASSERT(funcDecl && funcDecl->nodeType == snFunction);
  13031. asASSERT(bc.GetLastInstr() == -1);
  13032. Clear();
  13033. type.SetUndefinedFuncHandle(bc.GetEngine());
  13034. exprNode = funcDecl;
  13035. }
  13036. bool asCExprContext::IsLambda() const
  13037. {
  13038. if (type.IsUndefinedFuncHandle() && exprNode && exprNode->nodeType == snFunction)
  13039. return true;
  13040. return false;
  13041. }
  13042. void asCExprContext::SetVoidExpression()
  13043. {
  13044. Clear();
  13045. type.SetVoid();
  13046. isVoidExpression = true;
  13047. }
  13048. bool asCExprContext::IsVoidExpression() const
  13049. {
  13050. if (isVoidExpression && type.IsVoid() && exprNode == 0)
  13051. return true;
  13052. return false;
  13053. }
  13054. void asCExprContext::Merge(asCExprContext *after)
  13055. {
  13056. type = after->type;
  13057. property_get = after->property_get;
  13058. property_set = after->property_set;
  13059. property_const = after->property_const;
  13060. property_handle = after->property_handle;
  13061. property_ref = after->property_ref;
  13062. property_arg = after->property_arg;
  13063. exprNode = after->exprNode;
  13064. methodName = after->methodName;
  13065. enumValue = after->enumValue;
  13066. isVoidExpression = after->isVoidExpression;
  13067. isCleanArg = after->isCleanArg;
  13068. after->property_arg = 0;
  13069. // Do not copy the origExpr member
  13070. }
  13071. END_AS_NAMESPACE
  13072. #endif // AS_NO_COMPILER