symdef.pas 225 KB

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