symdef.pas 246 KB

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