symdef.pas 235 KB

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