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_INC } 'inc',
  376. { _OP_DEC } 'dec');
  377. implementation
  378. uses
  379. { global }
  380. verbose,globals,
  381. { symtable }
  382. symutil,defutil,defcmp,objcdef,
  383. { module }
  384. fmodule,
  385. { codegen }
  386. procinfo,
  387. { ppu }
  388. entfile
  389. ;
  390. var
  391. dupnr : longint; { unique number for duplicate symbols }
  392. {*****************************************************************************
  393. TStoredSymtable
  394. *****************************************************************************}
  395. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  396. begin
  397. inherited insert(sym,checkdup);
  398. end;
  399. procedure tstoredsymtable.delete(sym:TSymEntry);
  400. begin
  401. inherited delete(sym);
  402. end;
  403. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  404. begin
  405. { load the table's flags }
  406. if ppufile.readentry<>ibsymtableoptions then
  407. Message(unit_f_ppu_read_error);
  408. ppufile.getsmallset(tableoptions);
  409. { load definitions }
  410. loaddefs(ppufile);
  411. { load symbols }
  412. loadsyms(ppufile);
  413. init_final_check_done:=true;
  414. end;
  415. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  416. begin
  417. { ensure that we have the sto_needs_init_final flag set if needed }
  418. if not init_final_check_done then
  419. needs_init_final;
  420. { write the table's flags }
  421. ppufile.putsmallset(tableoptions);
  422. ppufile.writeentry(ibsymtableoptions);
  423. { write definitions }
  424. writedefs(ppufile);
  425. { write symbols }
  426. writesyms(ppufile);
  427. end;
  428. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  429. var
  430. def : tdef;
  431. b : byte;
  432. begin
  433. def:=nil;
  434. { load start of definition section, which holds the amount of defs }
  435. if ppufile.readentry<>ibstartdefs then
  436. Message(unit_f_ppu_read_error);
  437. { read definitions }
  438. repeat
  439. b:=ppufile.readentry;
  440. case b of
  441. ibpointerdef : def:=cpointerdef.ppuload(ppufile);
  442. ibarraydef : def:=carraydef.ppuload(ppufile);
  443. iborddef : def:=corddef.ppuload(ppufile);
  444. ibfloatdef : def:=cfloatdef.ppuload(ppufile);
  445. ibprocdef : def:=cprocdef.ppuload(ppufile);
  446. ibshortstringdef : def:=cstringdef.loadshort(ppufile);
  447. iblongstringdef : def:=cstringdef.loadlong(ppufile);
  448. ibansistringdef : def:=cstringdef.loadansi(ppufile);
  449. ibwidestringdef : def:=cstringdef.loadwide(ppufile);
  450. ibunicodestringdef : def:=cstringdef.loadunicode(ppufile);
  451. ibrecorddef : def:=crecorddef.ppuload(ppufile);
  452. ibobjectdef : def:=cobjectdef.ppuload(ppufile);
  453. ibenumdef : def:=cenumdef.ppuload(ppufile);
  454. ibsetdef : def:=csetdef.ppuload(ppufile);
  455. ibprocvardef : def:=cprocvardef.ppuload(ppufile);
  456. ibfiledef : def:=cfiledef.ppuload(ppufile);
  457. ibclassrefdef : def:=cclassrefdef.ppuload(ppufile);
  458. ibformaldef : def:=cformaldef.ppuload(ppufile);
  459. ibvariantdef : def:=cvariantdef.ppuload(ppufile);
  460. ibundefineddef : def:=cundefineddef.ppuload(ppufile);
  461. ibenddefs : break;
  462. ibend : Message(unit_f_ppu_read_error);
  463. else
  464. Message1(unit_f_ppu_invalid_entry,tostr(b));
  465. end;
  466. InsertDef(def);
  467. until false;
  468. end;
  469. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  470. var
  471. b : byte;
  472. sym : tsym;
  473. begin
  474. sym:=nil;
  475. { load start of definition section, which holds the amount of defs }
  476. if ppufile.readentry<>ibstartsyms then
  477. Message(unit_f_ppu_read_error);
  478. { now read the symbols }
  479. repeat
  480. b:=ppufile.readentry;
  481. case b of
  482. ibtypesym : sym:=ctypesym.ppuload(ppufile);
  483. ibprocsym : sym:=cprocsym.ppuload(ppufile);
  484. ibconstsym : sym:=cconstsym.ppuload(ppufile);
  485. ibstaticvarsym : sym:=cstaticvarsym.ppuload(ppufile);
  486. iblocalvarsym : sym:=clocalvarsym.ppuload(ppufile);
  487. ibparavarsym : sym:=cparavarsym.ppuload(ppufile);
  488. ibfieldvarsym : sym:=cfieldvarsym.ppuload(ppufile);
  489. ibabsolutevarsym : sym:=cabsolutevarsym.ppuload(ppufile);
  490. ibenumsym : sym:=cenumsym.ppuload(ppufile);
  491. ibpropertysym : sym:=cpropertysym.ppuload(ppufile);
  492. ibunitsym : sym:=cunitsym.ppuload(ppufile);
  493. iblabelsym : sym:=clabelsym.ppuload(ppufile);
  494. ibsyssym : sym:=csyssym.ppuload(ppufile);
  495. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  496. ibnamespacesym : sym:=cnamespacesym.ppuload(ppufile);
  497. ibendsyms : break;
  498. ibend : Message(unit_f_ppu_read_error);
  499. else
  500. Message1(unit_f_ppu_invalid_entry,tostr(b));
  501. end;
  502. Insert(sym,false);
  503. until false;
  504. end;
  505. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  506. var
  507. defcount,
  508. i : longint;
  509. def : tstoreddef;
  510. begin
  511. defcount:=0;
  512. for i:=0 to DefList.Count-1 do
  513. if tstoreddef(DefList[i]).is_registered then
  514. inc(defcount);
  515. { each definition get a number, write then the amount of defs to the
  516. ibstartdef entry }
  517. ppufile.putlongint(defcount);
  518. ppufile.writeentry(ibstartdefs);
  519. { now write the definition }
  520. for i:=0 to DefList.Count-1 do
  521. begin
  522. def:=tstoreddef(DefList[i]);
  523. if def.is_registered then
  524. def.ppuwrite(ppufile);
  525. end;
  526. { write end of definitions }
  527. ppufile.writeentry(ibenddefs);
  528. end;
  529. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  530. var
  531. symcount,
  532. i : longint;
  533. sym : Tstoredsym;
  534. begin
  535. symcount:=0;
  536. for i:=0 to SymList.Count-1 do
  537. if tstoredsym(SymList[i]).is_registered then
  538. inc(symcount);
  539. { each definition get a number, write then the amount of syms and the
  540. datasize to the ibsymdef entry }
  541. ppufile.putlongint(symcount);
  542. ppufile.writeentry(ibstartsyms);
  543. { foreach is used to write all symbols }
  544. for i:=0 to SymList.Count-1 do
  545. begin
  546. sym:=tstoredsym(SymList[i]);
  547. if sym.is_registered then
  548. sym.ppuwrite(ppufile);
  549. end;
  550. { end of symbols }
  551. ppufile.writeentry(ibendsyms);
  552. end;
  553. procedure tstoredsymtable.buildderef;
  554. var
  555. i : longint;
  556. def : tstoreddef;
  557. sym : tstoredsym;
  558. begin
  559. { interface definitions }
  560. for i:=0 to DefList.Count-1 do
  561. begin
  562. def:=tstoreddef(DefList[i]);
  563. def.buildderef;
  564. end;
  565. { interface symbols }
  566. for i:=0 to SymList.Count-1 do
  567. begin
  568. sym:=tstoredsym(SymList[i]);
  569. sym.buildderef;
  570. end;
  571. end;
  572. procedure tstoredsymtable.buildderefimpl;
  573. var
  574. i : longint;
  575. def : tstoreddef;
  576. begin
  577. { implementation definitions }
  578. for i:=0 to DefList.Count-1 do
  579. begin
  580. def:=tstoreddef(DefList[i]);
  581. def.buildderefimpl;
  582. end;
  583. end;
  584. procedure tstoredsymtable.buildderef_registered;
  585. var
  586. def : tstoreddef;
  587. sym : tstoredsym;
  588. i : longint;
  589. defidmax,
  590. symidmax: longint;
  591. newbuiltdefderefs,
  592. builtdefderefs,
  593. builtsymderefs: array of boolean;
  594. begin
  595. { tdefs for which we already built the deref }
  596. setlength(builtdefderefs,deflist.count);
  597. { tdefs for which we built the deref in this iteration }
  598. setlength(newbuiltdefderefs,deflist.count);
  599. { syms for which we already built the deref }
  600. setlength(builtsymderefs,symlist.count);
  601. repeat
  602. { we only have to store the defs (recursively) referred by wpo info
  603. or inlined routines in the static symbtable }
  604. { current number of registered defs/syms }
  605. defidmax:=current_module.deflist.count;
  606. symidmax:=current_module.symlist.count;
  607. { build the derefs for the registered defs we haven't processed yet }
  608. for i:=0 to DefList.Count-1 do
  609. begin
  610. if not builtdefderefs[i] then
  611. begin
  612. def:=tstoreddef(DefList[i]);
  613. if def.is_registered then
  614. begin
  615. def.buildderef;
  616. newbuiltdefderefs[i]:=true;
  617. builtdefderefs[i]:=true;
  618. end;
  619. end;
  620. end;
  621. { same for the syms }
  622. for i:=0 to SymList.Count-1 do
  623. begin
  624. if not builtsymderefs[i] then
  625. begin
  626. sym:=tstoredsym(SymList[i]);
  627. if sym.is_registered then
  628. begin
  629. sym.buildderef;
  630. builtsymderefs[i]:=true;
  631. end;
  632. end;
  633. end;
  634. { now buildderefimpl for the defs we processed in this iteration }
  635. for i:=0 to DefList.Count-1 do
  636. begin
  637. if newbuiltdefderefs[i] then
  638. begin
  639. newbuiltdefderefs[i]:=false;
  640. tstoreddef(DefList[i]).buildderefimpl;
  641. end;
  642. end;
  643. { stop when no new defs or syms have been registered while processing
  644. the currently registered ones (defs/syms get added to the module's
  645. deflist/symlist when they are registered) }
  646. until
  647. (defidmax=current_module.deflist.count) and
  648. (symidmax=current_module.symlist.count);
  649. end;
  650. procedure tstoredsymtable.deref(only_registered: boolean);
  651. var
  652. i : longint;
  653. def : tstoreddef;
  654. sym : tstoredsym;
  655. begin
  656. { first deref the interface ttype symbols. This is needs
  657. to be done before the interface defs are derefed, because
  658. the interface defs can contain references to the type symbols
  659. which then already need to contain a resolved typedef field (PFV) }
  660. for i:=0 to SymList.Count-1 do
  661. begin
  662. sym:=tstoredsym(SymList[i]);
  663. if (sym.typ=typesym) and
  664. (not only_registered or
  665. sym.is_registered) then
  666. sym.deref;
  667. end;
  668. { interface definitions }
  669. for i:=0 to DefList.Count-1 do
  670. begin
  671. def:=tstoreddef(DefList[i]);
  672. if not only_registered or
  673. def.is_registered then
  674. def.deref;
  675. end;
  676. { interface symbols }
  677. for i:=0 to SymList.Count-1 do
  678. begin
  679. sym:=tstoredsym(SymList[i]);
  680. if (not only_registered or
  681. sym.is_registered) and
  682. (sym.typ<>typesym) then
  683. sym.deref;
  684. end;
  685. end;
  686. procedure tstoredsymtable.derefimpl(only_registered: boolean);
  687. var
  688. i : longint;
  689. def : tstoreddef;
  690. begin
  691. { implementation definitions }
  692. for i:=0 to DefList.Count-1 do
  693. begin
  694. def:=tstoreddef(DefList[i]);
  695. if not only_registered or
  696. def.is_registered then
  697. def.derefimpl;
  698. end;
  699. end;
  700. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  701. var
  702. hsym : tsym;
  703. begin
  704. hsym:=tsym(FindWithHash(hashedid));
  705. if assigned(hsym) then
  706. DuplicateSym(hashedid,sym,hsym,false);
  707. result:=assigned(hsym);
  708. end;
  709. {**************************************
  710. Callbacks
  711. **************************************}
  712. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  713. begin
  714. if tsym(sym).typ=procsym then
  715. tprocsym(sym).check_forward
  716. { check also object method table }
  717. { we needn't to test the def list }
  718. { because each object has to have a type sym,
  719. only test objects declarations, not type renamings }
  720. else
  721. if (tsym(sym).typ=typesym) and
  722. assigned(ttypesym(sym).typedef) and
  723. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  724. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  725. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  726. end;
  727. procedure tstoredsymtable.check_block_valid(def: TObject; arg: pointer);
  728. var
  729. founderrordef: tdef;
  730. begin
  731. { all parameters passed to a block must be handled by the Objective-C
  732. runtime }
  733. if is_block(tdef(def)) and
  734. not objcchecktype(tdef(def),founderrordef) then
  735. if assigned(tdef(def).typesym) then
  736. MessagePos1(tdef(def).typesym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename)
  737. else
  738. Message1(type_e_objc_type_unsupported,tprocvardef(def).typename)
  739. end;
  740. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  741. begin
  742. if (tsym(sym).typ=labelsym) and
  743. not(tlabelsym(sym).defined) then
  744. begin
  745. if tlabelsym(sym).used then
  746. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  747. else
  748. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  749. end;
  750. end;
  751. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  752. begin
  753. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  754. ((tsym(sym).owner.symtabletype in
  755. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  756. begin
  757. { unused symbol should be reported only if no }
  758. { error is reported }
  759. { if the symbol is in a register it is used }
  760. { also don't count the value parameters which have local copies }
  761. { also don't claim for high param of open parameters (PM) }
  762. { also don't complain about unused symbols in generic procedures }
  763. { and methods }
  764. if (Errorcount<>0) or
  765. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  766. (sp_internal in tsym(sym).symoptions) or
  767. ((assigned(tsym(sym).owner.defowner) and
  768. (tsym(sym).owner.defowner.typ=procdef) and
  769. (df_generic in tprocdef(tsym(sym).owner.defowner).defoptions))) then
  770. exit;
  771. if (tstoredsym(sym).refs=0) then
  772. begin
  773. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  774. begin
  775. { don't warn about the result of constructors }
  776. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  777. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  778. not (po_noreturn in tprocdef(tsym(sym).owner.defowner).procoptions) and
  779. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  780. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  781. end
  782. else if (tsym(sym).owner.symtabletype=parasymtable) then
  783. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  784. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  785. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  786. else
  787. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  788. end
  789. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  790. begin
  791. if (tsym(sym).owner.symtabletype=parasymtable) then
  792. begin
  793. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  794. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  795. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  796. end
  797. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  798. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  799. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  800. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  801. end
  802. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  803. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  804. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  805. end
  806. else if ((tsym(sym).owner.symtabletype in
  807. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  808. begin
  809. if (Errorcount<>0) or
  810. (sp_internal in tsym(sym).symoptions) then
  811. exit;
  812. { do not claim for inherited private fields !! }
  813. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  814. case tsym(sym).typ of
  815. typesym:
  816. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  817. constsym:
  818. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  819. propertysym:
  820. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  821. else
  822. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  823. end
  824. { units references are problematic }
  825. else
  826. begin
  827. if (tsym(sym).refs=0) and
  828. not(tsym(sym).typ in [enumsym,unitsym,namespacesym]) and
  829. not(is_funcret_sym(tsym(sym))) and
  830. { don't complain about compiler generated syms for specializations, see also #13405 }
  831. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  832. (pos('$',ttypesym(sym).Realname)<>0)) and
  833. (
  834. (tsym(sym).typ<>procsym) or
  835. ((tsym(sym).owner.symtabletype=staticsymtable) and
  836. not current_module.is_unit)
  837. ) and
  838. { don't complain about alias for hidden _cmd parameter to
  839. obj-c methods }
  840. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  841. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  842. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  843. end;
  844. end;
  845. end;
  846. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  847. begin
  848. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  849. varsymbolused(sym,arg);
  850. end;
  851. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  852. begin
  853. {
  854. Don't test simple object aliases PM
  855. }
  856. if (tsym(sym).typ=typesym) and
  857. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  858. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  859. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  860. end;
  861. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  862. begin
  863. if (tsym(sym).typ=propertysym) and
  864. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  865. ppointer(arg)^:=sym;
  866. end;
  867. {***********************************************
  868. Process all entries
  869. ***********************************************}
  870. { checks, if all procsyms and methods are defined }
  871. procedure tstoredsymtable.check_forwards;
  872. begin
  873. SymList.ForEachCall(@check_forward,nil);
  874. { check whether all block definitions contain valid Objective-C types
  875. (now that all forward definitions have been resolved) }
  876. DefList.ForEachCall(@check_block_valid,nil);
  877. end;
  878. procedure tstoredsymtable.checklabels;
  879. begin
  880. SymList.ForEachCall(@labeldefined,nil);
  881. end;
  882. procedure tstoredsymtable.allsymbolsused;
  883. begin
  884. SymList.ForEachCall(@varsymbolused,nil);
  885. end;
  886. procedure tstoredsymtable.allprivatesused;
  887. begin
  888. SymList.ForEachCall(@objectprivatesymbolused,nil);
  889. end;
  890. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  891. begin
  892. if sto_needs_init_final in tableoptions then
  893. exit;
  894. { don't check static symbols - they can be present in structures only and
  895. always have a reference to a symbol defined on unit level }
  896. if sp_static in tsym(sym).symoptions then
  897. exit;
  898. case tsym(sym).typ of
  899. fieldvarsym,
  900. staticvarsym,
  901. localvarsym,
  902. paravarsym :
  903. begin
  904. if assigned(tabstractvarsym(sym).vardef) and
  905. is_managed_type(tabstractvarsym(sym).vardef) then
  906. include(tableoptions,sto_needs_init_final);
  907. end;
  908. end;
  909. end;
  910. { returns true, if p contains data which needs init/final code }
  911. function tstoredsymtable.needs_init_final : boolean;
  912. begin
  913. if not init_final_check_done then
  914. begin
  915. exclude(tableoptions,sto_needs_init_final);
  916. SymList.ForEachCall(@_needs_init_final,nil);
  917. init_final_check_done:=true;
  918. end;
  919. result:=sto_needs_init_final in tableoptions;
  920. end;
  921. {****************************************************************************
  922. TAbstractRecordSymtable
  923. ****************************************************************************}
  924. {$ifdef llvm}
  925. function tabstractrecordsymtable.getllvmshadowsymtabll: tllvmshadowsymtable;
  926. begin
  927. if not assigned(fllvmst) then
  928. fllvmst:=tllvmshadowsymtable.create(self);
  929. result:=fllvmst;
  930. end;
  931. {$endif llvm}
  932. constructor tabstractrecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  933. begin
  934. inherited create(n);
  935. moduleid:=current_module.moduleid;
  936. _datasize:=0;
  937. databitsize:=0;
  938. recordalignment:=1;
  939. usefieldalignment:=usealign;
  940. recordalignmin:=recordminalign;
  941. maxCrecordalign:=recordmaxCalign;
  942. padalignment:=1;
  943. { recordalign C_alignment means C record packing, that starts
  944. with an alignment of 1 }
  945. case usealign of
  946. C_alignment,
  947. bit_alignment:
  948. fieldalignment:=1;
  949. mac68k_alignment:
  950. fieldalignment:=2;
  951. else
  952. fieldalignment:=usealign;
  953. end;
  954. end;
  955. destructor tabstractrecordsymtable.destroy;
  956. begin
  957. {$ifdef llvm}
  958. fllvmst.free;
  959. {$endif llvm}
  960. inherited destroy;
  961. end;
  962. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  963. begin
  964. if ppufile.readentry<>ibrecsymtableoptions then
  965. Message(unit_f_ppu_read_error);
  966. recordalignment:=shortint(ppufile.getbyte);
  967. usefieldalignment:=shortint(ppufile.getbyte);
  968. recordalignmin:=shortint(ppufile.getbyte);
  969. if (usefieldalignment=C_alignment) then
  970. fieldalignment:=shortint(ppufile.getbyte);
  971. inherited ppuload(ppufile);
  972. end;
  973. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  974. var
  975. oldtyp : byte;
  976. begin
  977. oldtyp:=ppufile.entrytyp;
  978. ppufile.entrytyp:=subentryid;
  979. { in case of classes using C alignment, the alignment of the parent
  980. affects the alignment of fields of the childs }
  981. ppufile.putbyte(byte(recordalignment));
  982. ppufile.putbyte(byte(usefieldalignment));
  983. ppufile.putbyte(byte(recordalignmin));
  984. if (usefieldalignment=C_alignment) then
  985. ppufile.putbyte(byte(fieldalignment));
  986. ppufile.writeentry(ibrecsymtableoptions);
  987. inherited ppuwrite(ppufile);
  988. ppufile.entrytyp:=oldtyp;
  989. end;
  990. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  991. begin
  992. { optimal alignment of the record when declaring a variable of this }
  993. { type is independent of the packrecords setting }
  994. if (fieldoffs mod fieldalign) = 0 then
  995. result:=fieldalign
  996. else if (fieldalign >= 16) and
  997. ((fieldoffs mod 16) = 0) and
  998. ((fieldalign mod 16) = 0) then
  999. result:=16
  1000. else if (fieldalign >= 8) and
  1001. ((fieldoffs mod 8) = 0) and
  1002. ((fieldalign mod 8) = 0) then
  1003. result:=8
  1004. else if (fieldalign >= 4) and
  1005. ((fieldoffs mod 4) = 0) and
  1006. ((fieldalign mod 4) = 0) then
  1007. result:=4
  1008. else if (fieldalign >= 2) and
  1009. ((fieldoffs mod 2) = 0) and
  1010. ((fieldalign mod 2) = 0) then
  1011. result:=2
  1012. else
  1013. result:=1;
  1014. end;
  1015. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1016. var
  1017. varalignrecord: shortint;
  1018. begin
  1019. case usefieldalignment of
  1020. C_alignment:
  1021. varalignrecord:=used_align(varalign,recordalignmin,maxCrecordalign);
  1022. mac68k_alignment:
  1023. varalignrecord:=2;
  1024. else
  1025. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1026. end;
  1027. recordalignment:=max(recordalignment,varalignrecord);
  1028. end;
  1029. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1030. var
  1031. l : asizeint;
  1032. varalign : shortint;
  1033. vardef : tdef;
  1034. begin
  1035. if (sym.owner<>self) then
  1036. internalerror(200602031);
  1037. if sym.fieldoffset<>-1 then
  1038. internalerror(200602032);
  1039. { set visibility for the symbol }
  1040. sym.visibility:=vis;
  1041. { this symbol can't be loaded to a register }
  1042. sym.varregable:=vr_none;
  1043. { Calculate field offset }
  1044. l:=sym.getsize;
  1045. vardef:=sym.vardef;
  1046. varalign:=vardef.structalignment;
  1047. case usefieldalignment of
  1048. bit_alignment:
  1049. begin
  1050. { bitpacking only happens for ordinals, the rest is aligned at }
  1051. { 1 byte (compatible with GPC/GCC) }
  1052. if is_ordinal(vardef) then
  1053. begin
  1054. sym.fieldoffset:=databitsize;
  1055. l:=sym.getpackedbitsize;
  1056. end
  1057. else
  1058. begin
  1059. databitsize:=_datasize*8;
  1060. sym.fieldoffset:=databitsize;
  1061. if (l>high(asizeint) div 8) then
  1062. Message(sym_e_segment_too_large);
  1063. l:=l*8;
  1064. end;
  1065. if varalign=0 then
  1066. varalign:=size_2_align(l);
  1067. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1068. { bit packed records are limited to high(aint) bits }
  1069. { instead of bytes to avoid double precision }
  1070. { arithmetic in offset calculations }
  1071. if int64(l)>high(asizeint)-sym.fieldoffset then
  1072. begin
  1073. Message(sym_e_segment_too_large);
  1074. _datasize:=high(asizeint);
  1075. databitsize:=high(asizeint);
  1076. end
  1077. else
  1078. begin
  1079. databitsize:=sym.fieldoffset+l;
  1080. _datasize:=(databitsize+7) div 8;
  1081. end;
  1082. { rest is not applicable }
  1083. exit;
  1084. end;
  1085. else
  1086. begin
  1087. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1088. if l>high(asizeint)-sym.fieldoffset then
  1089. begin
  1090. Message(sym_e_segment_too_large);
  1091. _datasize:=high(asizeint);
  1092. end
  1093. else
  1094. _datasize:=sym.fieldoffset+l;
  1095. { Calc alignment needed for this record }
  1096. alignrecord(sym.fieldoffset,varalign);
  1097. end;
  1098. end;
  1099. end;
  1100. function field_alignment_compare(item1, item2: pointer): integer;
  1101. var
  1102. field1: tfieldvarsym absolute item1;
  1103. field2: tfieldvarsym absolute item2;
  1104. begin
  1105. { we don't care about static fields, those become global variables }
  1106. if (sp_static in field1.symoptions) or
  1107. (sp_static in field2.symoptions) then
  1108. exit(0);
  1109. { sort from large to small alignment, and in case of the same alignment
  1110. in declaration order (items declared close together are possibly
  1111. also related and hence possibly used together -> putting them next
  1112. to each other can improve cache behaviour) }
  1113. result:=field2.vardef.alignment-field1.vardef.alignment;
  1114. if result=0 then
  1115. result:=field1.fieldoffset-field2.fieldoffset;
  1116. end;
  1117. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1118. var
  1119. fieldvs, insertfieldvs: tfieldvarsym;
  1120. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1121. i, j, bestfieldindex: longint;
  1122. globalfieldalignment,
  1123. prevglobalfieldalignment,
  1124. newfieldalignment: shortint;
  1125. changed: boolean;
  1126. begin
  1127. if maybereorder and
  1128. (cs_opt_reorder_fields in current_settings.optimizerswitches) then
  1129. begin
  1130. { assign dummy field offsets so we can know their order in the
  1131. sorting routine }
  1132. for i:=0 to list.count-1 do
  1133. tfieldvarsym(list[i]).fieldoffset:=i;
  1134. { sort the non-class fields to minimise losses due to alignment }
  1135. list.sort(@field_alignment_compare);
  1136. { now fill up gaps caused by alignment skips with smaller fields
  1137. where possible }
  1138. repeat
  1139. i:=0;
  1140. base:=_datasize;
  1141. globalfieldalignment:=fieldalignment;
  1142. changed:=false;
  1143. while i<list.count do
  1144. begin
  1145. fieldvs:=tfieldvarsym(list[i]);
  1146. if sp_static in fieldvs.symoptions then
  1147. begin
  1148. inc(i);
  1149. continue;
  1150. end;
  1151. prevglobalfieldalignment:=globalfieldalignment;
  1152. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1153. newfieldalignment:=globalfieldalignment;
  1154. { size of the gap between the end of the previous field and
  1155. the start of the current one }
  1156. space:=fieldoffset-base;
  1157. bestspaceleft:=space;
  1158. while space>0 do
  1159. begin
  1160. bestfieldindex:=-1;
  1161. bestinsertfieldoffset:=-1;
  1162. for j:=i+1 to list.count-1 do
  1163. begin
  1164. insertfieldvs:=tfieldvarsym(list[j]);
  1165. if sp_static in insertfieldvs.symoptions then
  1166. continue;
  1167. insertfieldsize:=insertfieldvs.getsize;
  1168. { can the new field fit possibly in the gap? }
  1169. if insertfieldsize<=space then
  1170. begin
  1171. { restore globalfieldalignment to situation before
  1172. the original field was inserted }
  1173. globalfieldalignment:=prevglobalfieldalignment;
  1174. { at what offset would it be inserted? (this new
  1175. field has its own alignment requirements, which
  1176. may make it impossible to fit after all) }
  1177. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1178. globalfieldalignment:=prevglobalfieldalignment;
  1179. { taking into account the alignment, does it still
  1180. fit and if so, does it fit better than the
  1181. previously found best fit? }
  1182. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1183. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1184. begin
  1185. { new best fit }
  1186. bestfieldindex:=j;
  1187. bestinsertfieldoffset:=insertfieldoffset;
  1188. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1189. if bestspaceleft=0 then
  1190. break;
  1191. end;
  1192. end;
  1193. end;
  1194. { if we didn't find any field to fit, stop trying for this
  1195. gap }
  1196. if bestfieldindex=-1 then
  1197. break;
  1198. changed:=true;
  1199. { we found a field to insert -> adjust the new base
  1200. address }
  1201. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1202. { update globalfieldalignment for this newly inserted
  1203. field }
  1204. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1205. { move the new field before the current one }
  1206. list.move(bestfieldindex,i);
  1207. { and skip the new field (which is now at position i) }
  1208. inc(i);
  1209. { there may be more space left -> continue }
  1210. space:=bestspaceleft;
  1211. end;
  1212. if base>fieldoffset then
  1213. internalerror(2012071302);
  1214. { check the next field }
  1215. base:=fieldoffset+fieldvs.getsize;
  1216. { since the original field had the same or greater alignment
  1217. than anything we inserted before it, the global field
  1218. alignment is still the same now as it was originally after
  1219. inserting that field }
  1220. globalfieldalignment:=newfieldalignment;
  1221. inc(i);
  1222. end;
  1223. { there may be small gaps left *before* inserted fields }
  1224. until not changed;
  1225. end;
  1226. { reset the dummy field offsets }
  1227. for i:=0 to list.count-1 do
  1228. tfieldvarsym(list[i]).fieldoffset:=-1;
  1229. { finally, set the actual field offsets }
  1230. for i:=0 to list.count-1 do
  1231. begin
  1232. fieldvs:=tfieldvarsym(list[i]);
  1233. { static data fields are already inserted in the globalsymtable }
  1234. if not(sp_static in fieldvs.symoptions) then
  1235. begin
  1236. { read_record_fields already set the visibility of the fields,
  1237. because a single list can contain symbols with different
  1238. visibility }
  1239. addfield(fieldvs,fieldvs.visibility);
  1240. end;
  1241. end;
  1242. end;
  1243. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1244. var
  1245. i: longint;
  1246. sym: tsym;
  1247. begin
  1248. { there could be multiple fields in case of a variant record }
  1249. if (defowner.typ=recorddef) and
  1250. trecorddef(defowner).isunion then
  1251. internalerror(2014090403);
  1252. for i:=0 to SymList.count-1 do
  1253. begin
  1254. sym:=tsym(symlist[i]);
  1255. if (sym.typ=fieldvarsym) and
  1256. not(sp_static in sym.symoptions) and
  1257. (tfieldvarsym(sym).fieldoffset>=offset) then
  1258. begin
  1259. result:=tfieldvarsym(sym);
  1260. exit;
  1261. end;
  1262. end;
  1263. result:=nil;
  1264. end;
  1265. procedure tabstractrecordsymtable.addalignmentpadding;
  1266. var
  1267. padded_datasize: asizeint;
  1268. begin
  1269. { make the record size aligned correctly so it can be
  1270. used as elements in an array. For C records we
  1271. use the fieldalignment, because that is updated with the
  1272. used alignment. }
  1273. if (padalignment = 1) then
  1274. case usefieldalignment of
  1275. C_alignment:
  1276. padalignment:=fieldalignment;
  1277. { bitpacked }
  1278. bit_alignment:
  1279. padalignment:=1;
  1280. { mac68k: always round to multiple of 2 }
  1281. mac68k_alignment:
  1282. padalignment:=2;
  1283. { default/no packrecords specified }
  1284. 0:
  1285. padalignment:=recordalignment
  1286. { specific packrecords setting -> use as upper limit }
  1287. else
  1288. padalignment:=min(recordalignment,usefieldalignment);
  1289. end;
  1290. padded_datasize:=align(_datasize,padalignment);
  1291. _paddingsize:=padded_datasize-_datasize;
  1292. _datasize:=padded_datasize;
  1293. end;
  1294. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1295. begin
  1296. { Enums must also be available outside the record scope,
  1297. insert in the owner of this symtable }
  1298. if def.typ=enumdef then
  1299. defowner.owner.insertdef(def)
  1300. else
  1301. inherited insertdef(def);
  1302. end;
  1303. function tabstractrecordsymtable.is_packed: boolean;
  1304. begin
  1305. result:=usefieldalignment=bit_alignment;
  1306. end;
  1307. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1308. var
  1309. i: longint;
  1310. currentsymlist: TFPHashObjectList;
  1311. currentdef: tdef;
  1312. sym: tfieldvarsym;
  1313. begin
  1314. result:=false;
  1315. { If a record contains a union, it does not contain a "single
  1316. non-composite field" in the context of certain ABIs requiring
  1317. special treatment for such records }
  1318. if (defowner.typ=recorddef) and
  1319. trecorddef(defowner).isunion then
  1320. exit;
  1321. { a record/object can contain other things than fields }
  1322. currentsymlist:=symlist;
  1323. { recurse in arrays and records }
  1324. sym:=nil;
  1325. repeat
  1326. { record has one field? }
  1327. for i:=0 to currentsymlist.Count-1 do
  1328. begin
  1329. if (tsym(currentsymlist[i]).typ=fieldvarsym) and
  1330. not(sp_static in tsym(currentsymlist[i]).symoptions) then
  1331. begin
  1332. if result then
  1333. begin
  1334. result:=false;
  1335. exit;
  1336. end;
  1337. result:=true;
  1338. sym:=tfieldvarsym(currentsymlist[i])
  1339. end;
  1340. end;
  1341. if assigned(sym) then
  1342. begin
  1343. { if the field is an array, does it contain one element? }
  1344. currentdef:=sym.vardef;
  1345. while (currentdef.typ=arraydef) and
  1346. not is_special_array(currentdef) do
  1347. begin
  1348. if tarraydef(currentdef).elecount<>1 then
  1349. begin
  1350. result:=false;
  1351. exit;
  1352. end;
  1353. currentdef:=tarraydef(currentdef).elementdef;
  1354. end;
  1355. { if the array element is again a record, continue descending }
  1356. if currentdef.typ=recorddef then
  1357. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1358. else
  1359. begin
  1360. { otherwise we found the type of the single element }
  1361. def:=currentdef;
  1362. exit;
  1363. end;
  1364. end
  1365. else
  1366. exit
  1367. until false;
  1368. end;
  1369. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1370. begin
  1371. result:=defowner.owner;
  1372. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1373. result:=result.defowner.owner;
  1374. end;
  1375. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1376. begin
  1377. _datasize:=val;
  1378. if (usefieldalignment=bit_alignment) then
  1379. { can overflow in non bitpacked records }
  1380. databitsize:=val*8;
  1381. end;
  1382. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1383. var
  1384. l : asizeint;
  1385. varalignfield,
  1386. varalign : shortint;
  1387. vardef : tdef;
  1388. begin
  1389. { Calculate field offset }
  1390. l:=sym.getsize;
  1391. vardef:=sym.vardef;
  1392. varalign:=vardef.structalignment;
  1393. case usefieldalignment of
  1394. bit_alignment:
  1395. { has to be handled separately }
  1396. internalerror(2012071301);
  1397. C_alignment:
  1398. begin
  1399. { Calc the alignment size for C style records }
  1400. if (varalign>4) and
  1401. ((varalign mod 4)<>0) and
  1402. (vardef.typ=arraydef) then
  1403. Message1(sym_w_wrong_C_pack,vardef.typename);
  1404. if varalign=0 then
  1405. varalign:=l;
  1406. if (globalfieldalignment<maxCrecordalign) then
  1407. begin
  1408. if (varalign>16) and (globalfieldalignment<32) then
  1409. globalfieldalignment:=32
  1410. else if (varalign>12) and (globalfieldalignment<16) then
  1411. globalfieldalignment:=16
  1412. { 12 is needed for long double }
  1413. else if (varalign>8) and (globalfieldalignment<12) then
  1414. globalfieldalignment:=12
  1415. else if (varalign>4) and (globalfieldalignment<8) then
  1416. globalfieldalignment:=8
  1417. else if (varalign>2) and (globalfieldalignment<4) then
  1418. globalfieldalignment:=4
  1419. else if (varalign>1) and (globalfieldalignment<2) then
  1420. globalfieldalignment:=2;
  1421. end;
  1422. globalfieldalignment:=min(globalfieldalignment,maxCrecordalign);
  1423. end;
  1424. mac68k_alignment:
  1425. begin
  1426. { mac68k alignment (C description):
  1427. * char is aligned to 1 byte
  1428. * everything else (except vector) is aligned to 2 bytes
  1429. * vector is aligned to 16 bytes
  1430. }
  1431. if l>1 then
  1432. globalfieldalignment:=2
  1433. else
  1434. globalfieldalignment:=1;
  1435. varalign:=2;
  1436. end;
  1437. end;
  1438. if varalign=0 then
  1439. varalign:=size_2_align(l);
  1440. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1441. result:=align(base,varalignfield);
  1442. end;
  1443. function tabstractrecordsymtable.iscurrentunit: boolean;
  1444. begin
  1445. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1446. end;
  1447. {****************************************************************************
  1448. TRecordSymtable
  1449. ****************************************************************************}
  1450. constructor trecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1451. begin
  1452. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1453. symtabletype:=recordsymtable;
  1454. end;
  1455. { this procedure is reserved for inserting case variant into
  1456. a record symtable }
  1457. { the offset is the location of the start of the variant
  1458. and datasize and dataalignment corresponds to
  1459. the complete size (see code in pdecl unit) PM }
  1460. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  1461. var
  1462. sym : tsym;
  1463. def : tdef;
  1464. i : integer;
  1465. varalignrecord,varalign,
  1466. storesize,storealign : aint;
  1467. bitsize: tcgint;
  1468. begin
  1469. storesize:=_datasize;
  1470. storealign:=fieldalignment;
  1471. _datasize:=offset;
  1472. if (usefieldalignment=bit_alignment) then
  1473. databitsize:=offset*8;
  1474. { We move the ownership of the defs and symbols to the new recordsymtable.
  1475. The old unionsymtable keeps the references, but doesn't own the
  1476. objects anymore }
  1477. unionst.DefList.OwnsObjects:=false;
  1478. unionst.SymList.OwnsObjects:=false;
  1479. { copy symbols }
  1480. for i:=0 to unionst.SymList.Count-1 do
  1481. begin
  1482. sym:=TSym(unionst.SymList[i]);
  1483. if sym.typ<>fieldvarsym then
  1484. internalerror(200601272);
  1485. if tfieldvarsym(sym).fieldoffset=0 then
  1486. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1487. { add to this record symtable, checking for duplicate names }
  1488. // unionst.SymList.List.List^[i].Data:=nil;
  1489. insert(sym);
  1490. varalign:=tfieldvarsym(sym).vardef.alignment;
  1491. if varalign=0 then
  1492. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1493. { retrieve size }
  1494. if (usefieldalignment=bit_alignment) then
  1495. begin
  1496. { bit packed records are limited to high(aint) bits }
  1497. { instead of bytes to avoid double precision }
  1498. { arithmetic in offset calculations }
  1499. if is_ordinal(tfieldvarsym(sym).vardef) then
  1500. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1501. else
  1502. begin
  1503. bitsize:=tfieldvarsym(sym).getsize;
  1504. if (bitsize>high(asizeint) div 8) then
  1505. Message(sym_e_segment_too_large);
  1506. bitsize:=bitsize*8;
  1507. end;
  1508. if bitsize>high(asizeint)-databitsize then
  1509. begin
  1510. Message(sym_e_segment_too_large);
  1511. _datasize:=high(asizeint);
  1512. databitsize:=high(asizeint);
  1513. end
  1514. else
  1515. begin
  1516. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1517. _datasize:=(databitsize+7) div 8;
  1518. end;
  1519. tfieldvarsym(sym).fieldoffset:=databitsize;
  1520. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1521. end
  1522. else
  1523. begin
  1524. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1525. begin
  1526. Message(sym_e_segment_too_large);
  1527. _datasize:=high(asizeint);
  1528. end
  1529. else
  1530. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1531. { update address }
  1532. tfieldvarsym(sym).fieldoffset:=_datasize;
  1533. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1534. end;
  1535. { update alignment of this record }
  1536. if (usefieldalignment<>C_alignment) and
  1537. (usefieldalignment<>mac68k_alignment) then
  1538. recordalignment:=max(recordalignment,varalignrecord);
  1539. end;
  1540. { update alignment for C records }
  1541. if (usefieldalignment=C_alignment) and
  1542. (usefieldalignment<>mac68k_alignment) then
  1543. recordalignment:=max(recordalignment,unionst.recordalignment);
  1544. { Register defs in the new record symtable }
  1545. for i:=0 to unionst.DefList.Count-1 do
  1546. begin
  1547. def:=TDef(unionst.DefList[i]);
  1548. def.ChangeOwner(self);
  1549. end;
  1550. _datasize:=storesize;
  1551. fieldalignment:=storealign;
  1552. { If a record contains a union, it does not contain a "single
  1553. non-composite field" in the context of certain ABIs requiring
  1554. special treatment for such records }
  1555. if defowner.typ=recorddef then
  1556. trecorddef(defowner).isunion:=true;
  1557. end;
  1558. {****************************************************************************
  1559. TObjectSymtable
  1560. ****************************************************************************}
  1561. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1562. begin
  1563. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1564. symtabletype:=ObjectSymtable;
  1565. defowner:=adefowner;
  1566. end;
  1567. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1568. var
  1569. hsym: tsym;
  1570. warn: boolean;
  1571. begin
  1572. result:=false;
  1573. if not assigned(defowner) then
  1574. internalerror(200602061);
  1575. { procsym and propertysym have special code
  1576. to override values in inherited classes. For other
  1577. symbols check for duplicates }
  1578. if not(sym.typ in [procsym,propertysym]) then
  1579. begin
  1580. { but private ids can be reused }
  1581. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1582. if assigned(hsym) and
  1583. (
  1584. (
  1585. not(m_delphi in current_settings.modeswitches) and
  1586. is_visible_for_object(hsym,tobjectdef(defowner))
  1587. ) or
  1588. (
  1589. { In Delphi, you can repeat members of a parent class. You can't }
  1590. { do this for objects however, and you (obviouly) can't }
  1591. { declare two fields with the same name in a single class }
  1592. (m_delphi in current_settings.modeswitches) and
  1593. (
  1594. is_object(tdef(defowner)) or
  1595. (hsym.owner = self)
  1596. )
  1597. )
  1598. ) then
  1599. begin
  1600. { only watn when a parameter/local variable in a method
  1601. conflicts with a category method, because this can easily
  1602. happen due to all possible categories being imported via
  1603. CocoaAll }
  1604. warn:=
  1605. (is_objccategory(tdef(hsym.owner.defowner)) or
  1606. is_classhelper(tdef(hsym.owner.defowner))) and
  1607. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1608. DuplicateSym(hashedid,sym,hsym,warn);
  1609. result:=true;
  1610. end;
  1611. end
  1612. else
  1613. result:=inherited checkduplicate(hashedid,sym);
  1614. end;
  1615. {$ifdef llvm}
  1616. {****************************************************************************
  1617. tLlvmShadowSymtableEntry
  1618. ****************************************************************************}
  1619. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1620. begin
  1621. fdef:=def;
  1622. ffieldoffset:=fieldoffset;
  1623. end;
  1624. {****************************************************************************
  1625. TLlvmShadowSymtable
  1626. ****************************************************************************}
  1627. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1628. begin
  1629. result:=get_by_llvm_index(f.llvmfieldnr)
  1630. end;
  1631. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1632. begin
  1633. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1634. end;
  1635. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1636. begin
  1637. equivst:=st;
  1638. curroffset:=0;
  1639. symdeflist:=tfpobjectlist.create(true);
  1640. generate;
  1641. end;
  1642. destructor tllvmshadowsymtable.destroy;
  1643. begin
  1644. symdeflist.free;
  1645. end;
  1646. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1647. var
  1648. sizectr,
  1649. tmpsize: aint;
  1650. begin
  1651. case equivst.usefieldalignment of
  1652. bit_alignment:
  1653. begin
  1654. { curoffset: bit address after the previous field. }
  1655. { llvm has no special support for bitfields in records, }
  1656. { so we replace them with plain bytes. }
  1657. { as soon as a single bit of a byte is allocated, we }
  1658. { allocate the byte in the llvm shadow record }
  1659. if (fieldoffset>curroffset) then
  1660. curroffset:=align(curroffset,8);
  1661. { fields in bitpacked records always start either right }
  1662. { after the previous one, or at the next byte boundary. }
  1663. if (curroffset<>fieldoffset) then
  1664. internalerror(2008051002);
  1665. if is_ordinal(vardef) then
  1666. begin
  1667. tmpsize:=vardef.packedbitsize;
  1668. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1669. inc(curroffset,tmpsize);
  1670. tmpsize:=0;
  1671. while sizectr<>0 do
  1672. begin
  1673. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1674. dec(sizectr);
  1675. inc(tmpsize);
  1676. end;
  1677. end
  1678. else
  1679. begin
  1680. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1681. if not(derefclass) then
  1682. inc(curroffset,vardef.size*8)
  1683. else
  1684. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1685. end;
  1686. end
  1687. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1688. begin
  1689. { curoffset: address right after the previous field }
  1690. while (fieldoffset>curroffset) do
  1691. begin
  1692. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1693. inc(curroffset);
  1694. end;
  1695. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1696. if not(derefclass) then
  1697. inc(curroffset,vardef.size)
  1698. else
  1699. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1700. end
  1701. else
  1702. { default for llvm, don't add explicit padding }
  1703. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1704. end
  1705. end;
  1706. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1707. begin
  1708. case equivst.usefieldalignment of
  1709. { already correct in this case }
  1710. bit_alignment:
  1711. ;
  1712. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1713. begin
  1714. { add padding fields }
  1715. while (finalsize>curroffset) do
  1716. begin
  1717. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1718. inc(curroffset);
  1719. end;
  1720. end;
  1721. end;
  1722. end;
  1723. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1724. var
  1725. sym: tfieldvarsym;
  1726. lastoffset: aint;
  1727. newalignment: aint;
  1728. i, j: longint;
  1729. begin
  1730. i:=0;
  1731. while (i<equivst.symlist.count) do
  1732. begin
  1733. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1734. begin
  1735. inc(i);
  1736. continue;
  1737. end;
  1738. sym:=tfieldvarsym(equivst.symlist[i]);
  1739. { a "better" algorithm might be to use the largest }
  1740. { variant in case of (bit)packing, since then }
  1741. { alignment doesn't matter }
  1742. if (vo_is_first_field in sym.varoptions) then
  1743. begin
  1744. { we assume that all fields are processed in order. }
  1745. if (variantstarts.count<>0) then
  1746. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1747. else
  1748. lastoffset:=-1;
  1749. { new variant at same level as last one: use if higher alignment }
  1750. if (lastoffset=sym.fieldoffset) then
  1751. begin
  1752. if (equivst.fieldalignment<>bit_alignment) then
  1753. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1754. else
  1755. newalignment:=1;
  1756. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1757. variantstarts[variantstarts.count-1]:=sym;
  1758. end
  1759. { variant at deeper level than last one -> add }
  1760. else if (lastoffset<sym.fieldoffset) then
  1761. variantstarts.add(sym)
  1762. else
  1763. begin
  1764. { a variant at a less deep level, so backtrack }
  1765. j:=variantstarts.count-2;
  1766. while (j>=0) do
  1767. begin
  1768. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1769. break;
  1770. dec(j);
  1771. end;
  1772. if (j<0) then
  1773. internalerror(2008051003);
  1774. { new variant has higher alignment? }
  1775. if (equivst.fieldalignment<>bit_alignment) then
  1776. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1777. else
  1778. newalignment:=1;
  1779. { yes, replace and remove previous nested variants }
  1780. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1781. begin
  1782. variantstarts[j]:=sym;
  1783. variantstarts.count:=j+1;
  1784. end
  1785. { no, skip this variant }
  1786. else
  1787. begin
  1788. inc(i);
  1789. while (i<equivst.symlist.count) and
  1790. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1791. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  1792. inc(i);
  1793. continue;
  1794. end;
  1795. end;
  1796. end;
  1797. inc(i);
  1798. end;
  1799. end;
  1800. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1801. var
  1802. lastvaroffsetprocessed: aint;
  1803. i, equivcount, varcount: longint;
  1804. begin
  1805. { if it's an object/class, the first entry is the parent (if there is one) }
  1806. if (equivst.symtabletype=objectsymtable) and
  1807. assigned(tobjectdef(equivst.defowner).childof) then
  1808. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  1809. equivcount:=equivst.symlist.count;
  1810. varcount:=0;
  1811. i:=0;
  1812. lastvaroffsetprocessed:=-1;
  1813. while (i<equivcount) do
  1814. begin
  1815. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1816. begin
  1817. inc(i);
  1818. continue;
  1819. end;
  1820. { start of a new variant? }
  1821. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1822. begin
  1823. { if we want to process the same variant offset twice, it means that we }
  1824. { got to the end and are trying to process the next variant part -> stop }
  1825. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  1826. break;
  1827. if (varcount>=variantstarts.count) then
  1828. internalerror(2008051005);
  1829. { new variant part -> use the one with the biggest alignment }
  1830. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  1831. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  1832. inc(varcount);
  1833. if (i<0) then
  1834. internalerror(2008051004);
  1835. end;
  1836. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  1837. inc(i);
  1838. end;
  1839. addalignmentpadding(equivst.datasize);
  1840. end;
  1841. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  1842. var
  1843. i, varcount: longint;
  1844. shadowindex: longint;
  1845. equivcount : longint;
  1846. begin
  1847. varcount:=0;
  1848. shadowindex:=0;
  1849. equivcount:=equivst.symlist.count;
  1850. i:=0;
  1851. while (i < equivcount) do
  1852. begin
  1853. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1854. begin
  1855. inc(i);
  1856. continue;
  1857. end;
  1858. { start of a new variant? }
  1859. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1860. begin
  1861. { back up to a less deeply nested variant level? }
  1862. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  1863. dec(varcount);
  1864. { it's possible that some variants are more deeply nested than the
  1865. one we recorded in the shadowsymtable (since we recorded the one
  1866. with the biggest alignment, not necessarily the biggest one in size
  1867. }
  1868. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1869. varcount:=variantstarts.count-1
  1870. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1871. internalerror(2008051006);
  1872. { reset the shadowindex to the start of this variant. }
  1873. { in case the llvmfieldnr is not (yet) set for this }
  1874. { field, shadowindex will simply be reset to zero and }
  1875. { we'll start searching from the start of the record }
  1876. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  1877. if (varcount<pred(variantstarts.count)) then
  1878. inc(varcount);
  1879. end;
  1880. { find the last shadowfield whose offset <= the current field's offset }
  1881. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  1882. (shadowindex<symdeflist.count-1) and
  1883. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  1884. inc(shadowindex);
  1885. { set the field number and potential offset from that field (in case }
  1886. { of overlapping variants) }
  1887. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  1888. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  1889. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  1890. inc(i);
  1891. end;
  1892. end;
  1893. procedure tllvmshadowsymtable.generate;
  1894. var
  1895. variantstarts: tfplist;
  1896. begin
  1897. variantstarts:=tfplist.create;
  1898. { first go through the entire record and }
  1899. { store the fieldvarsyms of the variants }
  1900. { with the highest alignment }
  1901. findvariantstarts(variantstarts);
  1902. { now go through the regular fields and the selected variants, }
  1903. { and add them to the llvm shadow record symtable }
  1904. buildtable(variantstarts);
  1905. { finally map all original fields to the llvm definition }
  1906. buildmapping(variantstarts);
  1907. variantstarts.free;
  1908. end;
  1909. {$endif llvm}
  1910. {****************************************************************************
  1911. TAbstractSubSymtable
  1912. ****************************************************************************}
  1913. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  1914. var
  1915. oldtyp : byte;
  1916. begin
  1917. oldtyp:=ppufile.entrytyp;
  1918. ppufile.entrytyp:=subentryid;
  1919. inherited ppuwrite(ppufile);
  1920. ppufile.entrytyp:=oldtyp;
  1921. end;
  1922. {****************************************************************************
  1923. TAbstractLocalSymtable
  1924. ****************************************************************************}
  1925. function tabstractlocalsymtable.count_locals:longint;
  1926. var
  1927. i : longint;
  1928. sym : tsym;
  1929. begin
  1930. result:=0;
  1931. for i:=0 to SymList.Count-1 do
  1932. begin
  1933. sym:=tsym(SymList[i]);
  1934. { Count only varsyms, but ignore the funcretsym }
  1935. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1936. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1937. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1938. (tstoredsym(sym).refs>0)) then
  1939. inc(result);
  1940. end;
  1941. end;
  1942. function tabstractlocalsymtable.iscurrentunit: boolean;
  1943. begin
  1944. Result:=
  1945. assigned(defowner) and
  1946. defowner.owner.iscurrentunit;
  1947. end;
  1948. {****************************************************************************
  1949. TLocalSymtable
  1950. ****************************************************************************}
  1951. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1952. begin
  1953. inherited create('');
  1954. defowner:=adefowner;
  1955. symtabletype:=localsymtable;
  1956. symtablelevel:=level;
  1957. end;
  1958. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1959. var
  1960. hsym : tsym;
  1961. begin
  1962. if not assigned(defowner) or
  1963. (defowner.typ<>procdef) then
  1964. internalerror(200602042);
  1965. result:=false;
  1966. hsym:=tsym(FindWithHash(hashedid));
  1967. if assigned(hsym) then
  1968. begin
  1969. { a local and the function can have the same
  1970. name in TP and Delphi, but RESULT not }
  1971. if (m_duplicate_names in current_settings.modeswitches) and
  1972. (hsym.typ in [absolutevarsym,localvarsym]) and
  1973. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1974. not((m_result in current_settings.modeswitches) and
  1975. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1976. HideSym(hsym)
  1977. else
  1978. DuplicateSym(hashedid,sym,hsym,false);
  1979. result:=true;
  1980. exit;
  1981. end;
  1982. { check also parasymtable, this needs to be done here because
  1983. of the special situation with the funcret sym that needs to be
  1984. hidden for tp and delphi modes }
  1985. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1986. if assigned(hsym) then
  1987. begin
  1988. { a local and the function can have the same
  1989. name in TP and Delphi, but RESULT not }
  1990. if (m_duplicate_names in current_settings.modeswitches) and
  1991. (sym.typ in [absolutevarsym,localvarsym]) and
  1992. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1993. not((m_result in current_settings.modeswitches) and
  1994. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1995. Hidesym(sym)
  1996. else
  1997. DuplicateSym(hashedid,sym,hsym,false);
  1998. result:=true;
  1999. exit;
  2000. end;
  2001. { check ObjectSymtable, skip this for funcret sym because
  2002. that will always be positive because it has the same name
  2003. as the procsym }
  2004. if not is_funcret_sym(sym) and
  2005. (defowner.typ=procdef) and
  2006. assigned(tprocdef(defowner).struct) and
  2007. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2008. (
  2009. not(m_delphi in current_settings.modeswitches) or
  2010. is_object(tprocdef(defowner).struct)
  2011. ) then
  2012. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2013. end;
  2014. {****************************************************************************
  2015. TParaSymtable
  2016. ****************************************************************************}
  2017. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2018. begin
  2019. inherited create('');
  2020. readonly:=false;
  2021. defowner:=adefowner;
  2022. symtabletype:=parasymtable;
  2023. symtablelevel:=level;
  2024. end;
  2025. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2026. begin
  2027. result:=inherited checkduplicate(hashedid,sym);
  2028. if result then
  2029. exit;
  2030. if not(m_duplicate_names in current_settings.modeswitches) and
  2031. assigned(defowner) and (defowner.typ=procdef) and
  2032. assigned(tprocdef(defowner).struct) and
  2033. assigned(tprocdef(defowner).owner) and
  2034. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2035. (
  2036. not(m_delphi in current_settings.modeswitches) or
  2037. is_object(tprocdef(defowner).struct)
  2038. ) then
  2039. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2040. end;
  2041. procedure tparasymtable.insertdef(def: TDefEntry);
  2042. begin
  2043. if readonly then
  2044. defowner.owner.insertdef(def)
  2045. else
  2046. inherited insertdef(def);
  2047. end;
  2048. {****************************************************************************
  2049. TAbstractUniTSymtable
  2050. ****************************************************************************}
  2051. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2052. begin
  2053. inherited create(n);
  2054. moduleid:=id;
  2055. end;
  2056. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2057. var
  2058. hsym : tsym;
  2059. begin
  2060. result:=false;
  2061. hsym:=tsym(FindWithHash(hashedid));
  2062. if assigned(hsym) then
  2063. begin
  2064. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2065. exit;
  2066. if hsym.typ=symconst.namespacesym then
  2067. begin
  2068. case sym.typ of
  2069. symconst.namespacesym:;
  2070. symconst.unitsym:
  2071. begin
  2072. HideSym(sym); { if we add a unit and there is a namespace with the same name then hide the unit name and not the namespace }
  2073. tnamespacesym(hsym).unitsym:=tsym(sym);
  2074. end
  2075. else
  2076. HideSym(hsym);
  2077. end;
  2078. end
  2079. else
  2080. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2081. unit, the unit can then not be accessed anymore using
  2082. <unit>.<id>, so we can hide the symbol.
  2083. Do the same if we add a namespace and there is a unit with the same name }
  2084. if (hsym.typ=symconst.unitsym) and
  2085. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2086. begin
  2087. HideSym(hsym);
  2088. if sym.typ=symconst.namespacesym then
  2089. tnamespacesym(sym).unitsym:=tsym(hsym);
  2090. end
  2091. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2092. in this case, copy the isoindex and make the original symbol invisible }
  2093. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2094. and (tprogramparasym(hsym).isoindex<>0) then
  2095. begin
  2096. HideSym(hsym);
  2097. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2098. end
  2099. else
  2100. DuplicateSym(hashedid,sym,hsym,false);
  2101. result:=true;
  2102. exit;
  2103. end;
  2104. end;
  2105. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2106. begin
  2107. result:=find(n);
  2108. if assigned(result)and(result.typ<>namespacesym)then
  2109. result:=nil;
  2110. end;
  2111. function tabstractuniTSymtable.iscurrentunit:boolean;
  2112. begin
  2113. result:=assigned(current_module) and
  2114. (
  2115. (current_module.globalsymtable=self) or
  2116. (current_module.localsymtable=self)
  2117. );
  2118. end;
  2119. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2120. var
  2121. p:integer;
  2122. n,ns:string;
  2123. oldsym:TSymEntry;
  2124. begin
  2125. insert(sym);
  2126. n:=sym.realname;
  2127. p:=pos('.',n);
  2128. ns:='';
  2129. while p>0 do
  2130. begin
  2131. if ns='' then
  2132. ns:=copy(n,1,p-1)
  2133. else
  2134. ns:=ns+'.'+copy(n,1,p-1);
  2135. system.delete(n,1,p);
  2136. oldsym:=findnamespace(upper(ns));
  2137. if not assigned(oldsym) then
  2138. insert(cnamespacesym.create(ns));
  2139. p:=pos('.',n);
  2140. end;
  2141. end;
  2142. {****************************************************************************
  2143. TStaticSymtable
  2144. ****************************************************************************}
  2145. constructor tstaticsymtable.create(const n : string;id:word);
  2146. begin
  2147. inherited create(n,id);
  2148. symtabletype:=staticsymtable;
  2149. symtablelevel:=main_program_level;
  2150. currentvisibility:=vis_private;
  2151. end;
  2152. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2153. begin
  2154. inherited ppuload(ppufile);
  2155. { now we can deref the syms and defs }
  2156. deref(false);
  2157. end;
  2158. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2159. begin
  2160. inherited ppuwrite(ppufile);
  2161. end;
  2162. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2163. begin
  2164. result:=inherited checkduplicate(hashedid,sym);
  2165. if not result and
  2166. (current_module.localsymtable=self) and
  2167. assigned(current_module.globalsymtable) then
  2168. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2169. end;
  2170. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2171. begin
  2172. result:=inherited findnamespace(n);
  2173. if not assigned(result) and
  2174. (current_module.localsymtable=self) and
  2175. assigned(current_module.globalsymtable) then
  2176. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2177. end;
  2178. {****************************************************************************
  2179. TGlobalSymtable
  2180. ****************************************************************************}
  2181. constructor tglobalsymtable.create(const n : string;id:word);
  2182. begin
  2183. inherited create(n,id);
  2184. symtabletype:=globalsymtable;
  2185. symtablelevel:=main_program_level;
  2186. end;
  2187. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2188. begin
  2189. inherited ppuload(ppufile);
  2190. { now we can deref the syms and defs }
  2191. deref(false);
  2192. end;
  2193. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2194. begin
  2195. { write the symtable entries }
  2196. inherited ppuwrite(ppufile);
  2197. end;
  2198. {*****************************************************************************
  2199. tspecializesymtable
  2200. *****************************************************************************}
  2201. constructor tspecializesymtable.create(const n : string;id:word);
  2202. begin
  2203. inherited create(n,id);
  2204. { the specialize symtable does not own the syms and defs as they are all
  2205. moved to a different symtable before the symtable is destroyed; this
  2206. avoids calls to "extract" }
  2207. symlist.ownsobjects:=false;
  2208. deflist.ownsobjects:=false;
  2209. end;
  2210. function tspecializesymtable.iscurrentunit: boolean;
  2211. begin
  2212. Result:=true;
  2213. end;
  2214. {****************************************************************************
  2215. TWITHSYMTABLE
  2216. ****************************************************************************}
  2217. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2218. begin
  2219. inherited create('');
  2220. symtabletype:=withsymtable;
  2221. withrefnode:=refnode;
  2222. { Replace SymList with the passed symlist }
  2223. SymList.free;
  2224. SymList:=ASymList;
  2225. defowner:=aowner;
  2226. end;
  2227. destructor twithsymtable.destroy;
  2228. begin
  2229. withrefnode.free;
  2230. { Disable SymList because we don't Own it }
  2231. SymList:=nil;
  2232. inherited destroy;
  2233. end;
  2234. procedure twithsymtable.clear;
  2235. begin
  2236. { remove no entry from a withsymtable as it is only a pointer to the
  2237. recorddef or objectdef symtable }
  2238. end;
  2239. procedure twithsymtable.insertdef(def:TDefEntry);
  2240. begin
  2241. { Definitions can't be registered in the withsymtable
  2242. because the withsymtable is removed after the with block.
  2243. We can't easily solve it here because the next symtable in the
  2244. stack is not known. }
  2245. internalerror(200602046);
  2246. end;
  2247. {****************************************************************************
  2248. TSTT_ExceptionSymtable
  2249. ****************************************************************************}
  2250. constructor tstt_excepTSymtable.create;
  2251. begin
  2252. inherited create('');
  2253. symtabletype:=stt_excepTSymtable;
  2254. end;
  2255. {****************************************************************************
  2256. TMacroSymtable
  2257. ****************************************************************************}
  2258. constructor tmacrosymtable.create(exported: boolean);
  2259. begin
  2260. inherited create('');
  2261. if exported then
  2262. symtabletype:=exportedmacrosymtable
  2263. else
  2264. symtabletype:=localmacrosymtable;
  2265. symtablelevel:=main_program_level;
  2266. end;
  2267. {****************************************************************************
  2268. TEnumSymtable
  2269. ****************************************************************************}
  2270. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  2271. var
  2272. value: longint;
  2273. def: tenumdef;
  2274. begin
  2275. // defowner = nil only when we are loading from ppu
  2276. if defowner<>nil then
  2277. begin
  2278. { First entry? Then we need to set the minval }
  2279. value:=tenumsym(sym).value;
  2280. def:=tenumdef(defowner);
  2281. if SymList.count=0 then
  2282. begin
  2283. if value>0 then
  2284. def.has_jumps:=true;
  2285. def.setmin(value);
  2286. def.setmax(value);
  2287. end
  2288. else
  2289. begin
  2290. { check for jumps }
  2291. if value>def.max+1 then
  2292. def.has_jumps:=true;
  2293. { update low and high }
  2294. if def.min>value then
  2295. def.setmin(value);
  2296. if def.max<value then
  2297. def.setmax(value);
  2298. end;
  2299. end;
  2300. inherited insert(sym, checkdup);
  2301. end;
  2302. constructor tenumsymtable.create(adefowner: tdef);
  2303. begin
  2304. inherited Create('');
  2305. symtabletype:=enumsymtable;
  2306. defowner:=adefowner;
  2307. end;
  2308. {****************************************************************************
  2309. TArraySymtable
  2310. ****************************************************************************}
  2311. procedure tarraysymtable.insertdef(def: TDefEntry);
  2312. begin
  2313. { Enums must also be available outside the record scope,
  2314. insert in the owner of this symtable }
  2315. if def.typ=enumdef then
  2316. defowner.owner.insertdef(def)
  2317. else
  2318. inherited insertdef(def);
  2319. end;
  2320. constructor tarraysymtable.create(adefowner: tdef);
  2321. begin
  2322. inherited Create('');
  2323. symtabletype:=arraysymtable;
  2324. defowner:=adefowner;
  2325. end;
  2326. {*****************************************************************************
  2327. Helper Routines
  2328. *****************************************************************************}
  2329. function FullTypeName(def,otherdef:tdef):string;
  2330. var
  2331. s1,s2 : string;
  2332. begin
  2333. if def.typ in [objectdef,recorddef] then
  2334. s1:=tabstractrecorddef(def).RttiName
  2335. else
  2336. s1:=def.typename;
  2337. { When the names are the same try to include the unit name }
  2338. if assigned(otherdef) and
  2339. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2340. begin
  2341. s2:=otherdef.typename;
  2342. if upper(s1)=upper(s2) then
  2343. s1:=def.owner.realname^+'.'+s1;
  2344. end;
  2345. FullTypeName:=s1;
  2346. end;
  2347. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  2348. begin
  2349. result:='';
  2350. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2351. begin
  2352. if (result='') then
  2353. if symtable.name<>nil then
  2354. result:=symtable.name^
  2355. else
  2356. else
  2357. if symtable.name<>nil then
  2358. result:=symtable.name^+delimiter+result
  2359. else
  2360. result:=delimiter+result;
  2361. symtable:=symtable.defowner.owner;
  2362. end;
  2363. end;
  2364. function generate_objectpascal_helper_key(def:tdef):string;
  2365. begin
  2366. if not assigned(def) then
  2367. internalerror(2013020501);
  2368. if def.typ in [recorddef,objectdef] then
  2369. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2370. else
  2371. result:=make_mangledname('',def.owner,def.typesym.name);
  2372. end;
  2373. procedure incompatibletypes(def1,def2:tdef);
  2374. begin
  2375. { When there is an errordef there is already an error message show }
  2376. if (def2.typ=errordef) or
  2377. (def1.typ=errordef) then
  2378. exit;
  2379. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2380. end;
  2381. procedure hidesym(sym:TSymEntry);
  2382. begin
  2383. sym.realname:='$hidden'+sym.realname;
  2384. tsym(sym).visibility:=vis_hidden;
  2385. end;
  2386. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2387. var
  2388. st : TSymtable;
  2389. filename : TIDString;
  2390. begin
  2391. if not warn then
  2392. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2393. else
  2394. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2395. { Write hint where the original symbol was found }
  2396. st:=finduniTSymtable(origsym.owner);
  2397. with tsym(origsym).fileinfo do
  2398. begin
  2399. if assigned(st) and
  2400. (st.symtabletype=globalsymtable) and
  2401. st.iscurrentunit then
  2402. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2403. else if assigned(st.name) then
  2404. begin
  2405. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2406. if filename<>'' then
  2407. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2408. else
  2409. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2410. end;
  2411. end;
  2412. { Rename duplicate sym to an unreachable name, but it can be
  2413. inserted in the symtable without errors }
  2414. inc(dupnr);
  2415. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2416. if assigned(dupsym) then
  2417. include(tsym(dupsym).symoptions,sp_implicitrename);
  2418. end;
  2419. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2420. begin
  2421. result:=false;
  2422. if not assigned(sym) or not (sym is tstoredsym) then
  2423. Internalerror(2011081101);
  2424. { For generics a dummy symbol without the parameter count is created
  2425. if such a symbol not yet exists so that different parts of the
  2426. parser can find that symbol. If that symbol is still a
  2427. undefineddef we replace the generic dummy symbol's
  2428. name with a "dup" name and use the new symbol as the generic dummy
  2429. symbol }
  2430. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2431. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2432. (m_delphi in current_settings.modeswitches) then
  2433. begin
  2434. inc(dupnr);
  2435. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2436. include(tsym(sym).symoptions,sp_implicitrename);
  2437. { we need to find the new symbol now if checking for a dummy }
  2438. include(symoptions,sp_generic_dummy);
  2439. result:=true;
  2440. end;
  2441. end;
  2442. {*****************************************************************************
  2443. Search
  2444. *****************************************************************************}
  2445. procedure addsymref(sym:tsym);
  2446. var
  2447. owner: tsymtable;
  2448. begin
  2449. { symbol uses count }
  2450. sym.IncRefCount;
  2451. owner:=sym.owner;
  2452. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2453. owner:=tdef(owner.defowner).owner;
  2454. if assigned(current_module) and
  2455. (owner.symtabletype=globalsymtable) then
  2456. begin
  2457. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2458. internalerror(200501152);
  2459. { unit uses count }
  2460. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2461. { symbol is imported from another unit }
  2462. if current_module.globalsymtable<>owner then
  2463. current_module.addimportedsym(sym);
  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 is_objccategory(defowner) 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.