symdef.pas 268 KB

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