symtable.pas 76 KB

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