2
0

symdef.pas 233 KB

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