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_procvar_intconstr, // Java procvar class constructor that accepts an interface instance for easy Java interoperation
  492. tsk_jvm_virtual_clmethod, // Java wrapper for virtual class method
  493. tsk_field_getter, // getter for a field (callthrough property is passed in skpara)
  494. tsk_field_setter // Setter for a field (callthrough property is passed in skpara)
  495. );
  496. {$ifdef oldregvars}
  497. { register variables }
  498. pregvarinfo = ^tregvarinfo;
  499. tregvarinfo = record
  500. regvars : array[1..maxvarregs] of tsym;
  501. regvars_para : array[1..maxvarregs] of boolean;
  502. regvars_refs : array[1..maxvarregs] of longint;
  503. fpuregvars : array[1..maxfpuvarregs] of tsym;
  504. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  505. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  506. end;
  507. {$endif oldregvars}
  508. { tprocdef }
  509. tprocdef = class(tabstractprocdef)
  510. private
  511. {$ifdef symansistr}
  512. _mangledname : ansistring;
  513. {$else symansistr}
  514. _mangledname : pshortstring;
  515. {$endif}
  516. public
  517. messageinf : tmessageinf;
  518. dispid : longint;
  519. {$ifndef EXTDEBUG}
  520. { where is this function defined and what were the symbol
  521. flags, needed here because there
  522. is only one symbol for all overloaded functions
  523. EXTDEBUG has fileinfo in tdef (PFV) }
  524. fileinfo : tfileposinfo;
  525. {$endif}
  526. symoptions : tsymoptions;
  527. deprecatedmsg : pshortstring;
  528. { symbol owning this definition }
  529. procsym : tsym;
  530. procsymderef : tderef;
  531. { alias names }
  532. aliasnames : TCmdStrList;
  533. { symtables }
  534. localst : TSymtable;
  535. funcretsym : tsym;
  536. funcretsymderef : tderef;
  537. struct : tabstractrecorddef;
  538. structderef : tderef;
  539. {$if defined(powerpc) or defined(m68k)}
  540. { library symbol for AmigaOS/MorphOS }
  541. libsym : tsym;
  542. libsymderef : tderef;
  543. {$endif powerpc or m68k}
  544. { name of the result variable to insert in the localsymtable }
  545. resultname : pshortstring;
  546. { import info }
  547. import_dll,
  548. import_name : pshortstring;
  549. { info for inlining the subroutine, if this pointer is nil,
  550. the procedure can't be inlined }
  551. inlininginfo : pinlininginfo;
  552. {$ifdef jvm}
  553. { generated assembler code; used by JVM backend so it can afterwards
  554. easily write out all methods grouped per class }
  555. exprasmlist : TAsmList;
  556. {$endif jvm}
  557. { temporary reference to structure containing copies of all local
  558. variables and parameters accessed by nested routines; reference to
  559. this structure is passed as "parent frame pointer" on targets that
  560. lack this concept (at least JVM and LLVM); no need to save to/
  561. restore from ppu, since nested routines are always in the same
  562. unit (no need to save to ppu) }
  563. parentfpstruct: tsym;
  564. { pointer to parentfpstruct's type (not yet valid during parsing, so
  565. cannot be used for $parentfp parameter) (no need to save to ppu) }
  566. parentfpstructptrtype: tdef;
  567. { code to copy the parameters accessed from nested routines into the
  568. parentfpstruct (no need to save to ppu) }
  569. parentfpinitblock: tnode;
  570. {$ifdef oldregvars}
  571. regvarinfo: pregvarinfo;
  572. {$endif oldregvars}
  573. { interrupt vector }
  574. interruptvector : longint;
  575. { First/last assembler symbol/instruction in aasmoutput list.
  576. Note: initialised after compiling the code for the procdef, but
  577. not saved to/restored from ppu. Used when inserting debug info }
  578. procstarttai,
  579. procendtai : tai;
  580. import_nr : word;
  581. extnumber : word;
  582. {$ifdef i386}
  583. fpu_used : byte;
  584. {$endif i386}
  585. visibility : tvisibility;
  586. { set to a value different from tsk_none in case this procdef is for
  587. a routine that has to be internally generated by the compiler }
  588. synthetickind : tsynthetickind;
  589. { optional parameter for the synthetic routine generation logic }
  590. skpara: pointer;
  591. { true, if the procedure is only declared
  592. (forward procedure) }
  593. forwarddef,
  594. { true if the procedure is declared in the interface }
  595. interfacedef : boolean;
  596. { true if the procedure has a forward declaration }
  597. hasforward : boolean;
  598. constructor create(level:byte);
  599. constructor ppuload(ppufile:tcompilerppufile);
  600. destructor destroy;override;
  601. procedure ppuwrite(ppufile:tcompilerppufile);override;
  602. procedure buildderef;override;
  603. procedure buildderefimpl;override;
  604. procedure deref;override;
  605. procedure derefimpl;override;
  606. function GetSymtable(t:tGetSymtable):TSymtable;override;
  607. { warnings:
  608. * the symtablestack top has to be the symtable to which the copy
  609. should be added
  610. * getcopy does not create a finished/ready-to-use procdef; it
  611. needs to be finalised afterwards by calling
  612. symcreat.finish_copied_procdef() afterwards
  613. }
  614. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; override;
  615. function getcopy: tstoreddef; override;
  616. function GetTypeName : string;override;
  617. function mangledname : TSymStr;
  618. procedure setmangledname(const s : TSymStr);
  619. function fullprocname(showhidden:boolean):string;
  620. function customprocname(pno: tprocnameoptions):ansistring;
  621. function defaultmangledname: TSymStr;
  622. function cplusplusmangledname : TSymStr;
  623. function objcmangledname : TSymStr;
  624. {$ifdef jvm}
  625. function jvmmangledbasename(signature: boolean): TSymStr;
  626. {$endif}
  627. function is_methodpointer:boolean;override;
  628. function is_addressonly:boolean;override;
  629. procedure make_external;
  630. end;
  631. { single linked list of overloaded procs }
  632. pprocdeflist = ^tprocdeflist;
  633. tprocdeflist = record
  634. def : tprocdef;
  635. defderef : tderef;
  636. next : pprocdeflist;
  637. end;
  638. tstringdef = class(tstoreddef)
  639. stringtype : tstringtype;
  640. len : asizeint;
  641. constructor createshort(l : byte);
  642. constructor loadshort(ppufile:tcompilerppufile);
  643. constructor createlong(l : asizeint);
  644. constructor loadlong(ppufile:tcompilerppufile);
  645. constructor createansi;
  646. constructor loadansi(ppufile:tcompilerppufile);
  647. constructor createwide;
  648. constructor loadwide(ppufile:tcompilerppufile);
  649. constructor createunicode;
  650. constructor loadunicode(ppufile:tcompilerppufile);
  651. function getcopy : tstoreddef;override;
  652. function stringtypname:string;
  653. procedure ppuwrite(ppufile:tcompilerppufile);override;
  654. function GetTypeName:string;override;
  655. function getmangledparaname:TSymStr;override;
  656. function is_publishable : boolean;override;
  657. function alignment : shortint;override;
  658. function needs_inittable : boolean;override;
  659. function getvardef:longint;override;
  660. function is_related(d : tdef) : boolean;override;
  661. end;
  662. { tenumdef }
  663. tenumdef = class(tstoreddef)
  664. minval,
  665. maxval : asizeint;
  666. basedef : tenumdef;
  667. basedefderef : tderef;
  668. symtable : TSymtable;
  669. {$ifdef jvm}
  670. { class representing this enum on the Java side }
  671. classdef : tobjectdef;
  672. classdefderef : tderef;
  673. {$endif}
  674. has_jumps : boolean;
  675. constructor create;
  676. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  677. constructor ppuload(ppufile:tcompilerppufile);
  678. destructor destroy;override;
  679. function getcopy : tstoreddef;override;
  680. procedure ppuwrite(ppufile:tcompilerppufile);override;
  681. procedure buildderef;override;
  682. procedure deref;override;
  683. function GetTypeName:string;override;
  684. function is_publishable : boolean;override;
  685. procedure calcsavesize;
  686. function packedbitsize: asizeint; override;
  687. procedure setmax(_max:asizeint);
  688. procedure setmin(_min:asizeint);
  689. function min:asizeint;
  690. function max:asizeint;
  691. function getfirstsym:tsym;
  692. function int2enumsym(l: asizeint): tsym;
  693. { returns basedef if assigned, otherwise self }
  694. function getbasedef: tenumdef;
  695. end;
  696. tsetdef = class(tstoreddef)
  697. elementdef : tdef;
  698. elementdefderef : tderef;
  699. setbase,
  700. setmax : aword;
  701. constructor create(def:tdef;low, high : asizeint);
  702. constructor ppuload(ppufile:tcompilerppufile);
  703. function getcopy : tstoreddef;override;
  704. procedure ppuwrite(ppufile:tcompilerppufile);override;
  705. procedure buildderef;override;
  706. procedure deref;override;
  707. function GetTypeName:string;override;
  708. function is_publishable : boolean;override;
  709. end;
  710. tdefawaresymtablestack = class(TSymtablestack)
  711. private
  712. procedure addhelpers(st: TSymtable);
  713. procedure removehelpers(st: TSymtable);
  714. public
  715. procedure push(st: TSymtable); override;
  716. procedure pop(st: TSymtable); override;
  717. end;
  718. var
  719. current_structdef: tabstractrecorddef; { used for private functions check !! }
  720. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  721. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  722. { default types }
  723. generrordef, { error in definition }
  724. voidpointertype, { pointer for Void-pointeddef }
  725. charpointertype, { pointer for Char-pointeddef }
  726. widecharpointertype, { pointer for WideChar-pointeddef }
  727. voidfarpointertype,
  728. cundefinedtype,
  729. cformaltype, { unique formal definition }
  730. ctypedformaltype, { unique typed formal definition }
  731. voidtype, { Void (procedure) }
  732. cansichartype, { Char }
  733. cwidechartype, { WideChar }
  734. pasbool8type, { boolean type }
  735. pasbool16type,
  736. pasbool32type,
  737. pasbool64type,
  738. bool8type,
  739. bool16type,
  740. bool32type,
  741. bool64type, { implement me }
  742. u8inttype, { 8-Bit unsigned integer }
  743. s8inttype, { 8-Bit signed integer }
  744. u16inttype, { 16-Bit unsigned integer }
  745. s16inttype, { 16-Bit signed integer }
  746. u32inttype, { 32-Bit unsigned integer }
  747. s32inttype, { 32-Bit signed integer }
  748. u64inttype, { 64-bit unsigned integer }
  749. s64inttype, { 64-bit signed integer }
  750. s32floattype, { 32 bit floating point number }
  751. s64floattype, { 64 bit floating point number }
  752. s80floattype, { 80 bit floating point number }
  753. sc80floattype, { 80 bit floating point number but stored like in C }
  754. s64currencytype, { pointer to a currency type }
  755. cshortstringtype, { pointer to type of short string const }
  756. clongstringtype, { pointer to type of long string const }
  757. cansistringtype, { pointer to type of ansi string const }
  758. cwidestringtype, { pointer to type of wide string const }
  759. cunicodestringtype,
  760. openshortstringtype, { pointer to type of an open shortstring,
  761. needed for readln() }
  762. openchararraytype, { pointer to type of an open array of char,
  763. needed for readln() }
  764. cfiletype, { get the same definition for all file }
  765. { used for stabs }
  766. methodpointertype, { typecasting of methodpointers to extract self }
  767. hresultdef,
  768. { we use only one variant def for every variant class }
  769. cvarianttype,
  770. colevarianttype,
  771. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  772. sinttype,
  773. uinttype,
  774. { unsigned and signed ord type with the same size as a pointer }
  775. ptruinttype,
  776. ptrsinttype,
  777. { several types to simulate more or less C++ objects for GDB }
  778. vmttype,
  779. vmtarraytype,
  780. pvmttype : tdef; { type of classrefs, used for stabs }
  781. { pointer to the anchestor of all classes }
  782. class_tobject : tobjectdef;
  783. { pointer to the ancestor of all COM interfaces }
  784. interface_iunknown : tobjectdef;
  785. { pointer to the ancestor of all dispinterfaces }
  786. interface_idispatch : tobjectdef;
  787. { pointer to the TGUID type
  788. of all interfaces }
  789. rec_tguid : trecorddef;
  790. { pointer to jump buffer }
  791. rec_jmp_buf : trecorddef;
  792. { Objective-C base types }
  793. objc_metaclasstype,
  794. objc_superclasstype,
  795. objc_idtype,
  796. objc_seltype : tpointerdef;
  797. objc_objecttype : trecorddef;
  798. { base type of @protocol(protocolname) Objective-C statements }
  799. objc_protocoltype : tobjectdef;
  800. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  801. objc_fastenumeration : tobjectdef;
  802. objc_fastenumerationstate : trecorddef;
  803. { Java base types }
  804. { java.lang.Object }
  805. java_jlobject : tobjectdef;
  806. { java.lang.Throwable }
  807. java_jlthrowable : tobjectdef;
  808. { FPC base type for records }
  809. java_fpcbaserecordtype : tobjectdef;
  810. { java.lang.String }
  811. java_jlstring : tobjectdef;
  812. { java.lang.Enum }
  813. java_jlenum : tobjectdef;
  814. { java.util.EnumSet }
  815. java_juenumset : tobjectdef;
  816. { java.util.BitSet }
  817. java_jubitset : tobjectdef;
  818. { FPC java implementation of ansistrings }
  819. java_ansistring : tobjectdef;
  820. { FPC java implementation of shortstrings }
  821. java_shortstring : tobjectdef;
  822. { FPC java procvar base class }
  823. java_procvarbase : tobjectdef;
  824. const
  825. {$ifdef i386}
  826. pbestrealtype : ^tdef = @s80floattype;
  827. {$endif}
  828. {$ifdef x86_64}
  829. pbestrealtype : ^tdef = @s80floattype;
  830. {$endif}
  831. {$ifdef m68k}
  832. pbestrealtype : ^tdef = @s64floattype;
  833. {$endif}
  834. {$ifdef alpha}
  835. pbestrealtype : ^tdef = @s64floattype;
  836. {$endif}
  837. {$ifdef powerpc}
  838. pbestrealtype : ^tdef = @s64floattype;
  839. {$endif}
  840. {$ifdef POWERPC64}
  841. pbestrealtype : ^tdef = @s64floattype;
  842. {$endif}
  843. {$ifdef ia64}
  844. pbestrealtype : ^tdef = @s64floattype;
  845. {$endif}
  846. {$ifdef SPARC}
  847. pbestrealtype : ^tdef = @s64floattype;
  848. {$endif SPARC}
  849. {$ifdef vis}
  850. pbestrealtype : ^tdef = @s64floattype;
  851. {$endif vis}
  852. {$ifdef ARM}
  853. pbestrealtype : ^tdef = @s64floattype;
  854. {$endif ARM}
  855. {$ifdef MIPS}
  856. pbestrealtype : ^tdef = @s64floattype;
  857. {$endif MIPS}
  858. {$ifdef AVR}
  859. pbestrealtype : ^tdef = @s64floattype;
  860. {$endif AVR}
  861. {$ifdef JVM}
  862. pbestrealtype : ^tdef = @s64floattype;
  863. {$endif JVM}
  864. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  865. function make_dllmangledname(const dllname,importname:TSymStr;
  866. import_nr : word; pco : tproccalloption):TSymStr;
  867. { should be in the types unit, but the types unit uses the node stuff :( }
  868. function is_interfacecom(def: tdef): boolean;
  869. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  870. function is_any_interface_kind(def: tdef): boolean;
  871. function is_interfacecorba(def: tdef): boolean;
  872. function is_interface(def: tdef): boolean;
  873. function is_dispinterface(def: tdef): boolean;
  874. function is_object(def: tdef): boolean;
  875. function is_class(def: tdef): boolean;
  876. function is_cppclass(def: tdef): boolean;
  877. function is_objectpascal_helper(def: tdef): boolean;
  878. function is_objcclass(def: tdef): boolean;
  879. function is_objcclassref(def: tdef): boolean;
  880. function is_objcprotocol(def: tdef): boolean;
  881. function is_objccategory(def: tdef): boolean;
  882. function is_objc_class_or_protocol(def: tdef): boolean;
  883. function is_objc_protocol_or_category(def: tdef): boolean;
  884. function is_classhelper(def: tdef): boolean;
  885. function is_class_or_interface(def: tdef): boolean;
  886. function is_class_or_interface_or_objc(def: tdef): boolean;
  887. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  888. function is_class_or_interface_or_object(def: tdef): boolean;
  889. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  890. function is_implicit_pointer_object_type(def: tdef): boolean;
  891. function is_class_or_object(def: tdef): boolean;
  892. function is_record(def: tdef): boolean;
  893. function is_javaclass(def: tdef): boolean;
  894. function is_javaclassref(def: tdef): boolean;
  895. function is_javainterface(def: tdef): boolean;
  896. function is_java_class_or_interface(def: tdef): boolean;
  897. procedure loadobjctypes;
  898. procedure maybeloadcocoatypes;
  899. function use_vectorfpu(def : tdef) : boolean;
  900. { returns a pointerdef for def, reusing an existing one in case it exists
  901. in the current module }
  902. function getpointerdef(def: tdef): tpointerdef;
  903. { returns an arraydef for an array containing a single array of def, resuing
  904. an existing one in case it exists in the current module }
  905. function getsingletonarraydef(def: tdef): tarraydef;
  906. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  907. implementation
  908. uses
  909. SysUtils,
  910. cutils,
  911. { global }
  912. verbose,
  913. { target }
  914. systems,aasmcpu,paramgr,
  915. { symtable }
  916. symsym,symtable,symutil,defutil,objcdef,
  917. {$ifdef jvm}
  918. jvmdef,
  919. {$endif}
  920. { module }
  921. fmodule,
  922. { other }
  923. gendef,
  924. fpccrc
  925. ;
  926. {****************************************************************************
  927. Helpers
  928. ****************************************************************************}
  929. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  930. var
  931. s,hs,
  932. prefix : TSymStr;
  933. oldlen,
  934. newlen,
  935. i : longint;
  936. crc : dword;
  937. hp : tparavarsym;
  938. begin
  939. prefix:='';
  940. if not assigned(st) then
  941. internalerror(200204212);
  942. { sub procedures }
  943. while (st.symtabletype=localsymtable) do
  944. begin
  945. if st.defowner.typ<>procdef then
  946. internalerror(200204173);
  947. { Add the full mangledname of procedure to prevent
  948. conflicts with 2 overloads having both a nested procedure
  949. with the same name, see tb0314 (PFV) }
  950. s:=tprocdef(st.defowner).procsym.name;
  951. oldlen:=length(s);
  952. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  953. begin
  954. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  955. if not(vo_is_hidden_para in hp.varoptions) then
  956. s:=s+'$'+hp.vardef.mangledparaname;
  957. end;
  958. if not is_void(tprocdef(st.defowner).returndef) then
  959. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  960. newlen:=length(s);
  961. { Replace with CRC if the parameter line is very long }
  962. if (newlen-oldlen>12) and
  963. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  964. begin
  965. crc:=0;
  966. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  967. begin
  968. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  969. if not(vo_is_hidden_para in hp.varoptions) then
  970. begin
  971. hs:=hp.vardef.mangledparaname;
  972. crc:=UpdateCrc32(crc,hs[1],length(hs));
  973. end;
  974. end;
  975. hs:=hp.vardef.mangledparaname;
  976. crc:=UpdateCrc32(crc,hs[1],length(hs));
  977. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  978. end;
  979. if prefix<>'' then
  980. prefix:=s+'_'+prefix
  981. else
  982. prefix:=s;
  983. if length(prefix)>100 then
  984. begin
  985. crc:=0;
  986. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  987. prefix:='$CRC'+hexstr(crc,8);
  988. end;
  989. st:=st.defowner.owner;
  990. end;
  991. { object/classes symtable, nested type definitions in classes require the while loop }
  992. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  993. begin
  994. if not (st.defowner.typ in [objectdef,recorddef]) then
  995. internalerror(200204174);
  996. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  997. st:=st.defowner.owner;
  998. end;
  999. { symtable must now be static or global }
  1000. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1001. internalerror(200204175);
  1002. result:='';
  1003. if typeprefix<>'' then
  1004. result:=result+typeprefix+'_';
  1005. { Add P$ for program, which can have the same name as
  1006. a unit }
  1007. if (TSymtable(main_module.localsymtable)=st) and
  1008. (not main_module.is_unit) then
  1009. result:=result+'P$'+st.name^
  1010. else
  1011. result:=result+st.name^;
  1012. if prefix<>'' then
  1013. result:=result+'_'+prefix;
  1014. if suffix<>'' then
  1015. result:=result+'_'+suffix;
  1016. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1017. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1018. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1019. { those in the debug map, leading to troubles with dsymutil). So always }
  1020. { add an underscore on darwin. }
  1021. if (target_info.system in systems_darwin) then
  1022. result := '_' + result;
  1023. end;
  1024. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1025. var
  1026. crc : cardinal;
  1027. i : longint;
  1028. use_crc : boolean;
  1029. dllprefix : TSymStr;
  1030. begin
  1031. if (target_info.system in (systems_all_windows + systems_nativent +
  1032. [system_i386_emx, system_i386_os2]))
  1033. and (dllname <> '') then
  1034. begin
  1035. dllprefix:=lower(ExtractFileName(dllname));
  1036. { Remove .dll suffix if present }
  1037. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1038. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1039. use_crc:=false;
  1040. for i:=1 to length(dllprefix) do
  1041. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1042. begin
  1043. use_crc:=true;
  1044. break;
  1045. end;
  1046. if use_crc then
  1047. begin
  1048. crc:=0;
  1049. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1050. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1051. end
  1052. else
  1053. dllprefix:='_$dll$'+dllprefix+'$';
  1054. if importname<>'' then
  1055. result:=dllprefix+importname
  1056. else
  1057. result:=dllprefix+'_index_'+tostr(import_nr);
  1058. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1059. { This allows to import VC++ mangled names from DLLs. }
  1060. { Do not perform replacement, if external symbol is not imported from DLL. }
  1061. if (dllname<>'') then
  1062. begin
  1063. Replace(result,'?','__q$$');
  1064. {$ifdef arm}
  1065. { @ symbol is not allowed in ARM assembler only }
  1066. Replace(result,'@','__a$$');
  1067. {$endif arm}
  1068. end;
  1069. end
  1070. else
  1071. begin
  1072. if importname<>'' then
  1073. begin
  1074. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1075. result:=importname
  1076. else
  1077. result:=target_info.Cprefix+importname;
  1078. end
  1079. else
  1080. result:='_index_'+tostr(import_nr);
  1081. end;
  1082. end;
  1083. {****************************************************************************
  1084. TDEFAWARESYMTABLESTACK
  1085. (symtablestack descendant that does some special actions on
  1086. the pushed/popped symtables)
  1087. ****************************************************************************}
  1088. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  1089. var
  1090. i: integer;
  1091. s: string;
  1092. list: TFPObjectList;
  1093. def: tdef;
  1094. begin
  1095. { search the symtable from first to last; the helper to use will be the
  1096. last one in the list }
  1097. for i:=0 to st.symlist.count-1 do
  1098. begin
  1099. if not (st.symlist[i] is ttypesym) then
  1100. continue;
  1101. def:=ttypesym(st.SymList[i]).typedef;
  1102. if is_objectpascal_helper(def) then
  1103. begin
  1104. s:=make_mangledname('',tobjectdef(def).extendeddef.symtable,'');
  1105. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1106. if not assigned(list) then
  1107. begin
  1108. list:=TFPObjectList.Create(false);
  1109. current_module.extendeddefs.Add(s,list);
  1110. end;
  1111. list.Add(def);
  1112. end
  1113. else
  1114. { add nested helpers as well }
  1115. if def.typ in [recorddef,objectdef] then
  1116. addhelpers(tabstractrecorddef(def).symtable);
  1117. end;
  1118. end;
  1119. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  1120. var
  1121. i, j: integer;
  1122. tmpst: TSymtable;
  1123. list: TFPObjectList;
  1124. begin
  1125. for i:=current_module.extendeddefs.count-1 downto 0 do
  1126. begin
  1127. list:=TFPObjectList(current_module.extendeddefs[i]);
  1128. for j:=list.count-1 downto 0 do
  1129. begin
  1130. if not (list[j] is tobjectdef) then
  1131. Internalerror(2011031501);
  1132. tmpst:=tobjectdef(list[j]).owner;
  1133. repeat
  1134. if tmpst=st then
  1135. begin
  1136. list.delete(j);
  1137. break;
  1138. end
  1139. else
  1140. begin
  1141. if assigned(tmpst.defowner) then
  1142. tmpst:=tmpst.defowner.owner
  1143. else
  1144. tmpst:=nil;
  1145. end;
  1146. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1147. end;
  1148. if list.count=0 then
  1149. current_module.extendeddefs.delete(i);
  1150. end;
  1151. end;
  1152. procedure tdefawaresymtablestack.push(st: TSymtable);
  1153. begin
  1154. { nested helpers will be added as well }
  1155. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1156. (sto_has_helper in st.tableoptions) then
  1157. addhelpers(st);
  1158. inherited push(st);
  1159. end;
  1160. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1161. begin
  1162. inherited pop(st);
  1163. { nested helpers will be removed as well }
  1164. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  1165. (sto_has_helper in st.tableoptions) then
  1166. removehelpers(st);
  1167. end;
  1168. {****************************************************************************
  1169. TDEF (base class for definitions)
  1170. ****************************************************************************}
  1171. constructor tstoreddef.create(dt:tdeftyp);
  1172. var
  1173. insertstack : psymtablestackitem;
  1174. begin
  1175. inherited create(dt);
  1176. savesize := 0;
  1177. {$ifdef EXTDEBUG}
  1178. fileinfo := current_filepos;
  1179. {$endif}
  1180. generictokenbuf:=nil;
  1181. genericdef:=nil;
  1182. { Don't register forwarddefs, they are disposed at the
  1183. end of an type block }
  1184. if (dt=forwarddef) then
  1185. exit;
  1186. { Register in current_module }
  1187. if assigned(current_module) then
  1188. begin
  1189. current_module.deflist.Add(self);
  1190. DefId:=current_module.deflist.Count-1;
  1191. end;
  1192. { Register in symtable stack }
  1193. if assigned(symtablestack) then
  1194. begin
  1195. insertstack:=symtablestack.stack;
  1196. while assigned(insertstack) and
  1197. (insertstack^.symtable.symtabletype=withsymtable) do
  1198. insertstack:=insertstack^.next;
  1199. if not assigned(insertstack) then
  1200. internalerror(200602044);
  1201. insertstack^.symtable.insertdef(self);
  1202. end;
  1203. end;
  1204. destructor tstoreddef.destroy;
  1205. begin
  1206. { Direct calls are not allowed, use symtable.deletedef() }
  1207. if assigned(owner) then
  1208. internalerror(200612311);
  1209. if assigned(generictokenbuf) then
  1210. begin
  1211. generictokenbuf.free;
  1212. generictokenbuf:=nil;
  1213. end;
  1214. inherited destroy;
  1215. end;
  1216. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1217. var
  1218. sizeleft,i : longint;
  1219. buf : array[0..255] of byte;
  1220. begin
  1221. inherited create(dt);
  1222. DefId:=ppufile.getlongint;
  1223. current_module.deflist[DefId]:=self;
  1224. {$ifdef EXTDEBUG}
  1225. fillchar(fileinfo,sizeof(fileinfo),0);
  1226. {$endif}
  1227. { load }
  1228. ppufile.getderef(typesymderef);
  1229. ppufile.getsmallset(defoptions);
  1230. ppufile.getsmallset(defstates);
  1231. if df_generic in defoptions then
  1232. begin
  1233. sizeleft:=ppufile.getlongint;
  1234. initgeneric;
  1235. while sizeleft>0 do
  1236. begin
  1237. if sizeleft>sizeof(buf) then
  1238. i:=sizeof(buf)
  1239. else
  1240. i:=sizeleft;
  1241. ppufile.getdata(buf,i);
  1242. generictokenbuf.write(buf,i);
  1243. dec(sizeleft,i);
  1244. end;
  1245. end;
  1246. if df_specialization in defoptions then
  1247. ppufile.getderef(genericdefderef);
  1248. end;
  1249. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1250. var
  1251. prefix : string[4];
  1252. begin
  1253. if rt=fullrtti then
  1254. begin
  1255. prefix:='RTTI';
  1256. include(defstates,ds_rtti_table_used);
  1257. end
  1258. else
  1259. begin
  1260. prefix:='INIT';
  1261. include(defstates,ds_init_table_used);
  1262. end;
  1263. if assigned(typesym) and
  1264. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1265. result:=make_mangledname(prefix,owner,typesym.name)
  1266. else
  1267. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1268. end;
  1269. function tstoreddef.OwnerHierarchyName: string;
  1270. var
  1271. tmp: tdef;
  1272. begin
  1273. tmp:=self;
  1274. result:='';
  1275. repeat
  1276. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  1277. tmp:=tdef(tmp.owner.defowner)
  1278. else
  1279. break;
  1280. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1281. until tmp=nil;
  1282. end;
  1283. function tstoreddef.in_currentunit: boolean;
  1284. var
  1285. st: tsymtable;
  1286. begin
  1287. st:=owner;
  1288. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1289. st:=st.defowner.owner;
  1290. result:=st.iscurrentunit;
  1291. end;
  1292. function tstoreddef.getcopy : tstoreddef;
  1293. begin
  1294. Message(sym_e_cant_create_unique_type);
  1295. getcopy:=terrordef.create;
  1296. end;
  1297. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1298. var
  1299. sizeleft,i : longint;
  1300. buf : array[0..255] of byte;
  1301. oldintfcrc : boolean;
  1302. begin
  1303. ppufile.putlongint(DefId);
  1304. ppufile.putderef(typesymderef);
  1305. ppufile.putsmallset(defoptions);
  1306. oldintfcrc:=ppufile.do_crc;
  1307. ppufile.do_crc:=false;
  1308. ppufile.putsmallset(defstates);
  1309. if df_generic in defoptions then
  1310. begin
  1311. if assigned(generictokenbuf) then
  1312. begin
  1313. sizeleft:=generictokenbuf.size;
  1314. generictokenbuf.seek(0);
  1315. end
  1316. else
  1317. sizeleft:=0;
  1318. ppufile.putlongint(sizeleft);
  1319. while sizeleft>0 do
  1320. begin
  1321. if sizeleft>sizeof(buf) then
  1322. i:=sizeof(buf)
  1323. else
  1324. i:=sizeleft;
  1325. generictokenbuf.read(buf,i);
  1326. ppufile.putdata(buf,i);
  1327. dec(sizeleft,i);
  1328. end;
  1329. end;
  1330. ppufile.do_crc:=oldintfcrc;
  1331. if df_specialization in defoptions then
  1332. ppufile.putderef(genericdefderef);
  1333. end;
  1334. procedure tstoreddef.buildderef;
  1335. begin
  1336. typesymderef.build(typesym);
  1337. genericdefderef.build(genericdef);
  1338. end;
  1339. procedure tstoreddef.buildderefimpl;
  1340. begin
  1341. end;
  1342. procedure tstoreddef.deref;
  1343. begin
  1344. typesym:=ttypesym(typesymderef.resolve);
  1345. if df_specialization in defoptions then
  1346. genericdef:=tstoreddef(genericdefderef.resolve);
  1347. end;
  1348. procedure tstoreddef.derefimpl;
  1349. begin
  1350. end;
  1351. function tstoreddef.size : asizeint;
  1352. begin
  1353. size:=savesize;
  1354. end;
  1355. function tstoreddef.getvardef:longint;
  1356. begin
  1357. result:=varUndefined;
  1358. end;
  1359. function tstoreddef.alignment : shortint;
  1360. begin
  1361. { natural alignment by default }
  1362. alignment:=size_2_align(savesize);
  1363. { can happen if savesize = 0, e.g. for voiddef or
  1364. an empty record
  1365. }
  1366. if (alignment=0) then
  1367. alignment:=1;
  1368. end;
  1369. { returns true, if the definition can be published }
  1370. function tstoreddef.is_publishable : boolean;
  1371. begin
  1372. is_publishable:=false;
  1373. end;
  1374. { needs an init table }
  1375. function tstoreddef.needs_inittable : boolean;
  1376. begin
  1377. needs_inittable:=false;
  1378. end;
  1379. function tstoreddef.is_intregable : boolean;
  1380. var
  1381. recsize,temp: longint;
  1382. begin
  1383. is_intregable:=false;
  1384. case typ of
  1385. orddef,
  1386. pointerdef,
  1387. enumdef,
  1388. classrefdef:
  1389. is_intregable:=true;
  1390. procvardef :
  1391. is_intregable:=tprocvardef(self).is_addressonly;
  1392. objectdef:
  1393. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1394. setdef:
  1395. is_intregable:=is_smallset(self);
  1396. recorddef:
  1397. begin
  1398. recsize:=size;
  1399. is_intregable:=
  1400. ispowerof2(recsize,temp) and
  1401. (recsize <= sizeof(asizeint));
  1402. end;
  1403. end;
  1404. end;
  1405. function tstoreddef.is_fpuregable : boolean;
  1406. begin
  1407. {$ifdef x86}
  1408. result:=use_vectorfpu(self);
  1409. {$else x86}
  1410. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1411. {$endif x86}
  1412. end;
  1413. procedure tstoreddef.initgeneric;
  1414. begin
  1415. if assigned(generictokenbuf) then
  1416. internalerror(200512131);
  1417. generictokenbuf:=tdynamicarray.create(256);
  1418. end;
  1419. {****************************************************************************
  1420. Tstringdef
  1421. ****************************************************************************}
  1422. constructor tstringdef.createshort(l : byte);
  1423. begin
  1424. inherited create(stringdef);
  1425. stringtype:=st_shortstring;
  1426. len:=l;
  1427. savesize:=len+1;
  1428. end;
  1429. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1430. begin
  1431. inherited ppuload(stringdef,ppufile);
  1432. stringtype:=st_shortstring;
  1433. len:=ppufile.getbyte;
  1434. savesize:=len+1;
  1435. end;
  1436. constructor tstringdef.createlong(l : asizeint);
  1437. begin
  1438. inherited create(stringdef);
  1439. stringtype:=st_longstring;
  1440. len:=l;
  1441. savesize:=sizeof(pint);
  1442. end;
  1443. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1444. begin
  1445. inherited ppuload(stringdef,ppufile);
  1446. stringtype:=st_longstring;
  1447. len:=ppufile.getasizeint;
  1448. savesize:=sizeof(pint);
  1449. end;
  1450. constructor tstringdef.createansi;
  1451. begin
  1452. inherited create(stringdef);
  1453. stringtype:=st_ansistring;
  1454. len:=-1;
  1455. savesize:=sizeof(pint);
  1456. end;
  1457. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1458. begin
  1459. inherited ppuload(stringdef,ppufile);
  1460. stringtype:=st_ansistring;
  1461. len:=ppufile.getaint;
  1462. savesize:=sizeof(pint);
  1463. end;
  1464. constructor tstringdef.createwide;
  1465. begin
  1466. inherited create(stringdef);
  1467. stringtype:=st_widestring;
  1468. len:=-1;
  1469. savesize:=sizeof(pint);
  1470. end;
  1471. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1472. begin
  1473. inherited ppuload(stringdef,ppufile);
  1474. stringtype:=st_widestring;
  1475. len:=ppufile.getaint;
  1476. savesize:=sizeof(pint);
  1477. end;
  1478. constructor tstringdef.createunicode;
  1479. begin
  1480. inherited create(stringdef);
  1481. stringtype:=st_unicodestring;
  1482. len:=-1;
  1483. savesize:=sizeof(pint);
  1484. end;
  1485. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1486. begin
  1487. inherited ppuload(stringdef,ppufile);
  1488. stringtype:=st_unicodestring;
  1489. len:=ppufile.getaint;
  1490. savesize:=sizeof(pint);
  1491. end;
  1492. function tstringdef.getcopy : tstoreddef;
  1493. begin
  1494. result:=tstringdef.create(typ);
  1495. result.typ:=stringdef;
  1496. tstringdef(result).stringtype:=stringtype;
  1497. tstringdef(result).len:=len;
  1498. tstringdef(result).savesize:=savesize;
  1499. end;
  1500. function tstringdef.stringtypname:string;
  1501. const
  1502. typname:array[tstringtype] of string[10]=(
  1503. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1504. );
  1505. begin
  1506. stringtypname:=typname[stringtype];
  1507. end;
  1508. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1509. begin
  1510. inherited ppuwrite(ppufile);
  1511. if stringtype=st_shortstring then
  1512. begin
  1513. {$ifdef extdebug}
  1514. if len > 255 then internalerror(12122002);
  1515. {$endif}
  1516. ppufile.putbyte(byte(len))
  1517. end
  1518. else
  1519. ppufile.putaint(len);
  1520. case stringtype of
  1521. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1522. st_longstring : ppufile.writeentry(iblongstringdef);
  1523. st_ansistring : ppufile.writeentry(ibansistringdef);
  1524. st_widestring : ppufile.writeentry(ibwidestringdef);
  1525. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1526. end;
  1527. end;
  1528. function tstringdef.needs_inittable : boolean;
  1529. begin
  1530. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1531. end;
  1532. function tstringdef.GetTypeName : string;
  1533. const
  1534. names : array[tstringtype] of string[15] = (
  1535. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1536. begin
  1537. GetTypeName:=names[stringtype];
  1538. end;
  1539. function tstringdef.getvardef : longint;
  1540. const
  1541. vardef : array[tstringtype] of longint = (
  1542. varUndefined,varUndefined,varString,varOleStr,varUString);
  1543. begin
  1544. result:=vardef[stringtype];
  1545. end;
  1546. function tstringdef.is_related(d: tdef): boolean;
  1547. begin
  1548. result:=
  1549. (target_info.system=system_jvm_java32) and
  1550. (((stringtype in [st_unicodestring,st_widestring]) and
  1551. ((d=java_jlobject) or
  1552. (d=java_jlstring))) or
  1553. ((stringtype=st_ansistring) and
  1554. ((d=java_jlobject) or
  1555. (d=java_ansistring))));
  1556. end;
  1557. function tstringdef.alignment : shortint;
  1558. begin
  1559. case stringtype of
  1560. st_unicodestring,
  1561. st_widestring,
  1562. st_ansistring:
  1563. alignment:=size_2_align(savesize);
  1564. st_longstring,
  1565. st_shortstring:
  1566. { char to string accesses byte 0 and 1 with one word access }
  1567. if (tf_requires_proper_alignment in target_info.flags) or
  1568. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1569. (m_mac in current_settings.modeswitches) then
  1570. alignment:=size_2_align(2)
  1571. else
  1572. alignment:=size_2_align(1);
  1573. else
  1574. internalerror(200412301);
  1575. end;
  1576. end;
  1577. function tstringdef.getmangledparaname : TSymStr;
  1578. begin
  1579. getmangledparaname:='STRING';
  1580. end;
  1581. function tstringdef.is_publishable : boolean;
  1582. begin
  1583. is_publishable:=true;
  1584. end;
  1585. {****************************************************************************
  1586. TENUMDEF
  1587. ****************************************************************************}
  1588. constructor tenumdef.create;
  1589. begin
  1590. inherited create(enumdef);
  1591. minval:=0;
  1592. maxval:=0;
  1593. calcsavesize;
  1594. has_jumps:=false;
  1595. basedef:=nil;
  1596. symtable:=tenumsymtable.create(self);
  1597. end;
  1598. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1599. begin
  1600. inherited create(enumdef);
  1601. minval:=_min;
  1602. maxval:=_max;
  1603. basedef:=_basedef;
  1604. calcsavesize;
  1605. has_jumps:=false;
  1606. symtable:=basedef.symtable.getcopy;
  1607. include(defoptions, df_copied_def);
  1608. end;
  1609. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1610. begin
  1611. inherited ppuload(enumdef,ppufile);
  1612. minval:=ppufile.getaint;
  1613. maxval:=ppufile.getaint;
  1614. savesize:=ppufile.getaint;
  1615. has_jumps:=false;
  1616. {$ifdef jvm}
  1617. ppufile.getderef(classdefderef);
  1618. {$endif}
  1619. if df_copied_def in defoptions then
  1620. begin
  1621. symtable:=nil;
  1622. ppufile.getderef(basedefderef);
  1623. end
  1624. else
  1625. begin
  1626. // create with nil defowner first to prevent values changes on insert
  1627. symtable:=tenumsymtable.create(nil);
  1628. tenumsymtable(symtable).ppuload(ppufile);
  1629. symtable.defowner:=self;
  1630. end;
  1631. end;
  1632. destructor tenumdef.destroy;
  1633. begin
  1634. symtable.free;
  1635. symtable:=nil;
  1636. inherited destroy;
  1637. end;
  1638. function tenumdef.getcopy : tstoreddef;
  1639. begin
  1640. if assigned(basedef) then
  1641. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1642. else
  1643. begin
  1644. result:=tenumdef.create;
  1645. tenumdef(result).minval:=minval;
  1646. tenumdef(result).maxval:=maxval;
  1647. tenumdef(result).symtable.free;
  1648. tenumdef(result).symtable:=symtable.getcopy;
  1649. tenumdef(result).basedef:=self;
  1650. end;
  1651. {$ifdef jvm}
  1652. tenumdef(result).classdef:=classdef;
  1653. {$endif}
  1654. tenumdef(result).has_jumps:=has_jumps;
  1655. tenumdef(result).basedefderef:=basedefderef;
  1656. include(tenumdef(result).defoptions,df_copied_def);
  1657. end;
  1658. procedure tenumdef.calcsavesize;
  1659. begin
  1660. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1661. savesize:=8
  1662. else
  1663. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1664. savesize:=4
  1665. else
  1666. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1667. savesize:=2
  1668. else
  1669. savesize:=1;
  1670. end;
  1671. function tenumdef.packedbitsize: asizeint;
  1672. var
  1673. sizeval: tconstexprint;
  1674. power: longint;
  1675. begin
  1676. result := 0;
  1677. if (minval >= 0) and
  1678. (maxval <= 1) then
  1679. result := 1
  1680. else
  1681. begin
  1682. if (minval>=0) then
  1683. sizeval:=maxval
  1684. else
  1685. { don't count 0 twice }
  1686. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1687. { 256 must become 512 etc. }
  1688. nextpowerof2(sizeval+1,power);
  1689. result := power;
  1690. end;
  1691. end;
  1692. procedure tenumdef.setmax(_max:asizeint);
  1693. begin
  1694. maxval:=_max;
  1695. calcsavesize;
  1696. end;
  1697. procedure tenumdef.setmin(_min:asizeint);
  1698. begin
  1699. minval:=_min;
  1700. calcsavesize;
  1701. end;
  1702. function tenumdef.min:asizeint;
  1703. begin
  1704. min:=minval;
  1705. end;
  1706. function tenumdef.max:asizeint;
  1707. begin
  1708. max:=maxval;
  1709. end;
  1710. function tenumdef.getfirstsym: tsym;
  1711. var
  1712. i:integer;
  1713. begin
  1714. for i := 0 to symtable.SymList.Count - 1 do
  1715. begin
  1716. result:=tsym(symtable.SymList[i]);
  1717. if tenumsym(result).value=minval then
  1718. exit;
  1719. end;
  1720. result:=nil;
  1721. end;
  1722. function tenumdef.int2enumsym(l: asizeint): tsym;
  1723. var
  1724. i: longint;
  1725. sym: tsym;
  1726. bdef: tenumdef;
  1727. begin
  1728. result:=nil;
  1729. if (l<minval) or
  1730. (l>maxval) then
  1731. exit;
  1732. bdef:=getbasedef;
  1733. for i:=0 to bdef.symtable.symlist.count-1 do
  1734. begin
  1735. sym:=tsym(bdef.symtable.symlist[i]);
  1736. if (sym.typ=enumsym) and
  1737. (tenumsym(sym).value=l) then
  1738. begin
  1739. result:=sym;
  1740. exit;
  1741. end;
  1742. end;
  1743. end;
  1744. function tenumdef.getbasedef: tenumdef;
  1745. begin
  1746. if not assigned(basedef) then
  1747. result:=self
  1748. else
  1749. result:=basedef;
  1750. end;
  1751. procedure tenumdef.buildderef;
  1752. begin
  1753. inherited buildderef;
  1754. if df_copied_def in defoptions then
  1755. basedefderef.build(basedef)
  1756. else
  1757. tenumsymtable(symtable).buildderef;
  1758. {$ifdef jvm}
  1759. classdefderef.build(classdef);
  1760. {$endif}
  1761. end;
  1762. procedure tenumdef.deref;
  1763. begin
  1764. inherited deref;
  1765. if df_copied_def in defoptions then
  1766. begin
  1767. basedef:=tenumdef(basedefderef.resolve);
  1768. symtable:=basedef.symtable.getcopy;
  1769. end
  1770. else
  1771. tenumsymtable(symtable).deref;
  1772. {$ifdef jvm}
  1773. classdef:=tobjectdef(classdefderef.resolve);
  1774. {$endif}
  1775. end;
  1776. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1777. begin
  1778. inherited ppuwrite(ppufile);
  1779. ppufile.putaint(min);
  1780. ppufile.putaint(max);
  1781. ppufile.putaint(savesize);
  1782. {$ifdef jvm}
  1783. ppufile.putderef(classdefderef);
  1784. {$endif}
  1785. if df_copied_def in defoptions then
  1786. ppufile.putderef(basedefderef);
  1787. ppufile.writeentry(ibenumdef);
  1788. if not (df_copied_def in defoptions) then
  1789. tenumsymtable(symtable).ppuwrite(ppufile);
  1790. end;
  1791. function tenumdef.is_publishable : boolean;
  1792. begin
  1793. is_publishable:=true;
  1794. end;
  1795. function tenumdef.GetTypeName : string;
  1796. begin
  1797. GetTypeName:='<enumeration type>';
  1798. end;
  1799. {****************************************************************************
  1800. TORDDEF
  1801. ****************************************************************************}
  1802. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1803. begin
  1804. inherited create(orddef);
  1805. low:=v;
  1806. high:=b;
  1807. ordtype:=t;
  1808. setsize;
  1809. end;
  1810. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1811. begin
  1812. inherited ppuload(orddef,ppufile);
  1813. ordtype:=tordtype(ppufile.getbyte);
  1814. low:=ppufile.getexprint;
  1815. high:=ppufile.getexprint;
  1816. setsize;
  1817. end;
  1818. function torddef.getcopy : tstoreddef;
  1819. begin
  1820. result:=torddef.create(ordtype,low,high);
  1821. result.typ:=orddef;
  1822. torddef(result).low:=low;
  1823. torddef(result).high:=high;
  1824. torddef(result).ordtype:=ordtype;
  1825. torddef(result).savesize:=savesize;
  1826. end;
  1827. function torddef.alignment:shortint;
  1828. begin
  1829. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1830. (ordtype in [s64bit,u64bit]) then
  1831. result := 4
  1832. else
  1833. result := inherited alignment;
  1834. end;
  1835. procedure torddef.setsize;
  1836. const
  1837. sizetbl : array[tordtype] of longint = (
  1838. 0,
  1839. 1,2,4,8,
  1840. 1,2,4,8,
  1841. 1,2,4,8,
  1842. 1,2,4,8,
  1843. 1,2,8
  1844. );
  1845. begin
  1846. savesize:=sizetbl[ordtype];
  1847. end;
  1848. function torddef.packedbitsize: asizeint;
  1849. var
  1850. sizeval: tconstexprint;
  1851. power: longint;
  1852. begin
  1853. result := 0;
  1854. if ordtype = uvoid then
  1855. exit;
  1856. {$ifndef cpu64bitalu}
  1857. if (ordtype in [s64bit,u64bit]) then
  1858. {$else not cpu64bitalu}
  1859. if (ordtype = u64bit) or
  1860. ((ordtype = s64bit) and
  1861. ((low <= (system.low(int64) div 2)) or
  1862. (high > (system.high(int64) div 2)))) then
  1863. {$endif cpu64bitalu}
  1864. result := 64
  1865. else if (low >= 0) and
  1866. (high <= 1) then
  1867. result := 1
  1868. else
  1869. begin
  1870. if (low>=0) then
  1871. sizeval:=high
  1872. else
  1873. { don't count 0 twice }
  1874. sizeval:=(cutils.max(-low,high)*2)-1;
  1875. { 256 must become 512 etc. }
  1876. nextpowerof2(sizeval+1,power);
  1877. result := power;
  1878. end;
  1879. end;
  1880. function torddef.getvardef : longint;
  1881. const
  1882. basetype2vardef : array[tordtype] of longint = (
  1883. varUndefined,
  1884. varbyte,varword,varlongword,varqword,
  1885. varshortint,varsmallint,varinteger,varint64,
  1886. varboolean,varboolean,varboolean,varboolean,
  1887. varboolean,varboolean,varUndefined,varUndefined,
  1888. varUndefined,varUndefined,varCurrency);
  1889. begin
  1890. result:=basetype2vardef[ordtype];
  1891. end;
  1892. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1893. begin
  1894. inherited ppuwrite(ppufile);
  1895. ppufile.putbyte(byte(ordtype));
  1896. ppufile.putexprint(low);
  1897. ppufile.putexprint(high);
  1898. ppufile.writeentry(iborddef);
  1899. end;
  1900. function torddef.is_publishable : boolean;
  1901. begin
  1902. is_publishable:=(ordtype<>uvoid);
  1903. end;
  1904. function torddef.GetTypeName : string;
  1905. const
  1906. names : array[tordtype] of string[20] = (
  1907. 'untyped',
  1908. 'Byte','Word','DWord','QWord',
  1909. 'ShortInt','SmallInt','LongInt','Int64',
  1910. 'Boolean','Boolean16','Boolean32','Boolean64',
  1911. 'ByteBool','WordBool','LongBool','QWordBool',
  1912. 'Char','WideChar','Currency');
  1913. begin
  1914. GetTypeName:=names[ordtype];
  1915. end;
  1916. {****************************************************************************
  1917. TFLOATDEF
  1918. ****************************************************************************}
  1919. constructor tfloatdef.create(t : tfloattype);
  1920. begin
  1921. inherited create(floatdef);
  1922. floattype:=t;
  1923. setsize;
  1924. end;
  1925. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1926. begin
  1927. inherited ppuload(floatdef,ppufile);
  1928. floattype:=tfloattype(ppufile.getbyte);
  1929. setsize;
  1930. end;
  1931. function tfloatdef.getcopy : tstoreddef;
  1932. begin
  1933. result:=tfloatdef.create(floattype);
  1934. result.typ:=floatdef;
  1935. tfloatdef(result).savesize:=savesize;
  1936. end;
  1937. function tfloatdef.alignment:shortint;
  1938. begin
  1939. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1940. case floattype of
  1941. s80real: result:=16;
  1942. s64real,
  1943. s64currency,
  1944. s64comp : result:=4;
  1945. else
  1946. result := inherited alignment;
  1947. end
  1948. else
  1949. result := inherited alignment;
  1950. end;
  1951. procedure tfloatdef.setsize;
  1952. begin
  1953. case floattype of
  1954. s32real : savesize:=4;
  1955. s80real : savesize:=10;
  1956. sc80real:
  1957. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1958. system_x86_64_linux,system_x86_64_freebsd,
  1959. system_x86_64_solaris,system_x86_64_embedded] then
  1960. savesize:=16
  1961. else
  1962. savesize:=12;
  1963. s64real,
  1964. s64currency,
  1965. s64comp : savesize:=8;
  1966. else
  1967. savesize:=0;
  1968. end;
  1969. end;
  1970. function tfloatdef.getvardef : longint;
  1971. const
  1972. floattype2vardef : array[tfloattype] of longint = (
  1973. varSingle,varDouble,varUndefined,varUndefined,
  1974. varUndefined,varCurrency,varUndefined);
  1975. begin
  1976. if (upper(typename)='TDATETIME') and
  1977. assigned(owner) and
  1978. assigned(owner.name) and
  1979. (owner.name^='SYSTEM') then
  1980. result:=varDate
  1981. else
  1982. result:=floattype2vardef[floattype];
  1983. end;
  1984. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1985. begin
  1986. inherited ppuwrite(ppufile);
  1987. ppufile.putbyte(byte(floattype));
  1988. ppufile.writeentry(ibfloatdef);
  1989. end;
  1990. function tfloatdef.is_publishable : boolean;
  1991. begin
  1992. is_publishable:=true;
  1993. end;
  1994. function tfloatdef.GetTypeName : string;
  1995. const
  1996. names : array[tfloattype] of string[20] = (
  1997. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1998. begin
  1999. GetTypeName:=names[floattype];
  2000. end;
  2001. {****************************************************************************
  2002. TFILEDEF
  2003. ****************************************************************************}
  2004. constructor tfiledef.createtext;
  2005. begin
  2006. inherited create(filedef);
  2007. filetyp:=ft_text;
  2008. typedfiledef:=nil;
  2009. setsize;
  2010. end;
  2011. constructor tfiledef.createuntyped;
  2012. begin
  2013. inherited create(filedef);
  2014. filetyp:=ft_untyped;
  2015. typedfiledef:=nil;
  2016. setsize;
  2017. end;
  2018. constructor tfiledef.createtyped(def:tdef);
  2019. begin
  2020. inherited create(filedef);
  2021. filetyp:=ft_typed;
  2022. typedfiledef:=def;
  2023. setsize;
  2024. end;
  2025. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2026. begin
  2027. inherited ppuload(filedef,ppufile);
  2028. filetyp:=tfiletyp(ppufile.getbyte);
  2029. if filetyp=ft_typed then
  2030. ppufile.getderef(typedfiledefderef)
  2031. else
  2032. typedfiledef:=nil;
  2033. setsize;
  2034. end;
  2035. function tfiledef.getcopy : tstoreddef;
  2036. begin
  2037. case filetyp of
  2038. ft_typed:
  2039. result:=tfiledef.createtyped(typedfiledef);
  2040. ft_untyped:
  2041. result:=tfiledef.createuntyped;
  2042. ft_text:
  2043. result:=tfiledef.createtext;
  2044. else
  2045. internalerror(2004121201);
  2046. end;
  2047. end;
  2048. procedure tfiledef.buildderef;
  2049. begin
  2050. inherited buildderef;
  2051. if filetyp=ft_typed then
  2052. typedfiledefderef.build(typedfiledef);
  2053. end;
  2054. procedure tfiledef.deref;
  2055. begin
  2056. inherited deref;
  2057. if filetyp=ft_typed then
  2058. typedfiledef:=tdef(typedfiledefderef.resolve);
  2059. end;
  2060. procedure tfiledef.setsize;
  2061. begin
  2062. {$ifdef cpu64bitaddr}
  2063. case filetyp of
  2064. ft_text :
  2065. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  2066. savesize:=632{+8}
  2067. else
  2068. savesize:=628{+8};
  2069. ft_typed,
  2070. ft_untyped :
  2071. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  2072. savesize:=372
  2073. else
  2074. savesize:=368;
  2075. end;
  2076. {$endif cpu64bitaddr}
  2077. {$ifdef cpu32bitaddr}
  2078. case filetyp of
  2079. ft_text :
  2080. savesize:=592{+4};
  2081. ft_typed,
  2082. ft_untyped :
  2083. savesize:=332;
  2084. end;
  2085. {$endif cpu32bitaddr}
  2086. {$ifdef cpu8bitaddr}
  2087. case filetyp of
  2088. ft_text :
  2089. savesize:=127;
  2090. ft_typed,
  2091. ft_untyped :
  2092. savesize:=127;
  2093. end;
  2094. {$endif cpu8bitaddr}
  2095. end;
  2096. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2097. begin
  2098. inherited ppuwrite(ppufile);
  2099. ppufile.putbyte(byte(filetyp));
  2100. if filetyp=ft_typed then
  2101. ppufile.putderef(typedfiledefderef);
  2102. ppufile.writeentry(ibfiledef);
  2103. end;
  2104. function tfiledef.GetTypeName : string;
  2105. begin
  2106. case filetyp of
  2107. ft_untyped:
  2108. GetTypeName:='File';
  2109. ft_typed:
  2110. GetTypeName:='File Of '+typedfiledef.typename;
  2111. ft_text:
  2112. GetTypeName:='Text'
  2113. end;
  2114. end;
  2115. function tfiledef.getmangledparaname : TSymStr;
  2116. begin
  2117. case filetyp of
  2118. ft_untyped:
  2119. getmangledparaname:='FILE';
  2120. ft_typed:
  2121. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2122. ft_text:
  2123. getmangledparaname:='TEXT'
  2124. end;
  2125. end;
  2126. {****************************************************************************
  2127. TVARIANTDEF
  2128. ****************************************************************************}
  2129. constructor tvariantdef.create(v : tvarianttype);
  2130. begin
  2131. inherited create(variantdef);
  2132. varianttype:=v;
  2133. setsize;
  2134. end;
  2135. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2136. begin
  2137. inherited ppuload(variantdef,ppufile);
  2138. varianttype:=tvarianttype(ppufile.getbyte);
  2139. setsize;
  2140. end;
  2141. function tvariantdef.getcopy : tstoreddef;
  2142. begin
  2143. result:=tvariantdef.create(varianttype);
  2144. end;
  2145. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2146. begin
  2147. inherited ppuwrite(ppufile);
  2148. ppufile.putbyte(byte(varianttype));
  2149. ppufile.writeentry(ibvariantdef);
  2150. end;
  2151. function tvariantdef.getvardef : longint;
  2152. begin
  2153. Result:=varVariant;
  2154. end;
  2155. procedure tvariantdef.setsize;
  2156. begin
  2157. {$ifdef cpu64bitaddr}
  2158. savesize:=24;
  2159. {$else cpu64bitaddr}
  2160. savesize:=16;
  2161. {$endif cpu64bitaddr}
  2162. end;
  2163. function tvariantdef.GetTypeName : string;
  2164. begin
  2165. case varianttype of
  2166. vt_normalvariant:
  2167. GetTypeName:='Variant';
  2168. vt_olevariant:
  2169. GetTypeName:='OleVariant';
  2170. end;
  2171. end;
  2172. function tvariantdef.needs_inittable : boolean;
  2173. begin
  2174. needs_inittable:=true;
  2175. end;
  2176. function tvariantdef.is_publishable : boolean;
  2177. begin
  2178. is_publishable:=true;
  2179. end;
  2180. {****************************************************************************
  2181. TABSTRACtpointerdef
  2182. ****************************************************************************}
  2183. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2184. begin
  2185. inherited create(dt);
  2186. pointeddef:=def;
  2187. savesize:=sizeof(pint);
  2188. end;
  2189. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2190. begin
  2191. inherited ppuload(dt,ppufile);
  2192. ppufile.getderef(pointeddefderef);
  2193. savesize:=sizeof(pint);
  2194. end;
  2195. procedure tabstractpointerdef.buildderef;
  2196. begin
  2197. inherited buildderef;
  2198. pointeddefderef.build(pointeddef);
  2199. end;
  2200. procedure tabstractpointerdef.deref;
  2201. begin
  2202. inherited deref;
  2203. pointeddef:=tdef(pointeddefderef.resolve);
  2204. end;
  2205. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2206. begin
  2207. inherited ppuwrite(ppufile);
  2208. ppufile.putderef(pointeddefderef);
  2209. end;
  2210. {****************************************************************************
  2211. tpointerdef
  2212. ****************************************************************************}
  2213. constructor tpointerdef.create(def:tdef);
  2214. begin
  2215. inherited create(pointerdef,def);
  2216. is_far:=false;
  2217. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2218. end;
  2219. constructor tpointerdef.createfar(def:tdef);
  2220. begin
  2221. inherited create(pointerdef,def);
  2222. is_far:=true;
  2223. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2224. end;
  2225. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2226. begin
  2227. inherited ppuload(pointerdef,ppufile);
  2228. is_far:=(ppufile.getbyte<>0);
  2229. has_pointer_math:=(ppufile.getbyte<>0);
  2230. end;
  2231. function tpointerdef.getcopy : tstoreddef;
  2232. begin
  2233. { don't use direct pointeddef if it is a forwarddef because in other case
  2234. one of them will be destroyed on forward type resolve and the second will
  2235. point to garbage }
  2236. if pointeddef.typ=forwarddef then
  2237. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  2238. else
  2239. result:=tpointerdef.create(pointeddef);
  2240. tpointerdef(result).is_far:=is_far;
  2241. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2242. tpointerdef(result).savesize:=savesize;
  2243. end;
  2244. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2245. begin
  2246. inherited ppuwrite(ppufile);
  2247. ppufile.putbyte(byte(is_far));
  2248. ppufile.putbyte(byte(has_pointer_math));
  2249. ppufile.writeentry(ibpointerdef);
  2250. end;
  2251. function tpointerdef.GetTypeName : string;
  2252. begin
  2253. if is_far then
  2254. GetTypeName:='^'+pointeddef.typename+';far'
  2255. else
  2256. GetTypeName:='^'+pointeddef.typename;
  2257. end;
  2258. {****************************************************************************
  2259. TCLASSREFDEF
  2260. ****************************************************************************}
  2261. constructor tclassrefdef.create(def:tdef);
  2262. begin
  2263. inherited create(classrefdef,def);
  2264. end;
  2265. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2266. begin
  2267. inherited ppuload(classrefdef,ppufile);
  2268. end;
  2269. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2270. begin
  2271. inherited ppuwrite(ppufile);
  2272. ppufile.writeentry(ibclassrefdef);
  2273. end;
  2274. function tclassrefdef.getcopy:tstoreddef;
  2275. begin
  2276. if pointeddef.typ=forwarddef then
  2277. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2278. else
  2279. result:=tclassrefdef.create(pointeddef);
  2280. tclassrefdef(result).savesize:=savesize;
  2281. end;
  2282. function tclassrefdef.GetTypeName : string;
  2283. begin
  2284. GetTypeName:='Class Of '+pointeddef.typename;
  2285. end;
  2286. function tclassrefdef.is_publishable : boolean;
  2287. begin
  2288. result:=true;
  2289. end;
  2290. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2291. begin
  2292. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2293. result:=inherited rtti_mangledname(rt)
  2294. else
  2295. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2296. end;
  2297. procedure tclassrefdef.register_created_object_type;
  2298. begin
  2299. tobjectdef(pointeddef).register_created_classref_type;
  2300. end;
  2301. {***************************************************************************
  2302. TSETDEF
  2303. ***************************************************************************}
  2304. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2305. var
  2306. setallocbits: aint;
  2307. packedsavesize: aint;
  2308. begin
  2309. inherited create(setdef);
  2310. elementdef:=def;
  2311. setmax:=high;
  2312. if (current_settings.setalloc=0) then
  2313. begin
  2314. setbase:=0;
  2315. if (high<32) then
  2316. savesize:=Sizeof(longint)
  2317. else if (high<256) then
  2318. savesize:=32
  2319. else
  2320. savesize:=(high+7) div 8
  2321. end
  2322. else
  2323. begin
  2324. setallocbits:=current_settings.setalloc*8;
  2325. setbase:=low and not(setallocbits-1);
  2326. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2327. savesize:=packedsavesize;
  2328. if savesize=3 then
  2329. savesize:=4;
  2330. end;
  2331. end;
  2332. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2333. begin
  2334. inherited ppuload(setdef,ppufile);
  2335. ppufile.getderef(elementdefderef);
  2336. savesize:=ppufile.getaint;
  2337. setbase:=ppufile.getaint;
  2338. setmax:=ppufile.getaint;
  2339. end;
  2340. function tsetdef.getcopy : tstoreddef;
  2341. begin
  2342. result:=tsetdef.create(elementdef,setbase,setmax);
  2343. { the copy might have been created with a different setalloc setting }
  2344. tsetdef(result).savesize:=savesize;
  2345. end;
  2346. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2347. begin
  2348. inherited ppuwrite(ppufile);
  2349. ppufile.putderef(elementdefderef);
  2350. ppufile.putaint(savesize);
  2351. ppufile.putaint(setbase);
  2352. ppufile.putaint(setmax);
  2353. ppufile.writeentry(ibsetdef);
  2354. end;
  2355. procedure tsetdef.buildderef;
  2356. begin
  2357. inherited buildderef;
  2358. elementdefderef.build(elementdef);
  2359. end;
  2360. procedure tsetdef.deref;
  2361. begin
  2362. inherited deref;
  2363. elementdef:=tdef(elementdefderef.resolve);
  2364. end;
  2365. function tsetdef.is_publishable : boolean;
  2366. begin
  2367. is_publishable:=savesize in [1,2,4];
  2368. end;
  2369. function tsetdef.GetTypeName : string;
  2370. begin
  2371. if assigned(elementdef) then
  2372. GetTypeName:='Set Of '+elementdef.typename
  2373. else
  2374. GetTypeName:='Empty Set';
  2375. end;
  2376. {***************************************************************************
  2377. TFORMALDEF
  2378. ***************************************************************************}
  2379. constructor tformaldef.create(Atyped:boolean);
  2380. begin
  2381. inherited create(formaldef);
  2382. typed:=Atyped;
  2383. savesize:=0;
  2384. end;
  2385. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2386. begin
  2387. inherited ppuload(formaldef,ppufile);
  2388. typed:=boolean(ppufile.getbyte);
  2389. savesize:=0;
  2390. end;
  2391. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2392. begin
  2393. inherited ppuwrite(ppufile);
  2394. ppufile.putbyte(byte(typed));
  2395. ppufile.writeentry(ibformaldef);
  2396. end;
  2397. function tformaldef.GetTypeName : string;
  2398. begin
  2399. if typed then
  2400. GetTypeName:='<Typed formal type>'
  2401. else
  2402. GetTypeName:='<Formal type>';
  2403. end;
  2404. {***************************************************************************
  2405. TARRAYDEF
  2406. ***************************************************************************}
  2407. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2408. begin
  2409. inherited create(arraydef);
  2410. lowrange:=l;
  2411. highrange:=h;
  2412. rangedef:=def;
  2413. _elementdef:=nil;
  2414. arrayoptions:=[];
  2415. symtable:=tarraysymtable.create(self);
  2416. end;
  2417. destructor tarraydef.destroy;
  2418. begin
  2419. symtable.free;
  2420. symtable:=nil;
  2421. inherited;
  2422. end;
  2423. constructor tarraydef.create_from_pointer(def:tdef);
  2424. begin
  2425. { use -1 so that the elecount will not overflow }
  2426. self.create(0,high(asizeint)-1,s32inttype);
  2427. arrayoptions:=[ado_IsConvertedPointer];
  2428. setelementdef(def);
  2429. end;
  2430. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2431. begin
  2432. inherited ppuload(arraydef,ppufile);
  2433. { the addresses are calculated later }
  2434. ppufile.getderef(_elementdefderef);
  2435. ppufile.getderef(rangedefderef);
  2436. lowrange:=ppufile.getaint;
  2437. highrange:=ppufile.getaint;
  2438. ppufile.getsmallset(arrayoptions);
  2439. symtable:=tarraysymtable.create(self);
  2440. tarraysymtable(symtable).ppuload(ppufile)
  2441. end;
  2442. function tarraydef.getcopy : tstoreddef;
  2443. begin
  2444. result:=tarraydef.create(lowrange,highrange,rangedef);
  2445. tarraydef(result).arrayoptions:=arrayoptions;
  2446. tarraydef(result)._elementdef:=_elementdef;
  2447. end;
  2448. procedure tarraydef.buildderef;
  2449. begin
  2450. inherited buildderef;
  2451. tarraysymtable(symtable).buildderef;
  2452. _elementdefderef.build(_elementdef);
  2453. rangedefderef.build(rangedef);
  2454. end;
  2455. procedure tarraydef.deref;
  2456. begin
  2457. inherited deref;
  2458. tarraysymtable(symtable).deref;
  2459. _elementdef:=tdef(_elementdefderef.resolve);
  2460. rangedef:=tdef(rangedefderef.resolve);
  2461. end;
  2462. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2463. begin
  2464. inherited ppuwrite(ppufile);
  2465. ppufile.putderef(_elementdefderef);
  2466. ppufile.putderef(rangedefderef);
  2467. ppufile.putaint(lowrange);
  2468. ppufile.putaint(highrange);
  2469. ppufile.putsmallset(arrayoptions);
  2470. ppufile.writeentry(ibarraydef);
  2471. tarraysymtable(symtable).ppuwrite(ppufile);
  2472. end;
  2473. function tarraydef.elesize : asizeint;
  2474. begin
  2475. if (ado_IsBitPacked in arrayoptions) then
  2476. internalerror(2006080101);
  2477. if assigned(_elementdef) then
  2478. result:=_elementdef.size
  2479. else
  2480. result:=0;
  2481. end;
  2482. function tarraydef.elepackedbitsize : asizeint;
  2483. begin
  2484. if not(ado_IsBitPacked in arrayoptions) then
  2485. internalerror(2006080102);
  2486. if assigned(_elementdef) then
  2487. result:=_elementdef.packedbitsize
  2488. else
  2489. result:=0;
  2490. end;
  2491. function tarraydef.elecount : asizeuint;
  2492. var
  2493. qhigh,qlow : qword;
  2494. begin
  2495. if ado_IsDynamicArray in arrayoptions then
  2496. begin
  2497. result:=0;
  2498. exit;
  2499. end;
  2500. if (highrange>0) and (lowrange<0) then
  2501. begin
  2502. qhigh:=highrange;
  2503. qlow:=qword(-lowrange);
  2504. { prevent overflow, return 0 to indicate overflow }
  2505. if qhigh+qlow>qword(high(asizeint)-1) then
  2506. result:=0
  2507. else
  2508. result:=qhigh+qlow+1;
  2509. end
  2510. else
  2511. result:=int64(highrange)-lowrange+1;
  2512. end;
  2513. function tarraydef.size : asizeint;
  2514. var
  2515. cachedelecount : asizeuint;
  2516. cachedelesize : asizeint;
  2517. begin
  2518. if ado_IsDynamicArray in arrayoptions then
  2519. begin
  2520. size:=sizeof(pint);
  2521. exit;
  2522. end;
  2523. { Tarraydef.size may never be called for an open array! }
  2524. if highrange<lowrange then
  2525. internalerror(99080501);
  2526. if not (ado_IsBitPacked in arrayoptions) then
  2527. cachedelesize:=elesize
  2528. else
  2529. cachedelesize := elepackedbitsize;
  2530. cachedelecount:=elecount;
  2531. if (cachedelesize = 0) then
  2532. begin
  2533. size := 0;
  2534. exit;
  2535. end;
  2536. if (cachedelecount = 0) then
  2537. begin
  2538. size := -1;
  2539. exit;
  2540. end;
  2541. { prevent overflow, return -1 to indicate overflow }
  2542. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2543. if (cachedelecount > asizeuint(high(asizeint))) or
  2544. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2545. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2546. accessing the array, see ncgmem (PFV) }
  2547. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2548. begin
  2549. result:=-1;
  2550. exit;
  2551. end;
  2552. result:=cachedelesize*asizeint(cachedelecount);
  2553. if (ado_IsBitPacked in arrayoptions) then
  2554. { can't just add 7 and divide by 8, because that may overflow }
  2555. result:=result div 8 + ord((result mod 8)<>0);
  2556. end;
  2557. procedure tarraydef.setelementdef(def:tdef);
  2558. begin
  2559. _elementdef:=def;
  2560. if not(
  2561. (ado_IsDynamicArray in arrayoptions) or
  2562. (ado_IsConvertedPointer in arrayoptions) or
  2563. (highrange<lowrange)
  2564. ) and
  2565. (size=-1) then
  2566. Message(sym_e_segment_too_large);
  2567. end;
  2568. function tarraydef.alignment : shortint;
  2569. begin
  2570. { alignment of dyn. arrays doesn't depend on the element size }
  2571. if (ado_IsDynamicArray in arrayoptions) then
  2572. alignment:=size_2_align(sizeof(pint))
  2573. { alignment is the alignment of the elements }
  2574. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2575. ((elementdef.typ=objectdef) and
  2576. is_object(elementdef)) then
  2577. alignment:=elementdef.alignment
  2578. { alignment is the size of the elements }
  2579. else if not (ado_IsBitPacked in arrayoptions) then
  2580. alignment:=size_2_align(elesize)
  2581. else
  2582. alignment:=packedbitsloadsize(elepackedbitsize);
  2583. end;
  2584. function tarraydef.needs_inittable : boolean;
  2585. begin
  2586. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2587. end;
  2588. function tarraydef.GetTypeName : string;
  2589. begin
  2590. if (ado_IsConstString in arrayoptions) then
  2591. result:='Constant String'
  2592. else if (ado_isarrayofconst in arrayoptions) or
  2593. (ado_isConstructor in arrayoptions) then
  2594. begin
  2595. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2596. GetTypeName:='Array Of Const'
  2597. else
  2598. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  2599. end
  2600. else if (ado_IsDynamicArray in arrayoptions) then
  2601. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  2602. else if ((highrange=-1) and (lowrange=0)) then
  2603. GetTypeName:='{Open} Array Of '+elementdef.typename
  2604. else
  2605. begin
  2606. result := '';
  2607. if (ado_IsBitPacked in arrayoptions) then
  2608. result:='BitPacked ';
  2609. if rangedef.typ=enumdef then
  2610. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2611. else
  2612. result:=result+'Array['+tostr(lowrange)+'..'+
  2613. tostr(highrange)+'] Of '+elementdef.typename
  2614. end;
  2615. end;
  2616. function tarraydef.getmangledparaname : TSymStr;
  2617. begin
  2618. if ado_isarrayofconst in arrayoptions then
  2619. getmangledparaname:='array_of_const'
  2620. else
  2621. if ((highrange=-1) and (lowrange=0)) then
  2622. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2623. else
  2624. internalerror(200204176);
  2625. end;
  2626. function tarraydef.is_publishable : boolean;
  2627. begin
  2628. Result:=ado_IsDynamicArray in arrayoptions;
  2629. end;
  2630. {***************************************************************************
  2631. tabstractrecorddef
  2632. ***************************************************************************}
  2633. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2634. begin
  2635. inherited create(dt);
  2636. objname:=stringdup(upper(n));
  2637. objrealname:=stringdup(n);
  2638. objectoptions:=[];
  2639. if assigned(current_module.namespace) then
  2640. begin
  2641. import_lib:=stringdup(current_module.namespace^);
  2642. replace(import_lib^,'.','/');
  2643. end;
  2644. end;
  2645. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2646. begin
  2647. inherited ppuload(dt,ppufile);
  2648. objrealname:=stringdup(ppufile.getstring);
  2649. objname:=stringdup(upper(objrealname^));
  2650. import_lib:=stringdup(ppufile.getstring);
  2651. { only used for external C++ classes and Java classes/records }
  2652. if (import_lib^='') then
  2653. stringdispose(import_lib);
  2654. ppufile.getsmallset(objectoptions);
  2655. end;
  2656. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2657. begin
  2658. inherited ppuwrite(ppufile);
  2659. ppufile.putstring(objrealname^);
  2660. if assigned(import_lib) then
  2661. ppufile.putstring(import_lib^)
  2662. else
  2663. ppufile.putstring('');
  2664. ppufile.putsmallset(objectoptions);
  2665. end;
  2666. destructor tabstractrecorddef.destroy;
  2667. begin
  2668. stringdispose(objname);
  2669. stringdispose(objrealname);
  2670. stringdispose(import_lib);
  2671. tcinitcode.free;
  2672. inherited destroy;
  2673. end;
  2674. procedure tabstractrecorddef.check_forwards;
  2675. begin
  2676. tstoredsymtable(symtable).check_forwards;
  2677. end;
  2678. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2679. var
  2680. i: longint;
  2681. sym: tsym;
  2682. begin
  2683. for i:=0 to symtable.SymList.Count-1 do
  2684. begin
  2685. sym:=tsym(symtable.SymList[i]);
  2686. if sym.typ=procsym then
  2687. begin
  2688. result:=tprocsym(sym).find_procdef_bytype(pt);
  2689. if assigned(result) then
  2690. exit;
  2691. end;
  2692. end;
  2693. result:=nil;
  2694. end;
  2695. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2696. begin
  2697. if t=gs_record then
  2698. GetSymtable:=symtable
  2699. else
  2700. GetSymtable:=nil;
  2701. end;
  2702. function tabstractrecorddef.is_packed:boolean;
  2703. begin
  2704. result:=tabstractrecordsymtable(symtable).is_packed;
  2705. end;
  2706. function tabstractrecorddef.RttiName: string;
  2707. begin
  2708. Result:=OwnerHierarchyName+objrealname^;
  2709. end;
  2710. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2711. var
  2712. sym : tsym;
  2713. i : integer;
  2714. pd : tprocdef;
  2715. hashedid : THashedIDString;
  2716. begin
  2717. result:=nil;
  2718. hashedid.id:='GETENUMERATOR';
  2719. sym:=tsym(symtable.FindWithHash(hashedid));
  2720. if assigned(sym) and (sym.typ=procsym) then
  2721. begin
  2722. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2723. begin
  2724. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2725. if (pd.proctypeoption = potype_function) and
  2726. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2727. (pd.visibility >= vis_public) then
  2728. begin
  2729. result:=pd;
  2730. exit;
  2731. end;
  2732. end;
  2733. end;
  2734. end;
  2735. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2736. var
  2737. sym : tsym;
  2738. i : integer;
  2739. pd : tprocdef;
  2740. hashedid : THashedIDString;
  2741. begin
  2742. result:=nil;
  2743. // first search for po_enumerator_movenext method modifier
  2744. // then search for public function MoveNext: Boolean
  2745. for i:=0 to symtable.SymList.Count-1 do
  2746. begin
  2747. sym:=TSym(symtable.SymList[i]);
  2748. if (sym.typ=procsym) then
  2749. begin
  2750. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2751. if assigned(pd) then
  2752. begin
  2753. result:=pd;
  2754. exit;
  2755. end;
  2756. end;
  2757. end;
  2758. hashedid.id:='MOVENEXT';
  2759. sym:=tsym(symtable.FindWithHash(hashedid));
  2760. if assigned(sym) and (sym.typ=procsym) then
  2761. begin
  2762. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2763. begin
  2764. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2765. if (pd.proctypeoption = potype_function) and
  2766. is_boolean(pd.returndef) and
  2767. (pd.minparacount = 0) and
  2768. (pd.visibility >= vis_public) then
  2769. begin
  2770. result:=pd;
  2771. exit;
  2772. end;
  2773. end;
  2774. end;
  2775. end;
  2776. function tabstractrecorddef.search_enumerator_current: tsym;
  2777. var
  2778. sym: tsym;
  2779. i: integer;
  2780. hashedid : THashedIDString;
  2781. begin
  2782. result:=nil;
  2783. // first search for ppo_enumerator_current property modifier
  2784. // then search for public property Current
  2785. for i:=0 to symtable.SymList.Count-1 do
  2786. begin
  2787. sym:=TSym(symtable.SymList[i]);
  2788. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2789. begin
  2790. result:=sym;
  2791. exit;
  2792. end;
  2793. end;
  2794. hashedid.id:='CURRENT';
  2795. sym:=tsym(symtable.FindWithHash(hashedid));
  2796. if assigned(sym) and (sym.typ=propertysym) and
  2797. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2798. begin
  2799. result:=sym;
  2800. exit;
  2801. end;
  2802. end;
  2803. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  2804. var
  2805. st: tsymtable;
  2806. enclosingdef: tdef;
  2807. begin
  2808. if typ=objectdef then
  2809. result:=tobjectdef(self).objextname^
  2810. else if assigned(typesym) then
  2811. result:=typesym.realname
  2812. { have to generate anonymous nested type in current unit/class/record }
  2813. else
  2814. internalerror(2011032601);
  2815. { in case of specializations, add some extras to prevent name conflicts
  2816. with nested classes }
  2817. if df_specialization in defoptions then
  2818. result:='$'+result+'$specialization$';
  2819. st:=owner;
  2820. while assigned(st) and
  2821. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  2822. begin
  2823. { nested classes are named as "OuterClass$InnerClass" }
  2824. enclosingdef:=tdef(st.defowner);
  2825. if enclosingdef.typ=procdef then
  2826. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  2827. else if enclosingdef.typ=objectdef then
  2828. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  2829. else if assigned(enclosingdef.typesym) then
  2830. result:=enclosingdef.typesym.realname+'$'+result
  2831. else
  2832. internalerror(2011060305);
  2833. st:=enclosingdef.owner;
  2834. end;
  2835. if with_package_name and
  2836. assigned(import_lib) then
  2837. result:=import_lib^+'/'+result;
  2838. end;
  2839. {***************************************************************************
  2840. trecorddef
  2841. ***************************************************************************}
  2842. constructor trecorddef.create(const n:string; p:TSymtable);
  2843. begin
  2844. inherited create(n,recorddef);
  2845. symtable:=p;
  2846. { we can own the symtable only if nobody else owns a copy so far }
  2847. if symtable.refcount=1 then
  2848. symtable.defowner:=self;
  2849. isunion:=false;
  2850. end;
  2851. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2852. begin
  2853. inherited ppuload(recorddef,ppufile);
  2854. if df_copied_def in defoptions then
  2855. ppufile.getderef(cloneddefderef)
  2856. else
  2857. begin
  2858. symtable:=trecordsymtable.create(objrealname^,0);
  2859. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2860. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2861. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2862. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2863. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  2864. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  2865. trecordsymtable(symtable).ppuload(ppufile);
  2866. { requires usefieldalignment to be set }
  2867. symtable.defowner:=self;
  2868. end;
  2869. isunion:=false;
  2870. end;
  2871. destructor trecorddef.destroy;
  2872. begin
  2873. if assigned(symtable) then
  2874. begin
  2875. symtable.free;
  2876. symtable:=nil;
  2877. end;
  2878. inherited destroy;
  2879. end;
  2880. function trecorddef.getcopy : tstoreddef;
  2881. begin
  2882. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2883. trecorddef(result).isunion:=isunion;
  2884. include(trecorddef(result).defoptions,df_copied_def);
  2885. if assigned(tcinitcode) then
  2886. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  2887. if assigned(import_lib) then
  2888. trecorddef(result).import_lib:=stringdup(import_lib^);
  2889. end;
  2890. function trecorddef.needs_inittable : boolean;
  2891. begin
  2892. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2893. end;
  2894. function trecorddef.is_related(d: tdef): boolean;
  2895. begin
  2896. { records are implemented via classes in the JVM target, and are
  2897. all descendents of the java_fpcbaserecordtype class }
  2898. is_related:=false;
  2899. if (target_info.system=system_jvm_java32) then
  2900. begin
  2901. if d.typ=objectdef then
  2902. begin
  2903. d:=find_real_class_definition(tobjectdef(d),false);
  2904. if (d=java_jlobject) or
  2905. (d=java_fpcbaserecordtype) then
  2906. is_related:=true
  2907. end;
  2908. end;
  2909. end;
  2910. procedure trecorddef.buildderef;
  2911. begin
  2912. inherited buildderef;
  2913. if df_copied_def in defoptions then
  2914. cloneddefderef.build(symtable.defowner)
  2915. else
  2916. tstoredsymtable(symtable).buildderef;
  2917. end;
  2918. procedure trecorddef.deref;
  2919. begin
  2920. inherited deref;
  2921. { now dereference the definitions }
  2922. if df_copied_def in defoptions then
  2923. begin
  2924. cloneddef:=trecorddef(cloneddefderef.resolve);
  2925. symtable:=cloneddef.symtable.getcopy;
  2926. end
  2927. else
  2928. tstoredsymtable(symtable).deref;
  2929. { assign TGUID? load only from system unit }
  2930. if not(assigned(rec_tguid)) and
  2931. (upper(typename)='TGUID') and
  2932. assigned(owner) and
  2933. assigned(owner.name) and
  2934. (owner.name^='SYSTEM') then
  2935. rec_tguid:=self;
  2936. { assign JMP_BUF? load only from system unit }
  2937. if not(assigned(rec_jmp_buf)) and
  2938. (upper(typename)='JMP_BUF') and
  2939. assigned(owner) and
  2940. assigned(owner.name) and
  2941. (owner.name^='SYSTEM') then
  2942. rec_jmp_buf:=self;
  2943. end;
  2944. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2945. begin
  2946. inherited ppuwrite(ppufile);
  2947. if df_copied_def in defoptions then
  2948. ppufile.putderef(cloneddefderef)
  2949. else
  2950. begin
  2951. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2952. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2953. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2954. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2955. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  2956. ppufile.putword(trecordsymtable(symtable).paddingsize);
  2957. end;
  2958. ppufile.writeentry(ibrecorddef);
  2959. if not(df_copied_def in defoptions) then
  2960. trecordsymtable(symtable).ppuwrite(ppufile);
  2961. end;
  2962. function trecorddef.size:asizeint;
  2963. begin
  2964. result:=trecordsymtable(symtable).datasize;
  2965. end;
  2966. function trecorddef.alignment:shortint;
  2967. begin
  2968. alignment:=trecordsymtable(symtable).recordalignment;
  2969. end;
  2970. function trecorddef.padalignment:shortint;
  2971. begin
  2972. padalignment := trecordsymtable(symtable).padalignment;
  2973. end;
  2974. function trecorddef.GetTypeName : string;
  2975. begin
  2976. GetTypeName:='<record type>'
  2977. end;
  2978. {***************************************************************************
  2979. TABSTRACTPROCDEF
  2980. ***************************************************************************}
  2981. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2982. begin
  2983. inherited create(dt);
  2984. parast:=tparasymtable.create(self,level);
  2985. paras:=nil;
  2986. minparacount:=0;
  2987. maxparacount:=0;
  2988. proctypeoption:=potype_none;
  2989. proccalloption:=pocall_none;
  2990. procoptions:=[];
  2991. returndef:=voidtype;
  2992. savesize:=sizeof(pint);
  2993. callerargareasize:=0;
  2994. calleeargareasize:=0;
  2995. has_paraloc_info:=callnoside;
  2996. funcretloc[callerside].init;
  2997. funcretloc[calleeside].init;
  2998. check_mark_as_nested;
  2999. end;
  3000. destructor tabstractprocdef.destroy;
  3001. begin
  3002. if assigned(paras) then
  3003. begin
  3004. {$ifdef MEMDEBUG}
  3005. memprocpara.start;
  3006. {$endif MEMDEBUG}
  3007. paras.free;
  3008. paras:=nil;
  3009. {$ifdef MEMDEBUG}
  3010. memprocpara.stop;
  3011. {$endif MEMDEBUG}
  3012. end;
  3013. if assigned(parast) then
  3014. begin
  3015. {$ifdef MEMDEBUG}
  3016. memprocparast.start;
  3017. {$endif MEMDEBUG}
  3018. parast.free;
  3019. parast:=nil;
  3020. {$ifdef MEMDEBUG}
  3021. memprocparast.stop;
  3022. {$endif MEMDEBUG}
  3023. end;
  3024. funcretloc[callerside].done;
  3025. funcretloc[calleeside].done;
  3026. inherited destroy;
  3027. end;
  3028. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3029. begin
  3030. if (tsym(p).typ<>paravarsym) then
  3031. exit;
  3032. inc(plongint(arg)^);
  3033. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3034. begin
  3035. if not assigned(tparavarsym(p).defaultconstsym) then
  3036. inc(minparacount);
  3037. inc(maxparacount);
  3038. end;
  3039. end;
  3040. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3041. begin
  3042. if (tsym(p).typ<>paravarsym) then
  3043. exit;
  3044. paras.add(p);
  3045. end;
  3046. procedure tabstractprocdef.calcparas;
  3047. var
  3048. paracount : longint;
  3049. begin
  3050. { This can already be assigned when
  3051. we need to reresolve this unit (PFV) }
  3052. if assigned(paras) then
  3053. paras.free;
  3054. paras:=tparalist.create(false);
  3055. paracount:=0;
  3056. minparacount:=0;
  3057. maxparacount:=0;
  3058. parast.SymList.ForEachCall(@count_para,@paracount);
  3059. paras.capacity:=paracount;
  3060. { Insert parameters in table }
  3061. parast.SymList.ForEachCall(@insert_para,nil);
  3062. { Order parameters }
  3063. paras.sortparas;
  3064. end;
  3065. procedure tabstractprocdef.buildderef;
  3066. begin
  3067. { released procdef? }
  3068. if not assigned(parast) then
  3069. exit;
  3070. inherited buildderef;
  3071. returndefderef.build(returndef);
  3072. if po_explicitparaloc in procoptions then
  3073. funcretloc[callerside].buildderef;
  3074. { parast }
  3075. tparasymtable(parast).buildderef;
  3076. end;
  3077. procedure tabstractprocdef.deref;
  3078. begin
  3079. inherited deref;
  3080. returndef:=tdef(returndefderef.resolve);
  3081. if po_explicitparaloc in procoptions then
  3082. begin
  3083. funcretloc[callerside].deref;
  3084. has_paraloc_info:=callerside;
  3085. end
  3086. else
  3087. begin
  3088. { deref is called after loading from a ppu, but also after another
  3089. unit has been reloaded/recompiled and all references must be
  3090. re-resolved. Since the funcretloc contains a reference to a tdef,
  3091. reset it so that we won't try to access the stale def }
  3092. funcretloc[callerside].init;
  3093. has_paraloc_info:=callnoside;
  3094. end;
  3095. { parast }
  3096. tparasymtable(parast).deref;
  3097. { recalculated parameters }
  3098. calcparas;
  3099. end;
  3100. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3101. begin
  3102. inherited ppuload(dt,ppufile);
  3103. parast:=nil;
  3104. Paras:=nil;
  3105. minparacount:=0;
  3106. maxparacount:=0;
  3107. ppufile.getderef(returndefderef);
  3108. { TODO: remove fpu_used loading}
  3109. ppufile.getbyte;
  3110. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3111. proccalloption:=tproccalloption(ppufile.getbyte);
  3112. ppufile.getnormalset(procoptions);
  3113. funcretloc[callerside].init;
  3114. if po_explicitparaloc in procoptions then
  3115. funcretloc[callerside].ppuload(ppufile);
  3116. savesize:=sizeof(pint);
  3117. if (po_explicitparaloc in procoptions) then
  3118. has_paraloc_info:=callerside;
  3119. end;
  3120. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3121. var
  3122. oldintfcrc : boolean;
  3123. begin
  3124. { released procdef? }
  3125. if not assigned(parast) then
  3126. exit;
  3127. inherited ppuwrite(ppufile);
  3128. ppufile.putderef(returndefderef);
  3129. oldintfcrc:=ppufile.do_interface_crc;
  3130. ppufile.do_interface_crc:=false;
  3131. ppufile.putbyte(0);
  3132. ppufile.putbyte(ord(proctypeoption));
  3133. ppufile.putbyte(ord(proccalloption));
  3134. ppufile.putnormalset(procoptions);
  3135. ppufile.do_interface_crc:=oldintfcrc;
  3136. if (po_explicitparaloc in procoptions) then
  3137. funcretloc[callerside].ppuwrite(ppufile);
  3138. end;
  3139. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3140. var
  3141. hs,s : ansistring;
  3142. hp : TParavarsym;
  3143. hpc : tconstsym;
  3144. first : boolean;
  3145. i : integer;
  3146. begin
  3147. s:='';
  3148. first:=true;
  3149. for i:=0 to paras.count-1 do
  3150. begin
  3151. hp:=tparavarsym(paras[i]);
  3152. if not(vo_is_hidden_para in hp.varoptions) or
  3153. (pno_showhidden in pno) then
  3154. begin
  3155. if first then
  3156. begin
  3157. s:=s+'(';
  3158. first:=false;
  3159. end
  3160. else
  3161. s:=s+';';
  3162. if vo_is_hidden_para in hp.varoptions then
  3163. s:=s+'<';
  3164. case hp.varspez of
  3165. vs_var :
  3166. s:=s+'var ';
  3167. vs_const :
  3168. s:=s+'const ';
  3169. vs_out :
  3170. s:=s+'out ';
  3171. vs_constref :
  3172. s:=s+'constref ';
  3173. end;
  3174. if (pno_paranames in pno) then
  3175. s:=s+hp.realname+':';
  3176. if hp.univpara then
  3177. s:=s+'univ ';
  3178. if assigned(hp.vardef.typesym) then
  3179. begin
  3180. hs:=hp.vardef.typesym.realname;
  3181. if hs[1]<>'$' then
  3182. s:=s+hp.vardef.OwnerHierarchyName+hs
  3183. else
  3184. s:=s+hp.vardef.GetTypeName;
  3185. end
  3186. else
  3187. s:=s+hp.vardef.GetTypeName;
  3188. { default value }
  3189. if assigned(hp.defaultconstsym) then
  3190. begin
  3191. hpc:=tconstsym(hp.defaultconstsym);
  3192. hs:='';
  3193. case hpc.consttyp of
  3194. conststring,
  3195. constresourcestring :
  3196. begin
  3197. If hpc.value.len>0 then
  3198. begin
  3199. setLength(hs,hpc.value.len);
  3200. { don't write past the end of hs if the constant
  3201. is > 255 chars }
  3202. move(hpc.value.valueptr^,hs[1],length(hs));
  3203. { make sure that constant strings with newline chars
  3204. don't create a linebreak in the assembler code,
  3205. since comments are line-based. Also remove nulls
  3206. because the comments are written as a pchar. }
  3207. ReplaceCase(hs,#0,'.');
  3208. ReplaceCase(hs,#10,'.');
  3209. ReplaceCase(hs,#13,'.');
  3210. end;
  3211. end;
  3212. constreal :
  3213. str(pbestreal(hpc.value.valueptr)^,hs);
  3214. constpointer :
  3215. hs:=tostr(hpc.value.valueordptr);
  3216. constord :
  3217. begin
  3218. if is_boolean(hpc.constdef) then
  3219. begin
  3220. if hpc.value.valueord<>0 then
  3221. hs:='TRUE'
  3222. else
  3223. hs:='FALSE';
  3224. end
  3225. else
  3226. hs:=tostr(hpc.value.valueord);
  3227. end;
  3228. constnil :
  3229. hs:='nil';
  3230. constset :
  3231. hs:='<set>';
  3232. end;
  3233. if hs<>'' then
  3234. s:=s+'="'+hs+'"';
  3235. end;
  3236. if vo_is_hidden_para in hp.varoptions then
  3237. s:=s+'>';
  3238. end;
  3239. end;
  3240. if not first then
  3241. s:=s+')';
  3242. if (po_varargs in procoptions) then
  3243. s:=s+';VarArgs';
  3244. typename_paras:=s;
  3245. end;
  3246. function tabstractprocdef.is_methodpointer:boolean;
  3247. begin
  3248. result:=false;
  3249. end;
  3250. function tabstractprocdef.is_addressonly:boolean;
  3251. begin
  3252. result:=true;
  3253. end;
  3254. function tabstractprocdef.no_self_node: boolean;
  3255. begin
  3256. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  3257. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  3258. end;
  3259. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  3260. var
  3261. j, nestinglevel: longint;
  3262. pvs, npvs: tparavarsym;
  3263. csym, ncsym: tconstsym;
  3264. begin
  3265. nestinglevel:=parast.symtablelevel;
  3266. if newtyp=procdef then
  3267. begin
  3268. if (copytyp<>pc_bareproc) then
  3269. result:=tprocdef.create(nestinglevel)
  3270. else
  3271. result:=tprocdef.create(normal_function_level);
  3272. tprocdef(result).visibility:=vis_public;
  3273. end
  3274. else
  3275. begin
  3276. result:=tprocvardef.create(nestinglevel);
  3277. end;
  3278. tabstractprocdef(result).returndef:=returndef;
  3279. tabstractprocdef(result).returndefderef:=returndefderef;
  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.