symtable.pas 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082
  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. { for some reason a compiler built with 3.3.1 fails building the libxml2
  1074. package if the below define is not defined and thus the code snippet is
  1075. part of the destructor itself and not a nested procedure; until that bug
  1076. is fixed this is used as a workaround :/ }
  1077. {$define codegen_workaround}
  1078. {$ifdef codegen_workaround}
  1079. procedure free_mop_list(mop:tmanagementoperator);
  1080. var
  1081. i : longint;
  1082. begin
  1083. if assigned(mop_list[mop]) then
  1084. for i:=0 to mop_list[mop].count-1 do
  1085. dispose(pmanagementoperator_offset_entry(mop_list[mop][i]));
  1086. mop_list[mop].free;
  1087. end;
  1088. {$endif codegen_workaround}
  1089. var
  1090. mop : tmanagementoperator;
  1091. {$ifndef codegen_workaround}
  1092. i : longint;
  1093. {$endif codegen_workaround}
  1094. begin
  1095. if refcount>1 then
  1096. exit;
  1097. {$ifdef llvm}
  1098. fllvmst.free;
  1099. {$endif llvm}
  1100. for mop:=low(tmanagementoperator) to high(tmanagementoperator) do
  1101. begin
  1102. {$ifdef codegen_workaround}
  1103. free_mop_list(mop);
  1104. {$else codegen_workaround}
  1105. if assigned(mop_list[mop]) then
  1106. for i:=0 to mop_list[mop].count-1 do
  1107. dispose(pmanagementoperator_offset_entry(mop_list[mop][i]));
  1108. mop_list[mop].free;
  1109. {$endif codegen_workaround}
  1110. end;
  1111. inherited destroy;
  1112. end;
  1113. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  1114. begin
  1115. if ppufile.readentry<>ibrecsymtableoptions then
  1116. Message(unit_f_ppu_read_error);
  1117. recordalignment:=shortint(ppufile.getbyte);
  1118. usefieldalignment:=shortint(ppufile.getbyte);
  1119. recordalignmin:=shortint(ppufile.getbyte);
  1120. if (usefieldalignment=C_alignment) then
  1121. fieldalignment:=shortint(ppufile.getbyte);
  1122. ppufile.getset(tppuset1(has_fields_with_mop));
  1123. inherited ppuload(ppufile);
  1124. end;
  1125. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  1126. var
  1127. oldtyp : byte;
  1128. begin
  1129. oldtyp:=ppufile.entrytyp;
  1130. ppufile.entrytyp:=subentryid;
  1131. { in case of classes using C alignment, the alignment of the parent
  1132. affects the alignment of fields of the childs }
  1133. ppufile.putbyte(byte(recordalignment));
  1134. ppufile.putbyte(byte(usefieldalignment));
  1135. ppufile.putbyte(byte(recordalignmin));
  1136. if (usefieldalignment=C_alignment) then
  1137. ppufile.putbyte(byte(fieldalignment));
  1138. { it's not really a "symtableoption", but loading this from the record
  1139. def requires storing the set in the recorddef at least between
  1140. ppuload and deref/derefimpl }
  1141. ppufile.putset(tppuset1(has_fields_with_mop));
  1142. ppufile.writeentry(ibrecsymtableoptions);
  1143. inherited ppuwrite(ppufile);
  1144. ppufile.entrytyp:=oldtyp;
  1145. end;
  1146. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  1147. begin
  1148. { optimal alignment of the record when declaring a variable of this }
  1149. { type is independent of the packrecords setting }
  1150. if (fieldoffs mod fieldalign) = 0 then
  1151. result:=fieldalign
  1152. else if (fieldalign >= 16) and
  1153. ((fieldoffs mod 16) = 0) and
  1154. ((fieldalign mod 16) = 0) then
  1155. result:=16
  1156. else if (fieldalign >= 8) and
  1157. ((fieldoffs mod 8) = 0) and
  1158. ((fieldalign mod 8) = 0) then
  1159. result:=8
  1160. else if (fieldalign >= 4) and
  1161. ((fieldoffs mod 4) = 0) and
  1162. ((fieldalign mod 4) = 0) then
  1163. result:=4
  1164. else if (fieldalign >= 2) and
  1165. ((fieldoffs mod 2) = 0) and
  1166. ((fieldalign mod 2) = 0) then
  1167. result:=2
  1168. else
  1169. result:=1;
  1170. end;
  1171. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1172. var
  1173. varalignrecord: shortint;
  1174. begin
  1175. case usefieldalignment of
  1176. C_alignment:
  1177. varalignrecord:=used_align(varalign,recordalignmin,current_settings.alignment.maxCrecordalign);
  1178. mac68k_alignment:
  1179. varalignrecord:=2;
  1180. else
  1181. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1182. end;
  1183. recordalignment:=max(recordalignment,varalignrecord);
  1184. end;
  1185. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1186. var
  1187. l : asizeint;
  1188. varalign : shortint;
  1189. vardef : tdef;
  1190. begin
  1191. if (sym.owner<>self) then
  1192. internalerror(200602031);
  1193. if sym.fieldoffset<>-1 then
  1194. internalerror(200602032);
  1195. { set visibility for the symbol }
  1196. sym.visibility:=vis;
  1197. { this symbol can't be loaded to a register }
  1198. sym.varregable:=vr_none;
  1199. { management operators }
  1200. if sym.vardef.typ in [recorddef,objectdef] then
  1201. has_fields_with_mop:=has_fields_with_mop + tabstractrecordsymtable(tabstractrecorddef(sym.vardef).symtable).has_fields_with_mop;
  1202. if sym.vardef.typ=recorddef then
  1203. has_fields_with_mop:=has_fields_with_mop + trecordsymtable(trecorddef(sym.vardef).symtable).managementoperators;
  1204. { Calculate field offset }
  1205. l:=sym.getsize;
  1206. vardef:=sym.vardef;
  1207. varalign:=vardef.structalignment;
  1208. case usefieldalignment of
  1209. bit_alignment:
  1210. begin
  1211. { bitpacking only happens for ordinals, the rest is aligned at }
  1212. { 1 byte (compatible with GPC/GCC) }
  1213. if is_ordinal(vardef) then
  1214. begin
  1215. sym.fieldoffset:=databitsize;
  1216. l:=sym.getpackedbitsize;
  1217. end
  1218. else
  1219. begin
  1220. databitsize:=_datasize*8;
  1221. sym.fieldoffset:=databitsize;
  1222. if (l>high(asizeint) div 8) then
  1223. Message(sym_e_segment_too_large);
  1224. l:=l*8;
  1225. end;
  1226. if varalign=0 then
  1227. varalign:=size_2_align(l);
  1228. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1229. { bit packed records are limited to high(aint) bits }
  1230. { instead of bytes to avoid double precision }
  1231. { arithmetic in offset calculations }
  1232. if int64(l)>high(asizeint)-sym.fieldoffset then
  1233. begin
  1234. Message(sym_e_segment_too_large);
  1235. _datasize:=high(asizeint);
  1236. databitsize:=high(asizeint);
  1237. end
  1238. else
  1239. begin
  1240. databitsize:=sym.fieldoffset+l;
  1241. _datasize:=(databitsize+7) div 8;
  1242. end;
  1243. { rest is not applicable }
  1244. exit;
  1245. end;
  1246. else
  1247. begin
  1248. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1249. if l>high(asizeint)-sym.fieldoffset then
  1250. begin
  1251. Message(sym_e_segment_too_large);
  1252. _datasize:=high(asizeint);
  1253. end
  1254. else
  1255. _datasize:=sym.fieldoffset+l;
  1256. { Calc alignment needed for this record }
  1257. alignrecord(sym.fieldoffset,varalign);
  1258. end;
  1259. end;
  1260. end;
  1261. function field_alignment_compare(item1, item2: pointer): integer;
  1262. var
  1263. field1: tfieldvarsym absolute item1;
  1264. field2: tfieldvarsym absolute item2;
  1265. begin
  1266. { we don't care about static fields, those become global variables }
  1267. if (sp_static in field1.symoptions) or
  1268. (sp_static in field2.symoptions) then
  1269. exit(0);
  1270. { sort from large to small alignment, and in case of the same alignment
  1271. in declaration order (items declared close together are possibly
  1272. also related and hence possibly used together -> putting them next
  1273. to each other can improve cache behaviour) }
  1274. result:=field2.vardef.alignment-field1.vardef.alignment;
  1275. if result=0 then
  1276. result:=field1.fieldoffset-field2.fieldoffset;
  1277. end;
  1278. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1279. var
  1280. fieldvs, insertfieldvs: tfieldvarsym;
  1281. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1282. i, j, bestfieldindex: longint;
  1283. globalfieldalignment,
  1284. prevglobalfieldalignment,
  1285. newfieldalignment: shortint;
  1286. changed: boolean;
  1287. begin
  1288. if maybereorder and
  1289. (cs_opt_reorder_fields in current_settings.optimizerswitches) and
  1290. (list.count>1) then
  1291. begin
  1292. { assign dummy field offsets so we can know their order in the
  1293. sorting routine }
  1294. for i:=0 to list.count-1 do
  1295. begin
  1296. fieldvs:=tfieldvarsym(list[i]);
  1297. if sp_static in fieldvs.symoptions then
  1298. continue;
  1299. fieldvs.fieldoffset:=i;
  1300. end;
  1301. { sort the non-class fields to minimise losses due to alignment }
  1302. list.sort(@field_alignment_compare);
  1303. { now fill up gaps caused by alignment skips with smaller fields
  1304. where possible }
  1305. repeat
  1306. i:=0;
  1307. base:=_datasize;
  1308. globalfieldalignment:=fieldalignment;
  1309. changed:=false;
  1310. while i<list.count do
  1311. begin
  1312. fieldvs:=tfieldvarsym(list[i]);
  1313. if sp_static in fieldvs.symoptions then
  1314. begin
  1315. inc(i);
  1316. continue;
  1317. end;
  1318. prevglobalfieldalignment:=globalfieldalignment;
  1319. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1320. newfieldalignment:=globalfieldalignment;
  1321. { size of the gap between the end of the previous field and
  1322. the start of the current one }
  1323. space:=fieldoffset-base;
  1324. bestspaceleft:=space;
  1325. while space>0 do
  1326. begin
  1327. bestfieldindex:=-1;
  1328. bestinsertfieldoffset:=-1;
  1329. for j:=i+1 to list.count-1 do
  1330. begin
  1331. insertfieldvs:=tfieldvarsym(list[j]);
  1332. if sp_static in insertfieldvs.symoptions then
  1333. continue;
  1334. insertfieldsize:=insertfieldvs.getsize;
  1335. { can the new field fit possibly in the gap? }
  1336. if insertfieldsize<=space then
  1337. begin
  1338. { restore globalfieldalignment to situation before
  1339. the original field was inserted }
  1340. globalfieldalignment:=prevglobalfieldalignment;
  1341. { at what offset would it be inserted? (this new
  1342. field has its own alignment requirements, which
  1343. may make it impossible to fit after all) }
  1344. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1345. globalfieldalignment:=prevglobalfieldalignment;
  1346. { taking into account the alignment, does it still
  1347. fit and if so, does it fit better than the
  1348. previously found best fit? }
  1349. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1350. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1351. begin
  1352. { new best fit }
  1353. bestfieldindex:=j;
  1354. bestinsertfieldoffset:=insertfieldoffset;
  1355. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1356. if bestspaceleft=0 then
  1357. break;
  1358. end;
  1359. end;
  1360. end;
  1361. { if we didn't find any field to fit, stop trying for this
  1362. gap }
  1363. if bestfieldindex=-1 then
  1364. break;
  1365. changed:=true;
  1366. { we found a field to insert -> adjust the new base
  1367. address }
  1368. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1369. { update globalfieldalignment for this newly inserted
  1370. field }
  1371. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1372. { move the new field before the current one }
  1373. list.move(bestfieldindex,i);
  1374. { and skip the new field (which is now at position i) }
  1375. inc(i);
  1376. { there may be more space left -> continue }
  1377. space:=bestspaceleft;
  1378. end;
  1379. if base>fieldoffset then
  1380. internalerror(2012071302);
  1381. { check the next field }
  1382. base:=fieldoffset+fieldvs.getsize;
  1383. { since the original field had the same or greater alignment
  1384. than anything we inserted before it, the global field
  1385. alignment is still the same now as it was originally after
  1386. inserting that field }
  1387. globalfieldalignment:=newfieldalignment;
  1388. inc(i);
  1389. end;
  1390. { there may be small gaps left *before* inserted fields }
  1391. until not changed;
  1392. end;
  1393. { reset the dummy field offsets }
  1394. for i:=0 to list.count-1 do
  1395. begin
  1396. fieldvs:=tfieldvarsym(list[i]);
  1397. if sp_static in fieldvs.symoptions then
  1398. continue;
  1399. fieldvs.fieldoffset:=-1;
  1400. end;
  1401. { finally, set the actual field offsets }
  1402. for i:=0 to list.count-1 do
  1403. begin
  1404. fieldvs:=tfieldvarsym(list[i]);
  1405. { static data fields are already inserted in the globalsymtable }
  1406. if not(sp_static in fieldvs.symoptions) then
  1407. begin
  1408. { read_record_fields already set the visibility of the fields,
  1409. because a single list can contain symbols with different
  1410. visibility }
  1411. addfield(fieldvs,fieldvs.visibility);
  1412. end;
  1413. end;
  1414. end;
  1415. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1416. var
  1417. i: longint;
  1418. sym: tsym;
  1419. begin
  1420. { there could be multiple fields in case of a variant record }
  1421. if (defowner.typ=recorddef) and
  1422. trecorddef(defowner).isunion then
  1423. internalerror(2014090403);
  1424. for i:=0 to SymList.count-1 do
  1425. begin
  1426. sym:=tsym(symlist[i]);
  1427. if is_normal_fieldvarsym(sym) and
  1428. (tfieldvarsym(sym).fieldoffset>=offset) then
  1429. begin
  1430. result:=tfieldvarsym(sym);
  1431. exit;
  1432. end;
  1433. end;
  1434. result:=nil;
  1435. end;
  1436. procedure tabstractrecordsymtable.addalignmentpadding;
  1437. var
  1438. padded_datasize: asizeint;
  1439. begin
  1440. { make the record size aligned correctly so it can be
  1441. used as elements in an array. For C records we
  1442. use the fieldalignment, because that is updated with the
  1443. used alignment. }
  1444. if (padalignment = 1) then
  1445. case usefieldalignment of
  1446. C_alignment:
  1447. padalignment:=fieldalignment;
  1448. { bitpacked }
  1449. bit_alignment:
  1450. padalignment:=1;
  1451. { mac68k: always round to multiple of 2 }
  1452. mac68k_alignment:
  1453. padalignment:=2;
  1454. { default/no packrecords specified }
  1455. 0:
  1456. padalignment:=recordalignment
  1457. { specific packrecords setting -> use as upper limit }
  1458. else
  1459. padalignment:=min(recordalignment,usefieldalignment);
  1460. end;
  1461. padded_datasize:=align(_datasize,padalignment);
  1462. _paddingsize:=padded_datasize-_datasize;
  1463. _datasize:=padded_datasize;
  1464. end;
  1465. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1466. begin
  1467. { Enums must also be available outside the record scope,
  1468. insert in the owner of this symtable }
  1469. if def.typ=enumdef then
  1470. defowner.owner.insertdef(def)
  1471. else
  1472. inherited insertdef(def);
  1473. end;
  1474. function tabstractrecordsymtable.is_packed: boolean;
  1475. begin
  1476. result:=usefieldalignment=bit_alignment;
  1477. end;
  1478. function tabstractrecordsymtable.has_double_field(out def1,def2:tdef; out offset:integer): integer;
  1479. var
  1480. i,cnt: longint;
  1481. currentsymlist: TFPHashObjectList;
  1482. currentdef: tdef;
  1483. sym: tfieldvarsym;
  1484. begin
  1485. has_double_field := 0;
  1486. offset := 0;
  1487. if (defowner.typ=recorddef) and
  1488. trecorddef(defowner).isunion then
  1489. exit;
  1490. currentsymlist:=symlist;
  1491. if currentsymlist = nil then
  1492. exit;
  1493. if currentsymlist.Count <> 2 then
  1494. exit;
  1495. currentdef := nil;
  1496. if is_normal_fieldvarsym(tsym(currentsymlist[0])) then
  1497. begin
  1498. sym:=tfieldvarsym(currentsymlist[0]);
  1499. def1 := sym.vardef;
  1500. end
  1501. else
  1502. exit;
  1503. if is_normal_fieldvarsym(tsym(currentsymlist[1])) then
  1504. begin
  1505. sym:=tfieldvarsym(currentsymlist[1]);
  1506. def2 := sym.vardef;
  1507. end
  1508. else
  1509. exit;
  1510. offset := sym.fieldoffset;
  1511. if(def2.typ = def1.typ)then
  1512. cnt := 2
  1513. else
  1514. cnt := 1;
  1515. if((offset = 0))then
  1516. cnt := 0;
  1517. has_double_field := cnt;
  1518. end;
  1519. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1520. var
  1521. i: longint;
  1522. currentsymlist: TFPHashObjectList;
  1523. currentdef: tdef;
  1524. sym: tfieldvarsym;
  1525. begin
  1526. result:=false;
  1527. def:=generrordef;
  1528. { If a record contains a union, it does not contain a "single
  1529. non-composite field" in the context of certain ABIs requiring
  1530. special treatment for such records }
  1531. if (defowner.typ=recorddef) and
  1532. trecorddef(defowner).isunion then
  1533. exit;
  1534. { a record/object can contain other things than fields }
  1535. currentsymlist:=symlist;
  1536. { recurse in arrays and records }
  1537. repeat
  1538. sym:=nil;
  1539. { record has one field? }
  1540. for i:=0 to currentsymlist.Count-1 do
  1541. begin
  1542. if is_normal_fieldvarsym(tsym(currentsymlist[i])) then
  1543. begin
  1544. if result then
  1545. begin
  1546. result:=false;
  1547. exit;
  1548. end;
  1549. result:=true;
  1550. sym:=tfieldvarsym(currentsymlist[i]);
  1551. end;
  1552. end;
  1553. if assigned(sym) then
  1554. begin
  1555. { if the field is an array, does it contain one element? }
  1556. currentdef:=sym.vardef;
  1557. while (currentdef.typ=arraydef) and
  1558. not is_special_array(currentdef) do
  1559. begin
  1560. if tarraydef(currentdef).elecount<>1 then
  1561. begin
  1562. result:=false;
  1563. exit;
  1564. end;
  1565. currentdef:=tarraydef(currentdef).elementdef;
  1566. end;
  1567. { if the array element is again a record, continue descending }
  1568. if currentdef.typ=recorddef then
  1569. begin
  1570. { the record might be empty, so reset the result until we've
  1571. really found something }
  1572. result:=false;
  1573. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1574. end
  1575. else
  1576. begin
  1577. { otherwise we found the type of the single element }
  1578. def:=currentdef;
  1579. exit;
  1580. end;
  1581. end
  1582. else
  1583. exit
  1584. until false;
  1585. end;
  1586. procedure tabstractrecordsymtable.do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  1587. var
  1588. sym : tsym absolute data;
  1589. fsym : tfieldvarsym absolute data;
  1590. mop : tmanagementoperator;
  1591. entry : pmanagementoperator_offset_entry;
  1592. sublist : tfplist;
  1593. i : longint;
  1594. begin
  1595. if not is_normal_fieldvarsym(sym) then
  1596. exit;
  1597. if not is_record(fsym.vardef) and not is_object(fsym.vardef) and not is_cppclass(fsym.vardef) then
  1598. exit;
  1599. mop:=tmanagementoperator(ptruint(arg));
  1600. if not assigned(mop_list[mop]) then
  1601. internalerror(2018082303);
  1602. if is_record(fsym.vardef) then
  1603. begin
  1604. if mop in trecordsymtable(trecorddef(fsym.vardef).symtable).managementoperators then
  1605. begin
  1606. new(entry);
  1607. entry^.pd:=search_management_operator(mop,fsym.vardef);
  1608. if not assigned(entry^.pd) then
  1609. internalerror(2018082302);
  1610. entry^.offset:=fsym.fieldoffset;
  1611. mop_list[mop].add(entry);
  1612. end;
  1613. end;
  1614. sublist:=tfplist.create;
  1615. tabstractrecordsymtable(tabstractrecorddef(fsym.vardef).symtable).get_managementoperator_offset_list(mop,sublist);
  1616. for i:=0 to sublist.count-1 do
  1617. begin
  1618. entry:=pmanagementoperator_offset_entry(sublist[i]);
  1619. entry^.offset:=entry^.offset+fsym.fieldoffset;
  1620. mop_list[mop].add(entry);
  1621. end;
  1622. { we don't need to remove the entries as they become part of list }
  1623. sublist.free;
  1624. end;
  1625. procedure tabstractrecordsymtable.get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  1626. var
  1627. i : longint;
  1628. entry,entrycopy : pmanagementoperator_offset_entry;
  1629. begin
  1630. if not assigned(list) then
  1631. internalerror(2018082301);
  1632. if mop=mop_none then
  1633. exit;
  1634. if not (mop in has_fields_with_mop) then
  1635. { none of the fields or one of the field's fields has the requested operator }
  1636. exit;
  1637. if not assigned(mop_list[mop]) then
  1638. begin
  1639. mop_list[mop]:=tfplist.create;
  1640. SymList.ForEachCall(@do_get_managementoperator_offset_list,pointer(ptruint(mop)));
  1641. end;
  1642. for i:=0 to mop_list[mop].count-1 do
  1643. begin
  1644. entry:=pmanagementoperator_offset_entry(mop_list[mop][i]);
  1645. New(entrycopy);
  1646. entrycopy^:=entry^;
  1647. list.add(entrycopy);
  1648. end;
  1649. end;
  1650. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1651. begin
  1652. _datasize:=val;
  1653. if (usefieldalignment=bit_alignment) then
  1654. { can overflow in non bitpacked records }
  1655. databitsize:=val*8;
  1656. end;
  1657. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1658. var
  1659. l : asizeint;
  1660. varalignfield,
  1661. varalign : shortint;
  1662. vardef : tdef;
  1663. begin
  1664. { Calculate field offset }
  1665. l:=sym.getsize;
  1666. vardef:=sym.vardef;
  1667. varalign:=vardef.structalignment;
  1668. case usefieldalignment of
  1669. bit_alignment:
  1670. { has to be handled separately }
  1671. internalerror(2012071301);
  1672. C_alignment:
  1673. begin
  1674. { Calc the alignment size for C style records }
  1675. if (varalign>4) and
  1676. ((varalign mod 4)<>0) and
  1677. (vardef.typ=arraydef) then
  1678. Message1(sym_w_wrong_C_pack,vardef.typename);
  1679. if varalign=0 then
  1680. varalign:=l;
  1681. if (globalfieldalignment<current_settings.alignment.maxCrecordalign) then
  1682. begin
  1683. if (varalign>16) and (globalfieldalignment<32) then
  1684. globalfieldalignment:=32
  1685. else if (varalign>12) and (globalfieldalignment<16) then
  1686. globalfieldalignment:=16
  1687. { 12 is needed for long double }
  1688. else if (varalign>8) and (globalfieldalignment<12) then
  1689. globalfieldalignment:=12
  1690. else if (varalign>4) and (globalfieldalignment<8) then
  1691. globalfieldalignment:=8
  1692. else if (varalign>2) and (globalfieldalignment<4) then
  1693. globalfieldalignment:=4
  1694. else if (varalign>1) and (globalfieldalignment<2) then
  1695. globalfieldalignment:=2;
  1696. end;
  1697. globalfieldalignment:=min(globalfieldalignment,current_settings.alignment.maxCrecordalign);
  1698. end;
  1699. mac68k_alignment:
  1700. begin
  1701. { mac68k alignment (C description):
  1702. * char is aligned to 1 byte
  1703. * everything else (except vector) is aligned to 2 bytes
  1704. * vector is aligned to 16 bytes
  1705. }
  1706. if l>1 then
  1707. globalfieldalignment:=2
  1708. else
  1709. globalfieldalignment:=1;
  1710. varalign:=2;
  1711. end;
  1712. end;
  1713. if varalign=0 then
  1714. varalign:=size_2_align(l);
  1715. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1716. result:=align(base,varalignfield);
  1717. end;
  1718. function tabstractrecordsymtable.iscurrentunit: boolean;
  1719. begin
  1720. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1721. end;
  1722. {****************************************************************************
  1723. TRecordSymtable
  1724. ****************************************************************************}
  1725. constructor trecordsymtable.create(const n:string;usealign,recordminalign:shortint);
  1726. begin
  1727. inherited create(n,usealign,recordminalign);
  1728. symtabletype:=recordsymtable;
  1729. end;
  1730. { this procedure is reserved for inserting case variant into
  1731. a record symtable }
  1732. { the offset is the location of the start of the variant
  1733. and datasize and dataalignment corresponds to
  1734. the complete size (see code in pdecl unit) PM }
  1735. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : asizeint);
  1736. var
  1737. sym : tsym;
  1738. def : tdef;
  1739. i : integer;
  1740. varalignrecord,varalign,
  1741. storesize,storealign : asizeint;
  1742. bitsize: tcgint;
  1743. begin
  1744. storesize:=_datasize;
  1745. storealign:=fieldalignment;
  1746. _datasize:=offset;
  1747. if (usefieldalignment=bit_alignment) then
  1748. databitsize:=offset*8;
  1749. { We move the ownership of the defs and symbols to the new recordsymtable.
  1750. The old unionsymtable keeps the references, but doesn't own the
  1751. objects anymore }
  1752. unionst.DefList.OwnsObjects:=false;
  1753. unionst.SymList.OwnsObjects:=false;
  1754. { copy symbols }
  1755. for i:=0 to unionst.SymList.Count-1 do
  1756. begin
  1757. sym:=TSym(unionst.SymList[i]);
  1758. if not is_normal_fieldvarsym(sym) then
  1759. internalerror(200601272);
  1760. if tfieldvarsym(sym).fieldoffset=0 then
  1761. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1762. { add to this record symtable, checking for duplicate names }
  1763. // unionst.SymList.List.List^[i].Data:=nil;
  1764. insertsym(sym);
  1765. varalign:=tfieldvarsym(sym).vardef.alignment;
  1766. if varalign=0 then
  1767. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1768. { retrieve size }
  1769. if (usefieldalignment=bit_alignment) then
  1770. begin
  1771. { bit packed records are limited to high(aint) bits }
  1772. { instead of bytes to avoid double precision }
  1773. { arithmetic in offset calculations }
  1774. if is_ordinal(tfieldvarsym(sym).vardef) then
  1775. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1776. else
  1777. begin
  1778. bitsize:=tfieldvarsym(sym).getsize;
  1779. if (bitsize>high(asizeint) div 8) then
  1780. Message(sym_e_segment_too_large);
  1781. bitsize:=bitsize*8;
  1782. end;
  1783. if bitsize>high(asizeint)-databitsize then
  1784. begin
  1785. Message(sym_e_segment_too_large);
  1786. _datasize:=high(asizeint);
  1787. databitsize:=high(asizeint);
  1788. end
  1789. else
  1790. begin
  1791. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1792. _datasize:=(databitsize+7) div 8;
  1793. end;
  1794. tfieldvarsym(sym).fieldoffset:=databitsize;
  1795. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1796. end
  1797. else
  1798. begin
  1799. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1800. begin
  1801. Message(sym_e_segment_too_large);
  1802. _datasize:=high(asizeint);
  1803. end
  1804. else
  1805. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1806. { update address }
  1807. tfieldvarsym(sym).fieldoffset:=_datasize;
  1808. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1809. end;
  1810. { update alignment of this record }
  1811. if (usefieldalignment<>C_alignment) and
  1812. (usefieldalignment<>mac68k_alignment) then
  1813. recordalignment:=max(recordalignment,varalignrecord);
  1814. end;
  1815. { update alignment for C records }
  1816. if (usefieldalignment=C_alignment) and
  1817. (usefieldalignment<>mac68k_alignment) then
  1818. recordalignment:=max(recordalignment,unionst.recordalignment);
  1819. { Register defs in the new record symtable }
  1820. for i:=0 to unionst.DefList.Count-1 do
  1821. begin
  1822. def:=TDef(unionst.DefList[i]);
  1823. def.ChangeOwner(self);
  1824. end;
  1825. _datasize:=storesize;
  1826. fieldalignment:=storealign;
  1827. { If a record contains a union, it does not contain a "single
  1828. non-composite field" in the context of certain ABIs requiring
  1829. special treatment for such records }
  1830. if defowner.typ=recorddef then
  1831. trecorddef(defowner).isunion:=true;
  1832. end;
  1833. procedure trecordsymtable.includemanagementoperator(mop:tmanagementoperator);
  1834. begin
  1835. if mop in managementoperators then
  1836. exit;
  1837. include(managementoperators,mop);
  1838. end;
  1839. {****************************************************************************
  1840. TObjectSymtable
  1841. ****************************************************************************}
  1842. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign:shortint);
  1843. begin
  1844. inherited create(n,usealign,recordminalign);
  1845. symtabletype:=ObjectSymtable;
  1846. defowner:=adefowner;
  1847. end;
  1848. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1849. var
  1850. hsym: tsym;
  1851. warn: boolean;
  1852. begin
  1853. result:=false;
  1854. if not assigned(defowner) then
  1855. internalerror(200602061);
  1856. { procsym and propertysym have special code
  1857. to override values in inherited classes. For other
  1858. symbols check for duplicates (but for internal symbols only in this
  1859. symtable, not the whole hierarchy) }
  1860. if not(sym.typ in [procsym,propertysym]) and
  1861. not (sp_internal in tsym(sym).symoptions) then
  1862. begin
  1863. { but private ids can be reused }
  1864. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1865. if assigned(hsym) and
  1866. (
  1867. (
  1868. not(m_delphi in current_settings.modeswitches) and
  1869. is_visible_for_object(hsym,tobjectdef(defowner))
  1870. ) or
  1871. (
  1872. { In Delphi, you can repeat members of a parent class. You can't }
  1873. { do this for objects however, and you (obviouly) can't }
  1874. { declare two fields with the same name in a single class }
  1875. (m_delphi in current_settings.modeswitches) and
  1876. (
  1877. is_object(tdef(defowner)) or
  1878. (hsym.owner = self)
  1879. )
  1880. )
  1881. ) then
  1882. begin
  1883. { only warn when a parameter/local variable in a method
  1884. conflicts with a category method, because this can easily
  1885. happen due to all possible categories being imported via
  1886. CocoaAll }
  1887. warn:=
  1888. (is_objccategory(tdef(hsym.owner.defowner)) or
  1889. is_classhelper(tdef(hsym.owner.defowner))) and
  1890. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1891. DuplicateSym(hashedid,sym,hsym,warn);
  1892. result:=true;
  1893. end;
  1894. end
  1895. else
  1896. result:=inherited checkduplicate(hashedid,sym);
  1897. end;
  1898. {$ifdef llvm}
  1899. {****************************************************************************
  1900. tLlvmShadowSymtableEntry
  1901. ****************************************************************************}
  1902. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1903. begin
  1904. fdef:=def;
  1905. ffieldoffset:=fieldoffset;
  1906. end;
  1907. {****************************************************************************
  1908. TLlvmShadowSymtable
  1909. ****************************************************************************}
  1910. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1911. begin
  1912. result:=get_by_llvm_index(f.llvmfieldnr)
  1913. end;
  1914. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1915. begin
  1916. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1917. end;
  1918. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1919. begin
  1920. equivst:=st;
  1921. curroffset:=0;
  1922. symdeflist:=tfpobjectlist.create(true);
  1923. generate;
  1924. end;
  1925. destructor tllvmshadowsymtable.destroy;
  1926. begin
  1927. symdeflist.free;
  1928. end;
  1929. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1930. var
  1931. sizectr,
  1932. tmpsize: aint;
  1933. begin
  1934. case equivst.usefieldalignment of
  1935. bit_alignment:
  1936. begin
  1937. { curoffset: bit address after the previous field. }
  1938. { llvm has no special support for bitfields in records, }
  1939. { so we replace them with plain bytes. }
  1940. { as soon as a single bit of a byte is allocated, we }
  1941. { allocate the byte in the llvm shadow record }
  1942. if (fieldoffset>curroffset) then
  1943. curroffset:=align(curroffset,8);
  1944. { fields in bitpacked records always start either right }
  1945. { after the previous one, or at the next byte boundary. }
  1946. if (curroffset<>fieldoffset) then
  1947. internalerror(2008051002);
  1948. if is_ordinal(vardef) then
  1949. begin
  1950. tmpsize:=vardef.packedbitsize;
  1951. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1952. inc(curroffset,tmpsize);
  1953. tmpsize:=0;
  1954. while sizectr<>0 do
  1955. begin
  1956. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1957. dec(sizectr);
  1958. inc(tmpsize);
  1959. end;
  1960. end
  1961. else
  1962. begin
  1963. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1964. if not(derefclass) then
  1965. inc(curroffset,vardef.size*8)
  1966. else
  1967. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1968. end;
  1969. end
  1970. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1971. begin
  1972. { curoffset: address right after the previous field }
  1973. while (fieldoffset>curroffset) do
  1974. begin
  1975. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1976. inc(curroffset);
  1977. end;
  1978. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1979. if not(derefclass) then
  1980. inc(curroffset,vardef.size)
  1981. else
  1982. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1983. end
  1984. else
  1985. { default for llvm, don't add explicit padding }
  1986. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1987. end
  1988. end;
  1989. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1990. begin
  1991. if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1992. begin
  1993. if equivst.usefieldalignment=bit_alignment then
  1994. curroffset:=align(curroffset,8) div 8;
  1995. { add padding fields }
  1996. while (finalsize>curroffset) do
  1997. begin
  1998. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1999. inc(curroffset);
  2000. end;
  2001. end;
  2002. end;
  2003. function field_offset_compare(item1, item2: pointer): integer;
  2004. var
  2005. field1: tfieldvarsym absolute item1;
  2006. field2: tfieldvarsym absolute item2;
  2007. begin
  2008. result:=field1.fieldoffset-field2.fieldoffset;
  2009. end;
  2010. procedure tllvmshadowsymtable.preprocess(out tempsymlist, variantstarts: tfplist);
  2011. var
  2012. fieldvs: tfieldvarsym;
  2013. lastvariantstartoffset, prevfieldoffset: aint;
  2014. newalignment: aint;
  2015. i, j: longint;
  2016. sorttempsymlist: boolean;
  2017. begin
  2018. i:=0;
  2019. variantstarts:=nil;
  2020. tempsymlist:=tfplist.create;
  2021. sorttempsymlist:=false;
  2022. prevfieldoffset:=-1;
  2023. while (i<equivst.symlist.count) do
  2024. begin
  2025. if not is_normal_fieldvarsym(tsym(equivst.symlist[i])) then
  2026. begin
  2027. inc(i);
  2028. continue;
  2029. end;
  2030. fieldvs:=tfieldvarsym(equivst.symlist[i]);
  2031. tempsymlist.Add(fieldvs);
  2032. { a "better" algorithm might be to use the largest }
  2033. { variant in case of (bit)packing, since then }
  2034. { alignment doesn't matter }
  2035. if (vo_is_first_field in fieldvs.varoptions) then
  2036. begin
  2037. { we assume that all fields are processed in order. }
  2038. if assigned(variantstarts) then
  2039. lastvariantstartoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  2040. else
  2041. begin
  2042. lastvariantstartoffset:=-1;
  2043. variantstarts:=tfplist.create;
  2044. end;
  2045. { new variant at same level as last one: use if higher alignment }
  2046. if (lastvariantstartoffset=fieldvs.fieldoffset) then
  2047. begin
  2048. if (equivst.usefieldalignment<>bit_alignment) then
  2049. newalignment:=used_align(fieldvs.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  2050. else
  2051. newalignment:=1;
  2052. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  2053. variantstarts[variantstarts.count-1]:=fieldvs;
  2054. end
  2055. { variant at deeper level than last one -> add }
  2056. else if (lastvariantstartoffset<fieldvs.fieldoffset) then
  2057. variantstarts.add(fieldvs)
  2058. else
  2059. begin
  2060. { a variant at a less deep level, so backtrack }
  2061. j:=variantstarts.count-2;
  2062. while (j>=0) do
  2063. begin
  2064. if (tfieldvarsym(variantstarts[j]).fieldoffset=fieldvs.fieldoffset) then
  2065. break;
  2066. dec(j);
  2067. end;
  2068. if (j<0) then
  2069. internalerror(2008051003);
  2070. { new variant has higher alignment? }
  2071. if (equivst.fieldalignment<>bit_alignment) then
  2072. newalignment:=used_align(fieldvs.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  2073. else
  2074. newalignment:=1;
  2075. { yes, replace and remove previous nested variants }
  2076. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  2077. begin
  2078. variantstarts[j]:=fieldvs;
  2079. variantstarts.count:=j+1;
  2080. end
  2081. { no, skip this variant }
  2082. else
  2083. begin
  2084. inc(i);
  2085. while (i<equivst.symlist.count) and
  2086. (not is_normal_fieldvarsym(tsym(equivst.symlist[i])) or
  2087. (tfieldvarsym(equivst.symlist[i]).fieldoffset>fieldvs.fieldoffset)) do
  2088. begin
  2089. if is_normal_fieldvarsym(tsym(equivst.symlist[i])) then
  2090. tempsymlist.Add(equivst.symlist[i]);
  2091. inc(i);
  2092. end;
  2093. continue;
  2094. end;
  2095. end;
  2096. end;
  2097. if not assigned(variantstarts) and
  2098. (fieldvs.fieldoffset<prevfieldoffset) then
  2099. sorttempsymlist:=true;
  2100. prevfieldoffset:=fieldvs.fieldoffset;
  2101. inc(i);
  2102. end;
  2103. if sorttempsymlist then
  2104. tempsymlist.Sort(@field_offset_compare);
  2105. end;
  2106. procedure tllvmshadowsymtable.buildtable(tempsymlist, variantstarts: tfplist);
  2107. var
  2108. lastvaroffsetprocessed: aint;
  2109. i, symcount, varcount: longint;
  2110. fieldvs: tfieldvarsym;
  2111. begin
  2112. { if it's an object/class, the first entry is the parent (if there is one) }
  2113. if (equivst.symtabletype=objectsymtable) and
  2114. assigned(tobjectdef(equivst.defowner).childof) then
  2115. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  2116. symcount:=tempsymlist.count;
  2117. varcount:=0;
  2118. i:=0;
  2119. lastvaroffsetprocessed:=-1;
  2120. while (i<symcount) do
  2121. begin
  2122. fieldvs:=tfieldvarsym(tempsymlist[i]);
  2123. { start of a new variant? }
  2124. if (vo_is_first_field in fieldvs.varoptions) then
  2125. begin
  2126. { if we want to process the same variant offset twice, it means that we }
  2127. { got to the end and are trying to process the next variant part -> stop }
  2128. if (fieldvs.fieldoffset<=lastvaroffsetprocessed) then
  2129. break;
  2130. if (varcount>=variantstarts.count) then
  2131. internalerror(2008051005);
  2132. { new variant part -> use the one with the biggest alignment }
  2133. fieldvs:=tfieldvarsym(variantstarts[varcount]);
  2134. i:=tempsymlist.indexof(fieldvs);
  2135. lastvaroffsetprocessed:=fieldvs.fieldoffset;
  2136. inc(varcount);
  2137. if (i<0) then
  2138. internalerror(2008051004);
  2139. end;
  2140. appenddefoffset(fieldvs.vardef,fieldvs.fieldoffset,false);
  2141. inc(i);
  2142. end;
  2143. addalignmentpadding(equivst.datasize);
  2144. end;
  2145. procedure tllvmshadowsymtable.buildmapping(tempsymlist, variantstarts: tfplist);
  2146. var
  2147. fieldvs: tfieldvarsym;
  2148. i, varcount: longint;
  2149. shadowindex: longint;
  2150. symcount : longint;
  2151. begin
  2152. varcount:=0;
  2153. shadowindex:=0;
  2154. symcount:=tempsymlist.count;
  2155. i:=0;
  2156. while (i<symcount) do
  2157. begin
  2158. fieldvs:=tfieldvarsym(tempsymlist[i]);
  2159. { start of a new variant? }
  2160. if (vo_is_first_field in fieldvs.varoptions) then
  2161. begin
  2162. { back up to a less deeply nested variant level? }
  2163. while fieldvs.fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset do
  2164. dec(varcount);
  2165. { it's possible that some variants are more deeply nested than the
  2166. one we recorded in the shadowsymtable (since we recorded the one
  2167. with the biggest alignment, not necessarily the biggest one in size
  2168. }
  2169. if fieldvs.fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset then
  2170. varcount:=variantstarts.count-1
  2171. else if fieldvs.fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset then
  2172. internalerror(2008051006);
  2173. { reset the shadowindex to the start of this variant. }
  2174. { in case the llvmfieldnr is not (yet) set for this }
  2175. { field, shadowindex will simply be reset to zero and }
  2176. { we'll start searching from the start of the record }
  2177. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  2178. if (varcount<pred(variantstarts.count)) then
  2179. inc(varcount);
  2180. end;
  2181. { find the last shadowfield whose offset <= the current field's offset }
  2182. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<fieldvs.fieldoffset) and
  2183. (shadowindex<symdeflist.count-1) and
  2184. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=fieldvs.fieldoffset) do
  2185. inc(shadowindex);
  2186. { set the field number and potential offset from that field (in case }
  2187. { of overlapping variants) }
  2188. fieldvs.llvmfieldnr:=shadowindex;
  2189. fieldvs.offsetfromllvmfield:=
  2190. fieldvs.fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  2191. inc(i);
  2192. end;
  2193. end;
  2194. procedure tllvmshadowsymtable.generate;
  2195. var
  2196. variantstarts, tempsymlist: tfplist;
  2197. begin
  2198. { first go through the entire record and }
  2199. { store the fieldvarsyms of the variants }
  2200. { with the highest alignment }
  2201. preprocess(tempsymlist, variantstarts);
  2202. { now go through the regular fields and the selected variants, }
  2203. { and add them to the llvm shadow record symtable }
  2204. buildtable(tempsymlist, variantstarts);
  2205. { finally map all original fields to the llvm definition }
  2206. buildmapping(tempsymlist, variantstarts);
  2207. variantstarts.free;
  2208. tempsymlist.free;
  2209. end;
  2210. {$endif llvm}
  2211. {****************************************************************************
  2212. TAbstractSubSymtable
  2213. ****************************************************************************}
  2214. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  2215. var
  2216. oldtyp : byte;
  2217. begin
  2218. oldtyp:=ppufile.entrytyp;
  2219. ppufile.entrytyp:=subentryid;
  2220. inherited ppuwrite(ppufile);
  2221. ppufile.entrytyp:=oldtyp;
  2222. end;
  2223. {****************************************************************************
  2224. TAbstractLocalSymtable
  2225. ****************************************************************************}
  2226. function tabstractlocalsymtable.count_locals:longint;
  2227. var
  2228. i : longint;
  2229. sym : tsym;
  2230. begin
  2231. result:=0;
  2232. for i:=0 to SymList.Count-1 do
  2233. begin
  2234. sym:=tsym(SymList[i]);
  2235. { Count only varsyms, but ignore the funcretsym }
  2236. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  2237. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  2238. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  2239. (tstoredsym(sym).refs>0)) then
  2240. inc(result);
  2241. end;
  2242. end;
  2243. function tabstractlocalsymtable.iscurrentunit: boolean;
  2244. begin
  2245. Result:=
  2246. assigned(defowner) and
  2247. defowner.owner.iscurrentunit;
  2248. end;
  2249. {****************************************************************************
  2250. TLocalSymtable
  2251. ****************************************************************************}
  2252. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  2253. begin
  2254. inherited create('');
  2255. defowner:=adefowner;
  2256. symtabletype:=localsymtable;
  2257. symtablelevel:=level;
  2258. end;
  2259. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2260. var
  2261. hsym : tsym;
  2262. begin
  2263. if not assigned(defowner) or
  2264. (defowner.typ<>procdef) then
  2265. internalerror(200602042);
  2266. result:=false;
  2267. hsym:=tsym(FindWithHash(hashedid));
  2268. if assigned(hsym) then
  2269. begin
  2270. { a local and the function can have the same
  2271. name in TP and Delphi, but RESULT not }
  2272. if (m_duplicate_names in current_settings.modeswitches) and
  2273. (hsym.typ in [absolutevarsym,localvarsym]) and
  2274. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  2275. not((m_result in current_settings.modeswitches) and
  2276. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  2277. HideSym(hsym)
  2278. else
  2279. DuplicateSym(hashedid,sym,hsym,false);
  2280. result:=true;
  2281. exit;
  2282. end;
  2283. { check also parasymtable, this needs to be done here because
  2284. of the special situation with the funcret sym that needs to be
  2285. hidden for tp and delphi modes }
  2286. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  2287. if assigned(hsym) then
  2288. begin
  2289. { a local and the function can have the same
  2290. name in TP and Delphi, but RESULT not }
  2291. if (m_duplicate_names in current_settings.modeswitches) and
  2292. (sym.typ in [absolutevarsym,localvarsym]) and
  2293. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  2294. not((m_result in current_settings.modeswitches) and
  2295. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  2296. Hidesym(sym)
  2297. else
  2298. DuplicateSym(hashedid,sym,hsym,false);
  2299. result:=true;
  2300. exit;
  2301. end;
  2302. { check ObjectSymtable, skip this for funcret sym because
  2303. that will always be positive because it has the same name
  2304. as the procsym }
  2305. if not is_funcret_sym(sym) and
  2306. (defowner.typ=procdef) and
  2307. assigned(tprocdef(defowner).struct) and
  2308. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2309. (
  2310. not(m_duplicate_names in current_settings.modeswitches) or
  2311. is_object(tprocdef(defowner).struct)
  2312. ) then
  2313. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2314. end;
  2315. {****************************************************************************
  2316. TParaSymtable
  2317. ****************************************************************************}
  2318. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2319. begin
  2320. inherited create('');
  2321. defowner:=adefowner;
  2322. symtabletype:=parasymtable;
  2323. symtablelevel:=level;
  2324. end;
  2325. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2326. begin
  2327. result:=inherited checkduplicate(hashedid,sym);
  2328. if result then
  2329. exit;
  2330. if not(m_duplicate_names in current_settings.modeswitches) and
  2331. assigned(defowner) and (defowner.typ=procdef) and
  2332. assigned(tprocdef(defowner).struct) and
  2333. assigned(tprocdef(defowner).owner) and
  2334. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2335. (
  2336. not(m_delphi in current_settings.modeswitches) or
  2337. is_object(tprocdef(defowner).struct)
  2338. ) then
  2339. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2340. end;
  2341. {****************************************************************************
  2342. TAbstractUniTSymtable
  2343. ****************************************************************************}
  2344. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2345. begin
  2346. inherited create(n);
  2347. moduleid:=id;
  2348. end;
  2349. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2350. var
  2351. hsym : tsym;
  2352. begin
  2353. result:=false;
  2354. hsym:=tsym(FindWithHash(hashedid));
  2355. if assigned(hsym) then
  2356. begin
  2357. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2358. exit;
  2359. if hsym.typ=symconst.namespacesym then
  2360. begin
  2361. case sym.typ of
  2362. symconst.namespacesym:;
  2363. symconst.unitsym:
  2364. begin
  2365. 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 }
  2366. tnamespacesym(hsym).unitsym:=tsym(sym);
  2367. end
  2368. else
  2369. HideSym(hsym);
  2370. end;
  2371. end
  2372. else
  2373. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2374. unit, the unit can then not be accessed anymore using
  2375. <unit>.<id>, so we can hide the symbol.
  2376. Do the same if we add a namespace and there is a unit with the same name }
  2377. if (hsym.typ=symconst.unitsym) and
  2378. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2379. begin
  2380. HideSym(hsym);
  2381. if sym.typ=symconst.namespacesym then
  2382. tnamespacesym(sym).unitsym:=tsym(hsym);
  2383. end
  2384. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2385. in this case, copy the isoindex and make the original symbol invisible }
  2386. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2387. and (tprogramparasym(hsym).isoindex<>0) then
  2388. begin
  2389. HideSym(hsym);
  2390. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2391. end
  2392. else if (m_iso in current_settings.modeswitches) and (hsym.typ=unitsym) then
  2393. HideSym(hsym)
  2394. else
  2395. DuplicateSym(hashedid,sym,hsym,false);
  2396. result:=true;
  2397. exit;
  2398. end;
  2399. end;
  2400. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2401. begin
  2402. result:=find(n);
  2403. if assigned(result)and(result.typ<>namespacesym)then
  2404. result:=nil;
  2405. end;
  2406. function tabstractuniTSymtable.iscurrentunit:boolean;
  2407. begin
  2408. result:=assigned(current_module) and
  2409. (
  2410. (current_module.globalsymtable=self) or
  2411. (current_module.localsymtable=self)
  2412. );
  2413. end;
  2414. function tabstractuniTSymtable.needs_init_final: boolean;
  2415. begin
  2416. if not init_final_check_done then
  2417. begin
  2418. result:=inherited needs_init_final;
  2419. if not result then
  2420. begin
  2421. result:=has_class_condestructors;
  2422. if result then
  2423. include(tableoptions,sto_needs_init_final);
  2424. end;
  2425. end;
  2426. result:=sto_needs_init_final in tableoptions;
  2427. end;
  2428. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2429. var
  2430. p:integer;
  2431. n,ns:string;
  2432. oldsym:TSymEntry;
  2433. begin
  2434. insertsym(sym);
  2435. n:=sym.realname;
  2436. p:=pos('.',n);
  2437. ns:='';
  2438. while p>0 do
  2439. begin
  2440. if ns='' then
  2441. ns:=copy(n,1,p-1)
  2442. else
  2443. ns:=ns+'.'+copy(n,1,p-1);
  2444. system.delete(n,1,p);
  2445. oldsym:=findnamespace(upper(ns));
  2446. if not assigned(oldsym) then
  2447. insertsym(cnamespacesym.create(ns));
  2448. p:=pos('.',n);
  2449. end;
  2450. end;
  2451. procedure CheckForClassConDestructors(p:TObject;arg:pointer);
  2452. var
  2453. result: pboolean absolute arg;
  2454. begin
  2455. if result^ then
  2456. exit;
  2457. if (tdef(p).typ in [objectdef,recorddef]) and
  2458. not (df_generic in tdef(p).defoptions) then
  2459. begin
  2460. { first check the class... }
  2461. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  2462. result^:=true;
  2463. { ... and then also check all subclasses }
  2464. if not result^ then
  2465. tabstractrecorddef(p).symtable.deflist.foreachcall(@CheckForClassConDestructors,arg);
  2466. end;
  2467. end;
  2468. function tabstractuniTSymtable.has_class_condestructors: boolean;
  2469. begin
  2470. result:=false;
  2471. deflist.foreachcall(@CheckForClassConDestructors,@result);
  2472. end;
  2473. {****************************************************************************
  2474. TStaticSymtable
  2475. ****************************************************************************}
  2476. constructor tstaticsymtable.create(const n : string;id:word);
  2477. begin
  2478. inherited create(n,id);
  2479. symtabletype:=staticsymtable;
  2480. symtablelevel:=main_program_level;
  2481. currentvisibility:=vis_private;
  2482. end;
  2483. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2484. begin
  2485. inherited ppuload(ppufile);
  2486. { now we can deref the syms and defs }
  2487. deref(false);
  2488. end;
  2489. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2490. begin
  2491. inherited ppuwrite(ppufile);
  2492. end;
  2493. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2494. begin
  2495. result:=inherited checkduplicate(hashedid,sym);
  2496. if not result and
  2497. (current_module.localsymtable=self) and
  2498. assigned(current_module.globalsymtable) then
  2499. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2500. end;
  2501. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2502. begin
  2503. result:=inherited findnamespace(n);
  2504. if not assigned(result) and
  2505. (current_module.localsymtable=self) and
  2506. assigned(current_module.globalsymtable) then
  2507. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2508. end;
  2509. {****************************************************************************
  2510. TGlobalSymtable
  2511. ****************************************************************************}
  2512. constructor tglobalsymtable.create(const n : string;id:word);
  2513. begin
  2514. inherited create(n,id);
  2515. symtabletype:=globalsymtable;
  2516. symtablelevel:=main_program_level;
  2517. end;
  2518. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2519. begin
  2520. inherited ppuload(ppufile);
  2521. { now we can deref the syms and defs }
  2522. deref(false);
  2523. end;
  2524. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2525. begin
  2526. { write the symtable entries }
  2527. inherited ppuwrite(ppufile);
  2528. end;
  2529. {*****************************************************************************
  2530. tspecializesymtable
  2531. *****************************************************************************}
  2532. constructor tspecializesymtable.create(const n : string;id:word);
  2533. begin
  2534. inherited create(n,id);
  2535. { the specialize symtable does not own the syms and defs as they are all
  2536. moved to a different symtable before the symtable is destroyed; this
  2537. avoids calls to "extract" }
  2538. symlist.ownsobjects:=false;
  2539. deflist.ownsobjects:=false;
  2540. end;
  2541. function tspecializesymtable.iscurrentunit: boolean;
  2542. begin
  2543. Result:=true;
  2544. end;
  2545. {****************************************************************************
  2546. TWITHSYMTABLE
  2547. ****************************************************************************}
  2548. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2549. begin
  2550. inherited create('');
  2551. symtabletype:=withsymtable;
  2552. withrefnode:=refnode;
  2553. { Replace SymList with the passed symlist }
  2554. SymList.free;
  2555. SymList:=ASymList;
  2556. defowner:=aowner;
  2557. end;
  2558. destructor twithsymtable.destroy;
  2559. begin
  2560. if refcount>1 then
  2561. exit;
  2562. withrefnode.free;
  2563. { Disable SymList because we don't Own it }
  2564. SymList:=nil;
  2565. inherited destroy;
  2566. end;
  2567. procedure twithsymtable.clear;
  2568. begin
  2569. { remove no entry from a withsymtable as it is only a pointer to the
  2570. recorddef or objectdef symtable }
  2571. end;
  2572. procedure twithsymtable.insertdef(def:TDefEntry);
  2573. begin
  2574. { Definitions can't be registered in the withsymtable
  2575. because the withsymtable is removed after the with block.
  2576. We can't easily solve it here because the next symtable in the
  2577. stack is not known. }
  2578. internalerror(200602046);
  2579. end;
  2580. {****************************************************************************
  2581. TSTT_ExceptionSymtable
  2582. ****************************************************************************}
  2583. constructor tstt_excepTSymtable.create;
  2584. begin
  2585. inherited create('');
  2586. symtabletype:=exceptsymtable;
  2587. end;
  2588. {****************************************************************************
  2589. TMacroSymtable
  2590. ****************************************************************************}
  2591. constructor tmacrosymtable.create(exported: boolean);
  2592. begin
  2593. inherited create('');
  2594. if exported then
  2595. symtabletype:=exportedmacrosymtable
  2596. else
  2597. symtabletype:=localmacrosymtable;
  2598. symtablelevel:=main_program_level;
  2599. end;
  2600. {****************************************************************************
  2601. TEnumSymtable
  2602. ****************************************************************************}
  2603. procedure tenumsymtable.insertsym(sym: TSymEntry; checkdup: boolean);
  2604. var
  2605. value: longint;
  2606. def: tenumdef;
  2607. begin
  2608. // defowner = nil only when we are loading from ppu
  2609. if defowner<>nil then
  2610. begin
  2611. { First entry? Then we need to set the minval }
  2612. value:=tenumsym(sym).value;
  2613. def:=tenumdef(defowner);
  2614. if SymList.count=0 then
  2615. begin
  2616. if value>0 then
  2617. def.has_jumps:=true;
  2618. def.setmin(value);
  2619. def.setmax(value);
  2620. end
  2621. else
  2622. begin
  2623. { check for jumps }
  2624. if value>def.max+1 then
  2625. def.has_jumps:=true;
  2626. { update low and high }
  2627. if def.min>value then
  2628. def.setmin(value);
  2629. if def.max<value then
  2630. def.setmax(value);
  2631. end;
  2632. end;
  2633. inherited insertsym(sym, checkdup);
  2634. end;
  2635. constructor tenumsymtable.create(adefowner: tdef);
  2636. begin
  2637. inherited Create('');
  2638. symtabletype:=enumsymtable;
  2639. defowner:=adefowner;
  2640. end;
  2641. {****************************************************************************
  2642. TArraySymtable
  2643. ****************************************************************************}
  2644. procedure tarraysymtable.insertdef(def: TDefEntry);
  2645. begin
  2646. { Enums must also be available outside the record scope,
  2647. insert in the owner of this symtable }
  2648. if def.typ=enumdef then
  2649. defowner.owner.insertdef(def)
  2650. else
  2651. inherited insertdef(def);
  2652. end;
  2653. constructor tarraysymtable.create(adefowner: tdef);
  2654. begin
  2655. inherited Create('');
  2656. symtabletype:=arraysymtable;
  2657. defowner:=adefowner;
  2658. end;
  2659. {*****************************************************************************
  2660. Helper Routines
  2661. *****************************************************************************}
  2662. function FullTypeName(def,otherdef:tdef):string;
  2663. var
  2664. s1,s2 : string;
  2665. begin
  2666. if def.typ in [objectdef,recorddef] then
  2667. s1:=tabstractrecorddef(def).RttiName
  2668. else
  2669. s1:=def.typename;
  2670. { When the names are the same try to include the unit name }
  2671. if assigned(otherdef) and
  2672. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2673. begin
  2674. s2:=otherdef.typename;
  2675. if upper(s1)=upper(s2) then
  2676. s1:=def.owner.realname^+'.'+s1;
  2677. end;
  2678. FullTypeName:=s1;
  2679. end;
  2680. function generate_nested_name(symtable:tsymtable;const delimiter:string):string;
  2681. begin
  2682. result:='';
  2683. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2684. begin
  2685. if (result='') then
  2686. if symtable.name<>nil then
  2687. result:=symtable.name^
  2688. else
  2689. else
  2690. if symtable.name<>nil then
  2691. result:=symtable.name^+delimiter+result
  2692. else
  2693. result:=delimiter+result;
  2694. symtable:=symtable.defowner.owner;
  2695. end;
  2696. end;
  2697. function generate_objectpascal_helper_key(def:tdef):TSymStr;
  2698. begin
  2699. if not assigned(def) then
  2700. internalerror(2013020501);
  2701. if def.typ in [recorddef,objectdef] then
  2702. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2703. else
  2704. result:=make_mangledname('',def.owner,def.typesym.name);
  2705. end;
  2706. procedure incompatibletypes(def1,def2:tdef);
  2707. begin
  2708. { When there is an errordef there is already an error message show }
  2709. if (def2.typ=errordef) or
  2710. (def1.typ=errordef) then
  2711. exit;
  2712. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2713. end;
  2714. procedure hidesym(sym:TSymEntry);
  2715. begin
  2716. sym.realname:='$hidden'+sym.realname;
  2717. tsym(sym).visibility:=vis_hidden;
  2718. end;
  2719. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2720. var
  2721. st : TSymtable;
  2722. filename : TIDString;
  2723. begin
  2724. if not warn then
  2725. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2726. else
  2727. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2728. { Write hint where the original symbol was found }
  2729. st:=finduniTSymtable(origsym.owner);
  2730. with tsym(origsym).fileinfo do
  2731. begin
  2732. if assigned(st) and
  2733. (st.symtabletype=globalsymtable) and
  2734. st.iscurrentunit then
  2735. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2736. else if assigned(st.name) then
  2737. begin
  2738. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2739. if filename<>'' then
  2740. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2741. else
  2742. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2743. end;
  2744. end;
  2745. { Rename duplicate sym to an unreachable name, but it can be
  2746. inserted in the symtable without errors }
  2747. inc(dupnr);
  2748. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2749. if assigned(dupsym) then
  2750. include(tsym(dupsym).symoptions,sp_implicitrename);
  2751. end;
  2752. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2753. begin
  2754. result:=false;
  2755. if not assigned(sym) or not (sym is tstoredsym) then
  2756. Internalerror(2011081101);
  2757. { For generics a dummy symbol without the parameter count is created
  2758. if such a symbol not yet exists so that different parts of the
  2759. parser can find that symbol. If that symbol is still a
  2760. undefineddef we replace the generic dummy symbol's
  2761. name with a "dup" name and use the new symbol as the generic dummy
  2762. symbol }
  2763. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2764. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2765. (m_delphi in current_settings.modeswitches) then
  2766. begin
  2767. inc(dupnr);
  2768. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2769. include(tsym(sym).symoptions,sp_implicitrename);
  2770. { we need to find the new symbol now if checking for a dummy }
  2771. include(symoptions,sp_generic_dummy);
  2772. result:=true;
  2773. end;
  2774. end;
  2775. procedure check_systemunit_loaded; inline;
  2776. begin
  2777. if systemunit=nil then
  2778. Message(sym_f_systemunitnotloaded);
  2779. end;
  2780. procedure write_system_parameter_lists(const name:string);
  2781. var
  2782. srsym:tprocsym;
  2783. begin
  2784. check_systemunit_loaded;
  2785. srsym:=tprocsym(systemunit.find(name));
  2786. if not assigned(srsym) or not (srsym.typ=procsym) then
  2787. internalerror(2016060302);
  2788. srsym.write_parameter_lists(nil);
  2789. end;
  2790. {*****************************************************************************
  2791. Search
  2792. *****************************************************************************}
  2793. procedure addsymref(sym:tsym;def:tdef);
  2794. var
  2795. owner,procowner : tsymtable;
  2796. begin
  2797. { for symbols used in preprocessor expressions, we don't want to
  2798. increase references count (for smaller final binaries) }
  2799. if not assigned(current_scanner) then
  2800. internalerror(2017050601);
  2801. if current_scanner.in_preproc_comp_expr then
  2802. exit;
  2803. { symbol uses count }
  2804. sym.IncRefCount;
  2805. owner:=sym.owner;
  2806. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2807. owner:=tdef(owner.defowner).owner;
  2808. if assigned(current_module) and
  2809. (owner.symtabletype=globalsymtable) then
  2810. begin
  2811. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2812. internalerror(200501152);
  2813. { unit uses count }
  2814. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2815. { Note: don't check the symtable directly as owner might be
  2816. a specialize symtable which is a globalsymtable as well }
  2817. if (
  2818. assigned(current_module.globalsymtable) and
  2819. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2820. ) or (
  2821. assigned(current_module.localsymtable) and
  2822. (current_module.localsymtable.moduleid<>owner.moduleid)
  2823. ) then
  2824. { symbol is imported from another unit }
  2825. current_module.addimportedsym(sym);
  2826. end;
  2827. { static symbols that are used in public functions must be exported
  2828. for packages as well }
  2829. if ([tf_supports_packages,tf_supports_hidden_symbols]<=target_info.flags) and
  2830. (owner.symtabletype=staticsymtable) and
  2831. assigned(current_procinfo) and
  2832. (
  2833. (
  2834. (sym.typ=staticvarsym) and
  2835. ([vo_is_public,vo_has_global_ref]*tstaticvarsym(sym).varoptions=[])
  2836. ) or (
  2837. (sym.typ=localvarsym) and
  2838. assigned(tlocalvarsym(sym).defaultconstsym) and
  2839. ([vo_is_public,vo_has_global_ref]*tstaticvarsym(tlocalvarsym(sym).defaultconstsym).varoptions=[])
  2840. ) or (
  2841. (sym.typ=procsym) and
  2842. assigned(def) and
  2843. (def.typ=procdef) and
  2844. not (df_has_global_ref in def.defoptions) and
  2845. not (po_public in tprocdef(def).procoptions)
  2846. )
  2847. ) then
  2848. begin
  2849. procowner:=current_procinfo.procdef.owner;
  2850. while procowner.symtabletype in [objectsymtable,recordsymtable] do
  2851. procowner:=tdef(procowner.defowner).owner;
  2852. if procowner.symtabletype=globalsymtable then
  2853. begin
  2854. if sym.typ=procsym then
  2855. current_procinfo.add_local_ref_def(def)
  2856. else if sym.typ=staticvarsym then
  2857. current_procinfo.add_local_ref_sym(sym)
  2858. else
  2859. current_procinfo.add_local_ref_sym(tlocalvarsym(sym).defaultconstsym);
  2860. end;
  2861. end;
  2862. end;
  2863. procedure addsymref(sym:tsym);
  2864. begin
  2865. addsymref(sym,nil);
  2866. end;
  2867. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2868. begin
  2869. result:=nesteddef=ownerdef;
  2870. if not result and
  2871. { types declared locally in a record method are not defined in the
  2872. record itself }
  2873. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2874. assigned(nesteddef.owner.defowner) then
  2875. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2876. end;
  2877. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2878. begin
  2879. result:=assigned(childsym) and (childsym.owner=symtable);
  2880. if not result and assigned(childsym) and
  2881. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2882. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2883. end;
  2884. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2885. begin
  2886. result:=false;
  2887. if not assigned(def1) or not assigned(def2) then
  2888. exit;
  2889. { for both defs walk to the topmost generic }
  2890. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2891. def1:=tdef(def1.owner.defowner);
  2892. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2893. def2:=tdef(def2.owner.defowner);
  2894. result:=def1=def2;
  2895. end;
  2896. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2897. var
  2898. uname : string;
  2899. begin
  2900. { TODO : check regarding arrays and records declared as their type }
  2901. if not (def.typ in [recorddef,objectdef]) then
  2902. internalerror(2012051501);
  2903. uname:=upper(srsym.realname);
  2904. repeat
  2905. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2906. begin
  2907. result:=def;
  2908. exit;
  2909. end;
  2910. def:=tdef(def.owner.defowner);
  2911. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2912. result:=nil;
  2913. end;
  2914. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2915. begin
  2916. { TODO : check regarding arrays and records declared as their type }
  2917. if not (nesteddef.typ in [recorddef,objectdef]) then
  2918. internalerror(2012051601);
  2919. repeat
  2920. if tstoreddef(nesteddef).genericdef=genericdef then
  2921. begin
  2922. resultdef:=nesteddef;
  2923. result:=true;
  2924. exit;
  2925. end;
  2926. nesteddef:=tdef(nesteddef.owner.defowner);
  2927. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2928. resultdef:=nil;
  2929. result:=false;
  2930. end;
  2931. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2932. symvisibility: visibility of the symbol
  2933. contextobjdef: via which def the symbol is accessed, e.g.:
  2934. fieldname:=1 -> contextobjdef = current_structdef
  2935. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2936. }
  2937. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2938. var
  2939. curstruct : tabstractrecorddef;
  2940. function is_current_unit(st:tsymtable):boolean;
  2941. begin
  2942. result :=
  2943. (
  2944. (
  2945. assigned(curstruct) and
  2946. (st.moduleid=curstruct.symtable.moduleid)
  2947. ) or
  2948. (
  2949. not assigned(curstruct) and
  2950. st.iscurrentunit
  2951. )
  2952. );
  2953. end;
  2954. var
  2955. orgcontextobjdef,
  2956. orgsymownerdef,
  2957. symownerdef : tabstractrecorddef;
  2958. nonlocalst : tsymtable;
  2959. isspezproc : boolean;
  2960. function check_strict_private:boolean;
  2961. begin
  2962. result:=assigned(curstruct) and
  2963. is_owned_by(curstruct,symownerdef);
  2964. end;
  2965. function check_strict_protected:boolean;
  2966. function owner_hierarchy_related(nested,check:tabstractrecorddef):boolean;
  2967. var
  2968. owner:tabstractrecorddef;
  2969. begin
  2970. result:=true;
  2971. repeat
  2972. if def_is_related(nested,check) then
  2973. exit;
  2974. if nested.owner.symtabletype in [recordsymtable,objectsymtable] then
  2975. nested:=tabstractrecorddef(nested.owner.defowner)
  2976. else
  2977. break;
  2978. until not assigned(nested);
  2979. result:=false;
  2980. end;
  2981. begin
  2982. result:=(
  2983. { access from nested class (specialization case) }
  2984. assigned(curstruct) and
  2985. is_owned_by(curstruct,symownerdef)
  2986. ) or
  2987. (
  2988. { access from nested class (non-specialization case) }
  2989. (orgsymownerdef<>symownerdef) and
  2990. assigned(curstruct) and
  2991. is_owned_by(curstruct,orgsymownerdef)
  2992. ) or
  2993. (
  2994. { access from child class (specialization case) }
  2995. assigned(contextobjdef) and
  2996. assigned(curstruct) and
  2997. owner_hierarchy_related(contextobjdef,symownerdef) and
  2998. def_is_related(curstruct,contextobjdef)
  2999. ) or
  3000. (
  3001. { access from child class (non-specialization case) }
  3002. assigned(orgcontextobjdef) and
  3003. (
  3004. (orgcontextobjdef<>contextobjdef) or
  3005. (orgsymownerdef<>symownerdef)
  3006. ) and
  3007. assigned(curstruct) and
  3008. owner_hierarchy_related(orgcontextobjdef,orgsymownerdef) and
  3009. def_is_related(curstruct,orgcontextobjdef)
  3010. ) or
  3011. (
  3012. { helpers can access strict protected symbols }
  3013. is_objectpascal_helper(contextobjdef) and
  3014. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  3015. ) or
  3016. (
  3017. { same as above, but from context of call node inside
  3018. helper method }
  3019. is_objectpascal_helper(curstruct) and
  3020. def_is_related(tobjectdef(curstruct).extendeddef,symownerdef)
  3021. );
  3022. end;
  3023. begin
  3024. result:=false;
  3025. { Get objdectdef owner of the symtable for the is_related checks }
  3026. if not assigned(symst) or
  3027. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  3028. internalerror(200810285);
  3029. symownerdef:=tabstractrecorddef(symst.defowner);
  3030. orgsymownerdef:=symownerdef;
  3031. orgcontextobjdef:=contextobjdef;
  3032. { for specializations we need to check the visibility of the generic,
  3033. not the specialization (at least when comparing outside of the
  3034. specialization }
  3035. if df_specialization in symownerdef.defoptions then
  3036. begin
  3037. if not (symownerdef.genericdef.typ in [objectdef,recorddef]) then
  3038. internalerror(2024020901);
  3039. orgsymownerdef:=symownerdef;
  3040. symownerdef:=tabstractrecorddef(symownerdef.genericdef);
  3041. end;
  3042. if assigned(contextobjdef) and (df_specialization in contextobjdef.defoptions) then
  3043. begin
  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 (current_structdef.genericdef.typ in [objectdef,recorddef]) then
  3052. internalerror(2024030903);
  3053. curstruct:=tabstractrecorddef(current_structdef.genericdef)
  3054. end
  3055. else
  3056. curstruct:=current_structdef;
  3057. { specializations might belong to a localsymtable or parasymtable }
  3058. nonlocalst:=symownerdef.owner;
  3059. if tstoreddef(symownerdef).is_specialization then
  3060. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  3061. nonlocalst:=nonlocalst.defowner.owner;
  3062. isspezproc:=false;
  3063. if assigned(current_procinfo) then
  3064. begin
  3065. if current_procinfo.procdef.is_specialization and
  3066. assigned(current_procinfo.procdef.struct) then
  3067. isspezproc:=true;
  3068. end;
  3069. case symvisibility of
  3070. vis_private :
  3071. begin
  3072. { private symbols are allowed when we are in the same
  3073. module as they are defined }
  3074. result:=check_strict_private or
  3075. (
  3076. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  3077. is_current_unit(nonlocalst)
  3078. ) or
  3079. ( // the case of specialize inside the generic declaration and nested types
  3080. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  3081. (
  3082. assigned(curstruct) and
  3083. (
  3084. (curstruct=symownerdef) or
  3085. (curstruct.owner.moduleid=symownerdef.symtable.moduleid)
  3086. )
  3087. ) or
  3088. (
  3089. not assigned(curstruct) and
  3090. (symownerdef.owner.iscurrentunit)
  3091. ) or
  3092. { access from a generic method that belongs to the class
  3093. but that is specialized elsewere }
  3094. (
  3095. isspezproc and
  3096. (current_procinfo.procdef.struct=curstruct)
  3097. ) or
  3098. { specializations may access private symbols that their
  3099. generics are allowed to access }
  3100. (
  3101. assigned(curstruct) and
  3102. (df_specialization in curstruct.defoptions) and
  3103. (symst.moduleid=curstruct.genericdef.owner.moduleid)
  3104. )
  3105. );
  3106. end;
  3107. vis_strictprivate :
  3108. begin
  3109. result:=check_strict_private;
  3110. end;
  3111. vis_strictprotected :
  3112. begin
  3113. result:=check_strict_protected;
  3114. end;
  3115. vis_protected :
  3116. begin
  3117. { protected symbols are visible in the module that defines them and
  3118. also visible to related objects. The related object must be defined
  3119. in the current module }
  3120. result:=check_strict_protected or
  3121. (
  3122. (
  3123. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  3124. is_current_unit(nonlocalst)
  3125. ) or
  3126. (
  3127. { context object is inside the current unit and related to
  3128. the symbol owner (specialization case) }
  3129. assigned(contextobjdef) and
  3130. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable,localsymtable]) and
  3131. is_current_unit(contextobjdef.owner) and
  3132. def_is_related(contextobjdef,symownerdef)
  3133. ) or
  3134. (
  3135. { context object is inside the current unit and related to
  3136. the symbol owner (non-specialization case) }
  3137. assigned(orgcontextobjdef) and
  3138. (
  3139. (orgcontextobjdef<>contextobjdef) or
  3140. (orgsymownerdef<>symownerdef)
  3141. ) and
  3142. (orgcontextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable,localsymtable]) and
  3143. is_current_unit(orgcontextobjdef.owner) and
  3144. def_is_related(orgcontextobjdef,orgsymownerdef)
  3145. ) or
  3146. ( // the case of specialize inside the generic declaration and nested types
  3147. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  3148. (
  3149. assigned(curstruct) and
  3150. (
  3151. (curstruct=symownerdef) or
  3152. (curstruct.owner.moduleid=symownerdef.symtable.moduleid)
  3153. )
  3154. ) or
  3155. (
  3156. not assigned(curstruct) and
  3157. (symownerdef.owner.iscurrentunit)
  3158. )
  3159. ) or
  3160. { access from a generic method that belongs to the class
  3161. but that is specialized elsewere }
  3162. (
  3163. isspezproc and
  3164. (current_procinfo.procdef.struct=curstruct)
  3165. ) or
  3166. { specializations may access private symbols that their
  3167. generics are allowed to access }
  3168. (
  3169. assigned(curstruct) and
  3170. (df_specialization in curstruct.defoptions) and
  3171. (symst.moduleid=curstruct.genericdef.owner.moduleid)
  3172. )
  3173. );
  3174. end;
  3175. vis_public,
  3176. vis_published :
  3177. result:=true;
  3178. else
  3179. internalerror(2019050702);
  3180. end;
  3181. if not result then
  3182. begin
  3183. { capturers have access to anything as we assume checks were done
  3184. before the procdef was inserted into the capturer }
  3185. result:=assigned(curstruct) and
  3186. (curstruct.typ=objectdef) and
  3187. (oo_is_capturer in tobjectdef(curstruct).objectoptions);
  3188. end;
  3189. end;
  3190. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  3191. begin
  3192. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  3193. end;
  3194. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  3195. var
  3196. i : longint;
  3197. pd : tprocdef;
  3198. begin
  3199. if sym.typ=procsym then
  3200. begin
  3201. result:=false;
  3202. { A procsym is visible, when there is at least one of the procdefs visible }
  3203. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  3204. begin
  3205. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  3206. if (pd.owner=sym.owner) and
  3207. is_visible_for_object(pd,contextobjdef) then
  3208. begin
  3209. result:=true;
  3210. exit;
  3211. end;
  3212. end;
  3213. { check dummy sym visbility by following associated procsyms }
  3214. if tprocsym(sym).could_be_implicitly_specialized then
  3215. begin
  3216. for i:=0 to tprocsym(sym).genprocsymovlds.count-1 do
  3217. if is_visible_for_object(tsym(tprocsym(sym).genprocsymovlds[i]),contextobjdef) then
  3218. begin
  3219. result:=true;
  3220. exit;
  3221. end;
  3222. end;
  3223. if (tprocsym(sym).procdeflist.count=0) and (sp_generic_dummy in tprocsym(sym).symoptions) then
  3224. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  3225. end
  3226. else
  3227. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  3228. end;
  3229. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3230. begin
  3231. case s[1] of
  3232. internal_macro_escape_unit_namespace_name:
  3233. result:=searchsym_maybe_with_symoption(copy(s,2,length(s)-1),srsym,srsymtable,[ssf_unit_or_namespace_only],sp_none)
  3234. else
  3235. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  3236. end
  3237. end;
  3238. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3239. begin
  3240. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  3241. end;
  3242. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  3243. var
  3244. hashedid: THashedIDString;
  3245. contextstructdef: tabstractrecorddef;
  3246. stackitem: psymtablestackitem;
  3247. begin
  3248. result:=false;
  3249. hashedid.id:=s;
  3250. stackitem:=symtablestack.stack;
  3251. while assigned(stackitem) do
  3252. begin
  3253. srsymtable:=stackitem^.symtable;
  3254. if not(ssf_unit_or_namespace_only in flags) and
  3255. (srsymtable.symtabletype=objectsymtable) then
  3256. begin
  3257. { TODO : implement the search for an option in classes as well }
  3258. if ssf_search_option in flags then
  3259. begin
  3260. result:=false;
  3261. exit;
  3262. end;
  3263. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  3264. begin
  3265. result:=true;
  3266. exit;
  3267. end;
  3268. end
  3269. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  3270. (srsymtable.defowner.typ=undefineddef)) then
  3271. begin
  3272. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3273. { First check if it is a unit/namespace symbol.
  3274. They are visible only if they are from the current unit or
  3275. unit of generic of currently processed specialization. }
  3276. if assigned(srsym) and
  3277. (not(ssf_unit_or_namespace_only in flags) or
  3278. (srsym.typ in [unitsym,namespacesym])) and
  3279. (
  3280. not(srsym.typ in [unitsym,namespacesym]) or
  3281. srsymtable.iscurrentunit or
  3282. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid)) or
  3283. (
  3284. assigned(current_procinfo) and
  3285. (df_specialization in current_procinfo.procdef.defoptions) and
  3286. (current_procinfo.procdef.genericdef.owner.moduleid=srsymtable.moduleid)
  3287. )
  3288. ) and
  3289. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  3290. begin
  3291. { use the class from withsymtable only when it is
  3292. defined in this unit }
  3293. if (srsymtable.symtabletype=withsymtable) and
  3294. assigned(srsymtable.defowner) and
  3295. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  3296. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  3297. (srsymtable.defowner.owner.iscurrentunit) then
  3298. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  3299. else
  3300. contextstructdef:=current_structdef;
  3301. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  3302. is_visible_for_object(srsym,contextstructdef) then
  3303. begin
  3304. { we need to know if a procedure references symbols
  3305. in the static symtable, because then it can't be
  3306. inlined from outside this unit }
  3307. if assigned(current_procinfo) and
  3308. (srsym.owner.symtabletype=staticsymtable) then
  3309. include(current_procinfo.flags,pi_uses_static_symtable);
  3310. if not (ssf_no_addsymref in flags) then
  3311. addsymref(srsym);
  3312. result:=true;
  3313. exit;
  3314. end;
  3315. end;
  3316. end;
  3317. stackitem:=stackitem^.next;
  3318. end;
  3319. srsym:=nil;
  3320. srsymtable:=nil;
  3321. end;
  3322. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  3323. srsymtable:TSymtable;option:tsymoption):boolean;
  3324. begin
  3325. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  3326. end;
  3327. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3328. var
  3329. hashedid : THashedIDString;
  3330. stackitem : psymtablestackitem;
  3331. classh : tobjectdef;
  3332. begin
  3333. result:=false;
  3334. hashedid.id:=s;
  3335. stackitem:=symtablestack.stack;
  3336. while assigned(stackitem) do
  3337. begin
  3338. {
  3339. It is not possible to have type symbols in:
  3340. parameters
  3341. Exception are classes, objects, records, generic definitions and specializations
  3342. that have the parameterized types inserted in the symtable.
  3343. }
  3344. srsymtable:=stackitem^.symtable;
  3345. if (srsymtable.symtabletype=ObjectSymtable) then
  3346. begin
  3347. classh:=tobjectdef(srsymtable.defowner);
  3348. while assigned(classh) do
  3349. begin
  3350. srsymtable:=classh.symtable;
  3351. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3352. if assigned(srsym) and
  3353. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3354. is_visible_for_object(srsym,current_structdef) then
  3355. begin
  3356. addsymref(srsym);
  3357. result:=true;
  3358. exit;
  3359. end;
  3360. classh:=classh.childof;
  3361. end;
  3362. end
  3363. else
  3364. begin
  3365. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3366. if assigned(srsym) and
  3367. (
  3368. not(srsym.typ in [unitsym,namespacesym]) or
  3369. srsymtable.iscurrentunit or
  3370. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  3371. ) and
  3372. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3373. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  3374. begin
  3375. { we need to know if a procedure references symbols
  3376. in the static symtable, because then it can't be
  3377. inlined from outside this unit }
  3378. if assigned(current_procinfo) and
  3379. (srsym.owner.symtabletype=staticsymtable) then
  3380. include(current_procinfo.flags,pi_uses_static_symtable);
  3381. addsymref(srsym);
  3382. result:=true;
  3383. exit;
  3384. end;
  3385. end;
  3386. stackitem:=stackitem^.next;
  3387. end;
  3388. result:=false;
  3389. srsym:=nil;
  3390. srsymtable:=nil;
  3391. end;
  3392. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3393. var
  3394. pmod : tmodule;
  3395. begin
  3396. result:=false;
  3397. if not assigned(pm) then exit;
  3398. pmod:=tmodule(pm);
  3399. if assigned(pmod.globalsymtable) then
  3400. begin
  3401. srsym:=tsym(pmod.globalsymtable.Find(s));
  3402. if assigned(srsym) then
  3403. begin
  3404. srsymtable:=pmod.globalsymtable;
  3405. addsymref(srsym);
  3406. result:=true;
  3407. exit;
  3408. end;
  3409. end;
  3410. { If the module is the current unit we also need
  3411. to search the local symtable }
  3412. if (pmod=current_module) and
  3413. assigned(pmod.localsymtable) then
  3414. begin
  3415. srsym:=tsym(pmod.localsymtable.Find(s));
  3416. if assigned(srsym) then
  3417. begin
  3418. srsymtable:=pmod.localsymtable;
  3419. addsymref(srsym);
  3420. result:=true;
  3421. exit;
  3422. end;
  3423. end;
  3424. srsym:=nil;
  3425. srsymtable:=nil;
  3426. end;
  3427. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  3428. var
  3429. stackitem : psymtablestackitem;
  3430. begin
  3431. result:=false;
  3432. stackitem:=symtablestack.stack;
  3433. while assigned(stackitem) do
  3434. begin
  3435. srsymtable:=stackitem^.symtable;
  3436. if (srsymtable.symtabletype=globalsymtable) and
  3437. (srsymtable.name^=unitname) then
  3438. begin
  3439. srsym:=tsym(srsymtable.find(symname));
  3440. if not assigned(srsym) then
  3441. break;
  3442. result:=true;
  3443. exit;
  3444. end;
  3445. stackitem:=stackitem^.next;
  3446. end;
  3447. { If the module is the current unit we also need
  3448. to search the local symtable }
  3449. if assigned(current_module.localsymtable) and
  3450. (current_module.localsymtable.name^=unitname) then
  3451. begin
  3452. srsymtable:=current_module.localsymtable;
  3453. srsym:=tsym(srsymtable.find(symname));
  3454. if assigned(srsym) then
  3455. begin
  3456. result:=true;
  3457. exit;
  3458. end;
  3459. end;
  3460. end;
  3461. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  3462. begin
  3463. result:=pd;
  3464. if pd.typ<>objectdef then
  3465. exit;
  3466. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  3467. end;
  3468. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  3469. var
  3470. hashedid : THashedIDString;
  3471. stackitem : psymtablestackitem;
  3472. srsymtable : tsymtable;
  3473. srsym : tsym;
  3474. formalname,
  3475. foundname : shortstring;
  3476. formalnameptr,
  3477. foundnameptr: pshortstring;
  3478. begin
  3479. while pd.is_unique_objpasdef do
  3480. begin
  3481. pd:=pd.childof;
  3482. end;
  3483. { not a formal definition -> return it }
  3484. if not(oo_is_formal in pd.objectoptions) then
  3485. begin
  3486. result:=pd;
  3487. exit;
  3488. end;
  3489. hashedid.id:=pd.typesym.name;
  3490. stackitem:=symtablestack.stack;
  3491. while assigned(stackitem) do
  3492. begin
  3493. srsymtable:=stackitem^.symtable;
  3494. { ObjC classes can't appear in generics or as nested class
  3495. definitions. Java classes can. }
  3496. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  3497. (is_java_class_or_interface(pd) and
  3498. (srsymtable.symtabletype=ObjectSymtable)) then
  3499. begin
  3500. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3501. if assigned(srsym) and
  3502. (srsym.typ=typesym) and
  3503. (ttypesym(srsym).typedef.typ=objectdef) and
  3504. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  3505. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3506. begin
  3507. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3508. begin
  3509. { the external name for the formal and the real
  3510. definition must match }
  3511. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  3512. assigned(pd.import_lib) then
  3513. begin
  3514. if assigned(pd.import_lib) then
  3515. formalname:=pd.import_lib^+'.'
  3516. else
  3517. formalname:='';
  3518. formalname:=formalname+pd.objextname^;
  3519. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  3520. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  3521. else
  3522. foundname:='';
  3523. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3524. formalnameptr:=@formalname;
  3525. foundnameptr:=@foundname;
  3526. end
  3527. else
  3528. begin
  3529. formalnameptr:=pd.objextname;
  3530. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3531. end;
  3532. if foundnameptr^<>formalnameptr^ then
  3533. begin
  3534. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3535. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3536. end;
  3537. end;
  3538. result:=tobjectdef(ttypesym(srsym).typedef);
  3539. if assigned(current_procinfo) and
  3540. (srsym.owner.symtabletype=staticsymtable) then
  3541. include(current_procinfo.flags,pi_uses_static_symtable);
  3542. addsymref(srsym);
  3543. exit;
  3544. end;
  3545. end;
  3546. stackitem:=stackitem^.next;
  3547. end;
  3548. { nothing found: optionally give an error and return the original
  3549. (empty) one }
  3550. if erroronfailure then
  3551. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3552. result:=pd;
  3553. end;
  3554. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3555. var
  3556. hashedid : THashedIDString;
  3557. orgclass : tobjectdef;
  3558. i : longint;
  3559. begin
  3560. orgclass:=classh;
  3561. { in case this is a formal class, first find the real definition }
  3562. if assigned(classh) then
  3563. begin
  3564. if (oo_is_formal in classh.objectoptions) then
  3565. classh:=find_real_class_definition(classh,true);
  3566. { The contextclassh is used for visibility. The classh must be equal to
  3567. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3568. parent or a class helper. }
  3569. if not (def_is_related(contextclassh,classh) or
  3570. (is_classhelper(contextclassh) and
  3571. assigned(tobjectdef(contextclassh).extendeddef) and
  3572. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3573. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3574. internalerror(200811161);
  3575. end;
  3576. result:=false;
  3577. hashedid.id:=s;
  3578. { an Objective-C protocol or Java interface can inherit from multiple
  3579. other protocols/interfaces -> use ImplementedInterfaces instead }
  3580. if is_objcprotocol(classh) or
  3581. is_javainterface(classh) then
  3582. begin
  3583. srsymtable:=classh.symtable;
  3584. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3585. if assigned(srsym) and
  3586. is_visible_for_object(srsym,contextclassh) then
  3587. begin
  3588. if not (ssf_no_addsymref in flags) then
  3589. addsymref(srsym);
  3590. result:=true;
  3591. exit;
  3592. end;
  3593. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3594. begin
  3595. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3596. begin
  3597. result:=true;
  3598. exit;
  3599. end;
  3600. end;
  3601. end
  3602. else
  3603. if is_objectpascal_helper(classh) then
  3604. begin
  3605. { helpers have their own obscure search logic... }
  3606. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3607. if result then
  3608. exit;
  3609. end
  3610. else
  3611. begin
  3612. while assigned(classh) do
  3613. begin
  3614. { search for a class helper method first if this is an Object
  3615. Pascal class and we haven't yet found a helper symbol }
  3616. if (classh.objecttype in objecttypes_with_helpers) and
  3617. (ssf_search_helper in flags) then
  3618. begin
  3619. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3620. { an eventual overload inside the extended type's hierarchy
  3621. will be found by tcallcandidates }
  3622. if result then
  3623. exit;
  3624. end;
  3625. srsymtable:=classh.symtable;
  3626. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3627. if assigned(srsym) and
  3628. is_visible_for_object(srsym,contextclassh) then
  3629. begin
  3630. if not (ssf_no_addsymref in flags) then
  3631. addsymref(srsym);
  3632. result:=true;
  3633. exit;
  3634. end;
  3635. classh:=classh.childof;
  3636. end;
  3637. end;
  3638. if is_objcclass(orgclass) then
  3639. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3640. else
  3641. begin
  3642. srsym:=nil;
  3643. srsymtable:=nil;
  3644. end;
  3645. end;
  3646. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3647. var
  3648. hashedid : THashedIDString;
  3649. begin
  3650. result:=false;
  3651. hashedid.id:=s;
  3652. { search for a record helper method first }
  3653. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3654. if result then
  3655. { an eventual overload inside the extended type's hierarchy
  3656. will be found by tcallcandidates }
  3657. exit;
  3658. srsymtable:=recordh.symtable;
  3659. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3660. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3661. begin
  3662. addsymref(srsym);
  3663. result:=true;
  3664. exit;
  3665. end;
  3666. srsym:=nil;
  3667. srsymtable:=nil;
  3668. end;
  3669. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3670. var
  3671. def : tdef;
  3672. i : longint;
  3673. begin
  3674. { in case this is a formal class, first find the real definition }
  3675. if assigned(classh) and
  3676. (oo_is_formal in classh.objectoptions) then
  3677. classh:=find_real_class_definition(classh,true);
  3678. result:=false;
  3679. def:=nil;
  3680. while assigned(classh) do
  3681. begin
  3682. for i:=0 to classh.symtable.DefList.Count-1 do
  3683. begin
  3684. def:=tstoreddef(classh.symtable.DefList[i]);
  3685. { Find also all hidden private methods to
  3686. be compatible with delphi, see tw6203 (PFV) }
  3687. if (def.typ=procdef) and
  3688. (po_msgint in tprocdef(def).procoptions) and
  3689. (tprocdef(def).messageinf.i=msgid) then
  3690. begin
  3691. srdef:=def;
  3692. srsym:=tprocdef(def).procsym;
  3693. srsymtable:=classh.symtable;
  3694. addsymref(srsym);
  3695. result:=true;
  3696. exit;
  3697. end;
  3698. end;
  3699. classh:=classh.childof;
  3700. end;
  3701. srdef:=nil;
  3702. srsym:=nil;
  3703. srsymtable:=nil;
  3704. end;
  3705. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3706. var
  3707. def : tdef;
  3708. i : longint;
  3709. begin
  3710. { in case this is a formal class, first find the real definition }
  3711. if assigned(classh) and
  3712. (oo_is_formal in classh.objectoptions) then
  3713. classh:=find_real_class_definition(classh,true);
  3714. result:=false;
  3715. def:=nil;
  3716. while assigned(classh) do
  3717. begin
  3718. for i:=0 to classh.symtable.DefList.Count-1 do
  3719. begin
  3720. def:=tstoreddef(classh.symtable.DefList[i]);
  3721. { Find also all hidden private methods to
  3722. be compatible with delphi, see tw6203 (PFV) }
  3723. if (def.typ=procdef) and
  3724. (po_msgstr in tprocdef(def).procoptions) and
  3725. (tprocdef(def).messageinf.str^=s) then
  3726. begin
  3727. srsym:=tprocdef(def).procsym;
  3728. srsymtable:=classh.symtable;
  3729. addsymref(srsym);
  3730. result:=true;
  3731. exit;
  3732. end;
  3733. end;
  3734. classh:=classh.childof;
  3735. end;
  3736. srsym:=nil;
  3737. srsymtable:=nil;
  3738. end;
  3739. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;forward;
  3740. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3741. var
  3742. hashedid : THashedIDString;
  3743. parentclassh : tobjectdef;
  3744. begin
  3745. result:=false;
  3746. if not is_objectpascal_helper(classh) then
  3747. Internalerror(2011030101);
  3748. hashedid.id:=s;
  3749. { in a helper things are a bit more complex:
  3750. 1. search the symbol in the helper (if not "inherited")
  3751. 2. search the symbol in the extended type
  3752. 3. search the symbol in the parent helpers
  3753. 4. only classes: search the symbol in the parents of the extended type
  3754. }
  3755. if not (ssf_has_inherited in flags) then
  3756. begin
  3757. { search in the helper itself }
  3758. srsymtable:=classh.symtable;
  3759. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3760. if assigned(srsym) and
  3761. is_visible_for_object(srsym,contextclassh) then
  3762. begin
  3763. if not (ssf_no_addsymref in flags) then
  3764. addsymref(srsym);
  3765. result:=true;
  3766. exit;
  3767. end;
  3768. end;
  3769. { now search in the extended type itself }
  3770. { Note: the extendeddef might be Nil if we are currently parsing the
  3771. extended type itself and the identifier was not found }
  3772. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3773. begin
  3774. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3775. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3776. if assigned(srsym) and
  3777. is_visible_for_object(srsym,contextclassh) then
  3778. begin
  3779. if not (ssf_no_addsymref in flags) then
  3780. addsymref(srsym);
  3781. result:=true;
  3782. exit;
  3783. end;
  3784. end;
  3785. { now search in the parent helpers }
  3786. parentclassh:=classh.childof;
  3787. while assigned(parentclassh) do
  3788. begin
  3789. srsymtable:=parentclassh.symtable;
  3790. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3791. if assigned(srsym) and
  3792. is_visible_for_object(srsym,contextclassh) then
  3793. begin
  3794. if not (ssf_no_addsymref in flags) then
  3795. addsymref(srsym);
  3796. result:=true;
  3797. exit;
  3798. end;
  3799. parentclassh:=parentclassh.childof;
  3800. end;
  3801. { now search in the parents of the extended class (with helpers!) }
  3802. if is_class(classh.extendeddef) then
  3803. begin
  3804. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3805. { addsymref is already called by searchsym_in_class }
  3806. if result then
  3807. exit;
  3808. end;
  3809. { now search all helpers using the extendeddef as the starting point }
  3810. if (m_multi_helpers in current_settings.modeswitches) and
  3811. (
  3812. (current_structdef<>classh) or
  3813. assigned(classh.extendeddef)
  3814. ) then
  3815. begin
  3816. { this is only allowed if classh is currently parsed }
  3817. if not assigned(classh.extendeddef) then
  3818. internalerror(2019110101);
  3819. result:=search_best_objectpascal_helper(s,classh.extendeddef,contextclassh,srsym,srsymtable);
  3820. end;
  3821. end;
  3822. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3823. var
  3824. sym : Tprocsym;
  3825. hashedid : THashedIDString;
  3826. curreq,
  3827. besteq : tequaltype;
  3828. currpd,
  3829. bestpd : tprocdef;
  3830. stackitem : psymtablestackitem;
  3831. shortstringcount : longint;
  3832. isexplicit,
  3833. checkshortstring : boolean;
  3834. begin
  3835. hashedid.id:=overloaded_names[assignment_type];
  3836. besteq:=te_incompatible;
  3837. bestpd:=nil;
  3838. stackitem:=symtablestack.stack;
  3839. { special handling for assignments to shortstrings with a specific length:
  3840. - if we get an operator to ShortString we use that
  3841. - if we get only a single String[x] operator we use that
  3842. - otherwise it's a nogo }
  3843. isexplicit:=assignment_type=_OP_EXPLICIT;
  3844. shortstringcount:=0;
  3845. checkshortstring:=not isexplicit and is_shortstring(to_def) and (tstringdef(to_def).len<>255);
  3846. while assigned(stackitem) do
  3847. begin
  3848. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3849. if sym<>nil then
  3850. begin
  3851. if sym.typ<>procsym then
  3852. internalerror(200402031);
  3853. { if the source type is an alias then this is only the second choice,
  3854. if you mess with this code, check tw4093 }
  3855. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq,isexplicit);
  3856. { we found a ShortString overload, use that and be done }
  3857. if checkshortstring and
  3858. assigned(currpd) and
  3859. is_shortstring(currpd.returndef) and
  3860. (tstringdef(currpd.returndef).len=255) then
  3861. begin
  3862. besteq:=curreq;
  3863. bestpd:=currpd;
  3864. break;
  3865. end;
  3866. { independently of the operator being better count if we encountered
  3867. multpile String[x] operators }
  3868. if checkshortstring and assigned(currpd) and is_shortstring(currpd.returndef) then
  3869. inc(shortstringcount);
  3870. if curreq>besteq then
  3871. begin
  3872. besteq:=curreq;
  3873. bestpd:=currpd;
  3874. { don't stop searching if we have a String[x] operator cause
  3875. we might find a ShortString one or multiple ones (which
  3876. leads to no operator use) }
  3877. if (besteq=te_exact) and not checkshortstring then
  3878. break;
  3879. end;
  3880. end;
  3881. stackitem:=stackitem^.next;
  3882. end;
  3883. if checkshortstring and (shortstringcount>1) then
  3884. bestpd:=nil;
  3885. result:=bestpd;
  3886. end;
  3887. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3888. begin
  3889. { search record/object symtable first for a suitable operator }
  3890. if from_def.typ in [recorddef,objectdef] then
  3891. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3892. if to_def.typ in [recorddef,objectdef] then
  3893. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3894. { if type conversion is explicit then search first for explicit
  3895. operator overload and if not found then use implicit operator }
  3896. if explicit then
  3897. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3898. else
  3899. result:=nil;
  3900. if result=nil then
  3901. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3902. { restore symtable stack }
  3903. if to_def.typ in [recorddef,objectdef] then
  3904. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3905. if from_def.typ in [recorddef,objectdef] then
  3906. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3907. end;
  3908. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3909. var
  3910. sym : Tprocsym;
  3911. hashedid : THashedIDString;
  3912. curreq,
  3913. besteq : tequaltype;
  3914. currpd,
  3915. bestpd : tprocdef;
  3916. stackitem : psymtablestackitem;
  3917. begin
  3918. hashedid.id:='enumerator';
  3919. besteq:=te_incompatible;
  3920. bestpd:=nil;
  3921. stackitem:=symtablestack.stack;
  3922. while assigned(stackitem) do
  3923. begin
  3924. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3925. if sym<>nil then
  3926. begin
  3927. if sym.typ<>procsym then
  3928. internalerror(200910241);
  3929. { if the source type is an alias then this is only the second choice,
  3930. if you mess with this code, check tw4093 }
  3931. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3932. if curreq>besteq then
  3933. begin
  3934. besteq:=curreq;
  3935. bestpd:=currpd;
  3936. if (besteq=te_exact) then
  3937. break;
  3938. end;
  3939. end;
  3940. stackitem:=stackitem^.next;
  3941. end;
  3942. result:=bestpd;
  3943. end;
  3944. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  3945. var
  3946. sym : Tprocsym;
  3947. hashedid : THashedIDString;
  3948. optoken: ttoken;
  3949. begin
  3950. optoken := managementoperator2tok[mop];
  3951. if (optoken<first_managment_operator) or
  3952. (optoken>last_managment_operator) then
  3953. internalerror(201602280);
  3954. hashedid.id:=overloaded_names[optoken];
  3955. if not (pd.typ in [recorddef]) then
  3956. internalerror(201602281);
  3957. sym:=Tprocsym(tabstractrecorddef(pd).symtable.FindWithHash(hashedid));
  3958. if sym<>nil then
  3959. begin
  3960. if sym.typ<>procsym then
  3961. internalerror(201602282);
  3962. result:=sym.find_procdef_bytype(potype_operator);
  3963. end
  3964. else
  3965. result:=nil;
  3966. end;
  3967. function search_system_type(const s: TIDString): ttypesym;
  3968. var
  3969. sym : tsym;
  3970. begin
  3971. check_systemunit_loaded;
  3972. sym:=tsym(systemunit.Find(s));
  3973. if not assigned(sym) or
  3974. (sym.typ<>typesym) then
  3975. message1(cg_f_unknown_system_type,s);
  3976. result:=ttypesym(sym);
  3977. end;
  3978. function try_search_system_type(const s: TIDString): ttypesym;
  3979. var
  3980. sym : tsym;
  3981. begin
  3982. check_systemunit_loaded;
  3983. sym:=tsym(systemunit.Find(s));
  3984. if not assigned(sym) then
  3985. result:=nil
  3986. else
  3987. begin
  3988. if sym.typ<>typesym then
  3989. message1(cg_f_unknown_system_type,s);
  3990. result:=ttypesym(sym);
  3991. end;
  3992. end;
  3993. function try_search_current_module_type(const s: TIDString): ttypesym;
  3994. var
  3995. found: boolean;
  3996. srsymtable: tsymtable;
  3997. srsym: tsym;
  3998. begin
  3999. if s[1]='$' then
  4000. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  4001. else
  4002. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  4003. if found then
  4004. begin
  4005. if (srsym.typ<>typesym) then
  4006. internalerror(2014091207);
  4007. result:=ttypesym(srsym);
  4008. end
  4009. else
  4010. result:=nil;
  4011. end;
  4012. function search_system_proc(const s: TIDString): tprocdef;
  4013. var
  4014. srsym: tsym;
  4015. begin
  4016. check_systemunit_loaded;
  4017. srsym:=tsym(systemunit.find(s));
  4018. if not assigned(srsym) and
  4019. (cs_compilesystem in current_settings.moduleswitches) then
  4020. srsym:=tsym(systemunit.Find(upper(s)));
  4021. if not assigned(srsym) or
  4022. (srsym.typ<>procsym) then
  4023. message1(cg_f_unknown_compilerproc,s);
  4024. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  4025. end;
  4026. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  4027. var
  4028. srsymtable: tsymtable;
  4029. sym: tsym;
  4030. begin
  4031. sym:=nil;
  4032. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  4033. (sym.typ=typesym) then
  4034. begin
  4035. result:=ttypesym(sym);
  4036. exit;
  4037. end
  4038. else
  4039. begin
  4040. if throwerror then
  4041. message2(cg_f_unknown_type_in_unit,typename,unitname);
  4042. result:=nil;
  4043. end;
  4044. end;
  4045. function search_sym_in_helperdef(const s: string;classh : tobjectdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable): boolean;
  4046. var
  4047. hashedid : THashedIDString;
  4048. pdef : tprocdef;
  4049. i : integer;
  4050. begin
  4051. hashedid.id:=s;
  4052. result:=false;
  4053. repeat
  4054. srsymtable:=classh.symtable;
  4055. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  4056. if srsym<>nil then
  4057. begin
  4058. case srsym.typ of
  4059. procsym:
  4060. begin
  4061. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  4062. begin
  4063. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  4064. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  4065. continue;
  4066. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  4067. srsymtable:=srsym.owner;
  4068. result:=true;
  4069. exit;
  4070. end;
  4071. if (sp_generic_dummy in tprocsym(srsym).symoptions) and
  4072. (tprocsym(srsym).procdeflist.count=0) and
  4073. is_visible_for_object(srsym.owner,srsym.visibility,contextclassh) then
  4074. begin
  4075. srsymtable:=srsym.owner;
  4076. result:=true;
  4077. exit;
  4078. end;
  4079. end;
  4080. typesym,
  4081. fieldvarsym,
  4082. constsym,
  4083. enumsym,
  4084. undefinedsym,
  4085. propertysym:
  4086. begin
  4087. result:=true;
  4088. exit;
  4089. end;
  4090. else
  4091. internalerror(2014041101);
  4092. end;
  4093. end;
  4094. { try the helper parent if available }
  4095. classh:=classh.childof;
  4096. until classh=nil;
  4097. end;
  4098. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  4099. var
  4100. s : TSymStr;
  4101. st : tsymtable;
  4102. begin
  4103. result:=nil;
  4104. { when there are no helpers active currently then we don't need to do
  4105. anything }
  4106. if current_module.extendeddefs.count=0 then
  4107. exit;
  4108. if (df_genconstraint in pd.defoptions) then
  4109. begin
  4110. { if we have a constraint for a class type or a single interface we
  4111. use that to resolve helpers at declaration time of the generic,
  4112. otherwise there can't be any helpers as the type isn't known yet }
  4113. if pd.typ=objectdef then
  4114. pd:=tobjectdef(pd).getparentdef
  4115. else
  4116. exit;
  4117. end;
  4118. { no helpers for anonymous types }
  4119. if ((pd.typ in [recorddef,objectdef]) and
  4120. (
  4121. not assigned(tabstractrecorddef(pd).objrealname) or
  4122. (tabstractrecorddef(pd).objrealname^='')
  4123. )
  4124. ) or
  4125. not assigned(pd.typesym) then
  4126. exit;
  4127. { if pd is defined inside a procedure we must not use make_mangledname
  4128. (as a helper may not be defined in a procedure this is no problem...)}
  4129. st:=pd.owner;
  4130. while st.symtabletype in [objectsymtable,recordsymtable] do
  4131. st:=st.defowner.owner;
  4132. if st.symtabletype=localsymtable then
  4133. exit;
  4134. { the mangled name is used as the key for tmodule.extendeddefs }
  4135. s:=generate_objectpascal_helper_key(pd);
  4136. result:=TFPObjectList(current_module.extendeddefs.Find(s));
  4137. end;
  4138. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;
  4139. var
  4140. list : TFPObjectList;
  4141. i : integer;
  4142. odef : tobjectdef;
  4143. begin
  4144. result:=false;
  4145. list:=get_objectpascal_helpers(pd);
  4146. if assigned(list) and (list.count>0) then
  4147. begin
  4148. i:=list.count-1;
  4149. repeat
  4150. odef:=tobjectdef(list[i]);
  4151. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  4152. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  4153. if result then
  4154. result:=search_sym_in_helperdef(name,odef,contextclassh,srsym,srsymtable);
  4155. dec(i);
  4156. until result or (i<0);
  4157. end;
  4158. end;
  4159. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  4160. var
  4161. list : TFPObjectList;
  4162. i : integer;
  4163. begin
  4164. result:=false;
  4165. odef:=nil;
  4166. list:=get_objectpascal_helpers(pd);
  4167. if assigned(list) and (list.count>0) then
  4168. begin
  4169. i:=list.count-1;
  4170. repeat
  4171. odef:=tobjectdef(list[list.count-1]);
  4172. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  4173. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  4174. dec(i);
  4175. until result or (i<0);
  4176. if not result then
  4177. { just to be sure that noone uses odef }
  4178. odef:=nil;
  4179. end;
  4180. end;
  4181. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4182. var
  4183. classh : tobjectdef;
  4184. begin
  4185. result:=false;
  4186. { if there is no class helper for the class then there is no need to
  4187. search further }
  4188. if m_multi_helpers in current_settings.modeswitches then
  4189. result:=search_best_objectpascal_helper(s,pd,contextclassh,srsym,srsymtable)
  4190. else
  4191. begin
  4192. if search_last_objectpascal_helper(pd,contextclassh,classh) and
  4193. search_sym_in_helperdef(s,classh,contextclassh,srsym,srsymtable) then
  4194. result:=true;
  4195. end;
  4196. if result then
  4197. begin
  4198. { we need to know if a procedure references symbols
  4199. in the static symtable, because then it can't be
  4200. inlined from outside this unit }
  4201. if (srsym.typ=procsym) and
  4202. assigned(current_procinfo) and
  4203. (srsym.owner.symtabletype=staticsymtable) then
  4204. include(current_procinfo.flags,pi_uses_static_symtable);
  4205. addsymref(srsym);
  4206. end
  4207. else
  4208. begin
  4209. srsym:=nil;
  4210. srsymtable:=nil;
  4211. end;
  4212. end;
  4213. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4214. var
  4215. searchst : tsymtable;
  4216. searchsym : tsym;
  4217. hashedid : THashedIDString;
  4218. stackitem : psymtablestackitem;
  4219. i : longint;
  4220. founddefowner,
  4221. defowner : tobjectdef;
  4222. begin
  4223. hashedid.id:=class_helper_prefix+s;
  4224. stackitem:=symtablestack.stack;
  4225. result:=false;
  4226. srsym:=nil;
  4227. srsymtable:=nil;
  4228. founddefowner:=nil;
  4229. while assigned(stackitem) do
  4230. begin
  4231. searchst:=stackitem^.symtable;
  4232. searchsym:=tsym(searchst.FindWithHash(hashedid));
  4233. if assigned(searchsym) then
  4234. begin
  4235. if not(searchst.symtabletype in [globalsymtable,staticsymtable]) or
  4236. not(searchsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  4237. (searchsym.typ<>procsym) then
  4238. internalerror(2009111505);
  4239. { check whether this procsym includes a helper for this particular class }
  4240. for i:=0 to tprocsym(searchsym).procdeflist.count-1 do
  4241. begin
  4242. { does pd inherit from (or is the same as) the class
  4243. that this method's category extended?
  4244. Warning: this list contains both category and objcclass methods
  4245. (for id.randommethod), so only check category methods here
  4246. }
  4247. defowner:=tobjectdef(tprocdef(tprocsym(searchsym).procdeflist[i]).owner.defowner);
  4248. if is_objccategory(defowner) and
  4249. def_is_related(pd,defowner.childof) then
  4250. begin
  4251. { we need to know if a procedure references symbols
  4252. in the static symtable, because then it can't be
  4253. inlined from outside this unit }
  4254. if assigned(current_procinfo) and
  4255. (searchsym.owner.symtabletype=staticsymtable) then
  4256. include(current_procinfo.flags,pi_uses_static_symtable);
  4257. { Stop looking if this is a category that extends the specified
  4258. class itself. There might be other categories that extend this,
  4259. but that doesn't matter. If it extens a parent, keep looking
  4260. in case we find the symbol in a category that extends this class
  4261. (or a closer parent).
  4262. }
  4263. if not result or
  4264. def_is_related(defowner.childof,founddefowner) then
  4265. begin
  4266. founddefowner:=defowner.childof;
  4267. srsym:=tprocdef(tprocsym(searchsym).procdeflist[i]).procsym;
  4268. srsymtable:=srsym.owner;
  4269. result:=true;
  4270. if pd=founddefowner then
  4271. begin
  4272. addsymref(srsym);
  4273. exit;
  4274. end;
  4275. end;
  4276. end;
  4277. end;
  4278. end;
  4279. stackitem:=stackitem^.next;
  4280. end;
  4281. if result then
  4282. begin
  4283. addsymref(srsym);
  4284. exit;
  4285. end;
  4286. end;
  4287. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4288. var
  4289. hashedid : THashedIDString;
  4290. stackitem : psymtablestackitem;
  4291. i : longint;
  4292. begin
  4293. hashedid.id:=class_helper_prefix+s;
  4294. stackitem:=symtablestack.stack;
  4295. while assigned(stackitem) do
  4296. begin
  4297. srsymtable:=stackitem^.symtable;
  4298. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  4299. if assigned(srsym) then
  4300. begin
  4301. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  4302. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  4303. (srsym.typ<>procsym) then
  4304. internalerror(2009112005);
  4305. { check whether this procsym includes a helper for this particular class }
  4306. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  4307. begin
  4308. { we need to know if a procedure references symbols
  4309. in the static symtable, because then it can't be
  4310. inlined from outside this unit }
  4311. if assigned(current_procinfo) and
  4312. (srsym.owner.symtabletype=staticsymtable) then
  4313. include(current_procinfo.flags,pi_uses_static_symtable);
  4314. { no need to keep looking. There might be other
  4315. methods with the same name, but that doesn't matter
  4316. as far as the basic procsym is concerned.
  4317. }
  4318. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  4319. { We need the symtable in which the classhelper-like sym
  4320. is located, not the objectdef. The reason is that the
  4321. callnode will climb the symtablestack until it encounters
  4322. this symtable to start looking for overloads (and it won't
  4323. find the objectsymtable in which this method sym is
  4324. located
  4325. srsymtable:=srsym.owner;
  4326. }
  4327. addsymref(srsym);
  4328. result:=true;
  4329. exit;
  4330. end;
  4331. end;
  4332. stackitem:=stackitem^.next;
  4333. end;
  4334. srsym:=nil;
  4335. srsymtable:=nil;
  4336. result:=false;
  4337. end;
  4338. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  4339. { searches n in symtable of pd and all anchestors }
  4340. var
  4341. srsymtable : tsymtable;
  4342. begin
  4343. { in case this is a formal class, first find the real definition }
  4344. if (oo_is_formal in pd.objectoptions) then
  4345. pd:=find_real_class_definition(tobjectdef(pd),true);
  4346. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  4347. exit;
  4348. result:=search_struct_member_no_helper(pd,s);
  4349. if assigned(result) then
  4350. exit;
  4351. { not found, now look for class helpers }
  4352. if is_objcclass(pd) then
  4353. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  4354. end;
  4355. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  4356. var
  4357. hashedid : THashedIDString;
  4358. srsym : tsym;
  4359. begin
  4360. hashedid.id:=s;
  4361. while assigned(pd) do
  4362. begin
  4363. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  4364. if assigned(srsym) then
  4365. begin
  4366. result:=srsym;
  4367. exit;
  4368. end;
  4369. if pd.typ=objectdef then
  4370. pd:=tobjectdef(pd).childof
  4371. else
  4372. pd:=nil;
  4373. end;
  4374. result:=nil;
  4375. end;
  4376. function search_macro(const s : string):tsym;
  4377. var
  4378. stackitem : psymtablestackitem;
  4379. hashedid : THashedIDString;
  4380. srsym : tsym;
  4381. begin
  4382. hashedid.id:=s;
  4383. { First search the localmacrosymtable before searching the
  4384. global macrosymtables from the units }
  4385. if assigned(current_module) then
  4386. begin
  4387. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  4388. if assigned(srsym) then
  4389. begin
  4390. result:= srsym;
  4391. exit;
  4392. end;
  4393. end;
  4394. stackitem:=macrosymtablestack.stack;
  4395. while assigned(stackitem) do
  4396. begin
  4397. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  4398. if assigned(srsym) then
  4399. begin
  4400. result:= srsym;
  4401. exit;
  4402. end;
  4403. stackitem:=stackitem^.next;
  4404. end;
  4405. result:= nil;
  4406. end;
  4407. function defined_macro(const s : string):boolean;
  4408. var
  4409. mac: tmacro;
  4410. begin
  4411. mac:=tmacro(search_macro(s));
  4412. if assigned(mac) then
  4413. begin
  4414. mac.is_used:=true;
  4415. defined_macro:=mac.defined;
  4416. end
  4417. else
  4418. defined_macro:=false;
  4419. end;
  4420. {****************************************************************************
  4421. Object Helpers
  4422. ****************************************************************************}
  4423. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  4424. { returns the default property of a class, searches also anchestors }
  4425. var
  4426. _defaultprop : tpropertysym;
  4427. helperpd : tobjectdef;
  4428. begin
  4429. _defaultprop:=nil;
  4430. { first search in helper's hierarchy }
  4431. if search_last_objectpascal_helper(pd,nil,helperpd) then
  4432. while assigned(helperpd) do
  4433. begin
  4434. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  4435. if assigned(_defaultprop) then
  4436. break;
  4437. helperpd:=helperpd.childof;
  4438. end;
  4439. if assigned(_defaultprop) then
  4440. begin
  4441. search_default_property:=_defaultprop;
  4442. exit;
  4443. end;
  4444. { now search in the type's hierarchy itself }
  4445. while assigned(pd) do
  4446. begin
  4447. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  4448. if assigned(_defaultprop) then
  4449. break;
  4450. if (pd.typ=objectdef) then
  4451. pd:=tobjectdef(pd).childof
  4452. else
  4453. break;
  4454. end;
  4455. search_default_property:=_defaultprop;
  4456. end;
  4457. {****************************************************************************
  4458. Macro Helpers
  4459. ****************************************************************************}
  4460. procedure def_system_macro(const name : string);
  4461. var
  4462. mac : tmacro;
  4463. s: string;
  4464. begin
  4465. if name = '' then
  4466. internalerror(2004121202);
  4467. s:= upper(name);
  4468. mac:=tmacro(search_macro(s));
  4469. if not assigned(mac) then
  4470. begin
  4471. mac:=tmacro.create(s);
  4472. if assigned(current_module) then
  4473. current_module.localmacrosymtable.insertsym(mac)
  4474. else
  4475. initialmacrosymtable.insertsym(mac);
  4476. end;
  4477. Message1(parser_c_macro_defined,mac.name);
  4478. mac.defined:=true;
  4479. end;
  4480. procedure set_system_macro(const name, value : string);
  4481. var
  4482. mac : tmacro;
  4483. s: string;
  4484. begin
  4485. if name = '' then
  4486. internalerror(2004121203);
  4487. s:= upper(name);
  4488. mac:=tmacro(search_macro(s));
  4489. if not assigned(mac) then
  4490. begin
  4491. mac:=tmacro.create(s);
  4492. if assigned(current_module) then
  4493. current_module.localmacrosymtable.insertsym(mac)
  4494. else
  4495. initialmacrosymtable.insertsym(mac);
  4496. end
  4497. else
  4498. begin
  4499. mac.is_compiler_var:=false;
  4500. if assigned(mac.buftext) then
  4501. freemem(mac.buftext,mac.buflen);
  4502. end;
  4503. Message2(parser_c_macro_set_to,mac.name,value);
  4504. mac.buflen:=length(value);
  4505. getmem(mac.buftext,mac.buflen);
  4506. move(value[1],mac.buftext^,mac.buflen);
  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. begin
  4529. mac.is_compiler_var:=true;
  4530. if assigned(mac.buftext) then
  4531. freemem(mac.buftext,mac.buflen);
  4532. end;
  4533. Message2(parser_c_macro_set_to,mac.name,value);
  4534. mac.buflen:=length(value);
  4535. getmem(mac.buftext,mac.buflen);
  4536. move(value[1],mac.buftext^,mac.buflen);
  4537. mac.defined:=true;
  4538. end;
  4539. procedure undef_system_macro(const name : string);
  4540. var
  4541. mac : tmacro;
  4542. s: string;
  4543. begin
  4544. if name = '' then
  4545. internalerror(2004121205);
  4546. s:= upper(name);
  4547. mac:=tmacro(search_macro(s));
  4548. if not assigned(mac) then
  4549. {If not found, then it's already undefined.}
  4550. else
  4551. begin
  4552. Message1(parser_c_macro_undefined,mac.name);
  4553. mac.defined:=false;
  4554. mac.is_compiler_var:=false;
  4555. { delete old definition }
  4556. if assigned(mac.buftext) then
  4557. begin
  4558. freemem(mac.buftext,mac.buflen);
  4559. mac.buftext:=nil;
  4560. end;
  4561. end;
  4562. end;
  4563. {$ifdef UNITALIASES}
  4564. {****************************************************************************
  4565. TUNIT_ALIAS
  4566. ****************************************************************************}
  4567. constructor tunit_alias.create(const n:string);
  4568. var
  4569. i : longint;
  4570. begin
  4571. i:=pos('=',n);
  4572. if i=0 then
  4573. fail;
  4574. inherited createname(Copy(n,1,i-1));
  4575. newname:=stringdup(Copy(n,i+1,255));
  4576. end;
  4577. destructor tunit_alias.destroy;
  4578. begin
  4579. stringdispose(newname);
  4580. inherited destroy;
  4581. end;
  4582. procedure addunitalias(const n:string);
  4583. begin
  4584. unitaliases^.insert(tunit_alias,init(Upper(n))));
  4585. end;
  4586. function getunitalias(const n:string):string;
  4587. var
  4588. p : punit_alias;
  4589. begin
  4590. p:=punit_alias(unitaliases^.Find(Upper(n)));
  4591. if assigned(p) then
  4592. getunitalias:=punit_alias(p).newname^
  4593. else
  4594. getunitalias:=n;
  4595. end;
  4596. {$endif UNITALIASES}
  4597. {****************************************************************************
  4598. Init/Done Symtable
  4599. ****************************************************************************}
  4600. procedure InitSymtable;
  4601. begin
  4602. { Reset symbolstack }
  4603. symtablestack:=nil;
  4604. systemunit:=nil;
  4605. { create error syms and def }
  4606. generrorsym:=terrorsym.create;
  4607. generrordef:=cerrordef.create;
  4608. { macros }
  4609. initialmacrosymtable:=tmacrosymtable.create(false);
  4610. macrosymtablestack:=TSymtablestack.create;
  4611. macrosymtablestack.push(initialmacrosymtable);
  4612. {$ifdef UNITALIASES}
  4613. { unit aliases }
  4614. unitaliases:=TFPHashObjectList.create;
  4615. {$endif}
  4616. { set some global vars to nil, might be important for the ide }
  4617. class_tobject:=nil;
  4618. class_tcustomattribute:=nil;
  4619. interface_iunknown:=nil;
  4620. interface_idispatch:=nil;
  4621. rec_tguid:=nil;
  4622. rec_jmp_buf:=nil;
  4623. rec_exceptaddr:=nil;
  4624. objc_metaclasstype:=nil;
  4625. objc_superclasstype:=nil;
  4626. objc_idtype:=nil;
  4627. objc_seltype:=nil;
  4628. objc_objecttype:=nil;
  4629. dupnr:=0;
  4630. end;
  4631. procedure DoneSymtable;
  4632. begin
  4633. generrorsym.owner:=nil;
  4634. generrorsym.free;
  4635. generrordef.owner:=nil;
  4636. generrordef.free;
  4637. initialmacrosymtable.free;
  4638. macrosymtablestack.free;
  4639. {$ifdef UNITALIASES}
  4640. unitaliases.free;
  4641. {$endif}
  4642. end;
  4643. end.