symdef.pas 285 KB

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