symdef.pas 202 KB

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