symdef.pas 242 KB

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