symdef.pas 244 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549
  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 symansistr}
  4681. mangledname:=_mangledname;
  4682. {$else symansistr}
  4683. mangledname:=_mangledname^;
  4684. {$endif symansistr}
  4685. exit;
  4686. end;
  4687. {$ifndef jvm}
  4688. mangledname:=defaultmangledname;
  4689. {$else not jvm}
  4690. mangledname:=jvmmangledbasename(false);
  4691. if (po_has_importdll in procoptions) then
  4692. begin
  4693. { import_dll comes from "external 'import_dll_name' name 'external_name'" }
  4694. if assigned(import_dll) then
  4695. mangledname:=import_dll^+'/'+mangledname
  4696. else
  4697. internalerror(2010122607);
  4698. end
  4699. else
  4700. jvmaddtypeownerprefix(owner,mangledname);
  4701. {$endif not jvm}
  4702. {$ifdef symansistr}
  4703. _mangledname:=mangledname;
  4704. {$else symansistr}
  4705. _mangledname:=stringdup(mangledname);
  4706. {$endif symansistr}
  4707. end;
  4708. function tprocdef.defaultmangledname: TSymStr;
  4709. var
  4710. hp : TParavarsym;
  4711. hs : TSymStr;
  4712. crc : dword;
  4713. newlen,
  4714. oldlen,
  4715. i : integer;
  4716. begin
  4717. hp:=nil;
  4718. { we need to use the symtable where the procsym is inserted,
  4719. because that is visible to the world }
  4720. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4721. oldlen:=length(defaultmangledname);
  4722. { add parameter types }
  4723. for i:=0 to paras.count-1 do
  4724. begin
  4725. hp:=tparavarsym(paras[i]);
  4726. if not(vo_is_hidden_para in hp.varoptions) then
  4727. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4728. end;
  4729. { add resultdef, add $$ as separator to make it unique from a
  4730. parameter separator }
  4731. if not is_void(returndef) then
  4732. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4733. newlen:=length(defaultmangledname);
  4734. { Replace with CRC if the parameter line is very long }
  4735. if (newlen-oldlen>12) and
  4736. ((newlen>100) or (newlen-oldlen>64)) then
  4737. begin
  4738. crc:=0;
  4739. for i:=0 to paras.count-1 do
  4740. begin
  4741. hp:=tparavarsym(paras[i]);
  4742. if not(vo_is_hidden_para in hp.varoptions) then
  4743. begin
  4744. hs:=hp.vardef.mangledparaname;
  4745. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4746. end;
  4747. end;
  4748. hs:=hp.vardef.mangledparaname;
  4749. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4750. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4751. end;
  4752. end;
  4753. function tprocdef.cplusplusmangledname : TSymStr;
  4754. function getcppparaname(p : tdef) : TSymStr;
  4755. const
  4756. {$ifdef NAMEMANGLING_GCC2}
  4757. ordtype2str : array[tordtype] of string[2] = (
  4758. '',
  4759. 'Uc','Us','Ui','Us',
  4760. 'Sc','s','i','x',
  4761. 'b','b','b','b','b',
  4762. 'c','w','x');
  4763. {$else NAMEMANGLING_GCC2}
  4764. ordtype2str : array[tordtype] of string[1] = (
  4765. 'v',
  4766. 'h','t','j','y',
  4767. 'a','s','i','x',
  4768. 'b','b','b','b',
  4769. 'b','b','b','b',
  4770. 'c','w','x');
  4771. floattype2str : array[tfloattype] of string[1] = (
  4772. 'f','d','e','e',
  4773. 'd','d','g');
  4774. {$endif NAMEMANGLING_GCC2}
  4775. var
  4776. s : TSymStr;
  4777. begin
  4778. case p.typ of
  4779. orddef:
  4780. s:=ordtype2str[torddef(p).ordtype];
  4781. pointerdef:
  4782. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4783. {$ifndef NAMEMANGLING_GCC2}
  4784. floatdef:
  4785. s:=floattype2str[tfloatdef(p).floattype];
  4786. {$endif NAMEMANGLING_GCC2}
  4787. else
  4788. internalerror(2103001);
  4789. end;
  4790. getcppparaname:=s;
  4791. end;
  4792. var
  4793. s,s2 : TSymStr;
  4794. hp : TParavarsym;
  4795. i : integer;
  4796. begin
  4797. {$ifdef NAMEMANGLING_GCC2}
  4798. { outdated gcc 2.x name mangling scheme }
  4799. s := procsym.realname;
  4800. if procsym.owner.symtabletype=ObjectSymtable then
  4801. begin
  4802. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4803. case proctypeoption of
  4804. potype_destructor:
  4805. s:='_$_'+tostr(length(s2))+s2;
  4806. potype_constructor:
  4807. s:='___'+tostr(length(s2))+s2;
  4808. else
  4809. s:='_'+s+'__'+tostr(length(s2))+s2;
  4810. end;
  4811. end
  4812. else s:=s+'__';
  4813. s:=s+'F';
  4814. { concat modifiers }
  4815. { !!!!! }
  4816. { now we handle the parameters }
  4817. if maxparacount>0 then
  4818. begin
  4819. for i:=0 to paras.count-1 do
  4820. begin
  4821. hp:=tparavarsym(paras[i]);
  4822. { no hidden parameters form part of a C++ mangled name:
  4823. a) self is not included
  4824. b) there are no "high" or other hidden parameters
  4825. }
  4826. if vo_is_hidden_para in hp.varoptions then
  4827. continue;
  4828. s2:=getcppparaname(hp.vardef);
  4829. if hp.varspez in [vs_var,vs_out] then
  4830. s2:='R'+s2;
  4831. s:=s+s2;
  4832. end;
  4833. end
  4834. else
  4835. s:=s+'v';
  4836. cplusplusmangledname:=s;
  4837. {$else NAMEMANGLING_GCC2}
  4838. { gcc 3.x and 4.x name mangling scheme }
  4839. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4840. if procsym.owner.symtabletype=ObjectSymtable then
  4841. begin
  4842. s:='_ZN';
  4843. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4844. s:=s+tostr(length(s2))+s2;
  4845. case proctypeoption of
  4846. potype_constructor:
  4847. s:=s+'C1';
  4848. potype_destructor:
  4849. s:=s+'D1';
  4850. else
  4851. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4852. end;
  4853. s:=s+'E';
  4854. end
  4855. else
  4856. s:=procsym.realname;
  4857. { now we handle the parameters }
  4858. if maxparacount>0 then
  4859. begin
  4860. for i:=0 to paras.count-1 do
  4861. begin
  4862. hp:=tparavarsym(paras[i]);
  4863. { no hidden parameters form part of a C++ mangled name:
  4864. a) self is not included
  4865. b) there are no "high" or other hidden parameters
  4866. }
  4867. if vo_is_hidden_para in hp.varoptions then
  4868. continue;
  4869. s2:=getcppparaname(hp.vardef);
  4870. if hp.varspez in [vs_var,vs_out] then
  4871. s2:='R'+s2;
  4872. s:=s+s2;
  4873. end;
  4874. end
  4875. else
  4876. s:=s+'v';
  4877. cplusplusmangledname:=s;
  4878. {$endif NAMEMANGLING_GCC2}
  4879. end;
  4880. function tprocdef.objcmangledname : TSymStr;
  4881. var
  4882. manglednamelen: longint;
  4883. iscatmethod : boolean;
  4884. begin
  4885. if not (po_msgstr in procoptions) then
  4886. internalerror(2009030901);
  4887. { we may very well need longer strings to handle these... }
  4888. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  4889. length('+"[ ]"')+length(messageinf.str^);
  4890. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  4891. if (iscatmethod) then
  4892. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  4893. if manglednamelen>255 then
  4894. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  4895. if not(po_classmethod in procoptions) then
  4896. result:='"-['
  4897. else
  4898. result:='"+[';
  4899. { quotes are necessary because the +/- otherwise confuse the assembler
  4900. into expecting a number
  4901. }
  4902. if iscatmethod then
  4903. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  4904. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  4905. if iscatmethod then
  4906. result:=result+')';
  4907. result:=result+' '+messageinf.str^+']"';
  4908. end;
  4909. {$ifdef jvm}
  4910. function tprocdef.jvmmangledbasename(signature: boolean): TSymStr;
  4911. var
  4912. vs: tparavarsym;
  4913. i: longint;
  4914. founderror: tdef;
  4915. tmpresult: TSymStr;
  4916. container: tsymtable;
  4917. begin
  4918. { format:
  4919. * method definition (in Jasmin):
  4920. (private|protected|public) [static] method(parametertypes)returntype
  4921. * method invocation
  4922. package/class/method(parametertypes)returntype
  4923. -> store common part: method(parametertypes)returntype and
  4924. adorn as required when using it.
  4925. }
  4926. if not signature then
  4927. begin
  4928. { method name }
  4929. { special names for constructors and class constructors }
  4930. if proctypeoption=potype_constructor then
  4931. tmpresult:='<init>'
  4932. else if proctypeoption in [potype_class_constructor,potype_unitinit] then
  4933. tmpresult:='<clinit>'
  4934. else if po_has_importname in procoptions then
  4935. begin
  4936. if assigned(import_name) then
  4937. tmpresult:=import_name^
  4938. else
  4939. internalerror(2010122608);
  4940. end
  4941. else
  4942. begin
  4943. tmpresult:=procsym.realname;
  4944. if tmpresult[1]='$' then
  4945. tmpresult:=copy(tmpresult,2,length(tmpresult)-1);
  4946. { nested functions }
  4947. container:=owner;
  4948. while container.symtabletype=localsymtable do
  4949. begin
  4950. tmpresult:='$'+tprocdef(owner.defowner).procsym.realname+'$'+tostr(tprocdef(owner.defowner).procsym.symid)+'$'+tmpresult;
  4951. container:=container.defowner.owner;
  4952. end;
  4953. end;
  4954. end
  4955. else
  4956. tmpresult:='';
  4957. { parameter types }
  4958. tmpresult:=tmpresult+'(';
  4959. { not the case for the main program (not required for defaultmangledname
  4960. because setmangledname() is called for the main program; in case of
  4961. the JVM, this only sets the importname, however) }
  4962. if assigned(paras) then
  4963. begin
  4964. init_paraloc_info(callerside);
  4965. for i:=0 to paras.count-1 do
  4966. begin
  4967. vs:=tparavarsym(paras[i]);
  4968. { function result is not part of the mangled name }
  4969. if vo_is_funcret in vs.varoptions then
  4970. continue;
  4971. { self pointer neither, except for class methods (the JVM only
  4972. supports static class methods natively, so the self pointer
  4973. here is a regular parameter as far as the JVM is concerned }
  4974. if not(po_classmethod in procoptions) and
  4975. (vo_is_self in vs.varoptions) then
  4976. continue;
  4977. { passing by reference is emulated by passing an array of one
  4978. element containing the value; for types that aren't pointers
  4979. in regular Pascal, simply passing the underlying pointer type
  4980. does achieve regular call-by-reference semantics though;
  4981. formaldefs always have to be passed like that because their
  4982. contents can be replaced }
  4983. if paramanager.push_copyout_param(vs.varspez,vs.vardef,proccalloption) then
  4984. tmpresult:=tmpresult+'[';
  4985. { Add the parameter type. }
  4986. if not jvmaddencodedtype(vs.vardef,false,tmpresult,signature,founderror) then
  4987. { an internalerror here is also triggered in case of errors in the source code }
  4988. tmpresult:='<error>';
  4989. end;
  4990. end;
  4991. tmpresult:=tmpresult+')';
  4992. { And the type of the function result (void in case of a procedure and
  4993. constructor). }
  4994. if (proctypeoption in [potype_constructor,potype_class_constructor]) then
  4995. jvmaddencodedtype(voidtype,false,tmpresult,signature,founderror)
  4996. else if not jvmaddencodedtype(returndef,false,tmpresult,signature,founderror) then
  4997. { an internalerror here is also triggered in case of errors in the source code }
  4998. tmpresult:='<error>';
  4999. result:=tmpresult;
  5000. end;
  5001. {$endif jvm}
  5002. procedure tprocdef.setmangledname(const s : TSymStr);
  5003. begin
  5004. { This is not allowed anymore, the forward declaration
  5005. already needs to create the correct mangledname, no changes
  5006. afterwards are allowed (PFV) }
  5007. { Exception: interface definitions in mode macpas, since in that }
  5008. { case no reference to the old name can exist yet (JM) }
  5009. {$ifdef symansistr}
  5010. if _mangledname<>'' then
  5011. if ((m_mac in current_settings.modeswitches) and
  5012. (interfacedef)) then
  5013. _mangledname:=''
  5014. else
  5015. internalerror(200411171);
  5016. {$else symansistr}
  5017. if assigned(_mangledname) then
  5018. if ((m_mac in current_settings.modeswitches) and
  5019. (interfacedef)) then
  5020. stringdispose(_mangledname)
  5021. else
  5022. internalerror(200411171);
  5023. {$endif symansistr}
  5024. {$ifdef jvm}
  5025. { this routine can be called for compilerproces. can't set mangled
  5026. name since it must be calculated, but it uses import_name when set
  5027. -> set that one }
  5028. import_name:=stringdup(s);
  5029. include(procoptions,po_has_importname);
  5030. include(procoptions,po_has_mangledname);
  5031. {$else}
  5032. {$ifdef symansistr}
  5033. _mangledname:=s;
  5034. {$else symansistr}
  5035. _mangledname:=stringdup(s);
  5036. {$endif symansistr}
  5037. {$endif jvm}
  5038. include(procoptions,po_has_mangledname);
  5039. end;
  5040. {***************************************************************************
  5041. TPROCVARDEF
  5042. ***************************************************************************}
  5043. constructor tprocvardef.create(level:byte);
  5044. begin
  5045. inherited create(procvardef,level);
  5046. end;
  5047. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  5048. begin
  5049. inherited ppuload(procvardef,ppufile);
  5050. { load para symtable }
  5051. parast:=tparasymtable.create(self,ppufile.getbyte);
  5052. {$ifdef jvm}
  5053. ppufile.getderef(classdefderef);
  5054. {$endif}
  5055. tparasymtable(parast).ppuload(ppufile);
  5056. end;
  5057. function tprocvardef.getcopy : tstoreddef;
  5058. var
  5059. i : tcallercallee;
  5060. j : longint;
  5061. begin
  5062. result:=tprocvardef.create(parast.symtablelevel);
  5063. tprocvardef(result).returndef:=returndef;
  5064. tprocvardef(result).returndefderef:=returndefderef;
  5065. tprocvardef(result).parast:=parast.getcopy;
  5066. tprocvardef(result).savesize:=savesize;
  5067. { create paralist copy }
  5068. calcparas;
  5069. tprocvardef(result).paras:=tparalist.create(false);
  5070. tprocvardef(result).paras.count:=paras.count;
  5071. for j:=0 to paras.count-1 do
  5072. tprocvardef(result).paras[j]:=paras[j];
  5073. tprocvardef(result).proctypeoption:=proctypeoption;
  5074. tprocvardef(result).proccalloption:=proccalloption;
  5075. tprocvardef(result).procoptions:=procoptions;
  5076. tprocvardef(result).callerargareasize:=callerargareasize;
  5077. tprocvardef(result).calleeargareasize:=calleeargareasize;
  5078. tprocvardef(result).maxparacount:=maxparacount;
  5079. tprocvardef(result).minparacount:=minparacount;
  5080. for i:=low(tcallercallee) to high(tcallercallee) do
  5081. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  5082. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  5083. {$ifdef m68k}
  5084. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  5085. {$endif}
  5086. {$ifdef jvm}
  5087. tprocvardef(result).classdef:=classdef;
  5088. {$endif}
  5089. end;
  5090. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  5091. begin
  5092. inherited ppuwrite(ppufile);
  5093. { Save the para symtable level (necessary to distinguish nested
  5094. procvars) }
  5095. ppufile.putbyte(parast.symtablelevel);
  5096. {$ifdef jvm}
  5097. ppufile.putderef(classdefderef);
  5098. {$endif}
  5099. { Write this entry }
  5100. ppufile.writeentry(ibprocvardef);
  5101. { Save the para symtable, this is taken from the interface }
  5102. tparasymtable(parast).ppuwrite(ppufile);
  5103. end;
  5104. {$ifdef jvm}
  5105. procedure tprocvardef.buildderef;
  5106. begin
  5107. inherited buildderef;
  5108. classdefderef.build(classdef);
  5109. end;
  5110. procedure tprocvardef.deref;
  5111. begin
  5112. inherited deref;
  5113. classdef:=tobjectdef(classdefderef.resolve);
  5114. end;
  5115. {$endif}
  5116. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  5117. begin
  5118. case t of
  5119. gs_para :
  5120. GetSymtable:=parast;
  5121. else
  5122. GetSymtable:=nil;
  5123. end;
  5124. end;
  5125. function tprocvardef.size : asizeint;
  5126. var
  5127. far_code_extra_bytes: integer = 0;
  5128. begin
  5129. {$ifdef i8086}
  5130. if po_far in procoptions then
  5131. far_code_extra_bytes:=2;
  5132. {$endif i8086}
  5133. if ((po_methodpointer in procoptions) or
  5134. is_nested_pd(self)) and
  5135. not(po_addressonly in procoptions) then
  5136. size:=sizeof(pint)+far_code_extra_bytes+voidpointertype.size
  5137. else
  5138. size:=sizeof(pint)+far_code_extra_bytes;
  5139. end;
  5140. function tprocvardef.is_methodpointer:boolean;
  5141. begin
  5142. result:=(po_methodpointer in procoptions);
  5143. end;
  5144. function tprocvardef.is_addressonly:boolean;
  5145. begin
  5146. result:=(not(po_methodpointer in procoptions) and
  5147. not is_nested_pd(self)) or
  5148. (po_addressonly in procoptions);
  5149. end;
  5150. function tprocvardef.getmangledparaname:TSymStr;
  5151. begin
  5152. if not(po_methodpointer in procoptions) then
  5153. if not is_nested_pd(self) then
  5154. result:='procvar'
  5155. else
  5156. result:='nestedprovar'
  5157. else
  5158. result:='procvarofobj'
  5159. end;
  5160. function tprocvardef.is_publishable : boolean;
  5161. begin
  5162. is_publishable:=(po_methodpointer in procoptions);
  5163. end;
  5164. function tprocvardef.GetTypeName : string;
  5165. var
  5166. s: string;
  5167. pno : tprocnameoptions;
  5168. begin
  5169. {$ifdef EXTDEBUG}
  5170. pno:=[pno_showhidden];
  5171. {$else EXTDEBUG}
  5172. pno:=[];
  5173. {$endif EXTDEBUG}
  5174. s:='<';
  5175. if po_classmethod in procoptions then
  5176. s := s+'class method type of'
  5177. else
  5178. if po_addressonly in procoptions then
  5179. s := s+'address of'
  5180. else
  5181. s := s+'procedure variable type of';
  5182. if assigned(returndef) and
  5183. (returndef<>voidtype) then
  5184. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5185. else
  5186. s:=s+' procedure'+typename_paras(pno);
  5187. if po_methodpointer in procoptions then
  5188. s := s+' of object';
  5189. if is_nested_pd(self) then
  5190. s := s+' is nested';
  5191. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5192. end;
  5193. {***************************************************************************
  5194. TOBJECTDEF
  5195. ***************************************************************************}
  5196. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  5197. begin
  5198. inherited create(n,objectdef);
  5199. fcurrent_dispid:=0;
  5200. objecttype:=ot;
  5201. childof:=nil;
  5202. if objecttype=odt_helper then
  5203. owner.includeoption(sto_has_helper);
  5204. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  5205. { create space for vmt !! }
  5206. vmtentries:=TFPList.Create;
  5207. vmt_offset:=0;
  5208. set_parent(c);
  5209. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5210. prepareguid;
  5211. { setup implemented interfaces }
  5212. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5213. ImplementedInterfaces:=TFPObjectList.Create(true)
  5214. else
  5215. ImplementedInterfaces:=nil;
  5216. writing_class_record_dbginfo:=false;
  5217. end;
  5218. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5219. var
  5220. i,
  5221. implintfcount : longint;
  5222. d, getterd : tderef;
  5223. ImplIntf : TImplementedInterface;
  5224. vmtentry : pvmtentry;
  5225. begin
  5226. inherited ppuload(objectdef,ppufile);
  5227. objecttype:=tobjecttyp(ppufile.getbyte);
  5228. objextname:=stringdup(ppufile.getstring);
  5229. { only used for external Objective-C classes/protocols }
  5230. if (objextname^='') then
  5231. stringdispose(objextname);
  5232. symtable:=tObjectSymtable.create(self,objrealname^,0);
  5233. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5234. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5235. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5236. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5237. vmt_offset:=ppufile.getlongint;
  5238. ppufile.getderef(childofderef);
  5239. { load guid }
  5240. iidstr:=nil;
  5241. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5242. begin
  5243. new(iidguid);
  5244. ppufile.getguid(iidguid^);
  5245. iidstr:=stringdup(ppufile.getstring);
  5246. end;
  5247. abstractcnt:=ppufile.getlongint;
  5248. if objecttype=odt_helper then
  5249. ppufile.getderef(extendeddefderef);
  5250. vmtentries:=TFPList.Create;
  5251. vmtentries.count:=ppufile.getlongint;
  5252. for i:=0 to vmtentries.count-1 do
  5253. begin
  5254. ppufile.getderef(d);
  5255. new(vmtentry);
  5256. vmtentry^.procdef:=nil;
  5257. vmtentry^.procdefderef:=d;
  5258. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5259. vmtentries[i]:=vmtentry;
  5260. end;
  5261. { load implemented interfaces }
  5262. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5263. begin
  5264. ImplementedInterfaces:=TFPObjectList.Create(true);
  5265. implintfcount:=ppufile.getlongint;
  5266. for i:=0 to implintfcount-1 do
  5267. begin
  5268. ppufile.getderef(d);
  5269. ppufile.getderef(getterd);
  5270. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5271. ImplIntf.IOffset:=ppufile.getlongint;
  5272. byte(ImplIntf.IType):=ppufile.getbyte;
  5273. ImplementedInterfaces.Add(ImplIntf);
  5274. end;
  5275. end
  5276. else
  5277. ImplementedInterfaces:=nil;
  5278. if df_copied_def in defoptions then
  5279. ppufile.getderef(cloneddefderef)
  5280. else
  5281. tObjectSymtable(symtable).ppuload(ppufile);
  5282. { handles the predefined class tobject }
  5283. { the last TOBJECT which is loaded gets }
  5284. { it ! }
  5285. if (childof=nil) and
  5286. (objecttype in [odt_class,odt_javaclass]) and
  5287. (objname^='TOBJECT') then
  5288. class_tobject:=self;
  5289. if (childof=nil) and
  5290. (objecttype=odt_interfacecom) then
  5291. if (objname^='IUNKNOWN') then
  5292. interface_iunknown:=self
  5293. else
  5294. if (objname^='IDISPATCH') then
  5295. interface_idispatch:=self;
  5296. if (childof=nil) and
  5297. (objecttype=odt_objcclass) and
  5298. (objname^='PROTOCOL') then
  5299. objc_protocoltype:=self;
  5300. if (objecttype=odt_javaclass) and
  5301. not(oo_is_formal in objectoptions) then
  5302. begin
  5303. if (objname^='JLOBJECT') then
  5304. java_jlobject:=self
  5305. else if (objname^='JLTHROWABLE') then
  5306. java_jlthrowable:=self
  5307. else if (objname^='FPCBASERECORDTYPE') then
  5308. java_fpcbaserecordtype:=self
  5309. else if (objname^='JLSTRING') then
  5310. java_jlstring:=self
  5311. else if (objname^='ANSISTRINGCLASS') then
  5312. java_ansistring:=self
  5313. else if (objname^='SHORTSTRINGCLASS') then
  5314. java_shortstring:=self
  5315. else if (objname^='JLENUM') then
  5316. java_jlenum:=self
  5317. else if (objname^='JUENUMSET') then
  5318. java_juenumset:=self
  5319. else if (objname^='FPCBITSET') then
  5320. java_jubitset:=self
  5321. else if (objname^='FPCBASEPROCVARTYPE') then
  5322. java_procvarbase:=self;
  5323. end;
  5324. writing_class_record_dbginfo:=false;
  5325. end;
  5326. destructor tobjectdef.destroy;
  5327. begin
  5328. if assigned(symtable) then
  5329. begin
  5330. symtable.free;
  5331. symtable:=nil;
  5332. end;
  5333. stringdispose(objextname);
  5334. stringdispose(iidstr);
  5335. if assigned(ImplementedInterfaces) then
  5336. begin
  5337. ImplementedInterfaces.free;
  5338. ImplementedInterfaces:=nil;
  5339. end;
  5340. if assigned(iidguid) then
  5341. begin
  5342. dispose(iidguid);
  5343. iidguid:=nil;
  5344. end;
  5345. if assigned(vmtentries) then
  5346. begin
  5347. resetvmtentries;
  5348. vmtentries.free;
  5349. vmtentries:=nil;
  5350. end;
  5351. if assigned(vmcallstaticinfo) then
  5352. begin
  5353. freemem(vmcallstaticinfo);
  5354. vmcallstaticinfo:=nil;
  5355. end;
  5356. inherited destroy;
  5357. end;
  5358. function tobjectdef.getcopy : tstoreddef;
  5359. var
  5360. i : longint;
  5361. begin
  5362. result:=tobjectdef.create(objecttype,objrealname^,childof);
  5363. { the constructor allocates a symtable which we release to avoid memory leaks }
  5364. tobjectdef(result).symtable.free;
  5365. tobjectdef(result).symtable:=symtable.getcopy;
  5366. if assigned(objextname) then
  5367. tobjectdef(result).objextname:=stringdup(objextname^);
  5368. if assigned(import_lib) then
  5369. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5370. tobjectdef(result).objectoptions:=objectoptions;
  5371. include(tobjectdef(result).defoptions,df_copied_def);
  5372. tobjectdef(result).extendeddef:=extendeddef;
  5373. if assigned(tcinitcode) then
  5374. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5375. tobjectdef(result).vmt_offset:=vmt_offset;
  5376. if assigned(iidguid) then
  5377. begin
  5378. new(tobjectdef(result).iidguid);
  5379. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5380. end;
  5381. if assigned(iidstr) then
  5382. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5383. tobjectdef(result).abstractcnt:=abstractcnt;
  5384. if assigned(ImplementedInterfaces) then
  5385. begin
  5386. for i:=0 to ImplementedInterfaces.count-1 do
  5387. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5388. end;
  5389. if assigned(vmtentries) then
  5390. begin
  5391. tobjectdef(result).vmtentries:=TFPList.Create;
  5392. tobjectdef(result).copyvmtentries(self);
  5393. end;
  5394. end;
  5395. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5396. var
  5397. i : longint;
  5398. vmtentry : pvmtentry;
  5399. ImplIntf : TImplementedInterface;
  5400. old_do_indirect_crc: boolean;
  5401. begin
  5402. { if class1 in unit A changes, and class2 in unit B inherits from it
  5403. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5404. However, if there is also a class3 in unit C that only depends on
  5405. unit B, then unit C will not be recompiled because nothing changed
  5406. to the interface of unit B. Nevertheless, unit C can indirectly
  5407. depend on unit A via derefs, and these must be updated -> the
  5408. indirect crc keeps track of such changes. }
  5409. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5410. ppufile.do_indirect_crc:=true;
  5411. inherited ppuwrite(ppufile);
  5412. ppufile.putbyte(byte(objecttype));
  5413. if assigned(objextname) then
  5414. ppufile.putstring(objextname^)
  5415. else
  5416. ppufile.putstring('');
  5417. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5418. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5419. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5420. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5421. ppufile.putlongint(vmt_offset);
  5422. ppufile.putderef(childofderef);
  5423. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5424. begin
  5425. ppufile.putguid(iidguid^);
  5426. ppufile.putstring(iidstr^);
  5427. end;
  5428. ppufile.putlongint(abstractcnt);
  5429. if objecttype=odt_helper then
  5430. ppufile.putderef(extendeddefderef);
  5431. ppufile.putlongint(vmtentries.count);
  5432. for i:=0 to vmtentries.count-1 do
  5433. begin
  5434. vmtentry:=pvmtentry(vmtentries[i]);
  5435. ppufile.putderef(vmtentry^.procdefderef);
  5436. ppufile.putbyte(byte(vmtentry^.visibility));
  5437. end;
  5438. if assigned(ImplementedInterfaces) then
  5439. begin
  5440. ppufile.putlongint(ImplementedInterfaces.Count);
  5441. for i:=0 to ImplementedInterfaces.Count-1 do
  5442. begin
  5443. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5444. ppufile.putderef(ImplIntf.intfdefderef);
  5445. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5446. ppufile.putlongint(ImplIntf.Ioffset);
  5447. ppufile.putbyte(byte(ImplIntf.IType));
  5448. end;
  5449. end;
  5450. if df_copied_def in defoptions then
  5451. ppufile.putderef(cloneddefderef);
  5452. ppufile.writeentry(ibobjectdef);
  5453. if not(df_copied_def in defoptions) then
  5454. tObjectSymtable(symtable).ppuwrite(ppufile);
  5455. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5456. end;
  5457. function tobjectdef.GetTypeName:string;
  5458. begin
  5459. { in this case we will go in endless recursion, because then }
  5460. { there is no tsym associated yet with the def. It can occur }
  5461. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5462. { instead of the actual type name }
  5463. if not assigned(typesym) then
  5464. result:='<Currently Parsed Class>'
  5465. else
  5466. result:=typesymbolprettyname;
  5467. end;
  5468. procedure tobjectdef.buildderef;
  5469. var
  5470. i : longint;
  5471. vmtentry : pvmtentry;
  5472. begin
  5473. inherited buildderef;
  5474. childofderef.build(childof);
  5475. if df_copied_def in defoptions then
  5476. cloneddefderef.build(symtable.defowner)
  5477. else
  5478. tstoredsymtable(symtable).buildderef;
  5479. if objecttype=odt_helper then
  5480. extendeddefderef.build(extendeddef);
  5481. for i:=0 to vmtentries.count-1 do
  5482. begin
  5483. vmtentry:=pvmtentry(vmtentries[i]);
  5484. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5485. end;
  5486. if assigned(ImplementedInterfaces) then
  5487. begin
  5488. for i:=0 to ImplementedInterfaces.count-1 do
  5489. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5490. end;
  5491. end;
  5492. procedure tobjectdef.deref;
  5493. var
  5494. i : longint;
  5495. vmtentry : pvmtentry;
  5496. begin
  5497. inherited deref;
  5498. childof:=tobjectdef(childofderef.resolve);
  5499. if df_copied_def in defoptions then
  5500. begin
  5501. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5502. symtable:=cloneddef.symtable.getcopy;
  5503. end
  5504. else
  5505. tstoredsymtable(symtable).deref;
  5506. fillgenericparas(symtable);
  5507. if objecttype=odt_helper then
  5508. extendeddef:=tdef(extendeddefderef.resolve);
  5509. for i:=0 to vmtentries.count-1 do
  5510. begin
  5511. vmtentry:=pvmtentry(vmtentries[i]);
  5512. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5513. end;
  5514. if assigned(ImplementedInterfaces) then
  5515. begin
  5516. for i:=0 to ImplementedInterfaces.count-1 do
  5517. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5518. end;
  5519. end;
  5520. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5521. var
  5522. pd: tprocdef absolute def;
  5523. st: tsymtable;
  5524. psym: tsym;
  5525. nname: TIDString;
  5526. begin
  5527. if (tdef(def).typ<>procdef) then
  5528. exit;
  5529. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5530. owner = symtable in which objcclassdef is defined
  5531. }
  5532. st:=pd.owner.defowner.owner;
  5533. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5534. { check for an existing procsym with our special name }
  5535. psym:=tsym(st.find(nname));
  5536. if not assigned(psym) then
  5537. begin
  5538. psym:=tprocsym.create(nname);
  5539. { avoid warning about this symbol being unused }
  5540. psym.IncRefCount;
  5541. { don't check for duplicates:
  5542. a) we checked above
  5543. b) in case we are in the implementation section of a unit, this
  5544. will also check for this symbol in the interface section
  5545. (since you normally cannot have symbols with the same name
  5546. both interface and implementation), and it's possible to
  5547. have class helpers for the same class in the interface and
  5548. in the implementation, and they cannot be merged since only
  5549. the once in the interface must be saved to the ppu/visible
  5550. from other units }
  5551. st.insert(psym,false);
  5552. end
  5553. else if (psym.typ<>procsym) then
  5554. internalerror(2009111501);
  5555. { add ourselves to this special procsym }
  5556. tprocsym(psym).procdeflist.add(def);
  5557. end;
  5558. procedure tobjectdef.buildderefimpl;
  5559. begin
  5560. inherited buildderefimpl;
  5561. if not (df_copied_def in defoptions) then
  5562. tstoredsymtable(symtable).buildderefimpl;
  5563. end;
  5564. procedure tobjectdef.derefimpl;
  5565. begin
  5566. inherited derefimpl;
  5567. if not (df_copied_def in defoptions) then
  5568. tstoredsymtable(symtable).derefimpl;
  5569. { the procdefs are not owned by the class helper procsyms, so they
  5570. are not stored/restored either -> re-add them here }
  5571. if (objecttype=odt_objcclass) or
  5572. (oo_is_classhelper in objectoptions) then
  5573. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  5574. end;
  5575. procedure tobjectdef.resetvmtentries;
  5576. var
  5577. i : longint;
  5578. begin
  5579. for i:=0 to vmtentries.Count-1 do
  5580. Dispose(pvmtentry(vmtentries[i]));
  5581. vmtentries.clear;
  5582. end;
  5583. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  5584. var
  5585. i : longint;
  5586. vmtentry : pvmtentry;
  5587. begin
  5588. resetvmtentries;
  5589. vmtentries.count:=objdef.vmtentries.count;
  5590. for i:=0 to objdef.vmtentries.count-1 do
  5591. begin
  5592. new(vmtentry);
  5593. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  5594. vmtentries[i]:=vmtentry;
  5595. end;
  5596. end;
  5597. function tobjectdef.getparentdef:tdef;
  5598. begin
  5599. { TODO: Remove getparentdef hack}
  5600. { With 2 forward declared classes with the child class before the
  5601. parent class the child class is written earlier to the ppu. Leaving it
  5602. possible to have a reference to the parent class for property overriding,
  5603. but the parent class still has the childof not resolved yet (PFV) }
  5604. if childof=nil then
  5605. childof:=tobjectdef(childofderef.resolve);
  5606. result:=childof;
  5607. end;
  5608. procedure tobjectdef.prepareguid;
  5609. begin
  5610. { set up guid }
  5611. if not assigned(iidguid) then
  5612. begin
  5613. new(iidguid);
  5614. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5615. end;
  5616. { setup iidstring }
  5617. if not assigned(iidstr) then
  5618. iidstr:=stringdup(''); { default is empty string }
  5619. end;
  5620. procedure tobjectdef.set_parent( c : tobjectdef);
  5621. begin
  5622. if assigned(childof) then
  5623. exit;
  5624. childof:=c;
  5625. if not assigned(c) then
  5626. exit;
  5627. { inherit options and status }
  5628. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5629. { initially has the same number of abstract methods as the parent }
  5630. abstractcnt:=c.abstractcnt;
  5631. { add the data of the anchestor class/object }
  5632. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5633. begin
  5634. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5635. { inherit recordalignment }
  5636. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5637. { if both the parent and this record use C-alignment, also inherit
  5638. the current field alignment }
  5639. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5640. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5641. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5642. { the padding is not inherited for Objective-C classes (maybe not
  5643. for cppclass either?) }
  5644. if objecttype=odt_objcclass then
  5645. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5646. if (oo_has_vmt in objectoptions) and
  5647. (oo_has_vmt in c.objectoptions) then
  5648. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5649. { if parent has a vmt field then the offset is the same for the child PM }
  5650. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5651. begin
  5652. vmt_offset:=c.vmt_offset;
  5653. include(objectoptions,oo_has_vmt);
  5654. end;
  5655. end;
  5656. end;
  5657. procedure tobjectdef.insertvmt;
  5658. var
  5659. vs: tfieldvarsym;
  5660. begin
  5661. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5662. exit;
  5663. if (oo_has_vmt in objectoptions) then
  5664. internalerror(12345)
  5665. else
  5666. begin
  5667. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5668. tObjectSymtable(symtable).fieldalignment);
  5669. if (tf_requires_proper_alignment in target_info.flags) then
  5670. begin
  5671. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5672. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5673. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5674. end;
  5675. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5676. hidesym(vs);
  5677. tObjectSymtable(symtable).insert(vs);
  5678. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5679. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5680. vmt_offset:=vs.fieldoffset
  5681. else
  5682. vmt_offset:=vs.fieldoffset div 8;
  5683. include(objectoptions,oo_has_vmt);
  5684. end;
  5685. end;
  5686. procedure tobjectdef.check_forwards;
  5687. begin
  5688. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5689. inherited;
  5690. if (oo_is_forward in objectoptions) then
  5691. begin
  5692. { ok, in future, the forward can be resolved }
  5693. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5694. exclude(objectoptions,oo_is_forward);
  5695. end;
  5696. end;
  5697. function tobjectdef.find_destructor: tprocdef;
  5698. var
  5699. objdef: tobjectdef;
  5700. begin
  5701. objdef:=self;
  5702. while assigned(objdef) do
  5703. begin
  5704. result:=objdef.find_procdef_bytype(potype_destructor);
  5705. if assigned(result) then
  5706. exit;
  5707. objdef:=objdef.childof;
  5708. end;
  5709. result:=nil;
  5710. end;
  5711. function tobjectdef.implements_any_interfaces: boolean;
  5712. begin
  5713. result := (ImplementedInterfaces.Count > 0) or
  5714. (assigned(childof) and childof.implements_any_interfaces);
  5715. end;
  5716. function tobjectdef.size : asizeint;
  5717. begin
  5718. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5719. result:=voidpointertype.size
  5720. else
  5721. result:=tObjectSymtable(symtable).datasize;
  5722. end;
  5723. function tobjectdef.alignment:shortint;
  5724. begin
  5725. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5726. alignment:=voidpointertype.size
  5727. else
  5728. alignment:=tObjectSymtable(symtable).recordalignment;
  5729. end;
  5730. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5731. var
  5732. codeptrsize: Integer;
  5733. begin
  5734. {$ifdef i8086}
  5735. if current_settings.x86memorymodel in x86_far_code_models then
  5736. codeptrsize:=4
  5737. else
  5738. codeptrsize:=2;
  5739. {$else i8086}
  5740. codeptrsize:=sizeof(pint);
  5741. {$endif i8086}
  5742. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5743. case objecttype of
  5744. odt_class:
  5745. { the +2*sizeof(pint) is size and -size }
  5746. vmtmethodoffset:=index*codeptrsize+10*sizeof(pint)+2*sizeof(pint);
  5747. odt_helper,
  5748. odt_objcclass,
  5749. odt_objcprotocol:
  5750. vmtmethodoffset:=0;
  5751. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5752. vmtmethodoffset:=index*codeptrsize;
  5753. odt_javaclass,
  5754. odt_interfacejava:
  5755. { invalid }
  5756. vmtmethodoffset:=-1;
  5757. else
  5758. {$ifdef WITHDMT}
  5759. vmtmethodoffset:=index*codeptrsize+4*sizeof(pint);
  5760. {$else WITHDMT}
  5761. vmtmethodoffset:=index*codeptrsize+3*sizeof(pint);
  5762. {$endif WITHDMT}
  5763. end;
  5764. end;
  5765. function tobjectdef.vmt_mangledname : TSymStr;
  5766. begin
  5767. if not(oo_has_vmt in objectoptions) then
  5768. Message1(parser_n_object_has_no_vmt,objrealname^);
  5769. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5770. end;
  5771. function tobjectdef.needs_inittable : boolean;
  5772. var
  5773. hp : tobjectdef;
  5774. begin
  5775. case objecttype of
  5776. odt_helper,
  5777. odt_class :
  5778. needs_inittable:=false;
  5779. odt_dispinterface,
  5780. odt_interfacecom:
  5781. needs_inittable:=true;
  5782. odt_interfacecorba:
  5783. begin
  5784. hp:=childof;
  5785. while assigned(hp) do
  5786. begin
  5787. if hp=interface_iunknown then
  5788. begin
  5789. needs_inittable:=true;
  5790. exit;
  5791. end;
  5792. hp:=hp.childof;
  5793. end;
  5794. needs_inittable:=false;
  5795. end;
  5796. odt_object:
  5797. needs_inittable:=
  5798. tObjectSymtable(symtable).needs_init_final or
  5799. (assigned(childof) and
  5800. childof.needs_inittable);
  5801. odt_cppclass,
  5802. odt_objcclass,
  5803. odt_objcprotocol,
  5804. odt_javaclass,
  5805. odt_interfacejava:
  5806. needs_inittable:=false;
  5807. else
  5808. internalerror(200108267);
  5809. end;
  5810. end;
  5811. function tobjectdef.needs_separate_initrtti : boolean;
  5812. begin
  5813. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5814. end;
  5815. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5816. begin
  5817. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5818. result:=inherited rtti_mangledname(rt)
  5819. else
  5820. begin
  5821. { necessary in case of a dynamic array of nsobject, or
  5822. if an nsobject field appears in a record that needs
  5823. init/finalisation }
  5824. if rt=initrtti then
  5825. begin
  5826. result:=voidpointertype.rtti_mangledname(rt);
  5827. exit;
  5828. end;
  5829. if not(target_info.system in systems_objc_nfabi) then
  5830. begin
  5831. result:=target_asm.labelprefix;
  5832. case objecttype of
  5833. odt_objcclass:
  5834. begin
  5835. case rt of
  5836. objcclassrtti:
  5837. if not(oo_is_classhelper in objectoptions) then
  5838. result:=result+'_OBJC_CLASS_'
  5839. else
  5840. result:=result+'_OBJC_CATEGORY_';
  5841. objcmetartti:
  5842. if not(oo_is_classhelper in objectoptions) then
  5843. result:=result+'_OBJC_METACLASS_'
  5844. else
  5845. internalerror(2009111511);
  5846. else
  5847. internalerror(2009092302);
  5848. end;
  5849. end;
  5850. odt_objcprotocol:
  5851. result:=result+'_OBJC_PROTOCOL_';
  5852. end;
  5853. end
  5854. else
  5855. begin
  5856. case objecttype of
  5857. odt_objcclass:
  5858. begin
  5859. if (oo_is_classhelper in objectoptions) and
  5860. (rt<>objcclassrtti) then
  5861. internalerror(2009111512);
  5862. case rt of
  5863. objcclassrtti:
  5864. if not(oo_is_classhelper in objectoptions) then
  5865. result:='_OBJC_CLASS_$_'
  5866. else
  5867. result:='_OBJC_$_CATEGORY_';
  5868. objcmetartti:
  5869. result:='_OBJC_METACLASS_$_';
  5870. objcclassrortti:
  5871. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5872. objcmetarortti:
  5873. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5874. else
  5875. internalerror(2009092303);
  5876. end;
  5877. end;
  5878. odt_objcprotocol:
  5879. begin
  5880. result:=lower(target_asm.labelprefix);
  5881. case rt of
  5882. objcclassrtti:
  5883. result:=result+'_OBJC_PROTOCOL_$_';
  5884. objcmetartti:
  5885. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5886. else
  5887. internalerror(2009092501);
  5888. end;
  5889. end;
  5890. else
  5891. internalerror(2013113005);
  5892. end;
  5893. end;
  5894. result:=result+objextname^;
  5895. end;
  5896. end;
  5897. function tobjectdef.members_need_inittable : boolean;
  5898. begin
  5899. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5900. end;
  5901. function tobjectdef.is_publishable : boolean;
  5902. begin
  5903. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5904. end;
  5905. function tobjectdef.get_next_dispid: longint;
  5906. begin
  5907. inc(fcurrent_dispid);
  5908. result:=fcurrent_dispid;
  5909. end;
  5910. function tobjectdef.search_enumerator_get: tprocdef;
  5911. begin
  5912. result:=inherited;
  5913. if not assigned(result) and assigned(childof) then
  5914. result:=childof.search_enumerator_get;
  5915. end;
  5916. function tobjectdef.search_enumerator_move: tprocdef;
  5917. begin
  5918. result:=inherited;
  5919. if not assigned(result) and assigned(childof) then
  5920. result:=childof.search_enumerator_move;
  5921. end;
  5922. function tobjectdef.search_enumerator_current: tsym;
  5923. begin
  5924. result:=inherited;
  5925. if not assigned(result) and assigned(childof) then
  5926. result:=childof.search_enumerator_current;
  5927. end;
  5928. procedure tobjectdef.register_created_classref_type;
  5929. begin
  5930. if not classref_created_in_current_module then
  5931. begin
  5932. classref_created_in_current_module:=true;
  5933. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5934. end;
  5935. end;
  5936. procedure tobjectdef.register_created_object_type;
  5937. begin
  5938. if not created_in_current_module then
  5939. begin
  5940. created_in_current_module:=true;
  5941. current_module.wpoinfo.addcreatedobjtype(self);
  5942. end;
  5943. end;
  5944. procedure tobjectdef.register_maybe_created_object_type;
  5945. begin
  5946. { if we know it has been created for sure, no need
  5947. to also record that it maybe can be created in
  5948. this module
  5949. }
  5950. if not (created_in_current_module) and
  5951. not (maybe_created_in_current_module) then
  5952. begin
  5953. maybe_created_in_current_module:=true;
  5954. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5955. end;
  5956. end;
  5957. procedure tobjectdef.register_vmt_call(index: longint);
  5958. begin
  5959. if (is_object(self) or is_class(self)) then
  5960. current_module.wpoinfo.addcalledvmtentry(self,index);
  5961. end;
  5962. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5963. var
  5964. def: tdef absolute data;
  5965. pd: tprocdef absolute data;
  5966. i,
  5967. paracount: longint;
  5968. begin
  5969. if (def.typ=procdef) then
  5970. begin
  5971. { add all messages also under a dummy name to the symtable in
  5972. which the objcclass/protocol/category is declared, so they can
  5973. be called via id.<name>
  5974. }
  5975. create_class_helper_for_procdef(pd,nil);
  5976. { we have to wait until now to set the mangled name because it
  5977. depends on the (possibly external) class name, which is defined
  5978. at the very end. }
  5979. if not(po_msgstr in pd.procoptions) then
  5980. begin
  5981. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5982. { recover to avoid internalerror later on }
  5983. include(pd.procoptions,po_msgstr);
  5984. pd.messageinf.str:=stringdup('MissingDeclaration');
  5985. end;
  5986. { Mangled name is already set in case this is a copy of
  5987. another type. }
  5988. if not(po_has_mangledname in pd.procoptions) then
  5989. begin
  5990. { check whether the number of formal parameters is correct,
  5991. and whether they have valid Objective-C types }
  5992. paracount:=0;
  5993. for i:=1 to length(pd.messageinf.str^) do
  5994. if pd.messageinf.str^[i]=':' then
  5995. inc(paracount);
  5996. for i:=0 to pd.paras.count-1 do
  5997. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5998. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5999. dec(paracount);
  6000. if (paracount<>0) then
  6001. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  6002. pd.setmangledname(pd.objcmangledname);
  6003. end
  6004. else
  6005. { all checks already done }
  6006. exit;
  6007. if not(oo_is_external in pd.struct.objectoptions) then
  6008. begin
  6009. if (po_varargs in pd.procoptions) then
  6010. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  6011. else
  6012. begin
  6013. { check for "array of const" parameters }
  6014. for i:=0 to pd.parast.symlist.count-1 do
  6015. begin
  6016. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  6017. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  6018. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  6019. end;
  6020. end;
  6021. end;
  6022. end;
  6023. end;
  6024. procedure mark_private_fields_used(data: tobject; arg: pointer);
  6025. var
  6026. sym: tsym absolute data;
  6027. begin
  6028. if (sym.typ=fieldvarsym) and
  6029. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  6030. sym.IncRefCount;
  6031. end;
  6032. procedure tobjectdef.finish_objc_data;
  6033. begin
  6034. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  6035. if (oo_is_external in objectoptions) then
  6036. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  6037. end;
  6038. procedure verify_objc_vardef(data: tobject; arg: pointer);
  6039. var
  6040. sym: tabstractvarsym absolute data;
  6041. res: pboolean absolute arg;
  6042. founderrordef: tdef;
  6043. begin
  6044. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  6045. exit;
  6046. if (sym.typ=paravarsym) and
  6047. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  6048. is_array_of_const(tparavarsym(sym).vardef)) then
  6049. exit;
  6050. if not objcchecktype(sym.vardef,founderrordef) then
  6051. begin
  6052. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6053. res^:=false;
  6054. end;
  6055. end;
  6056. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  6057. var
  6058. def: tdef absolute data;
  6059. res: pboolean absolute arg;
  6060. founderrordef: tdef;
  6061. begin
  6062. if (def.typ<>procdef) then
  6063. exit;
  6064. { check parameter types for validity }
  6065. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  6066. { check the result type for validity }
  6067. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  6068. begin
  6069. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  6070. res^:=false;
  6071. end;
  6072. end;
  6073. function tobjectdef.check_objc_types: boolean;
  6074. begin
  6075. { done in separate step from finish_objc_data, because when
  6076. finish_objc_data is called, not all forwarddefs have been resolved
  6077. yet and we need to know all types here }
  6078. result:=true;
  6079. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  6080. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  6081. end;
  6082. procedure do_cpp_import_info(data: tobject; arg: pointer);
  6083. var
  6084. def: tdef absolute data;
  6085. pd: tprocdef absolute data;
  6086. begin
  6087. if (def.typ=procdef) then
  6088. begin
  6089. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  6090. if (oo_is_external in pd.struct.objectoptions) then
  6091. begin
  6092. { copied from psub.read_proc }
  6093. if assigned(tobjectdef(pd.struct).import_lib) then
  6094. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  6095. else
  6096. begin
  6097. { add import name to external list for DLL scanning }
  6098. if tf_has_dllscanner in target_info.flags then
  6099. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  6100. end;
  6101. end;
  6102. end;
  6103. end;
  6104. procedure tobjectdef.finish_cpp_data;
  6105. begin
  6106. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  6107. end;
  6108. {****************************************************************************
  6109. TImplementedInterface
  6110. ****************************************************************************}
  6111. function TImplementedInterface.GetIOffset: longint;
  6112. begin
  6113. if (fIOffset=-1) and
  6114. (IType in [etFieldValue,etFieldValueClass]) then
  6115. result:=tfieldvarsym(ImplementsField).fieldoffset
  6116. else
  6117. result:=fIOffset;
  6118. end;
  6119. constructor TImplementedInterface.create(aintf: tobjectdef);
  6120. begin
  6121. inherited create;
  6122. intfdef:=aintf;
  6123. IOffset:=-1;
  6124. IType:=etStandard;
  6125. NameMappings:=nil;
  6126. procdefs:=nil;
  6127. end;
  6128. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6129. begin
  6130. inherited create;
  6131. intfdef:=nil;
  6132. intfdefderef:=intfd;
  6133. ImplementsGetterDeref:=getterd;
  6134. IOffset:=-1;
  6135. IType:=etStandard;
  6136. NameMappings:=nil;
  6137. procdefs:=nil;
  6138. end;
  6139. destructor TImplementedInterface.destroy;
  6140. var
  6141. i : longint;
  6142. mappedname : pshortstring;
  6143. begin
  6144. if assigned(NameMappings) then
  6145. begin
  6146. for i:=0 to NameMappings.Count-1 do
  6147. begin
  6148. mappedname:=pshortstring(NameMappings[i]);
  6149. stringdispose(mappedname);
  6150. end;
  6151. NameMappings.free;
  6152. NameMappings:=nil;
  6153. end;
  6154. if assigned(procdefs) then
  6155. begin
  6156. procdefs.free;
  6157. procdefs:=nil;
  6158. end;
  6159. inherited destroy;
  6160. end;
  6161. procedure TImplementedInterface.buildderef;
  6162. begin
  6163. intfdefderef.build(intfdef);
  6164. ImplementsGetterDeref.build(ImplementsGetter);
  6165. end;
  6166. procedure TImplementedInterface.deref;
  6167. begin
  6168. intfdef:=tobjectdef(intfdefderef.resolve);
  6169. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6170. end;
  6171. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6172. begin
  6173. if not assigned(NameMappings) then
  6174. NameMappings:=TFPHashList.Create;
  6175. NameMappings.Add(origname,stringdup(newname));
  6176. end;
  6177. function TImplementedInterface.GetMapping(const origname: string):string;
  6178. var
  6179. mappedname : pshortstring;
  6180. begin
  6181. result:='';
  6182. if not assigned(NameMappings) then
  6183. exit;
  6184. mappedname:=PShortstring(NameMappings.Find(origname));
  6185. if assigned(mappedname) then
  6186. result:=mappedname^;
  6187. end;
  6188. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6189. begin
  6190. if not assigned(procdefs) then
  6191. procdefs:=TFPObjectList.Create(false);
  6192. { duplicate entries must be stored, because multiple }
  6193. { interfaces can declare methods with the same name }
  6194. { and all of these get their own VMT entry }
  6195. procdefs.Add(pd);
  6196. end;
  6197. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6198. var
  6199. i : longint;
  6200. begin
  6201. result:=false;
  6202. { interfaces being implemented through delegation are not mergable (FK) }
  6203. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6204. exit;
  6205. weight:=0;
  6206. { empty interface is mergeable }
  6207. if ProcDefs.Count=0 then
  6208. begin
  6209. result:=true;
  6210. exit;
  6211. end;
  6212. { The interface to merge must at least the number of
  6213. procedures of this interface }
  6214. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6215. exit;
  6216. for i:=0 to ProcDefs.Count-1 do
  6217. begin
  6218. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6219. exit;
  6220. end;
  6221. weight:=ProcDefs.Count;
  6222. result:=true;
  6223. end;
  6224. function TImplementedInterface.getcopy:TImplementedInterface;
  6225. begin
  6226. Result:=TImplementedInterface.Create(nil);
  6227. { 1) the procdefs list will be freed once for each copy
  6228. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6229. 3) idem for the name mappings
  6230. }
  6231. { warning: this is completely wrong on so many levels...
  6232. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6233. We need to make clean copies of the different fields
  6234. this is not implemented yet, and thus we generate an internal
  6235. error instead PM 2011-06-14 }
  6236. internalerror(2011061401);
  6237. end;
  6238. {****************************************************************************
  6239. TFORWARDDEF
  6240. ****************************************************************************}
  6241. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6242. begin
  6243. inherited create(forwarddef);
  6244. tosymname:=stringdup(s);
  6245. forwardpos:=pos;
  6246. end;
  6247. function tforwarddef.GetTypeName:string;
  6248. begin
  6249. GetTypeName:='unresolved forward to '+tosymname^;
  6250. end;
  6251. destructor tforwarddef.destroy;
  6252. begin
  6253. stringdispose(tosymname);
  6254. inherited destroy;
  6255. end;
  6256. function tforwarddef.getcopy:tstoreddef;
  6257. begin
  6258. result:=tforwarddef.create(tosymname^, forwardpos);
  6259. end;
  6260. {****************************************************************************
  6261. TUNDEFINEDDEF
  6262. ****************************************************************************}
  6263. constructor tundefineddef.create;
  6264. begin
  6265. inherited create(undefineddef);
  6266. end;
  6267. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6268. begin
  6269. inherited ppuload(undefineddef,ppufile);
  6270. end;
  6271. function tundefineddef.GetTypeName:string;
  6272. begin
  6273. GetTypeName:='<undefined type>';
  6274. end;
  6275. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6276. begin
  6277. inherited ppuwrite(ppufile);
  6278. ppufile.writeentry(ibundefineddef);
  6279. end;
  6280. {****************************************************************************
  6281. TERRORDEF
  6282. ****************************************************************************}
  6283. constructor terrordef.create;
  6284. begin
  6285. inherited create(errordef);
  6286. { prevent consecutive faults }
  6287. savesize:=1;
  6288. end;
  6289. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6290. begin
  6291. { Can't write errordefs to ppu }
  6292. internalerror(200411063);
  6293. end;
  6294. function terrordef.GetTypeName:string;
  6295. begin
  6296. GetTypeName:='<erroneous type>';
  6297. end;
  6298. function terrordef.getmangledparaname:TSymStr;
  6299. begin
  6300. getmangledparaname:='error';
  6301. end;
  6302. {****************************************************************************
  6303. Definition Helpers
  6304. ****************************************************************************}
  6305. function is_interfacecom(def: tdef): boolean;
  6306. begin
  6307. is_interfacecom:=
  6308. assigned(def) and
  6309. (def.typ=objectdef) and
  6310. (tobjectdef(def).objecttype=odt_interfacecom);
  6311. end;
  6312. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6313. begin
  6314. is_interfacecom_or_dispinterface:=
  6315. assigned(def) and
  6316. (def.typ=objectdef) and
  6317. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6318. end;
  6319. function is_any_interface_kind(def: tdef): boolean;
  6320. begin
  6321. result:=
  6322. assigned(def) and
  6323. (def.typ=objectdef) and
  6324. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6325. is_objccategory(def));
  6326. end;
  6327. function is_interfacecorba(def: tdef): boolean;
  6328. begin
  6329. is_interfacecorba:=
  6330. assigned(def) and
  6331. (def.typ=objectdef) and
  6332. (tobjectdef(def).objecttype=odt_interfacecorba);
  6333. end;
  6334. function is_interface(def: tdef): boolean;
  6335. begin
  6336. is_interface:=
  6337. assigned(def) and
  6338. (def.typ=objectdef) and
  6339. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6340. end;
  6341. function is_dispinterface(def: tdef): boolean;
  6342. begin
  6343. result:=
  6344. assigned(def) and
  6345. (def.typ=objectdef) and
  6346. (tobjectdef(def).objecttype=odt_dispinterface);
  6347. end;
  6348. function is_class(def: tdef): boolean;
  6349. begin
  6350. is_class:=
  6351. assigned(def) and
  6352. (def.typ=objectdef) and
  6353. (tobjectdef(def).objecttype=odt_class);
  6354. end;
  6355. function is_object(def: tdef): boolean;
  6356. begin
  6357. is_object:=
  6358. assigned(def) and
  6359. (def.typ=objectdef) and
  6360. (tobjectdef(def).objecttype=odt_object);
  6361. end;
  6362. function is_cppclass(def: tdef): boolean;
  6363. begin
  6364. is_cppclass:=
  6365. assigned(def) and
  6366. (def.typ=objectdef) and
  6367. (tobjectdef(def).objecttype=odt_cppclass);
  6368. end;
  6369. function is_objcclass(def: tdef): boolean;
  6370. begin
  6371. is_objcclass:=
  6372. assigned(def) and
  6373. (def.typ=objectdef) and
  6374. (tobjectdef(def).objecttype=odt_objcclass);
  6375. end;
  6376. function is_objectpascal_helper(def: tdef): boolean;
  6377. begin
  6378. result:=
  6379. assigned(def) and
  6380. (def.typ=objectdef) and
  6381. (tobjectdef(def).objecttype=odt_helper);
  6382. end;
  6383. function is_objcclassref(def: tdef): boolean;
  6384. begin
  6385. is_objcclassref:=
  6386. assigned(def) and
  6387. (def.typ=classrefdef) and
  6388. is_objcclass(tclassrefdef(def).pointeddef);
  6389. end;
  6390. function is_objcprotocol(def: tdef): boolean;
  6391. begin
  6392. result:=
  6393. assigned(def) and
  6394. (def.typ=objectdef) and
  6395. (tobjectdef(def).objecttype=odt_objcprotocol);
  6396. end;
  6397. function is_objccategory(def: tdef): boolean;
  6398. begin
  6399. result:=
  6400. assigned(def) and
  6401. (def.typ=objectdef) and
  6402. { if used as a forward type }
  6403. ((tobjectdef(def).objecttype=odt_objccategory) or
  6404. { if used as after it has been resolved }
  6405. ((tobjectdef(def).objecttype=odt_objcclass) and
  6406. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6407. end;
  6408. function is_objc_class_or_protocol(def: tdef): boolean;
  6409. begin
  6410. result:=
  6411. assigned(def) and
  6412. (def.typ=objectdef) and
  6413. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6414. end;
  6415. function is_objc_protocol_or_category(def: tdef): boolean;
  6416. begin
  6417. result:=
  6418. assigned(def) and
  6419. (def.typ=objectdef) and
  6420. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6421. ((tobjectdef(def).objecttype = odt_objcclass) and
  6422. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6423. end;
  6424. function is_classhelper(def: tdef): boolean;
  6425. begin
  6426. result:=
  6427. is_objectpascal_helper(def) or
  6428. is_objccategory(def);
  6429. end;
  6430. function is_class_or_interface(def: tdef): boolean;
  6431. begin
  6432. result:=
  6433. assigned(def) and
  6434. (def.typ=objectdef) and
  6435. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6436. end;
  6437. function is_class_or_interface_or_objc(def: tdef): boolean;
  6438. begin
  6439. result:=
  6440. assigned(def) and
  6441. (def.typ=objectdef) and
  6442. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6443. end;
  6444. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6445. begin
  6446. result:=
  6447. assigned(def) and
  6448. (def.typ=objectdef) and
  6449. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6450. end;
  6451. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6452. begin
  6453. result:=
  6454. assigned(def) and
  6455. (def.typ=objectdef) and
  6456. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6457. end;
  6458. function is_class_or_interface_or_object(def: tdef): boolean;
  6459. begin
  6460. result:=
  6461. assigned(def) and
  6462. (def.typ=objectdef) and
  6463. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6464. end;
  6465. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6466. begin
  6467. result:=
  6468. assigned(def) and
  6469. (def.typ=objectdef) and
  6470. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6471. end;
  6472. function is_implicit_pointer_object_type(def: tdef): boolean;
  6473. begin
  6474. result:=
  6475. assigned(def) and
  6476. (((def.typ=objectdef) and
  6477. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6478. ((target_info.system in systems_jvm) and
  6479. (def.typ=recorddef)));
  6480. end;
  6481. function is_implicit_array_pointer(def: tdef): boolean;
  6482. begin
  6483. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6484. end;
  6485. function is_class_or_object(def: tdef): boolean;
  6486. begin
  6487. result:=
  6488. assigned(def) and
  6489. (def.typ=objectdef) and
  6490. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6491. end;
  6492. function is_record(def: tdef): boolean;
  6493. begin
  6494. result:=
  6495. assigned(def) and
  6496. (def.typ=recorddef);
  6497. end;
  6498. function is_javaclass(def: tdef): boolean;
  6499. begin
  6500. result:=
  6501. assigned(def) and
  6502. (def.typ=objectdef) and
  6503. (tobjectdef(def).objecttype=odt_javaclass);
  6504. end;
  6505. function is_javaclassref(def: tdef): boolean;
  6506. begin
  6507. is_javaclassref:=
  6508. assigned(def) and
  6509. (def.typ=classrefdef) and
  6510. is_javaclass(tclassrefdef(def).pointeddef);
  6511. end;
  6512. function is_javainterface(def: tdef): boolean;
  6513. begin
  6514. result:=
  6515. assigned(def) and
  6516. (def.typ=objectdef) and
  6517. (tobjectdef(def).objecttype=odt_interfacejava);
  6518. end;
  6519. function is_java_class_or_interface(def: tdef): boolean;
  6520. begin
  6521. result:=
  6522. assigned(def) and
  6523. (def.typ=objectdef) and
  6524. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6525. end;
  6526. procedure loadobjctypes;
  6527. begin
  6528. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6529. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6530. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6531. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6532. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6533. end;
  6534. procedure maybeloadcocoatypes;
  6535. var
  6536. tsym: ttypesym;
  6537. cocoaunit: string[15];
  6538. begin
  6539. if assigned(objc_fastenumeration) then
  6540. exit;
  6541. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6542. cocoaunit:='COCOAALL'
  6543. else
  6544. cocoaunit:='IPHONEALL';
  6545. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6546. if assigned(tsym) then
  6547. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6548. else
  6549. objc_fastenumeration:=nil;
  6550. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6551. if assigned(tsym) then
  6552. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6553. else
  6554. objc_fastenumerationstate:=nil;
  6555. end;
  6556. function use_vectorfpu(def : tdef) : boolean;
  6557. begin
  6558. {$ifdef x86}
  6559. {$define use_vectorfpuimplemented}
  6560. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6561. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6562. {$endif x86}
  6563. {$ifdef arm}
  6564. {$define use_vectorfpuimplemented}
  6565. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6566. {$endif arm}
  6567. {$ifndef use_vectorfpuimplemented}
  6568. use_vectorfpu:=false;
  6569. {$endif}
  6570. end;
  6571. function getpointerdef(def: tdef): tpointerdef;
  6572. var
  6573. res: PHashSetItem;
  6574. oldsymtablestack: tsymtablestack;
  6575. begin
  6576. if not assigned(current_module) then
  6577. internalerror(2011071101);
  6578. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6579. if not assigned(res^.Data) then
  6580. begin
  6581. { since these pointerdefs can be reused anywhere in the current
  6582. unit, add them to the global/staticsymtable }
  6583. oldsymtablestack:=symtablestack;
  6584. { do not simply push/pop current_module.localsymtable, because
  6585. that can have side-effects (e.g., it removes helpers) }
  6586. symtablestack:=nil;
  6587. res^.Data:=tpointerdef.create(def);
  6588. if assigned(current_module.localsymtable) then
  6589. current_module.localsymtable.insertdef(tdef(res^.Data))
  6590. else
  6591. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6592. symtablestack:=oldsymtablestack;
  6593. end;
  6594. result:=tpointerdef(res^.Data);
  6595. end;
  6596. function getsingletonarraydef(def: tdef): tarraydef;
  6597. begin
  6598. result:=getarraydef(def,1);
  6599. end;
  6600. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6601. var
  6602. res: PHashSetItem;
  6603. oldsymtablestack: tsymtablestack;
  6604. arrdesc: packed record
  6605. def: tdef;
  6606. elecount: asizeint;
  6607. end;
  6608. begin
  6609. if not assigned(current_module) then
  6610. internalerror(2011081301);
  6611. arrdesc.def:=def;
  6612. arrdesc.elecount:=elecount;
  6613. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6614. if not assigned(res^.Data) then
  6615. begin
  6616. { since these arraydef can be reused anywhere in the current
  6617. unit, add them to the global/staticsymtable }
  6618. oldsymtablestack:=symtablestack;
  6619. symtablestack:=nil;
  6620. res^.Data:=tarraydef.create(0,elecount-1,ptrsinttype);
  6621. tarraydef(res^.Data).elementdef:=def;
  6622. if assigned(current_module.localsymtable) then
  6623. current_module.localsymtable.insertdef(tdef(res^.Data))
  6624. else
  6625. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6626. symtablestack:=oldsymtablestack;
  6627. end;
  6628. result:=tarraydef(res^.Data);
  6629. end;
  6630. {$ifdef x86}
  6631. function default_x86_data_pointer_type: tx86pointertyp;
  6632. begin
  6633. {$ifdef i8086}
  6634. if current_settings.x86memorymodel in x86_far_data_models then
  6635. result:=x86pt_far
  6636. else
  6637. {$endif i8086}
  6638. result:=x86pt_near;
  6639. end;
  6640. {$endif x86}
  6641. end.