ctranslator.bmx 206 KB

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