symdef.pas 322 KB

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