as_compiler.cpp 411 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 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 references 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 *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  78. {
  79. this->builder = builder;
  80. this->engine = builder->engine;
  81. this->script = script;
  82. this->outFunc = outFunc;
  83. hasCompileErrors = false;
  84. m_isConstructor = false;
  85. m_isConstructorCalled = false;
  86. m_classDecl = 0;
  87. nextLabel = 0;
  88. breakLabels.SetLength(0);
  89. continueLabels.SetLength(0);
  90. byteCode.ClearAll();
  91. }
  92. int asCCompiler::CompileDefaultConstructor(asCBuilder *builder, asCScriptCode *script, asCScriptNode *node, asCScriptFunction *outFunc, sClassDeclaration *classDecl)
  93. {
  94. Reset(builder, script, outFunc);
  95. m_classDecl = classDecl;
  96. // Insert a JitEntry at the start of the function for JIT compilers
  97. byteCode.InstrPTR(asBC_JitEntry, 0);
  98. // Add a variable scope that might be needed to declare dummy variables
  99. // in case the member initialization refers to undefined symbols.
  100. AddVariableScope();
  101. // Initialize the class members that have no explicit expression first. This will allow the
  102. // base class' constructor to access these members without worry they will be uninitialized.
  103. // This can happen if the base class' constructor calls a method that is overridden by the derived class
  104. CompileMemberInitialization(&byteCode, true);
  105. // If the class is derived from another, then the base class' default constructor must be called
  106. if( outFunc->objectType->derivedFrom )
  107. {
  108. // Make sure the base class really has a default constructor
  109. if( outFunc->objectType->derivedFrom->beh.construct == 0 )
  110. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, node);
  111. // Call the base class' default constructor
  112. byteCode.InstrSHORT(asBC_PSF, 0);
  113. byteCode.Instr(asBC_RDSPtr);
  114. byteCode.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  115. }
  116. // Initialize the class members that explicit expressions afterwards. This allow the expressions
  117. // to access the base class members without worry they will be uninitialized
  118. CompileMemberInitialization(&byteCode, false);
  119. byteCode.OptimizeLocally(tempVariableOffsets);
  120. // If there are compile errors, there is no reason to build the final code
  121. if( hasCompileErrors )
  122. return -1;
  123. // Pop the object pointer from the stack
  124. byteCode.Ret(AS_PTR_SIZE);
  125. // Count total variable size
  126. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  127. outFunc->scriptData->variableSpace = varSize;
  128. FinalizeFunction();
  129. #ifdef AS_DEBUG
  130. // DEBUG: output byte code
  131. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + "__defconstr.txt").AddressOf(), engine, outFunc);
  132. #endif
  133. return 0;
  134. }
  135. int asCCompiler::CompileFactory(asCBuilder *builder, asCScriptCode *script, asCScriptFunction *outFunc)
  136. {
  137. Reset(builder, script, outFunc);
  138. // Insert a JitEntry at the start of the function for JIT compilers
  139. byteCode.InstrPTR(asBC_JitEntry, 0);
  140. // Find the corresponding constructor
  141. asCDataType dt = asCDataType::CreateObject(outFunc->returnType.GetObjectType(), false);
  142. int constructor = 0;
  143. for( unsigned int n = 0; n < dt.GetBehaviour()->factories.GetLength(); n++ )
  144. {
  145. if( dt.GetBehaviour()->factories[n] == outFunc->id )
  146. {
  147. constructor = dt.GetBehaviour()->constructors[n];
  148. break;
  149. }
  150. }
  151. // Allocate the class and instanciate it with the constructor
  152. int varOffset = AllocateVariable(dt, true);
  153. outFunc->scriptData->variableSpace = AS_PTR_SIZE;
  154. byteCode.InstrSHORT(asBC_PSF, (short)varOffset);
  155. // Copy all arguments to the top of the stack
  156. // TODO: runtime optimize: Might be interesting to have a specific instruction for copying all arguments
  157. int offset = (int)outFunc->GetSpaceNeededForArguments();
  158. for( int a = int(outFunc->parameterTypes.GetLength()) - 1; a >= 0; a-- )
  159. {
  160. if( !outFunc->parameterTypes[a].IsPrimitive() ||
  161. outFunc->parameterTypes[a].IsReference() )
  162. {
  163. offset -= AS_PTR_SIZE;
  164. byteCode.InstrSHORT(asBC_PshVPtr, short(-offset));
  165. }
  166. else
  167. {
  168. if( outFunc->parameterTypes[a].GetSizeOnStackDWords() == 2 )
  169. {
  170. offset -= 2;
  171. byteCode.InstrSHORT(asBC_PshV8, short(-offset));
  172. }
  173. else
  174. {
  175. offset -= 1;
  176. byteCode.InstrSHORT(asBC_PshV4, short(-offset));
  177. }
  178. }
  179. }
  180. int argDwords = (int)outFunc->GetSpaceNeededForArguments();
  181. byteCode.Alloc(asBC_ALLOC, dt.GetObjectType(), constructor, argDwords + AS_PTR_SIZE);
  182. // Return a handle to the newly created object
  183. byteCode.InstrSHORT(asBC_LOADOBJ, (short)varOffset);
  184. byteCode.Ret(argDwords);
  185. FinalizeFunction();
  186. // Tell the virtual machine not to clean up parameters on exception
  187. outFunc->dontCleanUpOnException = true;
  188. /*
  189. #ifdef AS_DEBUG
  190. // DEBUG: output byte code
  191. asCString args;
  192. args.Format("%d", outFunc->parameterTypes.GetLength());
  193. byteCode.DebugOutput(("__" + outFunc->name + "__factory" + args + ".txt").AddressOf(), engine);
  194. #endif
  195. */
  196. return 0;
  197. }
  198. void asCCompiler::FinalizeFunction()
  199. {
  200. TimeIt("asCCompiler::FinalizeFunction");
  201. asASSERT( outFunc->scriptData );
  202. asUINT n;
  203. // Finalize the bytecode
  204. byteCode.Finalize(tempVariableOffsets);
  205. byteCode.ExtractObjectVariableInfo(outFunc);
  206. // Compile the list of object variables for the exception handler
  207. // Start with the variables allocated on the heap, and then the ones allocated on the stack
  208. for( n = 0; n < variableAllocations.GetLength(); n++ )
  209. {
  210. if( variableAllocations[n].IsObject() && !variableAllocations[n].IsReference() )
  211. {
  212. if( variableIsOnHeap[n] )
  213. {
  214. outFunc->scriptData->objVariableTypes.PushLast(variableAllocations[n].GetObjectType());
  215. outFunc->scriptData->funcVariableTypes.PushLast(variableAllocations[n].GetFuncDef());
  216. outFunc->scriptData->objVariablePos.PushLast(GetVariableOffset(n));
  217. }
  218. }
  219. }
  220. outFunc->scriptData->objVariablesOnHeap = asUINT(outFunc->scriptData->objVariablePos.GetLength());
  221. for( n = 0; n < variableAllocations.GetLength(); n++ )
  222. {
  223. if( variableAllocations[n].IsObject() && !variableAllocations[n].IsReference() )
  224. {
  225. if( !variableIsOnHeap[n] )
  226. {
  227. outFunc->scriptData->objVariableTypes.PushLast(variableAllocations[n].GetObjectType());
  228. outFunc->scriptData->funcVariableTypes.PushLast(variableAllocations[n].GetFuncDef());
  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.CanBeInstanciated() && returnType != asCDataType::CreatePrimitive(ttVoid, false)) ||
  276. (returnType.IsReference() && !returnType.CanBeInstanciated()) )
  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().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.CanBeInstanciated()) ||
  299. (!type.IsReference() && !type.CanBeInstanciated()) )
  300. {
  301. asCString parm = type.Format();
  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 *byteCode, 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.GetObjectType()->flags & asOBJ_REF) &&
  391. prop->type.GetBehaviour()->factory == 0) ||
  392. ((prop->type.GetObjectType()->flags & asOBJ_VALUE) &&
  393. prop->type.GetBehaviour()->construct == 0 &&
  394. !(prop->type.GetObjectType()->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.GetObjectType()->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(byteCode, declNode->tokenPos);
  412. // Compile the initialization
  413. asQWORD constantValue;
  414. asCByteCode bc(engine);
  415. CompileInitialization(initNode, &bc, prop->type, declNode, prop->byteOffset, &constantValue, 2);
  416. byteCode->AddCode(&bc);
  417. script = origScript;
  418. #endif
  419. }
  420. }
  421. }
  422. // Entry
  423. int asCCompiler::CompileFunction(asCBuilder *builder, asCScriptCode *script, asCArray<asCString> &parameterNames, asCScriptNode *func, asCScriptFunction *outFunc, sClassDeclaration *classDecl)
  424. {
  425. TimeIt("asCCompiler::CompileFunction");
  426. Reset(builder, script, outFunc);
  427. int buildErrors = builder->numErrors;
  428. int stackPos = SetupParametersAndReturnVariable(parameterNames, func);
  429. //--------------------------------------------
  430. // Compile the statement block
  431. if( m_isConstructor )
  432. m_classDecl = classDecl;
  433. // We need to parse the statement block now
  434. asCScriptNode *blockBegin;
  435. // If the function signature was implicit, e.g. virtual property
  436. // accessor, then the received node already is the statement block
  437. if( func->nodeType != snStatementBlock )
  438. blockBegin = func->lastChild;
  439. else
  440. blockBegin = func;
  441. // TODO: memory: We can parse the statement block one statement at a time, thus save even more memory
  442. // 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
  443. asCParser parser(builder);
  444. int r = parser.ParseStatementBlock(script, blockBegin);
  445. if( r < 0 ) return -1;
  446. asCScriptNode *block = parser.GetScriptNode();
  447. // Reserve a label for the cleanup code
  448. nextLabel++;
  449. bool hasReturn;
  450. asCByteCode bc(engine);
  451. LineInstr(&bc, blockBegin->tokenPos);
  452. CompileStatementBlock(block, false, &hasReturn, &bc);
  453. LineInstr(&bc, blockBegin->tokenPos + blockBegin->tokenLength);
  454. // Make sure there is a return in all paths (if not return type is void)
  455. // Don't bother with this check if there are compiler errors, e.g. Unreachable code
  456. if( !hasCompileErrors && outFunc->returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  457. {
  458. if( hasReturn == false )
  459. Error(TXT_NOT_ALL_PATHS_RETURN, blockBegin);
  460. }
  461. //------------------------------------------------
  462. // Concatenate the bytecode
  463. // Insert a JitEntry at the start of the function for JIT compilers
  464. byteCode.InstrPTR(asBC_JitEntry, 0);
  465. if( outFunc->objectType )
  466. {
  467. if( m_isConstructor )
  468. {
  469. if( outFunc->objectType->derivedFrom )
  470. {
  471. // Call the base class' default constructor unless called manually in the code
  472. if( !m_isConstructorCalled )
  473. {
  474. if( outFunc->objectType->derivedFrom->beh.construct )
  475. {
  476. // Initialize members without explicit expression first
  477. CompileMemberInitialization(&byteCode, true);
  478. // Call base class' constructor
  479. asCByteCode tmpBC(engine);
  480. tmpBC.InstrSHORT(asBC_PSF, 0);
  481. tmpBC.Instr(asBC_RDSPtr);
  482. tmpBC.Call(asBC_CALL, outFunc->objectType->derivedFrom->beh.construct, AS_PTR_SIZE);
  483. tmpBC.OptimizeLocally(tempVariableOffsets);
  484. byteCode.AddCode(&tmpBC);
  485. // Add the initialization of the members with explicit expressions
  486. CompileMemberInitialization(&byteCode, false);
  487. }
  488. else
  489. Error(TEXT_BASE_DOESNT_HAVE_DEF_CONSTR, blockBegin);
  490. }
  491. else
  492. {
  493. // Only initialize members that don't have an explicit expression
  494. // The members that are explicitly initialized will be initialized after the call to base class' constructor
  495. CompileMemberInitialization(&byteCode, true);
  496. }
  497. }
  498. else
  499. {
  500. // Add the initialization of the members
  501. CompileMemberInitialization(&byteCode, true);
  502. CompileMemberInitialization(&byteCode, false);
  503. }
  504. }
  505. // Increase the reference for the object pointer, so that it is guaranteed to live during the entire call
  506. if( !m_isConstructor && !outFunc->returnType.IsReference() )
  507. {
  508. // TODO: runtime optimize: If the function is trivial, i.e. doesn't access any outside functions,
  509. // then this is not necessary. If I implement this, then the function needs
  510. // to set a flag so the exception handler doesn't try to release the handle.
  511. // It is not necessary to do this for constructors, as they have no outside references that can be released anyway
  512. // It is not necessary to do this for methods that return references, as the caller is guaranteed to hold a reference to the object
  513. asCByteCode tmpBC(engine);
  514. tmpBC.InstrSHORT(asBC_PSF, 0);
  515. tmpBC.Instr(asBC_RDSPtr);
  516. tmpBC.Call(asBC_CALLSYS, outFunc->objectType->beh.addref, AS_PTR_SIZE);
  517. tmpBC.OptimizeLocally(tempVariableOffsets);
  518. byteCode.AddCode(&tmpBC);
  519. }
  520. }
  521. // Add the code for the statement block
  522. byteCode.AddCode(&bc);
  523. // Count total variable size
  524. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  525. outFunc->scriptData->variableSpace = varSize;
  526. // Deallocate all local variables
  527. int n;
  528. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  529. {
  530. sVariable *v = variables->variables[n];
  531. if( v->stackOffset > 0 )
  532. {
  533. // Call variables destructors
  534. if( v->name != "return" && v->name != "return address" )
  535. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  536. DeallocateVariable(v->stackOffset);
  537. }
  538. }
  539. // This is the label that return statements jump to
  540. // in order to exit the function
  541. byteCode.Label(0);
  542. // Call destructors for function parameters
  543. for( n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  544. {
  545. sVariable *v = variables->variables[n];
  546. if( v->stackOffset <= 0 )
  547. {
  548. // Call variable destructors here, for variables not yet destroyed
  549. if( v->name != "return" && v->name != "return address" )
  550. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  551. }
  552. // Do not deallocate parameters
  553. }
  554. // Release the object pointer again
  555. if( outFunc->objectType && !m_isConstructor && !outFunc->returnType.IsReference() )
  556. byteCode.InstrW_PTR(asBC_FREE, 0, outFunc->objectType);
  557. // Check if the number of labels in the functions isn't too many to be handled
  558. if( nextLabel >= (1<<15) )
  559. Error(TXT_TOO_MANY_JUMP_LABELS, func);
  560. // If there are compile errors, there is no reason to build the final code
  561. if( hasCompileErrors || builder->numErrors != buildErrors )
  562. return -1;
  563. // At this point there should be no variables allocated
  564. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  565. // Remove the variable scope
  566. RemoveVariableScope();
  567. byteCode.Ret(-stackPos);
  568. FinalizeFunction();
  569. #ifdef AS_DEBUG
  570. // DEBUG: output byte code
  571. if( outFunc->objectType )
  572. byteCode.DebugOutput(("__" + outFunc->objectType->name + "_" + outFunc->name + ".txt").AddressOf(), engine, outFunc);
  573. else
  574. byteCode.DebugOutput(("__" + outFunc->name + ".txt").AddressOf(), engine, outFunc);
  575. #endif
  576. return 0;
  577. }
  578. int asCCompiler::CallCopyConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asSExprContext *arg, asCScriptNode *node, bool isGlobalVar, bool derefDest)
  579. {
  580. if( !type.IsObject() )
  581. return 0;
  582. // CallCopyConstructor should not be called for object handles.
  583. asASSERT( !type.IsObjectHandle() );
  584. asCArray<asSExprContext*> args;
  585. args.PushLast(arg);
  586. // The reference parameter must be pushed on the stack
  587. asASSERT( arg->type.dataType.GetObjectType() == type.GetObjectType() );
  588. // Since we're calling the copy constructor, we have to trust the function to not do
  589. // anything stupid otherwise we will just enter a loop, as we try to make temporary
  590. // copies of the argument in order to guarantee safety.
  591. if( type.GetObjectType()->flags & asOBJ_REF )
  592. {
  593. asSExprContext ctx(engine);
  594. int func = 0;
  595. asSTypeBehaviour *beh = type.GetBehaviour();
  596. if( beh ) func = beh->copyfactory;
  597. if( func > 0 )
  598. {
  599. if( !isGlobalVar )
  600. {
  601. // Call factory and store the handle in the given variable
  602. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType(), true, offset);
  603. // Pop the reference left by the function call
  604. ctx.bc.Instr(asBC_PopPtr);
  605. }
  606. else
  607. {
  608. // Call factory
  609. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType());
  610. // Store the returned handle in the global variable
  611. ctx.bc.Instr(asBC_RDSPtr);
  612. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  613. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  614. ctx.bc.Instr(asBC_PopPtr);
  615. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  616. }
  617. bc->AddCode(&ctx.bc);
  618. return 0;
  619. }
  620. }
  621. else
  622. {
  623. asSTypeBehaviour *beh = type.GetBehaviour();
  624. int func = beh ? beh->copyconstruct : 0;
  625. if( func > 0 )
  626. {
  627. // Push the address where the object will be stored on the stack, before the argument
  628. // TODO: When the context is serializable this probably has to be changed, since this
  629. // pointer can remain on the stack while the context is suspended. There is no
  630. // risk the pointer becomes invalid though, there is just no easy way to serialize it.
  631. asCByteCode tmp(engine);
  632. if( isGlobalVar )
  633. tmp.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  634. else if( isObjectOnHeap )
  635. tmp.InstrSHORT(asBC_PSF, (short)offset);
  636. tmp.AddCode(bc);
  637. bc->AddCode(&tmp);
  638. // When the object is allocated on the stack the object pointer
  639. // must be pushed on the stack after the arguments
  640. if( !isObjectOnHeap )
  641. {
  642. asASSERT( !isGlobalVar );
  643. bc->InstrSHORT(asBC_PSF, (short)offset);
  644. if( derefDest )
  645. {
  646. // The variable is a reference to the real location, so we need to dereference it
  647. bc->Instr(asBC_RDSPtr);
  648. }
  649. }
  650. asSExprContext ctx(engine);
  651. PerformFunctionCall(func, &ctx, isObjectOnHeap, &args, type.GetObjectType());
  652. bc->AddCode(&ctx.bc);
  653. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  654. // Mark the object as initialized
  655. if( !isObjectOnHeap )
  656. bc->ObjInfo(offset, asOBJ_INIT);
  657. return 0;
  658. }
  659. }
  660. // Class has no copy constructor/factory.
  661. asCString str;
  662. str.Format(TXT_NO_COPY_CONSTRUCTOR_FOR_s, type.GetObjectType()->GetName());
  663. Error(str, node);
  664. return -1;
  665. }
  666. int asCCompiler::CallDefaultConstructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc, asCScriptNode *node, int isVarGlobOrMem, bool derefDest)
  667. {
  668. if( !type.IsObject() || type.IsObjectHandle() )
  669. return 0;
  670. if( type.GetObjectType()->flags & asOBJ_REF )
  671. {
  672. asSExprContext ctx(engine);
  673. ctx.exprNode = node;
  674. int func = 0;
  675. asSTypeBehaviour *beh = type.GetBehaviour();
  676. if( beh )
  677. {
  678. func = beh->factory;
  679. // If no trivial default factory is found, look for a factory where all params have default args
  680. if( func == 0 )
  681. {
  682. for( asUINT n = 0; n < beh->factories.GetLength(); n++ )
  683. {
  684. asCScriptFunction *f = engine->scriptFunctions[beh->factories[n]];
  685. if( f->defaultArgs.GetLength() == f->parameterTypes.GetLength() &&
  686. f->defaultArgs[0] != 0 )
  687. {
  688. func = beh->factories[n];
  689. break;
  690. }
  691. }
  692. }
  693. }
  694. if( func > 0 )
  695. {
  696. asCArray<asSExprContext *> args;
  697. asCScriptFunction *f = engine->scriptFunctions[func];
  698. if( f->parameterTypes.GetLength() )
  699. {
  700. // Add the default values for arguments not explicitly supplied
  701. CompileDefaultArgs(node, args, f);
  702. PrepareFunctionCall(func, &ctx.bc, args);
  703. MoveArgsToStack(func, &ctx.bc, args, false);
  704. }
  705. if( isVarGlobOrMem == 0 )
  706. {
  707. // Call factory and store the handle in the given variable
  708. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType(), true, offset);
  709. // Pop the reference left by the function call
  710. ctx.bc.Instr(asBC_PopPtr);
  711. }
  712. else
  713. {
  714. // Call factory
  715. PerformFunctionCall(func, &ctx, false, &args, type.GetObjectType());
  716. // TODO: runtime optimize: Should have a way of storing the object pointer directly to the destination
  717. // instead of first storing it in a local variable and then copying it to the
  718. // destination.
  719. if( !(type.GetObjectType()->flags & asOBJ_SCOPED) )
  720. {
  721. // Only dereference the variable if not a scoped type
  722. ctx.bc.Instr(asBC_RDSPtr);
  723. }
  724. if( isVarGlobOrMem == 1 )
  725. {
  726. // Store the returned handle in the global variable
  727. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  728. }
  729. else
  730. {
  731. // Store the returned handle in the class member
  732. ctx.bc.InstrSHORT(asBC_PSF, 0);
  733. ctx.bc.Instr(asBC_RDSPtr);
  734. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  735. }
  736. if( type.GetObjectType()->flags & asOBJ_SCOPED )
  737. {
  738. // For scoped typed we must move the reference from the local
  739. // variable rather than copy it as there is no AddRef behaviour
  740. ctx.bc.InstrSHORT_DW(asBC_COPY, AS_PTR_SIZE, asTYPEID_OBJHANDLE | engine->GetTypeIdFromDataType(type));
  741. // Clear the local variable so the reference isn't released
  742. ctx.bc.InstrSHORT(asBC_ClrVPtr, ctx.type.stackOffset);
  743. }
  744. else
  745. {
  746. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  747. }
  748. ctx.bc.Instr(asBC_PopPtr);
  749. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  750. }
  751. bc->AddCode(&ctx.bc);
  752. // Cleanup
  753. for( asUINT n = 0; n < args.GetLength(); n++ )
  754. if( args[n] )
  755. {
  756. asDELETE(args[n],asSExprContext);
  757. }
  758. return 0;
  759. }
  760. }
  761. else
  762. {
  763. asSExprContext ctx(engine);
  764. ctx.exprNode = node;
  765. asSTypeBehaviour *beh = type.GetBehaviour();
  766. int func = 0;
  767. if( beh )
  768. {
  769. func = beh->construct;
  770. // If no trivial default constructor is found, look for a constructor where all params have default args
  771. if( func == 0 )
  772. {
  773. for( asUINT n = 0; n < beh->constructors.GetLength(); n++ )
  774. {
  775. asCScriptFunction *f = engine->scriptFunctions[beh->constructors[n]];
  776. if( f->defaultArgs.GetLength() == f->parameterTypes.GetLength() &&
  777. f->defaultArgs[0] != 0 )
  778. {
  779. func = beh->constructors[n];
  780. break;
  781. }
  782. }
  783. }
  784. }
  785. // Allocate and initialize with the default constructor
  786. if( func != 0 || (type.GetObjectType()->flags & asOBJ_POD) )
  787. {
  788. asCArray<asSExprContext *> args;
  789. asCScriptFunction *f = engine->scriptFunctions[func];
  790. if( f && f->parameterTypes.GetLength() )
  791. {
  792. // Add the default values for arguments not explicitly supplied
  793. CompileDefaultArgs(node, args, f);
  794. PrepareFunctionCall(func, &ctx.bc, args);
  795. MoveArgsToStack(func, &ctx.bc, args, false);
  796. }
  797. if( !isObjectOnHeap )
  798. {
  799. if( isVarGlobOrMem == 0 )
  800. {
  801. // There is nothing to do if there is no function,
  802. // as the memory is already allocated on the stack
  803. if( func )
  804. {
  805. // Call the constructor as a normal function
  806. bc->InstrSHORT(asBC_PSF, (short)offset);
  807. if( derefDest )
  808. bc->Instr(asBC_RDSPtr);
  809. asSExprContext ctx(engine);
  810. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType());
  811. bc->AddCode(&ctx.bc);
  812. // TODO: value on stack: This probably needs to be done in PerformFunctionCall
  813. // Mark the object as initialized
  814. bc->ObjInfo(offset, asOBJ_INIT);
  815. }
  816. }
  817. else if( isVarGlobOrMem == 2 )
  818. {
  819. // Only POD types can be allocated inline in script classes
  820. asASSERT( type.GetObjectType()->flags & asOBJ_POD );
  821. if( func )
  822. {
  823. // Call the constructor as a normal function
  824. bc->InstrSHORT(asBC_PSF, 0);
  825. bc->Instr(asBC_RDSPtr);
  826. bc->InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  827. asSExprContext ctx(engine);
  828. PerformFunctionCall(func, &ctx, false, 0, type.GetObjectType());
  829. bc->AddCode(&ctx.bc);
  830. }
  831. }
  832. else
  833. {
  834. asASSERT( false );
  835. }
  836. }
  837. else
  838. {
  839. if( isVarGlobOrMem == 0 )
  840. bc->InstrSHORT(asBC_PSF, (short)offset);
  841. else if( isVarGlobOrMem == 1 )
  842. bc->InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  843. else
  844. {
  845. bc->InstrSHORT(asBC_PSF, 0);
  846. bc->Instr(asBC_RDSPtr);
  847. bc->InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  848. }
  849. bc->Alloc(asBC_ALLOC, type.GetObjectType(), func, AS_PTR_SIZE);
  850. }
  851. // Cleanup
  852. for( asUINT n = 0; n < args.GetLength(); n++ )
  853. if( args[n] )
  854. {
  855. asDELETE(args[n],asSExprContext);
  856. }
  857. return 0;
  858. }
  859. }
  860. // Class has no default factory/constructor.
  861. asCString str;
  862. // TODO: funcdef: asCDataType should have a GetTypeName()
  863. if( type.GetFuncDef() )
  864. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetFuncDef()->GetName());
  865. else
  866. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, type.GetObjectType()->GetName());
  867. Error(str, node);
  868. return -1;
  869. }
  870. void asCCompiler::CallDestructor(asCDataType &type, int offset, bool isObjectOnHeap, asCByteCode *bc)
  871. {
  872. if( !type.IsReference() )
  873. {
  874. // Call destructor for the data type
  875. if( type.IsObject() )
  876. {
  877. // Nothing is done for list pattern types, as this is taken care of by the CompileInitList method
  878. if( type.GetObjectType()->flags & asOBJ_LIST_PATTERN )
  879. return;
  880. if( isObjectOnHeap || type.IsObjectHandle() )
  881. {
  882. // Free the memory
  883. bc->InstrW_PTR(asBC_FREE, (short)offset, type.GetObjectType());
  884. }
  885. else
  886. {
  887. asASSERT( type.GetObjectType()->GetFlags() & asOBJ_VALUE );
  888. if( type.GetBehaviour()->destruct )
  889. {
  890. // Call the destructor as a regular function
  891. asSExprContext ctx(engine);
  892. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  893. PerformFunctionCall(type.GetBehaviour()->destruct, &ctx);
  894. ctx.bc.OptimizeLocally(tempVariableOffsets);
  895. bc->AddCode(&ctx.bc);
  896. }
  897. // TODO: Value on stack: This probably needs to be done in PerformFunctionCall
  898. // Mark the object as destroyed
  899. bc->ObjInfo(offset, asOBJ_UNINIT);
  900. }
  901. }
  902. }
  903. }
  904. void asCCompiler::LineInstr(asCByteCode *bc, size_t pos)
  905. {
  906. int r, c;
  907. script->ConvertPosToRowCol(pos, &r, &c);
  908. bc->Line(r, c, script->idx);
  909. }
  910. void asCCompiler::CompileStatementBlock(asCScriptNode *block, bool ownVariableScope, bool *hasReturn, asCByteCode *bc)
  911. {
  912. *hasReturn = false;
  913. bool isFinished = false;
  914. bool hasUnreachableCode = false;
  915. bool hasReturnBefore = false;
  916. if( ownVariableScope )
  917. {
  918. bc->Block(true);
  919. AddVariableScope();
  920. }
  921. asCScriptNode *node = block->firstChild;
  922. while( node )
  923. {
  924. if( !hasUnreachableCode && (*hasReturn || isFinished) )
  925. {
  926. // Empty statements don't count
  927. if( node->nodeType != snExpressionStatement || node->firstChild )
  928. {
  929. hasUnreachableCode = true;
  930. Warning(TXT_UNREACHABLE_CODE, node);
  931. }
  932. if( *hasReturn )
  933. hasReturnBefore = true;
  934. }
  935. if( node->nodeType == snBreak || node->nodeType == snContinue )
  936. isFinished = true;
  937. asCByteCode statement(engine);
  938. if( node->nodeType == snDeclaration )
  939. CompileDeclaration(node, &statement);
  940. else
  941. CompileStatement(node, hasReturn, &statement);
  942. // Ignore missing returns in unreachable code paths
  943. if( !(*hasReturn) && hasReturnBefore )
  944. *hasReturn = true;
  945. LineInstr(bc, node->tokenPos);
  946. bc->AddCode(&statement);
  947. if( !hasCompileErrors )
  948. {
  949. asASSERT( tempVariables.GetLength() == 0 );
  950. asASSERT( reservedVariables.GetLength() == 0 );
  951. }
  952. node = node->next;
  953. }
  954. if( ownVariableScope )
  955. {
  956. // Deallocate variables in this block, in reverse order
  957. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  958. {
  959. sVariable *v = variables->variables[n];
  960. // Call variable destructors here, for variables not yet destroyed
  961. // If the block is terminated with a break, continue, or
  962. // return the variables are already destroyed
  963. if( !isFinished && !*hasReturn )
  964. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  965. // Don't deallocate function parameters
  966. if( v->stackOffset > 0 )
  967. DeallocateVariable(v->stackOffset);
  968. }
  969. RemoveVariableScope();
  970. bc->Block(false);
  971. }
  972. }
  973. // Entry
  974. int asCCompiler::CompileGlobalVariable(asCBuilder *builder, asCScriptCode *script, asCScriptNode *node, sGlobalVariableDescription *gvar, asCScriptFunction *outFunc)
  975. {
  976. Reset(builder, script, outFunc);
  977. // Add a variable scope (even though variables can't be declared)
  978. AddVariableScope();
  979. gvar->isPureConstant = false;
  980. // Parse the initialization nodes
  981. asCParser parser(builder);
  982. if( node )
  983. {
  984. int r = parser.ParseVarInit(script, node);
  985. if( r < 0 )
  986. return r;
  987. node = parser.GetScriptNode();
  988. }
  989. // Compile the expression
  990. asSExprContext ctx(engine);
  991. asQWORD constantValue;
  992. if( CompileInitialization(node, &ctx.bc, gvar->datatype, gvar->declaredAtNode, gvar->index, &constantValue, 1) )
  993. {
  994. // Should the variable be marked as pure constant?
  995. if( gvar->datatype.IsPrimitive() && gvar->datatype.IsReadOnly() )
  996. {
  997. gvar->isPureConstant = true;
  998. gvar->constantValue = constantValue;
  999. }
  1000. }
  1001. // Concatenate the bytecode
  1002. int varSize = GetVariableOffset((int)variableAllocations.GetLength()) - 1;
  1003. // Add information on the line number for the global variable
  1004. size_t pos = 0;
  1005. if( gvar->declaredAtNode )
  1006. pos = gvar->declaredAtNode->tokenPos;
  1007. else if( gvar->initializationNode )
  1008. pos = gvar->initializationNode->tokenPos;
  1009. LineInstr(&byteCode, pos);
  1010. // Reserve space for all local variables
  1011. outFunc->scriptData->variableSpace = varSize;
  1012. ctx.bc.OptimizeLocally(tempVariableOffsets);
  1013. byteCode.AddCode(&ctx.bc);
  1014. // Deallocate variables in this block, in reverse order
  1015. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; --n )
  1016. {
  1017. sVariable *v = variables->variables[n];
  1018. // Call variable destructors here, for variables not yet destroyed
  1019. CallDestructor(v->type, v->stackOffset, v->onHeap, &byteCode);
  1020. DeallocateVariable(v->stackOffset);
  1021. }
  1022. if( hasCompileErrors ) return -1;
  1023. // At this point there should be no variables allocated
  1024. asASSERT(variableAllocations.GetLength() == freeVariables.GetLength());
  1025. // Remove the variable scope again
  1026. RemoveVariableScope();
  1027. byteCode.Ret(0);
  1028. FinalizeFunction();
  1029. #ifdef AS_DEBUG
  1030. // DEBUG: output byte code
  1031. byteCode.DebugOutput(("___init_" + gvar->name + ".txt").AddressOf(), engine, outFunc);
  1032. #endif
  1033. return 0;
  1034. }
  1035. void asCCompiler::DetermineSingleFunc(asSExprContext *ctx, asCScriptNode *node)
  1036. {
  1037. // Don't do anything if this is not a deferred global function
  1038. if( !ctx->IsGlobalFunc() )
  1039. return;
  1040. // Determine the namespace
  1041. asSNameSpace *ns = 0;
  1042. asCString name = "";
  1043. int pos = ctx->methodName.FindLast("::");
  1044. if( pos >= 0 )
  1045. {
  1046. asCString nsName = ctx->methodName.SubString(0, pos+2);
  1047. // Cut off the ::
  1048. if( nsName.GetLength() > 2 )
  1049. nsName.SetLength(nsName.GetLength()-2);
  1050. ns = DetermineNameSpace(nsName);
  1051. name = ctx->methodName.SubString(pos+2);
  1052. }
  1053. else
  1054. {
  1055. DetermineNameSpace("");
  1056. name = ctx->methodName;
  1057. }
  1058. asCArray<int> funcs;
  1059. if( ns )
  1060. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  1061. // CompileVariableAccess should guarantee that at least one function is exists
  1062. asASSERT( funcs.GetLength() > 0 );
  1063. if( funcs.GetLength() > 1 )
  1064. {
  1065. asCString str;
  1066. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, ctx->methodName.AddressOf());
  1067. Error(str, node);
  1068. // Fall through so the compiler can continue as if only one function was matching
  1069. }
  1070. // A shared object may not access global functions unless they too are shared (e.g. registered functions)
  1071. if( !builder->GetFunctionDescription(funcs[0])->IsShared() &&
  1072. outFunc->IsShared() )
  1073. {
  1074. asCString msg;
  1075. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, builder->GetFunctionDescription(funcs[0])->GetDeclaration());
  1076. Error(msg, node);
  1077. // Fall through so the compiler can continue anyway
  1078. }
  1079. // Push the function pointer on the stack
  1080. ctx->bc.InstrPTR(asBC_FuncPtr, builder->GetFunctionDescription(funcs[0]));
  1081. ctx->type.Set(asCDataType::CreateFuncDef(builder->GetFunctionDescription(funcs[0])));
  1082. ctx->type.dataType.MakeHandle(true);
  1083. ctx->type.isExplicitHandle = true;
  1084. ctx->methodName = "";
  1085. }
  1086. void asCCompiler::PrepareArgument(asCDataType *paramType, asSExprContext *ctx, asCScriptNode *node, bool isFunction, int refType, bool isMakingCopy)
  1087. {
  1088. asCDataType param = *paramType;
  1089. if( paramType->GetTokenType() == ttQuestion )
  1090. {
  1091. // The function is expecting a var type. If the argument is a function name, we must now decide which function it is
  1092. DetermineSingleFunc(ctx, node);
  1093. // Since the function is expecting a var type ?, then we don't want to convert the argument to anything else
  1094. param = ctx->type.dataType;
  1095. param.MakeHandle(ctx->type.isExplicitHandle || ctx->type.IsNullConstant());
  1096. // If value assign is disabled for reference types, then make
  1097. // sure to always pass the handle to ? parameters
  1098. if( builder->engine->ep.disallowValueAssignForRefType &&
  1099. ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_REF) && !(ctx->type.dataType.GetObjectType()->flags & asOBJ_SCOPED) )
  1100. {
  1101. param.MakeHandle(true);
  1102. }
  1103. param.MakeReference(paramType->IsReference());
  1104. param.MakeReadOnly(paramType->IsReadOnly());
  1105. }
  1106. else
  1107. param = *paramType;
  1108. asCDataType dt = param;
  1109. // Need to protect arguments by reference
  1110. if( isFunction && dt.IsReference() )
  1111. {
  1112. // Allocate a temporary variable of the same type as the argument
  1113. dt.MakeReference(false);
  1114. dt.MakeReadOnly(false);
  1115. int offset;
  1116. if( refType == 1 ) // &in
  1117. {
  1118. ProcessPropertyGetAccessor(ctx, node);
  1119. // Add the type id as hidden arg if the parameter is a ? type
  1120. if( paramType->GetTokenType() == ttQuestion )
  1121. {
  1122. asCByteCode tmpBC(engine);
  1123. // Place the type id on the stack as a hidden parameter
  1124. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1125. // Insert the code before the expression code
  1126. tmpBC.AddCode(&ctx->bc);
  1127. ctx->bc.AddCode(&tmpBC);
  1128. }
  1129. // If the reference is const, then it is not necessary to make a copy if the value already is a variable
  1130. // Even if the same variable is passed in another argument as non-const then there is no problem
  1131. if( dt.IsPrimitive() || dt.IsNullHandle() )
  1132. {
  1133. IsVariableInitialized(&ctx->type, node);
  1134. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1135. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV, true);
  1136. if( !(param.IsReadOnly() && ctx->type.isVariable) )
  1137. ConvertToTempVariable(ctx);
  1138. PushVariableOnStack(ctx, true);
  1139. ctx->type.dataType.MakeReadOnly(param.IsReadOnly());
  1140. }
  1141. else
  1142. {
  1143. IsVariableInitialized(&ctx->type, node);
  1144. if( !isMakingCopy )
  1145. {
  1146. ImplicitConversion(ctx, param, node, asIC_IMPLICIT_CONV, true);
  1147. if( !ctx->type.dataType.IsEqualExceptRef(param) )
  1148. {
  1149. asCString str;
  1150. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), param.Format().AddressOf());
  1151. Error(str, node);
  1152. ctx->type.Set(param);
  1153. }
  1154. }
  1155. // If the argument already is a temporary
  1156. // variable we don't need to allocate another
  1157. // If the parameter is read-only and the object already is a local
  1158. // variable then it is not necessary to make a copy either
  1159. if( !ctx->type.isTemporary && !(param.IsReadOnly() && ctx->type.isVariable) && !isMakingCopy )
  1160. {
  1161. // TODO: 2.28.1: Need to reserve variables, as the default constructor may need
  1162. // to allocate temporary variables to compute default args
  1163. // Make sure the variable is not used in the expression
  1164. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1165. // TODO: copy: Use copy constructor if available. See PrepareTemporaryObject()
  1166. // Allocate and construct the temporary object
  1167. asCByteCode tmpBC(engine);
  1168. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1169. // Insert the code before the expression code
  1170. tmpBC.AddCode(&ctx->bc);
  1171. ctx->bc.AddCode(&tmpBC);
  1172. // Assign the evaluated expression to the temporary variable
  1173. PrepareForAssignment(&dt, ctx, node, true);
  1174. dt.MakeReference(IsVariableOnHeap(offset));
  1175. asCTypeInfo type;
  1176. type.Set(dt);
  1177. type.isTemporary = true;
  1178. type.stackOffset = (short)offset;
  1179. if( dt.IsObjectHandle() )
  1180. type.isExplicitHandle = true;
  1181. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1182. PerformAssignment(&type, &ctx->type, &ctx->bc, node);
  1183. ctx->bc.Instr(asBC_PopPtr);
  1184. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  1185. ctx->type.Set(dt);
  1186. ctx->type.isTemporary = true;
  1187. ctx->type.stackOffset = offset;
  1188. if( dt.IsObjectHandle() )
  1189. ctx->type.isExplicitHandle = true;
  1190. ctx->type.dataType.MakeReference(false);
  1191. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1192. if( dt.IsObject() && !dt.IsObjectHandle() )
  1193. ctx->bc.Instr(asBC_RDSPtr);
  1194. if( paramType->IsReadOnly() )
  1195. ctx->type.dataType.MakeReadOnly(true);
  1196. }
  1197. else if( isMakingCopy )
  1198. {
  1199. // We must guarantee that the address to the value is on the stack
  1200. if( ctx->type.dataType.IsObject() &&
  1201. !ctx->type.dataType.IsObjectHandle() &&
  1202. ctx->type.dataType.IsReference() )
  1203. Dereference(ctx, true);
  1204. }
  1205. }
  1206. }
  1207. else if( refType == 2 ) // &out
  1208. {
  1209. // Add the type id as hidden arg if the parameter is a ? type
  1210. if( paramType->GetTokenType() == ttQuestion )
  1211. {
  1212. asCByteCode tmpBC(engine);
  1213. // Place the type id on the stack as a hidden parameter
  1214. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1215. // Insert the code before the expression code
  1216. tmpBC.AddCode(&ctx->bc);
  1217. ctx->bc.AddCode(&tmpBC);
  1218. }
  1219. // Make sure the variable is not used in the expression
  1220. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1221. if( dt.IsPrimitive() )
  1222. {
  1223. ctx->type.SetVariable(dt, offset, true);
  1224. PushVariableOnStack(ctx, true);
  1225. }
  1226. else
  1227. {
  1228. // TODO: 2.28.1: Need to reserve variables, as the default constructor may need
  1229. // to allocate temporary variables to compute default args
  1230. // Allocate and construct the temporary object
  1231. asCByteCode tmpBC(engine);
  1232. CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &tmpBC, node);
  1233. // Insert the code before the expression code
  1234. tmpBC.AddCode(&ctx->bc);
  1235. ctx->bc.AddCode(&tmpBC);
  1236. dt.MakeReference((!dt.IsObject() || dt.IsObjectHandle()));
  1237. asCTypeInfo type;
  1238. type.Set(dt);
  1239. type.isTemporary = true;
  1240. type.stackOffset = (short)offset;
  1241. ctx->type = type;
  1242. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  1243. if( dt.IsObject() && !dt.IsObjectHandle() )
  1244. ctx->bc.Instr(asBC_RDSPtr);
  1245. }
  1246. // After the function returns the temporary variable will
  1247. // be assigned to the expression, if it is a valid lvalue
  1248. }
  1249. else if( refType == asTM_INOUTREF )
  1250. {
  1251. ProcessPropertyGetAccessor(ctx, node);
  1252. // Add the type id as hidden arg if the parameter is a ? type
  1253. if( paramType->GetTokenType() == ttQuestion )
  1254. {
  1255. asCByteCode tmpBC(engine);
  1256. // Place the type id on the stack as a hidden parameter
  1257. tmpBC.InstrDWORD(asBC_TYPEID, engine->GetTypeIdFromDataType(param));
  1258. // Insert the code before the expression code
  1259. tmpBC.AddCode(&ctx->bc);
  1260. ctx->bc.AddCode(&tmpBC);
  1261. }
  1262. // Literal constants cannot be passed to inout ref arguments
  1263. if( !ctx->type.isVariable && ctx->type.isConstant )
  1264. {
  1265. Error(TXT_NOT_VALID_REFERENCE, node);
  1266. }
  1267. // Only objects that support object handles
  1268. // can be guaranteed to be safe. Local variables are
  1269. // already safe, so there is no need to add an extra
  1270. // references
  1271. if( !engine->ep.allowUnsafeReferences &&
  1272. !ctx->type.isVariable &&
  1273. ctx->type.dataType.IsObject() &&
  1274. !ctx->type.dataType.IsObjectHandle() &&
  1275. ((ctx->type.dataType.GetBehaviour()->addref &&
  1276. ctx->type.dataType.GetBehaviour()->release) ||
  1277. (ctx->type.dataType.GetObjectType()->flags & asOBJ_NOCOUNT)) )
  1278. {
  1279. // Store a handle to the object as local variable
  1280. asSExprContext tmp(engine);
  1281. asCDataType dt = ctx->type.dataType;
  1282. dt.MakeHandle(true);
  1283. dt.MakeReference(false);
  1284. offset = AllocateVariableNotIn(dt, true, false, ctx);
  1285. // Copy the handle
  1286. if( !ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReference() )
  1287. ctx->bc.Instr(asBC_RDSPtr);
  1288. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1289. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  1290. ctx->bc.Instr(asBC_PopPtr);
  1291. ctx->bc.InstrWORD(asBC_PSF, (asWORD)offset);
  1292. dt.MakeHandle(false);
  1293. dt.MakeReference(true);
  1294. // Release previous temporary variable stored in the context (if any)
  1295. if( ctx->type.isTemporary )
  1296. {
  1297. ReleaseTemporaryVariable(ctx->type.stackOffset, &ctx->bc);
  1298. }
  1299. ctx->type.SetVariable(dt, offset, true);
  1300. }
  1301. // Make sure the reference to the value is on the stack
  1302. // For objects, the reference needs to be dereferenced so the pointer on the stack is to the actual object
  1303. // For handles, the reference shouldn't be changed because the pointer on the stack should be to the handle
  1304. if( ctx->type.dataType.IsObject() && ctx->type.dataType.IsReference() && !param.IsObjectHandle() )
  1305. Dereference(ctx, true);
  1306. else if( ctx->type.isVariable && !ctx->type.dataType.IsObject() )
  1307. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  1308. else if( ctx->type.dataType.IsPrimitive() )
  1309. ctx->bc.Instr(asBC_PshRPtr);
  1310. else if( ctx->type.dataType.IsObjectHandle() && !ctx->type.dataType.IsReference() )
  1311. ImplicitConversion(ctx, param, node, asIC_IMPLICIT_CONV, true, false);
  1312. }
  1313. }
  1314. else
  1315. {
  1316. ProcessPropertyGetAccessor(ctx, node);
  1317. if( dt.IsPrimitive() )
  1318. {
  1319. IsVariableInitialized(&ctx->type, node);
  1320. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  1321. // Implicitly convert primitives to the parameter type
  1322. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1323. if( ctx->type.isVariable )
  1324. {
  1325. PushVariableOnStack(ctx, dt.IsReference());
  1326. }
  1327. else if( ctx->type.isConstant )
  1328. {
  1329. ConvertToVariable(ctx);
  1330. PushVariableOnStack(ctx, dt.IsReference());
  1331. }
  1332. }
  1333. else
  1334. {
  1335. IsVariableInitialized(&ctx->type, node);
  1336. // Implicitly convert primitives to the parameter type
  1337. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  1338. // Was the conversion successful?
  1339. if( !ctx->type.dataType.IsEqualExceptRef(dt) )
  1340. {
  1341. asCString str;
  1342. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), dt.Format().AddressOf());
  1343. Error(str, node);
  1344. ctx->type.Set(dt);
  1345. }
  1346. if( dt.IsObjectHandle() )
  1347. ctx->type.isExplicitHandle = true;
  1348. if( dt.IsObject() )
  1349. {
  1350. if( !dt.IsReference() )
  1351. {
  1352. // Objects passed by value must be placed in temporary variables
  1353. // so that they are guaranteed to not be referenced anywhere else.
  1354. // The object must also be allocated on the heap, as the memory will
  1355. // be deleted by in as_callfunc_xxx.
  1356. // TODO: value on stack: How can we avoid this unnecessary allocation?
  1357. // Local variables doesn't need to be copied into
  1358. // a temp if we're already compiling an assignment
  1359. if( !isMakingCopy || !ctx->type.dataType.IsObjectHandle() || !ctx->type.isVariable )
  1360. PrepareTemporaryObject(node, ctx, true);
  1361. // The implicit conversion shouldn't convert the object to
  1362. // non-reference yet. It will be dereferenced just before the call.
  1363. // Otherwise the object might be missed by the exception handler.
  1364. dt.MakeReference(true);
  1365. }
  1366. else
  1367. {
  1368. // An object passed by reference should place the pointer to
  1369. // the object on the stack.
  1370. dt.MakeReference(false);
  1371. }
  1372. }
  1373. }
  1374. }
  1375. // Don't put any pointer on the stack yet
  1376. if( param.IsReference() || param.IsObject() )
  1377. {
  1378. // &inout parameter may leave the reference on the stack already
  1379. if( refType != 3 )
  1380. {
  1381. asASSERT( ctx->type.isVariable || ctx->type.isTemporary || isMakingCopy );
  1382. if( ctx->type.isVariable || ctx->type.isTemporary )
  1383. {
  1384. ctx->bc.Instr(asBC_PopPtr);
  1385. ctx->bc.InstrSHORT(asBC_VAR, ctx->type.stackOffset);
  1386. ProcessDeferredParams(ctx);
  1387. }
  1388. }
  1389. }
  1390. }
  1391. void asCCompiler::PrepareFunctionCall(int funcId, asCByteCode *bc, asCArray<asSExprContext *> &args)
  1392. {
  1393. // When a match has been found, compile the final byte code using correct parameter types
  1394. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1395. // If the function being called is the opAssign or copy constructor for the same type
  1396. // as the argument, then we should avoid making temporary copy of the argument
  1397. bool makingCopy = false;
  1398. if( descr->parameterTypes.GetLength() == 1 &&
  1399. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1400. ((descr->name == "opAssign" && descr->objectType && descr->objectType == args[0]->type.dataType.GetObjectType()) ||
  1401. (args[0]->type.dataType.GetObjectType() && descr->name == args[0]->type.dataType.GetObjectType()->name)) )
  1402. makingCopy = true;
  1403. // Add code for arguments
  1404. asSExprContext e(engine);
  1405. for( int n = (int)args.GetLength()-1; n >= 0; n-- )
  1406. {
  1407. // Make sure PrepareArgument doesn't use any variable that is already
  1408. // being used by any of the following argument expressions
  1409. int l = int(reservedVariables.GetLength());
  1410. for( int m = n-1; m >= 0; m-- )
  1411. args[m]->bc.GetVarsUsed(reservedVariables);
  1412. PrepareArgument2(&e, args[n], &descr->parameterTypes[n], true, descr->inOutFlags[n], makingCopy);
  1413. reservedVariables.SetLength(l);
  1414. }
  1415. bc->AddCode(&e.bc);
  1416. }
  1417. void asCCompiler::MoveArgsToStack(int funcId, asCByteCode *bc, asCArray<asSExprContext *> &args, bool addOneToOffset)
  1418. {
  1419. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  1420. int offset = 0;
  1421. if( addOneToOffset )
  1422. offset += AS_PTR_SIZE;
  1423. // The address of where the return value should be stored is push on top of the arguments
  1424. if( descr->DoesReturnOnStack() )
  1425. offset += AS_PTR_SIZE;
  1426. #ifdef AS_DEBUG
  1427. // If the function being called is the opAssign or copy constructor for the same type
  1428. // as the argument, then we should avoid making temporary copy of the argument
  1429. bool makingCopy = false;
  1430. if( descr->parameterTypes.GetLength() == 1 &&
  1431. descr->parameterTypes[0].IsEqualExceptRefAndConst(args[0]->type.dataType) &&
  1432. ((descr->name == "opAssign" && descr->objectType && descr->objectType == args[0]->type.dataType.GetObjectType()) ||
  1433. (args[0]->type.dataType.GetObjectType() && descr->name == args[0]->type.dataType.GetObjectType()->name)) )
  1434. makingCopy = true;
  1435. #endif
  1436. // Move the objects that are sent by value to the stack just before the call
  1437. for( asUINT n = 0; n < descr->parameterTypes.GetLength(); n++ )
  1438. {
  1439. if( descr->parameterTypes[n].IsReference() )
  1440. {
  1441. if( descr->parameterTypes[n].IsObject() && !descr->parameterTypes[n].IsObjectHandle() )
  1442. {
  1443. if( descr->inOutFlags[n] != asTM_INOUTREF )
  1444. {
  1445. #ifdef AS_DEBUG
  1446. asASSERT( args[n]->type.isVariable || args[n]->type.isTemporary || makingCopy );
  1447. #endif
  1448. if( (args[n]->type.isVariable || args[n]->type.isTemporary) )
  1449. {
  1450. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1451. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1452. // as the value allocated on the stack is guaranteed to be safe
  1453. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1454. else
  1455. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1456. }
  1457. }
  1458. if( args[n]->type.dataType.IsObjectHandle() )
  1459. bc->InstrWORD(asBC_ChkNullS, (asWORD)offset);
  1460. }
  1461. else if( descr->inOutFlags[n] != asTM_INOUTREF )
  1462. {
  1463. if( descr->parameterTypes[n].GetTokenType() == ttQuestion &&
  1464. args[n]->type.dataType.IsObject() && !args[n]->type.dataType.IsObjectHandle() )
  1465. {
  1466. // Send the object as a reference to the object,
  1467. // and not to the variable holding the object
  1468. if( !IsVariableOnHeap(args[n]->type.stackOffset) )
  1469. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  1470. // as the value allocated on the stack is guaranteed to be safe
  1471. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1472. else
  1473. bc->InstrWORD(asBC_GETOBJREF, (asWORD)offset);
  1474. }
  1475. else
  1476. {
  1477. bc->InstrWORD(asBC_GETREF, (asWORD)offset);
  1478. }
  1479. }
  1480. }
  1481. else if( descr->parameterTypes[n].IsObject() )
  1482. {
  1483. // TODO: value on stack: What can we do to avoid this unnecessary allocation?
  1484. // The object must be allocated on the heap, because this memory will be deleted in as_callfunc_xxx
  1485. asASSERT(IsVariableOnHeap(args[n]->type.stackOffset));
  1486. bc->InstrWORD(asBC_GETOBJ, (asWORD)offset);
  1487. // The temporary variable must not be freed as it will no longer hold an object
  1488. DeallocateVariable(args[n]->type.stackOffset);
  1489. args[n]->type.isTemporary = false;
  1490. }
  1491. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  1492. }
  1493. }
  1494. int asCCompiler::CompileArgumentList(asCScriptNode *node, asCArray<asSExprContext*> &args)
  1495. {
  1496. asASSERT(node->nodeType == snArgList);
  1497. // Count arguments
  1498. asCScriptNode *arg = node->firstChild;
  1499. int argCount = 0;
  1500. while( arg )
  1501. {
  1502. argCount++;
  1503. arg = arg->next;
  1504. }
  1505. // Prepare the arrays
  1506. args.SetLength(argCount);
  1507. int n;
  1508. for( n = 0; n < argCount; n++ )
  1509. args[n] = 0;
  1510. n = argCount-1;
  1511. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1512. bool anyErrors = false;
  1513. arg = node->lastChild;
  1514. while( arg )
  1515. {
  1516. asSExprContext expr(engine);
  1517. int r = CompileAssignment(arg, &expr);
  1518. if( r < 0 ) anyErrors = true;
  1519. args[n] = asNEW(asSExprContext)(engine);
  1520. if( args[n] == 0 )
  1521. {
  1522. // Out of memory
  1523. return -1;
  1524. }
  1525. MergeExprBytecodeAndType(args[n], &expr);
  1526. n--;
  1527. arg = arg->prev;
  1528. }
  1529. return anyErrors ? -1 : 0;
  1530. }
  1531. int asCCompiler::CompileDefaultArgs(asCScriptNode *node, asCArray<asSExprContext*> &args, asCScriptFunction *func)
  1532. {
  1533. bool anyErrors = false;
  1534. asCArray<int> varsUsed;
  1535. int explicitArgs = (int)args.GetLength();
  1536. for( int p = 0; p < explicitArgs; p++ )
  1537. args[p]->bc.GetVarsUsed(varsUsed);
  1538. // Compile the arguments in reverse order (as they will be pushed on the stack)
  1539. args.SetLength(func->parameterTypes.GetLength());
  1540. for( asUINT c = explicitArgs; c < args.GetLength(); c++ )
  1541. args[c] = 0;
  1542. for( int n = (int)func->parameterTypes.GetLength() - 1; n >= explicitArgs; n-- )
  1543. {
  1544. if( func->defaultArgs[n] == 0 ) { anyErrors = true; continue; }
  1545. // Parse the default arg string
  1546. asCParser parser(builder);
  1547. asCScriptCode code;
  1548. code.SetCode("default arg", func->defaultArgs[n]->AddressOf(), false);
  1549. int r = parser.ParseExpression(&code);
  1550. if( r < 0 )
  1551. {
  1552. asCString msg;
  1553. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1554. Error(msg, node);
  1555. anyErrors = true;
  1556. continue;
  1557. }
  1558. asCScriptNode *arg = parser.GetScriptNode();
  1559. // Temporarily set the script code to the default arg expression
  1560. asCScriptCode *origScript = script;
  1561. script = &code;
  1562. // Don't allow the expression to access local variables
  1563. // TODO: namespace: The default arg should see the symbols declared in the same scope as the function that is called
  1564. isCompilingDefaultArg = true;
  1565. asSExprContext expr(engine);
  1566. r = CompileExpression(arg, &expr);
  1567. // Don't allow address of class method
  1568. if( expr.methodName != "" )
  1569. {
  1570. // TODO: Improve error message
  1571. Error(TXT_DEF_ARG_TYPE_DOESNT_MATCH, arg);
  1572. r = -1;
  1573. }
  1574. // Make sure the expression can be implicitly converted to the parameter type
  1575. if( r >= 0 )
  1576. {
  1577. asCArray<int> funcs;
  1578. funcs.PushLast(func->id);
  1579. asCArray<asSOverloadCandidate> matches;
  1580. if( MatchArgument(funcs, matches, &expr, n) == 0 )
  1581. {
  1582. Error(TXT_DEF_ARG_TYPE_DOESNT_MATCH, arg);
  1583. r = -1;
  1584. }
  1585. }
  1586. isCompilingDefaultArg = false;
  1587. script = origScript;
  1588. if( r < 0 )
  1589. {
  1590. asCString msg;
  1591. msg.Format(TXT_FAILED_TO_COMPILE_DEF_ARG_d_IN_FUNC_s, n, func->GetDeclaration());
  1592. Error(msg, node);
  1593. anyErrors = true;
  1594. continue;
  1595. }
  1596. args[n] = asNEW(asSExprContext)(engine);
  1597. if( args[n] == 0 )
  1598. {
  1599. // Out of memory
  1600. return -1;
  1601. }
  1602. MergeExprBytecodeAndType(args[n], &expr);
  1603. // Make sure the default arg expression doesn't end up
  1604. // with a variable that is used in a previous expression
  1605. if( args[n]->type.isVariable )
  1606. {
  1607. int offset = args[n]->type.stackOffset;
  1608. if( varsUsed.Exists(offset) )
  1609. {
  1610. // Release the current temporary variable
  1611. ReleaseTemporaryVariable(args[n]->type, 0);
  1612. asCDataType dt = args[n]->type.dataType;
  1613. dt.MakeReference(false);
  1614. // Reserve all variables already used in the expression so none of them will be used
  1615. asCArray<int> used;
  1616. args[n]->bc.GetVarsUsed(used);
  1617. size_t prevReserved = reservedVariables.GetLength();
  1618. reservedVariables.Concatenate(used);
  1619. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(offset));
  1620. asASSERT( IsVariableOnHeap(offset) == IsVariableOnHeap(newOffset) );
  1621. reservedVariables.SetLength(prevReserved);
  1622. // Replace the variable in the expression
  1623. args[n]->bc.ExchangeVar(offset, newOffset);
  1624. args[n]->type.stackOffset = (short)newOffset;
  1625. args[n]->type.isTemporary = true;
  1626. args[n]->type.isVariable = true;
  1627. }
  1628. }
  1629. }
  1630. return anyErrors ? -1 : 0;
  1631. }
  1632. asUINT asCCompiler::MatchFunctions(asCArray<int> &funcs, asCArray<asSExprContext*> &args, asCScriptNode *node, const char *name, asCObjectType *objectType, bool isConstMethod, bool silent, bool allowObjectConstruct, const asCString &scope)
  1633. {
  1634. asCArray<int> origFuncs = funcs; // Keep the original list for error message
  1635. asUINT cost = 0;
  1636. asUINT n;
  1637. if( funcs.GetLength() > 0 )
  1638. {
  1639. // Check the number of parameters in the found functions
  1640. for( n = 0; n < funcs.GetLength(); ++n )
  1641. {
  1642. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  1643. if( desc->parameterTypes.GetLength() != args.GetLength() )
  1644. {
  1645. bool noMatch = true;
  1646. if( args.GetLength() < desc->parameterTypes.GetLength() )
  1647. {
  1648. // For virtual functions, the default args are defined in the real function of the object
  1649. if( desc->funcType == asFUNC_VIRTUAL )
  1650. desc = objectType->virtualFunctionTable[desc->vfTableIdx];
  1651. // Count the number of default args
  1652. asUINT defaultArgs = 0;
  1653. for( asUINT d = 0; d < desc->defaultArgs.GetLength(); d++ )
  1654. if( desc->defaultArgs[d] )
  1655. defaultArgs++;
  1656. if( args.GetLength() >= desc->parameterTypes.GetLength() - defaultArgs )
  1657. noMatch = false;
  1658. }
  1659. if( noMatch )
  1660. {
  1661. // remove it from the list
  1662. if( n == funcs.GetLength()-1 )
  1663. funcs.PopLast();
  1664. else
  1665. funcs[n] = funcs.PopLast();
  1666. n--;
  1667. }
  1668. }
  1669. }
  1670. // Match functions with the parameters, and discard those that do not match
  1671. asCArray<asSOverloadCandidate> matchingFuncs;
  1672. matchingFuncs.SetLengthNoConstruct( funcs.GetLength() );
  1673. for ( n = 0; n < funcs.GetLength(); ++n )
  1674. {
  1675. matchingFuncs[n].funcId = funcs[n];
  1676. matchingFuncs[n].cost = 0;
  1677. }
  1678. for( n = 0; n < args.GetLength(); ++n )
  1679. {
  1680. asCArray<asSOverloadCandidate> tempFuncs;
  1681. MatchArgument(funcs, tempFuncs, args[n], n, allowObjectConstruct);
  1682. // Intersect the found functions with the list of matching functions
  1683. for( asUINT f = 0; f < matchingFuncs.GetLength(); f++ )
  1684. {
  1685. asUINT c;
  1686. for( c = 0; c < tempFuncs.GetLength(); c++ )
  1687. {
  1688. if( matchingFuncs[f].funcId == tempFuncs[c].funcId )
  1689. {
  1690. // Sum argument cost
  1691. matchingFuncs[f].cost += tempFuncs[c].cost;
  1692. break;
  1693. } // End if match
  1694. }
  1695. // Was the function a match?
  1696. if( c == tempFuncs.GetLength() )
  1697. {
  1698. // No, remove it from the list
  1699. if( f == matchingFuncs.GetLength()-1 )
  1700. matchingFuncs.PopLast();
  1701. else
  1702. matchingFuncs[f] = matchingFuncs.PopLast();
  1703. f--;
  1704. }
  1705. }
  1706. }
  1707. // Select the overload(s) with the lowest overall cost
  1708. funcs.SetLength(0);
  1709. asUINT bestCost = asUINT(-1);
  1710. for( n = 0; n < matchingFuncs.GetLength(); ++n )
  1711. {
  1712. cost = matchingFuncs[n].cost;
  1713. if( cost < bestCost )
  1714. {
  1715. funcs.SetLength(0);
  1716. bestCost = cost;
  1717. }
  1718. if( cost == bestCost )
  1719. funcs.PushLast( matchingFuncs[n].funcId );
  1720. }
  1721. // Cost returned is equivalent to the best cost discovered
  1722. cost = bestCost;
  1723. }
  1724. if( !isConstMethod )
  1725. FilterConst(funcs);
  1726. if( funcs.GetLength() != 1 && !silent )
  1727. {
  1728. // Build a readable string of the function with parameter types
  1729. asCString str;
  1730. if( scope != "" )
  1731. {
  1732. if( scope == "::" )
  1733. str = scope;
  1734. else
  1735. str = scope + "::";
  1736. }
  1737. str += name;
  1738. str += "(";
  1739. if( args.GetLength() )
  1740. {
  1741. if( args[0]->methodName != "" )
  1742. str += args[0]->methodName;
  1743. else
  1744. str += args[0]->type.dataType.Format();
  1745. }
  1746. for( n = 1; n < args.GetLength(); n++ )
  1747. {
  1748. str += ", ";
  1749. if( args[n]->methodName != "" )
  1750. str += args[n]->methodName;
  1751. else
  1752. str += args[n]->type.dataType.Format();
  1753. }
  1754. str += ")";
  1755. if( isConstMethod )
  1756. str += " const";
  1757. if( objectType && scope == "" )
  1758. str = objectType->name + "::" + str;
  1759. if( funcs.GetLength() == 0 )
  1760. {
  1761. str.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  1762. Error(str, node);
  1763. // Print the list of candidates
  1764. if( origFuncs.GetLength() > 0 )
  1765. {
  1766. int r = 0, c = 0;
  1767. asASSERT( node );
  1768. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  1769. builder->WriteInfo(script->name.AddressOf(), TXT_CANDIDATES_ARE, r, c, false);
  1770. PrintMatchingFuncs(origFuncs, node);
  1771. }
  1772. }
  1773. else
  1774. {
  1775. str.Format(TXT_MULTIPLE_MATCHING_SIGNATURES_TO_s, str.AddressOf());
  1776. Error(str, node);
  1777. PrintMatchingFuncs(funcs, node);
  1778. }
  1779. }
  1780. return cost;
  1781. }
  1782. void asCCompiler::CompileDeclaration(asCScriptNode *decl, asCByteCode *bc)
  1783. {
  1784. // Get the data type
  1785. asCDataType type = builder->CreateDataTypeFromNode(decl->firstChild, script, outFunc->nameSpace);
  1786. // Declare all variables in this declaration
  1787. asCScriptNode *node = decl->firstChild->next;
  1788. while( node )
  1789. {
  1790. // Is the type allowed?
  1791. if( !type.CanBeInstanciated() )
  1792. {
  1793. asCString str;
  1794. // TODO: Change to "'type' cannot be declared as variable"
  1795. str.Format(TXT_DATA_TYPE_CANT_BE_s, type.Format().AddressOf());
  1796. Error(str, node);
  1797. // Use int instead to avoid further problems
  1798. type = asCDataType::CreatePrimitive(ttInt, false);
  1799. }
  1800. // A shared object may not declare variables of non-shared types
  1801. if( outFunc->IsShared() )
  1802. {
  1803. asCObjectType *ot = type.GetObjectType();
  1804. if( ot && !ot->IsShared() )
  1805. {
  1806. asCString msg;
  1807. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, ot->name.AddressOf());
  1808. Error(msg, decl);
  1809. }
  1810. }
  1811. // Get the name of the identifier
  1812. asCString name(&script->code[node->tokenPos], node->tokenLength);
  1813. // Verify that the name isn't used by a dynamic data type
  1814. // TODO: Must check against registered funcdefs too
  1815. if( engine->GetRegisteredObjectType(name.AddressOf(), outFunc->nameSpace) != 0 )
  1816. {
  1817. asCString str;
  1818. str.Format(TXT_ILLEGAL_VARIABLE_NAME_s, name.AddressOf());
  1819. Error(str, node);
  1820. }
  1821. int offset = AllocateVariable(type, false);
  1822. if( variables->DeclareVariable(name.AddressOf(), type, offset, IsVariableOnHeap(offset)) < 0 )
  1823. {
  1824. // TODO: It might be an out-of-memory too
  1825. asCString str;
  1826. str.Format(TXT_s_ALREADY_DECLARED, name.AddressOf());
  1827. Error(str, node);
  1828. // Don't continue after this error, as it will just
  1829. // lead to more errors that are likely false
  1830. return;
  1831. }
  1832. // Add marker that the variable has been declared
  1833. bc->VarDecl((int)outFunc->scriptData->variables.GetLength());
  1834. outFunc->AddVariable(name, type, offset);
  1835. // Keep the node for the variable decl
  1836. asCScriptNode *varNode = node;
  1837. node = node->next;
  1838. if( node == 0 || node->nodeType == snIdentifier )
  1839. {
  1840. // Initialize with default constructor
  1841. CompileInitialization(0, bc, type, varNode, offset, 0, 0);
  1842. }
  1843. else
  1844. {
  1845. // Compile the initialization expression
  1846. asQWORD constantValue = 0;
  1847. if( CompileInitialization(node, bc, type, varNode, offset, &constantValue, 0) )
  1848. {
  1849. // Check if the variable should be marked as pure constant
  1850. if( type.IsPrimitive() && type.IsReadOnly() )
  1851. {
  1852. sVariable *v = variables->GetVariable(name.AddressOf());
  1853. v->isPureConstant = true;
  1854. v->constantValue = constantValue;
  1855. }
  1856. }
  1857. node = node->next;
  1858. }
  1859. }
  1860. bc->OptimizeLocally(tempVariableOffsets);
  1861. }
  1862. bool asCCompiler::CompileInitialization(asCScriptNode *node, asCByteCode *bc, asCDataType &type, asCScriptNode *errNode, int offset, asQWORD *constantValue, int isVarGlobOrMem)
  1863. {
  1864. bool isConstantExpression = false;
  1865. if( node && node->nodeType == snArgList )
  1866. {
  1867. // Make sure it is an object and not a handle
  1868. if( type.GetObjectType() == 0 || type.IsObjectHandle() )
  1869. {
  1870. Error(TXT_MUST_BE_OBJECT, node);
  1871. }
  1872. else
  1873. {
  1874. // Compile the arguments
  1875. asCArray<asSExprContext *> args;
  1876. if( CompileArgumentList(node, args) >= 0 )
  1877. {
  1878. // Find all constructors
  1879. asCArray<int> funcs;
  1880. asSTypeBehaviour *beh = type.GetBehaviour();
  1881. if( beh )
  1882. {
  1883. if( type.GetObjectType()->flags & asOBJ_REF )
  1884. funcs = beh->factories;
  1885. else
  1886. funcs = beh->constructors;
  1887. }
  1888. asCString str = type.Format();
  1889. MatchFunctions(funcs, args, node, str.AddressOf());
  1890. if( funcs.GetLength() == 1 )
  1891. {
  1892. int r = asSUCCESS;
  1893. // Add the default values for arguments not explicitly supplied
  1894. asCScriptFunction *func = (funcs[0] & FUNC_IMPORTED) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  1895. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  1896. r = CompileDefaultArgs(node, args, func);
  1897. if( r == asSUCCESS )
  1898. {
  1899. asSExprContext ctx(engine);
  1900. if( type.GetObjectType() && (type.GetObjectType()->flags & asOBJ_REF) )
  1901. {
  1902. if( isVarGlobOrMem == 0 )
  1903. MakeFunctionCall(&ctx, funcs[0], 0, args, node, true, offset);
  1904. else
  1905. {
  1906. MakeFunctionCall(&ctx, funcs[0], 0, args, node);
  1907. ctx.bc.Instr(asBC_RDSPtr);
  1908. if( isVarGlobOrMem == 1 )
  1909. {
  1910. // Store the returned handle in the global variable
  1911. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  1912. }
  1913. else
  1914. {
  1915. // Store the returned handle in the member
  1916. ctx.bc.InstrSHORT(asBC_PSF, 0);
  1917. ctx.bc.Instr(asBC_RDSPtr);
  1918. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  1919. }
  1920. ctx.bc.InstrPTR(asBC_REFCPY, type.GetObjectType());
  1921. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  1922. }
  1923. // Pop the reference left by the function call
  1924. ctx.bc.Instr(asBC_PopPtr);
  1925. }
  1926. else
  1927. {
  1928. bool onHeap = false;
  1929. if( isVarGlobOrMem == 0 )
  1930. {
  1931. // When the object is allocated on the heap, the address where the
  1932. // reference will be stored must be pushed on the stack before the
  1933. // arguments. This reference on the stack is safe, even if the script
  1934. // is suspended during the evaluation of the arguments.
  1935. onHeap = IsVariableOnHeap(offset);
  1936. if( onHeap )
  1937. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  1938. }
  1939. else if( isVarGlobOrMem == 1 )
  1940. {
  1941. // Push the address of the location where the variable will be stored on the stack.
  1942. // This reference is safe, because the addresses of the global variables cannot change.
  1943. onHeap = true;
  1944. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  1945. }
  1946. else
  1947. {
  1948. // Value types may be allocated inline if they are POD types
  1949. onHeap = !type.IsObject() || type.IsReference() || (type.GetObjectType()->flags & asOBJ_REF);
  1950. if( onHeap )
  1951. {
  1952. ctx.bc.InstrSHORT(asBC_PSF, 0);
  1953. ctx.bc.Instr(asBC_RDSPtr);
  1954. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  1955. }
  1956. }
  1957. PrepareFunctionCall(funcs[0], &ctx.bc, args);
  1958. MoveArgsToStack(funcs[0], &ctx.bc, args, false);
  1959. // When the object is allocated on the stack, the address to the
  1960. // object is pushed on the stack after the arguments as the object pointer
  1961. if( !onHeap )
  1962. {
  1963. if( isVarGlobOrMem == 2 )
  1964. {
  1965. ctx.bc.InstrSHORT(asBC_PSF, 0);
  1966. ctx.bc.Instr(asBC_RDSPtr);
  1967. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  1968. }
  1969. else
  1970. {
  1971. ctx.bc.InstrSHORT(asBC_PSF, (short)offset);
  1972. }
  1973. }
  1974. PerformFunctionCall(funcs[0], &ctx, onHeap, &args, type.GetObjectType());
  1975. if( isVarGlobOrMem == 0 )
  1976. {
  1977. // Mark the object in the local variable as initialized
  1978. ctx.bc.ObjInfo(offset, asOBJ_INIT);
  1979. }
  1980. }
  1981. bc->AddCode(&ctx.bc);
  1982. }
  1983. }
  1984. }
  1985. // Cleanup
  1986. for( asUINT n = 0; n < args.GetLength(); n++ )
  1987. if( args[n] )
  1988. {
  1989. asDELETE(args[n],asSExprContext);
  1990. }
  1991. }
  1992. }
  1993. else if( node && node->nodeType == snInitList )
  1994. {
  1995. asCTypeInfo ti;
  1996. ti.Set(type);
  1997. ti.isVariable = (isVarGlobOrMem == 0);
  1998. ti.isTemporary = false;
  1999. ti.stackOffset = (short)offset;
  2000. ti.isLValue = true;
  2001. CompileInitList(&ti, node, bc, isVarGlobOrMem);
  2002. }
  2003. else if( node && node->nodeType == snAssignment )
  2004. {
  2005. asSExprContext ctx(engine);
  2006. // TODO: copy: Here we should look for the best matching constructor, instead of
  2007. // just the copy constructor. Only if no appropriate constructor is
  2008. // available should the assignment operator be used.
  2009. // Call the default constructor here
  2010. if( isVarGlobOrMem == 0 )
  2011. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), &ctx.bc, errNode);
  2012. else if( isVarGlobOrMem == 1 )
  2013. CallDefaultConstructor(type, offset, true, &ctx.bc, errNode, isVarGlobOrMem);
  2014. else if( isVarGlobOrMem == 2 )
  2015. CallDefaultConstructor(type, offset, type.IsReference(), &ctx.bc, errNode, isVarGlobOrMem);
  2016. // Compile the expression
  2017. asSExprContext expr(engine);
  2018. int r = CompileAssignment(node, &expr);
  2019. if( r >= 0 )
  2020. {
  2021. if( type.IsPrimitive() )
  2022. {
  2023. if( type.IsReadOnly() && expr.type.isConstant )
  2024. {
  2025. ImplicitConversion(&expr, type, node, asIC_IMPLICIT_CONV);
  2026. // Tell caller that the expression is a constant so it can mark the variable as pure constant
  2027. isConstantExpression = true;
  2028. *constantValue = expr.type.qwordValue;
  2029. }
  2030. asSExprContext lctx(engine);
  2031. if( isVarGlobOrMem == 0 )
  2032. lctx.type.SetVariable(type, offset, false);
  2033. else if( isVarGlobOrMem == 1 )
  2034. {
  2035. lctx.type.Set(type);
  2036. lctx.type.dataType.MakeReference(true);
  2037. // If it is an enum value, i.e. offset is negative, that is being compiled then
  2038. // we skip this as the bytecode won't be used anyway, only the constant value
  2039. if( offset >= 0 )
  2040. lctx.bc.InstrPTR(asBC_LDG, engine->globalProperties[offset]->GetAddressOfValue());
  2041. }
  2042. else
  2043. {
  2044. asASSERT( isVarGlobOrMem == 2 );
  2045. lctx.type.Set(type);
  2046. lctx.type.dataType.MakeReference(true);
  2047. // Load the reference of the primitive member into the register
  2048. lctx.bc.InstrSHORT(asBC_PSF, 0);
  2049. lctx.bc.Instr(asBC_RDSPtr);
  2050. lctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  2051. lctx.bc.Instr(asBC_PopRPtr);
  2052. }
  2053. lctx.type.dataType.MakeReadOnly(false);
  2054. lctx.type.isLValue = true;
  2055. DoAssignment(&ctx, &lctx, &expr, node, node, ttAssignment, node);
  2056. ProcessDeferredParams(&ctx);
  2057. }
  2058. else
  2059. {
  2060. // TODO: runtime optimize: Here we should look for the best matching constructor, instead of
  2061. // just the copy constructor. Only if no appropriate constructor is
  2062. // available should the assignment operator be used.
  2063. asSExprContext lexpr(engine);
  2064. lexpr.type.Set(type);
  2065. if( isVarGlobOrMem == 0 )
  2066. lexpr.type.dataType.MakeReference(IsVariableOnHeap(offset));
  2067. else if( isVarGlobOrMem == 1 )
  2068. lexpr.type.dataType.MakeReference(true);
  2069. else if( isVarGlobOrMem == 2 )
  2070. {
  2071. if( !lexpr.type.dataType.IsObject() || (lexpr.type.dataType.GetObjectType()->flags & asOBJ_REF) )
  2072. lexpr.type.dataType.MakeReference(true);
  2073. }
  2074. // Allow initialization of constant variables
  2075. lexpr.type.dataType.MakeReadOnly(false);
  2076. if( type.IsObjectHandle() )
  2077. lexpr.type.isExplicitHandle = true;
  2078. if( isVarGlobOrMem == 0 )
  2079. {
  2080. lexpr.bc.InstrSHORT(asBC_PSF, (short)offset);
  2081. lexpr.type.stackOffset = (short)offset;
  2082. lexpr.type.isVariable = true;
  2083. }
  2084. else if( isVarGlobOrMem == 1 )
  2085. {
  2086. lexpr.bc.InstrPTR(asBC_PGA, engine->globalProperties[offset]->GetAddressOfValue());
  2087. }
  2088. else
  2089. {
  2090. lexpr.bc.InstrSHORT(asBC_PSF, 0);
  2091. lexpr.bc.Instr(asBC_RDSPtr);
  2092. lexpr.bc.InstrSHORT_DW(asBC_ADDSi, (short)offset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  2093. lexpr.type.stackOffset = -1;
  2094. }
  2095. lexpr.type.isLValue = true;
  2096. // If left expression resolves into a registered type
  2097. // check if the assignment operator is overloaded, and check
  2098. // the type of the right hand expression. If none is found
  2099. // the default action is a direct copy if it is the same type
  2100. // and a simple assignment.
  2101. bool assigned = false;
  2102. // Even though an ASHANDLE can be an explicit handle the overloaded operator needs to be called
  2103. if( lexpr.type.dataType.IsObject() && (!lexpr.type.isExplicitHandle || (lexpr.type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  2104. {
  2105. assigned = CompileOverloadedDualOperator(node, &lexpr, &expr, &ctx);
  2106. if( assigned )
  2107. {
  2108. // Pop the resulting value
  2109. if( !ctx.type.dataType.IsPrimitive() )
  2110. ctx.bc.Instr(asBC_PopPtr);
  2111. // Release the argument
  2112. ProcessDeferredParams(&ctx);
  2113. // Release temporary variable that may be allocated by the overloaded operator
  2114. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  2115. }
  2116. }
  2117. if( !assigned )
  2118. {
  2119. PrepareForAssignment(&lexpr.type.dataType, &expr, node, false);
  2120. // If the expression is constant and the variable also is constant
  2121. // then mark the variable as pure constant. This will allow the compiler
  2122. // to optimize expressions with this variable.
  2123. if( type.IsReadOnly() && expr.type.isConstant )
  2124. {
  2125. isConstantExpression = true;
  2126. *constantValue = expr.type.qwordValue;
  2127. }
  2128. // Add expression code to bytecode
  2129. MergeExprBytecode(&ctx, &expr);
  2130. // Add byte code for storing value of expression in variable
  2131. ctx.bc.AddCode(&lexpr.bc);
  2132. PerformAssignment(&lexpr.type, &expr.type, &ctx.bc, errNode);
  2133. // Release temporary variables used by expression
  2134. ReleaseTemporaryVariable(expr.type, &ctx.bc);
  2135. ctx.bc.Instr(asBC_PopPtr);
  2136. ProcessDeferredParams(&ctx);
  2137. }
  2138. }
  2139. }
  2140. bc->AddCode(&ctx.bc);
  2141. }
  2142. else
  2143. {
  2144. asASSERT( node == 0 );
  2145. // Call the default constructor here, as no explicit initialization is done
  2146. if( isVarGlobOrMem == 0 )
  2147. CallDefaultConstructor(type, offset, IsVariableOnHeap(offset), bc, errNode);
  2148. else if( isVarGlobOrMem == 1 )
  2149. CallDefaultConstructor(type, offset, true, bc, errNode, isVarGlobOrMem);
  2150. else if( isVarGlobOrMem == 2 )
  2151. {
  2152. if( !type.IsObject() || type.IsReference() || (type.GetObjectType()->flags & asOBJ_REF) )
  2153. CallDefaultConstructor(type, offset, true, bc, errNode, isVarGlobOrMem);
  2154. else
  2155. CallDefaultConstructor(type, offset, false, bc, errNode, isVarGlobOrMem);
  2156. }
  2157. }
  2158. bc->OptimizeLocally(tempVariableOffsets);
  2159. return isConstantExpression;
  2160. }
  2161. void asCCompiler::CompileInitList(asCTypeInfo *var, asCScriptNode *node, asCByteCode *bc, int isVarGlobOrMem)
  2162. {
  2163. // Check if the type supports initialization lists
  2164. if( var->dataType.GetObjectType() == 0 ||
  2165. var->dataType.GetBehaviour()->listFactory == 0 ||
  2166. var->dataType.IsObjectHandle() )
  2167. {
  2168. asCString str;
  2169. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, var->dataType.Format().AddressOf());
  2170. Error(str, node);
  2171. return;
  2172. }
  2173. // Construct the buffer with the elements
  2174. // Find the list factory
  2175. int funcId = var->dataType.GetBehaviour()->listFactory;
  2176. asASSERT( engine->scriptFunctions[funcId]->listPattern );
  2177. // TODO: runtime optimize: A future optimization should be to use the stack space directly
  2178. // for small buffers so that the dynamic allocation is skipped
  2179. // Create a new special object type for the lists. Both asCRestore and the
  2180. // context exception handler will need this to know how to parse the buffer.
  2181. asCObjectType *listPatternType = engine->GetListPatternType(funcId);
  2182. // Allocate a temporary variable to hold the pointer to the buffer
  2183. int bufferVar = AllocateVariable(asCDataType::CreateObject(listPatternType, false), true);
  2184. asUINT bufferSize = 0;
  2185. // Evaluate all elements of the list
  2186. asSExprContext valueExpr(engine);
  2187. asCScriptNode *el = node;
  2188. asSListPatternNode *patternNode = engine->scriptFunctions[listPatternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
  2189. int r = CompileInitListElement(patternNode, el, engine->GetTypeIdFromDataType(asCDataType::CreateObject(listPatternType, false)), bufferVar, bufferSize, valueExpr.bc);
  2190. asASSERT( r || patternNode == 0 );
  2191. UNUSED_VAR(r);
  2192. // After all values have been evaluated we know the final size of the buffer
  2193. asSExprContext allocExpr(engine);
  2194. allocExpr.bc.InstrSHORT_DW(asBC_AllocMem, bufferVar, bufferSize);
  2195. // Merge the bytecode into the final sequence
  2196. bc->AddCode(&allocExpr.bc);
  2197. bc->AddCode(&valueExpr.bc);
  2198. // The object itself is the last to be created and will receive the pointer to the buffer
  2199. asCArray<asSExprContext *> args;
  2200. asSExprContext arg1(engine);
  2201. arg1.type.Set(asCDataType::CreatePrimitive(ttUInt, false));
  2202. arg1.type.dataType.MakeReference(true);
  2203. arg1.bc.InstrSHORT(asBC_PshVPtr, bufferVar);
  2204. args.PushLast(&arg1);
  2205. asSExprContext ctx(engine);
  2206. if( var->isVariable )
  2207. {
  2208. asASSERT( isVarGlobOrMem == 0 );
  2209. if( var->dataType.GetObjectType()->GetFlags() & asOBJ_REF )
  2210. {
  2211. ctx.bc.AddCode(&arg1.bc);
  2212. // Call factory and store the handle in the given variable
  2213. PerformFunctionCall(funcId, &ctx, false, &args, 0, true, var->stackOffset);
  2214. ctx.bc.Instr(asBC_PopPtr);
  2215. }
  2216. else
  2217. {
  2218. // Call the constructor
  2219. // When the object is allocated on the heap, the address where the
  2220. // reference will be stored must be pushed on the stack before the
  2221. // arguments. This reference on the stack is safe, even if the script
  2222. // is suspended during the evaluation of the arguments.
  2223. bool onHeap = IsVariableOnHeap(var->stackOffset);
  2224. if( onHeap )
  2225. ctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  2226. ctx.bc.AddCode(&arg1.bc);
  2227. // When the object is allocated on the stack, the address to the
  2228. // object is pushed on the stack after the arguments as the object pointer
  2229. if( !onHeap )
  2230. ctx.bc.InstrSHORT(asBC_PSF, var->stackOffset);
  2231. PerformFunctionCall(funcId, &ctx, onHeap, &args, var->dataType.GetObjectType());
  2232. // Mark the object in the local variable as initialized
  2233. ctx.bc.ObjInfo(var->stackOffset, asOBJ_INIT);
  2234. }
  2235. }
  2236. else
  2237. {
  2238. if( var->dataType.GetObjectType()->GetFlags() & asOBJ_REF )
  2239. {
  2240. ctx.bc.AddCode(&arg1.bc);
  2241. PerformFunctionCall(funcId, &ctx, false, &args);
  2242. ctx.bc.Instr(asBC_RDSPtr);
  2243. if( isVarGlobOrMem == 1 )
  2244. {
  2245. // Store the returned handle in the global variable
  2246. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  2247. }
  2248. else
  2249. {
  2250. // Store the returned handle in the member
  2251. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2252. ctx.bc.Instr(asBC_RDSPtr);
  2253. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  2254. }
  2255. ctx.bc.InstrPTR(asBC_REFCPY, var->dataType.GetObjectType());
  2256. ctx.bc.Instr(asBC_PopPtr);
  2257. ReleaseTemporaryVariable(ctx.type.stackOffset, &ctx.bc);
  2258. }
  2259. else
  2260. {
  2261. bool onHeap = true;
  2262. // Put the address where the object pointer will be placed on the stack
  2263. if( isVarGlobOrMem == 1 )
  2264. ctx.bc.InstrPTR(asBC_PGA, engine->globalProperties[var->stackOffset]->GetAddressOfValue());
  2265. else
  2266. {
  2267. onHeap = !var->dataType.IsObject() || var->dataType.IsReference() || (var->dataType.GetObjectType()->flags & asOBJ_REF);
  2268. if( onHeap )
  2269. {
  2270. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2271. ctx.bc.Instr(asBC_RDSPtr);
  2272. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  2273. }
  2274. }
  2275. // Add the address of the list buffer as the argument
  2276. ctx.bc.AddCode(&arg1.bc);
  2277. if( !onHeap )
  2278. {
  2279. ctx.bc.InstrSHORT(asBC_PSF, 0);
  2280. ctx.bc.Instr(asBC_RDSPtr);
  2281. ctx.bc.InstrSHORT_DW(asBC_ADDSi, (short)var->stackOffset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(outFunc->objectType, false)));
  2282. }
  2283. // Call the ALLOC instruction to allocate memory and invoke constructor
  2284. PerformFunctionCall(funcId, &ctx, onHeap, &args, var->dataType.GetObjectType());
  2285. }
  2286. }
  2287. bc->AddCode(&ctx.bc);
  2288. // Free the temporary buffer. The FREE instruction will make sure to destroy
  2289. // each element in the buffer so there is no need to do this manually
  2290. bc->InstrW_PTR(asBC_FREE, bufferVar, listPatternType);
  2291. ReleaseTemporaryVariable(bufferVar, bc);
  2292. }
  2293. int asCCompiler::CompileInitListElement(asSListPatternNode *&patternNode, asCScriptNode *&valueNode, int bufferTypeId, short bufferVar, asUINT &bufferSize, asCByteCode &byteCode)
  2294. {
  2295. if( patternNode->type == asLPT_START )
  2296. {
  2297. if( valueNode->nodeType != snInitList )
  2298. {
  2299. Error(TXT_EXPECTED_LIST, valueNode);
  2300. return -1;
  2301. }
  2302. // Compile all values until asLPT_END
  2303. patternNode = patternNode->next;
  2304. asCScriptNode *node = valueNode->firstChild;
  2305. while( patternNode->type != asLPT_END )
  2306. {
  2307. if( node == 0 )
  2308. {
  2309. Error(TXT_NOT_ENOUGH_VALUES_FOR_LIST, valueNode);
  2310. return -1;
  2311. }
  2312. int r = CompileInitListElement(patternNode, node, bufferTypeId, bufferVar, bufferSize, byteCode);
  2313. if( r < 0 ) return r;
  2314. asASSERT( patternNode );
  2315. }
  2316. if( node )
  2317. {
  2318. Error(TXT_TOO_MANY_VALUES_FOR_LIST, valueNode);
  2319. return -1;
  2320. }
  2321. // Move to the next node
  2322. valueNode = valueNode->next;
  2323. patternNode = patternNode->next;
  2324. }
  2325. else if( patternNode->type == asLPT_REPEAT )
  2326. {
  2327. // The following values will be repeated N times
  2328. patternNode = patternNode->next;
  2329. // Keep track of the patternNode so it can be reset
  2330. asSListPatternNode *nextNode = patternNode;
  2331. // Align the buffer size to 4 bytes in case previous value was smaller than 4 bytes
  2332. if( bufferSize & 0x3 )
  2333. bufferSize += 4 - (bufferSize & 0x3);
  2334. // The first dword will hold the number of elements in the list
  2335. asDWORD currSize = bufferSize;
  2336. bufferSize += 4;
  2337. asUINT countElements = 0;
  2338. asSExprContext ctx(engine);
  2339. while( valueNode )
  2340. {
  2341. patternNode = nextNode;
  2342. int r = CompileInitListElement(patternNode, valueNode, bufferTypeId, bufferVar, bufferSize, ctx.bc);
  2343. if( r < 0 ) return r;
  2344. countElements++;
  2345. }
  2346. // The first dword in the buffer will hold the number of elements
  2347. byteCode.InstrSHORT_DW_DW(asBC_SetListSize, bufferVar, currSize, countElements);
  2348. // Add the values
  2349. byteCode.AddCode(&ctx.bc);
  2350. }
  2351. else if( patternNode->type == asLPT_TYPE )
  2352. {
  2353. // Determine the size of the element
  2354. asUINT size = 0;
  2355. asCDataType dt = reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType;
  2356. if( valueNode->nodeType == snAssignment || valueNode->nodeType == snInitList )
  2357. {
  2358. asSExprContext lctx(engine);
  2359. asSExprContext rctx(engine);
  2360. if( valueNode->nodeType == snAssignment )
  2361. {
  2362. // Compile the assignment expression
  2363. CompileAssignment(valueNode, &rctx);
  2364. if( dt.GetTokenType() == ttQuestion )
  2365. {
  2366. // We now know the type
  2367. dt = rctx.type.dataType;
  2368. dt.MakeReadOnly(false);
  2369. dt.MakeReference(false);
  2370. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  2371. if( bufferSize & 0x3 )
  2372. bufferSize += 4 - (bufferSize & 0x3);
  2373. // Place the type id in the buffer
  2374. byteCode.InstrSHORT_DW_DW(asBC_SetListType, bufferVar, bufferSize, engine->GetTypeIdFromDataType(dt));
  2375. bufferSize += 4;
  2376. }
  2377. }
  2378. else if( valueNode->nodeType == snInitList )
  2379. {
  2380. if( dt.GetTokenType() == ttQuestion )
  2381. {
  2382. // Can't use init lists with var type as it is not possible to determine what type should be allocated
  2383. asCString str;
  2384. str.Format(TXT_INIT_LIST_CANNOT_BE_USED_WITH_s, "?");
  2385. Error(str.AddressOf(), valueNode);
  2386. rctx.type.SetDummy();
  2387. dt = rctx.type.dataType;
  2388. }
  2389. else
  2390. {
  2391. // Allocate a temporary variable that will be initialized with the list
  2392. int offset = AllocateVariable(dt, true);
  2393. rctx.type.Set(dt);
  2394. rctx.type.isVariable = true;
  2395. rctx.type.isTemporary = true;
  2396. rctx.type.stackOffset = (short)offset;
  2397. CompileInitList(&rctx.type, valueNode, &rctx.bc, 0);
  2398. // Put the object on the stack
  2399. rctx.bc.InstrSHORT(asBC_PSF, rctx.type.stackOffset);
  2400. // It is a reference that we place on the stack
  2401. rctx.type.dataType.MakeReference(true);
  2402. }
  2403. }
  2404. // Determine size of the element
  2405. if( dt.IsPrimitive() || (!dt.IsNullHandle() && (dt.GetObjectType()->flags & asOBJ_VALUE)) )
  2406. size = dt.GetSizeInMemoryBytes();
  2407. else
  2408. size = AS_PTR_SIZE*4;
  2409. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  2410. if( size >= 4 && (bufferSize & 0x3) )
  2411. bufferSize += 4 - (bufferSize & 0x3);
  2412. // Compile the lvalue
  2413. lctx.bc.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  2414. lctx.type.Set(dt);
  2415. lctx.type.isLValue = true;
  2416. if( dt.IsPrimitive() )
  2417. {
  2418. lctx.bc.Instr(asBC_PopRPtr);
  2419. lctx.type.dataType.MakeReference(true);
  2420. }
  2421. else if( dt.IsObjectHandle() ||
  2422. dt.GetObjectType()->flags & asOBJ_REF )
  2423. {
  2424. lctx.type.isExplicitHandle = true;
  2425. lctx.type.dataType.MakeReference(true);
  2426. }
  2427. else
  2428. {
  2429. asASSERT( dt.GetObjectType()->flags & asOBJ_VALUE );
  2430. // Make sure the object has been constructed before the assignment
  2431. // TODO: runtime optimize: Use copy constructor instead of assignment to initialize the objects
  2432. asSTypeBehaviour *beh = dt.GetBehaviour();
  2433. int func = 0;
  2434. if( beh ) func = beh->construct;
  2435. if( func == 0 && (dt.GetObjectType()->flags & asOBJ_POD) == 0 )
  2436. {
  2437. asCString str;
  2438. // TODO: funcdef: asCDataType should have a GetTypeName()
  2439. if( dt.GetFuncDef() )
  2440. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetFuncDef()->GetName());
  2441. else
  2442. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetObjectType()->GetName());
  2443. Error(str, valueNode);
  2444. }
  2445. else if( func )
  2446. {
  2447. // Call the constructor as a normal function
  2448. byteCode.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  2449. asSExprContext ctx(engine);
  2450. PerformFunctionCall(func, &ctx, false, 0, dt.GetObjectType());
  2451. byteCode.AddCode(&ctx.bc);
  2452. }
  2453. }
  2454. asSExprContext ctx(engine);
  2455. DoAssignment(&ctx, &lctx, &rctx, valueNode, valueNode, ttAssignment, valueNode);
  2456. if( !lctx.type.dataType.IsPrimitive() )
  2457. ctx.bc.Instr(asBC_PopPtr);
  2458. // Release temporary variables used by expression
  2459. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  2460. ProcessDeferredParams(&ctx);
  2461. byteCode.AddCode(&ctx.bc);
  2462. }
  2463. else
  2464. {
  2465. // There is no specific value so we need to fill it with a default value
  2466. if( dt.GetTokenType() == ttQuestion )
  2467. {
  2468. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  2469. if( bufferSize & 0x3 )
  2470. bufferSize += 4 - (bufferSize & 0x3);
  2471. // Place the type id for a null handle in the buffer
  2472. byteCode.InstrSHORT_DW_DW(asBC_SetListType, bufferVar, bufferSize, 0);
  2473. bufferSize += 4;
  2474. dt = asCDataType::CreateNullHandle();
  2475. // No need to initialize the handle as the buffer is already initialized with zeroes
  2476. }
  2477. else if( dt.GetObjectType() && dt.GetObjectType()->flags & asOBJ_VALUE )
  2478. {
  2479. // For value types with default constructor we need to call the constructor
  2480. asSTypeBehaviour *beh = dt.GetBehaviour();
  2481. int func = 0;
  2482. if( beh ) func = beh->construct;
  2483. if( func == 0 && (dt.GetObjectType()->flags & asOBJ_POD) == 0 )
  2484. {
  2485. asCString str;
  2486. // TODO: funcdef: asCDataType should have a GetTypeName()
  2487. if( dt.GetFuncDef() )
  2488. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetFuncDef()->GetName());
  2489. else
  2490. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetObjectType()->GetName());
  2491. Error(str, valueNode);
  2492. }
  2493. else if( func )
  2494. {
  2495. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  2496. if( bufferSize & 0x3 )
  2497. bufferSize += 4 - (bufferSize & 0x3);
  2498. // Call the constructor as a normal function
  2499. byteCode.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  2500. asSExprContext ctx(engine);
  2501. PerformFunctionCall(func, &ctx, false, 0, dt.GetObjectType());
  2502. byteCode.AddCode(&ctx.bc);
  2503. }
  2504. }
  2505. else if( !dt.IsObjectHandle() && dt.GetObjectType() && dt.GetObjectType()->flags & asOBJ_REF )
  2506. {
  2507. // For ref types (not handles) we need to call the default factory
  2508. asSTypeBehaviour *beh = dt.GetBehaviour();
  2509. int func = 0;
  2510. if( beh ) func = beh->factory;
  2511. if( func == 0 )
  2512. {
  2513. asCString str;
  2514. // TODO: funcdef: asCDataType should have a GetTypeName()
  2515. if( dt.GetFuncDef() )
  2516. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetFuncDef()->GetName());
  2517. else
  2518. str.Format(TXT_NO_DEFAULT_CONSTRUCTOR_FOR_s, dt.GetObjectType()->GetName());
  2519. Error(str, valueNode);
  2520. }
  2521. else if( func )
  2522. {
  2523. asSExprContext rctx(engine);
  2524. PerformFunctionCall(func, &rctx, false, 0, dt.GetObjectType());
  2525. // Values on the list must be aligned to 32bit boundaries, except if the type is smaller than 32bit.
  2526. if( bufferSize & 0x3 )
  2527. bufferSize += 4 - (bufferSize & 0x3);
  2528. asSExprContext lctx(engine);
  2529. lctx.bc.InstrSHORT_DW(asBC_PshListElmnt, bufferVar, bufferSize);
  2530. lctx.type.Set(dt);
  2531. lctx.type.isLValue = true;
  2532. lctx.type.isExplicitHandle = true;
  2533. lctx.type.dataType.MakeReference(true);
  2534. asSExprContext ctx(engine);
  2535. DoAssignment(&ctx, &lctx, &rctx, valueNode, valueNode, ttAssignment, valueNode);
  2536. if( !lctx.type.dataType.IsPrimitive() )
  2537. ctx.bc.Instr(asBC_PopPtr);
  2538. // Release temporary variables used by expression
  2539. ReleaseTemporaryVariable(ctx.type, &ctx.bc);
  2540. ProcessDeferredParams(&ctx);
  2541. byteCode.AddCode(&ctx.bc);
  2542. }
  2543. }
  2544. }
  2545. // Determine size of the element
  2546. if( dt.IsPrimitive() || (!dt.IsNullHandle() && (dt.GetObjectType()->flags & asOBJ_VALUE)) )
  2547. size = dt.GetSizeInMemoryBytes();
  2548. else
  2549. size = AS_PTR_SIZE*4;
  2550. asASSERT( size <= 4 || (size & 0x3) == 0 );
  2551. // Move to the next element
  2552. bufferSize += size;
  2553. patternNode = patternNode->next;
  2554. valueNode = valueNode->next;
  2555. }
  2556. else
  2557. asASSERT( false );
  2558. return 0;
  2559. }
  2560. void asCCompiler::CompileStatement(asCScriptNode *statement, bool *hasReturn, asCByteCode *bc)
  2561. {
  2562. // Don't clear the hasReturn flag if this is an empty statement
  2563. // to avoid false errors of 'not all paths return'
  2564. if( statement->nodeType != snExpressionStatement || statement->firstChild )
  2565. *hasReturn = false;
  2566. if( statement->nodeType == snStatementBlock )
  2567. CompileStatementBlock(statement, true, hasReturn, bc);
  2568. else if( statement->nodeType == snIf )
  2569. CompileIfStatement(statement, hasReturn, bc);
  2570. else if( statement->nodeType == snFor )
  2571. CompileForStatement(statement, bc);
  2572. else if( statement->nodeType == snWhile )
  2573. CompileWhileStatement(statement, bc);
  2574. else if( statement->nodeType == snDoWhile )
  2575. CompileDoWhileStatement(statement, bc);
  2576. else if( statement->nodeType == snExpressionStatement )
  2577. CompileExpressionStatement(statement, bc);
  2578. else if( statement->nodeType == snBreak )
  2579. CompileBreakStatement(statement, bc);
  2580. else if( statement->nodeType == snContinue )
  2581. CompileContinueStatement(statement, bc);
  2582. else if( statement->nodeType == snSwitch )
  2583. CompileSwitchStatement(statement, hasReturn, bc);
  2584. else if( statement->nodeType == snReturn )
  2585. {
  2586. CompileReturnStatement(statement, bc);
  2587. *hasReturn = true;
  2588. }
  2589. }
  2590. void asCCompiler::CompileSwitchStatement(asCScriptNode *snode, bool *, asCByteCode *bc)
  2591. {
  2592. // TODO: inheritance: Must guarantee that all options in the switch case call a constructor, or that none call it.
  2593. // Reserve label for break statements
  2594. int breakLabel = nextLabel++;
  2595. breakLabels.PushLast(breakLabel);
  2596. // Add a variable scope that will be used by CompileBreak
  2597. // to know where to stop deallocating variables
  2598. AddVariableScope(true, false);
  2599. //---------------------------
  2600. // Compile the switch expression
  2601. //-------------------------------
  2602. // Compile the switch expression
  2603. asSExprContext expr(engine);
  2604. CompileAssignment(snode->firstChild, &expr);
  2605. // Verify that the expression is a primitive type
  2606. if( !expr.type.dataType.IsIntegerType() && !expr.type.dataType.IsUnsignedType() )
  2607. {
  2608. Error(TXT_SWITCH_MUST_BE_INTEGRAL, snode->firstChild);
  2609. return;
  2610. }
  2611. ProcessPropertyGetAccessor(&expr, snode);
  2612. // TODO: Need to support 64bit integers
  2613. // Convert the expression to a 32bit variable
  2614. asCDataType to;
  2615. if( expr.type.dataType.IsIntegerType() )
  2616. to.SetTokenType(ttInt);
  2617. else if( expr.type.dataType.IsUnsignedType() )
  2618. to.SetTokenType(ttUInt);
  2619. // Make sure the value is in a variable
  2620. if( expr.type.dataType.IsReference() )
  2621. ConvertToVariable(&expr);
  2622. ImplicitConversion(&expr, to, snode->firstChild, asIC_IMPLICIT_CONV, true);
  2623. ConvertToVariable(&expr);
  2624. int offset = expr.type.stackOffset;
  2625. ProcessDeferredParams(&expr);
  2626. //-------------------------------
  2627. // Determine case values and labels
  2628. //--------------------------------
  2629. // Remember the first label so that we can later pass the
  2630. // correct label to each CompileCase()
  2631. int firstCaseLabel = nextLabel;
  2632. int defaultLabel = 0;
  2633. asCArray<int> caseValues;
  2634. asCArray<int> caseLabels;
  2635. // Compile all case comparisons and make them jump to the right label
  2636. asCScriptNode *cnode = snode->firstChild->next;
  2637. while( cnode )
  2638. {
  2639. // Each case should have a constant expression
  2640. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  2641. {
  2642. // Compile expression
  2643. asSExprContext c(engine);
  2644. CompileExpression(cnode->firstChild, &c);
  2645. // Verify that the result is a constant
  2646. if( !c.type.isConstant )
  2647. Error(TXT_SWITCH_CASE_MUST_BE_CONSTANT, cnode->firstChild);
  2648. // Verify that the result is an integral number
  2649. if( !c.type.dataType.IsIntegerType() && !c.type.dataType.IsUnsignedType() )
  2650. Error(TXT_SWITCH_MUST_BE_INTEGRAL, cnode->firstChild);
  2651. ImplicitConversion(&c, to, cnode->firstChild, asIC_IMPLICIT_CONV, true);
  2652. // Has this case been declared already?
  2653. if( caseValues.IndexOf(c.type.intValue) >= 0 )
  2654. {
  2655. Error(TXT_DUPLICATE_SWITCH_CASE, cnode->firstChild);
  2656. }
  2657. // TODO: Optimize: We can insert the numbers sorted already
  2658. // Store constant for later use
  2659. caseValues.PushLast(c.type.intValue);
  2660. // Reserve label for this case
  2661. caseLabels.PushLast(nextLabel++);
  2662. }
  2663. else
  2664. {
  2665. // TODO: It shouldn't be necessary for the default case to be the last one.
  2666. // Is default the last case?
  2667. if( cnode->next )
  2668. {
  2669. Error(TXT_DEFAULT_MUST_BE_LAST, cnode);
  2670. break;
  2671. }
  2672. // Reserve label for this case
  2673. defaultLabel = nextLabel++;
  2674. }
  2675. cnode = cnode->next;
  2676. }
  2677. // check for empty switch
  2678. if (caseValues.GetLength() == 0)
  2679. {
  2680. Error(TXT_EMPTY_SWITCH, snode);
  2681. return;
  2682. }
  2683. if( defaultLabel == 0 )
  2684. defaultLabel = breakLabel;
  2685. //---------------------------------
  2686. // Output the optimized case comparisons
  2687. // with jumps to the case code
  2688. //------------------------------------
  2689. // Sort the case values by increasing value. Do the sort together with the labels
  2690. // A simple bubble sort is sufficient since we don't expect a huge number of values
  2691. for( asUINT fwd = 1; fwd < caseValues.GetLength(); fwd++ )
  2692. {
  2693. for( int bck = fwd - 1; bck >= 0; bck-- )
  2694. {
  2695. int bckp = bck + 1;
  2696. if( caseValues[bck] > caseValues[bckp] )
  2697. {
  2698. // Swap the values in both arrays
  2699. int swap = caseValues[bckp];
  2700. caseValues[bckp] = caseValues[bck];
  2701. caseValues[bck] = swap;
  2702. swap = caseLabels[bckp];
  2703. caseLabels[bckp] = caseLabels[bck];
  2704. caseLabels[bck] = swap;
  2705. }
  2706. else
  2707. break;
  2708. }
  2709. }
  2710. // Find ranges of consecutive numbers
  2711. asCArray<int> ranges;
  2712. ranges.PushLast(0);
  2713. asUINT n;
  2714. for( n = 1; n < caseValues.GetLength(); ++n )
  2715. {
  2716. // We can join numbers that are less than 5 numbers
  2717. // apart since the output code will still be smaller
  2718. if( caseValues[n] > caseValues[n-1] + 5 )
  2719. ranges.PushLast(n);
  2720. }
  2721. // If the value is larger than the largest case value, jump to default
  2722. int tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2723. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[caseValues.GetLength()-1]);
  2724. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2725. expr.bc.InstrDWORD(asBC_JP, defaultLabel);
  2726. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2727. // TODO: runtime optimize: We could possibly optimize this even more by doing a
  2728. // binary search instead of a linear search through the ranges
  2729. // For each range
  2730. int range;
  2731. for( range = 0; range < (int)ranges.GetLength(); range++ )
  2732. {
  2733. // Find the largest value in this range
  2734. int maxRange = caseValues[ranges[range]];
  2735. int index = ranges[range];
  2736. for( ; (index < (int)caseValues.GetLength()) && (caseValues[index] <= maxRange + 5); index++ )
  2737. maxRange = caseValues[index];
  2738. // If there are only 2 numbers then it is better to compare them directly
  2739. if( index - ranges[range] > 2 )
  2740. {
  2741. // If the value is smaller than the smallest case value in the range, jump to default
  2742. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2743. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  2744. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2745. expr.bc.InstrDWORD(asBC_JS, defaultLabel);
  2746. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2747. int nextRangeLabel = nextLabel++;
  2748. // If this is the last range we don't have to make this test
  2749. if( range < (int)ranges.GetLength() - 1 )
  2750. {
  2751. // If the value is larger than the largest case value in the range, jump to the next range
  2752. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2753. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, maxRange);
  2754. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2755. expr.bc.InstrDWORD(asBC_JP, nextRangeLabel);
  2756. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2757. }
  2758. // Jump forward according to the value
  2759. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2760. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[ranges[range]]);
  2761. expr.bc.InstrW_W_W(asBC_SUBi, tmpOffset, offset, tmpOffset);
  2762. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2763. expr.bc.JmpP(tmpOffset, maxRange - caseValues[ranges[range]]);
  2764. // Add the list of jumps to the correct labels (any holes, jump to default)
  2765. index = ranges[range];
  2766. for( int n = caseValues[index]; n <= maxRange; n++ )
  2767. {
  2768. if( caseValues[index] == n )
  2769. expr.bc.InstrINT(asBC_JMP, caseLabels[index++]);
  2770. else
  2771. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  2772. }
  2773. expr.bc.Label((short)nextRangeLabel);
  2774. }
  2775. else
  2776. {
  2777. // Simply make a comparison with each value
  2778. int n;
  2779. for( n = ranges[range]; n < index; ++n )
  2780. {
  2781. tmpOffset = AllocateVariable(asCDataType::CreatePrimitive(ttInt, false), true);
  2782. expr.bc.InstrSHORT_DW(asBC_SetV4, (short)tmpOffset, caseValues[n]);
  2783. expr.bc.InstrW_W(asBC_CMPi, offset, tmpOffset);
  2784. expr.bc.InstrDWORD(asBC_JZ, caseLabels[n]);
  2785. ReleaseTemporaryVariable(tmpOffset, &expr.bc);
  2786. }
  2787. }
  2788. }
  2789. // Catch any value that falls trough
  2790. expr.bc.InstrINT(asBC_JMP, defaultLabel);
  2791. // Release the temporary variable previously stored
  2792. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2793. // TODO: optimize: Should optimize each piece individually
  2794. expr.bc.OptimizeLocally(tempVariableOffsets);
  2795. //----------------------------------
  2796. // Output case implementations
  2797. //----------------------------------
  2798. // Compile case implementations, each one with the label before it
  2799. cnode = snode->firstChild->next;
  2800. while( cnode )
  2801. {
  2802. // Each case should have a constant expression
  2803. if( cnode->firstChild && cnode->firstChild->nodeType == snExpression )
  2804. {
  2805. expr.bc.Label((short)firstCaseLabel++);
  2806. CompileCase(cnode->firstChild->next, &expr.bc);
  2807. }
  2808. else
  2809. {
  2810. expr.bc.Label((short)defaultLabel);
  2811. // Is default the last case?
  2812. if( cnode->next )
  2813. {
  2814. // We've already reported this error
  2815. break;
  2816. }
  2817. CompileCase(cnode->firstChild, &expr.bc);
  2818. }
  2819. cnode = cnode->next;
  2820. }
  2821. //--------------------------------
  2822. bc->AddCode(&expr.bc);
  2823. // Add break label
  2824. bc->Label((short)breakLabel);
  2825. breakLabels.PopLast();
  2826. RemoveVariableScope();
  2827. }
  2828. void asCCompiler::CompileCase(asCScriptNode *node, asCByteCode *bc)
  2829. {
  2830. bool isFinished = false;
  2831. bool hasReturn = false;
  2832. bool hasUnreachableCode = false;
  2833. while( node )
  2834. {
  2835. if( !hasUnreachableCode && (hasReturn || isFinished) )
  2836. {
  2837. hasUnreachableCode = true;
  2838. Warning(TXT_UNREACHABLE_CODE, node);
  2839. break;
  2840. }
  2841. if( node->nodeType == snBreak || node->nodeType == snContinue )
  2842. isFinished = true;
  2843. asCByteCode statement(engine);
  2844. if( node->nodeType == snDeclaration )
  2845. {
  2846. Error(TXT_DECL_IN_SWITCH, node);
  2847. // Compile it anyway to avoid further compiler errors
  2848. CompileDeclaration(node, &statement);
  2849. }
  2850. else
  2851. CompileStatement(node, &hasReturn, &statement);
  2852. LineInstr(bc, node->tokenPos);
  2853. bc->AddCode(&statement);
  2854. if( !hasCompileErrors )
  2855. asASSERT( tempVariables.GetLength() == 0 );
  2856. node = node->next;
  2857. }
  2858. }
  2859. void asCCompiler::CompileIfStatement(asCScriptNode *inode, bool *hasReturn, asCByteCode *bc)
  2860. {
  2861. // We will use one label for the if statement
  2862. // and possibly another for the else statement
  2863. int afterLabel = nextLabel++;
  2864. // Compile the expression
  2865. asSExprContext expr(engine);
  2866. CompileAssignment(inode->firstChild, &expr);
  2867. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2868. {
  2869. Error(TXT_EXPR_MUST_BE_BOOL, inode->firstChild);
  2870. expr.type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 1);
  2871. }
  2872. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2873. ProcessDeferredParams(&expr);
  2874. if( !expr.type.isConstant )
  2875. {
  2876. ProcessPropertyGetAccessor(&expr, inode);
  2877. ConvertToVariable(&expr);
  2878. // Add a test
  2879. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2880. expr.bc.Instr(asBC_ClrHi);
  2881. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  2882. ReleaseTemporaryVariable(expr.type, &expr.bc);
  2883. expr.bc.OptimizeLocally(tempVariableOffsets);
  2884. bc->AddCode(&expr.bc);
  2885. }
  2886. else if( expr.type.dwordValue == 0 )
  2887. {
  2888. // Jump to the else case
  2889. bc->InstrINT(asBC_JMP, afterLabel);
  2890. // TODO: Should we warn that the expression will always go to the else?
  2891. }
  2892. // Compile the if statement
  2893. bool origIsConstructorCalled = m_isConstructorCalled;
  2894. bool hasReturn1;
  2895. asCByteCode ifBC(engine);
  2896. CompileStatement(inode->firstChild->next, &hasReturn1, &ifBC);
  2897. // Add the byte code
  2898. LineInstr(bc, inode->firstChild->next->tokenPos);
  2899. bc->AddCode(&ifBC);
  2900. if( inode->firstChild->next->nodeType == snExpressionStatement && inode->firstChild->next->firstChild == 0 )
  2901. {
  2902. // Don't allow if( expr );
  2903. Error(TXT_IF_WITH_EMPTY_STATEMENT, inode->firstChild->next);
  2904. }
  2905. // If one of the statements call the constructor, the other must as well
  2906. // otherwise it is possible the constructor is never called
  2907. bool constructorCall1 = false;
  2908. bool constructorCall2 = false;
  2909. if( !origIsConstructorCalled && m_isConstructorCalled )
  2910. constructorCall1 = true;
  2911. // Do we have an else statement?
  2912. if( inode->firstChild->next != inode->lastChild )
  2913. {
  2914. // Reset the constructor called flag so the else statement can call the constructor too
  2915. m_isConstructorCalled = origIsConstructorCalled;
  2916. int afterElse = 0;
  2917. if( !hasReturn1 )
  2918. {
  2919. afterElse = nextLabel++;
  2920. // Add jump to after the else statement
  2921. bc->InstrINT(asBC_JMP, afterElse);
  2922. }
  2923. // Add label for the else statement
  2924. bc->Label((short)afterLabel);
  2925. bool hasReturn2;
  2926. asCByteCode elseBC(engine);
  2927. CompileStatement(inode->lastChild, &hasReturn2, &elseBC);
  2928. // Add byte code for the else statement
  2929. LineInstr(bc, inode->lastChild->tokenPos);
  2930. bc->AddCode(&elseBC);
  2931. if( inode->lastChild->nodeType == snExpressionStatement && inode->lastChild->firstChild == 0 )
  2932. {
  2933. // Don't allow if( expr ) {} else;
  2934. Error(TXT_ELSE_WITH_EMPTY_STATEMENT, inode->lastChild);
  2935. }
  2936. if( !hasReturn1 )
  2937. {
  2938. // Add label for the end of else statement
  2939. bc->Label((short)afterElse);
  2940. }
  2941. // The if statement only has return if both alternatives have
  2942. *hasReturn = hasReturn1 && hasReturn2;
  2943. if( !origIsConstructorCalled && m_isConstructorCalled )
  2944. constructorCall2 = true;
  2945. }
  2946. else
  2947. {
  2948. // Add label for the end of if statement
  2949. bc->Label((short)afterLabel);
  2950. *hasReturn = false;
  2951. }
  2952. // Make sure both or neither conditions call a constructor
  2953. if( (constructorCall1 && !constructorCall2) ||
  2954. (constructorCall2 && !constructorCall1) )
  2955. {
  2956. Error(TXT_BOTH_CONDITIONS_MUST_CALL_CONSTRUCTOR, inode);
  2957. }
  2958. m_isConstructorCalled = origIsConstructorCalled || constructorCall1 || constructorCall2;
  2959. }
  2960. void asCCompiler::CompileForStatement(asCScriptNode *fnode, asCByteCode *bc)
  2961. {
  2962. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  2963. AddVariableScope(true, true);
  2964. // We will use three labels for the for loop
  2965. int conditionLabel = nextLabel++;
  2966. int afterLabel = nextLabel++;
  2967. int continueLabel = nextLabel++;
  2968. int insideLabel = nextLabel++;
  2969. continueLabels.PushLast(continueLabel);
  2970. breakLabels.PushLast(afterLabel);
  2971. //---------------------------------------
  2972. // Compile the initialization statement
  2973. asCByteCode initBC(engine);
  2974. LineInstr(&initBC, fnode->firstChild->tokenPos);
  2975. if( fnode->firstChild->nodeType == snDeclaration )
  2976. CompileDeclaration(fnode->firstChild, &initBC);
  2977. else
  2978. CompileExpressionStatement(fnode->firstChild, &initBC);
  2979. //-----------------------------------
  2980. // Compile the condition statement
  2981. asSExprContext expr(engine);
  2982. asCScriptNode *second = fnode->firstChild->next;
  2983. if( second->firstChild )
  2984. {
  2985. int r = CompileAssignment(second->firstChild, &expr);
  2986. if( r >= 0 )
  2987. {
  2988. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  2989. Error(TXT_EXPR_MUST_BE_BOOL, second);
  2990. else
  2991. {
  2992. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  2993. ProcessDeferredParams(&expr);
  2994. ProcessPropertyGetAccessor(&expr, second);
  2995. // If expression is false exit the loop
  2996. ConvertToVariable(&expr);
  2997. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  2998. expr.bc.Instr(asBC_ClrHi);
  2999. expr.bc.InstrDWORD(asBC_JNZ, insideLabel);
  3000. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3001. expr.bc.OptimizeLocally(tempVariableOffsets);
  3002. // Prepend the line instruction for the condition
  3003. asCByteCode tmp(engine);
  3004. LineInstr(&tmp, second->firstChild->tokenPos);
  3005. tmp.AddCode(&expr.bc);
  3006. expr.bc.AddCode(&tmp);
  3007. }
  3008. }
  3009. }
  3010. //---------------------------
  3011. // Compile the increment statement
  3012. asCByteCode nextBC(engine);
  3013. asCScriptNode *third = second->next;
  3014. if( third->nodeType == snExpressionStatement )
  3015. {
  3016. LineInstr(&nextBC, third->tokenPos);
  3017. CompileExpressionStatement(third, &nextBC);
  3018. }
  3019. //------------------------------
  3020. // Compile loop statement
  3021. bool hasReturn;
  3022. asCByteCode forBC(engine);
  3023. CompileStatement(fnode->lastChild, &hasReturn, &forBC);
  3024. //-------------------------------
  3025. // Join the code pieces
  3026. bc->AddCode(&initBC);
  3027. bc->InstrDWORD(asBC_JMP, conditionLabel);
  3028. bc->Label((short)insideLabel);
  3029. // Add a suspend bytecode inside the loop to guarantee
  3030. // that the application can suspend the execution
  3031. bc->Instr(asBC_SUSPEND);
  3032. bc->InstrPTR(asBC_JitEntry, 0);
  3033. LineInstr(bc, fnode->lastChild->tokenPos);
  3034. bc->AddCode(&forBC);
  3035. bc->Label((short)continueLabel);
  3036. bc->AddCode(&nextBC);
  3037. bc->Label((short)conditionLabel);
  3038. if( expr.bc.GetLastInstr() == -1 )
  3039. // There is no condition, so we just always jump
  3040. bc->InstrDWORD(asBC_JMP, insideLabel);
  3041. else
  3042. bc->AddCode(&expr.bc);
  3043. bc->Label((short)afterLabel);
  3044. continueLabels.PopLast();
  3045. breakLabels.PopLast();
  3046. // Deallocate variables in this block, in reverse order
  3047. for( int n = (int)variables->variables.GetLength() - 1; n >= 0; n-- )
  3048. {
  3049. sVariable *v = variables->variables[n];
  3050. // Call variable destructors here, for variables not yet destroyed
  3051. CallDestructor(v->type, v->stackOffset, v->onHeap, bc);
  3052. // Don't deallocate function parameters
  3053. if( v->stackOffset > 0 )
  3054. DeallocateVariable(v->stackOffset);
  3055. }
  3056. RemoveVariableScope();
  3057. }
  3058. void asCCompiler::CompileWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  3059. {
  3060. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3061. AddVariableScope(true, true);
  3062. // We will use two labels for the while loop
  3063. int beforeLabel = nextLabel++;
  3064. int afterLabel = nextLabel++;
  3065. continueLabels.PushLast(beforeLabel);
  3066. breakLabels.PushLast(afterLabel);
  3067. // Add label before the expression
  3068. bc->Label((short)beforeLabel);
  3069. // Compile expression
  3070. asSExprContext expr(engine);
  3071. CompileAssignment(wnode->firstChild, &expr);
  3072. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3073. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  3074. else
  3075. {
  3076. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3077. ProcessDeferredParams(&expr);
  3078. ProcessPropertyGetAccessor(&expr, wnode);
  3079. // Add byte code for the expression
  3080. ConvertToVariable(&expr);
  3081. // Jump to end of statement if expression is false
  3082. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3083. expr.bc.Instr(asBC_ClrHi);
  3084. expr.bc.InstrDWORD(asBC_JZ, afterLabel);
  3085. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3086. expr.bc.OptimizeLocally(tempVariableOffsets);
  3087. bc->AddCode(&expr.bc);
  3088. }
  3089. // Add a suspend bytecode inside the loop to guarantee
  3090. // that the application can suspend the execution
  3091. bc->Instr(asBC_SUSPEND);
  3092. bc->InstrPTR(asBC_JitEntry, 0);
  3093. // Compile statement
  3094. bool hasReturn;
  3095. asCByteCode whileBC(engine);
  3096. CompileStatement(wnode->lastChild, &hasReturn, &whileBC);
  3097. // Add byte code for the statement
  3098. LineInstr(bc, wnode->lastChild->tokenPos);
  3099. bc->AddCode(&whileBC);
  3100. // Jump to the expression
  3101. bc->InstrINT(asBC_JMP, beforeLabel);
  3102. // Add label after the statement
  3103. bc->Label((short)afterLabel);
  3104. continueLabels.PopLast();
  3105. breakLabels.PopLast();
  3106. RemoveVariableScope();
  3107. }
  3108. void asCCompiler::CompileDoWhileStatement(asCScriptNode *wnode, asCByteCode *bc)
  3109. {
  3110. // Add a variable scope that will be used by CompileBreak/Continue to know where to stop deallocating variables
  3111. AddVariableScope(true, true);
  3112. // We will use two labels for the while loop
  3113. int beforeLabel = nextLabel++;
  3114. int beforeTest = nextLabel++;
  3115. int afterLabel = nextLabel++;
  3116. continueLabels.PushLast(beforeTest);
  3117. breakLabels.PushLast(afterLabel);
  3118. // Add label before the statement
  3119. bc->Label((short)beforeLabel);
  3120. // Compile statement
  3121. bool hasReturn;
  3122. asCByteCode whileBC(engine);
  3123. CompileStatement(wnode->firstChild, &hasReturn, &whileBC);
  3124. // Add byte code for the statement
  3125. LineInstr(bc, wnode->firstChild->tokenPos);
  3126. bc->AddCode(&whileBC);
  3127. // Add label before the expression
  3128. bc->Label((short)beforeTest);
  3129. // Add a suspend bytecode inside the loop to guarantee
  3130. // that the application can suspend the execution
  3131. bc->Instr(asBC_SUSPEND);
  3132. bc->InstrPTR(asBC_JitEntry, 0);
  3133. // Add a line instruction
  3134. LineInstr(bc, wnode->lastChild->tokenPos);
  3135. // Compile expression
  3136. asSExprContext expr(engine);
  3137. CompileAssignment(wnode->lastChild, &expr);
  3138. if( !expr.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  3139. Error(TXT_EXPR_MUST_BE_BOOL, wnode->firstChild);
  3140. else
  3141. {
  3142. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3143. ProcessDeferredParams(&expr);
  3144. ProcessPropertyGetAccessor(&expr, wnode);
  3145. // Add byte code for the expression
  3146. ConvertToVariable(&expr);
  3147. // Jump to next iteration if expression is true
  3148. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3149. expr.bc.Instr(asBC_ClrHi);
  3150. expr.bc.InstrDWORD(asBC_JNZ, beforeLabel);
  3151. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3152. expr.bc.OptimizeLocally(tempVariableOffsets);
  3153. bc->AddCode(&expr.bc);
  3154. }
  3155. // Add label after the statement
  3156. bc->Label((short)afterLabel);
  3157. continueLabels.PopLast();
  3158. breakLabels.PopLast();
  3159. RemoveVariableScope();
  3160. }
  3161. void asCCompiler::CompileBreakStatement(asCScriptNode *node, asCByteCode *bc)
  3162. {
  3163. if( breakLabels.GetLength() == 0 )
  3164. {
  3165. Error(TXT_INVALID_BREAK, node);
  3166. return;
  3167. }
  3168. // Add destructor calls for all variables that will go out of scope
  3169. // Put this clean up in a block to allow exception handler to understand them
  3170. bc->Block(true);
  3171. asCVariableScope *vs = variables;
  3172. while( !vs->isBreakScope )
  3173. {
  3174. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  3175. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  3176. vs = vs->parent;
  3177. }
  3178. bc->Block(false);
  3179. bc->InstrINT(asBC_JMP, breakLabels[breakLabels.GetLength()-1]);
  3180. }
  3181. void asCCompiler::CompileContinueStatement(asCScriptNode *node, asCByteCode *bc)
  3182. {
  3183. if( continueLabels.GetLength() == 0 )
  3184. {
  3185. Error(TXT_INVALID_CONTINUE, node);
  3186. return;
  3187. }
  3188. // Add destructor calls for all variables that will go out of scope
  3189. // Put this clean up in a block to allow exception handler to understand them
  3190. bc->Block(true);
  3191. asCVariableScope *vs = variables;
  3192. while( !vs->isContinueScope )
  3193. {
  3194. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  3195. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  3196. vs = vs->parent;
  3197. }
  3198. bc->Block(false);
  3199. bc->InstrINT(asBC_JMP, continueLabels[continueLabels.GetLength()-1]);
  3200. }
  3201. void asCCompiler::CompileExpressionStatement(asCScriptNode *enode, asCByteCode *bc)
  3202. {
  3203. if( enode->firstChild )
  3204. {
  3205. // Compile the expression
  3206. asSExprContext expr(engine);
  3207. CompileAssignment(enode->firstChild, &expr);
  3208. // If we get here and there is still an unprocessed property
  3209. // accessor, then process it as a get access. Don't call if there is
  3210. // already a compile error, or we might report an error that is not valid
  3211. if( !hasCompileErrors )
  3212. ProcessPropertyGetAccessor(&expr, enode);
  3213. // Pop the value from the stack
  3214. if( !expr.type.dataType.IsPrimitive() )
  3215. expr.bc.Instr(asBC_PopPtr);
  3216. // Release temporary variables used by expression
  3217. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3218. ProcessDeferredParams(&expr);
  3219. expr.bc.OptimizeLocally(tempVariableOffsets);
  3220. bc->AddCode(&expr.bc);
  3221. }
  3222. }
  3223. void asCCompiler::PrepareTemporaryObject(asCScriptNode *node, asSExprContext *ctx, bool forceOnHeap)
  3224. {
  3225. // If the object already is stored in temporary variable then nothing needs to be done
  3226. // Note, a type can be temporary without being a variable, in which case it is holding off
  3227. // on releasing a previously used object.
  3228. if( ctx->type.isTemporary && ctx->type.isVariable &&
  3229. !(forceOnHeap && !IsVariableOnHeap(ctx->type.stackOffset)) )
  3230. {
  3231. // If the temporary object is currently not a reference
  3232. // the expression needs to be reevaluated to a reference
  3233. if( !ctx->type.dataType.IsReference() )
  3234. {
  3235. ctx->bc.Instr(asBC_PopPtr);
  3236. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  3237. ctx->type.dataType.MakeReference(true);
  3238. }
  3239. return;
  3240. }
  3241. // Allocate temporary variable
  3242. asCDataType dt = ctx->type.dataType;
  3243. dt.MakeReference(false);
  3244. dt.MakeReadOnly(false);
  3245. int offset = AllocateVariable(dt, true, forceOnHeap);
  3246. // Objects stored on the stack are not considered references
  3247. dt.MakeReference(IsVariableOnHeap(offset));
  3248. asCTypeInfo lvalue;
  3249. lvalue.Set(dt);
  3250. lvalue.isExplicitHandle = ctx->type.isExplicitHandle;
  3251. bool isExplicitHandle = ctx->type.isExplicitHandle;
  3252. if( !dt.IsObjectHandle() &&
  3253. dt.GetObjectType() && (dt.GetBehaviour()->copyconstruct || dt.GetBehaviour()->copyfactory) )
  3254. {
  3255. PrepareForAssignment(&lvalue.dataType, ctx, node, true);
  3256. // Use the copy constructor/factory when available
  3257. CallCopyConstructor(dt, offset, IsVariableOnHeap(offset), &ctx->bc, ctx, node);
  3258. }
  3259. else
  3260. {
  3261. // Allocate and construct the temporary object
  3262. int r = CallDefaultConstructor(dt, offset, IsVariableOnHeap(offset), &ctx->bc, node);
  3263. if( r < 0 )
  3264. {
  3265. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  3266. }
  3267. else
  3268. {
  3269. // Assign the object to the temporary variable
  3270. PrepareForAssignment(&lvalue.dataType, ctx, node, true);
  3271. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  3272. r = PerformAssignment(&lvalue, &ctx->type, &ctx->bc, node);
  3273. if( r < 0 )
  3274. {
  3275. Error(TXT_FAILED_TO_CREATE_TEMP_OBJ, node);
  3276. }
  3277. // Release any temp that may have been created as the result of opAssign
  3278. ReleaseTemporaryVariable(lvalue, &ctx->bc);
  3279. // Pop the original reference
  3280. if( !lvalue.dataType.IsPrimitive() )
  3281. ctx->bc.Instr(asBC_PopPtr);
  3282. }
  3283. // If the expression was holding off on releasing a
  3284. // previously used object, we need to release it now
  3285. if( ctx->type.isTemporary )
  3286. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  3287. }
  3288. // Push the reference to the temporary variable on the stack
  3289. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  3290. ctx->type.Set(dt);
  3291. ctx->type.isTemporary = true;
  3292. ctx->type.stackOffset = (short)offset;
  3293. ctx->type.isVariable = true;
  3294. ctx->type.isExplicitHandle = isExplicitHandle;
  3295. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  3296. }
  3297. void asCCompiler::CompileReturnStatement(asCScriptNode *rnode, asCByteCode *bc)
  3298. {
  3299. // Get return type and location
  3300. sVariable *v = variables->GetVariable("return");
  3301. // Basic validations
  3302. if( v->type.GetSizeOnStackDWords() > 0 && !rnode->firstChild )
  3303. {
  3304. Error(TXT_MUST_RETURN_VALUE, rnode);
  3305. return;
  3306. }
  3307. else if( v->type.GetSizeOnStackDWords() == 0 && rnode->firstChild )
  3308. {
  3309. Error(TXT_CANT_RETURN_VALUE, rnode);
  3310. return;
  3311. }
  3312. // Compile the expression
  3313. if( rnode->firstChild )
  3314. {
  3315. // Compile the expression
  3316. asSExprContext expr(engine);
  3317. int r = CompileAssignment(rnode->firstChild, &expr);
  3318. if( r < 0 ) return;
  3319. if( v->type.IsReference() )
  3320. {
  3321. // The expression that gives the reference must not use any of the
  3322. // variables that must be destroyed upon exit, because then it means
  3323. // reference will stay alive while the clean-up is done, which could
  3324. // potentially mean that the reference is invalidated by the clean-up.
  3325. //
  3326. // When the function is returning a reference, the clean-up of the
  3327. // variables must be done before the evaluation of the expression.
  3328. //
  3329. // A reference to a global variable, or a class member for class methods
  3330. // should be allowed to be returned.
  3331. if( !(expr.type.dataType.IsReference() ||
  3332. (expr.type.dataType.IsObject() && !expr.type.dataType.IsObjectHandle())) )
  3333. {
  3334. // Clean up the potential deferred parameters
  3335. ProcessDeferredParams(&expr);
  3336. Error(TXT_NOT_VALID_REFERENCE, rnode);
  3337. return;
  3338. }
  3339. // No references to local variables, temporary variables, or parameters
  3340. // are allowed to be returned, since they go out of scope when the function
  3341. // returns. Even reference parameters are disallowed, since it is not possible
  3342. // to know the scope of them. The exception is the 'this' pointer, which
  3343. // is treated by the compiler as a local variable, but isn't really so.
  3344. if( (expr.type.isVariable && !(expr.type.stackOffset == 0 && outFunc->objectType)) || expr.type.isTemporary )
  3345. {
  3346. // Clean up the potential deferred parameters
  3347. ProcessDeferredParams(&expr);
  3348. Error(TXT_CANNOT_RETURN_REF_TO_LOCAL, rnode);
  3349. return;
  3350. }
  3351. // The type must match exactly as we cannot convert
  3352. // the reference without loosing the original value
  3353. if( !(v->type.IsEqualExceptConst(expr.type.dataType) ||
  3354. (expr.type.dataType.IsObject() &&
  3355. !expr.type.dataType.IsObjectHandle() &&
  3356. v->type.IsEqualExceptRefAndConst(expr.type.dataType))) ||
  3357. (!v->type.IsReadOnly() && expr.type.dataType.IsReadOnly()) )
  3358. {
  3359. // Clean up the potential deferred parameters
  3360. ProcessDeferredParams(&expr);
  3361. asCString str;
  3362. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  3363. Error(str, rnode);
  3364. return;
  3365. }
  3366. // The expression must not have any deferred expressions, because the evaluation
  3367. // of these cannot be done without keeping the reference which is not safe
  3368. if( expr.deferredParams.GetLength() )
  3369. {
  3370. // Clean up the potential deferred parameters
  3371. ProcessDeferredParams(&expr);
  3372. Error(TXT_REF_CANT_BE_RETURNED_DEFERRED_PARAM, rnode);
  3373. return;
  3374. }
  3375. // Make sure the expression isn't using any local variables that
  3376. // will need to be cleaned up before the function completes
  3377. asCArray<int> usedVars;
  3378. expr.bc.GetVarsUsed(usedVars);
  3379. for( asUINT n = 0; n < usedVars.GetLength(); n++ )
  3380. {
  3381. int var = GetVariableSlot(usedVars[n]);
  3382. if( var != -1 )
  3383. {
  3384. asCDataType dt = variableAllocations[var];
  3385. if( dt.IsObject() )
  3386. {
  3387. ProcessDeferredParams(&expr);
  3388. Error(TXT_REF_CANT_BE_RETURNED_LOCAL_VARS, rnode);
  3389. return;
  3390. }
  3391. }
  3392. }
  3393. // All objects in the function must be cleaned up before the expression
  3394. // is evaluated, otherwise there is a possibility that the cleanup will
  3395. // invalidate the reference.
  3396. // Destroy the local variables before loading
  3397. // the reference into the register. This will
  3398. // be done before the expression is evaluated.
  3399. DestroyVariables(bc);
  3400. // For primitives the reference is already in the register,
  3401. // but for non-primitives the reference is on the stack so we
  3402. // need to load it into the register
  3403. if( !expr.type.dataType.IsPrimitive() )
  3404. {
  3405. if( !expr.type.dataType.IsObjectHandle() &&
  3406. expr.type.dataType.IsReference() )
  3407. expr.bc.Instr(asBC_RDSPtr);
  3408. expr.bc.Instr(asBC_PopRPtr);
  3409. }
  3410. // There are no temporaries to release so we're done
  3411. }
  3412. else // if( !v->type.IsReference() )
  3413. {
  3414. ProcessPropertyGetAccessor(&expr, rnode);
  3415. // Prepare the value for assignment
  3416. IsVariableInitialized(&expr.type, rnode->firstChild);
  3417. if( v->type.IsPrimitive() )
  3418. {
  3419. if( expr.type.dataType.IsReference() ) ConvertToVariable(&expr);
  3420. // Implicitly convert the value to the return type
  3421. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  3422. // Verify that the conversion was successful
  3423. if( expr.type.dataType != v->type )
  3424. {
  3425. asCString str;
  3426. str.Format(TXT_NO_CONVERSION_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  3427. Error(str, rnode);
  3428. return;
  3429. }
  3430. else
  3431. {
  3432. ConvertToVariable(&expr);
  3433. // Clean up the local variables and process deferred parameters
  3434. DestroyVariables(&expr.bc);
  3435. ProcessDeferredParams(&expr);
  3436. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3437. // Load the variable in the register
  3438. if( v->type.GetSizeOnStackDWords() == 1 )
  3439. expr.bc.InstrSHORT(asBC_CpyVtoR4, expr.type.stackOffset);
  3440. else
  3441. expr.bc.InstrSHORT(asBC_CpyVtoR8, expr.type.stackOffset);
  3442. }
  3443. }
  3444. else if( v->type.IsObject() )
  3445. {
  3446. // Value types are returned on the stack, in a location
  3447. // that has been reserved by the calling function.
  3448. if( outFunc->DoesReturnOnStack() )
  3449. {
  3450. // TODO: runtime optimize: If the return type has a constructor that takes the type of the expression,
  3451. // it should be called directly instead of first converting the expression and
  3452. // then copy the value.
  3453. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  3454. {
  3455. ImplicitConversion(&expr, v->type, rnode->firstChild, asIC_IMPLICIT_CONV);
  3456. if( !v->type.IsEqualExceptRefAndConst(expr.type.dataType) )
  3457. {
  3458. asCString str;
  3459. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, expr.type.dataType.Format().AddressOf(), v->type.Format().AddressOf());
  3460. Error(str, rnode->firstChild);
  3461. return;
  3462. }
  3463. }
  3464. int offset = outFunc->objectType ? -AS_PTR_SIZE : 0;
  3465. if( v->type.GetObjectType()->beh.copyconstruct )
  3466. {
  3467. PrepareForAssignment(&v->type, &expr, rnode->firstChild, false);
  3468. CallCopyConstructor(v->type, offset, false, &expr.bc, &expr, rnode->firstChild, false, true);
  3469. }
  3470. else
  3471. {
  3472. // If the copy constructor doesn't exist, then a manual assignment needs to be done instead.
  3473. CallDefaultConstructor(v->type, offset, false, &expr.bc, rnode->firstChild, 0, true);
  3474. PrepareForAssignment(&v->type, &expr, rnode->firstChild, false);
  3475. expr.bc.InstrSHORT(asBC_PSF, (short)offset);
  3476. expr.bc.Instr(asBC_RDSPtr);
  3477. asSExprContext lexpr(engine);
  3478. lexpr.type.Set(v->type);
  3479. lexpr.type.isLValue = true;
  3480. PerformAssignment(&lexpr.type, &expr.type, &expr.bc, rnode->firstChild);
  3481. expr.bc.Instr(asBC_PopPtr);
  3482. // Release any temporary variable
  3483. ReleaseTemporaryVariable(expr.type, &expr.bc);
  3484. }
  3485. // Clean up the local variables and process deferred parameters
  3486. DestroyVariables(&expr.bc);
  3487. ProcessDeferredParams(&expr);
  3488. }
  3489. else
  3490. {
  3491. asASSERT( v->type.GetObjectType()->flags & asOBJ_REF );
  3492. // Prepare the expression to be loaded into the object
  3493. // register. This will place the reference in local variable
  3494. PrepareArgument(&v->type, &expr, rnode->firstChild, false, 0);
  3495. // Pop the reference to the temporary variable
  3496. expr.bc.Instr(asBC_PopPtr);
  3497. // Clean up the local variables and process deferred parameters
  3498. DestroyVariables(&expr.bc);
  3499. ProcessDeferredParams(&expr);
  3500. // Load the object pointer into the object register
  3501. // LOADOBJ also clears the address in the variable
  3502. expr.bc.InstrSHORT(asBC_LOADOBJ, expr.type.stackOffset);
  3503. // LOADOBJ cleared the address in the variable so the object will not be freed
  3504. // here, but the temporary variable must still be freed so the slot can be reused
  3505. // By releasing without the bytecode we do just that.
  3506. ReleaseTemporaryVariable(expr.type, 0);
  3507. }
  3508. }
  3509. }
  3510. expr.bc.OptimizeLocally(tempVariableOffsets);
  3511. bc->AddCode(&expr.bc);
  3512. }
  3513. else
  3514. {
  3515. // For functions that don't return anything
  3516. // we just detroy the local variables
  3517. DestroyVariables(bc);
  3518. }
  3519. // Jump to the end of the function
  3520. bc->InstrINT(asBC_JMP, 0);
  3521. }
  3522. void asCCompiler::DestroyVariables(asCByteCode *bc)
  3523. {
  3524. // Call destructor on all variables except for the function parameters
  3525. // Put the clean-up in a block to allow exception handler to understand this
  3526. bc->Block(true);
  3527. asCVariableScope *vs = variables;
  3528. while( vs )
  3529. {
  3530. for( int n = (int)vs->variables.GetLength() - 1; n >= 0; n-- )
  3531. if( vs->variables[n]->stackOffset > 0 )
  3532. CallDestructor(vs->variables[n]->type, vs->variables[n]->stackOffset, vs->variables[n]->onHeap, bc);
  3533. vs = vs->parent;
  3534. }
  3535. bc->Block(false);
  3536. }
  3537. void asCCompiler::AddVariableScope(bool isBreakScope, bool isContinueScope)
  3538. {
  3539. variables = asNEW(asCVariableScope)(variables);
  3540. if( variables == 0 )
  3541. {
  3542. // Out of memory
  3543. return;
  3544. }
  3545. variables->isBreakScope = isBreakScope;
  3546. variables->isContinueScope = isContinueScope;
  3547. }
  3548. void asCCompiler::RemoveVariableScope()
  3549. {
  3550. if( variables )
  3551. {
  3552. asCVariableScope *var = variables;
  3553. variables = variables->parent;
  3554. asDELETE(var,asCVariableScope);
  3555. }
  3556. }
  3557. void asCCompiler::Error(const asCString &msg, asCScriptNode *node)
  3558. {
  3559. asCString str;
  3560. int r = 0, c = 0;
  3561. asASSERT( node );
  3562. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  3563. builder->WriteError(script->name, msg, r, c);
  3564. hasCompileErrors = true;
  3565. }
  3566. void asCCompiler::Warning(const asCString &msg, asCScriptNode *node)
  3567. {
  3568. asCString str;
  3569. int r = 0, c = 0;
  3570. asASSERT( node );
  3571. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  3572. builder->WriteWarning(script->name, msg, r, c);
  3573. }
  3574. void asCCompiler::Information(const asCString &msg, asCScriptNode *node)
  3575. {
  3576. asCString str;
  3577. int r = 0, c = 0;
  3578. asASSERT( node );
  3579. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  3580. builder->WriteInfo(script->name, msg, r, c, false);
  3581. }
  3582. void asCCompiler::PrintMatchingFuncs(asCArray<int> &funcs, asCScriptNode *node)
  3583. {
  3584. int r = 0, c = 0;
  3585. asASSERT( node );
  3586. if( node ) script->ConvertPosToRowCol(node->tokenPos, &r, &c);
  3587. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  3588. {
  3589. asIScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  3590. builder->WriteInfo(script->name, func->GetDeclaration(true), r, c, false);
  3591. }
  3592. }
  3593. int asCCompiler::AllocateVariableNotIn(const asCDataType &type, bool isTemporary, bool forceOnHeap, asSExprContext *ctx)
  3594. {
  3595. int l = int(reservedVariables.GetLength());
  3596. ctx->bc.GetVarsUsed(reservedVariables);
  3597. int var = AllocateVariable(type, isTemporary, forceOnHeap);
  3598. reservedVariables.SetLength(l);
  3599. return var;
  3600. }
  3601. int asCCompiler::AllocateVariable(const asCDataType &type, bool isTemporary, bool forceOnHeap)
  3602. {
  3603. asCDataType t(type);
  3604. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 1 )
  3605. t.SetTokenType(ttInt);
  3606. if( t.IsPrimitive() && t.GetSizeOnStackDWords() == 2 )
  3607. t.SetTokenType(ttDouble);
  3608. // Only null handles have the token type unrecognized token
  3609. asASSERT( t.IsObjectHandle() || t.GetTokenType() != ttUnrecognizedToken );
  3610. bool isOnHeap = true;
  3611. if( t.IsPrimitive() ||
  3612. (t.GetObjectType() && (t.GetObjectType()->GetFlags() & asOBJ_VALUE) && !forceOnHeap) )
  3613. {
  3614. // Primitives and value types (unless overridden) are allocated on the stack
  3615. isOnHeap = false;
  3616. }
  3617. // Find a free location with the same type
  3618. for( asUINT n = 0; n < freeVariables.GetLength(); n++ )
  3619. {
  3620. int slot = freeVariables[n];
  3621. if( variableAllocations[slot].IsEqualExceptConst(t) &&
  3622. variableIsTemporary[slot] == isTemporary &&
  3623. variableIsOnHeap[slot] == isOnHeap )
  3624. {
  3625. // We can't return by slot, must count variable sizes
  3626. int offset = GetVariableOffset(slot);
  3627. // Verify that it is not in the list of reserved variables
  3628. bool isUsed = false;
  3629. if( reservedVariables.GetLength() )
  3630. isUsed = reservedVariables.Exists(offset);
  3631. if( !isUsed )
  3632. {
  3633. if( n != freeVariables.GetLength() - 1 )
  3634. freeVariables[n] = freeVariables.PopLast();
  3635. else
  3636. freeVariables.PopLast();
  3637. if( isTemporary )
  3638. tempVariables.PushLast(offset);
  3639. return offset;
  3640. }
  3641. }
  3642. }
  3643. variableAllocations.PushLast(t);
  3644. variableIsTemporary.PushLast(isTemporary);
  3645. variableIsOnHeap.PushLast(isOnHeap);
  3646. int offset = GetVariableOffset((int)variableAllocations.GetLength()-1);
  3647. if( isTemporary )
  3648. {
  3649. // Add offset to the currently allocated temporary variables
  3650. tempVariables.PushLast(offset);
  3651. // Add offset to all known offsets to temporary variables, whether allocated or not
  3652. tempVariableOffsets.PushLast(offset);
  3653. }
  3654. return offset;
  3655. }
  3656. int asCCompiler::GetVariableOffset(int varIndex)
  3657. {
  3658. // Return offset to the last dword on the stack
  3659. // Start at 1 as offset 0 is reserved for the this pointer (or first argument for global functions)
  3660. int varOffset = 1;
  3661. // Skip lower variables
  3662. for( int n = 0; n < varIndex; n++ )
  3663. {
  3664. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  3665. varOffset += variableAllocations[n].GetSizeInMemoryDWords();
  3666. else
  3667. varOffset += variableAllocations[n].GetSizeOnStackDWords();
  3668. }
  3669. if( varIndex < (int)variableAllocations.GetLength() )
  3670. {
  3671. // For variables larger than 1 dword the returned offset should be to the last dword
  3672. int size;
  3673. if( !variableIsOnHeap[varIndex] && variableAllocations[varIndex].IsObject() )
  3674. size = variableAllocations[varIndex].GetSizeInMemoryDWords();
  3675. else
  3676. size = variableAllocations[varIndex].GetSizeOnStackDWords();
  3677. if( size > 1 )
  3678. varOffset += size-1;
  3679. }
  3680. return varOffset;
  3681. }
  3682. int asCCompiler::GetVariableSlot(int offset)
  3683. {
  3684. int varOffset = 1;
  3685. for( asUINT n = 0; n < variableAllocations.GetLength(); n++ )
  3686. {
  3687. if( !variableIsOnHeap[n] && variableAllocations[n].IsObject() )
  3688. varOffset += -1 + variableAllocations[n].GetSizeInMemoryDWords();
  3689. else
  3690. varOffset += -1 + variableAllocations[n].GetSizeOnStackDWords();
  3691. if( varOffset == offset )
  3692. return n;
  3693. varOffset++;
  3694. }
  3695. return -1;
  3696. }
  3697. bool asCCompiler::IsVariableOnHeap(int offset)
  3698. {
  3699. int varSlot = GetVariableSlot(offset);
  3700. if( varSlot < 0 )
  3701. {
  3702. // This happens for function arguments that are considered as on the heap
  3703. return true;
  3704. }
  3705. return variableIsOnHeap[varSlot];
  3706. }
  3707. void asCCompiler::DeallocateVariable(int offset)
  3708. {
  3709. // Remove temporary variable
  3710. int n;
  3711. for( n = 0; n < (int)tempVariables.GetLength(); n++ )
  3712. {
  3713. if( offset == tempVariables[n] )
  3714. {
  3715. if( n == (int)tempVariables.GetLength()-1 )
  3716. tempVariables.PopLast();
  3717. else
  3718. tempVariables[n] = tempVariables.PopLast();
  3719. break;
  3720. }
  3721. }
  3722. n = GetVariableSlot(offset);
  3723. if( n != -1 )
  3724. {
  3725. freeVariables.PushLast(n);
  3726. return;
  3727. }
  3728. // We might get here if the variable was implicitly declared
  3729. // because it was use before a formal declaration, in this case
  3730. // the offset is 0x7FFF
  3731. asASSERT(offset == 0x7FFF);
  3732. }
  3733. void asCCompiler::ReleaseTemporaryVariable(asCTypeInfo &t, asCByteCode *bc)
  3734. {
  3735. if( t.isTemporary )
  3736. {
  3737. ReleaseTemporaryVariable(t.stackOffset, bc);
  3738. t.isTemporary = false;
  3739. }
  3740. }
  3741. void asCCompiler::ReleaseTemporaryVariable(int offset, asCByteCode *bc)
  3742. {
  3743. if( bc )
  3744. {
  3745. // We need to call the destructor on the true variable type
  3746. int n = GetVariableSlot(offset);
  3747. asASSERT( n >= 0 );
  3748. if( n >= 0 )
  3749. {
  3750. asCDataType dt = variableAllocations[n];
  3751. bool isOnHeap = variableIsOnHeap[n];
  3752. // Call destructor
  3753. CallDestructor(dt, offset, isOnHeap, bc);
  3754. }
  3755. }
  3756. DeallocateVariable(offset);
  3757. }
  3758. void asCCompiler::Dereference(asSExprContext *ctx, bool generateCode)
  3759. {
  3760. if( ctx->type.dataType.IsReference() )
  3761. {
  3762. if( ctx->type.dataType.IsObject() )
  3763. {
  3764. ctx->type.dataType.MakeReference(false);
  3765. if( generateCode )
  3766. ctx->bc.Instr(asBC_RDSPtr);
  3767. }
  3768. else
  3769. {
  3770. // This should never happen as primitives are treated differently
  3771. asASSERT(false);
  3772. }
  3773. }
  3774. }
  3775. bool asCCompiler::IsVariableInitialized(asCTypeInfo *type, asCScriptNode *node)
  3776. {
  3777. // No need to check if there is no variable scope
  3778. if( variables == 0 ) return true;
  3779. // Temporary variables are assumed to be initialized
  3780. if( type->isTemporary ) return true;
  3781. // Verify that it is a variable
  3782. if( !type->isVariable ) return true;
  3783. // Find the variable
  3784. sVariable *v = variables->GetVariableByOffset(type->stackOffset);
  3785. // The variable isn't found if it is a constant, in which case it is guaranteed to be initialized
  3786. if( v == 0 ) return true;
  3787. if( v->isInitialized ) return true;
  3788. // Complex types don't need this test
  3789. if( v->type.IsObject() ) return true;
  3790. // Mark as initialized so that the user will not be bothered again
  3791. v->isInitialized = true;
  3792. // Write warning
  3793. asCString str;
  3794. str.Format(TXT_s_NOT_INITIALIZED, (const char *)v->name.AddressOf());
  3795. Warning(str, node);
  3796. return false;
  3797. }
  3798. void asCCompiler::PrepareOperand(asSExprContext *ctx, asCScriptNode *node)
  3799. {
  3800. // Check if the variable is initialized (if it indeed is a variable)
  3801. IsVariableInitialized(&ctx->type, node);
  3802. asCDataType to = ctx->type.dataType;
  3803. to.MakeReference(false);
  3804. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  3805. ProcessDeferredParams(ctx);
  3806. }
  3807. void asCCompiler::PrepareForAssignment(asCDataType *lvalue, asSExprContext *rctx, asCScriptNode *node, bool toTemporary, asSExprContext *lvalueExpr)
  3808. {
  3809. // Reserve the temporary variables used in the lvalue expression so they won't end up being used by the rvalue too
  3810. int l = int(reservedVariables.GetLength());
  3811. if( lvalueExpr ) lvalueExpr->bc.GetVarsUsed(reservedVariables);
  3812. ProcessPropertyGetAccessor(rctx, node);
  3813. // Make sure the rvalue is initialized if it is a variable
  3814. IsVariableInitialized(&rctx->type, node);
  3815. if( lvalue->IsPrimitive() )
  3816. {
  3817. if( rctx->type.dataType.IsPrimitive() )
  3818. {
  3819. if( rctx->type.dataType.IsReference() )
  3820. {
  3821. // Cannot do implicit conversion of references so we first convert the reference to a variable
  3822. ConvertToVariableNotIn(rctx, lvalueExpr);
  3823. }
  3824. }
  3825. // Implicitly convert the value to the right type
  3826. ImplicitConversion(rctx, *lvalue, node, asIC_IMPLICIT_CONV);
  3827. // Check data type
  3828. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  3829. {
  3830. asCString str;
  3831. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lvalue->Format().AddressOf());
  3832. Error(str, node);
  3833. rctx->type.SetDummy();
  3834. }
  3835. // Make sure the rvalue is a variable
  3836. if( !rctx->type.isVariable )
  3837. ConvertToVariableNotIn(rctx, lvalueExpr);
  3838. }
  3839. else
  3840. {
  3841. asCDataType to = *lvalue;
  3842. to.MakeReference(false);
  3843. // TODO: ImplicitConversion should know to do this by itself
  3844. // First convert to a handle which will do a reference cast
  3845. if( !lvalue->IsObjectHandle() &&
  3846. (lvalue->GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) )
  3847. to.MakeHandle(true);
  3848. // Don't allow the implicit conversion to create an object
  3849. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  3850. if( !lvalue->IsObjectHandle() &&
  3851. (lvalue->GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) )
  3852. {
  3853. // Then convert to a reference, which will validate the handle
  3854. to.MakeHandle(false);
  3855. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true, !toTemporary);
  3856. }
  3857. // Check data type
  3858. if( !lvalue->IsEqualExceptRefAndConst(rctx->type.dataType) )
  3859. {
  3860. asCString str;
  3861. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lvalue->Format().AddressOf());
  3862. Error(str, node);
  3863. }
  3864. else
  3865. {
  3866. // If the assignment will be made with the copy behaviour then the rvalue must not be a reference
  3867. if( lvalue->IsObject() )
  3868. asASSERT(!rctx->type.dataType.IsReference());
  3869. }
  3870. }
  3871. // Unreserve variables
  3872. reservedVariables.SetLength(l);
  3873. }
  3874. bool asCCompiler::IsLValue(asCTypeInfo &type)
  3875. {
  3876. if( !type.isLValue ) return false;
  3877. if( type.dataType.IsReadOnly() ) return false;
  3878. if( !type.dataType.IsObject() && !type.isVariable && !type.dataType.IsReference() ) return false;
  3879. return true;
  3880. }
  3881. int asCCompiler::PerformAssignment(asCTypeInfo *lvalue, asCTypeInfo *rvalue, asCByteCode *bc, asCScriptNode *node)
  3882. {
  3883. if( lvalue->dataType.IsReadOnly() )
  3884. {
  3885. Error(TXT_REF_IS_READ_ONLY, node);
  3886. return -1;
  3887. }
  3888. if( lvalue->dataType.IsPrimitive() )
  3889. {
  3890. if( lvalue->isVariable )
  3891. {
  3892. // Copy the value between the variables directly
  3893. if( lvalue->dataType.GetSizeInMemoryDWords() == 1 )
  3894. bc->InstrW_W(asBC_CpyVtoV4, lvalue->stackOffset, rvalue->stackOffset);
  3895. else
  3896. bc->InstrW_W(asBC_CpyVtoV8, lvalue->stackOffset, rvalue->stackOffset);
  3897. // Mark variable as initialized
  3898. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  3899. if( v ) v->isInitialized = true;
  3900. }
  3901. else if( lvalue->dataType.IsReference() )
  3902. {
  3903. // Copy the value of the variable to the reference in the register
  3904. int s = lvalue->dataType.GetSizeInMemoryBytes();
  3905. if( s == 1 )
  3906. bc->InstrSHORT(asBC_WRTV1, rvalue->stackOffset);
  3907. else if( s == 2 )
  3908. bc->InstrSHORT(asBC_WRTV2, rvalue->stackOffset);
  3909. else if( s == 4 )
  3910. bc->InstrSHORT(asBC_WRTV4, rvalue->stackOffset);
  3911. else if( s == 8 )
  3912. bc->InstrSHORT(asBC_WRTV8, rvalue->stackOffset);
  3913. }
  3914. else
  3915. {
  3916. Error(TXT_NOT_VALID_LVALUE, node);
  3917. return -1;
  3918. }
  3919. }
  3920. else if( !lvalue->isExplicitHandle )
  3921. {
  3922. asSExprContext ctx(engine);
  3923. ctx.type = *lvalue;
  3924. Dereference(&ctx, true);
  3925. *lvalue = ctx.type;
  3926. bc->AddCode(&ctx.bc);
  3927. // TODO: Should find the opAssign method that implements the default copy behaviour.
  3928. // The beh->copy member will be removed.
  3929. asSTypeBehaviour *beh = lvalue->dataType.GetBehaviour();
  3930. if( beh->copy && beh->copy != engine->scriptTypeBehaviours.beh.copy )
  3931. {
  3932. asSExprContext res(engine);
  3933. PerformFunctionCall(beh->copy, &res, false, 0, lvalue->dataType.GetObjectType());
  3934. bc->AddCode(&res.bc);
  3935. *lvalue = res.type;
  3936. }
  3937. else if( beh->copy == engine->scriptTypeBehaviours.beh.copy )
  3938. {
  3939. // Call the default copy operator for script classes
  3940. // This is done differently because the default copy operator
  3941. // is registered as returning int&, but in reality it returns
  3942. // a reference to the object.
  3943. // TODO: Avoid this special case by implementing a copystub for
  3944. // script classes that uses the default copy operator
  3945. bc->Call(asBC_CALLSYS, beh->copy, 2*AS_PTR_SIZE);
  3946. bc->Instr(asBC_PshRPtr);
  3947. }
  3948. else
  3949. {
  3950. // Default copy operator
  3951. if( lvalue->dataType.GetSizeInMemoryDWords() == 0 ||
  3952. !(lvalue->dataType.GetObjectType()->flags & asOBJ_POD) )
  3953. {
  3954. asCString msg;
  3955. msg.Format(TXT_NO_DEFAULT_COPY_OP_FOR_s, lvalue->dataType.GetObjectType()->name.AddressOf());
  3956. Error(msg, node);
  3957. return -1;
  3958. }
  3959. // Copy larger data types from a reference
  3960. // TODO: runtime optimize: COPY should pop both arguments and store the reference in the register.
  3961. bc->InstrSHORT_DW(asBC_COPY, (short)lvalue->dataType.GetSizeInMemoryDWords(), engine->GetTypeIdFromDataType(lvalue->dataType));
  3962. }
  3963. }
  3964. else
  3965. {
  3966. // TODO: The object handle can be stored in a variable as well
  3967. if( !lvalue->dataType.IsReference() )
  3968. {
  3969. Error(TXT_NOT_VALID_REFERENCE, node);
  3970. return -1;
  3971. }
  3972. bc->InstrPTR(asBC_REFCPY, lvalue->dataType.GetObjectType());
  3973. // Mark variable as initialized
  3974. if( variables )
  3975. {
  3976. sVariable *v = variables->GetVariableByOffset(lvalue->stackOffset);
  3977. if( v ) v->isInitialized = true;
  3978. }
  3979. }
  3980. return 0;
  3981. }
  3982. bool asCCompiler::CompileRefCast(asSExprContext *ctx, const asCDataType &to, bool isExplicit, asCScriptNode *node, bool generateCode)
  3983. {
  3984. bool conversionDone = false;
  3985. asCArray<int> ops;
  3986. asUINT n;
  3987. if( ctx->type.dataType.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT )
  3988. {
  3989. // We need it to be a reference
  3990. if( !ctx->type.dataType.IsReference() )
  3991. {
  3992. asCDataType to = ctx->type.dataType;
  3993. to.MakeReference(true);
  3994. ImplicitConversion(ctx, to, 0, isExplicit ? asIC_EXPLICIT_REF_CAST : asIC_IMPLICIT_CONV, generateCode);
  3995. }
  3996. if( isExplicit )
  3997. {
  3998. // Allow dynamic cast between object handles (only for script objects).
  3999. // At run time this may result in a null handle,
  4000. // which when used will throw an exception
  4001. conversionDone = true;
  4002. if( generateCode )
  4003. {
  4004. ctx->bc.InstrDWORD(asBC_Cast, engine->GetTypeIdFromDataType(to));
  4005. // Allocate a temporary variable for the returned object
  4006. int returnOffset = AllocateVariable(to, true);
  4007. // Move the pointer from the object register to the temporary variable
  4008. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  4009. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  4010. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4011. ctx->type.SetVariable(to, returnOffset, true);
  4012. ctx->type.dataType.MakeReference(true);
  4013. }
  4014. else
  4015. {
  4016. ctx->type.dataType = to;
  4017. ctx->type.dataType.MakeReference(true);
  4018. }
  4019. }
  4020. else
  4021. {
  4022. if( ctx->type.dataType.GetObjectType()->DerivesFrom(to.GetObjectType()) )
  4023. {
  4024. conversionDone = true;
  4025. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4026. }
  4027. }
  4028. }
  4029. else
  4030. {
  4031. // Find a suitable registered behaviour
  4032. asSTypeBehaviour *beh = &ctx->type.dataType.GetObjectType()->beh;
  4033. for( n = 0; n < beh->operators.GetLength(); n+= 2 )
  4034. {
  4035. if( (isExplicit && asBEHAVE_REF_CAST == beh->operators[n]) ||
  4036. asBEHAVE_IMPLICIT_REF_CAST == beh->operators[n] )
  4037. {
  4038. int funcId = beh->operators[n+1];
  4039. // Is the operator for the output type?
  4040. asCScriptFunction *func = engine->scriptFunctions[funcId];
  4041. if( func->returnType.GetObjectType() != to.GetObjectType() )
  4042. continue;
  4043. ops.PushLast(funcId);
  4044. }
  4045. }
  4046. // It shouldn't be possible to have more than one
  4047. asASSERT( ops.GetLength() <= 1 );
  4048. // Should only have one behaviour for each output type
  4049. if( ops.GetLength() == 1 )
  4050. {
  4051. if( generateCode )
  4052. {
  4053. // TODO: runtime optimize: Instead of producing bytecode for checking if the handle is
  4054. // null, we can create a special CALLSYS instruction that checks
  4055. // if the object pointer is null and if so sets the object register
  4056. // to null directly without executing the function.
  4057. //
  4058. // Alternatively I could force the ref cast behaviours be global
  4059. // functions with 1 parameter, even though they should still be
  4060. // registered with RegisterObjectBehaviour()
  4061. // Add code to avoid calling the cast behaviour if the handle is already null,
  4062. // because that will raise a null pointer exception due to the cast behaviour
  4063. // being a class method, and the this pointer cannot be null.
  4064. if( ctx->type.isVariable )
  4065. ctx->bc.Instr(asBC_PopPtr);
  4066. else
  4067. {
  4068. Dereference(ctx, true);
  4069. ConvertToVariable(ctx);
  4070. }
  4071. // TODO: runtime optimize: should have immediate comparison for null pointer
  4072. int offset = AllocateVariable(asCDataType::CreateNullHandle(), true);
  4073. // 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)
  4074. ctx->bc.InstrSHORT(asBC_ClrVPtr, (asWORD)offset);
  4075. ctx->bc.InstrW_W(asBC_CmpPtr, ctx->type.stackOffset, offset);
  4076. DeallocateVariable(offset);
  4077. int afterLabel = nextLabel++;
  4078. ctx->bc.InstrDWORD(asBC_JZ, afterLabel);
  4079. // Call the cast operator
  4080. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4081. ctx->bc.Instr(asBC_RDSPtr);
  4082. ctx->type.dataType.MakeReference(false);
  4083. asCTypeInfo objType = ctx->type;
  4084. asCArray<asSExprContext *> args;
  4085. MakeFunctionCall(ctx, ops[0], objType.dataType.GetObjectType(), args, node);
  4086. ctx->bc.Instr(asBC_PopPtr);
  4087. int endLabel = nextLabel++;
  4088. ctx->bc.InstrINT(asBC_JMP, endLabel);
  4089. ctx->bc.Label((short)afterLabel);
  4090. // Make a NULL pointer
  4091. ctx->bc.InstrSHORT(asBC_ClrVPtr, ctx->type.stackOffset);
  4092. ctx->bc.Label((short)endLabel);
  4093. // Since we're receiving a handle, we can release the original variable
  4094. ReleaseTemporaryVariable(objType, &ctx->bc);
  4095. // Push the reference to the handle on the stack
  4096. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4097. }
  4098. else
  4099. {
  4100. asCScriptFunction *func = engine->scriptFunctions[ops[0]];
  4101. ctx->type.Set(func->returnType);
  4102. }
  4103. }
  4104. else if( ops.GetLength() == 0 )
  4105. {
  4106. // Check for the generic ref cast behaviour
  4107. for( n = 0; n < beh->operators.GetLength(); n+= 2 )
  4108. {
  4109. if( (isExplicit && asBEHAVE_REF_CAST == beh->operators[n]) ||
  4110. asBEHAVE_IMPLICIT_REF_CAST == beh->operators[n] )
  4111. {
  4112. int funcId = beh->operators[n+1];
  4113. // Does the operator take the ?&out parameter?
  4114. asCScriptFunction *func = engine->scriptFunctions[funcId];
  4115. if( func->parameterTypes.GetLength() != 1 ||
  4116. func->parameterTypes[0].GetTokenType() != ttQuestion ||
  4117. func->inOutFlags[0] != asTM_OUTREF )
  4118. continue;
  4119. ops.PushLast(funcId);
  4120. }
  4121. }
  4122. // It shouldn't be possible to have more than one
  4123. asASSERT( ops.GetLength() <= 1 );
  4124. if( ops.GetLength() == 1 )
  4125. {
  4126. if( generateCode )
  4127. {
  4128. asASSERT(to.IsObjectHandle());
  4129. // Allocate a temporary variable of the requested handle type
  4130. int stackOffset = AllocateVariableNotIn(to, true, false, ctx);
  4131. // Pass the reference of that variable to the function as output parameter
  4132. asCDataType toRef(to);
  4133. toRef.MakeReference(true);
  4134. asCArray<asSExprContext *> args;
  4135. asSExprContext arg(engine);
  4136. arg.bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  4137. // Don't mark the variable as temporary, so it won't be freed too early
  4138. arg.type.SetVariable(toRef, stackOffset, false);
  4139. arg.type.isLValue = true;
  4140. arg.type.isExplicitHandle = true;
  4141. args.PushLast(&arg);
  4142. asCTypeInfo prev = ctx->type;
  4143. // Call the behaviour method
  4144. MakeFunctionCall(ctx, ops[0], ctx->type.dataType.GetObjectType(), args, node);
  4145. // Release previous temporary variable
  4146. ReleaseTemporaryVariable(prev, &ctx->bc);
  4147. // Use the reference to the variable as the result of the expression
  4148. // Now we can mark the variable as temporary
  4149. ctx->type.SetVariable(toRef, stackOffset, true);
  4150. ctx->bc.InstrSHORT(asBC_PSF, (short)stackOffset);
  4151. }
  4152. else
  4153. {
  4154. // All casts are legal
  4155. ctx->type.Set(to);
  4156. }
  4157. }
  4158. }
  4159. }
  4160. return conversionDone;
  4161. }
  4162. asUINT asCCompiler::ImplicitConvPrimitiveToPrimitive(asSExprContext *ctx, const asCDataType &toOrig, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  4163. {
  4164. asCDataType to = toOrig;
  4165. to.MakeReference(false);
  4166. asASSERT( !ctx->type.dataType.IsReference() );
  4167. // Maybe no conversion is needed
  4168. if( to.IsEqualExceptConst(ctx->type.dataType) )
  4169. {
  4170. // A primitive is const or not
  4171. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4172. return asCC_NO_CONV;
  4173. }
  4174. // Is the conversion an ambiguous enum value?
  4175. if( ctx->enumValue != "" )
  4176. {
  4177. if( to.IsEnumType() )
  4178. {
  4179. // Attempt to resolve an ambiguous enum value
  4180. asCDataType out;
  4181. asDWORD value;
  4182. if( builder->GetEnumValueFromObjectType(to.GetObjectType(), ctx->enumValue.AddressOf(), out, value) )
  4183. {
  4184. ctx->type.SetConstantDW(out, value);
  4185. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4186. // It wasn't really a conversion. The compiler just resolved the ambiguity (or not)
  4187. return asCC_NO_CONV;
  4188. }
  4189. }
  4190. // The enum value is ambiguous
  4191. if( node && generateCode )
  4192. Error(TXT_FOUND_MULTIPLE_ENUM_VALUES, node);
  4193. // Set a dummy to allow the compiler to try to continue the conversion
  4194. ctx->type.SetDummy();
  4195. }
  4196. // Determine the cost of this conversion
  4197. asUINT cost = asCC_NO_CONV;
  4198. if( (to.IsIntegerType() || to.IsUnsignedType()) && (ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  4199. cost = asCC_INT_FLOAT_CONV;
  4200. else if( (to.IsFloatType() || to.IsDoubleType()) && (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType()) )
  4201. cost = asCC_INT_FLOAT_CONV;
  4202. else if( to.IsUnsignedType() && ctx->type.dataType.IsIntegerType() )
  4203. cost = asCC_SIGNED_CONV;
  4204. else if( to.IsIntegerType() && ctx->type.dataType.IsUnsignedType() )
  4205. cost = asCC_SIGNED_CONV;
  4206. else if( to.GetSizeInMemoryBytes() || ctx->type.dataType.GetSizeInMemoryBytes() )
  4207. cost = asCC_PRIMITIVE_SIZE_CONV;
  4208. // Start by implicitly converting constant values
  4209. if( ctx->type.isConstant )
  4210. {
  4211. ImplicitConversionConstant(ctx, to, node, convType);
  4212. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4213. return cost;
  4214. }
  4215. // Allow implicit conversion between numbers
  4216. if( generateCode )
  4217. {
  4218. // When generating the code the decision has already been made, so we don't bother determining the cost
  4219. // Convert smaller types to 32bit first
  4220. int s = ctx->type.dataType.GetSizeInMemoryBytes();
  4221. if( s < 4 )
  4222. {
  4223. ConvertToTempVariable(ctx);
  4224. if( ctx->type.dataType.IsIntegerType() )
  4225. {
  4226. if( s == 1 )
  4227. ctx->bc.InstrSHORT(asBC_sbTOi, ctx->type.stackOffset);
  4228. else if( s == 2 )
  4229. ctx->bc.InstrSHORT(asBC_swTOi, ctx->type.stackOffset);
  4230. ctx->type.dataType.SetTokenType(ttInt);
  4231. }
  4232. else if( ctx->type.dataType.IsUnsignedType() )
  4233. {
  4234. if( s == 1 )
  4235. ctx->bc.InstrSHORT(asBC_ubTOi, ctx->type.stackOffset);
  4236. else if( s == 2 )
  4237. ctx->bc.InstrSHORT(asBC_uwTOi, ctx->type.stackOffset);
  4238. ctx->type.dataType.SetTokenType(ttUInt);
  4239. }
  4240. }
  4241. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1 && !to.IsEnumType()) ||
  4242. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  4243. {
  4244. if( ctx->type.dataType.IsIntegerType() ||
  4245. ctx->type.dataType.IsUnsignedType() )
  4246. {
  4247. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4248. {
  4249. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4250. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4251. }
  4252. else
  4253. {
  4254. ConvertToTempVariable(ctx);
  4255. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4256. int offset = AllocateVariable(to, true);
  4257. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  4258. ctx->type.SetVariable(to, offset, true);
  4259. }
  4260. }
  4261. else if( ctx->type.dataType.IsFloatType() )
  4262. {
  4263. ConvertToTempVariable(ctx);
  4264. ctx->bc.InstrSHORT(asBC_fTOi, ctx->type.stackOffset);
  4265. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4266. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4267. if( convType != asIC_EXPLICIT_VAL_CAST )
  4268. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4269. }
  4270. else if( ctx->type.dataType.IsDoubleType() )
  4271. {
  4272. ConvertToTempVariable(ctx);
  4273. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4274. int offset = AllocateVariable(to, true);
  4275. ctx->bc.InstrW_W(asBC_dTOi, offset, ctx->type.stackOffset);
  4276. ctx->type.SetVariable(to, offset, true);
  4277. if( convType != asIC_EXPLICIT_VAL_CAST )
  4278. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4279. }
  4280. // Convert to smaller integer if necessary
  4281. int s = to.GetSizeInMemoryBytes();
  4282. if( s < 4 )
  4283. {
  4284. ConvertToTempVariable(ctx);
  4285. if( s == 1 )
  4286. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  4287. else if( s == 2 )
  4288. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  4289. }
  4290. }
  4291. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  4292. {
  4293. if( ctx->type.dataType.IsIntegerType() ||
  4294. ctx->type.dataType.IsUnsignedType() )
  4295. {
  4296. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4297. {
  4298. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4299. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4300. }
  4301. else
  4302. {
  4303. ConvertToTempVariable(ctx);
  4304. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4305. int offset = AllocateVariable(to, true);
  4306. if( ctx->type.dataType.IsUnsignedType() )
  4307. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  4308. else
  4309. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  4310. ctx->type.SetVariable(to, offset, true);
  4311. }
  4312. }
  4313. else if( ctx->type.dataType.IsFloatType() )
  4314. {
  4315. ConvertToTempVariable(ctx);
  4316. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4317. int offset = AllocateVariable(to, true);
  4318. ctx->bc.InstrW_W(asBC_fTOi64, offset, ctx->type.stackOffset);
  4319. ctx->type.SetVariable(to, offset, true);
  4320. if( convType != asIC_EXPLICIT_VAL_CAST )
  4321. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4322. }
  4323. else if( ctx->type.dataType.IsDoubleType() )
  4324. {
  4325. ConvertToTempVariable(ctx);
  4326. ctx->bc.InstrSHORT(asBC_dTOi64, ctx->type.stackOffset);
  4327. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4328. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4329. if( convType != asIC_EXPLICIT_VAL_CAST )
  4330. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4331. }
  4332. }
  4333. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  4334. {
  4335. if( ctx->type.dataType.IsIntegerType() ||
  4336. ctx->type.dataType.IsUnsignedType() )
  4337. {
  4338. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4339. {
  4340. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4341. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4342. }
  4343. else
  4344. {
  4345. ConvertToTempVariable(ctx);
  4346. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4347. int offset = AllocateVariable(to, true);
  4348. ctx->bc.InstrW_W(asBC_i64TOi, offset, ctx->type.stackOffset);
  4349. ctx->type.SetVariable(to, offset, true);
  4350. }
  4351. }
  4352. else if( ctx->type.dataType.IsFloatType() )
  4353. {
  4354. ConvertToTempVariable(ctx);
  4355. ctx->bc.InstrSHORT(asBC_fTOu, ctx->type.stackOffset);
  4356. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4357. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4358. if( convType != asIC_EXPLICIT_VAL_CAST )
  4359. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4360. }
  4361. else if( ctx->type.dataType.IsDoubleType() )
  4362. {
  4363. ConvertToTempVariable(ctx);
  4364. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4365. int offset = AllocateVariable(to, true);
  4366. ctx->bc.InstrW_W(asBC_dTOu, offset, ctx->type.stackOffset);
  4367. ctx->type.SetVariable(to, offset, true);
  4368. if( convType != asIC_EXPLICIT_VAL_CAST )
  4369. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4370. }
  4371. // Convert to smaller integer if necessary
  4372. int s = to.GetSizeInMemoryBytes();
  4373. if( s < 4 )
  4374. {
  4375. ConvertToTempVariable(ctx);
  4376. if( s == 1 )
  4377. ctx->bc.InstrSHORT(asBC_iTOb, ctx->type.stackOffset);
  4378. else if( s == 2 )
  4379. ctx->bc.InstrSHORT(asBC_iTOw, ctx->type.stackOffset);
  4380. }
  4381. }
  4382. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  4383. {
  4384. if( ctx->type.dataType.IsIntegerType() ||
  4385. ctx->type.dataType.IsUnsignedType() )
  4386. {
  4387. if( ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4388. {
  4389. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4390. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4391. }
  4392. else
  4393. {
  4394. ConvertToTempVariable(ctx);
  4395. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4396. int offset = AllocateVariable(to, true);
  4397. if( ctx->type.dataType.IsUnsignedType() )
  4398. ctx->bc.InstrW_W(asBC_uTOi64, offset, ctx->type.stackOffset);
  4399. else
  4400. ctx->bc.InstrW_W(asBC_iTOi64, offset, ctx->type.stackOffset);
  4401. ctx->type.SetVariable(to, offset, true);
  4402. }
  4403. }
  4404. else if( ctx->type.dataType.IsFloatType() )
  4405. {
  4406. ConvertToTempVariable(ctx);
  4407. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4408. int offset = AllocateVariable(to, true);
  4409. ctx->bc.InstrW_W(asBC_fTOu64, offset, ctx->type.stackOffset);
  4410. ctx->type.SetVariable(to, offset, true);
  4411. if( convType != asIC_EXPLICIT_VAL_CAST )
  4412. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4413. }
  4414. else if( ctx->type.dataType.IsDoubleType() )
  4415. {
  4416. ConvertToTempVariable(ctx);
  4417. ctx->bc.InstrSHORT(asBC_dTOu64, ctx->type.stackOffset);
  4418. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4419. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4420. if( convType != asIC_EXPLICIT_VAL_CAST )
  4421. Warning(TXT_FLOAT_CONV_TO_INT_CAUSE_TRUNC, node);
  4422. }
  4423. }
  4424. else if( to.IsFloatType() )
  4425. {
  4426. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4427. {
  4428. ConvertToTempVariable(ctx);
  4429. ctx->bc.InstrSHORT(asBC_iTOf, ctx->type.stackOffset);
  4430. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4431. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4432. }
  4433. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4434. {
  4435. ConvertToTempVariable(ctx);
  4436. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4437. int offset = AllocateVariable(to, true);
  4438. ctx->bc.InstrW_W(asBC_i64TOf, offset, ctx->type.stackOffset);
  4439. ctx->type.SetVariable(to, offset, true);
  4440. }
  4441. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4442. {
  4443. ConvertToTempVariable(ctx);
  4444. ctx->bc.InstrSHORT(asBC_uTOf, ctx->type.stackOffset);
  4445. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4446. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4447. }
  4448. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4449. {
  4450. ConvertToTempVariable(ctx);
  4451. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4452. int offset = AllocateVariable(to, true);
  4453. ctx->bc.InstrW_W(asBC_u64TOf, offset, ctx->type.stackOffset);
  4454. ctx->type.SetVariable(to, offset, true);
  4455. }
  4456. else if( ctx->type.dataType.IsDoubleType() )
  4457. {
  4458. ConvertToTempVariable(ctx);
  4459. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4460. int offset = AllocateVariable(to, true);
  4461. ctx->bc.InstrW_W(asBC_dTOf, offset, ctx->type.stackOffset);
  4462. ctx->type.SetVariable(to, offset, true);
  4463. }
  4464. }
  4465. else if( to.IsDoubleType() )
  4466. {
  4467. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4468. {
  4469. ConvertToTempVariable(ctx);
  4470. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4471. int offset = AllocateVariable(to, true);
  4472. ctx->bc.InstrW_W(asBC_iTOd, offset, ctx->type.stackOffset);
  4473. ctx->type.SetVariable(to, offset, true);
  4474. }
  4475. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4476. {
  4477. ConvertToTempVariable(ctx);
  4478. ctx->bc.InstrSHORT(asBC_i64TOd, ctx->type.stackOffset);
  4479. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4480. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4481. }
  4482. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  4483. {
  4484. ConvertToTempVariable(ctx);
  4485. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4486. int offset = AllocateVariable(to, true);
  4487. ctx->bc.InstrW_W(asBC_uTOd, offset, ctx->type.stackOffset);
  4488. ctx->type.SetVariable(to, offset, true);
  4489. }
  4490. else if( ctx->type.dataType.IsUnsignedType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  4491. {
  4492. ConvertToTempVariable(ctx);
  4493. ctx->bc.InstrSHORT(asBC_u64TOd, ctx->type.stackOffset);
  4494. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4495. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4496. }
  4497. else if( ctx->type.dataType.IsFloatType() )
  4498. {
  4499. ConvertToTempVariable(ctx);
  4500. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4501. int offset = AllocateVariable(to, true);
  4502. ctx->bc.InstrW_W(asBC_fTOd, offset, ctx->type.stackOffset);
  4503. ctx->type.SetVariable(to, offset, true);
  4504. }
  4505. }
  4506. }
  4507. else
  4508. {
  4509. if( ((to.IsIntegerType() && !to.IsEnumType()) || to.IsUnsignedType() ||
  4510. to.IsFloatType() || to.IsDoubleType() ||
  4511. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST)) &&
  4512. (ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() ||
  4513. ctx->type.dataType.IsFloatType() || ctx->type.dataType.IsDoubleType()) )
  4514. {
  4515. ctx->type.dataType.SetTokenType(to.GetTokenType());
  4516. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4517. }
  4518. }
  4519. // Primitive types on the stack, can be const or non-const
  4520. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  4521. return cost;
  4522. }
  4523. asUINT asCCompiler::ImplicitConversion(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  4524. {
  4525. asASSERT( ctx->type.dataType.GetTokenType() != ttUnrecognizedToken ||
  4526. ctx->type.dataType.IsNullHandle() );
  4527. // No conversion from void to any other type
  4528. if( ctx->type.dataType.GetTokenType() == ttVoid )
  4529. return asCC_NO_CONV;
  4530. // Do we want a var type?
  4531. if( to.GetTokenType() == ttQuestion )
  4532. {
  4533. // Any type can be converted to a var type, but only when not generating code
  4534. asASSERT( !generateCode );
  4535. ctx->type.dataType = to;
  4536. return asCC_VARIABLE_CONV;
  4537. }
  4538. // Do we want a primitive?
  4539. else if( to.IsPrimitive() )
  4540. {
  4541. if( !ctx->type.dataType.IsPrimitive() )
  4542. return ImplicitConvObjectToPrimitive(ctx, to, node, convType, generateCode);
  4543. else
  4544. return ImplicitConvPrimitiveToPrimitive(ctx, to, node, convType, generateCode);
  4545. }
  4546. else // The target is a complex type
  4547. {
  4548. if( ctx->type.dataType.IsPrimitive() )
  4549. return ImplicitConvPrimitiveToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  4550. else if( ctx->type.IsNullConstant() || ctx->type.dataType.GetObjectType() )
  4551. return ImplicitConvObjectToObject(ctx, to, node, convType, generateCode, allowObjectConstruct);
  4552. }
  4553. return asCC_NO_CONV;
  4554. }
  4555. asUINT asCCompiler::ImplicitConvObjectToPrimitive(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  4556. {
  4557. if( ctx->type.isExplicitHandle )
  4558. {
  4559. // An explicit handle cannot be converted to a primitive
  4560. if( convType != asIC_IMPLICIT_CONV && node )
  4561. {
  4562. asCString str;
  4563. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  4564. Error(str, node);
  4565. }
  4566. return asCC_NO_CONV;
  4567. }
  4568. // TODO: Must use the const cast behaviour if the object is read-only
  4569. // Find matching value cast behaviours
  4570. // Here we're only interested in those that convert the type to a primitive type
  4571. asCArray<int> funcs;
  4572. asSTypeBehaviour *beh = ctx->type.dataType.GetBehaviour();
  4573. if( beh )
  4574. {
  4575. if( convType == asIC_EXPLICIT_VAL_CAST )
  4576. {
  4577. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  4578. {
  4579. // accept both implicit and explicit cast
  4580. if( (beh->operators[n] == asBEHAVE_VALUE_CAST ||
  4581. beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST) &&
  4582. builder->GetFunctionDescription(beh->operators[n+1])->returnType.IsPrimitive() )
  4583. funcs.PushLast(beh->operators[n+1]);
  4584. }
  4585. }
  4586. else
  4587. {
  4588. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  4589. {
  4590. // accept only implicit cast
  4591. if( beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST &&
  4592. builder->GetFunctionDescription(beh->operators[n+1])->returnType.IsPrimitive() )
  4593. funcs.PushLast(beh->operators[n+1]);
  4594. }
  4595. }
  4596. }
  4597. // This matrix describes the priorities of the types to search for, for each target type
  4598. // The first column is the target type, the priorities goes from left to right
  4599. eTokenType matchMtx[10][10] =
  4600. {
  4601. {ttDouble, ttFloat, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  4602. {ttFloat, ttDouble, ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8},
  4603. {ttInt64, ttUInt64, ttInt, ttUInt, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  4604. {ttUInt64, ttInt64, ttUInt, ttInt, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  4605. {ttInt, ttUInt, ttInt64, ttUInt64, ttInt16, ttUInt16, ttInt8, ttUInt8, ttDouble, ttFloat},
  4606. {ttUInt, ttInt, ttUInt64, ttInt64, ttUInt16, ttInt16, ttUInt8, ttInt8, ttDouble, ttFloat},
  4607. {ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttInt8, ttUInt8, ttDouble, ttFloat},
  4608. {ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttUInt8, ttInt8, ttDouble, ttFloat},
  4609. {ttInt8, ttUInt8, ttInt16, ttUInt16, ttInt, ttUInt, ttInt64, ttUInt64, ttDouble, ttFloat},
  4610. {ttUInt8, ttInt8, ttUInt16, ttInt16, ttUInt, ttInt, ttUInt64, ttInt64, ttDouble, ttFloat},
  4611. };
  4612. // Which row to use?
  4613. eTokenType *row = 0;
  4614. for( unsigned int type = 0; type < 10; type++ )
  4615. {
  4616. if( to.GetTokenType() == matchMtx[type][0] )
  4617. {
  4618. row = &matchMtx[type][0];
  4619. break;
  4620. }
  4621. }
  4622. // Find the best matching cast operator
  4623. int funcId = 0;
  4624. if( row )
  4625. {
  4626. asCDataType target(to);
  4627. // Priority goes from left to right in the matrix
  4628. for( unsigned int attempt = 0; attempt < 10 && funcId == 0; attempt++ )
  4629. {
  4630. target.SetTokenType(row[attempt]);
  4631. for( unsigned int n = 0; n < funcs.GetLength(); n++ )
  4632. {
  4633. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[n]);
  4634. if( descr->returnType.IsEqualExceptRefAndConst(target) )
  4635. {
  4636. funcId = funcs[n];
  4637. break;
  4638. }
  4639. }
  4640. }
  4641. }
  4642. // Did we find a suitable function?
  4643. if( funcId != 0 )
  4644. {
  4645. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  4646. if( generateCode )
  4647. {
  4648. asCTypeInfo objType = ctx->type;
  4649. Dereference(ctx, true);
  4650. PerformFunctionCall(funcId, ctx);
  4651. ReleaseTemporaryVariable(objType, &ctx->bc);
  4652. }
  4653. else
  4654. ctx->type.Set(descr->returnType);
  4655. // Allow one more implicit conversion to another primitive type
  4656. return asCC_OBJ_TO_PRIMITIVE_CONV + ImplicitConversion(ctx, to, node, convType, generateCode, false);
  4657. }
  4658. else
  4659. {
  4660. if( convType != asIC_IMPLICIT_CONV && node )
  4661. {
  4662. asCString str;
  4663. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  4664. Error(str, node);
  4665. }
  4666. }
  4667. return asCC_NO_CONV;
  4668. }
  4669. asUINT asCCompiler::ImplicitConvObjectRef(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode)
  4670. {
  4671. // Convert null to any object type handle, but not to a non-handle type
  4672. if( ctx->type.IsNullConstant() && ctx->methodName == "" )
  4673. {
  4674. if( to.IsObjectHandle() )
  4675. {
  4676. ctx->type.dataType = to;
  4677. return asCC_REF_CONV;
  4678. }
  4679. return asCC_NO_CONV;
  4680. }
  4681. asASSERT(ctx->type.dataType.GetObjectType() || ctx->methodName != "");
  4682. // First attempt to convert the base type without instanciating another instance
  4683. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() && ctx->methodName == "" )
  4684. {
  4685. // If the to type is an interface and the from type implements it, then we can convert it immediately
  4686. if( ctx->type.dataType.GetObjectType()->Implements(to.GetObjectType()) )
  4687. {
  4688. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4689. return asCC_REF_CONV;
  4690. }
  4691. // If the to type is a class and the from type derives from it, then we can convert it immediately
  4692. else if( ctx->type.dataType.GetObjectType()->DerivesFrom(to.GetObjectType()) )
  4693. {
  4694. ctx->type.dataType.SetObjectType(to.GetObjectType());
  4695. return asCC_REF_CONV;
  4696. }
  4697. // If the types are not equal yet, then we may still be able to find a reference cast
  4698. else if( ctx->type.dataType.GetObjectType() != to.GetObjectType() )
  4699. {
  4700. // A ref cast must not remove the constness
  4701. bool isConst = false;
  4702. if( (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) ||
  4703. (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) )
  4704. isConst = true;
  4705. // We may still be able to find an implicit ref cast behaviour
  4706. CompileRefCast(ctx, to, convType == asIC_EXPLICIT_REF_CAST, node, generateCode);
  4707. ctx->type.dataType.MakeHandleToConst(isConst);
  4708. // Was the conversion done?
  4709. if( ctx->type.dataType.GetObjectType() == to.GetObjectType() )
  4710. return asCC_REF_CONV;
  4711. }
  4712. }
  4713. // Convert matching function types
  4714. if( to.GetFuncDef() )
  4715. {
  4716. // If the input expression is already a funcdef, check if it can be converted
  4717. if( ctx->type.dataType.GetFuncDef() &&
  4718. to.GetFuncDef() != ctx->type.dataType.GetFuncDef() )
  4719. {
  4720. asCScriptFunction *toFunc = to.GetFuncDef();
  4721. asCScriptFunction *fromFunc = ctx->type.dataType.GetFuncDef();
  4722. if( toFunc->IsSignatureExceptNameEqual(fromFunc) )
  4723. {
  4724. ctx->type.dataType.SetFuncDef(toFunc);
  4725. return asCC_REF_CONV;
  4726. }
  4727. }
  4728. // If the input expression is a deferred function ref, check if there is a matching func
  4729. if( ctx->methodName != "" )
  4730. {
  4731. // Determine the namespace
  4732. asSNameSpace *ns = 0;
  4733. asCString name = "";
  4734. int pos = ctx->methodName.FindLast("::");
  4735. if( pos >= 0 )
  4736. {
  4737. asCString nsName = ctx->methodName.SubString(0, pos+2);
  4738. // Trim off the last ::
  4739. if( nsName.GetLength() > 2 )
  4740. nsName.SetLength(nsName.GetLength()-2);
  4741. ns = DetermineNameSpace(nsName);
  4742. name = ctx->methodName.SubString(pos+2);
  4743. }
  4744. else
  4745. {
  4746. DetermineNameSpace("");
  4747. name = ctx->methodName;
  4748. }
  4749. asCArray<int> funcs;
  4750. if( ns )
  4751. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  4752. // Check if any of the functions have perfect match
  4753. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  4754. {
  4755. asCScriptFunction *func = builder->GetFunctionDescription(funcs[n]);
  4756. if( to.GetFuncDef()->IsSignatureExceptNameEqual(func) )
  4757. {
  4758. if( generateCode )
  4759. {
  4760. ctx->bc.InstrPTR(asBC_FuncPtr, func);
  4761. // Make sure the identified function is shared if we're compiling a shared function
  4762. if( !func->IsShared() && outFunc->IsShared() )
  4763. {
  4764. asCString msg;
  4765. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, func->GetDeclaration());
  4766. Error(msg, node);
  4767. }
  4768. }
  4769. ctx->type.dataType = asCDataType::CreateFuncDef(to.GetFuncDef());
  4770. return asCC_REF_CONV;
  4771. }
  4772. }
  4773. }
  4774. }
  4775. return asCC_NO_CONV;
  4776. }
  4777. asUINT asCCompiler::ImplicitConvObjectValue(asSExprContext *ctx, const asCDataType &to, asCScriptNode * /*node*/, EImplicitConv convType, bool generateCode)
  4778. {
  4779. asUINT cost = asCC_NO_CONV;
  4780. // If the base type is still different, and we are allowed to instance
  4781. // another object then we can try an implicit value cast
  4782. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  4783. {
  4784. // TODO: Implement support for implicit constructor/factory
  4785. asCArray<int> funcs;
  4786. asSTypeBehaviour *beh = ctx->type.dataType.GetBehaviour();
  4787. if( beh )
  4788. {
  4789. if( convType == asIC_EXPLICIT_VAL_CAST )
  4790. {
  4791. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  4792. {
  4793. // accept both implicit and explicit cast
  4794. if( (beh->operators[n] == asBEHAVE_VALUE_CAST ||
  4795. beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST) &&
  4796. builder->GetFunctionDescription(beh->operators[n+1])->returnType.GetObjectType() == to.GetObjectType() )
  4797. funcs.PushLast(beh->operators[n+1]);
  4798. }
  4799. }
  4800. else
  4801. {
  4802. for( unsigned int n = 0; n < beh->operators.GetLength(); n += 2 )
  4803. {
  4804. // accept only implicit cast
  4805. if( beh->operators[n] == asBEHAVE_IMPLICIT_VALUE_CAST &&
  4806. builder->GetFunctionDescription(beh->operators[n+1])->returnType.GetObjectType() == to.GetObjectType() )
  4807. funcs.PushLast(beh->operators[n+1]);
  4808. }
  4809. }
  4810. }
  4811. // TODO: If there are multiple valid value casts, then we must choose the most appropriate one
  4812. asASSERT( funcs.GetLength() <= 1 );
  4813. if( funcs.GetLength() == 1 )
  4814. {
  4815. asCScriptFunction *f = builder->GetFunctionDescription(funcs[0]);
  4816. if( generateCode )
  4817. {
  4818. asCTypeInfo objType = ctx->type;
  4819. Dereference(ctx, true);
  4820. bool useVariable = false;
  4821. int stackOffset = 0;
  4822. if( f->DoesReturnOnStack() )
  4823. {
  4824. useVariable = true;
  4825. stackOffset = AllocateVariable(f->returnType, true);
  4826. // Push the pointer to the pre-allocated space for the return value
  4827. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  4828. // The object pointer is already on the stack, but should be the top
  4829. // one, so we need to swap the pointers in order to get the correct
  4830. ctx->bc.Instr(asBC_SwapPtr);
  4831. }
  4832. PerformFunctionCall(funcs[0], ctx, false, 0, 0, useVariable, stackOffset);
  4833. ReleaseTemporaryVariable(objType, &ctx->bc);
  4834. }
  4835. else
  4836. ctx->type.Set(f->returnType);
  4837. cost = asCC_TO_OBJECT_CONV;
  4838. }
  4839. }
  4840. return cost;
  4841. }
  4842. asUINT asCCompiler::ImplicitConvObjectToObject(asSExprContext *ctx, const asCDataType &to, asCScriptNode *node, EImplicitConv convType, bool generateCode, bool allowObjectConstruct)
  4843. {
  4844. // First try a ref cast
  4845. asUINT cost = ImplicitConvObjectRef(ctx, to, node, convType, generateCode);
  4846. // If the desired type is an asOBJ_ASHANDLE then we'll assume it is allowed to implicitly
  4847. // construct the object through any of the available constructors
  4848. if( to.GetObjectType() && (to.GetObjectType()->flags & asOBJ_ASHANDLE) && to.GetObjectType() != ctx->type.dataType.GetObjectType() && allowObjectConstruct )
  4849. {
  4850. asCArray<int> funcs;
  4851. funcs = to.GetObjectType()->beh.constructors;
  4852. asCArray<asSExprContext *> args;
  4853. args.PushLast(ctx);
  4854. cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, node, 0, 0, false, true, false);
  4855. // Did we find a matching constructor?
  4856. if( funcs.GetLength() == 1 )
  4857. {
  4858. if( generateCode )
  4859. {
  4860. // If the ASHANDLE receives a variable type parameter, then we need to
  4861. // make sure the expression is treated as a handle and not as a value
  4862. asCScriptFunction *func = engine->scriptFunctions[funcs[0]];
  4863. if( func->parameterTypes[0].GetTokenType() == ttQuestion )
  4864. {
  4865. if( !ctx->type.isExplicitHandle )
  4866. {
  4867. asCDataType toHandle = ctx->type.dataType;
  4868. toHandle.MakeHandle(true);
  4869. toHandle.MakeReference(true);
  4870. toHandle.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  4871. ImplicitConversion(ctx, toHandle, node, asIC_IMPLICIT_CONV, true, false);
  4872. asASSERT( ctx->type.dataType.IsObjectHandle() );
  4873. }
  4874. ctx->type.isExplicitHandle = true;
  4875. }
  4876. // TODO: This should really reuse the code from CompileConstructCall
  4877. // Allocate the new object
  4878. asCTypeInfo tempObj;
  4879. tempObj.dataType = to;
  4880. tempObj.dataType.MakeReference(false);
  4881. tempObj.stackOffset = (short)AllocateVariable(tempObj.dataType, true);
  4882. tempObj.dataType.MakeReference(true);
  4883. tempObj.isTemporary = true;
  4884. tempObj.isVariable = true;
  4885. bool onHeap = IsVariableOnHeap(tempObj.stackOffset);
  4886. // Push the address of the object on the stack
  4887. asSExprContext e(engine);
  4888. if( onHeap )
  4889. e.bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  4890. PrepareFunctionCall(funcs[0], &e.bc, args);
  4891. MoveArgsToStack(funcs[0], &e.bc, args, false);
  4892. // If the object is allocated on the stack, then call the constructor as a normal function
  4893. if( onHeap )
  4894. {
  4895. int offset = 0;
  4896. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  4897. offset = descr->parameterTypes[0].GetSizeOnStackDWords();
  4898. e.bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  4899. }
  4900. else
  4901. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  4902. PerformFunctionCall(funcs[0], &e, onHeap, &args, tempObj.dataType.GetObjectType());
  4903. // Add tag that the object has been initialized
  4904. e.bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  4905. // The constructor doesn't return anything,
  4906. // so we have to manually inform the type of
  4907. // the return value
  4908. e.type = tempObj;
  4909. if( !onHeap )
  4910. e.type.dataType.MakeReference(false);
  4911. // Push the address of the object on the stack again
  4912. e.bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  4913. MergeExprBytecodeAndType(ctx, &e);
  4914. }
  4915. else
  4916. {
  4917. ctx->type.Set(asCDataType::CreateObject(to.GetObjectType(), false));
  4918. }
  4919. }
  4920. }
  4921. // If the base type is still different, and we are allowed to instance
  4922. // another object then we can try an implicit value cast
  4923. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() && allowObjectConstruct )
  4924. {
  4925. // Attempt implicit value cast
  4926. cost = ImplicitConvObjectValue(ctx, to, node, convType, generateCode);
  4927. }
  4928. // If we still haven't converted the base type to the correct type, then there is
  4929. // no need to continue as it is not possible to do the conversion
  4930. if( to.GetObjectType() != ctx->type.dataType.GetObjectType() )
  4931. return asCC_NO_CONV;
  4932. if( to.IsObjectHandle() )
  4933. {
  4934. // There is no extra cost in converting to a handle
  4935. // reference to handle -> handle
  4936. // reference -> handle
  4937. // object -> handle
  4938. // handle -> reference to handle
  4939. // reference -> reference to handle
  4940. // object -> reference to handle
  4941. // TODO: If the type is handle, then we can't use IsReadOnly to determine the constness of the basetype
  4942. // If the rvalue is a handle to a const object, then
  4943. // the lvalue must also be a handle to a const object
  4944. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() )
  4945. {
  4946. if( convType != asIC_IMPLICIT_CONV )
  4947. {
  4948. asASSERT(node);
  4949. asCString str;
  4950. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, ctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  4951. Error(str, node);
  4952. }
  4953. }
  4954. if( !ctx->type.dataType.IsObjectHandle() )
  4955. {
  4956. // An object type can be directly converted to a handle of the
  4957. // same type by doing a ref copy to a new variable
  4958. if( ctx->type.dataType.SupportHandles() )
  4959. {
  4960. asCDataType dt = ctx->type.dataType;
  4961. dt.MakeHandle(true);
  4962. dt.MakeReference(false);
  4963. if( generateCode )
  4964. {
  4965. // If the expression is already a local variable, then it is not
  4966. // necessary to do a ref copy, as the ref objects on the stack are
  4967. // really handles, only the handles cannot be modified.
  4968. if( ctx->type.isVariable )
  4969. {
  4970. bool isHandleToConst = ctx->type.dataType.IsReadOnly();
  4971. ctx->type.dataType.MakeReadOnly(false);
  4972. ctx->type.dataType.MakeHandle(true);
  4973. ctx->type.dataType.MakeReadOnly(true);
  4974. ctx->type.dataType.MakeHandleToConst(isHandleToConst);
  4975. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  4976. {
  4977. ctx->bc.Instr(asBC_PopPtr);
  4978. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  4979. ctx->type.dataType.MakeReference(true);
  4980. }
  4981. else if( ctx->type.dataType.IsReference() )
  4982. {
  4983. ctx->bc.Instr(asBC_RDSPtr);
  4984. ctx->type.dataType.MakeReference(false);
  4985. }
  4986. }
  4987. else
  4988. {
  4989. int offset = AllocateVariable(dt, true);
  4990. if( ctx->type.dataType.IsReference() )
  4991. ctx->bc.Instr(asBC_RDSPtr);
  4992. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  4993. ctx->bc.InstrPTR(asBC_REFCPY, dt.GetObjectType());
  4994. ctx->bc.Instr(asBC_PopPtr);
  4995. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  4996. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  4997. if( to.IsReference() )
  4998. dt.MakeReference(true);
  4999. else
  5000. ctx->bc.Instr(asBC_RDSPtr);
  5001. ctx->type.SetVariable(dt, offset, true);
  5002. }
  5003. }
  5004. else
  5005. ctx->type.dataType = dt;
  5006. // When this conversion is done the expression is no longer an lvalue
  5007. ctx->type.isLValue = false;
  5008. }
  5009. }
  5010. if( ctx->type.dataType.IsObjectHandle() )
  5011. {
  5012. // A handle to non-const can be converted to a
  5013. // handle to const, but not the other way
  5014. if( to.IsHandleToConst() )
  5015. ctx->type.dataType.MakeHandleToConst(true);
  5016. // A const handle can be converted to a non-const
  5017. // handle and vice versa as the handle is just a value
  5018. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5019. }
  5020. if( to.IsReference() && !ctx->type.dataType.IsReference() )
  5021. {
  5022. if( generateCode )
  5023. {
  5024. asASSERT( ctx->type.dataType.IsObjectHandle() );
  5025. // If the input type is a handle, then a simple ref copy is enough
  5026. bool isExplicitHandle = ctx->type.isExplicitHandle;
  5027. ctx->type.isExplicitHandle = ctx->type.dataType.IsObjectHandle();
  5028. // If the input type is read-only we'll need to temporarily
  5029. // remove this constness, otherwise the assignment will fail
  5030. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  5031. ctx->type.dataType.MakeReadOnly(false);
  5032. // If the object already is a temporary variable, then the copy
  5033. // doesn't have to be made as it is already a unique object
  5034. PrepareTemporaryObject(node, ctx);
  5035. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  5036. ctx->type.isExplicitHandle = isExplicitHandle;
  5037. }
  5038. // A non-reference can be converted to a reference,
  5039. // by putting the value in a temporary variable
  5040. ctx->type.dataType.MakeReference(true);
  5041. // Since it is a new temporary variable it doesn't have to be const
  5042. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5043. }
  5044. else if( !to.IsReference() && ctx->type.dataType.IsReference() )
  5045. {
  5046. Dereference(ctx, generateCode);
  5047. }
  5048. }
  5049. else // if( !to.IsObjectHandle() )
  5050. {
  5051. if( !to.IsReference() )
  5052. {
  5053. // reference to handle -> object
  5054. // handle -> object
  5055. // reference -> object
  5056. // An implicit handle can be converted to an object by adding a check for null pointer
  5057. if( ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  5058. {
  5059. if( generateCode )
  5060. {
  5061. if( ctx->type.dataType.IsReference() )
  5062. {
  5063. // The pointer on the stack refers to the handle
  5064. ctx->bc.Instr(asBC_ChkRefS);
  5065. }
  5066. else
  5067. {
  5068. // The pointer on the stack refers to the object
  5069. ctx->bc.Instr(asBC_CHKREF);
  5070. }
  5071. }
  5072. ctx->type.dataType.MakeHandle(false);
  5073. }
  5074. // A const object can be converted to a non-const object through a copy
  5075. if( ctx->type.dataType.IsReadOnly() && !to.IsReadOnly() &&
  5076. allowObjectConstruct )
  5077. {
  5078. // Does the object type allow a copy to be made?
  5079. if( ctx->type.dataType.CanBeCopied() )
  5080. {
  5081. if( generateCode )
  5082. {
  5083. // Make a temporary object with the copy
  5084. PrepareTemporaryObject(node, ctx);
  5085. }
  5086. // In case the object was already in a temporary variable, then the function
  5087. // didn't really do anything so we need to remove the constness here
  5088. ctx->type.dataType.MakeReadOnly(false);
  5089. // Add the cost for the copy
  5090. cost += asCC_TO_OBJECT_CONV;
  5091. }
  5092. }
  5093. if( ctx->type.dataType.IsReference() )
  5094. {
  5095. // This may look strange, but a value type allocated on the stack is already
  5096. // correct, so nothing should be done other than remove the mark as reference.
  5097. // For types allocated on the heap, it is necessary to dereference the pointer
  5098. // that is currently on the stack
  5099. if( IsVariableOnHeap(ctx->type.stackOffset) )
  5100. Dereference(ctx, generateCode);
  5101. else
  5102. ctx->type.dataType.MakeReference(false);
  5103. }
  5104. // A non-const object can be converted to a const object directly
  5105. if( !ctx->type.dataType.IsReadOnly() && to.IsReadOnly() )
  5106. {
  5107. ctx->type.dataType.MakeReadOnly(true);
  5108. }
  5109. }
  5110. else // if( to.IsReference() )
  5111. {
  5112. // reference to handle -> reference
  5113. // handle -> reference
  5114. // object -> reference
  5115. if( ctx->type.dataType.IsReference() )
  5116. {
  5117. if( ctx->type.isExplicitHandle && ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  5118. {
  5119. // ASHANDLE objects are really value types, so explicit handle can be removed
  5120. ctx->type.isExplicitHandle = false;
  5121. ctx->type.dataType.MakeHandle(false);
  5122. }
  5123. // A reference to a handle can be converted to a reference to an object
  5124. // by first reading the address, then verifying that it is not null
  5125. if( !to.IsObjectHandle() && ctx->type.dataType.IsObjectHandle() && !ctx->type.isExplicitHandle )
  5126. {
  5127. ctx->type.dataType.MakeHandle(false);
  5128. if( generateCode )
  5129. ctx->bc.Instr(asBC_ChkRefS);
  5130. }
  5131. // A reference to a non-const can be converted to a reference to a const
  5132. if( to.IsReadOnly() )
  5133. ctx->type.dataType.MakeReadOnly(true);
  5134. else if( ctx->type.dataType.IsReadOnly() )
  5135. {
  5136. // A reference to a const can be converted to a reference to a
  5137. // non-const by copying the object to a temporary variable
  5138. ctx->type.dataType.MakeReadOnly(false);
  5139. if( generateCode )
  5140. {
  5141. // If the object already is a temporary variable, then the copy
  5142. // doesn't have to be made as it is already a unique object
  5143. PrepareTemporaryObject(node, ctx);
  5144. }
  5145. // Add the cost for the copy
  5146. cost += asCC_TO_OBJECT_CONV;
  5147. }
  5148. }
  5149. else // if( !ctx->type.dataType.IsReference() )
  5150. {
  5151. // A non-reference handle can be converted to a non-handle reference by checking against null handle
  5152. if( ctx->type.dataType.IsObjectHandle() )
  5153. {
  5154. bool readOnly = false;
  5155. if( ctx->type.dataType.IsHandleToConst() )
  5156. readOnly = true;
  5157. if( generateCode )
  5158. {
  5159. if( ctx->type.isVariable )
  5160. ctx->bc.InstrSHORT(asBC_ChkNullV, ctx->type.stackOffset);
  5161. else
  5162. ctx->bc.Instr(asBC_CHKREF);
  5163. }
  5164. ctx->type.dataType.MakeHandle(false);
  5165. ctx->type.dataType.MakeReference(true);
  5166. // Make sure a handle to const isn't converted to non-const reference
  5167. if( readOnly )
  5168. ctx->type.dataType.MakeReadOnly(true);
  5169. }
  5170. else
  5171. {
  5172. // A value type allocated on the stack is differentiated
  5173. // by it not being a reference. But it can be handled as
  5174. // reference by pushing the pointer on the stack
  5175. if( (ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_VALUE) &&
  5176. (ctx->type.isVariable || ctx->type.isTemporary) &&
  5177. !IsVariableOnHeap(ctx->type.stackOffset) )
  5178. {
  5179. // Actually the pointer is already pushed on the stack in
  5180. // CompileVariableAccess, so we don't need to do anything else
  5181. }
  5182. else if( generateCode )
  5183. {
  5184. // A non-reference can be converted to a reference,
  5185. // by putting the value in a temporary variable
  5186. // If the input type is read-only we'll need to temporarily
  5187. // remove this constness, otherwise the assignment will fail
  5188. bool typeIsReadOnly = ctx->type.dataType.IsReadOnly();
  5189. ctx->type.dataType.MakeReadOnly(false);
  5190. // If the object already is a temporary variable, then the copy
  5191. // doesn't have to be made as it is already a unique object
  5192. PrepareTemporaryObject(node, ctx);
  5193. ctx->type.dataType.MakeReadOnly(typeIsReadOnly);
  5194. // Add the cost for the copy
  5195. cost += asCC_TO_OBJECT_CONV;
  5196. }
  5197. // This may look strange as the conversion was to make the expression a reference
  5198. // but a value type allocated on the stack is a reference even without the type
  5199. // being marked as such.
  5200. ctx->type.dataType.MakeReference(IsVariableOnHeap(ctx->type.stackOffset));
  5201. }
  5202. // TODO: If the variable is an object allocated on the stack the following is not true as the copy may not have been made
  5203. // Since it is a new temporary variable it doesn't have to be const
  5204. ctx->type.dataType.MakeReadOnly(to.IsReadOnly());
  5205. }
  5206. }
  5207. }
  5208. return cost;
  5209. }
  5210. asUINT asCCompiler::ImplicitConvPrimitiveToObject(asSExprContext *ctx, const asCDataType &to, asCScriptNode * /*node*/, EImplicitConv /*isExplicit*/, bool generateCode, bool /*allowObjectConstruct*/)
  5211. {
  5212. // Reference types currently don't allow implicit conversion from primitive to object
  5213. // TODO: Allow implicit conversion to scoped reference types as they are supposed to appear like ordinary value types
  5214. asCObjectType *objType = to.GetObjectType();
  5215. asASSERT( objType );
  5216. if( !objType || (objType->flags & asOBJ_REF) )
  5217. return asCC_NO_CONV;
  5218. // For value types the object must have a constructor that takes a single primitive argument either by value or as input reference
  5219. asCArray<int> funcs;
  5220. for( asUINT n = 0; n < objType->beh.constructors.GetLength(); n++ )
  5221. {
  5222. asCScriptFunction *func = engine->scriptFunctions[objType->beh.constructors[n]];
  5223. if( func->parameterTypes.GetLength() == 1 &&
  5224. func->parameterTypes[0].IsPrimitive() &&
  5225. !(func->inOutFlags[0] & asTM_OUTREF) )
  5226. {
  5227. funcs.PushLast(func->id);
  5228. }
  5229. }
  5230. if( funcs.GetLength() == 0 )
  5231. return asCC_NO_CONV;
  5232. // Check if it is possible to choose a best match
  5233. asSExprContext arg(engine);
  5234. arg.type = ctx->type;
  5235. arg.exprNode = ctx->exprNode; // Use the same node for compiler messages
  5236. asCArray<asSExprContext*> args;
  5237. args.PushLast(&arg);
  5238. asUINT cost = asCC_TO_OBJECT_CONV + MatchFunctions(funcs, args, 0, 0, objType, false, true, false);
  5239. if( funcs.GetLength() != 1 )
  5240. return asCC_NO_CONV;
  5241. if( !generateCode )
  5242. {
  5243. ctx->type.Set(to);
  5244. return cost;
  5245. }
  5246. // TODO: clean up: This part is similar to CompileConstructCall(). It should be put in a common function
  5247. bool onHeap = true;
  5248. // Value types and script types are allocated through the constructor
  5249. asCTypeInfo tempObj;
  5250. tempObj.dataType = to;
  5251. tempObj.stackOffset = (short)AllocateVariable(to, true);
  5252. tempObj.dataType.MakeReference(true);
  5253. tempObj.isTemporary = true;
  5254. tempObj.isVariable = true;
  5255. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  5256. // Push the address of the object on the stack
  5257. if( onHeap )
  5258. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  5259. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  5260. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  5261. if( !(objType->flags & asOBJ_REF) )
  5262. {
  5263. // If the object is allocated on the stack, then call the constructor as a normal function
  5264. if( onHeap )
  5265. {
  5266. int offset = 0;
  5267. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  5268. for( asUINT n = 0; n < args.GetLength(); n++ )
  5269. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  5270. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  5271. }
  5272. else
  5273. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  5274. PerformFunctionCall(funcs[0], ctx, onHeap, &args, tempObj.dataType.GetObjectType());
  5275. // Add tag that the object has been initialized
  5276. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  5277. // The constructor doesn't return anything,
  5278. // so we have to manually inform the type of
  5279. // the return value
  5280. ctx->type = tempObj;
  5281. if( !onHeap )
  5282. ctx->type.dataType.MakeReference(false);
  5283. // Push the address of the object on the stack again
  5284. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  5285. }
  5286. else
  5287. {
  5288. asASSERT( objType->flags & asOBJ_SCOPED );
  5289. // Call the factory to create the reference type
  5290. PerformFunctionCall(funcs[0], ctx, false, &args);
  5291. }
  5292. return cost;
  5293. }
  5294. void asCCompiler::ImplicitConversionConstant(asSExprContext *from, const asCDataType &to, asCScriptNode *node, EImplicitConv convType)
  5295. {
  5296. asASSERT(from->type.isConstant);
  5297. // TODO: node should be the node of the value that is
  5298. // converted (not the operator that provokes the implicit
  5299. // conversion)
  5300. // If the base type is correct there is no more to do
  5301. if( to.IsEqualExceptRefAndConst(from->type.dataType) ) return;
  5302. // References cannot be constants
  5303. if( from->type.dataType.IsReference() ) return;
  5304. if( (to.IsIntegerType() && to.GetSizeInMemoryDWords() == 1 && !to.IsEnumType()) ||
  5305. (to.IsEnumType() && convType == asIC_EXPLICIT_VAL_CAST) )
  5306. {
  5307. if( from->type.dataType.IsFloatType() ||
  5308. from->type.dataType.IsDoubleType() ||
  5309. from->type.dataType.IsUnsignedType() ||
  5310. from->type.dataType.IsIntegerType() )
  5311. {
  5312. // Transform the value
  5313. // Float constants can be implicitly converted to int
  5314. if( from->type.dataType.IsFloatType() )
  5315. {
  5316. float fc = from->type.floatValue;
  5317. int ic = int(fc);
  5318. if( float(ic) != fc )
  5319. {
  5320. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5321. }
  5322. from->type.intValue = ic;
  5323. }
  5324. // Double constants can be implicitly converted to int
  5325. else if( from->type.dataType.IsDoubleType() )
  5326. {
  5327. double fc = from->type.doubleValue;
  5328. int ic = int(fc);
  5329. if( double(ic) != fc )
  5330. {
  5331. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5332. }
  5333. from->type.intValue = ic;
  5334. }
  5335. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  5336. {
  5337. // Verify that it is possible to convert to signed without getting negative
  5338. if( from->type.intValue < 0 )
  5339. {
  5340. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  5341. }
  5342. // Convert to 32bit
  5343. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5344. from->type.intValue = from->type.byteValue;
  5345. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5346. from->type.intValue = from->type.wordValue;
  5347. }
  5348. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  5349. {
  5350. // Convert to 32bit
  5351. from->type.intValue = int(from->type.qwordValue);
  5352. }
  5353. else if( from->type.dataType.IsIntegerType() &&
  5354. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  5355. {
  5356. // Convert to 32bit
  5357. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5358. from->type.intValue = (signed char)from->type.byteValue;
  5359. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5360. from->type.intValue = (short)from->type.wordValue;
  5361. }
  5362. // Set the resulting type
  5363. if( to.IsEnumType() )
  5364. from->type.dataType = to;
  5365. else
  5366. from->type.dataType = asCDataType::CreatePrimitive(ttInt, true);
  5367. }
  5368. // Check if a downsize is necessary
  5369. if( to.IsIntegerType() &&
  5370. from->type.dataType.IsIntegerType() &&
  5371. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  5372. {
  5373. // Verify if it is possible
  5374. if( to.GetSizeInMemoryBytes() == 1 )
  5375. {
  5376. if( char(from->type.intValue) != from->type.intValue )
  5377. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  5378. from->type.byteValue = char(from->type.intValue);
  5379. }
  5380. else if( to.GetSizeInMemoryBytes() == 2 )
  5381. {
  5382. if( short(from->type.intValue) != from->type.intValue )
  5383. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  5384. from->type.wordValue = short(from->type.intValue);
  5385. }
  5386. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5387. }
  5388. }
  5389. else if( to.IsIntegerType() && to.GetSizeInMemoryDWords() == 2 )
  5390. {
  5391. // Float constants can be implicitly converted to int
  5392. if( from->type.dataType.IsFloatType() )
  5393. {
  5394. float fc = from->type.floatValue;
  5395. asINT64 ic = asINT64(fc);
  5396. if( float(ic) != fc )
  5397. {
  5398. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5399. }
  5400. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  5401. from->type.qwordValue = ic;
  5402. }
  5403. // Double constants can be implicitly converted to int
  5404. else if( from->type.dataType.IsDoubleType() )
  5405. {
  5406. double fc = from->type.doubleValue;
  5407. asINT64 ic = asINT64(fc);
  5408. if( double(ic) != fc )
  5409. {
  5410. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5411. }
  5412. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  5413. from->type.qwordValue = ic;
  5414. }
  5415. else if( from->type.dataType.IsUnsignedType() )
  5416. {
  5417. // Convert to 64bit
  5418. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5419. from->type.qwordValue = from->type.byteValue;
  5420. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5421. from->type.qwordValue = from->type.wordValue;
  5422. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  5423. from->type.qwordValue = from->type.dwordValue;
  5424. else if( from->type.dataType.GetSizeInMemoryBytes() == 8 )
  5425. {
  5426. if( asINT64(from->type.qwordValue) < 0 )
  5427. {
  5428. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  5429. }
  5430. }
  5431. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  5432. }
  5433. else if( from->type.dataType.IsIntegerType() )
  5434. {
  5435. // Convert to 64bit
  5436. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5437. from->type.qwordValue = (signed char)from->type.byteValue;
  5438. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5439. from->type.qwordValue = (short)from->type.wordValue;
  5440. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  5441. from->type.qwordValue = from->type.intValue;
  5442. from->type.dataType = asCDataType::CreatePrimitive(ttInt64, true);
  5443. }
  5444. }
  5445. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 1 )
  5446. {
  5447. if( from->type.dataType.IsFloatType() )
  5448. {
  5449. float fc = from->type.floatValue;
  5450. // Some compilers set the value to 0 when converting a negative float to unsigned int.
  5451. // To maintain a consistent behaviour across compilers we convert to int first.
  5452. asUINT uic = asUINT(int(fc));
  5453. if( float(uic) != fc )
  5454. {
  5455. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5456. }
  5457. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  5458. from->type.intValue = uic;
  5459. // Try once more, in case of a smaller type
  5460. ImplicitConversionConstant(from, to, node, convType);
  5461. }
  5462. else if( from->type.dataType.IsDoubleType() )
  5463. {
  5464. double fc = from->type.doubleValue;
  5465. // Some compilers set the value to 0 when converting a negative double to unsigned int.
  5466. // To maintain a consistent behaviour across compilers we convert to int first.
  5467. asUINT uic = asUINT(int(fc));
  5468. if( double(uic) != fc )
  5469. {
  5470. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5471. }
  5472. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  5473. from->type.intValue = uic;
  5474. // Try once more, in case of a smaller type
  5475. ImplicitConversionConstant(from, to, node, convType);
  5476. }
  5477. else if( from->type.dataType.IsIntegerType() )
  5478. {
  5479. // Verify that it is possible to convert to unsigned without loosing negative
  5480. if( (from->type.dataType.GetSizeInMemoryBytes() > 4 && asINT64(from->type.qwordValue) < 0) ||
  5481. (from->type.dataType.GetSizeInMemoryBytes() <= 4 && from->type.intValue < 0) )
  5482. {
  5483. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  5484. }
  5485. // Check if any data is lost
  5486. if( from->type.dataType.GetSizeInMemoryBytes() > 4 && (from->type.qwordValue >> 32) != 0 && (from->type.qwordValue >> 32) != 0xFFFFFFFF )
  5487. {
  5488. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  5489. }
  5490. // Convert to 32bit
  5491. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5492. from->type.intValue = (signed char)from->type.byteValue;
  5493. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5494. from->type.intValue = (short)from->type.wordValue;
  5495. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  5496. // Try once more, in case of a smaller type
  5497. ImplicitConversionConstant(from, to, node, convType);
  5498. }
  5499. else if( from->type.dataType.IsUnsignedType() &&
  5500. from->type.dataType.GetSizeInMemoryBytes() < 4 )
  5501. {
  5502. // Convert to 32bit
  5503. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5504. from->type.dwordValue = from->type.byteValue;
  5505. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5506. from->type.dwordValue = from->type.wordValue;
  5507. from->type.dataType = asCDataType::CreatePrimitive(ttUInt, true);
  5508. // Try once more, in case of a smaller type
  5509. ImplicitConversionConstant(from, to, node, convType);
  5510. }
  5511. else if( from->type.dataType.IsUnsignedType() &&
  5512. from->type.dataType.GetSizeInMemoryBytes() > to.GetSizeInMemoryBytes() )
  5513. {
  5514. // Verify if it is possible
  5515. if( to.GetSizeInMemoryBytes() == 1 )
  5516. {
  5517. if( asBYTE(from->type.dwordValue) != from->type.dwordValue )
  5518. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  5519. from->type.byteValue = asBYTE(from->type.dwordValue);
  5520. }
  5521. else if( to.GetSizeInMemoryBytes() == 2 )
  5522. {
  5523. if( asWORD(from->type.dwordValue) != from->type.dwordValue )
  5524. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_VALUE_TOO_LARGE_FOR_TYPE, node);
  5525. from->type.wordValue = asWORD(from->type.dwordValue);
  5526. }
  5527. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5528. }
  5529. }
  5530. else if( to.IsUnsignedType() && to.GetSizeInMemoryDWords() == 2 )
  5531. {
  5532. if( from->type.dataType.IsFloatType() )
  5533. {
  5534. float fc = from->type.floatValue;
  5535. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  5536. asQWORD uic = asQWORD(asINT64(fc));
  5537. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  5538. // MSVC6 doesn't support this conversion
  5539. if( float(uic) != fc )
  5540. {
  5541. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5542. }
  5543. #endif
  5544. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  5545. from->type.qwordValue = uic;
  5546. }
  5547. else if( from->type.dataType.IsDoubleType() )
  5548. {
  5549. double fc = from->type.doubleValue;
  5550. // Convert first to int64 then to uint64 to avoid negative float becoming 0 on gnuc base compilers
  5551. asQWORD uic = asQWORD(asINT64(fc));
  5552. #if !defined(_MSC_VER) || _MSC_VER > 1200 // MSVC++ 6
  5553. // MSVC6 doesn't support this conversion
  5554. if( double(uic) != fc )
  5555. {
  5556. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5557. }
  5558. #endif
  5559. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  5560. from->type.qwordValue = uic;
  5561. }
  5562. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  5563. {
  5564. // Convert to 64bit
  5565. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5566. from->type.qwordValue = (asINT64)(signed char)from->type.byteValue;
  5567. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5568. from->type.qwordValue = (asINT64)(short)from->type.wordValue;
  5569. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  5570. from->type.qwordValue = (asINT64)from->type.intValue;
  5571. // Verify that it is possible to convert to unsigned without loosing negative
  5572. if( asINT64(from->type.qwordValue) < 0 )
  5573. {
  5574. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  5575. }
  5576. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  5577. }
  5578. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  5579. {
  5580. // Verify that it is possible to convert to unsigned without loosing negative
  5581. if( asINT64(from->type.qwordValue) < 0 )
  5582. {
  5583. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_CHANGE_SIGN, node);
  5584. }
  5585. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  5586. }
  5587. else if( from->type.dataType.IsUnsignedType() )
  5588. {
  5589. // Convert to 64bit
  5590. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5591. from->type.qwordValue = from->type.byteValue;
  5592. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5593. from->type.qwordValue = from->type.wordValue;
  5594. else if( from->type.dataType.GetSizeInMemoryBytes() == 4 )
  5595. from->type.qwordValue = from->type.dwordValue;
  5596. from->type.dataType = asCDataType::CreatePrimitive(ttUInt64, true);
  5597. }
  5598. }
  5599. else if( to.IsFloatType() )
  5600. {
  5601. if( from->type.dataType.IsDoubleType() )
  5602. {
  5603. double ic = from->type.doubleValue;
  5604. float fc = float(ic);
  5605. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5606. from->type.floatValue = fc;
  5607. }
  5608. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  5609. {
  5610. // Must properly convert value in case the from value is smaller
  5611. int ic;
  5612. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5613. ic = (signed char)from->type.byteValue;
  5614. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5615. ic = (short)from->type.wordValue;
  5616. else
  5617. ic = from->type.intValue;
  5618. float fc = float(ic);
  5619. if( int(fc) != ic )
  5620. {
  5621. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5622. }
  5623. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5624. from->type.floatValue = fc;
  5625. }
  5626. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  5627. {
  5628. float fc = float(asINT64(from->type.qwordValue));
  5629. if( asINT64(fc) != asINT64(from->type.qwordValue) )
  5630. {
  5631. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5632. }
  5633. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5634. from->type.floatValue = fc;
  5635. }
  5636. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  5637. {
  5638. // Must properly convert value in case the from value is smaller
  5639. unsigned int uic;
  5640. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5641. uic = from->type.byteValue;
  5642. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5643. uic = from->type.wordValue;
  5644. else
  5645. uic = from->type.dwordValue;
  5646. float fc = float(uic);
  5647. if( (unsigned int)(fc) != uic )
  5648. {
  5649. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5650. }
  5651. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5652. from->type.floatValue = fc;
  5653. }
  5654. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  5655. {
  5656. float fc = float((asINT64)from->type.qwordValue);
  5657. if( asQWORD(fc) != from->type.qwordValue )
  5658. {
  5659. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5660. }
  5661. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5662. from->type.floatValue = fc;
  5663. }
  5664. }
  5665. else if( to.IsDoubleType() )
  5666. {
  5667. if( from->type.dataType.IsFloatType() )
  5668. {
  5669. float ic = from->type.floatValue;
  5670. double fc = double(ic);
  5671. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5672. from->type.doubleValue = fc;
  5673. }
  5674. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  5675. {
  5676. // Must properly convert value in case the from value is smaller
  5677. int ic;
  5678. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5679. ic = (signed char)from->type.byteValue;
  5680. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5681. ic = (short)from->type.wordValue;
  5682. else
  5683. ic = from->type.intValue;
  5684. double fc = double(ic);
  5685. if( int(fc) != ic )
  5686. {
  5687. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5688. }
  5689. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5690. from->type.doubleValue = fc;
  5691. }
  5692. else if( from->type.dataType.IsIntegerType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  5693. {
  5694. double fc = double(asINT64(from->type.qwordValue));
  5695. if( asINT64(fc) != asINT64(from->type.qwordValue) )
  5696. {
  5697. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5698. }
  5699. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5700. from->type.doubleValue = fc;
  5701. }
  5702. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 1 )
  5703. {
  5704. // Must properly convert value in case the from value is smaller
  5705. unsigned int uic;
  5706. if( from->type.dataType.GetSizeInMemoryBytes() == 1 )
  5707. uic = from->type.byteValue;
  5708. else if( from->type.dataType.GetSizeInMemoryBytes() == 2 )
  5709. uic = from->type.wordValue;
  5710. else
  5711. uic = from->type.dwordValue;
  5712. double fc = double(uic);
  5713. if( (unsigned int)(fc) != uic )
  5714. {
  5715. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5716. }
  5717. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5718. from->type.doubleValue = fc;
  5719. }
  5720. else if( from->type.dataType.IsUnsignedType() && from->type.dataType.GetSizeInMemoryDWords() == 2 )
  5721. {
  5722. double fc = double((asINT64)from->type.qwordValue);
  5723. if( asQWORD(fc) != from->type.qwordValue )
  5724. {
  5725. if( convType != asIC_EXPLICIT_VAL_CAST && node ) Warning(TXT_NOT_EXACT, node);
  5726. }
  5727. from->type.dataType = asCDataType::CreatePrimitive(to.GetTokenType(), true);
  5728. from->type.doubleValue = fc;
  5729. }
  5730. }
  5731. }
  5732. int asCCompiler::DoAssignment(asSExprContext *ctx, asSExprContext *lctx, asSExprContext *rctx, asCScriptNode *lexpr, asCScriptNode *rexpr, int op, asCScriptNode *opNode)
  5733. {
  5734. // Don't allow any operators on expressions that take address of class method
  5735. // If methodName is set but the type is not an object, then it is a global function
  5736. if( lctx->methodName != "" || rctx->IsClassMethod() )
  5737. {
  5738. Error(TXT_INVALID_OP_ON_METHOD, opNode);
  5739. return -1;
  5740. }
  5741. // Implicit handle types should always be treated as handles in assignments
  5742. if (lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE) )
  5743. {
  5744. lctx->type.dataType.MakeHandle(true);
  5745. lctx->type.isExplicitHandle = true;
  5746. }
  5747. // Urho3D: if there is a handle type, and it does not have an overloaded assignment operator, convert to an explicit handle
  5748. // for scripting convenience. (For the Urho3D handle types, value assignment is not supported)
  5749. if (lctx->type.dataType.IsObjectHandle() && !lctx->type.dataType.IsTemplate() && !lctx->type.isExplicitHandle &&
  5750. !lctx->type.dataType.GetBehaviour()->copy)
  5751. lctx->type.isExplicitHandle = true;
  5752. // If the left hand expression is a property accessor, then that should be used
  5753. // to do the assignment instead of the ordinary operator. The exception is when
  5754. // the property accessor is for a handle property, and the operation is a value
  5755. // assignment.
  5756. if( (lctx->property_get || lctx->property_set) &&
  5757. !(lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle) )
  5758. {
  5759. if( op != ttAssignment )
  5760. {
  5761. // TODO: getset: We may actually be able to support this, if we can
  5762. // guarantee that the object reference will stay valid
  5763. // between the calls to the get and set accessors.
  5764. // Process the property to free the memory
  5765. ProcessPropertySetAccessor(lctx, rctx, opNode);
  5766. // Compound assignments are not allowed for properties
  5767. Error(TXT_COMPOUND_ASGN_WITH_PROP, opNode);
  5768. return -1;
  5769. }
  5770. // It is not allowed to do a handle assignment on a property
  5771. // accessor that doesn't take a handle in the set accessor.
  5772. if( lctx->property_set && lctx->type.isExplicitHandle )
  5773. {
  5774. // set_opIndex has 2 arguments, where as normal setters have only 1
  5775. asCArray<asCDataType>& parameterTypes =
  5776. builder->GetFunctionDescription(lctx->property_set)->parameterTypes;
  5777. if( !parameterTypes[parameterTypes.GetLength() - 1].IsObjectHandle() )
  5778. {
  5779. // Process the property to free the memory
  5780. ProcessPropertySetAccessor(lctx, rctx, opNode);
  5781. Error(TXT_HANDLE_ASSIGN_ON_NON_HANDLE_PROP, opNode);
  5782. return -1;
  5783. }
  5784. }
  5785. MergeExprBytecodeAndType(ctx, lctx);
  5786. return ProcessPropertySetAccessor(ctx, rctx, opNode);
  5787. }
  5788. else if( lctx->property_get && lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  5789. {
  5790. // Get the handle to the object that will be used for the value assignment
  5791. ProcessPropertyGetAccessor(lctx, opNode);
  5792. }
  5793. if( lctx->type.dataType.IsPrimitive() )
  5794. {
  5795. if( !lctx->type.isLValue )
  5796. {
  5797. Error(TXT_NOT_LVALUE, lexpr);
  5798. return -1;
  5799. }
  5800. if( op != ttAssignment )
  5801. {
  5802. // Compute the operator before the assignment
  5803. asCTypeInfo lvalue = lctx->type;
  5804. if( lctx->type.isTemporary && !lctx->type.isVariable )
  5805. {
  5806. // The temporary variable must not be freed until the
  5807. // assignment has been performed. lvalue still holds
  5808. // the information about the temporary variable
  5809. lctx->type.isTemporary = false;
  5810. }
  5811. asSExprContext o(engine);
  5812. CompileOperator(opNode, lctx, rctx, &o);
  5813. MergeExprBytecode(rctx, &o);
  5814. rctx->type = o.type;
  5815. // Convert the rvalue to the right type and validate it
  5816. PrepareForAssignment(&lvalue.dataType, rctx, rexpr, false);
  5817. MergeExprBytecode(ctx, rctx);
  5818. lctx->type = lvalue;
  5819. // The lvalue continues the same, either it was a variable, or a reference in the register
  5820. }
  5821. else
  5822. {
  5823. // Convert the rvalue to the right type and validate it
  5824. PrepareForAssignment(&lctx->type.dataType, rctx, rexpr, false, lctx);
  5825. MergeExprBytecode(ctx, rctx);
  5826. MergeExprBytecode(ctx, lctx);
  5827. }
  5828. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  5829. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  5830. ctx->type = lctx->type;
  5831. }
  5832. else if( lctx->type.isExplicitHandle )
  5833. {
  5834. if( !lctx->type.isLValue )
  5835. {
  5836. Error(TXT_NOT_LVALUE, lexpr);
  5837. return -1;
  5838. }
  5839. // Object handles don't have any compound assignment operators
  5840. if( op != ttAssignment )
  5841. {
  5842. asCString str;
  5843. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  5844. Error(str, lexpr);
  5845. return -1;
  5846. }
  5847. if( lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  5848. {
  5849. // The object is a value type but that should be treated as a handle
  5850. // Make sure the right hand value is a handle
  5851. if( !rctx->type.isExplicitHandle &&
  5852. !(rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  5853. {
  5854. // Function names can be considered handles already
  5855. if( rctx->methodName == "" )
  5856. {
  5857. asCDataType dt = rctx->type.dataType;
  5858. dt.MakeHandle(true);
  5859. dt.MakeReference(false);
  5860. PrepareArgument(&dt, rctx, rexpr, true, asTM_INREF);
  5861. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5862. {
  5863. asCString str;
  5864. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  5865. Error(str, rexpr);
  5866. return -1;
  5867. }
  5868. }
  5869. }
  5870. if( CompileOverloadedDualOperator(opNode, lctx, rctx, ctx) )
  5871. {
  5872. // An overloaded assignment operator was found (or a compilation error occured)
  5873. return 0;
  5874. }
  5875. // The object must implement the opAssign method
  5876. Error(TXT_NO_APPROPRIATE_OPASSIGN, opNode);
  5877. return -1;
  5878. }
  5879. else
  5880. {
  5881. asCDataType dt = lctx->type.dataType;
  5882. dt.MakeReference(false);
  5883. PrepareArgument(&dt, rctx, rexpr, true, asTM_INREF , true);
  5884. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5885. {
  5886. asCString str;
  5887. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  5888. Error(str, rexpr);
  5889. return -1;
  5890. }
  5891. MergeExprBytecode(ctx, rctx);
  5892. MergeExprBytecode(ctx, lctx);
  5893. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  5894. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  5895. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  5896. ctx->type = lctx->type;
  5897. // After the handle assignment the original handle is left on the stack
  5898. ctx->type.dataType.MakeReference(false);
  5899. }
  5900. }
  5901. else // if( lctx->type.dataType.IsObject() )
  5902. {
  5903. // An ASHANDLE type must not allow a value assignment, as
  5904. // the opAssign operator is used for the handle assignment
  5905. if( lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  5906. {
  5907. asCString str;
  5908. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  5909. Error(str, lexpr);
  5910. return -1;
  5911. }
  5912. // The lvalue reference may be marked as a temporary, if for example
  5913. // it was originated as a handle returned from a function. In such
  5914. // cases it must be possible to assign values to it anyway.
  5915. if( lctx->type.dataType.IsObjectHandle() && !lctx->type.isExplicitHandle )
  5916. {
  5917. // Convert the handle to a object reference
  5918. asCDataType to;
  5919. to = lctx->type.dataType;
  5920. to.MakeHandle(false);
  5921. ImplicitConversion(lctx, to, lexpr, asIC_IMPLICIT_CONV);
  5922. lctx->type.isLValue = true; // Handle may not have been an lvalue, but the dereferenced object is
  5923. }
  5924. // Check for overloaded assignment operator
  5925. if( CompileOverloadedDualOperator(opNode, lctx, rctx, ctx) )
  5926. {
  5927. // An overloaded assignment operator was found (or a compilation error occured)
  5928. return 0;
  5929. }
  5930. // No registered operator was found. In case the operation is a direct
  5931. // assignment and the rvalue is the same type as the lvalue, then we can
  5932. // still use the byte-for-byte copy to do the assignment
  5933. if( op != ttAssignment )
  5934. {
  5935. asCString str;
  5936. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  5937. Error(str, lexpr);
  5938. return -1;
  5939. }
  5940. // If the left hand expression is simple, i.e. without any
  5941. // function calls or allocations of memory, then we can avoid
  5942. // doing a copy of the right hand expression (done by PrepareArgument).
  5943. // Instead the reference to the value can be placed directly on the
  5944. // stack.
  5945. //
  5946. // This optimization should only be done for value types, where
  5947. // the application developer is responsible for making the
  5948. // implementation safe against unwanted destruction of the input
  5949. // reference before the time.
  5950. bool simpleExpr = (lctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_VALUE) && lctx->bc.IsSimpleExpression();
  5951. // Implicitly convert the rvalue to the type of the lvalue
  5952. bool needConversion = false;
  5953. if( !lctx->type.dataType.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5954. needConversion = true;
  5955. if( !simpleExpr || needConversion )
  5956. {
  5957. asCDataType dt = lctx->type.dataType;
  5958. dt.MakeReference(true);
  5959. dt.MakeReadOnly(true);
  5960. PrepareArgument(&dt, rctx, rexpr, true, 1, !needConversion);
  5961. if( !dt.IsEqualExceptRefAndConst(rctx->type.dataType) )
  5962. {
  5963. asCString str;
  5964. str.Format(TXT_CANT_IMPLICITLY_CONVERT_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  5965. Error(str, rexpr);
  5966. return -1;
  5967. }
  5968. }
  5969. else
  5970. {
  5971. // Process any property accessor first, before placing the final reference on the stack
  5972. ProcessPropertyGetAccessor(rctx, rexpr);
  5973. if( rctx->type.dataType.IsReference() && (!(rctx->type.isVariable || rctx->type.isTemporary) || IsVariableOnHeap(rctx->type.stackOffset)) )
  5974. rctx->bc.Instr(asBC_RDSPtr);
  5975. }
  5976. MergeExprBytecode(ctx, rctx);
  5977. MergeExprBytecode(ctx, lctx);
  5978. if( !simpleExpr || needConversion )
  5979. {
  5980. if( (rctx->type.isVariable || rctx->type.isTemporary) )
  5981. {
  5982. if( !IsVariableOnHeap(rctx->type.stackOffset) )
  5983. // TODO: runtime optimize: Actually the reference can be pushed on the stack directly
  5984. // as the value allocated on the stack is guaranteed to be safe.
  5985. // The bytecode optimizer should be able to determine this and optimize away the VAR + GETREF
  5986. ctx->bc.InstrWORD(asBC_GETREF, AS_PTR_SIZE);
  5987. else
  5988. ctx->bc.InstrWORD(asBC_GETOBJREF, AS_PTR_SIZE);
  5989. }
  5990. }
  5991. PerformAssignment(&lctx->type, &rctx->type, &ctx->bc, opNode);
  5992. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  5993. ctx->type = lctx->type;
  5994. }
  5995. return 0;
  5996. }
  5997. int asCCompiler::CompileAssignment(asCScriptNode *expr, asSExprContext *ctx)
  5998. {
  5999. asCScriptNode *lexpr = expr->firstChild;
  6000. if( lexpr->next )
  6001. {
  6002. // Compile the two expression terms
  6003. asSExprContext lctx(engine), rctx(engine);
  6004. int rr = CompileAssignment(lexpr->next->next, &rctx);
  6005. int lr = CompileCondition(lexpr, &lctx);
  6006. if( lr >= 0 && rr >= 0 )
  6007. return DoAssignment(ctx, &lctx, &rctx, lexpr, lexpr->next->next, lexpr->next->tokenType, lexpr->next);
  6008. // Since the operands failed, the assignment was not computed
  6009. ctx->type.SetDummy();
  6010. return -1;
  6011. }
  6012. return CompileCondition(lexpr, ctx);
  6013. }
  6014. int asCCompiler::CompileCondition(asCScriptNode *expr, asSExprContext *ctx)
  6015. {
  6016. asCTypeInfo ctype;
  6017. // Compile the conditional expression
  6018. asCScriptNode *cexpr = expr->firstChild;
  6019. if( cexpr->next )
  6020. {
  6021. //-------------------------------
  6022. // Compile the condition
  6023. asSExprContext e(engine);
  6024. int r = CompileExpression(cexpr, &e);
  6025. if( r < 0 )
  6026. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  6027. if( r >= 0 && !e.type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  6028. {
  6029. Error(TXT_EXPR_MUST_BE_BOOL, cexpr);
  6030. e.type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  6031. }
  6032. ctype = e.type;
  6033. ProcessPropertyGetAccessor(&e, cexpr);
  6034. if( e.type.dataType.IsReference() ) ConvertToVariable(&e);
  6035. ProcessDeferredParams(&e);
  6036. //-------------------------------
  6037. // Compile the left expression
  6038. asSExprContext le(engine);
  6039. int lr = CompileAssignment(cexpr->next, &le);
  6040. //-------------------------------
  6041. // Compile the right expression
  6042. asSExprContext re(engine);
  6043. int rr = CompileAssignment(cexpr->next->next, &re);
  6044. if( lr >= 0 && rr >= 0 )
  6045. {
  6046. // Don't allow any operators on expressions that take address of class method
  6047. if( le.IsClassMethod() || re.IsClassMethod() )
  6048. {
  6049. Error(TXT_INVALID_OP_ON_METHOD, expr);
  6050. return -1;
  6051. }
  6052. ProcessPropertyGetAccessor(&le, cexpr->next);
  6053. ProcessPropertyGetAccessor(&re, cexpr->next->next);
  6054. bool isExplicitHandle = le.type.isExplicitHandle || re.type.isExplicitHandle;
  6055. // Allow a 0 or null in the first case to be implicitly converted to the second type
  6056. if( le.type.isConstant && le.type.intValue == 0 && le.type.dataType.IsIntegerType() )
  6057. {
  6058. asCDataType to = re.type.dataType;
  6059. to.MakeReference(false);
  6060. to.MakeReadOnly(true);
  6061. ImplicitConversionConstant(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  6062. }
  6063. else if( le.type.IsNullConstant() )
  6064. {
  6065. asCDataType to = re.type.dataType;
  6066. to.MakeHandle(true);
  6067. ImplicitConversion(&le, to, cexpr->next, asIC_IMPLICIT_CONV);
  6068. }
  6069. // Allow either case to be converted to const @ if the other is const @
  6070. if( (le.type.dataType.IsHandleToConst() && !le.type.IsNullConstant()) || (re.type.dataType.IsHandleToConst() && !re.type.dataType.IsNullHandle()) )
  6071. {
  6072. le.type.dataType.MakeHandleToConst(true);
  6073. re.type.dataType.MakeHandleToConst(true);
  6074. }
  6075. //---------------------------------
  6076. // Output the byte code
  6077. int afterLabel = nextLabel++;
  6078. int elseLabel = nextLabel++;
  6079. // If left expression is void, then we don't need to store the result
  6080. if( le.type.dataType.IsEqualExceptConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  6081. {
  6082. // Put the code for the condition expression on the output
  6083. MergeExprBytecode(ctx, &e);
  6084. // Added the branch decision
  6085. ctx->type = e.type;
  6086. ConvertToVariable(ctx);
  6087. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  6088. ctx->bc.Instr(asBC_ClrHi);
  6089. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  6090. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  6091. // Add the left expression
  6092. MergeExprBytecode(ctx, &le);
  6093. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  6094. // Add the right expression
  6095. ctx->bc.Label((short)elseLabel);
  6096. MergeExprBytecode(ctx, &re);
  6097. ctx->bc.Label((short)afterLabel);
  6098. // Make sure both expressions have the same type
  6099. if( le.type.dataType != re.type.dataType )
  6100. Error(TXT_BOTH_MUST_BE_SAME, expr);
  6101. // Set the type of the result
  6102. ctx->type = le.type;
  6103. }
  6104. else
  6105. {
  6106. // Allocate temporary variable and copy the result to that one
  6107. asCTypeInfo temp;
  6108. temp = le.type;
  6109. temp.dataType.MakeReference(false);
  6110. temp.dataType.MakeReadOnly(false);
  6111. // Make sure the variable isn't used in any of the expressions,
  6112. // as it would be overwritten which may cause crashes or less visible bugs
  6113. int l = int(reservedVariables.GetLength());
  6114. e.bc.GetVarsUsed(reservedVariables);
  6115. le.bc.GetVarsUsed(reservedVariables);
  6116. re.bc.GetVarsUsed(reservedVariables);
  6117. int offset = AllocateVariable(temp.dataType, true, false);
  6118. reservedVariables.SetLength(l);
  6119. temp.SetVariable(temp.dataType, offset, true);
  6120. // TODO: copy: Use copy constructor if available. See PrepareTemporaryObject()
  6121. CallDefaultConstructor(temp.dataType, offset, IsVariableOnHeap(offset), &ctx->bc, expr);
  6122. // Put the code for the condition expression on the output
  6123. MergeExprBytecode(ctx, &e);
  6124. // Add the branch decision
  6125. ctx->type = e.type;
  6126. ConvertToVariable(ctx);
  6127. ctx->bc.InstrSHORT(asBC_CpyVtoR4, ctx->type.stackOffset);
  6128. ctx->bc.Instr(asBC_ClrHi);
  6129. ctx->bc.InstrDWORD(asBC_JZ, elseLabel);
  6130. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  6131. // Assign the result of the left expression to the temporary variable
  6132. asCTypeInfo rtemp;
  6133. rtemp = temp;
  6134. if( rtemp.dataType.IsObjectHandle() )
  6135. rtemp.isExplicitHandle = true;
  6136. PrepareForAssignment(&rtemp.dataType, &le, cexpr->next, true);
  6137. MergeExprBytecode(ctx, &le);
  6138. if( !rtemp.dataType.IsPrimitive() )
  6139. {
  6140. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  6141. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  6142. }
  6143. asCTypeInfo result;
  6144. result = rtemp;
  6145. PerformAssignment(&result, &le.type, &ctx->bc, cexpr->next);
  6146. if( !result.dataType.IsPrimitive() )
  6147. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  6148. // Release the old temporary variable
  6149. ReleaseTemporaryVariable(le.type, &ctx->bc);
  6150. ctx->bc.InstrINT(asBC_JMP, afterLabel);
  6151. // Start of the right expression
  6152. ctx->bc.Label((short)elseLabel);
  6153. // Copy the result to the same temporary variable
  6154. PrepareForAssignment(&rtemp.dataType, &re, cexpr->next, true);
  6155. MergeExprBytecode(ctx, &re);
  6156. if( !rtemp.dataType.IsPrimitive() )
  6157. {
  6158. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  6159. rtemp.dataType.MakeReference(IsVariableOnHeap(offset));
  6160. }
  6161. result = rtemp;
  6162. PerformAssignment(&result, &re.type, &ctx->bc, cexpr->next);
  6163. if( !result.dataType.IsPrimitive() )
  6164. ctx->bc.Instr(asBC_PopPtr); // Pop the original value (always a pointer)
  6165. // Release the old temporary variable
  6166. ReleaseTemporaryVariable(re.type, &ctx->bc);
  6167. ctx->bc.Label((short)afterLabel);
  6168. // Make sure both expressions have the same type
  6169. if( !le.type.dataType.IsEqualExceptConst(re.type.dataType) )
  6170. Error(TXT_BOTH_MUST_BE_SAME, expr);
  6171. // Set the temporary variable as output
  6172. ctx->type = rtemp;
  6173. ctx->type.isExplicitHandle = isExplicitHandle;
  6174. if( !ctx->type.dataType.IsPrimitive() )
  6175. {
  6176. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  6177. ctx->type.dataType.MakeReference(IsVariableOnHeap(offset));
  6178. }
  6179. // Make sure the output isn't marked as being a literal constant
  6180. ctx->type.isConstant = false;
  6181. }
  6182. }
  6183. else
  6184. {
  6185. ctx->type.SetDummy();
  6186. return -1;
  6187. }
  6188. }
  6189. else
  6190. return CompileExpression(cexpr, ctx);
  6191. return 0;
  6192. }
  6193. int asCCompiler::CompileExpression(asCScriptNode *expr, asSExprContext *ctx)
  6194. {
  6195. asASSERT(expr->nodeType == snExpression);
  6196. // Convert to polish post fix, i.e: a+b => ab+
  6197. // The algorithm that I've implemented here is similar to
  6198. // Djikstra's Shunting Yard algorithm, though I didn't know it at the time.
  6199. // ref: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
  6200. // Count the nodes in order to preallocate the buffers
  6201. int count = 0;
  6202. asCScriptNode *node = expr->firstChild;
  6203. while( node )
  6204. {
  6205. count++;
  6206. node = node->next;
  6207. }
  6208. asCArray<asCScriptNode *> stack(count);
  6209. asCArray<asCScriptNode *> stack2(count);
  6210. node = expr->firstChild;
  6211. while( node )
  6212. {
  6213. int precedence = GetPrecedence(node);
  6214. while( stack.GetLength() > 0 &&
  6215. precedence <= GetPrecedence(stack[stack.GetLength()-1]) )
  6216. stack2.PushLast(stack.PopLast());
  6217. stack.PushLast(node);
  6218. node = node->next;
  6219. }
  6220. while( stack.GetLength() > 0 )
  6221. stack2.PushLast(stack.PopLast());
  6222. // Compile the postfix formatted expression
  6223. return CompilePostFixExpression(&stack2, ctx);
  6224. }
  6225. int asCCompiler::CompilePostFixExpression(asCArray<asCScriptNode *> *postfix, asSExprContext *ctx)
  6226. {
  6227. // Shouldn't send any byte code
  6228. asASSERT(ctx->bc.GetLastInstr() == -1);
  6229. // Set the context to a dummy type to avoid further
  6230. // errors in case the expression fails to compile
  6231. ctx->type.SetDummy();
  6232. // Evaluate the operands and operators
  6233. asCArray<asSExprContext*> free;
  6234. asCArray<asSExprContext*> expr;
  6235. int ret = 0;
  6236. for( asUINT n = 0; ret == 0 && n < postfix->GetLength(); n++ )
  6237. {
  6238. asCScriptNode *node = (*postfix)[n];
  6239. if( node->nodeType == snExprTerm )
  6240. {
  6241. asSExprContext *e = free.GetLength() ? free.PopLast() : asNEW(asSExprContext)(engine);
  6242. expr.PushLast(e);
  6243. e->exprNode = node;
  6244. ret = CompileExpressionTerm(node, e);
  6245. }
  6246. else
  6247. {
  6248. asSExprContext *r = expr.PopLast();
  6249. asSExprContext *l = expr.PopLast();
  6250. // Now compile the operator
  6251. asSExprContext *e = free.GetLength() ? free.PopLast() : asNEW(asSExprContext)(engine);
  6252. ret = CompileOperator(node, l, r, e);
  6253. expr.PushLast(e);
  6254. // Free the operands
  6255. l->Clear();
  6256. free.PushLast(l);
  6257. r->Clear();
  6258. free.PushLast(r);
  6259. }
  6260. }
  6261. if( ret == 0 )
  6262. {
  6263. asASSERT(expr.GetLength() == 1);
  6264. // The final result should be moved to the output context
  6265. MergeExprBytecodeAndType(ctx, expr[0]);
  6266. }
  6267. // Clean up
  6268. for( asUINT e = 0; e < expr.GetLength(); e++ )
  6269. asDELETE(expr[e], asSExprContext);
  6270. for( asUINT f = 0; f < free.GetLength(); f++ )
  6271. asDELETE(free[f], asSExprContext);
  6272. return ret;
  6273. }
  6274. int asCCompiler::CompileExpressionTerm(asCScriptNode *node, asSExprContext *ctx)
  6275. {
  6276. // Shouldn't send any byte code
  6277. asASSERT(ctx->bc.GetLastInstr() == -1);
  6278. // Set the type as a dummy by default, in case of any compiler errors
  6279. ctx->type.SetDummy();
  6280. // Compile the value node
  6281. asCScriptNode *vnode = node->firstChild;
  6282. while( vnode->nodeType != snExprValue )
  6283. vnode = vnode->next;
  6284. asSExprContext v(engine);
  6285. int r = CompileExpressionValue(vnode, &v); if( r < 0 ) return r;
  6286. // Compile post fix operators
  6287. asCScriptNode *pnode = vnode->next;
  6288. while( pnode )
  6289. {
  6290. r = CompileExpressionPostOp(pnode, &v); if( r < 0 ) return r;
  6291. pnode = pnode->next;
  6292. }
  6293. // Compile pre fix operators
  6294. pnode = vnode->prev;
  6295. while( pnode )
  6296. {
  6297. r = CompileExpressionPreOp(pnode, &v); if( r < 0 ) return r;
  6298. pnode = pnode->prev;
  6299. }
  6300. // Return the byte code and final type description
  6301. MergeExprBytecodeAndType(ctx, &v);
  6302. return 0;
  6303. }
  6304. int asCCompiler::CompileVariableAccess(const asCString &name, const asCString &scope, asSExprContext *ctx, asCScriptNode *errNode, bool isOptional, bool noFunction, bool noGlobal, asCObjectType *objType)
  6305. {
  6306. bool found = false;
  6307. // It is a local variable or parameter?
  6308. // This is not accessible by default arg expressions
  6309. sVariable *v = 0;
  6310. if( !isCompilingDefaultArg && scope == "" && !objType && variables )
  6311. v = variables->GetVariable(name.AddressOf());
  6312. if( v )
  6313. {
  6314. found = true;
  6315. if( v->isPureConstant )
  6316. ctx->type.SetConstantQW(v->type, v->constantValue);
  6317. else if( v->type.IsPrimitive() )
  6318. {
  6319. if( v->type.IsReference() )
  6320. {
  6321. // Copy the reference into the register
  6322. ctx->bc.InstrSHORT(asBC_PshVPtr, (short)v->stackOffset);
  6323. ctx->bc.Instr(asBC_PopRPtr);
  6324. ctx->type.Set(v->type);
  6325. }
  6326. else
  6327. ctx->type.SetVariable(v->type, v->stackOffset, false);
  6328. ctx->type.isLValue = true;
  6329. }
  6330. else
  6331. {
  6332. ctx->bc.InstrSHORT(asBC_PSF, (short)v->stackOffset);
  6333. ctx->type.SetVariable(v->type, v->stackOffset, false);
  6334. // If the variable is allocated on the heap we have a reference,
  6335. // otherwise the actual object pointer is pushed on the stack.
  6336. if( v->onHeap || v->type.IsObjectHandle() ) ctx->type.dataType.MakeReference(true);
  6337. // Implicitly dereference handle parameters sent by reference
  6338. if( v->type.IsReference() && (!v->type.IsObject() || v->type.IsObjectHandle()) )
  6339. ctx->bc.Instr(asBC_RDSPtr);
  6340. ctx->type.isLValue = true;
  6341. }
  6342. }
  6343. // Is it a class member?
  6344. // This is not accessible by default arg expressions
  6345. if( !isCompilingDefaultArg && !found && ((objType) || (outFunc && outFunc->objectType && scope == "")) )
  6346. {
  6347. if( name == THIS_TOKEN && !objType )
  6348. {
  6349. asCDataType dt = asCDataType::CreateObject(outFunc->objectType, outFunc->isReadOnly);
  6350. // The object pointer is located at stack position 0
  6351. ctx->bc.InstrSHORT(asBC_PSF, 0);
  6352. ctx->type.SetVariable(dt, 0, false);
  6353. ctx->type.dataType.MakeReference(true);
  6354. ctx->type.isLValue = true;
  6355. found = true;
  6356. }
  6357. if( !found )
  6358. {
  6359. // See if there are any matching property accessors
  6360. asSExprContext access(engine);
  6361. if( objType )
  6362. access.type.Set(asCDataType::CreateObject(objType, false));
  6363. else
  6364. access.type.Set(asCDataType::CreateObject(outFunc->objectType, outFunc->isReadOnly));
  6365. access.type.dataType.MakeReference(true);
  6366. int r = 0;
  6367. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  6368. {
  6369. // This is an index access, check if there is a property accessor that takes an index arg
  6370. asSExprContext dummyArg(engine);
  6371. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, 0, true);
  6372. }
  6373. if( r == 0 )
  6374. {
  6375. // Normal property access
  6376. r = FindPropertyAccessor(name, &access, errNode, 0, true);
  6377. }
  6378. if( r < 0 ) return -1;
  6379. if( access.property_get || access.property_set )
  6380. {
  6381. if( !objType )
  6382. {
  6383. // Prepare the bytecode for the member access
  6384. // This is only done when accessing through the implicit this pointer
  6385. ctx->bc.InstrSHORT(asBC_PSF, 0);
  6386. }
  6387. MergeExprBytecodeAndType(ctx, &access);
  6388. found = true;
  6389. }
  6390. }
  6391. if( !found )
  6392. {
  6393. asCDataType dt;
  6394. if( objType )
  6395. dt = asCDataType::CreateObject(objType, false);
  6396. else
  6397. dt = asCDataType::CreateObject(outFunc->objectType, false);
  6398. asCObjectProperty *prop = builder->GetObjectProperty(dt, name.AddressOf());
  6399. if( prop )
  6400. {
  6401. if( !objType )
  6402. {
  6403. // The object pointer is located at stack position 0
  6404. // This is only done when accessing through the implicit this pointer
  6405. ctx->bc.InstrSHORT(asBC_PSF, 0);
  6406. ctx->type.SetVariable(dt, 0, false);
  6407. ctx->type.dataType.MakeReference(true);
  6408. Dereference(ctx, true);
  6409. }
  6410. // TODO: This is the same as what is in CompileExpressionPostOp
  6411. // Put the offset on the stack
  6412. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(dt));
  6413. if( prop->type.IsReference() )
  6414. ctx->bc.Instr(asBC_RDSPtr);
  6415. // Reference to primitive must be stored in the temp register
  6416. if( prop->type.IsPrimitive() )
  6417. {
  6418. // TODO: runtime optimize: The ADD offset command should store the reference in the register directly
  6419. ctx->bc.Instr(asBC_PopRPtr);
  6420. }
  6421. // Set the new type (keeping info about temp variable)
  6422. ctx->type.dataType = prop->type;
  6423. ctx->type.dataType.MakeReference(true);
  6424. ctx->type.isVariable = false;
  6425. ctx->type.isLValue = true;
  6426. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  6427. {
  6428. // Objects that are members are not references
  6429. ctx->type.dataType.MakeReference(false);
  6430. }
  6431. // If the object reference is const, the property will also be const
  6432. ctx->type.dataType.MakeReadOnly(outFunc->isReadOnly);
  6433. found = true;
  6434. }
  6435. else if( outFunc->objectType )
  6436. {
  6437. // If it is not a property, it may still be the name of a method which can be used to create delegates
  6438. asCObjectType *ot = outFunc->objectType;
  6439. asCScriptFunction *func = 0;
  6440. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  6441. {
  6442. if( engine->scriptFunctions[ot->methods[n]]->name == name )
  6443. {
  6444. func = engine->scriptFunctions[ot->methods[n]];
  6445. break;
  6446. }
  6447. }
  6448. if( func )
  6449. {
  6450. // An object method was found. Keep the name of the method in the expression, but
  6451. // don't actually modify the bytecode at this point since it is not yet known what
  6452. // the method will be used for, or even what overloaded method should be used.
  6453. ctx->methodName = name;
  6454. // Place the object pointer on the stack, as if the expression was this.func
  6455. if( !objType )
  6456. {
  6457. // The object pointer is located at stack position 0
  6458. // This is only done when accessing through the implicit this pointer
  6459. ctx->bc.InstrSHORT(asBC_PSF, 0);
  6460. ctx->type.SetVariable(asCDataType::CreateObject(outFunc->objectType, false), 0, false);
  6461. ctx->type.dataType.MakeReference(true);
  6462. Dereference(ctx, true);
  6463. }
  6464. found = true;
  6465. }
  6466. }
  6467. }
  6468. }
  6469. // Recursively search parent namespaces for global entities
  6470. asCString currScope = scope;
  6471. if( scope == "" )
  6472. currScope = outFunc->nameSpace->name;
  6473. while( !found && !noGlobal && !objType )
  6474. {
  6475. asSNameSpace *ns = DetermineNameSpace(currScope);
  6476. // Is it a global property?
  6477. if( !found && ns )
  6478. {
  6479. // See if there are any matching global property accessors
  6480. asSExprContext access(engine);
  6481. int r = 0;
  6482. if( errNode->next && errNode->next->tokenType == ttOpenBracket )
  6483. {
  6484. // This is an index access, check if there is a property accessor that takes an index arg
  6485. asSExprContext dummyArg(engine);
  6486. r = FindPropertyAccessor(name, &access, &dummyArg, errNode, ns);
  6487. }
  6488. if( r == 0 )
  6489. {
  6490. // Normal property access
  6491. r = FindPropertyAccessor(name, &access, errNode, ns);
  6492. }
  6493. if( r < 0 ) return -1;
  6494. if( access.property_get || access.property_set )
  6495. {
  6496. // Prepare the bytecode for the function call
  6497. MergeExprBytecodeAndType(ctx, &access);
  6498. found = true;
  6499. }
  6500. // See if there is any matching global property
  6501. if( !found )
  6502. {
  6503. bool isCompiled = true;
  6504. bool isPureConstant = false;
  6505. bool isAppProp = false;
  6506. asQWORD constantValue = 0;
  6507. asCGlobalProperty *prop = builder->GetGlobalProperty(name.AddressOf(), ns, &isCompiled, &isPureConstant, &constantValue, &isAppProp);
  6508. if( prop )
  6509. {
  6510. found = true;
  6511. // Verify that the global property has been compiled already
  6512. if( isCompiled )
  6513. {
  6514. if( ctx->type.dataType.GetObjectType() && (ctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE) )
  6515. {
  6516. ctx->type.dataType.MakeHandle(true);
  6517. ctx->type.isExplicitHandle = true;
  6518. }
  6519. // If the global property is a pure constant
  6520. // we can allow the compiler to optimize it. Pure
  6521. // constants are global constant variables that were
  6522. // initialized by literal constants.
  6523. if( isPureConstant )
  6524. ctx->type.SetConstantQW(prop->type, constantValue);
  6525. else
  6526. {
  6527. // A shared type must not access global vars, unless they
  6528. // too are shared, e.g. application registered vars
  6529. if( outFunc->IsShared() )
  6530. {
  6531. if( !isAppProp )
  6532. {
  6533. asCString str;
  6534. str.Format(TXT_SHARED_CANNOT_ACCESS_NON_SHARED_VAR_s, prop->name.AddressOf());
  6535. Error(str, errNode);
  6536. // Allow the compilation to continue to catch other problems
  6537. }
  6538. }
  6539. ctx->type.Set(prop->type);
  6540. ctx->type.isLValue = true;
  6541. if( ctx->type.dataType.IsPrimitive() )
  6542. {
  6543. // Load the address of the variable into the register
  6544. ctx->bc.InstrPTR(asBC_LDG, prop->GetAddressOfValue());
  6545. ctx->type.dataType.MakeReference(true);
  6546. }
  6547. else
  6548. {
  6549. // Push the address of the variable on the stack
  6550. ctx->bc.InstrPTR(asBC_PGA, prop->GetAddressOfValue());
  6551. // If the object is a value type or a non-handle variable to a reference type,
  6552. // then we must validate the existance as it could potentially be accessed
  6553. // before it is initialized.
  6554. if( (ctx->type.dataType.GetObjectType()->flags & asOBJ_VALUE) ||
  6555. !ctx->type.dataType.IsObjectHandle() )
  6556. {
  6557. // TODO: runtime optimize: This is not necessary for application registered properties
  6558. ctx->bc.Instr(asBC_ChkRefS);
  6559. }
  6560. // If the address pushed on the stack is to a value type or an object
  6561. // handle, then mark the expression as a reference. Addresses to a reference
  6562. // type aren't marked as references to get correct behaviour
  6563. if( (ctx->type.dataType.GetObjectType()->flags & asOBJ_VALUE) ||
  6564. ctx->type.dataType.IsObjectHandle() )
  6565. {
  6566. ctx->type.dataType.MakeReference(true);
  6567. }
  6568. else
  6569. {
  6570. asASSERT( (ctx->type.dataType.GetObjectType()->flags & asOBJ_REF) && !ctx->type.dataType.IsObjectHandle() );
  6571. // It's necessary to dereference the pointer so the pointer on the stack will point to the actual object
  6572. ctx->bc.Instr(asBC_RDSPtr);
  6573. }
  6574. }
  6575. }
  6576. }
  6577. else
  6578. {
  6579. asCString str;
  6580. str.Format(TXT_UNINITIALIZED_GLOBAL_VAR_s, prop->name.AddressOf());
  6581. Error(str, errNode);
  6582. return -1;
  6583. }
  6584. }
  6585. }
  6586. }
  6587. // Is it the name of a global function?
  6588. if( !noFunction && !found && ns )
  6589. {
  6590. asCArray<int> funcs;
  6591. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  6592. if( funcs.GetLength() > 0 )
  6593. {
  6594. found = true;
  6595. // Defer the evaluation of which function until it is actually used
  6596. // Store the namespace and name of the function for later
  6597. ctx->type.SetUndefinedFuncHandle(engine);
  6598. ctx->methodName = ns ? ns->name + "::" + name : name;
  6599. }
  6600. }
  6601. // Is it an enum value?
  6602. if( !found )
  6603. {
  6604. // The enum type may be declared in a namespace too
  6605. asCObjectType *scopeType = 0;
  6606. if( currScope != "" && currScope != "::" )
  6607. {
  6608. // Use the last scope name as the enum type
  6609. asCString enumType = currScope;
  6610. asCString nsScope;
  6611. int p = currScope.FindLast("::");
  6612. if( p != -1 )
  6613. {
  6614. enumType = currScope.SubString(p+2);
  6615. nsScope = currScope.SubString(0, p);
  6616. }
  6617. asSNameSpace *ns = engine->FindNameSpace(nsScope.AddressOf());
  6618. if( ns )
  6619. scopeType = builder->GetObjectType(enumType.AddressOf(), ns);
  6620. }
  6621. asDWORD value = 0;
  6622. asCDataType dt;
  6623. if( scopeType && builder->GetEnumValueFromObjectType(scopeType, name.AddressOf(), dt, value) )
  6624. {
  6625. // scoped enum value found
  6626. found = true;
  6627. }
  6628. else if( !engine->ep.requireEnumScope )
  6629. {
  6630. // Look for the enum value without explicitly informing the enum type
  6631. asSNameSpace *ns = DetermineNameSpace(currScope);
  6632. int e = 0;
  6633. if( ns )
  6634. e = builder->GetEnumValue(name.AddressOf(), dt, value, ns);
  6635. if( e )
  6636. {
  6637. found = true;
  6638. if( e == 2 )
  6639. {
  6640. // Ambiguous enum value: Save the name for resolution later.
  6641. // The ambiguity could be resolved now, but I hesitate
  6642. // to store too much information in the context.
  6643. ctx->enumValue = name.AddressOf();
  6644. // We cannot set a dummy value because it will pass through
  6645. // cleanly as an integer.
  6646. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttIdentifier, true), 0);
  6647. return 0;
  6648. }
  6649. }
  6650. }
  6651. if( found )
  6652. {
  6653. // Even if the enum type is not shared, and we're compiling a shared object,
  6654. // the use of the values are still allowed, since they are treated as constants.
  6655. // an enum value was resolved
  6656. ctx->type.SetConstantDW(dt, value);
  6657. }
  6658. else
  6659. {
  6660. // If nothing was found because the scope doesn't match a namespace or an enum
  6661. // then this should be reported as an error and the search interrupted
  6662. if( !ns && !scopeType )
  6663. {
  6664. ctx->type.SetDummy();
  6665. asCString str;
  6666. str.Format(TXT_UNKNOWN_SCOPE_s, currScope.AddressOf());
  6667. Error(str, errNode);
  6668. return -1;
  6669. }
  6670. }
  6671. }
  6672. if( !found )
  6673. {
  6674. if( currScope == "" || currScope == "::" )
  6675. break;
  6676. // Move up to parent namespace
  6677. int pos = currScope.FindLast("::");
  6678. if( pos >= 0 )
  6679. currScope = currScope.SubString(0, pos);
  6680. else
  6681. currScope = "::";
  6682. }
  6683. }
  6684. // The name doesn't match any variable
  6685. if( !found )
  6686. {
  6687. // Give dummy value
  6688. ctx->type.SetDummy();
  6689. if( !isOptional )
  6690. {
  6691. // Prepend the scope to the name for the error message
  6692. asCString ename;
  6693. if( scope != "" && scope != "::" )
  6694. ename = scope + "::";
  6695. else
  6696. ename = scope;
  6697. ename += name;
  6698. asCString str;
  6699. str.Format(TXT_s_NOT_DECLARED, ename.AddressOf());
  6700. Error(str, errNode);
  6701. // Declare the variable now so that it will not be reported again
  6702. variables->DeclareVariable(name.AddressOf(), asCDataType::CreatePrimitive(ttInt, false), 0x7FFF, true);
  6703. // Mark the variable as initialized so that the user will not be bother by it again
  6704. sVariable *v = variables->GetVariable(name.AddressOf());
  6705. asASSERT(v);
  6706. if( v ) v->isInitialized = true;
  6707. }
  6708. // Return -1 to signal that the variable wasn't found
  6709. return -1;
  6710. }
  6711. return 0;
  6712. }
  6713. int asCCompiler::CompileExpressionValue(asCScriptNode *node, asSExprContext *ctx)
  6714. {
  6715. // Shouldn't receive any byte code
  6716. asASSERT(ctx->bc.GetLastInstr() == -1);
  6717. asCScriptNode *vnode = node->firstChild;
  6718. ctx->exprNode = vnode;
  6719. if( vnode->nodeType == snVariableAccess )
  6720. {
  6721. // Determine the scope resolution of the variable
  6722. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script, &vnode);
  6723. // Determine the name of the variable
  6724. asASSERT(vnode->nodeType == snIdentifier );
  6725. asCString name(&script->code[vnode->tokenPos], vnode->tokenLength);
  6726. return CompileVariableAccess(name, scope, ctx, node);
  6727. }
  6728. else if( vnode->nodeType == snConstant )
  6729. {
  6730. if( vnode->tokenType == ttIntConstant )
  6731. {
  6732. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  6733. asQWORD val = asStringScanUInt64(value.AddressOf(), 10, 0);
  6734. // Do we need 64 bits?
  6735. // If the 31st bit is set we'll treat the value as a signed 64bit number to avoid
  6736. // incorrect warnings about changing signs if the value is assigned to a 64bit variable
  6737. if( val>>31 )
  6738. {
  6739. // Only if the value uses the last bit of a 64bit word do we consider the number unsigned
  6740. if( val>>63 )
  6741. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  6742. else
  6743. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttInt64, true), val);
  6744. }
  6745. else
  6746. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), asDWORD(val));
  6747. }
  6748. else if( vnode->tokenType == ttBitsConstant )
  6749. {
  6750. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  6751. // Let the function determine the radix from the prefix 0x = 16, 0d = 10, 0o = 8, or 0b = 2
  6752. // TODO: Check for overflow
  6753. asQWORD val = asStringScanUInt64(value.AddressOf(), 0, 0);
  6754. // Do we need 64 bits?
  6755. if( val>>32 )
  6756. ctx->type.SetConstantQW(asCDataType::CreatePrimitive(ttUInt64, true), val);
  6757. else
  6758. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), asDWORD(val));
  6759. }
  6760. else if( vnode->tokenType == ttFloatConstant )
  6761. {
  6762. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  6763. // TODO: Check for overflow
  6764. size_t numScanned;
  6765. float v = float(asStringScanDouble(value.AddressOf(), &numScanned));
  6766. ctx->type.SetConstantF(asCDataType::CreatePrimitive(ttFloat, true), v);
  6767. #ifndef AS_USE_DOUBLE_AS_FLOAT
  6768. // Don't check this if we have double as float, because then the whole token would be scanned (i.e. no f suffix)
  6769. asASSERT(numScanned == vnode->tokenLength - 1);
  6770. #endif
  6771. }
  6772. else if( vnode->tokenType == ttDoubleConstant )
  6773. {
  6774. asCString value(&script->code[vnode->tokenPos], vnode->tokenLength);
  6775. // TODO: Check for overflow
  6776. size_t numScanned;
  6777. double v = asStringScanDouble(value.AddressOf(), &numScanned);
  6778. ctx->type.SetConstantD(asCDataType::CreatePrimitive(ttDouble, true), v);
  6779. asASSERT(numScanned == vnode->tokenLength);
  6780. }
  6781. else if( vnode->tokenType == ttTrue ||
  6782. vnode->tokenType == ttFalse )
  6783. {
  6784. #if AS_SIZEOF_BOOL == 1
  6785. ctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  6786. #else
  6787. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), vnode->tokenType == ttTrue ? VALUE_OF_BOOLEAN_TRUE : 0);
  6788. #endif
  6789. }
  6790. else if( vnode->tokenType == ttStringConstant ||
  6791. vnode->tokenType == ttMultilineStringConstant ||
  6792. vnode->tokenType == ttHeredocStringConstant )
  6793. {
  6794. asCString str;
  6795. asCScriptNode *snode = vnode->firstChild;
  6796. if( script->code[snode->tokenPos] == '\'' && engine->ep.useCharacterLiterals )
  6797. {
  6798. // Treat the single quoted string as a single character literal
  6799. str.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  6800. asDWORD val = 0;
  6801. if( str.GetLength() && (unsigned char)str[0] > 127 && engine->ep.scanner == 1 )
  6802. {
  6803. // This is the start of a UTF8 encoded character. We need to decode it
  6804. val = asStringDecodeUTF8(str.AddressOf(), 0);
  6805. if( val == (asDWORD)-1 )
  6806. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  6807. }
  6808. else
  6809. {
  6810. val = ProcessStringConstant(str, snode);
  6811. if( val == (asDWORD)-1 )
  6812. Error(TXT_INVALID_CHAR_LITERAL, vnode);
  6813. }
  6814. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttUInt, true), val);
  6815. }
  6816. else
  6817. {
  6818. // Process the string constants
  6819. while( snode )
  6820. {
  6821. asCString cat;
  6822. if( snode->tokenType == ttStringConstant )
  6823. {
  6824. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  6825. ProcessStringConstant(cat, snode);
  6826. }
  6827. else if( snode->tokenType == ttMultilineStringConstant )
  6828. {
  6829. if( !engine->ep.allowMultilineStrings )
  6830. Error(TXT_MULTILINE_STRINGS_NOT_ALLOWED, snode);
  6831. cat.Assign(&script->code[snode->tokenPos+1], snode->tokenLength-2);
  6832. ProcessStringConstant(cat, snode);
  6833. }
  6834. else if( snode->tokenType == ttHeredocStringConstant )
  6835. {
  6836. cat.Assign(&script->code[snode->tokenPos+3], snode->tokenLength-6);
  6837. ProcessHeredocStringConstant(cat, snode);
  6838. }
  6839. str += cat;
  6840. snode = snode->next;
  6841. }
  6842. // Call the string factory function to create a string object
  6843. asCScriptFunction *descr = engine->stringFactory;
  6844. if( descr == 0 )
  6845. {
  6846. // Error
  6847. Error(TXT_STRINGS_NOT_RECOGNIZED, vnode);
  6848. // Give dummy value
  6849. ctx->type.SetDummy();
  6850. return -1;
  6851. }
  6852. else
  6853. {
  6854. // Register the constant string with the engine
  6855. int id = engine->AddConstantString(str.AddressOf(), str.GetLength());
  6856. ctx->bc.InstrWORD(asBC_STR, (asWORD)id);
  6857. bool useVariable = false;
  6858. int stackOffset = 0;
  6859. if( descr->DoesReturnOnStack() )
  6860. {
  6861. useVariable = true;
  6862. stackOffset = AllocateVariable(descr->returnType, true);
  6863. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  6864. }
  6865. PerformFunctionCall(descr->id, ctx, false, 0, 0, useVariable, stackOffset);
  6866. }
  6867. }
  6868. }
  6869. else if( vnode->tokenType == ttNull )
  6870. {
  6871. ctx->bc.Instr(asBC_PshNull);
  6872. ctx->type.SetNullConstant();
  6873. }
  6874. else
  6875. asASSERT(false);
  6876. }
  6877. else if( vnode->nodeType == snFunctionCall )
  6878. {
  6879. // Determine the scope resolution
  6880. asCString scope = builder->GetScopeFromNode(vnode->firstChild, script);
  6881. return CompileFunctionCall(vnode, ctx, 0, false, scope);
  6882. }
  6883. else if( vnode->nodeType == snConstructCall )
  6884. {
  6885. CompileConstructCall(vnode, ctx);
  6886. }
  6887. else if( vnode->nodeType == snAssignment )
  6888. {
  6889. asSExprContext e(engine);
  6890. int r = CompileAssignment(vnode, &e);
  6891. if( r < 0 )
  6892. {
  6893. ctx->type.SetDummy();
  6894. return r;
  6895. }
  6896. MergeExprBytecodeAndType(ctx, &e);
  6897. }
  6898. else if( vnode->nodeType == snCast )
  6899. {
  6900. // Implement the cast operator
  6901. CompileConversion(vnode, ctx);
  6902. }
  6903. else if( vnode->nodeType == snUndefined && vnode->tokenType == ttVoid )
  6904. {
  6905. // This is a void expression
  6906. ctx->type.SetVoidExpression();
  6907. }
  6908. else
  6909. asASSERT(false);
  6910. return 0;
  6911. }
  6912. asUINT asCCompiler::ProcessStringConstant(asCString &cstr, asCScriptNode *node, bool processEscapeSequences)
  6913. {
  6914. int charLiteral = -1;
  6915. // Process escape sequences
  6916. asCArray<char> str((int)cstr.GetLength());
  6917. for( asUINT n = 0; n < cstr.GetLength(); n++ )
  6918. {
  6919. #ifdef AS_DOUBLEBYTE_CHARSET
  6920. // Double-byte charset is only allowed for ASCII and not UTF16 encoded strings
  6921. if( (cstr[n] & 0x80) && engine->ep.scanner == 0 && engine->ep.stringEncoding != 1 )
  6922. {
  6923. // This is the lead character of a double byte character
  6924. // include the trail character without checking it's value.
  6925. str.PushLast(cstr[n]);
  6926. n++;
  6927. str.PushLast(cstr[n]);
  6928. continue;
  6929. }
  6930. #endif
  6931. asUINT val;
  6932. if( processEscapeSequences && cstr[n] == '\\' )
  6933. {
  6934. ++n;
  6935. if( n == cstr.GetLength() )
  6936. {
  6937. if( charLiteral == -1 ) charLiteral = 0;
  6938. return charLiteral;
  6939. }
  6940. // Hexadecimal escape sequences will allow the construction of
  6941. // invalid unicode sequences, but the string should also work as
  6942. // a bytearray so we must support this. The code for working with
  6943. // unicode text must be prepared to handle invalid unicode sequences
  6944. if( cstr[n] == 'x' || cstr[n] == 'X' )
  6945. {
  6946. ++n;
  6947. if( n == cstr.GetLength() ) break;
  6948. val = 0;
  6949. int c = engine->ep.stringEncoding == 1 ? 4 : 2;
  6950. for( ; c > 0 && n < cstr.GetLength(); c--, n++ )
  6951. {
  6952. if( cstr[n] >= '0' && cstr[n] <= '9' )
  6953. val = val*16 + cstr[n] - '0';
  6954. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  6955. val = val*16 + cstr[n] - 'a' + 10;
  6956. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  6957. val = val*16 + cstr[n] - 'A' + 10;
  6958. else
  6959. break;
  6960. }
  6961. // Rewind one, since the loop will increment it again
  6962. n--;
  6963. // Hexadecimal escape sequences produce exact value, even if it is not proper unicode chars
  6964. if( engine->ep.stringEncoding == 0 )
  6965. {
  6966. str.PushLast((asBYTE)val);
  6967. }
  6968. else
  6969. {
  6970. #ifndef AS_BIG_ENDIAN
  6971. str.PushLast((asBYTE)val);
  6972. str.PushLast((asBYTE)(val>>8));
  6973. #else
  6974. str.PushLast((asBYTE)(val>>8));
  6975. str.PushLast((asBYTE)val);
  6976. #endif
  6977. }
  6978. if( charLiteral == -1 ) charLiteral = val;
  6979. continue;
  6980. }
  6981. else if( cstr[n] == 'u' || cstr[n] == 'U' )
  6982. {
  6983. // \u expects 4 hex digits
  6984. // \U expects 8 hex digits
  6985. bool expect2 = cstr[n] == 'u';
  6986. int c = expect2 ? 4 : 8;
  6987. val = 0;
  6988. for( ; c > 0; c-- )
  6989. {
  6990. ++n;
  6991. if( n == cstr.GetLength() ) break;
  6992. if( cstr[n] >= '0' && cstr[n] <= '9' )
  6993. val = val*16 + cstr[n] - '0';
  6994. else if( cstr[n] >= 'a' && cstr[n] <= 'f' )
  6995. val = val*16 + cstr[n] - 'a' + 10;
  6996. else if( cstr[n] >= 'A' && cstr[n] <= 'F' )
  6997. val = val*16 + cstr[n] - 'A' + 10;
  6998. else
  6999. break;
  7000. }
  7001. if( c != 0 )
  7002. {
  7003. // Give warning about invalid code point
  7004. // TODO: Need code position for warning
  7005. asCString msg;
  7006. msg.Format(TXT_INVALID_UNICODE_FORMAT_EXPECTED_d, expect2 ? 4 : 8);
  7007. Warning(msg, node);
  7008. continue;
  7009. }
  7010. }
  7011. else
  7012. {
  7013. if( cstr[n] == '"' )
  7014. val = '"';
  7015. else if( cstr[n] == '\'' )
  7016. val = '\'';
  7017. else if( cstr[n] == 'n' )
  7018. val = '\n';
  7019. else if( cstr[n] == 'r' )
  7020. val = '\r';
  7021. else if( cstr[n] == 't' )
  7022. val = '\t';
  7023. else if( cstr[n] == '0' )
  7024. val = '\0';
  7025. else if( cstr[n] == '\\' )
  7026. val = '\\';
  7027. else
  7028. {
  7029. // Invalid escape sequence
  7030. Warning(TXT_INVALID_ESCAPE_SEQUENCE, node);
  7031. continue;
  7032. }
  7033. }
  7034. }
  7035. else
  7036. {
  7037. if( engine->ep.scanner == 1 && (cstr[n] & 0x80) )
  7038. {
  7039. unsigned int len;
  7040. val = asStringDecodeUTF8(&cstr[n], &len);
  7041. if( val == 0xFFFFFFFF )
  7042. {
  7043. // Incorrect UTF8 encoding. Use only the first byte
  7044. // TODO: Need code position for warning
  7045. Warning(TXT_INVALID_UNICODE_SEQUENCE_IN_SRC, node);
  7046. val = (unsigned char)cstr[n];
  7047. }
  7048. else
  7049. n += len-1;
  7050. }
  7051. else
  7052. val = (unsigned char)cstr[n];
  7053. }
  7054. // Add the character to the final string
  7055. char encodedValue[5];
  7056. int len;
  7057. if( engine->ep.scanner == 1 && engine->ep.stringEncoding == 0 )
  7058. {
  7059. // Convert to UTF8 encoded
  7060. len = asStringEncodeUTF8(val, encodedValue);
  7061. }
  7062. else if( engine->ep.stringEncoding == 1 )
  7063. {
  7064. // Convert to 16bit wide character string (even if the script is scanned as ASCII)
  7065. len = asStringEncodeUTF16(val, encodedValue);
  7066. }
  7067. else
  7068. {
  7069. // Do not convert ASCII characters
  7070. encodedValue[0] = (asBYTE)val;
  7071. len = 1;
  7072. }
  7073. if( len < 0 )
  7074. {
  7075. // Give warning about invalid code point
  7076. // TODO: Need code position for warning
  7077. Warning(TXT_INVALID_UNICODE_VALUE, node);
  7078. }
  7079. else
  7080. {
  7081. // Add the encoded value to the final string
  7082. str.Concatenate(encodedValue, len);
  7083. if( charLiteral == -1 ) charLiteral = val;
  7084. }
  7085. }
  7086. cstr.Assign(str.AddressOf(), str.GetLength());
  7087. return charLiteral;
  7088. }
  7089. void asCCompiler::ProcessHeredocStringConstant(asCString &str, asCScriptNode *node)
  7090. {
  7091. // Remove first line if it only contains whitespace
  7092. int start;
  7093. for( start = 0; start < (int)str.GetLength(); start++ )
  7094. {
  7095. if( str[start] == '\n' )
  7096. {
  7097. // Remove the linebreak as well
  7098. start++;
  7099. break;
  7100. }
  7101. if( str[start] != ' ' &&
  7102. str[start] != '\t' &&
  7103. str[start] != '\r' )
  7104. {
  7105. // Don't remove anything
  7106. start = 0;
  7107. break;
  7108. }
  7109. }
  7110. // Remove the line after the last line break if it only contains whitespaces
  7111. int end;
  7112. for( end = (int)str.GetLength() - 1; end >= 0; end-- )
  7113. {
  7114. if( str[end] == '\n' )
  7115. {
  7116. // Don't remove the last line break
  7117. end++;
  7118. break;
  7119. }
  7120. if( str[end] != ' ' &&
  7121. str[end] != '\t' &&
  7122. str[end] != '\r' )
  7123. {
  7124. // Don't remove anything
  7125. end = (int)str.GetLength();
  7126. break;
  7127. }
  7128. }
  7129. if( end < 0 ) end = 0;
  7130. asCString tmp;
  7131. if( end > start )
  7132. tmp.Assign(&str[start], end-start);
  7133. ProcessStringConstant(tmp, node, false);
  7134. str = tmp;
  7135. }
  7136. void asCCompiler::CompileConversion(asCScriptNode *node, asSExprContext *ctx)
  7137. {
  7138. asSExprContext expr(engine);
  7139. asCDataType to;
  7140. bool anyErrors = false;
  7141. EImplicitConv convType;
  7142. if( node->nodeType == snConstructCall )
  7143. {
  7144. convType = asIC_EXPLICIT_VAL_CAST;
  7145. // Verify that there is only one argument
  7146. if( node->lastChild->firstChild == 0 ||
  7147. node->lastChild->firstChild != node->lastChild->lastChild )
  7148. {
  7149. Error(TXT_ONLY_ONE_ARGUMENT_IN_CAST, node->lastChild);
  7150. expr.type.SetDummy();
  7151. anyErrors = true;
  7152. }
  7153. else
  7154. {
  7155. // Compile the expression
  7156. int r = CompileAssignment(node->lastChild->firstChild, &expr);
  7157. if( r < 0 )
  7158. anyErrors = true;
  7159. }
  7160. // Determine the requested type
  7161. to = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  7162. to.MakeReadOnly(true); // Default to const
  7163. asASSERT(to.IsPrimitive());
  7164. }
  7165. else
  7166. {
  7167. convType = asIC_EXPLICIT_REF_CAST;
  7168. // Compile the expression
  7169. int r = CompileAssignment(node->lastChild, &expr);
  7170. if( r < 0 )
  7171. anyErrors = true;
  7172. // Determine the requested type
  7173. to = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  7174. to = builder->ModifyDataTypeFromNode(to, node->firstChild->next, script, 0, 0);
  7175. // If the type support object handles, then use it
  7176. if( to.SupportHandles() )
  7177. {
  7178. to.MakeHandle(true);
  7179. }
  7180. else if( !to.IsObjectHandle() )
  7181. {
  7182. // The cast<type> operator can only be used for reference casts
  7183. Error(TXT_ILLEGAL_TARGET_TYPE_FOR_REF_CAST, node->firstChild);
  7184. anyErrors = true;
  7185. }
  7186. }
  7187. // Do not allow casting to non shared type if we're compiling a shared method
  7188. if( outFunc->IsShared() &&
  7189. to.GetObjectType() && !to.GetObjectType()->IsShared() )
  7190. {
  7191. asCString msg;
  7192. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, to.GetObjectType()->name.AddressOf());
  7193. Error(msg, node);
  7194. anyErrors = true;
  7195. }
  7196. if( anyErrors )
  7197. {
  7198. // Assume that the error can be fixed and allow the compilation to continue
  7199. ctx->type.SetConstantDW(to, 0);
  7200. return;
  7201. }
  7202. ProcessPropertyGetAccessor(&expr, node);
  7203. // Don't allow any operators on expressions that take address of class method
  7204. if( expr.IsClassMethod() )
  7205. {
  7206. Error(TXT_INVALID_OP_ON_METHOD, node);
  7207. return;
  7208. }
  7209. // We don't want a reference for conversion casts
  7210. if( convType == asIC_EXPLICIT_VAL_CAST && expr.type.dataType.IsReference() )
  7211. {
  7212. if( expr.type.dataType.IsObject() )
  7213. Dereference(&expr, true);
  7214. else
  7215. ConvertToVariable(&expr);
  7216. }
  7217. ImplicitConversion(&expr, to, node, convType);
  7218. IsVariableInitialized(&expr.type, node);
  7219. // If no type conversion is really tried ignore it
  7220. if( to == expr.type.dataType )
  7221. {
  7222. // This will keep information about constant type
  7223. MergeExprBytecode(ctx, &expr);
  7224. ctx->type = expr.type;
  7225. return;
  7226. }
  7227. if( to.IsEqualExceptConst(expr.type.dataType) && to.IsPrimitive() )
  7228. {
  7229. MergeExprBytecode(ctx, &expr);
  7230. ctx->type = expr.type;
  7231. ctx->type.dataType.MakeReadOnly(true);
  7232. return;
  7233. }
  7234. // The implicit conversion already does most of the conversions permitted,
  7235. // here we'll only treat those conversions that require an explicit cast.
  7236. bool conversionOK = false;
  7237. if( !expr.type.isConstant && expr.type.dataType != asCDataType::CreatePrimitive(ttVoid, false) )
  7238. {
  7239. if( !expr.type.dataType.IsObject() )
  7240. ConvertToTempVariable(&expr);
  7241. if( to.IsObjectHandle() &&
  7242. expr.type.dataType.IsObjectHandle() &&
  7243. !(!to.IsHandleToConst() && expr.type.dataType.IsHandleToConst()) )
  7244. {
  7245. conversionOK = CompileRefCast(&expr, to, true, node);
  7246. MergeExprBytecode(ctx, &expr);
  7247. ctx->type = expr.type;
  7248. }
  7249. }
  7250. if( conversionOK )
  7251. return;
  7252. // Conversion not available
  7253. ctx->type.SetDummy();
  7254. asCString strTo, strFrom;
  7255. strTo = to.Format();
  7256. strFrom = expr.type.dataType.Format();
  7257. asCString msg;
  7258. msg.Format(TXT_NO_CONVERSION_s_TO_s, strFrom.AddressOf(), strTo.AddressOf());
  7259. Error(msg, node);
  7260. }
  7261. void asCCompiler::AfterFunctionCall(int funcID, asCArray<asSExprContext*> &args, asSExprContext *ctx, bool deferAll)
  7262. {
  7263. asCScriptFunction *descr = builder->GetFunctionDescription(funcID);
  7264. // Parameters that are sent by reference should be assigned
  7265. // to the evaluated expression if it is an lvalue
  7266. // Evaluate the arguments from last to first
  7267. int n = (int)descr->parameterTypes.GetLength() - 1;
  7268. for( ; n >= 0; n-- )
  7269. {
  7270. if( (descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] & asTM_OUTREF)) ||
  7271. (descr->parameterTypes[n].IsObject() && deferAll) )
  7272. {
  7273. asASSERT( !(descr->parameterTypes[n].IsReference() && (descr->inOutFlags[n] == asTM_OUTREF)) || args[n]->origExpr );
  7274. // For &inout, only store the argument if it is for a temporary variable
  7275. if( engine->ep.allowUnsafeReferences ||
  7276. descr->inOutFlags[n] != asTM_INOUTREF || args[n]->type.isTemporary )
  7277. {
  7278. // Store the argument for later processing
  7279. asSDeferredParam outParam;
  7280. outParam.argNode = args[n]->exprNode;
  7281. outParam.argType = args[n]->type;
  7282. outParam.argInOutFlags = descr->inOutFlags[n];
  7283. outParam.origExpr = args[n]->origExpr;
  7284. ctx->deferredParams.PushLast(outParam);
  7285. }
  7286. }
  7287. else
  7288. {
  7289. // Release the temporary variable now
  7290. ReleaseTemporaryVariable(args[n]->type, &ctx->bc);
  7291. }
  7292. // Move the argument's deferred expressions over to the final expression
  7293. for( asUINT m = 0; m < args[n]->deferredParams.GetLength(); m++ )
  7294. {
  7295. ctx->deferredParams.PushLast(args[n]->deferredParams[m]);
  7296. args[n]->deferredParams[m].origExpr = 0;
  7297. }
  7298. args[n]->deferredParams.SetLength(0);
  7299. }
  7300. }
  7301. void asCCompiler::ProcessDeferredParams(asSExprContext *ctx)
  7302. {
  7303. if( isProcessingDeferredParams ) return;
  7304. isProcessingDeferredParams = true;
  7305. for( asUINT n = 0; n < ctx->deferredParams.GetLength(); n++ )
  7306. {
  7307. asSDeferredParam outParam = ctx->deferredParams[n];
  7308. if( outParam.argInOutFlags < asTM_OUTREF ) // &in, or not reference
  7309. {
  7310. // Just release the variable
  7311. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  7312. }
  7313. else if( outParam.argInOutFlags == asTM_OUTREF )
  7314. {
  7315. asSExprContext *expr = outParam.origExpr;
  7316. outParam.origExpr = 0;
  7317. if( outParam.argType.dataType.IsObjectHandle() )
  7318. {
  7319. // Implicitly convert the value to a handle
  7320. if( expr->type.dataType.IsObjectHandle() )
  7321. expr->type.isExplicitHandle = true;
  7322. }
  7323. // Verify that the expression result in a lvalue, or a property accessor
  7324. if( IsLValue(expr->type) || expr->property_get || expr->property_set )
  7325. {
  7326. asSExprContext rctx(engine);
  7327. rctx.type = outParam.argType;
  7328. if( rctx.type.dataType.IsPrimitive() )
  7329. rctx.type.dataType.MakeReference(false);
  7330. else
  7331. {
  7332. rctx.bc.InstrSHORT(asBC_PSF, outParam.argType.stackOffset);
  7333. rctx.type.dataType.MakeReference(IsVariableOnHeap(outParam.argType.stackOffset));
  7334. if( expr->type.isExplicitHandle )
  7335. rctx.type.isExplicitHandle = true;
  7336. }
  7337. asSExprContext o(engine);
  7338. DoAssignment(&o, expr, &rctx, outParam.argNode, outParam.argNode, ttAssignment, outParam.argNode);
  7339. if( !o.type.dataType.IsPrimitive() ) o.bc.Instr(asBC_PopPtr);
  7340. // The assignment may itself have resulted in a new temporary variable, e.g. if
  7341. // the opAssign returns a non-reference. We must release this temporary variable
  7342. // since it won't be used
  7343. ReleaseTemporaryVariable(o.type, &o.bc);
  7344. MergeExprBytecode(ctx, &o);
  7345. }
  7346. else
  7347. {
  7348. // We must still evaluate the expression
  7349. MergeExprBytecode(ctx, expr);
  7350. if( !expr->type.IsVoidExpression() && (!expr->type.isConstant || expr->type.IsNullConstant()) )
  7351. ctx->bc.Instr(asBC_PopPtr);
  7352. // Give a warning, except if the argument is void, null or 0 which indicate the argument is really to be ignored
  7353. if( !expr->type.IsVoidExpression() && !expr->type.IsNullConstant() && !(expr->type.isConstant && expr->type.qwordValue == 0) )
  7354. Warning(TXT_ARG_NOT_LVALUE, outParam.argNode);
  7355. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  7356. }
  7357. ReleaseTemporaryVariable(expr->type, &ctx->bc);
  7358. // Delete the original expression context
  7359. asDELETE(expr,asSExprContext);
  7360. }
  7361. else // &inout
  7362. {
  7363. if( outParam.argType.isTemporary )
  7364. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  7365. else if( !outParam.argType.isVariable )
  7366. {
  7367. if( outParam.argType.dataType.IsObject() &&
  7368. ((outParam.argType.dataType.GetBehaviour()->addref &&
  7369. outParam.argType.dataType.GetBehaviour()->release) ||
  7370. (outParam.argType.dataType.GetObjectType()->flags & asOBJ_NOCOUNT)) )
  7371. {
  7372. // Release the object handle that was taken to guarantee the reference
  7373. ReleaseTemporaryVariable(outParam.argType, &ctx->bc);
  7374. }
  7375. }
  7376. }
  7377. }
  7378. ctx->deferredParams.SetLength(0);
  7379. isProcessingDeferredParams = false;
  7380. }
  7381. void asCCompiler::CompileConstructCall(asCScriptNode *node, asSExprContext *ctx)
  7382. {
  7383. // The first node is a datatype node
  7384. asCString name;
  7385. asCTypeInfo tempObj;
  7386. bool onHeap = true;
  7387. asCArray<int> funcs;
  7388. // It is possible that the name is really a constructor
  7389. asCDataType dt;
  7390. dt = builder->CreateDataTypeFromNode(node->firstChild, script, outFunc->nameSpace);
  7391. if( dt.IsPrimitive() )
  7392. {
  7393. // This is a cast to a primitive type
  7394. CompileConversion(node, ctx);
  7395. return;
  7396. }
  7397. // Do not allow constructing non-shared types in shared functions
  7398. if( outFunc->IsShared() &&
  7399. dt.GetObjectType() && !dt.GetObjectType()->IsShared() )
  7400. {
  7401. asCString msg;
  7402. msg.Format(TXT_SHARED_CANNOT_USE_NON_SHARED_TYPE_s, dt.GetObjectType()->name.AddressOf());
  7403. Error(msg, node);
  7404. }
  7405. // Compile the arguments
  7406. asCArray<asSExprContext *> args;
  7407. asCArray<asCTypeInfo> temporaryVariables;
  7408. if( CompileArgumentList(node->lastChild, args) >= 0 )
  7409. {
  7410. // Check for a value cast behaviour
  7411. if( args.GetLength() == 1 && args[0]->type.dataType.GetObjectType() )
  7412. {
  7413. asSExprContext conv(engine);
  7414. conv.type = args[0]->type;
  7415. ImplicitConversion(&conv, dt, node->lastChild, asIC_EXPLICIT_VAL_CAST, false);
  7416. if( conv.type.dataType.IsEqualExceptRef(dt) )
  7417. {
  7418. ImplicitConversion(args[0], dt, node->lastChild, asIC_EXPLICIT_VAL_CAST);
  7419. ctx->bc.AddCode(&args[0]->bc);
  7420. ctx->type = args[0]->type;
  7421. asDELETE(args[0],asSExprContext);
  7422. return;
  7423. }
  7424. }
  7425. // Check for possible constructor/factory
  7426. name = dt.Format();
  7427. asSTypeBehaviour *beh = dt.GetBehaviour();
  7428. if( !(dt.GetObjectType()->flags & asOBJ_REF) )
  7429. {
  7430. funcs = beh->constructors;
  7431. // Value types and script types are allocated through the constructor
  7432. tempObj.dataType = dt;
  7433. tempObj.stackOffset = (short)AllocateVariable(dt, true);
  7434. tempObj.dataType.MakeReference(true);
  7435. tempObj.isTemporary = true;
  7436. tempObj.isVariable = true;
  7437. onHeap = IsVariableOnHeap(tempObj.stackOffset);
  7438. // Push the address of the object on the stack
  7439. if( onHeap )
  7440. ctx->bc.InstrSHORT(asBC_VAR, tempObj.stackOffset);
  7441. }
  7442. else
  7443. {
  7444. funcs = beh->factories;
  7445. }
  7446. // Special case: Allow calling func(void) with a void expression.
  7447. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) )
  7448. {
  7449. // Evaluate the expression before the function call
  7450. MergeExprBytecode(ctx, args[0]);
  7451. asDELETE(args[0],asSExprContext);
  7452. args.SetLength(0);
  7453. }
  7454. // Special case: If this is an object constructor and there are no arguments use the default constructor.
  7455. // If none has been registered, just allocate the variable and push it on the stack.
  7456. if( args.GetLength() == 0 )
  7457. {
  7458. asSTypeBehaviour *beh = tempObj.dataType.GetBehaviour();
  7459. if( beh && beh->construct == 0 && !(dt.GetObjectType()->flags & asOBJ_REF) )
  7460. {
  7461. // Call the default constructor
  7462. ctx->type = tempObj;
  7463. if( onHeap )
  7464. {
  7465. asASSERT(ctx->bc.GetLastInstr() == asBC_VAR);
  7466. ctx->bc.RemoveLastInstr();
  7467. }
  7468. CallDefaultConstructor(tempObj.dataType, tempObj.stackOffset, IsVariableOnHeap(tempObj.stackOffset), &ctx->bc, node);
  7469. // Push the reference on the stack
  7470. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  7471. return;
  7472. }
  7473. }
  7474. // Special case: If this is a construction of a delegate and the expression names an object method
  7475. if( dt.GetFuncDef() && args.GetLength() == 1 && args[0]->methodName != "" )
  7476. {
  7477. // TODO: delegate: It is possible that the argument returns a function pointer already, in which
  7478. // case no object delegate will be created, but instead a delegate for a function pointer
  7479. // In theory a simple cast would be good in this case, but this is a construct call so it
  7480. // is expected that a new object is created.
  7481. dt.MakeHandle(true);
  7482. ctx->type.Set(dt);
  7483. // The delegate must be able to hold on to a reference to the object
  7484. if( !args[0]->type.dataType.SupportHandles() )
  7485. Error(TXT_CANNOT_CREATE_DELEGATE_FOR_NOREF_TYPES, node);
  7486. else
  7487. {
  7488. // Filter the available object methods to find the one that matches the func def
  7489. asCObjectType *type = args[0]->type.dataType.GetObjectType();
  7490. asCScriptFunction *bestMethod = 0;
  7491. for( asUINT n = 0; n < type->methods.GetLength(); n++ )
  7492. {
  7493. asCScriptFunction *func = engine->scriptFunctions[type->methods[n]];
  7494. if( func->name != args[0]->methodName )
  7495. continue;
  7496. // If the expression is for a const object, then only const methods should be accepted
  7497. if( args[0]->type.dataType.IsReadOnly() && !func->IsReadOnly() )
  7498. continue;
  7499. if( func->IsSignatureExceptNameAndObjectTypeEqual(dt.GetFuncDef()) )
  7500. {
  7501. bestMethod = func;
  7502. // If the expression is non-const the non-const overloaded method has priority
  7503. if( args[0]->type.dataType.IsReadOnly() == func->IsReadOnly() )
  7504. break;
  7505. }
  7506. }
  7507. if( bestMethod )
  7508. {
  7509. // The object pointer is already on the stack
  7510. MergeExprBytecode(ctx, args[0]);
  7511. // Push the function pointer as an additional argument
  7512. ctx->bc.InstrPTR(asBC_FuncPtr, bestMethod);
  7513. // Call the factory function for the delegate
  7514. asCArray<int> funcs;
  7515. builder->GetFunctionDescriptions(DELEGATE_FACTORY, funcs, engine->nameSpaces[0]);
  7516. asASSERT( funcs.GetLength() == 1 );
  7517. ctx->bc.Call(asBC_CALLSYS , funcs[0], 2*AS_PTR_SIZE);
  7518. // Store the returned delegate in a temporary variable
  7519. int returnOffset = AllocateVariable(dt, true, false);
  7520. dt.MakeReference(true);
  7521. ctx->type.SetVariable(dt, returnOffset, true);
  7522. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  7523. // Push a reference to the temporary variable on the stack
  7524. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  7525. }
  7526. else
  7527. {
  7528. asCString msg;
  7529. msg.Format(TXT_NO_MATCHING_SIGNATURES_TO_s, dt.GetFuncDef()->GetDeclaration());
  7530. Error(msg.AddressOf(), node);
  7531. }
  7532. }
  7533. // Clean-up arg
  7534. asDELETE(args[0],asSExprContext);
  7535. return;
  7536. }
  7537. MatchFunctions(funcs, args, node, name.AddressOf(), NULL, false);
  7538. if( funcs.GetLength() != 1 )
  7539. {
  7540. // The error was reported by MatchFunctions()
  7541. // Dummy value
  7542. ctx->type.SetDummy();
  7543. }
  7544. else
  7545. {
  7546. int r = asSUCCESS;
  7547. // TODO: 2.28.1: Merge this with MakeFunctionCall
  7548. // Add the default values for arguments not explicitly supplied
  7549. asCScriptFunction *func = (funcs[0] & FUNC_IMPORTED) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  7550. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  7551. r = CompileDefaultArgs(node, args, func);
  7552. if( r == asSUCCESS )
  7553. {
  7554. asCByteCode objBC(engine);
  7555. PrepareFunctionCall(funcs[0], &ctx->bc, args);
  7556. MoveArgsToStack(funcs[0], &ctx->bc, args, false);
  7557. if( !(dt.GetObjectType()->flags & asOBJ_REF) )
  7558. {
  7559. // If the object is allocated on the stack, then call the constructor as a normal function
  7560. if( onHeap )
  7561. {
  7562. int offset = 0;
  7563. asCScriptFunction *descr = builder->GetFunctionDescription(funcs[0]);
  7564. for( asUINT n = 0; n < args.GetLength(); n++ )
  7565. offset += descr->parameterTypes[n].GetSizeOnStackDWords();
  7566. ctx->bc.InstrWORD(asBC_GETREF, (asWORD)offset);
  7567. }
  7568. else
  7569. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  7570. PerformFunctionCall(funcs[0], ctx, onHeap, &args, tempObj.dataType.GetObjectType());
  7571. // Add tag that the object has been initialized
  7572. ctx->bc.ObjInfo(tempObj.stackOffset, asOBJ_INIT);
  7573. // The constructor doesn't return anything,
  7574. // so we have to manually inform the type of
  7575. // the return value
  7576. ctx->type = tempObj;
  7577. if( !onHeap )
  7578. ctx->type.dataType.MakeReference(false);
  7579. // Push the address of the object on the stack again
  7580. ctx->bc.InstrSHORT(asBC_PSF, tempObj.stackOffset);
  7581. }
  7582. else
  7583. {
  7584. // Call the factory to create the reference type
  7585. PerformFunctionCall(funcs[0], ctx, false, &args);
  7586. }
  7587. }
  7588. }
  7589. }
  7590. else
  7591. {
  7592. // Failed to compile the argument list, set the result to the dummy type
  7593. ctx->type.SetDummy();
  7594. }
  7595. // Cleanup
  7596. for( asUINT n = 0; n < args.GetLength(); n++ )
  7597. if( args[n] )
  7598. {
  7599. asDELETE(args[n],asSExprContext);
  7600. }
  7601. }
  7602. int asCCompiler::CompileFunctionCall(asCScriptNode *node, asSExprContext *ctx, asCObjectType *objectType, bool objIsConst, const asCString &scope)
  7603. {
  7604. asCString name;
  7605. asCTypeInfo tempObj;
  7606. asCArray<int> funcs;
  7607. int localVar = -1;
  7608. bool initializeMembers = false;
  7609. asCScriptNode *nm = node->lastChild->prev;
  7610. name.Assign(&script->code[nm->tokenPos], nm->tokenLength);
  7611. // First check for a local variable of a function type as it would take precedence
  7612. // Must not allow function names, nor global variables to be returned in this instance
  7613. // If objectType is set then this is a post op expression and we shouldn't look for local variables
  7614. asSExprContext funcPtr(engine);
  7615. if( objectType == 0 )
  7616. {
  7617. localVar = CompileVariableAccess(name, scope, &funcPtr, node, true, true, true);
  7618. if( localVar >= 0 && !funcPtr.type.dataType.GetFuncDef() && funcPtr.methodName == "" )
  7619. {
  7620. // The variable is not a function
  7621. asCString msg;
  7622. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  7623. Error(msg, node);
  7624. return -1;
  7625. }
  7626. // If the name matches a method name, then reset the indicator that nothing was found
  7627. if( funcPtr.methodName != "" )
  7628. localVar = -1;
  7629. }
  7630. if( localVar < 0 )
  7631. {
  7632. // If this is an expression post op, or if a class method is
  7633. // being compiled, then we should look for matching class methods
  7634. if( objectType || (outFunc && outFunc->objectType && scope != "::") )
  7635. {
  7636. // If we're compiling a constructor and the name of the function is super then
  7637. // the constructor of the base class is being called.
  7638. // super cannot be prefixed with a scope operator
  7639. if( scope == "" && m_isConstructor && name == SUPER_TOKEN )
  7640. {
  7641. // If the class is not derived from anyone else, calling super should give an error
  7642. if( outFunc && outFunc->objectType->derivedFrom )
  7643. funcs = outFunc->objectType->derivedFrom->beh.constructors;
  7644. // Must not allow calling base class' constructor multiple times
  7645. if( continueLabels.GetLength() > 0 )
  7646. {
  7647. // If a continue label is set we are in a loop
  7648. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_LOOPS, node);
  7649. }
  7650. else if( breakLabels.GetLength() > 0 )
  7651. {
  7652. // TODO: inheritance: Should eventually allow constructors in switch statements
  7653. // If a break label is set we are either in a loop or a switch statements
  7654. Error(TXT_CANNOT_CALL_CONSTRUCTOR_IN_SWITCH, node);
  7655. }
  7656. else if( m_isConstructorCalled )
  7657. {
  7658. Error(TXT_CANNOT_CALL_CONSTRUCTOR_TWICE, node);
  7659. }
  7660. m_isConstructorCalled = true;
  7661. // We need to initialize the class members, but only after all the deferred arguments have been completed
  7662. initializeMembers = true;
  7663. }
  7664. else
  7665. {
  7666. // The scope is can be used to specify the base class
  7667. builder->GetObjectMethodDescriptions(name.AddressOf(), objectType ? objectType : outFunc->objectType, funcs, objIsConst, scope);
  7668. }
  7669. // It is still possible that there is a class member of a function type
  7670. if( funcs.GetLength() == 0 )
  7671. {
  7672. int r = CompileVariableAccess(name, scope, &funcPtr, node, true, true, true, objectType);
  7673. if( r >= 0 && !funcPtr.type.dataType.GetFuncDef() && funcPtr.methodName == "" )
  7674. {
  7675. // The variable is not a function
  7676. asCString msg;
  7677. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  7678. Error(msg, node);
  7679. return -1;
  7680. }
  7681. }
  7682. // If a class method is being called implicitly, then add the this pointer for the call
  7683. if( funcs.GetLength() && !objectType )
  7684. {
  7685. objectType = outFunc->objectType;
  7686. asCDataType dt = asCDataType::CreateObject(objectType, false);
  7687. // The object pointer is located at stack position 0
  7688. ctx->bc.InstrSHORT(asBC_PSF, 0);
  7689. ctx->type.SetVariable(dt, 0, false);
  7690. ctx->type.dataType.MakeReference(true);
  7691. Dereference(ctx, true);
  7692. }
  7693. }
  7694. // If it is not a class method or member function pointer,
  7695. // then look for global functions or global function pointers,
  7696. // unless this is an expression post op, incase only member
  7697. // functions are expected
  7698. if( objectType == 0 && funcs.GetLength() == 0 && funcPtr.type.dataType.GetFuncDef() == 0 )
  7699. {
  7700. // The scope is used to define the namespace
  7701. asSNameSpace *ns = DetermineNameSpace(scope);
  7702. if( ns )
  7703. {
  7704. // Search recursively in parent namespaces
  7705. while( ns && funcs.GetLength() == 0 && funcPtr.type.dataType.GetFuncDef() == 0 )
  7706. {
  7707. builder->GetFunctionDescriptions(name.AddressOf(), funcs, ns);
  7708. if( funcs.GetLength() == 0 )
  7709. {
  7710. int r = CompileVariableAccess(name, scope, &funcPtr, node, true, true);
  7711. if( r >= 0 && !funcPtr.type.dataType.GetFuncDef() )
  7712. {
  7713. // The variable is not a function
  7714. asCString msg;
  7715. msg.Format(TXT_NOT_A_FUNC_s_IS_VAR, name.AddressOf());
  7716. Error(msg, node);
  7717. return -1;
  7718. }
  7719. }
  7720. ns = builder->GetParentNameSpace(ns);
  7721. }
  7722. }
  7723. else
  7724. {
  7725. asCString msg;
  7726. msg.Format(TXT_NAMESPACE_s_DOESNT_EXIST, scope.AddressOf());
  7727. Error(msg, node);
  7728. return -1;
  7729. }
  7730. }
  7731. }
  7732. if( funcs.GetLength() == 0 && funcPtr.type.dataType.GetFuncDef() )
  7733. {
  7734. funcs.PushLast(funcPtr.type.dataType.GetFuncDef()->id);
  7735. }
  7736. // Compile the arguments
  7737. asCArray<asSExprContext *> args;
  7738. if( CompileArgumentList(node->lastChild, args) >= 0 )
  7739. {
  7740. // Special case: Allow calling func(void) with an expression that evaluates to no datatype, but isn't exactly 'void'
  7741. if( args.GetLength() == 1 && args[0]->type.dataType == asCDataType::CreatePrimitive(ttVoid, false) && !args[0]->type.IsVoidExpression() )
  7742. {
  7743. // Evaluate the expression before the function call
  7744. MergeExprBytecode(ctx, args[0]);
  7745. asDELETE(args[0],asSExprContext);
  7746. args.SetLength(0);
  7747. }
  7748. MatchFunctions(funcs, args, node, name.AddressOf(), objectType, objIsConst, false, true, scope);
  7749. if( funcs.GetLength() != 1 )
  7750. {
  7751. // The error was reported by MatchFunctions()
  7752. // Dummy value
  7753. ctx->type.SetDummy();
  7754. }
  7755. else
  7756. {
  7757. int r = asSUCCESS;
  7758. // Add the default values for arguments not explicitly supplied
  7759. asCScriptFunction *func = builder->GetFunctionDescription(funcs[0]);
  7760. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  7761. {
  7762. // Make sure to use the real function for virtual functions
  7763. asCScriptFunction *realFunc = func;
  7764. if( realFunc->funcType == asFUNC_VIRTUAL )
  7765. realFunc = objectType->virtualFunctionTable[realFunc->vfTableIdx];
  7766. r = CompileDefaultArgs(node, args, realFunc);
  7767. }
  7768. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  7769. // is it enough to make sure it is in a local variable?
  7770. // For function pointer we must guarantee that the function is safe, i.e.
  7771. // by first storing the function pointer in a local variable (if it isn't already in one)
  7772. if( r == asSUCCESS )
  7773. {
  7774. if( func->funcType == asFUNC_FUNCDEF )
  7775. {
  7776. if( objectType && funcPtr.property_get <= 0 )
  7777. {
  7778. Dereference(ctx, true); // Dereference the object pointer to access the member
  7779. // The actual function should be called as if a global function
  7780. objectType = 0;
  7781. }
  7782. if( funcPtr.property_get > 0 )
  7783. {
  7784. ProcessPropertyGetAccessor(&funcPtr, node);
  7785. Dereference(&funcPtr, true);
  7786. // The function call will be made directly from the local variable so the function pointer shouldn't be on the stack
  7787. funcPtr.bc.Instr(asBC_PopPtr);
  7788. }
  7789. else
  7790. {
  7791. Dereference(&funcPtr, true);
  7792. ConvertToVariable(&funcPtr);
  7793. // The function call will be made directly from the local variable so the function pointer shouldn't be on the stack
  7794. if( !funcPtr.type.isTemporary )
  7795. funcPtr.bc.Instr(asBC_PopPtr);
  7796. }
  7797. MergeExprBytecodeAndType(ctx, &funcPtr);
  7798. }
  7799. MakeFunctionCall(ctx, funcs[0], objectType, args, node, false, 0, funcPtr.type.stackOffset);
  7800. // If the function pointer was copied to a local variable for the call, then
  7801. // release it again (temporary local variable)
  7802. if( (funcs[0] & FUNC_IMPORTED) == 0 && engine->scriptFunctions[funcs[0]]->funcType == asFUNC_FUNCDEF )
  7803. {
  7804. ReleaseTemporaryVariable(funcPtr.type, &ctx->bc);
  7805. }
  7806. }
  7807. }
  7808. }
  7809. else
  7810. {
  7811. // Failed to compile the argument list, set the dummy type and continue compilation
  7812. ctx->type.SetDummy();
  7813. }
  7814. // Cleanup
  7815. for( asUINT n = 0; n < args.GetLength(); n++ )
  7816. if( args[n] )
  7817. {
  7818. asDELETE(args[n],asSExprContext);
  7819. }
  7820. if( initializeMembers )
  7821. {
  7822. asASSERT( m_isConstructor );
  7823. // Need to initialize members here, as they may use the properties of the base class
  7824. // If there are multiple paths that call super(), then there will also be multiple
  7825. // locations with initializations of the members. It is not possible to consolidate
  7826. // these in one place, as the expressions for the initialization are evaluated where
  7827. // they are compiled, which means that they may access different variables depending
  7828. // on the scope where super() is called.
  7829. // Members that don't have an explicit initialization expression will be initialized
  7830. // beginning of the constructor as they are guaranteed not to use at the any
  7831. // members of the base class.
  7832. CompileMemberInitialization(&ctx->bc, false);
  7833. }
  7834. return 0;
  7835. }
  7836. asSNameSpace *asCCompiler::DetermineNameSpace(const asCString &scope)
  7837. {
  7838. asSNameSpace *ns;
  7839. if( scope == "" )
  7840. {
  7841. if( outFunc->nameSpace->name != "" )
  7842. ns = outFunc->nameSpace;
  7843. else if( outFunc->objectType && outFunc->objectType->nameSpace->name != "" )
  7844. ns = outFunc->objectType->nameSpace;
  7845. else
  7846. ns = engine->nameSpaces[0];
  7847. }
  7848. else if( scope == "::" )
  7849. ns = engine->nameSpaces[0];
  7850. else
  7851. ns = engine->FindNameSpace(scope.AddressOf());
  7852. return ns;
  7853. }
  7854. int asCCompiler::CompileExpressionPreOp(asCScriptNode *node, asSExprContext *ctx)
  7855. {
  7856. int op = node->tokenType;
  7857. // Don't allow any prefix operators except handle on expressions that take address of class method
  7858. if( ctx->IsClassMethod() && op != ttHandle )
  7859. {
  7860. Error(TXT_INVALID_OP_ON_METHOD, node);
  7861. return -1;
  7862. }
  7863. // Don't allow any operators on void expressions
  7864. if( ctx->type.IsVoidExpression() )
  7865. {
  7866. Error(TXT_VOID_CANT_BE_OPERAND, node);
  7867. return -1;
  7868. }
  7869. IsVariableInitialized(&ctx->type, node);
  7870. if( op == ttHandle )
  7871. {
  7872. if( ctx->methodName != "" )
  7873. {
  7874. // Don't allow taking the handle of a handle
  7875. if( ctx->type.isExplicitHandle )
  7876. {
  7877. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  7878. return -1;
  7879. }
  7880. }
  7881. else
  7882. {
  7883. // Don't allow taking handle of a handle, i.e. @@
  7884. if( ctx->type.isExplicitHandle )
  7885. {
  7886. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  7887. return -1;
  7888. }
  7889. // @null is allowed even though it is implicit
  7890. if( !ctx->type.IsNullConstant() )
  7891. {
  7892. // Verify that the type allow its handle to be taken
  7893. if( !ctx->type.dataType.IsObject() ||
  7894. !(((ctx->type.dataType.GetObjectType()->beh.addref && ctx->type.dataType.GetObjectType()->beh.release) || (ctx->type.dataType.GetObjectType()->flags & asOBJ_NOCOUNT)) ||
  7895. (ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) )
  7896. {
  7897. Error(TXT_OBJECT_HANDLE_NOT_SUPPORTED, node);
  7898. return -1;
  7899. }
  7900. // Objects that are not local variables are not references
  7901. // Objects allocated on the stack are also not marked as references
  7902. if( !ctx->type.dataType.IsReference() &&
  7903. !(ctx->type.dataType.IsObject() && !ctx->type.isVariable) &&
  7904. !(ctx->type.isVariable && !IsVariableOnHeap(ctx->type.stackOffset)) )
  7905. {
  7906. Error(TXT_NOT_VALID_REFERENCE, node);
  7907. return -1;
  7908. }
  7909. // Convert the expression to a handle
  7910. if( !ctx->type.dataType.IsObjectHandle() && !(ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  7911. {
  7912. asCDataType to = ctx->type.dataType;
  7913. to.MakeHandle(true);
  7914. to.MakeReference(true);
  7915. to.MakeHandleToConst(ctx->type.dataType.IsReadOnly());
  7916. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV, true, false);
  7917. asASSERT( ctx->type.dataType.IsObjectHandle() );
  7918. }
  7919. else if( ctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE )
  7920. {
  7921. // For the ASHANDLE type we'll simply set the expression as a handle
  7922. ctx->type.dataType.MakeHandle(true);
  7923. }
  7924. }
  7925. }
  7926. // Mark the expression as an explicit handle to avoid implicit conversions to non-handle expressions
  7927. ctx->type.isExplicitHandle = true;
  7928. }
  7929. else if( (op == ttMinus || op == ttPlus || op == ttBitNot || op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  7930. {
  7931. // Look for the appropriate method
  7932. // There is no overloadable operator for unary plus
  7933. const char *opName = 0;
  7934. switch( op )
  7935. {
  7936. case ttMinus: opName = "opNeg"; break;
  7937. case ttBitNot: opName = "opCom"; break;
  7938. case ttInc: opName = "opPreInc"; break;
  7939. case ttDec: opName = "opPreDec"; break;
  7940. }
  7941. if( opName )
  7942. {
  7943. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  7944. ProcessPropertyGetAccessor(ctx, node);
  7945. // Is it a const value?
  7946. bool isConst = false;
  7947. if( ctx->type.dataType.IsObjectHandle() )
  7948. isConst = ctx->type.dataType.IsHandleToConst();
  7949. else
  7950. isConst = ctx->type.dataType.IsReadOnly();
  7951. // 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
  7952. // Find the correct method
  7953. asCArray<int> funcs;
  7954. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  7955. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  7956. {
  7957. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  7958. if( func->name == opName &&
  7959. func->parameterTypes.GetLength() == 0 &&
  7960. (!isConst || func->isReadOnly) )
  7961. {
  7962. funcs.PushLast(func->id);
  7963. }
  7964. }
  7965. // Did we find the method?
  7966. if( funcs.GetLength() == 1 )
  7967. {
  7968. asCTypeInfo objType = ctx->type;
  7969. asCArray<asSExprContext *> args;
  7970. MakeFunctionCall(ctx, funcs[0], objType.dataType.GetObjectType(), args, node);
  7971. ReleaseTemporaryVariable(objType, &ctx->bc);
  7972. return 0;
  7973. }
  7974. else if( funcs.GetLength() == 0 )
  7975. {
  7976. asCString str;
  7977. str = asCString(opName) + "()";
  7978. if( isConst )
  7979. str += " const";
  7980. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  7981. Error(str, node);
  7982. ctx->type.SetDummy();
  7983. return -1;
  7984. }
  7985. else if( funcs.GetLength() > 1 )
  7986. {
  7987. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  7988. PrintMatchingFuncs(funcs, node);
  7989. ctx->type.SetDummy();
  7990. return -1;
  7991. }
  7992. }
  7993. else if( op == ttPlus )
  7994. {
  7995. Error(TXT_ILLEGAL_OPERATION, node);
  7996. ctx->type.SetDummy();
  7997. return -1;
  7998. }
  7999. }
  8000. else if( op == ttPlus || op == ttMinus )
  8001. {
  8002. // This is only for primitives. Objects are treated in the above block
  8003. // Make sure the type is a math type
  8004. if( !(ctx->type.dataType.IsIntegerType() ||
  8005. ctx->type.dataType.IsUnsignedType() ||
  8006. ctx->type.dataType.IsFloatType() ||
  8007. ctx->type.dataType.IsDoubleType() ) )
  8008. {
  8009. Error(TXT_ILLEGAL_OPERATION, node);
  8010. return -1;
  8011. }
  8012. ProcessPropertyGetAccessor(ctx, node);
  8013. asCDataType to = ctx->type.dataType;
  8014. // TODO: The case -2147483648 gives an unecessary warning of changed sign for implicit conversion
  8015. if( ctx->type.dataType.IsUnsignedType() )
  8016. {
  8017. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  8018. to = asCDataType::CreatePrimitive(ttInt8, false);
  8019. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  8020. to = asCDataType::CreatePrimitive(ttInt16, false);
  8021. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  8022. to = asCDataType::CreatePrimitive(ttInt, false);
  8023. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  8024. to = asCDataType::CreatePrimitive(ttInt64, false);
  8025. else
  8026. {
  8027. Error(TXT_INVALID_TYPE, node);
  8028. return -1;
  8029. }
  8030. }
  8031. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  8032. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  8033. if( !ctx->type.isConstant )
  8034. {
  8035. ConvertToTempVariable(ctx);
  8036. asASSERT(!ctx->type.isLValue);
  8037. if( op == ttMinus )
  8038. {
  8039. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8040. ctx->bc.InstrSHORT(asBC_NEGi, ctx->type.stackOffset);
  8041. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8042. ctx->bc.InstrSHORT(asBC_NEGi64, ctx->type.stackOffset);
  8043. else if( ctx->type.dataType.IsFloatType() )
  8044. ctx->bc.InstrSHORT(asBC_NEGf, ctx->type.stackOffset);
  8045. else if( ctx->type.dataType.IsDoubleType() )
  8046. ctx->bc.InstrSHORT(asBC_NEGd, ctx->type.stackOffset);
  8047. else
  8048. {
  8049. Error(TXT_ILLEGAL_OPERATION, node);
  8050. return -1;
  8051. }
  8052. return 0;
  8053. }
  8054. }
  8055. else
  8056. {
  8057. if( op == ttMinus )
  8058. {
  8059. if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8060. ctx->type.intValue = -ctx->type.intValue;
  8061. else if( ctx->type.dataType.IsIntegerType() && ctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  8062. ctx->type.qwordValue = -(asINT64)ctx->type.qwordValue;
  8063. else if( ctx->type.dataType.IsFloatType() )
  8064. ctx->type.floatValue = -ctx->type.floatValue;
  8065. else if( ctx->type.dataType.IsDoubleType() )
  8066. ctx->type.doubleValue = -ctx->type.doubleValue;
  8067. else
  8068. {
  8069. Error(TXT_ILLEGAL_OPERATION, node);
  8070. return -1;
  8071. }
  8072. return 0;
  8073. }
  8074. }
  8075. }
  8076. else if( op == ttNot )
  8077. {
  8078. if( ctx->type.dataType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, true)) )
  8079. {
  8080. if( ctx->type.isConstant )
  8081. {
  8082. ctx->type.dwordValue = (ctx->type.dwordValue == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  8083. return 0;
  8084. }
  8085. ProcessPropertyGetAccessor(ctx, node);
  8086. ConvertToTempVariable(ctx);
  8087. asASSERT(!ctx->type.isLValue);
  8088. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  8089. }
  8090. else
  8091. {
  8092. Error(TXT_ILLEGAL_OPERATION, node);
  8093. return -1;
  8094. }
  8095. }
  8096. else if( op == ttBitNot )
  8097. {
  8098. ProcessPropertyGetAccessor(ctx, node);
  8099. asCDataType to = ctx->type.dataType;
  8100. if( ctx->type.dataType.IsIntegerType() )
  8101. {
  8102. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  8103. to = asCDataType::CreatePrimitive(ttUInt8, false);
  8104. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  8105. to = asCDataType::CreatePrimitive(ttUInt16, false);
  8106. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  8107. to = asCDataType::CreatePrimitive(ttUInt, false);
  8108. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 8 )
  8109. to = asCDataType::CreatePrimitive(ttUInt64, false);
  8110. else
  8111. {
  8112. Error(TXT_INVALID_TYPE, node);
  8113. return -1;
  8114. }
  8115. }
  8116. if( ctx->type.dataType.IsReference() ) ConvertToVariable(ctx);
  8117. ImplicitConversion(ctx, to, node, asIC_IMPLICIT_CONV);
  8118. if( ctx->type.dataType.IsUnsignedType() )
  8119. {
  8120. if( ctx->type.isConstant )
  8121. {
  8122. ctx->type.qwordValue = ~ctx->type.qwordValue;
  8123. return 0;
  8124. }
  8125. ConvertToTempVariable(ctx);
  8126. asASSERT(!ctx->type.isLValue);
  8127. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  8128. ctx->bc.InstrSHORT(asBC_BNOT, ctx->type.stackOffset);
  8129. else
  8130. ctx->bc.InstrSHORT(asBC_BNOT64, ctx->type.stackOffset);
  8131. }
  8132. else
  8133. {
  8134. Error(TXT_ILLEGAL_OPERATION, node);
  8135. return -1;
  8136. }
  8137. }
  8138. else if( op == ttInc || op == ttDec )
  8139. {
  8140. // Need a reference to the primitive that will be updated
  8141. // The result of this expression is the same reference as before
  8142. // Make sure the reference isn't a temporary variable
  8143. if( ctx->type.isTemporary )
  8144. {
  8145. Error(TXT_REF_IS_TEMP, node);
  8146. return -1;
  8147. }
  8148. if( ctx->type.dataType.IsReadOnly() )
  8149. {
  8150. Error(TXT_REF_IS_READ_ONLY, node);
  8151. return -1;
  8152. }
  8153. if( ctx->property_get || ctx->property_set )
  8154. {
  8155. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  8156. return -1;
  8157. }
  8158. if( !ctx->type.isLValue )
  8159. {
  8160. Error(TXT_NOT_LVALUE, node);
  8161. return -1;
  8162. }
  8163. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  8164. ConvertToReference(ctx);
  8165. else if( !ctx->type.dataType.IsReference() )
  8166. {
  8167. Error(TXT_NOT_VALID_REFERENCE, node);
  8168. return -1;
  8169. }
  8170. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  8171. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  8172. {
  8173. if( op == ttInc )
  8174. ctx->bc.Instr(asBC_INCi64);
  8175. else
  8176. ctx->bc.Instr(asBC_DECi64);
  8177. }
  8178. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt, false)) ||
  8179. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt, false)) )
  8180. {
  8181. if( op == ttInc )
  8182. ctx->bc.Instr(asBC_INCi);
  8183. else
  8184. ctx->bc.Instr(asBC_DECi);
  8185. }
  8186. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  8187. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  8188. {
  8189. if( op == ttInc )
  8190. ctx->bc.Instr(asBC_INCi16);
  8191. else
  8192. ctx->bc.Instr(asBC_DECi16);
  8193. }
  8194. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  8195. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  8196. {
  8197. if( op == ttInc )
  8198. ctx->bc.Instr(asBC_INCi8);
  8199. else
  8200. ctx->bc.Instr(asBC_DECi8);
  8201. }
  8202. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttFloat, false)) )
  8203. {
  8204. if( op == ttInc )
  8205. ctx->bc.Instr(asBC_INCf);
  8206. else
  8207. ctx->bc.Instr(asBC_DECf);
  8208. }
  8209. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttDouble, false)) )
  8210. {
  8211. if( op == ttInc )
  8212. ctx->bc.Instr(asBC_INCd);
  8213. else
  8214. ctx->bc.Instr(asBC_DECd);
  8215. }
  8216. else
  8217. {
  8218. Error(TXT_ILLEGAL_OPERATION, node);
  8219. return -1;
  8220. }
  8221. }
  8222. else
  8223. {
  8224. // Unknown operator
  8225. asASSERT(false);
  8226. return -1;
  8227. }
  8228. return 0;
  8229. }
  8230. void asCCompiler::ConvertToReference(asSExprContext *ctx)
  8231. {
  8232. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  8233. {
  8234. ctx->bc.InstrSHORT(asBC_LDV, ctx->type.stackOffset);
  8235. ctx->type.dataType.MakeReference(true);
  8236. ctx->type.SetVariable(ctx->type.dataType, ctx->type.stackOffset, ctx->type.isTemporary);
  8237. }
  8238. }
  8239. int asCCompiler::FindPropertyAccessor(const asCString &name, asSExprContext *ctx, asCScriptNode *node, asSNameSpace *ns, bool isThisAccess)
  8240. {
  8241. return FindPropertyAccessor(name, ctx, 0, node, ns, isThisAccess);
  8242. }
  8243. int asCCompiler::FindPropertyAccessor(const asCString &name, asSExprContext *ctx, asSExprContext *arg, asCScriptNode *node, asSNameSpace *ns, bool isThisAccess)
  8244. {
  8245. if( engine->ep.propertyAccessorMode == 0 )
  8246. {
  8247. // Property accessors have been disabled by the application
  8248. return 0;
  8249. }
  8250. int getId = 0, setId = 0;
  8251. asCString getName = "get_" + name;
  8252. asCString setName = "set_" + name;
  8253. asCArray<int> multipleGetFuncs, multipleSetFuncs;
  8254. if( ctx->type.dataType.IsObject() )
  8255. {
  8256. asASSERT( ns == 0 );
  8257. // Don't look for property accessors in script classes if the script
  8258. // property accessors have been disabled by the application
  8259. if( !(ctx->type.dataType.GetObjectType()->flags & asOBJ_SCRIPT_OBJECT) ||
  8260. engine->ep.propertyAccessorMode == 2 )
  8261. {
  8262. // Check if the object has any methods with the corresponding accessor name(s)
  8263. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  8264. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  8265. {
  8266. asCScriptFunction *f = engine->scriptFunctions[ot->methods[n]];
  8267. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  8268. if( f->name == getName && (int)f->parameterTypes.GetLength() == (arg?1:0) )
  8269. {
  8270. if( getId == 0 )
  8271. getId = ot->methods[n];
  8272. else
  8273. {
  8274. if( multipleGetFuncs.GetLength() == 0 )
  8275. multipleGetFuncs.PushLast(getId);
  8276. multipleGetFuncs.PushLast(ot->methods[n]);
  8277. }
  8278. }
  8279. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  8280. if( f->name == setName && (int)f->parameterTypes.GetLength() == (arg?2:1) )
  8281. {
  8282. if( setId == 0 )
  8283. setId = ot->methods[n];
  8284. else
  8285. {
  8286. if( multipleSetFuncs.GetLength() == 0 )
  8287. multipleSetFuncs.PushLast(setId);
  8288. multipleSetFuncs.PushLast(ot->methods[n]);
  8289. }
  8290. }
  8291. }
  8292. }
  8293. }
  8294. else
  8295. {
  8296. asASSERT( ns != 0 );
  8297. // Look for appropriate global functions.
  8298. asCArray<int> funcs;
  8299. asUINT n;
  8300. builder->GetFunctionDescriptions(getName.AddressOf(), funcs, ns);
  8301. for( n = 0; n < funcs.GetLength(); n++ )
  8302. {
  8303. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  8304. // TODO: The type of the parameter should match the argument (unless the arg is a dummy)
  8305. if( (int)f->parameterTypes.GetLength() == (arg?1:0) )
  8306. {
  8307. if( getId == 0 )
  8308. getId = funcs[n];
  8309. else
  8310. {
  8311. if( multipleGetFuncs.GetLength() == 0 )
  8312. multipleGetFuncs.PushLast(getId);
  8313. multipleGetFuncs.PushLast(funcs[n]);
  8314. }
  8315. }
  8316. }
  8317. funcs.SetLength(0);
  8318. builder->GetFunctionDescriptions(setName.AddressOf(), funcs, ns);
  8319. for( n = 0; n < funcs.GetLength(); n++ )
  8320. {
  8321. asCScriptFunction *f = builder->GetFunctionDescription(funcs[n]);
  8322. // TODO: getset: If the parameter is a reference, it must not be an out reference. Should we allow inout ref?
  8323. if( (int)f->parameterTypes.GetLength() == (arg?2:1) )
  8324. {
  8325. if( setId == 0 )
  8326. setId = funcs[n];
  8327. else
  8328. {
  8329. if( multipleSetFuncs.GetLength() == 0 )
  8330. multipleSetFuncs.PushLast(getId);
  8331. multipleSetFuncs.PushLast(funcs[n]);
  8332. }
  8333. }
  8334. }
  8335. }
  8336. bool isConst = false;
  8337. if( ctx->type.dataType.IsObjectHandle() )
  8338. isConst = ctx->type.dataType.IsHandleToConst();
  8339. else
  8340. isConst = ctx->type.dataType.IsReadOnly();
  8341. // Check for multiple matches
  8342. if( multipleGetFuncs.GetLength() > 0 )
  8343. {
  8344. // Filter the list by constness
  8345. FilterConst(multipleGetFuncs, !isConst);
  8346. if( multipleGetFuncs.GetLength() > 1 )
  8347. {
  8348. asCString str;
  8349. str.Format(TXT_MULTIPLE_PROP_GET_ACCESSOR_FOR_s, name.AddressOf());
  8350. Error(str, node);
  8351. PrintMatchingFuncs(multipleGetFuncs, node);
  8352. return -1;
  8353. }
  8354. else
  8355. {
  8356. // The id may have changed
  8357. getId = multipleGetFuncs[0];
  8358. }
  8359. }
  8360. if( multipleSetFuncs.GetLength() > 0 )
  8361. {
  8362. // Filter the list by constness
  8363. FilterConst(multipleSetFuncs, !isConst);
  8364. if( multipleSetFuncs.GetLength() > 1 )
  8365. {
  8366. asCString str;
  8367. str.Format(TXT_MULTIPLE_PROP_SET_ACCESSOR_FOR_s, name.AddressOf());
  8368. Error(str, node);
  8369. PrintMatchingFuncs(multipleSetFuncs, node);
  8370. return -1;
  8371. }
  8372. else
  8373. {
  8374. // The id may have changed
  8375. setId = multipleSetFuncs[0];
  8376. }
  8377. }
  8378. // Check for type compatibility between get and set accessor
  8379. if( getId && setId )
  8380. {
  8381. asCScriptFunction *getFunc = builder->GetFunctionDescription(getId);
  8382. asCScriptFunction *setFunc = builder->GetFunctionDescription(setId);
  8383. // It is permitted for a getter to return a handle and the setter to take a reference
  8384. int idx = (arg?1:0);
  8385. if( !getFunc->returnType.IsEqualExceptRefAndConst(setFunc->parameterTypes[idx]) &&
  8386. !((getFunc->returnType.IsObjectHandle() && !setFunc->parameterTypes[idx].IsObjectHandle()) &&
  8387. (getFunc->returnType.GetObjectType() == setFunc->parameterTypes[idx].GetObjectType())) )
  8388. {
  8389. asCString str;
  8390. str.Format(TXT_GET_SET_ACCESSOR_TYPE_MISMATCH_FOR_s, name.AddressOf());
  8391. Error(str, node);
  8392. asCArray<int> funcs;
  8393. funcs.PushLast(getId);
  8394. funcs.PushLast(setId);
  8395. PrintMatchingFuncs(funcs, node);
  8396. return -1;
  8397. }
  8398. }
  8399. // Check if we are within one of the accessors
  8400. int realGetId = getId;
  8401. int realSetId = setId;
  8402. if( outFunc->objectType && isThisAccess )
  8403. {
  8404. // The property accessors would be virtual functions, so we need to find the real implementation
  8405. asCScriptFunction *getFunc = getId ? builder->GetFunctionDescription(getId) : 0;
  8406. if( getFunc &&
  8407. getFunc->funcType == asFUNC_VIRTUAL &&
  8408. outFunc->objectType->DerivesFrom(getFunc->objectType) )
  8409. realGetId = outFunc->objectType->virtualFunctionTable[getFunc->vfTableIdx]->id;
  8410. asCScriptFunction *setFunc = setId ? builder->GetFunctionDescription(setId) : 0;
  8411. if( setFunc &&
  8412. setFunc->funcType == asFUNC_VIRTUAL &&
  8413. outFunc->objectType->DerivesFrom(setFunc->objectType) )
  8414. realSetId = outFunc->objectType->virtualFunctionTable[setFunc->vfTableIdx]->id;
  8415. }
  8416. // Avoid recursive call, by not treating this as a property accessor call.
  8417. // This will also allow having the real property with the same name as the accessors.
  8418. if( (isThisAccess || outFunc->objectType == 0) &&
  8419. ((realGetId && realGetId == outFunc->id) ||
  8420. (realSetId && realSetId == outFunc->id)) )
  8421. {
  8422. getId = 0;
  8423. setId = 0;
  8424. }
  8425. // Check if the application has disabled script written property accessors
  8426. if( engine->ep.propertyAccessorMode == 1 )
  8427. {
  8428. if( getId && builder->GetFunctionDescription(getId)->funcType != asFUNC_SYSTEM )
  8429. getId = 0;
  8430. if( setId && builder->GetFunctionDescription(setId)->funcType != asFUNC_SYSTEM )
  8431. setId = 0;
  8432. }
  8433. if( getId || setId )
  8434. {
  8435. // Property accessors were found, but we don't know which is to be used yet, so
  8436. // we just prepare the bytecode for the method call, and then store the function ids
  8437. // so that the right one can be used when we get there.
  8438. ctx->property_get = getId;
  8439. ctx->property_set = setId;
  8440. if( ctx->type.dataType.IsObject() )
  8441. {
  8442. // If the object is read-only then we need to remember that
  8443. if( (!ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsReadOnly()) ||
  8444. (ctx->type.dataType.IsObjectHandle() && ctx->type.dataType.IsHandleToConst()) )
  8445. ctx->property_const = true;
  8446. else
  8447. ctx->property_const = false;
  8448. // If the object is a handle then we need to remember that
  8449. ctx->property_handle = ctx->type.dataType.IsObjectHandle();
  8450. ctx->property_ref = ctx->type.dataType.IsReference();
  8451. }
  8452. // The setter's parameter type is used as the property type,
  8453. // unless only the getter is available
  8454. asCDataType dt;
  8455. if( setId )
  8456. dt = builder->GetFunctionDescription(setId)->parameterTypes[(arg?1:0)];
  8457. else
  8458. dt = builder->GetFunctionDescription(getId)->returnType;
  8459. // Just change the type, the context must still maintain information
  8460. // about previous variable offset and the indicator of temporary variable.
  8461. int offset = ctx->type.stackOffset;
  8462. bool isTemp = ctx->type.isTemporary;
  8463. ctx->type.Set(dt);
  8464. ctx->type.stackOffset = (short)offset;
  8465. ctx->type.isTemporary = isTemp;
  8466. ctx->exprNode = node;
  8467. // Store the argument for later use
  8468. if( arg )
  8469. {
  8470. ctx->property_arg = asNEW(asSExprContext)(engine);
  8471. if( ctx->property_arg == 0 )
  8472. {
  8473. // Out of memory
  8474. return -1;
  8475. }
  8476. MergeExprBytecodeAndType(ctx->property_arg, arg);
  8477. }
  8478. return 1;
  8479. }
  8480. // No accessor was found
  8481. return 0;
  8482. }
  8483. int asCCompiler::ProcessPropertySetAccessor(asSExprContext *ctx, asSExprContext *arg, asCScriptNode *node)
  8484. {
  8485. // TODO: A lot of this code is similar to ProcessPropertyGetAccessor. Can we unify them?
  8486. if( !ctx->property_set )
  8487. {
  8488. Error(TXT_PROPERTY_HAS_NO_SET_ACCESSOR, node);
  8489. return -1;
  8490. }
  8491. asCTypeInfo objType = ctx->type;
  8492. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_set);
  8493. // Make sure the arg match the property
  8494. asCArray<int> funcs;
  8495. funcs.PushLast(ctx->property_set);
  8496. asCArray<asSExprContext *> args;
  8497. if( ctx->property_arg )
  8498. args.PushLast(ctx->property_arg);
  8499. args.PushLast(arg);
  8500. MatchFunctions(funcs, args, node, func->GetName(), func->objectType, ctx->property_const);
  8501. if( funcs.GetLength() == 0 )
  8502. {
  8503. // MatchFunctions already reported the error
  8504. if( ctx->property_arg )
  8505. {
  8506. asDELETE(ctx->property_arg, asSExprContext);
  8507. ctx->property_arg = 0;
  8508. }
  8509. return -1;
  8510. }
  8511. if( func->objectType )
  8512. {
  8513. // Setup the context with the original type so the method call gets built correctly
  8514. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  8515. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  8516. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  8517. // Don't allow the call if the object is read-only and the property accessor is not const
  8518. if( ctx->property_const && !func->isReadOnly )
  8519. {
  8520. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  8521. asCArray<int> funcs;
  8522. funcs.PushLast(ctx->property_set);
  8523. PrintMatchingFuncs(funcs, node);
  8524. }
  8525. }
  8526. // Call the accessor
  8527. MakeFunctionCall(ctx, ctx->property_set, func->objectType, args, node);
  8528. if( func->objectType )
  8529. {
  8530. // TODO: This is from CompileExpressionPostOp, can we unify the code?
  8531. if( !objType.isTemporary ||
  8532. !ctx->type.dataType.IsReference() ||
  8533. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  8534. {
  8535. // As the method didn't return a reference to a member
  8536. // we can safely release the original object now
  8537. ReleaseTemporaryVariable(objType, &ctx->bc);
  8538. }
  8539. }
  8540. ctx->property_get = 0;
  8541. ctx->property_set = 0;
  8542. if( ctx->property_arg )
  8543. {
  8544. asDELETE(ctx->property_arg, asSExprContext);
  8545. ctx->property_arg = 0;
  8546. }
  8547. return 0;
  8548. }
  8549. void asCCompiler::ProcessPropertyGetAccessor(asSExprContext *ctx, asCScriptNode *node)
  8550. {
  8551. // If no property accessor has been prepared then don't do anything
  8552. if( !ctx->property_get && !ctx->property_set )
  8553. return;
  8554. if( !ctx->property_get )
  8555. {
  8556. // Raise error on missing accessor
  8557. Error(TXT_PROPERTY_HAS_NO_GET_ACCESSOR, node);
  8558. ctx->type.SetDummy();
  8559. return;
  8560. }
  8561. asCTypeInfo objType = ctx->type;
  8562. asCScriptFunction *func = builder->GetFunctionDescription(ctx->property_get);
  8563. // Make sure the arg match the property
  8564. asCArray<int> funcs;
  8565. funcs.PushLast(ctx->property_get);
  8566. asCArray<asSExprContext *> args;
  8567. if( ctx->property_arg )
  8568. args.PushLast(ctx->property_arg);
  8569. MatchFunctions(funcs, args, node, func->GetName(), func->objectType, ctx->property_const);
  8570. if( funcs.GetLength() == 0 )
  8571. {
  8572. // MatchFunctions already reported the error
  8573. if( ctx->property_arg )
  8574. {
  8575. asDELETE(ctx->property_arg, asSExprContext);
  8576. ctx->property_arg = 0;
  8577. }
  8578. ctx->type.SetDummy();
  8579. return;
  8580. }
  8581. if( func->objectType )
  8582. {
  8583. // Setup the context with the original type so the method call gets built correctly
  8584. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  8585. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  8586. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  8587. // Don't allow the call if the object is read-only and the property accessor is not const
  8588. if( ctx->property_const && !func->isReadOnly )
  8589. {
  8590. Error(TXT_NON_CONST_METHOD_ON_CONST_OBJ, node);
  8591. asCArray<int> funcs;
  8592. funcs.PushLast(ctx->property_get);
  8593. PrintMatchingFuncs(funcs, node);
  8594. }
  8595. }
  8596. // Call the accessor
  8597. MakeFunctionCall(ctx, ctx->property_get, func->objectType, args, node);
  8598. if( func->objectType )
  8599. {
  8600. // TODO: This is from CompileExpressionPostOp, can we unify the code?
  8601. // If the method returned a reference, then we can't release the original
  8602. // object yet, because the reference may be to a member of it
  8603. if( !objType.isTemporary ||
  8604. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  8605. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  8606. {
  8607. // As the method didn't return a reference to a member
  8608. // we can safely release the original object now
  8609. ReleaseTemporaryVariable(objType, &ctx->bc);
  8610. }
  8611. }
  8612. ctx->property_get = 0;
  8613. ctx->property_set = 0;
  8614. if( ctx->property_arg )
  8615. {
  8616. asDELETE(ctx->property_arg, asSExprContext);
  8617. ctx->property_arg = 0;
  8618. }
  8619. }
  8620. int asCCompiler::CompileExpressionPostOp(asCScriptNode *node, asSExprContext *ctx)
  8621. {
  8622. // Don't allow any postfix operators on expressions that take address of class method
  8623. if( ctx->IsClassMethod() )
  8624. {
  8625. Error(TXT_INVALID_OP_ON_METHOD, node);
  8626. return -1;
  8627. }
  8628. // Don't allow any operators on void expressions
  8629. if( ctx->type.IsVoidExpression() )
  8630. {
  8631. Error(TXT_VOID_CANT_BE_OPERAND, node);
  8632. return -1;
  8633. }
  8634. // Check if the variable is initialized (if it indeed is a variable)
  8635. IsVariableInitialized(&ctx->type, node);
  8636. int op = node->tokenType;
  8637. if( (op == ttInc || op == ttDec) && ctx->type.dataType.IsObject() )
  8638. {
  8639. const char *opName = 0;
  8640. switch( op )
  8641. {
  8642. case ttInc: opName = "opPostInc"; break;
  8643. case ttDec: opName = "opPostDec"; break;
  8644. }
  8645. if( opName )
  8646. {
  8647. // TODO: Should convert this to something similar to CompileOverloadedDualOperator2
  8648. ProcessPropertyGetAccessor(ctx, node);
  8649. // Is it a const value?
  8650. bool isConst = false;
  8651. if( ctx->type.dataType.IsObjectHandle() )
  8652. isConst = ctx->type.dataType.IsHandleToConst();
  8653. else
  8654. isConst = ctx->type.dataType.IsReadOnly();
  8655. // 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
  8656. // Find the correct method
  8657. asCArray<int> funcs;
  8658. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  8659. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  8660. {
  8661. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  8662. if( func->name == opName &&
  8663. func->parameterTypes.GetLength() == 0 &&
  8664. (!isConst || func->isReadOnly) )
  8665. {
  8666. funcs.PushLast(func->id);
  8667. }
  8668. }
  8669. // Did we find the method?
  8670. if( funcs.GetLength() == 1 )
  8671. {
  8672. asCTypeInfo objType = ctx->type;
  8673. asCArray<asSExprContext *> args;
  8674. MakeFunctionCall(ctx, funcs[0], objType.dataType.GetObjectType(), args, node);
  8675. ReleaseTemporaryVariable(objType, &ctx->bc);
  8676. return 0;
  8677. }
  8678. else if( funcs.GetLength() == 0 )
  8679. {
  8680. asCString str;
  8681. str = asCString(opName) + "()";
  8682. if( isConst )
  8683. str += " const";
  8684. str.Format(TXT_FUNCTION_s_NOT_FOUND, str.AddressOf());
  8685. Error(str, node);
  8686. ctx->type.SetDummy();
  8687. return -1;
  8688. }
  8689. else if( funcs.GetLength() > 1 )
  8690. {
  8691. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  8692. PrintMatchingFuncs(funcs, node);
  8693. ctx->type.SetDummy();
  8694. return -1;
  8695. }
  8696. }
  8697. }
  8698. else if( op == ttInc || op == ttDec )
  8699. {
  8700. // Make sure the reference isn't a temporary variable
  8701. if( ctx->type.isTemporary )
  8702. {
  8703. Error(TXT_REF_IS_TEMP, node);
  8704. return -1;
  8705. }
  8706. if( ctx->type.dataType.IsReadOnly() )
  8707. {
  8708. Error(TXT_REF_IS_READ_ONLY, node);
  8709. return -1;
  8710. }
  8711. if( ctx->property_get || ctx->property_set )
  8712. {
  8713. Error(TXT_INVALID_REF_PROP_ACCESS, node);
  8714. return -1;
  8715. }
  8716. if( !ctx->type.isLValue )
  8717. {
  8718. Error(TXT_NOT_LVALUE, node);
  8719. return -1;
  8720. }
  8721. if( ctx->type.isVariable && !ctx->type.dataType.IsReference() )
  8722. ConvertToReference(ctx);
  8723. else if( !ctx->type.dataType.IsReference() )
  8724. {
  8725. Error(TXT_NOT_VALID_REFERENCE, node);
  8726. return -1;
  8727. }
  8728. // Copy the value to a temp before changing it
  8729. ConvertToTempVariable(ctx);
  8730. asASSERT(!ctx->type.isLValue);
  8731. // Increment the value pointed to by the reference still in the register
  8732. asEBCInstr iInc = asBC_INCi, iDec = asBC_DECi;
  8733. if( ctx->type.dataType.IsDoubleType() )
  8734. {
  8735. iInc = asBC_INCd;
  8736. iDec = asBC_DECd;
  8737. }
  8738. else if( ctx->type.dataType.IsFloatType() )
  8739. {
  8740. iInc = asBC_INCf;
  8741. iDec = asBC_DECf;
  8742. }
  8743. else if( ctx->type.dataType.IsIntegerType() || ctx->type.dataType.IsUnsignedType() )
  8744. {
  8745. if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt16, false)) ||
  8746. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt16, false)) )
  8747. {
  8748. iInc = asBC_INCi16;
  8749. iDec = asBC_DECi16;
  8750. }
  8751. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt8, false)) ||
  8752. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt8, false)) )
  8753. {
  8754. iInc = asBC_INCi8;
  8755. iDec = asBC_DECi8;
  8756. }
  8757. else if( ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttInt64, false)) ||
  8758. ctx->type.dataType.IsEqualExceptRef(asCDataType::CreatePrimitive(ttUInt64, false)) )
  8759. {
  8760. iInc = asBC_INCi64;
  8761. iDec = asBC_DECi64;
  8762. }
  8763. }
  8764. else
  8765. {
  8766. Error(TXT_ILLEGAL_OPERATION, node);
  8767. return -1;
  8768. }
  8769. if( op == ttInc ) ctx->bc.Instr(iInc); else ctx->bc.Instr(iDec);
  8770. }
  8771. else if( op == ttDot )
  8772. {
  8773. if( node->firstChild->nodeType == snIdentifier )
  8774. {
  8775. ProcessPropertyGetAccessor(ctx, node);
  8776. // Get the property name
  8777. asCString name(&script->code[node->firstChild->tokenPos], node->firstChild->tokenLength);
  8778. if( ctx->type.dataType.IsObject() )
  8779. {
  8780. // We need to look for get/set property accessors.
  8781. // If found, the context stores information on the get/set accessors
  8782. // until it is known which is to be used.
  8783. int r = 0;
  8784. if( node->next && node->next->tokenType == ttOpenBracket )
  8785. {
  8786. // The property accessor should take an index arg
  8787. asSExprContext dummyArg(engine);
  8788. r = FindPropertyAccessor(name, ctx, &dummyArg, node, 0);
  8789. }
  8790. if( r == 0 )
  8791. r = FindPropertyAccessor(name, ctx, node, 0);
  8792. if( r != 0 )
  8793. return r;
  8794. if( !ctx->type.dataType.IsPrimitive() )
  8795. Dereference(ctx, true);
  8796. if( ctx->type.dataType.IsObjectHandle() )
  8797. {
  8798. // Convert the handle to a normal object
  8799. asCDataType dt = ctx->type.dataType;
  8800. dt.MakeHandle(false);
  8801. ImplicitConversion(ctx, dt, node, asIC_IMPLICIT_CONV);
  8802. // The handle may not have been an lvalue, but the dereferenced object is
  8803. ctx->type.isLValue = true;
  8804. }
  8805. bool isConst = ctx->type.dataType.IsReadOnly();
  8806. asCObjectProperty *prop = builder->GetObjectProperty(ctx->type.dataType, name.AddressOf());
  8807. if( prop )
  8808. {
  8809. // Is the property access allowed?
  8810. if( prop->isPrivate && (!outFunc || outFunc->objectType != ctx->type.dataType.GetObjectType()) )
  8811. {
  8812. asCString msg;
  8813. msg.Format(TXT_PRIVATE_PROP_ACCESS_s, name.AddressOf());
  8814. Error(msg, node);
  8815. }
  8816. // Put the offset on the stack
  8817. ctx->bc.InstrSHORT_DW(asBC_ADDSi, (short)prop->byteOffset, engine->GetTypeIdFromDataType(asCDataType::CreateObject(ctx->type.dataType.GetObjectType(), false)));
  8818. if( prop->type.IsReference() )
  8819. ctx->bc.Instr(asBC_RDSPtr);
  8820. // Reference to primitive must be stored in the temp register
  8821. if( prop->type.IsPrimitive() )
  8822. {
  8823. ctx->bc.Instr(asBC_PopRPtr);
  8824. }
  8825. // Keep information about temporary variables as deferred expression
  8826. if( ctx->type.isTemporary )
  8827. {
  8828. // Add the release of this reference, as a deferred expression
  8829. asSDeferredParam deferred;
  8830. deferred.origExpr = 0;
  8831. deferred.argInOutFlags = asTM_INREF;
  8832. deferred.argNode = 0;
  8833. deferred.argType.SetVariable(ctx->type.dataType, ctx->type.stackOffset, true);
  8834. ctx->deferredParams.PushLast(deferred);
  8835. }
  8836. // Set the new type and make sure it is not treated as a variable anymore
  8837. ctx->type.dataType = prop->type;
  8838. ctx->type.dataType.MakeReference(true);
  8839. ctx->type.isVariable = false;
  8840. ctx->type.isTemporary = false;
  8841. if( ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle() )
  8842. {
  8843. // Objects that are members are not references
  8844. ctx->type.dataType.MakeReference(false);
  8845. }
  8846. ctx->type.dataType.MakeReadOnly(isConst ? true : prop->type.IsReadOnly());
  8847. }
  8848. else
  8849. {
  8850. // If the name is not a property, the compiler must check if the name matches
  8851. // a method, which can be used for constructing delegates
  8852. asIScriptFunction *func = 0;
  8853. asCObjectType *ot = ctx->type.dataType.GetObjectType();
  8854. for( asUINT n = 0; n < ot->methods.GetLength(); n++ )
  8855. {
  8856. if( engine->scriptFunctions[ot->methods[n]]->name == name )
  8857. {
  8858. func = engine->scriptFunctions[ot->methods[n]];
  8859. break;
  8860. }
  8861. }
  8862. if( func )
  8863. {
  8864. // An object method was found. Keep the name of the method in the expression, but
  8865. // don't actually modify the bytecode at this point since it is not yet known what
  8866. // the method will be used for, or even what overloaded method should be used.
  8867. ctx->methodName = name;
  8868. }
  8869. else
  8870. {
  8871. asCString str;
  8872. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format().AddressOf());
  8873. Error(str, node);
  8874. return -1;
  8875. }
  8876. }
  8877. }
  8878. else
  8879. {
  8880. asCString str;
  8881. str.Format(TXT_s_NOT_MEMBER_OF_s, name.AddressOf(), ctx->type.dataType.Format().AddressOf());
  8882. Error(str, node);
  8883. return -1;
  8884. }
  8885. }
  8886. else
  8887. {
  8888. // Make sure it is an object we are accessing
  8889. if( !ctx->type.dataType.IsObject() )
  8890. {
  8891. asCString str;
  8892. str.Format(TXT_ILLEGAL_OPERATION_ON_s, ctx->type.dataType.Format().AddressOf());
  8893. Error(str, node);
  8894. return -1;
  8895. }
  8896. // Process the get property accessor
  8897. ProcessPropertyGetAccessor(ctx, node);
  8898. bool isConst = false;
  8899. if( ctx->type.dataType.IsObjectHandle() )
  8900. isConst = ctx->type.dataType.IsHandleToConst();
  8901. else
  8902. isConst = ctx->type.dataType.IsReadOnly();
  8903. asCObjectType *trueObj = ctx->type.dataType.GetObjectType();
  8904. asCTypeInfo objType = ctx->type;
  8905. // Compile function call
  8906. int r = CompileFunctionCall(node->firstChild, ctx, trueObj, isConst);
  8907. if( r < 0 ) return r;
  8908. // If the method returned a reference, then we can't release the original
  8909. // object yet, because the reference may be to a member of it
  8910. if( !objType.isTemporary ||
  8911. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  8912. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not a member
  8913. {
  8914. // As the method didn't return a reference to a member
  8915. // we can safely release the original object now
  8916. ReleaseTemporaryVariable(objType, &ctx->bc);
  8917. }
  8918. }
  8919. }
  8920. else if( op == ttOpenBracket )
  8921. {
  8922. // If the property access takes an index arg and the argument hasn't been evaluated yet,
  8923. // then we should use that instead of processing it now. If the argument has already been
  8924. // evaluated, then we should process the property accessor as a get access now as the new
  8925. // index operator is on the result of that accessor.
  8926. asCString propertyName;
  8927. asSNameSpace *ns = 0;
  8928. if( ((ctx->property_get && builder->GetFunctionDescription(ctx->property_get)->GetParamCount() == 1) ||
  8929. (ctx->property_set && builder->GetFunctionDescription(ctx->property_set)->GetParamCount() == 2)) &&
  8930. (ctx->property_arg && ctx->property_arg->type.dataType.GetTokenType() == ttUnrecognizedToken) )
  8931. {
  8932. // Determine the name of the property accessor
  8933. asCScriptFunction *func = 0;
  8934. if( ctx->property_get )
  8935. func = builder->GetFunctionDescription(ctx->property_get);
  8936. else
  8937. func = builder->GetFunctionDescription(ctx->property_set);
  8938. propertyName = func->GetName();
  8939. propertyName = propertyName.SubString(4);
  8940. // Set the original type of the expression so we can re-evaluate the property accessor
  8941. if( func->objectType )
  8942. {
  8943. ctx->type.dataType = asCDataType::CreateObject(func->objectType, ctx->property_const);
  8944. if( ctx->property_handle ) ctx->type.dataType.MakeHandle(true);
  8945. if( ctx->property_ref ) ctx->type.dataType.MakeReference(true);
  8946. }
  8947. else
  8948. {
  8949. // Store the namespace where the function is declared
  8950. // so the same function can be found later
  8951. ctx->type.SetDummy();
  8952. ns = func->nameSpace;
  8953. }
  8954. ctx->property_get = ctx->property_set = 0;
  8955. if( ctx->property_arg )
  8956. {
  8957. asDELETE(ctx->property_arg, asSExprContext);
  8958. ctx->property_arg = 0;
  8959. }
  8960. }
  8961. else
  8962. {
  8963. if( !ctx->type.dataType.IsObject() )
  8964. {
  8965. asCString str;
  8966. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format().AddressOf());
  8967. Error(str, node);
  8968. return -1;
  8969. }
  8970. ProcessPropertyGetAccessor(ctx, node);
  8971. }
  8972. Dereference(ctx, true);
  8973. // Compile the expression
  8974. asSExprContext expr(engine);
  8975. CompileAssignment(node->firstChild, &expr);
  8976. // Check for the existence of the opIndex method
  8977. asSExprContext lctx(engine);
  8978. MergeExprBytecodeAndType(&lctx, ctx);
  8979. int r = 0;
  8980. if( propertyName == "" )
  8981. r = CompileOverloadedDualOperator2(node, "opIndex", &lctx, &expr, ctx);
  8982. if( r == 0 )
  8983. {
  8984. // Check for accessors methods for the opIndex
  8985. r = FindPropertyAccessor(propertyName == "" ? "opIndex" : propertyName.AddressOf(), &lctx, &expr, node, ns);
  8986. if( r == 0 )
  8987. {
  8988. asCString str;
  8989. str.Format(TXT_OBJECT_DOESNT_SUPPORT_INDEX_OP, ctx->type.dataType.Format().AddressOf());
  8990. Error(str, node);
  8991. return -1;
  8992. }
  8993. else if( r < 0 )
  8994. return -1;
  8995. MergeExprBytecodeAndType(ctx, &lctx);
  8996. }
  8997. }
  8998. else if( op == ttOpenParanthesis )
  8999. {
  9000. // TODO: Most of this is already done by CompileFunctionCall(). Can we share the code?
  9001. // Make sure the expression is a funcdef
  9002. if( !ctx->type.dataType.GetFuncDef() )
  9003. {
  9004. Error(TXT_EXPR_DOESNT_EVAL_TO_FUNC, node);
  9005. return -1;
  9006. }
  9007. // Compile arguments
  9008. asCArray<asSExprContext *> args;
  9009. if( CompileArgumentList(node->lastChild, args) >= 0 )
  9010. {
  9011. // Match arguments with the funcdef
  9012. asCArray<int> funcs;
  9013. funcs.PushLast(ctx->type.dataType.GetFuncDef()->id);
  9014. MatchFunctions(funcs, args, node, ctx->type.dataType.GetFuncDef()->name.AddressOf());
  9015. if( funcs.GetLength() != 1 )
  9016. {
  9017. // The error was reported by MatchFunctions()
  9018. // Dummy value
  9019. ctx->type.SetDummy();
  9020. }
  9021. else
  9022. {
  9023. int r = asSUCCESS;
  9024. // Add the default values for arguments not explicitly supplied
  9025. asCScriptFunction *func = (funcs[0] & FUNC_IMPORTED) == 0 ? engine->scriptFunctions[funcs[0]] : 0;
  9026. if( func && args.GetLength() < (asUINT)func->GetParamCount() )
  9027. r = CompileDefaultArgs(node, args, func);
  9028. // TODO: funcdef: Do we have to make sure the handle is stored in a temporary variable, or
  9029. // is it enough to make sure it is in a local variable?
  9030. // For function pointer we must guarantee that the function is safe, i.e.
  9031. // by first storing the function pointer in a local variable (if it isn't already in one)
  9032. if( r == asSUCCESS )
  9033. {
  9034. Dereference(ctx, true);
  9035. if( !ctx->type.isVariable )
  9036. ConvertToVariable(ctx);
  9037. else
  9038. {
  9039. // Remove the reference from the stack as the asBC_CALLPTR instruction takes the variable as argument
  9040. ctx->bc.Instr(asBC_PopPtr);
  9041. }
  9042. asCTypeInfo t = ctx->type;
  9043. MakeFunctionCall(ctx, funcs[0], 0, args, node, false, 0, ctx->type.stackOffset);
  9044. ReleaseTemporaryVariable(t, &ctx->bc);
  9045. }
  9046. }
  9047. }
  9048. else
  9049. ctx->type.SetDummy();
  9050. // Cleanup
  9051. for( asUINT n = 0; n < args.GetLength(); n++ )
  9052. if( args[n] )
  9053. {
  9054. asDELETE(args[n],asSExprContext);
  9055. }
  9056. }
  9057. return 0;
  9058. }
  9059. int asCCompiler::GetPrecedence(asCScriptNode *op)
  9060. {
  9061. // x ** y
  9062. // x * y, x / y, x % y
  9063. // x + y, x - y
  9064. // x <= y, x < y, x >= y, x > y
  9065. // x = =y, x != y, x xor y, x is y, x !is y
  9066. // x and y
  9067. // x or y
  9068. // The following are not used in this function,
  9069. // but should have lower precedence than the above
  9070. // x ? y : z
  9071. // x = y
  9072. // The expression term have the highest precedence
  9073. if( op->nodeType == snExprTerm )
  9074. return 1;
  9075. // Evaluate operators by token
  9076. int tokenType = op->tokenType;
  9077. if( tokenType == ttStarStar )
  9078. return 0;
  9079. if( tokenType == ttStar || tokenType == ttSlash || tokenType == ttPercent )
  9080. return -1;
  9081. if( tokenType == ttPlus || tokenType == ttMinus )
  9082. return -2;
  9083. if( tokenType == ttBitShiftLeft ||
  9084. tokenType == ttBitShiftRight ||
  9085. tokenType == ttBitShiftRightArith )
  9086. return -3;
  9087. if( tokenType == ttAmp )
  9088. return -4;
  9089. if( tokenType == ttBitXor )
  9090. return -5;
  9091. if( tokenType == ttBitOr )
  9092. return -6;
  9093. if( tokenType == ttLessThanOrEqual ||
  9094. tokenType == ttLessThan ||
  9095. tokenType == ttGreaterThanOrEqual ||
  9096. tokenType == ttGreaterThan )
  9097. return -7;
  9098. if( tokenType == ttEqual || tokenType == ttNotEqual || tokenType == ttXor || tokenType == ttIs || tokenType == ttNotIs )
  9099. return -8;
  9100. if( tokenType == ttAnd )
  9101. return -9;
  9102. if( tokenType == ttOr )
  9103. return -10;
  9104. // Unknown operator
  9105. asASSERT(false);
  9106. return 0;
  9107. }
  9108. asUINT asCCompiler::MatchArgument(asCArray<int> &funcs, asCArray<asSOverloadCandidate> &matches, const asSExprContext *argExpr, int paramNum, bool allowObjectConstruct)
  9109. {
  9110. matches.SetLength(0);
  9111. for( asUINT n = 0; n < funcs.GetLength(); n++ )
  9112. {
  9113. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[n]);
  9114. // Does the function have arguments enough?
  9115. if( (int)desc->parameterTypes.GetLength() <= paramNum )
  9116. continue;
  9117. // void expressions can match any out parameter, but nothing else
  9118. if( argExpr->type.IsVoidExpression() )
  9119. {
  9120. if( desc->inOutFlags[paramNum] == asTM_OUTREF )
  9121. matches.PushLast(asSOverloadCandidate(funcs[n], 0));
  9122. continue;
  9123. }
  9124. // Can we make the match by implicit conversion?
  9125. asSExprContext ti(engine);
  9126. ti.type = argExpr->type;
  9127. ti.methodName = argExpr->methodName;
  9128. ti.enumValue = argExpr->enumValue;
  9129. if( argExpr->type.dataType.IsPrimitive() ) ti.type.dataType.MakeReference(false);
  9130. asUINT cost = ImplicitConversion(&ti, desc->parameterTypes[paramNum], 0, asIC_IMPLICIT_CONV, false, allowObjectConstruct);
  9131. // If the function parameter is an inout-reference then it must not be possible to call the
  9132. // function with an incorrect argument type, even though the type can normally be converted.
  9133. if( desc->parameterTypes[paramNum].IsReference() &&
  9134. desc->inOutFlags[paramNum] == asTM_INOUTREF &&
  9135. desc->parameterTypes[paramNum].GetTokenType() != ttQuestion )
  9136. {
  9137. // Observe, that the below checks are only necessary for when unsafe references have been
  9138. // enabled by the application. Without this the &inout reference form wouldn't be allowed
  9139. // for these value types.
  9140. // Don't allow a primitive to be converted to a reference of another primitive type
  9141. if( desc->parameterTypes[paramNum].IsPrimitive() &&
  9142. desc->parameterTypes[paramNum].GetTokenType() != argExpr->type.dataType.GetTokenType() )
  9143. {
  9144. asASSERT( engine->ep.allowUnsafeReferences );
  9145. continue;
  9146. }
  9147. // Don't allow an enum to be converted to a reference of another enum type
  9148. if( desc->parameterTypes[paramNum].IsEnumType() &&
  9149. desc->parameterTypes[paramNum].GetObjectType() != argExpr->type.dataType.GetObjectType() )
  9150. {
  9151. asASSERT( engine->ep.allowUnsafeReferences );
  9152. continue;
  9153. }
  9154. // Don't allow a non-handle expression to be converted to a reference to a handle
  9155. if( desc->parameterTypes[paramNum].IsObjectHandle() &&
  9156. !argExpr->type.dataType.IsObjectHandle() )
  9157. {
  9158. asASSERT( engine->ep.allowUnsafeReferences );
  9159. continue;
  9160. }
  9161. // Don't allow a value type to be converted
  9162. if( (desc->parameterTypes[paramNum].GetObjectType() && (desc->parameterTypes[paramNum].GetObjectType()->GetFlags() & asOBJ_VALUE)) &&
  9163. (desc->parameterTypes[paramNum].GetObjectType() != argExpr->type.dataType.GetObjectType()) )
  9164. {
  9165. asASSERT( engine->ep.allowUnsafeReferences );
  9166. continue;
  9167. }
  9168. }
  9169. // How well does the argument match the function parameter?
  9170. if( desc->parameterTypes[paramNum].IsEqualExceptRef(ti.type.dataType) )
  9171. matches.PushLast(asSOverloadCandidate(funcs[n], cost));
  9172. }
  9173. return (asUINT)matches.GetLength();
  9174. }
  9175. void asCCompiler::PrepareArgument2(asSExprContext *ctx, asSExprContext *arg, asCDataType *paramType, bool isFunction, int refType, bool isMakingCopy)
  9176. {
  9177. // Reference parameters whose value won't be used don't evaluate the expression
  9178. if( paramType->IsReference() && !(refType & asTM_INREF) )
  9179. {
  9180. // Store the original bytecode so that it can be reused when processing the deferred output parameter
  9181. asSExprContext *orig = asNEW(asSExprContext)(engine);
  9182. if( orig == 0 )
  9183. {
  9184. // Out of memory
  9185. return;
  9186. }
  9187. MergeExprBytecodeAndType(orig, arg);
  9188. arg->origExpr = orig;
  9189. }
  9190. PrepareArgument(paramType, arg, arg->exprNode, isFunction, refType, isMakingCopy);
  9191. // arg still holds the original expression for output parameters
  9192. ctx->bc.AddCode(&arg->bc);
  9193. }
  9194. bool asCCompiler::CompileOverloadedDualOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9195. {
  9196. DetermineSingleFunc(lctx, node);
  9197. DetermineSingleFunc(rctx, node);
  9198. ctx->exprNode = node;
  9199. // What type of operator is it?
  9200. int token = node->tokenType;
  9201. if( token == ttUnrecognizedToken )
  9202. {
  9203. // This happens when the compiler is inferring an assignment
  9204. // operation from another action, for example in preparing a value
  9205. // as a function argument
  9206. token = ttAssignment;
  9207. }
  9208. // boolean operators are not overloadable
  9209. if( token == ttAnd ||
  9210. token == ttOr ||
  9211. token == ttXor )
  9212. return false;
  9213. // Dual operators can also be implemented as class methods
  9214. if( token == ttEqual ||
  9215. token == ttNotEqual )
  9216. {
  9217. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  9218. // Find the matching opEquals method
  9219. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  9220. if( r == 0 )
  9221. {
  9222. // Try again by switching the order of the operands
  9223. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  9224. }
  9225. if( r == 1 )
  9226. {
  9227. if( token == ttNotEqual )
  9228. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  9229. // Success, don't continue
  9230. return true;
  9231. }
  9232. else if( r < 0 )
  9233. {
  9234. // Compiler error, don't continue
  9235. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  9236. return true;
  9237. }
  9238. }
  9239. if( token == ttEqual ||
  9240. token == ttNotEqual ||
  9241. token == ttLessThan ||
  9242. token == ttLessThanOrEqual ||
  9243. token == ttGreaterThan ||
  9244. token == ttGreaterThanOrEqual )
  9245. {
  9246. bool swappedOrder = false;
  9247. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  9248. // Find the matching opCmp method
  9249. int r = CompileOverloadedDualOperator2(node, "opCmp", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  9250. if( r == 0 )
  9251. {
  9252. // Try again by switching the order of the operands
  9253. swappedOrder = true;
  9254. r = CompileOverloadedDualOperator2(node, "opCmp", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttInt, false));
  9255. }
  9256. if( r == 1 )
  9257. {
  9258. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  9259. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  9260. ctx->bc.InstrW_DW(asBC_CMPIi, ctx->type.stackOffset, 0);
  9261. if( token == ttEqual )
  9262. ctx->bc.Instr(asBC_TZ);
  9263. else if( token == ttNotEqual )
  9264. ctx->bc.Instr(asBC_TNZ);
  9265. else if( (token == ttLessThan && !swappedOrder) ||
  9266. (token == ttGreaterThan && swappedOrder) )
  9267. ctx->bc.Instr(asBC_TS);
  9268. else if( (token == ttLessThanOrEqual && !swappedOrder) ||
  9269. (token == ttGreaterThanOrEqual && swappedOrder) )
  9270. ctx->bc.Instr(asBC_TNP);
  9271. else if( (token == ttGreaterThan && !swappedOrder) ||
  9272. (token == ttLessThan && swappedOrder) )
  9273. ctx->bc.Instr(asBC_TP);
  9274. else if( (token == ttGreaterThanOrEqual && !swappedOrder) ||
  9275. (token == ttLessThanOrEqual && swappedOrder) )
  9276. ctx->bc.Instr(asBC_TNS);
  9277. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  9278. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), a, true);
  9279. // Success, don't continue
  9280. return true;
  9281. }
  9282. else if( r < 0 )
  9283. {
  9284. // Compiler error, don't continue
  9285. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  9286. return true;
  9287. }
  9288. }
  9289. // The rest of the operators are not commutative, and doesn't require specific return type
  9290. const char *op = 0, *op_r = 0;
  9291. switch( token )
  9292. {
  9293. case ttPlus: op = "opAdd"; op_r = "opAdd_r"; break;
  9294. case ttMinus: op = "opSub"; op_r = "opSub_r"; break;
  9295. case ttStar: op = "opMul"; op_r = "opMul_r"; break;
  9296. case ttSlash: op = "opDiv"; op_r = "opDiv_r"; break;
  9297. case ttPercent: op = "opMod"; op_r = "opMod_r"; break;
  9298. case ttStarStar: op = "opPow"; op_r = "opPow_r"; break;
  9299. case ttBitOr: op = "opOr"; op_r = "opOr_r"; break;
  9300. case ttAmp: op = "opAnd"; op_r = "opAnd_r"; break;
  9301. case ttBitXor: op = "opXor"; op_r = "opXor_r"; break;
  9302. case ttBitShiftLeft: op = "opShl"; op_r = "opShl_r"; break;
  9303. case ttBitShiftRight: op = "opShr"; op_r = "opShr_r"; break;
  9304. case ttBitShiftRightArith: op = "opUShr"; op_r = "opUShr_r"; break;
  9305. }
  9306. // TODO: Might be interesting to support a concatenation operator, e.g. ~
  9307. if( op && op_r )
  9308. {
  9309. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  9310. // Find the matching operator method
  9311. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, ctx);
  9312. if( r == 0 )
  9313. {
  9314. // Try again by switching the order of the operands, and using the reversed operator
  9315. r = CompileOverloadedDualOperator2(node, op_r, rctx, lctx, ctx);
  9316. }
  9317. if( r == 1 )
  9318. {
  9319. // Success, don't continue
  9320. return true;
  9321. }
  9322. else if( r < 0 )
  9323. {
  9324. // Compiler error, don't continue
  9325. ctx->type.SetDummy();
  9326. return true;
  9327. }
  9328. }
  9329. // Assignment operators
  9330. op = 0;
  9331. switch( token )
  9332. {
  9333. case ttAssignment: op = "opAssign"; break;
  9334. case ttAddAssign: op = "opAddAssign"; break;
  9335. case ttSubAssign: op = "opSubAssign"; break;
  9336. case ttMulAssign: op = "opMulAssign"; break;
  9337. case ttDivAssign: op = "opDivAssign"; break;
  9338. case ttModAssign: op = "opModAssign"; break;
  9339. case ttPowAssign: op = "opPowAssign"; break;
  9340. case ttOrAssign: op = "opOrAssign"; break;
  9341. case ttAndAssign: op = "opAndAssign"; break;
  9342. case ttXorAssign: op = "opXorAssign"; break;
  9343. case ttShiftLeftAssign: op = "opShlAssign"; break;
  9344. case ttShiftRightLAssign: op = "opShrAssign"; break;
  9345. case ttShiftRightAAssign: op = "opUShrAssign"; break;
  9346. }
  9347. if( op )
  9348. {
  9349. if( builder->engine->ep.disallowValueAssignForRefType &&
  9350. lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_REF) && !(lctx->type.dataType.GetObjectType()->flags & asOBJ_SCOPED) )
  9351. {
  9352. if( token == ttAssignment )
  9353. Error(TXT_DISALLOW_ASSIGN_ON_REF_TYPE, node);
  9354. else
  9355. Error(TXT_DISALLOW_COMPOUND_ASSIGN_ON_REF_TYPE, node);
  9356. // Set a dummy output
  9357. ctx->type.Set(lctx->type.dataType);
  9358. return true;
  9359. }
  9360. // TODO: Shouldn't accept const lvalue with the assignment operators
  9361. // Find the matching operator method
  9362. int r = CompileOverloadedDualOperator2(node, op, lctx, rctx, ctx);
  9363. if( r == 1 )
  9364. {
  9365. // Success, don't continue
  9366. return true;
  9367. }
  9368. else if( r < 0 )
  9369. {
  9370. // Compiler error, don't continue
  9371. ctx->type.SetDummy();
  9372. return true;
  9373. }
  9374. }
  9375. // No suitable operator was found
  9376. return false;
  9377. }
  9378. // Returns negative on compile error
  9379. // zero on no matching operator
  9380. // one on matching operator
  9381. int asCCompiler::CompileOverloadedDualOperator2(asCScriptNode *node, const char *methodName, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx, bool specificReturn, const asCDataType &returnType)
  9382. {
  9383. // Find the matching method
  9384. if( lctx->type.dataType.IsObject() &&
  9385. (!lctx->type.isExplicitHandle ||
  9386. lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE) )
  9387. {
  9388. asUINT n;
  9389. // Is the left value a const?
  9390. bool isConst = false;
  9391. if( lctx->type.dataType.IsObjectHandle() )
  9392. isConst = lctx->type.dataType.IsHandleToConst();
  9393. else
  9394. isConst = lctx->type.dataType.IsReadOnly();
  9395. asCArray<int> funcs;
  9396. asCObjectType *ot = lctx->type.dataType.GetObjectType();
  9397. for( n = 0; n < ot->methods.GetLength(); n++ )
  9398. {
  9399. asCScriptFunction *func = engine->scriptFunctions[ot->methods[n]];
  9400. if( func->name == methodName &&
  9401. (!specificReturn || func->returnType == returnType) &&
  9402. func->parameterTypes.GetLength() == 1 &&
  9403. (!isConst || func->isReadOnly) )
  9404. {
  9405. // Make sure the method is accessible by the module
  9406. if( builder->module->accessMask & func->accessMask )
  9407. {
  9408. funcs.PushLast(func->id);
  9409. }
  9410. }
  9411. }
  9412. // Which is the best matching function?
  9413. asCArray<asSOverloadCandidate> tempFuncs;
  9414. MatchArgument(funcs, tempFuncs, rctx, 0);
  9415. // Find the lowest cost operator(s)
  9416. asCArray<int> ops;
  9417. asUINT bestCost = asUINT(-1);
  9418. for( n = 0; n < tempFuncs.GetLength(); ++n )
  9419. {
  9420. asUINT cost = tempFuncs[n].cost;
  9421. if( cost < bestCost )
  9422. {
  9423. ops.SetLength(0);
  9424. bestCost = cost;
  9425. }
  9426. if( cost == bestCost )
  9427. ops.PushLast(tempFuncs[n].funcId);
  9428. }
  9429. // If the object is not const, then we need to prioritize non-const methods
  9430. if( !isConst )
  9431. FilterConst(ops);
  9432. // Did we find an operator?
  9433. if( ops.GetLength() == 1 )
  9434. {
  9435. // Process the lctx expression as get accessor
  9436. ProcessPropertyGetAccessor(lctx, node);
  9437. // Merge the bytecode so that it forms lvalue.methodName(rvalue)
  9438. asCTypeInfo objType = lctx->type;
  9439. asCArray<asSExprContext *> args;
  9440. args.PushLast(rctx);
  9441. MergeExprBytecode(ctx, lctx);
  9442. ctx->type = lctx->type;
  9443. MakeFunctionCall(ctx, ops[0], objType.dataType.GetObjectType(), args, node);
  9444. // If the method returned a reference, then we can't release the original
  9445. // object yet, because the reference may be to a member of it
  9446. if( !objType.isTemporary ||
  9447. !(ctx->type.dataType.IsReference() || (ctx->type.dataType.IsObject() && !ctx->type.dataType.IsObjectHandle())) ||
  9448. ctx->type.isVariable ) // If the resulting type is a variable, then the reference is not to a member
  9449. {
  9450. // As the index operator didn't return a reference to a
  9451. // member we can release the original object now
  9452. ReleaseTemporaryVariable(objType, &ctx->bc);
  9453. }
  9454. // Found matching operator
  9455. return 1;
  9456. }
  9457. else if( ops.GetLength() > 1 )
  9458. {
  9459. Error(TXT_MORE_THAN_ONE_MATCHING_OP, node);
  9460. PrintMatchingFuncs(ops, node);
  9461. ctx->type.SetDummy();
  9462. // Compiler error
  9463. return -1;
  9464. }
  9465. }
  9466. // No matching operator
  9467. return 0;
  9468. }
  9469. void asCCompiler::MakeFunctionCall(asSExprContext *ctx, int funcId, asCObjectType *objectType, asCArray<asSExprContext*> &args, asCScriptNode * /*node*/, bool useVariable, int stackOffset, int funcPtrVar)
  9470. {
  9471. if( objectType )
  9472. {
  9473. Dereference(ctx, true);
  9474. // This following warning was removed as there may be valid reasons
  9475. // for calling non-const methods on temporary objects, and we shouldn't
  9476. // warn when there is no way of removing the warning.
  9477. /*
  9478. // Warn if the method is non-const and the object is temporary
  9479. // since the changes will be lost when the object is destroyed.
  9480. // If the object is accessed through a handle, then it is assumed
  9481. // the object is not temporary, even though the handle is.
  9482. if( ctx->type.isTemporary &&
  9483. !ctx->type.dataType.IsObjectHandle() &&
  9484. !engine->scriptFunctions[funcId]->isReadOnly )
  9485. {
  9486. Warning("A non-const method is called on temporary object. Changes to the object may be lost.", node);
  9487. Information(engine->scriptFunctions[funcId]->GetDeclaration(), node);
  9488. }
  9489. */ }
  9490. asCByteCode objBC(engine);
  9491. objBC.AddCode(&ctx->bc);
  9492. PrepareFunctionCall(funcId, &ctx->bc, args);
  9493. // Verify if any of the args variable offsets are used in the other code.
  9494. // If they are exchange the offset for a new one
  9495. asUINT n;
  9496. for( n = 0; n < args.GetLength(); n++ )
  9497. {
  9498. if( args[n]->type.isTemporary && objBC.IsVarUsed(args[n]->type.stackOffset) )
  9499. {
  9500. // Release the current temporary variable
  9501. ReleaseTemporaryVariable(args[n]->type, 0);
  9502. asCDataType dt = args[n]->type.dataType;
  9503. dt.MakeReference(false);
  9504. int l = int(reservedVariables.GetLength());
  9505. objBC.GetVarsUsed(reservedVariables);
  9506. ctx->bc.GetVarsUsed(reservedVariables);
  9507. int newOffset = AllocateVariable(dt, true, IsVariableOnHeap(args[n]->type.stackOffset));
  9508. reservedVariables.SetLength(l);
  9509. asASSERT( IsVariableOnHeap(args[n]->type.stackOffset) == IsVariableOnHeap(newOffset) );
  9510. ctx->bc.ExchangeVar(args[n]->type.stackOffset, newOffset);
  9511. args[n]->type.stackOffset = (short)newOffset;
  9512. args[n]->type.isTemporary = true;
  9513. args[n]->type.isVariable = true;
  9514. }
  9515. }
  9516. // If the function will return a value type on the stack, then we must allocate space
  9517. // for that here and push the address on the stack as a hidden argument to the function
  9518. asCScriptFunction *func = builder->GetFunctionDescription(funcId);
  9519. if( func->DoesReturnOnStack() )
  9520. {
  9521. asASSERT(!useVariable);
  9522. useVariable = true;
  9523. stackOffset = AllocateVariable(func->returnType, true);
  9524. ctx->bc.InstrSHORT(asBC_PSF, short(stackOffset));
  9525. }
  9526. ctx->bc.AddCode(&objBC);
  9527. MoveArgsToStack(funcId, &ctx->bc, args, objectType ? true : false);
  9528. PerformFunctionCall(funcId, ctx, false, &args, 0, useVariable, stackOffset, funcPtrVar);
  9529. }
  9530. int asCCompiler::CompileOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9531. {
  9532. // Don't allow any operators on expressions that take address of class method, but allow it on global functions
  9533. if( (lctx->IsClassMethod()) || (rctx->IsClassMethod()) )
  9534. {
  9535. Error(TXT_INVALID_OP_ON_METHOD, node);
  9536. return -1;
  9537. }
  9538. // Don't allow any operators on void expressions
  9539. if( lctx->type.IsVoidExpression() || rctx->type.IsVoidExpression() )
  9540. {
  9541. Error(TXT_VOID_CANT_BE_OPERAND, node);
  9542. return -1;
  9543. }
  9544. IsVariableInitialized(&lctx->type, node);
  9545. IsVariableInitialized(&rctx->type, node);
  9546. if( lctx->type.isExplicitHandle || rctx->type.isExplicitHandle ||
  9547. lctx->type.IsNullConstant() || rctx->type.IsNullConstant() ||
  9548. node->tokenType == ttIs || node->tokenType == ttNotIs )
  9549. {
  9550. CompileOperatorOnHandles(node, lctx, rctx, ctx);
  9551. return 0;
  9552. }
  9553. else
  9554. {
  9555. // Compile an overloaded operator for the two operands
  9556. if( CompileOverloadedDualOperator(node, lctx, rctx, ctx) )
  9557. return 0;
  9558. // If both operands are objects, then we shouldn't continue
  9559. if( lctx->type.dataType.IsObject() && rctx->type.dataType.IsObject() )
  9560. {
  9561. asCString str;
  9562. str.Format(TXT_NO_MATCHING_OP_FOUND_FOR_TYPES_s_AND_s, lctx->type.dataType.Format().AddressOf(), rctx->type.dataType.Format().AddressOf());
  9563. Error(str, node);
  9564. ctx->type.SetDummy();
  9565. return -1;
  9566. }
  9567. // Process the property get accessors (if any)
  9568. ProcessPropertyGetAccessor(lctx, node);
  9569. ProcessPropertyGetAccessor(rctx, node);
  9570. // Make sure we have two variables or constants
  9571. if( lctx->type.dataType.IsReference() ) ConvertToVariableNotIn(lctx, rctx);
  9572. if( rctx->type.dataType.IsReference() ) ConvertToVariableNotIn(rctx, lctx);
  9573. // Make sure lctx doesn't end up with a variable used in rctx
  9574. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  9575. {
  9576. int offset = AllocateVariableNotIn(lctx->type.dataType, true, false, rctx);
  9577. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  9578. ReleaseTemporaryVariable(offset, 0);
  9579. }
  9580. // Math operators
  9581. // + - * / % ** += -= *= /= %= **=
  9582. int op = node->tokenType;
  9583. if( op == ttPlus || op == ttAddAssign ||
  9584. op == ttMinus || op == ttSubAssign ||
  9585. op == ttStar || op == ttMulAssign ||
  9586. op == ttSlash || op == ttDivAssign ||
  9587. op == ttPercent || op == ttModAssign ||
  9588. op == ttStarStar || op == ttPowAssign )
  9589. {
  9590. CompileMathOperator(node, lctx, rctx, ctx);
  9591. return 0;
  9592. }
  9593. // Bitwise operators
  9594. // << >> >>> & | ^ <<= >>= >>>= &= |= ^=
  9595. if( op == ttAmp || op == ttAndAssign ||
  9596. op == ttBitOr || op == ttOrAssign ||
  9597. op == ttBitXor || op == ttXorAssign ||
  9598. op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  9599. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  9600. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  9601. {
  9602. CompileBitwiseOperator(node, lctx, rctx, ctx);
  9603. return 0;
  9604. }
  9605. // Comparison operators
  9606. // == != < > <= >=
  9607. if( op == ttEqual || op == ttNotEqual ||
  9608. op == ttLessThan || op == ttLessThanOrEqual ||
  9609. op == ttGreaterThan || op == ttGreaterThanOrEqual )
  9610. {
  9611. CompileComparisonOperator(node, lctx, rctx, ctx);
  9612. return 0;
  9613. }
  9614. // Boolean operators
  9615. // && || ^^
  9616. if( op == ttAnd || op == ttOr || op == ttXor )
  9617. {
  9618. CompileBooleanOperator(node, lctx, rctx, ctx);
  9619. return 0;
  9620. }
  9621. }
  9622. asASSERT(false);
  9623. return -1;
  9624. }
  9625. void asCCompiler::ConvertToTempVariableNotIn(asSExprContext *ctx, asSExprContext *exclude)
  9626. {
  9627. int l = int(reservedVariables.GetLength());
  9628. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  9629. ConvertToTempVariable(ctx);
  9630. reservedVariables.SetLength(l);
  9631. }
  9632. void asCCompiler::ConvertToTempVariable(asSExprContext *ctx)
  9633. {
  9634. // This is only used for primitive types and null handles
  9635. asASSERT( ctx->type.dataType.IsPrimitive() || ctx->type.dataType.IsNullHandle() );
  9636. ConvertToVariable(ctx);
  9637. if( !ctx->type.isTemporary )
  9638. {
  9639. if( ctx->type.dataType.IsPrimitive() )
  9640. {
  9641. // Copy the variable to a temporary variable
  9642. int offset = AllocateVariable(ctx->type.dataType, true);
  9643. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9644. ctx->bc.InstrW_W(asBC_CpyVtoV4, offset, ctx->type.stackOffset);
  9645. else
  9646. ctx->bc.InstrW_W(asBC_CpyVtoV8, offset, ctx->type.stackOffset);
  9647. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  9648. }
  9649. else
  9650. {
  9651. // We should never get here
  9652. asASSERT(false);
  9653. }
  9654. }
  9655. }
  9656. void asCCompiler::ConvertToVariable(asSExprContext *ctx)
  9657. {
  9658. // We should never get here while the context is still an unprocessed property accessor
  9659. asASSERT(ctx->property_get == 0 && ctx->property_set == 0);
  9660. int offset;
  9661. if( !ctx->type.isVariable &&
  9662. (ctx->type.dataType.IsObjectHandle() ||
  9663. (ctx->type.dataType.IsObject() && ctx->type.dataType.SupportHandles())) )
  9664. {
  9665. offset = AllocateVariable(ctx->type.dataType, true);
  9666. if( ctx->type.IsNullConstant() )
  9667. {
  9668. if( ctx->bc.GetLastInstr() == asBC_PshNull )
  9669. ctx->bc.Instr(asBC_PopPtr); // Pop the null constant pushed onto the stack
  9670. ctx->bc.InstrSHORT(asBC_ClrVPtr, (short)offset);
  9671. }
  9672. else
  9673. {
  9674. // Copy the object handle to a variable
  9675. ctx->bc.InstrSHORT(asBC_PSF, (short)offset);
  9676. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  9677. ctx->bc.Instr(asBC_PopPtr);
  9678. }
  9679. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  9680. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  9681. ctx->type.dataType.MakeHandle(true);
  9682. }
  9683. else if( (!ctx->type.isVariable || ctx->type.dataType.IsReference()) &&
  9684. ctx->type.dataType.IsPrimitive() )
  9685. {
  9686. if( ctx->type.isConstant )
  9687. {
  9688. offset = AllocateVariable(ctx->type.dataType, true);
  9689. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  9690. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, ctx->type.byteValue);
  9691. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  9692. ctx->bc.InstrSHORT_W(asBC_SetV2, (short)offset, ctx->type.wordValue);
  9693. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 4 )
  9694. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, ctx->type.dwordValue);
  9695. else
  9696. ctx->bc.InstrSHORT_QW(asBC_SetV8, (short)offset, ctx->type.qwordValue);
  9697. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  9698. return;
  9699. }
  9700. else
  9701. {
  9702. asASSERT(ctx->type.dataType.IsPrimitive());
  9703. asASSERT(ctx->type.dataType.IsReference());
  9704. ctx->type.dataType.MakeReference(false);
  9705. offset = AllocateVariable(ctx->type.dataType, true);
  9706. // Read the value from the address in the register directly into the variable
  9707. if( ctx->type.dataType.GetSizeInMemoryBytes() == 1 )
  9708. ctx->bc.InstrSHORT(asBC_RDR1, (short)offset);
  9709. else if( ctx->type.dataType.GetSizeInMemoryBytes() == 2 )
  9710. ctx->bc.InstrSHORT(asBC_RDR2, (short)offset);
  9711. else if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9712. ctx->bc.InstrSHORT(asBC_RDR4, (short)offset);
  9713. else
  9714. ctx->bc.InstrSHORT(asBC_RDR8, (short)offset);
  9715. }
  9716. ReleaseTemporaryVariable(ctx->type, &ctx->bc);
  9717. ctx->type.SetVariable(ctx->type.dataType, offset, true);
  9718. }
  9719. }
  9720. void asCCompiler::ConvertToVariableNotIn(asSExprContext *ctx, asSExprContext *exclude)
  9721. {
  9722. int l = int(reservedVariables.GetLength());
  9723. if( exclude ) exclude->bc.GetVarsUsed(reservedVariables);
  9724. ConvertToVariable(ctx);
  9725. reservedVariables.SetLength(l);
  9726. }
  9727. void asCCompiler::CompileMathOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  9728. {
  9729. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  9730. // TODO: clean up: This initial part is identical to CompileComparisonOperator. Make a common function out of it
  9731. // Implicitly convert the operands to a number type
  9732. asCDataType to;
  9733. // If either operand is a non-primitive then use the primitive type
  9734. if( !lctx->type.dataType.IsPrimitive() )
  9735. to.SetTokenType(rctx->type.dataType.GetTokenType());
  9736. else if( !rctx->type.dataType.IsPrimitive() )
  9737. to.SetTokenType(lctx->type.dataType.GetTokenType());
  9738. else if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  9739. to.SetTokenType(ttDouble);
  9740. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  9741. to.SetTokenType(ttFloat);
  9742. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  9743. {
  9744. // Convert to int64 if both are signed or if one is non-constant and signed
  9745. if( (lctx->type.dataType.IsIntegerType() && rctx->type.dataType.IsIntegerType()) ||
  9746. (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  9747. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  9748. to.SetTokenType(ttInt64);
  9749. else
  9750. to.SetTokenType(ttUInt64);
  9751. }
  9752. else
  9753. {
  9754. // Convert to int32 if both are signed or if one is non-constant and signed
  9755. if( (lctx->type.dataType.IsIntegerType() && rctx->type.dataType.IsIntegerType()) ||
  9756. (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  9757. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  9758. to.SetTokenType(ttInt);
  9759. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  9760. to.SetTokenType(ttUInt);
  9761. else if( lctx->type.dataType.IsBooleanType() || rctx->type.dataType.IsBooleanType() )
  9762. to.SetTokenType(ttBool);
  9763. }
  9764. // If doing an operation with double constant and float variable, the constant should be converted to float
  9765. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  9766. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  9767. to.SetTokenType(ttFloat);
  9768. // Do the actual conversion
  9769. int l = int(reservedVariables.GetLength());
  9770. rctx->bc.GetVarsUsed(reservedVariables);
  9771. lctx->bc.GetVarsUsed(reservedVariables);
  9772. if( lctx->type.dataType.IsReference() )
  9773. ConvertToVariable(lctx);
  9774. if( rctx->type.dataType.IsReference() )
  9775. ConvertToVariable(rctx);
  9776. int op = node->tokenType;
  9777. if( to.IsPrimitive() )
  9778. {
  9779. // ttStarStar allows an integer, right-hand operand and a double
  9780. // left-hand operand.
  9781. if( (op == ttStarStar || op == ttPowAssign) &&
  9782. lctx->type.dataType.IsDoubleType() &&
  9783. (rctx->type.dataType.IsIntegerType() ||
  9784. rctx->type.dataType.IsUnsignedType()) )
  9785. {
  9786. to.SetTokenType(ttInt);
  9787. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  9788. to.SetTokenType(ttDouble);
  9789. }
  9790. else
  9791. {
  9792. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  9793. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV, true);
  9794. }
  9795. }
  9796. reservedVariables.SetLength(l);
  9797. // Verify that the conversion was successful
  9798. if( !lctx->type.dataType.IsIntegerType() &&
  9799. !lctx->type.dataType.IsUnsignedType() &&
  9800. !lctx->type.dataType.IsFloatType() &&
  9801. !lctx->type.dataType.IsDoubleType() )
  9802. {
  9803. asCString str;
  9804. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, lctx->type.dataType.Format().AddressOf());
  9805. Error(str, node);
  9806. ctx->type.SetDummy();
  9807. return;
  9808. }
  9809. if( !rctx->type.dataType.IsIntegerType() &&
  9810. !rctx->type.dataType.IsUnsignedType() &&
  9811. !rctx->type.dataType.IsFloatType() &&
  9812. !rctx->type.dataType.IsDoubleType() )
  9813. {
  9814. asCString str;
  9815. str.Format(TXT_NO_CONVERSION_s_TO_MATH_TYPE, rctx->type.dataType.Format().AddressOf());
  9816. Error(str, node);
  9817. ctx->type.SetDummy();
  9818. return;
  9819. }
  9820. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  9821. // Verify if we are dividing with a constant zero
  9822. if( rctx->type.isConstant && rctx->type.qwordValue == 0 &&
  9823. (op == ttSlash || op == ttDivAssign ||
  9824. op == ttPercent || op == ttModAssign) )
  9825. {
  9826. Error(TXT_DIVIDE_BY_ZERO, node);
  9827. }
  9828. if( !isConstant )
  9829. {
  9830. ConvertToVariableNotIn(lctx, rctx);
  9831. ConvertToVariableNotIn(rctx, lctx);
  9832. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  9833. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  9834. if( op == ttAddAssign || op == ttSubAssign ||
  9835. op == ttMulAssign || op == ttDivAssign ||
  9836. op == ttModAssign || op == ttPowAssign )
  9837. {
  9838. // Merge the operands in the different order so that they are evaluated correctly
  9839. MergeExprBytecode(ctx, rctx);
  9840. MergeExprBytecode(ctx, lctx);
  9841. // We must not process the deferred parameters yet, as
  9842. // it may overwrite the lvalue kept in the register
  9843. }
  9844. else
  9845. {
  9846. MergeExprBytecode(ctx, lctx);
  9847. MergeExprBytecode(ctx, rctx);
  9848. ProcessDeferredParams(ctx);
  9849. }
  9850. asEBCInstr instruction = asBC_ADDi;
  9851. if( lctx->type.dataType.IsIntegerType() ||
  9852. lctx->type.dataType.IsUnsignedType() )
  9853. {
  9854. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9855. {
  9856. if( op == ttPlus || op == ttAddAssign )
  9857. instruction = asBC_ADDi;
  9858. else if( op == ttMinus || op == ttSubAssign )
  9859. instruction = asBC_SUBi;
  9860. else if( op == ttStar || op == ttMulAssign )
  9861. instruction = asBC_MULi;
  9862. else if( op == ttSlash || op == ttDivAssign )
  9863. {
  9864. if( lctx->type.dataType.IsIntegerType() )
  9865. instruction = asBC_DIVi;
  9866. else
  9867. instruction = asBC_DIVu;
  9868. }
  9869. else if( op == ttPercent || op == ttModAssign )
  9870. {
  9871. if( lctx->type.dataType.IsIntegerType() )
  9872. instruction = asBC_MODi;
  9873. else
  9874. instruction = asBC_MODu;
  9875. }
  9876. else if( op == ttStarStar || op == ttPowAssign )
  9877. {
  9878. if( lctx->type.dataType.IsIntegerType() )
  9879. instruction = asBC_POWi;
  9880. else
  9881. instruction = asBC_POWu;
  9882. }
  9883. }
  9884. else
  9885. {
  9886. if( op == ttPlus || op == ttAddAssign )
  9887. instruction = asBC_ADDi64;
  9888. else if( op == ttMinus || op == ttSubAssign )
  9889. instruction = asBC_SUBi64;
  9890. else if( op == ttStar || op == ttMulAssign )
  9891. instruction = asBC_MULi64;
  9892. else if( op == ttSlash || op == ttDivAssign )
  9893. {
  9894. if( lctx->type.dataType.IsIntegerType() )
  9895. instruction = asBC_DIVi64;
  9896. else
  9897. instruction = asBC_DIVu64;
  9898. }
  9899. else if( op == ttPercent || op == ttModAssign )
  9900. {
  9901. if( lctx->type.dataType.IsIntegerType() )
  9902. instruction = asBC_MODi64;
  9903. else
  9904. instruction = asBC_MODu64;
  9905. }
  9906. else if( op == ttStarStar || op == ttPowAssign )
  9907. {
  9908. if( lctx->type.dataType.IsIntegerType() )
  9909. instruction = asBC_POWi64;
  9910. else
  9911. instruction = asBC_POWu64;
  9912. }
  9913. }
  9914. }
  9915. else if( lctx->type.dataType.IsFloatType() )
  9916. {
  9917. if( op == ttPlus || op == ttAddAssign )
  9918. instruction = asBC_ADDf;
  9919. else if( op == ttMinus || op == ttSubAssign )
  9920. instruction = asBC_SUBf;
  9921. else if( op == ttStar || op == ttMulAssign )
  9922. instruction = asBC_MULf;
  9923. else if( op == ttSlash || op == ttDivAssign )
  9924. instruction = asBC_DIVf;
  9925. else if( op == ttPercent || op == ttModAssign )
  9926. instruction = asBC_MODf;
  9927. else if( op == ttStarStar || op == ttPowAssign )
  9928. instruction = asBC_POWf;
  9929. }
  9930. else if( lctx->type.dataType.IsDoubleType() )
  9931. {
  9932. if( rctx->type.dataType.IsIntegerType() )
  9933. {
  9934. asASSERT(rctx->type.dataType.GetSizeInMemoryDWords() == 1);
  9935. if( op == ttStarStar || op == ttPowAssign )
  9936. instruction = asBC_POWdi;
  9937. else
  9938. asASSERT(false); // Should not be possible
  9939. }
  9940. else
  9941. {
  9942. if( op == ttPlus || op == ttAddAssign )
  9943. instruction = asBC_ADDd;
  9944. else if( op == ttMinus || op == ttSubAssign )
  9945. instruction = asBC_SUBd;
  9946. else if( op == ttStar || op == ttMulAssign )
  9947. instruction = asBC_MULd;
  9948. else if( op == ttSlash || op == ttDivAssign )
  9949. instruction = asBC_DIVd;
  9950. else if( op == ttPercent || op == ttModAssign )
  9951. instruction = asBC_MODd;
  9952. else if( op == ttStarStar || op == ttPowAssign )
  9953. instruction = asBC_POWd;
  9954. }
  9955. }
  9956. else
  9957. {
  9958. // Shouldn't be possible
  9959. asASSERT(false);
  9960. }
  9961. // Do the operation
  9962. int a = AllocateVariable(lctx->type.dataType, true);
  9963. int b = lctx->type.stackOffset;
  9964. int c = rctx->type.stackOffset;
  9965. ctx->bc.InstrW_W_W(instruction, a, b, c);
  9966. ctx->type.SetVariable(lctx->type.dataType, a, true);
  9967. }
  9968. else
  9969. {
  9970. // Both values are constants
  9971. if( lctx->type.dataType.IsIntegerType() ||
  9972. lctx->type.dataType.IsUnsignedType() )
  9973. {
  9974. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  9975. {
  9976. int v = 0;
  9977. if( op == ttPlus )
  9978. v = lctx->type.intValue + rctx->type.intValue;
  9979. else if( op == ttMinus )
  9980. v = lctx->type.intValue - rctx->type.intValue;
  9981. else if( op == ttStar )
  9982. v = lctx->type.intValue * rctx->type.intValue;
  9983. else if( op == ttSlash )
  9984. {
  9985. // TODO: Should probably report an error, rather than silently convert the value to 0
  9986. if( rctx->type.intValue == 0 || (rctx->type.intValue == -1 && lctx->type.dwordValue == 0x80000000) )
  9987. v = 0;
  9988. else
  9989. if( lctx->type.dataType.IsIntegerType() )
  9990. v = lctx->type.intValue / rctx->type.intValue;
  9991. else
  9992. v = lctx->type.dwordValue / rctx->type.dwordValue;
  9993. }
  9994. else if( op == ttPercent )
  9995. {
  9996. // TODO: Should probably report an error, rather than silently convert the value to 0
  9997. if( rctx->type.intValue == 0 || (rctx->type.intValue == -1 && lctx->type.dwordValue == 0x80000000) )
  9998. v = 0;
  9999. else
  10000. if( lctx->type.dataType.IsIntegerType() )
  10001. v = lctx->type.intValue % rctx->type.intValue;
  10002. else
  10003. v = lctx->type.dwordValue % rctx->type.dwordValue;
  10004. }
  10005. else if( op == ttStarStar )
  10006. {
  10007. bool isOverflow;
  10008. if( lctx->type.dataType.IsIntegerType() )
  10009. v = as_powi(lctx->type.intValue, rctx->type.intValue, isOverflow);
  10010. else
  10011. v = as_powu(lctx->type.dwordValue, rctx->type.dwordValue, isOverflow);
  10012. }
  10013. ctx->type.SetConstantDW(lctx->type.dataType, v);
  10014. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  10015. if( lctx->type.dataType.GetTokenType() == ttUInt && op == ttMinus && lctx->type.intValue < rctx->type.intValue )
  10016. ctx->type.dataType.SetTokenType(ttInt);
  10017. }
  10018. else
  10019. {
  10020. asQWORD v = 0;
  10021. if( op == ttPlus )
  10022. v = lctx->type.qwordValue + rctx->type.qwordValue;
  10023. else if( op == ttMinus )
  10024. v = lctx->type.qwordValue - rctx->type.qwordValue;
  10025. else if( op == ttStar )
  10026. v = lctx->type.qwordValue * rctx->type.qwordValue;
  10027. else if( op == ttSlash )
  10028. {
  10029. // TODO: Should probably report an error, rather than silently convert the value to 0
  10030. if( rctx->type.qwordValue == 0 || (rctx->type.qwordValue == asQWORD(-1) && lctx->type.qwordValue == (asQWORD(1)<<63)) )
  10031. v = 0;
  10032. else
  10033. if( lctx->type.dataType.IsIntegerType() )
  10034. v = asINT64(lctx->type.qwordValue) / asINT64(rctx->type.qwordValue);
  10035. else
  10036. v = lctx->type.qwordValue / rctx->type.qwordValue;
  10037. }
  10038. else if( op == ttPercent )
  10039. {
  10040. // TODO: Should probably report an error, rather than silently convert the value to 0
  10041. if( rctx->type.qwordValue == 0 || (rctx->type.qwordValue == asQWORD(-1) && lctx->type.qwordValue == (asQWORD(1)<<63)) )
  10042. v = 0;
  10043. else
  10044. if( lctx->type.dataType.IsIntegerType() )
  10045. v = asINT64(lctx->type.qwordValue) % asINT64(rctx->type.qwordValue);
  10046. else
  10047. v = lctx->type.qwordValue % rctx->type.qwordValue;
  10048. }
  10049. else if( op == ttStarStar )
  10050. {
  10051. bool isOverflow;
  10052. if( lctx->type.dataType.IsIntegerType() )
  10053. v = as_powi64(asINT64(lctx->type.qwordValue), asINT64(rctx->type.qwordValue), isOverflow);
  10054. else
  10055. v = as_powu64(lctx->type.qwordValue, rctx->type.qwordValue, isOverflow);
  10056. }
  10057. ctx->type.SetConstantQW(lctx->type.dataType, v);
  10058. // If the right value is greater than the left value in a minus operation, then we need to convert the type to int
  10059. if( lctx->type.dataType.GetTokenType() == ttUInt64 && op == ttMinus && lctx->type.qwordValue < rctx->type.qwordValue )
  10060. ctx->type.dataType.SetTokenType(ttInt64);
  10061. }
  10062. }
  10063. else if( lctx->type.dataType.IsFloatType() )
  10064. {
  10065. float v = 0.0f;
  10066. if( op == ttPlus )
  10067. v = lctx->type.floatValue + rctx->type.floatValue;
  10068. else if( op == ttMinus )
  10069. v = lctx->type.floatValue - rctx->type.floatValue;
  10070. else if( op == ttStar )
  10071. v = lctx->type.floatValue * rctx->type.floatValue;
  10072. else if( op == ttSlash )
  10073. {
  10074. if( rctx->type.floatValue == 0 )
  10075. v = 0;
  10076. else
  10077. v = lctx->type.floatValue / rctx->type.floatValue;
  10078. }
  10079. else if( op == ttPercent )
  10080. {
  10081. if( rctx->type.floatValue == 0 )
  10082. v = 0;
  10083. else
  10084. v = fmodf(lctx->type.floatValue, rctx->type.floatValue);
  10085. }
  10086. else if( op == ttStarStar )
  10087. v = pow(lctx->type.floatValue, rctx->type.floatValue);
  10088. ctx->type.SetConstantF(lctx->type.dataType, v);
  10089. }
  10090. else if( lctx->type.dataType.IsDoubleType() )
  10091. {
  10092. double v = 0.0;
  10093. if( rctx->type.dataType.IsIntegerType() )
  10094. {
  10095. asASSERT(rctx->type.dataType.GetSizeInMemoryDWords() == 1);
  10096. if( op == ttStarStar || op == ttPowAssign )
  10097. v = pow(lctx->type.doubleValue, rctx->type.intValue);
  10098. else
  10099. asASSERT(false); // Should not be possible
  10100. }
  10101. else
  10102. {
  10103. if( op == ttPlus )
  10104. v = lctx->type.doubleValue + rctx->type.doubleValue;
  10105. else if( op == ttMinus )
  10106. v = lctx->type.doubleValue - rctx->type.doubleValue;
  10107. else if( op == ttStar )
  10108. v = lctx->type.doubleValue * rctx->type.doubleValue;
  10109. else if( op == ttSlash )
  10110. {
  10111. if( rctx->type.doubleValue == 0 )
  10112. v = 0;
  10113. else
  10114. v = lctx->type.doubleValue / rctx->type.doubleValue;
  10115. }
  10116. else if( op == ttPercent )
  10117. {
  10118. if( rctx->type.doubleValue == 0 )
  10119. v = 0;
  10120. else
  10121. v = fmod(lctx->type.doubleValue, rctx->type.doubleValue);
  10122. }
  10123. else if( op == ttStarStar )
  10124. v = pow(lctx->type.doubleValue, rctx->type.doubleValue);
  10125. }
  10126. ctx->type.SetConstantD(lctx->type.dataType, v);
  10127. }
  10128. else
  10129. {
  10130. // Shouldn't be possible
  10131. asASSERT(false);
  10132. }
  10133. }
  10134. }
  10135. void asCCompiler::CompileBitwiseOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  10136. {
  10137. // TODO: If a constant is only using 32bits, then a 32bit operation is preferred
  10138. int op = node->tokenType;
  10139. if( op == ttAmp || op == ttAndAssign ||
  10140. op == ttBitOr || op == ttOrAssign ||
  10141. op == ttBitXor || op == ttXorAssign )
  10142. {
  10143. // Convert left hand operand to integer if it's not already one
  10144. asCDataType to;
  10145. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 ||
  10146. rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10147. to.SetTokenType(ttUInt64);
  10148. else
  10149. to.SetTokenType(ttUInt);
  10150. // Do the actual conversion
  10151. int l = int(reservedVariables.GetLength());
  10152. rctx->bc.GetVarsUsed(reservedVariables);
  10153. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  10154. reservedVariables.SetLength(l);
  10155. // Verify that the conversion was successful
  10156. if( !lctx->type.dataType.IsUnsignedType() )
  10157. {
  10158. asCString str;
  10159. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  10160. Error(str, node);
  10161. }
  10162. // Convert right hand operand to same type as left hand operand
  10163. l = int(reservedVariables.GetLength());
  10164. lctx->bc.GetVarsUsed(reservedVariables);
  10165. ImplicitConversion(rctx, lctx->type.dataType, node, asIC_IMPLICIT_CONV, true);
  10166. reservedVariables.SetLength(l);
  10167. if( !rctx->type.dataType.IsEqualExceptRef(lctx->type.dataType) )
  10168. {
  10169. asCString str;
  10170. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), lctx->type.dataType.Format().AddressOf());
  10171. Error(str, node);
  10172. }
  10173. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  10174. if( !isConstant )
  10175. {
  10176. ConvertToVariableNotIn(lctx, rctx);
  10177. ConvertToVariableNotIn(rctx, lctx);
  10178. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  10179. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  10180. if( op == ttAndAssign || op == ttOrAssign || op == ttXorAssign )
  10181. {
  10182. // Compound assignments execute the right hand value first
  10183. MergeExprBytecode(ctx, rctx);
  10184. MergeExprBytecode(ctx, lctx);
  10185. }
  10186. else
  10187. {
  10188. MergeExprBytecode(ctx, lctx);
  10189. MergeExprBytecode(ctx, rctx);
  10190. }
  10191. ProcessDeferredParams(ctx);
  10192. asEBCInstr instruction = asBC_BAND;
  10193. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10194. {
  10195. if( op == ttAmp || op == ttAndAssign )
  10196. instruction = asBC_BAND;
  10197. else if( op == ttBitOr || op == ttOrAssign )
  10198. instruction = asBC_BOR;
  10199. else if( op == ttBitXor || op == ttXorAssign )
  10200. instruction = asBC_BXOR;
  10201. }
  10202. else
  10203. {
  10204. if( op == ttAmp || op == ttAndAssign )
  10205. instruction = asBC_BAND64;
  10206. else if( op == ttBitOr || op == ttOrAssign )
  10207. instruction = asBC_BOR64;
  10208. else if( op == ttBitXor || op == ttXorAssign )
  10209. instruction = asBC_BXOR64;
  10210. }
  10211. // Do the operation
  10212. int a = AllocateVariable(lctx->type.dataType, true);
  10213. int b = lctx->type.stackOffset;
  10214. int c = rctx->type.stackOffset;
  10215. ctx->bc.InstrW_W_W(instruction, a, b, c);
  10216. ctx->type.SetVariable(lctx->type.dataType, a, true);
  10217. }
  10218. else
  10219. {
  10220. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10221. {
  10222. asQWORD v = 0;
  10223. if( op == ttAmp )
  10224. v = lctx->type.qwordValue & rctx->type.qwordValue;
  10225. else if( op == ttBitOr )
  10226. v = lctx->type.qwordValue | rctx->type.qwordValue;
  10227. else if( op == ttBitXor )
  10228. v = lctx->type.qwordValue ^ rctx->type.qwordValue;
  10229. // Remember the result
  10230. ctx->type.SetConstantQW(lctx->type.dataType, v);
  10231. }
  10232. else
  10233. {
  10234. asDWORD v = 0;
  10235. if( op == ttAmp )
  10236. v = lctx->type.dwordValue & rctx->type.dwordValue;
  10237. else if( op == ttBitOr )
  10238. v = lctx->type.dwordValue | rctx->type.dwordValue;
  10239. else if( op == ttBitXor )
  10240. v = lctx->type.dwordValue ^ rctx->type.dwordValue;
  10241. // Remember the result
  10242. ctx->type.SetConstantDW(lctx->type.dataType, v);
  10243. }
  10244. }
  10245. }
  10246. else if( op == ttBitShiftLeft || op == ttShiftLeftAssign ||
  10247. op == ttBitShiftRight || op == ttShiftRightLAssign ||
  10248. op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  10249. {
  10250. // Don't permit object to primitive conversion, since we don't know which integer type is the correct one
  10251. if( lctx->type.dataType.IsObject() )
  10252. {
  10253. asCString str;
  10254. str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format().AddressOf());
  10255. Error(str, node);
  10256. // Set an integer value and allow the compiler to continue
  10257. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
  10258. return;
  10259. }
  10260. // Convert left hand operand to integer if it's not already one
  10261. asCDataType to = lctx->type.dataType;
  10262. if( lctx->type.dataType.IsUnsignedType() &&
  10263. lctx->type.dataType.GetSizeInMemoryBytes() < 4 )
  10264. {
  10265. to = asCDataType::CreatePrimitive(ttUInt, false);
  10266. }
  10267. else if( !lctx->type.dataType.IsUnsignedType() )
  10268. {
  10269. asCDataType to;
  10270. if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10271. to.SetTokenType(ttInt64);
  10272. else
  10273. to.SetTokenType(ttInt);
  10274. }
  10275. // Do the actual conversion
  10276. int l = int(reservedVariables.GetLength());
  10277. rctx->bc.GetVarsUsed(reservedVariables);
  10278. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV, true);
  10279. reservedVariables.SetLength(l);
  10280. // Verify that the conversion was successful
  10281. if( lctx->type.dataType != to )
  10282. {
  10283. asCString str;
  10284. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  10285. Error(str, node);
  10286. }
  10287. // Right operand must be 32bit uint
  10288. l = int(reservedVariables.GetLength());
  10289. lctx->bc.GetVarsUsed(reservedVariables);
  10290. ImplicitConversion(rctx, asCDataType::CreatePrimitive(ttUInt, true), node, asIC_IMPLICIT_CONV, true);
  10291. reservedVariables.SetLength(l);
  10292. if( !rctx->type.dataType.IsUnsignedType() )
  10293. {
  10294. asCString str;
  10295. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), "uint");
  10296. Error(str, node);
  10297. }
  10298. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  10299. if( !isConstant )
  10300. {
  10301. ConvertToVariableNotIn(lctx, rctx);
  10302. ConvertToVariableNotIn(rctx, lctx);
  10303. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  10304. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  10305. if( op == ttShiftLeftAssign || op == ttShiftRightLAssign || op == ttShiftRightAAssign )
  10306. {
  10307. // Compound assignments execute the right hand value first
  10308. MergeExprBytecode(ctx, rctx);
  10309. MergeExprBytecode(ctx, lctx);
  10310. }
  10311. else
  10312. {
  10313. MergeExprBytecode(ctx, lctx);
  10314. MergeExprBytecode(ctx, rctx);
  10315. }
  10316. ProcessDeferredParams(ctx);
  10317. asEBCInstr instruction = asBC_BSLL;
  10318. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10319. {
  10320. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  10321. instruction = asBC_BSLL;
  10322. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  10323. instruction = asBC_BSRL;
  10324. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  10325. instruction = asBC_BSRA;
  10326. }
  10327. else
  10328. {
  10329. if( op == ttBitShiftLeft || op == ttShiftLeftAssign )
  10330. instruction = asBC_BSLL64;
  10331. else if( op == ttBitShiftRight || op == ttShiftRightLAssign )
  10332. instruction = asBC_BSRL64;
  10333. else if( op == ttBitShiftRightArith || op == ttShiftRightAAssign )
  10334. instruction = asBC_BSRA64;
  10335. }
  10336. // Do the operation
  10337. int a = AllocateVariable(lctx->type.dataType, true);
  10338. int b = lctx->type.stackOffset;
  10339. int c = rctx->type.stackOffset;
  10340. ctx->bc.InstrW_W_W(instruction, a, b, c);
  10341. ctx->type.SetVariable(lctx->type.dataType, a, true);
  10342. }
  10343. else
  10344. {
  10345. if( lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10346. {
  10347. asDWORD v = 0;
  10348. if( op == ttBitShiftLeft )
  10349. v = lctx->type.dwordValue << rctx->type.dwordValue;
  10350. else if( op == ttBitShiftRight )
  10351. v = lctx->type.dwordValue >> rctx->type.dwordValue;
  10352. else if( op == ttBitShiftRightArith )
  10353. v = lctx->type.intValue >> rctx->type.dwordValue;
  10354. ctx->type.SetConstantDW(lctx->type.dataType, v);
  10355. }
  10356. else
  10357. {
  10358. asQWORD v = 0;
  10359. if( op == ttBitShiftLeft )
  10360. v = lctx->type.qwordValue << rctx->type.dwordValue;
  10361. else if( op == ttBitShiftRight )
  10362. v = lctx->type.qwordValue >> rctx->type.dwordValue;
  10363. else if( op == ttBitShiftRightArith )
  10364. v = asINT64(lctx->type.qwordValue) >> rctx->type.dwordValue;
  10365. ctx->type.SetConstantQW(lctx->type.dataType, v);
  10366. }
  10367. }
  10368. }
  10369. }
  10370. void asCCompiler::CompileComparisonOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  10371. {
  10372. // Both operands must be of the same type
  10373. // Implicitly convert the operands to a number type
  10374. asCDataType to;
  10375. // If either operand is a non-primitive then use the primitive type
  10376. if( !lctx->type.dataType.IsPrimitive() )
  10377. to.SetTokenType(rctx->type.dataType.GetTokenType());
  10378. else if( !rctx->type.dataType.IsPrimitive() )
  10379. to.SetTokenType(lctx->type.dataType.GetTokenType());
  10380. else if( lctx->type.dataType.IsDoubleType() || rctx->type.dataType.IsDoubleType() )
  10381. to.SetTokenType(ttDouble);
  10382. else if( lctx->type.dataType.IsFloatType() || rctx->type.dataType.IsFloatType() )
  10383. to.SetTokenType(ttFloat);
  10384. else if( lctx->type.dataType.GetSizeInMemoryDWords() == 2 || rctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10385. {
  10386. // Convert to int64 if both are signed or if one is non-constant and signed
  10387. if( (lctx->type.dataType.IsIntegerType() && rctx->type.dataType.IsIntegerType()) ||
  10388. (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  10389. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  10390. to.SetTokenType(ttInt64);
  10391. else
  10392. to.SetTokenType(ttUInt64);
  10393. }
  10394. else
  10395. {
  10396. // Convert to int32 if both are signed or if one is non-constant and signed
  10397. if( (lctx->type.dataType.IsIntegerType() && rctx->type.dataType.IsIntegerType()) ||
  10398. (lctx->type.dataType.IsIntegerType() && !lctx->type.isConstant) ||
  10399. (rctx->type.dataType.IsIntegerType() && !rctx->type.isConstant) )
  10400. to.SetTokenType(ttInt);
  10401. else if( lctx->type.dataType.IsUnsignedType() || rctx->type.dataType.IsUnsignedType() )
  10402. to.SetTokenType(ttUInt);
  10403. else if( lctx->type.dataType.IsBooleanType() || rctx->type.dataType.IsBooleanType() )
  10404. to.SetTokenType(ttBool);
  10405. }
  10406. // If doing an operation with double constant and float variable, the constant should be converted to float
  10407. if( (lctx->type.isConstant && lctx->type.dataType.IsDoubleType() && !rctx->type.isConstant && rctx->type.dataType.IsFloatType()) ||
  10408. (rctx->type.isConstant && rctx->type.dataType.IsDoubleType() && !lctx->type.isConstant && lctx->type.dataType.IsFloatType()) )
  10409. to.SetTokenType(ttFloat);
  10410. asASSERT( to.GetTokenType() != ttUnrecognizedToken );
  10411. // Do we have a mismatch between the sign of the operand?
  10412. bool signMismatch = false;
  10413. for( int n = 0; !signMismatch && n < 2; n++ )
  10414. {
  10415. asSExprContext *op = n ? rctx : lctx;
  10416. if( op->type.dataType.IsUnsignedType() != to.IsUnsignedType() )
  10417. {
  10418. // We have a mismatch, unless the value is a literal constant and the conversion won't affect its value
  10419. signMismatch = true;
  10420. if( op->type.isConstant )
  10421. {
  10422. if( op->type.dataType.GetTokenType() == ttUInt64 || op->type.dataType.GetTokenType() == ttInt64 )
  10423. {
  10424. if( !(op->type.qwordValue & (asQWORD(1)<<63)) )
  10425. signMismatch = false;
  10426. }
  10427. else
  10428. {
  10429. if( !(op->type.dwordValue & (1<<31)) )
  10430. signMismatch = false;
  10431. }
  10432. // It's not necessary to check for floats or double, because if
  10433. // it was then the types for the conversion will never be unsigned
  10434. }
  10435. }
  10436. }
  10437. // Check for signed/unsigned mismatch
  10438. if( signMismatch )
  10439. Warning(TXT_SIGNED_UNSIGNED_MISMATCH, node);
  10440. // Do the actual conversion
  10441. int l = int(reservedVariables.GetLength());
  10442. rctx->bc.GetVarsUsed(reservedVariables);
  10443. if( lctx->type.dataType.IsReference() )
  10444. ConvertToVariable(lctx);
  10445. if( rctx->type.dataType.IsReference() )
  10446. ConvertToVariable(rctx);
  10447. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  10448. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  10449. reservedVariables.SetLength(l);
  10450. // Verify that the conversion was successful
  10451. bool ok = true;
  10452. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  10453. {
  10454. asCString str;
  10455. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  10456. Error(str, node);
  10457. ok = false;
  10458. }
  10459. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  10460. {
  10461. asCString str;
  10462. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  10463. Error(str, node);
  10464. ok = false;
  10465. }
  10466. if( !ok )
  10467. {
  10468. // It wasn't possible to get two valid operands, so we just return
  10469. // a boolean result and let the compiler continue.
  10470. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  10471. return;
  10472. }
  10473. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  10474. int op = node->tokenType;
  10475. if( !isConstant )
  10476. {
  10477. if( to.IsBooleanType() )
  10478. {
  10479. int op = node->tokenType;
  10480. if( op == ttEqual || op == ttNotEqual )
  10481. {
  10482. // Must convert to temporary variable, because we are changing the value before comparison
  10483. ConvertToTempVariableNotIn(lctx, rctx);
  10484. ConvertToTempVariableNotIn(rctx, lctx);
  10485. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  10486. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  10487. // Make sure they are equal if not false
  10488. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  10489. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  10490. MergeExprBytecode(ctx, lctx);
  10491. MergeExprBytecode(ctx, rctx);
  10492. ProcessDeferredParams(ctx);
  10493. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  10494. int b = lctx->type.stackOffset;
  10495. int c = rctx->type.stackOffset;
  10496. if( op == ttEqual )
  10497. {
  10498. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  10499. ctx->bc.Instr(asBC_TZ);
  10500. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  10501. }
  10502. else if( op == ttNotEqual )
  10503. {
  10504. ctx->bc.InstrW_W(asBC_CMPi,b,c);
  10505. ctx->bc.Instr(asBC_TNZ);
  10506. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  10507. }
  10508. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  10509. }
  10510. else
  10511. {
  10512. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  10513. Error(TXT_ILLEGAL_OPERATION, node);
  10514. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), 0);
  10515. }
  10516. }
  10517. else
  10518. {
  10519. ConvertToVariableNotIn(lctx, rctx);
  10520. ConvertToVariableNotIn(rctx, lctx);
  10521. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  10522. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  10523. MergeExprBytecode(ctx, lctx);
  10524. MergeExprBytecode(ctx, rctx);
  10525. ProcessDeferredParams(ctx);
  10526. asEBCInstr iCmp = asBC_CMPi, iT = asBC_TZ;
  10527. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10528. iCmp = asBC_CMPi;
  10529. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10530. iCmp = asBC_CMPu;
  10531. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10532. iCmp = asBC_CMPi64;
  10533. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10534. iCmp = asBC_CMPu64;
  10535. else if( lctx->type.dataType.IsFloatType() )
  10536. iCmp = asBC_CMPf;
  10537. else if( lctx->type.dataType.IsDoubleType() )
  10538. iCmp = asBC_CMPd;
  10539. else
  10540. asASSERT(false);
  10541. if( op == ttEqual )
  10542. iT = asBC_TZ;
  10543. else if( op == ttNotEqual )
  10544. iT = asBC_TNZ;
  10545. else if( op == ttLessThan )
  10546. iT = asBC_TS;
  10547. else if( op == ttLessThanOrEqual )
  10548. iT = asBC_TNP;
  10549. else if( op == ttGreaterThan )
  10550. iT = asBC_TP;
  10551. else if( op == ttGreaterThanOrEqual )
  10552. iT = asBC_TNS;
  10553. int a = AllocateVariable(asCDataType::CreatePrimitive(ttBool, true), true);
  10554. int b = lctx->type.stackOffset;
  10555. int c = rctx->type.stackOffset;
  10556. ctx->bc.InstrW_W(iCmp, b, c);
  10557. ctx->bc.Instr(iT);
  10558. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  10559. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  10560. }
  10561. }
  10562. else
  10563. {
  10564. if( to.IsBooleanType() )
  10565. {
  10566. int op = node->tokenType;
  10567. if( op == ttEqual || op == ttNotEqual )
  10568. {
  10569. // Make sure they are equal if not false
  10570. if( lctx->type.dwordValue != 0 ) lctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  10571. if( rctx->type.dwordValue != 0 ) rctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  10572. asDWORD v = 0;
  10573. if( op == ttEqual )
  10574. {
  10575. v = lctx->type.intValue - rctx->type.intValue;
  10576. if( v == 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  10577. }
  10578. else if( op == ttNotEqual )
  10579. {
  10580. v = lctx->type.intValue - rctx->type.intValue;
  10581. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  10582. }
  10583. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), v);
  10584. }
  10585. else
  10586. {
  10587. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  10588. Error(TXT_ILLEGAL_OPERATION, node);
  10589. }
  10590. }
  10591. else
  10592. {
  10593. int i = 0;
  10594. if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10595. {
  10596. int v = lctx->type.intValue - rctx->type.intValue;
  10597. if( v < 0 ) i = -1;
  10598. if( v > 0 ) i = 1;
  10599. }
  10600. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10601. {
  10602. asDWORD v1 = lctx->type.dwordValue;
  10603. asDWORD v2 = rctx->type.dwordValue;
  10604. if( v1 < v2 ) i = -1;
  10605. if( v1 > v2 ) i = 1;
  10606. }
  10607. else if( lctx->type.dataType.IsIntegerType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10608. {
  10609. asINT64 v = asINT64(lctx->type.qwordValue) - asINT64(rctx->type.qwordValue);
  10610. if( v < 0 ) i = -1;
  10611. if( v > 0 ) i = 1;
  10612. }
  10613. else if( lctx->type.dataType.IsUnsignedType() && lctx->type.dataType.GetSizeInMemoryDWords() == 2 )
  10614. {
  10615. asQWORD v1 = lctx->type.qwordValue;
  10616. asQWORD v2 = rctx->type.qwordValue;
  10617. if( v1 < v2 ) i = -1;
  10618. if( v1 > v2 ) i = 1;
  10619. }
  10620. else if( lctx->type.dataType.IsFloatType() )
  10621. {
  10622. float v = lctx->type.floatValue - rctx->type.floatValue;
  10623. if( v < 0 ) i = -1;
  10624. if( v > 0 ) i = 1;
  10625. }
  10626. else if( lctx->type.dataType.IsDoubleType() )
  10627. {
  10628. double v = lctx->type.doubleValue - rctx->type.doubleValue;
  10629. if( v < 0 ) i = -1;
  10630. if( v > 0 ) i = 1;
  10631. }
  10632. if( op == ttEqual )
  10633. i = (i == 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10634. else if( op == ttNotEqual )
  10635. i = (i != 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10636. else if( op == ttLessThan )
  10637. i = (i < 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10638. else if( op == ttLessThanOrEqual )
  10639. i = (i <= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10640. else if( op == ttGreaterThan )
  10641. i = (i > 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10642. else if( op == ttGreaterThanOrEqual )
  10643. i = (i >= 0 ? VALUE_OF_BOOLEAN_TRUE : 0);
  10644. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), i);
  10645. }
  10646. }
  10647. }
  10648. void asCCompiler::PushVariableOnStack(asSExprContext *ctx, bool asReference)
  10649. {
  10650. // Put the result on the stack
  10651. if( asReference )
  10652. {
  10653. ctx->bc.InstrSHORT(asBC_PSF, ctx->type.stackOffset);
  10654. ctx->type.dataType.MakeReference(true);
  10655. }
  10656. else
  10657. {
  10658. if( ctx->type.dataType.GetSizeInMemoryDWords() == 1 )
  10659. ctx->bc.InstrSHORT(asBC_PshV4, ctx->type.stackOffset);
  10660. else
  10661. ctx->bc.InstrSHORT(asBC_PshV8, ctx->type.stackOffset);
  10662. }
  10663. }
  10664. void asCCompiler::CompileBooleanOperator(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  10665. {
  10666. // Both operands must be booleans
  10667. asCDataType to;
  10668. to.SetTokenType(ttBool);
  10669. // Do the actual conversion
  10670. int l = int(reservedVariables.GetLength());
  10671. rctx->bc.GetVarsUsed(reservedVariables);
  10672. lctx->bc.GetVarsUsed(reservedVariables);
  10673. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  10674. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  10675. reservedVariables.SetLength(l);
  10676. // Verify that the conversion was successful
  10677. if( !lctx->type.dataType.IsBooleanType() )
  10678. {
  10679. asCString str;
  10680. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), "bool");
  10681. Error(str, node);
  10682. // Force the conversion to allow compilation to proceed
  10683. lctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  10684. }
  10685. if( !rctx->type.dataType.IsBooleanType() )
  10686. {
  10687. asCString str;
  10688. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), "bool");
  10689. Error(str, node);
  10690. // Force the conversion to allow compilation to proceed
  10691. rctx->type.SetConstantB(asCDataType::CreatePrimitive(ttBool, true), true);
  10692. }
  10693. bool isConstant = lctx->type.isConstant && rctx->type.isConstant;
  10694. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  10695. // What kind of operator is it?
  10696. int op = node->tokenType;
  10697. if( op == ttXor )
  10698. {
  10699. if( !isConstant )
  10700. {
  10701. // Must convert to temporary variable, because we are changing the value before comparison
  10702. ConvertToTempVariableNotIn(lctx, rctx);
  10703. ConvertToTempVariableNotIn(rctx, lctx);
  10704. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  10705. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  10706. // Make sure they are equal if not false
  10707. lctx->bc.InstrWORD(asBC_NOT, lctx->type.stackOffset);
  10708. rctx->bc.InstrWORD(asBC_NOT, rctx->type.stackOffset);
  10709. MergeExprBytecode(ctx, lctx);
  10710. MergeExprBytecode(ctx, rctx);
  10711. ProcessDeferredParams(ctx);
  10712. int a = AllocateVariable(ctx->type.dataType, true);
  10713. int b = lctx->type.stackOffset;
  10714. int c = rctx->type.stackOffset;
  10715. ctx->bc.InstrW_W_W(asBC_BXOR,a,b,c);
  10716. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  10717. }
  10718. else
  10719. {
  10720. // Make sure they are equal if not false
  10721. #if AS_SIZEOF_BOOL == 1
  10722. if( lctx->type.byteValue != 0 ) lctx->type.byteValue = VALUE_OF_BOOLEAN_TRUE;
  10723. if( rctx->type.byteValue != 0 ) rctx->type.byteValue = VALUE_OF_BOOLEAN_TRUE;
  10724. asBYTE v = 0;
  10725. v = lctx->type.byteValue - rctx->type.byteValue;
  10726. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  10727. ctx->type.isConstant = true;
  10728. ctx->type.byteValue = v;
  10729. #else
  10730. if( lctx->type.dwordValue != 0 ) lctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  10731. if( rctx->type.dwordValue != 0 ) rctx->type.dwordValue = VALUE_OF_BOOLEAN_TRUE;
  10732. asDWORD v = 0;
  10733. v = lctx->type.intValue - rctx->type.intValue;
  10734. if( v != 0 ) v = VALUE_OF_BOOLEAN_TRUE; else v = 0;
  10735. ctx->type.isConstant = true;
  10736. ctx->type.dwordValue = v;
  10737. #endif
  10738. }
  10739. }
  10740. else if( op == ttAnd ||
  10741. op == ttOr )
  10742. {
  10743. if( !isConstant )
  10744. {
  10745. // If or-operator and first value is 1 the second value shouldn't be calculated
  10746. // if and-operator and first value is 0 the second value shouldn't be calculated
  10747. ConvertToVariable(lctx);
  10748. ReleaseTemporaryVariable(lctx->type, &lctx->bc);
  10749. MergeExprBytecode(ctx, lctx);
  10750. int offset = AllocateVariable(asCDataType::CreatePrimitive(ttBool, false), true);
  10751. int label1 = nextLabel++;
  10752. int label2 = nextLabel++;
  10753. ctx->bc.InstrSHORT(asBC_CpyVtoR4, lctx->type.stackOffset);
  10754. ctx->bc.Instr(asBC_ClrHi);
  10755. if( op == ttAnd )
  10756. {
  10757. ctx->bc.InstrDWORD(asBC_JNZ, label1);
  10758. ctx->bc.InstrW_DW(asBC_SetV4, (asWORD)offset, 0);
  10759. ctx->bc.InstrINT(asBC_JMP, label2);
  10760. }
  10761. else if( op == ttOr )
  10762. {
  10763. ctx->bc.InstrDWORD(asBC_JZ, label1);
  10764. #if AS_SIZEOF_BOOL == 1
  10765. ctx->bc.InstrSHORT_B(asBC_SetV1, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  10766. #else
  10767. ctx->bc.InstrSHORT_DW(asBC_SetV4, (short)offset, VALUE_OF_BOOLEAN_TRUE);
  10768. #endif
  10769. ctx->bc.InstrINT(asBC_JMP, label2);
  10770. }
  10771. ctx->bc.Label((short)label1);
  10772. ConvertToVariable(rctx);
  10773. ReleaseTemporaryVariable(rctx->type, &rctx->bc);
  10774. rctx->bc.InstrW_W(asBC_CpyVtoV4, offset, rctx->type.stackOffset);
  10775. MergeExprBytecode(ctx, rctx);
  10776. ctx->bc.Label((short)label2);
  10777. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, false), offset, true);
  10778. }
  10779. else
  10780. {
  10781. #if AS_SIZEOF_BOOL == 1
  10782. asBYTE v = 0;
  10783. if( op == ttAnd )
  10784. v = lctx->type.byteValue && rctx->type.byteValue;
  10785. else if( op == ttOr )
  10786. v = lctx->type.byteValue || rctx->type.byteValue;
  10787. // Remember the result
  10788. ctx->type.isConstant = true;
  10789. ctx->type.byteValue = v;
  10790. #else
  10791. asDWORD v = 0;
  10792. if( op == ttAnd )
  10793. v = lctx->type.dwordValue && rctx->type.dwordValue;
  10794. else if( op == ttOr )
  10795. v = lctx->type.dwordValue || rctx->type.dwordValue;
  10796. // Remember the result
  10797. ctx->type.isConstant = true;
  10798. ctx->type.dwordValue = v;
  10799. #endif
  10800. }
  10801. }
  10802. }
  10803. void asCCompiler::CompileOperatorOnHandles(asCScriptNode *node, asSExprContext *lctx, asSExprContext *rctx, asSExprContext *ctx)
  10804. {
  10805. // Process the property accessor as get
  10806. ProcessPropertyGetAccessor(lctx, node);
  10807. ProcessPropertyGetAccessor(rctx, node);
  10808. DetermineSingleFunc(lctx, node);
  10809. DetermineSingleFunc(rctx, node);
  10810. // Make sure lctx doesn't end up with a variable used in rctx
  10811. if( lctx->type.isTemporary && rctx->bc.IsVarUsed(lctx->type.stackOffset) )
  10812. {
  10813. asCArray<int> vars;
  10814. rctx->bc.GetVarsUsed(vars);
  10815. int offset = AllocateVariable(lctx->type.dataType, true);
  10816. rctx->bc.ExchangeVar(lctx->type.stackOffset, offset);
  10817. ReleaseTemporaryVariable(offset, 0);
  10818. }
  10819. // Warn if not both operands are explicit handles or null handles
  10820. if( (node->tokenType == ttEqual || node->tokenType == ttNotEqual) &&
  10821. ((!(lctx->type.isExplicitHandle || lctx->type.IsNullConstant()) && !(lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE))) ||
  10822. (!(rctx->type.isExplicitHandle || rctx->type.IsNullConstant()) && !(rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_IMPLICIT_HANDLE)))) )
  10823. {
  10824. Warning(TXT_HANDLE_COMPARISON, node);
  10825. }
  10826. // If one of the operands is a value type used as handle, we should look for the opEquals method
  10827. if( ((lctx->type.dataType.GetObjectType() && (lctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE)) ||
  10828. (rctx->type.dataType.GetObjectType() && (rctx->type.dataType.GetObjectType()->flags & asOBJ_ASHANDLE))) &&
  10829. (node->tokenType == ttEqual || node->tokenType == ttIs ||
  10830. node->tokenType == ttNotEqual || node->tokenType == ttNotIs) )
  10831. {
  10832. // TODO: Should evaluate which of the two have the best match. If both have equal match, the first version should be used
  10833. // Find the matching opEquals method
  10834. int r = CompileOverloadedDualOperator2(node, "opEquals", lctx, rctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  10835. if( r == 0 )
  10836. {
  10837. // Try again by switching the order of the operands
  10838. r = CompileOverloadedDualOperator2(node, "opEquals", rctx, lctx, ctx, true, asCDataType::CreatePrimitive(ttBool, false));
  10839. }
  10840. if( r == 1 )
  10841. {
  10842. if( node->tokenType == ttNotEqual || node->tokenType == ttNotIs )
  10843. ctx->bc.InstrSHORT(asBC_NOT, ctx->type.stackOffset);
  10844. // Success, don't continue
  10845. return;
  10846. }
  10847. else if( r == 0 )
  10848. {
  10849. // Couldn't find opEquals method
  10850. Error(TXT_NO_APPROPRIATE_OPEQUALS, node);
  10851. }
  10852. // Compiler error, don't continue
  10853. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  10854. return;
  10855. }
  10856. // Implicitly convert null to the other type
  10857. asCDataType to;
  10858. if( lctx->type.IsNullConstant() )
  10859. to = rctx->type.dataType;
  10860. else if( rctx->type.IsNullConstant() )
  10861. to = lctx->type.dataType;
  10862. else
  10863. {
  10864. // TODO: Use the common base type
  10865. to = lctx->type.dataType;
  10866. }
  10867. // Need to pop the value if it is a null constant
  10868. if( lctx->type.IsNullConstant() )
  10869. lctx->bc.Instr(asBC_PopPtr);
  10870. if( rctx->type.IsNullConstant() )
  10871. rctx->bc.Instr(asBC_PopPtr);
  10872. // Convert both sides to explicit handles
  10873. to.MakeHandle(true);
  10874. to.MakeReference(false);
  10875. if( !to.IsObjectHandle() )
  10876. {
  10877. // Compiler error, don't continue
  10878. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  10879. ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttBool, true), true);
  10880. return;
  10881. }
  10882. // Do the conversion
  10883. ImplicitConversion(lctx, to, node, asIC_IMPLICIT_CONV);
  10884. ImplicitConversion(rctx, to, node, asIC_IMPLICIT_CONV);
  10885. // Both operands must be of the same type
  10886. // Verify that the conversion was successful
  10887. if( !lctx->type.dataType.IsEqualExceptConst(to) )
  10888. {
  10889. asCString str;
  10890. str.Format(TXT_NO_CONVERSION_s_TO_s, lctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  10891. Error(str, node);
  10892. }
  10893. if( !rctx->type.dataType.IsEqualExceptConst(to) )
  10894. {
  10895. asCString str;
  10896. str.Format(TXT_NO_CONVERSION_s_TO_s, rctx->type.dataType.Format().AddressOf(), to.Format().AddressOf());
  10897. Error(str, node);
  10898. }
  10899. // Make sure it really is handles that are being compared
  10900. if( !lctx->type.dataType.IsObjectHandle() )
  10901. {
  10902. Error(TXT_OPERANDS_MUST_BE_HANDLES, node);
  10903. }
  10904. ctx->type.Set(asCDataType::CreatePrimitive(ttBool, true));
  10905. int op = node->tokenType;
  10906. if( op == ttEqual || op == ttNotEqual || op == ttIs || op == ttNotIs )
  10907. {
  10908. // If the object handle already is in a variable we must manually pop it from the stack
  10909. if( lctx->type.isVariable )
  10910. lctx->bc.Instr(asBC_PopPtr);
  10911. if( rctx->type.isVariable )
  10912. rctx->bc.Instr(asBC_PopPtr);
  10913. // TODO: runtime optimize: don't do REFCPY
  10914. ConvertToVariableNotIn(lctx, rctx);
  10915. ConvertToVariable(rctx);
  10916. MergeExprBytecode(ctx, lctx);
  10917. MergeExprBytecode(ctx, rctx);
  10918. int a = AllocateVariable(ctx->type.dataType, true);
  10919. int b = lctx->type.stackOffset;
  10920. int c = rctx->type.stackOffset;
  10921. ctx->bc.InstrW_W(asBC_CmpPtr, b, c);
  10922. if( op == ttEqual || op == ttIs )
  10923. ctx->bc.Instr(asBC_TZ);
  10924. else if( op == ttNotEqual || op == ttNotIs )
  10925. ctx->bc.Instr(asBC_TNZ);
  10926. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)a);
  10927. ctx->type.SetVariable(asCDataType::CreatePrimitive(ttBool, true), a, true);
  10928. ReleaseTemporaryVariable(lctx->type, &ctx->bc);
  10929. ReleaseTemporaryVariable(rctx->type, &ctx->bc);
  10930. ProcessDeferredParams(ctx);
  10931. }
  10932. else
  10933. {
  10934. // TODO: Use TXT_ILLEGAL_OPERATION_ON
  10935. Error(TXT_ILLEGAL_OPERATION, node);
  10936. }
  10937. }
  10938. void asCCompiler::PerformFunctionCall(int funcId, asSExprContext *ctx, bool isConstructor, asCArray<asSExprContext*> *args, asCObjectType *objType, bool useVariable, int varOffset, int funcPtrVar)
  10939. {
  10940. asCScriptFunction *descr = builder->GetFunctionDescription(funcId);
  10941. // A shared object may not call non-shared functions
  10942. if( outFunc->IsShared() && !descr->IsShared() )
  10943. {
  10944. asCString msg;
  10945. msg.Format(TXT_SHARED_CANNOT_CALL_NON_SHARED_FUNC_s, descr->GetDeclarationStr().AddressOf());
  10946. Error(msg, ctx->exprNode);
  10947. }
  10948. // Check if the function is private
  10949. if( descr->isPrivate && descr->GetObjectType() != outFunc->GetObjectType() )
  10950. {
  10951. asCString msg;
  10952. msg.Format(TXT_PRIVATE_METHOD_CALL_s, descr->GetDeclarationStr().AddressOf());
  10953. Error(msg, ctx->exprNode);
  10954. }
  10955. int argSize = descr->GetSpaceNeededForArguments();
  10956. if( descr->objectType && descr->returnType.IsReference() &&
  10957. !(ctx->type.isVariable || ctx->type.isTemporary) &&
  10958. (ctx->type.dataType.IsObjectHandle() || ctx->type.dataType.SupportHandles()) &&
  10959. !(ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_SCOPED) &&
  10960. !(ctx->type.dataType.GetObjectType()->GetFlags() & asOBJ_ASHANDLE) )
  10961. {
  10962. // The class method we're calling is returning a reference, which may be to a member of the object.
  10963. // In order to guarantee the lifetime of the reference, we must hold a local reference to the object.
  10964. // TODO: runtime optimize: This can be avoided for local variables (non-handles) as they have a well defined life time
  10965. int tempRef = AllocateVariable(ctx->type.dataType, true);
  10966. ctx->bc.InstrSHORT(asBC_PSF, (short)tempRef);
  10967. ctx->bc.InstrPTR(asBC_REFCPY, ctx->type.dataType.GetObjectType());
  10968. // Add the release of this reference, as a deferred expression
  10969. asSDeferredParam deferred;
  10970. deferred.origExpr = 0;
  10971. deferred.argInOutFlags = asTM_INREF;
  10972. deferred.argNode = 0;
  10973. deferred.argType.SetVariable(ctx->type.dataType, tempRef, true);
  10974. ctx->deferredParams.PushLast(deferred);
  10975. // Forget the current type
  10976. ctx->type.SetDummy();
  10977. }
  10978. if( isConstructor )
  10979. {
  10980. // Sometimes the value types are allocated on the heap,
  10981. // which is when this way of constructing them is used.
  10982. asASSERT(useVariable == false);
  10983. ctx->bc.Alloc(asBC_ALLOC, objType, descr->id, argSize+AS_PTR_SIZE);
  10984. // The instruction has already moved the returned object to the variable
  10985. ctx->type.Set(asCDataType::CreatePrimitive(ttVoid, false));
  10986. ctx->type.isLValue = false;
  10987. // Clean up arguments
  10988. if( args )
  10989. AfterFunctionCall(funcId, *args, ctx, false);
  10990. ProcessDeferredParams(ctx);
  10991. return;
  10992. }
  10993. else
  10994. {
  10995. if( descr->objectType )
  10996. argSize += AS_PTR_SIZE;
  10997. // If the function returns an object by value the address of the location
  10998. // where the value should be stored is passed as an argument too
  10999. if( descr->DoesReturnOnStack() )
  11000. {
  11001. argSize += AS_PTR_SIZE;
  11002. }
  11003. // TODO: runtime optimize: If it is known that a class method cannot be overridden the call
  11004. // should be made with asBC_CALL as it is faster. Examples where this
  11005. // is known is for example finalled methods where the class doesn't derive
  11006. // from any other, or even non-finalled methods but where it is known
  11007. // at compile time the true type of the object. The first should be
  11008. // quite easy to determine, but the latter will be quite complex and possibly
  11009. // not worth it.
  11010. if( descr->funcType == asFUNC_IMPORTED )
  11011. ctx->bc.Call(asBC_CALLBND , descr->id, argSize);
  11012. // TODO: Maybe we need two different byte codes
  11013. else if( descr->funcType == asFUNC_INTERFACE || descr->funcType == asFUNC_VIRTUAL )
  11014. ctx->bc.Call(asBC_CALLINTF, descr->id, argSize);
  11015. else if( descr->funcType == asFUNC_SCRIPT )
  11016. ctx->bc.Call(asBC_CALL , descr->id, argSize);
  11017. else if( descr->funcType == asFUNC_SYSTEM )
  11018. ctx->bc.Call(asBC_CALLSYS , descr->id, argSize);
  11019. else if( descr->funcType == asFUNC_FUNCDEF )
  11020. ctx->bc.CallPtr(asBC_CallPtr, funcPtrVar, argSize);
  11021. }
  11022. if( descr->returnType.IsObject() && !descr->returnType.IsReference() )
  11023. {
  11024. int returnOffset = 0;
  11025. if( descr->DoesReturnOnStack() )
  11026. {
  11027. asASSERT( useVariable );
  11028. // The variable was allocated before the function was called
  11029. returnOffset = varOffset;
  11030. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  11031. // The variable was initialized by the function, so we need to mark it as initialized here
  11032. ctx->bc.ObjInfo(varOffset, asOBJ_INIT);
  11033. }
  11034. else
  11035. {
  11036. if( useVariable )
  11037. {
  11038. // Use the given variable
  11039. returnOffset = varOffset;
  11040. ctx->type.SetVariable(descr->returnType, returnOffset, false);
  11041. }
  11042. else
  11043. {
  11044. // Allocate a temporary variable for the returned object
  11045. // The returned object will actually be allocated on the heap, so
  11046. // we must force the allocation of the variable to do the same
  11047. returnOffset = AllocateVariable(descr->returnType, true, !descr->returnType.IsObjectHandle());
  11048. ctx->type.SetVariable(descr->returnType, returnOffset, true);
  11049. }
  11050. // Move the pointer from the object register to the temporary variable
  11051. ctx->bc.InstrSHORT(asBC_STOREOBJ, (short)returnOffset);
  11052. }
  11053. ctx->type.dataType.MakeReference(IsVariableOnHeap(returnOffset));
  11054. ctx->type.isLValue = false; // It is a reference, but not an lvalue
  11055. // Clean up arguments
  11056. if( args )
  11057. AfterFunctionCall(funcId, *args, ctx, false);
  11058. ProcessDeferredParams(ctx);
  11059. ctx->bc.InstrSHORT(asBC_PSF, (short)returnOffset);
  11060. }
  11061. else if( descr->returnType.IsReference() )
  11062. {
  11063. asASSERT(useVariable == false);
  11064. // We cannot clean up the arguments yet, because the
  11065. // reference might be pointing to one of them.
  11066. if( args )
  11067. AfterFunctionCall(funcId, *args, ctx, true);
  11068. // Do not process the output parameters yet, because it
  11069. // might invalidate the returned reference
  11070. // If the context holds a variable that needs cleanup
  11071. // store it as a deferred parameter so it will be cleaned up
  11072. // afterwards.
  11073. if( ctx->type.isTemporary )
  11074. {
  11075. asSDeferredParam defer;
  11076. defer.argNode = 0;
  11077. defer.argType = ctx->type;
  11078. defer.argInOutFlags = asTM_INOUTREF;
  11079. defer.origExpr = 0;
  11080. ctx->deferredParams.PushLast(defer);
  11081. }
  11082. ctx->type.Set(descr->returnType);
  11083. if( !descr->returnType.IsPrimitive() )
  11084. {
  11085. ctx->bc.Instr(asBC_PshRPtr);
  11086. if( descr->returnType.IsObject() &&
  11087. !descr->returnType.IsObjectHandle() )
  11088. {
  11089. // We are getting the pointer to the object
  11090. // not a pointer to a object variable
  11091. ctx->type.dataType.MakeReference(false);
  11092. }
  11093. }
  11094. // A returned reference can be used as lvalue
  11095. ctx->type.isLValue = true;
  11096. }
  11097. else
  11098. {
  11099. asASSERT(useVariable == false);
  11100. if( descr->returnType.GetSizeInMemoryBytes() )
  11101. {
  11102. // Allocate a temporary variable to hold the value, but make sure
  11103. // the temporary variable isn't used in any of the deferred arguments
  11104. int l = int(reservedVariables.GetLength());
  11105. for( asUINT n = 0; args && n < args->GetLength(); n++ )
  11106. {
  11107. asSExprContext *expr = (*args)[n]->origExpr;
  11108. if( expr )
  11109. expr->bc.GetVarsUsed(reservedVariables);
  11110. }
  11111. int offset = AllocateVariable(descr->returnType, true);
  11112. reservedVariables.SetLength(l);
  11113. ctx->type.SetVariable(descr->returnType, offset, true);
  11114. // Move the value from the return register to the variable
  11115. if( descr->returnType.GetSizeOnStackDWords() == 1 )
  11116. ctx->bc.InstrSHORT(asBC_CpyRtoV4, (short)offset);
  11117. else if( descr->returnType.GetSizeOnStackDWords() == 2 )
  11118. ctx->bc.InstrSHORT(asBC_CpyRtoV8, (short)offset);
  11119. }
  11120. else
  11121. ctx->type.Set(descr->returnType);
  11122. ctx->type.isLValue = false;
  11123. // Clean up arguments
  11124. if( args )
  11125. AfterFunctionCall(funcId, *args, ctx, false);
  11126. ProcessDeferredParams(ctx);
  11127. }
  11128. }
  11129. // This only merges the bytecode, but doesn't modify the type of the final context
  11130. void asCCompiler::MergeExprBytecode(asSExprContext *before, asSExprContext *after)
  11131. {
  11132. before->bc.AddCode(&after->bc);
  11133. for( asUINT n = 0; n < after->deferredParams.GetLength(); n++ )
  11134. {
  11135. before->deferredParams.PushLast(after->deferredParams[n]);
  11136. after->deferredParams[n].origExpr = 0;
  11137. }
  11138. after->deferredParams.SetLength(0);
  11139. }
  11140. // This merges both bytecode and the type of the final context
  11141. void asCCompiler::MergeExprBytecodeAndType(asSExprContext *before, asSExprContext *after)
  11142. {
  11143. MergeExprBytecode(before, after);
  11144. before->type = after->type;
  11145. before->property_get = after->property_get;
  11146. before->property_set = after->property_set;
  11147. before->property_const = after->property_const;
  11148. before->property_handle = after->property_handle;
  11149. before->property_ref = after->property_ref;
  11150. before->property_arg = after->property_arg;
  11151. before->exprNode = after->exprNode;
  11152. before->methodName = after->methodName;
  11153. before->enumValue = after->enumValue;
  11154. after->property_arg = 0;
  11155. // Do not copy the origExpr member
  11156. }
  11157. void asCCompiler::FilterConst(asCArray<int> &funcs, bool removeConst)
  11158. {
  11159. if( funcs.GetLength() == 0 ) return;
  11160. // This is only done for object methods
  11161. asCScriptFunction *desc = builder->GetFunctionDescription(funcs[0]);
  11162. if( desc->objectType == 0 ) return;
  11163. // Check if there are any non-const matches
  11164. asUINT n;
  11165. bool foundNonConst = false;
  11166. for( n = 0; n < funcs.GetLength(); n++ )
  11167. {
  11168. desc = builder->GetFunctionDescription(funcs[n]);
  11169. if( desc->isReadOnly != removeConst )
  11170. {
  11171. foundNonConst = true;
  11172. break;
  11173. }
  11174. }
  11175. if( foundNonConst )
  11176. {
  11177. // Remove all const methods
  11178. for( n = 0; n < funcs.GetLength(); n++ )
  11179. {
  11180. desc = builder->GetFunctionDescription(funcs[n]);
  11181. if( desc->isReadOnly == removeConst )
  11182. {
  11183. if( n == funcs.GetLength() - 1 )
  11184. funcs.PopLast();
  11185. else
  11186. funcs[n] = funcs.PopLast();
  11187. n--;
  11188. }
  11189. }
  11190. }
  11191. }
  11192. END_AS_NAMESPACE
  11193. #endif // AS_NO_COMPILER