symdef.pas 246 KB

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