symdef.pas 268 KB

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