symdef.pas 294 KB

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