symdef.pas 216 KB

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