symdef.pas 130 KB

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