symdef.pas 250 KB

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