symdef.pas 141 KB

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