symdef.pas 131 KB

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