symdef.pas 135 KB

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