symtable.pas 177 KB

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