symdef.pas 159 KB

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