symtable.pas 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653
  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. if refcount>1 then
  1046. exit;
  1047. {$ifdef llvm}
  1048. fllvmst.free;
  1049. {$endif llvm}
  1050. for mop in tmanagementoperator do
  1051. begin
  1052. if assigned(mop_list[mop]) then
  1053. for i:=0 to mop_list[mop].count-1 do
  1054. dispose(pmanagementoperator_offset_entry(mop_list[mop][i]));
  1055. mop_list[mop].free;
  1056. end;
  1057. inherited destroy;
  1058. end;
  1059. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  1060. begin
  1061. if ppufile.readentry<>ibrecsymtableoptions then
  1062. Message(unit_f_ppu_read_error);
  1063. recordalignment:=shortint(ppufile.getbyte);
  1064. usefieldalignment:=shortint(ppufile.getbyte);
  1065. recordalignmin:=shortint(ppufile.getbyte);
  1066. if (usefieldalignment=C_alignment) then
  1067. fieldalignment:=shortint(ppufile.getbyte);
  1068. ppufile.getsmallset(has_fields_with_mop);
  1069. inherited ppuload(ppufile);
  1070. end;
  1071. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  1072. var
  1073. oldtyp : byte;
  1074. begin
  1075. oldtyp:=ppufile.entrytyp;
  1076. ppufile.entrytyp:=subentryid;
  1077. { in case of classes using C alignment, the alignment of the parent
  1078. affects the alignment of fields of the childs }
  1079. ppufile.putbyte(byte(recordalignment));
  1080. ppufile.putbyte(byte(usefieldalignment));
  1081. ppufile.putbyte(byte(recordalignmin));
  1082. if (usefieldalignment=C_alignment) then
  1083. ppufile.putbyte(byte(fieldalignment));
  1084. { it's not really a "symtableoption", but loading this from the record
  1085. def requires storing the set in the recorddef at least between
  1086. ppuload and deref/derefimpl }
  1087. ppufile.putsmallset(has_fields_with_mop);
  1088. ppufile.writeentry(ibrecsymtableoptions);
  1089. inherited ppuwrite(ppufile);
  1090. ppufile.entrytyp:=oldtyp;
  1091. end;
  1092. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  1093. begin
  1094. { optimal alignment of the record when declaring a variable of this }
  1095. { type is independent of the packrecords setting }
  1096. if (fieldoffs mod fieldalign) = 0 then
  1097. result:=fieldalign
  1098. else if (fieldalign >= 16) and
  1099. ((fieldoffs mod 16) = 0) and
  1100. ((fieldalign mod 16) = 0) then
  1101. result:=16
  1102. else if (fieldalign >= 8) and
  1103. ((fieldoffs mod 8) = 0) and
  1104. ((fieldalign mod 8) = 0) then
  1105. result:=8
  1106. else if (fieldalign >= 4) and
  1107. ((fieldoffs mod 4) = 0) and
  1108. ((fieldalign mod 4) = 0) then
  1109. result:=4
  1110. else if (fieldalign >= 2) and
  1111. ((fieldoffs mod 2) = 0) and
  1112. ((fieldalign mod 2) = 0) then
  1113. result:=2
  1114. else
  1115. result:=1;
  1116. end;
  1117. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1118. var
  1119. varalignrecord: shortint;
  1120. begin
  1121. case usefieldalignment of
  1122. C_alignment:
  1123. varalignrecord:=used_align(varalign,recordalignmin,maxCrecordalign);
  1124. mac68k_alignment:
  1125. varalignrecord:=2;
  1126. else
  1127. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1128. end;
  1129. recordalignment:=max(recordalignment,varalignrecord);
  1130. end;
  1131. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1132. var
  1133. l : asizeint;
  1134. varalign : shortint;
  1135. vardef : tdef;
  1136. begin
  1137. if (sym.owner<>self) then
  1138. internalerror(200602031);
  1139. if sym.fieldoffset<>-1 then
  1140. internalerror(200602032);
  1141. { set visibility for the symbol }
  1142. sym.visibility:=vis;
  1143. { this symbol can't be loaded to a register }
  1144. sym.varregable:=vr_none;
  1145. { management operators }
  1146. if sym.vardef.typ in [recorddef,objectdef] then
  1147. has_fields_with_mop:=has_fields_with_mop + tabstractrecordsymtable(tabstractrecorddef(sym.vardef).symtable).has_fields_with_mop;
  1148. if sym.vardef.typ=recorddef then
  1149. has_fields_with_mop:=has_fields_with_mop + trecordsymtable(trecorddef(sym.vardef).symtable).managementoperators;
  1150. { Calculate field offset }
  1151. l:=sym.getsize;
  1152. vardef:=sym.vardef;
  1153. varalign:=vardef.structalignment;
  1154. case usefieldalignment of
  1155. bit_alignment:
  1156. begin
  1157. { bitpacking only happens for ordinals, the rest is aligned at }
  1158. { 1 byte (compatible with GPC/GCC) }
  1159. if is_ordinal(vardef) then
  1160. begin
  1161. sym.fieldoffset:=databitsize;
  1162. l:=sym.getpackedbitsize;
  1163. end
  1164. else
  1165. begin
  1166. databitsize:=_datasize*8;
  1167. sym.fieldoffset:=databitsize;
  1168. if (l>high(asizeint) div 8) then
  1169. Message(sym_e_segment_too_large);
  1170. l:=l*8;
  1171. end;
  1172. if varalign=0 then
  1173. varalign:=size_2_align(l);
  1174. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1175. { bit packed records are limited to high(aint) bits }
  1176. { instead of bytes to avoid double precision }
  1177. { arithmetic in offset calculations }
  1178. if int64(l)>high(asizeint)-sym.fieldoffset then
  1179. begin
  1180. Message(sym_e_segment_too_large);
  1181. _datasize:=high(asizeint);
  1182. databitsize:=high(asizeint);
  1183. end
  1184. else
  1185. begin
  1186. databitsize:=sym.fieldoffset+l;
  1187. _datasize:=(databitsize+7) div 8;
  1188. end;
  1189. { rest is not applicable }
  1190. exit;
  1191. end;
  1192. else
  1193. begin
  1194. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1195. if l>high(asizeint)-sym.fieldoffset then
  1196. begin
  1197. Message(sym_e_segment_too_large);
  1198. _datasize:=high(asizeint);
  1199. end
  1200. else
  1201. _datasize:=sym.fieldoffset+l;
  1202. { Calc alignment needed for this record }
  1203. alignrecord(sym.fieldoffset,varalign);
  1204. end;
  1205. end;
  1206. end;
  1207. function field_alignment_compare(item1, item2: pointer): integer;
  1208. var
  1209. field1: tfieldvarsym absolute item1;
  1210. field2: tfieldvarsym absolute item2;
  1211. begin
  1212. { we don't care about static fields, those become global variables }
  1213. if (sp_static in field1.symoptions) or
  1214. (sp_static in field2.symoptions) then
  1215. exit(0);
  1216. { sort from large to small alignment, and in case of the same alignment
  1217. in declaration order (items declared close together are possibly
  1218. also related and hence possibly used together -> putting them next
  1219. to each other can improve cache behaviour) }
  1220. result:=field2.vardef.alignment-field1.vardef.alignment;
  1221. if result=0 then
  1222. result:=field1.fieldoffset-field2.fieldoffset;
  1223. end;
  1224. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1225. var
  1226. fieldvs, insertfieldvs: tfieldvarsym;
  1227. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1228. i, j, bestfieldindex: longint;
  1229. globalfieldalignment,
  1230. prevglobalfieldalignment,
  1231. newfieldalignment: shortint;
  1232. changed: boolean;
  1233. begin
  1234. if maybereorder and
  1235. (cs_opt_reorder_fields in current_settings.optimizerswitches) then
  1236. begin
  1237. { assign dummy field offsets so we can know their order in the
  1238. sorting routine }
  1239. for i:=0 to list.count-1 do
  1240. tfieldvarsym(list[i]).fieldoffset:=i;
  1241. { sort the non-class fields to minimise losses due to alignment }
  1242. list.sort(@field_alignment_compare);
  1243. { now fill up gaps caused by alignment skips with smaller fields
  1244. where possible }
  1245. repeat
  1246. i:=0;
  1247. base:=_datasize;
  1248. globalfieldalignment:=fieldalignment;
  1249. changed:=false;
  1250. while i<list.count do
  1251. begin
  1252. fieldvs:=tfieldvarsym(list[i]);
  1253. if sp_static in fieldvs.symoptions then
  1254. begin
  1255. inc(i);
  1256. continue;
  1257. end;
  1258. prevglobalfieldalignment:=globalfieldalignment;
  1259. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1260. newfieldalignment:=globalfieldalignment;
  1261. { size of the gap between the end of the previous field and
  1262. the start of the current one }
  1263. space:=fieldoffset-base;
  1264. bestspaceleft:=space;
  1265. while space>0 do
  1266. begin
  1267. bestfieldindex:=-1;
  1268. bestinsertfieldoffset:=-1;
  1269. for j:=i+1 to list.count-1 do
  1270. begin
  1271. insertfieldvs:=tfieldvarsym(list[j]);
  1272. if sp_static in insertfieldvs.symoptions then
  1273. continue;
  1274. insertfieldsize:=insertfieldvs.getsize;
  1275. { can the new field fit possibly in the gap? }
  1276. if insertfieldsize<=space then
  1277. begin
  1278. { restore globalfieldalignment to situation before
  1279. the original field was inserted }
  1280. globalfieldalignment:=prevglobalfieldalignment;
  1281. { at what offset would it be inserted? (this new
  1282. field has its own alignment requirements, which
  1283. may make it impossible to fit after all) }
  1284. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1285. globalfieldalignment:=prevglobalfieldalignment;
  1286. { taking into account the alignment, does it still
  1287. fit and if so, does it fit better than the
  1288. previously found best fit? }
  1289. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1290. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1291. begin
  1292. { new best fit }
  1293. bestfieldindex:=j;
  1294. bestinsertfieldoffset:=insertfieldoffset;
  1295. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1296. if bestspaceleft=0 then
  1297. break;
  1298. end;
  1299. end;
  1300. end;
  1301. { if we didn't find any field to fit, stop trying for this
  1302. gap }
  1303. if bestfieldindex=-1 then
  1304. break;
  1305. changed:=true;
  1306. { we found a field to insert -> adjust the new base
  1307. address }
  1308. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1309. { update globalfieldalignment for this newly inserted
  1310. field }
  1311. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1312. { move the new field before the current one }
  1313. list.move(bestfieldindex,i);
  1314. { and skip the new field (which is now at position i) }
  1315. inc(i);
  1316. { there may be more space left -> continue }
  1317. space:=bestspaceleft;
  1318. end;
  1319. if base>fieldoffset then
  1320. internalerror(2012071302);
  1321. { check the next field }
  1322. base:=fieldoffset+fieldvs.getsize;
  1323. { since the original field had the same or greater alignment
  1324. than anything we inserted before it, the global field
  1325. alignment is still the same now as it was originally after
  1326. inserting that field }
  1327. globalfieldalignment:=newfieldalignment;
  1328. inc(i);
  1329. end;
  1330. { there may be small gaps left *before* inserted fields }
  1331. until not changed;
  1332. end;
  1333. { reset the dummy field offsets }
  1334. for i:=0 to list.count-1 do
  1335. tfieldvarsym(list[i]).fieldoffset:=-1;
  1336. { finally, set the actual field offsets }
  1337. for i:=0 to list.count-1 do
  1338. begin
  1339. fieldvs:=tfieldvarsym(list[i]);
  1340. { static data fields are already inserted in the globalsymtable }
  1341. if not(sp_static in fieldvs.symoptions) then
  1342. begin
  1343. { read_record_fields already set the visibility of the fields,
  1344. because a single list can contain symbols with different
  1345. visibility }
  1346. addfield(fieldvs,fieldvs.visibility);
  1347. end;
  1348. end;
  1349. end;
  1350. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1351. var
  1352. i: longint;
  1353. sym: tsym;
  1354. begin
  1355. { there could be multiple fields in case of a variant record }
  1356. if (defowner.typ=recorddef) and
  1357. trecorddef(defowner).isunion then
  1358. internalerror(2014090403);
  1359. for i:=0 to SymList.count-1 do
  1360. begin
  1361. sym:=tsym(symlist[i]);
  1362. if (sym.typ=fieldvarsym) and
  1363. not(sp_static in sym.symoptions) and
  1364. (tfieldvarsym(sym).fieldoffset>=offset) then
  1365. begin
  1366. result:=tfieldvarsym(sym);
  1367. exit;
  1368. end;
  1369. end;
  1370. result:=nil;
  1371. end;
  1372. procedure tabstractrecordsymtable.addalignmentpadding;
  1373. var
  1374. padded_datasize: asizeint;
  1375. begin
  1376. { make the record size aligned correctly so it can be
  1377. used as elements in an array. For C records we
  1378. use the fieldalignment, because that is updated with the
  1379. used alignment. }
  1380. if (padalignment = 1) then
  1381. case usefieldalignment of
  1382. C_alignment:
  1383. padalignment:=fieldalignment;
  1384. { bitpacked }
  1385. bit_alignment:
  1386. padalignment:=1;
  1387. { mac68k: always round to multiple of 2 }
  1388. mac68k_alignment:
  1389. padalignment:=2;
  1390. { default/no packrecords specified }
  1391. 0:
  1392. padalignment:=recordalignment
  1393. { specific packrecords setting -> use as upper limit }
  1394. else
  1395. padalignment:=min(recordalignment,usefieldalignment);
  1396. end;
  1397. padded_datasize:=align(_datasize,padalignment);
  1398. _paddingsize:=padded_datasize-_datasize;
  1399. _datasize:=padded_datasize;
  1400. end;
  1401. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1402. begin
  1403. { Enums must also be available outside the record scope,
  1404. insert in the owner of this symtable }
  1405. if def.typ=enumdef then
  1406. defowner.owner.insertdef(def)
  1407. else
  1408. inherited insertdef(def);
  1409. end;
  1410. function tabstractrecordsymtable.is_packed: boolean;
  1411. begin
  1412. result:=usefieldalignment=bit_alignment;
  1413. end;
  1414. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1415. var
  1416. i: longint;
  1417. currentsymlist: TFPHashObjectList;
  1418. currentdef: tdef;
  1419. sym: tfieldvarsym;
  1420. begin
  1421. result:=false;
  1422. { If a record contains a union, it does not contain a "single
  1423. non-composite field" in the context of certain ABIs requiring
  1424. special treatment for such records }
  1425. if (defowner.typ=recorddef) and
  1426. trecorddef(defowner).isunion then
  1427. exit;
  1428. { a record/object can contain other things than fields }
  1429. currentsymlist:=symlist;
  1430. { recurse in arrays and records }
  1431. sym:=nil;
  1432. repeat
  1433. { record has one field? }
  1434. for i:=0 to currentsymlist.Count-1 do
  1435. begin
  1436. if (tsym(currentsymlist[i]).typ=fieldvarsym) and
  1437. not(sp_static in tsym(currentsymlist[i]).symoptions) then
  1438. begin
  1439. if result then
  1440. begin
  1441. result:=false;
  1442. exit;
  1443. end;
  1444. result:=true;
  1445. sym:=tfieldvarsym(currentsymlist[i])
  1446. end;
  1447. end;
  1448. if assigned(sym) then
  1449. begin
  1450. { if the field is an array, does it contain one element? }
  1451. currentdef:=sym.vardef;
  1452. while (currentdef.typ=arraydef) and
  1453. not is_special_array(currentdef) do
  1454. begin
  1455. if tarraydef(currentdef).elecount<>1 then
  1456. begin
  1457. result:=false;
  1458. exit;
  1459. end;
  1460. currentdef:=tarraydef(currentdef).elementdef;
  1461. end;
  1462. { if the array element is again a record, continue descending }
  1463. if currentdef.typ=recorddef then
  1464. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1465. else
  1466. begin
  1467. { otherwise we found the type of the single element }
  1468. def:=currentdef;
  1469. exit;
  1470. end;
  1471. end
  1472. else
  1473. exit
  1474. until false;
  1475. end;
  1476. function tabstractrecordsymtable.get_unit_symtable: tsymtable;
  1477. begin
  1478. result:=defowner.owner;
  1479. while assigned(result) and (result.symtabletype in [ObjectSymtable,recordsymtable]) do
  1480. result:=result.defowner.owner;
  1481. end;
  1482. procedure tabstractrecordsymtable.do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  1483. var
  1484. sym : tsym absolute data;
  1485. fsym : tfieldvarsym absolute data;
  1486. mop : tmanagementoperator absolute arg;
  1487. entry : pmanagementoperator_offset_entry;
  1488. sublist : tfplist;
  1489. i : longint;
  1490. begin
  1491. if sym.typ<>fieldvarsym then
  1492. exit;
  1493. if not is_record(fsym.vardef) and not is_object(fsym.vardef) and not is_cppclass(fsym.vardef) then
  1494. exit;
  1495. if not assigned(mop_list[mop]) then
  1496. internalerror(2018082303);
  1497. if is_record(fsym.vardef) then
  1498. begin
  1499. if mop in trecordsymtable(trecorddef(fsym.vardef).symtable).managementoperators then
  1500. begin
  1501. new(entry);
  1502. entry^.pd:=search_management_operator(mop,fsym.vardef);
  1503. if not assigned(entry^.pd) then
  1504. internalerror(2018082302);
  1505. entry^.offset:=fsym.fieldoffset;
  1506. mop_list[mop].add(entry);
  1507. end;
  1508. end;
  1509. sublist:=tfplist.create;
  1510. tabstractrecordsymtable(tabstractrecorddef(fsym.vardef).symtable).get_managementoperator_offset_list(mop,sublist);
  1511. for i:=0 to sublist.count-1 do
  1512. begin
  1513. entry:=pmanagementoperator_offset_entry(sublist[i]);
  1514. entry^.offset:=entry^.offset+fsym.fieldoffset;
  1515. mop_list[mop].add(entry);
  1516. end;
  1517. { we don't need to remove the entries as they become part of list }
  1518. sublist.free;
  1519. end;
  1520. procedure tabstractrecordsymtable.get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  1521. var
  1522. i : longint;
  1523. entry,entrycopy : pmanagementoperator_offset_entry;
  1524. begin
  1525. if not assigned(list) then
  1526. internalerror(2018082301);
  1527. if mop=mop_none then
  1528. exit;
  1529. if not (mop in has_fields_with_mop) then
  1530. { none of the fields or one of the field's fields has the requested operator }
  1531. exit;
  1532. if not assigned(mop_list[mop]) then
  1533. begin
  1534. mop_list[mop]:=tfplist.create;
  1535. SymList.ForEachCall(@do_get_managementoperator_offset_list,pointer(ptruint(mop)));
  1536. end;
  1537. for i:=0 to mop_list[mop].count-1 do
  1538. begin
  1539. entry:=pmanagementoperator_offset_entry(mop_list[mop][i]);
  1540. New(entrycopy);
  1541. entrycopy^:=entry^;
  1542. list.add(entrycopy);
  1543. end;
  1544. end;
  1545. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1546. begin
  1547. _datasize:=val;
  1548. if (usefieldalignment=bit_alignment) then
  1549. { can overflow in non bitpacked records }
  1550. databitsize:=val*8;
  1551. end;
  1552. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1553. var
  1554. l : asizeint;
  1555. varalignfield,
  1556. varalign : shortint;
  1557. vardef : tdef;
  1558. begin
  1559. { Calculate field offset }
  1560. l:=sym.getsize;
  1561. vardef:=sym.vardef;
  1562. varalign:=vardef.structalignment;
  1563. case usefieldalignment of
  1564. bit_alignment:
  1565. { has to be handled separately }
  1566. internalerror(2012071301);
  1567. C_alignment:
  1568. begin
  1569. { Calc the alignment size for C style records }
  1570. if (varalign>4) and
  1571. ((varalign mod 4)<>0) and
  1572. (vardef.typ=arraydef) then
  1573. Message1(sym_w_wrong_C_pack,vardef.typename);
  1574. if varalign=0 then
  1575. varalign:=l;
  1576. if (globalfieldalignment<maxCrecordalign) then
  1577. begin
  1578. if (varalign>16) and (globalfieldalignment<32) then
  1579. globalfieldalignment:=32
  1580. else if (varalign>12) and (globalfieldalignment<16) then
  1581. globalfieldalignment:=16
  1582. { 12 is needed for long double }
  1583. else if (varalign>8) and (globalfieldalignment<12) then
  1584. globalfieldalignment:=12
  1585. else if (varalign>4) and (globalfieldalignment<8) then
  1586. globalfieldalignment:=8
  1587. else if (varalign>2) and (globalfieldalignment<4) then
  1588. globalfieldalignment:=4
  1589. else if (varalign>1) and (globalfieldalignment<2) then
  1590. globalfieldalignment:=2;
  1591. end;
  1592. globalfieldalignment:=min(globalfieldalignment,maxCrecordalign);
  1593. end;
  1594. mac68k_alignment:
  1595. begin
  1596. { mac68k alignment (C description):
  1597. * char is aligned to 1 byte
  1598. * everything else (except vector) is aligned to 2 bytes
  1599. * vector is aligned to 16 bytes
  1600. }
  1601. if l>1 then
  1602. globalfieldalignment:=2
  1603. else
  1604. globalfieldalignment:=1;
  1605. varalign:=2;
  1606. end;
  1607. end;
  1608. if varalign=0 then
  1609. varalign:=size_2_align(l);
  1610. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1611. result:=align(base,varalignfield);
  1612. end;
  1613. function tabstractrecordsymtable.iscurrentunit: boolean;
  1614. begin
  1615. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1616. end;
  1617. {****************************************************************************
  1618. TRecordSymtable
  1619. ****************************************************************************}
  1620. constructor trecordsymtable.create(const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1621. begin
  1622. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1623. symtabletype:=recordsymtable;
  1624. end;
  1625. { this procedure is reserved for inserting case variant into
  1626. a record symtable }
  1627. { the offset is the location of the start of the variant
  1628. and datasize and dataalignment corresponds to
  1629. the complete size (see code in pdecl unit) PM }
  1630. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : asizeint);
  1631. var
  1632. sym : tsym;
  1633. def : tdef;
  1634. i : integer;
  1635. varalignrecord,varalign,
  1636. storesize,storealign : asizeint;
  1637. bitsize: tcgint;
  1638. begin
  1639. storesize:=_datasize;
  1640. storealign:=fieldalignment;
  1641. _datasize:=offset;
  1642. if (usefieldalignment=bit_alignment) then
  1643. databitsize:=offset*8;
  1644. { We move the ownership of the defs and symbols to the new recordsymtable.
  1645. The old unionsymtable keeps the references, but doesn't own the
  1646. objects anymore }
  1647. unionst.DefList.OwnsObjects:=false;
  1648. unionst.SymList.OwnsObjects:=false;
  1649. { copy symbols }
  1650. for i:=0 to unionst.SymList.Count-1 do
  1651. begin
  1652. sym:=TSym(unionst.SymList[i]);
  1653. if sym.typ<>fieldvarsym then
  1654. internalerror(200601272);
  1655. if tfieldvarsym(sym).fieldoffset=0 then
  1656. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1657. { add to this record symtable, checking for duplicate names }
  1658. // unionst.SymList.List.List^[i].Data:=nil;
  1659. insert(sym);
  1660. varalign:=tfieldvarsym(sym).vardef.alignment;
  1661. if varalign=0 then
  1662. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1663. { retrieve size }
  1664. if (usefieldalignment=bit_alignment) then
  1665. begin
  1666. { bit packed records are limited to high(aint) bits }
  1667. { instead of bytes to avoid double precision }
  1668. { arithmetic in offset calculations }
  1669. if is_ordinal(tfieldvarsym(sym).vardef) then
  1670. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1671. else
  1672. begin
  1673. bitsize:=tfieldvarsym(sym).getsize;
  1674. if (bitsize>high(asizeint) div 8) then
  1675. Message(sym_e_segment_too_large);
  1676. bitsize:=bitsize*8;
  1677. end;
  1678. if bitsize>high(asizeint)-databitsize then
  1679. begin
  1680. Message(sym_e_segment_too_large);
  1681. _datasize:=high(asizeint);
  1682. databitsize:=high(asizeint);
  1683. end
  1684. else
  1685. begin
  1686. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1687. _datasize:=(databitsize+7) div 8;
  1688. end;
  1689. tfieldvarsym(sym).fieldoffset:=databitsize;
  1690. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1691. end
  1692. else
  1693. begin
  1694. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1695. begin
  1696. Message(sym_e_segment_too_large);
  1697. _datasize:=high(asizeint);
  1698. end
  1699. else
  1700. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1701. { update address }
  1702. tfieldvarsym(sym).fieldoffset:=_datasize;
  1703. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1704. end;
  1705. { update alignment of this record }
  1706. if (usefieldalignment<>C_alignment) and
  1707. (usefieldalignment<>mac68k_alignment) then
  1708. recordalignment:=max(recordalignment,varalignrecord);
  1709. end;
  1710. { update alignment for C records }
  1711. if (usefieldalignment=C_alignment) and
  1712. (usefieldalignment<>mac68k_alignment) then
  1713. recordalignment:=max(recordalignment,unionst.recordalignment);
  1714. { Register defs in the new record symtable }
  1715. for i:=0 to unionst.DefList.Count-1 do
  1716. begin
  1717. def:=TDef(unionst.DefList[i]);
  1718. def.ChangeOwner(self);
  1719. end;
  1720. _datasize:=storesize;
  1721. fieldalignment:=storealign;
  1722. { If a record contains a union, it does not contain a "single
  1723. non-composite field" in the context of certain ABIs requiring
  1724. special treatment for such records }
  1725. if defowner.typ=recorddef then
  1726. trecorddef(defowner).isunion:=true;
  1727. end;
  1728. procedure trecordsymtable.includemanagementoperator(mop:tmanagementoperator);
  1729. begin
  1730. if mop in managementoperators then
  1731. exit;
  1732. include(managementoperators,mop);
  1733. end;
  1734. {****************************************************************************
  1735. TObjectSymtable
  1736. ****************************************************************************}
  1737. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign,recordmaxCalign:shortint);
  1738. begin
  1739. inherited create(n,usealign,recordminalign,recordmaxCalign);
  1740. symtabletype:=ObjectSymtable;
  1741. defowner:=adefowner;
  1742. end;
  1743. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1744. var
  1745. hsym: tsym;
  1746. warn: boolean;
  1747. begin
  1748. result:=false;
  1749. if not assigned(defowner) then
  1750. internalerror(200602061);
  1751. { procsym and propertysym have special code
  1752. to override values in inherited classes. For other
  1753. symbols check for duplicates }
  1754. if not(sym.typ in [procsym,propertysym]) then
  1755. begin
  1756. { but private ids can be reused }
  1757. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1758. if assigned(hsym) and
  1759. (
  1760. (
  1761. not(m_delphi in current_settings.modeswitches) and
  1762. is_visible_for_object(hsym,tobjectdef(defowner))
  1763. ) or
  1764. (
  1765. { In Delphi, you can repeat members of a parent class. You can't }
  1766. { do this for objects however, and you (obviouly) can't }
  1767. { declare two fields with the same name in a single class }
  1768. (m_delphi in current_settings.modeswitches) and
  1769. (
  1770. is_object(tdef(defowner)) or
  1771. (hsym.owner = self)
  1772. )
  1773. )
  1774. ) then
  1775. begin
  1776. { only watn when a parameter/local variable in a method
  1777. conflicts with a category method, because this can easily
  1778. happen due to all possible categories being imported via
  1779. CocoaAll }
  1780. warn:=
  1781. (is_objccategory(tdef(hsym.owner.defowner)) or
  1782. is_classhelper(tdef(hsym.owner.defowner))) and
  1783. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1784. DuplicateSym(hashedid,sym,hsym,warn);
  1785. result:=true;
  1786. end;
  1787. end
  1788. else
  1789. result:=inherited checkduplicate(hashedid,sym);
  1790. end;
  1791. {$ifdef llvm}
  1792. {****************************************************************************
  1793. tLlvmShadowSymtableEntry
  1794. ****************************************************************************}
  1795. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1796. begin
  1797. fdef:=def;
  1798. ffieldoffset:=fieldoffset;
  1799. end;
  1800. {****************************************************************************
  1801. TLlvmShadowSymtable
  1802. ****************************************************************************}
  1803. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1804. begin
  1805. result:=get_by_llvm_index(f.llvmfieldnr)
  1806. end;
  1807. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1808. begin
  1809. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1810. end;
  1811. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1812. begin
  1813. equivst:=st;
  1814. curroffset:=0;
  1815. symdeflist:=tfpobjectlist.create(true);
  1816. generate;
  1817. end;
  1818. destructor tllvmshadowsymtable.destroy;
  1819. begin
  1820. symdeflist.free;
  1821. end;
  1822. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1823. var
  1824. sizectr,
  1825. tmpsize: aint;
  1826. begin
  1827. case equivst.usefieldalignment of
  1828. bit_alignment:
  1829. begin
  1830. { curoffset: bit address after the previous field. }
  1831. { llvm has no special support for bitfields in records, }
  1832. { so we replace them with plain bytes. }
  1833. { as soon as a single bit of a byte is allocated, we }
  1834. { allocate the byte in the llvm shadow record }
  1835. if (fieldoffset>curroffset) then
  1836. curroffset:=align(curroffset,8);
  1837. { fields in bitpacked records always start either right }
  1838. { after the previous one, or at the next byte boundary. }
  1839. if (curroffset<>fieldoffset) then
  1840. internalerror(2008051002);
  1841. if is_ordinal(vardef) then
  1842. begin
  1843. tmpsize:=vardef.packedbitsize;
  1844. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1845. inc(curroffset,tmpsize);
  1846. tmpsize:=0;
  1847. while sizectr<>0 do
  1848. begin
  1849. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1850. dec(sizectr);
  1851. inc(tmpsize);
  1852. end;
  1853. end
  1854. else
  1855. begin
  1856. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1857. if not(derefclass) then
  1858. inc(curroffset,vardef.size*8)
  1859. else
  1860. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1861. end;
  1862. end
  1863. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1864. begin
  1865. { curoffset: address right after the previous field }
  1866. while (fieldoffset>curroffset) do
  1867. begin
  1868. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1869. inc(curroffset);
  1870. end;
  1871. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1872. if not(derefclass) then
  1873. inc(curroffset,vardef.size)
  1874. else
  1875. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1876. end
  1877. else
  1878. { default for llvm, don't add explicit padding }
  1879. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1880. end
  1881. end;
  1882. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1883. begin
  1884. case equivst.usefieldalignment of
  1885. { already correct in this case }
  1886. bit_alignment:
  1887. ;
  1888. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1889. begin
  1890. { add padding fields }
  1891. while (finalsize>curroffset) do
  1892. begin
  1893. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1894. inc(curroffset);
  1895. end;
  1896. end;
  1897. end;
  1898. end;
  1899. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1900. var
  1901. sym: tfieldvarsym;
  1902. lastoffset: aint;
  1903. newalignment: aint;
  1904. i, j: longint;
  1905. begin
  1906. i:=0;
  1907. while (i<equivst.symlist.count) do
  1908. begin
  1909. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1910. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  1911. begin
  1912. inc(i);
  1913. continue;
  1914. end;
  1915. sym:=tfieldvarsym(equivst.symlist[i]);
  1916. { a "better" algorithm might be to use the largest }
  1917. { variant in case of (bit)packing, since then }
  1918. { alignment doesn't matter }
  1919. if (vo_is_first_field in sym.varoptions) then
  1920. begin
  1921. { we assume that all fields are processed in order. }
  1922. if (variantstarts.count<>0) then
  1923. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1924. else
  1925. lastoffset:=-1;
  1926. { new variant at same level as last one: use if higher alignment }
  1927. if (lastoffset=sym.fieldoffset) then
  1928. begin
  1929. if (equivst.fieldalignment<>bit_alignment) then
  1930. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1931. else
  1932. newalignment:=1;
  1933. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1934. variantstarts[variantstarts.count-1]:=sym;
  1935. end
  1936. { variant at deeper level than last one -> add }
  1937. else if (lastoffset<sym.fieldoffset) then
  1938. variantstarts.add(sym)
  1939. else
  1940. begin
  1941. { a variant at a less deep level, so backtrack }
  1942. j:=variantstarts.count-2;
  1943. while (j>=0) do
  1944. begin
  1945. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1946. break;
  1947. dec(j);
  1948. end;
  1949. if (j<0) then
  1950. internalerror(2008051003);
  1951. { new variant has higher alignment? }
  1952. if (equivst.fieldalignment<>bit_alignment) then
  1953. newalignment:=used_align(sym.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  1954. else
  1955. newalignment:=1;
  1956. { yes, replace and remove previous nested variants }
  1957. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1958. begin
  1959. variantstarts[j]:=sym;
  1960. variantstarts.count:=j+1;
  1961. end
  1962. { no, skip this variant }
  1963. else
  1964. begin
  1965. inc(i);
  1966. while (i<equivst.symlist.count) and
  1967. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1968. (sp_static in tsym(equivst.symlist[i]).symoptions) or
  1969. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  1970. inc(i);
  1971. continue;
  1972. end;
  1973. end;
  1974. end;
  1975. inc(i);
  1976. end;
  1977. end;
  1978. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1979. var
  1980. lastvaroffsetprocessed: aint;
  1981. i, equivcount, varcount: longint;
  1982. begin
  1983. { if it's an object/class, the first entry is the parent (if there is one) }
  1984. if (equivst.symtabletype=objectsymtable) and
  1985. assigned(tobjectdef(equivst.defowner).childof) then
  1986. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  1987. equivcount:=equivst.symlist.count;
  1988. varcount:=0;
  1989. i:=0;
  1990. lastvaroffsetprocessed:=-1;
  1991. while (i<equivcount) do
  1992. begin
  1993. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1994. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  1995. begin
  1996. inc(i);
  1997. continue;
  1998. end;
  1999. { start of a new variant? }
  2000. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  2001. begin
  2002. { if we want to process the same variant offset twice, it means that we }
  2003. { got to the end and are trying to process the next variant part -> stop }
  2004. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  2005. break;
  2006. if (varcount>=variantstarts.count) then
  2007. internalerror(2008051005);
  2008. { new variant part -> use the one with the biggest alignment }
  2009. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  2010. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  2011. inc(varcount);
  2012. if (i<0) then
  2013. internalerror(2008051004);
  2014. end;
  2015. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  2016. inc(i);
  2017. end;
  2018. addalignmentpadding(equivst.datasize);
  2019. end;
  2020. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  2021. var
  2022. i, varcount: longint;
  2023. shadowindex: longint;
  2024. equivcount : longint;
  2025. begin
  2026. varcount:=0;
  2027. shadowindex:=0;
  2028. equivcount:=equivst.symlist.count;
  2029. i:=0;
  2030. while (i < equivcount) do
  2031. begin
  2032. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  2033. (sp_static in tsym(equivst.symlist[i]).symoptions) then
  2034. begin
  2035. inc(i);
  2036. continue;
  2037. end;
  2038. { start of a new variant? }
  2039. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  2040. begin
  2041. { back up to a less deeply nested variant level? }
  2042. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  2043. dec(varcount);
  2044. { it's possible that some variants are more deeply nested than the
  2045. one we recorded in the shadowsymtable (since we recorded the one
  2046. with the biggest alignment, not necessarily the biggest one in size
  2047. }
  2048. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  2049. varcount:=variantstarts.count-1
  2050. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  2051. internalerror(2008051006);
  2052. { reset the shadowindex to the start of this variant. }
  2053. { in case the llvmfieldnr is not (yet) set for this }
  2054. { field, shadowindex will simply be reset to zero and }
  2055. { we'll start searching from the start of the record }
  2056. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  2057. if (varcount<pred(variantstarts.count)) then
  2058. inc(varcount);
  2059. end;
  2060. { find the last shadowfield whose offset <= the current field's offset }
  2061. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  2062. (shadowindex<symdeflist.count-1) and
  2063. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  2064. inc(shadowindex);
  2065. { set the field number and potential offset from that field (in case }
  2066. { of overlapping variants) }
  2067. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  2068. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  2069. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  2070. inc(i);
  2071. end;
  2072. end;
  2073. procedure tllvmshadowsymtable.generate;
  2074. var
  2075. variantstarts: tfplist;
  2076. begin
  2077. variantstarts:=tfplist.create;
  2078. { first go through the entire record and }
  2079. { store the fieldvarsyms of the variants }
  2080. { with the highest alignment }
  2081. findvariantstarts(variantstarts);
  2082. { now go through the regular fields and the selected variants, }
  2083. { and add them to the llvm shadow record symtable }
  2084. buildtable(variantstarts);
  2085. { finally map all original fields to the llvm definition }
  2086. buildmapping(variantstarts);
  2087. variantstarts.free;
  2088. end;
  2089. {$endif llvm}
  2090. {****************************************************************************
  2091. TAbstractSubSymtable
  2092. ****************************************************************************}
  2093. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  2094. var
  2095. oldtyp : byte;
  2096. begin
  2097. oldtyp:=ppufile.entrytyp;
  2098. ppufile.entrytyp:=subentryid;
  2099. inherited ppuwrite(ppufile);
  2100. ppufile.entrytyp:=oldtyp;
  2101. end;
  2102. {****************************************************************************
  2103. TAbstractLocalSymtable
  2104. ****************************************************************************}
  2105. function tabstractlocalsymtable.count_locals:longint;
  2106. var
  2107. i : longint;
  2108. sym : tsym;
  2109. begin
  2110. result:=0;
  2111. for i:=0 to SymList.Count-1 do
  2112. begin
  2113. sym:=tsym(SymList[i]);
  2114. { Count only varsyms, but ignore the funcretsym }
  2115. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  2116. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  2117. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  2118. (tstoredsym(sym).refs>0)) then
  2119. inc(result);
  2120. end;
  2121. end;
  2122. function tabstractlocalsymtable.iscurrentunit: boolean;
  2123. begin
  2124. Result:=
  2125. assigned(defowner) and
  2126. defowner.owner.iscurrentunit;
  2127. end;
  2128. {****************************************************************************
  2129. TLocalSymtable
  2130. ****************************************************************************}
  2131. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  2132. begin
  2133. inherited create('');
  2134. defowner:=adefowner;
  2135. symtabletype:=localsymtable;
  2136. symtablelevel:=level;
  2137. end;
  2138. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2139. var
  2140. hsym : tsym;
  2141. begin
  2142. if not assigned(defowner) or
  2143. (defowner.typ<>procdef) then
  2144. internalerror(200602042);
  2145. result:=false;
  2146. hsym:=tsym(FindWithHash(hashedid));
  2147. if assigned(hsym) then
  2148. begin
  2149. { a local and the function can have the same
  2150. name in TP and Delphi, but RESULT not }
  2151. if (m_duplicate_names in current_settings.modeswitches) and
  2152. (hsym.typ in [absolutevarsym,localvarsym]) and
  2153. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  2154. not((m_result in current_settings.modeswitches) and
  2155. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  2156. HideSym(hsym)
  2157. else
  2158. DuplicateSym(hashedid,sym,hsym,false);
  2159. result:=true;
  2160. exit;
  2161. end;
  2162. { check also parasymtable, this needs to be done here because
  2163. of the special situation with the funcret sym that needs to be
  2164. hidden for tp and delphi modes }
  2165. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  2166. if assigned(hsym) then
  2167. begin
  2168. { a local and the function can have the same
  2169. name in TP and Delphi, but RESULT not }
  2170. if (m_duplicate_names in current_settings.modeswitches) and
  2171. (sym.typ in [absolutevarsym,localvarsym]) and
  2172. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  2173. not((m_result in current_settings.modeswitches) and
  2174. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  2175. Hidesym(sym)
  2176. else
  2177. DuplicateSym(hashedid,sym,hsym,false);
  2178. result:=true;
  2179. exit;
  2180. end;
  2181. { check ObjectSymtable, skip this for funcret sym because
  2182. that will always be positive because it has the same name
  2183. as the procsym }
  2184. if not is_funcret_sym(sym) and
  2185. (defowner.typ=procdef) and
  2186. assigned(tprocdef(defowner).struct) and
  2187. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2188. (
  2189. not(m_delphi in current_settings.modeswitches) or
  2190. is_object(tprocdef(defowner).struct)
  2191. ) then
  2192. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2193. end;
  2194. {****************************************************************************
  2195. TParaSymtable
  2196. ****************************************************************************}
  2197. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2198. begin
  2199. inherited create('');
  2200. readonly:=false;
  2201. defowner:=adefowner;
  2202. symtabletype:=parasymtable;
  2203. symtablelevel:=level;
  2204. end;
  2205. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2206. begin
  2207. result:=inherited checkduplicate(hashedid,sym);
  2208. if result then
  2209. exit;
  2210. if not(m_duplicate_names in current_settings.modeswitches) and
  2211. assigned(defowner) and (defowner.typ=procdef) and
  2212. assigned(tprocdef(defowner).struct) and
  2213. assigned(tprocdef(defowner).owner) and
  2214. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2215. (
  2216. not(m_delphi in current_settings.modeswitches) or
  2217. is_object(tprocdef(defowner).struct)
  2218. ) then
  2219. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2220. end;
  2221. procedure tparasymtable.insertdef(def: TDefEntry);
  2222. begin
  2223. if readonly then
  2224. defowner.owner.insertdef(def)
  2225. else
  2226. inherited insertdef(def);
  2227. end;
  2228. {****************************************************************************
  2229. TAbstractUniTSymtable
  2230. ****************************************************************************}
  2231. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2232. begin
  2233. inherited create(n);
  2234. moduleid:=id;
  2235. end;
  2236. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2237. var
  2238. hsym : tsym;
  2239. begin
  2240. result:=false;
  2241. hsym:=tsym(FindWithHash(hashedid));
  2242. if assigned(hsym) then
  2243. begin
  2244. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2245. exit;
  2246. if hsym.typ=symconst.namespacesym then
  2247. begin
  2248. case sym.typ of
  2249. symconst.namespacesym:;
  2250. symconst.unitsym:
  2251. begin
  2252. 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 }
  2253. tnamespacesym(hsym).unitsym:=tsym(sym);
  2254. end
  2255. else
  2256. HideSym(hsym);
  2257. end;
  2258. end
  2259. else
  2260. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2261. unit, the unit can then not be accessed anymore using
  2262. <unit>.<id>, so we can hide the symbol.
  2263. Do the same if we add a namespace and there is a unit with the same name }
  2264. if (hsym.typ=symconst.unitsym) and
  2265. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2266. begin
  2267. HideSym(hsym);
  2268. if sym.typ=symconst.namespacesym then
  2269. tnamespacesym(sym).unitsym:=tsym(hsym);
  2270. end
  2271. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2272. in this case, copy the isoindex and make the original symbol invisible }
  2273. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2274. and (tprogramparasym(hsym).isoindex<>0) then
  2275. begin
  2276. HideSym(hsym);
  2277. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2278. end
  2279. else
  2280. DuplicateSym(hashedid,sym,hsym,false);
  2281. result:=true;
  2282. exit;
  2283. end;
  2284. end;
  2285. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2286. begin
  2287. result:=find(n);
  2288. if assigned(result)and(result.typ<>namespacesym)then
  2289. result:=nil;
  2290. end;
  2291. function tabstractuniTSymtable.iscurrentunit:boolean;
  2292. begin
  2293. result:=assigned(current_module) and
  2294. (
  2295. (current_module.globalsymtable=self) or
  2296. (current_module.localsymtable=self)
  2297. );
  2298. end;
  2299. function tabstractuniTSymtable.needs_init_final: boolean;
  2300. begin
  2301. if not init_final_check_done then
  2302. begin
  2303. result:=inherited needs_init_final;
  2304. if not result then
  2305. begin
  2306. result:=has_class_condestructors;
  2307. if result then
  2308. include(tableoptions,sto_needs_init_final);
  2309. end;
  2310. end;
  2311. result:=sto_needs_init_final in tableoptions;
  2312. end;
  2313. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2314. var
  2315. p:integer;
  2316. n,ns:string;
  2317. oldsym:TSymEntry;
  2318. begin
  2319. insert(sym);
  2320. n:=sym.realname;
  2321. p:=pos('.',n);
  2322. ns:='';
  2323. while p>0 do
  2324. begin
  2325. if ns='' then
  2326. ns:=copy(n,1,p-1)
  2327. else
  2328. ns:=ns+'.'+copy(n,1,p-1);
  2329. system.delete(n,1,p);
  2330. oldsym:=findnamespace(upper(ns));
  2331. if not assigned(oldsym) then
  2332. insert(cnamespacesym.create(ns));
  2333. p:=pos('.',n);
  2334. end;
  2335. end;
  2336. procedure CheckForClassConDestructors(p:TObject;arg:pointer);
  2337. var
  2338. result: pboolean absolute arg;
  2339. begin
  2340. if result^ then
  2341. exit;
  2342. if (tdef(p).typ in [objectdef,recorddef]) and
  2343. not (df_generic in tdef(p).defoptions) then
  2344. begin
  2345. { first check the class... }
  2346. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  2347. result^:=true;;
  2348. { ... and then also check all subclasses }
  2349. if not result^ then
  2350. tabstractrecorddef(p).symtable.deflist.foreachcall(@CheckForClassConDestructors,arg);
  2351. end;
  2352. end;
  2353. function tabstractuniTSymtable.has_class_condestructors: boolean;
  2354. begin
  2355. result:=false;
  2356. deflist.foreachcall(@CheckForClassConDestructors,@result);
  2357. end;
  2358. {****************************************************************************
  2359. TStaticSymtable
  2360. ****************************************************************************}
  2361. constructor tstaticsymtable.create(const n : string;id:word);
  2362. begin
  2363. inherited create(n,id);
  2364. symtabletype:=staticsymtable;
  2365. symtablelevel:=main_program_level;
  2366. currentvisibility:=vis_private;
  2367. end;
  2368. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2369. begin
  2370. inherited ppuload(ppufile);
  2371. { now we can deref the syms and defs }
  2372. deref(false);
  2373. end;
  2374. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2375. begin
  2376. inherited ppuwrite(ppufile);
  2377. end;
  2378. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2379. begin
  2380. result:=inherited checkduplicate(hashedid,sym);
  2381. if not result and
  2382. (current_module.localsymtable=self) and
  2383. assigned(current_module.globalsymtable) then
  2384. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2385. end;
  2386. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2387. begin
  2388. result:=inherited findnamespace(n);
  2389. if not assigned(result) and
  2390. (current_module.localsymtable=self) and
  2391. assigned(current_module.globalsymtable) then
  2392. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2393. end;
  2394. {****************************************************************************
  2395. TGlobalSymtable
  2396. ****************************************************************************}
  2397. constructor tglobalsymtable.create(const n : string;id:word);
  2398. begin
  2399. inherited create(n,id);
  2400. symtabletype:=globalsymtable;
  2401. symtablelevel:=main_program_level;
  2402. end;
  2403. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2404. begin
  2405. inherited ppuload(ppufile);
  2406. { now we can deref the syms and defs }
  2407. deref(false);
  2408. end;
  2409. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2410. begin
  2411. { write the symtable entries }
  2412. inherited ppuwrite(ppufile);
  2413. end;
  2414. {*****************************************************************************
  2415. tspecializesymtable
  2416. *****************************************************************************}
  2417. constructor tspecializesymtable.create(const n : string;id:word);
  2418. begin
  2419. inherited create(n,id);
  2420. { the specialize symtable does not own the syms and defs as they are all
  2421. moved to a different symtable before the symtable is destroyed; this
  2422. avoids calls to "extract" }
  2423. symlist.ownsobjects:=false;
  2424. deflist.ownsobjects:=false;
  2425. end;
  2426. function tspecializesymtable.iscurrentunit: boolean;
  2427. begin
  2428. Result:=true;
  2429. end;
  2430. {****************************************************************************
  2431. TWITHSYMTABLE
  2432. ****************************************************************************}
  2433. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2434. begin
  2435. inherited create('');
  2436. symtabletype:=withsymtable;
  2437. withrefnode:=refnode;
  2438. { Replace SymList with the passed symlist }
  2439. SymList.free;
  2440. SymList:=ASymList;
  2441. defowner:=aowner;
  2442. end;
  2443. destructor twithsymtable.destroy;
  2444. begin
  2445. if refcount>1 then
  2446. exit;
  2447. withrefnode.free;
  2448. { Disable SymList because we don't Own it }
  2449. SymList:=nil;
  2450. inherited destroy;
  2451. end;
  2452. procedure twithsymtable.clear;
  2453. begin
  2454. { remove no entry from a withsymtable as it is only a pointer to the
  2455. recorddef or objectdef symtable }
  2456. end;
  2457. procedure twithsymtable.insertdef(def:TDefEntry);
  2458. begin
  2459. { Definitions can't be registered in the withsymtable
  2460. because the withsymtable is removed after the with block.
  2461. We can't easily solve it here because the next symtable in the
  2462. stack is not known. }
  2463. internalerror(200602046);
  2464. end;
  2465. {****************************************************************************
  2466. TSTT_ExceptionSymtable
  2467. ****************************************************************************}
  2468. constructor tstt_excepTSymtable.create;
  2469. begin
  2470. inherited create('');
  2471. symtabletype:=stt_excepTSymtable;
  2472. end;
  2473. {****************************************************************************
  2474. TMacroSymtable
  2475. ****************************************************************************}
  2476. constructor tmacrosymtable.create(exported: boolean);
  2477. begin
  2478. inherited create('');
  2479. if exported then
  2480. symtabletype:=exportedmacrosymtable
  2481. else
  2482. symtabletype:=localmacrosymtable;
  2483. symtablelevel:=main_program_level;
  2484. end;
  2485. {****************************************************************************
  2486. TEnumSymtable
  2487. ****************************************************************************}
  2488. procedure tenumsymtable.insert(sym: TSymEntry; checkdup: boolean);
  2489. var
  2490. value: longint;
  2491. def: tenumdef;
  2492. begin
  2493. // defowner = nil only when we are loading from ppu
  2494. if defowner<>nil then
  2495. begin
  2496. { First entry? Then we need to set the minval }
  2497. value:=tenumsym(sym).value;
  2498. def:=tenumdef(defowner);
  2499. if SymList.count=0 then
  2500. begin
  2501. if value>0 then
  2502. def.has_jumps:=true;
  2503. def.setmin(value);
  2504. def.setmax(value);
  2505. end
  2506. else
  2507. begin
  2508. { check for jumps }
  2509. if value>def.max+1 then
  2510. def.has_jumps:=true;
  2511. { update low and high }
  2512. if def.min>value then
  2513. def.setmin(value);
  2514. if def.max<value then
  2515. def.setmax(value);
  2516. end;
  2517. end;
  2518. inherited insert(sym, checkdup);
  2519. end;
  2520. constructor tenumsymtable.create(adefowner: tdef);
  2521. begin
  2522. inherited Create('');
  2523. symtabletype:=enumsymtable;
  2524. defowner:=adefowner;
  2525. end;
  2526. {****************************************************************************
  2527. TArraySymtable
  2528. ****************************************************************************}
  2529. procedure tarraysymtable.insertdef(def: TDefEntry);
  2530. begin
  2531. { Enums must also be available outside the record scope,
  2532. insert in the owner of this symtable }
  2533. if def.typ=enumdef then
  2534. defowner.owner.insertdef(def)
  2535. else
  2536. inherited insertdef(def);
  2537. end;
  2538. constructor tarraysymtable.create(adefowner: tdef);
  2539. begin
  2540. inherited Create('');
  2541. symtabletype:=arraysymtable;
  2542. defowner:=adefowner;
  2543. end;
  2544. {*****************************************************************************
  2545. Helper Routines
  2546. *****************************************************************************}
  2547. function FullTypeName(def,otherdef:tdef):string;
  2548. var
  2549. s1,s2 : string;
  2550. begin
  2551. if def.typ in [objectdef,recorddef] then
  2552. s1:=tabstractrecorddef(def).RttiName
  2553. else
  2554. s1:=def.typename;
  2555. { When the names are the same try to include the unit name }
  2556. if assigned(otherdef) and
  2557. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2558. begin
  2559. s2:=otherdef.typename;
  2560. if upper(s1)=upper(s2) then
  2561. s1:=def.owner.realname^+'.'+s1;
  2562. end;
  2563. FullTypeName:=s1;
  2564. end;
  2565. function generate_nested_name(symtable:tsymtable;delimiter:string):string;
  2566. begin
  2567. result:='';
  2568. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2569. begin
  2570. if (result='') then
  2571. if symtable.name<>nil then
  2572. result:=symtable.name^
  2573. else
  2574. else
  2575. if symtable.name<>nil then
  2576. result:=symtable.name^+delimiter+result
  2577. else
  2578. result:=delimiter+result;
  2579. symtable:=symtable.defowner.owner;
  2580. end;
  2581. end;
  2582. function generate_objectpascal_helper_key(def:tdef):string;
  2583. begin
  2584. if not assigned(def) then
  2585. internalerror(2013020501);
  2586. if def.typ in [recorddef,objectdef] then
  2587. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2588. else
  2589. result:=make_mangledname('',def.owner,def.typesym.name);
  2590. end;
  2591. procedure incompatibletypes(def1,def2:tdef);
  2592. begin
  2593. { When there is an errordef there is already an error message show }
  2594. if (def2.typ=errordef) or
  2595. (def1.typ=errordef) then
  2596. exit;
  2597. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2598. end;
  2599. procedure hidesym(sym:TSymEntry);
  2600. begin
  2601. sym.realname:='$hidden'+sym.realname;
  2602. tsym(sym).visibility:=vis_hidden;
  2603. end;
  2604. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2605. var
  2606. st : TSymtable;
  2607. filename : TIDString;
  2608. begin
  2609. if not warn then
  2610. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2611. else
  2612. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2613. { Write hint where the original symbol was found }
  2614. st:=finduniTSymtable(origsym.owner);
  2615. with tsym(origsym).fileinfo do
  2616. begin
  2617. if assigned(st) and
  2618. (st.symtabletype=globalsymtable) and
  2619. st.iscurrentunit then
  2620. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2621. else if assigned(st.name) then
  2622. begin
  2623. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2624. if filename<>'' then
  2625. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2626. else
  2627. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2628. end;
  2629. end;
  2630. { Rename duplicate sym to an unreachable name, but it can be
  2631. inserted in the symtable without errors }
  2632. inc(dupnr);
  2633. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2634. if assigned(dupsym) then
  2635. include(tsym(dupsym).symoptions,sp_implicitrename);
  2636. end;
  2637. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2638. begin
  2639. result:=false;
  2640. if not assigned(sym) or not (sym is tstoredsym) then
  2641. Internalerror(2011081101);
  2642. { For generics a dummy symbol without the parameter count is created
  2643. if such a symbol not yet exists so that different parts of the
  2644. parser can find that symbol. If that symbol is still a
  2645. undefineddef we replace the generic dummy symbol's
  2646. name with a "dup" name and use the new symbol as the generic dummy
  2647. symbol }
  2648. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2649. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2650. (m_delphi in current_settings.modeswitches) then
  2651. begin
  2652. inc(dupnr);
  2653. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2654. include(tsym(sym).symoptions,sp_implicitrename);
  2655. { we need to find the new symbol now if checking for a dummy }
  2656. include(symoptions,sp_generic_dummy);
  2657. result:=true;
  2658. end;
  2659. end;
  2660. procedure write_system_parameter_lists(const name:string);
  2661. var
  2662. srsym:tprocsym;
  2663. begin
  2664. srsym:=tprocsym(systemunit.find(name));
  2665. if not assigned(srsym) or not (srsym.typ=procsym) then
  2666. internalerror(2016060302);
  2667. srsym.write_parameter_lists(nil);
  2668. end;
  2669. {*****************************************************************************
  2670. Search
  2671. *****************************************************************************}
  2672. procedure addsymref(sym:tsym);
  2673. var
  2674. owner: tsymtable;
  2675. begin
  2676. { for symbols used in preprocessor expressions, we don't want to
  2677. increase references count (for smaller final binaries) }
  2678. if not assigned(current_scanner) then
  2679. internalerror(2017050601);
  2680. if current_scanner.in_preproc_comp_expr then
  2681. exit;
  2682. { symbol uses count }
  2683. sym.IncRefCount;
  2684. owner:=sym.owner;
  2685. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2686. owner:=tdef(owner.defowner).owner;
  2687. if assigned(current_module) and
  2688. (owner.symtabletype=globalsymtable) then
  2689. begin
  2690. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2691. internalerror(200501152);
  2692. { unit uses count }
  2693. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2694. { Note: don't check the symtable directly as owner might be
  2695. a specialize symtable which is a globalsymtable as well }
  2696. if (
  2697. assigned(current_module.globalsymtable) and
  2698. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2699. ) or (
  2700. assigned(current_module.localsymtable) and
  2701. (current_module.localsymtable.moduleid<>owner.moduleid)
  2702. ) then
  2703. { symbol is imported from another unit }
  2704. current_module.addimportedsym(sym);
  2705. end;
  2706. end;
  2707. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2708. begin
  2709. result:=nesteddef=ownerdef;
  2710. if not result and
  2711. { types declared locally in a record method are not defined in the
  2712. record itself }
  2713. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2714. assigned(nesteddef.owner.defowner) then
  2715. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2716. end;
  2717. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2718. begin
  2719. result:=assigned(childsym) and (childsym.owner=symtable);
  2720. if not result and assigned(childsym) and
  2721. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2722. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2723. end;
  2724. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2725. begin
  2726. result:=false;
  2727. if not assigned(def1) or not assigned(def2) then
  2728. exit;
  2729. { for both defs walk to the topmost generic }
  2730. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2731. def1:=tdef(def1.owner.defowner);
  2732. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2733. def2:=tdef(def2.owner.defowner);
  2734. result:=def1=def2;
  2735. end;
  2736. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2737. var
  2738. uname : string;
  2739. begin
  2740. { TODO : check regarding arrays and records declared as their type }
  2741. if not (def.typ in [recorddef,objectdef]) then
  2742. internalerror(2012051501);
  2743. uname:=upper(srsym.realname);
  2744. repeat
  2745. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2746. begin
  2747. result:=def;
  2748. exit;
  2749. end;
  2750. def:=tdef(def.owner.defowner);
  2751. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2752. result:=nil;
  2753. end;
  2754. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2755. begin
  2756. { TODO : check regarding arrays and records declared as their type }
  2757. if not (nesteddef.typ in [recorddef,objectdef]) then
  2758. internalerror(2012051601);
  2759. repeat
  2760. if tstoreddef(nesteddef).genericdef=genericdef then
  2761. begin
  2762. resultdef:=nesteddef;
  2763. result:=true;
  2764. exit;
  2765. end;
  2766. nesteddef:=tdef(nesteddef.owner.defowner);
  2767. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2768. resultdef:=nil;
  2769. result:=false;
  2770. end;
  2771. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2772. symvisibility: visibility of the symbol
  2773. contextobjdef: via which def the symbol is accessed, e.g.:
  2774. fieldname:=1 -> contextobjdef = current_structdef
  2775. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2776. }
  2777. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2778. var
  2779. symownerdef : tabstractrecorddef;
  2780. nonlocalst : tsymtable;
  2781. isspezproc : boolean;
  2782. begin
  2783. result:=false;
  2784. { Get objdectdef owner of the symtable for the is_related checks }
  2785. if not assigned(symst) or
  2786. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  2787. internalerror(200810285);
  2788. symownerdef:=tabstractrecorddef(symst.defowner);
  2789. { specializations might belong to a localsymtable or parasymtable }
  2790. nonlocalst:=symownerdef.owner;
  2791. if tstoreddef(symst.defowner).is_specialization then
  2792. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  2793. nonlocalst:=nonlocalst.defowner.owner;
  2794. isspezproc:=false;
  2795. if assigned(current_procinfo) then
  2796. begin
  2797. if current_procinfo.procdef.is_specialization and
  2798. assigned(current_procinfo.procdef.struct) then
  2799. isspezproc:=true;
  2800. end;
  2801. case symvisibility of
  2802. vis_private :
  2803. begin
  2804. { private symbols are allowed when we are in the same
  2805. module as they are defined }
  2806. result:=(
  2807. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2808. (nonlocalst.iscurrentunit)
  2809. ) or
  2810. ( // the case of specialize inside the generic declaration and nested types
  2811. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2812. (
  2813. assigned(current_structdef) and
  2814. (
  2815. (current_structdef=symownerdef) or
  2816. (current_structdef.owner.iscurrentunit)
  2817. )
  2818. ) or
  2819. (
  2820. not assigned(current_structdef) and
  2821. (symownerdef.owner.iscurrentunit)
  2822. ) or
  2823. { access from a generic method that belongs to the class
  2824. but that is specialized elsewere }
  2825. (
  2826. isspezproc and
  2827. (current_procinfo.procdef.struct=current_structdef)
  2828. ) or
  2829. { specializations may access private symbols that their
  2830. generics are allowed to access }
  2831. (
  2832. assigned(current_structdef) and
  2833. (df_specialization in current_structdef.defoptions) and
  2834. (symst.moduleid=current_structdef.genericdef.owner.moduleid)
  2835. )
  2836. );
  2837. end;
  2838. vis_strictprivate :
  2839. begin
  2840. result:=assigned(current_structdef) and
  2841. is_owned_by(current_structdef,symownerdef);
  2842. end;
  2843. vis_strictprotected :
  2844. begin
  2845. result:=(
  2846. { access from nested class }
  2847. assigned(current_structdef) and
  2848. is_owned_by(current_structdef,symownerdef)
  2849. ) or
  2850. (
  2851. { access from child class }
  2852. assigned(contextobjdef) and
  2853. assigned(current_structdef) and
  2854. def_is_related(contextobjdef,symownerdef) and
  2855. def_is_related(current_structdef,contextobjdef)
  2856. ) or
  2857. (
  2858. { helpers can access strict protected symbols }
  2859. is_objectpascal_helper(contextobjdef) and
  2860. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2861. ) or
  2862. (
  2863. { same as above, but from context of call node inside
  2864. helper method }
  2865. is_objectpascal_helper(current_structdef) and
  2866. def_is_related(tobjectdef(current_structdef).extendeddef,symownerdef)
  2867. );
  2868. end;
  2869. vis_protected :
  2870. begin
  2871. { protected symbols are visible in the module that defines them and
  2872. also visible to related objects. The related object must be defined
  2873. in the current module }
  2874. result:=(
  2875. (
  2876. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  2877. (nonlocalst.iscurrentunit)
  2878. ) or
  2879. (
  2880. assigned(contextobjdef) and
  2881. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable]) and
  2882. (contextobjdef.owner.iscurrentunit) and
  2883. def_is_related(contextobjdef,symownerdef)
  2884. ) or
  2885. ( // the case of specialize inside the generic declaration and nested types
  2886. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  2887. (
  2888. assigned(current_structdef) and
  2889. (
  2890. (current_structdef=symownerdef) or
  2891. (current_structdef.owner.iscurrentunit)
  2892. )
  2893. ) or
  2894. (
  2895. not assigned(current_structdef) and
  2896. (symownerdef.owner.iscurrentunit)
  2897. ) or
  2898. (
  2899. { helpers can access protected symbols }
  2900. is_objectpascal_helper(contextobjdef) and
  2901. def_is_related(tobjectdef(contextobjdef).extendeddef,symownerdef)
  2902. )
  2903. ) or
  2904. { access from a generic method that belongs to the class
  2905. but that is specialized elsewere }
  2906. (
  2907. isspezproc and
  2908. (current_procinfo.procdef.struct=current_structdef)
  2909. ) or
  2910. { specializations may access private symbols that their
  2911. generics are allowed to access }
  2912. (
  2913. assigned(current_structdef) and
  2914. (df_specialization in current_structdef.defoptions) and
  2915. (symst.moduleid=current_structdef.genericdef.owner.moduleid)
  2916. )
  2917. );
  2918. end;
  2919. vis_public,
  2920. vis_published :
  2921. result:=true;
  2922. end;
  2923. end;
  2924. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  2925. begin
  2926. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  2927. end;
  2928. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  2929. var
  2930. i : longint;
  2931. pd : tprocdef;
  2932. begin
  2933. if sym.typ=procsym then
  2934. begin
  2935. { A procsym is visible, when there is at least one of the procdefs visible }
  2936. result:=false;
  2937. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2938. begin
  2939. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2940. if (pd.owner=sym.owner) and
  2941. is_visible_for_object(pd,contextobjdef) then
  2942. begin
  2943. result:=true;
  2944. exit;
  2945. end;
  2946. end;
  2947. end
  2948. else
  2949. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2950. end;
  2951. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2952. begin
  2953. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  2954. end;
  2955. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  2956. begin
  2957. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  2958. end;
  2959. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  2960. var
  2961. hashedid: THashedIDString;
  2962. contextstructdef: tabstractrecorddef;
  2963. stackitem: psymtablestackitem;
  2964. begin
  2965. result:=false;
  2966. hashedid.id:=s;
  2967. stackitem:=symtablestack.stack;
  2968. while assigned(stackitem) do
  2969. begin
  2970. srsymtable:=stackitem^.symtable;
  2971. if (srsymtable.symtabletype=objectsymtable) then
  2972. begin
  2973. { TODO : implement the search for an option in classes as well }
  2974. if ssf_search_option in flags then
  2975. begin
  2976. result:=false;
  2977. exit;
  2978. end;
  2979. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  2980. begin
  2981. result:=true;
  2982. exit;
  2983. end;
  2984. end
  2985. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  2986. (srsymtable.defowner.typ=undefineddef)) then
  2987. begin
  2988. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2989. { First check if it is a unit/namespace symbol.
  2990. They are visible only if they are from the current unit or
  2991. unit of generic of currently processed specialization. }
  2992. if assigned(srsym) and
  2993. (
  2994. not(srsym.typ in [unitsym,namespacesym]) or
  2995. srsymtable.iscurrentunit or
  2996. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid)) or
  2997. (
  2998. assigned(current_procinfo) and
  2999. (df_specialization in current_procinfo.procdef.defoptions) and
  3000. (current_procinfo.procdef.genericdef.owner.moduleid=srsymtable.moduleid)
  3001. )
  3002. ) and
  3003. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  3004. begin
  3005. { use the class from withsymtable only when it is
  3006. defined in this unit }
  3007. if (srsymtable.symtabletype=withsymtable) and
  3008. assigned(srsymtable.defowner) and
  3009. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  3010. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  3011. (srsymtable.defowner.owner.iscurrentunit) then
  3012. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  3013. else
  3014. contextstructdef:=current_structdef;
  3015. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  3016. is_visible_for_object(srsym,contextstructdef) then
  3017. begin
  3018. { we need to know if a procedure references symbols
  3019. in the static symtable, because then it can't be
  3020. inlined from outside this unit }
  3021. if assigned(current_procinfo) and
  3022. (srsym.owner.symtabletype=staticsymtable) then
  3023. include(current_procinfo.flags,pi_uses_static_symtable);
  3024. if not (ssf_no_addsymref in flags) then
  3025. addsymref(srsym);
  3026. result:=true;
  3027. exit;
  3028. end;
  3029. end;
  3030. end;
  3031. stackitem:=stackitem^.next;
  3032. end;
  3033. srsym:=nil;
  3034. srsymtable:=nil;
  3035. end;
  3036. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  3037. srsymtable:TSymtable;option:tsymoption):boolean;
  3038. begin
  3039. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  3040. end;
  3041. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3042. var
  3043. hashedid : THashedIDString;
  3044. stackitem : psymtablestackitem;
  3045. classh : tobjectdef;
  3046. begin
  3047. result:=false;
  3048. hashedid.id:=s;
  3049. stackitem:=symtablestack.stack;
  3050. while assigned(stackitem) do
  3051. begin
  3052. {
  3053. It is not possible to have type symbols in:
  3054. parameters
  3055. Exception are classes, objects, records, generic definitions and specializations
  3056. that have the parameterized types inserted in the symtable.
  3057. }
  3058. srsymtable:=stackitem^.symtable;
  3059. if (srsymtable.symtabletype=ObjectSymtable) then
  3060. begin
  3061. classh:=tobjectdef(srsymtable.defowner);
  3062. while assigned(classh) do
  3063. begin
  3064. srsymtable:=classh.symtable;
  3065. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3066. if assigned(srsym) and
  3067. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3068. is_visible_for_object(srsym,current_structdef) then
  3069. begin
  3070. addsymref(srsym);
  3071. result:=true;
  3072. exit;
  3073. end;
  3074. classh:=classh.childof;
  3075. end;
  3076. end
  3077. else
  3078. begin
  3079. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3080. if assigned(srsym) and
  3081. (
  3082. not(srsym.typ in [unitsym,namespacesym]) or
  3083. srsymtable.iscurrentunit or
  3084. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  3085. ) and
  3086. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3087. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  3088. begin
  3089. { we need to know if a procedure references symbols
  3090. in the static symtable, because then it can't be
  3091. inlined from outside this unit }
  3092. if assigned(current_procinfo) and
  3093. (srsym.owner.symtabletype=staticsymtable) then
  3094. include(current_procinfo.flags,pi_uses_static_symtable);
  3095. addsymref(srsym);
  3096. result:=true;
  3097. exit;
  3098. end;
  3099. end;
  3100. stackitem:=stackitem^.next;
  3101. end;
  3102. result:=false;
  3103. srsym:=nil;
  3104. srsymtable:=nil;
  3105. end;
  3106. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3107. var
  3108. pmod : tmodule;
  3109. begin
  3110. pmod:=tmodule(pm);
  3111. result:=false;
  3112. if assigned(pmod.globalsymtable) then
  3113. begin
  3114. srsym:=tsym(pmod.globalsymtable.Find(s));
  3115. if assigned(srsym) then
  3116. begin
  3117. srsymtable:=pmod.globalsymtable;
  3118. addsymref(srsym);
  3119. result:=true;
  3120. exit;
  3121. end;
  3122. end;
  3123. { If the module is the current unit we also need
  3124. to search the local symtable }
  3125. if (pmod=current_module) and
  3126. assigned(pmod.localsymtable) then
  3127. begin
  3128. srsym:=tsym(pmod.localsymtable.Find(s));
  3129. if assigned(srsym) then
  3130. begin
  3131. srsymtable:=pmod.localsymtable;
  3132. addsymref(srsym);
  3133. result:=true;
  3134. exit;
  3135. end;
  3136. end;
  3137. srsym:=nil;
  3138. srsymtable:=nil;
  3139. end;
  3140. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  3141. var
  3142. stackitem : psymtablestackitem;
  3143. begin
  3144. result:=false;
  3145. stackitem:=symtablestack.stack;
  3146. while assigned(stackitem) do
  3147. begin
  3148. srsymtable:=stackitem^.symtable;
  3149. if (srsymtable.symtabletype=globalsymtable) and
  3150. (srsymtable.name^=unitname) then
  3151. begin
  3152. srsym:=tsym(srsymtable.find(symname));
  3153. if not assigned(srsym) then
  3154. break;
  3155. result:=true;
  3156. exit;
  3157. end;
  3158. stackitem:=stackitem^.next;
  3159. end;
  3160. { If the module is the current unit we also need
  3161. to search the local symtable }
  3162. if assigned(current_module.localsymtable) and
  3163. (current_module.localsymtable.name^=unitname) then
  3164. begin
  3165. srsymtable:=current_module.localsymtable;
  3166. srsym:=tsym(srsymtable.find(symname));
  3167. if assigned(srsym) then
  3168. begin
  3169. result:=true;
  3170. exit;
  3171. end;
  3172. end;
  3173. end;
  3174. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  3175. begin
  3176. result:=pd;
  3177. if pd.typ<>objectdef then
  3178. exit;
  3179. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  3180. end;
  3181. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  3182. var
  3183. hashedid : THashedIDString;
  3184. stackitem : psymtablestackitem;
  3185. srsymtable : tsymtable;
  3186. srsym : tsym;
  3187. formalname,
  3188. foundname : shortstring;
  3189. formalnameptr,
  3190. foundnameptr: pshortstring;
  3191. begin
  3192. { not a formal definition -> return it }
  3193. if not(oo_is_formal in pd.objectoptions) then
  3194. begin
  3195. result:=pd;
  3196. exit;
  3197. end;
  3198. hashedid.id:=pd.typesym.name;
  3199. stackitem:=symtablestack.stack;
  3200. while assigned(stackitem) do
  3201. begin
  3202. srsymtable:=stackitem^.symtable;
  3203. { ObjC classes can't appear in generics or as nested class
  3204. definitions. Java classes can. }
  3205. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  3206. (is_java_class_or_interface(pd) and
  3207. (srsymtable.symtabletype=ObjectSymtable)) then
  3208. begin
  3209. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3210. if assigned(srsym) and
  3211. (srsym.typ=typesym) and
  3212. (ttypesym(srsym).typedef.typ=objectdef) and
  3213. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  3214. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3215. begin
  3216. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3217. begin
  3218. { the external name for the formal and the real
  3219. definition must match }
  3220. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  3221. assigned(pd.import_lib) then
  3222. begin
  3223. if assigned(pd.import_lib) then
  3224. formalname:=pd.import_lib^+'.'
  3225. else
  3226. formalname:='';
  3227. formalname:=formalname+pd.objextname^;
  3228. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  3229. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  3230. else
  3231. foundname:='';
  3232. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3233. formalnameptr:=@formalname;
  3234. foundnameptr:=@foundname;
  3235. end
  3236. else
  3237. begin
  3238. formalnameptr:=pd.objextname;
  3239. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3240. end;
  3241. if foundnameptr^<>formalnameptr^ then
  3242. begin
  3243. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3244. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3245. end;
  3246. end;
  3247. result:=tobjectdef(ttypesym(srsym).typedef);
  3248. if assigned(current_procinfo) and
  3249. (srsym.owner.symtabletype=staticsymtable) then
  3250. include(current_procinfo.flags,pi_uses_static_symtable);
  3251. addsymref(srsym);
  3252. exit;
  3253. end;
  3254. end;
  3255. stackitem:=stackitem^.next;
  3256. end;
  3257. { nothing found: optionally give an error and return the original
  3258. (empty) one }
  3259. if erroronfailure then
  3260. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3261. result:=pd;
  3262. end;
  3263. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3264. var
  3265. hashedid : THashedIDString;
  3266. orgclass : tobjectdef;
  3267. i : longint;
  3268. begin
  3269. orgclass:=classh;
  3270. { in case this is a formal class, first find the real definition }
  3271. if assigned(classh) then
  3272. begin
  3273. if (oo_is_formal in classh.objectoptions) then
  3274. classh:=find_real_class_definition(classh,true);
  3275. { The contextclassh is used for visibility. The classh must be equal to
  3276. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3277. parent or a class helper. }
  3278. if not (def_is_related(contextclassh,classh) or
  3279. (is_classhelper(contextclassh) and
  3280. assigned(tobjectdef(contextclassh).extendeddef) and
  3281. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3282. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3283. internalerror(200811161);
  3284. end;
  3285. result:=false;
  3286. hashedid.id:=s;
  3287. { an Objective-C protocol or Java interface can inherit from multiple
  3288. other protocols/interfaces -> use ImplementedInterfaces instead }
  3289. if is_objcprotocol(classh) or
  3290. is_javainterface(classh) then
  3291. begin
  3292. srsymtable:=classh.symtable;
  3293. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3294. if assigned(srsym) and
  3295. is_visible_for_object(srsym,contextclassh) then
  3296. begin
  3297. if not (ssf_no_addsymref in flags) then
  3298. addsymref(srsym);
  3299. result:=true;
  3300. exit;
  3301. end;
  3302. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3303. begin
  3304. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3305. begin
  3306. result:=true;
  3307. exit;
  3308. end;
  3309. end;
  3310. end
  3311. else
  3312. if is_objectpascal_helper(classh) then
  3313. begin
  3314. { helpers have their own obscure search logic... }
  3315. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3316. if result then
  3317. exit;
  3318. end
  3319. else
  3320. begin
  3321. while assigned(classh) do
  3322. begin
  3323. { search for a class helper method first if this is an Object
  3324. Pascal class and we haven't yet found a helper symbol }
  3325. if (classh.objecttype in objecttypes_with_helpers) and
  3326. (ssf_search_helper in flags) then
  3327. begin
  3328. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3329. { an eventual overload inside the extended type's hierarchy
  3330. will be found by tcallcandidates }
  3331. if result then
  3332. exit;
  3333. end;
  3334. srsymtable:=classh.symtable;
  3335. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3336. if assigned(srsym) and
  3337. is_visible_for_object(srsym,contextclassh) then
  3338. begin
  3339. if not (ssf_no_addsymref in flags) then
  3340. addsymref(srsym);
  3341. result:=true;
  3342. exit;
  3343. end;
  3344. classh:=classh.childof;
  3345. end;
  3346. end;
  3347. if is_objcclass(orgclass) then
  3348. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3349. else
  3350. begin
  3351. srsym:=nil;
  3352. srsymtable:=nil;
  3353. end;
  3354. end;
  3355. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3356. var
  3357. hashedid : THashedIDString;
  3358. begin
  3359. result:=false;
  3360. hashedid.id:=s;
  3361. { search for a record helper method first }
  3362. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3363. if result then
  3364. { an eventual overload inside the extended type's hierarchy
  3365. will be found by tcallcandidates }
  3366. exit;
  3367. srsymtable:=recordh.symtable;
  3368. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3369. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3370. begin
  3371. addsymref(srsym);
  3372. result:=true;
  3373. exit;
  3374. end;
  3375. srsym:=nil;
  3376. srsymtable:=nil;
  3377. end;
  3378. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3379. var
  3380. def : tdef;
  3381. i : longint;
  3382. begin
  3383. { in case this is a formal class, first find the real definition }
  3384. if assigned(classh) and
  3385. (oo_is_formal in classh.objectoptions) then
  3386. classh:=find_real_class_definition(classh,true);
  3387. result:=false;
  3388. def:=nil;
  3389. while assigned(classh) do
  3390. begin
  3391. for i:=0 to classh.symtable.DefList.Count-1 do
  3392. begin
  3393. def:=tstoreddef(classh.symtable.DefList[i]);
  3394. { Find also all hidden private methods to
  3395. be compatible with delphi, see tw6203 (PFV) }
  3396. if (def.typ=procdef) and
  3397. (po_msgint in tprocdef(def).procoptions) and
  3398. (tprocdef(def).messageinf.i=msgid) then
  3399. begin
  3400. srdef:=def;
  3401. srsym:=tprocdef(def).procsym;
  3402. srsymtable:=classh.symtable;
  3403. addsymref(srsym);
  3404. result:=true;
  3405. exit;
  3406. end;
  3407. end;
  3408. classh:=classh.childof;
  3409. end;
  3410. srdef:=nil;
  3411. srsym:=nil;
  3412. srsymtable:=nil;
  3413. end;
  3414. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3415. var
  3416. def : tdef;
  3417. i : longint;
  3418. begin
  3419. { in case this is a formal class, first find the real definition }
  3420. if assigned(classh) and
  3421. (oo_is_formal in classh.objectoptions) then
  3422. classh:=find_real_class_definition(classh,true);
  3423. result:=false;
  3424. def:=nil;
  3425. while assigned(classh) do
  3426. begin
  3427. for i:=0 to classh.symtable.DefList.Count-1 do
  3428. begin
  3429. def:=tstoreddef(classh.symtable.DefList[i]);
  3430. { Find also all hidden private methods to
  3431. be compatible with delphi, see tw6203 (PFV) }
  3432. if (def.typ=procdef) and
  3433. (po_msgstr in tprocdef(def).procoptions) and
  3434. (tprocdef(def).messageinf.str^=s) then
  3435. begin
  3436. srsym:=tprocdef(def).procsym;
  3437. srsymtable:=classh.symtable;
  3438. addsymref(srsym);
  3439. result:=true;
  3440. exit;
  3441. end;
  3442. end;
  3443. classh:=classh.childof;
  3444. end;
  3445. srsym:=nil;
  3446. srsymtable:=nil;
  3447. end;
  3448. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3449. var
  3450. hashedid : THashedIDString;
  3451. parentclassh : tobjectdef;
  3452. begin
  3453. result:=false;
  3454. if not is_objectpascal_helper(classh) then
  3455. Internalerror(2011030101);
  3456. hashedid.id:=s;
  3457. { in a helper things are a bit more complex:
  3458. 1. search the symbol in the helper (if not "inherited")
  3459. 2. search the symbol in the extended type
  3460. 3. search the symbol in the parent helpers
  3461. 4. only classes: search the symbol in the parents of the extended type
  3462. }
  3463. if not (ssf_has_inherited in flags) then
  3464. begin
  3465. { search in the helper itself }
  3466. srsymtable:=classh.symtable;
  3467. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3468. if assigned(srsym) and
  3469. is_visible_for_object(srsym,contextclassh) then
  3470. begin
  3471. if not (ssf_no_addsymref in flags) then
  3472. addsymref(srsym);
  3473. result:=true;
  3474. exit;
  3475. end;
  3476. end;
  3477. { now search in the extended type itself }
  3478. { Note: the extendeddef might be Nil if we are currently parsing the
  3479. extended type itself and the identifier was not found }
  3480. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3481. begin
  3482. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3483. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3484. if assigned(srsym) and
  3485. is_visible_for_object(srsym,contextclassh) then
  3486. begin
  3487. if not (ssf_no_addsymref in flags) then
  3488. addsymref(srsym);
  3489. result:=true;
  3490. exit;
  3491. end;
  3492. end;
  3493. { now search in the parent helpers }
  3494. parentclassh:=classh.childof;
  3495. while assigned(parentclassh) do
  3496. begin
  3497. srsymtable:=parentclassh.symtable;
  3498. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3499. if assigned(srsym) and
  3500. is_visible_for_object(srsym,contextclassh) then
  3501. begin
  3502. if not (ssf_no_addsymref in flags) then
  3503. addsymref(srsym);
  3504. result:=true;
  3505. exit;
  3506. end;
  3507. parentclassh:=parentclassh.childof;
  3508. end;
  3509. if is_class(classh.extendeddef) then
  3510. { now search in the parents of the extended class (with helpers!) }
  3511. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3512. { addsymref is already called by searchsym_in_class }
  3513. end;
  3514. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3515. var
  3516. sym : Tprocsym;
  3517. hashedid : THashedIDString;
  3518. curreq,
  3519. besteq : tequaltype;
  3520. currpd,
  3521. bestpd : tprocdef;
  3522. stackitem : psymtablestackitem;
  3523. begin
  3524. hashedid.id:=overloaded_names[assignment_type];
  3525. besteq:=te_incompatible;
  3526. bestpd:=nil;
  3527. stackitem:=symtablestack.stack;
  3528. while assigned(stackitem) do
  3529. begin
  3530. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3531. if sym<>nil then
  3532. begin
  3533. if sym.typ<>procsym then
  3534. internalerror(200402031);
  3535. { if the source type is an alias then this is only the second choice,
  3536. if you mess with this code, check tw4093 }
  3537. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  3538. if curreq>besteq then
  3539. begin
  3540. besteq:=curreq;
  3541. bestpd:=currpd;
  3542. if (besteq=te_exact) then
  3543. break;
  3544. end;
  3545. end;
  3546. stackitem:=stackitem^.next;
  3547. end;
  3548. result:=bestpd;
  3549. end;
  3550. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3551. begin
  3552. { search record/object symtable first for a suitable operator }
  3553. if from_def.typ in [recorddef,objectdef] then
  3554. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3555. if to_def.typ in [recorddef,objectdef] then
  3556. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3557. { if type conversion is explicit then search first for explicit
  3558. operator overload and if not found then use implicit operator }
  3559. if explicit then
  3560. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3561. else
  3562. result:=nil;
  3563. if result=nil then
  3564. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3565. { restore symtable stack }
  3566. if to_def.typ in [recorddef,objectdef] then
  3567. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3568. if from_def.typ in [recorddef,objectdef] then
  3569. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3570. end;
  3571. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3572. var
  3573. sym : Tprocsym;
  3574. hashedid : THashedIDString;
  3575. curreq,
  3576. besteq : tequaltype;
  3577. currpd,
  3578. bestpd : tprocdef;
  3579. stackitem : psymtablestackitem;
  3580. begin
  3581. hashedid.id:='enumerator';
  3582. besteq:=te_incompatible;
  3583. bestpd:=nil;
  3584. stackitem:=symtablestack.stack;
  3585. while assigned(stackitem) do
  3586. begin
  3587. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3588. if sym<>nil then
  3589. begin
  3590. if sym.typ<>procsym then
  3591. internalerror(200910241);
  3592. { if the source type is an alias then this is only the second choice,
  3593. if you mess with this code, check tw4093 }
  3594. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3595. if curreq>besteq then
  3596. begin
  3597. besteq:=curreq;
  3598. bestpd:=currpd;
  3599. if (besteq=te_exact) then
  3600. break;
  3601. end;
  3602. end;
  3603. stackitem:=stackitem^.next;
  3604. end;
  3605. result:=bestpd;
  3606. end;
  3607. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  3608. var
  3609. sym : Tprocsym;
  3610. hashedid : THashedIDString;
  3611. optoken: ttoken;
  3612. begin
  3613. optoken := managementoperator2tok[mop];
  3614. if (optoken<first_managment_operator) or
  3615. (optoken>last_managment_operator) then
  3616. internalerror(201602280);
  3617. hashedid.id:=overloaded_names[optoken];
  3618. if not (pd.typ in [recorddef]) then
  3619. internalerror(201602281);
  3620. sym:=Tprocsym(tabstractrecorddef(pd).symtable.FindWithHash(hashedid));
  3621. if sym<>nil then
  3622. begin
  3623. if sym.typ<>procsym then
  3624. internalerror(201602282);
  3625. result:=sym.find_procdef_bytype(potype_operator);
  3626. end
  3627. else
  3628. result:=nil;
  3629. end;
  3630. function search_system_type(const s: TIDString): ttypesym;
  3631. var
  3632. sym : tsym;
  3633. begin
  3634. sym:=tsym(systemunit.Find(s));
  3635. if not assigned(sym) or
  3636. (sym.typ<>typesym) then
  3637. message1(cg_f_unknown_system_type,s);
  3638. result:=ttypesym(sym);
  3639. end;
  3640. function try_search_system_type(const s: TIDString): ttypesym;
  3641. var
  3642. sym : tsym;
  3643. begin
  3644. sym:=tsym(systemunit.Find(s));
  3645. if not assigned(sym) then
  3646. result:=nil
  3647. else
  3648. begin
  3649. if sym.typ<>typesym then
  3650. message1(cg_f_unknown_system_type,s);
  3651. result:=ttypesym(sym);
  3652. end;
  3653. end;
  3654. function try_search_current_module_type(const s: TIDString): ttypesym;
  3655. var
  3656. found: boolean;
  3657. srsymtable: tsymtable;
  3658. srsym: tsym;
  3659. begin
  3660. if s[1]='$' then
  3661. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  3662. else
  3663. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  3664. if found then
  3665. begin
  3666. if (srsym.typ<>typesym) then
  3667. internalerror(2014091207);
  3668. result:=ttypesym(srsym);
  3669. end
  3670. else
  3671. result:=nil;
  3672. end;
  3673. function search_system_proc(const s: TIDString): tprocdef;
  3674. var
  3675. srsym: tsym;
  3676. begin
  3677. srsym:=tsym(systemunit.find(s));
  3678. if not assigned(srsym) and
  3679. (cs_compilesystem in current_settings.moduleswitches) then
  3680. srsym:=tsym(systemunit.Find(upper(s)));
  3681. if not assigned(srsym) or
  3682. (srsym.typ<>procsym) then
  3683. message1(cg_f_unknown_compilerproc,s);
  3684. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  3685. end;
  3686. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  3687. var
  3688. srsymtable: tsymtable;
  3689. sym: tsym;
  3690. begin
  3691. sym:=nil;
  3692. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  3693. (sym.typ=typesym) then
  3694. begin
  3695. result:=ttypesym(sym);
  3696. exit;
  3697. end
  3698. else
  3699. begin
  3700. if throwerror then
  3701. message2(cg_f_unknown_type_in_unit,typename,unitname);
  3702. result:=nil;
  3703. end;
  3704. end;
  3705. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  3706. var
  3707. s: string;
  3708. list: TFPObjectList;
  3709. i: integer;
  3710. st: tsymtable;
  3711. begin
  3712. result:=false;
  3713. odef:=nil;
  3714. { when there are no helpers active currently then we don't need to do
  3715. anything }
  3716. if current_module.extendeddefs.count=0 then
  3717. exit;
  3718. { no helpers for anonymous types }
  3719. if ((pd.typ in [recorddef,objectdef]) and
  3720. (
  3721. not assigned(tabstractrecorddef(pd).objrealname) or
  3722. (tabstractrecorddef(pd).objrealname^='')
  3723. )
  3724. ) or
  3725. not assigned(pd.typesym) then
  3726. exit;
  3727. { if pd is defined inside a procedure we must not use make_mangledname
  3728. (as a helper may not be defined in a procedure this is no problem...)}
  3729. st:=pd.owner;
  3730. while st.symtabletype in [objectsymtable,recordsymtable] do
  3731. st:=st.defowner.owner;
  3732. if st.symtabletype=localsymtable then
  3733. exit;
  3734. { the mangled name is used as the key for tmodule.extendeddefs }
  3735. s:=generate_objectpascal_helper_key(pd);
  3736. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  3737. if assigned(list) and (list.count>0) then
  3738. begin
  3739. i:=list.count-1;
  3740. repeat
  3741. odef:=tobjectdef(list[list.count-1]);
  3742. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  3743. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  3744. dec(i);
  3745. until result or (i<0);
  3746. if not result then
  3747. { just to be sure that noone uses odef }
  3748. odef:=nil;
  3749. end;
  3750. end;
  3751. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3752. var
  3753. hashedid : THashedIDString;
  3754. classh : tobjectdef;
  3755. i : integer;
  3756. pdef : tprocdef;
  3757. begin
  3758. result:=false;
  3759. { if there is no class helper for the class then there is no need to
  3760. search further }
  3761. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  3762. exit;
  3763. hashedid.id:=s;
  3764. repeat
  3765. srsymtable:=classh.symtable;
  3766. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3767. if srsym<>nil then
  3768. begin
  3769. case srsym.typ of
  3770. procsym:
  3771. begin
  3772. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3773. begin
  3774. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  3775. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  3776. continue;
  3777. { we need to know if a procedure references symbols
  3778. in the static symtable, because then it can't be
  3779. inlined from outside this unit }
  3780. if assigned(current_procinfo) and
  3781. (srsym.owner.symtabletype=staticsymtable) then
  3782. include(current_procinfo.flags,pi_uses_static_symtable);
  3783. { the first found method wins }
  3784. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3785. srsymtable:=srsym.owner;
  3786. addsymref(srsym);
  3787. result:=true;
  3788. exit;
  3789. end;
  3790. end;
  3791. typesym,
  3792. fieldvarsym,
  3793. constsym,
  3794. enumsym,
  3795. undefinedsym,
  3796. propertysym:
  3797. begin
  3798. addsymref(srsym);
  3799. result:=true;
  3800. exit;
  3801. end;
  3802. else
  3803. internalerror(2014041101);
  3804. end;
  3805. end;
  3806. { try the helper parent if available }
  3807. classh:=classh.childof;
  3808. until classh=nil;
  3809. srsym:=nil;
  3810. srsymtable:=nil;
  3811. end;
  3812. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3813. var
  3814. hashedid : THashedIDString;
  3815. stackitem : psymtablestackitem;
  3816. i : longint;
  3817. defowner : tobjectdef;
  3818. begin
  3819. hashedid.id:=class_helper_prefix+s;
  3820. stackitem:=symtablestack.stack;
  3821. while assigned(stackitem) do
  3822. begin
  3823. srsymtable:=stackitem^.symtable;
  3824. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3825. if assigned(srsym) then
  3826. begin
  3827. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3828. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3829. (srsym.typ<>procsym) then
  3830. internalerror(2009111505);
  3831. { check whether this procsym includes a helper for this particular class }
  3832. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3833. begin
  3834. { does pd inherit from (or is the same as) the class
  3835. that this method's category extended?
  3836. Warning: this list contains both category and objcclass methods
  3837. (for id.randommethod), so only check category methods here
  3838. }
  3839. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  3840. if is_objccategory(defowner) and
  3841. def_is_related(pd,defowner.childof) then
  3842. begin
  3843. { we need to know if a procedure references symbols
  3844. in the static symtable, because then it can't be
  3845. inlined from outside this unit }
  3846. if assigned(current_procinfo) and
  3847. (srsym.owner.symtabletype=staticsymtable) then
  3848. include(current_procinfo.flags,pi_uses_static_symtable);
  3849. { no need to keep looking. There might be other
  3850. categories that extend this, a parent or child
  3851. class with a method with the same name (either
  3852. overriding this one, or overridden by this one),
  3853. but that doesn't matter as far as the basic
  3854. procsym is concerned.
  3855. }
  3856. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3857. srsymtable:=srsym.owner;
  3858. addsymref(srsym);
  3859. result:=true;
  3860. exit;
  3861. end;
  3862. end;
  3863. end;
  3864. stackitem:=stackitem^.next;
  3865. end;
  3866. srsym:=nil;
  3867. srsymtable:=nil;
  3868. result:=false;
  3869. end;
  3870. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  3871. var
  3872. hashedid : THashedIDString;
  3873. stackitem : psymtablestackitem;
  3874. i : longint;
  3875. begin
  3876. hashedid.id:=class_helper_prefix+s;
  3877. stackitem:=symtablestack.stack;
  3878. while assigned(stackitem) do
  3879. begin
  3880. srsymtable:=stackitem^.symtable;
  3881. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3882. if assigned(srsym) then
  3883. begin
  3884. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  3885. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  3886. (srsym.typ<>procsym) then
  3887. internalerror(2009112005);
  3888. { check whether this procsym includes a helper for this particular class }
  3889. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  3890. begin
  3891. { we need to know if a procedure references symbols
  3892. in the static symtable, because then it can't be
  3893. inlined from outside this unit }
  3894. if assigned(current_procinfo) and
  3895. (srsym.owner.symtabletype=staticsymtable) then
  3896. include(current_procinfo.flags,pi_uses_static_symtable);
  3897. { no need to keep looking. There might be other
  3898. methods with the same name, but that doesn't matter
  3899. as far as the basic procsym is concerned.
  3900. }
  3901. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  3902. { We need the symtable in which the classhelper-like sym
  3903. is located, not the objectdef. The reason is that the
  3904. callnode will climb the symtablestack until it encounters
  3905. this symtable to start looking for overloads (and it won't
  3906. find the objectsymtable in which this method sym is
  3907. located
  3908. srsymtable:=srsym.owner;
  3909. }
  3910. addsymref(srsym);
  3911. result:=true;
  3912. exit;
  3913. end;
  3914. end;
  3915. stackitem:=stackitem^.next;
  3916. end;
  3917. srsym:=nil;
  3918. srsymtable:=nil;
  3919. result:=false;
  3920. end;
  3921. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  3922. { searches n in symtable of pd and all anchestors }
  3923. var
  3924. srsymtable : tsymtable;
  3925. begin
  3926. { in case this is a formal class, first find the real definition }
  3927. if (oo_is_formal in pd.objectoptions) then
  3928. pd:=find_real_class_definition(tobjectdef(pd),true);
  3929. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  3930. exit;
  3931. result:=search_struct_member_no_helper(pd,s);
  3932. if assigned(result) then
  3933. exit;
  3934. { not found, now look for class helpers }
  3935. if is_objcclass(pd) then
  3936. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  3937. end;
  3938. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  3939. var
  3940. hashedid : THashedIDString;
  3941. srsym : tsym;
  3942. begin
  3943. hashedid.id:=s;
  3944. while assigned(pd) do
  3945. begin
  3946. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  3947. if assigned(srsym) then
  3948. begin
  3949. result:=srsym;
  3950. exit;
  3951. end;
  3952. if pd.typ=objectdef then
  3953. pd:=tobjectdef(pd).childof
  3954. else
  3955. pd:=nil;
  3956. end;
  3957. result:=nil;
  3958. end;
  3959. function search_macro(const s : string):tsym;
  3960. var
  3961. stackitem : psymtablestackitem;
  3962. hashedid : THashedIDString;
  3963. srsym : tsym;
  3964. begin
  3965. hashedid.id:=s;
  3966. { First search the localmacrosymtable before searching the
  3967. global macrosymtables from the units }
  3968. if assigned(current_module) then
  3969. begin
  3970. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  3971. if assigned(srsym) then
  3972. begin
  3973. result:= srsym;
  3974. exit;
  3975. end;
  3976. end;
  3977. stackitem:=macrosymtablestack.stack;
  3978. while assigned(stackitem) do
  3979. begin
  3980. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  3981. if assigned(srsym) then
  3982. begin
  3983. result:= srsym;
  3984. exit;
  3985. end;
  3986. stackitem:=stackitem^.next;
  3987. end;
  3988. result:= nil;
  3989. end;
  3990. function defined_macro(const s : string):boolean;
  3991. var
  3992. mac: tmacro;
  3993. begin
  3994. mac:=tmacro(search_macro(s));
  3995. if assigned(mac) then
  3996. begin
  3997. mac.is_used:=true;
  3998. defined_macro:=mac.defined;
  3999. end
  4000. else
  4001. defined_macro:=false;
  4002. end;
  4003. {****************************************************************************
  4004. Object Helpers
  4005. ****************************************************************************}
  4006. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  4007. { returns the default property of a class, searches also anchestors }
  4008. var
  4009. _defaultprop : tpropertysym;
  4010. helperpd : tobjectdef;
  4011. begin
  4012. _defaultprop:=nil;
  4013. { first search in helper's hierarchy }
  4014. if search_last_objectpascal_helper(pd,nil,helperpd) then
  4015. while assigned(helperpd) do
  4016. begin
  4017. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  4018. if assigned(_defaultprop) then
  4019. break;
  4020. helperpd:=helperpd.childof;
  4021. end;
  4022. if assigned(_defaultprop) then
  4023. begin
  4024. search_default_property:=_defaultprop;
  4025. exit;
  4026. end;
  4027. { now search in the type's hierarchy itself }
  4028. while assigned(pd) do
  4029. begin
  4030. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  4031. if assigned(_defaultprop) then
  4032. break;
  4033. if (pd.typ=objectdef) then
  4034. pd:=tobjectdef(pd).childof
  4035. else
  4036. break;
  4037. end;
  4038. search_default_property:=_defaultprop;
  4039. end;
  4040. {****************************************************************************
  4041. Macro Helpers
  4042. ****************************************************************************}
  4043. procedure def_system_macro(const name : string);
  4044. var
  4045. mac : tmacro;
  4046. s: string;
  4047. begin
  4048. if name = '' then
  4049. internalerror(2004121202);
  4050. s:= upper(name);
  4051. mac:=tmacro(search_macro(s));
  4052. if not assigned(mac) then
  4053. begin
  4054. mac:=tmacro.create(s);
  4055. if assigned(current_module) then
  4056. current_module.localmacrosymtable.insert(mac)
  4057. else
  4058. initialmacrosymtable.insert(mac);
  4059. end;
  4060. Message1(parser_c_macro_defined,mac.name);
  4061. mac.defined:=true;
  4062. end;
  4063. procedure set_system_macro(const name, value : string);
  4064. var
  4065. mac : tmacro;
  4066. s: string;
  4067. begin
  4068. if name = '' then
  4069. internalerror(2004121203);
  4070. s:= upper(name);
  4071. mac:=tmacro(search_macro(s));
  4072. if not assigned(mac) then
  4073. begin
  4074. mac:=tmacro.create(s);
  4075. if assigned(current_module) then
  4076. current_module.localmacrosymtable.insert(mac)
  4077. else
  4078. initialmacrosymtable.insert(mac);
  4079. end
  4080. else
  4081. begin
  4082. mac.is_compiler_var:=false;
  4083. if assigned(mac.buftext) then
  4084. freemem(mac.buftext,mac.buflen);
  4085. end;
  4086. Message2(parser_c_macro_set_to,mac.name,value);
  4087. mac.buflen:=length(value);
  4088. getmem(mac.buftext,mac.buflen);
  4089. move(value[1],mac.buftext^,mac.buflen);
  4090. mac.defined:=true;
  4091. end;
  4092. procedure set_system_compvar(const name, value : string);
  4093. var
  4094. mac : tmacro;
  4095. s: string;
  4096. begin
  4097. if name = '' then
  4098. internalerror(2004121204);
  4099. s:= upper(name);
  4100. mac:=tmacro(search_macro(s));
  4101. if not assigned(mac) then
  4102. begin
  4103. mac:=tmacro.create(s);
  4104. mac.is_compiler_var:=true;
  4105. if assigned(current_module) then
  4106. current_module.localmacrosymtable.insert(mac)
  4107. else
  4108. initialmacrosymtable.insert(mac);
  4109. end
  4110. else
  4111. begin
  4112. mac.is_compiler_var:=true;
  4113. if assigned(mac.buftext) then
  4114. freemem(mac.buftext,mac.buflen);
  4115. end;
  4116. Message2(parser_c_macro_set_to,mac.name,value);
  4117. mac.buflen:=length(value);
  4118. getmem(mac.buftext,mac.buflen);
  4119. move(value[1],mac.buftext^,mac.buflen);
  4120. mac.defined:=true;
  4121. end;
  4122. procedure undef_system_macro(const name : string);
  4123. var
  4124. mac : tmacro;
  4125. s: string;
  4126. begin
  4127. if name = '' then
  4128. internalerror(2004121205);
  4129. s:= upper(name);
  4130. mac:=tmacro(search_macro(s));
  4131. if not assigned(mac) then
  4132. {If not found, then it's already undefined.}
  4133. else
  4134. begin
  4135. Message1(parser_c_macro_undefined,mac.name);
  4136. mac.defined:=false;
  4137. mac.is_compiler_var:=false;
  4138. { delete old definition }
  4139. if assigned(mac.buftext) then
  4140. begin
  4141. freemem(mac.buftext,mac.buflen);
  4142. mac.buftext:=nil;
  4143. end;
  4144. end;
  4145. end;
  4146. {$ifdef UNITALIASES}
  4147. {****************************************************************************
  4148. TUNIT_ALIAS
  4149. ****************************************************************************}
  4150. constructor tunit_alias.create(const n:string);
  4151. var
  4152. i : longint;
  4153. begin
  4154. i:=pos('=',n);
  4155. if i=0 then
  4156. fail;
  4157. inherited createname(Copy(n,1,i-1));
  4158. newname:=stringdup(Copy(n,i+1,255));
  4159. end;
  4160. destructor tunit_alias.destroy;
  4161. begin
  4162. stringdispose(newname);
  4163. inherited destroy;
  4164. end;
  4165. procedure addunitalias(const n:string);
  4166. begin
  4167. unitaliases^.insert(tunit_alias,init(Upper(n))));
  4168. end;
  4169. function getunitalias(const n:string):string;
  4170. var
  4171. p : punit_alias;
  4172. begin
  4173. p:=punit_alias(unitaliases^.Find(Upper(n)));
  4174. if assigned(p) then
  4175. getunitalias:=punit_alias(p).newname^
  4176. else
  4177. getunitalias:=n;
  4178. end;
  4179. {$endif UNITALIASES}
  4180. {****************************************************************************
  4181. Init/Done Symtable
  4182. ****************************************************************************}
  4183. procedure InitSymtable;
  4184. begin
  4185. { Reset symbolstack }
  4186. symtablestack:=nil;
  4187. systemunit:=nil;
  4188. { create error syms and def }
  4189. generrorsym:=terrorsym.create;
  4190. generrordef:=cerrordef.create;
  4191. { macros }
  4192. initialmacrosymtable:=tmacrosymtable.create(false);
  4193. macrosymtablestack:=TSymtablestack.create;
  4194. macrosymtablestack.push(initialmacrosymtable);
  4195. {$ifdef UNITALIASES}
  4196. { unit aliases }
  4197. unitaliases:=TFPHashObjectList.create;
  4198. {$endif}
  4199. { set some global vars to nil, might be important for the ide }
  4200. class_tobject:=nil;
  4201. interface_iunknown:=nil;
  4202. interface_idispatch:=nil;
  4203. rec_tguid:=nil;
  4204. rec_jmp_buf:=nil;
  4205. rec_exceptaddr:=nil;
  4206. objc_metaclasstype:=nil;
  4207. objc_superclasstype:=nil;
  4208. objc_idtype:=nil;
  4209. objc_seltype:=nil;
  4210. objc_objecttype:=nil;
  4211. dupnr:=0;
  4212. end;
  4213. procedure DoneSymtable;
  4214. begin
  4215. generrorsym.owner:=nil;
  4216. generrorsym.free;
  4217. generrordef.owner:=nil;
  4218. generrordef.free;
  4219. initialmacrosymtable.free;
  4220. macrosymtablestack.free;
  4221. {$ifdef UNITALIASES}
  4222. unitaliases.free;
  4223. {$endif}
  4224. end;
  4225. end.