symdef.pas 201 KB

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