symtable.pas 169 KB

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