symdef.pas 242 KB

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