symdef.pas 131 KB

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