symdef.pas 136 KB

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