symdef.pas 223 KB

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