symdef.pas 317 KB

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