symdef.pas 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:asizeint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function OwnerHierarchyName: string; override;
  69. function in_currentunit: boolean;
  70. { regvars }
  71. function is_intregable : boolean;
  72. function is_fpuregable : boolean;
  73. { generics }
  74. procedure initgeneric;
  75. private
  76. savesize : asizeuint;
  77. end;
  78. tfiletyp = (ft_text,ft_typed,ft_untyped);
  79. tfiledef = class(tstoreddef)
  80. filetyp : tfiletyp;
  81. typedfiledef : tdef;
  82. typedfiledefderef : tderef;
  83. constructor createtext;
  84. constructor createuntyped;
  85. constructor createtyped(def : tdef);
  86. constructor ppuload(ppufile:tcompilerppufile);
  87. function getcopy : tstoreddef;override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderef;override;
  90. procedure deref;override;
  91. function GetTypeName:string;override;
  92. function getmangledparaname:string;override;
  93. procedure setsize;
  94. end;
  95. tvariantdef = class(tstoreddef)
  96. varianttype : tvarianttype;
  97. constructor create(v : tvarianttype);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. function getcopy : tstoreddef;override;
  100. function GetTypeName:string;override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. function getvardef:longint;override;
  103. procedure setsize;
  104. function is_publishable : boolean;override;
  105. function needs_inittable : boolean;override;
  106. end;
  107. tformaldef = class(tstoreddef)
  108. typed:boolean;
  109. constructor create(Atyped:boolean);
  110. constructor ppuload(ppufile:tcompilerppufile);
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. function GetTypeName:string;override;
  113. end;
  114. tforwarddef = class(tstoreddef)
  115. tosymname : pshortstring;
  116. forwardpos : tfileposinfo;
  117. constructor create(const s:string;const pos:tfileposinfo);
  118. destructor destroy;override;
  119. function getcopy:tstoreddef;override;
  120. function GetTypeName:string;override;
  121. end;
  122. tundefineddef = class(tstoreddef)
  123. constructor create;
  124. constructor ppuload(ppufile:tcompilerppufile);
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. function GetTypeName:string;override;
  127. end;
  128. terrordef = class(tstoreddef)
  129. constructor create;
  130. procedure ppuwrite(ppufile:tcompilerppufile);override;
  131. function GetTypeName:string;override;
  132. function getmangledparaname : string;override;
  133. end;
  134. tabstractpointerdef = class(tstoreddef)
  135. pointeddef : tdef;
  136. pointeddefderef : tderef;
  137. constructor create(dt:tdeftyp;def:tdef);
  138. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. procedure buildderef;override;
  141. procedure deref;override;
  142. end;
  143. tpointerdef = class(tabstractpointerdef)
  144. is_far : boolean;
  145. has_pointer_math : boolean;
  146. constructor create(def:tdef);
  147. constructor createfar(def:tdef);
  148. function getcopy:tstoreddef;override;
  149. constructor ppuload(ppufile:tcompilerppufile);
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function GetTypeName:string;override;
  152. end;
  153. tprocdef = class;
  154. { tabstractrecorddef }
  155. tabstractrecorddef= class(tstoreddef)
  156. objname,
  157. objrealname : PShortString;
  158. symtable : TSymtable;
  159. cloneddef : tabstractrecorddef;
  160. cloneddefderef : tderef;
  161. objectoptions : tobjectoptions;
  162. constructor create(const n:string; dt:tdeftyp);
  163. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. destructor destroy; override;
  166. procedure check_forwards; virtual;
  167. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  168. function GetSymtable(t:tGetSymtable):TSymtable;override;
  169. function is_packed:boolean;
  170. function RttiName: string;
  171. { enumerator support }
  172. function search_enumerator_get: tprocdef; virtual;
  173. function search_enumerator_move: tprocdef; virtual;
  174. function search_enumerator_current: tsym; virtual;
  175. end;
  176. trecorddef = class(tabstractrecorddef)
  177. public
  178. isunion : boolean;
  179. constructor create(const n:string; p:TSymtable);
  180. constructor ppuload(ppufile:tcompilerppufile);
  181. destructor destroy;override;
  182. function getcopy : tstoreddef;override;
  183. procedure ppuwrite(ppufile:tcompilerppufile);override;
  184. procedure buildderef;override;
  185. procedure deref;override;
  186. function size:asizeint;override;
  187. function alignment : shortint;override;
  188. function padalignment: shortint;
  189. function GetTypeName:string;override;
  190. { debug }
  191. function needs_inittable : boolean;override;
  192. end;
  193. tobjectdef = class;
  194. { TImplementedInterface }
  195. TImplementedInterface = class
  196. IntfDef : tobjectdef;
  197. IntfDefDeref : tderef;
  198. IType : tinterfaceentrytype;
  199. IOffset : longint;
  200. VtblImplIntf : TImplementedInterface;
  201. NameMappings : TFPHashList;
  202. ProcDefs : TFPObjectList;
  203. ImplementsGetter : tsym;
  204. constructor create(aintf: tobjectdef);
  205. constructor create_deref(d:tderef);
  206. destructor destroy; override;
  207. function getcopy:TImplementedInterface;
  208. procedure buildderef;
  209. procedure deref;
  210. procedure AddMapping(const origname, newname: string);
  211. function GetMapping(const origname: string):string;
  212. procedure AddImplProc(pd:tprocdef);
  213. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  214. end;
  215. { tvmtentry }
  216. tvmtentry = record
  217. procdef : tprocdef;
  218. procdefderef : tderef;
  219. visibility : tvisibility;
  220. end;
  221. pvmtentry = ^tvmtentry;
  222. { tobjectdef }
  223. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  224. pmvcallstaticinfo = ^tmvcallstaticinfo;
  225. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  226. tobjectdef = class(tabstractrecorddef)
  227. private
  228. fcurrent_dispid: longint;
  229. public
  230. dwarf_struct_lab : tasmsymbol;
  231. childof : tobjectdef;
  232. childofderef : tderef;
  233. { for Object Pascal helpers }
  234. extendeddef : tabstractrecorddef;
  235. extendeddefderef: tderef;
  236. { for C++ classes: name of the library this class is imported from }
  237. import_lib,
  238. { for Objective-C: protocols and classes can have the same name there }
  239. objextname : pshortstring;
  240. { to be able to have a variable vmt position }
  241. { and no vmt field for objects without virtuals }
  242. vmtentries : TFPList;
  243. vmcallstaticinfo : pmvcallstaticinfo;
  244. vmt_offset : longint;
  245. iidguid : pguid;
  246. iidstr : pshortstring;
  247. { store implemented interfaces defs and name mappings }
  248. ImplementedInterfaces : TFPObjectList;
  249. writing_class_record_dbginfo,
  250. { a class of this type has been created in this module }
  251. created_in_current_module,
  252. { a loadvmtnode for this class has been created in this
  253. module, so if a classrefdef variable of this or a parent
  254. class is used somewhere to instantiate a class, then this
  255. class may be instantiated
  256. }
  257. maybe_created_in_current_module,
  258. { a "class of" this particular class has been created in
  259. this module
  260. }
  261. classref_created_in_current_module : boolean;
  262. objecttype : tobjecttyp;
  263. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  264. constructor ppuload(ppufile:tcompilerppufile);
  265. destructor destroy;override;
  266. function getcopy : tstoreddef;override;
  267. procedure ppuwrite(ppufile:tcompilerppufile);override;
  268. function GetTypeName:string;override;
  269. procedure buildderef;override;
  270. procedure deref;override;
  271. procedure buildderefimpl;override;
  272. procedure derefimpl;override;
  273. procedure resetvmtentries;
  274. procedure copyvmtentries(objdef:tobjectdef);
  275. function getparentdef:tdef;override;
  276. function size : asizeint;override;
  277. function alignment:shortint;override;
  278. function vmtmethodoffset(index:longint):longint;
  279. function members_need_inittable : boolean;
  280. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  281. { this should be called when this class implements an interface }
  282. procedure prepareguid;
  283. function is_publishable : boolean;override;
  284. function is_related(d : tdef) : boolean;override;
  285. function needs_inittable : boolean;override;
  286. function rtti_mangledname(rt:trttitype):string;override;
  287. function vmt_mangledname : string;
  288. procedure check_forwards; override;
  289. procedure insertvmt;
  290. procedure set_parent(c : tobjectdef);
  291. function find_destructor: tprocdef;
  292. function implements_any_interfaces: boolean;
  293. { dispinterface support }
  294. function get_next_dispid: longint;
  295. { enumerator support }
  296. function search_enumerator_get: tprocdef; override;
  297. function search_enumerator_move: tprocdef; override;
  298. function search_enumerator_current: tsym; override;
  299. { WPO }
  300. procedure register_created_object_type;override;
  301. procedure register_maybe_created_object_type;
  302. procedure register_created_classref_type;
  303. procedure register_vmt_call(index:longint);
  304. { ObjC }
  305. procedure finish_objc_data;
  306. function check_objc_types: boolean;
  307. { C++ }
  308. procedure finish_cpp_data;
  309. end;
  310. tclassrefdef = class(tabstractpointerdef)
  311. constructor create(def:tdef);
  312. constructor ppuload(ppufile:tcompilerppufile);
  313. procedure ppuwrite(ppufile:tcompilerppufile);override;
  314. function getcopy:tstoreddef;override;
  315. function GetTypeName:string;override;
  316. function is_publishable : boolean;override;
  317. function rtti_mangledname(rt:trttitype):string;override;
  318. procedure register_created_object_type;override;
  319. end;
  320. tarraydef = class(tstoreddef)
  321. lowrange,
  322. highrange : asizeint;
  323. rangedef : tdef;
  324. rangedefderef : tderef;
  325. arrayoptions : tarraydefoptions;
  326. symtable : TSymtable;
  327. protected
  328. _elementdef : tdef;
  329. _elementdefderef : tderef;
  330. procedure setelementdef(def:tdef);
  331. public
  332. function elesize : asizeint;
  333. function elepackedbitsize : asizeint;
  334. function elecount : asizeuint;
  335. constructor create_from_pointer(def:tdef);
  336. constructor create(l,h:asizeint;def:tdef);
  337. constructor ppuload(ppufile:tcompilerppufile);
  338. destructor destroy; override;
  339. function getcopy : tstoreddef;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. function GetTypeName:string;override;
  342. function getmangledparaname : string;override;
  343. procedure buildderef;override;
  344. procedure deref;override;
  345. function size : asizeint;override;
  346. function alignment : shortint;override;
  347. { returns the label of the range check string }
  348. function needs_inittable : boolean;override;
  349. property elementdef : tdef read _elementdef write setelementdef;
  350. function is_publishable : boolean;override;
  351. end;
  352. torddef = class(tstoreddef)
  353. low,high : TConstExprInt;
  354. ordtype : tordtype;
  355. constructor create(t : tordtype;v,b : TConstExprInt);
  356. constructor ppuload(ppufile:tcompilerppufile);
  357. function getcopy : tstoreddef;override;
  358. procedure ppuwrite(ppufile:tcompilerppufile);override;
  359. function is_publishable : boolean;override;
  360. function GetTypeName:string;override;
  361. function alignment:shortint;override;
  362. procedure setsize;
  363. function packedbitsize: asizeint; override;
  364. function getvardef : longint;override;
  365. end;
  366. tfloatdef = class(tstoreddef)
  367. floattype : tfloattype;
  368. constructor create(t : tfloattype);
  369. constructor ppuload(ppufile:tcompilerppufile);
  370. function getcopy : tstoreddef;override;
  371. procedure ppuwrite(ppufile:tcompilerppufile);override;
  372. function GetTypeName:string;override;
  373. function is_publishable : boolean;override;
  374. function alignment:shortint;override;
  375. procedure setsize;
  376. function getvardef:longint;override;
  377. end;
  378. { tabstractprocdef }
  379. tabstractprocdef = class(tstoreddef)
  380. { saves a definition to the return type }
  381. returndef : tdef;
  382. returndefderef : tderef;
  383. parast : TSymtable;
  384. paras : tparalist;
  385. proctypeoption : tproctypeoption;
  386. proccalloption : tproccalloption;
  387. procoptions : tprocoptions;
  388. callerargareasize,
  389. calleeargareasize: pint;
  390. {$ifdef m68k}
  391. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  392. {$endif}
  393. funcretloc : array[tcallercallee] of TCGPara;
  394. has_paraloc_info : tcallercallee; { paraloc info is available }
  395. { number of user visible parameters }
  396. maxparacount,
  397. minparacount : byte;
  398. constructor create(dt:tdeftyp;level:byte);
  399. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  400. destructor destroy;override;
  401. procedure ppuwrite(ppufile:tcompilerppufile);override;
  402. procedure buildderef;override;
  403. procedure deref;override;
  404. procedure calcparas;
  405. function typename_paras(showhidden:boolean): string;
  406. function is_methodpointer:boolean;virtual;
  407. function is_addressonly:boolean;virtual;
  408. function no_self_node:boolean;
  409. procedure check_mark_as_nested;
  410. procedure init_paraloc_info(side: tcallercallee);
  411. function stack_tainting_parameter(side: tcallercallee): boolean;
  412. private
  413. procedure count_para(p:TObject;arg:pointer);
  414. procedure insert_para(p:TObject;arg:pointer);
  415. end;
  416. tprocvardef = class(tabstractprocdef)
  417. constructor create(level:byte);
  418. constructor ppuload(ppufile:tcompilerppufile);
  419. function getcopy : tstoreddef;override;
  420. procedure ppuwrite(ppufile:tcompilerppufile);override;
  421. function GetSymtable(t:tGetSymtable):TSymtable;override;
  422. function size : asizeint;override;
  423. function GetTypeName:string;override;
  424. function is_publishable : boolean;override;
  425. function is_methodpointer:boolean;override;
  426. function is_addressonly:boolean;override;
  427. function getmangledparaname:string;override;
  428. end;
  429. tmessageinf = record
  430. case integer of
  431. 0 : (str : pshortstring);
  432. 1 : (i : longint);
  433. end;
  434. tinlininginfo = record
  435. { node tree }
  436. code : tnode;
  437. flags : tprocinfoflags;
  438. end;
  439. pinlininginfo = ^tinlininginfo;
  440. {$ifdef oldregvars}
  441. { register variables }
  442. pregvarinfo = ^tregvarinfo;
  443. tregvarinfo = record
  444. regvars : array[1..maxvarregs] of tsym;
  445. regvars_para : array[1..maxvarregs] of boolean;
  446. regvars_refs : array[1..maxvarregs] of longint;
  447. fpuregvars : array[1..maxfpuvarregs] of tsym;
  448. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  449. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  450. end;
  451. {$endif oldregvars}
  452. { tprocdef }
  453. tprocdef = class(tabstractprocdef)
  454. private
  455. _mangledname : pshortstring;
  456. public
  457. messageinf : tmessageinf;
  458. dispid : longint;
  459. {$ifndef EXTDEBUG}
  460. { where is this function defined and what were the symbol
  461. flags, needed here because there
  462. is only one symbol for all overloaded functions
  463. EXTDEBUG has fileinfo in tdef (PFV) }
  464. fileinfo : tfileposinfo;
  465. {$endif}
  466. symoptions : tsymoptions;
  467. deprecatedmsg : pshortstring;
  468. { symbol owning this definition }
  469. procsym : tsym;
  470. procsymderef : tderef;
  471. { alias names }
  472. aliasnames : TCmdStrList;
  473. { symtables }
  474. localst : TSymtable;
  475. funcretsym : tsym;
  476. funcretsymderef : tderef;
  477. struct : tabstractrecorddef;
  478. structderef : tderef;
  479. {$if defined(powerpc) or defined(m68k)}
  480. { library symbol for AmigaOS/MorphOS }
  481. libsym : tsym;
  482. libsymderef : tderef;
  483. {$endif powerpc or m68k}
  484. { name of the result variable to insert in the localsymtable }
  485. resultname : pshortstring;
  486. { import info }
  487. import_dll,
  488. import_name : pshortstring;
  489. { info for inlining the subroutine, if this pointer is nil,
  490. the procedure can't be inlined }
  491. inlininginfo : pinlininginfo;
  492. {$ifdef jvm}
  493. { generated assembler code; used by JVM backend so it can afterwards
  494. easily write out all methods grouped per class }
  495. exprasmlist : TAsmList;
  496. {$endif jvm}
  497. {$ifdef oldregvars}
  498. regvarinfo: pregvarinfo;
  499. {$endif oldregvars}
  500. { interrupt vector }
  501. interruptvector : longint;
  502. { First/last assembler symbol/instruction in aasmoutput list.
  503. Note: initialised after compiling the code for the procdef, but
  504. not saved to/restored from ppu. Used when inserting debug info }
  505. procstarttai,
  506. procendtai : tai;
  507. import_nr : word;
  508. extnumber : word;
  509. {$ifdef i386}
  510. fpu_used : byte;
  511. {$endif i386}
  512. visibility : tvisibility;
  513. { true, if the procedure is only declared
  514. (forward procedure) }
  515. forwarddef,
  516. { true if the procedure is declared in the interface }
  517. interfacedef : boolean;
  518. { true if the procedure has a forward declaration }
  519. hasforward : boolean;
  520. constructor create(level:byte);
  521. constructor ppuload(ppufile:tcompilerppufile);
  522. destructor destroy;override;
  523. procedure ppuwrite(ppufile:tcompilerppufile);override;
  524. procedure buildderef;override;
  525. procedure buildderefimpl;override;
  526. procedure deref;override;
  527. procedure derefimpl;override;
  528. function GetSymtable(t:tGetSymtable):TSymtable;override;
  529. function GetTypeName : string;override;
  530. { on some targets the mangled name for defining a procedure is
  531. different from the mangled name used for calling it later }
  532. function mangledname(fordefinition: boolean) : string;
  533. procedure setmangledname(const s : string);
  534. function fullprocname(showhidden:boolean):string;
  535. function defaultmangledname: string;
  536. function cplusplusmangledname : string;
  537. function objcmangledname : string;
  538. function jvmmangledname: string;
  539. procedure adornmangledname(var name: string; fordefinition: boolean);{$ifndef jvm}inline;{$endif}
  540. function is_methodpointer:boolean;override;
  541. function is_addressonly:boolean;override;
  542. procedure make_external;
  543. end;
  544. { single linked list of overloaded procs }
  545. pprocdeflist = ^tprocdeflist;
  546. tprocdeflist = record
  547. def : tprocdef;
  548. defderef : tderef;
  549. next : pprocdeflist;
  550. end;
  551. tstringdef = class(tstoreddef)
  552. stringtype : tstringtype;
  553. len : asizeint;
  554. constructor createshort(l : byte);
  555. constructor loadshort(ppufile:tcompilerppufile);
  556. constructor createlong(l : asizeint);
  557. constructor loadlong(ppufile:tcompilerppufile);
  558. constructor createansi;
  559. constructor loadansi(ppufile:tcompilerppufile);
  560. constructor createwide;
  561. constructor loadwide(ppufile:tcompilerppufile);
  562. constructor createunicode;
  563. constructor loadunicode(ppufile:tcompilerppufile);
  564. function getcopy : tstoreddef;override;
  565. function stringtypname:string;
  566. procedure ppuwrite(ppufile:tcompilerppufile);override;
  567. function GetTypeName:string;override;
  568. function getmangledparaname:string;override;
  569. function is_publishable : boolean;override;
  570. function alignment : shortint;override;
  571. function needs_inittable : boolean;override;
  572. function getvardef:longint;override;
  573. end;
  574. { tenumdef }
  575. tenumdef = class(tstoreddef)
  576. minval,
  577. maxval : asizeint;
  578. basedef : tenumdef;
  579. basedefderef : tderef;
  580. symtable : TSymtable;
  581. has_jumps : boolean;
  582. constructor create;
  583. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  584. constructor ppuload(ppufile:tcompilerppufile);
  585. destructor destroy;override;
  586. function getcopy : tstoreddef;override;
  587. procedure ppuwrite(ppufile:tcompilerppufile);override;
  588. procedure buildderef;override;
  589. procedure deref;override;
  590. function GetTypeName:string;override;
  591. function is_publishable : boolean;override;
  592. procedure calcsavesize;
  593. function packedbitsize: asizeint; override;
  594. procedure setmax(_max:asizeint);
  595. procedure setmin(_min:asizeint);
  596. function min:asizeint;
  597. function max:asizeint;
  598. function getfirstsym:tsym;
  599. end;
  600. tsetdef = class(tstoreddef)
  601. elementdef : tdef;
  602. elementdefderef : tderef;
  603. setbase,
  604. setmax : aword;
  605. constructor create(def:tdef;low, high : asizeint);
  606. constructor ppuload(ppufile:tcompilerppufile);
  607. function getcopy : tstoreddef;override;
  608. procedure ppuwrite(ppufile:tcompilerppufile);override;
  609. procedure buildderef;override;
  610. procedure deref;override;
  611. function GetTypeName:string;override;
  612. function is_publishable : boolean;override;
  613. end;
  614. tdefawaresymtablestack = class(TSymtablestack)
  615. private
  616. procedure addhelpers(st: TSymtable);
  617. procedure removehelpers(st: TSymtable);
  618. public
  619. procedure push(st: TSymtable); override;
  620. procedure pop(st: TSymtable); override;
  621. end;
  622. var
  623. current_structdef: tabstractrecorddef; { used for private functions check !! }
  624. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  625. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  626. { default types }
  627. generrordef, { error in definition }
  628. voidpointertype, { pointer for Void-pointeddef }
  629. charpointertype, { pointer for Char-pointeddef }
  630. widecharpointertype, { pointer for WideChar-pointeddef }
  631. voidfarpointertype,
  632. cundefinedtype,
  633. cformaltype, { unique formal definition }
  634. ctypedformaltype, { unique typed formal definition }
  635. voidtype, { Void (procedure) }
  636. cchartype, { Char }
  637. cwidechartype, { WideChar }
  638. pasbool8type, { boolean type }
  639. pasbool16type,
  640. pasbool32type,
  641. pasbool64type,
  642. bool8type,
  643. bool16type,
  644. bool32type,
  645. bool64type, { implement me }
  646. u8inttype, { 8-Bit unsigned integer }
  647. s8inttype, { 8-Bit signed integer }
  648. u16inttype, { 16-Bit unsigned integer }
  649. s16inttype, { 16-Bit signed integer }
  650. u32inttype, { 32-Bit unsigned integer }
  651. s32inttype, { 32-Bit signed integer }
  652. u64inttype, { 64-bit unsigned integer }
  653. s64inttype, { 64-bit signed integer }
  654. s32floattype, { 32 bit floating point number }
  655. s64floattype, { 64 bit floating point number }
  656. s80floattype, { 80 bit floating point number }
  657. sc80floattype, { 80 bit floating point number but stored like in C }
  658. s64currencytype, { pointer to a currency type }
  659. cshortstringtype, { pointer to type of short string const }
  660. clongstringtype, { pointer to type of long string const }
  661. cansistringtype, { pointer to type of ansi string const }
  662. cwidestringtype, { pointer to type of wide string const }
  663. cunicodestringtype,
  664. openshortstringtype, { pointer to type of an open shortstring,
  665. needed for readln() }
  666. openchararraytype, { pointer to type of an open array of char,
  667. needed for readln() }
  668. cfiletype, { get the same definition for all file }
  669. { used for stabs }
  670. methodpointertype, { typecasting of methodpointers to extract self }
  671. hresultdef,
  672. { we use only one variant def for every variant class }
  673. cvarianttype,
  674. colevarianttype,
  675. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  676. sinttype,
  677. uinttype,
  678. { unsigned and signed ord type with the same size as a pointer }
  679. ptruinttype,
  680. ptrsinttype,
  681. { several types to simulate more or less C++ objects for GDB }
  682. vmttype,
  683. vmtarraytype,
  684. pvmttype : tdef; { type of classrefs, used for stabs }
  685. { pointer to the anchestor of all classes }
  686. class_tobject : tobjectdef;
  687. { pointer to the ancestor of all COM interfaces }
  688. interface_iunknown : tobjectdef;
  689. { pointer to the ancestor of all dispinterfaces }
  690. interface_idispatch : tobjectdef;
  691. { pointer to the TGUID type
  692. of all interfaces }
  693. rec_tguid : trecorddef;
  694. { pointer to jump buffer }
  695. rec_jmp_buf : trecorddef;
  696. { Objective-C base types }
  697. objc_metaclasstype,
  698. objc_superclasstype,
  699. objc_idtype,
  700. objc_seltype : tpointerdef;
  701. objc_objecttype : trecorddef;
  702. { base type of @protocol(protocolname) Objective-C statements }
  703. objc_protocoltype : tobjectdef;
  704. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  705. objc_fastenumeration : tobjectdef;
  706. objc_fastenumerationstate : trecorddef;
  707. { Java base types }
  708. { java.lang.Object }
  709. java_jlobject : tobjectdef;
  710. const
  711. {$ifdef i386}
  712. pbestrealtype : ^tdef = @s80floattype;
  713. {$endif}
  714. {$ifdef x86_64}
  715. pbestrealtype : ^tdef = @s80floattype;
  716. {$endif}
  717. {$ifdef m68k}
  718. pbestrealtype : ^tdef = @s64floattype;
  719. {$endif}
  720. {$ifdef alpha}
  721. pbestrealtype : ^tdef = @s64floattype;
  722. {$endif}
  723. {$ifdef powerpc}
  724. pbestrealtype : ^tdef = @s64floattype;
  725. {$endif}
  726. {$ifdef POWERPC64}
  727. pbestrealtype : ^tdef = @s64floattype;
  728. {$endif}
  729. {$ifdef ia64}
  730. pbestrealtype : ^tdef = @s64floattype;
  731. {$endif}
  732. {$ifdef SPARC}
  733. pbestrealtype : ^tdef = @s64floattype;
  734. {$endif SPARC}
  735. {$ifdef vis}
  736. pbestrealtype : ^tdef = @s64floattype;
  737. {$endif vis}
  738. {$ifdef ARM}
  739. pbestrealtype : ^tdef = @s64floattype;
  740. {$endif ARM}
  741. {$ifdef MIPS}
  742. pbestrealtype : ^tdef = @s64floattype;
  743. {$endif MIPS}
  744. {$ifdef AVR}
  745. pbestrealtype : ^tdef = @s64floattype;
  746. {$endif AVR}
  747. {$ifdef JVM}
  748. pbestrealtype : ^tdef = @s64floattype;
  749. {$endif JVM}
  750. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  751. function make_dllmangledname(const dllname,importname:string;
  752. import_nr : word; pco : tproccalloption):string;
  753. { should be in the types unit, but the types unit uses the node stuff :( }
  754. function is_interfacecom(def: tdef): boolean;
  755. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  756. function is_interfacecorba(def: tdef): boolean;
  757. function is_interface(def: tdef): boolean;
  758. function is_dispinterface(def: tdef): boolean;
  759. function is_object(def: tdef): boolean;
  760. function is_class(def: tdef): boolean;
  761. function is_cppclass(def: tdef): boolean;
  762. function is_objectpascal_helper(def: tdef): boolean;
  763. function is_objcclass(def: tdef): boolean;
  764. function is_objcclassref(def: tdef): boolean;
  765. function is_objcprotocol(def: tdef): boolean;
  766. function is_objccategory(def: tdef): boolean;
  767. function is_objc_class_or_protocol(def: tdef): boolean;
  768. function is_objc_protocol_or_category(def: tdef): boolean;
  769. function is_classhelper(def: tdef): boolean;
  770. function is_class_or_interface(def: tdef): boolean;
  771. function is_class_or_interface_or_objc(def: tdef): boolean;
  772. function is_class_or_interface_or_object(def: tdef): boolean;
  773. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  774. function is_implicit_pointer_object_type(def: tdef): boolean;
  775. function is_class_or_object(def: tdef): boolean;
  776. function is_record(def: tdef): boolean;
  777. function is_javaclass(def: tdef): boolean;
  778. function is_javainterface(def: tdef): boolean;
  779. function is_java_class_or_interface(def: tdef): boolean;
  780. procedure loadobjctypes;
  781. procedure maybeloadcocoatypes;
  782. function use_vectorfpu(def : tdef) : boolean;
  783. implementation
  784. uses
  785. SysUtils,
  786. cutils,
  787. { global }
  788. verbose,
  789. { target }
  790. systems,aasmcpu,paramgr,
  791. { symtable }
  792. symsym,symtable,symutil,defutil,objcdef,jvmdef,
  793. { module }
  794. fmodule,
  795. { other }
  796. gendef,
  797. fpccrc
  798. ;
  799. {****************************************************************************
  800. Helpers
  801. ****************************************************************************}
  802. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  803. var
  804. s,hs,
  805. prefix : string;
  806. oldlen,
  807. newlen,
  808. i : longint;
  809. crc : dword;
  810. hp : tparavarsym;
  811. begin
  812. prefix:='';
  813. if not assigned(st) then
  814. internalerror(200204212);
  815. { sub procedures }
  816. while (st.symtabletype=localsymtable) do
  817. begin
  818. if st.defowner.typ<>procdef then
  819. internalerror(200204173);
  820. { Add the full mangledname of procedure to prevent
  821. conflicts with 2 overloads having both a nested procedure
  822. with the same name, see tb0314 (PFV) }
  823. s:=tprocdef(st.defowner).procsym.name;
  824. oldlen:=length(s);
  825. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  826. begin
  827. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  828. if not(vo_is_hidden_para in hp.varoptions) then
  829. s:=s+'$'+hp.vardef.mangledparaname;
  830. end;
  831. if not is_void(tprocdef(st.defowner).returndef) then
  832. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  833. newlen:=length(s);
  834. { Replace with CRC if the parameter line is very long }
  835. if (newlen-oldlen>12) and
  836. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  837. begin
  838. crc:=0;
  839. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  840. begin
  841. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  842. if not(vo_is_hidden_para in hp.varoptions) then
  843. begin
  844. hs:=hp.vardef.mangledparaname;
  845. crc:=UpdateCrc32(crc,hs[1],length(hs));
  846. end;
  847. end;
  848. hs:=hp.vardef.mangledparaname;
  849. crc:=UpdateCrc32(crc,hs[1],length(hs));
  850. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  851. end;
  852. if prefix<>'' then
  853. prefix:=s+'_'+prefix
  854. else
  855. prefix:=s;
  856. if length(prefix)>100 then
  857. begin
  858. crc:=0;
  859. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  860. prefix:='$CRC'+hexstr(crc,8);
  861. end;
  862. st:=st.defowner.owner;
  863. end;
  864. { object/classes symtable, nested type definitions in classes require the while loop }
  865. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  866. begin
  867. if not (st.defowner.typ in [objectdef,recorddef]) then
  868. internalerror(200204174);
  869. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  870. st:=st.defowner.owner;
  871. end;
  872. { symtable must now be static or global }
  873. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  874. internalerror(200204175);
  875. result:='';
  876. if typeprefix<>'' then
  877. result:=result+typeprefix+'_';
  878. { Add P$ for program, which can have the same name as
  879. a unit }
  880. if (TSymtable(main_module.localsymtable)=st) and
  881. (not main_module.is_unit) then
  882. result:=result+'P$'+st.name^
  883. else
  884. result:=result+st.name^;
  885. if prefix<>'' then
  886. result:=result+'_'+prefix;
  887. if suffix<>'' then
  888. result:=result+'_'+suffix;
  889. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  890. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  891. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  892. { those in the debug map, leading to troubles with dsymutil). So always }
  893. { add an underscore on darwin. }
  894. if (target_info.system in systems_darwin) then
  895. result := '_' + result;
  896. end;
  897. function make_dllmangledname(const dllname,importname:string;import_nr : word; pco : tproccalloption):string;
  898. var
  899. crc : cardinal;
  900. i : longint;
  901. use_crc : boolean;
  902. dllprefix : string;
  903. begin
  904. if (target_info.system in (systems_all_windows + systems_nativent +
  905. [system_i386_emx, system_i386_os2]))
  906. and (dllname <> '') then
  907. begin
  908. dllprefix:=lower(ExtractFileName(dllname));
  909. { Remove .dll suffix if present }
  910. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  911. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  912. use_crc:=false;
  913. for i:=1 to length(dllprefix) do
  914. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  915. begin
  916. use_crc:=true;
  917. break;
  918. end;
  919. if use_crc then
  920. begin
  921. crc:=0;
  922. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  923. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  924. end
  925. else
  926. dllprefix:='_$dll$'+dllprefix+'$';
  927. if importname<>'' then
  928. result:=dllprefix+importname
  929. else
  930. result:=dllprefix+'_index_'+tostr(import_nr);
  931. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  932. { This allows to import VC++ mangled names from DLLs. }
  933. { Do not perform replacement, if external symbol is not imported from DLL. }
  934. if (dllname<>'') then
  935. begin
  936. Replace(result,'?','__q$$');
  937. {$ifdef arm}
  938. { @ symbol is not allowed in ARM assembler only }
  939. Replace(result,'@','__a$$');
  940. {$endif arm}
  941. end;
  942. end
  943. else
  944. begin
  945. if importname<>'' then
  946. begin
  947. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  948. result:=importname
  949. else
  950. result:=target_info.Cprefix+importname;
  951. end
  952. else
  953. result:='_index_'+tostr(import_nr);
  954. end;
  955. end;
  956. {****************************************************************************
  957. TDEFAWARESYMTABLESTACK
  958. (symtablestack descendant that does some special actions on
  959. the pushed/popped symtables)
  960. ****************************************************************************}
  961. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  962. var
  963. i: integer;
  964. s: string;
  965. list: TFPObjectList;
  966. def: tdef;
  967. begin
  968. { search the symtable from first to last; the helper to use will be the
  969. last one in the list }
  970. for i:=0 to st.symlist.count-1 do
  971. begin
  972. if not (st.symlist[i] is ttypesym) then
  973. continue;
  974. def:=ttypesym(st.SymList[i]).typedef;
  975. if is_objectpascal_helper(def) then
  976. begin
  977. s:=make_mangledname('',tobjectdef(def).extendeddef.symtable,'');
  978. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  979. if not assigned(list) then
  980. begin
  981. list:=TFPObjectList.Create(false);
  982. current_module.extendeddefs.Add(s,list);
  983. end;
  984. list.Add(def);
  985. end
  986. else
  987. { add nested helpers as well }
  988. if def.typ in [recorddef,objectdef] then
  989. addhelpers(tabstractrecorddef(def).symtable);
  990. end;
  991. end;
  992. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  993. var
  994. i, j: integer;
  995. tmpst: TSymtable;
  996. list: TFPObjectList;
  997. begin
  998. for i:=current_module.extendeddefs.count-1 downto 0 do
  999. begin
  1000. list:=TFPObjectList(current_module.extendeddefs[i]);
  1001. for j:=list.count-1 downto 0 do
  1002. begin
  1003. if not (list[j] is tobjectdef) then
  1004. Internalerror(2011031501);
  1005. tmpst:=tobjectdef(list[j]).owner;
  1006. repeat
  1007. if tmpst=st then
  1008. begin
  1009. list.delete(j);
  1010. break;
  1011. end
  1012. else
  1013. begin
  1014. if assigned(tmpst.defowner) then
  1015. tmpst:=tmpst.defowner.owner
  1016. else
  1017. tmpst:=nil;
  1018. end;
  1019. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1020. end;
  1021. if list.count=0 then
  1022. current_module.extendeddefs.delete(i);
  1023. end;
  1024. end;
  1025. procedure tdefawaresymtablestack.push(st: TSymtable);
  1026. begin
  1027. { nested helpers will be added as well }
  1028. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1029. (sto_has_helper in st.tableoptions) then
  1030. addhelpers(st);
  1031. inherited push(st);
  1032. end;
  1033. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1034. begin
  1035. inherited pop(st);
  1036. { nested helpers will be removed as well }
  1037. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1038. (sto_has_helper in st.tableoptions) then
  1039. removehelpers(st);
  1040. end;
  1041. {****************************************************************************
  1042. TDEF (base class for definitions)
  1043. ****************************************************************************}
  1044. constructor tstoreddef.create(dt:tdeftyp);
  1045. var
  1046. insertstack : psymtablestackitem;
  1047. begin
  1048. inherited create(dt);
  1049. savesize := 0;
  1050. {$ifdef EXTDEBUG}
  1051. fileinfo := current_filepos;
  1052. {$endif}
  1053. generictokenbuf:=nil;
  1054. genericdef:=nil;
  1055. { Don't register forwarddefs, they are disposed at the
  1056. end of an type block }
  1057. if (dt=forwarddef) then
  1058. exit;
  1059. { Register in current_module }
  1060. if assigned(current_module) then
  1061. begin
  1062. current_module.deflist.Add(self);
  1063. DefId:=current_module.deflist.Count-1;
  1064. end;
  1065. { Register in symtable stack }
  1066. if assigned(symtablestack) then
  1067. begin
  1068. insertstack:=symtablestack.stack;
  1069. while assigned(insertstack) and
  1070. (insertstack^.symtable.symtabletype=withsymtable) do
  1071. insertstack:=insertstack^.next;
  1072. if not assigned(insertstack) then
  1073. internalerror(200602044);
  1074. insertstack^.symtable.insertdef(self);
  1075. end;
  1076. end;
  1077. destructor tstoreddef.destroy;
  1078. begin
  1079. { Direct calls are not allowed, use symtable.deletedef() }
  1080. if assigned(owner) then
  1081. internalerror(200612311);
  1082. if assigned(generictokenbuf) then
  1083. begin
  1084. generictokenbuf.free;
  1085. generictokenbuf:=nil;
  1086. end;
  1087. inherited destroy;
  1088. end;
  1089. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1090. var
  1091. sizeleft,i : longint;
  1092. buf : array[0..255] of byte;
  1093. begin
  1094. inherited create(dt);
  1095. DefId:=ppufile.getlongint;
  1096. current_module.deflist[DefId]:=self;
  1097. {$ifdef EXTDEBUG}
  1098. fillchar(fileinfo,sizeof(fileinfo),0);
  1099. {$endif}
  1100. { load }
  1101. ppufile.getderef(typesymderef);
  1102. ppufile.getsmallset(defoptions);
  1103. ppufile.getsmallset(defstates);
  1104. if df_generic in defoptions then
  1105. begin
  1106. sizeleft:=ppufile.getlongint;
  1107. initgeneric;
  1108. while sizeleft>0 do
  1109. begin
  1110. if sizeleft>sizeof(buf) then
  1111. i:=sizeof(buf)
  1112. else
  1113. i:=sizeleft;
  1114. ppufile.getdata(buf,i);
  1115. generictokenbuf.write(buf,i);
  1116. dec(sizeleft,i);
  1117. end;
  1118. end;
  1119. if df_specialization in defoptions then
  1120. ppufile.getderef(genericdefderef);
  1121. end;
  1122. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1123. var
  1124. prefix : string[4];
  1125. begin
  1126. if rt=fullrtti then
  1127. begin
  1128. prefix:='RTTI';
  1129. include(defstates,ds_rtti_table_used);
  1130. end
  1131. else
  1132. begin
  1133. prefix:='INIT';
  1134. include(defstates,ds_init_table_used);
  1135. end;
  1136. if assigned(typesym) and
  1137. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1138. result:=make_mangledname(prefix,owner,typesym.name)
  1139. else
  1140. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1141. end;
  1142. function tstoreddef.OwnerHierarchyName: string;
  1143. var
  1144. tmp: tdef;
  1145. begin
  1146. tmp:=self;
  1147. result:='';
  1148. repeat
  1149. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  1150. tmp:=tdef(tmp.owner.defowner)
  1151. else
  1152. break;
  1153. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1154. until tmp=nil;
  1155. end;
  1156. function tstoreddef.in_currentunit: boolean;
  1157. var
  1158. st: tsymtable;
  1159. begin
  1160. st:=owner;
  1161. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1162. st:=st.defowner.owner;
  1163. result:=st.iscurrentunit;
  1164. end;
  1165. function tstoreddef.getcopy : tstoreddef;
  1166. begin
  1167. Message(sym_e_cant_create_unique_type);
  1168. getcopy:=terrordef.create;
  1169. end;
  1170. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1171. var
  1172. sizeleft,i : longint;
  1173. buf : array[0..255] of byte;
  1174. oldintfcrc : boolean;
  1175. begin
  1176. ppufile.putlongint(DefId);
  1177. ppufile.putderef(typesymderef);
  1178. ppufile.putsmallset(defoptions);
  1179. oldintfcrc:=ppufile.do_crc;
  1180. ppufile.do_crc:=false;
  1181. ppufile.putsmallset(defstates);
  1182. if df_generic in defoptions then
  1183. begin
  1184. if assigned(generictokenbuf) then
  1185. begin
  1186. sizeleft:=generictokenbuf.size;
  1187. generictokenbuf.seek(0);
  1188. end
  1189. else
  1190. sizeleft:=0;
  1191. ppufile.putlongint(sizeleft);
  1192. while sizeleft>0 do
  1193. begin
  1194. if sizeleft>sizeof(buf) then
  1195. i:=sizeof(buf)
  1196. else
  1197. i:=sizeleft;
  1198. generictokenbuf.read(buf,i);
  1199. ppufile.putdata(buf,i);
  1200. dec(sizeleft,i);
  1201. end;
  1202. end;
  1203. ppufile.do_crc:=oldintfcrc;
  1204. if df_specialization in defoptions then
  1205. ppufile.putderef(genericdefderef);
  1206. end;
  1207. procedure tstoreddef.buildderef;
  1208. begin
  1209. typesymderef.build(typesym);
  1210. genericdefderef.build(genericdef);
  1211. end;
  1212. procedure tstoreddef.buildderefimpl;
  1213. begin
  1214. end;
  1215. procedure tstoreddef.deref;
  1216. begin
  1217. typesym:=ttypesym(typesymderef.resolve);
  1218. if df_specialization in defoptions then
  1219. genericdef:=tstoreddef(genericdefderef.resolve);
  1220. end;
  1221. procedure tstoreddef.derefimpl;
  1222. begin
  1223. end;
  1224. function tstoreddef.size : asizeint;
  1225. begin
  1226. size:=savesize;
  1227. end;
  1228. function tstoreddef.getvardef:longint;
  1229. begin
  1230. result:=varUndefined;
  1231. end;
  1232. function tstoreddef.alignment : shortint;
  1233. begin
  1234. { natural alignment by default }
  1235. alignment:=size_2_align(savesize);
  1236. { can happen if savesize = 0, e.g. for voiddef or
  1237. an empty record
  1238. }
  1239. if (alignment=0) then
  1240. alignment:=1;
  1241. end;
  1242. { returns true, if the definition can be published }
  1243. function tstoreddef.is_publishable : boolean;
  1244. begin
  1245. is_publishable:=false;
  1246. end;
  1247. { needs an init table }
  1248. function tstoreddef.needs_inittable : boolean;
  1249. begin
  1250. needs_inittable:=false;
  1251. end;
  1252. function tstoreddef.is_intregable : boolean;
  1253. var
  1254. recsize,temp: longint;
  1255. begin
  1256. is_intregable:=false;
  1257. case typ of
  1258. orddef,
  1259. pointerdef,
  1260. enumdef,
  1261. classrefdef:
  1262. is_intregable:=true;
  1263. procvardef :
  1264. is_intregable:=tprocvardef(self).is_addressonly;
  1265. objectdef:
  1266. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1267. setdef:
  1268. is_intregable:=is_smallset(self);
  1269. recorddef:
  1270. begin
  1271. recsize:=size;
  1272. is_intregable:=
  1273. ispowerof2(recsize,temp) and
  1274. (recsize <= sizeof(asizeint));
  1275. end;
  1276. end;
  1277. end;
  1278. function tstoreddef.is_fpuregable : boolean;
  1279. begin
  1280. {$ifdef x86}
  1281. result:=use_vectorfpu(self);
  1282. {$else x86}
  1283. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1284. {$endif x86}
  1285. end;
  1286. procedure tstoreddef.initgeneric;
  1287. begin
  1288. if assigned(generictokenbuf) then
  1289. internalerror(200512131);
  1290. generictokenbuf:=tdynamicarray.create(256);
  1291. end;
  1292. {****************************************************************************
  1293. Tstringdef
  1294. ****************************************************************************}
  1295. constructor tstringdef.createshort(l : byte);
  1296. begin
  1297. inherited create(stringdef);
  1298. stringtype:=st_shortstring;
  1299. len:=l;
  1300. savesize:=len+1;
  1301. end;
  1302. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1303. begin
  1304. inherited ppuload(stringdef,ppufile);
  1305. stringtype:=st_shortstring;
  1306. len:=ppufile.getbyte;
  1307. savesize:=len+1;
  1308. end;
  1309. constructor tstringdef.createlong(l : asizeint);
  1310. begin
  1311. inherited create(stringdef);
  1312. stringtype:=st_longstring;
  1313. len:=l;
  1314. savesize:=sizeof(pint);
  1315. end;
  1316. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1317. begin
  1318. inherited ppuload(stringdef,ppufile);
  1319. stringtype:=st_longstring;
  1320. len:=ppufile.getasizeint;
  1321. savesize:=sizeof(pint);
  1322. end;
  1323. constructor tstringdef.createansi;
  1324. begin
  1325. inherited create(stringdef);
  1326. stringtype:=st_ansistring;
  1327. len:=-1;
  1328. savesize:=sizeof(pint);
  1329. end;
  1330. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1331. begin
  1332. inherited ppuload(stringdef,ppufile);
  1333. stringtype:=st_ansistring;
  1334. len:=ppufile.getaint;
  1335. savesize:=sizeof(pint);
  1336. end;
  1337. constructor tstringdef.createwide;
  1338. begin
  1339. inherited create(stringdef);
  1340. stringtype:=st_widestring;
  1341. len:=-1;
  1342. savesize:=sizeof(pint);
  1343. end;
  1344. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1345. begin
  1346. inherited ppuload(stringdef,ppufile);
  1347. stringtype:=st_widestring;
  1348. len:=ppufile.getaint;
  1349. savesize:=sizeof(pint);
  1350. end;
  1351. constructor tstringdef.createunicode;
  1352. begin
  1353. inherited create(stringdef);
  1354. stringtype:=st_unicodestring;
  1355. len:=-1;
  1356. savesize:=sizeof(pint);
  1357. end;
  1358. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1359. begin
  1360. inherited ppuload(stringdef,ppufile);
  1361. stringtype:=st_unicodestring;
  1362. len:=ppufile.getaint;
  1363. savesize:=sizeof(pint);
  1364. end;
  1365. function tstringdef.getcopy : tstoreddef;
  1366. begin
  1367. result:=tstringdef.create(typ);
  1368. result.typ:=stringdef;
  1369. tstringdef(result).stringtype:=stringtype;
  1370. tstringdef(result).len:=len;
  1371. tstringdef(result).savesize:=savesize;
  1372. end;
  1373. function tstringdef.stringtypname:string;
  1374. const
  1375. typname:array[tstringtype] of string[10]=(
  1376. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1377. );
  1378. begin
  1379. stringtypname:=typname[stringtype];
  1380. end;
  1381. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1382. begin
  1383. inherited ppuwrite(ppufile);
  1384. if stringtype=st_shortstring then
  1385. begin
  1386. {$ifdef extdebug}
  1387. if len > 255 then internalerror(12122002);
  1388. {$endif}
  1389. ppufile.putbyte(byte(len))
  1390. end
  1391. else
  1392. ppufile.putaint(len);
  1393. case stringtype of
  1394. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1395. st_longstring : ppufile.writeentry(iblongstringdef);
  1396. st_ansistring : ppufile.writeentry(ibansistringdef);
  1397. st_widestring : ppufile.writeentry(ibwidestringdef);
  1398. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1399. end;
  1400. end;
  1401. function tstringdef.needs_inittable : boolean;
  1402. begin
  1403. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1404. end;
  1405. function tstringdef.GetTypeName : string;
  1406. const
  1407. names : array[tstringtype] of string[15] = (
  1408. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1409. begin
  1410. GetTypeName:=names[stringtype];
  1411. end;
  1412. function tstringdef.getvardef : longint;
  1413. const
  1414. vardef : array[tstringtype] of longint = (
  1415. varUndefined,varUndefined,varString,varOleStr,varUString);
  1416. begin
  1417. result:=vardef[stringtype];
  1418. end;
  1419. function tstringdef.alignment : shortint;
  1420. begin
  1421. case stringtype of
  1422. st_unicodestring,
  1423. st_widestring,
  1424. st_ansistring:
  1425. alignment:=size_2_align(savesize);
  1426. st_longstring,
  1427. st_shortstring:
  1428. { char to string accesses byte 0 and 1 with one word access }
  1429. if (tf_requires_proper_alignment in target_info.flags) or
  1430. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1431. (m_mac in current_settings.modeswitches) then
  1432. alignment:=size_2_align(2)
  1433. else
  1434. alignment:=size_2_align(1);
  1435. else
  1436. internalerror(200412301);
  1437. end;
  1438. end;
  1439. function tstringdef.getmangledparaname : string;
  1440. begin
  1441. getmangledparaname:='STRING';
  1442. end;
  1443. function tstringdef.is_publishable : boolean;
  1444. begin
  1445. is_publishable:=true;
  1446. end;
  1447. {****************************************************************************
  1448. TENUMDEF
  1449. ****************************************************************************}
  1450. constructor tenumdef.create;
  1451. begin
  1452. inherited create(enumdef);
  1453. minval:=0;
  1454. maxval:=0;
  1455. calcsavesize;
  1456. has_jumps:=false;
  1457. basedef:=nil;
  1458. symtable:=tenumsymtable.create(self);
  1459. end;
  1460. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1461. begin
  1462. inherited create(enumdef);
  1463. minval:=_min;
  1464. maxval:=_max;
  1465. basedef:=_basedef;
  1466. calcsavesize;
  1467. has_jumps:=false;
  1468. symtable:=basedef.symtable.getcopy;
  1469. include(defoptions, df_copied_def);
  1470. end;
  1471. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1472. begin
  1473. inherited ppuload(enumdef,ppufile);
  1474. minval:=ppufile.getaint;
  1475. maxval:=ppufile.getaint;
  1476. savesize:=ppufile.getaint;
  1477. has_jumps:=false;
  1478. if df_copied_def in defoptions then
  1479. begin
  1480. symtable:=nil;
  1481. ppufile.getderef(basedefderef);
  1482. end
  1483. else
  1484. begin
  1485. // create with nil defowner first to prevent values changes on insert
  1486. symtable:=tenumsymtable.create(nil);
  1487. tenumsymtable(symtable).ppuload(ppufile);
  1488. symtable.defowner:=self;
  1489. end;
  1490. end;
  1491. destructor tenumdef.destroy;
  1492. begin
  1493. symtable.free;
  1494. symtable:=nil;
  1495. inherited destroy;
  1496. end;
  1497. function tenumdef.getcopy : tstoreddef;
  1498. begin
  1499. if assigned(basedef) then
  1500. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1501. else
  1502. begin
  1503. result:=tenumdef.create;
  1504. tenumdef(result).minval:=minval;
  1505. tenumdef(result).maxval:=maxval;
  1506. tenumdef(result).symtable.free;
  1507. tenumdef(result).symtable:=symtable.getcopy;
  1508. tenumdef(result).basedef:=self;
  1509. end;
  1510. tenumdef(result).has_jumps:=has_jumps;
  1511. tenumdef(result).basedefderef:=basedefderef;
  1512. include(tenumdef(result).defoptions,df_copied_def);
  1513. end;
  1514. procedure tenumdef.calcsavesize;
  1515. begin
  1516. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1517. savesize:=8
  1518. else
  1519. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1520. savesize:=4
  1521. else
  1522. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1523. savesize:=2
  1524. else
  1525. savesize:=1;
  1526. end;
  1527. function tenumdef.packedbitsize: asizeint;
  1528. var
  1529. sizeval: tconstexprint;
  1530. power: longint;
  1531. begin
  1532. result := 0;
  1533. if (minval >= 0) and
  1534. (maxval <= 1) then
  1535. result := 1
  1536. else
  1537. begin
  1538. if (minval>=0) then
  1539. sizeval:=maxval
  1540. else
  1541. { don't count 0 twice }
  1542. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1543. { 256 must become 512 etc. }
  1544. nextpowerof2(sizeval+1,power);
  1545. result := power;
  1546. end;
  1547. end;
  1548. procedure tenumdef.setmax(_max:asizeint);
  1549. begin
  1550. maxval:=_max;
  1551. calcsavesize;
  1552. end;
  1553. procedure tenumdef.setmin(_min:asizeint);
  1554. begin
  1555. minval:=_min;
  1556. calcsavesize;
  1557. end;
  1558. function tenumdef.min:asizeint;
  1559. begin
  1560. min:=minval;
  1561. end;
  1562. function tenumdef.max:asizeint;
  1563. begin
  1564. max:=maxval;
  1565. end;
  1566. function tenumdef.getfirstsym: tsym;
  1567. var
  1568. i:integer;
  1569. begin
  1570. for i := 0 to symtable.SymList.Count - 1 do
  1571. begin
  1572. result:=tsym(symtable.SymList[i]);
  1573. if tenumsym(result).value=minval then
  1574. exit;
  1575. end;
  1576. result:=nil;
  1577. end;
  1578. procedure tenumdef.buildderef;
  1579. begin
  1580. inherited buildderef;
  1581. if df_copied_def in defoptions then
  1582. basedefderef.build(basedef)
  1583. else
  1584. tenumsymtable(symtable).buildderef;
  1585. end;
  1586. procedure tenumdef.deref;
  1587. begin
  1588. inherited deref;
  1589. if df_copied_def in defoptions then
  1590. begin
  1591. basedef:=tenumdef(basedefderef.resolve);
  1592. symtable:=basedef.symtable.getcopy;
  1593. end
  1594. else
  1595. tenumsymtable(symtable).deref;
  1596. end;
  1597. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1598. begin
  1599. inherited ppuwrite(ppufile);
  1600. ppufile.putaint(min);
  1601. ppufile.putaint(max);
  1602. ppufile.putaint(savesize);
  1603. if df_copied_def in defoptions then
  1604. ppufile.putderef(basedefderef);
  1605. ppufile.writeentry(ibenumdef);
  1606. if not (df_copied_def in defoptions) then
  1607. tenumsymtable(symtable).ppuwrite(ppufile);
  1608. end;
  1609. function tenumdef.is_publishable : boolean;
  1610. begin
  1611. is_publishable:=true;
  1612. end;
  1613. function tenumdef.GetTypeName : string;
  1614. begin
  1615. GetTypeName:='<enumeration type>';
  1616. end;
  1617. {****************************************************************************
  1618. TORDDEF
  1619. ****************************************************************************}
  1620. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1621. begin
  1622. inherited create(orddef);
  1623. low:=v;
  1624. high:=b;
  1625. ordtype:=t;
  1626. setsize;
  1627. end;
  1628. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1629. begin
  1630. inherited ppuload(orddef,ppufile);
  1631. ordtype:=tordtype(ppufile.getbyte);
  1632. low:=ppufile.getexprint;
  1633. high:=ppufile.getexprint;
  1634. setsize;
  1635. end;
  1636. function torddef.getcopy : tstoreddef;
  1637. begin
  1638. result:=torddef.create(ordtype,low,high);
  1639. result.typ:=orddef;
  1640. torddef(result).low:=low;
  1641. torddef(result).high:=high;
  1642. torddef(result).ordtype:=ordtype;
  1643. torddef(result).savesize:=savesize;
  1644. end;
  1645. function torddef.alignment:shortint;
  1646. begin
  1647. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1648. (ordtype in [s64bit,u64bit]) then
  1649. result := 4
  1650. else
  1651. result := inherited alignment;
  1652. end;
  1653. procedure torddef.setsize;
  1654. const
  1655. sizetbl : array[tordtype] of longint = (
  1656. 0,
  1657. 1,2,4,8,
  1658. 1,2,4,8,
  1659. 1,2,4,8,
  1660. 1,2,4,8,
  1661. 1,2,8
  1662. );
  1663. begin
  1664. savesize:=sizetbl[ordtype];
  1665. end;
  1666. function torddef.packedbitsize: asizeint;
  1667. var
  1668. sizeval: tconstexprint;
  1669. power: longint;
  1670. begin
  1671. result := 0;
  1672. if ordtype = uvoid then
  1673. exit;
  1674. {$ifndef cpu64bitalu}
  1675. if (ordtype in [s64bit,u64bit]) then
  1676. {$else not cpu64bitalu}
  1677. if (ordtype = u64bit) or
  1678. ((ordtype = s64bit) and
  1679. ((low <= (system.low(int64) div 2)) or
  1680. (high > (system.high(int64) div 2)))) then
  1681. {$endif cpu64bitalu}
  1682. result := 64
  1683. else if (low >= 0) and
  1684. (high <= 1) then
  1685. result := 1
  1686. else
  1687. begin
  1688. if (low>=0) then
  1689. sizeval:=high
  1690. else
  1691. { don't count 0 twice }
  1692. sizeval:=(cutils.max(-low,high)*2)-1;
  1693. { 256 must become 512 etc. }
  1694. nextpowerof2(sizeval+1,power);
  1695. result := power;
  1696. end;
  1697. end;
  1698. function torddef.getvardef : longint;
  1699. const
  1700. basetype2vardef : array[tordtype] of longint = (
  1701. varUndefined,
  1702. varbyte,varword,varlongword,varqword,
  1703. varshortint,varsmallint,varinteger,varint64,
  1704. varboolean,varboolean,varboolean,varboolean,
  1705. varboolean,varboolean,varUndefined,varUndefined,
  1706. varUndefined,varUndefined,varCurrency);
  1707. begin
  1708. result:=basetype2vardef[ordtype];
  1709. end;
  1710. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1711. begin
  1712. inherited ppuwrite(ppufile);
  1713. ppufile.putbyte(byte(ordtype));
  1714. ppufile.putexprint(low);
  1715. ppufile.putexprint(high);
  1716. ppufile.writeentry(iborddef);
  1717. end;
  1718. function torddef.is_publishable : boolean;
  1719. begin
  1720. is_publishable:=(ordtype<>uvoid);
  1721. end;
  1722. function torddef.GetTypeName : string;
  1723. const
  1724. names : array[tordtype] of string[20] = (
  1725. 'untyped',
  1726. 'Byte','Word','DWord','QWord',
  1727. 'ShortInt','SmallInt','LongInt','Int64',
  1728. 'Boolean','Boolean16','Boolean32','Boolean64',
  1729. 'ByteBool','WordBool','LongBool','QWordBool',
  1730. 'Char','WideChar','Currency');
  1731. begin
  1732. GetTypeName:=names[ordtype];
  1733. end;
  1734. {****************************************************************************
  1735. TFLOATDEF
  1736. ****************************************************************************}
  1737. constructor tfloatdef.create(t : tfloattype);
  1738. begin
  1739. inherited create(floatdef);
  1740. floattype:=t;
  1741. setsize;
  1742. end;
  1743. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1744. begin
  1745. inherited ppuload(floatdef,ppufile);
  1746. floattype:=tfloattype(ppufile.getbyte);
  1747. setsize;
  1748. end;
  1749. function tfloatdef.getcopy : tstoreddef;
  1750. begin
  1751. result:=tfloatdef.create(floattype);
  1752. result.typ:=floatdef;
  1753. tfloatdef(result).savesize:=savesize;
  1754. end;
  1755. function tfloatdef.alignment:shortint;
  1756. begin
  1757. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1758. case floattype of
  1759. s80real: result:=16;
  1760. s64real,
  1761. s64currency,
  1762. s64comp : result:=4;
  1763. else
  1764. result := inherited alignment;
  1765. end
  1766. else
  1767. result := inherited alignment;
  1768. end;
  1769. procedure tfloatdef.setsize;
  1770. begin
  1771. case floattype of
  1772. s32real : savesize:=4;
  1773. s80real : savesize:=10;
  1774. sc80real:
  1775. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1776. system_x86_64_linux,system_x86_64_freebsd,
  1777. system_x86_64_solaris,system_x86_64_embedded] then
  1778. savesize:=16
  1779. else
  1780. savesize:=12;
  1781. s64real,
  1782. s64currency,
  1783. s64comp : savesize:=8;
  1784. else
  1785. savesize:=0;
  1786. end;
  1787. end;
  1788. function tfloatdef.getvardef : longint;
  1789. const
  1790. floattype2vardef : array[tfloattype] of longint = (
  1791. varSingle,varDouble,varUndefined,varUndefined,
  1792. varUndefined,varCurrency,varUndefined);
  1793. begin
  1794. if (upper(typename)='TDATETIME') and
  1795. assigned(owner) and
  1796. assigned(owner.name) and
  1797. (owner.name^='SYSTEM') then
  1798. result:=varDate
  1799. else
  1800. result:=floattype2vardef[floattype];
  1801. end;
  1802. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1803. begin
  1804. inherited ppuwrite(ppufile);
  1805. ppufile.putbyte(byte(floattype));
  1806. ppufile.writeentry(ibfloatdef);
  1807. end;
  1808. function tfloatdef.is_publishable : boolean;
  1809. begin
  1810. is_publishable:=true;
  1811. end;
  1812. function tfloatdef.GetTypeName : string;
  1813. const
  1814. names : array[tfloattype] of string[20] = (
  1815. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1816. begin
  1817. GetTypeName:=names[floattype];
  1818. end;
  1819. {****************************************************************************
  1820. TFILEDEF
  1821. ****************************************************************************}
  1822. constructor tfiledef.createtext;
  1823. begin
  1824. inherited create(filedef);
  1825. filetyp:=ft_text;
  1826. typedfiledef:=nil;
  1827. setsize;
  1828. end;
  1829. constructor tfiledef.createuntyped;
  1830. begin
  1831. inherited create(filedef);
  1832. filetyp:=ft_untyped;
  1833. typedfiledef:=nil;
  1834. setsize;
  1835. end;
  1836. constructor tfiledef.createtyped(def:tdef);
  1837. begin
  1838. inherited create(filedef);
  1839. filetyp:=ft_typed;
  1840. typedfiledef:=def;
  1841. setsize;
  1842. end;
  1843. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1844. begin
  1845. inherited ppuload(filedef,ppufile);
  1846. filetyp:=tfiletyp(ppufile.getbyte);
  1847. if filetyp=ft_typed then
  1848. ppufile.getderef(typedfiledefderef)
  1849. else
  1850. typedfiledef:=nil;
  1851. setsize;
  1852. end;
  1853. function tfiledef.getcopy : tstoreddef;
  1854. begin
  1855. case filetyp of
  1856. ft_typed:
  1857. result:=tfiledef.createtyped(typedfiledef);
  1858. ft_untyped:
  1859. result:=tfiledef.createuntyped;
  1860. ft_text:
  1861. result:=tfiledef.createtext;
  1862. else
  1863. internalerror(2004121201);
  1864. end;
  1865. end;
  1866. procedure tfiledef.buildderef;
  1867. begin
  1868. inherited buildderef;
  1869. if filetyp=ft_typed then
  1870. typedfiledefderef.build(typedfiledef);
  1871. end;
  1872. procedure tfiledef.deref;
  1873. begin
  1874. inherited deref;
  1875. if filetyp=ft_typed then
  1876. typedfiledef:=tdef(typedfiledefderef.resolve);
  1877. end;
  1878. procedure tfiledef.setsize;
  1879. begin
  1880. {$ifdef cpu64bitaddr}
  1881. case filetyp of
  1882. ft_text :
  1883. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1884. savesize:=632{+8}
  1885. else
  1886. savesize:=628{+8};
  1887. ft_typed,
  1888. ft_untyped :
  1889. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1890. savesize:=372
  1891. else
  1892. savesize:=368;
  1893. end;
  1894. {$endif cpu64bitaddr}
  1895. {$ifdef cpu32bitaddr}
  1896. case filetyp of
  1897. ft_text :
  1898. savesize:=592{+4};
  1899. ft_typed,
  1900. ft_untyped :
  1901. savesize:=332;
  1902. end;
  1903. {$endif cpu32bitaddr}
  1904. {$ifdef cpu8bitaddr}
  1905. case filetyp of
  1906. ft_text :
  1907. savesize:=127;
  1908. ft_typed,
  1909. ft_untyped :
  1910. savesize:=127;
  1911. end;
  1912. {$endif cpu8bitaddr}
  1913. end;
  1914. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1915. begin
  1916. inherited ppuwrite(ppufile);
  1917. ppufile.putbyte(byte(filetyp));
  1918. if filetyp=ft_typed then
  1919. ppufile.putderef(typedfiledefderef);
  1920. ppufile.writeentry(ibfiledef);
  1921. end;
  1922. function tfiledef.GetTypeName : string;
  1923. begin
  1924. case filetyp of
  1925. ft_untyped:
  1926. GetTypeName:='File';
  1927. ft_typed:
  1928. GetTypeName:='File Of '+typedfiledef.typename;
  1929. ft_text:
  1930. GetTypeName:='Text'
  1931. end;
  1932. end;
  1933. function tfiledef.getmangledparaname : string;
  1934. begin
  1935. case filetyp of
  1936. ft_untyped:
  1937. getmangledparaname:='FILE';
  1938. ft_typed:
  1939. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1940. ft_text:
  1941. getmangledparaname:='TEXT'
  1942. end;
  1943. end;
  1944. {****************************************************************************
  1945. TVARIANTDEF
  1946. ****************************************************************************}
  1947. constructor tvariantdef.create(v : tvarianttype);
  1948. begin
  1949. inherited create(variantdef);
  1950. varianttype:=v;
  1951. setsize;
  1952. end;
  1953. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1954. begin
  1955. inherited ppuload(variantdef,ppufile);
  1956. varianttype:=tvarianttype(ppufile.getbyte);
  1957. setsize;
  1958. end;
  1959. function tvariantdef.getcopy : tstoreddef;
  1960. begin
  1961. result:=tvariantdef.create(varianttype);
  1962. end;
  1963. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1964. begin
  1965. inherited ppuwrite(ppufile);
  1966. ppufile.putbyte(byte(varianttype));
  1967. ppufile.writeentry(ibvariantdef);
  1968. end;
  1969. function tvariantdef.getvardef : longint;
  1970. begin
  1971. Result:=varVariant;
  1972. end;
  1973. procedure tvariantdef.setsize;
  1974. begin
  1975. {$ifdef cpu64bitaddr}
  1976. savesize:=24;
  1977. {$else cpu64bitaddr}
  1978. savesize:=16;
  1979. {$endif cpu64bitaddr}
  1980. end;
  1981. function tvariantdef.GetTypeName : string;
  1982. begin
  1983. case varianttype of
  1984. vt_normalvariant:
  1985. GetTypeName:='Variant';
  1986. vt_olevariant:
  1987. GetTypeName:='OleVariant';
  1988. end;
  1989. end;
  1990. function tvariantdef.needs_inittable : boolean;
  1991. begin
  1992. needs_inittable:=true;
  1993. end;
  1994. function tvariantdef.is_publishable : boolean;
  1995. begin
  1996. is_publishable:=true;
  1997. end;
  1998. {****************************************************************************
  1999. TABSTRACtpointerdef
  2000. ****************************************************************************}
  2001. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2002. begin
  2003. inherited create(dt);
  2004. pointeddef:=def;
  2005. savesize:=sizeof(pint);
  2006. end;
  2007. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2008. begin
  2009. inherited ppuload(dt,ppufile);
  2010. ppufile.getderef(pointeddefderef);
  2011. savesize:=sizeof(pint);
  2012. end;
  2013. procedure tabstractpointerdef.buildderef;
  2014. begin
  2015. inherited buildderef;
  2016. pointeddefderef.build(pointeddef);
  2017. end;
  2018. procedure tabstractpointerdef.deref;
  2019. begin
  2020. inherited deref;
  2021. pointeddef:=tdef(pointeddefderef.resolve);
  2022. end;
  2023. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2024. begin
  2025. inherited ppuwrite(ppufile);
  2026. ppufile.putderef(pointeddefderef);
  2027. end;
  2028. {****************************************************************************
  2029. tpointerdef
  2030. ****************************************************************************}
  2031. constructor tpointerdef.create(def:tdef);
  2032. begin
  2033. inherited create(pointerdef,def);
  2034. is_far:=false;
  2035. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2036. end;
  2037. constructor tpointerdef.createfar(def:tdef);
  2038. begin
  2039. inherited create(pointerdef,def);
  2040. is_far:=true;
  2041. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2042. end;
  2043. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2044. begin
  2045. inherited ppuload(pointerdef,ppufile);
  2046. is_far:=(ppufile.getbyte<>0);
  2047. has_pointer_math:=(ppufile.getbyte<>0);
  2048. end;
  2049. function tpointerdef.getcopy : tstoreddef;
  2050. begin
  2051. { don't use direct pointeddef if it is a forwarddef because in other case
  2052. one of them will be destroyed on forward type resolve and the second will
  2053. point to garbage }
  2054. if pointeddef.typ=forwarddef then
  2055. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  2056. else
  2057. result:=tpointerdef.create(pointeddef);
  2058. tpointerdef(result).is_far:=is_far;
  2059. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2060. tpointerdef(result).savesize:=savesize;
  2061. end;
  2062. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2063. begin
  2064. inherited ppuwrite(ppufile);
  2065. ppufile.putbyte(byte(is_far));
  2066. ppufile.putbyte(byte(has_pointer_math));
  2067. ppufile.writeentry(ibpointerdef);
  2068. end;
  2069. function tpointerdef.GetTypeName : string;
  2070. begin
  2071. if is_far then
  2072. GetTypeName:='^'+pointeddef.typename+';far'
  2073. else
  2074. GetTypeName:='^'+pointeddef.typename;
  2075. end;
  2076. {****************************************************************************
  2077. TCLASSREFDEF
  2078. ****************************************************************************}
  2079. constructor tclassrefdef.create(def:tdef);
  2080. begin
  2081. inherited create(classrefdef,def);
  2082. end;
  2083. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2084. begin
  2085. inherited ppuload(classrefdef,ppufile);
  2086. end;
  2087. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2088. begin
  2089. inherited ppuwrite(ppufile);
  2090. ppufile.writeentry(ibclassrefdef);
  2091. end;
  2092. function tclassrefdef.getcopy:tstoreddef;
  2093. begin
  2094. if pointeddef.typ=forwarddef then
  2095. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2096. else
  2097. result:=tclassrefdef.create(pointeddef);
  2098. tclassrefdef(result).savesize:=savesize;
  2099. end;
  2100. function tclassrefdef.GetTypeName : string;
  2101. begin
  2102. GetTypeName:='Class Of '+pointeddef.typename;
  2103. end;
  2104. function tclassrefdef.is_publishable : boolean;
  2105. begin
  2106. result:=true;
  2107. end;
  2108. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2109. begin
  2110. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2111. result:=inherited rtti_mangledname(rt)
  2112. else
  2113. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2114. end;
  2115. procedure tclassrefdef.register_created_object_type;
  2116. begin
  2117. tobjectdef(pointeddef).register_created_classref_type;
  2118. end;
  2119. {***************************************************************************
  2120. TSETDEF
  2121. ***************************************************************************}
  2122. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2123. var
  2124. setallocbits: aint;
  2125. packedsavesize: aint;
  2126. begin
  2127. inherited create(setdef);
  2128. elementdef:=def;
  2129. setmax:=high;
  2130. if (current_settings.setalloc=0) then
  2131. begin
  2132. setbase:=0;
  2133. if (high<32) then
  2134. savesize:=Sizeof(longint)
  2135. else if (high<256) then
  2136. savesize:=32
  2137. else
  2138. savesize:=(high+7) div 8
  2139. end
  2140. else
  2141. begin
  2142. setallocbits:=current_settings.setalloc*8;
  2143. setbase:=low and not(setallocbits-1);
  2144. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2145. savesize:=packedsavesize;
  2146. if savesize=3 then
  2147. savesize:=4;
  2148. end;
  2149. end;
  2150. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2151. begin
  2152. inherited ppuload(setdef,ppufile);
  2153. ppufile.getderef(elementdefderef);
  2154. savesize:=ppufile.getaint;
  2155. setbase:=ppufile.getaint;
  2156. setmax:=ppufile.getaint;
  2157. end;
  2158. function tsetdef.getcopy : tstoreddef;
  2159. begin
  2160. result:=tsetdef.create(elementdef,setbase,setmax);
  2161. { the copy might have been created with a different setalloc setting }
  2162. tsetdef(result).savesize:=savesize;
  2163. end;
  2164. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2165. begin
  2166. inherited ppuwrite(ppufile);
  2167. ppufile.putderef(elementdefderef);
  2168. ppufile.putaint(savesize);
  2169. ppufile.putaint(setbase);
  2170. ppufile.putaint(setmax);
  2171. ppufile.writeentry(ibsetdef);
  2172. end;
  2173. procedure tsetdef.buildderef;
  2174. begin
  2175. inherited buildderef;
  2176. elementdefderef.build(elementdef);
  2177. end;
  2178. procedure tsetdef.deref;
  2179. begin
  2180. inherited deref;
  2181. elementdef:=tdef(elementdefderef.resolve);
  2182. end;
  2183. function tsetdef.is_publishable : boolean;
  2184. begin
  2185. is_publishable:=savesize in [1,2,4];
  2186. end;
  2187. function tsetdef.GetTypeName : string;
  2188. begin
  2189. if assigned(elementdef) then
  2190. GetTypeName:='Set Of '+elementdef.typename
  2191. else
  2192. GetTypeName:='Empty Set';
  2193. end;
  2194. {***************************************************************************
  2195. TFORMALDEF
  2196. ***************************************************************************}
  2197. constructor tformaldef.create(Atyped:boolean);
  2198. begin
  2199. inherited create(formaldef);
  2200. typed:=Atyped;
  2201. savesize:=0;
  2202. end;
  2203. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2204. begin
  2205. inherited ppuload(formaldef,ppufile);
  2206. typed:=boolean(ppufile.getbyte);
  2207. savesize:=0;
  2208. end;
  2209. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2210. begin
  2211. inherited ppuwrite(ppufile);
  2212. ppufile.putbyte(byte(typed));
  2213. ppufile.writeentry(ibformaldef);
  2214. end;
  2215. function tformaldef.GetTypeName : string;
  2216. begin
  2217. if typed then
  2218. GetTypeName:='<Typed formal type>'
  2219. else
  2220. GetTypeName:='<Formal type>';
  2221. end;
  2222. {***************************************************************************
  2223. TARRAYDEF
  2224. ***************************************************************************}
  2225. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2226. begin
  2227. inherited create(arraydef);
  2228. lowrange:=l;
  2229. highrange:=h;
  2230. rangedef:=def;
  2231. _elementdef:=nil;
  2232. arrayoptions:=[];
  2233. symtable:=tarraysymtable.create(self);
  2234. end;
  2235. destructor tarraydef.destroy;
  2236. begin
  2237. symtable.free;
  2238. symtable:=nil;
  2239. inherited;
  2240. end;
  2241. constructor tarraydef.create_from_pointer(def:tdef);
  2242. begin
  2243. { use -1 so that the elecount will not overflow }
  2244. self.create(0,high(aint)-1,s32inttype);
  2245. arrayoptions:=[ado_IsConvertedPointer];
  2246. setelementdef(def);
  2247. end;
  2248. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2249. begin
  2250. inherited ppuload(arraydef,ppufile);
  2251. { the addresses are calculated later }
  2252. ppufile.getderef(_elementdefderef);
  2253. ppufile.getderef(rangedefderef);
  2254. lowrange:=ppufile.getaint;
  2255. highrange:=ppufile.getaint;
  2256. ppufile.getsmallset(arrayoptions);
  2257. symtable:=tarraysymtable.create(self);
  2258. tarraysymtable(symtable).ppuload(ppufile)
  2259. end;
  2260. function tarraydef.getcopy : tstoreddef;
  2261. begin
  2262. result:=tarraydef.create(lowrange,highrange,rangedef);
  2263. tarraydef(result).arrayoptions:=arrayoptions;
  2264. tarraydef(result)._elementdef:=_elementdef;
  2265. end;
  2266. procedure tarraydef.buildderef;
  2267. begin
  2268. inherited buildderef;
  2269. tarraysymtable(symtable).buildderef;
  2270. _elementdefderef.build(_elementdef);
  2271. rangedefderef.build(rangedef);
  2272. end;
  2273. procedure tarraydef.deref;
  2274. begin
  2275. inherited deref;
  2276. tarraysymtable(symtable).deref;
  2277. _elementdef:=tdef(_elementdefderef.resolve);
  2278. rangedef:=tdef(rangedefderef.resolve);
  2279. end;
  2280. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2281. begin
  2282. inherited ppuwrite(ppufile);
  2283. ppufile.putderef(_elementdefderef);
  2284. ppufile.putderef(rangedefderef);
  2285. ppufile.putaint(lowrange);
  2286. ppufile.putaint(highrange);
  2287. ppufile.putsmallset(arrayoptions);
  2288. ppufile.writeentry(ibarraydef);
  2289. tarraysymtable(symtable).ppuwrite(ppufile);
  2290. end;
  2291. function tarraydef.elesize : asizeint;
  2292. begin
  2293. if (ado_IsBitPacked in arrayoptions) then
  2294. internalerror(2006080101);
  2295. if assigned(_elementdef) then
  2296. result:=_elementdef.size
  2297. else
  2298. result:=0;
  2299. end;
  2300. function tarraydef.elepackedbitsize : asizeint;
  2301. begin
  2302. if not(ado_IsBitPacked in arrayoptions) then
  2303. internalerror(2006080102);
  2304. if assigned(_elementdef) then
  2305. result:=_elementdef.packedbitsize
  2306. else
  2307. result:=0;
  2308. end;
  2309. function tarraydef.elecount : asizeuint;
  2310. var
  2311. qhigh,qlow : qword;
  2312. begin
  2313. if ado_IsDynamicArray in arrayoptions then
  2314. begin
  2315. result:=0;
  2316. exit;
  2317. end;
  2318. if (highrange>0) and (lowrange<0) then
  2319. begin
  2320. qhigh:=highrange;
  2321. qlow:=qword(-lowrange);
  2322. { prevent overflow, return 0 to indicate overflow }
  2323. if qhigh+qlow>qword(high(asizeint)-1) then
  2324. result:=0
  2325. else
  2326. result:=qhigh+qlow+1;
  2327. end
  2328. else
  2329. result:=int64(highrange)-lowrange+1;
  2330. end;
  2331. function tarraydef.size : asizeint;
  2332. var
  2333. cachedelecount : asizeuint;
  2334. cachedelesize : asizeint;
  2335. begin
  2336. if ado_IsDynamicArray in arrayoptions then
  2337. begin
  2338. size:=sizeof(pint);
  2339. exit;
  2340. end;
  2341. { Tarraydef.size may never be called for an open array! }
  2342. if highrange<lowrange then
  2343. internalerror(99080501);
  2344. if not (ado_IsBitPacked in arrayoptions) then
  2345. cachedelesize:=elesize
  2346. else
  2347. cachedelesize := elepackedbitsize;
  2348. cachedelecount:=elecount;
  2349. if (cachedelesize = 0) then
  2350. begin
  2351. size := 0;
  2352. exit;
  2353. end;
  2354. if (cachedelecount = 0) then
  2355. begin
  2356. size := -1;
  2357. exit;
  2358. end;
  2359. { prevent overflow, return -1 to indicate overflow }
  2360. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2361. if (cachedelecount > asizeuint(high(asizeint))) or
  2362. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2363. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2364. accessing the array, see ncgmem (PFV) }
  2365. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2366. begin
  2367. result:=-1;
  2368. exit;
  2369. end;
  2370. result:=cachedelesize*asizeint(cachedelecount);
  2371. if (ado_IsBitPacked in arrayoptions) then
  2372. { can't just add 7 and divide by 8, because that may overflow }
  2373. result:=result div 8 + ord((result mod 8)<>0);
  2374. end;
  2375. procedure tarraydef.setelementdef(def:tdef);
  2376. begin
  2377. _elementdef:=def;
  2378. if not(
  2379. (ado_IsDynamicArray in arrayoptions) or
  2380. (ado_IsConvertedPointer in arrayoptions) or
  2381. (highrange<lowrange)
  2382. ) and
  2383. (size=-1) then
  2384. Message(sym_e_segment_too_large);
  2385. end;
  2386. function tarraydef.alignment : shortint;
  2387. begin
  2388. { alignment of dyn. arrays doesn't depend on the element size }
  2389. if (ado_IsDynamicArray in arrayoptions) then
  2390. alignment:=size_2_align(sizeof(pint))
  2391. { alignment is the alignment of the elements }
  2392. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2393. ((elementdef.typ=objectdef) and
  2394. is_object(elementdef)) then
  2395. alignment:=elementdef.alignment
  2396. { alignment is the size of the elements }
  2397. else if not (ado_IsBitPacked in arrayoptions) then
  2398. alignment:=size_2_align(elesize)
  2399. else
  2400. alignment:=packedbitsloadsize(elepackedbitsize);
  2401. end;
  2402. function tarraydef.needs_inittable : boolean;
  2403. begin
  2404. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2405. end;
  2406. function tarraydef.GetTypeName : string;
  2407. begin
  2408. if (ado_IsConstString in arrayoptions) then
  2409. result:='Constant String'
  2410. else if (ado_isarrayofconst in arrayoptions) or
  2411. (ado_isConstructor in arrayoptions) then
  2412. begin
  2413. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2414. GetTypeName:='Array Of Const'
  2415. else
  2416. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2417. end
  2418. else if (ado_IsDynamicArray in arrayoptions) then
  2419. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2420. else if ((highrange=-1) and (lowrange=0)) then
  2421. GetTypeName:='Open Array Of '+elementdef.typename
  2422. else
  2423. begin
  2424. result := '';
  2425. if (ado_IsBitPacked in arrayoptions) then
  2426. result:='Packed ';
  2427. if rangedef.typ=enumdef then
  2428. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2429. else
  2430. result:=result+'Array['+tostr(lowrange)+'..'+
  2431. tostr(highrange)+'] Of '+elementdef.typename
  2432. end;
  2433. end;
  2434. function tarraydef.getmangledparaname : string;
  2435. begin
  2436. if ado_isarrayofconst in arrayoptions then
  2437. getmangledparaname:='array_of_const'
  2438. else
  2439. if ((highrange=-1) and (lowrange=0)) then
  2440. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2441. else
  2442. internalerror(200204176);
  2443. end;
  2444. function tarraydef.is_publishable : boolean;
  2445. begin
  2446. Result:=ado_IsDynamicArray in arrayoptions;
  2447. end;
  2448. {***************************************************************************
  2449. tabstractrecorddef
  2450. ***************************************************************************}
  2451. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2452. begin
  2453. inherited create(dt);
  2454. objname:=stringdup(upper(n));
  2455. objrealname:=stringdup(n);
  2456. objectoptions:=[];
  2457. end;
  2458. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2459. begin
  2460. inherited ppuload(dt,ppufile);
  2461. objrealname:=stringdup(ppufile.getstring);
  2462. objname:=stringdup(upper(objrealname^));
  2463. ppufile.getsmallset(objectoptions);
  2464. end;
  2465. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2466. begin
  2467. inherited ppuwrite(ppufile);
  2468. ppufile.putstring(objrealname^);
  2469. ppufile.putsmallset(objectoptions);
  2470. end;
  2471. destructor tabstractrecorddef.destroy;
  2472. begin
  2473. stringdispose(objname);
  2474. stringdispose(objrealname);
  2475. inherited destroy;
  2476. end;
  2477. procedure tabstractrecorddef.check_forwards;
  2478. begin
  2479. tstoredsymtable(symtable).check_forwards;
  2480. end;
  2481. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2482. var
  2483. i: longint;
  2484. sym: tsym;
  2485. begin
  2486. for i:=0 to symtable.SymList.Count-1 do
  2487. begin
  2488. sym:=tsym(symtable.SymList[i]);
  2489. if sym.typ=procsym then
  2490. begin
  2491. result:=tprocsym(sym).find_procdef_bytype(pt);
  2492. if assigned(result) then
  2493. exit;
  2494. end;
  2495. end;
  2496. result:=nil;
  2497. end;
  2498. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2499. begin
  2500. if t=gs_record then
  2501. GetSymtable:=symtable
  2502. else
  2503. GetSymtable:=nil;
  2504. end;
  2505. function tabstractrecorddef.is_packed:boolean;
  2506. begin
  2507. result:=tabstractrecordsymtable(symtable).is_packed;
  2508. end;
  2509. function tabstractrecorddef.RttiName: string;
  2510. begin
  2511. Result:=OwnerHierarchyName+objrealname^;
  2512. end;
  2513. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2514. var
  2515. sym : tsym;
  2516. i : integer;
  2517. pd : tprocdef;
  2518. hashedid : THashedIDString;
  2519. begin
  2520. result:=nil;
  2521. hashedid.id:='GETENUMERATOR';
  2522. sym:=tsym(symtable.FindWithHash(hashedid));
  2523. if assigned(sym) and (sym.typ=procsym) then
  2524. begin
  2525. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2526. begin
  2527. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2528. if (pd.proctypeoption = potype_function) and
  2529. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2530. (pd.visibility >= vis_public) then
  2531. begin
  2532. result:=pd;
  2533. exit;
  2534. end;
  2535. end;
  2536. end;
  2537. end;
  2538. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2539. var
  2540. sym : tsym;
  2541. i : integer;
  2542. pd : tprocdef;
  2543. hashedid : THashedIDString;
  2544. begin
  2545. result:=nil;
  2546. // first search for po_enumerator_movenext method modifier
  2547. // then search for public function MoveNext: Boolean
  2548. for i:=0 to symtable.SymList.Count-1 do
  2549. begin
  2550. sym:=TSym(symtable.SymList[i]);
  2551. if (sym.typ=procsym) then
  2552. begin
  2553. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2554. if assigned(pd) then
  2555. begin
  2556. result:=pd;
  2557. exit;
  2558. end;
  2559. end;
  2560. end;
  2561. hashedid.id:='MOVENEXT';
  2562. sym:=tsym(symtable.FindWithHash(hashedid));
  2563. if assigned(sym) and (sym.typ=procsym) then
  2564. begin
  2565. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2566. begin
  2567. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2568. if (pd.proctypeoption = potype_function) and
  2569. is_boolean(pd.returndef) and
  2570. (pd.minparacount = 0) and
  2571. (pd.visibility >= vis_public) then
  2572. begin
  2573. result:=pd;
  2574. exit;
  2575. end;
  2576. end;
  2577. end;
  2578. end;
  2579. function tabstractrecorddef.search_enumerator_current: tsym;
  2580. var
  2581. sym: tsym;
  2582. i: integer;
  2583. hashedid : THashedIDString;
  2584. begin
  2585. result:=nil;
  2586. // first search for ppo_enumerator_current property modifier
  2587. // then search for public property Current
  2588. for i:=0 to symtable.SymList.Count-1 do
  2589. begin
  2590. sym:=TSym(symtable.SymList[i]);
  2591. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2592. begin
  2593. result:=sym;
  2594. exit;
  2595. end;
  2596. end;
  2597. hashedid.id:='CURRENT';
  2598. sym:=tsym(symtable.FindWithHash(hashedid));
  2599. if assigned(sym) and (sym.typ=propertysym) and
  2600. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2601. begin
  2602. result:=sym;
  2603. exit;
  2604. end;
  2605. end;
  2606. {***************************************************************************
  2607. trecorddef
  2608. ***************************************************************************}
  2609. constructor trecorddef.create(const n:string; p:TSymtable);
  2610. begin
  2611. inherited create(n,recorddef);
  2612. symtable:=p;
  2613. { we can own the symtable only if nobody else owns a copy so far }
  2614. if symtable.refcount=1 then
  2615. symtable.defowner:=self;
  2616. isunion:=false;
  2617. end;
  2618. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2619. begin
  2620. inherited ppuload(recorddef,ppufile);
  2621. if df_copied_def in defoptions then
  2622. ppufile.getderef(cloneddefderef)
  2623. else
  2624. begin
  2625. symtable:=trecordsymtable.create(objrealname^,0);
  2626. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2627. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2628. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2629. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2630. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  2631. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  2632. trecordsymtable(symtable).ppuload(ppufile);
  2633. { requires usefieldalignment to be set }
  2634. symtable.defowner:=self;
  2635. end;
  2636. isunion:=false;
  2637. end;
  2638. destructor trecorddef.destroy;
  2639. begin
  2640. if assigned(symtable) then
  2641. begin
  2642. symtable.free;
  2643. symtable:=nil;
  2644. end;
  2645. inherited destroy;
  2646. end;
  2647. function trecorddef.getcopy : tstoreddef;
  2648. begin
  2649. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2650. trecorddef(result).isunion:=isunion;
  2651. include(trecorddef(result).defoptions,df_copied_def);
  2652. end;
  2653. function trecorddef.needs_inittable : boolean;
  2654. begin
  2655. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2656. end;
  2657. procedure trecorddef.buildderef;
  2658. begin
  2659. inherited buildderef;
  2660. if df_copied_def in defoptions then
  2661. cloneddefderef.build(symtable.defowner)
  2662. else
  2663. tstoredsymtable(symtable).buildderef;
  2664. end;
  2665. procedure trecorddef.deref;
  2666. begin
  2667. inherited deref;
  2668. { now dereference the definitions }
  2669. if df_copied_def in defoptions then
  2670. begin
  2671. cloneddef:=trecorddef(cloneddefderef.resolve);
  2672. symtable:=cloneddef.symtable.getcopy;
  2673. end
  2674. else
  2675. tstoredsymtable(symtable).deref;
  2676. { assign TGUID? load only from system unit }
  2677. if not(assigned(rec_tguid)) and
  2678. (upper(typename)='TGUID') and
  2679. assigned(owner) and
  2680. assigned(owner.name) and
  2681. (owner.name^='SYSTEM') then
  2682. rec_tguid:=self;
  2683. { assign JMP_BUF? load only from system unit }
  2684. if not(assigned(rec_jmp_buf)) and
  2685. (upper(typename)='JMP_BUF') and
  2686. assigned(owner) and
  2687. assigned(owner.name) and
  2688. (owner.name^='SYSTEM') then
  2689. rec_jmp_buf:=self;
  2690. end;
  2691. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2692. begin
  2693. inherited ppuwrite(ppufile);
  2694. if df_copied_def in defoptions then
  2695. ppufile.putderef(cloneddefderef)
  2696. else
  2697. begin
  2698. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2699. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2700. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2701. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2702. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  2703. ppufile.putword(trecordsymtable(symtable).paddingsize);
  2704. end;
  2705. ppufile.writeentry(ibrecorddef);
  2706. if not(df_copied_def in defoptions) then
  2707. trecordsymtable(symtable).ppuwrite(ppufile);
  2708. end;
  2709. function trecorddef.size:asizeint;
  2710. begin
  2711. result:=trecordsymtable(symtable).datasize;
  2712. end;
  2713. function trecorddef.alignment:shortint;
  2714. begin
  2715. alignment:=trecordsymtable(symtable).recordalignment;
  2716. end;
  2717. function trecorddef.padalignment:shortint;
  2718. begin
  2719. padalignment := trecordsymtable(symtable).padalignment;
  2720. end;
  2721. function trecorddef.GetTypeName : string;
  2722. begin
  2723. GetTypeName:='<record type>'
  2724. end;
  2725. {***************************************************************************
  2726. TABSTRACTPROCDEF
  2727. ***************************************************************************}
  2728. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2729. begin
  2730. inherited create(dt);
  2731. parast:=tparasymtable.create(self,level);
  2732. paras:=nil;
  2733. minparacount:=0;
  2734. maxparacount:=0;
  2735. proctypeoption:=potype_none;
  2736. proccalloption:=pocall_none;
  2737. procoptions:=[];
  2738. returndef:=voidtype;
  2739. savesize:=sizeof(pint);
  2740. callerargareasize:=0;
  2741. calleeargareasize:=0;
  2742. has_paraloc_info:=callnoside;
  2743. funcretloc[callerside].init;
  2744. funcretloc[calleeside].init;
  2745. check_mark_as_nested;
  2746. end;
  2747. destructor tabstractprocdef.destroy;
  2748. begin
  2749. if assigned(paras) then
  2750. begin
  2751. {$ifdef MEMDEBUG}
  2752. memprocpara.start;
  2753. {$endif MEMDEBUG}
  2754. paras.free;
  2755. paras:=nil;
  2756. {$ifdef MEMDEBUG}
  2757. memprocpara.stop;
  2758. {$endif MEMDEBUG}
  2759. end;
  2760. if assigned(parast) then
  2761. begin
  2762. {$ifdef MEMDEBUG}
  2763. memprocparast.start;
  2764. {$endif MEMDEBUG}
  2765. parast.free;
  2766. parast:=nil;
  2767. {$ifdef MEMDEBUG}
  2768. memprocparast.stop;
  2769. {$endif MEMDEBUG}
  2770. end;
  2771. funcretloc[callerside].done;
  2772. funcretloc[calleeside].done;
  2773. inherited destroy;
  2774. end;
  2775. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2776. begin
  2777. if (tsym(p).typ<>paravarsym) then
  2778. exit;
  2779. inc(plongint(arg)^);
  2780. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2781. begin
  2782. if not assigned(tparavarsym(p).defaultconstsym) then
  2783. inc(minparacount);
  2784. inc(maxparacount);
  2785. end;
  2786. end;
  2787. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2788. begin
  2789. if (tsym(p).typ<>paravarsym) then
  2790. exit;
  2791. paras.add(p);
  2792. end;
  2793. procedure tabstractprocdef.calcparas;
  2794. var
  2795. paracount : longint;
  2796. begin
  2797. { This can already be assigned when
  2798. we need to reresolve this unit (PFV) }
  2799. if assigned(paras) then
  2800. paras.free;
  2801. paras:=tparalist.create(false);
  2802. paracount:=0;
  2803. minparacount:=0;
  2804. maxparacount:=0;
  2805. parast.SymList.ForEachCall(@count_para,@paracount);
  2806. paras.capacity:=paracount;
  2807. { Insert parameters in table }
  2808. parast.SymList.ForEachCall(@insert_para,nil);
  2809. { Order parameters }
  2810. paras.sortparas;
  2811. end;
  2812. procedure tabstractprocdef.buildderef;
  2813. begin
  2814. { released procdef? }
  2815. if not assigned(parast) then
  2816. exit;
  2817. inherited buildderef;
  2818. returndefderef.build(returndef);
  2819. { parast }
  2820. tparasymtable(parast).buildderef;
  2821. end;
  2822. procedure tabstractprocdef.deref;
  2823. begin
  2824. inherited deref;
  2825. returndef:=tdef(returndefderef.resolve);
  2826. { parast }
  2827. tparasymtable(parast).deref;
  2828. { recalculated parameters }
  2829. calcparas;
  2830. end;
  2831. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2832. begin
  2833. inherited ppuload(dt,ppufile);
  2834. parast:=nil;
  2835. Paras:=nil;
  2836. minparacount:=0;
  2837. maxparacount:=0;
  2838. ppufile.getderef(returndefderef);
  2839. { TODO: remove fpu_used loading}
  2840. ppufile.getbyte;
  2841. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2842. proccalloption:=tproccalloption(ppufile.getbyte);
  2843. ppufile.getnormalset(procoptions);
  2844. funcretloc[callerside].init;
  2845. if po_explicitparaloc in procoptions then
  2846. funcretloc[callerside].ppuload(ppufile);
  2847. savesize:=sizeof(pint);
  2848. if (po_explicitparaloc in procoptions) then
  2849. has_paraloc_info:=callerside;
  2850. end;
  2851. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2852. var
  2853. oldintfcrc : boolean;
  2854. begin
  2855. { released procdef? }
  2856. if not assigned(parast) then
  2857. exit;
  2858. inherited ppuwrite(ppufile);
  2859. ppufile.putderef(returndefderef);
  2860. oldintfcrc:=ppufile.do_interface_crc;
  2861. ppufile.do_interface_crc:=false;
  2862. ppufile.putbyte(0);
  2863. ppufile.putbyte(ord(proctypeoption));
  2864. ppufile.putbyte(ord(proccalloption));
  2865. ppufile.putnormalset(procoptions);
  2866. ppufile.do_interface_crc:=oldintfcrc;
  2867. if (po_explicitparaloc in procoptions) then
  2868. funcretloc[callerside].ppuwrite(ppufile);
  2869. end;
  2870. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2871. var
  2872. hs,s : string;
  2873. hp : TParavarsym;
  2874. hpc : tconstsym;
  2875. first : boolean;
  2876. i : integer;
  2877. begin
  2878. s:='';
  2879. first:=true;
  2880. for i:=0 to paras.count-1 do
  2881. begin
  2882. hp:=tparavarsym(paras[i]);
  2883. if not(vo_is_hidden_para in hp.varoptions) or
  2884. (showhidden) then
  2885. begin
  2886. if first then
  2887. begin
  2888. s:=s+'(';
  2889. first:=false;
  2890. end
  2891. else
  2892. s:=s+',';
  2893. if vo_is_hidden_para in hp.varoptions then
  2894. s:=s+'<';
  2895. case hp.varspez of
  2896. vs_var :
  2897. s:=s+'var ';
  2898. vs_const :
  2899. s:=s+'const ';
  2900. vs_out :
  2901. s:=s+'out ';
  2902. vs_constref :
  2903. s:=s+'constref ';
  2904. end;
  2905. if hp.univpara then
  2906. s:=s+'univ ';
  2907. if assigned(hp.vardef.typesym) then
  2908. begin
  2909. hs:=hp.vardef.typesym.realname;
  2910. if hs[1]<>'$' then
  2911. s:=s+hs
  2912. else
  2913. s:=s+hp.vardef.GetTypeName;
  2914. end
  2915. else
  2916. s:=s+hp.vardef.GetTypeName;
  2917. { default value }
  2918. if assigned(hp.defaultconstsym) then
  2919. begin
  2920. hpc:=tconstsym(hp.defaultconstsym);
  2921. hs:='';
  2922. case hpc.consttyp of
  2923. conststring,
  2924. constresourcestring :
  2925. begin
  2926. If hpc.value.len>0 then
  2927. begin
  2928. setLength(hs,hpc.value.len);
  2929. { don't write past the end of hs if the constant
  2930. is > 255 chars }
  2931. move(hpc.value.valueptr^,hs[1],length(hs));
  2932. { make sure that constant strings with newline chars
  2933. don't create a linebreak in the assembler code,
  2934. since comments are line-based. Also remove nulls
  2935. because the comments are written as a pchar. }
  2936. ReplaceCase(hs,#0,'.');
  2937. ReplaceCase(hs,#10,'.');
  2938. ReplaceCase(hs,#13,'.');
  2939. end;
  2940. end;
  2941. constreal :
  2942. str(pbestreal(hpc.value.valueptr)^,hs);
  2943. constpointer :
  2944. hs:=tostr(hpc.value.valueordptr);
  2945. constord :
  2946. begin
  2947. if is_boolean(hpc.constdef) then
  2948. begin
  2949. if hpc.value.valueord<>0 then
  2950. hs:='TRUE'
  2951. else
  2952. hs:='FALSE';
  2953. end
  2954. else
  2955. hs:=tostr(hpc.value.valueord);
  2956. end;
  2957. constnil :
  2958. hs:='nil';
  2959. constset :
  2960. hs:='<set>';
  2961. end;
  2962. if hs<>'' then
  2963. s:=s+'="'+hs+'"';
  2964. end;
  2965. if vo_is_hidden_para in hp.varoptions then
  2966. s:=s+'>';
  2967. end;
  2968. end;
  2969. if not first then
  2970. s:=s+')';
  2971. if (po_varargs in procoptions) then
  2972. s:=s+';VarArgs';
  2973. typename_paras:=s;
  2974. end;
  2975. function tabstractprocdef.is_methodpointer:boolean;
  2976. begin
  2977. result:=false;
  2978. end;
  2979. function tabstractprocdef.is_addressonly:boolean;
  2980. begin
  2981. result:=true;
  2982. end;
  2983. function tabstractprocdef.no_self_node: boolean;
  2984. begin
  2985. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2986. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  2987. end;
  2988. procedure tabstractprocdef.check_mark_as_nested;
  2989. begin
  2990. { nested procvars require that nested functions use the Delphi-style
  2991. nested procedure calling convention }
  2992. if (parast.symtablelevel>normal_function_level) and
  2993. (m_nested_procvars in current_settings.modeswitches) then
  2994. include(procoptions,po_delphi_nested_cc);
  2995. end;
  2996. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2997. begin
  2998. if (side in [callerside,callbothsides]) and
  2999. not(has_paraloc_info in [callerside,callbothsides]) then
  3000. begin
  3001. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  3002. if has_paraloc_info in [calleeside,callbothsides] then
  3003. has_paraloc_info:=callbothsides
  3004. else
  3005. has_paraloc_info:=callerside;
  3006. end;
  3007. if (side in [calleeside,callbothsides]) and
  3008. not(has_paraloc_info in [calleeside,callbothsides]) then
  3009. begin
  3010. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  3011. if has_paraloc_info in [callerside,callbothsides] then
  3012. has_paraloc_info:=callbothsides
  3013. else
  3014. has_paraloc_info:=calleeside;
  3015. end;
  3016. end;
  3017. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  3018. var
  3019. p: tparavarsym;
  3020. ploc: PCGParalocation;
  3021. i: longint;
  3022. begin
  3023. result:=false;
  3024. init_paraloc_info(side);
  3025. for i:=0 to parast.SymList.Count-1 do
  3026. if tsym(parast.SymList[i]).typ=paravarsym then
  3027. begin
  3028. p:=tparavarsym(parast.SymList[i]);
  3029. { check if no parameter is located on the stack }
  3030. if is_open_array(p.vardef) or
  3031. is_array_of_const(p.vardef) then
  3032. begin
  3033. result:=true;
  3034. exit;
  3035. end;
  3036. ploc:=p.paraloc[side].location;
  3037. while assigned(ploc) do
  3038. begin
  3039. if (ploc^.loc=LOC_REFERENCE) then
  3040. begin
  3041. result:=true;
  3042. exit
  3043. end;
  3044. ploc:=ploc^.next;
  3045. end;
  3046. end;
  3047. end;
  3048. {***************************************************************************
  3049. TPROCDEF
  3050. ***************************************************************************}
  3051. constructor tprocdef.create(level:byte);
  3052. begin
  3053. inherited create(procdef,level);
  3054. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  3055. _mangledname:=nil;
  3056. fileinfo:=current_filepos;
  3057. extnumber:=$ffff;
  3058. aliasnames:=TCmdStrList.create;
  3059. funcretsym:=nil;
  3060. forwarddef:=true;
  3061. interfacedef:=false;
  3062. hasforward:=false;
  3063. struct := nil;
  3064. import_dll:=nil;
  3065. import_name:=nil;
  3066. import_nr:=0;
  3067. inlininginfo:=nil;
  3068. deprecatedmsg:=nil;
  3069. {$ifdef i386}
  3070. fpu_used:=maxfpuregs;
  3071. {$endif i386}
  3072. interruptvector:=-1;
  3073. end;
  3074. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  3075. var
  3076. i,aliasnamescount : longint;
  3077. level : byte;
  3078. begin
  3079. inherited ppuload(procdef,ppufile);
  3080. if po_has_mangledname in procoptions then
  3081. _mangledname:=stringdup(ppufile.getstring)
  3082. else
  3083. _mangledname:=nil;
  3084. extnumber:=ppufile.getword;
  3085. level:=ppufile.getbyte;
  3086. ppufile.getderef(structderef);
  3087. ppufile.getderef(procsymderef);
  3088. ppufile.getposinfo(fileinfo);
  3089. visibility:=tvisibility(ppufile.getbyte);
  3090. ppufile.getsmallset(symoptions);
  3091. if sp_has_deprecated_msg in symoptions then
  3092. deprecatedmsg:=stringdup(ppufile.getstring)
  3093. else
  3094. deprecatedmsg:=nil;
  3095. {$ifdef powerpc}
  3096. { library symbol for AmigaOS/MorphOS }
  3097. ppufile.getderef(libsymderef);
  3098. {$endif powerpc}
  3099. { import stuff }
  3100. if po_has_importdll in procoptions then
  3101. import_dll:=stringdup(ppufile.getstring)
  3102. else
  3103. import_dll:=nil;
  3104. if po_has_importname in procoptions then
  3105. import_name:=stringdup(ppufile.getstring)
  3106. else
  3107. import_name:=nil;
  3108. import_nr:=ppufile.getword;
  3109. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3110. if target_info.system in systems_interrupt_table then
  3111. interruptvector:=ppufile.getlongint;
  3112. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3113. if (po_msgint in procoptions) then
  3114. messageinf.i:=ppufile.getlongint;
  3115. if (po_msgstr in procoptions) then
  3116. messageinf.str:=stringdup(ppufile.getstring);
  3117. if (po_dispid in procoptions) then
  3118. dispid:=ppufile.getlongint;
  3119. { inline stuff }
  3120. if (po_has_inlininginfo in procoptions) then
  3121. begin
  3122. ppufile.getderef(funcretsymderef);
  3123. new(inlininginfo);
  3124. ppufile.getsmallset(inlininginfo^.flags);
  3125. end
  3126. else
  3127. begin
  3128. inlininginfo:=nil;
  3129. funcretsym:=nil;
  3130. end;
  3131. aliasnames:=TCmdStrList.create;
  3132. { count alias names }
  3133. aliasnamescount:=ppufile.getbyte;
  3134. for i:=1 to aliasnamescount do
  3135. aliasnames.insert(ppufile.getstring);
  3136. { load para symtable }
  3137. parast:=tparasymtable.create(self,level);
  3138. tparasymtable(parast).ppuload(ppufile);
  3139. { load local symtable }
  3140. if (po_has_inlininginfo in procoptions) then
  3141. begin
  3142. localst:=tlocalsymtable.create(self,level);
  3143. tlocalsymtable(localst).ppuload(ppufile);
  3144. end
  3145. else
  3146. localst:=nil;
  3147. { inline stuff }
  3148. if (po_has_inlininginfo in procoptions) then
  3149. inlininginfo^.code:=ppuloadnodetree(ppufile);
  3150. { default values for no persistent data }
  3151. if (cs_link_deffile in current_settings.globalswitches) and
  3152. (tf_need_export in target_info.flags) and
  3153. (po_exports in procoptions) then
  3154. deffile.AddExport(mangledname(false));
  3155. forwarddef:=false;
  3156. interfacedef:=false;
  3157. hasforward:=false;
  3158. { Disable po_has_inlining until the derefimpl is done }
  3159. exclude(procoptions,po_has_inlininginfo);
  3160. {$ifdef i386}
  3161. fpu_used:=maxfpuregs;
  3162. {$endif i386}
  3163. end;
  3164. destructor tprocdef.destroy;
  3165. begin
  3166. aliasnames.free;
  3167. aliasnames:=nil;
  3168. if assigned(localst) and
  3169. (localst.symtabletype<>staticsymtable) then
  3170. begin
  3171. {$ifdef MEMDEBUG}
  3172. memproclocalst.start;
  3173. {$endif MEMDEBUG}
  3174. localst.free;
  3175. localst:=nil;
  3176. {$ifdef MEMDEBUG}
  3177. memproclocalst.start;
  3178. {$endif MEMDEBUG}
  3179. end;
  3180. if assigned(inlininginfo) then
  3181. begin
  3182. {$ifdef MEMDEBUG}
  3183. memprocnodetree.start;
  3184. {$endif MEMDEBUG}
  3185. tnode(inlininginfo^.code).free;
  3186. {$ifdef MEMDEBUG}
  3187. memprocnodetree.start;
  3188. {$endif MEMDEBUG}
  3189. dispose(inlininginfo);
  3190. inlininginfo:=nil;
  3191. end;
  3192. {$ifdef jvm}
  3193. exprasmlist.free;
  3194. {$endif}
  3195. stringdispose(resultname);
  3196. stringdispose(import_dll);
  3197. stringdispose(import_name);
  3198. stringdispose(deprecatedmsg);
  3199. if (po_msgstr in procoptions) then
  3200. stringdispose(messageinf.str);
  3201. if assigned(_mangledname) then
  3202. begin
  3203. {$ifdef MEMDEBUG}
  3204. memmanglednames.start;
  3205. {$endif MEMDEBUG}
  3206. stringdispose(_mangledname);
  3207. {$ifdef MEMDEBUG}
  3208. memmanglednames.stop;
  3209. {$endif MEMDEBUG}
  3210. end;
  3211. inherited destroy;
  3212. end;
  3213. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  3214. var
  3215. oldintfcrc : boolean;
  3216. aliasnamescount : longint;
  3217. item : TCmdStrListItem;
  3218. begin
  3219. { released procdef? }
  3220. if not assigned(parast) then
  3221. exit;
  3222. inherited ppuwrite(ppufile);
  3223. if po_has_mangledname in procoptions then
  3224. ppufile.putstring(_mangledname^);
  3225. ppufile.putword(extnumber);
  3226. ppufile.putbyte(parast.symtablelevel);
  3227. ppufile.putderef(structderef);
  3228. ppufile.putderef(procsymderef);
  3229. ppufile.putposinfo(fileinfo);
  3230. ppufile.putbyte(byte(visibility));
  3231. ppufile.putsmallset(symoptions);
  3232. if sp_has_deprecated_msg in symoptions then
  3233. ppufile.putstring(deprecatedmsg^);
  3234. {$ifdef powerpc}
  3235. { library symbol for AmigaOS/MorphOS }
  3236. ppufile.putderef(libsymderef);
  3237. {$endif powerpc}
  3238. { import }
  3239. if po_has_importdll in procoptions then
  3240. ppufile.putstring(import_dll^);
  3241. if po_has_importname in procoptions then
  3242. ppufile.putstring(import_name^);
  3243. ppufile.putword(import_nr);
  3244. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3245. if target_info.system in systems_interrupt_table then
  3246. ppufile.putlongint(interruptvector);
  3247. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  3248. if (po_msgint in procoptions) then
  3249. ppufile.putlongint(messageinf.i);
  3250. if (po_msgstr in procoptions) then
  3251. ppufile.putstring(messageinf.str^);
  3252. if (po_dispid in procoptions) then
  3253. ppufile.putlongint(dispid);
  3254. { inline stuff }
  3255. oldintfcrc:=ppufile.do_crc;
  3256. ppufile.do_crc:=false;
  3257. if (po_has_inlininginfo in procoptions) then
  3258. begin
  3259. ppufile.putderef(funcretsymderef);
  3260. ppufile.putsmallset(inlininginfo^.flags);
  3261. end;
  3262. { count alias names }
  3263. aliasnamescount:=0;
  3264. item:=TCmdStrListItem(aliasnames.first);
  3265. while assigned(item) do
  3266. begin
  3267. inc(aliasnamescount);
  3268. item:=TCmdStrListItem(item.next);
  3269. end;
  3270. if aliasnamescount>255 then
  3271. internalerror(200711021);
  3272. ppufile.putbyte(aliasnamescount);
  3273. item:=TCmdStrListItem(aliasnames.first);
  3274. while assigned(item) do
  3275. begin
  3276. ppufile.putstring(item.str);
  3277. item:=TCmdStrListItem(item.next);
  3278. end;
  3279. ppufile.do_crc:=oldintfcrc;
  3280. { write this entry }
  3281. ppufile.writeentry(ibprocdef);
  3282. { Save the para symtable, this is taken from the interface }
  3283. tparasymtable(parast).ppuwrite(ppufile);
  3284. { save localsymtable for inline procedures or when local
  3285. browser info is requested, this has no influence on the crc }
  3286. if (po_has_inlininginfo in procoptions) then
  3287. begin
  3288. oldintfcrc:=ppufile.do_crc;
  3289. ppufile.do_crc:=false;
  3290. tlocalsymtable(localst).ppuwrite(ppufile);
  3291. ppufile.do_crc:=oldintfcrc;
  3292. end;
  3293. { node tree for inlining }
  3294. oldintfcrc:=ppufile.do_crc;
  3295. ppufile.do_crc:=false;
  3296. if (po_has_inlininginfo in procoptions) then
  3297. ppuwritenodetree(ppufile,inlininginfo^.code);
  3298. ppufile.do_crc:=oldintfcrc;
  3299. end;
  3300. function tprocdef.fullprocname(showhidden:boolean):string;
  3301. var
  3302. s : string;
  3303. t : ttoken;
  3304. begin
  3305. {$ifdef EXTDEBUG}
  3306. showhidden:=true;
  3307. {$endif EXTDEBUG}
  3308. s:='';
  3309. if assigned(struct) then
  3310. begin
  3311. s:=struct.RttiName+'.';
  3312. if (po_classmethod in procoptions) and
  3313. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3314. s:='class ' + s;
  3315. end;
  3316. if proctypeoption=potype_operator then
  3317. begin
  3318. for t:=NOTOKEN to last_overloaded do
  3319. if procsym.realname='$'+overloaded_names[t] then
  3320. begin
  3321. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  3322. break;
  3323. end;
  3324. end
  3325. else
  3326. s:=s+procsym.realname+typename_paras(showhidden);
  3327. case proctypeoption of
  3328. potype_constructor:
  3329. s:='constructor '+s;
  3330. potype_destructor:
  3331. s:='destructor '+s;
  3332. potype_class_constructor:
  3333. s:='class constructor '+s;
  3334. potype_class_destructor:
  3335. s:='class destructor '+s;
  3336. else
  3337. if assigned(returndef) and
  3338. not(is_void(returndef)) then
  3339. s:=s+':'+returndef.GetTypeName;
  3340. end;
  3341. if owner.symtabletype=localsymtable then
  3342. s:=s+' is nested';
  3343. s:=s+';';
  3344. { forced calling convention? }
  3345. if (po_hascallingconvention in procoptions) then
  3346. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3347. if (po_staticmethod in procoptions) and
  3348. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3349. s:=s+' Static;';
  3350. fullprocname:=s;
  3351. end;
  3352. function tprocdef.is_methodpointer:boolean;
  3353. begin
  3354. { don't check assigned(_class), that's also the case for nested
  3355. procedures inside methods }
  3356. result:=owner.symtabletype=ObjectSymtable;
  3357. end;
  3358. function tprocdef.is_addressonly:boolean;
  3359. begin
  3360. result:=assigned(owner) and
  3361. (owner.symtabletype<>ObjectSymtable) and
  3362. (not(m_nested_procvars in current_settings.modeswitches) or
  3363. not is_nested_pd(self));
  3364. end;
  3365. procedure tprocdef.make_external;
  3366. begin
  3367. include(procoptions,po_external);
  3368. forwarddef:=false;
  3369. end;
  3370. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3371. begin
  3372. case t of
  3373. gs_local :
  3374. GetSymtable:=localst;
  3375. gs_para :
  3376. GetSymtable:=parast;
  3377. else
  3378. GetSymtable:=nil;
  3379. end;
  3380. end;
  3381. procedure tprocdef.buildderef;
  3382. begin
  3383. inherited buildderef;
  3384. structderef.build(struct);
  3385. { procsym that originaly defined this definition, should be in the
  3386. same symtable }
  3387. procsymderef.build(procsym);
  3388. {$ifdef powerpc}
  3389. { library symbol for AmigaOS/MorphOS }
  3390. libsymderef.build(libsym);
  3391. {$endif powerpc}
  3392. end;
  3393. procedure tprocdef.buildderefimpl;
  3394. begin
  3395. inherited buildderefimpl;
  3396. { Localst is not available for main/unit init }
  3397. if assigned(localst) then
  3398. begin
  3399. tlocalsymtable(localst).buildderef;
  3400. tlocalsymtable(localst).buildderefimpl;
  3401. end;
  3402. { inline tree }
  3403. if (po_has_inlininginfo in procoptions) then
  3404. begin
  3405. funcretsymderef.build(funcretsym);
  3406. inlininginfo^.code.buildderefimpl;
  3407. end;
  3408. end;
  3409. procedure tprocdef.deref;
  3410. begin
  3411. inherited deref;
  3412. struct:=tabstractrecorddef(structderef.resolve);
  3413. { procsym that originaly defined this definition, should be in the
  3414. same symtable }
  3415. procsym:=tprocsym(procsymderef.resolve);
  3416. {$ifdef powerpc}
  3417. { library symbol for AmigaOS/MorphOS }
  3418. libsym:=tsym(libsymderef.resolve);
  3419. {$endif powerpc}
  3420. end;
  3421. procedure tprocdef.derefimpl;
  3422. begin
  3423. { Enable has_inlininginfo when the inlininginfo
  3424. structure is available. The has_inlininginfo was disabled
  3425. after the load, since the data was invalid }
  3426. if assigned(inlininginfo) then
  3427. include(procoptions,po_has_inlininginfo);
  3428. { Locals }
  3429. if assigned(localst) then
  3430. begin
  3431. tlocalsymtable(localst).deref;
  3432. tlocalsymtable(localst).derefimpl;
  3433. end;
  3434. { Inline }
  3435. if (po_has_inlininginfo in procoptions) then
  3436. begin
  3437. inlininginfo^.code.derefimpl;
  3438. { funcretsym, this is always located in the localst }
  3439. funcretsym:=tsym(funcretsymderef.resolve);
  3440. end
  3441. else
  3442. begin
  3443. { safety }
  3444. { Not safe! A unit may be reresolved after its interface has been
  3445. parsed but before its implementation has been parsed, and in that
  3446. case the funcretsym is still required!
  3447. funcretsym:=nil; }
  3448. end;
  3449. end;
  3450. function tprocdef.GetTypeName : string;
  3451. begin
  3452. GetTypeName := FullProcName(false);
  3453. end;
  3454. function tprocdef.mangledname(fordefinition: boolean) : string;
  3455. begin
  3456. if assigned(_mangledname) then
  3457. begin
  3458. {$ifdef compress}
  3459. mangledname:=minilzw_decode(_mangledname^);
  3460. {$else}
  3461. mangledname:=_mangledname^;
  3462. {$endif}
  3463. adornmangledname(mangledname,fordefinition);
  3464. exit;
  3465. end;
  3466. {$ifndef jvm}
  3467. mangledname:=defaultmangledname;
  3468. {$else not jvm}
  3469. mangledname:=jvmmangledname;
  3470. {$endif not jvm}
  3471. {$ifdef compress}
  3472. _mangledname:=stringdup(minilzw_encode(mangledname));
  3473. {$else}
  3474. _mangledname:=stringdup(mangledname);
  3475. {$endif}
  3476. adornmangledname(mangledname,fordefinition);
  3477. end;
  3478. function tprocdef.defaultmangledname: string;
  3479. var
  3480. hp : TParavarsym;
  3481. hs : string;
  3482. crc : dword;
  3483. newlen,
  3484. oldlen,
  3485. i : integer;
  3486. begin
  3487. { we need to use the symtable where the procsym is inserted,
  3488. because that is visible to the world }
  3489. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  3490. oldlen:=length(defaultmangledname);
  3491. { add parameter types }
  3492. for i:=0 to paras.count-1 do
  3493. begin
  3494. hp:=tparavarsym(paras[i]);
  3495. if not(vo_is_hidden_para in hp.varoptions) then
  3496. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  3497. end;
  3498. { add resultdef, add $$ as separator to make it unique from a
  3499. parameter separator }
  3500. if not is_void(returndef) then
  3501. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  3502. newlen:=length(defaultmangledname);
  3503. { Replace with CRC if the parameter line is very long }
  3504. if (newlen-oldlen>12) and
  3505. ((newlen>100) or (newlen-oldlen>64)) then
  3506. begin
  3507. crc:=0;
  3508. for i:=0 to paras.count-1 do
  3509. begin
  3510. hp:=tparavarsym(paras[i]);
  3511. if not(vo_is_hidden_para in hp.varoptions) then
  3512. begin
  3513. hs:=hp.vardef.mangledparaname;
  3514. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3515. end;
  3516. end;
  3517. hs:=hp.vardef.mangledparaname;
  3518. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3519. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3520. end;
  3521. end;
  3522. function tprocdef.cplusplusmangledname : string;
  3523. function getcppparaname(p : tdef) : string;
  3524. const
  3525. {$ifdef NAMEMANGLING_GCC2}
  3526. ordtype2str : array[tordtype] of string[2] = (
  3527. '',
  3528. 'Uc','Us','Ui','Us',
  3529. 'Sc','s','i','x',
  3530. 'b','b','b','b','b',
  3531. 'c','w','x');
  3532. {$else NAMEMANGLING_GCC2}
  3533. ordtype2str : array[tordtype] of string[1] = (
  3534. 'v',
  3535. 'h','t','j','y',
  3536. 'a','s','i','x',
  3537. 'b','b','b','b',
  3538. 'b','b','b','b',
  3539. 'c','w','x');
  3540. floattype2str : array[tfloattype] of string[1] = (
  3541. 'f','d','e','e',
  3542. 'd','d','g');
  3543. {$endif NAMEMANGLING_GCC2}
  3544. var
  3545. s : string;
  3546. begin
  3547. case p.typ of
  3548. orddef:
  3549. s:=ordtype2str[torddef(p).ordtype];
  3550. pointerdef:
  3551. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3552. {$ifndef NAMEMANGLING_GCC2}
  3553. floatdef:
  3554. s:=floattype2str[tfloatdef(p).floattype];
  3555. {$endif NAMEMANGLING_GCC2}
  3556. else
  3557. internalerror(2103001);
  3558. end;
  3559. getcppparaname:=s;
  3560. end;
  3561. var
  3562. s,s2 : string;
  3563. hp : TParavarsym;
  3564. i : integer;
  3565. begin
  3566. {$ifdef NAMEMANGLING_GCC2}
  3567. { outdated gcc 2.x name mangling scheme }
  3568. s := procsym.realname;
  3569. if procsym.owner.symtabletype=ObjectSymtable then
  3570. begin
  3571. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3572. case proctypeoption of
  3573. potype_destructor:
  3574. s:='_$_'+tostr(length(s2))+s2;
  3575. potype_constructor:
  3576. s:='___'+tostr(length(s2))+s2;
  3577. else
  3578. s:='_'+s+'__'+tostr(length(s2))+s2;
  3579. end;
  3580. end
  3581. else s:=s+'__';
  3582. s:=s+'F';
  3583. { concat modifiers }
  3584. { !!!!! }
  3585. { now we handle the parameters }
  3586. if maxparacount>0 then
  3587. begin
  3588. for i:=0 to paras.count-1 do
  3589. begin
  3590. hp:=tparavarsym(paras[i]);
  3591. { no hidden parameters form part of a C++ mangled name:
  3592. a) self is not included
  3593. b) there are no "high" or other hidden parameters
  3594. }
  3595. if vo_is_hidden_para in hp.varoptions then
  3596. continue;
  3597. s2:=getcppparaname(hp.vardef);
  3598. if hp.varspez in [vs_var,vs_out] then
  3599. s2:='R'+s2;
  3600. s:=s+s2;
  3601. end;
  3602. end
  3603. else
  3604. s:=s+'v';
  3605. cplusplusmangledname:=s;
  3606. {$else NAMEMANGLING_GCC2}
  3607. { gcc 3.x and 4.x name mangling scheme }
  3608. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3609. if procsym.owner.symtabletype=ObjectSymtable then
  3610. begin
  3611. s:='_ZN';
  3612. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3613. s:=s+tostr(length(s2))+s2;
  3614. case proctypeoption of
  3615. potype_constructor:
  3616. s:=s+'C1';
  3617. potype_destructor:
  3618. s:=s+'D1';
  3619. else
  3620. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3621. end;
  3622. s:=s+'E';
  3623. end
  3624. else
  3625. s:=procsym.realname;
  3626. { now we handle the parameters }
  3627. if maxparacount>0 then
  3628. begin
  3629. for i:=0 to paras.count-1 do
  3630. begin
  3631. hp:=tparavarsym(paras[i]);
  3632. { no hidden parameters form part of a C++ mangled name:
  3633. a) self is not included
  3634. b) there are no "high" or other hidden parameters
  3635. }
  3636. if vo_is_hidden_para in hp.varoptions then
  3637. continue;
  3638. s2:=getcppparaname(hp.vardef);
  3639. if hp.varspez in [vs_var,vs_out] then
  3640. s2:='R'+s2;
  3641. s:=s+s2;
  3642. end;
  3643. end
  3644. else
  3645. s:=s+'v';
  3646. cplusplusmangledname:=s;
  3647. {$endif NAMEMANGLING_GCC2}
  3648. end;
  3649. function tprocdef.objcmangledname : string;
  3650. var
  3651. manglednamelen: longint;
  3652. iscatmethod : boolean;
  3653. begin
  3654. if not (po_msgstr in procoptions) then
  3655. internalerror(2009030901);
  3656. { we may very well need longer strings to handle these... }
  3657. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3658. length('+"[ ]"')+length(messageinf.str^);
  3659. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3660. if (iscatmethod) then
  3661. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3662. if manglednamelen>255 then
  3663. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3664. if not(po_classmethod in procoptions) then
  3665. result:='"-['
  3666. else
  3667. result:='"+[';
  3668. { quotes are necessary because the +/- otherwise confuse the assembler
  3669. into expecting a number
  3670. }
  3671. if iscatmethod then
  3672. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3673. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3674. if iscatmethod then
  3675. result:=result+')';
  3676. result:=result+' '+messageinf.str^+']"';
  3677. end;
  3678. procedure tprocdef.adornmangledname(var name: string; fordefinition: boolean);
  3679. {$ifdef jvm}
  3680. var
  3681. owningunit:tsymtable;
  3682. tmpresult:string;
  3683. {$endif jvm}
  3684. begin
  3685. {$ifdef jvm}
  3686. { see tprocdef.jvmmangledname for description of the format }
  3687. if fordefinition then
  3688. begin
  3689. { definition: visibility/static }
  3690. case visibility of
  3691. vis_hidden,
  3692. vis_strictprivate:
  3693. tmpresult:='private ';
  3694. vis_strictprotected:
  3695. tmpresult:='protected ';
  3696. vis_protected,
  3697. vis_private,
  3698. vis_public:
  3699. tmpresult:='public ';
  3700. else
  3701. internalerror(2010122609);
  3702. end;
  3703. if (procsym.owner.symtabletype in [globalsymtable,staticsymtable,localsymtable]) or
  3704. (po_staticmethod in procoptions) then
  3705. tmpresult:=tmpresult+'static ';
  3706. end
  3707. else
  3708. begin
  3709. { invocation: package/class name }
  3710. case procsym.owner.symtabletype of
  3711. globalsymtable,
  3712. staticsymtable,
  3713. localsymtable:
  3714. begin
  3715. owningunit:=procsym.owner;
  3716. while (owningunit.symtabletype in [localsymtable,objectsymtable,recordsymtable]) do
  3717. owningunit:=owner.defowner.owner;
  3718. { TODO: add package name !!! }
  3719. tmpresult:=owningunit.realname^+'/';
  3720. end;
  3721. objectsymtable:
  3722. case tobjectdef(procsym.owner.defowner).objecttype of
  3723. odt_javaclass,
  3724. odt_interfacejava:
  3725. tmpresult:=tobjectdef(procsym.owner.defowner).objextname^+'/';
  3726. else
  3727. internalerror(2010122606);
  3728. end
  3729. else
  3730. internalerror(2010122605);
  3731. end;
  3732. if po_has_importdll in procoptions then
  3733. begin
  3734. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  3735. if assigned(import_dll) then
  3736. tmpresult:=tmpresult+import_dll^+'/'
  3737. else
  3738. internalerror(2010122607);
  3739. end;
  3740. end;
  3741. name:=tmpresult+name;
  3742. {$endif}
  3743. end;
  3744. function tprocdef.jvmmangledname: string;
  3745. var
  3746. owningunit: tsymtable;
  3747. parasize,
  3748. vs: tparavarsym;
  3749. i: longint;
  3750. founderror: tdef;
  3751. tmpresult: ansistring;
  3752. begin
  3753. { format:
  3754. * method definition:
  3755. (private|protected|public) [static] method(parametertypes)returntype
  3756. * method invocation
  3757. package/class/method(parametertypes)returntype
  3758. -> store common part: method(parametertypes)returntype and
  3759. adorn as required when using it.
  3760. }
  3761. { method name }
  3762. { special names for constructors and class constructors }
  3763. if proctypeoption=potype_constructor then
  3764. tmpresult:='<init>'
  3765. else if proctypeoption=potype_class_constructor then
  3766. tmpresult:='<clinit>'
  3767. else if po_has_importname in procoptions then
  3768. begin
  3769. if assigned(import_name) then
  3770. tmpresult:=import_name^
  3771. else
  3772. internalerror(2010122608);
  3773. end
  3774. else
  3775. tmpresult:=procsym.realname;
  3776. { parameter types }
  3777. tmpresult:=tmpresult+'(';
  3778. init_paraloc_info(callerside);
  3779. for i:=0 to paras.count-1 do
  3780. begin
  3781. vs:=tparavarsym(paras[i]);
  3782. { function result and self pointer are not part of the mangled
  3783. name }
  3784. if ([vo_is_funcret,vo_is_self] * vs.varoptions <> []) then
  3785. continue;
  3786. { reference parameters are not yet supported }
  3787. if (vs.varspez in [vs_var,vs_out,vs_constref]) then
  3788. internalerror(2010122603);
  3789. { Add the parameter type. }
  3790. if not jvmaddencodedtype(vs.vardef,false,tmpresult,founderror) then
  3791. { should be checked earlier on }
  3792. internalerror(2010122604);
  3793. end;
  3794. tmpresult:=tmpresult+')';
  3795. { And the type of the function result (void in case of a procedure and
  3796. constructor). }
  3797. if (proctypeoption=potype_constructor) then
  3798. jvmaddencodedtype(voidtype,false,tmpresult,founderror)
  3799. else if not jvmaddencodedtype(returndef,false,tmpresult,founderror) then
  3800. internalerror(2010122610);
  3801. result:=tmpresult;
  3802. end;
  3803. procedure tprocdef.setmangledname(const s : string);
  3804. begin
  3805. { This is not allowed anymore, the forward declaration
  3806. already needs to create the correct mangledname, no changes
  3807. afterwards are allowed (PFV) }
  3808. { Exception: interface definitions in mode macpas, since in that }
  3809. { case no reference to the old name can exist yet (JM) }
  3810. if assigned(_mangledname) then
  3811. if ((m_mac in current_settings.modeswitches) and
  3812. (interfacedef)) then
  3813. stringdispose(_mangledname)
  3814. else
  3815. internalerror(200411171);
  3816. {$ifdef compress}
  3817. _mangledname:=stringdup(minilzw_encode(s));
  3818. {$else}
  3819. _mangledname:=stringdup(s);
  3820. {$endif}
  3821. include(procoptions,po_has_mangledname);
  3822. end;
  3823. {***************************************************************************
  3824. TPROCVARDEF
  3825. ***************************************************************************}
  3826. constructor tprocvardef.create(level:byte);
  3827. begin
  3828. inherited create(procvardef,level);
  3829. end;
  3830. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3831. begin
  3832. inherited ppuload(procvardef,ppufile);
  3833. { load para symtable }
  3834. parast:=tparasymtable.create(self,ppufile.getbyte);
  3835. tparasymtable(parast).ppuload(ppufile);
  3836. end;
  3837. function tprocvardef.getcopy : tstoreddef;
  3838. var
  3839. i : tcallercallee;
  3840. j : longint;
  3841. begin
  3842. result:=tprocvardef.create(parast.symtablelevel);
  3843. tprocvardef(result).returndef:=returndef;
  3844. tprocvardef(result).returndefderef:=returndefderef;
  3845. tprocvardef(result).parast:=parast.getcopy;
  3846. tprocvardef(result).savesize:=savesize;
  3847. { create paralist copy }
  3848. tprocvardef(result).paras:=tparalist.create(false);
  3849. tprocvardef(result).paras.count:=paras.count;
  3850. for j:=0 to paras.count-1 do
  3851. tprocvardef(result).paras[j]:=paras[j];
  3852. tprocvardef(result).proctypeoption:=proctypeoption;
  3853. tprocvardef(result).proccalloption:=proccalloption;
  3854. tprocvardef(result).procoptions:=procoptions;
  3855. tprocvardef(result).callerargareasize:=callerargareasize;
  3856. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3857. tprocvardef(result).maxparacount:=maxparacount;
  3858. tprocvardef(result).minparacount:=minparacount;
  3859. for i:=low(tcallercallee) to high(tcallercallee) do
  3860. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3861. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3862. {$ifdef m68k}
  3863. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3864. {$endif}
  3865. end;
  3866. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3867. begin
  3868. inherited ppuwrite(ppufile);
  3869. { Save the para symtable level (necessary to distinguish nested
  3870. procvars) }
  3871. ppufile.putbyte(parast.symtablelevel);
  3872. { Write this entry }
  3873. ppufile.writeentry(ibprocvardef);
  3874. { Save the para symtable, this is taken from the interface }
  3875. tparasymtable(parast).ppuwrite(ppufile);
  3876. end;
  3877. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3878. begin
  3879. case t of
  3880. gs_para :
  3881. GetSymtable:=parast;
  3882. else
  3883. GetSymtable:=nil;
  3884. end;
  3885. end;
  3886. function tprocvardef.size : asizeint;
  3887. begin
  3888. if ((po_methodpointer in procoptions) or
  3889. is_nested_pd(self)) and
  3890. not(po_addressonly in procoptions) then
  3891. size:=2*sizeof(pint)
  3892. else
  3893. size:=sizeof(pint);
  3894. end;
  3895. function tprocvardef.is_methodpointer:boolean;
  3896. begin
  3897. result:=(po_methodpointer in procoptions);
  3898. end;
  3899. function tprocvardef.is_addressonly:boolean;
  3900. begin
  3901. result:=(not(po_methodpointer in procoptions) and
  3902. not is_nested_pd(self)) or
  3903. (po_addressonly in procoptions);
  3904. end;
  3905. function tprocvardef.getmangledparaname:string;
  3906. begin
  3907. if not(po_methodpointer in procoptions) then
  3908. if not is_nested_pd(self) then
  3909. result:='procvar'
  3910. else
  3911. result:='nestedprovar'
  3912. else
  3913. result:='procvarofobj'
  3914. end;
  3915. function tprocvardef.is_publishable : boolean;
  3916. begin
  3917. is_publishable:=(po_methodpointer in procoptions);
  3918. end;
  3919. function tprocvardef.GetTypeName : string;
  3920. var
  3921. s: string;
  3922. showhidden : boolean;
  3923. begin
  3924. {$ifdef EXTDEBUG}
  3925. showhidden:=true;
  3926. {$else EXTDEBUG}
  3927. showhidden:=false;
  3928. {$endif EXTDEBUG}
  3929. s:='<';
  3930. if po_classmethod in procoptions then
  3931. s := s+'class method type of'
  3932. else
  3933. if po_addressonly in procoptions then
  3934. s := s+'address of'
  3935. else
  3936. s := s+'procedure variable type of';
  3937. if assigned(returndef) and
  3938. (returndef<>voidtype) then
  3939. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3940. else
  3941. s:=s+' procedure'+typename_paras(showhidden);
  3942. if po_methodpointer in procoptions then
  3943. s := s+' of object';
  3944. if is_nested_pd(self) then
  3945. s := s+' is nested';
  3946. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3947. end;
  3948. {***************************************************************************
  3949. TOBJECTDEF
  3950. ***************************************************************************}
  3951. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3952. begin
  3953. inherited create(n,objectdef);
  3954. fcurrent_dispid:=0;
  3955. objecttype:=ot;
  3956. childof:=nil;
  3957. if objecttype=odt_helper then
  3958. owner.includeoption(sto_has_helper);
  3959. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3960. { create space for vmt !! }
  3961. vmtentries:=TFPList.Create;
  3962. vmt_offset:=0;
  3963. set_parent(c);
  3964. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3965. prepareguid;
  3966. { setup implemented interfaces }
  3967. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_interfacejava] then
  3968. ImplementedInterfaces:=TFPObjectList.Create(true)
  3969. else
  3970. ImplementedInterfaces:=nil;
  3971. writing_class_record_dbginfo:=false;
  3972. end;
  3973. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3974. var
  3975. i,
  3976. implintfcount : longint;
  3977. d : tderef;
  3978. ImplIntf : TImplementedInterface;
  3979. vmtentry : pvmtentry;
  3980. begin
  3981. inherited ppuload(objectdef,ppufile);
  3982. objecttype:=tobjecttyp(ppufile.getbyte);
  3983. objextname:=stringdup(ppufile.getstring);
  3984. { only used for external Objective-C classes/protocols }
  3985. if (objextname^='') then
  3986. stringdispose(objextname);
  3987. import_lib:=stringdup(ppufile.getstring);
  3988. { only used for external C++ classes }
  3989. if (import_lib^='') then
  3990. stringdispose(import_lib);
  3991. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3992. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  3993. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  3994. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3995. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3996. vmt_offset:=ppufile.getlongint;
  3997. ppufile.getderef(childofderef);
  3998. { load guid }
  3999. iidstr:=nil;
  4000. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4001. begin
  4002. new(iidguid);
  4003. ppufile.getguid(iidguid^);
  4004. iidstr:=stringdup(ppufile.getstring);
  4005. end;
  4006. if objecttype=odt_helper then
  4007. ppufile.getderef(extendeddefderef);
  4008. vmtentries:=TFPList.Create;
  4009. vmtentries.count:=ppufile.getlongint;
  4010. for i:=0 to vmtentries.count-1 do
  4011. begin
  4012. ppufile.getderef(d);
  4013. new(vmtentry);
  4014. vmtentry^.procdef:=nil;
  4015. vmtentry^.procdefderef:=d;
  4016. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  4017. vmtentries[i]:=vmtentry;
  4018. end;
  4019. { load implemented interfaces }
  4020. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_interfacejava] then
  4021. begin
  4022. ImplementedInterfaces:=TFPObjectList.Create(true);
  4023. implintfcount:=ppufile.getlongint;
  4024. for i:=0 to implintfcount-1 do
  4025. begin
  4026. ppufile.getderef(d);
  4027. ImplIntf:=TImplementedInterface.Create_deref(d);
  4028. ImplIntf.IOffset:=ppufile.getlongint;
  4029. ImplementedInterfaces.Add(ImplIntf);
  4030. end;
  4031. end
  4032. else
  4033. ImplementedInterfaces:=nil;
  4034. if df_copied_def in defoptions then
  4035. ppufile.getderef(cloneddefderef)
  4036. else
  4037. tObjectSymtable(symtable).ppuload(ppufile);
  4038. { handles the predefined class tobject }
  4039. { the last TOBJECT which is loaded gets }
  4040. { it ! }
  4041. if (childof=nil) and
  4042. (objecttype=odt_class) and
  4043. (objname^='TOBJECT') then
  4044. class_tobject:=self;
  4045. if (childof=nil) and
  4046. (objecttype=odt_interfacecom) then
  4047. if (objname^='IUNKNOWN') then
  4048. interface_iunknown:=self
  4049. else
  4050. if (objname^='IDISPATCH') then
  4051. interface_idispatch:=self;
  4052. if (childof=nil) and
  4053. (objecttype=odt_objcclass) and
  4054. (objname^='PROTOCOL') then
  4055. objc_protocoltype:=self;
  4056. if (childof=nil) and
  4057. (objecttype=odt_javaclass) and
  4058. (objname^='TOBJECT') then
  4059. java_jlobject:=self;
  4060. writing_class_record_dbginfo:=false;
  4061. end;
  4062. destructor tobjectdef.destroy;
  4063. begin
  4064. if assigned(symtable) then
  4065. begin
  4066. symtable.free;
  4067. symtable:=nil;
  4068. end;
  4069. stringdispose(objextname);
  4070. stringdispose(import_lib);
  4071. stringdispose(iidstr);
  4072. if assigned(ImplementedInterfaces) then
  4073. begin
  4074. ImplementedInterfaces.free;
  4075. ImplementedInterfaces:=nil;
  4076. end;
  4077. if assigned(iidguid) then
  4078. begin
  4079. dispose(iidguid);
  4080. iidguid:=nil;
  4081. end;
  4082. if assigned(vmtentries) then
  4083. begin
  4084. resetvmtentries;
  4085. vmtentries.free;
  4086. vmtentries:=nil;
  4087. end;
  4088. if assigned(vmcallstaticinfo) then
  4089. begin
  4090. freemem(vmcallstaticinfo);
  4091. vmcallstaticinfo:=nil;
  4092. end;
  4093. inherited destroy;
  4094. end;
  4095. function tobjectdef.getcopy : tstoreddef;
  4096. var
  4097. i : longint;
  4098. begin
  4099. result:=tobjectdef.create(objecttype,objrealname^,childof);
  4100. { the constructor allocates a symtable which we release to avoid memory leaks }
  4101. tobjectdef(result).symtable.free;
  4102. tobjectdef(result).symtable:=symtable.getcopy;
  4103. if assigned(objextname) then
  4104. tobjectdef(result).objextname:=stringdup(objextname^);
  4105. if assigned(import_lib) then
  4106. tobjectdef(result).import_lib:=stringdup(import_lib^);
  4107. tobjectdef(result).objectoptions:=objectoptions;
  4108. include(tobjectdef(result).defoptions,df_copied_def);
  4109. tobjectdef(result).extendeddef:=extendeddef;
  4110. tobjectdef(result).vmt_offset:=vmt_offset;
  4111. if assigned(iidguid) then
  4112. begin
  4113. new(tobjectdef(result).iidguid);
  4114. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  4115. end;
  4116. if assigned(iidstr) then
  4117. tobjectdef(result).iidstr:=stringdup(iidstr^);
  4118. if assigned(ImplementedInterfaces) then
  4119. begin
  4120. for i:=0 to ImplementedInterfaces.count-1 do
  4121. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  4122. end;
  4123. if assigned(vmtentries) then
  4124. begin
  4125. tobjectdef(result).vmtentries:=TFPList.Create;
  4126. tobjectdef(result).copyvmtentries(self);
  4127. end;
  4128. end;
  4129. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  4130. var
  4131. i : longint;
  4132. vmtentry : pvmtentry;
  4133. ImplIntf : TImplementedInterface;
  4134. old_do_indirect_crc: boolean;
  4135. begin
  4136. { if class1 in unit A changes, and class2 in unit B inherits from it
  4137. (so unit B uses unit A), then unit B with class2 will be recompiled.
  4138. However, if there is also a class3 in unit C that only depends on
  4139. unit B, then unit C will not be recompiled because nothing changed
  4140. to the interface of unit B. Nevertheless, unit C can indirectly
  4141. depend on unit A via derefs, and these must be updated -> the
  4142. indirect crc keeps track of such changes. }
  4143. old_do_indirect_crc:=ppufile.do_indirect_crc;
  4144. ppufile.do_indirect_crc:=true;
  4145. inherited ppuwrite(ppufile);
  4146. ppufile.putbyte(byte(objecttype));
  4147. if assigned(objextname) then
  4148. ppufile.putstring(objextname^)
  4149. else
  4150. ppufile.putstring('');
  4151. if assigned(import_lib) then
  4152. ppufile.putstring(import_lib^)
  4153. else
  4154. ppufile.putstring('');
  4155. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  4156. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  4157. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  4158. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  4159. ppufile.putlongint(vmt_offset);
  4160. ppufile.putderef(childofderef);
  4161. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4162. begin
  4163. ppufile.putguid(iidguid^);
  4164. ppufile.putstring(iidstr^);
  4165. end;
  4166. if objecttype=odt_helper then
  4167. ppufile.putderef(extendeddefderef);
  4168. ppufile.putlongint(vmtentries.count);
  4169. for i:=0 to vmtentries.count-1 do
  4170. begin
  4171. vmtentry:=pvmtentry(vmtentries[i]);
  4172. ppufile.putderef(vmtentry^.procdefderef);
  4173. ppufile.putbyte(byte(vmtentry^.visibility));
  4174. end;
  4175. if assigned(ImplementedInterfaces) then
  4176. begin
  4177. ppufile.putlongint(ImplementedInterfaces.Count);
  4178. for i:=0 to ImplementedInterfaces.Count-1 do
  4179. begin
  4180. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4181. ppufile.putderef(ImplIntf.intfdefderef);
  4182. ppufile.putlongint(ImplIntf.Ioffset);
  4183. end;
  4184. end;
  4185. if df_copied_def in defoptions then
  4186. ppufile.putderef(cloneddefderef);
  4187. ppufile.writeentry(ibobjectdef);
  4188. if not(df_copied_def in defoptions) then
  4189. tObjectSymtable(symtable).ppuwrite(ppufile);
  4190. ppufile.do_indirect_crc:=old_do_indirect_crc;
  4191. end;
  4192. function tobjectdef.GetTypeName:string;
  4193. begin
  4194. { in this case we will go in endless recursion, because then }
  4195. { there is no tsym associated yet with the def. It can occur }
  4196. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  4197. { instead of the actual type name }
  4198. if not assigned(typesym) then
  4199. result:='<Currently Parsed Class>'
  4200. else
  4201. result:=typesymbolprettyname;
  4202. end;
  4203. procedure tobjectdef.buildderef;
  4204. var
  4205. i : longint;
  4206. vmtentry : pvmtentry;
  4207. begin
  4208. inherited buildderef;
  4209. childofderef.build(childof);
  4210. if df_copied_def in defoptions then
  4211. cloneddefderef.build(symtable.defowner)
  4212. else
  4213. tstoredsymtable(symtable).buildderef;
  4214. if objecttype=odt_helper then
  4215. extendeddefderef.build(extendeddef);
  4216. for i:=0 to vmtentries.count-1 do
  4217. begin
  4218. vmtentry:=pvmtentry(vmtentries[i]);
  4219. vmtentry^.procdefderef.build(vmtentry^.procdef);
  4220. end;
  4221. if assigned(ImplementedInterfaces) then
  4222. begin
  4223. for i:=0 to ImplementedInterfaces.count-1 do
  4224. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  4225. end;
  4226. end;
  4227. procedure tobjectdef.deref;
  4228. var
  4229. i : longint;
  4230. vmtentry : pvmtentry;
  4231. begin
  4232. inherited deref;
  4233. childof:=tobjectdef(childofderef.resolve);
  4234. if df_copied_def in defoptions then
  4235. begin
  4236. cloneddef:=tobjectdef(cloneddefderef.resolve);
  4237. symtable:=cloneddef.symtable.getcopy;
  4238. end
  4239. else
  4240. tstoredsymtable(symtable).deref;
  4241. if objecttype=odt_helper then
  4242. extendeddef:=tobjectdef(extendeddefderef.resolve);
  4243. for i:=0 to vmtentries.count-1 do
  4244. begin
  4245. vmtentry:=pvmtentry(vmtentries[i]);
  4246. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  4247. end;
  4248. if assigned(ImplementedInterfaces) then
  4249. begin
  4250. for i:=0 to ImplementedInterfaces.count-1 do
  4251. TImplementedInterface(ImplementedInterfaces[i]).deref;
  4252. end;
  4253. end;
  4254. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  4255. var
  4256. pd: tprocdef absolute def;
  4257. st: tsymtable;
  4258. psym: tsym;
  4259. nname: TIDString;
  4260. begin
  4261. if (tdef(def).typ<>procdef) then
  4262. exit;
  4263. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  4264. owner = symtable in which objcclassdef is defined
  4265. }
  4266. st:=pd.owner.defowner.owner;
  4267. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  4268. { check for an existing procsym with our special name }
  4269. psym:=tsym(st.find(nname));
  4270. if not assigned(psym) then
  4271. begin
  4272. psym:=tprocsym.create(nname);
  4273. { avoid warning about this symbol being unused }
  4274. psym.IncRefCount;
  4275. { don't check for duplicates:
  4276. a) we checked above
  4277. b) in case we are in the implementation section of a unit, this
  4278. will also check for this symbol in the interface section
  4279. (since you normally cannot have symbols with the same name
  4280. both interface and implementation), and it's possible to
  4281. have class helpers for the same class in the interface and
  4282. in the implementation, and they cannot be merged since only
  4283. the once in the interface must be saved to the ppu/visible
  4284. from other units }
  4285. st.insert(psym,false);
  4286. end
  4287. else if (psym.typ<>procsym) then
  4288. internalerror(2009111501);
  4289. { add ourselves to this special procsym }
  4290. tprocsym(psym).procdeflist.add(def);
  4291. end;
  4292. procedure tobjectdef.buildderefimpl;
  4293. begin
  4294. inherited buildderefimpl;
  4295. if not (df_copied_def in defoptions) then
  4296. tstoredsymtable(symtable).buildderefimpl;
  4297. end;
  4298. procedure tobjectdef.derefimpl;
  4299. begin
  4300. inherited derefimpl;
  4301. if not (df_copied_def in defoptions) then
  4302. tstoredsymtable(symtable).derefimpl;
  4303. { the procdefs are not owned by the class helper procsyms, so they
  4304. are not stored/restored either -> re-add them here }
  4305. if (objecttype=odt_objcclass) or
  4306. (oo_is_classhelper in objectoptions) then
  4307. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  4308. end;
  4309. procedure tobjectdef.resetvmtentries;
  4310. var
  4311. i : longint;
  4312. begin
  4313. for i:=0 to vmtentries.Count-1 do
  4314. Dispose(pvmtentry(vmtentries[i]));
  4315. vmtentries.clear;
  4316. end;
  4317. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  4318. var
  4319. i : longint;
  4320. vmtentry : pvmtentry;
  4321. begin
  4322. resetvmtentries;
  4323. vmtentries.count:=objdef.vmtentries.count;
  4324. for i:=0 to objdef.vmtentries.count-1 do
  4325. begin
  4326. new(vmtentry);
  4327. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  4328. vmtentries[i]:=vmtentry;
  4329. end;
  4330. end;
  4331. function tobjectdef.getparentdef:tdef;
  4332. begin
  4333. { TODO: Remove getparentdef hack}
  4334. { With 2 forward declared classes with the child class before the
  4335. parent class the child class is written earlier to the ppu. Leaving it
  4336. possible to have a reference to the parent class for property overriding,
  4337. but the parent class still has the childof not resolved yet (PFV) }
  4338. if childof=nil then
  4339. childof:=tobjectdef(childofderef.resolve);
  4340. result:=childof;
  4341. end;
  4342. procedure tobjectdef.prepareguid;
  4343. begin
  4344. { set up guid }
  4345. if not assigned(iidguid) then
  4346. begin
  4347. new(iidguid);
  4348. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  4349. end;
  4350. { setup iidstring }
  4351. if not assigned(iidstr) then
  4352. iidstr:=stringdup(''); { default is empty string }
  4353. end;
  4354. procedure tobjectdef.set_parent( c : tobjectdef);
  4355. begin
  4356. if assigned(childof) then
  4357. exit;
  4358. childof:=c;
  4359. if not assigned(c) then
  4360. exit;
  4361. { inherit options and status }
  4362. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  4363. { add the data of the anchestor class/object }
  4364. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  4365. begin
  4366. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  4367. { inherit recordalignment }
  4368. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  4369. { if both the parent and this record use C-alignment, also inherit
  4370. the current field alignment }
  4371. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  4372. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  4373. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  4374. { the padding is not inherited for Objective-C classes (maybe not
  4375. for cppclass either?) }
  4376. if objecttype=odt_objcclass then
  4377. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  4378. if (oo_has_vmt in objectoptions) and
  4379. (oo_has_vmt in c.objectoptions) then
  4380. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  4381. { if parent has a vmt field then the offset is the same for the child PM }
  4382. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  4383. begin
  4384. vmt_offset:=c.vmt_offset;
  4385. include(objectoptions,oo_has_vmt);
  4386. end;
  4387. end;
  4388. end;
  4389. procedure tobjectdef.insertvmt;
  4390. var
  4391. vs: tfieldvarsym;
  4392. begin
  4393. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  4394. exit;
  4395. if (oo_has_vmt in objectoptions) then
  4396. internalerror(12345)
  4397. else
  4398. begin
  4399. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  4400. tObjectSymtable(symtable).fieldalignment);
  4401. if (tf_requires_proper_alignment in target_info.flags) then
  4402. begin
  4403. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  4404. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  4405. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  4406. end;
  4407. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  4408. hidesym(vs);
  4409. tObjectSymtable(symtable).insert(vs);
  4410. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  4411. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  4412. vmt_offset:=vs.fieldoffset
  4413. else
  4414. vmt_offset:=vs.fieldoffset div 8;
  4415. include(objectoptions,oo_has_vmt);
  4416. end;
  4417. end;
  4418. procedure tobjectdef.check_forwards;
  4419. begin
  4420. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  4421. inherited;
  4422. if (oo_is_forward in objectoptions) then
  4423. begin
  4424. { ok, in future, the forward can be resolved }
  4425. Message1(sym_e_class_forward_not_resolved,objrealname^);
  4426. exclude(objectoptions,oo_is_forward);
  4427. end;
  4428. end;
  4429. { true if prot implements d (or if they are equal) }
  4430. function is_related_interface_multiple(prot: tobjectdef; d : tdef) : boolean;
  4431. var
  4432. i : longint;
  4433. begin
  4434. { objcprotocols have multiple inheritance, all protocols from which
  4435. the current protocol inherits are stored in implementedinterfaces }
  4436. result:=prot=d;
  4437. if result then
  4438. exit;
  4439. for i:=0 to prot.ImplementedInterfaces.count-1 do
  4440. begin
  4441. result:=is_related_interface_multiple(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  4442. if result then
  4443. exit;
  4444. end;
  4445. end;
  4446. { true, if self inherits from d (or if they are equal) }
  4447. function tobjectdef.is_related(d : tdef) : boolean;
  4448. var
  4449. hp : tobjectdef;
  4450. begin
  4451. if self=d then
  4452. begin
  4453. is_related:=true;
  4454. exit;
  4455. end;
  4456. if (d.typ<>objectdef) then
  4457. begin
  4458. is_related:=false;
  4459. exit;
  4460. end;
  4461. { Objective-C protocols and Java interfaces can use multiple
  4462. inheritance }
  4463. if (objecttype in [odt_objcprotocol,odt_interfacejava]) then
  4464. begin
  4465. is_related:=is_related_interface_multiple(self,d);
  4466. exit
  4467. end;
  4468. { formally declared Objective-C and Java classes match Objective-C/Java
  4469. classes with the same name. In case of Java, the package must also
  4470. match}
  4471. if (objecttype in [odt_objcclass,odt_javaclass]) and
  4472. (tobjectdef(d).objecttype=objecttype) and
  4473. ((oo_is_formal in objectoptions) or
  4474. (oo_is_formal in tobjectdef(d).objectoptions)) and
  4475. (objrealname^=tobjectdef(d).objrealname^) then
  4476. begin
  4477. { check package name for Java }
  4478. if objecttype=odt_objcclass then
  4479. is_related:=true
  4480. else
  4481. begin
  4482. is_related:=
  4483. assigned(import_lib)=assigned(tobjectdef(d).import_lib);
  4484. if is_related and
  4485. assigned(import_lib) then
  4486. is_related:=import_lib^=tobjectdef(d).import_lib^;
  4487. end;
  4488. exit;
  4489. end;
  4490. hp:=childof;
  4491. while assigned(hp) do
  4492. begin
  4493. if hp=d then
  4494. begin
  4495. is_related:=true;
  4496. exit;
  4497. end;
  4498. hp:=hp.childof;
  4499. end;
  4500. is_related:=false;
  4501. end;
  4502. function tobjectdef.find_destructor: tprocdef;
  4503. var
  4504. objdef: tobjectdef;
  4505. begin
  4506. objdef:=self;
  4507. while assigned(objdef) do
  4508. begin
  4509. result:=objdef.find_procdef_bytype(potype_destructor);
  4510. if assigned(result) then
  4511. exit;
  4512. objdef:=objdef.childof;
  4513. end;
  4514. result:=nil;
  4515. end;
  4516. function tobjectdef.implements_any_interfaces: boolean;
  4517. begin
  4518. result := (ImplementedInterfaces.Count > 0) or
  4519. (assigned(childof) and childof.implements_any_interfaces);
  4520. end;
  4521. function tobjectdef.size : asizeint;
  4522. begin
  4523. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  4524. result:=sizeof(pint)
  4525. else
  4526. result:=tObjectSymtable(symtable).datasize;
  4527. end;
  4528. function tobjectdef.alignment:shortint;
  4529. begin
  4530. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  4531. alignment:=sizeof(pint)
  4532. else
  4533. alignment:=tObjectSymtable(symtable).recordalignment;
  4534. end;
  4535. function tobjectdef.vmtmethodoffset(index:longint):longint;
  4536. begin
  4537. { for offset of methods for classes, see rtl/inc/objpash.inc }
  4538. case objecttype of
  4539. odt_class:
  4540. { the +2*sizeof(pint) is size and -size }
  4541. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  4542. odt_helper,
  4543. odt_objcclass,
  4544. odt_objcprotocol:
  4545. vmtmethodoffset:=0;
  4546. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  4547. vmtmethodoffset:=index*sizeof(pint);
  4548. odt_javaclass,
  4549. odt_interfacejava:
  4550. { invalid }
  4551. vmtmethodoffset:=-1;
  4552. else
  4553. {$ifdef WITHDMT}
  4554. vmtmethodoffset:=(index+4)*sizeof(pint);
  4555. {$else WITHDMT}
  4556. vmtmethodoffset:=(index+3)*sizeof(pint);
  4557. {$endif WITHDMT}
  4558. end;
  4559. end;
  4560. function tobjectdef.vmt_mangledname : string;
  4561. begin
  4562. if not(oo_has_vmt in objectoptions) then
  4563. Message1(parser_n_object_has_no_vmt,objrealname^);
  4564. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  4565. end;
  4566. function tobjectdef.needs_inittable : boolean;
  4567. begin
  4568. case objecttype of
  4569. odt_helper,
  4570. odt_class :
  4571. needs_inittable:=false;
  4572. odt_dispinterface,
  4573. odt_interfacecom:
  4574. needs_inittable:=true;
  4575. odt_interfacecorba:
  4576. needs_inittable:=is_related(interface_iunknown);
  4577. odt_object:
  4578. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  4579. odt_cppclass,
  4580. odt_objcclass,
  4581. odt_objcprotocol,
  4582. odt_javaclass,
  4583. odt_interfacejava:
  4584. needs_inittable:=false;
  4585. else
  4586. internalerror(200108267);
  4587. end;
  4588. end;
  4589. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  4590. begin
  4591. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  4592. result:=inherited rtti_mangledname(rt)
  4593. else
  4594. begin
  4595. { necessary in case of a dynamic array of nsobject, or
  4596. if an nsobject field appears in a record that needs
  4597. init/finalisation }
  4598. if rt=initrtti then
  4599. begin
  4600. result:=voidpointertype.rtti_mangledname(rt);
  4601. exit;
  4602. end;
  4603. if not(target_info.system in systems_objc_nfabi) then
  4604. begin
  4605. result:=target_asm.labelprefix;
  4606. case objecttype of
  4607. odt_objcclass:
  4608. begin
  4609. case rt of
  4610. objcclassrtti:
  4611. if not(oo_is_classhelper in objectoptions) then
  4612. result:=result+'_OBJC_CLASS_'
  4613. else
  4614. result:=result+'_OBJC_CATEGORY_';
  4615. objcmetartti:
  4616. if not(oo_is_classhelper in objectoptions) then
  4617. result:=result+'_OBJC_METACLASS_'
  4618. else
  4619. internalerror(2009111511);
  4620. else
  4621. internalerror(2009092302);
  4622. end;
  4623. end;
  4624. odt_objcprotocol:
  4625. result:=result+'_OBJC_PROTOCOL_';
  4626. end;
  4627. end
  4628. else
  4629. begin
  4630. case objecttype of
  4631. odt_objcclass:
  4632. begin
  4633. if (oo_is_classhelper in objectoptions) and
  4634. (rt<>objcclassrtti) then
  4635. internalerror(2009111512);
  4636. case rt of
  4637. objcclassrtti:
  4638. if not(oo_is_classhelper in objectoptions) then
  4639. result:='_OBJC_CLASS_$_'
  4640. else
  4641. result:='_OBJC_$_CATEGORY_';
  4642. objcmetartti:
  4643. result:='_OBJC_METACLASS_$_';
  4644. objcclassrortti:
  4645. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4646. objcmetarortti:
  4647. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4648. else
  4649. internalerror(2009092303);
  4650. end;
  4651. end;
  4652. odt_objcprotocol:
  4653. begin
  4654. result:=lower(target_asm.labelprefix);
  4655. case rt of
  4656. objcclassrtti:
  4657. result:=result+'_OBJC_PROTOCOL_$_';
  4658. objcmetartti:
  4659. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4660. else
  4661. internalerror(2009092501);
  4662. end;
  4663. end;
  4664. end;
  4665. end;
  4666. result:=result+objextname^;
  4667. end;
  4668. end;
  4669. function tobjectdef.members_need_inittable : boolean;
  4670. begin
  4671. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4672. end;
  4673. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4674. var
  4675. ImplIntf : TImplementedInterface;
  4676. i : longint;
  4677. begin
  4678. result:=nil;
  4679. if not assigned(ImplementedInterfaces) then
  4680. exit;
  4681. for i:=0 to ImplementedInterfaces.Count-1 do
  4682. begin
  4683. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4684. if ImplIntf.intfdef=aintfdef then
  4685. begin
  4686. result:=ImplIntf;
  4687. exit;
  4688. end;
  4689. end;
  4690. end;
  4691. function tobjectdef.is_publishable : boolean;
  4692. begin
  4693. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4694. end;
  4695. function tobjectdef.get_next_dispid: longint;
  4696. begin
  4697. inc(fcurrent_dispid);
  4698. result:=fcurrent_dispid;
  4699. end;
  4700. function tobjectdef.search_enumerator_get: tprocdef;
  4701. begin
  4702. result:=inherited;
  4703. if not assigned(result) and assigned(childof) then
  4704. result:=childof.search_enumerator_get;
  4705. end;
  4706. function tobjectdef.search_enumerator_move: tprocdef;
  4707. begin
  4708. result:=inherited;
  4709. if not assigned(result) and assigned(childof) then
  4710. result:=childof.search_enumerator_move;
  4711. end;
  4712. function tobjectdef.search_enumerator_current: tsym;
  4713. begin
  4714. result:=inherited;
  4715. if not assigned(result) and assigned(childof) then
  4716. result:=childof.search_enumerator_current;
  4717. end;
  4718. procedure tobjectdef.register_created_classref_type;
  4719. begin
  4720. if not classref_created_in_current_module then
  4721. begin
  4722. classref_created_in_current_module:=true;
  4723. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4724. end;
  4725. end;
  4726. procedure tobjectdef.register_created_object_type;
  4727. begin
  4728. if not created_in_current_module then
  4729. begin
  4730. created_in_current_module:=true;
  4731. current_module.wpoinfo.addcreatedobjtype(self);
  4732. end;
  4733. end;
  4734. procedure tobjectdef.register_maybe_created_object_type;
  4735. begin
  4736. { if we know it has been created for sure, no need
  4737. to also record that it maybe can be created in
  4738. this module
  4739. }
  4740. if not (created_in_current_module) and
  4741. not (maybe_created_in_current_module) then
  4742. begin
  4743. maybe_created_in_current_module:=true;
  4744. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4745. end;
  4746. end;
  4747. procedure tobjectdef.register_vmt_call(index: longint);
  4748. begin
  4749. if (is_object(self) or is_class(self)) then
  4750. current_module.wpoinfo.addcalledvmtentry(self,index);
  4751. end;
  4752. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4753. var
  4754. def: tdef absolute data;
  4755. pd: tprocdef absolute data;
  4756. i,
  4757. paracount: longint;
  4758. begin
  4759. if (def.typ=procdef) then
  4760. begin
  4761. { add all messages also under a dummy name to the symtable in
  4762. which the objcclass/protocol/category is declared, so they can
  4763. be called via id.<name>
  4764. }
  4765. create_class_helper_for_procdef(pd,nil);
  4766. { we have to wait until now to set the mangled name because it
  4767. depends on the (possibly external) class name, which is defined
  4768. at the very end. }
  4769. if not(po_msgstr in pd.procoptions) then
  4770. begin
  4771. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4772. { recover to avoid internalerror later on }
  4773. include(pd.procoptions,po_msgstr);
  4774. pd.messageinf.str:=stringdup('MissingDeclaration');
  4775. end;
  4776. { Mangled name is already set in case this is a copy of
  4777. another type. }
  4778. if not(po_has_mangledname in pd.procoptions) then
  4779. begin
  4780. { check whether the number of formal parameters is correct,
  4781. and whether they have valid Objective-C types }
  4782. paracount:=0;
  4783. for i:=1 to length(pd.messageinf.str^) do
  4784. if pd.messageinf.str^[i]=':' then
  4785. inc(paracount);
  4786. for i:=0 to pd.paras.count-1 do
  4787. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4788. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4789. dec(paracount);
  4790. if (paracount<>0) then
  4791. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4792. pd.setmangledname(pd.objcmangledname);
  4793. end
  4794. else
  4795. { all checks already done }
  4796. exit;
  4797. if not(oo_is_external in pd.struct.objectoptions) then
  4798. begin
  4799. if (po_varargs in pd.procoptions) then
  4800. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4801. else
  4802. begin
  4803. { check for "array of const" parameters }
  4804. for i:=0 to pd.parast.symlist.count-1 do
  4805. begin
  4806. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4807. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4808. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4809. end;
  4810. end;
  4811. end;
  4812. end;
  4813. end;
  4814. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4815. var
  4816. sym: tsym absolute data;
  4817. begin
  4818. if (sym.typ=fieldvarsym) and
  4819. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4820. sym.IncRefCount;
  4821. end;
  4822. procedure tobjectdef.finish_objc_data;
  4823. begin
  4824. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4825. if (oo_is_external in objectoptions) then
  4826. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4827. end;
  4828. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4829. var
  4830. sym: tabstractvarsym absolute data;
  4831. res: pboolean absolute arg;
  4832. founderrordef: tdef;
  4833. begin
  4834. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4835. exit;
  4836. if (sym.typ=paravarsym) and
  4837. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4838. is_array_of_const(tparavarsym(sym).vardef)) then
  4839. exit;
  4840. if not objcchecktype(sym.vardef,founderrordef) then
  4841. begin
  4842. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4843. res^:=false;
  4844. end;
  4845. end;
  4846. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4847. var
  4848. def: tdef absolute data;
  4849. res: pboolean absolute arg;
  4850. founderrordef: tdef;
  4851. begin
  4852. if (def.typ<>procdef) then
  4853. exit;
  4854. { check parameter types for validity }
  4855. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4856. { check the result type for validity }
  4857. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4858. begin
  4859. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4860. res^:=false;
  4861. end;
  4862. end;
  4863. function tobjectdef.check_objc_types: boolean;
  4864. begin
  4865. { done in separate step from finish_objc_data, because when
  4866. finish_objc_data is called, not all forwarddefs have been resolved
  4867. yet and we need to know all types here }
  4868. result:=true;
  4869. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4870. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4871. end;
  4872. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4873. var
  4874. def: tdef absolute data;
  4875. pd: tprocdef absolute data;
  4876. begin
  4877. if (def.typ=procdef) then
  4878. begin
  4879. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4880. if (oo_is_external in pd.struct.objectoptions) then
  4881. begin
  4882. { copied from psub.read_proc }
  4883. if assigned(tobjectdef(pd.struct).import_lib) then
  4884. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname(false),pd.mangledname(false),0,false,false)
  4885. else
  4886. begin
  4887. { add import name to external list for DLL scanning }
  4888. if tf_has_dllscanner in target_info.flags then
  4889. current_module.dllscannerinputlist.Add(pd.mangledname(false),pd);
  4890. end;
  4891. end;
  4892. end;
  4893. end;
  4894. procedure tobjectdef.finish_cpp_data;
  4895. begin
  4896. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4897. end;
  4898. {****************************************************************************
  4899. TImplementedInterface
  4900. ****************************************************************************}
  4901. constructor TImplementedInterface.create(aintf: tobjectdef);
  4902. begin
  4903. inherited create;
  4904. intfdef:=aintf;
  4905. IOffset:=-1;
  4906. IType:=etStandard;
  4907. NameMappings:=nil;
  4908. procdefs:=nil;
  4909. end;
  4910. constructor TImplementedInterface.create_deref(d:tderef);
  4911. begin
  4912. inherited create;
  4913. intfdef:=nil;
  4914. intfdefderef:=d;
  4915. IOffset:=-1;
  4916. IType:=etStandard;
  4917. NameMappings:=nil;
  4918. procdefs:=nil;
  4919. end;
  4920. destructor TImplementedInterface.destroy;
  4921. var
  4922. i : longint;
  4923. mappedname : pshortstring;
  4924. begin
  4925. if assigned(NameMappings) then
  4926. begin
  4927. for i:=0 to NameMappings.Count-1 do
  4928. begin
  4929. mappedname:=pshortstring(NameMappings[i]);
  4930. stringdispose(mappedname);
  4931. end;
  4932. NameMappings.free;
  4933. NameMappings:=nil;
  4934. end;
  4935. if assigned(procdefs) then
  4936. begin
  4937. procdefs.free;
  4938. procdefs:=nil;
  4939. end;
  4940. inherited destroy;
  4941. end;
  4942. procedure TImplementedInterface.buildderef;
  4943. begin
  4944. intfdefderef.build(intfdef);
  4945. end;
  4946. procedure TImplementedInterface.deref;
  4947. begin
  4948. intfdef:=tobjectdef(intfdefderef.resolve);
  4949. end;
  4950. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4951. begin
  4952. if not assigned(NameMappings) then
  4953. NameMappings:=TFPHashList.Create;
  4954. NameMappings.Add(origname,stringdup(newname));
  4955. end;
  4956. function TImplementedInterface.GetMapping(const origname: string):string;
  4957. var
  4958. mappedname : pshortstring;
  4959. begin
  4960. result:='';
  4961. if not assigned(NameMappings) then
  4962. exit;
  4963. mappedname:=PShortstring(NameMappings.Find(origname));
  4964. if assigned(mappedname) then
  4965. result:=mappedname^;
  4966. end;
  4967. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4968. begin
  4969. if not assigned(procdefs) then
  4970. procdefs:=TFPObjectList.Create(false);
  4971. { duplicate entries must be stored, because multiple }
  4972. { interfaces can declare methods with the same name }
  4973. { and all of these get their own VMT entry }
  4974. procdefs.Add(pd);
  4975. end;
  4976. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4977. var
  4978. i : longint;
  4979. begin
  4980. result:=false;
  4981. { interfaces being implemented through delegation are not mergable (FK) }
  4982. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  4983. exit;
  4984. weight:=0;
  4985. { empty interface is mergeable }
  4986. if ProcDefs.Count=0 then
  4987. begin
  4988. result:=true;
  4989. exit;
  4990. end;
  4991. { The interface to merge must at least the number of
  4992. procedures of this interface }
  4993. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4994. exit;
  4995. for i:=0 to ProcDefs.Count-1 do
  4996. begin
  4997. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4998. exit;
  4999. end;
  5000. weight:=ProcDefs.Count;
  5001. result:=true;
  5002. end;
  5003. function TImplementedInterface.getcopy:TImplementedInterface;
  5004. begin
  5005. Result:=TImplementedInterface.Create(nil);
  5006. { 1) the procdefs list will be freed once for each copy
  5007. 2) since the procdefs list owns its elements, those will also be freed for each copy
  5008. 3) idem for the name mappings
  5009. }
  5010. { warning: this is completely wrong on so many levels...
  5011. Move(pointer(self)^,pointer(result)^,InstanceSize);
  5012. We need to make clean copies of the different fields
  5013. this is not implemented yet, and thus we generate an internal
  5014. error instead PM 2011-06-14 }
  5015. internalerror(2011061401);
  5016. end;
  5017. {****************************************************************************
  5018. TFORWARDDEF
  5019. ****************************************************************************}
  5020. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  5021. begin
  5022. inherited create(forwarddef);
  5023. tosymname:=stringdup(s);
  5024. forwardpos:=pos;
  5025. end;
  5026. function tforwarddef.GetTypeName:string;
  5027. begin
  5028. GetTypeName:='unresolved forward to '+tosymname^;
  5029. end;
  5030. destructor tforwarddef.destroy;
  5031. begin
  5032. stringdispose(tosymname);
  5033. inherited destroy;
  5034. end;
  5035. function tforwarddef.getcopy:tstoreddef;
  5036. begin
  5037. result:=tforwarddef.create(tosymname^, forwardpos);
  5038. end;
  5039. {****************************************************************************
  5040. TUNDEFINEDDEF
  5041. ****************************************************************************}
  5042. constructor tundefineddef.create;
  5043. begin
  5044. inherited create(undefineddef);
  5045. end;
  5046. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  5047. begin
  5048. inherited ppuload(undefineddef,ppufile);
  5049. end;
  5050. function tundefineddef.GetTypeName:string;
  5051. begin
  5052. GetTypeName:='<undefined type>';
  5053. end;
  5054. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  5055. begin
  5056. inherited ppuwrite(ppufile);
  5057. ppufile.writeentry(ibundefineddef);
  5058. end;
  5059. {****************************************************************************
  5060. TERRORDEF
  5061. ****************************************************************************}
  5062. constructor terrordef.create;
  5063. begin
  5064. inherited create(errordef);
  5065. { prevent consecutive faults }
  5066. savesize:=1;
  5067. end;
  5068. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  5069. begin
  5070. { Can't write errordefs to ppu }
  5071. internalerror(200411063);
  5072. end;
  5073. function terrordef.GetTypeName:string;
  5074. begin
  5075. GetTypeName:='<erroneous type>';
  5076. end;
  5077. function terrordef.getmangledparaname:string;
  5078. begin
  5079. getmangledparaname:='error';
  5080. end;
  5081. {****************************************************************************
  5082. Definition Helpers
  5083. ****************************************************************************}
  5084. function is_interfacecom(def: tdef): boolean;
  5085. begin
  5086. is_interfacecom:=
  5087. assigned(def) and
  5088. (def.typ=objectdef) and
  5089. (tobjectdef(def).objecttype=odt_interfacecom);
  5090. end;
  5091. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  5092. begin
  5093. is_interfacecom_or_dispinterface:=
  5094. assigned(def) and
  5095. (def.typ=objectdef) and
  5096. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  5097. end;
  5098. function is_interfacecorba(def: tdef): boolean;
  5099. begin
  5100. is_interfacecorba:=
  5101. assigned(def) and
  5102. (def.typ=objectdef) and
  5103. (tobjectdef(def).objecttype=odt_interfacecorba);
  5104. end;
  5105. function is_interface(def: tdef): boolean;
  5106. begin
  5107. is_interface:=
  5108. assigned(def) and
  5109. (def.typ=objectdef) and
  5110. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  5111. end;
  5112. function is_dispinterface(def: tdef): boolean;
  5113. begin
  5114. result:=
  5115. assigned(def) and
  5116. (def.typ=objectdef) and
  5117. (tobjectdef(def).objecttype=odt_dispinterface);
  5118. end;
  5119. function is_class(def: tdef): boolean;
  5120. begin
  5121. is_class:=
  5122. assigned(def) and
  5123. (def.typ=objectdef) and
  5124. (tobjectdef(def).objecttype=odt_class);
  5125. end;
  5126. function is_object(def: tdef): boolean;
  5127. begin
  5128. is_object:=
  5129. assigned(def) and
  5130. (def.typ=objectdef) and
  5131. (tobjectdef(def).objecttype=odt_object);
  5132. end;
  5133. function is_cppclass(def: tdef): boolean;
  5134. begin
  5135. is_cppclass:=
  5136. assigned(def) and
  5137. (def.typ=objectdef) and
  5138. (tobjectdef(def).objecttype=odt_cppclass);
  5139. end;
  5140. function is_objcclass(def: tdef): boolean;
  5141. begin
  5142. is_objcclass:=
  5143. assigned(def) and
  5144. (def.typ=objectdef) and
  5145. (tobjectdef(def).objecttype=odt_objcclass);
  5146. end;
  5147. function is_objectpascal_helper(def: tdef): boolean;
  5148. begin
  5149. result:=
  5150. assigned(def) and
  5151. (def.typ=objectdef) and
  5152. (tobjectdef(def).objecttype=odt_helper);
  5153. end;
  5154. function is_objcclassref(def: tdef): boolean;
  5155. begin
  5156. is_objcclassref:=
  5157. assigned(def) and
  5158. (def.typ=classrefdef) and
  5159. is_objcclass(tclassrefdef(def).pointeddef);
  5160. end;
  5161. function is_objcprotocol(def: tdef): boolean;
  5162. begin
  5163. result:=
  5164. assigned(def) and
  5165. (def.typ=objectdef) and
  5166. (tobjectdef(def).objecttype=odt_objcprotocol);
  5167. end;
  5168. function is_objccategory(def: tdef): boolean;
  5169. begin
  5170. result:=
  5171. assigned(def) and
  5172. (def.typ=objectdef) and
  5173. { if used as a forward type }
  5174. ((tobjectdef(def).objecttype=odt_objccategory) or
  5175. { if used as after it has been resolved }
  5176. ((tobjectdef(def).objecttype=odt_objcclass) and
  5177. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  5178. end;
  5179. function is_objc_class_or_protocol(def: tdef): boolean;
  5180. begin
  5181. result:=
  5182. assigned(def) and
  5183. (def.typ=objectdef) and
  5184. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  5185. end;
  5186. function is_objc_protocol_or_category(def: tdef): boolean;
  5187. begin
  5188. result:=
  5189. assigned(def) and
  5190. (def.typ=objectdef) and
  5191. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  5192. ((tobjectdef(def).objecttype = odt_objcclass) and
  5193. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  5194. end;
  5195. function is_classhelper(def: tdef): boolean;
  5196. begin
  5197. result:=
  5198. is_objectpascal_helper(def) or
  5199. is_objccategory(def);
  5200. end;
  5201. function is_class_or_interface(def: tdef): boolean;
  5202. begin
  5203. result:=
  5204. assigned(def) and
  5205. (def.typ=objectdef) and
  5206. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  5207. end;
  5208. function is_class_or_interface_or_objc(def: tdef): boolean;
  5209. begin
  5210. result:=
  5211. assigned(def) and
  5212. (def.typ=objectdef) and
  5213. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  5214. end;
  5215. function is_class_or_interface_or_object(def: tdef): boolean;
  5216. begin
  5217. result:=
  5218. assigned(def) and
  5219. (def.typ=objectdef) and
  5220. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  5221. end;
  5222. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  5223. begin
  5224. result:=
  5225. assigned(def) and
  5226. (def.typ=objectdef) and
  5227. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5228. end;
  5229. function is_implicit_pointer_object_type(def: tdef): boolean;
  5230. begin
  5231. result:=
  5232. assigned(def) and
  5233. (def.typ=objectdef) and
  5234. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava]);
  5235. end;
  5236. function is_class_or_object(def: tdef): boolean;
  5237. begin
  5238. result:=
  5239. assigned(def) and
  5240. (def.typ=objectdef) and
  5241. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  5242. end;
  5243. function is_record(def: tdef): boolean;
  5244. begin
  5245. result:=
  5246. assigned(def) and
  5247. (def.typ=recorddef);
  5248. end;
  5249. function is_javaclass(def: tdef): boolean;
  5250. begin
  5251. result:=
  5252. assigned(def) and
  5253. (def.typ=objectdef) and
  5254. (tobjectdef(def).objecttype=odt_javaclass);
  5255. end;
  5256. function is_javainterface(def: tdef): boolean;
  5257. begin
  5258. result:=
  5259. assigned(def) and
  5260. (def.typ=objectdef) and
  5261. (tobjectdef(def).objecttype=odt_interfacejava);
  5262. end;
  5263. function is_java_class_or_interface(def: tdef): boolean;
  5264. begin
  5265. result:=
  5266. assigned(def) and
  5267. (def.typ=objectdef) and
  5268. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  5269. end;
  5270. procedure loadobjctypes;
  5271. begin
  5272. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  5273. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  5274. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  5275. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  5276. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  5277. end;
  5278. procedure maybeloadcocoatypes;
  5279. var
  5280. tsym: ttypesym;
  5281. begin
  5282. if assigned(objc_fastenumeration) then
  5283. exit;
  5284. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  5285. if assigned(tsym) then
  5286. objc_fastenumeration:=tobjectdef(tsym.typedef)
  5287. else
  5288. objc_fastenumeration:=nil;
  5289. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  5290. if assigned(tsym) then
  5291. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  5292. else
  5293. objc_fastenumerationstate:=nil;
  5294. end;
  5295. function use_vectorfpu(def : tdef) : boolean;
  5296. begin
  5297. {$ifdef x86}
  5298. {$define use_vectorfpuimplemented}
  5299. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  5300. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  5301. {$endif x86}
  5302. {$ifdef arm}
  5303. {$define use_vectorfpuimplemented}
  5304. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  5305. {$endif arm}
  5306. {$ifndef use_vectorfpuimplemented}
  5307. use_vectorfpu:=false;
  5308. {$endif}
  5309. end;
  5310. end.