symdef.pas 130 KB

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