ctranslator.bmx 207 KB

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