symdef.pas 307 KB

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