symdef.pas 246 KB

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