symdef.pas 195 KB

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