symdef.pas 276 KB

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