symdef.pas 137 KB

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