symtable.pas 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407
  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 (
  2483. assigned(current_module.globalsymtable) and
  2484. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2485. ) or (
  2486. assigned(current_module.localsymtable) and
  2487. (current_module.localsymtable.moduleid<>owner.moduleid)
  2488. ) then
  2489. { symbol is imported from another unit }
  2490. current_module.addimportedsym(sym);
  2491. end;
  2492. end;
  2493. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2494. begin
  2495. result:=nesteddef=ownerdef;
  2496. if not result and
  2497. { types declared locally in a record method are not defined in the
  2498. record itself }
  2499. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2500. assigned(nesteddef.owner.defowner) then
  2501. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2502. end;
  2503. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2504. begin
  2505. result:=assigned(childsym) and (childsym.owner=symtable);
  2506. if not result and assigned(childsym) and
  2507. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2508. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2509. end;
  2510. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2511. begin
  2512. result:=false;
  2513. if not assigned(def1) or not assigned(def2) then
  2514. exit;
  2515. { for both defs walk to the topmost generic }
  2516. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2517. def1:=tdef(def1.owner.defowner);
  2518. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2519. def2:=tdef(def2.owner.defowner);
  2520. result:=def1=def2;
  2521. end;
  2522. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2523. var
  2524. uname : string;
  2525. begin
  2526. { TODO : check regarding arrays and records declared as their type }
  2527. if not (def.typ in [recorddef,objectdef]) then
  2528. internalerror(2012051501);
  2529. uname:=upper(srsym.realname);
  2530. repeat
  2531. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2532. begin
  2533. result:=def;
  2534. exit;
  2535. end;
  2536. def:=tdef(def.owner.defowner);
  2537. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2538. result:=nil;
  2539. end;
  2540. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2541. begin
  2542. { TODO : check regarding arrays and records declared as their type }
  2543. if not (nesteddef.typ in [recorddef,objectdef]) then
  2544. internalerror(2012051601);
  2545. repeat
  2546. if tstoreddef(nesteddef).genericdef=genericdef then
  2547. begin
  2548. resultdef:=nesteddef;
  2549. result:=true;
  2550. exit;
  2551. end;
  2552. nesteddef:=tdef(nesteddef.owner.defowner);
  2553. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2554. resultdef:=nil;
  2555. result:=false;
  2556. end;
  2557. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2558. symvisibility: visibility of the symbol
  2559. contextobjdef: via which def the symbol is accessed, e.g.:
  2560. fieldname:=1 -> contextobjdef = current_structdef
  2561. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2562. }
  2563. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2564. var
  2565. symownerdef : tabstractrecorddef;
  2566. nonlocalst : tsymtable;
  2567. isspezproc : boolean;
  2568. begin
  2569. result:=false;
  2570. { Get objdectdef owner of the symtable for the is_related checks }
  2571. if not assigned(symst) or
  2572. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2573. internalerror(200810285);
  2574. symownerdef:=tabstractrecorddef(symst.defowner);
  2575. { specializations might belong to a localsymtable or parasymtable }
  2576. nonlocalst:=symownerdef.owner;
  2577. if tstoreddef(symst.defowner).is_specialization then
  2578. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2579. nonlocalst:=nonlocalst.defowner.owner;
  2580. isspezproc:=false;
  2581. if assigned(current_procinfo) then
  2582. begin
  2583. if current_procinfo.procdef.is_specialization and
  2584. assigned(current_procinfo.procdef.struct) then
  2585. isspezproc:=true;
  2586. end;
  2587. case symvisibility of
  2588. vis_private :
  2589. begin
  2590. { private symbols are allowed when we are in the same
  2591. module as they are defined }
  2592. result:=(
  2593. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2594. (nonlocalst.iscurrentunit)
  2595. ) or
  2596. ( // the case of specialize inside the generic declaration and nested types
  2597. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2598. (
  2599. assigned(current_structdef) and
  2600. (
  2601. (current_structdef=symownerdef) or
  2602. (current_structdef.owner.iscurrentunit)
  2603. )
  2604. ) or
  2605. (
  2606. not assigned(current_structdef) and
  2607. (symownerdef.owner.iscurrentunit)
  2608. ) or
  2609. { access from a generic method that belongs to the class
  2610. but that is specialized elsewere }
  2611. (
  2612. isspezproc and
  2613. (current_procinfo.procdef.struct=current_structdef)
  2614. )
  2615. );
  2616. end;
  2617. vis_strictprivate :
  2618. begin
  2619. result:=assigned(current_structdef) and
  2620. is_owned_by(current_structdef,symownerdef);
  2621. end;
  2622. vis_strictprotected :
  2623. begin
  2624. result:=(
  2625. { access from nested class }
  2626. assigned(current_structdef) and
  2627. is_owned_by(current_structdef,symownerdef)
  2628. ) or
  2629. (
  2630. { access from child class }
  2631. assigned(contextobjdef) and
  2632. assigned(current_structdef) and
  2633. def_is_related(contextobjdef,symownerdef) and
  2634. def_is_related(current_structdef,contextobjdef)
  2635. ) or
  2636. (
  2637. { helpers can access strict protected symbols }
  2638. is_objectpascal_helper(contextobjdef) and
  2639. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2640. ) or
  2641. (
  2642. { same as above, but from context of call node inside
  2643. helper method }
  2644. is_objectpascal_helper(current_structdef) and
  2645. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2646. );
  2647. end;
  2648. vis_protected :
  2649. begin
  2650. { protected symbols are visible in the module that defines them and
  2651. also visible to related objects. The related object must be defined
  2652. in the current module }
  2653. result:=(
  2654. (
  2655. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2656. (nonlocalst.iscurrentunit)
  2657. ) or
  2658. (
  2659. assigned(contextobjdef) and
  2660. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable]) and
  2661. (contextobjdef.owner.iscurrentunit) and
  2662. def_is_related(contextobjdef,symownerdef)
  2663. ) or
  2664. ( // the case of specialize inside the generic declaration and nested types
  2665. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2666. (
  2667. assigned(current_structdef) and
  2668. (
  2669. (current_structdef=symownerdef) or
  2670. (current_structdef.owner.iscurrentunit)
  2671. )
  2672. ) or
  2673. (
  2674. not assigned(current_structdef) and
  2675. (symownerdef.owner.iscurrentunit)
  2676. ) or
  2677. (
  2678. { helpers can access protected symbols }
  2679. is_objectpascal_helper(contextobjdef) and
  2680. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2681. )
  2682. ) or
  2683. { access from a generic method that belongs to the class
  2684. but that is specialized elsewere }
  2685. (
  2686. isspezproc and
  2687. (current_procinfo.procdef.struct=current_structdef)
  2688. )
  2689. );
  2690. end;
  2691. vis_public,
  2692. vis_published :
  2693. result:=true;
  2694. end;
  2695. end;
  2696. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2697. begin
  2698. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2699. end;
  2700. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2701. var
  2702. i : longint;
  2703. pd : tprocdef;
  2704. begin
  2705. if sym.typ=procsym then
  2706. begin
  2707. { A procsym is visible, when there is at least one of the procdefs visible }
  2708. result:=false;
  2709. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2710. begin
  2711. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2712. if (pd.owner=sym.owner) and
  2713. is_visible_for_object(pd,contextobjdef) then
  2714. begin
  2715. result:=true;
  2716. exit;
  2717. end;
  2718. end;
  2719. end
  2720. else
  2721. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2722. end;
  2723. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2724. begin
  2725. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2726. end;
  2727. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2728. begin
  2729. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2730. end;
  2731. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2732. var
  2733. hashedid: THashedIDString;
  2734. contextstructdef: tabstractrecorddef;
  2735. stackitem: psymtablestackitem;
  2736. begin
  2737. result:=false;
  2738. hashedid.id:=s;
  2739. stackitem:=symtablestack.stack;
  2740. while assigned(stackitem) do
  2741. begin
  2742. srsymtable:=stackitem^.symtable;
  2743. if (srsymtable.symtabletype=objectsymtable) then
  2744. begin
  2745. { TODO : implement the search for an option in classes as well }
  2746. if ssf_search_option in flags then
  2747. begin
  2748. result:=false;
  2749. exit;
  2750. end;
  2751. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  2752. begin
  2753. result:=true;
  2754. exit;
  2755. end;
  2756. end
  2757. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2758. (srsymtable.defowner.typ=undefineddef)) then
  2759. begin
  2760. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2761. { First check if it is a unit/namespace symbol.
  2762. They are visible only if they are from the current unit or
  2763. unit of generic of currently processed specialization. }
  2764. if assigned(srsym) and
  2765. (
  2766. not(srsym.typ in [unitsym,namespacesym]) or
  2767. srsymtable.iscurrentunit or
  2768. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2769. ) and
  2770. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  2771. begin
  2772. { use the class from withsymtable only when it is
  2773. defined in this unit }
  2774. if (srsymtable.symtabletype=withsymtable) and
  2775. assigned(srsymtable.defowner) and
  2776. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2777. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2778. (srsymtable.defowner.owner.iscurrentunit) then
  2779. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2780. else
  2781. contextstructdef:=current_structdef;
  2782. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2783. is_visible_for_object(srsym,contextstructdef) then
  2784. begin
  2785. { we need to know if a procedure references symbols
  2786. in the static symtable, because then it can't be
  2787. inlined from outside this unit }
  2788. if assigned(current_procinfo) and
  2789. (srsym.owner.symtabletype=staticsymtable) then
  2790. include(current_procinfo.flags,pi_uses_static_symtable);
  2791. if not (ssf_no_addsymref in flags) then
  2792. addsymref(srsym);
  2793. result:=true;
  2794. exit;
  2795. end;
  2796. end;
  2797. end;
  2798. stackitem:=stackitem^.next;
  2799. end;
  2800. srsym:=nil;
  2801. srsymtable:=nil;
  2802. end;
  2803. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2804. srsymtable:TSymtable;option:tsymoption):boolean;
  2805. begin
  2806. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  2807. end;
  2808. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2809. var
  2810. hashedid : THashedIDString;
  2811. stackitem : psymtablestackitem;
  2812. classh : tobjectdef;
  2813. begin
  2814. result:=false;
  2815. hashedid.id:=s;
  2816. stackitem:=symtablestack.stack;
  2817. while assigned(stackitem) do
  2818. begin
  2819. {
  2820. It is not possible to have type symbols in:
  2821. parameters
  2822. Exception are classes, objects, records, generic definitions and specializations
  2823. that have the parameterized types inserted in the symtable.
  2824. }
  2825. srsymtable:=stackitem^.symtable;
  2826. if (srsymtable.symtabletype=ObjectSymtable) then
  2827. begin
  2828. classh:=tobjectdef(srsymtable.defowner);
  2829. while assigned(classh) do
  2830. begin
  2831. srsymtable:=classh.symtable;
  2832. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2833. if assigned(srsym) and
  2834. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2835. is_visible_for_object(srsym,current_structdef) then
  2836. begin
  2837. addsymref(srsym);
  2838. result:=true;
  2839. exit;
  2840. end;
  2841. classh:=classh.childof;
  2842. end;
  2843. end
  2844. else
  2845. begin
  2846. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2847. if assigned(srsym) and
  2848. (
  2849. not(srsym.typ in [unitsym,namespacesym]) or
  2850. srsymtable.iscurrentunit or
  2851. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2852. ) and
  2853. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2854. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2855. begin
  2856. { we need to know if a procedure references symbols
  2857. in the static symtable, because then it can't be
  2858. inlined from outside this unit }
  2859. if assigned(current_procinfo) and
  2860. (srsym.owner.symtabletype=staticsymtable) then
  2861. include(current_procinfo.flags,pi_uses_static_symtable);
  2862. addsymref(srsym);
  2863. result:=true;
  2864. exit;
  2865. end;
  2866. end;
  2867. stackitem:=stackitem^.next;
  2868. end;
  2869. result:=false;
  2870. srsym:=nil;
  2871. srsymtable:=nil;
  2872. end;
  2873. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2874. var
  2875. pmod : tmodule;
  2876. begin
  2877. pmod:=tmodule(pm);
  2878. result:=false;
  2879. if assigned(pmod.globalsymtable) then
  2880. begin
  2881. srsym:=tsym(pmod.globalsymtable.Find(s));
  2882. if assigned(srsym) then
  2883. begin
  2884. srsymtable:=pmod.globalsymtable;
  2885. addsymref(srsym);
  2886. result:=true;
  2887. exit;
  2888. end;
  2889. end;
  2890. { If the module is the current unit we also need
  2891. to search the local symtable }
  2892. if (pmod=current_module) and
  2893. assigned(pmod.localsymtable) then
  2894. begin
  2895. srsym:=tsym(pmod.localsymtable.Find(s));
  2896. if assigned(srsym) then
  2897. begin
  2898. srsymtable:=pmod.localsymtable;
  2899. addsymref(srsym);
  2900. result:=true;
  2901. exit;
  2902. end;
  2903. end;
  2904. srsym:=nil;
  2905. srsymtable:=nil;
  2906. end;
  2907. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2908. var
  2909. stackitem : psymtablestackitem;
  2910. begin
  2911. result:=false;
  2912. stackitem:=symtablestack.stack;
  2913. while assigned(stackitem) do
  2914. begin
  2915. srsymtable:=stackitem^.symtable;
  2916. if (srsymtable.symtabletype=globalsymtable) and
  2917. (srsymtable.name^=unitname) then
  2918. begin
  2919. srsym:=tsym(srsymtable.find(symname));
  2920. if not assigned(srsym) then
  2921. break;
  2922. result:=true;
  2923. exit;
  2924. end;
  2925. stackitem:=stackitem^.next;
  2926. end;
  2927. { If the module is the current unit we also need
  2928. to search the local symtable }
  2929. if assigned(current_module.localsymtable) and
  2930. (current_module.localsymtable.name^=unitname) then
  2931. begin
  2932. srsymtable:=current_module.localsymtable;
  2933. srsym:=tsym(srsymtable.find(symname));
  2934. if assigned(srsym) then
  2935. begin
  2936. result:=true;
  2937. exit;
  2938. end;
  2939. end;
  2940. end;
  2941. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2942. begin
  2943. result:=pd;
  2944. if pd.typ<>objectdef then
  2945. exit;
  2946. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2947. end;
  2948. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2949. var
  2950. hashedid : THashedIDString;
  2951. stackitem : psymtablestackitem;
  2952. srsymtable : tsymtable;
  2953. srsym : tsym;
  2954. formalname,
  2955. foundname : shortstring;
  2956. formalnameptr,
  2957. foundnameptr: pshortstring;
  2958. begin
  2959. { not a formal definition -> return it }
  2960. if not(oo_is_formal in pd.objectoptions) then
  2961. begin
  2962. result:=pd;
  2963. exit;
  2964. end;
  2965. hashedid.id:=pd.typesym.name;
  2966. stackitem:=symtablestack.stack;
  2967. while assigned(stackitem) do
  2968. begin
  2969. srsymtable:=stackitem^.symtable;
  2970. { ObjC classes can't appear in generics or as nested class
  2971. definitions. Java classes can. }
  2972. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2973. (is_java_class_or_interface(pd) and
  2974. (srsymtable.symtabletype=ObjectSymtable)) then
  2975. begin
  2976. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2977. if assigned(srsym) and
  2978. (srsym.typ=typesym) and
  2979. (ttypesym(srsym).typedef.typ=objectdef) and
  2980. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2981. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2982. begin
  2983. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2984. begin
  2985. { the external name for the formal and the real
  2986. definition must match }
  2987. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2988. assigned(pd.import_lib) then
  2989. begin
  2990. if assigned(pd.import_lib) then
  2991. formalname:=pd.import_lib^+'.'
  2992. else
  2993. formalname:='';
  2994. formalname:=formalname+pd.objextname^;
  2995. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2996. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2997. else
  2998. foundname:='';
  2999. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3000. formalnameptr:=@formalname;
  3001. foundnameptr:=@foundname;
  3002. end
  3003. else
  3004. begin
  3005. formalnameptr:=pd.objextname;
  3006. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3007. end;
  3008. if foundnameptr^<>formalnameptr^ then
  3009. begin
  3010. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3011. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3012. end;
  3013. end;
  3014. result:=tobjectdef(ttypesym(srsym).typedef);
  3015. if assigned(current_procinfo) and
  3016. (srsym.owner.symtabletype=staticsymtable) then
  3017. include(current_procinfo.flags,pi_uses_static_symtable);
  3018. addsymref(srsym);
  3019. exit;
  3020. end;
  3021. end;
  3022. stackitem:=stackitem^.next;
  3023. end;
  3024. { nothing found: optionally give an error and return the original
  3025. (empty) one }
  3026. if erroronfailure then
  3027. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3028. result:=pd;
  3029. end;
  3030. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3031. var
  3032. hashedid : THashedIDString;
  3033. orgclass : tobjectdef;
  3034. i : longint;
  3035. hlpsrsym : tsym;
  3036. hlpsrsymtable : tsymtable;
  3037. begin
  3038. orgclass:=classh;
  3039. { in case this is a formal class, first find the real definition }
  3040. if assigned(classh) then
  3041. begin
  3042. if (oo_is_formal in classh.objectoptions) then
  3043. classh:=find_real_class_definition(classh,true);
  3044. { The contextclassh is used for visibility. The classh must be equal to
  3045. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3046. parent or a class helper. }
  3047. if not (def_is_related(contextclassh,classh) or
  3048. (is_classhelper(contextclassh) and
  3049. assigned(tobjectdef(contextclassh).extendeddef) and
  3050. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3051. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3052. internalerror(200811161);
  3053. end;
  3054. result:=false;
  3055. hashedid.id:=s;
  3056. { an Objective-C protocol or Java interface can inherit from multiple
  3057. other protocols/interfaces -> use ImplementedInterfaces instead }
  3058. if is_objcprotocol(classh) or
  3059. is_javainterface(classh) then
  3060. begin
  3061. srsymtable:=classh.symtable;
  3062. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3063. if assigned(srsym) and
  3064. is_visible_for_object(srsym,contextclassh) then
  3065. begin
  3066. if not (ssf_no_addsymref in flags) then
  3067. addsymref(srsym);
  3068. result:=true;
  3069. exit;
  3070. end;
  3071. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3072. begin
  3073. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3074. begin
  3075. result:=true;
  3076. exit;
  3077. end;
  3078. end;
  3079. end
  3080. else
  3081. if is_objectpascal_helper(classh) then
  3082. begin
  3083. { helpers have their own obscure search logic... }
  3084. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3085. if result then
  3086. exit;
  3087. end
  3088. else
  3089. begin
  3090. hlpsrsym:=nil;
  3091. hlpsrsymtable:=nil;
  3092. while assigned(classh) do
  3093. begin
  3094. { search for a class helper method first if this is an Object
  3095. Pascal class and we haven't yet found a helper symbol }
  3096. if is_class(classh) and
  3097. (ssf_search_helper in flags) and
  3098. not assigned(hlpsrsym) then
  3099. begin
  3100. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3101. if result then
  3102. { if the procsym is overloaded we need to use the
  3103. "original" symbol; the helper symbol will be found when
  3104. searching for overloads }
  3105. if (srsym.typ<>procsym) or
  3106. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  3107. exit
  3108. else
  3109. begin
  3110. { remember the found symbol if the class hierarchy
  3111. should not contain the a method with that name }
  3112. hlpsrsym:=srsym;
  3113. hlpsrsymtable:=srsymtable;
  3114. end;
  3115. end;
  3116. srsymtable:=classh.symtable;
  3117. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3118. if assigned(srsym) and
  3119. is_visible_for_object(srsym,contextclassh) then
  3120. begin
  3121. if not (ssf_no_addsymref in flags) then
  3122. addsymref(srsym);
  3123. result:=true;
  3124. exit;
  3125. end;
  3126. classh:=classh.childof;
  3127. end;
  3128. { did we find a helper symbol, but no symbol with the same name in
  3129. the extended object's hierarchy? }
  3130. if assigned(hlpsrsym) then
  3131. begin
  3132. srsym:=hlpsrsym;
  3133. srsymtable:=hlpsrsymtable;
  3134. result:=true;
  3135. exit;
  3136. end;
  3137. end;
  3138. if is_objcclass(orgclass) then
  3139. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3140. else
  3141. begin
  3142. srsym:=nil;
  3143. srsymtable:=nil;
  3144. end;
  3145. end;
  3146. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3147. var
  3148. hashedid : THashedIDString;
  3149. hlpsrsym : tsym;
  3150. hlpsrsymtable : tsymtable;
  3151. begin
  3152. result:=false;
  3153. hlpsrsym:=nil;
  3154. hlpsrsymtable:=nil;
  3155. hashedid.id:=s;
  3156. { search for a record helper method first }
  3157. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3158. if result then
  3159. { if the procsym is overloaded we need to use the
  3160. "original" symbol; the helper symbol will be found when
  3161. searching for overloads }
  3162. if (srsym.typ<>procsym) or
  3163. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  3164. exit
  3165. else
  3166. begin
  3167. { remember the found symbol if we should not find a symbol with
  3168. the same name in the extended record }
  3169. hlpsrsym:=srsym;
  3170. hlpsrsymtable:=srsymtable;
  3171. end;
  3172. srsymtable:=recordh.symtable;
  3173. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3174. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3175. begin
  3176. addsymref(srsym);
  3177. result:=true;
  3178. exit;
  3179. end;
  3180. srsym:=hlpsrsym;
  3181. srsymtable:=hlpsrsymtable;
  3182. result:=assigned(srsym);
  3183. end;
  3184. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3185. var
  3186. def : tdef;
  3187. i : longint;
  3188. begin
  3189. { in case this is a formal class, first find the real definition }
  3190. if assigned(classh) and
  3191. (oo_is_formal in classh.objectoptions) then
  3192. classh:=find_real_class_definition(classh,true);
  3193. result:=false;
  3194. def:=nil;
  3195. while assigned(classh) do
  3196. begin
  3197. for i:=0 to classh.symtable.DefList.Count-1 do
  3198. begin
  3199. def:=tstoreddef(classh.symtable.DefList[i]);
  3200. { Find also all hidden private methods to
  3201. be compatible with delphi, see tw6203 (PFV) }
  3202. if (def.typ=procdef) and
  3203. (po_msgint in tprocdef(def).procoptions) and
  3204. (tprocdef(def).messageinf.i=msgid) then
  3205. begin
  3206. srdef:=def;
  3207. srsym:=tprocdef(def).procsym;
  3208. srsymtable:=classh.symtable;
  3209. addsymref(srsym);
  3210. result:=true;
  3211. exit;
  3212. end;
  3213. end;
  3214. classh:=classh.childof;
  3215. end;
  3216. srdef:=nil;
  3217. srsym:=nil;
  3218. srsymtable:=nil;
  3219. end;
  3220. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3221. var
  3222. def : tdef;
  3223. i : longint;
  3224. begin
  3225. { in case this is a formal class, first find the real definition }
  3226. if assigned(classh) and
  3227. (oo_is_formal in classh.objectoptions) then
  3228. classh:=find_real_class_definition(classh,true);
  3229. result:=false;
  3230. def:=nil;
  3231. while assigned(classh) do
  3232. begin
  3233. for i:=0 to classh.symtable.DefList.Count-1 do
  3234. begin
  3235. def:=tstoreddef(classh.symtable.DefList[i]);
  3236. { Find also all hidden private methods to
  3237. be compatible with delphi, see tw6203 (PFV) }
  3238. if (def.typ=procdef) and
  3239. (po_msgstr in tprocdef(def).procoptions) and
  3240. (tprocdef(def).messageinf.str^=s) then
  3241. begin
  3242. srsym:=tprocdef(def).procsym;
  3243. srsymtable:=classh.symtable;
  3244. addsymref(srsym);
  3245. result:=true;
  3246. exit;
  3247. end;
  3248. end;
  3249. classh:=classh.childof;
  3250. end;
  3251. srsym:=nil;
  3252. srsymtable:=nil;
  3253. end;
  3254. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3255. var
  3256. hashedid : THashedIDString;
  3257. parentclassh : tobjectdef;
  3258. begin
  3259. result:=false;
  3260. if not is_objectpascal_helper(classh) then
  3261. Internalerror(2011030101);
  3262. hashedid.id:=s;
  3263. { in a helper things are a bit more complex:
  3264. 1. search the symbol in the helper (if not "inherited")
  3265. 2. search the symbol in the extended type
  3266. 3. search the symbol in the parent helpers
  3267. 4. only classes: search the symbol in the parents of the extended type
  3268. }
  3269. if not (ssf_has_inherited in flags) then
  3270. begin
  3271. { search in the helper itself }
  3272. srsymtable:=classh.symtable;
  3273. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3274. if assigned(srsym) and
  3275. is_visible_for_object(srsym,contextclassh) then
  3276. begin
  3277. if not (ssf_no_addsymref in flags) then
  3278. addsymref(srsym);
  3279. result:=true;
  3280. exit;
  3281. end;
  3282. end;
  3283. { now search in the extended type itself }
  3284. { Note: the extendeddef might be Nil if we are currently parsing the
  3285. extended type itself and the identifier was not found }
  3286. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3287. begin
  3288. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3289. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3290. if assigned(srsym) and
  3291. is_visible_for_object(srsym,contextclassh) then
  3292. begin
  3293. if not (ssf_no_addsymref in flags) then
  3294. addsymref(srsym);
  3295. result:=true;
  3296. exit;
  3297. end;
  3298. end;
  3299. { now search in the parent helpers }
  3300. parentclassh:=classh.childof;
  3301. while assigned(parentclassh) do
  3302. begin
  3303. srsymtable:=parentclassh.symtable;
  3304. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3305. if assigned(srsym) and
  3306. is_visible_for_object(srsym,contextclassh) then
  3307. begin
  3308. if not (ssf_no_addsymref in flags) then
  3309. addsymref(srsym);
  3310. result:=true;
  3311. exit;
  3312. end;
  3313. parentclassh:=parentclassh.childof;
  3314. end;
  3315. if is_class(classh.extendeddef) then
  3316. { now search in the parents of the extended class (with helpers!) }
  3317. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3318. { addsymref is already called by searchsym_in_class }
  3319. end;
  3320. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3321. var
  3322. sym : Tprocsym;
  3323. hashedid : THashedIDString;
  3324. curreq,
  3325. besteq : tequaltype;
  3326. currpd,
  3327. bestpd : tprocdef;
  3328. stackitem : psymtablestackitem;
  3329. begin
  3330. hashedid.id:=overloaded_names[assignment_type];
  3331. besteq:=te_incompatible;
  3332. bestpd:=nil;
  3333. stackitem:=symtablestack.stack;
  3334. while assigned(stackitem) do
  3335. begin
  3336. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3337. if sym<>nil then
  3338. begin
  3339. if sym.typ<>procsym then
  3340. internalerror(200402031);
  3341. { if the source type is an alias then this is only the second choice,
  3342. if you mess with this code, check tw4093 }
  3343. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  3344. if curreq>besteq then
  3345. begin
  3346. besteq:=curreq;
  3347. bestpd:=currpd;
  3348. if (besteq=te_exact) then
  3349. break;
  3350. end;
  3351. end;
  3352. stackitem:=stackitem^.next;
  3353. end;
  3354. result:=bestpd;
  3355. end;
  3356. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3357. begin
  3358. { search record/object symtable first for a suitable operator }
  3359. if from_def.typ in [recorddef,objectdef] then
  3360. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3361. if to_def.typ in [recorddef,objectdef] then
  3362. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3363. { if type conversion is explicit then search first for explicit
  3364. operator overload and if not found then use implicit operator }
  3365. if explicit then
  3366. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3367. else
  3368. result:=nil;
  3369. if result=nil then
  3370. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3371. { restore symtable stack }
  3372. if to_def.typ in [recorddef,objectdef] then
  3373. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3374. if from_def.typ in [recorddef,objectdef] then
  3375. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3376. end;
  3377. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3378. var
  3379. sym : Tprocsym;
  3380. hashedid : THashedIDString;
  3381. curreq,
  3382. besteq : tequaltype;
  3383. currpd,
  3384. bestpd : tprocdef;
  3385. stackitem : psymtablestackitem;
  3386. begin
  3387. hashedid.id:='enumerator';
  3388. besteq:=te_incompatible;
  3389. bestpd:=nil;
  3390. stackitem:=symtablestack.stack;
  3391. while assigned(stackitem) do
  3392. begin
  3393. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3394. if sym<>nil then
  3395. begin
  3396. if sym.typ<>procsym then
  3397. internalerror(200910241);
  3398. { if the source type is an alias then this is only the second choice,
  3399. if you mess with this code, check tw4093 }
  3400. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3401. if curreq>besteq then
  3402. begin
  3403. besteq:=curreq;
  3404. bestpd:=currpd;
  3405. if (besteq=te_exact) then
  3406. break;
  3407. end;
  3408. end;
  3409. stackitem:=stackitem^.next;
  3410. end;
  3411. result:=bestpd;
  3412. end;
  3413. function search_system_type(const s: TIDString): ttypesym;
  3414. var
  3415. sym : tsym;
  3416. begin
  3417. sym:=tsym(systemunit.Find(s));
  3418. if not assigned(sym) or
  3419. (sym.typ<>typesym) then
  3420. message1(cg_f_unknown_system_type,s);
  3421. result:=ttypesym(sym);
  3422. end;
  3423. function try_search_system_type(const s: TIDString): ttypesym;
  3424. var
  3425. sym : tsym;
  3426. begin
  3427. sym:=tsym(systemunit.Find(s));
  3428. if not assigned(sym) then
  3429. result:=nil
  3430. else
  3431. begin
  3432. if sym.typ<>typesym then
  3433. message1(cg_f_unknown_system_type,s);
  3434. result:=ttypesym(sym);
  3435. end;
  3436. end;
  3437. function try_search_current_module_type(const s: TIDString): ttypesym;
  3438. var
  3439. found: boolean;
  3440. srsymtable: tsymtable;
  3441. srsym: tsym;
  3442. begin
  3443. if s[1]='$' then
  3444. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  3445. else
  3446. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  3447. if found then
  3448. begin
  3449. if (srsym.typ<>typesym) then
  3450. internalerror(2014091207);
  3451. result:=ttypesym(srsym);
  3452. end
  3453. else
  3454. result:=nil;
  3455. end;
  3456. function search_system_proc(const s: TIDString): tprocdef;
  3457. var
  3458. srsym: tsym;
  3459. begin
  3460. srsym:=tsym(systemunit.find(s));
  3461. if not assigned(srsym) and
  3462. (cs_compilesystem in current_settings.moduleswitches) then
  3463. srsym:=tsym(systemunit.Find(upper(s)));
  3464. if not assigned(srsym) or
  3465. (srsym.typ<>procsym) then
  3466. message1(cg_f_unknown_compilerproc,s);
  3467. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3468. end;
  3469. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  3470. var
  3471. srsymtable: tsymtable;
  3472. sym: tsym;
  3473. begin
  3474. sym:=nil;
  3475. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  3476. (sym.typ=typesym) then
  3477. begin
  3478. result:=ttypesym(sym);
  3479. exit;
  3480. end
  3481. else
  3482. begin
  3483. if throwerror then
  3484. message2(cg_f_unknown_type_in_unit,typename,unitname);
  3485. result:=nil;
  3486. end;
  3487. end;
  3488. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  3489. var
  3490. s: string;
  3491. list: TFPObjectList;
  3492. i: integer;
  3493. st: tsymtable;
  3494. begin
  3495. result:=false;
  3496. odef:=nil;
  3497. { when there are no helpers active currently then we don't need to do
  3498. anything }
  3499. if current_module.extendeddefs.count=0 then
  3500. exit;
  3501. { no helpers for anonymous types }
  3502. if ((pd.typ in [recorddef,objectdef]) and
  3503. (
  3504. not assigned(tabstractrecorddef(pd).objrealname) or
  3505. (tabstractrecorddef(pd).objrealname^='')
  3506. )
  3507. ) or
  3508. not assigned(pd.typesym) then
  3509. exit;
  3510. { if pd is defined inside a procedure we must not use make_mangledname
  3511. (as a helper may not be defined in a procedure this is no problem...)}
  3512. st:=pd.owner;
  3513. while st.symtabletype in [objectsymtable,recordsymtable] do
  3514. st:=st.defowner.owner;
  3515. if st.symtabletype=localsymtable then
  3516. exit;
  3517. { the mangled name is used as the key for tmodule.extendeddefs }
  3518. s:=generate_objectpascal_helper_key(pd);
  3519. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  3520. if assigned(list) and (list.count>0) then
  3521. begin
  3522. i:=list.count-1;
  3523. repeat
  3524. odef:=tobjectdef(list[list.count-1]);
  3525. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  3526. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  3527. dec(i);
  3528. until result or (i<0);
  3529. if not result then
  3530. { just to be sure that noone uses odef }
  3531. odef:=nil;
  3532. end;
  3533. end;
  3534. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3535. var
  3536. hashedid : THashedIDString;
  3537. classh : tobjectdef;
  3538. i : integer;
  3539. pdef : tprocdef;
  3540. begin
  3541. result:=false;
  3542. { if there is no class helper for the class then there is no need to
  3543. search further }
  3544. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  3545. exit;
  3546. hashedid.id:=s;
  3547. repeat
  3548. srsymtable:=classh.symtable;
  3549. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3550. if srsym<>nil then
  3551. begin
  3552. case srsym.typ of
  3553. procsym:
  3554. begin
  3555. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3556. begin
  3557. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  3558. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  3559. continue;
  3560. { we need to know if a procedure references symbols
  3561. in the static symtable, because then it can't be
  3562. inlined from outside this unit }
  3563. if assigned(current_procinfo) and
  3564. (srsym.owner.symtabletype=staticsymtable) then
  3565. include(current_procinfo.flags,pi_uses_static_symtable);
  3566. { the first found method wins }
  3567. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3568. srsymtable:=srsym.owner;
  3569. addsymref(srsym);
  3570. result:=true;
  3571. exit;
  3572. end;
  3573. end;
  3574. typesym,
  3575. fieldvarsym,
  3576. constsym,
  3577. enumsym,
  3578. undefinedsym,
  3579. propertysym:
  3580. begin
  3581. addsymref(srsym);
  3582. result:=true;
  3583. exit;
  3584. end;
  3585. else
  3586. internalerror(2014041101);
  3587. end;
  3588. end;
  3589. { try the helper parent if available }
  3590. classh:=classh.childof;
  3591. until classh=nil;
  3592. srsym:=nil;
  3593. srsymtable:=nil;
  3594. end;
  3595. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3596. var
  3597. hashedid : THashedIDString;
  3598. stackitem : psymtablestackitem;
  3599. i : longint;
  3600. defowner : tobjectdef;
  3601. begin
  3602. hashedid.id:=class_helper_prefix+s;
  3603. stackitem:=symtablestack.stack;
  3604. while assigned(stackitem) do
  3605. begin
  3606. srsymtable:=stackitem^.symtable;
  3607. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3608. if assigned(srsym) then
  3609. begin
  3610. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3611. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3612. (srsym.typ<>procsym) then
  3613. internalerror(2009111505);
  3614. { check whether this procsym includes a helper for this particular class }
  3615. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3616. begin
  3617. { does pd inherit from (or is the same as) the class
  3618. that this method's category extended?
  3619. Warning: this list contains both category and objcclass methods
  3620. (for id.randommethod), so only check category methods here
  3621. }
  3622. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  3623. if is_objccategory(defowner) and
  3624. def_is_related(pd,defowner.childof) then
  3625. begin
  3626. { we need to know if a procedure references symbols
  3627. in the static symtable, because then it can't be
  3628. inlined from outside this unit }
  3629. if assigned(current_procinfo) and
  3630. (srsym.owner.symtabletype=staticsymtable) then
  3631. include(current_procinfo.flags,pi_uses_static_symtable);
  3632. { no need to keep looking. There might be other
  3633. categories that extend this, a parent or child
  3634. class with a method with the same name (either
  3635. overriding this one, or overridden by this one),
  3636. but that doesn't matter as far as the basic
  3637. procsym is concerned.
  3638. }
  3639. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3640. srsymtable:=srsym.owner;
  3641. addsymref(srsym);
  3642. result:=true;
  3643. exit;
  3644. end;
  3645. end;
  3646. end;
  3647. stackitem:=stackitem^.next;
  3648. end;
  3649. srsym:=nil;
  3650. srsymtable:=nil;
  3651. result:=false;
  3652. end;
  3653. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3654. var
  3655. hashedid : THashedIDString;
  3656. stackitem : psymtablestackitem;
  3657. i : longint;
  3658. begin
  3659. hashedid.id:=class_helper_prefix+s;
  3660. stackitem:=symtablestack.stack;
  3661. while assigned(stackitem) do
  3662. begin
  3663. srsymtable:=stackitem^.symtable;
  3664. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3665. if assigned(srsym) then
  3666. begin
  3667. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3668. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3669. (srsym.typ<>procsym) then
  3670. internalerror(2009112005);
  3671. { check whether this procsym includes a helper for this particular class }
  3672. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3673. begin
  3674. { we need to know if a procedure references symbols
  3675. in the static symtable, because then it can't be
  3676. inlined from outside this unit }
  3677. if assigned(current_procinfo) and
  3678. (srsym.owner.symtabletype=staticsymtable) then
  3679. include(current_procinfo.flags,pi_uses_static_symtable);
  3680. { no need to keep looking. There might be other
  3681. methods with the same name, but that doesn't matter
  3682. as far as the basic procsym is concerned.
  3683. }
  3684. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3685. { We need the symtable in which the classhelper-like sym
  3686. is located, not the objectdef. The reason is that the
  3687. callnode will climb the symtablestack until it encounters
  3688. this symtable to start looking for overloads (and it won't
  3689. find the objectsymtable in which this method sym is
  3690. located
  3691. srsymtable:=srsym.owner;
  3692. }
  3693. addsymref(srsym);
  3694. result:=true;
  3695. exit;
  3696. end;
  3697. end;
  3698. stackitem:=stackitem^.next;
  3699. end;
  3700. srsym:=nil;
  3701. srsymtable:=nil;
  3702. result:=false;
  3703. end;
  3704. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  3705. { searches n in symtable of pd and all anchestors }
  3706. var
  3707. srsymtable : tsymtable;
  3708. begin
  3709. { in case this is a formal class, first find the real definition }
  3710. if (oo_is_formal in pd.objectoptions) then
  3711. pd:=find_real_class_definition(tobjectdef(pd),true);
  3712. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  3713. exit;
  3714. result:=search_struct_member_no_helper(pd,s);
  3715. if assigned(result) then
  3716. exit;
  3717. { not found, now look for class helpers }
  3718. if is_objcclass(pd) then
  3719. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  3720. end;
  3721. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  3722. var
  3723. hashedid : THashedIDString;
  3724. srsym : tsym;
  3725. begin
  3726. hashedid.id:=s;
  3727. while assigned(pd) do
  3728. begin
  3729. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  3730. if assigned(srsym) then
  3731. begin
  3732. result:=srsym;
  3733. exit;
  3734. end;
  3735. if pd.typ=objectdef then
  3736. pd:=tobjectdef(pd).childof
  3737. else
  3738. pd:=nil;
  3739. end;
  3740. result:=nil;
  3741. end;
  3742. function search_macro(const s : string):tsym;
  3743. var
  3744. stackitem : psymtablestackitem;
  3745. hashedid : THashedIDString;
  3746. srsym : tsym;
  3747. begin
  3748. hashedid.id:=s;
  3749. { First search the localmacrosymtable before searching the
  3750. global macrosymtables from the units }
  3751. if assigned(current_module) then
  3752. begin
  3753. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  3754. if assigned(srsym) then
  3755. begin
  3756. result:= srsym;
  3757. exit;
  3758. end;
  3759. end;
  3760. stackitem:=macrosymtablestack.stack;
  3761. while assigned(stackitem) do
  3762. begin
  3763. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  3764. if assigned(srsym) then
  3765. begin
  3766. result:= srsym;
  3767. exit;
  3768. end;
  3769. stackitem:=stackitem^.next;
  3770. end;
  3771. result:= nil;
  3772. end;
  3773. function defined_macro(const s : string):boolean;
  3774. var
  3775. mac: tmacro;
  3776. begin
  3777. mac:=tmacro(search_macro(s));
  3778. if assigned(mac) then
  3779. begin
  3780. mac.is_used:=true;
  3781. defined_macro:=mac.defined;
  3782. end
  3783. else
  3784. defined_macro:=false;
  3785. end;
  3786. {****************************************************************************
  3787. Object Helpers
  3788. ****************************************************************************}
  3789. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  3790. { returns the default property of a class, searches also anchestors }
  3791. var
  3792. _defaultprop : tpropertysym;
  3793. helperpd : tobjectdef;
  3794. begin
  3795. _defaultprop:=nil;
  3796. { first search in helper's hierarchy }
  3797. if search_last_objectpascal_helper(pd,nil,helperpd) then
  3798. while assigned(helperpd) do
  3799. begin
  3800. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  3801. if assigned(_defaultprop) then
  3802. break;
  3803. helperpd:=helperpd.childof;
  3804. end;
  3805. if assigned(_defaultprop) then
  3806. begin
  3807. search_default_property:=_defaultprop;
  3808. exit;
  3809. end;
  3810. { now search in the type's hierarchy itself }
  3811. while assigned(pd) do
  3812. begin
  3813. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  3814. if assigned(_defaultprop) then
  3815. break;
  3816. if (pd.typ=objectdef) then
  3817. pd:=tobjectdef(pd).childof
  3818. else
  3819. break;
  3820. end;
  3821. search_default_property:=_defaultprop;
  3822. end;
  3823. {****************************************************************************
  3824. Macro Helpers
  3825. ****************************************************************************}
  3826. procedure def_system_macro(const name : string);
  3827. var
  3828. mac : tmacro;
  3829. s: string;
  3830. begin
  3831. if name = '' then
  3832. internalerror(2004121202);
  3833. s:= upper(name);
  3834. mac:=tmacro(search_macro(s));
  3835. if not assigned(mac) then
  3836. begin
  3837. mac:=tmacro.create(s);
  3838. if assigned(current_module) then
  3839. current_module.localmacrosymtable.insert(mac)
  3840. else
  3841. initialmacrosymtable.insert(mac);
  3842. end;
  3843. Message1(parser_c_macro_defined,mac.name);
  3844. mac.defined:=true;
  3845. end;
  3846. procedure set_system_macro(const name, value : string);
  3847. var
  3848. mac : tmacro;
  3849. s: string;
  3850. begin
  3851. if name = '' then
  3852. internalerror(2004121203);
  3853. s:= upper(name);
  3854. mac:=tmacro(search_macro(s));
  3855. if not assigned(mac) then
  3856. begin
  3857. mac:=tmacro.create(s);
  3858. if assigned(current_module) then
  3859. current_module.localmacrosymtable.insert(mac)
  3860. else
  3861. initialmacrosymtable.insert(mac);
  3862. end
  3863. else
  3864. begin
  3865. mac.is_compiler_var:=false;
  3866. if assigned(mac.buftext) then
  3867. freemem(mac.buftext,mac.buflen);
  3868. end;
  3869. Message2(parser_c_macro_set_to,mac.name,value);
  3870. mac.buflen:=length(value);
  3871. getmem(mac.buftext,mac.buflen);
  3872. move(value[1],mac.buftext^,mac.buflen);
  3873. mac.defined:=true;
  3874. end;
  3875. procedure set_system_compvar(const name, value : string);
  3876. var
  3877. mac : tmacro;
  3878. s: string;
  3879. begin
  3880. if name = '' then
  3881. internalerror(2004121204);
  3882. s:= upper(name);
  3883. mac:=tmacro(search_macro(s));
  3884. if not assigned(mac) then
  3885. begin
  3886. mac:=tmacro.create(s);
  3887. mac.is_compiler_var:=true;
  3888. if assigned(current_module) then
  3889. current_module.localmacrosymtable.insert(mac)
  3890. else
  3891. initialmacrosymtable.insert(mac);
  3892. end
  3893. else
  3894. begin
  3895. mac.is_compiler_var:=true;
  3896. if assigned(mac.buftext) then
  3897. freemem(mac.buftext,mac.buflen);
  3898. end;
  3899. Message2(parser_c_macro_set_to,mac.name,value);
  3900. mac.buflen:=length(value);
  3901. getmem(mac.buftext,mac.buflen);
  3902. move(value[1],mac.buftext^,mac.buflen);
  3903. mac.defined:=true;
  3904. end;
  3905. procedure undef_system_macro(const name : string);
  3906. var
  3907. mac : tmacro;
  3908. s: string;
  3909. begin
  3910. if name = '' then
  3911. internalerror(2004121205);
  3912. s:= upper(name);
  3913. mac:=tmacro(search_macro(s));
  3914. if not assigned(mac) then
  3915. {If not found, then it's already undefined.}
  3916. else
  3917. begin
  3918. Message1(parser_c_macro_undefined,mac.name);
  3919. mac.defined:=false;
  3920. mac.is_compiler_var:=false;
  3921. { delete old definition }
  3922. if assigned(mac.buftext) then
  3923. begin
  3924. freemem(mac.buftext,mac.buflen);
  3925. mac.buftext:=nil;
  3926. end;
  3927. end;
  3928. end;
  3929. {$ifdef UNITALIASES}
  3930. {****************************************************************************
  3931. TUNIT_ALIAS
  3932. ****************************************************************************}
  3933. constructor tunit_alias.create(const n:string);
  3934. var
  3935. i : longint;
  3936. begin
  3937. i:=pos('=',n);
  3938. if i=0 then
  3939. fail;
  3940. inherited createname(Copy(n,1,i-1));
  3941. newname:=stringdup(Copy(n,i+1,255));
  3942. end;
  3943. destructor tunit_alias.destroy;
  3944. begin
  3945. stringdispose(newname);
  3946. inherited destroy;
  3947. end;
  3948. procedure addunitalias(const n:string);
  3949. begin
  3950. unitaliases^.insert(tunit_alias,init(Upper(n))));
  3951. end;
  3952. function getunitalias(const n:string):string;
  3953. var
  3954. p : punit_alias;
  3955. begin
  3956. p:=punit_alias(unitaliases^.Find(Upper(n)));
  3957. if assigned(p) then
  3958. getunitalias:=punit_alias(p).newname^
  3959. else
  3960. getunitalias:=n;
  3961. end;
  3962. {$endif UNITALIASES}
  3963. {****************************************************************************
  3964. Init/Done Symtable
  3965. ****************************************************************************}
  3966. procedure InitSymtable;
  3967. begin
  3968. { Reset symbolstack }
  3969. symtablestack:=nil;
  3970. systemunit:=nil;
  3971. { create error syms and def }
  3972. generrorsym:=terrorsym.create;
  3973. generrordef:=cerrordef.create;
  3974. { macros }
  3975. initialmacrosymtable:=tmacrosymtable.create(false);
  3976. macrosymtablestack:=TSymtablestack.create;
  3977. macrosymtablestack.push(initialmacrosymtable);
  3978. {$ifdef UNITALIASES}
  3979. { unit aliases }
  3980. unitaliases:=TFPHashObjectList.create;
  3981. {$endif}
  3982. { set some global vars to nil, might be important for the ide }
  3983. class_tobject:=nil;
  3984. interface_iunknown:=nil;
  3985. interface_idispatch:=nil;
  3986. rec_tguid:=nil;
  3987. rec_jmp_buf:=nil;
  3988. rec_exceptaddr:=nil;
  3989. objc_metaclasstype:=nil;
  3990. objc_superclasstype:=nil;
  3991. objc_idtype:=nil;
  3992. objc_seltype:=nil;
  3993. objc_objecttype:=nil;
  3994. dupnr:=0;
  3995. end;
  3996. procedure DoneSymtable;
  3997. begin
  3998. generrorsym.owner:=nil;
  3999. generrorsym.free;
  4000. generrordef.owner:=nil;
  4001. generrordef.free;
  4002. initialmacrosymtable.free;
  4003. macrosymtablestack.free;
  4004. {$ifdef UNITALIASES}
  4005. unitaliases.free;
  4006. {$endif}
  4007. end;
  4008. end.