symdef.pas 316 KB

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