symdef.pas 221 KB

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