symdef.pas 332 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781
  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,widestr,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { node }
  28. node,
  29. { aasm }
  30. aasmtai,
  31. cpuinfo,
  32. cgbase,
  33. parabase
  34. ;
  35. type
  36. {************************************************
  37. TDef
  38. ************************************************}
  39. tgenericconstraintdata=class
  40. interfaces : tfpobjectlist;
  41. interfacesderef : tfplist;
  42. flags : tgenericconstraintflags;
  43. { only required while parsing }
  44. fileinfo : tfileposinfo;
  45. constructor create;
  46. destructor destroy;override;
  47. procedure ppuload(ppufile:tcompilerppufile);
  48. procedure ppuwrite(ppufile:tcompilerppufile);
  49. procedure buildderef;
  50. procedure deref;
  51. end;
  52. { trtti_attribute_list }
  53. trtti_attribute = class
  54. typesym : tsym;
  55. typesymderef : tderef;
  56. typeconstr : tdef;
  57. typeconstrderef : tderef;
  58. { these two are not stored in PPU }
  59. constructorcall : tnode;
  60. constructorpd : tdef;
  61. paras : array of tnode;
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. procedure ppuwrite(ppufile:tcompilerppufile);
  64. procedure ppuload_subentries(ppufile:tcompilerppufile);
  65. procedure ppuwrite_subentries(ppufile:tcompilerppufile);
  66. destructor destroy;override;
  67. procedure buildderef;
  68. procedure deref;
  69. end;
  70. trtti_attribute_list = class
  71. rtti_attributes : TFPObjectList;
  72. { if the attribute list is bound to a def or symbol }
  73. is_bound : Boolean;
  74. class procedure bind(var dangling,owned:trtti_attribute_list);
  75. class procedure copyandbind(alist :trtti_attribute_list; var owned : trtti_attribute_list);
  76. procedure addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  77. procedure addattribute(attr:trtti_attribute);
  78. destructor destroy; override;
  79. function get_attribute_count:longint;
  80. procedure buildderef;
  81. procedure deref;
  82. class function ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  83. class procedure ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  84. class procedure ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  85. class procedure ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  86. end;
  87. { tstoreddef }
  88. tstoreddef = class(tdef)
  89. private
  90. {$ifdef symansistr}
  91. _fullownerhierarchyname : ansistring;
  92. {$else symansistr}
  93. _fullownerhierarchyname : pshortstring;
  94. {$endif symansistr}
  95. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  96. protected
  97. typesymderef : tderef;
  98. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  99. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  100. { a (possibly) reusable def is always created on the basis of another
  101. def, and contains a reference to this other def. If this other
  102. def is in a non-persistent symboltable, the new def cannot actually
  103. be safely reused everywhere in the current module. This routine
  104. abtracts that checking, and also restores the symtable stack
  105. (which had to be reset before creating the new def, so that the new
  106. def did not automatically get added to its top) }
  107. class procedure setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  108. public
  109. {$ifdef EXTDEBUG}
  110. fileinfo : tfileposinfo;
  111. {$endif}
  112. { generic support }
  113. genericdef : tstoreddef;
  114. genericdefderef : tderef;
  115. generictokenbuf : tdynamicarray;
  116. { this list contains references to the symbols that make up the
  117. generic parameters; the symbols are not owned by this list
  118. Note: this list is allocated on demand! }
  119. genericparas : tfphashobjectlist;
  120. genericparaderefs : tfplist;
  121. { contains additional data if this def is a generic constraint
  122. Note: this class is allocated on demand! }
  123. genconstraintdata : tgenericconstraintdata;
  124. { this is Nil if the def has no RTTI attributes }
  125. rtti_attribute_list : trtti_attribute_list;
  126. { original def for "type <name>" declarations }
  127. orgdef : tstoreddef;
  128. orgdefderef : tderef;
  129. constructor create(dt:tdeftyp;doregister:boolean);
  130. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  131. destructor destroy;override;
  132. function getcopy : tstoreddef;virtual;
  133. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  134. { this is called directly after ppuload }
  135. procedure ppuload_subentries(ppufile:tcompilerppufile);virtual;
  136. { this is called directly after ppuwrite }
  137. procedure ppuwrite_subentries(ppufile:tcompilerppufile);virtual;
  138. procedure buildderef;override;
  139. procedure buildderefimpl;override;
  140. procedure deref;override;
  141. procedure derefimpl;override;
  142. function size:asizeint;override;
  143. function getvardef:longint;override;
  144. function alignment:shortint;override;
  145. function is_publishable : tpublishproperty;override;
  146. function needs_inittable : boolean;override;
  147. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  148. function rtti_mangledname(rt:trttitype):TSymStr;override;
  149. function OwnerHierarchyName: string; override;
  150. function OwnerHierarchyPrettyName: string; override;
  151. function fullownerhierarchyname(skipprocparams:boolean):TSymStr;override;
  152. function needs_separate_initrtti:boolean;override;
  153. function in_currentunit: boolean;
  154. { regvars }
  155. function is_intregable : boolean;
  156. function is_fpuregable : boolean;
  157. { def can be put into a register if it is const/immutable }
  158. function is_const_intregable : boolean;
  159. { generics }
  160. procedure initgeneric;
  161. { this function can be used to determine whether a def is really a
  162. generic declaration or just a normal type declared inside another
  163. generic }
  164. function is_generic:boolean;
  165. { same as above for specializations }
  166. function is_specialization:boolean;
  167. { generic utilities }
  168. function is_generic_param_const(index:integer):boolean;inline;
  169. function get_generic_param_def(index:integer):tdef;inline;
  170. { registers this def in the unit's deflist; no-op if already registered }
  171. procedure register_def; override;
  172. { add the def to the top of the symtable stack if it's not yet owned
  173. by another symtable }
  174. procedure maybe_put_in_symtable_stack;
  175. private
  176. savesize : asizeuint;
  177. end;
  178. tfiletyp = (ft_text,ft_typed,ft_untyped);
  179. tfiledef = class(tstoreddef)
  180. filetyp : tfiletyp;
  181. typedfiledef : tdef;
  182. typedfiledefderef : tderef;
  183. constructor createtext;virtual;
  184. constructor createuntyped;virtual;
  185. constructor createtyped(def : tdef);virtual;
  186. constructor ppuload(ppufile:tcompilerppufile);
  187. function getcopy : tstoreddef;override;
  188. { do not override this routine in platform-specific subclasses,
  189. override ppuwrite_platform instead }
  190. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  191. procedure buildderef;override;
  192. procedure deref;override;
  193. function GetTypeName:string;override;
  194. function getmangledparaname:TSymStr;override;
  195. function size:asizeint;override;
  196. procedure setsize;
  197. function alignment: shortint; override;
  198. end;
  199. tfiledefclass = class of tfiledef;
  200. tvariantdef = class(tstoreddef)
  201. varianttype : tvarianttype;
  202. constructor create(v : tvarianttype);virtual;
  203. constructor ppuload(ppufile:tcompilerppufile);
  204. function getcopy : tstoreddef;override;
  205. function GetTypeName:string;override;
  206. function alignment : shortint;override;
  207. { do not override this routine in platform-specific subclasses,
  208. override ppuwrite_platform instead }
  209. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  210. function getvardef:longint;override;
  211. procedure setsize;
  212. function is_publishable : tpublishproperty;override;
  213. function needs_inittable : boolean;override;
  214. end;
  215. tvariantdefclass = class of tvariantdef;
  216. tformaldef = class(tstoreddef)
  217. typed:boolean;
  218. constructor create(Atyped:boolean);virtual;
  219. constructor ppuload(ppufile:tcompilerppufile);
  220. { do not override this routine in platform-specific subclasses,
  221. override ppuwrite_platform instead }
  222. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  223. function GetTypeName:string;override;
  224. end;
  225. tformaldefclass = class of tformaldef;
  226. tforwarddef = class(tstoreddef)
  227. tosymname : pshortstring;
  228. forwardpos : tfileposinfo;
  229. constructor create(const s:string;const pos:tfileposinfo);virtual;
  230. destructor destroy;override;
  231. function getcopy:tstoreddef;override;
  232. function GetTypeName:string;override;
  233. end;
  234. tforwarddefclass = class of tforwarddef;
  235. tundefineddef = class(tstoreddef)
  236. constructor create(doregister:boolean);virtual;
  237. constructor ppuload(ppufile:tcompilerppufile);
  238. { do not override this routine in platform-specific subclasses,
  239. override ppuwrite_platform instead }
  240. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  241. function GetTypeName:string;override;
  242. end;
  243. tundefineddefclass = class of tundefineddef;
  244. terrordef = class(tstoreddef)
  245. constructor create;virtual;
  246. { do not override this routine in platform-specific subclasses,
  247. override ppuwrite_platform instead }
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  249. function GetTypeName:string;override;
  250. function getmangledparaname : TSymStr;override;
  251. end;
  252. terrordefclass = class of terrordef;
  253. tabstractpointerdef = class(tstoreddef)
  254. pointeddef : tdef;
  255. pointeddefderef : tderef;
  256. constructor create(dt:tdeftyp;def:tdef);
  257. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  258. procedure ppuwrite(ppufile:tcompilerppufile);override;
  259. procedure buildderef;override;
  260. procedure deref;override;
  261. function size:asizeint;override;
  262. function alignment:shortint;override;
  263. end;
  264. tpointerdef = class(tabstractpointerdef)
  265. has_pointer_math : boolean;
  266. constructor create(def:tdef);virtual;
  267. { returns a pointerdef for def, reusing an existing one in case it
  268. exists in the current module }
  269. class function getreusable(def: tdef): tpointerdef; virtual;
  270. { same as above, but in case the def must never be freed after the
  271. current module has been compiled -- even if the def was not written
  272. to the ppu file (for defs in para locations, as we don't reset them
  273. so we don't have to recalculate them all the time) }
  274. class function getreusable_no_free(def: tdef): tpointerdef;
  275. function size:asizeint;override;
  276. function getcopy:tstoreddef;override;
  277. constructor ppuload(ppufile:tcompilerppufile);
  278. { do not override this routine in platform-specific subclasses,
  279. override ppuwrite_platform instead }
  280. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  281. function GetTypeName:string;override;
  282. {# returns the appropriate int type for pointer arithmetic with the given pointer type.
  283. When adding or subtracting a number to/from a pointer, this function returns the
  284. int type to which that number has to be converted, before the operation can be performed.
  285. Normally, this is sinttype, except on i8086, where it takes into account the
  286. special i8086 pointer types (near, far, huge). }
  287. function pointer_arithmetic_int_type:tdef;virtual;
  288. {# the unsigned version of pointer_arithmetic_int_type. Used with inc/dec. }
  289. function pointer_arithmetic_uint_type:tdef;virtual;
  290. {# returns the int type produced when subtracting two pointers of the given type.
  291. Normally, this is sinttype, except on i8086, where it takes into account the
  292. special i8086 pointer types (near, far, huge). }
  293. function pointer_subtraction_result_type:tdef;virtual;
  294. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  295. {# the integer index type used to convert the pointer to array (i.e. denotes int_type in: ptr[int_type]) }
  296. function converted_pointer_to_array_range_type:tdef;virtual;
  297. end;
  298. tpointerdefclass = class of tpointerdef;
  299. tprocdef = class;
  300. tabstractrecorddef= class(tstoreddef)
  301. private
  302. rttistring : string;
  303. {$ifdef DEBUG_NODE_XML}
  304. protected
  305. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  306. {$endif DEBUG_NODE_XML}
  307. public
  308. objname,
  309. objrealname : PShortString;
  310. { for C++ classes: name of the library this class is imported from }
  311. { for Java classes/records: package name }
  312. import_lib : PShortString;
  313. symtable : TSymtable;
  314. cloneddef : tabstractrecorddef;
  315. cloneddefderef : tderef;
  316. objectoptions : tobjectoptions;
  317. rtti : trtti_directive;
  318. { for targets that initialise typed constants via explicit assignments
  319. instead of by generating an initialised data sectino }
  320. tcinitcode : tnode;
  321. constructor create(const n:string; dt:tdeftyp;doregister:boolean);
  322. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. destructor destroy; override;
  325. procedure buildderefimpl;override;
  326. procedure derefimpl;override;
  327. procedure check_forwards; virtual;
  328. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  329. function GetSymtable(t:tGetSymtable):TSymtable;override;
  330. function is_packed:boolean;
  331. function RttiName: string;
  332. { enumerator support }
  333. function search_enumerator_get: tprocdef; virtual;
  334. function search_enumerator_move: tprocdef; virtual;
  335. function search_enumerator_current: tsym; virtual;
  336. { JVM }
  337. function jvm_full_typename(with_package_name: boolean): string;
  338. { check if the symtable contains a float field }
  339. function contains_float_field : boolean;
  340. { check if the symtable contains a field that spans an aword boundary }
  341. function contains_cross_aword_field: boolean;
  342. { extended RTTI }
  343. procedure apply_rtti_directive(dir: trtti_directive); virtual;
  344. function is_visible_for_rtti(option: trtti_option; vis: tvisibility): boolean; inline;
  345. function rtti_visibilities_for_option(option: trtti_option): tvisibilities; inline;
  346. function has_extended_rtti: boolean; inline;
  347. end;
  348. pvariantrecdesc = ^tvariantrecdesc;
  349. tvariantrecbranch = record
  350. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  351. which does not support range expressions in variant record definitions }
  352. values : array of Tconstexprint;
  353. nestedvariant : pvariantrecdesc;
  354. end;
  355. ppvariantrecdesc = ^pvariantrecdesc;
  356. tvariantrecdesc = record
  357. variantselector : tsym;
  358. variantselectorderef : tderef;
  359. branches : array of tvariantrecbranch;
  360. end;
  361. trecorddef = class(tabstractrecorddef)
  362. {$ifdef DEBUG_NODE_XML}
  363. protected
  364. function XMLPrintType: ansistring; override;
  365. {$endif DEBUG_NODE_XML}
  366. public
  367. variantrecdesc : pvariantrecdesc;
  368. isunion : boolean;
  369. constructor create(const n:string; p:TSymtable);virtual;
  370. constructor create_global_internal(const n: string; packrecords, recordalignmin: shortint); virtual;
  371. constructor create_internal(const n: string; packrecords, recordalignmin: shortint; where: tsymtable); virtual;
  372. function add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  373. procedure add_fields_from_deflist(fieldtypes: tfplist);
  374. constructor ppuload(ppufile:tcompilerppufile);
  375. destructor destroy;override;
  376. function getcopy : tstoreddef;override;
  377. { do not override this routine in platform-specific subclasses,
  378. override ppuwrite_platform instead }
  379. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  380. procedure buildderef;override;
  381. procedure deref;override;
  382. function size:asizeint;override;
  383. function alignment : shortint;override;
  384. function padalignment: shortint;
  385. function GetTypeName:string;override;
  386. { debug }
  387. function needs_inittable : boolean;override;
  388. function needs_separate_initrtti:boolean;override;
  389. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  390. end;
  391. trecorddefclass = class of trecorddef;
  392. tobjectdef = class;
  393. { TImplementedInterface }
  394. TImplementedInterface = class
  395. private
  396. fIOffset : longint;
  397. function GetIOffset: longint;
  398. public
  399. IntfDef : tobjectdef;
  400. IntfDefDeref : tderef;
  401. IType : tinterfaceentrytype;
  402. VtblImplIntf : TImplementedInterface;
  403. NameMappings : TFPHashList;
  404. ProcDefs : TFPObjectList;
  405. ImplementsGetter : tsym;
  406. ImplementsGetterDeref : tderef;
  407. ImplementsField : tsym;
  408. constructor create(aintf: tobjectdef);virtual;
  409. constructor create_deref(intfd,getterd:tderef);virtual;
  410. destructor destroy; override;
  411. function getcopy:TImplementedInterface;
  412. procedure buildderef;
  413. procedure deref;
  414. procedure AddMapping(const origname, newname: string);
  415. function GetMapping(const origname: string):string;
  416. procedure AddImplProc(pd:tprocdef);
  417. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  418. property IOffset: longint read GetIOffset write fIOffset;
  419. end;
  420. timplementedinterfaceclass = class of timplementedinterface;
  421. { tvmtentry }
  422. tvmtentry = record
  423. procdef : tprocdef;
  424. procdefderef : tderef;
  425. visibility : tvisibility;
  426. end;
  427. pvmtentry = ^tvmtentry;
  428. { tobjectdef }
  429. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  430. pmvcallstaticinfo = ^tmvcallstaticinfo;
  431. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  432. tobjectdef = class(tabstractrecorddef)
  433. private
  434. fcurrent_dispid: longint;
  435. {$ifdef DEBUG_NODE_XML}
  436. protected
  437. function XMLPrintType: ansistring; override;
  438. procedure XMLPrintDefInfo(var T: Text; Sym: TSym); override;
  439. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  440. {$endif DEBUG_NODE_XML}
  441. public
  442. childof : tobjectdef;
  443. childofderef : tderef;
  444. { for Object Pascal helpers }
  445. extendeddef : tdef;
  446. extendeddefderef: tderef;
  447. helpertype : thelpertype;
  448. { for Objective-C: protocols and classes can have the same name there }
  449. objextname : pshortstring;
  450. { to be able to have a variable vmt position }
  451. { and no vmt field for objects without virtuals }
  452. vmtentries : TFPList;
  453. vmcallstaticinfo : pmvcallstaticinfo;
  454. vmt_field : tsym;
  455. vmt_fieldderef : tderef;
  456. iidguid : pguid;
  457. iidstr : pshortstring;
  458. { store implemented interfaces defs and name mappings }
  459. ImplementedInterfaces : TFPObjectList;
  460. { number of abstract methods (used by JVM target to determine whether
  461. or not the class should be marked as abstract: must be done if 1 or
  462. more abstract methods) }
  463. abstractcnt : longint;
  464. writing_class_record_dbginfo,
  465. { a class of this type has been created in this module }
  466. created_in_current_module,
  467. { a loadvmtnode for this class has been created in this
  468. module, so if a classrefdef variable of this or a parent
  469. class is used somewhere to instantiate a class, then this
  470. class may be instantiated
  471. }
  472. maybe_created_in_current_module,
  473. { a "class of" this particular class has been created in
  474. this module
  475. }
  476. classref_created_in_current_module : boolean;
  477. objecttype : tobjecttyp;
  478. { for interfaces that can be invoked using Invoke(),
  479. this is the definition of the hidden class that is generated by the compiler.
  480. we need this definition to reference it in the RTTI.
  481. Since interfaces can inherit, so can these hidden classes,
  482. so we need to write this to the ppu to be able to reference the parents.
  483. }
  484. hiddenclassdef : tobjectdef;
  485. hiddenclassdefref : tderef;
  486. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);virtual;
  487. constructor ppuload(ppufile:tcompilerppufile);
  488. destructor destroy;override;
  489. function getcopy : tstoreddef;override;
  490. { do not override this routine in platform-specific subclasses,
  491. override ppuwrite_platform instead }
  492. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  493. function GetTypeName:string;override;
  494. procedure buildderef;override;
  495. procedure deref;override;
  496. procedure derefimpl;override;
  497. procedure copyvmtentries(objdef:tobjectdef);
  498. function getparentdef:tdef;override;
  499. function size : asizeint;override;
  500. function alignment:shortint;override;
  501. function vmtmethodoffset(index:longint):longint;
  502. function members_need_inittable : boolean;
  503. { this should be called when this class implements an interface }
  504. procedure prepareguid;
  505. function is_publishable : tpublishproperty;override;
  506. function needs_inittable : boolean;override;
  507. function needs_separate_initrtti : boolean;override;
  508. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  509. function rtti_mangledname(rt:trttitype):TSymStr;override;
  510. function is_unique_objpasdef: boolean;
  511. function vmt_mangledname : TSymStr;
  512. function vmt_def: trecorddef;
  513. procedure check_forwards; override;
  514. procedure insertvmt;
  515. function vmt_offset: asizeint;
  516. procedure set_parent(c : tobjectdef);
  517. function find_destructor: tprocdef;
  518. function implements_any_interfaces: boolean;
  519. function register_implemented_interface(intfdef:tobjectdef;useguid:boolean):timplementedinterface;
  520. { dispinterface support }
  521. function get_next_dispid: longint;
  522. { enumerator support }
  523. function search_enumerator_get: tprocdef; override;
  524. function search_enumerator_move: tprocdef; override;
  525. function search_enumerator_current: tsym; override;
  526. { WPO }
  527. procedure register_created_object_type;override;
  528. procedure register_maybe_created_object_type;
  529. procedure register_created_classref_type;
  530. procedure register_vmt_call(index:longint);
  531. { ObjC }
  532. procedure finish_objc_data;
  533. function check_objc_types: boolean;
  534. { C++ }
  535. procedure finish_cpp_data;
  536. procedure apply_rtti_directive(dir: trtti_directive); override;
  537. end;
  538. tobjectdefclass = class of tobjectdef;
  539. tclassrefdef = class(tabstractpointerdef)
  540. constructor create(def:tdef);virtual;
  541. constructor ppuload(ppufile:tcompilerppufile);
  542. { do not override this routine in platform-specific subclasses,
  543. override ppuwrite_platform instead }
  544. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  545. function getcopy:tstoreddef;override;
  546. function GetTypeName:string;override;
  547. function is_publishable : tpublishproperty;override;
  548. function rtti_mangledname(rt:trttitype):TSymStr;override;
  549. procedure register_created_object_type;override;
  550. end;
  551. tclassrefdefclass = class of tclassrefdef;
  552. tarraydef = class(tstoreddef)
  553. lowrange,
  554. highrange : asizeint;
  555. rangedef : tdef;
  556. rangedefderef : tderef;
  557. arrayoptions : tarraydefoptions;
  558. symtable : TSymtable;
  559. protected
  560. _elementdef : tdef;
  561. _elementdefderef : tderef;
  562. procedure setelementdef(def:tdef);
  563. class function getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  564. public
  565. function elesize : asizeint;
  566. function elepackedbitsize : asizeint;
  567. function elecount : asizeuint;
  568. constructor create_from_pointer(def: tpointerdef);virtual;
  569. constructor create(l, h: asizeint; def: tdef);virtual;
  570. constructor create_vector(l,h:asizeint; def:tdef);
  571. constructor create_openarray;virtual;
  572. class function getreusable(def: tdef; elems: asizeint): tarraydef;
  573. class function getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  574. { same as above, but in case the def must never be freed after the
  575. current module has been compiled -- even if the def was not written
  576. to the ppu file (for defs in para locations, as we don't reset them
  577. so we don't have to recalculate them all the time) }
  578. class function getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  579. class function getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  580. constructor ppuload(ppufile:tcompilerppufile);
  581. destructor destroy; override;
  582. function getcopy : tstoreddef;override;
  583. { do not override this routine in platform-specific subclasses,
  584. override ppuwrite_platform instead }
  585. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  586. function GetTypeName:string;override;
  587. function getmangledparaname : TSymStr;override;
  588. procedure buildderef;override;
  589. procedure deref;override;
  590. function size : asizeint;override;
  591. function alignment : shortint;override;
  592. { returns the label of the range check string }
  593. function needs_inittable : boolean;override;
  594. function needs_separate_initrtti : boolean;override;
  595. property elementdef : tdef read _elementdef write setelementdef;
  596. function is_publishable : tpublishproperty;override;
  597. function is_hwvector: boolean;
  598. end;
  599. tarraydefclass = class of tarraydef;
  600. torddef = class(tstoreddef)
  601. low,high : TConstExprInt;
  602. ordtype : tordtype;
  603. constructor create(t : tordtype;v,b : TConstExprInt; doregister: boolean);virtual;
  604. constructor ppuload(ppufile:tcompilerppufile);
  605. function getcopy : tstoreddef;override;
  606. { do not override this routine in platform-specific subclasses,
  607. override ppuwrite_platform instead }
  608. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  609. function is_publishable : tpublishproperty;override;
  610. function GetTypeName:string;override;
  611. function alignment:shortint;override;
  612. procedure setsize;
  613. function packedbitsize: asizeint; override;
  614. function getvardef : longint;override;
  615. end;
  616. torddefclass = class of torddef;
  617. tfloatdef = class(tstoreddef)
  618. floattype : tfloattype;
  619. constructor create(t: tfloattype; doregister: boolean);virtual;
  620. constructor ppuload(ppufile:tcompilerppufile);
  621. function getcopy : tstoreddef;override;
  622. { do not override this routine in platform-specific subclasses,
  623. override ppuwrite_platform instead }
  624. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  625. function GetTypeName:string;override;
  626. function is_publishable : tpublishproperty;override;
  627. function alignment:shortint;override;
  628. function structalignment: shortint;override;
  629. procedure setsize;
  630. function getvardef:longint;override;
  631. end;
  632. tfloatdefclass = class of tfloatdef;
  633. { tabstractprocdef }
  634. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  635. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  636. pno_mangledname, pno_noparams, pno_prettynames);
  637. tprocnameoptions = set of tprocnameoption;
  638. tproccopytyp = (pc_normal,
  639. {$ifdef i8086}
  640. { the address in a far format }
  641. pc_far_address,
  642. { the offset part of the far address }
  643. pc_offset,
  644. {$endif i8086}
  645. { creates a procvardef describing only the code pointer
  646. of a method/netsted function/... }
  647. pc_address_only,
  648. { everything except for hidden parameters }
  649. pc_normal_no_hidden,
  650. { everything except for the parameters (because they
  651. might be moved over to the copy) }
  652. pc_normal_no_paras,
  653. { always creates a top-level function, removes all
  654. special parameters (self, vmt, parentfp, ...) }
  655. pc_bareproc
  656. );
  657. tcacheableproccopytyp = pc_normal..pc_address_only;
  658. tabstractprocdef = class(tstoreddef)
  659. { saves a definition to the return type }
  660. returndef : tdef;
  661. returndefderef : tderef;
  662. parast : TSymtable;
  663. paras : tparalist;
  664. proctypeoption : tproctypeoption;
  665. proccalloption : tproccalloption;
  666. procoptions : tprocoptions;
  667. callerargareasize,
  668. calleeargareasize: pint;
  669. {$ifdef m68k}
  670. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  671. {$endif}
  672. funcretloc : array[callerside..calleeside] of TCGPara;
  673. has_paraloc_info : tcallercallee; { paraloc info is available }
  674. { number of user visible parameters }
  675. maxparacount,
  676. minparacount : byte;
  677. constructor create(dt:tdeftyp;level:byte;doregister:boolean);
  678. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  679. destructor destroy;override;
  680. procedure ppuwrite(ppufile:tcompilerppufile);override;
  681. procedure buildderef;override;
  682. procedure deref;override;
  683. procedure calcparas;
  684. function mangledprocparanames(oldlen : longint) : string;
  685. function typename_paras(pno: tprocnameoptions): ansistring;
  686. function is_methodpointer:boolean;virtual;
  687. function is_addressonly:boolean;virtual;
  688. function no_self_node:boolean;
  689. { get either a copy as a procdef or procvardef }
  690. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; virtual;
  691. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  692. procedure check_mark_as_nested;
  693. procedure init_paraloc_info(side: tcallercallee);
  694. procedure done_paraloc_info(side: tcallercallee);
  695. function stack_tainting_parameter(side: tcallercallee): boolean;
  696. function is_pushleftright: boolean;virtual;
  697. function address_type:tdef;virtual;
  698. { address type, generated for ofs() }
  699. function ofs_address_type:tdef;virtual;
  700. procedure declared_far;virtual;
  701. procedure declared_near;virtual;
  702. function generate_safecall_wrapper: boolean; virtual;
  703. { returns true if the def is a generic param of the procdef }
  704. function is_generic_param(def:tdef): boolean;
  705. function wpo_may_create_instance(optionalmethodpointer: tnode): boolean;
  706. private
  707. procedure count_para(p:TObject;arg:pointer);
  708. procedure insert_para(p:TObject;arg:pointer);
  709. end;
  710. tprocvardef = class(tabstractprocdef)
  711. constructor create(level:byte;doregister:boolean);virtual;
  712. { returns a procvardef that represents the address of a proc(var)def }
  713. class function getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef; virtual;
  714. { same as above, but in case the def must never be freed after the
  715. current module has been compiled -- even if the def was not written
  716. to the ppu file (for defs in para locations, as we don't reset them
  717. so we don't have to recalculate them all the time) }
  718. class function getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  719. constructor ppuload(ppufile:tcompilerppufile);
  720. function getcopy : tstoreddef;override;
  721. { do not override this routine in platform-specific subclasses,
  722. override ppuwrite_platform instead }
  723. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  724. function GetSymtable(t:tGetSymtable):TSymtable;override;
  725. function size : asizeint;override;
  726. function GetTypeName:string;override;
  727. function is_publishable : tpublishproperty;override;
  728. function is_methodpointer:boolean;override;
  729. function is_addressonly:boolean;override;
  730. function getmangledparaname:TSymStr;override;
  731. function getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; override;
  732. end;
  733. tprocvardefclass = class of tprocvardef;
  734. tmessageinf = record
  735. case integer of
  736. 0 : (str : pshortstring);
  737. 1 : (i : longint);
  738. end;
  739. tinlininginfo = record
  740. { node tree }
  741. code : tnode;
  742. flags : tprocinfoflags;
  743. end;
  744. pinlininginfo = ^tinlininginfo;
  745. tcapturedsyminfo = record
  746. sym : tsym;
  747. def : tdef;
  748. { the location where the symbol was first encountered }
  749. fileinfo : tfileposinfo;
  750. end;
  751. pcapturedsyminfo = ^tcapturedsyminfo;
  752. timplprocdefinfo = record
  753. resultname : pshortstring;
  754. parentfpstruct: tsym;
  755. parentfpstructptrtype: tdef;
  756. parentfpinitblock: tnode;
  757. procstarttai,
  758. procendtai : tai;
  759. skpara: pointer;
  760. personality: tprocdef;
  761. was_anonymous,
  762. has_capturer,
  763. forwarddef,
  764. interfacedef : boolean;
  765. hasforward : boolean;
  766. is_implemented : boolean;
  767. capturedsyms : tfplist;
  768. end;
  769. pimplprocdefinfo = ^timplprocdefinfo;
  770. { tprocdef }
  771. tprocdef = class(tabstractprocdef)
  772. protected
  773. {$ifdef symansistr}
  774. _mangledname : ansistring;
  775. {$else symansistr}
  776. _mangledname : pshortstring;
  777. {$endif}
  778. _parentfpsym : tsym;
  779. { information that is only required until the implementation of the
  780. procdef has been handled }
  781. implprocdefinfo : pimplprocdefinfo;
  782. function store_localst:boolean;
  783. function GetResultName: PShortString;
  784. procedure SetResultName(AValue: PShortString);
  785. function GetParentFPStruct: tsym;
  786. procedure SetParentFPStruct(AValue: tsym);
  787. function GetParentFPStructPtrType: tdef;
  788. procedure SetParentFPStructPtrType(AValue: tdef);
  789. function GetParentFPInitBlock: tnode;
  790. procedure SetParentFPInitBlock(AValue: tnode);
  791. function Getprocstarttai: tai;
  792. procedure Setprocstarttai(AValue: tai);
  793. function Getprocendtai: tai;
  794. procedure Setprocendtai(AValue: tai);
  795. function Getskpara: pointer;
  796. procedure Setskpara(AValue: pointer);
  797. function Getpersonality: tprocdef;
  798. procedure Setpersonality(AValue: tprocdef);
  799. function Getforwarddef: boolean;
  800. procedure Setforwarddef(AValue: boolean);
  801. function Getinterfacedef: boolean;
  802. procedure Setinterfacedef(AValue: boolean);virtual;
  803. function Gethasforward: boolean;
  804. procedure Sethasforward(AValue: boolean);
  805. function GetIsEmpty: boolean;
  806. procedure SetIsEmpty(AValue: boolean);
  807. function GetHasInliningInfo: boolean;
  808. procedure SetHasInliningInfo(AValue: boolean);
  809. function Getis_implemented: boolean;
  810. procedure Setis_implemented(AValue: boolean);
  811. function getwas_anonymous:boolean;
  812. procedure setwas_anonymous(avalue:boolean);
  813. function gethas_capturer:boolean;
  814. procedure sethas_capturer(avalue:boolean);
  815. function Getcapturedsyms:tfplist;
  816. function getparentfpsym: tsym;
  817. public
  818. messageinf : tmessageinf;
  819. dispid : longint;
  820. {$ifndef EXTDEBUG}
  821. { where is this function defined and what were the symbol
  822. flags, needed here because there
  823. is only one symbol for all overloaded functions
  824. EXTDEBUG has fileinfo in tdef (PFV) }
  825. fileinfo : tfileposinfo;
  826. {$endif}
  827. symoptions : tsymoptions;
  828. deprecatedmsg : pshortstring;
  829. { generic support }
  830. genericdecltokenbuf : tdynamicarray;
  831. { symbol owning this definition }
  832. procsym : tsym;
  833. procsymderef : tderef;
  834. { alias names }
  835. aliasnames : TCmdStrList;
  836. { symtables }
  837. localst : TSymtable;
  838. funcretsym : tsym;
  839. funcretsymderef : tderef;
  840. struct : tabstractrecorddef;
  841. structderef : tderef;
  842. implprocoptions: timplprocoptions;
  843. { import info }
  844. import_dll,
  845. import_name : pshortstring;
  846. { info for inlining the subroutine, if this pointer is nil,
  847. the procedure can't be inlined }
  848. inlininginfo : pinlininginfo;
  849. import_nr : word;
  850. extnumber : word;
  851. { set to a value different from tsk_none in case this procdef is for
  852. a routine that has to be internally generated by the compiler }
  853. synthetickind: tsynthetickind;
  854. visibility : tvisibility;
  855. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  856. seenmarker : pointer; // used for filtering in tcandidate
  857. {$endif}
  858. {$ifdef symansistr}
  859. section: ansistring;
  860. {$else symansistr}
  861. section: pshortstring;
  862. {$endif}
  863. { only needed when actually compiling a unit, no need to save/load from ppu }
  864. invoke_helper : tprocdef;
  865. copied_from : tprocdef;
  866. constructor create(level:byte;doregister:boolean);virtual;
  867. constructor ppuload(ppufile:tcompilerppufile);
  868. destructor destroy;override;
  869. procedure freeimplprocdefinfo;
  870. { do not override this routine in platform-specific subclasses,
  871. override ppuwrite_platform instead }
  872. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  873. procedure buildderef;override;
  874. procedure buildderefimpl;override;
  875. procedure deref;override;
  876. procedure derefimpl;override;
  877. function GetSymtable(t:tGetSymtable):TSymtable;override;
  878. { warnings:
  879. * the symtablestack top has to be the symtable to which the copy
  880. should be added
  881. * getcopy does not create a finished/ready-to-use procdef; it
  882. needs to be finalised afterwards by calling
  883. symcreat.finish_copied_procdef() afterwards
  884. }
  885. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; override;
  886. function getcopy: tstoreddef; override;
  887. function GetTypeName : string;override;
  888. function mangledname : TSymStr; virtual;
  889. procedure setmangledname(const s : TSymStr);
  890. function needsglobalasmsym: boolean;
  891. procedure setcompilerprocname;
  892. function fullprocname(showhidden:boolean):string;
  893. function customprocname(pno: tprocnameoptions):ansistring;
  894. function defaultmangledname: TSymStr;
  895. function cplusplusmangledname : TSymStr;
  896. function objcmangledname : TSymStr;
  897. function is_methodpointer:boolean;override;
  898. function is_addressonly:boolean;override;
  899. procedure make_external;
  900. procedure init_genericdecl;
  901. function get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  902. function get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  903. procedure add_captured_sym(sym:tsym;def:tdef;const filepos:tfileposinfo);
  904. { returns whether the mangled name or any of its aliases is equal to
  905. s }
  906. function has_alias_name(const s: TSymStr):boolean;
  907. { aliases to fields only required when a function is implemented in
  908. the current unit }
  909. property resultname: PShortString read GetResultName write SetResultName;
  910. { temporary reference to structure containing copies of all local
  911. variables and parameters accessed by nested routines; reference to
  912. this structure is passed as "parent frame pointer" on targets that
  913. lack this concept (at least JVM and LLVM); no need to save to/
  914. restore from ppu, since nested routines are always in the same
  915. unit }
  916. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  917. { pointer to parentfpstruct's type (not yet valid during parsing, so
  918. cannot be used for $parentfp parameter) (no need to save to ppu) }
  919. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  920. { code to copy the parameters accessed from nested routines into the
  921. parentfpstruct (no need to save to ppu) }
  922. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  923. { First/last assembler symbol/instruction in aasmoutput list.
  924. Note: initialised after compiling the code for the procdef, but
  925. not saved to/restored from ppu. Used when inserting debug info }
  926. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  927. property procendtai: tai read Getprocendtai write Setprocendtai;
  928. { optional parameter for the synthetic routine generation logic }
  929. property skpara: pointer read Getskpara write Setskpara;
  930. { ABI-conformant exception handling personality function }
  931. property personality: tprocdef read Getpersonality write Setpersonality;
  932. { true, if the procedure is only declared
  933. (forward procedure) }
  934. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  935. { true if the procedure is declared in the interface }
  936. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  937. { true if the procedure has a forward declaration }
  938. property hasforward: boolean read Gethasforward write Sethasforward;
  939. { true if the routine's body is empty }
  940. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  941. { true if all information required to inline this routine is available }
  942. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  943. { returns the $parentfp parameter for nested routines }
  944. property parentfpsym: tsym read getparentfpsym;
  945. { true if the implementation part for this procdef has been handled }
  946. property is_implemented: boolean read Getis_implemented write Setis_implemented;
  947. { valid if the procdef captures any symbols from outer scopes }
  948. property capturedsyms:tfplist read Getcapturedsyms;
  949. { true if this procdef was originally an anonymous function }
  950. property was_anonymous:boolean read getwas_anonymous write setwas_anonymous;
  951. { true if the procdef has a capturer for anonymous functions }
  952. property has_capturer:boolean read gethas_capturer write sethas_capturer;
  953. end;
  954. tprocdefclass = class of tprocdef;
  955. { single linked list of overloaded procs }
  956. pprocdeflist = ^tprocdeflist;
  957. tprocdeflist = record
  958. def : tprocdef;
  959. defderef : tderef;
  960. next : pprocdeflist;
  961. end;
  962. tstringdef = class(tstoreddef)
  963. encoding : tstringencoding;
  964. stringtype : tstringtype;
  965. len : asizeint;
  966. constructor createshort(l: byte; doregister: boolean);virtual;
  967. constructor loadshort(ppufile:tcompilerppufile);
  968. constructor createlong(l: asizeint; doregister: boolean);virtual;
  969. constructor loadlong(ppufile:tcompilerppufile);
  970. constructor createansi(aencoding: tstringencoding; doregister: boolean);virtual;
  971. constructor loadansi(ppufile:tcompilerppufile);
  972. constructor createwide(doregister: boolean);virtual;
  973. constructor loadwide(ppufile:tcompilerppufile);
  974. constructor createunicode(doregister: boolean);virtual;
  975. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  976. function getcopy : tstoreddef;override;
  977. function stringtypname:string;
  978. { do not override this routine in platform-specific subclasses,
  979. override ppuwrite_platform instead }
  980. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  981. function GetTypeName:string;override;
  982. function getmangledparaname:TSymStr;override;
  983. function is_publishable : tpublishproperty;override;
  984. function size:asizeint;override;
  985. function alignment : shortint;override;
  986. function needs_inittable : boolean;override;
  987. function getvardef:longint;override;
  988. { returns the default char type def for the string }
  989. function get_default_char_type: tdef;
  990. { returns the default string type }
  991. function get_default_string_type: tdef;
  992. end;
  993. tstringdefclass = class of tstringdef;
  994. tenumdef = class(tstoreddef)
  995. minval,
  996. maxval : asizeint;
  997. basedef : tenumdef;
  998. basedefderef : tderef;
  999. symtable : TSymtable;
  1000. has_jumps : boolean;
  1001. constructor create;virtual;
  1002. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  1003. constructor ppuload(ppufile:tcompilerppufile);
  1004. destructor destroy;override;
  1005. function getcopy : tstoreddef;override;
  1006. { do not override this routine in platform-specific subclasses,
  1007. override ppuwrite_platform instead }
  1008. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  1009. procedure buildderef;override;
  1010. procedure deref;override;
  1011. function GetTypeName:string;override;
  1012. function is_publishable : tpublishproperty;override;
  1013. procedure calcsavesize(packenum: shortint);
  1014. function packedbitsize: asizeint; override;
  1015. procedure setmax(_max:asizeint);
  1016. procedure setmin(_min:asizeint);
  1017. function min:asizeint;
  1018. function max:asizeint;
  1019. function getfirstsym:tsym;
  1020. function int2enumsym(l: asizeint): tsym;
  1021. { returns basedef if assigned, otherwise self }
  1022. function getbasedef: tenumdef;
  1023. end;
  1024. tenumdefclass = class of tenumdef;
  1025. tsetdef = class(tstoreddef)
  1026. elementdef : tdef;
  1027. elementdefderef : tderef;
  1028. setbase,
  1029. setlow,setmax : asizeint;
  1030. constructor create(def: tdef; low, high: asizeint; doregister: boolean);virtual;
  1031. constructor ppuload(ppufile:tcompilerppufile);
  1032. function getcopy : tstoreddef;override;
  1033. { do not override this routine in platform-specific subclasses,
  1034. override ppuwrite_platform instead }
  1035. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  1036. procedure buildderef;override;
  1037. procedure deref;override;
  1038. function GetTypeName:string;override;
  1039. function is_publishable : tpublishproperty;override;
  1040. function alignment: shortint; override;
  1041. end;
  1042. tsetdefclass = class of tsetdef;
  1043. tgenericdummyentry = class
  1044. dummysym : tsym;
  1045. resolvedsym : tsym;
  1046. end;
  1047. tdefawaresymtablestack = class(TSymtablestack)
  1048. private
  1049. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1050. procedure remove_helpers_and_generics(st:tsymtable);inline;
  1051. procedure remove_helpers(st:tsymtable);
  1052. procedure remove_generics(st:tsymtable);
  1053. procedure pushcommon(st:tsymtable);inline;
  1054. public
  1055. procedure push(st: TSymtable); override;
  1056. procedure pushafter(st,afterst:TSymtable); override;
  1057. procedure pop(st: TSymtable); override;
  1058. end;
  1059. var
  1060. current_structdef: tabstractrecorddef; { used for private functions check !! }
  1061. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  1062. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  1063. cfiledef: tfiledefclass;
  1064. cvariantdef: tvariantdefclass;
  1065. cformaldef: tformaldefclass;
  1066. cforwarddef: tforwarddefclass;
  1067. cundefineddef: tundefineddefclass;
  1068. cerrordef: terrordefclass;
  1069. cpointerdef: tpointerdefclass;
  1070. crecorddef: trecorddefclass;
  1071. cimplementedinterface: timplementedinterfaceclass;
  1072. cobjectdef: tobjectdefclass;
  1073. cclassrefdef: tclassrefdefclass;
  1074. carraydef: tarraydefclass;
  1075. corddef: torddefclass;
  1076. cfloatdef: tfloatdefclass;
  1077. cprocvardef: tprocvardefclass;
  1078. cprocdef: tprocdefclass;
  1079. cstringdef: tstringdefclass;
  1080. cenumdef: tenumdefclass;
  1081. csetdef: tsetdefclass;
  1082. { default types }
  1083. generrordef, { error in definition }
  1084. voidpointertype, { pointer for Void-pointeddef }
  1085. charpointertype, { pointer for Char-pointeddef }
  1086. widecharpointertype, { pointer for WideChar-pointeddef }
  1087. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  1088. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  1089. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  1090. {$ifdef x86}
  1091. voidnearpointertype,
  1092. voidnearcspointertype,
  1093. voidneardspointertype,
  1094. voidnearsspointertype,
  1095. voidnearespointertype,
  1096. voidnearfspointertype,
  1097. voidneargspointertype,
  1098. {$ifdef i8086}
  1099. voidfarpointertype,
  1100. voidhugepointertype,
  1101. charnearpointertype,
  1102. charfarpointertype,
  1103. charhugepointertype,
  1104. bytefarpointertype, { used for Mem[] }
  1105. wordfarpointertype, { used for MemW[] }
  1106. longintfarpointertype, { used for MemL[] }
  1107. {$endif i8086}
  1108. {$endif x86}
  1109. {$ifdef wasm}
  1110. wasmvoidexternreftype,
  1111. {$endif wasm}
  1112. cundefinedtype,
  1113. cformaltype, { unique formal definition }
  1114. ctypedformaltype, { unique typed formal definition }
  1115. voidtype, { Void (procedure) }
  1116. cansichartype, { Char }
  1117. cwidechartype, { WideChar }
  1118. cchartype, { either cansichartype or cwidechartype. Do not free }
  1119. pasbool1type, { boolean type }
  1120. pasbool8type,
  1121. pasbool16type,
  1122. pasbool32type,
  1123. pasbool64type,
  1124. bool8type,
  1125. bool16type,
  1126. bool32type,
  1127. bool64type, { implement me }
  1128. {$ifdef llvm}
  1129. llvmbool1type, { LLVM i1 type }
  1130. {$endif llvm}
  1131. u8inttype, { 8-Bit unsigned integer }
  1132. s8inttype, { 8-Bit signed integer }
  1133. u16inttype, { 16-Bit unsigned integer }
  1134. s16inttype, { 16-Bit signed integer }
  1135. u24inttype, { 24-Bit unsigned integer }
  1136. s24inttype, { 24-Bit signed integer }
  1137. u32inttype, { 32-Bit unsigned integer }
  1138. s32inttype, { 32-Bit signed integer }
  1139. u40inttype, { 40-Bit unsigned integer }
  1140. s40inttype, { 40-Bit signed integer }
  1141. u48inttype, { 48-Bit unsigned integer }
  1142. s48inttype, { 48-Bit signed integer }
  1143. u56inttype, { 56-Bit unsigned integer }
  1144. s56inttype, { 56-Bit signed integer }
  1145. u64inttype, { 64-bit unsigned integer }
  1146. s64inttype, { 64-bit signed integer }
  1147. u128inttype, { 128-bit unsigned integer }
  1148. s128inttype, { 128-bit signed integer }
  1149. s32floattype, { 32 bit floating point number }
  1150. s64floattype, { 64 bit floating point number }
  1151. s80floattype, { 80 bit floating point number }
  1152. sc80floattype, { 80 bit floating point number but stored like in C }
  1153. s64currencytype, { pointer to a currency type }
  1154. cshortstringtype, { pointer to type of short string const }
  1155. clongstringtype, { pointer to type of long string const }
  1156. cansistringtype, { pointer to type of ansi string const }
  1157. cwidestringtype, { pointer to type of wide string const }
  1158. cunicodestringtype,
  1159. openshortstringtype, { pointer to type of an open shortstring,
  1160. needed for readln() }
  1161. openchararraytype, { pointer to type of an open array of char,
  1162. needed for readln() }
  1163. cfiletype, { get the same definition for all file }
  1164. { used for stabs }
  1165. methodpointertype, { typecasting of methodpointers to extract self }
  1166. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  1167. hresultdef,
  1168. typekindtype, { def of TTypeKind for correct handling of GetTypeKind parameters }
  1169. { we use only one variant def for every variant class }
  1170. cvarianttype,
  1171. colevarianttype,
  1172. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  1173. sinttype,
  1174. uinttype,
  1175. { integer types corresponding to OS_SINT/OS_INT }
  1176. ossinttype,
  1177. osuinttype,
  1178. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  1179. alusinttype,
  1180. aluuinttype,
  1181. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  1182. sizeuinttype,
  1183. sizesinttype,
  1184. { unsigned and signed ord type with the same size as a pointer }
  1185. ptruinttype,
  1186. ptrsinttype,
  1187. { unsigned and signed ord type with the same size as a codepointer }
  1188. codeptruinttype,
  1189. codeptrsinttype,
  1190. { several types to simulate more or less C++ objects for GDB }
  1191. vmttype,
  1192. vmtarraytype,
  1193. { type of classrefs, used for stabs }
  1194. pvmttype,
  1195. { return type of the setjmp function }
  1196. exceptionreasontype : tdef;
  1197. { pointer to the anchestor of all classes }
  1198. class_tobject : tobjectdef;
  1199. { pointer to the base type for custom attributes }
  1200. class_tcustomattribute : tobjectdef;
  1201. { pointer to the ancestor of all COM interfaces }
  1202. interface_iunknown : tobjectdef;
  1203. { pointer to the ancestor of all dispinterfaces }
  1204. interface_idispatch : tobjectdef;
  1205. { pointer to the TGUID type
  1206. of all interfaces }
  1207. rec_tguid : trecorddef;
  1208. { jump buffer type, used by setjmp }
  1209. rec_jmp_buf : trecorddef;
  1210. { system.texceptaddr type, used by fpc_pushexceptaddr }
  1211. rec_exceptaddr: trecorddef;
  1212. { Objective-C base types }
  1213. objc_metaclasstype,
  1214. objc_superclasstype,
  1215. objc_idtype,
  1216. objc_seltype : tpointerdef;
  1217. objc_objecttype : trecorddef;
  1218. { base type of @protocol(protocolname) Objective-C statements }
  1219. objc_protocoltype : tobjectdef;
  1220. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  1221. objc_fastenumeration : tobjectdef;
  1222. objc_fastenumerationstate : trecorddef;
  1223. {$ifdef llvm}
  1224. { llvm types }
  1225. { a unique def to identify any kind of metadata }
  1226. llvm_metadatatype : tdef;
  1227. {$endif llvm}
  1228. { Java base types }
  1229. { java.lang.Object }
  1230. java_jlobject : tobjectdef;
  1231. { java.lang.Throwable }
  1232. java_jlthrowable : tobjectdef;
  1233. { FPC base type for records }
  1234. java_fpcbaserecordtype : tobjectdef;
  1235. { java.lang.String }
  1236. java_jlstring : tobjectdef;
  1237. { java.lang.Enum }
  1238. java_jlenum : tobjectdef;
  1239. { java.util.EnumSet }
  1240. java_juenumset : tobjectdef;
  1241. { java.util.BitSet }
  1242. java_jubitset : tobjectdef;
  1243. { FPC java implementation of ansistrings }
  1244. java_ansistring : tobjectdef;
  1245. { FPC java implementation of shortstrings }
  1246. java_shortstring : tobjectdef;
  1247. { FPC java procvar base class }
  1248. java_procvarbase : tobjectdef;
  1249. { x86 vector types }
  1250. x86_m64type,
  1251. x86_m128type,
  1252. x86_m128dtype,
  1253. x86_m128itype,
  1254. x86_m256type,
  1255. x86_m256dtype,
  1256. x86_m256itype : tdef;
  1257. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1258. function make_dllmangledname(const dllname,importname:TSymStr;
  1259. import_nr : word; pco : tproccalloption):TSymStr;
  1260. { should be in the types unit, but the types unit uses the node stuff :( }
  1261. function is_interfacecom(def: tdef): boolean;
  1262. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  1263. function is_any_interface_kind(def: tdef): boolean;
  1264. function is_interfacecorba(def: tdef): boolean;
  1265. function is_interface(def: tdef): boolean;
  1266. function is_dispinterface(def: tdef): boolean;
  1267. function is_object(def: tdef): boolean;
  1268. function is_class(def: tdef): boolean;
  1269. function is_cppclass(def: tdef): boolean;
  1270. function is_objectpascal_helper(def: tdef): boolean;
  1271. function is_objcclass(def: tdef): boolean;
  1272. function is_objcclassref(def: tdef): boolean;
  1273. function is_objcprotocol(def: tdef): boolean;
  1274. function is_objccategory(def: tdef): boolean;
  1275. function is_objc_class_or_protocol(def: tdef): boolean;
  1276. function is_objc_protocol_or_category(def: tdef): boolean;
  1277. function is_classhelper(def: tdef): boolean;
  1278. function is_class_or_interface(def: tdef): boolean;
  1279. function is_class_or_interface_or_objc(def: tdef): boolean;
  1280. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1281. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1282. function is_class_or_interface_or_object(def: tdef): boolean;
  1283. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1284. function is_implicit_pointer_object_type(def: tdef): boolean;
  1285. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1286. function is_implicit_array_pointer(def: tdef): boolean;
  1287. function is_class_or_object(def: tdef): boolean;
  1288. function is_record(def: tdef): boolean;
  1289. function is_javaclass(def: tdef): boolean;
  1290. function is_javaclassref(def: tdef): boolean;
  1291. function is_javainterface(def: tdef): boolean;
  1292. function is_java_class_or_interface(def: tdef): boolean;
  1293. procedure loadobjctypes;
  1294. procedure maybeloadcocoatypes;
  1295. function use_vectorfpu(def : tdef) : boolean;
  1296. function getansistringcodepage:tstringencoding; inline;
  1297. function getansistringdef:tstringdef;
  1298. function getparaencoding(def:tdef):tstringencoding; inline;
  1299. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1300. function get_recorddef(prefix:tinternaltypeprefix;const fields:array of tdef; packrecords:shortint): trecorddef;
  1301. { get a table def of the form
  1302. record
  1303. count: countdef;
  1304. elements: array[0..count-1] of elementdef
  1305. end;
  1306. Returns both the outer record and the inner arraydef
  1307. }
  1308. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1309. function fileinfo_of_typesym_in_def(def:tdef;sym:tsym;out filepos:tfileposinfo):boolean;
  1310. procedure reset_all_default_types; {housekeeping for Textmode IDE sanity}
  1311. implementation
  1312. uses
  1313. SysUtils,
  1314. cutils,
  1315. { global }
  1316. verbose,
  1317. { target }
  1318. systems,paramgr,
  1319. { symtable }
  1320. symsym,symtable,defutil,objcdef,
  1321. { parser }
  1322. pgenutil,
  1323. { module }
  1324. fmodule,ppu,
  1325. { other }
  1326. aasmbase,
  1327. gendef,
  1328. fpchash,
  1329. entfile
  1330. ;
  1331. {****************************************************************************
  1332. Helpers
  1333. ****************************************************************************}
  1334. function fileinfo_of_typesym_in_def(def:tdef;sym:tsym;out filepos:tfileposinfo):boolean;
  1335. var
  1336. gst : tgetsymtable;
  1337. st : tsymtable;
  1338. i : longint;
  1339. srsym : tsym;
  1340. begin
  1341. result:=false;
  1342. if sym.typ<>typesym then
  1343. exit;
  1344. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  1345. begin
  1346. st:=def.getsymtable(gst);
  1347. if not assigned(st) then
  1348. continue;
  1349. for i:=0 to st.symlist.count-1 do
  1350. begin
  1351. srsym:=tsym(st.symlist[i]);
  1352. case srsym.typ of
  1353. fieldvarsym,
  1354. paravarsym:
  1355. if tabstractvarsym(srsym).vardef.typesym=sym then
  1356. begin
  1357. filepos:=srsym.fileinfo;
  1358. result:=true;
  1359. break;
  1360. end;
  1361. typesym:
  1362. begin
  1363. result:=fileinfo_of_typesym_in_def(ttypesym(srsym).typedef,sym,filepos);
  1364. if result then
  1365. break;
  1366. end
  1367. else
  1368. ;
  1369. end;
  1370. end;
  1371. if result then
  1372. break;
  1373. end;
  1374. { for this we would need the position of the result declaration :/ }
  1375. {if not result and
  1376. (def.typ in [procdef,procvardef]) and
  1377. assigned(tabstractprocdef(def).returndef) and
  1378. (tabstractprocdef(def).returndef.typesym=sym) then
  1379. begin
  1380. result:=true;
  1381. end;}
  1382. end;
  1383. function getansistringcodepage:tstringencoding; inline;
  1384. begin
  1385. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1386. result:=current_settings.sourcecodepage
  1387. else
  1388. result:=0;
  1389. end;
  1390. function getansistringdef:tstringdef;
  1391. var
  1392. symtable:tsymtable;
  1393. oldstack : tsymtablestack;
  1394. begin
  1395. { if a codepage is explicitly defined in this mudule we need to return
  1396. a replacement for ansistring def }
  1397. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1398. begin
  1399. if not assigned(current_module) then
  1400. internalerror(2011101301);
  1401. { codepage can be redeclared only once per unit so we don't need a list of
  1402. redefined ansistring but only one pointer }
  1403. if not assigned(current_module.ansistrdef) then
  1404. begin
  1405. { if we did not create it yet we need to do this now }
  1406. if current_module.in_interface then
  1407. symtable:=current_module.globalsymtable
  1408. else
  1409. symtable:=current_module.localsymtable;
  1410. { create a temporary stack as it's not good (TM) to mess around
  1411. with the order if the unit contains generics or helpers; don't
  1412. use a def aware symtablestack though }
  1413. oldstack:=symtablestack;
  1414. symtablestack:=tsymtablestack.create;
  1415. symtablestack.push(symtable);
  1416. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage,true);
  1417. symtablestack.pop(symtable);
  1418. symtablestack.free;
  1419. symtablestack:=oldstack;
  1420. end;
  1421. result:=tstringdef(current_module.ansistrdef);
  1422. end
  1423. else
  1424. result:=tstringdef(cansistringtype);
  1425. end;
  1426. function getparaencoding(def:tdef):tstringencoding; inline;
  1427. begin
  1428. { don't pass CP_NONE encoding to internal functions
  1429. they expect 0 encoding instead
  1430. exception: result of string concatenation, because if you pass the
  1431. result of a string concatenation to a rawbytestring, the result of
  1432. that concatenation shouldn't be converted to defaultsystemcodepage
  1433. if all strings have the same type }
  1434. result:=tstringdef(def).encoding;
  1435. if result=globals.CP_NONE then
  1436. result:=0
  1437. end;
  1438. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1439. var
  1440. typ: ttypesym;
  1441. name: string;
  1442. begin
  1443. name:=internaltypeprefixName[itp_threadvar_record]+def.unique_id_str;
  1444. typ:=try_search_current_module_type(name);
  1445. if assigned(typ) then
  1446. begin
  1447. result:=trecorddef(ttypesym(typ).typedef);
  1448. index_field:=tsym(result.symtable.symlist[0]);
  1449. non_mt_data_field:=tsym(result.symtable.symlist[1]);
  1450. exit;
  1451. end;
  1452. { set recordalinmin to sizeof(pint), so the second field gets put at
  1453. offset = sizeof(pint) as expected }
  1454. result:=crecorddef.create_global_internal(
  1455. name,sizeof(pint),sizeof(pint));
  1456. {$ifdef cpu16bitaddr}
  1457. index_field:=result.add_field_by_def('',u16inttype);
  1458. {$else cpu16bitaddr}
  1459. index_field:=result.add_field_by_def('',u32inttype);
  1460. {$endif cpu16bitaddr}
  1461. non_mt_data_field:=result.add_field_by_def('',def);
  1462. { no need to add alignment padding, we won't create arrays of these }
  1463. end;
  1464. function get_recorddef(prefix:tinternaltypeprefix; const fields:array of tdef; packrecords:shortint): trecorddef;
  1465. var
  1466. fieldlist: tfplist;
  1467. srsym: tsym;
  1468. srsymtable: tsymtable;
  1469. i: longint;
  1470. name : TIDString;
  1471. begin
  1472. name:=copy(internaltypeprefixName[prefix],2,length(internaltypeprefixName[prefix]));
  1473. if searchsym_type(name,srsym,srsymtable) then
  1474. begin
  1475. result:=trecorddef(ttypesym(srsym).typedef);
  1476. exit
  1477. end;
  1478. { also always search in the current module (symtables are popped for
  1479. RTTI related code already) }
  1480. if searchsym_in_module(pointer(current_module),name,srsym,srsymtable) then
  1481. begin
  1482. result:=trecorddef(ttypesym(srsym).typedef);
  1483. exit;
  1484. end;
  1485. fieldlist:=tfplist.create;
  1486. for i:=low(fields) to high(fields) do
  1487. fieldlist.add(fields[i]);
  1488. result:=crecorddef.create_global_internal(internaltypeprefixName[prefix],packrecords,
  1489. targetinfos[target_info.system]^.alignment.recordalignmin);
  1490. result.add_fields_from_deflist(fieldlist);
  1491. fieldlist.free;
  1492. end;
  1493. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1494. var
  1495. fields: tfplist;
  1496. name: TIDString;
  1497. srsym: tsym;
  1498. fieldvarsym: tfieldvarsym;
  1499. srsymtable: tsymtable;
  1500. begin
  1501. { already created a message string table with this number of elements
  1502. in this unit -> reuse the def }
  1503. name:=internaltypeprefixName[prefix]+tostr(count);
  1504. if searchsym_type(copy(name,2,length(name)),srsym,srsymtable) then
  1505. begin
  1506. recdef:=trecorddef(ttypesym(srsym).typedef);
  1507. fieldvarsym:=trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size);
  1508. if fieldvarsym<>nil then
  1509. arrdef:=tarraydef(fieldvarsym.vardef)
  1510. else
  1511. arrdef:=nil;
  1512. exit
  1513. end;
  1514. { also always search in the current module (symtables are popped for
  1515. RTTI related code already) }
  1516. if searchsym_in_module(pointer(current_module),copy(name,2,length(name)),srsym,srsymtable) then
  1517. begin
  1518. recdef:=trecorddef(ttypesym(srsym).typedef);
  1519. fieldvarsym:=trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size);
  1520. if fieldvarsym<>nil then
  1521. arrdef:=tarraydef(fieldvarsym.vardef)
  1522. else
  1523. arrdef:=nil;
  1524. exit;
  1525. end;
  1526. recdef:=crecorddef.create_global_internal(name,packrecords,
  1527. targetinfos[target_info.system]^.alignment.recordalignmin);
  1528. fields:=tfplist.create;
  1529. fields.add(countdef);
  1530. if count>0 then
  1531. begin
  1532. arrdef:=carraydef.create(0,count-1,sizeuinttype);
  1533. arrdef.elementdef:=elementdef;
  1534. fields.add(arrdef);
  1535. end
  1536. else
  1537. arrdef:=nil;
  1538. recdef.add_fields_from_deflist(fields);
  1539. fields.free;
  1540. end;
  1541. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1542. var
  1543. s,t,
  1544. prefix : TSymStr;
  1545. hash : qword;
  1546. begin
  1547. prefix:='';
  1548. if not assigned(st) then
  1549. internalerror(200204212);
  1550. repeat
  1551. { sub procedures }
  1552. while (st.symtabletype in [localsymtable,parasymtable]) do
  1553. begin
  1554. if st.defowner.typ<>procdef then
  1555. internalerror(200204173);
  1556. { Add the full mangledname of the routine to prevent
  1557. conflicts with two overloads both having a local entity
  1558. -- routine (tb0314), class, interface -- with the same name }
  1559. s:=tprocdef(st.defowner).procsym.name;
  1560. s:=s+tprocdef(st.defowner).mangledprocparanames(Length(s));
  1561. if prefix<>'' then
  1562. begin
  1563. if length(prefix)>(maxidlen-length(s)-1) then
  1564. begin
  1565. hash:=InitFnv64;
  1566. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1567. prefix:='$H'+Base64Mangle(hash);
  1568. end;
  1569. prefix:=s+'_'+prefix
  1570. end
  1571. else
  1572. prefix:=s;
  1573. st:=st.defowner.owner;
  1574. end;
  1575. { object/classes symtable, nested type definitions in classes require the while loop }
  1576. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1577. begin
  1578. if not (st.defowner.typ in [objectdef,recorddef]) then
  1579. internalerror(200204174);
  1580. if length(prefix)>(maxidlen-length(tabstractrecorddef(st.defowner).objname^)-3) then
  1581. begin
  1582. hash:=InitFnv64;
  1583. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1584. prefix:='$H'+Base64Mangle(hash);
  1585. end;
  1586. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1587. st:=st.defowner.owner;
  1588. end;
  1589. { local classes & interfaces are possible (because of closures)
  1590. or parasymtable for this case:
  1591. class function Trace<TResult>(const Func: TFunc<TLogToken, TResult>): TResult;
  1592. }
  1593. if not (st.symtabletype in [localsymtable,parasymtable]) then
  1594. break;
  1595. prefix:='$'+prefix;
  1596. until false;
  1597. { symtable must now be static or global }
  1598. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1599. internalerror(200204175);
  1600. { The mangled name is made out of at most 4 parts:
  1601. 1) Optional typeprefix given as first parameter
  1602. with '_$' appended if not empty
  1603. 2) Unit name or 'P$'+program name (never empty)
  1604. 3) optional prefix variable that contains a unique
  1605. name for the local symbol table (prepended with '$_$'
  1606. if not empty)
  1607. 4) suffix as given as third parameter,
  1608. also optional (i.e. can be empty)
  1609. prepended by '_$$_' if not empty }
  1610. result:='';
  1611. if typeprefix<>'' then
  1612. result:=result+typeprefix+'_$';
  1613. { Add P$ for program, which can have the same name as
  1614. a unit }
  1615. if (TSymtable(main_module.localsymtable)=st) and
  1616. (not main_module.is_unit) then
  1617. result:=result+'P$'+st.name^
  1618. else
  1619. result:=result+st.name^;
  1620. if prefix<>'' then
  1621. result:=result+'$_$'+prefix;
  1622. if suffix<>'' then
  1623. result:=result+'_$$_'+suffix;
  1624. if length(result)>(maxidlen-1) then
  1625. begin
  1626. hash:=InitFnv64;
  1627. hash:=UpdateFnv64(hash,result[1],length(result));
  1628. result:=copy(result,1,maxidlen-(high(Base64OfUint64String)-low(Base64OfUint64String)+1)-2)+'$H'+Base64Mangle(hash);
  1629. end;
  1630. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1631. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1632. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1633. { those in the debug map, leading to troubles with dsymutil). So always }
  1634. { add an underscore on darwin. }
  1635. if (target_info.system in systems_darwin) then
  1636. result := '_' + result;
  1637. end;
  1638. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1639. var
  1640. crc : cardinal;
  1641. i : longint;
  1642. use_crc : boolean;
  1643. dllprefix : TSymStr;
  1644. begin
  1645. if (target_info.system in (systems_all_windows + systems_nativent +
  1646. [system_i386_emx, system_i386_os2]))
  1647. and (dllname <> '') then
  1648. begin
  1649. dllprefix:=lower(ExtractFileName(dllname));
  1650. { Remove .dll suffix if present }
  1651. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1652. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1653. use_crc:=false;
  1654. for i:=1 to length(dllprefix) do
  1655. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1656. begin
  1657. use_crc:=true;
  1658. break;
  1659. end;
  1660. if use_crc then
  1661. begin
  1662. crc:=0;
  1663. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1664. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1665. end
  1666. else
  1667. dllprefix:='_$dll$'+dllprefix+'$';
  1668. if importname<>'' then
  1669. result:=dllprefix+importname
  1670. else
  1671. result:=dllprefix+'_index_'+tostr(import_nr);
  1672. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1673. { This allows to import VC++ mangled names from DLLs. }
  1674. { Do not perform replacement, if external symbol is not imported from DLL. }
  1675. if (dllname<>'') then
  1676. begin
  1677. Replace(result,'?','__q$$');
  1678. {$ifdef arm}
  1679. { @ symbol is not allowed in ARM assembler only }
  1680. Replace(result,'@','__a$$');
  1681. {$endif arm}
  1682. end;
  1683. end
  1684. else
  1685. begin
  1686. if importname<>'' then
  1687. begin
  1688. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1689. result:=importname
  1690. else
  1691. result:=target_info.Cprefix+importname;
  1692. end
  1693. else
  1694. result:='_index_'+tostr(import_nr);
  1695. end;
  1696. end;
  1697. {****************************************************************************
  1698. TDEFAWARESYMTABLESTACK
  1699. (symtablestack descendant that does some special actions on
  1700. the pushed/popped symtables)
  1701. ****************************************************************************}
  1702. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1703. var
  1704. s: TSymStr;
  1705. list: TFPObjectList;
  1706. def: tdef;
  1707. sym : tsym;
  1708. i: integer;
  1709. begin
  1710. { search the symtable from first to last; the helper to use will be the
  1711. last one in the list }
  1712. for i:=0 to st.symlist.count-1 do
  1713. begin
  1714. sym:=tsym(st.symlist[i]);
  1715. if not (sym.typ in [typesym,procsym]) then
  1716. continue;
  1717. if sym.typ=typesym then
  1718. def:=ttypesym(st.SymList[i]).typedef
  1719. else
  1720. def:=nil;
  1721. if is_objectpascal_helper(def) then
  1722. begin
  1723. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1724. Message1(sym_d_adding_helper_for,s);
  1725. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1726. if not assigned(list) then
  1727. begin
  1728. list:=TFPObjectList.Create(false);
  1729. current_module.extendeddefs.Add(s,list);
  1730. end;
  1731. list.Add(def);
  1732. end
  1733. else
  1734. begin
  1735. if addgenerics then
  1736. add_generic_dummysym(sym);
  1737. { add nested helpers as well }
  1738. if assigned(def) and
  1739. (def.typ in [recorddef,objectdef]) and
  1740. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) and
  1741. not is_owned_by(def,tdef(st.defowner)) then
  1742. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1743. end;
  1744. end;
  1745. end;
  1746. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1747. begin
  1748. if sto_has_helper in st.tableoptions then
  1749. remove_helpers(st);
  1750. if sto_has_generic in st.tableoptions then
  1751. remove_generics(st);
  1752. end;
  1753. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1754. var
  1755. i, j: integer;
  1756. tmpst: TSymtable;
  1757. list: TFPObjectList;
  1758. begin
  1759. for i:=current_module.extendeddefs.count-1 downto 0 do
  1760. begin
  1761. list:=TFPObjectList(current_module.extendeddefs[i]);
  1762. for j:=list.count-1 downto 0 do
  1763. begin
  1764. if not (list[j] is tobjectdef) then
  1765. Internalerror(2011031501);
  1766. tmpst:=tobjectdef(list[j]).owner;
  1767. repeat
  1768. if tmpst=st then
  1769. begin
  1770. list.delete(j);
  1771. break;
  1772. end
  1773. else
  1774. begin
  1775. if assigned(tmpst.defowner) then
  1776. tmpst:=tmpst.defowner.owner
  1777. else
  1778. tmpst:=nil;
  1779. end;
  1780. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1781. end;
  1782. if list.count=0 then
  1783. current_module.extendeddefs.delete(i);
  1784. end;
  1785. end;
  1786. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1787. var
  1788. i,j : longint;
  1789. entry : tgenericdummyentry;
  1790. list : tfpobjectlist;
  1791. begin
  1792. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1793. begin
  1794. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1795. if not assigned(list) then
  1796. continue;
  1797. for j:=list.count-1 downto 0 do
  1798. begin
  1799. entry:=tgenericdummyentry(list[j]);
  1800. if entry.dummysym.owner=st then
  1801. list.delete(j);
  1802. end;
  1803. if list.count=0 then
  1804. current_module.genericdummysyms.delete(i);
  1805. end;
  1806. end;
  1807. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1808. begin
  1809. if (sto_has_generic in st.tableoptions) or
  1810. (
  1811. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1812. (sto_has_helper in st.tableoptions)
  1813. ) then
  1814. { nested helpers will be added as well }
  1815. add_helpers_and_generics(st,true);
  1816. end;
  1817. procedure tdefawaresymtablestack.push(st: TSymtable);
  1818. begin
  1819. pushcommon(st);
  1820. inherited push(st);
  1821. end;
  1822. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1823. begin
  1824. pushcommon(st);
  1825. inherited pushafter(st,afterst);
  1826. end;
  1827. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1828. begin
  1829. inherited pop(st);
  1830. if (sto_has_generic in st.tableoptions) or
  1831. (
  1832. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1833. (sto_has_helper in st.tableoptions)
  1834. ) then
  1835. { nested helpers will be removed as well }
  1836. remove_helpers_and_generics(st);
  1837. end;
  1838. {****************************************************************************
  1839. TDEF (base class for definitions)
  1840. ****************************************************************************}
  1841. constructor tgenericconstraintdata.create;
  1842. begin
  1843. interfaces:=tfpobjectlist.create(false);
  1844. interfacesderef:=tfplist.create;
  1845. end;
  1846. destructor tgenericconstraintdata.destroy;
  1847. begin
  1848. TFPList.FreeAndNilDisposing(interfacesderef,typeinfo(tderef));
  1849. interfaces.free;
  1850. inherited destroy;
  1851. end;
  1852. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1853. var
  1854. cnt,i : longint;
  1855. intfderef : pderef;
  1856. begin
  1857. ppufile.getset(tppuset1(flags));
  1858. cnt:=ppufile.getlongint;
  1859. for i:=0 to cnt-1 do
  1860. begin
  1861. new(intfderef);
  1862. ppufile.getderef(intfderef^);
  1863. interfacesderef.add(intfderef);
  1864. end;
  1865. end;
  1866. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1867. var
  1868. i : longint;
  1869. begin
  1870. ppufile.putset(tppuset1(flags));
  1871. ppufile.putlongint(interfacesderef.count);
  1872. for i:=0 to interfacesderef.count-1 do
  1873. ppufile.putderef(pderef(interfacesderef[i])^);
  1874. end;
  1875. procedure tgenericconstraintdata.buildderef;
  1876. var
  1877. intfderef : pderef;
  1878. i : longint;
  1879. begin
  1880. interfacesderef.capacity:=interfacesderef.count+interfaces.count;
  1881. for i:=0 to interfaces.count-1 do
  1882. begin
  1883. new(intfderef);
  1884. intfderef^.build(tobjectdef(interfaces[i]));
  1885. interfacesderef.add(intfderef);
  1886. end;
  1887. end;
  1888. procedure tgenericconstraintdata.deref;
  1889. var
  1890. i : longint;
  1891. begin
  1892. interfaces.capacity:=interfaces.count+interfacesderef.count;
  1893. for i:=0 to interfacesderef.count-1 do
  1894. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1895. end;
  1896. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1897. begin
  1898. ppuwrite_platform(ppufile);
  1899. ppufile.writeentry(ibnr);
  1900. end;
  1901. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1902. begin
  1903. { by default: do nothing }
  1904. end;
  1905. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1906. begin
  1907. { by default: do nothing }
  1908. end;
  1909. class procedure tstoreddef.setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  1910. var
  1911. reusablesymtab: tsymtable;
  1912. begin
  1913. { must not yet belong to a symtable }
  1914. if assigned(newdef.owner) then
  1915. internalerror(2015111503);
  1916. reusablesymtab:=origdef.getreusablesymtab;
  1917. res^.Data:=newdef;
  1918. reusablesymtab.insertdef(newdef);
  1919. symtablestack:=oldsymtablestack;
  1920. end;
  1921. constructor tstoreddef.create(dt:tdeftyp;doregister:boolean);
  1922. begin
  1923. inherited create(dt);
  1924. savesize := 0;
  1925. {$ifdef EXTDEBUG}
  1926. fileinfo := current_filepos;
  1927. {$endif}
  1928. generictokenbuf:=nil;
  1929. genericdef:=nil;
  1930. typesymderef.reset;
  1931. genericdefderef.reset;
  1932. { Don't register forwarddefs, they are disposed at the
  1933. end of an type block }
  1934. if (dt=forwarddef) then
  1935. exit;
  1936. { Register in symtable stack }
  1937. if doregister then
  1938. begin
  1939. { immediately register interface defs, as they will always be
  1940. written to the ppu, their defid influences the interface crc and
  1941. if we wait, depending on e.g. compiler defines they may get a
  1942. different defid (e.g. when a function is called, its procdef is
  1943. registered, so depending on whether or not, or when, an interface
  1944. procedure is called in the implementation, that may change its
  1945. defid otherwise) }
  1946. if assigned(current_module) and
  1947. current_module.in_interface then
  1948. register_def
  1949. else
  1950. maybe_put_in_symtable_stack;
  1951. end;
  1952. end;
  1953. destructor tstoreddef.destroy;
  1954. begin
  1955. { Direct calls are not allowed, use symtable.deletedef() }
  1956. if assigned(owner) then
  1957. internalerror(200612311);
  1958. if assigned(generictokenbuf) then
  1959. begin
  1960. generictokenbuf.free;
  1961. generictokenbuf:=nil;
  1962. end;
  1963. rtti_attribute_list.free;
  1964. genericparas.free;
  1965. TFPList.FreeAndNilDisposing(genericparaderefs,typeinfo(tderef));
  1966. genconstraintdata.free;
  1967. {$ifndef symansistr}
  1968. stringdispose(_fullownerhierarchyname);
  1969. {$endif not symansistr}
  1970. inherited destroy;
  1971. end;
  1972. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1973. var
  1974. sizeleft,i,cnt : longint;
  1975. buf : array[0..255] of byte;
  1976. symderef : pderef;
  1977. begin
  1978. inherited create(dt);
  1979. DefId:=ppufile.getlongint;
  1980. current_module.deflist[DefId]:=self;
  1981. {$ifdef EXTDEBUG}
  1982. fillchar(fileinfo,sizeof(fileinfo),0);
  1983. {$endif}
  1984. { load }
  1985. ppufile.getderef(typesymderef);
  1986. ppufile.getset(tppuset2(defoptions));
  1987. ppufile.getset(tppuset1(defstates));
  1988. if df_unique in defoptions then
  1989. ppufile.getderef(orgdefderef);
  1990. if df_genconstraint in defoptions then
  1991. begin
  1992. genconstraintdata:=tgenericconstraintdata.create;
  1993. genconstraintdata.ppuload(ppufile);
  1994. end;
  1995. if [df_generic,df_specialization]*defoptions<>[] then
  1996. begin
  1997. cnt:=ppufile.getlongint;
  1998. if cnt>0 then
  1999. begin
  2000. genericparas:=tfphashobjectlist.create(false);
  2001. genericparaderefs:=tfplist.create;
  2002. for i:=0 to cnt-1 do
  2003. begin
  2004. genericparas.add(ppufile.getstring,nil);
  2005. New(symderef);
  2006. ppufile.getderef(symderef^);
  2007. genericparaderefs.add(symderef);
  2008. end;
  2009. end;
  2010. end;
  2011. if df_generic in defoptions then
  2012. begin
  2013. sizeleft:=ppufile.getlongint;
  2014. initgeneric;
  2015. while sizeleft>0 do
  2016. begin
  2017. if sizeleft>sizeof(buf) then
  2018. i:=sizeof(buf)
  2019. else
  2020. i:=sizeleft;
  2021. ppufile.getdata(buf,i);
  2022. generictokenbuf.write(buf,i);
  2023. dec(sizeleft,i);
  2024. end;
  2025. end;
  2026. if df_specialization in defoptions then
  2027. ppufile.getderef(genericdefderef);
  2028. rtti_attribute_list:=trtti_attribute_list.ppuload(ppufile);
  2029. end;
  2030. function tstoreddef.needs_separate_initrtti:boolean;
  2031. begin
  2032. result:=false;
  2033. end;
  2034. function tstoreddef.rtti_mangledname(rt : trttitype) : TSymStr;
  2035. var
  2036. prefix : string[4];
  2037. begin
  2038. if (rt=fullrtti) or (not needs_separate_initrtti) then
  2039. begin
  2040. prefix:='RTTI';
  2041. include(defstates,ds_rtti_table_used);
  2042. end
  2043. else
  2044. begin
  2045. prefix:='INIT';
  2046. include(defstates,ds_init_table_used);
  2047. end;
  2048. if assigned(typesym) and
  2049. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  2050. result:=make_mangledname(prefix,typesym.owner,typesym.name)
  2051. else
  2052. result:=make_mangledname(prefix,findunitsymtable(owner),'def'+unique_id_str)
  2053. end;
  2054. function tstoreddef.OwnerHierarchyName: string;
  2055. var
  2056. tmp: tdef;
  2057. begin
  2058. tmp:=self;
  2059. result:='';
  2060. repeat
  2061. { can be not assigned in case of a forwarddef }
  2062. if assigned(tmp.owner) and
  2063. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2064. tmp:=tdef(tmp.owner.defowner)
  2065. else
  2066. break;
  2067. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  2068. until tmp=nil;
  2069. end;
  2070. function tstoreddef.OwnerHierarchyPrettyName: string;
  2071. var
  2072. tmp: tdef;
  2073. begin
  2074. tmp:=self;
  2075. result:='';
  2076. repeat
  2077. { can be not assigned in case of a forwarddef }
  2078. if assigned(tmp.owner) and
  2079. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2080. tmp:=tdef(tmp.owner.defowner)
  2081. else
  2082. break;
  2083. result:=tabstractrecorddef(tmp).typesymbolprettyname+'.'+result;
  2084. until tmp=nil;
  2085. end;
  2086. function tstoreddef.fullownerhierarchyname(skipprocparams:boolean): TSymStr;
  2087. var
  2088. lastowner: tsymtable;
  2089. tmp: tdef;
  2090. pno: tprocnameoptions;
  2091. begin
  2092. {$ifdef symansistr}
  2093. if not skipprocparams and (_fullownerhierarchyname<>'') then
  2094. exit(_fullownerhierarchyname);
  2095. {$else symansistr}
  2096. if not skipprocparams and assigned(_fullownerhierarchyname) then
  2097. exit(_fullownerhierarchyname^);
  2098. {$endif symansistr}
  2099. { the def can only reside inside structured types or
  2100. procedures/functions/methods }
  2101. tmp:=self;
  2102. result:='';
  2103. repeat
  2104. lastowner:=tmp.owner;
  2105. { can be not assigned in case of a forwarddef }
  2106. if not assigned(lastowner) then
  2107. break
  2108. else
  2109. tmp:=tdef(lastowner.defowner);
  2110. if not assigned(tmp) then
  2111. break;
  2112. if tmp.typ in [recorddef,objectdef] then
  2113. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  2114. else
  2115. if tmp.typ=procdef then
  2116. begin
  2117. pno:=[pno_paranames,pno_proctypeoption];
  2118. if skipprocparams then
  2119. include(pno,pno_noparams);
  2120. result:=tprocdef(tmp).customprocname(pno)+'.'+result;
  2121. end;
  2122. until tmp=nil;
  2123. { add the unit name }
  2124. if assigned(lastowner) and
  2125. assigned(lastowner.realname) then
  2126. result:=lastowner.realname^+'.'+result;
  2127. if not skipprocparams then
  2128. { don't store the name in this case }
  2129. {$ifdef symansistr}
  2130. _fullownerhierarchyname:=result;
  2131. {$else symansistr}
  2132. _fullownerhierarchyname:=stringdup(result);
  2133. {$endif symansistr}
  2134. end;
  2135. function tstoreddef.in_currentunit: boolean;
  2136. var
  2137. st: tsymtable;
  2138. begin
  2139. st:=owner;
  2140. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  2141. st:=st.defowner.owner;
  2142. result:=st.iscurrentunit;
  2143. end;
  2144. function tstoreddef.getcopy : tstoreddef;
  2145. begin
  2146. Message(sym_e_cant_create_unique_type);
  2147. getcopy:=cerrordef.create;
  2148. end;
  2149. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  2150. var
  2151. sizeleft,i : longint;
  2152. buf : array[0..255] of byte;
  2153. oldintfcrc : boolean;
  2154. begin
  2155. if defid<0 then
  2156. internalerror(2015101401);
  2157. ppufile.putlongint(DefId);
  2158. ppufile.putderef(typesymderef);
  2159. ppufile.putset(tppuset2(defoptions));
  2160. oldintfcrc:=ppufile.do_crc;
  2161. ppufile.do_crc:=false;
  2162. ppufile.putset(tppuset1(defstates));
  2163. if df_unique in defoptions then
  2164. ppufile.putderef(orgdefderef);
  2165. if df_genconstraint in defoptions then
  2166. genconstraintdata.ppuwrite(ppufile);
  2167. if [df_generic,df_specialization]*defoptions<>[] then
  2168. begin
  2169. if not assigned(genericparas) then
  2170. ppufile.putlongint(0)
  2171. else
  2172. begin
  2173. if not assigned(genericparaderefs) then
  2174. internalerror(2014052305);
  2175. ppufile.putlongint(genericparas.count);
  2176. for i:=0 to genericparas.count-1 do
  2177. begin
  2178. ppufile.putstring(genericparas.nameofindex(i));
  2179. ppufile.putderef(pderef(genericparaderefs[i])^);
  2180. end;
  2181. end;
  2182. end;
  2183. if df_generic in defoptions then
  2184. begin
  2185. if assigned(generictokenbuf) then
  2186. begin
  2187. sizeleft:=generictokenbuf.size;
  2188. generictokenbuf.seek(0);
  2189. end
  2190. else
  2191. sizeleft:=0;
  2192. ppufile.putlongint(sizeleft);
  2193. while sizeleft>0 do
  2194. begin
  2195. if sizeleft>sizeof(buf) then
  2196. i:=sizeof(buf)
  2197. else
  2198. i:=sizeleft;
  2199. generictokenbuf.read(buf,i);
  2200. ppufile.putdata(buf,i);
  2201. dec(sizeleft,i);
  2202. end;
  2203. end;
  2204. ppufile.do_crc:=oldintfcrc;
  2205. if df_specialization in defoptions then
  2206. ppufile.putderef(genericdefderef);
  2207. trtti_attribute_list.ppuwrite(rtti_attribute_list,ppufile);
  2208. end;
  2209. procedure tstoreddef.ppuload_subentries(ppufile: tcompilerppufile);
  2210. begin
  2211. trtti_attribute_list.ppuload_subentries(rtti_attribute_list,ppufile);
  2212. end;
  2213. procedure tstoreddef.ppuwrite_subentries(ppufile: tcompilerppufile);
  2214. begin
  2215. trtti_attribute_list.ppuwrite_subentries(rtti_attribute_list,ppufile);
  2216. end;
  2217. procedure tstoreddef.buildderef;
  2218. var
  2219. i : longint;
  2220. sym : tsym;
  2221. symderef : pderef;
  2222. begin
  2223. if not registered then
  2224. register_def;
  2225. typesymderef.build(typesym);
  2226. orgdefderef.build(orgdef);
  2227. genericdefderef.build(genericdef);
  2228. if assigned(rtti_attribute_list) then
  2229. rtti_attribute_list.buildderef;
  2230. if assigned(genconstraintdata) then
  2231. genconstraintdata.buildderef;
  2232. if assigned(genericparas) then
  2233. begin
  2234. if not assigned(genericparaderefs) then
  2235. genericparaderefs:=tfplist.create;
  2236. genericparaderefs.capacity:=genericparaderefs.count+genericparas.count;
  2237. for i:=0 to genericparas.count-1 do
  2238. begin
  2239. sym:=tsym(genericparas.items[i]);
  2240. new(symderef);
  2241. symderef^.build(sym);
  2242. genericparaderefs.add(symderef);
  2243. end;
  2244. end;
  2245. end;
  2246. procedure tstoreddef.buildderefimpl;
  2247. begin
  2248. end;
  2249. procedure tstoreddef.deref;
  2250. var
  2251. symderef : pderef;
  2252. i : longint;
  2253. begin
  2254. typesym:=ttypesym(typesymderef.resolve);
  2255. if df_unique in defoptions then
  2256. orgdef:=tstoreddef(orgdefderef.resolve);
  2257. if df_specialization in defoptions then
  2258. genericdef:=tstoreddef(genericdefderef.resolve);
  2259. if assigned(rtti_attribute_list) then
  2260. rtti_attribute_list.deref;
  2261. if assigned(genconstraintdata) then
  2262. genconstraintdata.deref;
  2263. if assigned(genericparas) then
  2264. begin
  2265. if not assigned(genericparaderefs) then
  2266. internalerror(2014052302);
  2267. if genericparas.count<>genericparaderefs.count then
  2268. internalerror(2014052303);
  2269. for i:=0 to genericparaderefs.count-1 do
  2270. begin
  2271. symderef:=pderef(genericparaderefs[i]);
  2272. genericparas.items[i]:=symderef^.resolve;
  2273. end;
  2274. end;
  2275. end;
  2276. procedure tstoreddef.derefimpl;
  2277. begin
  2278. end;
  2279. function tstoreddef.size : asizeint;
  2280. begin
  2281. size:=savesize;
  2282. end;
  2283. function tstoreddef.getvardef:longint;
  2284. begin
  2285. result:=varUndefined;
  2286. end;
  2287. function tstoreddef.alignment : shortint;
  2288. begin
  2289. { natural alignment by default }
  2290. alignment:=size_2_align(savesize);
  2291. { can happen if savesize = 0, e.g. for voiddef or
  2292. an empty record
  2293. }
  2294. if (alignment=0) then
  2295. alignment:=1;
  2296. end;
  2297. { returns true, if the definition can be published }
  2298. function tstoreddef.is_publishable : tpublishproperty;
  2299. begin
  2300. is_publishable:=pp_error;
  2301. end;
  2302. { needs an init table }
  2303. function tstoreddef.needs_inittable : boolean;
  2304. begin
  2305. needs_inittable:=false;
  2306. end;
  2307. function tstoreddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  2308. begin
  2309. result:=false;
  2310. end;
  2311. function tstoreddef.is_intregable : boolean;
  2312. {$ifndef cpuhighleveltarget}
  2313. var
  2314. recsize,temp: longint;
  2315. {$endif cpuhighleveltarget}
  2316. begin
  2317. case typ of
  2318. orddef,
  2319. pointerdef,
  2320. enumdef,
  2321. classrefdef:
  2322. is_intregable:=true;
  2323. procvardef :
  2324. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  2325. objectdef:
  2326. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  2327. setdef:
  2328. is_intregable:=is_smallset(self);
  2329. arraydef:
  2330. {$ifdef cpuhighleveltarget}
  2331. is_intregable:=false
  2332. {$else cpuhighleveltarget}
  2333. is_intregable:=not(is_special_array(self)) and
  2334. (tarraydef(self).size<=8) and (tarraydef(self).size in [1,2,4,8]) and
  2335. tstoreddef(tarraydef(self).elementdef).is_intregable
  2336. {$ifdef SUPPORT_MMX}
  2337. and not((cs_mmx in current_settings.localswitches) and
  2338. is_mmx_able_array(self))
  2339. {$endif SUPPORT_MMX}
  2340. {$endif cpuhighleveltarget}
  2341. ;
  2342. recorddef:
  2343. begin
  2344. {$ifdef cpuhighleveltarget}
  2345. is_intregable:=false;
  2346. {$else cpuhighleveltarget}
  2347. recsize:=size;
  2348. is_intregable:=
  2349. ispowerof2(recsize,temp) and
  2350. ((recsize<=sizeof(aint)*2) and
  2351. not trecorddef(self).contains_cross_aword_field and
  2352. { records cannot go into registers on 16 bit targets for now }
  2353. (sizeof(aint)>2) and
  2354. not trecorddef(self).contains_float_field
  2355. ) and
  2356. not needs_inittable;
  2357. {$endif cpuhighleveltarget}
  2358. end;
  2359. else
  2360. is_intregable:=false;
  2361. end;
  2362. end;
  2363. function tstoreddef.is_fpuregable : boolean;
  2364. begin
  2365. {$ifdef x86}
  2366. result:=use_vectorfpu(self);
  2367. {$else x86}
  2368. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches)
  2369. {$ifdef xtensa}
  2370. and (FPUXTENSA_SINGLE in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)
  2371. {$endif xtensa}
  2372. {$ifdef arm}
  2373. and (((FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)) or
  2374. (FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[init_settings.fputype]))
  2375. {$endif arm}
  2376. ;
  2377. {$endif x86}
  2378. end;
  2379. function tstoreddef.is_const_intregable : boolean;
  2380. begin
  2381. case typ of
  2382. stringdef:
  2383. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  2384. arraydef:
  2385. result:=is_dynamic_array(self);
  2386. objectdef:
  2387. result:=is_interface(self);
  2388. else
  2389. result:=false;
  2390. end;
  2391. end;
  2392. procedure tstoreddef.initgeneric;
  2393. begin
  2394. if assigned(generictokenbuf) then
  2395. internalerror(200512131);
  2396. generictokenbuf:=tdynamicarray.create(256);
  2397. end;
  2398. function tstoreddef.is_generic: boolean;
  2399. var
  2400. sym: tsym;
  2401. i: longint;
  2402. begin
  2403. result:=assigned(genericparas) and
  2404. (genericparas.count>0) and
  2405. (df_generic in defoptions);
  2406. if result then
  2407. { if any of the type parameters does *not* belong to us (meaning it was passed
  2408. in from outside) then we aren't a generic, but a specialization }
  2409. for i:=0 to genericparas.count-1 do
  2410. begin
  2411. sym:=tsym(genericparas[i]);
  2412. { sym must be either a type or const }
  2413. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2414. internalerror(2014050903);
  2415. if (sym.owner.defowner<>self) or (sp_generic_unnamed_type in sym.symoptions) then
  2416. exit(false);
  2417. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2418. exit(false);
  2419. end;
  2420. end;
  2421. function tstoreddef.is_generic_param_const(index:integer):boolean;
  2422. begin
  2423. result:=tsym(genericparas[index]).typ=constsym;
  2424. end;
  2425. function tstoreddef.get_generic_param_def(index:integer):tdef;
  2426. begin
  2427. if tsym(genericparas[index]).typ=constsym then
  2428. result:=tconstsym(genericparas[index]).constdef
  2429. else
  2430. result:=ttypesym(genericparas[index]).typedef;
  2431. end;
  2432. function tstoreddef.is_specialization: boolean;
  2433. var
  2434. i : longint;
  2435. sym : tsym;
  2436. begin
  2437. result:=assigned(genericparas) and
  2438. (genericparas.count>0) and
  2439. (df_specialization in defoptions);
  2440. if result then
  2441. begin
  2442. { if at least one of the generic parameters is not owned by us (meaning it was
  2443. passed in from outside) then we have a specialization, otherwise we have a generic }
  2444. for i:=0 to genericparas.count-1 do
  2445. begin
  2446. sym:=tsym(genericparas[i]);
  2447. { sym must be either a type or const }
  2448. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2449. internalerror(2014050904);
  2450. if (sym.owner.defowner<>self) or (sp_generic_unnamed_type in sym.symoptions) then
  2451. exit(true);
  2452. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2453. exit(true);
  2454. end;
  2455. result:=false;
  2456. end;
  2457. end;
  2458. procedure tstoreddef.register_def;
  2459. var
  2460. gst : tgetsymtable;
  2461. st : tsymtable;
  2462. begin
  2463. if registered then
  2464. exit;
  2465. { Register in current_module }
  2466. if assigned(current_module) then
  2467. begin
  2468. exclude(defoptions,df_not_registered_no_free);
  2469. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  2470. begin
  2471. st:=getsymtable(gst);
  2472. if assigned(st) then
  2473. tstoredsymtable(st).register_children;
  2474. end;
  2475. if defid<defid_not_registered then
  2476. defid:=deflist_index
  2477. else
  2478. begin
  2479. current_module.deflist.Add(self);
  2480. defid:=current_module.deflist.Count-1;
  2481. registered_in_module:=current_module;
  2482. end;
  2483. maybe_put_in_symtable_stack;
  2484. end
  2485. else
  2486. DefId:=defid_registered_nost;
  2487. end;
  2488. procedure tstoreddef.maybe_put_in_symtable_stack;
  2489. var
  2490. insertstack: psymtablestackitem;
  2491. begin
  2492. if assigned(symtablestack) and
  2493. not assigned(self.owner) then
  2494. begin
  2495. insertstack:=symtablestack.stack;
  2496. { don't insert defs in exception symtables, as they are freed before
  2497. the module is compiled, so we can get crashes on high level targets
  2498. if they still need it while e.g. writing assembler code }
  2499. while assigned(insertstack) and
  2500. (insertstack^.symtable.symtabletype in [exceptsymtable,withsymtable]) do
  2501. insertstack:=insertstack^.next;
  2502. if not assigned(insertstack) then
  2503. internalerror(200602044);
  2504. if insertstack^.symtable.sealed then
  2505. internalerror(2015022301);
  2506. insertstack^.symtable.insertdef(self);
  2507. end;
  2508. end;
  2509. {****************************************************************************
  2510. Tstringdef
  2511. ****************************************************************************}
  2512. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2513. begin
  2514. inherited create(stringdef,doregister);
  2515. stringtype:=st_shortstring;
  2516. encoding:=0;
  2517. len:=l;
  2518. end;
  2519. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2520. begin
  2521. inherited ppuload(stringdef,ppufile);
  2522. stringtype:=st_shortstring;
  2523. encoding:=0;
  2524. len:=ppufile.getbyte;
  2525. ppuload_platform(ppufile);
  2526. end;
  2527. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2528. begin
  2529. inherited create(stringdef,doregister);
  2530. stringtype:=st_longstring;
  2531. encoding:=0;
  2532. len:=l;
  2533. end;
  2534. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2535. begin
  2536. inherited ppuload(stringdef,ppufile);
  2537. stringtype:=st_longstring;
  2538. encoding:=0;
  2539. len:=ppufile.getasizeint;
  2540. ppuload_platform(ppufile);
  2541. end;
  2542. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2543. begin
  2544. inherited create(stringdef,doregister);
  2545. stringtype:=st_ansistring;
  2546. encoding:=aencoding;
  2547. len:=-1;
  2548. end;
  2549. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2550. begin
  2551. inherited ppuload(stringdef,ppufile);
  2552. stringtype:=st_ansistring;
  2553. len:=ppufile.getasizeint;
  2554. encoding:=ppufile.getword;
  2555. ppuload_platform(ppufile);
  2556. end;
  2557. constructor tstringdef.createwide(doregister: boolean);
  2558. begin
  2559. inherited create(stringdef,doregister);
  2560. stringtype:=st_widestring;
  2561. if target_info.endian=endian_little then
  2562. encoding:=CP_UTF16LE
  2563. else
  2564. encoding:=CP_UTF16BE;
  2565. len:=-1;
  2566. end;
  2567. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2568. begin
  2569. inherited ppuload(stringdef,ppufile);
  2570. stringtype:=st_widestring;
  2571. if target_info.endian=endian_little then
  2572. encoding:=CP_UTF16LE
  2573. else
  2574. encoding:=CP_UTF16BE;
  2575. len:=ppufile.getasizeint;
  2576. ppuload_platform(ppufile);
  2577. end;
  2578. constructor tstringdef.createunicode(doregister: boolean);
  2579. begin
  2580. inherited create(stringdef,doregister);
  2581. stringtype:=st_unicodestring;
  2582. if target_info.endian=endian_little then
  2583. encoding:=CP_UTF16LE
  2584. else
  2585. encoding:=CP_UTF16BE;
  2586. len:=-1;
  2587. end;
  2588. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2589. begin
  2590. inherited ppuload(stringdef,ppufile);
  2591. stringtype:=st_unicodestring;
  2592. len:=ppufile.getasizeint;
  2593. encoding:=ppufile.getword;
  2594. ppuload_platform(ppufile);
  2595. end;
  2596. function tstringdef.getcopy : tstoreddef;
  2597. begin
  2598. result:=cstringdef.create(typ,true);
  2599. result.typ:=stringdef;
  2600. tstringdef(result).stringtype:=stringtype;
  2601. tstringdef(result).encoding:=encoding;
  2602. tstringdef(result).len:=len;
  2603. end;
  2604. function tstringdef.stringtypname:string;
  2605. const
  2606. typname:array[tstringtype] of string[10]=(
  2607. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2608. );
  2609. begin
  2610. stringtypname:=typname[stringtype];
  2611. end;
  2612. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2613. begin
  2614. inherited ppuwrite(ppufile);
  2615. if stringtype=st_shortstring then
  2616. begin
  2617. {$ifdef extdebug}
  2618. if len > 255 then internalerror(12122002);
  2619. {$endif}
  2620. ppufile.putbyte(byte(len))
  2621. end
  2622. else
  2623. ppufile.putasizeint(len);
  2624. if stringtype in [st_ansistring,st_unicodestring] then
  2625. ppufile.putword(encoding);
  2626. case stringtype of
  2627. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2628. st_longstring : writeentry(ppufile,iblongstringdef);
  2629. st_ansistring : writeentry(ppufile,ibansistringdef);
  2630. st_widestring : writeentry(ppufile,ibwidestringdef);
  2631. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2632. end;
  2633. end;
  2634. function tstringdef.needs_inittable : boolean;
  2635. begin
  2636. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2637. end;
  2638. function tstringdef.GetTypeName : string;
  2639. const
  2640. names : array[tstringtype] of string[15] = (
  2641. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2642. begin
  2643. GetTypeName:=names[stringtype];
  2644. case stringtype of
  2645. st_ansistring,st_unicodestring:
  2646. GetTypeName:=GetTypeName+'('+tostr(encoding)+')';
  2647. st_shortstring:
  2648. GetTypeName:=GetTypeName+'['+tostr(len)+']';
  2649. else
  2650. ;
  2651. end;
  2652. end;
  2653. function tstringdef.getvardef : longint;
  2654. const
  2655. vardef : array[tstringtype] of longint = (
  2656. varUndefined,varUndefined,varString,varOleStr,varUString);
  2657. begin
  2658. result:=vardef[stringtype];
  2659. end;
  2660. function tstringdef.get_default_char_type: tdef;
  2661. begin
  2662. case stringtype of
  2663. st_shortstring,
  2664. st_longstring,
  2665. st_ansistring:
  2666. result:=cansichartype;
  2667. st_unicodestring,
  2668. st_widestring:
  2669. result:=cwidechartype;
  2670. end;
  2671. end;
  2672. function tstringdef.get_default_string_type: tdef;
  2673. begin
  2674. case stringtype of
  2675. st_shortstring:
  2676. result:=cshortstringtype;
  2677. { st_longstring is currently not supported but
  2678. when it is this case will need to be supplied }
  2679. st_longstring:
  2680. internalerror(2021040801);
  2681. st_ansistring:
  2682. result:=cansistringtype;
  2683. st_widestring:
  2684. result:=cwidestringtype;
  2685. st_unicodestring:
  2686. result:=cunicodestringtype;
  2687. end
  2688. end;
  2689. function tstringdef.alignment : shortint;
  2690. begin
  2691. case stringtype of
  2692. st_unicodestring,
  2693. st_widestring,
  2694. st_ansistring:
  2695. alignment:=voidpointertype.alignment;
  2696. st_longstring,
  2697. st_shortstring:
  2698. { char to string accesses byte 0 and 1 with one word access }
  2699. if (tf_requires_proper_alignment in target_info.flags) or
  2700. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2701. (m_mac in current_settings.modeswitches) then
  2702. alignment:=size_2_align(2)
  2703. else
  2704. alignment:=size_2_align(1);
  2705. end;
  2706. end;
  2707. function tstringdef.getmangledparaname : TSymStr;
  2708. begin
  2709. getmangledparaname:='STRING';
  2710. end;
  2711. function tstringdef.is_publishable : tpublishproperty;
  2712. begin
  2713. is_publishable:=pp_publish;
  2714. end;
  2715. function tstringdef.size: asizeint;
  2716. begin
  2717. case stringtype of
  2718. st_shortstring:
  2719. Result:=len+1;
  2720. st_longstring,
  2721. st_ansistring,
  2722. st_widestring,
  2723. st_unicodestring:
  2724. Result:=voidpointertype.size;
  2725. end;
  2726. end;
  2727. {****************************************************************************
  2728. TENUMDEF
  2729. ****************************************************************************}
  2730. constructor tenumdef.create;
  2731. begin
  2732. inherited create(enumdef,true);
  2733. minval:=0;
  2734. maxval:=0;
  2735. calcsavesize(current_settings.packenum);
  2736. has_jumps:=false;
  2737. basedef:=nil;
  2738. basedefderef.reset;
  2739. symtable:=tenumsymtable.create(self);
  2740. end;
  2741. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2742. begin
  2743. inherited create(enumdef,true);
  2744. minval:=_min;
  2745. maxval:=_max;
  2746. basedef:=_basedef;
  2747. calcsavesize(current_settings.packenum);
  2748. has_jumps:=false;
  2749. symtable:=basedef.symtable.getcopy;
  2750. include(defoptions, df_copied_def);
  2751. end;
  2752. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2753. begin
  2754. inherited ppuload(enumdef,ppufile);
  2755. minval:=ppufile.getaint;
  2756. maxval:=ppufile.getaint;
  2757. savesize:=ppufile.getaint;
  2758. has_jumps:=false;
  2759. if df_copied_def in defoptions then
  2760. begin
  2761. symtable:=nil;
  2762. ppufile.getderef(basedefderef);
  2763. ppuload_platform(ppufile);
  2764. end
  2765. else
  2766. begin
  2767. ppuload_platform(ppufile);
  2768. // create with nil defowner first to prevent values changes on insert
  2769. symtable:=tenumsymtable.create(nil);
  2770. tenumsymtable(symtable).ppuload(ppufile);
  2771. symtable.defowner:=self;
  2772. end;
  2773. end;
  2774. destructor tenumdef.destroy;
  2775. begin
  2776. symtable.free;
  2777. symtable:=nil;
  2778. inherited destroy;
  2779. end;
  2780. function tenumdef.getcopy : tstoreddef;
  2781. begin
  2782. if assigned(basedef) then
  2783. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2784. else
  2785. begin
  2786. result:=cenumdef.create;
  2787. tenumdef(result).minval:=minval;
  2788. tenumdef(result).maxval:=maxval;
  2789. tenumdef(result).symtable.free;
  2790. tenumdef(result).symtable:=symtable.getcopy;
  2791. tenumdef(result).basedef:=self;
  2792. end;
  2793. tenumdef(result).has_jumps:=has_jumps;
  2794. tenumdef(result).basedefderef:=basedefderef;
  2795. include(tenumdef(result).defoptions,df_copied_def);
  2796. end;
  2797. procedure tenumdef.calcsavesize(packenum: shortint);
  2798. begin
  2799. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2800. if (packenum=8) or (int64(min)<low(longint)) or (int64(max)>high(cardinal)) or ((min<0) and (max>high(longint))) then
  2801. savesize:=8
  2802. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2803. else
  2804. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2805. if (packenum=4) or (min<low(smallint)) or (max>high(word)) or ((min<0) and (max>high(smallint))) then
  2806. savesize:=4
  2807. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2808. else
  2809. if (packenum=2) or (min<low(shortint)) or (max>high(byte)) or ((min<0) and (max>high(shortint))) then
  2810. savesize:=2
  2811. else
  2812. savesize:=1;
  2813. end;
  2814. function tenumdef.packedbitsize: asizeint;
  2815. var
  2816. sizeval: tconstexprint;
  2817. power: longint;
  2818. begin
  2819. result := 0;
  2820. if (minval >= 0) and
  2821. (maxval <= 1) then
  2822. result := 1
  2823. else
  2824. begin
  2825. if (minval>=0) then
  2826. sizeval:=maxval
  2827. else
  2828. { don't count 0 twice, but take into account that range goes from -n-1..n }
  2829. sizeval:=(cutils.max(-minval,maxval+1)*2)-1;
  2830. { 256 must become 512 etc. }
  2831. nextpowerof2(sizeval+1,power);
  2832. result := power;
  2833. end;
  2834. end;
  2835. procedure tenumdef.setmax(_max:asizeint);
  2836. begin
  2837. maxval:=_max;
  2838. calcsavesize(current_settings.packenum);
  2839. end;
  2840. procedure tenumdef.setmin(_min:asizeint);
  2841. begin
  2842. minval:=_min;
  2843. calcsavesize(current_settings.packenum);
  2844. end;
  2845. function tenumdef.min:asizeint;
  2846. begin
  2847. min:=minval;
  2848. end;
  2849. function tenumdef.max:asizeint;
  2850. begin
  2851. max:=maxval;
  2852. end;
  2853. function tenumdef.getfirstsym: tsym;
  2854. var
  2855. i:integer;
  2856. begin
  2857. for i := 0 to symtable.SymList.Count - 1 do
  2858. begin
  2859. result:=tsym(symtable.SymList[i]);
  2860. if tenumsym(result).value=minval then
  2861. exit;
  2862. end;
  2863. result:=nil;
  2864. end;
  2865. function tenumdef.int2enumsym(l: asizeint): tsym;
  2866. var
  2867. i: longint;
  2868. sym: tsym;
  2869. bdef: tenumdef;
  2870. begin
  2871. result:=nil;
  2872. if (l<minval) or
  2873. (l>maxval) then
  2874. exit;
  2875. bdef:=getbasedef;
  2876. for i:=0 to bdef.symtable.symlist.count-1 do
  2877. begin
  2878. sym:=tsym(bdef.symtable.symlist[i]);
  2879. if (sym.typ=enumsym) and
  2880. (tenumsym(sym).value=l) then
  2881. begin
  2882. result:=sym;
  2883. exit;
  2884. end;
  2885. end;
  2886. end;
  2887. function tenumdef.getbasedef: tenumdef;
  2888. begin
  2889. if not assigned(basedef) then
  2890. result:=self
  2891. else
  2892. result:=basedef;
  2893. end;
  2894. procedure tenumdef.buildderef;
  2895. begin
  2896. inherited buildderef;
  2897. if df_copied_def in defoptions then
  2898. basedefderef.build(basedef)
  2899. else
  2900. tenumsymtable(symtable).buildderef;
  2901. end;
  2902. procedure tenumdef.deref;
  2903. begin
  2904. inherited deref;
  2905. if df_copied_def in defoptions then
  2906. begin
  2907. basedef:=tenumdef(basedefderef.resolve);
  2908. symtable:=basedef.symtable.getcopy;
  2909. end
  2910. else
  2911. tenumsymtable(symtable).deref(false);
  2912. end;
  2913. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2914. begin
  2915. inherited ppuwrite(ppufile);
  2916. ppufile.putaint(min);
  2917. ppufile.putaint(max);
  2918. ppufile.putaint(savesize);
  2919. if df_copied_def in defoptions then
  2920. ppufile.putderef(basedefderef);
  2921. writeentry(ppufile,ibenumdef);
  2922. if not (df_copied_def in defoptions) then
  2923. tenumsymtable(symtable).ppuwrite(ppufile);
  2924. end;
  2925. function tenumdef.is_publishable : tpublishproperty;
  2926. begin
  2927. if not has_jumps then
  2928. is_publishable:=pp_publish
  2929. else
  2930. if m_delphi in current_settings.modeswitches then
  2931. is_publishable:=pp_ignore
  2932. else
  2933. is_publishable:=pp_error;
  2934. end;
  2935. function tenumdef.GetTypeName : string;
  2936. begin
  2937. GetTypeName:='<enumeration type>';
  2938. end;
  2939. {****************************************************************************
  2940. TRTTI_ATTRIBUTE_LIST
  2941. ****************************************************************************}
  2942. constructor trtti_attribute.ppuload(ppufile: tcompilerppufile);
  2943. begin
  2944. ppufile.getderef(typesymderef);
  2945. ppufile.getderef(typeconstrderef);
  2946. setlength(paras,ppufile.getlongint);
  2947. end;
  2948. procedure trtti_attribute.ppuwrite(ppufile: tcompilerppufile);
  2949. begin
  2950. ppufile.putderef(typesymderef);
  2951. ppufile.putderef(typeconstrderef);
  2952. ppufile.putlongint(length(paras));
  2953. end;
  2954. procedure trtti_attribute.ppuload_subentries(ppufile: tcompilerppufile);
  2955. var
  2956. i : sizeint;
  2957. begin
  2958. for i:=0 to high(paras) do
  2959. paras[i]:=ppuloadnodetree(ppufile);
  2960. end;
  2961. procedure trtti_attribute.ppuwrite_subentries(ppufile: tcompilerppufile);
  2962. var
  2963. i : sizeint;
  2964. begin
  2965. for i:=0 to high(paras) do
  2966. ppuwritenodetree(ppufile,paras[i]);
  2967. end;
  2968. destructor trtti_attribute.destroy;
  2969. var
  2970. n : tnode;
  2971. begin
  2972. constructorcall.free;
  2973. for n in paras do
  2974. n.free;
  2975. inherited destroy;
  2976. end;
  2977. procedure trtti_attribute.buildderef;
  2978. var
  2979. i : sizeint;
  2980. begin
  2981. typesymderef.build(typesym);
  2982. typeconstrderef.build(typeconstr);
  2983. for i:=0 to high(paras) do
  2984. paras[i].buildderefimpl;
  2985. end;
  2986. procedure trtti_attribute.deref;
  2987. var
  2988. i : sizeint;
  2989. begin
  2990. typesym:=tsym(typesymderef.resolve);
  2991. typeconstr:=tdef(typeconstrderef.resolve);
  2992. for i:=0 to high(paras) do
  2993. paras[i].derefimpl;
  2994. end;
  2995. class procedure trtti_attribute_list.bind(var dangling,owned:trtti_attribute_list);
  2996. begin
  2997. if assigned(owned) then
  2998. internalerror(2019071001);
  2999. if not assigned(dangling) then
  3000. exit;
  3001. if dangling.is_bound then
  3002. internalerror(2019071002);
  3003. current_module.used_rtti_attrs.concatlistcopy(dangling.rtti_attributes);
  3004. dangling.is_bound:=true;
  3005. owned:=dangling;
  3006. dangling:=nil;
  3007. end;
  3008. class procedure trtti_attribute_list.copyandbind(alist : trtti_attribute_list; var owned: trtti_attribute_list);
  3009. var
  3010. i,j : Integer;
  3011. attr,newattribute : trtti_attribute;
  3012. begin
  3013. if owned=Nil then
  3014. owned:=trtti_attribute_list.Create;
  3015. owned.is_bound:=True;
  3016. for i:=0 to aList.rtti_attributes.Count-1 do
  3017. begin
  3018. attr:=trtti_attribute(alist.rtti_attributes[i]);
  3019. newattribute:=trtti_attribute.Create;
  3020. newattribute.typesym:=attr.typesym;
  3021. newattribute.typeconstr:=attr.typeconstr;
  3022. newattribute.constructorcall:=attr.constructorcall.getcopy;
  3023. setlength(newattribute.paras,length(attr.paras));
  3024. for j:=0 to length(attr.paras)-1 do
  3025. newattribute.paras[j]:=attr.paras[j].getcopy;
  3026. owned.AddAttribute(newattribute);
  3027. end;
  3028. current_module.used_rtti_attrs.concatlistcopy(owned.rtti_attributes);
  3029. end;
  3030. procedure trtti_attribute_list.addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  3031. var
  3032. newattribute : trtti_attribute;
  3033. i : sizeint;
  3034. begin
  3035. if not assigned(rtti_attributes) then
  3036. rtti_attributes:=TFPObjectList.Create(true);
  3037. newattribute:=trtti_attribute.Create;
  3038. newattribute.typesym:=atypesym;
  3039. newattribute.typeconstr:=typeconstr;
  3040. newattribute.constructorcall:=constructorcall;
  3041. setlength(newattribute.paras,length(paras));
  3042. for i:=0 to high(paras) do
  3043. newattribute.paras[i]:=paras[i];
  3044. rtti_attributes.Add(newattribute);
  3045. end;
  3046. procedure trtti_attribute_list.addattribute(attr:trtti_attribute);
  3047. begin
  3048. if not assigned(rtti_attributes) then
  3049. rtti_attributes:=TFPObjectList.Create(true);
  3050. rtti_attributes.add(attr);
  3051. end;
  3052. destructor trtti_attribute_list.destroy;
  3053. begin
  3054. rtti_attributes.Free;
  3055. inherited destroy;
  3056. end;
  3057. function trtti_attribute_list.get_attribute_count:longint;
  3058. begin
  3059. if assigned(rtti_attributes) then
  3060. result:=rtti_attributes.Count
  3061. else
  3062. result:=0;
  3063. end;
  3064. procedure trtti_attribute_list.buildderef;
  3065. var
  3066. i : sizeint;
  3067. begin
  3068. if not assigned(rtti_attributes) then
  3069. exit;
  3070. for i:=0 to rtti_attributes.count-1 do
  3071. trtti_attribute(rtti_attributes[i]).buildderef;
  3072. end;
  3073. procedure trtti_attribute_list.deref;
  3074. var
  3075. i : sizeint;
  3076. begin
  3077. if not assigned(rtti_attributes) then
  3078. exit;
  3079. for i:=0 to rtti_attributes.count-1 do
  3080. trtti_attribute(rtti_attributes[i]).deref;
  3081. end;
  3082. class procedure trtti_attribute_list.ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3083. var
  3084. i : sizeint;
  3085. begin
  3086. if assigned(attrlist) then
  3087. begin
  3088. if not assigned(attrlist.rtti_attributes) then
  3089. internalerror(2019071101);
  3090. for i:=0 to attrlist.rtti_attributes.count-1 do
  3091. trtti_attribute(attrlist.rtti_attributes[i]).ppuload_subentries(ppufile);
  3092. end;
  3093. end;
  3094. class procedure trtti_attribute_list.ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3095. var
  3096. i : sizeint;
  3097. begin
  3098. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  3099. begin
  3100. for i:=0 to attrlist.rtti_attributes.count-1 do
  3101. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite_subentries(ppufile);
  3102. end;
  3103. end;
  3104. class function trtti_attribute_list.ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  3105. var
  3106. cnt,i : longint;
  3107. begin
  3108. cnt:=ppufile.getlongint;
  3109. if cnt>0 then
  3110. begin
  3111. result:=trtti_attribute_list.create;
  3112. for i:=0 to cnt-1 do
  3113. result.addattribute(trtti_attribute.ppuload(ppufile));
  3114. end
  3115. else
  3116. result:=nil;
  3117. end;
  3118. class procedure trtti_attribute_list.ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3119. var
  3120. i : longint;
  3121. begin
  3122. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  3123. begin
  3124. ppufile.putlongint(attrlist.rtti_attributes.count);
  3125. for i:=0 to attrlist.rtti_attributes.count-1 do
  3126. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite(ppufile);
  3127. end
  3128. else
  3129. ppufile.putlongint(0);
  3130. end;
  3131. {****************************************************************************
  3132. TORDDEF
  3133. ****************************************************************************}
  3134. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  3135. begin
  3136. inherited create(orddef,doregister);
  3137. low:=v;
  3138. high:=b;
  3139. ordtype:=t;
  3140. setsize;
  3141. end;
  3142. constructor torddef.ppuload(ppufile:tcompilerppufile);
  3143. begin
  3144. inherited ppuload(orddef,ppufile);
  3145. ordtype:=tordtype(ppufile.getbyte);
  3146. low:=ppufile.getexprint;
  3147. high:=ppufile.getexprint;
  3148. setsize;
  3149. ppuload_platform(ppufile);
  3150. end;
  3151. function torddef.getcopy : tstoreddef;
  3152. begin
  3153. result:=corddef.create(ordtype,low,high,true);
  3154. result.typ:=orddef;
  3155. torddef(result).low:=low;
  3156. torddef(result).high:=high;
  3157. torddef(result).ordtype:=ordtype;
  3158. torddef(result).savesize:=savesize;
  3159. end;
  3160. function torddef.alignment:shortint;
  3161. begin
  3162. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) and
  3163. (ordtype in [s64bit,u64bit]) then
  3164. result := 4
  3165. else
  3166. result := inherited alignment;
  3167. end;
  3168. procedure torddef.setsize;
  3169. const
  3170. sizetbl : array[tordtype] of longint = (
  3171. 0,
  3172. 1,2,4,8,16,
  3173. 1,2,4,8,16,
  3174. 1,1,2,4,8,
  3175. 1,2,4,8,
  3176. 1,2,8,system.high(longint)
  3177. );
  3178. begin
  3179. savesize:=sizetbl[ordtype];
  3180. if savesize=system.high(longint) then
  3181. savesize:=packedbitsize div 8;
  3182. end;
  3183. function torddef.packedbitsize: asizeint;
  3184. var
  3185. sizeval: tconstexprint;
  3186. power: longint;
  3187. begin
  3188. result := 0;
  3189. if ordtype = uvoid then
  3190. exit;
  3191. {$ifndef cpu64bitalu}
  3192. if (ordtype in [s64bit,u64bit]) then
  3193. {$else not cpu64bitalu}
  3194. if ((ordtype = u64bit) and
  3195. (high > system.high(int64))) or
  3196. ((ordtype = s64bit) and
  3197. ((low <= (system.low(int64) div 2)) or
  3198. ((low < 0) and
  3199. (high > (system.high(int64) div 2))))) then
  3200. {$endif cpu64bitalu}
  3201. result := 64
  3202. else if (
  3203. (low >= 0) and
  3204. (high <= 1)
  3205. ) or (
  3206. ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]
  3207. ) then
  3208. result := 1
  3209. else
  3210. begin
  3211. if (low>=0) then
  3212. sizeval:=high
  3213. else
  3214. { don't count 0 twice, but take into account that range goes from -n-1..n }
  3215. sizeval:=(cutils.max(-low,high+1)*2)-1;
  3216. { 256 must become 512 etc. }
  3217. nextpowerof2(sizeval+1,power);
  3218. result := power;
  3219. end;
  3220. end;
  3221. function torddef.getvardef : longint;
  3222. const
  3223. basetype2vardef : array[tordtype] of longint = (
  3224. varUndefined,
  3225. varbyte,varword,varlongword,varqword,varUndefined,
  3226. varshortint,varsmallint,varinteger,varint64,varUndefined,
  3227. varboolean,varboolean,varboolean,varboolean,varboolean,
  3228. varboolean,varboolean,varUndefined,varUndefined,
  3229. varUndefined,varUndefined,varCurrency,varEmpty);
  3230. begin
  3231. result:=basetype2vardef[ordtype];
  3232. if result=varEmpty then
  3233. result:=basetype2vardef[range_to_basetype(low,high)];
  3234. end;
  3235. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  3236. begin
  3237. inherited ppuwrite(ppufile);
  3238. ppufile.putbyte(byte(ordtype));
  3239. ppufile.putexprint(low);
  3240. ppufile.putexprint(high);
  3241. writeentry(ppufile,iborddef);
  3242. end;
  3243. function torddef.is_publishable : tpublishproperty;
  3244. begin
  3245. if ordtype<>uvoid then
  3246. is_publishable:=pp_publish
  3247. else
  3248. is_publishable:=pp_error;
  3249. end;
  3250. function torddef.GetTypeName : string;
  3251. const
  3252. names : array[tordtype] of string[20] = (
  3253. 'untyped',
  3254. 'Byte','Word','DWord','QWord','UInt128',
  3255. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  3256. 'Boolean','Boolean8','Boolean16','Boolean32','Boolean64',
  3257. 'ByteBool','WordBool','LongBool','QWordBool',
  3258. 'AnsiChar','WideChar','Currency','CustomRange');
  3259. begin
  3260. GetTypeName:=names[ordtype];
  3261. end;
  3262. {****************************************************************************
  3263. TFLOATDEF
  3264. ****************************************************************************}
  3265. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  3266. begin
  3267. inherited create(floatdef,doregister);
  3268. floattype:=t;
  3269. setsize;
  3270. end;
  3271. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  3272. begin
  3273. inherited ppuload(floatdef,ppufile);
  3274. floattype:=tfloattype(ppufile.getbyte);
  3275. setsize;
  3276. ppuload_platform(ppufile);
  3277. end;
  3278. function tfloatdef.getcopy : tstoreddef;
  3279. begin
  3280. result:=cfloatdef.create(floattype,true);
  3281. result.typ:=floatdef;
  3282. tfloatdef(result).savesize:=savesize;
  3283. end;
  3284. function tfloatdef.alignment:shortint;
  3285. begin
  3286. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) then
  3287. case floattype of
  3288. sc80real,
  3289. s80real: result:=16;
  3290. s64real,
  3291. s64currency,
  3292. s64comp : result:=4;
  3293. else
  3294. result := inherited alignment;
  3295. end
  3296. else
  3297. result := inherited alignment;
  3298. end;
  3299. function tfloatdef.structalignment: shortint;
  3300. begin
  3301. { aix is really annoying: the recommended scalar alignment for both
  3302. int64 and double is 64 bits, but in structs int64 has to be aligned
  3303. to 8 bytes and double to 4 bytes }
  3304. if (target_info.system in systems_aix) and
  3305. (floattype=s64real) then
  3306. result:=4
  3307. else
  3308. result:=alignment;
  3309. end;
  3310. procedure tfloatdef.setsize;
  3311. begin
  3312. case floattype of
  3313. s32real : savesize:=4;
  3314. s80real : savesize:=10;
  3315. sc80real:
  3316. if target_info.system in [system_i386_darwin,
  3317. system_i386_iphonesim,system_x86_64_darwin,
  3318. system_x86_64_iphonesim,
  3319. system_x86_64_linux,system_x86_64_freebsd,
  3320. system_x86_64_openbsd,system_x86_64_netbsd,
  3321. system_x86_64_solaris,system_x86_64_embedded,
  3322. system_x86_64_dragonfly,system_x86_64_haiku] then
  3323. savesize:=16
  3324. else
  3325. savesize:=12;
  3326. s64real,
  3327. s64currency,
  3328. s64comp : savesize:=8;
  3329. else
  3330. savesize:=0;
  3331. end;
  3332. end;
  3333. function tfloatdef.getvardef : longint;
  3334. const
  3335. floattype2vardef : array[tfloattype] of longint = (
  3336. varSingle,varDouble,varUndefined,varUndefined,
  3337. varUndefined,varCurrency,varUndefined);
  3338. begin
  3339. if (upper(typename)='TDATETIME') and
  3340. assigned(owner) and
  3341. assigned(owner.name) and
  3342. (owner.name^='SYSTEM') then
  3343. result:=varDate
  3344. else
  3345. result:=floattype2vardef[floattype];
  3346. end;
  3347. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  3348. begin
  3349. inherited ppuwrite(ppufile);
  3350. ppufile.putbyte(byte(floattype));
  3351. writeentry(ppufile,ibfloatdef);
  3352. end;
  3353. function tfloatdef.is_publishable : tpublishproperty;
  3354. begin
  3355. is_publishable:=pp_publish;
  3356. end;
  3357. function tfloatdef.GetTypeName : string;
  3358. const
  3359. names : array[tfloattype] of string[20] = (
  3360. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  3361. begin
  3362. GetTypeName:=names[floattype];
  3363. end;
  3364. {****************************************************************************
  3365. TFILEDEF
  3366. ****************************************************************************}
  3367. constructor tfiledef.createtext;
  3368. begin
  3369. inherited create(filedef,true);
  3370. filetyp:=ft_text;
  3371. typedfiledef:=nil;
  3372. typedfiledefderef.reset;
  3373. end;
  3374. constructor tfiledef.createuntyped;
  3375. begin
  3376. inherited create(filedef,true);
  3377. filetyp:=ft_untyped;
  3378. typedfiledef:=nil;
  3379. end;
  3380. constructor tfiledef.createtyped(def:tdef);
  3381. begin
  3382. inherited create(filedef,true);
  3383. filetyp:=ft_typed;
  3384. typedfiledef:=def;
  3385. end;
  3386. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  3387. begin
  3388. inherited ppuload(filedef,ppufile);
  3389. filetyp:=tfiletyp(ppufile.getbyte);
  3390. if filetyp=ft_typed then
  3391. ppufile.getderef(typedfiledefderef)
  3392. else
  3393. typedfiledef:=nil;
  3394. ppuload_platform(ppufile);
  3395. end;
  3396. function tfiledef.getcopy : tstoreddef;
  3397. begin
  3398. case filetyp of
  3399. ft_typed:
  3400. result:=cfiledef.createtyped(typedfiledef);
  3401. ft_untyped:
  3402. result:=cfiledef.createuntyped;
  3403. ft_text:
  3404. result:=cfiledef.createtext;
  3405. end;
  3406. end;
  3407. procedure tfiledef.buildderef;
  3408. begin
  3409. inherited buildderef;
  3410. if filetyp=ft_typed then
  3411. typedfiledefderef.build(typedfiledef);
  3412. end;
  3413. procedure tfiledef.deref;
  3414. begin
  3415. inherited deref;
  3416. if filetyp=ft_typed then
  3417. typedfiledef:=tdef(typedfiledefderef.resolve);
  3418. end;
  3419. function tfiledef.size:asizeint;
  3420. begin
  3421. if savesize=0 then
  3422. setsize;
  3423. size:=savesize;
  3424. end;
  3425. procedure tfiledef.setsize;
  3426. begin
  3427. case filetyp of
  3428. ft_text:
  3429. savesize:=search_system_type('TEXTREC').typedef.size;
  3430. ft_typed:
  3431. begin
  3432. savesize:=search_system_type('FILEREC').typedef.size;
  3433. { allocate put/get buffer in iso mode }
  3434. if m_isolike_io in current_settings.modeswitches then
  3435. inc(savesize,typedfiledef.size);
  3436. end;
  3437. ft_untyped:
  3438. savesize:=search_system_type('FILEREC').typedef.size;
  3439. end;
  3440. end;
  3441. function tfiledef.alignment: shortint;
  3442. begin
  3443. case filetyp of
  3444. ft_text:
  3445. result:=search_system_type('TEXTREC').typedef.alignment;
  3446. ft_typed,
  3447. ft_untyped:
  3448. result:=search_system_type('FILEREC').typedef.alignment;
  3449. end;
  3450. end;
  3451. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  3452. begin
  3453. inherited ppuwrite(ppufile);
  3454. ppufile.putbyte(byte(filetyp));
  3455. if filetyp=ft_typed then
  3456. ppufile.putderef(typedfiledefderef);
  3457. writeentry(ppufile,ibfiledef);
  3458. end;
  3459. function tfiledef.GetTypeName : string;
  3460. begin
  3461. case filetyp of
  3462. ft_untyped:
  3463. GetTypeName:='File';
  3464. ft_typed:
  3465. GetTypeName:='File Of '+typedfiledef.typename;
  3466. ft_text:
  3467. GetTypeName:='Text'
  3468. end;
  3469. end;
  3470. function tfiledef.getmangledparaname : TSymStr;
  3471. begin
  3472. case filetyp of
  3473. ft_untyped:
  3474. getmangledparaname:='FILE';
  3475. ft_typed:
  3476. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  3477. ft_text:
  3478. getmangledparaname:='TEXT'
  3479. end;
  3480. end;
  3481. {****************************************************************************
  3482. TVARIANTDEF
  3483. ****************************************************************************}
  3484. constructor tvariantdef.create(v : tvarianttype);
  3485. begin
  3486. inherited create(variantdef,true);
  3487. varianttype:=v;
  3488. setsize;
  3489. end;
  3490. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  3491. begin
  3492. inherited ppuload(variantdef,ppufile);
  3493. varianttype:=tvarianttype(ppufile.getbyte);
  3494. setsize;
  3495. ppuload_platform(ppufile);
  3496. end;
  3497. function tvariantdef.getcopy : tstoreddef;
  3498. begin
  3499. result:=cvariantdef.create(varianttype);
  3500. end;
  3501. function tvariantdef.alignment: shortint;
  3502. begin
  3503. result:=search_system_type('TVARDATA').typedef.alignment;
  3504. end;
  3505. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  3506. begin
  3507. inherited ppuwrite(ppufile);
  3508. ppufile.putbyte(byte(varianttype));
  3509. writeentry(ppufile,ibvariantdef);
  3510. end;
  3511. function tvariantdef.getvardef : longint;
  3512. begin
  3513. Result:=varVariant;
  3514. end;
  3515. procedure tvariantdef.setsize;
  3516. begin
  3517. {$ifdef cpu64bitaddr}
  3518. savesize:=24;
  3519. {$else cpu64bitaddr}
  3520. savesize:=16;
  3521. {$endif cpu64bitaddr}
  3522. end;
  3523. function tvariantdef.GetTypeName : string;
  3524. begin
  3525. case varianttype of
  3526. vt_normalvariant:
  3527. GetTypeName:='Variant';
  3528. vt_olevariant:
  3529. GetTypeName:='OleVariant';
  3530. end;
  3531. end;
  3532. function tvariantdef.needs_inittable : boolean;
  3533. begin
  3534. needs_inittable:=true;
  3535. end;
  3536. function tvariantdef.is_publishable : tpublishproperty;
  3537. begin
  3538. is_publishable:=pp_publish;
  3539. end;
  3540. {****************************************************************************
  3541. TABSTRACtpointerdef
  3542. ****************************************************************************}
  3543. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  3544. begin
  3545. inherited create(dt,true);
  3546. pointeddef:=def;
  3547. pointeddefderef.reset;
  3548. if df_generic in pointeddef.defoptions then
  3549. include(defoptions,df_generic);
  3550. if df_specialization in pointeddef.defoptions then
  3551. include(defoptions,df_specialization);
  3552. end;
  3553. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3554. begin
  3555. inherited ppuload(dt,ppufile);
  3556. ppufile.getderef(pointeddefderef);
  3557. end;
  3558. procedure tabstractpointerdef.buildderef;
  3559. begin
  3560. inherited buildderef;
  3561. pointeddefderef.build(pointeddef);
  3562. end;
  3563. procedure tabstractpointerdef.deref;
  3564. begin
  3565. inherited deref;
  3566. pointeddef:=tdef(pointeddefderef.resolve);
  3567. end;
  3568. function tabstractpointerdef.size: asizeint;
  3569. begin
  3570. Result:=voidpointertype.size;
  3571. end;
  3572. function tabstractpointerdef.alignment: shortint;
  3573. begin
  3574. alignment:=size_2_align(voidpointertype.size);
  3575. end;
  3576. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3577. begin
  3578. inherited ppuwrite(ppufile);
  3579. ppufile.putderef(pointeddefderef);
  3580. end;
  3581. {****************************************************************************
  3582. tpointerdef
  3583. ****************************************************************************}
  3584. constructor tpointerdef.create(def:tdef);
  3585. begin
  3586. inherited create(pointerdef,def);
  3587. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  3588. if (df_specialization in tstoreddef(def).defoptions)
  3589. {$ifndef genericdef_for_nested}
  3590. { currently, nested procdefs of generic routines get df_specialization,
  3591. but no genericdef }
  3592. and assigned(tstoreddef(def).genericdef)
  3593. {$endif}
  3594. then
  3595. genericdef:=cpointerdef.getreusable(tstoreddef(def).genericdef);
  3596. end;
  3597. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  3598. var
  3599. res: PHashSetItem;
  3600. oldsymtablestack: tsymtablestack;
  3601. begin
  3602. if not assigned(current_module) then
  3603. internalerror(2011071101);
  3604. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  3605. if not assigned(res^.Data) then
  3606. begin
  3607. { since these pointerdefs can be reused anywhere in the current
  3608. unit, add them to the global/staticsymtable (or local symtable
  3609. if they're a local def, because otherwise they'll be saved
  3610. to the ppu referencing a local symtable entry that doesn't
  3611. exist in the ppu) }
  3612. oldsymtablestack:=symtablestack;
  3613. { do not simply push/pop current_module.localsymtable, because
  3614. that can have side-effects (e.g., it removes helpers) }
  3615. symtablestack:=nil;
  3616. result:=cpointerdef.create(def);
  3617. setup_reusable_def(def,result,res,oldsymtablestack);
  3618. { res^.Data may still be nil -> don't overwrite result }
  3619. exit;
  3620. end;
  3621. result:=tpointerdef(res^.Data);
  3622. end;
  3623. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  3624. begin
  3625. result:=getreusable(def);
  3626. if not result.is_registered then
  3627. include(result.defoptions,df_not_registered_no_free);
  3628. end;
  3629. function tpointerdef.size: asizeint;
  3630. begin
  3631. result:=sizeof(pint);
  3632. end;
  3633. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  3634. begin
  3635. inherited ppuload(pointerdef,ppufile);
  3636. has_pointer_math:=(ppufile.getbyte<>0);
  3637. ppuload_platform(ppufile);
  3638. end;
  3639. function tpointerdef.getcopy : tstoreddef;
  3640. begin
  3641. { don't use direct pointeddef if it is a forwarddef because in other case
  3642. one of them will be destroyed on forward type resolve and the second will
  3643. point to garbage }
  3644. if pointeddef.typ=forwarddef then
  3645. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  3646. else
  3647. result:=cpointerdef.create(pointeddef);
  3648. tpointerdef(result).has_pointer_math:=has_pointer_math;
  3649. tpointerdef(result).savesize:=savesize;
  3650. end;
  3651. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3652. begin
  3653. inherited ppuwrite(ppufile);
  3654. ppufile.putbyte(byte(has_pointer_math));
  3655. writeentry(ppufile,ibpointerdef);
  3656. end;
  3657. function tpointerdef.GetTypeName : string;
  3658. begin
  3659. { parameter types and the resultdef of a procvardef can contain a
  3660. pointer to this procvardef itself, resulting in endless recursion ->
  3661. use the typesym's name instead if it exists (if it doesn't, such as
  3662. for anynonymous procedure types in macpas/iso mode, then there cannot
  3663. be any recursive references to it either) }
  3664. if (pointeddef.typ<>procvardef) or
  3665. not assigned(pointeddef.typesym) then
  3666. GetTypeName:='^'+pointeddef.typename
  3667. else
  3668. GetTypeName:='^'+pointeddef.typesym.realname;
  3669. end;
  3670. function tpointerdef.pointer_arithmetic_int_type:tdef;
  3671. begin
  3672. result:=ptrsinttype;
  3673. end;
  3674. function tpointerdef.pointer_arithmetic_uint_type:tdef;
  3675. begin
  3676. result:=ptruinttype;
  3677. end;
  3678. function tpointerdef.pointer_subtraction_result_type:tdef;
  3679. begin
  3680. result:=ptrsinttype;
  3681. end;
  3682. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  3683. begin
  3684. result:=true;
  3685. end;
  3686. function tpointerdef.converted_pointer_to_array_range_type:tdef;
  3687. begin
  3688. result:=ptrsinttype;
  3689. end;
  3690. {****************************************************************************
  3691. TCLASSREFDEF
  3692. ****************************************************************************}
  3693. constructor tclassrefdef.create(def:tdef);
  3694. begin
  3695. while (def.typ=objectdef) and tobjectdef(def).is_unique_objpasdef do
  3696. def:=tobjectdef(def).childof;
  3697. inherited create(classrefdef,def);
  3698. if df_specialization in tstoreddef(def).defoptions then
  3699. genericdef:=cclassrefdef.create(tstoreddef(def).genericdef);
  3700. end;
  3701. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  3702. begin
  3703. inherited ppuload(classrefdef,ppufile);
  3704. ppuload_platform(ppufile);
  3705. end;
  3706. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  3707. begin
  3708. inherited ppuwrite(ppufile);
  3709. writeentry(ppufile,ibclassrefdef);
  3710. end;
  3711. function tclassrefdef.getcopy:tstoreddef;
  3712. begin
  3713. if pointeddef.typ=forwarddef then
  3714. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3715. else
  3716. result:=cclassrefdef.create(pointeddef);
  3717. end;
  3718. function tclassrefdef.GetTypeName : string;
  3719. begin
  3720. GetTypeName:='Class Of '+pointeddef.typename;
  3721. end;
  3722. function tclassrefdef.is_publishable : tpublishproperty;
  3723. begin
  3724. is_publishable:=pp_publish;
  3725. end;
  3726. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3727. begin
  3728. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3729. result:=inherited rtti_mangledname(rt)
  3730. else
  3731. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3732. end;
  3733. procedure tclassrefdef.register_created_object_type;
  3734. begin
  3735. tobjectdef(pointeddef).register_created_classref_type;
  3736. end;
  3737. {***************************************************************************
  3738. TSETDEF
  3739. ***************************************************************************}
  3740. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3741. var
  3742. setallocbits: aint;
  3743. packedsavesize: aint;
  3744. actual_setalloc: ShortInt;
  3745. begin
  3746. inherited create(setdef,doregister);
  3747. elementdef:=def;
  3748. elementdefderef.reset;
  3749. setmax:=high;
  3750. setlow:=low;
  3751. actual_setalloc:=current_settings.setalloc;
  3752. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3753. if actual_setalloc=0 then
  3754. actual_setalloc:=1;
  3755. {$endif}
  3756. if (actual_setalloc=0) then
  3757. begin
  3758. setbase:=0;
  3759. if (high<32) then
  3760. savesize:=Sizeof(longint)
  3761. else if (high<256) then
  3762. savesize:=32
  3763. else
  3764. savesize:=(high+7) div 8
  3765. end
  3766. else
  3767. begin
  3768. setallocbits:=actual_setalloc*8;
  3769. setbase:=low and not(setallocbits-1);
  3770. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3771. savesize:=packedsavesize;
  3772. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3773. if savesize=3 then
  3774. savesize:=4;
  3775. {$endif}
  3776. end;
  3777. end;
  3778. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3779. begin
  3780. inherited ppuload(setdef,ppufile);
  3781. ppufile.getderef(elementdefderef);
  3782. savesize:=ppufile.getasizeint;
  3783. setbase:=ppufile.getasizeint;
  3784. setlow:=ppufile.getasizeint;
  3785. setmax:=ppufile.getasizeint;
  3786. ppuload_platform(ppufile);
  3787. end;
  3788. function tsetdef.getcopy : tstoreddef;
  3789. begin
  3790. result:=csetdef.create(elementdef,setbase,setmax,true);
  3791. { the copy might have been created with a different setalloc setting }
  3792. tsetdef(result).savesize:=savesize;
  3793. end;
  3794. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3795. begin
  3796. inherited ppuwrite(ppufile);
  3797. ppufile.putderef(elementdefderef);
  3798. ppufile.putasizeint(savesize);
  3799. ppufile.putasizeint(setbase);
  3800. ppufile.putasizeint(setlow);
  3801. ppufile.putasizeint(setmax);
  3802. writeentry(ppufile,ibsetdef);
  3803. end;
  3804. procedure tsetdef.buildderef;
  3805. begin
  3806. inherited buildderef;
  3807. elementdefderef.build(elementdef);
  3808. end;
  3809. procedure tsetdef.deref;
  3810. begin
  3811. inherited deref;
  3812. elementdef:=tdef(elementdefderef.resolve);
  3813. end;
  3814. function tsetdef.is_publishable : tpublishproperty;
  3815. begin
  3816. if savesize in [1,2,4] then
  3817. is_publishable:=pp_publish
  3818. else
  3819. is_publishable:=pp_error;
  3820. end;
  3821. function tsetdef.alignment: shortint;
  3822. begin
  3823. Result:=inherited;
  3824. if result>sizeof(aint) then
  3825. result:=sizeof(aint);
  3826. end;
  3827. function tsetdef.GetTypeName : string;
  3828. begin
  3829. if assigned(elementdef) then
  3830. GetTypeName:='Set Of '+elementdef.typename
  3831. else
  3832. GetTypeName:='Empty Set';
  3833. end;
  3834. {***************************************************************************
  3835. TFORMALDEF
  3836. ***************************************************************************}
  3837. constructor tformaldef.create(Atyped:boolean);
  3838. begin
  3839. inherited create(formaldef,true);
  3840. typed:=Atyped;
  3841. savesize:=0;
  3842. end;
  3843. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3844. begin
  3845. inherited ppuload(formaldef,ppufile);
  3846. typed:=boolean(ppufile.getbyte);
  3847. savesize:=0;
  3848. ppuload_platform(ppufile);
  3849. end;
  3850. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3851. begin
  3852. inherited ppuwrite(ppufile);
  3853. ppufile.putbyte(byte(typed));
  3854. writeentry(ppufile,ibformaldef);
  3855. end;
  3856. function tformaldef.GetTypeName : string;
  3857. begin
  3858. if typed then
  3859. GetTypeName:='<Typed formal type>'
  3860. else
  3861. GetTypeName:='<Formal type>';
  3862. end;
  3863. {***************************************************************************
  3864. TARRAYDEF
  3865. ***************************************************************************}
  3866. constructor tarraydef.create(l, h: asizeint; def: tdef);
  3867. begin
  3868. inherited create(arraydef,true);
  3869. lowrange:=l;
  3870. highrange:=h;
  3871. rangedef:=def;
  3872. rangedefderef.reset;
  3873. _elementdef:=nil;
  3874. _elementdefderef.reset;
  3875. arrayoptions:=[];
  3876. symtable:=tarraysymtable.create(self);
  3877. end;
  3878. constructor tarraydef.create_vector(l ,h: asizeint; def: tdef);
  3879. begin
  3880. self.create(l,h,def);
  3881. include(arrayoptions,ado_IsVector);
  3882. end;
  3883. constructor tarraydef.create_openarray;
  3884. begin
  3885. self.create(0,-1,sizesinttype);
  3886. include(arrayoptions,ado_OpenArray);
  3887. end;
  3888. class function tarraydef.getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  3889. var
  3890. res: PHashSetItem;
  3891. oldsymtablestack: tsymtablestack;
  3892. arrdesc: packed record
  3893. def: tdef;
  3894. elecount: asizeint;
  3895. options: tarraydefoptions
  3896. end;
  3897. begin
  3898. if not assigned(current_module) then
  3899. internalerror(2011081301);
  3900. arrdesc.def:=def;
  3901. arrdesc.elecount:=elems;
  3902. arrdesc.options:=options;
  3903. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3904. if not assigned(res^.Data) then
  3905. begin
  3906. { since these pointerdefs can be reused anywhere in the current
  3907. unit, add them to the global/staticsymtable (or local symtable
  3908. if they're a local def, because otherwise they'll be saved
  3909. to the ppu referencing a local symtable entry that doesn't
  3910. exist in the ppu) }
  3911. oldsymtablestack:=symtablestack;
  3912. { do not simply push/pop current_module.localsymtable, because
  3913. that can have side-effects (e.g., it removes helpers) }
  3914. symtablestack:=nil;
  3915. result:=carraydef.create(0,elems-1,sizesinttype);
  3916. result.elementdef:=def;
  3917. result.arrayoptions:=options;
  3918. setup_reusable_def(def,result,res,oldsymtablestack);
  3919. { res^.Data may still be nil -> don't overwrite result }
  3920. exit;
  3921. end;
  3922. result:=tarraydef(res^.Data);
  3923. end;
  3924. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3925. begin
  3926. result:=getreusable_intern(def,elems,[]);
  3927. end;
  3928. class function tarraydef.getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  3929. begin
  3930. result:=getreusable_intern(def,elems,[ado_IsVector]);
  3931. end;
  3932. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3933. begin
  3934. result:=getreusable(def,elems);
  3935. if not result.is_registered then
  3936. include(result.defoptions,df_not_registered_no_free);
  3937. end;
  3938. class function tarraydef.getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  3939. begin
  3940. result:=getreusable_vector(def,elems);
  3941. if not result.is_registered then
  3942. include(result.defoptions,df_not_registered_no_free);
  3943. end;
  3944. destructor tarraydef.destroy;
  3945. begin
  3946. symtable.free;
  3947. symtable:=nil;
  3948. inherited;
  3949. end;
  3950. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3951. begin
  3952. { divide by the element size and do -1 so the array will have a valid size,
  3953. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3954. to avoid a division by zero }
  3955. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,
  3956. def.converted_pointer_to_array_range_type);
  3957. arrayoptions:=[ado_IsConvertedPointer];
  3958. setelementdef(def.pointeddef);
  3959. end;
  3960. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3961. begin
  3962. inherited ppuload(arraydef,ppufile);
  3963. { the addresses are calculated later }
  3964. ppufile.getderef(_elementdefderef);
  3965. ppufile.getderef(rangedefderef);
  3966. lowrange:=ppufile.getasizeint;
  3967. highrange:=ppufile.getasizeint;
  3968. ppufile.getset(tppuset2(arrayoptions));
  3969. ppuload_platform(ppufile);
  3970. symtable:=tarraysymtable.create(self);
  3971. tarraysymtable(symtable).ppuload(ppufile)
  3972. end;
  3973. function tarraydef.getcopy : tstoreddef;
  3974. begin
  3975. result:=carraydef.create(lowrange,highrange,rangedef);
  3976. tarraydef(result).arrayoptions:=arrayoptions;
  3977. tarraydef(result)._elementdef:=_elementdef;
  3978. end;
  3979. procedure tarraydef.buildderef;
  3980. begin
  3981. inherited buildderef;
  3982. tarraysymtable(symtable).buildderef;
  3983. _elementdefderef.build(_elementdef);
  3984. rangedefderef.build(rangedef);
  3985. end;
  3986. procedure tarraydef.deref;
  3987. begin
  3988. inherited deref;
  3989. tarraysymtable(symtable).deref(false);
  3990. _elementdef:=tdef(_elementdefderef.resolve);
  3991. rangedef:=tdef(rangedefderef.resolve);
  3992. end;
  3993. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3994. begin
  3995. inherited ppuwrite(ppufile);
  3996. ppufile.putderef(_elementdefderef);
  3997. ppufile.putderef(rangedefderef);
  3998. ppufile.putasizeint(lowrange);
  3999. ppufile.putasizeint(highrange);
  4000. ppufile.putset(tppuset2(arrayoptions));
  4001. writeentry(ppufile,ibarraydef);
  4002. tarraysymtable(symtable).ppuwrite(ppufile);
  4003. end;
  4004. function tarraydef.elesize : asizeint;
  4005. begin
  4006. if (ado_IsBitPacked in arrayoptions) then
  4007. internalerror(2006080101);
  4008. if assigned(_elementdef) then
  4009. result:=_elementdef.size
  4010. else
  4011. result:=0;
  4012. end;
  4013. function tarraydef.elepackedbitsize : asizeint;
  4014. begin
  4015. if not(ado_IsBitPacked in arrayoptions) then
  4016. internalerror(2006080102);
  4017. if assigned(_elementdef) then
  4018. result:=_elementdef.packedbitsize
  4019. else
  4020. result:=0;
  4021. end;
  4022. function tarraydef.elecount : asizeuint;
  4023. var
  4024. qhigh,qlow : qword;
  4025. begin
  4026. if ado_IsDynamicArray in arrayoptions then
  4027. begin
  4028. result:=0;
  4029. exit;
  4030. end;
  4031. { check whether the range might be larger than high(asizeint). Has
  4032. to include 0..high(sizeint), since that's high(sizeint)+1 elements }
  4033. if (highrange>=0) and (lowrange<=0) then
  4034. begin
  4035. qhigh:=highrange;
  4036. if lowrange=low(asizeint) then
  4037. qlow:=high(asizeint) + 1
  4038. else
  4039. qlow:=qword(-lowrange);
  4040. { prevent overflow, return 0 to indicate overflow }
  4041. if qhigh+qlow>qword(high(asizeint)-1) then
  4042. result:=0
  4043. else
  4044. result:=qhigh+qlow+1;
  4045. end
  4046. else
  4047. result:=int64(highrange)-lowrange+1;
  4048. end;
  4049. function tarraydef.size : asizeint;
  4050. var
  4051. cachedelecount : asizeuint;
  4052. cachedelesize : asizeint;
  4053. begin
  4054. if ado_IsDynamicArray in arrayoptions then
  4055. begin
  4056. size:=voidpointertype.size;
  4057. exit;
  4058. end;
  4059. { Tarraydef.size may never be called for an open array! }
  4060. if ado_OpenArray in arrayoptions then
  4061. internalerror(99080501);
  4062. if not (ado_IsBitPacked in arrayoptions) then
  4063. cachedelesize:=elesize
  4064. else
  4065. cachedelesize := elepackedbitsize;
  4066. cachedelecount:=elecount;
  4067. if (cachedelesize = 0) then
  4068. begin
  4069. size := 0;
  4070. exit;
  4071. end;
  4072. if (cachedelecount = 0) then
  4073. begin
  4074. if ado_isconststring in arrayoptions then
  4075. size := 0
  4076. else
  4077. size := -1;
  4078. exit;
  4079. end;
  4080. { prevent overflow, return -1 to indicate overflow }
  4081. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  4082. if (cachedelecount > asizeuint(high(asizeint))) or
  4083. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  4084. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  4085. accessing the array, see ncgmem (PFV) }
  4086. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  4087. begin
  4088. result:=-1;
  4089. exit;
  4090. end;
  4091. result:=cachedelesize*asizeint(cachedelecount);
  4092. if (ado_IsBitPacked in arrayoptions) then
  4093. { can't just add 7 and divide by 8, because that may overflow }
  4094. result:=result div 8 + ord((result mod 8)<>0);
  4095. {$ifdef cpu16bitaddr}
  4096. if result>65535 then
  4097. begin
  4098. result:=-1;
  4099. exit;
  4100. end;
  4101. {$endif cpu16bitaddr}
  4102. end;
  4103. procedure tarraydef.setelementdef(def:tdef);
  4104. begin
  4105. _elementdef:=def;
  4106. if not(
  4107. (ado_IsDynamicArray in arrayoptions) or
  4108. (ado_IsConvertedPointer in arrayoptions) or
  4109. (ado_IsConstructor in arrayoptions) or
  4110. (ado_IsGeneric in arrayoptions) or
  4111. (highrange<lowrange)
  4112. ) and
  4113. (size=-1) then
  4114. Message(sym_e_segment_too_large);
  4115. end;
  4116. function tarraydef.alignment : shortint;
  4117. begin
  4118. if ado_IsVector in arrayoptions then
  4119. alignment:=size
  4120. { alignment of dyn. arrays doesn't depend on the element size }
  4121. else if ado_IsDynamicArray in arrayoptions then
  4122. alignment:=voidpointertype.alignment
  4123. { alignment is the target alignment for the used load size }
  4124. else if (ado_IsBitPacked in arrayoptions) and
  4125. (elementdef.typ in [enumdef,orddef]) then
  4126. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  4127. { alignment is the alignment of the elements }
  4128. else
  4129. alignment:=elementdef.alignment
  4130. end;
  4131. function tarraydef.needs_inittable : boolean;
  4132. begin
  4133. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  4134. end;
  4135. function tarraydef.needs_separate_initrtti : boolean;
  4136. begin
  4137. if ado_IsBitPacked in arrayoptions then
  4138. result:=false
  4139. else
  4140. result:=elementdef.needs_separate_initrtti;
  4141. end;
  4142. function tarraydef.GetTypeName : string;
  4143. begin
  4144. if (ado_IsConstString in arrayoptions) then
  4145. result:='Constant String'
  4146. else if (ado_isarrayofconst in arrayoptions) or
  4147. (ado_isConstructor in arrayoptions) then
  4148. begin
  4149. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  4150. GetTypeName:='Array Of Const'
  4151. else
  4152. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  4153. end
  4154. else if (ado_IsDynamicArray in arrayoptions) then
  4155. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  4156. else if (ado_OpenArray in arrayoptions) then
  4157. GetTypeName:='{Open} Array Of '+elementdef.typename
  4158. else
  4159. begin
  4160. result := '';
  4161. if (ado_IsBitPacked in arrayoptions) then
  4162. result:='BitPacked ';
  4163. if rangedef.typ=enumdef then
  4164. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  4165. else
  4166. result:=result+'Array['+tostr(lowrange)+'..'+
  4167. tostr(highrange)+'] Of '+elementdef.typename
  4168. end;
  4169. end;
  4170. function tarraydef.getmangledparaname : TSymStr;
  4171. begin
  4172. if ado_isarrayofconst in arrayoptions then
  4173. getmangledparaname:='array_of_const'
  4174. else
  4175. if ((highrange=-1) and (lowrange=0)) then
  4176. getmangledparaname:='array_of_'+elementdef.mangledparaname
  4177. else
  4178. internalerror(200204176);
  4179. end;
  4180. function tarraydef.is_publishable : tpublishproperty;
  4181. begin
  4182. if ado_IsDynamicArray in arrayoptions then
  4183. is_publishable:=pp_publish
  4184. else
  4185. is_publishable:=pp_error;
  4186. end;
  4187. function tarraydef.is_hwvector: boolean;
  4188. begin
  4189. result:=ado_IsVector in arrayoptions;
  4190. end;
  4191. {***************************************************************************
  4192. tabstractrecorddef
  4193. ***************************************************************************}
  4194. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  4195. begin
  4196. inherited create(dt,doregister);
  4197. objname:=stringdup(upper(n));
  4198. objrealname:=stringdup(n);
  4199. objectoptions:=[];
  4200. if assigned(current_module.namespace) then
  4201. begin
  4202. import_lib:=stringdup(current_module.namespace^);
  4203. replace(import_lib^,'.','/');
  4204. end;
  4205. end;
  4206. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4207. var
  4208. ro: trtti_option;
  4209. begin
  4210. inherited ppuload(dt,ppufile);
  4211. objrealname:=ppufile.getpshortstring;
  4212. objname:=stringdup(upper(objrealname^));
  4213. import_lib:=ppufile.getpshortstring;
  4214. { only used for external C++ classes and Java classes/records }
  4215. if (import_lib^='') then
  4216. stringdispose(import_lib);
  4217. ppufile.getset(tppuset4(objectoptions));
  4218. rtti.clause:=trtti_clause(ppufile.getbyte);
  4219. for ro in trtti_option do
  4220. ppufile.getset(tppuset1(rtti.options[ro]));
  4221. end;
  4222. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  4223. var
  4224. ro: trtti_option;
  4225. begin
  4226. inherited ppuwrite(ppufile);
  4227. ppufile.putstring(objrealname^);
  4228. if assigned(import_lib) then
  4229. ppufile.putstring(import_lib^)
  4230. else
  4231. ppufile.putstring('');
  4232. ppufile.putset(tppuset4(objectoptions));
  4233. ppufile.putbyte(byte(rtti.clause));
  4234. for ro in trtti_option do
  4235. ppufile.putset(tppuset1(rtti.options[ro]));
  4236. end;
  4237. destructor tabstractrecorddef.destroy;
  4238. begin
  4239. stringdispose(objname);
  4240. stringdispose(objrealname);
  4241. stringdispose(import_lib);
  4242. tcinitcode.free;
  4243. inherited destroy;
  4244. end;
  4245. procedure tabstractrecorddef.buildderefimpl;
  4246. begin
  4247. inherited buildderefimpl;
  4248. if not (df_copied_def in defoptions) then
  4249. tstoredsymtable(symtable).buildderefimpl;
  4250. end;
  4251. procedure tabstractrecorddef.derefimpl;
  4252. begin
  4253. inherited derefimpl;
  4254. if not (df_copied_def in defoptions) then
  4255. tstoredsymtable(symtable).derefimpl(false);
  4256. end;
  4257. procedure tabstractrecorddef.check_forwards;
  4258. begin
  4259. { the defs of a copied def are defined for the original type only }
  4260. if not(df_copied_def in defoptions) then
  4261. tstoredsymtable(symtable).check_forwards;
  4262. end;
  4263. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  4264. var
  4265. i: longint;
  4266. sym: tsym;
  4267. begin
  4268. for i:=0 to symtable.SymList.Count-1 do
  4269. begin
  4270. sym:=tsym(symtable.SymList[i]);
  4271. if sym.typ=procsym then
  4272. begin
  4273. result:=tprocsym(sym).find_procdef_bytype(pt);
  4274. if assigned(result) then
  4275. exit;
  4276. end;
  4277. end;
  4278. result:=nil;
  4279. end;
  4280. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  4281. begin
  4282. if t=gs_record then
  4283. GetSymtable:=symtable
  4284. else
  4285. GetSymtable:=nil;
  4286. end;
  4287. function tabstractrecorddef.is_packed:boolean;
  4288. begin
  4289. result:=tabstractrecordsymtable(symtable).is_packed;
  4290. end;
  4291. function tabstractrecorddef.RttiName: string;
  4292. function generate_full_paramname(maxlength:longint):string;
  4293. const
  4294. commacount : array[boolean] of longint = (0,1);
  4295. var
  4296. fullparas,
  4297. paramname : ansistring;
  4298. module : tmodule;
  4299. sym : tsym;
  4300. def : tdef;
  4301. i : longint;
  4302. begin
  4303. { we want at least enough space for an ellipsis }
  4304. if maxlength<3 then
  4305. internalerror(2014121203);
  4306. fullparas:='';
  4307. for i:=0 to genericparas.count-1 do
  4308. begin
  4309. sym:=tsym(genericparas[i]);
  4310. module:=find_module_from_symtable(sym.owner);
  4311. if not assigned(module) then
  4312. internalerror(2014121202);
  4313. if not (sym.typ in [constsym,symconst.typesym]) then
  4314. internalerror(2020042501);
  4315. if sym.typ=constsym then
  4316. def:=tconstsym(sym).constdef
  4317. else
  4318. def:=ttypesym(sym).typedef;
  4319. paramname:=module.realmodulename^;
  4320. if def.typ in [objectdef,recorddef] then
  4321. paramname:=paramname+'.'+tabstractrecorddef(def).rttiname
  4322. else
  4323. paramname:=paramname+'.'+def.typename;
  4324. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  4325. begin
  4326. if i>0 then
  4327. fullparas:=fullparas+',...'
  4328. else
  4329. fullparas:=fullparas+'...';
  4330. break;
  4331. end;
  4332. { could we fit an ellipsis after this parameter if it should be too long? }
  4333. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  4334. begin
  4335. { then omit already this parameter }
  4336. if i>0 then
  4337. fullparas:=fullparas+',...'
  4338. else
  4339. fullparas:=fullparas+'...';
  4340. break;
  4341. end;
  4342. if i>0 then
  4343. fullparas:=fullparas+',';
  4344. fullparas:=fullparas+paramname;
  4345. end;
  4346. result:=fullparas;
  4347. end;
  4348. var
  4349. nongeneric,
  4350. basename : string;
  4351. i,
  4352. remlength,
  4353. paramcount,
  4354. crcidx : longint;
  4355. begin
  4356. if rttistring='' then
  4357. begin
  4358. if is_specialization then
  4359. begin
  4360. rttistring:=OwnerHierarchyName;
  4361. { there should be two $ characters, one before the CRC and one before the count }
  4362. crcidx:=-1;
  4363. for i:=length(objrealname^) downto 1 do
  4364. if objrealname^[i]='$' then
  4365. begin
  4366. crcidx:=i;
  4367. break;
  4368. end;
  4369. if crcidx<0 then
  4370. internalerror(2014121201);
  4371. basename:=copy(objrealname^,1,crcidx-1);
  4372. split_generic_name(basename,nongeneric,paramcount);
  4373. rttistring:=rttistring+nongeneric+'<';
  4374. remlength:=255-length(rttistring)-1;
  4375. if remlength<4 then
  4376. rttistring:=rttistring+'>'
  4377. else
  4378. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  4379. end
  4380. else
  4381. if is_generic then
  4382. begin
  4383. rttistring:=OwnerHierarchyName;
  4384. split_generic_name(objrealname^,nongeneric,paramcount);
  4385. rttistring:=rttistring+nongeneric+'<';
  4386. { we don't want any ',' if there is only one parameter }
  4387. for i:=0 to paramcount-0 do
  4388. rttistring:=rttistring+',';
  4389. rttistring:=rttistring+'>';
  4390. end
  4391. else
  4392. rttistring:=OwnerHierarchyName+objrealname^;
  4393. end;
  4394. result:=rttistring;
  4395. end;
  4396. function tabstractrecorddef.search_enumerator_get: tprocdef;
  4397. var
  4398. sym : tsym;
  4399. i : integer;
  4400. pd : tprocdef;
  4401. hashedid : THashedIDString;
  4402. begin
  4403. result:=nil;
  4404. hashedid.id:='GETENUMERATOR';
  4405. sym:=tsym(symtable.FindWithHash(hashedid));
  4406. if assigned(sym) and (sym.typ=procsym) then
  4407. begin
  4408. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4409. begin
  4410. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4411. if (pd.proctypeoption = potype_function) and
  4412. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  4413. (pd.visibility >= vis_public) then
  4414. begin
  4415. result:=pd;
  4416. exit;
  4417. end;
  4418. end;
  4419. end;
  4420. end;
  4421. function tabstractrecorddef.search_enumerator_move: tprocdef;
  4422. var
  4423. sym : tsym;
  4424. i : integer;
  4425. pd : tprocdef;
  4426. hashedid : THashedIDString;
  4427. begin
  4428. result:=nil;
  4429. // first search for po_enumerator_movenext method modifier
  4430. // then search for public function MoveNext: Boolean
  4431. for i:=0 to symtable.SymList.Count-1 do
  4432. begin
  4433. sym:=TSym(symtable.SymList[i]);
  4434. if (sym.typ=procsym) then
  4435. begin
  4436. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4437. if assigned(pd) then
  4438. begin
  4439. result:=pd;
  4440. exit;
  4441. end;
  4442. end;
  4443. end;
  4444. hashedid.id:='MOVENEXT';
  4445. sym:=tsym(symtable.FindWithHash(hashedid));
  4446. if assigned(sym) and (sym.typ=procsym) then
  4447. begin
  4448. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  4449. begin
  4450. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4451. if (pd.proctypeoption = potype_function) and
  4452. is_boolean(pd.returndef) and
  4453. (pd.minparacount = 0) and
  4454. (pd.visibility >= vis_public) then
  4455. begin
  4456. result:=pd;
  4457. exit;
  4458. end;
  4459. end;
  4460. end;
  4461. end;
  4462. function tabstractrecorddef.search_enumerator_current: tsym;
  4463. var
  4464. sym: tsym;
  4465. i: integer;
  4466. hashedid : THashedIDString;
  4467. begin
  4468. result:=nil;
  4469. // first search for ppo_enumerator_current property modifier
  4470. // then search for public property Current
  4471. for i:=0 to symtable.SymList.Count-1 do
  4472. begin
  4473. sym:=TSym(symtable.SymList[i]);
  4474. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4475. begin
  4476. result:=sym;
  4477. exit;
  4478. end;
  4479. end;
  4480. hashedid.id:='CURRENT';
  4481. sym:=tsym(symtable.FindWithHash(hashedid));
  4482. if assigned(sym) and (sym.typ=propertysym) and
  4483. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4484. begin
  4485. result:=sym;
  4486. exit;
  4487. end;
  4488. end;
  4489. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  4490. var
  4491. st: tsymtable;
  4492. enclosingdef: tdef;
  4493. begin
  4494. if typ=objectdef then
  4495. result:=tobjectdef(self).objextname^
  4496. else if assigned(typesym) then
  4497. result:=typesym.realname
  4498. { have to generate anonymous nested type in current unit/class/record }
  4499. else
  4500. internalerror(2011032601);
  4501. { in case of specializations, add some extras to prevent name conflicts
  4502. with nested classes }
  4503. if df_specialization in defoptions then
  4504. result:='$'+result+'$specialization$';
  4505. st:=owner;
  4506. while assigned(st) and
  4507. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  4508. begin
  4509. { nested classes are named as "OuterClass$InnerClass" }
  4510. enclosingdef:=tdef(st.defowner);
  4511. if enclosingdef.typ=procdef then
  4512. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  4513. else if enclosingdef.typ=objectdef then
  4514. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  4515. else if assigned(enclosingdef.typesym) then
  4516. result:=enclosingdef.typesym.realname+'$'+result
  4517. else
  4518. internalerror(2011060305);
  4519. st:=enclosingdef.owner;
  4520. end;
  4521. if with_package_name and
  4522. assigned(import_lib) then
  4523. result:=import_lib^+'/'+result;
  4524. end;
  4525. function tabstractrecorddef.contains_float_field: boolean;
  4526. var
  4527. i : longint;
  4528. begin
  4529. result:=true;
  4530. for i:=0 to symtable.symlist.count-1 do
  4531. begin
  4532. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4533. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4534. continue;
  4535. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4536. begin
  4537. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  4538. exit;
  4539. { search recursively }
  4540. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4541. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  4542. exit;
  4543. end;
  4544. end;
  4545. result:=false;
  4546. end;
  4547. function tabstractrecorddef.contains_cross_aword_field: boolean;
  4548. var
  4549. i : longint;
  4550. foffset, fsize: aword;
  4551. begin
  4552. result:=true;
  4553. for i:=0 to symtable.symlist.count-1 do
  4554. begin
  4555. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4556. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4557. continue;
  4558. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4559. begin
  4560. if is_packed then
  4561. begin
  4562. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  4563. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  4564. end
  4565. else
  4566. begin
  4567. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  4568. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  4569. end;
  4570. if (fsize>0) and ((foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8))) then
  4571. exit;
  4572. { search recursively }
  4573. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4574. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  4575. exit;
  4576. end;
  4577. end;
  4578. result:=false;
  4579. end;
  4580. procedure tabstractrecorddef.apply_rtti_directive(dir: trtti_directive);
  4581. begin
  4582. { records don't support the inherit clause but shouldn't
  4583. give an error either if used (for Delphi compatibility),
  4584. so we silently enforce the clause as explicit. }
  4585. rtti.clause:=rtc_explicit;
  4586. rtti.options:=dir.options;
  4587. end;
  4588. function tabstractrecorddef.is_visible_for_rtti(option: trtti_option; vis: tvisibility): boolean;
  4589. begin
  4590. case vis of
  4591. vis_private,
  4592. vis_strictprivate: result:=rv_private in rtti.options[option];
  4593. vis_protected,
  4594. vis_strictprotected: result:=rv_protected in rtti.options[option];
  4595. vis_public: result:=rv_public in rtti.options[option];
  4596. vis_published: result:=rv_published in rtti.options[option];
  4597. otherwise
  4598. result:=false;
  4599. end;
  4600. end;
  4601. function tabstractrecorddef.rtti_visibilities_for_option(option: trtti_option): tvisibilities;
  4602. begin
  4603. result:=[];
  4604. if rv_private in rtti.options[option] then
  4605. begin
  4606. include(result,vis_private);
  4607. include(result,vis_strictprivate);
  4608. end;
  4609. if rv_protected in rtti.options[option] then
  4610. begin
  4611. include(result,vis_protected);
  4612. include(result,vis_strictprotected);
  4613. end;
  4614. if rv_public in rtti.options[option] then
  4615. include(result,vis_public);
  4616. if rv_published in rtti.options[option] then
  4617. include(result,vis_published);
  4618. end;
  4619. function tabstractrecorddef.has_extended_rtti: boolean;
  4620. begin
  4621. result := (rtti.options[ro_fields]<>[]) or
  4622. (rtti.options[ro_methods]<>[]) or
  4623. (rtti.options[ro_properties]<>[]);
  4624. end;
  4625. {$ifdef DEBUG_NODE_XML}
  4626. procedure tabstractrecorddef.XMLPrintDefData(var T: Text; Sym: TSym);
  4627. procedure WriteSymOptions(SourceSym: TSym);
  4628. var
  4629. i: TSymOption;
  4630. first: Boolean;
  4631. begin
  4632. First := True;
  4633. for i := Low(TSymOption) to High(TSymOption) do
  4634. if i in SourceSym.symoptions then
  4635. begin
  4636. if First then
  4637. begin
  4638. Write(T, '" symoptions="', i);
  4639. First := False;
  4640. end
  4641. else
  4642. Write(T, ',', i)
  4643. end;
  4644. end;
  4645. var
  4646. List: TFPHashObjectList;
  4647. i: Integer;
  4648. begin
  4649. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  4650. if (alignment = structalignment) and (alignment = aggregatealignment) then
  4651. begin
  4652. { Straightforward and simple }
  4653. WriteLn(T, PrintNodeIndention, '<alignment>', alignment, '</alignment>');
  4654. end
  4655. else
  4656. begin
  4657. WriteLn(T, PrintNodeIndention, '<alignment>');
  4658. printnodeindent;
  4659. WriteLn(T, PrintNodeIndention, '<basic>', alignment, '</basic>');
  4660. if (structalignment <> alignment) then
  4661. WriteLn(T, PrintNodeIndention, '<struct>', structalignment, '</struct>');
  4662. if (aggregatealignment <> alignment) and (aggregatealignment <> structalignment) then
  4663. WriteLn(T, PrintNodeIndention, '<aggregate>', aggregatealignment, '</aggregate>');
  4664. printnodeunindent;
  4665. WriteLn(T, PrintNodeIndention, '</alignment>');
  4666. end;
  4667. { List the fields }
  4668. List := Symtable.SymList;
  4669. for i := 0 to List.Count - 1 do
  4670. case TSym(List[i]).typ of
  4671. { staticvarsym,localvarsym,paravarsym,fieldvarsym,
  4672. typesym,procsym,unitsym,}
  4673. constsym:
  4674. with TConstSym(List[i]) do
  4675. begin
  4676. Write(T, PrintNodeIndention, '<const name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4677. WriteSymOptions(TSym(List[i]));
  4678. WriteLn(T, '">');
  4679. PrintNodeIndent;
  4680. XMLPrintConstData(T);
  4681. PrintNodeUnindent;
  4682. WriteLn(T, PrintNodeIndention, '</const>');
  4683. end;
  4684. {
  4685. errorsym,syssym,labelsym,absolutevarsym,propertysym,
  4686. macrosym,namespacesym,undefinedsym,programparasym
  4687. }
  4688. fieldvarsym:
  4689. with TFieldVarSym(List[i]) do
  4690. begin
  4691. Write(T, PrintNodeIndention, '<field name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4692. WriteSymOptions(TSym(List[i]));
  4693. WriteLn(T, '">');
  4694. PrintNodeIndent;
  4695. XMLPrintFieldData(T);
  4696. PrintNodeUnindent;
  4697. WriteLn(T, PrintNodeIndention, '</field>');
  4698. end;
  4699. else
  4700. ;
  4701. end;
  4702. end;
  4703. {$endif DEBUG_NODE_XML}
  4704. {***************************************************************************
  4705. trecorddef
  4706. ***************************************************************************}
  4707. constructor trecorddef.create(const n:string; p:TSymtable);
  4708. begin
  4709. inherited create(n,recorddef,true);
  4710. symtable:=p;
  4711. { we can own the symtable only if nobody else owns a copy so far }
  4712. if symtable.refcount=1 then
  4713. symtable.defowner:=self;
  4714. isunion:=false;
  4715. cloneddefderef.reset;
  4716. end;
  4717. constructor trecorddef.create_internal(const n: string; packrecords, recordalignmin: shortint; where: tsymtable);
  4718. var
  4719. name : string;
  4720. pname : pshortstring;
  4721. oldsymtablestack: tsymtablestack;
  4722. ts: ttypesym;
  4723. begin
  4724. { construct name }
  4725. if n<>'' then
  4726. pname:=@n
  4727. else
  4728. begin
  4729. init_defid;
  4730. name:='$InternalRec'+unique_id_str;
  4731. pname:=@name;
  4732. end;
  4733. oldsymtablestack:=symtablestack;
  4734. { do not simply push/pop current_module.localsymtable, because
  4735. that can have side-effects (e.g., it removes helpers) }
  4736. symtablestack:=nil;
  4737. symtable:=trecordsymtable.create(pname^,packrecords,recordalignmin);
  4738. symtable.defowner:=self;
  4739. isunion:=false;
  4740. inherited create(pname^,recorddef,true);
  4741. where.insertdef(self);
  4742. { if we specified a name, then we'll probably want to look up the
  4743. type again by name too -> create typesym }
  4744. if n<>'' then
  4745. begin
  4746. ts:=ctypesym.create(n,self);
  4747. include(ts.symoptions,sp_internal);
  4748. { avoid hints about unused types (these may only be used for
  4749. typed constant data) }
  4750. ts.increfcount;
  4751. where.insertsym(ts);
  4752. end;
  4753. symtablestack:=oldsymtablestack;
  4754. { don't create RTTI for internal types, these are not exported }
  4755. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  4756. include(defoptions,df_internal);
  4757. end;
  4758. constructor trecorddef.create_global_internal(const n: string; packrecords, recordalignmin: shortint);
  4759. var
  4760. where : tsymtable;
  4761. begin
  4762. where:=current_module.localsymtable;
  4763. if not assigned(where) then
  4764. where:=current_module.globalsymtable;
  4765. create_internal(n,packrecords,recordalignmin,where);
  4766. end;
  4767. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  4768. var
  4769. sym: tfieldvarsym;
  4770. name: TIDString;
  4771. pname: ^TIDString;
  4772. begin
  4773. if optionalname='' then
  4774. begin
  4775. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  4776. pname:=@name
  4777. end
  4778. else
  4779. pname:=@optionalname;
  4780. sym:=cfieldvarsym.create(pname^,vs_value,def,[]);
  4781. symtable.insertsym(sym);
  4782. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4783. result:=sym;
  4784. end;
  4785. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4786. var
  4787. i: longint;
  4788. begin
  4789. for i:=0 to fieldtypes.count-1 do
  4790. add_field_by_def('',tdef(fieldtypes[i]));
  4791. end;
  4792. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4793. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4794. var
  4795. i,j : longint;
  4796. begin
  4797. if ppufile.getbyte=1 then
  4798. begin
  4799. new(variantrecdesc);
  4800. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4801. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4802. for i:=0 to high(variantrecdesc^.branches) do
  4803. begin
  4804. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4805. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4806. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4807. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4808. end;
  4809. end
  4810. else
  4811. variantrecdesc:=nil;
  4812. end;
  4813. begin
  4814. inherited ppuload(recorddef,ppufile);
  4815. if df_copied_def in defoptions then
  4816. begin
  4817. ppufile.getderef(cloneddefderef);
  4818. ppuload_platform(ppufile);
  4819. end
  4820. else
  4821. begin
  4822. symtable:=trecordsymtable.create(objrealname^,0,0);
  4823. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4824. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4825. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4826. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4827. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4828. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4829. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4830. ppufile.getset(tppuset1(trecordsymtable(symtable).managementoperators));
  4831. { position of ppuload_platform call must correspond
  4832. to position of writeentry in ppuwrite method }
  4833. ppuload_platform(ppufile);
  4834. trecordsymtable(symtable).ppuload(ppufile);
  4835. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4836. but because iso mode supports no units, there is no need to store the variantrecdesc
  4837. in the ppu
  4838. }
  4839. // readvariantrecdesc(variantrecdesc);
  4840. { requires usefieldalignment to be set }
  4841. symtable.defowner:=self;
  4842. end;
  4843. isunion:=false;
  4844. end;
  4845. destructor trecorddef.destroy;
  4846. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4847. var
  4848. i : longint;
  4849. begin
  4850. while assigned(variantrecdesc) do
  4851. begin
  4852. for i:=0 to high(variantrecdesc^.branches) do
  4853. begin
  4854. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4855. SetLength(variantrecdesc^.branches[i].values,0);
  4856. end;
  4857. SetLength(variantrecdesc^.branches,0);
  4858. dispose(variantrecdesc);
  4859. variantrecdesc:=nil;
  4860. end;
  4861. end;
  4862. begin
  4863. if assigned(variantrecdesc) then
  4864. free_variantrecdesc(variantrecdesc);
  4865. if assigned(symtable) then
  4866. begin
  4867. symtable.free;
  4868. symtable:=nil;
  4869. end;
  4870. inherited destroy;
  4871. end;
  4872. function trecorddef.getcopy : tstoreddef;
  4873. begin
  4874. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4875. trecorddef(result).isunion:=isunion;
  4876. include(trecorddef(result).defoptions,df_copied_def);
  4877. if assigned(tcinitcode) then
  4878. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4879. if assigned(import_lib) then
  4880. trecorddef(result).import_lib:=stringdup(import_lib^);
  4881. end;
  4882. function trecorddef.needs_inittable : boolean;
  4883. begin
  4884. { each record with managed field or with any management operator needs
  4885. init table }
  4886. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4887. trecordsymtable(symtable).needs_init_final
  4888. end;
  4889. function trecorddef.needs_separate_initrtti : boolean;
  4890. begin
  4891. result:=true;
  4892. end;
  4893. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4894. begin
  4895. result:=trecordsymtable(symtable).has_non_trivial_init;
  4896. end;
  4897. procedure trecorddef.buildderef;
  4898. begin
  4899. inherited buildderef;
  4900. if df_copied_def in defoptions then
  4901. cloneddefderef.build(symtable.defowner)
  4902. else
  4903. tstoredsymtable(symtable).buildderef;
  4904. end;
  4905. procedure trecorddef.deref;
  4906. begin
  4907. inherited deref;
  4908. { now dereference the definitions }
  4909. if df_copied_def in defoptions then
  4910. begin
  4911. cloneddef:=trecorddef(cloneddefderef.resolve);
  4912. symtable:=cloneddef.symtable.getcopy;
  4913. end
  4914. else
  4915. tstoredsymtable(symtable).deref(false);
  4916. { internal types, only load from the system unit }
  4917. if assigned(owner) and
  4918. assigned(owner.name) and
  4919. (owner.name^='SYSTEM') then
  4920. begin
  4921. { TGUID }
  4922. if not assigned(rec_tguid) and
  4923. (upper(typename)='TGUID') then
  4924. rec_tguid:=self
  4925. { JMP_BUF }
  4926. else if not assigned(rec_jmp_buf) and
  4927. (upper(typename)='JMP_BUF') then
  4928. rec_jmp_buf:=self
  4929. else if not assigned(rec_exceptaddr) and
  4930. (upper(typename)='TEXCEPTADDR') then
  4931. rec_exceptaddr:=self;
  4932. end;
  4933. end;
  4934. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4935. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4936. var
  4937. i,j : longint;
  4938. begin
  4939. if assigned(variantrecdesc) then
  4940. begin
  4941. ppufile.putbyte(1);
  4942. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4943. ppufile.putasizeint(length(variantrecdesc^.branches));
  4944. for i:=0 to high(variantrecdesc^.branches) do
  4945. begin
  4946. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4947. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4948. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4949. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4950. end;
  4951. end
  4952. else
  4953. ppufile.putbyte(0);
  4954. end;
  4955. begin
  4956. inherited ppuwrite(ppufile);
  4957. if df_copied_def in defoptions then
  4958. ppufile.putderef(cloneddefderef)
  4959. else
  4960. begin
  4961. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4962. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4963. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4964. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4965. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4966. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4967. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4968. ppufile.putset(tppuset1(trecordsymtable(symtable).managementoperators));
  4969. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4970. but because iso mode supports no units, there is no need to store the variantrecdesc
  4971. in the ppu
  4972. }
  4973. // writevariantrecdesc(variantrecdesc);
  4974. end;
  4975. writeentry(ppufile,ibrecorddef);
  4976. if not(df_copied_def in defoptions) then
  4977. trecordsymtable(symtable).ppuwrite(ppufile);
  4978. end;
  4979. function trecorddef.size:asizeint;
  4980. begin
  4981. result:=trecordsymtable(symtable).datasize;
  4982. end;
  4983. function trecorddef.alignment:shortint;
  4984. begin
  4985. alignment:=trecordsymtable(symtable).recordalignment;
  4986. end;
  4987. function trecorddef.padalignment:shortint;
  4988. begin
  4989. padalignment := trecordsymtable(symtable).padalignment;
  4990. end;
  4991. function trecorddef.GetTypeName : string;
  4992. begin
  4993. GetTypeName:='<record type>'
  4994. end;
  4995. {$ifdef DEBUG_NODE_XML}
  4996. function TRecordDef.XMLPrintType: ansistring;
  4997. begin
  4998. Result := '&lt;record&gt;';
  4999. end;
  5000. {$endif DEBUG_NODE_XML}
  5001. {***************************************************************************
  5002. TABSTRACTPROCDEF
  5003. ***************************************************************************}
  5004. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  5005. begin
  5006. inherited create(dt,doregister);
  5007. parast:=tparasymtable.create(self,level);
  5008. paras:=nil;
  5009. minparacount:=0;
  5010. maxparacount:=0;
  5011. proctypeoption:=potype_none;
  5012. proccalloption:=pocall_none;
  5013. procoptions:=[];
  5014. returndef:=voidtype;
  5015. returndefderef.reset;
  5016. savesize:=sizeof(pint);
  5017. callerargareasize:=0;
  5018. calleeargareasize:=0;
  5019. has_paraloc_info:=callnoside;
  5020. funcretloc[callerside].init;
  5021. funcretloc[calleeside].init;
  5022. check_mark_as_nested;
  5023. end;
  5024. destructor tabstractprocdef.destroy;
  5025. begin
  5026. if assigned(paras) then
  5027. begin
  5028. {$ifdef MEMDEBUG}
  5029. memprocpara.start;
  5030. {$endif MEMDEBUG}
  5031. paras.free;
  5032. paras:=nil;
  5033. {$ifdef MEMDEBUG}
  5034. memprocpara.stop;
  5035. {$endif MEMDEBUG}
  5036. end;
  5037. if assigned(parast) then
  5038. begin
  5039. {$ifdef MEMDEBUG}
  5040. memprocparast.start;
  5041. {$endif MEMDEBUG}
  5042. parast.free;
  5043. parast:=nil;
  5044. {$ifdef MEMDEBUG}
  5045. memprocparast.stop;
  5046. {$endif MEMDEBUG}
  5047. end;
  5048. funcretloc[callerside].done;
  5049. funcretloc[calleeside].done;
  5050. inherited destroy;
  5051. end;
  5052. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  5053. begin
  5054. if (tsym(p).typ<>paravarsym) then
  5055. exit;
  5056. inc(plongint(arg)^);
  5057. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  5058. begin
  5059. if not assigned(tparavarsym(p).defaultconstsym) then
  5060. inc(minparacount);
  5061. inc(maxparacount);
  5062. end;
  5063. end;
  5064. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  5065. begin
  5066. if (tsym(p).typ<>paravarsym) then
  5067. exit;
  5068. paras.add(p);
  5069. end;
  5070. procedure tabstractprocdef.calcparas;
  5071. var
  5072. paracount : longint;
  5073. begin
  5074. { This can already be assigned when
  5075. we need to reresolve this unit (PFV) }
  5076. if assigned(paras) then
  5077. paras.free;
  5078. paras:=tparalist.create(false);
  5079. paracount:=0;
  5080. minparacount:=0;
  5081. maxparacount:=0;
  5082. parast.SymList.ForEachCall(@count_para,@paracount);
  5083. paras.capacity:=paracount;
  5084. { Insert parameters in table }
  5085. parast.SymList.ForEachCall(@insert_para,nil);
  5086. { Order parameters }
  5087. paras.sortparas;
  5088. end;
  5089. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  5090. var
  5091. hash : qword;
  5092. hp : TParavarsym;
  5093. hs : TSymStr;
  5094. newlen,
  5095. i : integer;
  5096. begin
  5097. result:='';
  5098. hp:=nil;
  5099. { add parameter types }
  5100. for i:=0 to paras.count-1 do
  5101. begin
  5102. hp:=tparavarsym(paras[i]);
  5103. if not(vo_is_hidden_para in hp.varoptions) then
  5104. result:=result+'$'+hp.vardef.mangledparaname;
  5105. end;
  5106. { add resultdef, add $$ as separator to make it unique from a
  5107. parameter separator }
  5108. if not is_void(returndef) then
  5109. result:=result+'$$'+returndef.mangledparaname;
  5110. newlen:=length(result)+oldlen;
  5111. { Replace with hash if the parameter line is very long }
  5112. if (newlen-oldlen>12) and
  5113. ((newlen>100) or (newlen-oldlen>64)) then
  5114. begin
  5115. hash:=InitFnv64;
  5116. for i:=0 to paras.count-1 do
  5117. begin
  5118. hp:=tparavarsym(paras[i]);
  5119. if not(vo_is_hidden_para in hp.varoptions) then
  5120. begin
  5121. hs:=hp.vardef.mangledparaname;
  5122. hash:=UpdateFnv64(hash,hs[1],length(hs));
  5123. end;
  5124. end;
  5125. if not is_void(returndef) then
  5126. begin
  5127. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  5128. hs:='$$'+returndef.mangledparaname;
  5129. hash:=UpdateFnv64(hash,hs[1],length(hs));
  5130. end;
  5131. result:='$h'+Base64Mangle(hash);
  5132. end;
  5133. end;
  5134. procedure tabstractprocdef.buildderef;
  5135. begin
  5136. { released procdef? }
  5137. if not assigned(parast) then
  5138. exit;
  5139. inherited buildderef;
  5140. returndefderef.build(returndef);
  5141. if po_explicitparaloc in procoptions then
  5142. funcretloc[callerside].buildderef;
  5143. { parast }
  5144. tparasymtable(parast).buildderef;
  5145. end;
  5146. procedure tabstractprocdef.deref;
  5147. begin
  5148. inherited deref;
  5149. returndef:=tdef(returndefderef.resolve);
  5150. if po_explicitparaloc in procoptions then
  5151. begin
  5152. funcretloc[callerside].deref;
  5153. has_paraloc_info:=callerside;
  5154. end
  5155. else
  5156. begin
  5157. { deref is called after loading from a ppu, but also after another
  5158. unit has been reloaded/recompiled and all references must be
  5159. re-resolved. Since the funcretloc contains a reference to a tdef,
  5160. reset it so that we won't try to access the stale def }
  5161. funcretloc[callerside].init;
  5162. has_paraloc_info:=callnoside;
  5163. end;
  5164. { parast }
  5165. tparasymtable(parast).deref(false);
  5166. { recalculated parameters }
  5167. calcparas;
  5168. end;
  5169. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  5170. begin
  5171. inherited ppuload(dt,ppufile);
  5172. parast:=nil;
  5173. Paras:=nil;
  5174. minparacount:=0;
  5175. maxparacount:=0;
  5176. ppufile.getderef(returndefderef);
  5177. proctypeoption:=tproctypeoption(ppufile.getbyte);
  5178. proccalloption:=tproccalloption(ppufile.getbyte);
  5179. ppufile.getset(tppuset8(procoptions));
  5180. funcretloc[callerside].init;
  5181. if po_explicitparaloc in procoptions then
  5182. funcretloc[callerside].ppuload(ppufile);
  5183. savesize:=sizeof(pint);
  5184. if (po_explicitparaloc in procoptions) then
  5185. has_paraloc_info:=callerside;
  5186. end;
  5187. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  5188. var
  5189. oldintfcrc : boolean;
  5190. begin
  5191. { released procdef? }
  5192. if not assigned(parast) then
  5193. exit;
  5194. inherited ppuwrite(ppufile);
  5195. ppufile.putderef(returndefderef);
  5196. oldintfcrc:=ppufile.do_interface_crc;
  5197. ppufile.do_interface_crc:=false;
  5198. ppufile.putbyte(ord(proctypeoption));
  5199. ppufile.putbyte(ord(proccalloption));
  5200. ppufile.putset(tppuset8(procoptions));
  5201. ppufile.do_interface_crc:=oldintfcrc;
  5202. if (po_explicitparaloc in procoptions) then
  5203. funcretloc[callerside].ppuwrite(ppufile);
  5204. end;
  5205. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  5206. var
  5207. hs,s : ansistring;
  5208. hp : TParavarsym;
  5209. hpc : tconstsym;
  5210. first : boolean;
  5211. i,j : integer;
  5212. begin
  5213. s:='';
  5214. first:=true;
  5215. for i:=0 to paras.count-1 do
  5216. begin
  5217. hp:=tparavarsym(paras[i]);
  5218. if not(vo_is_hidden_para in hp.varoptions) or
  5219. (pno_showhidden in pno) then
  5220. begin
  5221. if first then
  5222. begin
  5223. s:=s+'(';
  5224. first:=false;
  5225. end
  5226. else
  5227. s:=s+';';
  5228. if vo_is_hidden_para in hp.varoptions then
  5229. s:=s+'<';
  5230. case hp.varspez of
  5231. vs_var :
  5232. s:=s+'var ';
  5233. vs_const :
  5234. s:=s+'const ';
  5235. vs_out :
  5236. s:=s+'out ';
  5237. vs_constref :
  5238. s:=s+'constref ';
  5239. else
  5240. ;
  5241. end;
  5242. if (pno_paranames in pno) then
  5243. s:=s+hp.realname+':';
  5244. if hp.univpara then
  5245. s:=s+'univ ';
  5246. if assigned(hp.vardef.typesym) then
  5247. begin
  5248. hs:=hp.vardef.typesym.realname;
  5249. if hs[1]<>'$' then
  5250. s:=s+hp.vardef.OwnerHierarchyName+hs
  5251. else
  5252. s:=s+hp.vardef.GetTypeName;
  5253. end
  5254. else
  5255. s:=s+hp.vardef.GetTypeName;
  5256. { default value }
  5257. if assigned(hp.defaultconstsym) then
  5258. begin
  5259. hpc:=tconstsym(hp.defaultconstsym);
  5260. hs:='';
  5261. case hpc.consttyp of
  5262. constwresourcestring,
  5263. constwstring:
  5264. begin
  5265. if hpc.value.valuews.len>0 then
  5266. begin
  5267. setlength(hs,hpc.value.valuews.len);
  5268. for j:=0 to hpc.value.valuews.len-1 do
  5269. begin
  5270. if (ord(hpc.value.valuews.data[j])<127) and
  5271. not(byte(hpc.value.valuews.data[j]) in [0,10,13]) then
  5272. hs[j+1]:=char(hpc.value.valuews.data[j])
  5273. else
  5274. hs[j+1]:='.';
  5275. end;
  5276. end;
  5277. end;
  5278. conststring,
  5279. constresourcestring :
  5280. begin
  5281. if hpc.value.len>0 then
  5282. begin
  5283. setLength(hs,hpc.value.len);
  5284. { don't write past the end of hs if the constant
  5285. is > 255 chars }
  5286. move(hpc.value.valueptr^,hs[1],length(hs));
  5287. { make sure that constant strings with newline chars
  5288. don't create a linebreak in the assembler code,
  5289. since comments are line-based. Also remove nulls
  5290. because the comments are written as a pchar. }
  5291. ReplaceCase(hs,#0,'.');
  5292. ReplaceCase(hs,#10,'.');
  5293. ReplaceCase(hs,#13,'.');
  5294. end;
  5295. end;
  5296. constreal :
  5297. str(pbestreal(hpc.value.valueptr)^,hs);
  5298. constpointer :
  5299. hs:=tostr(hpc.value.valueordptr);
  5300. constord :
  5301. begin
  5302. if is_boolean(hpc.constdef) then
  5303. begin
  5304. if hpc.value.valueord<>0 then
  5305. hs:='TRUE'
  5306. else
  5307. hs:='FALSE';
  5308. end
  5309. else
  5310. hs:=tostr(hpc.value.valueord);
  5311. end;
  5312. constnil :
  5313. hs:='nil';
  5314. constset :
  5315. hs:='<set>';
  5316. constguid:
  5317. hs:=guid2string(pguid(hpc.value.valueptr)^);
  5318. constnone:
  5319. internalerror(2019050704);
  5320. end;
  5321. if hs<>'' then
  5322. s:=s+'=`'+hs+'`';
  5323. end;
  5324. if vo_is_hidden_para in hp.varoptions then
  5325. s:=s+'>';
  5326. end;
  5327. end;
  5328. if not first then
  5329. s:=s+')';
  5330. if (po_varargs in procoptions) then
  5331. s:=s+';VarArgs';
  5332. typename_paras:=s;
  5333. end;
  5334. function tabstractprocdef.is_methodpointer:boolean;
  5335. begin
  5336. result:=false;
  5337. end;
  5338. function tabstractprocdef.is_addressonly:boolean;
  5339. begin
  5340. result:=true;
  5341. end;
  5342. function tabstractprocdef.no_self_node: boolean;
  5343. begin
  5344. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  5345. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  5346. end;
  5347. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  5348. var
  5349. j, nestinglevel: longint;
  5350. pvs, npvs: tparavarsym;
  5351. begin
  5352. nestinglevel:=parast.symtablelevel;
  5353. if newtyp=procdef then
  5354. begin
  5355. if (copytyp<>pc_bareproc) then
  5356. result:=cprocdef.create(nestinglevel,doregister)
  5357. else
  5358. result:=cprocdef.create(normal_function_level,doregister);
  5359. tprocdef(result).visibility:=vis_public;
  5360. end
  5361. else
  5362. begin
  5363. result:=cprocvardef.create(nestinglevel,true);
  5364. end;
  5365. tabstractprocdef(result).returndef:=returndef;
  5366. tabstractprocdef(result).returndefderef:=returndefderef;
  5367. pvs:=nil;
  5368. npvs:=nil;
  5369. if copytyp<>pc_normal_no_paras then
  5370. begin
  5371. for j:=0 to parast.symlist.count-1 do
  5372. begin
  5373. case tsym(parast.symlist[j]).typ of
  5374. paravarsym:
  5375. begin
  5376. pvs:=tparavarsym(parast.symlist[j]);
  5377. { in case of bare proc, don't copy self, vmt or framepointer
  5378. parameters }
  5379. if (copytyp in [pc_bareproc,pc_normal_no_hidden]) and
  5380. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret,vo_is_high_para]*pvs.varoptions)<>[]) then
  5381. continue;
  5382. if paraprefix='' then
  5383. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  5384. pvs.vardef,pvs.varoptions)
  5385. else if not(vo_is_high_para in pvs.varoptions) then
  5386. npvs:=cparavarsym.create(paraprefix+pvs.realname,pvs.paranr,pvs.varspez,
  5387. pvs.vardef,pvs.varoptions)
  5388. else
  5389. npvs:=cparavarsym.create('$high'+paraprefix+copy(pvs.name,5,length(pvs.name)),pvs.paranr,pvs.varspez,
  5390. pvs.vardef,pvs.varoptions);
  5391. npvs.defaultconstsym:=pvs.defaultconstsym;
  5392. tabstractprocdef(result).parast.insertsym(npvs);
  5393. end;
  5394. constsym:
  5395. begin
  5396. // ignore, reuse original constym. Should also be duplicated
  5397. // be safe though
  5398. end;
  5399. symconst.typesym:
  5400. begin
  5401. // reuse original, part of generic declaration
  5402. end
  5403. else
  5404. internalerror(201160604);
  5405. end;
  5406. end;
  5407. end;
  5408. tabstractprocdef(result).savesize:=savesize;
  5409. if (typ<>procvardef) and
  5410. (newtyp=procvardef) then
  5411. begin
  5412. { procvars can't be (class)constructures/destructors etc }
  5413. if proctypeoption=potype_constructor then
  5414. begin
  5415. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  5416. if not(is_implicit_pointer_object_type(returndef) or
  5417. (returndef.typ<>objectdef)) then
  5418. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  5419. tabstractprocdef(result).proctypeoption:=potype_function;
  5420. end
  5421. else if is_void(returndef) then
  5422. tabstractprocdef(result).proctypeoption:=potype_procedure
  5423. else
  5424. tabstractprocdef(result).proctypeoption:=potype_function;
  5425. end
  5426. else
  5427. tabstractprocdef(result).proctypeoption:=proctypeoption;
  5428. tabstractprocdef(result).proccalloption:=proccalloption;
  5429. tabstractprocdef(result).procoptions:=procoptions;
  5430. if (copytyp=pc_bareproc) then
  5431. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  5432. if newtyp=procvardef then
  5433. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  5434. if copytyp=pc_address_only then
  5435. include(tabstractprocdef(result).procoptions,po_addressonly);
  5436. tabstractprocdef(result).callerargareasize:=callerargareasize;
  5437. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  5438. tabstractprocdef(result).maxparacount:=maxparacount;
  5439. tabstractprocdef(result).minparacount:=minparacount;
  5440. if po_explicitparaloc in procoptions then
  5441. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  5442. { recalculate parameter info }
  5443. tabstractprocdef(result).has_paraloc_info:=callnoside;
  5444. {$ifdef m68k}
  5445. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  5446. {$endif}
  5447. if (typ=procdef) and
  5448. (newtyp=procvardef) and
  5449. (owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  5450. include(tprocvardef(result).procoptions,po_methodpointer);
  5451. end;
  5452. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  5453. begin
  5454. result:=is_addressonly;
  5455. end;
  5456. procedure tabstractprocdef.check_mark_as_nested;
  5457. begin
  5458. { nested procvars require that nested functions use the Delphi-style
  5459. nested procedure calling convention }
  5460. if (parast.symtablelevel>normal_function_level) and
  5461. (m_nested_procvars in current_settings.modeswitches) then
  5462. include(procoptions,po_delphi_nested_cc);
  5463. end;
  5464. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  5465. begin
  5466. if (side in [callerside,callbothsides]) and
  5467. not(has_paraloc_info in [callerside,callbothsides]) then
  5468. begin
  5469. if not is_c_variadic(self) then
  5470. callerargareasize:=paramanager.create_paraloc_info(self,callerside)
  5471. else
  5472. callerargareasize:=paramanager.create_varargs_paraloc_info(self,callerside,nil);
  5473. if has_paraloc_info in [calleeside,callbothsides] then
  5474. has_paraloc_info:=callbothsides
  5475. else
  5476. has_paraloc_info:=callerside;
  5477. end;
  5478. if (side in [calleeside,callbothsides]) and
  5479. not(has_paraloc_info in [calleeside,callbothsides]) then
  5480. begin
  5481. if not is_c_variadic(self) then
  5482. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside)
  5483. else
  5484. callerargareasize:=paramanager.create_varargs_paraloc_info(self,calleeside,nil);
  5485. if has_paraloc_info in [callerside,callbothsides] then
  5486. has_paraloc_info:=callbothsides
  5487. else
  5488. has_paraloc_info:=calleeside;
  5489. end;
  5490. end;
  5491. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  5492. var
  5493. i: longint;
  5494. begin
  5495. if (side in [callerside,callbothsides]) and
  5496. (has_paraloc_info in [callerside,callbothsides]) then
  5497. begin
  5498. funcretloc[callerside].done;
  5499. for i:=0 to paras.count-1 do
  5500. tparavarsym(paras[i]).paraloc[callerside].done;
  5501. if has_paraloc_info=callerside then
  5502. has_paraloc_info:=callnoside
  5503. else
  5504. has_paraloc_info:=calleeside;
  5505. end;
  5506. if (side in [calleeside,callbothsides]) and
  5507. (has_paraloc_info in [calleeside,callbothsides]) then
  5508. begin
  5509. funcretloc[calleeside].done;
  5510. for i:=0 to paras.count-1 do
  5511. tparavarsym(paras[i]).paraloc[calleeside].done;
  5512. if has_paraloc_info=calleeside then
  5513. has_paraloc_info:=callnoside
  5514. else
  5515. has_paraloc_info:=callerside;
  5516. end;
  5517. end;
  5518. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  5519. var
  5520. p: tparavarsym;
  5521. ploc: PCGParalocation;
  5522. i: longint;
  5523. begin
  5524. result:=false;
  5525. init_paraloc_info(side);
  5526. for i:=0 to parast.SymList.Count-1 do
  5527. if tsym(parast.SymList[i]).typ=paravarsym then
  5528. begin
  5529. p:=tparavarsym(parast.SymList[i]);
  5530. if not p.is_used then
  5531. continue;
  5532. { check if no parameter is located on the stack }
  5533. if (is_open_array(p.vardef) or
  5534. is_array_of_const(p.vardef)) and (p.varspez=vs_value) then
  5535. begin
  5536. result:=true;
  5537. exit;
  5538. end;
  5539. ploc:=p.paraloc[side].location;
  5540. while assigned(ploc) do
  5541. begin
  5542. if (ploc^.loc=LOC_REFERENCE) then
  5543. begin
  5544. result:=true;
  5545. exit
  5546. end;
  5547. ploc:=ploc^.next;
  5548. end;
  5549. end;
  5550. end;
  5551. function tabstractprocdef.is_pushleftright: boolean;
  5552. begin
  5553. result:=false;
  5554. end;
  5555. function tabstractprocdef.address_type: tdef;
  5556. begin
  5557. result:=voidcodepointertype;
  5558. end;
  5559. function tabstractprocdef.ofs_address_type:tdef;
  5560. begin
  5561. result:=address_type;
  5562. end;
  5563. procedure tabstractprocdef.declared_far;
  5564. begin
  5565. Message1(parser_w_proc_directive_ignored,'FAR');
  5566. end;
  5567. procedure tabstractprocdef.declared_near;
  5568. begin
  5569. Message1(parser_w_proc_directive_ignored,'NEAR');
  5570. end;
  5571. function tabstractprocdef.generate_safecall_wrapper: boolean;
  5572. begin
  5573. {$ifdef SUPPORT_SAFECALL}
  5574. result:=
  5575. (proccalloption=pocall_safecall) and
  5576. not(po_assembler in procoptions) and
  5577. (tf_safecall_exceptions in target_info.flags);
  5578. {$else SUPPORT_SAFECALL}
  5579. result:=false;
  5580. {$endif}
  5581. end;
  5582. function tabstractprocdef.is_generic_param(def:tdef): boolean;
  5583. begin
  5584. if def.typ=undefineddef then
  5585. result:=def.owner=self.parast
  5586. else if def.typ=objectdef then
  5587. result:=(def.owner=self.parast) and (tstoreddef(def).genconstraintdata<>nil)
  5588. else
  5589. result:=false;
  5590. end;
  5591. function tabstractprocdef.wpo_may_create_instance(optionalmethodpointer: tnode): boolean;
  5592. begin
  5593. result:=
  5594. (proctypeoption=potype_constructor) or
  5595. ((typ=procdef) and
  5596. ((not assigned(optionalmethodpointer) and
  5597. is_class(tdef(owner.defowner))) or
  5598. (assigned(optionalmethodpointer) and
  5599. ((optionalmethodpointer.resultdef.typ=classrefdef) or
  5600. (optionalmethodpointer.nodetype=typen)))) and
  5601. (tprocdef(self).procsym.Name='NEWINSTANCE'))
  5602. end;
  5603. {***************************************************************************
  5604. TPROCDEF
  5605. ***************************************************************************}
  5606. function tprocdef.getparentfpsym: tsym;
  5607. begin
  5608. if not assigned(_parentfpsym) then
  5609. begin
  5610. _parentfpsym:=tsym(parast.Find('parentfp'));
  5611. if not assigned(_parentfpsym) then
  5612. internalerror(200309281);
  5613. end;
  5614. result:=_parentfpsym;
  5615. end;
  5616. function tprocdef.Getis_implemented: boolean;
  5617. begin
  5618. result:=not assigned(implprocdefinfo) or implprocdefinfo^.is_implemented;
  5619. end;
  5620. procedure tprocdef.Setis_implemented(AValue: boolean);
  5621. begin
  5622. if not assigned(implprocdefinfo) then
  5623. internalerror(2020062101);
  5624. implprocdefinfo^.is_implemented:=AValue;
  5625. end;
  5626. function tprocdef.getwas_anonymous:boolean;
  5627. begin
  5628. result:=assigned(implprocdefinfo) and implprocdefinfo^.was_anonymous;
  5629. end;
  5630. procedure tprocdef.setwas_anonymous(avalue:boolean);
  5631. begin
  5632. if not assigned(implprocdefinfo) then
  5633. internalerror(2022020502);
  5634. implprocdefinfo^.was_anonymous:=avalue;
  5635. end;
  5636. function tprocdef.gethas_capturer:boolean;
  5637. begin
  5638. result:=assigned(implprocdefinfo) and implprocdefinfo^.has_capturer;
  5639. end;
  5640. procedure tprocdef.sethas_capturer(avalue:boolean);
  5641. begin
  5642. if not assigned(implprocdefinfo) then
  5643. internalerror(2022020503);
  5644. implprocdefinfo^.has_capturer:=avalue;
  5645. end;
  5646. function tprocdef.Getcapturedsyms:tfplist;
  5647. begin
  5648. if not assigned(implprocdefinfo) then
  5649. result:=nil
  5650. else
  5651. result:=implprocdefinfo^.capturedsyms;
  5652. end;
  5653. function tprocdef.store_localst: boolean;
  5654. begin
  5655. result:=has_inlininginfo or (df_generic in defoptions);
  5656. end;
  5657. function tprocdef.GetResultName: PShortString;
  5658. begin
  5659. if not assigned(implprocdefinfo) then
  5660. internalerror(2014010301);
  5661. result:=implprocdefinfo^.resultname;
  5662. end;
  5663. procedure tprocdef.SetResultName(AValue: PShortString);
  5664. begin
  5665. if not assigned(implprocdefinfo) then
  5666. internalerror(2014010302);
  5667. implprocdefinfo^.resultname:=AValue;
  5668. end;
  5669. function tprocdef.GetParentFPInitBlock: tnode;
  5670. begin
  5671. if not assigned(implprocdefinfo) then
  5672. internalerror(2014010303);
  5673. result:=implprocdefinfo^.parentfpinitblock;
  5674. end;
  5675. function tprocdef.GetParentFPStruct: tsym;
  5676. begin
  5677. if not assigned(implprocdefinfo) then
  5678. internalerror(2014010304);
  5679. result:=implprocdefinfo^.parentfpstruct;
  5680. end;
  5681. function tprocdef.GetParentFPStructPtrType: tdef;
  5682. begin
  5683. if not assigned(implprocdefinfo) then
  5684. internalerror(2014010305);
  5685. result:=implprocdefinfo^.parentfpstructptrtype;
  5686. end;
  5687. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  5688. begin
  5689. if not assigned(implprocdefinfo) then
  5690. internalerror(2014010306);
  5691. implprocdefinfo^.parentfpinitblock:=AValue;
  5692. end;
  5693. function tprocdef.Getprocendtai: tai;
  5694. begin
  5695. if not assigned(implprocdefinfo) then
  5696. internalerror(2014010307);
  5697. result:=implprocdefinfo^.procendtai;
  5698. end;
  5699. function tprocdef.Getprocstarttai: tai;
  5700. begin
  5701. if not assigned(implprocdefinfo) then
  5702. internalerror(2014010308);
  5703. result:=implprocdefinfo^.procstarttai;
  5704. end;
  5705. procedure tprocdef.Setprocendtai(AValue: tai);
  5706. begin
  5707. if not assigned(implprocdefinfo) then
  5708. internalerror(2014010309);
  5709. implprocdefinfo^.procendtai:=AValue;
  5710. end;
  5711. function tprocdef.Getskpara: pointer;
  5712. begin
  5713. if not assigned(implprocdefinfo) then
  5714. internalerror(2014010310);
  5715. result:=implprocdefinfo^.skpara;
  5716. end;
  5717. procedure tprocdef.Setskpara(AValue: pointer);
  5718. begin
  5719. if not assigned(implprocdefinfo) then
  5720. internalerror(2014010311);
  5721. implprocdefinfo^.skpara:=AValue;
  5722. end;
  5723. function tprocdef.Getpersonality: tprocdef;
  5724. begin
  5725. if not assigned(implprocdefinfo) then
  5726. internalerror(2016121704);
  5727. result:=implprocdefinfo^.personality;
  5728. end;
  5729. procedure tprocdef.Setpersonality(AValue: tprocdef);
  5730. begin
  5731. if not assigned(implprocdefinfo) then
  5732. internalerror(2016121705);
  5733. implprocdefinfo^.personality:=AValue;
  5734. end;
  5735. function tprocdef.Getforwarddef: boolean;
  5736. begin
  5737. if not assigned(implprocdefinfo) then
  5738. internalerror(2014010312);
  5739. result:=implprocdefinfo^.forwarddef;
  5740. end;
  5741. function tprocdef.Gethasforward: boolean;
  5742. begin
  5743. if not assigned(implprocdefinfo) then
  5744. internalerror(2014010313);
  5745. result:=implprocdefinfo^.hasforward;
  5746. end;
  5747. function tprocdef.Getinterfacedef: boolean;
  5748. begin
  5749. if not assigned(implprocdefinfo) then
  5750. internalerror(2014010314);
  5751. result:=implprocdefinfo^.interfacedef;
  5752. end;
  5753. procedure tprocdef.Setforwarddef(AValue: boolean);
  5754. begin
  5755. if not assigned(implprocdefinfo) then
  5756. internalerror(2014010315);
  5757. implprocdefinfo^.forwarddef:=AValue;
  5758. end;
  5759. procedure tprocdef.Sethasforward(AValue: boolean);
  5760. begin
  5761. if not assigned(implprocdefinfo) then
  5762. internalerror(2014010316);
  5763. implprocdefinfo^.hasforward:=AValue;
  5764. end;
  5765. function tprocdef.GetIsEmpty: boolean;
  5766. begin
  5767. result:=pio_empty in implprocoptions;
  5768. end;
  5769. procedure tprocdef.SetIsEmpty(AValue: boolean);
  5770. begin
  5771. if AValue then
  5772. include(implprocoptions,pio_empty)
  5773. else
  5774. exclude(implprocoptions,pio_empty);
  5775. end;
  5776. function tprocdef.GetHasInliningInfo: boolean;
  5777. begin
  5778. result:=pio_has_inlininginfo in implprocoptions;
  5779. end;
  5780. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  5781. begin
  5782. if AValue then
  5783. include(implprocoptions,pio_has_inlininginfo)
  5784. else
  5785. exclude(implprocoptions,pio_has_inlininginfo);
  5786. end;
  5787. procedure tprocdef.Setinterfacedef(AValue: boolean);
  5788. begin
  5789. if not assigned(implprocdefinfo) then
  5790. internalerror(2014010317);
  5791. implprocdefinfo^.interfacedef:=AValue;
  5792. end;
  5793. procedure tprocdef.Setprocstarttai(AValue: tai);
  5794. begin
  5795. if not assigned(implprocdefinfo) then
  5796. internalerror(2014010318);
  5797. implprocdefinfo^.procstarttai:=AValue;
  5798. end;
  5799. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  5800. begin
  5801. if not assigned(implprocdefinfo) then
  5802. internalerror(2014010319);
  5803. implprocdefinfo^.parentfpstruct:=AValue;
  5804. end;
  5805. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  5806. begin
  5807. if not assigned(implprocdefinfo) then
  5808. internalerror(2014010320);
  5809. implprocdefinfo^.parentfpstructptrtype:=AValue;
  5810. end;
  5811. constructor tprocdef.create(level:byte;doregister:boolean);
  5812. begin
  5813. inherited create(procdef,level,doregister);
  5814. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  5815. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  5816. {$ifdef symansistr}
  5817. _mangledname:='';
  5818. {$else symansistr}
  5819. _mangledname:=nil;
  5820. {$endif symansistr}
  5821. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  5822. seenmarker := nil;
  5823. {$endif}
  5824. fileinfo:=current_filepos;
  5825. extnumber:=$ffff;
  5826. aliasnames:=TCmdStrList.create;
  5827. funcretsym:=nil;
  5828. funcretsymderef.reset;
  5829. procsymderef.reset;
  5830. forwarddef:=true;
  5831. interfacedef:=false;
  5832. hasforward:=false;
  5833. struct := nil;
  5834. structderef.reset;
  5835. import_dll:=nil;
  5836. import_name:=nil;
  5837. import_nr:=0;
  5838. inlininginfo:=nil;
  5839. deprecatedmsg:=nil;
  5840. genericdecltokenbuf:=nil;
  5841. if cs_opt_fastmath in current_settings.optimizerswitches then
  5842. include(implprocoptions, pio_fastmath);
  5843. end;
  5844. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  5845. var
  5846. i,aliasnamescount,sizeleft : longint;
  5847. level : byte;
  5848. buf : array[0..255] of byte;
  5849. begin
  5850. inherited ppuload(procdef,ppufile);
  5851. {$ifdef symansistr}
  5852. if po_has_mangledname in procoptions then
  5853. _mangledname:=ppufile.getansistring
  5854. else
  5855. _mangledname:='';
  5856. {$else symansistr}
  5857. if po_has_mangledname in procoptions then
  5858. _mangledname:=ppufile.getpshortstring
  5859. else
  5860. _mangledname:=nil;
  5861. {$endif symansistr}
  5862. extnumber:=ppufile.getword;
  5863. level:=ppufile.getbyte;
  5864. ppufile.getderef(structderef);
  5865. ppufile.getderef(procsymderef);
  5866. ppufile.getposinfo(fileinfo);
  5867. visibility:=tvisibility(ppufile.getbyte);
  5868. ppufile.getset(tppuset2(symoptions));
  5869. if sp_has_deprecated_msg in symoptions then
  5870. deprecatedmsg:=ppufile.getpshortstring
  5871. else
  5872. deprecatedmsg:=nil;
  5873. { import stuff }
  5874. if po_has_importdll in procoptions then
  5875. import_dll:=ppufile.getpshortstring
  5876. else
  5877. import_dll:=nil;
  5878. if po_has_importname in procoptions then
  5879. import_name:=ppufile.getpshortstring
  5880. else
  5881. import_name:=nil;
  5882. import_nr:=ppufile.getword;
  5883. if (po_msgint in procoptions) then
  5884. messageinf.i:=ppufile.getlongint;
  5885. if (po_msgstr in procoptions) then
  5886. messageinf.str:=ppufile.getpshortstring;
  5887. if (po_dispid in procoptions) then
  5888. dispid:=ppufile.getlongint;
  5889. { inline stuff }
  5890. ppufile.getset(tppuset1(implprocoptions));
  5891. if has_inlininginfo then
  5892. begin
  5893. ppufile.getderef(funcretsymderef);
  5894. new(inlininginfo);
  5895. ppufile.getset(tppuset4(inlininginfo^.flags));
  5896. end
  5897. else
  5898. begin
  5899. inlininginfo:=nil;
  5900. funcretsym:=nil;
  5901. end;
  5902. aliasnames:=TCmdStrList.create;
  5903. { count alias names }
  5904. aliasnamescount:=ppufile.getbyte;
  5905. for i:=1 to aliasnamescount do
  5906. aliasnames.insert(ppufile.getstring);
  5907. { load the token stream containing the declaration }
  5908. sizeleft:=ppufile.getlongint;
  5909. if sizeleft>0 then
  5910. begin
  5911. init_genericdecl;
  5912. while sizeleft>0 do
  5913. begin
  5914. if sizeleft>sizeof(buf) then
  5915. i:=sizeof(buf)
  5916. else
  5917. i:=sizeleft;
  5918. ppufile.getdata(buf,i);
  5919. genericdecltokenbuf.write(buf,i);
  5920. dec(sizeleft,i);
  5921. end;
  5922. end;
  5923. ppuload_platform(ppufile);
  5924. { load para symtable }
  5925. parast:=tparasymtable.create(self,level);
  5926. tparasymtable(parast).ppuload(ppufile);
  5927. { load local symtable }
  5928. if store_localst then
  5929. begin
  5930. localst:=tlocalsymtable.create(self,level);
  5931. tlocalsymtable(localst).ppuload(ppufile);
  5932. end
  5933. else
  5934. localst:=nil;
  5935. { inline stuff }
  5936. if has_inlininginfo then
  5937. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5938. { default values for no persistent data }
  5939. if (cs_link_deffile in current_settings.globalswitches) and
  5940. (tf_need_export in target_info.flags) and
  5941. (po_exports in procoptions) then
  5942. deffile.AddExport(mangledname);
  5943. { Disable po_has_inlining until the derefimpl is done }
  5944. has_inlininginfo:=false;
  5945. end;
  5946. destructor tprocdef.destroy;
  5947. begin
  5948. aliasnames.free;
  5949. aliasnames:=nil;
  5950. if assigned(localst) and
  5951. (localst.symtabletype<>staticsymtable) then
  5952. begin
  5953. {$ifdef MEMDEBUG}
  5954. memproclocalst.start;
  5955. {$endif MEMDEBUG}
  5956. localst.free;
  5957. localst:=nil;
  5958. {$ifdef MEMDEBUG}
  5959. memproclocalst.start;
  5960. {$endif MEMDEBUG}
  5961. end;
  5962. if assigned(inlininginfo) then
  5963. begin
  5964. {$ifdef MEMDEBUG}
  5965. memprocnodetree.start;
  5966. {$endif MEMDEBUG}
  5967. tnode(inlininginfo^.code).free;
  5968. {$ifdef MEMDEBUG}
  5969. memprocnodetree.start;
  5970. {$endif MEMDEBUG}
  5971. dispose(inlininginfo);
  5972. inlininginfo:=nil;
  5973. end;
  5974. freeimplprocdefinfo;
  5975. genericdecltokenbuf.free;
  5976. genericdecltokenbuf:=nil;
  5977. stringdispose(import_dll);
  5978. stringdispose(import_name);
  5979. stringdispose(deprecatedmsg);
  5980. if (po_msgstr in procoptions) then
  5981. stringdispose(messageinf.str);
  5982. {$ifndef symansistr}
  5983. if assigned(_mangledname) then
  5984. begin
  5985. {$ifdef MEMDEBUG}
  5986. memmanglednames.start;
  5987. {$endif MEMDEBUG}
  5988. stringdispose(_mangledname);
  5989. {$ifdef MEMDEBUG}
  5990. memmanglednames.stop;
  5991. {$endif MEMDEBUG}
  5992. end;
  5993. {$endif symansistr}
  5994. inherited destroy;
  5995. end;
  5996. procedure tprocdef.freeimplprocdefinfo;
  5997. begin
  5998. if assigned(implprocdefinfo) then
  5999. begin
  6000. stringdispose(implprocdefinfo^.resultname);
  6001. TFPList.FreeAndNilDisposing(implprocdefinfo^.capturedsyms,typeinfo(tcapturedsyminfo));
  6002. freemem(implprocdefinfo);
  6003. implprocdefinfo:=nil;
  6004. end;
  6005. end;
  6006. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  6007. var
  6008. oldintfcrc : boolean;
  6009. aliasnamescount,i,sizeleft : longint;
  6010. item : TCmdStrListItem;
  6011. buf : array[0..255] of byte;
  6012. begin
  6013. { released procdef? }
  6014. if not assigned(parast) then
  6015. exit;
  6016. inherited ppuwrite(ppufile);
  6017. {$ifdef symansistr}
  6018. if po_has_mangledname in procoptions then
  6019. ppufile.putansistring(_mangledname);
  6020. {$else symansistr}
  6021. if po_has_mangledname in procoptions then
  6022. ppufile.putstring(_mangledname^);
  6023. {$endif symansistr}
  6024. ppufile.putword(extnumber);
  6025. ppufile.putbyte(parast.symtablelevel);
  6026. ppufile.putderef(structderef);
  6027. ppufile.putderef(procsymderef);
  6028. ppufile.putposinfo(fileinfo);
  6029. ppufile.putbyte(byte(visibility));
  6030. ppufile.putset(tppuset2(symoptions));
  6031. if sp_has_deprecated_msg in symoptions then
  6032. ppufile.putstring(deprecatedmsg^);
  6033. { import }
  6034. if po_has_importdll in procoptions then
  6035. ppufile.putstring(import_dll^);
  6036. if po_has_importname in procoptions then
  6037. ppufile.putstring(import_name^);
  6038. ppufile.putword(import_nr);
  6039. if (po_msgint in procoptions) then
  6040. ppufile.putlongint(messageinf.i);
  6041. if (po_msgstr in procoptions) then
  6042. ppufile.putstring(messageinf.str^);
  6043. if (po_dispid in procoptions) then
  6044. ppufile.putlongint(dispid);
  6045. { inline stuff }
  6046. oldintfcrc:=ppufile.do_crc;
  6047. ppufile.do_crc:=false;
  6048. ppufile.putset(tppuset1(implprocoptions));
  6049. if has_inlininginfo then
  6050. begin
  6051. ppufile.putderef(funcretsymderef);
  6052. ppufile.putset(tppuset4(inlininginfo^.flags));
  6053. end;
  6054. { count alias names }
  6055. aliasnamescount:=0;
  6056. item:=TCmdStrListItem(aliasnames.first);
  6057. while assigned(item) do
  6058. begin
  6059. inc(aliasnamescount);
  6060. item:=TCmdStrListItem(item.next);
  6061. end;
  6062. if aliasnamescount>255 then
  6063. internalerror(200711021);
  6064. ppufile.putbyte(aliasnamescount);
  6065. item:=TCmdStrListItem(aliasnames.first);
  6066. while assigned(item) do
  6067. begin
  6068. ppufile.putstring(item.str);
  6069. item:=TCmdStrListItem(item.next);
  6070. end;
  6071. ppufile.do_crc:=oldintfcrc;
  6072. { generic tokens for the declaration }
  6073. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  6074. begin
  6075. sizeleft:=genericdecltokenbuf.size;
  6076. genericdecltokenbuf.seek(0);
  6077. ppufile.putlongint(sizeleft);
  6078. while sizeleft>0 do
  6079. begin
  6080. if sizeleft>sizeof(buf) then
  6081. i:=sizeof(buf)
  6082. else
  6083. i:=sizeleft;
  6084. genericdecltokenbuf.read(buf,i);
  6085. ppufile.putdata(buf,i);
  6086. dec(sizeleft,i);
  6087. end;
  6088. end
  6089. else
  6090. ppufile.putlongint(0);
  6091. { write this entry }
  6092. writeentry(ppufile,ibprocdef);
  6093. { Save the para symtable, this is taken from the interface }
  6094. tparasymtable(parast).ppuwrite(ppufile);
  6095. { save localsymtable for inline procedures or when local
  6096. browser info is requested, this has no influence on the crc }
  6097. if store_localst and not ppufile.crc_only then
  6098. begin
  6099. oldintfcrc:=ppufile.do_crc;
  6100. ppufile.do_crc:=false;
  6101. tlocalsymtable(localst).ppuwrite(ppufile);
  6102. ppufile.do_crc:=oldintfcrc;
  6103. end;
  6104. { node tree for inlining }
  6105. oldintfcrc:=ppufile.do_crc;
  6106. ppufile.do_crc:=false;
  6107. if has_inlininginfo then
  6108. ppuwritenodetree(ppufile,inlininginfo^.code);
  6109. ppufile.do_crc:=oldintfcrc;
  6110. end;
  6111. function tprocdef.fullprocname(showhidden:boolean):string;
  6112. var
  6113. pno: tprocnameoptions;
  6114. begin
  6115. pno:=[];
  6116. if showhidden then
  6117. include(pno,pno_showhidden);
  6118. result:=customprocname(pno);
  6119. end;
  6120. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  6121. var
  6122. s, hs, rn : ansistring;
  6123. t : ttoken;
  6124. module : tmodule;
  6125. syssym : tsyssym;
  6126. begin
  6127. {$ifdef EXTDEBUG}
  6128. include(pno,pno_showhidden);
  6129. {$endif EXTDEBUG}
  6130. s:='';
  6131. rn:='';
  6132. if proctypeoption=potype_operator then
  6133. begin
  6134. for t:=NOTOKEN to last_overloaded do
  6135. if procsym.realname='$'+overloaded_names[t] then
  6136. begin
  6137. s:='operator ';
  6138. if (pno_ownername in pno) and
  6139. assigned(struct) then
  6140. s:=s+struct.RttiName+'.';
  6141. s:=s+arraytokeninfo[t].str;
  6142. if not (pno_noparams in pno) then
  6143. s:=s+typename_paras(pno);
  6144. break;
  6145. end;
  6146. end
  6147. else
  6148. begin
  6149. if (po_classmethod in procoptions) and
  6150. not(pno_noclassmarker in pno) then
  6151. s:='class ';
  6152. case proctypeoption of
  6153. potype_constructor,
  6154. potype_class_constructor:
  6155. s:=s+'constructor ';
  6156. potype_class_destructor,
  6157. potype_destructor:
  6158. s:=s+'destructor ';
  6159. else
  6160. if (pno_proctypeoption in pno) and not (po_anonymous in procoptions) then
  6161. begin
  6162. if assigned(returndef) and
  6163. not(is_void(returndef)) then
  6164. s:=s+'function '
  6165. else
  6166. s:=s+'procedure ';
  6167. end;
  6168. end;
  6169. if (pno_ownername in pno) and
  6170. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  6171. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  6172. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  6173. begin
  6174. syssym:=tsyssym.find_by_number(extnumber);
  6175. if not assigned(syssym) then
  6176. internalerror(2016060305);
  6177. rn:=syssym.realname;
  6178. end
  6179. else if po_anonymous in procoptions then
  6180. begin
  6181. s:=s+'anonymous ';
  6182. if assigned(returndef) and
  6183. not(is_void(returndef)) then
  6184. s:=s+'function'
  6185. else
  6186. s:=s+'procedure';
  6187. end
  6188. else if pno_prettynames in pno then
  6189. rn:=procsym.prettyname
  6190. else
  6191. rn:=procsym.RealName;
  6192. if (pno_noleadingdollar in pno) and
  6193. (rn[1]='$') then
  6194. delete(rn,1,1);
  6195. s:=s+rn;
  6196. if not (pno_noparams in pno) then
  6197. s:=s+typename_paras(pno);
  6198. end;
  6199. if not(proctypeoption in [potype_constructor,potype_destructor,
  6200. potype_class_constructor,potype_class_destructor]) and
  6201. assigned(returndef) and
  6202. not(is_void(returndef)) then
  6203. begin
  6204. if assigned(returndef.typesym) then
  6205. begin
  6206. module:=find_module_from_symtable(returndef.typesym.owner);
  6207. if module <> current_module then
  6208. s:=s+':'+module.realmodulename^+'.'
  6209. else
  6210. s:=s+':';
  6211. if pno_prettynames in pno then
  6212. hs:=returndef.typesym.prettyname
  6213. else
  6214. hs:=returndef.typesym.realname;
  6215. if hs[1]<>'$' then
  6216. s:=s+returndef.OwnerHierarchyName+hs
  6217. else
  6218. s:=s+returndef.GetTypeName;
  6219. end
  6220. else
  6221. s:=s+':'+returndef.GetTypeName;
  6222. end;
  6223. if not (po_anonymous in procoptions) then
  6224. if assigned(owner) and (owner.symtabletype=localsymtable) then
  6225. s:=s+' is nested'
  6226. else if po_is_block in procoptions then
  6227. s:=s+' is block';
  6228. s:=s+';';
  6229. if po_far in procoptions then
  6230. s:=s+' far;';
  6231. { forced calling convention? }
  6232. if (po_hascallingconvention in procoptions) then
  6233. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  6234. if (po_staticmethod in procoptions) and
  6235. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  6236. s:=s+' Static;';
  6237. if pno_mangledname in pno then
  6238. s:=s+' -- mangled name: '+mangledname;
  6239. customprocname:=s;
  6240. end;
  6241. function tprocdef.is_methodpointer:boolean;
  6242. begin
  6243. { don't check assigned(_class), that's also the case for nested
  6244. procedures inside methods }
  6245. result:=(owner.symtabletype in [recordsymtable,ObjectSymtable]) and not no_self_node;
  6246. end;
  6247. function tprocdef.is_addressonly:boolean;
  6248. begin
  6249. result:=assigned(owner) and
  6250. not is_methodpointer and
  6251. (not(m_nested_procvars in current_settings.modeswitches) or
  6252. not is_nested_pd(self)) and
  6253. (
  6254. not (po_anonymous in procoptions) or
  6255. not assigned(capturedsyms) or
  6256. (capturedsyms.count=0)
  6257. );
  6258. end;
  6259. procedure tprocdef.make_external;
  6260. begin
  6261. include(procoptions,po_external);
  6262. forwarddef:=false;
  6263. end;
  6264. procedure tprocdef.init_genericdecl;
  6265. begin
  6266. if assigned(genericdecltokenbuf) then
  6267. internalerror(2015061901);
  6268. genericdecltokenbuf:=tdynamicarray.create(256);
  6269. end;
  6270. function tprocdef.get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  6271. begin
  6272. result:=false;
  6273. if proctypeoption=potype_constructor then
  6274. begin
  6275. result:=true;
  6276. ressym:=tsym(parast.Find('self'));
  6277. resdef:=tabstractnormalvarsym(ressym).vardef;
  6278. { and TP-style constructors return a pointer to self }
  6279. if is_object(resdef) then
  6280. resdef:=cpointerdef.getreusable(resdef);
  6281. end
  6282. else if (proccalloption=pocall_safecall) and
  6283. (tf_safecall_exceptions in target_info.flags) then
  6284. begin
  6285. result:=true;
  6286. ressym:=tsym(localst.Find('safecallresult'));
  6287. resdef:=tabstractnormalvarsym(ressym).vardef;
  6288. end
  6289. else if not is_void(returndef) then
  6290. begin
  6291. result:=true;
  6292. ressym:=funcretsym;
  6293. resdef:=tabstractnormalvarsym(ressym).vardef;
  6294. end;
  6295. end;
  6296. function tprocdef.get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  6297. begin
  6298. result:=false;
  6299. if (proctypeoption<>potype_constructor) and
  6300. (proccalloption=pocall_safecall) and
  6301. (tf_safecall_exceptions in target_info.flags) then
  6302. begin
  6303. result:=true;
  6304. ressym:=tsym(localst.Find('safecallresult'));
  6305. resdef:=tabstractnormalvarsym(ressym).vardef;
  6306. end
  6307. end;
  6308. procedure tprocdef.add_captured_sym(sym:tsym;def:tdef;const filepos:tfileposinfo);
  6309. var
  6310. i : longint;
  6311. capturedsym : pcapturedsyminfo;
  6312. begin
  6313. if not assigned(implprocdefinfo) then
  6314. internalerror(2021052601);
  6315. if not assigned(implprocdefinfo^.capturedsyms) then
  6316. implprocdefinfo^.capturedsyms:=tfplist.create;
  6317. for i:=0 to implprocdefinfo^.capturedsyms.count-1 do
  6318. begin
  6319. capturedsym:=pcapturedsyminfo(implprocdefinfo^.capturedsyms[i]);
  6320. if (capturedsym^.sym=sym) and (capturedsym^.def=def) then
  6321. exit;
  6322. end;
  6323. new(capturedsym);
  6324. capturedsym^.sym:=sym;
  6325. capturedsym^.def:=def;
  6326. capturedsym^.fileinfo:=filepos;
  6327. implprocdefinfo^.capturedsyms.add(capturedsym);
  6328. end;
  6329. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  6330. var
  6331. item : TCmdStrListItem;
  6332. begin
  6333. result:=true;
  6334. if mangledname=s then
  6335. exit;
  6336. item:=TCmdStrListItem(aliasnames.first);
  6337. while assigned(item) do
  6338. begin
  6339. if item.str=s then
  6340. exit;
  6341. item:=TCmdStrListItem(item.next);
  6342. end;
  6343. result:=false;
  6344. end;
  6345. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  6346. begin
  6347. case t of
  6348. gs_local :
  6349. GetSymtable:=localst;
  6350. gs_para :
  6351. GetSymtable:=parast;
  6352. else
  6353. GetSymtable:=nil;
  6354. end;
  6355. end;
  6356. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  6357. var
  6358. j : longint;
  6359. begin
  6360. result:=inherited;
  6361. if newtyp=procvardef then
  6362. begin
  6363. { create new paralist }
  6364. tprocvardef(result).calcparas;
  6365. exit;
  6366. end;
  6367. { don't copy mangled name, can be different }
  6368. tprocdef(result).messageinf:=messageinf;
  6369. tprocdef(result).dispid:=dispid;
  6370. if po_msgstr in procoptions then
  6371. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  6372. tprocdef(result).symoptions:=symoptions;
  6373. if assigned(deprecatedmsg) then
  6374. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  6375. { will have to be associated with appropriate procsym }
  6376. tprocdef(result).procsym:=nil;
  6377. { don't create aliases for bare copies, nor copy the funcretsym as
  6378. the function result parameter will be inserted again if necessary
  6379. (e.g. if the calling convention is changed) }
  6380. if not(copytyp in [pc_bareproc,pc_normal_no_hidden]) then
  6381. begin
  6382. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  6383. if assigned(funcretsym) then
  6384. begin
  6385. if funcretsym.owner=parast then
  6386. begin
  6387. j:=parast.symlist.indexof(funcretsym);
  6388. if j<0 then
  6389. internalerror(2011040606);
  6390. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  6391. end
  6392. else if funcretsym.owner=localst then
  6393. begin
  6394. { nothing to do, will be inserted for the new procdef while
  6395. parsing its body (by pdecsub.insert_funcret_local) }
  6396. end
  6397. else
  6398. internalerror(2011040605);
  6399. end;
  6400. end;
  6401. { will have to be associated with a new struct }
  6402. tprocdef(result).struct:=nil;
  6403. if assigned(implprocdefinfo) then
  6404. begin
  6405. if assigned(resultname) then
  6406. tprocdef(result).resultname:=stringdup(resultname^);
  6407. tprocdef(result).synthetickind:=synthetickind;
  6408. end;
  6409. if assigned(import_dll) then
  6410. tprocdef(result).import_dll:=stringdup(import_dll^);
  6411. if assigned(import_name) then
  6412. tprocdef(result).import_name:=stringdup(import_name^);
  6413. tprocdef(result).import_nr:=import_nr;
  6414. tprocdef(result).extnumber:=$ffff;
  6415. tprocdef(result).visibility:=visibility;
  6416. { we need a separate implementation for the copied def }
  6417. tprocdef(result).forwarddef:=true;
  6418. tprocdef(result).interfacedef:=true;
  6419. { create new paralist }
  6420. tprocdef(result).calcparas;
  6421. end;
  6422. function tprocdef.getcopy: tstoreddef;
  6423. begin
  6424. result:=getcopyas(procdef,pc_normal,'',true);
  6425. end;
  6426. procedure tprocdef.buildderef;
  6427. begin
  6428. inherited buildderef;
  6429. structderef.build(struct);
  6430. { procsym that originaly defined this definition, should be in the
  6431. same symtable }
  6432. procsymderef.build(procsym);
  6433. end;
  6434. procedure tprocdef.buildderefimpl;
  6435. begin
  6436. inherited buildderefimpl;
  6437. { Localst is not available for main/unit init }
  6438. if store_localst and assigned(localst) then
  6439. begin
  6440. tlocalsymtable(localst).buildderef;
  6441. tlocalsymtable(localst).buildderefimpl;
  6442. end;
  6443. { inline tree }
  6444. if has_inlininginfo then
  6445. begin
  6446. funcretsymderef.build(funcretsym);
  6447. inlininginfo^.code.buildderefimpl;
  6448. end;
  6449. end;
  6450. procedure tprocdef.deref;
  6451. begin
  6452. inherited deref;
  6453. struct:=tabstractrecorddef(structderef.resolve);
  6454. { procsym that originaly defined this definition, should be in the
  6455. same symtable }
  6456. procsym:=tprocsym(procsymderef.resolve);
  6457. end;
  6458. procedure tprocdef.derefimpl;
  6459. begin
  6460. { Enable has_inlininginfo when the inlininginfo
  6461. structure is available. The has_inlininginfo was disabled
  6462. after the load, since the data was invalid }
  6463. if assigned(inlininginfo) then
  6464. has_inlininginfo:=true;
  6465. { Locals }
  6466. if store_localst and assigned(localst) then
  6467. begin
  6468. tlocalsymtable(localst).deref(false);
  6469. tlocalsymtable(localst).derefimpl(false);
  6470. end;
  6471. { Inline }
  6472. if has_inlininginfo then
  6473. begin
  6474. inlininginfo^.code.derefimpl;
  6475. { funcretsym, this is always located in the localst }
  6476. funcretsym:=tsym(funcretsymderef.resolve);
  6477. end
  6478. else
  6479. begin
  6480. { safety }
  6481. { Not safe! A unit may be reresolved after its interface has been
  6482. parsed but before its implementation has been parsed, and in that
  6483. case the funcretsym is still required!
  6484. funcretsym:=nil; }
  6485. end;
  6486. end;
  6487. function tprocdef.GetTypeName : string;
  6488. begin
  6489. GetTypeName := FullProcName(false);
  6490. end;
  6491. function tprocdef.mangledname : TSymStr;
  6492. begin
  6493. {$ifdef symansistr}
  6494. if _mangledname='' then
  6495. begin
  6496. result:=defaultmangledname;
  6497. _mangledname:=result;
  6498. end
  6499. else
  6500. result:=_mangledname;
  6501. {$else symansistr}
  6502. if not assigned(_mangledname) then
  6503. begin
  6504. result:=defaultmangledname;
  6505. _mangledname:=stringdup(mangledname);
  6506. end
  6507. else
  6508. result:=_mangledname^;
  6509. {$endif symansistr}
  6510. end;
  6511. function tprocdef.defaultmangledname: TSymStr;
  6512. var
  6513. n : TSymStr;
  6514. begin
  6515. n:=procsym.name;
  6516. { make sure that the mangled names of these overloadable methods types is
  6517. unique even if it's made lowercase (e.g. for section names) }
  6518. if proctypeoption in [potype_operator,potype_class_constructor,potype_class_destructor] then
  6519. n:='$'+n;
  6520. { we need to use the symtable where the procsym is inserted,
  6521. because that is visible to the world }
  6522. defaultmangledname:=make_mangledname('',procsym.owner,n);
  6523. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  6524. end;
  6525. function tprocdef.cplusplusmangledname : TSymStr;
  6526. function getcppparaname(p : tdef) : TSymStr;
  6527. const
  6528. {$ifdef NAMEMANGLING_GCC2}
  6529. ordtype2str : array[tordtype] of string[2] = (
  6530. '',
  6531. 'Uc','Us','Ui','Us','',
  6532. 'Sc','s','i','x','',
  6533. 'b','b','b','b','b','b',
  6534. 'c','w','x');
  6535. {$else NAMEMANGLING_GCC2}
  6536. ordtype2str : array[tordtype] of string[1] = (
  6537. 'v',
  6538. 'h','t','j','y','',
  6539. 'a','s','i','x','',
  6540. 'b','b','b','b','b',
  6541. 'b','b','b','b',
  6542. 'c','w','x','C');
  6543. floattype2str : array[tfloattype] of string[1] = (
  6544. 'f','d','e','e',
  6545. 'd','d','g');
  6546. {$endif NAMEMANGLING_GCC2}
  6547. var
  6548. s : TSymStr;
  6549. begin
  6550. case p.typ of
  6551. orddef:
  6552. begin
  6553. s:=ordtype2str[torddef(p).ordtype];
  6554. if s='C' then
  6555. s:=ordtype2str[range_to_basetype(torddef(p).low,torddef(p).high)];
  6556. end;
  6557. pointerdef:
  6558. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  6559. {$ifndef NAMEMANGLING_GCC2}
  6560. floatdef:
  6561. s:=floattype2str[tfloatdef(p).floattype];
  6562. {$endif NAMEMANGLING_GCC2}
  6563. else
  6564. internalerror(2103001);
  6565. end;
  6566. getcppparaname:=s;
  6567. end;
  6568. var
  6569. s,s2 : TSymStr;
  6570. hp : TParavarsym;
  6571. i : integer;
  6572. begin
  6573. {$ifdef NAMEMANGLING_GCC2}
  6574. { outdated gcc 2.x name mangling scheme }
  6575. s := procsym.realname;
  6576. if procsym.owner.symtabletype=ObjectSymtable then
  6577. begin
  6578. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  6579. case proctypeoption of
  6580. potype_destructor:
  6581. s:='_$_'+tostr(length(s2))+s2;
  6582. potype_constructor:
  6583. s:='___'+tostr(length(s2))+s2;
  6584. else
  6585. s:='_'+s+'__'+tostr(length(s2))+s2;
  6586. end;
  6587. end
  6588. else s:=s+'__';
  6589. s:=s+'F';
  6590. { concat modifiers }
  6591. { !!!!! }
  6592. { now we handle the parameters }
  6593. if maxparacount>0 then
  6594. begin
  6595. for i:=0 to paras.count-1 do
  6596. begin
  6597. hp:=tparavarsym(paras[i]);
  6598. { no hidden parameters form part of a C++ mangled name:
  6599. a) self is not included
  6600. b) there are no "high" or other hidden parameters
  6601. }
  6602. if vo_is_hidden_para in hp.varoptions then
  6603. continue;
  6604. s2:=getcppparaname(hp.vardef);
  6605. if hp.varspez in [vs_var,vs_out] then
  6606. s2:='R'+s2;
  6607. s:=s+s2;
  6608. end;
  6609. end
  6610. else
  6611. s:=s+'v';
  6612. cplusplusmangledname:=s;
  6613. {$else NAMEMANGLING_GCC2}
  6614. { gcc 3.x and 4.x name mangling scheme }
  6615. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  6616. if procsym.owner.symtabletype=ObjectSymtable then
  6617. begin
  6618. s:='_ZN';
  6619. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  6620. s:=s+tostr(length(s2))+s2;
  6621. case proctypeoption of
  6622. potype_constructor:
  6623. s:=s+'C1';
  6624. potype_destructor:
  6625. s:=s+'D1';
  6626. else
  6627. s:=s+tostr(length(procsym.realname))+procsym.realname;
  6628. end;
  6629. s:=s+'E';
  6630. end
  6631. else
  6632. s:=procsym.realname;
  6633. { now we handle the parameters }
  6634. if maxparacount>0 then
  6635. begin
  6636. for i:=0 to paras.count-1 do
  6637. begin
  6638. hp:=tparavarsym(paras[i]);
  6639. { no hidden parameters form part of a C++ mangled name:
  6640. a) self is not included
  6641. b) there are no "high" or other hidden parameters
  6642. }
  6643. if vo_is_hidden_para in hp.varoptions then
  6644. continue;
  6645. s2:=getcppparaname(hp.vardef);
  6646. if hp.varspez in [vs_var,vs_out] then
  6647. s2:='R'+s2;
  6648. s:=s+s2;
  6649. end;
  6650. end
  6651. else
  6652. s:=s+'v';
  6653. cplusplusmangledname:=s;
  6654. {$endif NAMEMANGLING_GCC2}
  6655. end;
  6656. function tprocdef.objcmangledname : TSymStr;
  6657. var
  6658. manglednamelen: longint;
  6659. iscatmethod : boolean;
  6660. begin
  6661. if not (po_msgstr in procoptions) then
  6662. internalerror(2009030901);
  6663. { we may very well need longer strings to handle these... }
  6664. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  6665. length('+"[ ]"')+length(messageinf.str^);
  6666. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  6667. if (iscatmethod) then
  6668. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  6669. if manglednamelen>255 then
  6670. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  6671. if not(po_classmethod in procoptions) then
  6672. result:='"-['
  6673. else
  6674. result:='"+[';
  6675. { quotes are necessary because the +/- otherwise confuse the assembler
  6676. into expecting a number
  6677. }
  6678. if iscatmethod then
  6679. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  6680. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  6681. if iscatmethod then
  6682. result:=result+')';
  6683. result:=result+' '+messageinf.str^+']"';
  6684. end;
  6685. procedure tprocdef.setmangledname(const s : TSymStr);
  6686. begin
  6687. { This is not allowed anymore, the forward declaration
  6688. already needs to create the correct mangledname, no changes
  6689. afterwards are allowed (PFV) }
  6690. { Exception: interface definitions in mode macpas, since in that }
  6691. { case no reference to the old name can exist yet (JM) }
  6692. {$ifdef symansistr}
  6693. if _mangledname<>'' then
  6694. if ((m_mac in current_settings.modeswitches) and
  6695. (interfacedef)) then
  6696. _mangledname:=''
  6697. else
  6698. internalerror(200411171);
  6699. {$else symansistr}
  6700. if assigned(_mangledname) then
  6701. if ((m_mac in current_settings.modeswitches) and
  6702. (interfacedef)) then
  6703. stringdispose(_mangledname)
  6704. else
  6705. internalerror(200411171);
  6706. {$endif symansistr}
  6707. {$ifdef jvm}
  6708. { this routine can be called for compilerproces. can't set mangled
  6709. name since it must be calculated, but it uses import_name when set
  6710. -> set that one }
  6711. import_name:=stringdup(s);
  6712. include(procoptions,po_has_importname);
  6713. include(procoptions,po_has_mangledname);
  6714. {$else}
  6715. {$ifdef symansistr}
  6716. _mangledname:=s;
  6717. {$else symansistr}
  6718. _mangledname:=stringdup(s);
  6719. {$endif symansistr}
  6720. {$endif jvm}
  6721. include(procoptions,po_has_mangledname);
  6722. end;
  6723. function tprocdef.needsglobalasmsym: boolean;
  6724. begin
  6725. result:=
  6726. (cs_profile in current_settings.moduleswitches) or
  6727. { smart linking using a library requires to promote
  6728. all non-nested procedures to AB_GLOBAL
  6729. otherwise you get undefined symbol error at linking
  6730. for msdos target with -CX option for instance }
  6731. (create_smartlink_library and not is_nested_pd(self)) or
  6732. (po_global in procoptions);
  6733. end;
  6734. procedure tprocdef.setcompilerprocname;
  6735. begin
  6736. procsym.realname:='$'+lower(procsym.name);
  6737. end;
  6738. {***************************************************************************
  6739. TPROCVARDEF
  6740. ***************************************************************************}
  6741. constructor tprocvardef.create(level:byte;doregister:boolean);
  6742. begin
  6743. inherited create(procvardef,level,doregister);
  6744. end;
  6745. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef;
  6746. var
  6747. res: PHashSetItem;
  6748. oldsymtablestack: tsymtablestack;
  6749. key: packed record
  6750. def: tabstractprocdef;
  6751. copytyp: tcacheableproccopytyp;
  6752. end;
  6753. begin
  6754. if not assigned(current_module) then
  6755. internalerror(2011081302);
  6756. key.def:=def;
  6757. key.copytyp:=copytyp;
  6758. res:=current_module.procaddrdefs.FindOrAdd(@key,sizeof(key));
  6759. if not assigned(res^.Data) then
  6760. begin
  6761. { since these pointerdefs can be reused anywhere in the current
  6762. unit, add them to the global/staticsymtable (or local symtable
  6763. if they're a local def, because otherwise they'll be saved
  6764. to the ppu referencing a local symtable entry that doesn't
  6765. exist in the ppu) }
  6766. oldsymtablestack:=symtablestack;
  6767. { do not simply push/pop current_module.localsymtable, because
  6768. that can have side-effects (e.g., it removes helpers) }
  6769. symtablestack:=nil;
  6770. result:=tprocvardef(def.getcopyas(procvardef,copytyp,'',true));
  6771. setup_reusable_def(def,result,res,oldsymtablestack);
  6772. { res^.Data may still be nil -> don't overwrite result }
  6773. exit;
  6774. end;
  6775. result:=tprocvardef(res^.Data);
  6776. end;
  6777. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  6778. begin
  6779. result:=getreusableprocaddr(def,pc_address_only);
  6780. if not result.is_registered then
  6781. include(result.defoptions,df_not_registered_no_free);
  6782. end;
  6783. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  6784. begin
  6785. inherited ppuload(procvardef,ppufile);
  6786. { load para symtable }
  6787. parast:=tparasymtable.create(self,ppufile.getbyte);
  6788. ppuload_platform(ppufile);
  6789. tparasymtable(parast).ppuload(ppufile);
  6790. end;
  6791. function tprocvardef.getcopy : tstoreddef;
  6792. var
  6793. i : tcallercallee;
  6794. j : longint;
  6795. begin
  6796. result:=cprocvardef.create(parast.symtablelevel,true);
  6797. tprocvardef(result).returndef:=returndef;
  6798. tprocvardef(result).returndefderef:=returndefderef;
  6799. tprocvardef(result).parast:=parast.getcopy;
  6800. tprocvardef(result).savesize:=savesize;
  6801. { create paralist copy }
  6802. calcparas;
  6803. tprocvardef(result).paras:=tparalist.create(false);
  6804. tprocvardef(result).paras.count:=paras.count;
  6805. for j:=0 to paras.count-1 do
  6806. tprocvardef(result).paras[j]:=paras[j];
  6807. tprocvardef(result).proctypeoption:=proctypeoption;
  6808. tprocvardef(result).proccalloption:=proccalloption;
  6809. tprocvardef(result).procoptions:=procoptions;
  6810. tprocvardef(result).callerargareasize:=callerargareasize;
  6811. tprocvardef(result).calleeargareasize:=calleeargareasize;
  6812. tprocvardef(result).maxparacount:=maxparacount;
  6813. tprocvardef(result).minparacount:=minparacount;
  6814. for i:=low(funcretloc) to high(funcretloc) do
  6815. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  6816. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  6817. {$ifdef m68k}
  6818. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  6819. {$endif}
  6820. end;
  6821. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  6822. begin
  6823. inherited ppuwrite(ppufile);
  6824. { Save the para symtable level (necessary to distinguish nested
  6825. procvars) }
  6826. ppufile.putbyte(parast.symtablelevel);
  6827. { Write this entry }
  6828. writeentry(ppufile,ibprocvardef);
  6829. { Save the para symtable, this is taken from the interface }
  6830. tparasymtable(parast).ppuwrite(ppufile);
  6831. end;
  6832. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  6833. begin
  6834. case t of
  6835. gs_para :
  6836. GetSymtable:=parast;
  6837. else
  6838. GetSymtable:=nil;
  6839. end;
  6840. end;
  6841. function tprocvardef.size : asizeint;
  6842. begin
  6843. { we return false for is_addressonly for a block (because it's not a
  6844. simple pointer to a function), but they are handled as implicit
  6845. pointers to a datastructure that contains everything ->
  6846. voidpointertype.size instead of voidcodepointertype.size }
  6847. if po_is_block in procoptions then
  6848. size:=voidpointertype.size
  6849. else if not is_addressonly then
  6850. begin
  6851. if is_nested_pd(self) then
  6852. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  6853. else
  6854. size:=voidcodepointertype.size+voidpointertype.size;
  6855. end
  6856. else
  6857. size:=voidcodepointertype.size;
  6858. end;
  6859. function tprocvardef.is_methodpointer:boolean;
  6860. begin
  6861. result:=(po_methodpointer in procoptions);
  6862. end;
  6863. function tprocvardef.is_addressonly:boolean;
  6864. begin
  6865. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  6866. not(po_is_block in procoptions) and
  6867. not is_nested_pd(self)) or
  6868. (po_addressonly in procoptions);
  6869. end;
  6870. function tprocvardef.getmangledparaname:TSymStr;
  6871. begin
  6872. if not(po_methodpointer in procoptions) then
  6873. if not is_nested_pd(self) then
  6874. result:='procvar'
  6875. else
  6876. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  6877. having not a type name or not an unique one, see webtbs/tw27515.pp
  6878. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  6879. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  6880. else
  6881. result:='procvarofobj'
  6882. end;
  6883. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  6884. begin
  6885. result:=inherited;
  6886. tabstractprocdef(result).calcparas;
  6887. end;
  6888. function tprocvardef.is_publishable : tpublishproperty;
  6889. begin
  6890. if po_methodpointer in procoptions then
  6891. is_publishable:=pp_publish
  6892. else
  6893. is_publishable:=pp_error;
  6894. end;
  6895. function tprocvardef.GetTypeName : string;
  6896. var
  6897. s: string;
  6898. pno : tprocnameoptions;
  6899. begin
  6900. {$ifdef EXTDEBUG}
  6901. pno:=[pno_showhidden];
  6902. {$else EXTDEBUG}
  6903. pno:=[];
  6904. {$endif EXTDEBUG}
  6905. s:='<';
  6906. if po_is_block in procoptions then
  6907. s := s+'reference to'
  6908. else if po_classmethod in procoptions then
  6909. s := s+'class method type of'
  6910. else
  6911. if po_addressonly in procoptions then
  6912. s := s+'address of'
  6913. else
  6914. s := s+'procedure variable type of';
  6915. if assigned(returndef) and
  6916. (returndef<>voidtype) then
  6917. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  6918. else
  6919. s:=s+' procedure'+typename_paras(pno);
  6920. if po_methodpointer in procoptions then
  6921. s := s+' of object';
  6922. if is_nested_pd(self) then
  6923. s := s+' is nested';
  6924. if po_far in procoptions then
  6925. s := s+';far';
  6926. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  6927. end;
  6928. {***************************************************************************
  6929. TOBJECTDEF
  6930. ***************************************************************************}
  6931. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  6932. begin
  6933. inherited create(n,objectdef,doregister);
  6934. fcurrent_dispid:=0;
  6935. objecttype:=ot;
  6936. childof:=nil;
  6937. childofderef.reset;
  6938. vmt_fieldderef.reset;
  6939. hiddenclassdefref.reset;
  6940. extendeddefderef.reset;
  6941. cloneddefderef.reset;
  6942. if objecttype=odt_helper then
  6943. owner.includeoption(sto_has_helper);
  6944. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  6945. current_settings.alignment.recordalignmin);
  6946. { create space for vmt !! }
  6947. vmtentries:=TFPList.Create;
  6948. set_parent(c);
  6949. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  6950. prepareguid;
  6951. { setup implemented interfaces }
  6952. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  6953. ImplementedInterfaces:=TFPObjectList.Create(true)
  6954. else
  6955. ImplementedInterfaces:=nil;
  6956. writing_class_record_dbginfo:=false;
  6957. end;
  6958. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  6959. var
  6960. i,
  6961. implintfcount : longint;
  6962. d, getterd : tderef;
  6963. ImplIntf : TImplementedInterface;
  6964. vmtentry : pvmtentry;
  6965. begin
  6966. inherited ppuload(objectdef,ppufile);
  6967. objecttype:=tobjecttyp(ppufile.getbyte);
  6968. helpertype:=thelpertype(ppufile.getbyte);
  6969. objextname:=ppufile.getpshortstring;
  6970. { only used for external Objective-C classes/protocols }
  6971. if (objextname^='') then
  6972. stringdispose(objextname);
  6973. symtable:=tObjectSymtable.create(self,objrealname^,0,0);
  6974. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  6975. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  6976. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  6977. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  6978. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  6979. ppufile.getderef(vmt_fieldderef);
  6980. ppufile.getderef(childofderef);
  6981. { load guid }
  6982. iidstr:=nil;
  6983. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6984. begin
  6985. new(iidguid);
  6986. ppufile.getguid(iidguid^);
  6987. iidstr:=ppufile.getpshortstring;
  6988. end;
  6989. abstractcnt:=ppufile.getlongint;
  6990. if objecttype=odt_helper then
  6991. ppufile.getderef(extendeddefderef);
  6992. vmtentries:=TFPList.Create;
  6993. vmtentries.count:=ppufile.getlongint;
  6994. for i:=0 to vmtentries.count-1 do
  6995. begin
  6996. ppufile.getderef(d);
  6997. new(vmtentry);
  6998. vmtentry^.procdef:=nil;
  6999. vmtentry^.procdefderef:=d;
  7000. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  7001. vmtentries[i]:=vmtentry;
  7002. end;
  7003. { load implemented interfaces }
  7004. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  7005. begin
  7006. ImplementedInterfaces:=TFPObjectList.Create(true);
  7007. implintfcount:=ppufile.getlongint;
  7008. for i:=0 to implintfcount-1 do
  7009. begin
  7010. ppufile.getderef(d);
  7011. ppufile.getderef(getterd);
  7012. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  7013. ImplIntf.IOffset:=ppufile.getlongint;
  7014. byte(ImplIntf.IType):=ppufile.getbyte;
  7015. ImplementedInterfaces.Add(ImplIntf);
  7016. end;
  7017. end
  7018. else
  7019. ImplementedInterfaces:=nil;
  7020. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7021. ppufile.getderef(hiddenclassdefref);
  7022. if df_copied_def in defoptions then
  7023. begin
  7024. ppufile.getderef(cloneddefderef);
  7025. ppuload_platform(ppufile);
  7026. end
  7027. else
  7028. begin
  7029. ppuload_platform(ppufile);
  7030. tObjectSymtable(symtable).ppuload(ppufile);
  7031. end;
  7032. { handles the predefined class tobject }
  7033. { the last TOBJECT which is loaded gets }
  7034. { it ! }
  7035. { but do this only from a unit that's }
  7036. { marked as system unit to avoid some }
  7037. { equally named user's type to override }
  7038. { the internal types! }
  7039. if mf_system_unit in current_module.moduleflags then
  7040. begin
  7041. if (childof=nil) and
  7042. (objecttype in [odt_class,odt_javaclass]) and
  7043. (objname^='TOBJECT') then
  7044. class_tobject:=self;
  7045. if (childof=nil) and
  7046. (objecttype=odt_interfacecom) then
  7047. if (objname^='IUNKNOWN') then
  7048. interface_iunknown:=self
  7049. else
  7050. if (objname^='IDISPATCH') then
  7051. interface_idispatch:=self;
  7052. if (objecttype=odt_javaclass) and
  7053. not(oo_is_formal in objectoptions) then
  7054. begin
  7055. if (objname^='JLOBJECT') then
  7056. java_jlobject:=self
  7057. else if (objname^='JLTHROWABLE') then
  7058. java_jlthrowable:=self
  7059. else if (objname^='FPCBASERECORDTYPE') then
  7060. java_fpcbaserecordtype:=self
  7061. else if (objname^='JLSTRING') then
  7062. java_jlstring:=self
  7063. else if (objname^='ANSISTRINGCLASS') then
  7064. java_ansistring:=self
  7065. else if (objname^='SHORTSTRINGCLASS') then
  7066. java_shortstring:=self
  7067. else if (objname^='JLENUM') then
  7068. java_jlenum:=self
  7069. else if (objname^='JUENUMSET') then
  7070. java_juenumset:=self
  7071. else if (objname^='FPCBITSET') then
  7072. java_jubitset:=self
  7073. else if (objname^='FPCBASEPROCVARTYPE') then
  7074. java_procvarbase:=self;
  7075. end;
  7076. end;
  7077. if (childof=nil) and
  7078. (objecttype=odt_objcclass) and
  7079. (objname^='PROTOCOL') then
  7080. objc_protocoltype:=self;
  7081. writing_class_record_dbginfo:=false;
  7082. end;
  7083. destructor tobjectdef.destroy;
  7084. begin
  7085. if assigned(symtable) then
  7086. begin
  7087. symtable.free;
  7088. symtable:=nil;
  7089. end;
  7090. stringdispose(objextname);
  7091. stringdispose(iidstr);
  7092. if assigned(ImplementedInterfaces) then
  7093. begin
  7094. ImplementedInterfaces.free;
  7095. ImplementedInterfaces:=nil;
  7096. end;
  7097. if assigned(iidguid) then
  7098. begin
  7099. dispose(iidguid);
  7100. iidguid:=nil;
  7101. end;
  7102. TFPList.FreeAndNilDisposing(vmtentries,TypeInfo(tvmtentry));
  7103. if assigned(vmcallstaticinfo) then
  7104. begin
  7105. freemem(vmcallstaticinfo);
  7106. vmcallstaticinfo:=nil;
  7107. end;
  7108. inherited destroy;
  7109. end;
  7110. function tobjectdef.getcopy : tstoreddef;
  7111. var
  7112. i : longint;
  7113. begin
  7114. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  7115. { the constructor allocates a symtable which we release to avoid memory leaks }
  7116. tobjectdef(result).symtable.free;
  7117. tobjectdef(result).symtable:=symtable.getcopy;
  7118. if assigned(objextname) then
  7119. tobjectdef(result).objextname:=stringdup(objextname^);
  7120. if assigned(import_lib) then
  7121. tobjectdef(result).import_lib:=stringdup(import_lib^);
  7122. tobjectdef(result).objectoptions:=objectoptions;
  7123. include(tobjectdef(result).defoptions,df_copied_def);
  7124. tobjectdef(result).extendeddef:=extendeddef;
  7125. if assigned(tcinitcode) then
  7126. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  7127. tobjectdef(result).vmt_field:=vmt_field;
  7128. if assigned(iidguid) then
  7129. begin
  7130. new(tobjectdef(result).iidguid);
  7131. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  7132. end;
  7133. if assigned(iidstr) then
  7134. tobjectdef(result).iidstr:=stringdup(iidstr^);
  7135. tobjectdef(result).abstractcnt:=abstractcnt;
  7136. if assigned(ImplementedInterfaces) then
  7137. begin
  7138. tobjectdef(result).ImplementedInterfaces.capacity:=tobjectdef(result).ImplementedInterfaces.count+ImplementedInterfaces.count;
  7139. for i:=0 to ImplementedInterfaces.count-1 do
  7140. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  7141. end;
  7142. tobjectdef(result).copyvmtentries(self);
  7143. end;
  7144. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  7145. var
  7146. i : longint;
  7147. vmtentry : pvmtentry;
  7148. ImplIntf : TImplementedInterface;
  7149. old_do_indirect_crc: boolean;
  7150. begin
  7151. { if class1 in unit A changes, and class2 in unit B inherits from it
  7152. (so unit B uses unit A), then unit B with class2 will be recompiled.
  7153. However, if there is also a class3 in unit C that only depends on
  7154. unit B, then unit C will not be recompiled because nothing changed
  7155. to the interface of unit B. Nevertheless, unit C can indirectly
  7156. depend on unit A via derefs, and these must be updated -> the
  7157. indirect crc keeps track of such changes. }
  7158. old_do_indirect_crc:=ppufile.do_indirect_crc;
  7159. ppufile.do_indirect_crc:=true;
  7160. inherited ppuwrite(ppufile);
  7161. ppufile.putbyte(byte(objecttype));
  7162. ppufile.putbyte(byte(helpertype));
  7163. if assigned(objextname) then
  7164. ppufile.putstring(objextname^)
  7165. else
  7166. ppufile.putstring('');
  7167. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  7168. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  7169. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  7170. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  7171. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  7172. ppufile.putderef(vmt_fieldderef);
  7173. ppufile.putderef(childofderef);
  7174. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  7175. begin
  7176. ppufile.putguid(iidguid^);
  7177. ppufile.putstring(iidstr^);
  7178. end;
  7179. ppufile.putlongint(abstractcnt);
  7180. if objecttype=odt_helper then
  7181. ppufile.putderef(extendeddefderef);
  7182. ppufile.putlongint(vmtentries.count);
  7183. for i:=0 to vmtentries.count-1 do
  7184. begin
  7185. vmtentry:=pvmtentry(vmtentries[i]);
  7186. ppufile.putderef(vmtentry^.procdefderef);
  7187. ppufile.putbyte(byte(vmtentry^.visibility));
  7188. end;
  7189. if assigned(ImplementedInterfaces) then
  7190. begin
  7191. ppufile.putlongint(ImplementedInterfaces.Count);
  7192. for i:=0 to ImplementedInterfaces.Count-1 do
  7193. begin
  7194. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  7195. ppufile.putderef(ImplIntf.intfdefderef);
  7196. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  7197. ppufile.putlongint(ImplIntf.Ioffset);
  7198. ppufile.putbyte(byte(ImplIntf.IType));
  7199. end;
  7200. end;
  7201. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7202. ppufile.putderef(hiddenclassdefref);
  7203. if df_copied_def in defoptions then
  7204. ppufile.putderef(cloneddefderef);
  7205. writeentry(ppufile,ibobjectdef);
  7206. if not(df_copied_def in defoptions) then
  7207. tObjectSymtable(symtable).ppuwrite(ppufile);
  7208. ppufile.do_indirect_crc:=old_do_indirect_crc;
  7209. end;
  7210. function tobjectdef.GetTypeName:string;
  7211. begin
  7212. { in this case we will go in endless recursion, because then }
  7213. { there is no tsym associated yet with the def. It can occur }
  7214. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  7215. { instead of the actual type name }
  7216. if not assigned(typesym) then
  7217. result:='<Currently Parsed Class>'
  7218. else
  7219. result:=typesymbolprettyname;
  7220. end;
  7221. procedure tobjectdef.buildderef;
  7222. var
  7223. i : longint;
  7224. vmtentry : pvmtentry;
  7225. begin
  7226. inherited buildderef;
  7227. vmt_fieldderef.build(vmt_field);
  7228. childofderef.build(childof);
  7229. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7230. hiddenclassdefref.build(hiddenclassdef);
  7231. if df_copied_def in defoptions then
  7232. cloneddefderef.build(symtable.defowner)
  7233. else
  7234. tstoredsymtable(symtable).buildderef;
  7235. if objecttype=odt_helper then
  7236. extendeddefderef.build(extendeddef);
  7237. for i:=0 to vmtentries.count-1 do
  7238. begin
  7239. vmtentry:=pvmtentry(vmtentries[i]);
  7240. vmtentry^.procdefderef.build(vmtentry^.procdef);
  7241. end;
  7242. if assigned(ImplementedInterfaces) then
  7243. begin
  7244. for i:=0 to ImplementedInterfaces.count-1 do
  7245. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  7246. end;
  7247. end;
  7248. procedure tobjectdef.deref;
  7249. var
  7250. i : longint;
  7251. vmtentry : pvmtentry;
  7252. begin
  7253. inherited deref;
  7254. vmt_field:=tsym(vmt_fieldderef.resolve);
  7255. childof:=tobjectdef(childofderef.resolve);
  7256. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7257. hiddenclassdef:=tobjectdef(hiddenclassdefref.resolve);
  7258. if df_copied_def in defoptions then
  7259. begin
  7260. cloneddef:=tobjectdef(cloneddefderef.resolve);
  7261. symtable:=cloneddef.symtable.getcopy;
  7262. end
  7263. else
  7264. tstoredsymtable(symtable).deref(false);
  7265. if objecttype=odt_helper then
  7266. extendeddef:=tdef(extendeddefderef.resolve);
  7267. for i:=0 to vmtentries.count-1 do
  7268. begin
  7269. vmtentry:=pvmtentry(vmtentries[i]);
  7270. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  7271. end;
  7272. if assigned(ImplementedInterfaces) then
  7273. begin
  7274. for i:=0 to ImplementedInterfaces.count-1 do
  7275. TImplementedInterface(ImplementedInterfaces[i]).deref;
  7276. end;
  7277. end;
  7278. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  7279. var
  7280. pd: tprocdef absolute def;
  7281. st: tsymtable;
  7282. psym: tsym;
  7283. nname: TIDString;
  7284. begin
  7285. if (tdef(def).typ<>procdef) then
  7286. exit;
  7287. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  7288. owner = symtable in which objcclassdef is defined
  7289. }
  7290. st:=pd.owner.defowner.owner;
  7291. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  7292. { check for an existing procsym with our special name }
  7293. psym:=tsym(st.find(nname));
  7294. if not assigned(psym) then
  7295. begin
  7296. psym:=cprocsym.create(nname);
  7297. { avoid warning about this symbol being unused }
  7298. psym.IncRefCount;
  7299. { don't check for duplicates:
  7300. a) we checked above
  7301. b) in case we are in the implementation section of a unit, this
  7302. will also check for this symbol in the interface section
  7303. (since you normally cannot have symbols with the same name
  7304. both interface and implementation), and it's possible to
  7305. have class helpers for the same class in the interface and
  7306. in the implementation, and they cannot be merged since only
  7307. the once in the interface must be saved to the ppu/visible
  7308. from other units }
  7309. st.insertsym(psym,false);
  7310. end
  7311. else if (psym.typ<>procsym) then
  7312. internalerror(2009111501);
  7313. { add ourselves to this special procsym }
  7314. tprocsym(psym).procdeflist.add(def);
  7315. end;
  7316. procedure tobjectdef.derefimpl;
  7317. begin
  7318. inherited derefimpl;
  7319. { the procdefs are not owned by the class helper procsyms, so they
  7320. are not stored/restored either -> re-add them here }
  7321. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  7322. (oo_is_classhelper in objectoptions) then
  7323. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  7324. end;
  7325. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  7326. var
  7327. i : longint;
  7328. vmtentry : pvmtentry;
  7329. begin
  7330. if vmtentries.count<>0 then
  7331. internalerror(2019081401);
  7332. vmtentries.count:=objdef.vmtentries.count;
  7333. for i:=0 to objdef.vmtentries.count-1 do
  7334. begin
  7335. new(vmtentry);
  7336. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  7337. vmtentries[i]:=vmtentry;
  7338. end;
  7339. end;
  7340. function tobjectdef.getparentdef:tdef;
  7341. begin
  7342. { TODO: Remove getparentdef hack}
  7343. { With 2 forward declared classes with the child class before the
  7344. parent class the child class is written earlier to the ppu. Leaving it
  7345. possible to have a reference to the parent class for property overriding,
  7346. but the parent class still has the childof not resolved yet (PFV) }
  7347. if childof=nil then
  7348. childof:=tobjectdef(childofderef.resolve);
  7349. result:=childof;
  7350. end;
  7351. procedure tobjectdef.prepareguid;
  7352. begin
  7353. { set up guid }
  7354. if not assigned(iidguid) then
  7355. begin
  7356. new(iidguid);
  7357. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  7358. end;
  7359. { setup iidstring }
  7360. if not assigned(iidstr) then
  7361. iidstr:=stringdup(''); { default is empty string }
  7362. end;
  7363. procedure tobjectdef.set_parent( c : tobjectdef);
  7364. begin
  7365. if assigned(childof) then
  7366. exit;
  7367. childof:=c;
  7368. if not assigned(c) then
  7369. exit;
  7370. { inherit options and status }
  7371. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  7372. { check if parent is a generic parameter }
  7373. if sp_generic_para in c.typesym.symoptions then
  7374. objectoptions:=objectoptions+[oo_inherits_not_specialized];
  7375. { initially has the same number of abstract methods as the parent }
  7376. abstractcnt:=c.abstractcnt;
  7377. { add the data of the anchestor class/object }
  7378. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  7379. begin
  7380. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  7381. { inherit recordalignment }
  7382. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  7383. { if both the parent and this record use C-alignment, also inherit
  7384. the current field alignment }
  7385. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  7386. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  7387. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  7388. { the padding is not inherited for Objective-C classes (maybe not
  7389. for cppclass either?) }
  7390. if objecttype=odt_objcclass then
  7391. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  7392. if (oo_has_vmt in objectoptions) and
  7393. (oo_has_vmt in c.objectoptions) then
  7394. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  7395. { if parent has a vmt field then the offset is the same for the child PM }
  7396. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  7397. begin
  7398. vmt_field:=c.vmt_field;
  7399. include(objectoptions,oo_has_vmt);
  7400. end;
  7401. end;
  7402. end;
  7403. procedure tobjectdef.insertvmt;
  7404. begin
  7405. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7406. exit;
  7407. if (oo_has_vmt in objectoptions) then
  7408. internalerror(2020100816)
  7409. else
  7410. begin
  7411. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  7412. tObjectSymtable(symtable).fieldalignment);
  7413. if (tf_requires_proper_alignment in target_info.flags) then
  7414. begin
  7415. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  7416. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  7417. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  7418. end;
  7419. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  7420. hidesym(vmt_field);
  7421. tObjectSymtable(symtable).insertsym(vmt_field);
  7422. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  7423. include(objectoptions,oo_has_vmt);
  7424. end;
  7425. end;
  7426. function tobjectdef.vmt_offset: asizeint;
  7427. begin
  7428. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7429. result:=0
  7430. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  7431. result:=tfieldvarsym(vmt_field).fieldoffset
  7432. else
  7433. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  7434. end;
  7435. procedure tobjectdef.check_forwards;
  7436. begin
  7437. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  7438. inherited;
  7439. if (oo_is_forward in objectoptions) then
  7440. begin
  7441. { ok, in future, the forward can be resolved }
  7442. Message1(sym_e_class_forward_not_resolved,objrealname^);
  7443. exclude(objectoptions,oo_is_forward);
  7444. end;
  7445. end;
  7446. function tobjectdef.find_destructor: tprocdef;
  7447. var
  7448. objdef: tobjectdef;
  7449. begin
  7450. objdef:=self;
  7451. while assigned(objdef) do
  7452. begin
  7453. result:=objdef.find_procdef_bytype(potype_destructor);
  7454. if assigned(result) then
  7455. exit;
  7456. objdef:=objdef.childof;
  7457. end;
  7458. result:=nil;
  7459. end;
  7460. function tobjectdef.implements_any_interfaces: boolean;
  7461. begin
  7462. result := (ImplementedInterfaces.Count > 0) or
  7463. (assigned(childof) and childof.implements_any_interfaces);
  7464. end;
  7465. function tobjectdef.register_implemented_interface(intfdef:tobjectdef;useguid:boolean):timplementedinterface;
  7466. begin
  7467. { allocate the GUID only if the class implements at least one interface }
  7468. if useguid then
  7469. prepareguid;
  7470. result:=timplementedinterface.create(intfdef);
  7471. ImplementedInterfaces.Add(result);
  7472. end;
  7473. function tobjectdef.size : asizeint;
  7474. begin
  7475. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7476. result:=voidpointertype.size
  7477. else
  7478. result:=tObjectSymtable(symtable).datasize;
  7479. end;
  7480. function tobjectdef.alignment:shortint;
  7481. begin
  7482. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7483. alignment:=voidpointertype.alignment
  7484. else
  7485. alignment:=tObjectSymtable(symtable).recordalignment;
  7486. end;
  7487. function tobjectdef.vmtmethodoffset(index:longint):longint;
  7488. begin
  7489. { for offset of methods for classes, see rtl/inc/objpash.inc }
  7490. case objecttype of
  7491. odt_class:
  7492. { the +2*sizeof(pint) is size and -size }
  7493. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  7494. odt_helper,
  7495. odt_objcclass,
  7496. odt_objcprotocol:
  7497. vmtmethodoffset:=0;
  7498. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  7499. vmtmethodoffset:=index*voidcodepointertype.size;
  7500. odt_javaclass,
  7501. odt_interfacejava:
  7502. { invalid }
  7503. vmtmethodoffset:=-1;
  7504. else
  7505. { the +2*sizeof(pint) is size and -size }
  7506. {$ifdef WITHDMT}
  7507. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  7508. {$else WITHDMT}
  7509. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  7510. {$endif WITHDMT}
  7511. end;
  7512. end;
  7513. function tobjectdef.vmt_mangledname : TSymStr;
  7514. begin
  7515. if not(oo_has_vmt in objectoptions) then
  7516. Message1(parser_n_object_has_no_vmt,objrealname^);
  7517. if not is_unique_objpasdef then
  7518. vmt_mangledname:=make_mangledname('VMT',owner,objname^)
  7519. else
  7520. vmt_mangledname:=childof.vmt_mangledname;
  7521. end;
  7522. function tobjectdef.vmt_def: trecorddef;
  7523. var
  7524. where: tsymtable;
  7525. vmttypesym: tsymentry;
  7526. begin
  7527. if not is_unique_objpasdef then
  7528. begin
  7529. vmttypesym:=symtable.Find('vmtdef');
  7530. if not assigned(vmttypesym) or
  7531. (vmttypesym.typ<>symconst.typesym) or
  7532. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  7533. internalerror(2015052501);
  7534. result:=trecorddef(ttypesym(vmttypesym).typedef);
  7535. end
  7536. else
  7537. result:=childof.vmt_def;
  7538. end;
  7539. function tobjectdef.needs_inittable : boolean;
  7540. var
  7541. hp : tobjectdef;
  7542. begin
  7543. case objecttype of
  7544. odt_helper,
  7545. odt_class :
  7546. needs_inittable:=false;
  7547. odt_dispinterface,
  7548. odt_interfacecom:
  7549. needs_inittable:=true;
  7550. odt_interfacecorba:
  7551. begin
  7552. hp:=childof;
  7553. while assigned(hp) do
  7554. begin
  7555. if hp=interface_iunknown then
  7556. begin
  7557. needs_inittable:=true;
  7558. exit;
  7559. end;
  7560. hp:=hp.childof;
  7561. end;
  7562. needs_inittable:=false;
  7563. end;
  7564. odt_object:
  7565. needs_inittable:=
  7566. tObjectSymtable(symtable).needs_init_final or
  7567. (assigned(childof) and
  7568. childof.needs_inittable);
  7569. odt_cppclass,
  7570. odt_objcclass,
  7571. odt_objcprotocol,
  7572. odt_javaclass,
  7573. odt_interfacejava:
  7574. needs_inittable:=false;
  7575. else
  7576. internalerror(200108267);
  7577. end;
  7578. end;
  7579. function tobjectdef.needs_separate_initrtti : boolean;
  7580. begin
  7581. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7582. end;
  7583. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  7584. begin
  7585. if objecttype in [odt_class,odt_object] then
  7586. begin
  7587. result:=tobjectsymtable(symtable).has_non_trivial_init or
  7588. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  7589. end
  7590. else
  7591. result:=false;
  7592. end;
  7593. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  7594. begin
  7595. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  7596. begin
  7597. if not is_unique_objpasdef then
  7598. result:=inherited rtti_mangledname(rt)
  7599. else
  7600. result:=childof.rtti_mangledname(rt)
  7601. end
  7602. else
  7603. begin
  7604. { necessary in case of a dynamic array of nsobject, or
  7605. if an nsobject field appears in a record that needs
  7606. init/finalisation }
  7607. if rt=initrtti then
  7608. begin
  7609. result:=voidpointertype.rtti_mangledname(rt);
  7610. exit;
  7611. end;
  7612. if not(target_info.system in systems_objc_nfabi) then
  7613. begin
  7614. result:=target_asm.labelprefix;
  7615. case objecttype of
  7616. odt_objcclass:
  7617. begin
  7618. case rt of
  7619. objcclassrtti:
  7620. if not(oo_is_classhelper in objectoptions) then
  7621. result:=result+'_OBJC_CLASS_'
  7622. else
  7623. result:=result+'_OBJC_CATEGORY_';
  7624. objcmetartti:
  7625. if not(oo_is_classhelper in objectoptions) then
  7626. result:=result+'_OBJC_METACLASS_'
  7627. else
  7628. internalerror(2009111511);
  7629. else
  7630. internalerror(2009092302);
  7631. end;
  7632. end;
  7633. odt_objcprotocol:
  7634. result:=result+'_OBJC_PROTOCOL_';
  7635. else
  7636. ;
  7637. end;
  7638. end
  7639. else
  7640. begin
  7641. case objecttype of
  7642. odt_objcclass:
  7643. begin
  7644. if (oo_is_classhelper in objectoptions) and
  7645. (rt<>objcclassrtti) then
  7646. internalerror(2009111512);
  7647. case rt of
  7648. objcclassrtti:
  7649. if not(oo_is_classhelper in objectoptions) then
  7650. result:='_OBJC_CLASS_$_'
  7651. else
  7652. result:='_OBJC_$_CATEGORY_';
  7653. objcmetartti:
  7654. result:='_OBJC_METACLASS_$_';
  7655. objcclassrortti:
  7656. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  7657. objcmetarortti:
  7658. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  7659. else
  7660. internalerror(2009092303);
  7661. end;
  7662. end;
  7663. odt_objcprotocol:
  7664. begin
  7665. result:=lower(target_asm.labelprefix);
  7666. case rt of
  7667. objcclassrtti:
  7668. result:=result+'_OBJC_PROTOCOL_$_';
  7669. objcmetartti:
  7670. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  7671. else
  7672. internalerror(2009092501);
  7673. end;
  7674. end;
  7675. else
  7676. internalerror(2013113005);
  7677. end;
  7678. end;
  7679. result:=result+objextname^;
  7680. end;
  7681. end;
  7682. function tobjectdef.is_unique_objpasdef: boolean;
  7683. begin
  7684. result:=
  7685. (df_unique in defoptions) and
  7686. is_class_or_interface_or_dispinterface(self)
  7687. end;
  7688. function tobjectdef.members_need_inittable : boolean;
  7689. begin
  7690. members_need_inittable:=tObjectSymtable(symtable).needs_init_final or
  7691. has_non_trivial_init_child(true);
  7692. end;
  7693. function tobjectdef.is_publishable : tpublishproperty;
  7694. begin
  7695. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  7696. is_publishable:=pp_publish
  7697. else
  7698. is_publishable:=pp_error;
  7699. end;
  7700. function tobjectdef.get_next_dispid: longint;
  7701. begin
  7702. inc(fcurrent_dispid);
  7703. result:=fcurrent_dispid;
  7704. end;
  7705. function tobjectdef.search_enumerator_get: tprocdef;
  7706. begin
  7707. result:=inherited;
  7708. if not assigned(result) and assigned(childof) then
  7709. result:=childof.search_enumerator_get;
  7710. end;
  7711. function tobjectdef.search_enumerator_move: tprocdef;
  7712. begin
  7713. result:=inherited;
  7714. if not assigned(result) and assigned(childof) then
  7715. result:=childof.search_enumerator_move;
  7716. end;
  7717. function tobjectdef.search_enumerator_current: tsym;
  7718. begin
  7719. result:=inherited;
  7720. if not assigned(result) and assigned(childof) then
  7721. result:=childof.search_enumerator_current;
  7722. end;
  7723. procedure tobjectdef.register_created_classref_type;
  7724. begin
  7725. if not classref_created_in_current_module then
  7726. begin
  7727. classref_created_in_current_module:=true;
  7728. if not (owner.symtabletype in [localsymtable]) then
  7729. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  7730. end;
  7731. end;
  7732. procedure tobjectdef.register_created_object_type;
  7733. begin
  7734. if not created_in_current_module then
  7735. begin
  7736. created_in_current_module:=true;
  7737. if not (owner.symtabletype in [localsymtable]) then
  7738. current_module.wpoinfo.addcreatedobjtype(self);
  7739. end;
  7740. end;
  7741. procedure tobjectdef.register_maybe_created_object_type;
  7742. begin
  7743. { if we know it has been created for sure, no need
  7744. to also record that it maybe can be created in
  7745. this module
  7746. }
  7747. if not (created_in_current_module) and
  7748. not (maybe_created_in_current_module) then
  7749. begin
  7750. maybe_created_in_current_module:=true;
  7751. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  7752. end;
  7753. end;
  7754. procedure tobjectdef.register_vmt_call(index: longint);
  7755. begin
  7756. if (is_object(self) or is_class(self)) then
  7757. current_module.wpoinfo.addcalledvmtentry(self,index);
  7758. end;
  7759. procedure check_and_finish_msg(data: tobject; arg: pointer);
  7760. var
  7761. def: tdef absolute data;
  7762. pd: tprocdef absolute data;
  7763. i,
  7764. paracount: longint;
  7765. begin
  7766. if (def.typ=procdef) then
  7767. begin
  7768. { add all messages also under a dummy name to the symtable in
  7769. which the objcclass/protocol/category is declared, so they can
  7770. be called via id.<name>
  7771. }
  7772. create_class_helper_for_procdef(pd,nil);
  7773. { we have to wait until now to set the mangled name because it
  7774. depends on the (possibly external) class name, which is defined
  7775. at the very end. }
  7776. if not(po_msgstr in pd.procoptions) then
  7777. begin
  7778. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  7779. { recover to avoid internalerror later on }
  7780. include(pd.procoptions,po_msgstr);
  7781. pd.messageinf.str:=stringdup('MissingDeclaration');
  7782. end;
  7783. { Mangled name is already set in case this is a copy of
  7784. another type. }
  7785. if not(po_has_mangledname in pd.procoptions) then
  7786. begin
  7787. { check whether the number of formal parameters is correct,
  7788. and whether they have valid Objective-C types }
  7789. paracount:=0;
  7790. for i:=1 to length(pd.messageinf.str^) do
  7791. if pd.messageinf.str^[i]=':' then
  7792. inc(paracount);
  7793. for i:=0 to pd.paras.count-1 do
  7794. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  7795. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  7796. dec(paracount);
  7797. if (paracount<>0) then
  7798. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  7799. pd.setmangledname(pd.objcmangledname);
  7800. end
  7801. else
  7802. { all checks already done }
  7803. exit;
  7804. if not(oo_is_external in pd.struct.objectoptions) then
  7805. begin
  7806. if (po_varargs in pd.procoptions) then
  7807. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  7808. else
  7809. begin
  7810. { check for "array of const" parameters }
  7811. for i:=0 to pd.parast.symlist.count-1 do
  7812. begin
  7813. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  7814. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  7815. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  7816. end;
  7817. end;
  7818. end;
  7819. end;
  7820. end;
  7821. procedure mark_private_fields_used(data: tobject; arg: pointer);
  7822. var
  7823. sym: tsym absolute data;
  7824. begin
  7825. if (sym.typ=fieldvarsym) and
  7826. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  7827. sym.IncRefCount;
  7828. end;
  7829. procedure tobjectdef.finish_objc_data;
  7830. begin
  7831. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  7832. if (oo_is_external in objectoptions) then
  7833. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  7834. end;
  7835. procedure verify_objc_vardef(data: tobject; arg: pointer);
  7836. var
  7837. sym: tabstractvarsym absolute data;
  7838. res: pboolean absolute arg;
  7839. founderrordef: tdef;
  7840. begin
  7841. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  7842. exit;
  7843. if (sym.typ=paravarsym) and
  7844. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  7845. is_array_of_const(tparavarsym(sym).vardef)) then
  7846. exit;
  7847. if not objcchecktype(sym.vardef,founderrordef) then
  7848. begin
  7849. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7850. res^:=false;
  7851. end;
  7852. end;
  7853. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  7854. var
  7855. def: tdef absolute data;
  7856. res: pboolean absolute arg;
  7857. founderrordef: tdef;
  7858. begin
  7859. if (def.typ<>procdef) then
  7860. exit;
  7861. { check parameter types for validity }
  7862. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  7863. { check the result type for validity }
  7864. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  7865. begin
  7866. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7867. res^:=false;
  7868. end;
  7869. end;
  7870. function tobjectdef.check_objc_types: boolean;
  7871. begin
  7872. { done in separate step from finish_objc_data, because when
  7873. finish_objc_data is called, not all forwarddefs have been resolved
  7874. yet and we need to know all types here }
  7875. result:=true;
  7876. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  7877. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  7878. end;
  7879. procedure do_cpp_import_info(data: tobject; arg: pointer);
  7880. var
  7881. def: tdef absolute data;
  7882. pd: tprocdef absolute data;
  7883. begin
  7884. if (def.typ=procdef) then
  7885. begin
  7886. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  7887. if (oo_is_external in pd.struct.objectoptions) then
  7888. begin
  7889. { copied from psub.read_proc }
  7890. if assigned(tobjectdef(pd.struct).import_lib) then
  7891. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  7892. else
  7893. begin
  7894. { add import name to external list for DLL scanning }
  7895. if tf_has_dllscanner in target_info.flags then
  7896. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  7897. end;
  7898. end;
  7899. end;
  7900. end;
  7901. procedure tobjectdef.finish_cpp_data;
  7902. begin
  7903. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  7904. end;
  7905. procedure tobjectdef.apply_rtti_directive(dir: trtti_directive);
  7906. begin
  7907. rtti.clause:=dir.clause;
  7908. rtti.options:=dir.options;
  7909. if (dir.clause=rtc_inherit) and assigned(childof) and (childof.rtti.clause<>rtc_none) then
  7910. begin
  7911. rtti.options[ro_methods]:=rtti.options[ro_methods]+childof.rtti.options[ro_methods];
  7912. rtti.options[ro_fields]:=rtti.options[ro_fields]+childof.rtti.options[ro_fields];
  7913. rtti.options[ro_properties]:=rtti.options[ro_properties]+childof.rtti.options[ro_properties];
  7914. end;
  7915. end;
  7916. {$ifdef DEBUG_NODE_XML}
  7917. function TObjectDef.XMLPrintType: ansistring;
  7918. begin
  7919. if (oo_is_forward in objectoptions) then
  7920. Result := '&lt;class prototype&gt;'
  7921. else
  7922. Result := '&lt;class&gt;';
  7923. end;
  7924. procedure TObjectDef.XMLPrintDefInfo(var T: Text; Sym: TSym);
  7925. var
  7926. i: TObjectOption;
  7927. first: Boolean;
  7928. begin
  7929. inherited XMLPrintDefInfo(T, Sym);
  7930. First := True;
  7931. for i := Low(TObjectOption) to High(TObjectOption) do
  7932. if i in objectoptions then
  7933. begin
  7934. if First then
  7935. begin
  7936. Write(T, ' objectoptions="', i);
  7937. First := False;
  7938. end
  7939. else
  7940. Write(T, ',', i)
  7941. end;
  7942. if not first then
  7943. Write(T, '"');
  7944. end;
  7945. procedure TObjectDef.XMLPrintDefData(var T: Text; Sym: TSym);
  7946. begin
  7947. { There's nothing useful yet if the type is only forward-declared }
  7948. if not (oo_is_forward in objectoptions) then
  7949. begin
  7950. if Assigned(childof) then
  7951. WriteLn(T, printnodeindention, '<ancestor>', SanitiseXMLString(childof.typesym.RealName), '</ancestor>');
  7952. inherited XMLPrintDefData(T, Sym);
  7953. end;
  7954. end;
  7955. {$endif DEBUG_NODE_XML}
  7956. {****************************************************************************
  7957. TImplementedInterface
  7958. ****************************************************************************}
  7959. function TImplementedInterface.GetIOffset: longint;
  7960. begin
  7961. if (fIOffset=-1) and
  7962. (IType in [etFieldValue,etFieldValueClass]) then
  7963. result:=tfieldvarsym(ImplementsField).fieldoffset
  7964. else
  7965. result:=fIOffset;
  7966. end;
  7967. constructor TImplementedInterface.create(aintf: tobjectdef);
  7968. begin
  7969. inherited create;
  7970. intfdef:=aintf;
  7971. intfdefderef.reset;
  7972. IOffset:=-1;
  7973. IType:=etStandard;
  7974. NameMappings:=nil;
  7975. procdefs:=nil;
  7976. end;
  7977. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  7978. begin
  7979. inherited create;
  7980. intfdef:=nil;
  7981. intfdefderef:=intfd;
  7982. ImplementsGetterDeref:=getterd;
  7983. IOffset:=-1;
  7984. IType:=etStandard;
  7985. NameMappings:=nil;
  7986. procdefs:=nil;
  7987. end;
  7988. destructor TImplementedInterface.destroy;
  7989. var
  7990. i : longint;
  7991. mappedname : pshortstring;
  7992. begin
  7993. if assigned(NameMappings) then
  7994. begin
  7995. for i:=0 to NameMappings.Count-1 do
  7996. begin
  7997. mappedname:=pshortstring(NameMappings[i]);
  7998. stringdispose(mappedname);
  7999. end;
  8000. NameMappings.free;
  8001. NameMappings:=nil;
  8002. end;
  8003. if assigned(procdefs) then
  8004. begin
  8005. procdefs.free;
  8006. procdefs:=nil;
  8007. end;
  8008. inherited destroy;
  8009. end;
  8010. procedure TImplementedInterface.buildderef;
  8011. begin
  8012. intfdefderef.build(intfdef);
  8013. ImplementsGetterDeref.build(ImplementsGetter);
  8014. end;
  8015. procedure TImplementedInterface.deref;
  8016. begin
  8017. intfdef:=tobjectdef(intfdefderef.resolve);
  8018. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  8019. end;
  8020. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  8021. begin
  8022. if not assigned(NameMappings) then
  8023. NameMappings:=TFPHashList.Create;
  8024. NameMappings.Add(origname,stringdup(newname));
  8025. end;
  8026. function TImplementedInterface.GetMapping(const origname: string):string;
  8027. var
  8028. mappedname : pshortstring;
  8029. begin
  8030. result:='';
  8031. if not assigned(NameMappings) then
  8032. exit;
  8033. mappedname:=PShortstring(NameMappings.Find(origname));
  8034. if assigned(mappedname) then
  8035. result:=mappedname^;
  8036. end;
  8037. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  8038. begin
  8039. if not assigned(procdefs) then
  8040. procdefs:=TFPObjectList.Create(false);
  8041. { duplicate entries must be stored, because multiple }
  8042. { interfaces can declare methods with the same name }
  8043. { and all of these get their own VMT entry }
  8044. procdefs.Add(pd);
  8045. end;
  8046. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  8047. var
  8048. i : longint;
  8049. begin
  8050. result:=false;
  8051. { interfaces being implemented through delegation are not mergable (FK) }
  8052. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  8053. exit;
  8054. weight:=0;
  8055. { empty interface is mergeable }
  8056. if ProcDefs.Count=0 then
  8057. begin
  8058. result:=true;
  8059. exit;
  8060. end;
  8061. { The interface to merge must at least the number of
  8062. procedures of this interface }
  8063. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  8064. exit;
  8065. for i:=0 to ProcDefs.Count-1 do
  8066. begin
  8067. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  8068. exit;
  8069. end;
  8070. weight:=ProcDefs.Count;
  8071. result:=true;
  8072. end;
  8073. function TImplementedInterface.getcopy:TImplementedInterface;
  8074. var
  8075. i : longint;
  8076. begin
  8077. Result:=TImplementedInterface.Create(nil);
  8078. { 1) the procdefs list will be freed once for each copy
  8079. 2) since the procdefs list owns its elements, those will also be freed for each copy
  8080. Nope: procdefs are owned by their symtable, so no copy necessary
  8081. 3) idem for the name mappings
  8082. }
  8083. result.fIOffset:=fIOffset;
  8084. result.IntfDef:=IntfDef;
  8085. result.IntfDefDeref.reset;
  8086. result.IType:=IType;
  8087. result.VtblImplIntf:=VtblImplIntf;
  8088. if assigned(NameMappings) then
  8089. begin
  8090. result.NameMappings:=TFPHashList.create;
  8091. for i:=0 to NameMappings.Count-1 do
  8092. Result.NameMappings.Add(NameMappings.NameOfIndex(i),
  8093. stringdup(pshortstring(NameMappings.Items[i])^));
  8094. end;
  8095. if assigned(ProcDefs) then
  8096. begin
  8097. result.ProcDefs:=TFPObjectList.create(false);
  8098. { Note: this is probably wrong, because those procdefs are owned by
  8099. the old objectdef from which we copy, what would be the correct way
  8100. of doing this is to lookup the equivalent copy in the new owner
  8101. and reference this instead... But this is complicated so let's try
  8102. it this way until it blows up ok? }
  8103. for i:=0 to ProcDefs.Count-1 do
  8104. Result.ProcDefs.add(tprocdef(procdefs[i]).getcopy);
  8105. end;
  8106. result.ImplementsGetter:=ImplementsGetter;
  8107. result.ImplementsGetterDeref.reset;
  8108. result.ImplementsField:=ImplementsField;
  8109. end;
  8110. {****************************************************************************
  8111. TFORWARDDEF
  8112. ****************************************************************************}
  8113. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  8114. begin
  8115. inherited create(forwarddef,true);
  8116. tosymname:=stringdup(s);
  8117. forwardpos:=pos;
  8118. end;
  8119. function tforwarddef.GetTypeName:string;
  8120. begin
  8121. GetTypeName:='unresolved forward to '+tosymname^;
  8122. end;
  8123. destructor tforwarddef.destroy;
  8124. begin
  8125. stringdispose(tosymname);
  8126. inherited destroy;
  8127. end;
  8128. function tforwarddef.getcopy:tstoreddef;
  8129. begin
  8130. result:=cforwarddef.create(tosymname^, forwardpos);
  8131. end;
  8132. {****************************************************************************
  8133. TUNDEFINEDDEF
  8134. ****************************************************************************}
  8135. constructor tundefineddef.create(doregister:boolean);
  8136. begin
  8137. inherited create(undefineddef,doregister);
  8138. end;
  8139. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  8140. begin
  8141. inherited ppuload(undefineddef,ppufile);
  8142. ppuload_platform(ppufile);
  8143. end;
  8144. function tundefineddef.GetTypeName:string;
  8145. begin
  8146. GetTypeName:='<undefined type>';
  8147. end;
  8148. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  8149. begin
  8150. inherited ppuwrite(ppufile);
  8151. writeentry(ppufile,ibundefineddef);
  8152. end;
  8153. {****************************************************************************
  8154. TERRORDEF
  8155. ****************************************************************************}
  8156. constructor terrordef.create;
  8157. begin
  8158. inherited create(errordef,true);
  8159. { prevent consecutive faults }
  8160. savesize:=1;
  8161. end;
  8162. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  8163. begin
  8164. { Can't write errordefs to ppu }
  8165. internalerror(200411063);
  8166. end;
  8167. function terrordef.GetTypeName:string;
  8168. begin
  8169. GetTypeName:='<erroneous type>';
  8170. end;
  8171. function terrordef.getmangledparaname:TSymStr;
  8172. begin
  8173. getmangledparaname:='error';
  8174. end;
  8175. {****************************************************************************
  8176. Definition Helpers
  8177. ****************************************************************************}
  8178. function is_interfacecom(def: tdef): boolean;
  8179. begin
  8180. is_interfacecom:=
  8181. assigned(def) and
  8182. (def.typ=objectdef) and
  8183. (tobjectdef(def).objecttype=odt_interfacecom);
  8184. end;
  8185. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  8186. begin
  8187. is_interfacecom_or_dispinterface:=
  8188. assigned(def) and
  8189. (def.typ=objectdef) and
  8190. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  8191. end;
  8192. function is_any_interface_kind(def: tdef): boolean;
  8193. begin
  8194. result:=
  8195. assigned(def) and
  8196. (def.typ=objectdef) and
  8197. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  8198. is_objccategory(def));
  8199. end;
  8200. function is_interfacecorba(def: tdef): boolean;
  8201. begin
  8202. is_interfacecorba:=
  8203. assigned(def) and
  8204. (def.typ=objectdef) and
  8205. (tobjectdef(def).objecttype=odt_interfacecorba);
  8206. end;
  8207. function is_interface(def: tdef): boolean;
  8208. begin
  8209. is_interface:=
  8210. assigned(def) and
  8211. (def.typ=objectdef) and
  8212. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  8213. end;
  8214. function is_dispinterface(def: tdef): boolean;
  8215. begin
  8216. result:=
  8217. assigned(def) and
  8218. (def.typ=objectdef) and
  8219. (tobjectdef(def).objecttype=odt_dispinterface);
  8220. end;
  8221. function is_class(def: tdef): boolean;
  8222. begin
  8223. is_class:=
  8224. assigned(def) and
  8225. (def.typ=objectdef) and
  8226. (tobjectdef(def).objecttype=odt_class);
  8227. end;
  8228. function is_object(def: tdef): boolean;
  8229. begin
  8230. is_object:=
  8231. assigned(def) and
  8232. (def.typ=objectdef) and
  8233. (tobjectdef(def).objecttype=odt_object);
  8234. end;
  8235. function is_cppclass(def: tdef): boolean;
  8236. begin
  8237. is_cppclass:=
  8238. assigned(def) and
  8239. (def.typ=objectdef) and
  8240. (tobjectdef(def).objecttype=odt_cppclass);
  8241. end;
  8242. function is_objcclass(def: tdef): boolean;
  8243. begin
  8244. is_objcclass:=
  8245. assigned(def) and
  8246. (def.typ=objectdef) and
  8247. (tobjectdef(def).objecttype=odt_objcclass);
  8248. end;
  8249. function is_objectpascal_helper(def: tdef): boolean;
  8250. begin
  8251. result:=
  8252. assigned(def) and
  8253. (def.typ=objectdef) and
  8254. (tobjectdef(def).objecttype=odt_helper);
  8255. end;
  8256. function is_objcclassref(def: tdef): boolean;
  8257. begin
  8258. is_objcclassref:=
  8259. assigned(def) and
  8260. (def.typ=classrefdef) and
  8261. is_objcclass(tclassrefdef(def).pointeddef);
  8262. end;
  8263. function is_objcprotocol(def: tdef): boolean;
  8264. begin
  8265. result:=
  8266. assigned(def) and
  8267. (def.typ=objectdef) and
  8268. (tobjectdef(def).objecttype=odt_objcprotocol);
  8269. end;
  8270. function is_objccategory(def: tdef): boolean;
  8271. begin
  8272. result:=
  8273. assigned(def) and
  8274. (def.typ=objectdef) and
  8275. { if used as a forward type }
  8276. ((tobjectdef(def).objecttype=odt_objccategory) or
  8277. { if used as after it has been resolved }
  8278. ((tobjectdef(def).objecttype=odt_objcclass) and
  8279. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  8280. end;
  8281. function is_objc_class_or_protocol(def: tdef): boolean;
  8282. begin
  8283. result:=
  8284. assigned(def) and
  8285. (def.typ=objectdef) and
  8286. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  8287. end;
  8288. function is_objc_protocol_or_category(def: tdef): boolean;
  8289. begin
  8290. result:=
  8291. assigned(def) and
  8292. (def.typ=objectdef) and
  8293. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  8294. ((tobjectdef(def).objecttype = odt_objcclass) and
  8295. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  8296. end;
  8297. function is_classhelper(def: tdef): boolean;
  8298. begin
  8299. result:=
  8300. is_objectpascal_helper(def) or
  8301. is_objccategory(def);
  8302. end;
  8303. function is_class_or_interface(def: tdef): boolean;
  8304. begin
  8305. result:=
  8306. assigned(def) and
  8307. (def.typ=objectdef) and
  8308. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  8309. end;
  8310. function is_class_or_interface_or_objc(def: tdef): boolean;
  8311. begin
  8312. result:=
  8313. assigned(def) and
  8314. (def.typ=objectdef) and
  8315. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  8316. end;
  8317. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  8318. begin
  8319. result:=
  8320. assigned(def) and
  8321. (def.typ=objectdef) and
  8322. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  8323. end;
  8324. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  8325. begin
  8326. result:=
  8327. assigned(def) and
  8328. (def.typ=objectdef) and
  8329. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  8330. end;
  8331. function is_class_or_interface_or_object(def: tdef): boolean;
  8332. begin
  8333. result:=
  8334. assigned(def) and
  8335. (def.typ=objectdef) and
  8336. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  8337. end;
  8338. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  8339. begin
  8340. result:=
  8341. assigned(def) and
  8342. (def.typ=objectdef) and
  8343. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  8344. end;
  8345. function is_implicit_pointer_object_type(def: tdef): boolean;
  8346. begin
  8347. result:=
  8348. assigned(def) and
  8349. (((def.typ=objectdef) and
  8350. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  8351. ((target_info.system in systems_jvm) and
  8352. (def.typ=recorddef)));
  8353. end;
  8354. function is_implicit_array_pointer(def: tdef): boolean;
  8355. begin
  8356. result:=is_dynamic_array(def) or is_dynamicstring(def);
  8357. end;
  8358. function is_class_or_object(def: tdef): boolean;
  8359. begin
  8360. result:=
  8361. assigned(def) and
  8362. (def.typ=objectdef) and
  8363. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  8364. end;
  8365. function is_record(def: tdef): boolean;
  8366. begin
  8367. result:=
  8368. assigned(def) and
  8369. (def.typ=recorddef);
  8370. end;
  8371. function is_javaclass(def: tdef): boolean;
  8372. begin
  8373. result:=
  8374. assigned(def) and
  8375. (def.typ=objectdef) and
  8376. (tobjectdef(def).objecttype=odt_javaclass);
  8377. end;
  8378. function is_javaclassref(def: tdef): boolean;
  8379. begin
  8380. is_javaclassref:=
  8381. assigned(def) and
  8382. (def.typ=classrefdef) and
  8383. is_javaclass(tclassrefdef(def).pointeddef);
  8384. end;
  8385. function is_javainterface(def: tdef): boolean;
  8386. begin
  8387. result:=
  8388. assigned(def) and
  8389. (def.typ=objectdef) and
  8390. (tobjectdef(def).objecttype=odt_interfacejava);
  8391. end;
  8392. function is_java_class_or_interface(def: tdef): boolean;
  8393. begin
  8394. result:=
  8395. assigned(def) and
  8396. (def.typ=objectdef) and
  8397. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  8398. end;
  8399. procedure loadobjctypes;
  8400. begin
  8401. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  8402. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  8403. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  8404. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  8405. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  8406. end;
  8407. procedure maybeloadcocoatypes;
  8408. var
  8409. tsym: ttypesym;
  8410. cocoaunit: string[15];
  8411. begin
  8412. if assigned(objc_fastenumeration) then
  8413. exit;
  8414. if not(target_info.system in [system_arm_ios,system_i386_iphonesim,system_aarch64_ios,system_x86_64_iphonesim,system_aarch64_iphonesim]) then
  8415. cocoaunit:='COCOAALL'
  8416. else
  8417. cocoaunit:='IPHONEALL';
  8418. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  8419. if assigned(tsym) then
  8420. objc_fastenumeration:=tobjectdef(tsym.typedef)
  8421. else
  8422. objc_fastenumeration:=nil;
  8423. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  8424. if assigned(tsym) then
  8425. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  8426. else
  8427. objc_fastenumerationstate:=nil;
  8428. end;
  8429. function use_vectorfpu(def : tdef) : boolean;
  8430. begin
  8431. {$ifdef x86}
  8432. {$define use_vectorfpuimplemented}
  8433. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  8434. (is_double(def) and (current_settings.fputype in sse_doublescalar)) or
  8435. { Check vector types }
  8436. (
  8437. is_normal_array(def) and
  8438. (ado_IsVector in tarraydef(def).arrayoptions) and
  8439. (
  8440. (
  8441. is_single(tarraydef(def).elementdef) and
  8442. (
  8443. { SSE or AVX XMM register }
  8444. ((tarraydef(def).elecount = 4) and (current_settings.fputype in sse_singlescalar)) or
  8445. { AVX YMM register }
  8446. ((tarraydef(def).elecount = 8) and (current_settings.fputype in fpu_avx_instructionsets))
  8447. {$ifndef i8086}
  8448. or
  8449. { AVX512 ZMM register }
  8450. ((tarraydef(def).elecount = 16) and (current_settings.fputype in [fpu_avx512f]))
  8451. {$endif not i8086}
  8452. )
  8453. ) or
  8454. (
  8455. is_double(tarraydef(def).elementdef) and
  8456. (
  8457. { SSE or AVX XMM register }
  8458. ((tarraydef(def).elecount = 2) and (current_settings.fputype in sse_doublescalar)) or
  8459. { AVX YMM register }
  8460. ((tarraydef(def).elecount = 4) and (current_settings.fputype in fpu_avx_instructionsets))
  8461. {$ifndef i8086}
  8462. { AVX512 ZMM register }
  8463. or ((tarraydef(def).elecount = 8) and (current_settings.fputype in [fpu_avx512f]))
  8464. {$endif not i8086}
  8465. )
  8466. )
  8467. )
  8468. );
  8469. {$endif x86}
  8470. {$ifdef arm}
  8471. {$define use_vectorfpuimplemented}
  8472. use_vectorfpu:=FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype];
  8473. {$endif arm}
  8474. {$ifdef aarch64}
  8475. {$define use_vectorfpuimplemented}
  8476. use_vectorfpu:=true;
  8477. {$endif aarch64}
  8478. {$ifndef use_vectorfpuimplemented}
  8479. use_vectorfpu:=false;
  8480. {$endif}
  8481. end;
  8482. procedure reset_all_default_types;
  8483. { Those class pointers have to be set to nil manually }
  8484. { after memory they point to have been released. }
  8485. { Necessary strictly for Textmode IDE. }
  8486. begin
  8487. voidpointertype:=nil; { pointer for Void-pointeddef }
  8488. charpointertype:=nil; { pointer for Char-pointeddef }
  8489. widecharpointertype:=nil; { pointer for WideChar-pointeddef }
  8490. voidcodepointertype:=nil; { pointer to code; corresponds to System.CodePointer }
  8491. voidstackpointertype:=nil; { the pointer type used for accessing parameters and local vars on the stack }
  8492. parentfpvoidpointertype:=nil; { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  8493. {$ifdef x86}
  8494. voidnearpointertype:=nil;
  8495. voidnearcspointertype:=nil;
  8496. voidneardspointertype:=nil;
  8497. voidnearsspointertype:=nil;
  8498. voidnearespointertype:=nil;
  8499. voidnearfspointertype:=nil;
  8500. voidneargspointertype:=nil;
  8501. {$ifdef i8086}
  8502. voidfarpointertype:=nil;
  8503. voidhugepointertype:=nil;
  8504. charnearpointertype:=nil;
  8505. charfarpointertype:=nil;
  8506. charhugepointertype:=nil;
  8507. bytefarpointertype:=nil; { used for Mem[] }
  8508. wordfarpointertype:=nil; { used for MemW[] }
  8509. longintfarpointertype:=nil; { used for MemL[] }
  8510. {$endif i8086}
  8511. {$endif x86}
  8512. {$ifdef wasm}
  8513. wasmvoidexternreftype:=nil;
  8514. {$endif wasm}
  8515. cundefinedtype:=nil;
  8516. cformaltype:=nil; { unique formal definition }
  8517. ctypedformaltype:=nil; { unique typed formal definition }
  8518. voidtype:=nil; { Void (procedure) }
  8519. cansichartype:=nil; { Char }
  8520. cwidechartype:=nil; { WideChar }
  8521. cchartype:=nil; { either cansichartype or cwidechartype. Do not free }
  8522. pasbool1type:=nil; { boolean type }
  8523. pasbool8type:=nil;
  8524. pasbool16type:=nil;
  8525. pasbool32type:=nil;
  8526. pasbool64type:=nil;
  8527. bool8type:=nil;
  8528. bool16type:=nil;
  8529. bool32type:=nil;
  8530. bool64type:=nil;
  8531. {$ifdef llvm}
  8532. llvmbool1type:=nil; { LLVM i1 type }
  8533. {$endif llvm}
  8534. u8inttype:=nil; { 8-Bit unsigned integer }
  8535. s8inttype:=nil; { 8-Bit signed integer }
  8536. u16inttype:=nil; { 16-Bit unsigned integer }
  8537. s16inttype:=nil; { 16-Bit signed integer }
  8538. u24inttype:=nil; { 24-Bit unsigned integer }
  8539. s24inttype:=nil; { 24-Bit signed integer }
  8540. u32inttype:=nil; { 32-Bit unsigned integer }
  8541. s32inttype:=nil; { 32-Bit signed integer }
  8542. u40inttype:=nil; { 40-Bit unsigned integer }
  8543. s40inttype:=nil; { 40-Bit signed integer }
  8544. u48inttype:=nil; { 48-Bit unsigned integer }
  8545. s48inttype:=nil; { 48-Bit signed integer }
  8546. u56inttype:=nil; { 56-Bit unsigned integer }
  8547. s56inttype:=nil; { 56-Bit signed integer }
  8548. u64inttype:=nil; { 64-bit unsigned integer }
  8549. s64inttype:=nil; { 64-bit signed integer }
  8550. u128inttype:=nil; { 128-bit unsigned integer }
  8551. s128inttype:=nil; { 128-bit signed integer }
  8552. s32floattype:=nil; { 32 bit floating point number }
  8553. s64floattype:=nil; { 64 bit floating point number }
  8554. s80floattype:=nil; { 80 bit floating point number }
  8555. sc80floattype:=nil; { 80 bit floating point number but stored like in C }
  8556. s64currencytype:=nil; { pointer to a currency type }
  8557. cshortstringtype:=nil; { pointer to type of short string const }
  8558. clongstringtype:=nil; { pointer to type of long string const }
  8559. cansistringtype:=nil; { pointer to type of ansi string const }
  8560. cwidestringtype:=nil; { pointer to type of wide string const }
  8561. cunicodestringtype:=nil;
  8562. openshortstringtype:=nil;
  8563. openchararraytype:=nil;
  8564. cfiletype:=nil;
  8565. methodpointertype:=nil; { typecasting of methodpointers to extract self }
  8566. nestedprocpointertype:=nil; { typecasting of nestedprocpointers to extract parentfp }
  8567. hresultdef:=nil;
  8568. typekindtype:=nil; { def of TTypeKind for correct handling of GetTypeKind parameters }
  8569. { we use only one variant def for every variant class }
  8570. cvarianttype:=nil;
  8571. colevarianttype:=nil;
  8572. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  8573. sinttype:=nil;
  8574. uinttype:=nil;
  8575. { integer types corresponding to OS_SINT/OS_INT }
  8576. ossinttype:=nil;
  8577. osuinttype:=nil;
  8578. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  8579. alusinttype:=nil;
  8580. aluuinttype:=nil;
  8581. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  8582. sizeuinttype:=nil;
  8583. sizesinttype:=nil;
  8584. { unsigned and signed ord type with the same size as a pointer }
  8585. ptruinttype:=nil;
  8586. ptrsinttype:=nil;
  8587. { unsigned and signed ord type with the same size as a codepointer }
  8588. codeptruinttype:=nil;
  8589. codeptrsinttype:=nil;
  8590. { several types to simulate more or less C++ objects for GDB }
  8591. vmttype:=nil;
  8592. vmtarraytype:=nil;
  8593. { type of classrefs, used for stabs }
  8594. pvmttype:=nil;
  8595. { return type of the setjmp function }
  8596. exceptionreasontype:=nil;
  8597. class_tobject:=nil;
  8598. class_tcustomattribute:=nil;
  8599. interface_iunknown:=nil;
  8600. interface_idispatch:=nil;
  8601. rec_tguid:=nil;
  8602. rec_jmp_buf:=nil;
  8603. rec_exceptaddr:=nil;
  8604. objc_metaclasstype:=nil;
  8605. objc_superclasstype:=nil;
  8606. objc_idtype:=nil;
  8607. objc_seltype:=nil;
  8608. objc_objecttype:=nil;
  8609. objc_protocoltype:=nil;
  8610. objc_fastenumeration:=nil;
  8611. objc_fastenumerationstate:=nil;
  8612. {$ifdef llvm}
  8613. { llvm types }
  8614. { a unique def to identify any kind of metadata }
  8615. llvm_metadatatype:=nil;
  8616. {$endif llvm}
  8617. { Java base types }
  8618. java_jlobject:=nil;
  8619. java_jlthrowable:=nil;
  8620. java_fpcbaserecordtype:=nil;
  8621. java_jlstring:=nil;
  8622. java_jlenum:=nil;
  8623. java_juenumset:=nil;
  8624. java_jubitset:=nil;
  8625. java_ansistring:=nil;
  8626. java_shortstring:=nil;
  8627. java_procvarbase:=nil;
  8628. { x86 vector types }
  8629. x86_m64type:=nil;
  8630. x86_m128type:=nil;
  8631. x86_m128dtype:=nil;
  8632. x86_m128itype:=nil;
  8633. x86_m256type:=nil;
  8634. x86_m256dtype:=nil;
  8635. x86_m256itype:=nil;
  8636. end;
  8637. end.