symdef.pas 280 KB

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