ctranslator.bmx 235 KB

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