symdef.pas 246 KB

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