symdef.pas 128 KB

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