symtable.pas 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  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 unchain_overloads(sym:TObject;arg:pointer);
  46. procedure loaddefs(ppufile:tcompilerppufile);
  47. procedure loadsyms(ppufile:tcompilerppufile);
  48. procedure writedefs(ppufile:tcompilerppufile);
  49. procedure writesyms(ppufile:tcompilerppufile);
  50. public
  51. { load/write }
  52. procedure ppuload(ppufile:tcompilerppufile);virtual;
  53. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  54. procedure buildderef;virtual;
  55. procedure buildderefimpl;virtual;
  56. procedure deref;virtual;
  57. procedure derefimpl;virtual;
  58. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  59. procedure reset_all_defs;virtual;
  60. procedure allsymbolsused;
  61. procedure allprivatesused;
  62. procedure check_forwards;
  63. procedure checklabels;
  64. function needs_init_final : boolean;
  65. procedure unchain_overloaded;
  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);
  79. procedure insertfield(sym:tfieldvarsym);
  80. procedure addalignmentpadding;
  81. procedure insertdef(def:TDefEntry);override;
  82. function is_packed: boolean;
  83. protected
  84. procedure setdatasize(val: aint);
  85. _datasize : aint;
  86. { size in bits of the data in case of bitpacked record. Only important during construction, }
  87. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  88. databitsize : aint;
  89. { bitpacked? -> all fieldvarsym offsets are in bits instead of bytes }
  90. public
  91. property datasize : aint read _datasize write setdatasize;
  92. end;
  93. trecordsymtable = class(tabstractrecordsymtable)
  94. public
  95. constructor create(usealign:shortint);
  96. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  97. end;
  98. tObjectSymtable = class(tabstractrecordsymtable)
  99. public
  100. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  101. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  102. end;
  103. { tabstractlocalsymtable }
  104. tabstractlocalsymtable = class(tstoredsymtable)
  105. public
  106. procedure ppuwrite(ppufile:tcompilerppufile);override;
  107. function count_locals:longint;
  108. end;
  109. tlocalsymtable = class(tabstractlocalsymtable)
  110. public
  111. constructor create(adefowner:tdef;level:byte);
  112. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  113. end;
  114. tparasymtable = class(tabstractlocalsymtable)
  115. public
  116. constructor create(adefowner:tdef;level:byte);
  117. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  118. end;
  119. tabstractuniTSymtable = class(tstoredsymtable)
  120. public
  121. constructor create(const n : string;id:word);
  122. function iscurrentunit:boolean;override;
  123. end;
  124. tglobalsymtable = class(tabstractuniTSymtable)
  125. public
  126. unittypecount : word;
  127. constructor create(const n : string;id:word);
  128. procedure ppuload(ppufile:tcompilerppufile);override;
  129. procedure ppuwrite(ppufile:tcompilerppufile);override;
  130. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  131. end;
  132. tstaticsymtable = class(tabstractuniTSymtable)
  133. public
  134. constructor create(const n : string;id:word);
  135. procedure ppuload(ppufile:tcompilerppufile);override;
  136. procedure ppuwrite(ppufile:tcompilerppufile);override;
  137. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  138. end;
  139. twithsymtable = class(TSymtable)
  140. withrefnode : tobject; { tnode }
  141. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  142. destructor destroy;override;
  143. procedure clear;override;
  144. procedure insertdef(def:TDefEntry);override;
  145. end;
  146. tstt_excepTSymtable = class(TSymtable)
  147. public
  148. constructor create;
  149. end;
  150. tmacrosymtable = class(tstoredsymtable)
  151. public
  152. constructor create(exported: boolean);
  153. end;
  154. var
  155. systemunit : tglobalsymtable; { pointer to the system unit }
  156. {****************************************************************************
  157. Functions
  158. ****************************************************************************}
  159. {*** Misc ***}
  160. function FullTypeName(def,otherdef:tdef):string;
  161. procedure incompatibletypes(def1,def2:tdef);
  162. procedure hidesym(sym:TSymEntry);
  163. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  164. {*** Search ***}
  165. procedure addsymref(sym:tsym);
  166. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  167. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  168. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  169. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  170. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  171. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  172. function search_system_type(const s: TIDString): ttypesym;
  173. function search_class_member(pd : tobjectdef;const s : string):tsym;
  174. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  175. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  176. {and returns it if found. Returns nil otherwise.}
  177. function search_macro(const s : string):tsym;
  178. { Additionally to searching for a macro, also checks whether it's still }
  179. { actually defined (could be disable using "undef") }
  180. function defined_macro(const s : string):boolean;
  181. {*** Object Helpers ***}
  182. procedure search_class_overloads(aprocsym : tprocsym);
  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.ppuload(ppufile:tcompilerppufile);
  242. begin
  243. { load definitions }
  244. loaddefs(ppufile);
  245. { load symbols }
  246. loadsyms(ppufile);
  247. end;
  248. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  249. begin
  250. { write definitions }
  251. writedefs(ppufile);
  252. { write symbols }
  253. writesyms(ppufile);
  254. end;
  255. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  256. var
  257. def : tdef;
  258. b : byte;
  259. begin
  260. { load start of definition section, which holds the amount of defs }
  261. if ppufile.readentry<>ibstartdefs then
  262. Message(unit_f_ppu_read_error);
  263. { read definitions }
  264. repeat
  265. b:=ppufile.readentry;
  266. case b of
  267. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  268. ibarraydef : def:=tarraydef.ppuload(ppufile);
  269. iborddef : def:=torddef.ppuload(ppufile);
  270. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  271. ibprocdef : def:=tprocdef.ppuload(ppufile);
  272. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  273. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  274. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  275. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  276. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  277. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  278. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  279. ibenumdef : def:=tenumdef.ppuload(ppufile);
  280. ibsetdef : def:=tsetdef.ppuload(ppufile);
  281. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  282. ibfiledef : def:=tfiledef.ppuload(ppufile);
  283. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  284. ibformaldef : def:=tformaldef.ppuload(ppufile);
  285. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  286. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  287. ibenddefs : break;
  288. ibend : Message(unit_f_ppu_read_error);
  289. else
  290. Message1(unit_f_ppu_invalid_entry,tostr(b));
  291. end;
  292. InsertDef(def);
  293. until false;
  294. end;
  295. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  296. var
  297. b : byte;
  298. sym : tsym;
  299. begin
  300. { load start of definition section, which holds the amount of defs }
  301. if ppufile.readentry<>ibstartsyms then
  302. Message(unit_f_ppu_read_error);
  303. { now read the symbols }
  304. repeat
  305. b:=ppufile.readentry;
  306. case b of
  307. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  308. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  309. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  310. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  311. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  312. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  313. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  314. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  315. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  316. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  317. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  318. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  319. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  320. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  321. ibendsyms : break;
  322. ibend : Message(unit_f_ppu_read_error);
  323. else
  324. Message1(unit_f_ppu_invalid_entry,tostr(b));
  325. end;
  326. Insert(sym,false);
  327. until false;
  328. end;
  329. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  330. var
  331. i : longint;
  332. def : tstoreddef;
  333. begin
  334. { each definition get a number, write then the amount of defs to the
  335. ibstartdef entry }
  336. ppufile.putlongint(DefList.count);
  337. ppufile.writeentry(ibstartdefs);
  338. { now write the definition }
  339. for i:=0 to DefList.Count-1 do
  340. begin
  341. def:=tstoreddef(DefList[i]);
  342. def.ppuwrite(ppufile);
  343. end;
  344. { write end of definitions }
  345. ppufile.writeentry(ibenddefs);
  346. end;
  347. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  348. var
  349. i : longint;
  350. sym : Tstoredsym;
  351. begin
  352. { each definition get a number, write then the amount of syms and the
  353. datasize to the ibsymdef entry }
  354. ppufile.putlongint(SymList.count);
  355. ppufile.writeentry(ibstartsyms);
  356. { foreach is used to write all symbols }
  357. for i:=0 to SymList.Count-1 do
  358. begin
  359. sym:=tstoredsym(SymList[i]);
  360. sym.ppuwrite(ppufile);
  361. end;
  362. { end of symbols }
  363. ppufile.writeentry(ibendsyms);
  364. end;
  365. procedure tstoredsymtable.buildderef;
  366. var
  367. i : longint;
  368. def : tstoreddef;
  369. sym : tstoredsym;
  370. begin
  371. { interface definitions }
  372. for i:=0 to DefList.Count-1 do
  373. begin
  374. def:=tstoreddef(DefList[i]);
  375. def.buildderef;
  376. end;
  377. { interface symbols }
  378. for i:=0 to SymList.Count-1 do
  379. begin
  380. sym:=tstoredsym(SymList[i]);
  381. sym.buildderef;
  382. end;
  383. end;
  384. procedure tstoredsymtable.buildderefimpl;
  385. var
  386. i : longint;
  387. def : tstoreddef;
  388. begin
  389. { implementation definitions }
  390. for i:=0 to DefList.Count-1 do
  391. begin
  392. def:=tstoreddef(DefList[i]);
  393. def.buildderefimpl;
  394. end;
  395. end;
  396. procedure tstoredsymtable.deref;
  397. var
  398. i : longint;
  399. def : tstoreddef;
  400. sym : tstoredsym;
  401. begin
  402. { first deref the interface ttype symbols. This is needs
  403. to be done before the interface defs are derefed, because
  404. the interface defs can contain references to the type symbols
  405. which then already need to contain a resolved typedef field (PFV) }
  406. for i:=0 to SymList.Count-1 do
  407. begin
  408. sym:=tstoredsym(SymList[i]);
  409. if sym.typ=typesym then
  410. sym.deref;
  411. end;
  412. { interface definitions }
  413. for i:=0 to DefList.Count-1 do
  414. begin
  415. def:=tstoreddef(DefList[i]);
  416. def.deref;
  417. end;
  418. { interface symbols }
  419. for i:=0 to SymList.Count-1 do
  420. begin
  421. sym:=tstoredsym(SymList[i]);
  422. if sym.typ<>typesym then
  423. sym.deref;
  424. end;
  425. end;
  426. procedure tstoredsymtable.derefimpl;
  427. var
  428. i : longint;
  429. def : tstoreddef;
  430. begin
  431. { implementation definitions }
  432. for i:=0 to DefList.Count-1 do
  433. begin
  434. def:=tstoreddef(DefList[i]);
  435. def.derefimpl;
  436. end;
  437. end;
  438. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  439. var
  440. hsym : tsym;
  441. begin
  442. hsym:=tsym(FindWithHash(hashedid));
  443. if assigned(hsym) then
  444. DuplicateSym(hashedid,sym,hsym);
  445. result:=assigned(hsym);
  446. end;
  447. {**************************************
  448. Callbacks
  449. **************************************}
  450. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  451. begin
  452. if tsym(sym).typ=procsym then
  453. tprocsym(sym).check_forward
  454. { check also object method table }
  455. { we needn't to test the def list }
  456. { because each object has to have a type sym,
  457. only test objects declarations, not type renamings }
  458. else
  459. if (tsym(sym).typ=typesym) and
  460. assigned(ttypesym(sym).typedef) and
  461. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  462. (ttypesym(sym).typedef.typ=objectdef) then
  463. tobjectdef(ttypesym(sym).typedef).check_forwards;
  464. end;
  465. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  466. begin
  467. if (tsym(sym).typ=labelsym) and
  468. not(tlabelsym(sym).defined) then
  469. begin
  470. if tlabelsym(sym).used then
  471. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  472. else
  473. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  474. end;
  475. end;
  476. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  477. begin
  478. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  479. ((tsym(sym).owner.symtabletype in
  480. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  481. begin
  482. { unused symbol should be reported only if no }
  483. { error is reported }
  484. { if the symbol is in a register it is used }
  485. { also don't count the value parameters which have local copies }
  486. { also don't claim for high param of open parameters (PM) }
  487. if (Errorcount<>0) or
  488. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  489. exit;
  490. if (tstoredsym(sym).refs=0) then
  491. begin
  492. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  493. begin
  494. { don't warn about the result of constructors }
  495. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  496. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  497. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  498. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  499. end
  500. else if (tsym(sym).owner.symtabletype=parasymtable) then
  501. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).realname)
  502. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  503. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  504. else
  505. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).realname);
  506. end
  507. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  508. begin
  509. if (tsym(sym).owner.symtabletype=parasymtable) then
  510. begin
  511. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  512. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  513. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).realname)
  514. end
  515. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  516. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).realname)
  517. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  518. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).realname);
  519. end
  520. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  521. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  522. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).realname)
  523. end
  524. else if ((tsym(sym).owner.symtabletype in
  525. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  526. begin
  527. if (Errorcount<>0) or
  528. (sp_internal in tsym(sym).symoptions) then
  529. exit;
  530. { do not claim for inherited private fields !! }
  531. if (Tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  532. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  533. { units references are problematic }
  534. else
  535. begin
  536. if (Tsym(sym).refs=0) and
  537. not(tsym(sym).typ in [enumsym,unitsym]) and
  538. not(is_funcret_sym(tsym(sym))) and
  539. (
  540. (tsym(sym).typ<>procsym) or
  541. ((tsym(sym).owner.symtabletype=staticsymtable) and
  542. not current_module.is_unit)
  543. ) then
  544. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).realname);
  545. end;
  546. end;
  547. end;
  548. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  549. begin
  550. if sp_private in tsym(sym).symoptions then
  551. varsymbolused(sym,arg);
  552. end;
  553. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  554. begin
  555. {
  556. Don't test simple object aliases PM
  557. }
  558. if (tsym(sym).typ=typesym) and
  559. (ttypesym(sym).typedef.typ=objectdef) and
  560. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  561. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  562. end;
  563. procedure tstoredsymtable.unchain_overloads(sym:TObject;arg:pointer);
  564. begin
  565. if tsym(sym).typ=procsym then
  566. tprocsym(sym).unchain_overload;
  567. end;
  568. {***********************************************
  569. Process all entries
  570. ***********************************************}
  571. procedure Tstoredsymtable.reset_all_defs;
  572. var
  573. i : longint;
  574. def : tstoreddef;
  575. begin
  576. for i:=0 to DefList.Count-1 do
  577. begin
  578. def:=tstoreddef(DefList[i]);
  579. def.reset;
  580. end;
  581. end;
  582. { checks, if all procsyms and methods are defined }
  583. procedure tstoredsymtable.check_forwards;
  584. begin
  585. SymList.ForEachCall(@check_forward,nil);
  586. end;
  587. procedure tstoredsymtable.checklabels;
  588. begin
  589. SymList.ForEachCall(@labeldefined,nil);
  590. end;
  591. procedure tstoredsymtable.allsymbolsused;
  592. begin
  593. SymList.ForEachCall(@varsymbolused,nil);
  594. end;
  595. procedure tstoredsymtable.allprivatesused;
  596. begin
  597. SymList.ForEachCall(@objectprivatesymbolused,nil);
  598. end;
  599. procedure tstoredsymtable.unchain_overloaded;
  600. begin
  601. SymList.ForEachCall(@unchain_overloads,nil);
  602. end;
  603. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  604. begin
  605. if b_needs_init_final then
  606. exit;
  607. case tsym(sym).typ of
  608. fieldvarsym,
  609. staticvarsym,
  610. localvarsym,
  611. paravarsym :
  612. begin
  613. if not(is_class(tabstractvarsym(sym).vardef)) and
  614. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  615. b_needs_init_final:=true;
  616. end;
  617. end;
  618. end;
  619. { returns true, if p contains data which needs init/final code }
  620. function tstoredsymtable.needs_init_final : boolean;
  621. begin
  622. b_needs_init_final:=false;
  623. SymList.ForEachCall(@_needs_init_final,nil);
  624. needs_init_final:=b_needs_init_final;
  625. end;
  626. {****************************************************************************
  627. TAbstractRecordSymtable
  628. ****************************************************************************}
  629. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  630. begin
  631. inherited create(n);
  632. _datasize:=0;
  633. databitsize:=0;
  634. recordalignment:=1;
  635. usefieldalignment:=usealign;
  636. padalignment:=1;
  637. { recordalign C_alignment means C record packing, that starts
  638. with an alignment of 1 }
  639. case usealign of
  640. C_alignment,
  641. bit_alignment:
  642. fieldalignment:=1
  643. else
  644. fieldalignment:=usealign;
  645. end;
  646. end;
  647. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  648. begin
  649. inherited ppuload(ppufile);
  650. end;
  651. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  652. var
  653. oldtyp : byte;
  654. begin
  655. oldtyp:=ppufile.entrytyp;
  656. ppufile.entrytyp:=subentryid;
  657. inherited ppuwrite(ppufile);
  658. ppufile.entrytyp:=oldtyp;
  659. end;
  660. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  661. begin
  662. { optimal alignment of the record when declaring a variable of this }
  663. { type is independent of the packrecords setting }
  664. if (fieldoffs mod fieldalign) = 0 then
  665. result:=fieldalign
  666. else if (fieldalign >= 16) and
  667. ((fieldoffs mod 16) = 0) and
  668. ((fieldalign mod 16) = 0) then
  669. result:=16
  670. else if (fieldalign >= 8) and
  671. ((fieldoffs mod 8) = 0) and
  672. ((fieldalign mod 8) = 0) then
  673. result:=8
  674. else if (fieldalign >= 4) and
  675. ((fieldoffs mod 4) = 0) and
  676. ((fieldalign mod 4) = 0) then
  677. result:=4
  678. else if (fieldalign >= 2) and
  679. ((fieldoffs mod 2) = 0) and
  680. ((fieldalign mod 2) = 0) then
  681. result:=2
  682. else
  683. result:=1;
  684. end;
  685. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  686. var
  687. varalignrecord: shortint;
  688. begin
  689. if (usefieldalignment=C_alignment) then
  690. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  691. else
  692. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  693. recordalignment:=max(recordalignment,varalignrecord);
  694. end;
  695. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym);
  696. var
  697. l : aint;
  698. varalignfield,
  699. varalign : shortint;
  700. vardef : tdef;
  701. begin
  702. if (sym.owner<>self) then
  703. internalerror(200602031);
  704. if sym.fieldoffset<>-1 then
  705. internalerror(200602032);
  706. { this symbol can't be loaded to a register }
  707. sym.varregable:=vr_none;
  708. { Calculate field offset }
  709. l:=sym.getsize;
  710. vardef:=sym.vardef;
  711. varalign:=vardef.alignment;
  712. if (usefieldalignment=bit_alignment) then
  713. begin
  714. { bitpacking only happens for ordinals, the rest is aligned at }
  715. { 1 byte (compatible with GPC/GCC) }
  716. if is_ordinal(vardef) then
  717. begin
  718. sym.fieldoffset:=databitsize;
  719. l:=sym.getpackedbitsize;
  720. end
  721. else
  722. begin
  723. databitsize:=_datasize*8;
  724. sym.fieldoffset:=databitsize;
  725. if (l>high(aint) div 8) then
  726. Message(sym_e_segment_too_large);
  727. l:=l*8;
  728. end;
  729. if varalign=0 then
  730. varalign:=size_2_align(l);
  731. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  732. { bit packed records are limited to high(aint) bits }
  733. { instead of bytes to avoid double precision }
  734. { arithmetic in offset calculations }
  735. if int64(l)>high(aint)-sym.fieldoffset then
  736. begin
  737. Message(sym_e_segment_too_large);
  738. _datasize:=high(aint);
  739. databitsize:=high(aint);
  740. end
  741. else
  742. begin
  743. databitsize:=sym.fieldoffset+l;
  744. _datasize:=(databitsize+7) div 8;
  745. end;
  746. { rest is not applicable }
  747. exit;
  748. end;
  749. { Calc the alignment size for C style records }
  750. if (usefieldalignment=C_alignment) then
  751. begin
  752. if (varalign>4) and
  753. ((varalign mod 4)<>0) and
  754. (vardef.typ=arraydef) then
  755. Message1(sym_w_wrong_C_pack,vardef.typename);
  756. if varalign=0 then
  757. varalign:=l;
  758. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  759. begin
  760. if (varalign>16) and (fieldalignment<32) then
  761. fieldalignment:=32
  762. else if (varalign>12) and (fieldalignment<16) then
  763. fieldalignment:=16
  764. { 12 is needed for long double }
  765. else if (varalign>8) and (fieldalignment<12) then
  766. fieldalignment:=12
  767. else if (varalign>4) and (fieldalignment<8) then
  768. fieldalignment:=8
  769. else if (varalign>2) and (fieldalignment<4) then
  770. fieldalignment:=4
  771. else if (varalign>1) and (fieldalignment<2) then
  772. fieldalignment:=2;
  773. end;
  774. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  775. end;
  776. if varalign=0 then
  777. varalign:=size_2_align(l);
  778. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  779. sym.fieldoffset:=align(_datasize,varalignfield);
  780. if l>high(aint)-sym.fieldoffset then
  781. begin
  782. Message(sym_e_segment_too_large);
  783. _datasize:=high(aint);
  784. end
  785. else
  786. _datasize:=sym.fieldoffset+l;
  787. { Calc alignment needed for this record }
  788. alignrecord(sym.fieldoffset,varalign);
  789. end;
  790. procedure tabstractrecordsymtable.insertfield(sym:tfieldvarsym);
  791. begin
  792. insert(sym);
  793. addfield(sym);
  794. end;
  795. procedure tabstractrecordsymtable.addalignmentpadding;
  796. begin
  797. { make the record size aligned correctly so it can be
  798. used as elements in an array. For C records we
  799. use the fieldalignment, because that is updated with the
  800. used alignment. }
  801. if (padalignment = 1) then
  802. case usefieldalignment of
  803. C_alignment:
  804. padalignment:=fieldalignment;
  805. { bitpacked }
  806. bit_alignment:
  807. padalignment:=1;
  808. { default/no packrecords specified }
  809. 0:
  810. padalignment:=recordalignment
  811. { specific packrecords setting -> use as upper limit }
  812. else
  813. padalignment:=min(recordalignment,usefieldalignment);
  814. end;
  815. _datasize:=align(_datasize,padalignment);
  816. end;
  817. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  818. begin
  819. { Enums must also be available outside the record scope,
  820. insert in the owner of this symtable }
  821. if def.typ=enumdef then
  822. defowner.owner.insertdef(def)
  823. else
  824. inherited insertdef(def);
  825. end;
  826. function tabstractrecordsymtable.is_packed: boolean;
  827. begin
  828. result:=usefieldalignment=bit_alignment;
  829. end;
  830. procedure tabstractrecordsymtable.setdatasize(val: aint);
  831. begin
  832. _datasize:=val;
  833. if (usefieldalignment=bit_alignment) then
  834. { can overflow in non bitpacked records }
  835. databitsize:=val*8;
  836. end;
  837. {****************************************************************************
  838. TRecordSymtable
  839. ****************************************************************************}
  840. constructor trecordsymtable.create(usealign:shortint);
  841. begin
  842. inherited create('',usealign);
  843. symtabletype:=recordsymtable;
  844. end;
  845. { this procedure is reserved for inserting case variant into
  846. a record symtable }
  847. { the offset is the location of the start of the variant
  848. and datasize and dataalignment corresponds to
  849. the complete size (see code in pdecl unit) PM }
  850. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  851. var
  852. sym : tsym;
  853. def : tdef;
  854. i : integer;
  855. varalignrecord,varalign,
  856. storesize,storealign : aint;
  857. bitsize: aint;
  858. begin
  859. storesize:=_datasize;
  860. storealign:=fieldalignment;
  861. _datasize:=offset;
  862. if (usefieldalignment=bit_alignment) then
  863. databitsize:=offset*8;
  864. { We move the ownership of the defs and symbols to the new recordsymtable.
  865. The old unionsymtable keeps the references, but doesn't own the
  866. objects anymore }
  867. unionst.DefList.OwnsObjects:=false;
  868. unionst.SymList.OwnsObjects:=false;
  869. { copy symbols }
  870. for i:=0 to unionst.SymList.Count-1 do
  871. begin
  872. sym:=TSym(unionst.SymList[i]);
  873. if sym.typ<>fieldvarsym then
  874. internalerror(200601272);
  875. { add to this record symtable }
  876. // unionst.SymList.List.List^[i].Data:=nil;
  877. sym.ChangeOwner(self);
  878. varalign:=tfieldvarsym(sym).vardef.alignment;
  879. if varalign=0 then
  880. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  881. { retrieve size }
  882. if (usefieldalignment=bit_alignment) then
  883. begin
  884. { bit packed records are limited to high(aint) bits }
  885. { instead of bytes to avoid double precision }
  886. { arithmetic in offset calculations }
  887. if is_ordinal(tfieldvarsym(sym).vardef) then
  888. bitsize:=tfieldvarsym(sym).getpackedbitsize
  889. else
  890. begin
  891. bitsize:=tfieldvarsym(sym).getsize;
  892. if (bitsize>high(aint) div 8) then
  893. Message(sym_e_segment_too_large);
  894. bitsize:=bitsize*8;
  895. end;
  896. if bitsize>high(aint)-databitsize then
  897. begin
  898. Message(sym_e_segment_too_large);
  899. _datasize:=high(aint);
  900. databitsize:=high(aint);
  901. end
  902. else
  903. begin
  904. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  905. _datasize:=(databitsize+7) div 8;
  906. end;
  907. tfieldvarsym(sym).fieldoffset:=databitsize;
  908. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  909. end
  910. else
  911. begin
  912. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  913. begin
  914. Message(sym_e_segment_too_large);
  915. _datasize:=high(aint);
  916. end
  917. else
  918. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  919. { update address }
  920. tfieldvarsym(sym).fieldoffset:=_datasize;
  921. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  922. end;
  923. { update alignment of this record }
  924. if (usefieldalignment<>C_alignment) then
  925. recordalignment:=max(recordalignment,varalignrecord);
  926. end;
  927. { update alignment for C records }
  928. if (usefieldalignment=C_alignment) then
  929. recordalignment:=max(recordalignment,unionst.recordalignment);
  930. { Register defs in the new record symtable }
  931. for i:=0 to unionst.DefList.Count-1 do
  932. begin
  933. def:=TDef(unionst.DefList[i]);
  934. def.ChangeOwner(self);
  935. end;
  936. { add the types that may need to be forward-checked }
  937. forwardchecksyms.capacity:=forwardchecksyms.capacity+unionst.forwardchecksyms.count;
  938. for i:=0 to unionst.forwardchecksyms.count-1 do
  939. forwardchecksyms.add(tsym(unionst.forwardchecksyms[i]));
  940. unionst.forwardchecksyms.clear;
  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. (not(m_delphi in current_settings.modeswitches) and
  970. tsym(hsym).is_visible_for_object(tobjectdef(defowner),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. include(tsym(sym).symoptions,sp_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 searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1339. var
  1340. hashedid : THashedIDString;
  1341. topclass : tobjectdef;
  1342. context : tobjectdef;
  1343. stackitem : psymtablestackitem;
  1344. begin
  1345. result:=false;
  1346. hashedid.id:=s;
  1347. stackitem:=symtablestack.stack;
  1348. while assigned(stackitem) do
  1349. begin
  1350. srsymtable:=stackitem^.symtable;
  1351. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1352. if assigned(srsym) then
  1353. begin
  1354. topclass:=nil;
  1355. { use the class from withsymtable only when it is
  1356. defined in this unit }
  1357. if (srsymtable.symtabletype=withsymtable) and
  1358. assigned(srsymtable.defowner) and
  1359. (srsymtable.defowner.typ=objectdef) and
  1360. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1361. (srsymtable.defowner.owner.iscurrentunit) then
  1362. topclass:=tobjectdef(srsymtable.defowner)
  1363. else
  1364. begin
  1365. if assigned(current_procinfo) then
  1366. topclass:=current_procinfo.procdef._class;
  1367. end;
  1368. if assigned(current_procinfo) then
  1369. context:=current_procinfo.procdef._class
  1370. else
  1371. context:=nil;
  1372. if tsym(srsym).is_visible_for_object(topclass,context) then
  1373. begin
  1374. { we need to know if a procedure references symbols
  1375. in the static symtable, because then it can't be
  1376. inlined from outside this unit }
  1377. if assigned(current_procinfo) and
  1378. (srsym.owner.symtabletype=staticsymtable) then
  1379. include(current_procinfo.flags,pi_uses_static_symtable);
  1380. addsymref(srsym);
  1381. result:=true;
  1382. exit;
  1383. end;
  1384. end;
  1385. stackitem:=stackitem^.next;
  1386. end;
  1387. srsym:=nil;
  1388. srsymtable:=nil;
  1389. end;
  1390. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1391. var
  1392. hashedid : THashedIDString;
  1393. stackitem : psymtablestackitem;
  1394. begin
  1395. result:=false;
  1396. hashedid.id:=s;
  1397. stackitem:=symtablestack.stack;
  1398. while assigned(stackitem) do
  1399. begin
  1400. {
  1401. It is not possible to have type symbols in:
  1402. records
  1403. objects
  1404. parameters
  1405. Exception are generic definitions and specializations
  1406. that have the parameterized types inserted in the symtable.
  1407. }
  1408. srsymtable:=stackitem^.symtable;
  1409. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1410. (assigned(srsymtable.defowner) and
  1411. (
  1412. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1413. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1414. ) then
  1415. begin
  1416. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1417. if assigned(srsym) and
  1418. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1419. (not assigned(current_procinfo) or
  1420. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1421. begin
  1422. { we need to know if a procedure references symbols
  1423. in the static symtable, because then it can't be
  1424. inlined from outside this unit }
  1425. if assigned(current_procinfo) and
  1426. (srsym.owner.symtabletype=staticsymtable) then
  1427. include(current_procinfo.flags,pi_uses_static_symtable);
  1428. addsymref(srsym);
  1429. result:=true;
  1430. exit;
  1431. end;
  1432. end;
  1433. stackitem:=stackitem^.next;
  1434. end;
  1435. result:=false;
  1436. srsym:=nil;
  1437. srsymtable:=nil;
  1438. end;
  1439. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1440. var
  1441. pmod : tmodule;
  1442. begin
  1443. pmod:=tmodule(pm);
  1444. result:=false;
  1445. if assigned(pmod.globalsymtable) then
  1446. begin
  1447. srsym:=tsym(pmod.globalsymtable.Find(s));
  1448. if assigned(srsym) then
  1449. begin
  1450. srsymtable:=pmod.globalsymtable;
  1451. addsymref(srsym);
  1452. result:=true;
  1453. exit;
  1454. end;
  1455. end;
  1456. { If the module is the current unit we also need
  1457. to search the local symtable }
  1458. if (pmod=current_module) and
  1459. assigned(pmod.localsymtable) then
  1460. begin
  1461. srsym:=tsym(pmod.localsymtable.Find(s));
  1462. if assigned(srsym) then
  1463. begin
  1464. srsymtable:=pmod.localsymtable;
  1465. addsymref(srsym);
  1466. result:=true;
  1467. exit;
  1468. end;
  1469. end;
  1470. srsym:=nil;
  1471. srsymtable:=nil;
  1472. end;
  1473. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1474. var
  1475. hashedid : THashedIDString;
  1476. currentclassh : tobjectdef;
  1477. begin
  1478. result:=false;
  1479. hashedid.id:=s;
  1480. if assigned(current_procinfo) and assigned(current_procinfo.procdef) then
  1481. currentclassh:=current_procinfo.procdef._class
  1482. else
  1483. currentclassh:=nil;
  1484. while assigned(classh) do
  1485. begin
  1486. srsymtable:=classh.symtable;
  1487. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1488. if assigned(srsym) and
  1489. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1490. begin
  1491. addsymref(srsym);
  1492. result:=true;
  1493. exit;
  1494. end;
  1495. classh:=classh.childof;
  1496. end;
  1497. srsym:=nil;
  1498. srsymtable:=nil;
  1499. end;
  1500. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1501. var
  1502. def : tdef;
  1503. i : longint;
  1504. begin
  1505. result:=false;
  1506. def:=nil;
  1507. while assigned(classh) do
  1508. begin
  1509. for i:=0 to classh.symtable.DefList.Count-1 do
  1510. begin
  1511. def:=tstoreddef(classh.symtable.DefList[i]);
  1512. { Find also all hidden private methods to
  1513. be compatible with delphi, see tw6203 (PFV) }
  1514. if (def.typ=procdef) and
  1515. (po_msgint in tprocdef(def).procoptions) and
  1516. (tprocdef(def).messageinf.i=msgid) then
  1517. begin
  1518. srdef:=def;
  1519. srsym:=tprocdef(def).procsym;
  1520. srsymtable:=classh.symtable;
  1521. addsymref(srsym);
  1522. result:=true;
  1523. exit;
  1524. end;
  1525. end;
  1526. classh:=classh.childof;
  1527. end;
  1528. srdef:=nil;
  1529. srsym:=nil;
  1530. srsymtable:=nil;
  1531. end;
  1532. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1533. var
  1534. def : tdef;
  1535. i : longint;
  1536. begin
  1537. result:=false;
  1538. def:=nil;
  1539. while assigned(classh) do
  1540. begin
  1541. for i:=0 to classh.symtable.DefList.Count-1 do
  1542. begin
  1543. def:=tstoreddef(classh.symtable.DefList[i]);
  1544. { Find also all hidden private methods to
  1545. be compatible with delphi, see tw6203 (PFV) }
  1546. if (def.typ=procdef) and
  1547. (po_msgstr in tprocdef(def).procoptions) and
  1548. (tprocdef(def).messageinf.str^=s) then
  1549. begin
  1550. srsym:=tprocdef(def).procsym;
  1551. srsymtable:=classh.symtable;
  1552. addsymref(srsym);
  1553. result:=true;
  1554. exit;
  1555. end;
  1556. end;
  1557. classh:=classh.childof;
  1558. end;
  1559. srsym:=nil;
  1560. srsymtable:=nil;
  1561. end;
  1562. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1563. var
  1564. sym : Tprocsym;
  1565. hashedid : THashedIDString;
  1566. curreq,
  1567. besteq : tequaltype;
  1568. currpd,
  1569. bestpd : tprocdef;
  1570. stackitem : psymtablestackitem;
  1571. begin
  1572. hashedid.id:='assign';
  1573. besteq:=te_incompatible;
  1574. bestpd:=nil;
  1575. stackitem:=symtablestack.stack;
  1576. while assigned(stackitem) do
  1577. begin
  1578. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1579. if sym<>nil then
  1580. begin
  1581. if sym.typ<>procsym then
  1582. internalerror(200402031);
  1583. { if the source type is an alias then this is only the second choice,
  1584. if you mess with this code, check tw4093 }
  1585. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1586. if curreq>besteq then
  1587. begin
  1588. besteq:=curreq;
  1589. bestpd:=currpd;
  1590. if (besteq=te_exact) then
  1591. break;
  1592. end;
  1593. end;
  1594. stackitem:=stackitem^.next;
  1595. end;
  1596. result:=bestpd;
  1597. end;
  1598. function search_system_type(const s: TIDString): ttypesym;
  1599. var
  1600. sym : tsym;
  1601. begin
  1602. sym:=tsym(systemunit.Find(s));
  1603. if not assigned(sym) or
  1604. (sym.typ<>typesym) then
  1605. cgmessage1(cg_f_unknown_system_type,s);
  1606. result:=ttypesym(sym);
  1607. end;
  1608. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1609. { searches n in symtable of pd and all anchestors }
  1610. var
  1611. hashedid : THashedIDString;
  1612. srsym : tsym;
  1613. begin
  1614. hashedid.id:=s;
  1615. while assigned(pd) do
  1616. begin
  1617. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1618. if assigned(srsym) then
  1619. begin
  1620. search_class_member:=srsym;
  1621. exit;
  1622. end;
  1623. pd:=pd.childof;
  1624. end;
  1625. search_class_member:=nil;
  1626. end;
  1627. function search_macro(const s : string):tsym;
  1628. var
  1629. stackitem : psymtablestackitem;
  1630. hashedid : THashedIDString;
  1631. srsym : tsym;
  1632. begin
  1633. hashedid.id:=s;
  1634. { First search the localmacrosymtable before searching the
  1635. global macrosymtables from the units }
  1636. if assigned(current_module) then
  1637. begin
  1638. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1639. if assigned(srsym) then
  1640. begin
  1641. result:= srsym;
  1642. exit;
  1643. end;
  1644. end;
  1645. stackitem:=macrosymtablestack.stack;
  1646. while assigned(stackitem) do
  1647. begin
  1648. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1649. if assigned(srsym) then
  1650. begin
  1651. result:= srsym;
  1652. exit;
  1653. end;
  1654. stackitem:=stackitem^.next;
  1655. end;
  1656. result:= nil;
  1657. end;
  1658. function defined_macro(const s : string):boolean;
  1659. var
  1660. mac: tmacro;
  1661. begin
  1662. mac:=tmacro(search_macro(s));
  1663. if assigned(mac) then
  1664. begin
  1665. mac.is_used:=true;
  1666. defined_macro:=mac.defined;
  1667. end
  1668. else
  1669. defined_macro:=false;
  1670. end;
  1671. {****************************************************************************
  1672. Object Helpers
  1673. ****************************************************************************}
  1674. procedure search_class_overloads(aprocsym : tprocsym);
  1675. { searches n in symtable of pd and all anchestors }
  1676. var
  1677. hashedid : THashedIDString;
  1678. srsym : tprocsym;
  1679. objdef : tobjectdef;
  1680. begin
  1681. if aprocsym.overloadchecked then
  1682. exit;
  1683. aprocsym.overloadchecked:=true;
  1684. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1685. internalerror(200111021);
  1686. objdef:=tobjectdef(aprocsym.owner.defowner);
  1687. { we start in the parent }
  1688. if not assigned(objdef.childof) then
  1689. exit;
  1690. objdef:=objdef.childof;
  1691. hashedid.id:=aprocsym.name;
  1692. while assigned(objdef) do
  1693. begin
  1694. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1695. if assigned(srsym) then
  1696. begin
  1697. if (srsym.typ<>procsym) then
  1698. internalerror(200111022);
  1699. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1700. begin
  1701. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1702. { we can stop if the overloads were already added
  1703. for the found symbol }
  1704. if srsym.overloadchecked then
  1705. break;
  1706. end;
  1707. end;
  1708. { next parent }
  1709. objdef:=objdef.childof;
  1710. end;
  1711. end;
  1712. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1713. begin
  1714. if (tsym(sym).typ=propertysym) and
  1715. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1716. ppointer(arg)^:=sym;
  1717. end;
  1718. function search_default_property(pd : tobjectdef) : tpropertysym;
  1719. { returns the default property of a class, searches also anchestors }
  1720. var
  1721. _defaultprop : tpropertysym;
  1722. begin
  1723. _defaultprop:=nil;
  1724. while assigned(pd) do
  1725. begin
  1726. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1727. if assigned(_defaultprop) then
  1728. break;
  1729. pd:=pd.childof;
  1730. end;
  1731. search_default_property:=_defaultprop;
  1732. end;
  1733. {****************************************************************************
  1734. Macro Helpers
  1735. ****************************************************************************}
  1736. procedure def_system_macro(const name : string);
  1737. var
  1738. mac : tmacro;
  1739. s: string;
  1740. begin
  1741. if name = '' then
  1742. internalerror(2004121202);
  1743. s:= upper(name);
  1744. mac:=tmacro(search_macro(s));
  1745. if not assigned(mac) then
  1746. begin
  1747. mac:=tmacro.create(s);
  1748. if assigned(current_module) then
  1749. current_module.localmacrosymtable.insert(mac)
  1750. else
  1751. initialmacrosymtable.insert(mac);
  1752. end;
  1753. if not mac.defined then
  1754. Message1(parser_c_macro_defined,mac.name);
  1755. mac.defined:=true;
  1756. end;
  1757. procedure set_system_macro(const name, value : string);
  1758. var
  1759. mac : tmacro;
  1760. s: string;
  1761. begin
  1762. if name = '' then
  1763. internalerror(2004121203);
  1764. s:= upper(name);
  1765. mac:=tmacro(search_macro(s));
  1766. if not assigned(mac) then
  1767. begin
  1768. mac:=tmacro.create(s);
  1769. if assigned(current_module) then
  1770. current_module.localmacrosymtable.insert(mac)
  1771. else
  1772. initialmacrosymtable.insert(mac);
  1773. end
  1774. else
  1775. begin
  1776. mac.is_compiler_var:=false;
  1777. if assigned(mac.buftext) then
  1778. freemem(mac.buftext,mac.buflen);
  1779. end;
  1780. Message2(parser_c_macro_set_to,mac.name,value);
  1781. mac.buflen:=length(value);
  1782. getmem(mac.buftext,mac.buflen);
  1783. move(value[1],mac.buftext^,mac.buflen);
  1784. mac.defined:=true;
  1785. end;
  1786. procedure set_system_compvar(const name, value : string);
  1787. var
  1788. mac : tmacro;
  1789. s: string;
  1790. begin
  1791. if name = '' then
  1792. internalerror(2004121204);
  1793. s:= upper(name);
  1794. mac:=tmacro(search_macro(s));
  1795. if not assigned(mac) then
  1796. begin
  1797. mac:=tmacro.create(s);
  1798. mac.is_compiler_var:=true;
  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:=true;
  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 undef_system_macro(const name : string);
  1817. var
  1818. mac : tmacro;
  1819. s: string;
  1820. begin
  1821. if name = '' then
  1822. internalerror(2004121205);
  1823. s:= upper(name);
  1824. mac:=tmacro(search_macro(s));
  1825. if not assigned(mac) then
  1826. {If not found, then it's already undefined.}
  1827. else
  1828. begin
  1829. if mac.defined then
  1830. Message1(parser_c_macro_undefined,mac.name);
  1831. mac.defined:=false;
  1832. mac.is_compiler_var:=false;
  1833. { delete old definition }
  1834. if assigned(mac.buftext) then
  1835. begin
  1836. freemem(mac.buftext,mac.buflen);
  1837. mac.buftext:=nil;
  1838. end;
  1839. end;
  1840. end;
  1841. {$ifdef UNITALIASES}
  1842. {****************************************************************************
  1843. TUNIT_ALIAS
  1844. ****************************************************************************}
  1845. constructor tunit_alias.create(const n:string);
  1846. var
  1847. i : longint;
  1848. begin
  1849. i:=pos('=',n);
  1850. if i=0 then
  1851. fail;
  1852. inherited createname(Copy(n,1,i-1));
  1853. newname:=stringdup(Copy(n,i+1,255));
  1854. end;
  1855. destructor tunit_alias.destroy;
  1856. begin
  1857. stringdispose(newname);
  1858. inherited destroy;
  1859. end;
  1860. procedure addunitalias(const n:string);
  1861. begin
  1862. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1863. end;
  1864. function getunitalias(const n:string):string;
  1865. var
  1866. p : punit_alias;
  1867. begin
  1868. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1869. if assigned(p) then
  1870. getunitalias:=punit_alias(p).newname^
  1871. else
  1872. getunitalias:=n;
  1873. end;
  1874. {$endif UNITALIASES}
  1875. {****************************************************************************
  1876. Init/Done Symtable
  1877. ****************************************************************************}
  1878. procedure InitSymtable;
  1879. begin
  1880. { Reset symbolstack }
  1881. symtablestack:=nil;
  1882. systemunit:=nil;
  1883. { create error syms and def }
  1884. generrorsym:=terrorsym.create;
  1885. generrordef:=terrordef.create;
  1886. { macros }
  1887. initialmacrosymtable:=tmacrosymtable.create(false);
  1888. macrosymtablestack:=TSymtablestack.create;
  1889. macrosymtablestack.push(initialmacrosymtable);
  1890. {$ifdef UNITALIASES}
  1891. { unit aliases }
  1892. unitaliases:=TFPHashObjectList.create;
  1893. {$endif}
  1894. { set some global vars to nil, might be important for the ide }
  1895. class_tobject:=nil;
  1896. interface_iunknown:=nil;
  1897. rec_tguid:=nil;
  1898. aktobjectdef:=nil;
  1899. dupnr:=0;
  1900. end;
  1901. procedure DoneSymtable;
  1902. begin
  1903. generrorsym.owner:=nil;
  1904. generrorsym.free;
  1905. generrordef.owner:=nil;
  1906. generrordef.free;
  1907. initialmacrosymtable.free;
  1908. macrosymtablestack.free;
  1909. {$ifdef UNITALIASES}
  1910. unitaliases.free;
  1911. {$endif}
  1912. end;
  1913. end.