symdef.pas 270 KB

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