symdef.pas 304 KB

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