symdef.pas 271 KB

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