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