symdef.pas 220 KB

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