symdef.pas 270 KB

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