symtable.pas 75 KB

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