symdef.pas 291 KB

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