symdef.pas 267 KB

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