symdef.pas 136 KB

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