symdef.pas 221 KB

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