symtable.pas 166 KB

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