symtable.pas 135 KB

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