symdef.pas 222 KB

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