symtable.pas 187 KB

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