symdef.pas 133 KB

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