symdef.pas 238 KB

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