symdef.pas 224 KB

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