symtable.pas 72 KB

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