symtable.pas 186 KB

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