symtable.pas 131 KB

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