symdef.pas 133 KB

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