symdef.pas 242 KB

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