symtable.pas 136 KB

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