symdef.pas 286 KB

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