ctranslator.bmx 235 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854
  1. ' Copyright (c) 2013-2026 Bruce A Henderson
  2. '
  3. ' Based on the public domain Monkey "trans" by Mark Sibly
  4. '
  5. ' This software is provided 'as-is', without any express or implied
  6. ' warranty. In no event will the authors be held liable for any damages
  7. ' arising from the use of this software.
  8. '
  9. ' Permission is granted to anyone to use this software for any purpose,
  10. ' including commercial applications, and to alter it and redistribute it
  11. ' freely, subject to the following restrictions:
  12. '
  13. ' 1. The origin of this software must not be misrepresented; you must not
  14. ' claim that you wrote the original software. If you use this software
  15. ' in a product, an acknowledgment in the product documentation would be
  16. ' appreciated but is not required.
  17. '
  18. ' 2. Altered source versions must be plainly marked as such, and must not be
  19. ' misrepresented as being the original software.
  20. '
  21. ' 3. This notice may not be removed or altered from any source
  22. ' distribution.
  23. '
  24. SuperStrict
  25. Import "parser.bmx"
  26. Type TCTranslator Extends TTranslator
  27. 'Field stringConstCount:Int
  28. Field prefix:String
  29. Field reserved_methods:String = ",New,Delete,ToString,Compare,SendMessage,HashCode,Equals,_reserved3_,".ToLower()
  30. Method New()
  31. _trans = Self
  32. End Method
  33. Method TransSPointer$( ty:TType, withVar:Int = False )
  34. Local p:String
  35. If ty
  36. If withVar And (ty._flags & TType.T_VAR) Then
  37. p:+ "*"
  38. End If
  39. If ty._flags & TType.T_PTR Then
  40. p:+ "*"
  41. Else If ty._flags & TType.T_PTRPTR Then
  42. p:+ "**"
  43. Else If ty._flags & TType.T_PTRPTRPTR Then
  44. p:+ "***"
  45. End If
  46. End If
  47. Return p
  48. End Method
  49. Method TransArrayType$( ty:TType)
  50. Local p:String = TransSPointer(ty)
  51. If TBoolType( ty ) Return "~q" + p + "i~q"
  52. If TByteType( ty ) Return "~q" + p + "b~q"
  53. If TShortType( ty ) Return "~q" + p + "s~q"
  54. If TIntType( ty ) Return "~q" + p + "i~q"
  55. If TUIntType( ty ) Return "~q" + p + "u~q"
  56. If TFloatType( ty ) Return "~q" + p + "f~q"
  57. If TDoubleType( ty ) Return "~q" + p + "d~q"
  58. If TLongType( ty ) Return "~q" + p + "l~q"
  59. If TULongType( ty ) Return "~q" + p + "y~q"
  60. If TLongIntType( ty ) Return "~q" + p + "v~q"
  61. If TULongIntType( ty ) Return "~q" + p + "e~q"
  62. If TSizeTType( ty ) Return "~q" + p + "t~q"
  63. If TWParamType( ty ) Return "~q" + p + "w~q"
  64. If TLParamType( ty ) Return "~q" + p + "x~q"
  65. If TStringType( ty ) Return "~q$~q"
  66. If TInt128Type( ty ) Return "~q" + p + "j~q"
  67. If TFloat128Type( ty ) Return "~q" + p + "k~q"
  68. If TDouble128Type( ty ) Return "~q" + p + "m~q"
  69. If TFloat64Type( ty ) Return "~q" + p + "h~q"
  70. If TArrayType( ty ) Then
  71. Local s:String = "["
  72. For Local i:Int = 0 Until TArrayType( ty ).dims - 1
  73. s:+ ","
  74. Next
  75. s:+ "]"
  76. s:+ TransArrayType(TArrayType( ty ).elemType)
  77. Return Enquote(s.Replace("~q", ""))
  78. End If
  79. If TObjectType( ty ) Then
  80. If TObjectType( ty ).classdecl.IsStruct()
  81. Return "~q" + p + "@" + TObjectType(ty).classDecl.ident + "~q"
  82. Else
  83. If Not TObjectType( ty ).classdecl.IsExtern()
  84. Return "~q:" + TObjectType(ty).classDecl.ident + "~q"
  85. Else
  86. If TObjectType( ty ).classdecl.IsInterface() Then
  87. Return "~q" + p + "*#" + TObjectType(ty).classDecl.ident + "~q"
  88. ' ElseIf TObjectType( ty ).classdecl.IsStruct()
  89. ' Return "~q" + p + "@" + TObjectType(ty).classDecl.ident + "~q"
  90. Else
  91. Return "~q" + p + "#" + TObjectType(ty).classDecl.ident + "~q"
  92. End If
  93. End If
  94. End If
  95. End If
  96. If TFunctionPtrType( ty ) Return "~q(~q"
  97. If TEnumType( ty ) Return "~q/" + TEnumType(ty).decl.ident + "~q"
  98. End Method
  99. Method TransDefDataType$( ty:TType)
  100. If TByteType( ty ) Return "~qb~q"
  101. If TShortType( ty ) Return "~qs~q"
  102. If TIntType( ty ) Return "~qi~q"
  103. If TUIntType( ty ) Return "~qu~q"
  104. If TFloatType( ty ) Return "~qf~q"
  105. If TDoubleType( ty ) Return "~qd~q"
  106. If TLongType( ty ) Return "~ql~q"
  107. If TULongType( ty ) Return "~qy~q"
  108. If TSizeTType( ty ) Return "~qz~q"
  109. If TLongIntType( ty ) Return "~qv~q"
  110. If TULongIntType( ty ) Return "~qe~q"
  111. If TStringType( ty ) Return "~q$~q"
  112. If TWParamType( ty ) Return "~qw~q"
  113. If TLParamType( ty ) Return "~qx~q"
  114. End Method
  115. Method TransDefDataConversion$(ty:TType)
  116. If TByteType( ty ) Return "bbConvertToInt"
  117. If TShortType( ty ) Return "bbConvertToInt"
  118. If TIntType( ty ) Return "bbConvertToInt"
  119. If TUIntType( ty ) Return "bbConvertToUInt"
  120. If TFloatType( ty ) Return "bbConvertToFloat"
  121. If TDoubleType( ty ) Return "bbConvertToDouble"
  122. If TLongType( ty ) Return "bbConvertToLong"
  123. If TULongType( ty ) Return "bbConvertToULong"
  124. If TSizeTType( ty ) Return "bbConvertToSizet"
  125. If TLongIntType( ty ) Return "bbConvertToLongInt"
  126. If TULongIntType( ty ) Return "bbConvertToULongInt"
  127. If TStringType( ty ) Return "bbConvertToString"
  128. End Method
  129. Method TransDefDataUnionType$(ty:TType)
  130. If TByteType( ty ) Return "b"
  131. If TShortType( ty ) Return "s"
  132. If TIntType( ty ) Return "i"
  133. If TUIntType( ty ) Return "u"
  134. If TFloatType( ty ) Return "f"
  135. If TDoubleType( ty ) Return "d"
  136. If TLongType( ty ) Return "l"
  137. If TULongType( ty ) Return "y"
  138. If TSizeTType( ty ) Return "z"
  139. If TLongIntType( ty ) Return "v"
  140. If TULongIntType( ty ) Return "e"
  141. If TWParamType( ty ) Return "w"
  142. If TLParamType( ty ) Return "x"
  143. If TStringType( ty ) Return "t"
  144. End Method
  145. Method TransCifType$(ty:TType)
  146. Local p:String = TransSPointer(ty)
  147. If Not p Then
  148. If TByteType( ty ) Return "&ffi_type_uint8"
  149. If TShortType( ty ) Return "&ffi_type_uint16"
  150. If TIntType( ty ) Return "&ffi_type_sint32"
  151. If TUIntType( ty ) Return "&ffi_type_uint32"
  152. If TFloatType( ty ) Return "&ffi_type_float"
  153. If TDoubleType( ty ) Return "&ffi_type_double"
  154. If TLongType( ty ) Return "&ffi_type_sint64"
  155. If TULongType( ty ) Return "&ffi_type_uint64"
  156. If TSizeTType( ty ) Then
  157. If WORD_SIZE = 8 Then
  158. Return "&ffi_type_uint32"
  159. Else
  160. Return "&ffi_type_uint64"
  161. End If
  162. End If
  163. If TWParamType( ty ) Return "&ffi_type_sint32"
  164. If TLParamType( ty ) Return "&ffi_type_sint64"
  165. End If
  166. ' everything else is a pointer...
  167. Return "&ffi_type_pointer"
  168. End Method
  169. Method TransDebugScopeType$(ty:TType)
  170. Local p:String = TransSPointer(ty)
  171. If ty._flags & TType.T_VAR Then
  172. p = "&" + p
  173. End If
  174. If TByteType( ty ) Return p + "b"
  175. If TShortType( ty ) Return p + "s"
  176. If TIntType( ty ) Return p + "i"
  177. If TUIntType( ty ) Return p + "u"
  178. If TFloatType( ty ) Return p + "f"
  179. If TDoubleType( ty ) Return p + "d"
  180. If TLongType( ty ) Return p + "l"
  181. If TULongType( ty ) Return p + "y"
  182. If TSizeTType( ty ) Return p + "t"
  183. If TLongIntType( ty ) Return p + "v"
  184. If TULongIntType( ty ) Return p + "e"
  185. If TWParamType( ty ) Return p + "W"
  186. If TLParamType( ty ) Return p + "X"
  187. If TInt128Type( ty ) Return p + "j"
  188. If TFloat128Type( ty ) Return p + "k"
  189. If TDouble128Type( ty ) Return p + "m"
  190. If TFloat64Type( ty ) Return p + "h"
  191. If TStringType( ty ) Return p + "$"
  192. If TArrayType( ty ) Then
  193. Local s:String = p + "["
  194. If TArrayType( ty ).isStatic Then
  195. s :+ TArrayType( ty ).length
  196. Else
  197. For Local i:Int = 0 Until TArrayType( ty ).dims - 1
  198. s:+ ","
  199. Next
  200. End If
  201. s:+ "]"
  202. Return s + TransDebugScopeType(TArrayType( ty ).elemType)
  203. End If
  204. If TObjectType( ty ) Then
  205. If TObjectType( ty ).classdecl.IsStruct() Then
  206. Return p + "@" + TObjectType(ty).classDecl.ident
  207. Else If Not TObjectType( ty ).classdecl.IsExtern()
  208. Return p + ":" + TObjectType( ty ).classDecl.ident
  209. Else
  210. If TObjectType( ty ).classdecl.IsInterface() Then
  211. Return p + "*#" + TObjectType(ty).classDecl.ident
  212. Else
  213. Return p + "#" + TObjectType(ty).classDecl.ident
  214. End If
  215. End If
  216. End If
  217. If TFunctionPtrType( ty ) Then
  218. Local func:TFuncDecl = TFunctionPtrType( ty ).func
  219. Local s:String = p + "("
  220. For Local i:Int = 0 Until func.argDecls.length
  221. If i Then
  222. s :+ ","
  223. End If
  224. s :+ TransDebugScopeType(func.argDecls[i].ty)
  225. Next
  226. Return s + ")" + TransDebugScopeType(func.retType)
  227. End If
  228. If TEnumType( ty ) Then
  229. Return p + "/" + TEnumType( ty ).decl.ident
  230. End If
  231. End Method
  232. Method TransType$( ty:TType, ident:String, fpReturnTypeFunctionArgs:String = Null, fpReturnTypeClassFunc:Int = False)
  233. Local p:String = TransSPointer(ty, True)
  234. If TVoidType( ty ) Or Not ty Then
  235. Return "void"
  236. End If
  237. If TBoolType( ty ) Return "BBINT" + p
  238. If TByteType( ty ) Return "BBBYTE" + p
  239. If TShortType( ty ) Return "BBSHORT" + p
  240. If TIntType( ty ) Return "BBINT" + p
  241. If TUIntType( ty ) Return "BBUINT" + p
  242. If TFloatType( ty ) Return "BBFLOAT" + p
  243. If TDoubleType( ty ) Return "BBDOUBLE" + p
  244. If TLongType( ty ) Return "BBLONG" + p
  245. If TULongType( ty ) Return "BBULONG" + p
  246. If TSizeTType( ty ) Return "BBSIZET" + p
  247. If TLongIntType( ty ) Return "BBLONGINT" + p
  248. If TULongIntType( ty ) Return "BBULONGINT" + p
  249. If TWParamType( ty ) Return "WPARAM" + p
  250. If TLParamType( ty ) Return "LPARAM" + p
  251. If TInt128Type( ty ) Return "BBINT128" + p
  252. If TFloat128Type( ty ) Return "BBFLOAT128" + p
  253. If TDouble128Type( ty ) Return "BBDOUBLE128" + p
  254. If TFloat64Type( ty ) Return "BBFLOAT64" + p
  255. If TStringType( ty ) Then
  256. If ty._flags & TType.T_CHAR_PTR Then
  257. Return "BBBYTE *"
  258. Else If ty._flags & TType.T_SHORT_PTR Then
  259. Return "BBSHORT *"
  260. End If
  261. Return "BBSTRING" + p
  262. End If
  263. If TArrayType( ty ) Then
  264. If TArrayType( ty ).isStatic Then
  265. Return TransType(TArrayType( ty ).elemType, ident) + p
  266. Else
  267. Return "BBARRAY" + p
  268. End If
  269. End If
  270. If TObjectType( ty ) Then
  271. Return TransObject(TObjectType(ty).classdecl) + p
  272. End If
  273. If TFunctionPtrType( ty ) Then
  274. TFunctionPtrType(ty).func.Semant
  275. Local api:String
  276. If TFunctionPtrType(ty).func.attrs & DECL_API_STDCALL Then
  277. api = " __stdcall "
  278. End If
  279. Local args:String
  280. For Local arg:TArgDecl = EachIn TFunctionPtrType(ty).func.argDecls
  281. arg.Semant()
  282. If args Then
  283. args :+ ","
  284. End If
  285. args :+ TransType(arg.ty, "")
  286. Next
  287. Local ret:String = ""
  288. If fpReturnTypeFunctionArgs Then
  289. ret = Bra(fpReturnTypeFunctionArgs)
  290. End If
  291. If fpReturnTypeClassFunc Then
  292. ' typedef for function pointer return type
  293. Return ident + "x" + Bra(api + p +"* " + ident) + Bra(args)
  294. Else
  295. ' if a function F returns another function (let's call it G),
  296. ' then C syntax requires the declaration of F to be nested into that of the type of G
  297. ' e.g. "Function F:RetG(ArgG)(ArgF)" in BlitzMax becomes "RetG(* F(ArgF) )(ArgG)" in C
  298. ' solution: use "* F(ArgF)" as an ident to generate a declaration for G
  299. ' the result will be the declaration for F
  300. Local callable:String = Bra(api + p +"* " + ident + ret)
  301. If TFunctionPtrType(TFunctionPtrType(ty).func.retType) Then
  302. If Not args Then args = " " ' make sure the parentheses aren't ommited even if the parameter list is empty
  303. Return TransType(TFunctionPtrType(ty).func.retType, callable, args)
  304. Else
  305. Local retTypeStr:String = TransType(TFunctionPtrType(ty).func.retType, "")
  306. Return retTypeStr + callable + Bra(args)
  307. End If
  308. End If
  309. End If
  310. If TExternObjectType( ty ) Return "struct " + TExternObjectType( ty ).classDecl.munged + p
  311. If TEnumType( ty ) Return TransType( TEnumType( ty ).decl.ty, ident ) + p
  312. InternalErr "TCTranslator.TransType"
  313. End Method
  314. Method TransIfcType$( ty:TType, isSuperStrict:Int = False )
  315. Local p:String = TransSPointer(ty)
  316. If ty And (ty._flags & TType.T_VAR) Then
  317. p :+ " Var"
  318. End If
  319. If Not ty Then
  320. If opt_issuperstrict Or isSuperStrict Then
  321. Return p
  322. Else
  323. Return "%" + p
  324. End If
  325. End If
  326. If TVoidType( ty ) Then
  327. Return p
  328. End If
  329. If TByteType( ty ) Return "@" + p
  330. If TShortType( ty ) Return "@@" + p
  331. If TIntType( ty ) Return "%" + p
  332. If TUIntType( ty ) Return "|" + p
  333. If TFloatType( ty ) Return "#" + p
  334. If TDoubleType( ty ) Return "!" + p
  335. If TLongType( ty ) Return "%%" + p
  336. If TULongType( ty ) Return "||" + p
  337. If TSizeTType( ty ) Return "%z" + p
  338. If TLongIntType( ty ) Return "%v" + p
  339. If TULongIntType( ty ) Return "%e" + p
  340. If TWParamType( ty ) Return "%w" + p
  341. If TLParamType( ty ) Return "%x" + p
  342. If TInt128Type( ty ) Return "%j" + p
  343. If TFloat128Type( ty ) Return "!k" + p
  344. If TDouble128Type( ty ) Return "!m" + p
  345. If TFloat64Type( ty ) Return "!h" + p
  346. If TStringType( ty ) Then
  347. If ty._flags & TType.T_CHAR_PTR Then
  348. Return "$z"
  349. Else If ty._flags & TType.T_SHORT_PTR Then
  350. Return "$w"
  351. End If
  352. Return "$" + p
  353. End If
  354. If TArrayType( ty ) Then
  355. Local s:String = TransIfcType(TArrayType( ty ).elemType) + "&["
  356. If TArrayType( ty ).isStatic Then
  357. s :+ TArrayType( ty ).length
  358. Else
  359. For Local i:Int = 0 Until TArrayType( ty ).dims - 1
  360. s:+ ","
  361. Next
  362. End If
  363. Return s + "]" + p
  364. End If
  365. If TObjectType( ty ) Then
  366. Local t:String = ":"
  367. If TObjectType(ty).classDecl.IsExtern() Then
  368. If TObjectType(ty).classDecl.IsInterface() Then
  369. t = "??"
  370. ElseIf TObjectType(ty).classDecl.IsStruct() Then
  371. t = "~~"
  372. Else
  373. t = "?"
  374. End If
  375. End If
  376. Local cdecl:TClassDecl = TObjectType(ty).classDecl
  377. ' find first type in hierarchy that isn't private
  378. While cdecl.IsPrivate() And cdecl.superClass <> Null
  379. cdecl = cdecl.superClass
  380. Wend
  381. Local args:String
  382. If cdecl.instArgs And cdecl.instArgs.length Then
  383. args = "<"
  384. For Local i:Int = 0 Until cdecl.instArgs.length
  385. If i Then
  386. args :+ ","
  387. End If
  388. args :+ cdecl.instArgs[i].ToString()
  389. Next
  390. args :+ ">"
  391. End If
  392. Return t + cdecl.ident + args + p
  393. End If
  394. If TFunctionPtrType( ty ) Then
  395. Local t:String = TransIfcType(TFunctionPtrType(ty).func.retType, TFunctionPtrType(ty).func.ModuleScope().IsSuperStrict()) + TransIfcArgs(TFunctionPtrType(ty).func)
  396. If TFunctionPtrType( ty ).func.attrs & DECL_API_STDCALL Then
  397. t :+ "W"
  398. End If
  399. Return t
  400. End If
  401. If TExternObjectType( ty ) Return ":" + TExternObjectType(ty).classDecl.ident + p
  402. If TEnumType( ty ) Then
  403. Return "/" + TEnumType( ty ).decl.ident + p
  404. End If
  405. InternalErr "TCTranslator.TransIfcType"
  406. End Method
  407. Method TransRefType$( ty:TType, ident:String )
  408. Return TransType( ty, ident )
  409. End Method
  410. Method TransDebugScopeModifiers:String(decl:TDecl)
  411. Local modifiers:String
  412. If decl.IsAbstract() Then modifiers :+ "A"
  413. If decl.IsFinal() Then modifiers :+ "F"
  414. If decl.IsExtern() Then modifiers :+ "E"
  415. If decl.IsPrivate() Then modifiers :+ "P"
  416. If decl.IsProtected() Then modifiers :+ "R"
  417. If modifiers Then modifiers = "'" + modifiers
  418. Return modifiers
  419. End Method
  420. Method TransValue$( ty:TType,value$, isStructInit:Int = False )
  421. If value
  422. If IsPointerType(ty, 0, TType.T_POINTER) Return value
  423. If TBoolType( ty ) Return "1"
  424. If TShortType( ty ) Return value
  425. If TIntType( ty ) Return value
  426. If TUIntType( ty ) Return value+"U"
  427. If TLongType( ty ) Return value+"LL"
  428. If TULongType( ty ) Return value+"ULL"
  429. If TSizeTType( ty ) Return value
  430. If TLongIntType( ty ) Return value
  431. If TULongIntType( ty ) Return value
  432. If TWParamType( ty ) Return value
  433. If TLParamType( ty ) Return value
  434. If TInt128Type( ty ) Return value
  435. If TFloatType( ty ) Then
  436. If value = "nan" Or value.StartsWith("1.#IND0000") Then
  437. Return "bbPOSNANf"
  438. Else If value="-nan" Or value.StartsWith("-1.#IND0000") Then
  439. Return "bbNEGNANf"
  440. Else If value = "inf" Or value.StartsWith("1.#INF0000") Then
  441. Return "bbPOSINFf"
  442. Else If value = "-inf" Or value.StartsWith("-1.#INF0000") Then
  443. Return "bbNEGINFf"
  444. Else
  445. If value.ToLower().Find("e")>=0 Then
  446. Return value
  447. End If
  448. If value.Find(".") < 0 Then
  449. value :+ ".0"
  450. End If
  451. Return value+"f"
  452. End If
  453. End If
  454. If TDoubleType( ty ) Or TFloat128Type(ty) Or TDouble128Type(ty) Or TFloat64Type(ty) Then
  455. If value = "nan" Or value.StartsWith("1.#IND0000") Then
  456. Return "bbPOSNANd"
  457. Else If value="-nan" Or value.StartsWith("-1.#IND0000") Then
  458. Return "bbNEGNANd"
  459. Else If value = "inf" Or value.StartsWith("1.#INF0000") Then
  460. Return "bbPOSINFd"
  461. Else If value = "-inf" Or value.StartsWith("-1.#INF0000") Then
  462. Return "bbNEGINFd"
  463. Else
  464. If value.ToLower().Find("e") >=0 Then
  465. Return value
  466. End If
  467. If value.Find(".") < 0 Then
  468. value :+ ".0"
  469. End If
  470. Return value
  471. End If
  472. End If
  473. If TStringType( ty ) Return TransStringConst(value )
  474. If TByteType( ty ) Return value
  475. If TEnumType( ty ) Return value
  476. Else
  477. If TBoolType( ty ) Return "0"
  478. If TIntrinsicType( ty) Then
  479. If IsPointerType(ty, 0, TType.T_POINTER) Then
  480. Return "0"
  481. Else
  482. Return "{}"
  483. End If
  484. End If
  485. If TNumericType( ty ) Return "0" ' numeric and pointers
  486. If TStringType( ty ) Then
  487. If isStructInit Then
  488. Return "&bbEmptyString"
  489. Else
  490. Return Bra("&bbEmptyString")
  491. End If
  492. End If
  493. If TArrayType( ty ) Then
  494. If isStructInit Then
  495. If TArrayType( ty ).isStatic Then
  496. Local t:String = "{"
  497. Local count:Int = 0
  498. For Local i:Int = 0 Until Int(TArrayType( ty ).length)
  499. count :+ 1
  500. If i Then
  501. t :+ ","
  502. End If
  503. If count = 100 Then
  504. t :+ "~n"
  505. count = 0
  506. End If
  507. t :+ TransValue(TArrayType( ty ).elemType, "", True)
  508. Next
  509. Return t + "}"
  510. Else
  511. Return "&bbEmptyArray"
  512. End If
  513. Else
  514. Return Bra("&bbEmptyArray")
  515. End If
  516. End If
  517. If TObjectType( ty ) Then
  518. If TObjectType( ty ).classDecl.IsExtern() Or TObjectType( ty ).classDecl.IsStruct() Then
  519. If TObjectType( ty ).classDecl.IsInterface() Or IsPointerType(ty,0,TType.T_POINTER) Or (Not TObjectType( ty ).classDecl.IsStruct()) Then
  520. Return "0"
  521. Else
  522. If TObjectType( ty ).classDecl.IsStruct() Then
  523. Local t:String
  524. If Not isStructInit Then
  525. t = "((" + TransType(ty, "") + "){"
  526. Else
  527. t = "{"
  528. End If
  529. Local fields:Int
  530. For Local f:TFieldDecl = EachIn TObjectType( ty ).classDecl.Decls()
  531. If fields Then
  532. t :+ ","
  533. End If
  534. fields = True
  535. t :+ TransValue(f.ty, "", True)
  536. Next
  537. If Not isStructInit Then
  538. t :+ "})"
  539. Else
  540. t :+ "}"
  541. End If
  542. Return t
  543. Else
  544. Return "{}"
  545. End If
  546. End If
  547. Else
  548. If isStructInit Then
  549. Return "&bbNullObject"
  550. Else
  551. Return Bra(Bra(TransType(ty, "*")) + "&bbNullObject")
  552. End If
  553. End If
  554. End If
  555. If TFunctionPtrType( ty) Return Bra(TransType(ty, "")) + "(&brl_blitz_NullFunctionError)"
  556. If TEnumType( ty ) Then
  557. If TEnumType( ty ).decl.isFlags Then
  558. Return "0"
  559. Else
  560. Return TEnumType( ty ).decl.values[0].Value()
  561. End If
  562. End If
  563. EndIf
  564. InternalErr "TCTranslator.TransValue"
  565. End Method
  566. Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null, objectNew:Int = False )
  567. 'If decl.ident="AddS" DebugStop
  568. Local t:TStringBuffer = New TStringBuffer(128)
  569. If objParam And (decl.IsMethod() Or decl.isCtor()) And ((Not decl.IsExtern()) Or (decl.IsExtern() And TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct())) Then
  570. ' object cast to match param type
  571. If objectNew Then
  572. t.Append( Bra("BBClass *") )
  573. Else
  574. If TClassDecl(decl.scope) Then
  575. t.Append( Bra(TransObject(TClassDecl(decl.scope).GetLatestFuncDecl(decl).scope, TClassDecl(decl.scope).IsStruct())) )
  576. End If
  577. End If
  578. t.Append( objParam )
  579. End If
  580. For Local i:Int=0 Until decl.argDecls.Length
  581. Local argDecl:TArgDecl = decl.argDecls[i]
  582. Local ty:TType = TArgDecl(argDecl.actual).ty
  583. If t.Length() t.Append( "," )
  584. If i < args.length
  585. Local arg:TExpr = args[i]
  586. ' object cast to match param type
  587. If TObjectType(ty) And Not TObjectType(ty).classDecl.IsStruct() And Not argDecl.castTo Then
  588. Local fdecl:TFuncDecl = decl
  589. If TClassDecl(decl.scope) Then
  590. fdecl = TClassDecl(decl.scope).GetLatestFuncDecl(decl)
  591. End If
  592. t.Append( Bra(TransObject(TObjectType(TArgDecl(fdecl.argDecls[i].actual).ty).classDecl)) )
  593. End If
  594. Local varRef:String
  595. If TNullExpr(arg) Then
  596. t.Append( TransValue(ty, Null) )
  597. Continue
  598. Else If TIndexExpr(arg) And (ty._flags & TType.T_VAR)Then
  599. varRef = "&"
  600. Else If TStringType(ty) And (ty._flags & TType.T_VAR) Then
  601. If TCastExpr(arg) And TStringType(TCastExpr(arg).expr.exprType) Then
  602. varRef = "&"
  603. End If
  604. Else If TArrayType(ty) And (ty._flags & TType.T_VAR) Then
  605. If (TVarExpr(arg) And TArrayType(TVarExpr(arg).exprType) Or (TMemberVarExpr(arg) And TArrayType(TMemberVarExpr(arg).exprType))) And Not (arg.exprType._flags & TType.T_VAR) Then
  606. varRef = "&"
  607. End If
  608. Else If TObjectType(ty) And (ty._flags & TType.T_VAR) Then
  609. If (TVarExpr(arg) Or TMemberVarExpr(arg)) And TObjectType(arg.exprType) And Not (arg.exprType._flags & TType.T_VAR) Then
  610. varRef = "&"
  611. End If
  612. Else If TFunctionPtrType(ty) Or IsPointerType(ty, TType.T_BYTE) Then
  613. If TFunctionPtrType(ty) And (ty._flags & TType.T_VAR) Then
  614. varRef = "&"
  615. End If
  616. If TInvokeExpr(arg) And Not TInvokeExpr(arg).decl.IsMethod() Then
  617. t.Append( varRef )
  618. If IsPointerType(ty, TType.T_BYTE) Then
  619. t.Append( TInvokeExpr(arg).Trans() )
  620. Else
  621. ' need to test scopes to see if we need to use the current instance's function or not
  622. ' use the "actual", not the copy we made for the function pointer.
  623. Local fdecl:TFuncDecl = TFuncDecl(TInvokeExpr(arg).decl.actual)
  624. If Not fdecl.munged Then
  625. MungDecl fdecl
  626. TInvokeExpr(arg).decl.munged = fdecl.munged
  627. End If
  628. If TClassDecl(fdecl.scope) Then
  629. ' current scope is related to function scope?
  630. If _env.ClassScope() And _env.FuncScope() And _env.FuncScope().IsMethod() Then
  631. If _env.ClassScope().ExtendsClass(TClassDecl(fdecl.scope)) Then
  632. Local scope:TScopeDecl = _env.scope
  633. Local obj:String = Bra("struct " + scope.munged + "_obj*")
  634. Local class:String = "o->clas"
  635. t.Append( class ).Append( "->f_" ).Append( fdecl.ident ).Append( MangleMethod(fdecl) )
  636. Else
  637. t.Append( fdecl.munged )
  638. End If
  639. Else
  640. t.Append( fdecl.munged )
  641. End If
  642. Else
  643. t.Append( fdecl.munged )
  644. End If
  645. End If
  646. Continue
  647. End If
  648. ' some cases where we are passing a function pointer via a void* parameter.
  649. If TCastExpr(arg) And TInvokeExpr(TCastExpr(arg).expr) And Not TInvokeExpr(TCastExpr(arg).expr).invokedWithBraces Then
  650. t.Append( varRef )
  651. If Not TInvokeExpr(TCastExpr(arg).expr).decl.munged Then
  652. t.Append( TInvokeExpr(TCastExpr(arg).expr).decl.actual.munged )
  653. Else
  654. t.Append( TInvokeExpr(TCastExpr(arg).expr).decl.munged )
  655. End If
  656. Continue
  657. End If
  658. If TCastExpr(arg) And TVarExpr(TCastExpr(arg).expr) Then
  659. t.Append( varRef )
  660. t.Append( TransCast(TFunctionPtrType(ty)) ).Append( Bra(arg.Trans()) )
  661. Continue
  662. End If
  663. ' Object -> Byte Ptr
  664. If IsPointerType(ty, TType.T_BYTE) And TObjectType(arg.exprType) Then
  665. t.Append( varRef ).Append( Bra("bbObjectToFieldOffset" + Bra("(BBObject*)" + arg.Trans())) )
  666. Continue
  667. End If
  668. Else If IsNumericType(ty) Then
  669. If TObjectType(arg.exprType) 'And TObjectType(args[i].exprType).classDecl = TClassDecl.nullObjectClass Then
  670. err "NULL"
  671. t.Append( "0" )
  672. Continue
  673. End If
  674. Else If TEnumType(ty) And (ty._flags & TType.T_VAR) Then
  675. If (TVarExpr(arg) Or TMemberVarExpr(arg)) And TEnumType(arg.exprType) And Not (arg.exprType._flags & TType.T_VAR) Then
  676. varRef = "&"
  677. End If
  678. End If
  679. If argDecl.castTo Then
  680. If argDecl.castTo.Find("*") >= 0 Then
  681. t.Append( Bra(argDecl.castTo) ).Append( varRef ).Append( arg.Trans() )
  682. Else
  683. t.Append( varRef ).Append( Bra(argDecl.castTo) ).Append( arg.Trans() )
  684. End If
  685. Else
  686. t.Append( varRef )
  687. Local tc:String = TransTemplateCast( ty,arg.exprType,arg.Trans() )
  688. ' *sigh*
  689. ' if var is going to var, remove any leading dereference character.
  690. ' rather hacky. Would be better to cast variable to varptr during semanting (well done if you can work out where!)
  691. If arg.exprType.EqualsType( ty.ActualType() ) And (ty._flags & TType.T_VAR) And ( (arg.exprType._flags & TType.T_VAR) Or (TSelfExpr(arg) And TObjectType(arg.exprType) And TObjectType(arg.exprType).classdecl.IsStruct())) Then
  692. If tc.startswith("*") Then
  693. tc = tc[1..]
  694. End If
  695. End If
  696. t.Append( tc )
  697. 't:+TransTemplateCast( ty,args[i].exprType,args[i].Trans() )
  698. End If
  699. Else
  700. argDecl.Semant()
  701. ' default values
  702. Local init:TExpr = argDecl.init
  703. If init Then
  704. If TConstExpr(init) Then
  705. If TObjectType(TConstExpr(init).exprType) Then
  706. 't:+"NULLNULLNULL"
  707. ' And TNullDecl(TObjectType(TConstExpr(init).exprType).classDecl)) Or (TConstExpr(init).value = "bbNullObject") Then
  708. If TStringType(argDecl.ty) Then
  709. t.Append( "&bbEmptyString" )
  710. Else If TArrayType(argDecl.ty) Then
  711. t.Append( "&bbEmptyArray" )
  712. Else
  713. t.Append( "&bbNullObject" )
  714. End If
  715. Else
  716. t.Append( argDecl.init.Trans() )
  717. End If
  718. Else If TFunctionPtrType(ty) Then
  719. If TInvokeExpr(init) Then
  720. t.Append( TInvokeExpr(init).decl.munged )
  721. End If
  722. Else
  723. t.Append( argDecl.init.Trans() )
  724. End If
  725. End If
  726. End If
  727. Next
  728. Return Bra(t.ToString())
  729. End Method
  730. ' translate to C cast
  731. Method TransCast:String(funcPtr:TFunctionPtrType)
  732. If Not funcPtr Then
  733. Return ""
  734. End If
  735. Local s:TStringBuffer = New TStringBuffer.Create("(")
  736. s.Append( TransType(funcPtr.func.retType, "") )
  737. s.Append( "(*)(" )
  738. For Local i:Int=0 Until funcPtr.func.argDecls.Length
  739. If i Then
  740. s.Append( "," )
  741. End If
  742. s.Append( TransType(funcPtr.func.argDecls[i].ty, "") )
  743. Next
  744. s.Append( ")" )
  745. s.Append( ")" )
  746. Return s.ToString()
  747. End Method
  748. Method TransArgsTypes$( args:TExpr[],declArgTypes:TType[])
  749. Local t:TStringBuffer = New TStringBuffer(128)
  750. For Local i:Int=0 Until args.Length
  751. If t.Length() > 0 Then t.Append( "," )
  752. t.Append( TransTemplateCast( declArgTypes[i],args[i].exprType,args[i].Trans() ) )
  753. Next
  754. Return Bra(t.ToString())
  755. End Method
  756. Method TransPtrCast$( ty:TType,src:TType,expr$,cast$ )
  757. If IsPointerType(ty, 0, TType.T_POINTER | TType.T_VARPTR | TType.T_VAR) Or TFunctionPtrType(ty) Then
  758. ' TODO : pointer stuff
  759. If TNullType(src) Return TransValue(ty, Null)
  760. Return expr
  761. 'Return Bra(Bra(TransType(ty, "")) + expr)
  762. End If
  763. 'If expr = "NULL" DebugStop
  764. ' If TIntType(ty) And TStringType(src) Then
  765. 'DebugStop
  766. ' Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(src).cDecl.munged)
  767. ' End If
  768. If TNullType(src)
  769. Return TransValue(ty, Null)
  770. End If
  771. If TStringType(ty) And TObjectType(src) Then
  772. If Not TStringType(ty).cDecl Then
  773. ty.Semant()
  774. End If
  775. 'If TNullDecl(TObjectType(src).classDecl) Then
  776. ' Return "&bbEmptyString"
  777. 'End If
  778. Return Bra("(BBString *)bbObjectStringcast" + Bra("(BBOBJECT)" + expr ))
  779. End If
  780. 'If TArrayType(ty) And TObjectType(src) Then
  781. ' If TNullDecl(TObjectType(src).classDecl) Then
  782. ' Return "&bbEmptyArray"
  783. ' End If
  784. 'End If
  785. If TVarPtrType(src) And TNumericType(ty) Then
  786. Return "*" + expr
  787. End If
  788. If TIntType(ty) And TStringType(src) Then
  789. Return Bra(expr + " != &bbEmptyString")
  790. End If
  791. ' If TIntType(ty) And TObjectType(src) Then
  792. ' Return Bra(expr + " != &bbNullObject")
  793. ' End If
  794. If TObjectType(ty) And TStringType(src) Then
  795. Return expr
  796. End If
  797. If Not TObjectType(ty) Or Not TObjectType(src) Then
  798. InternalErr "TCTranslator.TransPtrCast"
  799. End If
  800. Local t$=TransType(ty, "TODO: TransPtrCast")
  801. If src.GetClass().IsInterface() Or ty.GetClass().IsInterface() cast="dynamic"
  802. If src.GetClass().IsInterface() And Not ty.GetClass().IsInterface() Then
  803. Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
  804. End If
  805. 'upcast?
  806. If src.GetClass().ExtendsClass( ty.GetClass() ) Return expr
  807. If TObjectType(ty) Then
  808. Return Bra(Bra(TransObject(TObjectType(ty).classDecl)) + "bbObjectDowncast" + Bra("(BBOBJECT)" + expr + ",(BBClass*)&" + TObjectType(ty).classDecl.munged))
  809. End If
  810. Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
  811. End Method
  812. '***** Utility *****
  813. Method TransLocalDecl$( decl:TLocalDecl,init:TExpr, declare:Int = False, outputInit:Int = True )
  814. Local initTrans:String
  815. If outputInit Then
  816. Local cast:String
  817. If (TObjectType(decl.ty) And Not TObjectType(decl.ty).classDecl.IsStruct()) Or TFunctionPtrType(decl.ty) Then
  818. cast = Bra(TransType(decl.ty, ""))
  819. End If
  820. If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
  821. initTrans = "=" + cast + TInvokeExpr(init).decl.munged
  822. Else
  823. If Not TArrayType(decl.ty) Or Not TArrayType(decl.ty).isStatic Then
  824. initTrans = "=" + cast + init.Trans()
  825. Else
  826. initTrans = "[" + TArrayType(decl.ty).length + "]=" + TransValue(decl.ty, Null, True)
  827. End If
  828. End If
  829. End If
  830. Local volTrans:String = " "
  831. If decl.volatile Then
  832. volTrans = " volatile "
  833. End If
  834. If Not declare And opt_debug Then
  835. Local ty:TType = decl.ty
  836. If Not TObjectType( ty ) Or (TObjectType( ty ) And Not TObjectType( ty ).classDecl.IsStruct()) Then
  837. If TIntrinsicType(ty) Then
  838. If Not TConstExpr(init) Then
  839. Return decl.munged + initTrans
  840. End If
  841. Else If Not TArrayType(ty) Or Not TArrayType(ty).isStatic Then
  842. Return decl.munged + initTrans
  843. End If
  844. Else If TObjectType( ty ) And TObjectType( ty ).classDecl.IsStruct() Then
  845. If Not TConstExpr(init) Then
  846. Return decl.munged + initTrans
  847. End If
  848. End If
  849. Else
  850. If TFunctionPtrType(decl.ty) Then
  851. If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
  852. Return TransType( decl.ty, decl.munged ) + " = " + TInvokeExpr(init).decl.munged
  853. Else
  854. Return TransType( decl.ty, decl.munged ) + initTrans
  855. End If
  856. Else
  857. Local ty:TType = decl.ty
  858. If TVoidType( ty ) Or Not ty Then
  859. ty = init.exprType
  860. End If
  861. If TObjectType(ty) Then
  862. If TObjectType(ty).classdecl.IsExtern() Then
  863. If TObjectType(ty).classdecl.IsInterface() Then
  864. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  865. Else
  866. Return TransType( ty, decl.munged )+ volTrans +decl.munged + initTrans
  867. End If
  868. Else
  869. If TObjectType(ty).classdecl.IsStruct() Then
  870. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  871. Else
  872. 'If decl.volatile Then
  873. Return TransType( ty, decl.munged )+ volTrans +decl.munged + initTrans
  874. 'Else
  875. ' Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  876. 'End If
  877. End If
  878. End If
  879. Else
  880. Return TransType( ty, decl.munged )+ volTrans +decl.munged + initTrans
  881. End If
  882. End If
  883. End If
  884. End Method
  885. Method TransLocalDeclNoInit$( decl:TVarDecl )
  886. If TFunctionPtrType(decl.ty) Then
  887. Return TransType( decl.ty, decl.munged ) + "=" + TransValue(decl.ty, "")
  888. Else
  889. If TObjectType(decl.ty) Then
  890. If TObjectType(decl.ty).classdecl.IsExtern() Then
  891. If Not TObjectType(decl.ty).classdecl.IsStruct() Then
  892. Return TransType( decl.ty, decl.munged )+" "+decl.munged+"=" + TransValue(decl.ty, "")
  893. Else
  894. Return TransType( decl.ty, decl.munged )+" "+decl.munged
  895. End If
  896. Else
  897. If Not TObjectType(decl.ty).classdecl.IsStruct() Then
  898. Local cast:String = Bra(TransObject(TObjectType(decl.ty).classDecl))
  899. If TLocalDecl(decl) And TLocalDecl(decl).volatile Then
  900. Return TransType( decl.ty, decl.munged )+" volatile "+decl.munged + "=" + cast + TransValue(decl.ty, "")
  901. Else
  902. Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + cast + TransValue(decl.ty, "")
  903. End If
  904. Else
  905. Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
  906. End If
  907. End If
  908. Else
  909. If TLocalDecl(decl) And TLocalDecl(decl).volatile Then
  910. Return TransType( decl.ty, decl.munged )+" volatile "+decl.munged + "=" + TransValue(decl.ty, "")
  911. Else
  912. If TArrayType(decl.ty) And TArrayType(decl.ty).isStatic Then
  913. Local t:String = TransType( decl.ty, decl.munged )+" "+decl.munged + "[" + TArrayType(decl.ty).length + "]"
  914. t :+ "={"
  915. Local count:Int
  916. For Local i:Int = 0 Until Int(TArrayType( decl.ty ).length)
  917. count :+ 1
  918. If i Then
  919. t :+ ","
  920. End If
  921. If count = 100 Then
  922. t :+ "~n"
  923. count = 0
  924. End If
  925. t :+ TransValue(TArrayType( decl.ty ).elemType, "", True)
  926. Next
  927. Return t + "}"
  928. Else
  929. Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
  930. End If
  931. End If
  932. End If
  933. End If
  934. End Method
  935. Method TransGlobalDecl$( gdecl:TGlobalDecl )
  936. Local glob:TStringBuffer = New TStringBuffer(256)
  937. If Not gdecl.funcGlobal Then
  938. If Not (gdecl.attrs & DECL_INITONLY) Then
  939. glob.Append("static ").Append(TransThreadedGlobal(gdecl)).Append(TransType(gdecl.init.exprType, gdecl.munged)).Append(" ")
  940. End If
  941. glob.Append( gdecl.munged ).Append( "=" )
  942. If (TNewObjectExpr(gdecl.init) Or TNewArrayExpr(gdecl.init)) And Not (gdecl.attrs & DECL_INITONLY) Then
  943. glob.Append("0;~n")
  944. glob.Append(indent).Append("if (").Append(gdecl.munged).Append("==0) {~n")
  945. glob.Append(indent).Append("~t").Append(gdecl.munged).Append("=").Append(gdecl.init.Trans()).Append(";~n")
  946. glob.Append(indent).Append("}")
  947. Else If TArrayExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
  948. glob.Append("0;~n")
  949. Emit glob.ToString()
  950. Emit "if (" + gdecl.munged + "==0) {"
  951. glob.SetLength(0)
  952. glob.Append(gdecl.munged).Append("=").Append(gdecl.init.Trans()).Append(";")
  953. Emit glob.ToString()
  954. Emit "}"
  955. glob.SetLength(0)
  956. Else
  957. If gdecl.init Then
  958. If TFunctionPtrType(gdecl.ty) Then
  959. If TInvokeExpr(gdecl.init) And Not TInvokeExpr(gdecl.init).invokedWithBraces Then
  960. glob.Append( TransCast(TFunctionPtrType(gdecl.ty)) ).Append(TInvokeExpr(gdecl.init).decl.munged)
  961. Else
  962. glob.Append( TransCast(TFunctionPtrType(gdecl.ty)) ).Append(gdecl.init.Trans())
  963. End If
  964. Else If Not TConstExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
  965. ' for non const, we need to add an initialiser
  966. glob.Append(TransValue(gdecl.ty, "")).Append(";~n")
  967. glob.Append(indent).Append("static ").Append(TransThreadedGlobal(gdecl)).Append(" int _").Append(gdecl.munged).Append("_inited = 0;~n")
  968. glob.Append(indent).Append("if (!").Append("_").Append(gdecl.munged).Append("_inited) {~n")
  969. glob.Append(indent).Append("~t").Append("_").Append(gdecl.munged).Append("_inited = 1;~n")
  970. glob.Append(indent).Append("~t").Append(gdecl.munged).Append(" = ").Append(gdecl.init.Trans()).Append(";~n")
  971. glob.Append(indent).Append("}")
  972. Else
  973. If TObjectType(gdecl.ty) Then
  974. glob.Append(Bra(TransObject(TObjectType(gdecl.ty).classDecl)))
  975. End If
  976. glob.Append(gdecl.init.Trans())
  977. End If
  978. Else
  979. If TFunctionPtrType(gdecl.ty) Then
  980. glob.Append("&brl_blitz_NullFunctionError")
  981. Else
  982. glob.Append("0")
  983. End If
  984. End If
  985. End If
  986. Else
  987. glob.Append("static ").Append(TransThreadedGlobal(gdecl)).Append(" int _").Append(gdecl.munged).Append("_inited = 0;~n")
  988. glob.Append(indent).Append("if (!").Append("_").Append(gdecl.munged).Append("_inited) {~n")
  989. glob.Append(indent).Append("~t").Append("_").Append(gdecl.munged).Append("_inited = 1;~n")
  990. glob.Append(indent).Append("~t").Append(gdecl.munged).Append(" = ")
  991. If gdecl.init Then
  992. glob.Append(gdecl.init.Trans())
  993. Else
  994. glob.Append(TransValue(gdecl.ty, ""))
  995. End If
  996. glob.Append(";~n")
  997. glob.Append(indent).Append("}")
  998. End If
  999. Return glob.ToString()
  1000. End Method
  1001. Method TransExportDef:String(decl:TFuncDecl, withApi:Int = True)
  1002. Local t:String = decl.munged
  1003. If withApi And decl.attrs & DECL_API_STDCALL Then
  1004. t :+ "@"
  1005. Local size:Int
  1006. For Local arg:TArgDecl = EachIn decl.argDecls
  1007. size :+ arg.ty.GetSize()
  1008. Next
  1009. t :+ size
  1010. End If
  1011. Return t
  1012. End Method
  1013. Method CreateLocal2$( ty:TType, t$ )
  1014. Local tmp:TLocalDecl=New TLocalDecl.Create( "", ty,Null, True )
  1015. MungDecl tmp
  1016. If TShortType(ty) Then
  1017. Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToWString" + Bra(t)+ ";"
  1018. Else
  1019. Emit TransType(ty, "") + " " + tmp.munged + " = (BBBYTE*)bbStringToCString" + Bra(t)+ ";"
  1020. End If
  1021. customVarStack.Push(tmp.munged)
  1022. Return tmp.munged
  1023. End Method
  1024. Method EmitPushErr()
  1025. Emit "pushErr();"
  1026. End Method
  1027. Method EmitSetErr( info$ )
  1028. Emit "errInfo=~q"+info.Replace( "\","/" )+"~q;"
  1029. End Method
  1030. Method EmitPopErr()
  1031. Emit "popErr();"
  1032. End Method
  1033. '***** Declarations *****
  1034. Method TransStatic$( decl:TDecl )
  1035. If decl.IsExtern() Then
  1036. If Not decl.munged
  1037. Return decl.ident
  1038. End If
  1039. Return decl.munged
  1040. Else If _env And decl.scope And decl.scope=_env.ClassScope()
  1041. ' calling a class function from a method?
  1042. If TFuncDecl(decl) And _env.ClassScope() And _env.FuncScope() And _env.FuncScope().IsMethod() And Not (decl.attrs & FUNC_PTR) And Not _env.ClassScope().IsStruct() Then
  1043. Local scope:TScopeDecl = _env.ClassScope()
  1044. Local obj:String = Bra("struct " + scope.munged + "_obj*")
  1045. Local class:String = "o->clas"
  1046. Return class + "->f_" + decl.ident + MangleMethod(TFuncDecl(decl))
  1047. Else
  1048. Return decl.munged
  1049. End If
  1050. Else If TClassDecl( decl.scope )
  1051. 'Return decl.scope.munged+"::"+decl.munged
  1052. Return decl.munged
  1053. Else If TModuleDecl( decl.scope )
  1054. Return decl.munged
  1055. Else If TFuncDecl(decl.scope)
  1056. Return decl.munged
  1057. Else If TGlobalDecl(decl)
  1058. Return decl.munged
  1059. Else If TBlockDecl(decl.scope)
  1060. Return decl.munged
  1061. Else If TEnumDecl(decl.scope)
  1062. Select decl.ident
  1063. Case "Values"
  1064. Return "bbEnumValues"
  1065. Default
  1066. Return decl.munged
  1067. End Select
  1068. EndIf
  1069. InternalErr "TCTranslator.TransStatic"
  1070. End Method
  1071. Method TransThreadedGlobal:String( decl:TDecl )
  1072. If decl.attrs & DECL_THREADED Then
  1073. Return "BBThreadLocal "
  1074. Else
  1075. Return ""
  1076. End If
  1077. End Method
  1078. Method TransTemplateCast$( ty:TType,src:TType,expr$ )
  1079. ' *sigh*
  1080. ' if var is going to var, remove any leading dereference character.
  1081. ' rather hacky. Would be better to cast variable to varptr during semanting (well done if you can work out where!)
  1082. 'If src.EqualsType( ty.ActualType() ) And (ty._flags & TType.T_VAR) And (src._flags & TType.T_VAR) Then
  1083. ' If expr.startswith("*") Then
  1084. ' expr = expr[1..]
  1085. ' End If
  1086. 'End If
  1087. If ty=src Return expr
  1088. ty=ty.ActualType()
  1089. 'src=src.ActualType()
  1090. If src.EqualsType( ty ) Return expr
  1091. Return TransPtrCast( ty,src,expr,"static" )
  1092. End Method
  1093. Method TransGlobal$( decl:TGlobalDecl )
  1094. Return TransStatic( decl )
  1095. End Method
  1096. Method TransField$( decl:TFieldDecl,lhs:TExpr )
  1097. If lhs Then
  1098. Return TransFieldRef(decl, TransSubExpr( lhs ), lhs.exprType)
  1099. Else
  1100. Return TransFieldRef(decl, "o", Null)
  1101. End If
  1102. ' Local swiz$
  1103. ' If TObjectType( decl.ty )
  1104. ' If TObjectType( decl.ty ).classDecl.IsInterface() swiz=".p"
  1105. ' EndIf
  1106. ' If lhs Return TransSubExpr( lhs )+"->"+decl.munged+swiz
  1107. ' Return decl.munged+swiz
  1108. End Method
  1109. Method TransFunc$( decl:TFuncDecl,args:TExpr[],lhs:TExpr, sup:Int = False, scope:TScopeDecl = Null )
  1110. ' for calling the super class method instead
  1111. Local tSuper:String
  1112. If sup Then
  1113. tSuper = "->super"
  1114. End If
  1115. If Not decl.munged
  1116. MungDecl decl
  1117. End If
  1118. 'If decl.IsMethod()
  1119. If lhs And Not TSelfExpr(lhs) Then
  1120. If TStringType(lhs.exprType) Then
  1121. Return decl.munged + TransArgs(args, decl, TransSubExpr( lhs ))
  1122. End If
  1123. If TStmtExpr(lhs) Then
  1124. lhs.Trans()
  1125. lhs = TStmtExpr(lhs).expr
  1126. End If
  1127. If TVarExpr(lhs) Then
  1128. Local cdecl:TClassDecl
  1129. If TObjectType(TVarExpr(lhs).decl.ty) Then
  1130. cdecl = TObjectType(TVarExpr(lhs).decl.ty).classDecl
  1131. Else If TArrayType(TVarExpr(lhs).decl.ty) Then
  1132. Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  1133. End If
  1134. If decl.attrs & FUNC_PTR Then
  1135. 'Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
  1136. Local op:String
  1137. If cdecl.IsStruct() Then op = "." Else op = "->"
  1138. Return TransSubExpr( lhs ) + op + decl.munged+TransArgs( args,decl, Null)
  1139. Else
  1140. 'Local lvar:String = CreateLocal(lhs, False)
  1141. 'Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1142. If decl.scope.IsExtern()
  1143. If Not cdecl.IsStruct() Then
  1144. 'Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
  1145. Return Bra(TransSubExpr( lhs )) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
  1146. 'Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  1147. End If
  1148. Err "TODO extern types not allowed methods"
  1149. Else
  1150. If cdecl And cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  1151. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface((BBObject*)" + TransSubExpr( lhs ) + ", " + "(BBInterface*)&" + cdecl.munged + "_ifc)"))
  1152. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
  1153. ' Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + lvarInit + ", " + "&" + cdecl.munged + "_ifc)"))
  1154. ' Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1155. Else
  1156. If cdecl And cdecl.IsStruct() Then
  1157. Local pref:String
  1158. If decl.IsMethod() Then
  1159. pref = "_"
  1160. End If
  1161. If Not isPointerType(lhs.exprType) Then
  1162. Return pref + decl.munged+TransArgs( args,decl, "&" + TransSubExpr( lhs ) )
  1163. Else
  1164. Return pref + decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  1165. End If
  1166. Else
  1167. If cdecl Then
  1168. Local obj:String = TransSubExpr( lhs )
  1169. Local preObj:String = obj
  1170. If opt_debug Then
  1171. preObj = TransDebugNullObjectError(obj, cdecl)
  1172. End If
  1173. Local class:String = Bra(preObj) + "->clas" + tSuper
  1174. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, obj )
  1175. Else
  1176. If TEnumDecl(decl.scope) Then
  1177. ' since we already have the ordinal, we can simply output that
  1178. If decl.ident = "Ordinal" Then
  1179. Return Bra(TransSubExpr( lhs ))
  1180. Else
  1181. Return decl.munged + Bra(TransSubExpr( lhs ))
  1182. End If
  1183. End If
  1184. End If
  1185. ' Local class:String = Bra(lvarInit) + "->clas" + tSuper
  1186. ' Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1187. End If
  1188. End If
  1189. End If
  1190. End If
  1191. Else If TNewObjectExpr(lhs) Then
  1192. Local cdecl:TClassDecl = TNewObjectExpr(lhs).classDecl
  1193. If cdecl.IsStruct() Then
  1194. ' create a local variable of the inner invocation
  1195. Local lvar:String = CreateLocal(lhs)
  1196. Local t:String
  1197. If decl.IsMethod() Then
  1198. t = "_"
  1199. End If
  1200. Return t + decl.munged+TransArgs( args,decl, "&" + lvar )
  1201. Else
  1202. If decl.IsMethod() Then
  1203. Local class:String = cdecl.munged
  1204. Return class + "." + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
  1205. Else
  1206. Local class:String = Bra(Bra("struct " + cdecl.munged + "_obj*") + Bra(TransSubExpr( lhs ))) + "->clas" + tSuper
  1207. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl )
  1208. End If
  1209. End If
  1210. Else If TCastExpr(lhs) Then
  1211. Local ty:TType = TCastExpr(lhs).ty
  1212. If TObjectType(ty) Then
  1213. ' create a local variable of the inner invocation
  1214. Local lvar:String = CreateLocal(lhs, False, False)
  1215. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1216. Local cdecl:TClassDecl = TObjectType(ty).classDecl
  1217. Local obj:String = Bra(TransObject(cdecl))
  1218. If decl.attrs & FUNC_PTR Then
  1219. Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
  1220. Else
  1221. ' Null test
  1222. If opt_debug Then
  1223. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1224. End If
  1225. If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  1226. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface((BBObject*)" + obj + lvarInit + ", " + "(BBInterface*)&" + cdecl.munged + "_ifc)"))
  1227. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1228. Else
  1229. Local class:String = Bra("(" + obj + lvarInit + ")->clas" + tSuper)
  1230. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1231. End If
  1232. End If
  1233. Else If TEnumType(ty) Then
  1234. If TEnumDecl(decl.scope) Then
  1235. ' since we already have the ordinal, we can simply output that
  1236. If decl.ident = "Ordinal" Then
  1237. Return Bra(TransSubExpr( lhs ))
  1238. Else
  1239. Return decl.munged + Bra(TransSubExpr( lhs ))
  1240. End If
  1241. End If
  1242. End If
  1243. Else If TMemberVarExpr(lhs) Then
  1244. If TObjectType(TMemberVarExpr(lhs).decl.ty) Then
  1245. Local cdecl:TClassDecl = TObjectType(TMemberVarExpr(lhs).decl.ty).classDecl
  1246. Local obj:String = Bra(TransObject(cdecl))
  1247. If decl.scope.IsExtern()
  1248. If TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct() Then
  1249. Local lvar:String = CreateLocal(lhs, False, False)
  1250. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1251. Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  1252. Else
  1253. Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
  1254. End If
  1255. Else
  1256. If cdecl.IsStruct() Then
  1257. Local pref:String
  1258. If decl.IsMethod() Then
  1259. pref = "_"
  1260. End If
  1261. If Not isPointerType(lhs.exprType) Then
  1262. Return pref + decl.munged+TransArgs( args,decl, "&" + TransSubExpr( lhs ) )
  1263. Else
  1264. Return pref + decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  1265. End If
  1266. Else
  1267. If decl.attrs & FUNC_PTR Then
  1268. Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
  1269. Else
  1270. Local lvar:String = CreateLocal(lhs, False, False)
  1271. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1272. ' Null test
  1273. If opt_debug Then
  1274. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1275. End If
  1276. If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  1277. Local obj:String = Bra(TransObject(cdecl))
  1278. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface((BBObject*)" + obj + lvarInit + ", " + "(BBInterface*)&" + cdecl.munged + "_ifc)"))
  1279. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1280. Else
  1281. Local class:String = Bra("(" + obj + lvarInit + ")->clas" + tSuper)
  1282. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1283. End If
  1284. End If
  1285. End If
  1286. End If
  1287. Else If TArrayType(TMemberVarExpr(lhs).decl.ty) Then
  1288. Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  1289. End If
  1290. Else If TInvokeExpr(lhs) Then
  1291. If TEnumType(lhs.exprType) Then
  1292. If decl.ident = "Ordinal" Then
  1293. Return Bra(TransSubExpr( lhs ))
  1294. Else
  1295. Return decl.munged + Bra(TransSubExpr( lhs ))
  1296. End If
  1297. End If
  1298. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  1299. ' create a local variable of the inner invocation
  1300. Local lvar:String = CreateLocal(lhs, True)
  1301. Local pref:String
  1302. If decl.IsMethod() Then
  1303. pref = "_"
  1304. End If
  1305. If Not isPointerType(lhs.exprType) Then
  1306. Return pref + decl.munged+TransArgs( args,decl, "&" + lvar )
  1307. Else
  1308. Return pref + decl.munged+TransArgs( args,decl, lvar)
  1309. End If
  1310. Else
  1311. ' create a local variable of the inner invocation
  1312. Local lvar:String = CreateLocal(lhs, False, False)
  1313. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1314. ' Null test
  1315. If opt_debug Then
  1316. Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1317. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1318. End If
  1319. Local obj:String = Bra(TransObject(decl.scope))
  1320. Local class:String = Bra("(" + obj + lvarInit +")->clas" + tSuper)
  1321. Return class + "->" + TransFuncPrefix(decl.scope, decl)+ FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1322. End If
  1323. 'Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
  1324. 'Local class:String = Bra("(" + obj + TransSubExpr( lhs ) +")->clas" + tSuper)
  1325. 'Local class:String = Bra("&" + decl.scope.munged)
  1326. 'Return class + "->" + TransFuncPrefix(decl.scope, decl.ident) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
  1327. Else If TInvokeMemberExpr(lhs)
  1328. If TEnumType(lhs.exprType) Then
  1329. If decl.ident = "Ordinal" Then
  1330. Return Bra(TransSubExpr( lhs ))
  1331. Else
  1332. Return decl.munged + Bra(TransSubExpr( lhs ))
  1333. End If
  1334. End If
  1335. ' create a local variable of the inner invocation
  1336. Local lvar:String
  1337. Local lvarInit:String
  1338. If Not decl.scope.IsExtern() And TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  1339. lvar = CreateLocal(lhs, True)
  1340. Else
  1341. lvar = CreateLocal(lhs, False, False)
  1342. lvarInit = Bra(lvar + " = " + lhs.Trans())
  1343. End If
  1344. If decl.scope.IsExtern()
  1345. If TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct() Then
  1346. Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  1347. End If
  1348. Return "// TODO"
  1349. Else
  1350. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  1351. If Not isPointerType(lhs.exprType) Then
  1352. Return "_" + decl.munged+TransArgs( args,decl, "&" + lvar )
  1353. Else
  1354. Return "_" + decl.munged+TransArgs( args,decl, lvar )
  1355. End If
  1356. Else
  1357. Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1358. ' Null test
  1359. If opt_debug Then
  1360. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1361. End If
  1362. If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  1363. Local obj:String = Bra(TransObject(cdecl))
  1364. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface((BBObject*)" + obj + lvarInit + ", " + "(BBInterface*)&" + cdecl.munged + "_ifc)"))
  1365. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1366. Else
  1367. Local obj:String = lvarInit + "->clas" + tSuper
  1368. Return obj + "->" + TransFuncPrefix(decl.scope, decl)+ FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1369. End If
  1370. End If
  1371. End If
  1372. Else If TIndexExpr(lhs) Then
  1373. If TEnumType(lhs.exprType) Then
  1374. If decl.ident = "Ordinal" Then
  1375. Return Bra(TransSubExpr( lhs ))
  1376. Else
  1377. Return decl.munged + Bra(TransSubExpr( lhs ))
  1378. End If
  1379. End If
  1380. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  1381. Local lvar:String = CreateLocal(lhs, True, False)
  1382. Local pref:String
  1383. If decl.IsMethod() Then
  1384. pref = "_"
  1385. End If
  1386. If Not isPointerType(lhs.exprType) Then
  1387. Return pref + decl.munged+TransArgs( args,decl, "&" + lvar )
  1388. Else
  1389. Return pref + decl.munged+TransArgs( args,decl, lvar )
  1390. End If
  1391. Else
  1392. Local lvar:String = CreateLocal(lhs, False, False)
  1393. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1394. ' Local loc:String = CreateLocal(lhs)
  1395. Local obj:String = Bra(TransObject(decl.scope))
  1396. Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1397. ' Null test
  1398. If opt_debug Then
  1399. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1400. End If
  1401. If decl.attrs & FUNC_PTR Then
  1402. Local op:String
  1403. If cdecl.IsStruct() Then op = "." Else op = "->"
  1404. Return lhs.Trans() + op + decl.munged+TransArgs( args,decl, Null)
  1405. Else
  1406. If decl.scope.IsExtern()
  1407. 'Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1408. If Not cdecl.IsStruct() Then
  1409. Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  1410. End If
  1411. Err "TODO extern types not allowed methods"
  1412. Else
  1413. 'Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1414. If cdecl And (cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl)) Then
  1415. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface((BBObject*)" + obj + lvarInit + ", " + "(BBInterface*)&" + cdecl.munged + "_ifc)"))
  1416. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1417. Else
  1418. Local class:String = Bra(lvarInit + "->clas" + tSuper)
  1419. Return class + "->" + TransFuncPrefix(decl.scope, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1420. End If
  1421. End If
  1422. End If
  1423. End If
  1424. Else If TEnumType(lhs.exprType) Then
  1425. If decl.ident = "Ordinal" Then
  1426. Return Bra(TransSubExpr( lhs ))
  1427. Else
  1428. Return decl.munged + Bra(TransSubExpr( lhs ))
  1429. End If
  1430. Else If TInvokeSuperExpr(lhs) Then
  1431. Local lvar:String = CreateLocal(lhs, False, False)
  1432. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1433. Local class:String = Bra(lvarInit + "->clas" + tSuper)
  1434. Return class + "->" + TransFuncPrefix(decl.scope, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1435. Else
  1436. InternalErr "TCTranslator.TransFunc"
  1437. End If
  1438. 'Return TransSubExpr( lhs )+"->"+decl.munged+TransArgs( args,decl )
  1439. 'Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  1440. End If
  1441. ' ((brl_standardio_TCStandardIO_obj*)o->clas)->md_Read(o, xxx, xxx)
  1442. If decl.IsMethod() Or decl.IsField() Then
  1443. If Not (decl.attrs & FUNC_PTR) Then
  1444. Local class:String
  1445. If Not scope Then
  1446. scope = decl.scope
  1447. ' prefer the current class scope for interface calls
  1448. If TClassDecl(scope) And TClassDecl(scope).IsInterface() Then
  1449. If _env.ClassScope() Then
  1450. scope = _env.ClassScope()
  1451. End If
  1452. End If
  1453. If TClassDecl(scope) And Not TClassDecl(scope).IsStruct() Then
  1454. Local obj:String = Bra(TransObject(scope))
  1455. class = "(" + obj + "o)->clas" + tSuper
  1456. ' Null test
  1457. If opt_debug Then
  1458. Emit TransDebugNullObjectError("o", TClassDecl(scope)) + ";"
  1459. End If
  1460. End If
  1461. Else
  1462. class = Bra("&" + scope.munged) + tSuper
  1463. End If
  1464. 'Local obj:String = Bra("struct " + scope.munged + "_obj*")
  1465. 'Local class:String = Bra("(" + obj + "o)->clas" + tSuper)
  1466. 'Local class:String = Bra("&" + decl.scope.munged)
  1467. If TEnumDecl(scope) Then
  1468. ' since we already have the ordinal, we can simply output that
  1469. If decl.ident = "Ordinal" Then
  1470. Return Bra(TransSubExpr( lhs ))
  1471. Else
  1472. Return decl.munged + Bra(TransSubExpr( lhs ))
  1473. End If
  1474. Else If TClassDecl(scope) Then
  1475. If TClassDecl(scope).IsStruct() Then
  1476. Return "_" + decl.munged+TransArgs( args,decl, "o" )
  1477. Else
  1478. Local cdecl:TClassDecl = TClassDecl(scope)
  1479. If cdecl And (cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl)) Then
  1480. Local obj:String = Bra(TransObject(cdecl))
  1481. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface((BBObject*)" + obj + "o, " + "(BBInterface*)&" + cdecl.munged + "_ifc)"))
  1482. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, "o" )
  1483. Else
  1484. Return class + "->" + TransFuncPrefix(scope, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, "o" )
  1485. End If
  1486. End If
  1487. End If
  1488. InternalErr "TCTranslator.TransFunc.2"
  1489. Else
  1490. ' Null test
  1491. If opt_debug Then
  1492. Emit TransDebugNullObjectError("o", TClassDecl(decl.scope)) + ";"
  1493. End If
  1494. Local obj:String
  1495. If TClassDecl(scope) And Not TClassDecl(scope).IsStruct() Then
  1496. obj = Bra(TransObject(decl.scope))
  1497. End If
  1498. Return Bra(obj + "o") + "->" + decl.munged+TransArgs( args,decl )
  1499. End If
  1500. End If
  1501. ' for want of a better place to put it...
  1502. ' It may be possible to have the generate via the TransStatic call below, but we'd need to inject a custom arg somewhere else then
  1503. If TEnumDecl(decl.scope) And decl.ident = "Values" Then
  1504. Return "bbEnumValues" + Bra(decl.scope.munged + "_BBEnum_impl")
  1505. End If
  1506. Return TransStatic( decl )+TransArgs( args,decl )
  1507. End Method
  1508. Method TransObject:String(decl:TScopeDecl, this:Int = False)
  1509. If decl.ident = "Object"
  1510. Return "BBOBJECT"
  1511. Else If decl.ident = "String" Then
  1512. Return "BBSTRING"
  1513. Else
  1514. If TClassDecl(decl) And TClassDecl(decl).IsStruct() Then
  1515. Local t:String = "struct "
  1516. If decl.IsExtern() Then
  1517. t :+ decl.ident
  1518. Else
  1519. t :+ decl.munged
  1520. End If
  1521. If this Then
  1522. Return t + "*"
  1523. Else
  1524. Return t
  1525. End If
  1526. Else
  1527. If decl.IsExtern() Then
  1528. Return "struct " + decl.ident + "*"
  1529. Else
  1530. Return "struct " + decl.munged + "_obj*"
  1531. End If
  1532. End If
  1533. End If
  1534. End Method
  1535. Method TransFuncClass:String(decl:TClassDecl)
  1536. If decl.ident = "Object"
  1537. Return Bra("&bbObjectClass")
  1538. Else
  1539. Return Bra("&" + decl.munged)
  1540. End If
  1541. End Method
  1542. Method TransFuncPrefix:String(decl:TScopeDecl, fdecl:TFuncDecl)
  1543. Local ident:String = fdecl.ident
  1544. If Not decl Or decl.ident = "Object" Or equalsBuiltInFunc(fdecl.ClassScope(), fdecl)
  1545. Return ""
  1546. Else
  1547. If fdecl.IsMethod() Then
  1548. Return "m_"
  1549. Else
  1550. Return "f_"
  1551. End If
  1552. End If
  1553. End Method
  1554. Method TransSuperFunc$( decl:TFuncDecl,args:TExpr[], scope:TScopeDecl )
  1555. Return TransFunc(decl, args, Null, True, scope)
  1556. ' If decl.IsMethod()
  1557. ' Return decl.ClassScope().munged+".md_"+decl.ident+TransArgs( args,decl, "o" )
  1558. ' Else
  1559. ' Return decl.ClassScope().munged+".fn_"+decl.ident+TransArgs( args,decl)
  1560. ' End If
  1561. End Method
  1562. Method TransAscExpr:String(expr:TAscExpr)
  1563. Return "bbStringAsc" + Bra(expr.expr.Trans())
  1564. End Method
  1565. Method TransChrExpr:String(expr:TChrExpr)
  1566. Return "bbStringFromChar" + Bra(expr.expr.Trans())
  1567. End Method
  1568. Method TransLenExpr:String(expr:TLenExpr)
  1569. 'constant strings do not have "->length", so we use the
  1570. 'precalculated value
  1571. If TConstExpr(expr.expr) Then
  1572. If TStringType(expr.expr.exprType) Then
  1573. Return TConstExpr(expr.expr).value.Length
  1574. End If
  1575. End If
  1576. If TStringType(expr.expr.exprType) Then
  1577. Return Bra(expr.expr.Trans()) + "->length"
  1578. Else If TArrayType(expr.expr.exprType) Then
  1579. Return Bra(expr.expr.Trans()) + "->scales[0]"
  1580. Else If TCastExpr(expr.expr) Then
  1581. If TArrayType(TCastExpr(expr.expr).expr.exprType) Then
  1582. Return Bra(TCastExpr(expr.expr).expr.Trans()) + "->scales[0]"
  1583. End If
  1584. 'other types just have a length of "1"
  1585. Else
  1586. Return "1"
  1587. End If
  1588. End Method
  1589. Method TransSizeOfExpr:String(expr:TSizeOfExpr)
  1590. Local cexpr:TConstExpr = TConstExpr(expr.expr)
  1591. If cexpr Then
  1592. If TNumericType(cexpr.exprType) Then
  1593. Return "sizeof" + Bra(TransType(cexpr.exprType, ""))
  1594. ' strings
  1595. Else If TStringType(cexpr.exprType) Then
  1596. ' length of const string * 2 bytes per char
  1597. Return Len(cexpr.value) * 2
  1598. End If
  1599. Else
  1600. If TNumericType(expr.expr.exprType) Then
  1601. ' remove Var-ness first, if any
  1602. Local t:TType = expr.expr.exprType.Copy()
  1603. If t._flags & TType.T_VAR Then
  1604. t._flags :~ TType.T_VAR
  1605. End If
  1606. Return "sizeof" + Bra(TransType(t, ""))
  1607. ' strings
  1608. Else If TStringType(expr.expr.exprType) Then
  1609. 'unicode chars each take 2 bytes
  1610. Return Bra(expr.expr.Trans()) + "->length * 2"
  1611. ' arrays
  1612. Else If TArrayType(expr.expr.exprType) Then
  1613. 'normal exprType is something like "int[]" that
  1614. 'is why it has to be checked against elemType
  1615. Local elemType:TType = TArrayType( expr.expr.exprType ).elemType
  1616. ' numerics - including numeric pointers
  1617. If TNumericType(elemType) Then
  1618. 'multiply element count * size of element type
  1619. Return Bra(expr.expr.Trans()) + "->scales[0] * sizeof" + Bra(TransType(elemType, ""))
  1620. ' everything else : string, array, object, function pointer - are all pointers
  1621. Else
  1622. 'arrays of objects are of size: elementCount * pointerSize
  1623. Return Bra(expr.expr.Trans()) + "->scales[0] * sizeof(void*)"
  1624. EndIf
  1625. ' objects
  1626. Else If TObjectType(expr.expr.exprType) Then
  1627. If TObjectType( expr.expr.exprType ).classDecl.ident = "Object" Then
  1628. Return "0"
  1629. Else
  1630. Local cdecl:TClassDecl = TObjectType( expr.expr.exprType ).classDecl
  1631. If cdecl.IsStruct() Then
  1632. If TIdentTypeExpr(expr.expr) Then
  1633. If cdecl.IsExtern() Then
  1634. Return "sizeof" + Bra("struct " + cdecl.ident)
  1635. Else
  1636. Return "sizeof" + Bra("struct " + cdecl.munged)
  1637. End If
  1638. Else
  1639. Return "sizeof" + Bra(expr.expr.Trans())
  1640. End If
  1641. Else
  1642. If TIdentTypeExpr(expr.expr) Then
  1643. Return Bra(Bra(TransFuncClass(cdecl)) + "->obj_size")
  1644. Else
  1645. Return Bra(Bra(expr.expr.Trans()) + "->clas->obj_size")
  1646. End If
  1647. End If
  1648. End If
  1649. End If
  1650. End If
  1651. InternalErr "TCTranslator.TransSizeOfExpr"
  1652. End Method
  1653. Method TransStackAllocExpr:String(expr:TStackAllocExpr)
  1654. Return "bbStackAlloc" + Bra(expr.expr.Trans())
  1655. End Method
  1656. Method TransFieldOffsetExpr:String(expr:TFieldOffsetExpr)
  1657. Local t:TStringBuffer = New TStringBuffer(128)
  1658. t.Append( "offsetof(" )
  1659. Local cdecl:TClassDecl = TIdentTypeExpr(expr.typeExpr).cdecl
  1660. t.Append( "struct " ).Append( cdecl.munged )
  1661. If Not cdecl.IsStruct() Then
  1662. t.Append( "_obj" )
  1663. End If
  1664. t.Append( ", " ).Append( TVarExpr(expr.fieldExpr).decl.munged ).Append( ")" )
  1665. Return t.ToString()
  1666. End Method
  1667. '***** Expressions *****
  1668. Method TransConstExpr$( expr:TConstExpr )
  1669. If TStringType(expr.exprType) Then
  1670. Return TransStringConst(expr.value)
  1671. Else
  1672. Return TransValue( expr.exprType,expr.value )
  1673. End If
  1674. End Method
  1675. Method TransStringConst:String(value:String)
  1676. If value Then
  1677. _appInstance.mapStringConsts(value)
  1678. End If
  1679. Local sc:TStringConst = TStringConst(_app.stringConsts.ValueForKey(value))
  1680. Local s:String
  1681. If Not sc Then
  1682. s = "bbEmptyString"
  1683. Else
  1684. sc.used :+ 1
  1685. s = sc.id
  1686. End If
  1687. Return Bra("(BBString*)&" + s)
  1688. End Method
  1689. Method StringConstId:String(value:String)
  1690. Local sc:TStringConst = TStringConst(_app.stringConsts.ValueForKey(value))
  1691. If sc Then
  1692. sc.used :+ 1
  1693. Return sc.id
  1694. End If
  1695. InternalErr "Missing const for string : " + value
  1696. End Method
  1697. Method TransNewObjectExpr$( expr:TNewObjectExpr )
  1698. Local t:TStringBuffer = New TStringBuffer(256)
  1699. If Not expr.classDecl.IsStruct() And (Not expr.ctor.argDecls Or expr.ctor.argDecls.length = 0) Then
  1700. If expr.instanceExpr Then
  1701. t.Append( "bbObjectNew(" ).Append( Bra(expr.instanceExpr.Trans()) ).Append( "->clas)" )
  1702. Else
  1703. If ClassHasObjectField(expr.classDecl) Then
  1704. t.Append( Bra(TransObject(TScopeDecl(expr.classDecl.actual))) ).Append( "bbObjectNew((BBClass *)&" ).Append( expr.classDecl.actual.munged ).Append( ")" )
  1705. Else
  1706. t.Append( Bra(TransObject(TScopeDecl(expr.classDecl.actual))) ).Append( "bbObjectAtomicNew((BBClass *)&" ).Append( expr.classDecl.actual.munged ).Append( ")" )
  1707. End If
  1708. End If
  1709. Else
  1710. Local ctorMunged:String
  1711. If expr.classDecl = expr.ctor.scope Then
  1712. MungDecl expr.ctor
  1713. ctorMunged = expr.ctor.munged
  1714. Else
  1715. ctorMunged = expr.classDecl.actual.munged + "_" + expr.ctor.ident + MangleMethod(expr.ctor)
  1716. End If
  1717. If expr.instanceExpr Then
  1718. If expr.classDecl.IsStruct() Then
  1719. t.SetLength(0)
  1720. t.Append( ctorMunged ).Append( "_ObjectNew" ).Append( TransArgs( expr.args,expr.ctor) )
  1721. Else
  1722. t.Append( "_" ).Append( ctorMunged ).Append( "_ObjectNew" ).Append( TransArgs( expr.args,expr.ctor, Bra(expr.instanceExpr.Trans()) + "->clas" ) )
  1723. End If
  1724. Else
  1725. If ClassHasObjectField(expr.classDecl) And Not expr.classDecl.IsStruct() Then
  1726. t.Append( "_" ).Append( ctorMunged ).Append( "_ObjectNew" ).Append( TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged, True ) )
  1727. Else
  1728. If expr.classDecl.IsStruct() Then
  1729. t.Append( ctorMunged ).Append( "_ObjectNew" ).Append( TransArgs( expr.args,expr.ctor) )
  1730. Else
  1731. t.Append( "_" ).Append( ctorMunged ).Append( "_ObjectNew" ).Append( TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged, True) )
  1732. End If
  1733. End If
  1734. End If
  1735. End If
  1736. 'Local t$="(new "+expr.classDecl.actual.munged+")"
  1737. 'If expr.ctor t:+"->"+expr.ctor.actual.munged+TransArgs( expr.args,expr.ctor )
  1738. Return t.ToString()
  1739. End Method
  1740. Method TransNewArrayExpr$( expr:TNewArrayExpr )
  1741. If expr.expr.length = 1 Then
  1742. If TObjectType(expr.ty) And TObjectType(expr.ty).classdecl.IsStruct() And Not IsPointerType(expr.ty) Then
  1743. Return "bbArrayNew1DStruct_" + TObjectType(expr.ty).classdecl.munged + Bra(expr.expr[0].Trans())
  1744. Else If TEnumType(expr.ty) Then
  1745. Return New TStringBuffer(128).Append( "bbArrayNew1DEnum" ).Append( Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans() + ", " + TEnumType(expr.ty).decl.munged + "_BBEnum_impl") ).ToString()
  1746. Else
  1747. Return New TStringBuffer().Append( "bbArrayNew1D" ).Append( Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans()) ).ToString()
  1748. End If
  1749. Else
  1750. ' multiple array
  1751. Local s:TStringBuffer = New TStringBuffer
  1752. For Local i:Int = 0 Until expr.expr.length
  1753. If i Then
  1754. s.Append(", ")
  1755. End If
  1756. s.Append(expr.expr[i].Trans())
  1757. Next
  1758. Local sb:TStringBuffer = New TStringBuffer(256)
  1759. If TObjectType(expr.ty) And TObjectType(expr.ty).classdecl.IsStruct() And Not IsPointerType(expr.ty) Then
  1760. sb.Append( "bbArrayNewStruct" )
  1761. sb.Append("(")
  1762. sb.Append( TransArrayType(expr.ty) ).Append( ", sizeof" ).Append( Bra(TransObject(TObjectType(expr.ty).classdecl)) )
  1763. sb.Append( ", _").Append( TObjectType(expr.ty).classdecl.munged ).Append( "_New, " ).Append( expr.expr.length ).Append( ", " ).Append( s.ToString() )
  1764. sb.Append( ")" )
  1765. Else If TEnumType(expr.ty) Then
  1766. sb.Append( "bbArrayNewEnum").Append("(")
  1767. sb.Append( TransArrayType(expr.ty) ).Append( ", " ).Append( TEnumType(expr.ty).decl.munged ).Append( "_BBEnum_impl" ).Append( ", " ).Append( expr.expr.length ).Append( ", " ).Append( s.ToString() )
  1768. sb.Append( ")" )
  1769. Else
  1770. sb.Append( "bbArrayNew" )
  1771. sb.Append("(")
  1772. sb.Append( TransArrayType(expr.ty) ).Append( ", " ).Append( expr.expr.length ).Append( ", " ).Append( s.ToString() )
  1773. sb.Append( ")" )
  1774. End If
  1775. Return sb.ToString()
  1776. End If
  1777. End Method
  1778. Method TransSelfExpr$( expr:TSelfExpr )
  1779. If (TObjectType(expr.exprType) And TObjectType(expr.exprType).classDecl.IsStruct()) Or ..
  1780. (TClassType(expr.exprType) And TClassType(expr.exprType).classDecl.IsStruct()) Then
  1781. Return "*o"
  1782. End If
  1783. Return "o"
  1784. End Method
  1785. Method TransIdentTypeExpr:String(expr:TIdentTypeExpr)
  1786. Return "struct " + expr.cdecl.munged + "_obj"
  1787. End Method
  1788. Method TransCastExpr$( expr:TCastExpr )
  1789. Local t$= expr.expr.Trans()
  1790. Local dst:TType=expr.ty
  1791. Local src:TType=expr.expr.exprType
  1792. If TNumericType(src) And (src._flags & TType.T_VAR) Then
  1793. ' var number being cast to a varptr
  1794. If (dst._flags & TType.T_VARPTR) Then
  1795. Return "&" + Bra(t)
  1796. End If
  1797. End If
  1798. If (dst._flags & TType.T_VARPTR) Or (dst._flags & TType.T_VAR) Then
  1799. If Not TConstExpr(expr.expr) Then
  1800. If TInvokeExpr(expr.expr) Return t
  1801. If TByteType( src) Return Bra("&"+t)
  1802. If TShortType( src) Return Bra("&"+t)
  1803. If TFloatType( src) Return Bra("&"+t)
  1804. If TIntType( src) Return Bra("&"+t)
  1805. If TUIntType( src) Return Bra("&"+t)
  1806. If TLongType( src) Return Bra("&"+t)
  1807. If TULongType( src) Return Bra("&"+t)
  1808. If TSizeTType( src) Return Bra("&"+t)
  1809. If TLongIntType( src) Return Bra("&"+t)
  1810. If TULongIntType( src) Return Bra("&"+t)
  1811. If TDoubleType( src) Return Bra("&"+t)
  1812. If TInt128Type( src) Return Bra("&"+t)
  1813. If TFloat128Type( src) Return Bra("&"+t)
  1814. If TDouble128Type( src) Return Bra("&"+t)
  1815. If TFloat64Type( src) Return Bra("&"+t)
  1816. If TWParamType( src) Return Bra("&"+t)
  1817. If TLParamType( src) Return Bra("&"+t)
  1818. If TObjectType(src) Then
  1819. If TObjectType(src).classDecl.IsExtern() Or (dst._flags & TType.T_VARPTR) Then
  1820. Return Bra("&" + t)
  1821. Else
  1822. If TObjectType(dst) Then
  1823. Return Bra("&" + t)
  1824. Else
  1825. Return Bra("bbObjectToFieldOffset" + Bra("(BBObject*)" + "&" + t))
  1826. End If
  1827. End If
  1828. End If
  1829. If TFunctionPtrType(src) Return Bra("&"+t)
  1830. 'If TPointerType( src) Return Bra("&"+t)
  1831. Else
  1832. Return Bra(TransValue(TConstExpr(expr.expr).ty, TConstExpr(expr.expr).value))
  1833. End If
  1834. Else If IsPointerType( dst, 0, TType.T_POINTER | TType.T_CHAR_PTR | TType.T_SHORT_PTR )
  1835. If TArrayType(src) Then
  1836. If TArrayType(src).isStatic Then
  1837. Return Bra(t)
  1838. Else
  1839. Return Bra(Bra(TransType(dst, "")) + "BBARRAYDATA(" + t + ",1)")
  1840. End If
  1841. End If
  1842. 'If TByteType(src) And Not IsPointerType(src, TType.T_BYTE, TType.T_POINTER) Return Bra("&"+t)
  1843. If TStringType(src) Then
  1844. Local tmp:String
  1845. If IsPointerType( dst, 0, TType.T_SHORT_PTR ) Or IsPointerType( dst, TType.T_SHORT, TType.T_PTR ) Then
  1846. tmp = CreateLocal2(NewPointerType(TType.T_SHORT), t)
  1847. Else
  1848. tmp = CreateLocal2(NewPointerType(TType.T_BYTE), t)
  1849. End If
  1850. Return tmp
  1851. End If
  1852. If (TStringType(dst) And IsPointerType( dst, 0, TType.T_CHAR_PTR | TType.T_SHORT_PTR )) And TNullType(src) Then
  1853. Return "0"
  1854. End If
  1855. If TObjectType(src) Then
  1856. If TObjectType(src).classDecl.IsExtern() Or (src._flags & TType.T_VARPTR) Then
  1857. Return Bra(t)
  1858. Else
  1859. If Not TObjectType(src).classDecl.IsStruct() Then
  1860. Return Bra("bbObjectToFieldOffset" + Bra("(BBObject*)" + t))
  1861. Else
  1862. Return Bra("(BBBYTE*)" + t)
  1863. End If
  1864. End If
  1865. End If
  1866. Local p:String = TransSPointer(dst)
  1867. If TByteType( dst )
  1868. If IsPointerType(src, TType.T_BYTE, TType.T_POINTER & dst._flags) Return t
  1869. If TNumericType( src ) Return Bra("(BBBYTE" + p + ")"+t)
  1870. Else If TShortType( dst )
  1871. If IsPointerType(src, TType.T_SHORT, TType.T_POINTER & dst._flags) Return t
  1872. If TNumericType( src ) Return Bra("(BBSHORT" + p + ")"+t)
  1873. Else If TIntType( dst )
  1874. If IsPointerType(src, TType.T_INT, TType.T_POINTER & dst._flags) Return t
  1875. If TNumericType( src ) Return Bra("(BBINT" + p + ")"+t)
  1876. Else If TUIntType( dst )
  1877. If IsPointerType(src, TType.T_UINT, TType.T_POINTER & dst._flags) Return t
  1878. If TNumericType( src ) Return Bra("(BBUINT" + p + ")"+t)
  1879. Else If TFloatType( dst )
  1880. If IsPointerType(src, TType.T_FLOAT, TType.T_POINTER & dst._flags) Return t
  1881. If TNumericType( src ) Return Bra("(BBFLOAT" + p + ")"+t)
  1882. Else If TDoubleType( dst )
  1883. If IsPointerType(src, TType.T_DOUBLE, TType.T_POINTER & dst._flags) Return t
  1884. If TNumericType( src ) Return Bra("(BBDOUBLE" + p + ")"+t)
  1885. Else If TLongType( dst )
  1886. If IsPointerType(src, TType.T_LONG, TType.T_POINTER & dst._flags) Return t
  1887. If TNumericType( src ) Return Bra("(BBLONG" + p + ")"+t)
  1888. Else If TULongType( dst )
  1889. If IsPointerType(src, TType.T_ULONG, TType.T_POINTER & dst._flags) Return t
  1890. If TNumericType( src ) Return Bra("(BBULONG" + p + ")"+t)
  1891. Else If TSizeTType( dst )
  1892. If IsPointerType(src, TType.T_SIZET, TType.T_POINTER & dst._flags) Return t
  1893. If TNumericType( src ) Return Bra("(BBSIZET" + p + ")"+t)
  1894. Else If TLongIntType( dst )
  1895. If IsPointerType(src, TType.T_LONGINT, TType.T_POINTER & dst._flags) Return t
  1896. If TNumericType( src ) Return Bra("(BBLONGINT" + p + ")"+t)
  1897. Else If TULongIntType( dst )
  1898. If IsPointerType(src, TType.T_ULONGINT, TType.T_POINTER & dst._flags) Return t
  1899. If TNumericType( src ) Return Bra("(BBULONGINT" + p + ")"+t)
  1900. Else If TWParamType( dst )
  1901. If IsPointerType(src, TType.T_WPARAM, TType.T_POINTER & dst._flags) Return t
  1902. If TNumericType( src ) Return Bra("(WPARAM" + p + ")"+t)
  1903. Else If TLParamType( dst )
  1904. If IsPointerType(src, TType.T_LPARAM, TType.T_POINTER & dst._flags) Return t
  1905. If TNumericType( src ) Return Bra("(LPARAM" + p + ")"+t)
  1906. Else If TInt128Type( dst )
  1907. If IsPointerType(src, TType.T_INT128, TType.T_POINTER & dst._flags) Return t
  1908. If TNumericType( src ) Return Bra("(BBINT128" + p + ")"+t)
  1909. Else If TFloat128Type( dst )
  1910. If IsPointerType(src, TType.T_FLOAT128, TType.T_POINTER & dst._flags) Return t
  1911. If TNumericType( src ) Return Bra("(BBFLOAT128" + p + ")"+t)
  1912. Else If TDouble128Type( dst )
  1913. If IsPointerType(src, TType.T_DOUBLE128, TType.T_POINTER & dst._flags) Return t
  1914. If TNumericType( src ) Return Bra("(BBDOUBLE128" + p + ")"+t)
  1915. Else If TFloat64Type( dst )
  1916. If IsPointerType(src, TType.T_FLOAT64, TType.T_POINTER & dst._flags) Return t
  1917. If TNumericType( src ) Return Bra("(BBFLOAT64" + p + ")"+t)
  1918. 'Else If TIntPtrPtrType( dst )
  1919. ' If TBytePtrType( src) Return Bra("(BBINT**)"+t)
  1920. ' If TShortPtrType( src ) Return Bra("(BBINT**)"+t)
  1921. ' If TIntPtrType( src ) Return Bra("(BBINT**)"+t)
  1922. ' If TFloatPtrType( src ) Return Bra("(BBINT**)"+t)
  1923. ' If TDoublePtrType( src ) Return Bra("(BBINT**)"+t)
  1924. ' If TLongPtrType( src ) Return Bra("(BBINT**)"+t)
  1925. ' If TNumericType( src ) Return Bra("(BBINT**)"+t)
  1926. End If
  1927. Else If TBoolType( dst )
  1928. If TFunctionPtrType(src) Return Bra(Bra( t+"!=0" ) + " && " + Bra( t+"!=&brl_blitz_NullFunctionError" ))
  1929. 'If TFunctionPtrType(src) Return Bra( t+"!=0" )
  1930. If IsPointerType( src, 0, TType.T_POINTER ) Return Bra( t )
  1931. If TBoolType( src ) Return t
  1932. If TByteType( src ) Return Bra( t+"!=0" )
  1933. If TShortType( src ) Return Bra( t+"!=0" )
  1934. If TIntType( src ) Return Bra( t+"!=0" )
  1935. If TUIntType( src ) Return Bra( t+"!=0" )
  1936. If TFloatType( src ) Return Bra( t+"!=0.0f" )
  1937. 'If TCastExpr(expr.expr) And (TArrayType( src ) Or TStringType( src ) Or TObjectType( src )) Then
  1938. ' Return Bra( t+"!= &bbNullObject" )
  1939. 'End If
  1940. If TLongType( src ) Return Bra( t+"!=0" )
  1941. If TULongType( src ) Return Bra( t+"!=0" )
  1942. If TSizeTType( src ) Return Bra( t+"!=0" )
  1943. If TLongIntType( src ) Return Bra( t+"!=0" )
  1944. If TULongIntType( src ) Return Bra( t+"!=0" )
  1945. If TWParamType( src ) Return Bra( t+"!=0" )
  1946. If TLParamType( src ) Return Bra( t+"!=0" )
  1947. If TDoubleType( src ) Return Bra( t+"!=0.0f" )
  1948. If TArrayType( src ) Return Bra( t+"!= &bbEmptyArray" )
  1949. If TStringType( src ) Return Bra( t+"!= &bbEmptyString" )
  1950. If TObjectType( src ) Then
  1951. If TObjectType(src).classDecl.IsExtern() Then
  1952. If Not TObjectType(src).classDecl.IsStruct() Then
  1953. Return Bra( t+"!=0" )
  1954. Else
  1955. Return Bra("1")
  1956. End If
  1957. Else
  1958. If Not TObjectType(src).classDecl.IsStruct() Then
  1959. Return Bra( Bra(Bra("BBObject*") + t )+"!= &bbNullObject" )
  1960. Else
  1961. Return Bra("1")
  1962. End If
  1963. End If
  1964. End If
  1965. If TEnumType( src ) Return Bra( t+"!=0" )
  1966. Else If TIntType( dst )
  1967. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBINT)"+t)
  1968. If TBoolType( src ) Return Bra( t )
  1969. If TByteType( src) Return Bra("(BBINT)"+t)
  1970. If TShortType( src) Return Bra("(BBINT)"+t)
  1971. If TBoolType( src ) Return t
  1972. If TIntType( src ) Return t
  1973. If TUIntType( src ) Return Bra("(BBINT)"+t)
  1974. If TFloatType( src ) Return Bra("(BBINT)"+t)
  1975. If TDoubleType( src ) Return Bra("(BBINT)"+t)
  1976. If TLongType( src ) Return Bra("(BBINT)"+t)
  1977. If TULongType( src ) Return Bra("(BBINT)"+t)
  1978. If TSizeTType( src ) Return Bra("(BBINT)"+t)
  1979. If TLongIntType( src ) Return Bra("(BBINT)"+t)
  1980. If TULongIntType( src ) Return Bra("(BBINT)"+t)
  1981. If TWParamType( src ) Return Bra("(BBINT)"+t)
  1982. If TLParamType( src ) Return Bra("(BBINT)"+t)
  1983. If TStringType( src ) Return "bbStringToInt" + Bra(t)
  1984. If TEnumType( src) Return Bra("(BBINT)"+t)
  1985. 'If TIntVarPtrType( src ) Return Bra("*" + t)
  1986. 'If TPointerType( src ) Return Bra("(BBINT)"+t)
  1987. Else If TLongType( dst )
  1988. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBLONG)"+t)
  1989. If TBoolType( src ) Return Bra( t )
  1990. If TByteType( src) Return Bra("(BBLONG)"+t)
  1991. If TShortType( src) Return Bra("(BBLONG)"+t)
  1992. If TIntType( src) Return Bra("(BBLONG)"+t)
  1993. If TUIntType( src) Return Bra("(BBLONG)"+t)
  1994. If TLongType( src ) Return t
  1995. If TULongType( src ) Return Bra("(BBLONG)"+t)
  1996. If TSizeTType( src ) Return Bra("(BBLONG)"+t)
  1997. If TLongIntType( src ) Return Bra("(BBLONG)"+t)
  1998. If TULongIntType( src ) Return Bra("(BBLONG)"+t)
  1999. If TWParamType( src ) Return Bra("(BBLONG)"+t)
  2000. If TLParamType( src ) Return Bra("(BBLONG)"+t)
  2001. If TFloatType( src ) Return Bra("(BBLONG)"+t)
  2002. If TDoubleType( src ) Return Bra("(BBLONG)"+t)
  2003. If TStringType( src ) Return "bbStringToLong" + Bra(t)
  2004. If TFloat64Type( src ) Return Bra("(BBLONG)"+t)
  2005. If TEnumType( src) Return Bra("(BBLONG)"+t)
  2006. 'If TPointerType( src ) Return Bra("(BBLONG)"+t)
  2007. Else If TSizeTType( dst )
  2008. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBSIZET)"+t)
  2009. If TBoolType( src ) Return Bra( t )
  2010. If TByteType( src) Return Bra("(BBSIZET)"+t)
  2011. If TShortType( src) Return Bra("(BBSIZET)"+t)
  2012. If TIntType( src) Return Bra("(BBSIZET)"+t)
  2013. If TUIntType( src) Return Bra("(BBSIZET)"+t)
  2014. If TLongType( src) Return Bra("(BBSIZET)"+t)
  2015. If TULongType( src) Return Bra("(BBSIZET)"+t)
  2016. If TSizeTType( src ) Return t
  2017. If TLongIntType( src) Return Bra("(BBSIZET)"+t)
  2018. If TULongIntType( src) Return Bra("(BBSIZET)"+t)
  2019. If TWParamType( src ) Return Bra("(BBSIZET)"+t)
  2020. If TLParamType( src ) Return Bra("(BBSIZET)"+t)
  2021. If TFloatType( src ) Return Bra("(BBSIZET)"+t)
  2022. If TDoubleType( src ) Return Bra("(BBSIZET)"+t)
  2023. If TStringType( src ) Return "bbStringToSizet" + Bra(t)
  2024. If TFloat64Type( src ) Return Bra("(BBSIZET)"+t)
  2025. If TEnumType( src) Return Bra("(BBSIZET)"+t)
  2026. 'If TPointerType( src ) Return Bra("(BBLONG)"+t)
  2027. Else If TLongIntType( dst )
  2028. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBLONGINT)"+t)
  2029. If TBoolType( src ) Return Bra( t )
  2030. If TByteType( src) Return Bra("(BBLONGINT)"+t)
  2031. If TShortType( src) Return Bra("(BBLONGINT)"+t)
  2032. If TIntType( src) Return Bra("(BBLONGINT)"+t)
  2033. If TUIntType( src) Return Bra("(BBLONGINT)"+t)
  2034. If TLongType( src ) Return Bra("(BBLONGINT)"+t)
  2035. If TULongType( src ) Return Bra("(BBLONGINT)"+t)
  2036. If TSizeTType( src ) Return Bra("(BBLONGINT)"+t)
  2037. If TLongIntType( src ) Return t
  2038. If TULongIntType( src ) Return Bra("(BBLONGINT)"+t)
  2039. If TWParamType( src ) Return Bra("(BBLONGINT)"+t)
  2040. If TLParamType( src ) Return Bra("(BBLONGINT)"+t)
  2041. If TFloatType( src ) Return Bra("(BBLONGINT)"+t)
  2042. If TDoubleType( src ) Return Bra("(BBLONGINT)"+t)
  2043. If TStringType( src ) Return "bbStringToLongInt" + Bra(t)
  2044. If TFloat64Type( src ) Return Bra("(BBLONGINT)"+t)
  2045. If TEnumType( src) Return Bra("(BBLONGINT)"+t)
  2046. 'If TPointerType( src ) Return Bra("(BBLONGINT)"+t)
  2047. Else If TULongIntType( dst )
  2048. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBULONGINT)"+t)
  2049. If TBoolType( src ) Return Bra( t )
  2050. If TByteType( src) Return Bra("(BBULONGINT)"+t)
  2051. If TShortType( src) Return Bra("(BBULONGINT)"+t)
  2052. If TIntType( src) Return Bra("(BBULONGINT)"+t)
  2053. If TUIntType( src) Return Bra("(BBULONGINT)"+t)
  2054. If TLongType( src ) Return Bra("(BBULONGINT)"+t)
  2055. If TULongType( src ) Return Bra("(BBULONGINT)"+t)
  2056. If TSizeTType( src ) Return Bra("(BBULONGINT)"+t)
  2057. If TLongIntType( src ) Return Bra("(BBULONGINT)"+t)
  2058. If TULongIntType( src ) Return t
  2059. If TWParamType( src ) Return Bra("(BBULONGINT)"+t)
  2060. If TLParamType( src ) Return Bra("(BBULONGINT)"+t)
  2061. If TFloatType( src ) Return Bra("(BBULONGINT)"+t)
  2062. If TDoubleType( src ) Return Bra("(BBULONGINT)"+t)
  2063. If TStringType( src ) Return "bbStringToULongInt" + Bra(t)
  2064. If TFloat64Type( src ) Return Bra("(BBULONGINT)"+t)
  2065. If TEnumType( src) Return Bra("(BBULONGINT)"+t)
  2066. 'If TPointerType( src ) Return Bra("(BBULONGINT)"+t)
  2067. Else If TFloatType( dst )
  2068. If TBoolType( src ) Return Bra( t )
  2069. If TByteType( src ) Return Bra("(BBFLOAT)"+t)
  2070. If TIntType( src ) Return Bra("(BBFLOAT)"+t)
  2071. If TUIntType( src ) Return Bra("(BBFLOAT)"+t)
  2072. If TShortType( src ) Return Bra("(BBFLOAT)"+t)
  2073. If TFloatType( src ) Return t
  2074. If TDoubleType( src ) Return Bra("(BBFLOAT)"+t)
  2075. If TLongType( src ) Return Bra("(BBFLOAT)"+t)
  2076. If TULongType( src ) Return Bra("(BBFLOAT)"+t)
  2077. If TSizeTType( src ) Return Bra("(BBFLOAT)"+t)
  2078. If TLongIntType( src ) Return Bra("(BBFLOAT)"+t)
  2079. If TULongIntType( src ) Return Bra("(BBFLOAT)"+t)
  2080. If TWParamType( src ) Return Bra("(BBFLOAT)"+t)
  2081. If TLParamType( src ) Return Bra("(BBFLOAT)"+t)
  2082. If TStringType( src ) Return "bbStringToFloat" + Bra(t)
  2083. 'If TFloatVarPtrType( src ) Return Bra("*" + t)
  2084. 'If TPointerType( src ) Return Bra("(BBFLOAT)"+t)
  2085. Else If TDoubleType( dst )
  2086. If TBoolType( src ) Return Bra( t )
  2087. If TByteType( src ) Return Bra("(BBDOUBLE)"+t)
  2088. If TIntType( src ) Return Bra("(BBDOUBLE)"+t)
  2089. If TUIntType( src ) Return Bra("(BBDOUBLE)"+t)
  2090. If TShortType( src ) Return Bra("(BBDOUBLE)"+t)
  2091. If TDoubleType( src ) Return t
  2092. If TFloatType( src ) Return Bra("(BBDOUBLE)"+t)
  2093. If TLongType( src ) Return Bra("(BBDOUBLE)"+t)
  2094. If TULongType( src ) Return Bra("(BBDOUBLE)"+t)
  2095. If TSizeTType( src ) Return Bra("(BBDOUBLE)"+t)
  2096. If TLongIntType( src ) Return Bra("(BBDOUBLE)"+t)
  2097. If TULongIntType( src ) Return Bra("(BBDOUBLE)"+t)
  2098. If TWParamType( src ) Return Bra("(BBDOUBLE)"+t)
  2099. If TLParamType( src ) Return Bra("(BBDOUBLE)"+t)
  2100. If TStringType( src ) Return "bbStringToDouble" + Bra(t)
  2101. 'If TDoubleVarPtrType( src ) Return Bra("*" + t)
  2102. 'If TPointerType( src ) Return Bra("(BBDOUBLE)"+t)
  2103. Else If TStringType( dst )
  2104. If IsPointerType(src, 0, TType.T_POINTER) Return "bbStringFromSizet"+Bra( t )
  2105. If TBoolType( src ) Return "bbStringFromInt"+Bra( t )
  2106. If TByteType( src ) Return "bbStringFromInt"+Bra( t )
  2107. If TShortType( src ) Return "bbStringFromInt"+Bra( t )
  2108. If TIntType( src ) Return "bbStringFromInt"+Bra( t )
  2109. If TUIntType( src ) Return "bbStringFromUInt"+Bra( t )
  2110. If TLongType( src ) Return "bbStringFromLong"+Bra( t )
  2111. If TULongType( src ) Return "bbStringFromULong"+Bra( t )
  2112. If TSizeTType( src ) Return "bbStringFromSizet"+Bra( t )
  2113. If TLongIntType( src ) Return "bbStringFromLongInt"+Bra( t )
  2114. If TULongIntType( src ) Return "bbStringFromULongInt"+Bra( t )
  2115. If TWParamType( src ) Return "bbStringFromWParam"+Bra( t )
  2116. If TLParamType( src ) Return "bbStringFromLParam"+Bra( t )
  2117. If TFloatType( src ) Return "bbStringFromFloat"+Bra( t + ",0" )
  2118. If TDoubleType( src ) Return "bbStringFromDouble"+Bra( t + ",0" )
  2119. If TStringType( src ) Then
  2120. If src._flags & TType.T_CHAR_PTR Then
  2121. Return "bbStringFromCString"+Bra( t )
  2122. End If
  2123. If src._flags & TType.T_SHORT_PTR Then
  2124. Return "bbStringFromWString"+Bra( t )
  2125. End If
  2126. If src._flags & TType.T_VAR Then
  2127. If TSliceExpr( expr.expr ) Then
  2128. Return "&" + Bra(t)
  2129. End If
  2130. Return t
  2131. End If
  2132. Return t
  2133. End If
  2134. If TEnumType( src ) Then
  2135. Local ty:TType = TEnumType( src ).decl.ty
  2136. If TByteType( ty ) Return "bbStringFromInt"+Bra( t )
  2137. If TShortType( ty ) Return "bbStringFromInt"+Bra( t )
  2138. If TIntType( ty ) Return "bbStringFromInt"+Bra( t )
  2139. If TUIntType( ty ) Return "bbStringFromUInt"+Bra( t )
  2140. If TLongType( ty ) Return "bbStringFromLong"+Bra( t )
  2141. If TULongType( ty ) Return "bbStringFromULong"+Bra( t )
  2142. If TSizeTType( ty ) Return "bbStringFromSizet"+Bra( t )
  2143. If TLongIntType( ty ) Return "bbStringFromLongInt"+Bra( t )
  2144. If TULongIntType( ty ) Return "bbStringFromULongInt"+Bra( t )
  2145. End If
  2146. 'If TStringVarPtrType( src ) Then
  2147. ' If TSliceExpr( expr.expr ) Then
  2148. ' Return t
  2149. ' End If
  2150. ' Return "*" + t
  2151. 'End If
  2152. 'If TStringCharPtrType( src ) Return "bbStringFromCString"+Bra( t )
  2153. 'Else If TStringVarPtrType( dst )
  2154. 'DebugStop
  2155. Else If TByteType( dst )
  2156. If TBoolType( src ) Return Bra( t )
  2157. If TByteType( src) Return t
  2158. If TShortType( src ) Return Bra("(BBBYTE)"+t)
  2159. If TIntType( src ) Return Bra("(BBBYTE)"+t)
  2160. If TUIntType( src ) Return Bra("(BBBYTE)"+t)
  2161. If TFloatType( src ) Return Bra("(BBBYTE)"+t)
  2162. If TDoubleType( src ) Return Bra("(BBBYTE)"+t)
  2163. If TLongType( src ) Return Bra("(BBBYTE)"+t)
  2164. If TULongType( src ) Return Bra("(BBBYTE)"+t)
  2165. If TSizeTType( src ) Return Bra("(BBBYTE)"+t)
  2166. If TLongIntType( src ) Return Bra("(BBBYTE)"+t)
  2167. If TULongIntType( src ) Return Bra("(BBBYTE)"+t)
  2168. If TWParamType( src ) Return Bra("(BBBYTE)"+t)
  2169. If TLParamType( src ) Return Bra("(BBBYTE)"+t)
  2170. If TStringType( src ) Return Bra("(BBBYTE)bbStringToInt" + Bra(t))
  2171. If TEnumType( src) Return Bra("(BBYTE)"+t)
  2172. 'If TByteVarPtrType( src ) Return Bra("*" + t)
  2173. Else If TShortType( dst )
  2174. If TBoolType( src ) Return Bra( t )
  2175. If TShortType( src) Return t
  2176. If TByteType( src) Return Bra("(BBSHORT)"+t)
  2177. If TIntType( src ) Return Bra("(BBSHORT)"+t)
  2178. If TUIntType( src ) Return Bra("(BBSHORT)"+t)
  2179. If TFloatType( src ) Return Bra("(BBSHORT)"+t)
  2180. If TDoubleType( src ) Return Bra("(BBSHORT)"+t)
  2181. If TLongType( src ) Return Bra("(BBSHORT)"+t)
  2182. If TULongType( src ) Return Bra("(BBSHORT)"+t)
  2183. If TSizeTType( src ) Return Bra("(BBSHORT)"+t)
  2184. If TLongIntType( src ) Return Bra("(BBSHORT)"+t)
  2185. If TULongIntType( src ) Return Bra("(BBSHORT)"+t)
  2186. If TWParamType( src ) Return Bra("(BBSHORT)"+t)
  2187. If TLParamType( src ) Return Bra("(BBSHORT)"+t)
  2188. If TStringType( src ) Return Bra("(BBSHORT)bbStringToInt" + Bra(t))
  2189. If TEnumType( src) Return Bra("(BBSHORT)"+t)
  2190. 'If TShortVarPtrType( src ) Return Bra("*" + t)
  2191. Else If TUIntType( dst )
  2192. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBUINT)"+t)
  2193. If TBoolType( src ) Return Bra( t )
  2194. If TShortType( src ) Return Bra("(BBUINT)"+t)
  2195. If TByteType( src) Return Bra("(BBUINT)"+t)
  2196. If TIntType( src ) Return Bra("(BBUINT)"+t)
  2197. If TUIntType( src) Return t
  2198. If TFloatType( src ) Return Bra("(BBUINT)"+t)
  2199. If TDoubleType( src ) Return Bra("(BBUINT)"+t)
  2200. If TLongType( src ) Return Bra("(BBUINT)"+t)
  2201. If TULongType( src ) Return Bra("(BBUINT)"+t)
  2202. If TSizeTType( src ) Return Bra("(BBUINT)"+t)
  2203. If TLongIntType( src ) Return Bra("(BBUINT)"+t)
  2204. If TULongIntType( src ) Return Bra("(BBUINT)"+t)
  2205. If TWParamType( src ) Return Bra("(BBUINT)"+t)
  2206. If TLParamType( src ) Return Bra("(BBUINT)"+t)
  2207. If TStringType( src ) Return "bbStringToUInt" + Bra(t)
  2208. If TEnumType( src) Return Bra("(BBUINT)"+t)
  2209. Else If TULongType( dst )
  2210. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBULONG)"+t)
  2211. If TBoolType( src ) Return Bra( t )
  2212. If TShortType( src ) Return Bra("(BBULONG)"+t)
  2213. If TByteType( src) Return Bra("(BBULONG)"+t)
  2214. If TIntType( src ) Return Bra("(BBULONG)"+t)
  2215. If TUIntType( src ) Return Bra("(BBULONG)"+t)
  2216. If TFloatType( src ) Return Bra("(BBULONG)"+t)
  2217. If TDoubleType( src ) Return Bra("(BBULONG)"+t)
  2218. If TLongType( src ) Return Bra("(BBULONG)"+t)
  2219. If TULongType( src) Return t
  2220. If TSizeTType( src ) Return Bra("(BBULONG)"+t)
  2221. If TLongIntType( src ) Return Bra("(BBULONG)"+t)
  2222. If TULongIntType( src ) Return Bra("(BBULONG)"+t)
  2223. If TWParamType( src ) Return Bra("(BBULONG)"+t)
  2224. If TLParamType( src ) Return Bra("(BBULONG)"+t)
  2225. If TStringType( src ) Return "bbStringToULong" + Bra(t)
  2226. If TFloat64Type( src ) Return Bra("(BBULONG)"+t)
  2227. If TEnumType( src) Return Bra("(BBULONG)"+t)
  2228. Else If TFloat64Type( dst )
  2229. If TFloat64Type( src) Return t
  2230. If TLongType( src ) Return Bra("(BBFLOAT64)"+t)
  2231. If TULongType( src ) Return Bra("(BBFLOAT64)"+t)
  2232. If TSizeTType( src ) Return Bra("(BBFLOAT64)"+t)
  2233. Else If TInt128Type( dst )
  2234. If TInt128Type( src) Return t
  2235. If TFloat128Type( src ) Return Bra("(BBINT128)"+t)
  2236. If TDouble128Type( src ) Return Bra("(BBINT128)"+t)
  2237. Else If TFloat128Type( dst )
  2238. If TFloat128Type( src) Return t
  2239. If TInt128Type( src ) Return Bra("(BBFLOAT128)"+t)
  2240. If TDouble128Type( src ) Return Bra("(BBFLOAT128)"+t)
  2241. Else If TDouble128Type( dst )
  2242. If TDouble128Type( src) Return t
  2243. If TInt128Type( src ) Return Bra("(BBDOUBLE128)"+t)
  2244. If TFloat128Type( src ) Return Bra("(BBDOUBLE128)"+t)
  2245. Else If TWParamType( dst )
  2246. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(WPARAM)"+t)
  2247. If TBoolType( src ) Return Bra( t )
  2248. If TByteType( src) Return Bra("(WPARAM)"+t)
  2249. If TShortType( src) Return Bra("(WPARAM)"+t)
  2250. If TIntType( src) Return Bra("(WPARAM)"+t)
  2251. If TUIntType( src) Return Bra("(WPARAM)"+t)
  2252. If TLongType( src) Return Bra("(WPARAM)"+t)
  2253. If TULongType( src) Return Bra("(WPARAM)"+t)
  2254. If TSizeTType( src ) Return Bra("(WPARAM)"+t)
  2255. If TLongIntType( src) Return Bra("(WPARAM)"+t)
  2256. If TULongIntType( src) Return Bra("(WPARAM)"+t)
  2257. If TWParamType( src ) Return t
  2258. If TLParamType( src ) Return Bra("(WPARAM)"+t)
  2259. If TFloatType( src ) Return Bra("(WPARAM)"+t)
  2260. If TDoubleType( src ) Return Bra("(WPARAM)"+t)
  2261. If TStringType( src ) Return "bbStringToWParam" + Bra(t)
  2262. Else If TLParamType( dst )
  2263. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(LPARAM)"+t)
  2264. If TBoolType( src ) Return Bra( t )
  2265. If TByteType( src) Return Bra("(LPARAM)"+t)
  2266. If TShortType( src) Return Bra("(LPARAM)"+t)
  2267. If TIntType( src) Return Bra("(LPARAM)"+t)
  2268. If TUIntType( src) Return Bra("(LPARAM)"+t)
  2269. If TLongType( src) Return Bra("(LPARAM)"+t)
  2270. If TULongType( src) Return Bra("(LPARAM)"+t)
  2271. If TSizeTType( src ) Return Bra("(LPARAM)"+t)
  2272. If TLongIntType( src) Return Bra("(LPARAM)"+t)
  2273. If TULongIntType( src) Return Bra("(LPARAM)"+t)
  2274. If TWParamType( src ) Return Bra("(LPARAM)"+t)
  2275. If TLParamType( src ) Return t
  2276. If TFloatType( src ) Return Bra("(LPARAM)"+t)
  2277. If TDoubleType( src ) Return Bra("(LPARAM)"+t)
  2278. If TStringType( src ) Return "bbStringToLParam" + Bra(t)
  2279. Else If TArrayType( dst )
  2280. If TArrayType( src ) Then
  2281. If TObjectType( TArrayType( dst ).elemType ) And TObjectType( TArrayType( dst ).elemType ).classDecl.ident = "Object" Then
  2282. ' if we are casting to Object[], don't actually cast.
  2283. Return Bra(t)
  2284. Else
  2285. Return "bbArrayCastFromObject" + Bra("(BBOBJECT)" + t + "," + TransArrayType(TArrayType( dst ).elemType))
  2286. End If
  2287. End If
  2288. If TObjectType( src) And (TObjectType( src ).classDecl.ident = "___Array" Or TObjectType( src ).classDecl.ident = "Object") Then
  2289. Return "bbArrayCastFromObject" + Bra("(BBOBJECT)" + t + "," + TransArrayType(TArrayType( dst ).elemType))
  2290. End If
  2291. Else If TObjectType( dst )
  2292. 'If TArrayType( src ) Return Bra("(BBOBJECT)"+t)
  2293. 'If TStringType( src ) Return Bra("(BBOBJECT)"+t)
  2294. 'If TObjectType( src ) Return t
  2295. If Not TObjectType( dst ).classDecl.IsExtern() Then
  2296. If TObjectType( dst ).classDecl.IsStruct() Then
  2297. Return TransValue(dst, Null)
  2298. End If
  2299. If TNullType( src ) Return "&bbNullObject"
  2300. If TObjectType(dst).classDecl.IsInterface() Then
  2301. Local sb:TStringBuffer = New TStringBuffer(128)
  2302. sb.Append("(")
  2303. sb.Append("(")
  2304. sb.Append( TransObject( TObjectType( dst ).classDecl ) )
  2305. sb.Append(")")
  2306. sb.Append("bbInterfaceDowncast")
  2307. sb.Append("(")
  2308. sb.Append("(BBObject*)")
  2309. sb.Append(t)
  2310. sb.Append(",(BBInterface*)&")
  2311. sb.Append(TObjectType(dst).classDecl.munged)
  2312. sb.Append("_ifc")
  2313. sb.Append(")")
  2314. sb.Append(")")
  2315. Return sb.ToString()
  2316. Else
  2317. ' no need to downcast to BBObject, as all objects extend it...
  2318. If TObjectType( dst ).classDecl.ident = "Object" Then
  2319. Return t
  2320. Else
  2321. Local sb:TStringBuffer = New TStringBuffer(128)
  2322. sb.Append("(")
  2323. sb.Append("(")
  2324. sb.Append( TransObject( TObjectType( dst ).classDecl ) )
  2325. sb.Append(")")
  2326. sb.Append("bbObjectDowncast")
  2327. sb.Append("(")
  2328. sb.Append("(BBOBJECT)")
  2329. sb.Append(t)
  2330. sb.Append(",(BBClass*)&")
  2331. sb.Append(TObjectType(dst).classDecl.munged)
  2332. sb.Append(")")
  2333. sb.Append(")")
  2334. Return sb.ToString()
  2335. End If
  2336. End If
  2337. Else
  2338. If TObjectType( dst ).classDecl.IsInterface() Then
  2339. Return t
  2340. Else
  2341. Return "" ' TODO??
  2342. End If
  2343. End If
  2344. Else If TEnumType( dst )
  2345. If TEnumType( src) Return t
  2346. If TIntegralType(src) Then
  2347. If opt_debug Then
  2348. Return "bbEnumCast_" + TransDebugScopeType(TEnumType(dst).decl.ty) + Bra(TEnumType(dst).decl.munged + "_BBEnum_impl," + t)
  2349. Else
  2350. ' no checking in release mode.
  2351. Return t
  2352. End If
  2353. End If
  2354. End If
  2355. Return TransPtrCast( dst,src,t,"dynamic" )
  2356. Err "C++ translator can't convert "+src.ToString()+" to "+dst.ToString()
  2357. End Method
  2358. Method TransUnaryExpr$( expr:TUnaryExpr )
  2359. Local pri:Int=ExprPri( expr )
  2360. Local t_expr$
  2361. If TVarExpr(expr.expr) Then
  2362. If TObjectType(TVarExpr(expr.expr).exprType) Then
  2363. If TObjectType(TVarExpr(expr.expr).exprType).classDecl.IsStruct() Then
  2364. t_expr = Bra( "1" )
  2365. Else
  2366. t_expr = Bra( expr.expr.Trans() + "!= &bbNullObject")
  2367. End If
  2368. Else If TStringType(TVarExpr(expr.expr).exprType) Then
  2369. t_expr = Bra( expr.expr.Trans() + "!= &bbEmptyString")
  2370. Else If expr.op = "~~" And TEnumType(expr.exprType) Then
  2371. Return Bra("bbEnum" + TEnumType(expr.exprType).decl.munged +"_Mask & ~~" + Bra(TransSubExpr( expr.expr,pri )))
  2372. Else
  2373. t_expr = TransSubExpr( expr.expr,pri )
  2374. End If
  2375. Else
  2376. If expr.op = "~~" And TEnumType(expr.exprType) Then
  2377. Return Bra("bbEnum" + TEnumType(expr.exprType).decl.munged +"_Mask & ~~" + Bra(TransSubExpr( expr.expr,pri )))
  2378. Else
  2379. t_expr = TransSubExpr( expr.expr,pri )
  2380. End If
  2381. End If
  2382. Return TransUnaryOp( expr.op )+t_expr
  2383. End Method
  2384. Method TransBinaryExpr$( expr:TBinaryExpr )
  2385. Local pri:Int=ExprPri( expr )
  2386. Local t_lhs$=TransSubExpr( expr.lhs,pri )
  2387. ' If TVarPtrType(expr.lhs.exprType) Then
  2388. ' t_lhs = "*" + t_lhs
  2389. ' End If
  2390. Local t_rhs$=TransSubExpr( expr.rhs,pri-1 )
  2391. ' If TVarPtrType(expr.rhs.exprType) Then
  2392. ' t_rhs = "*" + t_rhs
  2393. ' End If
  2394. If expr.op = "+" Then
  2395. If TStringType(expr.exprType) Then
  2396. Return "bbStringConcat(" + t_lhs + "," + t_rhs + ")"
  2397. Else If TArrayType(expr.exprType) Then
  2398. Return "bbArrayConcat(" + TransArrayType(TArrayType(expr.lhs.exprType).elemType) + "," + t_lhs + "," + t_rhs + ")"
  2399. End If
  2400. End If
  2401. If expr.op = "^" Then
  2402. If TIntegralType(expr.exprType) Then
  2403. Return "bbLongPow" + Bra(t_lhs + ", " + t_rhs)
  2404. Else
  2405. Return "bbFloatPow" + Bra(t_lhs + ", " + t_rhs)
  2406. End If
  2407. End If
  2408. If expr.op = "mod" Or expr.op = "%" Then
  2409. If TDecimalType(expr.lhs.exprType) Or TDecimalType(expr.rhs.exprType) Then
  2410. Return "bbFloatMod" + Bra(t_lhs + ", " + t_rhs)
  2411. End If
  2412. End If
  2413. If (expr.op = "shr" Or expr.op = "&" Or expr.op = "|") Then
  2414. If TIntType(expr.exprType) Then
  2415. t_lhs = "(unsigned int)(" + t_lhs + ")"
  2416. t_rhs = "(unsigned int)(" + t_rhs + ")"
  2417. Else If TLongType(expr.exprType) Then
  2418. t_lhs = "(unsigned long long)(" + t_lhs + ")"
  2419. t_rhs = "(unsigned long long)(" + t_rhs + ")"
  2420. Else If TLongIntType(expr.exprType) Then
  2421. t_lhs = "(unsigned long)(" + t_lhs + ")"
  2422. t_rhs = "(unsigned long)(" + t_rhs + ")"
  2423. End If
  2424. End If
  2425. If TBinaryCompareExpr(expr) Then
  2426. If TStringType(TBinaryCompareExpr(expr).ty) Then
  2427. If t_lhs="&bbNullObject" Then
  2428. err "NULL"
  2429. t_lhs = "&bbEmptyString"
  2430. End If
  2431. If t_rhs="&bbNullObject" Then
  2432. err "NULL"
  2433. t_rhs = "&bbEmptyString"
  2434. End If
  2435. If t_lhs <> "&bbEmptyString" And t_rhs <> "&bbEmptyString" Then
  2436. If expr.op = "=" Or expr.op = "<>" Then
  2437. Return "bbStringEquals" + Bra(t_lhs + ", " + t_rhs) + TransBinaryOp(expr.op, "") + "1"
  2438. Else
  2439. Return "bbStringCompare" + Bra(t_lhs + ", " + t_rhs) + TransBinaryOp(expr.op, "") + "0"
  2440. End If
  2441. End If
  2442. Else If IsPointerType(TBinaryCompareExpr(expr).ty, 0, TType.T_POINTER) Then
  2443. If t_lhs="&bbNullObject" Then
  2444. t_lhs = "0"
  2445. End If
  2446. If t_rhs="&bbNullObject" Then
  2447. t_rhs = "0"
  2448. End If
  2449. Else If TArrayType(TBinaryCompareExpr(expr).ty) Then
  2450. If t_lhs="&bbNullObject" Then
  2451. err "NULL"
  2452. t_lhs = "&bbEmptyArray"
  2453. End If
  2454. If t_rhs="&bbNullObject" Then
  2455. err "NULL"
  2456. t_rhs = "&bbEmptyArray"
  2457. End If
  2458. Else If TObjectType(TBinaryCompareExpr(expr).ty) Then
  2459. Local bcExpr:TBinaryCompareExpr = TBinaryCompareExpr(expr)
  2460. If bcExpr.lhs.exprType.ExtendsType(bcExpr.rhs.exprType) Then
  2461. If t_rhs="&bbNullObject" Then
  2462. t_lhs = Bra("(BBOBJECT)" + t_lhs)
  2463. Else
  2464. t_lhs = Bra(Bra(TransType(bcExpr.rhs.exprType, "*")) + t_lhs)
  2465. End If
  2466. Else If bcExpr.rhs.exprType.ExtendsType(bcExpr.lhs.exprType)
  2467. If t_lhs="&bbNullObject" Then
  2468. t_rhs = Bra("(BBOBJECT)" + t_rhs)
  2469. Else
  2470. t_rhs = Bra(Bra(TransType(bcExpr.lhs.exprType, "*")) + t_rhs)
  2471. End If
  2472. End If
  2473. If t_rhs="&bbNullObject" And TObjectType(bcExpr.lhs.exprType) And TObjectType(bcExpr.lhs.exprType).classDecl.ident = "Object" Then
  2474. If bcExpr.op = "=" Or bcExpr.op = "<>" Then
  2475. Local t:String = t_lhs
  2476. 'If Not TVarExpr(bcExpr.lhs) Then
  2477. ' t = CreateLocal(bcExpr.lhs)
  2478. 'End If
  2479. If bcExpr.op = "="
  2480. Return Bra(t + "==" + t_rhs )
  2481. Else
  2482. Return Bra(t + "!=" + t_rhs )
  2483. End If
  2484. End If
  2485. End If
  2486. If t_lhs="&bbNullObject" And TObjectType(bcExpr.rhs.exprType) And TObjectType(bcExpr.rhs.exprType).classDecl.ident = "Object" Then
  2487. If bcExpr.op = "=" Or bcExpr.op = "<>" Then
  2488. Local t:String = t_rhs
  2489. 'If Not TVarExpr(bcExpr.rhs) Then
  2490. ' t = CreateLocal(bcExpr.rhs)
  2491. 'End If
  2492. If bcExpr.op = "="
  2493. Return Bra(t + "==" + t_lhs )
  2494. Else
  2495. Return Bra(t + "!=" + t_lhs )
  2496. End If
  2497. End If
  2498. End If
  2499. End If
  2500. End If
  2501. Return bra(t_lhs+TransBinaryOp( expr.op,t_rhs )+t_rhs)
  2502. End Method
  2503. Method TransIndexExpr$( expr:TIndexExpr )
  2504. Local t_expr$=TransSubExpr( expr.expr )
  2505. Local t_index:TStringBuffer = New TStringBuffer( 64 )
  2506. If expr.index.length = 1 Then
  2507. If TArraySizeExpr(expr.index[0]) Then
  2508. Local sizes:TArraySizeExpr = TArraySizeExpr(expr.index[0])
  2509. sizes.Trans()
  2510. Local v:String = sizes.val.munged
  2511. Local i:Int = 0
  2512. For i = 0 Until sizes.index.length - 1
  2513. If i Then
  2514. t_index.Append(" + ")
  2515. End If
  2516. t_index.Append( "(*(" ).Append( v )
  2517. If i Then
  2518. t_index.Append( "+" ).Append( i )
  2519. End If
  2520. t_index.Append( ")) * ").Append( sizes.index[i].Trans() )
  2521. Next
  2522. t_index.Append( " + " ).Append( sizes.index[i].Trans() )
  2523. ' (*(v+0)) * var1 + (*(v+1)) * var2 + var3
  2524. 'DebugStop
  2525. Else
  2526. t_index.Append( expr.index[0].Trans() )
  2527. End If
  2528. End If
  2529. If TStringType( expr.expr.exprType ) Then
  2530. Return Bra(t_expr) + "->buf[" + t_index.ToString() + "]"
  2531. 'Return "(BBINT)"+t_expr+"["+t_index+"]"
  2532. End If
  2533. If TArrayType( expr.expr.exprType ) Then
  2534. If TFunctionPtrType(TArrayType( expr.expr.exprType ).elemType) Then
  2535. If opt_debug Then
  2536. Local sb:TStringBuffer = New TStringBuffer( 256 )
  2537. Local in:String = t_index.ToString()
  2538. 'Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATAINDEX(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims," + t_index + ")")) + "[" + t_index + "]"
  2539. sb.Append("((")
  2540. sb.Append( TransType( TArrayType( expr.expr.exprType ).elemType, "*" ) )
  2541. sb.Append(")(BBARRAYDATAINDEX((")
  2542. sb.Append(t_expr)
  2543. sb.Append("),(").Append(t_expr)
  2544. sb.Append(")->dims,")
  2545. sb.Append(in)
  2546. sb.Append(")))[")
  2547. sb.Append(in)
  2548. sb.Append("]")
  2549. Return sb.ToString()
  2550. Else
  2551. Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATA(" + t_expr + ",1)")) + "[" + t_index.ToString() + "]"
  2552. End If
  2553. Else
  2554. If TArrayType( expr.expr.exprType ).isStatic Then
  2555. Return t_expr + "[" + t_index.ToString() + "]"
  2556. Else
  2557. If opt_debug Then
  2558. Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATAINDEX(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims," + t_index.ToString() + ")") + "[" + t_index.ToString() + "]"
  2559. Else
  2560. Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + t_expr + ",1)") + "[" + t_index.ToString() + "]"
  2561. End If
  2562. End If
  2563. End If
  2564. End If
  2565. 'Local swiz$
  2566. 'If TObjectType( expr.exprType )And expr.exprType.GetClass().IsInterface() swiz=".p"
  2567. 'If ENV_CONFIG="debug" Return t_expr+".At("+t_index+")"+swiz
  2568. Return t_expr+"["+t_index.ToString()+"]"
  2569. End Method
  2570. Method TransSliceExpr$( expr:TSliceExpr )
  2571. 'DebugStop
  2572. Local t_expr:String=TransSubExpr( expr.expr )
  2573. Local t_args$
  2574. If expr.from Then
  2575. t_args=expr.from.Trans()
  2576. Else
  2577. t_args = "0"
  2578. End If
  2579. If expr.term Then
  2580. t_args:+","+expr.term.Trans()
  2581. Else
  2582. If TArrayType(expr.exprType) Then
  2583. t_args :+ "," + Bra(t_expr) + "->scales[0]"
  2584. 'Else If TStringVarPtrType(expr.exprType) Then
  2585. ' t_args :+ ",(*" + t_expr + ")->length"
  2586. Else
  2587. t_args :+ "," + Bra(t_expr) + "->length"
  2588. End If
  2589. End If
  2590. If TArrayType(expr.exprType) Then
  2591. Local ty:TType = TArrayType(expr.exprType).elemType
  2592. If TObjectType(ty) And TObjectType(ty).classDecl.IsStruct() Then
  2593. Return "bbArraySliceStruct_" + TObjectType(ty).classdecl.munged + Bra( t_expr + "," + t_args )
  2594. Else
  2595. Return "bbArraySlice" + Bra(TransArrayType(ty) + "," + t_expr + "," + t_args)
  2596. End If
  2597. 'Else If TStringVarPtrType(expr.exprType) Then
  2598. ' Return "bbStringSlice" + Bra("*" + t_expr + "," + t_args)
  2599. Else
  2600. Return "bbStringSlice" + Bra(t_expr + "," + t_args)
  2601. End If
  2602. 'Return t_expr+".Slice("+t_args+")"
  2603. End Method
  2604. Method TransArrayExpr$( expr:TArrayExpr )
  2605. Local elemType:TType=TArrayType( expr.exprType ).elemType
  2606. Local tmpData:TLocalDecl =New TLocalDecl.Create( "",TType.voidType,Null )
  2607. MungDecl tmpData
  2608. Local tmpArray:TLocalDecl =New TLocalDecl.Create( "",TType.voidType,Null )
  2609. MungDecl tmpArray
  2610. Local t$
  2611. Local count:Int
  2612. For Local elem:TExpr=EachIn expr.exprs
  2613. If t t:+","
  2614. t:+elem.Trans()
  2615. count :+ 1
  2616. Next
  2617. Local tt$
  2618. ' If Not _env tt="static "
  2619. If Not TFunctionPtrType(elemType) Then
  2620. tt :+ TransType( elemType, tmpData.munged ) + " "+tmpData.munged + "[]"
  2621. Else
  2622. tt :+ TransType( elemType, tmpData.munged + "[]" )
  2623. End If
  2624. Emit tt+"={"+t+"};"
  2625. If TObjectType(elemType) And TObjectType(elemType).classdecl.IsStruct() And Not IsPointerType(elemType) Then
  2626. Emit "BBARRAY " + tmpArray.munged + " = bbArrayFromDataStruct" + Bra(TransArrayType(elemType) + "," + count + "," + tmpData.munged + ", sizeof" + Bra(TransObject(TObjectType(elemType).classdecl))) + ";"
  2627. Else If TEnumType(elemType)
  2628. Emit "BBARRAY " + tmpArray.munged + " = bbArrayFromDataSize" + Bra(TransArrayType(elemType) + "," + count + "," + tmpData.munged + "," + TEnumType(elemType).decl.ty.GetSize() ) + ";"
  2629. Else
  2630. Emit "BBARRAY " + tmpArray.munged + " = bbArrayFromData" + Bra(TransArrayType(elemType) + "," + count + "," + tmpData.munged ) + ";"
  2631. End If
  2632. Return tmpArray.munged
  2633. 'Return "bbArrayFromData" + Bra(TransArrayType(elemType) + "," + count + "," + tmp.munged )
  2634. 'Return "Array<"+TransRefType( elemType, "MM" )+" >("+tmp.munged+","+expr.exprs.Length+")"
  2635. End Method
  2636. Method TransArraySizeExpr$ ( expr:TArraySizeExpr )
  2637. ' scales[0] is the total size of the array
  2638. ' we start from [1] because it is the size of the next full dimension.
  2639. ' in the case of a 2-dimensional array, [1] represents the length of a row.
  2640. Return Bra("(BBARRAY)" + expr.expr.Trans()) + "->scales + 1"
  2641. End Method
  2642. Method TransIntrinsicExpr$( decl:TDecl,expr:TExpr,args:TExpr[] )
  2643. Local texpr$,arg0$,arg1$,arg2$
  2644. If expr texpr=TransSubExpr( expr )
  2645. If args.Length>0 And args[0] arg0=args[0].Trans()
  2646. If args.Length>1 And args[1] arg1=args[1].Trans()
  2647. If args.Length>2 And args[2] arg2=args[2].Trans()
  2648. Local id$=decl.munged[1..]
  2649. Local id2$=id[..1].ToUpper()+id[1..]
  2650. Select id
  2651. '
  2652. 'global functions
  2653. Case "print" Return "Print"+Bra( arg0 )
  2654. Case "error" Return "Error"+Bra( arg0 )
  2655. '
  2656. 'string/array methods
  2657. Case "length" Return texpr+".Length()"
  2658. Case "resize" Return texpr+".Resize"+Bra( arg0 )
  2659. 'string methods
  2660. Case "compare" Return texpr+".Compare"+Bra( arg0 )
  2661. Case "find" Return texpr+".Find"+Bra( arg0+","+arg1 )
  2662. Case "findlast" Return texpr+".FindLast"+Bra( arg0 )
  2663. Case "findlast2" Return texpr+".FindLast"+Bra( arg0+","+arg1 )
  2664. Case "trim" Return texpr+".Trim()"
  2665. Case "join" Return texpr+".Join"+Bra( arg0 )
  2666. Case "split" Return texpr+".Split"+Bra( arg0 )
  2667. Case "replace" Return texpr+".Replace"+Bra( arg0+","+arg1 )
  2668. Case "tolower" Return texpr+".ToLower()"
  2669. Case "toupper" Return texpr+".ToUpper()"
  2670. Case "contains" Return texpr+".Contains"+Bra( arg0 )
  2671. Case "startswith" Return texpr+".StartsWith"+Bra( arg0 )
  2672. Case "endswith" Return texpr+".EndsWith"+Bra( arg0 )
  2673. 'string functions
  2674. Case "fromchar" Return "String"+Bra( "(Char)"+Bra(arg0)+",1" )
  2675. 'math methods
  2676. Case "sin","cos","tan" Return "(float)"+id+Bra( Bra(arg0)+"*D2R" )
  2677. Case "asin","acos","atan" Return "(float)"+Bra( id+Bra(arg0)+"*R2D" )
  2678. Case "atan2" Return "(float)"+Bra( id+Bra(arg0+","+arg1)+"*R2D" )
  2679. Case "sqrt","floor","ceil","log" Return "(float)"+id+Bra( arg0 )
  2680. Case "pow" Return "(float)bbFloatPow"+Bra( arg0+","+arg1 )
  2681. '
  2682. End Select
  2683. InternalErr "TCTranslator.TransIntrinsicExpr"
  2684. End Method
  2685. '***** Statements *****
  2686. Method TransTryStmt$(tryStmt:TTryStmt)
  2687. Emit "{"
  2688. If tryStmt.finallyStmt Then MungDecl tryStmt.finallyStmt.finallyLabel
  2689. MungDecl tryStmt.rethrowLabel
  2690. MungDecl tryStmt.endTryLabel
  2691. Emit "BBOBJECT ex;"
  2692. If tryStmt.finallyStmt Then
  2693. ' for a nested Try construct, only declare this label once, because leaving such a construct
  2694. ' via Return, Exit Or Continue requires a jump to multiple Finally blocks in direct succession
  2695. ' and the "inner" declarations of retptr wouldn't be visible to the "outer" Finally blocks
  2696. Local alreadyDeclared:Int = False
  2697. For Local t:TTryStmt = EachIn tryStack
  2698. If t.finallyStmt Then alreadyDeclared = True; Exit
  2699. Next
  2700. If Not alreadyDeclared Then
  2701. Emit "void* retptr = &&" + tryStmt.rethrowLabel.munged + ";"
  2702. Else
  2703. Emit "retptr = &&" + tryStmt.rethrowLabel.munged + ";"
  2704. End If
  2705. End If
  2706. Emit "bbExTry {"
  2707. ' Try block:
  2708. Emit "case 0: {"
  2709. EmitLocalDeclarations tryStmt.block
  2710. If opt_debug Then Emit "bbOnDebugPushExState();"
  2711. PushLoopTryStack tryStmt
  2712. tryStack.Push tryStmt
  2713. EmitBlock tryStmt.block
  2714. tryStack.Pop
  2715. PopLoopTryStack
  2716. Emit "bbExLeave();"
  2717. If opt_debug Then Emit "bbOnDebugPopExState();"
  2718. ' run the Finally block if control reaches the end of the Try block
  2719. If tryStmt.finallyStmt Then EmitFinallyJmp tryStmt.finallyStmt, tryStmt.endTryLabel
  2720. Emit "}"
  2721. Emit "break;"
  2722. ' Catch blocks:
  2723. If tryStmt.catches Then
  2724. Emit "case 1: {"
  2725. If opt_debug Then Emit "bbOnDebugPopExState();"
  2726. If tryStmt.finallyStmt Then
  2727. If opt_debug Then Emit "bbOnDebugPushExState();"
  2728. Emit "ex = bbExCatchAndReenter();"
  2729. Else
  2730. Emit "ex = bbExCatch();"
  2731. End If
  2732. Local s:String = ""
  2733. For Local catchStmt:TCatchStmt = EachIn tryStmt.catches
  2734. MungDecl catchStmt.init
  2735. If TStringType(catchStmt.init.ty) Then
  2736. Emit s + "if (bbObjectStringcast((BBOBJECT)ex) != (BBOBJECT)&bbEmptyString) {"
  2737. Emit TransType(catchStmt.init.ty, catchStmt.init.munged) + " " + catchStmt.init.munged + "=(BBSTRING)ex;"
  2738. Else If TArrayType(catchStmt.init.ty) Then
  2739. Emit s + "if (bbObjectArraycast((BBOBJECT)ex) != &bbEmptyArray) {"
  2740. Emit TransType(catchStmt.init.ty, catchStmt.init.munged) + " " + catchStmt.init.munged + "=(BBARRAY)ex;"
  2741. Else If TObjectType(catchStmt.init.ty) Then
  2742. If TObjectType(catchStmt.init.ty).classDecl.IsInterface() Then
  2743. Emit s + "if (bbInterfaceDowncast((BBObject*)ex,(BBInterface*)&" + TObjectType(catchStmt.init.ty).classDecl.munged + "_ifc) != &bbNullObject) {"
  2744. Else
  2745. Emit s + "if (bbObjectDowncast((BBOBJECT)ex,(BBClass*)&" + TObjectType(catchStmt.init.ty).classDecl.munged + ") != &bbNullObject) {"
  2746. End If
  2747. Emit TransType(catchStmt.init.ty, catchStmt.init.munged) + " " + catchStmt.init.munged + "=" + Bra(TransType(catchStmt.init.ty, catchStmt.init.munged)) + "ex;"
  2748. Else
  2749. Err "Not an object"
  2750. End If
  2751. EmitLocalDeclarations catchStmt.block, catchStmt.init
  2752. If tryStmt.finallyStmt Then
  2753. PushLoopTryStack tryStmt
  2754. tryStack.Push tryStmt
  2755. EmitBlock catchStmt.block
  2756. tryStack.Pop
  2757. PopLoopTryStack
  2758. Else
  2759. EmitBlock catchStmt.block
  2760. End If
  2761. s = "} else "
  2762. Next
  2763. If tryStmt.finallyStmt Then
  2764. Emit s + "{"
  2765. ' run the Finally block if an exception was thrown from the Try block but not handled by any of the Catch blocks
  2766. Emit "bbExLeave();"
  2767. If opt_debug Then Emit "bbOnDebugPopExState();"
  2768. EmitFinallyJmp tryStmt.finallyStmt, tryStmt.rethrowLabel
  2769. Emit "}"
  2770. ' run the Finally block if an exception was thrown from the Try block and handled by one of the Catch blocks
  2771. Emit "bbExLeave();"
  2772. If opt_debug Then Emit "bbOnDebugPopExState();"
  2773. EmitFinallyJmp tryStmt.finallyStmt, tryStmt.endTryLabel
  2774. Else
  2775. Emit s + "{"
  2776. Emit "goto " + tryStmt.rethrowLabel.munged + ";"
  2777. Emit "}"
  2778. Emit "goto " + tryStmt.endTryLabel.munged + ";"
  2779. End If
  2780. Emit "}"
  2781. Emit "break;"
  2782. Else ' no catch blocks exist
  2783. Emit "case 1:"
  2784. 'If opt_debug Then Emit "bbOnDebugPopExState();"
  2785. End If
  2786. If tryStmt.finallyStmt Then
  2787. ' run the Finally block if an exception was thrown from a Catch block
  2788. Emit "case 2: {"
  2789. If opt_debug Then Emit "bbOnDebugPopExState();"
  2790. Emit "ex = bbExCatch();"
  2791. Emit "retptr = &&" + tryStmt.rethrowLabel.munged + ";"
  2792. Emit TransLabel(tryStmt.finallyStmt.finallyLabel)
  2793. EmitFinallyStmt tryStmt.finallyStmt
  2794. Emit "goto *retptr;"
  2795. Emit TransLabel(tryStmt.rethrowLabel)
  2796. Emit "bbExThrow(ex);"
  2797. Emit "}"
  2798. Emit "break;"
  2799. Else
  2800. Emit TransLabel(tryStmt.rethrowLabel)
  2801. Emit "bbExThrow(ex);"
  2802. End If
  2803. Emit "}"
  2804. Emit "}"
  2805. Emit TransLabel(tryStmt.endTryLabel)
  2806. End Method
  2807. Method EmitFinallyJmp(stmt:TFinallyStmt, returnLabel:TLoopLabelDecl)
  2808. Emit "retptr = &&" + returnLabel.munged + ";"
  2809. Emit "goto " + stmt.finallyLabel.munged + ";"
  2810. End Method
  2811. Method EmitFinallyStmt(f:TFinallyStmt)
  2812. Emit "{"
  2813. EmitLocalDeclarations f.block
  2814. EmitBlock f.block
  2815. Emit "}"
  2816. End Method
  2817. Method TransUsingStmt$( stmt:TUsingStmt )
  2818. Emit "{"
  2819. EmitLocalDeclarations stmt.wrapperBlock
  2820. EmitBlock stmt.wrapperBlock
  2821. Emit "}"
  2822. End Method
  2823. Method EmitDebugEnterScope(block:TBlockDecl)
  2824. Local scopeIndex:Int
  2825. Local count:Int
  2826. For Local decl:TDecl = EachIn block.Decls()
  2827. If TLocalDecl(decl) Or TConstDecl(decl) Or TGlobalDecl(decl) Then
  2828. count :+ 1
  2829. End If
  2830. Next
  2831. If _app.mainFunc = block Then
  2832. For Local decl:TDecl = EachIn _app.mainModule.Decls()
  2833. If TConstDecl(decl) Then
  2834. count :+ 1
  2835. End If
  2836. Next
  2837. End If
  2838. ' a method also includes "Self" reference back to parent Type
  2839. If TFuncDecl(block) And TFuncDecl(block).IsMethod() Then
  2840. count :+ 1
  2841. End If
  2842. If Not count Then
  2843. Emit "struct BBDebugScope __scope = {"
  2844. Else
  2845. Emit "struct BBDebugScope_" + count + " __scope = {"
  2846. _app.scopeDefs.Insert(String(count), "")
  2847. End If
  2848. If TFuncDecl(block) Then
  2849. Emit "BBDEBUGSCOPE_FUNCTION,"
  2850. If _app.mainFunc = block Then
  2851. ' use the filename as the base function name
  2852. Emit Enquote(StripExt(StripDir(_app.mainModule.filepath))) + ","
  2853. Else
  2854. Emit Enquote(TFuncDecl(block).ident) + ","
  2855. End If
  2856. Else
  2857. Emit "BBDEBUGSCOPE_LOCALBLOCK,"
  2858. Emit "(char*)0,"
  2859. End If
  2860. Emit "{"
  2861. If TFuncDecl(block) And TFuncDecl(block).IsMethod() Then
  2862. Emit "{"
  2863. Emit "BBDEBUGDECL_LOCAL,"
  2864. Emit "~qSelf~q,"
  2865. Emit Enquote(TransDebugScopeType(TClassDecl(block.scope).objectType)) + ","
  2866. Local prefix:String = "&"
  2867. If block.ClassScope().IsStruct() Then
  2868. prefix = ""
  2869. End If
  2870. Emit ".var_address=" + prefix + "o,"
  2871. Emit "(void (*)(void**))0"
  2872. Emit "},"
  2873. scopeIndex :+ 1
  2874. End If
  2875. ' add module consts
  2876. If _app.mainFunc = block Then
  2877. ' consts
  2878. For Local cdecl:TConstDecl = EachIn _app.mainModule.Decls()
  2879. EmitConstDebugScope(cdecl)
  2880. scopeIndex :+ 1
  2881. Next
  2882. End If
  2883. ' block consts and globals
  2884. ' consts
  2885. For Local cdecl:TConstDecl = EachIn block.Decls()
  2886. EmitConstDebugScope(cdecl)
  2887. scopeIndex :+ 1
  2888. Next
  2889. ' globals
  2890. For Local gdecl:TGlobalDecl = EachIn block.Decls()
  2891. EmitGlobalDebugScope(gdecl, scopeIndex)
  2892. scopeIndex :+ 1
  2893. Next
  2894. ' iterate through decls and add as appropriate
  2895. For Local decl:TDecl = EachIn block.Decls()
  2896. Local ldecl:TLocalDecl = TLocalDecl(decl)
  2897. If ldecl Then
  2898. Emit "{"
  2899. If ldecl.ty._flags & TType.T_VAR Then
  2900. Emit "BBDEBUGDECL_VARPARAM,"
  2901. Else
  2902. Emit "BBDEBUGDECL_LOCAL,"
  2903. End If
  2904. Emit Enquote(ldecl.ident) + ","
  2905. Emit Enquote(TransDebugScopeType(ldecl.ty)) + ","
  2906. Emit ".var_address=&" + ldecl.munged + ","
  2907. Emit "(void (*)(void**))0"
  2908. Emit "},"
  2909. scopeIndex :+ 1
  2910. End If
  2911. Next
  2912. Emit "{"
  2913. Emit "BBDEBUGDECL_END,"
  2914. Emit "(char*)0,"
  2915. Emit "(char*)0,"
  2916. Emit ".var_address=(void*)0,"
  2917. Emit "(void (*)(void**))0"
  2918. Emit "}"
  2919. Emit "}"
  2920. Emit "};"
  2921. ' threaded global
  2922. For Local gdecl:TGlobalDecl = EachIn block.Decls()
  2923. If gdecl.IsThreaded() Then
  2924. Emit "__scope.decls[" + gdecl.scopeIndex + "].var_address = &" + gdecl.munged + ";"
  2925. End If
  2926. Next
  2927. Emit "bbOnDebugEnterScope((BBDebugScope *)&__scope);"
  2928. End Method
  2929. Method EmitClassThreadedGlobalDebugInit(classDecl:TClassDecl)
  2930. Local classid:String = classDecl.munged
  2931. ' classid + "_scope
  2932. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  2933. If decl.IsThreaded() Then
  2934. Emit classid + "_scope.decls[" + decl.scopeIndex + "].var_address = &" + decl.munged + ";"
  2935. End If
  2936. Next
  2937. End Method
  2938. Method TransDebugNullObjectError:String(variable:String, cdecl:TClassDecl)
  2939. If cdecl.IsStruct() Or cdecl.ident = "String" Or cdecl.ident = "___Array" Then
  2940. 'Return cdecl.munged + "NullObjectTest(" + variable + ")"
  2941. Return variable
  2942. Else
  2943. Return Bra(Bra(TransObject(cdecl)) + "bbNullObjectTest((BBObject*)" + variable + ")")
  2944. End If
  2945. End Method
  2946. Method TransAssignStmt$( stmt:TAssignStmt )
  2947. If Not stmt.rhs Return stmt.lhs.Trans()
  2948. Local rhs$=stmt.rhs.Trans()
  2949. Local lhs$=stmt.lhs.TransVar()
  2950. Local s:TStringBuffer=New TStringBuffer( 256 )
  2951. Local cast:String
  2952. If TObjectType(stmt.lhs.exprType) And (Not TObjectType(stmt.lhs.exprType).classdecl.IsStruct() Or IsPointerType(stmt.lhs.exprType)) Then
  2953. If Not IsNumericType(stmt.rhs.exprType) Then
  2954. cast = Bra(TransType(stmt.lhs.exprType, ""))
  2955. End If
  2956. End If
  2957. If IsPointerType(stmt.lhs.exprType, TType.T_BYTE) And rhs = "&bbNullObject" Then
  2958. rhs = "0"
  2959. End If
  2960. If stmt.op = ":%" Then
  2961. If TDecimalType(stmt.lhs.exprType) Or TDecimalType(stmt.rhs.exprType) Then
  2962. Return lhs + "=bbFloatMod" + Bra(lhs + "," + rhs)
  2963. End If
  2964. End If
  2965. If TStringType(stmt.lhs.exprType) 'Or TStringVarPtrType(stmt.lhs.exprType) Then
  2966. ' s:+ "{"
  2967. ' s:+ "BBSTRING tmp=" + lhs + ";~n"
  2968. If stmt.op = ":+" Then
  2969. s.Append( lhs ).Append( "=bbStringConcat(" ).Append( lhs ).Append( "," ).Append( rhs ).Append( ")" )
  2970. Else If rhs = "&bbNullObject" Then
  2971. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( "&bbEmptyString" )
  2972. Else
  2973. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( rhs )
  2974. End If
  2975. ' s :+ ";~nBBRETAIN(" + lhs +")~n"
  2976. ' s :+ "BBRELEASE(tmp)~n"
  2977. ' s:+ "}"
  2978. Else If TVarPtrType(stmt.lhs.exprType) Then
  2979. If TCastExpr(stmt.rhs) And IsNumericType(TCastExpr(stmt.rhs).expr.exprType) Then
  2980. rhs = TCastExpr(stmt.rhs).expr.Trans()
  2981. End If
  2982. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( cast ).Append( rhs )
  2983. Else If TArrayType(stmt.lhs.exprType) Then
  2984. If stmt.op = ":+" Then
  2985. s.Append( lhs ).Append( "=bbArrayConcat(" ).Append( TransArrayType(TArrayType(stmt.lhs.exprType).elemType) ).Append( "," ).Append( lhs ).Append( "," ).Append( rhs ).Append( ")" )
  2986. Else If rhs = "&bbNullObject" Then
  2987. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( "&bbEmptyArray" )
  2988. Else
  2989. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( cast ).Append( rhs )
  2990. End If
  2991. Else If (TFunctionPtrType(stmt.lhs.exprType) <> Null Or IsPointerType(stmt.lhs.exprType, TType.T_BYTE)) And TInvokeExpr(stmt.rhs) And Not TInvokeExpr(stmt.rhs).invokedWithBraces Then
  2992. If Not cast And TFunctionPtrType(stmt.lhs.exprType) Then
  2993. Local fp:TFunctionPtrType = TFunctionPtrType(stmt.lhs.exprType)
  2994. If fp.func.cdets Then
  2995. cast = fp.func.cdets.TransCast()
  2996. End If
  2997. End If
  2998. rhs = TInvokeExpr(stmt.rhs).decl.munged
  2999. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( cast ).Append( rhs )
  3000. Else If TObjectType(stmt.lhs.exprType) And TObjectType(stmt.lhs.exprType).classDecl.IsStruct() And rhs = "&bbNullObject" Then
  3001. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( cast ).Append( "{}" )
  3002. Else
  3003. s.Append( lhs ).Append( TransAssignOp( stmt.op ) ).Append( cast ).Append( rhs )
  3004. End If
  3005. If DEBUG Then
  3006. DebugObject(stmt.lhs.exprType, lhs, Null, True)
  3007. End If
  3008. Return s.ToString()
  3009. End Method
  3010. Method TransThrowStmt:String( stmt:TThrowStmt )
  3011. Local s:String = "bbExThrow((BBObject *)"
  3012. s:+ stmt.expr.Trans()
  3013. s :+ ")"
  3014. Return s
  3015. End Method
  3016. Method TransAssertStmt$( stmt:TAssertStmt )
  3017. If opt_debug Then
  3018. Emit "if (!" + Bra(stmt.expr.Trans()) + ") {"
  3019. Emit "brl_blitz_RuntimeError(" + stmt.elseExpr.Trans() + ");"
  3020. Emit "}"
  3021. End If
  3022. End Method
  3023. Method TransEndStmt$( stmt:TEndStmt )
  3024. Emit "bbEnd();"
  3025. End Method
  3026. Method TransReleaseStmt$( stmt:TReleaseStmt )
  3027. Emit "bbHandleRelease" + Bra(stmt.expr.Trans()) + ";"
  3028. End Method
  3029. Method TransRestoreDataStmt$( stmt:TRestoreDataStmt )
  3030. Emit "_defDataOffset = &_defData[" + TDataLabelExpr(stmt.label).dataDef.label.index + "];"
  3031. End Method
  3032. Method TransReadDataStmt$( stmt:TReadDataStmt )
  3033. For Local expr:TExpr = EachIn stmt.args
  3034. ' buffer overflow test
  3035. If opt_debug Then
  3036. Emit "if (_defDataOffset - _defData >= " + TDefDataDecl.count + ") brl_blitz_OutOfDataError();"
  3037. End If
  3038. Emit expr.Trans() + " = " + TransDefDataConversion(expr.exprType) + Bra("_defDataOffset++") + ";"
  3039. Next
  3040. End Method
  3041. Method TransNativeStmt$( stmt:TNativeStmt)
  3042. Emit stmt.raw
  3043. End Method
  3044. Method TransFullName:String(decl:TDecl)
  3045. Local s:String
  3046. If decl.scope Then
  3047. s:+ TransFullName(decl.scope)
  3048. End If
  3049. If s Then
  3050. s :+ " : "
  3051. End If
  3052. If TModuleDecl(decl) Then
  3053. s:+ decl.ModuleScope().munged
  3054. Else
  3055. s :+ decl.ident
  3056. End If
  3057. If TFuncDecl(decl) Then
  3058. s:+ "()"
  3059. End If
  3060. Return s
  3061. End Method
  3062. Method ClassHasObjectField:Int(classDecl:TClassDecl, checked:TMap = Null)
  3063. If Not checked Then
  3064. checked = New TMap
  3065. End If
  3066. If checked.Contains(classDecl) Then
  3067. Return False
  3068. End If
  3069. checked.Insert(classDecl, "")
  3070. If classDecl.superClass Then
  3071. If ClassHasObjectField(classDecl.superClass, checked) Then
  3072. Return True
  3073. End If
  3074. End If
  3075. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3076. If Not decl.IsSemanted() Then
  3077. decl.Semant()
  3078. End If
  3079. If IsManagedType(decl.ty, checked) Then
  3080. Return True
  3081. End If
  3082. Next
  3083. Return False
  3084. End Method
  3085. Method IsManagedType:Int(ty:TType, checked:TMap = Null)
  3086. If IsPointerType(ty) Then
  3087. Return False
  3088. End If
  3089. If TStringType(ty) Or (TArrayType(ty) And Not TArrayType(ty).isStatic) Or (TObjectType(ty) And Not TObjectType(ty).classDecl.IsStruct()) Then
  3090. Return True
  3091. End If
  3092. If TArrayType(ty) And TArrayType(ty).isStatic Then
  3093. Return IsManagedType(TArrayType(ty).elemType)
  3094. End If
  3095. If TObjectType(ty) And TObjectType(ty).classDecl.IsStruct() Then
  3096. If ClassHasObjectField(TObjectType(ty).classDecl, checked) Then
  3097. Return True
  3098. End If
  3099. End If
  3100. Return False
  3101. End Method
  3102. '***** Declarations *****
  3103. Rem
  3104. Method EmitFuncProto( decl:TFuncDecl )
  3105. PushMungScope
  3106. decl.Semant
  3107. MungDecl decl
  3108. 'Find decl we override
  3109. Local odecl:TFuncDecl=decl
  3110. While odecl.overrides
  3111. odecl=odecl.overrides
  3112. Wend
  3113. Local args$
  3114. For Local arg:TArgDecl=EachIn odecl.argDecls
  3115. If args args:+","
  3116. args:+TransType( arg.ty )
  3117. Next
  3118. Local t$=TransType( odecl.retType )+" "+decl.munged+Bra( args )
  3119. If decl.IsAbstract() t:+"=0"
  3120. Local q$
  3121. If decl.IsExtern() q:+"extern "
  3122. If decl.IsMethod() q:+"virtual "
  3123. If decl.IsStatic() And decl.ClassScope() q:+"static "
  3124. Emit q+t+";"
  3125. PopMungScope
  3126. End Method
  3127. End Rem
  3128. Method EmitBBClassFuncProto( decl:TFuncDecl)
  3129. 'PushMungScope
  3130. BeginLocalScope
  3131. 'DebugStop
  3132. ' decl.Semant
  3133. ' MungDecl decl
  3134. 'Find decl we override
  3135. Local odecl:TFuncDecl=decl
  3136. 'If odecl.overrides And Not odecl.returnTypeSubclassed Then Return
  3137. 'DebugLog decl.ident
  3138. ' While odecl.overrides
  3139. ' odecl=odecl.overrides
  3140. ' Wend
  3141. MungDecl decl
  3142. Local id$=decl.munged
  3143. Local pre:String
  3144. If decl.IsMethod() Then
  3145. id :+ "_m"
  3146. pre = "m_"
  3147. Else
  3148. id :+ "_f"
  3149. pre = "f_"
  3150. End If
  3151. Local bk:String = ";"
  3152. 'Local pre:String = "typedef "
  3153. 'If odecl.IsExtern() Then
  3154. ' pre = "extern "
  3155. 'End If
  3156. 'DebugLog "id = " + id
  3157. Emit id + " " + pre + FuncDeclMangleIdent(odecl) + ";"
  3158. ' If Not proto Or (proto And Not odecl.IsExtern()) Then
  3159. Rem
  3160. If Not TFunctionPtrType(odecl.retType) Then
  3161. If Not odecl.castTo Then
  3162. Emit pre + TransType( odecl.retType, "" )+" "+ Bra("*" + id)+Bra( args ) + bk
  3163. Else
  3164. If Not odecl.noCastGen Then
  3165. Emit pre + odecl.castTo +" "+Bra("*" + id)+Bra( args ) + bk
  3166. End If
  3167. End If
  3168. Else
  3169. If Not odecl.castTo Then
  3170. Emit pre + TransType( odecl.retType, id )+" "+Bra( args ) + bk
  3171. Else
  3172. If Not odecl.noCastGen Then
  3173. Emit pre + odecl.castTo +" "+Bra( args ) + bk
  3174. End If
  3175. End If
  3176. End If
  3177. For Local t$=EachIn argCasts
  3178. Emit t
  3179. Next
  3180. ' End If
  3181. End Rem
  3182. 'PopMungScope
  3183. EndLocalScope
  3184. End Method
  3185. Method FuncDeclMangleIdent:String(fdecl:TFuncDecl)
  3186. If (Not fdecl.ClassScope()) Or (equalsBuiltInFunc(fdecl.classScope(), fdecl)) Then
  3187. Return fdecl.ident
  3188. End If
  3189. If Not fdecl.mangled Then
  3190. Local id:String = fdecl.ident
  3191. If fdecl.attrs & FUNC_OPERATOR Then
  3192. id = MungSymbol(id)
  3193. End If
  3194. fdecl.mangled = id + MangleMethod(fdecl)
  3195. End If
  3196. Return fdecl.mangled
  3197. ' If fdecl.olIndex Then
  3198. ' Return fdecl.ident + fdecl.olIndex
  3199. ' Else
  3200. ' Return fdecl.ident
  3201. ' End If
  3202. End Method
  3203. Method EmitClassFuncProto( decl:TFuncDecl, isStruct:Int = False, emitFuncProtos:Int = True)
  3204. 'PushMungScope
  3205. BeginLocalScope
  3206. decl.Semant
  3207. MungDecl decl
  3208. 'Find decl we override
  3209. Local odecl:TFuncDecl=decl
  3210. ' If odecl.overrides Then Return
  3211. While odecl.overrides
  3212. odecl=odecl.overrides
  3213. Wend
  3214. 'Generate 'args' string and arg casts
  3215. Local args:TStringBuffer = New TStringBuffer(128)
  3216. ' pass object for method
  3217. If decl.IsMethod() Then
  3218. args.Append( TransObject(decl.scope, True) )
  3219. End If
  3220. Local argCasts:TStackList =New TStackList
  3221. For Local i:Int=0 Until decl.argDecls.Length
  3222. Local arg:TArgDecl=decl.argDecls[i]
  3223. Local oarg:TArgDecl=odecl.argDecls[i]
  3224. MungDecl arg
  3225. If args.Length() > 0 Then args.Append(",")
  3226. If Not TFunctionPtrType(oarg.ty) Then
  3227. If Not odecl.castTo Then
  3228. args.Append( TransType( oarg.ty, arg.munged ) )
  3229. If TArrayType(oarg.ty) And TArrayType(oarg.ty).isStatic Then
  3230. args.Append( "[" ).Append( TArrayType(oarg.ty).length ).Append( "]" )
  3231. End If
  3232. Else
  3233. args.Append( oarg.castTo ).Append( " " ).Append( arg.munged )
  3234. End If
  3235. Else
  3236. If Not odecl.castTo Then
  3237. args.Append( TransType( oarg.ty, arg.munged ) )
  3238. Else
  3239. args.Append( oarg.castTo )
  3240. End If
  3241. End If
  3242. If arg.ty.EqualsType( oarg.ty ) Continue
  3243. Local t$=arg.munged
  3244. arg.munged=""
  3245. MungDecl arg
  3246. 'argCasts.Push TransType( arg.ty, arg.munged )+" "+arg.munged+"=("+TransType(arg.ty, "")+")"+Bra(t)+";"
  3247. Next
  3248. Local id$=decl.munged
  3249. Local bk:String = ";"
  3250. Local pre:String = "typedef "
  3251. Local api:String
  3252. If decl.IsMethod() Then
  3253. id :+ "_m"
  3254. Else
  3255. id :+ "_f"
  3256. End If
  3257. If decl.attrs & DECL_API_STDCALL Then
  3258. api = " __stdcall "
  3259. End If
  3260. 'If odecl.IsExtern() Then
  3261. ' pre = "extern "
  3262. 'End If
  3263. Local argStr:String = args.ToString()
  3264. Local sb:TStringBuffer = New TStringBuffer(128)
  3265. ' If Not proto Or (proto And Not odecl.IsExtern()) Then
  3266. 'If emitFuncProtos
  3267. If Not TFunctionPtrType(decl.retType) Then
  3268. If Not odecl.castTo Then
  3269. If Not isStruct Then
  3270. sb.Append( pre ).Append( TransType( decl.retType, "" ) )
  3271. sb.Append( " (" )
  3272. sb.Append( api ).Append( "*" ).Append( id )
  3273. sb.Append( ")(" )
  3274. sb.Append( argStr )
  3275. sb.Append( ")" ).Append( bk )
  3276. Emit sb.ToString()
  3277. End If
  3278. If emitFuncProtos
  3279. sb.SetLength(0)
  3280. If decl.IsMethod() Then
  3281. sb.Append( TransType(decl.retType, "") ).Append( " _" ).Append( decl.munged )
  3282. sb.Append( "(" ).Append( argStr ).Append( ")" )
  3283. sb.Append( bk )
  3284. Emit sb.ToString()
  3285. Else
  3286. sb.Append( TransType(decl.retType, "") ).Append( api )
  3287. sb.Append( " " ).Append( decl.munged )
  3288. sb.Append( "(" ).Append( argStr ).Append( ")" )
  3289. sb.Append( bk )
  3290. Emit sb.ToString()
  3291. End If
  3292. End If
  3293. Else
  3294. If Not odecl.noCastGen Then
  3295. If Not isStruct Then
  3296. If Not decl.overrides Or decl.returnTypeSubclassed Then
  3297. sb.Append( pre ).Append( odecl.castTo )
  3298. sb.Append( " (" ).Append( api ).Append( "*" ).Append( id )
  3299. sb.Append( ")(" )
  3300. sb.Append( argStr )
  3301. sb.Append( ")" )
  3302. sb.Append( bk )
  3303. Emit sb.ToString()
  3304. End If
  3305. End If
  3306. If emitFuncProtos
  3307. sb.SetLength(0)
  3308. If decl.IsMethod() Then
  3309. sb.Append( odecl.castTo ).Append( " _" ).Append( decl.munged )
  3310. sb.Append( "(" ).Append( argStr ).Append( ")" )
  3311. sb.Append( bk )
  3312. Emit sb.ToString()
  3313. Else
  3314. sb.Append( odecl.castTo ).Append( " " ).Append( decl.munged )
  3315. sb.Append( "(" ).Append( argStr ).Append( ")" )
  3316. sb.Append( bk )
  3317. Emit sb.ToString()
  3318. End If
  3319. End If
  3320. End If
  3321. End If
  3322. Else
  3323. If Not odecl.castTo Then
  3324. If Not argStr Then
  3325. ' for function pointer return type, we need to generate () regardless of whether there are
  3326. ' args or not.
  3327. argStr = " "
  3328. End If
  3329. ' emit function ptr typedef
  3330. sb.Append( pre ).Append( TransType( decl.retType, id + "x" ) ).Append( bk )
  3331. Emit sb.ToString()
  3332. sb.SetLength(0)
  3333. ' emit actual typedef (with return type of above typedef)
  3334. sb.Append( pre ).Append( TransType( decl.retType, id, argStr, True ) ).Append( bk )
  3335. Emit sb.ToString()
  3336. Else
  3337. If Not odecl.noCastGen Then
  3338. sb.Append( pre ).Append( odecl.castTo )
  3339. sb.Append( " (" )
  3340. sb.Append( argStr )
  3341. sb.Append( ")" )
  3342. sb.Append( bk )
  3343. Emit sb.ToString()
  3344. End If
  3345. End If
  3346. End If
  3347. For Local t$=EachIn argCasts
  3348. Emit t
  3349. Next
  3350. 'End If
  3351. 'PopMungScope
  3352. EndLocalScope
  3353. End Method
  3354. Method EmitFuncDecl( decl:TFuncDecl, proto:Int = False, classFunc:Int = False, createReflectionWrapper:Int = True )
  3355. 'If Not proto And decl.IsAbstract() Return
  3356. Local tmpDebug:Int = opt_debug
  3357. If decl.isNoDebug() Then
  3358. opt_debug = False
  3359. End If
  3360. BeginLocalScope
  3361. decl.Semant
  3362. MungDecl decl
  3363. ' export defs?
  3364. If opt_apptype And opt_def And decl.attrs & DECL_EXPORT Then
  3365. If Not _appInstance.exportDefs.Contains(decl) Then
  3366. _appInstance.exportDefs.AddLast(decl)
  3367. End If
  3368. End If
  3369. ' emit nested functions/classes
  3370. If Not proto Then
  3371. ' emit nested classes
  3372. For Local cdecl:TClassDecl = EachIn decl._decls
  3373. MungDecl cdecl
  3374. EmitClassProto(cdecl, False)
  3375. EmitClassDecl(cdecl)
  3376. Next
  3377. ' emit nested protos
  3378. For Local fdecl:TFuncDecl = EachIn decl._decls
  3379. EmitFuncDecl(fdecl, True, classFunc)
  3380. Next
  3381. ' emit nested bodies
  3382. For Local fdecl:TFuncDecl = EachIn decl._decls
  3383. EmitFuncDecl(fdecl, proto, classFunc)
  3384. Next
  3385. End If
  3386. 'Find decl we override
  3387. Local odecl:TFuncDecl=decl
  3388. While odecl.overrides
  3389. odecl=odecl.overrides
  3390. Wend
  3391. 'Generate 'args' string and arg casts
  3392. Local args:TStringBuffer = New TStringBuffer(256)
  3393. ' pass object for method
  3394. If decl.IsMethod() Then
  3395. args.Append( TransObject(decl.scope, True) ).Append( " o" )
  3396. End If
  3397. Local argCasts:TStackList =New TStackList
  3398. For Local i:Int=0 Until decl.argDecls.Length
  3399. Local arg:TArgDecl=decl.argDecls[i]
  3400. Local oarg:TArgDecl=odecl.argDecls[i]
  3401. MungDecl arg, True
  3402. If args.Length() > 0 Then args.Append(",")
  3403. If Not TFunctionPtrType(oarg.ty) Then
  3404. If Not odecl.castTo Then
  3405. args.Append(TransType( oarg.ty, arg.munged )).Append(" ").Append(arg.munged)
  3406. If TArrayType(oarg.ty) And TArrayType(oarg.ty).isStatic Then
  3407. args.Append("[").Append(TArrayType(oarg.ty).length).Append("]")
  3408. End If
  3409. Else
  3410. args.Append( oarg.castTo ).Append(" ").Append(arg.munged)
  3411. End If
  3412. Else
  3413. If Not odecl.castTo Then
  3414. args.Append(TransType( oarg.ty, arg.munged ))
  3415. Else
  3416. args.Append( oarg.castTo )
  3417. End If
  3418. End If
  3419. If arg.ty.EqualsType( oarg.ty ) Continue
  3420. Local t$=arg.munged
  3421. arg.munged=""
  3422. MungDecl arg
  3423. argCasts.Push TransType( arg.ty, arg.munged )+" "+arg.munged+"=("+TransType(arg.ty, "")+")"+Bra(t)+";"
  3424. Next
  3425. Local id$=decl.munged
  3426. If classFunc Then
  3427. If decl.IsMethod() Then
  3428. id = "_" + id
  3429. End If
  3430. Else
  3431. If Not odecl.IsExtern() Then
  3432. id = id
  3433. End If
  3434. End If
  3435. Local iterations:Int = 1
  3436. If decl.attrs & DECL_INLINE Then
  3437. iterations = 2
  3438. End If
  3439. Local origProto:Int = proto
  3440. For Local i:Int = 0 Until iterations
  3441. proto = origProto
  3442. Local bk:String = "{"
  3443. Local pre:String
  3444. Local api:String
  3445. If proto Then
  3446. If odecl.IsExtern() Then
  3447. pre = "extern "
  3448. If TFunctionPtrType(decl.retType) Then
  3449. pre = ""
  3450. End If
  3451. End If
  3452. If decl.attrs & DECL_INLINE And i = 0 Then
  3453. pre = "inline "
  3454. Else
  3455. bk = ";"
  3456. End If
  3457. Else If decl.attrs & DECL_INLINE And i = 0 Then
  3458. pre = "extern "
  3459. bk = ";"
  3460. End If
  3461. If decl.attrs & DECL_INLINE Then
  3462. Select i
  3463. Case 0
  3464. pre = "#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L~n" + pre
  3465. Case 1
  3466. pre = "#else~n" + pre
  3467. End Select
  3468. End If
  3469. If decl.attrs & DECL_API_STDCALL Then
  3470. api = " __stdcall "
  3471. End If
  3472. Local sb:TStringBuffer = New TStringBuffer(256)
  3473. ' If Not proto Or (proto And Not odecl.IsExtern()) Then
  3474. If Not IsStandardFunc(decl.munged) Then
  3475. If Not TFunctionPtrType(odecl.retType) Then
  3476. If Not odecl.castTo Then
  3477. sb.Append( pre ).Append( TransType( decl.retType, "" ) )
  3478. sb.Append( api ).Append( " " ).Append( id )
  3479. sb.Append( "(" ).Append( args.ToString() ).Append( ")" ).Append( bk )
  3480. Emit sb.ToString()
  3481. Else
  3482. If Not odecl.noCastGen Then
  3483. sb.Append( pre ).Append( odecl.castTo )
  3484. sb.Append( api ).Append( " " ).Append( id )
  3485. sb.Append( "(" ).Append( args.ToString() ).Append( ")" ).Append( bk )
  3486. Emit sb.ToString()
  3487. End If
  3488. End If
  3489. Else
  3490. If Not odecl.castTo Then
  3491. If args.Length() = 0 Then
  3492. ' for function pointer return type, we need to generate () regardless of whether there are
  3493. ' args or not.
  3494. args.SetLength(0)
  3495. args.Append(" ")
  3496. End If
  3497. sb.Append( pre ).Append( TransType( decl.retType, id, args.ToString() ) ).Append( bk )
  3498. Emit sb.ToString()
  3499. Else
  3500. If Not odecl.noCastGen Then
  3501. sb.Append( pre ).Append( odecl.castTo )
  3502. sb.Append( " (" )
  3503. sb.Append( args.ToString() )
  3504. sb.Append( ")" ).Append( bk )
  3505. Emit sb.ToString()
  3506. End If
  3507. End If
  3508. End If
  3509. For Local t$=EachIn argCasts
  3510. Emit t
  3511. Next
  3512. End If
  3513. If decl.attrs & DECL_INLINE And i = 0 Then
  3514. proto = Not proto
  3515. End If
  3516. If Not proto Then
  3517. If opt_coverage Then
  3518. EmitCoverageFunction(decl)
  3519. End If
  3520. If PROFILER Then
  3521. Select decl.ident
  3522. Case "WritePixel", "PixelPtr", "CopyPixels", "ConvertPixels", "ConvertPixelsToStdFormat", "ConvertPixelsFromStdFormat"
  3523. Case "OnDebugEnterScope", "OnDebugEnterStm", "GetDbgState", "OnDebugLeaveScope", "OnDebugPopExState", "OnDebugPushExState"
  3524. Default
  3525. DebugPrint("", TransFullName(decl))
  3526. End Select
  3527. End If
  3528. If DEBUG Then
  3529. For Local i:Int=0 Until decl.argDecls.Length
  3530. Local arg:TArgDecl=decl.argDecls[i]
  3531. DebugObject(arg.ty, arg.munged, id)
  3532. Next
  3533. End If
  3534. If decl.IsAbstract() Then
  3535. Emit "brl_blitz_NullMethodError();"
  3536. If Not TVoidType( decl.retType ) Then
  3537. Local ret:TReturnStmt = New TReturnStmt.Create(New TConstExpr.Create( decl.retType,"" ).Semant())
  3538. ret.fRetType = decl.retType
  3539. Emit ret.Trans() + ";"
  3540. unreachable = False
  3541. End If
  3542. Else
  3543. decl.Semant()
  3544. If opt_debug And decl.IsMethod() And Not TClassDecl(decl.scope).IsStruct() Then
  3545. Emit TransDebugNullObjectError("o", TClassDecl(decl.scope)) + ";"
  3546. End If
  3547. EmitLocalDeclarations(decl)
  3548. EmitBlock decl
  3549. End If
  3550. Emit "}"
  3551. End If
  3552. Next
  3553. If decl.attrs & DECL_INLINE Then
  3554. Emit "#endif"
  3555. End If
  3556. ' reset label ids
  3557. contLabelId = 0
  3558. exitLabelId = 0
  3559. EndLocalScope
  3560. 'PopMungScope
  3561. opt_debug = tmpDebug
  3562. ' wrapper function for invocation via reflection
  3563. If createReflectionWrapper And Not proto Then EmitReflectionWrapper Null, decl
  3564. End Method
  3565. Method EmitReflectionWrapper(classDecl:TClassDecl, decl:TFuncDecl)
  3566. ' classDecl is only required for constructors
  3567. Local funcName:String
  3568. If decl.IsCTor() Then
  3569. funcName = MungedConstructorName(classDecl, decl)
  3570. Else If decl.IsMethod() Then
  3571. funcName = "_" + decl.munged
  3572. Else
  3573. funcName = decl.munged
  3574. End If
  3575. ' wrapper signature
  3576. Emit "void " + funcName + "_ReflectionWrapper(void** buf){"
  3577. Local offsetStr:TStringBuffer = New TStringBuffer(256)
  3578. Local sb:TStringBuffer = New TStringBuffer(256)
  3579. ' call to original method/function
  3580. If TVoidType(decl.retType) Or decl.IsCTor() Then
  3581. Emit funcName + "("
  3582. Else
  3583. offsetStr.Append("(")
  3584. ArgSizeStr(TransType(decl.retType, ""), offsetStr)
  3585. offsetStr.Append(")")
  3586. 'offsetStr = Bra(ArgSizeStr(TransType(decl.retType, "")))
  3587. sb.Append("*(").Append(TransType(TType.MapToPointerType(decl.retType.Copy()), ""))
  3588. sb.Append(")(buf) = ").Append(funcName).Append("(")
  3589. Emit sb.ToString()
  3590. ' Emit "*" + Bra(TransType(TType.MapToPointerType(decl.retType.Copy()), "")) + "(buf) = " + funcName + "("
  3591. End If
  3592. ' arguments for call
  3593. Local startIndex:Int = 0
  3594. If decl.IsMethod() Or decl.IsCTor() Then startIndex = -1 ' add Self argument
  3595. For Local a:Int = startIndex Until decl.argDecls.Length
  3596. Local argTypeStr:String
  3597. Local argPtrTypeStr:String
  3598. If a = -1 Then
  3599. If decl.IsCTor() Then argTypeStr = TransObject(classDecl, True) Else argTypeStr = TransObject(decl.scope, True)
  3600. argPtrTypeStr = argTypeStr + "*"
  3601. Else
  3602. argTypeStr = TransType(decl.argDecls[a].ty, "")
  3603. Local ty:TType = decl.argDecls[a].ty
  3604. ' for static arrays we need to spin up an extra level of indirection
  3605. ' as the array is passed as a pointer to the array
  3606. If TArrayType(ty) And TArrayType(ty).isStatic Then
  3607. ty = TType.MapToPointerType(ty.Copy())
  3608. End If
  3609. argPtrTypeStr = TransType(TType.MapToPointerType(ty.Copy()), "")
  3610. End If
  3611. Local argSb:TStringBuffer = sb
  3612. argSb.SetLength(0)
  3613. argSb.Append("~t*(").Append(argPtrTypeStr).Append(")")
  3614. If offsetStr.Length() Then
  3615. argSb.Append("(buf + (").Append(offsetStr.ToString()).Append("))")
  3616. offsetStr.Append(" + ")
  3617. Else
  3618. argSb.Append("(buf)")
  3619. End If
  3620. offsetStr.Append("(")
  3621. ArgSizeStr(argTypeStr, offsetStr)
  3622. offsetStr.Append(")")
  3623. 'offsetStr :+ Bra(ArgSizeStr(argTypeStr))
  3624. If a <> decl.argDecls.Length - 1 Then argSb.Append(",")
  3625. Emit argSb.ToString()
  3626. Next
  3627. Emit ");"
  3628. Emit "}"
  3629. Function ArgSizeStr(typeStr:String, sb:TStringBuffer)
  3630. ' rounds up to pointer size
  3631. ' Local sb:TStringBuffer = New TStringBuffer(128)
  3632. sb.Append("((sizeof(").Append( typeStr ).Append(") - 1) * (sizeof(").Append( typeStr ).Append(") != 0)) / sizeof(void*) + 1")
  3633. ' Return sb.ToString()
  3634. End Function
  3635. End Method
  3636. Method EmitLocalDeclarations(decl:TScopeDecl, ignoreVar:TValDecl = Null)
  3637. If opt_debug Then
  3638. For Local ldecl:TLocalDecl = EachIn decl.Decls()
  3639. If ldecl <> ignoreVar Then
  3640. If Not TArgDecl(ldecl) And Not ldecl.generated Then
  3641. MungDecl ldecl
  3642. Local ty:TType = ldecl.ty
  3643. Local t:String = TransLocalDeclNoInit(ldecl)
  3644. ' If TObjectType( ty ) And TObjectType( ty ).classDecl.IsStruct() Then
  3645. ' t :+ "={}"
  3646. ' End If
  3647. Emit t + ";"
  3648. End If
  3649. End If
  3650. Next
  3651. End If
  3652. End Method
  3653. Method EmitClassFieldsProto(classDecl:TClassDecl)
  3654. If classDecl.superClass Then
  3655. EmitClassFieldsProto(classDecl.superClass)
  3656. End If
  3657. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3658. decl.Semant()
  3659. If Not TFunctionPtrType(decl.ty) Then
  3660. If classDecl.IsExtern() Then
  3661. Emit TransType(decl.ty, "") + " " + decl.ident + ";"
  3662. Else
  3663. Local t:String = TransType(decl.ty, classDecl.actual.munged) + " _" + classDecl.actual.munged.ToLower() + "_" + decl.IdentLower()
  3664. If TArrayType(decl.ty) And TArrayType(decl.ty).isStatic Then
  3665. t :+ "[" + TArrayType(decl.ty).length + "]"
  3666. End If
  3667. Emit t + ";"
  3668. End If
  3669. Else
  3670. If classDecl.IsExtern() Then
  3671. Emit TransType(decl.ty, decl.ident) + ";"
  3672. Else
  3673. Emit TransType(decl.ty, "_" + classDecl.actual.munged.ToLower() + "_" + decl.IdentLower()) + ";"
  3674. End If
  3675. End If
  3676. Next
  3677. End Method
  3678. Method EmitClassGlobalsProto(classDecl:TClassDecl)
  3679. Local sb:TStringBuffer = New TStringBuffer(128)
  3680. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  3681. decl.Semant()
  3682. sb.SetLength(0)
  3683. If TFunctionPtrType(decl.ty) Then
  3684. sb.Append("extern ").Append(TransThreadedGlobal(decl)).Append(TransRefType(decl.ty, decl.munged)).Append(";")
  3685. Emit sb.ToString()
  3686. Else
  3687. sb.Append("extern ").Append(TransThreadedGlobal(decl)).Append(TransRefType(decl.ty, "")).Append(" ").Append(decl.munged).Append(";")
  3688. Emit sb.ToString()
  3689. End If
  3690. Next
  3691. End Method
  3692. Method BBClassClassFuncProtoBuildList( classDecl:TClassDecl, list:TList )
  3693. Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls()
  3694. For Local decl:TFuncDecl=EachIn fdecls
  3695. If Not decl.IsSemanted()
  3696. decl.Semant()
  3697. End If
  3698. If Not equalsBuiltInFunc(classDecl, decl) And Not equalsTorFunc(classDecl, decl) Then
  3699. Local fdecl:TFuncDecl = classDecl.GetLatestFuncDecl(decl)
  3700. list.AddLast(fdecl)
  3701. End If
  3702. Next
  3703. End Method
  3704. Method EmitBBClassClassFuncProto( classDecl:TClassDecl )
  3705. Local list:TList = New TList
  3706. BBClassClassFuncProtoBuildList(classDecl, list)
  3707. For Local fdecl:TFuncDecl = EachIn list
  3708. EmitBBClassFuncProto( fdecl )
  3709. Next
  3710. End Method
  3711. Method EmitClassProto( classDecl:TClassDecl, emitFuncProtos:Int = True )
  3712. If classDecl.args Then
  3713. Return
  3714. End If
  3715. Local classid$=classDecl.munged
  3716. Local superid$
  3717. If classDecl.superClass Then
  3718. superid=classDecl.superClass.actual.munged
  3719. End If
  3720. 'Emit "void _" + classid + "_New" + Bra(TransObject(classdecl) + " o") + ";"
  3721. Local sb:TStringBuffer = New TStringBuffer(256)
  3722. If emitFuncProtos Then
  3723. EmitClassDeclNewListProto(classDecl)
  3724. If classHierarchyGetFunction(classDecl, "Delete") Then
  3725. sb.Append("void _").Append(classid).Append("_Delete(")
  3726. sb.Append(TransObject(classDecl)).Append(" o);")
  3727. Emit sb.ToString()
  3728. End If
  3729. If classGetFunction(classDecl, "ToString") Then
  3730. sb.SetLength(0)
  3731. sb.Append("BBSTRING _").Append(classid).Append("_ToString(")
  3732. sb.Append(TransObject(classDecl)).Append(" o);")
  3733. Emit sb.ToString()
  3734. End If
  3735. If classGetFunction(classDecl, "Compare") Then
  3736. sb.SetLength(0)
  3737. sb.Append("BBINT _").Append(classid).Append("_Compare(")
  3738. sb.Append(TransObject(classDecl)).Append(" o, BBOBJECT otherObject);")
  3739. Emit sb.ToString()
  3740. End If
  3741. If classGetFunction(classDecl, "SendMessage") Then
  3742. sb.SetLength(0)
  3743. sb.Append("BBOBJECT _").Append(classid).Append("_SendMessage(")
  3744. sb.Append(TransObject(classDecl)).Append(" o, BBOBJECT message, BBOBJECT source);")
  3745. Emit sb.ToString()
  3746. End If
  3747. If classGetFunction(classDecl, "HashCode") Then
  3748. sb.SetLength(0)
  3749. sb.Append("BBUINT _").Append(classid).Append("_HashCode(")
  3750. sb.Append(TransObject(classDecl)).Append(" o);")
  3751. Emit sb.ToString()
  3752. End If
  3753. If classGetFunction(classDecl, "Equals") Then
  3754. sb.SetLength(0)
  3755. sb.Append("BBINT _").Append(classid).Append("_Equals(")
  3756. sb.Append(TransObject(classDecl)).Append(" o, BBOBJECT otherObject);")
  3757. Emit sb.ToString()
  3758. End If
  3759. End If
  3760. 'Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
  3761. classDecl.SemantParts()
  3762. 'Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls(Null, False)
  3763. For Local decl:TDecl=EachIn classDecl.Decls()
  3764. 'For Local fdecl:TFuncDecl = EachIn fdecls
  3765. Local fdecl:TFuncDecl =TFuncDecl( decl )
  3766. If fdecl
  3767. If Not equalsBuiltInFunc(classDecl, fdecl) And Not equalsTorFunc(classDecl, fdecl) Then
  3768. EmitClassFuncProto( fdecl, , emitFuncProtos )
  3769. Continue
  3770. End If
  3771. EndIf
  3772. Local gdecl:TGlobalDecl =TGlobalDecl( decl )
  3773. If gdecl
  3774. MungDecl gdecl
  3775. ' Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
  3776. Continue
  3777. EndIf
  3778. Next
  3779. Emit ""
  3780. ' emit the class structure
  3781. Emit "#ifndef DEF_BBClass_" + classid + "_STRUCT"
  3782. Emit "#define DEF_BBClass_" + classid + "_STRUCT"
  3783. Emit "struct BBClass_" + classid + " {"
  3784. If classDecl.superClass.ident = "Object" Then
  3785. Emit "BBClass* super;"
  3786. Else
  3787. Emit "struct BBClass_" + classDecl.superClass.munged + "* super;"
  3788. End If
  3789. Emit "void (*free)( BBObject *o );"
  3790. Emit "BBDebugScope* debug_scope;"
  3791. Emit "unsigned int instance_size;"
  3792. Emit "void (*ctor)( BBOBJECT o );"
  3793. Emit "void (*dtor)( BBOBJECT o );"
  3794. If classHierarchyGetFunction(classDecl, "ToString") Then
  3795. Emit "BBSTRING (*ToString)( struct " + classidForFunction(classDecl, "ToString") + "_obj* x );"
  3796. Else
  3797. Emit "BBSTRING (*ToString)( BBOBJECT x );"
  3798. End If
  3799. If classHierarchyGetFunction(classDecl, "Compare") Then
  3800. Emit "BBINT (*Compare)( struct " + classidForFunction(classDecl, "Compare") + "_obj* x, BBOBJECT y );"
  3801. Else
  3802. Emit "int (*Compare)( BBOBJECT x,BBOBJECT y );"
  3803. End If
  3804. If classHierarchyGetFunction(classDecl, "SendMessage") Then
  3805. Emit "BBOBJECT (*SendMessage)( struct " + classidForFunction(classDecl, "SendMessage") + "_obj* x, BBOBJECT m, BBOBJECT s );"
  3806. Else
  3807. Emit "BBOBJECT (*SendMessage)( BBOBJECT o,BBOBJECT m,BBOBJECT s );"
  3808. End If
  3809. If classHierarchyGetFunction(classDecl, "HashCode") Then
  3810. Emit "BBUINT (*HashCode)( struct " + classidForFunction(classDecl, "HashCode") + "_obj* x );"
  3811. Else
  3812. Emit "BBUINT (*HashCode)( BBOBJECT o );"
  3813. End If
  3814. If classHierarchyGetFunction(classDecl, "Equals") Then
  3815. Emit "BBINT (*Equals)( struct " + classidForFunction(classDecl, "Equals") + "_obj* x, BBOBJECT y );"
  3816. Else
  3817. Emit "BBINT (*Equals)( BBOBJECT o, BBOBJECT y );"
  3818. End If
  3819. Emit "BBINTERFACETABLE itable;"
  3820. Emit "void* extra;"
  3821. Emit "unsigned int obj_size;"
  3822. Emit "unsigned int instance_count;"
  3823. Emit "unsigned int fields_offset;"
  3824. EmitBBClassClassFuncProto(classDecl)
  3825. Emit "};~n"
  3826. If classDecl.IsInterface() Then
  3827. Emit "struct " + classid + "_methods {"
  3828. EmitBBClassClassFuncProto(classDecl)
  3829. Emit "};~n"
  3830. End If
  3831. Emit "struct " + classid + "_obj {"
  3832. Emit "struct BBClass_" + classid + "* clas;"
  3833. BeginLocalScope
  3834. EmitClassFieldsProto(classDecl)
  3835. EndLocalScope
  3836. Emit "};"
  3837. Emit "#endif~n"
  3838. sb.SetLength(0)
  3839. sb.Append("extern struct BBClass_").Append(classid).Append(" ").Append(classid).Append(";")
  3840. Emit sb.ToString()
  3841. EmitClassGlobalsProto(classDecl);
  3842. ' fields
  3843. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3844. MungDecl decl
  3845. Next
  3846. End Method
  3847. Method EmitExternClassFuncProto( classDecl:TClassDecl )
  3848. If classDecl.superClass Then
  3849. EmitExternClassFuncProto(classDecl.superClass)
  3850. End If
  3851. For Local decl:TFuncDecl = EachIn classDecl.Decls()
  3852. decl.Semant()
  3853. ' code is written as a method, but emitted as a function pointer
  3854. ' with self as the first parameter
  3855. Local func:TFuncDecl = TFuncDecl(decl.Copy())
  3856. Local argDecl:TArgDecl = New TArgDecl.Create("This", classDecl.objectType, Null)
  3857. func.argDecls = [argDecl] + func.argDecls
  3858. func.Semant()
  3859. Local ty:TFunctionPtrType = New TFunctionPtrType
  3860. ty.func = func
  3861. Emit TransType(ty, decl.Ident) + ";"
  3862. Next
  3863. End Method
  3864. Method EmitExternClassTypeFuncProto( classDecl:TClassDecl )
  3865. Local doneCtorDtor:Int
  3866. Local iDecl:TClassDecl
  3867. For Local decl:TFuncDecl = EachIn classDecl.GetAllOriginalFuncDecls(Null, True)
  3868. decl.Semant()
  3869. ' first interface preceeds ctor/dtor
  3870. If Not doneCtorDtor
  3871. If Not iDecl And TClassDecl(decl.scope).IsInterface() Then
  3872. iDecl = TClassDecl(decl.scope)
  3873. End If
  3874. If iDecl
  3875. If iDecl <> TClassDecl(decl.scope) Then
  3876. ' a different interface
  3877. doneCtorDtor = True
  3878. Emit "void(*_ctor)();"
  3879. Emit "void(*_dtor)();"
  3880. End If
  3881. Else
  3882. doneCtorDtor = True
  3883. Emit "void(*_ctor)();"
  3884. Emit "void(*_dtor)();"
  3885. End If
  3886. End If
  3887. ' code is written as a method, but emitted as a function pointer
  3888. ' with self as the first parameter
  3889. Local func:TFuncDecl = TFuncDecl(decl.Copy())
  3890. Local argDecl:TArgDecl = New TArgDecl.Create("This", classDecl.objectType, Null)
  3891. func.argDecls = [argDecl] + func.argDecls
  3892. func.Semant()
  3893. Local ty:TFunctionPtrType = New TFunctionPtrType
  3894. ty.func = func
  3895. Emit TransType(ty, decl.Ident) + ";"
  3896. Next
  3897. End Method
  3898. Method EmitExternClassProtoTypedef( classDecl:TClassDecl )
  3899. Emit "typedef struct " + classDecl.ident + " " + classDecl.ident + ";"
  3900. End Method
  3901. Method EmitExternClassProto( classDecl:TClassDecl )
  3902. ' vtable
  3903. Emit "struct " + classDecl.ident + "Vtbl {"
  3904. ' methods
  3905. If classDecl.IsInterface() Then
  3906. EmitExternClassFuncProto(classDecl)
  3907. Else
  3908. EmitExternClassTypeFuncProto(classDecl)
  3909. End If
  3910. Emit "};"
  3911. Emit "struct " + classDecl.ident + " {"
  3912. Emit "struct " + classDecl.ident + "Vtbl* vtbl;"
  3913. Emit "};"
  3914. End Method
  3915. Field emittedStructs:TList = New TList
  3916. Method EmitStructClassProto( classDecl:TClassDecl )
  3917. If classDecl.IsImported() Return
  3918. If emittedStructs.Contains(classDecl) Return
  3919. emittedStructs.AddLast(classDecl)
  3920. ' emit any dependent structs first
  3921. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3922. decl.Semant()
  3923. If TObjectType(decl.ty) And TObjectType(decl.ty).classDecl.IsStruct() Then
  3924. If Not emittedStructs.Contains(TObjectType(decl.ty).classDecl) Then
  3925. EmitStructClassProto(TObjectType(decl.ty).classDecl)
  3926. End If
  3927. End If
  3928. Next
  3929. If classDecl.IsExtern()
  3930. Emit "struct " + classDecl.ident + " {"
  3931. Else
  3932. EmitClassDeclNewListProto( classDecl )
  3933. For Local fdecl:TFuncDecl=EachIn classDecl.Decls()
  3934. If fdecl.IdentLower() <> "new" Then
  3935. EmitClassFuncProto( fdecl, True )
  3936. End If
  3937. Next
  3938. Emit "struct " + classDecl.munged + " {"
  3939. End If
  3940. BeginLocalScope
  3941. EmitClassFieldsProto(classDecl)
  3942. EndLocalScope
  3943. Emit "};"
  3944. EmitClassGlobalsProto(classDecl);
  3945. ' struct arrays
  3946. Emit "BBArray *bbArrayNew1DStruct_" + classDecl.munged + "(int length);"
  3947. Emit "BBArray *bbArraySliceStruct_" + classDecl.munged + "(BBArray *inarr, int beg, int end);"
  3948. End Method
  3949. Method classGetFunction:TDecl(classDecl:TClassDecl, func:String)
  3950. Local f:String = func.ToLower()
  3951. For Local decl:TFuncDecl = EachIn classDecl.Decls()
  3952. If Not decl.IsSemanted() Then
  3953. decl.Semant
  3954. End If
  3955. If decl.IdentLower() = f And equalsBuiltInFunc(classDecl.superClass, decl) Then
  3956. Return decl
  3957. End If
  3958. Next
  3959. Return Null
  3960. End Method
  3961. Method classHierarchyGetFunction:TDecl(classDecl:TClassDecl, func:String)
  3962. Local decl:TDecl = classGetFunction(classDecl, func)
  3963. If decl Then Return decl
  3964. If classDecl.superClass And classDecl.superClass.munged <> "bbObjectClass" Then
  3965. Return classHierarchyGetFunction(classDecl.superClass, func)
  3966. End If
  3967. Return Null
  3968. End Method
  3969. Method classidForFunction:String(classDecl:TClassDecl, func:String)
  3970. If classGetFunction(classDecl, func) Return classDecl.munged
  3971. If classDecl.superClass And classDecl.superClass.munged <> "bbObjectClass" Then
  3972. Return classidForFunction(classDecl.superClass, func)
  3973. End If
  3974. Return Null
  3975. End Method
  3976. Method EmitMark( id$,ty:TType,queue:Int )
  3977. If TObjectType( ty )
  3978. If id.EndsWith( ".p" )
  3979. If ty.GetClass().IsInterface() id=id[..-2] Else InternalErr "TCTranslator.EmitMark"
  3980. Else
  3981. If ty.GetClass().IsInterface() InternalErr "TCTranslator.EmitMark"
  3982. EndIf
  3983. If queue
  3984. Emit "gc_mark_q("+id+");"
  3985. Else
  3986. Emit "gc_mark("+id+");"
  3987. EndIf
  3988. Else If TArrayType( ty )
  3989. Emit "gc_mark("+id+");"
  3990. Return
  3991. EndIf
  3992. End Method
  3993. Method EmitClassConstsDebugScope(classDecl:TClassDecl, scopeIndex:Int Var)
  3994. For Local decl:TConstDecl = EachIn classDecl.Decls()
  3995. EmitConstDebugScope(decl)
  3996. scopeIndex :+ 1
  3997. Next
  3998. End Method
  3999. Method EmitConstDebugScope(decl:TConstDecl)
  4000. Emit "{"
  4001. Emit "BBDEBUGDECL_CONST,"
  4002. Emit Enquote(decl.ident) + ","
  4003. Emit Enquote(TransDebugScopeType(decl.ty) + TransDebugScopeModifiers(decl) + TransDebugMetaData(decl.metadata.metadataString)) + ","
  4004. _appInstance.mapStringConsts(decl.value)
  4005. Emit ".const_value=(BBString*)&" + StringConstId(decl.value) + ","
  4006. Emit "(void (*)(void**))0"
  4007. Emit "},"
  4008. End Method
  4009. Method EmitClassFieldsDebugScope(classDecl:TClassDecl, scopeIndex:Int Var)
  4010. ' Don't list superclass fields in our debug scope
  4011. 'If classDecl.superClass Then
  4012. ' EmitClassFieldsDebugScope(classDecl.superClass)
  4013. 'End If
  4014. Local offset:TStringBuffer = New TStringBuffer(128)
  4015. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  4016. Emit "{"
  4017. Emit "BBDEBUGDECL_FIELD,"
  4018. Emit Enquote(decl.ident) + ","
  4019. Emit Enquote(TransDebugScopeType(decl.ty) + TransDebugScopeModifiers(decl) + TransDebugMetaData(decl.metadata.metadataString)) + ","
  4020. offset.SetLength(0)
  4021. offset.Append(".field_offset=offsetof(struct ")
  4022. ' Local offset:String = ".field_offset=offsetof"
  4023. If classDecl.IsStruct() Then
  4024. offset.Append(classDecl.munged).Append( ",").Append( decl.munged )
  4025. Else
  4026. offset.Append(classDecl.munged).Append("_obj,").Append( decl.munged )
  4027. End If
  4028. ' If WORD_SIZE = 8 Then
  4029. ' Emit Bra("BBLONG") + offset
  4030. ' Else
  4031. offset.Append("),")
  4032. Emit offset.ToString()
  4033. Emit "(void (*)(void**))0"
  4034. ' End If
  4035. 'If Not TFunctionPtrType(decl.ty) Then
  4036. ' Emit TransType(decl.ty, classDecl.actual.munged) + " _" + classDecl.actual.munged.ToLower() + "_" + decl.ident.ToLower() + ";"
  4037. 'Else
  4038. ' Emit TransType(decl.ty, "_" + classDecl.actual.munged.ToLower() + "_" + decl.ident.ToLower()) + ";"
  4039. 'End If
  4040. Emit "},"
  4041. scopeIndex :+ 1
  4042. 'offset:+ decl.ty.GetSize()
  4043. Next
  4044. 'Return offset
  4045. End Method
  4046. Method EmitClassStandardMethodDebugScope(ident:String, ty:String, munged:String)
  4047. Emit "{"
  4048. Emit "BBDEBUGDECL_TYPEMETHOD,"
  4049. Emit Enquote(ident) + ","
  4050. Emit Enquote(ty) + ","
  4051. Emit ".func_ptr=(BBFuncPtr)&" + munged + ","
  4052. Emit "&" + munged + "_ReflectionWrapper"
  4053. Emit "},"
  4054. End Method
  4055. Method TransDebugMetaData:String(meta:String)
  4056. If meta Then
  4057. Return "{" + meta + "}"
  4058. End If
  4059. End Method
  4060. Method EmitBBClassFuncsDebugScope(classDecl:TClassDecl, decl:TFuncDecl)
  4061. Emit "{"
  4062. If decl.IsMethod() Or decl.IsCTor() Then
  4063. Emit "BBDEBUGDECL_TYPEMETHOD,"
  4064. Else
  4065. Emit "BBDEBUGDECL_TYPEFUNCTION,"
  4066. End If
  4067. Emit Enquote(decl.ident) + ","
  4068. Local s:String = "("
  4069. For Local i:Int = 0 Until decl.argDecls.length
  4070. If i Then
  4071. s:+ ","
  4072. End If
  4073. s:+ TransDebugScopeType(decl.argDecls[i].ty)
  4074. Next
  4075. s:+ ")"
  4076. If decl.retType And Not decl.IsCTor() Then
  4077. s:+ TransDebugScopeType(decl.retType)
  4078. End If
  4079. s:+ TransDebugScopeModifiers(decl) + TransDebugMetaData(decl.metadata.metadataString)
  4080. Emit Enquote(s) + ","
  4081. Local funcname:String
  4082. If decl.IsCTor() Then
  4083. ' only parameterized constructors here
  4084. ' the default constructor is handled as a special case in EmitClassFuncsDebugScope
  4085. funcname = MungedConstructorName(classDecl, decl)
  4086. Else If decl.IsMethod() Then
  4087. funcname = "_" + decl.munged
  4088. Else
  4089. funcname = decl.munged
  4090. End If
  4091. Emit ".func_ptr=(BBFuncPtr)&" + funcname + ","
  4092. Emit "&" + funcname + "_ReflectionWrapper"
  4093. Emit "},"
  4094. End Method
  4095. Method BBClassClassFuncsDebugScopeBuildList(classDecl:TClassDecl, list:TList)
  4096. 'Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
  4097. Local funcDecls:TFuncDecl[] = classDecl.GetAllFuncDecls(Null, False)
  4098. For Local fdecl:TFuncDecl = EachIn funcDecls
  4099. If Not fdecl.IsSemanted()
  4100. fdecl.Semant()
  4101. End If
  4102. If Not equalsBuiltInFunc(classDecl, fdecl) Then
  4103. Local ignore:Int
  4104. Local link:TLink=list._head._succ
  4105. While link<>list._head
  4106. Local ofdecl:TFuncDecl = TFuncDecl(link._value)
  4107. If fdecl.ident = ofdecl.ident And fdecl.EqualsArgs(ofdecl, True) And fdecl.scope <> ofdecl.scope Then
  4108. If fdecl.overrides Then
  4109. link._value = fdecl
  4110. ignore = True
  4111. Exit
  4112. End If
  4113. EndIf
  4114. link = link._succ
  4115. Wend
  4116. If Not ignore Then
  4117. list.AddLast(fdecl)
  4118. End If
  4119. Continue
  4120. End If
  4121. Next
  4122. End Method
  4123. Method EmitBBClassClassFuncsDebugScope(classDecl:TClassDecl)
  4124. Local list:TList = New TList
  4125. BBClassClassFuncsDebugScopeBuildList(classDecl, list)
  4126. For Local fdecl:TFuncDecl = EachIn list
  4127. EmitBBClassFuncsDebugScope(classDecl, fdecl)
  4128. Next
  4129. End Method
  4130. Method EmitClassFuncsDebugScope(classDecl:TClassDecl)
  4131. If classDecl.IsExtern() Return
  4132. Local classid$=classDecl.munged
  4133. Local superid$
  4134. If classDecl.superClass Then
  4135. superid = classDecl.superClass.actual.munged
  4136. End If
  4137. Local ret:String = "()i"
  4138. If opt_issuperstrict Then
  4139. ret = "()"
  4140. End If
  4141. If Not classDecl.IsInterface() And Not classDecl.IsStruct() Then
  4142. Local newDecl:TDecl = classGetFunction(classDecl, "New")
  4143. If newDecl Then
  4144. EmitClassStandardMethodDebugScope("New", ret + TransDebugScopeModifiers(newDecl) , "_" + classid + "_New")
  4145. Else
  4146. EmitClassStandardMethodDebugScope("New", ret, "_" + classid + "_New")
  4147. End If
  4148. End If
  4149. Local toStringDecl:TDecl = classGetFunction(classDecl, "ToString")
  4150. If toStringDecl Then
  4151. EmitClassStandardMethodDebugScope("ToString", "()$" + TransDebugScopeModifiers(toStringDecl), "_" + classidForFunction(classDecl, "ToString") + "_ToString")
  4152. 'Emit "_" + classid + "_ToString,"
  4153. End If
  4154. Local compareDecl:TDecl = classGetFunction(classDecl, "Compare")
  4155. If compareDecl Then
  4156. EmitClassStandardMethodDebugScope("Compare", "(:Object)i" + TransDebugScopeModifiers(compareDecl), "_" + classidForFunction(classDecl, "Compare") + "_Compare")
  4157. 'Emit "_" + classid + "_ObjectCompare,"
  4158. End If
  4159. Local sendMessageDecl:TDecl = classGetFunction(classDecl, "SendMessage")
  4160. If sendMessageDecl Then
  4161. EmitClassStandardMethodDebugScope("SendMessage", "(:Object, :Object):Object" + TransDebugScopeModifiers(sendMessageDecl), "_" + classidForFunction(classDecl, "SendMessage") + "_SendMessage")
  4162. 'Emit "_" + classid + "_SendMessage,"
  4163. End If
  4164. Local hashCodeDecl:TDecl = classGetFunction(classDecl, "HashCode")
  4165. If hashCodeDecl Then
  4166. EmitClassStandardMethodDebugScope("HashCode", "()u" + TransDebugScopeModifiers(hashCodeDecl), "_" + classidForFunction(classDecl, "HashCode") + "_HashCode")
  4167. End If
  4168. Local equalsDecl:TDecl = classGetFunction(classDecl, "Equals")
  4169. If equalsDecl Then
  4170. EmitClassStandardMethodDebugScope("Equals", "()u" + TransDebugScopeModifiers(equalsDecl), "_" + classidForFunction(classDecl, "Equals") + "_Equals")
  4171. End If
  4172. EmitBBClassClassFuncsDebugScope(classDecl)
  4173. End Method
  4174. Method EmitClassFuncsDebugScopeCifs(classDecl:TClassDecl)
  4175. If classDecl.IsExtern() Return
  4176. Local classid$=classDecl.munged
  4177. Local superid$
  4178. If classDecl.superClass Then
  4179. superid = classDecl.superClass.actual.munged
  4180. End If
  4181. Local list:TList = New TList
  4182. BBClassClassFuncsDebugScopeBuildList(classDecl, list)
  4183. For Local func:TFuncDecl = EachIn list
  4184. Local s:String
  4185. If func.IsMethod() Or func.IsCTor() Then
  4186. s :+ "&ffi_type_pointer"
  4187. End If
  4188. For Local i:Int = 0 Until func.argDecls.length
  4189. If s Then
  4190. s :+ ","
  4191. End If
  4192. s :+ TransCifType(func.argDecls[i].ty)
  4193. Next
  4194. Emit "ffi_type * bbCif_" + func.munged + "_arg_types[] = [" + s + "];"
  4195. Emit "BBCif bbCif_" + func.munged + " = {"
  4196. Emit "FFI_DEFAULT_ABI,"
  4197. Local count:Int
  4198. If func.IsMethod() Then
  4199. count = 1
  4200. End If
  4201. Emit count + func.argDecls.length + ","
  4202. If TVoidType(func.retType) Then
  4203. Emit "&ffi_type_void,"
  4204. Else
  4205. Emit TransCifType(func.retType) + ","
  4206. End If
  4207. Emit "bbCif_" + func.munged + "_arg_types"
  4208. Emit "};"
  4209. Next
  4210. End Method
  4211. Method EmitClassGlobalDebugScope( classDecl:TClassDecl, scopeIndex:Int Var )
  4212. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  4213. EmitGlobalDebugScope(decl, scopeIndex)
  4214. scopeIndex :+ 1
  4215. Next
  4216. End Method
  4217. Method EmitGlobalDebugScope( decl:TGlobalDecl, scopeIndex:Int )
  4218. Emit "{"
  4219. Emit "BBDEBUGDECL_GLOBAL,"
  4220. Emit Enquote(decl.ident) + ","
  4221. Emit Enquote(TransDebugScopeType(decl.ty) + TransDebugScopeModifiers(decl) + TransDebugMetaData(decl.metadata.metadataString)) + ","
  4222. If decl.IsThreaded() Then
  4223. Emit ".var_address=0,"
  4224. decl.scopeIndex = scopeIndex
  4225. Else
  4226. Emit ".var_address=(void*)&" + decl.munged + ","
  4227. End If
  4228. Emit "(void (*)(void**))0"
  4229. Emit "},"
  4230. End Method
  4231. Method CountBBClassClassFuncsDebugScope(classDecl:TClassDecl, count:Int Var)
  4232. For Local decl:TDecl=EachIn classDecl.GetAllFuncDecls(Null, False)
  4233. Local fdecl:TFuncDecl =TFuncDecl( decl )
  4234. If fdecl
  4235. If Not equalsBuiltInFunc(classDecl, fdecl) Then
  4236. count :+ 1
  4237. End If
  4238. End If
  4239. Next
  4240. End Method
  4241. Method CountClassConstsDebugScope(classDecl:TClassDecl, count:Int Var)
  4242. For Local decl:TConstDecl = EachIn classDecl.Decls()
  4243. count :+ 1
  4244. Next
  4245. End Method
  4246. Method CountClassFieldsDebugScope(classDecl:TClassDecl, count:Int Var)
  4247. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  4248. count :+ 1
  4249. Next
  4250. End Method
  4251. Method DebugScopeDeclCount:Int(classDecl:TClassDecl)
  4252. Local count:Int = 2 ' "New" counts as first one
  4253. ' but we don't use "New" for interfaces or for extern structs...
  4254. If classDecl.IsInterface() Or (classDecl.IsExtern() And classDecl.IsStruct()) Then
  4255. count :- 1
  4256. ' ...or for regular structs, because GetAllFuncDecls returns New() but equalsBuiltInFunc returns False for it
  4257. Else If classDecl.IsStruct() Then
  4258. count :- 1
  4259. End If
  4260. ' consts
  4261. CountClassConstsDebugScope(classDecl, count)
  4262. ' fields
  4263. CountClassFieldsDebugScope(classDecl, count)
  4264. ' standard methods
  4265. If classGetFunction(classDecl, "ToString") Then
  4266. count :+ 1
  4267. End If
  4268. If classGetFunction(classDecl, "Compare") Then
  4269. count :+ 1
  4270. End If
  4271. If classGetFunction(classDecl, "SendMessage") Then
  4272. count :+ 1
  4273. End If
  4274. If classGetFunction(classDecl, "HashCode") Then
  4275. count :+ 1
  4276. End If
  4277. If classGetFunction(classDecl, "Equals") Then
  4278. count :+ 1
  4279. End If
  4280. ' methods and functions
  4281. CountBBClassClassFuncsDebugScope(classDecl, count)
  4282. ' class globals
  4283. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  4284. count :+ 1
  4285. Next
  4286. Return count
  4287. End Method
  4288. Method EmitClassDecl( classDecl:TClassDecl )
  4289. If classDecl.args Then
  4290. Return
  4291. End If
  4292. ' don't emit instanceof classes unless opt_apptype is set
  4293. If classDecl.instanceof And Not opt_apptype Then
  4294. Return
  4295. End If
  4296. PushEnv classDecl
  4297. 'If classDecl.IsTemplateInst()
  4298. ' Return
  4299. 'EndIf
  4300. If classDecl.IsExtern() And Not classDecl.IsStruct() Then
  4301. Return
  4302. EndIf
  4303. Local classid$=classDecl.munged
  4304. Local superid$
  4305. If classDecl.superClass Then
  4306. superid = classDecl.superClass.actual.munged
  4307. End If
  4308. If Not classDecl.IsExtern() Then
  4309. ' process nested classes
  4310. For Local cdecl:TClassDecl = EachIn classDecl._decls
  4311. MungDecl cdecl
  4312. EmitClassProto(cdecl, False)
  4313. EmitClassDecl(cdecl)
  4314. Next
  4315. ' process nested functions for new
  4316. Local decl:TFuncDecl
  4317. Try
  4318. decl = classDecl.FindFuncDecl("new",,,,,True,SCOPE_CLASS_HEIRARCHY)
  4319. Catch e:String
  4320. End Try
  4321. If decl And decl.scope = classDecl Then ' only our own New method, not any from superclasses
  4322. decl.Semant
  4323. ' emit nested protos
  4324. For Local fdecl:TFuncDecl = EachIn decl._decls
  4325. EmitFuncDecl(fdecl, True, False)
  4326. Next
  4327. ' emit nested bodies
  4328. For Local fdecl:TFuncDecl = EachIn decl._decls
  4329. EmitFuncDecl(fdecl, False, False)
  4330. Next
  4331. End If
  4332. EmitClassDeclNewList(classDecl)
  4333. If Not (classDecl.attrs & CLASS_STRUCT) Then
  4334. ' process nested functions for delete
  4335. decl = classDecl.FindFuncDecl("delete",,,,,,SCOPE_CLASS_HEIRARCHY)
  4336. If decl Then
  4337. decl.Semant
  4338. ' emit nested protos
  4339. For Local fdecl:TFuncDecl = EachIn decl._decls
  4340. EmitFuncDecl(fdecl, True, False)
  4341. Next
  4342. ' emit nested bodies
  4343. For Local fdecl:TFuncDecl = EachIn decl._decls
  4344. EmitFuncDecl(fdecl, False, False)
  4345. Next
  4346. End If
  4347. If classHierarchyGetFunction(classDecl, "Delete") Then
  4348. EmitClassDeclDelete(classDecl)
  4349. End If
  4350. End If
  4351. Rem
  4352. 'fields ctor
  4353. Emit classid+"::"+classid+"(){"
  4354. For Local decl:TDecl=EachIn classDecl.Semanted()
  4355. Local fdecl:TFieldDecl=TFieldDecl( decl )
  4356. If Not fdecl Continue
  4357. Emit TransField(fdecl,Null)+"="+fdecl.init.Trans()+";"
  4358. Next
  4359. Emit "}"
  4360. End Rem
  4361. Local reserved:String = ",New,Delete,".ToLower()
  4362. 'methods
  4363. For Local decl:TDecl=EachIn classDecl.Decls()
  4364. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4365. If fdecl
  4366. If reserved.Find("," + fdecl.IdentLower() + ",") = -1 Then
  4367. EmitGDBDebug(fdecl)
  4368. EmitFuncDecl fdecl, , True
  4369. Continue
  4370. End If
  4371. EndIf
  4372. 'Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  4373. 'If gdecl
  4374. ' Emit TransRefType( gdecl.ty )+" "+classid+"::"+gdecl.munged+";"
  4375. ' Continue
  4376. ' EndIf
  4377. Next
  4378. Rem
  4379. 'gc_mark
  4380. Emit "void "+classid+"::mark(){"
  4381. If classDecl.superClass
  4382. Emit classDecl.superClass.actual.munged+"::mark();"
  4383. EndIf
  4384. For Local decl:TDecl=EachIn classDecl.Semanted()
  4385. Local fdecl:TFieldDecl=TFieldDecl( decl )
  4386. If fdecl EmitMark TransField(fdecl,Null),fdecl.ty,True
  4387. Next
  4388. Emit "}"
  4389. End Rem
  4390. For Local decl:TDecl=EachIn classDecl.Semanted()
  4391. Local gdecl:TGlobalDecl =TGlobalDecl( decl )
  4392. If gdecl
  4393. If TFunctionPtrType(gdecl.ty) Then
  4394. Emit TransThreadedGlobal(gdecl) + TransRefType( gdecl.ty, gdecl.munged ) + ";"
  4395. Else
  4396. Emit TransThreadedGlobal(gdecl) + TransRefType( gdecl.ty, "" )+" "+ gdecl.munged+";"
  4397. End If
  4398. Continue
  4399. EndIf
  4400. Next
  4401. reserved = ",New,Delete,ToString,Compare,SendMessage,HashCode,Equals,_reserved3_,".ToLower()
  4402. If (classDecl.attrs & CLASS_STRUCT) Then
  4403. Emit "BBARRAYNEW1DSTRUCT_FUNC" + Bra(classid + "," + classid + ", _" + classid + "_New" + ", " + EnQuote("@" + classDecl.ident))
  4404. Emit "BBARRAYSLICESTRUCT_FUNC" + Bra(classid + "," + classid + ", _" + classid + "_New" + ", " + EnQuote("@" + classDecl.ident))
  4405. End If
  4406. End If
  4407. ' cif defs
  4408. 'EmitClassFuncsDebugScopeCifs(classDecl)
  4409. 'Emit "struct _" + classid + "_DebugScope{"
  4410. 'Emit "int kind;"
  4411. 'Emit "const char *name;"
  4412. 'Emit "BBDebugDecl decls[" + DebugScopeDeclCount(classDecl) + "];"
  4413. 'Emit "};"
  4414. Local count:Int = DebugScopeDeclCount(classDecl)
  4415. ' debugscope
  4416. If count > 1 Then
  4417. _app.scopeDefs.Insert(String(count - 1), "")
  4418. Emit "struct BBDebugScope_" + (count - 1) + " " + classid + "_scope ={"
  4419. Else
  4420. Emit "struct BBDebugScope " + classid + "_scope ={"
  4421. End If
  4422. If classDecl.IsInterface() Then
  4423. Emit "BBDEBUGSCOPE_USERINTERFACE,"
  4424. Else If classDecl.IsStruct() Then
  4425. Emit "BBDEBUGSCOPE_USERSTRUCT,"
  4426. Else
  4427. Emit "BBDEBUGSCOPE_USERTYPE,"
  4428. End If
  4429. Emit Enquote(classDecl.ident + TransDebugScopeModifiers(classDecl) + TransDebugMetaData(classDecl.metadata.metadataString)) + ","
  4430. Emit "{"
  4431. Local scopeIndex:Int
  4432. ' debug const decls
  4433. EmitClassConstsDebugScope(classDecl, scopeIndex)
  4434. ' debug field decls
  4435. EmitClassFieldsDebugScope(classDecl, scopeIndex)
  4436. ' debug global decls
  4437. EmitClassGlobalDebugScope(classDecl, scopeIndex)
  4438. ' debug func decls
  4439. EmitClassFuncsDebugScope(classDecl)
  4440. If classDecl.IsStruct() Then
  4441. Emit "{"
  4442. Emit "BBDEBUGDECL_END,"
  4443. Emit "(char*)0,"
  4444. Emit "(char*)0,"
  4445. Emit ".struct_size=sizeof(struct " + classid + "),"
  4446. Emit "(void (*)(void**))0"
  4447. Emit "}"
  4448. Else
  4449. Emit "{"
  4450. Emit "BBDEBUGDECL_END,"
  4451. Emit "(char*)0,"
  4452. Emit "(char*)0,"
  4453. Emit ".var_address=(void*)0,"
  4454. Emit "(void (*)(void**))0"
  4455. Emit "}"
  4456. End If
  4457. Emit "}"
  4458. Emit "};"
  4459. Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls()
  4460. Local implementedInterfaces:TMap = classDecl.GetInterfaces()
  4461. Local ifcCount:Int
  4462. Local sb:TStringBuffer = New TStringBuffer(128)
  4463. If Not classDecl.IsStruct() Then
  4464. ' interface class implementation
  4465. 'If Not classDecl.IsInterface()
  4466. If Not implementedInterfaces.IsEmpty() Then
  4467. Emit "struct " + classid + "_vdef {"
  4468. For Local ifc:TClassDecl = EachIn implementedInterfaces.Values()
  4469. Emit "struct " + ifc.munged + "_methods interface_" + ifc.ident + ";"
  4470. ifcCount :+ 1
  4471. Next
  4472. Emit "};~n"
  4473. Emit "static struct BBInterfaceOffsets " + classid + "_ifc_offsets[] = {"
  4474. For Local ifc:TClassDecl = EachIn implementedInterfaces.Values()
  4475. Emit "{&" + ifc.munged + "_ifc, offsetof(struct " + classid + "_vdef, interface_" + ifc.ident + ")},"
  4476. Next
  4477. Emit "};~n"
  4478. Emit "struct " + classid + "_vdef " + classid + "_ifc_vtable = {"
  4479. For Local ifc:TClassDecl = EachIn implementedInterfaces.Values()
  4480. Emit ".interface_" + ifc.ident + "={"
  4481. Local dups:TMap = New TMap
  4482. For Local func:TFuncDecl = EachIn ifc.GetImplementedFuncs()
  4483. If func.IsMethod() Then
  4484. MungDecl func
  4485. Local cast:String = Bra( func.munged + "_m" )
  4486. For Local f:TFuncDecl = EachIn fdecls
  4487. Mungdecl f
  4488. If f.ident = func.ident And f.EqualsFunc(func) Then
  4489. sb.SetLength(0)
  4490. sb.Append(f.ident).Append("_")
  4491. 'Local id:String = f.ident + "_"
  4492. For Local arg:TArgDecl = EachIn f.argDecls
  4493. ' id :+ TransMangleType(arg.ty)
  4494. TransMangleTypeToBuf(arg.ty, sb)
  4495. Next
  4496. Local id:String = sb.ToString()
  4497. If Not dups.ValueForKey(id) Then
  4498. Emit cast + "_" + f.munged + ","
  4499. dups.Insert(id, "")
  4500. End If
  4501. Exit
  4502. End If
  4503. Next
  4504. End If
  4505. Next
  4506. Emit "},"
  4507. Next
  4508. Emit "};~n"
  4509. Emit "struct BBInterfaceTable " + classid + "_itable = {"
  4510. Emit classid + "_ifc_offsets,"
  4511. Emit "&" + classid + "_ifc_vtable,"
  4512. Emit ifcCount
  4513. Emit "};~n"
  4514. End If
  4515. 'End If
  4516. Emit "struct BBClass_" + classid + " " + classid + "={"
  4517. ' super class reference
  4518. Emit "&" + classDecl.superClass.munged + ","
  4519. Emit "bbObjectFree,"
  4520. ' debugscope
  4521. Emit "(BBDebugScope*)&" + classid + "_scope,"
  4522. ' object instance size
  4523. Emit "sizeof" + Bra("struct " + classid + "_obj") + ","
  4524. ' standard methods
  4525. Emit "(void (*)(BBOBJECT))_" + classid + "_New,"
  4526. If Not classHierarchyGetFunction(classDecl, "Delete") Then
  4527. Emit "bbObjectDtor,"
  4528. Else
  4529. Emit "(void (*)(BBOBJECT))_" + classid + "_Delete,"
  4530. End If
  4531. If classHierarchyGetFunction(classDecl, "ToString") Then
  4532. Emit "_" + classidForFunction(classDecl, "ToString") + "_ToString,"
  4533. Else
  4534. Emit "bbObjectToString,"
  4535. End If
  4536. If classHierarchyGetFunction(classDecl, "Compare") Then
  4537. Emit "_" + classidForFunction(classDecl, "Compare") + "_Compare,"
  4538. Else
  4539. Emit "bbObjectCompare,"
  4540. End If
  4541. If classHierarchyGetFunction(classDecl, "SendMessage") Then
  4542. Emit "_" + classidForFunction(classDecl, "SendMessage") + "_SendMessage,"
  4543. Else
  4544. Emit "bbObjectSendMessage,"
  4545. End If
  4546. If classHierarchyGetFunction(classDecl, "HashCode") Then
  4547. Emit "_" + classidForFunction(classDecl, "HashCode") + "_HashCode,"
  4548. Else
  4549. Emit "bbObjectHashCode,"
  4550. End If
  4551. If classHierarchyGetFunction(classDecl, "Equals") Then
  4552. Emit "_" + classidForFunction(classDecl, "Equals") + "_Equals,"
  4553. Else
  4554. Emit "bbObjectEquals,"
  4555. End If
  4556. 'Emit "public:"
  4557. 'fields
  4558. 'For Local decl:TDecl=EachIn classDecl.Semanted()
  4559. ' Local fdecl:TFieldDecl =TFieldDecl( decl )
  4560. ' If fdecl
  4561. ' Emit TransRefType( fdecl.ty )+" "+fdecl.munged+";"
  4562. ' Continue
  4563. ' EndIf
  4564. 'Next
  4565. 'fields ctor
  4566. 'Emit classid+"();"
  4567. 'methods
  4568. 'For Local decl:TDecl=EachIn classDecl.Semanted()
  4569. '
  4570. ' Local fdecl:TFuncDecl =TFuncDecl( decl )
  4571. ' If fdecl
  4572. ' EmitFuncProto fdecl
  4573. ' Continue
  4574. ' EndIf
  4575. '
  4576. ' Local gdecl:TGlobalDecl =TGlobalDecl( decl )
  4577. ' If gdecl
  4578. ' Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
  4579. ' Continue
  4580. ' EndIf
  4581. 'Next
  4582. 'gc mark
  4583. 'Emit "void mark();"
  4584. If implementedInterfaces.IsEmpty() Then
  4585. ' itable
  4586. Emit "0,"
  4587. ' extra pointer
  4588. Emit "0,"
  4589. Else
  4590. Emit "&" + classid + "_itable,"
  4591. ' extra pointer
  4592. Emit "0,"
  4593. End If
  4594. ' obj_size
  4595. Emit TransObjectSize(classDecl)
  4596. ' instance_count
  4597. Emit ",0"
  4598. ' fields_offset
  4599. Emit TransFirstFieldOffset(classDecl)
  4600. ' methods/funcs
  4601. 'reserved = "New,Delete,ToString,ObjectCompare,SendMessage".ToLower()
  4602. 'For Local decl:TFuncDecl = EachIn classDecl.Decls()
  4603. For Local decl:TFuncDecl = EachIn fdecls
  4604. If Not equalsBuiltInFunc(classDecl, decl) And Not equalsTorFunc(classDecl, decl) Then
  4605. Local fdecl:TFuncDecl = classDecl.GetLatestFuncDecl(decl)
  4606. MungDecl decl
  4607. sb.SetLength(0)
  4608. sb.Append(",")
  4609. 'Local t:String = ","
  4610. If fdecl <> decl Then
  4611. MungDecl fdecl
  4612. If decl.IsMethod() Then
  4613. sb.Append("(").Append(fdecl.munged).Append("_m)")
  4614. Else
  4615. sb.Append("(").Append(fdecl.munged).Append("_f)")
  4616. End If
  4617. End If
  4618. If decl.IsMethod() Then
  4619. sb.Append("_")
  4620. End If
  4621. sb.Append(decl.munged)
  4622. Emit sb.ToString()
  4623. End If
  4624. Next
  4625. Emit "};~n"
  4626. If classDecl.IsInterface() Then
  4627. sb.SetLength(0)
  4628. sb.Append("const struct BBInterface ").Append(classid).Append("_ifc = { (BBClass *)&")
  4629. sb.Append(classid).Append(", (const char *) ~q").Append(classDecl.ident).Append("~q };")
  4630. Emit sb.ToString()
  4631. Else
  4632. End If
  4633. End If
  4634. PopEnv
  4635. End Method
  4636. Method EmitEnumDecl(decl:TEnumDecl)
  4637. Local id:String = decl.munged
  4638. Emit "struct BBEnum" + decl.munged + "{"
  4639. Emit "const char * name;"
  4640. Emit "char * type;"
  4641. Emit "char * atype;"
  4642. Emit "int flags;"
  4643. Emit "int length;"
  4644. Emit "void * values;"
  4645. Emit "BBString * names[" + decl.values.length + "];"
  4646. Emit "};"
  4647. If decl.isFlags Then
  4648. Local s:String
  4649. For Local value:TEnumValueDecl = EachIn decl.values
  4650. If s Then
  4651. s :+ "|"
  4652. End If
  4653. s :+ value.Value()
  4654. Next
  4655. Emit "const " + TransType(decl.ty, "") + " bbEnum" + decl.munged +"_Mask = " + s + ";"
  4656. End If
  4657. Local count:Int
  4658. For Local value:TEnumValueDecl = EachIn decl.values
  4659. count :+ 1
  4660. Next
  4661. ' debugscope
  4662. If count > 0 Then
  4663. _app.scopeDefs.Insert(String(count), "")
  4664. Emit "struct BBDebugScope_" + count + " " + id + "_scope ={"
  4665. Else
  4666. Emit "struct BBDebugScope " + id + "_scope ={"
  4667. End If
  4668. Emit "BBDEBUGSCOPE_USERENUM,"
  4669. Emit EnQuote(decl.ident) + ","
  4670. Emit "{"
  4671. Local ty:TEnumType = New TEnumType.Create(decl)
  4672. For Local value:TEnumValueDecl = EachIn decl.values
  4673. Emit "{"
  4674. Emit "BBDEBUGDECL_CONST,"
  4675. Emit Enquote(value.ident) + ","
  4676. Emit Enquote(TransDebugScopeType(ty) + TransDebugMetaData(decl.metadata.metadataString)) + ","
  4677. _appInstance.mapStringConsts(value.ident)
  4678. _appInstance.mapStringConsts(value.Value())
  4679. Emit ".const_value=(BBString*)&" + StringConstId(value.Value())
  4680. Emit "},"
  4681. Next
  4682. Emit "{"
  4683. Emit "BBDEBUGDECL_END,"
  4684. Emit "(char*)0,"
  4685. Emit Enquote(TransDebugScopeType(decl.ty)) + ","
  4686. Emit ".is_flags_enum=" + decl.isFlags + ","
  4687. Emit "(void (*)(void**))0"
  4688. Emit "}"
  4689. Emit "}"
  4690. Emit "};"
  4691. Local t:String
  4692. Local n:String
  4693. For Local v:TEnumValueDecl = EachIn decl.values
  4694. If t Then
  4695. t :+ ","
  4696. n :+ ","
  4697. End If
  4698. t :+ v.Value()
  4699. n :+ "(BBString*)&" + StringConstId(v.ident)
  4700. Next
  4701. Emit TransType(decl.ty, "") + " " + decl.munged + "_values[" + decl.values.length + "] = {" + t + "};"
  4702. Emit "struct BBEnum" + decl.munged + " " + decl.munged + "_BBEnum = {"
  4703. Emit EnQuote(decl.ident) + ","
  4704. Emit TransArrayType(decl.ty) + ","
  4705. Emit TransArrayType(New TEnumType.Create(decl)) + ","
  4706. Emit decl.isFlags + ","
  4707. Emit decl.values.length + ","
  4708. Emit "&" + decl.munged + "_values,"
  4709. Emit "{" + n + "}"
  4710. Emit "};"
  4711. Emit "BBEnum * " + decl.munged + "_BBEnum_impl;"
  4712. For Local fdecl:TFuncDecl = EachIn decl.FuncDecls()
  4713. MungDecl fdecl
  4714. Select fdecl.ident
  4715. Case "ToString"
  4716. Emit "BBSTRING " + fdecl.munged + Bra(TransType(decl.ty, "") + " ordinal") + " {"
  4717. Emit "return bbEnumToString_" + TransDebugScopeType(decl.ty) + Bra(decl.munged + "_BBEnum_impl, ordinal") + ";"
  4718. Emit "}"
  4719. Case "TryConvert"
  4720. Emit "BBINT " + fdecl.munged + Bra(TransType(decl.ty, "") + " ordinalValue, " + TransType(decl.ty, "") + " * ordinalResult") + " {"
  4721. Emit "return bbEnumTryConvert_" + TransDebugScopeType(decl.ty) + Bra(decl.munged + "_BBEnum_impl, ordinalValue, ordinalResult") + ";"
  4722. Emit "}"
  4723. Case "FromString"
  4724. Emit TransType(decl.ty, "") + " " + fdecl.munged + Bra("BBSTRING name") + " {"
  4725. Emit "return bbEnumFromString_" + TransDebugScopeType(decl.ty) + Bra(decl.munged + "_BBEnum_impl, name") + ";"
  4726. Emit "}"
  4727. End Select
  4728. Next
  4729. End Method
  4730. Method EmitEnumProto(decl:TEnumDecl)
  4731. Emit "extern BBEnum* " + decl.munged + "_BBEnum_impl;"
  4732. For Local fdecl:TFuncDecl = EachIn decl.FuncDecls()
  4733. MungDecl fdecl
  4734. Select fdecl.ident
  4735. Case "ToString"
  4736. Emit "BBSTRING " + fdecl.munged + Bra(TransType(decl.ty, "")) + ";"
  4737. Case "TryConvert"
  4738. Emit "BBINT " + fdecl.munged + Bra(TransType(decl.ty, "") + " ordinalValue, " + TransType(decl.ty, "") + " * ordinalResult") + ";"
  4739. Case "FromString"
  4740. Emit TransType(decl.ty, "") + " " + fdecl.munged + Bra("BBSTRING name") + ";"
  4741. Case "Ordinal"
  4742. ' nothing to generate
  4743. End Select
  4744. Next
  4745. Emit "extern const " + TransType(decl.ty, "") + " bbEnum" + decl.munged +"_Mask;"
  4746. End Method
  4747. Method TransObjectSize:String(classDecl:TClassDecl)
  4748. Local t:String
  4749. Local firstDecl:TFieldDecl
  4750. Local lastDecl:TFieldDecl
  4751. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  4752. If Not firstDecl Then
  4753. firstDecl = decl
  4754. End If
  4755. lastDecl = decl
  4756. Next
  4757. If firstDecl Then
  4758. If firstDecl <> lastDecl Then
  4759. t = "offsetof" + Bra("struct " + classDecl.munged + "_obj," + lastDecl.munged) + " - offsetof" + Bra("struct " + classDecl.munged + "_obj," + firstDecl.munged) + " + sizeof" + Bra(TransType(lastDecl.ty, ""))
  4760. Else
  4761. t = "sizeof" + Bra(TransType(lastDecl.ty, ""))
  4762. End If
  4763. Else
  4764. t = "0"
  4765. End If
  4766. Return t
  4767. End Method
  4768. Method TransFirstFieldOffset:String(classDecl:TClassDecl)
  4769. Local t:String
  4770. Local fieldDecl:TFieldDecl
  4771. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  4772. fieldDecl = decl
  4773. Exit
  4774. Next
  4775. If fieldDecl Then
  4776. t = ",offsetof" + Bra("struct " + classDecl.munged + "_obj," + fieldDecl.munged)
  4777. Else
  4778. t = ",sizeof(void*)"
  4779. End If
  4780. Return t
  4781. End Method
  4782. Method MungedConstructorName:String( classDecl:TClassDecl, fdecl:TFuncDecl )
  4783. If fdecl.argDecls.Length Then
  4784. If classDecl = fdecl.scope Then
  4785. Return "_" + fdecl.munged
  4786. Else
  4787. Return "_" + classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  4788. End If
  4789. Else
  4790. Return "_" + classDecl.munged + "_New"
  4791. End If
  4792. End Method
  4793. Method EmitClassDeclNew( classDecl:TClassDecl, fdecl:TFuncDecl )
  4794. Local id:String = MungedConstructorName(classDecl, fdecl)
  4795. Local classid$=classDecl.munged
  4796. Local superid$
  4797. If classDecl.superClass Then
  4798. superid = classDecl.superClass.actual.munged
  4799. End If
  4800. 'Find decl we override
  4801. Local odecl:TFuncDecl=fdecl
  4802. If odecl.overrides And odecl.generated Then
  4803. fdecl = odecl.overrides
  4804. Else
  4805. While odecl.overrides
  4806. odecl=odecl.overrides
  4807. Wend
  4808. End If
  4809. Local args:String = TransObject(classdecl, True) + " o"
  4810. For Local i:Int=0 Until fdecl.argDecls.Length
  4811. Local arg:TArgDecl=fdecl.argDecls[i]
  4812. Local oarg:TArgDecl=odecl.argDecls[i]
  4813. MungDecl arg, True
  4814. If args args:+","
  4815. If Not TFunctionPtrType(oarg.ty) Then
  4816. If Not odecl.castTo Then
  4817. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  4818. Else
  4819. args:+ oarg.castTo + " " + arg.munged
  4820. End If
  4821. Else
  4822. If Not odecl.castTo Then
  4823. args:+TransType( oarg.ty, arg.munged )
  4824. Else
  4825. args:+ oarg.castTo
  4826. End If
  4827. End If
  4828. If arg.ty.EqualsType( oarg.ty ) Continue
  4829. Next
  4830. Emit "void " + id + Bra(args) + " {"
  4831. Local newDecl:TNewDecl = TNewDecl(fdecl)
  4832. If Not classDecl.IsStruct() Then
  4833. ' calling constructor?
  4834. If newDecl And newDecl.chainedCtor Then
  4835. mungdecl newDecl.chainedCtor.ctor
  4836. Emit "_" + newDecl.chainedCtor.ctor.ClassScope().munged + "_" + newDecl.chainedCtor.ctor.ident + MangleMethod(newDecl.chainedCtor.ctor) + TransArgs(newDecl.chainedCtor.args, newDecl.chainedCtor.ctor, "o") + ";"
  4837. Else
  4838. If classDecl.superClass.ident = "Object" Then
  4839. Emit "bbObjectCtor((BBOBJECT)o);"
  4840. Else
  4841. If fdecl And fdecl.scope <> classDecl And fdecl.argDecls.Length Then
  4842. Local t:String = Bra(TransObject(classDecl.superClass)) + "o"
  4843. For Local i:Int=0 Until fdecl.argDecls.Length
  4844. Local arg:TArgDecl=fdecl.argDecls[i]
  4845. t :+ ", " + arg.munged
  4846. Next
  4847. Emit "_" + newDecl.ClassScope().munged + "_" + newDecl.ident + MangleMethod(newDecl) + Bra(t) + ";"
  4848. Else
  4849. Emit "_" + superid + "_New((" + TransObject(classDecl.superClass) + ")o);"
  4850. End If
  4851. End If
  4852. End If
  4853. Emit "o->clas = &" + classid + ";" ' TODO
  4854. End If
  4855. ' only initialise fields if we are not chaining to a local (in our class) constructor.
  4856. ' this prevents fields being re-initialised through the call-chain.
  4857. If Not newDecl.chainedCtor Or (newDecl.chainedCtor And classDecl <> newDecl.chainedCtor.ctor.scope) Then
  4858. ' field initialisation
  4859. For Local decl:TFieldDecl=EachIn classDecl.Decls()
  4860. Local doEmit:Int = True
  4861. If Not decl.IsSemanted() Then
  4862. decl.Semant()
  4863. End If
  4864. Local fld:String
  4865. ' ((int*)((char*)o + 5))[0] =
  4866. fld :+ TransFieldRef(decl, "o")
  4867. If decl.init Then
  4868. If TObjectType(decl.ty) And TObjectType(decl.ty).classdecl.IsExtern() And TObjectType(decl.ty).classdecl.IsStruct() Then
  4869. ' skip for uninitialised extern type
  4870. If Not isPointerType(decl.ty) And TConstExpr(decl.init) And Not TConstExpr(decl.init).value Then
  4871. Continue
  4872. End If
  4873. End If
  4874. ' initial value
  4875. If (TConstExpr(decl.init) And Not TConstExpr(decl.init).value) And TIntrinsicType(decl.ty) Then
  4876. fld :+ "= "
  4877. If TFloat64Type(decl.ty) Then
  4878. fld :+ "_mm_setzero_si64();"
  4879. Else If TFloat128Type(decl.ty) Then
  4880. fld :+ "_mm_setzero_ps();"
  4881. Else If TDouble128Type(decl.ty) Then
  4882. fld :+ "_mm_setzero_pd();"
  4883. Else If TInt128Type(decl.ty) Then
  4884. fld :+ "_mm_setzero_si128();"
  4885. End If
  4886. Else
  4887. If TObjectType(decl.ty) And TObjectType(decl.ty).classdecl.IsStruct() And Not isPointerType(decl.ty) And (TConstExpr(decl.init) And Not TConstExpr(decl.init).value) Then
  4888. fld = "memset(&" + fld + ", 0, sizeof" + Bra(TransType(decl.ty, "")) + ");"
  4889. Else If TInvokeExpr(decl.init) And Not TInvokeExpr(decl.init).invokedWithBraces Then
  4890. fld :+ "= " + TInvokeExpr(decl.init).decl.munged + ";"
  4891. Else If TObjectType(decl.ty) Then
  4892. fld :+ "= "
  4893. If Not TObjectType(decl.ty).classDecl.IsStruct() Then
  4894. fld :+ Bra(TransObject(TObjectType(decl.ty).classDecl))
  4895. End If
  4896. fld :+ decl.init.Trans() + ";"
  4897. Else If TArrayType(decl.ty) And TArrayType(decl.ty).isStatic Then
  4898. Local idx:String = "i" + fdecl.NextIdx()
  4899. fld = "int " + idx + ";for(" + idx + "=0;" + idx + "<" + TArrayType(decl.ty).length + ";" + idx + "++) " + TransFieldRef(decl, "o") + "[" + idx + "]=" + TransValue(TArrayType(decl.ty).elemType,Null,False) + ";"
  4900. Else
  4901. fld :+ "= " + decl.init.Trans() + ";"
  4902. End If
  4903. End If
  4904. Else
  4905. If TNumericType(decl.ty) Or IsPointerType(decl.ty, 0, TType.T_POINTER) Then
  4906. doEmit = False
  4907. Else If TObjectType(decl.ty) Then
  4908. If TObjectType(decl.ty).classDecl.IsStruct() Then
  4909. fld :+ "= " + TObjectType(decl.ty).classDecl.munged + "_New_ObjectNew();"
  4910. Else
  4911. fld :+ "= &bbNullObject;"
  4912. End If
  4913. Else If TFunctionPtrType(decl.ty) Then
  4914. fld :+ "= &brl_blitz_NullFunctionError;"
  4915. Else If TStringType(decl.ty) Then
  4916. fld :+ "= &bbEmptyString;"
  4917. Else If TArrayType(decl.ty) Then
  4918. If TArrayType(decl.ty).isStatic Then
  4919. Local idx:String = "i" + fdecl.NextIdx()
  4920. fld = "int " + idx + ";for(" + idx + "=0;" + idx + "<" + TArrayType(decl.ty).length + ";" + idx + "++) " + TransFieldRef(decl, "o") + "[" + idx + "]=" + TransValue(TArrayType(decl.ty).elemType,Null,False) + ";"
  4921. Else
  4922. fld :+ "= &bbEmptyArray;"
  4923. End If
  4924. Else If TEnumType(decl.ty) Then
  4925. fld :+ "= " + TEnumType(decl.ty).decl.values[0].Value() + ";"
  4926. End If
  4927. End If
  4928. If doEmit Then
  4929. Emit fld
  4930. End If
  4931. Next
  4932. End If
  4933. 'Local decl:TFuncDecl = classDecl.FindFuncDecl("new",,,,,,SCOPE_CLASS_LOCAL)
  4934. If fdecl And (fdecl.scope = classDecl) Then ' only our own New method, not any from superclasses
  4935. fdecl.Semant
  4936. If fdecl.munged <> "bbObjectCtor" Then
  4937. EmitLocalDeclarations(fdecl)
  4938. EmitBlock fdecl
  4939. End If
  4940. End If
  4941. '
  4942. Emit "}"
  4943. EmitReflectionWrapper classDecl, fdecl
  4944. End Method
  4945. Method EmitClassDeclNewList( classDecl:TClassDecl )
  4946. Local classid$=classDecl.munged
  4947. Local newDecls:TFuncDeclList = TFuncDeclList(classdecl.FindDeclList("new", True,,,True))
  4948. For Local fdecl:TFuncDecl = EachIn newDecls
  4949. MungDecl fdecl
  4950. If fdecl.scope <> classDecl Then
  4951. fdecl.Clear()
  4952. EmitClassDeclNew(classDecl, fdecl)
  4953. Else
  4954. EmitClassDeclNew(classDecl, fdecl)
  4955. End If
  4956. ' generate "objectNew" function if required
  4957. If (fdecl.argDecls And fdecl.argDecls.length) Or classDecl.IsStruct() Then
  4958. EmitClassDeclNewInit(classDecl, fdecl)
  4959. End If
  4960. Next
  4961. End Method
  4962. Method EmitClassDeclNewListProto( classDecl:TClassDecl )
  4963. Local classid$=classDecl.munged
  4964. 'Local superid$=classDecl.superClass.actual.munged
  4965. Local newDecls:TFuncDeclList = TFuncDeclList(classdecl.FindDeclList("new", True,,,True))
  4966. For Local fdecl:TFuncDecl = EachIn newDecls
  4967. EmitClassDeclNewProto(classDecl, fdecl)
  4968. ' generate "objectNew" function if required
  4969. If (fdecl.argDecls And fdecl.argDecls.length) Or classDecl.IsStruct() Then
  4970. EmitClassDeclObjectNewProto(classDecl, fdecl)
  4971. End If
  4972. Next
  4973. End Method
  4974. Method EmitClassDeclNewInit(classDecl:TClassDecl, fdecl:TFuncDecl)
  4975. Local funcMunged:String
  4976. If classDecl = fdecl.scope Then
  4977. funcMunged = fdecl.munged
  4978. Else
  4979. funcMunged = classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  4980. End If
  4981. Local t:String = TransObject(classdecl) + " "
  4982. If Not classDecl.IsStruct() Then
  4983. t :+ "_"
  4984. End If
  4985. t :+ funcMunged + "_ObjectNew"
  4986. 'Find decl we override
  4987. Local odecl:TFuncDecl=fdecl
  4988. While odecl.overrides
  4989. odecl=odecl.overrides
  4990. Wend
  4991. Local args:String
  4992. If Not classDecl.IsStruct() Then
  4993. args = "BBClass * clas"
  4994. End If
  4995. For Local i:Int=0 Until fdecl.argDecls.Length
  4996. Local arg:TArgDecl=fdecl.argDecls[i]
  4997. Local oarg:TArgDecl=odecl.argDecls[i]
  4998. MungDecl arg, True
  4999. If args args:+","
  5000. If Not TFunctionPtrType(oarg.ty) Then
  5001. If Not odecl.castTo Then
  5002. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  5003. Else
  5004. args:+ oarg.castTo + " " + arg.munged
  5005. End If
  5006. Else
  5007. If Not odecl.castTo Then
  5008. args:+TransType( oarg.ty, arg.munged )
  5009. Else
  5010. args:+ oarg.castTo
  5011. End If
  5012. End If
  5013. If arg.ty.EqualsType( oarg.ty ) Continue
  5014. Next
  5015. Emit t + Bra(args) + " {"
  5016. t = TransObject(classdecl) + " o"
  5017. If classDecl.IsStruct() Then
  5018. t :+ " = {"
  5019. Local fields:Int
  5020. For Local f:TFieldDecl = EachIn classDecl.Decls()
  5021. If fields Then
  5022. t :+ ","
  5023. End If
  5024. fields = True
  5025. t :+ TransValue(f.ty, "", True)
  5026. Next
  5027. Emit t + "};"
  5028. Else
  5029. t :+ " = " + Bra(TransObject(classdecl))
  5030. If ClassHasObjectField(classDecl) Then
  5031. t :+ "bbObjectNewNC"
  5032. Else
  5033. t :+ "bbObjectAtomicNewNC"
  5034. End If
  5035. Emit t + "(clas);"
  5036. End If
  5037. t = "_" + funcMunged
  5038. If classDecl.IsStruct() Then
  5039. t :+ "(&o"
  5040. Else
  5041. t :+ "(o"
  5042. End If
  5043. For Local i:Int=0 Until fdecl.argDecls.Length
  5044. Local arg:TArgDecl=fdecl.argDecls[i]
  5045. t :+ ", " + arg.munged
  5046. Next
  5047. Emit t + ");"
  5048. Emit "return o;"
  5049. Emit "}"
  5050. End Method
  5051. Method EmitClassDeclNewProto( classDecl:TClassDecl, fdecl:TFuncDecl )
  5052. Local classid$=classDecl.munged
  5053. Local superid$
  5054. If classDecl.superClass Then
  5055. superid = classDecl.superClass.actual.munged
  5056. End If
  5057. Local t:String = "void _"
  5058. If fdecl.argDecls.Length Then
  5059. If classDecl = fdecl.scope Then
  5060. If Not fdecl.munged Then
  5061. MungDecl fdecl
  5062. End If
  5063. t :+ fdecl.munged
  5064. Else
  5065. t :+ classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  5066. End If
  5067. Else
  5068. t :+ classid + "_New"
  5069. End If
  5070. 'Find decl we override
  5071. Local odecl:TFuncDecl=fdecl
  5072. While odecl.overrides
  5073. odecl=odecl.overrides
  5074. Wend
  5075. Local args:String = TransObject(classdecl, True) + " o"
  5076. For Local i:Int=0 Until fdecl.argDecls.Length
  5077. Local arg:TArgDecl=fdecl.argDecls[i]
  5078. Local oarg:TArgDecl=odecl.argDecls[i]
  5079. MungDecl arg, True
  5080. If args args:+","
  5081. If Not TFunctionPtrType(oarg.ty) Then
  5082. If Not odecl.castTo Then
  5083. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  5084. Else
  5085. args:+ oarg.castTo + " " + arg.munged
  5086. End If
  5087. Else
  5088. If Not odecl.castTo Then
  5089. args:+TransType( oarg.ty, arg.munged )
  5090. Else
  5091. args:+ oarg.castTo
  5092. End If
  5093. End If
  5094. If arg.ty.EqualsType( oarg.ty ) Continue
  5095. Next
  5096. Emit t + Bra(args) + ";"
  5097. End Method
  5098. Method EmitClassDeclObjectNewProto(classDecl:TClassDecl, fdecl:TFuncDecl)
  5099. Local t:String = TransObject(classdecl) + " "
  5100. If Not classDecl.IsStruct() Then
  5101. t :+ "_"
  5102. End If
  5103. If classDecl = fdecl.scope Then
  5104. t :+ fdecl.munged
  5105. Else
  5106. t :+ classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  5107. End If
  5108. t:+ "_ObjectNew"
  5109. 'Find decl we override
  5110. Local odecl:TFuncDecl=fdecl
  5111. While odecl.overrides
  5112. odecl=odecl.overrides
  5113. Wend
  5114. Local args:String
  5115. If Not classDecl.IsStruct() Then
  5116. args = "BBClass * clas"
  5117. End If
  5118. For Local i:Int=0 Until fdecl.argDecls.Length
  5119. Local arg:TArgDecl=fdecl.argDecls[i]
  5120. Local oarg:TArgDecl=odecl.argDecls[i]
  5121. MungDecl arg, True
  5122. If args args:+","
  5123. If Not TFunctionPtrType(oarg.ty) Then
  5124. If Not odecl.castTo Then
  5125. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  5126. Else
  5127. args:+ oarg.castTo + " " + arg.munged
  5128. End If
  5129. Else
  5130. If Not odecl.castTo Then
  5131. args:+TransType( oarg.ty, arg.munged )
  5132. Else
  5133. args:+ oarg.castTo
  5134. End If
  5135. End If
  5136. If arg.ty.EqualsType( oarg.ty ) Continue
  5137. Next
  5138. Emit t + Bra(args) + ";"
  5139. End Method
  5140. Method EmitClassDeclDelete( classDecl:TClassDecl )
  5141. Local classid$=classDecl.munged
  5142. Local superid$=classDecl.superClass.actual.munged
  5143. ' New
  5144. ' If opt_issuperstrict Then
  5145. Emit "void _" + classid + "_Delete" + Bra(TransObject(classdecl) + " o") + " {"
  5146. ' Else
  5147. ' Emit "int _" + classid + "_Delete" + Bra(TransObject(classdecl) + " o") + " {"
  5148. ' End If
  5149. Local decl:TFuncDecl = classDecl.FindFuncDecl("delete",,,,,,SCOPE_CLASS_HEIRARCHY)
  5150. If decl And decl.ClassScope() = classDecl Then
  5151. decl.Semant
  5152. EmitLocalDeclarations(decl)
  5153. EmitBlock decl
  5154. End If
  5155. ' field cleanup
  5156. For Local decl:TFieldDecl=EachIn classDecl.Decls()
  5157. ' String
  5158. If TStringType(decl.declTy) Then
  5159. Emit "BBRELEASE(" + TransFieldRef(decl, "o") + ")"
  5160. End If
  5161. ' object
  5162. ' TODO
  5163. Next
  5164. ' finally, call super delete
  5165. EmitClassDeclDeleteDtor(classDecl)
  5166. '
  5167. Emit "}"
  5168. End Method
  5169. Method EmitClassDeclDeleteDtor( classDecl:TClassDecl )
  5170. Local superid$=classDecl.superClass.actual.munged
  5171. If classDecl.superClass.ident = "Object" Or Not classHierarchyGetFunction(classDecl.superClass, "Delete") Then
  5172. Emit "bbObjectDtor((BBOBJECT)o);"
  5173. Else
  5174. Emit "_" + superid + "_Delete((" + TransObject(TScopeDecl(classDecl.superClass.actual)) + ")o);"
  5175. End If
  5176. End Method
  5177. Method TransFieldRef:String(decl:TFieldDecl, variable:String, exprType:TType = Null)
  5178. Local s:String = variable
  5179. Local ind:String = "->"
  5180. If decl.scope And TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  5181. Local exprIsStruct:Int = Not exprType Or (TObjectType(exprType) And TObjectType(exprType).classDecl.attrs & CLASS_STRUCT)
  5182. If (exprIsStruct Or (exprType And Not IsPointerType(exprType))) And variable <> "o" Then
  5183. If Not exprIsStruct Or (exprType And Not IsPointerType(exprType)) Then
  5184. ind = "."
  5185. End If
  5186. End If
  5187. End If
  5188. If variable.StartsWith("*") Then
  5189. variable = Bra(variable)
  5190. End If
  5191. ' Null test
  5192. If opt_debug
  5193. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  5194. '
  5195. Else
  5196. variable = TransDebugNullObjectError(variable, TClassDecl(decl.scope))
  5197. End If
  5198. End If
  5199. ' array.length
  5200. If decl.scope And decl.scope.ident = "___Array" Then
  5201. If decl.ident = "length" Then
  5202. If TArrayType(exprType) And TArrayType(exprType).isStatic Then
  5203. Return TArrayType(exprType).length
  5204. Else
  5205. Return Bra(variable + "->scales[0]")
  5206. End If
  5207. End If
  5208. If decl.ident = "numberOfDimensions" Then
  5209. Return Bra(variable + "->dims")
  5210. End If
  5211. If decl.ident = "sizeMinusHeader" Then
  5212. Return Bra(variable + "->size")
  5213. End If
  5214. If decl.ident = "elementTypeEncoding" Then
  5215. Return Bra(variable + "->type")
  5216. End If
  5217. End If
  5218. ' string methods
  5219. If decl.scope And decl.scope.ident = "String" Then
  5220. If decl.ident = "length" Then
  5221. 'If exprType._flags & TType.T_VAR Then
  5222. ' Return Bra("(*" + variable + ")->length")
  5223. 'Else
  5224. If variable.StartsWith("&_s") Then
  5225. Return Bra(variable[1..] + ".length")
  5226. Else
  5227. Return Bra(variable + "->length")
  5228. End If
  5229. 'End If
  5230. End If
  5231. End If
  5232. 'If TObjectType(exprType) And (exprType._flags & TType.T_VAR) Then
  5233. ' ' get the object from the pointer
  5234. ' variable = Bra("*" + variable)
  5235. 'End If
  5236. If IsNumericType(decl.ty) Then
  5237. s = variable + ind + decl.munged + " "
  5238. Else If TStringType(decl.ty) Then
  5239. s = variable + ind + decl.munged + " "
  5240. Else If TObjectType(decl.ty) Then
  5241. s = variable + ind + decl.munged + " "
  5242. Else If IsPointerType(decl.ty, 0, TType.T_POINTER) Then
  5243. s = variable + ind + decl.munged + " "
  5244. Else If TFunctionPtrType(decl.ty) Then
  5245. s = variable + ind + decl.munged + " "
  5246. Else If TArrayType(decl.ty) Then
  5247. s = variable + ind + decl.munged + " "
  5248. Else If TEnumType(decl.ty) Then
  5249. s = variable + ind + decl.munged + " "
  5250. End If
  5251. Return s
  5252. End Method
  5253. ' " _" + classDecl.actual.munged + "_" + decl.ident.ToLower(
  5254. Method TransIfcArgs:String(funcDecl:TFuncDecl)
  5255. Local args:String
  5256. If Not funcDecl.IsSemanted() Then
  5257. funcDecl.Semant()
  5258. End If
  5259. For Local i:Int=0 Until funcDecl.argDecls.Length
  5260. Local arg:TArgDecl = funcDecl.argDecls[i]
  5261. If args args:+","
  5262. args:+ arg.ident + TransIfcType( arg.ty )
  5263. If arg.init Then
  5264. If TInvokeExpr(arg.init) Then
  5265. args:+ "=" + Enquote(TInvokeExpr(arg.init).decl.munged)
  5266. Else
  5267. args:+ "=" + TransIfcConstExpr(arg.init)
  5268. End If
  5269. End If
  5270. Next
  5271. Return Bra(args)
  5272. End Method
  5273. Method EmitIfcClassFuncDecl(funcDecl:TFuncDecl)
  5274. funcDecl.Semant
  5275. Local func:String
  5276. ' method / function
  5277. If funcDecl.IsMethod() Or funcDecl.IsCTor() Then
  5278. func :+ "-"
  5279. Else
  5280. func :+ "+"
  5281. End If
  5282. If funcDecl.attrs & FUNC_OPERATOR Then
  5283. func :+ BmxEnquote(funcDecl.ident)
  5284. Else
  5285. func :+ funcDecl.ident
  5286. End If
  5287. If Not TNewDecl(funcDecl) Then
  5288. func :+ TransIfcType(funcDecl.retType, funcDecl.ModuleScope().IsSuperStrict())
  5289. End If
  5290. ' function args
  5291. func :+ TransIfcArgs(funcDecl)
  5292. If funcDecl.attrs & DECL_FINAL Then
  5293. func :+ "F"
  5294. Else If funcDecl.attrs & DECL_ABSTRACT Then
  5295. func :+ "A"
  5296. End If
  5297. If funcDecl.attrs & FUNC_OPERATOR Then
  5298. func :+ "O"
  5299. End If
  5300. If funcDecl.attrs & DECL_PRIVATE Then
  5301. func :+ "P"
  5302. Else If funcDecl.attrs & DECL_PROTECTED Then
  5303. func :+ "R"
  5304. End If
  5305. If funcDecl.attrs & DECL_API_STDCALL Then
  5306. func :+ "W"
  5307. End If
  5308. If funcDecl.attrs & DECL_EXPORT Then
  5309. func :+ "E"
  5310. End If
  5311. func :+ "="
  5312. func :+ Enquote(funcDecl.munged)
  5313. Emit func
  5314. End Method
  5315. Method EmitIfcFuncDecl(funcDecl:TFuncDecl)
  5316. Local func:String
  5317. func :+ funcDecl.ident
  5318. ' ensure the function has been semanted
  5319. funcDecl.Semant()
  5320. func :+ TransIfcType(funcDecl.retType, funcDecl.ModuleScope().IsSuperStrict())
  5321. ' function args
  5322. func :+ TransIfcArgs(funcDecl)
  5323. If funcDecl.attrs & DECL_API_STDCALL Then
  5324. func :+ "W"
  5325. End If
  5326. func :+ "="
  5327. func :+ Enquote(funcDecl.munged)
  5328. If funcDecl.castTo Then
  5329. func :+ ":" + funcDecl.castTo
  5330. func :+ " " + funcDecl.munged + "("
  5331. For Local i:Int = 0 Until funcDecl.argDecls.length
  5332. If i Then
  5333. func :+ ", "
  5334. End If
  5335. func :+ funcDecl.argDecls[i].castTo
  5336. Next
  5337. func :+ ")"
  5338. End If
  5339. Emit func
  5340. End Method
  5341. Method TransIfcConstExpr:String(expr:TExpr)
  5342. If Not expr.exprType Then
  5343. expr.Semant()
  5344. End If
  5345. If TStringType(expr.exprType) Then
  5346. Return "$" + EscapeChars(BmxEnquote(expr.Eval()))
  5347. EndIf
  5348. If TArrayType(expr.exprType) Then
  5349. Return Enquote("bbEmptyArray")
  5350. End If
  5351. If TFunctionPtrType(expr.exprType) Then
  5352. If TCastExpr(expr) Then
  5353. If TInvokeExpr(TCastExpr(expr).expr) Then
  5354. Return Enquote(TInvokeExpr(TCastExpr(expr).expr).decl.munged)
  5355. End If
  5356. If TNullExpr(TCastExpr(expr).expr) Then
  5357. Return Enquote("brl_blitz_NullFunctionError")
  5358. End If
  5359. End If
  5360. InternalErr "TCTranslator.TransIfcConstExpr"
  5361. End If
  5362. If TObjectType(expr.exprType) Then
  5363. If TCastExpr(expr) Then
  5364. If TNullExpr(TCastExpr(expr).expr) Then
  5365. Return Enquote("bbNullObject")
  5366. End If
  5367. End If
  5368. End If
  5369. If IsPointerType(expr.exprType, 0, TType.T_POINTER) Then
  5370. If TCastExpr(expr) Then
  5371. If TNullExpr(TCastExpr(expr).expr) Then
  5372. Return "0"
  5373. End If
  5374. If TConstExpr(TCastExpr(expr).expr) Then
  5375. Return TConstExpr(TCastExpr(expr).expr).value
  5376. End If
  5377. End If
  5378. End If
  5379. If IsNumericType(expr.exprType) Then
  5380. Local s:String = expr.Eval()
  5381. If Not s Then
  5382. Return "0"
  5383. Else
  5384. If TDecimalType(expr.exprType) Then
  5385. If s.StartsWith("1.#INF0000") Or s = "1e1000" Then
  5386. s = "inf"
  5387. Else If s.StartsWith("-1.#INF0000") Then
  5388. s = "-inf"
  5389. Else If s.StartsWith("-1.#IND0000") Then
  5390. s = "nan"
  5391. End If
  5392. Return s + TransIfcType(expr.exprType)
  5393. Else
  5394. Return s
  5395. End If
  5396. End If
  5397. EndIf
  5398. If TEnumType(expr.exprType) Then
  5399. If TCastExpr(expr) And TNullExpr(TCastExpr(expr).expr) Then
  5400. Return TransValue(expr.exprType, Null)
  5401. Else
  5402. Return Expr.Eval()
  5403. End If
  5404. End If
  5405. 'If TObjectType(expr.exprType) And TNullDecl(TObjectType(expr.exprType).classDecl) Then
  5406. ' Return Enquote("bbNullObject")
  5407. 'End If
  5408. End Method
  5409. Method EmitIfcConstDecl(constDecl:TConstDecl)
  5410. Local c:String
  5411. c = constDecl.ident + TransIfcType(constDecl.ty)
  5412. If TExpr(constDecl.init) Then
  5413. c:+ "=" + TransIfcConstExpr(TExpr(constDecl.init))
  5414. End If
  5415. Emit c
  5416. End Method
  5417. Method EmitIfcFieldDecl(fieldDecl:TFieldDecl)
  5418. Local f:TStringBuffer = New TStringBuffer
  5419. If fieldDecl.IsReadOnly() Then
  5420. f.Append( "@" )
  5421. End If
  5422. If fieldDecl.IsStatic() Then
  5423. f.Append( "~~" )
  5424. End If
  5425. If Not f.Length() Then
  5426. f.Append( "." )
  5427. End If
  5428. f.Append( fieldDecl.ident ).Append( TransIfcType(fieldDecl.ty, fieldDecl.ModuleScope().IsSuperStrict()) )
  5429. f.Append( "&" )
  5430. If fieldDecl.IsPrivate() Then
  5431. f.Append( "`" )
  5432. Else If fieldDecl.IsProtected() Then
  5433. f.Append( "``" )
  5434. End If
  5435. Emit f.ToString()
  5436. End Method
  5437. Method EmitIfcClassDecl(classDecl:TClassDecl)
  5438. Local head:String = classDecl.ident + "^"
  5439. If classDecl.superClass Then
  5440. Local superDecl:TClassDecl = classDecl.superClass
  5441. head :+ superDecl.ident
  5442. If superDecl.instArgs Then
  5443. head :+ "<"
  5444. Local s:String
  5445. For Local ty:TType = EachIn superDecl.instArgs
  5446. If s Then
  5447. s :+ ","
  5448. End If
  5449. s :+ ty.ToString()
  5450. Next
  5451. head :+ s
  5452. head :+ ">"
  5453. End If
  5454. Else
  5455. head :+ "Null"
  5456. End If
  5457. If classDecl.implments Then
  5458. head :+ "@"
  5459. For Local i:Int = 0 Until classDecl.implments.length
  5460. If i Then
  5461. head :+ ","
  5462. End If
  5463. head :+ classDecl.implments[i].ident
  5464. ' interface inst args
  5465. Local idecl:TClassDecl = classDecl.implments[i]
  5466. If idecl.instArgs Then
  5467. head :+ "<"
  5468. Local s:String
  5469. For Local ty:TType = EachIn idecl.instArgs
  5470. If s Then
  5471. s :+ ","
  5472. End If
  5473. s :+ ty.ToString()
  5474. Next
  5475. head :+ s
  5476. head :+ ">"
  5477. End If
  5478. Next
  5479. End If
  5480. Emit head + "{", False
  5481. 'PushMungScope
  5482. BeginLocalScope
  5483. If Not classDecl.templateSource Then
  5484. ' const
  5485. For Local cDecl:TConstDecl = EachIn classDecl.Decls()
  5486. cDecl.Semant()
  5487. EmitIfcConstDecl(cDecl)
  5488. Next
  5489. ' global
  5490. For Local gDecl:TGlobalDecl = EachIn classDecl.Decls()
  5491. gDecl.Semant()
  5492. EmitIfcGlobalDecl(gDecl)
  5493. Next
  5494. ' field
  5495. For Local fDecl:TFieldDecl = EachIn classDecl.Decls()
  5496. fDecl.Semant()
  5497. EmitIfcFieldDecl(fDecl)
  5498. Next
  5499. End If
  5500. ' functions
  5501. If Not classDecl.IsExtern() Then
  5502. If Not classDecl.templateSource Then
  5503. If Not (classDecl.attrs & CLASS_INTERFACE) And Not classDecl.IsStruct() And Not classHierarchyGetFunction(classDecl, "New") Then
  5504. Emit "-New()=" + Enquote("_" + classDecl.munged + "_New")
  5505. End If
  5506. If classHierarchyGetFunction(classDecl, "Delete") Then
  5507. Emit "-Delete()=" + Enquote("_" + classDecl.munged + "_Delete")
  5508. End If
  5509. For Local decl:TDecl=EachIn classDecl.Decls()
  5510. Local fdecl:TFuncDecl=TFuncDecl( decl )
  5511. If fdecl
  5512. If Not equalsIfcBuiltInFunc(classDecl, fdecl) Then
  5513. EmitIfcClassFuncDecl fdecl
  5514. End If
  5515. Continue
  5516. EndIf
  5517. Next
  5518. End If
  5519. Local flags:String
  5520. If classDecl.IsAbstract() Then
  5521. flags :+ "A"
  5522. End If
  5523. If classDecl.attrs & DECL_FINAL Then
  5524. flags :+ "F"
  5525. End If
  5526. If classDecl.attrs & CLASS_INTERFACE Then
  5527. flags :+ "I"
  5528. Else If classDecl.IsStruct() Then
  5529. flags :+ "S"
  5530. End If
  5531. If classDecl.IsPrivate() Then
  5532. flags :+ "P"
  5533. End If
  5534. If classDecl.templateSource Then
  5535. flags :+ "G"
  5536. End If
  5537. Local t:String = "}" + flags + "="
  5538. If classDecl.templateSource Then
  5539. Local s:String
  5540. If classDecl.instArgs Then
  5541. t :+ Enquote(classDecl.scope.munged + "|" + classDecl.munged)
  5542. t :+ ",<"
  5543. For Local ty:TType = EachIn classDecl.instArgs
  5544. If s Then
  5545. s :+ ","
  5546. End If
  5547. s :+ ty.ToString()
  5548. Next
  5549. Else
  5550. t :+ Enquote(classDecl.scope.munged)
  5551. t :+ ",<"
  5552. s = "?"
  5553. End If
  5554. t :+ s + ">" + classDecl.templateSource.ToString()
  5555. Else
  5556. t :+ Enquote(classDecl.munged)
  5557. End If
  5558. Emit t, False
  5559. Else
  5560. For Local decl:TDecl=EachIn classDecl.Decls()
  5561. Local fdecl:TFuncDecl=TFuncDecl( decl )
  5562. If fdecl
  5563. EmitIfcClassFuncDecl fdecl
  5564. Continue
  5565. EndIf
  5566. Next
  5567. Local flags:String = "E"
  5568. If classDecl.IsInterface() Then
  5569. flags :+ "I"
  5570. Else If classDecl.IsStruct() Then
  5571. flags :+ "S"
  5572. End If
  5573. If classDecl.attrs & DECL_API_STDCALL Then
  5574. flags :+ "W"
  5575. End If
  5576. Emit "}" + flags + "=0", False
  5577. End If
  5578. 'PopMungScope
  5579. EndLocalScope
  5580. End Method
  5581. Method EmitIfcGlobalDecl(globalDecl:TGlobalDecl)
  5582. globalDecl.Semant
  5583. Local g:String = globalDecl.ident
  5584. g:+ TransIfcType(globalDecl.ty, globalDecl.ModuleScope().IsSuperStrict())
  5585. g:+ "&"
  5586. If globalDecl.IsPrivate() Then
  5587. g :+ "`"
  5588. Else If globalDecl.IsProtected() Then
  5589. g :+ "``"
  5590. End If
  5591. g :+ "="
  5592. g :+ "mem:p("
  5593. If TFunctionPtrType(globalDecl.ty) Then
  5594. g :+ Enquote(TFunctionPtrType(globalDecl.ty).func.munged)
  5595. Else
  5596. g :+ Enquote(globalDecl.munged)
  5597. End If
  5598. g :+ ")"
  5599. Emit g
  5600. End Method
  5601. Method EmitIfcEnumDecl(enumdecl:TEnumDecl)
  5602. enumDecl.Semant
  5603. Local e:String = enumDecl.ident + "\" + TransIfcType(enumDecl.ty)
  5604. Emit e + "{", False
  5605. For Local val:TEnumValueDecl = EachIn enumDecl.values
  5606. Emit val.ident + "=" + val.Value()
  5607. Next
  5608. Local flags:String
  5609. If enumDecl.isFlags Then
  5610. flags = "F"
  5611. End If
  5612. Emit "}" + flags + "=" + Enquote(enumDecl.munged), False
  5613. End Method
  5614. Method EmitModuleInclude(moduleDecl:TModuleDecl, included:TMap = Null)
  5615. If moduleDecl.filepath Then
  5616. ' a module import
  5617. If FileType(moduleDecl.filepath) = FILETYPE_DIR Or (opt_ismain And moduleDecl.ident = opt_modulename) Then
  5618. Local inc:String = ModuleHeaderFromIdent(moduleDecl.ident, True)
  5619. If Not included Or (included And Not included.Contains(inc)) Then
  5620. Emit "#include <" + inc + ">"
  5621. If included Then
  5622. included.Insert(inc, inc)
  5623. End If
  5624. End If
  5625. Else
  5626. ' a file import...
  5627. Local inc:String = FileHeaderFromFile(moduleDecl, False)
  5628. If Not included Or (included And Not included.Contains(inc)) Then
  5629. Emit "#include ~q" + inc + "~q"
  5630. If included Then
  5631. included.Insert(inc, inc)
  5632. End If
  5633. End If
  5634. End If
  5635. ' DebugLog moduleDecl.filepath
  5636. End If
  5637. End Method
  5638. Method EmitModuleInit(moduleDecl:TModuleDecl)
  5639. If moduleDecl.filepath Then
  5640. ' a module import
  5641. If FileType(moduleDecl.filepath) = FILETYPE_DIR Then
  5642. Emit MungModuleName(moduleDecl) + "();"
  5643. Else
  5644. ' maybe a file import...
  5645. Emit MungImportFromFile(moduleDecl) + "();"
  5646. End If
  5647. End If
  5648. End Method
  5649. Method EmitModuleRegisterInit(moduleDecl:TModuleDecl)
  5650. If moduleDecl.filepath Then
  5651. ' a module import
  5652. If FileType(moduleDecl.filepath) = FILETYPE_DIR Then
  5653. Emit MungModuleName(moduleDecl) + "_register();"
  5654. Else
  5655. ' maybe a file import...
  5656. Emit MungImportFromFile(moduleDecl) + "_register();"
  5657. End If
  5658. End If
  5659. End Method
  5660. Method EmitIncBinFile(ib:TIncbin)
  5661. If FileType(ib.path) = FILETYPE_FILE Then
  5662. If Not opt_legacy_incbin Then
  5663. Local ident:String = _appInstance.munged + "_" + ib.id
  5664. Emit "INCBIN(" + ident + ", ~q" + ib.path + "~q);"
  5665. Else
  5666. Local ident:String = _appInstance.munged + "_ib_" + ib.id
  5667. Local buf:Byte[] = LoadByteArray(ib.path)
  5668. ib.length = buf.length
  5669. Emit "unsigned char " + ident + "[] = {"
  5670. Local sb:TStringBuffer = New TStringBuffer
  5671. Local hx:Short[2]
  5672. Local LINES:Int
  5673. Local count:Int
  5674. For Local i:Int = 0 Until buf.length
  5675. Local val:Int = buf[i]
  5676. For Local k:Int=1 To 0 Step -1
  5677. Local n:Int=(val&15)+48
  5678. If n>57 n:+39
  5679. hx[k]=n
  5680. val:Shr 4
  5681. Next
  5682. sb.Append("0x").AppendShorts( hx,2 )
  5683. sb.Append(",")
  5684. count :+ 5
  5685. If count > 80 Then
  5686. sb.Append("~n")
  5687. count = 0
  5688. LINES :+ 1
  5689. End If
  5690. If LINES > 100 Then
  5691. Emit sb.ToString()
  5692. sb.SetLength(0)
  5693. LINES = 0
  5694. End If
  5695. Next
  5696. Emit sb.ToString()
  5697. Emit "};"
  5698. End If
  5699. End If
  5700. End Method
  5701. Method TransHeader(app:TAppDecl)
  5702. SetOutput("head")
  5703. _app = app
  5704. prefix = app.GetPathPrefix()
  5705. ' TODO
  5706. If Not opt_apptype Then
  5707. app.mainFunc.munged="bb_localmain"
  5708. Else
  5709. app.mainFunc.munged="bb_main"
  5710. End If
  5711. ' track what's been included so far - avoid duplicates
  5712. Local included:TMap = New TMap
  5713. For Local decl:TModuleDecl=EachIn app.imported.Values()
  5714. For Local mdecl:TDecl=EachIn decl.imported.Values()
  5715. MungDecl mdecl
  5716. 'skip mdecls we are not interested in
  5717. If Not TModuleDecl(mdecl) Then Continue
  5718. If app.mainModule = mdecl Then Continue
  5719. If mdecl.ident = "brl.classes" Then Continue
  5720. If mdecl.ident = "brl.blitzkeywords" Then Continue
  5721. EmitModuleInclude(TModuleDecl(mdecl), included)
  5722. Next
  5723. Next
  5724. For Local header:String=EachIn app.headers
  5725. Emit "#include ~q../" + header + "~q"
  5726. Next
  5727. Emit "int " + app.munged + "();"
  5728. Emit "void " + app.munged + "_register();"
  5729. For Local decl:TDecl=EachIn app.Semanted()
  5730. If decl.IsImported() And decl.munged Continue
  5731. MungDecl decl
  5732. Local cdecl:TClassDecl=TClassDecl( decl )
  5733. If Not cdecl Continue
  5734. ' mung, but don't emit
  5735. ' Emit prefix + decl.munged+";"
  5736. 'PushMungScope
  5737. funcMungs = New TMap
  5738. BeginLocalScope
  5739. For Local decl:TDecl=EachIn cdecl.Semanted()
  5740. MungDecl decl
  5741. cdecl.SemantParts()
  5742. Next
  5743. EndLocalScope
  5744. 'PopMungScope
  5745. Next
  5746. ' forward declarations
  5747. For Local decl:TClassDecl=EachIn app.Semanted()
  5748. If decl.IsImported() Or (decl.IsExtern() And Not decl.IsStruct()) Continue
  5749. If Not decl.IsStruct()
  5750. Emit "struct " + decl.munged + "_obj;"
  5751. Else
  5752. Emit "struct " + decl.munged + ";"
  5753. End If
  5754. If decl.IsInterface() Then
  5755. Emit "extern const struct BBInterface " + decl.munged + "_ifc;"
  5756. End If
  5757. Next
  5758. 'prototypes/header! - structs first
  5759. For Local decl:TDecl=EachIn app.Semanted()
  5760. If decl.IsImported() Continue
  5761. Local cdecl:TClassDecl=TClassDecl( decl )
  5762. If cdecl
  5763. If cdecl.IsStruct() Then
  5764. EmitStructClassProto cdecl
  5765. End If
  5766. EndIf
  5767. Next
  5768. ' prototypes/header - typedefs
  5769. For Local cdecl:TClassDecl=EachIn app.Semanted()
  5770. If cdecl.IsImported() Continue
  5771. If Not cdecl.IsStruct() And cdecl.IsExtern() Then
  5772. EmitExternClassProtoTypedef(cdecl)
  5773. End If
  5774. Next
  5775. 'prototypes/header!
  5776. For Local decl:TDecl=EachIn app.Semanted()
  5777. If decl.IsImported() Continue
  5778. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  5779. If gdecl
  5780. MungDecl gdecl
  5781. If Not gdecl.IsPrivate() Then
  5782. If Not TFunctionPtrType(gdecl.ty) Then
  5783. Emit "extern "+TransThreadedGlobal(gdecl)+TransRefType( gdecl.ty, "" )+" "+gdecl.munged+";" 'forward reference...
  5784. Else
  5785. If Not TFunctionPtrType(gdecl.ty).func.noCastGen Then
  5786. ' generate function pointer refs if we haven't been told not to
  5787. ' If Not gdecl.IsExtern() Then
  5788. Emit "extern " + TransThreadedGlobal(gdecl) + TransRefType( gdecl.ty, gdecl.munged )+";" 'forward reference...
  5789. ' End If
  5790. End If
  5791. End If
  5792. End If
  5793. Continue
  5794. EndIf
  5795. Local fdecl:TFuncDecl=TFuncDecl( decl )
  5796. If fdecl' And Not fdecl.IsExtern()
  5797. ' don't include the main function - it's handled separately
  5798. If fdecl = app.mainFunc Then
  5799. Continue
  5800. End If
  5801. EmitGDBDebug(fdecl)
  5802. EmitFuncDecl( fdecl, True)
  5803. Continue
  5804. EndIf
  5805. Local cdecl:TClassDecl=TClassDecl( decl )
  5806. If cdecl
  5807. If Not cdecl.IsStruct() Then
  5808. If Not cdecl.IsExtern()
  5809. EmitClassProto cdecl
  5810. Else
  5811. EmitExternClassProto cdecl
  5812. End If
  5813. 'Else
  5814. ' EmitStructClassProto cdecl
  5815. End If
  5816. 'Continue
  5817. EndIf
  5818. Local edecl:TEnumDecl = TEnumDecl( decl )
  5819. If edecl Then
  5820. EmitEnumProto edecl
  5821. Continue
  5822. End If
  5823. Next
  5824. End Method
  5825. Method IncBinRequiresRebuild:Int(file:String, incbins:TList)
  5826. ' file doesn't exist?
  5827. If Not FileType(file) Then
  5828. Return True
  5829. End If
  5830. Local timestamp:Int = FileTime(file)
  5831. ' file exists... read header and compare names
  5832. ' read lines until "// ----"
  5833. ' TODO
  5834. Local files:TList = New TList
  5835. Local hashes:TMap = New TMap
  5836. Local stream:TStream = ReadFile(file)
  5837. While True
  5838. Local s:String = ReadLine(stream)
  5839. If Not s.StartsWith("// ") Or s.StartsWith("// ----") Then
  5840. Exit
  5841. End If
  5842. Local ind:Int = s.Find("// FILE : ")
  5843. If ind = 0 Then
  5844. Local line:String = s[10..]
  5845. Local parts:String[] = line.Split("~t")
  5846. If parts.length = 1 Then
  5847. Return True
  5848. End If
  5849. Local filename:String = parts[0].Replace("~q","")
  5850. Local fileHash:String = parts[1]
  5851. files.AddLast(filename)
  5852. hashes.Insert(filename, fileHash)
  5853. End If
  5854. Wend
  5855. stream.Close()
  5856. ' different number of files?
  5857. If files.Count() <> incbins.Count() Then
  5858. Return True
  5859. End If
  5860. ' different file names?
  5861. Local count:Int
  5862. For Local s:String = EachIn files
  5863. For Local ib:TIncbin = EachIn incbins
  5864. If s = ib.file Then
  5865. count :+ 1
  5866. Exit
  5867. End If
  5868. Next
  5869. Next
  5870. If count <> files.count() Then
  5871. Return True
  5872. End If
  5873. count = 0
  5874. For Local ib:TIncbin = EachIn incbins
  5875. For Local s:String = EachIn files
  5876. If s = ib.file Then
  5877. count :+ 1
  5878. Exit
  5879. End If
  5880. Next
  5881. Next
  5882. If count <> incbins.count() Then
  5883. Return True
  5884. End If
  5885. For Local ib:TIncbin = EachIn incbins
  5886. If timestamp < FileTime(ib.path) Then
  5887. Return True
  5888. End If
  5889. Local fileHash:String = String(hashes.ValueForKey(ib.file))
  5890. If Not fileHash Then
  5891. Return True
  5892. End If
  5893. If fileHash <> CalculateFileHash(ib.path) Then
  5894. Return True
  5895. End If
  5896. ' set the length, as we will need this later if we aren't loading the files now.
  5897. ib.length = FileSize(ib.path)
  5898. Next
  5899. Return False
  5900. End Method
  5901. Method TransIncBin(app:TAppDecl)
  5902. If app.incbins.Count() > 0 Then
  5903. SetOutput("incbin")
  5904. Local mung:String = FileMung(False)
  5905. Local name:String = StripAll(app.mainModule.filepath)
  5906. Local file:String
  5907. If opt_legacy_incbin Then
  5908. file = "incbin.c"
  5909. Else
  5910. file = "incbin2.c"
  5911. End If
  5912. Local filepath:String = OutputFilePath(opt_filepath, mung, file)
  5913. If IncBinRequiresRebuild(filepath, app.incbins) Then
  5914. If Not opt_legacy_incbin Then
  5915. Emit "#define INCBIN_PREFIX _ib"
  5916. Emit "#define INCBIN_STYLE INCBIN_STYLE_SNAKE"
  5917. Emit "#include ~qbrl.mod/blitz.mod/incbin/incbin.h~q"
  5918. End If
  5919. app.genIncBinHeader = True
  5920. For Local ib:TIncbin = EachIn app.incbins
  5921. Local fileHash:String = CalculateFileHash(ib.path)
  5922. Emit "// FILE : " + Enquote(ib.file) + "~t" + fileHash
  5923. Next
  5924. Emit "// ----"
  5925. For Local ib:TIncbin = EachIn app.incbins
  5926. EmitIncBinFile(ib)
  5927. Next
  5928. End If
  5929. SetOutput("pre_source")
  5930. End If
  5931. End Method
  5932. Method TransGlobalInit(decl:TGlobalDecl)
  5933. If TFunctionPtrType(decl.ty) Then
  5934. If TInvokeExpr(decl.init) And Not TInvokeExpr(decl.init).invokedWithBraces Then
  5935. Emit TransGlobal( decl )+"="+TInvokeExpr(decl.init).decl.munged + ";"
  5936. Else
  5937. Emit TransGlobal( decl )+"="+decl.init.Trans()+";"
  5938. End If
  5939. Else
  5940. If Not decl.funcGlobal Then
  5941. If TObjectType(decl.ty) And Not TObjectType(decl.ty).classDecl.IsStruct() Then
  5942. Emit TransGlobal( decl )+"="+Bra(TransObject(TObjectType(decl.ty).classDecl))+decl.init.Trans()+";"
  5943. Else
  5944. Emit TransGlobal( decl )+"="+decl.init.Trans()+";"
  5945. End If
  5946. End If
  5947. End If
  5948. End Method
  5949. Method TransSource(app:TAppDecl)
  5950. SetOutput("pre_source")
  5951. ' include our header
  5952. EmitModuleInclude(app.mainModule)
  5953. ' incbins
  5954. TransIncBin(app)
  5955. SetOutput("source")
  5956. ' nested type forward declarations
  5957. For Local decl:TClassDecl=EachIn app.Semanted()
  5958. For Local cdecl:TClassDecl = EachIn decl._decls
  5959. MungDecl decl
  5960. MungDecl cdecl
  5961. If cdecl.IsImported() Or (cdecl.IsExtern() And Not cdecl.IsStruct()) Continue
  5962. If Not cdecl.IsStruct()
  5963. Emit "struct " + cdecl.munged + "_obj;"
  5964. Else
  5965. Emit "struct " + cdecl.munged + ";"
  5966. End If
  5967. If cdecl.IsInterface() Then
  5968. Emit "extern const struct BBInterface " + cdecl.munged + "_ifc;"
  5969. End If
  5970. Next
  5971. Next
  5972. ' Private Global declarations
  5973. ' since we don't declare them in the header, they need to be near the top of the source
  5974. For Local decl:TDecl=EachIn app.Semanted()
  5975. If decl.IsImported() Continue
  5976. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  5977. If gdecl And gdecl.IsPrivate() Then
  5978. If Not TFunctionPtrType(gdecl.ty) Then
  5979. If TConstExpr(gdecl.init) Then
  5980. Emit TransRefType( gdecl.ty, "WW" )+" "+TransGlobalDecl(gdecl)+";"
  5981. gdecl.inited = True
  5982. Else
  5983. If Not gdecl.IsExtern() Then
  5984. Emit TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+";"
  5985. Else
  5986. ' delcare in source for any references to it locally in this module
  5987. Emit "extern "+ TransThreadedGlobal(gdecl) +TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+";"
  5988. End If
  5989. End If
  5990. Else
  5991. If Not gdecl.IsExtern() Then
  5992. Emit TransRefType( gdecl.ty, gdecl.munged ) + ";"
  5993. EndIf
  5994. End If
  5995. Continue
  5996. EndIf
  5997. Next
  5998. For Local gdecl:TGlobalDecl=EachIn app.SemantedGlobals
  5999. If gdecl And gdecl.funcGlobal Then
  6000. MungDecl gdecl
  6001. If Not TFunctionPtrType(gdecl.ty) Then
  6002. Emit "static " + TransThreadedGlobal(gdecl) + TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+";"
  6003. Else
  6004. Emit "static " + TransThreadedGlobal(gdecl) + TransRefType( gdecl.ty, gdecl.munged ) + ";"
  6005. End If
  6006. Continue
  6007. End If
  6008. Next
  6009. 'definitions!
  6010. For Local decl:TDecl=EachIn app.Semanted()
  6011. Local isImportedClassImpl:Int = TClassDecl( decl ) And decl.declImported And TClassDecl( decl ).instanceof And opt_apptype
  6012. If decl.IsImported() And Not isImportedClassImpl Continue
  6013. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  6014. If gdecl
  6015. If gdecl.IsPrivate() Continue
  6016. If Not TFunctionPtrType(gdecl.ty) And Not gdecl.IsPrivate() Then
  6017. If TConstExpr(gdecl.init) Then
  6018. Emit TransThreadedGlobal(gdecl) + TransRefType( gdecl.ty, "WW" )+" "+TransGlobalDecl(gdecl)+";"
  6019. gdecl.inited = True
  6020. Else
  6021. If Not gdecl.IsExtern() Then
  6022. If TObjectType(gdecl.ty) Then
  6023. Emit TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+ "=" + Bra(TransObject(TObjectType(gdecl.ty).classDecl)) + TransValue(gdecl.ty, "") + ";"
  6024. Else
  6025. Emit TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+ "=" + TransValue(gdecl.ty, "") + ";"
  6026. End If
  6027. End If
  6028. End If
  6029. Else
  6030. If TFunctionPtrType(gdecl.ty) And Not gdecl.IsExtern() Then
  6031. Emit TransRefType( gdecl.ty, gdecl.munged ) + ";"
  6032. End If
  6033. End If
  6034. Continue
  6035. EndIf
  6036. Local fdecl:TFuncDecl=TFuncDecl( decl )
  6037. If fdecl And Not fdecl.IsExtern()
  6038. ' don't include the main function - it's handled separately
  6039. If fdecl = app.mainFunc Then
  6040. Continue
  6041. End If
  6042. EmitGDBDebug(fdecl)
  6043. EmitFuncDecl fdecl
  6044. Continue
  6045. EndIf
  6046. Local cdecl:TClassDecl=TClassDecl( decl )
  6047. If cdecl
  6048. EmitGDBDebug(cdecl)
  6049. EmitClassDecl cdecl
  6050. Continue
  6051. EndIf
  6052. Local edecl:TEnumDecl = TEnumDecl( decl )
  6053. If edecl Then
  6054. EmitEnumDecl edecl
  6055. Continue
  6056. End If
  6057. Next
  6058. ' emit nested functions/classes for localmain
  6059. ' emit nested protos
  6060. For Local fdecl:TFuncDecl = EachIn app.mainFunc._decls
  6061. EmitFuncDecl(fdecl, True)
  6062. Next
  6063. ' emit nested bodies
  6064. For Local fdecl:TFuncDecl = EachIn app.mainFunc._decls
  6065. EmitFuncDecl(fdecl, False)
  6066. Next
  6067. ' incbin decls
  6068. For Local ib:TIncbin = EachIn app.incbins
  6069. If opt_legacy_incbin Then
  6070. Emit "extern unsigned char * " + app.munged + "_ib_" + ib.id + ";"
  6071. Else
  6072. Emit "extern const unsigned char * " + ib.GeneratedDataName(app) + ";"
  6073. Emit "extern const unsigned int " + ib.GeneratedSizeName(app) + ";"
  6074. End If
  6075. Next
  6076. ' coverage
  6077. Local covCount:Int
  6078. If opt_coverage Then
  6079. Local id:Int
  6080. For Local file:String = EachIn coverageFileInfo.Keys()
  6081. Local covFile:TCoverageLineInfo = TCoverageLineInfo(coverageFileInfo.ValueForKey(file))
  6082. Local t:String
  6083. Emit "static int coverage_lines_" + id + "[] = {"
  6084. For Local i:Int = 0 Until covFile.lines.Length
  6085. If i And i Mod 40 = 0 Then
  6086. If i Then
  6087. t :+ ","
  6088. End If
  6089. Emit t
  6090. t = ""
  6091. Else
  6092. If i Then
  6093. t :+ ","
  6094. End If
  6095. End If
  6096. t :+ covFile.lines[i]
  6097. Next
  6098. If t Then
  6099. Emit t
  6100. End If
  6101. Emit "};"
  6102. Emit "static BBCoverageFunctionInfo coverage_funcs_" + id + "[] = {"
  6103. Local covFuncFile:TCoverageFunctionLineInfo = TCoverageFunctionLineInfo(coverageFunctionFileInfo.ValueForKey(file))
  6104. For Local i:Int = 0 Until covFuncFile.funcs.Length
  6105. Emit "{ " + Enquote(covFuncFile.funcs[i].name) + ", " + covFuncFile.funcs[i].line + " },"
  6106. Next
  6107. Emit "};"
  6108. id :+ 1
  6109. Next
  6110. covCount = id
  6111. If id Then
  6112. id = 0
  6113. Emit "static BBCoverageFileInfo coverage_files[] = {"
  6114. For Local file:String = EachIn coverageFileInfo.Keys()
  6115. Local covFile:TCoverageLineInfo = TCoverageLineInfo(coverageFileInfo.ValueForKey(file))
  6116. Emit "{"
  6117. Emit Enquote(file) + ","
  6118. Emit "coverage_lines_" + id + ","
  6119. Emit "sizeof(coverage_lines_" + id + ") / sizeof(coverage_lines_" + id + "[0]),"
  6120. Emit "NULL,"
  6121. Emit "coverage_funcs_" + id + ","
  6122. Emit "sizeof(coverage_funcs_" + id + ") / sizeof(coverage_funcs_" + id + "[0]),"
  6123. Emit "NULL,"
  6124. Emit "},"
  6125. id :+ 1
  6126. Next
  6127. Emit "{ NULL, NULL, 0, NULL, NULL, 0, NULL }"
  6128. Emit "};"
  6129. End If
  6130. End If
  6131. ' registrations
  6132. Emit "static int " + app.munged + "_reg_inited" + " = 0;"
  6133. Emit "void " + app.munged + "_register(){"
  6134. Emit "if (!" + app.munged + "_reg_inited) {"
  6135. Emit app.munged + "_reg_inited = 1;"
  6136. Local registerOnce:TMap = New TMap
  6137. ' call any imported mod registers
  6138. For Local decl:TModuleDecl=EachIn app.imported.Values()
  6139. For Local mdecl:TDecl=EachIn decl.imported.Values()
  6140. If TModuleDecl(mdecl) And app.mainModule <> mdecl And mdecl.ident <> "brl.classes" And mdecl.ident <> "brl.blitzkeywords" Then
  6141. If Not registerOnce.Contains(mdecl.ident) Then
  6142. EmitModuleRegisterInit(TModuleDecl(mdecl))
  6143. registerOnce.Insert(mdecl.ident, "")
  6144. End If
  6145. End If
  6146. Next
  6147. Next
  6148. ' initialise coverage
  6149. If opt_coverage And covCount Then
  6150. Emit "bbCoverageRegisterFile(coverage_files);"
  6151. End If
  6152. ' initialise enums
  6153. For Local decl:TEnumDecl = EachIn app.Semanted()
  6154. If decl.IsImported() Continue
  6155. Emit decl.munged + "_BBEnum_impl = (BBEnum *)&" + decl.munged + "_BBEnum;"
  6156. Next
  6157. ' register types
  6158. For Local decl:TDecl=EachIn app.Semanted()
  6159. If decl.IsImported() Continue
  6160. Local cdecl:TClassDecl=TClassDecl( decl )
  6161. If cdecl And Not cdecl.IsExtern() And Not cdecl.args
  6162. If cdecl.instanceof And Not opt_apptype
  6163. ' ony generated class implementations in main app
  6164. Continue
  6165. End If
  6166. If Not cdecl.IsInterface() Then
  6167. If Not cdecl.IsStruct() Then
  6168. Emit "bbObjectRegisterType((BBCLASS)&" + cdecl.munged + ");"
  6169. Else
  6170. Emit "bbObjectRegisterStruct((BBDebugScope *)&" + cdecl.munged + "_scope);"
  6171. End If
  6172. Else
  6173. Emit "bbObjectRegisterInterface((BBInterface *)&" + cdecl.munged + "_ifc);"
  6174. End If
  6175. Continue
  6176. EndIf
  6177. Local edecl:TEnumDecl = TEnumDecl( decl )
  6178. If edecl Then
  6179. Emit "bbEnumRegister((BBEnum *)" + decl.munged + "_BBEnum_impl, (BBDebugScope *)&" + edecl.munged + "_scope);"
  6180. End If
  6181. Next
  6182. '
  6183. ' register files
  6184. If opt_debug Then
  6185. For Local Hash:String = EachIn fileRegister.Keys()
  6186. Local file:String = String(fileRegister.ValueForKey(Hash))
  6187. file = file.Replace("\", "\\")
  6188. Emit "bbRegisterSource(" + Hash + ", ~q" + file + "~q);"
  6189. Next
  6190. End If
  6191. Emit "}"
  6192. Emit "}"
  6193. ' main init
  6194. Emit "static int " + app.munged + "_inited" + " = 0;"
  6195. Emit "int " + app.munged + "(){"
  6196. ' initialise stuff
  6197. Emit "if (!" + app.munged + "_inited) {"
  6198. Emit app.munged + "_inited = 1;"
  6199. ' only for main app
  6200. If opt_apptype Then
  6201. Emit app.munged + "_register();"
  6202. End If
  6203. Emit "bb_init_strings();"
  6204. ' add global roots
  6205. Local first:TGlobalDecl
  6206. Local last:TGlobalDecl
  6207. For Local decl:TGlobalDecl=EachIn app.semantedGlobals
  6208. If decl.IsImported() Continue
  6209. decl.Semant
  6210. If Not first Then
  6211. first = decl
  6212. End If
  6213. last = decl
  6214. Next
  6215. If first Then
  6216. Emit "GC_add_roots(&" + first.munged + ", &" + last.munged + " + 1);"
  6217. End If
  6218. ' threaded global scope assignments
  6219. For Local decl:TDecl=EachIn app.Semanted()
  6220. If decl.IsImported() Continue
  6221. Local cdecl:TClassDecl=TClassDecl( decl )
  6222. If cdecl
  6223. EmitClassThreadedGlobalDebugInit(cdecl)
  6224. End If
  6225. Next
  6226. ' register incbins
  6227. For Local ib:TIncbin = EachIn app.incbins
  6228. If opt_legacy_incbin Then
  6229. Emit "bbIncbinAdd((BBString*)&" + StringConstId(ib.file) + ",&" + app.munged + "_ib_" + ib.id + "," + ib.length + ");"
  6230. Else
  6231. Emit "bbIncbinAdd((BBString*)&" + StringConstId(ib.file) + ",&" + ib.GeneratedDataName(app) + "," + ib.GeneratedSizeName(app) + ");"
  6232. End If
  6233. Next
  6234. Local importOnce:TMap = New TMap
  6235. ' call any imported mod inits
  6236. For Local decl:TModuleDecl=EachIn app.imported.Values()
  6237. For Local mdecl:TDecl=EachIn decl.imported.Values()
  6238. If TModuleDecl(mdecl) And app.mainModule <> mdecl And mdecl.ident <> "brl.classes" And mdecl.ident <> "brl.blitzkeywords" Then
  6239. If Not importOnce.Contains(mdecl.ident) Then
  6240. EmitModuleInit(TModuleDecl(mdecl))
  6241. importOnce.Insert(mdecl.ident, "")
  6242. End If
  6243. End If
  6244. Next
  6245. Next
  6246. ' defdata init
  6247. If Not app.dataDefs.IsEmpty() Then
  6248. Emit "_defDataOffset = &_defData;"
  6249. End If
  6250. ' initialise globals
  6251. For Local decl:TGlobalDecl=EachIn app.semantedGlobals
  6252. If decl.IsImported() Continue
  6253. decl.Semant
  6254. If decl.scope And TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct() Then
  6255. Emit TransGlobal( decl )+"="+TransValue(decl.ty, Null)+";"
  6256. End If
  6257. Next
  6258. ' initialise globals
  6259. For Local decl:TGlobalDecl=EachIn app.semantedGlobals
  6260. If decl.IsImported() Continue
  6261. 'decl.Semant
  6262. ' TODO : what about OnDebugStop etc, who have no init ?
  6263. If decl.init And Not (decl.attrs & DECL_INITONLY) Then
  6264. If decl.scope And TClassDecl(decl.scope) Then
  6265. ' class global inits need to be generated in the correct order.
  6266. ' only generate global inits if the parent class hasn't already been processed,
  6267. ' otherwise, we will skip this global as it should already have been generated.
  6268. If Not TClassDecl(decl.scope).globInit Then
  6269. TClassDecl(decl.scope).globInit = True
  6270. For Local gdecl:TGlobalDecl = EachIn decl.scope._decls
  6271. If gdecl.IsImported() Continue
  6272. gdecl.Semant
  6273. If gdecl.init And Not (gdecl.attrs & DECL_INITONLY) Then
  6274. TransGlobalInit(gdecl)
  6275. End If
  6276. Next
  6277. End If
  6278. Else
  6279. TransGlobalInit(decl)
  6280. End If
  6281. End If
  6282. Next
  6283. If opt_apptype Then
  6284. Emit "bbRunAtstart();"
  6285. End If
  6286. ' now do the local main stuff
  6287. app.mainFunc.Semant()
  6288. EmitLocalDeclarations(app.mainFunc)
  6289. EmitBlock app.mainFunc
  6290. Emit "}"
  6291. Emit "return 0;"
  6292. Emit "}"
  6293. ' redirect string generation to the def data section of the source
  6294. SetOutput("def_data")
  6295. ' defdata
  6296. EmitDefDataArray(app)
  6297. ' redirect string generation to the top of the source
  6298. SetOutput("pre_source")
  6299. ' strings
  6300. ' generate sized structs
  6301. Local sizes:TMap = New TMap
  6302. For Local s:String = EachIn app.stringConsts.Keys()
  6303. If s Then
  6304. Local key:TStringConst = TStringConst(app.stringConsts.ValueForKey(s))
  6305. If key.used > 0 Then
  6306. If Not sizes.Contains(String s.length) Then
  6307. Emit "struct BBString_" + s.length + "{BBClass_String* clas;BBUINT hash;int length;BBChar buf[" + s.length + "];};"
  6308. sizes.Insert(String s.length, "")
  6309. End If
  6310. End If
  6311. End If
  6312. Next
  6313. For Local s:String = EachIn app.stringConsts.Keys()
  6314. If s Then
  6315. Local key:TStringConst = TStringConst(app.stringConsts.ValueForKey(s))
  6316. If key.used > 0 Then
  6317. Emit "static struct BBString_" + s.length + " " + key.id + "={"
  6318. Emit "&bbStringClass,"
  6319. Emit "0,"
  6320. Emit s.length + ","
  6321. Local t:String = "{"
  6322. For Local i:Int = 0 Until s.length
  6323. If i Then
  6324. t:+ ","
  6325. End If
  6326. t:+ s[i]
  6327. If i And Not (i Mod 16) Then
  6328. Emit t
  6329. t = ""
  6330. End If
  6331. Next
  6332. Emit t + "}"
  6333. Emit "};"
  6334. End If
  6335. End If
  6336. Next
  6337. ' scope defs
  6338. If Not app.scopedefs.IsEmpty() Then
  6339. For Local val:String = EachIn app.scopedefs.Keys()
  6340. Local i:Int = val.ToInt()
  6341. Emit "struct BBDebugScope_" + i + "{int kind; const char *name; BBDebugDecl decls[" + (i + 1) + "]; };"
  6342. Next
  6343. End If
  6344. ' init strings
  6345. Emit "static void bb_init_strings() {"
  6346. For Local s:String = EachIn app.stringConsts.Keys()
  6347. If s Then
  6348. Local key:TStringConst = TStringConst(app.stringConsts.ValueForKey(s))
  6349. If key.used > 0 Then
  6350. Emit "bbStringHash((BBString*)&" + key.id + ");"
  6351. End If
  6352. End If
  6353. Next
  6354. Emit "}"
  6355. End Method
  6356. Method EmitDefDataArray(app:TAppDecl)
  6357. If Not app.dataDefs.IsEmpty() Then
  6358. '
  6359. Emit "static struct bbDataDef * _defDataOffset;"
  6360. Emit "static struct bbDataDef _defData[" + TDefDataDecl.count + "]={"
  6361. For Local decl:TDefDataDecl = EachIn app.dataDefs
  6362. EmitDefData(decl)
  6363. Next
  6364. Emit "};"
  6365. End If
  6366. End Method
  6367. Method EmitDefData(decl:TDefDataDecl)
  6368. For Local i:Int = 0 Until decl.data.length
  6369. Local expr:TExpr = decl.data[i]
  6370. Emit "{"
  6371. Emit TransDefDataType(expr.exprType) + ","
  6372. Emit "{"
  6373. Emit "." + TransDefDataUnionType(expr.exprType) + " = " + expr.Trans()
  6374. Emit "}"
  6375. Emit "},"
  6376. Next
  6377. End Method
  6378. Method EmitIfcImports(impMod:TModuleDecl, processed:TMap)
  6379. For Local decl:TDecl=EachIn impMod.imported.Values()
  6380. Local mdecl:TModuleDecl=TModuleDecl( decl )
  6381. If mdecl And mdecl.ident <> "brl.classes" And mdecl.ident <> "brl.blitzkeywords" Then
  6382. If mdecl.filepath.EndsWith(".bmx")
  6383. If _appInstance.mainModule<>mdecl
  6384. EmitIfcImports(mdecl, processed)
  6385. For Local s:String = EachIn mdecl.fileImports
  6386. If Not processed.Contains("XX" + s + "XX") Then
  6387. Emit "import " + BmxEnquote(s)
  6388. processed.Insert("XX" + s + "XX", "")
  6389. End If
  6390. Next
  6391. End If
  6392. Else
  6393. If Not processed.Contains(mdecl.ident)
  6394. Emit "import " + mdecl.ident
  6395. processed.Insert(mdecl.ident, "")
  6396. End If
  6397. End If
  6398. End If
  6399. Next
  6400. End Method
  6401. Method EmitIfcStructImports(impMod:TModuleDecl, processed:TMap)
  6402. For Local decl:TDecl=EachIn impMod.imported.Values()
  6403. Local mdecl:TModuleDecl=TModuleDecl( decl )
  6404. If mdecl Then
  6405. If mdecl.filepath.EndsWith(".bmx") And _appInstance.mainModule<>mdecl And Not processed.Contains(mdecl)
  6406. EmitIfcStructImports(mdecl, processed)
  6407. processed.Insert(mdecl, mdecl)
  6408. For Local decl:TDecl=EachIn mdecl._decls
  6409. decl.Semant
  6410. ' enums
  6411. Local edecl:TEnumDecl=TEnumDecl( decl )
  6412. If edecl
  6413. EmitIfcEnumDecl(edecl)
  6414. Continue
  6415. EndIf
  6416. ' consts
  6417. Local cdecl:TConstDecl=TConstDecl( decl )
  6418. If cdecl
  6419. EmitIfcConstDecl(cdecl)
  6420. Continue
  6421. End If
  6422. ' classes
  6423. Local tdecl:TClassDecl=TClassDecl( decl )
  6424. If tdecl
  6425. EmitIfcClassDecl(tdecl)
  6426. Continue
  6427. EndIf
  6428. ' functions
  6429. Local fdecl:TFuncDecl=TFuncDecl( decl )
  6430. If fdecl And fdecl <> _appInstance.mainFunc Then
  6431. EmitIfcFuncDecl(fdecl)
  6432. Continue
  6433. End If
  6434. ' globals
  6435. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  6436. If gdecl
  6437. EmitIfcGlobalDecl(gdecl)
  6438. Continue
  6439. End If
  6440. Next
  6441. End If
  6442. End If
  6443. Next
  6444. End Method
  6445. Method FileHeaderFromFile:String(mdecl:TModuleDecl, includePath:Int = False)
  6446. Local name:String = StripAll(mdecl.filepath)
  6447. Local dir:String = ExtractDir(mdecl.filePath)
  6448. Local file:String = name + ".bmx" + FileMung(opt_apptype And (Not mdecl.IsImported())) + ".h"
  6449. If mdecl.relPath Then
  6450. Local dir:String = ExtractDir(mdecl.relPath)
  6451. If dir Then
  6452. file = "../" + dir + "/.bmx/" + file
  6453. End If
  6454. End If
  6455. Return file
  6456. End Method
  6457. Method MungImportFromFile:String(mdecl:TModuleDecl)
  6458. Local result:String
  6459. If opt_buildtype <> BUILDTYPE_MODULE Then
  6460. Local dir:String = ExtractDir(mdecl.filepath).ToLower()
  6461. dir = dir[dir.findLast("/") + 1..]
  6462. If dir.EndsWith(".mod") Then
  6463. dir = dir.Replace(".mod", "")
  6464. End If
  6465. Local file:String = StripDir(mdecl.filepath).ToLower()
  6466. result = "_bb_" + dir + "_" + StripExt(file)
  6467. Else
  6468. result = "_bb_" + mdecl.ident
  6469. End If
  6470. 'return with all non-allowed chars (like "-" or " ") removed
  6471. Return TStringHelper.Sanitize(result)
  6472. End Method
  6473. Method TransInterface(app:TAppDecl)
  6474. SetOutput("interface")
  6475. If app.mainModule.IsSuperStrict() Then
  6476. Emit "superstrict"
  6477. End If
  6478. ' module info
  6479. For Local info:String = EachIn app.mainModule.modInfo
  6480. Emit "ModuleInfo " + BmxEnquote(info)
  6481. Next
  6482. ' module pragmas
  6483. For Local pragma:String = EachIn app.mainModule.pragmas
  6484. Emit "#pragma " + BmxEnquote(pragma)
  6485. Next
  6486. Local processed:TMap = New TMap
  6487. ' module imports
  6488. For Local decl:TDecl=EachIn app.mainModule.imported.Values()
  6489. Local mdecl:TModuleDecl=TModuleDecl( decl )
  6490. If mdecl Then
  6491. If mdecl.IsActualModule() Then
  6492. Emit "import " + mdecl.ident
  6493. processed.Insert(mdecl.ident, "")
  6494. Else If Not opt_ismain And mdecl.filepath.EndsWith(".bmx") And app.mainModule<>mdecl
  6495. Local file:String = StripDir(mdecl.filepath)
  6496. If mdecl.relPath Then
  6497. Local dir:String = ExtractDir(mdecl.relPath)
  6498. If dir Then
  6499. file = dir + "/" + file
  6500. End If
  6501. End If
  6502. If Not processed.Contains(file) Then
  6503. Emit "import " + Enquote(file)
  6504. processed.Insert(file, "")
  6505. End If
  6506. End If
  6507. End If
  6508. Next
  6509. ' module imports from other files?
  6510. If opt_buildtype = BUILDTYPE_MODULE And opt_ismain Then
  6511. EmitIfcImports(app.mainModule, processed)
  6512. End If
  6513. ' other imports
  6514. For Local s:String = EachIn app.fileImports
  6515. Emit "import " + BmxEnquote(s)
  6516. Next
  6517. processed = New TMap
  6518. ' imported module structure (consts, classes, functions, etc)
  6519. If opt_buildtype = BUILDTYPE_MODULE And opt_ismain Then
  6520. EmitIfcStructImports(app.mainModule, processed)
  6521. End If
  6522. ' enums
  6523. For Local decl:TDecl=EachIn app.Semanted()
  6524. If decl.IsPrivate() Continue
  6525. Local edecl:TEnumDecl=TEnumDecl( decl )
  6526. If edecl And Not edecl.IsImported()
  6527. EmitIfcEnumDecl(edecl)
  6528. End If
  6529. Next
  6530. ' consts
  6531. For Local decl:TDecl=EachIn app.Semanted()
  6532. If decl.IsPrivate() Continue
  6533. Local cdecl:TConstDecl=TConstDecl( decl )
  6534. If cdecl And Not cdecl.IsImported()
  6535. EmitIfcConstDecl(cdecl)
  6536. End If
  6537. Next
  6538. ' classes
  6539. For Local decl:TDecl=EachIn app.Semanted()
  6540. Local cdecl:TClassDecl=TClassDecl( decl )
  6541. If cdecl And cdecl.IsPrivate() And (Not cdecl.IsStruct() Or (cdecl.IsStruct() And Not cdecl.exposed)) Then
  6542. Continue
  6543. End If
  6544. If cdecl And Not cdecl.IsImported()
  6545. EmitIfcClassDecl(cdecl)
  6546. EndIf
  6547. Next
  6548. ' functions
  6549. For Local decl:TDecl=EachIn app.Semanted()
  6550. If decl.IsPrivate() Continue
  6551. Local fdecl:TFuncDecl=TFuncDecl( decl )
  6552. If fdecl And fdecl <> app.mainFunc And Not fdecl.IsImported() Then
  6553. EmitIfcFuncDecl(fdecl)
  6554. End If
  6555. Next
  6556. ' globals
  6557. For Local decl:TDecl=EachIn app.Semanted()
  6558. If decl.IsPrivate() Continue
  6559. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  6560. If gdecl And Not gdecl.IsImported()
  6561. EmitIfcGlobalDecl(gdecl)
  6562. End If
  6563. Next
  6564. End Method
  6565. Method TransDef(app:TAppDecl)
  6566. SetOutput("def")
  6567. Emit "LIBRARY " + StripExt(StripDir(opt_filepath))
  6568. Emit "EXPORTS"
  6569. For Local decl:TFuncDecl=EachIn app.exportDefs
  6570. Emit "~t" + TransExportDef(decl, opt_arch = "x86")
  6571. Next
  6572. Emit "~n"
  6573. End Method
  6574. Method TransApp( app:TAppDecl )
  6575. If app.mainModule.IsSuperStrict()
  6576. opt_issuperstrict = True
  6577. End If
  6578. TransHeader(app)
  6579. TransSource(app)
  6580. TransInterface(app)
  6581. If opt_makelib Then
  6582. If opt_def And opt_apptype Then
  6583. TransDef(app)
  6584. End If
  6585. End If
  6586. End Method
  6587. End Type