symdef.pas 304 KB

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