symtable.pas 166 KB

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