symtable.pas 167 KB

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