symdef.pas 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274
  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. cloneddef : tabstractrecorddef;
  153. cloneddefderef : tderef;
  154. procedure reset;override;
  155. function GetSymtable(t:tGetSymtable):TSymtable;override;
  156. function is_packed:boolean;
  157. end;
  158. trecorddef = class(tabstractrecorddef)
  159. public
  160. isunion : boolean;
  161. constructor create(p : TSymtable);
  162. constructor ppuload(ppufile:tcompilerppufile);
  163. destructor destroy;override;
  164. function getcopy : tstoreddef;override;
  165. procedure ppuwrite(ppufile:tcompilerppufile);override;
  166. procedure buildderef;override;
  167. procedure deref;override;
  168. function size:aint;override;
  169. function alignment : shortint;override;
  170. function padalignment: shortint;
  171. function GetTypeName:string;override;
  172. { debug }
  173. function needs_inittable : boolean;override;
  174. end;
  175. tprocdef = class;
  176. tobjectdef = class;
  177. { TImplementedInterface }
  178. TImplementedInterface = class
  179. IntfDef : tobjectdef;
  180. IntfDefDeref : tderef;
  181. IType : tinterfaceentrytype;
  182. IOffset : longint;
  183. VtblImplIntf : TImplementedInterface;
  184. NameMappings : TFPHashList;
  185. ProcDefs : TFPObjectList;
  186. ImplementsGetter : tsym;
  187. constructor create(aintf: tobjectdef);
  188. constructor create_deref(d:tderef);
  189. destructor destroy; override;
  190. function getcopy:TImplementedInterface;
  191. procedure buildderef;
  192. procedure deref;
  193. procedure AddMapping(const origname, newname: string);
  194. function GetMapping(const origname: string):string;
  195. procedure AddImplProc(pd:tprocdef);
  196. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  197. end;
  198. { tvmtentry }
  199. tvmtentry = record
  200. procdef : tprocdef;
  201. procdefderef : tderef;
  202. visibility : tvisibility;
  203. end;
  204. pvmtentry = ^tvmtentry;
  205. { tobjectdef }
  206. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  207. pmvcallstaticinfo = ^tmvcallstaticinfo;
  208. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  209. tobjectdef = class(tabstractrecorddef)
  210. public
  211. dwarf_struct_lab : tasmsymbol;
  212. childof : tobjectdef;
  213. childofderef : tderef;
  214. { for C++ classes: name of the library this class is imported from }
  215. import_lib,
  216. objname,
  217. objrealname,
  218. { for Objective-C: protocols and classes can have the same name there }
  219. objextname : pshortstring;
  220. objectoptions : tobjectoptions;
  221. { to be able to have a variable vmt position }
  222. { and no vmt field for objects without virtuals }
  223. vmtentries : TFPList;
  224. vmcallstaticinfo : pmvcallstaticinfo;
  225. vmt_offset : longint;
  226. objecttype : tobjecttyp;
  227. iidguid : pguid;
  228. iidstr : pshortstring;
  229. writing_class_record_dbginfo,
  230. { a class of this type has been created in this module }
  231. created_in_current_module,
  232. { a loadvmtnode for this class has been created in this
  233. module, so if a classrefdef variable of this or a parent
  234. class is used somewhere to instantiate a class, then this
  235. class may be instantiated
  236. }
  237. maybe_created_in_current_module,
  238. { a "class of" this particular class has been created in
  239. this module
  240. }
  241. classref_created_in_current_module : boolean;
  242. { store implemented interfaces defs and name mappings }
  243. ImplementedInterfaces : TFPObjectList;
  244. constructor create(ot : tobjecttyp;const n : string;c : tobjectdef);
  245. constructor ppuload(ppufile:tcompilerppufile);
  246. destructor destroy;override;
  247. function getcopy : tstoreddef;override;
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;
  249. function GetTypeName:string;override;
  250. procedure buildderef;override;
  251. procedure deref;override;
  252. procedure buildderefimpl;override;
  253. procedure derefimpl;override;
  254. procedure resetvmtentries;
  255. procedure copyvmtentries(objdef:tobjectdef);
  256. function getparentdef:tdef;override;
  257. function size : aint;override;
  258. function alignment:shortint;override;
  259. function vmtmethodoffset(index:longint):longint;
  260. function members_need_inittable : boolean;
  261. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  262. { this should be called when this class implements an interface }
  263. procedure prepareguid;
  264. function is_publishable : boolean;override;
  265. function needs_inittable : boolean;override;
  266. function rtti_mangledname(rt:trttitype):string;override;
  267. function vmt_mangledname : string;
  268. procedure check_forwards;
  269. function is_related(d : tdef) : boolean;override;
  270. procedure insertvmt;
  271. procedure set_parent(c : tobjectdef);
  272. function FindDestructor : tprocdef;
  273. function implements_any_interfaces: boolean;
  274. procedure reset; override;
  275. { enumerator support }
  276. function search_enumerator_get: tprocdef;
  277. function search_enumerator_move: tprocdef;
  278. function search_enumerator_current: tsym;
  279. { WPO }
  280. procedure register_created_object_type;override;
  281. procedure register_maybe_created_object_type;
  282. procedure register_created_classref_type;
  283. procedure register_vmt_call(index:longint);
  284. { ObjC & C++ }
  285. procedure make_all_methods_external;
  286. { ObjC }
  287. procedure finish_objc_data;
  288. { C++ }
  289. procedure finish_cpp_data;
  290. function RttiName: string;
  291. end;
  292. tclassrefdef = class(tabstractpointerdef)
  293. constructor create(def:tdef);
  294. constructor ppuload(ppufile:tcompilerppufile);
  295. procedure ppuwrite(ppufile:tcompilerppufile);override;
  296. function GetTypeName:string;override;
  297. function is_publishable : boolean;override;
  298. function rtti_mangledname(rt:trttitype):string;override;
  299. procedure register_created_object_type;override;
  300. procedure reset;override;
  301. end;
  302. tarraydef = class(tstoreddef)
  303. lowrange,
  304. highrange : aint;
  305. rangedef : tdef;
  306. rangedefderef : tderef;
  307. arrayoptions : tarraydefoptions;
  308. protected
  309. _elementdef : tdef;
  310. _elementdefderef : tderef;
  311. procedure setelementdef(def:tdef);
  312. public
  313. function elesize : aint;
  314. function elepackedbitsize : aint;
  315. function elecount : aword;
  316. constructor create_from_pointer(def:tdef);
  317. constructor create(l,h : aint;def:tdef);
  318. constructor ppuload(ppufile:tcompilerppufile);
  319. function getcopy : tstoreddef;override;
  320. procedure ppuwrite(ppufile:tcompilerppufile);override;
  321. function GetTypeName:string;override;
  322. function getmangledparaname : string;override;
  323. procedure buildderef;override;
  324. procedure deref;override;
  325. function size : aint;override;
  326. function alignment : shortint;override;
  327. { returns the label of the range check string }
  328. function needs_inittable : boolean;override;
  329. property elementdef : tdef read _elementdef write setelementdef;
  330. function is_publishable : boolean;override;
  331. end;
  332. torddef = class(tstoreddef)
  333. low,high : TConstExprInt;
  334. ordtype : tordtype;
  335. constructor create(t : tordtype;v,b : TConstExprInt);
  336. constructor ppuload(ppufile:tcompilerppufile);
  337. function getcopy : tstoreddef;override;
  338. procedure ppuwrite(ppufile:tcompilerppufile);override;
  339. function is_publishable : boolean;override;
  340. function GetTypeName:string;override;
  341. function alignment:shortint;override;
  342. procedure setsize;
  343. function packedbitsize: aint; override;
  344. function getvardef : longint;override;
  345. end;
  346. tfloatdef = class(tstoreddef)
  347. floattype : tfloattype;
  348. constructor create(t : tfloattype);
  349. constructor ppuload(ppufile:tcompilerppufile);
  350. function getcopy : tstoreddef;override;
  351. procedure ppuwrite(ppufile:tcompilerppufile);override;
  352. function GetTypeName:string;override;
  353. function is_publishable : boolean;override;
  354. function alignment:shortint;override;
  355. procedure setsize;
  356. function getvardef:longint;override;
  357. end;
  358. tabstractprocdef = class(tstoreddef)
  359. { saves a definition to the return type }
  360. returndef : tdef;
  361. returndefderef : tderef;
  362. parast : TSymtable;
  363. paras : tparalist;
  364. proctypeoption : tproctypeoption;
  365. proccalloption : tproccalloption;
  366. procoptions : tprocoptions;
  367. requiredargarea : aint;
  368. { number of user visibile parameters }
  369. maxparacount,
  370. minparacount : byte;
  371. {$ifdef m68k}
  372. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  373. {$endif}
  374. funcretloc : array[tcallercallee] of TLocation;
  375. has_paraloc_info : boolean; { paraloc info is available }
  376. constructor create(dt:tdeftyp;level:byte);
  377. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  378. destructor destroy;override;
  379. procedure ppuwrite(ppufile:tcompilerppufile);override;
  380. procedure buildderef;override;
  381. procedure deref;override;
  382. procedure calcparas;
  383. function typename_paras(showhidden:boolean): string;
  384. function is_methodpointer:boolean;virtual;
  385. function is_addressonly:boolean;virtual;
  386. private
  387. procedure count_para(p:TObject;arg:pointer);
  388. procedure insert_para(p:TObject;arg:pointer);
  389. end;
  390. tprocvardef = class(tabstractprocdef)
  391. constructor create(level:byte);
  392. constructor ppuload(ppufile:tcompilerppufile);
  393. function getcopy : tstoreddef;override;
  394. procedure ppuwrite(ppufile:tcompilerppufile);override;
  395. function GetSymtable(t:tGetSymtable):TSymtable;override;
  396. function size : aint;override;
  397. function GetTypeName:string;override;
  398. function is_publishable : boolean;override;
  399. function is_methodpointer:boolean;override;
  400. function is_addressonly:boolean;override;
  401. function getmangledparaname:string;override;
  402. end;
  403. tmessageinf = record
  404. case integer of
  405. 0 : (str : pshortstring);
  406. 1 : (i : longint);
  407. end;
  408. tinlininginfo = record
  409. { node tree }
  410. code : tnode;
  411. flags : tprocinfoflags;
  412. end;
  413. pinlininginfo = ^tinlininginfo;
  414. {$ifdef oldregvars}
  415. { register variables }
  416. pregvarinfo = ^tregvarinfo;
  417. tregvarinfo = record
  418. regvars : array[1..maxvarregs] of tsym;
  419. regvars_para : array[1..maxvarregs] of boolean;
  420. regvars_refs : array[1..maxvarregs] of longint;
  421. fpuregvars : array[1..maxfpuvarregs] of tsym;
  422. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  423. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  424. end;
  425. {$endif oldregvars}
  426. tprocdef = class(tabstractprocdef)
  427. private
  428. _mangledname : pshortstring;
  429. public
  430. messageinf : tmessageinf;
  431. dispid : longint;
  432. {$ifndef EXTDEBUG}
  433. { where is this function defined and what were the symbol
  434. flags, needed here because there
  435. is only one symbol for all overloaded functions
  436. EXTDEBUG has fileinfo in tdef (PFV) }
  437. fileinfo : tfileposinfo;
  438. {$endif}
  439. symoptions : tsymoptions;
  440. deprecatedmsg : pshortstring;
  441. { symbol owning this definition }
  442. procsym : tsym;
  443. procsymderef : tderef;
  444. { alias names }
  445. aliasnames : TCmdStrList;
  446. { symtables }
  447. localst : TSymtable;
  448. funcretsym : tsym;
  449. funcretsymderef : tderef;
  450. _class : tobjectdef;
  451. _classderef : tderef;
  452. {$if defined(powerpc) or defined(m68k)}
  453. { library symbol for AmigaOS/MorphOS }
  454. libsym : tsym;
  455. libsymderef : tderef;
  456. {$endif powerpc or m68k}
  457. { name of the result variable to insert in the localsymtable }
  458. resultname : pshortstring;
  459. { import info }
  460. import_dll,
  461. import_name : pshortstring;
  462. { info for inlining the subroutine, if this pointer is nil,
  463. the procedure can't be inlined }
  464. inlininginfo : pinlininginfo;
  465. {$ifdef oldregvars}
  466. regvarinfo: pregvarinfo;
  467. {$endif oldregvars}
  468. { position in aasmoutput list }
  469. procstarttai,
  470. procendtai : tai;
  471. import_nr : word;
  472. extnumber : word;
  473. {$ifdef i386}
  474. fpu_used : byte;
  475. {$endif i386}
  476. visibility : tvisibility;
  477. { true, if the procedure is only declared
  478. (forward procedure) }
  479. forwarddef,
  480. { true if the procedure is declared in the interface }
  481. interfacedef : boolean;
  482. { true if the procedure has a forward declaration }
  483. hasforward : boolean;
  484. constructor create(level:byte);
  485. constructor ppuload(ppufile:tcompilerppufile);
  486. destructor destroy;override;
  487. procedure ppuwrite(ppufile:tcompilerppufile);override;
  488. procedure buildderef;override;
  489. procedure buildderefimpl;override;
  490. procedure deref;override;
  491. procedure derefimpl;override;
  492. procedure reset;override;
  493. function GetSymtable(t:tGetSymtable):TSymtable;override;
  494. function GetTypeName : string;override;
  495. function mangledname : string;
  496. procedure setmangledname(const s : string);
  497. function fullprocname(showhidden:boolean):string;
  498. function cplusplusmangledname : string;
  499. function objcmangledname : string;
  500. function is_methodpointer:boolean;override;
  501. function is_addressonly:boolean;override;
  502. end;
  503. { single linked list of overloaded procs }
  504. pprocdeflist = ^tprocdeflist;
  505. tprocdeflist = record
  506. def : tprocdef;
  507. defderef : tderef;
  508. next : pprocdeflist;
  509. end;
  510. tstringdef = class(tstoreddef)
  511. stringtype : tstringtype;
  512. len : aint;
  513. constructor createshort(l : byte);
  514. constructor loadshort(ppufile:tcompilerppufile);
  515. constructor createlong(l : aint);
  516. constructor loadlong(ppufile:tcompilerppufile);
  517. constructor createansi;
  518. constructor loadansi(ppufile:tcompilerppufile);
  519. constructor createwide;
  520. constructor loadwide(ppufile:tcompilerppufile);
  521. constructor createunicode;
  522. constructor loadunicode(ppufile:tcompilerppufile);
  523. function getcopy : tstoreddef;override;
  524. function stringtypname:string;
  525. procedure ppuwrite(ppufile:tcompilerppufile);override;
  526. function GetTypeName:string;override;
  527. function getmangledparaname:string;override;
  528. function is_publishable : boolean;override;
  529. function alignment : shortint;override;
  530. function needs_inittable : boolean;override;
  531. function getvardef:longint;override;
  532. end;
  533. tenumdef = class(tstoreddef)
  534. minval,
  535. maxval : aint;
  536. has_jumps : boolean;
  537. firstenum : tsym; {tenumsym}
  538. basedef : tenumdef;
  539. basedefderef : tderef;
  540. constructor create;
  541. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  542. constructor ppuload(ppufile:tcompilerppufile);
  543. function getcopy : tstoreddef;override;
  544. procedure ppuwrite(ppufile:tcompilerppufile);override;
  545. procedure buildderef;override;
  546. procedure deref;override;
  547. procedure derefimpl;override;
  548. function GetTypeName:string;override;
  549. function is_publishable : boolean;override;
  550. procedure calcsavesize;
  551. function packedbitsize: aint; override;
  552. procedure setmax(_max:aint);
  553. procedure setmin(_min:aint);
  554. function min:aint;
  555. function max:aint;
  556. end;
  557. tsetdef = class(tstoreddef)
  558. elementdef : tdef;
  559. elementdefderef : tderef;
  560. setbase,
  561. setmax : aword;
  562. constructor create(def:tdef;low, high : aint);
  563. constructor ppuload(ppufile:tcompilerppufile);
  564. function getcopy : tstoreddef;override;
  565. procedure ppuwrite(ppufile:tcompilerppufile);override;
  566. procedure buildderef;override;
  567. procedure deref;override;
  568. function GetTypeName:string;override;
  569. function is_publishable : boolean;override;
  570. end;
  571. var
  572. current_objectdef : tobjectdef; { used for private functions check !! }
  573. { default types }
  574. generrordef, { error in definition }
  575. voidpointertype, { pointer for Void-pointeddef }
  576. charpointertype, { pointer for Char-pointeddef }
  577. widecharpointertype, { pointer for WideChar-pointeddef }
  578. voidfarpointertype,
  579. cundefinedtype,
  580. cformaltype, { unique formal definition }
  581. ctypedformaltype, { unique typed formal definition }
  582. voidtype, { Void (procedure) }
  583. cchartype, { Char }
  584. cwidechartype, { WideChar }
  585. booltype, { boolean type }
  586. bool8type,
  587. bool16type,
  588. bool32type,
  589. bool64type, { implement me }
  590. u8inttype, { 8-Bit unsigned integer }
  591. s8inttype, { 8-Bit signed integer }
  592. u16inttype, { 16-Bit unsigned integer }
  593. s16inttype, { 16-Bit signed integer }
  594. u32inttype, { 32-Bit unsigned integer }
  595. s32inttype, { 32-Bit signed integer }
  596. u64inttype, { 64-bit unsigned integer }
  597. s64inttype, { 64-bit signed integer }
  598. s32floattype, { pointer for realconstn }
  599. s64floattype, { pointer for realconstn }
  600. s80floattype, { pointer to type of temp. floats }
  601. s64currencytype, { pointer to a currency type }
  602. cshortstringtype, { pointer to type of short string const }
  603. clongstringtype, { pointer to type of long string const }
  604. cansistringtype, { pointer to type of ansi string const }
  605. cwidestringtype, { pointer to type of wide string const }
  606. cunicodestringtype,
  607. openshortstringtype, { pointer to type of an open shortstring,
  608. needed for readln() }
  609. openchararraytype, { pointer to type of an open array of char,
  610. needed for readln() }
  611. cfiletype, { get the same definition for all file }
  612. { used for stabs }
  613. methodpointertype, { typecasting of methodpointers to extract self }
  614. hresultdef,
  615. { we use only one variant def for every variant class }
  616. cvarianttype,
  617. colevarianttype,
  618. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  619. sinttype,
  620. uinttype,
  621. { unsigned and signed ord type with the same size as a pointer }
  622. ptruinttype,
  623. ptrsinttype,
  624. { several types to simulate more or less C++ objects for GDB }
  625. vmttype,
  626. vmtarraytype,
  627. pvmttype : tdef; { type of classrefs, used for stabs }
  628. { pointer to the anchestor of all classes }
  629. class_tobject : tobjectdef;
  630. { pointer to the ancestor of all COM interfaces }
  631. interface_iunknown : tobjectdef;
  632. { pointer to the TGUID type
  633. of all interfaces }
  634. rec_tguid : trecorddef;
  635. { Objective-C base types }
  636. objc_metaclasstype,
  637. objc_superclasstype,
  638. objc_idtype,
  639. objc_seltype : tpointerdef;
  640. objc_objecttype : trecorddef;
  641. { base type of @protocol(protocolname) Objective-C statements }
  642. objc_protocoltype : tobjectdef;
  643. const
  644. {$ifdef i386}
  645. pbestrealtype : ^tdef = @s80floattype;
  646. {$endif}
  647. {$ifdef x86_64}
  648. pbestrealtype : ^tdef = @s80floattype;
  649. {$endif}
  650. {$ifdef m68k}
  651. pbestrealtype : ^tdef = @s64floattype;
  652. {$endif}
  653. {$ifdef alpha}
  654. pbestrealtype : ^tdef = @s64floattype;
  655. {$endif}
  656. {$ifdef powerpc}
  657. pbestrealtype : ^tdef = @s64floattype;
  658. {$endif}
  659. {$ifdef POWERPC64}
  660. pbestrealtype : ^tdef = @s64floattype;
  661. {$endif}
  662. {$ifdef ia64}
  663. pbestrealtype : ^tdef = @s64floattype;
  664. {$endif}
  665. {$ifdef SPARC}
  666. pbestrealtype : ^tdef = @s64floattype;
  667. {$endif SPARC}
  668. {$ifdef vis}
  669. pbestrealtype : ^tdef = @s64floattype;
  670. {$endif vis}
  671. {$ifdef ARM}
  672. pbestrealtype : ^tdef = @s64floattype;
  673. {$endif ARM}
  674. {$ifdef MIPS}
  675. pbestrealtype : ^tdef = @s64floattype;
  676. {$endif MIPS}
  677. {$ifdef AVR}
  678. pbestrealtype : ^tdef = @s64floattype;
  679. {$endif AVR}
  680. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  681. { should be in the types unit, but the types unit uses the node stuff :( }
  682. function is_interfacecom(def: tdef): boolean;
  683. function is_interfacecorba(def: tdef): boolean;
  684. function is_interface(def: tdef): boolean;
  685. function is_dispinterface(def: tdef): boolean;
  686. function is_object(def: tdef): boolean;
  687. function is_class(def: tdef): boolean;
  688. function is_cppclass(def: tdef): boolean;
  689. function is_objcclass(def: tdef): boolean;
  690. function is_objcclassref(def: tdef): boolean;
  691. function is_objcprotocol(def: tdef): boolean;
  692. function is_objccategory(def: tdef): boolean;
  693. function is_objc_class_or_protocol(def: tdef): boolean;
  694. function is_objc_protocol_or_category(def: tdef): boolean;
  695. function is_class_or_interface(def: tdef): boolean;
  696. function is_class_or_interface_or_objc(def: tdef): boolean;
  697. function is_class_or_interface_or_object(def: tdef): boolean;
  698. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  699. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  700. procedure loadobjctypes;
  701. function use_vectorfpu(def : tdef) : boolean;
  702. implementation
  703. uses
  704. SysUtils,
  705. cutils,
  706. { global }
  707. verbose,
  708. { target }
  709. systems,aasmcpu,paramgr,
  710. { symtable }
  711. symsym,symtable,symutil,defutil,
  712. { module }
  713. fmodule,
  714. { other }
  715. gendef,
  716. fpccrc
  717. ;
  718. {****************************************************************************
  719. Constants
  720. ****************************************************************************}
  721. const
  722. varempty = 0;
  723. varnull = 1;
  724. varsmallint = 2;
  725. varinteger = 3;
  726. varsingle = 4;
  727. vardouble = 5;
  728. varcurrency = 6;
  729. vardate = 7;
  730. varolestr = 8;
  731. vardispatch = 9;
  732. varerror = 10;
  733. varboolean = 11;
  734. varvariant = 12;
  735. varunknown = 13;
  736. vardecimal = 14;
  737. varshortint = 16;
  738. varbyte = 17;
  739. varword = 18;
  740. varlongword = 19;
  741. varint64 = 20;
  742. varqword = 21;
  743. varunicodestr = 22;
  744. varUndefined = -1;
  745. varstrarg = $48;
  746. varstring = $100;
  747. varany = $101;
  748. vardefmask = $fff;
  749. vararray = $2000;
  750. varbyref = $4000;
  751. {****************************************************************************
  752. Helpers
  753. ****************************************************************************}
  754. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  755. var
  756. s,hs,
  757. prefix : string;
  758. oldlen,
  759. newlen,
  760. i : longint;
  761. crc : dword;
  762. hp : tparavarsym;
  763. begin
  764. prefix:='';
  765. if not assigned(st) then
  766. internalerror(200204212);
  767. { sub procedures }
  768. while (st.symtabletype=localsymtable) do
  769. begin
  770. if st.defowner.typ<>procdef then
  771. internalerror(200204173);
  772. { Add the full mangledname of procedure to prevent
  773. conflicts with 2 overloads having both a nested procedure
  774. with the same name, see tb0314 (PFV) }
  775. s:=tprocdef(st.defowner).procsym.name;
  776. oldlen:=length(s);
  777. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  778. begin
  779. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  780. if not(vo_is_hidden_para in hp.varoptions) then
  781. s:=s+'$'+hp.vardef.mangledparaname;
  782. end;
  783. if not is_void(tprocdef(st.defowner).returndef) then
  784. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  785. newlen:=length(s);
  786. { Replace with CRC if the parameter line is very long }
  787. if (newlen-oldlen>12) and
  788. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  789. begin
  790. crc:=0;
  791. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  792. begin
  793. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  794. if not(vo_is_hidden_para in hp.varoptions) then
  795. begin
  796. hs:=hp.vardef.mangledparaname;
  797. crc:=UpdateCrc32(crc,hs[1],length(hs));
  798. end;
  799. end;
  800. hs:=hp.vardef.mangledparaname;
  801. crc:=UpdateCrc32(crc,hs[1],length(hs));
  802. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  803. end;
  804. if prefix<>'' then
  805. prefix:=s+'_'+prefix
  806. else
  807. prefix:=s;
  808. if length(prefix)>100 then
  809. begin
  810. crc:=0;
  811. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  812. prefix:='$CRC'+hexstr(crc,8);
  813. end;
  814. st:=st.defowner.owner;
  815. end;
  816. { object/classes symtable, nested type definitions in classes require the while loop }
  817. while st.symtabletype=ObjectSymtable do
  818. begin
  819. if st.defowner.typ<>objectdef then
  820. internalerror(200204174);
  821. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  822. st:=st.defowner.owner;
  823. end;
  824. { symtable must now be static or global }
  825. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  826. internalerror(200204175);
  827. result:='';
  828. if typeprefix<>'' then
  829. result:=result+typeprefix+'_';
  830. { Add P$ for program, which can have the same name as
  831. a unit }
  832. if (TSymtable(main_module.localsymtable)=st) and
  833. (not main_module.is_unit) then
  834. result:=result+'P$'+st.name^
  835. else
  836. result:=result+st.name^;
  837. if prefix<>'' then
  838. result:=result+'_'+prefix;
  839. if suffix<>'' then
  840. result:=result+'_'+suffix;
  841. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  842. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  843. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  844. { those in the debug map, leading to troubles with dsymutil). So always }
  845. { add an underscore on darwin. }
  846. if (target_info.system in systems_darwin) then
  847. result := '_' + result;
  848. end;
  849. {****************************************************************************
  850. TDEF (base class for definitions)
  851. ****************************************************************************}
  852. constructor tstoreddef.create(dt:tdeftyp);
  853. var
  854. insertstack : psymtablestackitem;
  855. begin
  856. inherited create(dt);
  857. savesize := 0;
  858. {$ifdef EXTDEBUG}
  859. fileinfo := current_filepos;
  860. {$endif}
  861. generictokenbuf:=nil;
  862. genericdef:=nil;
  863. { Don't register forwarddefs, they are disposed at the
  864. end of an type block }
  865. if (dt=forwarddef) then
  866. exit;
  867. { Register in current_module }
  868. if assigned(current_module) then
  869. begin
  870. current_module.deflist.Add(self);
  871. DefId:=current_module.deflist.Count-1;
  872. end;
  873. { Register in symtable stack }
  874. if assigned(symtablestack) then
  875. begin
  876. insertstack:=symtablestack.stack;
  877. while assigned(insertstack) and
  878. (insertstack^.symtable.symtabletype=withsymtable) do
  879. insertstack:=insertstack^.next;
  880. if not assigned(insertstack) then
  881. internalerror(200602044);
  882. insertstack^.symtable.insertdef(self);
  883. end;
  884. end;
  885. destructor tstoreddef.destroy;
  886. begin
  887. { Direct calls are not allowed, use symtable.deletedef() }
  888. if assigned(owner) then
  889. internalerror(200612311);
  890. if assigned(generictokenbuf) then
  891. begin
  892. generictokenbuf.free;
  893. generictokenbuf:=nil;
  894. end;
  895. inherited destroy;
  896. end;
  897. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  898. var
  899. sizeleft,i : longint;
  900. buf : array[0..255] of byte;
  901. begin
  902. inherited create(dt);
  903. DefId:=ppufile.getlongint;
  904. current_module.deflist[DefId]:=self;
  905. {$ifdef EXTDEBUG}
  906. fillchar(fileinfo,sizeof(fileinfo),0);
  907. {$endif}
  908. { load }
  909. ppufile.getderef(typesymderef);
  910. ppufile.getsmallset(defoptions);
  911. ppufile.getsmallset(defstates);
  912. if df_generic in defoptions then
  913. begin
  914. sizeleft:=ppufile.getlongint;
  915. initgeneric;
  916. while sizeleft>0 do
  917. begin
  918. if sizeleft>sizeof(buf) then
  919. i:=sizeof(buf)
  920. else
  921. i:=sizeleft;
  922. ppufile.getdata(buf,i);
  923. generictokenbuf.write(buf,i);
  924. dec(sizeleft,i);
  925. end;
  926. end;
  927. if df_specialization in defoptions then
  928. ppufile.getderef(genericdefderef);
  929. end;
  930. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  931. var
  932. prefix : string[4];
  933. begin
  934. if rt=fullrtti then
  935. begin
  936. prefix:='RTTI';
  937. include(defstates,ds_rtti_table_used);
  938. end
  939. else
  940. begin
  941. prefix:='INIT';
  942. include(defstates,ds_init_table_used);
  943. end;
  944. if assigned(typesym) and
  945. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  946. result:=make_mangledname(prefix,owner,typesym.name)
  947. else
  948. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  949. end;
  950. procedure Tstoreddef.reset;
  951. begin
  952. end;
  953. function tstoreddef.getcopy : tstoreddef;
  954. begin
  955. Message(sym_e_cant_create_unique_type);
  956. getcopy:=terrordef.create;
  957. end;
  958. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  959. var
  960. sizeleft,i : longint;
  961. buf : array[0..255] of byte;
  962. oldintfcrc : boolean;
  963. begin
  964. ppufile.putlongint(DefId);
  965. ppufile.putderef(typesymderef);
  966. ppufile.putsmallset(defoptions);
  967. oldintfcrc:=ppufile.do_crc;
  968. ppufile.do_crc:=false;
  969. ppufile.putsmallset(defstates);
  970. if df_generic in defoptions then
  971. begin
  972. if assigned(generictokenbuf) then
  973. begin
  974. sizeleft:=generictokenbuf.size;
  975. generictokenbuf.seek(0);
  976. end
  977. else
  978. sizeleft:=0;
  979. ppufile.putlongint(sizeleft);
  980. while sizeleft>0 do
  981. begin
  982. if sizeleft>sizeof(buf) then
  983. i:=sizeof(buf)
  984. else
  985. i:=sizeleft;
  986. generictokenbuf.read(buf,i);
  987. ppufile.putdata(buf,i);
  988. dec(sizeleft,i);
  989. end;
  990. end;
  991. ppufile.do_crc:=oldintfcrc;
  992. if df_specialization in defoptions then
  993. ppufile.putderef(genericdefderef);
  994. end;
  995. procedure tstoreddef.buildderef;
  996. begin
  997. typesymderef.build(typesym);
  998. genericdefderef.build(genericdef);
  999. end;
  1000. procedure tstoreddef.buildderefimpl;
  1001. begin
  1002. end;
  1003. procedure tstoreddef.deref;
  1004. begin
  1005. typesym:=ttypesym(typesymderef.resolve);
  1006. if df_specialization in defoptions then
  1007. genericdef:=tstoreddef(genericdefderef.resolve);
  1008. end;
  1009. procedure tstoreddef.derefimpl;
  1010. begin
  1011. end;
  1012. function tstoreddef.size : aint;
  1013. begin
  1014. size:=savesize;
  1015. end;
  1016. function tstoreddef.getvardef:longint;
  1017. begin
  1018. result:=varUndefined;
  1019. end;
  1020. function tstoreddef.alignment : shortint;
  1021. begin
  1022. { natural alignment by default }
  1023. alignment:=size_2_align(savesize);
  1024. { can happen if savesize = 0, e.g. for voiddef or
  1025. an empty record
  1026. }
  1027. if (alignment=0) then
  1028. alignment:=1;
  1029. end;
  1030. { returns true, if the definition can be published }
  1031. function tstoreddef.is_publishable : boolean;
  1032. begin
  1033. is_publishable:=false;
  1034. end;
  1035. { needs an init table }
  1036. function tstoreddef.needs_inittable : boolean;
  1037. begin
  1038. needs_inittable:=false;
  1039. end;
  1040. function tstoreddef.is_intregable : boolean;
  1041. var
  1042. recsize,temp: longint;
  1043. begin
  1044. is_intregable:=false;
  1045. case typ of
  1046. orddef,
  1047. pointerdef,
  1048. enumdef,
  1049. classrefdef:
  1050. is_intregable:=true;
  1051. procvardef :
  1052. is_intregable:=not(po_methodpointer in tprocvardef(self).procoptions);
  1053. objectdef:
  1054. is_intregable:=(is_class_or_interface_or_objc(self)) and not needs_inittable;
  1055. setdef:
  1056. is_intregable:=is_smallset(self);
  1057. recorddef:
  1058. begin
  1059. recsize:=size;
  1060. is_intregable:=
  1061. ispowerof2(recsize,temp) and
  1062. (recsize <= sizeof(aint));
  1063. end;
  1064. end;
  1065. end;
  1066. function tstoreddef.is_fpuregable : boolean;
  1067. begin
  1068. {$ifdef x86}
  1069. result:=use_vectorfpu(self);
  1070. {$else x86}
  1071. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1072. {$endif x86}
  1073. end;
  1074. procedure tstoreddef.initgeneric;
  1075. begin
  1076. if assigned(generictokenbuf) then
  1077. internalerror(200512131);
  1078. generictokenbuf:=tdynamicarray.create(256);
  1079. end;
  1080. {****************************************************************************
  1081. Tstringdef
  1082. ****************************************************************************}
  1083. constructor tstringdef.createshort(l : byte);
  1084. begin
  1085. inherited create(stringdef);
  1086. stringtype:=st_shortstring;
  1087. len:=l;
  1088. savesize:=len+1;
  1089. end;
  1090. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1091. begin
  1092. inherited ppuload(stringdef,ppufile);
  1093. stringtype:=st_shortstring;
  1094. len:=ppufile.getbyte;
  1095. savesize:=len+1;
  1096. end;
  1097. constructor tstringdef.createlong(l : aint);
  1098. begin
  1099. inherited create(stringdef);
  1100. stringtype:=st_longstring;
  1101. len:=l;
  1102. savesize:=sizeof(pint);
  1103. end;
  1104. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1105. begin
  1106. inherited ppuload(stringdef,ppufile);
  1107. stringtype:=st_longstring;
  1108. len:=ppufile.getaint;
  1109. savesize:=sizeof(pint);
  1110. end;
  1111. constructor tstringdef.createansi;
  1112. begin
  1113. inherited create(stringdef);
  1114. stringtype:=st_ansistring;
  1115. len:=-1;
  1116. savesize:=sizeof(pint);
  1117. end;
  1118. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1119. begin
  1120. inherited ppuload(stringdef,ppufile);
  1121. stringtype:=st_ansistring;
  1122. len:=ppufile.getaint;
  1123. savesize:=sizeof(pint);
  1124. end;
  1125. constructor tstringdef.createwide;
  1126. begin
  1127. inherited create(stringdef);
  1128. stringtype:=st_widestring;
  1129. len:=-1;
  1130. savesize:=sizeof(pint);
  1131. end;
  1132. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1133. begin
  1134. inherited ppuload(stringdef,ppufile);
  1135. stringtype:=st_widestring;
  1136. len:=ppufile.getaint;
  1137. savesize:=sizeof(pint);
  1138. end;
  1139. constructor tstringdef.createunicode;
  1140. begin
  1141. inherited create(stringdef);
  1142. stringtype:=st_unicodestring;
  1143. len:=-1;
  1144. savesize:=sizeof(pint);
  1145. end;
  1146. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1147. begin
  1148. inherited ppuload(stringdef,ppufile);
  1149. stringtype:=st_unicodestring;
  1150. len:=ppufile.getaint;
  1151. savesize:=sizeof(pint);
  1152. end;
  1153. function tstringdef.getcopy : tstoreddef;
  1154. begin
  1155. result:=tstringdef.create(typ);
  1156. result.typ:=stringdef;
  1157. tstringdef(result).stringtype:=stringtype;
  1158. tstringdef(result).len:=len;
  1159. tstringdef(result).savesize:=savesize;
  1160. end;
  1161. function tstringdef.stringtypname:string;
  1162. const
  1163. typname:array[tstringtype] of string[10]=(
  1164. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1165. );
  1166. begin
  1167. stringtypname:=typname[stringtype];
  1168. end;
  1169. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1170. begin
  1171. inherited ppuwrite(ppufile);
  1172. if stringtype=st_shortstring then
  1173. begin
  1174. {$ifdef extdebug}
  1175. if len > 255 then internalerror(12122002);
  1176. {$endif}
  1177. ppufile.putbyte(byte(len))
  1178. end
  1179. else
  1180. ppufile.putaint(len);
  1181. case stringtype of
  1182. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1183. st_longstring : ppufile.writeentry(iblongstringdef);
  1184. st_ansistring : ppufile.writeentry(ibansistringdef);
  1185. st_widestring : ppufile.writeentry(ibwidestringdef);
  1186. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1187. end;
  1188. end;
  1189. function tstringdef.needs_inittable : boolean;
  1190. begin
  1191. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1192. end;
  1193. function tstringdef.GetTypeName : string;
  1194. const
  1195. names : array[tstringtype] of string[15] = (
  1196. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1197. begin
  1198. GetTypeName:=names[stringtype];
  1199. end;
  1200. function tstringdef.getvardef : longint;
  1201. const
  1202. vardef : array[tstringtype] of longint = (
  1203. varUndefined,varUndefined,varString,varOleStr,varUnicodeStr);
  1204. begin
  1205. result:=vardef[stringtype];
  1206. end;
  1207. function tstringdef.alignment : shortint;
  1208. begin
  1209. case stringtype of
  1210. st_unicodestring,
  1211. st_widestring,
  1212. st_ansistring:
  1213. alignment:=size_2_align(savesize);
  1214. st_longstring,
  1215. st_shortstring:
  1216. { char to string accesses byte 0 and 1 with one word access }
  1217. if (tf_requires_proper_alignment in target_info.flags) or
  1218. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1219. (m_mac in current_settings.modeswitches) then
  1220. alignment:=size_2_align(2)
  1221. else
  1222. alignment:=size_2_align(1);
  1223. else
  1224. internalerror(200412301);
  1225. end;
  1226. end;
  1227. function tstringdef.getmangledparaname : string;
  1228. begin
  1229. getmangledparaname:='STRING';
  1230. end;
  1231. function tstringdef.is_publishable : boolean;
  1232. begin
  1233. is_publishable:=true;
  1234. end;
  1235. {****************************************************************************
  1236. TENUMDEF
  1237. ****************************************************************************}
  1238. constructor tenumdef.create;
  1239. begin
  1240. inherited create(enumdef);
  1241. minval:=0;
  1242. maxval:=0;
  1243. calcsavesize;
  1244. has_jumps:=false;
  1245. basedef:=nil;
  1246. firstenum:=nil;
  1247. end;
  1248. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1249. begin
  1250. inherited create(enumdef);
  1251. minval:=_min;
  1252. maxval:=_max;
  1253. basedef:=_basedef;
  1254. calcsavesize;
  1255. has_jumps:=false;
  1256. firstenum:=basedef.firstenum;
  1257. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1258. firstenum:=tenumsym(firstenum).nextenum;
  1259. end;
  1260. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1261. begin
  1262. inherited ppuload(enumdef,ppufile);
  1263. ppufile.getderef(basedefderef);
  1264. minval:=ppufile.getaint;
  1265. maxval:=ppufile.getaint;
  1266. savesize:=ppufile.getaint;
  1267. has_jumps:=false;
  1268. firstenum:=Nil;
  1269. end;
  1270. function tenumdef.getcopy : tstoreddef;
  1271. begin
  1272. if assigned(basedef) then
  1273. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1274. else
  1275. begin
  1276. result:=tenumdef.create;
  1277. tenumdef(result).minval:=minval;
  1278. tenumdef(result).maxval:=maxval;
  1279. end;
  1280. tenumdef(result).has_jumps:=has_jumps;
  1281. tenumdef(result).firstenum:=firstenum;
  1282. tenumdef(result).basedefderef:=basedefderef;
  1283. end;
  1284. procedure tenumdef.calcsavesize;
  1285. begin
  1286. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1287. savesize:=8
  1288. else
  1289. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1290. savesize:=4
  1291. else
  1292. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1293. savesize:=2
  1294. else
  1295. savesize:=1;
  1296. end;
  1297. function tenumdef.packedbitsize: aint;
  1298. var
  1299. sizeval: tconstexprint;
  1300. power: longint;
  1301. begin
  1302. result := 0;
  1303. if (minval >= 0) and
  1304. (maxval <= 1) then
  1305. result := 1
  1306. else
  1307. begin
  1308. if (minval>=0) then
  1309. sizeval:=maxval
  1310. else
  1311. { don't count 0 twice }
  1312. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1313. { 256 must become 512 etc. }
  1314. nextpowerof2(sizeval+1,power);
  1315. result := power;
  1316. end;
  1317. end;
  1318. procedure tenumdef.setmax(_max:aint);
  1319. begin
  1320. maxval:=_max;
  1321. calcsavesize;
  1322. end;
  1323. procedure tenumdef.setmin(_min:aint);
  1324. begin
  1325. minval:=_min;
  1326. calcsavesize;
  1327. end;
  1328. function tenumdef.min:aint;
  1329. begin
  1330. min:=minval;
  1331. end;
  1332. function tenumdef.max:aint;
  1333. begin
  1334. max:=maxval;
  1335. end;
  1336. procedure tenumdef.buildderef;
  1337. begin
  1338. inherited buildderef;
  1339. basedefderef.build(basedef);
  1340. end;
  1341. procedure tenumdef.deref;
  1342. begin
  1343. inherited deref;
  1344. basedef:=tenumdef(basedefderef.resolve);
  1345. { restart ordering }
  1346. firstenum:=nil;
  1347. end;
  1348. procedure tenumdef.derefimpl;
  1349. begin
  1350. if assigned(basedef) and
  1351. (firstenum=nil) then
  1352. begin
  1353. firstenum:=basedef.firstenum;
  1354. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1355. firstenum:=tenumsym(firstenum).nextenum;
  1356. end;
  1357. end;
  1358. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1359. begin
  1360. inherited ppuwrite(ppufile);
  1361. ppufile.putderef(basedefderef);
  1362. ppufile.putaint(min);
  1363. ppufile.putaint(max);
  1364. ppufile.putaint(savesize);
  1365. ppufile.writeentry(ibenumdef);
  1366. end;
  1367. function tenumdef.is_publishable : boolean;
  1368. begin
  1369. is_publishable:=true;
  1370. end;
  1371. function tenumdef.GetTypeName : string;
  1372. begin
  1373. GetTypeName:='<enumeration type>';
  1374. end;
  1375. {****************************************************************************
  1376. TORDDEF
  1377. ****************************************************************************}
  1378. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1379. begin
  1380. inherited create(orddef);
  1381. low:=v;
  1382. high:=b;
  1383. ordtype:=t;
  1384. setsize;
  1385. end;
  1386. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1387. begin
  1388. inherited ppuload(orddef,ppufile);
  1389. ordtype:=tordtype(ppufile.getbyte);
  1390. low:=ppufile.getexprint;
  1391. high:=ppufile.getexprint;
  1392. setsize;
  1393. end;
  1394. function torddef.getcopy : tstoreddef;
  1395. begin
  1396. result:=torddef.create(ordtype,low,high);
  1397. result.typ:=orddef;
  1398. torddef(result).low:=low;
  1399. torddef(result).high:=high;
  1400. torddef(result).ordtype:=ordtype;
  1401. torddef(result).savesize:=savesize;
  1402. end;
  1403. function torddef.alignment:shortint;
  1404. begin
  1405. if (target_info.system in [system_i386_darwin,system_arm_darwin]) and
  1406. (ordtype in [s64bit,u64bit]) then
  1407. result := 4
  1408. else
  1409. result := inherited alignment;
  1410. end;
  1411. procedure torddef.setsize;
  1412. const
  1413. sizetbl : array[tordtype] of longint = (
  1414. 0,
  1415. 1,2,4,8,
  1416. 1,2,4,8,
  1417. 1,1,2,4,8,
  1418. 1,2,8
  1419. );
  1420. begin
  1421. savesize:=sizetbl[ordtype];
  1422. end;
  1423. function torddef.packedbitsize: aint;
  1424. var
  1425. sizeval: tconstexprint;
  1426. power: longint;
  1427. begin
  1428. result := 0;
  1429. if ordtype = uvoid then
  1430. exit;
  1431. if (ordtype = u64bit) or
  1432. ((ordtype = s64bit) and
  1433. ((low <= (system.low(int64) div 2)) or
  1434. (high > (system.high(int64) div 2)))) then
  1435. result := 64
  1436. else if (low >= 0) and
  1437. (high <= 1) then
  1438. result := 1
  1439. else
  1440. begin
  1441. if (low>=0) then
  1442. sizeval:=high
  1443. else
  1444. { don't count 0 twice }
  1445. sizeval:=(cutils.max(-low,high)*2)-1;
  1446. { 256 must become 512 etc. }
  1447. nextpowerof2(sizeval+1,power);
  1448. result := power;
  1449. end;
  1450. end;
  1451. function torddef.getvardef : longint;
  1452. const
  1453. basetype2vardef : array[tordtype] of longint = (
  1454. varUndefined,
  1455. varbyte,varqword,varlongword,varqword,
  1456. varshortint,varsmallint,varinteger,varint64,
  1457. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1458. varUndefined,varUndefined,varCurrency);
  1459. begin
  1460. result:=basetype2vardef[ordtype];
  1461. end;
  1462. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1463. begin
  1464. inherited ppuwrite(ppufile);
  1465. ppufile.putbyte(byte(ordtype));
  1466. ppufile.putexprint(low);
  1467. ppufile.putexprint(high);
  1468. ppufile.writeentry(iborddef);
  1469. end;
  1470. function torddef.is_publishable : boolean;
  1471. begin
  1472. is_publishable:=(ordtype<>uvoid);
  1473. end;
  1474. function torddef.GetTypeName : string;
  1475. const
  1476. names : array[tordtype] of string[20] = (
  1477. 'untyped',
  1478. 'Byte','Word','DWord','QWord',
  1479. 'ShortInt','SmallInt','LongInt','Int64',
  1480. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1481. 'Char','WideChar','Currency');
  1482. begin
  1483. GetTypeName:=names[ordtype];
  1484. end;
  1485. {****************************************************************************
  1486. TFLOATDEF
  1487. ****************************************************************************}
  1488. constructor tfloatdef.create(t : tfloattype);
  1489. begin
  1490. inherited create(floatdef);
  1491. floattype:=t;
  1492. setsize;
  1493. end;
  1494. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1495. begin
  1496. inherited ppuload(floatdef,ppufile);
  1497. floattype:=tfloattype(ppufile.getbyte);
  1498. setsize;
  1499. end;
  1500. function tfloatdef.getcopy : tstoreddef;
  1501. begin
  1502. result:=tfloatdef.create(floattype);
  1503. result.typ:=floatdef;
  1504. tfloatdef(result).savesize:=savesize;
  1505. end;
  1506. function tfloatdef.alignment:shortint;
  1507. begin
  1508. if (target_info.system in [system_i386_darwin,system_arm_darwin]) then
  1509. case floattype of
  1510. s80real : result:=16;
  1511. s64real,
  1512. s64currency,
  1513. s64comp : result:=4;
  1514. else
  1515. result := inherited alignment;
  1516. end
  1517. else
  1518. result := inherited alignment;
  1519. end;
  1520. procedure tfloatdef.setsize;
  1521. begin
  1522. case floattype of
  1523. s32real : savesize:=4;
  1524. s80real : savesize:=10;
  1525. s64real,
  1526. s64currency,
  1527. s64comp : savesize:=8;
  1528. else
  1529. savesize:=0;
  1530. end;
  1531. end;
  1532. function tfloatdef.getvardef : longint;
  1533. const
  1534. floattype2vardef : array[tfloattype] of longint = (
  1535. varSingle,varDouble,varUndefined,
  1536. varUndefined,varCurrency,varUndefined);
  1537. begin
  1538. if (upper(typename)='TDATETIME') and
  1539. assigned(owner) and
  1540. assigned(owner.name) and
  1541. (owner.name^='SYSTEM') then
  1542. result:=varDate
  1543. else
  1544. result:=floattype2vardef[floattype];
  1545. end;
  1546. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1547. begin
  1548. inherited ppuwrite(ppufile);
  1549. ppufile.putbyte(byte(floattype));
  1550. ppufile.writeentry(ibfloatdef);
  1551. end;
  1552. function tfloatdef.is_publishable : boolean;
  1553. begin
  1554. is_publishable:=true;
  1555. end;
  1556. function tfloatdef.GetTypeName : string;
  1557. const
  1558. names : array[tfloattype] of string[20] = (
  1559. 'Single','Double','Extended','Comp','Currency','Float128');
  1560. begin
  1561. GetTypeName:=names[floattype];
  1562. end;
  1563. {****************************************************************************
  1564. TFILEDEF
  1565. ****************************************************************************}
  1566. constructor tfiledef.createtext;
  1567. begin
  1568. inherited create(filedef);
  1569. filetyp:=ft_text;
  1570. typedfiledef:=nil;
  1571. setsize;
  1572. end;
  1573. constructor tfiledef.createuntyped;
  1574. begin
  1575. inherited create(filedef);
  1576. filetyp:=ft_untyped;
  1577. typedfiledef:=nil;
  1578. setsize;
  1579. end;
  1580. constructor tfiledef.createtyped(def:tdef);
  1581. begin
  1582. inherited create(filedef);
  1583. filetyp:=ft_typed;
  1584. typedfiledef:=def;
  1585. setsize;
  1586. end;
  1587. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1588. begin
  1589. inherited ppuload(filedef,ppufile);
  1590. filetyp:=tfiletyp(ppufile.getbyte);
  1591. if filetyp=ft_typed then
  1592. ppufile.getderef(typedfiledefderef)
  1593. else
  1594. typedfiledef:=nil;
  1595. setsize;
  1596. end;
  1597. function tfiledef.getcopy : tstoreddef;
  1598. begin
  1599. case filetyp of
  1600. ft_typed:
  1601. result:=tfiledef.createtyped(typedfiledef);
  1602. ft_untyped:
  1603. result:=tfiledef.createuntyped;
  1604. ft_text:
  1605. result:=tfiledef.createtext;
  1606. else
  1607. internalerror(2004121201);
  1608. end;
  1609. end;
  1610. procedure tfiledef.buildderef;
  1611. begin
  1612. inherited buildderef;
  1613. if filetyp=ft_typed then
  1614. typedfiledefderef.build(typedfiledef);
  1615. end;
  1616. procedure tfiledef.deref;
  1617. begin
  1618. inherited deref;
  1619. if filetyp=ft_typed then
  1620. typedfiledef:=tdef(typedfiledefderef.resolve);
  1621. end;
  1622. procedure tfiledef.setsize;
  1623. begin
  1624. {$ifdef cpu64bitaddr}
  1625. case filetyp of
  1626. ft_text :
  1627. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1628. savesize:=632{+8}
  1629. else
  1630. savesize:=628{+8};
  1631. ft_typed,
  1632. ft_untyped :
  1633. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1634. savesize:=372
  1635. else
  1636. savesize:=368;
  1637. end;
  1638. {$else cpu64bitaddr}
  1639. case filetyp of
  1640. ft_text :
  1641. savesize:=592{+4};
  1642. ft_typed,
  1643. ft_untyped :
  1644. savesize:=332;
  1645. end;
  1646. {$endif cpu64bitaddr}
  1647. end;
  1648. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1649. begin
  1650. inherited ppuwrite(ppufile);
  1651. ppufile.putbyte(byte(filetyp));
  1652. if filetyp=ft_typed then
  1653. ppufile.putderef(typedfiledefderef);
  1654. ppufile.writeentry(ibfiledef);
  1655. end;
  1656. function tfiledef.GetTypeName : string;
  1657. begin
  1658. case filetyp of
  1659. ft_untyped:
  1660. GetTypeName:='File';
  1661. ft_typed:
  1662. GetTypeName:='File Of '+typedfiledef.typename;
  1663. ft_text:
  1664. GetTypeName:='Text'
  1665. end;
  1666. end;
  1667. function tfiledef.getmangledparaname : string;
  1668. begin
  1669. case filetyp of
  1670. ft_untyped:
  1671. getmangledparaname:='FILE';
  1672. ft_typed:
  1673. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1674. ft_text:
  1675. getmangledparaname:='TEXT'
  1676. end;
  1677. end;
  1678. {****************************************************************************
  1679. TVARIANTDEF
  1680. ****************************************************************************}
  1681. constructor tvariantdef.create(v : tvarianttype);
  1682. begin
  1683. inherited create(variantdef);
  1684. varianttype:=v;
  1685. setsize;
  1686. end;
  1687. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1688. begin
  1689. inherited ppuload(variantdef,ppufile);
  1690. varianttype:=tvarianttype(ppufile.getbyte);
  1691. setsize;
  1692. end;
  1693. function tvariantdef.getcopy : tstoreddef;
  1694. begin
  1695. result:=tvariantdef.create(varianttype);
  1696. end;
  1697. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1698. begin
  1699. inherited ppuwrite(ppufile);
  1700. ppufile.putbyte(byte(varianttype));
  1701. ppufile.writeentry(ibvariantdef);
  1702. end;
  1703. function tvariantdef.getvardef : longint;
  1704. begin
  1705. Result:=varVariant;
  1706. end;
  1707. procedure tvariantdef.setsize;
  1708. begin
  1709. {$ifdef cpu64bitaddr}
  1710. savesize:=24;
  1711. {$else cpu64bitaddr}
  1712. savesize:=16;
  1713. {$endif cpu64bitaddr}
  1714. end;
  1715. function tvariantdef.GetTypeName : string;
  1716. begin
  1717. case varianttype of
  1718. vt_normalvariant:
  1719. GetTypeName:='Variant';
  1720. vt_olevariant:
  1721. GetTypeName:='OleVariant';
  1722. end;
  1723. end;
  1724. function tvariantdef.needs_inittable : boolean;
  1725. begin
  1726. needs_inittable:=true;
  1727. end;
  1728. function tvariantdef.is_publishable : boolean;
  1729. begin
  1730. is_publishable:=true;
  1731. end;
  1732. {****************************************************************************
  1733. TABSTRACtpointerdef
  1734. ****************************************************************************}
  1735. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1736. begin
  1737. inherited create(dt);
  1738. pointeddef:=def;
  1739. savesize:=sizeof(pint);
  1740. end;
  1741. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1742. begin
  1743. inherited ppuload(dt,ppufile);
  1744. ppufile.getderef(pointeddefderef);
  1745. savesize:=sizeof(pint);
  1746. end;
  1747. procedure tabstractpointerdef.buildderef;
  1748. begin
  1749. inherited buildderef;
  1750. pointeddefderef.build(pointeddef);
  1751. end;
  1752. procedure tabstractpointerdef.deref;
  1753. begin
  1754. inherited deref;
  1755. pointeddef:=tdef(pointeddefderef.resolve);
  1756. end;
  1757. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1758. begin
  1759. inherited ppuwrite(ppufile);
  1760. ppufile.putderef(pointeddefderef);
  1761. end;
  1762. {****************************************************************************
  1763. tpointerdef
  1764. ****************************************************************************}
  1765. constructor tpointerdef.create(def:tdef);
  1766. begin
  1767. inherited create(pointerdef,def);
  1768. is_far:=false;
  1769. end;
  1770. constructor tpointerdef.createfar(def:tdef);
  1771. begin
  1772. inherited create(pointerdef,def);
  1773. is_far:=true;
  1774. end;
  1775. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1776. begin
  1777. inherited ppuload(pointerdef,ppufile);
  1778. is_far:=(ppufile.getbyte<>0);
  1779. end;
  1780. function tpointerdef.getcopy : tstoreddef;
  1781. begin
  1782. result:=tpointerdef.create(pointeddef);
  1783. tpointerdef(result).is_far:=is_far;
  1784. tpointerdef(result).savesize:=savesize;
  1785. end;
  1786. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1787. begin
  1788. inherited ppuwrite(ppufile);
  1789. ppufile.putbyte(byte(is_far));
  1790. ppufile.writeentry(ibpointerdef);
  1791. end;
  1792. function tpointerdef.GetTypeName : string;
  1793. begin
  1794. if is_far then
  1795. GetTypeName:='^'+pointeddef.typename+';far'
  1796. else
  1797. GetTypeName:='^'+pointeddef.typename;
  1798. end;
  1799. {****************************************************************************
  1800. TCLASSREFDEF
  1801. ****************************************************************************}
  1802. constructor tclassrefdef.create(def:tdef);
  1803. begin
  1804. inherited create(classrefdef,def);
  1805. end;
  1806. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1807. begin
  1808. inherited ppuload(classrefdef,ppufile);
  1809. end;
  1810. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1811. begin
  1812. inherited ppuwrite(ppufile);
  1813. ppufile.writeentry(ibclassrefdef);
  1814. end;
  1815. function tclassrefdef.GetTypeName : string;
  1816. begin
  1817. GetTypeName:='Class Of '+pointeddef.typename;
  1818. end;
  1819. function tclassrefdef.is_publishable : boolean;
  1820. begin
  1821. result:=true;
  1822. end;
  1823. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  1824. begin
  1825. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  1826. result:=inherited rtti_mangledname(rt)
  1827. else
  1828. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  1829. end;
  1830. procedure tclassrefdef.reset;
  1831. begin
  1832. tobjectdef(pointeddef).classref_created_in_current_module:=false;
  1833. inherited reset;
  1834. end;
  1835. procedure tclassrefdef.register_created_object_type;
  1836. begin
  1837. tobjectdef(pointeddef).register_created_classref_type;
  1838. end;
  1839. {***************************************************************************
  1840. TSETDEF
  1841. ***************************************************************************}
  1842. constructor tsetdef.create(def:tdef;low, high : aint);
  1843. var
  1844. setallocbits: aint;
  1845. packedsavesize: aint;
  1846. begin
  1847. inherited create(setdef);
  1848. elementdef:=def;
  1849. setmax:=high;
  1850. if (current_settings.setalloc=0) then
  1851. begin
  1852. setbase:=0;
  1853. if (high<32) then
  1854. savesize:=Sizeof(longint)
  1855. else if (high<256) then
  1856. savesize:=32
  1857. else
  1858. savesize:=(high+7) div 8
  1859. end
  1860. else
  1861. begin
  1862. setallocbits:=current_settings.setalloc*8;
  1863. setbase:=low and not(setallocbits-1);
  1864. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1865. savesize:=packedsavesize;
  1866. if savesize=3 then
  1867. savesize:=4;
  1868. end;
  1869. end;
  1870. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1871. begin
  1872. inherited ppuload(setdef,ppufile);
  1873. ppufile.getderef(elementdefderef);
  1874. savesize:=ppufile.getaint;
  1875. setbase:=ppufile.getaint;
  1876. setmax:=ppufile.getaint;
  1877. end;
  1878. function tsetdef.getcopy : tstoreddef;
  1879. begin
  1880. result:=tsetdef.create(elementdef,setbase,setmax);
  1881. { the copy might have been created with a different setalloc setting }
  1882. tsetdef(result).savesize:=savesize;
  1883. end;
  1884. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1885. begin
  1886. inherited ppuwrite(ppufile);
  1887. ppufile.putderef(elementdefderef);
  1888. ppufile.putaint(savesize);
  1889. ppufile.putaint(setbase);
  1890. ppufile.putaint(setmax);
  1891. ppufile.writeentry(ibsetdef);
  1892. end;
  1893. procedure tsetdef.buildderef;
  1894. begin
  1895. inherited buildderef;
  1896. elementdefderef.build(elementdef);
  1897. end;
  1898. procedure tsetdef.deref;
  1899. begin
  1900. inherited deref;
  1901. elementdef:=tdef(elementdefderef.resolve);
  1902. end;
  1903. function tsetdef.is_publishable : boolean;
  1904. begin
  1905. is_publishable:=savesize in [1,2,4];
  1906. end;
  1907. function tsetdef.GetTypeName : string;
  1908. begin
  1909. if assigned(elementdef) then
  1910. GetTypeName:='Set Of '+elementdef.typename
  1911. else
  1912. GetTypeName:='Empty Set';
  1913. end;
  1914. {***************************************************************************
  1915. TFORMALDEF
  1916. ***************************************************************************}
  1917. constructor tformaldef.create(Atyped:boolean);
  1918. begin
  1919. inherited create(formaldef);
  1920. typed:=Atyped;
  1921. savesize:=0;
  1922. end;
  1923. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1924. begin
  1925. inherited ppuload(formaldef,ppufile);
  1926. typed:=boolean(ppufile.getbyte);
  1927. savesize:=0;
  1928. end;
  1929. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1930. begin
  1931. inherited ppuwrite(ppufile);
  1932. ppufile.putbyte(byte(typed));
  1933. ppufile.writeentry(ibformaldef);
  1934. end;
  1935. function tformaldef.GetTypeName : string;
  1936. begin
  1937. if typed then
  1938. GetTypeName:='<Typed formal type>'
  1939. else
  1940. GetTypeName:='<Formal type>';
  1941. end;
  1942. {***************************************************************************
  1943. TARRAYDEF
  1944. ***************************************************************************}
  1945. constructor tarraydef.create(l,h : aint;def:tdef);
  1946. begin
  1947. inherited create(arraydef);
  1948. lowrange:=l;
  1949. highrange:=h;
  1950. rangedef:=def;
  1951. _elementdef:=nil;
  1952. arrayoptions:=[];
  1953. end;
  1954. constructor tarraydef.create_from_pointer(def:tdef);
  1955. begin
  1956. { use -1 so that the elecount will not overflow }
  1957. self.create(0,high(aint)-1,s32inttype);
  1958. arrayoptions:=[ado_IsConvertedPointer];
  1959. setelementdef(def);
  1960. end;
  1961. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  1962. begin
  1963. inherited ppuload(arraydef,ppufile);
  1964. { the addresses are calculated later }
  1965. ppufile.getderef(_elementdefderef);
  1966. ppufile.getderef(rangedefderef);
  1967. lowrange:=ppufile.getaint;
  1968. highrange:=ppufile.getaint;
  1969. ppufile.getsmallset(arrayoptions);
  1970. end;
  1971. function tarraydef.getcopy : tstoreddef;
  1972. begin
  1973. result:=tarraydef.create(lowrange,highrange,rangedef);
  1974. tarraydef(result).arrayoptions:=arrayoptions;
  1975. tarraydef(result)._elementdef:=_elementdef;
  1976. end;
  1977. procedure tarraydef.buildderef;
  1978. begin
  1979. inherited buildderef;
  1980. _elementdefderef.build(_elementdef);
  1981. rangedefderef.build(rangedef);
  1982. end;
  1983. procedure tarraydef.deref;
  1984. begin
  1985. inherited deref;
  1986. _elementdef:=tdef(_elementdefderef.resolve);
  1987. rangedef:=tdef(rangedefderef.resolve);
  1988. end;
  1989. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  1990. begin
  1991. inherited ppuwrite(ppufile);
  1992. ppufile.putderef(_elementdefderef);
  1993. ppufile.putderef(rangedefderef);
  1994. ppufile.putaint(lowrange);
  1995. ppufile.putaint(highrange);
  1996. ppufile.putsmallset(arrayoptions);
  1997. ppufile.writeentry(ibarraydef);
  1998. end;
  1999. function tarraydef.elesize : aint;
  2000. begin
  2001. if (ado_IsBitPacked in arrayoptions) then
  2002. internalerror(2006080101);
  2003. if assigned(_elementdef) then
  2004. result:=_elementdef.size
  2005. else
  2006. result:=0;
  2007. end;
  2008. function tarraydef.elepackedbitsize : aint;
  2009. begin
  2010. if not(ado_IsBitPacked in arrayoptions) then
  2011. internalerror(2006080102);
  2012. if assigned(_elementdef) then
  2013. result:=_elementdef.packedbitsize
  2014. else
  2015. result:=0;
  2016. end;
  2017. function tarraydef.elecount : aword;
  2018. var
  2019. qhigh,qlow : qword;
  2020. begin
  2021. if ado_IsDynamicArray in arrayoptions then
  2022. begin
  2023. result:=0;
  2024. exit;
  2025. end;
  2026. if (highrange>0) and (lowrange<0) then
  2027. begin
  2028. qhigh:=highrange;
  2029. qlow:=qword(-lowrange);
  2030. { prevent overflow, return 0 to indicate overflow }
  2031. if qhigh+qlow>qword(high(aint)-1) then
  2032. result:=0
  2033. else
  2034. result:=qhigh+qlow+1;
  2035. end
  2036. else
  2037. result:=int64(highrange)-lowrange+1;
  2038. end;
  2039. function tarraydef.size : aint;
  2040. var
  2041. cachedelecount : aword;
  2042. cachedelesize : aint;
  2043. begin
  2044. if ado_IsDynamicArray in arrayoptions then
  2045. begin
  2046. size:=sizeof(pint);
  2047. exit;
  2048. end;
  2049. { Tarraydef.size may never be called for an open array! }
  2050. if highrange<lowrange then
  2051. internalerror(99080501);
  2052. if not (ado_IsBitPacked in arrayoptions) then
  2053. cachedelesize:=elesize
  2054. else
  2055. cachedelesize := elepackedbitsize;
  2056. cachedelecount:=elecount;
  2057. if (cachedelesize = 0) then
  2058. begin
  2059. size := 0;
  2060. exit;
  2061. end;
  2062. if (cachedelecount = 0) then
  2063. begin
  2064. size := -1;
  2065. exit;
  2066. end;
  2067. { prevent overflow, return -1 to indicate overflow }
  2068. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2069. if (cachedelecount > aword(high(aint))) or
  2070. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2071. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2072. accessing the array, see ncgmem (PFV) }
  2073. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2074. begin
  2075. result:=-1;
  2076. exit;
  2077. end;
  2078. result:=cachedelesize*aint(cachedelecount);
  2079. if (ado_IsBitPacked in arrayoptions) then
  2080. { can't just add 7 and divide by 8, because that may overflow }
  2081. result:=result div 8 + ord((result mod 8)<>0);
  2082. end;
  2083. procedure tarraydef.setelementdef(def:tdef);
  2084. begin
  2085. _elementdef:=def;
  2086. if not(
  2087. (ado_IsDynamicArray in arrayoptions) or
  2088. (ado_IsConvertedPointer in arrayoptions) or
  2089. (highrange<lowrange)
  2090. ) and
  2091. (size=-1) then
  2092. Message(sym_e_segment_too_large);
  2093. end;
  2094. function tarraydef.alignment : shortint;
  2095. begin
  2096. { alignment of dyn. arrays doesn't depend on the element size }
  2097. if (ado_IsDynamicArray in arrayoptions) then
  2098. alignment:=size_2_align(sizeof(pint))
  2099. { alignment is the alignment of the elements }
  2100. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2101. ((elementdef.typ=objectdef) and
  2102. is_object(elementdef)) then
  2103. alignment:=elementdef.alignment
  2104. { alignment is the size of the elements }
  2105. else if not (ado_IsBitPacked in arrayoptions) then
  2106. alignment:=size_2_align(elesize)
  2107. else
  2108. alignment:=packedbitsloadsize(elepackedbitsize);
  2109. end;
  2110. function tarraydef.needs_inittable : boolean;
  2111. begin
  2112. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2113. end;
  2114. function tarraydef.GetTypeName : string;
  2115. begin
  2116. if (ado_IsConstString in arrayoptions) then
  2117. result:='Constant String'
  2118. else if (ado_isarrayofconst in arrayoptions) or
  2119. (ado_isConstructor in arrayoptions) then
  2120. begin
  2121. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2122. GetTypeName:='Array Of Const'
  2123. else
  2124. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2125. end
  2126. else if (ado_IsDynamicArray in arrayoptions) then
  2127. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2128. else if ((highrange=-1) and (lowrange=0)) then
  2129. GetTypeName:='Open Array Of '+elementdef.typename
  2130. else
  2131. begin
  2132. result := '';
  2133. if (ado_IsBitPacked in arrayoptions) then
  2134. result:='Packed ';
  2135. if rangedef.typ=enumdef then
  2136. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2137. else
  2138. result:=result+'Array['+tostr(lowrange)+'..'+
  2139. tostr(highrange)+'] Of '+elementdef.typename
  2140. end;
  2141. end;
  2142. function tarraydef.getmangledparaname : string;
  2143. begin
  2144. if ado_isarrayofconst in arrayoptions then
  2145. getmangledparaname:='array_of_const'
  2146. else
  2147. if ((highrange=-1) and (lowrange=0)) then
  2148. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2149. else
  2150. internalerror(200204176);
  2151. end;
  2152. function tarraydef.is_publishable : boolean;
  2153. begin
  2154. Result:=ado_IsDynamicArray in arrayoptions;
  2155. end;
  2156. {***************************************************************************
  2157. tabstractrecorddef
  2158. ***************************************************************************}
  2159. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2160. begin
  2161. if t=gs_record then
  2162. GetSymtable:=symtable
  2163. else
  2164. GetSymtable:=nil;
  2165. end;
  2166. procedure tabstractrecorddef.reset;
  2167. begin
  2168. inherited reset;
  2169. tstoredsymtable(symtable).reset_all_defs;
  2170. end;
  2171. function tabstractrecorddef.is_packed:boolean;
  2172. begin
  2173. result:=tabstractrecordsymtable(symtable).is_packed;
  2174. end;
  2175. {***************************************************************************
  2176. trecorddef
  2177. ***************************************************************************}
  2178. constructor trecorddef.create(p : TSymtable);
  2179. begin
  2180. inherited create(recorddef);
  2181. symtable:=p;
  2182. { we can own the symtable only if nobody else owns a copy so far }
  2183. if symtable.refcount=1 then
  2184. symtable.defowner:=self;
  2185. isunion:=false;
  2186. end;
  2187. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2188. begin
  2189. inherited ppuload(recorddef,ppufile);
  2190. if df_copied_def in defoptions then
  2191. ppufile.getderef(cloneddefderef)
  2192. else
  2193. begin
  2194. symtable:=trecordsymtable.create(0);
  2195. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2196. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2197. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2198. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2199. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2200. trecordsymtable(symtable).ppuload(ppufile);
  2201. { requires usefieldalignment to be set }
  2202. symtable.defowner:=self;
  2203. end;
  2204. isunion:=false;
  2205. end;
  2206. destructor trecorddef.destroy;
  2207. begin
  2208. if assigned(symtable) then
  2209. begin
  2210. symtable.free;
  2211. symtable:=nil;
  2212. end;
  2213. inherited destroy;
  2214. end;
  2215. function trecorddef.getcopy : tstoreddef;
  2216. begin
  2217. result:=trecorddef.create(symtable.getcopy);
  2218. trecorddef(result).isunion:=isunion;
  2219. include(trecorddef(result).defoptions,df_copied_def);
  2220. end;
  2221. function trecorddef.needs_inittable : boolean;
  2222. begin
  2223. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2224. end;
  2225. procedure trecorddef.buildderef;
  2226. begin
  2227. inherited buildderef;
  2228. if df_copied_def in defoptions then
  2229. cloneddefderef.build(symtable.defowner)
  2230. else
  2231. tstoredsymtable(symtable).buildderef;
  2232. end;
  2233. procedure trecorddef.deref;
  2234. begin
  2235. inherited deref;
  2236. { now dereference the definitions }
  2237. if df_copied_def in defoptions then
  2238. begin
  2239. cloneddef:=trecorddef(cloneddefderef.resolve);
  2240. symtable:=cloneddef.symtable.getcopy;
  2241. end
  2242. else
  2243. tstoredsymtable(symtable).deref;
  2244. { assign TGUID? load only from system unit }
  2245. if not(assigned(rec_tguid)) and
  2246. (upper(typename)='TGUID') and
  2247. assigned(owner) and
  2248. assigned(owner.name) and
  2249. (owner.name^='SYSTEM') then
  2250. rec_tguid:=self;
  2251. end;
  2252. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2253. begin
  2254. inherited ppuwrite(ppufile);
  2255. if df_copied_def in defoptions then
  2256. ppufile.putderef(cloneddefderef)
  2257. else
  2258. begin
  2259. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2260. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2261. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2262. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2263. ppufile.putaint(trecordsymtable(symtable).datasize);
  2264. end;
  2265. ppufile.writeentry(ibrecorddef);
  2266. if not(df_copied_def in defoptions) then
  2267. trecordsymtable(symtable).ppuwrite(ppufile);
  2268. end;
  2269. function trecorddef.size:aint;
  2270. begin
  2271. result:=trecordsymtable(symtable).datasize;
  2272. end;
  2273. function trecorddef.alignment:shortint;
  2274. begin
  2275. alignment:=trecordsymtable(symtable).recordalignment;
  2276. end;
  2277. function trecorddef.padalignment:shortint;
  2278. begin
  2279. padalignment := trecordsymtable(symtable).padalignment;
  2280. end;
  2281. function trecorddef.GetTypeName : string;
  2282. begin
  2283. GetTypeName:='<record type>'
  2284. end;
  2285. {***************************************************************************
  2286. TABSTRACTPROCDEF
  2287. ***************************************************************************}
  2288. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2289. begin
  2290. inherited create(dt);
  2291. parast:=tparasymtable.create(self,level);
  2292. paras:=nil;
  2293. minparacount:=0;
  2294. maxparacount:=0;
  2295. proctypeoption:=potype_none;
  2296. proccalloption:=pocall_none;
  2297. procoptions:=[];
  2298. returndef:=voidtype;
  2299. savesize:=sizeof(pint);
  2300. requiredargarea:=0;
  2301. has_paraloc_info:=false;
  2302. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2303. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2304. end;
  2305. destructor tabstractprocdef.destroy;
  2306. begin
  2307. if assigned(paras) then
  2308. begin
  2309. {$ifdef MEMDEBUG}
  2310. memprocpara.start;
  2311. {$endif MEMDEBUG}
  2312. paras.free;
  2313. paras:=nil;
  2314. {$ifdef MEMDEBUG}
  2315. memprocpara.stop;
  2316. {$endif MEMDEBUG}
  2317. end;
  2318. if assigned(parast) then
  2319. begin
  2320. {$ifdef MEMDEBUG}
  2321. memprocparast.start;
  2322. {$endif MEMDEBUG}
  2323. parast.free;
  2324. parast:=nil;
  2325. {$ifdef MEMDEBUG}
  2326. memprocparast.stop;
  2327. {$endif MEMDEBUG}
  2328. end;
  2329. inherited destroy;
  2330. end;
  2331. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2332. begin
  2333. if (tsym(p).typ<>paravarsym) then
  2334. exit;
  2335. inc(plongint(arg)^);
  2336. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2337. begin
  2338. if not assigned(tparavarsym(p).defaultconstsym) then
  2339. inc(minparacount);
  2340. inc(maxparacount);
  2341. end;
  2342. end;
  2343. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2344. begin
  2345. if (tsym(p).typ<>paravarsym) then
  2346. exit;
  2347. paras.add(p);
  2348. end;
  2349. procedure tabstractprocdef.calcparas;
  2350. var
  2351. paracount : longint;
  2352. begin
  2353. { This can already be assigned when
  2354. we need to reresolve this unit (PFV) }
  2355. if assigned(paras) then
  2356. paras.free;
  2357. paras:=tparalist.create(false);
  2358. paracount:=0;
  2359. minparacount:=0;
  2360. maxparacount:=0;
  2361. parast.SymList.ForEachCall(@count_para,@paracount);
  2362. paras.capacity:=paracount;
  2363. { Insert parameters in table }
  2364. parast.SymList.ForEachCall(@insert_para,nil);
  2365. { Order parameters }
  2366. paras.sortparas;
  2367. end;
  2368. procedure tabstractprocdef.buildderef;
  2369. begin
  2370. { released procdef? }
  2371. if not assigned(parast) then
  2372. exit;
  2373. inherited buildderef;
  2374. returndefderef.build(returndef);
  2375. { parast }
  2376. tparasymtable(parast).buildderef;
  2377. end;
  2378. procedure tabstractprocdef.deref;
  2379. begin
  2380. inherited deref;
  2381. returndef:=tdef(returndefderef.resolve);
  2382. { parast }
  2383. tparasymtable(parast).deref;
  2384. { recalculated parameters }
  2385. calcparas;
  2386. end;
  2387. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2388. var
  2389. b : byte;
  2390. begin
  2391. inherited ppuload(dt,ppufile);
  2392. parast:=nil;
  2393. Paras:=nil;
  2394. minparacount:=0;
  2395. maxparacount:=0;
  2396. ppufile.getderef(returndefderef);
  2397. { TODO: remove fpu_used loading}
  2398. ppufile.getbyte;
  2399. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2400. proccalloption:=tproccalloption(ppufile.getbyte);
  2401. ppufile.getnormalset(procoptions);
  2402. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2403. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2404. if po_explicitparaloc in procoptions then
  2405. begin
  2406. b:=ppufile.getbyte;
  2407. if b<>sizeof(funcretloc[callerside]) then
  2408. internalerror(200411155);
  2409. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2410. end;
  2411. savesize:=sizeof(pint);
  2412. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2413. end;
  2414. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2415. var
  2416. oldintfcrc : boolean;
  2417. begin
  2418. { released procdef? }
  2419. if not assigned(parast) then
  2420. exit;
  2421. inherited ppuwrite(ppufile);
  2422. ppufile.putderef(returndefderef);
  2423. oldintfcrc:=ppufile.do_interface_crc;
  2424. ppufile.do_interface_crc:=false;
  2425. ppufile.putbyte(0);
  2426. ppufile.putbyte(ord(proctypeoption));
  2427. ppufile.putbyte(ord(proccalloption));
  2428. ppufile.putnormalset(procoptions);
  2429. ppufile.do_interface_crc:=oldintfcrc;
  2430. if (po_explicitparaloc in procoptions) then
  2431. begin
  2432. { Make a 'valid' funcretloc for procedures }
  2433. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2434. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2435. end;
  2436. end;
  2437. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2438. var
  2439. hs,s : string;
  2440. hp : TParavarsym;
  2441. hpc : tconstsym;
  2442. first : boolean;
  2443. i : integer;
  2444. begin
  2445. s:='';
  2446. first:=true;
  2447. for i:=0 to paras.count-1 do
  2448. begin
  2449. hp:=tparavarsym(paras[i]);
  2450. if not(vo_is_hidden_para in hp.varoptions) or
  2451. (showhidden) then
  2452. begin
  2453. if first then
  2454. begin
  2455. s:=s+'(';
  2456. first:=false;
  2457. end
  2458. else
  2459. s:=s+',';
  2460. if vo_is_hidden_para in hp.varoptions then
  2461. s:=s+'<';
  2462. case hp.varspez of
  2463. vs_var :
  2464. s:=s+'var ';
  2465. vs_const :
  2466. s:=s+'const ';
  2467. vs_out :
  2468. s:=s+'out ';
  2469. end;
  2470. if assigned(hp.vardef.typesym) then
  2471. begin
  2472. hs:=hp.vardef.typesym.realname;
  2473. if hs[1]<>'$' then
  2474. s:=s+hs
  2475. else
  2476. s:=s+hp.vardef.GetTypeName;
  2477. end
  2478. else
  2479. s:=s+hp.vardef.GetTypeName;
  2480. { default value }
  2481. if assigned(hp.defaultconstsym) then
  2482. begin
  2483. hpc:=tconstsym(hp.defaultconstsym);
  2484. hs:='';
  2485. case hpc.consttyp of
  2486. conststring,
  2487. constresourcestring :
  2488. begin
  2489. If hpc.value.len>0 then
  2490. begin
  2491. setLength(hs,hpc.value.len);
  2492. move(hpc.value.valueptr^,hs[1],hpc.value.len);
  2493. end;
  2494. end;
  2495. constreal :
  2496. str(pbestreal(hpc.value.valueptr)^,hs);
  2497. constpointer :
  2498. hs:=tostr(hpc.value.valueordptr);
  2499. constord :
  2500. begin
  2501. if is_boolean(hpc.constdef) then
  2502. begin
  2503. if hpc.value.valueord<>0 then
  2504. hs:='TRUE'
  2505. else
  2506. hs:='FALSE';
  2507. end
  2508. else
  2509. hs:=tostr(hpc.value.valueord);
  2510. end;
  2511. constnil :
  2512. hs:='nil';
  2513. constset :
  2514. hs:='<set>';
  2515. end;
  2516. if hs<>'' then
  2517. s:=s+'="'+hs+'"';
  2518. end;
  2519. if vo_is_hidden_para in hp.varoptions then
  2520. s:=s+'>';
  2521. end;
  2522. end;
  2523. if not first then
  2524. s:=s+')';
  2525. if (po_varargs in procoptions) then
  2526. s:=s+';VarArgs';
  2527. typename_paras:=s;
  2528. end;
  2529. function tabstractprocdef.is_methodpointer:boolean;
  2530. begin
  2531. result:=false;
  2532. end;
  2533. function tabstractprocdef.is_addressonly:boolean;
  2534. begin
  2535. result:=true;
  2536. end;
  2537. {***************************************************************************
  2538. TPROCDEF
  2539. ***************************************************************************}
  2540. constructor tprocdef.create(level:byte);
  2541. begin
  2542. inherited create(procdef,level);
  2543. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2544. _mangledname:=nil;
  2545. fileinfo:=current_filepos;
  2546. extnumber:=$ffff;
  2547. aliasnames:=TCmdStrList.create;
  2548. funcretsym:=nil;
  2549. forwarddef:=true;
  2550. interfacedef:=false;
  2551. hasforward:=false;
  2552. _class := nil;
  2553. import_dll:=nil;
  2554. import_name:=nil;
  2555. import_nr:=0;
  2556. inlininginfo:=nil;
  2557. deprecatedmsg:=nil;
  2558. {$ifdef i386}
  2559. fpu_used:=maxfpuregs;
  2560. {$endif i386}
  2561. end;
  2562. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2563. var
  2564. i,aliasnamescount : longint;
  2565. level : byte;
  2566. begin
  2567. inherited ppuload(procdef,ppufile);
  2568. if po_has_mangledname in procoptions then
  2569. _mangledname:=stringdup(ppufile.getstring)
  2570. else
  2571. _mangledname:=nil;
  2572. extnumber:=ppufile.getword;
  2573. level:=ppufile.getbyte;
  2574. ppufile.getderef(_classderef);
  2575. ppufile.getderef(procsymderef);
  2576. ppufile.getposinfo(fileinfo);
  2577. visibility:=tvisibility(ppufile.getbyte);
  2578. ppufile.getsmallset(symoptions);
  2579. if sp_has_deprecated_msg in symoptions then
  2580. deprecatedmsg:=stringdup(ppufile.getstring)
  2581. else
  2582. deprecatedmsg:=nil;
  2583. {$ifdef powerpc}
  2584. { library symbol for AmigaOS/MorphOS }
  2585. ppufile.getderef(libsymderef);
  2586. {$endif powerpc}
  2587. { import stuff }
  2588. if po_has_importdll in procoptions then
  2589. import_dll:=stringdup(ppufile.getstring)
  2590. else
  2591. import_dll:=nil;
  2592. if po_has_importname in procoptions then
  2593. import_name:=stringdup(ppufile.getstring)
  2594. else
  2595. import_name:=nil;
  2596. import_nr:=ppufile.getword;
  2597. if (po_msgint in procoptions) then
  2598. messageinf.i:=ppufile.getlongint;
  2599. if (po_msgstr in procoptions) then
  2600. messageinf.str:=stringdup(ppufile.getstring);
  2601. if (po_dispid in procoptions) then
  2602. dispid:=ppufile.getlongint;
  2603. { inline stuff }
  2604. if (po_has_inlininginfo in procoptions) then
  2605. begin
  2606. ppufile.getderef(funcretsymderef);
  2607. new(inlininginfo);
  2608. ppufile.getsmallset(inlininginfo^.flags);
  2609. end
  2610. else
  2611. begin
  2612. inlininginfo:=nil;
  2613. funcretsym:=nil;
  2614. end;
  2615. aliasnames:=TCmdStrList.create;
  2616. { count alias names }
  2617. aliasnamescount:=ppufile.getbyte;
  2618. for i:=1 to aliasnamescount do
  2619. aliasnames.insert(ppufile.getstring);
  2620. { load para symtable }
  2621. parast:=tparasymtable.create(self,level);
  2622. tparasymtable(parast).ppuload(ppufile);
  2623. { load local symtable }
  2624. if (po_has_inlininginfo in procoptions) then
  2625. begin
  2626. localst:=tlocalsymtable.create(self,level);
  2627. tlocalsymtable(localst).ppuload(ppufile);
  2628. end
  2629. else
  2630. localst:=nil;
  2631. { inline stuff }
  2632. if (po_has_inlininginfo in procoptions) then
  2633. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2634. { default values for no persistent data }
  2635. if (cs_link_deffile in current_settings.globalswitches) and
  2636. (tf_need_export in target_info.flags) and
  2637. (po_exports in procoptions) then
  2638. deffile.AddExport(mangledname);
  2639. forwarddef:=false;
  2640. interfacedef:=false;
  2641. hasforward:=false;
  2642. { Disable po_has_inlining until the derefimpl is done }
  2643. exclude(procoptions,po_has_inlininginfo);
  2644. {$ifdef i386}
  2645. fpu_used:=maxfpuregs;
  2646. {$endif i386}
  2647. end;
  2648. destructor tprocdef.destroy;
  2649. begin
  2650. aliasnames.free;
  2651. aliasnames:=nil;
  2652. if assigned(localst) and
  2653. (localst.symtabletype<>staticsymtable) then
  2654. begin
  2655. {$ifdef MEMDEBUG}
  2656. memproclocalst.start;
  2657. {$endif MEMDEBUG}
  2658. localst.free;
  2659. localst:=nil;
  2660. {$ifdef MEMDEBUG}
  2661. memproclocalst.start;
  2662. {$endif MEMDEBUG}
  2663. end;
  2664. if assigned(inlininginfo) then
  2665. begin
  2666. {$ifdef MEMDEBUG}
  2667. memprocnodetree.start;
  2668. {$endif MEMDEBUG}
  2669. tnode(inlininginfo^.code).free;
  2670. {$ifdef MEMDEBUG}
  2671. memprocnodetree.start;
  2672. {$endif MEMDEBUG}
  2673. dispose(inlininginfo);
  2674. inlininginfo:=nil;
  2675. end;
  2676. stringdispose(resultname);
  2677. stringdispose(import_dll);
  2678. stringdispose(import_name);
  2679. stringdispose(deprecatedmsg);
  2680. if (po_msgstr in procoptions) then
  2681. stringdispose(messageinf.str);
  2682. if assigned(_mangledname) then
  2683. begin
  2684. {$ifdef MEMDEBUG}
  2685. memmanglednames.start;
  2686. {$endif MEMDEBUG}
  2687. stringdispose(_mangledname);
  2688. {$ifdef MEMDEBUG}
  2689. memmanglednames.stop;
  2690. {$endif MEMDEBUG}
  2691. end;
  2692. inherited destroy;
  2693. end;
  2694. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2695. var
  2696. oldintfcrc : boolean;
  2697. aliasnamescount : longint;
  2698. item : TCmdStrListItem;
  2699. begin
  2700. { released procdef? }
  2701. if not assigned(parast) then
  2702. exit;
  2703. inherited ppuwrite(ppufile);
  2704. if po_has_mangledname in procoptions then
  2705. ppufile.putstring(_mangledname^);
  2706. ppufile.putword(extnumber);
  2707. ppufile.putbyte(parast.symtablelevel);
  2708. ppufile.putderef(_classderef);
  2709. ppufile.putderef(procsymderef);
  2710. ppufile.putposinfo(fileinfo);
  2711. ppufile.putbyte(byte(visibility));
  2712. ppufile.putsmallset(symoptions);
  2713. if sp_has_deprecated_msg in symoptions then
  2714. ppufile.putstring(deprecatedmsg^);
  2715. {$ifdef powerpc}
  2716. { library symbol for AmigaOS/MorphOS }
  2717. ppufile.putderef(libsymderef);
  2718. {$endif powerpc}
  2719. { import }
  2720. if po_has_importdll in procoptions then
  2721. ppufile.putstring(import_dll^);
  2722. if po_has_importname in procoptions then
  2723. ppufile.putstring(import_name^);
  2724. ppufile.putword(import_nr);
  2725. if (po_msgint in procoptions) then
  2726. ppufile.putlongint(messageinf.i);
  2727. if (po_msgstr in procoptions) then
  2728. ppufile.putstring(messageinf.str^);
  2729. if (po_dispid in procoptions) then
  2730. ppufile.putlongint(dispid);
  2731. { inline stuff }
  2732. oldintfcrc:=ppufile.do_crc;
  2733. ppufile.do_crc:=false;
  2734. if (po_has_inlininginfo in procoptions) then
  2735. begin
  2736. ppufile.putderef(funcretsymderef);
  2737. ppufile.putsmallset(inlininginfo^.flags);
  2738. end;
  2739. { count alias names }
  2740. aliasnamescount:=0;
  2741. item:=TCmdStrListItem(aliasnames.first);
  2742. while assigned(item) do
  2743. begin
  2744. inc(aliasnamescount);
  2745. item:=TCmdStrListItem(item.next);
  2746. end;
  2747. if aliasnamescount>255 then
  2748. internalerror(200711021);
  2749. ppufile.putbyte(aliasnamescount);
  2750. item:=TCmdStrListItem(aliasnames.first);
  2751. while assigned(item) do
  2752. begin
  2753. ppufile.putstring(item.str);
  2754. item:=TCmdStrListItem(item.next);
  2755. end;
  2756. ppufile.do_crc:=oldintfcrc;
  2757. { write this entry }
  2758. ppufile.writeentry(ibprocdef);
  2759. { Save the para symtable, this is taken from the interface }
  2760. tparasymtable(parast).ppuwrite(ppufile);
  2761. { save localsymtable for inline procedures or when local
  2762. browser info is requested, this has no influence on the crc }
  2763. if (po_has_inlininginfo in procoptions) then
  2764. begin
  2765. oldintfcrc:=ppufile.do_crc;
  2766. ppufile.do_crc:=false;
  2767. tlocalsymtable(localst).ppuwrite(ppufile);
  2768. ppufile.do_crc:=oldintfcrc;
  2769. end;
  2770. { node tree for inlining }
  2771. oldintfcrc:=ppufile.do_crc;
  2772. ppufile.do_crc:=false;
  2773. if (po_has_inlininginfo in procoptions) then
  2774. ppuwritenodetree(ppufile,inlininginfo^.code);
  2775. ppufile.do_crc:=oldintfcrc;
  2776. end;
  2777. procedure tprocdef.reset;
  2778. begin
  2779. inherited reset;
  2780. procstarttai:=nil;
  2781. procendtai:=nil;
  2782. end;
  2783. function tprocdef.fullprocname(showhidden:boolean):string;
  2784. var
  2785. s : string;
  2786. t : ttoken;
  2787. begin
  2788. {$ifdef EXTDEBUG}
  2789. showhidden:=true;
  2790. {$endif EXTDEBUG}
  2791. s:='';
  2792. if assigned(_class) then
  2793. begin
  2794. s:=_class.RttiName+'.';
  2795. if (po_classmethod in procoptions) then
  2796. s:='class ' + s;
  2797. end;
  2798. if owner.symtabletype=localsymtable then
  2799. s:='local ' + s;
  2800. if proctypeoption=potype_operator then
  2801. begin
  2802. for t:=NOTOKEN to last_overloaded do
  2803. if procsym.realname='$'+overloaded_names[t] then
  2804. begin
  2805. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2806. break;
  2807. end;
  2808. end
  2809. else
  2810. s:=s+procsym.realname+typename_paras(showhidden);
  2811. case proctypeoption of
  2812. potype_constructor:
  2813. s:='constructor '+s;
  2814. potype_destructor:
  2815. s:='destructor '+s;
  2816. else
  2817. if assigned(returndef) and
  2818. not(is_void(returndef)) then
  2819. s:=s+':'+returndef.GetTypeName;
  2820. end;
  2821. s:=s+';';
  2822. { forced calling convention? }
  2823. if (po_hascallingconvention in procoptions) then
  2824. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2825. if po_staticmethod in procoptions then
  2826. s:=s+' Static;';
  2827. fullprocname:=s;
  2828. end;
  2829. function tprocdef.is_methodpointer:boolean;
  2830. begin
  2831. result:=assigned(_class);
  2832. end;
  2833. function tprocdef.is_addressonly:boolean;
  2834. begin
  2835. result:=assigned(owner) and
  2836. (owner.symtabletype<>ObjectSymtable);
  2837. end;
  2838. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2839. begin
  2840. case t of
  2841. gs_local :
  2842. GetSymtable:=localst;
  2843. gs_para :
  2844. GetSymtable:=parast;
  2845. else
  2846. GetSymtable:=nil;
  2847. end;
  2848. end;
  2849. procedure tprocdef.buildderef;
  2850. begin
  2851. inherited buildderef;
  2852. _classderef.build(_class);
  2853. { procsym that originaly defined this definition, should be in the
  2854. same symtable }
  2855. procsymderef.build(procsym);
  2856. {$ifdef powerpc}
  2857. { library symbol for AmigaOS/MorphOS }
  2858. libsymderef.build(libsym);
  2859. {$endif powerpc}
  2860. end;
  2861. procedure tprocdef.buildderefimpl;
  2862. begin
  2863. inherited buildderefimpl;
  2864. { Localst is not available for main/unit init }
  2865. if assigned(localst) then
  2866. begin
  2867. tlocalsymtable(localst).buildderef;
  2868. tlocalsymtable(localst).buildderefimpl;
  2869. end;
  2870. { inline tree }
  2871. if (po_has_inlininginfo in procoptions) then
  2872. begin
  2873. funcretsymderef.build(funcretsym);
  2874. inlininginfo^.code.buildderefimpl;
  2875. end;
  2876. end;
  2877. procedure tprocdef.deref;
  2878. begin
  2879. inherited deref;
  2880. _class:=tobjectdef(_classderef.resolve);
  2881. { procsym that originaly defined this definition, should be in the
  2882. same symtable }
  2883. procsym:=tprocsym(procsymderef.resolve);
  2884. {$ifdef powerpc}
  2885. { library symbol for AmigaOS/MorphOS }
  2886. libsym:=tsym(libsymderef.resolve);
  2887. {$endif powerpc}
  2888. end;
  2889. procedure tprocdef.derefimpl;
  2890. begin
  2891. { Enable has_inlininginfo when the inlininginfo
  2892. structure is available. The has_inlininginfo was disabled
  2893. after the load, since the data was invalid }
  2894. if assigned(inlininginfo) then
  2895. include(procoptions,po_has_inlininginfo);
  2896. { Locals }
  2897. if assigned(localst) then
  2898. begin
  2899. tlocalsymtable(localst).deref;
  2900. tlocalsymtable(localst).derefimpl;
  2901. end;
  2902. { Inline }
  2903. if (po_has_inlininginfo in procoptions) then
  2904. begin
  2905. inlininginfo^.code.derefimpl;
  2906. { funcretsym, this is always located in the localst }
  2907. funcretsym:=tsym(funcretsymderef.resolve);
  2908. end
  2909. else
  2910. begin
  2911. { safety }
  2912. { Not safe! A unit may be reresolved after its interface has been
  2913. parsed but before its implementation has been parsed, and in that
  2914. case the funcretsym is still required!
  2915. funcretsym:=nil; }
  2916. end;
  2917. end;
  2918. function tprocdef.GetTypeName : string;
  2919. begin
  2920. GetTypeName := FullProcName(false);
  2921. end;
  2922. function tprocdef.mangledname : string;
  2923. var
  2924. hp : TParavarsym;
  2925. hs : string;
  2926. crc : dword;
  2927. newlen,
  2928. oldlen,
  2929. i : integer;
  2930. begin
  2931. if assigned(_mangledname) then
  2932. begin
  2933. {$ifdef compress}
  2934. mangledname:=minilzw_decode(_mangledname^);
  2935. {$else}
  2936. mangledname:=_mangledname^;
  2937. {$endif}
  2938. exit;
  2939. end;
  2940. { we need to use the symtable where the procsym is inserted,
  2941. because that is visible to the world }
  2942. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2943. oldlen:=length(mangledname);
  2944. { add parameter types }
  2945. for i:=0 to paras.count-1 do
  2946. begin
  2947. hp:=tparavarsym(paras[i]);
  2948. if not(vo_is_hidden_para in hp.varoptions) then
  2949. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2950. end;
  2951. { add resultdef, add $$ as separator to make it unique from a
  2952. parameter separator }
  2953. if not is_void(returndef) then
  2954. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2955. newlen:=length(mangledname);
  2956. { Replace with CRC if the parameter line is very long }
  2957. if (newlen-oldlen>12) and
  2958. ((newlen>100) or (newlen-oldlen>64)) then
  2959. begin
  2960. crc:=0;
  2961. for i:=0 to paras.count-1 do
  2962. begin
  2963. hp:=tparavarsym(paras[i]);
  2964. if not(vo_is_hidden_para in hp.varoptions) then
  2965. begin
  2966. hs:=hp.vardef.mangledparaname;
  2967. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2968. end;
  2969. end;
  2970. hs:=hp.vardef.mangledparaname;
  2971. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2972. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2973. end;
  2974. {$ifdef compress}
  2975. _mangledname:=stringdup(minilzw_encode(mangledname));
  2976. {$else}
  2977. _mangledname:=stringdup(mangledname);
  2978. {$endif}
  2979. end;
  2980. function tprocdef.cplusplusmangledname : string;
  2981. function getcppparaname(p : tdef) : string;
  2982. const
  2983. {$ifdef NAMEMANGLING_GCC2}
  2984. ordtype2str : array[tordtype] of string[2] = (
  2985. '',
  2986. 'Uc','Us','Ui','Us',
  2987. 'Sc','s','i','x',
  2988. 'b','b','b','b','b',
  2989. 'c','w','x');
  2990. {$else NAMEMANGLING_GCC2}
  2991. ordtype2str : array[tordtype] of string[1] = (
  2992. 'v',
  2993. 'h','t','j','y',
  2994. 'a','s','i','x',
  2995. 'b','b','b','b','b',
  2996. 'c','w','x');
  2997. floattype2str : array[tfloattype] of string[1] = (
  2998. 'f','d','e',
  2999. 'd','d','g');
  3000. {$endif NAMEMANGLING_GCC2}
  3001. var
  3002. s : string;
  3003. begin
  3004. case p.typ of
  3005. orddef:
  3006. s:=ordtype2str[torddef(p).ordtype];
  3007. pointerdef:
  3008. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3009. {$ifndef NAMEMANGLING_GCC2}
  3010. floatdef:
  3011. s:=floattype2str[tfloatdef(p).floattype];
  3012. {$endif NAMEMANGLING_GCC2}
  3013. else
  3014. internalerror(2103001);
  3015. end;
  3016. getcppparaname:=s;
  3017. end;
  3018. var
  3019. s,s2 : string;
  3020. hp : TParavarsym;
  3021. i : integer;
  3022. begin
  3023. {$ifdef NAMEMANGLING_GCC2}
  3024. { outdated gcc 2.x name mangling scheme }
  3025. s := procsym.realname;
  3026. if procsym.owner.symtabletype=ObjectSymtable then
  3027. begin
  3028. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3029. case proctypeoption of
  3030. potype_destructor:
  3031. s:='_$_'+tostr(length(s2))+s2;
  3032. potype_constructor:
  3033. s:='___'+tostr(length(s2))+s2;
  3034. else
  3035. s:='_'+s+'__'+tostr(length(s2))+s2;
  3036. end;
  3037. end
  3038. else s:=s+'__';
  3039. s:=s+'F';
  3040. { concat modifiers }
  3041. { !!!!! }
  3042. { now we handle the parameters }
  3043. if maxparacount>0 then
  3044. begin
  3045. for i:=0 to paras.count-1 do
  3046. begin
  3047. hp:=tparavarsym(paras[i]);
  3048. { no hidden parameters form part of a C++ mangled name:
  3049. a) self is not included
  3050. b) there are no "high" or other hidden parameters
  3051. }
  3052. if vo_is_hidden_para in hp.varoptions then
  3053. continue;
  3054. s2:=getcppparaname(hp.vardef);
  3055. if hp.varspez in [vs_var,vs_out] then
  3056. s2:='R'+s2;
  3057. s:=s+s2;
  3058. end;
  3059. end
  3060. else
  3061. s:=s+'v';
  3062. cplusplusmangledname:=s;
  3063. {$else NAMEMANGLING_GCC2}
  3064. { gcc 3.x and 4.x name mangling scheme }
  3065. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3066. if procsym.owner.symtabletype=ObjectSymtable then
  3067. begin
  3068. s:='_ZN';
  3069. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3070. s:=s+tostr(length(s2))+s2;
  3071. case proctypeoption of
  3072. potype_constructor:
  3073. s:=s+'C1';
  3074. potype_destructor:
  3075. s:=s+'D1';
  3076. else
  3077. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3078. end;
  3079. s:=s+'E';
  3080. end
  3081. else
  3082. s:=procsym.realname;
  3083. { now we handle the parameters }
  3084. if maxparacount>0 then
  3085. begin
  3086. for i:=0 to paras.count-1 do
  3087. begin
  3088. hp:=tparavarsym(paras[i]);
  3089. { no hidden parameters form part of a C++ mangled name:
  3090. a) self is not included
  3091. b) there are no "high" or other hidden parameters
  3092. }
  3093. if vo_is_hidden_para in hp.varoptions then
  3094. continue;
  3095. s2:=getcppparaname(hp.vardef);
  3096. if hp.varspez in [vs_var,vs_out] then
  3097. s2:='R'+s2;
  3098. s:=s+s2;
  3099. end;
  3100. end
  3101. else
  3102. s:=s+'v';
  3103. cplusplusmangledname:=s;
  3104. {$endif NAMEMANGLING_GCC2}
  3105. end;
  3106. function tprocdef.objcmangledname : string;
  3107. var
  3108. manglednamelen: longint;
  3109. iscatmethod : boolean;
  3110. begin
  3111. if not (po_msgstr in procoptions) then
  3112. internalerror(2009030901);
  3113. { we may very well need longer strings to handle these... }
  3114. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3115. length('+"[ ]"')+length(messageinf.str^);
  3116. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3117. if (iscatmethod) then
  3118. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3119. if manglednamelen>255 then
  3120. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3121. if not(po_classmethod in procoptions) then
  3122. result:='"-['
  3123. else
  3124. result:='"+[';
  3125. { quotes are necessary because the +/- otherwise confuse the assembler
  3126. into expecting a number
  3127. }
  3128. if iscatmethod then
  3129. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3130. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3131. if iscatmethod then
  3132. result:=result+')';
  3133. result:=result+' '+messageinf.str^+']"';
  3134. end;
  3135. procedure tprocdef.setmangledname(const s : string);
  3136. begin
  3137. { This is not allowed anymore, the forward declaration
  3138. already needs to create the correct mangledname, no changes
  3139. afterwards are allowed (PFV) }
  3140. { Exception: interface definitions in mode macpas, since in that }
  3141. { case no reference to the old name can exist yet (JM) }
  3142. if assigned(_mangledname) then
  3143. if ((m_mac in current_settings.modeswitches) and
  3144. (interfacedef)) then
  3145. stringdispose(_mangledname)
  3146. else
  3147. internalerror(200411171);
  3148. {$ifdef compress}
  3149. _mangledname:=stringdup(minilzw_encode(s));
  3150. {$else}
  3151. _mangledname:=stringdup(s);
  3152. {$endif}
  3153. include(procoptions,po_has_mangledname);
  3154. end;
  3155. {***************************************************************************
  3156. TPROCVARDEF
  3157. ***************************************************************************}
  3158. constructor tprocvardef.create(level:byte);
  3159. begin
  3160. inherited create(procvardef,level);
  3161. end;
  3162. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3163. begin
  3164. inherited ppuload(procvardef,ppufile);
  3165. { load para symtable }
  3166. parast:=tparasymtable.create(self,unknown_level);
  3167. tparasymtable(parast).ppuload(ppufile);
  3168. end;
  3169. function tprocvardef.getcopy : tstoreddef;
  3170. var
  3171. i : tcallercallee;
  3172. j : longint;
  3173. begin
  3174. result:=tprocvardef.create(parast.symtablelevel);
  3175. tprocvardef(result).returndef:=returndef;
  3176. tprocvardef(result).returndefderef:=returndefderef;
  3177. tprocvardef(result).parast:=parast.getcopy;
  3178. tprocvardef(result).savesize:=savesize;
  3179. { create paralist copy }
  3180. tprocvardef(result).paras:=tparalist.create(false);
  3181. tprocvardef(result).paras.count:=paras.count;
  3182. for j:=0 to paras.count-1 do
  3183. tprocvardef(result).paras[j]:=paras[j];
  3184. tprocvardef(result).proctypeoption:=proctypeoption;
  3185. tprocvardef(result).proccalloption:=proccalloption;
  3186. tprocvardef(result).procoptions:=procoptions;
  3187. tprocvardef(result).requiredargarea:=requiredargarea;
  3188. tprocvardef(result).maxparacount:=maxparacount;
  3189. tprocvardef(result).minparacount:=minparacount;
  3190. for i:=low(tcallercallee) to high(tcallercallee) do
  3191. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3192. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3193. {$ifdef m68k}
  3194. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3195. {$endif}
  3196. end;
  3197. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3198. begin
  3199. inherited ppuwrite(ppufile);
  3200. { Write this entry }
  3201. ppufile.writeentry(ibprocvardef);
  3202. { Save the para symtable, this is taken from the interface }
  3203. tparasymtable(parast).ppuwrite(ppufile);
  3204. end;
  3205. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3206. begin
  3207. case t of
  3208. gs_para :
  3209. GetSymtable:=parast;
  3210. else
  3211. GetSymtable:=nil;
  3212. end;
  3213. end;
  3214. function tprocvardef.size : aint;
  3215. begin
  3216. if (po_methodpointer in procoptions) and
  3217. not(po_addressonly in procoptions) then
  3218. size:=2*sizeof(pint)
  3219. else
  3220. size:=sizeof(pint);
  3221. end;
  3222. function tprocvardef.is_methodpointer:boolean;
  3223. begin
  3224. result:=(po_methodpointer in procoptions);
  3225. end;
  3226. function tprocvardef.is_addressonly:boolean;
  3227. begin
  3228. result:=not(po_methodpointer in procoptions) or
  3229. (po_addressonly in procoptions);
  3230. end;
  3231. function tprocvardef.getmangledparaname:string;
  3232. begin
  3233. result:='procvar';
  3234. end;
  3235. function tprocvardef.is_publishable : boolean;
  3236. begin
  3237. is_publishable:=(po_methodpointer in procoptions);
  3238. end;
  3239. function tprocvardef.GetTypeName : string;
  3240. var
  3241. s: string;
  3242. showhidden : boolean;
  3243. begin
  3244. {$ifdef EXTDEBUG}
  3245. showhidden:=true;
  3246. {$else EXTDEBUG}
  3247. showhidden:=false;
  3248. {$endif EXTDEBUG}
  3249. s:='<';
  3250. if po_classmethod in procoptions then
  3251. s := s+'class method type of'
  3252. else
  3253. if po_addressonly in procoptions then
  3254. s := s+'address of'
  3255. else
  3256. s := s+'procedure variable type of';
  3257. if po_local in procoptions then
  3258. s := s+' local';
  3259. if assigned(returndef) and
  3260. (returndef<>voidtype) then
  3261. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3262. else
  3263. s:=s+' procedure'+typename_paras(showhidden);
  3264. if po_methodpointer in procoptions then
  3265. s := s+' of object';
  3266. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3267. end;
  3268. {***************************************************************************
  3269. TOBJECTDEF
  3270. ***************************************************************************}
  3271. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3272. begin
  3273. inherited create(objectdef);
  3274. objecttype:=ot;
  3275. objectoptions:=[];
  3276. childof:=nil;
  3277. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3278. { create space for vmt !! }
  3279. vmtentries:=TFPList.Create;
  3280. vmt_offset:=0;
  3281. set_parent(c);
  3282. objname:=stringdup(upper(n));
  3283. objrealname:=stringdup(n);
  3284. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3285. prepareguid;
  3286. { setup implemented interfaces }
  3287. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3288. ImplementedInterfaces:=TFPObjectList.Create(true)
  3289. else
  3290. ImplementedInterfaces:=nil;
  3291. writing_class_record_dbginfo:=false;
  3292. end;
  3293. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3294. var
  3295. i,
  3296. implintfcount : longint;
  3297. d : tderef;
  3298. ImplIntf : TImplementedInterface;
  3299. vmtentry : pvmtentry;
  3300. begin
  3301. inherited ppuload(objectdef,ppufile);
  3302. objecttype:=tobjecttyp(ppufile.getbyte);
  3303. objrealname:=stringdup(ppufile.getstring);
  3304. objname:=stringdup(upper(objrealname^));
  3305. objextname:=stringdup(ppufile.getstring);
  3306. { only used for external Objective-C classes/protocols }
  3307. if (objextname^='') then
  3308. stringdispose(objextname);
  3309. import_lib:=stringdup(ppufile.getstring);
  3310. { only used for external C++ classes }
  3311. if (import_lib^='') then
  3312. stringdispose(import_lib);
  3313. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3314. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3315. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3316. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3317. vmt_offset:=ppufile.getlongint;
  3318. ppufile.getderef(childofderef);
  3319. ppufile.getsmallset(objectoptions);
  3320. { load guid }
  3321. iidstr:=nil;
  3322. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3323. begin
  3324. new(iidguid);
  3325. ppufile.getguid(iidguid^);
  3326. iidstr:=stringdup(ppufile.getstring);
  3327. end;
  3328. vmtentries:=TFPList.Create;
  3329. vmtentries.count:=ppufile.getlongint;
  3330. for i:=0 to vmtentries.count-1 do
  3331. begin
  3332. ppufile.getderef(d);
  3333. new(vmtentry);
  3334. vmtentry^.procdef:=nil;
  3335. vmtentry^.procdefderef:=d;
  3336. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3337. vmtentries[i]:=vmtentry;
  3338. end;
  3339. { load implemented interfaces }
  3340. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3341. begin
  3342. ImplementedInterfaces:=TFPObjectList.Create(true);
  3343. implintfcount:=ppufile.getlongint;
  3344. for i:=0 to implintfcount-1 do
  3345. begin
  3346. ppufile.getderef(d);
  3347. ImplIntf:=TImplementedInterface.Create_deref(d);
  3348. ImplIntf.IOffset:=ppufile.getlongint;
  3349. ImplementedInterfaces.Add(ImplIntf);
  3350. end;
  3351. end
  3352. else
  3353. ImplementedInterfaces:=nil;
  3354. if df_copied_def in defoptions then
  3355. ppufile.getderef(cloneddefderef)
  3356. else
  3357. tObjectSymtable(symtable).ppuload(ppufile);
  3358. { handles the predefined class tobject }
  3359. { the last TOBJECT which is loaded gets }
  3360. { it ! }
  3361. if (childof=nil) and
  3362. (objecttype=odt_class) and
  3363. (objname^='TOBJECT') then
  3364. class_tobject:=self;
  3365. if (childof=nil) and
  3366. (objecttype=odt_interfacecom) and
  3367. (objname^='IUNKNOWN') then
  3368. interface_iunknown:=self;
  3369. if (childof=nil) and
  3370. (objecttype=odt_objcclass) and
  3371. (objname^='PROTOCOL') then
  3372. objc_protocoltype:=self;
  3373. writing_class_record_dbginfo:=false;
  3374. end;
  3375. destructor tobjectdef.destroy;
  3376. begin
  3377. if assigned(symtable) then
  3378. begin
  3379. symtable.free;
  3380. symtable:=nil;
  3381. end;
  3382. stringdispose(objname);
  3383. stringdispose(objrealname);
  3384. stringdispose(objextname);
  3385. stringdispose(import_lib);
  3386. stringdispose(iidstr);
  3387. if assigned(ImplementedInterfaces) then
  3388. begin
  3389. ImplementedInterfaces.free;
  3390. ImplementedInterfaces:=nil;
  3391. end;
  3392. if assigned(iidguid) then
  3393. begin
  3394. dispose(iidguid);
  3395. iidguid:=nil;
  3396. end;
  3397. if assigned(vmtentries) then
  3398. begin
  3399. resetvmtentries;
  3400. vmtentries.free;
  3401. vmtentries:=nil;
  3402. end;
  3403. if assigned(vmcallstaticinfo) then
  3404. begin
  3405. freemem(vmcallstaticinfo);
  3406. vmcallstaticinfo:=nil;
  3407. end;
  3408. inherited destroy;
  3409. end;
  3410. function tobjectdef.getcopy : tstoreddef;
  3411. var
  3412. i : longint;
  3413. begin
  3414. result:=tobjectdef.create(objecttype,objname^,childof);
  3415. { the constructor allocates a symtable which we release to avoid memory leaks }
  3416. tobjectdef(result).symtable.free;
  3417. tobjectdef(result).symtable:=symtable.getcopy;
  3418. if assigned(objname) then
  3419. tobjectdef(result).objname:=stringdup(objname^);
  3420. if assigned(objrealname) then
  3421. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3422. if assigned(objextname) then
  3423. tobjectdef(result).objextname:=stringdup(objextname^);
  3424. if assigned(import_lib) then
  3425. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3426. tobjectdef(result).objectoptions:=objectoptions;
  3427. include(tobjectdef(result).defoptions,df_copied_def);
  3428. tobjectdef(result).vmt_offset:=vmt_offset;
  3429. if assigned(iidguid) then
  3430. begin
  3431. new(tobjectdef(result).iidguid);
  3432. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3433. end;
  3434. if assigned(iidstr) then
  3435. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3436. if assigned(ImplementedInterfaces) then
  3437. begin
  3438. for i:=0 to ImplementedInterfaces.count-1 do
  3439. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3440. end;
  3441. if assigned(vmtentries) then
  3442. begin
  3443. tobjectdef(result).vmtentries:=TFPList.Create;
  3444. tobjectdef(result).copyvmtentries(self);
  3445. end;
  3446. end;
  3447. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3448. var
  3449. i : longint;
  3450. vmtentry : pvmtentry;
  3451. ImplIntf : TImplementedInterface;
  3452. old_do_indirect_crc: boolean;
  3453. begin
  3454. { if class1 in unit A changes, and class2 in unit B inherits from it
  3455. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3456. However, if there is also a class3 in unit C that only depends on
  3457. unit B, then unit C will not be recompiled because nothing changed
  3458. to the interface of unit B. Nevertheless, unit C can indirectly
  3459. depend on unit A via derefs, and these must be updated -> the
  3460. indirect crc keeps track of such changes. }
  3461. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3462. ppufile.do_indirect_crc:=true;
  3463. inherited ppuwrite(ppufile);
  3464. ppufile.putbyte(byte(objecttype));
  3465. ppufile.putstring(objrealname^);
  3466. if assigned(objextname) then
  3467. ppufile.putstring(objextname^)
  3468. else
  3469. ppufile.putstring('');
  3470. if assigned(import_lib) then
  3471. ppufile.putstring(import_lib^)
  3472. else
  3473. ppufile.putstring('');
  3474. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3475. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3476. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3477. ppufile.putlongint(vmt_offset);
  3478. ppufile.putderef(childofderef);
  3479. ppufile.putsmallset(objectoptions);
  3480. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3481. begin
  3482. ppufile.putguid(iidguid^);
  3483. ppufile.putstring(iidstr^);
  3484. end;
  3485. ppufile.putlongint(vmtentries.count);
  3486. for i:=0 to vmtentries.count-1 do
  3487. begin
  3488. vmtentry:=pvmtentry(vmtentries[i]);
  3489. ppufile.putderef(vmtentry^.procdefderef);
  3490. ppufile.putbyte(byte(vmtentry^.visibility));
  3491. end;
  3492. if assigned(ImplementedInterfaces) then
  3493. begin
  3494. ppufile.putlongint(ImplementedInterfaces.Count);
  3495. for i:=0 to ImplementedInterfaces.Count-1 do
  3496. begin
  3497. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3498. ppufile.putderef(ImplIntf.intfdefderef);
  3499. ppufile.putlongint(ImplIntf.Ioffset);
  3500. end;
  3501. end;
  3502. if df_copied_def in defoptions then
  3503. ppufile.putderef(cloneddefderef);
  3504. ppufile.writeentry(ibobjectdef);
  3505. if not(df_copied_def in defoptions) then
  3506. tObjectSymtable(symtable).ppuwrite(ppufile);
  3507. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3508. end;
  3509. function tobjectdef.GetTypeName:string;
  3510. begin
  3511. { in this case we will go in endless recursion, because then }
  3512. { there is no tsym associated yet with the def. It can occur }
  3513. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3514. { instead of the actual type name }
  3515. if not assigned(typesym) then
  3516. result:='<Currently Parsed Class>'
  3517. else
  3518. result:=typename;
  3519. end;
  3520. procedure tobjectdef.buildderef;
  3521. var
  3522. i : longint;
  3523. vmtentry : pvmtentry;
  3524. begin
  3525. inherited buildderef;
  3526. childofderef.build(childof);
  3527. if df_copied_def in defoptions then
  3528. cloneddefderef.build(symtable.defowner)
  3529. else
  3530. tstoredsymtable(symtable).buildderef;
  3531. for i:=0 to vmtentries.count-1 do
  3532. begin
  3533. vmtentry:=pvmtentry(vmtentries[i]);
  3534. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3535. end;
  3536. if assigned(ImplementedInterfaces) then
  3537. begin
  3538. for i:=0 to ImplementedInterfaces.count-1 do
  3539. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3540. end;
  3541. end;
  3542. procedure tobjectdef.deref;
  3543. var
  3544. i : longint;
  3545. vmtentry : pvmtentry;
  3546. begin
  3547. inherited deref;
  3548. childof:=tobjectdef(childofderef.resolve);
  3549. if df_copied_def in defoptions then
  3550. begin
  3551. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3552. symtable:=cloneddef.symtable.getcopy;
  3553. end
  3554. else
  3555. tstoredsymtable(symtable).deref;
  3556. for i:=0 to vmtentries.count-1 do
  3557. begin
  3558. vmtentry:=pvmtentry(vmtentries[i]);
  3559. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3560. end;
  3561. if assigned(ImplementedInterfaces) then
  3562. begin
  3563. for i:=0 to ImplementedInterfaces.count-1 do
  3564. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3565. end;
  3566. end;
  3567. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3568. var
  3569. pd: tprocdef absolute def;
  3570. st: tsymtable;
  3571. psym: tsym;
  3572. nname: TIDString;
  3573. begin
  3574. if (tdef(def).typ<>procdef) then
  3575. exit;
  3576. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3577. owner = symtable in which objcclassdef is defined
  3578. }
  3579. st:=pd.owner.defowner.owner;
  3580. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3581. { check for an existing procsym with our special name }
  3582. psym:=tsym(st.find(nname));
  3583. if not assigned(psym) then
  3584. begin
  3585. psym:=tprocsym.create(nname);
  3586. { avoid warning about this symbol being unused }
  3587. psym.IncRefCount;
  3588. st.insert(psym,true);
  3589. end
  3590. else if (psym.typ<>procsym) then
  3591. internalerror(2009111501);
  3592. { add ourselves to this special procsym }
  3593. tprocsym(psym).procdeflist.add(def);
  3594. end;
  3595. procedure tobjectdef.buildderefimpl;
  3596. begin
  3597. inherited buildderefimpl;
  3598. if not (df_copied_def in defoptions) then
  3599. tstoredsymtable(symtable).buildderefimpl;
  3600. end;
  3601. procedure tobjectdef.derefimpl;
  3602. begin
  3603. inherited derefimpl;
  3604. if not (df_copied_def in defoptions) then
  3605. tstoredsymtable(symtable).derefimpl;
  3606. { the procdefs are not owned by the class helper procsyms, so they
  3607. are not stored/restored either -> re-add them here }
  3608. if (objecttype=odt_objcclass) or
  3609. (oo_is_classhelper in objectoptions) then
  3610. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3611. end;
  3612. procedure tobjectdef.resetvmtentries;
  3613. var
  3614. i : longint;
  3615. begin
  3616. for i:=0 to vmtentries.Count-1 do
  3617. Dispose(pvmtentry(vmtentries[i]));
  3618. vmtentries.clear;
  3619. end;
  3620. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3621. var
  3622. i : longint;
  3623. vmtentry : pvmtentry;
  3624. begin
  3625. resetvmtentries;
  3626. vmtentries.count:=objdef.vmtentries.count;
  3627. for i:=0 to objdef.vmtentries.count-1 do
  3628. begin
  3629. new(vmtentry);
  3630. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3631. vmtentries[i]:=vmtentry;
  3632. end;
  3633. end;
  3634. function tobjectdef.getparentdef:tdef;
  3635. begin
  3636. { TODO: Remove getparentdef hack}
  3637. { With 2 forward declared classes with the child class before the
  3638. parent class the child class is written earlier to the ppu. Leaving it
  3639. possible to have a reference to the parent class for property overriding,
  3640. but the parent class still has the childof not resolved yet (PFV) }
  3641. if childof=nil then
  3642. childof:=tobjectdef(childofderef.resolve);
  3643. result:=childof;
  3644. end;
  3645. procedure tobjectdef.prepareguid;
  3646. begin
  3647. { set up guid }
  3648. if not assigned(iidguid) then
  3649. begin
  3650. new(iidguid);
  3651. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3652. end;
  3653. { setup iidstring }
  3654. if not assigned(iidstr) then
  3655. iidstr:=stringdup(''); { default is empty string }
  3656. end;
  3657. procedure tobjectdef.set_parent( c : tobjectdef);
  3658. begin
  3659. if assigned(childof) then
  3660. exit;
  3661. childof:=c;
  3662. if not assigned(c) then
  3663. exit;
  3664. { inherit options and status }
  3665. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3666. { add the data of the anchestor class/object }
  3667. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3668. begin
  3669. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3670. { inherit recordalignment }
  3671. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3672. if (oo_has_vmt in objectoptions) and
  3673. (oo_has_vmt in c.objectoptions) then
  3674. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3675. { if parent has a vmt field then the offset is the same for the child PM }
  3676. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3677. begin
  3678. vmt_offset:=c.vmt_offset;
  3679. include(objectoptions,oo_has_vmt);
  3680. end;
  3681. end;
  3682. end;
  3683. procedure tobjectdef.insertvmt;
  3684. var
  3685. vs: tfieldvarsym;
  3686. begin
  3687. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3688. exit;
  3689. if (oo_has_vmt in objectoptions) then
  3690. internalerror(12345)
  3691. else
  3692. begin
  3693. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3694. tObjectSymtable(symtable).fieldalignment);
  3695. if (tf_requires_proper_alignment in target_info.flags) then
  3696. begin
  3697. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3698. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3699. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3700. end;
  3701. vmt_offset:=tObjectSymtable(symtable).datasize;
  3702. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3703. hidesym(vs);
  3704. tObjectSymtable(symtable).insert(vs);
  3705. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3706. include(objectoptions,oo_has_vmt);
  3707. end;
  3708. end;
  3709. procedure tobjectdef.check_forwards;
  3710. begin
  3711. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3712. tstoredsymtable(symtable).check_forwards;
  3713. if (oo_is_forward in objectoptions) then
  3714. begin
  3715. { ok, in future, the forward can be resolved }
  3716. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3717. exclude(objectoptions,oo_is_forward);
  3718. end;
  3719. end;
  3720. { true if prot implements d (or if they are equal) }
  3721. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3722. var
  3723. i : longint;
  3724. begin
  3725. { objcprotocols have multiple inheritance, all protocols from which
  3726. the current protocol inherits are stored in implementedinterfaces }
  3727. result:=prot=d;
  3728. if result then
  3729. exit;
  3730. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3731. begin
  3732. result:=is_related_protocol(tobjectdef(prot.ImplementedInterfaces[i]),d);
  3733. if result then
  3734. exit;
  3735. end;
  3736. end;
  3737. { true, if self inherits from d (or if they are equal) }
  3738. function tobjectdef.is_related(d : tdef) : boolean;
  3739. var
  3740. hp : tobjectdef;
  3741. begin
  3742. if (objecttype=odt_objcprotocol) then
  3743. begin
  3744. is_related:=is_related_protocol(self,d);
  3745. exit
  3746. end;
  3747. hp:=self;
  3748. while assigned(hp) do
  3749. begin
  3750. if hp=d then
  3751. begin
  3752. is_related:=true;
  3753. exit;
  3754. end;
  3755. hp:=hp.childof;
  3756. end;
  3757. is_related:=false;
  3758. end;
  3759. function tobjectdef.FindDestructor : tprocdef;
  3760. var
  3761. objdef : tobjectdef;
  3762. i : longint;
  3763. sym : tsym;
  3764. pd : tprocdef;
  3765. begin
  3766. result:=nil;
  3767. objdef:=self;
  3768. while assigned(objdef) do
  3769. begin
  3770. for i:=0 to objdef.symtable.SymList.Count-1 do
  3771. begin
  3772. sym:=TSym(objdef.symtable.SymList[i]);
  3773. if sym.typ=procsym then
  3774. begin
  3775. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3776. if assigned(pd) then
  3777. begin
  3778. result:=pd;
  3779. exit;
  3780. end;
  3781. end;
  3782. end;
  3783. objdef:=objdef.childof;
  3784. end;
  3785. end;
  3786. function tobjectdef.implements_any_interfaces: boolean;
  3787. begin
  3788. result := (ImplementedInterfaces.Count > 0) or
  3789. (assigned(childof) and childof.implements_any_interfaces);
  3790. end;
  3791. function tobjectdef.size : aint;
  3792. begin
  3793. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3794. result:=sizeof(pint)
  3795. else
  3796. result:=tObjectSymtable(symtable).datasize;
  3797. end;
  3798. function tobjectdef.alignment:shortint;
  3799. begin
  3800. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3801. alignment:=sizeof(pint)
  3802. else
  3803. alignment:=tObjectSymtable(symtable).recordalignment;
  3804. end;
  3805. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3806. begin
  3807. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3808. case objecttype of
  3809. odt_class:
  3810. { the +2*sizeof(pint) is size and -size }
  3811. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3812. odt_objcclass,
  3813. odt_objcprotocol:
  3814. vmtmethodoffset:=0;
  3815. odt_interfacecom,odt_interfacecorba:
  3816. vmtmethodoffset:=index*sizeof(pint);
  3817. else
  3818. {$ifdef WITHDMT}
  3819. vmtmethodoffset:=(index+4)*sizeof(pint);
  3820. {$else WITHDMT}
  3821. vmtmethodoffset:=(index+3)*sizeof(pint);
  3822. {$endif WITHDMT}
  3823. end;
  3824. end;
  3825. function tobjectdef.vmt_mangledname : string;
  3826. begin
  3827. if not(oo_has_vmt in objectoptions) then
  3828. Message1(parser_n_object_has_no_vmt,objrealname^);
  3829. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3830. end;
  3831. function tobjectdef.needs_inittable : boolean;
  3832. begin
  3833. case objecttype of
  3834. odt_dispinterface,
  3835. odt_class :
  3836. needs_inittable:=false;
  3837. odt_interfacecom:
  3838. needs_inittable:=true;
  3839. odt_interfacecorba:
  3840. needs_inittable:=is_related(interface_iunknown);
  3841. odt_object:
  3842. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3843. odt_cppclass,
  3844. odt_objcclass,
  3845. odt_objcprotocol:
  3846. needs_inittable:=false;
  3847. else
  3848. internalerror(200108267);
  3849. end;
  3850. end;
  3851. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3852. begin
  3853. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3854. result:=inherited rtti_mangledname(rt)
  3855. else
  3856. begin
  3857. if not(target_info.system in systems_objc_nfabi) then
  3858. begin
  3859. result:=target_asm.labelprefix;
  3860. case objecttype of
  3861. odt_objcclass:
  3862. begin
  3863. case rt of
  3864. objcclassrtti:
  3865. if not(oo_is_classhelper in objectoptions) then
  3866. result:=result+'_OBJC_CLASS_'
  3867. else
  3868. result:=result+'_OBJC_CATEGORY_';
  3869. objcmetartti:
  3870. if not(oo_is_classhelper in objectoptions) then
  3871. result:=result+'_OBJC_METACLASS_'
  3872. else
  3873. internalerror(2009111511);
  3874. else
  3875. internalerror(2009092302);
  3876. end;
  3877. end;
  3878. odt_objcprotocol:
  3879. result:=result+'_OBJC_PROTOCOL_';
  3880. end;
  3881. end
  3882. else
  3883. begin
  3884. case objecttype of
  3885. odt_objcclass:
  3886. begin
  3887. if (oo_is_classhelper in objectoptions) and
  3888. (rt<>objcclassrtti) then
  3889. internalerror(2009111512);
  3890. case rt of
  3891. objcclassrtti:
  3892. if not(oo_is_classhelper in objectoptions) then
  3893. result:='_OBJC_CLASS_$_'
  3894. else
  3895. result:='_OBJC_$_CATEGORY_';
  3896. objcmetartti:
  3897. result:='_OBJC_METACLASS_$_';
  3898. objcclassrortti:
  3899. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  3900. objcmetarortti:
  3901. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  3902. else
  3903. internalerror(2009092303);
  3904. end;
  3905. end;
  3906. odt_objcprotocol:
  3907. begin
  3908. result:=lower(target_asm.labelprefix);
  3909. case rt of
  3910. objcclassrtti:
  3911. result:=result+'_OBJC_PROTOCOL_$_';
  3912. objcmetartti:
  3913. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  3914. else
  3915. internalerror(2009092501);
  3916. end;
  3917. end;
  3918. end;
  3919. end;
  3920. result:=result+objextname^;
  3921. end;
  3922. end;
  3923. function tobjectdef.members_need_inittable : boolean;
  3924. begin
  3925. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3926. end;
  3927. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3928. var
  3929. ImplIntf : TImplementedInterface;
  3930. i : longint;
  3931. begin
  3932. result:=nil;
  3933. if not assigned(ImplementedInterfaces) then
  3934. exit;
  3935. for i:=0 to ImplementedInterfaces.Count-1 do
  3936. begin
  3937. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3938. if ImplIntf.intfdef=aintfdef then
  3939. begin
  3940. result:=ImplIntf;
  3941. exit;
  3942. end;
  3943. end;
  3944. end;
  3945. function tobjectdef.is_publishable : boolean;
  3946. begin
  3947. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3948. end;
  3949. procedure tobjectdef.reset;
  3950. begin
  3951. inherited reset;
  3952. created_in_current_module:=false;
  3953. maybe_created_in_current_module:=false;
  3954. classref_created_in_current_module:=false;
  3955. end;
  3956. function tobjectdef.search_enumerator_get: tprocdef;
  3957. var
  3958. objdef : tobjectdef;
  3959. sym : tsym;
  3960. i : integer;
  3961. pd : tprocdef;
  3962. hashedid : THashedIDString;
  3963. begin
  3964. result:=nil;
  3965. objdef:=self;
  3966. hashedid.id:='GETENUMERATOR';
  3967. while assigned(objdef) do
  3968. begin
  3969. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3970. if assigned(sym) and (sym.typ=procsym) then
  3971. begin
  3972. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3973. begin
  3974. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3975. if (pd.proctypeoption = potype_function) and
  3976. is_class_or_interface_or_object(pd.returndef) and
  3977. (pd.visibility >= vis_public) then
  3978. begin
  3979. result:=pd;
  3980. exit;
  3981. end;
  3982. end;
  3983. end;
  3984. objdef:=objdef.childof;
  3985. end;
  3986. end;
  3987. function tobjectdef.search_enumerator_move: tprocdef;
  3988. var
  3989. objdef : tobjectdef;
  3990. sym : tsym;
  3991. i : integer;
  3992. pd : tprocdef;
  3993. hashedid : THashedIDString;
  3994. begin
  3995. result:=nil;
  3996. objdef:=self;
  3997. // first search for po_enumerator_movenext method modifier
  3998. // then search for public function MoveNext: Boolean
  3999. hashedid.id:='MOVENEXT';
  4000. while assigned(objdef) do
  4001. begin
  4002. for i:=0 to objdef.symtable.SymList.Count-1 do
  4003. begin
  4004. sym:=TSym(objdef.symtable.SymList[i]);
  4005. if (sym.typ=procsym) then
  4006. begin
  4007. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4008. if assigned(pd) then
  4009. begin
  4010. result:=pd;
  4011. exit;
  4012. end;
  4013. end;
  4014. end;
  4015. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4016. if assigned(sym) and (sym.typ=procsym) then
  4017. begin
  4018. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4019. begin
  4020. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4021. if (pd.proctypeoption = potype_function) and
  4022. is_boolean(pd.returndef) and
  4023. (pd.minparacount = 0) and
  4024. (pd.visibility >= vis_public) then
  4025. begin
  4026. result:=pd;
  4027. exit;
  4028. end;
  4029. end;
  4030. end;
  4031. objdef:=objdef.childof;
  4032. end;
  4033. end;
  4034. function tobjectdef.search_enumerator_current: tsym;
  4035. var
  4036. objdef : tobjectdef;
  4037. sym: tsym;
  4038. i: integer;
  4039. hashedid : THashedIDString;
  4040. begin
  4041. result:=nil;
  4042. objdef:=self;
  4043. hashedid.id:='CURRENT';
  4044. // first search for ppo_enumerator_current property modifier
  4045. // then search for public property Current
  4046. while assigned(objdef) do
  4047. begin
  4048. for i:=0 to objdef.symtable.SymList.Count-1 do
  4049. begin
  4050. sym:=TSym(objdef.symtable.SymList[i]);
  4051. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4052. begin
  4053. result:=sym;
  4054. exit;
  4055. end;
  4056. end;
  4057. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4058. if assigned(sym) and (sym.typ=propertysym) and
  4059. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4060. begin
  4061. result:=sym;
  4062. exit;
  4063. end;
  4064. objdef:=objdef.childof;
  4065. end;
  4066. end;
  4067. procedure tobjectdef.register_created_classref_type;
  4068. begin
  4069. if not classref_created_in_current_module then
  4070. begin
  4071. classref_created_in_current_module:=true;
  4072. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4073. end;
  4074. end;
  4075. procedure tobjectdef.register_created_object_type;
  4076. begin
  4077. if not created_in_current_module then
  4078. begin
  4079. created_in_current_module:=true;
  4080. current_module.wpoinfo.addcreatedobjtype(self);
  4081. end;
  4082. end;
  4083. procedure tobjectdef.register_maybe_created_object_type;
  4084. begin
  4085. { if we know it has been created for sure, no need
  4086. to also record that it maybe can be created in
  4087. this module
  4088. }
  4089. if not (created_in_current_module) and
  4090. not (maybe_created_in_current_module) then
  4091. begin
  4092. maybe_created_in_current_module:=true;
  4093. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4094. end;
  4095. end;
  4096. procedure tobjectdef.register_vmt_call(index: longint);
  4097. begin
  4098. if (is_object(self) or is_class(self)) then
  4099. current_module.wpoinfo.addcalledvmtentry(self,index);
  4100. end;
  4101. procedure make_procdef_external(data: tobject; arg: pointer);
  4102. var
  4103. def: tdef absolute data;
  4104. begin
  4105. if (def.typ = procdef) then
  4106. begin
  4107. include(tprocdef(def).procoptions,po_external);
  4108. tprocdef(def).forwarddef:=false;
  4109. end;
  4110. end;
  4111. procedure tobjectdef.make_all_methods_external;
  4112. begin
  4113. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4114. end;
  4115. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4116. var
  4117. def: tdef absolute data;
  4118. pd: tprocdef absolute data;
  4119. i,
  4120. paracount: longint;
  4121. begin
  4122. if (def.typ=procdef) then
  4123. begin
  4124. { add all messages also under a dummy name to the symtable in
  4125. which the objcclass/protocol/category is declared, so they can
  4126. be called via id.<name>
  4127. }
  4128. create_class_helper_for_procdef(pd,nil);
  4129. { we have to wait until now to set the mangled name because it
  4130. depends on the (possibly external) class name, which is defined
  4131. at the very end. }
  4132. if not(po_msgstr in pd.procoptions) then
  4133. begin
  4134. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4135. { recover to avoid internalerror later on }
  4136. include(pd.procoptions,po_msgstr);
  4137. pd.messageinf.str:=stringdup('MissingDeclaration');
  4138. end;
  4139. { Mangled name is already set in case this is a copy of
  4140. another type. }
  4141. if not(po_has_mangledname in pd.procoptions) then
  4142. begin
  4143. { check whether the number of formal parameters is correct }
  4144. paracount:=0;
  4145. for i:=1 to length(pd.messageinf.str^) do
  4146. if pd.messageinf.str^[i]=':' then
  4147. inc(paracount);
  4148. for i:=0 to pd.paras.count-1 do
  4149. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4150. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4151. dec(paracount);
  4152. if (paracount<>0) then
  4153. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4154. pd.setmangledname(pd.objcmangledname);
  4155. end
  4156. else
  4157. { all checks already done }
  4158. exit;
  4159. if not(oo_is_external in pd._class.objectoptions) then
  4160. begin
  4161. if (po_varargs in pd.procoptions) then
  4162. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4163. else
  4164. begin
  4165. { check for "array of const" parameters }
  4166. for i:=0 to pd.parast.symlist.count-1 do
  4167. begin
  4168. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4169. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4170. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4171. end;
  4172. end;
  4173. end;
  4174. end;
  4175. end;
  4176. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4177. var
  4178. sym: tsym absolute data;
  4179. begin
  4180. if (sym.typ=fieldvarsym) and
  4181. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4182. sym.IncRefCount;
  4183. end;
  4184. procedure tobjectdef.finish_objc_data;
  4185. begin
  4186. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4187. if (oo_is_external in objectoptions) then
  4188. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4189. end;
  4190. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4191. var
  4192. def: tdef absolute data;
  4193. pd: tprocdef absolute data;
  4194. begin
  4195. if (def.typ=procdef) then
  4196. begin
  4197. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4198. if (oo_is_external in pd._class.objectoptions) then
  4199. begin
  4200. { copied from psub.read_proc }
  4201. if assigned(pd._class.import_lib) then
  4202. current_module.AddExternalImport(pd._class.import_lib^,pd.mangledname,0,false,false)
  4203. else
  4204. begin
  4205. { add import name to external list for DLL scanning }
  4206. if tf_has_dllscanner in target_info.flags then
  4207. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4208. end;
  4209. end;
  4210. end;
  4211. end;
  4212. procedure tobjectdef.finish_cpp_data;
  4213. begin
  4214. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4215. end;
  4216. function tobjectdef.RttiName: string;
  4217. var
  4218. tmp: tobjectdef;
  4219. begin
  4220. Result:=objrealname^;
  4221. tmp:=self;
  4222. repeat
  4223. if tmp.owner.symtabletype=ObjectSymtable then
  4224. tmp:=tobjectdef(tmp.owner.defowner)
  4225. else
  4226. break;
  4227. Result:=tmp.objrealname^+'.'+Result;
  4228. until tmp=nil;
  4229. end;
  4230. {****************************************************************************
  4231. TImplementedInterface
  4232. ****************************************************************************}
  4233. constructor TImplementedInterface.create(aintf: tobjectdef);
  4234. begin
  4235. inherited create;
  4236. intfdef:=aintf;
  4237. IOffset:=-1;
  4238. IType:=etStandard;
  4239. NameMappings:=nil;
  4240. procdefs:=nil;
  4241. end;
  4242. constructor TImplementedInterface.create_deref(d:tderef);
  4243. begin
  4244. inherited create;
  4245. intfdef:=nil;
  4246. intfdefderef:=d;
  4247. IOffset:=-1;
  4248. IType:=etStandard;
  4249. NameMappings:=nil;
  4250. procdefs:=nil;
  4251. end;
  4252. destructor TImplementedInterface.destroy;
  4253. var
  4254. i : longint;
  4255. mappedname : pshortstring;
  4256. begin
  4257. if assigned(NameMappings) then
  4258. begin
  4259. for i:=0 to NameMappings.Count-1 do
  4260. begin
  4261. mappedname:=pshortstring(NameMappings[i]);
  4262. stringdispose(mappedname);
  4263. end;
  4264. NameMappings.free;
  4265. NameMappings:=nil;
  4266. end;
  4267. if assigned(procdefs) then
  4268. begin
  4269. procdefs.free;
  4270. procdefs:=nil;
  4271. end;
  4272. inherited destroy;
  4273. end;
  4274. procedure TImplementedInterface.buildderef;
  4275. begin
  4276. intfdefderef.build(intfdef);
  4277. end;
  4278. procedure TImplementedInterface.deref;
  4279. begin
  4280. intfdef:=tobjectdef(intfdefderef.resolve);
  4281. end;
  4282. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4283. begin
  4284. if not assigned(NameMappings) then
  4285. NameMappings:=TFPHashList.Create;
  4286. NameMappings.Add(origname,stringdup(newname));
  4287. end;
  4288. function TImplementedInterface.GetMapping(const origname: string):string;
  4289. var
  4290. mappedname : pshortstring;
  4291. begin
  4292. result:='';
  4293. if not assigned(NameMappings) then
  4294. exit;
  4295. mappedname:=PShortstring(NameMappings.Find(origname));
  4296. if assigned(mappedname) then
  4297. result:=mappedname^;
  4298. end;
  4299. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4300. begin
  4301. if not assigned(procdefs) then
  4302. procdefs:=TFPObjectList.Create(false);
  4303. { duplicate entries must be stored, because multiple }
  4304. { interfaces can declare methods with the same name }
  4305. { and all of these get their own VMT entry }
  4306. procdefs.Add(pd);
  4307. end;
  4308. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4309. var
  4310. i : longint;
  4311. begin
  4312. result:=false;
  4313. { interfaces being implemented through delegation are not mergable (FK) }
  4314. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4315. exit;
  4316. weight:=0;
  4317. { empty interface is mergeable }
  4318. if ProcDefs.Count=0 then
  4319. begin
  4320. result:=true;
  4321. exit;
  4322. end;
  4323. { The interface to merge must at least the number of
  4324. procedures of this interface }
  4325. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4326. exit;
  4327. for i:=0 to ProcDefs.Count-1 do
  4328. begin
  4329. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4330. exit;
  4331. end;
  4332. weight:=ProcDefs.Count;
  4333. result:=true;
  4334. end;
  4335. function TImplementedInterface.getcopy:TImplementedInterface;
  4336. begin
  4337. Result:=TImplementedInterface.Create(nil);
  4338. {$warning: this is completely wrong on so many levels...}
  4339. { 1) the procdefs list will be freed once for each copy
  4340. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4341. 3) idem for the name mappings
  4342. }
  4343. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4344. end;
  4345. {****************************************************************************
  4346. TFORWARDDEF
  4347. ****************************************************************************}
  4348. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4349. begin
  4350. inherited create(forwarddef);
  4351. tosymname:=stringdup(s);
  4352. forwardpos:=pos;
  4353. end;
  4354. function tforwarddef.GetTypeName:string;
  4355. begin
  4356. GetTypeName:='unresolved forward to '+tosymname^;
  4357. end;
  4358. destructor tforwarddef.destroy;
  4359. begin
  4360. stringdispose(tosymname);
  4361. inherited destroy;
  4362. end;
  4363. {****************************************************************************
  4364. TUNDEFINEDDEF
  4365. ****************************************************************************}
  4366. constructor tundefineddef.create;
  4367. begin
  4368. inherited create(undefineddef);
  4369. end;
  4370. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4371. begin
  4372. inherited ppuload(undefineddef,ppufile);
  4373. end;
  4374. function tundefineddef.GetTypeName:string;
  4375. begin
  4376. GetTypeName:='<undefined type>';
  4377. end;
  4378. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4379. begin
  4380. inherited ppuwrite(ppufile);
  4381. ppufile.writeentry(ibundefineddef);
  4382. end;
  4383. {****************************************************************************
  4384. TERRORDEF
  4385. ****************************************************************************}
  4386. constructor terrordef.create;
  4387. begin
  4388. inherited create(errordef);
  4389. { prevent consecutive faults }
  4390. savesize:=1;
  4391. end;
  4392. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4393. begin
  4394. { Can't write errordefs to ppu }
  4395. internalerror(200411063);
  4396. end;
  4397. function terrordef.GetTypeName:string;
  4398. begin
  4399. GetTypeName:='<erroneous type>';
  4400. end;
  4401. function terrordef.getmangledparaname:string;
  4402. begin
  4403. getmangledparaname:='error';
  4404. end;
  4405. {****************************************************************************
  4406. Definition Helpers
  4407. ****************************************************************************}
  4408. function is_interfacecom(def: tdef): boolean;
  4409. begin
  4410. is_interfacecom:=
  4411. assigned(def) and
  4412. (def.typ=objectdef) and
  4413. (tobjectdef(def).objecttype=odt_interfacecom);
  4414. end;
  4415. function is_interfacecorba(def: tdef): boolean;
  4416. begin
  4417. is_interfacecorba:=
  4418. assigned(def) and
  4419. (def.typ=objectdef) and
  4420. (tobjectdef(def).objecttype=odt_interfacecorba);
  4421. end;
  4422. function is_interface(def: tdef): boolean;
  4423. begin
  4424. is_interface:=
  4425. assigned(def) and
  4426. (def.typ=objectdef) and
  4427. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4428. end;
  4429. function is_dispinterface(def: tdef): boolean;
  4430. begin
  4431. result:=
  4432. assigned(def) and
  4433. (def.typ=objectdef) and
  4434. (tobjectdef(def).objecttype=odt_dispinterface);
  4435. end;
  4436. function is_class(def: tdef): boolean;
  4437. begin
  4438. is_class:=
  4439. assigned(def) and
  4440. (def.typ=objectdef) and
  4441. (tobjectdef(def).objecttype=odt_class);
  4442. end;
  4443. function is_object(def: tdef): boolean;
  4444. begin
  4445. is_object:=
  4446. assigned(def) and
  4447. (def.typ=objectdef) and
  4448. (tobjectdef(def).objecttype=odt_object);
  4449. end;
  4450. function is_cppclass(def: tdef): boolean;
  4451. begin
  4452. is_cppclass:=
  4453. assigned(def) and
  4454. (def.typ=objectdef) and
  4455. (tobjectdef(def).objecttype=odt_cppclass);
  4456. end;
  4457. function is_objcclass(def: tdef): boolean;
  4458. begin
  4459. is_objcclass:=
  4460. assigned(def) and
  4461. (def.typ=objectdef) and
  4462. (tobjectdef(def).objecttype=odt_objcclass);
  4463. end;
  4464. function is_objcclassref(def: tdef): boolean;
  4465. begin
  4466. is_objcclassref:=
  4467. assigned(def) and
  4468. (def.typ=classrefdef) and
  4469. is_objcclass(tclassrefdef(def).pointeddef);
  4470. end;
  4471. function is_objcprotocol(def: tdef): boolean;
  4472. begin
  4473. result:=
  4474. assigned(def) and
  4475. (def.typ=objectdef) and
  4476. (tobjectdef(def).objecttype=odt_objcprotocol);
  4477. end;
  4478. function is_objccategory(def: tdef): boolean;
  4479. begin
  4480. result:=
  4481. assigned(def) and
  4482. (def.typ=objectdef) and
  4483. { if used as a forward type }
  4484. ((tobjectdef(def).objecttype=odt_objccategory) or
  4485. { if used as after it has been resolved }
  4486. ((tobjectdef(def).objecttype=odt_objcclass) and
  4487. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4488. end;
  4489. function is_objc_class_or_protocol(def: tdef): boolean;
  4490. begin
  4491. result:=
  4492. assigned(def) and
  4493. (def.typ=objectdef) and
  4494. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4495. end;
  4496. function is_objc_protocol_or_category(def: tdef): boolean;
  4497. begin
  4498. result:=
  4499. assigned(def) and
  4500. (def.typ=objectdef) and
  4501. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4502. ((tobjectdef(def).objecttype = odt_objcclass) and
  4503. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4504. end;
  4505. function is_class_or_interface(def: tdef): boolean;
  4506. begin
  4507. result:=
  4508. assigned(def) and
  4509. (def.typ=objectdef) and
  4510. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4511. end;
  4512. function is_class_or_interface_or_objc(def: tdef): boolean;
  4513. begin
  4514. result:=
  4515. assigned(def) and
  4516. (def.typ=objectdef) and
  4517. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4518. end;
  4519. function is_class_or_interface_or_object(def: tdef): boolean;
  4520. begin
  4521. result:=
  4522. assigned(def) and
  4523. (def.typ=objectdef) and
  4524. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4525. end;
  4526. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4527. begin
  4528. result:=
  4529. assigned(def) and
  4530. (def.typ=objectdef) and
  4531. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4532. end;
  4533. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4534. begin
  4535. result:=
  4536. assigned(def) and
  4537. (def.typ=objectdef) and
  4538. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4539. end;
  4540. procedure loadobjctypes;
  4541. begin
  4542. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS').typedef);
  4543. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER').typedef);
  4544. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID').typedef);
  4545. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL').typedef);
  4546. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT').typedef);
  4547. end;
  4548. function use_vectorfpu(def : tdef) : boolean;
  4549. begin
  4550. {$ifdef x86}
  4551. {$define use_vectorfpuimplemented}
  4552. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4553. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4554. {$endif x86}
  4555. {$ifdef arm}
  4556. {$define use_vectorfpuimplemented}
  4557. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4558. {$endif arm}
  4559. {$ifndef use_vectorfpuimplemented}
  4560. use_vectorfpu:=false;
  4561. {$endif}
  4562. end;
  4563. end.