symtable.pas 159 KB

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