symdef.pas 195 KB

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