symtable.pas 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,globtype,tokens,
  23. { symtable }
  24. symconst,symbase,symtype,symdef,symsym;
  25. {****************************************************************************
  26. Symtable types
  27. ****************************************************************************}
  28. type
  29. tstoredsymtable = class(TSymtable)
  30. private
  31. init_final_check_done : boolean;
  32. procedure _needs_init_final(sym:TObject;arg:pointer);
  33. procedure check_forward(sym:TObject;arg:pointer);
  34. procedure check_block_valid(def: TObject;arg:pointer);
  35. procedure labeldefined(sym:TObject;arg:pointer);
  36. procedure varsymbolused(sym:TObject;arg:pointer);
  37. procedure TestPrivate(sym:TObject;arg:pointer);
  38. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  39. procedure loaddefs(ppufile:tcompilerppufile);
  40. procedure loadsyms(ppufile:tcompilerppufile);
  41. procedure writedefs(ppufile:tcompilerppufile);
  42. procedure writesyms(ppufile:tcompilerppufile);
  43. public
  44. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  45. procedure delete(sym:TSymEntry);override;
  46. { load/write }
  47. procedure ppuload(ppufile:tcompilerppufile);virtual;
  48. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  49. procedure buildderef;
  50. procedure buildderefimpl;
  51. { buildderef but only for (recursively) used symbols/defs }
  52. procedure buildderef_registered;
  53. procedure deref(only_registered: boolean);virtual;
  54. procedure derefimpl(only_registered: boolean);virtual;
  55. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  56. procedure allsymbolsused;
  57. procedure allprivatesused;
  58. procedure check_forwards;
  59. procedure checklabels;
  60. function needs_init_final : boolean;
  61. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  62. end;
  63. {$ifdef llvm}
  64. tllvmshadowsymtableentry = class
  65. constructor create(def: tdef; fieldoffset: aint);
  66. private
  67. ffieldoffset: aint;
  68. fdef: tdef;
  69. public
  70. property fieldoffset: aint read ffieldoffset;
  71. property def: tdef read fdef;
  72. end;
  73. tllvmshadowsymtable = class;
  74. {$endif llvm}
  75. tabstractrecordsymtable = class(tstoredsymtable)
  76. {$ifdef llvm}
  77. private
  78. fllvmst: tllvmshadowsymtable;
  79. function getllvmshadowsymtabll: tllvmshadowsymtable;
  80. {$endif llvm}
  81. public
  82. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  83. recordalignment, { alignment desired when inserting this record }
  84. fieldalignment, { alignment current alignment used when fields are inserted }
  85. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  86. recordalignmin, { local equivalents of global settings, so that records can }
  87. maxCrecordalign: shortint; { be created with custom settings internally }
  88. constructor create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  89. destructor destroy;override;
  90. procedure ppuload(ppufile:tcompilerppufile);override;
  91. procedure ppuwrite(ppufile:tcompilerppufile);override;
  92. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  93. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  94. procedure addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  95. { returns the field closest to this offset (may not be exact because
  96. of padding; internalerrors for variant records, assumes fields are
  97. ordered by increasing offset) }
  98. function findfieldbyoffset(offset:asizeint): tfieldvarsym;
  99. procedure addalignmentpadding;
  100. procedure insertdef(def:TDefEntry);override;
  101. function is_packed: boolean;
  102. function has_single_field(out def:tdef): boolean;
  103. function get_unit_symtable: tsymtable;
  104. protected
  105. { size in bytes including padding }
  106. _datasize : asizeint;
  107. { size in bits of the data in case of bitpacked record. Only important during construction, }
  108. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  109. databitsize : asizeint;
  110. { size in bytes of padding }
  111. _paddingsize : word;
  112. procedure setdatasize(val: asizeint);
  113. function getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  114. public
  115. function iscurrentunit: boolean; override;
  116. property datasize : asizeint read _datasize write setdatasize;
  117. property paddingsize: word read _paddingsize write _paddingsize;
  118. {$ifdef llvm}
  119. property llvmst: tllvmshadowsymtable read getllvmshadowsymtabll;
  120. {$endif llvm}
  121. end;
  122. trecordsymtable = class(tabstractrecordsymtable)
  123. public
  124. constructor create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  125. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  126. end;
  127. tObjectSymtable = class(tabstractrecordsymtable)
  128. public
  129. constructor create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  130. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  131. end;
  132. {$ifdef llvm}
  133. { llvm record definitions cannot contain variant/union parts, }
  134. { you have to flatten them first. the tllvmshadowsymtable }
  135. { contains a flattened version of a record/object symtable }
  136. tllvmshadowsymtable = class
  137. private
  138. equivst: tabstractrecordsymtable;
  139. curroffset: aint;
  140. recordalignmin: shortint;
  141. function get(f: tfieldvarsym): tllvmshadowsymtableentry;
  142. function get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  143. public
  144. symdeflist: TFPObjectList;
  145. constructor create(st: tabstractrecordsymtable);
  146. destructor destroy; override;
  147. property entries[index: tfieldvarsym]: tllvmshadowsymtableentry read get; default;
  148. { warning: do not call this with field.llvmfieldnr, as
  149. field.llvmfieldnr will only be initialised when the llvm shadow
  150. symtable is accessed for the first time. Use the default/entries
  151. property instead in this case }
  152. property entries_by_llvm_index[index: longint]: tllvmshadowsymtableentry read get_by_llvm_index;
  153. private
  154. // generate the table
  155. procedure generate;
  156. // helpers
  157. procedure appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  158. procedure findvariantstarts(variantstarts: tfplist);
  159. procedure addalignmentpadding(finalsize: aint);
  160. procedure buildmapping(variantstarts: tfplist);
  161. procedure buildtable(variantstarts: tfplist);
  162. end;
  163. {$endif llvm}
  164. { tabstractsubsymtable }
  165. tabstractsubsymtable = class(tstoredsymtable)
  166. public
  167. procedure ppuwrite(ppufile:tcompilerppufile);override;
  168. end;
  169. { tabstractlocalsymtable }
  170. tabstractlocalsymtable = class(tabstractsubsymtable)
  171. public
  172. function count_locals:longint;
  173. function iscurrentunit: boolean; override;
  174. end;
  175. tlocalsymtable = class(tabstractlocalsymtable)
  176. public
  177. constructor create(adefowner:tdef;level:byte);
  178. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  179. end;
  180. { tparasymtable }
  181. tparasymtable = class(tabstractlocalsymtable)
  182. public
  183. readonly: boolean;
  184. constructor create(adefowner:tdef;level:byte);
  185. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  186. procedure insertdef(def:TDefEntry);override;
  187. end;
  188. tabstractuniTSymtable = class(tstoredsymtable)
  189. public
  190. constructor create(const n : string;id:word);
  191. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  192. function findnamespace(const n:string):TSymEntry;virtual;
  193. function iscurrentunit:boolean;override;
  194. procedure insertunit(sym:TSymEntry);
  195. end;
  196. tglobalsymtable = class(tabstractuniTSymtable)
  197. public
  198. unittypecount : word;
  199. constructor create(const n : string;id:word);
  200. procedure ppuload(ppufile:tcompilerppufile);override;
  201. procedure ppuwrite(ppufile:tcompilerppufile);override;
  202. end;
  203. tstaticsymtable = class(tabstractuniTSymtable)
  204. public
  205. constructor create(const n : string;id:word);
  206. procedure ppuload(ppufile:tcompilerppufile);override;
  207. procedure ppuwrite(ppufile:tcompilerppufile);override;
  208. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  209. function findnamespace(const n:string):TSymEntry;override;
  210. end;
  211. tspecializesymtable = class(tglobalsymtable)
  212. public
  213. constructor create(const n : string;id:word);
  214. function iscurrentunit:boolean;override;
  215. end;
  216. twithsymtable = class(TSymtable)
  217. withrefnode : tobject; { tnode }
  218. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  219. destructor destroy;override;
  220. procedure clear;override;
  221. procedure insertdef(def:TDefEntry);override;
  222. end;
  223. tstt_excepTSymtable = class(TSymtable)
  224. public
  225. constructor create;
  226. end;
  227. tmacrosymtable = class(tstoredsymtable)
  228. public
  229. constructor create(exported: boolean);
  230. end;
  231. { tenumsymtable }
  232. tenumsymtable = class(tabstractsubsymtable)
  233. public
  234. procedure insert(sym: TSymEntry; checkdup: boolean = true); override;
  235. constructor create(adefowner:tdef);
  236. end;
  237. { tarraysymtable }
  238. tarraysymtable = class(tabstractsubsymtable)
  239. public
  240. procedure insertdef(def:TDefEntry);override;
  241. constructor create(adefowner:tdef);
  242. end;
  243. var
  244. systemunit : tglobalsymtable; { pointer to the system unit }
  245. type
  246. tsymbol_search_flag = (
  247. ssf_search_option,
  248. ssf_search_helper,
  249. ssf_has_inherited,
  250. ssf_no_addsymref
  251. );
  252. tsymbol_search_flags = set of tsymbol_search_flag;
  253. {****************************************************************************
  254. Functions
  255. ****************************************************************************}
  256. {*** Misc ***}
  257. function FullTypeName(def,otherdef:tdef):string;
  258. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  259. { def is the extended type of a helper }
  260. function generate_objectpascal_helper_key(def:tdef):string;
  261. procedure incompatibletypes(def1,def2:tdef);
  262. procedure hidesym(sym:TSymEntry);
  263. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym:TSymEntry; warn: boolean);
  264. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  265. {*** Search ***}
  266. procedure addsymref(sym:tsym);
  267. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  268. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  269. function defs_belong_to_same_generic(def1,def2:tdef):boolean;
  270. function get_generic_in_hierarchy_by_name(srsym:tsym;def:tdef):tdef;
  271. function return_specialization_of_generic(nesteddef,genericdef:tdef;out resultdef:tdef):boolean;
  272. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  273. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  274. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  275. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  276. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  277. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  278. { searches for a symbol with the given name that has the given option in
  279. symoptions set }
  280. function searchsym_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;option:tsymoption):boolean;
  281. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  282. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  283. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  284. function searchsym_in_class(classh: tobjectdef; contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  285. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  286. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  287. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  288. { searches symbols inside of a helper's implementation }
  289. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  290. function search_system_type(const s: TIDString): ttypesym;
  291. function try_search_system_type(const s: TIDString): ttypesym;
  292. function try_search_current_module_type(const s: TIDString): ttypesym;
  293. function search_system_proc(const s: TIDString): tprocdef;
  294. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  295. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  296. function search_struct_member_no_helper(pd : tabstractrecorddef;const s : string):tsym;
  297. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  298. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  299. { searches for the helper definition that's currently active for pd }
  300. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  301. { searches whether the symbol s is available in the currently active }
  302. { helper for pd }
  303. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  304. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  305. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  306. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  307. {and returns it if found. Returns nil otherwise.}
  308. function search_macro(const s : string):tsym;
  309. { Additionally to searching for a macro, also checks whether it's still }
  310. { actually defined (could be disable using "undef") }
  311. function defined_macro(const s : string):boolean;
  312. { Look for a system procedure (no overloads supported) }
  313. {*** Object Helpers ***}
  314. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  315. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  316. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  317. {*** Macro Helpers ***}
  318. {If called initially, the following procedures manipulate macros in }
  319. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  320. {Name can be given in any case (it will be converted to upper case).}
  321. procedure def_system_macro(const name : string);
  322. procedure set_system_macro(const name, value : string);
  323. procedure set_system_compvar(const name, value : string);
  324. procedure undef_system_macro(const name : string);
  325. {*** symtable stack ***}
  326. { $ifdef DEBUG
  327. procedure test_symtablestack;
  328. procedure list_symtablestack;
  329. $endif DEBUG}
  330. {$ifdef UNITALIASES}
  331. type
  332. punit_alias = ^tunit_alias;
  333. tunit_alias = object(TNamedIndexItem)
  334. newname : pshortstring;
  335. constructor init(const n:string);
  336. destructor done;virtual;
  337. end;
  338. var
  339. unitaliases : pdictionary;
  340. procedure addunitalias(const n:string);
  341. function getunitalias(const n:string):string;
  342. {$endif UNITALIASES}
  343. {*** Init / Done ***}
  344. procedure IniTSymtable;
  345. procedure DoneSymtable;
  346. const
  347. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  348. { NOTOKEN } 'error',
  349. { _PLUS } 'plus',
  350. { _MINUS } 'minus',
  351. { _STAR } 'star',
  352. { _SLASH } 'slash',
  353. { _EQ } 'equal',
  354. { _GT } 'greater',
  355. { _LT } 'lower',
  356. { _GTE } 'greater_or_equal',
  357. { _LTE } 'lower_or_equal',
  358. { _NE } 'not_equal',
  359. { _SYMDIF } 'sym_diff',
  360. { _STARSTAR } 'starstar',
  361. { _OP_AS } 'as',
  362. { _OP_IN } 'in',
  363. { _OP_IS } 'is',
  364. { _OP_OR } 'or',
  365. { _OP_AND } 'and',
  366. { _OP_DIV } 'div',
  367. { _OP_MOD } 'mod',
  368. { _OP_NOT } 'not',
  369. { _OP_SHL } 'shl',
  370. { _OP_SHR } 'shr',
  371. { _OP_XOR } 'xor',
  372. { _ASSIGNMENT } 'assign',
  373. { _OP_EXPLICIT } 'explicit',
  374. { _OP_ENUMERATOR } 'enumerator',
  375. { _OP_INC } 'inc',
  376. { _OP_DEC } 'dec');
  377. implementation
  378. uses
  379. { global }
  380. verbose,globals,
  381. { symtable }
  382. symutil,defutil,defcmp,objcdef,
  383. { module }
  384. fmodule,
  385. { codegen }
  386. procinfo,
  387. { ppu }
  388. entfile
  389. ;
  390. var
  391. dupnr : longint; { unique number for duplicate symbols }
  392. {*****************************************************************************
  393. TStoredSymtable
  394. *****************************************************************************}
  395. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  396. begin
  397. inherited insert(sym,checkdup);
  398. end;
  399. procedure tstoredsymtable.delete(sym:TSymEntry);
  400. begin
  401. inherited delete(sym);
  402. end;
  403. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  404. begin
  405. { load the table's flags }
  406. if ppufile.readentry<>ibsymtableoptions then
  407. Message(unit_f_ppu_read_error);
  408. ppufile.getsmallset(tableoptions);
  409. { load definitions }
  410. loaddefs(ppufile);
  411. { load symbols }
  412. loadsyms(ppufile);
  413. init_final_check_done:=true;
  414. end;
  415. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  416. begin
  417. { ensure that we have the sto_needs_init_final flag set if needed }
  418. if not init_final_check_done then
  419. needs_init_final;
  420. { write the table's flags }
  421. ppufile.putsmallset(tableoptions);
  422. ppufile.writeentry(ibsymtableoptions);
  423. { write definitions }
  424. writedefs(ppufile);
  425. { write symbols }
  426. writesyms(ppufile);
  427. end;
  428. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  429. var
  430. def : tdef;
  431. b : byte;
  432. begin
  433. def:=nil;
  434. { load start of definition section, which holds the amount of defs }
  435. if ppufile.readentry<>ibstartdefs then
  436. Message(unit_f_ppu_read_error);
  437. { read definitions }
  438. repeat
  439. b:=ppufile.readentry;
  440. case b of
  441. ibpointerdef : def:=cpointerdef.ppuload(ppufile);
  442. ibarraydef : def:=carraydef.ppuload(ppufile);
  443. iborddef : def:=corddef.ppuload(ppufile);
  444. ibfloatdef : def:=cfloatdef.ppuload(ppufile);
  445. ibprocdef : def:=cprocdef.ppuload(ppufile);
  446. ibshortstringdef : def:=cstringdef.loadshort(ppufile);
  447. iblongstringdef : def:=cstringdef.loadlong(ppufile);
  448. ibansistringdef : def:=cstringdef.loadansi(ppufile);
  449. ibwidestringdef : def:=cstringdef.loadwide(ppufile);
  450. ibunicodestringdef : def:=cstringdef.loadunicode(ppufile);
  451. ibrecorddef : def:=crecorddef.ppuload(ppufile);
  452. ibobjectdef : def:=cobjectdef.ppuload(ppufile);
  453. ibenumdef : def:=cenumdef.ppuload(ppufile);
  454. ibsetdef : def:=csetdef.ppuload(ppufile);
  455. ibprocvardef : def:=cprocvardef.ppuload(ppufile);
  456. ibfiledef : def:=cfiledef.ppuload(ppufile);
  457. ibclassrefdef : def:=cclassrefdef.ppuload(ppufile);
  458. ibformaldef : def:=cformaldef.ppuload(ppufile);
  459. ibvariantdef : def:=cvariantdef.ppuload(ppufile);
  460. ibundefineddef : def:=cundefineddef.ppuload(ppufile);
  461. ibenddefs : break;
  462. ibend : Message(unit_f_ppu_read_error);
  463. else
  464. Message1(unit_f_ppu_invalid_entry,tostr(b));
  465. end;
  466. InsertDef(def);
  467. until false;
  468. end;
  469. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  470. var
  471. b : byte;
  472. sym : tsym;
  473. begin
  474. sym:=nil;
  475. { load start of definition section, which holds the amount of defs }
  476. if ppufile.readentry<>ibstartsyms then
  477. Message(unit_f_ppu_read_error);
  478. { now read the symbols }
  479. repeat
  480. b:=ppufile.readentry;
  481. case b of
  482. ibtypesym : sym:=ctypesym.ppuload(ppufile);
  483. ibprocsym : sym:=cprocsym.ppuload(ppufile);
  484. ibconstsym : sym:=cconstsym.ppuload(ppufile);
  485. ibstaticvarsym : sym:=cstaticvarsym.ppuload(ppufile);
  486. iblocalvarsym : sym:=clocalvarsym.ppuload(ppufile);
  487. ibparavarsym : sym:=cparavarsym.ppuload(ppufile);
  488. ibfieldvarsym : sym:=cfieldvarsym.ppuload(ppufile);
  489. ibabsolutevarsym : sym:=cabsolutevarsym.ppuload(ppufile);
  490. ibenumsym : sym:=cenumsym.ppuload(ppufile);
  491. ibpropertysym : sym:=cpropertysym.ppuload(ppufile);
  492. ibunitsym : sym:=cunitsym.ppuload(ppufile);
  493. iblabelsym : sym:=clabelsym.ppuload(ppufile);
  494. ibsyssym : sym:=csyssym.ppuload(ppufile);
  495. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  496. ibnamespacesym : sym:=cnamespacesym.ppuload(ppufile);
  497. ibendsyms : break;
  498. ibend : Message(unit_f_ppu_read_error);
  499. else
  500. Message1(unit_f_ppu_invalid_entry,tostr(b));
  501. end;
  502. Insert(sym,false);
  503. until false;
  504. end;
  505. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  506. var
  507. defcount,
  508. i : longint;
  509. def : tstoreddef;
  510. begin
  511. defcount:=0;
  512. for i:=0 to DefList.Count-1 do
  513. if tstoreddef(DefList[i]).is_registered then
  514. inc(defcount);
  515. { each definition get a number, write then the amount of defs to the
  516. ibstartdef entry }
  517. ppufile.putlongint(defcount);
  518. ppufile.writeentry(ibstartdefs);
  519. { now write the definition }
  520. for i:=0 to DefList.Count-1 do
  521. begin
  522. def:=tstoreddef(DefList[i]);
  523. if def.is_registered then
  524. def.ppuwrite(ppufile);
  525. end;
  526. { write end of definitions }
  527. ppufile.writeentry(ibenddefs);
  528. end;
  529. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  530. var
  531. symcount,
  532. i : longint;
  533. sym : Tstoredsym;
  534. begin
  535. symcount:=0;
  536. for i:=0 to SymList.Count-1 do
  537. if tstoredsym(SymList[i]).is_registered then
  538. inc(symcount);
  539. { each definition get a number, write then the amount of syms and the
  540. datasize to the ibsymdef entry }
  541. ppufile.putlongint(symcount);
  542. ppufile.writeentry(ibstartsyms);
  543. { foreach is used to write all symbols }
  544. for i:=0 to SymList.Count-1 do
  545. begin
  546. sym:=tstoredsym(SymList[i]);
  547. if sym.is_registered then
  548. sym.ppuwrite(ppufile);
  549. end;
  550. { end of symbols }
  551. ppufile.writeentry(ibendsyms);
  552. end;
  553. procedure tstoredsymtable.buildderef;
  554. var
  555. i : longint;
  556. def : tstoreddef;
  557. sym : tstoredsym;
  558. begin
  559. { interface definitions }
  560. for i:=0 to DefList.Count-1 do
  561. begin
  562. def:=tstoreddef(DefList[i]);
  563. def.buildderef;
  564. end;
  565. { interface symbols }
  566. for i:=0 to SymList.Count-1 do
  567. begin
  568. sym:=tstoredsym(SymList[i]);
  569. sym.buildderef;
  570. end;
  571. end;
  572. procedure tstoredsymtable.buildderefimpl;
  573. var
  574. i : longint;
  575. def : tstoreddef;
  576. begin
  577. { implementation definitions }
  578. for i:=0 to DefList.Count-1 do
  579. begin
  580. def:=tstoreddef(DefList[i]);
  581. def.buildderefimpl;
  582. end;
  583. end;
  584. procedure tstoredsymtable.buildderef_registered;
  585. var
  586. def : tstoreddef;
  587. sym : tstoredsym;
  588. i : longint;
  589. defidmax,
  590. symidmax: longint;
  591. newbuiltdefderefs,
  592. builtdefderefs,
  593. builtsymderefs: array of boolean;
  594. begin
  595. { tdefs for which we already built the deref }
  596. setlength(builtdefderefs,deflist.count);
  597. { tdefs for which we built the deref in this iteration }
  598. setlength(newbuiltdefderefs,deflist.count);
  599. { syms for which we already built the deref }
  600. setlength(builtsymderefs,symlist.count);
  601. repeat
  602. { we only have to store the defs (recursively) referred by wpo info
  603. or inlined routines in the static symbtable }
  604. { current number of registered defs/syms }
  605. defidmax:=current_module.deflist.count;
  606. symidmax:=current_module.symlist.count;
  607. { build the derefs for the registered defs we haven't processed yet }
  608. for i:=0 to DefList.Count-1 do
  609. begin
  610. if not builtdefderefs[i] then
  611. begin
  612. def:=tstoreddef(DefList[i]);
  613. if def.is_registered then
  614. begin
  615. def.buildderef;
  616. newbuiltdefderefs[i]:=true;
  617. builtdefderefs[i]:=true;
  618. end;
  619. end;
  620. end;
  621. { same for the syms }
  622. for i:=0 to SymList.Count-1 do
  623. begin
  624. if not builtsymderefs[i] then
  625. begin
  626. sym:=tstoredsym(SymList[i]);
  627. if sym.is_registered then
  628. begin
  629. sym.buildderef;
  630. builtsymderefs[i]:=true;
  631. end;
  632. end;
  633. end;
  634. { now buildderefimpl for the defs we processed in this iteration }
  635. for i:=0 to DefList.Count-1 do
  636. begin
  637. if newbuiltdefderefs[i] then
  638. begin
  639. newbuiltdefderefs[i]:=false;
  640. tstoreddef(DefList[i]).buildderefimpl;
  641. end;
  642. end;
  643. { stop when no new defs or syms have been registered while processing
  644. the currently registered ones (defs/syms get added to the module's
  645. deflist/symlist when they are registered) }
  646. until
  647. (defidmax=current_module.deflist.count) and
  648. (symidmax=current_module.symlist.count);
  649. end;
  650. procedure tstoredsymtable.deref(only_registered: boolean);
  651. var
  652. i : longint;
  653. def : tstoreddef;
  654. sym : tstoredsym;
  655. begin
  656. { first deref the interface ttype symbols. This is needs
  657. to be done before the interface defs are derefed, because
  658. the interface defs can contain references to the type symbols
  659. which then already need to contain a resolved typedef field (PFV) }
  660. for i:=0 to SymList.Count-1 do
  661. begin
  662. sym:=tstoredsym(SymList[i]);
  663. if (sym.typ=typesym) and
  664. (not only_registered or
  665. sym.is_registered) then
  666. sym.deref;
  667. end;
  668. { interface definitions }
  669. for i:=0 to DefList.Count-1 do
  670. begin
  671. def:=tstoreddef(DefList[i]);
  672. if not only_registered or
  673. def.is_registered then
  674. def.deref;
  675. end;
  676. { interface symbols }
  677. for i:=0 to SymList.Count-1 do
  678. begin
  679. sym:=tstoredsym(SymList[i]);
  680. if (not only_registered or
  681. sym.is_registered) and
  682. (sym.typ<>typesym) then
  683. sym.deref;
  684. end;
  685. end;
  686. procedure tstoredsymtable.derefimpl(only_registered: boolean);
  687. var
  688. i : longint;
  689. def : tstoreddef;
  690. begin
  691. { implementation definitions }
  692. for i:=0 to DefList.Count-1 do
  693. begin
  694. def:=tstoreddef(DefList[i]);
  695. if not only_registered or
  696. def.is_registered then
  697. def.derefimpl;
  698. end;
  699. end;
  700. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  701. var
  702. hsym : tsym;
  703. begin
  704. hsym:=tsym(FindWithHash(hashedid));
  705. if assigned(hsym) then
  706. DuplicateSym(hashedid,sym,hsym,false);
  707. result:=assigned(hsym);
  708. end;
  709. {**************************************
  710. Callbacks
  711. **************************************}
  712. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  713. begin
  714. if tsym(sym).typ=procsym then
  715. tprocsym(sym).check_forward
  716. { check also object method table }
  717. { we needn't to test the def list }
  718. { because each object has to have a type sym,
  719. only test objects declarations, not type renamings }
  720. else
  721. if (tsym(sym).typ=typesym) and
  722. assigned(ttypesym(sym).typedef) and
  723. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  724. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  725. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  726. end;
  727. procedure tstoredsymtable.check_block_valid(def: TObject; arg: pointer);
  728. var
  729. founderrordef: tdef;
  730. begin
  731. { all parameters passed to a block must be handled by the Objective-C
  732. runtime }
  733. if is_block(tdef(def)) and
  734. not objcchecktype(tdef(def),founderrordef) then
  735. if assigned(tdef(def).typesym) then
  736. MessagePos1(tdef(def).typesym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename)
  737. else
  738. Message1(type_e_objc_type_unsupported,tprocvardef(def).typename)
  739. end;
  740. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  741. begin
  742. if (tsym(sym).typ=labelsym) and
  743. not(tlabelsym(sym).defined) then
  744. begin
  745. if tlabelsym(sym).used then
  746. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  747. else
  748. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  749. end;
  750. end;
  751. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  752. begin
  753. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  754. ((tsym(sym).owner.symtabletype in
  755. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  756. begin
  757. { unused symbol should be reported only if no }
  758. { error is reported }
  759. { if the symbol is in a register it is used }
  760. { also don't count the value parameters which have local copies }
  761. { also don't claim for high param of open parameters (PM) }
  762. { also don't complain about unused symbols in generic procedures }
  763. { and methods }
  764. if (Errorcount<>0) or
  765. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  766. (sp_internal in tsym(sym).symoptions) or
  767. ((assigned(tsym(sym).owner.defowner) and
  768. (tsym(sym).owner.defowner.typ=procdef) and
  769. (df_generic in tprocdef(tsym(sym).owner.defowner).defoptions))) then
  770. exit;
  771. if (tstoredsym(sym).refs=0) then
  772. begin
  773. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  774. begin
  775. { don't warn about the result of constructors }
  776. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  777. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  778. not (po_noreturn in tprocdef(tsym(sym).owner.defowner).procoptions) and
  779. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  780. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  781. end
  782. else if (tsym(sym).owner.symtabletype=parasymtable) then
  783. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  784. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  785. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  786. else
  787. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  788. end
  789. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  790. begin
  791. if (tsym(sym).owner.symtabletype=parasymtable) then
  792. begin
  793. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  794. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  795. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  796. end
  797. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  798. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  799. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  800. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  801. end
  802. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  803. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  804. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  805. end
  806. else if ((tsym(sym).owner.symtabletype in
  807. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  808. begin
  809. if (Errorcount<>0) or
  810. (sp_internal in tsym(sym).symoptions) then
  811. exit;
  812. { do not claim for inherited private fields !! }
  813. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  814. case tsym(sym).typ of
  815. typesym:
  816. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  817. constsym:
  818. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  819. propertysym:
  820. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  821. else
  822. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  823. end
  824. { units references are problematic }
  825. else
  826. begin
  827. if (tsym(sym).refs=0) and
  828. not(tsym(sym).typ in [enumsym,unitsym,namespacesym]) and
  829. not(is_funcret_sym(tsym(sym))) and
  830. { don't complain about compiler generated syms for specializations, see also #13405 }
  831. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  832. (pos('$',ttypesym(sym).Realname)<>0)) and
  833. (
  834. (tsym(sym).typ<>procsym) or
  835. ((tsym(sym).owner.symtabletype=staticsymtable) and
  836. not current_module.is_unit)
  837. ) and
  838. { don't complain about alias for hidden _cmd parameter to
  839. obj-c methods }
  840. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  841. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  842. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  843. end;
  844. end;
  845. end;
  846. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  847. begin
  848. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  849. varsymbolused(sym,arg);
  850. end;
  851. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  852. begin
  853. {
  854. Don't test simple object aliases PM
  855. }
  856. if (tsym(sym).typ=typesym) and
  857. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  858. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  859. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  860. end;
  861. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  862. begin
  863. if (tsym(sym).typ=propertysym) and
  864. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  865. ppointer(arg)^:=sym;
  866. end;
  867. {***********************************************
  868. Process all entries
  869. ***********************************************}
  870. { checks, if all procsyms and methods are defined }
  871. procedure tstoredsymtable.check_forwards;
  872. begin
  873. SymList.ForEachCall(@check_forward,nil);
  874. { check whether all block definitions contain valid Objective-C types
  875. (now that all forward definitions have been resolved) }
  876. DefList.ForEachCall(@check_block_valid,nil);
  877. end;
  878. procedure tstoredsymtable.checklabels;
  879. begin
  880. SymList.ForEachCall(@labeldefined,nil);
  881. end;
  882. procedure tstoredsymtable.allsymbolsused;
  883. begin
  884. SymList.ForEachCall(@varsymbolused,nil);
  885. end;
  886. procedure tstoredsymtable.allprivatesused;
  887. begin
  888. SymList.ForEachCall(@objectprivatesymbolused,nil);
  889. end;
  890. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  891. begin
  892. if sto_needs_init_final in tableoptions then
  893. exit;
  894. { don't check static symbols - they can be present in structures only and
  895. always have a reference to a symbol defined on unit level }
  896. if sp_static in tsym(sym).symoptions then
  897. exit;
  898. case tsym(sym).typ of
  899. fieldvarsym,
  900. staticvarsym,
  901. localvarsym,
  902. paravarsym :
  903. begin
  904. if assigned(tabstractvarsym(sym).vardef) and
  905. is_managed_type(tabstractvarsym(sym).vardef) then
  906. include(tableoptions,sto_needs_init_final);
  907. end;
  908. end;
  909. end;
  910. { returns true, if p contains data which needs init/final code }
  911. function tstoredsymtable.needs_init_final : boolean;
  912. begin
  913. if not init_final_check_done then
  914. begin
  915. exclude(tableoptions,sto_needs_init_final);
  916. SymList.ForEachCall(@_needs_init_final,nil);
  917. init_final_check_done:=true;
  918. end;
  919. result:=sto_needs_init_final in tableoptions;
  920. end;
  921. {****************************************************************************
  922. TAbstractRecordSymtable
  923. ****************************************************************************}
  924. {$ifdef llvm}
  925. function tabstractrecordsymtable.getllvmshadowsymtabll: tllvmshadowsymtable;
  926. begin
  927. if not assigned(fllvmst) then
  928. fllvmst:=tllvmshadowsymtable.create(self);
  929. result:=fllvmst;
  930. end;
  931. {$endif llvm}
  932. constructor tabstractrecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  933. begin
  934. inherited create(n);
  935. moduleid:=current_module.moduleid;
  936. _datasize:=0;
  937. databitsize:=0;
  938. recordalignment:=1;
  939. usefieldalignment:=usealign;
  940. recordalignmin:=recordminalign;
  941. maxCrecordalign:=recordmaxCalign;
  942. padalignment:=1;
  943. { recordalign C_alignment means C record packing, that starts
  944. with an alignment of 1 }
  945. case usealign of
  946. C_alignment,
  947. bit_alignment:
  948. fieldalignment:=1;
  949. mac68k_alignment:
  950. fieldalignment:=2;
  951. else
  952. fieldalignment:=usealign;
  953. end;
  954. end;
  955. destructor tabstractrecordsymtable.destroy;
  956. begin
  957. {$ifdef llvm}
  958. fllvmst.free;
  959. {$endif llvm}
  960. inherited destroy;
  961. end;
  962. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  963. begin
  964. if ppufile.readentry<>ibrecsymtableoptions then
  965. Message(unit_f_ppu_read_error);
  966. recordalignment:=shortint(ppufile.getbyte);
  967. usefieldalignment:=shortint(ppufile.getbyte);
  968. recordalignmin:=shortint(ppufile.getbyte);
  969. if (usefieldalignment=C_alignment) then
  970. fieldalignment:=shortint(ppufile.getbyte);
  971. inherited ppuload(ppufile);
  972. end;
  973. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  974. var
  975. oldtyp : byte;
  976. begin
  977. oldtyp:=ppufile.entrytyp;
  978. ppufile.entrytyp:=subentryid;
  979. { in case of classes using C alignment, the alignment of the parent
  980. affects the alignment of fields of the childs }
  981. ppufile.putbyte(byte(recordalignment));
  982. ppufile.putbyte(byte(usefieldalignment));
  983. ppufile.putbyte(byte(recordalignmin));
  984. if (usefieldalignment=C_alignment) then
  985. ppufile.putbyte(byte(fieldalignment));
  986. ppufile.writeentry(ibrecsymtableoptions);
  987. inherited ppuwrite(ppufile);
  988. ppufile.entrytyp:=oldtyp;
  989. end;
  990. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  991. begin
  992. { optimal alignment of the record when declaring a variable of this }
  993. { type is independent of the packrecords setting }
  994. if (fieldoffs mod fieldalign) = 0 then
  995. result:=fieldalign
  996. else if (fieldalign >= 16) and
  997. ((fieldoffs mod 16) = 0) and
  998. ((fieldalign mod 16) = 0) then
  999. result:=16
  1000. else if (fieldalign >= 8) and
  1001. ((fieldoffs mod 8) = 0) and
  1002. ((fieldalign mod 8) = 0) then
  1003. result:=8
  1004. else if (fieldalign >= 4) and
  1005. ((fieldoffs mod 4) = 0) and
  1006. ((fieldalign mod 4) = 0) then
  1007. result:=4
  1008. else if (fieldalign >= 2) and
  1009. ((fieldoffs mod 2) = 0) and
  1010. ((fieldalign mod 2) = 0) then
  1011. result:=2
  1012. else
  1013. result:=1;
  1014. end;
  1015. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1016. var
  1017. varalignrecord: shortint;
  1018. begin
  1019. case usefieldalignment of
  1020. C_alignment:
  1021. varalignrecord:=used_align(varalign,recordalignmin,maxCrecordalign);
  1022. mac68k_alignment:
  1023. varalignrecord:=2;
  1024. else
  1025. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1026. end;
  1027. recordalignment:=max(recordalignment,varalignrecord);
  1028. end;
  1029. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1030. var
  1031. l : asizeint;
  1032. varalign : shortint;
  1033. vardef : tdef;
  1034. begin
  1035. if (sym.owner<>self) then
  1036. internalerror(200602031);
  1037. if sym.fieldoffset<>-1 then
  1038. internalerror(200602032);
  1039. { set visibility for the symbol }
  1040. sym.visibility:=vis;
  1041. { this symbol can't be loaded to a register }
  1042. sym.varregable:=vr_none;
  1043. { Calculate field offset }
  1044. l:=sym.getsize;
  1045. vardef:=sym.vardef;
  1046. varalign:=vardef.structalignment;
  1047. case usefieldalignment of
  1048. bit_alignment:
  1049. begin
  1050. { bitpacking only happens for ordinals, the rest is aligned at }
  1051. { 1 byte (compatible with GPC/GCC) }
  1052. if is_ordinal(vardef) then
  1053. begin
  1054. sym.fieldoffset:=databitsize;
  1055. l:=sym.getpackedbitsize;
  1056. end
  1057. else
  1058. begin
  1059. databitsize:=_datasize*8;
  1060. sym.fieldoffset:=databitsize;
  1061. if (l>high(asizeint) div 8) then
  1062. Message(sym_e_segment_too_large);
  1063. l:=l*8;
  1064. end;
  1065. if varalign=0 then
  1066. varalign:=size_2_align(l);
  1067. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1068. { bit packed records are limited to high(aint) bits }
  1069. { instead of bytes to avoid double precision }
  1070. { arithmetic in offset calculations }
  1071. if int64(l)>high(asizeint)-sym.fieldoffset then
  1072. begin
  1073. Message(sym_e_segment_too_large);
  1074. _datasize:=high(asizeint);
  1075. databitsize:=high(asizeint);
  1076. end
  1077. else
  1078. begin
  1079. databitsize:=sym.fieldoffset+l;
  1080. _datasize:=(databitsize+7) div 8;
  1081. end;
  1082. { rest is not applicable }
  1083. exit;
  1084. end;
  1085. else
  1086. begin
  1087. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1088. if l>high(asizeint)-sym.fieldoffset then
  1089. begin
  1090. Message(sym_e_segment_too_large);
  1091. _datasize:=high(asizeint);
  1092. end
  1093. else
  1094. _datasize:=sym.fieldoffset+l;
  1095. { Calc alignment needed for this record }
  1096. alignrecord(sym.fieldoffset,varalign);
  1097. end;
  1098. end;
  1099. end;
  1100. function field_alignment_compare(item1, item2: pointer): integer;
  1101. var
  1102. field1: tfieldvarsym absolute item1;
  1103. field2: tfieldvarsym absolute item2;
  1104. begin
  1105. { we don't care about static fields, those become global variables }
  1106. if (sp_static in field1.symoptions) or
  1107. (sp_static in field2.symoptions) then
  1108. exit(0);
  1109. { sort from large to small alignment, and in case of the same alignment
  1110. in declaration order (items declared close together are possibly
  1111. also related and hence possibly used together -> putting them next
  1112. to each other can improve cache behaviour) }
  1113. result:=field2.vardef.alignment-field1.vardef.alignment;
  1114. if result=0 then
  1115. result:=field1.fieldoffset-field2.fieldoffset;
  1116. end;
  1117. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1118. var
  1119. fieldvs, insertfieldvs: tfieldvarsym;
  1120. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1121. i, j, bestfieldindex: longint;
  1122. globalfieldalignment,
  1123. prevglobalfieldalignment,
  1124. newfieldalignment: shortint;
  1125. changed: boolean;
  1126. begin
  1127. if maybereorder and
  1128. (cs_opt_reorder_fields in current_settings.optimizerswitches) then
  1129. begin
  1130. { assign dummy field offsets so we can know their order in the
  1131. sorting routine }
  1132. for i:=0 to list.count-1 do
  1133. tfieldvarsym(list[i]).fieldoffset:=i;
  1134. { sort the non-class fields to minimise losses due to alignment }
  1135. list.sort(@field_alignment_compare);
  1136. { now fill up gaps caused by alignment skips with smaller fields
  1137. where possible }
  1138. repeat
  1139. i:=0;
  1140. base:=_datasize;
  1141. globalfieldalignment:=fieldalignment;
  1142. changed:=false;
  1143. while i<list.count do
  1144. begin
  1145. fieldvs:=tfieldvarsym(list[i]);
  1146. if sp_static in fieldvs.symoptions then
  1147. begin
  1148. inc(i);
  1149. continue;
  1150. end;
  1151. prevglobalfieldalignment:=globalfieldalignment;
  1152. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1153. newfieldalignment:=globalfieldalignment;
  1154. { size of the gap between the end of the previous field and
  1155. the start of the current one }
  1156. space:=fieldoffset-base;
  1157. bestspaceleft:=space;
  1158. while space>0 do
  1159. begin
  1160. bestfieldindex:=-1;
  1161. bestinsertfieldoffset:=-1;
  1162. for j:=i+1 to list.count-1 do
  1163. begin
  1164. insertfieldvs:=tfieldvarsym(list[j]);
  1165. if sp_static in insertfieldvs.symoptions then
  1166. continue;
  1167. insertfieldsize:=insertfieldvs.getsize;
  1168. { can the new field fit possibly in the gap? }
  1169. if insertfieldsize<=space then
  1170. begin
  1171. { restore globalfieldalignment to situation before
  1172. the original field was inserted }
  1173. globalfieldalignment:=prevglobalfieldalignment;
  1174. { at what offset would it be inserted? (this new
  1175. field has its own alignment requirements, which
  1176. may make it impossible to fit after all) }
  1177. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1178. globalfieldalignment:=prevglobalfieldalignment;
  1179. { taking into account the alignment, does it still
  1180. fit and if so, does it fit better than the
  1181. previously found best fit? }
  1182. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1183. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1184. begin
  1185. { new best fit }
  1186. bestfieldindex:=j;
  1187. bestinsertfieldoffset:=insertfieldoffset;
  1188. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1189. if bestspaceleft=0 then
  1190. break;
  1191. end;
  1192. end;
  1193. end;
  1194. { if we didn't find any field to fit, stop trying for this
  1195. gap }
  1196. if bestfieldindex=-1 then
  1197. break;
  1198. changed:=true;
  1199. { we found a field to insert -> adjust the new base
  1200. address }
  1201. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1202. { update globalfieldalignment for this newly inserted
  1203. field }
  1204. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1205. { move the new field before the current one }
  1206. list.move(bestfieldindex,i);
  1207. { and skip the new field (which is now at position i) }
  1208. inc(i);
  1209. { there may be more space left -> continue }
  1210. space:=bestspaceleft;
  1211. end;
  1212. if base>fieldoffset then
  1213. internalerror(2012071302);
  1214. { check the next field }
  1215. base:=fieldoffset+fieldvs.getsize;
  1216. { since the original field had the same or greater alignment
  1217. than anything we inserted before it, the global field
  1218. alignment is still the same now as it was originally after
  1219. inserting that field }
  1220. globalfieldalignment:=newfieldalignment;
  1221. inc(i);
  1222. end;
  1223. { there may be small gaps left *before* inserted fields }
  1224. until not changed;
  1225. end;
  1226. { reset the dummy field offsets }
  1227. for i:=0 to list.count-1 do
  1228. tfieldvarsym(list[i]).fieldoffset:=-1;
  1229. { finally, set the actual field offsets }
  1230. for i:=0 to list.count-1 do
  1231. begin
  1232. fieldvs:=tfieldvarsym(list[i]);
  1233. { static data fields are already inserted in the globalsymtable }
  1234. if not(sp_static in fieldvs.symoptions) then
  1235. begin
  1236. { read_record_fields already set the visibility of the fields,
  1237. because a single list can contain symbols with different
  1238. visibility }
  1239. addfield(fieldvs,fieldvs.visibility);
  1240. end;
  1241. end;
  1242. end;
  1243. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1244. var
  1245. i: longint;
  1246. sym: tsym;
  1247. begin
  1248. { there could be multiple fields in case of a variant record }
  1249. if (defowner.typ=recorddef) and
  1250. trecorddef(defowner).isunion then
  1251. internalerror(2014090403);
  1252. for i:=0 to SymList.count-1 do
  1253. begin
  1254. sym:=tsym(symlist[i]);
  1255. if (sym.typ=fieldvarsym) and
  1256. not(sp_static in sym.symoptions) and
  1257. (tfieldvarsym(sym).fieldoffset>=offset) then
  1258. begin
  1259. result:=tfieldvarsym(sym);
  1260. exit;
  1261. end;
  1262. end;
  1263. result:=nil;
  1264. end;
  1265. procedure tabstractrecordsymtable.addalignmentpadding;
  1266. var
  1267. padded_datasize: asizeint;
  1268. begin
  1269. { make the record size aligned correctly so it can be
  1270. used as elements in an array. For C records we
  1271. use the fieldalignment, because that is updated with the
  1272. used alignment. }
  1273. if (padalignment = 1) then
  1274. case usefieldalignment of
  1275. C_alignment:
  1276. padalignment:=fieldalignment;
  1277. { bitpacked }
  1278. bit_alignment:
  1279. padalignment:=1;
  1280. { mac68k: always round to multiple of 2 }
  1281. mac68k_alignment:
  1282. padalignment:=2;
  1283. { default/no packrecords specified }
  1284. 0:
  1285. padalignment:=recordalignment
  1286. { specific packrecords setting -> use as upper limit }
  1287. else
  1288. padalignment:=min(recordalignment,usefieldalignment);
  1289. end;
  1290. padded_datasize:=align(_datasize,padalignment);
  1291. _paddingsize:=padded_datasize-_datasize;
  1292. _datasize:=padded_datasize;
  1293. end;
  1294. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1295. begin
  1296. { Enums must also be available outside the record scope,
  1297. insert in the owner of this symtable }
  1298. if def.typ=enumdef then
  1299. defowner.owner.insertdef(def)
  1300. else
  1301. inherited insertdef(def);
  1302. end;
  1303. function tabstractrecordsymtable.is_packed: boolean;
  1304. begin
  1305. result:=usefieldalignment=bit_alignment;
  1306. end;
  1307. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1308. var
  1309. i: longint;
  1310. currentsymlist: TFPHashObjectList;
  1311. currentdef: tdef;
  1312. sym: tfieldvarsym;
  1313. begin
  1314. result:=false;
  1315. { If a record contains a union, it does not contain a "single
  1316. non-composite field" in the context of certain ABIs requiring
  1317. special treatment for such records }
  1318. if (defowner.typ=recorddef) and
  1319. trecorddef(defowner).isunion then
  1320. exit;
  1321. { a record/object can contain other things than fields }
  1322. currentsymlist:=symlist;
  1323. { recurse in arrays and records }
  1324. sym:=nil;
  1325. repeat
  1326. { record has one field? }
  1327. for i:=0 to currentsymlist.Count-1 do
  1328. begin
  1329. if (tsym(currentsymlist[i]).typ=fieldvarsym) and
  1330. not(sp_static in tsym(currentsymlist[i]).symoptions) then
  1331. begin
  1332. if result then
  1333. begin
  1334. result:=false;
  1335. exit;
  1336. end;
  1337. result:=true;
  1338. sym:=tfieldvarsym(currentsymlist[i])
  1339. end;
  1340. end;
  1341. if assigned(sym) then
  1342. begin
  1343. { if the field is an array, does it contain one element? }
  1344. currentdef:=sym.vardef;
  1345. while (currentdef.typ=arraydef) and
  1346. not is_special_array(currentdef) do
  1347. begin
  1348. if tarraydef(currentdef).elecount<>1 then
  1349. begin
  1350. result:=false;
  1351. exit;
  1352. end;
  1353. currentdef:=tarraydef(currentdef).elementdef;
  1354. end;
  1355. { if the array element is again a record, continue descending }
  1356. if currentdef.typ=recorddef then
  1357. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1358. else
  1359. begin
  1360. { otherwise we found the type of the single element }
  1361. def:=currentdef;
  1362. exit;
  1363. end;
  1364. end
  1365. else
  1366. exit
  1367. until false;
  1368. end;
  1369. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1370. begin
  1371. result:=defowner.owner;
  1372. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1373. result:=result.defowner.owner;
  1374. end;
  1375. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1376. begin
  1377. _datasize:=val;
  1378. if (usefieldalignment=bit_alignment) then
  1379. { can overflow in non bitpacked records }
  1380. databitsize:=val*8;
  1381. end;
  1382. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1383. var
  1384. l : asizeint;
  1385. varalignfield,
  1386. varalign : shortint;
  1387. vardef : tdef;
  1388. begin
  1389. { Calculate field offset }
  1390. l:=sym.getsize;
  1391. vardef:=sym.vardef;
  1392. varalign:=vardef.structalignment;
  1393. case usefieldalignment of
  1394. bit_alignment:
  1395. { has to be handled separately }
  1396. internalerror(2012071301);
  1397. C_alignment:
  1398. begin
  1399. { Calc the alignment size for C style records }
  1400. if (varalign>4) and
  1401. ((varalign mod 4)<>0) and
  1402. (vardef.typ=arraydef) then
  1403. Message1(sym_w_wrong_C_pack,vardef.typename);
  1404. if varalign=0 then
  1405. varalign:=l;
  1406. if (globalfieldalignment<maxCrecordalign) then
  1407. begin
  1408. if (varalign>16) and (globalfieldalignment<32) then
  1409. globalfieldalignment:=32
  1410. else if (varalign>12) and (globalfieldalignment<16) then
  1411. globalfieldalignment:=16
  1412. { 12 is needed for long double }
  1413. else if (varalign>8) and (globalfieldalignment<12) then
  1414. globalfieldalignment:=12
  1415. else if (varalign>4) and (globalfieldalignment<8) then
  1416. globalfieldalignment:=8
  1417. else if (varalign>2) and (globalfieldalignment<4) then
  1418. globalfieldalignment:=4
  1419. else if (varalign>1) and (globalfieldalignment<2) then
  1420. globalfieldalignment:=2;
  1421. end;
  1422. globalfieldalignment:=min(globalfieldalignment,maxCrecordalign);
  1423. end;
  1424. mac68k_alignment:
  1425. begin
  1426. { mac68k alignment (C description):
  1427. * char is aligned to 1 byte
  1428. * everything else (except vector) is aligned to 2 bytes
  1429. * vector is aligned to 16 bytes
  1430. }
  1431. if l>1 then
  1432. globalfieldalignment:=2
  1433. else
  1434. globalfieldalignment:=1;
  1435. varalign:=2;
  1436. end;
  1437. end;
  1438. if varalign=0 then
  1439. varalign:=size_2_align(l);
  1440. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1441. result:=align(base,varalignfield);
  1442. end;
  1443. function tabstractrecordsymtable.iscurrentunit: boolean;
  1444. begin
  1445. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1446. end;
  1447. {****************************************************************************
  1448. TRecordSymtable
  1449. ****************************************************************************}
  1450. constructor trecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1451. begin
  1452. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1453. symtabletype:=recordsymtable;
  1454. end;
  1455. { this procedure is reserved for inserting case variant into
  1456. a record symtable }
  1457. { the offset is the location of the start of the variant
  1458. and datasize and dataalignment corresponds to
  1459. the complete size (see code in pdecl unit) PM }
  1460. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  1461. var
  1462. sym : tsym;
  1463. def : tdef;
  1464. i : integer;
  1465. varalignrecord,varalign,
  1466. storesize,storealign : aint;
  1467. bitsize: tcgint;
  1468. begin
  1469. storesize:=_datasize;
  1470. storealign:=fieldalignment;
  1471. _datasize:=offset;
  1472. if (usefieldalignment=bit_alignment) then
  1473. databitsize:=offset*8;
  1474. { We move the ownership of the defs and symbols to the new recordsymtable.
  1475. The old unionsymtable keeps the references, but doesn't own the
  1476. objects anymore }
  1477. unionst.DefList.OwnsObjects:=false;
  1478. unionst.SymList.OwnsObjects:=false;
  1479. { copy symbols }
  1480. for i:=0 to unionst.SymList.Count-1 do
  1481. begin
  1482. sym:=TSym(unionst.SymList[i]);
  1483. if sym.typ<>fieldvarsym then
  1484. internalerror(200601272);
  1485. if tfieldvarsym(sym).fieldoffset=0 then
  1486. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1487. { add to this record symtable, checking for duplicate names }
  1488. // unionst.SymList.List.List^[i].Data:=nil;
  1489. insert(sym);
  1490. varalign:=tfieldvarsym(sym).vardef.alignment;
  1491. if varalign=0 then
  1492. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1493. { retrieve size }
  1494. if (usefieldalignment=bit_alignment) then
  1495. begin
  1496. { bit packed records are limited to high(aint) bits }
  1497. { instead of bytes to avoid double precision }
  1498. { arithmetic in offset calculations }
  1499. if is_ordinal(tfieldvarsym(sym).vardef) then
  1500. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1501. else
  1502. begin
  1503. bitsize:=tfieldvarsym(sym).getsize;
  1504. if (bitsize>high(asizeint) div 8) then
  1505. Message(sym_e_segment_too_large);
  1506. bitsize:=bitsize*8;
  1507. end;
  1508. if bitsize>high(asizeint)-databitsize then
  1509. begin
  1510. Message(sym_e_segment_too_large);
  1511. _datasize:=high(asizeint);
  1512. databitsize:=high(asizeint);
  1513. end
  1514. else
  1515. begin
  1516. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1517. _datasize:=(databitsize+7) div 8;
  1518. end;
  1519. tfieldvarsym(sym).fieldoffset:=databitsize;
  1520. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1521. end
  1522. else
  1523. begin
  1524. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1525. begin
  1526. Message(sym_e_segment_too_large);
  1527. _datasize:=high(asizeint);
  1528. end
  1529. else
  1530. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1531. { update address }
  1532. tfieldvarsym(sym).fieldoffset:=_datasize;
  1533. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1534. end;
  1535. { update alignment of this record }
  1536. if (usefieldalignment<>C_alignment) and
  1537. (usefieldalignment<>mac68k_alignment) then
  1538. recordalignment:=max(recordalignment,varalignrecord);
  1539. end;
  1540. { update alignment for C records }
  1541. if (usefieldalignment=C_alignment) and
  1542. (usefieldalignment<>mac68k_alignment) then
  1543. recordalignment:=max(recordalignment,unionst.recordalignment);
  1544. { Register defs in the new record symtable }
  1545. for i:=0 to unionst.DefList.Count-1 do
  1546. begin
  1547. def:=TDef(unionst.DefList[i]);
  1548. def.ChangeOwner(self);
  1549. end;
  1550. _datasize:=storesize;
  1551. fieldalignment:=storealign;
  1552. { If a record contains a union, it does not contain a "single
  1553. non-composite field" in the context of certain ABIs requiring
  1554. special treatment for such records }
  1555. if defowner.typ=recorddef then
  1556. trecorddef(defowner).isunion:=true;
  1557. end;
  1558. {****************************************************************************
  1559. TObjectSymtable
  1560. ****************************************************************************}
  1561. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1562. begin
  1563. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1564. symtabletype:=ObjectSymtable;
  1565. defowner:=adefowner;
  1566. end;
  1567. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1568. var
  1569. hsym: tsym;
  1570. warn: boolean;
  1571. begin
  1572. result:=false;
  1573. if not assigned(defowner) then
  1574. internalerror(200602061);
  1575. { procsym and propertysym have special code
  1576. to override values in inherited classes. For other
  1577. symbols check for duplicates }
  1578. if not(sym.typ in [procsym,propertysym]) then
  1579. begin
  1580. { but private ids can be reused }
  1581. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1582. if assigned(hsym) and
  1583. (
  1584. (
  1585. not(m_delphi in current_settings.modeswitches) and
  1586. is_visible_for_object(hsym,tobjectdef(defowner))
  1587. ) or
  1588. (
  1589. { In Delphi, you can repeat members of a parent class. You can't }
  1590. { do this for objects however, and you (obviouly) can't }
  1591. { declare two fields with the same name in a single class }
  1592. (m_delphi in current_settings.modeswitches) and
  1593. (
  1594. is_object(tdef(defowner)) or
  1595. (hsym.owner = self)
  1596. )
  1597. )
  1598. ) then
  1599. begin
  1600. { only watn when a parameter/local variable in a method
  1601. conflicts with a category method, because this can easily
  1602. happen due to all possible categories being imported via
  1603. CocoaAll }
  1604. warn:=
  1605. (is_objccategory(tdef(hsym.owner.defowner)) or
  1606. is_classhelper(tdef(hsym.owner.defowner))) and
  1607. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1608. DuplicateSym(hashedid,sym,hsym,warn);
  1609. result:=true;
  1610. end;
  1611. end
  1612. else
  1613. result:=inherited checkduplicate(hashedid,sym);
  1614. end;
  1615. {$ifdef llvm}
  1616. {****************************************************************************
  1617. tLlvmShadowSymtableEntry
  1618. ****************************************************************************}
  1619. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1620. begin
  1621. fdef:=def;
  1622. ffieldoffset:=fieldoffset;
  1623. end;
  1624. {****************************************************************************
  1625. TLlvmShadowSymtable
  1626. ****************************************************************************}
  1627. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1628. begin
  1629. result:=get_by_llvm_index(f.llvmfieldnr)
  1630. end;
  1631. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1632. begin
  1633. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1634. end;
  1635. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1636. begin
  1637. equivst:=st;
  1638. curroffset:=0;
  1639. symdeflist:=tfpobjectlist.create(true);
  1640. generate;
  1641. end;
  1642. destructor tllvmshadowsymtable.destroy;
  1643. begin
  1644. symdeflist.free;
  1645. end;
  1646. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1647. var
  1648. sizectr,
  1649. tmpsize: aint;
  1650. begin
  1651. case equivst.usefieldalignment of
  1652. bit_alignment:
  1653. begin
  1654. { curoffset: bit address after the previous field. }
  1655. { llvm has no special support for bitfields in records, }
  1656. { so we replace them with plain bytes. }
  1657. { as soon as a single bit of a byte is allocated, we }
  1658. { allocate the byte in the llvm shadow record }
  1659. if (fieldoffset>curroffset) then
  1660. curroffset:=align(curroffset,8);
  1661. { fields in bitpacked records always start either right }
  1662. { after the previous one, or at the next byte boundary. }
  1663. if (curroffset<>fieldoffset) then
  1664. internalerror(2008051002);
  1665. if is_ordinal(vardef) then
  1666. begin
  1667. tmpsize:=vardef.packedbitsize;
  1668. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1669. inc(curroffset,tmpsize);
  1670. tmpsize:=0;
  1671. while sizectr<>0 do
  1672. begin
  1673. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1674. dec(sizectr);
  1675. inc(tmpsize);
  1676. end;
  1677. end
  1678. else
  1679. begin
  1680. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1681. if not(derefclass) then
  1682. inc(curroffset,vardef.size*8)
  1683. else
  1684. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1685. end;
  1686. end
  1687. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1688. begin
  1689. { curoffset: address right after the previous field }
  1690. while (fieldoffset>curroffset) do
  1691. begin
  1692. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1693. inc(curroffset);
  1694. end;
  1695. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1696. if not(derefclass) then
  1697. inc(curroffset,vardef.size)
  1698. else
  1699. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1700. end
  1701. else
  1702. { default for llvm, don't add explicit padding }
  1703. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1704. end
  1705. end;
  1706. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1707. begin
  1708. case equivst.usefieldalignment of
  1709. { already correct in this case }
  1710. bit_alignment:
  1711. ;
  1712. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1713. begin
  1714. { add padding fields }
  1715. while (finalsize>curroffset) do
  1716. begin
  1717. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1718. inc(curroffset);
  1719. end;
  1720. end;
  1721. end;
  1722. end;
  1723. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1724. var
  1725. sym: tfieldvarsym;
  1726. lastoffset: aint;
  1727. newalignment: aint;
  1728. i, j: longint;
  1729. begin
  1730. i:=0;
  1731. while (i<equivst.symlist.count) do
  1732. begin
  1733. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1734. begin
  1735. inc(i);
  1736. continue;
  1737. end;
  1738. sym:=tfieldvarsym(equivst.symlist[i]);
  1739. { a "better" algorithm might be to use the largest }
  1740. { variant in case of (bit)packing, since then }
  1741. { alignment doesn't matter }
  1742. if (vo_is_first_field in sym.varoptions) then
  1743. begin
  1744. { we assume that all fields are processed in order. }
  1745. if (variantstarts.count<>0) then
  1746. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1747. else
  1748. lastoffset:=-1;
  1749. { new variant at same level as last one: use if higher alignment }
  1750. if (lastoffset=sym.fieldoffset) then
  1751. begin
  1752. if (equivst.fieldalignment<>bit_alignment) then
  1753. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1754. else
  1755. newalignment:=1;
  1756. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1757. variantstarts[variantstarts.count-1]:=sym;
  1758. end
  1759. { variant at deeper level than last one -> add }
  1760. else if (lastoffset<sym.fieldoffset) then
  1761. variantstarts.add(sym)
  1762. else
  1763. begin
  1764. { a variant at a less deep level, so backtrack }
  1765. j:=variantstarts.count-2;
  1766. while (j>=0) do
  1767. begin
  1768. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1769. break;
  1770. dec(j);
  1771. end;
  1772. if (j<0) then
  1773. internalerror(2008051003);
  1774. { new variant has higher alignment? }
  1775. if (equivst.fieldalignment<>bit_alignment) then
  1776. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1777. else
  1778. newalignment:=1;
  1779. { yes, replace and remove previous nested variants }
  1780. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1781. begin
  1782. variantstarts[j]:=sym;
  1783. variantstarts.count:=j+1;
  1784. end
  1785. { no, skip this variant }
  1786. else
  1787. begin
  1788. inc(i);
  1789. while (i<equivst.symlist.count) and
  1790. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1791. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  1792. inc(i);
  1793. continue;
  1794. end;
  1795. end;
  1796. end;
  1797. inc(i);
  1798. end;
  1799. end;
  1800. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1801. var
  1802. lastvaroffsetprocessed: aint;
  1803. i, equivcount, varcount: longint;
  1804. begin
  1805. { if it's an object/class, the first entry is the parent (if there is one) }
  1806. if (equivst.symtabletype=objectsymtable) and
  1807. assigned(tobjectdef(equivst.defowner).childof) then
  1808. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  1809. equivcount:=equivst.symlist.count;
  1810. varcount:=0;
  1811. i:=0;
  1812. lastvaroffsetprocessed:=-1;
  1813. while (i<equivcount) do
  1814. begin
  1815. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1816. begin
  1817. inc(i);
  1818. continue;
  1819. end;
  1820. { start of a new variant? }
  1821. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1822. begin
  1823. { if we want to process the same variant offset twice, it means that we }
  1824. { got to the end and are trying to process the next variant part -> stop }
  1825. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  1826. break;
  1827. if (varcount>=variantstarts.count) then
  1828. internalerror(2008051005);
  1829. { new variant part -> use the one with the biggest alignment }
  1830. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  1831. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  1832. inc(varcount);
  1833. if (i<0) then
  1834. internalerror(2008051004);
  1835. end;
  1836. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  1837. inc(i);
  1838. end;
  1839. addalignmentpadding(equivst.datasize);
  1840. end;
  1841. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  1842. var
  1843. i, varcount: longint;
  1844. shadowindex: longint;
  1845. equivcount : longint;
  1846. begin
  1847. varcount:=0;
  1848. shadowindex:=0;
  1849. equivcount:=equivst.symlist.count;
  1850. i:=0;
  1851. while (i < equivcount) do
  1852. begin
  1853. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1854. begin
  1855. inc(i);
  1856. continue;
  1857. end;
  1858. { start of a new variant? }
  1859. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1860. begin
  1861. { back up to a less deeply nested variant level? }
  1862. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  1863. dec(varcount);
  1864. { it's possible that some variants are more deeply nested than the
  1865. one we recorded in the shadowsymtable (since we recorded the one
  1866. with the biggest alignment, not necessarily the biggest one in size
  1867. }
  1868. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1869. varcount:=variantstarts.count-1
  1870. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1871. internalerror(2008051006);
  1872. { reset the shadowindex to the start of this variant. }
  1873. { in case the llvmfieldnr is not (yet) set for this }
  1874. { field, shadowindex will simply be reset to zero and }
  1875. { we'll start searching from the start of the record }
  1876. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  1877. if (varcount<pred(variantstarts.count)) then
  1878. inc(varcount);
  1879. end;
  1880. { find the last shadowfield whose offset <= the current field's offset }
  1881. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  1882. (shadowindex<symdeflist.count-1) and
  1883. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  1884. inc(shadowindex);
  1885. { set the field number and potential offset from that field (in case }
  1886. { of overlapping variants) }
  1887. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  1888. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  1889. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  1890. inc(i);
  1891. end;
  1892. end;
  1893. procedure tllvmshadowsymtable.generate;
  1894. var
  1895. variantstarts: tfplist;
  1896. begin
  1897. variantstarts:=tfplist.create;
  1898. { first go through the entire record and }
  1899. { store the fieldvarsyms of the variants }
  1900. { with the highest alignment }
  1901. findvariantstarts(variantstarts);
  1902. { now go through the regular fields and the selected variants, }
  1903. { and add them to the llvm shadow record symtable }
  1904. buildtable(variantstarts);
  1905. { finally map all original fields to the llvm definition }
  1906. buildmapping(variantstarts);
  1907. variantstarts.free;
  1908. end;
  1909. {$endif llvm}
  1910. {****************************************************************************
  1911. TAbstractSubSymtable
  1912. ****************************************************************************}
  1913. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  1914. var
  1915. oldtyp : byte;
  1916. begin
  1917. oldtyp:=ppufile.entrytyp;
  1918. ppufile.entrytyp:=subentryid;
  1919. inherited ppuwrite(ppufile);
  1920. ppufile.entrytyp:=oldtyp;
  1921. end;
  1922. {****************************************************************************
  1923. TAbstractLocalSymtable
  1924. ****************************************************************************}
  1925. function tabstractlocalsymtable.count_locals:longint;
  1926. var
  1927. i : longint;
  1928. sym : tsym;
  1929. begin
  1930. result:=0;
  1931. for i:=0 to SymList.Count-1 do
  1932. begin
  1933. sym:=tsym(SymList[i]);
  1934. { Count only varsyms, but ignore the funcretsym }
  1935. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1936. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1937. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1938. (tstoredsym(sym).refs>0)) then
  1939. inc(result);
  1940. end;
  1941. end;
  1942. function tabstractlocalsymtable.iscurrentunit: boolean;
  1943. begin
  1944. Result:=
  1945. assigned(defowner) and
  1946. defowner.owner.iscurrentunit;
  1947. end;
  1948. {****************************************************************************
  1949. TLocalSymtable
  1950. ****************************************************************************}
  1951. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1952. begin
  1953. inherited create('');
  1954. defowner:=adefowner;
  1955. symtabletype:=localsymtable;
  1956. symtablelevel:=level;
  1957. end;
  1958. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1959. var
  1960. hsym : tsym;
  1961. begin
  1962. if not assigned(defowner) or
  1963. (defowner.typ<>procdef) then
  1964. internalerror(200602042);
  1965. result:=false;
  1966. hsym:=tsym(FindWithHash(hashedid));
  1967. if assigned(hsym) then
  1968. begin
  1969. { a local and the function can have the same
  1970. name in TP and Delphi, but RESULT not }
  1971. if (m_duplicate_names in current_settings.modeswitches) and
  1972. (hsym.typ in [absolutevarsym,localvarsym]) and
  1973. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1974. not((m_result in current_settings.modeswitches) and
  1975. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1976. HideSym(hsym)
  1977. else
  1978. DuplicateSym(hashedid,sym,hsym,false);
  1979. result:=true;
  1980. exit;
  1981. end;
  1982. { check also parasymtable, this needs to be done here because
  1983. of the special situation with the funcret sym that needs to be
  1984. hidden for tp and delphi modes }
  1985. hsym:=tsym(tabstractprocdef(defowner).parast.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. (sym.typ in [absolutevarsym,localvarsym]) and
  1992. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1993. not((m_result in current_settings.modeswitches) and
  1994. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1995. Hidesym(sym)
  1996. else
  1997. DuplicateSym(hashedid,sym,hsym,false);
  1998. result:=true;
  1999. exit;
  2000. end;
  2001. { check ObjectSymtable, skip this for funcret sym because
  2002. that will always be positive because it has the same name
  2003. as the procsym }
  2004. if not is_funcret_sym(sym) and
  2005. (defowner.typ=procdef) and
  2006. assigned(tprocdef(defowner).struct) and
  2007. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2008. (
  2009. not(m_delphi in current_settings.modeswitches) or
  2010. is_object(tprocdef(defowner).struct)
  2011. ) then
  2012. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2013. end;
  2014. {****************************************************************************
  2015. TParaSymtable
  2016. ****************************************************************************}
  2017. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2018. begin
  2019. inherited create('');
  2020. readonly:=false;
  2021. defowner:=adefowner;
  2022. symtabletype:=parasymtable;
  2023. symtablelevel:=level;
  2024. end;
  2025. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2026. begin
  2027. result:=inherited checkduplicate(hashedid,sym);
  2028. if result then
  2029. exit;
  2030. if not(m_duplicate_names in current_settings.modeswitches) and
  2031. assigned(defowner) and (defowner.typ=procdef) and
  2032. assigned(tprocdef(defowner).struct) and
  2033. assigned(tprocdef(defowner).owner) and
  2034. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2035. (
  2036. not(m_delphi in current_settings.modeswitches) or
  2037. is_object(tprocdef(defowner).struct)
  2038. ) then
  2039. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2040. end;
  2041. procedure tparasymtable.insertdef(def: TDefEntry);
  2042. begin
  2043. if readonly then
  2044. defowner.owner.insertdef(def)
  2045. else
  2046. inherited insertdef(def);
  2047. end;
  2048. {****************************************************************************
  2049. TAbstractUniTSymtable
  2050. ****************************************************************************}
  2051. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2052. begin
  2053. inherited create(n);
  2054. moduleid:=id;
  2055. end;
  2056. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2057. var
  2058. hsym : tsym;
  2059. begin
  2060. result:=false;
  2061. hsym:=tsym(FindWithHash(hashedid));
  2062. if assigned(hsym) then
  2063. begin
  2064. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2065. exit;
  2066. if hsym.typ=symconst.namespacesym then
  2067. begin
  2068. case sym.typ of
  2069. symconst.namespacesym:;
  2070. symconst.unitsym:
  2071. begin
  2072. 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 }
  2073. tnamespacesym(hsym).unitsym:=tsym(sym);
  2074. end
  2075. else
  2076. HideSym(hsym);
  2077. end;
  2078. end
  2079. else
  2080. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2081. unit, the unit can then not be accessed anymore using
  2082. <unit>.<id>, so we can hide the symbol.
  2083. Do the same if we add a namespace and there is a unit with the same name }
  2084. if (hsym.typ=symconst.unitsym) and
  2085. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2086. begin
  2087. HideSym(hsym);
  2088. if sym.typ=symconst.namespacesym then
  2089. tnamespacesym(sym).unitsym:=tsym(hsym);
  2090. end
  2091. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2092. in this case, copy the isoindex and make the original symbol invisible }
  2093. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2094. and (tprogramparasym(hsym).isoindex<>0) then
  2095. begin
  2096. HideSym(hsym);
  2097. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2098. end
  2099. else
  2100. DuplicateSym(hashedid,sym,hsym,false);
  2101. result:=true;
  2102. exit;
  2103. end;
  2104. end;
  2105. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2106. begin
  2107. result:=find(n);
  2108. if assigned(result)and(result.typ<>namespacesym)then
  2109. result:=nil;
  2110. end;
  2111. function tabstractuniTSymtable.iscurrentunit:boolean;
  2112. begin
  2113. result:=assigned(current_module) and
  2114. (
  2115. (current_module.globalsymtable=self) or
  2116. (current_module.localsymtable=self)
  2117. );
  2118. end;
  2119. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2120. var
  2121. p:integer;
  2122. n,ns:string;
  2123. oldsym:TSymEntry;
  2124. begin
  2125. insert(sym);
  2126. n:=sym.realname;
  2127. p:=pos('.',n);
  2128. ns:='';
  2129. while p>0 do
  2130. begin
  2131. if ns='' then
  2132. ns:=copy(n,1,p-1)
  2133. else
  2134. ns:=ns+'.'+copy(n,1,p-1);
  2135. system.delete(n,1,p);
  2136. oldsym:=findnamespace(upper(ns));
  2137. if not assigned(oldsym) then
  2138. insert(cnamespacesym.create(ns));
  2139. p:=pos('.',n);
  2140. end;
  2141. end;
  2142. {****************************************************************************
  2143. TStaticSymtable
  2144. ****************************************************************************}
  2145. constructor tstaticsymtable.create(const n : string;id:word);
  2146. begin
  2147. inherited create(n,id);
  2148. symtabletype:=staticsymtable;
  2149. symtablelevel:=main_program_level;
  2150. currentvisibility:=vis_private;
  2151. end;
  2152. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2153. begin
  2154. inherited ppuload(ppufile);
  2155. { now we can deref the syms and defs }
  2156. deref(false);
  2157. end;
  2158. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2159. begin
  2160. inherited ppuwrite(ppufile);
  2161. end;
  2162. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2163. begin
  2164. result:=inherited checkduplicate(hashedid,sym);
  2165. if not result and
  2166. (current_module.localsymtable=self) and
  2167. assigned(current_module.globalsymtable) then
  2168. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2169. end;
  2170. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2171. begin
  2172. result:=inherited findnamespace(n);
  2173. if not assigned(result) and
  2174. (current_module.localsymtable=self) and
  2175. assigned(current_module.globalsymtable) then
  2176. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2177. end;
  2178. {****************************************************************************
  2179. TGlobalSymtable
  2180. ****************************************************************************}
  2181. constructor tglobalsymtable.create(const n : string;id:word);
  2182. begin
  2183. inherited create(n,id);
  2184. symtabletype:=globalsymtable;
  2185. symtablelevel:=main_program_level;
  2186. end;
  2187. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2188. begin
  2189. inherited ppuload(ppufile);
  2190. { now we can deref the syms and defs }
  2191. deref(false);
  2192. end;
  2193. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2194. begin
  2195. { write the symtable entries }
  2196. inherited ppuwrite(ppufile);
  2197. end;
  2198. {*****************************************************************************
  2199. tspecializesymtable
  2200. *****************************************************************************}
  2201. constructor tspecializesymtable.create(const n : string;id:word);
  2202. begin
  2203. inherited create(n,id);
  2204. { the specialize symtable does not own the syms and defs as they are all
  2205. moved to a different symtable before the symtable is destroyed; this
  2206. avoids calls to "extract" }
  2207. symlist.ownsobjects:=false;
  2208. deflist.ownsobjects:=false;
  2209. end;
  2210. function tspecializesymtable.iscurrentunit: boolean;
  2211. begin
  2212. Result:=true;
  2213. end;
  2214. {****************************************************************************
  2215. TWITHSYMTABLE
  2216. ****************************************************************************}
  2217. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2218. begin
  2219. inherited create('');
  2220. symtabletype:=withsymtable;
  2221. withrefnode:=refnode;
  2222. { Replace SymList with the passed symlist }
  2223. SymList.free;
  2224. SymList:=ASymList;
  2225. defowner:=aowner;
  2226. end;
  2227. destructor twithsymtable.destroy;
  2228. begin
  2229. withrefnode.free;
  2230. { Disable SymList because we don't Own it }
  2231. SymList:=nil;
  2232. inherited destroy;
  2233. end;
  2234. procedure twithsymtable.clear;
  2235. begin
  2236. { remove no entry from a withsymtable as it is only a pointer to the
  2237. recorddef or objectdef symtable }
  2238. end;
  2239. procedure twithsymtable.insertdef(def:TDefEntry);
  2240. begin
  2241. { Definitions can't be registered in the withsymtable
  2242. because the withsymtable is removed after the with block.
  2243. We can't easily solve it here because the next symtable in the
  2244. stack is not known. }
  2245. internalerror(200602046);
  2246. end;
  2247. {****************************************************************************
  2248. TSTT_ExceptionSymtable
  2249. ****************************************************************************}
  2250. constructor tstt_excepTSymtable.create;
  2251. begin
  2252. inherited create('');
  2253. symtabletype:=stt_excepTSymtable;
  2254. end;
  2255. {****************************************************************************
  2256. TMacroSymtable
  2257. ****************************************************************************}
  2258. constructor tmacrosymtable.create(exported: boolean);
  2259. begin
  2260. inherited create('');
  2261. if exported then
  2262. symtabletype:=exportedmacrosymtable
  2263. else
  2264. symtabletype:=localmacrosymtable;
  2265. symtablelevel:=main_program_level;
  2266. end;
  2267. {****************************************************************************
  2268. TEnumSymtable
  2269. ****************************************************************************}
  2270. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  2271. var
  2272. value: longint;
  2273. def: tenumdef;
  2274. begin
  2275. // defowner = nil only when we are loading from ppu
  2276. if defowner<>nil then
  2277. begin
  2278. { First entry? Then we need to set the minval }
  2279. value:=tenumsym(sym).value;
  2280. def:=tenumdef(defowner);
  2281. if SymList.count=0 then
  2282. begin
  2283. if value>0 then
  2284. def.has_jumps:=true;
  2285. def.setmin(value);
  2286. def.setmax(value);
  2287. end
  2288. else
  2289. begin
  2290. { check for jumps }
  2291. if value>def.max+1 then
  2292. def.has_jumps:=true;
  2293. { update low and high }
  2294. if def.min>value then
  2295. def.setmin(value);
  2296. if def.max<value then
  2297. def.setmax(value);
  2298. end;
  2299. end;
  2300. inherited insert(sym, checkdup);
  2301. end;
  2302. constructor tenumsymtable.create(adefowner: tdef);
  2303. begin
  2304. inherited Create('');
  2305. symtabletype:=enumsymtable;
  2306. defowner:=adefowner;
  2307. end;
  2308. {****************************************************************************
  2309. TArraySymtable
  2310. ****************************************************************************}
  2311. procedure tarraysymtable.insertdef(def: TDefEntry);
  2312. begin
  2313. { Enums must also be available outside the record scope,
  2314. insert in the owner of this symtable }
  2315. if def.typ=enumdef then
  2316. defowner.owner.insertdef(def)
  2317. else
  2318. inherited insertdef(def);
  2319. end;
  2320. constructor tarraysymtable.create(adefowner: tdef);
  2321. begin
  2322. inherited Create('');
  2323. symtabletype:=arraysymtable;
  2324. defowner:=adefowner;
  2325. end;
  2326. {*****************************************************************************
  2327. Helper Routines
  2328. *****************************************************************************}
  2329. function FullTypeName(def,otherdef:tdef):string;
  2330. var
  2331. s1,s2 : string;
  2332. begin
  2333. if def.typ in [objectdef,recorddef] then
  2334. s1:=tabstractrecorddef(def).RttiName
  2335. else
  2336. s1:=def.typename;
  2337. { When the names are the same try to include the unit name }
  2338. if assigned(otherdef) and
  2339. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2340. begin
  2341. s2:=otherdef.typename;
  2342. if upper(s1)=upper(s2) then
  2343. s1:=def.owner.realname^+'.'+s1;
  2344. end;
  2345. FullTypeName:=s1;
  2346. end;
  2347. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  2348. begin
  2349. result:='';
  2350. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2351. begin
  2352. if (result='') then
  2353. if symtable.name<>nil then
  2354. result:=symtable.name^
  2355. else
  2356. else
  2357. if symtable.name<>nil then
  2358. result:=symtable.name^+delimiter+result
  2359. else
  2360. result:=delimiter+result;
  2361. symtable:=symtable.defowner.owner;
  2362. end;
  2363. end;
  2364. function generate_objectpascal_helper_key(def:tdef):string;
  2365. begin
  2366. if not assigned(def) then
  2367. internalerror(2013020501);
  2368. if def.typ in [recorddef,objectdef] then
  2369. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2370. else
  2371. result:=make_mangledname('',def.owner,def.typesym.name);
  2372. end;
  2373. procedure incompatibletypes(def1,def2:tdef);
  2374. begin
  2375. { When there is an errordef there is already an error message show }
  2376. if (def2.typ=errordef) or
  2377. (def1.typ=errordef) then
  2378. exit;
  2379. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2380. end;
  2381. procedure hidesym(sym:TSymEntry);
  2382. begin
  2383. sym.realname:='$hidden'+sym.realname;
  2384. tsym(sym).visibility:=vis_hidden;
  2385. end;
  2386. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2387. var
  2388. st : TSymtable;
  2389. filename : TIDString;
  2390. begin
  2391. if not warn then
  2392. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2393. else
  2394. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2395. { Write hint where the original symbol was found }
  2396. st:=finduniTSymtable(origsym.owner);
  2397. with tsym(origsym).fileinfo do
  2398. begin
  2399. if assigned(st) and
  2400. (st.symtabletype=globalsymtable) and
  2401. st.iscurrentunit then
  2402. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2403. else if assigned(st.name) then
  2404. begin
  2405. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2406. if filename<>'' then
  2407. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2408. else
  2409. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2410. end;
  2411. end;
  2412. { Rename duplicate sym to an unreachable name, but it can be
  2413. inserted in the symtable without errors }
  2414. inc(dupnr);
  2415. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2416. if assigned(dupsym) then
  2417. include(tsym(dupsym).symoptions,sp_implicitrename);
  2418. end;
  2419. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2420. begin
  2421. result:=false;
  2422. if not assigned(sym) or not (sym is tstoredsym) then
  2423. Internalerror(2011081101);
  2424. { For generics a dummy symbol without the parameter count is created
  2425. if such a symbol not yet exists so that different parts of the
  2426. parser can find that symbol. If that symbol is still a
  2427. undefineddef we replace the generic dummy symbol's
  2428. name with a "dup" name and use the new symbol as the generic dummy
  2429. symbol }
  2430. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2431. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2432. (m_delphi in current_settings.modeswitches) then
  2433. begin
  2434. inc(dupnr);
  2435. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2436. include(tsym(sym).symoptions,sp_implicitrename);
  2437. { we need to find the new symbol now if checking for a dummy }
  2438. include(symoptions,sp_generic_dummy);
  2439. result:=true;
  2440. end;
  2441. end;
  2442. {*****************************************************************************
  2443. Search
  2444. *****************************************************************************}
  2445. procedure addsymref(sym:tsym);
  2446. var
  2447. owner: tsymtable;
  2448. begin
  2449. { symbol uses count }
  2450. sym.IncRefCount;
  2451. { unit uses count }
  2452. owner:=sym.owner;
  2453. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2454. owner:=tdef(owner.defowner).owner;
  2455. if assigned(current_module) and
  2456. (owner.symtabletype=globalsymtable) then
  2457. begin
  2458. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2459. internalerror(200501152);
  2460. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2461. end;
  2462. end;
  2463. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2464. begin
  2465. result:=nesteddef=ownerdef;
  2466. if not result and
  2467. { types declared locally in a record method are not defined in the
  2468. record itself }
  2469. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2470. assigned(nesteddef.owner.defowner) then
  2471. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2472. end;
  2473. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2474. begin
  2475. result:=assigned(childsym) and (childsym.owner=symtable);
  2476. if not result and assigned(childsym) and
  2477. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2478. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2479. end;
  2480. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2481. begin
  2482. result:=false;
  2483. if not assigned(def1) or not assigned(def2) then
  2484. exit;
  2485. { for both defs walk to the topmost generic }
  2486. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2487. def1:=tdef(def1.owner.defowner);
  2488. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2489. def2:=tdef(def2.owner.defowner);
  2490. result:=def1=def2;
  2491. end;
  2492. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2493. var
  2494. uname : string;
  2495. begin
  2496. { TODO : check regarding arrays and records declared as their type }
  2497. if not (def.typ in [recorddef,objectdef]) then
  2498. internalerror(2012051501);
  2499. uname:=upper(srsym.realname);
  2500. repeat
  2501. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2502. begin
  2503. result:=def;
  2504. exit;
  2505. end;
  2506. def:=tdef(def.owner.defowner);
  2507. until not (def.typ in [recorddef,objectdef]);
  2508. result:=nil;
  2509. end;
  2510. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2511. begin
  2512. { TODO : check regarding arrays and records declared as their type }
  2513. if not (nesteddef.typ in [recorddef,objectdef]) then
  2514. internalerror(2012051601);
  2515. repeat
  2516. if tstoreddef(nesteddef).genericdef=genericdef then
  2517. begin
  2518. resultdef:=nesteddef;
  2519. result:=true;
  2520. exit;
  2521. end;
  2522. nesteddef:=tdef(nesteddef.owner.defowner);
  2523. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2524. resultdef:=nil;
  2525. result:=false;
  2526. end;
  2527. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2528. symvisibility: visibility of the symbol
  2529. contextobjdef: via which def the symbol is accessed, e.g.:
  2530. fieldname:=1 -> contextobjdef = current_structdef
  2531. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2532. }
  2533. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2534. var
  2535. symownerdef : tabstractrecorddef;
  2536. nonlocalst : tsymtable;
  2537. isspezproc : boolean;
  2538. begin
  2539. result:=false;
  2540. { Get objdectdef owner of the symtable for the is_related checks }
  2541. if not assigned(symst) or
  2542. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2543. internalerror(200810285);
  2544. symownerdef:=tabstractrecorddef(symst.defowner);
  2545. { specializations might belong to a localsymtable or parasymtable }
  2546. nonlocalst:=symownerdef.owner;
  2547. if tstoreddef(symst.defowner).is_specialization then
  2548. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2549. nonlocalst:=nonlocalst.defowner.owner;
  2550. isspezproc:=false;
  2551. if assigned(current_procinfo) then
  2552. begin
  2553. if current_procinfo.procdef.is_specialization and
  2554. assigned(current_procinfo.procdef.struct) then
  2555. isspezproc:=true;
  2556. end;
  2557. case symvisibility of
  2558. vis_private :
  2559. begin
  2560. { private symbols are allowed when we are in the same
  2561. module as they are defined }
  2562. result:=(
  2563. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2564. (nonlocalst.iscurrentunit)
  2565. ) or
  2566. ( // the case of specialize inside the generic declaration and nested types
  2567. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2568. (
  2569. assigned(current_structdef) and
  2570. (
  2571. (current_structdef=symownerdef) or
  2572. (current_structdef.owner.iscurrentunit)
  2573. )
  2574. ) or
  2575. (
  2576. not assigned(current_structdef) and
  2577. (symownerdef.owner.iscurrentunit)
  2578. ) or
  2579. { access from a generic method that belongs to the class
  2580. but that is specialized elsewere }
  2581. (
  2582. isspezproc and
  2583. (current_procinfo.procdef.struct=current_structdef)
  2584. )
  2585. );
  2586. end;
  2587. vis_strictprivate :
  2588. begin
  2589. result:=assigned(current_structdef) and
  2590. is_owned_by(current_structdef,symownerdef);
  2591. end;
  2592. vis_strictprotected :
  2593. begin
  2594. result:=(
  2595. { access from nested class }
  2596. assigned(current_structdef) and
  2597. is_owned_by(current_structdef,symownerdef)
  2598. ) or
  2599. (
  2600. { access from child class }
  2601. assigned(contextobjdef) and
  2602. assigned(current_structdef) and
  2603. def_is_related(contextobjdef,symownerdef) and
  2604. def_is_related(current_structdef,contextobjdef)
  2605. ) or
  2606. (
  2607. { helpers can access strict protected symbols }
  2608. is_objectpascal_helper(contextobjdef) and
  2609. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2610. ) or
  2611. (
  2612. { same as above, but from context of call node inside
  2613. helper method }
  2614. is_objectpascal_helper(current_structdef) and
  2615. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2616. );
  2617. end;
  2618. vis_protected :
  2619. begin
  2620. { protected symbols are visible in the module that defines them and
  2621. also visible to related objects. The related object must be defined
  2622. in the current module }
  2623. result:=(
  2624. (
  2625. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2626. (nonlocalst.iscurrentunit)
  2627. ) or
  2628. (
  2629. assigned(contextobjdef) and
  2630. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  2631. (contextobjdef.owner.iscurrentunit) and
  2632. def_is_related(contextobjdef,symownerdef)
  2633. ) or
  2634. ( // the case of specialize inside the generic declaration and nested types
  2635. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2636. (
  2637. assigned(current_structdef) and
  2638. (
  2639. (current_structdef=symownerdef) or
  2640. (current_structdef.owner.iscurrentunit)
  2641. )
  2642. ) or
  2643. (
  2644. not assigned(current_structdef) and
  2645. (symownerdef.owner.iscurrentunit)
  2646. ) or
  2647. (
  2648. { helpers can access protected symbols }
  2649. is_objectpascal_helper(contextobjdef) and
  2650. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2651. )
  2652. ) or
  2653. { access from a generic method that belongs to the class
  2654. but that is specialized elsewere }
  2655. (
  2656. isspezproc and
  2657. (current_procinfo.procdef.struct=current_structdef)
  2658. )
  2659. );
  2660. end;
  2661. vis_public,
  2662. vis_published :
  2663. result:=true;
  2664. end;
  2665. end;
  2666. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2667. begin
  2668. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2669. end;
  2670. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2671. var
  2672. i : longint;
  2673. pd : tprocdef;
  2674. begin
  2675. if sym.typ=procsym then
  2676. begin
  2677. { A procsym is visible, when there is at least one of the procdefs visible }
  2678. result:=false;
  2679. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2680. begin
  2681. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2682. if (pd.owner=sym.owner) and
  2683. is_visible_for_object(pd,contextobjdef) then
  2684. begin
  2685. result:=true;
  2686. exit;
  2687. end;
  2688. end;
  2689. end
  2690. else
  2691. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2692. end;
  2693. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2694. begin
  2695. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2696. end;
  2697. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2698. begin
  2699. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2700. end;
  2701. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2702. var
  2703. hashedid: THashedIDString;
  2704. contextstructdef: tabstractrecorddef;
  2705. stackitem: psymtablestackitem;
  2706. begin
  2707. result:=false;
  2708. hashedid.id:=s;
  2709. stackitem:=symtablestack.stack;
  2710. while assigned(stackitem) do
  2711. begin
  2712. srsymtable:=stackitem^.symtable;
  2713. if (srsymtable.symtabletype=objectsymtable) then
  2714. begin
  2715. { TODO : implement the search for an option in classes as well }
  2716. if ssf_search_option in flags then
  2717. begin
  2718. result:=false;
  2719. exit;
  2720. end;
  2721. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  2722. begin
  2723. result:=true;
  2724. exit;
  2725. end;
  2726. end
  2727. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2728. (srsymtable.defowner.typ=undefineddef)) then
  2729. begin
  2730. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2731. { First check if it is a unit/namespace symbol.
  2732. They are visible only if they are from the current unit or
  2733. unit of generic of currently processed specialization. }
  2734. if assigned(srsym) and
  2735. (
  2736. not(srsym.typ in [unitsym,namespacesym]) or
  2737. srsymtable.iscurrentunit or
  2738. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2739. ) and
  2740. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  2741. begin
  2742. { use the class from withsymtable only when it is
  2743. defined in this unit }
  2744. if (srsymtable.symtabletype=withsymtable) and
  2745. assigned(srsymtable.defowner) and
  2746. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2747. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2748. (srsymtable.defowner.owner.iscurrentunit) then
  2749. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2750. else
  2751. contextstructdef:=current_structdef;
  2752. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2753. is_visible_for_object(srsym,contextstructdef) then
  2754. begin
  2755. { we need to know if a procedure references symbols
  2756. in the static symtable, because then it can't be
  2757. inlined from outside this unit }
  2758. if assigned(current_procinfo) and
  2759. (srsym.owner.symtabletype=staticsymtable) then
  2760. include(current_procinfo.flags,pi_uses_static_symtable);
  2761. if not (ssf_no_addsymref in flags) then
  2762. addsymref(srsym);
  2763. result:=true;
  2764. exit;
  2765. end;
  2766. end;
  2767. end;
  2768. stackitem:=stackitem^.next;
  2769. end;
  2770. srsym:=nil;
  2771. srsymtable:=nil;
  2772. end;
  2773. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2774. srsymtable:TSymtable;option:tsymoption):boolean;
  2775. begin
  2776. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  2777. end;
  2778. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2779. var
  2780. hashedid : THashedIDString;
  2781. stackitem : psymtablestackitem;
  2782. classh : tobjectdef;
  2783. begin
  2784. result:=false;
  2785. hashedid.id:=s;
  2786. stackitem:=symtablestack.stack;
  2787. while assigned(stackitem) do
  2788. begin
  2789. {
  2790. It is not possible to have type symbols in:
  2791. parameters
  2792. Exception are classes, objects, records, generic definitions and specializations
  2793. that have the parameterized types inserted in the symtable.
  2794. }
  2795. srsymtable:=stackitem^.symtable;
  2796. if (srsymtable.symtabletype=ObjectSymtable) then
  2797. begin
  2798. classh:=tobjectdef(srsymtable.defowner);
  2799. while assigned(classh) do
  2800. begin
  2801. srsymtable:=classh.symtable;
  2802. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2803. if assigned(srsym) and
  2804. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2805. is_visible_for_object(srsym,current_structdef) then
  2806. begin
  2807. addsymref(srsym);
  2808. result:=true;
  2809. exit;
  2810. end;
  2811. classh:=classh.childof;
  2812. end;
  2813. end
  2814. else
  2815. begin
  2816. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2817. if assigned(srsym) and
  2818. (
  2819. not(srsym.typ in [unitsym,namespacesym]) or
  2820. srsymtable.iscurrentunit or
  2821. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  2822. ) and
  2823. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2824. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2825. begin
  2826. { we need to know if a procedure references symbols
  2827. in the static symtable, because then it can't be
  2828. inlined from outside this unit }
  2829. if assigned(current_procinfo) and
  2830. (srsym.owner.symtabletype=staticsymtable) then
  2831. include(current_procinfo.flags,pi_uses_static_symtable);
  2832. addsymref(srsym);
  2833. result:=true;
  2834. exit;
  2835. end;
  2836. end;
  2837. stackitem:=stackitem^.next;
  2838. end;
  2839. result:=false;
  2840. srsym:=nil;
  2841. srsymtable:=nil;
  2842. end;
  2843. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2844. var
  2845. pmod : tmodule;
  2846. begin
  2847. pmod:=tmodule(pm);
  2848. result:=false;
  2849. if assigned(pmod.globalsymtable) then
  2850. begin
  2851. srsym:=tsym(pmod.globalsymtable.Find(s));
  2852. if assigned(srsym) then
  2853. begin
  2854. srsymtable:=pmod.globalsymtable;
  2855. addsymref(srsym);
  2856. result:=true;
  2857. exit;
  2858. end;
  2859. end;
  2860. { If the module is the current unit we also need
  2861. to search the local symtable }
  2862. if (pmod=current_module) and
  2863. assigned(pmod.localsymtable) then
  2864. begin
  2865. srsym:=tsym(pmod.localsymtable.Find(s));
  2866. if assigned(srsym) then
  2867. begin
  2868. srsymtable:=pmod.localsymtable;
  2869. addsymref(srsym);
  2870. result:=true;
  2871. exit;
  2872. end;
  2873. end;
  2874. srsym:=nil;
  2875. srsymtable:=nil;
  2876. end;
  2877. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2878. var
  2879. stackitem : psymtablestackitem;
  2880. begin
  2881. result:=false;
  2882. stackitem:=symtablestack.stack;
  2883. while assigned(stackitem) do
  2884. begin
  2885. srsymtable:=stackitem^.symtable;
  2886. if (srsymtable.symtabletype=globalsymtable) and
  2887. (srsymtable.name^=unitname) then
  2888. begin
  2889. srsym:=tsym(srsymtable.find(symname));
  2890. if not assigned(srsym) then
  2891. break;
  2892. result:=true;
  2893. exit;
  2894. end;
  2895. stackitem:=stackitem^.next;
  2896. end;
  2897. { If the module is the current unit we also need
  2898. to search the local symtable }
  2899. if assigned(current_module.localsymtable) and
  2900. (current_module.localsymtable.name^=unitname) then
  2901. begin
  2902. srsymtable:=current_module.localsymtable;
  2903. srsym:=tsym(srsymtable.find(symname));
  2904. if assigned(srsym) then
  2905. begin
  2906. result:=true;
  2907. exit;
  2908. end;
  2909. end;
  2910. end;
  2911. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2912. begin
  2913. result:=pd;
  2914. if pd.typ<>objectdef then
  2915. exit;
  2916. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2917. end;
  2918. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2919. var
  2920. hashedid : THashedIDString;
  2921. stackitem : psymtablestackitem;
  2922. srsymtable : tsymtable;
  2923. srsym : tsym;
  2924. formalname,
  2925. foundname : shortstring;
  2926. formalnameptr,
  2927. foundnameptr: pshortstring;
  2928. begin
  2929. { not a formal definition -> return it }
  2930. if not(oo_is_formal in pd.objectoptions) then
  2931. begin
  2932. result:=pd;
  2933. exit;
  2934. end;
  2935. hashedid.id:=pd.typesym.name;
  2936. stackitem:=symtablestack.stack;
  2937. while assigned(stackitem) do
  2938. begin
  2939. srsymtable:=stackitem^.symtable;
  2940. { ObjC classes can't appear in generics or as nested class
  2941. definitions. Java classes can. }
  2942. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2943. (is_java_class_or_interface(pd) and
  2944. (srsymtable.symtabletype=ObjectSymtable)) then
  2945. begin
  2946. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2947. if assigned(srsym) and
  2948. (srsym.typ=typesym) and
  2949. (ttypesym(srsym).typedef.typ=objectdef) and
  2950. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2951. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2952. begin
  2953. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2954. begin
  2955. { the external name for the formal and the real
  2956. definition must match }
  2957. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2958. assigned(pd.import_lib) then
  2959. begin
  2960. if assigned(pd.import_lib) then
  2961. formalname:=pd.import_lib^+'.'
  2962. else
  2963. formalname:='';
  2964. formalname:=formalname+pd.objextname^;
  2965. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2966. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2967. else
  2968. foundname:='';
  2969. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  2970. formalnameptr:=@formalname;
  2971. foundnameptr:=@foundname;
  2972. end
  2973. else
  2974. begin
  2975. formalnameptr:=pd.objextname;
  2976. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  2977. end;
  2978. if foundnameptr^<>formalnameptr^ then
  2979. begin
  2980. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  2981. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  2982. end;
  2983. end;
  2984. result:=tobjectdef(ttypesym(srsym).typedef);
  2985. if assigned(current_procinfo) and
  2986. (srsym.owner.symtabletype=staticsymtable) then
  2987. include(current_procinfo.flags,pi_uses_static_symtable);
  2988. addsymref(srsym);
  2989. exit;
  2990. end;
  2991. end;
  2992. stackitem:=stackitem^.next;
  2993. end;
  2994. { nothing found: optionally give an error and return the original
  2995. (empty) one }
  2996. if erroronfailure then
  2997. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  2998. result:=pd;
  2999. end;
  3000. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3001. var
  3002. hashedid : THashedIDString;
  3003. orgclass : tobjectdef;
  3004. i : longint;
  3005. hlpsrsym : tsym;
  3006. hlpsrsymtable : tsymtable;
  3007. begin
  3008. orgclass:=classh;
  3009. { in case this is a formal class, first find the real definition }
  3010. if assigned(classh) then
  3011. begin
  3012. if (oo_is_formal in classh.objectoptions) then
  3013. classh:=find_real_class_definition(classh,true);
  3014. { The contextclassh is used for visibility. The classh must be equal to
  3015. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3016. parent or a class helper. }
  3017. if not (def_is_related(contextclassh,classh) or
  3018. (is_classhelper(contextclassh) and
  3019. assigned(tobjectdef(contextclassh).extendeddef) and
  3020. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3021. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3022. internalerror(200811161);
  3023. end;
  3024. result:=false;
  3025. hashedid.id:=s;
  3026. { an Objective-C protocol or Java interface can inherit from multiple
  3027. other protocols/interfaces -> use ImplementedInterfaces instead }
  3028. if is_objcprotocol(classh) or
  3029. is_javainterface(classh) then
  3030. begin
  3031. srsymtable:=classh.symtable;
  3032. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3033. if assigned(srsym) and
  3034. is_visible_for_object(srsym,contextclassh) then
  3035. begin
  3036. if not (ssf_no_addsymref in flags) then
  3037. addsymref(srsym);
  3038. result:=true;
  3039. exit;
  3040. end;
  3041. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3042. begin
  3043. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3044. begin
  3045. result:=true;
  3046. exit;
  3047. end;
  3048. end;
  3049. end
  3050. else
  3051. if is_objectpascal_helper(classh) then
  3052. begin
  3053. { helpers have their own obscure search logic... }
  3054. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3055. if result then
  3056. exit;
  3057. end
  3058. else
  3059. begin
  3060. hlpsrsym:=nil;
  3061. hlpsrsymtable:=nil;
  3062. while assigned(classh) do
  3063. begin
  3064. { search for a class helper method first if this is an Object
  3065. Pascal class and we haven't yet found a helper symbol }
  3066. if is_class(classh) and
  3067. (ssf_search_helper in flags) and
  3068. not assigned(hlpsrsym) then
  3069. begin
  3070. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3071. if result then
  3072. { if the procsym is overloaded we need to use the
  3073. "original" symbol; the helper symbol will be found when
  3074. searching for overloads }
  3075. if (srsym.typ<>procsym) or
  3076. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  3077. exit
  3078. else
  3079. begin
  3080. { remember the found symbol if the class hierarchy
  3081. should not contain the a method with that name }
  3082. hlpsrsym:=srsym;
  3083. hlpsrsymtable:=srsymtable;
  3084. end;
  3085. end;
  3086. srsymtable:=classh.symtable;
  3087. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3088. if assigned(srsym) and
  3089. is_visible_for_object(srsym,contextclassh) then
  3090. begin
  3091. if not (ssf_no_addsymref in flags) then
  3092. addsymref(srsym);
  3093. result:=true;
  3094. exit;
  3095. end;
  3096. classh:=classh.childof;
  3097. end;
  3098. { did we find a helper symbol, but no symbol with the same name in
  3099. the extended object's hierarchy? }
  3100. if assigned(hlpsrsym) then
  3101. begin
  3102. srsym:=hlpsrsym;
  3103. srsymtable:=hlpsrsymtable;
  3104. result:=true;
  3105. exit;
  3106. end;
  3107. end;
  3108. if is_objcclass(orgclass) then
  3109. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3110. else
  3111. begin
  3112. srsym:=nil;
  3113. srsymtable:=nil;
  3114. end;
  3115. end;
  3116. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3117. var
  3118. hashedid : THashedIDString;
  3119. hlpsrsym : tsym;
  3120. hlpsrsymtable : tsymtable;
  3121. begin
  3122. result:=false;
  3123. hlpsrsym:=nil;
  3124. hlpsrsymtable:=nil;
  3125. hashedid.id:=s;
  3126. { search for a record helper method first }
  3127. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3128. if result then
  3129. { if the procsym is overloaded we need to use the
  3130. "original" symbol; the helper symbol will be found when
  3131. searching for overloads }
  3132. if (srsym.typ<>procsym) or
  3133. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  3134. exit
  3135. else
  3136. begin
  3137. { remember the found symbol if we should not find a symbol with
  3138. the same name in the extended record }
  3139. hlpsrsym:=srsym;
  3140. hlpsrsymtable:=srsymtable;
  3141. end;
  3142. srsymtable:=recordh.symtable;
  3143. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3144. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3145. begin
  3146. addsymref(srsym);
  3147. result:=true;
  3148. exit;
  3149. end;
  3150. srsym:=hlpsrsym;
  3151. srsymtable:=hlpsrsymtable;
  3152. result:=assigned(srsym);
  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 (oo_is_classhelper in defowner.objectoptions) 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. dupnr:=0;
  3958. end;
  3959. procedure DoneSymtable;
  3960. begin
  3961. generrorsym.owner:=nil;
  3962. generrorsym.free;
  3963. generrordef.owner:=nil;
  3964. generrordef.free;
  3965. initialmacrosymtable.free;
  3966. macrosymtablestack.free;
  3967. {$ifdef UNITALIASES}
  3968. unitaliases.free;
  3969. {$endif}
  3970. end;
  3971. end.