symdef.pas 233 KB

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