symdef.pas 244 KB

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