symdef.pas 129 KB

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