symdef.pas 242 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420
  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):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):string;override;
  385. function vmt_mangledname : 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):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) : string;
  1628. var
  1629. prefix : string[4];
  1630. begin
  1631. if (rt=fullrtti) or (not needs_separate_initrtti) then
  1632. begin
  1633. prefix:='RTTI';
  1634. include(defstates,ds_rtti_table_used);
  1635. end
  1636. else
  1637. begin
  1638. prefix:='INIT';
  1639. include(defstates,ds_init_table_used);
  1640. end;
  1641. if assigned(typesym) and
  1642. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1643. result:=make_mangledname(prefix,owner,typesym.name)
  1644. else
  1645. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1646. end;
  1647. function tstoreddef.OwnerHierarchyName: string;
  1648. var
  1649. tmp: tdef;
  1650. begin
  1651. tmp:=self;
  1652. result:='';
  1653. repeat
  1654. { can be not assigned in case of a forwarddef }
  1655. if assigned(tmp.owner) and
  1656. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1657. tmp:=tdef(tmp.owner.defowner)
  1658. else
  1659. break;
  1660. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1661. until tmp=nil;
  1662. end;
  1663. function tstoreddef.fullownerhierarchyname: string;
  1664. var
  1665. tmp: tdef;
  1666. begin
  1667. if assigned(_fullownerhierarchyname) then
  1668. begin
  1669. result:=_fullownerhierarchyname^;
  1670. exit;
  1671. end;
  1672. { the def can only reside inside structured types or
  1673. procedures/functions/methods }
  1674. tmp:=self;
  1675. result:='';
  1676. repeat
  1677. { can be not assigned in case of a forwarddef }
  1678. if not assigned(tmp.owner) then
  1679. break
  1680. else
  1681. tmp:=tdef(tmp.owner.defowner);
  1682. if not assigned(tmp) then
  1683. break;
  1684. if tmp.typ in [recorddef,objectdef] then
  1685. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  1686. else
  1687. if tmp.typ=procdef then
  1688. result:=tprocdef(tmp).customprocname([pno_paranames,pno_proctypeoption])+'.'+result;
  1689. until tmp=nil;
  1690. _fullownerhierarchyname:=stringdup(result);
  1691. end;
  1692. function tstoreddef.in_currentunit: boolean;
  1693. var
  1694. st: tsymtable;
  1695. begin
  1696. st:=owner;
  1697. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1698. st:=st.defowner.owner;
  1699. result:=st.iscurrentunit;
  1700. end;
  1701. function tstoreddef.getcopy : tstoreddef;
  1702. begin
  1703. Message(sym_e_cant_create_unique_type);
  1704. getcopy:=cerrordef.create;
  1705. end;
  1706. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1707. var
  1708. sizeleft,i : longint;
  1709. buf : array[0..255] of byte;
  1710. oldintfcrc : boolean;
  1711. begin
  1712. ppufile.putlongint(DefId);
  1713. ppufile.putderef(typesymderef);
  1714. ppufile.putsmallset(defoptions);
  1715. oldintfcrc:=ppufile.do_crc;
  1716. ppufile.do_crc:=false;
  1717. ppufile.putsmallset(defstates);
  1718. if df_genconstraint in defoptions then
  1719. genconstraintdata.ppuwrite(ppufile);
  1720. if [df_generic,df_specialization]*defoptions<>[] then
  1721. begin
  1722. if not assigned(genericparas) then
  1723. ppufile.putlongint(0)
  1724. else
  1725. begin
  1726. if not assigned(genericparaderefs) then
  1727. internalerror(2014052305);
  1728. ppufile.putlongint(genericparas.count);
  1729. for i:=0 to genericparas.count-1 do
  1730. begin
  1731. ppufile.putstring(genericparas.nameofindex(i));
  1732. ppufile.putderef(pderef(genericparaderefs[i])^);
  1733. end;
  1734. end;
  1735. end;
  1736. if df_generic in defoptions then
  1737. begin
  1738. if assigned(generictokenbuf) then
  1739. begin
  1740. sizeleft:=generictokenbuf.size;
  1741. generictokenbuf.seek(0);
  1742. end
  1743. else
  1744. sizeleft:=0;
  1745. ppufile.putlongint(sizeleft);
  1746. while sizeleft>0 do
  1747. begin
  1748. if sizeleft>sizeof(buf) then
  1749. i:=sizeof(buf)
  1750. else
  1751. i:=sizeleft;
  1752. generictokenbuf.read(buf,i);
  1753. ppufile.putdata(buf,i);
  1754. dec(sizeleft,i);
  1755. end;
  1756. end;
  1757. ppufile.do_crc:=oldintfcrc;
  1758. if df_specialization in defoptions then
  1759. ppufile.putderef(genericdefderef);
  1760. end;
  1761. procedure tstoreddef.buildderef;
  1762. var
  1763. i : longint;
  1764. sym : tsym;
  1765. symderef : pderef;
  1766. begin
  1767. typesymderef.build(typesym);
  1768. genericdefderef.build(genericdef);
  1769. if assigned(genconstraintdata) then
  1770. genconstraintdata.buildderef;
  1771. if assigned(genericparas) then
  1772. begin
  1773. if not assigned(genericparaderefs) then
  1774. genericparaderefs:=tfplist.create;
  1775. for i:=0 to genericparas.count-1 do
  1776. begin
  1777. sym:=tsym(genericparas.items[i]);
  1778. new(symderef);
  1779. symderef^.build(sym);
  1780. genericparaderefs.add(symderef);
  1781. end;
  1782. end;
  1783. end;
  1784. procedure tstoreddef.buildderefimpl;
  1785. begin
  1786. end;
  1787. procedure tstoreddef.deref;
  1788. var
  1789. symderef : pderef;
  1790. i : longint;
  1791. begin
  1792. typesym:=ttypesym(typesymderef.resolve);
  1793. if df_specialization in defoptions then
  1794. genericdef:=tstoreddef(genericdefderef.resolve);
  1795. if assigned(genconstraintdata) then
  1796. genconstraintdata.deref;
  1797. if assigned(genericparas) then
  1798. begin
  1799. if not assigned(genericparaderefs) then
  1800. internalerror(2014052302);
  1801. if genericparas.count<>genericparaderefs.count then
  1802. internalerror(2014052303);
  1803. for i:=0 to genericparaderefs.count-1 do
  1804. begin
  1805. symderef:=pderef(genericparaderefs[i]);
  1806. genericparas.items[i]:=symderef^.resolve;
  1807. dispose(symderef);
  1808. end;
  1809. genericparaderefs.free;
  1810. genericparaderefs:=nil;
  1811. end;
  1812. end;
  1813. procedure tstoreddef.derefimpl;
  1814. begin
  1815. end;
  1816. function tstoreddef.size : asizeint;
  1817. begin
  1818. size:=savesize;
  1819. end;
  1820. function tstoreddef.getvardef:longint;
  1821. begin
  1822. result:=varUndefined;
  1823. end;
  1824. function tstoreddef.alignment : shortint;
  1825. begin
  1826. { natural alignment by default }
  1827. alignment:=size_2_align(savesize);
  1828. { can happen if savesize = 0, e.g. for voiddef or
  1829. an empty record
  1830. }
  1831. if (alignment=0) then
  1832. alignment:=1;
  1833. end;
  1834. { returns true, if the definition can be published }
  1835. function tstoreddef.is_publishable : boolean;
  1836. begin
  1837. is_publishable:=false;
  1838. end;
  1839. { needs an init table }
  1840. function tstoreddef.needs_inittable : boolean;
  1841. begin
  1842. needs_inittable:=false;
  1843. end;
  1844. function tstoreddef.is_intregable : boolean;
  1845. var
  1846. recsize,temp: longint;
  1847. begin
  1848. is_intregable:=false;
  1849. case typ of
  1850. orddef,
  1851. pointerdef,
  1852. enumdef,
  1853. classrefdef:
  1854. is_intregable:=true;
  1855. procvardef :
  1856. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  1857. objectdef:
  1858. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1859. setdef:
  1860. is_intregable:=is_smallset(self);
  1861. recorddef:
  1862. begin
  1863. recsize:=size;
  1864. is_intregable:=
  1865. ispowerof2(recsize,temp) and
  1866. { sizeof(asizeint)*2 records in int registers is currently broken for endian_big targets }
  1867. (((recsize <= sizeof(asizeint)*2) and (target_info.endian=endian_little)
  1868. { records cannot go into registers on 16 bit targets for now }
  1869. and (sizeof(asizeint)>2)
  1870. and not trecorddef(self).contains_float_field) or
  1871. (recsize <= sizeof(asizeint)))
  1872. and not needs_inittable;
  1873. end;
  1874. end;
  1875. end;
  1876. function tstoreddef.is_fpuregable : boolean;
  1877. begin
  1878. {$ifdef x86}
  1879. result:=use_vectorfpu(self);
  1880. {$else x86}
  1881. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1882. {$endif x86}
  1883. end;
  1884. function tstoreddef.is_const_intregable : boolean;
  1885. begin
  1886. case typ of
  1887. stringdef:
  1888. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  1889. arraydef:
  1890. result:=is_dynamic_array(self);
  1891. objectdef:
  1892. result:=is_interface(self);
  1893. else
  1894. result:=false;
  1895. end;
  1896. end;
  1897. procedure tstoreddef.initgeneric;
  1898. begin
  1899. if assigned(generictokenbuf) then
  1900. internalerror(200512131);
  1901. generictokenbuf:=tdynamicarray.create(256);
  1902. end;
  1903. function tstoreddef.is_generic: boolean;
  1904. var
  1905. sym: tsym;
  1906. i: longint;
  1907. begin
  1908. result:=assigned(genericparas) and
  1909. (genericparas.count>0) and
  1910. (df_generic in defoptions);
  1911. if result then
  1912. { if any of the type parameters does *not* belong to as (meaning it was passed
  1913. in from outside) then we aren't a generic, but a specialization }
  1914. for i:=0 to genericparas.count-1 do
  1915. begin
  1916. sym:=tsym(genericparas[i]);
  1917. if sym.typ<>symconst.typesym then
  1918. internalerror(2014050903);
  1919. if sym.owner.defowner<>self then
  1920. exit(false);
  1921. end;
  1922. end;
  1923. function tstoreddef.is_specialization: boolean;
  1924. var
  1925. i : longint;
  1926. sym : tsym;
  1927. begin
  1928. result:=assigned(genericparas) and
  1929. (genericparas.count>0) and
  1930. (df_specialization in defoptions);
  1931. if result then
  1932. begin
  1933. { if at least one of the generic parameters is not owned by us (meaning it was
  1934. passed in from outside) then we have a specialization, otherwise we have a generic }
  1935. for i:=0 to genericparas.count-1 do
  1936. begin
  1937. sym:=tsym(genericparas[i]);
  1938. if sym.typ<>symconst.typesym then
  1939. internalerror(2014050904);
  1940. if sym.owner.defowner=self then
  1941. exit(true);
  1942. end;
  1943. result:=false;
  1944. end;
  1945. end;
  1946. {****************************************************************************
  1947. Tstringdef
  1948. ****************************************************************************}
  1949. constructor tstringdef.createshort(l : byte);
  1950. begin
  1951. inherited create(stringdef);
  1952. stringtype:=st_shortstring;
  1953. encoding:=0;
  1954. len:=l;
  1955. end;
  1956. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1957. begin
  1958. inherited ppuload(stringdef,ppufile);
  1959. stringtype:=st_shortstring;
  1960. encoding:=0;
  1961. len:=ppufile.getbyte;
  1962. ppuload_platform(ppufile);
  1963. end;
  1964. constructor tstringdef.createlong(l : asizeint);
  1965. begin
  1966. inherited create(stringdef);
  1967. stringtype:=st_longstring;
  1968. encoding:=0;
  1969. len:=l;
  1970. end;
  1971. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1972. begin
  1973. inherited ppuload(stringdef,ppufile);
  1974. stringtype:=st_longstring;
  1975. encoding:=0;
  1976. len:=ppufile.getasizeint;
  1977. ppuload_platform(ppufile);
  1978. end;
  1979. constructor tstringdef.createansi(aencoding:tstringencoding);
  1980. begin
  1981. inherited create(stringdef);
  1982. stringtype:=st_ansistring;
  1983. encoding:=aencoding;
  1984. len:=-1;
  1985. end;
  1986. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1987. begin
  1988. inherited ppuload(stringdef,ppufile);
  1989. stringtype:=st_ansistring;
  1990. len:=ppufile.getaint;
  1991. encoding:=ppufile.getword;
  1992. ppuload_platform(ppufile);
  1993. end;
  1994. constructor tstringdef.createwide;
  1995. begin
  1996. inherited create(stringdef);
  1997. stringtype:=st_widestring;
  1998. if target_info.endian=endian_little then
  1999. encoding:=CP_UTF16LE
  2000. else
  2001. encoding:=CP_UTF16BE;
  2002. len:=-1;
  2003. end;
  2004. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2005. begin
  2006. inherited ppuload(stringdef,ppufile);
  2007. stringtype:=st_widestring;
  2008. if target_info.endian=endian_little then
  2009. encoding:=CP_UTF16LE
  2010. else
  2011. encoding:=CP_UTF16BE;
  2012. len:=ppufile.getaint;
  2013. ppuload_platform(ppufile);
  2014. end;
  2015. constructor tstringdef.createunicode;
  2016. begin
  2017. inherited create(stringdef);
  2018. stringtype:=st_unicodestring;
  2019. if target_info.endian=endian_little then
  2020. encoding:=CP_UTF16LE
  2021. else
  2022. encoding:=CP_UTF16BE;
  2023. len:=-1;
  2024. end;
  2025. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2026. begin
  2027. inherited ppuload(stringdef,ppufile);
  2028. stringtype:=st_unicodestring;
  2029. len:=ppufile.getaint;
  2030. encoding:=ppufile.getword;
  2031. ppuload_platform(ppufile);
  2032. end;
  2033. function tstringdef.getcopy : tstoreddef;
  2034. begin
  2035. result:=cstringdef.create(typ);
  2036. result.typ:=stringdef;
  2037. tstringdef(result).stringtype:=stringtype;
  2038. tstringdef(result).encoding:=encoding;
  2039. tstringdef(result).len:=len;
  2040. end;
  2041. function tstringdef.stringtypname:string;
  2042. const
  2043. typname:array[tstringtype] of string[10]=(
  2044. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2045. );
  2046. begin
  2047. stringtypname:=typname[stringtype];
  2048. end;
  2049. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2050. begin
  2051. inherited ppuwrite(ppufile);
  2052. if stringtype=st_shortstring then
  2053. begin
  2054. {$ifdef extdebug}
  2055. if len > 255 then internalerror(12122002);
  2056. {$endif}
  2057. ppufile.putbyte(byte(len))
  2058. end
  2059. else
  2060. ppufile.putaint(len);
  2061. if stringtype in [st_ansistring,st_unicodestring] then
  2062. ppufile.putword(encoding);
  2063. case stringtype of
  2064. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2065. st_longstring : writeentry(ppufile,iblongstringdef);
  2066. st_ansistring : writeentry(ppufile,ibansistringdef);
  2067. st_widestring : writeentry(ppufile,ibwidestringdef);
  2068. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2069. end;
  2070. end;
  2071. function tstringdef.needs_inittable : boolean;
  2072. begin
  2073. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2074. end;
  2075. function tstringdef.GetTypeName : string;
  2076. const
  2077. names : array[tstringtype] of string[15] = (
  2078. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2079. begin
  2080. GetTypeName:=names[stringtype];
  2081. end;
  2082. function tstringdef.getvardef : longint;
  2083. const
  2084. vardef : array[tstringtype] of longint = (
  2085. varUndefined,varUndefined,varString,varOleStr,varUString);
  2086. begin
  2087. result:=vardef[stringtype];
  2088. end;
  2089. function tstringdef.alignment : shortint;
  2090. begin
  2091. case stringtype of
  2092. st_unicodestring,
  2093. st_widestring,
  2094. st_ansistring:
  2095. alignment:=size_2_align(size);
  2096. st_longstring,
  2097. st_shortstring:
  2098. { char to string accesses byte 0 and 1 with one word access }
  2099. if (tf_requires_proper_alignment in target_info.flags) or
  2100. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2101. (m_mac in current_settings.modeswitches) then
  2102. alignment:=size_2_align(2)
  2103. else
  2104. alignment:=size_2_align(1);
  2105. else
  2106. internalerror(200412301);
  2107. end;
  2108. end;
  2109. function tstringdef.getmangledparaname : TSymStr;
  2110. begin
  2111. getmangledparaname:='STRING';
  2112. end;
  2113. function tstringdef.is_publishable : boolean;
  2114. begin
  2115. is_publishable:=true;
  2116. end;
  2117. function tstringdef.size: asizeint;
  2118. begin
  2119. case stringtype of
  2120. st_shortstring:
  2121. Result:=len+1;
  2122. st_longstring,
  2123. st_ansistring,
  2124. st_widestring,
  2125. st_unicodestring:
  2126. Result:=voidpointertype.size;
  2127. else
  2128. internalerror(2014032301);
  2129. end;
  2130. end;
  2131. {****************************************************************************
  2132. TENUMDEF
  2133. ****************************************************************************}
  2134. constructor tenumdef.create;
  2135. begin
  2136. inherited create(enumdef);
  2137. minval:=0;
  2138. maxval:=0;
  2139. calcsavesize;
  2140. has_jumps:=false;
  2141. basedef:=nil;
  2142. symtable:=tenumsymtable.create(self);
  2143. end;
  2144. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2145. begin
  2146. inherited create(enumdef);
  2147. minval:=_min;
  2148. maxval:=_max;
  2149. basedef:=_basedef;
  2150. calcsavesize;
  2151. has_jumps:=false;
  2152. symtable:=basedef.symtable.getcopy;
  2153. include(defoptions, df_copied_def);
  2154. end;
  2155. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2156. begin
  2157. inherited ppuload(enumdef,ppufile);
  2158. minval:=ppufile.getaint;
  2159. maxval:=ppufile.getaint;
  2160. savesize:=ppufile.getaint;
  2161. has_jumps:=false;
  2162. if df_copied_def in defoptions then
  2163. begin
  2164. symtable:=nil;
  2165. ppufile.getderef(basedefderef);
  2166. ppuload_platform(ppufile);
  2167. end
  2168. else
  2169. begin
  2170. ppuload_platform(ppufile);
  2171. // create with nil defowner first to prevent values changes on insert
  2172. symtable:=tenumsymtable.create(nil);
  2173. tenumsymtable(symtable).ppuload(ppufile);
  2174. symtable.defowner:=self;
  2175. end;
  2176. end;
  2177. destructor tenumdef.destroy;
  2178. begin
  2179. symtable.free;
  2180. symtable:=nil;
  2181. inherited destroy;
  2182. end;
  2183. function tenumdef.getcopy : tstoreddef;
  2184. begin
  2185. if assigned(basedef) then
  2186. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2187. else
  2188. begin
  2189. result:=cenumdef.create;
  2190. tenumdef(result).minval:=minval;
  2191. tenumdef(result).maxval:=maxval;
  2192. tenumdef(result).symtable.free;
  2193. tenumdef(result).symtable:=symtable.getcopy;
  2194. tenumdef(result).basedef:=self;
  2195. end;
  2196. tenumdef(result).has_jumps:=has_jumps;
  2197. tenumdef(result).basedefderef:=basedefderef;
  2198. include(tenumdef(result).defoptions,df_copied_def);
  2199. end;
  2200. procedure tenumdef.calcsavesize;
  2201. begin
  2202. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2203. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  2204. savesize:=8
  2205. {$IFDEF not cpu64bitaddr} {$pop} {$ENDIF}
  2206. else
  2207. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2208. savesize:=4
  2209. else
  2210. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2211. savesize:=2
  2212. else
  2213. savesize:=1;
  2214. end;
  2215. function tenumdef.packedbitsize: asizeint;
  2216. var
  2217. sizeval: tconstexprint;
  2218. power: longint;
  2219. begin
  2220. result := 0;
  2221. if (minval >= 0) and
  2222. (maxval <= 1) then
  2223. result := 1
  2224. else
  2225. begin
  2226. if (minval>=0) then
  2227. sizeval:=maxval
  2228. else
  2229. { don't count 0 twice }
  2230. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  2231. { 256 must become 512 etc. }
  2232. nextpowerof2(sizeval+1,power);
  2233. result := power;
  2234. end;
  2235. end;
  2236. procedure tenumdef.setmax(_max:asizeint);
  2237. begin
  2238. maxval:=_max;
  2239. calcsavesize;
  2240. end;
  2241. procedure tenumdef.setmin(_min:asizeint);
  2242. begin
  2243. minval:=_min;
  2244. calcsavesize;
  2245. end;
  2246. function tenumdef.min:asizeint;
  2247. begin
  2248. min:=minval;
  2249. end;
  2250. function tenumdef.max:asizeint;
  2251. begin
  2252. max:=maxval;
  2253. end;
  2254. function tenumdef.getfirstsym: tsym;
  2255. var
  2256. i:integer;
  2257. begin
  2258. for i := 0 to symtable.SymList.Count - 1 do
  2259. begin
  2260. result:=tsym(symtable.SymList[i]);
  2261. if tenumsym(result).value=minval then
  2262. exit;
  2263. end;
  2264. result:=nil;
  2265. end;
  2266. function tenumdef.int2enumsym(l: asizeint): tsym;
  2267. var
  2268. i: longint;
  2269. sym: tsym;
  2270. bdef: tenumdef;
  2271. begin
  2272. result:=nil;
  2273. if (l<minval) or
  2274. (l>maxval) then
  2275. exit;
  2276. bdef:=getbasedef;
  2277. for i:=0 to bdef.symtable.symlist.count-1 do
  2278. begin
  2279. sym:=tsym(bdef.symtable.symlist[i]);
  2280. if (sym.typ=enumsym) and
  2281. (tenumsym(sym).value=l) then
  2282. begin
  2283. result:=sym;
  2284. exit;
  2285. end;
  2286. end;
  2287. end;
  2288. function tenumdef.getbasedef: tenumdef;
  2289. begin
  2290. if not assigned(basedef) then
  2291. result:=self
  2292. else
  2293. result:=basedef;
  2294. end;
  2295. procedure tenumdef.buildderef;
  2296. begin
  2297. inherited buildderef;
  2298. if df_copied_def in defoptions then
  2299. basedefderef.build(basedef)
  2300. else
  2301. tenumsymtable(symtable).buildderef;
  2302. end;
  2303. procedure tenumdef.deref;
  2304. begin
  2305. inherited deref;
  2306. if df_copied_def in defoptions then
  2307. begin
  2308. basedef:=tenumdef(basedefderef.resolve);
  2309. symtable:=basedef.symtable.getcopy;
  2310. end
  2311. else
  2312. tenumsymtable(symtable).deref;
  2313. end;
  2314. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2315. begin
  2316. inherited ppuwrite(ppufile);
  2317. ppufile.putaint(min);
  2318. ppufile.putaint(max);
  2319. ppufile.putaint(savesize);
  2320. if df_copied_def in defoptions then
  2321. ppufile.putderef(basedefderef);
  2322. writeentry(ppufile,ibenumdef);
  2323. if not (df_copied_def in defoptions) then
  2324. tenumsymtable(symtable).ppuwrite(ppufile);
  2325. end;
  2326. function tenumdef.is_publishable : boolean;
  2327. begin
  2328. is_publishable:=true;
  2329. end;
  2330. function tenumdef.GetTypeName : string;
  2331. begin
  2332. GetTypeName:='<enumeration type>';
  2333. end;
  2334. {****************************************************************************
  2335. TORDDEF
  2336. ****************************************************************************}
  2337. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  2338. begin
  2339. inherited create(orddef);
  2340. low:=v;
  2341. high:=b;
  2342. ordtype:=t;
  2343. setsize;
  2344. end;
  2345. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2346. begin
  2347. inherited ppuload(orddef,ppufile);
  2348. ordtype:=tordtype(ppufile.getbyte);
  2349. low:=ppufile.getexprint;
  2350. high:=ppufile.getexprint;
  2351. setsize;
  2352. ppuload_platform(ppufile);
  2353. end;
  2354. function torddef.getcopy : tstoreddef;
  2355. begin
  2356. result:=corddef.create(ordtype,low,high);
  2357. result.typ:=orddef;
  2358. torddef(result).low:=low;
  2359. torddef(result).high:=high;
  2360. torddef(result).ordtype:=ordtype;
  2361. torddef(result).savesize:=savesize;
  2362. end;
  2363. function torddef.alignment:shortint;
  2364. begin
  2365. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  2366. (ordtype in [s64bit,u64bit]) then
  2367. result := 4
  2368. else
  2369. result := inherited alignment;
  2370. end;
  2371. procedure torddef.setsize;
  2372. const
  2373. sizetbl : array[tordtype] of longint = (
  2374. 0,
  2375. 1,2,4,8,
  2376. 1,2,4,8,
  2377. 1,2,4,8,
  2378. 1,2,4,8,
  2379. 1,2,8
  2380. );
  2381. begin
  2382. savesize:=sizetbl[ordtype];
  2383. end;
  2384. function torddef.packedbitsize: asizeint;
  2385. var
  2386. sizeval: tconstexprint;
  2387. power: longint;
  2388. begin
  2389. result := 0;
  2390. if ordtype = uvoid then
  2391. exit;
  2392. {$ifndef cpu64bitalu}
  2393. if (ordtype in [s64bit,u64bit]) then
  2394. {$else not cpu64bitalu}
  2395. if (ordtype = u64bit) or
  2396. ((ordtype = s64bit) and
  2397. ((low <= (system.low(int64) div 2)) or
  2398. (high > (system.high(int64) div 2)))) then
  2399. {$endif cpu64bitalu}
  2400. result := 64
  2401. else if (low >= 0) and
  2402. (high <= 1) then
  2403. result := 1
  2404. else
  2405. begin
  2406. if (low>=0) then
  2407. sizeval:=high
  2408. else
  2409. { don't count 0 twice }
  2410. sizeval:=(cutils.max(-low,high)*2)-1;
  2411. { 256 must become 512 etc. }
  2412. nextpowerof2(sizeval+1,power);
  2413. result := power;
  2414. end;
  2415. end;
  2416. function torddef.getvardef : longint;
  2417. const
  2418. basetype2vardef : array[tordtype] of longint = (
  2419. varUndefined,
  2420. varbyte,varword,varlongword,varqword,
  2421. varshortint,varsmallint,varinteger,varint64,
  2422. varboolean,varboolean,varboolean,varboolean,
  2423. varboolean,varboolean,varUndefined,varUndefined,
  2424. varUndefined,varUndefined,varCurrency);
  2425. begin
  2426. result:=basetype2vardef[ordtype];
  2427. end;
  2428. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2429. begin
  2430. inherited ppuwrite(ppufile);
  2431. ppufile.putbyte(byte(ordtype));
  2432. ppufile.putexprint(low);
  2433. ppufile.putexprint(high);
  2434. writeentry(ppufile,iborddef);
  2435. end;
  2436. function torddef.is_publishable : boolean;
  2437. begin
  2438. is_publishable:=(ordtype<>uvoid);
  2439. end;
  2440. function torddef.GetTypeName : string;
  2441. const
  2442. names : array[tordtype] of string[20] = (
  2443. 'untyped',
  2444. 'Byte','Word','DWord','QWord',
  2445. 'ShortInt','SmallInt','LongInt','Int64',
  2446. 'Boolean','Boolean16','Boolean32','Boolean64',
  2447. 'ByteBool','WordBool','LongBool','QWordBool',
  2448. 'Char','WideChar','Currency');
  2449. begin
  2450. GetTypeName:=names[ordtype];
  2451. end;
  2452. {****************************************************************************
  2453. TFLOATDEF
  2454. ****************************************************************************}
  2455. constructor tfloatdef.create(t : tfloattype);
  2456. begin
  2457. inherited create(floatdef);
  2458. floattype:=t;
  2459. setsize;
  2460. end;
  2461. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2462. begin
  2463. inherited ppuload(floatdef,ppufile);
  2464. floattype:=tfloattype(ppufile.getbyte);
  2465. setsize;
  2466. ppuload_platform(ppufile);
  2467. end;
  2468. function tfloatdef.getcopy : tstoreddef;
  2469. begin
  2470. result:=cfloatdef.create(floattype);
  2471. result.typ:=floatdef;
  2472. tfloatdef(result).savesize:=savesize;
  2473. end;
  2474. function tfloatdef.alignment:shortint;
  2475. begin
  2476. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2477. case floattype of
  2478. sc80real,
  2479. s80real: result:=16;
  2480. s64real,
  2481. s64currency,
  2482. s64comp : result:=4;
  2483. else
  2484. result := inherited alignment;
  2485. end
  2486. else
  2487. result := inherited alignment;
  2488. end;
  2489. function tfloatdef.structalignment: shortint;
  2490. begin
  2491. { aix is really annoying: the recommended scalar alignment for both
  2492. int64 and double is 64 bits, but in structs int64 has to be aligned
  2493. to 8 bytes and double to 4 bytes }
  2494. if (target_info.system in systems_aix) and
  2495. (floattype=s64real) then
  2496. result:=4
  2497. else
  2498. result:=alignment;
  2499. end;
  2500. procedure tfloatdef.setsize;
  2501. begin
  2502. case floattype of
  2503. s32real : savesize:=4;
  2504. s80real : savesize:=10;
  2505. sc80real:
  2506. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  2507. system_x86_64_linux,system_x86_64_freebsd,
  2508. system_x86_64_openbsd,system_x86_64_netbsd,
  2509. system_x86_64_solaris,system_x86_64_embedded] then
  2510. savesize:=16
  2511. else
  2512. savesize:=12;
  2513. s64real,
  2514. s64currency,
  2515. s64comp : savesize:=8;
  2516. else
  2517. savesize:=0;
  2518. end;
  2519. end;
  2520. function tfloatdef.getvardef : longint;
  2521. const
  2522. floattype2vardef : array[tfloattype] of longint = (
  2523. varSingle,varDouble,varUndefined,varUndefined,
  2524. varUndefined,varCurrency,varUndefined);
  2525. begin
  2526. if (upper(typename)='TDATETIME') and
  2527. assigned(owner) and
  2528. assigned(owner.name) and
  2529. (owner.name^='SYSTEM') then
  2530. result:=varDate
  2531. else
  2532. result:=floattype2vardef[floattype];
  2533. end;
  2534. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2535. begin
  2536. inherited ppuwrite(ppufile);
  2537. ppufile.putbyte(byte(floattype));
  2538. writeentry(ppufile,ibfloatdef);
  2539. end;
  2540. function tfloatdef.is_publishable : boolean;
  2541. begin
  2542. is_publishable:=true;
  2543. end;
  2544. function tfloatdef.GetTypeName : string;
  2545. const
  2546. names : array[tfloattype] of string[20] = (
  2547. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2548. begin
  2549. GetTypeName:=names[floattype];
  2550. end;
  2551. {****************************************************************************
  2552. TFILEDEF
  2553. ****************************************************************************}
  2554. constructor tfiledef.createtext;
  2555. begin
  2556. inherited create(filedef);
  2557. filetyp:=ft_text;
  2558. typedfiledef:=nil;
  2559. end;
  2560. constructor tfiledef.createuntyped;
  2561. begin
  2562. inherited create(filedef);
  2563. filetyp:=ft_untyped;
  2564. typedfiledef:=nil;
  2565. end;
  2566. constructor tfiledef.createtyped(def:tdef);
  2567. begin
  2568. inherited create(filedef);
  2569. filetyp:=ft_typed;
  2570. typedfiledef:=def;
  2571. end;
  2572. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2573. begin
  2574. inherited ppuload(filedef,ppufile);
  2575. filetyp:=tfiletyp(ppufile.getbyte);
  2576. if filetyp=ft_typed then
  2577. ppufile.getderef(typedfiledefderef)
  2578. else
  2579. typedfiledef:=nil;
  2580. ppuload_platform(ppufile);
  2581. end;
  2582. function tfiledef.getcopy : tstoreddef;
  2583. begin
  2584. case filetyp of
  2585. ft_typed:
  2586. result:=cfiledef.createtyped(typedfiledef);
  2587. ft_untyped:
  2588. result:=cfiledef.createuntyped;
  2589. ft_text:
  2590. result:=cfiledef.createtext;
  2591. else
  2592. internalerror(2004121201);
  2593. end;
  2594. end;
  2595. procedure tfiledef.buildderef;
  2596. begin
  2597. inherited buildderef;
  2598. if filetyp=ft_typed then
  2599. typedfiledefderef.build(typedfiledef);
  2600. end;
  2601. procedure tfiledef.deref;
  2602. begin
  2603. inherited deref;
  2604. if filetyp=ft_typed then
  2605. typedfiledef:=tdef(typedfiledefderef.resolve);
  2606. end;
  2607. function tfiledef.size:asizeint;
  2608. begin
  2609. if savesize=0 then
  2610. setsize;
  2611. size:=savesize;
  2612. end;
  2613. procedure tfiledef.setsize;
  2614. begin
  2615. case filetyp of
  2616. ft_text:
  2617. savesize:=search_system_type('TEXTREC').typedef.size;
  2618. ft_typed:
  2619. begin
  2620. savesize:=search_system_type('FILEREC').typedef.size;
  2621. { allocate put/get buffer in iso mode }
  2622. if m_iso in current_settings.modeswitches then
  2623. inc(savesize,typedfiledef.size);
  2624. end;
  2625. ft_untyped:
  2626. savesize:=search_system_type('FILEREC').typedef.size;
  2627. else
  2628. internalerror(2013113001);
  2629. end;
  2630. end;
  2631. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2632. begin
  2633. inherited ppuwrite(ppufile);
  2634. ppufile.putbyte(byte(filetyp));
  2635. if filetyp=ft_typed then
  2636. ppufile.putderef(typedfiledefderef);
  2637. writeentry(ppufile,ibfiledef);
  2638. end;
  2639. function tfiledef.GetTypeName : string;
  2640. begin
  2641. case filetyp of
  2642. ft_untyped:
  2643. GetTypeName:='File';
  2644. ft_typed:
  2645. GetTypeName:='File Of '+typedfiledef.typename;
  2646. ft_text:
  2647. GetTypeName:='Text'
  2648. else
  2649. internalerror(2013113002);
  2650. end;
  2651. end;
  2652. function tfiledef.getmangledparaname : TSymStr;
  2653. begin
  2654. case filetyp of
  2655. ft_untyped:
  2656. getmangledparaname:='FILE';
  2657. ft_typed:
  2658. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2659. ft_text:
  2660. getmangledparaname:='TEXT'
  2661. else
  2662. internalerror(2013113003);
  2663. end;
  2664. end;
  2665. {****************************************************************************
  2666. TVARIANTDEF
  2667. ****************************************************************************}
  2668. constructor tvariantdef.create(v : tvarianttype);
  2669. begin
  2670. inherited create(variantdef);
  2671. varianttype:=v;
  2672. setsize;
  2673. end;
  2674. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2675. begin
  2676. inherited ppuload(variantdef,ppufile);
  2677. varianttype:=tvarianttype(ppufile.getbyte);
  2678. setsize;
  2679. ppuload_platform(ppufile);
  2680. end;
  2681. function tvariantdef.getcopy : tstoreddef;
  2682. begin
  2683. result:=cvariantdef.create(varianttype);
  2684. end;
  2685. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2686. begin
  2687. inherited ppuwrite(ppufile);
  2688. ppufile.putbyte(byte(varianttype));
  2689. writeentry(ppufile,ibvariantdef);
  2690. end;
  2691. function tvariantdef.getvardef : longint;
  2692. begin
  2693. Result:=varVariant;
  2694. end;
  2695. procedure tvariantdef.setsize;
  2696. begin
  2697. {$ifdef cpu64bitaddr}
  2698. savesize:=24;
  2699. {$else cpu64bitaddr}
  2700. savesize:=16;
  2701. {$endif cpu64bitaddr}
  2702. end;
  2703. function tvariantdef.GetTypeName : string;
  2704. begin
  2705. case varianttype of
  2706. vt_normalvariant:
  2707. GetTypeName:='Variant';
  2708. vt_olevariant:
  2709. GetTypeName:='OleVariant';
  2710. else
  2711. internalerror(2013113004);
  2712. end;
  2713. end;
  2714. function tvariantdef.needs_inittable : boolean;
  2715. begin
  2716. needs_inittable:=true;
  2717. end;
  2718. function tvariantdef.is_publishable : boolean;
  2719. begin
  2720. is_publishable:=true;
  2721. end;
  2722. {****************************************************************************
  2723. TABSTRACtpointerdef
  2724. ****************************************************************************}
  2725. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2726. begin
  2727. inherited create(dt);
  2728. pointeddef:=def;
  2729. end;
  2730. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2731. begin
  2732. inherited ppuload(dt,ppufile);
  2733. ppufile.getderef(pointeddefderef);
  2734. end;
  2735. procedure tabstractpointerdef.buildderef;
  2736. begin
  2737. inherited buildderef;
  2738. pointeddefderef.build(pointeddef);
  2739. end;
  2740. procedure tabstractpointerdef.deref;
  2741. begin
  2742. inherited deref;
  2743. pointeddef:=tdef(pointeddefderef.resolve);
  2744. end;
  2745. function tabstractpointerdef.size: asizeint;
  2746. begin
  2747. Result:=voidpointertype.size;
  2748. end;
  2749. function tabstractpointerdef.alignment: shortint;
  2750. begin
  2751. alignment:=size_2_align(voidpointertype.size);
  2752. end;
  2753. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2754. begin
  2755. inherited ppuwrite(ppufile);
  2756. ppufile.putderef(pointeddefderef);
  2757. end;
  2758. {****************************************************************************
  2759. tpointerdef
  2760. ****************************************************************************}
  2761. constructor tpointerdef.create(def:tdef);
  2762. begin
  2763. inherited create(pointerdef,def);
  2764. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2765. end;
  2766. function tpointerdef.size: asizeint;
  2767. begin
  2768. result:=sizeof(pint);
  2769. end;
  2770. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2771. begin
  2772. inherited ppuload(pointerdef,ppufile);
  2773. has_pointer_math:=(ppufile.getbyte<>0);
  2774. ppuload_platform(ppufile);
  2775. end;
  2776. function tpointerdef.getcopy : tstoreddef;
  2777. begin
  2778. { don't use direct pointeddef if it is a forwarddef because in other case
  2779. one of them will be destroyed on forward type resolve and the second will
  2780. point to garbage }
  2781. if pointeddef.typ=forwarddef then
  2782. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  2783. else
  2784. result:=cpointerdef.create(pointeddef);
  2785. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2786. tpointerdef(result).savesize:=savesize;
  2787. end;
  2788. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2789. begin
  2790. inherited ppuwrite(ppufile);
  2791. ppufile.putbyte(byte(has_pointer_math));
  2792. writeentry(ppufile,ibpointerdef);
  2793. end;
  2794. function tpointerdef.GetTypeName : string;
  2795. begin
  2796. { parameter types and the resultdef of a procvardef can contain a
  2797. pointer to this procvardef itself, resulting in endless recursion ->
  2798. use the typesym's name instead if it exists (if it doesn't, such as
  2799. for anynonymous procedure types in macpas/iso mode, then there cannot
  2800. be any recursive references to it either) }
  2801. if (pointeddef.typ<>procvardef) or
  2802. not assigned(pointeddef.typesym) then
  2803. GetTypeName:='^'+pointeddef.typename
  2804. else
  2805. GetTypeName:='^'+pointeddef.typesym.realname;
  2806. end;
  2807. function tpointerdef.pointer_subtraction_result_type:tdef;
  2808. begin
  2809. result:=ptrsinttype;
  2810. end;
  2811. {****************************************************************************
  2812. TCLASSREFDEF
  2813. ****************************************************************************}
  2814. constructor tclassrefdef.create(def:tdef);
  2815. begin
  2816. inherited create(classrefdef,def);
  2817. end;
  2818. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2819. begin
  2820. inherited ppuload(classrefdef,ppufile);
  2821. ppuload_platform(ppufile);
  2822. end;
  2823. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2824. begin
  2825. inherited ppuwrite(ppufile);
  2826. writeentry(ppufile,ibclassrefdef);
  2827. end;
  2828. function tclassrefdef.getcopy:tstoreddef;
  2829. begin
  2830. if pointeddef.typ=forwarddef then
  2831. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2832. else
  2833. result:=cclassrefdef.create(pointeddef);
  2834. end;
  2835. function tclassrefdef.GetTypeName : string;
  2836. begin
  2837. GetTypeName:='Class Of '+pointeddef.typename;
  2838. end;
  2839. function tclassrefdef.is_publishable : boolean;
  2840. begin
  2841. result:=true;
  2842. end;
  2843. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2844. begin
  2845. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2846. result:=inherited rtti_mangledname(rt)
  2847. else
  2848. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2849. end;
  2850. procedure tclassrefdef.register_created_object_type;
  2851. begin
  2852. tobjectdef(pointeddef).register_created_classref_type;
  2853. end;
  2854. {***************************************************************************
  2855. TSETDEF
  2856. ***************************************************************************}
  2857. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2858. var
  2859. setallocbits: aint;
  2860. packedsavesize: aint;
  2861. actual_setalloc: ShortInt;
  2862. begin
  2863. inherited create(setdef);
  2864. elementdef:=def;
  2865. setmax:=high;
  2866. actual_setalloc:=current_settings.setalloc;
  2867. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  2868. if actual_setalloc=0 then
  2869. actual_setalloc:=1;
  2870. {$endif}
  2871. if (actual_setalloc=0) then
  2872. begin
  2873. setbase:=0;
  2874. if (high<32) then
  2875. savesize:=Sizeof(longint)
  2876. else if (high<256) then
  2877. savesize:=32
  2878. else
  2879. savesize:=(high+7) div 8
  2880. end
  2881. else
  2882. begin
  2883. setallocbits:=actual_setalloc*8;
  2884. setbase:=low and not(setallocbits-1);
  2885. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2886. savesize:=packedsavesize;
  2887. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  2888. if savesize=3 then
  2889. savesize:=4;
  2890. {$endif}
  2891. end;
  2892. end;
  2893. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2894. begin
  2895. inherited ppuload(setdef,ppufile);
  2896. ppufile.getderef(elementdefderef);
  2897. savesize:=ppufile.getaint;
  2898. setbase:=ppufile.getaint;
  2899. setmax:=ppufile.getaint;
  2900. ppuload_platform(ppufile);
  2901. end;
  2902. function tsetdef.getcopy : tstoreddef;
  2903. begin
  2904. result:=csetdef.create(elementdef,setbase,setmax);
  2905. { the copy might have been created with a different setalloc setting }
  2906. tsetdef(result).savesize:=savesize;
  2907. end;
  2908. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2909. begin
  2910. inherited ppuwrite(ppufile);
  2911. ppufile.putderef(elementdefderef);
  2912. ppufile.putaint(savesize);
  2913. ppufile.putaint(setbase);
  2914. ppufile.putaint(setmax);
  2915. writeentry(ppufile,ibsetdef);
  2916. end;
  2917. procedure tsetdef.buildderef;
  2918. begin
  2919. inherited buildderef;
  2920. elementdefderef.build(elementdef);
  2921. end;
  2922. procedure tsetdef.deref;
  2923. begin
  2924. inherited deref;
  2925. elementdef:=tdef(elementdefderef.resolve);
  2926. end;
  2927. function tsetdef.is_publishable : boolean;
  2928. begin
  2929. is_publishable:=savesize in [1,2,4];
  2930. end;
  2931. function tsetdef.GetTypeName : string;
  2932. begin
  2933. if assigned(elementdef) then
  2934. GetTypeName:='Set Of '+elementdef.typename
  2935. else
  2936. GetTypeName:='Empty Set';
  2937. end;
  2938. {***************************************************************************
  2939. TFORMALDEF
  2940. ***************************************************************************}
  2941. constructor tformaldef.create(Atyped:boolean);
  2942. begin
  2943. inherited create(formaldef);
  2944. typed:=Atyped;
  2945. savesize:=0;
  2946. end;
  2947. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2948. begin
  2949. inherited ppuload(formaldef,ppufile);
  2950. typed:=boolean(ppufile.getbyte);
  2951. savesize:=0;
  2952. ppuload_platform(ppufile);
  2953. end;
  2954. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2955. begin
  2956. inherited ppuwrite(ppufile);
  2957. ppufile.putbyte(byte(typed));
  2958. writeentry(ppufile,ibformaldef);
  2959. end;
  2960. function tformaldef.GetTypeName : string;
  2961. begin
  2962. if typed then
  2963. GetTypeName:='<Typed formal type>'
  2964. else
  2965. GetTypeName:='<Formal type>';
  2966. end;
  2967. {***************************************************************************
  2968. TARRAYDEF
  2969. ***************************************************************************}
  2970. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2971. begin
  2972. inherited create(arraydef);
  2973. lowrange:=l;
  2974. highrange:=h;
  2975. rangedef:=def;
  2976. _elementdef:=nil;
  2977. arrayoptions:=[];
  2978. symtable:=tarraysymtable.create(self);
  2979. end;
  2980. destructor tarraydef.destroy;
  2981. begin
  2982. symtable.free;
  2983. symtable:=nil;
  2984. inherited;
  2985. end;
  2986. constructor tarraydef.create_from_pointer(def:tdef);
  2987. begin
  2988. { use -1 so that the elecount will not overflow }
  2989. self.create(0,high(asizeint)-1,ptrsinttype);
  2990. arrayoptions:=[ado_IsConvertedPointer];
  2991. setelementdef(def);
  2992. end;
  2993. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2994. begin
  2995. inherited ppuload(arraydef,ppufile);
  2996. { the addresses are calculated later }
  2997. ppufile.getderef(_elementdefderef);
  2998. ppufile.getderef(rangedefderef);
  2999. lowrange:=ppufile.getasizeint;
  3000. highrange:=ppufile.getasizeint;
  3001. ppufile.getsmallset(arrayoptions);
  3002. ppuload_platform(ppufile);
  3003. symtable:=tarraysymtable.create(self);
  3004. tarraysymtable(symtable).ppuload(ppufile)
  3005. end;
  3006. function tarraydef.getcopy : tstoreddef;
  3007. begin
  3008. result:=carraydef.create(lowrange,highrange,rangedef);
  3009. tarraydef(result).arrayoptions:=arrayoptions;
  3010. tarraydef(result)._elementdef:=_elementdef;
  3011. end;
  3012. procedure tarraydef.buildderef;
  3013. begin
  3014. inherited buildderef;
  3015. tarraysymtable(symtable).buildderef;
  3016. _elementdefderef.build(_elementdef);
  3017. rangedefderef.build(rangedef);
  3018. end;
  3019. procedure tarraydef.deref;
  3020. begin
  3021. inherited deref;
  3022. tarraysymtable(symtable).deref;
  3023. _elementdef:=tdef(_elementdefderef.resolve);
  3024. rangedef:=tdef(rangedefderef.resolve);
  3025. end;
  3026. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3027. begin
  3028. inherited ppuwrite(ppufile);
  3029. ppufile.putderef(_elementdefderef);
  3030. ppufile.putderef(rangedefderef);
  3031. ppufile.putasizeint(lowrange);
  3032. ppufile.putasizeint(highrange);
  3033. ppufile.putsmallset(arrayoptions);
  3034. writeentry(ppufile,ibarraydef);
  3035. tarraysymtable(symtable).ppuwrite(ppufile);
  3036. end;
  3037. function tarraydef.elesize : asizeint;
  3038. begin
  3039. if (ado_IsBitPacked in arrayoptions) then
  3040. internalerror(2006080101);
  3041. if assigned(_elementdef) then
  3042. result:=_elementdef.size
  3043. else
  3044. result:=0;
  3045. end;
  3046. function tarraydef.elepackedbitsize : asizeint;
  3047. begin
  3048. if not(ado_IsBitPacked in arrayoptions) then
  3049. internalerror(2006080102);
  3050. if assigned(_elementdef) then
  3051. result:=_elementdef.packedbitsize
  3052. else
  3053. result:=0;
  3054. end;
  3055. function tarraydef.elecount : asizeuint;
  3056. var
  3057. qhigh,qlow : qword;
  3058. begin
  3059. if ado_IsDynamicArray in arrayoptions then
  3060. begin
  3061. result:=0;
  3062. exit;
  3063. end;
  3064. if (highrange>0) and (lowrange<0) then
  3065. begin
  3066. qhigh:=highrange;
  3067. qlow:=qword(-lowrange);
  3068. { prevent overflow, return 0 to indicate overflow }
  3069. if qhigh+qlow>qword(high(asizeint)-1) then
  3070. result:=0
  3071. else
  3072. result:=qhigh+qlow+1;
  3073. end
  3074. else
  3075. result:=int64(highrange)-lowrange+1;
  3076. end;
  3077. function tarraydef.size : asizeint;
  3078. var
  3079. cachedelecount : asizeuint;
  3080. cachedelesize : asizeint;
  3081. begin
  3082. if ado_IsDynamicArray in arrayoptions then
  3083. begin
  3084. size:=voidpointertype.size;
  3085. exit;
  3086. end;
  3087. { Tarraydef.size may never be called for an open array! }
  3088. if highrange<lowrange then
  3089. internalerror(99080501);
  3090. if not (ado_IsBitPacked in arrayoptions) then
  3091. cachedelesize:=elesize
  3092. else
  3093. cachedelesize := elepackedbitsize;
  3094. cachedelecount:=elecount;
  3095. if (cachedelesize = 0) then
  3096. begin
  3097. size := 0;
  3098. exit;
  3099. end;
  3100. if (cachedelecount = 0) then
  3101. begin
  3102. size := -1;
  3103. exit;
  3104. end;
  3105. { prevent overflow, return -1 to indicate overflow }
  3106. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3107. if (cachedelecount > asizeuint(high(asizeint))) or
  3108. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3109. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3110. accessing the array, see ncgmem (PFV) }
  3111. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3112. begin
  3113. result:=-1;
  3114. exit;
  3115. end;
  3116. result:=cachedelesize*asizeint(cachedelecount);
  3117. if (ado_IsBitPacked in arrayoptions) then
  3118. { can't just add 7 and divide by 8, because that may overflow }
  3119. result:=result div 8 + ord((result mod 8)<>0);
  3120. end;
  3121. procedure tarraydef.setelementdef(def:tdef);
  3122. begin
  3123. _elementdef:=def;
  3124. if not(
  3125. (ado_IsDynamicArray in arrayoptions) or
  3126. (ado_IsConvertedPointer in arrayoptions) or
  3127. (highrange<lowrange)
  3128. ) and
  3129. (size=-1) then
  3130. Message(sym_e_segment_too_large);
  3131. end;
  3132. function tarraydef.alignment : shortint;
  3133. begin
  3134. { alignment of dyn. arrays doesn't depend on the element size }
  3135. if (ado_IsDynamicArray in arrayoptions) then
  3136. alignment:=size_2_align(voidpointertype.size)
  3137. { alignment is the target alignment for the used load size }
  3138. else if (ado_IsBitPacked in arrayoptions) and
  3139. (elementdef.typ in [enumdef,orddef]) then
  3140. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3141. { alignment is the alignment of the elements }
  3142. else
  3143. alignment:=elementdef.alignment
  3144. end;
  3145. function tarraydef.needs_inittable : boolean;
  3146. begin
  3147. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3148. end;
  3149. function tarraydef.needs_separate_initrtti : boolean;
  3150. begin
  3151. if ado_IsBitPacked in arrayoptions then
  3152. result:=false
  3153. else
  3154. result:=elementdef.needs_separate_initrtti;
  3155. end;
  3156. function tarraydef.GetTypeName : string;
  3157. begin
  3158. if (ado_IsConstString in arrayoptions) then
  3159. result:='Constant String'
  3160. else if (ado_isarrayofconst in arrayoptions) or
  3161. (ado_isConstructor in arrayoptions) then
  3162. begin
  3163. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3164. GetTypeName:='Array Of Const'
  3165. else
  3166. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3167. end
  3168. else if (ado_IsDynamicArray in arrayoptions) then
  3169. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3170. else if ((highrange=-1) and (lowrange=0)) then
  3171. GetTypeName:='{Open} Array Of '+elementdef.typename
  3172. else
  3173. begin
  3174. result := '';
  3175. if (ado_IsBitPacked in arrayoptions) then
  3176. result:='BitPacked ';
  3177. if rangedef.typ=enumdef then
  3178. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3179. else
  3180. result:=result+'Array['+tostr(lowrange)+'..'+
  3181. tostr(highrange)+'] Of '+elementdef.typename
  3182. end;
  3183. end;
  3184. function tarraydef.getmangledparaname : TSymStr;
  3185. begin
  3186. if ado_isarrayofconst in arrayoptions then
  3187. getmangledparaname:='array_of_const'
  3188. else
  3189. if ((highrange=-1) and (lowrange=0)) then
  3190. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3191. else
  3192. internalerror(200204176);
  3193. end;
  3194. function tarraydef.is_publishable : boolean;
  3195. begin
  3196. Result:=ado_IsDynamicArray in arrayoptions;
  3197. end;
  3198. {***************************************************************************
  3199. tabstractrecorddef
  3200. ***************************************************************************}
  3201. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  3202. begin
  3203. inherited create(dt);
  3204. objname:=stringdup(upper(n));
  3205. objrealname:=stringdup(n);
  3206. objectoptions:=[];
  3207. if assigned(current_module.namespace) then
  3208. begin
  3209. import_lib:=stringdup(current_module.namespace^);
  3210. replace(import_lib^,'.','/');
  3211. end;
  3212. end;
  3213. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3214. begin
  3215. inherited ppuload(dt,ppufile);
  3216. objrealname:=stringdup(ppufile.getstring);
  3217. objname:=stringdup(upper(objrealname^));
  3218. import_lib:=stringdup(ppufile.getstring);
  3219. { only used for external C++ classes and Java classes/records }
  3220. if (import_lib^='') then
  3221. stringdispose(import_lib);
  3222. ppufile.getsmallset(objectoptions);
  3223. end;
  3224. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3225. begin
  3226. inherited ppuwrite(ppufile);
  3227. ppufile.putstring(objrealname^);
  3228. if assigned(import_lib) then
  3229. ppufile.putstring(import_lib^)
  3230. else
  3231. ppufile.putstring('');
  3232. ppufile.putsmallset(objectoptions);
  3233. end;
  3234. destructor tabstractrecorddef.destroy;
  3235. begin
  3236. stringdispose(objname);
  3237. stringdispose(objrealname);
  3238. stringdispose(import_lib);
  3239. tcinitcode.free;
  3240. inherited destroy;
  3241. end;
  3242. procedure tabstractrecorddef.check_forwards;
  3243. begin
  3244. { the defs of a copied def are defined for the original type only }
  3245. if not(df_copied_def in defoptions) then
  3246. tstoredsymtable(symtable).check_forwards;
  3247. end;
  3248. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3249. var
  3250. i: longint;
  3251. sym: tsym;
  3252. begin
  3253. for i:=0 to symtable.SymList.Count-1 do
  3254. begin
  3255. sym:=tsym(symtable.SymList[i]);
  3256. if sym.typ=procsym then
  3257. begin
  3258. result:=tprocsym(sym).find_procdef_bytype(pt);
  3259. if assigned(result) then
  3260. exit;
  3261. end;
  3262. end;
  3263. result:=nil;
  3264. end;
  3265. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3266. begin
  3267. if t=gs_record then
  3268. GetSymtable:=symtable
  3269. else
  3270. GetSymtable:=nil;
  3271. end;
  3272. function tabstractrecorddef.is_packed:boolean;
  3273. begin
  3274. result:=tabstractrecordsymtable(symtable).is_packed;
  3275. end;
  3276. function tabstractrecorddef.RttiName: string;
  3277. begin
  3278. Result:=OwnerHierarchyName+objrealname^;
  3279. end;
  3280. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3281. var
  3282. sym : tsym;
  3283. i : integer;
  3284. pd : tprocdef;
  3285. hashedid : THashedIDString;
  3286. begin
  3287. result:=nil;
  3288. hashedid.id:='GETENUMERATOR';
  3289. sym:=tsym(symtable.FindWithHash(hashedid));
  3290. if assigned(sym) and (sym.typ=procsym) then
  3291. begin
  3292. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3293. begin
  3294. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3295. if (pd.proctypeoption = potype_function) and
  3296. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3297. (pd.visibility >= vis_public) then
  3298. begin
  3299. result:=pd;
  3300. exit;
  3301. end;
  3302. end;
  3303. end;
  3304. end;
  3305. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3306. var
  3307. sym : tsym;
  3308. i : integer;
  3309. pd : tprocdef;
  3310. hashedid : THashedIDString;
  3311. begin
  3312. result:=nil;
  3313. // first search for po_enumerator_movenext method modifier
  3314. // then search for public function MoveNext: Boolean
  3315. for i:=0 to symtable.SymList.Count-1 do
  3316. begin
  3317. sym:=TSym(symtable.SymList[i]);
  3318. if (sym.typ=procsym) then
  3319. begin
  3320. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3321. if assigned(pd) then
  3322. begin
  3323. result:=pd;
  3324. exit;
  3325. end;
  3326. end;
  3327. end;
  3328. hashedid.id:='MOVENEXT';
  3329. sym:=tsym(symtable.FindWithHash(hashedid));
  3330. if assigned(sym) and (sym.typ=procsym) then
  3331. begin
  3332. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3333. begin
  3334. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3335. if (pd.proctypeoption = potype_function) and
  3336. is_boolean(pd.returndef) and
  3337. (pd.minparacount = 0) and
  3338. (pd.visibility >= vis_public) then
  3339. begin
  3340. result:=pd;
  3341. exit;
  3342. end;
  3343. end;
  3344. end;
  3345. end;
  3346. function tabstractrecorddef.search_enumerator_current: tsym;
  3347. var
  3348. sym: tsym;
  3349. i: integer;
  3350. hashedid : THashedIDString;
  3351. begin
  3352. result:=nil;
  3353. // first search for ppo_enumerator_current property modifier
  3354. // then search for public property Current
  3355. for i:=0 to symtable.SymList.Count-1 do
  3356. begin
  3357. sym:=TSym(symtable.SymList[i]);
  3358. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3359. begin
  3360. result:=sym;
  3361. exit;
  3362. end;
  3363. end;
  3364. hashedid.id:='CURRENT';
  3365. sym:=tsym(symtable.FindWithHash(hashedid));
  3366. if assigned(sym) and (sym.typ=propertysym) and
  3367. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3368. begin
  3369. result:=sym;
  3370. exit;
  3371. end;
  3372. end;
  3373. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3374. var
  3375. st: tsymtable;
  3376. enclosingdef: tdef;
  3377. begin
  3378. if typ=objectdef then
  3379. result:=tobjectdef(self).objextname^
  3380. else if assigned(typesym) then
  3381. result:=typesym.realname
  3382. { have to generate anonymous nested type in current unit/class/record }
  3383. else
  3384. internalerror(2011032601);
  3385. { in case of specializations, add some extras to prevent name conflicts
  3386. with nested classes }
  3387. if df_specialization in defoptions then
  3388. result:='$'+result+'$specialization$';
  3389. st:=owner;
  3390. while assigned(st) and
  3391. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3392. begin
  3393. { nested classes are named as "OuterClass$InnerClass" }
  3394. enclosingdef:=tdef(st.defowner);
  3395. if enclosingdef.typ=procdef then
  3396. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3397. else if enclosingdef.typ=objectdef then
  3398. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3399. else if assigned(enclosingdef.typesym) then
  3400. result:=enclosingdef.typesym.realname+'$'+result
  3401. else
  3402. internalerror(2011060305);
  3403. st:=enclosingdef.owner;
  3404. end;
  3405. if with_package_name and
  3406. assigned(import_lib) then
  3407. result:=import_lib^+'/'+result;
  3408. end;
  3409. function tabstractrecorddef.contains_float_field: boolean;
  3410. var
  3411. i : longint;
  3412. begin
  3413. result:=true;
  3414. for i:=0 to symtable.symlist.count-1 do
  3415. begin
  3416. if tsym(symtable.symlist[i]).typ<>fieldvarsym then
  3417. continue;
  3418. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) and
  3419. tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3420. exit;
  3421. end;
  3422. result:=false;
  3423. end;
  3424. {***************************************************************************
  3425. trecorddef
  3426. ***************************************************************************}
  3427. constructor trecorddef.create(const n:string; p:TSymtable);
  3428. begin
  3429. inherited create(n,recorddef);
  3430. symtable:=p;
  3431. { we can own the symtable only if nobody else owns a copy so far }
  3432. if symtable.refcount=1 then
  3433. symtable.defowner:=self;
  3434. isunion:=false;
  3435. end;
  3436. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3437. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3438. var
  3439. i,j : longint;
  3440. begin
  3441. if ppufile.getbyte=1 then
  3442. begin
  3443. new(variantrecdesc);
  3444. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3445. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3446. for i:=0 to high(variantrecdesc^.branches) do
  3447. begin
  3448. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3449. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3450. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3451. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3452. end;
  3453. end
  3454. else
  3455. variantrecdesc:=nil;
  3456. end;
  3457. begin
  3458. inherited ppuload(recorddef,ppufile);
  3459. if df_copied_def in defoptions then
  3460. begin
  3461. ppufile.getderef(cloneddefderef);
  3462. ppuload_platform(ppufile);
  3463. end
  3464. else
  3465. begin
  3466. ppuload_platform(ppufile);
  3467. symtable:=trecordsymtable.create(objrealname^,0);
  3468. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3469. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3470. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3471. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3472. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3473. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3474. trecordsymtable(symtable).ppuload(ppufile);
  3475. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3476. but because iso mode supports no units, there is no need to store the variantrecdesc
  3477. in the ppu
  3478. }
  3479. // readvariantrecdesc(variantrecdesc);
  3480. { requires usefieldalignment to be set }
  3481. symtable.defowner:=self;
  3482. end;
  3483. isunion:=false;
  3484. end;
  3485. destructor trecorddef.destroy;
  3486. begin
  3487. if assigned(symtable) then
  3488. begin
  3489. symtable.free;
  3490. symtable:=nil;
  3491. end;
  3492. inherited destroy;
  3493. end;
  3494. function trecorddef.getcopy : tstoreddef;
  3495. begin
  3496. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3497. trecorddef(result).isunion:=isunion;
  3498. include(trecorddef(result).defoptions,df_copied_def);
  3499. if assigned(tcinitcode) then
  3500. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3501. if assigned(import_lib) then
  3502. trecorddef(result).import_lib:=stringdup(import_lib^);
  3503. end;
  3504. function trecorddef.needs_inittable : boolean;
  3505. begin
  3506. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3507. end;
  3508. function trecorddef.needs_separate_initrtti : boolean;
  3509. begin
  3510. result:=true;
  3511. end;
  3512. procedure trecorddef.buildderef;
  3513. begin
  3514. inherited buildderef;
  3515. if df_copied_def in defoptions then
  3516. cloneddefderef.build(symtable.defowner)
  3517. else
  3518. tstoredsymtable(symtable).buildderef;
  3519. end;
  3520. procedure trecorddef.buildderefimpl;
  3521. begin
  3522. inherited buildderefimpl;
  3523. if not (df_copied_def in defoptions) then
  3524. tstoredsymtable(symtable).buildderefimpl;
  3525. end;
  3526. procedure trecorddef.deref;
  3527. begin
  3528. inherited deref;
  3529. { now dereference the definitions }
  3530. if df_copied_def in defoptions then
  3531. begin
  3532. cloneddef:=trecorddef(cloneddefderef.resolve);
  3533. symtable:=cloneddef.symtable.getcopy;
  3534. end
  3535. else
  3536. tstoredsymtable(symtable).deref;
  3537. { assign TGUID? load only from system unit }
  3538. if not(assigned(rec_tguid)) and
  3539. (upper(typename)='TGUID') and
  3540. assigned(owner) and
  3541. assigned(owner.name) and
  3542. (owner.name^='SYSTEM') then
  3543. rec_tguid:=self;
  3544. { assign JMP_BUF? load only from system unit }
  3545. if not(assigned(rec_jmp_buf)) and
  3546. (upper(typename)='JMP_BUF') and
  3547. assigned(owner) and
  3548. assigned(owner.name) and
  3549. (owner.name^='SYSTEM') then
  3550. rec_jmp_buf:=self;
  3551. end;
  3552. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3553. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3554. var
  3555. i,j : longint;
  3556. begin
  3557. if assigned(variantrecdesc) then
  3558. begin
  3559. ppufile.putbyte(1);
  3560. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3561. ppufile.putasizeint(length(variantrecdesc^.branches));
  3562. for i:=0 to high(variantrecdesc^.branches) do
  3563. begin
  3564. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3565. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3566. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3567. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3568. end;
  3569. end
  3570. else
  3571. ppufile.putbyte(0);
  3572. end;
  3573. begin
  3574. inherited ppuwrite(ppufile);
  3575. if df_copied_def in defoptions then
  3576. ppufile.putderef(cloneddefderef)
  3577. else
  3578. begin
  3579. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3580. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3581. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3582. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3583. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3584. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3585. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3586. but because iso mode supports no units, there is no need to store the variantrecdesc
  3587. in the ppu
  3588. }
  3589. // writevariantrecdesc(variantrecdesc);
  3590. end;
  3591. writeentry(ppufile,ibrecorddef);
  3592. if not(df_copied_def in defoptions) then
  3593. trecordsymtable(symtable).ppuwrite(ppufile);
  3594. end;
  3595. function trecorddef.size:asizeint;
  3596. begin
  3597. result:=trecordsymtable(symtable).datasize;
  3598. end;
  3599. function trecorddef.alignment:shortint;
  3600. begin
  3601. alignment:=trecordsymtable(symtable).recordalignment;
  3602. end;
  3603. function trecorddef.padalignment:shortint;
  3604. begin
  3605. padalignment := trecordsymtable(symtable).padalignment;
  3606. end;
  3607. function trecorddef.GetTypeName : string;
  3608. begin
  3609. GetTypeName:='<record type>'
  3610. end;
  3611. {***************************************************************************
  3612. TABSTRACTPROCDEF
  3613. ***************************************************************************}
  3614. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  3615. begin
  3616. inherited create(dt);
  3617. parast:=tparasymtable.create(self,level);
  3618. paras:=nil;
  3619. minparacount:=0;
  3620. maxparacount:=0;
  3621. proctypeoption:=potype_none;
  3622. proccalloption:=pocall_none;
  3623. procoptions:=[];
  3624. returndef:=voidtype;
  3625. savesize:=sizeof(pint);
  3626. callerargareasize:=0;
  3627. calleeargareasize:=0;
  3628. has_paraloc_info:=callnoside;
  3629. funcretloc[callerside].init;
  3630. funcretloc[calleeside].init;
  3631. check_mark_as_nested;
  3632. end;
  3633. destructor tabstractprocdef.destroy;
  3634. begin
  3635. if assigned(paras) then
  3636. begin
  3637. {$ifdef MEMDEBUG}
  3638. memprocpara.start;
  3639. {$endif MEMDEBUG}
  3640. paras.free;
  3641. paras:=nil;
  3642. {$ifdef MEMDEBUG}
  3643. memprocpara.stop;
  3644. {$endif MEMDEBUG}
  3645. end;
  3646. if assigned(parast) then
  3647. begin
  3648. {$ifdef MEMDEBUG}
  3649. memprocparast.start;
  3650. {$endif MEMDEBUG}
  3651. parast.free;
  3652. parast:=nil;
  3653. {$ifdef MEMDEBUG}
  3654. memprocparast.stop;
  3655. {$endif MEMDEBUG}
  3656. end;
  3657. funcretloc[callerside].done;
  3658. funcretloc[calleeside].done;
  3659. inherited destroy;
  3660. end;
  3661. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3662. begin
  3663. if (tsym(p).typ<>paravarsym) then
  3664. exit;
  3665. inc(plongint(arg)^);
  3666. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3667. begin
  3668. if not assigned(tparavarsym(p).defaultconstsym) then
  3669. inc(minparacount);
  3670. inc(maxparacount);
  3671. end;
  3672. end;
  3673. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3674. begin
  3675. if (tsym(p).typ<>paravarsym) then
  3676. exit;
  3677. paras.add(p);
  3678. end;
  3679. procedure tabstractprocdef.calcparas;
  3680. var
  3681. paracount : longint;
  3682. begin
  3683. { This can already be assigned when
  3684. we need to reresolve this unit (PFV) }
  3685. if assigned(paras) then
  3686. paras.free;
  3687. paras:=tparalist.create(false);
  3688. paracount:=0;
  3689. minparacount:=0;
  3690. maxparacount:=0;
  3691. parast.SymList.ForEachCall(@count_para,@paracount);
  3692. paras.capacity:=paracount;
  3693. { Insert parameters in table }
  3694. parast.SymList.ForEachCall(@insert_para,nil);
  3695. { Order parameters }
  3696. paras.sortparas;
  3697. end;
  3698. procedure tabstractprocdef.buildderef;
  3699. begin
  3700. { released procdef? }
  3701. if not assigned(parast) then
  3702. exit;
  3703. inherited buildderef;
  3704. returndefderef.build(returndef);
  3705. if po_explicitparaloc in procoptions then
  3706. funcretloc[callerside].buildderef;
  3707. { parast }
  3708. tparasymtable(parast).buildderef;
  3709. end;
  3710. procedure tabstractprocdef.deref;
  3711. begin
  3712. inherited deref;
  3713. returndef:=tdef(returndefderef.resolve);
  3714. if po_explicitparaloc in procoptions then
  3715. begin
  3716. funcretloc[callerside].deref;
  3717. has_paraloc_info:=callerside;
  3718. end
  3719. else
  3720. begin
  3721. { deref is called after loading from a ppu, but also after another
  3722. unit has been reloaded/recompiled and all references must be
  3723. re-resolved. Since the funcretloc contains a reference to a tdef,
  3724. reset it so that we won't try to access the stale def }
  3725. funcretloc[callerside].init;
  3726. has_paraloc_info:=callnoside;
  3727. end;
  3728. { parast }
  3729. tparasymtable(parast).deref;
  3730. { recalculated parameters }
  3731. calcparas;
  3732. end;
  3733. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3734. begin
  3735. inherited ppuload(dt,ppufile);
  3736. parast:=nil;
  3737. Paras:=nil;
  3738. minparacount:=0;
  3739. maxparacount:=0;
  3740. ppufile.getderef(returndefderef);
  3741. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3742. proccalloption:=tproccalloption(ppufile.getbyte);
  3743. ppufile.getnormalset(procoptions);
  3744. funcretloc[callerside].init;
  3745. if po_explicitparaloc in procoptions then
  3746. funcretloc[callerside].ppuload(ppufile);
  3747. savesize:=sizeof(pint);
  3748. if (po_explicitparaloc in procoptions) then
  3749. has_paraloc_info:=callerside;
  3750. end;
  3751. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3752. var
  3753. oldintfcrc : boolean;
  3754. begin
  3755. { released procdef? }
  3756. if not assigned(parast) then
  3757. exit;
  3758. inherited ppuwrite(ppufile);
  3759. ppufile.putderef(returndefderef);
  3760. oldintfcrc:=ppufile.do_interface_crc;
  3761. ppufile.do_interface_crc:=false;
  3762. ppufile.putbyte(ord(proctypeoption));
  3763. ppufile.putbyte(ord(proccalloption));
  3764. ppufile.putnormalset(procoptions);
  3765. ppufile.do_interface_crc:=oldintfcrc;
  3766. if (po_explicitparaloc in procoptions) then
  3767. funcretloc[callerside].ppuwrite(ppufile);
  3768. end;
  3769. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3770. var
  3771. hs,s : ansistring;
  3772. hp : TParavarsym;
  3773. hpc : tconstsym;
  3774. first : boolean;
  3775. i : integer;
  3776. begin
  3777. s:='';
  3778. first:=true;
  3779. for i:=0 to paras.count-1 do
  3780. begin
  3781. hp:=tparavarsym(paras[i]);
  3782. if not(vo_is_hidden_para in hp.varoptions) or
  3783. (pno_showhidden in pno) then
  3784. begin
  3785. if first then
  3786. begin
  3787. s:=s+'(';
  3788. first:=false;
  3789. end
  3790. else
  3791. s:=s+';';
  3792. if vo_is_hidden_para in hp.varoptions then
  3793. s:=s+'<';
  3794. case hp.varspez of
  3795. vs_var :
  3796. s:=s+'var ';
  3797. vs_const :
  3798. s:=s+'const ';
  3799. vs_out :
  3800. s:=s+'out ';
  3801. vs_constref :
  3802. s:=s+'constref ';
  3803. end;
  3804. if (pno_paranames in pno) then
  3805. s:=s+hp.realname+':';
  3806. if hp.univpara then
  3807. s:=s+'univ ';
  3808. if assigned(hp.vardef.typesym) then
  3809. begin
  3810. hs:=hp.vardef.typesym.realname;
  3811. if hs[1]<>'$' then
  3812. s:=s+hp.vardef.OwnerHierarchyName+hs
  3813. else
  3814. s:=s+hp.vardef.GetTypeName;
  3815. end
  3816. else
  3817. s:=s+hp.vardef.GetTypeName;
  3818. { default value }
  3819. if assigned(hp.defaultconstsym) then
  3820. begin
  3821. hpc:=tconstsym(hp.defaultconstsym);
  3822. hs:='';
  3823. case hpc.consttyp of
  3824. conststring,
  3825. constresourcestring :
  3826. begin
  3827. If hpc.value.len>0 then
  3828. begin
  3829. setLength(hs,hpc.value.len);
  3830. { don't write past the end of hs if the constant
  3831. is > 255 chars }
  3832. move(hpc.value.valueptr^,hs[1],length(hs));
  3833. { make sure that constant strings with newline chars
  3834. don't create a linebreak in the assembler code,
  3835. since comments are line-based. Also remove nulls
  3836. because the comments are written as a pchar. }
  3837. ReplaceCase(hs,#0,'.');
  3838. ReplaceCase(hs,#10,'.');
  3839. ReplaceCase(hs,#13,'.');
  3840. end;
  3841. end;
  3842. constreal :
  3843. str(pbestreal(hpc.value.valueptr)^,hs);
  3844. constpointer :
  3845. hs:=tostr(hpc.value.valueordptr);
  3846. constord :
  3847. begin
  3848. if is_boolean(hpc.constdef) then
  3849. begin
  3850. if hpc.value.valueord<>0 then
  3851. hs:='TRUE'
  3852. else
  3853. hs:='FALSE';
  3854. end
  3855. else
  3856. hs:=tostr(hpc.value.valueord);
  3857. end;
  3858. constnil :
  3859. hs:='nil';
  3860. constset :
  3861. hs:='<set>';
  3862. end;
  3863. if hs<>'' then
  3864. s:=s+'="'+hs+'"';
  3865. end;
  3866. if vo_is_hidden_para in hp.varoptions then
  3867. s:=s+'>';
  3868. end;
  3869. end;
  3870. if not first then
  3871. s:=s+')';
  3872. if (po_varargs in procoptions) then
  3873. s:=s+';VarArgs';
  3874. typename_paras:=s;
  3875. end;
  3876. function tabstractprocdef.is_methodpointer:boolean;
  3877. begin
  3878. result:=false;
  3879. end;
  3880. function tabstractprocdef.is_addressonly:boolean;
  3881. begin
  3882. result:=true;
  3883. end;
  3884. function tabstractprocdef.no_self_node: boolean;
  3885. begin
  3886. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  3887. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  3888. end;
  3889. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  3890. var
  3891. j, nestinglevel: longint;
  3892. pvs, npvs: tparavarsym;
  3893. begin
  3894. nestinglevel:=parast.symtablelevel;
  3895. if newtyp=procdef then
  3896. begin
  3897. if (copytyp<>pc_bareproc) then
  3898. result:=cprocdef.create(nestinglevel)
  3899. else
  3900. result:=cprocdef.create(normal_function_level);
  3901. tprocdef(result).visibility:=vis_public;
  3902. end
  3903. else
  3904. begin
  3905. result:=cprocvardef.create(nestinglevel);
  3906. end;
  3907. tabstractprocdef(result).returndef:=returndef;
  3908. tabstractprocdef(result).returndefderef:=returndefderef;
  3909. pvs:=nil;
  3910. npvs:=nil;
  3911. for j:=0 to parast.symlist.count-1 do
  3912. begin
  3913. case tsym(parast.symlist[j]).typ of
  3914. paravarsym:
  3915. begin
  3916. pvs:=tparavarsym(parast.symlist[j]);
  3917. { in case of bare proc, don't copy self, vmt or framepointer
  3918. parameters }
  3919. if (copytyp=pc_bareproc) and
  3920. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret]*pvs.varoptions)<>[]) then
  3921. continue;
  3922. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  3923. pvs.vardef,pvs.varoptions);
  3924. npvs.defaultconstsym:=pvs.defaultconstsym;
  3925. tabstractprocdef(result).parast.insert(npvs);
  3926. end;
  3927. constsym:
  3928. begin
  3929. // ignore, reuse original constym. Should also be duplicated
  3930. // be safe though
  3931. end;
  3932. symconst.typesym:
  3933. begin
  3934. // reuse original, part of generic declaration
  3935. end
  3936. else
  3937. internalerror(201160604);
  3938. end;
  3939. end;
  3940. tabstractprocdef(result).savesize:=savesize;
  3941. tabstractprocdef(result).proctypeoption:=proctypeoption;
  3942. tabstractprocdef(result).proccalloption:=proccalloption;
  3943. tabstractprocdef(result).procoptions:=procoptions;
  3944. if (copytyp=pc_bareproc) then
  3945. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  3946. if newtyp=procvardef then
  3947. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  3948. tabstractprocdef(result).callerargareasize:=callerargareasize;
  3949. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  3950. tabstractprocdef(result).maxparacount:=maxparacount;
  3951. tabstractprocdef(result).minparacount:=minparacount;
  3952. if po_explicitparaloc in procoptions then
  3953. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  3954. { recalculate parameter info }
  3955. tabstractprocdef(result).has_paraloc_info:=callnoside;
  3956. {$ifdef m68k}
  3957. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  3958. {$endif}
  3959. if (typ=procdef) and
  3960. (newtyp=procvardef) and
  3961. (owner.symtabletype=ObjectSymtable) then
  3962. include(tprocvardef(result).procoptions,po_methodpointer);
  3963. end;
  3964. procedure tabstractprocdef.check_mark_as_nested;
  3965. begin
  3966. { nested procvars require that nested functions use the Delphi-style
  3967. nested procedure calling convention }
  3968. if (parast.symtablelevel>normal_function_level) and
  3969. (m_nested_procvars in current_settings.modeswitches) then
  3970. include(procoptions,po_delphi_nested_cc);
  3971. end;
  3972. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  3973. begin
  3974. if (side in [callerside,callbothsides]) and
  3975. not(has_paraloc_info in [callerside,callbothsides]) then
  3976. begin
  3977. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  3978. if has_paraloc_info in [calleeside,callbothsides] then
  3979. has_paraloc_info:=callbothsides
  3980. else
  3981. has_paraloc_info:=callerside;
  3982. end;
  3983. if (side in [calleeside,callbothsides]) and
  3984. not(has_paraloc_info in [calleeside,callbothsides]) then
  3985. begin
  3986. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  3987. if has_paraloc_info in [callerside,callbothsides] then
  3988. has_paraloc_info:=callbothsides
  3989. else
  3990. has_paraloc_info:=calleeside;
  3991. end;
  3992. end;
  3993. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  3994. var
  3995. p: tparavarsym;
  3996. ploc: PCGParalocation;
  3997. i: longint;
  3998. begin
  3999. result:=false;
  4000. init_paraloc_info(side);
  4001. for i:=0 to parast.SymList.Count-1 do
  4002. if tsym(parast.SymList[i]).typ=paravarsym then
  4003. begin
  4004. p:=tparavarsym(parast.SymList[i]);
  4005. { check if no parameter is located on the stack }
  4006. if is_open_array(p.vardef) or
  4007. is_array_of_const(p.vardef) then
  4008. begin
  4009. result:=true;
  4010. exit;
  4011. end;
  4012. ploc:=p.paraloc[side].location;
  4013. while assigned(ploc) do
  4014. begin
  4015. if (ploc^.loc=LOC_REFERENCE) then
  4016. begin
  4017. result:=true;
  4018. exit
  4019. end;
  4020. ploc:=ploc^.next;
  4021. end;
  4022. end;
  4023. end;
  4024. function tabstractprocdef.is_pushleftright: boolean;
  4025. begin
  4026. result:=false;
  4027. end;
  4028. function tabstractprocdef.address_type: tdef;
  4029. begin
  4030. result:=voidcodepointertype;
  4031. end;
  4032. procedure tabstractprocdef.declared_far;
  4033. begin
  4034. Message1(parser_w_proc_directive_ignored,'FAR');
  4035. end;
  4036. procedure tabstractprocdef.declared_near;
  4037. begin
  4038. Message1(parser_w_proc_directive_ignored,'NEAR');
  4039. end;
  4040. {***************************************************************************
  4041. TPROCDEF
  4042. ***************************************************************************}
  4043. function tprocdef.GetResultName: PShortString;
  4044. begin
  4045. if not assigned(implprocdefinfo) then
  4046. internalerror(2014010301);
  4047. result:=implprocdefinfo^.resultname;
  4048. end;
  4049. procedure tprocdef.SetResultName(AValue: PShortString);
  4050. begin
  4051. if not assigned(implprocdefinfo) then
  4052. internalerror(2014010302);
  4053. implprocdefinfo^.resultname:=AValue;
  4054. end;
  4055. function tprocdef.GetParentFPInitBlock: tnode;
  4056. begin
  4057. if not assigned(implprocdefinfo) then
  4058. internalerror(2014010303);
  4059. result:=implprocdefinfo^.parentfpinitblock;
  4060. end;
  4061. function tprocdef.GetParentFPStruct: tsym;
  4062. begin
  4063. if not assigned(implprocdefinfo) then
  4064. internalerror(2014010304);
  4065. result:=implprocdefinfo^.parentfpstruct;
  4066. end;
  4067. function tprocdef.GetParentFPStructPtrType: tdef;
  4068. begin
  4069. if not assigned(implprocdefinfo) then
  4070. internalerror(2014010305);
  4071. result:=implprocdefinfo^.parentfpstructptrtype;
  4072. end;
  4073. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4074. begin
  4075. if not assigned(implprocdefinfo) then
  4076. internalerror(2014010306);
  4077. implprocdefinfo^.parentfpinitblock:=AValue;
  4078. end;
  4079. function tprocdef.Getprocendtai: tai;
  4080. begin
  4081. if not assigned(implprocdefinfo) then
  4082. internalerror(2014010307);
  4083. result:=implprocdefinfo^.procendtai;
  4084. end;
  4085. function tprocdef.Getprocstarttai: tai;
  4086. begin
  4087. if not assigned(implprocdefinfo) then
  4088. internalerror(2014010308);
  4089. result:=implprocdefinfo^.procstarttai;
  4090. end;
  4091. procedure tprocdef.Setprocendtai(AValue: tai);
  4092. begin
  4093. if not assigned(implprocdefinfo) then
  4094. internalerror(2014010309);
  4095. implprocdefinfo^.procendtai:=AValue;
  4096. end;
  4097. function tprocdef.Getskpara: pointer;
  4098. begin
  4099. if not assigned(implprocdefinfo) then
  4100. internalerror(2014010310);
  4101. result:=implprocdefinfo^.skpara;
  4102. end;
  4103. procedure tprocdef.Setskpara(AValue: pointer);
  4104. begin
  4105. if not assigned(implprocdefinfo) then
  4106. internalerror(2014010311);
  4107. implprocdefinfo^.skpara:=AValue;
  4108. end;
  4109. function tprocdef.Getforwarddef: boolean;
  4110. begin
  4111. if not assigned(implprocdefinfo) then
  4112. internalerror(2014010312);
  4113. result:=implprocdefinfo^.forwarddef;
  4114. end;
  4115. function tprocdef.Gethasforward: boolean;
  4116. begin
  4117. if not assigned(implprocdefinfo) then
  4118. internalerror(2014010313);
  4119. result:=implprocdefinfo^.hasforward;
  4120. end;
  4121. function tprocdef.Getinterfacedef: boolean;
  4122. begin
  4123. if not assigned(implprocdefinfo) then
  4124. internalerror(2014010314);
  4125. result:=implprocdefinfo^.interfacedef;
  4126. end;
  4127. procedure tprocdef.Setforwarddef(AValue: boolean);
  4128. begin
  4129. if not assigned(implprocdefinfo) then
  4130. internalerror(2014010315);
  4131. implprocdefinfo^.forwarddef:=AValue;
  4132. end;
  4133. procedure tprocdef.Sethasforward(AValue: boolean);
  4134. begin
  4135. if not assigned(implprocdefinfo) then
  4136. internalerror(2014010316);
  4137. implprocdefinfo^.hasforward:=AValue;
  4138. end;
  4139. function tprocdef.GetIsEmpty: boolean;
  4140. begin
  4141. result:=pio_empty in implprocoptions;
  4142. end;
  4143. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4144. begin
  4145. if AValue then
  4146. include(implprocoptions,pio_empty)
  4147. else
  4148. include(implprocoptions,pio_empty);
  4149. end;
  4150. function tprocdef.GetHasInliningInfo: boolean;
  4151. begin
  4152. result:=pio_has_inlininginfo in implprocoptions;
  4153. end;
  4154. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4155. begin
  4156. if AValue then
  4157. include(implprocoptions,pio_has_inlininginfo)
  4158. else
  4159. exclude(implprocoptions,pio_has_inlininginfo);
  4160. end;
  4161. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4162. begin
  4163. if not assigned(implprocdefinfo) then
  4164. internalerror(2014010317);
  4165. implprocdefinfo^.interfacedef:=AValue;
  4166. end;
  4167. procedure tprocdef.Setprocstarttai(AValue: tai);
  4168. begin
  4169. if not assigned(implprocdefinfo) then
  4170. internalerror(2014010318);
  4171. implprocdefinfo^.procstarttai:=AValue;
  4172. end;
  4173. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4174. begin
  4175. if not assigned(implprocdefinfo) then
  4176. internalerror(2014010319);
  4177. implprocdefinfo^.parentfpstruct:=AValue;
  4178. end;
  4179. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4180. begin
  4181. if not assigned(implprocdefinfo) then
  4182. internalerror(2014010320);
  4183. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4184. end;
  4185. constructor tprocdef.create(level:byte);
  4186. begin
  4187. inherited create(procdef,level);
  4188. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4189. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4190. {$ifdef symansistr}
  4191. _mangledname:='';
  4192. {$else symansistr}
  4193. _mangledname:=nil;
  4194. {$endif symansistr}
  4195. fileinfo:=current_filepos;
  4196. extnumber:=$ffff;
  4197. aliasnames:=TCmdStrList.create;
  4198. funcretsym:=nil;
  4199. forwarddef:=true;
  4200. interfacedef:=false;
  4201. hasforward:=false;
  4202. struct := nil;
  4203. import_dll:=nil;
  4204. import_name:=nil;
  4205. import_nr:=0;
  4206. inlininginfo:=nil;
  4207. deprecatedmsg:=nil;
  4208. end;
  4209. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4210. var
  4211. i,aliasnamescount : longint;
  4212. level : byte;
  4213. begin
  4214. inherited ppuload(procdef,ppufile);
  4215. {$ifdef symansistr}
  4216. if po_has_mangledname in procoptions then
  4217. _mangledname:=ppufile.getansistring
  4218. else
  4219. _mangledname:='';
  4220. {$else symansistr}
  4221. if po_has_mangledname in procoptions then
  4222. _mangledname:=stringdup(ppufile.getstring)
  4223. else
  4224. _mangledname:=nil;
  4225. {$endif symansistr}
  4226. extnumber:=ppufile.getword;
  4227. level:=ppufile.getbyte;
  4228. ppufile.getderef(structderef);
  4229. ppufile.getderef(procsymderef);
  4230. ppufile.getposinfo(fileinfo);
  4231. visibility:=tvisibility(ppufile.getbyte);
  4232. ppufile.getsmallset(symoptions);
  4233. if sp_has_deprecated_msg in symoptions then
  4234. deprecatedmsg:=stringdup(ppufile.getstring)
  4235. else
  4236. deprecatedmsg:=nil;
  4237. synthetickind:=tsynthetickind(ppufile.getbyte);
  4238. { import stuff }
  4239. if po_has_importdll in procoptions then
  4240. import_dll:=stringdup(ppufile.getstring)
  4241. else
  4242. import_dll:=nil;
  4243. if po_has_importname in procoptions then
  4244. import_name:=stringdup(ppufile.getstring)
  4245. else
  4246. import_name:=nil;
  4247. import_nr:=ppufile.getword;
  4248. if (po_msgint in procoptions) then
  4249. messageinf.i:=ppufile.getlongint;
  4250. if (po_msgstr in procoptions) then
  4251. messageinf.str:=stringdup(ppufile.getstring);
  4252. if (po_dispid in procoptions) then
  4253. dispid:=ppufile.getlongint;
  4254. { inline stuff }
  4255. ppufile.getsmallset(implprocoptions);
  4256. if has_inlininginfo then
  4257. begin
  4258. ppufile.getderef(funcretsymderef);
  4259. new(inlininginfo);
  4260. ppufile.getsmallset(inlininginfo^.flags);
  4261. end
  4262. else
  4263. begin
  4264. inlininginfo:=nil;
  4265. funcretsym:=nil;
  4266. end;
  4267. aliasnames:=TCmdStrList.create;
  4268. { count alias names }
  4269. aliasnamescount:=ppufile.getbyte;
  4270. for i:=1 to aliasnamescount do
  4271. aliasnames.insert(ppufile.getstring);
  4272. ppuload_platform(ppufile);
  4273. { load para symtable }
  4274. parast:=tparasymtable.create(self,level);
  4275. tparasymtable(parast).ppuload(ppufile);
  4276. { load local symtable }
  4277. if has_inlininginfo then
  4278. begin
  4279. localst:=tlocalsymtable.create(self,level);
  4280. tlocalsymtable(localst).ppuload(ppufile);
  4281. end
  4282. else
  4283. localst:=nil;
  4284. { inline stuff }
  4285. if has_inlininginfo then
  4286. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4287. { default values for no persistent data }
  4288. if (cs_link_deffile in current_settings.globalswitches) and
  4289. (tf_need_export in target_info.flags) and
  4290. (po_exports in procoptions) then
  4291. deffile.AddExport(mangledname);
  4292. { Disable po_has_inlining until the derefimpl is done }
  4293. has_inlininginfo:=false;
  4294. end;
  4295. destructor tprocdef.destroy;
  4296. begin
  4297. aliasnames.free;
  4298. aliasnames:=nil;
  4299. if assigned(localst) and
  4300. (localst.symtabletype<>staticsymtable) then
  4301. begin
  4302. {$ifdef MEMDEBUG}
  4303. memproclocalst.start;
  4304. {$endif MEMDEBUG}
  4305. localst.free;
  4306. localst:=nil;
  4307. {$ifdef MEMDEBUG}
  4308. memproclocalst.start;
  4309. {$endif MEMDEBUG}
  4310. end;
  4311. if assigned(inlininginfo) then
  4312. begin
  4313. {$ifdef MEMDEBUG}
  4314. memprocnodetree.start;
  4315. {$endif MEMDEBUG}
  4316. tnode(inlininginfo^.code).free;
  4317. {$ifdef MEMDEBUG}
  4318. memprocnodetree.start;
  4319. {$endif MEMDEBUG}
  4320. dispose(inlininginfo);
  4321. inlininginfo:=nil;
  4322. end;
  4323. if assigned(implprocdefinfo) then
  4324. begin
  4325. stringdispose(implprocdefinfo^.resultname);
  4326. freemem(implprocdefinfo);
  4327. implprocdefinfo:=nil;
  4328. end;
  4329. stringdispose(import_dll);
  4330. stringdispose(import_name);
  4331. stringdispose(deprecatedmsg);
  4332. if (po_msgstr in procoptions) then
  4333. stringdispose(messageinf.str);
  4334. {$ifndef symansistr}
  4335. if assigned(_mangledname) then
  4336. begin
  4337. {$ifdef MEMDEBUG}
  4338. memmanglednames.start;
  4339. {$endif MEMDEBUG}
  4340. stringdispose(_mangledname);
  4341. {$ifdef MEMDEBUG}
  4342. memmanglednames.stop;
  4343. {$endif MEMDEBUG}
  4344. end;
  4345. {$endif symansistr}
  4346. inherited destroy;
  4347. end;
  4348. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4349. var
  4350. oldintfcrc : boolean;
  4351. aliasnamescount : longint;
  4352. item : TCmdStrListItem;
  4353. begin
  4354. { released procdef? }
  4355. if not assigned(parast) then
  4356. exit;
  4357. inherited ppuwrite(ppufile);
  4358. {$ifdef symansistr}
  4359. if po_has_mangledname in procoptions then
  4360. ppufile.putansistring(_mangledname);
  4361. {$else symansistr}
  4362. if po_has_mangledname in procoptions then
  4363. ppufile.putstring(_mangledname^);
  4364. {$endif symansistr}
  4365. ppufile.putword(extnumber);
  4366. ppufile.putbyte(parast.symtablelevel);
  4367. ppufile.putderef(structderef);
  4368. ppufile.putderef(procsymderef);
  4369. ppufile.putposinfo(fileinfo);
  4370. ppufile.putbyte(byte(visibility));
  4371. ppufile.putsmallset(symoptions);
  4372. if sp_has_deprecated_msg in symoptions then
  4373. ppufile.putstring(deprecatedmsg^);
  4374. ppufile.putbyte(byte(synthetickind));
  4375. { import }
  4376. if po_has_importdll in procoptions then
  4377. ppufile.putstring(import_dll^);
  4378. if po_has_importname in procoptions then
  4379. ppufile.putstring(import_name^);
  4380. ppufile.putword(import_nr);
  4381. if (po_msgint in procoptions) then
  4382. ppufile.putlongint(messageinf.i);
  4383. if (po_msgstr in procoptions) then
  4384. ppufile.putstring(messageinf.str^);
  4385. if (po_dispid in procoptions) then
  4386. ppufile.putlongint(dispid);
  4387. { inline stuff }
  4388. oldintfcrc:=ppufile.do_crc;
  4389. ppufile.do_crc:=false;
  4390. ppufile.putsmallset(implprocoptions);
  4391. if has_inlininginfo then
  4392. begin
  4393. ppufile.putderef(funcretsymderef);
  4394. ppufile.putsmallset(inlininginfo^.flags);
  4395. end;
  4396. { count alias names }
  4397. aliasnamescount:=0;
  4398. item:=TCmdStrListItem(aliasnames.first);
  4399. while assigned(item) do
  4400. begin
  4401. inc(aliasnamescount);
  4402. item:=TCmdStrListItem(item.next);
  4403. end;
  4404. if aliasnamescount>255 then
  4405. internalerror(200711021);
  4406. ppufile.putbyte(aliasnamescount);
  4407. item:=TCmdStrListItem(aliasnames.first);
  4408. while assigned(item) do
  4409. begin
  4410. ppufile.putstring(item.str);
  4411. item:=TCmdStrListItem(item.next);
  4412. end;
  4413. ppufile.do_crc:=oldintfcrc;
  4414. { write this entry }
  4415. writeentry(ppufile,ibprocdef);
  4416. { Save the para symtable, this is taken from the interface }
  4417. tparasymtable(parast).ppuwrite(ppufile);
  4418. { save localsymtable for inline procedures or when local
  4419. browser info is requested, this has no influence on the crc }
  4420. if has_inlininginfo then
  4421. begin
  4422. oldintfcrc:=ppufile.do_crc;
  4423. ppufile.do_crc:=false;
  4424. tlocalsymtable(localst).ppuwrite(ppufile);
  4425. ppufile.do_crc:=oldintfcrc;
  4426. end;
  4427. { node tree for inlining }
  4428. oldintfcrc:=ppufile.do_crc;
  4429. ppufile.do_crc:=false;
  4430. if has_inlininginfo then
  4431. ppuwritenodetree(ppufile,inlininginfo^.code);
  4432. ppufile.do_crc:=oldintfcrc;
  4433. end;
  4434. function tprocdef.fullprocname(showhidden:boolean):string;
  4435. var
  4436. pno: tprocnameoptions;
  4437. begin
  4438. pno:=[];
  4439. if showhidden then
  4440. include(pno,pno_showhidden);
  4441. result:=customprocname(pno);
  4442. end;
  4443. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4444. var
  4445. s, rn : ansistring;
  4446. t : ttoken;
  4447. begin
  4448. {$ifdef EXTDEBUG}
  4449. include(pno,pno_showhidden);
  4450. {$endif EXTDEBUG}
  4451. s:='';
  4452. if proctypeoption=potype_operator then
  4453. begin
  4454. for t:=NOTOKEN to last_overloaded do
  4455. if procsym.realname='$'+overloaded_names[t] then
  4456. begin
  4457. s:='operator ';
  4458. if (pno_ownername in pno) and
  4459. assigned(struct) then
  4460. s:=s+struct.RttiName+'.';
  4461. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  4462. break;
  4463. end;
  4464. end
  4465. else
  4466. begin
  4467. if (po_classmethod in procoptions) and
  4468. not(pno_noclassmarker in pno) then
  4469. s:='class ';
  4470. case proctypeoption of
  4471. potype_constructor,
  4472. potype_class_constructor:
  4473. s:=s+'constructor ';
  4474. potype_class_destructor,
  4475. potype_destructor:
  4476. s:=s+'destructor ';
  4477. else
  4478. if (pno_proctypeoption in pno) then
  4479. begin
  4480. if assigned(returndef) and
  4481. not(is_void(returndef)) then
  4482. s:=s+'function '
  4483. else
  4484. s:=s+'procedure ';
  4485. end;
  4486. end;
  4487. if (pno_ownername in pno) and
  4488. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  4489. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  4490. rn:=procsym.realname;
  4491. if (pno_noleadingdollar in pno) and
  4492. (rn[1]='$') then
  4493. delete(rn,1,1);
  4494. s:=s+rn+typename_paras(pno);
  4495. end;
  4496. if not(proctypeoption in [potype_constructor,potype_destructor,
  4497. potype_class_constructor,potype_class_destructor]) and
  4498. assigned(returndef) and
  4499. not(is_void(returndef)) then
  4500. s:=s+':'+returndef.GetTypeName;
  4501. if owner.symtabletype=localsymtable then
  4502. s:=s+' is nested'
  4503. else if po_is_block in procoptions then
  4504. s:=s+' is block';
  4505. s:=s+';';
  4506. { forced calling convention? }
  4507. if (po_hascallingconvention in procoptions) then
  4508. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  4509. if (po_staticmethod in procoptions) and
  4510. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  4511. s:=s+' Static;';
  4512. if pno_mangledname in pno then
  4513. s:=s+' -- mangled name: '+mangledname;
  4514. customprocname:=s;
  4515. end;
  4516. function tprocdef.is_methodpointer:boolean;
  4517. begin
  4518. { don't check assigned(_class), that's also the case for nested
  4519. procedures inside methods }
  4520. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  4521. end;
  4522. function tprocdef.is_addressonly:boolean;
  4523. begin
  4524. result:=assigned(owner) and
  4525. not is_methodpointer and
  4526. (not(m_nested_procvars in current_settings.modeswitches) or
  4527. not is_nested_pd(self));
  4528. end;
  4529. procedure tprocdef.make_external;
  4530. begin
  4531. include(procoptions,po_external);
  4532. forwarddef:=false;
  4533. end;
  4534. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  4535. begin
  4536. case t of
  4537. gs_local :
  4538. GetSymtable:=localst;
  4539. gs_para :
  4540. GetSymtable:=parast;
  4541. else
  4542. GetSymtable:=nil;
  4543. end;
  4544. end;
  4545. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  4546. var
  4547. j : longint;
  4548. begin
  4549. result:=inherited getcopyas(newtyp,copytyp);
  4550. if newtyp=procvardef then
  4551. exit;
  4552. { don't copy mangled name, can be different }
  4553. tprocdef(result).messageinf:=messageinf;
  4554. tprocdef(result).dispid:=dispid;
  4555. if po_msgstr in procoptions then
  4556. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  4557. tprocdef(result).symoptions:=symoptions;
  4558. if assigned(deprecatedmsg) then
  4559. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  4560. { will have to be associated with appropriate procsym }
  4561. tprocdef(result).procsym:=nil;
  4562. { don't create aliases for bare copies, nor copy the funcretsym as
  4563. the function result parameter will be inserted again if necessary
  4564. (e.g. if the calling convention is changed) }
  4565. if copytyp<>pc_bareproc then
  4566. begin
  4567. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  4568. if assigned(funcretsym) then
  4569. begin
  4570. if funcretsym.owner=parast then
  4571. begin
  4572. j:=parast.symlist.indexof(funcretsym);
  4573. if j<0 then
  4574. internalerror(2011040606);
  4575. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  4576. end
  4577. else if funcretsym.owner=localst then
  4578. begin
  4579. { nothing to do, will be inserted for the new procdef while
  4580. parsing its body (by pdecsub.insert_funcret_local) }
  4581. end
  4582. else
  4583. internalerror(2011040605);
  4584. end;
  4585. end;
  4586. { will have to be associated with a new struct }
  4587. tprocdef(result).struct:=nil;
  4588. if assigned(implprocdefinfo) then
  4589. begin
  4590. if assigned(resultname) then
  4591. tprocdef(result).resultname:=stringdup(resultname^);
  4592. end;
  4593. if assigned(import_dll) then
  4594. tprocdef(result).import_dll:=stringdup(import_dll^);
  4595. if assigned(import_name) then
  4596. tprocdef(result).import_name:=stringdup(import_name^);
  4597. tprocdef(result).import_nr:=import_nr;
  4598. tprocdef(result).extnumber:=$ffff;
  4599. tprocdef(result).visibility:=visibility;
  4600. tprocdef(result).synthetickind:=synthetickind;
  4601. { we need a separate implementation for the copied def }
  4602. tprocdef(result).forwarddef:=true;
  4603. tprocdef(result).interfacedef:=true;
  4604. { create new paralist }
  4605. tprocdef(result).calcparas;
  4606. end;
  4607. function tprocdef.getcopy: tstoreddef;
  4608. begin
  4609. result:=getcopyas(procdef,pc_normal);
  4610. end;
  4611. procedure tprocdef.buildderef;
  4612. begin
  4613. inherited buildderef;
  4614. structderef.build(struct);
  4615. { procsym that originaly defined this definition, should be in the
  4616. same symtable }
  4617. procsymderef.build(procsym);
  4618. end;
  4619. procedure tprocdef.buildderefimpl;
  4620. begin
  4621. inherited buildderefimpl;
  4622. { inline tree }
  4623. if has_inlininginfo then
  4624. begin
  4625. { Localst is not available for main/unit init }
  4626. if assigned(localst) then
  4627. begin
  4628. tlocalsymtable(localst).buildderef;
  4629. tlocalsymtable(localst).buildderefimpl;
  4630. end;
  4631. funcretsymderef.build(funcretsym);
  4632. inlininginfo^.code.buildderefimpl;
  4633. end;
  4634. end;
  4635. procedure tprocdef.deref;
  4636. begin
  4637. inherited deref;
  4638. struct:=tabstractrecorddef(structderef.resolve);
  4639. { procsym that originaly defined this definition, should be in the
  4640. same symtable }
  4641. procsym:=tprocsym(procsymderef.resolve);
  4642. end;
  4643. procedure tprocdef.derefimpl;
  4644. begin
  4645. { Enable has_inlininginfo when the inlininginfo
  4646. structure is available. The has_inlininginfo was disabled
  4647. after the load, since the data was invalid }
  4648. if assigned(inlininginfo) then
  4649. has_inlininginfo:=true;
  4650. { Inline }
  4651. if has_inlininginfo then
  4652. begin
  4653. { Locals }
  4654. if assigned(localst) then
  4655. begin
  4656. tlocalsymtable(localst).deref;
  4657. tlocalsymtable(localst).derefimpl;
  4658. end;
  4659. inlininginfo^.code.derefimpl;
  4660. { funcretsym, this is always located in the localst }
  4661. funcretsym:=tsym(funcretsymderef.resolve);
  4662. end
  4663. else
  4664. begin
  4665. { safety }
  4666. { Not safe! A unit may be reresolved after its interface has been
  4667. parsed but before its implementation has been parsed, and in that
  4668. case the funcretsym is still required!
  4669. funcretsym:=nil; }
  4670. end;
  4671. end;
  4672. function tprocdef.GetTypeName : string;
  4673. begin
  4674. GetTypeName := FullProcName(false);
  4675. end;
  4676. function tprocdef.mangledname : TSymStr;
  4677. begin
  4678. {$ifdef symansistr}
  4679. if _mangledname='' then
  4680. begin
  4681. result:=defaultmangledname;
  4682. _mangledname:=result;
  4683. end
  4684. else
  4685. result:=_mangledname;
  4686. {$else symansistr}
  4687. if not assigned(_mangledname) then
  4688. begin
  4689. result:=defaultmangledname;
  4690. _mangledname:=stringdup(mangledname);
  4691. end
  4692. else
  4693. result:=_mangledname^;
  4694. {$endif symansistr}
  4695. end;
  4696. function tprocdef.defaultmangledname: TSymStr;
  4697. var
  4698. hp : TParavarsym;
  4699. hs : TSymStr;
  4700. crc : dword;
  4701. newlen,
  4702. oldlen,
  4703. i : integer;
  4704. begin
  4705. hp:=nil;
  4706. { we need to use the symtable where the procsym is inserted,
  4707. because that is visible to the world }
  4708. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4709. oldlen:=length(defaultmangledname);
  4710. { add parameter types }
  4711. for i:=0 to paras.count-1 do
  4712. begin
  4713. hp:=tparavarsym(paras[i]);
  4714. if not(vo_is_hidden_para in hp.varoptions) then
  4715. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4716. end;
  4717. { add resultdef, add $$ as separator to make it unique from a
  4718. parameter separator }
  4719. if not is_void(returndef) then
  4720. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4721. newlen:=length(defaultmangledname);
  4722. { Replace with CRC if the parameter line is very long }
  4723. if (newlen-oldlen>12) and
  4724. ((newlen>100) or (newlen-oldlen>64)) then
  4725. begin
  4726. crc:=0;
  4727. for i:=0 to paras.count-1 do
  4728. begin
  4729. hp:=tparavarsym(paras[i]);
  4730. if not(vo_is_hidden_para in hp.varoptions) then
  4731. begin
  4732. hs:=hp.vardef.mangledparaname;
  4733. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4734. end;
  4735. end;
  4736. hs:=hp.vardef.mangledparaname;
  4737. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4738. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4739. end;
  4740. end;
  4741. function tprocdef.cplusplusmangledname : TSymStr;
  4742. function getcppparaname(p : tdef) : TSymStr;
  4743. const
  4744. {$ifdef NAMEMANGLING_GCC2}
  4745. ordtype2str : array[tordtype] of string[2] = (
  4746. '',
  4747. 'Uc','Us','Ui','Us',
  4748. 'Sc','s','i','x',
  4749. 'b','b','b','b','b',
  4750. 'c','w','x');
  4751. {$else NAMEMANGLING_GCC2}
  4752. ordtype2str : array[tordtype] of string[1] = (
  4753. 'v',
  4754. 'h','t','j','y',
  4755. 'a','s','i','x',
  4756. 'b','b','b','b',
  4757. 'b','b','b','b',
  4758. 'c','w','x');
  4759. floattype2str : array[tfloattype] of string[1] = (
  4760. 'f','d','e','e',
  4761. 'd','d','g');
  4762. {$endif NAMEMANGLING_GCC2}
  4763. var
  4764. s : TSymStr;
  4765. begin
  4766. case p.typ of
  4767. orddef:
  4768. s:=ordtype2str[torddef(p).ordtype];
  4769. pointerdef:
  4770. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4771. {$ifndef NAMEMANGLING_GCC2}
  4772. floatdef:
  4773. s:=floattype2str[tfloatdef(p).floattype];
  4774. {$endif NAMEMANGLING_GCC2}
  4775. else
  4776. internalerror(2103001);
  4777. end;
  4778. getcppparaname:=s;
  4779. end;
  4780. var
  4781. s,s2 : TSymStr;
  4782. hp : TParavarsym;
  4783. i : integer;
  4784. begin
  4785. {$ifdef NAMEMANGLING_GCC2}
  4786. { outdated gcc 2.x name mangling scheme }
  4787. s := procsym.realname;
  4788. if procsym.owner.symtabletype=ObjectSymtable then
  4789. begin
  4790. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4791. case proctypeoption of
  4792. potype_destructor:
  4793. s:='_$_'+tostr(length(s2))+s2;
  4794. potype_constructor:
  4795. s:='___'+tostr(length(s2))+s2;
  4796. else
  4797. s:='_'+s+'__'+tostr(length(s2))+s2;
  4798. end;
  4799. end
  4800. else s:=s+'__';
  4801. s:=s+'F';
  4802. { concat modifiers }
  4803. { !!!!! }
  4804. { now we handle the parameters }
  4805. if maxparacount>0 then
  4806. begin
  4807. for i:=0 to paras.count-1 do
  4808. begin
  4809. hp:=tparavarsym(paras[i]);
  4810. { no hidden parameters form part of a C++ mangled name:
  4811. a) self is not included
  4812. b) there are no "high" or other hidden parameters
  4813. }
  4814. if vo_is_hidden_para in hp.varoptions then
  4815. continue;
  4816. s2:=getcppparaname(hp.vardef);
  4817. if hp.varspez in [vs_var,vs_out] then
  4818. s2:='R'+s2;
  4819. s:=s+s2;
  4820. end;
  4821. end
  4822. else
  4823. s:=s+'v';
  4824. cplusplusmangledname:=s;
  4825. {$else NAMEMANGLING_GCC2}
  4826. { gcc 3.x and 4.x name mangling scheme }
  4827. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4828. if procsym.owner.symtabletype=ObjectSymtable then
  4829. begin
  4830. s:='_ZN';
  4831. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4832. s:=s+tostr(length(s2))+s2;
  4833. case proctypeoption of
  4834. potype_constructor:
  4835. s:=s+'C1';
  4836. potype_destructor:
  4837. s:=s+'D1';
  4838. else
  4839. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4840. end;
  4841. s:=s+'E';
  4842. end
  4843. else
  4844. s:=procsym.realname;
  4845. { now we handle the parameters }
  4846. if maxparacount>0 then
  4847. begin
  4848. for i:=0 to paras.count-1 do
  4849. begin
  4850. hp:=tparavarsym(paras[i]);
  4851. { no hidden parameters form part of a C++ mangled name:
  4852. a) self is not included
  4853. b) there are no "high" or other hidden parameters
  4854. }
  4855. if vo_is_hidden_para in hp.varoptions then
  4856. continue;
  4857. s2:=getcppparaname(hp.vardef);
  4858. if hp.varspez in [vs_var,vs_out] then
  4859. s2:='R'+s2;
  4860. s:=s+s2;
  4861. end;
  4862. end
  4863. else
  4864. s:=s+'v';
  4865. cplusplusmangledname:=s;
  4866. {$endif NAMEMANGLING_GCC2}
  4867. end;
  4868. function tprocdef.objcmangledname : TSymStr;
  4869. var
  4870. manglednamelen: longint;
  4871. iscatmethod : boolean;
  4872. begin
  4873. if not (po_msgstr in procoptions) then
  4874. internalerror(2009030901);
  4875. { we may very well need longer strings to handle these... }
  4876. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  4877. length('+"[ ]"')+length(messageinf.str^);
  4878. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  4879. if (iscatmethod) then
  4880. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  4881. if manglednamelen>255 then
  4882. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  4883. if not(po_classmethod in procoptions) then
  4884. result:='"-['
  4885. else
  4886. result:='"+[';
  4887. { quotes are necessary because the +/- otherwise confuse the assembler
  4888. into expecting a number
  4889. }
  4890. if iscatmethod then
  4891. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  4892. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  4893. if iscatmethod then
  4894. result:=result+')';
  4895. result:=result+' '+messageinf.str^+']"';
  4896. end;
  4897. procedure tprocdef.setmangledname(const s : TSymStr);
  4898. begin
  4899. { This is not allowed anymore, the forward declaration
  4900. already needs to create the correct mangledname, no changes
  4901. afterwards are allowed (PFV) }
  4902. { Exception: interface definitions in mode macpas, since in that }
  4903. { case no reference to the old name can exist yet (JM) }
  4904. {$ifdef symansistr}
  4905. if _mangledname<>'' then
  4906. if ((m_mac in current_settings.modeswitches) and
  4907. (interfacedef)) then
  4908. _mangledname:=''
  4909. else
  4910. internalerror(200411171);
  4911. {$else symansistr}
  4912. if assigned(_mangledname) then
  4913. if ((m_mac in current_settings.modeswitches) and
  4914. (interfacedef)) then
  4915. stringdispose(_mangledname)
  4916. else
  4917. internalerror(200411171);
  4918. {$endif symansistr}
  4919. {$ifdef jvm}
  4920. { this routine can be called for compilerproces. can't set mangled
  4921. name since it must be calculated, but it uses import_name when set
  4922. -> set that one }
  4923. import_name:=stringdup(s);
  4924. include(procoptions,po_has_importname);
  4925. include(procoptions,po_has_mangledname);
  4926. {$else}
  4927. {$ifdef symansistr}
  4928. _mangledname:=s;
  4929. {$else symansistr}
  4930. _mangledname:=stringdup(s);
  4931. {$endif symansistr}
  4932. {$endif jvm}
  4933. include(procoptions,po_has_mangledname);
  4934. end;
  4935. {***************************************************************************
  4936. TPROCVARDEF
  4937. ***************************************************************************}
  4938. constructor tprocvardef.create(level:byte);
  4939. begin
  4940. inherited create(procvardef,level);
  4941. end;
  4942. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  4943. begin
  4944. inherited ppuload(procvardef,ppufile);
  4945. { load para symtable }
  4946. parast:=tparasymtable.create(self,ppufile.getbyte);
  4947. ppuload_platform(ppufile);
  4948. tparasymtable(parast).ppuload(ppufile);
  4949. end;
  4950. function tprocvardef.getcopy : tstoreddef;
  4951. var
  4952. i : tcallercallee;
  4953. j : longint;
  4954. begin
  4955. result:=cprocvardef.create(parast.symtablelevel);
  4956. tprocvardef(result).returndef:=returndef;
  4957. tprocvardef(result).returndefderef:=returndefderef;
  4958. tprocvardef(result).parast:=parast.getcopy;
  4959. tprocvardef(result).savesize:=savesize;
  4960. { create paralist copy }
  4961. calcparas;
  4962. tprocvardef(result).paras:=tparalist.create(false);
  4963. tprocvardef(result).paras.count:=paras.count;
  4964. for j:=0 to paras.count-1 do
  4965. tprocvardef(result).paras[j]:=paras[j];
  4966. tprocvardef(result).proctypeoption:=proctypeoption;
  4967. tprocvardef(result).proccalloption:=proccalloption;
  4968. tprocvardef(result).procoptions:=procoptions;
  4969. tprocvardef(result).callerargareasize:=callerargareasize;
  4970. tprocvardef(result).calleeargareasize:=calleeargareasize;
  4971. tprocvardef(result).maxparacount:=maxparacount;
  4972. tprocvardef(result).minparacount:=minparacount;
  4973. for i:=low(tcallercallee) to high(tcallercallee) do
  4974. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  4975. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  4976. {$ifdef m68k}
  4977. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  4978. {$endif}
  4979. end;
  4980. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  4981. begin
  4982. inherited ppuwrite(ppufile);
  4983. { Save the para symtable level (necessary to distinguish nested
  4984. procvars) }
  4985. ppufile.putbyte(parast.symtablelevel);
  4986. { Write this entry }
  4987. writeentry(ppufile,ibprocvardef);
  4988. { Save the para symtable, this is taken from the interface }
  4989. tparasymtable(parast).ppuwrite(ppufile);
  4990. end;
  4991. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  4992. begin
  4993. case t of
  4994. gs_para :
  4995. GetSymtable:=parast;
  4996. else
  4997. GetSymtable:=nil;
  4998. end;
  4999. end;
  5000. function tprocvardef.size : asizeint;
  5001. begin
  5002. if ((po_methodpointer in procoptions) or
  5003. is_nested_pd(self)) and
  5004. not(po_addressonly in procoptions) then
  5005. begin
  5006. if is_nested_pd(self) then
  5007. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5008. else
  5009. size:=voidcodepointertype.size+voidpointertype.size;
  5010. end
  5011. else
  5012. size:=voidcodepointertype.size;
  5013. end;
  5014. function tprocvardef.is_methodpointer:boolean;
  5015. begin
  5016. result:=(po_methodpointer in procoptions);
  5017. end;
  5018. function tprocvardef.is_addressonly:boolean;
  5019. begin
  5020. result:=(not(po_methodpointer in procoptions) and
  5021. not is_nested_pd(self)) or
  5022. (po_addressonly in procoptions);
  5023. end;
  5024. function tprocvardef.getmangledparaname:TSymStr;
  5025. begin
  5026. if not(po_methodpointer in procoptions) then
  5027. if not is_nested_pd(self) then
  5028. result:='procvar'
  5029. else
  5030. result:='nestedprovar'
  5031. else
  5032. result:='procvarofobj'
  5033. end;
  5034. function tprocvardef.is_publishable : boolean;
  5035. begin
  5036. is_publishable:=(po_methodpointer in procoptions);
  5037. end;
  5038. function tprocvardef.GetTypeName : string;
  5039. var
  5040. s: string;
  5041. pno : tprocnameoptions;
  5042. begin
  5043. {$ifdef EXTDEBUG}
  5044. pno:=[pno_showhidden];
  5045. {$else EXTDEBUG}
  5046. pno:=[];
  5047. {$endif EXTDEBUG}
  5048. s:='<';
  5049. if po_classmethod in procoptions then
  5050. s := s+'class method type of'
  5051. else
  5052. if po_addressonly in procoptions then
  5053. s := s+'address of'
  5054. else
  5055. s := s+'procedure variable type of';
  5056. if assigned(returndef) and
  5057. (returndef<>voidtype) then
  5058. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5059. else
  5060. s:=s+' procedure'+typename_paras(pno);
  5061. if po_methodpointer in procoptions then
  5062. s := s+' of object';
  5063. if is_nested_pd(self) then
  5064. s := s+' is nested';
  5065. { calling convention doesn't matter for blocks }
  5066. if po_is_block in procoptions then
  5067. GetTypeName := s+' is block;'
  5068. else
  5069. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5070. end;
  5071. {***************************************************************************
  5072. TOBJECTDEF
  5073. ***************************************************************************}
  5074. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  5075. begin
  5076. inherited create(n,objectdef);
  5077. fcurrent_dispid:=0;
  5078. objecttype:=ot;
  5079. childof:=nil;
  5080. if objecttype=odt_helper then
  5081. owner.includeoption(sto_has_helper);
  5082. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  5083. { create space for vmt !! }
  5084. vmtentries:=TFPList.Create;
  5085. vmt_offset:=0;
  5086. set_parent(c);
  5087. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5088. prepareguid;
  5089. { setup implemented interfaces }
  5090. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5091. ImplementedInterfaces:=TFPObjectList.Create(true)
  5092. else
  5093. ImplementedInterfaces:=nil;
  5094. writing_class_record_dbginfo:=false;
  5095. end;
  5096. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5097. var
  5098. i,
  5099. implintfcount : longint;
  5100. d, getterd : tderef;
  5101. ImplIntf : TImplementedInterface;
  5102. vmtentry : pvmtentry;
  5103. begin
  5104. inherited ppuload(objectdef,ppufile);
  5105. objecttype:=tobjecttyp(ppufile.getbyte);
  5106. objextname:=stringdup(ppufile.getstring);
  5107. { only used for external Objective-C classes/protocols }
  5108. if (objextname^='') then
  5109. stringdispose(objextname);
  5110. symtable:=tObjectSymtable.create(self,objrealname^,0);
  5111. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5112. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5113. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5114. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5115. vmt_offset:=ppufile.getlongint;
  5116. ppufile.getderef(childofderef);
  5117. { load guid }
  5118. iidstr:=nil;
  5119. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5120. begin
  5121. new(iidguid);
  5122. ppufile.getguid(iidguid^);
  5123. iidstr:=stringdup(ppufile.getstring);
  5124. end;
  5125. abstractcnt:=ppufile.getlongint;
  5126. if objecttype=odt_helper then
  5127. ppufile.getderef(extendeddefderef);
  5128. vmtentries:=TFPList.Create;
  5129. vmtentries.count:=ppufile.getlongint;
  5130. for i:=0 to vmtentries.count-1 do
  5131. begin
  5132. ppufile.getderef(d);
  5133. new(vmtentry);
  5134. vmtentry^.procdef:=nil;
  5135. vmtentry^.procdefderef:=d;
  5136. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5137. vmtentries[i]:=vmtentry;
  5138. end;
  5139. { load implemented interfaces }
  5140. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5141. begin
  5142. ImplementedInterfaces:=TFPObjectList.Create(true);
  5143. implintfcount:=ppufile.getlongint;
  5144. for i:=0 to implintfcount-1 do
  5145. begin
  5146. ppufile.getderef(d);
  5147. ppufile.getderef(getterd);
  5148. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5149. ImplIntf.IOffset:=ppufile.getlongint;
  5150. byte(ImplIntf.IType):=ppufile.getbyte;
  5151. ImplementedInterfaces.Add(ImplIntf);
  5152. end;
  5153. end
  5154. else
  5155. ImplementedInterfaces:=nil;
  5156. if df_copied_def in defoptions then
  5157. begin
  5158. ppufile.getderef(cloneddefderef);
  5159. ppuload_platform(ppufile);
  5160. end
  5161. else
  5162. begin
  5163. ppuload_platform(ppufile);
  5164. tObjectSymtable(symtable).ppuload(ppufile);
  5165. end;
  5166. { handles the predefined class tobject }
  5167. { the last TOBJECT which is loaded gets }
  5168. { it ! }
  5169. if (childof=nil) and
  5170. (objecttype in [odt_class,odt_javaclass]) and
  5171. (objname^='TOBJECT') then
  5172. class_tobject:=self;
  5173. if (childof=nil) and
  5174. (objecttype=odt_interfacecom) then
  5175. if (objname^='IUNKNOWN') then
  5176. interface_iunknown:=self
  5177. else
  5178. if (objname^='IDISPATCH') then
  5179. interface_idispatch:=self;
  5180. if (childof=nil) and
  5181. (objecttype=odt_objcclass) and
  5182. (objname^='PROTOCOL') then
  5183. objc_protocoltype:=self;
  5184. if (objecttype=odt_javaclass) and
  5185. not(oo_is_formal in objectoptions) then
  5186. begin
  5187. if (objname^='JLOBJECT') then
  5188. java_jlobject:=self
  5189. else if (objname^='JLTHROWABLE') then
  5190. java_jlthrowable:=self
  5191. else if (objname^='FPCBASERECORDTYPE') then
  5192. java_fpcbaserecordtype:=self
  5193. else if (objname^='JLSTRING') then
  5194. java_jlstring:=self
  5195. else if (objname^='ANSISTRINGCLASS') then
  5196. java_ansistring:=self
  5197. else if (objname^='SHORTSTRINGCLASS') then
  5198. java_shortstring:=self
  5199. else if (objname^='JLENUM') then
  5200. java_jlenum:=self
  5201. else if (objname^='JUENUMSET') then
  5202. java_juenumset:=self
  5203. else if (objname^='FPCBITSET') then
  5204. java_jubitset:=self
  5205. else if (objname^='FPCBASEPROCVARTYPE') then
  5206. java_procvarbase:=self;
  5207. end;
  5208. writing_class_record_dbginfo:=false;
  5209. end;
  5210. destructor tobjectdef.destroy;
  5211. begin
  5212. if assigned(symtable) then
  5213. begin
  5214. symtable.free;
  5215. symtable:=nil;
  5216. end;
  5217. stringdispose(objextname);
  5218. stringdispose(iidstr);
  5219. if assigned(ImplementedInterfaces) then
  5220. begin
  5221. ImplementedInterfaces.free;
  5222. ImplementedInterfaces:=nil;
  5223. end;
  5224. if assigned(iidguid) then
  5225. begin
  5226. dispose(iidguid);
  5227. iidguid:=nil;
  5228. end;
  5229. if assigned(vmtentries) then
  5230. begin
  5231. resetvmtentries;
  5232. vmtentries.free;
  5233. vmtentries:=nil;
  5234. end;
  5235. if assigned(vmcallstaticinfo) then
  5236. begin
  5237. freemem(vmcallstaticinfo);
  5238. vmcallstaticinfo:=nil;
  5239. end;
  5240. inherited destroy;
  5241. end;
  5242. function tobjectdef.getcopy : tstoreddef;
  5243. var
  5244. i : longint;
  5245. begin
  5246. result:=cobjectdef.create(objecttype,objrealname^,childof);
  5247. { the constructor allocates a symtable which we release to avoid memory leaks }
  5248. tobjectdef(result).symtable.free;
  5249. tobjectdef(result).symtable:=symtable.getcopy;
  5250. if assigned(objextname) then
  5251. tobjectdef(result).objextname:=stringdup(objextname^);
  5252. if assigned(import_lib) then
  5253. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5254. tobjectdef(result).objectoptions:=objectoptions;
  5255. include(tobjectdef(result).defoptions,df_copied_def);
  5256. tobjectdef(result).extendeddef:=extendeddef;
  5257. if assigned(tcinitcode) then
  5258. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5259. tobjectdef(result).vmt_offset:=vmt_offset;
  5260. if assigned(iidguid) then
  5261. begin
  5262. new(tobjectdef(result).iidguid);
  5263. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5264. end;
  5265. if assigned(iidstr) then
  5266. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5267. tobjectdef(result).abstractcnt:=abstractcnt;
  5268. if assigned(ImplementedInterfaces) then
  5269. begin
  5270. for i:=0 to ImplementedInterfaces.count-1 do
  5271. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5272. end;
  5273. if assigned(vmtentries) then
  5274. begin
  5275. tobjectdef(result).vmtentries:=TFPList.Create;
  5276. tobjectdef(result).copyvmtentries(self);
  5277. end;
  5278. end;
  5279. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5280. var
  5281. i : longint;
  5282. vmtentry : pvmtentry;
  5283. ImplIntf : TImplementedInterface;
  5284. old_do_indirect_crc: boolean;
  5285. begin
  5286. { if class1 in unit A changes, and class2 in unit B inherits from it
  5287. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5288. However, if there is also a class3 in unit C that only depends on
  5289. unit B, then unit C will not be recompiled because nothing changed
  5290. to the interface of unit B. Nevertheless, unit C can indirectly
  5291. depend on unit A via derefs, and these must be updated -> the
  5292. indirect crc keeps track of such changes. }
  5293. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5294. ppufile.do_indirect_crc:=true;
  5295. inherited ppuwrite(ppufile);
  5296. ppufile.putbyte(byte(objecttype));
  5297. if assigned(objextname) then
  5298. ppufile.putstring(objextname^)
  5299. else
  5300. ppufile.putstring('');
  5301. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5302. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5303. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5304. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5305. ppufile.putlongint(vmt_offset);
  5306. ppufile.putderef(childofderef);
  5307. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5308. begin
  5309. ppufile.putguid(iidguid^);
  5310. ppufile.putstring(iidstr^);
  5311. end;
  5312. ppufile.putlongint(abstractcnt);
  5313. if objecttype=odt_helper then
  5314. ppufile.putderef(extendeddefderef);
  5315. ppufile.putlongint(vmtentries.count);
  5316. for i:=0 to vmtentries.count-1 do
  5317. begin
  5318. vmtentry:=pvmtentry(vmtentries[i]);
  5319. ppufile.putderef(vmtentry^.procdefderef);
  5320. ppufile.putbyte(byte(vmtentry^.visibility));
  5321. end;
  5322. if assigned(ImplementedInterfaces) then
  5323. begin
  5324. ppufile.putlongint(ImplementedInterfaces.Count);
  5325. for i:=0 to ImplementedInterfaces.Count-1 do
  5326. begin
  5327. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5328. ppufile.putderef(ImplIntf.intfdefderef);
  5329. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5330. ppufile.putlongint(ImplIntf.Ioffset);
  5331. ppufile.putbyte(byte(ImplIntf.IType));
  5332. end;
  5333. end;
  5334. if df_copied_def in defoptions then
  5335. ppufile.putderef(cloneddefderef);
  5336. writeentry(ppufile,ibobjectdef);
  5337. if not(df_copied_def in defoptions) then
  5338. tObjectSymtable(symtable).ppuwrite(ppufile);
  5339. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5340. end;
  5341. function tobjectdef.GetTypeName:string;
  5342. begin
  5343. { in this case we will go in endless recursion, because then }
  5344. { there is no tsym associated yet with the def. It can occur }
  5345. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5346. { instead of the actual type name }
  5347. if not assigned(typesym) then
  5348. result:='<Currently Parsed Class>'
  5349. else
  5350. result:=typesymbolprettyname;
  5351. end;
  5352. procedure tobjectdef.buildderef;
  5353. var
  5354. i : longint;
  5355. vmtentry : pvmtentry;
  5356. begin
  5357. inherited buildderef;
  5358. childofderef.build(childof);
  5359. if df_copied_def in defoptions then
  5360. cloneddefderef.build(symtable.defowner)
  5361. else
  5362. tstoredsymtable(symtable).buildderef;
  5363. if objecttype=odt_helper then
  5364. extendeddefderef.build(extendeddef);
  5365. for i:=0 to vmtentries.count-1 do
  5366. begin
  5367. vmtentry:=pvmtentry(vmtentries[i]);
  5368. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5369. end;
  5370. if assigned(ImplementedInterfaces) then
  5371. begin
  5372. for i:=0 to ImplementedInterfaces.count-1 do
  5373. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5374. end;
  5375. end;
  5376. procedure tobjectdef.deref;
  5377. var
  5378. i : longint;
  5379. vmtentry : pvmtentry;
  5380. begin
  5381. inherited deref;
  5382. childof:=tobjectdef(childofderef.resolve);
  5383. if df_copied_def in defoptions then
  5384. begin
  5385. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5386. symtable:=cloneddef.symtable.getcopy;
  5387. end
  5388. else
  5389. tstoredsymtable(symtable).deref;
  5390. if objecttype=odt_helper then
  5391. extendeddef:=tdef(extendeddefderef.resolve);
  5392. for i:=0 to vmtentries.count-1 do
  5393. begin
  5394. vmtentry:=pvmtentry(vmtentries[i]);
  5395. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5396. end;
  5397. if assigned(ImplementedInterfaces) then
  5398. begin
  5399. for i:=0 to ImplementedInterfaces.count-1 do
  5400. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5401. end;
  5402. end;
  5403. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5404. var
  5405. pd: tprocdef absolute def;
  5406. st: tsymtable;
  5407. psym: tsym;
  5408. nname: TIDString;
  5409. begin
  5410. if (tdef(def).typ<>procdef) then
  5411. exit;
  5412. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5413. owner = symtable in which objcclassdef is defined
  5414. }
  5415. st:=pd.owner.defowner.owner;
  5416. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5417. { check for an existing procsym with our special name }
  5418. psym:=tsym(st.find(nname));
  5419. if not assigned(psym) then
  5420. begin
  5421. psym:=cprocsym.create(nname);
  5422. { avoid warning about this symbol being unused }
  5423. psym.IncRefCount;
  5424. { don't check for duplicates:
  5425. a) we checked above
  5426. b) in case we are in the implementation section of a unit, this
  5427. will also check for this symbol in the interface section
  5428. (since you normally cannot have symbols with the same name
  5429. both interface and implementation), and it's possible to
  5430. have class helpers for the same class in the interface and
  5431. in the implementation, and they cannot be merged since only
  5432. the once in the interface must be saved to the ppu/visible
  5433. from other units }
  5434. st.insert(psym,false);
  5435. end
  5436. else if (psym.typ<>procsym) then
  5437. internalerror(2009111501);
  5438. { add ourselves to this special procsym }
  5439. tprocsym(psym).procdeflist.add(def);
  5440. end;
  5441. procedure tobjectdef.buildderefimpl;
  5442. begin
  5443. inherited buildderefimpl;
  5444. if not (df_copied_def in defoptions) then
  5445. tstoredsymtable(symtable).buildderefimpl;
  5446. end;
  5447. procedure tobjectdef.derefimpl;
  5448. begin
  5449. inherited derefimpl;
  5450. if not (df_copied_def in defoptions) then
  5451. tstoredsymtable(symtable).derefimpl;
  5452. { the procdefs are not owned by the class helper procsyms, so they
  5453. are not stored/restored either -> re-add them here }
  5454. if (objecttype=odt_objcclass) or
  5455. (oo_is_classhelper in objectoptions) then
  5456. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  5457. end;
  5458. procedure tobjectdef.resetvmtentries;
  5459. var
  5460. i : longint;
  5461. begin
  5462. for i:=0 to vmtentries.Count-1 do
  5463. Dispose(pvmtentry(vmtentries[i]));
  5464. vmtentries.clear;
  5465. end;
  5466. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  5467. var
  5468. i : longint;
  5469. vmtentry : pvmtentry;
  5470. begin
  5471. resetvmtentries;
  5472. vmtentries.count:=objdef.vmtentries.count;
  5473. for i:=0 to objdef.vmtentries.count-1 do
  5474. begin
  5475. new(vmtentry);
  5476. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  5477. vmtentries[i]:=vmtentry;
  5478. end;
  5479. end;
  5480. function tobjectdef.getparentdef:tdef;
  5481. begin
  5482. { TODO: Remove getparentdef hack}
  5483. { With 2 forward declared classes with the child class before the
  5484. parent class the child class is written earlier to the ppu. Leaving it
  5485. possible to have a reference to the parent class for property overriding,
  5486. but the parent class still has the childof not resolved yet (PFV) }
  5487. if childof=nil then
  5488. childof:=tobjectdef(childofderef.resolve);
  5489. result:=childof;
  5490. end;
  5491. procedure tobjectdef.prepareguid;
  5492. begin
  5493. { set up guid }
  5494. if not assigned(iidguid) then
  5495. begin
  5496. new(iidguid);
  5497. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5498. end;
  5499. { setup iidstring }
  5500. if not assigned(iidstr) then
  5501. iidstr:=stringdup(''); { default is empty string }
  5502. end;
  5503. procedure tobjectdef.set_parent( c : tobjectdef);
  5504. begin
  5505. if assigned(childof) then
  5506. exit;
  5507. childof:=c;
  5508. if not assigned(c) then
  5509. exit;
  5510. { inherit options and status }
  5511. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5512. { initially has the same number of abstract methods as the parent }
  5513. abstractcnt:=c.abstractcnt;
  5514. { add the data of the anchestor class/object }
  5515. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5516. begin
  5517. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5518. { inherit recordalignment }
  5519. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5520. { if both the parent and this record use C-alignment, also inherit
  5521. the current field alignment }
  5522. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5523. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5524. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5525. { the padding is not inherited for Objective-C classes (maybe not
  5526. for cppclass either?) }
  5527. if objecttype=odt_objcclass then
  5528. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5529. if (oo_has_vmt in objectoptions) and
  5530. (oo_has_vmt in c.objectoptions) then
  5531. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5532. { if parent has a vmt field then the offset is the same for the child PM }
  5533. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5534. begin
  5535. vmt_offset:=c.vmt_offset;
  5536. include(objectoptions,oo_has_vmt);
  5537. end;
  5538. end;
  5539. end;
  5540. procedure tobjectdef.insertvmt;
  5541. var
  5542. vs: tfieldvarsym;
  5543. begin
  5544. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5545. exit;
  5546. if (oo_has_vmt in objectoptions) then
  5547. internalerror(12345)
  5548. else
  5549. begin
  5550. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5551. tObjectSymtable(symtable).fieldalignment);
  5552. if (tf_requires_proper_alignment in target_info.flags) then
  5553. begin
  5554. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5555. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5556. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5557. end;
  5558. vs:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5559. hidesym(vs);
  5560. tObjectSymtable(symtable).insert(vs);
  5561. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5562. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5563. vmt_offset:=vs.fieldoffset
  5564. else
  5565. vmt_offset:=vs.fieldoffset div 8;
  5566. include(objectoptions,oo_has_vmt);
  5567. end;
  5568. end;
  5569. procedure tobjectdef.check_forwards;
  5570. begin
  5571. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5572. inherited;
  5573. if (oo_is_forward in objectoptions) then
  5574. begin
  5575. { ok, in future, the forward can be resolved }
  5576. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5577. exclude(objectoptions,oo_is_forward);
  5578. end;
  5579. end;
  5580. function tobjectdef.find_destructor: tprocdef;
  5581. var
  5582. objdef: tobjectdef;
  5583. begin
  5584. objdef:=self;
  5585. while assigned(objdef) do
  5586. begin
  5587. result:=objdef.find_procdef_bytype(potype_destructor);
  5588. if assigned(result) then
  5589. exit;
  5590. objdef:=objdef.childof;
  5591. end;
  5592. result:=nil;
  5593. end;
  5594. function tobjectdef.implements_any_interfaces: boolean;
  5595. begin
  5596. result := (ImplementedInterfaces.Count > 0) or
  5597. (assigned(childof) and childof.implements_any_interfaces);
  5598. end;
  5599. function tobjectdef.size : asizeint;
  5600. begin
  5601. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5602. result:=voidpointertype.size
  5603. else
  5604. result:=tObjectSymtable(symtable).datasize;
  5605. end;
  5606. function tobjectdef.alignment:shortint;
  5607. begin
  5608. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5609. alignment:=voidpointertype.size
  5610. else
  5611. alignment:=tObjectSymtable(symtable).recordalignment;
  5612. end;
  5613. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5614. begin
  5615. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5616. case objecttype of
  5617. odt_class:
  5618. { the +2*sizeof(pint) is size and -size }
  5619. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  5620. odt_helper,
  5621. odt_objcclass,
  5622. odt_objcprotocol:
  5623. vmtmethodoffset:=0;
  5624. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5625. vmtmethodoffset:=index*voidcodepointertype.size;
  5626. odt_javaclass,
  5627. odt_interfacejava:
  5628. { invalid }
  5629. vmtmethodoffset:=-1;
  5630. else
  5631. { the +2*sizeof(pint) is size and -size }
  5632. {$ifdef WITHDMT}
  5633. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  5634. {$else WITHDMT}
  5635. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  5636. {$endif WITHDMT}
  5637. end;
  5638. end;
  5639. function tobjectdef.vmt_mangledname : TSymStr;
  5640. begin
  5641. if not(oo_has_vmt in objectoptions) then
  5642. Message1(parser_n_object_has_no_vmt,objrealname^);
  5643. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5644. end;
  5645. function tobjectdef.needs_inittable : boolean;
  5646. var
  5647. hp : tobjectdef;
  5648. begin
  5649. case objecttype of
  5650. odt_helper,
  5651. odt_class :
  5652. needs_inittable:=false;
  5653. odt_dispinterface,
  5654. odt_interfacecom:
  5655. needs_inittable:=true;
  5656. odt_interfacecorba:
  5657. begin
  5658. hp:=childof;
  5659. while assigned(hp) do
  5660. begin
  5661. if hp=interface_iunknown then
  5662. begin
  5663. needs_inittable:=true;
  5664. exit;
  5665. end;
  5666. hp:=hp.childof;
  5667. end;
  5668. needs_inittable:=false;
  5669. end;
  5670. odt_object:
  5671. needs_inittable:=
  5672. tObjectSymtable(symtable).needs_init_final or
  5673. (assigned(childof) and
  5674. childof.needs_inittable);
  5675. odt_cppclass,
  5676. odt_objcclass,
  5677. odt_objcprotocol,
  5678. odt_javaclass,
  5679. odt_interfacejava:
  5680. needs_inittable:=false;
  5681. else
  5682. internalerror(200108267);
  5683. end;
  5684. end;
  5685. function tobjectdef.needs_separate_initrtti : boolean;
  5686. begin
  5687. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5688. end;
  5689. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5690. begin
  5691. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5692. result:=inherited rtti_mangledname(rt)
  5693. else
  5694. begin
  5695. { necessary in case of a dynamic array of nsobject, or
  5696. if an nsobject field appears in a record that needs
  5697. init/finalisation }
  5698. if rt=initrtti then
  5699. begin
  5700. result:=voidpointertype.rtti_mangledname(rt);
  5701. exit;
  5702. end;
  5703. if not(target_info.system in systems_objc_nfabi) then
  5704. begin
  5705. result:=target_asm.labelprefix;
  5706. case objecttype of
  5707. odt_objcclass:
  5708. begin
  5709. case rt of
  5710. objcclassrtti:
  5711. if not(oo_is_classhelper in objectoptions) then
  5712. result:=result+'_OBJC_CLASS_'
  5713. else
  5714. result:=result+'_OBJC_CATEGORY_';
  5715. objcmetartti:
  5716. if not(oo_is_classhelper in objectoptions) then
  5717. result:=result+'_OBJC_METACLASS_'
  5718. else
  5719. internalerror(2009111511);
  5720. else
  5721. internalerror(2009092302);
  5722. end;
  5723. end;
  5724. odt_objcprotocol:
  5725. result:=result+'_OBJC_PROTOCOL_';
  5726. end;
  5727. end
  5728. else
  5729. begin
  5730. case objecttype of
  5731. odt_objcclass:
  5732. begin
  5733. if (oo_is_classhelper in objectoptions) and
  5734. (rt<>objcclassrtti) then
  5735. internalerror(2009111512);
  5736. case rt of
  5737. objcclassrtti:
  5738. if not(oo_is_classhelper in objectoptions) then
  5739. result:='_OBJC_CLASS_$_'
  5740. else
  5741. result:='_OBJC_$_CATEGORY_';
  5742. objcmetartti:
  5743. result:='_OBJC_METACLASS_$_';
  5744. objcclassrortti:
  5745. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5746. objcmetarortti:
  5747. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5748. else
  5749. internalerror(2009092303);
  5750. end;
  5751. end;
  5752. odt_objcprotocol:
  5753. begin
  5754. result:=lower(target_asm.labelprefix);
  5755. case rt of
  5756. objcclassrtti:
  5757. result:=result+'_OBJC_PROTOCOL_$_';
  5758. objcmetartti:
  5759. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5760. else
  5761. internalerror(2009092501);
  5762. end;
  5763. end;
  5764. else
  5765. internalerror(2013113005);
  5766. end;
  5767. end;
  5768. result:=result+objextname^;
  5769. end;
  5770. end;
  5771. function tobjectdef.members_need_inittable : boolean;
  5772. begin
  5773. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5774. end;
  5775. function tobjectdef.is_publishable : boolean;
  5776. begin
  5777. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5778. end;
  5779. function tobjectdef.get_next_dispid: longint;
  5780. begin
  5781. inc(fcurrent_dispid);
  5782. result:=fcurrent_dispid;
  5783. end;
  5784. function tobjectdef.search_enumerator_get: tprocdef;
  5785. begin
  5786. result:=inherited;
  5787. if not assigned(result) and assigned(childof) then
  5788. result:=childof.search_enumerator_get;
  5789. end;
  5790. function tobjectdef.search_enumerator_move: tprocdef;
  5791. begin
  5792. result:=inherited;
  5793. if not assigned(result) and assigned(childof) then
  5794. result:=childof.search_enumerator_move;
  5795. end;
  5796. function tobjectdef.search_enumerator_current: tsym;
  5797. begin
  5798. result:=inherited;
  5799. if not assigned(result) and assigned(childof) then
  5800. result:=childof.search_enumerator_current;
  5801. end;
  5802. procedure tobjectdef.register_created_classref_type;
  5803. begin
  5804. if not classref_created_in_current_module then
  5805. begin
  5806. classref_created_in_current_module:=true;
  5807. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5808. end;
  5809. end;
  5810. procedure tobjectdef.register_created_object_type;
  5811. begin
  5812. if not created_in_current_module then
  5813. begin
  5814. created_in_current_module:=true;
  5815. current_module.wpoinfo.addcreatedobjtype(self);
  5816. end;
  5817. end;
  5818. procedure tobjectdef.register_maybe_created_object_type;
  5819. begin
  5820. { if we know it has been created for sure, no need
  5821. to also record that it maybe can be created in
  5822. this module
  5823. }
  5824. if not (created_in_current_module) and
  5825. not (maybe_created_in_current_module) then
  5826. begin
  5827. maybe_created_in_current_module:=true;
  5828. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5829. end;
  5830. end;
  5831. procedure tobjectdef.register_vmt_call(index: longint);
  5832. begin
  5833. if (is_object(self) or is_class(self)) then
  5834. current_module.wpoinfo.addcalledvmtentry(self,index);
  5835. end;
  5836. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5837. var
  5838. def: tdef absolute data;
  5839. pd: tprocdef absolute data;
  5840. i,
  5841. paracount: longint;
  5842. begin
  5843. if (def.typ=procdef) then
  5844. begin
  5845. { add all messages also under a dummy name to the symtable in
  5846. which the objcclass/protocol/category is declared, so they can
  5847. be called via id.<name>
  5848. }
  5849. create_class_helper_for_procdef(pd,nil);
  5850. { we have to wait until now to set the mangled name because it
  5851. depends on the (possibly external) class name, which is defined
  5852. at the very end. }
  5853. if not(po_msgstr in pd.procoptions) then
  5854. begin
  5855. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5856. { recover to avoid internalerror later on }
  5857. include(pd.procoptions,po_msgstr);
  5858. pd.messageinf.str:=stringdup('MissingDeclaration');
  5859. end;
  5860. { Mangled name is already set in case this is a copy of
  5861. another type. }
  5862. if not(po_has_mangledname in pd.procoptions) then
  5863. begin
  5864. { check whether the number of formal parameters is correct,
  5865. and whether they have valid Objective-C types }
  5866. paracount:=0;
  5867. for i:=1 to length(pd.messageinf.str^) do
  5868. if pd.messageinf.str^[i]=':' then
  5869. inc(paracount);
  5870. for i:=0 to pd.paras.count-1 do
  5871. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5872. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5873. dec(paracount);
  5874. if (paracount<>0) then
  5875. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5876. pd.setmangledname(pd.objcmangledname);
  5877. end
  5878. else
  5879. { all checks already done }
  5880. exit;
  5881. if not(oo_is_external in pd.struct.objectoptions) then
  5882. begin
  5883. if (po_varargs in pd.procoptions) then
  5884. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  5885. else
  5886. begin
  5887. { check for "array of const" parameters }
  5888. for i:=0 to pd.parast.symlist.count-1 do
  5889. begin
  5890. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  5891. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  5892. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  5893. end;
  5894. end;
  5895. end;
  5896. end;
  5897. end;
  5898. procedure mark_private_fields_used(data: tobject; arg: pointer);
  5899. var
  5900. sym: tsym absolute data;
  5901. begin
  5902. if (sym.typ=fieldvarsym) and
  5903. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  5904. sym.IncRefCount;
  5905. end;
  5906. procedure tobjectdef.finish_objc_data;
  5907. begin
  5908. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  5909. if (oo_is_external in objectoptions) then
  5910. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  5911. end;
  5912. procedure verify_objc_vardef(data: tobject; arg: pointer);
  5913. var
  5914. sym: tabstractvarsym absolute data;
  5915. res: pboolean absolute arg;
  5916. founderrordef: tdef;
  5917. begin
  5918. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  5919. exit;
  5920. if (sym.typ=paravarsym) and
  5921. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  5922. is_array_of_const(tparavarsym(sym).vardef)) then
  5923. exit;
  5924. if not objcchecktype(sym.vardef,founderrordef) then
  5925. begin
  5926. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5927. res^:=false;
  5928. end;
  5929. end;
  5930. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  5931. var
  5932. def: tdef absolute data;
  5933. res: pboolean absolute arg;
  5934. founderrordef: tdef;
  5935. begin
  5936. if (def.typ<>procdef) then
  5937. exit;
  5938. { check parameter types for validity }
  5939. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  5940. { check the result type for validity }
  5941. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  5942. begin
  5943. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5944. res^:=false;
  5945. end;
  5946. end;
  5947. function tobjectdef.check_objc_types: boolean;
  5948. begin
  5949. { done in separate step from finish_objc_data, because when
  5950. finish_objc_data is called, not all forwarddefs have been resolved
  5951. yet and we need to know all types here }
  5952. result:=true;
  5953. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  5954. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  5955. end;
  5956. procedure do_cpp_import_info(data: tobject; arg: pointer);
  5957. var
  5958. def: tdef absolute data;
  5959. pd: tprocdef absolute data;
  5960. begin
  5961. if (def.typ=procdef) then
  5962. begin
  5963. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  5964. if (oo_is_external in pd.struct.objectoptions) then
  5965. begin
  5966. { copied from psub.read_proc }
  5967. if assigned(tobjectdef(pd.struct).import_lib) then
  5968. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  5969. else
  5970. begin
  5971. { add import name to external list for DLL scanning }
  5972. if tf_has_dllscanner in target_info.flags then
  5973. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  5974. end;
  5975. end;
  5976. end;
  5977. end;
  5978. procedure tobjectdef.finish_cpp_data;
  5979. begin
  5980. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  5981. end;
  5982. {****************************************************************************
  5983. TImplementedInterface
  5984. ****************************************************************************}
  5985. function TImplementedInterface.GetIOffset: longint;
  5986. begin
  5987. if (fIOffset=-1) and
  5988. (IType in [etFieldValue,etFieldValueClass]) then
  5989. result:=tfieldvarsym(ImplementsField).fieldoffset
  5990. else
  5991. result:=fIOffset;
  5992. end;
  5993. constructor TImplementedInterface.create(aintf: tobjectdef);
  5994. begin
  5995. inherited create;
  5996. intfdef:=aintf;
  5997. IOffset:=-1;
  5998. IType:=etStandard;
  5999. NameMappings:=nil;
  6000. procdefs:=nil;
  6001. end;
  6002. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  6003. begin
  6004. inherited create;
  6005. intfdef:=nil;
  6006. intfdefderef:=intfd;
  6007. ImplementsGetterDeref:=getterd;
  6008. IOffset:=-1;
  6009. IType:=etStandard;
  6010. NameMappings:=nil;
  6011. procdefs:=nil;
  6012. end;
  6013. destructor TImplementedInterface.destroy;
  6014. var
  6015. i : longint;
  6016. mappedname : pshortstring;
  6017. begin
  6018. if assigned(NameMappings) then
  6019. begin
  6020. for i:=0 to NameMappings.Count-1 do
  6021. begin
  6022. mappedname:=pshortstring(NameMappings[i]);
  6023. stringdispose(mappedname);
  6024. end;
  6025. NameMappings.free;
  6026. NameMappings:=nil;
  6027. end;
  6028. if assigned(procdefs) then
  6029. begin
  6030. procdefs.free;
  6031. procdefs:=nil;
  6032. end;
  6033. inherited destroy;
  6034. end;
  6035. procedure TImplementedInterface.buildderef;
  6036. begin
  6037. intfdefderef.build(intfdef);
  6038. ImplementsGetterDeref.build(ImplementsGetter);
  6039. end;
  6040. procedure TImplementedInterface.deref;
  6041. begin
  6042. intfdef:=tobjectdef(intfdefderef.resolve);
  6043. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6044. end;
  6045. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6046. begin
  6047. if not assigned(NameMappings) then
  6048. NameMappings:=TFPHashList.Create;
  6049. NameMappings.Add(origname,stringdup(newname));
  6050. end;
  6051. function TImplementedInterface.GetMapping(const origname: string):string;
  6052. var
  6053. mappedname : pshortstring;
  6054. begin
  6055. result:='';
  6056. if not assigned(NameMappings) then
  6057. exit;
  6058. mappedname:=PShortstring(NameMappings.Find(origname));
  6059. if assigned(mappedname) then
  6060. result:=mappedname^;
  6061. end;
  6062. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6063. begin
  6064. if not assigned(procdefs) then
  6065. procdefs:=TFPObjectList.Create(false);
  6066. { duplicate entries must be stored, because multiple }
  6067. { interfaces can declare methods with the same name }
  6068. { and all of these get their own VMT entry }
  6069. procdefs.Add(pd);
  6070. end;
  6071. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6072. var
  6073. i : longint;
  6074. begin
  6075. result:=false;
  6076. { interfaces being implemented through delegation are not mergable (FK) }
  6077. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6078. exit;
  6079. weight:=0;
  6080. { empty interface is mergeable }
  6081. if ProcDefs.Count=0 then
  6082. begin
  6083. result:=true;
  6084. exit;
  6085. end;
  6086. { The interface to merge must at least the number of
  6087. procedures of this interface }
  6088. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6089. exit;
  6090. for i:=0 to ProcDefs.Count-1 do
  6091. begin
  6092. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6093. exit;
  6094. end;
  6095. weight:=ProcDefs.Count;
  6096. result:=true;
  6097. end;
  6098. function TImplementedInterface.getcopy:TImplementedInterface;
  6099. begin
  6100. Result:=TImplementedInterface.Create(nil);
  6101. { 1) the procdefs list will be freed once for each copy
  6102. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6103. 3) idem for the name mappings
  6104. }
  6105. { warning: this is completely wrong on so many levels...
  6106. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6107. We need to make clean copies of the different fields
  6108. this is not implemented yet, and thus we generate an internal
  6109. error instead PM 2011-06-14 }
  6110. internalerror(2011061401);
  6111. end;
  6112. {****************************************************************************
  6113. TFORWARDDEF
  6114. ****************************************************************************}
  6115. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6116. begin
  6117. inherited create(forwarddef);
  6118. tosymname:=stringdup(s);
  6119. forwardpos:=pos;
  6120. end;
  6121. function tforwarddef.GetTypeName:string;
  6122. begin
  6123. GetTypeName:='unresolved forward to '+tosymname^;
  6124. end;
  6125. destructor tforwarddef.destroy;
  6126. begin
  6127. stringdispose(tosymname);
  6128. inherited destroy;
  6129. end;
  6130. function tforwarddef.getcopy:tstoreddef;
  6131. begin
  6132. result:=cforwarddef.create(tosymname^, forwardpos);
  6133. end;
  6134. {****************************************************************************
  6135. TUNDEFINEDDEF
  6136. ****************************************************************************}
  6137. constructor tundefineddef.create;
  6138. begin
  6139. inherited create(undefineddef);
  6140. end;
  6141. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6142. begin
  6143. inherited ppuload(undefineddef,ppufile);
  6144. ppuload_platform(ppufile);
  6145. end;
  6146. function tundefineddef.GetTypeName:string;
  6147. begin
  6148. GetTypeName:='<undefined type>';
  6149. end;
  6150. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6151. begin
  6152. inherited ppuwrite(ppufile);
  6153. writeentry(ppufile,ibundefineddef);
  6154. end;
  6155. {****************************************************************************
  6156. TERRORDEF
  6157. ****************************************************************************}
  6158. constructor terrordef.create;
  6159. begin
  6160. inherited create(errordef);
  6161. { prevent consecutive faults }
  6162. savesize:=1;
  6163. end;
  6164. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6165. begin
  6166. { Can't write errordefs to ppu }
  6167. internalerror(200411063);
  6168. end;
  6169. function terrordef.GetTypeName:string;
  6170. begin
  6171. GetTypeName:='<erroneous type>';
  6172. end;
  6173. function terrordef.getmangledparaname:TSymStr;
  6174. begin
  6175. getmangledparaname:='error';
  6176. end;
  6177. {****************************************************************************
  6178. Definition Helpers
  6179. ****************************************************************************}
  6180. function is_interfacecom(def: tdef): boolean;
  6181. begin
  6182. is_interfacecom:=
  6183. assigned(def) and
  6184. (def.typ=objectdef) and
  6185. (tobjectdef(def).objecttype=odt_interfacecom);
  6186. end;
  6187. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6188. begin
  6189. is_interfacecom_or_dispinterface:=
  6190. assigned(def) and
  6191. (def.typ=objectdef) and
  6192. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6193. end;
  6194. function is_any_interface_kind(def: tdef): boolean;
  6195. begin
  6196. result:=
  6197. assigned(def) and
  6198. (def.typ=objectdef) and
  6199. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6200. is_objccategory(def));
  6201. end;
  6202. function is_interfacecorba(def: tdef): boolean;
  6203. begin
  6204. is_interfacecorba:=
  6205. assigned(def) and
  6206. (def.typ=objectdef) and
  6207. (tobjectdef(def).objecttype=odt_interfacecorba);
  6208. end;
  6209. function is_interface(def: tdef): boolean;
  6210. begin
  6211. is_interface:=
  6212. assigned(def) and
  6213. (def.typ=objectdef) and
  6214. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6215. end;
  6216. function is_dispinterface(def: tdef): boolean;
  6217. begin
  6218. result:=
  6219. assigned(def) and
  6220. (def.typ=objectdef) and
  6221. (tobjectdef(def).objecttype=odt_dispinterface);
  6222. end;
  6223. function is_class(def: tdef): boolean;
  6224. begin
  6225. is_class:=
  6226. assigned(def) and
  6227. (def.typ=objectdef) and
  6228. (tobjectdef(def).objecttype=odt_class);
  6229. end;
  6230. function is_object(def: tdef): boolean;
  6231. begin
  6232. is_object:=
  6233. assigned(def) and
  6234. (def.typ=objectdef) and
  6235. (tobjectdef(def).objecttype=odt_object);
  6236. end;
  6237. function is_cppclass(def: tdef): boolean;
  6238. begin
  6239. is_cppclass:=
  6240. assigned(def) and
  6241. (def.typ=objectdef) and
  6242. (tobjectdef(def).objecttype=odt_cppclass);
  6243. end;
  6244. function is_objcclass(def: tdef): boolean;
  6245. begin
  6246. is_objcclass:=
  6247. assigned(def) and
  6248. (def.typ=objectdef) and
  6249. (tobjectdef(def).objecttype=odt_objcclass);
  6250. end;
  6251. function is_objectpascal_helper(def: tdef): boolean;
  6252. begin
  6253. result:=
  6254. assigned(def) and
  6255. (def.typ=objectdef) and
  6256. (tobjectdef(def).objecttype=odt_helper);
  6257. end;
  6258. function is_objcclassref(def: tdef): boolean;
  6259. begin
  6260. is_objcclassref:=
  6261. assigned(def) and
  6262. (def.typ=classrefdef) and
  6263. is_objcclass(tclassrefdef(def).pointeddef);
  6264. end;
  6265. function is_objcprotocol(def: tdef): boolean;
  6266. begin
  6267. result:=
  6268. assigned(def) and
  6269. (def.typ=objectdef) and
  6270. (tobjectdef(def).objecttype=odt_objcprotocol);
  6271. end;
  6272. function is_objccategory(def: tdef): boolean;
  6273. begin
  6274. result:=
  6275. assigned(def) and
  6276. (def.typ=objectdef) and
  6277. { if used as a forward type }
  6278. ((tobjectdef(def).objecttype=odt_objccategory) or
  6279. { if used as after it has been resolved }
  6280. ((tobjectdef(def).objecttype=odt_objcclass) and
  6281. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6282. end;
  6283. function is_objc_class_or_protocol(def: tdef): boolean;
  6284. begin
  6285. result:=
  6286. assigned(def) and
  6287. (def.typ=objectdef) and
  6288. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6289. end;
  6290. function is_objc_protocol_or_category(def: tdef): boolean;
  6291. begin
  6292. result:=
  6293. assigned(def) and
  6294. (def.typ=objectdef) and
  6295. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6296. ((tobjectdef(def).objecttype = odt_objcclass) and
  6297. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6298. end;
  6299. function is_classhelper(def: tdef): boolean;
  6300. begin
  6301. result:=
  6302. is_objectpascal_helper(def) or
  6303. is_objccategory(def);
  6304. end;
  6305. function is_class_or_interface(def: tdef): boolean;
  6306. begin
  6307. result:=
  6308. assigned(def) and
  6309. (def.typ=objectdef) and
  6310. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6311. end;
  6312. function is_class_or_interface_or_objc(def: tdef): boolean;
  6313. begin
  6314. result:=
  6315. assigned(def) and
  6316. (def.typ=objectdef) and
  6317. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6318. end;
  6319. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6320. begin
  6321. result:=
  6322. assigned(def) and
  6323. (def.typ=objectdef) and
  6324. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6325. end;
  6326. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6327. begin
  6328. result:=
  6329. assigned(def) and
  6330. (def.typ=objectdef) and
  6331. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6332. end;
  6333. function is_class_or_interface_or_object(def: tdef): boolean;
  6334. begin
  6335. result:=
  6336. assigned(def) and
  6337. (def.typ=objectdef) and
  6338. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6339. end;
  6340. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6341. begin
  6342. result:=
  6343. assigned(def) and
  6344. (def.typ=objectdef) and
  6345. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6346. end;
  6347. function is_implicit_pointer_object_type(def: tdef): boolean;
  6348. begin
  6349. result:=
  6350. assigned(def) and
  6351. (((def.typ=objectdef) and
  6352. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6353. ((target_info.system in systems_jvm) and
  6354. (def.typ=recorddef)));
  6355. end;
  6356. function is_implicit_array_pointer(def: tdef): boolean;
  6357. begin
  6358. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6359. end;
  6360. function is_class_or_object(def: tdef): boolean;
  6361. begin
  6362. result:=
  6363. assigned(def) and
  6364. (def.typ=objectdef) and
  6365. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6366. end;
  6367. function is_record(def: tdef): boolean;
  6368. begin
  6369. result:=
  6370. assigned(def) and
  6371. (def.typ=recorddef);
  6372. end;
  6373. function is_javaclass(def: tdef): boolean;
  6374. begin
  6375. result:=
  6376. assigned(def) and
  6377. (def.typ=objectdef) and
  6378. (tobjectdef(def).objecttype=odt_javaclass);
  6379. end;
  6380. function is_javaclassref(def: tdef): boolean;
  6381. begin
  6382. is_javaclassref:=
  6383. assigned(def) and
  6384. (def.typ=classrefdef) and
  6385. is_javaclass(tclassrefdef(def).pointeddef);
  6386. end;
  6387. function is_javainterface(def: tdef): boolean;
  6388. begin
  6389. result:=
  6390. assigned(def) and
  6391. (def.typ=objectdef) and
  6392. (tobjectdef(def).objecttype=odt_interfacejava);
  6393. end;
  6394. function is_java_class_or_interface(def: tdef): boolean;
  6395. begin
  6396. result:=
  6397. assigned(def) and
  6398. (def.typ=objectdef) and
  6399. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6400. end;
  6401. procedure loadobjctypes;
  6402. begin
  6403. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6404. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6405. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6406. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6407. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6408. end;
  6409. procedure maybeloadcocoatypes;
  6410. var
  6411. tsym: ttypesym;
  6412. cocoaunit: string[15];
  6413. begin
  6414. if assigned(objc_fastenumeration) then
  6415. exit;
  6416. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6417. cocoaunit:='COCOAALL'
  6418. else
  6419. cocoaunit:='IPHONEALL';
  6420. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6421. if assigned(tsym) then
  6422. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6423. else
  6424. objc_fastenumeration:=nil;
  6425. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6426. if assigned(tsym) then
  6427. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6428. else
  6429. objc_fastenumerationstate:=nil;
  6430. end;
  6431. function use_vectorfpu(def : tdef) : boolean;
  6432. begin
  6433. {$ifdef x86}
  6434. {$define use_vectorfpuimplemented}
  6435. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6436. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6437. {$endif x86}
  6438. {$ifdef arm}
  6439. {$define use_vectorfpuimplemented}
  6440. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6441. {$endif arm}
  6442. {$ifndef use_vectorfpuimplemented}
  6443. use_vectorfpu:=false;
  6444. {$endif}
  6445. end;
  6446. function getpointerdef(def: tdef): tpointerdef;
  6447. var
  6448. res: PHashSetItem;
  6449. oldsymtablestack: tsymtablestack;
  6450. begin
  6451. if not assigned(current_module) then
  6452. internalerror(2011071101);
  6453. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6454. if not assigned(res^.Data) then
  6455. begin
  6456. { since these pointerdefs can be reused anywhere in the current
  6457. unit, add them to the global/staticsymtable }
  6458. oldsymtablestack:=symtablestack;
  6459. { do not simply push/pop current_module.localsymtable, because
  6460. that can have side-effects (e.g., it removes helpers) }
  6461. symtablestack:=nil;
  6462. res^.Data:=cpointerdef.create(def);
  6463. if assigned(current_module.localsymtable) then
  6464. current_module.localsymtable.insertdef(tdef(res^.Data))
  6465. else
  6466. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6467. symtablestack:=oldsymtablestack;
  6468. end;
  6469. result:=tpointerdef(res^.Data);
  6470. end;
  6471. function getsingletonarraydef(def: tdef): tarraydef;
  6472. begin
  6473. result:=getarraydef(def,1);
  6474. end;
  6475. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6476. var
  6477. res: PHashSetItem;
  6478. oldsymtablestack: tsymtablestack;
  6479. arrdesc: packed record
  6480. def: tdef;
  6481. elecount: asizeint;
  6482. end;
  6483. begin
  6484. if not assigned(current_module) then
  6485. internalerror(2011081301);
  6486. arrdesc.def:=def;
  6487. arrdesc.elecount:=elecount;
  6488. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6489. if not assigned(res^.Data) then
  6490. begin
  6491. { since these arraydef can be reused anywhere in the current
  6492. unit, add them to the global/staticsymtable }
  6493. oldsymtablestack:=symtablestack;
  6494. symtablestack:=nil;
  6495. res^.Data:=carraydef.create(0,elecount-1,ptrsinttype);
  6496. tarraydef(res^.Data).elementdef:=def;
  6497. if assigned(current_module.localsymtable) then
  6498. current_module.localsymtable.insertdef(tdef(res^.Data))
  6499. else
  6500. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6501. symtablestack:=oldsymtablestack;
  6502. end;
  6503. result:=tarraydef(res^.Data);
  6504. end;
  6505. end.