symtable.pas 165 KB

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