symtable.pas 183 KB

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