symdef.pas 265 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978
  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, normally s32inttype on 32 bit systems and 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. lastowner: tsymtable;
  1736. tmp: tdef;
  1737. begin
  1738. {$ifdef symansistr}
  1739. if _fullownerhierarchyname<>'' then
  1740. exit(_fullownerhierarchyname);
  1741. {$else symansistr}
  1742. if assigned(_fullownerhierarchyname) then
  1743. exit(_fullownerhierarchyname^);
  1744. {$endif symansistr}
  1745. { the def can only reside inside structured types or
  1746. procedures/functions/methods }
  1747. tmp:=self;
  1748. result:='';
  1749. repeat
  1750. lastowner:=tmp.owner;
  1751. { can be not assigned in case of a forwarddef }
  1752. if not assigned(lastowner) then
  1753. break
  1754. else
  1755. tmp:=tdef(lastowner.defowner);
  1756. if not assigned(tmp) then
  1757. break;
  1758. if tmp.typ in [recorddef,objectdef] then
  1759. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  1760. else
  1761. if tmp.typ=procdef then
  1762. result:=tprocdef(tmp).customprocname([pno_paranames,pno_proctypeoption])+'.'+result;
  1763. until tmp=nil;
  1764. { add the unit name }
  1765. if assigned(lastowner) and
  1766. assigned(lastowner.realname) then
  1767. result:=lastowner.realname^+'.'+result;
  1768. {$ifdef symansistr}
  1769. _fullownerhierarchyname:=result;
  1770. {$else symansistr}
  1771. _fullownerhierarchyname:=stringdup(result);
  1772. {$endif symansistr}
  1773. end;
  1774. function tstoreddef.in_currentunit: boolean;
  1775. var
  1776. st: tsymtable;
  1777. begin
  1778. st:=owner;
  1779. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1780. st:=st.defowner.owner;
  1781. result:=st.iscurrentunit;
  1782. end;
  1783. function tstoreddef.getcopy : tstoreddef;
  1784. begin
  1785. Message(sym_e_cant_create_unique_type);
  1786. getcopy:=cerrordef.create;
  1787. end;
  1788. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1789. var
  1790. sizeleft,i : longint;
  1791. buf : array[0..255] of byte;
  1792. oldintfcrc : boolean;
  1793. begin
  1794. if defid<0 then
  1795. internalerror(2015101401);
  1796. ppufile.putlongint(DefId);
  1797. ppufile.putderef(typesymderef);
  1798. ppufile.putsmallset(defoptions);
  1799. oldintfcrc:=ppufile.do_crc;
  1800. ppufile.do_crc:=false;
  1801. ppufile.putsmallset(defstates);
  1802. if df_genconstraint in defoptions then
  1803. genconstraintdata.ppuwrite(ppufile);
  1804. if [df_generic,df_specialization]*defoptions<>[] then
  1805. begin
  1806. if not assigned(genericparas) then
  1807. ppufile.putlongint(0)
  1808. else
  1809. begin
  1810. if not assigned(genericparaderefs) then
  1811. internalerror(2014052305);
  1812. ppufile.putlongint(genericparas.count);
  1813. for i:=0 to genericparas.count-1 do
  1814. begin
  1815. ppufile.putstring(genericparas.nameofindex(i));
  1816. ppufile.putderef(pderef(genericparaderefs[i])^);
  1817. end;
  1818. end;
  1819. end;
  1820. if df_generic in defoptions then
  1821. begin
  1822. if assigned(generictokenbuf) then
  1823. begin
  1824. sizeleft:=generictokenbuf.size;
  1825. generictokenbuf.seek(0);
  1826. end
  1827. else
  1828. sizeleft:=0;
  1829. ppufile.putlongint(sizeleft);
  1830. while sizeleft>0 do
  1831. begin
  1832. if sizeleft>sizeof(buf) then
  1833. i:=sizeof(buf)
  1834. else
  1835. i:=sizeleft;
  1836. generictokenbuf.read(buf,i);
  1837. ppufile.putdata(buf,i);
  1838. dec(sizeleft,i);
  1839. end;
  1840. end;
  1841. ppufile.do_crc:=oldintfcrc;
  1842. if df_specialization in defoptions then
  1843. ppufile.putderef(genericdefderef);
  1844. end;
  1845. procedure tstoreddef.buildderef;
  1846. var
  1847. i : longint;
  1848. sym : tsym;
  1849. symderef : pderef;
  1850. begin
  1851. if not registered then
  1852. register_def;
  1853. typesymderef.build(typesym);
  1854. genericdefderef.build(genericdef);
  1855. if assigned(genconstraintdata) then
  1856. genconstraintdata.buildderef;
  1857. if assigned(genericparas) then
  1858. begin
  1859. if not assigned(genericparaderefs) then
  1860. genericparaderefs:=tfplist.create;
  1861. for i:=0 to genericparas.count-1 do
  1862. begin
  1863. sym:=tsym(genericparas.items[i]);
  1864. new(symderef);
  1865. symderef^.build(sym);
  1866. genericparaderefs.add(symderef);
  1867. end;
  1868. end;
  1869. end;
  1870. procedure tstoreddef.buildderefimpl;
  1871. begin
  1872. end;
  1873. procedure tstoreddef.deref;
  1874. var
  1875. symderef : pderef;
  1876. i : longint;
  1877. begin
  1878. typesym:=ttypesym(typesymderef.resolve);
  1879. if df_specialization in defoptions then
  1880. genericdef:=tstoreddef(genericdefderef.resolve);
  1881. if assigned(genconstraintdata) then
  1882. genconstraintdata.deref;
  1883. if assigned(genericparas) then
  1884. begin
  1885. if not assigned(genericparaderefs) then
  1886. internalerror(2014052302);
  1887. if genericparas.count<>genericparaderefs.count then
  1888. internalerror(2014052303);
  1889. for i:=0 to genericparaderefs.count-1 do
  1890. begin
  1891. symderef:=pderef(genericparaderefs[i]);
  1892. genericparas.items[i]:=symderef^.resolve;
  1893. dispose(symderef);
  1894. end;
  1895. genericparaderefs.free;
  1896. genericparaderefs:=nil;
  1897. end;
  1898. end;
  1899. procedure tstoreddef.derefimpl;
  1900. begin
  1901. end;
  1902. function tstoreddef.size : asizeint;
  1903. begin
  1904. size:=savesize;
  1905. end;
  1906. function tstoreddef.getvardef:longint;
  1907. begin
  1908. result:=varUndefined;
  1909. end;
  1910. function tstoreddef.alignment : shortint;
  1911. begin
  1912. { natural alignment by default }
  1913. alignment:=size_2_align(savesize);
  1914. { can happen if savesize = 0, e.g. for voiddef or
  1915. an empty record
  1916. }
  1917. if (alignment=0) then
  1918. alignment:=1;
  1919. end;
  1920. { returns true, if the definition can be published }
  1921. function tstoreddef.is_publishable : boolean;
  1922. begin
  1923. is_publishable:=false;
  1924. end;
  1925. { needs an init table }
  1926. function tstoreddef.needs_inittable : boolean;
  1927. begin
  1928. needs_inittable:=false;
  1929. end;
  1930. function tstoreddef.is_intregable : boolean;
  1931. var
  1932. recsize,temp: longint;
  1933. begin
  1934. is_intregable:=false;
  1935. case typ of
  1936. orddef,
  1937. pointerdef,
  1938. enumdef,
  1939. classrefdef:
  1940. is_intregable:=true;
  1941. procvardef :
  1942. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  1943. objectdef:
  1944. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1945. setdef:
  1946. is_intregable:=is_smallset(self);
  1947. recorddef:
  1948. begin
  1949. {$ifdef llvm}
  1950. is_intregable:=false;
  1951. {$else llvm}
  1952. recsize:=size;
  1953. is_intregable:=
  1954. ispowerof2(recsize,temp) and
  1955. (((recsize <= sizeof(aint)*2) and
  1956. { records cannot go into registers on 16 bit targets for now }
  1957. (sizeof(aint)>2) and
  1958. not trecorddef(self).contains_float_field) or
  1959. (recsize <= sizeof(aint))) and
  1960. not needs_inittable;
  1961. {$endif llvm}
  1962. end;
  1963. end;
  1964. end;
  1965. function tstoreddef.is_fpuregable : boolean;
  1966. begin
  1967. {$ifdef x86}
  1968. result:=use_vectorfpu(self);
  1969. {$else x86}
  1970. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1971. {$endif x86}
  1972. end;
  1973. function tstoreddef.is_const_intregable : boolean;
  1974. begin
  1975. case typ of
  1976. stringdef:
  1977. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  1978. arraydef:
  1979. result:=is_dynamic_array(self);
  1980. objectdef:
  1981. result:=is_interface(self);
  1982. else
  1983. result:=false;
  1984. end;
  1985. end;
  1986. procedure tstoreddef.initgeneric;
  1987. begin
  1988. if assigned(generictokenbuf) then
  1989. internalerror(200512131);
  1990. generictokenbuf:=tdynamicarray.create(256);
  1991. end;
  1992. function tstoreddef.is_generic: boolean;
  1993. var
  1994. sym: tsym;
  1995. i: longint;
  1996. begin
  1997. result:=assigned(genericparas) and
  1998. (genericparas.count>0) and
  1999. (df_generic in defoptions);
  2000. if result then
  2001. { if any of the type parameters does *not* belong to as (meaning it was passed
  2002. in from outside) then we aren't a generic, but a specialization }
  2003. for i:=0 to genericparas.count-1 do
  2004. begin
  2005. sym:=tsym(genericparas[i]);
  2006. if sym.typ<>symconst.typesym then
  2007. internalerror(2014050903);
  2008. if sym.owner.defowner<>self then
  2009. exit(false);
  2010. end;
  2011. end;
  2012. function tstoreddef.is_specialization: boolean;
  2013. var
  2014. i : longint;
  2015. sym : tsym;
  2016. begin
  2017. result:=assigned(genericparas) and
  2018. (genericparas.count>0) and
  2019. (df_specialization in defoptions);
  2020. if result then
  2021. begin
  2022. { if at least one of the generic parameters is not owned by us (meaning it was
  2023. passed in from outside) then we have a specialization, otherwise we have a generic }
  2024. for i:=0 to genericparas.count-1 do
  2025. begin
  2026. sym:=tsym(genericparas[i]);
  2027. if sym.typ<>symconst.typesym then
  2028. internalerror(2014050904);
  2029. if sym.owner.defowner<>self then
  2030. exit(true);
  2031. end;
  2032. result:=false;
  2033. end;
  2034. end;
  2035. procedure tstoreddef.register_def;
  2036. begin
  2037. if registered then
  2038. exit;
  2039. { Register in current_module }
  2040. if assigned(current_module) then
  2041. begin
  2042. exclude(defoptions,df_not_registered_no_free);
  2043. if defid<defid_not_registered then
  2044. defid:=deflist_index
  2045. else
  2046. begin
  2047. current_module.deflist.Add(self);
  2048. defid:=current_module.deflist.Count-1;
  2049. end;
  2050. maybe_put_in_symtable_stack;
  2051. end
  2052. else
  2053. DefId:=defid_registered_nost;
  2054. end;
  2055. procedure tstoreddef.maybe_put_in_symtable_stack;
  2056. var
  2057. insertstack: psymtablestackitem;
  2058. begin
  2059. if assigned(symtablestack) and
  2060. not assigned(self.owner) then
  2061. begin
  2062. insertstack:=symtablestack.stack;
  2063. { don't insert defs in exception symtables, as they are freed before
  2064. the module is compiled, so we can get crashes on high level targets
  2065. if they still need it while e.g. writing assembler code }
  2066. while assigned(insertstack) and
  2067. (insertstack^.symtable.symtabletype in [stt_exceptsymtable,withsymtable]) do
  2068. insertstack:=insertstack^.next;
  2069. if not assigned(insertstack) then
  2070. internalerror(200602044);
  2071. if insertstack^.symtable.sealed then
  2072. internalerror(2015022301);
  2073. insertstack^.symtable.insertdef(self);
  2074. end;
  2075. end;
  2076. {****************************************************************************
  2077. Tstringdef
  2078. ****************************************************************************}
  2079. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2080. begin
  2081. inherited create(stringdef,doregister);
  2082. stringtype:=st_shortstring;
  2083. encoding:=0;
  2084. len:=l;
  2085. end;
  2086. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2087. begin
  2088. inherited ppuload(stringdef,ppufile);
  2089. stringtype:=st_shortstring;
  2090. encoding:=0;
  2091. len:=ppufile.getbyte;
  2092. ppuload_platform(ppufile);
  2093. end;
  2094. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2095. begin
  2096. inherited create(stringdef,doregister);
  2097. stringtype:=st_longstring;
  2098. encoding:=0;
  2099. len:=l;
  2100. end;
  2101. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2102. begin
  2103. inherited ppuload(stringdef,ppufile);
  2104. stringtype:=st_longstring;
  2105. encoding:=0;
  2106. len:=ppufile.getasizeint;
  2107. ppuload_platform(ppufile);
  2108. end;
  2109. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2110. begin
  2111. inherited create(stringdef,doregister);
  2112. stringtype:=st_ansistring;
  2113. encoding:=aencoding;
  2114. len:=-1;
  2115. end;
  2116. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2117. begin
  2118. inherited ppuload(stringdef,ppufile);
  2119. stringtype:=st_ansistring;
  2120. len:=ppufile.getaint;
  2121. encoding:=ppufile.getword;
  2122. ppuload_platform(ppufile);
  2123. end;
  2124. constructor tstringdef.createwide(doregister: boolean);
  2125. begin
  2126. inherited create(stringdef,doregister);
  2127. stringtype:=st_widestring;
  2128. if target_info.endian=endian_little then
  2129. encoding:=CP_UTF16LE
  2130. else
  2131. encoding:=CP_UTF16BE;
  2132. len:=-1;
  2133. end;
  2134. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2135. begin
  2136. inherited ppuload(stringdef,ppufile);
  2137. stringtype:=st_widestring;
  2138. if target_info.endian=endian_little then
  2139. encoding:=CP_UTF16LE
  2140. else
  2141. encoding:=CP_UTF16BE;
  2142. len:=ppufile.getaint;
  2143. ppuload_platform(ppufile);
  2144. end;
  2145. constructor tstringdef.createunicode(doregister: boolean);
  2146. begin
  2147. inherited create(stringdef,doregister);
  2148. stringtype:=st_unicodestring;
  2149. if target_info.endian=endian_little then
  2150. encoding:=CP_UTF16LE
  2151. else
  2152. encoding:=CP_UTF16BE;
  2153. len:=-1;
  2154. end;
  2155. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2156. begin
  2157. inherited ppuload(stringdef,ppufile);
  2158. stringtype:=st_unicodestring;
  2159. len:=ppufile.getaint;
  2160. encoding:=ppufile.getword;
  2161. ppuload_platform(ppufile);
  2162. end;
  2163. function tstringdef.getcopy : tstoreddef;
  2164. begin
  2165. result:=cstringdef.create(typ,true);
  2166. result.typ:=stringdef;
  2167. tstringdef(result).stringtype:=stringtype;
  2168. tstringdef(result).encoding:=encoding;
  2169. tstringdef(result).len:=len;
  2170. end;
  2171. function tstringdef.stringtypname:string;
  2172. const
  2173. typname:array[tstringtype] of string[10]=(
  2174. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2175. );
  2176. begin
  2177. stringtypname:=typname[stringtype];
  2178. end;
  2179. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2180. begin
  2181. inherited ppuwrite(ppufile);
  2182. if stringtype=st_shortstring then
  2183. begin
  2184. {$ifdef extdebug}
  2185. if len > 255 then internalerror(12122002);
  2186. {$endif}
  2187. ppufile.putbyte(byte(len))
  2188. end
  2189. else
  2190. ppufile.putaint(len);
  2191. if stringtype in [st_ansistring,st_unicodestring] then
  2192. ppufile.putword(encoding);
  2193. case stringtype of
  2194. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2195. st_longstring : writeentry(ppufile,iblongstringdef);
  2196. st_ansistring : writeentry(ppufile,ibansistringdef);
  2197. st_widestring : writeentry(ppufile,ibwidestringdef);
  2198. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2199. end;
  2200. end;
  2201. function tstringdef.needs_inittable : boolean;
  2202. begin
  2203. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2204. end;
  2205. function tstringdef.GetTypeName : string;
  2206. const
  2207. names : array[tstringtype] of string[15] = (
  2208. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2209. begin
  2210. GetTypeName:=names[stringtype];
  2211. end;
  2212. function tstringdef.getvardef : longint;
  2213. const
  2214. vardef : array[tstringtype] of longint = (
  2215. varUndefined,varUndefined,varString,varOleStr,varUString);
  2216. begin
  2217. result:=vardef[stringtype];
  2218. end;
  2219. function tstringdef.alignment : shortint;
  2220. begin
  2221. case stringtype of
  2222. st_unicodestring,
  2223. st_widestring,
  2224. st_ansistring:
  2225. alignment:=voidpointertype.alignment;
  2226. st_longstring,
  2227. st_shortstring:
  2228. { char to string accesses byte 0 and 1 with one word access }
  2229. if (tf_requires_proper_alignment in target_info.flags) or
  2230. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2231. (m_mac in current_settings.modeswitches) then
  2232. alignment:=size_2_align(2)
  2233. else
  2234. alignment:=size_2_align(1);
  2235. else
  2236. internalerror(200412301);
  2237. end;
  2238. end;
  2239. function tstringdef.getmangledparaname : TSymStr;
  2240. begin
  2241. getmangledparaname:='STRING';
  2242. end;
  2243. function tstringdef.is_publishable : boolean;
  2244. begin
  2245. is_publishable:=true;
  2246. end;
  2247. function tstringdef.size: asizeint;
  2248. begin
  2249. case stringtype of
  2250. st_shortstring:
  2251. Result:=len+1;
  2252. st_longstring,
  2253. st_ansistring,
  2254. st_widestring,
  2255. st_unicodestring:
  2256. Result:=voidpointertype.size;
  2257. else
  2258. internalerror(2014032301);
  2259. end;
  2260. end;
  2261. {****************************************************************************
  2262. TENUMDEF
  2263. ****************************************************************************}
  2264. constructor tenumdef.create;
  2265. begin
  2266. inherited create(enumdef,true);
  2267. minval:=0;
  2268. maxval:=0;
  2269. calcsavesize;
  2270. has_jumps:=false;
  2271. basedef:=nil;
  2272. symtable:=tenumsymtable.create(self);
  2273. end;
  2274. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2275. begin
  2276. inherited create(enumdef,true);
  2277. minval:=_min;
  2278. maxval:=_max;
  2279. basedef:=_basedef;
  2280. calcsavesize;
  2281. has_jumps:=false;
  2282. symtable:=basedef.symtable.getcopy;
  2283. include(defoptions, df_copied_def);
  2284. end;
  2285. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2286. begin
  2287. inherited ppuload(enumdef,ppufile);
  2288. minval:=ppufile.getaint;
  2289. maxval:=ppufile.getaint;
  2290. savesize:=ppufile.getaint;
  2291. has_jumps:=false;
  2292. if df_copied_def in defoptions then
  2293. begin
  2294. symtable:=nil;
  2295. ppufile.getderef(basedefderef);
  2296. ppuload_platform(ppufile);
  2297. end
  2298. else
  2299. begin
  2300. ppuload_platform(ppufile);
  2301. // create with nil defowner first to prevent values changes on insert
  2302. symtable:=tenumsymtable.create(nil);
  2303. tenumsymtable(symtable).ppuload(ppufile);
  2304. symtable.defowner:=self;
  2305. end;
  2306. end;
  2307. destructor tenumdef.destroy;
  2308. begin
  2309. symtable.free;
  2310. symtable:=nil;
  2311. inherited destroy;
  2312. end;
  2313. function tenumdef.getcopy : tstoreddef;
  2314. begin
  2315. if assigned(basedef) then
  2316. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2317. else
  2318. begin
  2319. result:=cenumdef.create;
  2320. tenumdef(result).minval:=minval;
  2321. tenumdef(result).maxval:=maxval;
  2322. tenumdef(result).symtable.free;
  2323. tenumdef(result).symtable:=symtable.getcopy;
  2324. tenumdef(result).basedef:=self;
  2325. end;
  2326. tenumdef(result).has_jumps:=has_jumps;
  2327. tenumdef(result).basedefderef:=basedefderef;
  2328. include(tenumdef(result).defoptions,df_copied_def);
  2329. end;
  2330. procedure tenumdef.calcsavesize;
  2331. begin
  2332. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2333. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  2334. savesize:=8
  2335. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2336. else
  2337. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2338. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2339. savesize:=4
  2340. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2341. else
  2342. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2343. savesize:=2
  2344. else
  2345. savesize:=1;
  2346. end;
  2347. function tenumdef.packedbitsize: asizeint;
  2348. var
  2349. sizeval: tconstexprint;
  2350. power: longint;
  2351. begin
  2352. result := 0;
  2353. if (minval >= 0) and
  2354. (maxval <= 1) then
  2355. result := 1
  2356. else
  2357. begin
  2358. if (minval>=0) then
  2359. sizeval:=maxval
  2360. else
  2361. { don't count 0 twice }
  2362. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  2363. { 256 must become 512 etc. }
  2364. nextpowerof2(sizeval+1,power);
  2365. result := power;
  2366. end;
  2367. end;
  2368. procedure tenumdef.setmax(_max:asizeint);
  2369. begin
  2370. maxval:=_max;
  2371. calcsavesize;
  2372. end;
  2373. procedure tenumdef.setmin(_min:asizeint);
  2374. begin
  2375. minval:=_min;
  2376. calcsavesize;
  2377. end;
  2378. function tenumdef.min:asizeint;
  2379. begin
  2380. min:=minval;
  2381. end;
  2382. function tenumdef.max:asizeint;
  2383. begin
  2384. max:=maxval;
  2385. end;
  2386. function tenumdef.getfirstsym: tsym;
  2387. var
  2388. i:integer;
  2389. begin
  2390. for i := 0 to symtable.SymList.Count - 1 do
  2391. begin
  2392. result:=tsym(symtable.SymList[i]);
  2393. if tenumsym(result).value=minval then
  2394. exit;
  2395. end;
  2396. result:=nil;
  2397. end;
  2398. function tenumdef.int2enumsym(l: asizeint): tsym;
  2399. var
  2400. i: longint;
  2401. sym: tsym;
  2402. bdef: tenumdef;
  2403. begin
  2404. result:=nil;
  2405. if (l<minval) or
  2406. (l>maxval) then
  2407. exit;
  2408. bdef:=getbasedef;
  2409. for i:=0 to bdef.symtable.symlist.count-1 do
  2410. begin
  2411. sym:=tsym(bdef.symtable.symlist[i]);
  2412. if (sym.typ=enumsym) and
  2413. (tenumsym(sym).value=l) then
  2414. begin
  2415. result:=sym;
  2416. exit;
  2417. end;
  2418. end;
  2419. end;
  2420. function tenumdef.getbasedef: tenumdef;
  2421. begin
  2422. if not assigned(basedef) then
  2423. result:=self
  2424. else
  2425. result:=basedef;
  2426. end;
  2427. procedure tenumdef.buildderef;
  2428. begin
  2429. inherited buildderef;
  2430. if df_copied_def in defoptions then
  2431. basedefderef.build(basedef)
  2432. else
  2433. tenumsymtable(symtable).buildderef;
  2434. end;
  2435. procedure tenumdef.deref;
  2436. begin
  2437. inherited deref;
  2438. if df_copied_def in defoptions then
  2439. begin
  2440. basedef:=tenumdef(basedefderef.resolve);
  2441. symtable:=basedef.symtable.getcopy;
  2442. end
  2443. else
  2444. tenumsymtable(symtable).deref(false);
  2445. end;
  2446. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2447. begin
  2448. inherited ppuwrite(ppufile);
  2449. ppufile.putaint(min);
  2450. ppufile.putaint(max);
  2451. ppufile.putaint(savesize);
  2452. if df_copied_def in defoptions then
  2453. ppufile.putderef(basedefderef);
  2454. writeentry(ppufile,ibenumdef);
  2455. if not (df_copied_def in defoptions) then
  2456. tenumsymtable(symtable).ppuwrite(ppufile);
  2457. end;
  2458. function tenumdef.is_publishable : boolean;
  2459. begin
  2460. is_publishable:=true;
  2461. end;
  2462. function tenumdef.GetTypeName : string;
  2463. begin
  2464. GetTypeName:='<enumeration type>';
  2465. end;
  2466. {****************************************************************************
  2467. TORDDEF
  2468. ****************************************************************************}
  2469. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  2470. begin
  2471. inherited create(orddef,doregister);
  2472. low:=v;
  2473. high:=b;
  2474. ordtype:=t;
  2475. setsize;
  2476. end;
  2477. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2478. begin
  2479. inherited ppuload(orddef,ppufile);
  2480. ordtype:=tordtype(ppufile.getbyte);
  2481. low:=ppufile.getexprint;
  2482. high:=ppufile.getexprint;
  2483. setsize;
  2484. ppuload_platform(ppufile);
  2485. end;
  2486. function torddef.getcopy : tstoreddef;
  2487. begin
  2488. result:=corddef.create(ordtype,low,high,true);
  2489. result.typ:=orddef;
  2490. torddef(result).low:=low;
  2491. torddef(result).high:=high;
  2492. torddef(result).ordtype:=ordtype;
  2493. torddef(result).savesize:=savesize;
  2494. end;
  2495. function torddef.alignment:shortint;
  2496. begin
  2497. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  2498. (ordtype in [s64bit,u64bit]) then
  2499. result := 4
  2500. else
  2501. result := inherited alignment;
  2502. end;
  2503. procedure torddef.setsize;
  2504. const
  2505. sizetbl : array[tordtype] of longint = (
  2506. 0,
  2507. 1,2,4,8,16,
  2508. 1,2,4,8,16,
  2509. 1,2,4,8,
  2510. 1,2,4,8,
  2511. 1,2,8
  2512. );
  2513. begin
  2514. savesize:=sizetbl[ordtype];
  2515. end;
  2516. function torddef.packedbitsize: asizeint;
  2517. var
  2518. sizeval: tconstexprint;
  2519. power: longint;
  2520. begin
  2521. result := 0;
  2522. if ordtype = uvoid then
  2523. exit;
  2524. {$ifndef cpu64bitalu}
  2525. if (ordtype in [s64bit,u64bit]) then
  2526. {$else not cpu64bitalu}
  2527. if (ordtype = u64bit) or
  2528. ((ordtype = s64bit) and
  2529. ((low <= (system.low(int64) div 2)) or
  2530. (high > (system.high(int64) div 2)))) then
  2531. {$endif cpu64bitalu}
  2532. result := 64
  2533. else if (low >= 0) and
  2534. (high <= 1) then
  2535. result := 1
  2536. else
  2537. begin
  2538. if (low>=0) then
  2539. sizeval:=high
  2540. else
  2541. { don't count 0 twice }
  2542. sizeval:=(cutils.max(-low,high)*2)-1;
  2543. { 256 must become 512 etc. }
  2544. nextpowerof2(sizeval+1,power);
  2545. result := power;
  2546. end;
  2547. end;
  2548. function torddef.getvardef : longint;
  2549. const
  2550. basetype2vardef : array[tordtype] of longint = (
  2551. varUndefined,
  2552. varbyte,varword,varlongword,varqword,varUndefined,
  2553. varshortint,varsmallint,varinteger,varint64,varUndefined,
  2554. varboolean,varboolean,varboolean,varboolean,
  2555. varboolean,varboolean,varUndefined,varUndefined,
  2556. varUndefined,varUndefined,varCurrency);
  2557. begin
  2558. result:=basetype2vardef[ordtype];
  2559. end;
  2560. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2561. begin
  2562. inherited ppuwrite(ppufile);
  2563. ppufile.putbyte(byte(ordtype));
  2564. ppufile.putexprint(low);
  2565. ppufile.putexprint(high);
  2566. writeentry(ppufile,iborddef);
  2567. end;
  2568. function torddef.is_publishable : boolean;
  2569. begin
  2570. is_publishable:=(ordtype<>uvoid);
  2571. end;
  2572. function torddef.GetTypeName : string;
  2573. const
  2574. names : array[tordtype] of string[20] = (
  2575. 'untyped',
  2576. 'Byte','Word','DWord','QWord','UInt128',
  2577. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  2578. 'Boolean','Boolean16','Boolean32','Boolean64',
  2579. 'ByteBool','WordBool','LongBool','QWordBool',
  2580. 'Char','WideChar','Currency');
  2581. begin
  2582. GetTypeName:=names[ordtype];
  2583. end;
  2584. {****************************************************************************
  2585. TFLOATDEF
  2586. ****************************************************************************}
  2587. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  2588. begin
  2589. inherited create(floatdef,doregister);
  2590. floattype:=t;
  2591. setsize;
  2592. end;
  2593. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2594. begin
  2595. inherited ppuload(floatdef,ppufile);
  2596. floattype:=tfloattype(ppufile.getbyte);
  2597. setsize;
  2598. ppuload_platform(ppufile);
  2599. end;
  2600. function tfloatdef.getcopy : tstoreddef;
  2601. begin
  2602. result:=cfloatdef.create(floattype,true);
  2603. result.typ:=floatdef;
  2604. tfloatdef(result).savesize:=savesize;
  2605. end;
  2606. function tfloatdef.alignment:shortint;
  2607. begin
  2608. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2609. case floattype of
  2610. sc80real,
  2611. s80real: result:=16;
  2612. s64real,
  2613. s64currency,
  2614. s64comp : result:=4;
  2615. else
  2616. result := inherited alignment;
  2617. end
  2618. else
  2619. result := inherited alignment;
  2620. end;
  2621. function tfloatdef.structalignment: shortint;
  2622. begin
  2623. { aix is really annoying: the recommended scalar alignment for both
  2624. int64 and double is 64 bits, but in structs int64 has to be aligned
  2625. to 8 bytes and double to 4 bytes }
  2626. if (target_info.system in systems_aix) and
  2627. (floattype=s64real) then
  2628. result:=4
  2629. else
  2630. result:=alignment;
  2631. end;
  2632. procedure tfloatdef.setsize;
  2633. begin
  2634. case floattype of
  2635. s32real : savesize:=4;
  2636. s80real : savesize:=10;
  2637. sc80real:
  2638. if target_info.system in [system_i386_darwin,
  2639. system_i386_iphonesim,system_x86_64_darwin,
  2640. system_x86_64_iphonesim,
  2641. system_x86_64_linux,system_x86_64_freebsd,
  2642. system_x86_64_openbsd,system_x86_64_netbsd,
  2643. system_x86_64_solaris,system_x86_64_embedded,
  2644. system_x86_64_dragonfly] then
  2645. savesize:=16
  2646. else
  2647. savesize:=12;
  2648. s64real,
  2649. s64currency,
  2650. s64comp : savesize:=8;
  2651. else
  2652. savesize:=0;
  2653. end;
  2654. end;
  2655. function tfloatdef.getvardef : longint;
  2656. const
  2657. floattype2vardef : array[tfloattype] of longint = (
  2658. varSingle,varDouble,varUndefined,varUndefined,
  2659. varUndefined,varCurrency,varUndefined);
  2660. begin
  2661. if (upper(typename)='TDATETIME') and
  2662. assigned(owner) and
  2663. assigned(owner.name) and
  2664. (owner.name^='SYSTEM') then
  2665. result:=varDate
  2666. else
  2667. result:=floattype2vardef[floattype];
  2668. end;
  2669. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2670. begin
  2671. inherited ppuwrite(ppufile);
  2672. ppufile.putbyte(byte(floattype));
  2673. writeentry(ppufile,ibfloatdef);
  2674. end;
  2675. function tfloatdef.is_publishable : boolean;
  2676. begin
  2677. is_publishable:=true;
  2678. end;
  2679. function tfloatdef.GetTypeName : string;
  2680. const
  2681. names : array[tfloattype] of string[20] = (
  2682. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2683. begin
  2684. GetTypeName:=names[floattype];
  2685. end;
  2686. {****************************************************************************
  2687. TFILEDEF
  2688. ****************************************************************************}
  2689. constructor tfiledef.createtext;
  2690. begin
  2691. inherited create(filedef,true);
  2692. filetyp:=ft_text;
  2693. typedfiledef:=nil;
  2694. end;
  2695. constructor tfiledef.createuntyped;
  2696. begin
  2697. inherited create(filedef,true);
  2698. filetyp:=ft_untyped;
  2699. typedfiledef:=nil;
  2700. end;
  2701. constructor tfiledef.createtyped(def:tdef);
  2702. begin
  2703. inherited create(filedef,true);
  2704. filetyp:=ft_typed;
  2705. typedfiledef:=def;
  2706. end;
  2707. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2708. begin
  2709. inherited ppuload(filedef,ppufile);
  2710. filetyp:=tfiletyp(ppufile.getbyte);
  2711. if filetyp=ft_typed then
  2712. ppufile.getderef(typedfiledefderef)
  2713. else
  2714. typedfiledef:=nil;
  2715. ppuload_platform(ppufile);
  2716. end;
  2717. function tfiledef.getcopy : tstoreddef;
  2718. begin
  2719. case filetyp of
  2720. ft_typed:
  2721. result:=cfiledef.createtyped(typedfiledef);
  2722. ft_untyped:
  2723. result:=cfiledef.createuntyped;
  2724. ft_text:
  2725. result:=cfiledef.createtext;
  2726. else
  2727. internalerror(2004121201);
  2728. end;
  2729. end;
  2730. procedure tfiledef.buildderef;
  2731. begin
  2732. inherited buildderef;
  2733. if filetyp=ft_typed then
  2734. typedfiledefderef.build(typedfiledef);
  2735. end;
  2736. procedure tfiledef.deref;
  2737. begin
  2738. inherited deref;
  2739. if filetyp=ft_typed then
  2740. typedfiledef:=tdef(typedfiledefderef.resolve);
  2741. end;
  2742. function tfiledef.size:asizeint;
  2743. begin
  2744. if savesize=0 then
  2745. setsize;
  2746. size:=savesize;
  2747. end;
  2748. procedure tfiledef.setsize;
  2749. begin
  2750. case filetyp of
  2751. ft_text:
  2752. savesize:=search_system_type('TEXTREC').typedef.size;
  2753. ft_typed:
  2754. begin
  2755. savesize:=search_system_type('FILEREC').typedef.size;
  2756. { allocate put/get buffer in iso mode }
  2757. if m_isolike_io in current_settings.modeswitches then
  2758. inc(savesize,typedfiledef.size);
  2759. end;
  2760. ft_untyped:
  2761. savesize:=search_system_type('FILEREC').typedef.size;
  2762. else
  2763. internalerror(2013113001);
  2764. end;
  2765. end;
  2766. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2767. begin
  2768. inherited ppuwrite(ppufile);
  2769. ppufile.putbyte(byte(filetyp));
  2770. if filetyp=ft_typed then
  2771. ppufile.putderef(typedfiledefderef);
  2772. writeentry(ppufile,ibfiledef);
  2773. end;
  2774. function tfiledef.GetTypeName : string;
  2775. begin
  2776. case filetyp of
  2777. ft_untyped:
  2778. GetTypeName:='File';
  2779. ft_typed:
  2780. GetTypeName:='File Of '+typedfiledef.typename;
  2781. ft_text:
  2782. GetTypeName:='Text'
  2783. else
  2784. internalerror(2013113002);
  2785. end;
  2786. end;
  2787. function tfiledef.getmangledparaname : TSymStr;
  2788. begin
  2789. case filetyp of
  2790. ft_untyped:
  2791. getmangledparaname:='FILE';
  2792. ft_typed:
  2793. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2794. ft_text:
  2795. getmangledparaname:='TEXT'
  2796. else
  2797. internalerror(2013113003);
  2798. end;
  2799. end;
  2800. {****************************************************************************
  2801. TVARIANTDEF
  2802. ****************************************************************************}
  2803. constructor tvariantdef.create(v : tvarianttype);
  2804. begin
  2805. inherited create(variantdef,true);
  2806. varianttype:=v;
  2807. setsize;
  2808. end;
  2809. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2810. begin
  2811. inherited ppuload(variantdef,ppufile);
  2812. varianttype:=tvarianttype(ppufile.getbyte);
  2813. setsize;
  2814. ppuload_platform(ppufile);
  2815. end;
  2816. function tvariantdef.getcopy : tstoreddef;
  2817. begin
  2818. result:=cvariantdef.create(varianttype);
  2819. end;
  2820. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2821. begin
  2822. inherited ppuwrite(ppufile);
  2823. ppufile.putbyte(byte(varianttype));
  2824. writeentry(ppufile,ibvariantdef);
  2825. end;
  2826. function tvariantdef.getvardef : longint;
  2827. begin
  2828. Result:=varVariant;
  2829. end;
  2830. procedure tvariantdef.setsize;
  2831. begin
  2832. {$ifdef cpu64bitaddr}
  2833. savesize:=24;
  2834. {$else cpu64bitaddr}
  2835. savesize:=16;
  2836. {$endif cpu64bitaddr}
  2837. end;
  2838. function tvariantdef.GetTypeName : string;
  2839. begin
  2840. case varianttype of
  2841. vt_normalvariant:
  2842. GetTypeName:='Variant';
  2843. vt_olevariant:
  2844. GetTypeName:='OleVariant';
  2845. else
  2846. internalerror(2013113004);
  2847. end;
  2848. end;
  2849. function tvariantdef.needs_inittable : boolean;
  2850. begin
  2851. needs_inittable:=true;
  2852. end;
  2853. function tvariantdef.is_publishable : boolean;
  2854. begin
  2855. is_publishable:=true;
  2856. end;
  2857. {****************************************************************************
  2858. TABSTRACtpointerdef
  2859. ****************************************************************************}
  2860. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2861. begin
  2862. inherited create(dt,true);
  2863. pointeddef:=def;
  2864. end;
  2865. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2866. begin
  2867. inherited ppuload(dt,ppufile);
  2868. ppufile.getderef(pointeddefderef);
  2869. end;
  2870. procedure tabstractpointerdef.buildderef;
  2871. begin
  2872. inherited buildderef;
  2873. pointeddefderef.build(pointeddef);
  2874. end;
  2875. procedure tabstractpointerdef.deref;
  2876. begin
  2877. inherited deref;
  2878. pointeddef:=tdef(pointeddefderef.resolve);
  2879. end;
  2880. function tabstractpointerdef.size: asizeint;
  2881. begin
  2882. Result:=voidpointertype.size;
  2883. end;
  2884. function tabstractpointerdef.alignment: shortint;
  2885. begin
  2886. alignment:=size_2_align(voidpointertype.size);
  2887. end;
  2888. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2889. begin
  2890. inherited ppuwrite(ppufile);
  2891. ppufile.putderef(pointeddefderef);
  2892. end;
  2893. {****************************************************************************
  2894. tpointerdef
  2895. ****************************************************************************}
  2896. constructor tpointerdef.create(def:tdef);
  2897. begin
  2898. inherited create(pointerdef,def);
  2899. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2900. end;
  2901. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  2902. var
  2903. res: PHashSetItem;
  2904. oldsymtablestack: tsymtablestack;
  2905. begin
  2906. if not assigned(current_module) then
  2907. internalerror(2011071101);
  2908. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  2909. if not assigned(res^.Data) then
  2910. begin
  2911. { since these pointerdefs can be reused anywhere in the current
  2912. unit, add them to the global/staticsymtable (or local symtable
  2913. if they're a local def, because otherwise they'll be saved
  2914. to the ppu referencing a local symtable entry that doesn't
  2915. exist in the ppu) }
  2916. oldsymtablestack:=symtablestack;
  2917. { do not simply push/pop current_module.localsymtable, because
  2918. that can have side-effects (e.g., it removes helpers) }
  2919. symtablestack:=nil;
  2920. result:=cpointerdef.create(def);
  2921. setup_reusable_def(def,result,res,oldsymtablestack);
  2922. { res^.Data may still be nil -> don't overwrite result }
  2923. exit;
  2924. end;
  2925. result:=tpointerdef(res^.Data);
  2926. end;
  2927. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  2928. begin
  2929. result:=getreusable(def);
  2930. if not result.is_registered then
  2931. include(result.defoptions,df_not_registered_no_free);
  2932. end;
  2933. function tpointerdef.size: asizeint;
  2934. begin
  2935. result:=sizeof(pint);
  2936. end;
  2937. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2938. begin
  2939. inherited ppuload(pointerdef,ppufile);
  2940. has_pointer_math:=(ppufile.getbyte<>0);
  2941. ppuload_platform(ppufile);
  2942. end;
  2943. function tpointerdef.getcopy : tstoreddef;
  2944. begin
  2945. { don't use direct pointeddef if it is a forwarddef because in other case
  2946. one of them will be destroyed on forward type resolve and the second will
  2947. point to garbage }
  2948. if pointeddef.typ=forwarddef then
  2949. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  2950. else
  2951. result:=cpointerdef.create(pointeddef);
  2952. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2953. tpointerdef(result).savesize:=savesize;
  2954. end;
  2955. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2956. begin
  2957. inherited ppuwrite(ppufile);
  2958. ppufile.putbyte(byte(has_pointer_math));
  2959. writeentry(ppufile,ibpointerdef);
  2960. end;
  2961. function tpointerdef.GetTypeName : string;
  2962. begin
  2963. { parameter types and the resultdef of a procvardef can contain a
  2964. pointer to this procvardef itself, resulting in endless recursion ->
  2965. use the typesym's name instead if it exists (if it doesn't, such as
  2966. for anynonymous procedure types in macpas/iso mode, then there cannot
  2967. be any recursive references to it either) }
  2968. if (pointeddef.typ<>procvardef) or
  2969. not assigned(pointeddef.typesym) then
  2970. GetTypeName:='^'+pointeddef.typename
  2971. else
  2972. GetTypeName:='^'+pointeddef.typesym.realname;
  2973. end;
  2974. function tpointerdef.pointer_arithmetic_int_type:tdef;
  2975. begin
  2976. result:=ptrsinttype;
  2977. end;
  2978. function tpointerdef.pointer_subtraction_result_type:tdef;
  2979. begin
  2980. result:=ptrsinttype;
  2981. end;
  2982. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  2983. begin
  2984. result:=true;
  2985. end;
  2986. {****************************************************************************
  2987. TCLASSREFDEF
  2988. ****************************************************************************}
  2989. constructor tclassrefdef.create(def:tdef);
  2990. begin
  2991. inherited create(classrefdef,def);
  2992. end;
  2993. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2994. begin
  2995. inherited ppuload(classrefdef,ppufile);
  2996. ppuload_platform(ppufile);
  2997. end;
  2998. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2999. begin
  3000. inherited ppuwrite(ppufile);
  3001. writeentry(ppufile,ibclassrefdef);
  3002. end;
  3003. function tclassrefdef.getcopy:tstoreddef;
  3004. begin
  3005. if pointeddef.typ=forwarddef then
  3006. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3007. else
  3008. result:=cclassrefdef.create(pointeddef);
  3009. end;
  3010. function tclassrefdef.GetTypeName : string;
  3011. begin
  3012. GetTypeName:='Class Of '+pointeddef.typename;
  3013. end;
  3014. function tclassrefdef.is_publishable : boolean;
  3015. begin
  3016. result:=true;
  3017. end;
  3018. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3019. begin
  3020. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3021. result:=inherited rtti_mangledname(rt)
  3022. else
  3023. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3024. end;
  3025. procedure tclassrefdef.register_created_object_type;
  3026. begin
  3027. tobjectdef(pointeddef).register_created_classref_type;
  3028. end;
  3029. {***************************************************************************
  3030. TSETDEF
  3031. ***************************************************************************}
  3032. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3033. var
  3034. setallocbits: aint;
  3035. packedsavesize: aint;
  3036. actual_setalloc: ShortInt;
  3037. begin
  3038. inherited create(setdef,doregister);
  3039. elementdef:=def;
  3040. setmax:=high;
  3041. actual_setalloc:=current_settings.setalloc;
  3042. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3043. if actual_setalloc=0 then
  3044. actual_setalloc:=1;
  3045. {$endif}
  3046. if (actual_setalloc=0) then
  3047. begin
  3048. setbase:=0;
  3049. if (high<32) then
  3050. savesize:=Sizeof(longint)
  3051. else if (high<256) then
  3052. savesize:=32
  3053. else
  3054. savesize:=(high+7) div 8
  3055. end
  3056. else
  3057. begin
  3058. setallocbits:=actual_setalloc*8;
  3059. setbase:=low and not(setallocbits-1);
  3060. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3061. savesize:=packedsavesize;
  3062. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3063. if savesize=3 then
  3064. savesize:=4;
  3065. {$endif}
  3066. end;
  3067. end;
  3068. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3069. begin
  3070. inherited ppuload(setdef,ppufile);
  3071. ppufile.getderef(elementdefderef);
  3072. savesize:=ppufile.getasizeint;
  3073. setbase:=ppufile.getasizeint;
  3074. setmax:=ppufile.getasizeint;
  3075. ppuload_platform(ppufile);
  3076. end;
  3077. function tsetdef.getcopy : tstoreddef;
  3078. begin
  3079. result:=csetdef.create(elementdef,setbase,setmax,true);
  3080. { the copy might have been created with a different setalloc setting }
  3081. tsetdef(result).savesize:=savesize;
  3082. end;
  3083. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3084. begin
  3085. inherited ppuwrite(ppufile);
  3086. ppufile.putderef(elementdefderef);
  3087. ppufile.putasizeint(savesize);
  3088. ppufile.putasizeint(setbase);
  3089. ppufile.putasizeint(setmax);
  3090. writeentry(ppufile,ibsetdef);
  3091. end;
  3092. procedure tsetdef.buildderef;
  3093. begin
  3094. inherited buildderef;
  3095. elementdefderef.build(elementdef);
  3096. end;
  3097. procedure tsetdef.deref;
  3098. begin
  3099. inherited deref;
  3100. elementdef:=tdef(elementdefderef.resolve);
  3101. end;
  3102. function tsetdef.is_publishable : boolean;
  3103. begin
  3104. is_publishable:=savesize in [1,2,4];
  3105. end;
  3106. function tsetdef.GetTypeName : string;
  3107. begin
  3108. if assigned(elementdef) then
  3109. GetTypeName:='Set Of '+elementdef.typename
  3110. else
  3111. GetTypeName:='Empty Set';
  3112. end;
  3113. {***************************************************************************
  3114. TFORMALDEF
  3115. ***************************************************************************}
  3116. constructor tformaldef.create(Atyped:boolean);
  3117. begin
  3118. inherited create(formaldef,true);
  3119. typed:=Atyped;
  3120. savesize:=0;
  3121. end;
  3122. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3123. begin
  3124. inherited ppuload(formaldef,ppufile);
  3125. typed:=boolean(ppufile.getbyte);
  3126. savesize:=0;
  3127. ppuload_platform(ppufile);
  3128. end;
  3129. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3130. begin
  3131. inherited ppuwrite(ppufile);
  3132. ppufile.putbyte(byte(typed));
  3133. writeentry(ppufile,ibformaldef);
  3134. end;
  3135. function tformaldef.GetTypeName : string;
  3136. begin
  3137. if typed then
  3138. GetTypeName:='<Typed formal type>'
  3139. else
  3140. GetTypeName:='<Formal type>';
  3141. end;
  3142. {***************************************************************************
  3143. TARRAYDEF
  3144. ***************************************************************************}
  3145. constructor tarraydef.create(l,h:asizeint;def:tdef);
  3146. begin
  3147. inherited create(arraydef,true);
  3148. lowrange:=l;
  3149. highrange:=h;
  3150. rangedef:=def;
  3151. _elementdef:=nil;
  3152. arrayoptions:=[];
  3153. symtable:=tarraysymtable.create(self);
  3154. end;
  3155. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3156. var
  3157. res: PHashSetItem;
  3158. oldsymtablestack: tsymtablestack;
  3159. arrdesc: packed record
  3160. def: tdef;
  3161. elecount: asizeint;
  3162. end;
  3163. begin
  3164. if not assigned(current_module) then
  3165. internalerror(2011081301);
  3166. arrdesc.def:=def;
  3167. arrdesc.elecount:=elems;
  3168. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3169. if not assigned(res^.Data) then
  3170. begin
  3171. { since these pointerdefs can be reused anywhere in the current
  3172. unit, add them to the global/staticsymtable (or local symtable
  3173. if they're a local def, because otherwise they'll be saved
  3174. to the ppu referencing a local symtable entry that doesn't
  3175. exist in the ppu) }
  3176. oldsymtablestack:=symtablestack;
  3177. { do not simply push/pop current_module.localsymtable, because
  3178. that can have side-effects (e.g., it removes helpers) }
  3179. symtablestack:=nil;
  3180. result:=carraydef.create(0,elems-1,ptrsinttype);
  3181. result.elementdef:=def;
  3182. setup_reusable_def(def,result,res,oldsymtablestack);
  3183. { res^.Data may still be nil -> don't overwrite result }
  3184. exit;
  3185. end;
  3186. result:=tarraydef(res^.Data);
  3187. end;
  3188. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3189. begin
  3190. result:=getreusable(def,elems);
  3191. if not result.is_registered then
  3192. include(result.defoptions,df_not_registered_no_free);
  3193. end;
  3194. destructor tarraydef.destroy;
  3195. begin
  3196. symtable.free;
  3197. symtable:=nil;
  3198. inherited;
  3199. end;
  3200. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3201. begin
  3202. { divide by the element size and do -1 so the array will have a valid size,
  3203. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3204. to avoid a division by zero }
  3205. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,ptrsinttype);
  3206. arrayoptions:=[ado_IsConvertedPointer];
  3207. setelementdef(def.pointeddef);
  3208. end;
  3209. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3210. begin
  3211. inherited ppuload(arraydef,ppufile);
  3212. { the addresses are calculated later }
  3213. ppufile.getderef(_elementdefderef);
  3214. ppufile.getderef(rangedefderef);
  3215. lowrange:=ppufile.getasizeint;
  3216. highrange:=ppufile.getasizeint;
  3217. ppufile.getsmallset(arrayoptions);
  3218. ppuload_platform(ppufile);
  3219. symtable:=tarraysymtable.create(self);
  3220. tarraysymtable(symtable).ppuload(ppufile)
  3221. end;
  3222. function tarraydef.getcopy : tstoreddef;
  3223. begin
  3224. result:=carraydef.create(lowrange,highrange,rangedef);
  3225. tarraydef(result).arrayoptions:=arrayoptions;
  3226. tarraydef(result)._elementdef:=_elementdef;
  3227. end;
  3228. procedure tarraydef.buildderef;
  3229. begin
  3230. inherited buildderef;
  3231. tarraysymtable(symtable).buildderef;
  3232. _elementdefderef.build(_elementdef);
  3233. rangedefderef.build(rangedef);
  3234. end;
  3235. procedure tarraydef.deref;
  3236. begin
  3237. inherited deref;
  3238. tarraysymtable(symtable).deref(false);
  3239. _elementdef:=tdef(_elementdefderef.resolve);
  3240. rangedef:=tdef(rangedefderef.resolve);
  3241. end;
  3242. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3243. begin
  3244. inherited ppuwrite(ppufile);
  3245. ppufile.putderef(_elementdefderef);
  3246. ppufile.putderef(rangedefderef);
  3247. ppufile.putasizeint(lowrange);
  3248. ppufile.putasizeint(highrange);
  3249. ppufile.putsmallset(arrayoptions);
  3250. writeentry(ppufile,ibarraydef);
  3251. tarraysymtable(symtable).ppuwrite(ppufile);
  3252. end;
  3253. function tarraydef.elesize : asizeint;
  3254. begin
  3255. if (ado_IsBitPacked in arrayoptions) then
  3256. internalerror(2006080101);
  3257. if assigned(_elementdef) then
  3258. result:=_elementdef.size
  3259. else
  3260. result:=0;
  3261. end;
  3262. function tarraydef.elepackedbitsize : asizeint;
  3263. begin
  3264. if not(ado_IsBitPacked in arrayoptions) then
  3265. internalerror(2006080102);
  3266. if assigned(_elementdef) then
  3267. result:=_elementdef.packedbitsize
  3268. else
  3269. result:=0;
  3270. end;
  3271. function tarraydef.elecount : asizeuint;
  3272. var
  3273. qhigh,qlow : qword;
  3274. begin
  3275. if ado_IsDynamicArray in arrayoptions then
  3276. begin
  3277. result:=0;
  3278. exit;
  3279. end;
  3280. if (highrange>0) and (lowrange<0) then
  3281. begin
  3282. qhigh:=highrange;
  3283. qlow:=qword(-lowrange);
  3284. { prevent overflow, return 0 to indicate overflow }
  3285. if qhigh+qlow>qword(high(asizeint)-1) then
  3286. result:=0
  3287. else
  3288. result:=qhigh+qlow+1;
  3289. end
  3290. else
  3291. result:=int64(highrange)-lowrange+1;
  3292. end;
  3293. function tarraydef.size : asizeint;
  3294. var
  3295. cachedelecount : asizeuint;
  3296. cachedelesize : asizeint;
  3297. begin
  3298. if ado_IsDynamicArray in arrayoptions then
  3299. begin
  3300. size:=voidpointertype.size;
  3301. exit;
  3302. end;
  3303. { Tarraydef.size may never be called for an open array! }
  3304. if highrange<lowrange then
  3305. internalerror(99080501);
  3306. if not (ado_IsBitPacked in arrayoptions) then
  3307. cachedelesize:=elesize
  3308. else
  3309. cachedelesize := elepackedbitsize;
  3310. cachedelecount:=elecount;
  3311. if (cachedelesize = 0) then
  3312. begin
  3313. size := 0;
  3314. exit;
  3315. end;
  3316. if (cachedelecount = 0) then
  3317. begin
  3318. size := -1;
  3319. exit;
  3320. end;
  3321. { prevent overflow, return -1 to indicate overflow }
  3322. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3323. if (cachedelecount > asizeuint(high(asizeint))) or
  3324. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3325. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3326. accessing the array, see ncgmem (PFV) }
  3327. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3328. begin
  3329. result:=-1;
  3330. exit;
  3331. end;
  3332. result:=cachedelesize*asizeint(cachedelecount);
  3333. if (ado_IsBitPacked in arrayoptions) then
  3334. { can't just add 7 and divide by 8, because that may overflow }
  3335. result:=result div 8 + ord((result mod 8)<>0);
  3336. {$ifdef cpu16bitaddr}
  3337. if result>65535 then
  3338. begin
  3339. result:=-1;
  3340. exit;
  3341. end;
  3342. {$endif cpu16bitaddr}
  3343. end;
  3344. procedure tarraydef.setelementdef(def:tdef);
  3345. begin
  3346. _elementdef:=def;
  3347. if not(
  3348. (ado_IsDynamicArray in arrayoptions) or
  3349. (ado_IsConvertedPointer in arrayoptions) or
  3350. (highrange<lowrange)
  3351. ) and
  3352. (size=-1) then
  3353. Message(sym_e_segment_too_large);
  3354. end;
  3355. function tarraydef.alignment : shortint;
  3356. begin
  3357. { alignment of dyn. arrays doesn't depend on the element size }
  3358. if (ado_IsDynamicArray in arrayoptions) then
  3359. alignment:=voidpointertype.alignment
  3360. { alignment is the target alignment for the used load size }
  3361. else if (ado_IsBitPacked in arrayoptions) and
  3362. (elementdef.typ in [enumdef,orddef]) then
  3363. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3364. { alignment is the alignment of the elements }
  3365. else
  3366. alignment:=elementdef.alignment
  3367. end;
  3368. function tarraydef.needs_inittable : boolean;
  3369. begin
  3370. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3371. end;
  3372. function tarraydef.needs_separate_initrtti : boolean;
  3373. begin
  3374. if ado_IsBitPacked in arrayoptions then
  3375. result:=false
  3376. else
  3377. result:=elementdef.needs_separate_initrtti;
  3378. end;
  3379. function tarraydef.GetTypeName : string;
  3380. begin
  3381. if (ado_IsConstString in arrayoptions) then
  3382. result:='Constant String'
  3383. else if (ado_isarrayofconst in arrayoptions) or
  3384. (ado_isConstructor in arrayoptions) then
  3385. begin
  3386. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3387. GetTypeName:='Array Of Const'
  3388. else
  3389. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3390. end
  3391. else if (ado_IsDynamicArray in arrayoptions) then
  3392. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3393. else if ((highrange=-1) and (lowrange=0)) then
  3394. GetTypeName:='{Open} Array Of '+elementdef.typename
  3395. else
  3396. begin
  3397. result := '';
  3398. if (ado_IsBitPacked in arrayoptions) then
  3399. result:='BitPacked ';
  3400. if rangedef.typ=enumdef then
  3401. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3402. else
  3403. result:=result+'Array['+tostr(lowrange)+'..'+
  3404. tostr(highrange)+'] Of '+elementdef.typename
  3405. end;
  3406. end;
  3407. function tarraydef.getmangledparaname : TSymStr;
  3408. begin
  3409. if ado_isarrayofconst in arrayoptions then
  3410. getmangledparaname:='array_of_const'
  3411. else
  3412. if ((highrange=-1) and (lowrange=0)) then
  3413. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3414. else
  3415. internalerror(200204176);
  3416. end;
  3417. function tarraydef.is_publishable : boolean;
  3418. begin
  3419. Result:=ado_IsDynamicArray in arrayoptions;
  3420. end;
  3421. {***************************************************************************
  3422. tabstractrecorddef
  3423. ***************************************************************************}
  3424. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  3425. begin
  3426. inherited create(dt,doregister);
  3427. objname:=stringdup(upper(n));
  3428. objrealname:=stringdup(n);
  3429. objectoptions:=[];
  3430. if assigned(current_module.namespace) then
  3431. begin
  3432. import_lib:=stringdup(current_module.namespace^);
  3433. replace(import_lib^,'.','/');
  3434. end;
  3435. end;
  3436. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3437. begin
  3438. inherited ppuload(dt,ppufile);
  3439. objrealname:=stringdup(ppufile.getstring);
  3440. objname:=stringdup(upper(objrealname^));
  3441. import_lib:=stringdup(ppufile.getstring);
  3442. { only used for external C++ classes and Java classes/records }
  3443. if (import_lib^='') then
  3444. stringdispose(import_lib);
  3445. ppufile.getsmallset(objectoptions);
  3446. end;
  3447. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3448. begin
  3449. inherited ppuwrite(ppufile);
  3450. ppufile.putstring(objrealname^);
  3451. if assigned(import_lib) then
  3452. ppufile.putstring(import_lib^)
  3453. else
  3454. ppufile.putstring('');
  3455. ppufile.putsmallset(objectoptions);
  3456. end;
  3457. destructor tabstractrecorddef.destroy;
  3458. begin
  3459. stringdispose(objname);
  3460. stringdispose(objrealname);
  3461. stringdispose(import_lib);
  3462. tcinitcode.free;
  3463. inherited destroy;
  3464. end;
  3465. procedure tabstractrecorddef.buildderefimpl;
  3466. begin
  3467. inherited buildderefimpl;
  3468. if not (df_copied_def in defoptions) then
  3469. tstoredsymtable(symtable).buildderefimpl;
  3470. end;
  3471. procedure tabstractrecorddef.derefimpl;
  3472. begin
  3473. inherited derefimpl;
  3474. if not (df_copied_def in defoptions) then
  3475. tstoredsymtable(symtable).derefimpl(false);
  3476. end;
  3477. procedure tabstractrecorddef.check_forwards;
  3478. begin
  3479. { the defs of a copied def are defined for the original type only }
  3480. if not(df_copied_def in defoptions) then
  3481. tstoredsymtable(symtable).check_forwards;
  3482. end;
  3483. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3484. var
  3485. i: longint;
  3486. sym: tsym;
  3487. begin
  3488. for i:=0 to symtable.SymList.Count-1 do
  3489. begin
  3490. sym:=tsym(symtable.SymList[i]);
  3491. if sym.typ=procsym then
  3492. begin
  3493. result:=tprocsym(sym).find_procdef_bytype(pt);
  3494. if assigned(result) then
  3495. exit;
  3496. end;
  3497. end;
  3498. result:=nil;
  3499. end;
  3500. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3501. begin
  3502. if t=gs_record then
  3503. GetSymtable:=symtable
  3504. else
  3505. GetSymtable:=nil;
  3506. end;
  3507. function tabstractrecorddef.is_packed:boolean;
  3508. begin
  3509. result:=tabstractrecordsymtable(symtable).is_packed;
  3510. end;
  3511. function tabstractrecorddef.RttiName: string;
  3512. function generate_full_paramname(maxlength:longint):string;
  3513. const
  3514. commacount : array[boolean] of longint = (0,1);
  3515. var
  3516. fullparas,
  3517. paramname : ansistring;
  3518. module : tmodule;
  3519. sym : ttypesym;
  3520. i : longint;
  3521. begin
  3522. { we want at least enough space for an ellipsis }
  3523. if maxlength<3 then
  3524. internalerror(2014121203);
  3525. fullparas:='';
  3526. for i:=0 to genericparas.count-1 do
  3527. begin
  3528. sym:=ttypesym(genericparas[i]);
  3529. module:=find_module_from_symtable(sym.owner);
  3530. if not assigned(module) then
  3531. internalerror(2014121202);
  3532. paramname:=module.realmodulename^;
  3533. if sym.typedef.typ in [objectdef,recorddef] then
  3534. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3535. else
  3536. paramname:=paramname+'.'+sym.typedef.typename;
  3537. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3538. begin
  3539. if i>0 then
  3540. fullparas:=fullparas+',...'
  3541. else
  3542. fullparas:=fullparas+'...';
  3543. break;
  3544. end;
  3545. { could we fit an ellipsis after this parameter if it should be too long? }
  3546. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3547. begin
  3548. { then omit already this parameter }
  3549. if i>0 then
  3550. fullparas:=fullparas+',...'
  3551. else
  3552. fullparas:=fullparas+'...';
  3553. break;
  3554. end;
  3555. if i>0 then
  3556. fullparas:=fullparas+',';
  3557. fullparas:=fullparas+paramname;
  3558. end;
  3559. result:=fullparas;
  3560. end;
  3561. var
  3562. nongeneric,
  3563. basename : string;
  3564. i,
  3565. remlength,
  3566. paramcount,
  3567. crcidx : longint;
  3568. begin
  3569. if rttistring='' then
  3570. begin
  3571. if is_specialization then
  3572. begin
  3573. rttistring:=OwnerHierarchyName;
  3574. { there should be two $ characters, one before the CRC and one before the count }
  3575. crcidx:=-1;
  3576. for i:=length(objrealname^) downto 1 do
  3577. if objrealname^[i]='$' then
  3578. begin
  3579. crcidx:=i;
  3580. break;
  3581. end;
  3582. if crcidx<0 then
  3583. internalerror(2014121201);
  3584. basename:=copy(objrealname^,1,crcidx-1);
  3585. split_generic_name(basename,nongeneric,paramcount);
  3586. rttistring:=rttistring+nongeneric+'<';
  3587. remlength:=255-length(rttistring)-1;
  3588. if remlength<4 then
  3589. rttistring:=rttistring+'>'
  3590. else
  3591. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  3592. end
  3593. else
  3594. if is_generic then
  3595. begin
  3596. rttistring:=OwnerHierarchyName;
  3597. split_generic_name(objrealname^,nongeneric,paramcount);
  3598. rttistring:=rttistring+nongeneric+'<';
  3599. { we don't want any ',' if there is only one parameter }
  3600. for i:=0 to paramcount-0 do
  3601. rttistring:=rttistring+',';
  3602. rttistring:=rttistring+'>';
  3603. end
  3604. else
  3605. rttistring:=OwnerHierarchyName+objrealname^;
  3606. end;
  3607. result:=rttistring;
  3608. end;
  3609. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3610. var
  3611. sym : tsym;
  3612. i : integer;
  3613. pd : tprocdef;
  3614. hashedid : THashedIDString;
  3615. begin
  3616. result:=nil;
  3617. hashedid.id:='GETENUMERATOR';
  3618. sym:=tsym(symtable.FindWithHash(hashedid));
  3619. if assigned(sym) and (sym.typ=procsym) then
  3620. begin
  3621. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3622. begin
  3623. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3624. if (pd.proctypeoption = potype_function) and
  3625. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3626. (pd.visibility >= vis_public) then
  3627. begin
  3628. result:=pd;
  3629. exit;
  3630. end;
  3631. end;
  3632. end;
  3633. end;
  3634. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3635. var
  3636. sym : tsym;
  3637. i : integer;
  3638. pd : tprocdef;
  3639. hashedid : THashedIDString;
  3640. begin
  3641. result:=nil;
  3642. // first search for po_enumerator_movenext method modifier
  3643. // then search for public function MoveNext: Boolean
  3644. for i:=0 to symtable.SymList.Count-1 do
  3645. begin
  3646. sym:=TSym(symtable.SymList[i]);
  3647. if (sym.typ=procsym) then
  3648. begin
  3649. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3650. if assigned(pd) then
  3651. begin
  3652. result:=pd;
  3653. exit;
  3654. end;
  3655. end;
  3656. end;
  3657. hashedid.id:='MOVENEXT';
  3658. sym:=tsym(symtable.FindWithHash(hashedid));
  3659. if assigned(sym) and (sym.typ=procsym) then
  3660. begin
  3661. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3662. begin
  3663. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3664. if (pd.proctypeoption = potype_function) and
  3665. is_boolean(pd.returndef) and
  3666. (pd.minparacount = 0) and
  3667. (pd.visibility >= vis_public) then
  3668. begin
  3669. result:=pd;
  3670. exit;
  3671. end;
  3672. end;
  3673. end;
  3674. end;
  3675. function tabstractrecorddef.search_enumerator_current: tsym;
  3676. var
  3677. sym: tsym;
  3678. i: integer;
  3679. hashedid : THashedIDString;
  3680. begin
  3681. result:=nil;
  3682. // first search for ppo_enumerator_current property modifier
  3683. // then search for public property Current
  3684. for i:=0 to symtable.SymList.Count-1 do
  3685. begin
  3686. sym:=TSym(symtable.SymList[i]);
  3687. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3688. begin
  3689. result:=sym;
  3690. exit;
  3691. end;
  3692. end;
  3693. hashedid.id:='CURRENT';
  3694. sym:=tsym(symtable.FindWithHash(hashedid));
  3695. if assigned(sym) and (sym.typ=propertysym) and
  3696. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3697. begin
  3698. result:=sym;
  3699. exit;
  3700. end;
  3701. end;
  3702. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3703. var
  3704. st: tsymtable;
  3705. enclosingdef: tdef;
  3706. begin
  3707. if typ=objectdef then
  3708. result:=tobjectdef(self).objextname^
  3709. else if assigned(typesym) then
  3710. result:=typesym.realname
  3711. { have to generate anonymous nested type in current unit/class/record }
  3712. else
  3713. internalerror(2011032601);
  3714. { in case of specializations, add some extras to prevent name conflicts
  3715. with nested classes }
  3716. if df_specialization in defoptions then
  3717. result:='$'+result+'$specialization$';
  3718. st:=owner;
  3719. while assigned(st) and
  3720. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3721. begin
  3722. { nested classes are named as "OuterClass$InnerClass" }
  3723. enclosingdef:=tdef(st.defowner);
  3724. if enclosingdef.typ=procdef then
  3725. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3726. else if enclosingdef.typ=objectdef then
  3727. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3728. else if assigned(enclosingdef.typesym) then
  3729. result:=enclosingdef.typesym.realname+'$'+result
  3730. else
  3731. internalerror(2011060305);
  3732. st:=enclosingdef.owner;
  3733. end;
  3734. if with_package_name and
  3735. assigned(import_lib) then
  3736. result:=import_lib^+'/'+result;
  3737. end;
  3738. function tabstractrecorddef.contains_float_field: boolean;
  3739. var
  3740. i : longint;
  3741. begin
  3742. result:=true;
  3743. for i:=0 to symtable.symlist.count-1 do
  3744. begin
  3745. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  3746. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  3747. continue;
  3748. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  3749. begin
  3750. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3751. exit;
  3752. { search recursively }
  3753. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  3754. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  3755. exit;
  3756. end;
  3757. end;
  3758. result:=false;
  3759. end;
  3760. {***************************************************************************
  3761. trecorddef
  3762. ***************************************************************************}
  3763. constructor trecorddef.create(const n:string; p:TSymtable);
  3764. begin
  3765. inherited create(n,recorddef,true);
  3766. symtable:=p;
  3767. { we can own the symtable only if nobody else owns a copy so far }
  3768. if symtable.refcount=1 then
  3769. symtable.defowner:=self;
  3770. isunion:=false;
  3771. end;
  3772. constructor trecorddef.create_global_internal(n: string; packrecords, recordalignmin, maxCrecordalign: shortint);
  3773. var
  3774. oldsymtablestack: tsymtablestack;
  3775. ts: ttypesym;
  3776. definedname: boolean;
  3777. begin
  3778. { construct name }
  3779. definedname:=n<>'';
  3780. if not definedname then
  3781. n:='$InternalRec'+tostr(current_module.deflist.count);
  3782. oldsymtablestack:=symtablestack;
  3783. { do not simply push/pop current_module.localsymtable, because
  3784. that can have side-effects (e.g., it removes helpers) }
  3785. symtablestack:=nil;
  3786. symtable:=trecordsymtable.create(n,packrecords,recordalignmin,maxCrecordalign);
  3787. symtable.defowner:=self;
  3788. isunion:=false;
  3789. inherited create(n,recorddef,true);
  3790. { if we specified a name, then we'll probably want to look up the
  3791. type again by name too -> create typesym }
  3792. ts:=nil;
  3793. if definedname then
  3794. begin
  3795. ts:=ctypesym.create(n,self,true);
  3796. { avoid hints about unused types (these may only be used for
  3797. typed constant data) }
  3798. ts.increfcount;
  3799. end;
  3800. if assigned(current_module.localsymtable) then
  3801. begin
  3802. current_module.localsymtable.insertdef(self);
  3803. if definedname then
  3804. current_module.localsymtable.insert(ts);
  3805. end
  3806. else
  3807. begin
  3808. current_module.globalsymtable.insertdef(self);
  3809. if definedname then
  3810. current_module.globalsymtable.insert(ts);
  3811. end;
  3812. symtablestack:=oldsymtablestack;
  3813. { don't create RTTI for internal types, these are not exported }
  3814. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  3815. end;
  3816. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  3817. var
  3818. sym: tfieldvarsym;
  3819. name: TIDString;
  3820. pname: ^TIDString;
  3821. begin
  3822. if optionalname='' then
  3823. begin
  3824. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  3825. pname:=@name
  3826. end
  3827. else
  3828. pname:=@optionalname;
  3829. sym:=cfieldvarsym.create(pname^,vs_value,def,[],true);
  3830. symtable.insert(sym);
  3831. trecordsymtable(symtable).addfield(sym,vis_hidden);
  3832. result:=sym;
  3833. end;
  3834. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  3835. var
  3836. i: longint;
  3837. begin
  3838. for i:=0 to fieldtypes.count-1 do
  3839. add_field_by_def('',tdef(fieldtypes[i]));
  3840. end;
  3841. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3842. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3843. var
  3844. i,j : longint;
  3845. begin
  3846. if ppufile.getbyte=1 then
  3847. begin
  3848. new(variantrecdesc);
  3849. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3850. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3851. for i:=0 to high(variantrecdesc^.branches) do
  3852. begin
  3853. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3854. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3855. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3856. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3857. end;
  3858. end
  3859. else
  3860. variantrecdesc:=nil;
  3861. end;
  3862. begin
  3863. inherited ppuload(recorddef,ppufile);
  3864. if df_copied_def in defoptions then
  3865. begin
  3866. ppufile.getderef(cloneddefderef);
  3867. ppuload_platform(ppufile);
  3868. end
  3869. else
  3870. begin
  3871. ppuload_platform(ppufile);
  3872. symtable:=trecordsymtable.create(objrealname^,0,0,0);
  3873. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3874. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3875. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3876. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3877. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  3878. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3879. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3880. trecordsymtable(symtable).ppuload(ppufile);
  3881. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3882. but because iso mode supports no units, there is no need to store the variantrecdesc
  3883. in the ppu
  3884. }
  3885. // readvariantrecdesc(variantrecdesc);
  3886. { requires usefieldalignment to be set }
  3887. symtable.defowner:=self;
  3888. end;
  3889. isunion:=false;
  3890. end;
  3891. destructor trecorddef.destroy;
  3892. begin
  3893. if assigned(symtable) then
  3894. begin
  3895. symtable.free;
  3896. symtable:=nil;
  3897. end;
  3898. inherited destroy;
  3899. end;
  3900. function trecorddef.getcopy : tstoreddef;
  3901. begin
  3902. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3903. trecorddef(result).isunion:=isunion;
  3904. include(trecorddef(result).defoptions,df_copied_def);
  3905. if assigned(tcinitcode) then
  3906. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3907. if assigned(import_lib) then
  3908. trecorddef(result).import_lib:=stringdup(import_lib^);
  3909. end;
  3910. function trecorddef.needs_inittable : boolean;
  3911. begin
  3912. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3913. end;
  3914. function trecorddef.needs_separate_initrtti : boolean;
  3915. begin
  3916. result:=true;
  3917. end;
  3918. procedure trecorddef.buildderef;
  3919. begin
  3920. inherited buildderef;
  3921. if df_copied_def in defoptions then
  3922. cloneddefderef.build(symtable.defowner)
  3923. else
  3924. tstoredsymtable(symtable).buildderef;
  3925. end;
  3926. procedure trecorddef.deref;
  3927. begin
  3928. inherited deref;
  3929. { now dereference the definitions }
  3930. if df_copied_def in defoptions then
  3931. begin
  3932. cloneddef:=trecorddef(cloneddefderef.resolve);
  3933. symtable:=cloneddef.symtable.getcopy;
  3934. end
  3935. else
  3936. tstoredsymtable(symtable).deref(false);
  3937. { internal types, only load from the system unit }
  3938. if assigned(owner) and
  3939. assigned(owner.name) and
  3940. (owner.name^='SYSTEM') then
  3941. begin
  3942. { TGUID }
  3943. if not assigned(rec_tguid) and
  3944. (upper(typename)='TGUID') then
  3945. rec_tguid:=self
  3946. { JMP_BUF }
  3947. else if not assigned(rec_jmp_buf) and
  3948. (upper(typename)='JMP_BUF') then
  3949. rec_jmp_buf:=self
  3950. else if not assigned(rec_exceptaddr) and
  3951. (upper(typename)='TEXCEPTADDR') then
  3952. rec_exceptaddr:=self;
  3953. end;
  3954. end;
  3955. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3956. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3957. var
  3958. i,j : longint;
  3959. begin
  3960. if assigned(variantrecdesc) then
  3961. begin
  3962. ppufile.putbyte(1);
  3963. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3964. ppufile.putasizeint(length(variantrecdesc^.branches));
  3965. for i:=0 to high(variantrecdesc^.branches) do
  3966. begin
  3967. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3968. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3969. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3970. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3971. end;
  3972. end
  3973. else
  3974. ppufile.putbyte(0);
  3975. end;
  3976. begin
  3977. inherited ppuwrite(ppufile);
  3978. if df_copied_def in defoptions then
  3979. ppufile.putderef(cloneddefderef)
  3980. else
  3981. begin
  3982. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3983. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3984. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3985. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3986. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  3987. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3988. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3989. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3990. but because iso mode supports no units, there is no need to store the variantrecdesc
  3991. in the ppu
  3992. }
  3993. // writevariantrecdesc(variantrecdesc);
  3994. end;
  3995. writeentry(ppufile,ibrecorddef);
  3996. if not(df_copied_def in defoptions) then
  3997. trecordsymtable(symtable).ppuwrite(ppufile);
  3998. end;
  3999. function trecorddef.size:asizeint;
  4000. begin
  4001. result:=trecordsymtable(symtable).datasize;
  4002. end;
  4003. function trecorddef.alignment:shortint;
  4004. begin
  4005. alignment:=trecordsymtable(symtable).recordalignment;
  4006. end;
  4007. function trecorddef.padalignment:shortint;
  4008. begin
  4009. padalignment := trecordsymtable(symtable).padalignment;
  4010. end;
  4011. function trecorddef.GetTypeName : string;
  4012. begin
  4013. GetTypeName:='<record type>'
  4014. end;
  4015. {***************************************************************************
  4016. TABSTRACTPROCDEF
  4017. ***************************************************************************}
  4018. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  4019. begin
  4020. inherited create(dt,doregister);
  4021. parast:=tparasymtable.create(self,level);
  4022. paras:=nil;
  4023. minparacount:=0;
  4024. maxparacount:=0;
  4025. proctypeoption:=potype_none;
  4026. proccalloption:=pocall_none;
  4027. procoptions:=[];
  4028. returndef:=voidtype;
  4029. savesize:=sizeof(pint);
  4030. callerargareasize:=0;
  4031. calleeargareasize:=0;
  4032. has_paraloc_info:=callnoside;
  4033. funcretloc[callerside].init;
  4034. funcretloc[calleeside].init;
  4035. check_mark_as_nested;
  4036. end;
  4037. destructor tabstractprocdef.destroy;
  4038. begin
  4039. if assigned(paras) then
  4040. begin
  4041. {$ifdef MEMDEBUG}
  4042. memprocpara.start;
  4043. {$endif MEMDEBUG}
  4044. paras.free;
  4045. paras:=nil;
  4046. {$ifdef MEMDEBUG}
  4047. memprocpara.stop;
  4048. {$endif MEMDEBUG}
  4049. end;
  4050. if assigned(parast) then
  4051. begin
  4052. {$ifdef MEMDEBUG}
  4053. memprocparast.start;
  4054. {$endif MEMDEBUG}
  4055. parast.free;
  4056. parast:=nil;
  4057. {$ifdef MEMDEBUG}
  4058. memprocparast.stop;
  4059. {$endif MEMDEBUG}
  4060. end;
  4061. funcretloc[callerside].done;
  4062. funcretloc[calleeside].done;
  4063. inherited destroy;
  4064. end;
  4065. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  4066. begin
  4067. if (tsym(p).typ<>paravarsym) then
  4068. exit;
  4069. inc(plongint(arg)^);
  4070. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  4071. begin
  4072. if not assigned(tparavarsym(p).defaultconstsym) then
  4073. inc(minparacount);
  4074. inc(maxparacount);
  4075. end;
  4076. end;
  4077. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  4078. begin
  4079. if (tsym(p).typ<>paravarsym) then
  4080. exit;
  4081. paras.add(p);
  4082. end;
  4083. procedure tabstractprocdef.calcparas;
  4084. var
  4085. paracount : longint;
  4086. begin
  4087. { This can already be assigned when
  4088. we need to reresolve this unit (PFV) }
  4089. if assigned(paras) then
  4090. paras.free;
  4091. paras:=tparalist.create(false);
  4092. paracount:=0;
  4093. minparacount:=0;
  4094. maxparacount:=0;
  4095. parast.SymList.ForEachCall(@count_para,@paracount);
  4096. paras.capacity:=paracount;
  4097. { Insert parameters in table }
  4098. parast.SymList.ForEachCall(@insert_para,nil);
  4099. { Order parameters }
  4100. paras.sortparas;
  4101. end;
  4102. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  4103. var
  4104. crc : dword;
  4105. hp : TParavarsym;
  4106. hs : TSymStr;
  4107. newlen,
  4108. i : integer;
  4109. begin
  4110. result:='';
  4111. hp:=nil;
  4112. { add parameter types }
  4113. for i:=0 to paras.count-1 do
  4114. begin
  4115. hp:=tparavarsym(paras[i]);
  4116. if not(vo_is_hidden_para in hp.varoptions) then
  4117. result:=result+'$'+hp.vardef.mangledparaname;
  4118. end;
  4119. { add resultdef, add $$ as separator to make it unique from a
  4120. parameter separator }
  4121. if not is_void(returndef) then
  4122. result:=result+'$$'+returndef.mangledparaname;
  4123. newlen:=length(result)+oldlen;
  4124. { Replace with CRC if the parameter line is very long }
  4125. if (newlen-oldlen>12) and
  4126. ((newlen>100) or (newlen-oldlen>64)) then
  4127. begin
  4128. crc:=0;
  4129. for i:=0 to paras.count-1 do
  4130. begin
  4131. hp:=tparavarsym(paras[i]);
  4132. if not(vo_is_hidden_para in hp.varoptions) then
  4133. begin
  4134. hs:=hp.vardef.mangledparaname;
  4135. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4136. end;
  4137. end;
  4138. if not is_void(returndef) then
  4139. begin
  4140. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  4141. hs:='$$'+returndef.mangledparaname;
  4142. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4143. end;
  4144. result:='$crc'+hexstr(crc,8);
  4145. end;
  4146. end;
  4147. procedure tabstractprocdef.buildderef;
  4148. begin
  4149. { released procdef? }
  4150. if not assigned(parast) then
  4151. exit;
  4152. inherited buildderef;
  4153. returndefderef.build(returndef);
  4154. if po_explicitparaloc in procoptions then
  4155. funcretloc[callerside].buildderef;
  4156. { parast }
  4157. tparasymtable(parast).buildderef;
  4158. end;
  4159. procedure tabstractprocdef.deref;
  4160. begin
  4161. inherited deref;
  4162. returndef:=tdef(returndefderef.resolve);
  4163. if po_explicitparaloc in procoptions then
  4164. begin
  4165. funcretloc[callerside].deref;
  4166. has_paraloc_info:=callerside;
  4167. end
  4168. else
  4169. begin
  4170. { deref is called after loading from a ppu, but also after another
  4171. unit has been reloaded/recompiled and all references must be
  4172. re-resolved. Since the funcretloc contains a reference to a tdef,
  4173. reset it so that we won't try to access the stale def }
  4174. funcretloc[callerside].init;
  4175. has_paraloc_info:=callnoside;
  4176. end;
  4177. { parast }
  4178. tparasymtable(parast).deref(false);
  4179. { recalculated parameters }
  4180. calcparas;
  4181. end;
  4182. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4183. begin
  4184. inherited ppuload(dt,ppufile);
  4185. parast:=nil;
  4186. Paras:=nil;
  4187. minparacount:=0;
  4188. maxparacount:=0;
  4189. ppufile.getderef(returndefderef);
  4190. proctypeoption:=tproctypeoption(ppufile.getbyte);
  4191. proccalloption:=tproccalloption(ppufile.getbyte);
  4192. ppufile.getnormalset(procoptions);
  4193. funcretloc[callerside].init;
  4194. if po_explicitparaloc in procoptions then
  4195. funcretloc[callerside].ppuload(ppufile);
  4196. savesize:=sizeof(pint);
  4197. if (po_explicitparaloc in procoptions) then
  4198. has_paraloc_info:=callerside;
  4199. end;
  4200. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  4201. var
  4202. oldintfcrc : boolean;
  4203. begin
  4204. { released procdef? }
  4205. if not assigned(parast) then
  4206. exit;
  4207. inherited ppuwrite(ppufile);
  4208. ppufile.putderef(returndefderef);
  4209. oldintfcrc:=ppufile.do_interface_crc;
  4210. ppufile.do_interface_crc:=false;
  4211. ppufile.putbyte(ord(proctypeoption));
  4212. ppufile.putbyte(ord(proccalloption));
  4213. ppufile.putnormalset(procoptions);
  4214. ppufile.do_interface_crc:=oldintfcrc;
  4215. if (po_explicitparaloc in procoptions) then
  4216. funcretloc[callerside].ppuwrite(ppufile);
  4217. end;
  4218. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  4219. var
  4220. hs,s : ansistring;
  4221. hp : TParavarsym;
  4222. hpc : tconstsym;
  4223. first : boolean;
  4224. i : integer;
  4225. begin
  4226. s:='';
  4227. first:=true;
  4228. for i:=0 to paras.count-1 do
  4229. begin
  4230. hp:=tparavarsym(paras[i]);
  4231. if not(vo_is_hidden_para in hp.varoptions) or
  4232. (pno_showhidden in pno) then
  4233. begin
  4234. if first then
  4235. begin
  4236. s:=s+'(';
  4237. first:=false;
  4238. end
  4239. else
  4240. s:=s+';';
  4241. if vo_is_hidden_para in hp.varoptions then
  4242. s:=s+'<';
  4243. case hp.varspez of
  4244. vs_var :
  4245. s:=s+'var ';
  4246. vs_const :
  4247. s:=s+'const ';
  4248. vs_out :
  4249. s:=s+'out ';
  4250. vs_constref :
  4251. s:=s+'constref ';
  4252. end;
  4253. if (pno_paranames in pno) then
  4254. s:=s+hp.realname+':';
  4255. if hp.univpara then
  4256. s:=s+'univ ';
  4257. if assigned(hp.vardef.typesym) then
  4258. begin
  4259. hs:=hp.vardef.typesym.realname;
  4260. if hs[1]<>'$' then
  4261. s:=s+hp.vardef.OwnerHierarchyName+hs
  4262. else
  4263. s:=s+hp.vardef.GetTypeName;
  4264. end
  4265. else
  4266. s:=s+hp.vardef.GetTypeName;
  4267. { default value }
  4268. if assigned(hp.defaultconstsym) then
  4269. begin
  4270. hpc:=tconstsym(hp.defaultconstsym);
  4271. hs:='';
  4272. case hpc.consttyp of
  4273. conststring,
  4274. constresourcestring :
  4275. begin
  4276. If hpc.value.len>0 then
  4277. begin
  4278. setLength(hs,hpc.value.len);
  4279. { don't write past the end of hs if the constant
  4280. is > 255 chars }
  4281. move(hpc.value.valueptr^,hs[1],length(hs));
  4282. { make sure that constant strings with newline chars
  4283. don't create a linebreak in the assembler code,
  4284. since comments are line-based. Also remove nulls
  4285. because the comments are written as a pchar. }
  4286. ReplaceCase(hs,#0,'.');
  4287. ReplaceCase(hs,#10,'.');
  4288. ReplaceCase(hs,#13,'.');
  4289. end;
  4290. end;
  4291. constreal :
  4292. str(pbestreal(hpc.value.valueptr)^,hs);
  4293. constpointer :
  4294. hs:=tostr(hpc.value.valueordptr);
  4295. constord :
  4296. begin
  4297. if is_boolean(hpc.constdef) then
  4298. begin
  4299. if hpc.value.valueord<>0 then
  4300. hs:='TRUE'
  4301. else
  4302. hs:='FALSE';
  4303. end
  4304. else
  4305. hs:=tostr(hpc.value.valueord);
  4306. end;
  4307. constnil :
  4308. hs:='nil';
  4309. constset :
  4310. hs:='<set>';
  4311. end;
  4312. if hs<>'' then
  4313. s:=s+'="'+hs+'"';
  4314. end;
  4315. if vo_is_hidden_para in hp.varoptions then
  4316. s:=s+'>';
  4317. end;
  4318. end;
  4319. if not first then
  4320. s:=s+')';
  4321. if (po_varargs in procoptions) then
  4322. s:=s+';VarArgs';
  4323. typename_paras:=s;
  4324. end;
  4325. function tabstractprocdef.is_methodpointer:boolean;
  4326. begin
  4327. result:=false;
  4328. end;
  4329. function tabstractprocdef.is_addressonly:boolean;
  4330. begin
  4331. result:=true;
  4332. end;
  4333. function tabstractprocdef.no_self_node: boolean;
  4334. begin
  4335. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4336. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4337. end;
  4338. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  4339. var
  4340. j, nestinglevel: longint;
  4341. pvs, npvs: tparavarsym;
  4342. begin
  4343. nestinglevel:=parast.symtablelevel;
  4344. if newtyp=procdef then
  4345. begin
  4346. if (copytyp<>pc_bareproc) then
  4347. result:=cprocdef.create(nestinglevel,true)
  4348. else
  4349. result:=cprocdef.create(normal_function_level,true);
  4350. tprocdef(result).visibility:=vis_public;
  4351. end
  4352. else
  4353. begin
  4354. result:=cprocvardef.create(nestinglevel);
  4355. end;
  4356. tabstractprocdef(result).returndef:=returndef;
  4357. tabstractprocdef(result).returndefderef:=returndefderef;
  4358. pvs:=nil;
  4359. npvs:=nil;
  4360. for j:=0 to parast.symlist.count-1 do
  4361. begin
  4362. case tsym(parast.symlist[j]).typ of
  4363. paravarsym:
  4364. begin
  4365. pvs:=tparavarsym(parast.symlist[j]);
  4366. { in case of bare proc, don't copy self, vmt or framepointer
  4367. parameters }
  4368. if (copytyp=pc_bareproc) and
  4369. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4370. continue;
  4371. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4372. pvs.vardef,pvs.varoptions);
  4373. npvs.defaultconstsym:=pvs.defaultconstsym;
  4374. tabstractprocdef(result).parast.insert(npvs);
  4375. end;
  4376. constsym:
  4377. begin
  4378. // ignore, reuse original constym. Should also be duplicated
  4379. // be safe though
  4380. end;
  4381. symconst.typesym:
  4382. begin
  4383. // reuse original, part of generic declaration
  4384. end
  4385. else
  4386. internalerror(201160604);
  4387. end;
  4388. end;
  4389. tabstractprocdef(result).savesize:=savesize;
  4390. if (typ<>procvardef) and
  4391. (newtyp=procvardef) then
  4392. begin
  4393. { procvars can't be (class)constructures/destructors etc }
  4394. if proctypeoption=potype_constructor then
  4395. begin
  4396. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  4397. if not(is_implicit_pointer_object_type(returndef) or
  4398. (returndef.typ<>objectdef)) then
  4399. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  4400. tabstractprocdef(result).proctypeoption:=potype_function;
  4401. end
  4402. else if is_void(returndef) then
  4403. tabstractprocdef(result).proctypeoption:=potype_procedure
  4404. else
  4405. tabstractprocdef(result).proctypeoption:=potype_function;
  4406. end
  4407. else
  4408. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4409. tabstractprocdef(result).proccalloption:=proccalloption;
  4410. tabstractprocdef(result).procoptions:=procoptions;
  4411. if (copytyp=pc_bareproc) then
  4412. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4413. if newtyp=procvardef then
  4414. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4415. if copytyp=pc_address_only then
  4416. include(tabstractprocdef(result).procoptions,po_addressonly);
  4417. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4418. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4419. tabstractprocdef(result).maxparacount:=maxparacount;
  4420. tabstractprocdef(result).minparacount:=minparacount;
  4421. if po_explicitparaloc in procoptions then
  4422. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4423. { recalculate parameter info }
  4424. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4425. {$ifdef m68k}
  4426. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4427. {$endif}
  4428. if (typ=procdef) and
  4429. (newtyp=procvardef) and
  4430. (owner.symtabletype=ObjectSymtable) then
  4431. include(tprocvardef(result).procoptions,po_methodpointer);
  4432. end;
  4433. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  4434. begin
  4435. result:=is_addressonly;
  4436. end;
  4437. procedure tabstractprocdef.check_mark_as_nested;
  4438. begin
  4439. { nested procvars require that nested functions use the Delphi-style
  4440. nested procedure calling convention }
  4441. if (parast.symtablelevel>normal_function_level) and
  4442. (m_nested_procvars in current_settings.modeswitches) then
  4443. include(procoptions,po_delphi_nested_cc);
  4444. end;
  4445. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4446. begin
  4447. if (side in [callerside,callbothsides]) and
  4448. not(has_paraloc_info in [callerside,callbothsides]) then
  4449. begin
  4450. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  4451. if has_paraloc_info in [calleeside,callbothsides] then
  4452. has_paraloc_info:=callbothsides
  4453. else
  4454. has_paraloc_info:=callerside;
  4455. end;
  4456. if (side in [calleeside,callbothsides]) and
  4457. not(has_paraloc_info in [calleeside,callbothsides]) then
  4458. begin
  4459. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  4460. if has_paraloc_info in [callerside,callbothsides] then
  4461. has_paraloc_info:=callbothsides
  4462. else
  4463. has_paraloc_info:=calleeside;
  4464. end;
  4465. end;
  4466. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  4467. var
  4468. i: longint;
  4469. begin
  4470. if (side in [callerside,callbothsides]) and
  4471. (has_paraloc_info in [callerside,callbothsides]) then
  4472. begin
  4473. funcretloc[callerside].done;
  4474. for i:=0 to paras.count-1 do
  4475. tparavarsym(paras[i]).paraloc[callerside].done;
  4476. if has_paraloc_info=callerside then
  4477. has_paraloc_info:=callnoside
  4478. else
  4479. has_paraloc_info:=calleeside;
  4480. end;
  4481. if (side in [calleeside,callbothsides]) and
  4482. (has_paraloc_info in [calleeside,callbothsides]) then
  4483. begin
  4484. funcretloc[calleeside].done;
  4485. for i:=0 to paras.count-1 do
  4486. tparavarsym(paras[i]).paraloc[calleeside].done;
  4487. if has_paraloc_info=calleeside then
  4488. has_paraloc_info:=callnoside
  4489. else
  4490. has_paraloc_info:=callerside;
  4491. end;
  4492. end;
  4493. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4494. var
  4495. p: tparavarsym;
  4496. ploc: PCGParalocation;
  4497. i: longint;
  4498. begin
  4499. result:=false;
  4500. init_paraloc_info(side);
  4501. for i:=0 to parast.SymList.Count-1 do
  4502. if tsym(parast.SymList[i]).typ=paravarsym then
  4503. begin
  4504. p:=tparavarsym(parast.SymList[i]);
  4505. { check if no parameter is located on the stack }
  4506. if is_open_array(p.vardef) or
  4507. is_array_of_const(p.vardef) then
  4508. begin
  4509. result:=true;
  4510. exit;
  4511. end;
  4512. ploc:=p.paraloc[side].location;
  4513. while assigned(ploc) do
  4514. begin
  4515. if (ploc^.loc=LOC_REFERENCE) then
  4516. begin
  4517. result:=true;
  4518. exit
  4519. end;
  4520. ploc:=ploc^.next;
  4521. end;
  4522. end;
  4523. end;
  4524. function tabstractprocdef.is_pushleftright: boolean;
  4525. begin
  4526. result:=false;
  4527. end;
  4528. function tabstractprocdef.address_type: tdef;
  4529. begin
  4530. result:=voidcodepointertype;
  4531. end;
  4532. procedure tabstractprocdef.declared_far;
  4533. begin
  4534. Message1(parser_w_proc_directive_ignored,'FAR');
  4535. end;
  4536. procedure tabstractprocdef.declared_near;
  4537. begin
  4538. Message1(parser_w_proc_directive_ignored,'NEAR');
  4539. end;
  4540. {***************************************************************************
  4541. TPROCDEF
  4542. ***************************************************************************}
  4543. function tprocdef.GetResultName: PShortString;
  4544. begin
  4545. if not assigned(implprocdefinfo) then
  4546. internalerror(2014010301);
  4547. result:=implprocdefinfo^.resultname;
  4548. end;
  4549. procedure tprocdef.SetResultName(AValue: PShortString);
  4550. begin
  4551. if not assigned(implprocdefinfo) then
  4552. internalerror(2014010302);
  4553. implprocdefinfo^.resultname:=AValue;
  4554. end;
  4555. function tprocdef.GetParentFPInitBlock: tnode;
  4556. begin
  4557. if not assigned(implprocdefinfo) then
  4558. internalerror(2014010303);
  4559. result:=implprocdefinfo^.parentfpinitblock;
  4560. end;
  4561. function tprocdef.GetParentFPStruct: tsym;
  4562. begin
  4563. if not assigned(implprocdefinfo) then
  4564. internalerror(2014010304);
  4565. result:=implprocdefinfo^.parentfpstruct;
  4566. end;
  4567. function tprocdef.GetParentFPStructPtrType: tdef;
  4568. begin
  4569. if not assigned(implprocdefinfo) then
  4570. internalerror(2014010305);
  4571. result:=implprocdefinfo^.parentfpstructptrtype;
  4572. end;
  4573. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4574. begin
  4575. if not assigned(implprocdefinfo) then
  4576. internalerror(2014010306);
  4577. implprocdefinfo^.parentfpinitblock:=AValue;
  4578. end;
  4579. function tprocdef.Getprocendtai: tai;
  4580. begin
  4581. if not assigned(implprocdefinfo) then
  4582. internalerror(2014010307);
  4583. result:=implprocdefinfo^.procendtai;
  4584. end;
  4585. function tprocdef.Getprocstarttai: tai;
  4586. begin
  4587. if not assigned(implprocdefinfo) then
  4588. internalerror(2014010308);
  4589. result:=implprocdefinfo^.procstarttai;
  4590. end;
  4591. procedure tprocdef.Setprocendtai(AValue: tai);
  4592. begin
  4593. if not assigned(implprocdefinfo) then
  4594. internalerror(2014010309);
  4595. implprocdefinfo^.procendtai:=AValue;
  4596. end;
  4597. function tprocdef.Getskpara: pointer;
  4598. begin
  4599. if not assigned(implprocdefinfo) then
  4600. internalerror(2014010310);
  4601. result:=implprocdefinfo^.skpara;
  4602. end;
  4603. procedure tprocdef.Setskpara(AValue: pointer);
  4604. begin
  4605. if not assigned(implprocdefinfo) then
  4606. internalerror(2014010311);
  4607. implprocdefinfo^.skpara:=AValue;
  4608. end;
  4609. function tprocdef.Getforwarddef: boolean;
  4610. begin
  4611. if not assigned(implprocdefinfo) then
  4612. internalerror(2014010312);
  4613. result:=implprocdefinfo^.forwarddef;
  4614. end;
  4615. function tprocdef.Gethasforward: boolean;
  4616. begin
  4617. if not assigned(implprocdefinfo) then
  4618. internalerror(2014010313);
  4619. result:=implprocdefinfo^.hasforward;
  4620. end;
  4621. function tprocdef.Getinterfacedef: boolean;
  4622. begin
  4623. if not assigned(implprocdefinfo) then
  4624. internalerror(2014010314);
  4625. result:=implprocdefinfo^.interfacedef;
  4626. end;
  4627. procedure tprocdef.Setforwarddef(AValue: boolean);
  4628. begin
  4629. if not assigned(implprocdefinfo) then
  4630. internalerror(2014010315);
  4631. implprocdefinfo^.forwarddef:=AValue;
  4632. end;
  4633. procedure tprocdef.Sethasforward(AValue: boolean);
  4634. begin
  4635. if not assigned(implprocdefinfo) then
  4636. internalerror(2014010316);
  4637. implprocdefinfo^.hasforward:=AValue;
  4638. end;
  4639. function tprocdef.GetIsEmpty: boolean;
  4640. begin
  4641. result:=pio_empty in implprocoptions;
  4642. end;
  4643. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4644. begin
  4645. if AValue then
  4646. include(implprocoptions,pio_empty)
  4647. else
  4648. include(implprocoptions,pio_empty);
  4649. end;
  4650. function tprocdef.GetHasInliningInfo: boolean;
  4651. begin
  4652. result:=pio_has_inlininginfo in implprocoptions;
  4653. end;
  4654. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4655. begin
  4656. if AValue then
  4657. include(implprocoptions,pio_has_inlininginfo)
  4658. else
  4659. exclude(implprocoptions,pio_has_inlininginfo);
  4660. end;
  4661. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4662. begin
  4663. if not assigned(implprocdefinfo) then
  4664. internalerror(2014010317);
  4665. implprocdefinfo^.interfacedef:=AValue;
  4666. end;
  4667. procedure tprocdef.Setprocstarttai(AValue: tai);
  4668. begin
  4669. if not assigned(implprocdefinfo) then
  4670. internalerror(2014010318);
  4671. implprocdefinfo^.procstarttai:=AValue;
  4672. end;
  4673. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4674. begin
  4675. if not assigned(implprocdefinfo) then
  4676. internalerror(2014010319);
  4677. implprocdefinfo^.parentfpstruct:=AValue;
  4678. end;
  4679. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4680. begin
  4681. if not assigned(implprocdefinfo) then
  4682. internalerror(2014010320);
  4683. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4684. end;
  4685. constructor tprocdef.create(level:byte;doregister:boolean);
  4686. begin
  4687. inherited create(procdef,level,doregister);
  4688. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4689. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4690. {$ifdef symansistr}
  4691. _mangledname:='';
  4692. {$else symansistr}
  4693. _mangledname:=nil;
  4694. {$endif symansistr}
  4695. fileinfo:=current_filepos;
  4696. extnumber:=$ffff;
  4697. aliasnames:=TCmdStrList.create;
  4698. funcretsym:=nil;
  4699. forwarddef:=true;
  4700. interfacedef:=false;
  4701. hasforward:=false;
  4702. struct := nil;
  4703. import_dll:=nil;
  4704. import_name:=nil;
  4705. import_nr:=0;
  4706. inlininginfo:=nil;
  4707. deprecatedmsg:=nil;
  4708. end;
  4709. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4710. var
  4711. i,aliasnamescount,sizeleft : longint;
  4712. level : byte;
  4713. buf : array[0..255] of byte;
  4714. begin
  4715. inherited ppuload(procdef,ppufile);
  4716. {$ifdef symansistr}
  4717. if po_has_mangledname in procoptions then
  4718. _mangledname:=ppufile.getansistring
  4719. else
  4720. _mangledname:='';
  4721. {$else symansistr}
  4722. if po_has_mangledname in procoptions then
  4723. _mangledname:=stringdup(ppufile.getstring)
  4724. else
  4725. _mangledname:=nil;
  4726. {$endif symansistr}
  4727. extnumber:=ppufile.getword;
  4728. level:=ppufile.getbyte;
  4729. ppufile.getderef(structderef);
  4730. ppufile.getderef(procsymderef);
  4731. ppufile.getposinfo(fileinfo);
  4732. visibility:=tvisibility(ppufile.getbyte);
  4733. ppufile.getsmallset(symoptions);
  4734. if sp_has_deprecated_msg in symoptions then
  4735. deprecatedmsg:=stringdup(ppufile.getstring)
  4736. else
  4737. deprecatedmsg:=nil;
  4738. synthetickind:=tsynthetickind(ppufile.getbyte);
  4739. { import stuff }
  4740. if po_has_importdll in procoptions then
  4741. import_dll:=stringdup(ppufile.getstring)
  4742. else
  4743. import_dll:=nil;
  4744. if po_has_importname in procoptions then
  4745. import_name:=stringdup(ppufile.getstring)
  4746. else
  4747. import_name:=nil;
  4748. import_nr:=ppufile.getword;
  4749. if (po_msgint in procoptions) then
  4750. messageinf.i:=ppufile.getlongint;
  4751. if (po_msgstr in procoptions) then
  4752. messageinf.str:=stringdup(ppufile.getstring);
  4753. if (po_dispid in procoptions) then
  4754. dispid:=ppufile.getlongint;
  4755. { inline stuff }
  4756. ppufile.getsmallset(implprocoptions);
  4757. if has_inlininginfo then
  4758. begin
  4759. ppufile.getderef(funcretsymderef);
  4760. new(inlininginfo);
  4761. ppufile.getsmallset(inlininginfo^.flags);
  4762. end
  4763. else
  4764. begin
  4765. inlininginfo:=nil;
  4766. funcretsym:=nil;
  4767. end;
  4768. aliasnames:=TCmdStrList.create;
  4769. { count alias names }
  4770. aliasnamescount:=ppufile.getbyte;
  4771. for i:=1 to aliasnamescount do
  4772. aliasnames.insert(ppufile.getstring);
  4773. { load the token stream containing the declaration }
  4774. sizeleft:=ppufile.getlongint;
  4775. if sizeleft>0 then
  4776. begin
  4777. init_genericdecl;
  4778. while sizeleft>0 do
  4779. begin
  4780. if sizeleft>sizeof(buf) then
  4781. i:=sizeof(buf)
  4782. else
  4783. i:=sizeleft;
  4784. ppufile.getdata(buf,i);
  4785. genericdecltokenbuf.write(buf,i);
  4786. dec(sizeleft,i);
  4787. end;
  4788. end;
  4789. ppuload_platform(ppufile);
  4790. { load para symtable }
  4791. parast:=tparasymtable.create(self,level);
  4792. tparasymtable(parast).ppuload(ppufile);
  4793. { load local symtable }
  4794. if has_inlininginfo then
  4795. begin
  4796. localst:=tlocalsymtable.create(self,level);
  4797. tlocalsymtable(localst).ppuload(ppufile);
  4798. end
  4799. else
  4800. localst:=nil;
  4801. { inline stuff }
  4802. if has_inlininginfo then
  4803. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4804. { default values for no persistent data }
  4805. if (cs_link_deffile in current_settings.globalswitches) and
  4806. (tf_need_export in target_info.flags) and
  4807. (po_exports in procoptions) then
  4808. deffile.AddExport(mangledname);
  4809. { Disable po_has_inlining until the derefimpl is done }
  4810. has_inlininginfo:=false;
  4811. end;
  4812. destructor tprocdef.destroy;
  4813. begin
  4814. aliasnames.free;
  4815. aliasnames:=nil;
  4816. if assigned(localst) and
  4817. (localst.symtabletype<>staticsymtable) then
  4818. begin
  4819. {$ifdef MEMDEBUG}
  4820. memproclocalst.start;
  4821. {$endif MEMDEBUG}
  4822. localst.free;
  4823. localst:=nil;
  4824. {$ifdef MEMDEBUG}
  4825. memproclocalst.start;
  4826. {$endif MEMDEBUG}
  4827. end;
  4828. if assigned(inlininginfo) then
  4829. begin
  4830. {$ifdef MEMDEBUG}
  4831. memprocnodetree.start;
  4832. {$endif MEMDEBUG}
  4833. tnode(inlininginfo^.code).free;
  4834. {$ifdef MEMDEBUG}
  4835. memprocnodetree.start;
  4836. {$endif MEMDEBUG}
  4837. dispose(inlininginfo);
  4838. inlininginfo:=nil;
  4839. end;
  4840. freeimplprocdefinfo;
  4841. genericdecltokenbuf.free;
  4842. genericdecltokenbuf:=nil;
  4843. stringdispose(import_dll);
  4844. stringdispose(import_name);
  4845. stringdispose(deprecatedmsg);
  4846. if (po_msgstr in procoptions) then
  4847. stringdispose(messageinf.str);
  4848. {$ifndef symansistr}
  4849. if assigned(_mangledname) then
  4850. begin
  4851. {$ifdef MEMDEBUG}
  4852. memmanglednames.start;
  4853. {$endif MEMDEBUG}
  4854. stringdispose(_mangledname);
  4855. {$ifdef MEMDEBUG}
  4856. memmanglednames.stop;
  4857. {$endif MEMDEBUG}
  4858. end;
  4859. {$endif symansistr}
  4860. inherited destroy;
  4861. end;
  4862. procedure tprocdef.freeimplprocdefinfo;
  4863. begin
  4864. if assigned(implprocdefinfo) then
  4865. begin
  4866. stringdispose(implprocdefinfo^.resultname);
  4867. freemem(implprocdefinfo);
  4868. implprocdefinfo:=nil;
  4869. end;
  4870. end;
  4871. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4872. var
  4873. oldintfcrc : boolean;
  4874. aliasnamescount,i,sizeleft : longint;
  4875. item : TCmdStrListItem;
  4876. buf : array[0..255] of byte;
  4877. begin
  4878. { released procdef? }
  4879. if not assigned(parast) then
  4880. exit;
  4881. inherited ppuwrite(ppufile);
  4882. {$ifdef symansistr}
  4883. if po_has_mangledname in procoptions then
  4884. ppufile.putansistring(_mangledname);
  4885. {$else symansistr}
  4886. if po_has_mangledname in procoptions then
  4887. ppufile.putstring(_mangledname^);
  4888. {$endif symansistr}
  4889. ppufile.putword(extnumber);
  4890. ppufile.putbyte(parast.symtablelevel);
  4891. ppufile.putderef(structderef);
  4892. ppufile.putderef(procsymderef);
  4893. ppufile.putposinfo(fileinfo);
  4894. ppufile.putbyte(byte(visibility));
  4895. ppufile.putsmallset(symoptions);
  4896. if sp_has_deprecated_msg in symoptions then
  4897. ppufile.putstring(deprecatedmsg^);
  4898. ppufile.putbyte(byte(synthetickind));
  4899. { import }
  4900. if po_has_importdll in procoptions then
  4901. ppufile.putstring(import_dll^);
  4902. if po_has_importname in procoptions then
  4903. ppufile.putstring(import_name^);
  4904. ppufile.putword(import_nr);
  4905. if (po_msgint in procoptions) then
  4906. ppufile.putlongint(messageinf.i);
  4907. if (po_msgstr in procoptions) then
  4908. ppufile.putstring(messageinf.str^);
  4909. if (po_dispid in procoptions) then
  4910. ppufile.putlongint(dispid);
  4911. { inline stuff }
  4912. oldintfcrc:=ppufile.do_crc;
  4913. ppufile.do_crc:=false;
  4914. ppufile.putsmallset(implprocoptions);
  4915. if has_inlininginfo then
  4916. begin
  4917. ppufile.putderef(funcretsymderef);
  4918. ppufile.putsmallset(inlininginfo^.flags);
  4919. end;
  4920. { count alias names }
  4921. aliasnamescount:=0;
  4922. item:=TCmdStrListItem(aliasnames.first);
  4923. while assigned(item) do
  4924. begin
  4925. inc(aliasnamescount);
  4926. item:=TCmdStrListItem(item.next);
  4927. end;
  4928. if aliasnamescount>255 then
  4929. internalerror(200711021);
  4930. ppufile.putbyte(aliasnamescount);
  4931. item:=TCmdStrListItem(aliasnames.first);
  4932. while assigned(item) do
  4933. begin
  4934. ppufile.putstring(item.str);
  4935. item:=TCmdStrListItem(item.next);
  4936. end;
  4937. ppufile.do_crc:=oldintfcrc;
  4938. { generic tokens for the declaration }
  4939. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  4940. begin
  4941. sizeleft:=genericdecltokenbuf.size;
  4942. genericdecltokenbuf.seek(0);
  4943. ppufile.putlongint(sizeleft);
  4944. while sizeleft>0 do
  4945. begin
  4946. if sizeleft>sizeof(buf) then
  4947. i:=sizeof(buf)
  4948. else
  4949. i:=sizeleft;
  4950. genericdecltokenbuf.read(buf,i);
  4951. ppufile.putdata(buf,i);
  4952. dec(sizeleft,i);
  4953. end;
  4954. end
  4955. else
  4956. ppufile.putlongint(0);
  4957. { write this entry }
  4958. writeentry(ppufile,ibprocdef);
  4959. { Save the para symtable, this is taken from the interface }
  4960. tparasymtable(parast).ppuwrite(ppufile);
  4961. { save localsymtable for inline procedures or when local
  4962. browser info is requested, this has no influence on the crc }
  4963. if has_inlininginfo then
  4964. begin
  4965. oldintfcrc:=ppufile.do_crc;
  4966. ppufile.do_crc:=false;
  4967. tlocalsymtable(localst).ppuwrite(ppufile);
  4968. ppufile.do_crc:=oldintfcrc;
  4969. end;
  4970. { node tree for inlining }
  4971. oldintfcrc:=ppufile.do_crc;
  4972. ppufile.do_crc:=false;
  4973. if has_inlininginfo then
  4974. ppuwritenodetree(ppufile,inlininginfo^.code);
  4975. ppufile.do_crc:=oldintfcrc;
  4976. end;
  4977. function tprocdef.fullprocname(showhidden:boolean):string;
  4978. var
  4979. pno: tprocnameoptions;
  4980. begin
  4981. pno:=[];
  4982. if showhidden then
  4983. include(pno,pno_showhidden);
  4984. result:=customprocname(pno);
  4985. end;
  4986. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4987. var
  4988. s, rn : ansistring;
  4989. t : ttoken;
  4990. begin
  4991. {$ifdef EXTDEBUG}
  4992. include(pno,pno_showhidden);
  4993. {$endif EXTDEBUG}
  4994. s:='';
  4995. if proctypeoption=potype_operator then
  4996. begin
  4997. for t:=NOTOKEN to last_overloaded do
  4998. if procsym.realname='$'+overloaded_names[t] then
  4999. begin
  5000. s:='operator ';
  5001. if (pno_ownername in pno) and
  5002. assigned(struct) then
  5003. s:=s+struct.RttiName+'.';
  5004. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  5005. break;
  5006. end;
  5007. end
  5008. else
  5009. begin
  5010. if (po_classmethod in procoptions) and
  5011. not(pno_noclassmarker in pno) then
  5012. s:='class ';
  5013. case proctypeoption of
  5014. potype_constructor,
  5015. potype_class_constructor:
  5016. s:=s+'constructor ';
  5017. potype_class_destructor,
  5018. potype_destructor:
  5019. s:=s+'destructor ';
  5020. else
  5021. if (pno_proctypeoption in pno) then
  5022. begin
  5023. if assigned(returndef) and
  5024. not(is_void(returndef)) then
  5025. s:=s+'function '
  5026. else
  5027. s:=s+'procedure ';
  5028. end;
  5029. end;
  5030. if (pno_ownername in pno) and
  5031. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  5032. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  5033. rn:=procsym.realname;
  5034. if (pno_noleadingdollar in pno) and
  5035. (rn[1]='$') then
  5036. delete(rn,1,1);
  5037. s:=s+rn+typename_paras(pno);
  5038. end;
  5039. if not(proctypeoption in [potype_constructor,potype_destructor,
  5040. potype_class_constructor,potype_class_destructor]) and
  5041. assigned(returndef) and
  5042. not(is_void(returndef)) then
  5043. s:=s+':'+returndef.GetTypeName;
  5044. if owner.symtabletype=localsymtable then
  5045. s:=s+' is nested'
  5046. else if po_is_block in procoptions then
  5047. s:=s+' is block';
  5048. s:=s+';';
  5049. { forced calling convention? }
  5050. if (po_hascallingconvention in procoptions) then
  5051. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  5052. if (po_staticmethod in procoptions) and
  5053. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  5054. s:=s+' Static;';
  5055. if pno_mangledname in pno then
  5056. s:=s+' -- mangled name: '+mangledname;
  5057. customprocname:=s;
  5058. end;
  5059. function tprocdef.is_methodpointer:boolean;
  5060. begin
  5061. { don't check assigned(_class), that's also the case for nested
  5062. procedures inside methods }
  5063. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  5064. end;
  5065. function tprocdef.is_addressonly:boolean;
  5066. begin
  5067. result:=assigned(owner) and
  5068. not is_methodpointer and
  5069. (not(m_nested_procvars in current_settings.modeswitches) or
  5070. not is_nested_pd(self));
  5071. end;
  5072. procedure tprocdef.make_external;
  5073. begin
  5074. include(procoptions,po_external);
  5075. forwarddef:=false;
  5076. end;
  5077. procedure tprocdef.init_genericdecl;
  5078. begin
  5079. if assigned(genericdecltokenbuf) then
  5080. internalerror(2015061901);
  5081. genericdecltokenbuf:=tdynamicarray.create(256);
  5082. end;
  5083. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  5084. begin
  5085. case t of
  5086. gs_local :
  5087. GetSymtable:=localst;
  5088. gs_para :
  5089. GetSymtable:=parast;
  5090. else
  5091. GetSymtable:=nil;
  5092. end;
  5093. end;
  5094. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  5095. var
  5096. j : longint;
  5097. begin
  5098. result:=inherited getcopyas(newtyp,copytyp);
  5099. if newtyp=procvardef then
  5100. begin
  5101. { create new paralist }
  5102. tprocvardef(result).calcparas;
  5103. exit;
  5104. end;
  5105. { don't copy mangled name, can be different }
  5106. tprocdef(result).messageinf:=messageinf;
  5107. tprocdef(result).dispid:=dispid;
  5108. if po_msgstr in procoptions then
  5109. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  5110. tprocdef(result).symoptions:=symoptions;
  5111. if assigned(deprecatedmsg) then
  5112. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  5113. { will have to be associated with appropriate procsym }
  5114. tprocdef(result).procsym:=nil;
  5115. { don't create aliases for bare copies, nor copy the funcretsym as
  5116. the function result parameter will be inserted again if necessary
  5117. (e.g. if the calling convention is changed) }
  5118. if copytyp<>pc_bareproc then
  5119. begin
  5120. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  5121. if assigned(funcretsym) then
  5122. begin
  5123. if funcretsym.owner=parast then
  5124. begin
  5125. j:=parast.symlist.indexof(funcretsym);
  5126. if j<0 then
  5127. internalerror(2011040606);
  5128. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  5129. end
  5130. else if funcretsym.owner=localst then
  5131. begin
  5132. { nothing to do, will be inserted for the new procdef while
  5133. parsing its body (by pdecsub.insert_funcret_local) }
  5134. end
  5135. else
  5136. internalerror(2011040605);
  5137. end;
  5138. end;
  5139. { will have to be associated with a new struct }
  5140. tprocdef(result).struct:=nil;
  5141. if assigned(implprocdefinfo) then
  5142. begin
  5143. if assigned(resultname) then
  5144. tprocdef(result).resultname:=stringdup(resultname^);
  5145. end;
  5146. if assigned(import_dll) then
  5147. tprocdef(result).import_dll:=stringdup(import_dll^);
  5148. if assigned(import_name) then
  5149. tprocdef(result).import_name:=stringdup(import_name^);
  5150. tprocdef(result).import_nr:=import_nr;
  5151. tprocdef(result).extnumber:=$ffff;
  5152. tprocdef(result).visibility:=visibility;
  5153. tprocdef(result).synthetickind:=synthetickind;
  5154. { we need a separate implementation for the copied def }
  5155. tprocdef(result).forwarddef:=true;
  5156. tprocdef(result).interfacedef:=true;
  5157. { create new paralist }
  5158. tprocdef(result).calcparas;
  5159. end;
  5160. function tprocdef.getcopy: tstoreddef;
  5161. begin
  5162. result:=getcopyas(procdef,pc_normal);
  5163. end;
  5164. procedure tprocdef.buildderef;
  5165. begin
  5166. inherited buildderef;
  5167. structderef.build(struct);
  5168. { procsym that originaly defined this definition, should be in the
  5169. same symtable }
  5170. procsymderef.build(procsym);
  5171. end;
  5172. procedure tprocdef.buildderefimpl;
  5173. begin
  5174. inherited buildderefimpl;
  5175. { inline tree }
  5176. if has_inlininginfo then
  5177. begin
  5178. { Localst is not available for main/unit init }
  5179. if assigned(localst) then
  5180. begin
  5181. tlocalsymtable(localst).buildderef;
  5182. tlocalsymtable(localst).buildderefimpl;
  5183. end;
  5184. funcretsymderef.build(funcretsym);
  5185. inlininginfo^.code.buildderefimpl;
  5186. end;
  5187. end;
  5188. procedure tprocdef.deref;
  5189. begin
  5190. inherited deref;
  5191. struct:=tabstractrecorddef(structderef.resolve);
  5192. { procsym that originaly defined this definition, should be in the
  5193. same symtable }
  5194. procsym:=tprocsym(procsymderef.resolve);
  5195. end;
  5196. procedure tprocdef.derefimpl;
  5197. begin
  5198. { Enable has_inlininginfo when the inlininginfo
  5199. structure is available. The has_inlininginfo was disabled
  5200. after the load, since the data was invalid }
  5201. if assigned(inlininginfo) then
  5202. has_inlininginfo:=true;
  5203. { Inline }
  5204. if has_inlininginfo then
  5205. begin
  5206. { Locals }
  5207. if assigned(localst) then
  5208. begin
  5209. tlocalsymtable(localst).deref(false);
  5210. tlocalsymtable(localst).derefimpl(false);
  5211. end;
  5212. inlininginfo^.code.derefimpl;
  5213. { funcretsym, this is always located in the localst }
  5214. funcretsym:=tsym(funcretsymderef.resolve);
  5215. end
  5216. else
  5217. begin
  5218. { safety }
  5219. { Not safe! A unit may be reresolved after its interface has been
  5220. parsed but before its implementation has been parsed, and in that
  5221. case the funcretsym is still required!
  5222. funcretsym:=nil; }
  5223. end;
  5224. end;
  5225. function tprocdef.GetTypeName : string;
  5226. begin
  5227. GetTypeName := FullProcName(false);
  5228. end;
  5229. function tprocdef.mangledname : TSymStr;
  5230. begin
  5231. {$ifdef symansistr}
  5232. if _mangledname='' then
  5233. begin
  5234. result:=defaultmangledname;
  5235. _mangledname:=result;
  5236. end
  5237. else
  5238. result:=_mangledname;
  5239. {$else symansistr}
  5240. if not assigned(_mangledname) then
  5241. begin
  5242. result:=defaultmangledname;
  5243. _mangledname:=stringdup(mangledname);
  5244. end
  5245. else
  5246. result:=_mangledname^;
  5247. {$endif symansistr}
  5248. end;
  5249. function tprocdef.defaultmangledname: TSymStr;
  5250. begin
  5251. { we need to use the symtable where the procsym is inserted,
  5252. because that is visible to the world }
  5253. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  5254. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  5255. end;
  5256. function tprocdef.cplusplusmangledname : TSymStr;
  5257. function getcppparaname(p : tdef) : TSymStr;
  5258. const
  5259. {$ifdef NAMEMANGLING_GCC2}
  5260. ordtype2str : array[tordtype] of string[2] = (
  5261. '',
  5262. 'Uc','Us','Ui','Us','',
  5263. 'Sc','s','i','x','',
  5264. 'b','b','b','b','b',
  5265. 'c','w','x');
  5266. {$else NAMEMANGLING_GCC2}
  5267. ordtype2str : array[tordtype] of string[1] = (
  5268. 'v',
  5269. 'h','t','j','y','',
  5270. 'a','s','i','x','',
  5271. 'b','b','b','b',
  5272. 'b','b','b','b',
  5273. 'c','w','x');
  5274. floattype2str : array[tfloattype] of string[1] = (
  5275. 'f','d','e','e',
  5276. 'd','d','g');
  5277. {$endif NAMEMANGLING_GCC2}
  5278. var
  5279. s : TSymStr;
  5280. begin
  5281. case p.typ of
  5282. orddef:
  5283. s:=ordtype2str[torddef(p).ordtype];
  5284. pointerdef:
  5285. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  5286. {$ifndef NAMEMANGLING_GCC2}
  5287. floatdef:
  5288. s:=floattype2str[tfloatdef(p).floattype];
  5289. {$endif NAMEMANGLING_GCC2}
  5290. else
  5291. internalerror(2103001);
  5292. end;
  5293. getcppparaname:=s;
  5294. end;
  5295. var
  5296. s,s2 : TSymStr;
  5297. hp : TParavarsym;
  5298. i : integer;
  5299. begin
  5300. {$ifdef NAMEMANGLING_GCC2}
  5301. { outdated gcc 2.x name mangling scheme }
  5302. s := procsym.realname;
  5303. if procsym.owner.symtabletype=ObjectSymtable then
  5304. begin
  5305. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  5306. case proctypeoption of
  5307. potype_destructor:
  5308. s:='_$_'+tostr(length(s2))+s2;
  5309. potype_constructor:
  5310. s:='___'+tostr(length(s2))+s2;
  5311. else
  5312. s:='_'+s+'__'+tostr(length(s2))+s2;
  5313. end;
  5314. end
  5315. else s:=s+'__';
  5316. s:=s+'F';
  5317. { concat modifiers }
  5318. { !!!!! }
  5319. { now we handle the parameters }
  5320. if maxparacount>0 then
  5321. begin
  5322. for i:=0 to paras.count-1 do
  5323. begin
  5324. hp:=tparavarsym(paras[i]);
  5325. { no hidden parameters form part of a C++ mangled name:
  5326. a) self is not included
  5327. b) there are no "high" or other hidden parameters
  5328. }
  5329. if vo_is_hidden_para in hp.varoptions then
  5330. continue;
  5331. s2:=getcppparaname(hp.vardef);
  5332. if hp.varspez in [vs_var,vs_out] then
  5333. s2:='R'+s2;
  5334. s:=s+s2;
  5335. end;
  5336. end
  5337. else
  5338. s:=s+'v';
  5339. cplusplusmangledname:=s;
  5340. {$else NAMEMANGLING_GCC2}
  5341. { gcc 3.x and 4.x name mangling scheme }
  5342. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  5343. if procsym.owner.symtabletype=ObjectSymtable then
  5344. begin
  5345. s:='_ZN';
  5346. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  5347. s:=s+tostr(length(s2))+s2;
  5348. case proctypeoption of
  5349. potype_constructor:
  5350. s:=s+'C1';
  5351. potype_destructor:
  5352. s:=s+'D1';
  5353. else
  5354. s:=s+tostr(length(procsym.realname))+procsym.realname;
  5355. end;
  5356. s:=s+'E';
  5357. end
  5358. else
  5359. s:=procsym.realname;
  5360. { now we handle the parameters }
  5361. if maxparacount>0 then
  5362. begin
  5363. for i:=0 to paras.count-1 do
  5364. begin
  5365. hp:=tparavarsym(paras[i]);
  5366. { no hidden parameters form part of a C++ mangled name:
  5367. a) self is not included
  5368. b) there are no "high" or other hidden parameters
  5369. }
  5370. if vo_is_hidden_para in hp.varoptions then
  5371. continue;
  5372. s2:=getcppparaname(hp.vardef);
  5373. if hp.varspez in [vs_var,vs_out] then
  5374. s2:='R'+s2;
  5375. s:=s+s2;
  5376. end;
  5377. end
  5378. else
  5379. s:=s+'v';
  5380. cplusplusmangledname:=s;
  5381. {$endif NAMEMANGLING_GCC2}
  5382. end;
  5383. function tprocdef.objcmangledname : TSymStr;
  5384. var
  5385. manglednamelen: longint;
  5386. iscatmethod : boolean;
  5387. begin
  5388. if not (po_msgstr in procoptions) then
  5389. internalerror(2009030901);
  5390. { we may very well need longer strings to handle these... }
  5391. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  5392. length('+"[ ]"')+length(messageinf.str^);
  5393. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  5394. if (iscatmethod) then
  5395. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  5396. if manglednamelen>255 then
  5397. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  5398. if not(po_classmethod in procoptions) then
  5399. result:='"-['
  5400. else
  5401. result:='"+[';
  5402. { quotes are necessary because the +/- otherwise confuse the assembler
  5403. into expecting a number
  5404. }
  5405. if iscatmethod then
  5406. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5407. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5408. if iscatmethod then
  5409. result:=result+')';
  5410. result:=result+' '+messageinf.str^+']"';
  5411. end;
  5412. procedure tprocdef.setmangledname(const s : TSymStr);
  5413. begin
  5414. { This is not allowed anymore, the forward declaration
  5415. already needs to create the correct mangledname, no changes
  5416. afterwards are allowed (PFV) }
  5417. { Exception: interface definitions in mode macpas, since in that }
  5418. { case no reference to the old name can exist yet (JM) }
  5419. {$ifdef symansistr}
  5420. if _mangledname<>'' then
  5421. if ((m_mac in current_settings.modeswitches) and
  5422. (interfacedef)) then
  5423. _mangledname:=''
  5424. else
  5425. internalerror(200411171);
  5426. {$else symansistr}
  5427. if assigned(_mangledname) then
  5428. if ((m_mac in current_settings.modeswitches) and
  5429. (interfacedef)) then
  5430. stringdispose(_mangledname)
  5431. else
  5432. internalerror(200411171);
  5433. {$endif symansistr}
  5434. {$ifdef jvm}
  5435. { this routine can be called for compilerproces. can't set mangled
  5436. name since it must be calculated, but it uses import_name when set
  5437. -> set that one }
  5438. import_name:=stringdup(s);
  5439. include(procoptions,po_has_importname);
  5440. include(procoptions,po_has_mangledname);
  5441. {$else}
  5442. {$ifdef symansistr}
  5443. _mangledname:=s;
  5444. {$else symansistr}
  5445. _mangledname:=stringdup(s);
  5446. {$endif symansistr}
  5447. {$endif jvm}
  5448. include(procoptions,po_has_mangledname);
  5449. end;
  5450. {***************************************************************************
  5451. TPROCVARDEF
  5452. ***************************************************************************}
  5453. constructor tprocvardef.create(level:byte);
  5454. begin
  5455. inherited create(procvardef,level,true);
  5456. end;
  5457. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef): tprocvardef;
  5458. var
  5459. res: PHashSetItem;
  5460. oldsymtablestack: tsymtablestack;
  5461. begin
  5462. if not assigned(current_module) then
  5463. internalerror(2011081301);
  5464. res:=current_module.procaddrdefs.FindOrAdd(@def,sizeof(def));
  5465. if not assigned(res^.Data) then
  5466. begin
  5467. { since these pointerdefs can be reused anywhere in the current
  5468. unit, add them to the global/staticsymtable (or local symtable
  5469. if they're a local def, because otherwise they'll be saved
  5470. to the ppu referencing a local symtable entry that doesn't
  5471. exist in the ppu) }
  5472. oldsymtablestack:=symtablestack;
  5473. { do not simply push/pop current_module.localsymtable, because
  5474. that can have side-effects (e.g., it removes helpers) }
  5475. symtablestack:=nil;
  5476. result:=tprocvardef(def.getcopyas(procvardef,pc_address_only));
  5477. setup_reusable_def(def,result,res,oldsymtablestack);
  5478. { res^.Data may still be nil -> don't overwrite result }
  5479. exit;
  5480. end;
  5481. result:=tprocvardef(res^.Data);
  5482. end;
  5483. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  5484. begin
  5485. result:=getreusableprocaddr(def);
  5486. if not result.is_registered then
  5487. include(result.defoptions,df_not_registered_no_free);
  5488. end;
  5489. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5490. begin
  5491. inherited ppuload(procvardef,ppufile);
  5492. { load para symtable }
  5493. parast:=tparasymtable.create(self,ppufile.getbyte);
  5494. ppuload_platform(ppufile);
  5495. tparasymtable(parast).ppuload(ppufile);
  5496. end;
  5497. function tprocvardef.getcopy : tstoreddef;
  5498. var
  5499. i : tcallercallee;
  5500. j : longint;
  5501. begin
  5502. result:=cprocvardef.create(parast.symtablelevel);
  5503. tprocvardef(result).returndef:=returndef;
  5504. tprocvardef(result).returndefderef:=returndefderef;
  5505. tprocvardef(result).parast:=parast.getcopy;
  5506. tprocvardef(result).savesize:=savesize;
  5507. { create paralist copy }
  5508. calcparas;
  5509. tprocvardef(result).paras:=tparalist.create(false);
  5510. tprocvardef(result).paras.count:=paras.count;
  5511. for j:=0 to paras.count-1 do
  5512. tprocvardef(result).paras[j]:=paras[j];
  5513. tprocvardef(result).proctypeoption:=proctypeoption;
  5514. tprocvardef(result).proccalloption:=proccalloption;
  5515. tprocvardef(result).procoptions:=procoptions;
  5516. tprocvardef(result).callerargareasize:=callerargareasize;
  5517. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5518. tprocvardef(result).maxparacount:=maxparacount;
  5519. tprocvardef(result).minparacount:=minparacount;
  5520. for i:=low(funcretloc) to high(funcretloc) do
  5521. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5522. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5523. {$ifdef m68k}
  5524. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5525. {$endif}
  5526. end;
  5527. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5528. begin
  5529. inherited ppuwrite(ppufile);
  5530. { Save the para symtable level (necessary to distinguish nested
  5531. procvars) }
  5532. ppufile.putbyte(parast.symtablelevel);
  5533. { Write this entry }
  5534. writeentry(ppufile,ibprocvardef);
  5535. { Save the para symtable, this is taken from the interface }
  5536. tparasymtable(parast).ppuwrite(ppufile);
  5537. end;
  5538. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5539. begin
  5540. case t of
  5541. gs_para :
  5542. GetSymtable:=parast;
  5543. else
  5544. GetSymtable:=nil;
  5545. end;
  5546. end;
  5547. function tprocvardef.size : asizeint;
  5548. begin
  5549. if ((po_methodpointer in procoptions) or
  5550. is_nested_pd(self)) and
  5551. not(po_addressonly in procoptions) then
  5552. begin
  5553. if is_nested_pd(self) then
  5554. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5555. else
  5556. size:=voidcodepointertype.size+voidpointertype.size;
  5557. end
  5558. else
  5559. size:=voidcodepointertype.size;
  5560. end;
  5561. function tprocvardef.is_methodpointer:boolean;
  5562. begin
  5563. result:=(po_methodpointer in procoptions);
  5564. end;
  5565. function tprocvardef.is_addressonly:boolean;
  5566. begin
  5567. result:=(not(po_methodpointer in procoptions) and
  5568. not is_nested_pd(self)) or
  5569. (po_addressonly in procoptions);
  5570. end;
  5571. function tprocvardef.getmangledparaname:TSymStr;
  5572. begin
  5573. if not(po_methodpointer in procoptions) then
  5574. if not is_nested_pd(self) then
  5575. result:='procvar'
  5576. else
  5577. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  5578. having not a type name or not an unique one, see webtbs/tw27515.pp
  5579. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  5580. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  5581. else
  5582. result:='procvarofobj'
  5583. end;
  5584. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  5585. begin
  5586. result:=inherited;
  5587. tabstractprocdef(result).calcparas;
  5588. end;
  5589. function tprocvardef.is_publishable : boolean;
  5590. begin
  5591. is_publishable:=(po_methodpointer in procoptions);
  5592. end;
  5593. function tprocvardef.GetTypeName : string;
  5594. var
  5595. s: string;
  5596. pno : tprocnameoptions;
  5597. begin
  5598. {$ifdef EXTDEBUG}
  5599. pno:=[pno_showhidden];
  5600. {$else EXTDEBUG}
  5601. pno:=[];
  5602. {$endif EXTDEBUG}
  5603. s:='<';
  5604. if po_classmethod in procoptions then
  5605. s := s+'class method type of'
  5606. else
  5607. if po_addressonly in procoptions then
  5608. s := s+'address of'
  5609. else
  5610. s := s+'procedure variable type of';
  5611. if assigned(returndef) and
  5612. (returndef<>voidtype) then
  5613. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5614. else
  5615. s:=s+' procedure'+typename_paras(pno);
  5616. if po_methodpointer in procoptions then
  5617. s := s+' of object';
  5618. if is_nested_pd(self) then
  5619. s := s+' is nested';
  5620. { calling convention doesn't matter for blocks }
  5621. if po_is_block in procoptions then
  5622. GetTypeName := s+' is block;'
  5623. else
  5624. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5625. end;
  5626. {***************************************************************************
  5627. TOBJECTDEF
  5628. ***************************************************************************}
  5629. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  5630. begin
  5631. inherited create(n,objectdef,doregister);
  5632. fcurrent_dispid:=0;
  5633. objecttype:=ot;
  5634. childof:=nil;
  5635. if objecttype=odt_helper then
  5636. owner.includeoption(sto_has_helper);
  5637. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  5638. current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  5639. { create space for vmt !! }
  5640. vmtentries:=TFPList.Create;
  5641. set_parent(c);
  5642. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5643. prepareguid;
  5644. { setup implemented interfaces }
  5645. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5646. ImplementedInterfaces:=TFPObjectList.Create(true)
  5647. else
  5648. ImplementedInterfaces:=nil;
  5649. writing_class_record_dbginfo:=false;
  5650. end;
  5651. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5652. var
  5653. i,
  5654. implintfcount : longint;
  5655. d, getterd : tderef;
  5656. ImplIntf : TImplementedInterface;
  5657. vmtentry : pvmtentry;
  5658. begin
  5659. inherited ppuload(objectdef,ppufile);
  5660. objecttype:=tobjecttyp(ppufile.getbyte);
  5661. objextname:=stringdup(ppufile.getstring);
  5662. { only used for external Objective-C classes/protocols }
  5663. if (objextname^='') then
  5664. stringdispose(objextname);
  5665. symtable:=tObjectSymtable.create(self,objrealname^,0,0,0);
  5666. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5667. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5668. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5669. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5670. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  5671. ppufile.getderef(vmt_fieldderef);
  5672. ppufile.getderef(childofderef);
  5673. { load guid }
  5674. iidstr:=nil;
  5675. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5676. begin
  5677. new(iidguid);
  5678. ppufile.getguid(iidguid^);
  5679. iidstr:=stringdup(ppufile.getstring);
  5680. end;
  5681. abstractcnt:=ppufile.getlongint;
  5682. if objecttype=odt_helper then
  5683. ppufile.getderef(extendeddefderef);
  5684. vmtentries:=TFPList.Create;
  5685. vmtentries.count:=ppufile.getlongint;
  5686. for i:=0 to vmtentries.count-1 do
  5687. begin
  5688. ppufile.getderef(d);
  5689. new(vmtentry);
  5690. vmtentry^.procdef:=nil;
  5691. vmtentry^.procdefderef:=d;
  5692. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5693. vmtentries[i]:=vmtentry;
  5694. end;
  5695. { load implemented interfaces }
  5696. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5697. begin
  5698. ImplementedInterfaces:=TFPObjectList.Create(true);
  5699. implintfcount:=ppufile.getlongint;
  5700. for i:=0 to implintfcount-1 do
  5701. begin
  5702. ppufile.getderef(d);
  5703. ppufile.getderef(getterd);
  5704. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5705. ImplIntf.IOffset:=ppufile.getlongint;
  5706. byte(ImplIntf.IType):=ppufile.getbyte;
  5707. ImplementedInterfaces.Add(ImplIntf);
  5708. end;
  5709. end
  5710. else
  5711. ImplementedInterfaces:=nil;
  5712. if df_copied_def in defoptions then
  5713. begin
  5714. ppufile.getderef(cloneddefderef);
  5715. ppuload_platform(ppufile);
  5716. end
  5717. else
  5718. begin
  5719. ppuload_platform(ppufile);
  5720. tObjectSymtable(symtable).ppuload(ppufile);
  5721. end;
  5722. { handles the predefined class tobject }
  5723. { the last TOBJECT which is loaded gets }
  5724. { it ! }
  5725. if (childof=nil) and
  5726. (objecttype in [odt_class,odt_javaclass]) and
  5727. (objname^='TOBJECT') then
  5728. class_tobject:=self;
  5729. if (childof=nil) and
  5730. (objecttype=odt_interfacecom) then
  5731. if (objname^='IUNKNOWN') then
  5732. interface_iunknown:=self
  5733. else
  5734. if (objname^='IDISPATCH') then
  5735. interface_idispatch:=self;
  5736. if (childof=nil) and
  5737. (objecttype=odt_objcclass) and
  5738. (objname^='PROTOCOL') then
  5739. objc_protocoltype:=self;
  5740. if (objecttype=odt_javaclass) and
  5741. not(oo_is_formal in objectoptions) then
  5742. begin
  5743. if (objname^='JLOBJECT') then
  5744. java_jlobject:=self
  5745. else if (objname^='JLTHROWABLE') then
  5746. java_jlthrowable:=self
  5747. else if (objname^='FPCBASERECORDTYPE') then
  5748. java_fpcbaserecordtype:=self
  5749. else if (objname^='JLSTRING') then
  5750. java_jlstring:=self
  5751. else if (objname^='ANSISTRINGCLASS') then
  5752. java_ansistring:=self
  5753. else if (objname^='SHORTSTRINGCLASS') then
  5754. java_shortstring:=self
  5755. else if (objname^='JLENUM') then
  5756. java_jlenum:=self
  5757. else if (objname^='JUENUMSET') then
  5758. java_juenumset:=self
  5759. else if (objname^='FPCBITSET') then
  5760. java_jubitset:=self
  5761. else if (objname^='FPCBASEPROCVARTYPE') then
  5762. java_procvarbase:=self;
  5763. end;
  5764. writing_class_record_dbginfo:=false;
  5765. end;
  5766. destructor tobjectdef.destroy;
  5767. begin
  5768. if assigned(symtable) then
  5769. begin
  5770. symtable.free;
  5771. symtable:=nil;
  5772. end;
  5773. stringdispose(objextname);
  5774. stringdispose(iidstr);
  5775. if assigned(ImplementedInterfaces) then
  5776. begin
  5777. ImplementedInterfaces.free;
  5778. ImplementedInterfaces:=nil;
  5779. end;
  5780. if assigned(iidguid) then
  5781. begin
  5782. dispose(iidguid);
  5783. iidguid:=nil;
  5784. end;
  5785. if assigned(vmtentries) then
  5786. begin
  5787. resetvmtentries;
  5788. vmtentries.free;
  5789. vmtentries:=nil;
  5790. end;
  5791. if assigned(vmcallstaticinfo) then
  5792. begin
  5793. freemem(vmcallstaticinfo);
  5794. vmcallstaticinfo:=nil;
  5795. end;
  5796. inherited destroy;
  5797. end;
  5798. function tobjectdef.getcopy : tstoreddef;
  5799. var
  5800. i : longint;
  5801. begin
  5802. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  5803. { the constructor allocates a symtable which we release to avoid memory leaks }
  5804. tobjectdef(result).symtable.free;
  5805. tobjectdef(result).symtable:=symtable.getcopy;
  5806. if assigned(objextname) then
  5807. tobjectdef(result).objextname:=stringdup(objextname^);
  5808. if assigned(import_lib) then
  5809. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5810. tobjectdef(result).objectoptions:=objectoptions;
  5811. include(tobjectdef(result).defoptions,df_copied_def);
  5812. tobjectdef(result).extendeddef:=extendeddef;
  5813. if assigned(tcinitcode) then
  5814. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5815. tobjectdef(result).vmt_field:=vmt_field;
  5816. if assigned(iidguid) then
  5817. begin
  5818. new(tobjectdef(result).iidguid);
  5819. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5820. end;
  5821. if assigned(iidstr) then
  5822. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5823. tobjectdef(result).abstractcnt:=abstractcnt;
  5824. if assigned(ImplementedInterfaces) then
  5825. begin
  5826. for i:=0 to ImplementedInterfaces.count-1 do
  5827. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5828. end;
  5829. if assigned(vmtentries) then
  5830. begin
  5831. tobjectdef(result).vmtentries:=TFPList.Create;
  5832. tobjectdef(result).copyvmtentries(self);
  5833. end;
  5834. end;
  5835. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5836. var
  5837. i : longint;
  5838. vmtentry : pvmtentry;
  5839. ImplIntf : TImplementedInterface;
  5840. old_do_indirect_crc: boolean;
  5841. begin
  5842. { if class1 in unit A changes, and class2 in unit B inherits from it
  5843. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5844. However, if there is also a class3 in unit C that only depends on
  5845. unit B, then unit C will not be recompiled because nothing changed
  5846. to the interface of unit B. Nevertheless, unit C can indirectly
  5847. depend on unit A via derefs, and these must be updated -> the
  5848. indirect crc keeps track of such changes. }
  5849. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5850. ppufile.do_indirect_crc:=true;
  5851. inherited ppuwrite(ppufile);
  5852. ppufile.putbyte(byte(objecttype));
  5853. if assigned(objextname) then
  5854. ppufile.putstring(objextname^)
  5855. else
  5856. ppufile.putstring('');
  5857. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5858. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5859. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5860. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5861. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  5862. ppufile.putderef(vmt_fieldderef);
  5863. ppufile.putderef(childofderef);
  5864. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5865. begin
  5866. ppufile.putguid(iidguid^);
  5867. ppufile.putstring(iidstr^);
  5868. end;
  5869. ppufile.putlongint(abstractcnt);
  5870. if objecttype=odt_helper then
  5871. ppufile.putderef(extendeddefderef);
  5872. ppufile.putlongint(vmtentries.count);
  5873. for i:=0 to vmtentries.count-1 do
  5874. begin
  5875. vmtentry:=pvmtentry(vmtentries[i]);
  5876. ppufile.putderef(vmtentry^.procdefderef);
  5877. ppufile.putbyte(byte(vmtentry^.visibility));
  5878. end;
  5879. if assigned(ImplementedInterfaces) then
  5880. begin
  5881. ppufile.putlongint(ImplementedInterfaces.Count);
  5882. for i:=0 to ImplementedInterfaces.Count-1 do
  5883. begin
  5884. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5885. ppufile.putderef(ImplIntf.intfdefderef);
  5886. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5887. ppufile.putlongint(ImplIntf.Ioffset);
  5888. ppufile.putbyte(byte(ImplIntf.IType));
  5889. end;
  5890. end;
  5891. if df_copied_def in defoptions then
  5892. ppufile.putderef(cloneddefderef);
  5893. writeentry(ppufile,ibobjectdef);
  5894. if not(df_copied_def in defoptions) then
  5895. tObjectSymtable(symtable).ppuwrite(ppufile);
  5896. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5897. end;
  5898. function tobjectdef.GetTypeName:string;
  5899. begin
  5900. { in this case we will go in endless recursion, because then }
  5901. { there is no tsym associated yet with the def. It can occur }
  5902. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5903. { instead of the actual type name }
  5904. if not assigned(typesym) then
  5905. result:='<Currently Parsed Class>'
  5906. else
  5907. result:=typesymbolprettyname;
  5908. end;
  5909. procedure tobjectdef.buildderef;
  5910. var
  5911. i : longint;
  5912. vmtentry : pvmtentry;
  5913. begin
  5914. inherited buildderef;
  5915. vmt_fieldderef.build(vmt_field);
  5916. childofderef.build(childof);
  5917. if df_copied_def in defoptions then
  5918. cloneddefderef.build(symtable.defowner)
  5919. else
  5920. tstoredsymtable(symtable).buildderef;
  5921. if objecttype=odt_helper then
  5922. extendeddefderef.build(extendeddef);
  5923. for i:=0 to vmtentries.count-1 do
  5924. begin
  5925. vmtentry:=pvmtentry(vmtentries[i]);
  5926. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5927. end;
  5928. if assigned(ImplementedInterfaces) then
  5929. begin
  5930. for i:=0 to ImplementedInterfaces.count-1 do
  5931. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5932. end;
  5933. end;
  5934. procedure tobjectdef.deref;
  5935. var
  5936. i : longint;
  5937. vmtentry : pvmtentry;
  5938. begin
  5939. inherited deref;
  5940. vmt_field:=tsym(vmt_fieldderef.resolve);
  5941. childof:=tobjectdef(childofderef.resolve);
  5942. if df_copied_def in defoptions then
  5943. begin
  5944. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5945. symtable:=cloneddef.symtable.getcopy;
  5946. end
  5947. else
  5948. tstoredsymtable(symtable).deref(false);
  5949. if objecttype=odt_helper then
  5950. extendeddef:=tdef(extendeddefderef.resolve);
  5951. for i:=0 to vmtentries.count-1 do
  5952. begin
  5953. vmtentry:=pvmtentry(vmtentries[i]);
  5954. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5955. end;
  5956. if assigned(ImplementedInterfaces) then
  5957. begin
  5958. for i:=0 to ImplementedInterfaces.count-1 do
  5959. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5960. end;
  5961. end;
  5962. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5963. var
  5964. pd: tprocdef absolute def;
  5965. st: tsymtable;
  5966. psym: tsym;
  5967. nname: TIDString;
  5968. begin
  5969. if (tdef(def).typ<>procdef) then
  5970. exit;
  5971. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5972. owner = symtable in which objcclassdef is defined
  5973. }
  5974. st:=pd.owner.defowner.owner;
  5975. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5976. { check for an existing procsym with our special name }
  5977. psym:=tsym(st.find(nname));
  5978. if not assigned(psym) then
  5979. begin
  5980. psym:=cprocsym.create(nname);
  5981. { avoid warning about this symbol being unused }
  5982. psym.IncRefCount;
  5983. { don't check for duplicates:
  5984. a) we checked above
  5985. b) in case we are in the implementation section of a unit, this
  5986. will also check for this symbol in the interface section
  5987. (since you normally cannot have symbols with the same name
  5988. both interface and implementation), and it's possible to
  5989. have class helpers for the same class in the interface and
  5990. in the implementation, and they cannot be merged since only
  5991. the once in the interface must be saved to the ppu/visible
  5992. from other units }
  5993. st.insert(psym,false);
  5994. end
  5995. else if (psym.typ<>procsym) then
  5996. internalerror(2009111501);
  5997. { add ourselves to this special procsym }
  5998. tprocsym(psym).procdeflist.add(def);
  5999. end;
  6000. procedure tobjectdef.derefimpl;
  6001. begin
  6002. inherited derefimpl;
  6003. { the procdefs are not owned by the class helper procsyms, so they
  6004. are not stored/restored either -> re-add them here }
  6005. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  6006. (oo_is_classhelper in objectoptions) then
  6007. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  6008. end;
  6009. procedure tobjectdef.resetvmtentries;
  6010. var
  6011. i : longint;
  6012. begin
  6013. for i:=0 to vmtentries.Count-1 do
  6014. Dispose(pvmtentry(vmtentries[i]));
  6015. vmtentries.clear;
  6016. end;
  6017. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  6018. var
  6019. i : longint;
  6020. vmtentry : pvmtentry;
  6021. begin
  6022. resetvmtentries;
  6023. vmtentries.count:=objdef.vmtentries.count;
  6024. for i:=0 to objdef.vmtentries.count-1 do
  6025. begin
  6026. new(vmtentry);
  6027. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  6028. vmtentries[i]:=vmtentry;
  6029. end;
  6030. end;
  6031. function tobjectdef.getparentdef:tdef;
  6032. begin
  6033. { TODO: Remove getparentdef hack}
  6034. { With 2 forward declared classes with the child class before the
  6035. parent class the child class is written earlier to the ppu. Leaving it
  6036. possible to have a reference to the parent class for property overriding,
  6037. but the parent class still has the childof not resolved yet (PFV) }
  6038. if childof=nil then
  6039. childof:=tobjectdef(childofderef.resolve);
  6040. result:=childof;
  6041. end;
  6042. procedure tobjectdef.prepareguid;
  6043. begin
  6044. { set up guid }
  6045. if not assigned(iidguid) then
  6046. begin
  6047. new(iidguid);
  6048. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  6049. end;
  6050. { setup iidstring }
  6051. if not assigned(iidstr) then
  6052. iidstr:=stringdup(''); { default is empty string }
  6053. end;
  6054. procedure tobjectdef.set_parent( c : tobjectdef);
  6055. begin
  6056. if assigned(childof) then
  6057. exit;
  6058. childof:=c;
  6059. if not assigned(c) then
  6060. exit;
  6061. { inherit options and status }
  6062. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  6063. { initially has the same number of abstract methods as the parent }
  6064. abstractcnt:=c.abstractcnt;
  6065. { add the data of the anchestor class/object }
  6066. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  6067. begin
  6068. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  6069. { inherit recordalignment }
  6070. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  6071. { if both the parent and this record use C-alignment, also inherit
  6072. the current field alignment }
  6073. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  6074. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  6075. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  6076. { the padding is not inherited for Objective-C classes (maybe not
  6077. for cppclass either?) }
  6078. if objecttype=odt_objcclass then
  6079. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  6080. if (oo_has_vmt in objectoptions) and
  6081. (oo_has_vmt in c.objectoptions) then
  6082. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  6083. { if parent has a vmt field then the offset is the same for the child PM }
  6084. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  6085. begin
  6086. vmt_field:=c.vmt_field;
  6087. include(objectoptions,oo_has_vmt);
  6088. end;
  6089. end;
  6090. end;
  6091. procedure tobjectdef.insertvmt;
  6092. begin
  6093. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6094. exit;
  6095. if (oo_has_vmt in objectoptions) then
  6096. internalerror(12345)
  6097. else
  6098. begin
  6099. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  6100. tObjectSymtable(symtable).fieldalignment);
  6101. if (tf_requires_proper_alignment in target_info.flags) then
  6102. begin
  6103. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  6104. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  6105. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  6106. end;
  6107. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[],true);
  6108. hidesym(vmt_field);
  6109. tObjectSymtable(symtable).insert(vmt_field);
  6110. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  6111. include(objectoptions,oo_has_vmt);
  6112. end;
  6113. end;
  6114. function tobjectdef.vmt_offset: asizeint;
  6115. begin
  6116. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  6117. result:=0
  6118. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  6119. result:=tfieldvarsym(vmt_field).fieldoffset
  6120. else
  6121. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  6122. end;
  6123. procedure tobjectdef.check_forwards;
  6124. begin
  6125. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  6126. inherited;
  6127. if (oo_is_forward in objectoptions) then
  6128. begin
  6129. { ok, in future, the forward can be resolved }
  6130. Message1(sym_e_class_forward_not_resolved,objrealname^);
  6131. exclude(objectoptions,oo_is_forward);
  6132. end;
  6133. end;
  6134. function tobjectdef.find_destructor: tprocdef;
  6135. var
  6136. objdef: tobjectdef;
  6137. begin
  6138. objdef:=self;
  6139. while assigned(objdef) do
  6140. begin
  6141. result:=objdef.find_procdef_bytype(potype_destructor);
  6142. if assigned(result) then
  6143. exit;
  6144. objdef:=objdef.childof;
  6145. end;
  6146. result:=nil;
  6147. end;
  6148. function tobjectdef.implements_any_interfaces: boolean;
  6149. begin
  6150. result := (ImplementedInterfaces.Count > 0) or
  6151. (assigned(childof) and childof.implements_any_interfaces);
  6152. end;
  6153. function tobjectdef.size : asizeint;
  6154. begin
  6155. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6156. result:=voidpointertype.size
  6157. else
  6158. result:=tObjectSymtable(symtable).datasize;
  6159. end;
  6160. function tobjectdef.alignment:shortint;
  6161. begin
  6162. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  6163. alignment:=voidpointertype.alignment
  6164. else
  6165. alignment:=tObjectSymtable(symtable).recordalignment;
  6166. end;
  6167. function tobjectdef.vmtmethodoffset(index:longint):longint;
  6168. begin
  6169. { for offset of methods for classes, see rtl/inc/objpash.inc }
  6170. case objecttype of
  6171. odt_class:
  6172. { the +2*sizeof(pint) is size and -size }
  6173. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  6174. odt_helper,
  6175. odt_objcclass,
  6176. odt_objcprotocol:
  6177. vmtmethodoffset:=0;
  6178. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  6179. vmtmethodoffset:=index*voidcodepointertype.size;
  6180. odt_javaclass,
  6181. odt_interfacejava:
  6182. { invalid }
  6183. vmtmethodoffset:=-1;
  6184. else
  6185. { the +2*sizeof(pint) is size and -size }
  6186. {$ifdef WITHDMT}
  6187. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  6188. {$else WITHDMT}
  6189. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  6190. {$endif WITHDMT}
  6191. end;
  6192. end;
  6193. function tobjectdef.vmt_mangledname : TSymStr;
  6194. begin
  6195. if not(oo_has_vmt in objectoptions) then
  6196. Message1(parser_n_object_has_no_vmt,objrealname^);
  6197. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  6198. end;
  6199. function tobjectdef.vmt_def: trecorddef;
  6200. var
  6201. vmttypesym: tsym;
  6202. begin
  6203. vmttypesym:=tsym(get_top_level_symtable.Find('vmtdef$'+mangledparaname));
  6204. if not assigned(vmttypesym) or
  6205. (vmttypesym.typ<>symconst.typesym) or
  6206. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  6207. internalerror(2015052501);
  6208. result:=trecorddef(ttypesym(vmttypesym).typedef);
  6209. end;
  6210. function tobjectdef.needs_inittable : boolean;
  6211. var
  6212. hp : tobjectdef;
  6213. begin
  6214. case objecttype of
  6215. odt_helper,
  6216. odt_class :
  6217. needs_inittable:=false;
  6218. odt_dispinterface,
  6219. odt_interfacecom:
  6220. needs_inittable:=true;
  6221. odt_interfacecorba:
  6222. begin
  6223. hp:=childof;
  6224. while assigned(hp) do
  6225. begin
  6226. if hp=interface_iunknown then
  6227. begin
  6228. needs_inittable:=true;
  6229. exit;
  6230. end;
  6231. hp:=hp.childof;
  6232. end;
  6233. needs_inittable:=false;
  6234. end;
  6235. odt_object:
  6236. needs_inittable:=
  6237. tObjectSymtable(symtable).needs_init_final or
  6238. (assigned(childof) and
  6239. childof.needs_inittable);
  6240. odt_cppclass,
  6241. odt_objcclass,
  6242. odt_objcprotocol,
  6243. odt_javaclass,
  6244. odt_interfacejava:
  6245. needs_inittable:=false;
  6246. else
  6247. internalerror(200108267);
  6248. end;
  6249. end;
  6250. function tobjectdef.needs_separate_initrtti : boolean;
  6251. begin
  6252. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6253. end;
  6254. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  6255. begin
  6256. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  6257. result:=inherited rtti_mangledname(rt)
  6258. else
  6259. begin
  6260. { necessary in case of a dynamic array of nsobject, or
  6261. if an nsobject field appears in a record that needs
  6262. init/finalisation }
  6263. if rt=initrtti then
  6264. begin
  6265. result:=voidpointertype.rtti_mangledname(rt);
  6266. exit;
  6267. end;
  6268. if not(target_info.system in systems_objc_nfabi) then
  6269. begin
  6270. result:=target_asm.labelprefix;
  6271. case objecttype of
  6272. odt_objcclass:
  6273. begin
  6274. case rt of
  6275. objcclassrtti:
  6276. if not(oo_is_classhelper in objectoptions) then
  6277. result:=result+'_OBJC_CLASS_'
  6278. else
  6279. result:=result+'_OBJC_CATEGORY_';
  6280. objcmetartti:
  6281. if not(oo_is_classhelper in objectoptions) then
  6282. result:=result+'_OBJC_METACLASS_'
  6283. else
  6284. internalerror(2009111511);
  6285. else
  6286. internalerror(2009092302);
  6287. end;
  6288. end;
  6289. odt_objcprotocol:
  6290. result:=result+'_OBJC_PROTOCOL_';
  6291. end;
  6292. end
  6293. else
  6294. begin
  6295. case objecttype of
  6296. odt_objcclass:
  6297. begin
  6298. if (oo_is_classhelper in objectoptions) and
  6299. (rt<>objcclassrtti) then
  6300. internalerror(2009111512);
  6301. case rt of
  6302. objcclassrtti:
  6303. if not(oo_is_classhelper in objectoptions) then
  6304. result:='_OBJC_CLASS_$_'
  6305. else
  6306. result:='_OBJC_$_CATEGORY_';
  6307. objcmetartti:
  6308. result:='_OBJC_METACLASS_$_';
  6309. objcclassrortti:
  6310. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  6311. objcmetarortti:
  6312. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  6313. else
  6314. internalerror(2009092303);
  6315. end;
  6316. end;
  6317. odt_objcprotocol:
  6318. begin
  6319. result:=lower(target_asm.labelprefix);
  6320. case rt of
  6321. objcclassrtti:
  6322. result:=result+'_OBJC_PROTOCOL_$_';
  6323. objcmetartti:
  6324. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  6325. else
  6326. internalerror(2009092501);
  6327. end;
  6328. end;
  6329. else
  6330. internalerror(2013113005);
  6331. end;
  6332. end;
  6333. result:=result+objextname^;
  6334. end;
  6335. end;
  6336. function tobjectdef.members_need_inittable : boolean;
  6337. begin
  6338. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  6339. end;
  6340. function tobjectdef.is_publishable : boolean;
  6341. begin
  6342. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  6343. end;
  6344. function tobjectdef.get_next_dispid: longint;
  6345. begin
  6346. inc(fcurrent_dispid);
  6347. result:=fcurrent_dispid;
  6348. end;
  6349. function tobjectdef.search_enumerator_get: tprocdef;
  6350. begin
  6351. result:=inherited;
  6352. if not assigned(result) and assigned(childof) then
  6353. result:=childof.search_enumerator_get;
  6354. end;
  6355. function tobjectdef.search_enumerator_move: tprocdef;
  6356. begin
  6357. result:=inherited;
  6358. if not assigned(result) and assigned(childof) then
  6359. result:=childof.search_enumerator_move;
  6360. end;
  6361. function tobjectdef.search_enumerator_current: tsym;
  6362. begin
  6363. result:=inherited;
  6364. if not assigned(result) and assigned(childof) then
  6365. result:=childof.search_enumerator_current;
  6366. end;
  6367. procedure tobjectdef.register_created_classref_type;
  6368. begin
  6369. if not classref_created_in_current_module then
  6370. begin
  6371. classref_created_in_current_module:=true;
  6372. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  6373. end;
  6374. end;
  6375. procedure tobjectdef.register_created_object_type;
  6376. begin
  6377. if not created_in_current_module then
  6378. begin
  6379. created_in_current_module:=true;
  6380. current_module.wpoinfo.addcreatedobjtype(self);
  6381. end;
  6382. end;
  6383. procedure tobjectdef.register_maybe_created_object_type;
  6384. begin
  6385. { if we know it has been created for sure, no need
  6386. to also record that it maybe can be created in
  6387. this module
  6388. }
  6389. if not (created_in_current_module) and
  6390. not (maybe_created_in_current_module) then
  6391. begin
  6392. maybe_created_in_current_module:=true;
  6393. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  6394. end;
  6395. end;
  6396. procedure tobjectdef.register_vmt_call(index: longint);
  6397. begin
  6398. if (is_object(self) or is_class(self)) then
  6399. current_module.wpoinfo.addcalledvmtentry(self,index);
  6400. end;
  6401. procedure check_and_finish_msg(data: tobject; arg: pointer);
  6402. var
  6403. def: tdef absolute data;
  6404. pd: tprocdef absolute data;
  6405. i,
  6406. paracount: longint;
  6407. begin
  6408. if (def.typ=procdef) then
  6409. begin
  6410. { add all messages also under a dummy name to the symtable in
  6411. which the objcclass/protocol/category is declared, so they can
  6412. be called via id.<name>
  6413. }
  6414. create_class_helper_for_procdef(pd,nil);
  6415. { we have to wait until now to set the mangled name because it
  6416. depends on the (possibly external) class name, which is defined
  6417. at the very end. }
  6418. if not(po_msgstr in pd.procoptions) then
  6419. begin
  6420. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  6421. { recover to avoid internalerror later on }
  6422. include(pd.procoptions,po_msgstr);
  6423. pd.messageinf.str:=stringdup('MissingDeclaration');
  6424. end;
  6425. { Mangled name is already set in case this is a copy of
  6426. another type. }
  6427. if not(po_has_mangledname in pd.procoptions) then
  6428. begin
  6429. { check whether the number of formal parameters is correct,
  6430. and whether they have valid Objective-C types }
  6431. paracount:=0;
  6432. for i:=1 to length(pd.messageinf.str^) do
  6433. if pd.messageinf.str^[i]=':' then
  6434. inc(paracount);
  6435. for i:=0 to pd.paras.count-1 do
  6436. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  6437. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  6438. dec(paracount);
  6439. if (paracount<>0) then
  6440. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  6441. pd.setmangledname(pd.objcmangledname);
  6442. end
  6443. else
  6444. { all checks already done }
  6445. exit;
  6446. if not(oo_is_external in pd.struct.objectoptions) then
  6447. begin
  6448. if (po_varargs in pd.procoptions) then
  6449. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  6450. else
  6451. begin
  6452. { check for "array of const" parameters }
  6453. for i:=0 to pd.parast.symlist.count-1 do
  6454. begin
  6455. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6456. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6457. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6458. end;
  6459. end;
  6460. end;
  6461. end;
  6462. end;
  6463. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6464. var
  6465. sym: tsym absolute data;
  6466. begin
  6467. if (sym.typ=fieldvarsym) and
  6468. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6469. sym.IncRefCount;
  6470. end;
  6471. procedure tobjectdef.finish_objc_data;
  6472. begin
  6473. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6474. if (oo_is_external in objectoptions) then
  6475. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6476. end;
  6477. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6478. var
  6479. sym: tabstractvarsym absolute data;
  6480. res: pboolean absolute arg;
  6481. founderrordef: tdef;
  6482. begin
  6483. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6484. exit;
  6485. if (sym.typ=paravarsym) and
  6486. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6487. is_array_of_const(tparavarsym(sym).vardef)) then
  6488. exit;
  6489. if not objcchecktype(sym.vardef,founderrordef) then
  6490. begin
  6491. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6492. res^:=false;
  6493. end;
  6494. end;
  6495. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6496. var
  6497. def: tdef absolute data;
  6498. res: pboolean absolute arg;
  6499. founderrordef: tdef;
  6500. begin
  6501. if (def.typ<>procdef) then
  6502. exit;
  6503. { check parameter types for validity }
  6504. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6505. { check the result type for validity }
  6506. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6507. begin
  6508. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6509. res^:=false;
  6510. end;
  6511. end;
  6512. function tobjectdef.check_objc_types: boolean;
  6513. begin
  6514. { done in separate step from finish_objc_data, because when
  6515. finish_objc_data is called, not all forwarddefs have been resolved
  6516. yet and we need to know all types here }
  6517. result:=true;
  6518. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6519. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6520. end;
  6521. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6522. var
  6523. def: tdef absolute data;
  6524. pd: tprocdef absolute data;
  6525. begin
  6526. if (def.typ=procdef) then
  6527. begin
  6528. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6529. if (oo_is_external in pd.struct.objectoptions) then
  6530. begin
  6531. { copied from psub.read_proc }
  6532. if assigned(tobjectdef(pd.struct).import_lib) then
  6533. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6534. else
  6535. begin
  6536. { add import name to external list for DLL scanning }
  6537. if tf_has_dllscanner in target_info.flags then
  6538. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6539. end;
  6540. end;
  6541. end;
  6542. end;
  6543. procedure tobjectdef.finish_cpp_data;
  6544. begin
  6545. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6546. end;
  6547. {****************************************************************************
  6548. TImplementedInterface
  6549. ****************************************************************************}
  6550. function TImplementedInterface.GetIOffset: longint;
  6551. begin
  6552. if (fIOffset=-1) and
  6553. (IType in [etFieldValue,etFieldValueClass]) then
  6554. result:=tfieldvarsym(ImplementsField).fieldoffset
  6555. else
  6556. result:=fIOffset;
  6557. end;
  6558. constructor TImplementedInterface.create(aintf: tobjectdef);
  6559. begin
  6560. inherited create;
  6561. intfdef:=aintf;
  6562. IOffset:=-1;
  6563. IType:=etStandard;
  6564. NameMappings:=nil;
  6565. procdefs:=nil;
  6566. end;
  6567. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6568. begin
  6569. inherited create;
  6570. intfdef:=nil;
  6571. intfdefderef:=intfd;
  6572. ImplementsGetterDeref:=getterd;
  6573. IOffset:=-1;
  6574. IType:=etStandard;
  6575. NameMappings:=nil;
  6576. procdefs:=nil;
  6577. end;
  6578. destructor TImplementedInterface.destroy;
  6579. var
  6580. i : longint;
  6581. mappedname : pshortstring;
  6582. begin
  6583. if assigned(NameMappings) then
  6584. begin
  6585. for i:=0 to NameMappings.Count-1 do
  6586. begin
  6587. mappedname:=pshortstring(NameMappings[i]);
  6588. stringdispose(mappedname);
  6589. end;
  6590. NameMappings.free;
  6591. NameMappings:=nil;
  6592. end;
  6593. if assigned(procdefs) then
  6594. begin
  6595. procdefs.free;
  6596. procdefs:=nil;
  6597. end;
  6598. inherited destroy;
  6599. end;
  6600. procedure TImplementedInterface.buildderef;
  6601. begin
  6602. intfdefderef.build(intfdef);
  6603. ImplementsGetterDeref.build(ImplementsGetter);
  6604. end;
  6605. procedure TImplementedInterface.deref;
  6606. begin
  6607. intfdef:=tobjectdef(intfdefderef.resolve);
  6608. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6609. end;
  6610. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6611. begin
  6612. if not assigned(NameMappings) then
  6613. NameMappings:=TFPHashList.Create;
  6614. NameMappings.Add(origname,stringdup(newname));
  6615. end;
  6616. function TImplementedInterface.GetMapping(const origname: string):string;
  6617. var
  6618. mappedname : pshortstring;
  6619. begin
  6620. result:='';
  6621. if not assigned(NameMappings) then
  6622. exit;
  6623. mappedname:=PShortstring(NameMappings.Find(origname));
  6624. if assigned(mappedname) then
  6625. result:=mappedname^;
  6626. end;
  6627. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6628. begin
  6629. if not assigned(procdefs) then
  6630. procdefs:=TFPObjectList.Create(false);
  6631. { duplicate entries must be stored, because multiple }
  6632. { interfaces can declare methods with the same name }
  6633. { and all of these get their own VMT entry }
  6634. procdefs.Add(pd);
  6635. end;
  6636. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6637. var
  6638. i : longint;
  6639. begin
  6640. result:=false;
  6641. { interfaces being implemented through delegation are not mergable (FK) }
  6642. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6643. exit;
  6644. weight:=0;
  6645. { empty interface is mergeable }
  6646. if ProcDefs.Count=0 then
  6647. begin
  6648. result:=true;
  6649. exit;
  6650. end;
  6651. { The interface to merge must at least the number of
  6652. procedures of this interface }
  6653. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6654. exit;
  6655. for i:=0 to ProcDefs.Count-1 do
  6656. begin
  6657. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6658. exit;
  6659. end;
  6660. weight:=ProcDefs.Count;
  6661. result:=true;
  6662. end;
  6663. function TImplementedInterface.getcopy:TImplementedInterface;
  6664. begin
  6665. Result:=TImplementedInterface.Create(nil);
  6666. { 1) the procdefs list will be freed once for each copy
  6667. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6668. 3) idem for the name mappings
  6669. }
  6670. { warning: this is completely wrong on so many levels...
  6671. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6672. We need to make clean copies of the different fields
  6673. this is not implemented yet, and thus we generate an internal
  6674. error instead PM 2011-06-14 }
  6675. internalerror(2011061401);
  6676. end;
  6677. {****************************************************************************
  6678. TFORWARDDEF
  6679. ****************************************************************************}
  6680. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6681. begin
  6682. inherited create(forwarddef,true);
  6683. tosymname:=stringdup(s);
  6684. forwardpos:=pos;
  6685. end;
  6686. function tforwarddef.GetTypeName:string;
  6687. begin
  6688. GetTypeName:='unresolved forward to '+tosymname^;
  6689. end;
  6690. destructor tforwarddef.destroy;
  6691. begin
  6692. stringdispose(tosymname);
  6693. inherited destroy;
  6694. end;
  6695. function tforwarddef.getcopy:tstoreddef;
  6696. begin
  6697. result:=cforwarddef.create(tosymname^, forwardpos);
  6698. end;
  6699. {****************************************************************************
  6700. TUNDEFINEDDEF
  6701. ****************************************************************************}
  6702. constructor tundefineddef.create(doregister:boolean);
  6703. begin
  6704. inherited create(undefineddef,doregister);
  6705. end;
  6706. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6707. begin
  6708. inherited ppuload(undefineddef,ppufile);
  6709. ppuload_platform(ppufile);
  6710. end;
  6711. function tundefineddef.GetTypeName:string;
  6712. begin
  6713. GetTypeName:='<undefined type>';
  6714. end;
  6715. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6716. begin
  6717. inherited ppuwrite(ppufile);
  6718. writeentry(ppufile,ibundefineddef);
  6719. end;
  6720. {****************************************************************************
  6721. TERRORDEF
  6722. ****************************************************************************}
  6723. constructor terrordef.create;
  6724. begin
  6725. inherited create(errordef,true);
  6726. { prevent consecutive faults }
  6727. savesize:=1;
  6728. end;
  6729. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6730. begin
  6731. { Can't write errordefs to ppu }
  6732. internalerror(200411063);
  6733. end;
  6734. function terrordef.GetTypeName:string;
  6735. begin
  6736. GetTypeName:='<erroneous type>';
  6737. end;
  6738. function terrordef.getmangledparaname:TSymStr;
  6739. begin
  6740. getmangledparaname:='error';
  6741. end;
  6742. {****************************************************************************
  6743. Definition Helpers
  6744. ****************************************************************************}
  6745. function is_interfacecom(def: tdef): boolean;
  6746. begin
  6747. is_interfacecom:=
  6748. assigned(def) and
  6749. (def.typ=objectdef) and
  6750. (tobjectdef(def).objecttype=odt_interfacecom);
  6751. end;
  6752. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6753. begin
  6754. is_interfacecom_or_dispinterface:=
  6755. assigned(def) and
  6756. (def.typ=objectdef) and
  6757. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6758. end;
  6759. function is_any_interface_kind(def: tdef): boolean;
  6760. begin
  6761. result:=
  6762. assigned(def) and
  6763. (def.typ=objectdef) and
  6764. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6765. is_objccategory(def));
  6766. end;
  6767. function is_interfacecorba(def: tdef): boolean;
  6768. begin
  6769. is_interfacecorba:=
  6770. assigned(def) and
  6771. (def.typ=objectdef) and
  6772. (tobjectdef(def).objecttype=odt_interfacecorba);
  6773. end;
  6774. function is_interface(def: tdef): boolean;
  6775. begin
  6776. is_interface:=
  6777. assigned(def) and
  6778. (def.typ=objectdef) and
  6779. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6780. end;
  6781. function is_dispinterface(def: tdef): boolean;
  6782. begin
  6783. result:=
  6784. assigned(def) and
  6785. (def.typ=objectdef) and
  6786. (tobjectdef(def).objecttype=odt_dispinterface);
  6787. end;
  6788. function is_class(def: tdef): boolean;
  6789. begin
  6790. is_class:=
  6791. assigned(def) and
  6792. (def.typ=objectdef) and
  6793. (tobjectdef(def).objecttype=odt_class);
  6794. end;
  6795. function is_object(def: tdef): boolean;
  6796. begin
  6797. is_object:=
  6798. assigned(def) and
  6799. (def.typ=objectdef) and
  6800. (tobjectdef(def).objecttype=odt_object);
  6801. end;
  6802. function is_cppclass(def: tdef): boolean;
  6803. begin
  6804. is_cppclass:=
  6805. assigned(def) and
  6806. (def.typ=objectdef) and
  6807. (tobjectdef(def).objecttype=odt_cppclass);
  6808. end;
  6809. function is_objcclass(def: tdef): boolean;
  6810. begin
  6811. is_objcclass:=
  6812. assigned(def) and
  6813. (def.typ=objectdef) and
  6814. (tobjectdef(def).objecttype=odt_objcclass);
  6815. end;
  6816. function is_objectpascal_helper(def: tdef): boolean;
  6817. begin
  6818. result:=
  6819. assigned(def) and
  6820. (def.typ=objectdef) and
  6821. (tobjectdef(def).objecttype=odt_helper);
  6822. end;
  6823. function is_objcclassref(def: tdef): boolean;
  6824. begin
  6825. is_objcclassref:=
  6826. assigned(def) and
  6827. (def.typ=classrefdef) and
  6828. is_objcclass(tclassrefdef(def).pointeddef);
  6829. end;
  6830. function is_objcprotocol(def: tdef): boolean;
  6831. begin
  6832. result:=
  6833. assigned(def) and
  6834. (def.typ=objectdef) and
  6835. (tobjectdef(def).objecttype=odt_objcprotocol);
  6836. end;
  6837. function is_objccategory(def: tdef): boolean;
  6838. begin
  6839. result:=
  6840. assigned(def) and
  6841. (def.typ=objectdef) and
  6842. { if used as a forward type }
  6843. ((tobjectdef(def).objecttype=odt_objccategory) or
  6844. { if used as after it has been resolved }
  6845. ((tobjectdef(def).objecttype=odt_objcclass) and
  6846. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6847. end;
  6848. function is_objc_class_or_protocol(def: tdef): boolean;
  6849. begin
  6850. result:=
  6851. assigned(def) and
  6852. (def.typ=objectdef) and
  6853. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6854. end;
  6855. function is_objc_protocol_or_category(def: tdef): boolean;
  6856. begin
  6857. result:=
  6858. assigned(def) and
  6859. (def.typ=objectdef) and
  6860. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6861. ((tobjectdef(def).objecttype = odt_objcclass) and
  6862. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6863. end;
  6864. function is_classhelper(def: tdef): boolean;
  6865. begin
  6866. result:=
  6867. is_objectpascal_helper(def) or
  6868. is_objccategory(def);
  6869. end;
  6870. function is_class_or_interface(def: tdef): boolean;
  6871. begin
  6872. result:=
  6873. assigned(def) and
  6874. (def.typ=objectdef) and
  6875. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6876. end;
  6877. function is_class_or_interface_or_objc(def: tdef): boolean;
  6878. begin
  6879. result:=
  6880. assigned(def) and
  6881. (def.typ=objectdef) and
  6882. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6883. end;
  6884. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6885. begin
  6886. result:=
  6887. assigned(def) and
  6888. (def.typ=objectdef) and
  6889. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6890. end;
  6891. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6892. begin
  6893. result:=
  6894. assigned(def) and
  6895. (def.typ=objectdef) and
  6896. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6897. end;
  6898. function is_class_or_interface_or_object(def: tdef): boolean;
  6899. begin
  6900. result:=
  6901. assigned(def) and
  6902. (def.typ=objectdef) and
  6903. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6904. end;
  6905. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6906. begin
  6907. result:=
  6908. assigned(def) and
  6909. (def.typ=objectdef) and
  6910. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6911. end;
  6912. function is_implicit_pointer_object_type(def: tdef): boolean;
  6913. begin
  6914. result:=
  6915. assigned(def) and
  6916. (((def.typ=objectdef) and
  6917. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6918. ((target_info.system in systems_jvm) and
  6919. (def.typ=recorddef)));
  6920. end;
  6921. function is_implicit_array_pointer(def: tdef): boolean;
  6922. begin
  6923. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6924. end;
  6925. function is_class_or_object(def: tdef): boolean;
  6926. begin
  6927. result:=
  6928. assigned(def) and
  6929. (def.typ=objectdef) and
  6930. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6931. end;
  6932. function is_record(def: tdef): boolean;
  6933. begin
  6934. result:=
  6935. assigned(def) and
  6936. (def.typ=recorddef);
  6937. end;
  6938. function is_javaclass(def: tdef): boolean;
  6939. begin
  6940. result:=
  6941. assigned(def) and
  6942. (def.typ=objectdef) and
  6943. (tobjectdef(def).objecttype=odt_javaclass);
  6944. end;
  6945. function is_javaclassref(def: tdef): boolean;
  6946. begin
  6947. is_javaclassref:=
  6948. assigned(def) and
  6949. (def.typ=classrefdef) and
  6950. is_javaclass(tclassrefdef(def).pointeddef);
  6951. end;
  6952. function is_javainterface(def: tdef): boolean;
  6953. begin
  6954. result:=
  6955. assigned(def) and
  6956. (def.typ=objectdef) and
  6957. (tobjectdef(def).objecttype=odt_interfacejava);
  6958. end;
  6959. function is_java_class_or_interface(def: tdef): boolean;
  6960. begin
  6961. result:=
  6962. assigned(def) and
  6963. (def.typ=objectdef) and
  6964. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6965. end;
  6966. procedure loadobjctypes;
  6967. begin
  6968. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6969. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6970. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6971. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6972. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6973. end;
  6974. procedure maybeloadcocoatypes;
  6975. var
  6976. tsym: ttypesym;
  6977. cocoaunit: string[15];
  6978. begin
  6979. if assigned(objc_fastenumeration) then
  6980. exit;
  6981. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim,system_aarch64_darwin,system_x86_64_iphonesim]) then
  6982. cocoaunit:='COCOAALL'
  6983. else
  6984. cocoaunit:='IPHONEALL';
  6985. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6986. if assigned(tsym) then
  6987. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6988. else
  6989. objc_fastenumeration:=nil;
  6990. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6991. if assigned(tsym) then
  6992. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6993. else
  6994. objc_fastenumerationstate:=nil;
  6995. end;
  6996. function use_vectorfpu(def : tdef) : boolean;
  6997. begin
  6998. {$ifdef x86}
  6999. {$define use_vectorfpuimplemented}
  7000. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  7001. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  7002. {$endif x86}
  7003. {$ifdef arm}
  7004. {$define use_vectorfpuimplemented}
  7005. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  7006. {$endif arm}
  7007. {$ifdef aarch64}
  7008. {$define use_vectorfpuimplemented}
  7009. use_vectorfpu:=true;
  7010. {$endif aarch64}
  7011. {$ifndef use_vectorfpuimplemented}
  7012. use_vectorfpu:=false;
  7013. {$endif}
  7014. end;
  7015. end.