symdef.pas 172 KB

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