symtable.pas 164 KB

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