symtable.pas 176 KB

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