symtable.pas 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534
  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. );
  1945. end;
  1946. vis_protected :
  1947. begin
  1948. { protected symbols are visible in the module that defines them and
  1949. also visible to related objects. The related object must be defined
  1950. in the current module }
  1951. result:=(
  1952. (
  1953. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1954. (symownerdef.owner.iscurrentunit)
  1955. ) or
  1956. (
  1957. assigned(contextobjdef) and
  1958. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable]) and
  1959. (contextobjdef.owner.iscurrentunit) and
  1960. contextobjdef.is_related(symownerdef)
  1961. ) or
  1962. ( // the case of specialize inside the generic declaration and nested types
  1963. (symownerdef.owner.symtabletype in [objectsymtable,recordsymtable]) and
  1964. (
  1965. assigned(current_structdef) and
  1966. (
  1967. (current_structdef=symownerdef) or
  1968. (current_structdef.owner.iscurrentunit)
  1969. )
  1970. ) or
  1971. (
  1972. not assigned(current_structdef) and
  1973. (symownerdef.owner.iscurrentunit)
  1974. ) or
  1975. (
  1976. { helpers can access protected symbols }
  1977. is_objectpascal_helper(contextobjdef) and
  1978. tobjectdef(contextobjdef).extendeddef.is_related(symownerdef)
  1979. )
  1980. )
  1981. );
  1982. end;
  1983. vis_public,
  1984. vis_published :
  1985. result:=true;
  1986. end;
  1987. end;
  1988. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  1989. begin
  1990. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1991. end;
  1992. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  1993. var
  1994. i : longint;
  1995. pd : tprocdef;
  1996. begin
  1997. if sym.typ=procsym then
  1998. begin
  1999. { A procsym is visible, when there is at least one of the procdefs visible }
  2000. result:=false;
  2001. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  2002. begin
  2003. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  2004. if (pd.owner=sym.owner) and
  2005. is_visible_for_object(pd,contextobjdef) then
  2006. begin
  2007. result:=true;
  2008. exit;
  2009. end;
  2010. end;
  2011. end
  2012. else
  2013. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  2014. end;
  2015. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2016. begin
  2017. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,false,sp_none);
  2018. end;
  2019. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchoption:boolean;option:tsymoption):boolean;
  2020. var
  2021. hashedid : THashedIDString;
  2022. contextstructdef : tabstractrecorddef;
  2023. stackitem : psymtablestackitem;
  2024. begin
  2025. result:=false;
  2026. hashedid.id:=s;
  2027. stackitem:=symtablestack.stack;
  2028. while assigned(stackitem) do
  2029. begin
  2030. srsymtable:=stackitem^.symtable;
  2031. if (srsymtable.symtabletype=objectsymtable) then
  2032. begin
  2033. { TODO : implement the search for an option in classes as well }
  2034. if searchoption then
  2035. begin
  2036. result:=false;
  2037. exit;
  2038. end;
  2039. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,true) then
  2040. begin
  2041. result:=true;
  2042. exit;
  2043. end;
  2044. end
  2045. else
  2046. begin
  2047. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2048. if assigned(srsym) then
  2049. begin
  2050. { use the class from withsymtable only when it is
  2051. defined in this unit }
  2052. if (srsymtable.symtabletype=withsymtable) and
  2053. assigned(srsymtable.defowner) and
  2054. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  2055. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  2056. (srsymtable.defowner.owner.iscurrentunit) then
  2057. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  2058. else
  2059. contextstructdef:=current_structdef;
  2060. if not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  2061. is_visible_for_object(srsym,contextstructdef) and
  2062. (not searchoption or (option in srsym.symoptions)) then
  2063. begin
  2064. { we need to know if a procedure references symbols
  2065. in the static symtable, because then it can't be
  2066. inlined from outside this unit }
  2067. if assigned(current_procinfo) and
  2068. (srsym.owner.symtabletype=staticsymtable) then
  2069. include(current_procinfo.flags,pi_uses_static_symtable);
  2070. addsymref(srsym);
  2071. result:=true;
  2072. exit;
  2073. end;
  2074. end;
  2075. end;
  2076. stackitem:=stackitem^.next;
  2077. end;
  2078. srsym:=nil;
  2079. srsymtable:=nil;
  2080. end;
  2081. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  2082. srsymtable:TSymtable;option:tsymoption):boolean;
  2083. begin
  2084. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,true,option);
  2085. end;
  2086. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2087. var
  2088. hashedid : THashedIDString;
  2089. stackitem : psymtablestackitem;
  2090. classh : tobjectdef;
  2091. begin
  2092. result:=false;
  2093. hashedid.id:=s;
  2094. stackitem:=symtablestack.stack;
  2095. while assigned(stackitem) do
  2096. begin
  2097. {
  2098. It is not possible to have type symbols in:
  2099. parameters
  2100. Exception are classes, objects, records, generic definitions and specializations
  2101. that have the parameterized types inserted in the symtable.
  2102. }
  2103. srsymtable:=stackitem^.symtable;
  2104. if (srsymtable.symtabletype=ObjectSymtable) then
  2105. begin
  2106. classh:=tobjectdef(srsymtable.defowner);
  2107. while assigned(classh) do
  2108. begin
  2109. srsymtable:=classh.symtable;
  2110. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2111. if assigned(srsym) and
  2112. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2113. is_visible_for_object(srsym,current_structdef) then
  2114. begin
  2115. addsymref(srsym);
  2116. result:=true;
  2117. exit;
  2118. end;
  2119. classh:=classh.childof;
  2120. end;
  2121. end
  2122. else
  2123. begin
  2124. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2125. if assigned(srsym) and
  2126. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  2127. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  2128. begin
  2129. { we need to know if a procedure references symbols
  2130. in the static symtable, because then it can't be
  2131. inlined from outside this unit }
  2132. if assigned(current_procinfo) and
  2133. (srsym.owner.symtabletype=staticsymtable) then
  2134. include(current_procinfo.flags,pi_uses_static_symtable);
  2135. addsymref(srsym);
  2136. result:=true;
  2137. exit;
  2138. end;
  2139. end;
  2140. stackitem:=stackitem^.next;
  2141. end;
  2142. result:=false;
  2143. srsym:=nil;
  2144. srsymtable:=nil;
  2145. end;
  2146. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2147. var
  2148. pmod : tmodule;
  2149. begin
  2150. pmod:=tmodule(pm);
  2151. result:=false;
  2152. if assigned(pmod.globalsymtable) then
  2153. begin
  2154. srsym:=tsym(pmod.globalsymtable.Find(s));
  2155. if assigned(srsym) then
  2156. begin
  2157. srsymtable:=pmod.globalsymtable;
  2158. addsymref(srsym);
  2159. result:=true;
  2160. exit;
  2161. end;
  2162. end;
  2163. { If the module is the current unit we also need
  2164. to search the local symtable }
  2165. if (pmod=current_module) and
  2166. assigned(pmod.localsymtable) then
  2167. begin
  2168. srsym:=tsym(pmod.localsymtable.Find(s));
  2169. if assigned(srsym) then
  2170. begin
  2171. srsymtable:=pmod.localsymtable;
  2172. addsymref(srsym);
  2173. result:=true;
  2174. exit;
  2175. end;
  2176. end;
  2177. srsym:=nil;
  2178. srsymtable:=nil;
  2179. end;
  2180. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  2181. var
  2182. stackitem : psymtablestackitem;
  2183. begin
  2184. result:=false;
  2185. stackitem:=symtablestack.stack;
  2186. while assigned(stackitem) do
  2187. begin
  2188. srsymtable:=stackitem^.symtable;
  2189. if (srsymtable.symtabletype=globalsymtable) and
  2190. (srsymtable.name^=unitname) then
  2191. begin
  2192. srsym:=tsym(srsymtable.find(symname));
  2193. if not assigned(srsym) then
  2194. break;
  2195. result:=true;
  2196. exit;
  2197. end;
  2198. stackitem:=stackitem^.next;
  2199. end;
  2200. { If the module is the current unit we also need
  2201. to search the local symtable }
  2202. if assigned(current_module.localsymtable) and
  2203. (current_module.localsymtable.name^=unitname) then
  2204. begin
  2205. srsymtable:=current_module.localsymtable;
  2206. srsym:=tsym(srsymtable.find(symname));
  2207. if assigned(srsym) then
  2208. begin
  2209. result:=true;
  2210. exit;
  2211. end;
  2212. end;
  2213. end;
  2214. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  2215. begin
  2216. result:=pd;
  2217. if pd.typ<>objectdef then
  2218. exit;
  2219. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  2220. end;
  2221. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  2222. var
  2223. hashedid : THashedIDString;
  2224. stackitem : psymtablestackitem;
  2225. srsymtable : tsymtable;
  2226. srsym : tsym;
  2227. formalname,
  2228. foundname : shortstring;
  2229. formalnameptr,
  2230. foundnameptr: pshortstring;
  2231. begin
  2232. { not a formal definition -> return it }
  2233. if not(oo_is_formal in pd.objectoptions) then
  2234. begin
  2235. result:=pd;
  2236. exit;
  2237. end;
  2238. hashedid.id:=pd.typesym.name;
  2239. stackitem:=symtablestack.stack;
  2240. while assigned(stackitem) do
  2241. begin
  2242. srsymtable:=stackitem^.symtable;
  2243. { ObjC classes can't appear in generics or as nested class
  2244. definitions. Java classes can. }
  2245. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  2246. (is_java_class_or_interface(pd) and
  2247. (srsymtable.symtabletype=ObjectSymtable)) then
  2248. begin
  2249. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2250. if assigned(srsym) and
  2251. (srsym.typ=typesym) and
  2252. (ttypesym(srsym).typedef.typ=objectdef) and
  2253. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  2254. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2255. begin
  2256. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  2257. begin
  2258. { the external name for the formal and the real
  2259. definition must match }
  2260. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  2261. assigned(pd.import_lib) then
  2262. begin
  2263. if assigned(pd.import_lib) then
  2264. formalname:=pd.import_lib^+'.'
  2265. else
  2266. formalname:='';
  2267. formalname:=formalname+pd.objextname^;
  2268. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  2269. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  2270. else
  2271. foundname:='';
  2272. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  2273. formalnameptr:=@formalname;
  2274. foundnameptr:=@foundname;
  2275. end
  2276. else
  2277. begin
  2278. formalnameptr:=pd.objextname;
  2279. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  2280. end;
  2281. if foundnameptr^<>formalnameptr^ then
  2282. begin
  2283. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  2284. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  2285. end;
  2286. end;
  2287. result:=tobjectdef(ttypesym(srsym).typedef);
  2288. if assigned(current_procinfo) and
  2289. (srsym.owner.symtabletype=staticsymtable) then
  2290. include(current_procinfo.flags,pi_uses_static_symtable);
  2291. addsymref(srsym);
  2292. exit;
  2293. end;
  2294. end;
  2295. stackitem:=stackitem^.next;
  2296. end;
  2297. { nothing found: optionally give an error and return the original
  2298. (empty) one }
  2299. if erroronfailure then
  2300. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  2301. result:=pd;
  2302. end;
  2303. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;searchhelper:boolean):boolean;
  2304. var
  2305. hashedid : THashedIDString;
  2306. orgclass : tobjectdef;
  2307. i : longint;
  2308. hlpsrsym : tsym;
  2309. hlpsrsymtable : tsymtable;
  2310. begin
  2311. orgclass:=classh;
  2312. { in case this is a formal class, first find the real definition }
  2313. if assigned(classh) then
  2314. begin
  2315. if (oo_is_formal in classh.objectoptions) then
  2316. classh:=find_real_class_definition(classh,true);
  2317. { The contextclassh is used for visibility. The classh must be equal to
  2318. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  2319. parent or a class helper. }
  2320. if not (contextclassh.is_related(classh) or
  2321. (is_classhelper(contextclassh) and
  2322. assigned(tobjectdef(contextclassh).extendeddef) and
  2323. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  2324. tobjectdef(contextclassh).extendeddef.is_related(classh))) then
  2325. internalerror(200811161);
  2326. end;
  2327. result:=false;
  2328. hashedid.id:=s;
  2329. { an Objective-C protocol or Java interface can inherit from multiple
  2330. other protocols/interfaces -> use ImplementedInterfaces instead }
  2331. if is_objcprotocol(classh) or
  2332. is_javainterface(classh) then
  2333. begin
  2334. srsymtable:=classh.symtable;
  2335. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2336. if assigned(srsym) and
  2337. is_visible_for_object(srsym,contextclassh) then
  2338. begin
  2339. addsymref(srsym);
  2340. result:=true;
  2341. exit;
  2342. end;
  2343. for i:=0 to classh.ImplementedInterfaces.count-1 do
  2344. begin
  2345. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,false) then
  2346. begin
  2347. result:=true;
  2348. exit;
  2349. end;
  2350. end;
  2351. end
  2352. else
  2353. if is_objectpascal_helper(classh) then
  2354. begin
  2355. { helpers have their own obscure search logic... }
  2356. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,false);
  2357. if result then
  2358. exit;
  2359. end
  2360. else
  2361. begin
  2362. hlpsrsym:=nil;
  2363. hlpsrsymtable:=nil;
  2364. while assigned(classh) do
  2365. begin
  2366. { search for a class helper method first if this is an Object
  2367. Pascal class and we haven't yet found a helper symbol }
  2368. if is_class(classh) and searchhelper and not assigned(hlpsrsym) then
  2369. begin
  2370. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  2371. if result then
  2372. { if the procsym is overloaded we need to use the
  2373. "original" symbol; the helper symbol will be found when
  2374. searching for overloads }
  2375. if (srsym.typ<>procsym) or
  2376. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2377. exit
  2378. else
  2379. begin
  2380. { remember the found symbol if the class hierarchy
  2381. should not contain the a method with that name }
  2382. hlpsrsym:=srsym;
  2383. hlpsrsymtable:=srsymtable;
  2384. end;
  2385. end;
  2386. srsymtable:=classh.symtable;
  2387. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2388. if assigned(srsym) and
  2389. is_visible_for_object(srsym,contextclassh) then
  2390. begin
  2391. addsymref(srsym);
  2392. result:=true;
  2393. exit;
  2394. end;
  2395. classh:=classh.childof;
  2396. end;
  2397. { did we find a helper symbol, but no symbol with the same name in
  2398. the extended object's hierarchy? }
  2399. if assigned(hlpsrsym) then
  2400. begin
  2401. srsym:=hlpsrsym;
  2402. srsymtable:=hlpsrsymtable;
  2403. result:=true;
  2404. exit;
  2405. end;
  2406. end;
  2407. if is_objcclass(orgclass) then
  2408. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  2409. else
  2410. begin
  2411. srsym:=nil;
  2412. srsymtable:=nil;
  2413. end;
  2414. end;
  2415. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2416. var
  2417. hashedid : THashedIDString;
  2418. hlpsrsym : tsym;
  2419. hlpsrsymtable : tsymtable;
  2420. begin
  2421. result:=false;
  2422. hlpsrsym:=nil;
  2423. hlpsrsymtable:=nil;
  2424. hashedid.id:=s;
  2425. { search for a record helper method first }
  2426. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  2427. if result then
  2428. { if the procsym is overloaded we need to use the
  2429. "original" symbol; the helper symbol will be found when
  2430. searching for overloads }
  2431. if (srsym.typ<>procsym) or
  2432. not (sp_has_overloaded in tprocsym(srsym).symoptions) then
  2433. exit
  2434. else
  2435. begin
  2436. { remember the found symbol if we should not find a symbol with
  2437. the same name in the extended record }
  2438. hlpsrsym:=srsym;
  2439. hlpsrsymtable:=srsymtable;
  2440. end;
  2441. srsymtable:=recordh.symtable;
  2442. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2443. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  2444. begin
  2445. addsymref(srsym);
  2446. result:=true;
  2447. exit;
  2448. end;
  2449. srsym:=hlpsrsym;
  2450. srsymtable:=hlpsrsymtable;
  2451. result:=assigned(srsym);
  2452. end;
  2453. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2454. var
  2455. def : tdef;
  2456. i : longint;
  2457. begin
  2458. { in case this is a formal class, first find the real definition }
  2459. if assigned(classh) and
  2460. (oo_is_formal in classh.objectoptions) then
  2461. classh:=find_real_class_definition(classh,true);
  2462. result:=false;
  2463. def:=nil;
  2464. while assigned(classh) do
  2465. begin
  2466. for i:=0 to classh.symtable.DefList.Count-1 do
  2467. begin
  2468. def:=tstoreddef(classh.symtable.DefList[i]);
  2469. { Find also all hidden private methods to
  2470. be compatible with delphi, see tw6203 (PFV) }
  2471. if (def.typ=procdef) and
  2472. (po_msgint in tprocdef(def).procoptions) and
  2473. (tprocdef(def).messageinf.i=msgid) then
  2474. begin
  2475. srdef:=def;
  2476. srsym:=tprocdef(def).procsym;
  2477. srsymtable:=classh.symtable;
  2478. addsymref(srsym);
  2479. result:=true;
  2480. exit;
  2481. end;
  2482. end;
  2483. classh:=classh.childof;
  2484. end;
  2485. srdef:=nil;
  2486. srsym:=nil;
  2487. srsymtable:=nil;
  2488. end;
  2489. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  2490. var
  2491. def : tdef;
  2492. i : longint;
  2493. begin
  2494. { in case this is a formal class, first find the real definition }
  2495. if assigned(classh) and
  2496. (oo_is_formal in classh.objectoptions) then
  2497. classh:=find_real_class_definition(classh,true);
  2498. result:=false;
  2499. def:=nil;
  2500. while assigned(classh) do
  2501. begin
  2502. for i:=0 to classh.symtable.DefList.Count-1 do
  2503. begin
  2504. def:=tstoreddef(classh.symtable.DefList[i]);
  2505. { Find also all hidden private methods to
  2506. be compatible with delphi, see tw6203 (PFV) }
  2507. if (def.typ=procdef) and
  2508. (po_msgstr in tprocdef(def).procoptions) and
  2509. (tprocdef(def).messageinf.str^=s) then
  2510. begin
  2511. srsym:=tprocdef(def).procsym;
  2512. srsymtable:=classh.symtable;
  2513. addsymref(srsym);
  2514. result:=true;
  2515. exit;
  2516. end;
  2517. end;
  2518. classh:=classh.childof;
  2519. end;
  2520. srsym:=nil;
  2521. srsymtable:=nil;
  2522. end;
  2523. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;aHasInherited:boolean):boolean;
  2524. var
  2525. hashedid : THashedIDString;
  2526. parentclassh : tobjectdef;
  2527. begin
  2528. result:=false;
  2529. if not is_objectpascal_helper(classh) then
  2530. Internalerror(2011030101);
  2531. hashedid.id:=s;
  2532. { in a helper things are a bit more complex:
  2533. 1. search the symbol in the helper (if not "inherited")
  2534. 2. search the symbol in the extended type
  2535. 3. search the symbol in the parent helpers
  2536. 4. only classes: search the symbol in the parents of the extended type
  2537. }
  2538. if not aHasInherited then
  2539. begin
  2540. { search in the helper itself }
  2541. srsymtable:=classh.symtable;
  2542. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2543. if assigned(srsym) and
  2544. is_visible_for_object(srsym,contextclassh) then
  2545. begin
  2546. addsymref(srsym);
  2547. result:=true;
  2548. exit;
  2549. end;
  2550. end;
  2551. { now search in the extended type itself }
  2552. if classh.extendeddef.typ in [recorddef,objectdef] then
  2553. begin
  2554. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  2555. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2556. if assigned(srsym) and
  2557. is_visible_for_object(srsym,contextclassh) then
  2558. begin
  2559. addsymref(srsym);
  2560. result:=true;
  2561. exit;
  2562. end;
  2563. end;
  2564. { now search in the parent helpers }
  2565. parentclassh:=classh.childof;
  2566. while assigned(parentclassh) do
  2567. begin
  2568. srsymtable:=parentclassh.symtable;
  2569. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2570. if assigned(srsym) and
  2571. is_visible_for_object(srsym,contextclassh) then
  2572. begin
  2573. addsymref(srsym);
  2574. result:=true;
  2575. exit;
  2576. end;
  2577. parentclassh:=parentclassh.childof;
  2578. end;
  2579. if is_class(classh.extendeddef) then
  2580. { now search in the parents of the extended class (with helpers!) }
  2581. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,true);
  2582. { addsymref is already called by searchsym_in_class }
  2583. end;
  2584. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  2585. var
  2586. sym : Tprocsym;
  2587. hashedid : THashedIDString;
  2588. curreq,
  2589. besteq : tequaltype;
  2590. currpd,
  2591. bestpd : tprocdef;
  2592. stackitem : psymtablestackitem;
  2593. begin
  2594. hashedid.id:=overloaded_names[assignment_type];
  2595. besteq:=te_incompatible;
  2596. bestpd:=nil;
  2597. stackitem:=symtablestack.stack;
  2598. while assigned(stackitem) do
  2599. begin
  2600. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2601. if sym<>nil then
  2602. begin
  2603. if sym.typ<>procsym then
  2604. internalerror(200402031);
  2605. { if the source type is an alias then this is only the second choice,
  2606. if you mess with this code, check tw4093 }
  2607. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  2608. if curreq>besteq then
  2609. begin
  2610. besteq:=curreq;
  2611. bestpd:=currpd;
  2612. if (besteq=te_exact) then
  2613. break;
  2614. end;
  2615. end;
  2616. stackitem:=stackitem^.next;
  2617. end;
  2618. result:=bestpd;
  2619. end;
  2620. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  2621. begin
  2622. { search record/object symtable first for a suitable operator }
  2623. if from_def.typ in [recorddef,objectdef] then
  2624. symtablestack.push(tabstractrecorddef(from_def).symtable);
  2625. if to_def.typ in [recorddef,objectdef] then
  2626. symtablestack.push(tabstractrecorddef(to_def).symtable);
  2627. { if type conversion is explicit then search first for explicit
  2628. operator overload and if not found then use implicit operator }
  2629. if explicit then
  2630. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  2631. else
  2632. result:=nil;
  2633. if result=nil then
  2634. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  2635. { restore symtable stack }
  2636. if to_def.typ in [recorddef,objectdef] then
  2637. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  2638. if from_def.typ in [recorddef,objectdef] then
  2639. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  2640. end;
  2641. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  2642. var
  2643. sym : Tprocsym;
  2644. hashedid : THashedIDString;
  2645. curreq,
  2646. besteq : tequaltype;
  2647. currpd,
  2648. bestpd : tprocdef;
  2649. stackitem : psymtablestackitem;
  2650. begin
  2651. hashedid.id:='enumerator';
  2652. besteq:=te_incompatible;
  2653. bestpd:=nil;
  2654. stackitem:=symtablestack.stack;
  2655. while assigned(stackitem) do
  2656. begin
  2657. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  2658. if sym<>nil then
  2659. begin
  2660. if sym.typ<>procsym then
  2661. internalerror(200910241);
  2662. { if the source type is an alias then this is only the second choice,
  2663. if you mess with this code, check tw4093 }
  2664. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  2665. if curreq>besteq then
  2666. begin
  2667. besteq:=curreq;
  2668. bestpd:=currpd;
  2669. if (besteq=te_exact) then
  2670. break;
  2671. end;
  2672. end;
  2673. stackitem:=stackitem^.next;
  2674. end;
  2675. result:=bestpd;
  2676. end;
  2677. function search_system_type(const s: TIDString): ttypesym;
  2678. var
  2679. sym : tsym;
  2680. begin
  2681. sym:=tsym(systemunit.Find(s));
  2682. if not assigned(sym) or
  2683. (sym.typ<>typesym) then
  2684. cgmessage1(cg_f_unknown_system_type,s);
  2685. result:=ttypesym(sym);
  2686. end;
  2687. function try_search_system_type(const s: TIDString): ttypesym;
  2688. var
  2689. sym : tsym;
  2690. begin
  2691. sym:=tsym(systemunit.Find(s));
  2692. if not assigned(sym) then
  2693. result:=nil
  2694. else
  2695. begin
  2696. if sym.typ<>typesym then
  2697. cgmessage1(cg_f_unknown_system_type,s);
  2698. result:=ttypesym(sym);
  2699. end;
  2700. end;
  2701. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  2702. var
  2703. srsymtable: tsymtable;
  2704. sym: tsym;
  2705. begin
  2706. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  2707. (sym.typ=typesym) then
  2708. begin
  2709. result:=ttypesym(sym);
  2710. exit;
  2711. end
  2712. else
  2713. begin
  2714. if throwerror then
  2715. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  2716. result:=nil;
  2717. end;
  2718. end;
  2719. function search_last_objectpascal_helper(pd,contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  2720. var
  2721. s: string;
  2722. list: TFPObjectList;
  2723. i: integer;
  2724. st: tsymtable;
  2725. begin
  2726. result:=false;
  2727. odef:=nil;
  2728. { when there are no helpers active currently then we don't need to do
  2729. anything }
  2730. if current_module.extendeddefs.count=0 then
  2731. exit;
  2732. { no helpers for anonymous types }
  2733. if not assigned(pd.objrealname) or (pd.objrealname^='') then
  2734. exit;
  2735. { if pd is defined inside a procedure we must not use make_mangledname
  2736. (as a helper may not be defined in a procedure this is no problem...)}
  2737. st:=pd.owner;
  2738. while st.symtabletype in [objectsymtable,recordsymtable] do
  2739. st:=st.defowner.owner;
  2740. if st.symtabletype=localsymtable then
  2741. exit;
  2742. { the mangled name is used as the key for tmodule.extendeddefs }
  2743. s:=make_mangledname('',pd.symtable,'');
  2744. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  2745. if assigned(list) and (list.count>0) then
  2746. begin
  2747. i:=list.count-1;
  2748. repeat
  2749. odef:=tobjectdef(list[list.count-1]);
  2750. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  2751. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  2752. dec(i);
  2753. until result or (i<0);
  2754. if not result then
  2755. { just to be sure that noone uses odef }
  2756. odef:=nil;
  2757. end;
  2758. end;
  2759. function search_objectpascal_helper(pd,contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2760. var
  2761. hashedid : THashedIDString;
  2762. classh : tobjectdef;
  2763. i : integer;
  2764. pdef : tprocdef;
  2765. begin
  2766. result:=false;
  2767. { if there is no class helper for the class then there is no need to
  2768. search further }
  2769. if not search_last_objectpascal_helper(pd,contextclassh,classh) then
  2770. exit;
  2771. hashedid.id:=s;
  2772. repeat
  2773. srsymtable:=classh.symtable;
  2774. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2775. if srsym<>nil then
  2776. begin
  2777. if srsym.typ=propertysym then
  2778. begin
  2779. result:=true;
  2780. exit;
  2781. end;
  2782. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2783. begin
  2784. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  2785. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  2786. continue;
  2787. { we need to know if a procedure references symbols
  2788. in the static symtable, because then it can't be
  2789. inlined from outside this unit }
  2790. if assigned(current_procinfo) and
  2791. (srsym.owner.symtabletype=staticsymtable) then
  2792. include(current_procinfo.flags,pi_uses_static_symtable);
  2793. { the first found method wins }
  2794. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2795. srsymtable:=srsym.owner;
  2796. addsymref(srsym);
  2797. result:=true;
  2798. exit;
  2799. end;
  2800. end;
  2801. { try the helper parent if available }
  2802. classh:=classh.childof;
  2803. until classh=nil;
  2804. srsym:=nil;
  2805. srsymtable:=nil;
  2806. end;
  2807. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2808. var
  2809. hashedid : THashedIDString;
  2810. stackitem : psymtablestackitem;
  2811. i : longint;
  2812. defowner : tobjectdef;
  2813. begin
  2814. hashedid.id:=class_helper_prefix+s;
  2815. stackitem:=symtablestack.stack;
  2816. while assigned(stackitem) do
  2817. begin
  2818. srsymtable:=stackitem^.symtable;
  2819. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2820. if assigned(srsym) then
  2821. begin
  2822. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2823. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2824. (srsym.typ<>procsym) then
  2825. internalerror(2009111505);
  2826. { check whether this procsym includes a helper for this particular class }
  2827. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2828. begin
  2829. { does pd inherit from (or is the same as) the class
  2830. that this method's category extended?
  2831. Warning: this list contains both category and objcclass methods
  2832. (for id.randommethod), so only check category methods here
  2833. }
  2834. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  2835. if (oo_is_classhelper in defowner.objectoptions) and
  2836. pd.is_related(defowner.childof) then
  2837. begin
  2838. { we need to know if a procedure references symbols
  2839. in the static symtable, because then it can't be
  2840. inlined from outside this unit }
  2841. if assigned(current_procinfo) and
  2842. (srsym.owner.symtabletype=staticsymtable) then
  2843. include(current_procinfo.flags,pi_uses_static_symtable);
  2844. { no need to keep looking. There might be other
  2845. categories that extend this, a parent or child
  2846. class with a method with the same name (either
  2847. overriding this one, or overridden by this one),
  2848. but that doesn't matter as far as the basic
  2849. procsym is concerned.
  2850. }
  2851. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2852. srsymtable:=srsym.owner;
  2853. addsymref(srsym);
  2854. result:=true;
  2855. exit;
  2856. end;
  2857. end;
  2858. end;
  2859. stackitem:=stackitem^.next;
  2860. end;
  2861. srsym:=nil;
  2862. srsymtable:=nil;
  2863. result:=false;
  2864. end;
  2865. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  2866. var
  2867. hashedid : THashedIDString;
  2868. stackitem : psymtablestackitem;
  2869. i : longint;
  2870. begin
  2871. hashedid.id:=class_helper_prefix+s;
  2872. stackitem:=symtablestack.stack;
  2873. while assigned(stackitem) do
  2874. begin
  2875. srsymtable:=stackitem^.symtable;
  2876. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  2877. if assigned(srsym) then
  2878. begin
  2879. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  2880. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  2881. (srsym.typ<>procsym) then
  2882. internalerror(2009112005);
  2883. { check whether this procsym includes a helper for this particular class }
  2884. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  2885. begin
  2886. { we need to know if a procedure references symbols
  2887. in the static symtable, because then it can't be
  2888. inlined from outside this unit }
  2889. if assigned(current_procinfo) and
  2890. (srsym.owner.symtabletype=staticsymtable) then
  2891. include(current_procinfo.flags,pi_uses_static_symtable);
  2892. { no need to keep looking. There might be other
  2893. methods with the same name, but that doesn't matter
  2894. as far as the basic procsym is concerned.
  2895. }
  2896. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  2897. { We need the symtable in which the classhelper-like sym
  2898. is located, not the objectdef. The reason is that the
  2899. callnode will climb the symtablestack until it encounters
  2900. this symtable to start looking for overloads (and it won't
  2901. find the objectsymtable in which this method sym is
  2902. located
  2903. srsymtable:=srsym.owner;
  2904. }
  2905. addsymref(srsym);
  2906. result:=true;
  2907. exit;
  2908. end;
  2909. end;
  2910. stackitem:=stackitem^.next;
  2911. end;
  2912. srsym:=nil;
  2913. srsymtable:=nil;
  2914. result:=false;
  2915. end;
  2916. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  2917. { searches n in symtable of pd and all anchestors }
  2918. var
  2919. hashedid : THashedIDString;
  2920. srsym : tsym;
  2921. orgpd : tabstractrecorddef;
  2922. srsymtable : tsymtable;
  2923. begin
  2924. { in case this is a formal class, first find the real definition }
  2925. if (oo_is_formal in pd.objectoptions) then
  2926. pd:=find_real_class_definition(tobjectdef(pd),true);
  2927. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  2928. exit;
  2929. hashedid.id:=s;
  2930. orgpd:=pd;
  2931. while assigned(pd) do
  2932. begin
  2933. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  2934. if assigned(srsym) then
  2935. begin
  2936. search_struct_member:=srsym;
  2937. exit;
  2938. end;
  2939. if pd.typ=objectdef then
  2940. pd:=tobjectdef(pd).childof
  2941. else
  2942. pd:=nil;
  2943. end;
  2944. { not found, now look for class helpers }
  2945. if is_objcclass(pd) then
  2946. search_objc_helper(tobjectdef(orgpd),s,result,srsymtable)
  2947. else
  2948. result:=nil;
  2949. end;
  2950. function search_macro(const s : string):tsym;
  2951. var
  2952. stackitem : psymtablestackitem;
  2953. hashedid : THashedIDString;
  2954. srsym : tsym;
  2955. begin
  2956. hashedid.id:=s;
  2957. { First search the localmacrosymtable before searching the
  2958. global macrosymtables from the units }
  2959. if assigned(current_module) then
  2960. begin
  2961. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2962. if assigned(srsym) then
  2963. begin
  2964. result:= srsym;
  2965. exit;
  2966. end;
  2967. end;
  2968. stackitem:=macrosymtablestack.stack;
  2969. while assigned(stackitem) do
  2970. begin
  2971. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2972. if assigned(srsym) then
  2973. begin
  2974. result:= srsym;
  2975. exit;
  2976. end;
  2977. stackitem:=stackitem^.next;
  2978. end;
  2979. result:= nil;
  2980. end;
  2981. function defined_macro(const s : string):boolean;
  2982. var
  2983. mac: tmacro;
  2984. begin
  2985. mac:=tmacro(search_macro(s));
  2986. if assigned(mac) then
  2987. begin
  2988. mac.is_used:=true;
  2989. defined_macro:=mac.defined;
  2990. end
  2991. else
  2992. defined_macro:=false;
  2993. end;
  2994. {****************************************************************************
  2995. Object Helpers
  2996. ****************************************************************************}
  2997. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  2998. { returns the default property of a class, searches also anchestors }
  2999. var
  3000. _defaultprop : tpropertysym;
  3001. helperpd : tobjectdef;
  3002. begin
  3003. _defaultprop:=nil;
  3004. { first search in helper's hierarchy }
  3005. if search_last_objectpascal_helper(pd,nil,helperpd) then
  3006. while assigned(helperpd) do
  3007. begin
  3008. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  3009. if assigned(_defaultprop) then
  3010. break;
  3011. helperpd:=helperpd.childof;
  3012. end;
  3013. if assigned(_defaultprop) then
  3014. begin
  3015. search_default_property:=_defaultprop;
  3016. exit;
  3017. end;
  3018. { now search in the type's hierarchy itself }
  3019. while assigned(pd) do
  3020. begin
  3021. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  3022. if assigned(_defaultprop) then
  3023. break;
  3024. if (pd.typ=objectdef) then
  3025. pd:=tobjectdef(pd).childof
  3026. else
  3027. break;
  3028. end;
  3029. search_default_property:=_defaultprop;
  3030. end;
  3031. {****************************************************************************
  3032. Macro Helpers
  3033. ****************************************************************************}
  3034. procedure def_system_macro(const name : string);
  3035. var
  3036. mac : tmacro;
  3037. s: string;
  3038. begin
  3039. if name = '' then
  3040. internalerror(2004121202);
  3041. s:= upper(name);
  3042. mac:=tmacro(search_macro(s));
  3043. if not assigned(mac) then
  3044. begin
  3045. mac:=tmacro.create(s);
  3046. if assigned(current_module) then
  3047. current_module.localmacrosymtable.insert(mac)
  3048. else
  3049. initialmacrosymtable.insert(mac);
  3050. end;
  3051. Message1(parser_c_macro_defined,mac.name);
  3052. mac.defined:=true;
  3053. end;
  3054. procedure set_system_macro(const name, value : string);
  3055. var
  3056. mac : tmacro;
  3057. s: string;
  3058. begin
  3059. if name = '' then
  3060. internalerror(2004121203);
  3061. s:= upper(name);
  3062. mac:=tmacro(search_macro(s));
  3063. if not assigned(mac) then
  3064. begin
  3065. mac:=tmacro.create(s);
  3066. if assigned(current_module) then
  3067. current_module.localmacrosymtable.insert(mac)
  3068. else
  3069. initialmacrosymtable.insert(mac);
  3070. end
  3071. else
  3072. begin
  3073. mac.is_compiler_var:=false;
  3074. if assigned(mac.buftext) then
  3075. freemem(mac.buftext,mac.buflen);
  3076. end;
  3077. Message2(parser_c_macro_set_to,mac.name,value);
  3078. mac.buflen:=length(value);
  3079. getmem(mac.buftext,mac.buflen);
  3080. move(value[1],mac.buftext^,mac.buflen);
  3081. mac.defined:=true;
  3082. end;
  3083. procedure set_system_compvar(const name, value : string);
  3084. var
  3085. mac : tmacro;
  3086. s: string;
  3087. begin
  3088. if name = '' then
  3089. internalerror(2004121204);
  3090. s:= upper(name);
  3091. mac:=tmacro(search_macro(s));
  3092. if not assigned(mac) then
  3093. begin
  3094. mac:=tmacro.create(s);
  3095. mac.is_compiler_var:=true;
  3096. if assigned(current_module) then
  3097. current_module.localmacrosymtable.insert(mac)
  3098. else
  3099. initialmacrosymtable.insert(mac);
  3100. end
  3101. else
  3102. begin
  3103. mac.is_compiler_var:=true;
  3104. if assigned(mac.buftext) then
  3105. freemem(mac.buftext,mac.buflen);
  3106. end;
  3107. Message2(parser_c_macro_set_to,mac.name,value);
  3108. mac.buflen:=length(value);
  3109. getmem(mac.buftext,mac.buflen);
  3110. move(value[1],mac.buftext^,mac.buflen);
  3111. mac.defined:=true;
  3112. end;
  3113. procedure undef_system_macro(const name : string);
  3114. var
  3115. mac : tmacro;
  3116. s: string;
  3117. begin
  3118. if name = '' then
  3119. internalerror(2004121205);
  3120. s:= upper(name);
  3121. mac:=tmacro(search_macro(s));
  3122. if not assigned(mac) then
  3123. {If not found, then it's already undefined.}
  3124. else
  3125. begin
  3126. Message1(parser_c_macro_undefined,mac.name);
  3127. mac.defined:=false;
  3128. mac.is_compiler_var:=false;
  3129. { delete old definition }
  3130. if assigned(mac.buftext) then
  3131. begin
  3132. freemem(mac.buftext,mac.buflen);
  3133. mac.buftext:=nil;
  3134. end;
  3135. end;
  3136. end;
  3137. {$ifdef UNITALIASES}
  3138. {****************************************************************************
  3139. TUNIT_ALIAS
  3140. ****************************************************************************}
  3141. constructor tunit_alias.create(const n:string);
  3142. var
  3143. i : longint;
  3144. begin
  3145. i:=pos('=',n);
  3146. if i=0 then
  3147. fail;
  3148. inherited createname(Copy(n,1,i-1));
  3149. newname:=stringdup(Copy(n,i+1,255));
  3150. end;
  3151. destructor tunit_alias.destroy;
  3152. begin
  3153. stringdispose(newname);
  3154. inherited destroy;
  3155. end;
  3156. procedure addunitalias(const n:string);
  3157. begin
  3158. unitaliases^.insert(tunit_alias,init(Upper(n))));
  3159. end;
  3160. function getunitalias(const n:string):string;
  3161. var
  3162. p : punit_alias;
  3163. begin
  3164. p:=punit_alias(unitaliases^.Find(Upper(n)));
  3165. if assigned(p) then
  3166. getunitalias:=punit_alias(p).newname^
  3167. else
  3168. getunitalias:=n;
  3169. end;
  3170. {$endif UNITALIASES}
  3171. {****************************************************************************
  3172. Init/Done Symtable
  3173. ****************************************************************************}
  3174. procedure InitSymtable;
  3175. begin
  3176. { Reset symbolstack }
  3177. symtablestack:=nil;
  3178. systemunit:=nil;
  3179. { create error syms and def }
  3180. generrorsym:=terrorsym.create;
  3181. generrordef:=terrordef.create;
  3182. { macros }
  3183. initialmacrosymtable:=tmacrosymtable.create(false);
  3184. macrosymtablestack:=TSymtablestack.create;
  3185. macrosymtablestack.push(initialmacrosymtable);
  3186. {$ifdef UNITALIASES}
  3187. { unit aliases }
  3188. unitaliases:=TFPHashObjectList.create;
  3189. {$endif}
  3190. { set some global vars to nil, might be important for the ide }
  3191. class_tobject:=nil;
  3192. interface_iunknown:=nil;
  3193. interface_idispatch:=nil;
  3194. rec_tguid:=nil;
  3195. dupnr:=0;
  3196. end;
  3197. procedure DoneSymtable;
  3198. begin
  3199. generrorsym.owner:=nil;
  3200. generrorsym.free;
  3201. generrordef.owner:=nil;
  3202. generrordef.free;
  3203. initialmacrosymtable.free;
  3204. macrosymtablestack.free;
  3205. {$ifdef UNITALIASES}
  3206. unitaliases.free;
  3207. {$endif}
  3208. end;
  3209. end.