symdef.pas 179 KB

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