symdef.pas 243 KB

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