symdef.pas 264 KB

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