ctranslator.bmx 221 KB

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