symtable.pas 167 KB

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