symdef.pas 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661
  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. cutils,cclasses,
  23. { global }
  24. globtype,globals,tokens,
  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 = class(tdef)
  42. protected
  43. typesymderef : tderef;
  44. public
  45. { persistent (available across units) rtti and init tables }
  46. rttitablesym,
  47. inittablesym : tsym; {trttisym}
  48. rttitablesymderef,
  49. inittablesymderef : tderef;
  50. { local (per module) rtti and init tables }
  51. localrttilab : array[trttitype] of tasmlabel;
  52. {$ifdef EXTDEBUG}
  53. fileinfo : tfileposinfo;
  54. {$endif}
  55. { generic support }
  56. genericdef : tstoreddef;
  57. genericdefderef : tderef;
  58. generictokenbuf : tdynamicarray;
  59. constructor create(dt:tdeftype);
  60. constructor ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  61. destructor destroy;override;
  62. procedure reset;virtual;
  63. function getcopy : tstoreddef;virtual;
  64. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  65. procedure buildderef;override;
  66. procedure buildderefimpl;override;
  67. procedure deref;override;
  68. procedure derefimpl;override;
  69. function size:aint;override;
  70. function getvartype:longint;override;
  71. function alignment:shortint;override;
  72. function is_publishable : boolean;override;
  73. function needs_inittable : boolean;override;
  74. { rtti generation }
  75. procedure write_rtti_name;
  76. procedure write_rtti_data(rt:trttitype);virtual;
  77. procedure write_child_rtti_data(rt:trttitype);virtual;
  78. function get_rtti_label(rt:trttitype):tasmsymbol;
  79. { regvars }
  80. function is_intregable : boolean;
  81. function is_fpuregable : boolean;
  82. { generics }
  83. procedure initgeneric;
  84. private
  85. savesize : aint;
  86. end;
  87. tfiletyp = (ft_text,ft_typed,ft_untyped);
  88. tfiledef = class(tstoreddef)
  89. filetyp : tfiletyp;
  90. typedfiletype : ttype;
  91. constructor createtext;
  92. constructor createuntyped;
  93. constructor createtyped(const tt : ttype);
  94. constructor ppuload(ppufile:tcompilerppufile);
  95. function getcopy : tstoreddef;override;
  96. procedure ppuwrite(ppufile:tcompilerppufile);override;
  97. procedure buildderef;override;
  98. procedure deref;override;
  99. function gettypename:string;override;
  100. function getmangledparaname:string;override;
  101. procedure setsize;
  102. end;
  103. tvariantdef = class(tstoreddef)
  104. varianttype : tvarianttype;
  105. constructor create(v : tvarianttype);
  106. constructor ppuload(ppufile:tcompilerppufile);
  107. function getcopy : tstoreddef;override;
  108. function gettypename:string;override;
  109. procedure ppuwrite(ppufile:tcompilerppufile);override;
  110. procedure setsize;
  111. function is_publishable : boolean;override;
  112. function needs_inittable : boolean;override;
  113. procedure write_rtti_data(rt:trttitype);override;
  114. end;
  115. tformaldef = class(tstoreddef)
  116. constructor create;
  117. constructor ppuload(ppufile:tcompilerppufile);
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. function gettypename:string;override;
  120. end;
  121. tforwarddef = class(tstoreddef)
  122. tosymname : pstring;
  123. forwardpos : tfileposinfo;
  124. constructor create(const s:string;const pos : tfileposinfo);
  125. destructor destroy;override;
  126. function gettypename:string;override;
  127. end;
  128. tundefineddef = class(tstoreddef)
  129. constructor create;
  130. constructor ppuload(ppufile:tcompilerppufile);
  131. procedure ppuwrite(ppufile:tcompilerppufile);override;
  132. function gettypename:string;override;
  133. end;
  134. terrordef = class(tstoreddef)
  135. constructor create;
  136. procedure ppuwrite(ppufile:tcompilerppufile);override;
  137. function gettypename:string;override;
  138. function getmangledparaname : string;override;
  139. end;
  140. tabstractpointerdef = class(tstoreddef)
  141. pointertype : ttype;
  142. constructor create(dt:tdeftype;const tt : ttype);
  143. constructor ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. procedure buildderef;override;
  146. procedure deref;override;
  147. end;
  148. tpointerdef = class(tabstractpointerdef)
  149. is_far : boolean;
  150. constructor create(const tt : ttype);
  151. constructor createfar(const tt : ttype);
  152. function getcopy : tstoreddef;override;
  153. constructor ppuload(ppufile:tcompilerppufile);
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;
  155. function gettypename:string;override;
  156. end;
  157. tabstractrecorddef= class(tstoreddef)
  158. private
  159. Count : integer;
  160. FRTTIType : trttitype;
  161. procedure count_field_rtti(sym : tnamedindexitem;arg:pointer);
  162. procedure write_field_rtti(sym : tnamedindexitem;arg:pointer);
  163. procedure generate_field_rtti(sym : tnamedindexitem;arg:pointer);
  164. public
  165. symtable : tsymtable;
  166. procedure reset;override;
  167. function getsymtable(t:tgetsymtable):tsymtable;override;
  168. procedure buildderefimpl;override;
  169. procedure derefimpl;override;
  170. function is_packed:boolean;
  171. end;
  172. trecorddef = class(tabstractrecorddef)
  173. public
  174. isunion : boolean;
  175. constructor create(p : tsymtable);
  176. constructor ppuload(ppufile:tcompilerppufile);
  177. destructor destroy;override;
  178. function getcopy : tstoreddef;override;
  179. procedure ppuwrite(ppufile:tcompilerppufile);override;
  180. procedure buildderef;override;
  181. procedure deref;override;
  182. function size:aint;override;
  183. function alignment : shortint;override;
  184. function padalignment: shortint;
  185. function gettypename:string;override;
  186. { debug }
  187. function needs_inittable : boolean;override;
  188. { rtti }
  189. procedure write_child_rtti_data(rt:trttitype);override;
  190. procedure write_rtti_data(rt:trttitype);override;
  191. end;
  192. tprocdef = class;
  193. tobjectdef = class;
  194. timplementedinterfaces = class;
  195. timplintfentry = class(TNamedIndexItem)
  196. intf : tobjectdef;
  197. intfderef : tderef;
  198. ioffset : longint;
  199. implindex : longint;
  200. namemappings : tdictionary;
  201. procdefs : TIndexArray;
  202. constructor create(aintf: tobjectdef);
  203. constructor create_deref(const d:tderef);
  204. destructor destroy; override;
  205. end;
  206. tobjectdef = class(tabstractrecorddef)
  207. private
  208. procedure count_published_properties(sym:tnamedindexitem;arg:pointer);
  209. procedure collect_published_properties(sym:tnamedindexitem;arg:pointer);
  210. procedure write_property_info(sym : tnamedindexitem;arg:pointer);
  211. procedure generate_published_child_rtti(sym : tnamedindexitem;arg:pointer);
  212. procedure count_published_fields(sym:tnamedindexitem;arg:pointer);
  213. procedure writefields(sym:tnamedindexitem;arg:pointer);
  214. public
  215. childof : tobjectdef;
  216. childofderef : tderef;
  217. objname,
  218. objrealname : pstring;
  219. objectoptions : tobjectoptions;
  220. { to be able to have a variable vmt position }
  221. { and no vmt field for objects without virtuals }
  222. vmt_offset : longint;
  223. writing_class_record_dbginfo : boolean;
  224. objecttype : tobjectdeftype;
  225. iidguid: pguid;
  226. iidstr: pstring;
  227. iitype: tinterfaceentrytype;
  228. iioffset: longint;
  229. lastvtableindex: longint;
  230. { store implemented interfaces defs and name mappings }
  231. implementedinterfaces: timplementedinterfaces;
  232. constructor create(ot : tobjectdeftype;const n : string;c : tobjectdef);
  233. constructor ppuload(ppufile:tcompilerppufile);
  234. destructor destroy;override;
  235. function getcopy : tstoreddef;override;
  236. procedure ppuwrite(ppufile:tcompilerppufile);override;
  237. function gettypename:string;override;
  238. procedure buildderef;override;
  239. procedure deref;override;
  240. function getparentdef:tdef;override;
  241. function size : aint;override;
  242. function alignment:shortint;override;
  243. function vmtmethodoffset(index:longint):longint;
  244. function members_need_inittable : boolean;
  245. { this should be called when this class implements an interface }
  246. procedure prepareguid;
  247. function is_publishable : boolean;override;
  248. function needs_inittable : boolean;override;
  249. function vmt_mangledname : string;
  250. function rtti_name : string;
  251. procedure check_forwards;
  252. function is_related(d : tdef) : boolean;override;
  253. procedure insertvmt;
  254. procedure set_parent(c : tobjectdef);
  255. function searchdestructor : tprocdef;
  256. { rtti }
  257. procedure write_child_rtti_data(rt:trttitype);override;
  258. procedure write_rtti_data(rt:trttitype);override;
  259. function generate_field_table : tasmlabel;
  260. end;
  261. timplementedinterfaces = class
  262. constructor create;
  263. destructor destroy; override;
  264. function count: longint;
  265. function interfaces(intfindex: longint): tobjectdef;
  266. function interfacesderef(intfindex: longint): tderef;
  267. function ioffsets(intfindex: longint): longint;
  268. procedure setioffsets(intfindex,iofs:longint);
  269. function implindex(intfindex:longint):longint;
  270. procedure setimplindex(intfindex,implidx:longint);
  271. function searchintf(def: tdef): longint;
  272. procedure addintf(def: tdef);
  273. procedure buildderef;
  274. procedure deref;
  275. { add interface reference loaded from ppu }
  276. procedure addintf_deref(const d:tderef;iofs:longint);
  277. procedure addintf_ioffset(d:tdef;iofs:longint);
  278. procedure clearmappings;
  279. procedure addmappings(intfindex: longint; const origname, newname: string);
  280. function getmappings(intfindex: longint; const origname: string; var nextexist: pointer): string;
  281. procedure addimplproc(intfindex: longint; procdef: tprocdef);
  282. function implproccount(intfindex: longint): longint;
  283. function implprocs(intfindex: longint; procindex: longint): tprocdef;
  284. function isimplmergepossible(intfindex, remainindex: longint; var weight: longint): boolean;
  285. private
  286. finterfaces: tindexarray;
  287. procedure checkindex(intfindex: longint);
  288. end;
  289. tclassrefdef = class(tabstractpointerdef)
  290. constructor create(const t:ttype);
  291. constructor ppuload(ppufile:tcompilerppufile);
  292. procedure ppuwrite(ppufile:tcompilerppufile);override;
  293. function gettypename:string;override;
  294. function is_publishable : boolean;override;
  295. end;
  296. tarraydef = class(tstoreddef)
  297. lowrange,
  298. highrange : aint;
  299. rangetype : ttype;
  300. arrayoptions : tarraydefoptions;
  301. protected
  302. _elementtype : ttype;
  303. public
  304. function elesize : aint;
  305. function elepackedbitsize : aint;
  306. function elecount : aint;
  307. constructor create_from_pointer(const elemt : ttype);
  308. constructor create(l,h : aint;const t : ttype);
  309. constructor ppuload(ppufile:tcompilerppufile);
  310. function getcopy : tstoreddef;override;
  311. procedure ppuwrite(ppufile:tcompilerppufile);override;
  312. function gettypename:string;override;
  313. function getmangledparaname : string;override;
  314. procedure setelementtype(t: ttype);
  315. procedure buildderef;override;
  316. procedure deref;override;
  317. function size : aint;override;
  318. function alignment : shortint;override;
  319. { returns the label of the range check string }
  320. function needs_inittable : boolean;override;
  321. procedure write_child_rtti_data(rt:trttitype);override;
  322. procedure write_rtti_data(rt:trttitype);override;
  323. property elementtype : ttype Read _ElementType;
  324. end;
  325. torddef = class(tstoreddef)
  326. low,high : TConstExprInt;
  327. typ : tbasetype;
  328. constructor create(t : tbasetype;v,b : TConstExprInt);
  329. constructor ppuload(ppufile:tcompilerppufile);
  330. function getcopy : tstoreddef;override;
  331. procedure ppuwrite(ppufile:tcompilerppufile);override;
  332. function is_publishable : boolean;override;
  333. function gettypename:string;override;
  334. function alignment:shortint;override;
  335. procedure setsize;
  336. function packedbitsize: aint; override;
  337. function getvartype : longint;override;
  338. { rtti }
  339. procedure write_rtti_data(rt:trttitype);override;
  340. end;
  341. tfloatdef = class(tstoreddef)
  342. typ : tfloattype;
  343. constructor create(t : tfloattype);
  344. constructor ppuload(ppufile:tcompilerppufile);
  345. function getcopy : tstoreddef;override;
  346. procedure ppuwrite(ppufile:tcompilerppufile);override;
  347. function gettypename:string;override;
  348. function is_publishable : boolean;override;
  349. function alignment:shortint;override;
  350. procedure setsize;
  351. function getvartype:longint;override;
  352. { rtti }
  353. procedure write_rtti_data(rt:trttitype);override;
  354. end;
  355. tabstractprocdef = class(tstoreddef)
  356. { saves a definition to the return type }
  357. rettype : ttype;
  358. parast : tsymtable;
  359. paras : tparalist;
  360. proctypeoption : tproctypeoption;
  361. proccalloption : tproccalloption;
  362. procoptions : tprocoptions;
  363. requiredargarea : aint;
  364. { number of user visibile parameters }
  365. maxparacount,
  366. minparacount : byte;
  367. {$ifdef i386}
  368. fpu_used : longint; { how many stack fpu must be empty }
  369. {$endif i386}
  370. {$ifdef m68k}
  371. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  372. {$endif}
  373. funcretloc : array[tcallercallee] of TLocation;
  374. has_paraloc_info : boolean; { paraloc info is available }
  375. constructor create(dt:tdeftype;level:byte);
  376. constructor ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  377. destructor destroy;override;
  378. procedure ppuwrite(ppufile:tcompilerppufile);override;
  379. procedure buildderef;override;
  380. procedure deref;override;
  381. procedure releasemem;
  382. procedure calcparas;
  383. function typename_paras(showhidden:boolean): string;
  384. procedure test_if_fpu_result;
  385. function is_methodpointer:boolean;virtual;
  386. function is_addressonly:boolean;virtual;
  387. private
  388. procedure count_para(p:tnamedindexitem;arg:pointer);
  389. procedure insert_para(p:tnamedindexitem;arg:pointer);
  390. end;
  391. tprocvardef = class(tabstractprocdef)
  392. constructor create(level:byte);
  393. constructor ppuload(ppufile:tcompilerppufile);
  394. function getcopy : tstoreddef;override;
  395. procedure ppuwrite(ppufile:tcompilerppufile);override;
  396. procedure buildderef;override;
  397. procedure deref;override;
  398. function getsymtable(t:tgetsymtable):tsymtable;override;
  399. function size : aint;override;
  400. function gettypename:string;override;
  401. function is_publishable : boolean;override;
  402. function is_methodpointer:boolean;override;
  403. function is_addressonly:boolean;override;
  404. function getmangledparaname:string;override;
  405. { rtti }
  406. procedure write_rtti_data(rt:trttitype);override;
  407. end;
  408. tmessageinf = record
  409. case integer of
  410. 0 : (str : pstring);
  411. 1 : (i : longint);
  412. end;
  413. tinlininginfo = record
  414. { node tree }
  415. code : tnode;
  416. flags : tprocinfoflags;
  417. end;
  418. pinlininginfo = ^tinlininginfo;
  419. {$ifdef oldregvars}
  420. { register variables }
  421. pregvarinfo = ^tregvarinfo;
  422. tregvarinfo = record
  423. regvars : array[1..maxvarregs] of tsym;
  424. regvars_para : array[1..maxvarregs] of boolean;
  425. regvars_refs : array[1..maxvarregs] of longint;
  426. fpuregvars : array[1..maxfpuvarregs] of tsym;
  427. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  428. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  429. end;
  430. {$endif oldregvars}
  431. tprocdef = class(tabstractprocdef)
  432. private
  433. _mangledname : pstring;
  434. public
  435. extnumber : word;
  436. messageinf : tmessageinf;
  437. {$ifndef EXTDEBUG}
  438. { where is this function defined and what were the symbol
  439. flags, needed here because there
  440. is only one symbol for all overloaded functions
  441. EXTDEBUG has fileinfo in tdef (PFV) }
  442. fileinfo : tfileposinfo;
  443. {$endif}
  444. symoptions : tsymoptions;
  445. { symbol owning this definition }
  446. procsym : tsym;
  447. procsymderef : tderef;
  448. { alias names }
  449. aliasnames : tstringlist;
  450. { symtables }
  451. localst : tsymtable;
  452. funcretsym : tsym;
  453. funcretsymderef : tderef;
  454. { browser info }
  455. lastref,
  456. defref,
  457. lastwritten : tref;
  458. refcount : longint;
  459. _class : tobjectdef;
  460. _classderef : tderef;
  461. {$if defined(powerpc) or defined(m68k)}
  462. { library symbol for AmigaOS/MorphOS }
  463. libsym : tsym;
  464. libsymderef : tderef;
  465. {$endif powerpc or m68k}
  466. { name of the result variable to insert in the localsymtable }
  467. resultname : stringid;
  468. { true, if the procedure is only declared
  469. (forward procedure) }
  470. forwarddef,
  471. { true if the procedure is declared in the interface }
  472. interfacedef : boolean;
  473. { true if the procedure has a forward declaration }
  474. hasforward : boolean;
  475. { import info }
  476. import_dll,
  477. import_name : pstring;
  478. import_nr : word;
  479. { info for inlining the subroutine, if this pointer is nil,
  480. the procedure can't be inlined }
  481. inlininginfo : pinlininginfo;
  482. {$ifdef oldregvars}
  483. regvarinfo: pregvarinfo;
  484. {$endif oldregvars}
  485. { position in aasmoutput list }
  486. procstarttai,
  487. procendtai : tai;
  488. constructor create(level:byte);
  489. constructor ppuload(ppufile:tcompilerppufile);
  490. destructor destroy;override;
  491. procedure ppuwrite(ppufile:tcompilerppufile);override;
  492. procedure buildderef;override;
  493. procedure buildderefimpl;override;
  494. procedure deref;override;
  495. procedure derefimpl;override;
  496. procedure reset;override;
  497. function getsymtable(t:tgetsymtable):tsymtable;override;
  498. function gettypename : string;override;
  499. function mangledname : string;
  500. procedure setmangledname(const s : string);
  501. procedure load_references(ppufile:tcompilerppufile;locals:boolean);
  502. function write_references(ppufile:tcompilerppufile;locals:boolean):boolean;
  503. { inserts the local symbol table, if this is not
  504. no local symbol table is built. Should be called only
  505. when we are sure that a local symbol table will be required.
  506. }
  507. procedure insert_localst;
  508. function fullprocname(showhidden:boolean):string;
  509. function cplusplusmangledname : string;
  510. function is_methodpointer:boolean;override;
  511. function is_addressonly:boolean;override;
  512. function is_visible_for_object(currobjdef,contextobjdef:tobjectdef):boolean;
  513. end;
  514. { single linked list of overloaded procs }
  515. pprocdeflist = ^tprocdeflist;
  516. tprocdeflist = record
  517. def : tprocdef;
  518. defderef : tderef;
  519. next : pprocdeflist;
  520. end;
  521. tstringdef = class(tstoreddef)
  522. string_typ : tstringtype;
  523. len : aint;
  524. constructor createshort(l : byte);
  525. constructor loadshort(ppufile:tcompilerppufile);
  526. constructor createlong(l : aint);
  527. constructor loadlong(ppufile:tcompilerppufile);
  528. constructor createansi(l : aint);
  529. constructor loadansi(ppufile:tcompilerppufile);
  530. constructor createwide(l : aint);
  531. constructor loadwide(ppufile:tcompilerppufile);
  532. function getcopy : tstoreddef;override;
  533. function stringtypname:string;
  534. procedure ppuwrite(ppufile:tcompilerppufile);override;
  535. function gettypename:string;override;
  536. function getmangledparaname:string;override;
  537. function is_publishable : boolean;override;
  538. function alignment : shortint;override;
  539. { init/final }
  540. function needs_inittable : boolean;override;
  541. { rtti }
  542. procedure write_rtti_data(rt:trttitype);override;
  543. end;
  544. tenumdef = class(tstoreddef)
  545. minval,
  546. maxval : aint;
  547. has_jumps : boolean;
  548. firstenum : tsym; {tenumsym}
  549. basedef : tenumdef;
  550. basedefderef : tderef;
  551. constructor create;
  552. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  553. constructor ppuload(ppufile:tcompilerppufile);
  554. destructor destroy;override;
  555. function getcopy : tstoreddef;override;
  556. procedure ppuwrite(ppufile:tcompilerppufile);override;
  557. procedure buildderef;override;
  558. procedure deref;override;
  559. procedure derefimpl;override;
  560. function gettypename:string;override;
  561. function is_publishable : boolean;override;
  562. procedure calcsavesize;
  563. function packedbitsize: aint; override;
  564. procedure setmax(_max:aint);
  565. procedure setmin(_min:aint);
  566. function min:aint;
  567. function max:aint;
  568. { rtti }
  569. procedure write_rtti_data(rt:trttitype);override;
  570. procedure write_child_rtti_data(rt:trttitype);override;
  571. end;
  572. tsetdef = class(tstoreddef)
  573. elementtype : ttype;
  574. settype : tsettype;
  575. setbase,
  576. setmax : aint;
  577. constructor create(const t:ttype;high : aint);
  578. constructor ppuload(ppufile:tcompilerppufile);
  579. destructor destroy;override;
  580. function getcopy : tstoreddef;override;
  581. procedure ppuwrite(ppufile:tcompilerppufile);override;
  582. procedure buildderef;override;
  583. procedure deref;override;
  584. function gettypename:string;override;
  585. function is_publishable : boolean;override;
  586. { rtti }
  587. procedure write_rtti_data(rt:trttitype);override;
  588. procedure write_child_rtti_data(rt:trttitype);override;
  589. end;
  590. Tdefmatch=(dm_exact,dm_equal,dm_convertl1);
  591. var
  592. aktobjectdef : tobjectdef; { used for private functions check !! }
  593. { default types }
  594. generrortype, { error in definition }
  595. voidpointertype, { pointer for Void-Pointerdef }
  596. charpointertype, { pointer for Char-Pointerdef }
  597. widecharpointertype, { pointer for WideChar-Pointerdef }
  598. voidfarpointertype,
  599. cundefinedtype,
  600. cformaltype, { unique formal definition }
  601. voidtype, { Void (procedure) }
  602. cchartype, { Char }
  603. cwidechartype, { WideChar }
  604. booltype, { boolean type }
  605. bool16type,
  606. bool32type,
  607. bool64type, { implement me }
  608. u8inttype, { 8-Bit unsigned integer }
  609. s8inttype, { 8-Bit signed integer }
  610. u16inttype, { 16-Bit unsigned integer }
  611. s16inttype, { 16-Bit signed integer }
  612. u32inttype, { 32-Bit unsigned integer }
  613. s32inttype, { 32-Bit signed integer }
  614. u64inttype, { 64-bit unsigned integer }
  615. s64inttype, { 64-bit signed integer }
  616. s32floattype, { pointer for realconstn }
  617. s64floattype, { pointer for realconstn }
  618. s80floattype, { pointer to type of temp. floats }
  619. s64currencytype, { pointer to a currency type }
  620. cshortstringtype, { pointer to type of short string const }
  621. clongstringtype, { pointer to type of long string const }
  622. cansistringtype, { pointer to type of ansi string const }
  623. cwidestringtype, { pointer to type of wide string const }
  624. openshortstringtype, { pointer to type of an open shortstring,
  625. needed for readln() }
  626. openchararraytype, { pointer to type of an open array of char,
  627. needed for readln() }
  628. cfiletype, { get the same definition for all file }
  629. { used for stabs }
  630. methodpointertype, { typecasting of methodpointers to extract self }
  631. hresulttype,
  632. { we use only one variant def for every variant class }
  633. cvarianttype,
  634. colevarianttype,
  635. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  636. sinttype,
  637. uinttype,
  638. { unsigned ord type with the same size as a pointer }
  639. ptrinttype,
  640. { several types to simulate more or less C++ objects for GDB }
  641. vmttype,
  642. vmtarraytype,
  643. pvmttype : ttype; { type of classrefs, used for stabs }
  644. { pointer to the anchestor of all classes }
  645. class_tobject : tobjectdef;
  646. { pointer to the ancestor of all COM interfaces }
  647. interface_iunknown : tobjectdef;
  648. { pointer to the TGUID type
  649. of all interfaces }
  650. rec_tguid : trecorddef;
  651. const
  652. {$ifdef i386}
  653. pbestrealtype : ^ttype = @s80floattype;
  654. {$endif}
  655. {$ifdef x86_64}
  656. pbestrealtype : ^ttype = @s80floattype;
  657. {$endif}
  658. {$ifdef m68k}
  659. pbestrealtype : ^ttype = @s64floattype;
  660. {$endif}
  661. {$ifdef alpha}
  662. pbestrealtype : ^ttype = @s64floattype;
  663. {$endif}
  664. {$ifdef powerpc}
  665. pbestrealtype : ^ttype = @s64floattype;
  666. {$endif}
  667. {$ifdef POWERPC64}
  668. pbestrealtype : ^ttype = @s64floattype;
  669. {$endif}
  670. {$ifdef ia64}
  671. pbestrealtype : ^ttype = @s64floattype;
  672. {$endif}
  673. {$ifdef SPARC}
  674. pbestrealtype : ^ttype = @s64floattype;
  675. {$endif SPARC}
  676. {$ifdef vis}
  677. pbestrealtype : ^ttype = @s64floattype;
  678. {$endif vis}
  679. {$ifdef ARM}
  680. pbestrealtype : ^ttype = @s64floattype;
  681. {$endif ARM}
  682. {$ifdef MIPS}
  683. pbestrealtype : ^ttype = @s64floattype;
  684. {$endif MIPS}
  685. function make_mangledname(const typeprefix:string;st:tsymtable;const suffix:string):string;
  686. { should be in the types unit, but the types unit uses the node stuff :( }
  687. function is_interfacecom(def: tdef): boolean;
  688. function is_interfacecorba(def: tdef): boolean;
  689. function is_interface(def: tdef): boolean;
  690. function is_dispinterface(def: tdef): boolean;
  691. function is_object(def: tdef): boolean;
  692. function is_class(def: tdef): boolean;
  693. function is_cppclass(def: tdef): boolean;
  694. function is_class_or_interface(def: tdef): boolean;
  695. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  696. {$ifdef x86}
  697. function use_sse(def : tdef) : boolean;
  698. {$endif x86}
  699. implementation
  700. uses
  701. strings,
  702. { global }
  703. verbose,
  704. { target }
  705. systems,aasmcpu,paramgr,
  706. { symtable }
  707. symsym,symtable,symutil,defutil,
  708. { module }
  709. fmodule,
  710. { other }
  711. gendef,
  712. fpccrc
  713. ;
  714. {****************************************************************************
  715. Constants
  716. ****************************************************************************}
  717. const
  718. varempty = 0;
  719. varnull = 1;
  720. varsmallint = 2;
  721. varinteger = 3;
  722. varsingle = 4;
  723. vardouble = 5;
  724. varcurrency = 6;
  725. vardate = 7;
  726. varolestr = 8;
  727. vardispatch = 9;
  728. varerror = 10;
  729. varboolean = 11;
  730. varvariant = 12;
  731. varunknown = 13;
  732. vardecimal = 14;
  733. varshortint = 16;
  734. varbyte = 17;
  735. varword = 18;
  736. varlongword = 19;
  737. varint64 = 20;
  738. varqword = 21;
  739. varUndefined = -1;
  740. varstrarg = $48;
  741. varstring = $100;
  742. varany = $101;
  743. vartypemask = $fff;
  744. vararray = $2000;
  745. varbyref = $4000;
  746. {****************************************************************************
  747. Helpers
  748. ****************************************************************************}
  749. function make_mangledname(const typeprefix:string;st:tsymtable;const suffix:string):string;
  750. var
  751. s,hs,
  752. prefix : string;
  753. oldlen,
  754. newlen,
  755. i : longint;
  756. crc : dword;
  757. hp : tparavarsym;
  758. begin
  759. prefix:='';
  760. if not assigned(st) then
  761. internalerror(200204212);
  762. { sub procedures }
  763. while (st.symtabletype=localsymtable) do
  764. begin
  765. if st.defowner.deftype<>procdef then
  766. internalerror(200204173);
  767. { Add the full mangledname of procedure to prevent
  768. conflicts with 2 overloads having both a nested procedure
  769. with the same name, see tb0314 (PFV) }
  770. s:=tprocdef(st.defowner).procsym.name;
  771. oldlen:=length(s);
  772. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  773. begin
  774. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  775. if not(vo_is_hidden_para in hp.varoptions) then
  776. s:=s+'$'+hp.vartype.def.mangledparaname;
  777. end;
  778. if not is_void(tprocdef(st.defowner).rettype.def) then
  779. s:=s+'$$'+tprocdef(st.defowner).rettype.def.mangledparaname;
  780. newlen:=length(s);
  781. { Replace with CRC if the parameter line is very long }
  782. if (newlen-oldlen>12) and
  783. ((newlen>128) or (newlen-oldlen>64)) then
  784. begin
  785. crc:=$ffffffff;
  786. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  787. begin
  788. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  789. if not(vo_is_hidden_para in hp.varoptions) then
  790. begin
  791. hs:=hp.vartype.def.mangledparaname;
  792. crc:=UpdateCrc32(crc,hs[1],length(hs));
  793. end;
  794. end;
  795. hs:=hp.vartype.def.mangledparaname;
  796. crc:=UpdateCrc32(crc,hs[1],length(hs));
  797. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  798. end;
  799. if prefix<>'' then
  800. prefix:=s+'_'+prefix
  801. else
  802. prefix:=s;
  803. st:=st.defowner.owner;
  804. end;
  805. { object/classes symtable }
  806. if (st.symtabletype=objectsymtable) then
  807. begin
  808. if st.defowner.deftype<>objectdef then
  809. internalerror(200204174);
  810. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  811. st:=st.defowner.owner;
  812. end;
  813. { symtable must now be static or global }
  814. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  815. internalerror(200204175);
  816. result:='';
  817. if typeprefix<>'' then
  818. result:=result+typeprefix+'_';
  819. { Add P$ for program, which can have the same name as
  820. a unit }
  821. if (tsymtable(main_module.localsymtable)=st) and
  822. (not main_module.is_unit) then
  823. result:=result+'P$'+st.name^
  824. else
  825. result:=result+st.name^;
  826. if prefix<>'' then
  827. result:=result+'_'+prefix;
  828. if suffix<>'' then
  829. result:=result+'_'+suffix;
  830. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  831. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) and
  832. (result[1] = 'L') then
  833. result := '_' + result;
  834. end;
  835. {****************************************************************************
  836. TDEF (base class for definitions)
  837. ****************************************************************************}
  838. constructor tstoreddef.create(dt:tdeftype);
  839. var
  840. insertstack : psymtablestackitem;
  841. begin
  842. inherited create(dt);
  843. savesize := 0;
  844. {$ifdef EXTDEBUG}
  845. fileinfo := aktfilepos;
  846. {$endif}
  847. fillchar(localrttilab,sizeof(localrttilab),0);
  848. generictokenbuf:=nil;
  849. genericdef:=nil;
  850. { Register in symtable stack.
  851. Don't register forwarddefs, they are disposed at the
  852. end of an type block }
  853. if assigned(symtablestack) and
  854. (dt<>forwarddef) then
  855. begin
  856. insertstack:=symtablestack.stack;
  857. while assigned(insertstack) and
  858. (insertstack^.symtable.symtabletype=withsymtable) do
  859. insertstack:=insertstack^.next;
  860. if not assigned(insertstack) then
  861. internalerror(200602044);
  862. insertstack^.symtable.insertdef(self);
  863. end;
  864. end;
  865. destructor tstoreddef.destroy;
  866. begin
  867. { remove also index from symtable }
  868. if assigned(owner) then
  869. owner.deletedef(self);
  870. if assigned(generictokenbuf) then
  871. generictokenbuf.free;
  872. inherited destroy;
  873. end;
  874. constructor tstoreddef.ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  875. var
  876. sizeleft,i : longint;
  877. buf : array[0..255] of byte;
  878. begin
  879. inherited create(dt);
  880. {$ifdef EXTDEBUG}
  881. fillchar(fileinfo,sizeof(fileinfo),0);
  882. {$endif}
  883. fillchar(localrttilab,sizeof(localrttilab),0);
  884. { load }
  885. indexnr:=ppufile.getword;
  886. ppufile.getderef(typesymderef);
  887. ppufile.getsmallset(defoptions);
  888. if df_has_rttitable in defoptions then
  889. ppufile.getderef(rttitablesymderef);
  890. if df_has_inittable in defoptions then
  891. ppufile.getderef(inittablesymderef);
  892. if df_generic in defoptions then
  893. begin
  894. sizeleft:=ppufile.getlongint;
  895. initgeneric;
  896. while sizeleft>0 do
  897. begin
  898. if sizeleft>sizeof(buf) then
  899. i:=sizeof(buf)
  900. else
  901. i:=sizeleft;
  902. ppufile.getdata(buf,i);
  903. generictokenbuf.write(buf,i);
  904. dec(sizeleft,i);
  905. end;
  906. end;
  907. if df_specialization in defoptions then
  908. ppufile.getderef(genericdefderef);
  909. end;
  910. procedure Tstoreddef.reset;
  911. begin
  912. if assigned(rttitablesym) then
  913. trttisym(rttitablesym).lab := nil;
  914. if assigned(inittablesym) then
  915. trttisym(inittablesym).lab := nil;
  916. localrttilab[initrtti]:=nil;
  917. localrttilab[fullrtti]:=nil;
  918. end;
  919. function tstoreddef.getcopy : tstoreddef;
  920. begin
  921. Message(sym_e_cant_create_unique_type);
  922. getcopy:=terrordef.create;
  923. end;
  924. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  925. var
  926. sizeleft,i : longint;
  927. buf : array[0..255] of byte;
  928. oldintfcrc : boolean;
  929. begin
  930. ppufile.putword(indexnr);
  931. ppufile.putderef(typesymderef);
  932. ppufile.putsmallset(defoptions);
  933. if df_has_rttitable in defoptions then
  934. ppufile.putderef(rttitablesymderef);
  935. if df_has_inittable in defoptions then
  936. ppufile.putderef(inittablesymderef);
  937. if df_generic in defoptions then
  938. begin
  939. oldintfcrc:=ppufile.do_interface_crc;
  940. ppufile.do_interface_crc:=false;
  941. if assigned(generictokenbuf) then
  942. begin
  943. sizeleft:=generictokenbuf.size;
  944. generictokenbuf.seek(0);
  945. end
  946. else
  947. sizeleft:=0;
  948. ppufile.putlongint(sizeleft);
  949. while sizeleft>0 do
  950. begin
  951. if sizeleft>sizeof(buf) then
  952. i:=sizeof(buf)
  953. else
  954. i:=sizeleft;
  955. generictokenbuf.read(buf,i);
  956. ppufile.putdata(buf,i);
  957. dec(sizeleft,i);
  958. end;
  959. ppufile.do_interface_crc:=oldintfcrc;
  960. end;
  961. if df_specialization in defoptions then
  962. ppufile.putderef(genericdefderef);
  963. end;
  964. procedure tstoreddef.buildderef;
  965. begin
  966. typesymderef.build(typesym);
  967. rttitablesymderef.build(rttitablesym);
  968. inittablesymderef.build(inittablesym);
  969. genericdefderef.build(genericdef);
  970. end;
  971. procedure tstoreddef.buildderefimpl;
  972. begin
  973. end;
  974. procedure tstoreddef.deref;
  975. begin
  976. typesym:=ttypesym(typesymderef.resolve);
  977. if df_has_rttitable in defoptions then
  978. rttitablesym:=trttisym(rttitablesymderef.resolve);
  979. if df_has_inittable in defoptions then
  980. inittablesym:=trttisym(inittablesymderef.resolve);
  981. if df_specialization in defoptions then
  982. genericdef:=tstoreddef(genericdefderef.resolve);
  983. end;
  984. procedure tstoreddef.derefimpl;
  985. begin
  986. end;
  987. function tstoreddef.size : aint;
  988. begin
  989. size:=savesize;
  990. end;
  991. function tstoreddef.getvartype:longint;
  992. begin
  993. result:=varUndefined;
  994. end;
  995. function tstoreddef.alignment : shortint;
  996. begin
  997. { natural alignment by default }
  998. alignment:=size_2_align(savesize);
  999. end;
  1000. procedure tstoreddef.write_rtti_name;
  1001. var
  1002. str : string;
  1003. begin
  1004. { name }
  1005. if assigned(typesym) then
  1006. begin
  1007. str:=ttypesym(typesym).realname;
  1008. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(chr(length(str))+str));
  1009. end
  1010. else
  1011. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0))
  1012. end;
  1013. procedure tstoreddef.write_rtti_data(rt:trttitype);
  1014. begin
  1015. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  1016. write_rtti_name;
  1017. end;
  1018. procedure tstoreddef.write_child_rtti_data(rt:trttitype);
  1019. begin
  1020. end;
  1021. function tstoreddef.get_rtti_label(rt:trttitype) : tasmsymbol;
  1022. begin
  1023. { try to reuse persistent rtti data }
  1024. if (rt=fullrtti) and (df_has_rttitable in defoptions) then
  1025. get_rtti_label:=trttisym(rttitablesym).get_label
  1026. else
  1027. if (rt=initrtti) and (df_has_inittable in defoptions) then
  1028. get_rtti_label:=trttisym(inittablesym).get_label
  1029. else
  1030. begin
  1031. if not assigned(localrttilab[rt]) then
  1032. begin
  1033. current_asmdata.getdatalabel(localrttilab[rt]);
  1034. write_child_rtti_data(rt);
  1035. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1036. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,localrttilab[rt].name,const_align(sizeof(aint)));
  1037. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(localrttilab[rt],0));
  1038. write_rtti_data(rt);
  1039. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(localrttilab[rt]));
  1040. end;
  1041. get_rtti_label:=localrttilab[rt];
  1042. end;
  1043. end;
  1044. { returns true, if the definition can be published }
  1045. function tstoreddef.is_publishable : boolean;
  1046. begin
  1047. is_publishable:=false;
  1048. end;
  1049. { needs an init table }
  1050. function tstoreddef.needs_inittable : boolean;
  1051. begin
  1052. needs_inittable:=false;
  1053. end;
  1054. function tstoreddef.is_intregable : boolean;
  1055. var
  1056. recsize,temp: longint;
  1057. begin
  1058. is_intregable:=false;
  1059. case deftype of
  1060. orddef,
  1061. pointerdef,
  1062. enumdef,
  1063. classrefdef:
  1064. is_intregable:=true;
  1065. procvardef :
  1066. is_intregable:=not(po_methodpointer in tprocvardef(self).procoptions);
  1067. objectdef:
  1068. is_intregable:=is_class(self) or is_interface(self);
  1069. setdef:
  1070. is_intregable:=(tsetdef(self).settype=smallset);
  1071. recorddef:
  1072. begin
  1073. recsize:=size;
  1074. is_intregable:=
  1075. ispowerof2(recsize,temp) and
  1076. (recsize <= sizeof(aint));
  1077. end;
  1078. end;
  1079. end;
  1080. function tstoreddef.is_fpuregable : boolean;
  1081. begin
  1082. {$ifdef x86}
  1083. result:=use_sse(self);
  1084. {$else x86}
  1085. result:=(deftype=floatdef) and not(cs_fp_emulation in aktmoduleswitches);
  1086. {$endif x86}
  1087. end;
  1088. procedure tstoreddef.initgeneric;
  1089. begin
  1090. if assigned(generictokenbuf) then
  1091. internalerror(200512131);
  1092. generictokenbuf:=tdynamicarray.create(256);
  1093. end;
  1094. {****************************************************************************
  1095. Tstringdef
  1096. ****************************************************************************}
  1097. constructor tstringdef.createshort(l : byte);
  1098. begin
  1099. inherited create(stringdef);
  1100. string_typ:=st_shortstring;
  1101. len:=l;
  1102. savesize:=len+1;
  1103. end;
  1104. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1105. begin
  1106. inherited ppuload(stringdef,ppufile);
  1107. string_typ:=st_shortstring;
  1108. len:=ppufile.getbyte;
  1109. savesize:=len+1;
  1110. end;
  1111. constructor tstringdef.createlong(l : aint);
  1112. begin
  1113. inherited create(stringdef);
  1114. string_typ:=st_longstring;
  1115. len:=l;
  1116. savesize:=sizeof(aint);
  1117. end;
  1118. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1119. begin
  1120. inherited ppuload(stringdef,ppufile);
  1121. string_typ:=st_longstring;
  1122. len:=ppufile.getaint;
  1123. savesize:=sizeof(aint);
  1124. end;
  1125. constructor tstringdef.createansi(l:aint);
  1126. begin
  1127. inherited create(stringdef);
  1128. string_typ:=st_ansistring;
  1129. len:=l;
  1130. savesize:=sizeof(aint);
  1131. end;
  1132. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1133. begin
  1134. inherited ppuload(stringdef,ppufile);
  1135. string_typ:=st_ansistring;
  1136. len:=ppufile.getaint;
  1137. savesize:=sizeof(aint);
  1138. end;
  1139. constructor tstringdef.createwide(l : aint);
  1140. begin
  1141. inherited create(stringdef);
  1142. string_typ:=st_widestring;
  1143. len:=l;
  1144. savesize:=sizeof(aint);
  1145. end;
  1146. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1147. begin
  1148. inherited ppuload(stringdef,ppufile);
  1149. string_typ:=st_widestring;
  1150. len:=ppufile.getaint;
  1151. savesize:=sizeof(aint);
  1152. end;
  1153. function tstringdef.getcopy : tstoreddef;
  1154. begin
  1155. result:=tstringdef.create(deftype);
  1156. result.deftype:=stringdef;
  1157. tstringdef(result).string_typ:=string_typ;
  1158. tstringdef(result).len:=len;
  1159. tstringdef(result).savesize:=savesize;
  1160. end;
  1161. function tstringdef.stringtypname:string;
  1162. const
  1163. typname:array[tstringtype] of string[8]=(
  1164. 'shortstr','longstr','ansistr','widestr'
  1165. );
  1166. begin
  1167. stringtypname:=typname[string_typ];
  1168. end;
  1169. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1170. begin
  1171. inherited ppuwrite(ppufile);
  1172. if string_typ=st_shortstring then
  1173. begin
  1174. {$ifdef extdebug}
  1175. if len > 255 then internalerror(12122002);
  1176. {$endif}
  1177. ppufile.putbyte(byte(len))
  1178. end
  1179. else
  1180. ppufile.putaint(len);
  1181. case string_typ of
  1182. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1183. st_longstring : ppufile.writeentry(iblongstringdef);
  1184. st_ansistring : ppufile.writeentry(ibansistringdef);
  1185. st_widestring : ppufile.writeentry(ibwidestringdef);
  1186. end;
  1187. end;
  1188. function tstringdef.needs_inittable : boolean;
  1189. begin
  1190. needs_inittable:=string_typ in [st_ansistring,st_widestring];
  1191. end;
  1192. function tstringdef.gettypename : string;
  1193. const
  1194. names : array[tstringtype] of string[11] = (
  1195. 'ShortString','LongString','AnsiString','WideString');
  1196. begin
  1197. gettypename:=names[string_typ];
  1198. end;
  1199. function tstringdef.alignment : shortint;
  1200. begin
  1201. case string_typ of
  1202. st_widestring,
  1203. st_ansistring:
  1204. alignment:=size_2_align(savesize);
  1205. st_longstring,
  1206. st_shortstring:
  1207. {$ifdef cpurequiresproperalignment}
  1208. { char to string accesses byte 0 and 1 with one word access }
  1209. alignment:=size_2_align(2);
  1210. {$else cpurequiresproperalignment}
  1211. alignment:=size_2_align(1);
  1212. {$endif cpurequiresproperalignment}
  1213. else
  1214. internalerror(200412301);
  1215. end;
  1216. end;
  1217. procedure tstringdef.write_rtti_data(rt:trttitype);
  1218. begin
  1219. case string_typ of
  1220. st_ansistring:
  1221. begin
  1222. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkAString));
  1223. write_rtti_name;
  1224. end;
  1225. st_widestring:
  1226. begin
  1227. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWString));
  1228. write_rtti_name;
  1229. end;
  1230. st_longstring:
  1231. begin
  1232. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkLString));
  1233. write_rtti_name;
  1234. end;
  1235. st_shortstring:
  1236. begin
  1237. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSString));
  1238. write_rtti_name;
  1239. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(len));
  1240. {$ifdef cpurequiresproperalignment}
  1241. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1242. {$endif cpurequiresproperalignment}
  1243. end;
  1244. end;
  1245. end;
  1246. function tstringdef.getmangledparaname : string;
  1247. begin
  1248. getmangledparaname:='STRING';
  1249. end;
  1250. function tstringdef.is_publishable : boolean;
  1251. begin
  1252. is_publishable:=true;
  1253. end;
  1254. {****************************************************************************
  1255. TENUMDEF
  1256. ****************************************************************************}
  1257. constructor tenumdef.create;
  1258. begin
  1259. inherited create(enumdef);
  1260. minval:=0;
  1261. maxval:=0;
  1262. calcsavesize;
  1263. has_jumps:=false;
  1264. basedef:=nil;
  1265. firstenum:=nil;
  1266. end;
  1267. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1268. begin
  1269. inherited create(enumdef);
  1270. minval:=_min;
  1271. maxval:=_max;
  1272. basedef:=_basedef;
  1273. calcsavesize;
  1274. has_jumps:=false;
  1275. firstenum:=basedef.firstenum;
  1276. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1277. firstenum:=tenumsym(firstenum).nextenum;
  1278. end;
  1279. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1280. begin
  1281. inherited ppuload(enumdef,ppufile);
  1282. ppufile.getderef(basedefderef);
  1283. minval:=ppufile.getaint;
  1284. maxval:=ppufile.getaint;
  1285. savesize:=ppufile.getaint;
  1286. has_jumps:=false;
  1287. firstenum:=Nil;
  1288. end;
  1289. function tenumdef.getcopy : tstoreddef;
  1290. begin
  1291. if assigned(basedef) then
  1292. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1293. else
  1294. begin
  1295. result:=tenumdef.create;
  1296. tenumdef(result).minval:=minval;
  1297. tenumdef(result).maxval:=maxval;
  1298. end;
  1299. tenumdef(result).has_jumps:=has_jumps;
  1300. tenumdef(result).firstenum:=firstenum;
  1301. tenumdef(result).basedefderef:=basedefderef;
  1302. end;
  1303. procedure tenumdef.calcsavesize;
  1304. begin
  1305. if (aktpackenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1306. savesize:=8
  1307. else
  1308. if (aktpackenum=4) or (min<low(smallint)) or (max>high(word)) then
  1309. savesize:=4
  1310. else
  1311. if (aktpackenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1312. savesize:=2
  1313. else
  1314. savesize:=1;
  1315. end;
  1316. function tenumdef.packedbitsize: aint;
  1317. var
  1318. power: longint;
  1319. begin
  1320. result := 0;
  1321. if (minval < 0) then
  1322. result := inherited packedbitsize
  1323. else
  1324. begin
  1325. if (maxval <= 1) then
  1326. result := 1
  1327. else
  1328. begin
  1329. { 256 must become 512 etc. }
  1330. nextpowerof2(maxval+1,power);
  1331. result := power;
  1332. end;
  1333. end;
  1334. end;
  1335. procedure tenumdef.setmax(_max:aint);
  1336. begin
  1337. maxval:=_max;
  1338. calcsavesize;
  1339. end;
  1340. procedure tenumdef.setmin(_min:aint);
  1341. begin
  1342. minval:=_min;
  1343. calcsavesize;
  1344. end;
  1345. function tenumdef.min:aint;
  1346. begin
  1347. min:=minval;
  1348. end;
  1349. function tenumdef.max:aint;
  1350. begin
  1351. max:=maxval;
  1352. end;
  1353. procedure tenumdef.buildderef;
  1354. begin
  1355. inherited buildderef;
  1356. basedefderef.build(basedef);
  1357. end;
  1358. procedure tenumdef.deref;
  1359. begin
  1360. inherited deref;
  1361. basedef:=tenumdef(basedefderef.resolve);
  1362. { restart ordering }
  1363. firstenum:=nil;
  1364. end;
  1365. procedure tenumdef.derefimpl;
  1366. begin
  1367. if assigned(basedef) and
  1368. (firstenum=nil) then
  1369. begin
  1370. firstenum:=basedef.firstenum;
  1371. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1372. firstenum:=tenumsym(firstenum).nextenum;
  1373. end;
  1374. end;
  1375. destructor tenumdef.destroy;
  1376. begin
  1377. inherited destroy;
  1378. end;
  1379. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1380. begin
  1381. inherited ppuwrite(ppufile);
  1382. ppufile.putderef(basedefderef);
  1383. ppufile.putaint(min);
  1384. ppufile.putaint(max);
  1385. ppufile.putaint(savesize);
  1386. ppufile.writeentry(ibenumdef);
  1387. end;
  1388. procedure tenumdef.write_child_rtti_data(rt:trttitype);
  1389. begin
  1390. if assigned(basedef) then
  1391. basedef.get_rtti_label(rt);
  1392. end;
  1393. procedure tenumdef.write_rtti_data(rt:trttitype);
  1394. var
  1395. hp : tenumsym;
  1396. begin
  1397. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkEnumeration));
  1398. write_rtti_name;
  1399. {$ifdef cpurequiresproperalignment}
  1400. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1401. {$endif cpurequiresproperalignment}
  1402. case longint(savesize) of
  1403. 1:
  1404. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  1405. 2:
  1406. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  1407. 4:
  1408. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  1409. end;
  1410. {$ifdef cpurequiresproperalignment}
  1411. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1412. {$endif cpurequiresproperalignment}
  1413. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(min));
  1414. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(max));
  1415. if assigned(basedef) then
  1416. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(basedef.get_rtti_label(rt)))
  1417. else
  1418. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  1419. hp:=tenumsym(firstenum);
  1420. while assigned(hp) do
  1421. begin
  1422. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(hp.realname)));
  1423. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(hp.realname));
  1424. hp:=hp.nextenum;
  1425. end;
  1426. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  1427. end;
  1428. function tenumdef.is_publishable : boolean;
  1429. begin
  1430. is_publishable:=true;
  1431. end;
  1432. function tenumdef.gettypename : string;
  1433. begin
  1434. gettypename:='<enumeration type>';
  1435. end;
  1436. {****************************************************************************
  1437. TORDDEF
  1438. ****************************************************************************}
  1439. constructor torddef.create(t : tbasetype;v,b : TConstExprInt);
  1440. begin
  1441. inherited create(orddef);
  1442. low:=v;
  1443. high:=b;
  1444. typ:=t;
  1445. setsize;
  1446. end;
  1447. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1448. begin
  1449. inherited ppuload(orddef,ppufile);
  1450. typ:=tbasetype(ppufile.getbyte);
  1451. if sizeof(TConstExprInt)=8 then
  1452. begin
  1453. low:=ppufile.getint64;
  1454. high:=ppufile.getint64;
  1455. end
  1456. else
  1457. begin
  1458. low:=ppufile.getlongint;
  1459. high:=ppufile.getlongint;
  1460. end;
  1461. setsize;
  1462. end;
  1463. function torddef.getcopy : tstoreddef;
  1464. begin
  1465. result:=torddef.create(typ,low,high);
  1466. result.deftype:=orddef;
  1467. torddef(result).low:=low;
  1468. torddef(result).high:=high;
  1469. torddef(result).typ:=typ;
  1470. torddef(result).savesize:=savesize;
  1471. end;
  1472. function torddef.alignment:shortint;
  1473. begin
  1474. if (target_info.system = system_i386_darwin) and
  1475. (typ in [s64bit,u64bit]) then
  1476. result := 4
  1477. else
  1478. result := inherited alignment;
  1479. end;
  1480. procedure torddef.setsize;
  1481. const
  1482. sizetbl : array[tbasetype] of longint = (
  1483. 0,
  1484. 1,2,4,8,
  1485. 1,2,4,8,
  1486. 1,2,4,8,
  1487. 1,2,8
  1488. );
  1489. begin
  1490. savesize:=sizetbl[typ];
  1491. end;
  1492. function torddef.packedbitsize: aint;
  1493. var
  1494. power: longint;
  1495. begin
  1496. result := 0;
  1497. if typ = uvoid then
  1498. exit;
  1499. if (low < 0) then
  1500. result := inherited packedbitsize
  1501. else
  1502. begin
  1503. if (high <= 1) then
  1504. result := 1
  1505. else if (typ = u64bit) then
  1506. result := 64
  1507. else
  1508. begin
  1509. { 256 must become 512 etc. }
  1510. nextpowerof2(high+1,power);
  1511. result := power;
  1512. end;
  1513. end;
  1514. end;
  1515. function torddef.getvartype : longint;
  1516. const
  1517. basetype2vartype : array[tbasetype] of longint = (
  1518. varUndefined,
  1519. varbyte,varqword,varlongword,varqword,
  1520. varshortint,varsmallint,varinteger,varint64,
  1521. varboolean,varUndefined,varUndefined,varUndefined,
  1522. varUndefined,varUndefined,varCurrency);
  1523. begin
  1524. result:=basetype2vartype[typ];
  1525. end;
  1526. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1527. begin
  1528. inherited ppuwrite(ppufile);
  1529. ppufile.putbyte(byte(typ));
  1530. if sizeof(TConstExprInt)=8 then
  1531. begin
  1532. ppufile.putint64(low);
  1533. ppufile.putint64(high);
  1534. end
  1535. else
  1536. begin
  1537. ppufile.putlongint(low);
  1538. ppufile.putlongint(high);
  1539. end;
  1540. ppufile.writeentry(iborddef);
  1541. end;
  1542. procedure torddef.write_rtti_data(rt:trttitype);
  1543. procedure dointeger;
  1544. const
  1545. trans : array[tbasetype] of byte =
  1546. (otUByte{otNone},
  1547. otUByte,otUWord,otULong,otUByte{otNone},
  1548. otSByte,otSWord,otSLong,otUByte{otNone},
  1549. otUByte,otUWord,otULong,otUByte,
  1550. otUByte,otUWord,otUByte);
  1551. begin
  1552. write_rtti_name;
  1553. {$ifdef cpurequiresproperalignment}
  1554. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1555. {$endif cpurequiresproperalignment}
  1556. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[typ])));
  1557. {$ifdef cpurequiresproperalignment}
  1558. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1559. {$endif cpurequiresproperalignment}
  1560. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(low)));
  1561. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(high)));
  1562. end;
  1563. begin
  1564. case typ of
  1565. s64bit :
  1566. begin
  1567. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInt64));
  1568. write_rtti_name;
  1569. {$ifdef cpurequiresproperalignment}
  1570. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1571. {$endif cpurequiresproperalignment}
  1572. { low }
  1573. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(int64($80000000) shl 32));
  1574. { high }
  1575. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit((int64($7fffffff) shl 32) or int64($ffffffff)));
  1576. end;
  1577. u64bit :
  1578. begin
  1579. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkQWord));
  1580. write_rtti_name;
  1581. {$ifdef cpurequiresproperalignment}
  1582. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1583. {$endif cpurequiresproperalignment}
  1584. { low }
  1585. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(0));
  1586. { high }
  1587. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(int64((int64($ffffffff) shl 32) or int64($ffffffff))));
  1588. end;
  1589. bool8bit:
  1590. begin
  1591. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkBool));
  1592. dointeger;
  1593. end;
  1594. uchar:
  1595. begin
  1596. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkChar));
  1597. dointeger;
  1598. end;
  1599. uwidechar:
  1600. begin
  1601. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWChar));
  1602. dointeger;
  1603. end;
  1604. else
  1605. begin
  1606. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInteger));
  1607. dointeger;
  1608. end;
  1609. end;
  1610. end;
  1611. function torddef.is_publishable : boolean;
  1612. begin
  1613. is_publishable:=(typ<>uvoid);
  1614. end;
  1615. function torddef.gettypename : string;
  1616. const
  1617. names : array[tbasetype] of string[20] = (
  1618. 'untyped',
  1619. 'Byte','Word','DWord','QWord',
  1620. 'ShortInt','SmallInt','LongInt','Int64',
  1621. 'Boolean','WordBool','LongBool','QWordBool',
  1622. 'Char','WideChar','Currency');
  1623. begin
  1624. gettypename:=names[typ];
  1625. end;
  1626. {****************************************************************************
  1627. TFLOATDEF
  1628. ****************************************************************************}
  1629. constructor tfloatdef.create(t : tfloattype);
  1630. begin
  1631. inherited create(floatdef);
  1632. typ:=t;
  1633. setsize;
  1634. end;
  1635. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1636. begin
  1637. inherited ppuload(floatdef,ppufile);
  1638. typ:=tfloattype(ppufile.getbyte);
  1639. setsize;
  1640. end;
  1641. function tfloatdef.getcopy : tstoreddef;
  1642. begin
  1643. result:=tfloatdef.create(typ);
  1644. result.deftype:=floatdef;
  1645. tfloatdef(result).savesize:=savesize;
  1646. end;
  1647. function tfloatdef.alignment:shortint;
  1648. begin
  1649. if (target_info.system = system_i386_darwin) then
  1650. case typ of
  1651. s80real : result:=16;
  1652. s64real,
  1653. s64currency,
  1654. s64comp : result:=4;
  1655. else
  1656. result := inherited alignment;
  1657. end
  1658. else
  1659. result := inherited alignment;
  1660. end;
  1661. procedure tfloatdef.setsize;
  1662. begin
  1663. case typ of
  1664. s32real : savesize:=4;
  1665. s80real : savesize:=10;
  1666. s64real,
  1667. s64currency,
  1668. s64comp : savesize:=8;
  1669. else
  1670. savesize:=0;
  1671. end;
  1672. end;
  1673. function tfloatdef.getvartype : longint;
  1674. const
  1675. floattype2vartype : array[tfloattype] of longint = (
  1676. varSingle,varDouble,varUndefined,
  1677. varUndefined,varCurrency,varUndefined);
  1678. begin
  1679. if (upper(typename)='TDATETIME') and
  1680. assigned(owner) and
  1681. assigned(owner.name) and
  1682. (owner.name^='SYSTEM') then
  1683. result:=varDate
  1684. else
  1685. result:=floattype2vartype[typ];
  1686. end;
  1687. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1688. begin
  1689. inherited ppuwrite(ppufile);
  1690. ppufile.putbyte(byte(typ));
  1691. ppufile.writeentry(ibfloatdef);
  1692. end;
  1693. procedure tfloatdef.write_rtti_data(rt:trttitype);
  1694. const
  1695. {tfloattype = (s32real,s64real,s80real,s64bit,s128bit);}
  1696. translate : array[tfloattype] of byte =
  1697. (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,ftFloat128);
  1698. begin
  1699. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  1700. write_rtti_name;
  1701. {$ifdef cpurequiresproperalignment}
  1702. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1703. {$endif cpurequiresproperalignment}
  1704. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[typ]));
  1705. end;
  1706. function tfloatdef.is_publishable : boolean;
  1707. begin
  1708. is_publishable:=true;
  1709. end;
  1710. function tfloatdef.gettypename : string;
  1711. const
  1712. names : array[tfloattype] of string[20] = (
  1713. 'Single','Double','Extended','Comp','Currency','Float128');
  1714. begin
  1715. gettypename:=names[typ];
  1716. end;
  1717. {****************************************************************************
  1718. TFILEDEF
  1719. ****************************************************************************}
  1720. constructor tfiledef.createtext;
  1721. begin
  1722. inherited create(filedef);
  1723. filetyp:=ft_text;
  1724. typedfiletype.reset;
  1725. setsize;
  1726. end;
  1727. constructor tfiledef.createuntyped;
  1728. begin
  1729. inherited create(filedef);
  1730. filetyp:=ft_untyped;
  1731. typedfiletype.reset;
  1732. setsize;
  1733. end;
  1734. constructor tfiledef.createtyped(const tt : ttype);
  1735. begin
  1736. inherited create(filedef);
  1737. filetyp:=ft_typed;
  1738. typedfiletype:=tt;
  1739. setsize;
  1740. end;
  1741. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1742. begin
  1743. inherited ppuload(filedef,ppufile);
  1744. filetyp:=tfiletyp(ppufile.getbyte);
  1745. if filetyp=ft_typed then
  1746. ppufile.gettype(typedfiletype)
  1747. else
  1748. typedfiletype.reset;
  1749. setsize;
  1750. end;
  1751. function tfiledef.getcopy : tstoreddef;
  1752. begin
  1753. case filetyp of
  1754. ft_typed:
  1755. result:=tfiledef.createtyped(typedfiletype);
  1756. ft_untyped:
  1757. result:=tfiledef.createuntyped;
  1758. ft_text:
  1759. result:=tfiledef.createtext;
  1760. else
  1761. internalerror(2004121201);
  1762. end;
  1763. end;
  1764. procedure tfiledef.buildderef;
  1765. begin
  1766. inherited buildderef;
  1767. if filetyp=ft_typed then
  1768. typedfiletype.buildderef;
  1769. end;
  1770. procedure tfiledef.deref;
  1771. begin
  1772. inherited deref;
  1773. if filetyp=ft_typed then
  1774. typedfiletype.resolve;
  1775. end;
  1776. procedure tfiledef.setsize;
  1777. begin
  1778. {$ifdef cpu64bit}
  1779. case filetyp of
  1780. ft_text :
  1781. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1782. savesize:=632
  1783. else
  1784. savesize:=628;
  1785. ft_typed,
  1786. ft_untyped :
  1787. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1788. savesize:=372
  1789. else
  1790. savesize:=368;
  1791. end;
  1792. {$else cpu64bit}
  1793. case filetyp of
  1794. ft_text :
  1795. savesize:=592;
  1796. ft_typed,
  1797. ft_untyped :
  1798. savesize:=332;
  1799. end;
  1800. {$endif cpu64bit}
  1801. end;
  1802. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1803. begin
  1804. inherited ppuwrite(ppufile);
  1805. ppufile.putbyte(byte(filetyp));
  1806. if filetyp=ft_typed then
  1807. ppufile.puttype(typedfiletype);
  1808. ppufile.writeentry(ibfiledef);
  1809. end;
  1810. function tfiledef.gettypename : string;
  1811. begin
  1812. case filetyp of
  1813. ft_untyped:
  1814. gettypename:='File';
  1815. ft_typed:
  1816. gettypename:='File Of '+typedfiletype.def.typename;
  1817. ft_text:
  1818. gettypename:='Text'
  1819. end;
  1820. end;
  1821. function tfiledef.getmangledparaname : string;
  1822. begin
  1823. case filetyp of
  1824. ft_untyped:
  1825. getmangledparaname:='FILE';
  1826. ft_typed:
  1827. getmangledparaname:='FILE$OF$'+typedfiletype.def.mangledparaname;
  1828. ft_text:
  1829. getmangledparaname:='TEXT'
  1830. end;
  1831. end;
  1832. {****************************************************************************
  1833. TVARIANTDEF
  1834. ****************************************************************************}
  1835. constructor tvariantdef.create(v : tvarianttype);
  1836. begin
  1837. inherited create(variantdef);
  1838. varianttype:=v;
  1839. setsize;
  1840. end;
  1841. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1842. begin
  1843. inherited ppuload(variantdef,ppufile);
  1844. varianttype:=tvarianttype(ppufile.getbyte);
  1845. setsize;
  1846. end;
  1847. function tvariantdef.getcopy : tstoreddef;
  1848. begin
  1849. result:=tvariantdef.create(varianttype);
  1850. end;
  1851. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1852. begin
  1853. inherited ppuwrite(ppufile);
  1854. ppufile.putbyte(byte(varianttype));
  1855. ppufile.writeentry(ibvariantdef);
  1856. end;
  1857. procedure tvariantdef.setsize;
  1858. begin
  1859. {$ifdef cpu64bit}
  1860. savesize:=24;
  1861. {$else cpu64bit}
  1862. savesize:=16;
  1863. {$endif cpu64bit}
  1864. end;
  1865. function tvariantdef.gettypename : string;
  1866. begin
  1867. case varianttype of
  1868. vt_normalvariant:
  1869. gettypename:='Variant';
  1870. vt_olevariant:
  1871. gettypename:='OleVariant';
  1872. end;
  1873. end;
  1874. procedure tvariantdef.write_rtti_data(rt:trttitype);
  1875. begin
  1876. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkVariant));
  1877. end;
  1878. function tvariantdef.needs_inittable : boolean;
  1879. begin
  1880. needs_inittable:=true;
  1881. end;
  1882. function tvariantdef.is_publishable : boolean;
  1883. begin
  1884. is_publishable:=true;
  1885. end;
  1886. {****************************************************************************
  1887. TABSTRACTPOINTERDEF
  1888. ****************************************************************************}
  1889. constructor tabstractpointerdef.create(dt:tdeftype;const tt : ttype);
  1890. begin
  1891. inherited create(dt);
  1892. pointertype:=tt;
  1893. savesize:=sizeof(aint);
  1894. end;
  1895. constructor tabstractpointerdef.ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  1896. begin
  1897. inherited ppuload(dt,ppufile);
  1898. ppufile.gettype(pointertype);
  1899. savesize:=sizeof(aint);
  1900. end;
  1901. procedure tabstractpointerdef.buildderef;
  1902. begin
  1903. inherited buildderef;
  1904. pointertype.buildderef;
  1905. end;
  1906. procedure tabstractpointerdef.deref;
  1907. begin
  1908. inherited deref;
  1909. pointertype.resolve;
  1910. end;
  1911. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1912. begin
  1913. inherited ppuwrite(ppufile);
  1914. ppufile.puttype(pointertype);
  1915. end;
  1916. {****************************************************************************
  1917. TPOINTERDEF
  1918. ****************************************************************************}
  1919. constructor tpointerdef.create(const tt : ttype);
  1920. begin
  1921. inherited create(pointerdef,tt);
  1922. is_far:=false;
  1923. end;
  1924. constructor tpointerdef.createfar(const tt : ttype);
  1925. begin
  1926. inherited create(pointerdef,tt);
  1927. is_far:=true;
  1928. end;
  1929. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1930. begin
  1931. inherited ppuload(pointerdef,ppufile);
  1932. is_far:=(ppufile.getbyte<>0);
  1933. end;
  1934. function tpointerdef.getcopy : tstoreddef;
  1935. begin
  1936. result:=tpointerdef.create(pointertype);
  1937. tpointerdef(result).is_far:=is_far;
  1938. tpointerdef(result).savesize:=savesize;
  1939. end;
  1940. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1941. begin
  1942. inherited ppuwrite(ppufile);
  1943. ppufile.putbyte(byte(is_far));
  1944. ppufile.writeentry(ibpointerdef);
  1945. end;
  1946. function tpointerdef.gettypename : string;
  1947. begin
  1948. if is_far then
  1949. gettypename:='^'+pointertype.def.typename+';far'
  1950. else
  1951. gettypename:='^'+pointertype.def.typename;
  1952. end;
  1953. {****************************************************************************
  1954. TCLASSREFDEF
  1955. ****************************************************************************}
  1956. constructor tclassrefdef.create(const t:ttype);
  1957. begin
  1958. inherited create(classrefdef,t);
  1959. end;
  1960. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1961. begin
  1962. inherited ppuload(classrefdef,ppufile);
  1963. end;
  1964. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1965. begin
  1966. inherited ppuwrite(ppufile);
  1967. ppufile.writeentry(ibclassrefdef);
  1968. end;
  1969. function tclassrefdef.gettypename : string;
  1970. begin
  1971. gettypename:='Class Of '+pointertype.def.typename;
  1972. end;
  1973. function tclassrefdef.is_publishable : boolean;
  1974. begin
  1975. result:=true;
  1976. end;
  1977. {***************************************************************************
  1978. TSETDEF
  1979. ***************************************************************************}
  1980. constructor tsetdef.create(const t:ttype;high : aint);
  1981. begin
  1982. inherited create(setdef);
  1983. elementtype:=t;
  1984. // setbase:=low;
  1985. setmax:=high;
  1986. if high<32 then
  1987. begin
  1988. settype:=smallset;
  1989. {$ifdef testvarsets}
  1990. if aktsetalloc=0 THEN { $PACKSET Fixed?}
  1991. {$endif}
  1992. savesize:=Sizeof(longint)
  1993. {$ifdef testvarsets}
  1994. else {No, use $PACKSET VALUE for rounding}
  1995. savesize:=aktsetalloc*((high+aktsetalloc*8-1) DIV (aktsetalloc*8))
  1996. {$endif}
  1997. ;
  1998. end
  1999. else
  2000. if high<256 then
  2001. begin
  2002. settype:=normset;
  2003. savesize:=32;
  2004. end
  2005. else
  2006. {$ifdef testvarsets}
  2007. if high<$10000 then
  2008. begin
  2009. settype:=varset;
  2010. savesize:=4*((high+31) div 32);
  2011. end
  2012. else
  2013. {$endif testvarsets}
  2014. Message(sym_e_ill_type_decl_set);
  2015. end;
  2016. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2017. begin
  2018. inherited ppuload(setdef,ppufile);
  2019. ppufile.gettype(elementtype);
  2020. settype:=tsettype(ppufile.getbyte);
  2021. case settype of
  2022. normset : savesize:=32;
  2023. varset : savesize:=ppufile.getlongint;
  2024. smallset : savesize:=Sizeof(longint);
  2025. end;
  2026. end;
  2027. destructor tsetdef.destroy;
  2028. begin
  2029. inherited destroy;
  2030. end;
  2031. function tsetdef.getcopy : tstoreddef;
  2032. begin
  2033. case settype of
  2034. smallset:
  2035. result:=tsetdef.create(elementtype,31);
  2036. normset:
  2037. result:=tsetdef.create(elementtype,255);
  2038. else
  2039. internalerror(2004121202);
  2040. end;
  2041. end;
  2042. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2043. begin
  2044. inherited ppuwrite(ppufile);
  2045. ppufile.puttype(elementtype);
  2046. ppufile.putbyte(byte(settype));
  2047. if settype=varset then
  2048. ppufile.putlongint(savesize);
  2049. if settype=normset then
  2050. ppufile.putaint(savesize);
  2051. ppufile.writeentry(ibsetdef);
  2052. end;
  2053. procedure tsetdef.buildderef;
  2054. begin
  2055. inherited buildderef;
  2056. elementtype.buildderef;
  2057. end;
  2058. procedure tsetdef.deref;
  2059. begin
  2060. inherited deref;
  2061. elementtype.resolve;
  2062. end;
  2063. procedure tsetdef.write_child_rtti_data(rt:trttitype);
  2064. begin
  2065. tstoreddef(elementtype.def).get_rtti_label(rt);
  2066. end;
  2067. procedure tsetdef.write_rtti_data(rt:trttitype);
  2068. begin
  2069. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSet));
  2070. write_rtti_name;
  2071. {$ifdef cpurequiresproperalignment}
  2072. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2073. {$endif cpurequiresproperalignment}
  2074. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  2075. {$ifdef cpurequiresproperalignment}
  2076. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2077. {$endif cpurequiresproperalignment}
  2078. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(elementtype.def).get_rtti_label(rt)));
  2079. end;
  2080. function tsetdef.is_publishable : boolean;
  2081. begin
  2082. is_publishable:=(settype=smallset);
  2083. end;
  2084. function tsetdef.gettypename : string;
  2085. begin
  2086. if assigned(elementtype.def) then
  2087. gettypename:='Set Of '+elementtype.def.typename
  2088. else
  2089. gettypename:='Empty Set';
  2090. end;
  2091. {***************************************************************************
  2092. TFORMALDEF
  2093. ***************************************************************************}
  2094. constructor tformaldef.create;
  2095. begin
  2096. inherited create(formaldef);
  2097. savesize:=0;
  2098. end;
  2099. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2100. begin
  2101. inherited ppuload(formaldef,ppufile);
  2102. savesize:=0;
  2103. end;
  2104. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2105. begin
  2106. inherited ppuwrite(ppufile);
  2107. ppufile.writeentry(ibformaldef);
  2108. end;
  2109. function tformaldef.gettypename : string;
  2110. begin
  2111. gettypename:='<Formal type>';
  2112. end;
  2113. {***************************************************************************
  2114. TARRAYDEF
  2115. ***************************************************************************}
  2116. constructor tarraydef.create(l,h : aint;const t : ttype);
  2117. begin
  2118. inherited create(arraydef);
  2119. lowrange:=l;
  2120. highrange:=h;
  2121. rangetype:=t;
  2122. elementtype.reset;
  2123. arrayoptions:=[];
  2124. end;
  2125. constructor tarraydef.create_from_pointer(const elemt : ttype);
  2126. begin
  2127. self.create(0,$7fffffff,s32inttype);
  2128. arrayoptions:=[ado_IsConvertedPointer];
  2129. setelementtype(elemt);
  2130. end;
  2131. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2132. begin
  2133. inherited ppuload(arraydef,ppufile);
  2134. { the addresses are calculated later }
  2135. ppufile.gettype(_elementtype);
  2136. ppufile.gettype(rangetype);
  2137. lowrange:=ppufile.getaint;
  2138. highrange:=ppufile.getaint;
  2139. ppufile.getsmallset(arrayoptions);
  2140. end;
  2141. function tarraydef.getcopy : tstoreddef;
  2142. begin
  2143. result:=tarraydef.create(lowrange,highrange,rangetype);
  2144. tarraydef(result).arrayoptions:=arrayoptions;
  2145. tarraydef(result)._elementtype:=_elementtype;
  2146. end;
  2147. procedure tarraydef.buildderef;
  2148. begin
  2149. inherited buildderef;
  2150. _elementtype.buildderef;
  2151. rangetype.buildderef;
  2152. end;
  2153. procedure tarraydef.deref;
  2154. begin
  2155. inherited deref;
  2156. _elementtype.resolve;
  2157. rangetype.resolve;
  2158. end;
  2159. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2160. begin
  2161. inherited ppuwrite(ppufile);
  2162. ppufile.puttype(_elementtype);
  2163. ppufile.puttype(rangetype);
  2164. ppufile.putaint(lowrange);
  2165. ppufile.putaint(highrange);
  2166. ppufile.putsmallset(arrayoptions);
  2167. ppufile.writeentry(ibarraydef);
  2168. end;
  2169. function tarraydef.elesize : aint;
  2170. begin
  2171. if (ado_IsBitPacked in arrayoptions) then
  2172. internalerror(2006080101);
  2173. elesize:=_elementtype.def.size;
  2174. end;
  2175. function tarraydef.elepackedbitsize : aint;
  2176. begin
  2177. if not(ado_IsBitPacked in arrayoptions) then
  2178. internalerror(2006080102);
  2179. result:=_elementtype.def.packedbitsize;
  2180. end;
  2181. function tarraydef.elecount : aint;
  2182. var
  2183. qhigh,qlow : qword;
  2184. begin
  2185. if ado_IsDynamicArray in arrayoptions then
  2186. begin
  2187. result:=0;
  2188. exit;
  2189. end;
  2190. if (highrange>0) and (lowrange<0) then
  2191. begin
  2192. qhigh:=highrange;
  2193. qlow:=qword(-lowrange);
  2194. { prevent overflow, return -1 to indicate overflow }
  2195. if qhigh+qlow>qword(high(aint)-1) then
  2196. result:=-1
  2197. else
  2198. result:=qhigh+qlow+1;
  2199. end
  2200. else
  2201. result:=int64(highrange)-lowrange+1;
  2202. end;
  2203. function tarraydef.size : aint;
  2204. var
  2205. cachedelecount,
  2206. cachedelesize : aint;
  2207. begin
  2208. if ado_IsDynamicArray in arrayoptions then
  2209. begin
  2210. size:=sizeof(aint);
  2211. exit;
  2212. end;
  2213. { Tarraydef.size may never be called for an open array! }
  2214. if highrange<lowrange then
  2215. internalerror(99080501);
  2216. if not (ado_IsBitPacked in arrayoptions) then
  2217. cachedelesize:=elesize
  2218. else
  2219. cachedelesize := elepackedbitsize;
  2220. cachedelecount:=elecount;
  2221. if (cachedelesize = 0) then
  2222. begin
  2223. size := 0;
  2224. exit;
  2225. end;
  2226. if (cachedelecount = -1) then
  2227. begin
  2228. size := -1;
  2229. exit;
  2230. end;
  2231. { prevent overflow, return -1 to indicate overflow }
  2232. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2233. if (cachedelecount > high(aint)) or
  2234. ((high(aint) div cachedelesize) < cachedelecount) or
  2235. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2236. accessing the array, see ncgmem (PFV) }
  2237. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2238. begin
  2239. result:=-1;
  2240. exit;
  2241. end;
  2242. if (ado_IsBitPacked in arrayoptions) then
  2243. size:=align(cachedelesize * cachedelecount,alignment*8) div 8
  2244. else
  2245. result:=cachedelesize*cachedelecount;
  2246. end;
  2247. procedure tarraydef.setelementtype(t: ttype);
  2248. begin
  2249. _elementtype:=t;
  2250. if not((ado_IsDynamicArray in arrayoptions) or
  2251. (ado_IsConvertedPointer in arrayoptions) or
  2252. (highrange<lowrange)) then
  2253. begin
  2254. if (size=-1) then
  2255. Message(sym_e_segment_too_large);
  2256. end;
  2257. end;
  2258. function tarraydef.alignment : shortint;
  2259. begin
  2260. { alignment is the size of the elements }
  2261. if (elementtype.def.deftype in [arraydef,recorddef]) or
  2262. ((elementtype.def.deftype=objectdef) and
  2263. is_object(elementtype.def)) then
  2264. alignment:=elementtype.def.alignment
  2265. else if not (ado_IsBitPacked in arrayoptions) then
  2266. alignment:=size_2_align(elesize)
  2267. else
  2268. alignment:=packedbitsloadsize(elepackedbitsize);
  2269. end;
  2270. function tarraydef.needs_inittable : boolean;
  2271. begin
  2272. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementtype.def.needs_inittable;
  2273. end;
  2274. procedure tarraydef.write_child_rtti_data(rt:trttitype);
  2275. begin
  2276. tstoreddef(elementtype.def).get_rtti_label(rt);
  2277. end;
  2278. procedure tarraydef.write_rtti_data(rt:trttitype);
  2279. begin
  2280. if ado_IsBitPacked in arrayoptions then
  2281. begin
  2282. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  2283. write_rtti_name;
  2284. exit;
  2285. end;
  2286. if ado_IsDynamicArray in arrayoptions then
  2287. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  2288. else
  2289. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  2290. write_rtti_name;
  2291. {$ifdef cpurequiresproperalignment}
  2292. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2293. {$endif cpurequiresproperalignment}
  2294. { size of elements }
  2295. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(elesize));
  2296. if not(ado_IsDynamicArray in arrayoptions) then
  2297. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(elecount));
  2298. { element type }
  2299. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(elementtype.def).get_rtti_label(rt)));
  2300. { variant type }
  2301. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(elementtype.def).getvartype));
  2302. end;
  2303. function tarraydef.gettypename : string;
  2304. begin
  2305. if (ado_IsConstString in arrayoptions) then
  2306. result:='Constant String'
  2307. else if (ado_isarrayofconst in arrayoptions) or
  2308. (ado_isConstructor in arrayoptions) then
  2309. begin
  2310. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2311. gettypename:='Array Of Const'
  2312. else
  2313. gettypename:='Array Of '+elementtype.def.typename;
  2314. end
  2315. else if ((highrange=-1) and (lowrange=0)) or (ado_IsDynamicArray in arrayoptions) then
  2316. gettypename:='Array Of '+elementtype.def.typename
  2317. else
  2318. begin
  2319. result := '';
  2320. if (ado_IsBitPacked in arrayoptions) then
  2321. result:='Packed ';
  2322. if rangetype.def.deftype=enumdef then
  2323. result:=result+'Array['+rangetype.def.typename+'] Of '+elementtype.def.typename
  2324. else
  2325. result:=result+'Array['+tostr(lowrange)+'..'+
  2326. tostr(highrange)+'] Of '+elementtype.def.typename
  2327. end;
  2328. end;
  2329. function tarraydef.getmangledparaname : string;
  2330. begin
  2331. if ado_isarrayofconst in arrayoptions then
  2332. getmangledparaname:='array_of_const'
  2333. else
  2334. if ((highrange=-1) and (lowrange=0)) then
  2335. getmangledparaname:='array_of_'+elementtype.def.mangledparaname
  2336. else
  2337. internalerror(200204176);
  2338. end;
  2339. {***************************************************************************
  2340. tabstractrecorddef
  2341. ***************************************************************************}
  2342. function tabstractrecorddef.getsymtable(t:tgetsymtable):tsymtable;
  2343. begin
  2344. if t=gs_record then
  2345. getsymtable:=symtable
  2346. else
  2347. getsymtable:=nil;
  2348. end;
  2349. procedure tabstractrecorddef.reset;
  2350. begin
  2351. inherited reset;
  2352. tstoredsymtable(symtable).reset_all_defs;
  2353. end;
  2354. function tabstractrecorddef.is_packed:boolean;
  2355. begin
  2356. result:=tabstractrecordsymtable(symtable).is_packed;
  2357. end;
  2358. procedure tabstractrecorddef.count_field_rtti(sym : tnamedindexitem;arg:pointer);
  2359. begin
  2360. if (FRTTIType=fullrtti) or
  2361. ((tsym(sym).typ=fieldvarsym) and
  2362. tfieldvarsym(sym).vartype.def.needs_inittable) then
  2363. inc(Count);
  2364. end;
  2365. procedure tabstractrecorddef.generate_field_rtti(sym:tnamedindexitem;arg:pointer);
  2366. begin
  2367. if (FRTTIType=fullrtti) or
  2368. ((tsym(sym).typ=fieldvarsym) and
  2369. tfieldvarsym(sym).vartype.def.needs_inittable) then
  2370. tstoreddef(tfieldvarsym(sym).vartype.def).get_rtti_label(FRTTIType);
  2371. end;
  2372. procedure tabstractrecorddef.write_field_rtti(sym : tnamedindexitem;arg:pointer);
  2373. begin
  2374. if (FRTTIType=fullrtti) or
  2375. ((tsym(sym).typ=fieldvarsym) and
  2376. tfieldvarsym(sym).vartype.def.needs_inittable) then
  2377. begin
  2378. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(tfieldvarsym(sym).vartype.def).get_rtti_label(FRTTIType)));
  2379. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tfieldvarsym(sym).fieldoffset));
  2380. end;
  2381. end;
  2382. procedure tabstractrecorddef.buildderefimpl;
  2383. begin
  2384. inherited buildderefimpl;
  2385. tstoredsymtable(symtable).buildderefimpl;
  2386. end;
  2387. procedure tabstractrecorddef.derefimpl;
  2388. begin
  2389. inherited derefimpl;
  2390. tstoredsymtable(symtable).derefimpl;
  2391. end;
  2392. {***************************************************************************
  2393. trecorddef
  2394. ***************************************************************************}
  2395. constructor trecorddef.create(p : tsymtable);
  2396. begin
  2397. inherited create(recorddef);
  2398. symtable:=p;
  2399. symtable.defowner:=self;
  2400. isunion:=false;
  2401. end;
  2402. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2403. begin
  2404. inherited ppuload(recorddef,ppufile);
  2405. symtable:=trecordsymtable.create(0);
  2406. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2407. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2408. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2409. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2410. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2411. trecordsymtable(symtable).ppuload(ppufile);
  2412. symtable.defowner:=self;
  2413. isunion:=false;
  2414. end;
  2415. destructor trecorddef.destroy;
  2416. begin
  2417. if assigned(symtable) then
  2418. symtable.free;
  2419. inherited destroy;
  2420. end;
  2421. function trecorddef.getcopy : tstoreddef;
  2422. begin
  2423. result:=trecorddef.create(symtable.getcopy);
  2424. trecorddef(result).isunion:=isunion;
  2425. end;
  2426. function trecorddef.needs_inittable : boolean;
  2427. begin
  2428. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2429. end;
  2430. procedure trecorddef.buildderef;
  2431. var
  2432. oldrecsyms : tsymtable;
  2433. begin
  2434. inherited buildderef;
  2435. oldrecsyms:=aktrecordsymtable;
  2436. aktrecordsymtable:=symtable;
  2437. { now build the definitions }
  2438. tstoredsymtable(symtable).buildderef;
  2439. aktrecordsymtable:=oldrecsyms;
  2440. end;
  2441. procedure trecorddef.deref;
  2442. var
  2443. oldrecsyms : tsymtable;
  2444. begin
  2445. inherited deref;
  2446. oldrecsyms:=aktrecordsymtable;
  2447. aktrecordsymtable:=symtable;
  2448. { now dereference the definitions }
  2449. tstoredsymtable(symtable).deref;
  2450. aktrecordsymtable:=oldrecsyms;
  2451. { assign TGUID? load only from system unit }
  2452. if not(assigned(rec_tguid)) and
  2453. (upper(typename)='TGUID') and
  2454. assigned(owner) and
  2455. assigned(owner.name) and
  2456. (owner.name^='SYSTEM') then
  2457. rec_tguid:=self;
  2458. end;
  2459. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2460. begin
  2461. inherited ppuwrite(ppufile);
  2462. ppufile.putaint(trecordsymtable(symtable).datasize);
  2463. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2464. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2465. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2466. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2467. ppufile.writeentry(ibrecorddef);
  2468. trecordsymtable(symtable).ppuwrite(ppufile);
  2469. end;
  2470. function trecorddef.size:aint;
  2471. begin
  2472. result:=trecordsymtable(symtable).datasize;
  2473. end;
  2474. function trecorddef.alignment:shortint;
  2475. begin
  2476. alignment:=trecordsymtable(symtable).recordalignment;
  2477. end;
  2478. function trecorddef.padalignment:shortint;
  2479. begin
  2480. padalignment := trecordsymtable(symtable).padalignment;
  2481. end;
  2482. procedure trecorddef.write_child_rtti_data(rt:trttitype);
  2483. begin
  2484. FRTTIType:=rt;
  2485. symtable.foreach(@generate_field_rtti,nil);
  2486. end;
  2487. procedure trecorddef.write_rtti_data(rt:trttitype);
  2488. begin
  2489. if is_packed then
  2490. begin
  2491. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  2492. write_rtti_name;
  2493. exit;
  2494. end;
  2495. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkrecord));
  2496. write_rtti_name;
  2497. {$ifdef cpurequiresproperalignment}
  2498. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2499. {$endif cpurequiresproperalignment}
  2500. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(size));
  2501. Count:=0;
  2502. FRTTIType:=rt;
  2503. symtable.foreach(@count_field_rtti,nil);
  2504. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(Count));
  2505. symtable.foreach(@write_field_rtti,nil);
  2506. end;
  2507. function trecorddef.gettypename : string;
  2508. begin
  2509. gettypename:='<record type>'
  2510. end;
  2511. {***************************************************************************
  2512. TABSTRACTPROCDEF
  2513. ***************************************************************************}
  2514. constructor tabstractprocdef.create(dt:tdeftype;level:byte);
  2515. begin
  2516. inherited create(dt);
  2517. parast:=tparasymtable.create(level);
  2518. parast.defowner:=self;
  2519. paras:=nil;
  2520. minparacount:=0;
  2521. maxparacount:=0;
  2522. proctypeoption:=potype_none;
  2523. proccalloption:=pocall_none;
  2524. procoptions:=[];
  2525. rettype:=voidtype;
  2526. {$ifdef i386}
  2527. fpu_used:=0;
  2528. {$endif i386}
  2529. savesize:=sizeof(aint);
  2530. requiredargarea:=0;
  2531. has_paraloc_info:=false;
  2532. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2533. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2534. end;
  2535. destructor tabstractprocdef.destroy;
  2536. begin
  2537. if assigned(paras) then
  2538. begin
  2539. {$ifdef MEMDEBUG}
  2540. memprocpara.start;
  2541. {$endif MEMDEBUG}
  2542. paras.free;
  2543. {$ifdef MEMDEBUG}
  2544. memprocpara.stop;
  2545. {$endif MEMDEBUG}
  2546. end;
  2547. if assigned(parast) then
  2548. begin
  2549. {$ifdef MEMDEBUG}
  2550. memprocparast.start;
  2551. {$endif MEMDEBUG}
  2552. parast.free;
  2553. {$ifdef MEMDEBUG}
  2554. memprocparast.stop;
  2555. {$endif MEMDEBUG}
  2556. end;
  2557. inherited destroy;
  2558. end;
  2559. procedure tabstractprocdef.releasemem;
  2560. begin
  2561. if assigned(paras) then
  2562. begin
  2563. paras.free;
  2564. paras:=nil;
  2565. end;
  2566. parast.free;
  2567. parast:=nil;
  2568. end;
  2569. procedure tabstractprocdef.count_para(p:tnamedindexitem;arg:pointer);
  2570. begin
  2571. if (tsym(p).typ<>paravarsym) then
  2572. exit;
  2573. inc(plongint(arg)^);
  2574. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2575. begin
  2576. if not assigned(tparavarsym(p).defaultconstsym) then
  2577. inc(minparacount);
  2578. inc(maxparacount);
  2579. end;
  2580. end;
  2581. procedure tabstractprocdef.insert_para(p:tnamedindexitem;arg:pointer);
  2582. begin
  2583. if (tsym(p).typ<>paravarsym) then
  2584. exit;
  2585. paras.add(p);
  2586. end;
  2587. procedure tabstractprocdef.calcparas;
  2588. var
  2589. paracount : longint;
  2590. begin
  2591. { This can already be assigned when
  2592. we need to reresolve this unit (PFV) }
  2593. if assigned(paras) then
  2594. paras.free;
  2595. paras:=tparalist.create(false);
  2596. paracount:=0;
  2597. minparacount:=0;
  2598. maxparacount:=0;
  2599. parast.foreach(@count_para,@paracount);
  2600. paras.capacity:=paracount;
  2601. { Insert parameters in table }
  2602. parast.foreach(@insert_para,nil);
  2603. { Order parameters }
  2604. paras.sortparas;
  2605. end;
  2606. { all functions returning in FPU are
  2607. assume to use 2 FPU registers
  2608. until the function implementation
  2609. is processed PM }
  2610. procedure tabstractprocdef.test_if_fpu_result;
  2611. begin
  2612. {$ifdef i386}
  2613. if assigned(rettype.def) and
  2614. (rettype.def.deftype=floatdef) then
  2615. fpu_used:=maxfpuregs;
  2616. {$endif i386}
  2617. end;
  2618. procedure tabstractprocdef.buildderef;
  2619. begin
  2620. { released procdef? }
  2621. if not assigned(parast) then
  2622. exit;
  2623. inherited buildderef;
  2624. rettype.buildderef;
  2625. { parast }
  2626. tparasymtable(parast).buildderef;
  2627. end;
  2628. procedure tabstractprocdef.deref;
  2629. begin
  2630. inherited deref;
  2631. rettype.resolve;
  2632. { parast }
  2633. tparasymtable(parast).deref;
  2634. { recalculated parameters }
  2635. calcparas;
  2636. end;
  2637. constructor tabstractprocdef.ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  2638. var
  2639. b : byte;
  2640. begin
  2641. inherited ppuload(dt,ppufile);
  2642. parast:=nil;
  2643. Paras:=nil;
  2644. minparacount:=0;
  2645. maxparacount:=0;
  2646. ppufile.gettype(rettype);
  2647. {$ifdef i386}
  2648. fpu_used:=ppufile.getbyte;
  2649. {$else}
  2650. ppufile.getbyte;
  2651. {$endif i386}
  2652. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2653. proccalloption:=tproccalloption(ppufile.getbyte);
  2654. ppufile.getnormalset(procoptions);
  2655. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2656. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2657. if po_explicitparaloc in procoptions then
  2658. begin
  2659. b:=ppufile.getbyte;
  2660. if b<>sizeof(funcretloc[callerside]) then
  2661. internalerror(200411154);
  2662. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2663. end;
  2664. savesize:=sizeof(aint);
  2665. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2666. end;
  2667. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2668. var
  2669. oldintfcrc : boolean;
  2670. begin
  2671. { released procdef? }
  2672. if not assigned(parast) then
  2673. exit;
  2674. inherited ppuwrite(ppufile);
  2675. ppufile.puttype(rettype);
  2676. oldintfcrc:=ppufile.do_interface_crc;
  2677. ppufile.do_interface_crc:=false;
  2678. {$ifdef i386}
  2679. if simplify_ppu then
  2680. fpu_used:=0;
  2681. ppufile.putbyte(fpu_used);
  2682. {$else}
  2683. ppufile.putbyte(0);
  2684. {$endif}
  2685. ppufile.putbyte(ord(proctypeoption));
  2686. ppufile.putbyte(ord(proccalloption));
  2687. ppufile.putnormalset(procoptions);
  2688. ppufile.do_interface_crc:=oldintfcrc;
  2689. if (po_explicitparaloc in procoptions) then
  2690. begin
  2691. { Make a 'valid' funcretloc for procedures }
  2692. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2693. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2694. end;
  2695. end;
  2696. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2697. var
  2698. hs,s : string;
  2699. hp : TParavarsym;
  2700. hpc : tconstsym;
  2701. first : boolean;
  2702. i : integer;
  2703. begin
  2704. s:='';
  2705. first:=true;
  2706. for i:=0 to paras.count-1 do
  2707. begin
  2708. hp:=tparavarsym(paras[i]);
  2709. if not(vo_is_hidden_para in hp.varoptions) or
  2710. (showhidden) then
  2711. begin
  2712. if first then
  2713. begin
  2714. s:=s+'(';
  2715. first:=false;
  2716. end
  2717. else
  2718. s:=s+',';
  2719. case hp.varspez of
  2720. vs_var :
  2721. s:=s+'var';
  2722. vs_const :
  2723. s:=s+'const';
  2724. vs_out :
  2725. s:=s+'out';
  2726. end;
  2727. if assigned(hp.vartype.def.typesym) then
  2728. begin
  2729. if s<>'(' then
  2730. s:=s+' ';
  2731. hs:=hp.vartype.def.typesym.realname;
  2732. if hs[1]<>'$' then
  2733. s:=s+hp.vartype.def.typesym.realname
  2734. else
  2735. s:=s+hp.vartype.def.gettypename;
  2736. end
  2737. else
  2738. s:=s+hp.vartype.def.gettypename;
  2739. { default value }
  2740. if assigned(hp.defaultconstsym) then
  2741. begin
  2742. hpc:=tconstsym(hp.defaultconstsym);
  2743. hs:='';
  2744. case hpc.consttyp of
  2745. conststring,
  2746. constresourcestring :
  2747. hs:=strpas(pchar(hpc.value.valueptr));
  2748. constreal :
  2749. str(pbestreal(hpc.value.valueptr)^,hs);
  2750. constpointer :
  2751. hs:=tostr(hpc.value.valueordptr);
  2752. constord :
  2753. begin
  2754. if is_boolean(hpc.consttype.def) then
  2755. begin
  2756. if hpc.value.valueord<>0 then
  2757. hs:='TRUE'
  2758. else
  2759. hs:='FALSE';
  2760. end
  2761. else
  2762. hs:=tostr(hpc.value.valueord);
  2763. end;
  2764. constnil :
  2765. hs:='nil';
  2766. constset :
  2767. hs:='<set>';
  2768. end;
  2769. if hs<>'' then
  2770. s:=s+'="'+hs+'"';
  2771. end;
  2772. end;
  2773. end;
  2774. if not first then
  2775. s:=s+')';
  2776. if (po_varargs in procoptions) then
  2777. s:=s+';VarArgs';
  2778. typename_paras:=s;
  2779. end;
  2780. function tabstractprocdef.is_methodpointer:boolean;
  2781. begin
  2782. result:=false;
  2783. end;
  2784. function tabstractprocdef.is_addressonly:boolean;
  2785. begin
  2786. result:=true;
  2787. end;
  2788. {***************************************************************************
  2789. TPROCDEF
  2790. ***************************************************************************}
  2791. constructor tprocdef.create(level:byte);
  2792. begin
  2793. inherited create(procdef,level);
  2794. _mangledname:=nil;
  2795. fileinfo:=aktfilepos;
  2796. extnumber:=$ffff;
  2797. aliasnames:=tstringlist.create;
  2798. funcretsym:=nil;
  2799. localst := nil;
  2800. defref:=nil;
  2801. lastwritten:=nil;
  2802. refcount:=0;
  2803. if (cs_browser in aktmoduleswitches) and make_ref then
  2804. begin
  2805. defref:=tref.create(defref,@akttokenpos);
  2806. inc(refcount);
  2807. end;
  2808. lastref:=defref;
  2809. forwarddef:=true;
  2810. interfacedef:=false;
  2811. hasforward:=false;
  2812. _class := nil;
  2813. import_dll:=nil;
  2814. import_name:=nil;
  2815. import_nr:=0;
  2816. inlininginfo:=nil;
  2817. end;
  2818. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2819. var
  2820. level : byte;
  2821. begin
  2822. inherited ppuload(procdef,ppufile);
  2823. if po_has_mangledname in procoptions then
  2824. _mangledname:=stringdup(ppufile.getstring)
  2825. else
  2826. _mangledname:=nil;
  2827. extnumber:=ppufile.getword;
  2828. level:=ppufile.getbyte;
  2829. ppufile.getderef(_classderef);
  2830. ppufile.getderef(procsymderef);
  2831. ppufile.getposinfo(fileinfo);
  2832. ppufile.getsmallset(symoptions);
  2833. {$ifdef powerpc}
  2834. { library symbol for AmigaOS/MorphOS }
  2835. ppufile.getderef(libsymderef);
  2836. {$endif powerpc}
  2837. { import stuff }
  2838. if po_has_importdll in procoptions then
  2839. import_dll:=stringdup(ppufile.getstring)
  2840. else
  2841. import_dll:=nil;
  2842. if po_has_importname in procoptions then
  2843. import_name:=stringdup(ppufile.getstring)
  2844. else
  2845. import_name:=nil;
  2846. import_nr:=ppufile.getword;
  2847. if (po_msgint in procoptions) then
  2848. messageinf.i:=ppufile.getlongint;
  2849. if (po_msgstr in procoptions) then
  2850. messageinf.str:=stringdup(ppufile.getstring);
  2851. { inline stuff }
  2852. if (po_has_inlininginfo in procoptions) then
  2853. begin
  2854. ppufile.getderef(funcretsymderef);
  2855. new(inlininginfo);
  2856. ppufile.getsmallset(inlininginfo^.flags);
  2857. end
  2858. else
  2859. begin
  2860. inlininginfo:=nil;
  2861. funcretsym:=nil;
  2862. end;
  2863. { load para symtable }
  2864. parast:=tparasymtable.create(level);
  2865. tparasymtable(parast).ppuload(ppufile);
  2866. parast.defowner:=self;
  2867. { load local symtable }
  2868. if (po_has_inlininginfo in procoptions) or
  2869. ((current_module.flags and uf_local_browser)<>0) then
  2870. begin
  2871. localst:=tlocalsymtable.create(level);
  2872. tlocalsymtable(localst).ppuload(ppufile);
  2873. localst.defowner:=self;
  2874. end
  2875. else
  2876. localst:=nil;
  2877. { inline stuff }
  2878. if (po_has_inlininginfo in procoptions) then
  2879. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2880. { default values for no persistent data }
  2881. if (cs_link_deffile in aktglobalswitches) and
  2882. (tf_need_export in target_info.flags) and
  2883. (po_exports in procoptions) then
  2884. deffile.AddExport(mangledname);
  2885. aliasnames:=tstringlist.create;
  2886. forwarddef:=false;
  2887. interfacedef:=false;
  2888. hasforward:=false;
  2889. lastref:=nil;
  2890. lastwritten:=nil;
  2891. defref:=nil;
  2892. refcount:=0;
  2893. { Disable po_has_inlining until the derefimpl is done }
  2894. exclude(procoptions,po_has_inlininginfo);
  2895. end;
  2896. destructor tprocdef.destroy;
  2897. begin
  2898. if assigned(defref) then
  2899. begin
  2900. defref.freechain;
  2901. defref.free;
  2902. end;
  2903. aliasnames.free;
  2904. if assigned(localst) and (localst.symtabletype<>staticsymtable) then
  2905. begin
  2906. {$ifdef MEMDEBUG}
  2907. memproclocalst.start;
  2908. {$endif MEMDEBUG}
  2909. localst.free;
  2910. {$ifdef MEMDEBUG}
  2911. memproclocalst.start;
  2912. {$endif MEMDEBUG}
  2913. end;
  2914. if assigned(inlininginfo) then
  2915. begin
  2916. {$ifdef MEMDEBUG}
  2917. memprocnodetree.start;
  2918. {$endif MEMDEBUG}
  2919. tnode(inlininginfo^.code).free;
  2920. {$ifdef MEMDEBUG}
  2921. memprocnodetree.start;
  2922. {$endif MEMDEBUG}
  2923. dispose(inlininginfo);
  2924. end;
  2925. stringdispose(import_dll);
  2926. stringdispose(import_name);
  2927. if (po_msgstr in procoptions) then
  2928. stringdispose(messageinf.str);
  2929. if assigned(_mangledname) then
  2930. begin
  2931. {$ifdef MEMDEBUG}
  2932. memmanglednames.start;
  2933. {$endif MEMDEBUG}
  2934. stringdispose(_mangledname);
  2935. {$ifdef MEMDEBUG}
  2936. memmanglednames.stop;
  2937. {$endif MEMDEBUG}
  2938. end;
  2939. inherited destroy;
  2940. end;
  2941. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2942. var
  2943. oldintfcrc : boolean;
  2944. oldparasymtable,
  2945. oldlocalsymtable : tsymtable;
  2946. begin
  2947. { released procdef? }
  2948. if not assigned(parast) then
  2949. exit;
  2950. oldparasymtable:=aktparasymtable;
  2951. oldlocalsymtable:=aktlocalsymtable;
  2952. aktparasymtable:=parast;
  2953. aktlocalsymtable:=localst;
  2954. inherited ppuwrite(ppufile);
  2955. oldintfcrc:=ppufile.do_interface_crc;
  2956. ppufile.do_interface_crc:=false;
  2957. ppufile.do_interface_crc:=oldintfcrc;
  2958. if po_has_mangledname in procoptions then
  2959. ppufile.putstring(_mangledname^);
  2960. ppufile.putword(extnumber);
  2961. ppufile.putbyte(parast.symtablelevel);
  2962. ppufile.putderef(_classderef);
  2963. ppufile.putderef(procsymderef);
  2964. ppufile.putposinfo(fileinfo);
  2965. ppufile.putsmallset(symoptions);
  2966. {$ifdef powerpc}
  2967. { library symbol for AmigaOS/MorphOS }
  2968. ppufile.putderef(libsymderef);
  2969. {$endif powerpc}
  2970. { import }
  2971. if po_has_importdll in procoptions then
  2972. ppufile.putstring(import_dll^);
  2973. if po_has_importname in procoptions then
  2974. ppufile.putstring(import_name^);
  2975. ppufile.putword(import_nr);
  2976. if (po_msgint in procoptions) then
  2977. ppufile.putlongint(messageinf.i);
  2978. if (po_msgstr in procoptions) then
  2979. ppufile.putstring(messageinf.str^);
  2980. { inline stuff }
  2981. oldintfcrc:=ppufile.do_crc;
  2982. ppufile.do_crc:=false;
  2983. if (po_has_inlininginfo in procoptions) then
  2984. begin
  2985. ppufile.putderef(funcretsymderef);
  2986. ppufile.putsmallset(inlininginfo^.flags);
  2987. end;
  2988. ppufile.do_crc:=oldintfcrc;
  2989. { write this entry }
  2990. ppufile.writeentry(ibprocdef);
  2991. { Save the para symtable, this is taken from the interface }
  2992. tparasymtable(parast).ppuwrite(ppufile);
  2993. { save localsymtable for inline procedures or when local
  2994. browser info is requested, this has no influence on the crc }
  2995. if (po_has_inlininginfo in procoptions) or
  2996. ((current_module.flags and uf_local_browser)<>0) then
  2997. begin
  2998. { we must write a localsymtable }
  2999. if not assigned(localst) then
  3000. insert_localst;
  3001. oldintfcrc:=ppufile.do_crc;
  3002. ppufile.do_crc:=false;
  3003. tlocalsymtable(localst).ppuwrite(ppufile);
  3004. ppufile.do_crc:=oldintfcrc;
  3005. end;
  3006. { node tree for inlining }
  3007. oldintfcrc:=ppufile.do_crc;
  3008. ppufile.do_crc:=false;
  3009. if (po_has_inlininginfo in procoptions) then
  3010. ppuwritenodetree(ppufile,inlininginfo^.code);
  3011. ppufile.do_crc:=oldintfcrc;
  3012. aktparasymtable:=oldparasymtable;
  3013. aktlocalsymtable:=oldlocalsymtable;
  3014. end;
  3015. procedure tprocdef.reset;
  3016. begin
  3017. inherited reset;
  3018. procstarttai:=nil;
  3019. procendtai:=nil;
  3020. end;
  3021. procedure tprocdef.insert_localst;
  3022. begin
  3023. localst:=tlocalsymtable.create(parast.symtablelevel);
  3024. localst.defowner:=self;
  3025. end;
  3026. function tprocdef.fullprocname(showhidden:boolean):string;
  3027. var
  3028. s : string;
  3029. t : ttoken;
  3030. begin
  3031. {$ifdef EXTDEBUG}
  3032. showhidden:=true;
  3033. {$endif EXTDEBUG}
  3034. s:='';
  3035. if owner.symtabletype=localsymtable then
  3036. s:=s+'local ';
  3037. if assigned(_class) then
  3038. begin
  3039. if po_classmethod in procoptions then
  3040. s:=s+'class ';
  3041. s:=s+_class.objrealname^+'.';
  3042. end;
  3043. if proctypeoption=potype_operator then
  3044. begin
  3045. for t:=NOTOKEN to last_overloaded do
  3046. if procsym.realname='$'+overloaded_names[t] then
  3047. begin
  3048. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  3049. break;
  3050. end;
  3051. end
  3052. else
  3053. s:=s+procsym.realname+typename_paras(showhidden);
  3054. case proctypeoption of
  3055. potype_constructor:
  3056. s:='constructor '+s;
  3057. potype_destructor:
  3058. s:='destructor '+s;
  3059. else
  3060. if assigned(rettype.def) and
  3061. not(is_void(rettype.def)) then
  3062. s:=s+':'+rettype.def.gettypename;
  3063. end;
  3064. { forced calling convention? }
  3065. if (po_hascallingconvention in procoptions) then
  3066. s:=s+';'+ProcCallOptionStr[proccalloption];
  3067. fullprocname:=s;
  3068. end;
  3069. function tprocdef.is_methodpointer:boolean;
  3070. begin
  3071. result:=assigned(_class);
  3072. end;
  3073. function tprocdef.is_addressonly:boolean;
  3074. begin
  3075. result:=assigned(owner) and
  3076. (owner.symtabletype<>objectsymtable);
  3077. end;
  3078. function tprocdef.is_visible_for_object(currobjdef,contextobjdef:tobjectdef):boolean;
  3079. var
  3080. contextst : tsymtable;
  3081. begin
  3082. result:=false;
  3083. { Support passing a context in which module we are to find protected members }
  3084. if assigned(contextobjdef) then
  3085. contextst:=contextobjdef.owner
  3086. else
  3087. contextst:=nil;
  3088. { private symbols are allowed when we are in the same
  3089. module as they are defined }
  3090. if (sp_private in symoptions) and
  3091. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  3092. not(owner.defowner.owner.iscurrentunit or (owner.defowner.owner=contextst)) then
  3093. exit;
  3094. if (sp_strictprivate in symoptions) then
  3095. begin
  3096. result:=currobjdef=tobjectdef(owner.defowner);
  3097. exit;
  3098. end;
  3099. if (sp_strictprotected in symoptions) then
  3100. begin
  3101. result:=assigned(currobjdef) and
  3102. currobjdef.is_related(tobjectdef(owner.defowner));
  3103. exit;
  3104. end;
  3105. { protected symbols are visible in the module that defines them and
  3106. also visible to related objects. The related object must be defined
  3107. in the current module }
  3108. if (sp_protected in symoptions) and
  3109. (
  3110. (
  3111. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  3112. not((owner.defowner.owner.iscurrentunit) or (owner.defowner.owner=contextst))
  3113. ) and
  3114. not(
  3115. assigned(currobjdef) and
  3116. (currobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  3117. (currobjdef.owner.iscurrentunit) and
  3118. currobjdef.is_related(tobjectdef(owner.defowner))
  3119. )
  3120. ) then
  3121. exit;
  3122. result:=true;
  3123. end;
  3124. function tprocdef.getsymtable(t:tgetsymtable):tsymtable;
  3125. begin
  3126. case t of
  3127. gs_local :
  3128. getsymtable:=localst;
  3129. gs_para :
  3130. getsymtable:=parast;
  3131. else
  3132. getsymtable:=nil;
  3133. end;
  3134. end;
  3135. procedure tprocdef.load_references(ppufile:tcompilerppufile;locals:boolean);
  3136. var
  3137. pos : tfileposinfo;
  3138. move_last : boolean;
  3139. oldparasymtable,
  3140. oldlocalsymtable : tsymtable;
  3141. begin
  3142. oldparasymtable:=aktparasymtable;
  3143. oldlocalsymtable:=aktlocalsymtable;
  3144. aktparasymtable:=parast;
  3145. aktlocalsymtable:=localst;
  3146. move_last:=lastwritten=lastref;
  3147. while (not ppufile.endofentry) do
  3148. begin
  3149. ppufile.getposinfo(pos);
  3150. inc(refcount);
  3151. lastref:=tref.create(lastref,@pos);
  3152. lastref.is_written:=true;
  3153. if refcount=1 then
  3154. defref:=lastref;
  3155. end;
  3156. if move_last then
  3157. lastwritten:=lastref;
  3158. if ((current_module.flags and uf_local_browser)<>0) and
  3159. assigned(localst) and
  3160. locals then
  3161. begin
  3162. tparasymtable(parast).load_references(ppufile,locals);
  3163. tlocalsymtable(localst).load_references(ppufile,locals);
  3164. end;
  3165. aktparasymtable:=oldparasymtable;
  3166. aktlocalsymtable:=oldlocalsymtable;
  3167. end;
  3168. Const
  3169. local_symtable_index : word = $8001;
  3170. function tprocdef.write_references(ppufile:tcompilerppufile;locals:boolean):boolean;
  3171. var
  3172. ref : tref;
  3173. {$ifdef supportbrowser}
  3174. pdo : tobjectdef;
  3175. {$endif supportbrowser}
  3176. move_last : boolean;
  3177. d : tderef;
  3178. oldparasymtable,
  3179. oldlocalsymtable : tsymtable;
  3180. begin
  3181. d.reset;
  3182. move_last:=lastwritten=lastref;
  3183. if move_last and
  3184. (((current_module.flags and uf_local_browser)=0) or
  3185. not locals) then
  3186. exit;
  3187. oldparasymtable:=aktparasymtable;
  3188. oldlocalsymtable:=aktlocalsymtable;
  3189. aktparasymtable:=parast;
  3190. aktlocalsymtable:=localst;
  3191. { write address of this symbol }
  3192. d.build(self);
  3193. ppufile.putderef(d);
  3194. { write refs }
  3195. if assigned(lastwritten) then
  3196. ref:=lastwritten
  3197. else
  3198. ref:=defref;
  3199. while assigned(ref) do
  3200. begin
  3201. if ref.moduleindex=current_module.unit_index then
  3202. begin
  3203. ppufile.putposinfo(ref.posinfo);
  3204. ref.is_written:=true;
  3205. if move_last then
  3206. lastwritten:=ref;
  3207. end
  3208. else if not ref.is_written then
  3209. move_last:=false
  3210. else if move_last then
  3211. lastwritten:=ref;
  3212. ref:=ref.nextref;
  3213. end;
  3214. ppufile.writeentry(ibdefref);
  3215. write_references:=true;
  3216. {$ifdef supportbrowser}
  3217. if ((current_module.flags and uf_local_browser)<>0) and
  3218. assigned(localst) and
  3219. locals then
  3220. begin
  3221. pdo:=_class;
  3222. if (owner.symtabletype<>localsymtable) then
  3223. while assigned(pdo) do
  3224. begin
  3225. if pdo.symtable<>aktrecordsymtable then
  3226. begin
  3227. pdo.symtable.moduleid:=local_symtable_index;
  3228. inc(local_symtable_index);
  3229. end;
  3230. pdo:=pdo.childof;
  3231. end;
  3232. parast.moduleid:=local_symtable_index;
  3233. inc(local_symtable_index);
  3234. localst.moduleid:=local_symtable_index;
  3235. inc(local_symtable_index);
  3236. tstoredsymtable(parast).write_references(ppufile,locals);
  3237. tstoredsymtable(localst).write_references(ppufile,locals);
  3238. { decrement for }
  3239. local_symtable_index:=local_symtable_index-2;
  3240. pdo:=_class;
  3241. if (owner.symtabletype<>localsymtable) then
  3242. while assigned(pdo) do
  3243. begin
  3244. if pdo.symtable<>aktrecordsymtable then
  3245. dec(local_symtable_index);
  3246. pdo:=pdo.childof;
  3247. end;
  3248. end;
  3249. {$endif supportbrowser}
  3250. aktparasymtable:=oldparasymtable;
  3251. aktlocalsymtable:=oldlocalsymtable;
  3252. end;
  3253. procedure tprocdef.buildderef;
  3254. var
  3255. oldparasymtable,
  3256. oldlocalsymtable : tsymtable;
  3257. begin
  3258. oldparasymtable:=aktparasymtable;
  3259. oldlocalsymtable:=aktlocalsymtable;
  3260. aktparasymtable:=parast;
  3261. aktlocalsymtable:=localst;
  3262. inherited buildderef;
  3263. _classderef.build(_class);
  3264. { procsym that originaly defined this definition, should be in the
  3265. same symtable }
  3266. procsymderef.build(procsym);
  3267. {$ifdef powerpc}
  3268. { library symbol for AmigaOS/MorphOS }
  3269. libsymderef.build(libsym);
  3270. {$endif powerpc}
  3271. aktparasymtable:=oldparasymtable;
  3272. aktlocalsymtable:=oldlocalsymtable;
  3273. end;
  3274. procedure tprocdef.buildderefimpl;
  3275. var
  3276. oldparasymtable,
  3277. oldlocalsymtable : tsymtable;
  3278. begin
  3279. { released procdef? }
  3280. if not assigned(parast) then
  3281. exit;
  3282. oldparasymtable:=aktparasymtable;
  3283. oldlocalsymtable:=aktlocalsymtable;
  3284. aktparasymtable:=parast;
  3285. aktlocalsymtable:=localst;
  3286. inherited buildderefimpl;
  3287. { Locals, always build deref info it might be needed
  3288. if the unit needs to be reloaded }
  3289. if assigned(localst) then
  3290. begin
  3291. tlocalsymtable(localst).buildderef;
  3292. tlocalsymtable(localst).buildderefimpl;
  3293. end;
  3294. { inline tree }
  3295. if (po_has_inlininginfo in procoptions) then
  3296. begin
  3297. funcretsymderef.build(funcretsym);
  3298. inlininginfo^.code.buildderefimpl;
  3299. end;
  3300. aktparasymtable:=oldparasymtable;
  3301. aktlocalsymtable:=oldlocalsymtable;
  3302. end;
  3303. procedure tprocdef.deref;
  3304. var
  3305. oldparasymtable,
  3306. oldlocalsymtable : tsymtable;
  3307. begin
  3308. { released procdef? }
  3309. if not assigned(parast) then
  3310. exit;
  3311. oldparasymtable:=aktparasymtable;
  3312. oldlocalsymtable:=aktlocalsymtable;
  3313. aktparasymtable:=parast;
  3314. aktlocalsymtable:=localst;
  3315. inherited deref;
  3316. _class:=tobjectdef(_classderef.resolve);
  3317. { procsym that originaly defined this definition, should be in the
  3318. same symtable }
  3319. procsym:=tprocsym(procsymderef.resolve);
  3320. {$ifdef powerpc}
  3321. { library symbol for AmigaOS/MorphOS }
  3322. libsym:=tsym(libsymderef.resolve);
  3323. {$endif powerpc}
  3324. aktparasymtable:=oldparasymtable;
  3325. aktlocalsymtable:=oldlocalsymtable;
  3326. end;
  3327. procedure tprocdef.derefimpl;
  3328. var
  3329. oldparasymtable,
  3330. oldlocalsymtable : tsymtable;
  3331. begin
  3332. oldparasymtable:=aktparasymtable;
  3333. oldlocalsymtable:=aktlocalsymtable;
  3334. aktparasymtable:=parast;
  3335. aktlocalsymtable:=localst;
  3336. { Enable has_inlininginfo when the inlininginfo
  3337. structure is available. The has_inlininginfo was disabled
  3338. after the load, since the data was invalid }
  3339. if assigned(inlininginfo) then
  3340. include(procoptions,po_has_inlininginfo);
  3341. { Locals }
  3342. if assigned(localst) then
  3343. begin
  3344. tlocalsymtable(localst).deref;
  3345. tlocalsymtable(localst).derefimpl;
  3346. end;
  3347. { Inline }
  3348. if (po_has_inlininginfo in procoptions) then
  3349. begin
  3350. inlininginfo^.code.derefimpl;
  3351. { funcretsym, this is always located in the localst }
  3352. funcretsym:=tsym(funcretsymderef.resolve);
  3353. end
  3354. else
  3355. begin
  3356. { safety }
  3357. funcretsym:=nil;
  3358. end;
  3359. aktparasymtable:=oldparasymtable;
  3360. aktlocalsymtable:=oldlocalsymtable;
  3361. end;
  3362. function tprocdef.gettypename : string;
  3363. begin
  3364. gettypename := FullProcName(false);
  3365. end;
  3366. function tprocdef.mangledname : string;
  3367. var
  3368. hp : TParavarsym;
  3369. hs : string;
  3370. crc : dword;
  3371. newlen,
  3372. oldlen,
  3373. i : integer;
  3374. begin
  3375. if assigned(_mangledname) then
  3376. begin
  3377. {$ifdef compress}
  3378. mangledname:=minilzw_decode(_mangledname^);
  3379. {$else}
  3380. mangledname:=_mangledname^;
  3381. {$endif}
  3382. exit;
  3383. end;
  3384. { we need to use the symtable where the procsym is inserted,
  3385. because that is visible to the world }
  3386. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3387. oldlen:=length(mangledname);
  3388. { add parameter types }
  3389. for i:=0 to paras.count-1 do
  3390. begin
  3391. hp:=tparavarsym(paras[i]);
  3392. if not(vo_is_hidden_para in hp.varoptions) then
  3393. mangledname:=mangledname+'$'+hp.vartype.def.mangledparaname;
  3394. end;
  3395. { add resulttype, add $$ as separator to make it unique from a
  3396. parameter separator }
  3397. if not is_void(rettype.def) then
  3398. mangledname:=mangledname+'$$'+rettype.def.mangledparaname;
  3399. newlen:=length(mangledname);
  3400. { Replace with CRC if the parameter line is very long }
  3401. if (newlen-oldlen>12) and
  3402. ((newlen>128) or (newlen-oldlen>64)) then
  3403. begin
  3404. crc:=$ffffffff;
  3405. for i:=0 to paras.count-1 do
  3406. begin
  3407. hp:=tparavarsym(paras[i]);
  3408. if not(vo_is_hidden_para in hp.varoptions) then
  3409. begin
  3410. hs:=hp.vartype.def.mangledparaname;
  3411. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3412. end;
  3413. end;
  3414. hs:=hp.vartype.def.mangledparaname;
  3415. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3416. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3417. end;
  3418. {$ifdef compress}
  3419. _mangledname:=stringdup(minilzw_encode(mangledname));
  3420. {$else}
  3421. _mangledname:=stringdup(mangledname);
  3422. {$endif}
  3423. end;
  3424. function tprocdef.cplusplusmangledname : string;
  3425. function getcppparaname(p : tdef) : string;
  3426. const
  3427. ordtype2str : array[tbasetype] of string[2] = (
  3428. '',
  3429. 'Uc','Us','Ui','Us',
  3430. 'Sc','s','i','x',
  3431. 'b','b','b','b',
  3432. 'c','w','x');
  3433. var
  3434. s : string;
  3435. begin
  3436. case p.deftype of
  3437. orddef:
  3438. s:=ordtype2str[torddef(p).typ];
  3439. pointerdef:
  3440. s:='P'+getcppparaname(tpointerdef(p).pointertype.def);
  3441. else
  3442. internalerror(2103001);
  3443. end;
  3444. getcppparaname:=s;
  3445. end;
  3446. var
  3447. s,s2 : string;
  3448. hp : TParavarsym;
  3449. i : integer;
  3450. begin
  3451. s := procsym.realname;
  3452. if procsym.owner.symtabletype=objectsymtable then
  3453. begin
  3454. s2:=upper(tobjectdef(procsym.owner.defowner).typesym.realname);
  3455. case proctypeoption of
  3456. potype_destructor:
  3457. s:='_$_'+tostr(length(s2))+s2;
  3458. potype_constructor:
  3459. s:='___'+tostr(length(s2))+s2;
  3460. else
  3461. s:='_'+s+'__'+tostr(length(s2))+s2;
  3462. end;
  3463. end
  3464. else s:=s+'__';
  3465. s:=s+'F';
  3466. { concat modifiers }
  3467. { !!!!! }
  3468. { now we handle the parameters }
  3469. if maxparacount>0 then
  3470. begin
  3471. for i:=0 to paras.count-1 do
  3472. begin
  3473. hp:=tparavarsym(paras[i]);
  3474. s2:=getcppparaname(hp.vartype.def);
  3475. if hp.varspez in [vs_var,vs_out] then
  3476. s2:='R'+s2;
  3477. s:=s+s2;
  3478. end;
  3479. end
  3480. else
  3481. s:=s+'v';
  3482. cplusplusmangledname:=s;
  3483. end;
  3484. procedure tprocdef.setmangledname(const s : string);
  3485. begin
  3486. { This is not allowed anymore, the forward declaration
  3487. already needs to create the correct mangledname, no changes
  3488. afterwards are allowed (PFV) }
  3489. { Exception: interface definitions in mode macpas, since in that }
  3490. { case no reference to the old name can exist yet (JM) }
  3491. if assigned(_mangledname) then
  3492. if ((m_mac in aktmodeswitches) and
  3493. (interfacedef)) then
  3494. stringdispose(_mangledname)
  3495. else
  3496. internalerror(200411171);
  3497. {$ifdef compress}
  3498. _mangledname:=stringdup(minilzw_encode(s));
  3499. {$else}
  3500. _mangledname:=stringdup(s);
  3501. {$endif}
  3502. include(procoptions,po_has_mangledname);
  3503. end;
  3504. {***************************************************************************
  3505. TPROCVARDEF
  3506. ***************************************************************************}
  3507. constructor tprocvardef.create(level:byte);
  3508. begin
  3509. inherited create(procvardef,level);
  3510. end;
  3511. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3512. begin
  3513. inherited ppuload(procvardef,ppufile);
  3514. { load para symtable }
  3515. parast:=tparasymtable.create(unknown_level);
  3516. tparasymtable(parast).ppuload(ppufile);
  3517. parast.defowner:=self;
  3518. end;
  3519. function tprocvardef.getcopy : tstoreddef;
  3520. begin
  3521. result:=self;
  3522. (*
  3523. { saves a definition to the return type }
  3524. rettype : ttype;
  3525. parast : tsymtable;
  3526. paras : tparalist;
  3527. proctypeoption : tproctypeoption;
  3528. proccalloption : tproccalloption;
  3529. procoptions : tprocoptions;
  3530. requiredargarea : aint;
  3531. { number of user visibile parameters }
  3532. maxparacount,
  3533. minparacount : byte;
  3534. {$ifdef i386}
  3535. fpu_used : longint; { how many stack fpu must be empty }
  3536. {$endif i386}
  3537. funcretloc : array[tcallercallee] of TLocation;
  3538. has_paraloc_info : boolean; { paraloc info is available }
  3539. tprocvardef = class(tabstractprocdef)
  3540. constructor create(level:byte);
  3541. constructor ppuload(ppufile:tcompilerppufile);
  3542. function getcopy : tstoreddef;override;
  3543. *)
  3544. end;
  3545. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3546. var
  3547. oldparasymtable,
  3548. oldlocalsymtable : tsymtable;
  3549. begin
  3550. oldparasymtable:=aktparasymtable;
  3551. oldlocalsymtable:=aktlocalsymtable;
  3552. aktparasymtable:=parast;
  3553. aktlocalsymtable:=nil;
  3554. { here we cannot get a real good value so just give something }
  3555. { plausible (PM) }
  3556. { a more secure way would be
  3557. to allways store in a temp }
  3558. {$ifdef i386}
  3559. if is_fpu(rettype.def) then
  3560. fpu_used:={2}maxfpuregs
  3561. else
  3562. fpu_used:=0;
  3563. {$endif i386}
  3564. inherited ppuwrite(ppufile);
  3565. { Write this entry }
  3566. ppufile.writeentry(ibprocvardef);
  3567. { Save the para symtable, this is taken from the interface }
  3568. tparasymtable(parast).ppuwrite(ppufile);
  3569. aktparasymtable:=oldparasymtable;
  3570. aktlocalsymtable:=oldlocalsymtable;
  3571. end;
  3572. procedure tprocvardef.buildderef;
  3573. var
  3574. oldparasymtable,
  3575. oldlocalsymtable : tsymtable;
  3576. begin
  3577. oldparasymtable:=aktparasymtable;
  3578. oldlocalsymtable:=aktlocalsymtable;
  3579. aktparasymtable:=parast;
  3580. aktlocalsymtable:=nil;
  3581. inherited buildderef;
  3582. aktparasymtable:=oldparasymtable;
  3583. aktlocalsymtable:=oldlocalsymtable;
  3584. end;
  3585. procedure tprocvardef.deref;
  3586. var
  3587. oldparasymtable,
  3588. oldlocalsymtable : tsymtable;
  3589. begin
  3590. oldparasymtable:=aktparasymtable;
  3591. oldlocalsymtable:=aktlocalsymtable;
  3592. aktparasymtable:=parast;
  3593. aktlocalsymtable:=nil;
  3594. inherited deref;
  3595. aktparasymtable:=oldparasymtable;
  3596. aktlocalsymtable:=oldlocalsymtable;
  3597. end;
  3598. function tprocvardef.getsymtable(t:tgetsymtable):tsymtable;
  3599. begin
  3600. case t of
  3601. gs_para :
  3602. getsymtable:=parast;
  3603. else
  3604. getsymtable:=nil;
  3605. end;
  3606. end;
  3607. function tprocvardef.size : aint;
  3608. begin
  3609. if (po_methodpointer in procoptions) and
  3610. not(po_addressonly in procoptions) then
  3611. size:=2*sizeof(aint)
  3612. else
  3613. size:=sizeof(aint);
  3614. end;
  3615. function tprocvardef.is_methodpointer:boolean;
  3616. begin
  3617. result:=(po_methodpointer in procoptions);
  3618. end;
  3619. function tprocvardef.is_addressonly:boolean;
  3620. begin
  3621. result:=not(po_methodpointer in procoptions) or
  3622. (po_addressonly in procoptions);
  3623. end;
  3624. function tprocvardef.getmangledparaname:string;
  3625. begin
  3626. result:='procvar';
  3627. end;
  3628. procedure tprocvardef.write_rtti_data(rt:trttitype);
  3629. procedure write_para(parasym:tparavarsym);
  3630. var
  3631. paraspec : byte;
  3632. begin
  3633. { only store user visible parameters }
  3634. if not(vo_is_hidden_para in parasym.varoptions) then
  3635. begin
  3636. case parasym.varspez of
  3637. vs_value: paraspec := 0;
  3638. vs_const: paraspec := pfConst;
  3639. vs_var : paraspec := pfVar;
  3640. vs_out : paraspec := pfOut;
  3641. end;
  3642. { write flags for current parameter }
  3643. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  3644. { write name of current parameter }
  3645. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  3646. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  3647. { write name of type of current parameter }
  3648. tstoreddef(parasym.vartype.def).write_rtti_name;
  3649. end;
  3650. end;
  3651. var
  3652. methodkind : byte;
  3653. i : integer;
  3654. begin
  3655. if po_methodpointer in procoptions then
  3656. begin
  3657. { write method id and name }
  3658. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  3659. write_rtti_name;
  3660. {$ifdef cpurequiresproperalignment}
  3661. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  3662. {$endif cpurequiresproperalignment}
  3663. { write kind of method (can only be function or procedure)}
  3664. if rettype.def = voidtype.def then
  3665. methodkind := mkProcedure
  3666. else
  3667. methodkind := mkFunction;
  3668. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  3669. { get # of parameters }
  3670. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(maxparacount));
  3671. { write parameter info. The parameters must be written in reverse order
  3672. if this method uses right to left parameter pushing! }
  3673. if proccalloption in pushleftright_pocalls then
  3674. begin
  3675. for i:=0 to paras.count-1 do
  3676. write_para(tparavarsym(paras[i]));
  3677. end
  3678. else
  3679. begin
  3680. for i:=paras.count-1 downto 0 do
  3681. write_para(tparavarsym(paras[i]));
  3682. end;
  3683. { write name of result type }
  3684. tstoreddef(rettype.def).write_rtti_name;
  3685. end
  3686. else
  3687. begin
  3688. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  3689. write_rtti_name;
  3690. end;
  3691. end;
  3692. function tprocvardef.is_publishable : boolean;
  3693. begin
  3694. is_publishable:=(po_methodpointer in procoptions);
  3695. end;
  3696. function tprocvardef.gettypename : string;
  3697. var
  3698. s: string;
  3699. showhidden : boolean;
  3700. begin
  3701. {$ifdef EXTDEBUG}
  3702. showhidden:=true;
  3703. {$else EXTDEBUG}
  3704. showhidden:=false;
  3705. {$endif EXTDEBUG}
  3706. s:='<';
  3707. if po_classmethod in procoptions then
  3708. s := s+'class method type of'
  3709. else
  3710. if po_addressonly in procoptions then
  3711. s := s+'address of'
  3712. else
  3713. s := s+'procedure variable type of';
  3714. if po_local in procoptions then
  3715. s := s+' local';
  3716. if assigned(rettype.def) and
  3717. (rettype.def<>voidtype.def) then
  3718. s:=s+' function'+typename_paras(showhidden)+':'+rettype.def.gettypename
  3719. else
  3720. s:=s+' procedure'+typename_paras(showhidden);
  3721. if po_methodpointer in procoptions then
  3722. s := s+' of object';
  3723. gettypename := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3724. end;
  3725. {***************************************************************************
  3726. TOBJECTDEF
  3727. ***************************************************************************}
  3728. type
  3729. tproptablelistitem = class(TLinkedListItem)
  3730. index : longint;
  3731. def : tobjectdef;
  3732. end;
  3733. tpropnamelistitem = class(TLinkedListItem)
  3734. index : longint;
  3735. name : stringid;
  3736. owner : tsymtable;
  3737. end;
  3738. var
  3739. proptablelist : tlinkedlist;
  3740. propnamelist : tlinkedlist;
  3741. function searchproptablelist(p : tobjectdef) : tproptablelistitem;
  3742. var
  3743. hp : tproptablelistitem;
  3744. begin
  3745. hp:=tproptablelistitem(proptablelist.first);
  3746. while assigned(hp) do
  3747. if hp.def=p then
  3748. begin
  3749. result:=hp;
  3750. exit;
  3751. end
  3752. else
  3753. hp:=tproptablelistitem(hp.next);
  3754. result:=nil;
  3755. end;
  3756. function searchpropnamelist(const n:string) : tpropnamelistitem;
  3757. var
  3758. hp : tpropnamelistitem;
  3759. begin
  3760. hp:=tpropnamelistitem(propnamelist.first);
  3761. while assigned(hp) do
  3762. if hp.name=n then
  3763. begin
  3764. result:=hp;
  3765. exit;
  3766. end
  3767. else
  3768. hp:=tpropnamelistitem(hp.next);
  3769. result:=nil;
  3770. end;
  3771. constructor tobjectdef.create(ot : tobjectdeftype;const n : string;c : tobjectdef);
  3772. begin
  3773. inherited create(objectdef);
  3774. objecttype:=ot;
  3775. objectoptions:=[];
  3776. childof:=nil;
  3777. symtable:=tobjectsymtable.create(n,aktpackrecords);
  3778. { create space for vmt !! }
  3779. vmt_offset:=0;
  3780. symtable.defowner:=self;
  3781. lastvtableindex:=0;
  3782. set_parent(c);
  3783. objname:=stringdup(upper(n));
  3784. objrealname:=stringdup(n);
  3785. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3786. prepareguid;
  3787. { setup implemented interfaces }
  3788. if objecttype in [odt_class,odt_interfacecorba] then
  3789. implementedinterfaces:=timplementedinterfaces.create
  3790. else
  3791. implementedinterfaces:=nil;
  3792. writing_class_record_dbginfo:=false;
  3793. iitype := etStandard;
  3794. end;
  3795. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3796. var
  3797. i,implintfcount: longint;
  3798. d : tderef;
  3799. begin
  3800. inherited ppuload(objectdef,ppufile);
  3801. objecttype:=tobjectdeftype(ppufile.getbyte);
  3802. objrealname:=stringdup(ppufile.getstring);
  3803. objname:=stringdup(upper(objrealname^));
  3804. symtable:=tobjectsymtable.create(objrealname^,0);
  3805. tobjectsymtable(symtable).datasize:=ppufile.getaint;
  3806. tobjectsymtable(symtable).fieldalignment:=ppufile.getbyte;
  3807. tobjectsymtable(symtable).recordalignment:=ppufile.getbyte;
  3808. vmt_offset:=ppufile.getlongint;
  3809. ppufile.getderef(childofderef);
  3810. ppufile.getsmallset(objectoptions);
  3811. { load guid }
  3812. iidstr:=nil;
  3813. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3814. begin
  3815. new(iidguid);
  3816. ppufile.getguid(iidguid^);
  3817. iidstr:=stringdup(ppufile.getstring);
  3818. lastvtableindex:=ppufile.getlongint;
  3819. end;
  3820. { load implemented interfaces }
  3821. if objecttype in [odt_class,odt_interfacecorba] then
  3822. begin
  3823. implementedinterfaces:=timplementedinterfaces.create;
  3824. implintfcount:=ppufile.getlongint;
  3825. for i:=1 to implintfcount do
  3826. begin
  3827. ppufile.getderef(d);
  3828. implementedinterfaces.addintf_deref(d,ppufile.getlongint);
  3829. end;
  3830. end
  3831. else
  3832. implementedinterfaces:=nil;
  3833. tobjectsymtable(symtable).ppuload(ppufile);
  3834. symtable.defowner:=self;
  3835. { handles the predefined class tobject }
  3836. { the last TOBJECT which is loaded gets }
  3837. { it ! }
  3838. if (childof=nil) and
  3839. (objecttype=odt_class) and
  3840. (objname^='TOBJECT') then
  3841. class_tobject:=self;
  3842. if (childof=nil) and
  3843. (objecttype=odt_interfacecom) and
  3844. (objname^='IUNKNOWN') then
  3845. interface_iunknown:=self;
  3846. writing_class_record_dbginfo:=false;
  3847. end;
  3848. destructor tobjectdef.destroy;
  3849. begin
  3850. if assigned(symtable) then
  3851. symtable.free;
  3852. stringdispose(objname);
  3853. stringdispose(objrealname);
  3854. if assigned(iidstr) then
  3855. stringdispose(iidstr);
  3856. if assigned(implementedinterfaces) then
  3857. implementedinterfaces.free;
  3858. if assigned(iidguid) then
  3859. dispose(iidguid);
  3860. inherited destroy;
  3861. end;
  3862. function tobjectdef.getcopy : tstoreddef;
  3863. var
  3864. i,
  3865. implintfcount : longint;
  3866. begin
  3867. result:=tobjectdef.create(objecttype,objname^,childof);
  3868. tobjectdef(result).symtable:=symtable.getcopy;
  3869. if assigned(objname) then
  3870. tobjectdef(result).objname:=stringdup(objname^);
  3871. if assigned(objrealname) then
  3872. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3873. tobjectdef(result).objectoptions:=objectoptions;
  3874. tobjectdef(result).vmt_offset:=vmt_offset;
  3875. if assigned(iidguid) then
  3876. begin
  3877. new(tobjectdef(result).iidguid);
  3878. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3879. end;
  3880. if assigned(iidstr) then
  3881. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3882. tobjectdef(result).lastvtableindex:=lastvtableindex;
  3883. if assigned(implementedinterfaces) then
  3884. begin
  3885. implintfcount:=implementedinterfaces.count;
  3886. for i:=1 to implintfcount do
  3887. begin
  3888. tobjectdef(result).implementedinterfaces.addintf_ioffset(implementedinterfaces.interfaces(i),
  3889. implementedinterfaces.ioffsets(i));
  3890. end;
  3891. end;
  3892. end;
  3893. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3894. var
  3895. implintfcount : longint;
  3896. i : longint;
  3897. begin
  3898. inherited ppuwrite(ppufile);
  3899. ppufile.putbyte(byte(objecttype));
  3900. ppufile.putstring(objrealname^);
  3901. ppufile.putaint(tobjectsymtable(symtable).datasize);
  3902. ppufile.putbyte(tobjectsymtable(symtable).fieldalignment);
  3903. ppufile.putbyte(tobjectsymtable(symtable).recordalignment);
  3904. ppufile.putlongint(vmt_offset);
  3905. ppufile.putderef(childofderef);
  3906. ppufile.putsmallset(objectoptions);
  3907. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3908. begin
  3909. ppufile.putguid(iidguid^);
  3910. ppufile.putstring(iidstr^);
  3911. ppufile.putlongint(lastvtableindex);
  3912. end;
  3913. if objecttype in [odt_class,odt_interfacecorba] then
  3914. begin
  3915. implintfcount:=implementedinterfaces.count;
  3916. ppufile.putlongint(implintfcount);
  3917. for i:=1 to implintfcount do
  3918. begin
  3919. ppufile.putderef(implementedinterfaces.interfacesderef(i));
  3920. ppufile.putlongint(implementedinterfaces.ioffsets(i));
  3921. end;
  3922. end;
  3923. ppufile.writeentry(ibobjectdef);
  3924. tobjectsymtable(symtable).ppuwrite(ppufile);
  3925. end;
  3926. function tobjectdef.gettypename:string;
  3927. begin
  3928. if (self <> aktobjectdef) then
  3929. gettypename:=typename
  3930. else
  3931. { in this case we will go in endless recursion, because then }
  3932. { there is no tsym associated yet with the def. It can occur }
  3933. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3934. { instead of the actual type name }
  3935. gettypename:='<Currently Parsed Class>';
  3936. end;
  3937. procedure tobjectdef.buildderef;
  3938. var
  3939. oldrecsyms : tsymtable;
  3940. begin
  3941. inherited buildderef;
  3942. childofderef.build(childof);
  3943. oldrecsyms:=aktrecordsymtable;
  3944. aktrecordsymtable:=symtable;
  3945. tstoredsymtable(symtable).buildderef;
  3946. aktrecordsymtable:=oldrecsyms;
  3947. if objecttype in [odt_class,odt_interfacecorba] then
  3948. implementedinterfaces.buildderef;
  3949. end;
  3950. procedure tobjectdef.deref;
  3951. var
  3952. oldrecsyms : tsymtable;
  3953. begin
  3954. inherited deref;
  3955. childof:=tobjectdef(childofderef.resolve);
  3956. oldrecsyms:=aktrecordsymtable;
  3957. aktrecordsymtable:=symtable;
  3958. tstoredsymtable(symtable).deref;
  3959. aktrecordsymtable:=oldrecsyms;
  3960. if objecttype in [odt_class,odt_interfacecorba] then
  3961. implementedinterfaces.deref;
  3962. end;
  3963. function tobjectdef.getparentdef:tdef;
  3964. begin
  3965. {$warning TODO Remove getparentdef hack}
  3966. { With 2 forward declared classes with the child class before the
  3967. parent class the child class is written earlier to the ppu. Leaving it
  3968. possible to have a reference to the parent class for property overriding,
  3969. but the parent class still has the childof not resolved yet (PFV) }
  3970. if childof=nil then
  3971. childof:=tobjectdef(childofderef.resolve);
  3972. result:=childof;
  3973. end;
  3974. procedure tobjectdef.prepareguid;
  3975. begin
  3976. { set up guid }
  3977. if not assigned(iidguid) then
  3978. begin
  3979. new(iidguid);
  3980. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3981. end;
  3982. { setup iidstring }
  3983. if not assigned(iidstr) then
  3984. iidstr:=stringdup(''); { default is empty string }
  3985. end;
  3986. procedure tobjectdef.set_parent( c : tobjectdef);
  3987. begin
  3988. { nothing to do if the parent was not forward !}
  3989. if assigned(childof) then
  3990. exit;
  3991. childof:=c;
  3992. { some options are inherited !! }
  3993. if assigned(c) then
  3994. begin
  3995. { only important for classes }
  3996. lastvtableindex:=c.lastvtableindex;
  3997. objectoptions:=objectoptions+(c.objectoptions*
  3998. inherited_objectoptions);
  3999. if not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) then
  4000. begin
  4001. { add the data of the anchestor class }
  4002. inc(tobjectsymtable(symtable).datasize,tobjectsymtable(c.symtable).datasize);
  4003. if (oo_has_vmt in objectoptions) and
  4004. (oo_has_vmt in c.objectoptions) then
  4005. dec(tobjectsymtable(symtable).datasize,sizeof(aint));
  4006. { if parent has a vmt field then
  4007. the offset is the same for the child PM }
  4008. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  4009. begin
  4010. vmt_offset:=c.vmt_offset;
  4011. include(objectoptions,oo_has_vmt);
  4012. end;
  4013. end;
  4014. end;
  4015. end;
  4016. procedure tobjectdef.insertvmt;
  4017. begin
  4018. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4019. exit;
  4020. if (oo_has_vmt in objectoptions) then
  4021. internalerror(12345)
  4022. else
  4023. begin
  4024. tobjectsymtable(symtable).datasize:=align(tobjectsymtable(symtable).datasize,
  4025. tobjectsymtable(symtable).fieldalignment);
  4026. {$ifdef cpurequiresproperalignment}
  4027. tobjectsymtable(symtable).datasize:=align(tobjectsymtable(symtable).datasize,sizeof(aint));
  4028. {$endif cpurequiresproperalignment}
  4029. vmt_offset:=tobjectsymtable(symtable).datasize;
  4030. inc(tobjectsymtable(symtable).datasize,sizeof(aint));
  4031. include(objectoptions,oo_has_vmt);
  4032. end;
  4033. end;
  4034. procedure tobjectdef.check_forwards;
  4035. begin
  4036. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) then
  4037. tstoredsymtable(symtable).check_forwards;
  4038. if (oo_is_forward in objectoptions) then
  4039. begin
  4040. { ok, in future, the forward can be resolved }
  4041. Message1(sym_e_class_forward_not_resolved,objrealname^);
  4042. exclude(objectoptions,oo_is_forward);
  4043. end;
  4044. end;
  4045. { true, if self inherits from d (or if they are equal) }
  4046. function tobjectdef.is_related(d : tdef) : boolean;
  4047. var
  4048. hp : tobjectdef;
  4049. begin
  4050. hp:=self;
  4051. while assigned(hp) do
  4052. begin
  4053. if hp=d then
  4054. begin
  4055. is_related:=true;
  4056. exit;
  4057. end;
  4058. hp:=hp.childof;
  4059. end;
  4060. is_related:=false;
  4061. end;
  4062. procedure _searchdestructor(sym:Tnamedindexitem;sd:pointer);
  4063. begin
  4064. { if we found already a destructor, then we exit }
  4065. if (ppointer(sd)^=nil) and
  4066. (Tsym(sym).typ=procsym) then
  4067. ppointer(sd)^:=Tprocsym(sym).search_procdef_bytype(potype_destructor);
  4068. end;
  4069. function tobjectdef.searchdestructor : tprocdef;
  4070. var
  4071. o : tobjectdef;
  4072. sd : tprocdef;
  4073. begin
  4074. searchdestructor:=nil;
  4075. o:=self;
  4076. sd:=nil;
  4077. while assigned(o) do
  4078. begin
  4079. o.symtable.foreach_static(@_searchdestructor,@sd);
  4080. if assigned(sd) then
  4081. begin
  4082. searchdestructor:=sd;
  4083. exit;
  4084. end;
  4085. o:=o.childof;
  4086. end;
  4087. end;
  4088. function tobjectdef.size : aint;
  4089. begin
  4090. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4091. result:=sizeof(aint)
  4092. else
  4093. result:=tobjectsymtable(symtable).datasize;
  4094. end;
  4095. function tobjectdef.alignment:shortint;
  4096. begin
  4097. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  4098. alignment:=sizeof(aint)
  4099. else
  4100. alignment:=tobjectsymtable(symtable).recordalignment;
  4101. end;
  4102. function tobjectdef.vmtmethodoffset(index:longint):longint;
  4103. begin
  4104. { for offset of methods for classes, see rtl/inc/objpash.inc }
  4105. case objecttype of
  4106. odt_class:
  4107. { the +2*sizeof(Aint) is size and -size }
  4108. vmtmethodoffset:=(index+10)*sizeof(aint)+2*sizeof(AInt);
  4109. odt_interfacecom,odt_interfacecorba:
  4110. vmtmethodoffset:=index*sizeof(aint);
  4111. else
  4112. {$ifdef WITHDMT}
  4113. vmtmethodoffset:=(index+4)*sizeof(aint);
  4114. {$else WITHDMT}
  4115. vmtmethodoffset:=(index+3)*sizeof(aint);
  4116. {$endif WITHDMT}
  4117. end;
  4118. end;
  4119. function tobjectdef.vmt_mangledname : string;
  4120. begin
  4121. if not(oo_has_vmt in objectoptions) then
  4122. Message1(parser_n_object_has_no_vmt,objrealname^);
  4123. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  4124. end;
  4125. function tobjectdef.rtti_name : string;
  4126. begin
  4127. rtti_name:=make_mangledname('RTTI',owner,objname^);
  4128. end;
  4129. function tobjectdef.needs_inittable : boolean;
  4130. begin
  4131. case objecttype of
  4132. odt_dispinterface,
  4133. odt_class :
  4134. needs_inittable:=false;
  4135. odt_interfacecom:
  4136. needs_inittable:=true;
  4137. odt_interfacecorba:
  4138. needs_inittable:=is_related(interface_iunknown);
  4139. odt_object:
  4140. needs_inittable:=tobjectsymtable(symtable).needs_init_final;
  4141. else
  4142. internalerror(200108267);
  4143. end;
  4144. end;
  4145. function tobjectdef.members_need_inittable : boolean;
  4146. begin
  4147. members_need_inittable:=tobjectsymtable(symtable).needs_init_final;
  4148. end;
  4149. procedure tobjectdef.collect_published_properties(sym:tnamedindexitem;arg:pointer);
  4150. var
  4151. hp : tpropnamelistitem;
  4152. begin
  4153. if (tsym(sym).typ=propertysym) and
  4154. (sp_published in tsym(sym).symoptions) then
  4155. begin
  4156. hp:=searchpropnamelist(tsym(sym).name);
  4157. if not(assigned(hp)) then
  4158. begin
  4159. hp:=tpropnamelistitem.create;
  4160. hp.name:=tsym(sym).name;
  4161. hp.index:=propnamelist.count;
  4162. hp.owner:=tsym(sym).owner;
  4163. propnamelist.concat(hp);
  4164. end;
  4165. end;
  4166. end;
  4167. procedure tobjectdef.count_published_properties(sym:tnamedindexitem;arg:pointer);
  4168. begin
  4169. if (tsym(sym).typ=propertysym) and
  4170. (sp_published in tsym(sym).symoptions) then
  4171. inc(plongint(arg)^);
  4172. end;
  4173. procedure tobjectdef.write_property_info(sym : tnamedindexitem;arg:pointer);
  4174. var
  4175. proctypesinfo : byte;
  4176. propnameitem : tpropnamelistitem;
  4177. procedure writeproc(proc : tsymlist; shiftvalue : byte; unsetvalue: byte);
  4178. var
  4179. typvalue : byte;
  4180. hp : psymlistitem;
  4181. address : longint;
  4182. def : tdef;
  4183. begin
  4184. if not(assigned(proc) and assigned(proc.firstsym)) then
  4185. begin
  4186. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,unsetvalue));
  4187. typvalue:=3;
  4188. end
  4189. else if proc.firstsym^.sym.typ=fieldvarsym then
  4190. begin
  4191. address:=0;
  4192. hp:=proc.firstsym;
  4193. def:=nil;
  4194. while assigned(hp) do
  4195. begin
  4196. case hp^.sltype of
  4197. sl_load :
  4198. begin
  4199. def:=tfieldvarsym(hp^.sym).vartype.def;
  4200. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  4201. end;
  4202. sl_subscript :
  4203. begin
  4204. if not(assigned(def) and (def.deftype=recorddef)) then
  4205. internalerror(200402171);
  4206. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  4207. def:=tfieldvarsym(hp^.sym).vartype.def;
  4208. end;
  4209. sl_vec :
  4210. begin
  4211. if not(assigned(def) and (def.deftype=arraydef)) then
  4212. internalerror(200402172);
  4213. def:=tarraydef(def).elementtype.def;
  4214. inc(address,def.size*hp^.value);
  4215. end;
  4216. end;
  4217. hp:=hp^.next;
  4218. end;
  4219. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,address));
  4220. typvalue:=0;
  4221. end
  4222. else
  4223. begin
  4224. { When there was an error then procdef is not assigned }
  4225. if not assigned(proc.procdef) then
  4226. exit;
  4227. if not(po_virtualmethod in tprocdef(proc.procdef).procoptions) then
  4228. begin
  4229. current_asmdata.asmlists[al_rtti].concat(Tai_const.createname(tprocdef(proc.procdef).mangledname,0));
  4230. typvalue:=1;
  4231. end
  4232. else
  4233. begin
  4234. { virtual method, write vmt offset }
  4235. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,
  4236. tprocdef(proc.procdef)._class.vmtmethodoffset(tprocdef(proc.procdef).extnumber)));
  4237. typvalue:=2;
  4238. end;
  4239. end;
  4240. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  4241. end;
  4242. begin
  4243. if (tsym(sym).typ=propertysym) and
  4244. (sp_published in tsym(sym).symoptions) then
  4245. begin
  4246. if ppo_indexed in tpropertysym(sym).propoptions then
  4247. proctypesinfo:=$40
  4248. else
  4249. proctypesinfo:=0;
  4250. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(tpropertysym(sym).proptype.def).get_rtti_label(fullrtti)));
  4251. writeproc(tpropertysym(sym).readaccess,0,0);
  4252. writeproc(tpropertysym(sym).writeaccess,2,0);
  4253. { is it stored ? }
  4254. if not(ppo_stored in tpropertysym(sym).propoptions) then
  4255. writeproc(nil,4,0) { no, so put a constant zero }
  4256. else
  4257. writeproc(tpropertysym(sym).storedaccess,4,1); { maybe; if no procedure put a constant 1 (=true) }
  4258. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  4259. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  4260. propnameitem:=searchpropnamelist(tpropertysym(sym).name);
  4261. if not assigned(propnameitem) then
  4262. internalerror(200512201);
  4263. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.index));
  4264. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  4265. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tpropertysym(sym).realname)));
  4266. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tpropertysym(sym).realname));
  4267. {$ifdef cpurequiresproperalignment}
  4268. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4269. {$endif cpurequiresproperalignment}
  4270. end;
  4271. end;
  4272. procedure tobjectdef.generate_published_child_rtti(sym : tnamedindexitem;arg:pointer);
  4273. begin
  4274. if needs_prop_entry(tsym(sym)) then
  4275. begin
  4276. case tsym(sym).typ of
  4277. propertysym:
  4278. tstoreddef(tpropertysym(sym).proptype.def).get_rtti_label(fullrtti);
  4279. fieldvarsym:
  4280. tstoreddef(tfieldvarsym(sym).vartype.def).get_rtti_label(fullrtti);
  4281. else
  4282. internalerror(1509991);
  4283. end;
  4284. end;
  4285. end;
  4286. procedure tobjectdef.write_child_rtti_data(rt:trttitype);
  4287. begin
  4288. FRTTIType:=rt;
  4289. case rt of
  4290. initrtti :
  4291. symtable.foreach(@generate_field_rtti,nil);
  4292. fullrtti :
  4293. symtable.foreach(@generate_published_child_rtti,nil);
  4294. else
  4295. internalerror(200108301);
  4296. end;
  4297. end;
  4298. procedure tobjectdef.count_published_fields(sym:tnamedindexitem;arg:pointer);
  4299. var
  4300. hp : tproptablelistitem;
  4301. begin
  4302. if (tsym(sym).typ=fieldvarsym) and
  4303. (sp_published in tsym(sym).symoptions) then
  4304. begin
  4305. if tfieldvarsym(sym).vartype.def.deftype<>objectdef then
  4306. internalerror(0206001);
  4307. hp:=searchproptablelist(tobjectdef(tfieldvarsym(sym).vartype.def));
  4308. if not(assigned(hp)) then
  4309. begin
  4310. hp:=tproptablelistitem.create;
  4311. hp.def:=tobjectdef(tfieldvarsym(sym).vartype.def);
  4312. hp.index:=proptablelist.count+1;
  4313. proptablelist.concat(hp);
  4314. end;
  4315. inc(plongint(arg)^);
  4316. end;
  4317. end;
  4318. procedure tobjectdef.writefields(sym:tnamedindexitem;arg:pointer);
  4319. var
  4320. hp : tproptablelistitem;
  4321. begin
  4322. if needs_prop_entry(tsym(sym)) and
  4323. (tsym(sym).typ=fieldvarsym) then
  4324. begin
  4325. {$ifdef cpurequiresproperalignment}
  4326. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(AInt)));
  4327. {$endif cpurequiresproperalignment}
  4328. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(tfieldvarsym(sym).fieldoffset));
  4329. hp:=searchproptablelist(tobjectdef(tfieldvarsym(sym).vartype.def));
  4330. if not(assigned(hp)) then
  4331. internalerror(0206002);
  4332. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(hp.index));
  4333. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  4334. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tfieldvarsym(sym).realname));
  4335. end;
  4336. end;
  4337. function tobjectdef.generate_field_table : tasmlabel;
  4338. var
  4339. fieldtable,
  4340. classtable : tasmlabel;
  4341. hp : tproptablelistitem;
  4342. fieldcount : longint;
  4343. begin
  4344. proptablelist:=TLinkedList.Create;
  4345. current_asmdata.getdatalabel(fieldtable);
  4346. current_asmdata.getdatalabel(classtable);
  4347. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  4348. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,classtable.name,const_align(sizeof(aint)));
  4349. { fields }
  4350. fieldcount:=0;
  4351. symtable.foreach(@count_published_fields,@fieldcount);
  4352. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(fieldtable));
  4353. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(fieldcount));
  4354. {$ifdef cpurequiresproperalignment}
  4355. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4356. {$endif cpurequiresproperalignment}
  4357. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(classtable));
  4358. symtable.foreach(@writefields,nil);
  4359. { generate the class table }
  4360. current_asmdata.asmlists[al_rtti].concat(tai_align.create(const_align(sizeof(aint))));
  4361. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(classtable));
  4362. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(proptablelist.count));
  4363. {$ifdef cpurequiresproperalignment}
  4364. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4365. {$endif cpurequiresproperalignment}
  4366. hp:=tproptablelistitem(proptablelist.first);
  4367. while assigned(hp) do
  4368. begin
  4369. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(tobjectdef(hp.def).vmt_mangledname,0));
  4370. hp:=tproptablelistitem(hp.next);
  4371. end;
  4372. generate_field_table:=fieldtable;
  4373. proptablelist.free;
  4374. proptablelist:=nil;
  4375. end;
  4376. procedure tobjectdef.write_rtti_data(rt:trttitype);
  4377. procedure collect_unique_published_props(pd:tobjectdef);
  4378. begin
  4379. if assigned(pd.childof) then
  4380. collect_unique_published_props(pd.childof);
  4381. pd.symtable.foreach(@collect_published_properties,nil);
  4382. end;
  4383. var
  4384. i : longint;
  4385. propcount : longint;
  4386. begin
  4387. case objecttype of
  4388. odt_class:
  4389. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  4390. odt_object:
  4391. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  4392. odt_interfacecom:
  4393. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  4394. odt_interfacecorba:
  4395. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  4396. else
  4397. exit;
  4398. end;
  4399. { generate the name }
  4400. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(objrealname^)));
  4401. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(objrealname^));
  4402. {$ifdef cpurequiresproperalignment}
  4403. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4404. {$endif cpurequiresproperalignment}
  4405. case rt of
  4406. initrtti :
  4407. begin
  4408. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(size));
  4409. if objecttype in [odt_class,odt_object] then
  4410. begin
  4411. count:=0;
  4412. FRTTIType:=rt;
  4413. symtable.foreach(@count_field_rtti,nil);
  4414. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(count));
  4415. symtable.foreach(@write_field_rtti,nil);
  4416. end;
  4417. end;
  4418. fullrtti :
  4419. begin
  4420. { Collect unique property names with nameindex }
  4421. propnamelist:=TLinkedList.Create;
  4422. collect_unique_published_props(self);
  4423. if not(objecttype in [odt_interfacecom,odt_interfacecorba]) then
  4424. begin
  4425. if (oo_has_vmt in objectoptions) then
  4426. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(vmt_mangledname,0))
  4427. else
  4428. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  4429. end;
  4430. { write parent typeinfo }
  4431. if assigned(childof) and ((oo_can_have_published in childof.objectoptions) or
  4432. (objecttype in [odt_interfacecom,odt_interfacecorba])) then
  4433. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(childof.get_rtti_label(fullrtti)))
  4434. else
  4435. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  4436. if objecttype in [odt_object,odt_class] then
  4437. begin
  4438. { total number of unique properties }
  4439. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  4440. end
  4441. else
  4442. { interface: write flags, iid and iidstr }
  4443. begin
  4444. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(
  4445. { ugly, but working }
  4446. longint([
  4447. TCompilerIntfFlag(ord(ifHasGuid)*ord(assigned(iidguid))),
  4448. TCompilerIntfFlag(ord(ifHasStrGUID)*ord(assigned(iidstr)))
  4449. ])
  4450. {
  4451. ifDispInterface,
  4452. ifDispatch, }
  4453. ));
  4454. {$ifdef cpurequiresproperalignment}
  4455. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4456. {$endif cpurequiresproperalignment}
  4457. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(iidguid^.D1)));
  4458. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(iidguid^.D2));
  4459. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(iidguid^.D3));
  4460. for i:=Low(iidguid^.D4) to High(iidguid^.D4) do
  4461. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(iidguid^.D4[i]));
  4462. end;
  4463. { write unit name }
  4464. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  4465. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  4466. {$ifdef cpurequiresproperalignment}
  4467. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4468. {$endif cpurequiresproperalignment}
  4469. { write iidstr }
  4470. if objecttype in [odt_interfacecom,odt_interfacecorba] then
  4471. begin
  4472. if assigned(iidstr) then
  4473. begin
  4474. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(iidstr^)));
  4475. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(iidstr^));
  4476. end
  4477. else
  4478. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  4479. {$ifdef cpurequiresproperalignment}
  4480. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4481. {$endif cpurequiresproperalignment}
  4482. end;
  4483. { write published properties for this object }
  4484. if objecttype in [odt_object,odt_class] then
  4485. begin
  4486. propcount:=0;
  4487. symtable.foreach(@count_published_properties,@propcount);
  4488. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propcount));
  4489. {$ifdef cpurequiresproperalignment}
  4490. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4491. {$endif cpurequiresproperalignment}
  4492. end;
  4493. symtable.foreach(@write_property_info,nil);
  4494. propnamelist.free;
  4495. propnamelist:=nil;
  4496. end;
  4497. end;
  4498. end;
  4499. function tobjectdef.is_publishable : boolean;
  4500. begin
  4501. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba];
  4502. end;
  4503. {****************************************************************************
  4504. TIMPLEMENTEDINTERFACES
  4505. ****************************************************************************}
  4506. type
  4507. tnamemap = class(TNamedIndexItem)
  4508. listnext : TNamedIndexItem;
  4509. newname: pstring;
  4510. constructor create(const aname, anewname: string);
  4511. destructor destroy; override;
  4512. end;
  4513. constructor tnamemap.create(const aname, anewname: string);
  4514. begin
  4515. inherited createname(aname);
  4516. newname:=stringdup(anewname);
  4517. end;
  4518. destructor tnamemap.destroy;
  4519. begin
  4520. stringdispose(newname);
  4521. inherited destroy;
  4522. end;
  4523. type
  4524. tprocdefstore = class(TNamedIndexItem)
  4525. procdef: tprocdef;
  4526. constructor create(aprocdef: tprocdef);
  4527. end;
  4528. constructor tprocdefstore.create(aprocdef: tprocdef);
  4529. begin
  4530. inherited create;
  4531. procdef:=aprocdef;
  4532. end;
  4533. constructor timplintfentry.create(aintf: tobjectdef);
  4534. begin
  4535. inherited create;
  4536. intf:=aintf;
  4537. ioffset:=-1;
  4538. namemappings:=nil;
  4539. procdefs:=nil;
  4540. end;
  4541. constructor timplintfentry.create_deref(const d:tderef);
  4542. begin
  4543. inherited create;
  4544. intf:=nil;
  4545. intfderef:=d;
  4546. ioffset:=-1;
  4547. namemappings:=nil;
  4548. procdefs:=nil;
  4549. end;
  4550. destructor timplintfentry.destroy;
  4551. begin
  4552. if assigned(namemappings) then
  4553. namemappings.free;
  4554. if assigned(procdefs) then
  4555. procdefs.free;
  4556. inherited destroy;
  4557. end;
  4558. constructor timplementedinterfaces.create;
  4559. begin
  4560. finterfaces:=tindexarray.create(1);
  4561. end;
  4562. destructor timplementedinterfaces.destroy;
  4563. begin
  4564. finterfaces.destroy;
  4565. end;
  4566. function timplementedinterfaces.count: longint;
  4567. begin
  4568. count:=finterfaces.count;
  4569. end;
  4570. procedure timplementedinterfaces.checkindex(intfindex: longint);
  4571. begin
  4572. if (intfindex<1) or (intfindex>count) then
  4573. InternalError(200006123);
  4574. end;
  4575. function timplementedinterfaces.interfaces(intfindex: longint): tobjectdef;
  4576. begin
  4577. checkindex(intfindex);
  4578. interfaces:=timplintfentry(finterfaces.search(intfindex)).intf;
  4579. end;
  4580. function timplementedinterfaces.interfacesderef(intfindex: longint): tderef;
  4581. begin
  4582. checkindex(intfindex);
  4583. interfacesderef:=timplintfentry(finterfaces.search(intfindex)).intfderef;
  4584. end;
  4585. function timplementedinterfaces.ioffsets(intfindex: longint): longint;
  4586. begin
  4587. checkindex(intfindex);
  4588. ioffsets:=timplintfentry(finterfaces.search(intfindex)).ioffset;
  4589. end;
  4590. procedure timplementedinterfaces.setioffsets(intfindex,iofs:longint);
  4591. begin
  4592. checkindex(intfindex);
  4593. timplintfentry(finterfaces.search(intfindex)).ioffset:=iofs;
  4594. end;
  4595. function timplementedinterfaces.implindex(intfindex:longint):longint;
  4596. begin
  4597. checkindex(intfindex);
  4598. result:=timplintfentry(finterfaces.search(intfindex)).implindex;
  4599. end;
  4600. procedure timplementedinterfaces.setimplindex(intfindex,implidx:longint);
  4601. begin
  4602. checkindex(intfindex);
  4603. timplintfentry(finterfaces.search(intfindex)).implindex:=implidx;
  4604. end;
  4605. function timplementedinterfaces.searchintf(def: tdef): longint;
  4606. begin
  4607. for result := 1 to count do
  4608. if tdef(interfaces(result)) = def then
  4609. exit;
  4610. result := -1;
  4611. end;
  4612. procedure timplementedinterfaces.buildderef;
  4613. var
  4614. i: longint;
  4615. begin
  4616. for i:=1 to count do
  4617. with timplintfentry(finterfaces.search(i)) do
  4618. intfderef.build(intf);
  4619. end;
  4620. procedure timplementedinterfaces.deref;
  4621. var
  4622. i: longint;
  4623. begin
  4624. for i:=1 to count do
  4625. with timplintfentry(finterfaces.search(i)) do
  4626. intf:=tobjectdef(intfderef.resolve);
  4627. end;
  4628. procedure timplementedinterfaces.addintf_deref(const d:tderef;iofs:longint);
  4629. var
  4630. hintf : timplintfentry;
  4631. begin
  4632. hintf:=timplintfentry.create_deref(d);
  4633. hintf.ioffset:=iofs;
  4634. finterfaces.insert(hintf);
  4635. end;
  4636. procedure timplementedinterfaces.addintf_ioffset(d:tdef;iofs:longint);
  4637. var
  4638. hintf : timplintfentry;
  4639. begin
  4640. hintf:=timplintfentry.create(tobjectdef(d));
  4641. hintf.ioffset:=iofs;
  4642. finterfaces.insert(hintf);
  4643. end;
  4644. procedure timplementedinterfaces.addintf(def: tdef);
  4645. begin
  4646. if not assigned(def) or (searchintf(def)<>-1) or (def.deftype<>objectdef) or
  4647. not (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]) then
  4648. internalerror(200006124);
  4649. finterfaces.insert(timplintfentry.create(tobjectdef(def)));
  4650. end;
  4651. procedure timplementedinterfaces.clearmappings;
  4652. var
  4653. i: longint;
  4654. begin
  4655. for i:=1 to count do
  4656. with timplintfentry(finterfaces.search(i)) do
  4657. begin
  4658. if assigned(namemappings) then
  4659. namemappings.free;
  4660. namemappings:=nil;
  4661. end;
  4662. end;
  4663. procedure timplementedinterfaces.addmappings(intfindex: longint; const origname, newname: string);
  4664. begin
  4665. checkindex(intfindex);
  4666. with timplintfentry(finterfaces.search(intfindex)) do
  4667. begin
  4668. if not assigned(namemappings) then
  4669. namemappings:=tdictionary.create;
  4670. namemappings.insert(tnamemap.create(origname,newname));
  4671. end;
  4672. end;
  4673. function timplementedinterfaces.getmappings(intfindex: longint; const origname: string; var nextexist: pointer): string;
  4674. begin
  4675. checkindex(intfindex);
  4676. if not assigned(nextexist) then
  4677. with timplintfentry(finterfaces.search(intfindex)) do
  4678. begin
  4679. if assigned(namemappings) then
  4680. nextexist:=namemappings.search(origname)
  4681. else
  4682. nextexist:=nil;
  4683. end;
  4684. if assigned(nextexist) then
  4685. begin
  4686. getmappings:=tnamemap(nextexist).newname^;
  4687. nextexist:=tnamemap(nextexist).listnext;
  4688. end
  4689. else
  4690. getmappings:='';
  4691. end;
  4692. procedure timplementedinterfaces.addimplproc(intfindex: longint; procdef: tprocdef);
  4693. var
  4694. found : boolean;
  4695. i : longint;
  4696. begin
  4697. checkindex(intfindex);
  4698. with timplintfentry(finterfaces.search(intfindex)) do
  4699. begin
  4700. if not assigned(procdefs) then
  4701. procdefs:=tindexarray.create(4);
  4702. { No duplicate entries of the same procdef }
  4703. found:=false;
  4704. for i:=1 to procdefs.count do
  4705. if tprocdefstore(procdefs.search(i)).procdef=procdef then
  4706. begin
  4707. found:=true;
  4708. break;
  4709. end;
  4710. if not found then
  4711. procdefs.insert(tprocdefstore.create(procdef));
  4712. end;
  4713. end;
  4714. function timplementedinterfaces.implproccount(intfindex: longint): longint;
  4715. begin
  4716. checkindex(intfindex);
  4717. with timplintfentry(finterfaces.search(intfindex)) do
  4718. if assigned(procdefs) then
  4719. implproccount:=procdefs.count
  4720. else
  4721. implproccount:=0;
  4722. end;
  4723. function timplementedinterfaces.implprocs(intfindex: longint; procindex: longint): tprocdef;
  4724. begin
  4725. checkindex(intfindex);
  4726. with timplintfentry(finterfaces.search(intfindex)) do
  4727. if assigned(procdefs) then
  4728. implprocs:=tprocdefstore(procdefs.search(procindex)).procdef
  4729. else
  4730. internalerror(200006131);
  4731. end;
  4732. function timplementedinterfaces.isimplmergepossible(intfindex, remainindex: longint; var weight: longint): boolean;
  4733. var
  4734. possible: boolean;
  4735. i: longint;
  4736. iiep1: TIndexArray;
  4737. iiep2: TIndexArray;
  4738. begin
  4739. checkindex(intfindex);
  4740. checkindex(remainindex);
  4741. iiep1:=timplintfentry(finterfaces.search(intfindex)).procdefs;
  4742. iiep2:=timplintfentry(finterfaces.search(remainindex)).procdefs;
  4743. if not assigned(iiep1) then { empty interface is mergeable :-) }
  4744. begin
  4745. possible:=true;
  4746. weight:=0;
  4747. end
  4748. else
  4749. begin
  4750. possible:=assigned(iiep2) and (iiep1.count<=iiep2.count);
  4751. i:=1;
  4752. while (possible) and (i<=iiep1.count) do
  4753. begin
  4754. possible:=
  4755. (tprocdefstore(iiep1.search(i)).procdef=tprocdefstore(iiep2.search(i)).procdef);
  4756. inc(i);
  4757. end;
  4758. if possible then
  4759. weight:=iiep1.count;
  4760. end;
  4761. isimplmergepossible:=possible;
  4762. end;
  4763. {****************************************************************************
  4764. TFORWARDDEF
  4765. ****************************************************************************}
  4766. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4767. begin
  4768. inherited create(forwarddef);
  4769. tosymname:=stringdup(s);
  4770. forwardpos:=pos;
  4771. end;
  4772. function tforwarddef.gettypename:string;
  4773. begin
  4774. gettypename:='unresolved forward to '+tosymname^;
  4775. end;
  4776. destructor tforwarddef.destroy;
  4777. begin
  4778. if assigned(tosymname) then
  4779. stringdispose(tosymname);
  4780. inherited destroy;
  4781. end;
  4782. {****************************************************************************
  4783. TUNDEFINEDDEF
  4784. ****************************************************************************}
  4785. constructor tundefineddef.create;
  4786. begin
  4787. inherited create(undefineddef);
  4788. end;
  4789. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4790. begin
  4791. inherited ppuload(undefineddef,ppufile);
  4792. end;
  4793. function tundefineddef.gettypename:string;
  4794. begin
  4795. gettypename:='<undefined type>';
  4796. end;
  4797. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4798. begin
  4799. inherited ppuwrite(ppufile);
  4800. ppufile.writeentry(ibundefineddef);
  4801. end;
  4802. {****************************************************************************
  4803. TERRORDEF
  4804. ****************************************************************************}
  4805. constructor terrordef.create;
  4806. begin
  4807. inherited create(errordef);
  4808. end;
  4809. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4810. begin
  4811. { Can't write errordefs to ppu }
  4812. internalerror(200411063);
  4813. end;
  4814. function terrordef.gettypename:string;
  4815. begin
  4816. gettypename:='<erroneous type>';
  4817. end;
  4818. function terrordef.getmangledparaname:string;
  4819. begin
  4820. getmangledparaname:='error';
  4821. end;
  4822. {****************************************************************************
  4823. Definition Helpers
  4824. ****************************************************************************}
  4825. function is_interfacecom(def: tdef): boolean;
  4826. begin
  4827. is_interfacecom:=
  4828. assigned(def) and
  4829. (def.deftype=objectdef) and
  4830. (tobjectdef(def).objecttype=odt_interfacecom);
  4831. end;
  4832. function is_interfacecorba(def: tdef): boolean;
  4833. begin
  4834. is_interfacecorba:=
  4835. assigned(def) and
  4836. (def.deftype=objectdef) and
  4837. (tobjectdef(def).objecttype=odt_interfacecorba);
  4838. end;
  4839. function is_interface(def: tdef): boolean;
  4840. begin
  4841. is_interface:=
  4842. assigned(def) and
  4843. (def.deftype=objectdef) and
  4844. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4845. end;
  4846. function is_dispinterface(def: tdef): boolean;
  4847. begin
  4848. result:=
  4849. assigned(def) and
  4850. (def.deftype=objectdef) and
  4851. (tobjectdef(def).objecttype=odt_dispinterface);
  4852. end;
  4853. function is_class(def: tdef): boolean;
  4854. begin
  4855. is_class:=
  4856. assigned(def) and
  4857. (def.deftype=objectdef) and
  4858. (tobjectdef(def).objecttype=odt_class);
  4859. end;
  4860. function is_object(def: tdef): boolean;
  4861. begin
  4862. is_object:=
  4863. assigned(def) and
  4864. (def.deftype=objectdef) and
  4865. (tobjectdef(def).objecttype=odt_object);
  4866. end;
  4867. function is_cppclass(def: tdef): boolean;
  4868. begin
  4869. is_cppclass:=
  4870. assigned(def) and
  4871. (def.deftype=objectdef) and
  4872. (tobjectdef(def).objecttype=odt_cppclass);
  4873. end;
  4874. function is_class_or_interface(def: tdef): boolean;
  4875. begin
  4876. is_class_or_interface:=
  4877. assigned(def) and
  4878. (def.deftype=objectdef) and
  4879. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4880. end;
  4881. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4882. begin
  4883. result:=
  4884. assigned(def) and
  4885. (def.deftype=objectdef) and
  4886. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4887. end;
  4888. {$ifdef x86}
  4889. function use_sse(def : tdef) : boolean;
  4890. begin
  4891. use_sse:=(is_single(def) and (aktfputype in sse_singlescalar)) or
  4892. (is_double(def) and (aktfputype in sse_doublescalar));
  4893. end;
  4894. {$endif x86}
  4895. end.