symtable.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. cpuinfo,globtype,tokens,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,symsym,
  27. { ppu }
  28. ppu,
  29. { assembler }
  30. aasmtai,aasmdata
  31. ;
  32. {****************************************************************************
  33. Symtable types
  34. ****************************************************************************}
  35. type
  36. tstoredsymtable = class(TSymtable)
  37. private
  38. b_needs_init_final : boolean;
  39. procedure _needs_init_final(sym:TObject;arg:pointer);
  40. procedure check_forward(sym:TObject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  51. procedure delete(sym:TSymEntry);override;
  52. { load/write }
  53. procedure ppuload(ppufile:tcompilerppufile);virtual;
  54. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  55. procedure buildderef;virtual;
  56. procedure buildderefimpl;virtual;
  57. procedure deref;virtual;
  58. procedure derefimpl;virtual;
  59. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  60. procedure reset_all_defs;virtual;
  61. procedure allsymbolsused;
  62. procedure allprivatesused;
  63. procedure check_forwards;
  64. procedure checklabels;
  65. function needs_init_final : boolean;
  66. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  67. end;
  68. tabstractrecordsymtable = class(tstoredsymtable)
  69. public
  70. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  71. recordalignment, { alignment desired when inserting this record }
  72. fieldalignment, { alignment current alignment used when fields are inserted }
  73. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  74. constructor create(const n:string;usealign:shortint);
  75. procedure ppuload(ppufile:tcompilerppufile);override;
  76. procedure ppuwrite(ppufile:tcompilerppufile);override;
  77. procedure alignrecord(fieldoffset:aint;varalign:shortint);
  78. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  79. procedure addalignmentpadding;
  80. procedure insertdef(def:TDefEntry);override;
  81. function is_packed: boolean;
  82. protected
  83. _datasize : aint;
  84. { size in bits of the data in case of bitpacked record. Only important during construction, }
  85. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  86. databitsize : aint;
  87. procedure setdatasize(val: aint);
  88. public
  89. property datasize : aint read _datasize write setdatasize;
  90. end;
  91. trecordsymtable = class(tabstractrecordsymtable)
  92. public
  93. constructor create(usealign:shortint);
  94. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  95. end;
  96. tObjectSymtable = class(tabstractrecordsymtable)
  97. public
  98. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  99. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  100. end;
  101. { tabstractlocalsymtable }
  102. tabstractlocalsymtable = class(tstoredsymtable)
  103. public
  104. procedure ppuwrite(ppufile:tcompilerppufile);override;
  105. function count_locals:longint;
  106. end;
  107. tlocalsymtable = class(tabstractlocalsymtable)
  108. public
  109. constructor create(adefowner:tdef;level:byte);
  110. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  111. end;
  112. tparasymtable = class(tabstractlocalsymtable)
  113. public
  114. constructor create(adefowner:tdef;level:byte);
  115. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  116. end;
  117. tabstractuniTSymtable = class(tstoredsymtable)
  118. public
  119. constructor create(const n : string;id:word);
  120. function iscurrentunit:boolean;override;
  121. end;
  122. tglobalsymtable = class(tabstractuniTSymtable)
  123. public
  124. unittypecount : word;
  125. constructor create(const n : string;id:word);
  126. procedure ppuload(ppufile:tcompilerppufile);override;
  127. procedure ppuwrite(ppufile:tcompilerppufile);override;
  128. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  129. end;
  130. tstaticsymtable = class(tabstractuniTSymtable)
  131. public
  132. constructor create(const n : string;id:word);
  133. procedure ppuload(ppufile:tcompilerppufile);override;
  134. procedure ppuwrite(ppufile:tcompilerppufile);override;
  135. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  136. end;
  137. twithsymtable = class(TSymtable)
  138. withrefnode : tobject; { tnode }
  139. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  140. destructor destroy;override;
  141. procedure clear;override;
  142. procedure insertdef(def:TDefEntry);override;
  143. end;
  144. tstt_excepTSymtable = class(TSymtable)
  145. public
  146. constructor create;
  147. end;
  148. tmacrosymtable = class(tstoredsymtable)
  149. public
  150. constructor create(exported: boolean);
  151. end;
  152. var
  153. systemunit : tglobalsymtable; { pointer to the system unit }
  154. {****************************************************************************
  155. Functions
  156. ****************************************************************************}
  157. {*** Misc ***}
  158. function FullTypeName(def,otherdef:tdef):string;
  159. procedure incompatibletypes(def1,def2:tdef);
  160. procedure hidesym(sym:TSymEntry);
  161. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  162. {*** Search ***}
  163. procedure addsymref(sym:tsym);
  164. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  165. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  166. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  167. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  168. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  169. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  170. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  171. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  172. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  173. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  174. function search_system_type(const s: TIDString): ttypesym;
  175. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  176. function search_class_member(pd : tobjectdef;const s : string):tsym;
  177. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  178. function search_enumerator_operator(type_def:Tdef):Tprocdef;
  179. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  180. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  181. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  182. {and returns it if found. Returns nil otherwise.}
  183. function search_macro(const s : string):tsym;
  184. { Additionally to searching for a macro, also checks whether it's still }
  185. { actually defined (could be disable using "undef") }
  186. function defined_macro(const s : string):boolean;
  187. {*** Object Helpers ***}
  188. function search_default_property(pd : tobjectdef) : tpropertysym;
  189. {*** Macro Helpers ***}
  190. {If called initially, the following procedures manipulate macros in }
  191. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  192. {Name can be given in any case (it will be converted to upper case).}
  193. procedure def_system_macro(const name : string);
  194. procedure set_system_macro(const name, value : string);
  195. procedure set_system_compvar(const name, value : string);
  196. procedure undef_system_macro(const name : string);
  197. {*** symtable stack ***}
  198. { $ifdef DEBUG
  199. procedure test_symtablestack;
  200. procedure list_symtablestack;
  201. $endif DEBUG}
  202. {$ifdef UNITALIASES}
  203. type
  204. punit_alias = ^tunit_alias;
  205. tunit_alias = object(TNamedIndexItem)
  206. newname : pshortstring;
  207. constructor init(const n:string);
  208. destructor done;virtual;
  209. end;
  210. var
  211. unitaliases : pdictionary;
  212. procedure addunitalias(const n:string);
  213. function getunitalias(const n:string):string;
  214. {$endif UNITALIASES}
  215. {*** Init / Done ***}
  216. procedure IniTSymtable;
  217. procedure DoneSymtable;
  218. const
  219. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  220. ('error',
  221. 'plus','minus','star','slash','equal',
  222. 'greater','lower','greater_or_equal',
  223. 'lower_or_equal',
  224. 'sym_diff','starstar',
  225. 'as','is','in','or',
  226. 'and','div','mod','not','shl','shr','xor',
  227. 'assign','enumerator');
  228. implementation
  229. uses
  230. { global }
  231. verbose,globals,
  232. { target }
  233. systems,
  234. { symtable }
  235. symutil,defcmp,defutil,
  236. { module }
  237. fmodule,
  238. { codegen }
  239. procinfo
  240. ;
  241. var
  242. dupnr : longint; { unique number for duplicate symbols }
  243. {*****************************************************************************
  244. TStoredSymtable
  245. *****************************************************************************}
  246. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  247. begin
  248. inherited insert(sym,checkdup);
  249. end;
  250. procedure tstoredsymtable.delete(sym:TSymEntry);
  251. begin
  252. inherited delete(sym);
  253. end;
  254. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  255. begin
  256. { load definitions }
  257. loaddefs(ppufile);
  258. { load symbols }
  259. loadsyms(ppufile);
  260. end;
  261. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  262. begin
  263. { write definitions }
  264. writedefs(ppufile);
  265. { write symbols }
  266. writesyms(ppufile);
  267. end;
  268. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  269. var
  270. def : tdef;
  271. b : byte;
  272. begin
  273. { load start of definition section, which holds the amount of defs }
  274. if ppufile.readentry<>ibstartdefs then
  275. Message(unit_f_ppu_read_error);
  276. { read definitions }
  277. repeat
  278. b:=ppufile.readentry;
  279. case b of
  280. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  281. ibarraydef : def:=tarraydef.ppuload(ppufile);
  282. iborddef : def:=torddef.ppuload(ppufile);
  283. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  284. ibprocdef : def:=tprocdef.ppuload(ppufile);
  285. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  286. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  287. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  288. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  289. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  290. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  291. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  292. ibenumdef : def:=tenumdef.ppuload(ppufile);
  293. ibsetdef : def:=tsetdef.ppuload(ppufile);
  294. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  295. ibfiledef : def:=tfiledef.ppuload(ppufile);
  296. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  297. ibformaldef : def:=tformaldef.ppuload(ppufile);
  298. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  299. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  300. ibenddefs : break;
  301. ibend : Message(unit_f_ppu_read_error);
  302. else
  303. Message1(unit_f_ppu_invalid_entry,tostr(b));
  304. end;
  305. InsertDef(def);
  306. until false;
  307. end;
  308. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  309. var
  310. b : byte;
  311. sym : tsym;
  312. begin
  313. { load start of definition section, which holds the amount of defs }
  314. if ppufile.readentry<>ibstartsyms then
  315. Message(unit_f_ppu_read_error);
  316. { now read the symbols }
  317. repeat
  318. b:=ppufile.readentry;
  319. case b of
  320. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  321. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  322. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  323. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  324. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  325. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  326. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  327. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  328. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  329. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  330. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  331. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  332. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  333. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  334. ibendsyms : break;
  335. ibend : Message(unit_f_ppu_read_error);
  336. else
  337. Message1(unit_f_ppu_invalid_entry,tostr(b));
  338. end;
  339. Insert(sym,false);
  340. until false;
  341. end;
  342. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  343. var
  344. i : longint;
  345. def : tstoreddef;
  346. begin
  347. { each definition get a number, write then the amount of defs to the
  348. ibstartdef entry }
  349. ppufile.putlongint(DefList.count);
  350. ppufile.writeentry(ibstartdefs);
  351. { now write the definition }
  352. for i:=0 to DefList.Count-1 do
  353. begin
  354. def:=tstoreddef(DefList[i]);
  355. def.ppuwrite(ppufile);
  356. end;
  357. { write end of definitions }
  358. ppufile.writeentry(ibenddefs);
  359. end;
  360. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  361. var
  362. i : longint;
  363. sym : Tstoredsym;
  364. begin
  365. { each definition get a number, write then the amount of syms and the
  366. datasize to the ibsymdef entry }
  367. ppufile.putlongint(SymList.count);
  368. ppufile.writeentry(ibstartsyms);
  369. { foreach is used to write all symbols }
  370. for i:=0 to SymList.Count-1 do
  371. begin
  372. sym:=tstoredsym(SymList[i]);
  373. sym.ppuwrite(ppufile);
  374. end;
  375. { end of symbols }
  376. ppufile.writeentry(ibendsyms);
  377. end;
  378. procedure tstoredsymtable.buildderef;
  379. var
  380. i : longint;
  381. def : tstoreddef;
  382. sym : tstoredsym;
  383. begin
  384. { interface definitions }
  385. for i:=0 to DefList.Count-1 do
  386. begin
  387. def:=tstoreddef(DefList[i]);
  388. def.buildderef;
  389. end;
  390. { interface symbols }
  391. for i:=0 to SymList.Count-1 do
  392. begin
  393. sym:=tstoredsym(SymList[i]);
  394. sym.buildderef;
  395. end;
  396. end;
  397. procedure tstoredsymtable.buildderefimpl;
  398. var
  399. i : longint;
  400. def : tstoreddef;
  401. begin
  402. { implementation definitions }
  403. for i:=0 to DefList.Count-1 do
  404. begin
  405. def:=tstoreddef(DefList[i]);
  406. def.buildderefimpl;
  407. end;
  408. end;
  409. procedure tstoredsymtable.deref;
  410. var
  411. i : longint;
  412. def : tstoreddef;
  413. sym : tstoredsym;
  414. begin
  415. { first deref the interface ttype symbols. This is needs
  416. to be done before the interface defs are derefed, because
  417. the interface defs can contain references to the type symbols
  418. which then already need to contain a resolved typedef field (PFV) }
  419. for i:=0 to SymList.Count-1 do
  420. begin
  421. sym:=tstoredsym(SymList[i]);
  422. if sym.typ=typesym then
  423. sym.deref;
  424. end;
  425. { interface definitions }
  426. for i:=0 to DefList.Count-1 do
  427. begin
  428. def:=tstoreddef(DefList[i]);
  429. def.deref;
  430. end;
  431. { interface symbols }
  432. for i:=0 to SymList.Count-1 do
  433. begin
  434. sym:=tstoredsym(SymList[i]);
  435. if sym.typ<>typesym then
  436. sym.deref;
  437. end;
  438. end;
  439. procedure tstoredsymtable.derefimpl;
  440. var
  441. i : longint;
  442. def : tstoreddef;
  443. begin
  444. { implementation definitions }
  445. for i:=0 to DefList.Count-1 do
  446. begin
  447. def:=tstoreddef(DefList[i]);
  448. def.derefimpl;
  449. end;
  450. end;
  451. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  452. var
  453. hsym : tsym;
  454. begin
  455. hsym:=tsym(FindWithHash(hashedid));
  456. if assigned(hsym) then
  457. DuplicateSym(hashedid,sym,hsym);
  458. result:=assigned(hsym);
  459. end;
  460. {**************************************
  461. Callbacks
  462. **************************************}
  463. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  464. begin
  465. if tsym(sym).typ=procsym then
  466. tprocsym(sym).check_forward
  467. { check also object method table }
  468. { we needn't to test the def list }
  469. { because each object has to have a type sym,
  470. only test objects declarations, not type renamings }
  471. else
  472. if (tsym(sym).typ=typesym) and
  473. assigned(ttypesym(sym).typedef) and
  474. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  475. (ttypesym(sym).typedef.typ=objectdef) then
  476. tobjectdef(ttypesym(sym).typedef).check_forwards;
  477. end;
  478. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  479. begin
  480. if (tsym(sym).typ=labelsym) and
  481. not(tlabelsym(sym).defined) then
  482. begin
  483. if tlabelsym(sym).used then
  484. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  485. else
  486. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  487. end;
  488. end;
  489. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  490. begin
  491. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  492. ((tsym(sym).owner.symtabletype in
  493. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  494. begin
  495. { unused symbol should be reported only if no }
  496. { error is reported }
  497. { if the symbol is in a register it is used }
  498. { also don't count the value parameters which have local copies }
  499. { also don't claim for high param of open parameters (PM) }
  500. if (Errorcount<>0) or
  501. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  502. exit;
  503. if (tstoredsym(sym).refs=0) then
  504. begin
  505. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  506. begin
  507. { don't warn about the result of constructors }
  508. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  509. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  510. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  511. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  512. end
  513. else if (tsym(sym).owner.symtabletype=parasymtable) then
  514. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  515. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  516. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  517. else
  518. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  519. end
  520. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  521. begin
  522. if (tsym(sym).owner.symtabletype=parasymtable) then
  523. begin
  524. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  525. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  526. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  527. end
  528. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  529. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).prettyname)
  530. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  531. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  532. end
  533. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  534. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  535. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  536. end
  537. else if ((tsym(sym).owner.symtabletype in
  538. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  539. begin
  540. if (Errorcount<>0) or
  541. (sp_internal in tsym(sym).symoptions) then
  542. exit;
  543. { do not claim for inherited private fields !! }
  544. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  545. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  546. { units references are problematic }
  547. else
  548. begin
  549. if (tsym(sym).refs=0) and
  550. not(tsym(sym).typ in [enumsym,unitsym]) and
  551. not(is_funcret_sym(tsym(sym))) and
  552. { don't complain about compiler generated syms for specializations, see also #13405 }
  553. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  554. (pos('$',ttypesym(sym).Realname)<>0)) and
  555. (
  556. (tsym(sym).typ<>procsym) or
  557. ((tsym(sym).owner.symtabletype=staticsymtable) and
  558. not current_module.is_unit)
  559. ) and
  560. { don't complain about alias for hidden _cmd parameter to
  561. obj-c methods }
  562. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  563. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  564. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  565. end;
  566. end;
  567. end;
  568. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  569. begin
  570. if tsym(sym).visibility=vis_private then
  571. varsymbolused(sym,arg);
  572. end;
  573. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  574. begin
  575. {
  576. Don't test simple object aliases PM
  577. }
  578. if (tsym(sym).typ=typesym) and
  579. (ttypesym(sym).typedef.typ=objectdef) and
  580. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  581. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  582. end;
  583. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  584. begin
  585. if (tsym(sym).typ=propertysym) and
  586. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  587. ppointer(arg)^:=sym;
  588. end;
  589. {***********************************************
  590. Process all entries
  591. ***********************************************}
  592. procedure Tstoredsymtable.reset_all_defs;
  593. var
  594. i : longint;
  595. def : tstoreddef;
  596. begin
  597. for i:=0 to DefList.Count-1 do
  598. begin
  599. def:=tstoreddef(DefList[i]);
  600. def.reset;
  601. end;
  602. end;
  603. { checks, if all procsyms and methods are defined }
  604. procedure tstoredsymtable.check_forwards;
  605. begin
  606. SymList.ForEachCall(@check_forward,nil);
  607. end;
  608. procedure tstoredsymtable.checklabels;
  609. begin
  610. SymList.ForEachCall(@labeldefined,nil);
  611. end;
  612. procedure tstoredsymtable.allsymbolsused;
  613. begin
  614. SymList.ForEachCall(@varsymbolused,nil);
  615. end;
  616. procedure tstoredsymtable.allprivatesused;
  617. begin
  618. SymList.ForEachCall(@objectprivatesymbolused,nil);
  619. end;
  620. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  621. begin
  622. if b_needs_init_final then
  623. exit;
  624. case tsym(sym).typ of
  625. fieldvarsym,
  626. staticvarsym,
  627. localvarsym,
  628. paravarsym :
  629. begin
  630. if not(is_class(tabstractvarsym(sym).vardef)) and
  631. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  632. b_needs_init_final:=true;
  633. end;
  634. end;
  635. end;
  636. { returns true, if p contains data which needs init/final code }
  637. function tstoredsymtable.needs_init_final : boolean;
  638. begin
  639. b_needs_init_final:=false;
  640. SymList.ForEachCall(@_needs_init_final,nil);
  641. needs_init_final:=b_needs_init_final;
  642. end;
  643. {****************************************************************************
  644. TAbstractRecordSymtable
  645. ****************************************************************************}
  646. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  647. begin
  648. inherited create(n);
  649. _datasize:=0;
  650. databitsize:=0;
  651. recordalignment:=1;
  652. usefieldalignment:=usealign;
  653. padalignment:=1;
  654. { recordalign C_alignment means C record packing, that starts
  655. with an alignment of 1 }
  656. case usealign of
  657. C_alignment,
  658. bit_alignment:
  659. fieldalignment:=1;
  660. mac68k_alignment:
  661. fieldalignment:=2;
  662. else
  663. fieldalignment:=usealign;
  664. end;
  665. end;
  666. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  667. begin
  668. inherited ppuload(ppufile);
  669. end;
  670. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  671. var
  672. oldtyp : byte;
  673. begin
  674. oldtyp:=ppufile.entrytyp;
  675. ppufile.entrytyp:=subentryid;
  676. inherited ppuwrite(ppufile);
  677. ppufile.entrytyp:=oldtyp;
  678. end;
  679. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  680. begin
  681. { optimal alignment of the record when declaring a variable of this }
  682. { type is independent of the packrecords setting }
  683. if (fieldoffs mod fieldalign) = 0 then
  684. result:=fieldalign
  685. else if (fieldalign >= 16) and
  686. ((fieldoffs mod 16) = 0) and
  687. ((fieldalign mod 16) = 0) then
  688. result:=16
  689. else if (fieldalign >= 8) and
  690. ((fieldoffs mod 8) = 0) and
  691. ((fieldalign mod 8) = 0) then
  692. result:=8
  693. else if (fieldalign >= 4) and
  694. ((fieldoffs mod 4) = 0) and
  695. ((fieldalign mod 4) = 0) then
  696. result:=4
  697. else if (fieldalign >= 2) and
  698. ((fieldoffs mod 2) = 0) and
  699. ((fieldalign mod 2) = 0) then
  700. result:=2
  701. else
  702. result:=1;
  703. end;
  704. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  705. var
  706. varalignrecord: shortint;
  707. begin
  708. case usefieldalignment of
  709. C_alignment:
  710. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  711. mac68k_alignment:
  712. varalignrecord:=2;
  713. else
  714. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  715. end;
  716. recordalignment:=max(recordalignment,varalignrecord);
  717. end;
  718. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  719. var
  720. l : aint;
  721. varalignfield,
  722. varalign : shortint;
  723. vardef : tdef;
  724. begin
  725. if (sym.owner<>self) then
  726. internalerror(200602031);
  727. if sym.fieldoffset<>-1 then
  728. internalerror(200602032);
  729. { set visibility for the symbol }
  730. sym.visibility:=vis;
  731. { this symbol can't be loaded to a register }
  732. sym.varregable:=vr_none;
  733. { Calculate field offset }
  734. l:=sym.getsize;
  735. vardef:=sym.vardef;
  736. varalign:=vardef.alignment;
  737. case usefieldalignment of
  738. bit_alignment:
  739. begin
  740. { bitpacking only happens for ordinals, the rest is aligned at }
  741. { 1 byte (compatible with GPC/GCC) }
  742. if is_ordinal(vardef) then
  743. begin
  744. sym.fieldoffset:=databitsize;
  745. l:=sym.getpackedbitsize;
  746. end
  747. else
  748. begin
  749. databitsize:=_datasize*8;
  750. sym.fieldoffset:=databitsize;
  751. if (l>high(aint) div 8) then
  752. Message(sym_e_segment_too_large);
  753. l:=l*8;
  754. end;
  755. if varalign=0 then
  756. varalign:=size_2_align(l);
  757. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  758. { bit packed records are limited to high(aint) bits }
  759. { instead of bytes to avoid double precision }
  760. { arithmetic in offset calculations }
  761. if int64(l)>high(aint)-sym.fieldoffset then
  762. begin
  763. Message(sym_e_segment_too_large);
  764. _datasize:=high(aint);
  765. databitsize:=high(aint);
  766. end
  767. else
  768. begin
  769. databitsize:=sym.fieldoffset+l;
  770. _datasize:=(databitsize+7) div 8;
  771. end;
  772. { rest is not applicable }
  773. exit;
  774. end;
  775. { Calc the alignment size for C style records }
  776. C_alignment:
  777. begin
  778. if (varalign>4) and
  779. ((varalign mod 4)<>0) and
  780. (vardef.typ=arraydef) then
  781. Message1(sym_w_wrong_C_pack,vardef.typename);
  782. if varalign=0 then
  783. varalign:=l;
  784. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  785. begin
  786. if (varalign>16) and (fieldalignment<32) then
  787. fieldalignment:=32
  788. else if (varalign>12) and (fieldalignment<16) then
  789. fieldalignment:=16
  790. { 12 is needed for long double }
  791. else if (varalign>8) and (fieldalignment<12) then
  792. fieldalignment:=12
  793. else if (varalign>4) and (fieldalignment<8) then
  794. fieldalignment:=8
  795. else if (varalign>2) and (fieldalignment<4) then
  796. fieldalignment:=4
  797. else if (varalign>1) and (fieldalignment<2) then
  798. fieldalignment:=2;
  799. end;
  800. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  801. end;
  802. mac68k_alignment:
  803. begin
  804. { mac68k alignment (C description):
  805. * char is aligned to 1 byte
  806. * everything else (except vector) is aligned to 2 bytes
  807. * vector is aligned to 16 bytes
  808. }
  809. if l>1 then
  810. fieldalignment:=2
  811. else
  812. fieldalignment:=1;
  813. varalign:=2;
  814. end;
  815. end;
  816. if varalign=0 then
  817. varalign:=size_2_align(l);
  818. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  819. sym.fieldoffset:=align(_datasize,varalignfield);
  820. if l>high(aint)-sym.fieldoffset then
  821. begin
  822. Message(sym_e_segment_too_large);
  823. _datasize:=high(aint);
  824. end
  825. else
  826. _datasize:=sym.fieldoffset+l;
  827. { Calc alignment needed for this record }
  828. alignrecord(sym.fieldoffset,varalign);
  829. end;
  830. procedure tabstractrecordsymtable.addalignmentpadding;
  831. begin
  832. { make the record size aligned correctly so it can be
  833. used as elements in an array. For C records we
  834. use the fieldalignment, because that is updated with the
  835. used alignment. }
  836. if (padalignment = 1) then
  837. case usefieldalignment of
  838. C_alignment:
  839. padalignment:=fieldalignment;
  840. { bitpacked }
  841. bit_alignment:
  842. padalignment:=1;
  843. { mac68k: always round to multiple of 2 }
  844. mac68k_alignment:
  845. padalignment:=2;
  846. { default/no packrecords specified }
  847. 0:
  848. padalignment:=recordalignment
  849. { specific packrecords setting -> use as upper limit }
  850. else
  851. padalignment:=min(recordalignment,usefieldalignment);
  852. end;
  853. _datasize:=align(_datasize,padalignment);
  854. end;
  855. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  856. begin
  857. { Enums must also be available outside the record scope,
  858. insert in the owner of this symtable }
  859. if def.typ=enumdef then
  860. defowner.owner.insertdef(def)
  861. else
  862. inherited insertdef(def);
  863. end;
  864. function tabstractrecordsymtable.is_packed: boolean;
  865. begin
  866. result:=usefieldalignment=bit_alignment;
  867. end;
  868. procedure tabstractrecordsymtable.setdatasize(val: aint);
  869. begin
  870. _datasize:=val;
  871. if (usefieldalignment=bit_alignment) then
  872. { can overflow in non bitpacked records }
  873. databitsize:=val*8;
  874. end;
  875. {****************************************************************************
  876. TRecordSymtable
  877. ****************************************************************************}
  878. constructor trecordsymtable.create(usealign:shortint);
  879. begin
  880. inherited create('',usealign);
  881. symtabletype:=recordsymtable;
  882. end;
  883. { this procedure is reserved for inserting case variant into
  884. a record symtable }
  885. { the offset is the location of the start of the variant
  886. and datasize and dataalignment corresponds to
  887. the complete size (see code in pdecl unit) PM }
  888. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  889. var
  890. sym : tsym;
  891. def : tdef;
  892. i : integer;
  893. varalignrecord,varalign,
  894. storesize,storealign : aint;
  895. bitsize: aint;
  896. begin
  897. storesize:=_datasize;
  898. storealign:=fieldalignment;
  899. _datasize:=offset;
  900. if (usefieldalignment=bit_alignment) then
  901. databitsize:=offset*8;
  902. { We move the ownership of the defs and symbols to the new recordsymtable.
  903. The old unionsymtable keeps the references, but doesn't own the
  904. objects anymore }
  905. unionst.DefList.OwnsObjects:=false;
  906. unionst.SymList.OwnsObjects:=false;
  907. { copy symbols }
  908. for i:=0 to unionst.SymList.Count-1 do
  909. begin
  910. sym:=TSym(unionst.SymList[i]);
  911. if sym.typ<>fieldvarsym then
  912. internalerror(200601272);
  913. { add to this record symtable }
  914. // unionst.SymList.List.List^[i].Data:=nil;
  915. sym.ChangeOwner(self);
  916. varalign:=tfieldvarsym(sym).vardef.alignment;
  917. if varalign=0 then
  918. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  919. { retrieve size }
  920. if (usefieldalignment=bit_alignment) then
  921. begin
  922. { bit packed records are limited to high(aint) bits }
  923. { instead of bytes to avoid double precision }
  924. { arithmetic in offset calculations }
  925. if is_ordinal(tfieldvarsym(sym).vardef) then
  926. bitsize:=tfieldvarsym(sym).getpackedbitsize
  927. else
  928. begin
  929. bitsize:=tfieldvarsym(sym).getsize;
  930. if (bitsize>high(aint) div 8) then
  931. Message(sym_e_segment_too_large);
  932. bitsize:=bitsize*8;
  933. end;
  934. if bitsize>high(aint)-databitsize then
  935. begin
  936. Message(sym_e_segment_too_large);
  937. _datasize:=high(aint);
  938. databitsize:=high(aint);
  939. end
  940. else
  941. begin
  942. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  943. _datasize:=(databitsize+7) div 8;
  944. end;
  945. tfieldvarsym(sym).fieldoffset:=databitsize;
  946. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  947. end
  948. else
  949. begin
  950. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  951. begin
  952. Message(sym_e_segment_too_large);
  953. _datasize:=high(aint);
  954. end
  955. else
  956. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  957. { update address }
  958. tfieldvarsym(sym).fieldoffset:=_datasize;
  959. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  960. end;
  961. { update alignment of this record }
  962. if (usefieldalignment<>C_alignment) and
  963. (usefieldalignment<>mac68k_alignment) then
  964. recordalignment:=max(recordalignment,varalignrecord);
  965. end;
  966. { update alignment for C records }
  967. if (usefieldalignment=C_alignment) and
  968. (usefieldalignment<>mac68k_alignment) then
  969. recordalignment:=max(recordalignment,unionst.recordalignment);
  970. { Register defs in the new record symtable }
  971. for i:=0 to unionst.DefList.Count-1 do
  972. begin
  973. def:=TDef(unionst.DefList[i]);
  974. def.ChangeOwner(self);
  975. end;
  976. _datasize:=storesize;
  977. fieldalignment:=storealign;
  978. end;
  979. {****************************************************************************
  980. TObjectSymtable
  981. ****************************************************************************}
  982. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  983. begin
  984. inherited create(n,usealign);
  985. symtabletype:=ObjectSymtable;
  986. defowner:=adefowner;
  987. end;
  988. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  989. var
  990. hsym : tsym;
  991. begin
  992. result:=false;
  993. if not assigned(defowner) then
  994. internalerror(200602061);
  995. { procsym and propertysym have special code
  996. to override values in inherited classes. For other
  997. symbols check for duplicates }
  998. if not(sym.typ in [procsym,propertysym]) then
  999. begin
  1000. { but private ids can be reused }
  1001. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  1002. if assigned(hsym) and
  1003. (
  1004. (
  1005. not(m_delphi in current_settings.modeswitches) and
  1006. is_visible_for_object(hsym,tobjectdef(defowner))
  1007. ) or
  1008. (
  1009. { In Delphi, you can repeat members of a parent class. You can't }
  1010. { do this for objects however, and you (obviouly) can't }
  1011. { declare two fields with the same name in a single class }
  1012. (m_delphi in current_settings.modeswitches) and
  1013. (
  1014. is_object(tdef(defowner)) or
  1015. (hsym.owner = self)
  1016. )
  1017. )
  1018. ) then
  1019. begin
  1020. DuplicateSym(hashedid,sym,hsym);
  1021. result:=true;
  1022. end;
  1023. end
  1024. else
  1025. begin
  1026. if not(m_duplicate_names in current_settings.modeswitches) then
  1027. result:=inherited checkduplicate(hashedid,sym);
  1028. end;
  1029. end;
  1030. {****************************************************************************
  1031. TAbstractLocalSymtable
  1032. ****************************************************************************}
  1033. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1034. var
  1035. oldtyp : byte;
  1036. begin
  1037. oldtyp:=ppufile.entrytyp;
  1038. ppufile.entrytyp:=subentryid;
  1039. { write definitions }
  1040. writedefs(ppufile);
  1041. { write symbols }
  1042. writesyms(ppufile);
  1043. ppufile.entrytyp:=oldtyp;
  1044. end;
  1045. function tabstractlocalsymtable.count_locals:longint;
  1046. var
  1047. i : longint;
  1048. sym : tsym;
  1049. begin
  1050. result:=0;
  1051. for i:=0 to SymList.Count-1 do
  1052. begin
  1053. sym:=tsym(SymList[i]);
  1054. { Count only varsyms, but ignore the funcretsym }
  1055. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1056. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1057. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1058. (tstoredsym(sym).refs>0)) then
  1059. inc(result);
  1060. end;
  1061. end;
  1062. {****************************************************************************
  1063. TLocalSymtable
  1064. ****************************************************************************}
  1065. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1066. begin
  1067. inherited create('');
  1068. defowner:=adefowner;
  1069. symtabletype:=localsymtable;
  1070. symtablelevel:=level;
  1071. end;
  1072. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1073. var
  1074. hsym : tsym;
  1075. begin
  1076. if not assigned(defowner) or
  1077. (defowner.typ<>procdef) then
  1078. internalerror(200602042);
  1079. result:=false;
  1080. hsym:=tsym(FindWithHash(hashedid));
  1081. if assigned(hsym) then
  1082. begin
  1083. { a local and the function can have the same
  1084. name in TP and Delphi, but RESULT not }
  1085. if (m_duplicate_names in current_settings.modeswitches) and
  1086. (hsym.typ in [absolutevarsym,localvarsym]) and
  1087. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1088. not((m_result in current_settings.modeswitches) and
  1089. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1090. HideSym(hsym)
  1091. else
  1092. DuplicateSym(hashedid,sym,hsym);
  1093. result:=true;
  1094. exit;
  1095. end;
  1096. { check also parasymtable, this needs to be done here becuase
  1097. of the special situation with the funcret sym that needs to be
  1098. hidden for tp and delphi modes }
  1099. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1100. if assigned(hsym) then
  1101. begin
  1102. { a local and the function can have the same
  1103. name in TP and Delphi, but RESULT not }
  1104. if (m_duplicate_names in current_settings.modeswitches) and
  1105. (sym.typ in [absolutevarsym,localvarsym]) and
  1106. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1107. not((m_result in current_settings.modeswitches) and
  1108. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1109. Hidesym(sym)
  1110. else
  1111. DuplicateSym(hashedid,sym,hsym);
  1112. result:=true;
  1113. exit;
  1114. end;
  1115. { check ObjectSymtable, skip this for funcret sym because
  1116. that will always be positive because it has the same name
  1117. as the procsym }
  1118. if not is_funcret_sym(sym) and
  1119. (defowner.typ=procdef) and
  1120. assigned(tprocdef(defowner)._class) and
  1121. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1122. (
  1123. not(m_delphi in current_settings.modeswitches) or
  1124. is_object(tprocdef(defowner)._class)
  1125. ) then
  1126. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1127. end;
  1128. {****************************************************************************
  1129. TParaSymtable
  1130. ****************************************************************************}
  1131. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1132. begin
  1133. inherited create('');
  1134. defowner:=adefowner;
  1135. symtabletype:=parasymtable;
  1136. symtablelevel:=level;
  1137. end;
  1138. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1139. begin
  1140. result:=inherited checkduplicate(hashedid,sym);
  1141. if result then
  1142. exit;
  1143. if not(m_duplicate_names in current_settings.modeswitches) and
  1144. (defowner.typ=procdef) and
  1145. assigned(tprocdef(defowner)._class) and
  1146. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1147. (
  1148. not(m_delphi in current_settings.modeswitches) or
  1149. is_object(tprocdef(defowner)._class)
  1150. ) then
  1151. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1152. end;
  1153. {****************************************************************************
  1154. TAbstractUniTSymtable
  1155. ****************************************************************************}
  1156. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1157. begin
  1158. inherited create(n);
  1159. moduleid:=id;
  1160. end;
  1161. function tabstractuniTSymtable.iscurrentunit:boolean;
  1162. begin
  1163. result:=assigned(current_module) and
  1164. (
  1165. (current_module.globalsymtable=self) or
  1166. (current_module.localsymtable=self)
  1167. );
  1168. end;
  1169. {****************************************************************************
  1170. TStaticSymtable
  1171. ****************************************************************************}
  1172. constructor tstaticsymtable.create(const n : string;id:word);
  1173. begin
  1174. inherited create(n,id);
  1175. symtabletype:=staticsymtable;
  1176. symtablelevel:=main_program_level;
  1177. end;
  1178. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1179. begin
  1180. inherited ppuload(ppufile);
  1181. { now we can deref the syms and defs }
  1182. deref;
  1183. end;
  1184. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1185. begin
  1186. inherited ppuwrite(ppufile);
  1187. end;
  1188. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1189. var
  1190. hsym : tsym;
  1191. begin
  1192. result:=false;
  1193. hsym:=tsym(FindWithHash(hashedid));
  1194. if assigned(hsym) then
  1195. begin
  1196. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1197. unit, the unit can then not be accessed anymore using
  1198. <unit>.<id>, so we can hide the symbol }
  1199. if (m_delphi in current_settings.modeswitches) and
  1200. (hsym.typ=symconst.unitsym) then
  1201. HideSym(hsym)
  1202. else
  1203. DuplicateSym(hashedid,sym,hsym);
  1204. result:=true;
  1205. exit;
  1206. end;
  1207. if (current_module.localsymtable=self) and
  1208. assigned(current_module.globalsymtable) then
  1209. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1210. end;
  1211. {****************************************************************************
  1212. TGlobalSymtable
  1213. ****************************************************************************}
  1214. constructor tglobalsymtable.create(const n : string;id:word);
  1215. begin
  1216. inherited create(n,id);
  1217. symtabletype:=globalsymtable;
  1218. symtablelevel:=main_program_level;
  1219. end;
  1220. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1221. begin
  1222. inherited ppuload(ppufile);
  1223. { now we can deref the syms and defs }
  1224. deref;
  1225. end;
  1226. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1227. begin
  1228. { write the symtable entries }
  1229. inherited ppuwrite(ppufile);
  1230. end;
  1231. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1232. var
  1233. hsym : tsym;
  1234. begin
  1235. result:=false;
  1236. hsym:=tsym(FindWithHash(hashedid));
  1237. if assigned(hsym) then
  1238. begin
  1239. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1240. unit, the unit can then not be accessed anymore using
  1241. <unit>.<id>, so we can hide the symbol }
  1242. if (m_delphi in current_settings.modeswitches) and
  1243. (hsym.typ=symconst.unitsym) then
  1244. HideSym(hsym)
  1245. else
  1246. DuplicateSym(hashedid,sym,hsym);
  1247. result:=true;
  1248. exit;
  1249. end;
  1250. end;
  1251. {****************************************************************************
  1252. TWITHSYMTABLE
  1253. ****************************************************************************}
  1254. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1255. begin
  1256. inherited create('');
  1257. symtabletype:=withsymtable;
  1258. withrefnode:=refnode;
  1259. { Replace SymList with the passed symlist }
  1260. SymList.free;
  1261. SymList:=ASymList;
  1262. defowner:=aowner;
  1263. end;
  1264. destructor twithsymtable.destroy;
  1265. begin
  1266. withrefnode.free;
  1267. { Disable SymList because we don't Own it }
  1268. SymList:=nil;
  1269. inherited destroy;
  1270. end;
  1271. procedure twithsymtable.clear;
  1272. begin
  1273. { remove no entry from a withsymtable as it is only a pointer to the
  1274. recorddef or objectdef symtable }
  1275. end;
  1276. procedure twithsymtable.insertdef(def:TDefEntry);
  1277. begin
  1278. { Definitions can't be registered in the withsymtable
  1279. because the withsymtable is removed after the with block.
  1280. We can't easily solve it here because the next symtable in the
  1281. stack is not known. }
  1282. internalerror(200602046);
  1283. end;
  1284. {****************************************************************************
  1285. TSTT_ExceptionSymtable
  1286. ****************************************************************************}
  1287. constructor tstt_excepTSymtable.create;
  1288. begin
  1289. inherited create('');
  1290. symtabletype:=stt_excepTSymtable;
  1291. end;
  1292. {****************************************************************************
  1293. TMacroSymtable
  1294. ****************************************************************************}
  1295. constructor tmacrosymtable.create(exported: boolean);
  1296. begin
  1297. inherited create('');
  1298. if exported then
  1299. symtabletype:=exportedmacrosymtable
  1300. else
  1301. symtabletype:=localmacrosymtable;
  1302. symtablelevel:=main_program_level;
  1303. end;
  1304. {*****************************************************************************
  1305. Helper Routines
  1306. *****************************************************************************}
  1307. function FullTypeName(def,otherdef:tdef):string;
  1308. var
  1309. s1,s2 : string;
  1310. begin
  1311. s1:=def.typename;
  1312. { When the names are the same try to include the unit name }
  1313. if assigned(otherdef) and
  1314. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1315. begin
  1316. s2:=otherdef.typename;
  1317. if upper(s1)=upper(s2) then
  1318. s1:=def.owner.realname^+'.'+s1;
  1319. end;
  1320. FullTypeName:=s1;
  1321. end;
  1322. procedure incompatibletypes(def1,def2:tdef);
  1323. begin
  1324. { When there is an errordef there is already an error message show }
  1325. if (def2.typ=errordef) or
  1326. (def1.typ=errordef) then
  1327. exit;
  1328. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1329. end;
  1330. procedure hidesym(sym:TSymEntry);
  1331. begin
  1332. sym.realname:='$hidden'+sym.realname;
  1333. tsym(sym).visibility:=vis_hidden;
  1334. end;
  1335. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1336. var
  1337. st : TSymtable;
  1338. begin
  1339. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1340. { Write hint where the original symbol was found }
  1341. st:=finduniTSymtable(origsym.owner);
  1342. with tsym(origsym).fileinfo do
  1343. begin
  1344. if assigned(st) and
  1345. (st.symtabletype=globalsymtable) and
  1346. st.iscurrentunit then
  1347. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1348. else if assigned(st.name) then
  1349. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1350. end;
  1351. { Rename duplicate sym to an unreachable name, but it can be
  1352. inserted in the symtable without errors }
  1353. inc(dupnr);
  1354. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1355. if assigned(dupsym) then
  1356. include(tsym(dupsym).symoptions,sp_implicitrename);
  1357. end;
  1358. {*****************************************************************************
  1359. Search
  1360. *****************************************************************************}
  1361. procedure addsymref(sym:tsym);
  1362. begin
  1363. { symbol uses count }
  1364. sym.IncRefCount;
  1365. { unit uses count }
  1366. if assigned(current_module) and
  1367. (sym.owner.symtabletype=globalsymtable) then
  1368. begin
  1369. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1370. internalerror(200501152);
  1371. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1372. end;
  1373. end;
  1374. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1375. var
  1376. symownerdef : tobjectdef;
  1377. begin
  1378. result:=false;
  1379. { Get objdectdef owner of the symtable for the is_related checks }
  1380. if not assigned(symst) or
  1381. (symst.symtabletype<>objectsymtable) then
  1382. internalerror(200810285);
  1383. symownerdef:=tobjectdef(symst.defowner);
  1384. case symvisibility of
  1385. vis_private :
  1386. begin
  1387. { private symbols are allowed when we are in the same
  1388. module as they are defined }
  1389. result:=(
  1390. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1391. (symownerdef.owner.iscurrentunit)
  1392. ) or
  1393. ( // the case of specialize inside the generic declaration
  1394. (symownerdef.owner.symtabletype = objectsymtable) and
  1395. assigned(current_objectdef) and
  1396. (
  1397. (current_objectdef=symownerdef) or
  1398. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1399. )
  1400. );
  1401. end;
  1402. vis_strictprivate :
  1403. begin
  1404. result:=assigned(current_objectdef) and
  1405. (current_objectdef=symownerdef);
  1406. end;
  1407. vis_strictprotected :
  1408. begin
  1409. result:=assigned(current_objectdef) and
  1410. current_objectdef.is_related(symownerdef);
  1411. end;
  1412. vis_protected :
  1413. begin
  1414. { protected symbols are visible in the module that defines them and
  1415. also visible to related objects. The related object must be defined
  1416. in the current module }
  1417. result:=(
  1418. (
  1419. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1420. (symownerdef.owner.iscurrentunit)
  1421. ) or
  1422. (
  1423. assigned(contextobjdef) and
  1424. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1425. (contextobjdef.owner.iscurrentunit) and
  1426. contextobjdef.is_related(symownerdef)
  1427. ) or
  1428. ( // the case of specialize inside the generic declaration
  1429. (symownerdef.owner.symtabletype = objectsymtable) and
  1430. assigned(current_objectdef) and
  1431. (
  1432. (current_objectdef=symownerdef) or
  1433. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1434. )
  1435. )
  1436. );
  1437. end;
  1438. vis_public,
  1439. vis_published :
  1440. result:=true;
  1441. end;
  1442. end;
  1443. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1444. begin
  1445. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1446. end;
  1447. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1448. var
  1449. i : longint;
  1450. pd : tprocdef;
  1451. begin
  1452. if sym.typ=procsym then
  1453. begin
  1454. { A procsym is visible, when there is at least one of the procdefs visible }
  1455. result:=false;
  1456. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1457. begin
  1458. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1459. if (pd.owner=sym.owner) and
  1460. is_visible_for_object(pd,contextobjdef) then
  1461. begin
  1462. result:=true;
  1463. exit;
  1464. end;
  1465. end;
  1466. end
  1467. else
  1468. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1469. end;
  1470. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1471. var
  1472. hashedid : THashedIDString;
  1473. contextobjdef : tobjectdef;
  1474. stackitem : psymtablestackitem;
  1475. begin
  1476. result:=false;
  1477. hashedid.id:=s;
  1478. stackitem:=symtablestack.stack;
  1479. while assigned(stackitem) do
  1480. begin
  1481. srsymtable:=stackitem^.symtable;
  1482. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1483. if assigned(srsym) then
  1484. begin
  1485. { use the class from withsymtable only when it is
  1486. defined in this unit }
  1487. if (srsymtable.symtabletype=withsymtable) and
  1488. assigned(srsymtable.defowner) and
  1489. (srsymtable.defowner.typ=objectdef) and
  1490. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1491. (srsymtable.defowner.owner.iscurrentunit) then
  1492. contextobjdef:=tobjectdef(srsymtable.defowner)
  1493. else
  1494. contextobjdef:=current_objectdef;
  1495. if (srsym.owner.symtabletype<>objectsymtable) or
  1496. is_visible_for_object(srsym,contextobjdef) then
  1497. begin
  1498. { we need to know if a procedure references symbols
  1499. in the static symtable, because then it can't be
  1500. inlined from outside this unit }
  1501. if assigned(current_procinfo) and
  1502. (srsym.owner.symtabletype=staticsymtable) then
  1503. include(current_procinfo.flags,pi_uses_static_symtable);
  1504. addsymref(srsym);
  1505. result:=true;
  1506. exit;
  1507. end;
  1508. end;
  1509. { also search for class helpers }
  1510. if (srsymtable.symtabletype=objectsymtable) and
  1511. is_objcclass(tdef(srsymtable.defowner)) then
  1512. begin
  1513. if search_class_helper(tobjectdef(srsymtable.defowner),s,srsym,srsymtable) then
  1514. begin
  1515. result:=true;
  1516. exit;
  1517. end;
  1518. end;
  1519. stackitem:=stackitem^.next;
  1520. end;
  1521. srsym:=nil;
  1522. srsymtable:=nil;
  1523. end;
  1524. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1525. var
  1526. hashedid : THashedIDString;
  1527. stackitem : psymtablestackitem;
  1528. begin
  1529. result:=false;
  1530. hashedid.id:=s;
  1531. stackitem:=symtablestack.stack;
  1532. while assigned(stackitem) do
  1533. begin
  1534. {
  1535. It is not possible to have type symbols in:
  1536. records
  1537. objects
  1538. parameters
  1539. Exception are generic definitions and specializations
  1540. that have the parameterized types inserted in the symtable.
  1541. }
  1542. srsymtable:=stackitem^.symtable;
  1543. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1544. (assigned(srsymtable.defowner) and
  1545. (
  1546. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1547. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1548. ) then
  1549. begin
  1550. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1551. if assigned(srsym) and
  1552. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1553. (
  1554. (srsym.owner.symtabletype<>objectsymtable) or
  1555. is_visible_for_object(srsym,current_objectdef)
  1556. ) then
  1557. begin
  1558. { we need to know if a procedure references symbols
  1559. in the static symtable, because then it can't be
  1560. inlined from outside this unit }
  1561. if assigned(current_procinfo) and
  1562. (srsym.owner.symtabletype=staticsymtable) then
  1563. include(current_procinfo.flags,pi_uses_static_symtable);
  1564. addsymref(srsym);
  1565. result:=true;
  1566. exit;
  1567. end;
  1568. end;
  1569. stackitem:=stackitem^.next;
  1570. end;
  1571. result:=false;
  1572. srsym:=nil;
  1573. srsymtable:=nil;
  1574. end;
  1575. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1576. var
  1577. pmod : tmodule;
  1578. begin
  1579. pmod:=tmodule(pm);
  1580. result:=false;
  1581. if assigned(pmod.globalsymtable) then
  1582. begin
  1583. srsym:=tsym(pmod.globalsymtable.Find(s));
  1584. if assigned(srsym) then
  1585. begin
  1586. srsymtable:=pmod.globalsymtable;
  1587. addsymref(srsym);
  1588. result:=true;
  1589. exit;
  1590. end;
  1591. end;
  1592. { If the module is the current unit we also need
  1593. to search the local symtable }
  1594. if (pmod=current_module) and
  1595. assigned(pmod.localsymtable) then
  1596. begin
  1597. srsym:=tsym(pmod.localsymtable.Find(s));
  1598. if assigned(srsym) then
  1599. begin
  1600. srsymtable:=pmod.localsymtable;
  1601. addsymref(srsym);
  1602. result:=true;
  1603. exit;
  1604. end;
  1605. end;
  1606. srsym:=nil;
  1607. srsymtable:=nil;
  1608. end;
  1609. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1610. var
  1611. stackitem : psymtablestackitem;
  1612. begin
  1613. result:=false;
  1614. stackitem:=symtablestack.stack;
  1615. while assigned(stackitem) do
  1616. begin
  1617. srsymtable:=stackitem^.symtable;
  1618. if (srsymtable.symtabletype=globalsymtable) and
  1619. (srsymtable.name^=unitname) then
  1620. begin
  1621. srsym:=tsym(srsymtable.find(symname));
  1622. if not assigned(srsym) then
  1623. break;
  1624. result:=true;
  1625. exit;
  1626. end;
  1627. stackitem:=stackitem^.next;
  1628. end;
  1629. { If the module is the current unit we also need
  1630. to search the local symtable }
  1631. if (current_module.globalsymtable=srsymtable) and
  1632. assigned(current_module.localsymtable) then
  1633. begin
  1634. srsymtable:=current_module.localsymtable;
  1635. srsym:=tsym(srsymtable.find(symname));
  1636. if assigned(srsym) then
  1637. begin
  1638. result:=true;
  1639. exit;
  1640. end;
  1641. end;
  1642. end;
  1643. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1644. var
  1645. hashedid : THashedIDString;
  1646. orgclass : tobjectdef;
  1647. begin
  1648. orgclass:=classh;
  1649. { The contextclassh is used for visibility. The classh must be equal to
  1650. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1651. parent. }
  1652. if assigned(classh) and
  1653. not contextclassh.is_related(classh) then
  1654. internalerror(200811161);
  1655. result:=false;
  1656. hashedid.id:=s;
  1657. while assigned(classh) do
  1658. begin
  1659. srsymtable:=classh.symtable;
  1660. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1661. if assigned(srsym) and
  1662. is_visible_for_object(srsym,contextclassh) then
  1663. begin
  1664. addsymref(srsym);
  1665. result:=true;
  1666. exit;
  1667. end;
  1668. classh:=classh.childof;
  1669. end;
  1670. if is_objcclass(orgclass) then
  1671. result:=search_class_helper(orgclass,s,srsym,srsymtable)
  1672. else
  1673. begin
  1674. srsym:=nil;
  1675. srsymtable:=nil;
  1676. end;
  1677. end;
  1678. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1679. var
  1680. def : tdef;
  1681. i : longint;
  1682. begin
  1683. result:=false;
  1684. def:=nil;
  1685. while assigned(classh) do
  1686. begin
  1687. for i:=0 to classh.symtable.DefList.Count-1 do
  1688. begin
  1689. def:=tstoreddef(classh.symtable.DefList[i]);
  1690. { Find also all hidden private methods to
  1691. be compatible with delphi, see tw6203 (PFV) }
  1692. if (def.typ=procdef) and
  1693. (po_msgint in tprocdef(def).procoptions) and
  1694. (tprocdef(def).messageinf.i=msgid) then
  1695. begin
  1696. srdef:=def;
  1697. srsym:=tprocdef(def).procsym;
  1698. srsymtable:=classh.symtable;
  1699. addsymref(srsym);
  1700. result:=true;
  1701. exit;
  1702. end;
  1703. end;
  1704. classh:=classh.childof;
  1705. end;
  1706. srdef:=nil;
  1707. srsym:=nil;
  1708. srsymtable:=nil;
  1709. end;
  1710. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1711. var
  1712. def : tdef;
  1713. i : longint;
  1714. begin
  1715. result:=false;
  1716. def:=nil;
  1717. while assigned(classh) do
  1718. begin
  1719. for i:=0 to classh.symtable.DefList.Count-1 do
  1720. begin
  1721. def:=tstoreddef(classh.symtable.DefList[i]);
  1722. { Find also all hidden private methods to
  1723. be compatible with delphi, see tw6203 (PFV) }
  1724. if (def.typ=procdef) and
  1725. (po_msgstr in tprocdef(def).procoptions) and
  1726. (tprocdef(def).messageinf.str^=s) then
  1727. begin
  1728. srsym:=tprocdef(def).procsym;
  1729. srsymtable:=classh.symtable;
  1730. addsymref(srsym);
  1731. result:=true;
  1732. exit;
  1733. end;
  1734. end;
  1735. classh:=classh.childof;
  1736. end;
  1737. srsym:=nil;
  1738. srsymtable:=nil;
  1739. end;
  1740. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1741. var
  1742. sym : Tprocsym;
  1743. hashedid : THashedIDString;
  1744. curreq,
  1745. besteq : tequaltype;
  1746. currpd,
  1747. bestpd : tprocdef;
  1748. stackitem : psymtablestackitem;
  1749. begin
  1750. hashedid.id:='assign';
  1751. besteq:=te_incompatible;
  1752. bestpd:=nil;
  1753. stackitem:=symtablestack.stack;
  1754. while assigned(stackitem) do
  1755. begin
  1756. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1757. if sym<>nil then
  1758. begin
  1759. if sym.typ<>procsym then
  1760. internalerror(200402031);
  1761. { if the source type is an alias then this is only the second choice,
  1762. if you mess with this code, check tw4093 }
  1763. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1764. if curreq>besteq then
  1765. begin
  1766. besteq:=curreq;
  1767. bestpd:=currpd;
  1768. if (besteq=te_exact) then
  1769. break;
  1770. end;
  1771. end;
  1772. stackitem:=stackitem^.next;
  1773. end;
  1774. result:=bestpd;
  1775. end;
  1776. function search_enumerator_operator(type_def:Tdef): Tprocdef;
  1777. var
  1778. sym : Tprocsym;
  1779. hashedid : THashedIDString;
  1780. curreq,
  1781. besteq : tequaltype;
  1782. currpd,
  1783. bestpd : tprocdef;
  1784. stackitem : psymtablestackitem;
  1785. begin
  1786. hashedid.id:='enumerator';
  1787. besteq:=te_incompatible;
  1788. bestpd:=nil;
  1789. stackitem:=symtablestack.stack;
  1790. while assigned(stackitem) do
  1791. begin
  1792. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1793. if sym<>nil then
  1794. begin
  1795. if sym.typ<>procsym then
  1796. internalerror(200910241);
  1797. { if the source type is an alias then this is only the second choice,
  1798. if you mess with this code, check tw4093 }
  1799. currpd:=sym.find_procdef_enumerator_operator(type_def,curreq);
  1800. if curreq>besteq then
  1801. begin
  1802. besteq:=curreq;
  1803. bestpd:=currpd;
  1804. if (besteq=te_exact) then
  1805. break;
  1806. end;
  1807. end;
  1808. stackitem:=stackitem^.next;
  1809. end;
  1810. result:=bestpd;
  1811. end;
  1812. function search_system_type(const s: TIDString): ttypesym;
  1813. var
  1814. sym : tsym;
  1815. begin
  1816. sym:=tsym(systemunit.Find(s));
  1817. if not assigned(sym) or
  1818. (sym.typ<>typesym) then
  1819. cgmessage1(cg_f_unknown_system_type,s);
  1820. result:=ttypesym(sym);
  1821. end;
  1822. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1823. var
  1824. srsymtable: tsymtable;
  1825. sym: tsym;
  1826. begin
  1827. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1828. (sym.typ=typesym) then
  1829. begin
  1830. result:=ttypesym(sym);
  1831. exit;
  1832. end
  1833. else
  1834. begin
  1835. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1836. result:=nil;
  1837. end;
  1838. end;
  1839. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1840. var
  1841. hashedid : THashedIDString;
  1842. stackitem : psymtablestackitem;
  1843. i : longint;
  1844. defowner : tobjectdef;
  1845. begin
  1846. hashedid.id:=class_helper_prefix+s;
  1847. stackitem:=symtablestack.stack;
  1848. while assigned(stackitem) do
  1849. begin
  1850. srsymtable:=stackitem^.symtable;
  1851. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1852. if assigned(srsym) then
  1853. begin
  1854. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1855. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1856. (srsym.typ<>procsym) then
  1857. internalerror(2009111505);
  1858. { check whether this procsym includes a helper for this particular class }
  1859. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1860. begin
  1861. { does pd inherit from (or is the same as) the class
  1862. that this method's category extended?
  1863. Warning: this list contains both category and objcclass methods
  1864. (for id.randommethod), so only check category methods here
  1865. }
  1866. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  1867. if (oo_is_classhelper in defowner.objectoptions) and
  1868. pd.is_related(defowner.childof) then
  1869. begin
  1870. { we need to know if a procedure references symbols
  1871. in the static symtable, because then it can't be
  1872. inlined from outside this unit }
  1873. if assigned(current_procinfo) and
  1874. (srsym.owner.symtabletype=staticsymtable) then
  1875. include(current_procinfo.flags,pi_uses_static_symtable);
  1876. { no need to keep looking. There might be other
  1877. categories that extend this, a parent or child
  1878. class with a method with the same name (either
  1879. overriding this one, or overridden by this one),
  1880. but that doesn't matter as far as the basic
  1881. procsym is concerned.
  1882. }
  1883. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1884. srsymtable:=srsym.owner;
  1885. addsymref(srsym);
  1886. result:=true;
  1887. exit;
  1888. end;
  1889. end;
  1890. end;
  1891. stackitem:=stackitem^.next;
  1892. end;
  1893. srsym:=nil;
  1894. srsymtable:=nil;
  1895. result:=false;
  1896. end;
  1897. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1898. var
  1899. hashedid : THashedIDString;
  1900. stackitem : psymtablestackitem;
  1901. i : longint;
  1902. begin
  1903. hashedid.id:=class_helper_prefix+s;
  1904. stackitem:=symtablestack.stack;
  1905. while assigned(stackitem) do
  1906. begin
  1907. srsymtable:=stackitem^.symtable;
  1908. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1909. if assigned(srsym) then
  1910. begin
  1911. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1912. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1913. (srsym.typ<>procsym) then
  1914. internalerror(2009112005);
  1915. { check whether this procsym includes a helper for this particular class }
  1916. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1917. begin
  1918. { we need to know if a procedure references symbols
  1919. in the static symtable, because then it can't be
  1920. inlined from outside this unit }
  1921. if assigned(current_procinfo) and
  1922. (srsym.owner.symtabletype=staticsymtable) then
  1923. include(current_procinfo.flags,pi_uses_static_symtable);
  1924. { no need to keep looking. There might be other
  1925. methods with the same name, but that doesn't matter
  1926. as far as the basic procsym is concerned.
  1927. }
  1928. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1929. { We need the symtable in which the classhelper-like sym
  1930. is located, not the objectdef. The reason is that the
  1931. callnode will climb the symtablestack until it encounters
  1932. this symtable to start looking for overloads (and it won't
  1933. find the objectsymtable in which this method sym is
  1934. located
  1935. srsymtable:=srsym.owner;
  1936. }
  1937. addsymref(srsym);
  1938. result:=true;
  1939. exit;
  1940. end;
  1941. end;
  1942. stackitem:=stackitem^.next;
  1943. end;
  1944. srsym:=nil;
  1945. srsymtable:=nil;
  1946. result:=false;
  1947. end;
  1948. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1949. { searches n in symtable of pd and all anchestors }
  1950. var
  1951. hashedid : THashedIDString;
  1952. srsym : tsym;
  1953. orgpd : tobjectdef;
  1954. srsymtable : tsymtable;
  1955. begin
  1956. hashedid.id:=s;
  1957. orgpd:=pd;
  1958. while assigned(pd) do
  1959. begin
  1960. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1961. if assigned(srsym) then
  1962. begin
  1963. search_class_member:=srsym;
  1964. exit;
  1965. end;
  1966. pd:=pd.childof;
  1967. end;
  1968. { not found, now look for class helpers }
  1969. if is_objcclass(pd) then
  1970. search_class_helper(orgpd,s,result,srsymtable)
  1971. else
  1972. result:=nil;
  1973. end;
  1974. function search_macro(const s : string):tsym;
  1975. var
  1976. stackitem : psymtablestackitem;
  1977. hashedid : THashedIDString;
  1978. srsym : tsym;
  1979. begin
  1980. hashedid.id:=s;
  1981. { First search the localmacrosymtable before searching the
  1982. global macrosymtables from the units }
  1983. if assigned(current_module) then
  1984. begin
  1985. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1986. if assigned(srsym) then
  1987. begin
  1988. result:= srsym;
  1989. exit;
  1990. end;
  1991. end;
  1992. stackitem:=macrosymtablestack.stack;
  1993. while assigned(stackitem) do
  1994. begin
  1995. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1996. if assigned(srsym) then
  1997. begin
  1998. result:= srsym;
  1999. exit;
  2000. end;
  2001. stackitem:=stackitem^.next;
  2002. end;
  2003. result:= nil;
  2004. end;
  2005. function defined_macro(const s : string):boolean;
  2006. var
  2007. mac: tmacro;
  2008. begin
  2009. mac:=tmacro(search_macro(s));
  2010. if assigned(mac) then
  2011. begin
  2012. mac.is_used:=true;
  2013. defined_macro:=mac.defined;
  2014. end
  2015. else
  2016. defined_macro:=false;
  2017. end;
  2018. {****************************************************************************
  2019. Object Helpers
  2020. ****************************************************************************}
  2021. function search_default_property(pd : tobjectdef) : tpropertysym;
  2022. { returns the default property of a class, searches also anchestors }
  2023. var
  2024. _defaultprop : tpropertysym;
  2025. begin
  2026. _defaultprop:=nil;
  2027. while assigned(pd) do
  2028. begin
  2029. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2030. if assigned(_defaultprop) then
  2031. break;
  2032. pd:=pd.childof;
  2033. end;
  2034. search_default_property:=_defaultprop;
  2035. end;
  2036. {****************************************************************************
  2037. Macro Helpers
  2038. ****************************************************************************}
  2039. procedure def_system_macro(const name : string);
  2040. var
  2041. mac : tmacro;
  2042. s: string;
  2043. begin
  2044. if name = '' then
  2045. internalerror(2004121202);
  2046. s:= upper(name);
  2047. mac:=tmacro(search_macro(s));
  2048. if not assigned(mac) then
  2049. begin
  2050. mac:=tmacro.create(s);
  2051. if assigned(current_module) then
  2052. current_module.localmacrosymtable.insert(mac)
  2053. else
  2054. initialmacrosymtable.insert(mac);
  2055. end;
  2056. if not mac.defined then
  2057. Message1(parser_c_macro_defined,mac.name);
  2058. mac.defined:=true;
  2059. end;
  2060. procedure set_system_macro(const name, value : string);
  2061. var
  2062. mac : tmacro;
  2063. s: string;
  2064. begin
  2065. if name = '' then
  2066. internalerror(2004121203);
  2067. s:= upper(name);
  2068. mac:=tmacro(search_macro(s));
  2069. if not assigned(mac) then
  2070. begin
  2071. mac:=tmacro.create(s);
  2072. if assigned(current_module) then
  2073. current_module.localmacrosymtable.insert(mac)
  2074. else
  2075. initialmacrosymtable.insert(mac);
  2076. end
  2077. else
  2078. begin
  2079. mac.is_compiler_var:=false;
  2080. if assigned(mac.buftext) then
  2081. freemem(mac.buftext,mac.buflen);
  2082. end;
  2083. Message2(parser_c_macro_set_to,mac.name,value);
  2084. mac.buflen:=length(value);
  2085. getmem(mac.buftext,mac.buflen);
  2086. move(value[1],mac.buftext^,mac.buflen);
  2087. mac.defined:=true;
  2088. end;
  2089. procedure set_system_compvar(const name, value : string);
  2090. var
  2091. mac : tmacro;
  2092. s: string;
  2093. begin
  2094. if name = '' then
  2095. internalerror(2004121204);
  2096. s:= upper(name);
  2097. mac:=tmacro(search_macro(s));
  2098. if not assigned(mac) then
  2099. begin
  2100. mac:=tmacro.create(s);
  2101. mac.is_compiler_var:=true;
  2102. if assigned(current_module) then
  2103. current_module.localmacrosymtable.insert(mac)
  2104. else
  2105. initialmacrosymtable.insert(mac);
  2106. end
  2107. else
  2108. begin
  2109. mac.is_compiler_var:=true;
  2110. if assigned(mac.buftext) then
  2111. freemem(mac.buftext,mac.buflen);
  2112. end;
  2113. Message2(parser_c_macro_set_to,mac.name,value);
  2114. mac.buflen:=length(value);
  2115. getmem(mac.buftext,mac.buflen);
  2116. move(value[1],mac.buftext^,mac.buflen);
  2117. mac.defined:=true;
  2118. end;
  2119. procedure undef_system_macro(const name : string);
  2120. var
  2121. mac : tmacro;
  2122. s: string;
  2123. begin
  2124. if name = '' then
  2125. internalerror(2004121205);
  2126. s:= upper(name);
  2127. mac:=tmacro(search_macro(s));
  2128. if not assigned(mac) then
  2129. {If not found, then it's already undefined.}
  2130. else
  2131. begin
  2132. if mac.defined then
  2133. Message1(parser_c_macro_undefined,mac.name);
  2134. mac.defined:=false;
  2135. mac.is_compiler_var:=false;
  2136. { delete old definition }
  2137. if assigned(mac.buftext) then
  2138. begin
  2139. freemem(mac.buftext,mac.buflen);
  2140. mac.buftext:=nil;
  2141. end;
  2142. end;
  2143. end;
  2144. {$ifdef UNITALIASES}
  2145. {****************************************************************************
  2146. TUNIT_ALIAS
  2147. ****************************************************************************}
  2148. constructor tunit_alias.create(const n:string);
  2149. var
  2150. i : longint;
  2151. begin
  2152. i:=pos('=',n);
  2153. if i=0 then
  2154. fail;
  2155. inherited createname(Copy(n,1,i-1));
  2156. newname:=stringdup(Copy(n,i+1,255));
  2157. end;
  2158. destructor tunit_alias.destroy;
  2159. begin
  2160. stringdispose(newname);
  2161. inherited destroy;
  2162. end;
  2163. procedure addunitalias(const n:string);
  2164. begin
  2165. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2166. end;
  2167. function getunitalias(const n:string):string;
  2168. var
  2169. p : punit_alias;
  2170. begin
  2171. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2172. if assigned(p) then
  2173. getunitalias:=punit_alias(p).newname^
  2174. else
  2175. getunitalias:=n;
  2176. end;
  2177. {$endif UNITALIASES}
  2178. {****************************************************************************
  2179. Init/Done Symtable
  2180. ****************************************************************************}
  2181. procedure InitSymtable;
  2182. begin
  2183. { Reset symbolstack }
  2184. symtablestack:=nil;
  2185. systemunit:=nil;
  2186. { create error syms and def }
  2187. generrorsym:=terrorsym.create;
  2188. generrordef:=terrordef.create;
  2189. { macros }
  2190. initialmacrosymtable:=tmacrosymtable.create(false);
  2191. macrosymtablestack:=TSymtablestack.create;
  2192. macrosymtablestack.push(initialmacrosymtable);
  2193. {$ifdef UNITALIASES}
  2194. { unit aliases }
  2195. unitaliases:=TFPHashObjectList.create;
  2196. {$endif}
  2197. { set some global vars to nil, might be important for the ide }
  2198. class_tobject:=nil;
  2199. interface_iunknown:=nil;
  2200. rec_tguid:=nil;
  2201. dupnr:=0;
  2202. end;
  2203. procedure DoneSymtable;
  2204. begin
  2205. generrorsym.owner:=nil;
  2206. generrorsym.free;
  2207. generrordef.owner:=nil;
  2208. generrordef.free;
  2209. initialmacrosymtable.free;
  2210. macrosymtablestack.free;
  2211. {$ifdef UNITALIASES}
  2212. unitaliases.free;
  2213. {$endif}
  2214. end;
  2215. end.