symtable.pas 77 KB

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