symdef.pas 320 KB

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