symdef.pas 267 KB

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