2
0

symtable.pas 183 KB

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