symdef.pas 304 KB

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