symdef.pas 247 KB

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