symtable.pas 160 KB

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