ctranslator.bmx 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799
  1. ' Copyright (c) 2013-2016 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 TSizeTType( ty ) Return "~q" + p + "z~q"
  61. If TWParamType( ty ) Return "~q" + p + "w~q"
  62. If TLParamType( ty ) Return "~q" + p + "x~q"
  63. If TStringType( ty ) Return "~q$~q"
  64. If TInt128Type( ty ) Return "~q" + p + "j~q"
  65. If TFloat128Type( ty ) Return "~q" + p + "k~q"
  66. If TDouble128Type( ty ) Return "~q" + p + "m~q"
  67. If TFloat64Type( ty ) Return "~q" + p + "h~q"
  68. If TArrayType( ty ) Then
  69. Local s:String = "["
  70. For Local i:Int = 0 Until TArrayType( ty ).dims - 1
  71. s:+ ","
  72. Next
  73. s:+ "]"
  74. s:+ TransArrayType(TArrayType( ty ).elemType)
  75. Return Enquote(s.Replace("~q", ""))
  76. End If
  77. If TObjectType( ty ) Then
  78. If TObjectType( ty ).classdecl.IsStruct()
  79. Return "~q" + p + "@" + TObjectType(ty).classDecl.ident + "~q"
  80. Else
  81. If Not TObjectType( ty ).classdecl.IsExtern()
  82. Return "~q:" + TObjectType(ty).classDecl.ident + "~q"
  83. Else
  84. If TObjectType( ty ).classdecl.IsInterface() Then
  85. Return "~q" + p + "*#" + TObjectType(ty).classDecl.ident + "~q"
  86. ' ElseIf TObjectType( ty ).classdecl.IsStruct()
  87. ' Return "~q" + p + "@" + TObjectType(ty).classDecl.ident + "~q"
  88. Else
  89. Return "~q" + p + "#" + TObjectType(ty).classDecl.ident + "~q"
  90. End If
  91. End If
  92. End If
  93. End If
  94. If TFunctionPtrType( ty ) Return "~q(~q"
  95. End Method
  96. Method TransDefDataType$( ty:TType)
  97. If TByteType( ty ) Return "~qb~q"
  98. If TShortType( ty ) Return "~qs~q"
  99. If TIntType( ty ) Return "~qi~q"
  100. If TUIntType( ty ) Return "~qu~q"
  101. If TFloatType( ty ) Return "~qf~q"
  102. If TDoubleType( ty ) Return "~qd~q"
  103. If TLongType( ty ) Return "~ql~q"
  104. If TULongType( ty ) Return "~qy~q"
  105. If TSizeTType( ty ) Return "~qz~q"
  106. If TStringType( ty ) Return "~q$~q"
  107. If TWParamType( ty ) Return "~qw~q"
  108. If TLParamType( ty ) Return "~qx~q"
  109. End Method
  110. Method TransDefDataConversion$(ty:TType)
  111. If TByteType( ty ) Return "bbConvertToInt"
  112. If TShortType( ty ) Return "bbConvertToInt"
  113. If TIntType( ty ) Return "bbConvertToInt"
  114. If TUIntType( ty ) Return "bbConvertToUInt"
  115. If TFloatType( ty ) Return "bbConvertToFloat"
  116. If TDoubleType( ty ) Return "bbConvertToDouble"
  117. If TLongType( ty ) Return "bbConvertToLong"
  118. If TULongType( ty ) Return "bbConvertToULong"
  119. If TSizeTType( ty ) Return "bbConvertToSizet"
  120. If TStringType( ty ) Return "bbConvertToString"
  121. End Method
  122. Method TransDefDataUnionType$(ty:TType)
  123. If TByteType( ty ) Return "b"
  124. If TShortType( ty ) Return "s"
  125. If TIntType( ty ) Return "i"
  126. If TUIntType( ty ) Return "u"
  127. If TFloatType( ty ) Return "f"
  128. If TDoubleType( ty ) Return "d"
  129. If TLongType( ty ) Return "l"
  130. If TULongType( ty ) Return "y"
  131. If TSizeTType( ty ) Return "z"
  132. If TWParamType( ty ) Return "w"
  133. If TLParamType( ty ) Return "x"
  134. If TStringType( ty ) Return "t"
  135. End Method
  136. Method TransDebugScopeType$(ty:TType)
  137. Local p:String = TransSPointer(ty)
  138. If TByteType( ty ) Return p + "b"
  139. If TShortType( ty ) Return p + "s"
  140. If TIntType( ty ) Return p + "i"
  141. If TUIntType( ty ) Return p + "u"
  142. If TFloatType( ty ) Return p + "f"
  143. If TDoubleType( ty ) Return p + "d"
  144. If TLongType( ty ) Return p + "l"
  145. If TULongType( ty ) Return p + "y"
  146. If TSizeTType( ty ) Return p + "t"
  147. If TWParamType( ty ) Return p + "W"
  148. If TLParamType( ty ) Return p + "X"
  149. If TInt128Type( ty ) Return p + "j"
  150. If TFloat128Type( ty ) Return p + "k"
  151. If TDouble128Type( ty ) Return p + "m"
  152. If TFloat64Type( ty ) Return p + "h"
  153. If TStringType( ty ) Return "$"
  154. If TArrayType( ty ) Then
  155. Local s:String = "["
  156. For Local i:Int = 0 Until TArrayType( ty ).dims - 1
  157. s:+ ","
  158. Next
  159. s:+ "]"
  160. Return s + TransDebugScopeType(TArrayType( ty ).elemType)
  161. End If
  162. If TObjectType( ty ) Then
  163. If TObjectType( ty ).classdecl.IsStruct() Then
  164. Return p + "@" + TObjectType(ty).classDecl.ident
  165. Else If Not TObjectType( ty ).classdecl.IsExtern()
  166. Return ":" + TObjectType( ty ).classDecl.ident
  167. Else
  168. If TObjectType( ty ).classdecl.IsInterface() Then
  169. Return p + "*#" + TObjectType(ty).classDecl.ident
  170. Else
  171. Return p + "#" + TObjectType(ty).classDecl.ident
  172. End If
  173. End If
  174. End If
  175. If TFunctionPtrType( ty ) Then
  176. Local func:TFuncDecl = TFunctionPtrType( ty ).func
  177. Local s:String = "("
  178. For Local i:Int = 0 Until func.argDecls.length
  179. If i Then
  180. s :+ ","
  181. End If
  182. s :+ TransDebugScopeType(func.argDecls[i].ty)
  183. Next
  184. Return s + ")" + TransDebugScopeType(func.retType)
  185. End If
  186. End Method
  187. Method TransType$( ty:TType, ident:String, fpReturnTypeFunctionArgs:String = Null, fpReturnTypeClassFunc:Int = False)
  188. Local p:String = TransSPointer(ty, True)
  189. If TVoidType( ty ) Or Not ty Then
  190. Return "void"
  191. End If
  192. If TBoolType( ty ) Return "BBINT" + p
  193. If TByteType( ty ) Return "BBBYTE" + p
  194. If TShortType( ty ) Return "BBSHORT" + p
  195. If TIntType( ty ) Return "BBINT" + p
  196. If TUIntType( ty ) Return "BBUINT" + p
  197. If TFloatType( ty ) Return "BBFLOAT" + p
  198. If TDoubleType( ty ) Return "BBDOUBLE" + p
  199. If TLongType( ty ) Return "BBLONG" + p
  200. If TULongType( ty ) Return "BBULONG" + p
  201. If TSizeTType( ty ) Return "BBSIZET" + p
  202. If TWParamType( ty ) Return "WPARAM" + p
  203. If TLParamType( ty ) Return "LPARAM" + p
  204. If TInt128Type( ty ) Return "BBINT128" + p
  205. If TFloat128Type( ty ) Return "BBFLOAT128" + p
  206. If TDouble128Type( ty ) Return "BBDOUBLE128" + p
  207. If TFloat64Type( ty ) Return "BBFLOAT64" + p
  208. If TStringType( ty ) Then
  209. If ty._flags & TType.T_CHAR_PTR Then
  210. Return "BBBYTE *"
  211. Else If ty._flags & TType.T_SHORT_PTR Then
  212. Return "BBSHORT *"
  213. End If
  214. Return "BBSTRING" + p
  215. End If
  216. If TArrayType( ty ) Return "BBARRAY" + p
  217. If TObjectType( ty ) Then
  218. Return TransObject(TObjectType(ty).classdecl) + p
  219. End If
  220. If TFunctionPtrType( ty ) Then
  221. TFunctionPtrType(ty).func.Semant
  222. Local api:String
  223. If TFunctionPtrType(ty).func.attrs & DECL_API_WIN32 Then
  224. api = " __stdcall "
  225. End If
  226. Local args:String
  227. For Local arg:TArgDecl = EachIn TFunctionPtrType(ty).func.argDecls
  228. arg.Semant()
  229. If args Then
  230. args :+ ","
  231. End If
  232. args :+ TransType(arg.ty, "")
  233. Next
  234. Local ret:String = ""
  235. If fpReturnTypeFunctionArgs Then
  236. ret = Bra(fpReturnTypeFunctionArgs)
  237. End If
  238. If fpReturnTypeClassFunc Then
  239. ' typedef for function pointer return type
  240. Return ident + "x" + Bra(api + p +"* " + ident) + Bra(args)
  241. Else
  242. ' if a function F returns another function (let's call it G),
  243. ' then C syntax requires the declaration of F to be nested into that of the type of G
  244. ' e.g. "Function F:RetG(ArgG)(ArgF)" in BlitzMax becomes "RetG(* F(ArgF) )(ArgG)" in C
  245. ' solution: use "* F(ArgF)" as an ident to generate a declaration for G
  246. ' the result will be the declaration for F
  247. Local callable:String = Bra(api + p +"* " + ident + ret)
  248. If TFunctionPtrType(TFunctionPtrType(ty).func.retType) Then
  249. If Not args Then args = " " ' make sure the parentheses aren't ommited even if the parameter list is empty
  250. Return TransType(TFunctionPtrType(ty).func.retType, callable, args)
  251. Else
  252. Local retTypeStr:String = TransType(TFunctionPtrType(ty).func.retType, "")
  253. Return retTypeStr + callable + Bra(args)
  254. End If
  255. End If
  256. End If
  257. If TExternObjectType( ty ) Return "struct " + TExternObjectType( ty ).classDecl.munged + p
  258. InternalErr
  259. End Method
  260. Method TransIfcType$( ty:TType, isSuperStrict:Int = False )
  261. Local p:String = TransSPointer(ty)
  262. If ty And (ty._flags & TType.T_VAR) Then
  263. p :+ " Var"
  264. End If
  265. If Not ty Then
  266. If opt_issuperstrict Or isSuperStrict Then
  267. Return p
  268. Else
  269. Return "%" + p
  270. End If
  271. End If
  272. If TVoidType( ty ) Then
  273. Return p
  274. End If
  275. If TByteType( ty ) Return "@" + p
  276. If TShortType( ty ) Return "@@" + p
  277. If TIntType( ty ) Return "%" + p
  278. If TUIntType( ty ) Return "|" + p
  279. If TFloatType( ty ) Return "#" + p
  280. If TDoubleType( ty ) Return "!" + p
  281. If TLongType( ty ) Return "%%" + p
  282. If TULongType( ty ) Return "||" + p
  283. If TSizeTType( ty ) Return "%z" + p
  284. If TWParamType( ty ) Return "%w" + p
  285. If TLParamType( ty ) Return "%x" + p
  286. If TInt128Type( ty ) Return "%j" + p
  287. If TFloat128Type( ty ) Return "!k" + p
  288. If TDouble128Type( ty ) Return "!m" + p
  289. If TFloat64Type( ty ) Return "!h" + p
  290. If TStringType( ty ) Then
  291. If ty._flags & TType.T_CHAR_PTR Then
  292. Return "$z"
  293. Else If ty._flags & TType.T_SHORT_PTR Then
  294. Return "$w"
  295. End If
  296. Return "$" + p
  297. End If
  298. If TArrayType( ty ) Then
  299. Local s:String = TransIfcType(TArrayType( ty ).elemType) + "&["
  300. For Local i:Int = 0 Until TArrayType( ty ).dims - 1
  301. s:+ ","
  302. Next
  303. Return s + "]" + p
  304. End If
  305. If TObjectType( ty ) Then
  306. Local t:String = ":"
  307. If TObjectType(ty).classDecl.IsExtern() Then
  308. If TObjectType(ty).classDecl.IsInterface() Then
  309. t = "??"
  310. ElseIf TObjectType(ty).classDecl.IsStruct() Then
  311. t = "~~"
  312. Else
  313. t = "?"
  314. End If
  315. End If
  316. Local cdecl:TClassDecl = TObjectType(ty).classDecl
  317. ' find first type in hierarchy that isn't private
  318. While cdecl.IsPrivate() And cdecl.superClass <> Null
  319. cdecl = cdecl.superClass
  320. Wend
  321. Return t + cdecl.ident + p
  322. End If
  323. If TFunctionPtrType( ty ) Return TransIfcType(TFunctionPtrType(ty).func.retType, TFunctionPtrType(ty).func.ModuleScope().IsSuperStrict()) + TransIfcArgs(TFunctionPtrType(ty).func)
  324. If TExternObjectType( ty ) Return ":" + TExternObjectType(ty).classDecl.ident + p
  325. InternalErr
  326. End Method
  327. Method TransRefType$( ty:TType, ident:String )
  328. Return TransType( ty, ident )
  329. End Method
  330. Method TransValue$( ty:TType,value$ )
  331. If value
  332. If IsPointerType(ty, 0, TType.T_POINTER) Return value
  333. If TBoolType( ty ) Return "1"
  334. If TShortType( ty ) Return value
  335. If TIntType( ty ) Return value
  336. If TUIntType( ty ) Return value+"U"
  337. If TLongType( ty ) Return value+"LL"
  338. If TULongType( ty ) Return value+"ULL"
  339. If TSizeTType( ty ) Return value
  340. If TWParamType( ty ) Return value
  341. If TLParamType( ty ) Return value
  342. If TInt128Type( ty ) Return value
  343. If TFloatType( ty ) Then
  344. If value = "nan" Or value = "1.#IND0000" Then
  345. Return "bbPOSNANf"
  346. Else If value="-nan" Or value = "-1.#IND0000" Then
  347. Return "bbNEGNANf"
  348. Else If value = "inf" Or value = "1.#INF0000" Then
  349. Return "bbPOSINFf"
  350. Else If value = "-inf" Or value = "-1.#INF0000" Then
  351. Return "bbNEGINFf"
  352. Else
  353. If value.ToLower().Find("e")>=0 Then
  354. Return value
  355. End If
  356. If value.Find(".") < 0 Then
  357. value :+ ".0"
  358. End If
  359. Return value+"f"
  360. End If
  361. End If
  362. If TDoubleType( ty ) Or TFloat128Type(ty) Or TDouble128Type(ty) Or TFloat64Type(ty) Then
  363. If value = "nan" Or value = "1.#IND0000" Then
  364. Return "bbPOSNANd"
  365. Else If value="-nan" Or value = "-1.#IND0000" Then
  366. Return "bbNEGNANd"
  367. Else If value = "inf" Or value = "1.#INF0000" Then
  368. Return "bbPOSINFd"
  369. Else If value = "-inf" Or value = "-1.#INF0000" Then
  370. Return "bbNEGINFd"
  371. Else
  372. If value.ToLower().Find("e") >=0 Then
  373. Return value
  374. End If
  375. If value.Find(".") < 0 Then
  376. value :+ ".0"
  377. End If
  378. Return value
  379. End If
  380. End If
  381. If TStringType( ty ) Return TransStringConst(value )
  382. If TByteType( ty ) Return value
  383. Else
  384. If TBoolType( ty ) Return "0"
  385. If TIntrinsicType( ty) Then
  386. If IsPointerType(ty, 0, TType.T_POINTER) Then
  387. Return "0"
  388. Else
  389. Return "{}"
  390. End If
  391. End If
  392. If TNumericType( ty ) Return "0" ' numeric and pointers
  393. If TStringType( ty ) Return "&bbEmptyString"
  394. If TArrayType( ty ) Return "&bbEmptyArray"
  395. If TObjectType( ty ) Then
  396. If TObjectType( ty ).classDecl.IsExtern() Or TObjectType( ty ).classDecl.IsStruct() Then
  397. If TObjectType( ty ).classDecl.IsInterface() Or IsPointerType(ty) Or (Not TObjectType( ty ).classDecl.IsStruct()) Then
  398. Return "0"
  399. Else
  400. Return "{}"
  401. End If
  402. Else
  403. Return "&bbNullObject"
  404. End If
  405. End If
  406. If TFunctionPtrType( ty) Return "&brl_blitz_NullFunctionError" ' todo ??
  407. EndIf
  408. InternalErr
  409. End Method
  410. Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null )
  411. 'If decl.ident="AddS" DebugStop
  412. Local t$
  413. 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
  414. t:+ objParam
  415. End If
  416. For Local i:Int=0 Until decl.argDecls.Length
  417. Local ty:TType = TArgDecl(decl.argDecls[i].actual).ty
  418. If t t:+","
  419. If i < args.length
  420. Local arg:TExpr = args[i]
  421. If TNullExpr(arg) Then
  422. t :+ TransValue(ty, Null)
  423. Continue
  424. Else If TIndexExpr(arg) And (ty._flags & TType.T_VAR) Then
  425. t:+ "&"
  426. Else If TStringType(ty) And (ty._flags & TType.T_VAR) Then
  427. If TCastExpr(arg) And TStringType(TCastExpr(arg).expr.exprType) Then
  428. t:+ "&"
  429. End If
  430. Else If TArrayType(ty) And (ty._flags & TType.T_VAR) Then
  431. 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
  432. t:+ "&"
  433. End If
  434. Else If TObjectType(ty) And (ty._flags & TType.T_VAR) Then
  435. If (TVarExpr(arg) Or TMemberVarExpr(arg)) And TObjectType(arg.exprType) And Not (arg.exprType._flags & TType.T_VAR) Then
  436. t:+ "&"
  437. End If
  438. Else If TFunctionPtrType(ty) Or IsPointerType(ty, TType.T_BYTE) Then
  439. If TFunctionPtrType(ty) And (ty._flags & TType.T_VAR) Then
  440. t:+ "&"
  441. End If
  442. If TInvokeExpr(arg) And Not TInvokeExpr(arg).decl.IsMethod() Then
  443. If IsPointerType(ty, TType.T_BYTE) Then
  444. t:+ TInvokeExpr(arg).Trans()
  445. Else
  446. ' need to test scopes to see if we need to use the current instance's function or not
  447. ' use the "actual", not the copy we made for the function pointer.
  448. Local fdecl:TFuncDecl = TFuncDecl(TInvokeExpr(arg).decl.actual)
  449. If Not fdecl.munged Then
  450. MungDecl fdecl
  451. TInvokeExpr(arg).decl.munged = fdecl.munged
  452. End If
  453. If TClassDecl(fdecl.scope) Then
  454. ' current scope is related to function scope?
  455. If _env.ClassScope() And _env.FuncScope() And _env.FuncScope().IsMethod() Then
  456. If _env.ClassScope().ExtendsClass(TClassDecl(fdecl.scope)) Then
  457. Local scope:TScopeDecl = _env.scope
  458. Local obj:String = Bra("struct " + scope.munged + "_obj*")
  459. Local class:String = "o->clas"
  460. t:+ class + "->f_" + fdecl.ident + MangleMethod(fdecl)
  461. Else
  462. t:+ fdecl.munged
  463. End If
  464. Else
  465. t:+ fdecl.munged
  466. End If
  467. Else
  468. t:+ fdecl.munged
  469. End If
  470. End If
  471. Continue
  472. End If
  473. ' some cases where we are passing a function pointer via a void* parameter.
  474. If TCastExpr(arg) And TInvokeExpr(TCastExpr(arg).expr) And Not TInvokeExpr(TCastExpr(arg).expr).invokedWithBraces Then
  475. If Not TInvokeExpr(TCastExpr(arg).expr).decl.munged Then
  476. t:+ TInvokeExpr(TCastExpr(arg).expr).decl.actual.munged
  477. Else
  478. t:+ TInvokeExpr(TCastExpr(arg).expr).decl.munged
  479. End If
  480. Continue
  481. End If
  482. ' Object -> Byte Ptr
  483. If IsPointerType(ty, TType.T_BYTE) And TObjectType(arg.exprType) Then
  484. t:+ Bra(Bra("(BBBYTE*)" + Bra(arg.Trans())) + "+" + Bra("sizeof(void*)"))
  485. Continue
  486. End If
  487. Else If IsNumericType(ty) Then
  488. If TObjectType(arg.exprType) 'And TObjectType(args[i].exprType).classDecl = TClassDecl.nullObjectClass Then
  489. err "NULL"
  490. t:+ "0"
  491. Continue
  492. End If
  493. End If
  494. If decl.argDecls[i].castTo Then
  495. t:+ Bra(decl.argDecls[i].castTo) + arg.Trans()
  496. Else
  497. Local tc:String = TransTemplateCast( ty,arg.exprType,arg.Trans() )
  498. ' *sigh*
  499. ' if var is going to var, remove any leading dereference character.
  500. ' rather hacky. Would be better to cast variable to varptr during semanting (well done if you can work out where!)
  501. If arg.exprType.EqualsType( ty.ActualType() ) And (ty._flags & TType.T_VAR) And (arg.exprType._flags & TType.T_VAR) Then
  502. If tc.startswith("*") Then
  503. tc = tc[1..]
  504. End If
  505. End If
  506. t:+ tc
  507. 't:+TransTemplateCast( ty,args[i].exprType,args[i].Trans() )
  508. End If
  509. Else
  510. decl.argDecls[i].Semant()
  511. ' default values
  512. Local init:TExpr = decl.argDecls[i].init
  513. If init Then
  514. If TConstExpr(init) Then
  515. If TObjectType(TConstExpr(init).exprType) Then
  516. t:+"NULLNULLNULL"
  517. ' And TNullDecl(TObjectType(TConstExpr(init).exprType).classDecl)) Or (TConstExpr(init).value = "bbNullObject") Then
  518. If TStringType(decl.argDecls[i].ty) Then
  519. t :+ "&bbEmptyString"
  520. Else If TArrayType(decl.argDecls[i].ty) Then
  521. t :+ "&bbEmptyArray"
  522. Else
  523. t :+ "&bbNullObject"
  524. End If
  525. Else
  526. t:+ decl.argDecls[i].init.Trans()
  527. End If
  528. Else If TFunctionPtrType(ty) Then
  529. If TInvokeExpr(init) Then
  530. t:+ TInvokeExpr(init).decl.munged
  531. End If
  532. Else
  533. t:+ decl.argDecls[i].init.Trans()
  534. End If
  535. End If
  536. End If
  537. Next
  538. Return Bra(t)
  539. End Method
  540. Method TransArgsTypes$( args:TExpr[],declArgTypes:TType[])
  541. Local t$
  542. For Local i:Int=0 Until args.Length
  543. If t t:+","
  544. t:+TransTemplateCast( declArgTypes[i],args[i].exprType,args[i].Trans() )
  545. Next
  546. Return Bra(t)
  547. End Method
  548. Method TransPtrCast$( ty:TType,src:TType,expr$,cast$ )
  549. If IsPointerType(ty, 0, TType.T_POINTER | TType.T_VARPTR | TType.T_VAR) Or TFunctionPtrType(ty) Then
  550. ' TODO : pointer stuff
  551. If TNullType(src) Return TransValue(ty, Null)
  552. Return expr
  553. End If
  554. 'If expr = "NULL" DebugStop
  555. ' If TIntType(ty) And TStringType(src) Then
  556. 'DebugStop
  557. ' Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(src).cDecl.munged)
  558. ' End If
  559. If TNullType(src)
  560. Return TransValue(ty, Null)
  561. End If
  562. If TStringType(ty) And TObjectType(src) Then
  563. If Not TStringType(ty).cDecl Then
  564. ty.Semant()
  565. End If
  566. 'If TNullDecl(TObjectType(src).classDecl) Then
  567. ' Return "&bbEmptyString"
  568. 'End If
  569. Return Bra("(BBString *)bbObjectDowncast" + Bra(expr + ",&" + TStringType(ty).cDecl.munged))
  570. End If
  571. 'If TArrayType(ty) And TObjectType(src) Then
  572. ' If TNullDecl(TObjectType(src).classDecl) Then
  573. ' Return "&bbEmptyArray"
  574. ' End If
  575. 'End If
  576. If TVarPtrType(src) And TNumericType(ty) Then
  577. Return "*" + expr
  578. End If
  579. If TIntType(ty) And TStringType(src) Then
  580. Return Bra(expr + " != &bbEmptyString")
  581. End If
  582. ' If TIntType(ty) And TObjectType(src) Then
  583. ' Return Bra(expr + " != &bbNullObject")
  584. ' End If
  585. If TObjectType(ty) And TStringType(src) Then
  586. Return expr
  587. End If
  588. If Not TObjectType(ty) Or Not TObjectType(src) Then
  589. DebugStop
  590. InternalErr
  591. End If
  592. Local t$=TransType(ty, "TODO: TransPtrCast")
  593. If src.GetClass().IsInterface() Or ty.GetClass().IsInterface() cast="dynamic"
  594. If src.GetClass().IsInterface() And Not ty.GetClass().IsInterface() Then
  595. Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
  596. End If
  597. 'upcast?
  598. If src.GetClass().ExtendsClass( ty.GetClass() ) Return expr
  599. If TObjectType(ty) Then
  600. Return Bra(Bra(TransObject(TObjectType(ty).classDecl)) + "bbObjectDowncast" + Bra(expr + ",&" + TObjectType(ty).classDecl.munged))
  601. End If
  602. Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
  603. End Method
  604. '***** Utility *****
  605. Method TransLocalDecl$( decl:TLocalDecl,init:TExpr, declare:Int = False, outputInit:Int = True )
  606. Local initTrans:String
  607. If outputInit Then
  608. If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
  609. initTrans = "=" + TInvokeExpr(init).decl.munged
  610. Else
  611. initTrans = "=" + init.Trans()
  612. End If
  613. End If
  614. If Not declare And opt_debug Then
  615. Local ty:TType = decl.ty
  616. If Not TObjectType( ty ) Or (TObjectType( ty ) And Not TObjectType( ty ).classDecl.IsStruct()) Then
  617. If TIntrinsicType(ty) Then
  618. If Not TConstExpr(init) Then
  619. Return decl.munged + initTrans
  620. End If
  621. Else
  622. Return decl.munged + initTrans
  623. End If
  624. Else If TObjectType( ty ) And TObjectType( ty ).classDecl.IsStruct() Then
  625. If Not TConstExpr(init) Then
  626. Return decl.munged + initTrans
  627. End If
  628. End If
  629. Else
  630. If TFunctionPtrType(decl.ty) Then
  631. If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
  632. Return TransType( decl.ty, decl.munged ) + " = " + TInvokeExpr(init).decl.munged
  633. Else
  634. Return TransType( decl.ty, decl.munged ) + initTrans
  635. End If
  636. Else
  637. Local ty:TType = decl.ty
  638. If TVoidType( ty ) Or Not ty Then
  639. ty = init.exprType
  640. End If
  641. If TObjectType(ty) Then
  642. If TObjectType(ty).classdecl.IsExtern() Then
  643. If TObjectType(ty).classdecl.IsInterface() Then
  644. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  645. Else
  646. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  647. End If
  648. Else
  649. If TObjectType(ty).classdecl.IsStruct() Then
  650. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  651. Else
  652. If decl.volatile Then
  653. Return TransType( ty, decl.munged )+" volatile "+decl.munged + initTrans
  654. Else
  655. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  656. End If
  657. End If
  658. End If
  659. Else
  660. Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
  661. End If
  662. End If
  663. End If
  664. End Method
  665. Method TransLocalDeclNoInit$( decl:TVarDecl )
  666. If TFunctionPtrType(decl.ty) Then
  667. Return TransType( decl.ty, decl.munged ) + "=" + TransValue(decl.ty, "")
  668. Else
  669. If TObjectType(decl.ty) Then
  670. If TObjectType(decl.ty).classdecl.IsExtern() Then
  671. If Not TObjectType(decl.ty).classdecl.IsStruct() Then
  672. Return TransType( decl.ty, decl.munged )+" "+decl.munged+"=" + TransValue(decl.ty, "")
  673. Else
  674. Return TransType( decl.ty, decl.munged )+" "+decl.munged
  675. End If
  676. Else
  677. If Not TObjectType(decl.ty).classdecl.IsStruct() Then
  678. If TLocalDecl(decl) And TLocalDecl(decl).volatile Then
  679. Return TransType( decl.ty, decl.munged )+" volatile "+decl.munged + "=" + TransValue(decl.ty, "")
  680. Else
  681. Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
  682. End If
  683. Else
  684. Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
  685. End If
  686. End If
  687. Else
  688. Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
  689. End If
  690. End If
  691. End Method
  692. Method TransGlobalDecl$( gdecl:TGlobalDecl )
  693. Local glob:String
  694. If Not gdecl.funcGlobal Then
  695. If Not (gdecl.attrs & DECL_INITONLY) Then
  696. glob :+"static " + TransType( gdecl.init.exprType, gdecl.munged )+" "
  697. End If
  698. glob :+ gdecl.munged+"="
  699. If (TNewObjectExpr(gdecl.init) Or TNewArrayExpr(gdecl.init)) And Not (gdecl.attrs & DECL_INITONLY) Then
  700. glob :+ "0;~n"
  701. glob :+ indent + "if (" + gdecl.munged + "==0) {~n"
  702. glob :+ indent + "~t" + gdecl.munged + "=" + gdecl.init.Trans() + ";~n"
  703. glob :+ indent + "}"
  704. Else If TArrayExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
  705. glob :+ "0;~n"
  706. Emit glob
  707. Emit "if (" + gdecl.munged + "==0) {"
  708. glob = gdecl.munged + "=" + gdecl.init.Trans() + ";"
  709. Emit glob
  710. Emit "}"
  711. glob = ""
  712. Else
  713. If gdecl.init Then
  714. If TFunctionPtrType(gdecl.ty) Then
  715. If TInvokeExpr(gdecl.init) And Not TInvokeExpr(gdecl.init).invokedWithBraces Then
  716. glob :+ TInvokeExpr(gdecl.init).decl.munged
  717. Else
  718. glob :+ gdecl.init.Trans()
  719. End If
  720. Else If Not TConstExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
  721. ' for non const, we need to add an initialiser
  722. glob :+ TransValue(gdecl.ty, "") + ";~n"
  723. glob :+ indent +"static int _" + gdecl.munged + "_inited = 0;~n"
  724. glob :+ indent + "if (!_" + gdecl.munged + "_inited) {~n"
  725. glob :+ indent + "~t_" + gdecl.munged + "_inited = 1;~n"
  726. glob :+ indent + "~t" + gdecl.munged + " = " + gdecl.init.Trans() + ";~n"
  727. glob :+ indent + "}"
  728. Else
  729. glob :+ gdecl.init.Trans()
  730. End If
  731. Else
  732. If TFunctionPtrType(gdecl.ty) Then
  733. glob :+ "&brl_blitz_NullFunctionError"
  734. Else
  735. glob :+ "0"
  736. End If
  737. End If
  738. End If
  739. Else
  740. glob :+ "static int _" + gdecl.munged + "_inited = 0;~n"
  741. glob :+ indent + "if (!_" + gdecl.munged + "_inited) {~n"
  742. glob :+ indent + "~t_" + gdecl.munged + "_inited = 1;~n"
  743. glob :+ indent + "~t" + gdecl.munged + " = "
  744. If gdecl.init Then
  745. glob :+ gdecl.init.Trans()
  746. Else
  747. glob :+ TransValue(gdecl.ty, "")
  748. End If
  749. glob :+ ";~n"
  750. glob :+ indent + "}"
  751. End If
  752. Return glob
  753. End Method
  754. Method CreateLocal2$( ty:TType, t$ )
  755. Local tmp:TLocalDecl=New TLocalDecl.Create( "", ty,Null, True )
  756. MungDecl tmp
  757. If TShortType(ty) Then
  758. Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToWString" + Bra(t)+ ";"
  759. Else
  760. Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToCString" + Bra(t)+ ";"
  761. End If
  762. customVarStack.Push(tmp.munged)
  763. Return tmp.munged
  764. End Method
  765. Method EmitPushErr()
  766. Emit "pushErr();"
  767. End Method
  768. Method EmitSetErr( info$ )
  769. Emit "errInfo=~q"+info.Replace( "\","/" )+"~q;"
  770. End Method
  771. Method EmitPopErr()
  772. Emit "popErr();"
  773. End Method
  774. '***** Declarations *****
  775. Method TransStatic$( decl:TDecl )
  776. If decl.IsExtern() Then
  777. If Not decl.munged
  778. Return decl.ident
  779. End If
  780. Return decl.munged
  781. Else If _env And decl.scope And decl.scope=_env.ClassScope()
  782. ' calling a class function from a method?
  783. If TFuncDecl(decl) And _env.ClassScope() And _env.FuncScope() And _env.FuncScope().IsMethod() And Not (decl.attrs & FUNC_PTR) Then
  784. Local scope:TScopeDecl = _env.ClassScope()
  785. Local obj:String = Bra("struct " + scope.munged + "_obj*")
  786. Local class:String = "o->clas"
  787. Return class + "->f_" + decl.ident + MangleMethod(TFuncDecl(decl))
  788. Else
  789. Return decl.munged
  790. End If
  791. Else If TClassDecl( decl.scope )
  792. 'Return decl.scope.munged+"::"+decl.munged
  793. Return decl.munged
  794. Else If TModuleDecl( decl.scope )
  795. Return decl.munged
  796. Else If TFuncDecl(decl.scope)
  797. Return decl.munged
  798. Else If TGlobalDecl(decl)
  799. Return decl.munged
  800. Else If TBlockDecl(decl.scope)
  801. Return decl.munged
  802. EndIf
  803. InternalErr
  804. End Method
  805. Method TransTemplateCast$( ty:TType,src:TType,expr$ )
  806. ' *sigh*
  807. ' if var is going to var, remove any leading dereference character.
  808. ' rather hacky. Would be better to cast variable to varptr during semanting (well done if you can work out where!)
  809. 'If src.EqualsType( ty.ActualType() ) And (ty._flags & TType.T_VAR) And (src._flags & TType.T_VAR) Then
  810. ' If expr.startswith("*") Then
  811. ' expr = expr[1..]
  812. ' End If
  813. 'End If
  814. If ty=src Return expr
  815. ty=ty.ActualType()
  816. 'src=src.ActualType()
  817. If src.EqualsType( ty ) Return expr
  818. Return TransPtrCast( ty,src,expr,"static" )
  819. End Method
  820. Method TransGlobal$( decl:TGlobalDecl )
  821. Return TransStatic( decl )
  822. End Method
  823. Method TransField$( decl:TFieldDecl,lhs:TExpr )
  824. If lhs Then
  825. Return TransFieldRef(decl, TransSubExpr( lhs ), lhs.exprType)
  826. Else
  827. Return TransFieldRef(decl, "o", Null)
  828. End If
  829. ' Local swiz$
  830. ' If TObjectType( decl.ty )
  831. ' If TObjectType( decl.ty ).classDecl.IsInterface() swiz=".p"
  832. ' EndIf
  833. ' If lhs Return TransSubExpr( lhs )+"->"+decl.munged+swiz
  834. ' Return decl.munged+swiz
  835. End Method
  836. Method TransFunc$( decl:TFuncDecl,args:TExpr[],lhs:TExpr, sup:Int = False, scope:TScopeDecl = Null )
  837. 'If decl.ident = "eventfilter" DebugStop
  838. ' for calling the super class method instead
  839. Local tSuper:String
  840. If sup Then
  841. tSuper = "->super"
  842. End If
  843. If Not decl.munged
  844. MungDecl decl
  845. End If
  846. 'If decl.IsMethod()
  847. If lhs And Not TSelfExpr(lhs) Then
  848. If TStringType(lhs.exprType) Then
  849. Return decl.munged + TransArgs(args, decl, TransSubExpr( lhs ))
  850. End If
  851. If TStmtExpr(lhs) Then
  852. lhs.Trans()
  853. lhs = TStmtExpr(lhs).expr
  854. End If
  855. If TVarExpr(lhs) Then
  856. Local cdecl:TClassDecl
  857. If TObjectType(TVarExpr(lhs).decl.ty) Then
  858. cdecl = TObjectType(TVarExpr(lhs).decl.ty).classDecl
  859. Else If TArrayType(TVarExpr(lhs).decl.ty) Then
  860. Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  861. End If
  862. If decl.attrs & FUNC_PTR Then
  863. 'Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
  864. Local op:String
  865. If cdecl.IsStruct() Then op = "." Else op = "->"
  866. Return TransSubExpr( lhs ) + op + decl.munged+TransArgs( args,decl, Null)
  867. Else
  868. 'Local lvar:String = CreateLocal(lhs, False)
  869. 'Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  870. If decl.scope.IsExtern()
  871. If Not cdecl.IsStruct() Then
  872. 'Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
  873. Return Bra(TransSubExpr( lhs )) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
  874. 'Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  875. End If
  876. Err "TODO extern types not allowed methods"
  877. Else
  878. If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  879. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + TransSubExpr( lhs ) + ", " + "&" + cdecl.munged + "_ifc)"))
  880. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
  881. ' Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + lvarInit + ", " + "&" + cdecl.munged + "_ifc)"))
  882. ' Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  883. Else
  884. If cdecl.IsStruct() Then
  885. If Not isPointerType(lhs.exprType) Then
  886. Return "_" + decl.munged+TransArgs( args,decl, "&" + TransSubExpr( lhs ) )
  887. Else
  888. Return "_" + decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  889. End If
  890. Else
  891. Local class:String = Bra(TransSubExpr( lhs )) + "->clas" + tSuper
  892. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
  893. ' Local class:String = Bra(lvarInit) + "->clas" + tSuper
  894. ' Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  895. End If
  896. End If
  897. End If
  898. End If
  899. Else If TNewObjectExpr(lhs) Then
  900. Local cdecl:TClassDecl = TNewObjectExpr(lhs).classDecl
  901. If cdecl.IsStruct() Then
  902. ' create a local variable of the inner invocation
  903. Local lvar:String = CreateLocal(lhs)
  904. Return "_" + decl.munged+TransArgs( args,decl, "&" + lvar )
  905. Else
  906. Local class:String = cdecl.munged
  907. Return class + "." + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
  908. End If
  909. Else If TCastExpr(lhs) Then
  910. ' create a local variable of the inner invocation
  911. Local lvar:String = CreateLocal(lhs, False, False)
  912. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  913. Local cdecl:TClassDecl = TObjectType(TCastExpr(lhs).ty).classDecl
  914. Local obj:String = Bra(TransObject(cdecl))
  915. If decl.attrs & FUNC_PTR Then
  916. Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
  917. Else
  918. ' Null test
  919. If opt_debug Then
  920. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  921. End If
  922. If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  923. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + obj + lvarInit + ", " + "&" + cdecl.munged + "_ifc)"))
  924. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  925. Else
  926. Local class:String = Bra("(" + obj + lvarInit + ")->clas" + tSuper)
  927. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  928. End If
  929. End If
  930. Else If TMemberVarExpr(lhs) Then
  931. If TObjectType(TMemberVarExpr(lhs).decl.ty) Then
  932. Local cdecl:TClassDecl = TObjectType(TMemberVarExpr(lhs).decl.ty).classDecl
  933. Local obj:String = Bra(TransObject(cdecl))
  934. If decl.scope.IsExtern()
  935. If TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct() Then
  936. Local lvar:String = CreateLocal(lhs, False, False)
  937. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  938. Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  939. Else
  940. Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
  941. End If
  942. Else
  943. If cdecl.IsStruct() Then
  944. ' baaaaaaaaaaaaaaaaa
  945. If Not isPointerType(lhs.exprType) Then
  946. Return "_" + decl.munged+TransArgs( args,decl, "&" + TransSubExpr( lhs ) )
  947. Else
  948. Return "_" + decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  949. End If
  950. Else
  951. If decl.attrs & FUNC_PTR Then
  952. Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
  953. Else
  954. Local lvar:String = CreateLocal(lhs, False, False)
  955. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  956. ' Null test
  957. If opt_debug Then
  958. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  959. End If
  960. Local class:String = Bra("(" + obj + lvarInit + ")->clas" + tSuper)
  961. Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  962. End If
  963. End If
  964. End If
  965. Else If TArrayType(TMemberVarExpr(lhs).decl.ty) Then
  966. Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  967. End If
  968. Else If TInvokeExpr(lhs) Then
  969. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  970. ' create a local variable of the inner invocation
  971. Local lvar:String = CreateLocal(lhs, True)
  972. If Not isPointerType(lhs.exprType) Then
  973. Return "_" + decl.munged+TransArgs( args,decl, "&" + lvar )
  974. Else
  975. Return "_" + decl.munged+TransArgs( args,decl, lvar)
  976. End If
  977. Else
  978. ' create a local variable of the inner invocation
  979. Local lvar:String = CreateLocal(lhs, False, False)
  980. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  981. ' Null test
  982. If opt_debug Then
  983. Local cdecl:TClassDecl = TClassDecl(decl.scope)
  984. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  985. End If
  986. Local obj:String = Bra(TransObject(decl.scope))
  987. Local class:String = Bra("(" + obj + lvarInit +")->clas" + tSuper)
  988. Return class + "->" + TransFuncPrefix(decl.scope, decl)+ FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  989. End If
  990. 'Local obj:String = Bra("struct " + decl.scope.munged + "_obj*")
  991. 'Local class:String = Bra("(" + obj + TransSubExpr( lhs ) +")->clas" + tSuper)
  992. 'Local class:String = Bra("&" + decl.scope.munged)
  993. 'Return class + "->" + TransFuncPrefix(decl.scope, decl.ident) + decl.ident+TransArgs( args,decl, TransSubExpr( lhs ) )
  994. Else If TInvokeMemberExpr(lhs)
  995. ' create a local variable of the inner invocation
  996. Local lvar:String
  997. Local lvarInit:String
  998. If Not decl.scope.IsExtern() And TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  999. lvar = CreateLocal(lhs, True)
  1000. Else
  1001. lvar = CreateLocal(lhs, False, False)
  1002. lvarInit = Bra(lvar + " = " + lhs.Trans())
  1003. End If
  1004. If decl.scope.IsExtern()
  1005. If TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct() Then
  1006. Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  1007. End If
  1008. Return "// TODO"
  1009. Else
  1010. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  1011. If Not isPointerType(lhs.exprType) Then
  1012. Return "_" + decl.munged+TransArgs( args,decl, "&" + lvar )
  1013. Else
  1014. Return "_" + decl.munged+TransArgs( args,decl, lvar )
  1015. End If
  1016. Else
  1017. Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1018. ' Null test
  1019. If opt_debug Then
  1020. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1021. End If
  1022. If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
  1023. Local obj:String = Bra(TransObject(cdecl))
  1024. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + obj + lvarInit + ", " + "&" + cdecl.munged + "_ifc)"))
  1025. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1026. Else
  1027. Local obj:String = lvarInit + "->clas" + tSuper
  1028. Return obj + "->" + TransFuncPrefix(decl.scope, decl)+ FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1029. End If
  1030. End If
  1031. End If
  1032. Else If TIndexExpr(lhs) Then
  1033. Local lvar:String = CreateLocal(lhs, False, False)
  1034. Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
  1035. ' Local loc:String = CreateLocal(lhs)
  1036. Local obj:String = Bra(TransObject(decl.scope))
  1037. Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1038. ' Null test
  1039. If opt_debug Then
  1040. lvarInit = TransDebugNullObjectError(lvarInit, cdecl)
  1041. End If
  1042. If decl.attrs & FUNC_PTR Then
  1043. Local op:String
  1044. If cdecl.IsStruct() Then op = "." Else op = "->"
  1045. Return lhs.Trans() + op + decl.munged+TransArgs( args,decl, Null)
  1046. Else
  1047. If decl.scope.IsExtern()
  1048. 'Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1049. If Not cdecl.IsStruct() Then
  1050. Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
  1051. End If
  1052. Err "TODO extern types not allowed methods"
  1053. Else
  1054. 'Local cdecl:TClassDecl = TClassDecl(decl.scope)
  1055. If cdecl And (cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl)) Then
  1056. Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + obj + lvarInit + ", " + "&" + cdecl.munged + "_ifc)"))
  1057. Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1058. Else
  1059. Local class:String = Bra(lvarInit + "->clas" + tSuper)
  1060. Return class + "->" + TransFuncPrefix(decl.scope, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
  1061. End If
  1062. End If
  1063. End If
  1064. Else
  1065. InternalErr
  1066. End If
  1067. 'Return TransSubExpr( lhs )+"->"+decl.munged+TransArgs( args,decl )
  1068. 'Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
  1069. End If
  1070. ' ((brl_standardio_TCStandardIO_obj*)o->clas)->md_Read(o, xxx, xxx)
  1071. If decl.IsMethod() Then
  1072. If Not (decl.attrs & FUNC_PTR) Then
  1073. Local class:String
  1074. If Not scope Then
  1075. scope = decl.scope
  1076. If TClassDecl(scope) And Not TClassDecl(scope).IsStruct() Then
  1077. Local obj:String = Bra(TransObject(scope))
  1078. class = "(" + obj + "o)->clas" + tSuper
  1079. ' Null test
  1080. If opt_debug Then
  1081. Emit TransDebugNullObjectError("o", TClassDecl(scope)) + ";"
  1082. End If
  1083. End If
  1084. Else
  1085. class = Bra("&" + scope.munged) + tSuper
  1086. End If
  1087. 'Local obj:String = Bra("struct " + scope.munged + "_obj*")
  1088. 'Local class:String = Bra("(" + obj + "o)->clas" + tSuper)
  1089. 'Local class:String = Bra("&" + decl.scope.munged)
  1090. If TClassDecl(scope).IsStruct() Then
  1091. Return "_" + decl.munged+TransArgs( args,decl, "o" )
  1092. Else
  1093. Return class + "->" + TransFuncPrefix(scope, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, "o" )
  1094. End If
  1095. Else
  1096. ' Null test
  1097. If opt_debug Then
  1098. Emit TransDebugNullObjectError("o", TClassDecl(decl.scope)) + ";"
  1099. End If
  1100. Local obj:String
  1101. If TClassDecl(scope) And Not TClassDecl(scope).IsStruct() Then
  1102. obj = Bra(TransObject(decl.scope))
  1103. End If
  1104. Return Bra(obj + "o") + "->" + decl.munged+TransArgs( args,decl )
  1105. End If
  1106. End If
  1107. Return TransStatic( decl )+TransArgs( args,decl )
  1108. End Method
  1109. Method TransObject:String(decl:TScopeDecl, this:Int = False)
  1110. If decl.ident = "Object"
  1111. Return "BBOBJECT"
  1112. Else
  1113. If TClassDecl(decl) And TClassDecl(decl).IsStruct() Then
  1114. Local t:String = "struct "
  1115. If decl.IsExtern() Then
  1116. t :+ decl.ident
  1117. Else
  1118. t :+ decl.munged
  1119. End If
  1120. If this Then
  1121. Return t + "*"
  1122. Else
  1123. Return t
  1124. End If
  1125. Else
  1126. If decl.IsExtern() Then
  1127. Return "struct " + decl.ident + "*"
  1128. Else
  1129. Return "struct " + decl.munged + "_obj*"
  1130. End If
  1131. End If
  1132. End If
  1133. End Method
  1134. Method TransFuncClass:String(decl:TClassDecl)
  1135. If decl.ident = "Object"
  1136. Return Bra("&bbObjectClass")
  1137. Else
  1138. Return Bra("&" + decl.munged)
  1139. End If
  1140. End Method
  1141. Method TransFuncPrefix:String(decl:TScopeDecl, fdecl:TFuncDecl)
  1142. Local ident:String = fdecl.ident
  1143. If Not decl Or decl.ident = "Object" Or equalsBuiltInFunc(fdecl.ClassScope(), fdecl)
  1144. Return ""
  1145. Else
  1146. If fdecl.IsMethod() Then
  1147. Return "m_"
  1148. Else
  1149. Return "f_"
  1150. End If
  1151. End If
  1152. End Method
  1153. Method TransSuperFunc$( decl:TFuncDecl,args:TExpr[], scope:TScopeDecl )
  1154. Return TransFunc(decl, args, Null, True, scope)
  1155. ' If decl.IsMethod()
  1156. ' Return decl.ClassScope().munged+".md_"+decl.ident+TransArgs( args,decl, "o" )
  1157. ' Else
  1158. ' Return decl.ClassScope().munged+".fn_"+decl.ident+TransArgs( args,decl)
  1159. ' End If
  1160. End Method
  1161. Method TransMinExpr:String(expr:TMinExpr)
  1162. Local s:String
  1163. If TDecimalType(expr.exprType) Then
  1164. s = "bbFloatMin"
  1165. Else If TLongType(expr.exprType) Then
  1166. s = "bbLongMin"
  1167. Else If TSizeTType(expr.exprType) Then
  1168. s = "bbSizetMin"
  1169. Else If TUIntType(expr.exprType) Then
  1170. s = "bbUIntMin"
  1171. Else If TULongType(expr.exprType) Then
  1172. s = "bbULongMin"
  1173. Else
  1174. s = "bbIntMin"
  1175. End If
  1176. Return s + Bra(expr.expr.trans() + "," + expr.expr2.Trans())
  1177. End Method
  1178. Method TransMaxExpr:String(expr:TMaxExpr)
  1179. Local s:String
  1180. If TDecimalType(expr.exprType) Then
  1181. s = "bbFloatMax"
  1182. Else If TLongType(expr.exprType) Then
  1183. s = "bbLongMax"
  1184. Else If TSizeTType(expr.exprType) Then
  1185. s = "bbSizetMax"
  1186. Else If TUIntType(expr.exprType) Then
  1187. s = "bbUIntMax"
  1188. Else If TULongType(expr.exprType) Then
  1189. s = "bbULongMax"
  1190. Else
  1191. s = "bbIntMax"
  1192. End If
  1193. Return s + Bra(expr.expr.trans() + "," + expr.expr2.Trans())
  1194. End Method
  1195. Method TransAscExpr:String(expr:TAscExpr)
  1196. Return "bbStringAsc" + Bra(expr.expr.Trans())
  1197. End Method
  1198. Method TransChrExpr:String(expr:TChrExpr)
  1199. Return "bbStringFromChar" + Bra(expr.expr.Trans())
  1200. End Method
  1201. Method TransSgnExpr:String(expr:TSgnExpr)
  1202. Local s:String
  1203. If TFloatType(expr.expr.exprType) Or TDoubleType(expr.expr.exprType)
  1204. 'decl.ident contains "sgn", same like "bbFloatSng"
  1205. s = "bbFloatSgn"
  1206. Else If TLongType(expr.expr.exprType) Then
  1207. s = "bbLongSgn"
  1208. Else If TSizeTType(expr.expr.exprType) Then
  1209. s = "bbSizetSgn"
  1210. Else If TUIntType(expr.expr.exprType) Then
  1211. s = "bbUIntSgn"
  1212. Else If TULongType(expr.expr.exprType) Then
  1213. s = "bbULongSgn"
  1214. Else
  1215. s = "bbIntSgn"
  1216. End If
  1217. Return s + Bra(expr.expr.Trans())
  1218. End Method
  1219. Method TransAbsExpr:String(expr:TAbsExpr)
  1220. Local s:String
  1221. If TDecimalType(expr.exprType) Then
  1222. s = "bbFloatAbs"
  1223. Else If TLongType(expr.exprType)
  1224. s = "bbLongAbs"
  1225. Else If TSizeTType(expr.exprType)
  1226. s = "bbSizetAbs"
  1227. Else If TUIntType(expr.exprType)
  1228. s = "bbUIntAbs"
  1229. Else If TULongType(expr.exprType)
  1230. s = "bbULongAbs"
  1231. Else
  1232. s = "bbIntAbs"
  1233. End If
  1234. Return s + Bra(expr.expr.Trans())
  1235. End Method
  1236. Method TransLenExpr:String(expr:TLenExpr)
  1237. 'constant strings do not have "->length", so we use the
  1238. 'precalculated value
  1239. If TConstExpr(expr.expr) Then
  1240. If TStringType(expr.expr.exprType) Then
  1241. Return TConstExpr(expr.expr).value.Length
  1242. End If
  1243. End If
  1244. If TStringType(expr.expr.exprType) Then
  1245. Return Bra(expr.expr.Trans()) + "->length"
  1246. Else If TArrayType(expr.expr.exprType) Then
  1247. Return Bra(expr.expr.Trans()) + "->scales[0]"
  1248. Else If TCastExpr(expr.expr) Then
  1249. If TArrayType(TCastExpr(expr.expr).expr.exprType) Then
  1250. Return Bra(TCastExpr(expr.expr).expr.Trans()) + "->scales[0]"
  1251. End If
  1252. 'other types just have a length of "1"
  1253. Else
  1254. Return "1"
  1255. End If
  1256. End Method
  1257. Method TransSizeOfExpr:String(expr:TSizeOfExpr)
  1258. Local cexpr:TConstExpr = TConstExpr(expr.expr)
  1259. If cexpr Then
  1260. If TNumericType(cexpr.exprType) Then
  1261. Return "sizeof" + Bra(TransType(cexpr.exprType, ""))
  1262. ' strings
  1263. Else If TStringType(cexpr.exprType) Then
  1264. ' length of const string * 2 bytes per char
  1265. Return Len(cexpr.value) * 2
  1266. End If
  1267. Else
  1268. If TNumericType(expr.expr.exprType) Then
  1269. ' remove Var-ness first, if any
  1270. Local t:TType = expr.expr.exprType.Copy()
  1271. If t._flags & TType.T_VAR Then
  1272. t._flags :~ TType.T_VAR
  1273. End If
  1274. Return "sizeof" + Bra(TransType(t, ""))
  1275. ' strings
  1276. Else If TStringType(expr.expr.exprType) Then
  1277. 'unicode chars each take 2 bytes
  1278. Return Bra(expr.expr.Trans()) + "->length * 2"
  1279. ' arrays
  1280. Else If TArrayType(expr.expr.exprType) Then
  1281. 'normal exprType is something like "int[]" that
  1282. 'is why it has to be checked against elemType
  1283. Local elemType:TType = TArrayType( expr.expr.exprType ).elemType
  1284. ' numerics - including numeric pointers
  1285. If TNumericType(elemType) Then
  1286. 'multiply element count * size of element type
  1287. Return Bra(expr.expr.Trans()) + "->scales[0] * sizeof" + Bra(TransType(elemType, ""))
  1288. ' everything else : string, array, object, function pointer - are all pointers
  1289. Else
  1290. 'arrays of objects are of size: elementCount * pointerSize
  1291. Return Bra(expr.expr.Trans()) + "->scales[0] * sizeof(void*)"
  1292. EndIf
  1293. ' objects
  1294. Else If TObjectType(expr.expr.exprType) Then
  1295. If TObjectType( expr.expr.exprType ).classDecl.ident = "Object" Then
  1296. Return "0"
  1297. Else
  1298. Local cdecl:TClassDecl = TObjectType( expr.expr.exprType ).classDecl
  1299. If cdecl.IsStruct() Then
  1300. If TIdentTypeExpr(expr.expr) Then
  1301. If cdecl.IsExtern() Then
  1302. Return "sizeof" + Bra("struct " + cdecl.ident)
  1303. Else
  1304. Return "sizeof" + Bra("struct " + cdecl.munged)
  1305. End If
  1306. Else
  1307. Return "sizeof" + Bra(expr.expr.Trans())
  1308. End If
  1309. Else
  1310. If TIdentTypeExpr(expr.expr) Then
  1311. Return Bra(Bra(TransFuncClass(cdecl)) + "->obj_size")
  1312. Else
  1313. Return Bra(Bra(expr.expr.Trans()) + "->clas->obj_size")
  1314. End If
  1315. End If
  1316. End If
  1317. End If
  1318. End If
  1319. InternalErr
  1320. End Method
  1321. '***** Expressions *****
  1322. Method TransConstExpr$( expr:TConstExpr )
  1323. If TStringType(expr.exprType) Then
  1324. Return TransStringConst(expr.value)
  1325. Else
  1326. Return TransValue( expr.exprType,expr.value )
  1327. End If
  1328. End Method
  1329. Field stringMap:TMap = New TMap
  1330. Method TransStringConst:String(value:String)
  1331. If value Then
  1332. _appInstance.mapStringConsts(value)
  1333. End If
  1334. Local sc:TStringConst = TStringConst(_app.stringConsts.ValueForKey(value))
  1335. Local s:String
  1336. If Not sc Then
  1337. s = "bbEmptyString"
  1338. Else
  1339. If Not sc.count Then
  1340. sc.count :+ 1
  1341. End If
  1342. s = sc.id
  1343. End If
  1344. Return "&" + s
  1345. End Method
  1346. Method TransNewObjectExpr$( expr:TNewObjectExpr )
  1347. Local t$
  1348. If Not expr.classDecl.IsStruct() And (Not expr.ctor.argDecls Or expr.ctor.argDecls.length = 0) Then
  1349. If expr.instanceExpr Then
  1350. t = "bbObjectNew(" + Bra(expr.instanceExpr.Trans()) + "->clas)"
  1351. Else
  1352. If ClassHasObjectField(expr.classDecl) Then
  1353. t = "bbObjectNew(&" + expr.classDecl.actual.munged + ")"
  1354. Else
  1355. t = "bbObjectAtomicNew(&" + expr.classDecl.actual.munged + ")"
  1356. End If
  1357. End If
  1358. Else
  1359. Local ctorMunged:String
  1360. If expr.classDecl = expr.ctor.scope Then
  1361. ctorMunged = expr.ctor.munged
  1362. Else
  1363. ctorMunged = expr.classDecl.actual.munged + "_" + expr.ctor.ident + MangleMethod(expr.ctor)
  1364. End If
  1365. If expr.instanceExpr Then
  1366. t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, Bra(expr.instanceExpr.Trans()) + "->clas" )
  1367. Else
  1368. If ClassHasObjectField(expr.classDecl) And Not expr.classDecl.IsStruct() Then
  1369. t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged )
  1370. Else
  1371. If expr.classDecl.IsStruct() Then
  1372. t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor)
  1373. Else
  1374. t = "_" + ctorMunged + "_ObjectNew" + TransArgs( expr.args,expr.ctor, "&" + expr.classDecl.actual.munged)
  1375. End If
  1376. End If
  1377. End If
  1378. End If
  1379. 'Local t$="(new "+expr.classDecl.actual.munged+")"
  1380. 'If expr.ctor t:+"->"+expr.ctor.actual.munged+TransArgs( expr.args,expr.ctor )
  1381. Return t
  1382. End Method
  1383. Method TransNewArrayExpr$( expr:TNewArrayExpr )
  1384. If expr.expr.length = 1 Then
  1385. If TObjectType(expr.ty) And TObjectType(expr.ty).classdecl.IsStruct() And Not IsPointerType(expr.ty) Then
  1386. Return "bbArrayNew1DStruct" + Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans() + ", sizeof" + Bra(TransObject(TObjectType(expr.ty).classdecl)))
  1387. Else
  1388. Return "bbArrayNew1D" + Bra(TransArrayType(expr.ty) + ", " + expr.expr[0].Trans())
  1389. End If
  1390. Else
  1391. ' multiple array
  1392. Local s:String
  1393. For Local i:Int = 0 Until expr.expr.length
  1394. If i Then
  1395. s:+ ", "
  1396. End If
  1397. s:+ expr.expr[i].Trans()
  1398. Next
  1399. Return "bbArrayNew" + Bra(TransArrayType(expr.ty) + ", " + expr.expr.length + ", " + s)
  1400. End If
  1401. End Method
  1402. Method TransSelfExpr$( expr:TSelfExpr )
  1403. Return "o"
  1404. End Method
  1405. Method TransIdentTypeExpr:String(expr:TIdentTypeExpr)
  1406. Return "struct " + expr.cdecl.munged + "_obj"
  1407. End Method
  1408. Method TransCastExpr$( expr:TCastExpr )
  1409. Local t$= expr.expr.Trans()
  1410. Local dst:TType=expr.exprType
  1411. Local src:TType=expr.expr.exprType
  1412. If TNumericType(src) And (src._flags & TType.T_VAR) Then
  1413. ' var number being cast to a varptr
  1414. If (dst._flags & TType.T_VARPTR) Then
  1415. Return "&" + Bra(t)
  1416. End If
  1417. End If
  1418. If (dst._flags & TType.T_VARPTR) Or (dst._flags & TType.T_VAR) Then
  1419. If Not TConstExpr(expr.expr) Then
  1420. If TInvokeExpr(expr.expr) Return t
  1421. If TByteType( src) Return Bra("&"+t)
  1422. If TShortType( src) Return Bra("&"+t)
  1423. If TFloatType( src) Return Bra("&"+t)
  1424. If TIntType( src) Return Bra("&"+t)
  1425. If TUIntType( src) Return Bra("&"+t)
  1426. If TLongType( src) Return Bra("&"+t)
  1427. If TULongType( src) Return Bra("&"+t)
  1428. If TSizeTType( src) Return Bra("&"+t)
  1429. If TDoubleType( src) Return Bra("&"+t)
  1430. If TInt128Type( src) Return Bra("&"+t)
  1431. If TFloat128Type( src) Return Bra("&"+t)
  1432. If TDouble128Type( src) Return Bra("&"+t)
  1433. If TFloat64Type( src) Return Bra("&"+t)
  1434. If TWParamType( src) Return Bra("&"+t)
  1435. If TLParamType( src) Return Bra("&"+t)
  1436. If TObjectType(src) Then
  1437. If TObjectType(src).classDecl.IsExtern() Or (dst._flags & TType.T_VARPTR) Then
  1438. Return Bra("&" + t)
  1439. Else
  1440. If TObjectType(dst) Then
  1441. Return Bra("&" + t)
  1442. Else
  1443. Return Bra(Bra("(BBBYTE*)" + Bra("&" + t)) + "+" + Bra("sizeof(void*)"))
  1444. End If
  1445. End If
  1446. End If
  1447. If TFunctionPtrType(src) Return Bra("&"+t)
  1448. 'If TPointerType( src) Return Bra("&"+t)
  1449. Else
  1450. Return Bra(TransValue(TConstExpr(expr.expr).ty, TConstExpr(expr.expr).value))
  1451. End If
  1452. Else If IsPointerType( dst, 0, TType.T_POINTER | TType.T_CHAR_PTR | TType.T_SHORT_PTR )
  1453. If TArrayType(src) Then
  1454. Return Bra(Bra(TransType(dst, "")) + "BBARRAYDATA(" + t + "," + t + "->dims)")
  1455. End If
  1456. 'If TByteType(src) And Not IsPointerType(src, TType.T_BYTE, TType.T_POINTER) Return Bra("&"+t)
  1457. If TStringType(src) Then
  1458. Local tmp:String
  1459. If IsPointerType( dst, 0, TType.T_SHORT_PTR ) Or IsPointerType( dst, TType.T_SHORT, TType.T_PTR ) Then
  1460. tmp = CreateLocal2(NewPointerType(TType.T_SHORT), t)
  1461. Else
  1462. tmp = CreateLocal2(NewPointerType(TType.T_BYTE), t)
  1463. End If
  1464. Return tmp
  1465. End If
  1466. If (TStringType(dst) And IsPointerType( dst, 0, TType.T_CHAR_PTR | TType.T_SHORT_PTR )) And TNullType(src) Then
  1467. Return "0"
  1468. End If
  1469. If TObjectType(src) Then
  1470. If TObjectType(src).classDecl.IsExtern() Or (src._flags & TType.T_VARPTR) Then
  1471. Return Bra(t)
  1472. Else
  1473. Return Bra(Bra("(BBBYTE*)" + t) + "+" + Bra("sizeof(void*)"))
  1474. End If
  1475. End If
  1476. Local p:String = TransSPointer(dst)
  1477. If TByteType( dst )
  1478. If IsPointerType(src, TType.T_BYTE, TType.T_POINTER & dst._flags) Return t
  1479. If TNumericType( src ) Return Bra("(BBBYTE" + p + ")"+t)
  1480. Else If TShortType( dst )
  1481. If IsPointerType(src, TType.T_SHORT, TType.T_POINTER & dst._flags) Return t
  1482. If TNumericType( src ) Return Bra("(BBSHORT" + p + ")"+t)
  1483. Else If TIntType( dst )
  1484. If IsPointerType(src, TType.T_INT, TType.T_POINTER & dst._flags) Return t
  1485. If TNumericType( src ) Return Bra("(BBINT" + p + ")"+t)
  1486. Else If TUIntType( dst )
  1487. If IsPointerType(src, TType.T_UINT, TType.T_POINTER & dst._flags) Return t
  1488. If TNumericType( src ) Return Bra("(BBUINT" + p + ")"+t)
  1489. Else If TFloatType( dst )
  1490. If IsPointerType(src, TType.T_FLOAT, TType.T_POINTER & dst._flags) Return t
  1491. If TNumericType( src ) Return Bra("(BBFLOAT" + p + ")"+t)
  1492. Else If TDoubleType( dst )
  1493. If IsPointerType(src, TType.T_DOUBLE, TType.T_POINTER & dst._flags) Return t
  1494. If TNumericType( src ) Return Bra("(BBDOUBLE" + p + ")"+t)
  1495. Else If TLongType( dst )
  1496. If IsPointerType(src, TType.T_LONG, TType.T_POINTER & dst._flags) Return t
  1497. If TNumericType( src ) Return Bra("(BBLONG" + p + ")"+t)
  1498. Else If TULongType( dst )
  1499. If IsPointerType(src, TType.T_ULONG, TType.T_POINTER & dst._flags) Return t
  1500. If TNumericType( src ) Return Bra("(BBULONG" + p + ")"+t)
  1501. Else If TSizeTType( dst )
  1502. If IsPointerType(src, TType.T_SIZET, TType.T_POINTER & dst._flags) Return t
  1503. If TNumericType( src ) Return Bra("(BBSIZET" + p + ")"+t)
  1504. Else If TWParamType( dst )
  1505. If IsPointerType(src, TType.T_WPARAM, TType.T_POINTER & dst._flags) Return t
  1506. If TNumericType( src ) Return Bra("(WPARAM" + p + ")"+t)
  1507. Else If TLParamType( dst )
  1508. If IsPointerType(src, TType.T_LPARAM, TType.T_POINTER & dst._flags) Return t
  1509. If TNumericType( src ) Return Bra("(LPARAM" + p + ")"+t)
  1510. Else If TInt128Type( dst )
  1511. If IsPointerType(src, TType.T_INT128, TType.T_POINTER & dst._flags) Return t
  1512. If TNumericType( src ) Return Bra("(BBINT128" + p + ")"+t)
  1513. Else If TFloat128Type( dst )
  1514. If IsPointerType(src, TType.T_FLOAT128, TType.T_POINTER & dst._flags) Return t
  1515. If TNumericType( src ) Return Bra("(BBFLOAT128" + p + ")"+t)
  1516. Else If TDouble128Type( dst )
  1517. If IsPointerType(src, TType.T_DOUBLE128, TType.T_POINTER & dst._flags) Return t
  1518. If TNumericType( src ) Return Bra("(BBDOUBLE128" + p + ")"+t)
  1519. Else If TFloat64Type( dst )
  1520. If IsPointerType(src, TType.T_FLOAT64, TType.T_POINTER & dst._flags) Return t
  1521. If TNumericType( src ) Return Bra("(BBFLOAT64" + p + ")"+t)
  1522. 'Else If TIntPtrPtrType( dst )
  1523. ' If TBytePtrType( src) Return Bra("(BBINT**)"+t)
  1524. ' If TShortPtrType( src ) Return Bra("(BBINT**)"+t)
  1525. ' If TIntPtrType( src ) Return Bra("(BBINT**)"+t)
  1526. ' If TFloatPtrType( src ) Return Bra("(BBINT**)"+t)
  1527. ' If TDoublePtrType( src ) Return Bra("(BBINT**)"+t)
  1528. ' If TLongPtrType( src ) Return Bra("(BBINT**)"+t)
  1529. ' If TNumericType( src ) Return Bra("(BBINT**)"+t)
  1530. End If
  1531. Else If TBoolType( dst )
  1532. If TFunctionPtrType(src) Return Bra(Bra( t+"!=0" ) + " && " + Bra( t+"!=&brl_blitz_NullFunctionError" ))
  1533. 'If TFunctionPtrType(src) Return Bra( t+"!=0" )
  1534. If IsPointerType( src, 0, TType.T_POINTER ) Return Bra( t )
  1535. If TBoolType( src ) Return t
  1536. If TByteType( src ) Return Bra( t+"!=0" )
  1537. If TShortType( src ) Return Bra( t+"!=0" )
  1538. If TIntType( src ) Return Bra( t+"!=0" )
  1539. If TUIntType( src ) Return Bra( t+"!=0" )
  1540. If TFloatType( src ) Return Bra( t+"!=0.0f" )
  1541. 'If TCastExpr(expr.expr) And (TArrayType( src ) Or TStringType( src ) Or TObjectType( src )) Then
  1542. ' Return Bra( t+"!= &bbNullObject" )
  1543. 'End If
  1544. If TLongType( src ) Return Bra( t+"!=0" )
  1545. If TULongType( src ) Return Bra( t+"!=0" )
  1546. If TSizeTType( src ) Return Bra( t+"!=0" )
  1547. If TWParamType( src ) Return Bra( t+"!=0" )
  1548. If TLParamType( src ) Return Bra( t+"!=0" )
  1549. If TDoubleType( src ) Return Bra( t+"!=0.0f" )
  1550. If TArrayType( src ) Return Bra( t+"!= &bbEmptyArray" )
  1551. If TStringType( src ) Return Bra( t+"!= &bbEmptyString" )
  1552. If TObjectType( src ) Then
  1553. If TObjectType(src).classDecl.IsExtern() Then
  1554. If Not TObjectType(src).classDecl.IsStruct() Then
  1555. Return Bra( t+"!=0" )
  1556. Else
  1557. Return Bra("1")
  1558. End If
  1559. Else
  1560. Return Bra( t+"!= &bbNullObject" )
  1561. End If
  1562. End If
  1563. Else If TIntType( dst )
  1564. If TBoolType( src ) Return Bra( t )
  1565. If TByteType( src) Return Bra("(BBINT)"+t)
  1566. If TShortType( src) Return Bra("(BBINT)"+t)
  1567. If TBoolType( src ) Return t
  1568. If TIntType( src ) Return t
  1569. If TUIntType( src ) Return Bra("(BBINT)"+t)
  1570. If TFloatType( src ) Return Bra("(BBINT)"+t)
  1571. If TDoubleType( src ) Return Bra("(BBINT)"+t)
  1572. If TLongType( src ) Return Bra("(BBINT)"+t)
  1573. If TULongType( src ) Return Bra("(BBINT)"+t)
  1574. If TSizeTType( src ) Return Bra("(BBINT)"+t)
  1575. If TWParamType( src ) Return Bra("(BBINT)"+t)
  1576. If TLParamType( src ) Return Bra("(BBINT)"+t)
  1577. If TStringType( src ) Return "bbStringToInt" + Bra(t)
  1578. 'If TIntVarPtrType( src ) Return Bra("*" + t)
  1579. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBINT)"+t)
  1580. 'If TPointerType( src ) Return Bra("(BBINT)"+t)
  1581. Else If TLongType( dst )
  1582. If TBoolType( src ) Return Bra( t )
  1583. If TByteType( src) Return Bra("(BBLONG)"+t)
  1584. If TShortType( src) Return Bra("(BBLONG)"+t)
  1585. If TIntType( src) Return Bra("(BBLONG)"+t)
  1586. If TUIntType( src) Return Bra("(BBLONG)"+t)
  1587. If TLongType( src ) Return t
  1588. If TULongType( src ) Return Bra("(BBLONG)"+t)
  1589. If TSizeTType( src ) Return Bra("(BBLONG)"+t)
  1590. If TWParamType( src ) Return Bra("(BBLONG)"+t)
  1591. If TLParamType( src ) Return Bra("(BBLONG)"+t)
  1592. If TFloatType( src ) Return Bra("(BBLONG)"+t)
  1593. If TDoubleType( src ) Return Bra("(BBLONG)"+t)
  1594. If TStringType( src ) Return "bbStringToLong" + Bra(t)
  1595. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBLONG)"+t)
  1596. If TFloat64Type( src ) Return Bra("(BBLONG)"+t)
  1597. 'If TPointerType( src ) Return Bra("(BBLONG)"+t)
  1598. Else If TSizeTType( dst )
  1599. If TBoolType( src ) Return Bra( t )
  1600. If TByteType( src) Return Bra("(BBSIZET)"+t)
  1601. If TShortType( src) Return Bra("(BBSIZET)"+t)
  1602. If TIntType( src) Return Bra("(BBSIZET)"+t)
  1603. If TUIntType( src) Return Bra("(BBSIZET)"+t)
  1604. If TLongType( src) Return Bra("(BBSIZET)"+t)
  1605. If TULongType( src) Return Bra("(BBSIZET)"+t)
  1606. If TSizeTType( src ) Return t
  1607. If TWParamType( src ) Return Bra("(BBSIZET)"+t)
  1608. If TLParamType( src ) Return Bra("(BBSIZET)"+t)
  1609. If TFloatType( src ) Return Bra("(BBSIZET)"+t)
  1610. If TDoubleType( src ) Return Bra("(BBSIZET)"+t)
  1611. If TStringType( src ) Return "bbStringToSizet" + Bra(t)
  1612. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(BBSIZET)"+t)
  1613. If TFloat64Type( src ) Return Bra("(BBSIZET)"+t)
  1614. 'If TPointerType( src ) Return Bra("(BBLONG)"+t)
  1615. Else If TFloatType( dst )
  1616. If TBoolType( src ) Return Bra( t )
  1617. If TByteType( src ) Return Bra("(BBFLOAT)"+t)
  1618. If TIntType( src ) Return Bra("(BBFLOAT)"+t)
  1619. If TUIntType( src ) Return Bra("(BBFLOAT)"+t)
  1620. If TShortType( src ) Return Bra("(BBFLOAT)"+t)
  1621. If TFloatType( src ) Return t
  1622. If TDoubleType( src ) Return Bra("(BBFLOAT)"+t)
  1623. If TLongType( src ) Return Bra("(BBFLOAT)"+t)
  1624. If TULongType( src ) Return Bra("(BBFLOAT)"+t)
  1625. If TSizeTType( src ) Return Bra("(BBFLOAT)"+t)
  1626. If TWParamType( src ) Return Bra("(BBFLOAT)"+t)
  1627. If TLParamType( src ) Return Bra("(BBFLOAT)"+t)
  1628. If TStringType( src ) Return "bbStringToFloat" + Bra(t)
  1629. 'If TFloatVarPtrType( src ) Return Bra("*" + t)
  1630. 'If TPointerType( src ) Return Bra("(BBFLOAT)"+t)
  1631. Else If TDoubleType( dst )
  1632. If TBoolType( src ) Return Bra( t )
  1633. If TByteType( src ) Return Bra("(BBDOUBLE)"+t)
  1634. If TIntType( src ) Return Bra("(BBDOUBLE)"+t)
  1635. If TUIntType( src ) Return Bra("(BBDOUBLE)"+t)
  1636. If TShortType( src ) Return Bra("(BBDOUBLE)"+t)
  1637. If TDoubleType( src ) Return t
  1638. If TFloatType( src ) Return Bra("(BBDOUBLE)"+t)
  1639. If TLongType( src ) Return Bra("(BBDOUBLE)"+t)
  1640. If TULongType( src ) Return Bra("(BBDOUBLE)"+t)
  1641. If TSizeTType( src ) Return Bra("(BBDOUBLE)"+t)
  1642. If TWParamType( src ) Return Bra("(BBDOUBLE)"+t)
  1643. If TLParamType( src ) Return Bra("(BBDOUBLE)"+t)
  1644. If TStringType( src ) Return "bbStringToDouble" + Bra(t)
  1645. 'If TDoubleVarPtrType( src ) Return Bra("*" + t)
  1646. 'If TPointerType( src ) Return Bra("(BBDOUBLE)"+t)
  1647. Else If TStringType( dst )
  1648. If IsPointerType(src, 0, TType.T_POINTER) Return "bbStringFromSizet"+Bra( t )
  1649. If TBoolType( src ) Return "bbStringFromInt"+Bra( t )
  1650. If TByteType( src ) Return "bbStringFromInt"+Bra( t )
  1651. If TShortType( src ) Return "bbStringFromInt"+Bra( t )
  1652. If TIntType( src ) Return "bbStringFromInt"+Bra( t )
  1653. If TUIntType( src ) Return "bbStringFromUInt"+Bra( t )
  1654. If TLongType( src ) Return "bbStringFromLong"+Bra( t )
  1655. If TULongType( src ) Return "bbStringFromULong"+Bra( t )
  1656. If TSizeTType( src ) Return "bbStringFromSizet"+Bra( t )
  1657. If TWParamType( src ) Return "bbStringFromWParam"+Bra( t )
  1658. If TLParamType( src ) Return "bbStringFromLParam"+Bra( t )
  1659. If TFloatType( src ) Return "bbStringFromFloat"+Bra( t )
  1660. If TDoubleType( src ) Return "bbStringFromDouble"+Bra( t )
  1661. If TStringType( src ) Then
  1662. If src._flags & TType.T_CHAR_PTR Then
  1663. Return "bbStringFromCString"+Bra( t )
  1664. End If
  1665. If src._flags & TType.T_SHORT_PTR Then
  1666. Return "bbStringFromWString"+Bra( t )
  1667. End If
  1668. If src._flags & TType.T_VAR Then
  1669. If TSliceExpr( expr.expr ) Then
  1670. Return "&" + Bra(t)
  1671. End If
  1672. Return t
  1673. End If
  1674. Return t
  1675. End If
  1676. 'If TStringVarPtrType( src ) Then
  1677. ' If TSliceExpr( expr.expr ) Then
  1678. ' Return t
  1679. ' End If
  1680. ' Return "*" + t
  1681. 'End If
  1682. 'If TStringCharPtrType( src ) Return "bbStringFromCString"+Bra( t )
  1683. 'Else If TStringVarPtrType( dst )
  1684. 'DebugStop
  1685. Else If TByteType( dst )
  1686. If TBoolType( src ) Return Bra( t )
  1687. If TByteType( src) Return t
  1688. If TShortType( src ) Return Bra("(BBBYTE)"+t)
  1689. If TIntType( src ) Return Bra("(BBBYTE)"+t)
  1690. If TUIntType( src ) Return Bra("(BBBYTE)"+t)
  1691. If TFloatType( src ) Return Bra("(BBBYTE)"+t)
  1692. If TDoubleType( src ) Return Bra("(BBBYTE)"+t)
  1693. If TLongType( src ) Return Bra("(BBBYTE)"+t)
  1694. If TULongType( src ) Return Bra("(BBBYTE)"+t)
  1695. If TSizeTType( src ) Return Bra("(BBBYTE)"+t)
  1696. If TWParamType( src ) Return Bra("(BBBYTE)"+t)
  1697. If TLParamType( src ) Return Bra("(BBBYTE)"+t)
  1698. If TStringType( src ) Return "bbStringToInt" + Bra(t)
  1699. 'If TByteVarPtrType( src ) Return Bra("*" + t)
  1700. Else If TShortType( dst )
  1701. If TBoolType( src ) Return Bra( t )
  1702. If TShortType( src) Return t
  1703. If TByteType( src) Return Bra("(BBSHORT)"+t)
  1704. If TIntType( src ) Return Bra("(BBSHORT)"+t)
  1705. If TUIntType( src ) Return Bra("(BBSHORT)"+t)
  1706. If TFloatType( src ) Return Bra("(BBSHORT)"+t)
  1707. If TDoubleType( src ) Return Bra("(BBSHORT)"+t)
  1708. If TLongType( src ) Return Bra("(BBSHORT)"+t)
  1709. If TULongType( src ) Return Bra("(BBSHORT)"+t)
  1710. If TSizeTType( src ) Return Bra("(BBSHORT)"+t)
  1711. If TWParamType( src ) Return Bra("(BBSHORT)"+t)
  1712. If TLParamType( src ) Return Bra("(BBSHORT)"+t)
  1713. If TStringType( src ) Return "bbStringToInt" + Bra(t)
  1714. 'If TShortVarPtrType( src ) Return Bra("*" + t)
  1715. Else If TUIntType( dst )
  1716. If TBoolType( src ) Return Bra( t )
  1717. If TShortType( src ) Return Bra("(BBUINT)"+t)
  1718. If TByteType( src) Return Bra("(BBUINT)"+t)
  1719. If TIntType( src ) Return Bra("(BBUINT)"+t)
  1720. If TUIntType( src) Return t
  1721. If TFloatType( src ) Return Bra("(BBUINT)"+t)
  1722. If TDoubleType( src ) Return Bra("(BBUINT)"+t)
  1723. If TLongType( src ) Return Bra("(BBUINT)"+t)
  1724. If TULongType( src ) Return Bra("(BBUINT)"+t)
  1725. If TSizeTType( src ) Return Bra("(BBUINT)"+t)
  1726. If TWParamType( src ) Return Bra("(BBUINT)"+t)
  1727. If TLParamType( src ) Return Bra("(BBUINT)"+t)
  1728. If TStringType( src ) Return "bbStringToUInt" + Bra(t)
  1729. Else If TULongType( dst )
  1730. If TBoolType( src ) Return Bra( t )
  1731. If TShortType( src ) Return Bra("(BBULONG)"+t)
  1732. If TByteType( src) Return Bra("(BBULONG)"+t)
  1733. If TIntType( src ) Return Bra("(BBULONG)"+t)
  1734. If TUIntType( src ) Return Bra("(BBULONG)"+t)
  1735. If TFloatType( src ) Return Bra("(BBULONG)"+t)
  1736. If TDoubleType( src ) Return Bra("(BBULONG)"+t)
  1737. If TLongType( src ) Return Bra("(BBULONG)"+t)
  1738. If TULongType( src) Return t
  1739. If TSizeTType( src ) Return Bra("(BBULONG)"+t)
  1740. If TWParamType( src ) Return Bra("(BBULONG)"+t)
  1741. If TLParamType( src ) Return Bra("(BBULONG)"+t)
  1742. If TStringType( src ) Return "bbStringToULong" + Bra(t)
  1743. If TFloat64Type( src ) Return Bra("(BBULONG)"+t)
  1744. Else If TFloat64Type( dst )
  1745. If TFloat64Type( src) Return t
  1746. If TLongType( src ) Return Bra("(BBFLOAT64)"+t)
  1747. If TULongType( src ) Return Bra("(BBFLOAT64)"+t)
  1748. If TSizeTType( src ) Return Bra("(BBFLOAT64)"+t)
  1749. Else If TInt128Type( dst )
  1750. If TInt128Type( src) Return t
  1751. If TFloat128Type( src ) Return Bra("(BBINT128)"+t)
  1752. If TDouble128Type( src ) Return Bra("(BBINT128)"+t)
  1753. Else If TFloat128Type( dst )
  1754. If TFloat128Type( src) Return t
  1755. If TInt128Type( src ) Return Bra("(BBFLOAT128)"+t)
  1756. If TDouble128Type( src ) Return Bra("(BBFLOAT128)"+t)
  1757. Else If TDouble128Type( dst )
  1758. If TDouble128Type( src) Return t
  1759. If TInt128Type( src ) Return Bra("(BBDOUBLE128)"+t)
  1760. If TFloat128Type( src ) Return Bra("(BBDOUBLE128)"+t)
  1761. Else If TWParamType( dst )
  1762. If TBoolType( src ) Return Bra( t )
  1763. If TByteType( src) Return Bra("(WPARAM)"+t)
  1764. If TShortType( src) Return Bra("(WPARAM)"+t)
  1765. If TIntType( src) Return Bra("(WPARAM)"+t)
  1766. If TUIntType( src) Return Bra("(WPARAM)"+t)
  1767. If TLongType( src) Return Bra("(WPARAM)"+t)
  1768. If TULongType( src) Return Bra("(WPARAM)"+t)
  1769. If TSizeTType( src ) Return Bra("(WPARAM)"+t)
  1770. If TWParamType( src ) Return t
  1771. If TLParamType( src ) Return Bra("(WPARAM)"+t)
  1772. If TFloatType( src ) Return Bra("(WPARAM)"+t)
  1773. If TDoubleType( src ) Return Bra("(WPARAM)"+t)
  1774. If TStringType( src ) Return "bbStringToWParam" + Bra(t)
  1775. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(WPARAM)"+t)
  1776. Else If TLParamType( dst )
  1777. If TBoolType( src ) Return Bra( t )
  1778. If TByteType( src) Return Bra("(LPARAM)"+t)
  1779. If TShortType( src) Return Bra("(LPARAM)"+t)
  1780. If TIntType( src) Return Bra("(LPARAM)"+t)
  1781. If TUIntType( src) Return Bra("(LPARAM)"+t)
  1782. If TLongType( src) Return Bra("(LPARAM)"+t)
  1783. If TULongType( src) Return Bra("(LPARAM)"+t)
  1784. If TSizeTType( src ) Return Bra("(LPARAM)"+t)
  1785. If TWParamType( src ) Return Bra("(LPARAM)"+t)
  1786. If TLParamType( src ) Return t
  1787. If TFloatType( src ) Return Bra("(LPARAM)"+t)
  1788. If TDoubleType( src ) Return Bra("(LPARAM)"+t)
  1789. If TStringType( src ) Return "bbStringToLParam" + Bra(t)
  1790. If IsPointerType(src,0,TType.T_POINTER) Return Bra("(LPARAM)"+t)
  1791. Else If TArrayType( dst )
  1792. If TArrayType( src ) Then
  1793. If TObjectType( TArrayType( dst ).elemType ) And TObjectType( TArrayType( dst ).elemType ).classDecl.ident = "Object" Then
  1794. ' if we are casting to Object[], don't actually cast.
  1795. Return Bra(t)
  1796. Else
  1797. Return "bbArrayCastFromObject" + Bra(t + "," + TransArrayType(TArrayType( dst ).elemType))
  1798. End If
  1799. End If
  1800. If TObjectType( src) And (TObjectType( src ).classDecl.ident = "___Array" Or TObjectType( src ).classDecl.ident = "Object") Then
  1801. Return "bbArrayCastFromObject" + Bra(t + "," + TransArrayType(TArrayType( dst ).elemType))
  1802. End If
  1803. Else If TObjectType( dst )
  1804. 'If TArrayType( src ) Return Bra("(BBOBJECT)"+t)
  1805. 'If TStringType( src ) Return Bra("(BBOBJECT)"+t)
  1806. 'If TObjectType( src ) Return t
  1807. If Not TObjectType( dst ).classDecl.IsExtern() Then
  1808. If TNullType( src ) Return "&bbNullObject"
  1809. If TObjectType(dst).classDecl.IsInterface() Then
  1810. Return Bra(Bra(TransObject(TObjectType(dst).classDecl)) + "bbInterfaceDowncast" + Bra(t + ",&" + TObjectType(dst).classDecl.munged + "_ifc"))
  1811. Else
  1812. ' no need to downcast to BBObject, as all objects extend it...
  1813. If TObjectType( dst ).classDecl.ident = "Object" Then
  1814. Return t
  1815. Else
  1816. Return Bra(Bra(TransObject(TObjectType(dst).classDecl)) + "bbObjectDowncast" + Bra(t + ",&" + TObjectType(dst).classDecl.munged))
  1817. End If
  1818. End If
  1819. Else
  1820. If TObjectType( dst ).classDecl.IsInterface() Then
  1821. Return t
  1822. Else
  1823. Return "" ' TODO??
  1824. End If
  1825. End If
  1826. End If
  1827. Return TransPtrCast( dst,src,t,"dynamic" )
  1828. Err "C++ translator can't convert "+src.ToString()+" to "+dst.ToString()
  1829. End Method
  1830. Method TransUnaryExpr$( expr:TUnaryExpr )
  1831. Local pri:Int=ExprPri( expr )
  1832. Local t_expr$
  1833. If TVarExpr(expr.expr) Then
  1834. If TObjectType(TVarExpr(expr.expr).exprType) Then
  1835. t_expr = Bra( expr.expr.Trans() + "!= &bbNullObject")
  1836. Else If TStringType(TVarExpr(expr.expr).exprType) Then
  1837. t_expr = Bra( expr.expr.Trans() + "!= &bbEmptyString")
  1838. Else
  1839. t_expr = TransSubExpr( expr.expr,pri )
  1840. End If
  1841. Else
  1842. t_expr = TransSubExpr( expr.expr,pri )
  1843. End If
  1844. ' TransSubExpr( expr.expr,pri )
  1845. Return TransUnaryOp( expr.op )+t_expr
  1846. End Method
  1847. Method TransBinaryExpr$( expr:TBinaryExpr )
  1848. Local pri:Int=ExprPri( expr )
  1849. Local t_lhs$=TransSubExpr( expr.lhs,pri )
  1850. ' If TVarPtrType(expr.lhs.exprType) Then
  1851. ' t_lhs = "*" + t_lhs
  1852. ' End If
  1853. Local t_rhs$=TransSubExpr( expr.rhs,pri-1 )
  1854. ' If TVarPtrType(expr.rhs.exprType) Then
  1855. ' t_rhs = "*" + t_rhs
  1856. ' End If
  1857. If expr.op = "+" Then
  1858. If TStringType(expr.exprType) Then
  1859. Return "bbStringConcat(" + t_lhs + "," + t_rhs + ")"
  1860. Else If TArrayType(expr.exprType) Then
  1861. Return "bbArrayConcat(" + TransArrayType(TArrayType(expr.lhs.exprType).elemType) + "," + t_lhs + "," + t_rhs + ")"
  1862. End If
  1863. End If
  1864. If expr.op = "^" Then
  1865. Return "pow" + Bra(t_lhs + ", " + t_rhs)
  1866. End If
  1867. If expr.op = "mod" Or expr.op = "%" Then
  1868. If TDecimalType(expr.lhs.exprType) Or TDecimalType(expr.rhs.exprType) Then
  1869. Return "fmod" + Bra(t_lhs + ", " + t_rhs)
  1870. End If
  1871. End If
  1872. If (expr.op = "shr" Or expr.op = "&" Or expr.op = "|") And TIntType(expr.exprType) Then
  1873. t_lhs = "(unsigned int)(" + t_lhs + ")"
  1874. End If
  1875. If TBinaryCompareExpr(expr) Then
  1876. If TStringType(TBinaryCompareExpr(expr).ty) Then
  1877. If t_lhs="&bbNullObject" Then
  1878. err "NULL"
  1879. t_lhs = "&bbEmptyString"
  1880. End If
  1881. If t_rhs="&bbNullObject" Then
  1882. err "NULL"
  1883. t_rhs = "&bbEmptyString"
  1884. End If
  1885. If t_lhs <> "&bbEmptyString" And t_rhs <> "&bbEmptyString" Then
  1886. Return "bbStringCompare" + Bra(t_lhs + ", " + t_rhs) + TransBinaryOp(expr.op, "") + "0"
  1887. End If
  1888. End If
  1889. If IsPointerType(TBinaryCompareExpr(expr).ty, 0, TType.T_POINTER) Then
  1890. If t_lhs="&bbNullObject" Then
  1891. t_lhs = "0"
  1892. End If
  1893. If t_rhs="&bbNullObject" Then
  1894. t_rhs = "0"
  1895. End If
  1896. End If
  1897. If TArrayType(TBinaryCompareExpr(expr).ty) Then
  1898. If t_lhs="&bbNullObject" Then
  1899. err "NULL"
  1900. t_lhs = "&bbEmptyArray"
  1901. End If
  1902. If t_rhs="&bbNullObject" Then
  1903. err "NULL"
  1904. t_rhs = "&bbEmptyArray"
  1905. End If
  1906. End If
  1907. End If
  1908. Return bra(t_lhs+TransBinaryOp( expr.op,t_rhs )+t_rhs)
  1909. End Method
  1910. Method TransIndexExpr$( expr:TIndexExpr )
  1911. Local t_expr$=TransSubExpr( expr.expr )
  1912. Local t_index$
  1913. If expr.index.length = 1 Then
  1914. If TArraySizeExpr(expr.index[0]) Then
  1915. Local sizes:TArraySizeExpr = TArraySizeExpr(expr.index[0])
  1916. sizes.Trans()
  1917. Local v:String = sizes.val.munged
  1918. Local i:Int = 0
  1919. For i = 0 Until sizes.index.length - 1
  1920. If i Then
  1921. t_index :+ " + "
  1922. End If
  1923. t_index :+ "(*(" + v
  1924. If i Then
  1925. t_index :+ "+" + i
  1926. End If
  1927. t_index :+ ")) * " + sizes.index[i].Trans()
  1928. Next
  1929. t_index :+ " + " + sizes.index[i].Trans()
  1930. ' (*(v+0)) * var1 + (*(v+1)) * var2 + var3
  1931. 'DebugStop
  1932. Else
  1933. t_index=expr.index[0].Trans()
  1934. End If
  1935. End If
  1936. If TStringType( expr.expr.exprType ) Then
  1937. Return Bra(t_expr) + "->buf[" + t_index + "]"
  1938. 'Return "(BBINT)"+t_expr+"["+t_index+"]"
  1939. End If
  1940. If TArrayType( expr.expr.exprType ) Then
  1941. If TFunctionPtrType(TArrayType( expr.expr.exprType ).elemType) Then
  1942. If opt_debug Then
  1943. Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATAINDEX(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims," + t_index + ")")) + "[" + t_index + "]"
  1944. Else
  1945. Return Bra(Bra(TransType(TArrayType( expr.expr.exprType).elemType, "*")) + Bra("BBARRAYDATA(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims)")) + "[" + t_index + "]"
  1946. End If
  1947. Else
  1948. If opt_debug Then
  1949. Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATAINDEX(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims," + t_index + ")") + "[" + t_index + "]"
  1950. Else
  1951. Return Bra("(" + TransType(expr.exprType, "") + "*)BBARRAYDATA(" + Bra(t_expr) + "," + Bra(t_expr) + "->dims)") + "[" + t_index + "]"
  1952. End If
  1953. End If
  1954. End If
  1955. 'Local swiz$
  1956. 'If TObjectType( expr.exprType )And expr.exprType.GetClass().IsInterface() swiz=".p"
  1957. 'If ENV_CONFIG="debug" Return t_expr+".At("+t_index+")"+swiz
  1958. Return t_expr+"["+t_index+"]"
  1959. End Method
  1960. Method TransSliceExpr$( expr:TSliceExpr )
  1961. 'DebugStop
  1962. Local t_expr:String=TransSubExpr( expr.expr )
  1963. Local t_args$
  1964. If expr.from Then
  1965. t_args=expr.from.Trans()
  1966. Else
  1967. t_args = "0"
  1968. End If
  1969. If expr.term Then
  1970. t_args:+","+expr.term.Trans()
  1971. Else
  1972. If TArrayType(expr.exprType) Then
  1973. t_args :+ "," + Bra(t_expr) + "->scales[0]"
  1974. 'Else If TStringVarPtrType(expr.exprType) Then
  1975. ' t_args :+ ",(*" + t_expr + ")->length"
  1976. Else
  1977. t_args :+ "," + Bra(t_expr) + "->length"
  1978. End If
  1979. End If
  1980. If TArrayType(expr.exprType) Then
  1981. Return "bbArraySlice" + Bra(TransArrayType(TArrayType(expr.exprType).elemType) + "," + t_expr + "," + t_args)
  1982. 'Else If TStringVarPtrType(expr.exprType) Then
  1983. ' Return "bbStringSlice" + Bra("*" + t_expr + "," + t_args)
  1984. Else
  1985. Return "bbStringSlice" + Bra(t_expr + "," + t_args)
  1986. End If
  1987. 'Return t_expr+".Slice("+t_args+")"
  1988. End Method
  1989. Method TransArrayExpr$( expr:TArrayExpr )
  1990. Local elemType:TType=TArrayType( expr.exprType ).elemType
  1991. Local tmpData:TLocalDecl =New TLocalDecl.Create( "",TType.voidType,Null )
  1992. MungDecl tmpData
  1993. Local tmpArray:TLocalDecl =New TLocalDecl.Create( "",TType.voidType,Null )
  1994. MungDecl tmpArray
  1995. Local t$
  1996. Local count:Int
  1997. For Local elem:TExpr=EachIn expr.exprs
  1998. If t t:+","
  1999. t:+elem.Trans()
  2000. count :+ 1
  2001. Next
  2002. Local tt$
  2003. ' If Not _env tt="static "
  2004. If Not TFunctionPtrType(elemType) Then
  2005. tt :+ TransType( elemType, tmpData.munged ) + " "+tmpData.munged + "[]"
  2006. Else
  2007. tt :+ TransType( elemType, tmpData.munged + "[]" )
  2008. End If
  2009. Emit tt+"={"+t+"};"
  2010. Emit "BBARRAY " + tmpArray.munged + " = bbArrayFromData" + Bra(TransArrayType(elemType) + "," + count + "," + tmpData.munged ) + ";"
  2011. Return tmpArray.munged
  2012. 'Return "bbArrayFromData" + Bra(TransArrayType(elemType) + "," + count + "," + tmp.munged )
  2013. 'Return "Array<"+TransRefType( elemType, "MM" )+" >("+tmp.munged+","+expr.exprs.Length+")"
  2014. End Method
  2015. Method TransArraySizeExpr$ ( expr:TArraySizeExpr )
  2016. ' scales[0] is the total size of the array
  2017. ' we start from [1] because it is the size of the next full dimension.
  2018. ' in the case of a 2-dimensional array, [1] represents the length of a row.
  2019. Return Bra("(BBARRAY)" + expr.expr.Trans()) + "->scales + 1"
  2020. End Method
  2021. Method TransIntrinsicExpr$( decl:TDecl,expr:TExpr,args:TExpr[] )
  2022. Local texpr$,arg0$,arg1$,arg2$
  2023. If expr texpr=TransSubExpr( expr )
  2024. If args.Length>0 And args[0] arg0=args[0].Trans()
  2025. If args.Length>1 And args[1] arg1=args[1].Trans()
  2026. If args.Length>2 And args[2] arg2=args[2].Trans()
  2027. Local id$=decl.munged[1..]
  2028. Local id2$=id[..1].ToUpper()+id[1..]
  2029. Select id
  2030. '
  2031. 'global functions
  2032. Case "print" Return "Print"+Bra( arg0 )
  2033. Case "error" Return "Error"+Bra( arg0 )
  2034. '
  2035. 'string/array methods
  2036. Case "length" Return texpr+".Length()"
  2037. Case "resize" Return texpr+".Resize"+Bra( arg0 )
  2038. 'string methods
  2039. Case "compare" Return texpr+".Compare"+Bra( arg0 )
  2040. Case "find" Return texpr+".Find"+Bra( arg0+","+arg1 )
  2041. Case "findlast" Return texpr+".FindLast"+Bra( arg0 )
  2042. Case "findlast2" Return texpr+".FindLast"+Bra( arg0+","+arg1 )
  2043. Case "trim" Return texpr+".Trim()"
  2044. Case "join" Return texpr+".Join"+Bra( arg0 )
  2045. Case "split" Return texpr+".Split"+Bra( arg0 )
  2046. Case "replace" Return texpr+".Replace"+Bra( arg0+","+arg1 )
  2047. Case "tolower" Return texpr+".ToLower()"
  2048. Case "toupper" Return texpr+".ToUpper()"
  2049. Case "contains" Return texpr+".Contains"+Bra( arg0 )
  2050. Case "startswith" Return texpr+".StartsWith"+Bra( arg0 )
  2051. Case "endswith" Return texpr+".EndsWith"+Bra( arg0 )
  2052. 'string functions
  2053. Case "fromchar" Return "String"+Bra( "(Char)"+Bra(arg0)+",1" )
  2054. 'math methods
  2055. Case "sin","cos","tan" Return "(float)"+id+Bra( Bra(arg0)+"*D2R" )
  2056. Case "asin","acos","atan" Return "(float)"+Bra( id+Bra(arg0)+"*R2D" )
  2057. Case "atan2" Return "(float)"+Bra( id+Bra(arg0+","+arg1)+"*R2D" )
  2058. Case "sqrt","floor","ceil","log" Return "(float)"+id+Bra( arg0 )
  2059. Case "pow" Return "(float)"+id+Bra( arg0+","+arg1 )
  2060. '
  2061. End Select
  2062. InternalErr
  2063. End Method
  2064. '***** Statements *****
  2065. Method TransTryStmt$( stmt:TTryStmt )
  2066. Emit "do {"
  2067. EmitLocalDeclarations(stmt.block)
  2068. Emit "jmp_buf * buf = bbExEnter();"
  2069. Emit "switch(setjmp(*buf)) {"
  2070. Emit "case 0: {"
  2071. If opt_debug Then
  2072. Emit "bbOnDebugPushExState();"
  2073. End If
  2074. PushLoopTryStack(stmt)
  2075. tryStack.Push(stmt.block)
  2076. EmitBlock( stmt.block )
  2077. tryStack.Pop()
  2078. PopLoopTryStack
  2079. Emit "bbExLeave();"
  2080. If opt_debug Then
  2081. Emit "bbOnDebugPopExState();"
  2082. End If
  2083. Emit "}"
  2084. Emit "break;"
  2085. Emit "case 1:"
  2086. Emit "{"
  2087. If opt_debug Then
  2088. Emit "bbOnDebugPopExState();"
  2089. End If
  2090. Emit "BBOBJECT ex = bbExObject();"
  2091. Local s:String = ""
  2092. For Local c:TCatchStmt=EachIn stmt.catches
  2093. MungDecl c.init
  2094. If TStringType(c.init.ty) Then
  2095. Emit s + "if (bbObjectDowncast(ex,&bbStringClass) != &bbEmptyString) {"
  2096. Emit TransType( c.init.ty, c.init.munged )+" "+ c.init.munged + "=(BBSTRING)ex;"
  2097. Else If TArrayType(c.init.ty) Then
  2098. Emit s + "if (bbObjectDowncast(ex,&bbArrayClass) != &bbEmptyArray) {"
  2099. Emit TransType( c.init.ty, c.init.munged )+" "+ c.init.munged + "=(BBARRAY)ex;"
  2100. Else If TObjectType(c.init.ty) Then
  2101. Emit s + "if (bbObjectDowncast(ex,&"+TObjectType(c.init.ty).classDecl.munged+") != &bbNullObject) {"
  2102. Emit TransType( c.init.ty, c.init.munged )+" "+ c.init.munged + "=" + Bra(TransType( c.init.ty, c.init.munged )) + "ex;"
  2103. Else
  2104. Err "Not an object"
  2105. End If
  2106. EmitLocalDeclarations(c.block, c.init)
  2107. EmitBlock( c.block )
  2108. s = "} else "
  2109. Next
  2110. If s Then
  2111. Emit s + " {"
  2112. ' unhandled exception
  2113. Emit "bbExThrow(ex);"
  2114. Emit "}"
  2115. Else
  2116. ' unhandled exception
  2117. Emit "bbExThrow(ex);"
  2118. End If
  2119. Emit "}"
  2120. Emit "break;"
  2121. Emit "}"
  2122. Emit "} while(0);"
  2123. End Method
  2124. Method EmitTryStack()
  2125. For Local i:Int = 0 Until tryStack.Length()
  2126. Emit "bbExLeave();"
  2127. If opt_debug Then
  2128. Emit "bbOnDebugPopExState();"
  2129. End If
  2130. Next
  2131. End Method
  2132. Method EmitLocalScopeStack()
  2133. For Local i:Int = 0 Until localScope.Length()
  2134. Emit "// TODO"
  2135. Next
  2136. End Method
  2137. Method EmitDebugEnterScope(block:TBlockDecl)
  2138. Local count:Int
  2139. For Local decl:TDecl = EachIn block.Decls()
  2140. If TLocalDecl(decl) Then
  2141. count :+ 1
  2142. End If
  2143. If TConstDecl(decl) Then
  2144. count :+ 1
  2145. End If
  2146. If TGlobalDecl(decl) Then
  2147. count :+ 1
  2148. End If
  2149. Next
  2150. ' a method also includes "Self" reference back to parent Type
  2151. If TFuncDecl(block) And TFuncDecl(block).IsMethod() Then
  2152. count :+ 1
  2153. End If
  2154. If _app.mainFunc = block Then
  2155. For Local decl:TDecl = EachIn _app.mainModule.Decls()
  2156. If TConstDecl(decl) Then
  2157. count :+ 1
  2158. End If
  2159. If TGlobalDecl(decl) Then
  2160. count :+ 1
  2161. End If
  2162. Next
  2163. End If
  2164. If Not count Then
  2165. Emit "struct BBDebugScope __scope = {"
  2166. Else
  2167. Emit "struct BBDebugScope_" + count + " __scope = {"
  2168. _app.scopeDefs.Insert(String(count), "")
  2169. End If
  2170. If TFuncDecl(block) Then
  2171. Emit "BBDEBUGSCOPE_FUNCTION,"
  2172. If _app.mainFunc = block Then
  2173. ' use the filename as the base function name
  2174. Emit Enquote(StripExt(StripDir(_app.mainModule.filepath))) + ","
  2175. Else
  2176. Emit Enquote(TFuncDecl(block).ident) + ","
  2177. End If
  2178. Else
  2179. Emit "BBDEBUGSCOPE_LOCALBLOCK,"
  2180. Emit "0,"
  2181. End If
  2182. Emit "{"
  2183. If TFuncDecl(block) And TFuncDecl(block).IsMethod() Then
  2184. Emit "{"
  2185. Emit "BBDEBUGDECL_LOCAL,"
  2186. Emit "~qSelf~q,"
  2187. Emit Enquote(TransDebugScopeType(TClassDecl(block.scope).objectType)) + ","
  2188. Emit ".var_address=&o"
  2189. Emit "},"
  2190. End If
  2191. ' block consts and globals
  2192. ' consts
  2193. For Local cdecl:TConstDecl = EachIn block.Decls()
  2194. EmitConstDebugScope(cdecl)
  2195. Next
  2196. ' globals
  2197. For Local gdecl:TGlobalDecl = EachIn block.Decls()
  2198. EmitGlobalDebugScope(gdecl)
  2199. Next
  2200. ' iterate through decls and add as appropriate
  2201. For Local decl:TDecl = EachIn block.Decls()
  2202. Local ldecl:TLocalDecl = TLocalDecl(decl)
  2203. If ldecl Then
  2204. Emit "{"
  2205. If ldecl.ty._flags & TType.T_VAR Then
  2206. Emit "BBDEBUGDECL_VARPARAM,"
  2207. Else
  2208. Emit "BBDEBUGDECL_LOCAL,"
  2209. End If
  2210. Emit Enquote(ldecl.ident) + ","
  2211. Emit Enquote(TransDebugScopeType(ldecl.ty)) + ","
  2212. Emit ".var_address=&" + ldecl.munged
  2213. Emit "},"
  2214. Continue
  2215. End If
  2216. Next
  2217. ' add module consts and globals
  2218. If _app.mainFunc = block Then
  2219. ' consts
  2220. For Local cdecl:TConstDecl = EachIn _app.mainModule.Decls()
  2221. EmitConstDebugScope(cdecl)
  2222. Next
  2223. ' globals
  2224. For Local gdecl:TGlobalDecl = EachIn _app.mainModule.Decls()
  2225. EmitGlobalDebugScope(gdecl)
  2226. Next
  2227. End If
  2228. Emit "BBDEBUGDECL_END "
  2229. Emit "}"
  2230. Emit "};"
  2231. Emit "bbOnDebugEnterScope(&__scope);"
  2232. End Method
  2233. Method TransDebugNullObjectError:String(variable:String, cdecl:TClassDecl)
  2234. If cdecl.IsStruct() Or cdecl.ident = "String" Or cdecl.ident = "___Array" Then
  2235. 'Return cdecl.munged + "NullObjectTest(" + variable + ")"
  2236. Return variable
  2237. Else
  2238. Return Bra(Bra(TransObject(cdecl)) + "bbNullObjectTest(" + variable + ")")
  2239. End If
  2240. End Method
  2241. Method TransAssignStmt$( stmt:TAssignStmt )
  2242. If Not stmt.rhs Return stmt.lhs.Trans()
  2243. Local rhs$=stmt.rhs.Trans()
  2244. Local lhs$=stmt.lhs.TransVar()
  2245. Local s:String
  2246. If IsPointerType(stmt.lhs.exprType, TType.T_BYTE) And rhs = "&bbNullObject" Then
  2247. rhs = "0"
  2248. End If
  2249. If stmt.op = ":%" Then
  2250. If TDecimalType(stmt.lhs.exprType) Or TDecimalType(stmt.rhs.exprType) Then
  2251. Return lhs + "=fmod" + Bra(lhs + "," + rhs)
  2252. End If
  2253. End If
  2254. If TStringType(stmt.lhs.exprType) 'Or TStringVarPtrType(stmt.lhs.exprType) Then
  2255. ' s:+ "{"
  2256. ' s:+ "BBSTRING tmp=" + lhs + ";~n"
  2257. If stmt.op = ":+" Then
  2258. s :+ lhs+"=bbStringConcat("+lhs+","+rhs+")"
  2259. Else If rhs = "&bbNullObject" Then
  2260. s :+ lhs+TransAssignOp( stmt.op )+"&bbEmptyString"
  2261. Else
  2262. s :+ lhs+TransAssignOp( stmt.op )+rhs
  2263. End If
  2264. ' s :+ ";~nBBRETAIN(" + lhs +")~n"
  2265. ' s :+ "BBRELEASE(tmp)~n"
  2266. ' s:+ "}"
  2267. Else If TVarPtrType(stmt.lhs.exprType) Then
  2268. If TCastExpr(stmt.rhs) And IsNumericType(TCastExpr(stmt.rhs).expr.exprType) Then
  2269. rhs = TCastExpr(stmt.rhs).expr.Trans()
  2270. End If
  2271. s :+ lhs+TransAssignOp( stmt.op )+rhs
  2272. Else If TArrayType(stmt.lhs.exprType) Then
  2273. If stmt.op = ":+" Then
  2274. s :+ lhs+"=bbArrayConcat("+ TransArrayType(TArrayType(stmt.lhs.exprType).elemType) + "," + lhs+","+rhs+")"
  2275. Else If rhs = "&bbNullObject" Then
  2276. s :+ lhs+TransAssignOp( stmt.op )+"&bbEmptyArray"
  2277. Else
  2278. s :+ lhs+TransAssignOp( stmt.op )+rhs
  2279. End If
  2280. 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
  2281. rhs = TInvokeExpr(stmt.rhs).decl.munged
  2282. s :+ lhs+TransAssignOp( stmt.op )+rhs
  2283. Else
  2284. s :+ lhs+TransAssignOp( stmt.op )+rhs
  2285. End If
  2286. If DEBUG Then
  2287. DebugObject(stmt.lhs.exprType, lhs, Null, True)
  2288. End If
  2289. Return s
  2290. End Method
  2291. Method TransThrowStmt:String( stmt:TThrowStmt )
  2292. Local s:String = "bbExThrow("
  2293. s:+ stmt.expr.Trans()
  2294. s :+ ")"
  2295. Return s
  2296. End Method
  2297. Method TransAssertStmt$( stmt:TAssertStmt )
  2298. If opt_debug Then
  2299. Emit "if (!" + Bra(stmt.expr.Trans()) + ") {"
  2300. Emit "brl_blitz_RuntimeError(" + stmt.elseExpr.Trans() + ");"
  2301. Emit "}"
  2302. End If
  2303. End Method
  2304. Method TransEndStmt$( stmt:TEndStmt )
  2305. Emit "bbEnd();"
  2306. End Method
  2307. Method TransReleaseStmt$( stmt:TReleaseStmt )
  2308. Emit "bbHandleRelease" + Bra(stmt.expr.Trans()) + ";"
  2309. End Method
  2310. Method TransRestoreDataStmt$( stmt:TRestoreDataStmt )
  2311. Emit "_defDataOffset = &_defData[" + TDataLabelExpr(stmt.expr).dataDef.label.index + "];"
  2312. End Method
  2313. Method TransReadDataStmt$( stmt:TReadDataStmt )
  2314. For Local expr:TExpr = EachIn stmt.args
  2315. ' buffer overflow test
  2316. If opt_debug Then
  2317. Emit "if (_defDataOffset - _defData >= " + TDefDataDecl.count + ") brl_blitz_OutOfDataError();"
  2318. End If
  2319. Emit expr.Trans() + " = " + TransDefDataConversion(expr.exprType) + Bra("_defDataOffset++") + ";"
  2320. Next
  2321. End Method
  2322. Method TransNativeStmt$( stmt:TNativeStmt)
  2323. Emit stmt.raw
  2324. End Method
  2325. Method TransFullName:String(decl:TDecl)
  2326. Local s:String
  2327. If decl.scope Then
  2328. s:+ TransFullName(decl.scope)
  2329. End If
  2330. If s Then
  2331. s :+ " : "
  2332. End If
  2333. If TModuleDecl(decl) Then
  2334. s:+ decl.ModuleScope().munged
  2335. Else
  2336. s :+ decl.ident
  2337. End If
  2338. If TFuncDecl(decl) Then
  2339. s:+ "()"
  2340. End If
  2341. Return s
  2342. End Method
  2343. Method ClassHasObjectField:Int(classDecl:TClassDecl)
  2344. If classDecl.superClass Then
  2345. If ClassHasObjectField(classDecl.superClass) Then
  2346. Return True
  2347. End If
  2348. End If
  2349. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  2350. If Not decl.IsSemanted() Then
  2351. decl.Semant()
  2352. End If
  2353. If TStringType(decl.ty) Or TArrayType(decl.ty) Or (TObjectType(decl.ty) And Not TObjectType(decl.ty).classDecl.IsStruct()) Then
  2354. Return True
  2355. End If
  2356. Next
  2357. Return False
  2358. End Method
  2359. '***** Declarations *****
  2360. Rem
  2361. Method EmitFuncProto( decl:TFuncDecl )
  2362. PushMungScope
  2363. decl.Semant
  2364. MungDecl decl
  2365. 'Find decl we override
  2366. Local odecl:TFuncDecl=decl
  2367. While odecl.overrides
  2368. odecl=odecl.overrides
  2369. Wend
  2370. Local args$
  2371. For Local arg:TArgDecl=EachIn odecl.argDecls
  2372. If args args:+","
  2373. args:+TransType( arg.ty )
  2374. Next
  2375. Local t$=TransType( odecl.retType )+" "+decl.munged+Bra( args )
  2376. If decl.IsAbstract() t:+"=0"
  2377. Local q$
  2378. If decl.IsExtern() q:+"extern "
  2379. If decl.IsMethod() q:+"virtual "
  2380. If decl.IsStatic() And decl.ClassScope() q:+"static "
  2381. Emit q+t+";"
  2382. PopMungScope
  2383. End Method
  2384. End Rem
  2385. Method EmitBBClassFuncProto( decl:TFuncDecl)
  2386. 'PushMungScope
  2387. BeginLocalScope
  2388. 'DebugStop
  2389. ' decl.Semant
  2390. ' MungDecl decl
  2391. 'Find decl we override
  2392. Local odecl:TFuncDecl=decl
  2393. 'If odecl.overrides And Not odecl.returnTypeSubclassed Then Return
  2394. 'DebugLog decl.ident
  2395. ' While odecl.overrides
  2396. ' odecl=odecl.overrides
  2397. ' Wend
  2398. Local id$=decl.munged
  2399. Local pre:String
  2400. If decl.IsMethod() Then
  2401. id :+ "_m"
  2402. pre = "m_"
  2403. Else
  2404. id :+ "_f"
  2405. pre = "f_"
  2406. End If
  2407. Local bk:String = ";"
  2408. 'Local pre:String = "typedef "
  2409. 'If odecl.IsExtern() Then
  2410. ' pre = "extern "
  2411. 'End If
  2412. 'DebugLog "id = " + id
  2413. Emit id + " " + pre + FuncDeclMangleIdent(odecl) + ";"
  2414. ' If Not proto Or (proto And Not odecl.IsExtern()) Then
  2415. Rem
  2416. If Not TFunctionPtrType(odecl.retType) Then
  2417. If Not odecl.castTo Then
  2418. Emit pre + TransType( odecl.retType, "" )+" "+ Bra("*" + id)+Bra( args ) + bk
  2419. Else
  2420. If Not odecl.noCastGen Then
  2421. Emit pre + odecl.castTo +" "+Bra("*" + id)+Bra( args ) + bk
  2422. End If
  2423. End If
  2424. Else
  2425. If Not odecl.castTo Then
  2426. Emit pre + TransType( odecl.retType, id )+" "+Bra( args ) + bk
  2427. Else
  2428. If Not odecl.noCastGen Then
  2429. Emit pre + odecl.castTo +" "+Bra( args ) + bk
  2430. End If
  2431. End If
  2432. End If
  2433. For Local t$=EachIn argCasts
  2434. Emit t
  2435. Next
  2436. ' End If
  2437. End Rem
  2438. 'PopMungScope
  2439. EndLocalScope
  2440. End Method
  2441. Method FuncDeclMangleIdent:String(fdecl:TFuncDecl)
  2442. If (Not fdecl.ClassScope()) Or (equalsBuiltInFunc(fdecl.classScope(), fdecl)) Then
  2443. Return fdecl.ident
  2444. End If
  2445. If Not fdecl.mangled Then
  2446. Local id:String = fdecl.ident
  2447. If fdecl.attrs & FUNC_OPERATOR Then
  2448. id = MungSymbol(id)
  2449. End If
  2450. fdecl.mangled = id + MangleMethod(fdecl)
  2451. End If
  2452. Return fdecl.mangled
  2453. ' If fdecl.olIndex Then
  2454. ' Return fdecl.ident + fdecl.olIndex
  2455. ' Else
  2456. ' Return fdecl.ident
  2457. ' End If
  2458. End Method
  2459. Method EmitClassFuncProto( decl:TFuncDecl, isStruct:Int = False)
  2460. 'PushMungScope
  2461. BeginLocalScope
  2462. decl.Semant
  2463. MungDecl decl
  2464. 'Find decl we override
  2465. Local odecl:TFuncDecl=decl
  2466. ' If odecl.overrides Then Return
  2467. While odecl.overrides
  2468. odecl=odecl.overrides
  2469. Wend
  2470. 'Generate 'args' string and arg casts
  2471. Local args$
  2472. ' pass object for method
  2473. If decl.IsMethod() Then
  2474. args :+ TransObject(decl.scope, True)
  2475. End If
  2476. Local argCasts:TStack =New TStack
  2477. For Local i:Int=0 Until decl.argDecls.Length
  2478. Local arg:TArgDecl=decl.argDecls[i]
  2479. Local oarg:TArgDecl=odecl.argDecls[i]
  2480. MungDecl arg
  2481. If args args:+","
  2482. If Not TFunctionPtrType(oarg.ty) Then
  2483. If Not odecl.castTo Then
  2484. args:+TransType( oarg.ty, arg.munged )
  2485. Else
  2486. args:+ oarg.castTo + " " + arg.munged
  2487. End If
  2488. Else
  2489. If Not odecl.castTo Then
  2490. args:+TransType( oarg.ty, arg.munged )
  2491. Else
  2492. args:+ oarg.castTo
  2493. End If
  2494. End If
  2495. If arg.ty.EqualsType( oarg.ty ) Continue
  2496. Local t$=arg.munged
  2497. arg.munged=""
  2498. MungDecl arg
  2499. argCasts.Push TransType( arg.ty, arg.munged )+" "+arg.munged+"=static_cast<"+TransType(arg.ty, "")+" >"+Bra(t)+";"
  2500. Next
  2501. Local id$=decl.munged
  2502. Local bk:String = ";"
  2503. Local pre:String = "typedef "
  2504. Local api:String
  2505. If decl.IsMethod() Then
  2506. id :+ "_m"
  2507. Else
  2508. id :+ "_f"
  2509. End If
  2510. If decl.attrs & DECL_API_WIN32 Then
  2511. api = " __stdcall "
  2512. End If
  2513. 'If odecl.IsExtern() Then
  2514. ' pre = "extern "
  2515. 'End If
  2516. ' If Not proto Or (proto And Not odecl.IsExtern()) Then
  2517. If Not TFunctionPtrType(decl.retType) Then
  2518. If Not odecl.castTo Then
  2519. If Not isStruct Then
  2520. If Not decl.overrides Or decl.returnTypeSubclassed Then
  2521. Emit pre + TransType( decl.retType, "" )+" "+ Bra(api + "*" + id)+Bra( args ) + bk
  2522. End If
  2523. End If
  2524. If decl.IsMethod() Then
  2525. Emit TransType(decl.retType, "") + " _" + decl.munged +Bra( args ) + bk
  2526. Else
  2527. Emit TransType(decl.retType, "") + api + " " + decl.munged +Bra( args ) + bk
  2528. End If
  2529. Else
  2530. If Not odecl.noCastGen Then
  2531. If Not isStruct Then
  2532. If Not decl.overrides Or decl.returnTypeSubclassed Then
  2533. Emit pre + odecl.castTo +" "+Bra(api + "*" + id)+Bra( args ) + bk
  2534. End If
  2535. End If
  2536. If decl.IsMethod() Then
  2537. Emit odecl.castTo + " _" + decl.munged +Bra( args ) + bk
  2538. Else
  2539. Emit odecl.castTo + " " + decl.munged +Bra( args ) + bk
  2540. End If
  2541. End If
  2542. End If
  2543. Else
  2544. If Not odecl.castTo Then
  2545. If Not args Then
  2546. ' for function pointer return type, we need to generate () regardless of whether there are
  2547. ' args or not.
  2548. args = " "
  2549. End If
  2550. ' emit function ptr typedef
  2551. Emit pre + TransType( decl.retType, id + "x") + bk
  2552. ' emit actual typedef (with return type of above typedef)
  2553. Emit pre + TransType( decl.retType, id, args, True ) + bk
  2554. Else
  2555. If Not odecl.noCastGen Then
  2556. Emit pre + odecl.castTo +" "+Bra( args ) + bk
  2557. End If
  2558. End If
  2559. End If
  2560. For Local t$=EachIn argCasts
  2561. Emit t
  2562. Next
  2563. ' End If
  2564. 'PopMungScope
  2565. EndLocalScope
  2566. End Method
  2567. Method EmitFuncDecl( decl:TFuncDecl, proto:Int = False, classFunc:Int = False )
  2568. 'If Not proto And decl.IsAbstract() Return
  2569. Local tmpDebug:Int = opt_debug
  2570. If decl.isNoDebug() Then
  2571. opt_debug = False
  2572. End If
  2573. 'PushMungScope
  2574. BeginLocalScope
  2575. 'DebugStop
  2576. decl.Semant
  2577. MungDecl decl
  2578. ' emit nested functions
  2579. If Not proto Then
  2580. ' emit nested protos
  2581. For Local fdecl:TFuncDecl = EachIn decl._decls
  2582. EmitFuncDecl(fdecl, True, classFunc)
  2583. Next
  2584. ' emit nested bodies
  2585. For Local fdecl:TFuncDecl = EachIn decl._decls
  2586. EmitFuncDecl(fdecl, proto, classFunc)
  2587. Next
  2588. End If
  2589. 'Find decl we override
  2590. Local odecl:TFuncDecl=decl
  2591. While odecl.overrides
  2592. odecl=odecl.overrides
  2593. Wend
  2594. 'Generate 'args' string and arg casts
  2595. Local args$
  2596. ' pass object for method
  2597. If decl.IsMethod() Then
  2598. args :+ TransObject(decl.scope, True) + " o"
  2599. End If
  2600. Local argCasts:TStack =New TStack
  2601. For Local i:Int=0 Until decl.argDecls.Length
  2602. Local arg:TArgDecl=decl.argDecls[i]
  2603. Local oarg:TArgDecl=odecl.argDecls[i]
  2604. MungDecl arg, True
  2605. If args args:+","
  2606. If Not TFunctionPtrType(oarg.ty) Then
  2607. If Not odecl.castTo Then
  2608. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  2609. Else
  2610. args:+ oarg.castTo + " " + arg.munged
  2611. End If
  2612. Else
  2613. If Not odecl.castTo Then
  2614. args:+TransType( oarg.ty, arg.munged )
  2615. Else
  2616. args:+ oarg.castTo
  2617. End If
  2618. End If
  2619. If arg.ty.EqualsType( oarg.ty ) Continue
  2620. Local t$=arg.munged
  2621. arg.munged=""
  2622. MungDecl arg
  2623. argCasts.Push TransType( arg.ty, arg.munged )+" "+arg.munged+"=static_cast<"+TransType(arg.ty, "")+" >"+Bra(t)+";"
  2624. Next
  2625. Local id$=decl.munged
  2626. If classFunc Then
  2627. If decl.IsMethod() Then
  2628. id = "_" + id
  2629. End If
  2630. Else
  2631. If Not odecl.IsExtern() Then
  2632. id = id
  2633. End If
  2634. End If
  2635. Local bk:String = "{"
  2636. Local pre:String
  2637. Local api:String
  2638. If proto Then
  2639. If odecl.IsExtern() Then
  2640. pre = "extern "
  2641. If TFunctionPtrType(decl.retType) Then
  2642. pre = ""
  2643. End If
  2644. End If
  2645. bk = ";"
  2646. End If
  2647. If decl.attrs & DECL_API_WIN32 Then
  2648. api = " __stdcall "
  2649. End If
  2650. ' If Not proto Or (proto And Not odecl.IsExtern()) Then
  2651. If Not IsStandardFunc(decl.munged) Then
  2652. If Not TFunctionPtrType(odecl.retType) Then
  2653. If Not odecl.castTo Then
  2654. Emit pre + TransType( decl.retType, "" )+ api + " "+id+Bra( args ) + bk
  2655. Else
  2656. If Not odecl.noCastGen Then
  2657. Emit pre + odecl.castTo + api + " "+id+Bra( args ) + bk
  2658. End If
  2659. End If
  2660. Else
  2661. If Not odecl.castTo Then
  2662. If Not args Then
  2663. ' for function pointer return type, we need to generate () regardless of whether there are
  2664. ' args or not.
  2665. args = " "
  2666. End If
  2667. Emit pre + TransType( decl.retType, id, args )+ bk
  2668. Else
  2669. If Not odecl.noCastGen Then
  2670. Emit pre + odecl.castTo +" "+Bra( args ) + bk
  2671. End If
  2672. End If
  2673. End If
  2674. For Local t$=EachIn argCasts
  2675. Emit t
  2676. Next
  2677. End If
  2678. If Not proto Then
  2679. If PROFILER Then
  2680. DebugPrint("", TransFullName(decl))
  2681. End If
  2682. If DEBUG Then
  2683. For Local i:Int=0 Until decl.argDecls.Length
  2684. Local arg:TArgDecl=decl.argDecls[i]
  2685. DebugObject(arg.ty, arg.munged, id)
  2686. Next
  2687. End If
  2688. If decl.IsAbstract() Then
  2689. ' TODO : remove following line when generation stablises.
  2690. Emit "printf(~qAbstract method called : " + decl.ident + "\n~q);fflush(stdout);"
  2691. Emit "brl_blitz_NullMethodError();"
  2692. Else
  2693. decl.Semant()
  2694. If opt_debug And decl.IsMethod() Then
  2695. Emit TransDebugNullObjectError("o", TClassDecl(decl.scope)) + ";"
  2696. End If
  2697. EmitLocalDeclarations(decl)
  2698. EmitBlock decl
  2699. End If
  2700. Emit "}"
  2701. End If
  2702. ' reset label ids
  2703. contLabelId = 0
  2704. exitLabelId = 0
  2705. EndLocalScope
  2706. 'PopMungScope
  2707. opt_debug = tmpDebug
  2708. End Method
  2709. Method EmitLocalDeclarations(decl:TScopeDecl, ignoreVar:TValDecl = Null)
  2710. If opt_debug Then
  2711. For Local ldecl:TLocalDecl = EachIn decl.Decls()
  2712. If ldecl <> ignoreVar Then
  2713. If Not TArgDecl(ldecl) And Not ldecl.generated Then
  2714. MungDecl ldecl
  2715. Local ty:TType = ldecl.ty
  2716. Local t:String = TransLocalDeclNoInit(ldecl)
  2717. ' If TObjectType( ty ) And TObjectType( ty ).classDecl.IsStruct() Then
  2718. ' t :+ "={}"
  2719. ' End If
  2720. Emit t + ";"
  2721. End If
  2722. End If
  2723. Next
  2724. End If
  2725. End Method
  2726. Method EmitClassFieldsProto(classDecl:TClassDecl)
  2727. If classDecl.superClass Then
  2728. EmitClassFieldsProto(classDecl.superClass)
  2729. End If
  2730. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  2731. decl.Semant()
  2732. If Not TFunctionPtrType(decl.ty) Then
  2733. If classDecl.IsExtern() Then
  2734. Emit TransType(decl.ty, "") + " " + decl.ident + ";"
  2735. Else
  2736. Emit TransType(decl.ty, classDecl.actual.munged) + " _" + classDecl.actual.munged.ToLower() + "_" + decl.IdentLower() + ";"
  2737. End If
  2738. Else
  2739. If classDecl.IsExtern() Then
  2740. Emit TransType(decl.ty, decl.ident) + ";"
  2741. Else
  2742. Emit TransType(decl.ty, "_" + classDecl.actual.munged.ToLower() + "_" + decl.IdentLower()) + ";"
  2743. End If
  2744. End If
  2745. Next
  2746. End Method
  2747. Method EmitClassGlobalsProto(classDecl:TClassDecl)
  2748. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  2749. decl.Semant()
  2750. If TFunctionPtrType(decl.ty) Then
  2751. Emit TransRefType( decl.ty, decl.munged ) + ";"
  2752. Else
  2753. Emit "extern "+TransRefType( decl.ty, "" )+" "+ decl.munged+";"
  2754. End If
  2755. Next
  2756. End Method
  2757. Method BBClassClassFuncProtoBuildList( classDecl:TClassDecl, list:TList )
  2758. 'Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
  2759. If classDecl.superClass Then
  2760. BBClassClassFuncProtoBuildList(classDecl.superClass, list)
  2761. End If
  2762. For Local idecl:TClassDecl = EachIn classDecl.implmentsAll
  2763. BBClassClassFuncProtoBuildList(idecl, list)
  2764. Next
  2765. For Local decl:TDecl=EachIn classDecl.Decls()
  2766. Local fdecl:TFuncDecl =TFuncDecl( decl )
  2767. If fdecl
  2768. If Not fdecl.IsSemanted()
  2769. fdecl.Semant()
  2770. End If
  2771. If Not equalsBuiltInFunc(classDecl, fdecl) And Not equalsTorFunc(classDecl, fdecl) Then
  2772. Local ignore:Int
  2773. Local link:TLink=list._head._succ
  2774. While link<>list._head
  2775. Local ofdecl:TFuncDecl = TFuncDecl(link._value)
  2776. If fdecl.ident = ofdecl.ident And fdecl.EqualsArgs(ofdecl) Then
  2777. If fdecl.overrides Then
  2778. If fdecl.returnTypeSubclassed Then
  2779. link._value = fdecl
  2780. End If
  2781. ignore = True
  2782. Exit
  2783. End If
  2784. If TFuncDecl(link._value).IsMethod() Then
  2785. ignore = True
  2786. End If
  2787. EndIf
  2788. link = link._succ
  2789. Wend
  2790. If Not ignore Then
  2791. list.AddLast(fdecl)
  2792. End If
  2793. Continue
  2794. End If
  2795. EndIf
  2796. Next
  2797. End Method
  2798. Method EmitBBClassClassFuncProto( classDecl:TClassDecl )
  2799. Local list:TList = New TList
  2800. BBClassClassFuncProtoBuildList(classDecl, list)
  2801. For Local fdecl:TFuncDecl = EachIn list
  2802. EmitBBClassFuncProto( fdecl )
  2803. Next
  2804. End Method
  2805. Method EmitClassProto( classDecl:TClassDecl )
  2806. Local classid$=classDecl.munged
  2807. Local superid$
  2808. If classDecl.superClass Then
  2809. superid=classDecl.superClass.actual.munged
  2810. End If
  2811. 'Emit "void _" + classid + "_New" + Bra(TransObject(classdecl) + " o") + ";"
  2812. EmitClassDeclNewListProto(classDecl)
  2813. If classHierarchyHasFunction(classDecl, "Delete") Then
  2814. Emit "void _" + classid + "_Delete" + Bra(TransObject(classdecl) + " o") + ";"
  2815. End If
  2816. If classHasFunction(classDecl, "ToString") Then
  2817. Emit "BBSTRING _" + classid + "_ToString" + Bra(TransObject(classdecl) + " o") + ";"
  2818. End If
  2819. If classHasFunction(classDecl, "Compare") Then
  2820. Emit "BBINT _" + classid + "_Compare(" + TransObject(classdecl) + " o, BBOBJECT otherObject);"
  2821. End If
  2822. If classHasFunction(classDecl, "SendMessage") Then
  2823. Emit "BBOBJECT _" + classid + "_SendMessage(" + TransObject(classdecl) + " o, BBOBJECT message, BBOBJECT source);"
  2824. End If
  2825. 'Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
  2826. classDecl.SemantParts()
  2827. 'Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls(Null, False)
  2828. For Local decl:TDecl=EachIn classDecl.Decls()
  2829. 'For Local fdecl:TFuncDecl = EachIn fdecls
  2830. Local fdecl:TFuncDecl =TFuncDecl( decl )
  2831. If fdecl
  2832. If Not equalsBuiltInFunc(classDecl, fdecl) And Not equalsTorFunc(classDecl, fdecl) Then
  2833. EmitClassFuncProto( fdecl )
  2834. Continue
  2835. End If
  2836. EndIf
  2837. Local gdecl:TGlobalDecl =TGlobalDecl( decl )
  2838. If gdecl
  2839. MungDecl gdecl
  2840. ' Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
  2841. Continue
  2842. EndIf
  2843. Next
  2844. Emit ""
  2845. ' emit the class structure
  2846. Emit "struct BBClass_" + classid + " {"
  2847. If classDecl.superClass.ident = "Object" Then
  2848. Emit "BBClass* super;"
  2849. Else
  2850. Emit "struct BBClass_" + classDecl.superClass.munged + "* super;"
  2851. End If
  2852. Emit "void (*free)( BBObject *o );"
  2853. Emit "BBDebugScope* debug_scope;"
  2854. Emit "unsigned int instance_size;"
  2855. Emit "void (*ctor)( BBOBJECT o );"
  2856. Emit "void (*dtor)( BBOBJECT o );"
  2857. Emit "BBSTRING (*ToString)( BBOBJECT x );"
  2858. Emit "int (*Compare)( BBOBJECT x,BBOBJECT y );"
  2859. Emit "BBOBJECT (*SendMessage)( BBOBJECT o,BBOBJECT m,BBOBJECT s );"
  2860. Emit "BBINTERFACETABLE itable;"
  2861. Emit "void* extra;"
  2862. Emit "unsigned int obj_size;"
  2863. EmitBBClassClassFuncProto(classDecl)
  2864. Emit "};~n"
  2865. If classDecl.IsInterface() Then
  2866. Emit "struct " + classid + "_methods {"
  2867. EmitBBClassClassFuncProto(classDecl)
  2868. Emit "};~n"
  2869. End If
  2870. Emit "struct " + classid + "_obj {"
  2871. Emit "struct BBClass_" + classid + "* clas;"
  2872. BeginLocalScope
  2873. EmitClassFieldsProto(classDecl)
  2874. EndLocalScope
  2875. Emit "};"
  2876. Emit "extern struct BBClass_" + classid + " " + classid + ";"
  2877. EmitClassGlobalsProto(classDecl);
  2878. ' fields
  2879. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  2880. MungDecl decl
  2881. Next
  2882. End Method
  2883. Method EmitExternClassFuncProto( classDecl:TClassDecl )
  2884. If classDecl.superClass Then
  2885. EmitExternClassFuncProto(classDecl.superClass)
  2886. End If
  2887. For Local decl:TFuncDecl = EachIn classDecl.Decls()
  2888. decl.Semant()
  2889. ' code is written as a method, but emitted as a function pointer
  2890. ' with self as the first parameter
  2891. Local func:TFuncDecl = TFuncDecl(decl.Copy())
  2892. Local argDecl:TArgDecl = New TArgDecl.Create("This", classDecl.objectType, Null)
  2893. func.argDecls = [argDecl] + func.argDecls
  2894. func.Semant()
  2895. Local ty:TFunctionPtrType = New TFunctionPtrType
  2896. ty.func = func
  2897. Emit TransType(ty, decl.Ident) + ";"
  2898. Next
  2899. End Method
  2900. Method EmitExternClassTypeFuncProto( classDecl:TClassDecl )
  2901. Local doneCtorDtor:Int
  2902. Local iDecl:TClassDecl
  2903. For Local decl:TFuncDecl = EachIn classDecl.GetAllOriginalFuncDecls(Null, True)
  2904. decl.Semant()
  2905. ' first interface preceeds ctor/dtor
  2906. If Not doneCtorDtor
  2907. If Not iDecl And TClassDecl(decl.scope).IsInterface() Then
  2908. iDecl = TClassDecl(decl.scope)
  2909. End If
  2910. If iDecl
  2911. If iDecl <> TClassDecl(decl.scope) Then
  2912. ' a different interface
  2913. doneCtorDtor = True
  2914. Emit "void(*_ctor)();"
  2915. Emit "void(*_dtor)();"
  2916. End If
  2917. Else
  2918. doneCtorDtor = True
  2919. Emit "void(*_ctor)();"
  2920. Emit "void(*_dtor)();"
  2921. End If
  2922. End If
  2923. ' code is written as a method, but emitted as a function pointer
  2924. ' with self as the first parameter
  2925. Local func:TFuncDecl = TFuncDecl(decl.Copy())
  2926. Local argDecl:TArgDecl = New TArgDecl.Create("This", classDecl.objectType, Null)
  2927. func.argDecls = [argDecl] + func.argDecls
  2928. func.Semant()
  2929. Local ty:TFunctionPtrType = New TFunctionPtrType
  2930. ty.func = func
  2931. Emit TransType(ty, decl.Ident) + ";"
  2932. Next
  2933. End Method
  2934. Method EmitExternClassProto( classDecl:TClassDecl )
  2935. Emit "typedef struct " + classDecl.ident + " " + classDecl.ident + ";"
  2936. ' vtable
  2937. Emit "struct " + classDecl.ident + "Vtbl {"
  2938. ' methods
  2939. If classDecl.IsInterface() Then
  2940. EmitExternClassFuncProto(classDecl)
  2941. Else
  2942. EmitExternClassTypeFuncProto(classDecl)
  2943. End If
  2944. Emit "};"
  2945. Emit "struct " + classDecl.ident + " {"
  2946. Emit "struct " + classDecl.ident + "Vtbl* vtbl;"
  2947. Emit "};"
  2948. End Method
  2949. Field emittedStructs:TList = New TList
  2950. Method EmitStructClassProto( classDecl:TClassDecl )
  2951. If emittedStructs.Contains(classDecl) Return
  2952. emittedStructs.AddLast(classDecl)
  2953. ' emit any dependent structs first
  2954. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  2955. decl.Semant()
  2956. If TObjectType(decl.ty) And TObjectType(decl.ty).classDecl.IsStruct() Then
  2957. If Not emittedStructs.Contains(TObjectType(decl.ty).classDecl) Then
  2958. EmitStructClassProto(TObjectType(decl.ty).classDecl)
  2959. End If
  2960. End If
  2961. Next
  2962. If classDecl.IsExtern()
  2963. Emit "struct " + classDecl.ident + " {"
  2964. Else
  2965. EmitClassDeclNewListProto( classDecl )
  2966. For Local fdecl:TFuncDecl=EachIn classDecl.Decls()
  2967. If fdecl.IdentLower() <> "new" Then
  2968. EmitClassFuncProto( fdecl, True )
  2969. End If
  2970. Next
  2971. Emit "struct " + classDecl.munged + " {"
  2972. End If
  2973. BeginLocalScope
  2974. EmitClassFieldsProto(classDecl)
  2975. EndLocalScope
  2976. Emit "};"
  2977. End Method
  2978. Method classHasFunction:Int(classDecl:TClassDecl, func:String)
  2979. Local f:String = func.ToLower()
  2980. For Local decl:TFuncDecl = EachIn classDecl.Decls()
  2981. If Not decl.IsSemanted() Then
  2982. decl.Semant
  2983. End If
  2984. If decl.IdentLower() = f And equalsBuiltInFunc(classDecl.superClass, decl) Then
  2985. Return True
  2986. End If
  2987. Next
  2988. Return False
  2989. End Method
  2990. Method classHierarchyHasFunction:Int(classDecl:TClassDecl, func:String)
  2991. If classHasFunction(classDecl, func) Return True
  2992. If classDecl.superClass And classDecl.superClass.munged <> "bbObjectClass" Then
  2993. Return classHierarchyHasFunction(classDecl.superClass, func)
  2994. End If
  2995. Return False
  2996. End Method
  2997. Method classidForFunction:String(classDecl:TClassDecl, func:String)
  2998. If classHasFunction(classDecl, func) Return classDecl.munged
  2999. If classDecl.superClass And classDecl.superClass.munged <> "bbObjectClass" Then
  3000. Return classidForFunction(classDecl.superClass, func)
  3001. End If
  3002. Return Null
  3003. End Method
  3004. Method EmitMark( id$,ty:TType,queue:Int )
  3005. If TObjectType( ty )
  3006. If id.EndsWith( ".p" )
  3007. If ty.GetClass().IsInterface() id=id[..-2] Else InternalErr
  3008. Else
  3009. If ty.GetClass().IsInterface() InternalErr
  3010. EndIf
  3011. If queue
  3012. Emit "gc_mark_q("+id+");"
  3013. Else
  3014. Emit "gc_mark("+id+");"
  3015. EndIf
  3016. Else If TArrayType( ty )
  3017. Emit "gc_mark("+id+");"
  3018. Return
  3019. EndIf
  3020. End Method
  3021. Method EmitClassConstsDebugScope(classDecl:TClassDecl)
  3022. For Local decl:TConstDecl = EachIn classDecl.Decls()
  3023. EmitConstDebugScope(decl)
  3024. Next
  3025. End Method
  3026. Method EmitConstDebugScope(decl:TConstDecl)
  3027. Emit "{"
  3028. Emit "BBDEBUGDECL_CONST,"
  3029. Emit Enquote(decl.ident) + ","
  3030. Emit Enquote(TransDebugScopeType(decl.ty) + TransDebugMetaData(decl.metadata.metadataString)) + ","
  3031. _appInstance.mapStringConsts(decl.value)
  3032. Emit ".const_value=&" + TStringConst(_appInstance.stringConsts.ValueForKey(decl.value)).id
  3033. Emit "},"
  3034. End Method
  3035. Method EmitClassFieldsDebugScope(classDecl:TClassDecl)
  3036. ' Don't list superclass fields in our debug scope
  3037. 'If classDecl.superClass Then
  3038. ' EmitClassFieldsDebugScope(classDecl.superClass)
  3039. 'End If
  3040. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3041. Emit "{"
  3042. Emit "BBDEBUGDECL_FIELD,"
  3043. Emit Enquote(decl.ident) + ","
  3044. Emit Enquote(TransDebugScopeType(decl.ty) + TransDebugMetaData(decl.metadata.metadataString)) + ","
  3045. Local offset:String = ".field_offset=offsetof"
  3046. If classDecl.IsStruct() Then
  3047. offset :+ Bra("struct " + classDecl.munged + "," + decl.munged)
  3048. Else
  3049. offset :+ Bra("struct " + classDecl.munged + "_obj," + decl.munged)
  3050. End If
  3051. ' If WORD_SIZE = 8 Then
  3052. ' Emit Bra("BBLONG") + offset
  3053. ' Else
  3054. Emit offset
  3055. ' End If
  3056. 'If Not TFunctionPtrType(decl.ty) Then
  3057. ' Emit TransType(decl.ty, classDecl.actual.munged) + " _" + classDecl.actual.munged.ToLower() + "_" + decl.ident.ToLower() + ";"
  3058. 'Else
  3059. ' Emit TransType(decl.ty, "_" + classDecl.actual.munged.ToLower() + "_" + decl.ident.ToLower()) + ";"
  3060. 'End If
  3061. Emit "},"
  3062. 'offset:+ decl.ty.GetSize()
  3063. Next
  3064. 'Return offset
  3065. End Method
  3066. Method EmitClassStandardMethodDebugScope(ident:String, ty:String, munged:String)
  3067. Emit "{"
  3068. Emit "BBDEBUGDECL_TYPEMETHOD,"
  3069. Emit Enquote(ident) + ","
  3070. Emit Enquote(ty) + ","
  3071. Emit "&" + munged
  3072. Emit "},"
  3073. End Method
  3074. Method TransDebugMetaData:String(meta:String)
  3075. If meta Then
  3076. Return "{" + meta + "}"
  3077. End If
  3078. End Method
  3079. Method EmitBBClassFuncsDebugScope(decl:TFuncDecl)
  3080. Emit "{"
  3081. If decl.IsMethod() Then
  3082. Emit "BBDEBUGDECL_TYPEMETHOD,"
  3083. Else
  3084. Emit "BBDEBUGDECL_TYPEFUNCTION,"
  3085. End If
  3086. Emit Enquote(decl.ident) + ","
  3087. Local s:String = "("
  3088. For Local i:Int = 0 Until decl.argDecls.length
  3089. If i Then
  3090. s:+ ","
  3091. End If
  3092. s:+ TransDebugScopeType(decl.argDecls[i].ty)
  3093. Next
  3094. s:+ ")"
  3095. If decl.retType Then
  3096. s:+ TransDebugScopeType(decl.retType)
  3097. End If
  3098. s:+ TransDebugMetaData(decl.metadata.metadataString)
  3099. Emit Enquote(s) + ","
  3100. If decl.IsMethod() Or decl.IsCTor() Then
  3101. Emit "&_" + decl.munged
  3102. Else
  3103. Emit "&" + decl.munged
  3104. End If
  3105. Emit "},"
  3106. End Method
  3107. Method BBClassClassFuncsDebugScopeBuildList(classDecl:TClassDecl, list:TList)
  3108. 'Local reserved:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
  3109. For Local decl:TDecl=EachIn classDecl.GetAllFuncDecls(Null, False)
  3110. Local fdecl:TFuncDecl =TFuncDecl( decl )
  3111. If fdecl
  3112. If Not fdecl.IsSemanted()
  3113. fdecl.Semant()
  3114. End If
  3115. If Not classDecl.IsInterface() And fdecl.IsAbstract() Then
  3116. Continue
  3117. End If
  3118. If Not equalsBuiltInFunc(classDecl, fdecl) Then
  3119. Local ignore:Int
  3120. Local link:TLink=list._head._succ
  3121. While link<>list._head
  3122. If fdecl.ident = TFuncDecl(link._value).ident Then
  3123. If fdecl.overrides Then
  3124. link._value = fdecl
  3125. ignore = True
  3126. Exit
  3127. End If
  3128. EndIf
  3129. link = link._succ
  3130. Wend
  3131. If Not ignore Then
  3132. list.AddLast(fdecl)
  3133. End If
  3134. Continue
  3135. End If
  3136. EndIf
  3137. Next
  3138. End Method
  3139. Method EmitBBClassClassFuncsDebugScope(classDecl:TClassDecl)
  3140. Local list:TList = New TList
  3141. BBClassClassFuncsDebugScopeBuildList(classDecl, list)
  3142. For Local fdecl:TFuncDecl = EachIn list
  3143. EmitBBClassFuncsDebugScope( fdecl )
  3144. Next
  3145. End Method
  3146. Method EmitClassFuncsDebugScope(classDecl:TClassDecl)
  3147. If classDecl.IsExtern() Return
  3148. Local classid$=classDecl.munged
  3149. Local superid$
  3150. If classDecl.superClass Then
  3151. superid = classDecl.superClass.actual.munged
  3152. End If
  3153. Local ret:String = "()i"
  3154. If opt_issuperstrict Then
  3155. ret = "()"
  3156. End If
  3157. If Not classDecl.IsInterface() Then
  3158. EmitClassStandardMethodDebugScope("New", ret, "_" + classid + "_New")
  3159. End If
  3160. If classHasFunction(classDecl, "ToString") Then
  3161. EmitClassStandardMethodDebugScope("ToString", "()$", "_" + classidForFunction(classDecl, "ToString") + "_ToString")
  3162. 'Emit "_" + classid + "_ToString,"
  3163. End If
  3164. If classHasFunction(classDecl, "Compare") Then
  3165. EmitClassStandardMethodDebugScope("Compare", "(:Object)i", "_" + classidForFunction(classDecl, "Compare") + "_Compare")
  3166. 'Emit "_" + classid + "_ObjectCompare,"
  3167. End If
  3168. If classHasFunction(classDecl, "SendMessage") Then
  3169. EmitClassStandardMethodDebugScope("SendMessage", "(:Object, :Object):Object", "_" + classidForFunction(classDecl, "SendMessage") + "_SendMessage")
  3170. 'Emit "_" + classid + "_SendMessage,"
  3171. End If
  3172. EmitBBClassClassFuncsDebugScope(classDecl)
  3173. End Method
  3174. Method EmitClassGlobalDebugScope( classDecl:TClassDecl )
  3175. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  3176. EmitGlobalDebugScope(decl)
  3177. Next
  3178. End Method
  3179. Method EmitGlobalDebugScope( decl:TGlobalDecl )
  3180. Emit "{"
  3181. Emit "BBDEBUGDECL_GLOBAL,"
  3182. Emit Enquote(decl.ident) + ","
  3183. Emit Enquote(TransDebugScopeType(decl.ty)) + ","
  3184. Emit "&" + decl.munged
  3185. Emit "},"
  3186. End Method
  3187. Method CountBBClassClassFuncsDebugScope(classDecl:TClassDecl, count:Int Var)
  3188. For Local decl:TDecl=EachIn classDecl.GetAllFuncDecls(Null, False)
  3189. Local fdecl:TFuncDecl =TFuncDecl( decl )
  3190. If fdecl
  3191. If Not classDecl.IsInterface() And fdecl.IsAbstract() Then
  3192. Continue
  3193. End If
  3194. If Not equalsBuiltInFunc(classDecl, fdecl) Then
  3195. count :+ 1
  3196. End If
  3197. End If
  3198. Next
  3199. End Method
  3200. Method CountClassConstsDebugScope(classDecl:TClassDecl, count:Int Var)
  3201. For Local decl:TConstDecl = EachIn classDecl.Decls()
  3202. count :+ 1
  3203. Next
  3204. End Method
  3205. Method CountClassFieldsDebugScope(classDecl:TClassDecl, count:Int Var)
  3206. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3207. count :+ 1
  3208. Next
  3209. End Method
  3210. Method DebugScopeDeclCount:Int(classDecl:TClassDecl)
  3211. Local count:Int = 2 ' "New" counts as first one
  3212. ' but we don't use "New" for interfaces...
  3213. If classDecl.IsInterface() Or (classDecl.IsExtern() And classDecl.IsStruct()) Then
  3214. count :- 1
  3215. End If
  3216. ' consts
  3217. CountClassConstsDebugScope(classDecl, count)
  3218. ' fields
  3219. CountClassFieldsDebugScope(classDecl, count)
  3220. ' standard methods
  3221. If classHasFunction(classDecl, "ToString") Then
  3222. count :+ 1
  3223. End If
  3224. If classHasFunction(classDecl, "Compare") Then
  3225. count :+ 1
  3226. End If
  3227. If classHasFunction(classDecl, "SendMessage") Then
  3228. count :+ 1
  3229. End If
  3230. ' methods and functions
  3231. CountBBClassClassFuncsDebugScope(classDecl, count)
  3232. ' class globals
  3233. For Local decl:TGlobalDecl = EachIn classDecl.Decls()
  3234. count :+ 1
  3235. Next
  3236. Return count
  3237. End Method
  3238. Method EmitClassDecl( classDecl:TClassDecl )
  3239. PushEnv classDecl
  3240. 'If classDecl.IsTemplateInst()
  3241. ' Return
  3242. 'EndIf
  3243. If classDecl.IsExtern() And Not classDecl.IsStruct() Then
  3244. Return
  3245. EndIf
  3246. Local classid$=classDecl.munged
  3247. Local superid$
  3248. If classDecl.superClass Then
  3249. superid = classDecl.superClass.actual.munged
  3250. End If
  3251. If Not classDecl.IsExtern() Then
  3252. ' process nested functions for new
  3253. Local decl:TFuncDecl = classDecl.FindFuncDecl("new",,,,,,SCOPE_CLASS_HEIRARCHY)
  3254. If decl And decl.scope = classDecl Then ' only our own New method, not any from superclasses
  3255. decl.Semant
  3256. ' emit nested protos
  3257. For Local fdecl:TFuncDecl = EachIn decl._decls
  3258. EmitFuncDecl(fdecl, True, False)
  3259. Next
  3260. ' emit nested bodies
  3261. For Local fdecl:TFuncDecl = EachIn decl._decls
  3262. EmitFuncDecl(fdecl, False, False)
  3263. Next
  3264. End If
  3265. EmitClassDeclNewList(classDecl)
  3266. ' process nested functions for delete
  3267. decl = classDecl.FindFuncDecl("delete",,,,,,SCOPE_CLASS_HEIRARCHY)
  3268. If decl Then
  3269. decl.Semant
  3270. ' emit nested protos
  3271. For Local fdecl:TFuncDecl = EachIn decl._decls
  3272. EmitFuncDecl(fdecl, True, False)
  3273. Next
  3274. ' emit nested bodies
  3275. For Local fdecl:TFuncDecl = EachIn decl._decls
  3276. EmitFuncDecl(fdecl, False, False)
  3277. Next
  3278. End If
  3279. If classHierarchyHasFunction(classDecl, "Delete") Then
  3280. EmitClassDeclDelete(classDecl)
  3281. End If
  3282. Rem
  3283. 'fields ctor
  3284. Emit classid+"::"+classid+"(){"
  3285. For Local decl:TDecl=EachIn classDecl.Semanted()
  3286. Local fdecl:TFieldDecl=TFieldDecl( decl )
  3287. If Not fdecl Continue
  3288. Emit TransField(fdecl,Null)+"="+fdecl.init.Trans()+";"
  3289. Next
  3290. Emit "}"
  3291. End Rem
  3292. Local reserved:String = ",New,Delete,".ToLower()
  3293. 'methods
  3294. For Local decl:TDecl=EachIn classDecl.Decls()
  3295. Local fdecl:TFuncDecl=TFuncDecl( decl )
  3296. If fdecl
  3297. If reserved.Find("," + fdecl.IdentLower() + ",") = -1 Then
  3298. EmitGDBDebug(fdecl)
  3299. EmitFuncDecl fdecl, , True
  3300. Continue
  3301. End If
  3302. EndIf
  3303. 'Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  3304. 'If gdecl
  3305. ' Emit TransRefType( gdecl.ty )+" "+classid+"::"+gdecl.munged+";"
  3306. ' Continue
  3307. ' EndIf
  3308. Next
  3309. Rem
  3310. 'gc_mark
  3311. Emit "void "+classid+"::mark(){"
  3312. If classDecl.superClass
  3313. Emit classDecl.superClass.actual.munged+"::mark();"
  3314. EndIf
  3315. For Local decl:TDecl=EachIn classDecl.Semanted()
  3316. Local fdecl:TFieldDecl=TFieldDecl( decl )
  3317. If fdecl EmitMark TransField(fdecl,Null),fdecl.ty,True
  3318. Next
  3319. Emit "}"
  3320. End Rem
  3321. For Local decl:TDecl=EachIn classDecl.Semanted()
  3322. Local gdecl:TGlobalDecl =TGlobalDecl( decl )
  3323. If gdecl
  3324. If TFunctionPtrType(gdecl.ty) Then
  3325. Emit TransRefType( gdecl.ty, gdecl.munged ) + ";"
  3326. Else
  3327. Emit TransRefType( gdecl.ty, "" )+" "+gdecl.munged+";"
  3328. End If
  3329. Continue
  3330. EndIf
  3331. Next
  3332. reserved = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
  3333. End If
  3334. 'Emit "struct _" + classid + "_DebugScope{"
  3335. 'Emit "int kind;"
  3336. 'Emit "const char *name;"
  3337. 'Emit "BBDebugDecl decls[" + DebugScopeDeclCount(classDecl) + "];"
  3338. 'Emit "};"
  3339. Local count:Int = DebugScopeDeclCount(classDecl)
  3340. ' debugscope
  3341. If count > 1 Then
  3342. _app.scopeDefs.Insert(String(count - 1), "")
  3343. Emit "struct BBDebugScope_" + (count - 1) + " " + classid + "_scope ={"
  3344. Else
  3345. Emit "struct BBDebugScope " + classid + "_scope ={"
  3346. End If
  3347. If classDecl.IsInterface() Then
  3348. Emit "BBDEBUGSCOPE_USERINTERFACE,"
  3349. Else If classDecl.IsStruct() Then
  3350. Emit "BBDEBUGSCOPE_USERSTRUCT,"
  3351. Else
  3352. Emit "BBDEBUGSCOPE_USERTYPE,"
  3353. End If
  3354. Emit EnQuote(classDecl.ident + TransDebugMetaData(classDecl.metadata.metadataString)) + ","
  3355. Emit "{"
  3356. ' debug const decls
  3357. EmitClassConstsDebugScope(classDecl)
  3358. ' debug field decls
  3359. EmitClassFieldsDebugScope(classDecl)
  3360. ' debug global decls
  3361. EmitClassGlobalDebugScope(classDecl)
  3362. ' debug func decls
  3363. EmitClassFuncsDebugScope(classDecl)
  3364. Emit "BBDEBUGDECL_END"
  3365. Emit "}"
  3366. Emit "};"
  3367. Local fdecls:TFuncDecl[] = classDecl.GetAllFuncDecls()
  3368. Local implementedInterfaces:TMap = classDecl.GetInterfaces()
  3369. Local ifcCount:Int
  3370. If Not classDecl.IsStruct() Then
  3371. ' interface class implementation
  3372. If Not classDecl.IsInterface()
  3373. If Not implementedInterfaces.IsEmpty() Then
  3374. Emit "struct " + classid + "_vdef {"
  3375. For Local ifc:TClassDecl = EachIn implementedInterfaces.Values()
  3376. Emit "struct " + ifc.munged + "_methods interface_" + ifc.ident + ";"
  3377. ifcCount :+ 1
  3378. Next
  3379. Emit "};~n"
  3380. Emit "static struct BBInterfaceOffsets " + classid + "_ifc_offsets[] = {"
  3381. For Local ifc:TClassDecl = EachIn implementedInterfaces.Values()
  3382. Emit "{&" + ifc.munged + "_ifc, offsetof(struct " + classid + "_vdef, interface_" + ifc.ident + ")},"
  3383. Next
  3384. Emit "};~n"
  3385. Emit "struct " + classid + "_vdef " + classid + "_ifc_vtable = {"
  3386. For Local ifc:TClassDecl = EachIn implementedInterfaces.Values()
  3387. Emit ".interface_" + ifc.ident + "={"
  3388. For Local func:TFuncDecl = EachIn ifc.GetImplementedFuncs()
  3389. If func.IsMethod() Then
  3390. For Local f:TFuncDecl = EachIn fdecls
  3391. Mungdecl f
  3392. If f.ident = func.ident And func.EqualsFunc(f) Then
  3393. Emit "_" + f.munged + ","
  3394. Exit
  3395. End If
  3396. Next
  3397. End If
  3398. Next
  3399. Emit "},"
  3400. Next
  3401. Emit "};~n"
  3402. Emit "struct BBInterfaceTable " + classid + "_itable = {"
  3403. Emit classid + "_ifc_offsets,"
  3404. Emit "&" + classid + "_ifc_vtable,"
  3405. Emit ifcCount
  3406. Emit "};~n"
  3407. End If
  3408. End If
  3409. Emit "struct BBClass_" + classid + " " + classid + "={"
  3410. ' super class reference
  3411. Emit "&" + classDecl.superClass.munged + ","
  3412. Emit "bbObjectFree,"
  3413. ' debugscope
  3414. Emit "&" + classid + "_scope,"
  3415. ' object instance size
  3416. Emit "sizeof" + Bra("struct " + classid + "_obj") + ","
  3417. ' standard methods
  3418. Emit "_" + classid + "_New,"
  3419. If Not classHierarchyHasFunction(classDecl, "Delete") Then
  3420. Emit "bbObjectDtor,"
  3421. Else
  3422. Emit "_" + classid + "_Delete,"
  3423. End If
  3424. If classHierarchyHasFunction(classDecl, "ToString") Then
  3425. Emit "_" + classidForFunction(classDecl, "ToString") + "_ToString,"
  3426. Else
  3427. Emit "bbObjectToString,"
  3428. End If
  3429. If classHierarchyHasFunction(classDecl, "Compare") Then
  3430. Emit "_" + classidForFunction(classDecl, "Compare") + "_Compare,"
  3431. Else
  3432. Emit "bbObjectCompare,"
  3433. End If
  3434. If classHierarchyHasFunction(classDecl, "SendMessage") Then
  3435. Emit "_" + classidForFunction(classDecl, "SendMessage") + "_SendMessage,"
  3436. Else
  3437. Emit "bbObjectSendMessage,"
  3438. End If
  3439. 'Emit "public:"
  3440. 'fields
  3441. 'For Local decl:TDecl=EachIn classDecl.Semanted()
  3442. ' Local fdecl:TFieldDecl =TFieldDecl( decl )
  3443. ' If fdecl
  3444. ' Emit TransRefType( fdecl.ty )+" "+fdecl.munged+";"
  3445. ' Continue
  3446. ' EndIf
  3447. 'Next
  3448. 'fields ctor
  3449. 'Emit classid+"();"
  3450. 'methods
  3451. 'For Local decl:TDecl=EachIn classDecl.Semanted()
  3452. '
  3453. ' Local fdecl:TFuncDecl =TFuncDecl( decl )
  3454. ' If fdecl
  3455. ' EmitFuncProto fdecl
  3456. ' Continue
  3457. ' EndIf
  3458. '
  3459. ' Local gdecl:TGlobalDecl =TGlobalDecl( decl )
  3460. ' If gdecl
  3461. ' Emit "static "+TransRefType( gdecl.ty )+" "+gdecl.munged+";"
  3462. ' Continue
  3463. ' EndIf
  3464. 'Next
  3465. 'gc mark
  3466. 'Emit "void mark();"
  3467. If classDecl.IsInterface() Or implementedInterfaces.IsEmpty() Then
  3468. ' itable
  3469. Emit "0,"
  3470. ' extra pointer
  3471. Emit "0,"
  3472. Else
  3473. Emit "&" + classid + "_itable,"
  3474. ' extra pointer
  3475. Emit "0,"
  3476. End If
  3477. ' obj_size
  3478. Emit TransObjectSize(classDecl)
  3479. ' methods/funcs
  3480. 'reserved = "New,Delete,ToString,ObjectCompare,SendMessage".ToLower()
  3481. 'For Local decl:TFuncDecl = EachIn classDecl.Decls()
  3482. For Local decl:TFuncDecl = EachIn fdecls
  3483. If Not equalsBuiltInFunc(classDecl, decl) And Not equalsTorFunc(classDecl, decl) Then
  3484. MungDecl decl
  3485. If decl.IsMethod() Then
  3486. Emit ",_" + decl.munged
  3487. Else
  3488. Emit "," + decl.munged
  3489. End If
  3490. End If
  3491. Next
  3492. Emit "};~n"
  3493. If classDecl.IsInterface() Then
  3494. Emit "const struct BBInterface " + classid + "_ifc = { &" + classid + ", (const char *) ~q" + classDecl.ident + "~q };"
  3495. Else
  3496. End If
  3497. End If
  3498. PopEnv
  3499. End Method
  3500. Method TransObjectSize:String(classDecl:TClassDecl)
  3501. Local t:String
  3502. Local fieldDecl:TFieldDecl
  3503. For Local decl:TFieldDecl = EachIn classDecl.Decls()
  3504. fieldDecl = decl
  3505. Next
  3506. If fieldDecl Then
  3507. t = "offsetof" + Bra("struct " + classDecl.munged + "_obj," + fieldDecl.munged) + " - sizeof(void*) + sizeof" + Bra(TransType(fieldDecl.ty, ""))
  3508. Else
  3509. t = "0"
  3510. End If
  3511. Return t
  3512. End Method
  3513. Method EmitClassDeclNew( classDecl:TClassDecl, fdecl:TFuncDecl )
  3514. Local classid$=classDecl.munged
  3515. Local superid$=classDecl.superClass.actual.munged
  3516. Local t:String = "void _"
  3517. If fdecl.argDecls.Length Then
  3518. If classDecl = fdecl.scope Then
  3519. t :+ fdecl.munged
  3520. Else
  3521. t :+ classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  3522. End If
  3523. Else
  3524. t :+ classid + "_New"
  3525. End If
  3526. 'Find decl we override
  3527. Local odecl:TFuncDecl=fdecl
  3528. While odecl.overrides
  3529. odecl=odecl.overrides
  3530. Wend
  3531. Local args:String = TransObject(classdecl, True) + " o"
  3532. For Local i:Int=0 Until fdecl.argDecls.Length
  3533. Local arg:TArgDecl=fdecl.argDecls[i]
  3534. Local oarg:TArgDecl=odecl.argDecls[i]
  3535. MungDecl arg, True
  3536. If args args:+","
  3537. If Not TFunctionPtrType(oarg.ty) Then
  3538. If Not odecl.castTo Then
  3539. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  3540. Else
  3541. args:+ oarg.castTo + " " + arg.munged
  3542. End If
  3543. Else
  3544. If Not odecl.castTo Then
  3545. args:+TransType( oarg.ty, arg.munged )
  3546. Else
  3547. args:+ oarg.castTo
  3548. End If
  3549. End If
  3550. If arg.ty.EqualsType( oarg.ty ) Continue
  3551. Next
  3552. Emit t + Bra(args) + " {"
  3553. Local newDecl:TNewDecl = TNewDecl(fdecl)
  3554. If Not classDecl.IsStruct() Then
  3555. ' calling constructor?
  3556. If newDecl And newDecl.chainedCtor Then
  3557. mungdecl newDecl.chainedCtor.ctor
  3558. Emit "_" + newDecl.chainedCtor.ctor.ClassScope().munged + "_" + newDecl.chainedCtor.ctor.ident + MangleMethod(newDecl.chainedCtor.ctor) + TransArgs(newDecl.chainedCtor.args, newDecl.chainedCtor.ctor, "o") + ";"
  3559. Else
  3560. If classDecl.superClass.ident = "Object" Then
  3561. Emit "bbObjectCtor(o);"
  3562. Else
  3563. Emit "_" + superid + "_New(o);"
  3564. End If
  3565. End If
  3566. Emit "o->clas = (BBClass*)&" + classid + ";"
  3567. End If
  3568. ' only initialise fields if we are not chaining to a local (in our class) constructor.
  3569. ' this prevents fields being re-initialised through the call-chain.
  3570. If Not newDecl.chainedCtor Or (newDecl.chainedCtor And classDecl <> newDecl.chainedCtor.ctor.scope) Then
  3571. ' field initialisation
  3572. For Local decl:TFieldDecl=EachIn classDecl.Decls()
  3573. Local fld:String
  3574. ' ((int*)((char*)o + 5))[0] =
  3575. fld :+ TransFieldRef(decl, "o")
  3576. If decl.init Then
  3577. If TObjectType(decl.ty) And TObjectType(decl.ty).classdecl.IsExtern() And TObjectType(decl.ty).classdecl.IsStruct() Then
  3578. ' skip for uninitialised extern type
  3579. If Not isPointerType(decl.ty) And TConstExpr(decl.init) And Not TConstExpr(decl.init).value Then
  3580. Continue
  3581. End If
  3582. End If
  3583. ' initial value
  3584. If (TConstExpr(decl.init) And Not TConstExpr(decl.init).value) And TIntrinsicType(decl.ty) Then
  3585. fld :+ "= "
  3586. If TFloat64Type(decl.ty) Then
  3587. fld :+ "_mm_setzero_si64();"
  3588. Else If TFloat128Type(decl.ty) Then
  3589. fld :+ "_mm_setzero_ps();"
  3590. Else If TDouble128Type(decl.ty) Then
  3591. fld :+ "_mm_setzero_pd();"
  3592. Else If TInt128Type(decl.ty) Then
  3593. fld :+ "_mm_setzero_si128();"
  3594. End If
  3595. Else
  3596. 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
  3597. fld = "memset(&" + fld + ", 0, sizeof" + Bra(TransType(decl.ty, "")) + ");"
  3598. Else If TInvokeExpr(decl.init) And Not TInvokeExpr(decl.init).invokedWithBraces Then
  3599. fld :+ "= " + TInvokeExpr(decl.init).decl.munged + ";"
  3600. Else
  3601. fld :+ "= " + decl.init.Trans() + ";"
  3602. End If
  3603. End If
  3604. Else
  3605. If TNumericType(decl.ty) Or TObjectType(decl.ty) Or IsPointerType(decl.ty, 0, TType.T_POINTER) Then
  3606. fld :+ "= 0;"
  3607. Else If TFunctionPtrType(decl.ty) Then
  3608. fld :+ "= &brl_blitz_NullFunctionError;"
  3609. Else If TStringType(decl.ty) Then
  3610. fld :+ "= &bbEmptyString;"
  3611. Else If TArrayType(decl.ty) Then
  3612. fld :+ "= &bbEmptyArray;"
  3613. End If
  3614. End If
  3615. Emit fld
  3616. Next
  3617. End If
  3618. 'Local decl:TFuncDecl = classDecl.FindFuncDecl("new",,,,,,SCOPE_CLASS_LOCAL)
  3619. If fdecl And (fdecl.scope = classDecl Or fdecl.argDecls.Length) Then ' only our own New method, not any from superclasses
  3620. fdecl.Semant
  3621. If fdecl.munged <> "bbObjectCtor" Then
  3622. EmitLocalDeclarations(fdecl)
  3623. EmitBlock fdecl
  3624. End If
  3625. End If
  3626. '
  3627. Emit "}"
  3628. End Method
  3629. Method EmitClassDeclNewList( classDecl:TClassDecl )
  3630. Local classid$=classDecl.munged
  3631. Local superid$=classDecl.superClass.actual.munged
  3632. Local newDecls:TFuncDeclList = TFuncDeclList(classdecl.FindDeclList("new", SCOPE_CLASS_LOCAL))
  3633. For Local fdecl:TFuncDecl = EachIn newDecls
  3634. EmitClassDeclNew(classDecl, fdecl)
  3635. ' generate "objectNew" function if required
  3636. If (fdecl.argDecls And fdecl.argDecls.length) Or classDecl.IsStruct() Then
  3637. EmitClassDeclNewInit(classDecl, fdecl)
  3638. End If
  3639. Next
  3640. End Method
  3641. Method EmitClassDeclNewListProto( classDecl:TClassDecl )
  3642. Local classid$=classDecl.munged
  3643. 'Local superid$=classDecl.superClass.actual.munged
  3644. Local newDecls:TFuncDeclList = TFuncDeclList(classdecl.FindDeclList("new", SCOPE_CLASS_LOCAL))
  3645. For Local fdecl:TFuncDecl = EachIn newDecls
  3646. EmitClassDeclNewProto(classDecl, fdecl)
  3647. ' generate "objectNew" function if required
  3648. If (fdecl.argDecls And fdecl.argDecls.length) Or classDecl.IsStruct() Then
  3649. EmitClassDeclObjectNewProto(classDecl, fdecl)
  3650. End If
  3651. Next
  3652. End Method
  3653. Method EmitClassDeclNewInit(classDecl:TClassDecl, fdecl:TFuncDecl)
  3654. Local funcMunged:String
  3655. If classDecl = fdecl.scope Then
  3656. funcMunged = fdecl.munged
  3657. Else
  3658. funcMunged = classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  3659. End If
  3660. Local t:String = TransObject(classdecl) + " _" + funcMunged + "_ObjectNew"
  3661. 'Find decl we override
  3662. Local odecl:TFuncDecl=fdecl
  3663. While odecl.overrides
  3664. odecl=odecl.overrides
  3665. Wend
  3666. Local args:String
  3667. If Not classDecl.IsStruct() Then
  3668. args = "BBClass * clas"
  3669. End If
  3670. For Local i:Int=0 Until fdecl.argDecls.Length
  3671. Local arg:TArgDecl=fdecl.argDecls[i]
  3672. Local oarg:TArgDecl=odecl.argDecls[i]
  3673. MungDecl arg, True
  3674. If args args:+","
  3675. If Not TFunctionPtrType(oarg.ty) Then
  3676. If Not odecl.castTo Then
  3677. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  3678. Else
  3679. args:+ oarg.castTo + " " + arg.munged
  3680. End If
  3681. Else
  3682. If Not odecl.castTo Then
  3683. args:+TransType( oarg.ty, arg.munged )
  3684. Else
  3685. args:+ oarg.castTo
  3686. End If
  3687. End If
  3688. If arg.ty.EqualsType( oarg.ty ) Continue
  3689. Next
  3690. Emit t + Bra(args) + " {"
  3691. t = TransObject(classdecl) + " o"
  3692. If classDecl.IsStruct() Then
  3693. Emit t + ";"
  3694. Else
  3695. t :+ " = "
  3696. If ClassHasObjectField(classDecl) Then
  3697. t :+ "bbObjectNewNC"
  3698. Else
  3699. t :+ "bbObjectAtomicNewNC"
  3700. End If
  3701. Emit t + "(clas);"
  3702. End If
  3703. t = "_" + funcMunged
  3704. If classDecl.IsStruct() Then
  3705. t :+ "(&o"
  3706. Else
  3707. t :+ "(o"
  3708. End If
  3709. For Local i:Int=0 Until fdecl.argDecls.Length
  3710. Local arg:TArgDecl=fdecl.argDecls[i]
  3711. t :+ ", " + arg.munged
  3712. Next
  3713. Emit t + ");"
  3714. Emit "return o;"
  3715. Emit "}"
  3716. End Method
  3717. Method EmitClassDeclNewProto( classDecl:TClassDecl, fdecl:TFuncDecl )
  3718. Local classid$=classDecl.munged
  3719. Local superid$
  3720. If classDecl.superClass Then
  3721. superid = classDecl.superClass.actual.munged
  3722. End If
  3723. Local t:String = "void _"
  3724. If fdecl.argDecls.Length Then
  3725. If classDecl = fdecl.scope Then
  3726. t :+ fdecl.munged
  3727. Else
  3728. t :+ classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  3729. End If
  3730. Else
  3731. t :+ classid + "_New"
  3732. End If
  3733. 'Find decl we override
  3734. Local odecl:TFuncDecl=fdecl
  3735. While odecl.overrides
  3736. odecl=odecl.overrides
  3737. Wend
  3738. Local args:String = TransObject(classdecl, True) + " o"
  3739. For Local i:Int=0 Until fdecl.argDecls.Length
  3740. Local arg:TArgDecl=fdecl.argDecls[i]
  3741. Local oarg:TArgDecl=odecl.argDecls[i]
  3742. MungDecl arg, True
  3743. If args args:+","
  3744. If Not TFunctionPtrType(oarg.ty) Then
  3745. If Not odecl.castTo Then
  3746. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  3747. Else
  3748. args:+ oarg.castTo + " " + arg.munged
  3749. End If
  3750. Else
  3751. If Not odecl.castTo Then
  3752. args:+TransType( oarg.ty, arg.munged )
  3753. Else
  3754. args:+ oarg.castTo
  3755. End If
  3756. End If
  3757. If arg.ty.EqualsType( oarg.ty ) Continue
  3758. Next
  3759. Emit t + Bra(args) + ";"
  3760. End Method
  3761. Method EmitClassDeclObjectNewProto(classDecl:TClassDecl, fdecl:TFuncDecl)
  3762. Local t:String = TransObject(classdecl) + " _"
  3763. If classDecl = fdecl.scope Then
  3764. t :+ fdecl.munged
  3765. Else
  3766. t :+ classDecl.munged + "_" + fdecl.ident + MangleMethod(fdecl)
  3767. End If
  3768. t:+ "_ObjectNew"
  3769. 'Find decl we override
  3770. Local odecl:TFuncDecl=fdecl
  3771. While odecl.overrides
  3772. odecl=odecl.overrides
  3773. Wend
  3774. Local args:String
  3775. If Not classDecl.IsStruct() Then
  3776. args = "BBClass * clas"
  3777. End If
  3778. For Local i:Int=0 Until fdecl.argDecls.Length
  3779. Local arg:TArgDecl=fdecl.argDecls[i]
  3780. Local oarg:TArgDecl=odecl.argDecls[i]
  3781. MungDecl arg, True
  3782. If args args:+","
  3783. If Not TFunctionPtrType(oarg.ty) Then
  3784. If Not odecl.castTo Then
  3785. args:+TransType( oarg.ty, arg.munged )+" "+arg.munged
  3786. Else
  3787. args:+ oarg.castTo + " " + arg.munged
  3788. End If
  3789. Else
  3790. If Not odecl.castTo Then
  3791. args:+TransType( oarg.ty, arg.munged )
  3792. Else
  3793. args:+ oarg.castTo
  3794. End If
  3795. End If
  3796. If arg.ty.EqualsType( oarg.ty ) Continue
  3797. Next
  3798. Emit t + Bra(args) + ";"
  3799. End Method
  3800. Method EmitClassDeclDelete( classDecl:TClassDecl )
  3801. Local classid$=classDecl.munged
  3802. Local superid$=classDecl.superClass.actual.munged
  3803. ' New
  3804. ' If opt_issuperstrict Then
  3805. Emit "void _" + classid + "_Delete" + Bra(TransObject(classdecl) + " o") + " {"
  3806. ' Else
  3807. ' Emit "int _" + classid + "_Delete" + Bra(TransObject(classdecl) + " o") + " {"
  3808. ' End If
  3809. Local decl:TFuncDecl = classDecl.FindFuncDecl("delete",,,,,,SCOPE_CLASS_HEIRARCHY)
  3810. If decl Then
  3811. decl.Semant
  3812. EmitLocalDeclarations(decl)
  3813. EmitBlock decl
  3814. End If
  3815. ' field cleanup
  3816. For Local decl:TFieldDecl=EachIn classDecl.Decls()
  3817. ' String
  3818. If TStringType(decl.declTy) Then
  3819. Emit "BBRELEASE(" + TransFieldRef(decl, "o") + ")"
  3820. End If
  3821. ' object
  3822. ' TODO
  3823. Next
  3824. ' finally, call super delete
  3825. EmitClassDeclDeleteDtor(classDecl)
  3826. '
  3827. Emit "}"
  3828. End Method
  3829. Method EmitClassDeclDeleteDtor( classDecl:TClassDecl )
  3830. Local superid$=classDecl.superClass.actual.munged
  3831. If classDecl.superClass.ident = "Object" Or Not classHierarchyHasFunction(classDecl.superClass, "Delete") Then
  3832. Emit "bbObjectDtor(o);"
  3833. Else
  3834. Emit "_" + superid + "_Delete(o);"
  3835. End If
  3836. End Method
  3837. Method TransFieldRef:String(decl:TFieldDecl, variable:String, exprType:TType = Null)
  3838. Local s:String = variable
  3839. 'DebugStop
  3840. Local ind:String = "->"
  3841. If decl.scope And TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  3842. Local exprIsStruct:Int = TObjectType(exprType) And TObjectType(exprType).classDecl.attrs & CLASS_STRUCT
  3843. If exprType And (exprIsStruct Or Not IsPointerType(exprType)) And variable <> "o" Then
  3844. ind = "."
  3845. End If
  3846. End If
  3847. If variable.StartsWith("*") Then
  3848. variable = Bra(variable)
  3849. End If
  3850. ' Null test
  3851. If opt_debug
  3852. If TClassDecl(decl.scope) And TClassDecl(decl.scope).IsStruct() Then
  3853. '
  3854. Else
  3855. variable = TransDebugNullObjectError(variable, TClassDecl(decl.scope))
  3856. End If
  3857. End If
  3858. ' array.length
  3859. If decl.scope And decl.scope.ident = "___Array" Then
  3860. If decl.ident = "length" Then
  3861. Return Bra(variable + "->scales[0]")
  3862. End If
  3863. If decl.ident = "numberOfDimensions" Then
  3864. Return Bra(variable + "->dims")
  3865. End If
  3866. If decl.ident = "sizeMinusHeader" Then
  3867. Return Bra(variable + "->size")
  3868. End If
  3869. If decl.ident = "elementTypeEncoding" Then
  3870. Return Bra(variable + "->type")
  3871. End If
  3872. End If
  3873. ' string methods
  3874. If decl.scope And decl.scope.ident = "String" Then
  3875. If decl.ident = "length" Then
  3876. 'If exprType._flags & TType.T_VAR Then
  3877. ' Return Bra("(*" + variable + ")->length")
  3878. 'Else
  3879. If variable.StartsWith("&_s") Then
  3880. Return Bra(variable[1..] + ".length")
  3881. Else
  3882. Return Bra(variable + "->length")
  3883. End If
  3884. 'End If
  3885. End If
  3886. End If
  3887. 'If TObjectType(exprType) And (exprType._flags & TType.T_VAR) Then
  3888. ' ' get the object from the pointer
  3889. ' variable = Bra("*" + variable)
  3890. 'End If
  3891. If IsNumericType(decl.ty) Then
  3892. s = variable + ind + decl.munged + " "
  3893. Else If TStringType(decl.ty) Then
  3894. s = variable + ind + decl.munged + " "
  3895. Else If TObjectType(decl.ty) Then
  3896. s = variable + ind + decl.munged + " "
  3897. Else If IsPointerType(decl.ty, 0, TType.T_POINTER) Then
  3898. s = variable + ind + decl.munged + " "
  3899. Else If TFunctionPtrType(decl.ty) Then
  3900. s = variable + ind + decl.munged + " "
  3901. Else If TArrayType(decl.ty) Then
  3902. s = variable + ind + decl.munged + " "
  3903. End If
  3904. Return s
  3905. End Method
  3906. ' " _" + classDecl.actual.munged + "_" + decl.ident.ToLower(
  3907. Method TransIfcArgs:String(funcDecl:TFuncDecl)
  3908. Local args:String
  3909. If Not funcDecl.IsSemanted() Then
  3910. funcDecl.Semant()
  3911. End If
  3912. For Local i:Int=0 Until funcDecl.argDecls.Length
  3913. Local arg:TArgDecl = funcDecl.argDecls[i]
  3914. If args args:+","
  3915. args:+ arg.ident + TransIfcType( arg.ty )
  3916. If arg.init Then
  3917. If TInvokeExpr(arg.init) Then
  3918. args:+ "=" + Enquote(TInvokeExpr(arg.init).decl.munged)
  3919. Else
  3920. args:+ "=" + TransIfcConstExpr(arg.init)
  3921. End If
  3922. End If
  3923. Next
  3924. Return Bra(args)
  3925. End Method
  3926. Method EmitIfcClassFuncDecl(funcDecl:TFuncDecl)
  3927. funcDecl.Semant
  3928. Local func:String
  3929. ' method / function
  3930. If funcDecl.IsMethod() Or funcDecl.IsCTor() Then
  3931. func :+ "-"
  3932. Else
  3933. func :+ "+"
  3934. End If
  3935. If funcDecl.attrs & FUNC_OPERATOR Then
  3936. func :+ BmxEnquote(funcDecl.ident)
  3937. Else
  3938. func :+ funcDecl.ident
  3939. End If
  3940. func :+ TransIfcType(funcDecl.retType, funcDecl.ModuleScope().IsSuperStrict())
  3941. ' function args
  3942. func :+ TransIfcArgs(funcDecl)
  3943. If funcDecl.attrs & DECL_FINAL Then
  3944. func :+ "F"
  3945. Else If funcDecl.attrs & DECL_ABSTRACT Then
  3946. func :+ "A"
  3947. End If
  3948. If funcDecl.attrs & FUNC_OPERATOR Then
  3949. func :+ "O"
  3950. End If
  3951. If funcDecl.attrs & DECL_PRIVATE Then
  3952. func :+ "P"
  3953. Else If funcDecl.attrs & DECL_PROTECTED Then
  3954. func :+ "R"
  3955. End If
  3956. func :+ "="
  3957. func :+ Enquote(funcDecl.munged)
  3958. Emit func
  3959. End Method
  3960. Method EmitIfcFuncDecl(funcDecl:TFuncDecl)
  3961. Local func:String
  3962. func :+ funcDecl.ident
  3963. ' ensure the function has been semanted
  3964. funcDecl.Semant()
  3965. func :+ TransIfcType(funcDecl.retType, funcDecl.ModuleScope().IsSuperStrict())
  3966. ' function args
  3967. func :+ TransIfcArgs(funcDecl)
  3968. func :+ "="
  3969. func :+ Enquote(funcDecl.munged)
  3970. If funcDecl.castTo Then
  3971. func :+ ":" + funcDecl.castTo
  3972. func :+ " " + funcDecl.munged + "("
  3973. For Local i:Int = 0 Until funcDecl.argDecls.length
  3974. If i Then
  3975. func :+ ", "
  3976. End If
  3977. func :+ funcDecl.argDecls[i].castTo
  3978. Next
  3979. func :+ ")"
  3980. End If
  3981. Emit func
  3982. End Method
  3983. Method TransIfcConstExpr:String(expr:TExpr)
  3984. If Not expr.exprType Then
  3985. expr.Semant()
  3986. End If
  3987. If TStringType(expr.exprType) Then
  3988. Return "$" + EscapeChars(BmxEnquote(expr.Eval()))
  3989. EndIf
  3990. If TArrayType(expr.exprType) Then
  3991. Return Enquote("bbEmptyArray")
  3992. End If
  3993. If TFunctionPtrType(expr.exprType) Then
  3994. If TCastExpr(expr) Then
  3995. If TInvokeExpr(TCastExpr(expr).expr) Then
  3996. Return Enquote(TInvokeExpr(TCastExpr(expr).expr).decl.munged)
  3997. End If
  3998. If TNullExpr(TCastExpr(expr).expr) Then
  3999. Return Enquote("brl_blitz_NullFunctionError")
  4000. End If
  4001. End If
  4002. InternalErr
  4003. End If
  4004. If TObjectType(expr.exprType) Then
  4005. If TCastExpr(expr) Then
  4006. If TNullExpr(TCastExpr(expr).expr) Then
  4007. Return Enquote("bbNullObject")
  4008. End If
  4009. End If
  4010. End If
  4011. If IsPointerType(expr.exprType, 0, TType.T_POINTER) Then
  4012. If TCastExpr(expr) Then
  4013. If TNullExpr(TCastExpr(expr).expr) Then
  4014. Return "0"
  4015. End If
  4016. If TConstExpr(TCastExpr(expr).expr) Then
  4017. Return TConstExpr(TCastExpr(expr).expr).value
  4018. End If
  4019. End If
  4020. End If
  4021. If IsNumericType(expr.exprType) Then
  4022. Local s:String = expr.Eval()
  4023. If Not s Then
  4024. Return "0"
  4025. Else
  4026. If TDecimalType(expr.exprType) Then
  4027. Return s + TransIfcType(expr.exprType)
  4028. Else
  4029. Return s
  4030. End If
  4031. End If
  4032. EndIf
  4033. 'If TObjectType(expr.exprType) And TNullDecl(TObjectType(expr.exprType).classDecl) Then
  4034. ' Return Enquote("bbNullObject")
  4035. 'End If
  4036. End Method
  4037. Method EmitIfcConstDecl(constDecl:TConstDecl)
  4038. Local c:String
  4039. c = constDecl.ident + TransIfcType(constDecl.ty)
  4040. If TExpr(constDecl.init) Then
  4041. c:+ "=" + TransIfcConstExpr(TExpr(constDecl.init))
  4042. End If
  4043. Emit c
  4044. End Method
  4045. Method EmitIfcFieldDecl(fieldDecl:TFieldDecl)
  4046. Local f:String = "." + fieldDecl.ident + TransIfcType(fieldDecl.ty, fieldDecl.ModuleScope().IsSuperStrict())
  4047. f :+ "&"
  4048. If fieldDecl.IsPrivate() Then
  4049. f :+ "`"
  4050. Else If fieldDecl.IsProtected() Then
  4051. f :+ "``"
  4052. End If
  4053. Emit f
  4054. End Method
  4055. Method EmitIfcClassDecl(classDecl:TClassDecl)
  4056. Local head:String = classDecl.ident + "^"
  4057. If classDecl.superClass Then
  4058. head :+ classDecl.superClass.ident
  4059. Else
  4060. head :+ "Null"
  4061. End If
  4062. If classDecl.implments Then
  4063. head :+ "@"
  4064. For Local i:Int = 0 Until classDecl.implments.length
  4065. If i Then
  4066. head :+ ","
  4067. End If
  4068. head :+ classDecl.implments[i].ident
  4069. Next
  4070. End If
  4071. Emit head + "{", False
  4072. 'PushMungScope
  4073. BeginLocalScope
  4074. ' fields, globals and consts
  4075. ' For Local decl:TDecl=EachIn classDecl.Decls()
  4076. ' const
  4077. For Local cDecl:TConstDecl = EachIn classDecl.Decls()
  4078. cDecl.Semant()
  4079. EmitIfcConstDecl(cDecl)
  4080. Next
  4081. ' global
  4082. For Local gDecl:TGlobalDecl = EachIn classDecl.Decls()
  4083. gDecl.Semant()
  4084. EmitIfcGlobalDecl(gDecl)
  4085. Next
  4086. ' field
  4087. For Local fDecl:TFieldDecl = EachIn classDecl.Decls()
  4088. fDecl.Semant()
  4089. EmitIfcFieldDecl(fDecl)
  4090. Next
  4091. ' functions
  4092. If Not classDecl.IsExtern() Then
  4093. Emit "-New()=" + Enquote("_" + classDecl.munged + "_New")
  4094. If classHierarchyHasFunction(classDecl, "Delete") Then
  4095. Emit "-Delete()=" + Enquote("_" + classDecl.munged + "_Delete")
  4096. End If
  4097. For Local decl:TDecl=EachIn classDecl.Decls()
  4098. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4099. If fdecl
  4100. If Not equalsIfcBuiltInFunc(classDecl, fdecl) Then
  4101. EmitIfcClassFuncDecl fdecl
  4102. End If
  4103. Continue
  4104. EndIf
  4105. Next
  4106. Local flags:String
  4107. If classDecl.IsAbstract() Then
  4108. flags :+ "A"
  4109. End If
  4110. If classDecl.attrs & DECL_FINAL Then
  4111. flags :+ "F"
  4112. End If
  4113. If classDecl.attrs & CLASS_INTERFACE Then
  4114. flags :+ "I"
  4115. Else If classDecl.IsStruct() Then
  4116. flags :+ "S"
  4117. End If
  4118. Emit "}" + flags + "=" + Enquote(classDecl.munged), False
  4119. Else
  4120. For Local decl:TDecl=EachIn classDecl.Decls()
  4121. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4122. If fdecl
  4123. EmitIfcClassFuncDecl fdecl
  4124. Continue
  4125. EndIf
  4126. Next
  4127. Local flags:String = "E"
  4128. If classDecl.IsInterface() Then
  4129. flags :+ "I"
  4130. Else If classDecl.IsStruct() Then
  4131. flags :+ "S"
  4132. End If
  4133. If classDecl.attrs & DECL_API_WIN32 Then
  4134. flags :+ "W"
  4135. End If
  4136. Emit "}" + flags + "=0", False
  4137. End If
  4138. 'PopMungScope
  4139. EndLocalScope
  4140. End Method
  4141. Method EmitIfcGlobalDecl(globalDecl:TGlobalDecl)
  4142. globalDecl.Semant
  4143. Local g:String = globalDecl.ident
  4144. g:+ TransIfcType(globalDecl.ty, globalDecl.ModuleScope().IsSuperStrict())
  4145. g:+ "&"
  4146. If globalDecl.IsPrivate() Then
  4147. g :+ "`"
  4148. Else If globalDecl.IsProtected() Then
  4149. g :+ "``"
  4150. End If
  4151. g :+ "="
  4152. g :+ "mem:p("
  4153. If TFunctionPtrType(globalDecl.ty) Then
  4154. g :+ Enquote(TFunctionPtrType(globalDecl.ty).func.munged)
  4155. Else
  4156. g :+ Enquote(globalDecl.munged)
  4157. End If
  4158. g :+ ")"
  4159. Emit g
  4160. End Method
  4161. Method EmitModuleInclude(moduleDecl:TModuleDecl, included:TMap = Null)
  4162. If moduleDecl.filepath Then
  4163. ' a module import
  4164. If FileType(moduleDecl.filepath) = FILETYPE_DIR Or (opt_ismain And moduleDecl.ident = opt_modulename) Then
  4165. Local inc:String = ModuleHeaderFromIdent(moduleDecl.ident, True)
  4166. If Not included Or (included And Not included.Contains(inc)) Then
  4167. Emit "#include <" + inc + ">"
  4168. If included Then
  4169. included.Insert(inc, inc)
  4170. End If
  4171. End If
  4172. Else
  4173. ' a file import...
  4174. Local inc:String = FileHeaderFromFile(moduleDecl, False)
  4175. If Not included Or (included And Not included.Contains(inc)) Then
  4176. Emit "#include ~q" + inc + "~q"
  4177. If included Then
  4178. included.Insert(inc, inc)
  4179. End If
  4180. End If
  4181. End If
  4182. ' DebugLog moduleDecl.filepath
  4183. End If
  4184. End Method
  4185. Method EmitModuleInit(moduleDecl:TModuleDecl)
  4186. If moduleDecl.filepath Then
  4187. ' a module import
  4188. If FileType(moduleDecl.filepath) = FILETYPE_DIR Then
  4189. Emit MungModuleName(moduleDecl) + "();"
  4190. Else
  4191. ' maybe a file import...
  4192. Emit MungImportFromFile(moduleDecl) + "();"
  4193. End If
  4194. End If
  4195. End Method
  4196. Method EmitIncBinFile(ib:TIncbin)
  4197. If FileType(ib.path) = FILETYPE_FILE Then
  4198. Local ident:String = _appInstance.munged + "_ib_" + ib.id
  4199. Local buf:Byte[] = LoadByteArray(ib.path)
  4200. ib.length = buf.length
  4201. Emit "unsigned char " + ident + "[] = {"
  4202. Local s:String
  4203. Local hx:Short[2]
  4204. For Local i:Int = 0 Until buf.length
  4205. Local val:Int = buf[i]
  4206. For Local k:Int=1 To 0 Step -1
  4207. Local n:Int=(val&15)+48
  4208. If n>57 n:+39
  4209. hx[k]=n
  4210. val:Shr 4
  4211. Next
  4212. s :+ "0x" + String.FromShorts( hx,2 )
  4213. s :+ ","
  4214. If s.length > 80 Then
  4215. Emit s
  4216. s = ""
  4217. End If
  4218. Next
  4219. Emit s
  4220. Emit "};"
  4221. End If
  4222. End Method
  4223. Method TransHeader(app:TAppDecl)
  4224. SetOutput("head")
  4225. _app = app
  4226. prefix = app.GetPathPrefix()
  4227. ' TODO
  4228. If Not opt_apptype Then
  4229. app.mainFunc.munged="bb_localmain"
  4230. Else
  4231. app.mainFunc.munged="bb_main"
  4232. End If
  4233. ' track what's been included so far - avoid duplicates
  4234. Local included:TMap = New TMap
  4235. For Local decl:TModuleDecl=EachIn app.imported.Values()
  4236. For Local mdecl:TDecl=EachIn decl.imported.Values()
  4237. MungDecl mdecl
  4238. 'skip mdecls we are not interested in
  4239. If Not TModuleDecl(mdecl) Then Continue
  4240. If app.mainModule = mdecl Then Continue
  4241. If mdecl.ident = "brl.classes" Then Continue
  4242. If mdecl.ident = "brl.blitzkeywords" Then Continue
  4243. EmitModuleInclude(TModuleDecl(mdecl), included)
  4244. Next
  4245. Next
  4246. For Local header:String=EachIn app.headers
  4247. Emit "#include ~q../" + header + "~q"
  4248. Next
  4249. Emit "int " + app.munged + "();"
  4250. For Local decl:TDecl=EachIn app.Semanted()
  4251. If decl.declImported Continue
  4252. MungDecl decl
  4253. Local cdecl:TClassDecl=TClassDecl( decl )
  4254. If Not cdecl Continue
  4255. ' mung, but don't emit
  4256. ' Emit prefix + decl.munged+";"
  4257. 'PushMungScope
  4258. funcMungs = New TMap
  4259. BeginLocalScope
  4260. For Local decl:TDecl=EachIn cdecl.Semanted()
  4261. MungDecl decl
  4262. cdecl.SemantParts()
  4263. Next
  4264. EndLocalScope
  4265. 'PopMungScope
  4266. Next
  4267. ' forward declarations
  4268. For Local decl:TClassDecl=EachIn app.Semanted()
  4269. If decl.declImported Or (decl.IsExtern() And Not decl.IsStruct()) Continue
  4270. If Not decl.IsStruct()
  4271. Emit "struct " + decl.munged + "_obj;"
  4272. Else
  4273. Emit "struct " + decl.munged + ";"
  4274. End If
  4275. If decl.IsInterface() Then
  4276. Emit "extern const struct BBInterface " + decl.munged + "_ifc;"
  4277. End If
  4278. Next
  4279. 'prototypes/header! - structs first
  4280. For Local decl:TDecl=EachIn app.Semanted()
  4281. If decl.declImported Continue
  4282. Local cdecl:TClassDecl=TClassDecl( decl )
  4283. If cdecl
  4284. If cdecl.IsStruct() Then
  4285. EmitStructClassProto cdecl
  4286. End If
  4287. EndIf
  4288. Next
  4289. 'prototypes/header!
  4290. For Local decl:TDecl=EachIn app.Semanted()
  4291. If decl.declImported Continue
  4292. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  4293. If gdecl
  4294. MungDecl gdecl
  4295. If Not TFunctionPtrType(gdecl.ty) Then
  4296. If Not gdecl.IsPrivate() Then
  4297. Emit "extern "+TransRefType( gdecl.ty, "" )+" "+gdecl.munged+";" 'forward reference...
  4298. End If
  4299. Else
  4300. If Not TFunctionPtrType(gdecl.ty).func.noCastGen Then
  4301. ' generate function pointer refs if we haven't been told not to
  4302. ' If Not gdecl.IsExtern() Then
  4303. Emit TransRefType( gdecl.ty, gdecl.munged )+";" 'forward reference...
  4304. ' End If
  4305. End If
  4306. End If
  4307. Continue
  4308. EndIf
  4309. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4310. If fdecl' And Not fdecl.IsExtern()
  4311. ' don't include the main function - it's handled separately
  4312. If fdecl = app.mainFunc Then
  4313. Continue
  4314. End If
  4315. EmitGDBDebug(fdecl)
  4316. EmitFuncDecl( fdecl, True)
  4317. Continue
  4318. EndIf
  4319. Local cdecl:TClassDecl=TClassDecl( decl )
  4320. If cdecl
  4321. If Not cdecl.IsStruct() Then
  4322. If Not cdecl.IsExtern()
  4323. EmitClassProto cdecl
  4324. Else
  4325. EmitExternClassProto cdecl
  4326. End If
  4327. 'Else
  4328. ' EmitStructClassProto cdecl
  4329. End If
  4330. 'Continue
  4331. EndIf
  4332. Next
  4333. End Method
  4334. Method IncBinRequiresRebuild:Int(file:String, incbins:TList)
  4335. ' file doesn't exist?
  4336. If Not FileType(file) Then
  4337. Return True
  4338. End If
  4339. Local timestamp:Int = FileTime(file)
  4340. ' file exists... read header and compare names
  4341. ' read lines until "// ----"
  4342. ' TODO
  4343. Local files:TList = New TList
  4344. Local stream:TStream = ReadFile(file)
  4345. While True
  4346. Local s:String = ReadLine(stream)
  4347. If Not s.StartsWith("// ") Or s.StartsWith("// ----") Then
  4348. Exit
  4349. End If
  4350. Local ind:Int = s.Find("// FILE : ")
  4351. If ind = 0 Then
  4352. files.AddLast(s[10..].Replace("~q",""))
  4353. End If
  4354. Wend
  4355. stream.Close()
  4356. ' different number of files?
  4357. If files.Count() <> incbins.Count() Then
  4358. Return True
  4359. End If
  4360. ' different file names?
  4361. Local count:Int
  4362. For Local s:String = EachIn files
  4363. For Local ib:TIncbin = EachIn incbins
  4364. If s = ib.file Then
  4365. count :+ 1
  4366. Exit
  4367. End If
  4368. Next
  4369. Next
  4370. If count <> files.count() Then
  4371. Return True
  4372. End If
  4373. count = 0
  4374. For Local ib:TIncbin = EachIn incbins
  4375. For Local s:String = EachIn files
  4376. If s = ib.file Then
  4377. count :+ 1
  4378. Exit
  4379. End If
  4380. Next
  4381. Next
  4382. If count <> incbins.count() Then
  4383. Return True
  4384. End If
  4385. For Local ib:TIncbin = EachIn incbins
  4386. If timestamp < FileTime(ib.path) Then
  4387. Return True
  4388. End If
  4389. ' set the length, as we will need this later if we aren't loading the files now.
  4390. ib.length = FileSize(ib.path)
  4391. Next
  4392. Return False
  4393. End Method
  4394. Method TransIncBin(app:TAppDecl)
  4395. If app.incbins.Count() > 0 Then
  4396. SetOutput("incbin")
  4397. Local mung:String = FileMung(False)
  4398. Local name:String = StripAll(app.mainModule.filepath)
  4399. Local file:String = name + ".bmx" + mung + ".incbin.h"
  4400. Local filepath:String = OutputFilePath(opt_filepath, mung, "incbin.h")
  4401. If IncBinRequiresRebuild(filepath, app.incbins) Then
  4402. app.genIncBinHeader = True
  4403. For Local ib:TIncbin = EachIn app.incbins
  4404. Emit "// FILE : " + Enquote(ib.file)
  4405. Next
  4406. Emit "// ----"
  4407. For Local ib:TIncbin = EachIn app.incbins
  4408. EmitIncBinFile(ib)
  4409. Next
  4410. End If
  4411. SetOutput("pre_source")
  4412. Emit "#include ~q" + file + "~q"
  4413. End If
  4414. End Method
  4415. Method TransGlobalInit(decl:TGlobalDecl)
  4416. If TFunctionPtrType(decl.ty) Then
  4417. If TInvokeExpr(decl.init) And Not TInvokeExpr(decl.init).invokedWithBraces Then
  4418. Emit TransGlobal( decl )+"="+TInvokeExpr(decl.init).decl.munged + ";"
  4419. Else
  4420. Emit TransGlobal( decl )+"="+decl.init.Trans()+";"
  4421. End If
  4422. Else
  4423. If Not decl.funcGlobal Then
  4424. Emit TransGlobal( decl )+"="+decl.init.Trans()+";"
  4425. End If
  4426. End If
  4427. End Method
  4428. Method TransSource(app:TAppDecl)
  4429. SetOutput("pre_source")
  4430. ' include our header
  4431. EmitModuleInclude(app.mainModule)
  4432. ' incbins
  4433. TransIncBin(app)
  4434. SetOutput("source")
  4435. ' Private Global declarations
  4436. ' since we don't declare them in the header, they need to be near the top of the source
  4437. For Local decl:TDecl=EachIn app.Semanted()
  4438. If decl.declImported Continue
  4439. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  4440. If gdecl And gdecl.IsPrivate() Then
  4441. If Not TFunctionPtrType(gdecl.ty) Then
  4442. If TConstExpr(gdecl.init) Then
  4443. Emit TransRefType( gdecl.ty, "WW" )+" "+TransGlobalDecl(gdecl)+";"
  4444. gdecl.inited = True
  4445. Else
  4446. If Not gdecl.IsExtern() Then
  4447. Emit TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+";"
  4448. Else
  4449. ' delcare in source for any references to it locally in this module
  4450. Emit "extern "+TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+";"
  4451. End If
  4452. End If
  4453. Else
  4454. 'Emit TransRefType( gdecl.ty, gdecl.munged ) + ";"
  4455. End If
  4456. Continue
  4457. EndIf
  4458. Next
  4459. For Local gdecl:TGlobalDecl=EachIn app.SemantedGlobals
  4460. If gdecl And gdecl.funcGlobal Then
  4461. MungDecl gdecl
  4462. If Not TFunctionPtrType(gdecl.ty) Then
  4463. Emit "static " + TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+";"
  4464. Else
  4465. Emit "static " + TransRefType( gdecl.ty, gdecl.munged ) + ";"
  4466. End If
  4467. Continue
  4468. End If
  4469. Next
  4470. 'definitions!
  4471. For Local decl:TDecl=EachIn app.Semanted()
  4472. If decl.declImported Continue
  4473. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  4474. If gdecl
  4475. If Not TFunctionPtrType(gdecl.ty) And Not gdecl.IsPrivate() Then
  4476. If TConstExpr(gdecl.init) Then
  4477. Emit TransRefType( gdecl.ty, "WW" )+" "+TransGlobalDecl(gdecl)+";"
  4478. gdecl.inited = True
  4479. Else
  4480. If Not gdecl.IsExtern() Then
  4481. Emit TransRefType( gdecl.ty, "WW" )+" "+gdecl.munged+ "=" + TransValue(gdecl.ty, "") + ";"
  4482. End If
  4483. End If
  4484. Else
  4485. 'Emit TransRefType( gdecl.ty, gdecl.munged ) + ";"
  4486. End If
  4487. Continue
  4488. EndIf
  4489. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4490. If fdecl And Not fdecl.IsExtern()
  4491. ' don't include the main function - it's handled separately
  4492. If fdecl = app.mainFunc Then
  4493. Continue
  4494. End If
  4495. EmitGDBDebug(fdecl)
  4496. EmitFuncDecl fdecl
  4497. Continue
  4498. EndIf
  4499. Local cdecl:TClassDecl=TClassDecl( decl )
  4500. If cdecl
  4501. EmitGDBDebug(cdecl)
  4502. EmitClassDecl cdecl
  4503. Continue
  4504. EndIf
  4505. Next
  4506. Emit "static int " + app.munged + "_inited" + " = 0;"
  4507. Emit "int " + app.munged + "(){"
  4508. ' initialise stuff
  4509. Emit "if (!" + app.munged + "_inited) {"
  4510. Emit app.munged + "_inited = 1;"
  4511. ' register incbins
  4512. For Local ib:TIncbin = EachIn app.incbins
  4513. Emit "bbIncbinAdd(&" + TStringConst(app.stringConsts.ValueForKey(ib.file)).id + ",&" + app.munged + "_ib_" + ib.id + "," + ib.length + ");"
  4514. Next
  4515. Local importOnce:TMap = New TMap
  4516. ' call any imported mod inits
  4517. For Local decl:TModuleDecl=EachIn app.imported.Values()
  4518. For Local mdecl:TDecl=EachIn decl.imported.Values()
  4519. If TModuleDecl(mdecl) And app.mainModule <> mdecl And mdecl.ident <> "brl.classes" And mdecl.ident <> "brl.blitzkeywords" Then
  4520. If Not importOnce.Contains(mdecl.ident) Then
  4521. EmitModuleInit(TModuleDecl(mdecl))
  4522. importOnce.Insert(mdecl.ident, "")
  4523. End If
  4524. End If
  4525. Next
  4526. Next
  4527. ' register types
  4528. For Local decl:TDecl=EachIn app.Semanted()
  4529. If decl.declImported Continue
  4530. Local cdecl:TClassDecl=TClassDecl( decl )
  4531. If cdecl And Not cdecl.IsExtern()
  4532. If Not cdecl.IsInterface() Then
  4533. If Not cdecl.IsStruct() Then
  4534. Emit "bbObjectRegisterType(&" + cdecl.munged + ");"
  4535. Else
  4536. Emit "bbObjectRegisterStruct(&" + cdecl.munged + "_scope);"
  4537. End If
  4538. Else
  4539. Emit "bbObjectRegisterInterface(&" + cdecl.munged + "_ifc);"
  4540. End If
  4541. EndIf
  4542. Next
  4543. '
  4544. ' defdata init
  4545. If Not app.dataDefs.IsEmpty() Then
  4546. Emit "_defDataOffset = &_defData;"
  4547. End If
  4548. ' initialise globals
  4549. For Local decl:TGlobalDecl=EachIn app.semantedGlobals
  4550. If decl.declImported Continue
  4551. decl.Semant
  4552. ' TODO : what about OnDebugStop etc, who have no init ?
  4553. If decl.init And Not (decl.attrs & DECL_INITONLY) Then
  4554. If decl.scope And TClassDecl(decl.scope) Then
  4555. ' class global inits need to be generated in the correct order.
  4556. ' only generate global inits if the parent class hasn't already been processed,
  4557. ' otherwise, we will skip this global as it should already have been generated.
  4558. If Not TClassDecl(decl.scope).globInit Then
  4559. TClassDecl(decl.scope).globInit = True
  4560. For Local gdecl:TGlobalDecl = EachIn decl.scope._decls
  4561. If gdecl.declImported Continue
  4562. gdecl.Semant
  4563. If gdecl.init And Not (gdecl.attrs & DECL_INITONLY) Then
  4564. TransGlobalInit(gdecl)
  4565. End If
  4566. Next
  4567. End If
  4568. Else
  4569. TransGlobalInit(decl)
  4570. End If
  4571. End If
  4572. Next
  4573. ' now do the local main stuff
  4574. app.mainFunc.Semant()
  4575. EmitLocalDeclarations(app.mainFunc)
  4576. EmitBlock app.mainFunc
  4577. Emit "}"
  4578. Emit "return 0;"
  4579. Emit "}"
  4580. ' redirect string generation to the top of the source
  4581. SetOutput("pre_source")
  4582. ' strings
  4583. For Local s:String = EachIn app.stringConsts.Keys()
  4584. If s Then
  4585. Local key:TStringConst = TStringConst(app.stringConsts.ValueForKey(s))
  4586. If key.count > 0 Then
  4587. Emit "static BBString " + key.id + "={"
  4588. Emit "&bbStringClass,"
  4589. 'Emit "2147483647,"
  4590. Emit s.length + ","
  4591. Local t:String = "{"
  4592. For Local i:Int = 0 Until s.length
  4593. If i Then
  4594. t:+ ","
  4595. End If
  4596. t:+ s[i]
  4597. If i And Not (i Mod 16) Then
  4598. Emit t
  4599. t = ""
  4600. End If
  4601. Next
  4602. Emit t + "}"
  4603. Emit "};"
  4604. End If
  4605. End If
  4606. Next
  4607. ' defdata
  4608. EmitDefDataArray(app)
  4609. ' scope defs
  4610. If Not app.scopedefs.IsEmpty() Then
  4611. For Local val:String = EachIn app.scopedefs.Keys()
  4612. Local i:Int = val.ToInt()
  4613. Emit "struct BBDebugScope_" + i + "{int kind; const char *name; BBDebugDecl decls[" + (i + 1) + "]; };"
  4614. Next
  4615. End If
  4616. End Method
  4617. Method EmitDefDataArray(app:TAppDecl)
  4618. If Not app.dataDefs.IsEmpty() Then
  4619. '
  4620. Emit "static struct bbDataDef * _defDataOffset;"
  4621. Emit "static struct bbDataDef _defData[" + TDefDataDecl.count + "]={"
  4622. For Local decl:TDefDataDecl = EachIn app.dataDefs
  4623. EmitDefData(decl)
  4624. Next
  4625. Emit "};"
  4626. End If
  4627. End Method
  4628. Method EmitDefData(decl:TDefDataDecl)
  4629. For Local i:Int = 0 Until decl.data.length
  4630. Local expr:TExpr = decl.data[i]
  4631. Emit "{"
  4632. Emit TransDefDataType(expr.exprType) + ","
  4633. Emit "{"
  4634. Emit "." + TransDefDataUnionType(expr.exprType) + " = " + expr.Trans()
  4635. Emit "}"
  4636. Emit "},"
  4637. Next
  4638. End Method
  4639. Method EmitIfcImports(impMod:TModuleDecl, processed:TMap)
  4640. For Local decl:TDecl=EachIn impMod.imported.Values()
  4641. Local mdecl:TModuleDecl=TModuleDecl( decl )
  4642. If mdecl And mdecl.ident <> "brl.classes" And mdecl.ident <> "brl.blitzkeywords" Then
  4643. If mdecl.filepath.EndsWith(".bmx")
  4644. If _appInstance.mainModule<>mdecl
  4645. EmitIfcImports(mdecl, processed)
  4646. For Local s:String = EachIn mdecl.fileImports
  4647. If Not processed.Contains("XX" + s + "XX") Then
  4648. Emit "import " + BmxEnquote(s)
  4649. processed.Insert("XX" + s + "XX", "")
  4650. End If
  4651. Next
  4652. End If
  4653. Else
  4654. If Not processed.Contains(mdecl.ident)
  4655. Emit "import " + mdecl.ident
  4656. processed.Insert(mdecl.ident, "")
  4657. End If
  4658. End If
  4659. End If
  4660. Next
  4661. End Method
  4662. Method EmitIfcStructImports(impMod:TModuleDecl, processed:TMap)
  4663. For Local decl:TDecl=EachIn impMod.imported.Values()
  4664. Local mdecl:TModuleDecl=TModuleDecl( decl )
  4665. If mdecl Then
  4666. If mdecl.filepath.EndsWith(".bmx") And _appInstance.mainModule<>mdecl And Not processed.Contains(mdecl)
  4667. EmitIfcStructImports(mdecl, processed)
  4668. processed.Insert(mdecl, mdecl)
  4669. For Local decl:TDecl=EachIn mdecl._decls
  4670. decl.Semant
  4671. ' consts
  4672. Local cdecl:TConstDecl=TConstDecl( decl )
  4673. If cdecl
  4674. EmitIfcConstDecl(cdecl)
  4675. Continue
  4676. End If
  4677. ' classes
  4678. Local tdecl:TClassDecl=TClassDecl( decl )
  4679. If tdecl
  4680. EmitIfcClassDecl(tdecl)
  4681. Continue
  4682. EndIf
  4683. ' functions
  4684. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4685. If fdecl And fdecl <> _appInstance.mainFunc Then
  4686. EmitIfcFuncDecl(fdecl)
  4687. Continue
  4688. End If
  4689. ' globals
  4690. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  4691. If gdecl
  4692. EmitIfcGlobalDecl(gdecl)
  4693. Continue
  4694. End If
  4695. Next
  4696. End If
  4697. End If
  4698. Next
  4699. End Method
  4700. Method FileHeaderFromFile:String(mdecl:TModuleDecl, includePath:Int = False)
  4701. Local name:String = StripAll(mdecl.filepath)
  4702. Local dir:String = ExtractDir(mdecl.filePath)
  4703. Local file:String = name + ".bmx" + FileMung(opt_apptype And (Not mdecl.declImported)) + ".h"
  4704. If mdecl.relPath Then
  4705. Local dir:String = ExtractDir(mdecl.relPath)
  4706. If dir Then
  4707. file = "../" + dir + "/.bmx/" + file
  4708. End If
  4709. End If
  4710. Return file
  4711. End Method
  4712. Method MungImportFromFile:String(mdecl:TModuleDecl)
  4713. Local result:String
  4714. If opt_buildtype <> BUILDTYPE_MODULE Then
  4715. Local dir:String = ExtractDir(mdecl.filepath).ToLower()
  4716. dir = dir[dir.findLast("/") + 1..]
  4717. If dir.EndsWith(".mod") Then
  4718. dir = dir.Replace(".mod", "")
  4719. End If
  4720. Local file:String = StripDir(mdecl.filepath).ToLower()
  4721. result = "_bb_" + dir + "_" + StripExt(file)
  4722. Else
  4723. result = "_bb_" + mdecl.ident
  4724. End If
  4725. 'return with all non-allowed chars (like "-" or " ") removed
  4726. Return TStringHelper.Sanitize(result)
  4727. End Method
  4728. Method TransInterface(app:TAppDecl)
  4729. SetOutput("interface")
  4730. If app.mainModule.IsSuperStrict() Then
  4731. Emit "superstrict"
  4732. End If
  4733. ' module info
  4734. For Local info:String = EachIn app.mainModule.modInfo
  4735. Emit "ModuleInfo " + BmxEnquote(info)
  4736. Next
  4737. Local processed:TMap = New TMap
  4738. ' module imports
  4739. For Local decl:TDecl=EachIn app.mainModule.imported.Values()
  4740. Local mdecl:TModuleDecl=TModuleDecl( decl )
  4741. If mdecl Then
  4742. If mdecl.IsActualModule() Then
  4743. Emit "import " + mdecl.ident
  4744. processed.Insert(mdecl.ident, "")
  4745. Else If Not opt_ismain And mdecl.filepath.EndsWith(".bmx") And app.mainModule<>mdecl
  4746. Local file:String = StripDir(mdecl.filepath)
  4747. If mdecl.relPath Then
  4748. Local dir:String = ExtractDir(mdecl.relPath)
  4749. If dir Then
  4750. file = dir + "/" + file
  4751. End If
  4752. End If
  4753. If Not processed.Contains(file) Then
  4754. Emit "import " + Enquote(file)
  4755. processed.Insert(file, "")
  4756. End If
  4757. End If
  4758. End If
  4759. Next
  4760. ' module imports from other files?
  4761. If opt_buildtype = BUILDTYPE_MODULE And opt_ismain Then
  4762. EmitIfcImports(app.mainModule, processed)
  4763. End If
  4764. ' other imports
  4765. For Local s:String = EachIn app.fileImports
  4766. Emit "import " + BmxEnquote(s)
  4767. Next
  4768. processed = New TMap
  4769. ' imported module structure (consts, classes, functions, etc)
  4770. If opt_buildtype = BUILDTYPE_MODULE And opt_ismain Then
  4771. EmitIfcStructImports(app.mainModule, processed)
  4772. End If
  4773. ' consts
  4774. For Local decl:TDecl=EachIn app.Semanted()
  4775. If decl.IsPrivate() Continue
  4776. Local cdecl:TConstDecl=TConstDecl( decl )
  4777. If cdecl And Not cdecl.declImported
  4778. EmitIfcConstDecl(cdecl)
  4779. End If
  4780. Next
  4781. ' classes
  4782. For Local decl:TDecl=EachIn app.Semanted()
  4783. If decl.IsPrivate() Continue
  4784. Local cdecl:TClassDecl=TClassDecl( decl )
  4785. If cdecl And Not cdecl.declImported
  4786. EmitIfcClassDecl(cdecl)
  4787. EndIf
  4788. Next
  4789. ' functions
  4790. For Local decl:TDecl=EachIn app.Semanted()
  4791. If decl.IsPrivate() Continue
  4792. Local fdecl:TFuncDecl=TFuncDecl( decl )
  4793. If fdecl And fdecl <> app.mainFunc And Not fdecl.declImported Then
  4794. EmitIfcFuncDecl(fdecl)
  4795. End If
  4796. Next
  4797. ' globals
  4798. For Local decl:TDecl=EachIn app.Semanted()
  4799. If decl.IsPrivate() Continue
  4800. Local gdecl:TGlobalDecl=TGlobalDecl( decl )
  4801. If gdecl And Not gdecl.declImported
  4802. EmitIfcGlobalDecl(gdecl)
  4803. End If
  4804. Next
  4805. End Method
  4806. Method TransApp( app:TAppDecl )
  4807. If app.mainModule.IsSuperStrict()
  4808. opt_issuperstrict = True
  4809. End If
  4810. TransHeader(app)
  4811. TransSource(app)
  4812. TransInterface(app)
  4813. End Method
  4814. End Type