symdef.pas 316 KB

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