symdef.pas 256 KB

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