symdef.pas 246 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528
  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)
  1877. { records cannot go into registers on 16 bit targets for now }
  1878. and (sizeof(asizeint)>2)
  1879. and not trecorddef(self).contains_float_field) or
  1880. (recsize <= sizeof(asizeint)))
  1881. and 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_linux,system_x86_64_freebsd,
  2520. system_x86_64_openbsd,system_x86_64_netbsd,
  2521. system_x86_64_solaris,system_x86_64_embedded,
  2522. system_x86_64_dragonfly] then
  2523. savesize:=16
  2524. else
  2525. savesize:=12;
  2526. s64real,
  2527. s64currency,
  2528. s64comp : savesize:=8;
  2529. else
  2530. savesize:=0;
  2531. end;
  2532. end;
  2533. function tfloatdef.getvardef : longint;
  2534. const
  2535. floattype2vardef : array[tfloattype] of longint = (
  2536. varSingle,varDouble,varUndefined,varUndefined,
  2537. varUndefined,varCurrency,varUndefined);
  2538. begin
  2539. if (upper(typename)='TDATETIME') and
  2540. assigned(owner) and
  2541. assigned(owner.name) and
  2542. (owner.name^='SYSTEM') then
  2543. result:=varDate
  2544. else
  2545. result:=floattype2vardef[floattype];
  2546. end;
  2547. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2548. begin
  2549. inherited ppuwrite(ppufile);
  2550. ppufile.putbyte(byte(floattype));
  2551. writeentry(ppufile,ibfloatdef);
  2552. end;
  2553. function tfloatdef.is_publishable : boolean;
  2554. begin
  2555. is_publishable:=true;
  2556. end;
  2557. function tfloatdef.GetTypeName : string;
  2558. const
  2559. names : array[tfloattype] of string[20] = (
  2560. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2561. begin
  2562. GetTypeName:=names[floattype];
  2563. end;
  2564. {****************************************************************************
  2565. TFILEDEF
  2566. ****************************************************************************}
  2567. constructor tfiledef.createtext;
  2568. begin
  2569. inherited create(filedef);
  2570. filetyp:=ft_text;
  2571. typedfiledef:=nil;
  2572. end;
  2573. constructor tfiledef.createuntyped;
  2574. begin
  2575. inherited create(filedef);
  2576. filetyp:=ft_untyped;
  2577. typedfiledef:=nil;
  2578. end;
  2579. constructor tfiledef.createtyped(def:tdef);
  2580. begin
  2581. inherited create(filedef);
  2582. filetyp:=ft_typed;
  2583. typedfiledef:=def;
  2584. end;
  2585. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2586. begin
  2587. inherited ppuload(filedef,ppufile);
  2588. filetyp:=tfiletyp(ppufile.getbyte);
  2589. if filetyp=ft_typed then
  2590. ppufile.getderef(typedfiledefderef)
  2591. else
  2592. typedfiledef:=nil;
  2593. ppuload_platform(ppufile);
  2594. end;
  2595. function tfiledef.getcopy : tstoreddef;
  2596. begin
  2597. case filetyp of
  2598. ft_typed:
  2599. result:=cfiledef.createtyped(typedfiledef);
  2600. ft_untyped:
  2601. result:=cfiledef.createuntyped;
  2602. ft_text:
  2603. result:=cfiledef.createtext;
  2604. else
  2605. internalerror(2004121201);
  2606. end;
  2607. end;
  2608. procedure tfiledef.buildderef;
  2609. begin
  2610. inherited buildderef;
  2611. if filetyp=ft_typed then
  2612. typedfiledefderef.build(typedfiledef);
  2613. end;
  2614. procedure tfiledef.deref;
  2615. begin
  2616. inherited deref;
  2617. if filetyp=ft_typed then
  2618. typedfiledef:=tdef(typedfiledefderef.resolve);
  2619. end;
  2620. function tfiledef.size:asizeint;
  2621. begin
  2622. if savesize=0 then
  2623. setsize;
  2624. size:=savesize;
  2625. end;
  2626. procedure tfiledef.setsize;
  2627. begin
  2628. case filetyp of
  2629. ft_text:
  2630. savesize:=search_system_type('TEXTREC').typedef.size;
  2631. ft_typed:
  2632. begin
  2633. savesize:=search_system_type('FILEREC').typedef.size;
  2634. { allocate put/get buffer in iso mode }
  2635. if m_iso in current_settings.modeswitches then
  2636. inc(savesize,typedfiledef.size);
  2637. end;
  2638. ft_untyped:
  2639. savesize:=search_system_type('FILEREC').typedef.size;
  2640. else
  2641. internalerror(2013113001);
  2642. end;
  2643. end;
  2644. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2645. begin
  2646. inherited ppuwrite(ppufile);
  2647. ppufile.putbyte(byte(filetyp));
  2648. if filetyp=ft_typed then
  2649. ppufile.putderef(typedfiledefderef);
  2650. writeentry(ppufile,ibfiledef);
  2651. end;
  2652. function tfiledef.GetTypeName : string;
  2653. begin
  2654. case filetyp of
  2655. ft_untyped:
  2656. GetTypeName:='File';
  2657. ft_typed:
  2658. GetTypeName:='File Of '+typedfiledef.typename;
  2659. ft_text:
  2660. GetTypeName:='Text'
  2661. else
  2662. internalerror(2013113002);
  2663. end;
  2664. end;
  2665. function tfiledef.getmangledparaname : TSymStr;
  2666. begin
  2667. case filetyp of
  2668. ft_untyped:
  2669. getmangledparaname:='FILE';
  2670. ft_typed:
  2671. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2672. ft_text:
  2673. getmangledparaname:='TEXT'
  2674. else
  2675. internalerror(2013113003);
  2676. end;
  2677. end;
  2678. {****************************************************************************
  2679. TVARIANTDEF
  2680. ****************************************************************************}
  2681. constructor tvariantdef.create(v : tvarianttype);
  2682. begin
  2683. inherited create(variantdef);
  2684. varianttype:=v;
  2685. setsize;
  2686. end;
  2687. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2688. begin
  2689. inherited ppuload(variantdef,ppufile);
  2690. varianttype:=tvarianttype(ppufile.getbyte);
  2691. setsize;
  2692. ppuload_platform(ppufile);
  2693. end;
  2694. function tvariantdef.getcopy : tstoreddef;
  2695. begin
  2696. result:=cvariantdef.create(varianttype);
  2697. end;
  2698. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2699. begin
  2700. inherited ppuwrite(ppufile);
  2701. ppufile.putbyte(byte(varianttype));
  2702. writeentry(ppufile,ibvariantdef);
  2703. end;
  2704. function tvariantdef.getvardef : longint;
  2705. begin
  2706. Result:=varVariant;
  2707. end;
  2708. procedure tvariantdef.setsize;
  2709. begin
  2710. {$ifdef cpu64bitaddr}
  2711. savesize:=24;
  2712. {$else cpu64bitaddr}
  2713. savesize:=16;
  2714. {$endif cpu64bitaddr}
  2715. end;
  2716. function tvariantdef.GetTypeName : string;
  2717. begin
  2718. case varianttype of
  2719. vt_normalvariant:
  2720. GetTypeName:='Variant';
  2721. vt_olevariant:
  2722. GetTypeName:='OleVariant';
  2723. else
  2724. internalerror(2013113004);
  2725. end;
  2726. end;
  2727. function tvariantdef.needs_inittable : boolean;
  2728. begin
  2729. needs_inittable:=true;
  2730. end;
  2731. function tvariantdef.is_publishable : boolean;
  2732. begin
  2733. is_publishable:=true;
  2734. end;
  2735. {****************************************************************************
  2736. TABSTRACtpointerdef
  2737. ****************************************************************************}
  2738. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2739. begin
  2740. inherited create(dt);
  2741. pointeddef:=def;
  2742. end;
  2743. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2744. begin
  2745. inherited ppuload(dt,ppufile);
  2746. ppufile.getderef(pointeddefderef);
  2747. end;
  2748. procedure tabstractpointerdef.buildderef;
  2749. begin
  2750. inherited buildderef;
  2751. pointeddefderef.build(pointeddef);
  2752. end;
  2753. procedure tabstractpointerdef.deref;
  2754. begin
  2755. inherited deref;
  2756. pointeddef:=tdef(pointeddefderef.resolve);
  2757. end;
  2758. function tabstractpointerdef.size: asizeint;
  2759. begin
  2760. Result:=voidpointertype.size;
  2761. end;
  2762. function tabstractpointerdef.alignment: shortint;
  2763. begin
  2764. alignment:=size_2_align(voidpointertype.size);
  2765. end;
  2766. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2767. begin
  2768. inherited ppuwrite(ppufile);
  2769. ppufile.putderef(pointeddefderef);
  2770. end;
  2771. {****************************************************************************
  2772. tpointerdef
  2773. ****************************************************************************}
  2774. constructor tpointerdef.create(def:tdef);
  2775. begin
  2776. inherited create(pointerdef,def);
  2777. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2778. end;
  2779. function tpointerdef.size: asizeint;
  2780. begin
  2781. result:=sizeof(pint);
  2782. end;
  2783. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2784. begin
  2785. inherited ppuload(pointerdef,ppufile);
  2786. has_pointer_math:=(ppufile.getbyte<>0);
  2787. ppuload_platform(ppufile);
  2788. end;
  2789. function tpointerdef.getcopy : tstoreddef;
  2790. begin
  2791. { don't use direct pointeddef if it is a forwarddef because in other case
  2792. one of them will be destroyed on forward type resolve and the second will
  2793. point to garbage }
  2794. if pointeddef.typ=forwarddef then
  2795. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  2796. else
  2797. result:=cpointerdef.create(pointeddef);
  2798. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2799. tpointerdef(result).savesize:=savesize;
  2800. end;
  2801. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2802. begin
  2803. inherited ppuwrite(ppufile);
  2804. ppufile.putbyte(byte(has_pointer_math));
  2805. writeentry(ppufile,ibpointerdef);
  2806. end;
  2807. function tpointerdef.GetTypeName : string;
  2808. begin
  2809. { parameter types and the resultdef of a procvardef can contain a
  2810. pointer to this procvardef itself, resulting in endless recursion ->
  2811. use the typesym's name instead if it exists (if it doesn't, such as
  2812. for anynonymous procedure types in macpas/iso mode, then there cannot
  2813. be any recursive references to it either) }
  2814. if (pointeddef.typ<>procvardef) or
  2815. not assigned(pointeddef.typesym) then
  2816. GetTypeName:='^'+pointeddef.typename
  2817. else
  2818. GetTypeName:='^'+pointeddef.typesym.realname;
  2819. end;
  2820. function tpointerdef.pointer_arithmetic_int_type:tdef;
  2821. begin
  2822. result:=ptrsinttype;
  2823. end;
  2824. function tpointerdef.pointer_subtraction_result_type:tdef;
  2825. begin
  2826. result:=ptrsinttype;
  2827. end;
  2828. {****************************************************************************
  2829. TCLASSREFDEF
  2830. ****************************************************************************}
  2831. constructor tclassrefdef.create(def:tdef);
  2832. begin
  2833. inherited create(classrefdef,def);
  2834. end;
  2835. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2836. begin
  2837. inherited ppuload(classrefdef,ppufile);
  2838. ppuload_platform(ppufile);
  2839. end;
  2840. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2841. begin
  2842. inherited ppuwrite(ppufile);
  2843. writeentry(ppufile,ibclassrefdef);
  2844. end;
  2845. function tclassrefdef.getcopy:tstoreddef;
  2846. begin
  2847. if pointeddef.typ=forwarddef then
  2848. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2849. else
  2850. result:=cclassrefdef.create(pointeddef);
  2851. end;
  2852. function tclassrefdef.GetTypeName : string;
  2853. begin
  2854. GetTypeName:='Class Of '+pointeddef.typename;
  2855. end;
  2856. function tclassrefdef.is_publishable : boolean;
  2857. begin
  2858. result:=true;
  2859. end;
  2860. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2861. begin
  2862. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2863. result:=inherited rtti_mangledname(rt)
  2864. else
  2865. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2866. end;
  2867. procedure tclassrefdef.register_created_object_type;
  2868. begin
  2869. tobjectdef(pointeddef).register_created_classref_type;
  2870. end;
  2871. {***************************************************************************
  2872. TSETDEF
  2873. ***************************************************************************}
  2874. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2875. var
  2876. setallocbits: aint;
  2877. packedsavesize: aint;
  2878. actual_setalloc: ShortInt;
  2879. begin
  2880. inherited create(setdef);
  2881. elementdef:=def;
  2882. setmax:=high;
  2883. actual_setalloc:=current_settings.setalloc;
  2884. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  2885. if actual_setalloc=0 then
  2886. actual_setalloc:=1;
  2887. {$endif}
  2888. if (actual_setalloc=0) then
  2889. begin
  2890. setbase:=0;
  2891. if (high<32) then
  2892. savesize:=Sizeof(longint)
  2893. else if (high<256) then
  2894. savesize:=32
  2895. else
  2896. savesize:=(high+7) div 8
  2897. end
  2898. else
  2899. begin
  2900. setallocbits:=actual_setalloc*8;
  2901. setbase:=low and not(setallocbits-1);
  2902. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2903. savesize:=packedsavesize;
  2904. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  2905. if savesize=3 then
  2906. savesize:=4;
  2907. {$endif}
  2908. end;
  2909. end;
  2910. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2911. begin
  2912. inherited ppuload(setdef,ppufile);
  2913. ppufile.getderef(elementdefderef);
  2914. savesize:=ppufile.getaint;
  2915. setbase:=ppufile.getaint;
  2916. setmax:=ppufile.getaint;
  2917. ppuload_platform(ppufile);
  2918. end;
  2919. function tsetdef.getcopy : tstoreddef;
  2920. begin
  2921. result:=csetdef.create(elementdef,setbase,setmax);
  2922. { the copy might have been created with a different setalloc setting }
  2923. tsetdef(result).savesize:=savesize;
  2924. end;
  2925. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2926. begin
  2927. inherited ppuwrite(ppufile);
  2928. ppufile.putderef(elementdefderef);
  2929. ppufile.putaint(savesize);
  2930. ppufile.putaint(setbase);
  2931. ppufile.putaint(setmax);
  2932. writeentry(ppufile,ibsetdef);
  2933. end;
  2934. procedure tsetdef.buildderef;
  2935. begin
  2936. inherited buildderef;
  2937. elementdefderef.build(elementdef);
  2938. end;
  2939. procedure tsetdef.deref;
  2940. begin
  2941. inherited deref;
  2942. elementdef:=tdef(elementdefderef.resolve);
  2943. end;
  2944. function tsetdef.is_publishable : boolean;
  2945. begin
  2946. is_publishable:=savesize in [1,2,4];
  2947. end;
  2948. function tsetdef.GetTypeName : string;
  2949. begin
  2950. if assigned(elementdef) then
  2951. GetTypeName:='Set Of '+elementdef.typename
  2952. else
  2953. GetTypeName:='Empty Set';
  2954. end;
  2955. {***************************************************************************
  2956. TFORMALDEF
  2957. ***************************************************************************}
  2958. constructor tformaldef.create(Atyped:boolean);
  2959. begin
  2960. inherited create(formaldef);
  2961. typed:=Atyped;
  2962. savesize:=0;
  2963. end;
  2964. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2965. begin
  2966. inherited ppuload(formaldef,ppufile);
  2967. typed:=boolean(ppufile.getbyte);
  2968. savesize:=0;
  2969. ppuload_platform(ppufile);
  2970. end;
  2971. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2972. begin
  2973. inherited ppuwrite(ppufile);
  2974. ppufile.putbyte(byte(typed));
  2975. writeentry(ppufile,ibformaldef);
  2976. end;
  2977. function tformaldef.GetTypeName : string;
  2978. begin
  2979. if typed then
  2980. GetTypeName:='<Typed formal type>'
  2981. else
  2982. GetTypeName:='<Formal type>';
  2983. end;
  2984. {***************************************************************************
  2985. TARRAYDEF
  2986. ***************************************************************************}
  2987. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2988. begin
  2989. inherited create(arraydef);
  2990. lowrange:=l;
  2991. highrange:=h;
  2992. rangedef:=def;
  2993. _elementdef:=nil;
  2994. arrayoptions:=[];
  2995. symtable:=tarraysymtable.create(self);
  2996. end;
  2997. destructor tarraydef.destroy;
  2998. begin
  2999. symtable.free;
  3000. symtable:=nil;
  3001. inherited;
  3002. end;
  3003. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3004. begin
  3005. { use -1 so that the elecount will not overflow }
  3006. self.create(0,high(asizeint)-1,ptrsinttype);
  3007. arrayoptions:=[ado_IsConvertedPointer];
  3008. setelementdef(def.pointeddef);
  3009. end;
  3010. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3011. begin
  3012. inherited ppuload(arraydef,ppufile);
  3013. { the addresses are calculated later }
  3014. ppufile.getderef(_elementdefderef);
  3015. ppufile.getderef(rangedefderef);
  3016. lowrange:=ppufile.getasizeint;
  3017. highrange:=ppufile.getasizeint;
  3018. ppufile.getsmallset(arrayoptions);
  3019. ppuload_platform(ppufile);
  3020. symtable:=tarraysymtable.create(self);
  3021. tarraysymtable(symtable).ppuload(ppufile)
  3022. end;
  3023. function tarraydef.getcopy : tstoreddef;
  3024. begin
  3025. result:=carraydef.create(lowrange,highrange,rangedef);
  3026. tarraydef(result).arrayoptions:=arrayoptions;
  3027. tarraydef(result)._elementdef:=_elementdef;
  3028. end;
  3029. procedure tarraydef.buildderef;
  3030. begin
  3031. inherited buildderef;
  3032. tarraysymtable(symtable).buildderef;
  3033. _elementdefderef.build(_elementdef);
  3034. rangedefderef.build(rangedef);
  3035. end;
  3036. procedure tarraydef.deref;
  3037. begin
  3038. inherited deref;
  3039. tarraysymtable(symtable).deref;
  3040. _elementdef:=tdef(_elementdefderef.resolve);
  3041. rangedef:=tdef(rangedefderef.resolve);
  3042. end;
  3043. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3044. begin
  3045. inherited ppuwrite(ppufile);
  3046. ppufile.putderef(_elementdefderef);
  3047. ppufile.putderef(rangedefderef);
  3048. ppufile.putasizeint(lowrange);
  3049. ppufile.putasizeint(highrange);
  3050. ppufile.putsmallset(arrayoptions);
  3051. writeentry(ppufile,ibarraydef);
  3052. tarraysymtable(symtable).ppuwrite(ppufile);
  3053. end;
  3054. function tarraydef.elesize : asizeint;
  3055. begin
  3056. if (ado_IsBitPacked in arrayoptions) then
  3057. internalerror(2006080101);
  3058. if assigned(_elementdef) then
  3059. result:=_elementdef.size
  3060. else
  3061. result:=0;
  3062. end;
  3063. function tarraydef.elepackedbitsize : asizeint;
  3064. begin
  3065. if not(ado_IsBitPacked in arrayoptions) then
  3066. internalerror(2006080102);
  3067. if assigned(_elementdef) then
  3068. result:=_elementdef.packedbitsize
  3069. else
  3070. result:=0;
  3071. end;
  3072. function tarraydef.elecount : asizeuint;
  3073. var
  3074. qhigh,qlow : qword;
  3075. begin
  3076. if ado_IsDynamicArray in arrayoptions then
  3077. begin
  3078. result:=0;
  3079. exit;
  3080. end;
  3081. if (highrange>0) and (lowrange<0) then
  3082. begin
  3083. qhigh:=highrange;
  3084. qlow:=qword(-lowrange);
  3085. { prevent overflow, return 0 to indicate overflow }
  3086. if qhigh+qlow>qword(high(asizeint)-1) then
  3087. result:=0
  3088. else
  3089. result:=qhigh+qlow+1;
  3090. end
  3091. else
  3092. result:=int64(highrange)-lowrange+1;
  3093. end;
  3094. function tarraydef.size : asizeint;
  3095. var
  3096. cachedelecount : asizeuint;
  3097. cachedelesize : asizeint;
  3098. begin
  3099. if ado_IsDynamicArray in arrayoptions then
  3100. begin
  3101. size:=voidpointertype.size;
  3102. exit;
  3103. end;
  3104. { Tarraydef.size may never be called for an open array! }
  3105. if highrange<lowrange then
  3106. internalerror(99080501);
  3107. if not (ado_IsBitPacked in arrayoptions) then
  3108. cachedelesize:=elesize
  3109. else
  3110. cachedelesize := elepackedbitsize;
  3111. cachedelecount:=elecount;
  3112. if (cachedelesize = 0) then
  3113. begin
  3114. size := 0;
  3115. exit;
  3116. end;
  3117. if (cachedelecount = 0) then
  3118. begin
  3119. size := -1;
  3120. exit;
  3121. end;
  3122. { prevent overflow, return -1 to indicate overflow }
  3123. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3124. if (cachedelecount > asizeuint(high(asizeint))) or
  3125. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3126. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3127. accessing the array, see ncgmem (PFV) }
  3128. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3129. begin
  3130. result:=-1;
  3131. exit;
  3132. end;
  3133. result:=cachedelesize*asizeint(cachedelecount);
  3134. if (ado_IsBitPacked in arrayoptions) then
  3135. { can't just add 7 and divide by 8, because that may overflow }
  3136. result:=result div 8 + ord((result mod 8)<>0);
  3137. end;
  3138. procedure tarraydef.setelementdef(def:tdef);
  3139. begin
  3140. _elementdef:=def;
  3141. if not(
  3142. (ado_IsDynamicArray in arrayoptions) or
  3143. (ado_IsConvertedPointer in arrayoptions) or
  3144. (highrange<lowrange)
  3145. ) and
  3146. (size=-1) then
  3147. Message(sym_e_segment_too_large);
  3148. end;
  3149. function tarraydef.alignment : shortint;
  3150. begin
  3151. { alignment of dyn. arrays doesn't depend on the element size }
  3152. if (ado_IsDynamicArray in arrayoptions) then
  3153. alignment:=size_2_align(voidpointertype.size)
  3154. { alignment is the target alignment for the used load size }
  3155. else if (ado_IsBitPacked in arrayoptions) and
  3156. (elementdef.typ in [enumdef,orddef]) then
  3157. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3158. { alignment is the alignment of the elements }
  3159. else
  3160. alignment:=elementdef.alignment
  3161. end;
  3162. function tarraydef.needs_inittable : boolean;
  3163. begin
  3164. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3165. end;
  3166. function tarraydef.needs_separate_initrtti : boolean;
  3167. begin
  3168. if ado_IsBitPacked in arrayoptions then
  3169. result:=false
  3170. else
  3171. result:=elementdef.needs_separate_initrtti;
  3172. end;
  3173. function tarraydef.GetTypeName : string;
  3174. begin
  3175. if (ado_IsConstString in arrayoptions) then
  3176. result:='Constant String'
  3177. else if (ado_isarrayofconst in arrayoptions) or
  3178. (ado_isConstructor in arrayoptions) then
  3179. begin
  3180. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3181. GetTypeName:='Array Of Const'
  3182. else
  3183. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3184. end
  3185. else if (ado_IsDynamicArray in arrayoptions) then
  3186. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3187. else if ((highrange=-1) and (lowrange=0)) then
  3188. GetTypeName:='{Open} Array Of '+elementdef.typename
  3189. else
  3190. begin
  3191. result := '';
  3192. if (ado_IsBitPacked in arrayoptions) then
  3193. result:='BitPacked ';
  3194. if rangedef.typ=enumdef then
  3195. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3196. else
  3197. result:=result+'Array['+tostr(lowrange)+'..'+
  3198. tostr(highrange)+'] Of '+elementdef.typename
  3199. end;
  3200. end;
  3201. function tarraydef.getmangledparaname : TSymStr;
  3202. begin
  3203. if ado_isarrayofconst in arrayoptions then
  3204. getmangledparaname:='array_of_const'
  3205. else
  3206. if ((highrange=-1) and (lowrange=0)) then
  3207. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3208. else
  3209. internalerror(200204176);
  3210. end;
  3211. function tarraydef.is_publishable : boolean;
  3212. begin
  3213. Result:=ado_IsDynamicArray in arrayoptions;
  3214. end;
  3215. {***************************************************************************
  3216. tabstractrecorddef
  3217. ***************************************************************************}
  3218. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  3219. begin
  3220. inherited create(dt);
  3221. objname:=stringdup(upper(n));
  3222. objrealname:=stringdup(n);
  3223. objectoptions:=[];
  3224. if assigned(current_module.namespace) then
  3225. begin
  3226. import_lib:=stringdup(current_module.namespace^);
  3227. replace(import_lib^,'.','/');
  3228. end;
  3229. end;
  3230. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3231. begin
  3232. inherited ppuload(dt,ppufile);
  3233. objrealname:=stringdup(ppufile.getstring);
  3234. objname:=stringdup(upper(objrealname^));
  3235. import_lib:=stringdup(ppufile.getstring);
  3236. { only used for external C++ classes and Java classes/records }
  3237. if (import_lib^='') then
  3238. stringdispose(import_lib);
  3239. ppufile.getsmallset(objectoptions);
  3240. end;
  3241. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3242. begin
  3243. inherited ppuwrite(ppufile);
  3244. ppufile.putstring(objrealname^);
  3245. if assigned(import_lib) then
  3246. ppufile.putstring(import_lib^)
  3247. else
  3248. ppufile.putstring('');
  3249. ppufile.putsmallset(objectoptions);
  3250. end;
  3251. destructor tabstractrecorddef.destroy;
  3252. begin
  3253. stringdispose(objname);
  3254. stringdispose(objrealname);
  3255. stringdispose(import_lib);
  3256. tcinitcode.free;
  3257. inherited destroy;
  3258. end;
  3259. procedure tabstractrecorddef.buildderefimpl;
  3260. begin
  3261. inherited buildderefimpl;
  3262. if not (df_copied_def in defoptions) then
  3263. tstoredsymtable(symtable).buildderefimpl;
  3264. end;
  3265. procedure tabstractrecorddef.derefimpl;
  3266. begin
  3267. inherited derefimpl;
  3268. if not (df_copied_def in defoptions) then
  3269. tstoredsymtable(symtable).derefimpl;
  3270. end;
  3271. procedure tabstractrecorddef.check_forwards;
  3272. begin
  3273. { the defs of a copied def are defined for the original type only }
  3274. if not(df_copied_def in defoptions) then
  3275. tstoredsymtable(symtable).check_forwards;
  3276. end;
  3277. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3278. var
  3279. i: longint;
  3280. sym: tsym;
  3281. begin
  3282. for i:=0 to symtable.SymList.Count-1 do
  3283. begin
  3284. sym:=tsym(symtable.SymList[i]);
  3285. if sym.typ=procsym then
  3286. begin
  3287. result:=tprocsym(sym).find_procdef_bytype(pt);
  3288. if assigned(result) then
  3289. exit;
  3290. end;
  3291. end;
  3292. result:=nil;
  3293. end;
  3294. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3295. begin
  3296. if t=gs_record then
  3297. GetSymtable:=symtable
  3298. else
  3299. GetSymtable:=nil;
  3300. end;
  3301. function tabstractrecorddef.is_packed:boolean;
  3302. begin
  3303. result:=tabstractrecordsymtable(symtable).is_packed;
  3304. end;
  3305. function tabstractrecorddef.RttiName: string;
  3306. function generate_full_paramname(maxlength:longint):string;
  3307. const
  3308. commacount : array[boolean] of longint = (0,1);
  3309. var
  3310. fullparas,
  3311. paramname : ansistring;
  3312. module : tmodule;
  3313. sym : ttypesym;
  3314. i : longint;
  3315. begin
  3316. { we want at least enough space for an ellipsis }
  3317. if maxlength<3 then
  3318. internalerror(2014121203);
  3319. fullparas:='';
  3320. for i:=0 to genericparas.count-1 do
  3321. begin
  3322. sym:=ttypesym(genericparas[i]);
  3323. module:=find_module_from_symtable(sym.owner);
  3324. if not assigned(module) then
  3325. internalerror(2014121202);
  3326. paramname:=module.realmodulename^;
  3327. if sym.typedef.typ in [objectdef,recorddef] then
  3328. paramname:=paramname+'.'+tabstractrecorddef(sym.typedef).rttiname
  3329. else
  3330. paramname:=paramname+'.'+sym.typedef.typename;
  3331. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  3332. begin
  3333. if i>0 then
  3334. fullparas:=fullparas+',...'
  3335. else
  3336. fullparas:=fullparas+'...';
  3337. break;
  3338. end;
  3339. { could we fit an ellipsis after this parameter if it should be too long? }
  3340. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  3341. begin
  3342. { then omit already this parameter }
  3343. if i>0 then
  3344. fullparas:=fullparas+',...'
  3345. else
  3346. fullparas:=fullparas+'...';
  3347. break;
  3348. end;
  3349. if i>0 then
  3350. fullparas:=fullparas+',';
  3351. fullparas:=fullparas+paramname;
  3352. end;
  3353. result:=fullparas;
  3354. end;
  3355. var
  3356. nongeneric,
  3357. basename : string;
  3358. i,
  3359. remlength,
  3360. paramcount,
  3361. crcidx : longint;
  3362. begin
  3363. if rttistring='' then
  3364. begin
  3365. if is_specialization then
  3366. begin
  3367. rttistring:=OwnerHierarchyName;
  3368. { there should be two $ characters, one before the CRC and one before the count }
  3369. crcidx:=-1;
  3370. for i:=length(objrealname^) downto 1 do
  3371. if objrealname^[i]='$' then
  3372. begin
  3373. crcidx:=i;
  3374. break;
  3375. end;
  3376. if crcidx<0 then
  3377. internalerror(2014121201);
  3378. basename:=copy(objrealname^,1,crcidx-1);
  3379. split_generic_name(basename,nongeneric,paramcount);
  3380. rttistring:=rttistring+nongeneric+'<';
  3381. remlength:=255-length(rttistring)-1;
  3382. if remlength<4 then
  3383. rttistring:=rttistring+'>'
  3384. else
  3385. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  3386. end
  3387. else
  3388. if is_generic then
  3389. begin
  3390. rttistring:=OwnerHierarchyName;
  3391. split_generic_name(objrealname^,nongeneric,paramcount);
  3392. rttistring:=rttistring+nongeneric+'<';
  3393. { we don't want any ',' if there is only one parameter }
  3394. for i:=0 to paramcount-0 do
  3395. rttistring:=rttistring+',';
  3396. rttistring:=rttistring+'>';
  3397. end
  3398. else
  3399. rttistring:=OwnerHierarchyName+objrealname^;
  3400. end;
  3401. result:=rttistring;
  3402. end;
  3403. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3404. var
  3405. sym : tsym;
  3406. i : integer;
  3407. pd : tprocdef;
  3408. hashedid : THashedIDString;
  3409. begin
  3410. result:=nil;
  3411. hashedid.id:='GETENUMERATOR';
  3412. sym:=tsym(symtable.FindWithHash(hashedid));
  3413. if assigned(sym) and (sym.typ=procsym) then
  3414. begin
  3415. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3416. begin
  3417. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3418. if (pd.proctypeoption = potype_function) and
  3419. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3420. (pd.visibility >= vis_public) then
  3421. begin
  3422. result:=pd;
  3423. exit;
  3424. end;
  3425. end;
  3426. end;
  3427. end;
  3428. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3429. var
  3430. sym : tsym;
  3431. i : integer;
  3432. pd : tprocdef;
  3433. hashedid : THashedIDString;
  3434. begin
  3435. result:=nil;
  3436. // first search for po_enumerator_movenext method modifier
  3437. // then search for public function MoveNext: Boolean
  3438. for i:=0 to symtable.SymList.Count-1 do
  3439. begin
  3440. sym:=TSym(symtable.SymList[i]);
  3441. if (sym.typ=procsym) then
  3442. begin
  3443. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3444. if assigned(pd) then
  3445. begin
  3446. result:=pd;
  3447. exit;
  3448. end;
  3449. end;
  3450. end;
  3451. hashedid.id:='MOVENEXT';
  3452. sym:=tsym(symtable.FindWithHash(hashedid));
  3453. if assigned(sym) and (sym.typ=procsym) then
  3454. begin
  3455. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3456. begin
  3457. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3458. if (pd.proctypeoption = potype_function) and
  3459. is_boolean(pd.returndef) and
  3460. (pd.minparacount = 0) and
  3461. (pd.visibility >= vis_public) then
  3462. begin
  3463. result:=pd;
  3464. exit;
  3465. end;
  3466. end;
  3467. end;
  3468. end;
  3469. function tabstractrecorddef.search_enumerator_current: tsym;
  3470. var
  3471. sym: tsym;
  3472. i: integer;
  3473. hashedid : THashedIDString;
  3474. begin
  3475. result:=nil;
  3476. // first search for ppo_enumerator_current property modifier
  3477. // then search for public property Current
  3478. for i:=0 to symtable.SymList.Count-1 do
  3479. begin
  3480. sym:=TSym(symtable.SymList[i]);
  3481. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3482. begin
  3483. result:=sym;
  3484. exit;
  3485. end;
  3486. end;
  3487. hashedid.id:='CURRENT';
  3488. sym:=tsym(symtable.FindWithHash(hashedid));
  3489. if assigned(sym) and (sym.typ=propertysym) and
  3490. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3491. begin
  3492. result:=sym;
  3493. exit;
  3494. end;
  3495. end;
  3496. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3497. var
  3498. st: tsymtable;
  3499. enclosingdef: tdef;
  3500. begin
  3501. if typ=objectdef then
  3502. result:=tobjectdef(self).objextname^
  3503. else if assigned(typesym) then
  3504. result:=typesym.realname
  3505. { have to generate anonymous nested type in current unit/class/record }
  3506. else
  3507. internalerror(2011032601);
  3508. { in case of specializations, add some extras to prevent name conflicts
  3509. with nested classes }
  3510. if df_specialization in defoptions then
  3511. result:='$'+result+'$specialization$';
  3512. st:=owner;
  3513. while assigned(st) and
  3514. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3515. begin
  3516. { nested classes are named as "OuterClass$InnerClass" }
  3517. enclosingdef:=tdef(st.defowner);
  3518. if enclosingdef.typ=procdef then
  3519. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3520. else if enclosingdef.typ=objectdef then
  3521. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3522. else if assigned(enclosingdef.typesym) then
  3523. result:=enclosingdef.typesym.realname+'$'+result
  3524. else
  3525. internalerror(2011060305);
  3526. st:=enclosingdef.owner;
  3527. end;
  3528. if with_package_name and
  3529. assigned(import_lib) then
  3530. result:=import_lib^+'/'+result;
  3531. end;
  3532. function tabstractrecorddef.contains_float_field: boolean;
  3533. var
  3534. i : longint;
  3535. begin
  3536. result:=true;
  3537. for i:=0 to symtable.symlist.count-1 do
  3538. begin
  3539. if tsym(symtable.symlist[i]).typ<>fieldvarsym then
  3540. continue;
  3541. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) and
  3542. tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3543. exit;
  3544. end;
  3545. result:=false;
  3546. end;
  3547. {***************************************************************************
  3548. trecorddef
  3549. ***************************************************************************}
  3550. constructor trecorddef.create(const n:string; p:TSymtable);
  3551. begin
  3552. inherited create(n,recorddef);
  3553. symtable:=p;
  3554. { we can own the symtable only if nobody else owns a copy so far }
  3555. if symtable.refcount=1 then
  3556. symtable.defowner:=self;
  3557. isunion:=false;
  3558. end;
  3559. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3560. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3561. var
  3562. i,j : longint;
  3563. begin
  3564. if ppufile.getbyte=1 then
  3565. begin
  3566. new(variantrecdesc);
  3567. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3568. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3569. for i:=0 to high(variantrecdesc^.branches) do
  3570. begin
  3571. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3572. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3573. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3574. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3575. end;
  3576. end
  3577. else
  3578. variantrecdesc:=nil;
  3579. end;
  3580. begin
  3581. inherited ppuload(recorddef,ppufile);
  3582. if df_copied_def in defoptions then
  3583. begin
  3584. ppufile.getderef(cloneddefderef);
  3585. ppuload_platform(ppufile);
  3586. end
  3587. else
  3588. begin
  3589. ppuload_platform(ppufile);
  3590. symtable:=trecordsymtable.create(objrealname^,0);
  3591. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3592. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3593. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3594. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3595. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3596. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3597. trecordsymtable(symtable).ppuload(ppufile);
  3598. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3599. but because iso mode supports no units, there is no need to store the variantrecdesc
  3600. in the ppu
  3601. }
  3602. // readvariantrecdesc(variantrecdesc);
  3603. { requires usefieldalignment to be set }
  3604. symtable.defowner:=self;
  3605. end;
  3606. isunion:=false;
  3607. end;
  3608. destructor trecorddef.destroy;
  3609. begin
  3610. if assigned(symtable) then
  3611. begin
  3612. symtable.free;
  3613. symtable:=nil;
  3614. end;
  3615. inherited destroy;
  3616. end;
  3617. function trecorddef.getcopy : tstoreddef;
  3618. begin
  3619. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3620. trecorddef(result).isunion:=isunion;
  3621. include(trecorddef(result).defoptions,df_copied_def);
  3622. if assigned(tcinitcode) then
  3623. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3624. if assigned(import_lib) then
  3625. trecorddef(result).import_lib:=stringdup(import_lib^);
  3626. end;
  3627. function trecorddef.needs_inittable : boolean;
  3628. begin
  3629. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3630. end;
  3631. function trecorddef.needs_separate_initrtti : boolean;
  3632. begin
  3633. result:=true;
  3634. end;
  3635. procedure trecorddef.buildderef;
  3636. begin
  3637. inherited buildderef;
  3638. if df_copied_def in defoptions then
  3639. cloneddefderef.build(symtable.defowner)
  3640. else
  3641. tstoredsymtable(symtable).buildderef;
  3642. end;
  3643. procedure trecorddef.deref;
  3644. begin
  3645. inherited deref;
  3646. { now dereference the definitions }
  3647. if df_copied_def in defoptions then
  3648. begin
  3649. cloneddef:=trecorddef(cloneddefderef.resolve);
  3650. symtable:=cloneddef.symtable.getcopy;
  3651. end
  3652. else
  3653. tstoredsymtable(symtable).deref;
  3654. { assign TGUID? load only from system unit }
  3655. if not(assigned(rec_tguid)) and
  3656. (upper(typename)='TGUID') and
  3657. assigned(owner) and
  3658. assigned(owner.name) and
  3659. (owner.name^='SYSTEM') then
  3660. rec_tguid:=self;
  3661. { assign JMP_BUF? load only from system unit }
  3662. if not(assigned(rec_jmp_buf)) and
  3663. (upper(typename)='JMP_BUF') and
  3664. assigned(owner) and
  3665. assigned(owner.name) and
  3666. (owner.name^='SYSTEM') then
  3667. rec_jmp_buf:=self;
  3668. end;
  3669. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3670. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3671. var
  3672. i,j : longint;
  3673. begin
  3674. if assigned(variantrecdesc) then
  3675. begin
  3676. ppufile.putbyte(1);
  3677. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3678. ppufile.putasizeint(length(variantrecdesc^.branches));
  3679. for i:=0 to high(variantrecdesc^.branches) do
  3680. begin
  3681. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3682. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3683. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3684. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3685. end;
  3686. end
  3687. else
  3688. ppufile.putbyte(0);
  3689. end;
  3690. begin
  3691. inherited ppuwrite(ppufile);
  3692. if df_copied_def in defoptions then
  3693. ppufile.putderef(cloneddefderef)
  3694. else
  3695. begin
  3696. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3697. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3698. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3699. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3700. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3701. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3702. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3703. but because iso mode supports no units, there is no need to store the variantrecdesc
  3704. in the ppu
  3705. }
  3706. // writevariantrecdesc(variantrecdesc);
  3707. end;
  3708. writeentry(ppufile,ibrecorddef);
  3709. if not(df_copied_def in defoptions) then
  3710. trecordsymtable(symtable).ppuwrite(ppufile);
  3711. end;
  3712. function trecorddef.size:asizeint;
  3713. begin
  3714. result:=trecordsymtable(symtable).datasize;
  3715. end;
  3716. function trecorddef.alignment:shortint;
  3717. begin
  3718. alignment:=trecordsymtable(symtable).recordalignment;
  3719. end;
  3720. function trecorddef.padalignment:shortint;
  3721. begin
  3722. padalignment := trecordsymtable(symtable).padalignment;
  3723. end;
  3724. function trecorddef.GetTypeName : string;
  3725. begin
  3726. GetTypeName:='<record type>'
  3727. end;
  3728. {***************************************************************************
  3729. TABSTRACTPROCDEF
  3730. ***************************************************************************}
  3731. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  3732. begin
  3733. inherited create(dt);
  3734. parast:=tparasymtable.create(self,level);
  3735. paras:=nil;
  3736. minparacount:=0;
  3737. maxparacount:=0;
  3738. proctypeoption:=potype_none;
  3739. proccalloption:=pocall_none;
  3740. procoptions:=[];
  3741. returndef:=voidtype;
  3742. savesize:=sizeof(pint);
  3743. callerargareasize:=0;
  3744. calleeargareasize:=0;
  3745. has_paraloc_info:=callnoside;
  3746. funcretloc[callerside].init;
  3747. funcretloc[calleeside].init;
  3748. check_mark_as_nested;
  3749. end;
  3750. destructor tabstractprocdef.destroy;
  3751. begin
  3752. if assigned(paras) then
  3753. begin
  3754. {$ifdef MEMDEBUG}
  3755. memprocpara.start;
  3756. {$endif MEMDEBUG}
  3757. paras.free;
  3758. paras:=nil;
  3759. {$ifdef MEMDEBUG}
  3760. memprocpara.stop;
  3761. {$endif MEMDEBUG}
  3762. end;
  3763. if assigned(parast) then
  3764. begin
  3765. {$ifdef MEMDEBUG}
  3766. memprocparast.start;
  3767. {$endif MEMDEBUG}
  3768. parast.free;
  3769. parast:=nil;
  3770. {$ifdef MEMDEBUG}
  3771. memprocparast.stop;
  3772. {$endif MEMDEBUG}
  3773. end;
  3774. funcretloc[callerside].done;
  3775. funcretloc[calleeside].done;
  3776. inherited destroy;
  3777. end;
  3778. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3779. begin
  3780. if (tsym(p).typ<>paravarsym) then
  3781. exit;
  3782. inc(plongint(arg)^);
  3783. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3784. begin
  3785. if not assigned(tparavarsym(p).defaultconstsym) then
  3786. inc(minparacount);
  3787. inc(maxparacount);
  3788. end;
  3789. end;
  3790. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3791. begin
  3792. if (tsym(p).typ<>paravarsym) then
  3793. exit;
  3794. paras.add(p);
  3795. end;
  3796. procedure tabstractprocdef.calcparas;
  3797. var
  3798. paracount : longint;
  3799. begin
  3800. { This can already be assigned when
  3801. we need to reresolve this unit (PFV) }
  3802. if assigned(paras) then
  3803. paras.free;
  3804. paras:=tparalist.create(false);
  3805. paracount:=0;
  3806. minparacount:=0;
  3807. maxparacount:=0;
  3808. parast.SymList.ForEachCall(@count_para,@paracount);
  3809. paras.capacity:=paracount;
  3810. { Insert parameters in table }
  3811. parast.SymList.ForEachCall(@insert_para,nil);
  3812. { Order parameters }
  3813. paras.sortparas;
  3814. end;
  3815. procedure tabstractprocdef.buildderef;
  3816. begin
  3817. { released procdef? }
  3818. if not assigned(parast) then
  3819. exit;
  3820. inherited buildderef;
  3821. returndefderef.build(returndef);
  3822. if po_explicitparaloc in procoptions then
  3823. funcretloc[callerside].buildderef;
  3824. { parast }
  3825. tparasymtable(parast).buildderef;
  3826. end;
  3827. procedure tabstractprocdef.deref;
  3828. begin
  3829. inherited deref;
  3830. returndef:=tdef(returndefderef.resolve);
  3831. if po_explicitparaloc in procoptions then
  3832. begin
  3833. funcretloc[callerside].deref;
  3834. has_paraloc_info:=callerside;
  3835. end
  3836. else
  3837. begin
  3838. { deref is called after loading from a ppu, but also after another
  3839. unit has been reloaded/recompiled and all references must be
  3840. re-resolved. Since the funcretloc contains a reference to a tdef,
  3841. reset it so that we won't try to access the stale def }
  3842. funcretloc[callerside].init;
  3843. has_paraloc_info:=callnoside;
  3844. end;
  3845. { parast }
  3846. tparasymtable(parast).deref;
  3847. { recalculated parameters }
  3848. calcparas;
  3849. end;
  3850. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3851. begin
  3852. inherited ppuload(dt,ppufile);
  3853. parast:=nil;
  3854. Paras:=nil;
  3855. minparacount:=0;
  3856. maxparacount:=0;
  3857. ppufile.getderef(returndefderef);
  3858. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3859. proccalloption:=tproccalloption(ppufile.getbyte);
  3860. ppufile.getnormalset(procoptions);
  3861. funcretloc[callerside].init;
  3862. if po_explicitparaloc in procoptions then
  3863. funcretloc[callerside].ppuload(ppufile);
  3864. savesize:=sizeof(pint);
  3865. if (po_explicitparaloc in procoptions) then
  3866. has_paraloc_info:=callerside;
  3867. end;
  3868. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3869. var
  3870. oldintfcrc : boolean;
  3871. begin
  3872. { released procdef? }
  3873. if not assigned(parast) then
  3874. exit;
  3875. inherited ppuwrite(ppufile);
  3876. ppufile.putderef(returndefderef);
  3877. oldintfcrc:=ppufile.do_interface_crc;
  3878. ppufile.do_interface_crc:=false;
  3879. ppufile.putbyte(ord(proctypeoption));
  3880. ppufile.putbyte(ord(proccalloption));
  3881. ppufile.putnormalset(procoptions);
  3882. ppufile.do_interface_crc:=oldintfcrc;
  3883. if (po_explicitparaloc in procoptions) then
  3884. funcretloc[callerside].ppuwrite(ppufile);
  3885. end;
  3886. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3887. var
  3888. hs,s : ansistring;
  3889. hp : TParavarsym;
  3890. hpc : tconstsym;
  3891. first : boolean;
  3892. i : integer;
  3893. begin
  3894. s:='';
  3895. first:=true;
  3896. for i:=0 to paras.count-1 do
  3897. begin
  3898. hp:=tparavarsym(paras[i]);
  3899. if not(vo_is_hidden_para in hp.varoptions) or
  3900. (pno_showhidden in pno) then
  3901. begin
  3902. if first then
  3903. begin
  3904. s:=s+'(';
  3905. first:=false;
  3906. end
  3907. else
  3908. s:=s+';';
  3909. if vo_is_hidden_para in hp.varoptions then
  3910. s:=s+'<';
  3911. case hp.varspez of
  3912. vs_var :
  3913. s:=s+'var ';
  3914. vs_const :
  3915. s:=s+'const ';
  3916. vs_out :
  3917. s:=s+'out ';
  3918. vs_constref :
  3919. s:=s+'constref ';
  3920. end;
  3921. if (pno_paranames in pno) then
  3922. s:=s+hp.realname+':';
  3923. if hp.univpara then
  3924. s:=s+'univ ';
  3925. if assigned(hp.vardef.typesym) then
  3926. begin
  3927. hs:=hp.vardef.typesym.realname;
  3928. if hs[1]<>'$' then
  3929. s:=s+hp.vardef.OwnerHierarchyName+hs
  3930. else
  3931. s:=s+hp.vardef.GetTypeName;
  3932. end
  3933. else
  3934. s:=s+hp.vardef.GetTypeName;
  3935. { default value }
  3936. if assigned(hp.defaultconstsym) then
  3937. begin
  3938. hpc:=tconstsym(hp.defaultconstsym);
  3939. hs:='';
  3940. case hpc.consttyp of
  3941. conststring,
  3942. constresourcestring :
  3943. begin
  3944. If hpc.value.len>0 then
  3945. begin
  3946. setLength(hs,hpc.value.len);
  3947. { don't write past the end of hs if the constant
  3948. is > 255 chars }
  3949. move(hpc.value.valueptr^,hs[1],length(hs));
  3950. { make sure that constant strings with newline chars
  3951. don't create a linebreak in the assembler code,
  3952. since comments are line-based. Also remove nulls
  3953. because the comments are written as a pchar. }
  3954. ReplaceCase(hs,#0,'.');
  3955. ReplaceCase(hs,#10,'.');
  3956. ReplaceCase(hs,#13,'.');
  3957. end;
  3958. end;
  3959. constreal :
  3960. str(pbestreal(hpc.value.valueptr)^,hs);
  3961. constpointer :
  3962. hs:=tostr(hpc.value.valueordptr);
  3963. constord :
  3964. begin
  3965. if is_boolean(hpc.constdef) then
  3966. begin
  3967. if hpc.value.valueord<>0 then
  3968. hs:='TRUE'
  3969. else
  3970. hs:='FALSE';
  3971. end
  3972. else
  3973. hs:=tostr(hpc.value.valueord);
  3974. end;
  3975. constnil :
  3976. hs:='nil';
  3977. constset :
  3978. hs:='<set>';
  3979. end;
  3980. if hs<>'' then
  3981. s:=s+'="'+hs+'"';
  3982. end;
  3983. if vo_is_hidden_para in hp.varoptions then
  3984. s:=s+'>';
  3985. end;
  3986. end;
  3987. if not first then
  3988. s:=s+')';
  3989. if (po_varargs in procoptions) then
  3990. s:=s+';VarArgs';
  3991. typename_paras:=s;
  3992. end;
  3993. function tabstractprocdef.is_methodpointer:boolean;
  3994. begin
  3995. result:=false;
  3996. end;
  3997. function tabstractprocdef.is_addressonly:boolean;
  3998. begin
  3999. result:=true;
  4000. end;
  4001. function tabstractprocdef.no_self_node: boolean;
  4002. begin
  4003. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  4004. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  4005. end;
  4006. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  4007. var
  4008. j, nestinglevel: longint;
  4009. pvs, npvs: tparavarsym;
  4010. begin
  4011. nestinglevel:=parast.symtablelevel;
  4012. if newtyp=procdef then
  4013. begin
  4014. if (copytyp<>pc_bareproc) then
  4015. result:=cprocdef.create(nestinglevel)
  4016. else
  4017. result:=cprocdef.create(normal_function_level);
  4018. tprocdef(result).visibility:=vis_public;
  4019. end
  4020. else
  4021. begin
  4022. result:=cprocvardef.create(nestinglevel);
  4023. end;
  4024. tabstractprocdef(result).returndef:=returndef;
  4025. tabstractprocdef(result).returndefderef:=returndefderef;
  4026. pvs:=nil;
  4027. npvs:=nil;
  4028. for j:=0 to parast.symlist.count-1 do
  4029. begin
  4030. case tsym(parast.symlist[j]).typ of
  4031. paravarsym:
  4032. begin
  4033. pvs:=tparavarsym(parast.symlist[j]);
  4034. { in case of bare proc, don't copy self, vmt or framepointer
  4035. parameters }
  4036. if (copytyp=pc_bareproc) and
  4037. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  4038. continue;
  4039. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  4040. pvs.vardef,pvs.varoptions);
  4041. npvs.defaultconstsym:=pvs.defaultconstsym;
  4042. tabstractprocdef(result).parast.insert(npvs);
  4043. end;
  4044. constsym:
  4045. begin
  4046. // ignore, reuse original constym. Should also be duplicated
  4047. // be safe though
  4048. end;
  4049. symconst.typesym:
  4050. begin
  4051. // reuse original, part of generic declaration
  4052. end
  4053. else
  4054. internalerror(201160604);
  4055. end;
  4056. end;
  4057. tabstractprocdef(result).savesize:=savesize;
  4058. tabstractprocdef(result).proctypeoption:=proctypeoption;
  4059. tabstractprocdef(result).proccalloption:=proccalloption;
  4060. tabstractprocdef(result).procoptions:=procoptions;
  4061. if (copytyp=pc_bareproc) then
  4062. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  4063. if newtyp=procvardef then
  4064. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  4065. tabstractprocdef(result).callerargareasize:=callerargareasize;
  4066. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  4067. tabstractprocdef(result).maxparacount:=maxparacount;
  4068. tabstractprocdef(result).minparacount:=minparacount;
  4069. if po_explicitparaloc in procoptions then
  4070. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  4071. { recalculate parameter info }
  4072. tabstractprocdef(result).has_paraloc_info:=callnoside;
  4073. {$ifdef m68k}
  4074. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  4075. {$endif}
  4076. if (typ=procdef) and
  4077. (newtyp=procvardef) and
  4078. (owner.symtabletype=ObjectSymtable) then
  4079. include(tprocvardef(result).procoptions,po_methodpointer);
  4080. end;
  4081. procedure tabstractprocdef.check_mark_as_nested;
  4082. begin
  4083. { nested procvars require that nested functions use the Delphi-style
  4084. nested procedure calling convention }
  4085. if (parast.symtablelevel>normal_function_level) and
  4086. (m_nested_procvars in current_settings.modeswitches) then
  4087. include(procoptions,po_delphi_nested_cc);
  4088. end;
  4089. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  4090. begin
  4091. if (side in [callerside,callbothsides]) and
  4092. not(has_paraloc_info in [callerside,callbothsides]) then
  4093. begin
  4094. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  4095. if has_paraloc_info in [calleeside,callbothsides] then
  4096. has_paraloc_info:=callbothsides
  4097. else
  4098. has_paraloc_info:=callerside;
  4099. end;
  4100. if (side in [calleeside,callbothsides]) and
  4101. not(has_paraloc_info in [calleeside,callbothsides]) then
  4102. begin
  4103. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  4104. if has_paraloc_info in [callerside,callbothsides] then
  4105. has_paraloc_info:=callbothsides
  4106. else
  4107. has_paraloc_info:=calleeside;
  4108. end;
  4109. end;
  4110. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  4111. var
  4112. p: tparavarsym;
  4113. ploc: PCGParalocation;
  4114. i: longint;
  4115. begin
  4116. result:=false;
  4117. init_paraloc_info(side);
  4118. for i:=0 to parast.SymList.Count-1 do
  4119. if tsym(parast.SymList[i]).typ=paravarsym then
  4120. begin
  4121. p:=tparavarsym(parast.SymList[i]);
  4122. { check if no parameter is located on the stack }
  4123. if is_open_array(p.vardef) or
  4124. is_array_of_const(p.vardef) then
  4125. begin
  4126. result:=true;
  4127. exit;
  4128. end;
  4129. ploc:=p.paraloc[side].location;
  4130. while assigned(ploc) do
  4131. begin
  4132. if (ploc^.loc=LOC_REFERENCE) then
  4133. begin
  4134. result:=true;
  4135. exit
  4136. end;
  4137. ploc:=ploc^.next;
  4138. end;
  4139. end;
  4140. end;
  4141. function tabstractprocdef.is_pushleftright: boolean;
  4142. begin
  4143. result:=false;
  4144. end;
  4145. function tabstractprocdef.address_type: tdef;
  4146. begin
  4147. result:=voidcodepointertype;
  4148. end;
  4149. procedure tabstractprocdef.declared_far;
  4150. begin
  4151. Message1(parser_w_proc_directive_ignored,'FAR');
  4152. end;
  4153. procedure tabstractprocdef.declared_near;
  4154. begin
  4155. Message1(parser_w_proc_directive_ignored,'NEAR');
  4156. end;
  4157. {***************************************************************************
  4158. TPROCDEF
  4159. ***************************************************************************}
  4160. function tprocdef.GetResultName: PShortString;
  4161. begin
  4162. if not assigned(implprocdefinfo) then
  4163. internalerror(2014010301);
  4164. result:=implprocdefinfo^.resultname;
  4165. end;
  4166. procedure tprocdef.SetResultName(AValue: PShortString);
  4167. begin
  4168. if not assigned(implprocdefinfo) then
  4169. internalerror(2014010302);
  4170. implprocdefinfo^.resultname:=AValue;
  4171. end;
  4172. function tprocdef.GetParentFPInitBlock: tnode;
  4173. begin
  4174. if not assigned(implprocdefinfo) then
  4175. internalerror(2014010303);
  4176. result:=implprocdefinfo^.parentfpinitblock;
  4177. end;
  4178. function tprocdef.GetParentFPStruct: tsym;
  4179. begin
  4180. if not assigned(implprocdefinfo) then
  4181. internalerror(2014010304);
  4182. result:=implprocdefinfo^.parentfpstruct;
  4183. end;
  4184. function tprocdef.GetParentFPStructPtrType: tdef;
  4185. begin
  4186. if not assigned(implprocdefinfo) then
  4187. internalerror(2014010305);
  4188. result:=implprocdefinfo^.parentfpstructptrtype;
  4189. end;
  4190. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4191. begin
  4192. if not assigned(implprocdefinfo) then
  4193. internalerror(2014010306);
  4194. implprocdefinfo^.parentfpinitblock:=AValue;
  4195. end;
  4196. function tprocdef.Getprocendtai: tai;
  4197. begin
  4198. if not assigned(implprocdefinfo) then
  4199. internalerror(2014010307);
  4200. result:=implprocdefinfo^.procendtai;
  4201. end;
  4202. function tprocdef.Getprocstarttai: tai;
  4203. begin
  4204. if not assigned(implprocdefinfo) then
  4205. internalerror(2014010308);
  4206. result:=implprocdefinfo^.procstarttai;
  4207. end;
  4208. procedure tprocdef.Setprocendtai(AValue: tai);
  4209. begin
  4210. if not assigned(implprocdefinfo) then
  4211. internalerror(2014010309);
  4212. implprocdefinfo^.procendtai:=AValue;
  4213. end;
  4214. function tprocdef.Getskpara: pointer;
  4215. begin
  4216. if not assigned(implprocdefinfo) then
  4217. internalerror(2014010310);
  4218. result:=implprocdefinfo^.skpara;
  4219. end;
  4220. procedure tprocdef.Setskpara(AValue: pointer);
  4221. begin
  4222. if not assigned(implprocdefinfo) then
  4223. internalerror(2014010311);
  4224. implprocdefinfo^.skpara:=AValue;
  4225. end;
  4226. function tprocdef.Getforwarddef: boolean;
  4227. begin
  4228. if not assigned(implprocdefinfo) then
  4229. internalerror(2014010312);
  4230. result:=implprocdefinfo^.forwarddef;
  4231. end;
  4232. function tprocdef.Gethasforward: boolean;
  4233. begin
  4234. if not assigned(implprocdefinfo) then
  4235. internalerror(2014010313);
  4236. result:=implprocdefinfo^.hasforward;
  4237. end;
  4238. function tprocdef.Getinterfacedef: boolean;
  4239. begin
  4240. if not assigned(implprocdefinfo) then
  4241. internalerror(2014010314);
  4242. result:=implprocdefinfo^.interfacedef;
  4243. end;
  4244. procedure tprocdef.Setforwarddef(AValue: boolean);
  4245. begin
  4246. if not assigned(implprocdefinfo) then
  4247. internalerror(2014010315);
  4248. implprocdefinfo^.forwarddef:=AValue;
  4249. end;
  4250. procedure tprocdef.Sethasforward(AValue: boolean);
  4251. begin
  4252. if not assigned(implprocdefinfo) then
  4253. internalerror(2014010316);
  4254. implprocdefinfo^.hasforward:=AValue;
  4255. end;
  4256. function tprocdef.GetIsEmpty: boolean;
  4257. begin
  4258. result:=pio_empty in implprocoptions;
  4259. end;
  4260. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4261. begin
  4262. if AValue then
  4263. include(implprocoptions,pio_empty)
  4264. else
  4265. include(implprocoptions,pio_empty);
  4266. end;
  4267. function tprocdef.GetHasInliningInfo: boolean;
  4268. begin
  4269. result:=pio_has_inlininginfo in implprocoptions;
  4270. end;
  4271. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4272. begin
  4273. if AValue then
  4274. include(implprocoptions,pio_has_inlininginfo)
  4275. else
  4276. exclude(implprocoptions,pio_has_inlininginfo);
  4277. end;
  4278. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4279. begin
  4280. if not assigned(implprocdefinfo) then
  4281. internalerror(2014010317);
  4282. implprocdefinfo^.interfacedef:=AValue;
  4283. end;
  4284. procedure tprocdef.Setprocstarttai(AValue: tai);
  4285. begin
  4286. if not assigned(implprocdefinfo) then
  4287. internalerror(2014010318);
  4288. implprocdefinfo^.procstarttai:=AValue;
  4289. end;
  4290. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4291. begin
  4292. if not assigned(implprocdefinfo) then
  4293. internalerror(2014010319);
  4294. implprocdefinfo^.parentfpstruct:=AValue;
  4295. end;
  4296. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4297. begin
  4298. if not assigned(implprocdefinfo) then
  4299. internalerror(2014010320);
  4300. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4301. end;
  4302. constructor tprocdef.create(level:byte);
  4303. begin
  4304. inherited create(procdef,level);
  4305. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4306. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4307. {$ifdef symansistr}
  4308. _mangledname:='';
  4309. {$else symansistr}
  4310. _mangledname:=nil;
  4311. {$endif symansistr}
  4312. fileinfo:=current_filepos;
  4313. extnumber:=$ffff;
  4314. aliasnames:=TCmdStrList.create;
  4315. funcretsym:=nil;
  4316. forwarddef:=true;
  4317. interfacedef:=false;
  4318. hasforward:=false;
  4319. struct := nil;
  4320. import_dll:=nil;
  4321. import_name:=nil;
  4322. import_nr:=0;
  4323. inlininginfo:=nil;
  4324. deprecatedmsg:=nil;
  4325. end;
  4326. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4327. var
  4328. i,aliasnamescount : longint;
  4329. level : byte;
  4330. begin
  4331. inherited ppuload(procdef,ppufile);
  4332. {$ifdef symansistr}
  4333. if po_has_mangledname in procoptions then
  4334. _mangledname:=ppufile.getansistring
  4335. else
  4336. _mangledname:='';
  4337. {$else symansistr}
  4338. if po_has_mangledname in procoptions then
  4339. _mangledname:=stringdup(ppufile.getstring)
  4340. else
  4341. _mangledname:=nil;
  4342. {$endif symansistr}
  4343. extnumber:=ppufile.getword;
  4344. level:=ppufile.getbyte;
  4345. ppufile.getderef(structderef);
  4346. ppufile.getderef(procsymderef);
  4347. ppufile.getposinfo(fileinfo);
  4348. visibility:=tvisibility(ppufile.getbyte);
  4349. ppufile.getsmallset(symoptions);
  4350. if sp_has_deprecated_msg in symoptions then
  4351. deprecatedmsg:=stringdup(ppufile.getstring)
  4352. else
  4353. deprecatedmsg:=nil;
  4354. synthetickind:=tsynthetickind(ppufile.getbyte);
  4355. { import stuff }
  4356. if po_has_importdll in procoptions then
  4357. import_dll:=stringdup(ppufile.getstring)
  4358. else
  4359. import_dll:=nil;
  4360. if po_has_importname in procoptions then
  4361. import_name:=stringdup(ppufile.getstring)
  4362. else
  4363. import_name:=nil;
  4364. import_nr:=ppufile.getword;
  4365. if (po_msgint in procoptions) then
  4366. messageinf.i:=ppufile.getlongint;
  4367. if (po_msgstr in procoptions) then
  4368. messageinf.str:=stringdup(ppufile.getstring);
  4369. if (po_dispid in procoptions) then
  4370. dispid:=ppufile.getlongint;
  4371. { inline stuff }
  4372. ppufile.getsmallset(implprocoptions);
  4373. if has_inlininginfo then
  4374. begin
  4375. ppufile.getderef(funcretsymderef);
  4376. new(inlininginfo);
  4377. ppufile.getsmallset(inlininginfo^.flags);
  4378. end
  4379. else
  4380. begin
  4381. inlininginfo:=nil;
  4382. funcretsym:=nil;
  4383. end;
  4384. aliasnames:=TCmdStrList.create;
  4385. { count alias names }
  4386. aliasnamescount:=ppufile.getbyte;
  4387. for i:=1 to aliasnamescount do
  4388. aliasnames.insert(ppufile.getstring);
  4389. ppuload_platform(ppufile);
  4390. { load para symtable }
  4391. parast:=tparasymtable.create(self,level);
  4392. tparasymtable(parast).ppuload(ppufile);
  4393. { load local symtable }
  4394. if has_inlininginfo then
  4395. begin
  4396. localst:=tlocalsymtable.create(self,level);
  4397. tlocalsymtable(localst).ppuload(ppufile);
  4398. end
  4399. else
  4400. localst:=nil;
  4401. { inline stuff }
  4402. if has_inlininginfo then
  4403. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4404. { default values for no persistent data }
  4405. if (cs_link_deffile in current_settings.globalswitches) and
  4406. (tf_need_export in target_info.flags) and
  4407. (po_exports in procoptions) then
  4408. deffile.AddExport(mangledname);
  4409. { Disable po_has_inlining until the derefimpl is done }
  4410. has_inlininginfo:=false;
  4411. end;
  4412. destructor tprocdef.destroy;
  4413. begin
  4414. aliasnames.free;
  4415. aliasnames:=nil;
  4416. if assigned(localst) and
  4417. (localst.symtabletype<>staticsymtable) then
  4418. begin
  4419. {$ifdef MEMDEBUG}
  4420. memproclocalst.start;
  4421. {$endif MEMDEBUG}
  4422. localst.free;
  4423. localst:=nil;
  4424. {$ifdef MEMDEBUG}
  4425. memproclocalst.start;
  4426. {$endif MEMDEBUG}
  4427. end;
  4428. if assigned(inlininginfo) then
  4429. begin
  4430. {$ifdef MEMDEBUG}
  4431. memprocnodetree.start;
  4432. {$endif MEMDEBUG}
  4433. tnode(inlininginfo^.code).free;
  4434. {$ifdef MEMDEBUG}
  4435. memprocnodetree.start;
  4436. {$endif MEMDEBUG}
  4437. dispose(inlininginfo);
  4438. inlininginfo:=nil;
  4439. end;
  4440. if assigned(implprocdefinfo) then
  4441. begin
  4442. stringdispose(implprocdefinfo^.resultname);
  4443. freemem(implprocdefinfo);
  4444. implprocdefinfo:=nil;
  4445. end;
  4446. stringdispose(import_dll);
  4447. stringdispose(import_name);
  4448. stringdispose(deprecatedmsg);
  4449. if (po_msgstr in procoptions) then
  4450. stringdispose(messageinf.str);
  4451. {$ifndef symansistr}
  4452. if assigned(_mangledname) then
  4453. begin
  4454. {$ifdef MEMDEBUG}
  4455. memmanglednames.start;
  4456. {$endif MEMDEBUG}
  4457. stringdispose(_mangledname);
  4458. {$ifdef MEMDEBUG}
  4459. memmanglednames.stop;
  4460. {$endif MEMDEBUG}
  4461. end;
  4462. {$endif symansistr}
  4463. inherited destroy;
  4464. end;
  4465. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4466. var
  4467. oldintfcrc : boolean;
  4468. aliasnamescount : longint;
  4469. item : TCmdStrListItem;
  4470. begin
  4471. { released procdef? }
  4472. if not assigned(parast) then
  4473. exit;
  4474. inherited ppuwrite(ppufile);
  4475. {$ifdef symansistr}
  4476. if po_has_mangledname in procoptions then
  4477. ppufile.putansistring(_mangledname);
  4478. {$else symansistr}
  4479. if po_has_mangledname in procoptions then
  4480. ppufile.putstring(_mangledname^);
  4481. {$endif symansistr}
  4482. ppufile.putword(extnumber);
  4483. ppufile.putbyte(parast.symtablelevel);
  4484. ppufile.putderef(structderef);
  4485. ppufile.putderef(procsymderef);
  4486. ppufile.putposinfo(fileinfo);
  4487. ppufile.putbyte(byte(visibility));
  4488. ppufile.putsmallset(symoptions);
  4489. if sp_has_deprecated_msg in symoptions then
  4490. ppufile.putstring(deprecatedmsg^);
  4491. ppufile.putbyte(byte(synthetickind));
  4492. { import }
  4493. if po_has_importdll in procoptions then
  4494. ppufile.putstring(import_dll^);
  4495. if po_has_importname in procoptions then
  4496. ppufile.putstring(import_name^);
  4497. ppufile.putword(import_nr);
  4498. if (po_msgint in procoptions) then
  4499. ppufile.putlongint(messageinf.i);
  4500. if (po_msgstr in procoptions) then
  4501. ppufile.putstring(messageinf.str^);
  4502. if (po_dispid in procoptions) then
  4503. ppufile.putlongint(dispid);
  4504. { inline stuff }
  4505. oldintfcrc:=ppufile.do_crc;
  4506. ppufile.do_crc:=false;
  4507. ppufile.putsmallset(implprocoptions);
  4508. if has_inlininginfo then
  4509. begin
  4510. ppufile.putderef(funcretsymderef);
  4511. ppufile.putsmallset(inlininginfo^.flags);
  4512. end;
  4513. { count alias names }
  4514. aliasnamescount:=0;
  4515. item:=TCmdStrListItem(aliasnames.first);
  4516. while assigned(item) do
  4517. begin
  4518. inc(aliasnamescount);
  4519. item:=TCmdStrListItem(item.next);
  4520. end;
  4521. if aliasnamescount>255 then
  4522. internalerror(200711021);
  4523. ppufile.putbyte(aliasnamescount);
  4524. item:=TCmdStrListItem(aliasnames.first);
  4525. while assigned(item) do
  4526. begin
  4527. ppufile.putstring(item.str);
  4528. item:=TCmdStrListItem(item.next);
  4529. end;
  4530. ppufile.do_crc:=oldintfcrc;
  4531. { write this entry }
  4532. writeentry(ppufile,ibprocdef);
  4533. { Save the para symtable, this is taken from the interface }
  4534. tparasymtable(parast).ppuwrite(ppufile);
  4535. { save localsymtable for inline procedures or when local
  4536. browser info is requested, this has no influence on the crc }
  4537. if has_inlininginfo then
  4538. begin
  4539. oldintfcrc:=ppufile.do_crc;
  4540. ppufile.do_crc:=false;
  4541. tlocalsymtable(localst).ppuwrite(ppufile);
  4542. ppufile.do_crc:=oldintfcrc;
  4543. end;
  4544. { node tree for inlining }
  4545. oldintfcrc:=ppufile.do_crc;
  4546. ppufile.do_crc:=false;
  4547. if has_inlininginfo then
  4548. ppuwritenodetree(ppufile,inlininginfo^.code);
  4549. ppufile.do_crc:=oldintfcrc;
  4550. end;
  4551. function tprocdef.fullprocname(showhidden:boolean):string;
  4552. var
  4553. pno: tprocnameoptions;
  4554. begin
  4555. pno:=[];
  4556. if showhidden then
  4557. include(pno,pno_showhidden);
  4558. result:=customprocname(pno);
  4559. end;
  4560. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4561. var
  4562. s, rn : ansistring;
  4563. t : ttoken;
  4564. begin
  4565. {$ifdef EXTDEBUG}
  4566. include(pno,pno_showhidden);
  4567. {$endif EXTDEBUG}
  4568. s:='';
  4569. if proctypeoption=potype_operator then
  4570. begin
  4571. for t:=NOTOKEN to last_overloaded do
  4572. if procsym.realname='$'+overloaded_names[t] then
  4573. begin
  4574. s:='operator ';
  4575. if (pno_ownername in pno) and
  4576. assigned(struct) then
  4577. s:=s+struct.RttiName+'.';
  4578. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  4579. break;
  4580. end;
  4581. end
  4582. else
  4583. begin
  4584. if (po_classmethod in procoptions) and
  4585. not(pno_noclassmarker in pno) then
  4586. s:='class ';
  4587. case proctypeoption of
  4588. potype_constructor,
  4589. potype_class_constructor:
  4590. s:=s+'constructor ';
  4591. potype_class_destructor,
  4592. potype_destructor:
  4593. s:=s+'destructor ';
  4594. else
  4595. if (pno_proctypeoption in pno) then
  4596. begin
  4597. if assigned(returndef) and
  4598. not(is_void(returndef)) then
  4599. s:=s+'function '
  4600. else
  4601. s:=s+'procedure ';
  4602. end;
  4603. end;
  4604. if (pno_ownername in pno) and
  4605. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  4606. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  4607. rn:=procsym.realname;
  4608. if (pno_noleadingdollar in pno) and
  4609. (rn[1]='$') then
  4610. delete(rn,1,1);
  4611. s:=s+rn+typename_paras(pno);
  4612. end;
  4613. if not(proctypeoption in [potype_constructor,potype_destructor,
  4614. potype_class_constructor,potype_class_destructor]) and
  4615. assigned(returndef) and
  4616. not(is_void(returndef)) then
  4617. s:=s+':'+returndef.GetTypeName;
  4618. if owner.symtabletype=localsymtable then
  4619. s:=s+' is nested';
  4620. s:=s+';';
  4621. { forced calling convention? }
  4622. if (po_hascallingconvention in procoptions) then
  4623. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  4624. if (po_staticmethod in procoptions) and
  4625. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  4626. s:=s+' Static;';
  4627. if pno_mangledname in pno then
  4628. s:=s+' -- mangled name: '+mangledname;
  4629. customprocname:=s;
  4630. end;
  4631. function tprocdef.is_methodpointer:boolean;
  4632. begin
  4633. { don't check assigned(_class), that's also the case for nested
  4634. procedures inside methods }
  4635. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  4636. end;
  4637. function tprocdef.is_addressonly:boolean;
  4638. begin
  4639. result:=assigned(owner) and
  4640. not is_methodpointer and
  4641. (not(m_nested_procvars in current_settings.modeswitches) or
  4642. not is_nested_pd(self));
  4643. end;
  4644. procedure tprocdef.make_external;
  4645. begin
  4646. include(procoptions,po_external);
  4647. forwarddef:=false;
  4648. end;
  4649. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  4650. begin
  4651. case t of
  4652. gs_local :
  4653. GetSymtable:=localst;
  4654. gs_para :
  4655. GetSymtable:=parast;
  4656. else
  4657. GetSymtable:=nil;
  4658. end;
  4659. end;
  4660. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  4661. var
  4662. j : longint;
  4663. begin
  4664. result:=inherited getcopyas(newtyp,copytyp);
  4665. if newtyp=procvardef then
  4666. exit;
  4667. { don't copy mangled name, can be different }
  4668. tprocdef(result).messageinf:=messageinf;
  4669. tprocdef(result).dispid:=dispid;
  4670. if po_msgstr in procoptions then
  4671. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  4672. tprocdef(result).symoptions:=symoptions;
  4673. if assigned(deprecatedmsg) then
  4674. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  4675. { will have to be associated with appropriate procsym }
  4676. tprocdef(result).procsym:=nil;
  4677. { don't create aliases for bare copies, nor copy the funcretsym as
  4678. the function result parameter will be inserted again if necessary
  4679. (e.g. if the calling convention is changed) }
  4680. if copytyp<>pc_bareproc then
  4681. begin
  4682. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  4683. if assigned(funcretsym) then
  4684. begin
  4685. if funcretsym.owner=parast then
  4686. begin
  4687. j:=parast.symlist.indexof(funcretsym);
  4688. if j<0 then
  4689. internalerror(2011040606);
  4690. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  4691. end
  4692. else if funcretsym.owner=localst then
  4693. begin
  4694. { nothing to do, will be inserted for the new procdef while
  4695. parsing its body (by pdecsub.insert_funcret_local) }
  4696. end
  4697. else
  4698. internalerror(2011040605);
  4699. end;
  4700. end;
  4701. { will have to be associated with a new struct }
  4702. tprocdef(result).struct:=nil;
  4703. if assigned(implprocdefinfo) then
  4704. begin
  4705. if assigned(resultname) then
  4706. tprocdef(result).resultname:=stringdup(resultname^);
  4707. end;
  4708. if assigned(import_dll) then
  4709. tprocdef(result).import_dll:=stringdup(import_dll^);
  4710. if assigned(import_name) then
  4711. tprocdef(result).import_name:=stringdup(import_name^);
  4712. tprocdef(result).import_nr:=import_nr;
  4713. tprocdef(result).extnumber:=$ffff;
  4714. tprocdef(result).visibility:=visibility;
  4715. tprocdef(result).synthetickind:=synthetickind;
  4716. { we need a separate implementation for the copied def }
  4717. tprocdef(result).forwarddef:=true;
  4718. tprocdef(result).interfacedef:=true;
  4719. { create new paralist }
  4720. tprocdef(result).calcparas;
  4721. end;
  4722. function tprocdef.getcopy: tstoreddef;
  4723. begin
  4724. result:=getcopyas(procdef,pc_normal);
  4725. end;
  4726. procedure tprocdef.buildderef;
  4727. begin
  4728. inherited buildderef;
  4729. structderef.build(struct);
  4730. { procsym that originaly defined this definition, should be in the
  4731. same symtable }
  4732. procsymderef.build(procsym);
  4733. end;
  4734. procedure tprocdef.buildderefimpl;
  4735. begin
  4736. inherited buildderefimpl;
  4737. { inline tree }
  4738. if has_inlininginfo then
  4739. begin
  4740. { Localst is not available for main/unit init }
  4741. if assigned(localst) then
  4742. begin
  4743. tlocalsymtable(localst).buildderef;
  4744. tlocalsymtable(localst).buildderefimpl;
  4745. end;
  4746. funcretsymderef.build(funcretsym);
  4747. inlininginfo^.code.buildderefimpl;
  4748. end;
  4749. end;
  4750. procedure tprocdef.deref;
  4751. begin
  4752. inherited deref;
  4753. struct:=tabstractrecorddef(structderef.resolve);
  4754. { procsym that originaly defined this definition, should be in the
  4755. same symtable }
  4756. procsym:=tprocsym(procsymderef.resolve);
  4757. end;
  4758. procedure tprocdef.derefimpl;
  4759. begin
  4760. { Enable has_inlininginfo when the inlininginfo
  4761. structure is available. The has_inlininginfo was disabled
  4762. after the load, since the data was invalid }
  4763. if assigned(inlininginfo) then
  4764. has_inlininginfo:=true;
  4765. { Inline }
  4766. if has_inlininginfo then
  4767. begin
  4768. { Locals }
  4769. if assigned(localst) then
  4770. begin
  4771. tlocalsymtable(localst).deref;
  4772. tlocalsymtable(localst).derefimpl;
  4773. end;
  4774. inlininginfo^.code.derefimpl;
  4775. { funcretsym, this is always located in the localst }
  4776. funcretsym:=tsym(funcretsymderef.resolve);
  4777. end
  4778. else
  4779. begin
  4780. { safety }
  4781. { Not safe! A unit may be reresolved after its interface has been
  4782. parsed but before its implementation has been parsed, and in that
  4783. case the funcretsym is still required!
  4784. funcretsym:=nil; }
  4785. end;
  4786. end;
  4787. function tprocdef.GetTypeName : string;
  4788. begin
  4789. GetTypeName := FullProcName(false);
  4790. end;
  4791. function tprocdef.mangledname : TSymStr;
  4792. begin
  4793. {$ifdef symansistr}
  4794. if _mangledname='' then
  4795. begin
  4796. result:=defaultmangledname;
  4797. _mangledname:=result;
  4798. end
  4799. else
  4800. result:=_mangledname;
  4801. {$else symansistr}
  4802. if not assigned(_mangledname) then
  4803. begin
  4804. result:=defaultmangledname;
  4805. _mangledname:=stringdup(mangledname);
  4806. end
  4807. else
  4808. result:=_mangledname^;
  4809. {$endif symansistr}
  4810. end;
  4811. function tprocdef.defaultmangledname: TSymStr;
  4812. var
  4813. hp : TParavarsym;
  4814. hs : TSymStr;
  4815. crc : dword;
  4816. newlen,
  4817. oldlen,
  4818. i : integer;
  4819. begin
  4820. hp:=nil;
  4821. { we need to use the symtable where the procsym is inserted,
  4822. because that is visible to the world }
  4823. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4824. oldlen:=length(defaultmangledname);
  4825. { add parameter types }
  4826. for i:=0 to paras.count-1 do
  4827. begin
  4828. hp:=tparavarsym(paras[i]);
  4829. if not(vo_is_hidden_para in hp.varoptions) then
  4830. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4831. end;
  4832. { add resultdef, add $$ as separator to make it unique from a
  4833. parameter separator }
  4834. if not is_void(returndef) then
  4835. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4836. newlen:=length(defaultmangledname);
  4837. { Replace with CRC if the parameter line is very long }
  4838. if (newlen-oldlen>12) and
  4839. ((newlen>100) or (newlen-oldlen>64)) then
  4840. begin
  4841. crc:=0;
  4842. for i:=0 to paras.count-1 do
  4843. begin
  4844. hp:=tparavarsym(paras[i]);
  4845. if not(vo_is_hidden_para in hp.varoptions) then
  4846. begin
  4847. hs:=hp.vardef.mangledparaname;
  4848. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4849. end;
  4850. end;
  4851. hs:=hp.vardef.mangledparaname;
  4852. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4853. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4854. end;
  4855. end;
  4856. function tprocdef.cplusplusmangledname : TSymStr;
  4857. function getcppparaname(p : tdef) : TSymStr;
  4858. const
  4859. {$ifdef NAMEMANGLING_GCC2}
  4860. ordtype2str : array[tordtype] of string[2] = (
  4861. '',
  4862. 'Uc','Us','Ui','Us',
  4863. 'Sc','s','i','x',
  4864. 'b','b','b','b','b',
  4865. 'c','w','x');
  4866. {$else NAMEMANGLING_GCC2}
  4867. ordtype2str : array[tordtype] of string[1] = (
  4868. 'v',
  4869. 'h','t','j','y',
  4870. 'a','s','i','x',
  4871. 'b','b','b','b',
  4872. 'b','b','b','b',
  4873. 'c','w','x');
  4874. floattype2str : array[tfloattype] of string[1] = (
  4875. 'f','d','e','e',
  4876. 'd','d','g');
  4877. {$endif NAMEMANGLING_GCC2}
  4878. var
  4879. s : TSymStr;
  4880. begin
  4881. case p.typ of
  4882. orddef:
  4883. s:=ordtype2str[torddef(p).ordtype];
  4884. pointerdef:
  4885. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4886. {$ifndef NAMEMANGLING_GCC2}
  4887. floatdef:
  4888. s:=floattype2str[tfloatdef(p).floattype];
  4889. {$endif NAMEMANGLING_GCC2}
  4890. else
  4891. internalerror(2103001);
  4892. end;
  4893. getcppparaname:=s;
  4894. end;
  4895. var
  4896. s,s2 : TSymStr;
  4897. hp : TParavarsym;
  4898. i : integer;
  4899. begin
  4900. {$ifdef NAMEMANGLING_GCC2}
  4901. { outdated gcc 2.x name mangling scheme }
  4902. s := procsym.realname;
  4903. if procsym.owner.symtabletype=ObjectSymtable then
  4904. begin
  4905. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4906. case proctypeoption of
  4907. potype_destructor:
  4908. s:='_$_'+tostr(length(s2))+s2;
  4909. potype_constructor:
  4910. s:='___'+tostr(length(s2))+s2;
  4911. else
  4912. s:='_'+s+'__'+tostr(length(s2))+s2;
  4913. end;
  4914. end
  4915. else s:=s+'__';
  4916. s:=s+'F';
  4917. { concat modifiers }
  4918. { !!!!! }
  4919. { now we handle the parameters }
  4920. if maxparacount>0 then
  4921. begin
  4922. for i:=0 to paras.count-1 do
  4923. begin
  4924. hp:=tparavarsym(paras[i]);
  4925. { no hidden parameters form part of a C++ mangled name:
  4926. a) self is not included
  4927. b) there are no "high" or other hidden parameters
  4928. }
  4929. if vo_is_hidden_para in hp.varoptions then
  4930. continue;
  4931. s2:=getcppparaname(hp.vardef);
  4932. if hp.varspez in [vs_var,vs_out] then
  4933. s2:='R'+s2;
  4934. s:=s+s2;
  4935. end;
  4936. end
  4937. else
  4938. s:=s+'v';
  4939. cplusplusmangledname:=s;
  4940. {$else NAMEMANGLING_GCC2}
  4941. { gcc 3.x and 4.x name mangling scheme }
  4942. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4943. if procsym.owner.symtabletype=ObjectSymtable then
  4944. begin
  4945. s:='_ZN';
  4946. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4947. s:=s+tostr(length(s2))+s2;
  4948. case proctypeoption of
  4949. potype_constructor:
  4950. s:=s+'C1';
  4951. potype_destructor:
  4952. s:=s+'D1';
  4953. else
  4954. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4955. end;
  4956. s:=s+'E';
  4957. end
  4958. else
  4959. s:=procsym.realname;
  4960. { now we handle the parameters }
  4961. if maxparacount>0 then
  4962. begin
  4963. for i:=0 to paras.count-1 do
  4964. begin
  4965. hp:=tparavarsym(paras[i]);
  4966. { no hidden parameters form part of a C++ mangled name:
  4967. a) self is not included
  4968. b) there are no "high" or other hidden parameters
  4969. }
  4970. if vo_is_hidden_para in hp.varoptions then
  4971. continue;
  4972. s2:=getcppparaname(hp.vardef);
  4973. if hp.varspez in [vs_var,vs_out] then
  4974. s2:='R'+s2;
  4975. s:=s+s2;
  4976. end;
  4977. end
  4978. else
  4979. s:=s+'v';
  4980. cplusplusmangledname:=s;
  4981. {$endif NAMEMANGLING_GCC2}
  4982. end;
  4983. function tprocdef.objcmangledname : TSymStr;
  4984. var
  4985. manglednamelen: longint;
  4986. iscatmethod : boolean;
  4987. begin
  4988. if not (po_msgstr in procoptions) then
  4989. internalerror(2009030901);
  4990. { we may very well need longer strings to handle these... }
  4991. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  4992. length('+"[ ]"')+length(messageinf.str^);
  4993. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  4994. if (iscatmethod) then
  4995. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  4996. if manglednamelen>255 then
  4997. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  4998. if not(po_classmethod in procoptions) then
  4999. result:='"-['
  5000. else
  5001. result:='"+[';
  5002. { quotes are necessary because the +/- otherwise confuse the assembler
  5003. into expecting a number
  5004. }
  5005. if iscatmethod then
  5006. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  5007. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  5008. if iscatmethod then
  5009. result:=result+')';
  5010. result:=result+' '+messageinf.str^+']"';
  5011. end;
  5012. procedure tprocdef.setmangledname(const s : TSymStr);
  5013. begin
  5014. { This is not allowed anymore, the forward declaration
  5015. already needs to create the correct mangledname, no changes
  5016. afterwards are allowed (PFV) }
  5017. { Exception: interface definitions in mode macpas, since in that }
  5018. { case no reference to the old name can exist yet (JM) }
  5019. {$ifdef symansistr}
  5020. if _mangledname<>'' then
  5021. if ((m_mac in current_settings.modeswitches) and
  5022. (interfacedef)) then
  5023. _mangledname:=''
  5024. else
  5025. internalerror(200411171);
  5026. {$else symansistr}
  5027. if assigned(_mangledname) then
  5028. if ((m_mac in current_settings.modeswitches) and
  5029. (interfacedef)) then
  5030. stringdispose(_mangledname)
  5031. else
  5032. internalerror(200411171);
  5033. {$endif symansistr}
  5034. {$ifdef jvm}
  5035. { this routine can be called for compilerproces. can't set mangled
  5036. name since it must be calculated, but it uses import_name when set
  5037. -> set that one }
  5038. import_name:=stringdup(s);
  5039. include(procoptions,po_has_importname);
  5040. include(procoptions,po_has_mangledname);
  5041. {$else}
  5042. {$ifdef symansistr}
  5043. _mangledname:=s;
  5044. {$else symansistr}
  5045. _mangledname:=stringdup(s);
  5046. {$endif symansistr}
  5047. {$endif jvm}
  5048. include(procoptions,po_has_mangledname);
  5049. end;
  5050. {***************************************************************************
  5051. TPROCVARDEF
  5052. ***************************************************************************}
  5053. constructor tprocvardef.create(level:byte);
  5054. begin
  5055. inherited create(procvardef,level);
  5056. end;
  5057. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5058. begin
  5059. inherited ppuload(procvardef,ppufile);
  5060. { load para symtable }
  5061. parast:=tparasymtable.create(self,ppufile.getbyte);
  5062. ppuload_platform(ppufile);
  5063. tparasymtable(parast).ppuload(ppufile);
  5064. end;
  5065. function tprocvardef.getcopy : tstoreddef;
  5066. var
  5067. i : tcallercallee;
  5068. j : longint;
  5069. begin
  5070. result:=cprocvardef.create(parast.symtablelevel);
  5071. tprocvardef(result).returndef:=returndef;
  5072. tprocvardef(result).returndefderef:=returndefderef;
  5073. tprocvardef(result).parast:=parast.getcopy;
  5074. tprocvardef(result).savesize:=savesize;
  5075. { create paralist copy }
  5076. calcparas;
  5077. tprocvardef(result).paras:=tparalist.create(false);
  5078. tprocvardef(result).paras.count:=paras.count;
  5079. for j:=0 to paras.count-1 do
  5080. tprocvardef(result).paras[j]:=paras[j];
  5081. tprocvardef(result).proctypeoption:=proctypeoption;
  5082. tprocvardef(result).proccalloption:=proccalloption;
  5083. tprocvardef(result).procoptions:=procoptions;
  5084. tprocvardef(result).callerargareasize:=callerargareasize;
  5085. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5086. tprocvardef(result).maxparacount:=maxparacount;
  5087. tprocvardef(result).minparacount:=minparacount;
  5088. for i:=low(tcallercallee) to high(tcallercallee) do
  5089. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5090. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5091. {$ifdef m68k}
  5092. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5093. {$endif}
  5094. end;
  5095. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5096. begin
  5097. inherited ppuwrite(ppufile);
  5098. { Save the para symtable level (necessary to distinguish nested
  5099. procvars) }
  5100. ppufile.putbyte(parast.symtablelevel);
  5101. { Write this entry }
  5102. writeentry(ppufile,ibprocvardef);
  5103. { Save the para symtable, this is taken from the interface }
  5104. tparasymtable(parast).ppuwrite(ppufile);
  5105. end;
  5106. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5107. begin
  5108. case t of
  5109. gs_para :
  5110. GetSymtable:=parast;
  5111. else
  5112. GetSymtable:=nil;
  5113. end;
  5114. end;
  5115. function tprocvardef.size : asizeint;
  5116. begin
  5117. if ((po_methodpointer in procoptions) or
  5118. is_nested_pd(self)) and
  5119. not(po_addressonly in procoptions) then
  5120. begin
  5121. if is_nested_pd(self) then
  5122. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5123. else
  5124. size:=voidcodepointertype.size+voidpointertype.size;
  5125. end
  5126. else
  5127. size:=voidcodepointertype.size;
  5128. end;
  5129. function tprocvardef.is_methodpointer:boolean;
  5130. begin
  5131. result:=(po_methodpointer in procoptions);
  5132. end;
  5133. function tprocvardef.is_addressonly:boolean;
  5134. begin
  5135. result:=(not(po_methodpointer in procoptions) and
  5136. not is_nested_pd(self)) or
  5137. (po_addressonly in procoptions);
  5138. end;
  5139. function tprocvardef.getmangledparaname:TSymStr;
  5140. begin
  5141. if not(po_methodpointer in procoptions) then
  5142. if not is_nested_pd(self) then
  5143. result:='procvar'
  5144. else
  5145. result:='nestedprovar'
  5146. else
  5147. result:='procvarofobj'
  5148. end;
  5149. function tprocvardef.is_publishable : boolean;
  5150. begin
  5151. is_publishable:=(po_methodpointer in procoptions);
  5152. end;
  5153. function tprocvardef.GetTypeName : string;
  5154. var
  5155. s: string;
  5156. pno : tprocnameoptions;
  5157. begin
  5158. {$ifdef EXTDEBUG}
  5159. pno:=[pno_showhidden];
  5160. {$else EXTDEBUG}
  5161. pno:=[];
  5162. {$endif EXTDEBUG}
  5163. s:='<';
  5164. if po_classmethod in procoptions then
  5165. s := s+'class method type of'
  5166. else
  5167. if po_addressonly in procoptions then
  5168. s := s+'address of'
  5169. else
  5170. s := s+'procedure variable type of';
  5171. if assigned(returndef) and
  5172. (returndef<>voidtype) then
  5173. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5174. else
  5175. s:=s+' procedure'+typename_paras(pno);
  5176. if po_methodpointer in procoptions then
  5177. s := s+' of object';
  5178. if is_nested_pd(self) then
  5179. s := s+' is nested';
  5180. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5181. end;
  5182. {***************************************************************************
  5183. TOBJECTDEF
  5184. ***************************************************************************}
  5185. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  5186. begin
  5187. inherited create(n,objectdef);
  5188. fcurrent_dispid:=0;
  5189. objecttype:=ot;
  5190. childof:=nil;
  5191. if objecttype=odt_helper then
  5192. owner.includeoption(sto_has_helper);
  5193. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  5194. { create space for vmt !! }
  5195. vmtentries:=TFPList.Create;
  5196. vmt_offset:=0;
  5197. set_parent(c);
  5198. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5199. prepareguid;
  5200. { setup implemented interfaces }
  5201. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5202. ImplementedInterfaces:=TFPObjectList.Create(true)
  5203. else
  5204. ImplementedInterfaces:=nil;
  5205. writing_class_record_dbginfo:=false;
  5206. end;
  5207. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5208. var
  5209. i,
  5210. implintfcount : longint;
  5211. d, getterd : tderef;
  5212. ImplIntf : TImplementedInterface;
  5213. vmtentry : pvmtentry;
  5214. begin
  5215. inherited ppuload(objectdef,ppufile);
  5216. objecttype:=tobjecttyp(ppufile.getbyte);
  5217. objextname:=stringdup(ppufile.getstring);
  5218. { only used for external Objective-C classes/protocols }
  5219. if (objextname^='') then
  5220. stringdispose(objextname);
  5221. symtable:=tObjectSymtable.create(self,objrealname^,0);
  5222. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5223. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5224. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5225. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5226. vmt_offset:=ppufile.getlongint;
  5227. ppufile.getderef(childofderef);
  5228. { load guid }
  5229. iidstr:=nil;
  5230. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5231. begin
  5232. new(iidguid);
  5233. ppufile.getguid(iidguid^);
  5234. iidstr:=stringdup(ppufile.getstring);
  5235. end;
  5236. abstractcnt:=ppufile.getlongint;
  5237. if objecttype=odt_helper then
  5238. ppufile.getderef(extendeddefderef);
  5239. vmtentries:=TFPList.Create;
  5240. vmtentries.count:=ppufile.getlongint;
  5241. for i:=0 to vmtentries.count-1 do
  5242. begin
  5243. ppufile.getderef(d);
  5244. new(vmtentry);
  5245. vmtentry^.procdef:=nil;
  5246. vmtentry^.procdefderef:=d;
  5247. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5248. vmtentries[i]:=vmtentry;
  5249. end;
  5250. { load implemented interfaces }
  5251. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5252. begin
  5253. ImplementedInterfaces:=TFPObjectList.Create(true);
  5254. implintfcount:=ppufile.getlongint;
  5255. for i:=0 to implintfcount-1 do
  5256. begin
  5257. ppufile.getderef(d);
  5258. ppufile.getderef(getterd);
  5259. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5260. ImplIntf.IOffset:=ppufile.getlongint;
  5261. byte(ImplIntf.IType):=ppufile.getbyte;
  5262. ImplementedInterfaces.Add(ImplIntf);
  5263. end;
  5264. end
  5265. else
  5266. ImplementedInterfaces:=nil;
  5267. if df_copied_def in defoptions then
  5268. begin
  5269. ppufile.getderef(cloneddefderef);
  5270. ppuload_platform(ppufile);
  5271. end
  5272. else
  5273. begin
  5274. ppuload_platform(ppufile);
  5275. tObjectSymtable(symtable).ppuload(ppufile);
  5276. end;
  5277. { handles the predefined class tobject }
  5278. { the last TOBJECT which is loaded gets }
  5279. { it ! }
  5280. if (childof=nil) and
  5281. (objecttype in [odt_class,odt_javaclass]) and
  5282. (objname^='TOBJECT') then
  5283. class_tobject:=self;
  5284. if (childof=nil) and
  5285. (objecttype=odt_interfacecom) then
  5286. if (objname^='IUNKNOWN') then
  5287. interface_iunknown:=self
  5288. else
  5289. if (objname^='IDISPATCH') then
  5290. interface_idispatch:=self;
  5291. if (childof=nil) and
  5292. (objecttype=odt_objcclass) and
  5293. (objname^='PROTOCOL') then
  5294. objc_protocoltype:=self;
  5295. if (objecttype=odt_javaclass) and
  5296. not(oo_is_formal in objectoptions) then
  5297. begin
  5298. if (objname^='JLOBJECT') then
  5299. java_jlobject:=self
  5300. else if (objname^='JLTHROWABLE') then
  5301. java_jlthrowable:=self
  5302. else if (objname^='FPCBASERECORDTYPE') then
  5303. java_fpcbaserecordtype:=self
  5304. else if (objname^='JLSTRING') then
  5305. java_jlstring:=self
  5306. else if (objname^='ANSISTRINGCLASS') then
  5307. java_ansistring:=self
  5308. else if (objname^='SHORTSTRINGCLASS') then
  5309. java_shortstring:=self
  5310. else if (objname^='JLENUM') then
  5311. java_jlenum:=self
  5312. else if (objname^='JUENUMSET') then
  5313. java_juenumset:=self
  5314. else if (objname^='FPCBITSET') then
  5315. java_jubitset:=self
  5316. else if (objname^='FPCBASEPROCVARTYPE') then
  5317. java_procvarbase:=self;
  5318. end;
  5319. writing_class_record_dbginfo:=false;
  5320. end;
  5321. destructor tobjectdef.destroy;
  5322. begin
  5323. if assigned(symtable) then
  5324. begin
  5325. symtable.free;
  5326. symtable:=nil;
  5327. end;
  5328. stringdispose(objextname);
  5329. stringdispose(iidstr);
  5330. if assigned(ImplementedInterfaces) then
  5331. begin
  5332. ImplementedInterfaces.free;
  5333. ImplementedInterfaces:=nil;
  5334. end;
  5335. if assigned(iidguid) then
  5336. begin
  5337. dispose(iidguid);
  5338. iidguid:=nil;
  5339. end;
  5340. if assigned(vmtentries) then
  5341. begin
  5342. resetvmtentries;
  5343. vmtentries.free;
  5344. vmtentries:=nil;
  5345. end;
  5346. if assigned(vmcallstaticinfo) then
  5347. begin
  5348. freemem(vmcallstaticinfo);
  5349. vmcallstaticinfo:=nil;
  5350. end;
  5351. inherited destroy;
  5352. end;
  5353. function tobjectdef.getcopy : tstoreddef;
  5354. var
  5355. i : longint;
  5356. begin
  5357. result:=cobjectdef.create(objecttype,objrealname^,childof);
  5358. { the constructor allocates a symtable which we release to avoid memory leaks }
  5359. tobjectdef(result).symtable.free;
  5360. tobjectdef(result).symtable:=symtable.getcopy;
  5361. if assigned(objextname) then
  5362. tobjectdef(result).objextname:=stringdup(objextname^);
  5363. if assigned(import_lib) then
  5364. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5365. tobjectdef(result).objectoptions:=objectoptions;
  5366. include(tobjectdef(result).defoptions,df_copied_def);
  5367. tobjectdef(result).extendeddef:=extendeddef;
  5368. if assigned(tcinitcode) then
  5369. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5370. tobjectdef(result).vmt_offset:=vmt_offset;
  5371. if assigned(iidguid) then
  5372. begin
  5373. new(tobjectdef(result).iidguid);
  5374. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5375. end;
  5376. if assigned(iidstr) then
  5377. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5378. tobjectdef(result).abstractcnt:=abstractcnt;
  5379. if assigned(ImplementedInterfaces) then
  5380. begin
  5381. for i:=0 to ImplementedInterfaces.count-1 do
  5382. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5383. end;
  5384. if assigned(vmtentries) then
  5385. begin
  5386. tobjectdef(result).vmtentries:=TFPList.Create;
  5387. tobjectdef(result).copyvmtentries(self);
  5388. end;
  5389. end;
  5390. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5391. var
  5392. i : longint;
  5393. vmtentry : pvmtentry;
  5394. ImplIntf : TImplementedInterface;
  5395. old_do_indirect_crc: boolean;
  5396. begin
  5397. { if class1 in unit A changes, and class2 in unit B inherits from it
  5398. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5399. However, if there is also a class3 in unit C that only depends on
  5400. unit B, then unit C will not be recompiled because nothing changed
  5401. to the interface of unit B. Nevertheless, unit C can indirectly
  5402. depend on unit A via derefs, and these must be updated -> the
  5403. indirect crc keeps track of such changes. }
  5404. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5405. ppufile.do_indirect_crc:=true;
  5406. inherited ppuwrite(ppufile);
  5407. ppufile.putbyte(byte(objecttype));
  5408. if assigned(objextname) then
  5409. ppufile.putstring(objextname^)
  5410. else
  5411. ppufile.putstring('');
  5412. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5413. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5414. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5415. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5416. ppufile.putlongint(vmt_offset);
  5417. ppufile.putderef(childofderef);
  5418. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5419. begin
  5420. ppufile.putguid(iidguid^);
  5421. ppufile.putstring(iidstr^);
  5422. end;
  5423. ppufile.putlongint(abstractcnt);
  5424. if objecttype=odt_helper then
  5425. ppufile.putderef(extendeddefderef);
  5426. ppufile.putlongint(vmtentries.count);
  5427. for i:=0 to vmtentries.count-1 do
  5428. begin
  5429. vmtentry:=pvmtentry(vmtentries[i]);
  5430. ppufile.putderef(vmtentry^.procdefderef);
  5431. ppufile.putbyte(byte(vmtentry^.visibility));
  5432. end;
  5433. if assigned(ImplementedInterfaces) then
  5434. begin
  5435. ppufile.putlongint(ImplementedInterfaces.Count);
  5436. for i:=0 to ImplementedInterfaces.Count-1 do
  5437. begin
  5438. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5439. ppufile.putderef(ImplIntf.intfdefderef);
  5440. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5441. ppufile.putlongint(ImplIntf.Ioffset);
  5442. ppufile.putbyte(byte(ImplIntf.IType));
  5443. end;
  5444. end;
  5445. if df_copied_def in defoptions then
  5446. ppufile.putderef(cloneddefderef);
  5447. writeentry(ppufile,ibobjectdef);
  5448. if not(df_copied_def in defoptions) then
  5449. tObjectSymtable(symtable).ppuwrite(ppufile);
  5450. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5451. end;
  5452. function tobjectdef.GetTypeName:string;
  5453. begin
  5454. { in this case we will go in endless recursion, because then }
  5455. { there is no tsym associated yet with the def. It can occur }
  5456. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5457. { instead of the actual type name }
  5458. if not assigned(typesym) then
  5459. result:='<Currently Parsed Class>'
  5460. else
  5461. result:=typesymbolprettyname;
  5462. end;
  5463. procedure tobjectdef.buildderef;
  5464. var
  5465. i : longint;
  5466. vmtentry : pvmtentry;
  5467. begin
  5468. inherited buildderef;
  5469. childofderef.build(childof);
  5470. if df_copied_def in defoptions then
  5471. cloneddefderef.build(symtable.defowner)
  5472. else
  5473. tstoredsymtable(symtable).buildderef;
  5474. if objecttype=odt_helper then
  5475. extendeddefderef.build(extendeddef);
  5476. for i:=0 to vmtentries.count-1 do
  5477. begin
  5478. vmtentry:=pvmtentry(vmtentries[i]);
  5479. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5480. end;
  5481. if assigned(ImplementedInterfaces) then
  5482. begin
  5483. for i:=0 to ImplementedInterfaces.count-1 do
  5484. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5485. end;
  5486. end;
  5487. procedure tobjectdef.deref;
  5488. var
  5489. i : longint;
  5490. vmtentry : pvmtentry;
  5491. begin
  5492. inherited deref;
  5493. childof:=tobjectdef(childofderef.resolve);
  5494. if df_copied_def in defoptions then
  5495. begin
  5496. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5497. symtable:=cloneddef.symtable.getcopy;
  5498. end
  5499. else
  5500. tstoredsymtable(symtable).deref;
  5501. if objecttype=odt_helper then
  5502. extendeddef:=tdef(extendeddefderef.resolve);
  5503. for i:=0 to vmtentries.count-1 do
  5504. begin
  5505. vmtentry:=pvmtentry(vmtentries[i]);
  5506. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5507. end;
  5508. if assigned(ImplementedInterfaces) then
  5509. begin
  5510. for i:=0 to ImplementedInterfaces.count-1 do
  5511. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5512. end;
  5513. end;
  5514. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5515. var
  5516. pd: tprocdef absolute def;
  5517. st: tsymtable;
  5518. psym: tsym;
  5519. nname: TIDString;
  5520. begin
  5521. if (tdef(def).typ<>procdef) then
  5522. exit;
  5523. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5524. owner = symtable in which objcclassdef is defined
  5525. }
  5526. st:=pd.owner.defowner.owner;
  5527. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5528. { check for an existing procsym with our special name }
  5529. psym:=tsym(st.find(nname));
  5530. if not assigned(psym) then
  5531. begin
  5532. psym:=cprocsym.create(nname);
  5533. { avoid warning about this symbol being unused }
  5534. psym.IncRefCount;
  5535. { don't check for duplicates:
  5536. a) we checked above
  5537. b) in case we are in the implementation section of a unit, this
  5538. will also check for this symbol in the interface section
  5539. (since you normally cannot have symbols with the same name
  5540. both interface and implementation), and it's possible to
  5541. have class helpers for the same class in the interface and
  5542. in the implementation, and they cannot be merged since only
  5543. the once in the interface must be saved to the ppu/visible
  5544. from other units }
  5545. st.insert(psym,false);
  5546. end
  5547. else if (psym.typ<>procsym) then
  5548. internalerror(2009111501);
  5549. { add ourselves to this special procsym }
  5550. tprocsym(psym).procdeflist.add(def);
  5551. end;
  5552. procedure tobjectdef.derefimpl;
  5553. begin
  5554. inherited derefimpl;
  5555. { the procdefs are not owned by the class helper procsyms, so they
  5556. are not stored/restored either -> re-add them here }
  5557. if (objecttype=odt_objcclass) or
  5558. (oo_is_classhelper in objectoptions) then
  5559. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  5560. end;
  5561. procedure tobjectdef.resetvmtentries;
  5562. var
  5563. i : longint;
  5564. begin
  5565. for i:=0 to vmtentries.Count-1 do
  5566. Dispose(pvmtentry(vmtentries[i]));
  5567. vmtentries.clear;
  5568. end;
  5569. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  5570. var
  5571. i : longint;
  5572. vmtentry : pvmtentry;
  5573. begin
  5574. resetvmtentries;
  5575. vmtentries.count:=objdef.vmtentries.count;
  5576. for i:=0 to objdef.vmtentries.count-1 do
  5577. begin
  5578. new(vmtentry);
  5579. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  5580. vmtentries[i]:=vmtentry;
  5581. end;
  5582. end;
  5583. function tobjectdef.getparentdef:tdef;
  5584. begin
  5585. { TODO: Remove getparentdef hack}
  5586. { With 2 forward declared classes with the child class before the
  5587. parent class the child class is written earlier to the ppu. Leaving it
  5588. possible to have a reference to the parent class for property overriding,
  5589. but the parent class still has the childof not resolved yet (PFV) }
  5590. if childof=nil then
  5591. childof:=tobjectdef(childofderef.resolve);
  5592. result:=childof;
  5593. end;
  5594. procedure tobjectdef.prepareguid;
  5595. begin
  5596. { set up guid }
  5597. if not assigned(iidguid) then
  5598. begin
  5599. new(iidguid);
  5600. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5601. end;
  5602. { setup iidstring }
  5603. if not assigned(iidstr) then
  5604. iidstr:=stringdup(''); { default is empty string }
  5605. end;
  5606. procedure tobjectdef.set_parent( c : tobjectdef);
  5607. begin
  5608. if assigned(childof) then
  5609. exit;
  5610. childof:=c;
  5611. if not assigned(c) then
  5612. exit;
  5613. { inherit options and status }
  5614. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5615. { initially has the same number of abstract methods as the parent }
  5616. abstractcnt:=c.abstractcnt;
  5617. { add the data of the anchestor class/object }
  5618. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5619. begin
  5620. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5621. { inherit recordalignment }
  5622. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5623. { if both the parent and this record use C-alignment, also inherit
  5624. the current field alignment }
  5625. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5626. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5627. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5628. { the padding is not inherited for Objective-C classes (maybe not
  5629. for cppclass either?) }
  5630. if objecttype=odt_objcclass then
  5631. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5632. if (oo_has_vmt in objectoptions) and
  5633. (oo_has_vmt in c.objectoptions) then
  5634. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5635. { if parent has a vmt field then the offset is the same for the child PM }
  5636. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5637. begin
  5638. vmt_offset:=c.vmt_offset;
  5639. include(objectoptions,oo_has_vmt);
  5640. end;
  5641. end;
  5642. end;
  5643. procedure tobjectdef.insertvmt;
  5644. var
  5645. vs: tfieldvarsym;
  5646. begin
  5647. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5648. exit;
  5649. if (oo_has_vmt in objectoptions) then
  5650. internalerror(12345)
  5651. else
  5652. begin
  5653. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5654. tObjectSymtable(symtable).fieldalignment);
  5655. if (tf_requires_proper_alignment in target_info.flags) then
  5656. begin
  5657. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5658. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5659. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5660. end;
  5661. vs:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5662. hidesym(vs);
  5663. tObjectSymtable(symtable).insert(vs);
  5664. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5665. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5666. vmt_offset:=vs.fieldoffset
  5667. else
  5668. vmt_offset:=vs.fieldoffset div 8;
  5669. include(objectoptions,oo_has_vmt);
  5670. end;
  5671. end;
  5672. procedure tobjectdef.check_forwards;
  5673. begin
  5674. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5675. inherited;
  5676. if (oo_is_forward in objectoptions) then
  5677. begin
  5678. { ok, in future, the forward can be resolved }
  5679. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5680. exclude(objectoptions,oo_is_forward);
  5681. end;
  5682. end;
  5683. function tobjectdef.find_destructor: tprocdef;
  5684. var
  5685. objdef: tobjectdef;
  5686. begin
  5687. objdef:=self;
  5688. while assigned(objdef) do
  5689. begin
  5690. result:=objdef.find_procdef_bytype(potype_destructor);
  5691. if assigned(result) then
  5692. exit;
  5693. objdef:=objdef.childof;
  5694. end;
  5695. result:=nil;
  5696. end;
  5697. function tobjectdef.implements_any_interfaces: boolean;
  5698. begin
  5699. result := (ImplementedInterfaces.Count > 0) or
  5700. (assigned(childof) and childof.implements_any_interfaces);
  5701. end;
  5702. function tobjectdef.size : asizeint;
  5703. begin
  5704. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5705. result:=voidpointertype.size
  5706. else
  5707. result:=tObjectSymtable(symtable).datasize;
  5708. end;
  5709. function tobjectdef.alignment:shortint;
  5710. begin
  5711. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5712. alignment:=voidpointertype.size
  5713. else
  5714. alignment:=tObjectSymtable(symtable).recordalignment;
  5715. end;
  5716. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5717. begin
  5718. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5719. case objecttype of
  5720. odt_class:
  5721. { the +2*sizeof(pint) is size and -size }
  5722. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  5723. odt_helper,
  5724. odt_objcclass,
  5725. odt_objcprotocol:
  5726. vmtmethodoffset:=0;
  5727. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5728. vmtmethodoffset:=index*voidcodepointertype.size;
  5729. odt_javaclass,
  5730. odt_interfacejava:
  5731. { invalid }
  5732. vmtmethodoffset:=-1;
  5733. else
  5734. { the +2*sizeof(pint) is size and -size }
  5735. {$ifdef WITHDMT}
  5736. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  5737. {$else WITHDMT}
  5738. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  5739. {$endif WITHDMT}
  5740. end;
  5741. end;
  5742. function tobjectdef.vmt_mangledname : TSymStr;
  5743. begin
  5744. if not(oo_has_vmt in objectoptions) then
  5745. Message1(parser_n_object_has_no_vmt,objrealname^);
  5746. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5747. end;
  5748. function tobjectdef.needs_inittable : boolean;
  5749. var
  5750. hp : tobjectdef;
  5751. begin
  5752. case objecttype of
  5753. odt_helper,
  5754. odt_class :
  5755. needs_inittable:=false;
  5756. odt_dispinterface,
  5757. odt_interfacecom:
  5758. needs_inittable:=true;
  5759. odt_interfacecorba:
  5760. begin
  5761. hp:=childof;
  5762. while assigned(hp) do
  5763. begin
  5764. if hp=interface_iunknown then
  5765. begin
  5766. needs_inittable:=true;
  5767. exit;
  5768. end;
  5769. hp:=hp.childof;
  5770. end;
  5771. needs_inittable:=false;
  5772. end;
  5773. odt_object:
  5774. needs_inittable:=
  5775. tObjectSymtable(symtable).needs_init_final or
  5776. (assigned(childof) and
  5777. childof.needs_inittable);
  5778. odt_cppclass,
  5779. odt_objcclass,
  5780. odt_objcprotocol,
  5781. odt_javaclass,
  5782. odt_interfacejava:
  5783. needs_inittable:=false;
  5784. else
  5785. internalerror(200108267);
  5786. end;
  5787. end;
  5788. function tobjectdef.needs_separate_initrtti : boolean;
  5789. begin
  5790. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5791. end;
  5792. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5793. begin
  5794. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5795. result:=inherited rtti_mangledname(rt)
  5796. else
  5797. begin
  5798. { necessary in case of a dynamic array of nsobject, or
  5799. if an nsobject field appears in a record that needs
  5800. init/finalisation }
  5801. if rt=initrtti then
  5802. begin
  5803. result:=voidpointertype.rtti_mangledname(rt);
  5804. exit;
  5805. end;
  5806. if not(target_info.system in systems_objc_nfabi) then
  5807. begin
  5808. result:=target_asm.labelprefix;
  5809. case objecttype of
  5810. odt_objcclass:
  5811. begin
  5812. case rt of
  5813. objcclassrtti:
  5814. if not(oo_is_classhelper in objectoptions) then
  5815. result:=result+'_OBJC_CLASS_'
  5816. else
  5817. result:=result+'_OBJC_CATEGORY_';
  5818. objcmetartti:
  5819. if not(oo_is_classhelper in objectoptions) then
  5820. result:=result+'_OBJC_METACLASS_'
  5821. else
  5822. internalerror(2009111511);
  5823. else
  5824. internalerror(2009092302);
  5825. end;
  5826. end;
  5827. odt_objcprotocol:
  5828. result:=result+'_OBJC_PROTOCOL_';
  5829. end;
  5830. end
  5831. else
  5832. begin
  5833. case objecttype of
  5834. odt_objcclass:
  5835. begin
  5836. if (oo_is_classhelper in objectoptions) and
  5837. (rt<>objcclassrtti) then
  5838. internalerror(2009111512);
  5839. case rt of
  5840. objcclassrtti:
  5841. if not(oo_is_classhelper in objectoptions) then
  5842. result:='_OBJC_CLASS_$_'
  5843. else
  5844. result:='_OBJC_$_CATEGORY_';
  5845. objcmetartti:
  5846. result:='_OBJC_METACLASS_$_';
  5847. objcclassrortti:
  5848. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5849. objcmetarortti:
  5850. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5851. else
  5852. internalerror(2009092303);
  5853. end;
  5854. end;
  5855. odt_objcprotocol:
  5856. begin
  5857. result:=lower(target_asm.labelprefix);
  5858. case rt of
  5859. objcclassrtti:
  5860. result:=result+'_OBJC_PROTOCOL_$_';
  5861. objcmetartti:
  5862. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5863. else
  5864. internalerror(2009092501);
  5865. end;
  5866. end;
  5867. else
  5868. internalerror(2013113005);
  5869. end;
  5870. end;
  5871. result:=result+objextname^;
  5872. end;
  5873. end;
  5874. function tobjectdef.members_need_inittable : boolean;
  5875. begin
  5876. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5877. end;
  5878. function tobjectdef.is_publishable : boolean;
  5879. begin
  5880. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5881. end;
  5882. function tobjectdef.get_next_dispid: longint;
  5883. begin
  5884. inc(fcurrent_dispid);
  5885. result:=fcurrent_dispid;
  5886. end;
  5887. function tobjectdef.search_enumerator_get: tprocdef;
  5888. begin
  5889. result:=inherited;
  5890. if not assigned(result) and assigned(childof) then
  5891. result:=childof.search_enumerator_get;
  5892. end;
  5893. function tobjectdef.search_enumerator_move: tprocdef;
  5894. begin
  5895. result:=inherited;
  5896. if not assigned(result) and assigned(childof) then
  5897. result:=childof.search_enumerator_move;
  5898. end;
  5899. function tobjectdef.search_enumerator_current: tsym;
  5900. begin
  5901. result:=inherited;
  5902. if not assigned(result) and assigned(childof) then
  5903. result:=childof.search_enumerator_current;
  5904. end;
  5905. procedure tobjectdef.register_created_classref_type;
  5906. begin
  5907. if not classref_created_in_current_module then
  5908. begin
  5909. classref_created_in_current_module:=true;
  5910. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5911. end;
  5912. end;
  5913. procedure tobjectdef.register_created_object_type;
  5914. begin
  5915. if not created_in_current_module then
  5916. begin
  5917. created_in_current_module:=true;
  5918. current_module.wpoinfo.addcreatedobjtype(self);
  5919. end;
  5920. end;
  5921. procedure tobjectdef.register_maybe_created_object_type;
  5922. begin
  5923. { if we know it has been created for sure, no need
  5924. to also record that it maybe can be created in
  5925. this module
  5926. }
  5927. if not (created_in_current_module) and
  5928. not (maybe_created_in_current_module) then
  5929. begin
  5930. maybe_created_in_current_module:=true;
  5931. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5932. end;
  5933. end;
  5934. procedure tobjectdef.register_vmt_call(index: longint);
  5935. begin
  5936. if (is_object(self) or is_class(self)) then
  5937. current_module.wpoinfo.addcalledvmtentry(self,index);
  5938. end;
  5939. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5940. var
  5941. def: tdef absolute data;
  5942. pd: tprocdef absolute data;
  5943. i,
  5944. paracount: longint;
  5945. begin
  5946. if (def.typ=procdef) then
  5947. begin
  5948. { add all messages also under a dummy name to the symtable in
  5949. which the objcclass/protocol/category is declared, so they can
  5950. be called via id.<name>
  5951. }
  5952. create_class_helper_for_procdef(pd,nil);
  5953. { we have to wait until now to set the mangled name because it
  5954. depends on the (possibly external) class name, which is defined
  5955. at the very end. }
  5956. if not(po_msgstr in pd.procoptions) then
  5957. begin
  5958. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5959. { recover to avoid internalerror later on }
  5960. include(pd.procoptions,po_msgstr);
  5961. pd.messageinf.str:=stringdup('MissingDeclaration');
  5962. end;
  5963. { Mangled name is already set in case this is a copy of
  5964. another type. }
  5965. if not(po_has_mangledname in pd.procoptions) then
  5966. begin
  5967. { check whether the number of formal parameters is correct,
  5968. and whether they have valid Objective-C types }
  5969. paracount:=0;
  5970. for i:=1 to length(pd.messageinf.str^) do
  5971. if pd.messageinf.str^[i]=':' then
  5972. inc(paracount);
  5973. for i:=0 to pd.paras.count-1 do
  5974. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5975. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5976. dec(paracount);
  5977. if (paracount<>0) then
  5978. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5979. pd.setmangledname(pd.objcmangledname);
  5980. end
  5981. else
  5982. { all checks already done }
  5983. exit;
  5984. if not(oo_is_external in pd.struct.objectoptions) then
  5985. begin
  5986. if (po_varargs in pd.procoptions) then
  5987. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  5988. else
  5989. begin
  5990. { check for "array of const" parameters }
  5991. for i:=0 to pd.parast.symlist.count-1 do
  5992. begin
  5993. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  5994. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  5995. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  5996. end;
  5997. end;
  5998. end;
  5999. end;
  6000. end;
  6001. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6002. var
  6003. sym: tsym absolute data;
  6004. begin
  6005. if (sym.typ=fieldvarsym) and
  6006. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6007. sym.IncRefCount;
  6008. end;
  6009. procedure tobjectdef.finish_objc_data;
  6010. begin
  6011. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6012. if (oo_is_external in objectoptions) then
  6013. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6014. end;
  6015. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6016. var
  6017. sym: tabstractvarsym absolute data;
  6018. res: pboolean absolute arg;
  6019. founderrordef: tdef;
  6020. begin
  6021. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6022. exit;
  6023. if (sym.typ=paravarsym) and
  6024. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6025. is_array_of_const(tparavarsym(sym).vardef)) then
  6026. exit;
  6027. if not objcchecktype(sym.vardef,founderrordef) then
  6028. begin
  6029. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6030. res^:=false;
  6031. end;
  6032. end;
  6033. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6034. var
  6035. def: tdef absolute data;
  6036. res: pboolean absolute arg;
  6037. founderrordef: tdef;
  6038. begin
  6039. if (def.typ<>procdef) then
  6040. exit;
  6041. { check parameter types for validity }
  6042. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6043. { check the result type for validity }
  6044. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6045. begin
  6046. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6047. res^:=false;
  6048. end;
  6049. end;
  6050. function tobjectdef.check_objc_types: boolean;
  6051. begin
  6052. { done in separate step from finish_objc_data, because when
  6053. finish_objc_data is called, not all forwarddefs have been resolved
  6054. yet and we need to know all types here }
  6055. result:=true;
  6056. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6057. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6058. end;
  6059. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6060. var
  6061. def: tdef absolute data;
  6062. pd: tprocdef absolute data;
  6063. begin
  6064. if (def.typ=procdef) then
  6065. begin
  6066. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6067. if (oo_is_external in pd.struct.objectoptions) then
  6068. begin
  6069. { copied from psub.read_proc }
  6070. if assigned(tobjectdef(pd.struct).import_lib) then
  6071. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6072. else
  6073. begin
  6074. { add import name to external list for DLL scanning }
  6075. if tf_has_dllscanner in target_info.flags then
  6076. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6077. end;
  6078. end;
  6079. end;
  6080. end;
  6081. procedure tobjectdef.finish_cpp_data;
  6082. begin
  6083. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6084. end;
  6085. {****************************************************************************
  6086. TImplementedInterface
  6087. ****************************************************************************}
  6088. function TImplementedInterface.GetIOffset: longint;
  6089. begin
  6090. if (fIOffset=-1) and
  6091. (IType in [etFieldValue,etFieldValueClass]) then
  6092. result:=tfieldvarsym(ImplementsField).fieldoffset
  6093. else
  6094. result:=fIOffset;
  6095. end;
  6096. constructor TImplementedInterface.create(aintf: tobjectdef);
  6097. begin
  6098. inherited create;
  6099. intfdef:=aintf;
  6100. IOffset:=-1;
  6101. IType:=etStandard;
  6102. NameMappings:=nil;
  6103. procdefs:=nil;
  6104. end;
  6105. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6106. begin
  6107. inherited create;
  6108. intfdef:=nil;
  6109. intfdefderef:=intfd;
  6110. ImplementsGetterDeref:=getterd;
  6111. IOffset:=-1;
  6112. IType:=etStandard;
  6113. NameMappings:=nil;
  6114. procdefs:=nil;
  6115. end;
  6116. destructor TImplementedInterface.destroy;
  6117. var
  6118. i : longint;
  6119. mappedname : pshortstring;
  6120. begin
  6121. if assigned(NameMappings) then
  6122. begin
  6123. for i:=0 to NameMappings.Count-1 do
  6124. begin
  6125. mappedname:=pshortstring(NameMappings[i]);
  6126. stringdispose(mappedname);
  6127. end;
  6128. NameMappings.free;
  6129. NameMappings:=nil;
  6130. end;
  6131. if assigned(procdefs) then
  6132. begin
  6133. procdefs.free;
  6134. procdefs:=nil;
  6135. end;
  6136. inherited destroy;
  6137. end;
  6138. procedure TImplementedInterface.buildderef;
  6139. begin
  6140. intfdefderef.build(intfdef);
  6141. ImplementsGetterDeref.build(ImplementsGetter);
  6142. end;
  6143. procedure TImplementedInterface.deref;
  6144. begin
  6145. intfdef:=tobjectdef(intfdefderef.resolve);
  6146. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6147. end;
  6148. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6149. begin
  6150. if not assigned(NameMappings) then
  6151. NameMappings:=TFPHashList.Create;
  6152. NameMappings.Add(origname,stringdup(newname));
  6153. end;
  6154. function TImplementedInterface.GetMapping(const origname: string):string;
  6155. var
  6156. mappedname : pshortstring;
  6157. begin
  6158. result:='';
  6159. if not assigned(NameMappings) then
  6160. exit;
  6161. mappedname:=PShortstring(NameMappings.Find(origname));
  6162. if assigned(mappedname) then
  6163. result:=mappedname^;
  6164. end;
  6165. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6166. begin
  6167. if not assigned(procdefs) then
  6168. procdefs:=TFPObjectList.Create(false);
  6169. { duplicate entries must be stored, because multiple }
  6170. { interfaces can declare methods with the same name }
  6171. { and all of these get their own VMT entry }
  6172. procdefs.Add(pd);
  6173. end;
  6174. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6175. var
  6176. i : longint;
  6177. begin
  6178. result:=false;
  6179. { interfaces being implemented through delegation are not mergable (FK) }
  6180. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6181. exit;
  6182. weight:=0;
  6183. { empty interface is mergeable }
  6184. if ProcDefs.Count=0 then
  6185. begin
  6186. result:=true;
  6187. exit;
  6188. end;
  6189. { The interface to merge must at least the number of
  6190. procedures of this interface }
  6191. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6192. exit;
  6193. for i:=0 to ProcDefs.Count-1 do
  6194. begin
  6195. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6196. exit;
  6197. end;
  6198. weight:=ProcDefs.Count;
  6199. result:=true;
  6200. end;
  6201. function TImplementedInterface.getcopy:TImplementedInterface;
  6202. begin
  6203. Result:=TImplementedInterface.Create(nil);
  6204. { 1) the procdefs list will be freed once for each copy
  6205. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6206. 3) idem for the name mappings
  6207. }
  6208. { warning: this is completely wrong on so many levels...
  6209. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6210. We need to make clean copies of the different fields
  6211. this is not implemented yet, and thus we generate an internal
  6212. error instead PM 2011-06-14 }
  6213. internalerror(2011061401);
  6214. end;
  6215. {****************************************************************************
  6216. TFORWARDDEF
  6217. ****************************************************************************}
  6218. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6219. begin
  6220. inherited create(forwarddef);
  6221. tosymname:=stringdup(s);
  6222. forwardpos:=pos;
  6223. end;
  6224. function tforwarddef.GetTypeName:string;
  6225. begin
  6226. GetTypeName:='unresolved forward to '+tosymname^;
  6227. end;
  6228. destructor tforwarddef.destroy;
  6229. begin
  6230. stringdispose(tosymname);
  6231. inherited destroy;
  6232. end;
  6233. function tforwarddef.getcopy:tstoreddef;
  6234. begin
  6235. result:=cforwarddef.create(tosymname^, forwardpos);
  6236. end;
  6237. {****************************************************************************
  6238. TUNDEFINEDDEF
  6239. ****************************************************************************}
  6240. constructor tundefineddef.create;
  6241. begin
  6242. inherited create(undefineddef);
  6243. end;
  6244. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6245. begin
  6246. inherited ppuload(undefineddef,ppufile);
  6247. ppuload_platform(ppufile);
  6248. end;
  6249. function tundefineddef.GetTypeName:string;
  6250. begin
  6251. GetTypeName:='<undefined type>';
  6252. end;
  6253. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6254. begin
  6255. inherited ppuwrite(ppufile);
  6256. writeentry(ppufile,ibundefineddef);
  6257. end;
  6258. {****************************************************************************
  6259. TERRORDEF
  6260. ****************************************************************************}
  6261. constructor terrordef.create;
  6262. begin
  6263. inherited create(errordef);
  6264. { prevent consecutive faults }
  6265. savesize:=1;
  6266. end;
  6267. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6268. begin
  6269. { Can't write errordefs to ppu }
  6270. internalerror(200411063);
  6271. end;
  6272. function terrordef.GetTypeName:string;
  6273. begin
  6274. GetTypeName:='<erroneous type>';
  6275. end;
  6276. function terrordef.getmangledparaname:TSymStr;
  6277. begin
  6278. getmangledparaname:='error';
  6279. end;
  6280. {****************************************************************************
  6281. Definition Helpers
  6282. ****************************************************************************}
  6283. function is_interfacecom(def: tdef): boolean;
  6284. begin
  6285. is_interfacecom:=
  6286. assigned(def) and
  6287. (def.typ=objectdef) and
  6288. (tobjectdef(def).objecttype=odt_interfacecom);
  6289. end;
  6290. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6291. begin
  6292. is_interfacecom_or_dispinterface:=
  6293. assigned(def) and
  6294. (def.typ=objectdef) and
  6295. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6296. end;
  6297. function is_any_interface_kind(def: tdef): boolean;
  6298. begin
  6299. result:=
  6300. assigned(def) and
  6301. (def.typ=objectdef) and
  6302. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6303. is_objccategory(def));
  6304. end;
  6305. function is_interfacecorba(def: tdef): boolean;
  6306. begin
  6307. is_interfacecorba:=
  6308. assigned(def) and
  6309. (def.typ=objectdef) and
  6310. (tobjectdef(def).objecttype=odt_interfacecorba);
  6311. end;
  6312. function is_interface(def: tdef): boolean;
  6313. begin
  6314. is_interface:=
  6315. assigned(def) and
  6316. (def.typ=objectdef) and
  6317. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6318. end;
  6319. function is_dispinterface(def: tdef): boolean;
  6320. begin
  6321. result:=
  6322. assigned(def) and
  6323. (def.typ=objectdef) and
  6324. (tobjectdef(def).objecttype=odt_dispinterface);
  6325. end;
  6326. function is_class(def: tdef): boolean;
  6327. begin
  6328. is_class:=
  6329. assigned(def) and
  6330. (def.typ=objectdef) and
  6331. (tobjectdef(def).objecttype=odt_class);
  6332. end;
  6333. function is_object(def: tdef): boolean;
  6334. begin
  6335. is_object:=
  6336. assigned(def) and
  6337. (def.typ=objectdef) and
  6338. (tobjectdef(def).objecttype=odt_object);
  6339. end;
  6340. function is_cppclass(def: tdef): boolean;
  6341. begin
  6342. is_cppclass:=
  6343. assigned(def) and
  6344. (def.typ=objectdef) and
  6345. (tobjectdef(def).objecttype=odt_cppclass);
  6346. end;
  6347. function is_objcclass(def: tdef): boolean;
  6348. begin
  6349. is_objcclass:=
  6350. assigned(def) and
  6351. (def.typ=objectdef) and
  6352. (tobjectdef(def).objecttype=odt_objcclass);
  6353. end;
  6354. function is_objectpascal_helper(def: tdef): boolean;
  6355. begin
  6356. result:=
  6357. assigned(def) and
  6358. (def.typ=objectdef) and
  6359. (tobjectdef(def).objecttype=odt_helper);
  6360. end;
  6361. function is_objcclassref(def: tdef): boolean;
  6362. begin
  6363. is_objcclassref:=
  6364. assigned(def) and
  6365. (def.typ=classrefdef) and
  6366. is_objcclass(tclassrefdef(def).pointeddef);
  6367. end;
  6368. function is_objcprotocol(def: tdef): boolean;
  6369. begin
  6370. result:=
  6371. assigned(def) and
  6372. (def.typ=objectdef) and
  6373. (tobjectdef(def).objecttype=odt_objcprotocol);
  6374. end;
  6375. function is_objccategory(def: tdef): boolean;
  6376. begin
  6377. result:=
  6378. assigned(def) and
  6379. (def.typ=objectdef) and
  6380. { if used as a forward type }
  6381. ((tobjectdef(def).objecttype=odt_objccategory) or
  6382. { if used as after it has been resolved }
  6383. ((tobjectdef(def).objecttype=odt_objcclass) and
  6384. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6385. end;
  6386. function is_objc_class_or_protocol(def: tdef): boolean;
  6387. begin
  6388. result:=
  6389. assigned(def) and
  6390. (def.typ=objectdef) and
  6391. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6392. end;
  6393. function is_objc_protocol_or_category(def: tdef): boolean;
  6394. begin
  6395. result:=
  6396. assigned(def) and
  6397. (def.typ=objectdef) and
  6398. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6399. ((tobjectdef(def).objecttype = odt_objcclass) and
  6400. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6401. end;
  6402. function is_classhelper(def: tdef): boolean;
  6403. begin
  6404. result:=
  6405. is_objectpascal_helper(def) or
  6406. is_objccategory(def);
  6407. end;
  6408. function is_class_or_interface(def: tdef): boolean;
  6409. begin
  6410. result:=
  6411. assigned(def) and
  6412. (def.typ=objectdef) and
  6413. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6414. end;
  6415. function is_class_or_interface_or_objc(def: tdef): boolean;
  6416. begin
  6417. result:=
  6418. assigned(def) and
  6419. (def.typ=objectdef) and
  6420. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6421. end;
  6422. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6423. begin
  6424. result:=
  6425. assigned(def) and
  6426. (def.typ=objectdef) and
  6427. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6428. end;
  6429. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6430. begin
  6431. result:=
  6432. assigned(def) and
  6433. (def.typ=objectdef) and
  6434. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6435. end;
  6436. function is_class_or_interface_or_object(def: tdef): boolean;
  6437. begin
  6438. result:=
  6439. assigned(def) and
  6440. (def.typ=objectdef) and
  6441. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6442. end;
  6443. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6444. begin
  6445. result:=
  6446. assigned(def) and
  6447. (def.typ=objectdef) and
  6448. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6449. end;
  6450. function is_implicit_pointer_object_type(def: tdef): boolean;
  6451. begin
  6452. result:=
  6453. assigned(def) and
  6454. (((def.typ=objectdef) and
  6455. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6456. ((target_info.system in systems_jvm) and
  6457. (def.typ=recorddef)));
  6458. end;
  6459. function is_implicit_array_pointer(def: tdef): boolean;
  6460. begin
  6461. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6462. end;
  6463. function is_class_or_object(def: tdef): boolean;
  6464. begin
  6465. result:=
  6466. assigned(def) and
  6467. (def.typ=objectdef) and
  6468. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6469. end;
  6470. function is_record(def: tdef): boolean;
  6471. begin
  6472. result:=
  6473. assigned(def) and
  6474. (def.typ=recorddef);
  6475. end;
  6476. function is_javaclass(def: tdef): boolean;
  6477. begin
  6478. result:=
  6479. assigned(def) and
  6480. (def.typ=objectdef) and
  6481. (tobjectdef(def).objecttype=odt_javaclass);
  6482. end;
  6483. function is_javaclassref(def: tdef): boolean;
  6484. begin
  6485. is_javaclassref:=
  6486. assigned(def) and
  6487. (def.typ=classrefdef) and
  6488. is_javaclass(tclassrefdef(def).pointeddef);
  6489. end;
  6490. function is_javainterface(def: tdef): boolean;
  6491. begin
  6492. result:=
  6493. assigned(def) and
  6494. (def.typ=objectdef) and
  6495. (tobjectdef(def).objecttype=odt_interfacejava);
  6496. end;
  6497. function is_java_class_or_interface(def: tdef): boolean;
  6498. begin
  6499. result:=
  6500. assigned(def) and
  6501. (def.typ=objectdef) and
  6502. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6503. end;
  6504. procedure loadobjctypes;
  6505. begin
  6506. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6507. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6508. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6509. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6510. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6511. end;
  6512. procedure maybeloadcocoatypes;
  6513. var
  6514. tsym: ttypesym;
  6515. cocoaunit: string[15];
  6516. begin
  6517. if assigned(objc_fastenumeration) then
  6518. exit;
  6519. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6520. cocoaunit:='COCOAALL'
  6521. else
  6522. cocoaunit:='IPHONEALL';
  6523. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6524. if assigned(tsym) then
  6525. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6526. else
  6527. objc_fastenumeration:=nil;
  6528. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6529. if assigned(tsym) then
  6530. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6531. else
  6532. objc_fastenumerationstate:=nil;
  6533. end;
  6534. function use_vectorfpu(def : tdef) : boolean;
  6535. begin
  6536. {$ifdef x86}
  6537. {$define use_vectorfpuimplemented}
  6538. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6539. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6540. {$endif x86}
  6541. {$ifdef arm}
  6542. {$define use_vectorfpuimplemented}
  6543. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6544. {$endif arm}
  6545. {$ifndef use_vectorfpuimplemented}
  6546. use_vectorfpu:=false;
  6547. {$endif}
  6548. end;
  6549. function getpointerdef(def: tdef): tpointerdef;
  6550. var
  6551. res: PHashSetItem;
  6552. oldsymtablestack: tsymtablestack;
  6553. begin
  6554. if not assigned(current_module) then
  6555. internalerror(2011071101);
  6556. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6557. if not assigned(res^.Data) then
  6558. begin
  6559. { since these pointerdefs can be reused anywhere in the current
  6560. unit, add them to the global/staticsymtable }
  6561. oldsymtablestack:=symtablestack;
  6562. { do not simply push/pop current_module.localsymtable, because
  6563. that can have side-effects (e.g., it removes helpers) }
  6564. symtablestack:=nil;
  6565. res^.Data:=cpointerdef.create(def);
  6566. if assigned(current_module.localsymtable) then
  6567. current_module.localsymtable.insertdef(tdef(res^.Data))
  6568. else
  6569. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6570. symtablestack:=oldsymtablestack;
  6571. end;
  6572. result:=tpointerdef(res^.Data);
  6573. end;
  6574. function getsingletonarraydef(def: tdef): tarraydef;
  6575. begin
  6576. result:=getarraydef(def,1);
  6577. end;
  6578. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6579. var
  6580. res: PHashSetItem;
  6581. oldsymtablestack: tsymtablestack;
  6582. arrdesc: packed record
  6583. def: tdef;
  6584. elecount: asizeint;
  6585. end;
  6586. begin
  6587. if not assigned(current_module) then
  6588. internalerror(2011081301);
  6589. arrdesc.def:=def;
  6590. arrdesc.elecount:=elecount;
  6591. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6592. if not assigned(res^.Data) then
  6593. begin
  6594. { since these arraydef can be reused anywhere in the current
  6595. unit, add them to the global/staticsymtable }
  6596. oldsymtablestack:=symtablestack;
  6597. symtablestack:=nil;
  6598. res^.Data:=carraydef.create(0,elecount-1,ptrsinttype);
  6599. tarraydef(res^.Data).elementdef:=def;
  6600. if assigned(current_module.localsymtable) then
  6601. current_module.localsymtable.insertdef(tdef(res^.Data))
  6602. else
  6603. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6604. symtablestack:=oldsymtablestack;
  6605. end;
  6606. result:=tarraydef(res^.Data);
  6607. end;
  6608. end.