symdef.pas 222 KB

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