symtable.pas 192 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  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 symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,globtype,tokens,
  23. { symtable }
  24. symconst,symbase,symtype,symdef,symsym;
  25. {****************************************************************************
  26. Symtable types
  27. ****************************************************************************}
  28. type
  29. { tstoredsymtable }
  30. tstoredsymtable = class(TSymtable)
  31. private
  32. init_final_check_done : boolean;
  33. deref_built : boolean;
  34. derefimpl_built : boolean;
  35. procedure _needs_init_final(sym:TObject;arg:pointer);
  36. procedure do_init_final_check;
  37. procedure check_forward(sym:TObject;arg:pointer);
  38. procedure check_block_valid(def: TObject;arg:pointer);
  39. procedure register_defs(def:tobject;arg:pointer);
  40. procedure register_syms(sym:tobject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. constructor create(const s:string);
  51. procedure insertsym(sym:TSymEntry;checkdup:boolean=true);override;
  52. procedure deletesym(sym:TSymEntry);override;
  53. { load/write }
  54. procedure ppuload(ppufile:tcompilerppufile);virtual;
  55. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  56. procedure buildderef;
  57. procedure buildderefimpl;
  58. { buildderef but only for (recursively) used symbols/defs }
  59. procedure buildderef_registered;
  60. procedure deref(only_registered: boolean);virtual;
  61. procedure derefimpl(only_registered: boolean);virtual;
  62. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  63. procedure allsymbolsused;
  64. procedure allprivatesused;
  65. procedure check_forwards;
  66. procedure checklabels;
  67. function needs_init_final : boolean; virtual;
  68. function has_non_trivial_init:boolean;virtual;
  69. function is_derefimpl_built: boolean;
  70. function is_deref_built: boolean;
  71. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  72. procedure register_children;
  73. end;
  74. {$ifdef llvm}
  75. tllvmshadowsymtableentry = class
  76. constructor create(def: tdef; fieldoffset: aint);
  77. private
  78. ffieldoffset: aint;
  79. fdef: tdef;
  80. public
  81. property fieldoffset: aint read ffieldoffset;
  82. property def: tdef read fdef;
  83. end;
  84. tllvmshadowsymtable = class;
  85. {$endif llvm}
  86. tmanagementoperator_offset_entry = record
  87. pd : tprocdef;
  88. offset : asizeint;
  89. end;
  90. pmanagementoperator_offset_entry = ^tmanagementoperator_offset_entry;
  91. tabstractrecordsymtable = class(tstoredsymtable)
  92. {$ifdef llvm}
  93. private
  94. fllvmst: tllvmshadowsymtable;
  95. function getllvmshadowsymtabll: tllvmshadowsymtable;
  96. {$endif llvm}
  97. public
  98. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  99. recordalignment, { alignment desired when inserting this record }
  100. fieldalignment, { alignment current alignment used when fields are inserted }
  101. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  102. recordalignmin: shortint; { local equivalentsof global settings, so that records can be created with custom settings internally }
  103. has_fields_with_mop : tmanagementoperators; { whether any of the fields has the need for a management operator (or one of the field's fields) }
  104. constructor create(const n:string;usealign,recordminalign:shortint);
  105. destructor destroy;override;
  106. procedure ppuload(ppufile:tcompilerppufile);override;
  107. procedure ppuwrite(ppufile:tcompilerppufile);override;
  108. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  109. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  110. procedure addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  111. { returns the field closest to this offset (may not be exact because
  112. of padding; internalerrors for variant records, assumes fields are
  113. ordered by increasing offset) }
  114. function findfieldbyoffset(offset:asizeint): tfieldvarsym;
  115. procedure addalignmentpadding;
  116. procedure insertdef(def:TDefEntry);override;
  117. function is_packed: boolean;
  118. function has_single_field(out def:tdef): boolean;
  119. function has_double_field(out def1,def2:tdef; out offset:integer): integer;
  120. { collects all management operators of the specified type in list (which
  121. is not cleared); the entries are copies and thus must be freed by the
  122. caller }
  123. procedure get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  124. protected
  125. { size in bytes including padding }
  126. _datasize : asizeint;
  127. { size in bits of the data in case of bitpacked record. Only important during construction, }
  128. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  129. databitsize : asizeint;
  130. { size in bytes of padding }
  131. _paddingsize : word;
  132. { array of tmanagementoperator_offset_entry lists; only assigned if
  133. they had been queried once by get_management_operator_list }
  134. mop_list : array[tmanagementoperator] of tfplist;
  135. procedure setdatasize(val: asizeint);
  136. function getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  137. procedure do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  138. public
  139. function iscurrentunit: boolean; override;
  140. property datasize : asizeint read _datasize write setdatasize;
  141. property paddingsize: word read _paddingsize write _paddingsize;
  142. {$ifdef llvm}
  143. property llvmst: tllvmshadowsymtable read getllvmshadowsymtabll;
  144. {$endif llvm}
  145. end;
  146. trecordsymtable = class(tabstractrecordsymtable)
  147. public
  148. { maybe someday is worth to move managementoperators to }
  149. { tabstractrecordsymtable to perform management class operators for }
  150. { object/classes. In XE5 and newer is possible to use class operator }
  151. { for classes (like for Delphi .NET before) only for Delphi NEXTGEN }
  152. managementoperators : tmanagementoperators;
  153. constructor create(const n:string;usealign,recordminalign:shortint);
  154. procedure insertunionst(unionst : trecordsymtable;offset : asizeint);
  155. procedure includemanagementoperator(mop:tmanagementoperator);
  156. end;
  157. tObjectSymtable = class(tabstractrecordsymtable)
  158. public
  159. constructor create(adefowner:tdef;const n:string;usealign,recordminalign:shortint);
  160. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  161. end;
  162. {$ifdef llvm}
  163. { llvm record definitions cannot contain variant/union parts, }
  164. { you have to flatten them first. the tllvmshadowsymtable }
  165. { contains a flattened version of a record/object symtable }
  166. tllvmshadowsymtable = class
  167. private
  168. equivst: tabstractrecordsymtable;
  169. curroffset: aint;
  170. function get(f: tfieldvarsym): tllvmshadowsymtableentry;
  171. function get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  172. public
  173. symdeflist: TFPObjectList;
  174. constructor create(st: tabstractrecordsymtable);
  175. destructor destroy; override;
  176. property entries[index: tfieldvarsym]: tllvmshadowsymtableentry read get; default;
  177. { warning: do not call this with field.llvmfieldnr, as
  178. field.llvmfieldnr will only be initialised when the llvm shadow
  179. symtable is accessed for the first time. Use the default/entries
  180. property instead in this case }
  181. property entries_by_llvm_index[index: longint]: tllvmshadowsymtableentry read get_by_llvm_index;
  182. private
  183. // generate the table
  184. procedure generate;
  185. // helpers
  186. procedure appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  187. procedure preprocess(out tempsymlist, variantstarts: tfplist);
  188. procedure addalignmentpadding(finalsize: aint);
  189. procedure buildmapping(tempsymlist, variantstarts: tfplist);
  190. procedure buildtable(tempsymlist, variantstarts: tfplist);
  191. end;
  192. {$endif llvm}
  193. { tabstractsubsymtable }
  194. tabstractsubsymtable = class(tstoredsymtable)
  195. public
  196. procedure ppuwrite(ppufile:tcompilerppufile);override;
  197. end;
  198. { tabstractlocalsymtable }
  199. tabstractlocalsymtable = class(tabstractsubsymtable)
  200. public
  201. function count_locals:longint;
  202. function iscurrentunit: boolean; override;
  203. end;
  204. tlocalsymtable = class(tabstractlocalsymtable)
  205. public
  206. constructor create(adefowner:tdef;level:byte);
  207. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  208. end;
  209. { tparasymtable }
  210. tparasymtable = class(tabstractlocalsymtable)
  211. public
  212. constructor create(adefowner:tdef;level:byte);
  213. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  214. end;
  215. tabstractuniTSymtable = class(tstoredsymtable)
  216. public
  217. constructor create(const n : string;id:word);
  218. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  219. function findnamespace(const n:string):TSymEntry;virtual;
  220. function iscurrentunit:boolean;override;
  221. function needs_init_final: boolean; override;
  222. procedure insertunit(sym:TSymEntry);
  223. function has_class_condestructors: boolean;
  224. end;
  225. tglobalsymtable = class(tabstractuniTSymtable)
  226. public
  227. unittypecount : word;
  228. constructor create(const n : string;id:word);
  229. procedure ppuload(ppufile:tcompilerppufile);override;
  230. procedure ppuwrite(ppufile:tcompilerppufile);override;
  231. end;
  232. tstaticsymtable = class(tabstractuniTSymtable)
  233. public
  234. constructor create(const n : string;id:word);
  235. procedure ppuload(ppufile:tcompilerppufile);override;
  236. procedure ppuwrite(ppufile:tcompilerppufile);override;
  237. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  238. function findnamespace(const n:string):TSymEntry;override;
  239. end;
  240. tspecializesymtable = class(tglobalsymtable)
  241. public
  242. constructor create(const n : string;id:word);
  243. function iscurrentunit:boolean;override;
  244. end;
  245. twithsymtable = class(TSymtable)
  246. withrefnode : tobject; { tnode }
  247. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  248. destructor destroy;override;
  249. procedure clear;override;
  250. procedure insertdef(def:TDefEntry);override;
  251. end;
  252. tstt_exceptsymtable = class(TSymtable)
  253. public
  254. constructor create;
  255. end;
  256. tmacrosymtable = class(tstoredsymtable)
  257. public
  258. constructor create(exported: boolean);
  259. end;
  260. { tenumsymtable }
  261. tenumsymtable = class(tabstractsubsymtable)
  262. public
  263. procedure insertsym(sym: TSymEntry; checkdup: boolean = true); override;
  264. constructor create(adefowner:tdef);
  265. end;
  266. { tarraysymtable }
  267. tarraysymtable = class(tabstractsubsymtable)
  268. public
  269. procedure insertdef(def:TDefEntry);override;
  270. constructor create(adefowner:tdef);
  271. end;
  272. var
  273. systemunit : tglobalsymtable; { pointer to the system unit }
  274. type
  275. tsymbol_search_flag = (
  276. ssf_search_option,
  277. ssf_search_helper,
  278. ssf_has_inherited,
  279. ssf_no_addsymref,
  280. ssf_unit_or_namespace_only
  281. );
  282. tsymbol_search_flags = set of tsymbol_search_flag;
  283. {****************************************************************************
  284. Functions
  285. ****************************************************************************}
  286. {*** Misc ***}
  287. function FullTypeName(def,otherdef:tdef):string;
  288. function generate_nested_name(symtable:tsymtable;const delimiter:string):string;
  289. { def is the extended type of a helper }
  290. function generate_objectpascal_helper_key(def:tdef):TSymStr;
  291. procedure incompatibletypes(def1,def2:tdef);
  292. procedure hidesym(sym:TSymEntry);
  293. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym:TSymEntry; warn: boolean);
  294. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  295. { writes all declarations for the specified system unit symbol }
  296. procedure write_system_parameter_lists(const name:string);
  297. {*** Search ***}
  298. procedure addsymref(sym:tsym);inline;
  299. procedure addsymref(sym:tsym;def:tdef);
  300. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  301. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  302. function defs_belong_to_same_generic(def1,def2:tdef):boolean;
  303. function get_generic_in_hierarchy_by_name(srsym:tsym;def:tdef):tdef;
  304. function return_specialization_of_generic(nesteddef,genericdef:tdef;out resultdef:tdef):boolean;
  305. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  306. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  307. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  308. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  309. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  310. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  311. { searches for a symbol with the given name that has the given option in
  312. symoptions set }
  313. function searchsym_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;option:tsymoption):boolean;
  314. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  315. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  316. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  317. function searchsym_in_class(classh: tobjectdef; contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  318. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  319. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  320. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  321. { searches symbols inside of a helper's implementation }
  322. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  323. function search_system_type(const s: TIDString): ttypesym;
  324. function try_search_system_type(const s: TIDString): ttypesym;
  325. function try_search_current_module_type(const s: TIDString): ttypesym;
  326. function search_system_proc(const s: TIDString): tprocdef;
  327. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  328. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  329. function search_struct_member_no_helper(pd : tabstractrecorddef;const s : string):tsym;
  330. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  331. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  332. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  333. { searches for the helper definition that's currently active for pd }
  334. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  335. { searches whether the symbol s is available in the currently active }
  336. { helper for pd }
  337. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  338. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  339. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  340. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  341. {and returns it if found. Returns nil otherwise.}
  342. function search_macro(const s : string):tsym;
  343. { Additionally to searching for a macro, also checks whether it's still }
  344. { actually defined (could be disable using "undef") }
  345. function defined_macro(const s : string):boolean;
  346. { Look for a system procedure (no overloads supported) }
  347. { returns a list of helpers in the current module for the def }
  348. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  349. {*** Object Helpers ***}
  350. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  351. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  352. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  353. {*** Macro Helpers ***}
  354. {If called initially, the following procedures manipulate macros in }
  355. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  356. {Name can be given in any case (it will be converted to upper case).}
  357. procedure def_system_macro(const name : string);
  358. procedure set_system_macro(const name, value : string);
  359. procedure set_system_compvar(const name, value : string);
  360. procedure undef_system_macro(const name : string);
  361. {*** symtable stack ***}
  362. { $ifdef DEBUG
  363. procedure test_symtablestack;
  364. procedure list_symtablestack;
  365. $endif DEBUG}
  366. {$ifdef UNITALIASES}
  367. type
  368. punit_alias = ^tunit_alias;
  369. tunit_alias = object(TNamedIndexItem)
  370. newname : pshortstring;
  371. constructor init(const n:string);
  372. destructor done;virtual;
  373. end;
  374. var
  375. unitaliases : pdictionary;
  376. procedure addunitalias(const n:string);
  377. function getunitalias(const n:string):string;
  378. {$endif UNITALIASES}
  379. {*** Init / Done ***}
  380. procedure IniTSymtable;
  381. procedure DoneSymtable;
  382. const
  383. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  384. { NOTOKEN } 'error',
  385. { _PLUS } 'plus',
  386. { _MINUS } 'minus',
  387. { _STAR } 'star',
  388. { _SLASH } 'slash',
  389. { _EQ } 'equal',
  390. { _GT } 'greater',
  391. { _LT } 'lower',
  392. { _GTE } 'greater_or_equal',
  393. { _LTE } 'lower_or_equal',
  394. { _NE } 'not_equal',
  395. { _SYMDIF } 'sym_diff',
  396. { _STARSTAR } 'starstar',
  397. { _OP_AS } 'as',
  398. { _OP_IN } 'in',
  399. { _OP_IS } 'is',
  400. { _OP_OR } 'or',
  401. { _OP_AND } 'and',
  402. { _OP_DIV } 'div',
  403. { _OP_MOD } 'mod',
  404. { _OP_NOT } 'not',
  405. { _OP_SHL } 'shl',
  406. { _OP_SHR } 'shr',
  407. { _OP_XOR } 'xor',
  408. { _ASSIGNMENT } 'assign',
  409. { _OP_EXPLICIT } 'explicit',
  410. { _OP_ENUMERATOR } 'enumerator',
  411. { _OP_INITIALIZE } 'initialize',
  412. { _OP_FINALIZE } 'finalize',
  413. { _OP_ADDREF } 'addref',
  414. { _OP_COPY } 'copy',
  415. { _OP_INC } 'inc',
  416. { _OP_DEC } 'dec');
  417. managementoperator2tok:array[tmanagementoperator] of ttoken = (
  418. { mop_none } NOTOKEN,
  419. { mop_initialize } _OP_INITIALIZE,
  420. { mop_finalize } _OP_FINALIZE,
  421. { mop_addref } _OP_ADDREF,
  422. { mop_copy } _OP_COPY
  423. );
  424. implementation
  425. uses
  426. { global }
  427. verbose,globals,systems,
  428. { symtable }
  429. symutil,defutil,defcmp,objcdef,
  430. { module }
  431. fmodule,
  432. { codegen }
  433. procinfo,
  434. { ppu }
  435. entfile,ppu,
  436. { parser }
  437. scanner
  438. ;
  439. var
  440. dupnr : longint; { unique number for duplicate symbols }
  441. {*****************************************************************************
  442. TStoredSymtable
  443. *****************************************************************************}
  444. constructor tstoredsymtable.create(const s:string);
  445. begin
  446. inherited create(s);
  447. { Note: this happens for the initial macro symtable, so no error here }
  448. if not assigned(current_module) then
  449. comment(v_debug,'Current module not available for module id')
  450. else
  451. moduleid:=current_module.moduleid;
  452. end;
  453. procedure tstoredsymtable.insertsym(sym:TSymEntry;checkdup:boolean=true);
  454. begin
  455. inherited insertsym(sym,checkdup);
  456. init_final_check_done:=false;
  457. end;
  458. procedure tstoredsymtable.deletesym(sym:TSymEntry);
  459. begin
  460. inherited deletesym(sym);
  461. init_final_check_done:=false;
  462. end;
  463. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  464. begin
  465. { load the table's flags }
  466. if ppufile.readentry<>ibsymtableoptions then
  467. Message(unit_f_ppu_read_error);
  468. ppufile.getset(tppuset1(tableoptions));
  469. { load definitions }
  470. loaddefs(ppufile);
  471. { load symbols }
  472. loadsyms(ppufile);
  473. init_final_check_done:=true;
  474. end;
  475. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  476. begin
  477. { ensure that we have the sto_needs_init_final flag set if needed }
  478. if not init_final_check_done then
  479. needs_init_final;
  480. { write the table's flags }
  481. ppufile.putset(tppuset1(tableoptions));
  482. ppufile.writeentry(ibsymtableoptions);
  483. { write definitions }
  484. writedefs(ppufile);
  485. { write symbols }
  486. writesyms(ppufile);
  487. end;
  488. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  489. var
  490. def : tdef;
  491. b : byte;
  492. begin
  493. { load start of definition section, which holds the amount of defs }
  494. if ppufile.readentry<>ibstartdefs then
  495. Message(unit_f_ppu_read_error);
  496. { read definitions }
  497. repeat
  498. b:=ppufile.readentry;
  499. def:=nil;
  500. case b of
  501. ibpointerdef : def:=cpointerdef.ppuload(ppufile);
  502. ibarraydef : def:=carraydef.ppuload(ppufile);
  503. iborddef : def:=corddef.ppuload(ppufile);
  504. ibfloatdef : def:=cfloatdef.ppuload(ppufile);
  505. ibprocdef : def:=cprocdef.ppuload(ppufile);
  506. ibshortstringdef : def:=cstringdef.loadshort(ppufile);
  507. iblongstringdef : def:=cstringdef.loadlong(ppufile);
  508. ibansistringdef : def:=cstringdef.loadansi(ppufile);
  509. ibwidestringdef : def:=cstringdef.loadwide(ppufile);
  510. ibunicodestringdef : def:=cstringdef.loadunicode(ppufile);
  511. ibrecorddef : def:=crecorddef.ppuload(ppufile);
  512. ibobjectdef : def:=cobjectdef.ppuload(ppufile);
  513. ibenumdef : def:=cenumdef.ppuload(ppufile);
  514. ibsetdef : def:=csetdef.ppuload(ppufile);
  515. ibprocvardef : def:=cprocvardef.ppuload(ppufile);
  516. ibfiledef : def:=cfiledef.ppuload(ppufile);
  517. ibclassrefdef : def:=cclassrefdef.ppuload(ppufile);
  518. ibformaldef : def:=cformaldef.ppuload(ppufile);
  519. ibvariantdef : def:=cvariantdef.ppuload(ppufile);
  520. ibundefineddef : def:=cundefineddef.ppuload(ppufile);
  521. ibenddefs : break;
  522. ibend : Message(unit_f_ppu_read_error);
  523. else
  524. Message1(unit_f_ppu_invalid_entry,tostr(b));
  525. end;
  526. if assigned(def) then
  527. tstoreddef(def).ppuload_subentries(ppufile);
  528. InsertDef(def);
  529. until false;
  530. end;
  531. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  532. var
  533. b : byte;
  534. sym : tsym;
  535. begin
  536. { load start of definition section, which holds the amount of defs }
  537. if ppufile.readentry<>ibstartsyms then
  538. Message(unit_f_ppu_read_error);
  539. { now read the symbols }
  540. repeat
  541. sym:=nil;
  542. b:=ppufile.readentry;
  543. case b of
  544. ibtypesym : sym:=ctypesym.ppuload(ppufile);
  545. ibprocsym : sym:=cprocsym.ppuload(ppufile);
  546. ibconstsym : sym:=cconstsym.ppuload(ppufile);
  547. ibstaticvarsym : sym:=cstaticvarsym.ppuload(ppufile);
  548. iblocalvarsym : sym:=clocalvarsym.ppuload(ppufile);
  549. ibparavarsym : sym:=cparavarsym.ppuload(ppufile);
  550. ibfieldvarsym : sym:=cfieldvarsym.ppuload(ppufile);
  551. ibabsolutevarsym : sym:=cabsolutevarsym.ppuload(ppufile);
  552. ibenumsym : sym:=cenumsym.ppuload(ppufile);
  553. ibpropertysym : sym:=cpropertysym.ppuload(ppufile);
  554. ibunitsym : sym:=cunitsym.ppuload(ppufile);
  555. iblabelsym : sym:=clabelsym.ppuload(ppufile);
  556. ibsyssym : sym:=csyssym.ppuload(ppufile);
  557. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  558. ibnamespacesym : sym:=cnamespacesym.ppuload(ppufile);
  559. ibendsyms : break;
  560. ibend : Message(unit_f_ppu_read_error);
  561. else
  562. Message1(unit_f_ppu_invalid_entry,tostr(b));
  563. end;
  564. if assigned(sym) then
  565. tstoredsym(sym).ppuload_subentries(ppufile);
  566. InsertSym(sym,false);
  567. until false;
  568. end;
  569. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  570. var
  571. defcount,
  572. i : longint;
  573. def : tstoreddef;
  574. begin
  575. defcount:=0;
  576. for i:=0 to DefList.Count-1 do
  577. if tstoreddef(DefList[i]).is_registered then
  578. inc(defcount);
  579. { each definition get a number, write then the amount of defs to the
  580. ibstartdef entry }
  581. ppufile.putlongint(defcount);
  582. ppufile.writeentry(ibstartdefs);
  583. { now write the definition }
  584. for i:=0 to DefList.Count-1 do
  585. begin
  586. def:=tstoreddef(DefList[i]);
  587. if def.is_registered then
  588. begin
  589. def.ppuwrite(ppufile);
  590. def.ppuwrite_subentries(ppufile);
  591. end;
  592. end;
  593. { write end of definitions }
  594. ppufile.writeentry(ibenddefs);
  595. end;
  596. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  597. var
  598. symcount,
  599. i : longint;
  600. sym : Tstoredsym;
  601. begin
  602. symcount:=0;
  603. for i:=0 to SymList.Count-1 do
  604. if tstoredsym(SymList[i]).is_registered then
  605. inc(symcount);
  606. { each definition get a number, write then the amount of syms and the
  607. datasize to the ibsymdef entry }
  608. ppufile.putlongint(symcount);
  609. ppufile.writeentry(ibstartsyms);
  610. { foreach is used to write all symbols }
  611. for i:=0 to SymList.Count-1 do
  612. begin
  613. sym:=tstoredsym(SymList[i]);
  614. if sym.is_registered then
  615. begin
  616. sym.ppuwrite(ppufile);
  617. sym.ppuwrite_subentries(ppufile);
  618. end;
  619. end;
  620. { end of symbols }
  621. ppufile.writeentry(ibendsyms);
  622. end;
  623. function tstoredsymtable.is_deref_built: boolean;
  624. begin
  625. Result:=deref_built;
  626. end;
  627. function tstoredsymtable.is_derefimpl_built: boolean;
  628. begin
  629. Result:=derefimpl_built;
  630. end;
  631. procedure tstoredsymtable.buildderef;
  632. var
  633. i : longint;
  634. def : tstoreddef;
  635. sym : tstoredsym;
  636. begin
  637. { interface definitions }
  638. for i:=0 to DefList.Count-1 do
  639. begin
  640. def:=tstoreddef(DefList[i]);
  641. def.buildderef;
  642. end;
  643. { interface symbols }
  644. for i:=0 to SymList.Count-1 do
  645. begin
  646. sym:=tstoredsym(SymList[i]);
  647. sym.buildderef;
  648. end;
  649. deref_built:=True;
  650. end;
  651. procedure tstoredsymtable.buildderefimpl;
  652. var
  653. i : longint;
  654. def : tstoreddef;
  655. begin
  656. { implementation definitions }
  657. for i:=0 to DefList.Count-1 do
  658. begin
  659. def:=tstoreddef(DefList[i]);
  660. def.buildderefimpl;
  661. end;
  662. derefimpl_built:=True;
  663. end;
  664. procedure tstoredsymtable.buildderef_registered;
  665. var
  666. def : tstoreddef;
  667. sym : tstoredsym;
  668. i : longint;
  669. defidmax,
  670. symidmax: longint;
  671. newbuiltdefderefs,
  672. builtdefderefs,
  673. builtsymderefs: array of boolean;
  674. changed: boolean;
  675. begin
  676. newbuiltdefderefs:=nil;
  677. builtdefderefs:=nil;
  678. builtsymderefs:=nil;
  679. { tdefs for which we already built the deref }
  680. setlength(builtdefderefs,deflist.count);
  681. { tdefs for which we built the deref in this iteration }
  682. setlength(newbuiltdefderefs,deflist.count);
  683. { syms for which we already built the deref }
  684. setlength(builtsymderefs,symlist.count);
  685. repeat
  686. { we only have to store the defs (recursively) referred by wpo info
  687. or inlined routines in the static symbtable }
  688. { current number of registered defs/syms }
  689. defidmax:=current_module.deflist.count;
  690. symidmax:=current_module.symlist.count;
  691. changed:=false;
  692. { build the derefs for the registered defs we haven't processed yet }
  693. for i:=0 to DefList.Count-1 do
  694. begin
  695. if not builtdefderefs[i] then
  696. begin
  697. def:=tstoreddef(DefList[i]);
  698. if def.is_registered then
  699. begin
  700. def.buildderef;
  701. newbuiltdefderefs[i]:=true;
  702. builtdefderefs[i]:=true;
  703. changed:=true;
  704. end;
  705. end;
  706. end;
  707. { same for the syms }
  708. for i:=0 to SymList.Count-1 do
  709. begin
  710. if not builtsymderefs[i] then
  711. begin
  712. sym:=tstoredsym(SymList[i]);
  713. if sym.is_registered then
  714. begin
  715. sym.buildderef;
  716. builtsymderefs[i]:=true;
  717. changed:=true;
  718. end;
  719. end;
  720. end;
  721. { now buildderefimpl for the defs we processed in this iteration }
  722. for i:=0 to DefList.Count-1 do
  723. begin
  724. if newbuiltdefderefs[i] then
  725. begin
  726. newbuiltdefderefs[i]:=false;
  727. tstoreddef(DefList[i]).buildderefimpl;
  728. changed:=true;
  729. end;
  730. end;
  731. { stop when no new defs or syms have been registered while processing
  732. the currently registered ones (defs/syms get added to the module's
  733. deflist/symlist when they are registered) }
  734. until not changed and
  735. (defidmax=current_module.deflist.count) and
  736. (symidmax=current_module.symlist.count);
  737. end;
  738. procedure tstoredsymtable.deref(only_registered: boolean);
  739. var
  740. i : longint;
  741. def : tstoreddef;
  742. sym : tstoredsym;
  743. begin
  744. { first deref the interface ttype symbols. This is needs
  745. to be done before the interface defs are derefed, because
  746. the interface defs can contain references to the type symbols
  747. which then already need to contain a resolved typedef field (PFV) }
  748. for i:=0 to SymList.Count-1 do
  749. begin
  750. sym:=tstoredsym(SymList[i]);
  751. if (sym.typ=typesym) and
  752. (not only_registered or
  753. sym.is_registered) then
  754. sym.deref;
  755. end;
  756. { interface definitions }
  757. for i:=0 to DefList.Count-1 do
  758. begin
  759. def:=tstoreddef(DefList[i]);
  760. if not only_registered or
  761. def.is_registered then
  762. def.deref;
  763. end;
  764. { interface symbols }
  765. for i:=0 to SymList.Count-1 do
  766. begin
  767. sym:=tstoredsym(SymList[i]);
  768. if (not only_registered or
  769. sym.is_registered) and
  770. (sym.typ<>typesym) then
  771. sym.deref;
  772. end;
  773. end;
  774. procedure tstoredsymtable.derefimpl(only_registered: boolean);
  775. var
  776. i : longint;
  777. def : tstoreddef;
  778. begin
  779. { implementation definitions }
  780. for i:=0 to DefList.Count-1 do
  781. begin
  782. def:=tstoreddef(DefList[i]);
  783. if not only_registered or
  784. def.is_registered then
  785. def.derefimpl;
  786. end;
  787. end;
  788. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  789. var
  790. hsym : tsym;
  791. begin
  792. hsym:=tsym(FindWithHash(hashedid));
  793. if assigned(hsym) then
  794. DuplicateSym(hashedid,sym,hsym,false);
  795. result:=assigned(hsym);
  796. end;
  797. {**************************************
  798. Callbacks
  799. **************************************}
  800. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  801. begin
  802. if tsym(sym).typ=procsym then
  803. tprocsym(sym).check_forward
  804. { check also object method table }
  805. { we needn't to test the def list }
  806. { because each object has to have a type sym,
  807. only test objects declarations, not type renamings }
  808. else
  809. if (tsym(sym).typ=typesym) and
  810. assigned(ttypesym(sym).typedef) and
  811. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  812. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  813. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  814. end;
  815. procedure tstoredsymtable.check_block_valid(def: TObject; arg: pointer);
  816. var
  817. founderrordef: tdef;
  818. begin
  819. { all parameters passed to a block must be handled by the Objective-C
  820. runtime }
  821. if is_block(tdef(def)) and
  822. not objcchecktype(tdef(def),founderrordef) then
  823. if assigned(tdef(def).typesym) then
  824. MessagePos1(tdef(def).typesym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename)
  825. else
  826. Message1(type_e_objc_type_unsupported,tprocvardef(def).typename)
  827. end;
  828. procedure tstoredsymtable.register_syms(sym:tobject;arg:pointer);
  829. begin
  830. tsym(sym).register_sym;
  831. end;
  832. procedure tstoredsymtable.register_defs(def:tobject;arg:pointer);
  833. begin
  834. tdef(def).register_def;
  835. end;
  836. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  837. begin
  838. if (tsym(sym).typ=labelsym) and
  839. not(tlabelsym(sym).defined) then
  840. begin
  841. if tlabelsym(sym).used then
  842. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  843. else
  844. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  845. end;
  846. end;
  847. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  848. begin
  849. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  850. ((tsym(sym).owner.symtabletype in
  851. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  852. begin
  853. { unused symbol should be reported only if no }
  854. { error is reported }
  855. { if the symbol is in a register it is used }
  856. { also don't count the value parameters which have local copies }
  857. { also don't claim for high param of open parameters (PM) }
  858. { also don't complain about unused symbols in generic procedures }
  859. { and methods }
  860. { and neither in abstract methods }
  861. if (Errorcount<>0) or
  862. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  863. (sp_internal in tsym(sym).symoptions) or
  864. ((assigned(tsym(sym).owner.defowner) and
  865. (tsym(sym).owner.defowner.typ=procdef) and
  866. ((df_generic in tprocdef(tsym(sym).owner.defowner).defoptions) or
  867. (po_abstractmethod in tprocdef(tsym(sym).owner.defowner).procoptions)))) then
  868. exit;
  869. if (tstoredsym(sym).refs=0) then
  870. begin
  871. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  872. begin
  873. { don't warn about the result of constructors }
  874. { or the synthetic helper functions for class-attributes }
  875. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  876. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  877. not (po_noreturn in tprocdef(tsym(sym).owner.defowner).procoptions) and
  878. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  879. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  880. end
  881. else if (tsym(sym).owner.symtabletype=parasymtable) then
  882. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  883. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  884. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  885. else
  886. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  887. end
  888. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  889. begin
  890. if (tsym(sym).owner.symtabletype=parasymtable) then
  891. begin
  892. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  893. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  894. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  895. end
  896. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  897. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  898. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  899. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  900. end
  901. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  902. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  903. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  904. end
  905. else if ((tsym(sym).owner.symtabletype in
  906. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  907. begin
  908. if (Errorcount<>0) or
  909. (sp_internal in tsym(sym).symoptions) then
  910. exit;
  911. { do not claim for inherited private fields !! }
  912. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  913. case tsym(sym).typ of
  914. typesym:
  915. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  916. constsym:
  917. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  918. propertysym:
  919. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  920. else
  921. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  922. end
  923. { units references are problematic }
  924. else
  925. begin
  926. if (tsym(sym).refs=0) and
  927. not(tsym(sym).typ in [enumsym,unitsym,namespacesym]) and
  928. not(is_funcret_sym(tsym(sym))) and
  929. { don't complain about compiler generated syms for specializations, see also #13405 }
  930. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  931. (pos('$',ttypesym(sym).Realname)<>0)) and
  932. (
  933. (tsym(sym).typ<>procsym) or
  934. ((tsym(sym).owner.symtabletype=staticsymtable) and
  935. not current_module.is_unit)
  936. ) and
  937. { don't complain about alias for hidden _cmd parameter to
  938. obj-c methods }
  939. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  940. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  941. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  942. end;
  943. end;
  944. end;
  945. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  946. begin
  947. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  948. varsymbolused(sym,arg);
  949. end;
  950. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  951. begin
  952. {
  953. Don't test simple object aliases PM
  954. }
  955. if (tsym(sym).typ=typesym) and
  956. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  957. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  958. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  959. end;
  960. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  961. begin
  962. if (tsym(sym).typ=propertysym) and
  963. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  964. ppointer(arg)^:=sym;
  965. end;
  966. procedure tstoredsymtable.register_children;
  967. begin
  968. SymList.ForEachCall(@register_syms,nil);
  969. DefList.ForEachCall(@register_defs,nil);
  970. end;
  971. {***********************************************
  972. Process all entries
  973. ***********************************************}
  974. { checks, if all procsyms and methods are defined }
  975. procedure tstoredsymtable.check_forwards;
  976. begin
  977. SymList.ForEachCall(@check_forward,nil);
  978. { check whether all block definitions contain valid Objective-C types
  979. (now that all forward definitions have been resolved) }
  980. DefList.ForEachCall(@check_block_valid,nil);
  981. end;
  982. procedure tstoredsymtable.checklabels;
  983. begin
  984. SymList.ForEachCall(@labeldefined,nil);
  985. end;
  986. procedure tstoredsymtable.allsymbolsused;
  987. begin
  988. SymList.ForEachCall(@varsymbolused,nil);
  989. end;
  990. procedure tstoredsymtable.allprivatesused;
  991. begin
  992. SymList.ForEachCall(@objectprivatesymbolused,nil);
  993. end;
  994. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  995. begin
  996. if [sto_needs_init_final,sto_has_non_trivial_init] <= tableoptions then
  997. exit;
  998. { don't check static symbols - they can be present in structures only and
  999. always have a reference to a symbol defined on unit level }
  1000. if sp_static in tsym(sym).symoptions then
  1001. exit;
  1002. case tsym(sym).typ of
  1003. fieldvarsym,
  1004. staticvarsym,
  1005. localvarsym,
  1006. paravarsym :
  1007. begin
  1008. if assigned(tabstractvarsym(sym).vardef) and
  1009. is_managed_type(tabstractvarsym(sym).vardef) then
  1010. include(tableoptions,sto_needs_init_final);
  1011. if is_record((tabstractvarsym(sym).vardef)) and
  1012. (mop_initialize in trecordsymtable(trecorddef(tabstractvarsym(sym).vardef).symtable).managementoperators) then
  1013. include(tableoptions,sto_has_non_trivial_init);
  1014. end;
  1015. else
  1016. ;
  1017. end;
  1018. end;
  1019. procedure tstoredsymtable.do_init_final_check;
  1020. begin
  1021. if not init_final_check_done then
  1022. begin
  1023. exclude(tableoptions,sto_needs_init_final);
  1024. exclude(tableoptions,sto_has_non_trivial_init);
  1025. SymList.ForEachCall(@_needs_init_final,nil);
  1026. init_final_check_done:=true;
  1027. end;
  1028. end;
  1029. { returns true, if p contains data which needs init/final code }
  1030. function tstoredsymtable.needs_init_final : boolean;
  1031. begin
  1032. do_init_final_check;
  1033. result:=sto_needs_init_final in tableoptions;
  1034. end;
  1035. function tstoredsymtable.has_non_trivial_init:boolean;
  1036. begin
  1037. do_init_final_check;
  1038. result:=sto_has_non_trivial_init in tableoptions;
  1039. end;
  1040. {****************************************************************************
  1041. TAbstractRecordSymtable
  1042. ****************************************************************************}
  1043. {$ifdef llvm}
  1044. function tabstractrecordsymtable.getllvmshadowsymtabll: tllvmshadowsymtable;
  1045. begin
  1046. if not assigned(fllvmst) then
  1047. fllvmst:=tllvmshadowsymtable.create(self);
  1048. result:=fllvmst;
  1049. end;
  1050. {$endif llvm}
  1051. constructor tabstractrecordsymtable.create(const n:string;usealign,recordminalign:shortint);
  1052. begin
  1053. inherited create(n);
  1054. _datasize:=0;
  1055. databitsize:=0;
  1056. recordalignment:=1;
  1057. usefieldalignment:=usealign;
  1058. recordalignmin:=recordminalign;
  1059. padalignment:=1;
  1060. { recordalign C_alignment means C record packing, that starts
  1061. with an alignment of 1 }
  1062. case usealign of
  1063. C_alignment,
  1064. bit_alignment:
  1065. fieldalignment:=1;
  1066. mac68k_alignment:
  1067. fieldalignment:=2;
  1068. else
  1069. fieldalignment:=usealign;
  1070. end;
  1071. end;
  1072. destructor tabstractrecordsymtable.destroy;
  1073. var
  1074. mop : tmanagementoperator;
  1075. begin
  1076. if refcount>1 then
  1077. exit;
  1078. {$ifdef llvm}
  1079. fllvmst.free;
  1080. {$endif llvm}
  1081. for mop:=low(tmanagementoperator) to high(tmanagementoperator) do
  1082. TFPList.FreeAndNilDisposing(mop_list[mop],TypeInfo(tmanagementoperator_offset_entry));
  1083. inherited destroy;
  1084. end;
  1085. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  1086. begin
  1087. if ppufile.readentry<>ibrecsymtableoptions then
  1088. Message(unit_f_ppu_read_error);
  1089. recordalignment:=shortint(ppufile.getbyte);
  1090. usefieldalignment:=shortint(ppufile.getbyte);
  1091. recordalignmin:=shortint(ppufile.getbyte);
  1092. if (usefieldalignment=C_alignment) then
  1093. fieldalignment:=shortint(ppufile.getbyte);
  1094. ppufile.getset(tppuset1(has_fields_with_mop));
  1095. inherited ppuload(ppufile);
  1096. end;
  1097. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  1098. var
  1099. oldtyp : byte;
  1100. begin
  1101. oldtyp:=ppufile.entrytyp;
  1102. ppufile.entrytyp:=subentryid;
  1103. { in case of classes using C alignment, the alignment of the parent
  1104. affects the alignment of fields of the childs }
  1105. ppufile.putbyte(byte(recordalignment));
  1106. ppufile.putbyte(byte(usefieldalignment));
  1107. ppufile.putbyte(byte(recordalignmin));
  1108. if (usefieldalignment=C_alignment) then
  1109. ppufile.putbyte(byte(fieldalignment));
  1110. { it's not really a "symtableoption", but loading this from the record
  1111. def requires storing the set in the recorddef at least between
  1112. ppuload and deref/derefimpl }
  1113. ppufile.putset(tppuset1(has_fields_with_mop));
  1114. ppufile.writeentry(ibrecsymtableoptions);
  1115. inherited ppuwrite(ppufile);
  1116. ppufile.entrytyp:=oldtyp;
  1117. end;
  1118. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  1119. begin
  1120. { optimal alignment of the record when declaring a variable of this }
  1121. { type is independent of the packrecords setting }
  1122. if (fieldoffs mod fieldalign) = 0 then
  1123. result:=fieldalign
  1124. else if (fieldalign >= 16) and
  1125. ((fieldoffs mod 16) = 0) and
  1126. ((fieldalign mod 16) = 0) then
  1127. result:=16
  1128. else if (fieldalign >= 8) and
  1129. ((fieldoffs mod 8) = 0) and
  1130. ((fieldalign mod 8) = 0) then
  1131. result:=8
  1132. else if (fieldalign >= 4) and
  1133. ((fieldoffs mod 4) = 0) and
  1134. ((fieldalign mod 4) = 0) then
  1135. result:=4
  1136. else if (fieldalign >= 2) and
  1137. ((fieldoffs mod 2) = 0) and
  1138. ((fieldalign mod 2) = 0) then
  1139. result:=2
  1140. else
  1141. result:=1;
  1142. end;
  1143. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1144. var
  1145. varalignrecord: shortint;
  1146. begin
  1147. case usefieldalignment of
  1148. C_alignment:
  1149. varalignrecord:=used_align(varalign,recordalignmin,current_settings.alignment.maxCrecordalign);
  1150. mac68k_alignment:
  1151. varalignrecord:=2;
  1152. else
  1153. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1154. end;
  1155. recordalignment:=max(recordalignment,varalignrecord);
  1156. end;
  1157. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1158. var
  1159. l : asizeint;
  1160. varalign : shortint;
  1161. vardef : tdef;
  1162. begin
  1163. if (sym.owner<>self) then
  1164. internalerror(200602031);
  1165. if sym.fieldoffset<>-1 then
  1166. internalerror(200602032);
  1167. { set visibility for the symbol }
  1168. sym.visibility:=vis;
  1169. { this symbol can't be loaded to a register }
  1170. sym.varregable:=vr_none;
  1171. { management operators }
  1172. if sym.vardef.typ in [recorddef,objectdef] then
  1173. has_fields_with_mop:=has_fields_with_mop + tabstractrecordsymtable(tabstractrecorddef(sym.vardef).symtable).has_fields_with_mop;
  1174. if sym.vardef.typ=recorddef then
  1175. has_fields_with_mop:=has_fields_with_mop + trecordsymtable(trecorddef(sym.vardef).symtable).managementoperators;
  1176. { Calculate field offset }
  1177. l:=sym.getsize;
  1178. vardef:=sym.vardef;
  1179. varalign:=vardef.structalignment;
  1180. case usefieldalignment of
  1181. bit_alignment:
  1182. begin
  1183. { bitpacking only happens for ordinals, the rest is aligned at }
  1184. { 1 byte (compatible with GPC/GCC) }
  1185. if is_ordinal(vardef) then
  1186. begin
  1187. sym.fieldoffset:=databitsize;
  1188. l:=sym.getpackedbitsize;
  1189. end
  1190. else
  1191. begin
  1192. databitsize:=_datasize*8;
  1193. sym.fieldoffset:=databitsize;
  1194. if (l>high(asizeint) div 8) then
  1195. Message(sym_e_segment_too_large);
  1196. l:=l*8;
  1197. end;
  1198. if varalign=0 then
  1199. varalign:=size_2_align(l);
  1200. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1201. { bit packed records are limited to high(aint) bits }
  1202. { instead of bytes to avoid double precision }
  1203. { arithmetic in offset calculations }
  1204. if int64(l)>high(asizeint)-sym.fieldoffset then
  1205. begin
  1206. Message(sym_e_segment_too_large);
  1207. _datasize:=high(asizeint);
  1208. databitsize:=high(asizeint);
  1209. end
  1210. else
  1211. begin
  1212. databitsize:=sym.fieldoffset+l;
  1213. _datasize:=(databitsize+7) div 8;
  1214. end;
  1215. { rest is not applicable }
  1216. exit;
  1217. end;
  1218. else
  1219. begin
  1220. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1221. if l>high(asizeint)-sym.fieldoffset then
  1222. begin
  1223. Message(sym_e_segment_too_large);
  1224. _datasize:=high(asizeint);
  1225. end
  1226. else
  1227. _datasize:=sym.fieldoffset+l;
  1228. { Calc alignment needed for this record }
  1229. alignrecord(sym.fieldoffset,varalign);
  1230. end;
  1231. end;
  1232. end;
  1233. function field_alignment_compare(item1, item2: pointer): integer;
  1234. var
  1235. field1: tfieldvarsym absolute item1;
  1236. field2: tfieldvarsym absolute item2;
  1237. begin
  1238. { we don't care about static fields, those become global variables }
  1239. if (sp_static in field1.symoptions) or
  1240. (sp_static in field2.symoptions) then
  1241. exit(0);
  1242. { sort from large to small alignment, and in case of the same alignment
  1243. in declaration order (items declared close together are possibly
  1244. also related and hence possibly used together -> putting them next
  1245. to each other can improve cache behaviour) }
  1246. result:=field2.vardef.alignment-field1.vardef.alignment;
  1247. if result=0 then
  1248. result:=field1.fieldoffset-field2.fieldoffset;
  1249. end;
  1250. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1251. var
  1252. fieldvs, insertfieldvs: tfieldvarsym;
  1253. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1254. i, j, bestfieldindex: longint;
  1255. globalfieldalignment,
  1256. prevglobalfieldalignment,
  1257. newfieldalignment: shortint;
  1258. changed: boolean;
  1259. begin
  1260. if maybereorder and
  1261. (cs_opt_reorder_fields in current_settings.optimizerswitches) and
  1262. (list.count>1) then
  1263. begin
  1264. { assign dummy field offsets so we can know their order in the
  1265. sorting routine }
  1266. for i:=0 to list.count-1 do
  1267. begin
  1268. fieldvs:=tfieldvarsym(list[i]);
  1269. if sp_static in fieldvs.symoptions then
  1270. continue;
  1271. fieldvs.fieldoffset:=i;
  1272. end;
  1273. { sort the non-class fields to minimise losses due to alignment }
  1274. list.sort(@field_alignment_compare);
  1275. { now fill up gaps caused by alignment skips with smaller fields
  1276. where possible }
  1277. repeat
  1278. i:=0;
  1279. base:=_datasize;
  1280. globalfieldalignment:=fieldalignment;
  1281. changed:=false;
  1282. while i<list.count do
  1283. begin
  1284. fieldvs:=tfieldvarsym(list[i]);
  1285. if sp_static in fieldvs.symoptions then
  1286. begin
  1287. inc(i);
  1288. continue;
  1289. end;
  1290. prevglobalfieldalignment:=globalfieldalignment;
  1291. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1292. newfieldalignment:=globalfieldalignment;
  1293. { size of the gap between the end of the previous field and
  1294. the start of the current one }
  1295. space:=fieldoffset-base;
  1296. bestspaceleft:=space;
  1297. while space>0 do
  1298. begin
  1299. bestfieldindex:=-1;
  1300. bestinsertfieldoffset:=-1;
  1301. for j:=i+1 to list.count-1 do
  1302. begin
  1303. insertfieldvs:=tfieldvarsym(list[j]);
  1304. if sp_static in insertfieldvs.symoptions then
  1305. continue;
  1306. insertfieldsize:=insertfieldvs.getsize;
  1307. { can the new field fit possibly in the gap? }
  1308. if insertfieldsize<=space then
  1309. begin
  1310. { restore globalfieldalignment to situation before
  1311. the original field was inserted }
  1312. globalfieldalignment:=prevglobalfieldalignment;
  1313. { at what offset would it be inserted? (this new
  1314. field has its own alignment requirements, which
  1315. may make it impossible to fit after all) }
  1316. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1317. globalfieldalignment:=prevglobalfieldalignment;
  1318. { taking into account the alignment, does it still
  1319. fit and if so, does it fit better than the
  1320. previously found best fit? }
  1321. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1322. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1323. begin
  1324. { new best fit }
  1325. bestfieldindex:=j;
  1326. bestinsertfieldoffset:=insertfieldoffset;
  1327. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1328. if bestspaceleft=0 then
  1329. break;
  1330. end;
  1331. end;
  1332. end;
  1333. { if we didn't find any field to fit, stop trying for this
  1334. gap }
  1335. if bestfieldindex=-1 then
  1336. break;
  1337. changed:=true;
  1338. { we found a field to insert -> adjust the new base
  1339. address }
  1340. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1341. { update globalfieldalignment for this newly inserted
  1342. field }
  1343. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1344. { move the new field before the current one }
  1345. list.move(bestfieldindex,i);
  1346. { and skip the new field (which is now at position i) }
  1347. inc(i);
  1348. { there may be more space left -> continue }
  1349. space:=bestspaceleft;
  1350. end;
  1351. if base>fieldoffset then
  1352. internalerror(2012071302);
  1353. { check the next field }
  1354. base:=fieldoffset+fieldvs.getsize;
  1355. { since the original field had the same or greater alignment
  1356. than anything we inserted before it, the global field
  1357. alignment is still the same now as it was originally after
  1358. inserting that field }
  1359. globalfieldalignment:=newfieldalignment;
  1360. inc(i);
  1361. end;
  1362. { there may be small gaps left *before* inserted fields }
  1363. until not changed;
  1364. end;
  1365. { reset the dummy field offsets }
  1366. for i:=0 to list.count-1 do
  1367. begin
  1368. fieldvs:=tfieldvarsym(list[i]);
  1369. if sp_static in fieldvs.symoptions then
  1370. continue;
  1371. fieldvs.fieldoffset:=-1;
  1372. end;
  1373. { finally, set the actual field offsets }
  1374. for i:=0 to list.count-1 do
  1375. begin
  1376. fieldvs:=tfieldvarsym(list[i]);
  1377. { static data fields are already inserted in the globalsymtable }
  1378. if not(sp_static in fieldvs.symoptions) then
  1379. begin
  1380. { read_record_fields already set the visibility of the fields,
  1381. because a single list can contain symbols with different
  1382. visibility }
  1383. addfield(fieldvs,fieldvs.visibility);
  1384. end;
  1385. end;
  1386. end;
  1387. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1388. var
  1389. i: longint;
  1390. sym: tsym;
  1391. begin
  1392. { there could be multiple fields in case of a variant record }
  1393. if (defowner.typ=recorddef) and
  1394. trecorddef(defowner).isunion then
  1395. internalerror(2014090403);
  1396. for i:=0 to SymList.count-1 do
  1397. begin
  1398. sym:=tsym(symlist[i]);
  1399. if is_normal_fieldvarsym(sym) and
  1400. (tfieldvarsym(sym).fieldoffset>=offset) then
  1401. begin
  1402. result:=tfieldvarsym(sym);
  1403. exit;
  1404. end;
  1405. end;
  1406. result:=nil;
  1407. end;
  1408. procedure tabstractrecordsymtable.addalignmentpadding;
  1409. var
  1410. padded_datasize: asizeint;
  1411. begin
  1412. { make the record size aligned correctly so it can be
  1413. used as elements in an array. For C records we
  1414. use the fieldalignment, because that is updated with the
  1415. used alignment. }
  1416. if (padalignment = 1) then
  1417. case usefieldalignment of
  1418. C_alignment:
  1419. padalignment:=fieldalignment;
  1420. { bitpacked }
  1421. bit_alignment:
  1422. padalignment:=1;
  1423. { mac68k: always round to multiple of 2 }
  1424. mac68k_alignment:
  1425. padalignment:=2;
  1426. { default/no packrecords specified }
  1427. 0:
  1428. padalignment:=recordalignment
  1429. { specific packrecords setting -> use as upper limit }
  1430. else
  1431. padalignment:=min(recordalignment,usefieldalignment);
  1432. end;
  1433. padded_datasize:=align(_datasize,padalignment);
  1434. _paddingsize:=padded_datasize-_datasize;
  1435. _datasize:=padded_datasize;
  1436. end;
  1437. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1438. begin
  1439. { Enums must also be available outside the record scope,
  1440. insert in the owner of this symtable }
  1441. if def.typ=enumdef then
  1442. defowner.owner.insertdef(def)
  1443. else
  1444. inherited insertdef(def);
  1445. end;
  1446. function tabstractrecordsymtable.is_packed: boolean;
  1447. begin
  1448. result:=usefieldalignment=bit_alignment;
  1449. end;
  1450. function tabstractrecordsymtable.has_double_field(out def1,def2:tdef; out offset:integer): integer;
  1451. var
  1452. i,cnt: longint;
  1453. currentsymlist: TFPHashObjectList;
  1454. sym: tfieldvarsym;
  1455. begin
  1456. has_double_field := 0;
  1457. offset := 0;
  1458. if (defowner.typ=recorddef) and
  1459. trecorddef(defowner).isunion then
  1460. exit;
  1461. currentsymlist:=symlist;
  1462. if currentsymlist = nil then
  1463. exit;
  1464. if currentsymlist.Count <> 2 then
  1465. exit;
  1466. if is_normal_fieldvarsym(tsym(currentsymlist[0])) then
  1467. begin
  1468. sym:=tfieldvarsym(currentsymlist[0]);
  1469. def1 := sym.vardef;
  1470. end
  1471. else
  1472. exit;
  1473. if is_normal_fieldvarsym(tsym(currentsymlist[1])) then
  1474. begin
  1475. sym:=tfieldvarsym(currentsymlist[1]);
  1476. def2 := sym.vardef;
  1477. end
  1478. else
  1479. exit;
  1480. offset := sym.fieldoffset;
  1481. if def2.typ = def1.typ then
  1482. cnt := 2
  1483. else
  1484. cnt := 1;
  1485. if offset = 0 then
  1486. cnt := 0;
  1487. has_double_field := cnt;
  1488. end;
  1489. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1490. var
  1491. i: longint;
  1492. currentsymlist: TFPHashObjectList;
  1493. currentdef: tdef;
  1494. sym: tfieldvarsym;
  1495. begin
  1496. result:=false;
  1497. def:=generrordef;
  1498. { If a record contains a union, it does not contain a "single
  1499. non-composite field" in the context of certain ABIs requiring
  1500. special treatment for such records }
  1501. if (defowner.typ=recorddef) and
  1502. trecorddef(defowner).isunion then
  1503. exit;
  1504. { a record/object can contain other things than fields }
  1505. currentsymlist:=symlist;
  1506. { recurse in arrays and records }
  1507. while true do
  1508. begin
  1509. sym:=nil;
  1510. { record has one field? }
  1511. for i:=0 to currentsymlist.Count-1 do
  1512. begin
  1513. if is_normal_fieldvarsym(tsym(currentsymlist[i])) then
  1514. begin
  1515. if result then
  1516. begin
  1517. result:=false;
  1518. exit;
  1519. end;
  1520. result:=true;
  1521. sym:=tfieldvarsym(currentsymlist[i]);
  1522. end;
  1523. end;
  1524. if assigned(sym) then
  1525. begin
  1526. { if the field is an array, does it contain one element? }
  1527. currentdef:=sym.vardef;
  1528. while (currentdef.typ=arraydef) and
  1529. not is_special_array(currentdef) do
  1530. begin
  1531. if tarraydef(currentdef).elecount<>1 then
  1532. begin
  1533. result:=false;
  1534. exit;
  1535. end;
  1536. currentdef:=tarraydef(currentdef).elementdef;
  1537. end;
  1538. { if the array element is again a record, continue descending }
  1539. if currentdef.typ=recorddef then
  1540. begin
  1541. { the record might be empty, so reset the result until we've
  1542. really found something }
  1543. result:=false;
  1544. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1545. end
  1546. else
  1547. begin
  1548. { otherwise we found the type of the single element }
  1549. def:=currentdef;
  1550. exit;
  1551. end;
  1552. end
  1553. else
  1554. exit
  1555. end;
  1556. end;
  1557. procedure tabstractrecordsymtable.do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  1558. var
  1559. sym : tsym absolute data;
  1560. fsym : tfieldvarsym absolute data;
  1561. mop : tmanagementoperator;
  1562. entry : pmanagementoperator_offset_entry;
  1563. sublist : tfplist;
  1564. i : longint;
  1565. begin
  1566. if not is_normal_fieldvarsym(sym) then
  1567. exit;
  1568. if not is_record(fsym.vardef) and not is_object(fsym.vardef) and not is_cppclass(fsym.vardef) then
  1569. exit;
  1570. mop:=tmanagementoperator(ptruint(arg));
  1571. if not assigned(mop_list[mop]) then
  1572. internalerror(2018082303);
  1573. if is_record(fsym.vardef) then
  1574. begin
  1575. if mop in trecordsymtable(trecorddef(fsym.vardef).symtable).managementoperators then
  1576. begin
  1577. new(entry);
  1578. entry^.pd:=search_management_operator(mop,fsym.vardef);
  1579. if not assigned(entry^.pd) then
  1580. internalerror(2018082302);
  1581. entry^.offset:=fsym.fieldoffset;
  1582. mop_list[mop].add(entry);
  1583. end;
  1584. end;
  1585. sublist:=tfplist.create;
  1586. tabstractrecordsymtable(tabstractrecorddef(fsym.vardef).symtable).get_managementoperator_offset_list(mop,sublist);
  1587. mop_list[mop].capacity:=mop_list[mop].count+sublist.count;
  1588. for i:=0 to sublist.count-1 do
  1589. begin
  1590. entry:=pmanagementoperator_offset_entry(sublist[i]);
  1591. entry^.offset:=entry^.offset+fsym.fieldoffset;
  1592. mop_list[mop].add(entry);
  1593. end;
  1594. { we don't need to remove the entries as they become part of list }
  1595. sublist.free;
  1596. end;
  1597. procedure tabstractrecordsymtable.get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  1598. var
  1599. i : longint;
  1600. entry,entrycopy : pmanagementoperator_offset_entry;
  1601. begin
  1602. if not assigned(list) then
  1603. internalerror(2018082301);
  1604. if mop=mop_none then
  1605. exit;
  1606. if not (mop in has_fields_with_mop) then
  1607. { none of the fields or one of the field's fields has the requested operator }
  1608. exit;
  1609. if not assigned(mop_list[mop]) then
  1610. begin
  1611. mop_list[mop]:=tfplist.create;
  1612. SymList.ForEachCall(@do_get_managementoperator_offset_list,pointer(ptruint(mop)));
  1613. end;
  1614. for i:=0 to mop_list[mop].count-1 do
  1615. begin
  1616. entry:=pmanagementoperator_offset_entry(mop_list[mop][i]);
  1617. New(entrycopy);
  1618. entrycopy^:=entry^;
  1619. list.add(entrycopy);
  1620. end;
  1621. end;
  1622. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1623. begin
  1624. _datasize:=val;
  1625. if (usefieldalignment=bit_alignment) then
  1626. { can overflow in non bitpacked records }
  1627. databitsize:=val*8;
  1628. end;
  1629. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1630. var
  1631. l : asizeint;
  1632. varalignfield,
  1633. varalign : shortint;
  1634. vardef : tdef;
  1635. begin
  1636. { Calculate field offset }
  1637. l:=sym.getsize;
  1638. vardef:=sym.vardef;
  1639. varalign:=vardef.structalignment;
  1640. case usefieldalignment of
  1641. bit_alignment:
  1642. { has to be handled separately }
  1643. internalerror(2012071301);
  1644. C_alignment:
  1645. begin
  1646. { Calc the alignment size for C style records }
  1647. if (varalign>4) and
  1648. ((varalign mod 4)<>0) and
  1649. (vardef.typ=arraydef) then
  1650. Message1(sym_w_wrong_C_pack,vardef.typename);
  1651. if varalign=0 then
  1652. varalign:=l;
  1653. if (globalfieldalignment<current_settings.alignment.maxCrecordalign) then
  1654. begin
  1655. if (varalign>16) and (globalfieldalignment<32) then
  1656. globalfieldalignment:=32
  1657. else if (varalign>12) and (globalfieldalignment<16) then
  1658. globalfieldalignment:=16
  1659. { 12 is needed for long double }
  1660. else if (varalign>8) and (globalfieldalignment<12) then
  1661. globalfieldalignment:=12
  1662. else if (varalign>4) and (globalfieldalignment<8) then
  1663. globalfieldalignment:=8
  1664. else if (varalign>2) and (globalfieldalignment<4) then
  1665. globalfieldalignment:=4
  1666. else if (varalign>1) and (globalfieldalignment<2) then
  1667. globalfieldalignment:=2;
  1668. end;
  1669. globalfieldalignment:=min(globalfieldalignment,current_settings.alignment.maxCrecordalign);
  1670. end;
  1671. mac68k_alignment:
  1672. begin
  1673. { mac68k alignment (C description):
  1674. * char is aligned to 1 byte
  1675. * everything else (except vector) is aligned to 2 bytes
  1676. * vector is aligned to 16 bytes
  1677. }
  1678. if l>1 then
  1679. globalfieldalignment:=2
  1680. else
  1681. globalfieldalignment:=1;
  1682. varalign:=2;
  1683. end;
  1684. end;
  1685. if varalign=0 then
  1686. varalign:=size_2_align(l);
  1687. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1688. result:=align(base,varalignfield);
  1689. end;
  1690. function tabstractrecordsymtable.iscurrentunit: boolean;
  1691. begin
  1692. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1693. end;
  1694. {****************************************************************************
  1695. TRecordSymtable
  1696. ****************************************************************************}
  1697. constructor trecordsymtable.create(const n:string;usealign,recordminalign:shortint);
  1698. begin
  1699. inherited create(n,usealign,recordminalign);
  1700. symtabletype:=recordsymtable;
  1701. end;
  1702. { this procedure is reserved for inserting case variant into
  1703. a record symtable }
  1704. { the offset is the location of the start of the variant
  1705. and datasize and dataalignment corresponds to
  1706. the complete size (see code in pdecl unit) PM }
  1707. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : asizeint);
  1708. var
  1709. sym : tsym;
  1710. def : tdef;
  1711. i : integer;
  1712. varalignrecord,varalign,
  1713. storesize,storealign : asizeint;
  1714. bitsize: tcgint;
  1715. begin
  1716. storesize:=_datasize;
  1717. storealign:=fieldalignment;
  1718. _datasize:=offset;
  1719. if (usefieldalignment=bit_alignment) then
  1720. databitsize:=offset*8;
  1721. { We move the ownership of the defs and symbols to the new recordsymtable.
  1722. The old unionsymtable keeps the references, but doesn't own the
  1723. objects anymore }
  1724. unionst.DefList.OwnsObjects:=false;
  1725. unionst.SymList.OwnsObjects:=false;
  1726. { copy symbols }
  1727. for i:=0 to unionst.SymList.Count-1 do
  1728. begin
  1729. sym:=TSym(unionst.SymList[i]);
  1730. if not is_normal_fieldvarsym(sym) then
  1731. internalerror(200601272);
  1732. if tfieldvarsym(sym).fieldoffset=0 then
  1733. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1734. { add to this record symtable, checking for duplicate names }
  1735. // unionst.SymList.List.List^[i].Data:=nil;
  1736. insertsym(sym);
  1737. varalign:=tfieldvarsym(sym).vardef.alignment;
  1738. if varalign=0 then
  1739. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1740. { retrieve size }
  1741. if (usefieldalignment=bit_alignment) then
  1742. begin
  1743. { bit packed records are limited to high(aint) bits }
  1744. { instead of bytes to avoid double precision }
  1745. { arithmetic in offset calculations }
  1746. if is_ordinal(tfieldvarsym(sym).vardef) then
  1747. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1748. else
  1749. begin
  1750. bitsize:=tfieldvarsym(sym).getsize;
  1751. if (bitsize>high(asizeint) div 8) then
  1752. Message(sym_e_segment_too_large);
  1753. bitsize:=bitsize*8;
  1754. end;
  1755. if bitsize>high(asizeint)-databitsize then
  1756. begin
  1757. Message(sym_e_segment_too_large);
  1758. _datasize:=high(asizeint);
  1759. databitsize:=high(asizeint);
  1760. end
  1761. else
  1762. begin
  1763. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1764. _datasize:=(databitsize+7) div 8;
  1765. end;
  1766. tfieldvarsym(sym).fieldoffset:=databitsize;
  1767. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1768. end
  1769. else
  1770. begin
  1771. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1772. begin
  1773. Message(sym_e_segment_too_large);
  1774. _datasize:=high(asizeint);
  1775. end
  1776. else
  1777. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1778. { update address }
  1779. tfieldvarsym(sym).fieldoffset:=_datasize;
  1780. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1781. end;
  1782. { update alignment of this record }
  1783. if (usefieldalignment<>C_alignment) and
  1784. (usefieldalignment<>mac68k_alignment) then
  1785. recordalignment:=max(recordalignment,varalignrecord);
  1786. end;
  1787. { update alignment for C records }
  1788. if (usefieldalignment=C_alignment) and
  1789. (usefieldalignment<>mac68k_alignment) then
  1790. recordalignment:=max(recordalignment,unionst.recordalignment);
  1791. { Register defs in the new record symtable }
  1792. for i:=0 to unionst.DefList.Count-1 do
  1793. begin
  1794. def:=TDef(unionst.DefList[i]);
  1795. def.ChangeOwner(self);
  1796. end;
  1797. _datasize:=storesize;
  1798. fieldalignment:=storealign;
  1799. { If a record contains a union, it does not contain a "single
  1800. non-composite field" in the context of certain ABIs requiring
  1801. special treatment for such records }
  1802. if defowner.typ=recorddef then
  1803. trecorddef(defowner).isunion:=true;
  1804. end;
  1805. procedure trecordsymtable.includemanagementoperator(mop:tmanagementoperator);
  1806. begin
  1807. if mop in managementoperators then
  1808. exit;
  1809. include(managementoperators,mop);
  1810. end;
  1811. {****************************************************************************
  1812. TObjectSymtable
  1813. ****************************************************************************}
  1814. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign:shortint);
  1815. begin
  1816. inherited create(n,usealign,recordminalign);
  1817. symtabletype:=ObjectSymtable;
  1818. defowner:=adefowner;
  1819. end;
  1820. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1821. var
  1822. hsym: tsym;
  1823. warn: boolean;
  1824. begin
  1825. result:=false;
  1826. if not assigned(defowner) then
  1827. internalerror(200602061);
  1828. { procsym and propertysym have special code
  1829. to override values in inherited classes. For other
  1830. symbols check for duplicates (but for internal symbols only in this
  1831. symtable, not the whole hierarchy) }
  1832. if not(sym.typ in [procsym,propertysym]) and
  1833. not (sp_internal in tsym(sym).symoptions) then
  1834. begin
  1835. { but private ids can be reused }
  1836. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1837. if assigned(hsym) and
  1838. (
  1839. (
  1840. not(m_delphi in current_settings.modeswitches) and
  1841. is_visible_for_object(hsym,tobjectdef(defowner))
  1842. ) or
  1843. (
  1844. { In Delphi, you can repeat members of a parent class. You can't }
  1845. { do this for objects however, and you (obviouly) can't }
  1846. { declare two fields with the same name in a single class }
  1847. (m_delphi in current_settings.modeswitches) and
  1848. (
  1849. is_object(tdef(defowner)) or
  1850. (hsym.owner = self)
  1851. )
  1852. )
  1853. ) then
  1854. begin
  1855. { only warn when a parameter/local variable in a method
  1856. conflicts with a category method, because this can easily
  1857. happen due to all possible categories being imported via
  1858. CocoaAll }
  1859. warn:=
  1860. (is_objccategory(tdef(hsym.owner.defowner)) or
  1861. is_classhelper(tdef(hsym.owner.defowner))) and
  1862. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1863. DuplicateSym(hashedid,sym,hsym,warn);
  1864. result:=true;
  1865. end;
  1866. end
  1867. else
  1868. result:=inherited checkduplicate(hashedid,sym);
  1869. end;
  1870. {$ifdef llvm}
  1871. {****************************************************************************
  1872. tLlvmShadowSymtableEntry
  1873. ****************************************************************************}
  1874. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1875. begin
  1876. fdef:=def;
  1877. ffieldoffset:=fieldoffset;
  1878. end;
  1879. {****************************************************************************
  1880. TLlvmShadowSymtable
  1881. ****************************************************************************}
  1882. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1883. begin
  1884. result:=get_by_llvm_index(f.llvmfieldnr)
  1885. end;
  1886. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1887. begin
  1888. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1889. end;
  1890. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1891. begin
  1892. equivst:=st;
  1893. curroffset:=0;
  1894. symdeflist:=tfpobjectlist.create(true);
  1895. generate;
  1896. end;
  1897. destructor tllvmshadowsymtable.destroy;
  1898. begin
  1899. symdeflist.free;
  1900. end;
  1901. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1902. var
  1903. sizectr,
  1904. tmpsize: aint;
  1905. begin
  1906. case equivst.usefieldalignment of
  1907. bit_alignment:
  1908. begin
  1909. { curoffset: bit address after the previous field. }
  1910. { llvm has no special support for bitfields in records, }
  1911. { so we replace them with plain bytes. }
  1912. { as soon as a single bit of a byte is allocated, we }
  1913. { allocate the byte in the llvm shadow record }
  1914. if (fieldoffset>curroffset) then
  1915. curroffset:=align(curroffset,8);
  1916. { fields in bitpacked records always start either right }
  1917. { after the previous one, or at the next byte boundary. }
  1918. if (curroffset<>fieldoffset) then
  1919. internalerror(2008051002);
  1920. if is_ordinal(vardef) then
  1921. begin
  1922. tmpsize:=vardef.packedbitsize;
  1923. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1924. inc(curroffset,tmpsize);
  1925. tmpsize:=0;
  1926. while sizectr<>0 do
  1927. begin
  1928. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1929. dec(sizectr);
  1930. inc(tmpsize);
  1931. end;
  1932. end
  1933. else
  1934. begin
  1935. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1936. if not(derefclass) then
  1937. inc(curroffset,vardef.size*8)
  1938. else
  1939. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1940. end;
  1941. end
  1942. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1943. begin
  1944. { curoffset: address right after the previous field }
  1945. while (fieldoffset>curroffset) do
  1946. begin
  1947. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1948. inc(curroffset);
  1949. end;
  1950. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1951. if not(derefclass) then
  1952. inc(curroffset,vardef.size)
  1953. else
  1954. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1955. end
  1956. else
  1957. { default for llvm, don't add explicit padding }
  1958. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1959. end
  1960. end;
  1961. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1962. begin
  1963. if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1964. begin
  1965. if equivst.usefieldalignment=bit_alignment then
  1966. curroffset:=align(curroffset,8) div 8;
  1967. { add padding fields }
  1968. while (finalsize>curroffset) do
  1969. begin
  1970. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1971. inc(curroffset);
  1972. end;
  1973. end;
  1974. end;
  1975. function field_offset_compare(item1, item2: pointer): integer;
  1976. var
  1977. field1: tfieldvarsym absolute item1;
  1978. field2: tfieldvarsym absolute item2;
  1979. begin
  1980. result:=field1.fieldoffset-field2.fieldoffset;
  1981. end;
  1982. procedure tllvmshadowsymtable.preprocess(out tempsymlist, variantstarts: tfplist);
  1983. var
  1984. fieldvs: tfieldvarsym;
  1985. lastvariantstartoffset, prevfieldoffset: aint;
  1986. newalignment: aint;
  1987. i, j: longint;
  1988. sorttempsymlist: boolean;
  1989. begin
  1990. i:=0;
  1991. variantstarts:=nil;
  1992. tempsymlist:=tfplist.create;
  1993. sorttempsymlist:=false;
  1994. prevfieldoffset:=-1;
  1995. while (i<equivst.symlist.count) do
  1996. begin
  1997. if not is_normal_fieldvarsym(tsym(equivst.symlist[i])) then
  1998. begin
  1999. inc(i);
  2000. continue;
  2001. end;
  2002. fieldvs:=tfieldvarsym(equivst.symlist[i]);
  2003. tempsymlist.Add(fieldvs);
  2004. { a "better" algorithm might be to use the largest }
  2005. { variant in case of (bit)packing, since then }
  2006. { alignment doesn't matter }
  2007. if (vo_is_first_field in fieldvs.varoptions) then
  2008. begin
  2009. { we assume that all fields are processed in order. }
  2010. if assigned(variantstarts) then
  2011. lastvariantstartoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  2012. else
  2013. begin
  2014. lastvariantstartoffset:=-1;
  2015. variantstarts:=tfplist.create;
  2016. end;
  2017. { new variant at same level as last one: use if higher alignment }
  2018. if (lastvariantstartoffset=fieldvs.fieldoffset) then
  2019. begin
  2020. if (equivst.usefieldalignment<>bit_alignment) then
  2021. newalignment:=used_align(fieldvs.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  2022. else
  2023. newalignment:=1;
  2024. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  2025. variantstarts[variantstarts.count-1]:=fieldvs;
  2026. end
  2027. { variant at deeper level than last one -> add }
  2028. else if (lastvariantstartoffset<fieldvs.fieldoffset) then
  2029. variantstarts.add(fieldvs)
  2030. else
  2031. begin
  2032. { a variant at a less deep level, so backtrack }
  2033. j:=variantstarts.count-2;
  2034. while (j>=0) do
  2035. begin
  2036. if (tfieldvarsym(variantstarts[j]).fieldoffset=fieldvs.fieldoffset) then
  2037. break;
  2038. dec(j);
  2039. end;
  2040. if (j<0) then
  2041. internalerror(2008051003);
  2042. { new variant has higher alignment? }
  2043. if (equivst.fieldalignment<>bit_alignment) then
  2044. newalignment:=used_align(fieldvs.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  2045. else
  2046. newalignment:=1;
  2047. { yes, replace and remove previous nested variants }
  2048. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  2049. begin
  2050. variantstarts[j]:=fieldvs;
  2051. variantstarts.count:=j+1;
  2052. end
  2053. { no, skip this variant }
  2054. else
  2055. begin
  2056. inc(i);
  2057. while (i<equivst.symlist.count) and
  2058. (not is_normal_fieldvarsym(tsym(equivst.symlist[i])) or
  2059. (tfieldvarsym(equivst.symlist[i]).fieldoffset>fieldvs.fieldoffset)) do
  2060. begin
  2061. if is_normal_fieldvarsym(tsym(equivst.symlist[i])) then
  2062. tempsymlist.Add(equivst.symlist[i]);
  2063. inc(i);
  2064. end;
  2065. continue;
  2066. end;
  2067. end;
  2068. end;
  2069. if not assigned(variantstarts) and
  2070. (fieldvs.fieldoffset<prevfieldoffset) then
  2071. sorttempsymlist:=true;
  2072. prevfieldoffset:=fieldvs.fieldoffset;
  2073. inc(i);
  2074. end;
  2075. if sorttempsymlist then
  2076. tempsymlist.Sort(@field_offset_compare);
  2077. end;
  2078. procedure tllvmshadowsymtable.buildtable(tempsymlist, variantstarts: tfplist);
  2079. var
  2080. lastvaroffsetprocessed: aint;
  2081. i, symcount, varcount: longint;
  2082. fieldvs: tfieldvarsym;
  2083. begin
  2084. { if it's an object/class, the first entry is the parent (if there is one) }
  2085. if (equivst.symtabletype=objectsymtable) and
  2086. assigned(tobjectdef(equivst.defowner).childof) then
  2087. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  2088. symcount:=tempsymlist.count;
  2089. varcount:=0;
  2090. i:=0;
  2091. lastvaroffsetprocessed:=-1;
  2092. while (i<symcount) do
  2093. begin
  2094. fieldvs:=tfieldvarsym(tempsymlist[i]);
  2095. { start of a new variant? }
  2096. if (vo_is_first_field in fieldvs.varoptions) then
  2097. begin
  2098. { if we want to process the same variant offset twice, it means that we }
  2099. { got to the end and are trying to process the next variant part -> stop }
  2100. if (fieldvs.fieldoffset<=lastvaroffsetprocessed) then
  2101. break;
  2102. if (varcount>=variantstarts.count) then
  2103. internalerror(2008051005);
  2104. { new variant part -> use the one with the biggest alignment }
  2105. fieldvs:=tfieldvarsym(variantstarts[varcount]);
  2106. i:=tempsymlist.indexof(fieldvs);
  2107. lastvaroffsetprocessed:=fieldvs.fieldoffset;
  2108. inc(varcount);
  2109. if (i<0) then
  2110. internalerror(2008051004);
  2111. end;
  2112. appenddefoffset(fieldvs.vardef,fieldvs.fieldoffset,false);
  2113. inc(i);
  2114. end;
  2115. addalignmentpadding(equivst.datasize);
  2116. end;
  2117. procedure tllvmshadowsymtable.buildmapping(tempsymlist, variantstarts: tfplist);
  2118. var
  2119. fieldvs: tfieldvarsym;
  2120. i, varcount: longint;
  2121. shadowindex: longint;
  2122. symcount : longint;
  2123. begin
  2124. varcount:=0;
  2125. shadowindex:=0;
  2126. symcount:=tempsymlist.count;
  2127. i:=0;
  2128. while (i<symcount) do
  2129. begin
  2130. fieldvs:=tfieldvarsym(tempsymlist[i]);
  2131. { start of a new variant? }
  2132. if (vo_is_first_field in fieldvs.varoptions) then
  2133. begin
  2134. { back up to a less deeply nested variant level? }
  2135. while fieldvs.fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset do
  2136. dec(varcount);
  2137. { it's possible that some variants are more deeply nested than the
  2138. one we recorded in the shadowsymtable (since we recorded the one
  2139. with the biggest alignment, not necessarily the biggest one in size
  2140. }
  2141. if fieldvs.fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset then
  2142. varcount:=variantstarts.count-1
  2143. else if fieldvs.fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset then
  2144. internalerror(2008051006);
  2145. { reset the shadowindex to the start of this variant. }
  2146. { in case the llvmfieldnr is not (yet) set for this }
  2147. { field, shadowindex will simply be reset to zero and }
  2148. { we'll start searching from the start of the record }
  2149. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  2150. if (varcount<pred(variantstarts.count)) then
  2151. inc(varcount);
  2152. end;
  2153. { find the last shadowfield whose offset <= the current field's offset }
  2154. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<fieldvs.fieldoffset) and
  2155. (shadowindex<symdeflist.count-1) and
  2156. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=fieldvs.fieldoffset) do
  2157. inc(shadowindex);
  2158. { set the field number and potential offset from that field (in case }
  2159. { of overlapping variants) }
  2160. fieldvs.llvmfieldnr:=shadowindex;
  2161. fieldvs.offsetfromllvmfield:=
  2162. fieldvs.fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  2163. inc(i);
  2164. end;
  2165. end;
  2166. procedure tllvmshadowsymtable.generate;
  2167. var
  2168. variantstarts, tempsymlist: tfplist;
  2169. begin
  2170. { first go through the entire record and }
  2171. { store the fieldvarsyms of the variants }
  2172. { with the highest alignment }
  2173. preprocess(tempsymlist, variantstarts);
  2174. { now go through the regular fields and the selected variants, }
  2175. { and add them to the llvm shadow record symtable }
  2176. buildtable(tempsymlist, variantstarts);
  2177. { finally map all original fields to the llvm definition }
  2178. buildmapping(tempsymlist, variantstarts);
  2179. variantstarts.free;
  2180. tempsymlist.free;
  2181. end;
  2182. {$endif llvm}
  2183. {****************************************************************************
  2184. TAbstractSubSymtable
  2185. ****************************************************************************}
  2186. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  2187. var
  2188. oldtyp : byte;
  2189. begin
  2190. oldtyp:=ppufile.entrytyp;
  2191. ppufile.entrytyp:=subentryid;
  2192. inherited ppuwrite(ppufile);
  2193. ppufile.entrytyp:=oldtyp;
  2194. end;
  2195. {****************************************************************************
  2196. TAbstractLocalSymtable
  2197. ****************************************************************************}
  2198. function tabstractlocalsymtable.count_locals:longint;
  2199. var
  2200. i : longint;
  2201. sym : tsym;
  2202. begin
  2203. result:=0;
  2204. for i:=0 to SymList.Count-1 do
  2205. begin
  2206. sym:=tsym(SymList[i]);
  2207. { Count only varsyms, but ignore the funcretsym }
  2208. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  2209. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  2210. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  2211. (tstoredsym(sym).refs>0)) then
  2212. inc(result);
  2213. end;
  2214. end;
  2215. function tabstractlocalsymtable.iscurrentunit: boolean;
  2216. begin
  2217. Result:=
  2218. assigned(defowner) and
  2219. defowner.owner.iscurrentunit;
  2220. end;
  2221. {****************************************************************************
  2222. TLocalSymtable
  2223. ****************************************************************************}
  2224. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  2225. begin
  2226. inherited create('');
  2227. defowner:=adefowner;
  2228. symtabletype:=localsymtable;
  2229. symtablelevel:=level;
  2230. end;
  2231. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2232. var
  2233. hsym : tsym;
  2234. begin
  2235. if not assigned(defowner) or
  2236. (defowner.typ<>procdef) then
  2237. internalerror(200602042);
  2238. result:=false;
  2239. hsym:=tsym(FindWithHash(hashedid));
  2240. if assigned(hsym) then
  2241. begin
  2242. { a local and the function can have the same
  2243. name in TP and Delphi, but RESULT not }
  2244. if (m_duplicate_names in current_settings.modeswitches) and
  2245. (hsym.typ in [absolutevarsym,localvarsym]) and
  2246. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  2247. not((m_result in current_settings.modeswitches) and
  2248. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  2249. HideSym(hsym)
  2250. else
  2251. DuplicateSym(hashedid,sym,hsym,false);
  2252. result:=true;
  2253. exit;
  2254. end;
  2255. { check also parasymtable, this needs to be done here because
  2256. of the special situation with the funcret sym that needs to be
  2257. hidden for tp and delphi modes }
  2258. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  2259. if assigned(hsym) then
  2260. begin
  2261. { a local and the function can have the same
  2262. name in TP and Delphi, but RESULT not }
  2263. if (m_duplicate_names in current_settings.modeswitches) and
  2264. (sym.typ in [absolutevarsym,localvarsym]) and
  2265. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  2266. not((m_result in current_settings.modeswitches) and
  2267. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  2268. Hidesym(sym)
  2269. else
  2270. DuplicateSym(hashedid,sym,hsym,false);
  2271. result:=true;
  2272. exit;
  2273. end;
  2274. { check ObjectSymtable, skip this for funcret sym because
  2275. that will always be positive because it has the same name
  2276. as the procsym }
  2277. if not is_funcret_sym(sym) and
  2278. (defowner.typ=procdef) and
  2279. assigned(tprocdef(defowner).struct) and
  2280. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2281. (
  2282. not(m_duplicate_names in current_settings.modeswitches) or
  2283. is_object(tprocdef(defowner).struct)
  2284. ) then
  2285. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2286. end;
  2287. {****************************************************************************
  2288. TParaSymtable
  2289. ****************************************************************************}
  2290. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2291. begin
  2292. inherited create('');
  2293. defowner:=adefowner;
  2294. symtabletype:=parasymtable;
  2295. symtablelevel:=level;
  2296. end;
  2297. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2298. begin
  2299. result:=inherited checkduplicate(hashedid,sym);
  2300. if result then
  2301. exit;
  2302. if not(m_duplicate_names in current_settings.modeswitches) and
  2303. assigned(defowner) and (defowner.typ=procdef) and
  2304. assigned(tprocdef(defowner).struct) and
  2305. assigned(tprocdef(defowner).owner) and
  2306. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2307. (
  2308. not(m_delphi in current_settings.modeswitches) or
  2309. is_object(tprocdef(defowner).struct)
  2310. ) then
  2311. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2312. end;
  2313. {****************************************************************************
  2314. TAbstractUniTSymtable
  2315. ****************************************************************************}
  2316. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2317. begin
  2318. inherited create(n);
  2319. moduleid:=id;
  2320. end;
  2321. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2322. var
  2323. hsym : tsym;
  2324. begin
  2325. result:=false;
  2326. hsym:=tsym(FindWithHash(hashedid));
  2327. if assigned(hsym) then
  2328. begin
  2329. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2330. exit;
  2331. if hsym.typ=symconst.namespacesym then
  2332. begin
  2333. case sym.typ of
  2334. symconst.namespacesym:;
  2335. symconst.unitsym:
  2336. begin
  2337. HideSym(sym); { if we add a unit and there is a namespace with the same name then hide the unit name and not the namespace }
  2338. tnamespacesym(hsym).unitsym:=tsym(sym);
  2339. end
  2340. else
  2341. HideSym(hsym);
  2342. end;
  2343. end
  2344. else
  2345. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2346. unit, the unit can then not be accessed anymore using
  2347. <unit>.<id>, so we can hide the symbol.
  2348. Do the same if we add a namespace and there is a unit with the same name }
  2349. if (hsym.typ=symconst.unitsym) and
  2350. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2351. begin
  2352. HideSym(hsym);
  2353. if sym.typ=symconst.namespacesym then
  2354. tnamespacesym(sym).unitsym:=tsym(hsym);
  2355. end
  2356. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2357. in this case, copy the isoindex and make the original symbol invisible }
  2358. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2359. and (tprogramparasym(hsym).isoindex<>0) then
  2360. begin
  2361. HideSym(hsym);
  2362. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2363. end
  2364. else if (m_iso in current_settings.modeswitches) and (hsym.typ=unitsym) then
  2365. HideSym(hsym)
  2366. else
  2367. DuplicateSym(hashedid,sym,hsym,false);
  2368. result:=true;
  2369. exit;
  2370. end;
  2371. end;
  2372. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2373. begin
  2374. result:=find(n);
  2375. if assigned(result)and(result.typ<>namespacesym)then
  2376. result:=nil;
  2377. end;
  2378. function tabstractuniTSymtable.iscurrentunit:boolean;
  2379. begin
  2380. result:=assigned(current_module) and
  2381. (
  2382. (current_module.globalsymtable=self) or
  2383. (current_module.localsymtable=self)
  2384. );
  2385. end;
  2386. function tabstractuniTSymtable.needs_init_final: boolean;
  2387. begin
  2388. if not init_final_check_done then
  2389. begin
  2390. result:=inherited needs_init_final;
  2391. if not result then
  2392. begin
  2393. result:=has_class_condestructors;
  2394. if result then
  2395. include(tableoptions,sto_needs_init_final);
  2396. end;
  2397. end;
  2398. result:=sto_needs_init_final in tableoptions;
  2399. end;
  2400. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2401. var
  2402. p:integer;
  2403. n,ns:string;
  2404. oldsym:TSymEntry;
  2405. begin
  2406. insertsym(sym);
  2407. n:=sym.realname;
  2408. p:=pos('.',n);
  2409. ns:='';
  2410. while p>0 do
  2411. begin
  2412. if ns='' then
  2413. ns:=copy(n,1,p-1)
  2414. else
  2415. ns:=ns+'.'+copy(n,1,p-1);
  2416. system.delete(n,1,p);
  2417. oldsym:=findnamespace(upper(ns));
  2418. if not assigned(oldsym) then
  2419. insertsym(cnamespacesym.create(ns));
  2420. p:=pos('.',n);
  2421. end;
  2422. end;
  2423. procedure CheckForClassConDestructors(p:TObject;arg:pointer);
  2424. var
  2425. result: pboolean absolute arg;
  2426. begin
  2427. if result^ then
  2428. exit;
  2429. if (tdef(p).typ in [objectdef,recorddef]) and
  2430. not (df_generic in tdef(p).defoptions) then
  2431. begin
  2432. { first check the class... }
  2433. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  2434. result^:=true;
  2435. { ... and then also check all subclasses }
  2436. if not result^ then
  2437. tabstractrecorddef(p).symtable.deflist.foreachcall(@CheckForClassConDestructors,arg);
  2438. end;
  2439. end;
  2440. function tabstractuniTSymtable.has_class_condestructors: boolean;
  2441. begin
  2442. result:=false;
  2443. deflist.foreachcall(@CheckForClassConDestructors,@result);
  2444. end;
  2445. {****************************************************************************
  2446. TStaticSymtable
  2447. ****************************************************************************}
  2448. constructor tstaticsymtable.create(const n : string;id:word);
  2449. begin
  2450. inherited create(n,id);
  2451. symtabletype:=staticsymtable;
  2452. symtablelevel:=main_program_level;
  2453. currentvisibility:=vis_private;
  2454. end;
  2455. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2456. begin
  2457. inherited ppuload(ppufile);
  2458. { now we can deref the syms and defs }
  2459. deref(false);
  2460. end;
  2461. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2462. begin
  2463. inherited ppuwrite(ppufile);
  2464. end;
  2465. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2466. begin
  2467. result:=inherited checkduplicate(hashedid,sym);
  2468. if not result and
  2469. (current_module.localsymtable=self) and
  2470. assigned(current_module.globalsymtable) then
  2471. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2472. end;
  2473. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2474. begin
  2475. result:=inherited findnamespace(n);
  2476. if not assigned(result) and
  2477. (current_module.localsymtable=self) and
  2478. assigned(current_module.globalsymtable) then
  2479. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2480. end;
  2481. {****************************************************************************
  2482. TGlobalSymtable
  2483. ****************************************************************************}
  2484. constructor tglobalsymtable.create(const n : string;id:word);
  2485. begin
  2486. inherited create(n,id);
  2487. symtabletype:=globalsymtable;
  2488. symtablelevel:=main_program_level;
  2489. end;
  2490. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2491. begin
  2492. inherited ppuload(ppufile);
  2493. { now we can deref the syms and defs }
  2494. deref(false);
  2495. end;
  2496. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2497. begin
  2498. { write the symtable entries }
  2499. inherited ppuwrite(ppufile);
  2500. end;
  2501. {*****************************************************************************
  2502. tspecializesymtable
  2503. *****************************************************************************}
  2504. constructor tspecializesymtable.create(const n : string;id:word);
  2505. begin
  2506. inherited create(n,id);
  2507. { the specialize symtable does not own the syms and defs as they are all
  2508. moved to a different symtable before the symtable is destroyed; this
  2509. avoids calls to "extract" }
  2510. symlist.ownsobjects:=false;
  2511. deflist.ownsobjects:=false;
  2512. end;
  2513. function tspecializesymtable.iscurrentunit: boolean;
  2514. begin
  2515. Result:=true;
  2516. end;
  2517. {****************************************************************************
  2518. TWITHSYMTABLE
  2519. ****************************************************************************}
  2520. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2521. begin
  2522. inherited create('');
  2523. symtabletype:=withsymtable;
  2524. withrefnode:=refnode;
  2525. { Replace SymList with the passed symlist }
  2526. SymList.free;
  2527. SymList:=ASymList;
  2528. defowner:=aowner;
  2529. end;
  2530. destructor twithsymtable.destroy;
  2531. begin
  2532. if refcount>1 then
  2533. exit;
  2534. withrefnode.free;
  2535. { Disable SymList because we don't Own it }
  2536. SymList:=nil;
  2537. inherited destroy;
  2538. end;
  2539. procedure twithsymtable.clear;
  2540. begin
  2541. { remove no entry from a withsymtable as it is only a pointer to the
  2542. recorddef or objectdef symtable }
  2543. end;
  2544. procedure twithsymtable.insertdef(def:TDefEntry);
  2545. begin
  2546. { Definitions can't be registered in the withsymtable
  2547. because the withsymtable is removed after the with block.
  2548. We can't easily solve it here because the next symtable in the
  2549. stack is not known. }
  2550. internalerror(200602046);
  2551. end;
  2552. {****************************************************************************
  2553. TSTT_ExceptionSymtable
  2554. ****************************************************************************}
  2555. constructor tstt_excepTSymtable.create;
  2556. begin
  2557. inherited create('');
  2558. symtabletype:=exceptsymtable;
  2559. end;
  2560. {****************************************************************************
  2561. TMacroSymtable
  2562. ****************************************************************************}
  2563. constructor tmacrosymtable.create(exported: boolean);
  2564. begin
  2565. inherited create('');
  2566. if exported then
  2567. symtabletype:=exportedmacrosymtable
  2568. else
  2569. symtabletype:=localmacrosymtable;
  2570. symtablelevel:=main_program_level;
  2571. end;
  2572. {****************************************************************************
  2573. TEnumSymtable
  2574. ****************************************************************************}
  2575. procedure tenumsymtable.insertsym(sym: TSymEntry; checkdup: boolean);
  2576. var
  2577. value: longint;
  2578. def: tenumdef;
  2579. begin
  2580. // defowner = nil only when we are loading from ppu
  2581. if defowner<>nil then
  2582. begin
  2583. { First entry? Then we need to set the minval }
  2584. value:=tenumsym(sym).value;
  2585. def:=tenumdef(defowner);
  2586. if SymList.count=0 then
  2587. begin
  2588. if value>0 then
  2589. def.has_jumps:=true;
  2590. def.setmin(value);
  2591. def.setmax(value);
  2592. end
  2593. else
  2594. begin
  2595. { check for jumps }
  2596. if value>def.max+1 then
  2597. def.has_jumps:=true;
  2598. { update low and high }
  2599. if def.min>value then
  2600. def.setmin(value);
  2601. if def.max<value then
  2602. def.setmax(value);
  2603. end;
  2604. end;
  2605. inherited insertsym(sym, checkdup);
  2606. end;
  2607. constructor tenumsymtable.create(adefowner: tdef);
  2608. begin
  2609. inherited Create('');
  2610. symtabletype:=enumsymtable;
  2611. defowner:=adefowner;
  2612. end;
  2613. {****************************************************************************
  2614. TArraySymtable
  2615. ****************************************************************************}
  2616. procedure tarraysymtable.insertdef(def: TDefEntry);
  2617. begin
  2618. { Enums must also be available outside the record scope,
  2619. insert in the owner of this symtable }
  2620. if def.typ=enumdef then
  2621. defowner.owner.insertdef(def)
  2622. else
  2623. inherited insertdef(def);
  2624. end;
  2625. constructor tarraysymtable.create(adefowner: tdef);
  2626. begin
  2627. inherited Create('');
  2628. symtabletype:=arraysymtable;
  2629. defowner:=adefowner;
  2630. end;
  2631. {*****************************************************************************
  2632. Helper Routines
  2633. *****************************************************************************}
  2634. function FullTypeName(def,otherdef:tdef):string;
  2635. var
  2636. s1,s2 : string;
  2637. begin
  2638. if def.typ in [objectdef,recorddef] then
  2639. s1:=tabstractrecorddef(def).RttiName
  2640. else
  2641. s1:=def.typename;
  2642. { When the names are the same try to include the unit name }
  2643. if assigned(otherdef) and
  2644. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2645. begin
  2646. s2:=otherdef.typename;
  2647. if upper(s1)=upper(s2) then
  2648. s1:=def.owner.realname^+'.'+s1;
  2649. end;
  2650. FullTypeName:=s1;
  2651. end;
  2652. function generate_nested_name(symtable:tsymtable;const delimiter:string):string;
  2653. begin
  2654. result:='';
  2655. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2656. begin
  2657. if (result='') then
  2658. if symtable.name<>nil then
  2659. result:=symtable.name^
  2660. else
  2661. else
  2662. if symtable.name<>nil then
  2663. result:=symtable.name^+delimiter+result
  2664. else
  2665. result:=delimiter+result;
  2666. symtable:=symtable.defowner.owner;
  2667. end;
  2668. end;
  2669. function generate_objectpascal_helper_key(def:tdef):TSymStr;
  2670. begin
  2671. if not assigned(def) then
  2672. internalerror(2013020501);
  2673. if def.typ in [recorddef,objectdef] then
  2674. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2675. else
  2676. result:=make_mangledname('',def.owner,def.typesym.name);
  2677. end;
  2678. procedure incompatibletypes(def1,def2:tdef);
  2679. begin
  2680. { When there is an errordef there is already an error message show }
  2681. if (def2.typ=errordef) or
  2682. (def1.typ=errordef) then
  2683. exit;
  2684. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2685. end;
  2686. procedure hidesym(sym:TSymEntry);
  2687. begin
  2688. sym.realname:='$hidden'+sym.realname;
  2689. tsym(sym).visibility:=vis_hidden;
  2690. end;
  2691. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2692. var
  2693. st : TSymtable;
  2694. filename : TIDString;
  2695. begin
  2696. if not warn then
  2697. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2698. else
  2699. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2700. { Write hint where the original symbol was found }
  2701. st:=finduniTSymtable(origsym.owner);
  2702. with tsym(origsym).fileinfo do
  2703. begin
  2704. if assigned(st) and
  2705. (st.symtabletype=globalsymtable) and
  2706. st.iscurrentunit then
  2707. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2708. else if assigned(st.name) then
  2709. begin
  2710. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2711. if filename<>'' then
  2712. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2713. else
  2714. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2715. end;
  2716. end;
  2717. { Rename duplicate sym to an unreachable name, but it can be
  2718. inserted in the symtable without errors }
  2719. inc(dupnr);
  2720. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2721. if assigned(dupsym) then
  2722. include(tsym(dupsym).symoptions,sp_implicitrename);
  2723. end;
  2724. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2725. begin
  2726. result:=false;
  2727. if not assigned(sym) or not (sym is tstoredsym) then
  2728. Internalerror(2011081101);
  2729. { For generics a dummy symbol without the parameter count is created
  2730. if such a symbol not yet exists so that different parts of the
  2731. parser can find that symbol. If that symbol is still a
  2732. undefineddef we replace the generic dummy symbol's
  2733. name with a "dup" name and use the new symbol as the generic dummy
  2734. symbol }
  2735. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2736. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2737. (m_delphi in current_settings.modeswitches) then
  2738. begin
  2739. inc(dupnr);
  2740. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2741. include(tsym(sym).symoptions,sp_implicitrename);
  2742. { we need to find the new symbol now if checking for a dummy }
  2743. include(symoptions,sp_generic_dummy);
  2744. result:=true;
  2745. end;
  2746. end;
  2747. procedure check_systemunit_loaded; inline;
  2748. begin
  2749. if systemunit=nil then
  2750. Message(sym_f_systemunitnotloaded);
  2751. end;
  2752. procedure write_system_parameter_lists(const name:string);
  2753. var
  2754. srsym:tprocsym;
  2755. begin
  2756. check_systemunit_loaded;
  2757. srsym:=tprocsym(systemunit.find(name));
  2758. if not assigned(srsym) or not (srsym.typ=procsym) then
  2759. internalerror(2016060302);
  2760. srsym.write_parameter_lists(nil);
  2761. end;
  2762. {*****************************************************************************
  2763. Search
  2764. *****************************************************************************}
  2765. procedure addsymref(sym:tsym;def:tdef);
  2766. var
  2767. owner,procowner : tsymtable;
  2768. begin
  2769. { for symbols used in preprocessor expressions, we don't want to
  2770. increase references count (for smaller final binaries) }
  2771. if not assigned(current_scanner) then
  2772. internalerror(2017050601);
  2773. if current_scanner.in_preproc_comp_expr then
  2774. exit;
  2775. { symbol uses count }
  2776. sym.IncRefCount;
  2777. owner:=sym.owner;
  2778. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2779. owner:=tdef(owner.defowner).owner;
  2780. if assigned(current_module) and
  2781. (owner.symtabletype=globalsymtable) then
  2782. begin
  2783. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2784. internalerror(200501152);
  2785. { unit uses count }
  2786. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2787. { Note: don't check the symtable directly as owner might be
  2788. a specialize symtable which is a globalsymtable as well }
  2789. if (
  2790. assigned(current_module.globalsymtable) and
  2791. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2792. ) or (
  2793. assigned(current_module.localsymtable) and
  2794. (current_module.localsymtable.moduleid<>owner.moduleid)
  2795. ) then
  2796. { symbol is imported from another unit }
  2797. current_module.addimportedsym(sym);
  2798. end;
  2799. { static symbols that are used in public functions must be exported
  2800. for packages as well }
  2801. if ([tf_supports_packages,tf_supports_hidden_symbols]<=target_info.flags) and
  2802. (owner.symtabletype=staticsymtable) and
  2803. assigned(current_procinfo) and
  2804. (
  2805. (
  2806. (sym.typ=staticvarsym) and
  2807. ([vo_is_public,vo_has_global_ref]*tstaticvarsym(sym).varoptions=[])
  2808. ) or (
  2809. (sym.typ=localvarsym) and
  2810. assigned(tlocalvarsym(sym).defaultconstsym) and
  2811. ([vo_is_public,vo_has_global_ref]*tstaticvarsym(tlocalvarsym(sym).defaultconstsym).varoptions=[])
  2812. ) or (
  2813. (sym.typ=procsym) and
  2814. assigned(def) and
  2815. (def.typ=procdef) and
  2816. not (df_has_global_ref in def.defoptions) and
  2817. not (po_public in tprocdef(def).procoptions)
  2818. )
  2819. ) then
  2820. begin
  2821. procowner:=current_procinfo.procdef.owner;
  2822. while procowner.symtabletype in [objectsymtable,recordsymtable] do
  2823. procowner:=tdef(procowner.defowner).owner;
  2824. if procowner.symtabletype=globalsymtable then
  2825. begin
  2826. if sym.typ=procsym then
  2827. current_procinfo.add_local_ref_def(def)
  2828. else if sym.typ=staticvarsym then
  2829. current_procinfo.add_local_ref_sym(sym)
  2830. else
  2831. current_procinfo.add_local_ref_sym(tlocalvarsym(sym).defaultconstsym);
  2832. end;
  2833. end;
  2834. end;
  2835. procedure addsymref(sym:tsym);
  2836. begin
  2837. addsymref(sym,nil);
  2838. end;
  2839. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2840. begin
  2841. result:=nesteddef=ownerdef;
  2842. if not result and
  2843. { types declared locally in a record method are not defined in the
  2844. record itself }
  2845. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2846. assigned(nesteddef.owner.defowner) then
  2847. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2848. end;
  2849. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2850. begin
  2851. result:=assigned(childsym) and (childsym.owner=symtable);
  2852. if not result and assigned(childsym) and
  2853. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2854. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2855. end;
  2856. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2857. begin
  2858. result:=false;
  2859. if not assigned(def1) or not assigned(def2) then
  2860. exit;
  2861. { for both defs walk to the topmost generic }
  2862. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2863. def1:=tdef(def1.owner.defowner);
  2864. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2865. def2:=tdef(def2.owner.defowner);
  2866. result:=def1=def2;
  2867. end;
  2868. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2869. var
  2870. uname : string;
  2871. begin
  2872. { TODO : check regarding arrays and records declared as their type }
  2873. if not (def.typ in [recorddef,objectdef]) then
  2874. internalerror(2012051501);
  2875. uname:=upper(srsym.realname);
  2876. repeat
  2877. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2878. begin
  2879. result:=def;
  2880. exit;
  2881. end;
  2882. def:=tdef(def.owner.defowner);
  2883. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2884. result:=nil;
  2885. end;
  2886. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2887. begin
  2888. { TODO : check regarding arrays and records declared as their type }
  2889. if not (nesteddef.typ in [recorddef,objectdef]) then
  2890. internalerror(2012051601);
  2891. repeat
  2892. if tstoreddef(nesteddef).genericdef=genericdef then
  2893. begin
  2894. resultdef:=nesteddef;
  2895. result:=true;
  2896. exit;
  2897. end;
  2898. nesteddef:=tdef(nesteddef.owner.defowner);
  2899. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2900. resultdef:=nil;
  2901. result:=false;
  2902. end;
  2903. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2904. symvisibility: visibility of the symbol
  2905. contextobjdef: via which def the symbol is accessed, e.g.:
  2906. fieldname:=1 -> contextobjdef = current_structdef
  2907. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2908. }
  2909. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2910. var
  2911. curstruct : tabstractrecorddef;
  2912. function is_current_unit(st:tsymtable):boolean;
  2913. begin
  2914. result :=
  2915. (
  2916. (
  2917. assigned(curstruct) and
  2918. (st.moduleid=curstruct.symtable.moduleid)
  2919. ) or
  2920. (
  2921. not assigned(curstruct) and
  2922. st.iscurrentunit
  2923. )
  2924. );
  2925. end;
  2926. var
  2927. orgcontextobjdef,
  2928. orgsymownerdef,
  2929. symownerdef : tabstractrecorddef;
  2930. nonlocalst : tsymtable;
  2931. isspezproc : boolean;
  2932. function check_strict_private:boolean;
  2933. begin
  2934. result:=assigned(curstruct) and
  2935. is_owned_by(curstruct,symownerdef);
  2936. end;
  2937. function check_strict_protected:boolean;
  2938. function is_childof(child, potentialparent: tdef):boolean;
  2939. begin
  2940. result:=true;
  2941. if def_is_related(child, potentialparent) then
  2942. exit;
  2943. if (child.typ=objectdef) and
  2944. (potentialparent.typ=objectdef) and
  2945. (tobjectdef(potentialparent).defoptions*[df_generic,df_specialization]=[df_generic]) then
  2946. begin
  2947. repeat
  2948. if tobjectdef(child).genericdef<>nil then
  2949. begin
  2950. if tobjectdef(child).genericdef.typ<>objectdef then
  2951. break;
  2952. child:=tobjectdef(child).genericdef as tobjectdef
  2953. end
  2954. else
  2955. child:=tobjectdef(child).childof;
  2956. if (child<>nil) and equal_defs(child, potentialparent) then
  2957. exit;
  2958. until child=nil;
  2959. end;
  2960. result:=false;
  2961. end;
  2962. function owner_hierarchy_related(nested,check:tabstractrecorddef):boolean;
  2963. var
  2964. owner:tabstractrecorddef;
  2965. begin
  2966. result:=true;
  2967. repeat
  2968. if is_childof(nested,check) then
  2969. exit;
  2970. if nested.owner.symtabletype in [recordsymtable,objectsymtable] then
  2971. nested:=tabstractrecorddef(nested.owner.defowner)
  2972. else
  2973. break;
  2974. until not assigned(nested);
  2975. result:=false;
  2976. end;
  2977. begin
  2978. result:=(
  2979. { access from nested class (specialization case) }
  2980. assigned(curstruct) and
  2981. is_owned_by(curstruct,symownerdef)
  2982. ) or
  2983. (
  2984. { access from nested class (non-specialization case) }
  2985. (orgsymownerdef<>symownerdef) and
  2986. assigned(curstruct) and
  2987. is_owned_by(curstruct,orgsymownerdef)
  2988. ) or
  2989. (
  2990. { access from child class (specialization case) }
  2991. assigned(contextobjdef) and
  2992. assigned(curstruct) and
  2993. owner_hierarchy_related(contextobjdef,symownerdef) and
  2994. is_childof(curstruct,contextobjdef)
  2995. ) or
  2996. (
  2997. { access from child class (non-specialization case) }
  2998. assigned(orgcontextobjdef) and
  2999. (
  3000. (orgcontextobjdef<>contextobjdef) or
  3001. (orgsymownerdef<>symownerdef)
  3002. ) and
  3003. assigned(curstruct) and
  3004. owner_hierarchy_related(orgcontextobjdef,orgsymownerdef) and
  3005. is_childof(curstruct,orgcontextobjdef)
  3006. ) or
  3007. (
  3008. { helpers can access strict protected symbols }
  3009. is_objectpascal_helper(contextobjdef) and
  3010. is_childof(tobjectdef(contextobjdef).extendeddef,symownerdef)
  3011. ) or
  3012. (
  3013. { same as above, but from context of call node inside
  3014. helper method }
  3015. is_objectpascal_helper(curstruct) and
  3016. is_childof(tobjectdef(curstruct).extendeddef,symownerdef)
  3017. );
  3018. end;
  3019. begin
  3020. result:=false;
  3021. { Get objdectdef owner of the symtable for the is_related checks }
  3022. if not assigned(symst) or
  3023. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  3024. internalerror(200810285);
  3025. symownerdef:=tabstractrecorddef(symst.defowner);
  3026. orgsymownerdef:=symownerdef;
  3027. orgcontextobjdef:=contextobjdef;
  3028. { for specializations we need to check the visibility of the generic,
  3029. not the specialization (at least when comparing outside of the
  3030. specialization }
  3031. if df_specialization in symownerdef.defoptions then
  3032. begin
  3033. if not assigned(symownerdef.genericdef) then
  3034. internalerror(2024041201);
  3035. if not (symownerdef.genericdef.typ in [objectdef,recorddef]) then
  3036. internalerror(2024020901);
  3037. orgsymownerdef:=symownerdef;
  3038. symownerdef:=tabstractrecorddef(symownerdef.genericdef);
  3039. end;
  3040. if assigned(contextobjdef) and (df_specialization in contextobjdef.defoptions) then
  3041. begin
  3042. if not assigned(contextobjdef.genericdef) then
  3043. internalerror(2024041202);
  3044. if not (contextobjdef.genericdef.typ in [objectdef,recorddef]) then
  3045. internalerror(2024020902);
  3046. orgcontextobjdef:=contextobjdef;
  3047. contextobjdef:=tabstractrecorddef(contextobjdef.genericdef);
  3048. end;
  3049. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  3050. begin
  3051. if not assigned(current_structdef.genericdef) then
  3052. internalerror(2024041203);
  3053. if not (current_structdef.genericdef.typ in [objectdef,recorddef]) then
  3054. internalerror(2024030903);
  3055. curstruct:=tabstractrecorddef(current_structdef.genericdef)
  3056. end
  3057. else
  3058. curstruct:=current_structdef;
  3059. { specializations might belong to a localsymtable or parasymtable }
  3060. nonlocalst:=symownerdef.owner;
  3061. if tstoreddef(symownerdef).is_specialization then
  3062. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  3063. nonlocalst:=nonlocalst.defowner.owner;
  3064. isspezproc:=false;
  3065. if assigned(current_procinfo) then
  3066. begin
  3067. if current_procinfo.procdef.is_specialization and
  3068. assigned(current_procinfo.procdef.struct) then
  3069. isspezproc:=true;
  3070. end;
  3071. case symvisibility of
  3072. vis_private :
  3073. begin
  3074. { private symbols are allowed when we are in the same
  3075. module as they are defined }
  3076. result:=check_strict_private or
  3077. (
  3078. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  3079. is_current_unit(nonlocalst)
  3080. ) or
  3081. ( // the case of specialize inside the generic declaration and nested types
  3082. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  3083. (
  3084. assigned(curstruct) and
  3085. (
  3086. (curstruct=symownerdef) or
  3087. (curstruct.owner.moduleid=symownerdef.symtable.moduleid)
  3088. )
  3089. ) or
  3090. (
  3091. not assigned(curstruct) and
  3092. (symownerdef.owner.iscurrentunit)
  3093. ) or
  3094. { access from a generic method that belongs to the class
  3095. but that is specialized elsewere }
  3096. (
  3097. isspezproc and
  3098. (current_procinfo.procdef.struct=curstruct)
  3099. ) or
  3100. { specializations may access private symbols that their
  3101. generics are allowed to access }
  3102. (
  3103. assigned(curstruct) and
  3104. (df_specialization in curstruct.defoptions) and
  3105. (symst.moduleid=curstruct.genericdef.owner.moduleid)
  3106. )
  3107. );
  3108. end;
  3109. vis_strictprivate :
  3110. begin
  3111. result:=check_strict_private;
  3112. end;
  3113. vis_strictprotected :
  3114. begin
  3115. result:=check_strict_protected;
  3116. end;
  3117. vis_protected :
  3118. begin
  3119. { protected symbols are visible in the module that defines them and
  3120. also visible to related objects. The related object must be defined
  3121. in the current module }
  3122. result:=check_strict_protected or
  3123. (
  3124. (
  3125. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  3126. is_current_unit(nonlocalst)
  3127. ) or
  3128. (
  3129. { context object is inside the current unit and related to
  3130. the symbol owner (specialization case) }
  3131. assigned(contextobjdef) and
  3132. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable,localsymtable]) and
  3133. is_current_unit(contextobjdef.owner) and
  3134. def_is_related(contextobjdef,symownerdef)
  3135. ) or
  3136. (
  3137. { context object is inside the current unit and related to
  3138. the symbol owner (non-specialization case) }
  3139. assigned(orgcontextobjdef) and
  3140. (
  3141. (orgcontextobjdef<>contextobjdef) or
  3142. (orgsymownerdef<>symownerdef)
  3143. ) and
  3144. (orgcontextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable,localsymtable]) and
  3145. is_current_unit(orgcontextobjdef.owner) and
  3146. def_is_related(orgcontextobjdef,orgsymownerdef)
  3147. ) or
  3148. ( // the case of specialize inside the generic declaration and nested types
  3149. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  3150. (
  3151. assigned(curstruct) and
  3152. (
  3153. (curstruct=symownerdef) or
  3154. (curstruct.owner.moduleid=symownerdef.symtable.moduleid)
  3155. )
  3156. ) or
  3157. (
  3158. not assigned(curstruct) and
  3159. (symownerdef.owner.iscurrentunit)
  3160. )
  3161. ) or
  3162. { access from a generic method that belongs to the class
  3163. but that is specialized elsewere }
  3164. (
  3165. isspezproc and
  3166. (current_procinfo.procdef.struct=curstruct)
  3167. ) or
  3168. { specializations may access private symbols that their
  3169. generics are allowed to access }
  3170. (
  3171. assigned(curstruct) and
  3172. (df_specialization in curstruct.defoptions) and
  3173. (symst.moduleid=curstruct.genericdef.owner.moduleid)
  3174. )
  3175. );
  3176. end;
  3177. vis_public,
  3178. vis_published :
  3179. result:=true;
  3180. else
  3181. internalerror(2019050702);
  3182. end;
  3183. if not result then
  3184. begin
  3185. { capturers have access to anything as we assume checks were done
  3186. before the procdef was inserted into the capturer }
  3187. result:=assigned(curstruct) and
  3188. (curstruct.typ=objectdef) and
  3189. (oo_is_capturer in tobjectdef(curstruct).objectoptions);
  3190. end;
  3191. end;
  3192. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  3193. begin
  3194. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  3195. end;
  3196. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  3197. var
  3198. i : longint;
  3199. pd : tprocdef;
  3200. begin
  3201. if sym.typ=procsym then
  3202. begin
  3203. result:=false;
  3204. { A procsym is visible, when there is at least one of the procdefs visible }
  3205. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  3206. begin
  3207. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  3208. if (pd.owner=sym.owner) and
  3209. is_visible_for_object(pd,contextobjdef) then
  3210. begin
  3211. result:=true;
  3212. exit;
  3213. end;
  3214. end;
  3215. { check dummy sym visbility by following associated procsyms }
  3216. if tprocsym(sym).could_be_implicitly_specialized then
  3217. begin
  3218. for i:=0 to tprocsym(sym).genprocsymovlds.count-1 do
  3219. if is_visible_for_object(tsym(tprocsym(sym).genprocsymovlds[i]),contextobjdef) then
  3220. begin
  3221. result:=true;
  3222. exit;
  3223. end;
  3224. end;
  3225. if (tprocsym(sym).procdeflist.count=0) and (sp_generic_dummy in tprocsym(sym).symoptions) then
  3226. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  3227. end
  3228. else
  3229. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  3230. end;
  3231. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3232. begin
  3233. case s[1] of
  3234. internal_macro_escape_unit_namespace_name:
  3235. result:=searchsym_maybe_with_symoption(copy(s,2,length(s)-1),srsym,srsymtable,[ssf_unit_or_namespace_only],sp_none)
  3236. else
  3237. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  3238. end
  3239. end;
  3240. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3241. begin
  3242. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  3243. end;
  3244. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  3245. var
  3246. hashedid: THashedIDString;
  3247. contextstructdef: tabstractrecorddef;
  3248. stackitem: psymtablestackitem;
  3249. begin
  3250. result:=false;
  3251. hashedid.id:=s;
  3252. stackitem:=symtablestack.stack;
  3253. while assigned(stackitem) do
  3254. begin
  3255. srsymtable:=stackitem^.symtable;
  3256. if not(ssf_unit_or_namespace_only in flags) and
  3257. (srsymtable.symtabletype=objectsymtable) then
  3258. begin
  3259. { TODO : implement the search for an option in classes as well }
  3260. if ssf_search_option in flags then
  3261. begin
  3262. result:=false;
  3263. exit;
  3264. end;
  3265. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  3266. begin
  3267. result:=true;
  3268. exit;
  3269. end;
  3270. end
  3271. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  3272. (srsymtable.defowner.typ=undefineddef)) then
  3273. begin
  3274. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3275. { First check if it is a unit/namespace symbol.
  3276. They are visible only if they are from the current unit or
  3277. unit of generic of currently processed specialization. }
  3278. if assigned(srsym) and
  3279. (not(ssf_unit_or_namespace_only in flags) or
  3280. (srsym.typ in [unitsym,namespacesym])) and
  3281. (
  3282. not(srsym.typ in [unitsym,namespacesym]) or
  3283. srsymtable.iscurrentunit or
  3284. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid)) or
  3285. (
  3286. assigned(current_procinfo) and
  3287. (df_specialization in current_procinfo.procdef.defoptions) and
  3288. (current_procinfo.procdef.genericdef.owner.moduleid=srsymtable.moduleid)
  3289. )
  3290. ) and
  3291. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  3292. begin
  3293. { use the class from withsymtable only when it is
  3294. defined in this unit }
  3295. if (srsymtable.symtabletype=withsymtable) and
  3296. assigned(srsymtable.defowner) and
  3297. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  3298. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  3299. (srsymtable.defowner.owner.iscurrentunit) then
  3300. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  3301. else
  3302. contextstructdef:=current_structdef;
  3303. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  3304. is_visible_for_object(srsym,contextstructdef) then
  3305. begin
  3306. { we need to know if a procedure references symbols
  3307. in the static symtable, because then it can't be
  3308. inlined from outside this unit }
  3309. if assigned(current_procinfo) and
  3310. (srsym.owner.symtabletype=staticsymtable) then
  3311. include(current_procinfo.flags,pi_uses_static_symtable);
  3312. if not (ssf_no_addsymref in flags) then
  3313. addsymref(srsym);
  3314. result:=true;
  3315. exit;
  3316. end;
  3317. end;
  3318. end;
  3319. stackitem:=stackitem^.next;
  3320. end;
  3321. srsym:=nil;
  3322. srsymtable:=nil;
  3323. end;
  3324. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  3325. srsymtable:TSymtable;option:tsymoption):boolean;
  3326. begin
  3327. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  3328. end;
  3329. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3330. var
  3331. hashedid : THashedIDString;
  3332. stackitem : psymtablestackitem;
  3333. classh : tobjectdef;
  3334. begin
  3335. result:=false;
  3336. hashedid.id:=s;
  3337. stackitem:=symtablestack.stack;
  3338. while assigned(stackitem) do
  3339. begin
  3340. {
  3341. It is not possible to have type symbols in:
  3342. parameters
  3343. Exception are classes, objects, records, generic definitions and specializations
  3344. that have the parameterized types inserted in the symtable.
  3345. }
  3346. srsymtable:=stackitem^.symtable;
  3347. if (srsymtable.symtabletype=ObjectSymtable) then
  3348. begin
  3349. classh:=tobjectdef(srsymtable.defowner);
  3350. while assigned(classh) do
  3351. begin
  3352. srsymtable:=classh.symtable;
  3353. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3354. if assigned(srsym) and
  3355. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3356. is_visible_for_object(srsym,current_structdef) then
  3357. begin
  3358. addsymref(srsym);
  3359. result:=true;
  3360. exit;
  3361. end;
  3362. classh:=classh.childof;
  3363. end;
  3364. end
  3365. else
  3366. begin
  3367. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3368. if assigned(srsym) and
  3369. (
  3370. not(srsym.typ in [unitsym,namespacesym]) or
  3371. srsymtable.iscurrentunit or
  3372. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  3373. ) and
  3374. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3375. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  3376. begin
  3377. { we need to know if a procedure references symbols
  3378. in the static symtable, because then it can't be
  3379. inlined from outside this unit }
  3380. if assigned(current_procinfo) and
  3381. (srsym.owner.symtabletype=staticsymtable) then
  3382. include(current_procinfo.flags,pi_uses_static_symtable);
  3383. addsymref(srsym);
  3384. result:=true;
  3385. exit;
  3386. end;
  3387. end;
  3388. stackitem:=stackitem^.next;
  3389. end;
  3390. result:=false;
  3391. srsym:=nil;
  3392. srsymtable:=nil;
  3393. end;
  3394. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3395. var
  3396. pmod : tmodule;
  3397. begin
  3398. result:=false;
  3399. if not assigned(pm) then exit;
  3400. pmod:=tmodule(pm);
  3401. if assigned(pmod.globalsymtable) then
  3402. begin
  3403. srsym:=tsym(pmod.globalsymtable.Find(s));
  3404. if assigned(srsym) then
  3405. begin
  3406. srsymtable:=pmod.globalsymtable;
  3407. addsymref(srsym);
  3408. result:=true;
  3409. exit;
  3410. end;
  3411. end;
  3412. { If the module is the current unit we also need
  3413. to search the local symtable }
  3414. if (pmod=current_module) and
  3415. assigned(pmod.localsymtable) then
  3416. begin
  3417. srsym:=tsym(pmod.localsymtable.Find(s));
  3418. if assigned(srsym) then
  3419. begin
  3420. srsymtable:=pmod.localsymtable;
  3421. addsymref(srsym);
  3422. result:=true;
  3423. exit;
  3424. end;
  3425. end;
  3426. srsym:=nil;
  3427. srsymtable:=nil;
  3428. end;
  3429. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  3430. var
  3431. stackitem : psymtablestackitem;
  3432. begin
  3433. result:=false;
  3434. stackitem:=symtablestack.stack;
  3435. while assigned(stackitem) do
  3436. begin
  3437. srsymtable:=stackitem^.symtable;
  3438. if (srsymtable.symtabletype=globalsymtable) and
  3439. (srsymtable.name^=unitname) then
  3440. begin
  3441. srsym:=tsym(srsymtable.find(symname));
  3442. if not assigned(srsym) then
  3443. break;
  3444. result:=true;
  3445. exit;
  3446. end;
  3447. stackitem:=stackitem^.next;
  3448. end;
  3449. { If the module is the current unit we also need
  3450. to search the local symtable }
  3451. if assigned(current_module.localsymtable) and
  3452. (current_module.localsymtable.name^=unitname) then
  3453. begin
  3454. srsymtable:=current_module.localsymtable;
  3455. srsym:=tsym(srsymtable.find(symname));
  3456. if assigned(srsym) then
  3457. begin
  3458. result:=true;
  3459. exit;
  3460. end;
  3461. end;
  3462. end;
  3463. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  3464. begin
  3465. result:=pd;
  3466. if pd.typ<>objectdef then
  3467. exit;
  3468. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  3469. end;
  3470. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  3471. var
  3472. hashedid : THashedIDString;
  3473. stackitem : psymtablestackitem;
  3474. srsymtable : tsymtable;
  3475. srsym : tsym;
  3476. formalname,
  3477. foundname : shortstring;
  3478. formalnameptr,
  3479. foundnameptr: pshortstring;
  3480. begin
  3481. while pd.is_unique_objpasdef do
  3482. begin
  3483. pd:=pd.childof;
  3484. end;
  3485. { not a formal definition -> return it }
  3486. if not(oo_is_formal in pd.objectoptions) then
  3487. begin
  3488. result:=pd;
  3489. exit;
  3490. end;
  3491. hashedid.id:=pd.typesym.name;
  3492. stackitem:=symtablestack.stack;
  3493. while assigned(stackitem) do
  3494. begin
  3495. srsymtable:=stackitem^.symtable;
  3496. { ObjC classes can't appear in generics or as nested class
  3497. definitions. Java classes can. }
  3498. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  3499. (is_java_class_or_interface(pd) and
  3500. (srsymtable.symtabletype=ObjectSymtable)) then
  3501. begin
  3502. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3503. if assigned(srsym) and
  3504. (srsym.typ=typesym) and
  3505. (ttypesym(srsym).typedef.typ=objectdef) and
  3506. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  3507. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3508. begin
  3509. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3510. begin
  3511. { the external name for the formal and the real
  3512. definition must match }
  3513. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  3514. assigned(pd.import_lib) then
  3515. begin
  3516. if assigned(pd.import_lib) then
  3517. formalname:=pd.import_lib^+'.'
  3518. else
  3519. formalname:='';
  3520. formalname:=formalname+pd.objextname^;
  3521. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  3522. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  3523. else
  3524. foundname:='';
  3525. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3526. formalnameptr:=@formalname;
  3527. foundnameptr:=@foundname;
  3528. end
  3529. else
  3530. begin
  3531. formalnameptr:=pd.objextname;
  3532. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3533. end;
  3534. if foundnameptr^<>formalnameptr^ then
  3535. begin
  3536. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3537. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3538. end;
  3539. end;
  3540. result:=tobjectdef(ttypesym(srsym).typedef);
  3541. if assigned(current_procinfo) and
  3542. (srsym.owner.symtabletype=staticsymtable) then
  3543. include(current_procinfo.flags,pi_uses_static_symtable);
  3544. addsymref(srsym);
  3545. exit;
  3546. end;
  3547. end;
  3548. stackitem:=stackitem^.next;
  3549. end;
  3550. { nothing found: optionally give an error and return the original
  3551. (empty) one }
  3552. if erroronfailure then
  3553. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3554. result:=pd;
  3555. end;
  3556. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3557. var
  3558. hashedid : THashedIDString;
  3559. orgclass : tobjectdef;
  3560. i : longint;
  3561. begin
  3562. orgclass:=classh;
  3563. { in case this is a formal class, first find the real definition }
  3564. if assigned(classh) then
  3565. begin
  3566. if (oo_is_formal in classh.objectoptions) then
  3567. classh:=find_real_class_definition(classh,true);
  3568. { The contextclassh is used for visibility. The classh must be equal to
  3569. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3570. parent or a class helper. }
  3571. if not (def_is_related(contextclassh,classh) or
  3572. (is_classhelper(contextclassh) and
  3573. assigned(tobjectdef(contextclassh).extendeddef) and
  3574. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3575. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3576. internalerror(200811161);
  3577. end;
  3578. result:=false;
  3579. hashedid.id:=s;
  3580. { an Objective-C protocol or Java interface can inherit from multiple
  3581. other protocols/interfaces -> use ImplementedInterfaces instead }
  3582. if is_objcprotocol(classh) or
  3583. is_javainterface(classh) then
  3584. begin
  3585. srsymtable:=classh.symtable;
  3586. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3587. if assigned(srsym) and
  3588. is_visible_for_object(srsym,contextclassh) then
  3589. begin
  3590. if not (ssf_no_addsymref in flags) then
  3591. addsymref(srsym);
  3592. result:=true;
  3593. exit;
  3594. end;
  3595. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3596. begin
  3597. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3598. begin
  3599. result:=true;
  3600. exit;
  3601. end;
  3602. end;
  3603. end
  3604. else
  3605. if is_objectpascal_helper(classh) then
  3606. begin
  3607. { helpers have their own obscure search logic... }
  3608. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3609. if result then
  3610. exit;
  3611. end
  3612. else
  3613. begin
  3614. while assigned(classh) do
  3615. begin
  3616. { search for a class helper method first if this is an Object
  3617. Pascal class and we haven't yet found a helper symbol }
  3618. if (classh.objecttype in objecttypes_with_helpers) and
  3619. (ssf_search_helper in flags) then
  3620. begin
  3621. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3622. { an eventual overload inside the extended type's hierarchy
  3623. will be found by tcallcandidates }
  3624. if result then
  3625. exit;
  3626. end;
  3627. srsymtable:=classh.symtable;
  3628. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3629. if assigned(srsym) and
  3630. is_visible_for_object(srsym,contextclassh) then
  3631. begin
  3632. if not (ssf_no_addsymref in flags) then
  3633. addsymref(srsym);
  3634. result:=true;
  3635. exit;
  3636. end;
  3637. classh:=classh.childof;
  3638. end;
  3639. end;
  3640. if is_objcclass(orgclass) then
  3641. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3642. else
  3643. begin
  3644. srsym:=nil;
  3645. srsymtable:=nil;
  3646. end;
  3647. end;
  3648. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3649. var
  3650. hashedid : THashedIDString;
  3651. begin
  3652. result:=false;
  3653. hashedid.id:=s;
  3654. { search for a record helper method first }
  3655. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3656. if result then
  3657. { an eventual overload inside the extended type's hierarchy
  3658. will be found by tcallcandidates }
  3659. exit;
  3660. srsymtable:=recordh.symtable;
  3661. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3662. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3663. begin
  3664. addsymref(srsym);
  3665. result:=true;
  3666. exit;
  3667. end;
  3668. srsym:=nil;
  3669. srsymtable:=nil;
  3670. end;
  3671. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3672. var
  3673. def : tdef;
  3674. i : longint;
  3675. begin
  3676. { in case this is a formal class, first find the real definition }
  3677. if assigned(classh) and
  3678. (oo_is_formal in classh.objectoptions) then
  3679. classh:=find_real_class_definition(classh,true);
  3680. result:=false;
  3681. def:=nil;
  3682. while assigned(classh) do
  3683. begin
  3684. for i:=0 to classh.symtable.DefList.Count-1 do
  3685. begin
  3686. def:=tstoreddef(classh.symtable.DefList[i]);
  3687. { Find also all hidden private methods to
  3688. be compatible with delphi, see tw6203 (PFV) }
  3689. if (def.typ=procdef) and
  3690. (po_msgint in tprocdef(def).procoptions) and
  3691. (tprocdef(def).messageinf.i=msgid) then
  3692. begin
  3693. srdef:=def;
  3694. srsym:=tprocdef(def).procsym;
  3695. srsymtable:=classh.symtable;
  3696. addsymref(srsym);
  3697. result:=true;
  3698. exit;
  3699. end;
  3700. end;
  3701. classh:=classh.childof;
  3702. end;
  3703. srdef:=nil;
  3704. srsym:=nil;
  3705. srsymtable:=nil;
  3706. end;
  3707. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3708. var
  3709. def : tdef;
  3710. i : longint;
  3711. begin
  3712. { in case this is a formal class, first find the real definition }
  3713. if assigned(classh) and
  3714. (oo_is_formal in classh.objectoptions) then
  3715. classh:=find_real_class_definition(classh,true);
  3716. result:=false;
  3717. def:=nil;
  3718. while assigned(classh) do
  3719. begin
  3720. for i:=0 to classh.symtable.DefList.Count-1 do
  3721. begin
  3722. def:=tstoreddef(classh.symtable.DefList[i]);
  3723. { Find also all hidden private methods to
  3724. be compatible with delphi, see tw6203 (PFV) }
  3725. if (def.typ=procdef) and
  3726. (po_msgstr in tprocdef(def).procoptions) and
  3727. (tprocdef(def).messageinf.str^=s) then
  3728. begin
  3729. srsym:=tprocdef(def).procsym;
  3730. srsymtable:=classh.symtable;
  3731. addsymref(srsym);
  3732. result:=true;
  3733. exit;
  3734. end;
  3735. end;
  3736. classh:=classh.childof;
  3737. end;
  3738. srsym:=nil;
  3739. srsymtable:=nil;
  3740. end;
  3741. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;forward;
  3742. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3743. var
  3744. hashedid : THashedIDString;
  3745. parentclassh : tobjectdef;
  3746. begin
  3747. result:=false;
  3748. if not is_objectpascal_helper(classh) then
  3749. Internalerror(2011030101);
  3750. hashedid.id:=s;
  3751. { in a helper things are a bit more complex:
  3752. 1. search the symbol in the helper (if not "inherited")
  3753. 2. search the symbol in the extended type
  3754. 3. search the symbol in the parent helpers
  3755. 4. only classes: search the symbol in the parents of the extended type
  3756. }
  3757. if not (ssf_has_inherited in flags) then
  3758. begin
  3759. { search in the helper itself }
  3760. srsymtable:=classh.symtable;
  3761. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3762. if assigned(srsym) and
  3763. is_visible_for_object(srsym,contextclassh) then
  3764. begin
  3765. if not (ssf_no_addsymref in flags) then
  3766. addsymref(srsym);
  3767. result:=true;
  3768. exit;
  3769. end;
  3770. end;
  3771. { now search in the extended type itself }
  3772. { Note: the extendeddef might be Nil if we are currently parsing the
  3773. extended type itself and the identifier was not found }
  3774. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3775. begin
  3776. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3777. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3778. if assigned(srsym) and
  3779. is_visible_for_object(srsym,contextclassh) then
  3780. begin
  3781. if not (ssf_no_addsymref in flags) then
  3782. addsymref(srsym);
  3783. result:=true;
  3784. exit;
  3785. end;
  3786. end;
  3787. { now search in the parent helpers }
  3788. parentclassh:=classh.childof;
  3789. while assigned(parentclassh) do
  3790. begin
  3791. srsymtable:=parentclassh.symtable;
  3792. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3793. if assigned(srsym) and
  3794. is_visible_for_object(srsym,contextclassh) then
  3795. begin
  3796. if not (ssf_no_addsymref in flags) then
  3797. addsymref(srsym);
  3798. result:=true;
  3799. exit;
  3800. end;
  3801. parentclassh:=parentclassh.childof;
  3802. end;
  3803. { now search in the parents of the extended class (with helpers!) }
  3804. if is_class(classh.extendeddef) then
  3805. begin
  3806. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3807. { addsymref is already called by searchsym_in_class }
  3808. if result then
  3809. exit;
  3810. end;
  3811. { now search all helpers using the extendeddef as the starting point }
  3812. if (m_multi_helpers in current_settings.modeswitches) and
  3813. (
  3814. (current_structdef<>classh) or
  3815. assigned(classh.extendeddef)
  3816. ) then
  3817. begin
  3818. { this is only allowed if classh is currently parsed }
  3819. if not assigned(classh.extendeddef) then
  3820. internalerror(2019110101);
  3821. result:=search_best_objectpascal_helper(s,classh.extendeddef,contextclassh,srsym,srsymtable);
  3822. end;
  3823. end;
  3824. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3825. var
  3826. sym : Tprocsym;
  3827. hashedid : THashedIDString;
  3828. curreq,
  3829. besteq : tequaltype;
  3830. currpd,
  3831. bestpd : tprocdef;
  3832. stackitem : psymtablestackitem;
  3833. shortstringcount : longint;
  3834. isexplicit,
  3835. checkshortstring : boolean;
  3836. begin
  3837. hashedid.id:=overloaded_names[assignment_type];
  3838. besteq:=te_incompatible;
  3839. bestpd:=nil;
  3840. stackitem:=symtablestack.stack;
  3841. { special handling for assignments to shortstrings with a specific length:
  3842. - if we get an operator to ShortString we use that
  3843. - if we get only a single String[x] operator we use that
  3844. - otherwise it's a nogo }
  3845. isexplicit:=assignment_type=_OP_EXPLICIT;
  3846. shortstringcount:=0;
  3847. checkshortstring:=not isexplicit and is_shortstring(to_def) and (tstringdef(to_def).len<>255);
  3848. while assigned(stackitem) do
  3849. begin
  3850. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3851. if sym<>nil then
  3852. begin
  3853. if sym.typ<>procsym then
  3854. internalerror(200402031);
  3855. { if the source type is an alias then this is only the second choice,
  3856. if you mess with this code, check tw4093 }
  3857. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq,isexplicit);
  3858. { we found a ShortString overload, use that and be done }
  3859. if checkshortstring and
  3860. assigned(currpd) and
  3861. is_shortstring(currpd.returndef) and
  3862. (tstringdef(currpd.returndef).len=255) then
  3863. begin
  3864. besteq:=curreq;
  3865. bestpd:=currpd;
  3866. break;
  3867. end;
  3868. { independently of the operator being better count if we encountered
  3869. multpile String[x] operators }
  3870. if checkshortstring and assigned(currpd) and is_shortstring(currpd.returndef) then
  3871. inc(shortstringcount);
  3872. if curreq>besteq then
  3873. begin
  3874. besteq:=curreq;
  3875. bestpd:=currpd;
  3876. { don't stop searching if we have a String[x] operator cause
  3877. we might find a ShortString one or multiple ones (which
  3878. leads to no operator use) }
  3879. if (besteq=te_exact) and not checkshortstring then
  3880. break;
  3881. end;
  3882. end;
  3883. stackitem:=stackitem^.next;
  3884. end;
  3885. if checkshortstring and (shortstringcount>1) then
  3886. bestpd:=nil;
  3887. result:=bestpd;
  3888. end;
  3889. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3890. begin
  3891. { search record/object symtable first for a suitable operator }
  3892. if from_def.typ in [recorddef,objectdef] then
  3893. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3894. if to_def.typ in [recorddef,objectdef] then
  3895. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3896. { if type conversion is explicit then search first for explicit
  3897. operator overload and if not found then use implicit operator }
  3898. if explicit then
  3899. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3900. else
  3901. result:=nil;
  3902. if result=nil then
  3903. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3904. { restore symtable stack }
  3905. if to_def.typ in [recorddef,objectdef] then
  3906. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3907. if from_def.typ in [recorddef,objectdef] then
  3908. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3909. end;
  3910. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3911. var
  3912. sym : Tprocsym;
  3913. hashedid : THashedIDString;
  3914. curreq,
  3915. besteq : tequaltype;
  3916. currpd,
  3917. bestpd : tprocdef;
  3918. stackitem : psymtablestackitem;
  3919. begin
  3920. hashedid.id:='enumerator';
  3921. besteq:=te_incompatible;
  3922. bestpd:=nil;
  3923. stackitem:=symtablestack.stack;
  3924. while assigned(stackitem) do
  3925. begin
  3926. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3927. if sym<>nil then
  3928. begin
  3929. if sym.typ<>procsym then
  3930. internalerror(200910241);
  3931. { if the source type is an alias then this is only the second choice,
  3932. if you mess with this code, check tw4093 }
  3933. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3934. if curreq>besteq then
  3935. begin
  3936. besteq:=curreq;
  3937. bestpd:=currpd;
  3938. if (besteq=te_exact) then
  3939. break;
  3940. end;
  3941. end;
  3942. stackitem:=stackitem^.next;
  3943. end;
  3944. result:=bestpd;
  3945. end;
  3946. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  3947. var
  3948. sym : Tprocsym;
  3949. hashedid : THashedIDString;
  3950. optoken: ttoken;
  3951. begin
  3952. optoken := managementoperator2tok[mop];
  3953. if (optoken<first_managment_operator) or
  3954. (optoken>last_managment_operator) then
  3955. internalerror(201602280);
  3956. hashedid.id:=overloaded_names[optoken];
  3957. if not (pd.typ in [recorddef]) then
  3958. internalerror(201602281);
  3959. sym:=Tprocsym(tabstractrecorddef(pd).symtable.FindWithHash(hashedid));
  3960. if sym<>nil then
  3961. begin
  3962. if sym.typ<>procsym then
  3963. internalerror(201602282);
  3964. result:=sym.find_procdef_bytype(potype_operator);
  3965. end
  3966. else
  3967. result:=nil;
  3968. end;
  3969. function search_system_type(const s: TIDString): ttypesym;
  3970. var
  3971. sym : tsym;
  3972. begin
  3973. check_systemunit_loaded;
  3974. sym:=tsym(systemunit.Find(s));
  3975. if not assigned(sym) or
  3976. (sym.typ<>typesym) then
  3977. message1(cg_f_unknown_system_type,s);
  3978. result:=ttypesym(sym);
  3979. end;
  3980. function try_search_system_type(const s: TIDString): ttypesym;
  3981. var
  3982. sym : tsym;
  3983. begin
  3984. check_systemunit_loaded;
  3985. sym:=tsym(systemunit.Find(s));
  3986. if not assigned(sym) then
  3987. result:=nil
  3988. else
  3989. begin
  3990. if sym.typ<>typesym then
  3991. message1(cg_f_unknown_system_type,s);
  3992. result:=ttypesym(sym);
  3993. end;
  3994. end;
  3995. function try_search_current_module_type(const s: TIDString): ttypesym;
  3996. var
  3997. found: boolean;
  3998. srsymtable: tsymtable;
  3999. srsym: tsym;
  4000. begin
  4001. if s[1]='$' then
  4002. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  4003. else
  4004. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  4005. if found then
  4006. begin
  4007. if (srsym.typ<>typesym) then
  4008. internalerror(2014091207);
  4009. result:=ttypesym(srsym);
  4010. end
  4011. else
  4012. result:=nil;
  4013. end;
  4014. function search_system_proc(const s: TIDString): tprocdef;
  4015. var
  4016. srsym: tsym;
  4017. begin
  4018. check_systemunit_loaded;
  4019. srsym:=tsym(systemunit.find(s));
  4020. if not assigned(srsym) and
  4021. (cs_compilesystem in current_settings.moduleswitches) then
  4022. srsym:=tsym(systemunit.Find(upper(s)));
  4023. if not assigned(srsym) or
  4024. (srsym.typ<>procsym) then
  4025. message1(cg_f_unknown_compilerproc,s);
  4026. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  4027. end;
  4028. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  4029. var
  4030. srsymtable: tsymtable;
  4031. sym: tsym;
  4032. begin
  4033. sym:=nil;
  4034. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  4035. (sym.typ=typesym) then
  4036. begin
  4037. result:=ttypesym(sym);
  4038. exit;
  4039. end
  4040. else
  4041. begin
  4042. if throwerror then
  4043. message2(cg_f_unknown_type_in_unit,typename,unitname);
  4044. result:=nil;
  4045. end;
  4046. end;
  4047. function search_sym_in_helperdef(const s: string;classh : tobjectdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable): boolean;
  4048. var
  4049. hashedid : THashedIDString;
  4050. pdef : tprocdef;
  4051. i : integer;
  4052. begin
  4053. hashedid.id:=s;
  4054. result:=false;
  4055. repeat
  4056. srsymtable:=classh.symtable;
  4057. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  4058. if srsym<>nil then
  4059. begin
  4060. case srsym.typ of
  4061. procsym:
  4062. begin
  4063. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  4064. begin
  4065. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  4066. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  4067. continue;
  4068. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  4069. srsymtable:=srsym.owner;
  4070. result:=true;
  4071. exit;
  4072. end;
  4073. if (sp_generic_dummy in tprocsym(srsym).symoptions) and
  4074. (tprocsym(srsym).procdeflist.count=0) and
  4075. is_visible_for_object(srsym.owner,srsym.visibility,contextclassh) then
  4076. begin
  4077. srsymtable:=srsym.owner;
  4078. result:=true;
  4079. exit;
  4080. end;
  4081. end;
  4082. typesym,
  4083. fieldvarsym,
  4084. constsym,
  4085. enumsym,
  4086. undefinedsym,
  4087. propertysym:
  4088. begin
  4089. result:=true;
  4090. exit;
  4091. end;
  4092. else
  4093. internalerror(2014041101);
  4094. end;
  4095. end;
  4096. { try the helper parent if available }
  4097. classh:=classh.childof;
  4098. until classh=nil;
  4099. end;
  4100. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  4101. var
  4102. s : TSymStr;
  4103. st : tsymtable;
  4104. begin
  4105. result:=nil;
  4106. { when there are no helpers active currently then we don't need to do
  4107. anything }
  4108. if current_module.extendeddefs.count=0 then
  4109. exit;
  4110. if (df_genconstraint in pd.defoptions) then
  4111. begin
  4112. { if we have a constraint for a class type or a single interface we
  4113. use that to resolve helpers at declaration time of the generic,
  4114. otherwise there can't be any helpers as the type isn't known yet }
  4115. if pd.typ=objectdef then
  4116. pd:=tobjectdef(pd).getparentdef
  4117. else
  4118. exit;
  4119. end;
  4120. { no helpers for anonymous types }
  4121. if ((pd.typ in [recorddef,objectdef]) and
  4122. (
  4123. not assigned(tabstractrecorddef(pd).objrealname) or
  4124. (tabstractrecorddef(pd).objrealname^='')
  4125. )
  4126. ) or
  4127. not assigned(pd.typesym) then
  4128. exit;
  4129. { if pd is defined inside a procedure we must not use make_mangledname
  4130. (as a helper may not be defined in a procedure this is no problem...)}
  4131. st:=pd.owner;
  4132. while st.symtabletype in [objectsymtable,recordsymtable] do
  4133. st:=st.defowner.owner;
  4134. if st.symtabletype=localsymtable then
  4135. exit;
  4136. { the mangled name is used as the key for tmodule.extendeddefs }
  4137. s:=generate_objectpascal_helper_key(pd);
  4138. result:=TFPObjectList(current_module.extendeddefs.Find(s));
  4139. end;
  4140. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;
  4141. var
  4142. list : TFPObjectList;
  4143. i : integer;
  4144. odef : tobjectdef;
  4145. begin
  4146. result:=false;
  4147. list:=get_objectpascal_helpers(pd);
  4148. if assigned(list) and (list.count>0) then
  4149. begin
  4150. i:=list.count-1;
  4151. repeat
  4152. odef:=tobjectdef(list[i]);
  4153. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  4154. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  4155. if result then
  4156. result:=search_sym_in_helperdef(name,odef,contextclassh,srsym,srsymtable);
  4157. dec(i);
  4158. until result or (i<0);
  4159. end;
  4160. end;
  4161. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  4162. var
  4163. list : TFPObjectList;
  4164. i : integer;
  4165. begin
  4166. result:=false;
  4167. odef:=nil;
  4168. list:=get_objectpascal_helpers(pd);
  4169. if assigned(list) and (list.count>0) then
  4170. begin
  4171. i:=list.count-1;
  4172. repeat
  4173. odef:=tobjectdef(list[list.count-1]);
  4174. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  4175. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  4176. dec(i);
  4177. until result or (i<0);
  4178. if not result then
  4179. { just to be sure that noone uses odef }
  4180. odef:=nil;
  4181. end;
  4182. end;
  4183. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4184. var
  4185. classh : tobjectdef;
  4186. begin
  4187. result:=false;
  4188. { if there is no class helper for the class then there is no need to
  4189. search further }
  4190. if m_multi_helpers in current_settings.modeswitches then
  4191. result:=search_best_objectpascal_helper(s,pd,contextclassh,srsym,srsymtable)
  4192. else
  4193. begin
  4194. if search_last_objectpascal_helper(pd,contextclassh,classh) and
  4195. search_sym_in_helperdef(s,classh,contextclassh,srsym,srsymtable) then
  4196. result:=true;
  4197. end;
  4198. if result then
  4199. begin
  4200. { we need to know if a procedure references symbols
  4201. in the static symtable, because then it can't be
  4202. inlined from outside this unit }
  4203. if (srsym.typ=procsym) and
  4204. assigned(current_procinfo) and
  4205. (srsym.owner.symtabletype=staticsymtable) then
  4206. include(current_procinfo.flags,pi_uses_static_symtable);
  4207. addsymref(srsym);
  4208. end
  4209. else
  4210. begin
  4211. srsym:=nil;
  4212. srsymtable:=nil;
  4213. end;
  4214. end;
  4215. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4216. var
  4217. searchst : tsymtable;
  4218. searchsym : tsym;
  4219. hashedid : THashedIDString;
  4220. stackitem : psymtablestackitem;
  4221. i : longint;
  4222. founddefowner,
  4223. defowner : tobjectdef;
  4224. begin
  4225. hashedid.id:=class_helper_prefix+s;
  4226. stackitem:=symtablestack.stack;
  4227. result:=false;
  4228. srsym:=nil;
  4229. srsymtable:=nil;
  4230. founddefowner:=nil;
  4231. while assigned(stackitem) do
  4232. begin
  4233. searchst:=stackitem^.symtable;
  4234. searchsym:=tsym(searchst.FindWithHash(hashedid));
  4235. if assigned(searchsym) then
  4236. begin
  4237. if not(searchst.symtabletype in [globalsymtable,staticsymtable]) or
  4238. not(searchsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  4239. (searchsym.typ<>procsym) then
  4240. internalerror(2009111505);
  4241. { check whether this procsym includes a helper for this particular class }
  4242. for i:=0 to tprocsym(searchsym).procdeflist.count-1 do
  4243. begin
  4244. { does pd inherit from (or is the same as) the class
  4245. that this method's category extended?
  4246. Warning: this list contains both category and objcclass methods
  4247. (for id.randommethod), so only check category methods here
  4248. }
  4249. defowner:=tobjectdef(tprocdef(tprocsym(searchsym).procdeflist[i]).owner.defowner);
  4250. if is_objccategory(defowner) and
  4251. def_is_related(pd,defowner.childof) then
  4252. begin
  4253. { we need to know if a procedure references symbols
  4254. in the static symtable, because then it can't be
  4255. inlined from outside this unit }
  4256. if assigned(current_procinfo) and
  4257. (searchsym.owner.symtabletype=staticsymtable) then
  4258. include(current_procinfo.flags,pi_uses_static_symtable);
  4259. { Stop looking if this is a category that extends the specified
  4260. class itself. There might be other categories that extend this,
  4261. but that doesn't matter. If it extens a parent, keep looking
  4262. in case we find the symbol in a category that extends this class
  4263. (or a closer parent).
  4264. }
  4265. if not result or
  4266. def_is_related(defowner.childof,founddefowner) then
  4267. begin
  4268. founddefowner:=defowner.childof;
  4269. srsym:=tprocdef(tprocsym(searchsym).procdeflist[i]).procsym;
  4270. srsymtable:=srsym.owner;
  4271. result:=true;
  4272. if pd=founddefowner then
  4273. begin
  4274. addsymref(srsym);
  4275. exit;
  4276. end;
  4277. end;
  4278. end;
  4279. end;
  4280. end;
  4281. stackitem:=stackitem^.next;
  4282. end;
  4283. if result then
  4284. begin
  4285. addsymref(srsym);
  4286. exit;
  4287. end;
  4288. end;
  4289. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4290. var
  4291. hashedid : THashedIDString;
  4292. stackitem : psymtablestackitem;
  4293. i : longint;
  4294. begin
  4295. hashedid.id:=class_helper_prefix+s;
  4296. stackitem:=symtablestack.stack;
  4297. while assigned(stackitem) do
  4298. begin
  4299. srsymtable:=stackitem^.symtable;
  4300. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  4301. if assigned(srsym) then
  4302. begin
  4303. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  4304. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  4305. (srsym.typ<>procsym) then
  4306. internalerror(2009112005);
  4307. { check whether this procsym includes a helper for this particular class }
  4308. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  4309. begin
  4310. { we need to know if a procedure references symbols
  4311. in the static symtable, because then it can't be
  4312. inlined from outside this unit }
  4313. if assigned(current_procinfo) and
  4314. (srsym.owner.symtabletype=staticsymtable) then
  4315. include(current_procinfo.flags,pi_uses_static_symtable);
  4316. { no need to keep looking. There might be other
  4317. methods with the same name, but that doesn't matter
  4318. as far as the basic procsym is concerned.
  4319. }
  4320. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  4321. { We need the symtable in which the classhelper-like sym
  4322. is located, not the objectdef. The reason is that the
  4323. callnode will climb the symtablestack until it encounters
  4324. this symtable to start looking for overloads (and it won't
  4325. find the objectsymtable in which this method sym is
  4326. located
  4327. srsymtable:=srsym.owner;
  4328. }
  4329. addsymref(srsym);
  4330. result:=true;
  4331. exit;
  4332. end;
  4333. end;
  4334. stackitem:=stackitem^.next;
  4335. end;
  4336. srsym:=nil;
  4337. srsymtable:=nil;
  4338. result:=false;
  4339. end;
  4340. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  4341. { searches n in symtable of pd and all anchestors }
  4342. var
  4343. srsymtable : tsymtable;
  4344. begin
  4345. { in case this is a formal class, first find the real definition }
  4346. if (oo_is_formal in pd.objectoptions) then
  4347. pd:=find_real_class_definition(tobjectdef(pd),true);
  4348. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  4349. exit;
  4350. result:=search_struct_member_no_helper(pd,s);
  4351. if assigned(result) then
  4352. exit;
  4353. { not found, now look for class helpers }
  4354. if is_objcclass(pd) then
  4355. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  4356. end;
  4357. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  4358. var
  4359. hashedid : THashedIDString;
  4360. srsym : tsym;
  4361. begin
  4362. hashedid.id:=s;
  4363. while assigned(pd) do
  4364. begin
  4365. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  4366. if assigned(srsym) then
  4367. begin
  4368. result:=srsym;
  4369. exit;
  4370. end;
  4371. if pd.typ=objectdef then
  4372. pd:=tobjectdef(pd).childof
  4373. else
  4374. pd:=nil;
  4375. end;
  4376. result:=nil;
  4377. end;
  4378. function search_macro(const s : string):tsym;
  4379. var
  4380. stackitem : psymtablestackitem;
  4381. hashedid : THashedIDString;
  4382. srsym : tsym;
  4383. begin
  4384. hashedid.id:=s;
  4385. { First search the localmacrosymtable before searching the
  4386. global macrosymtables from the units }
  4387. if assigned(current_module) then
  4388. begin
  4389. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  4390. if assigned(srsym) then
  4391. begin
  4392. result:= srsym;
  4393. exit;
  4394. end;
  4395. end;
  4396. stackitem:=macrosymtablestack.stack;
  4397. while assigned(stackitem) do
  4398. begin
  4399. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  4400. if assigned(srsym) then
  4401. begin
  4402. result:= srsym;
  4403. exit;
  4404. end;
  4405. stackitem:=stackitem^.next;
  4406. end;
  4407. result:= nil;
  4408. end;
  4409. function defined_macro(const s : string):boolean;
  4410. var
  4411. mac: tmacro;
  4412. begin
  4413. mac:=tmacro(search_macro(s));
  4414. if assigned(mac) then
  4415. begin
  4416. mac.is_used:=true;
  4417. defined_macro:=mac.defined;
  4418. end
  4419. else
  4420. defined_macro:=false;
  4421. end;
  4422. {****************************************************************************
  4423. Object Helpers
  4424. ****************************************************************************}
  4425. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  4426. { returns the default property of a class, searches also anchestors }
  4427. var
  4428. _defaultprop : tpropertysym;
  4429. helperpd : tobjectdef;
  4430. begin
  4431. _defaultprop:=nil;
  4432. { first search in helper's hierarchy }
  4433. if search_last_objectpascal_helper(pd,nil,helperpd) then
  4434. while assigned(helperpd) do
  4435. begin
  4436. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  4437. if assigned(_defaultprop) then
  4438. break;
  4439. helperpd:=helperpd.childof;
  4440. end;
  4441. if assigned(_defaultprop) then
  4442. begin
  4443. search_default_property:=_defaultprop;
  4444. exit;
  4445. end;
  4446. { now search in the type's hierarchy itself }
  4447. while assigned(pd) do
  4448. begin
  4449. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  4450. if assigned(_defaultprop) then
  4451. break;
  4452. if (pd.typ=objectdef) then
  4453. pd:=tobjectdef(pd).childof
  4454. else
  4455. break;
  4456. end;
  4457. search_default_property:=_defaultprop;
  4458. end;
  4459. {****************************************************************************
  4460. Macro Helpers
  4461. ****************************************************************************}
  4462. procedure def_system_macro(const name : string);
  4463. var
  4464. mac : tmacro;
  4465. s: string;
  4466. begin
  4467. if name = '' then
  4468. internalerror(2004121202);
  4469. s:= upper(name);
  4470. mac:=tmacro(search_macro(s));
  4471. if not assigned(mac) then
  4472. begin
  4473. mac:=tmacro.create(s);
  4474. if assigned(current_module) then
  4475. current_module.localmacrosymtable.insertsym(mac)
  4476. else
  4477. initialmacrosymtable.insertsym(mac);
  4478. end;
  4479. Message1(parser_c_macro_defined,mac.name);
  4480. mac.defined:=true;
  4481. end;
  4482. procedure set_system_macro(const name, value : string);
  4483. var
  4484. mac : tmacro;
  4485. s: string;
  4486. begin
  4487. if name = '' then
  4488. internalerror(2004121203);
  4489. s:= upper(name);
  4490. mac:=tmacro(search_macro(s));
  4491. if not assigned(mac) then
  4492. begin
  4493. mac:=tmacro.create(s);
  4494. if assigned(current_module) then
  4495. current_module.localmacrosymtable.insertsym(mac)
  4496. else
  4497. initialmacrosymtable.insertsym(mac);
  4498. end
  4499. else
  4500. begin
  4501. mac.is_compiler_var:=false;
  4502. mac.free_buftext;
  4503. end;
  4504. mac.is_c_macro:=true;
  4505. Message2(parser_c_macro_set_to,mac.name,value);
  4506. move(value[1],mac.allocate_buftext(length(value))^,length(value));
  4507. mac.defined:=true;
  4508. end;
  4509. procedure set_system_compvar(const name, value : string);
  4510. var
  4511. mac : tmacro;
  4512. s: string;
  4513. begin
  4514. if name = '' then
  4515. internalerror(2004121204);
  4516. s:= upper(name);
  4517. mac:=tmacro(search_macro(s));
  4518. if not assigned(mac) then
  4519. begin
  4520. mac:=tmacro.create(s);
  4521. mac.is_compiler_var:=true;
  4522. if assigned(current_module) then
  4523. current_module.localmacrosymtable.insertsym(mac)
  4524. else
  4525. initialmacrosymtable.insertsym(mac);
  4526. end
  4527. else
  4528. mac.is_compiler_var:=true;
  4529. Message2(parser_c_macro_set_to,mac.name,value);
  4530. move(value[1],mac.allocate_buftext(length(value))^,length(value));
  4531. mac.defined:=true;
  4532. end;
  4533. procedure undef_system_macro(const name : string);
  4534. var
  4535. mac : tmacro;
  4536. s: string;
  4537. begin
  4538. if name = '' then
  4539. internalerror(2004121205);
  4540. s:= upper(name);
  4541. mac:=tmacro(search_macro(s));
  4542. if not assigned(mac) then
  4543. {If not found, then it's already undefined.}
  4544. else
  4545. begin
  4546. Message1(parser_c_macro_undefined,mac.name);
  4547. mac.defined:=false;
  4548. mac.is_compiler_var:=false;
  4549. { delete old definition }
  4550. mac.free_buftext;
  4551. end;
  4552. end;
  4553. {$ifdef UNITALIASES}
  4554. {****************************************************************************
  4555. TUNIT_ALIAS
  4556. ****************************************************************************}
  4557. constructor tunit_alias.create(const n:string);
  4558. var
  4559. i : longint;
  4560. begin
  4561. i:=pos('=',n);
  4562. if i=0 then
  4563. fail;
  4564. inherited createname(Copy(n,1,i-1));
  4565. newname:=stringdup(Copy(n,i+1,255));
  4566. end;
  4567. destructor tunit_alias.destroy;
  4568. begin
  4569. stringdispose(newname);
  4570. inherited destroy;
  4571. end;
  4572. procedure addunitalias(const n:string);
  4573. begin
  4574. unitaliases^.insert(tunit_alias,init(Upper(n))));
  4575. end;
  4576. function getunitalias(const n:string):string;
  4577. var
  4578. p : punit_alias;
  4579. begin
  4580. p:=punit_alias(unitaliases^.Find(Upper(n)));
  4581. if assigned(p) then
  4582. getunitalias:=punit_alias(p).newname^
  4583. else
  4584. getunitalias:=n;
  4585. end;
  4586. {$endif UNITALIASES}
  4587. {****************************************************************************
  4588. Init/Done Symtable
  4589. ****************************************************************************}
  4590. procedure InitSymtable;
  4591. begin
  4592. { Reset symbolstack }
  4593. symtablestack:=nil;
  4594. systemunit:=nil;
  4595. { create error syms and def }
  4596. generrorsym:=terrorsym.create;
  4597. generrordef:=cerrordef.create;
  4598. { macros }
  4599. initialmacrosymtable:=tmacrosymtable.create(false);
  4600. macrosymtablestack:=TSymtablestack.create;
  4601. macrosymtablestack.push(initialmacrosymtable);
  4602. {$ifdef UNITALIASES}
  4603. { unit aliases }
  4604. unitaliases:=TFPHashObjectList.create;
  4605. {$endif}
  4606. dupnr:=0;
  4607. end;
  4608. procedure DoneSymtable;
  4609. begin
  4610. generrorsym.owner:=nil;
  4611. generrorsym.free;
  4612. generrordef.owner:=nil;
  4613. generrordef.free;
  4614. initialmacrosymtable.free;
  4615. macrosymtablestack.free;
  4616. {$ifdef UNITALIASES}
  4617. unitaliases.free;
  4618. {$endif}
  4619. end;
  4620. end.