symtable.pas 134 KB

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