symtable.pas 73 KB

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