symdef.pas 130 KB

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