symdef.pas 253 KB

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