symdef.pas 268 KB

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