symdef.pas 279 KB

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