symdef.pas 210 KB

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