symdef.pas 232 KB

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