symdef.pas 129 KB

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