symdef.pas 184 KB

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