symdef.pas 135 KB

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