symdef.pas 265 KB

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