symdef.pas 290 KB

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