symdef.pas 236 KB

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