ctranslator.bmx 233 KB

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