symtable.pas 132 KB

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