symtable.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  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
  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(p : tnamedindexitem;arg:pointer);
  40. procedure check_forward(sym : TNamedIndexItem;arg:pointer);
  41. procedure labeldefined(p : TNamedIndexItem;arg:pointer);
  42. procedure varsymbolused(p : TNamedIndexItem;arg:pointer);
  43. procedure TestPrivate(p : TNamedIndexItem;arg:pointer);
  44. procedure objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  45. procedure unchain_overloads(p : TNamedIndexItem;arg:pointer);
  46. procedure loaddefs(ppufile:tcompilerppufile);
  47. procedure loadsyms(ppufile:tcompilerppufile);
  48. procedure reset_def(def:Tnamedindexitem;arg:pointer);
  49. procedure writedefs(ppufile:tcompilerppufile);
  50. procedure writesyms(ppufile:tcompilerppufile);
  51. public
  52. { load/write }
  53. procedure ppuload(ppufile:tcompilerppufile);virtual;
  54. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  55. procedure load_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  56. procedure write_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  57. procedure buildderef;virtual;
  58. procedure buildderefimpl;virtual;
  59. procedure deref;virtual;
  60. procedure derefimpl;virtual;
  61. procedure insert(sym : tsymentry);override;
  62. procedure reset_all_defs;virtual;
  63. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;override;
  64. procedure allsymbolsused;
  65. procedure allprivatesused;
  66. procedure check_forwards;
  67. procedure checklabels;
  68. function needs_init_final : boolean;
  69. procedure unchain_overloaded;
  70. {$ifdef GDB}
  71. procedure concatstabto(asmlist : taasmoutput);virtual;
  72. function getnewtypecount : word; override;
  73. {$endif GDB}
  74. procedure testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  75. end;
  76. tabstractrecordsymtable = class(tstoredsymtable)
  77. public
  78. datasize : aint;
  79. usefieldalignment, { alignment to use for fields (PACKRECORDS value), -1 is C style }
  80. recordalignment, { alignment required when inserting this record }
  81. fieldalignment, { alignment current alignment used when fields are inserted }
  82. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  83. constructor create(const n:string;usealign:shortint);
  84. procedure ppuload(ppufile:tcompilerppufile);override;
  85. procedure ppuwrite(ppufile:tcompilerppufile);override;
  86. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  87. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  88. procedure insertfield(sym:tfieldvarsym;addsym:boolean);
  89. procedure addalignmentpadding;
  90. end;
  91. trecordsymtable = class(tabstractrecordsymtable)
  92. public
  93. constructor create(usealign:shortint);
  94. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  95. end;
  96. tobjectsymtable = class(tabstractrecordsymtable)
  97. public
  98. constructor create(const n:string;usealign:shortint);
  99. procedure insert(sym : tsymentry);override;
  100. end;
  101. tabstractlocalsymtable = class(tstoredsymtable)
  102. public
  103. procedure ppuwrite(ppufile:tcompilerppufile);override;
  104. end;
  105. tlocalsymtable = class(tabstractlocalsymtable)
  106. public
  107. constructor create(level:byte);
  108. procedure insert(sym : tsymentry);override;
  109. end;
  110. tparasymtable = class(tabstractlocalsymtable)
  111. public
  112. constructor create(level:byte);
  113. procedure insert(sym : tsymentry);override;
  114. end;
  115. tabstractunitsymtable = class(tstoredsymtable)
  116. public
  117. {$ifdef GDB}
  118. dbx_count : longint;
  119. prev_dbx_counter : plongint;
  120. dbx_count_ok : boolean;
  121. {$endif GDB}
  122. constructor create(const n : string;id:word);
  123. {$ifdef GDB}
  124. procedure concattypestabto(asmlist : taasmoutput);
  125. {$endif GDB}
  126. function iscurrentunit:boolean;override;
  127. end;
  128. tglobalsymtable = class(tabstractunitsymtable)
  129. public
  130. unittypecount : word;
  131. constructor create(const n : string;id:word);
  132. procedure ppuload(ppufile:tcompilerppufile);override;
  133. procedure ppuwrite(ppufile:tcompilerppufile);override;
  134. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  135. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  136. procedure insert(sym : tsymentry);override;
  137. {$ifdef GDB}
  138. function getnewtypecount : word; override;
  139. {$endif}
  140. end;
  141. tstaticsymtable = class(tabstractunitsymtable)
  142. public
  143. constructor create(const n : string;id:word);
  144. procedure ppuload(ppufile:tcompilerppufile);override;
  145. procedure ppuwrite(ppufile:tcompilerppufile);override;
  146. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  147. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  148. procedure insert(sym : tsymentry);override;
  149. end;
  150. twithsymtable = class(tsymtable)
  151. withrefnode : pointer; { tnode }
  152. constructor create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  153. destructor destroy;override;
  154. procedure clear;override;
  155. end;
  156. tstt_exceptsymtable = class(tsymtable)
  157. public
  158. constructor create;
  159. end;
  160. tmacrosymtable = class(tstoredsymtable)
  161. public
  162. constructor create(exported: boolean);
  163. procedure ppuload(ppufile:tcompilerppufile);override;
  164. end;
  165. var
  166. constsymtable : tsymtable; { symtable were the constants can be inserted }
  167. systemunit : tglobalsymtable; { pointer to the system unit }
  168. {****************************************************************************
  169. Functions
  170. ****************************************************************************}
  171. {*** Misc ***}
  172. procedure globaldef(const s : string;var t:ttype);
  173. function findunitsymtable(st:tsymtable):tsymtable;
  174. function FullTypeName(def,otherdef:tdef):string;
  175. procedure incompatibletypes(def1,def2:tdef);
  176. procedure hidesym(sym:tsymentry);
  177. procedure duplicatesym(dupsym,sym:tsymentry);
  178. {*** Search ***}
  179. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  180. function searchsym_type(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  181. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  182. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  183. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  184. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  185. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  186. {$ifdef notused}
  187. function searchsysvar(const s: stringid; var srsym: tsym; var symowner: tsymtable): boolean;
  188. {$endif notused}
  189. function search_class_member(pd : tobjectdef;const s : string):tsym;
  190. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  191. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  192. {and returns it if found. Returns nil otherwise.}
  193. function search_macro(const s : string):tsym;
  194. {*** Object Helpers ***}
  195. procedure search_class_overloads(aprocsym : tprocsym);
  196. function search_default_property(pd : tobjectdef) : tpropertysym;
  197. {*** Macro Helpers ***}
  198. {If called initially, the following procedures manipulate macros in }
  199. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  200. {Name can be given in any case (it will be converted to upper case).}
  201. procedure def_system_macro(const name : string);
  202. procedure set_system_macro(const name, value : string);
  203. procedure set_system_compvar(const name, value : string);
  204. procedure undef_system_macro(const name : string);
  205. {*** symtable stack ***}
  206. {$ifdef DEBUG}
  207. procedure test_symtablestack;
  208. procedure list_symtablestack;
  209. {$endif DEBUG}
  210. {$ifdef UNITALIASES}
  211. type
  212. punit_alias = ^tunit_alias;
  213. tunit_alias = object(TNamedIndexItem)
  214. newname : pstring;
  215. constructor init(const n:string);
  216. destructor done;virtual;
  217. end;
  218. var
  219. unitaliases : pdictionary;
  220. procedure addunitalias(const n:string);
  221. function getunitalias(const n:string):string;
  222. {$endif UNITALIASES}
  223. {*** Init / Done ***}
  224. procedure InitSymtable;
  225. procedure DoneSymtable;
  226. const
  227. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  228. ('error',
  229. 'plus','minus','star','slash','equal',
  230. 'greater','lower','greater_or_equal',
  231. 'lower_or_equal',
  232. 'sym_diff','starstar',
  233. 'as','is','in','or',
  234. 'and','div','mod','not','shl','shr','xor',
  235. 'assign');
  236. implementation
  237. uses
  238. { global }
  239. verbose,globals,
  240. { target }
  241. systems,
  242. { symtable }
  243. symutil,defcmp,
  244. { module }
  245. fmodule,
  246. {$ifdef GDB}
  247. gdb,
  248. {$endif GDB}
  249. { codegen }
  250. procinfo
  251. ;
  252. {*****************************************************************************
  253. TStoredSymtable
  254. *****************************************************************************}
  255. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  256. begin
  257. { load definitions }
  258. loaddefs(ppufile);
  259. { load symbols }
  260. loadsyms(ppufile);
  261. end;
  262. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  263. begin
  264. { write definitions }
  265. writedefs(ppufile);
  266. { write symbols }
  267. writesyms(ppufile);
  268. end;
  269. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  270. var
  271. hp : tdef;
  272. b : byte;
  273. begin
  274. { load start of definition section, which holds the amount of defs }
  275. if ppufile.readentry<>ibstartdefs then
  276. Message(unit_f_ppu_read_error);
  277. ppufile.getlongint;
  278. { read definitions }
  279. repeat
  280. b:=ppufile.readentry;
  281. case b of
  282. ibpointerdef : hp:=tpointerdef.ppuload(ppufile);
  283. ibarraydef : hp:=tarraydef.ppuload(ppufile);
  284. iborddef : hp:=torddef.ppuload(ppufile);
  285. ibfloatdef : hp:=tfloatdef.ppuload(ppufile);
  286. ibprocdef : hp:=tprocdef.ppuload(ppufile);
  287. ibshortstringdef : hp:=tstringdef.loadshort(ppufile);
  288. iblongstringdef : hp:=tstringdef.loadlong(ppufile);
  289. {$ifdef ansistring_bits}
  290. ibansistring16def : hp:=tstringdef.loadansi(ppufile,sb_16);
  291. ibansistring32def : hp:=tstringdef.loadansi(ppufile,sb_32);
  292. ibansistring64def : hp:=tstringdef.loadansi(ppufile,sb_64);
  293. {$else}
  294. ibansistringdef : hp:=tstringdef.loadansi(ppufile);
  295. {$endif}
  296. ibwidestringdef : hp:=tstringdef.loadwide(ppufile);
  297. ibrecorddef : hp:=trecorddef.ppuload(ppufile);
  298. ibobjectdef : hp:=tobjectdef.ppuload(ppufile);
  299. ibenumdef : hp:=tenumdef.ppuload(ppufile);
  300. ibsetdef : hp:=tsetdef.ppuload(ppufile);
  301. ibprocvardef : hp:=tprocvardef.ppuload(ppufile);
  302. ibfiledef : hp:=tfiledef.ppuload(ppufile);
  303. ibclassrefdef : hp:=tclassrefdef.ppuload(ppufile);
  304. ibformaldef : hp:=tformaldef.ppuload(ppufile);
  305. ibvariantdef : hp:=tvariantdef.ppuload(ppufile);
  306. ibenddefs : break;
  307. ibend : Message(unit_f_ppu_read_error);
  308. else
  309. Message1(unit_f_ppu_invalid_entry,tostr(b));
  310. end;
  311. hp.owner:=self;
  312. defindex.insert(hp);
  313. until false;
  314. end;
  315. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  316. var
  317. b : byte;
  318. sym : tsym;
  319. begin
  320. { load start of definition section, which holds the amount of defs }
  321. if ppufile.readentry<>ibstartsyms then
  322. Message(unit_f_ppu_read_error);
  323. { skip amount of symbols, not used currently }
  324. ppufile.getlongint;
  325. { now read the symbols }
  326. repeat
  327. b:=ppufile.readentry;
  328. case b of
  329. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  330. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  331. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  332. ibglobalvarsym : sym:=tglobalvarsym.ppuload(ppufile);
  333. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  334. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  335. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  336. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  337. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  338. ibtypedconstsym : sym:=ttypedconstsym.ppuload(ppufile);
  339. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  340. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  341. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  342. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  343. ibrttisym : sym:=trttisym.ppuload(ppufile);
  344. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  345. ibendsyms : break;
  346. ibend : Message(unit_f_ppu_read_error);
  347. else
  348. Message1(unit_f_ppu_invalid_entry,tostr(b));
  349. end;
  350. sym.owner:=self;
  351. symindex.insert(sym);
  352. symsearch.insert(sym);
  353. until false;
  354. end;
  355. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  356. var
  357. pd : tstoreddef;
  358. begin
  359. { each definition get a number, write then the amount of defs to the
  360. ibstartdef entry }
  361. ppufile.putlongint(defindex.count);
  362. ppufile.writeentry(ibstartdefs);
  363. { now write the definition }
  364. pd:=tstoreddef(defindex.first);
  365. while assigned(pd) do
  366. begin
  367. pd.ppuwrite(ppufile);
  368. pd:=tstoreddef(pd.indexnext);
  369. end;
  370. { write end of definitions }
  371. ppufile.writeentry(ibenddefs);
  372. end;
  373. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  374. var
  375. pd : Tstoredsym;
  376. begin
  377. { each definition get a number, write then the amount of syms and the
  378. datasize to the ibsymdef entry }
  379. ppufile.putlongint(symindex.count);
  380. ppufile.writeentry(ibstartsyms);
  381. { foreach is used to write all symbols }
  382. pd:=Tstoredsym(symindex.first);
  383. while assigned(pd) do
  384. begin
  385. pd.ppuwrite(ppufile);
  386. pd:=Tstoredsym(pd.indexnext);
  387. end;
  388. { end of symbols }
  389. ppufile.writeentry(ibendsyms);
  390. end;
  391. procedure tstoredsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  392. var
  393. b : byte;
  394. d : tderef;
  395. sym : Tsym;
  396. prdef : tstoreddef;
  397. begin
  398. b:=ppufile.readentry;
  399. if b <> ibbeginsymtablebrowser then
  400. Message1(unit_f_ppu_invalid_entry,tostr(b));
  401. repeat
  402. b:=ppufile.readentry;
  403. case b of
  404. ibsymref :
  405. begin
  406. ppufile.getderef(d);
  407. sym:=Tsym(d.resolve);
  408. if assigned(sym) then
  409. sym.load_references(ppufile,locals);
  410. end;
  411. ibdefref :
  412. begin
  413. ppufile.getderef(d);
  414. prdef:=tstoreddef(d.resolve);
  415. if assigned(prdef) then
  416. begin
  417. if prdef.deftype<>procdef then
  418. Message(unit_f_ppu_read_error);
  419. tprocdef(prdef).load_references(ppufile,locals);
  420. end;
  421. end;
  422. ibendsymtablebrowser :
  423. break;
  424. else
  425. Message1(unit_f_ppu_invalid_entry,tostr(b));
  426. end;
  427. until false;
  428. end;
  429. procedure tstoredsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  430. var
  431. pd : Tsym;
  432. begin
  433. ppufile.writeentry(ibbeginsymtablebrowser);
  434. { write all symbols }
  435. pd:=Tsym(symindex.first);
  436. while assigned(pd) do
  437. begin
  438. pd.write_references(ppufile,locals);
  439. pd:=Tsym(pd.indexnext);
  440. end;
  441. ppufile.writeentry(ibendsymtablebrowser);
  442. end;
  443. procedure tstoredsymtable.buildderef;
  444. var
  445. hp : tdef;
  446. hs : tsym;
  447. begin
  448. { interface definitions }
  449. hp:=tdef(defindex.first);
  450. while assigned(hp) do
  451. begin
  452. hp.buildderef;
  453. hp:=tdef(hp.indexnext);
  454. end;
  455. { interface symbols }
  456. hs:=tsym(symindex.first);
  457. while assigned(hs) do
  458. begin
  459. hs.buildderef;
  460. hs:=tsym(hs.indexnext);
  461. end;
  462. end;
  463. procedure tstoredsymtable.buildderefimpl;
  464. var
  465. hp : tdef;
  466. begin
  467. { definitions }
  468. hp:=tdef(defindex.first);
  469. while assigned(hp) do
  470. begin
  471. hp.buildderefimpl;
  472. hp:=tdef(hp.indexnext);
  473. end;
  474. end;
  475. procedure tstoredsymtable.deref;
  476. var
  477. hp : tdef;
  478. hs : tsym;
  479. begin
  480. { first deref the interface ttype symbols. This is needs
  481. to be done before the interface defs are derefed, because
  482. the interface defs can contain references to the type symbols
  483. which then already need to contain a resolved restype field (PFV) }
  484. hs:=tsym(symindex.first);
  485. while assigned(hs) do
  486. begin
  487. if hs.typ=typesym then
  488. hs.deref;
  489. hs:=tsym(hs.indexnext);
  490. end;
  491. { deref the interface definitions }
  492. hp:=tdef(defindex.first);
  493. while assigned(hp) do
  494. begin
  495. hp.deref;
  496. hp:=tdef(hp.indexnext);
  497. end;
  498. { deref the interface symbols }
  499. hs:=tsym(symindex.first);
  500. while assigned(hs) do
  501. begin
  502. if hs.typ<>typesym then
  503. hs.deref;
  504. hs:=tsym(hs.indexnext);
  505. end;
  506. end;
  507. procedure tstoredsymtable.derefimpl;
  508. var
  509. hp : tdef;
  510. begin
  511. { definitions }
  512. hp:=tdef(defindex.first);
  513. while assigned(hp) do
  514. begin
  515. hp.derefimpl;
  516. hp:=tdef(hp.indexnext);
  517. end;
  518. end;
  519. procedure tstoredsymtable.insert(sym:tsymentry);
  520. var
  521. hsym : tsym;
  522. begin
  523. { set owner and sym indexnb }
  524. sym.owner:=self;
  525. { check the current symtable }
  526. hsym:=tsym(search(sym.name));
  527. if assigned(hsym) then
  528. begin
  529. { in TP and Delphi you can have a local with the
  530. same name as the function, the function is then hidden for
  531. the user. (Under delphi it can still be accessed using result),
  532. but don't allow hiding of RESULT }
  533. if (m_duplicate_names in aktmodeswitches) and
  534. (sym.typ in [localvarsym,paravarsym,absolutevarsym]) and
  535. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  536. not((m_result in aktmodeswitches) and
  537. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  538. sym.name:='hidden'+sym.name
  539. else
  540. DuplicateSym(sym,hsym);
  541. end;
  542. { register definition of typesym }
  543. if (sym.typ = typesym) and
  544. assigned(ttypesym(sym).restype.def) then
  545. begin
  546. if not(assigned(ttypesym(sym).restype.def.owner)) and
  547. (ttypesym(sym).restype.def.deftype<>errordef) then
  548. registerdef(ttypesym(sym).restype.def);
  549. end;
  550. { insert in index and search hash }
  551. symindex.insert(sym);
  552. symsearch.insert(sym);
  553. end;
  554. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  555. var
  556. hp : Tsym;
  557. newref : tref;
  558. begin
  559. hp:=Tsym(inherited speedsearch(s,speedvalue));
  560. if assigned(hp) then
  561. begin
  562. { reject non static members in static procedures }
  563. if (symtabletype=objectsymtable) and
  564. not(sp_static in hp.symoptions) and
  565. allow_only_static then
  566. Message(sym_e_only_static_in_static);
  567. { unit uses count }
  568. if assigned(current_module) and
  569. (symtabletype=globalsymtable) then
  570. begin
  571. if tglobalsymtable(self).moduleid>=current_module.unitmapsize then
  572. internalerror(200501152);
  573. inc(current_module.unitmap[tglobalsymtable(self).moduleid].refs);
  574. end;
  575. if make_ref and (cs_browser in aktmoduleswitches) then
  576. begin
  577. newref:=tref.create(hp.lastref,@akttokenpos);
  578. { for symbols that are in tables without browser info or syssyms }
  579. if hp.refcount=0 then
  580. begin
  581. hp.defref:=newref;
  582. hp.lastref:=newref;
  583. end
  584. else
  585. if resolving_forward and assigned(hp.defref) then
  586. { put it as second reference }
  587. begin
  588. newref.nextref:=hp.defref.nextref;
  589. hp.defref.nextref:=newref;
  590. hp.lastref.nextref:=nil;
  591. end
  592. else
  593. hp.lastref:=newref;
  594. inc(hp.refcount);
  595. end;
  596. if make_ref then
  597. inc(hp.refs);
  598. end; { value was not found }
  599. speedsearch:=hp;
  600. end;
  601. {**************************************
  602. Callbacks
  603. **************************************}
  604. procedure TStoredSymtable.check_forward(sym : TNamedIndexItem;arg:pointer);
  605. begin
  606. if tsym(sym).typ=procsym then
  607. tprocsym(sym).check_forward
  608. { check also object method table }
  609. { we needn't to test the def list }
  610. { because each object has to have a type sym,
  611. only test objects declarations, not type renamings }
  612. else
  613. if (tsym(sym).typ=typesym) and
  614. assigned(ttypesym(sym).restype.def) and
  615. (ttypesym(sym).restype.def.typesym=ttypesym(sym)) and
  616. (ttypesym(sym).restype.def.deftype=objectdef) then
  617. tobjectdef(ttypesym(sym).restype.def).check_forwards;
  618. end;
  619. procedure TStoredSymtable.labeldefined(p : TNamedIndexItem;arg:pointer);
  620. begin
  621. if (tsym(p).typ=labelsym) and
  622. not(tlabelsym(p).defined) then
  623. begin
  624. if tlabelsym(p).used then
  625. Message1(sym_e_label_used_and_not_defined,tlabelsym(p).realname)
  626. else
  627. Message1(sym_w_label_not_defined,tlabelsym(p).realname);
  628. end;
  629. end;
  630. procedure TStoredSymtable.varsymbolused(p : TNamedIndexItem;arg:pointer);
  631. begin
  632. if (tsym(p).typ in [globalvarsym,localvarsym,paravarsym,fieldvarsym]) and
  633. ((tsym(p).owner.symtabletype in
  634. [parasymtable,localsymtable,objectsymtable,staticsymtable])) then
  635. begin
  636. { unused symbol should be reported only if no }
  637. { error is reported }
  638. { if the symbol is in a register it is used }
  639. { also don't count the value parameters which have local copies }
  640. { also don't claim for high param of open parameters (PM) }
  641. if (Errorcount<>0) or
  642. (vo_is_hidden_para in tabstractvarsym(p).varoptions) then
  643. exit;
  644. if (tstoredsym(p).refs=0) then
  645. begin
  646. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  647. begin
  648. { don't warn about the result of constructors }
  649. if (tsym(p).owner.symtabletype<>localsymtable) or
  650. (tprocdef(tsym(p).owner.defowner).proctypeoption<>potype_constructor) then
  651. MessagePos(tsym(p).fileinfo,sym_w_function_result_not_set)
  652. end
  653. else if (tsym(p).owner.symtabletype=parasymtable) then
  654. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_not_used,tsym(p).realname)
  655. else if (tsym(p).owner.symtabletype=objectsymtable) then
  656. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_not_used,tsym(p).owner.realname^,tsym(p).realname)
  657. else
  658. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_not_used,tsym(p).realname);
  659. end
  660. else if tabstractvarsym(p).varstate=vs_assigned then
  661. begin
  662. if (tsym(p).owner.symtabletype=parasymtable) then
  663. begin
  664. if not(tabstractvarsym(p).varspez in [vs_var,vs_out]) and
  665. not(vo_is_funcret in tabstractvarsym(p).varoptions) then
  666. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_only_set,tsym(p).realname)
  667. end
  668. else if (tsym(p).owner.symtabletype=objectsymtable) then
  669. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_only_set,tsym(p).owner.realname^,tsym(p).realname)
  670. else if not(vo_is_exported in tabstractvarsym(p).varoptions) and
  671. not(vo_is_funcret in tabstractvarsym(p).varoptions) then
  672. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_only_set,tsym(p).realname);
  673. end;
  674. end
  675. else if ((tsym(p).owner.symtabletype in
  676. [objectsymtable,parasymtable,localsymtable,staticsymtable])) then
  677. begin
  678. if (Errorcount<>0) or
  679. (sp_internal in tsym(p).symoptions) then
  680. exit;
  681. { do not claim for inherited private fields !! }
  682. if (Tsym(p).refs=0) and (tsym(p).owner.symtabletype=objectsymtable) then
  683. MessagePos2(tsym(p).fileinfo,sym_n_private_method_not_used,tsym(p).owner.realname^,tsym(p).realname)
  684. { units references are problematic }
  685. else
  686. begin
  687. if (Tsym(p).refs=0) and
  688. not(tsym(p).typ in [enumsym,unitsym]) and
  689. not(is_funcret_sym(tsym(p))) and
  690. (
  691. (tsym(p).typ<>procsym) or
  692. ((tsym(p).owner.symtabletype=staticsymtable) and
  693. not current_module.is_unit)
  694. ) then
  695. MessagePos2(tsym(p).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(p).typ],tsym(p).realname);
  696. end;
  697. end;
  698. end;
  699. procedure TStoredSymtable.TestPrivate(p : TNamedIndexItem;arg:pointer);
  700. begin
  701. if sp_private in tsym(p).symoptions then
  702. varsymbolused(p,arg);
  703. end;
  704. procedure TStoredSymtable.objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  705. begin
  706. {
  707. Don't test simple object aliases PM
  708. }
  709. if (tsym(p).typ=typesym) and
  710. (ttypesym(p).restype.def.deftype=objectdef) and
  711. (ttypesym(p).restype.def.typesym=tsym(p)) then
  712. tobjectdef(ttypesym(p).restype.def).symtable.foreach(@TestPrivate,nil);
  713. end;
  714. procedure tstoredsymtable.unchain_overloads(p : TNamedIndexItem;arg:pointer);
  715. begin
  716. if tsym(p).typ=procsym then
  717. tprocsym(p).unchain_overload;
  718. end;
  719. procedure Tstoredsymtable.reset_def(def:Tnamedindexitem;arg:pointer);
  720. begin
  721. Tstoreddef(def).reset;
  722. end;
  723. {$ifdef GDB}
  724. function tstoredsymtable.getnewtypecount : word;
  725. begin
  726. getnewtypecount:=pglobaltypecount^;
  727. inc(pglobaltypecount^);
  728. end;
  729. {$endif GDB}
  730. {***********************************************
  731. Process all entries
  732. ***********************************************}
  733. procedure Tstoredsymtable.reset_all_defs;
  734. begin
  735. defindex.foreach(@reset_def,nil);
  736. end;
  737. { checks, if all procsyms and methods are defined }
  738. procedure tstoredsymtable.check_forwards;
  739. begin
  740. foreach(@check_forward,nil);
  741. end;
  742. procedure tstoredsymtable.checklabels;
  743. begin
  744. foreach(@labeldefined,nil);
  745. end;
  746. procedure tstoredsymtable.allsymbolsused;
  747. begin
  748. foreach(@varsymbolused,nil);
  749. end;
  750. procedure tstoredsymtable.allprivatesused;
  751. begin
  752. foreach(@objectprivatesymbolused,nil);
  753. end;
  754. procedure tstoredsymtable.unchain_overloaded;
  755. begin
  756. foreach(@unchain_overloads,nil);
  757. end;
  758. {$ifdef GDB}
  759. procedure tstoredsymtable.concatstabto(asmlist : taasmoutput);
  760. var
  761. stabstr : Pchar;
  762. p : tsym;
  763. begin
  764. p:=tsym(symindex.first);
  765. while assigned(p) do
  766. begin
  767. { Procsym and typesym are already written }
  768. if not(Tsym(p).typ in [procsym,typesym]) then
  769. begin
  770. if not Tsym(p).isstabwritten then
  771. begin
  772. stabstr:=Tsym(p).stabstring;
  773. if stabstr<>nil then
  774. asmlist.concat(Tai_stabs.create(stabstr));
  775. Tsym(p).isstabwritten:=true;
  776. end;
  777. end;
  778. p:=tsym(p.indexnext);
  779. end;
  780. end;
  781. {$endif}
  782. procedure TStoredSymtable._needs_init_final(p : tnamedindexitem;arg:pointer);
  783. begin
  784. if b_needs_init_final then
  785. exit;
  786. case tsym(p).typ of
  787. fieldvarsym,
  788. globalvarsym,
  789. localvarsym,
  790. paravarsym :
  791. begin
  792. if not(is_class(tabstractvarsym(p).vartype.def)) and
  793. tstoreddef(tabstractvarsym(p).vartype.def).needs_inittable then
  794. b_needs_init_final:=true;
  795. end;
  796. typedconstsym :
  797. begin
  798. if ttypedconstsym(p).is_writable and
  799. tstoreddef(ttypedconstsym(p).typedconsttype.def).needs_inittable then
  800. b_needs_init_final:=true;
  801. end;
  802. end;
  803. end;
  804. { returns true, if p contains data which needs init/final code }
  805. function tstoredsymtable.needs_init_final : boolean;
  806. begin
  807. b_needs_init_final:=false;
  808. foreach(@_needs_init_final,nil);
  809. needs_init_final:=b_needs_init_final;
  810. end;
  811. {****************************************************************************
  812. TAbstractRecordSymtable
  813. ****************************************************************************}
  814. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  815. begin
  816. inherited create(n);
  817. datasize:=0;
  818. recordalignment:=1;
  819. usefieldalignment:=usealign;
  820. padalignment:=1;
  821. { recordalign -1 means C record packing, that starts
  822. with an alignment of 1 }
  823. if usealign=-1 then
  824. fieldalignment:=1
  825. else
  826. fieldalignment:=usealign;
  827. end;
  828. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  829. var
  830. storesymtable : tsymtable;
  831. begin
  832. storesymtable:=aktrecordsymtable;
  833. aktrecordsymtable:=self;
  834. inherited ppuload(ppufile);
  835. aktrecordsymtable:=storesymtable;
  836. end;
  837. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  838. var
  839. oldtyp : byte;
  840. storesymtable : tsymtable;
  841. begin
  842. storesymtable:=aktrecordsymtable;
  843. aktrecordsymtable:=self;
  844. oldtyp:=ppufile.entrytyp;
  845. ppufile.entrytyp:=subentryid;
  846. inherited ppuwrite(ppufile);
  847. ppufile.entrytyp:=oldtyp;
  848. aktrecordsymtable:=storesymtable;
  849. end;
  850. procedure tabstractrecordsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  851. var
  852. storesymtable : tsymtable;
  853. begin
  854. storesymtable:=aktrecordsymtable;
  855. aktrecordsymtable:=self;
  856. inherited load_references(ppufile,locals);
  857. aktrecordsymtable:=storesymtable;
  858. end;
  859. procedure tabstractrecordsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  860. var
  861. storesymtable : tsymtable;
  862. begin
  863. storesymtable:=aktrecordsymtable;
  864. aktrecordsymtable:=self;
  865. inherited write_references(ppufile,locals);
  866. aktrecordsymtable:=storesymtable;
  867. end;
  868. procedure tabstractrecordsymtable.insertfield(sym : tfieldvarsym;addsym:boolean);
  869. var
  870. l : aint;
  871. varalignrecord,
  872. varalignfield,
  873. varalign : longint;
  874. vardef : tdef;
  875. begin
  876. if addsym then
  877. insert(sym);
  878. { this symbol can't be loaded to a register }
  879. sym.varregable:=vr_none;
  880. { Calculate field offset }
  881. l:=sym.getsize;
  882. vardef:=sym.vartype.def;
  883. varalign:=vardef.alignment;
  884. { Calc the alignment size for C style records }
  885. if (usefieldalignment=-1) then
  886. begin
  887. if (varalign>4) and
  888. ((varalign mod 4)<>0) and
  889. (vardef.deftype=arraydef) then
  890. Message1(sym_w_wrong_C_pack,vardef.typename);
  891. if varalign=0 then
  892. varalign:=l;
  893. if (fieldalignment<aktalignment.maxCrecordalign) then
  894. begin
  895. if (varalign>16) and (fieldalignment<32) then
  896. fieldalignment:=32
  897. else if (varalign>12) and (fieldalignment<16) then
  898. fieldalignment:=16
  899. { 12 is needed for long double }
  900. else if (varalign>8) and (fieldalignment<12) then
  901. fieldalignment:=12
  902. else if (varalign>4) and (fieldalignment<8) then
  903. fieldalignment:=8
  904. else if (varalign>2) and (fieldalignment<4) then
  905. fieldalignment:=4
  906. else if (varalign>1) and (fieldalignment<2) then
  907. fieldalignment:=2;
  908. end;
  909. fieldalignment:=min(fieldalignment,aktalignment.maxCrecordalign);
  910. end;
  911. if varalign=0 then
  912. varalign:=size_2_align(l);
  913. varalignfield:=used_align(varalign,aktalignment.recordalignmin,fieldalignment);
  914. sym.fieldoffset:=align(datasize,varalignfield);
  915. if (aword(l)+sym.fieldoffset)>high(aint) then
  916. begin
  917. Message(sym_e_segment_too_large);
  918. datasize:=high(aint);
  919. end
  920. else
  921. datasize:=sym.fieldoffset+l;
  922. { Calc alignment needed for this record }
  923. if (usefieldalignment=-1) then
  924. varalignrecord:=used_align(varalign,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
  925. else
  926. if (usefieldalignment=0) then
  927. varalignrecord:=used_align(varalign,aktalignment.recordalignmin,aktalignment.recordalignmax)
  928. else
  929. begin
  930. { packrecords is set explicit, ignore recordalignmax limit }
  931. varalignrecord:=used_align(varalign,aktalignment.recordalignmin,usefieldalignment);
  932. end;
  933. recordalignment:=max(recordalignment,varalignrecord);
  934. end;
  935. procedure tabstractrecordsymtable.addalignmentpadding;
  936. begin
  937. { make the record size aligned correctly so it can be
  938. used as elements in an array. For C records we
  939. use the fieldalignment, because that is updated with the
  940. used alignment. }
  941. if (padalignment = 1) then
  942. if usefieldalignment=-1 then
  943. padalignment:=fieldalignment
  944. else
  945. padalignment:=recordalignment;
  946. datasize:=align(datasize,padalignment);
  947. end;
  948. {****************************************************************************
  949. TRecordSymtable
  950. ****************************************************************************}
  951. constructor trecordsymtable.create(usealign:shortint);
  952. begin
  953. inherited create('',usealign);
  954. symtabletype:=recordsymtable;
  955. end;
  956. { this procedure is reserved for inserting case variant into
  957. a record symtable }
  958. { the offset is the location of the start of the variant
  959. and datasize and dataalignment corresponds to
  960. the complete size (see code in pdecl unit) PM }
  961. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  962. var
  963. ps,nps : tfieldvarsym;
  964. pd,npd : tdef;
  965. varalignrecord,varalign,
  966. storesize,storealign : longint;
  967. begin
  968. storesize:=datasize;
  969. storealign:=fieldalignment;
  970. datasize:=offset;
  971. ps:=tfieldvarsym(unionst.symindex.first);
  972. while assigned(ps) do
  973. begin
  974. nps:=tfieldvarsym(ps.indexnext);
  975. { remove from current symtable }
  976. unionst.symindex.deleteindex(ps);
  977. ps.left:=nil;
  978. ps.right:=nil;
  979. { add to this record }
  980. ps.owner:=self;
  981. datasize:=ps.fieldoffset+offset;
  982. symindex.insert(ps);
  983. symsearch.insert(ps);
  984. { update address }
  985. ps.fieldoffset:=datasize;
  986. { update alignment of this record }
  987. varalign:=ps.vartype.def.alignment;
  988. if varalign=0 then
  989. varalign:=size_2_align(ps.getsize);
  990. varalignrecord:=used_align(varalign,aktalignment.recordalignmin,fieldalignment);
  991. recordalignment:=max(recordalignment,varalignrecord);
  992. { next }
  993. ps:=nps;
  994. end;
  995. pd:=tdef(unionst.defindex.first);
  996. while assigned(pd) do
  997. begin
  998. npd:=tdef(pd.indexnext);
  999. unionst.defindex.deleteindex(pd);
  1000. pd.left:=nil;
  1001. pd.right:=nil;
  1002. registerdef(pd);
  1003. pd:=npd;
  1004. end;
  1005. datasize:=storesize;
  1006. fieldalignment:=storealign;
  1007. end;
  1008. {****************************************************************************
  1009. TObjectSymtable
  1010. ****************************************************************************}
  1011. constructor tobjectsymtable.create(const n:string;usealign:shortint);
  1012. begin
  1013. inherited create(n,usealign);
  1014. symtabletype:=objectsymtable;
  1015. end;
  1016. procedure tobjectsymtable.insert(sym:tsymentry);
  1017. var
  1018. hsym : tsym;
  1019. begin
  1020. { check for duplicate field id in inherited classes }
  1021. if (sym.typ=fieldvarsym) and
  1022. assigned(defowner) and
  1023. (
  1024. not(m_delphi in aktmodeswitches) or
  1025. is_object(tdef(defowner))
  1026. ) then
  1027. begin
  1028. { but private ids can be reused }
  1029. hsym:=search_class_member(tobjectdef(defowner),sym.name);
  1030. if assigned(hsym) and
  1031. tsym(hsym).is_visible_for_object(tobjectdef(defowner),tobjectdef(defowner)) then
  1032. DuplicateSym(sym,hsym);
  1033. end;
  1034. inherited insert(sym);
  1035. end;
  1036. {****************************************************************************
  1037. TAbstractLocalSymtable
  1038. ****************************************************************************}
  1039. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1040. var
  1041. oldtyp : byte;
  1042. begin
  1043. oldtyp:=ppufile.entrytyp;
  1044. ppufile.entrytyp:=subentryid;
  1045. { write definitions }
  1046. writedefs(ppufile);
  1047. { write symbols }
  1048. writesyms(ppufile);
  1049. ppufile.entrytyp:=oldtyp;
  1050. end;
  1051. {****************************************************************************
  1052. TLocalSymtable
  1053. ****************************************************************************}
  1054. constructor tlocalsymtable.create(level:byte);
  1055. begin
  1056. inherited create('');
  1057. symtabletype:=localsymtable;
  1058. symtablelevel:=level;
  1059. end;
  1060. procedure tlocalsymtable.insert(sym:tsymentry);
  1061. var
  1062. hsym : tsym;
  1063. begin
  1064. { need to hide function result? }
  1065. hsym:=tsym(search(sym.name));
  1066. if assigned(hsym) then
  1067. begin
  1068. { a local and the function can have the same
  1069. name in TP and Delphi, but RESULT not }
  1070. if (m_duplicate_names in aktmodeswitches) and
  1071. (hsym.typ in [absolutevarsym,localvarsym]) and
  1072. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1073. not((m_result in aktmodeswitches) and
  1074. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1075. HideSym(hsym)
  1076. else
  1077. DuplicateSym(sym,hsym);
  1078. end;
  1079. if assigned(next) and
  1080. (next.symtabletype=parasymtable) then
  1081. begin
  1082. { check para symtable }
  1083. hsym:=tsym(next.search(sym.name));
  1084. if assigned(hsym) then
  1085. begin
  1086. { a local and the function can have the same
  1087. name in TP and Delphi, but RESULT not }
  1088. if (m_duplicate_names in aktmodeswitches) and
  1089. (sym.typ in [absolutevarsym,paravarsym]) and
  1090. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1091. not((m_result in aktmodeswitches) and
  1092. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1093. sym.name:='hidden'+sym.name
  1094. else
  1095. DuplicateSym(sym,hsym);
  1096. end;
  1097. { check for duplicate id in local symtable of methods }
  1098. if assigned(next.next) and
  1099. { funcretsym is allowed !! }
  1100. (not is_funcret_sym(sym)) and
  1101. (next.next.symtabletype=objectsymtable) then
  1102. begin
  1103. hsym:=search_class_member(tobjectdef(next.next.defowner),sym.name);
  1104. if assigned(hsym) and
  1105. { private ids can be reused }
  1106. (hsym.is_visible_for_object(tobjectdef(next.next.defowner),tobjectdef(next.next.defowner)) or
  1107. (hsym.owner.defowner.owner.symtabletype<>globalsymtable)) then
  1108. begin
  1109. { delphi allows to reuse the names in a class, but not
  1110. in object (tp7 compatible) }
  1111. if not((m_delphi in aktmodeswitches) and
  1112. is_class(tdef(next.next.defowner))) then
  1113. DuplicateSym(sym,hsym);
  1114. end;
  1115. end;
  1116. end;
  1117. inherited insert(sym);
  1118. end;
  1119. {****************************************************************************
  1120. TParaSymtable
  1121. ****************************************************************************}
  1122. constructor tparasymtable.create(level:byte);
  1123. begin
  1124. inherited create('');
  1125. symtabletype:=parasymtable;
  1126. symtablelevel:=level;
  1127. end;
  1128. procedure tparasymtable.insert(sym:tsymentry);
  1129. var
  1130. hsym : tsym;
  1131. begin
  1132. { check for duplicate id in para symtable of methods }
  1133. if assigned(next) and
  1134. (next.symtabletype=objectsymtable) and
  1135. { funcretsym is allowed }
  1136. (not is_funcret_sym(sym)) then
  1137. begin
  1138. hsym:=search_class_member(tobjectdef(next.defowner),sym.name);
  1139. { private ids can be reused }
  1140. if assigned(hsym) and
  1141. Tsym(hsym).is_visible_for_object(tobjectdef(next.defowner),tobjectdef(next.defowner)) then
  1142. begin
  1143. { delphi allows to reuse the names in a class, but not
  1144. in object (tp7 compatible) }
  1145. if not((m_delphi in aktmodeswitches) and
  1146. is_class_or_interface(tobjectdef(next.defowner))) then
  1147. DuplicateSym(sym,hsym);
  1148. end;
  1149. end;
  1150. inherited insert(sym);
  1151. end;
  1152. {****************************************************************************
  1153. TAbstractUnitSymtable
  1154. ****************************************************************************}
  1155. constructor tabstractunitsymtable.create(const n : string;id:word);
  1156. begin
  1157. inherited create(n);
  1158. moduleid:=id;
  1159. symsearch.usehash;
  1160. {$ifdef GDB}
  1161. { reset GDB things }
  1162. prev_dbx_counter := dbx_counter;
  1163. dbx_counter := nil;
  1164. dbx_count := -1;
  1165. {$endif GDB}
  1166. end;
  1167. function tabstractunitsymtable.iscurrentunit:boolean;
  1168. begin
  1169. result:=assigned(current_module) and
  1170. (
  1171. (current_module.globalsymtable=self) or
  1172. (current_module.localsymtable=self)
  1173. );
  1174. end;
  1175. {$ifdef GDB}
  1176. procedure tabstractunitsymtable.concattypestabto(asmlist : taasmoutput);
  1177. procedure dowritestabs(asmlist:taasmoutput;st:tsymtable);
  1178. var
  1179. p : tstoreddef;
  1180. begin
  1181. p:=tstoreddef(st.defindex.first);
  1182. while assigned(p) do
  1183. begin
  1184. { also insert local types for the current unit }
  1185. if iscurrentunit then
  1186. begin
  1187. case p.deftype of
  1188. procdef :
  1189. if assigned(tprocdef(p).localst) then
  1190. dowritestabs(asmlist,tprocdef(p).localst);
  1191. objectdef :
  1192. dowritestabs(asmlist,tobjectdef(p).symtable);
  1193. end;
  1194. end;
  1195. if (p.stab_state=stab_state_used) then
  1196. p.concatstabto(asmlist);
  1197. p:=tstoreddef(p.indexnext);
  1198. end;
  1199. end;
  1200. var
  1201. old_writing_def_stabs : boolean;
  1202. prev_dbx_count : plongint;
  1203. begin
  1204. if not assigned(name) then
  1205. name := stringdup('Main_program');
  1206. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(moduleid))));
  1207. if cs_gdb_dbx in aktglobalswitches then
  1208. begin
  1209. if dbx_count_ok then
  1210. begin
  1211. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1212. +' has index '+tostr(moduleid)+' dbx count = '+tostr(dbx_count))));
  1213. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1214. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1215. exit;
  1216. end
  1217. else if not iscurrentunit then
  1218. begin
  1219. prev_dbx_count := dbx_counter;
  1220. dbx_counter := nil;
  1221. do_count_dbx:=false;
  1222. if (symtabletype = globalsymtable) then
  1223. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1224. dbx_counter := @dbx_count;
  1225. dbx_count:=0;
  1226. do_count_dbx:=assigned(dbx_counter);
  1227. end;
  1228. end;
  1229. old_writing_def_stabs:=writing_def_stabs;
  1230. writing_def_stabs:=true;
  1231. dowritestabs(asmlist,self);
  1232. writing_def_stabs:=old_writing_def_stabs;
  1233. if cs_gdb_dbx in aktglobalswitches then
  1234. begin
  1235. if not iscurrentunit then
  1236. begin
  1237. dbx_counter := prev_dbx_count;
  1238. do_count_dbx:=false;
  1239. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1240. +tostr(N_EINCL)+',0,0,0')));
  1241. do_count_dbx:=assigned(dbx_counter);
  1242. dbx_count_ok := {true}false;
  1243. end;
  1244. end;
  1245. asmList.concat(tai_comment.Create(strpnew('End unit '+name^+' has index '+tostr(moduleid))));
  1246. end;
  1247. {$endif GDB}
  1248. {****************************************************************************
  1249. TStaticSymtable
  1250. ****************************************************************************}
  1251. constructor tstaticsymtable.create(const n : string;id:word);
  1252. begin
  1253. inherited create(n,id);
  1254. symtabletype:=staticsymtable;
  1255. symtablelevel:=main_program_level;
  1256. end;
  1257. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1258. begin
  1259. next:=symtablestack;
  1260. symtablestack:=self;
  1261. inherited ppuload(ppufile);
  1262. { now we can deref the syms and defs }
  1263. deref;
  1264. { restore symtablestack }
  1265. symtablestack:=next;
  1266. end;
  1267. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1268. begin
  1269. inherited ppuwrite(ppufile);
  1270. end;
  1271. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1272. begin
  1273. inherited load_references(ppufile,locals);
  1274. end;
  1275. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1276. begin
  1277. inherited write_references(ppufile,locals);
  1278. end;
  1279. procedure tstaticsymtable.insert(sym:tsymentry);
  1280. var
  1281. hsym : tsym;
  1282. begin
  1283. { also check the global symtable }
  1284. if assigned(next) and
  1285. (next.symtabletype=globalsymtable) and
  1286. (next.iscurrentunit) then
  1287. begin
  1288. hsym:=tsym(next.search(sym.name));
  1289. if assigned(hsym) then
  1290. begin
  1291. { Delphi you can have a symbol with the same name as the
  1292. unit, the unit can then not be accessed anymore using
  1293. <unit>.<id>, so we can hide the symbol }
  1294. if (m_duplicate_names in aktmodeswitches) and
  1295. (hsym.typ=symconst.unitsym) then
  1296. HideSym(hsym)
  1297. else
  1298. DuplicateSym(sym,hsym);
  1299. end;
  1300. end;
  1301. inherited insert(sym);
  1302. end;
  1303. {****************************************************************************
  1304. TGlobalSymtable
  1305. ****************************************************************************}
  1306. constructor tglobalsymtable.create(const n : string;id:word);
  1307. begin
  1308. inherited create(n,id);
  1309. symtabletype:=globalsymtable;
  1310. symtablelevel:=main_program_level;
  1311. {$ifdef GDB}
  1312. if cs_gdb_dbx in aktglobalswitches then
  1313. begin
  1314. dbx_count := 0;
  1315. unittypecount:=1;
  1316. pglobaltypecount := @unittypecount;
  1317. {moduleid:=current_module.unitcount;}
  1318. {debugList.concat(tai_comment.Create(strpnew('Global '+name^+' has index '+tostr(moduleid))));
  1319. debugList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));}
  1320. {inc(current_module.unitcount);}
  1321. { we can't use dbx_vcount, because we don't know
  1322. if the object file will be loaded before or afeter PM }
  1323. dbx_count_ok:=false;
  1324. dbx_counter:=@dbx_count;
  1325. do_count_dbx:=true;
  1326. end;
  1327. {$endif GDB}
  1328. end;
  1329. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1330. {$ifdef GDB}
  1331. var
  1332. b : byte;
  1333. {$endif GDB}
  1334. begin
  1335. {$ifdef GDB}
  1336. if cs_gdb_dbx in aktglobalswitches then
  1337. begin
  1338. UnitTypeCount:=1;
  1339. PglobalTypeCount:=@UnitTypeCount;
  1340. end;
  1341. {$endif GDB}
  1342. next:=symtablestack;
  1343. symtablestack:=self;
  1344. inherited ppuload(ppufile);
  1345. { now we can deref the syms and defs }
  1346. deref;
  1347. { restore symtablestack }
  1348. symtablestack:=next;
  1349. { read dbx count }
  1350. {$ifdef GDB}
  1351. if (current_module.flags and uf_has_dbx)<>0 then
  1352. begin
  1353. b:=ppufile.readentry;
  1354. if b<>ibdbxcount then
  1355. Message(unit_f_ppu_dbx_count_problem)
  1356. else
  1357. dbx_count:=ppufile.getlongint;
  1358. {$IfDef EXTDEBUG}
  1359. writeln('Read dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1360. {$ENDIF EXTDEBUG}
  1361. { we can't use dbx_vcount, because we don't know
  1362. if the object file will be loaded before or afeter PM }
  1363. dbx_count_ok := {true}false;
  1364. end
  1365. else
  1366. begin
  1367. dbx_count:=-1;
  1368. dbx_count_ok:=false;
  1369. end;
  1370. {$endif GDB}
  1371. end;
  1372. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1373. begin
  1374. { write the symtable entries }
  1375. inherited ppuwrite(ppufile);
  1376. { write dbx count }
  1377. {$ifdef GDB}
  1378. if cs_gdb_dbx in aktglobalswitches then
  1379. begin
  1380. {$IfDef EXTDEBUG}
  1381. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1382. {$ENDIF EXTDEBUG}
  1383. ppufile.do_crc:=false;
  1384. ppufile.putlongint(dbx_count);
  1385. ppufile.writeentry(ibdbxcount);
  1386. ppufile.do_crc:=true;
  1387. end;
  1388. {$endif GDB}
  1389. end;
  1390. procedure tglobalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1391. begin
  1392. inherited load_references(ppufile,locals);
  1393. end;
  1394. procedure tglobalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1395. begin
  1396. inherited write_references(ppufile,locals);
  1397. end;
  1398. procedure tglobalsymtable.insert(sym:tsymentry);
  1399. var
  1400. hsym : tsym;
  1401. begin
  1402. hsym:=tsym(search(sym.name));
  1403. if assigned(hsym) then
  1404. begin
  1405. { Delphi you can have a symbol with the same name as the
  1406. unit, the unit can then not be accessed anymore using
  1407. <unit>.<id>, so we can hide the symbol }
  1408. if (m_duplicate_names in aktmodeswitches) and
  1409. (hsym.typ=symconst.unitsym) then
  1410. HideSym(hsym)
  1411. else
  1412. DuplicateSym(sym,hsym);
  1413. end;
  1414. inherited insert(sym);
  1415. end;
  1416. {$ifdef GDB}
  1417. function tglobalsymtable.getnewtypecount : word;
  1418. begin
  1419. if not (cs_gdb_dbx in aktglobalswitches) then
  1420. getnewtypecount:=inherited getnewtypecount
  1421. else
  1422. begin
  1423. getnewtypecount:=unittypecount;
  1424. inc(unittypecount);
  1425. end;
  1426. end;
  1427. {$endif}
  1428. {****************************************************************************
  1429. TWITHSYMTABLE
  1430. ****************************************************************************}
  1431. constructor twithsymtable.create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  1432. begin
  1433. inherited create('');
  1434. symtabletype:=withsymtable;
  1435. withrefnode:=refnode;
  1436. { we don't need the symsearch }
  1437. symsearch.free;
  1438. { set the defaults }
  1439. symsearch:=asymsearch;
  1440. defowner:=aowner;
  1441. end;
  1442. destructor twithsymtable.destroy;
  1443. begin
  1444. tobject(withrefnode).free;
  1445. symsearch:=nil;
  1446. inherited destroy;
  1447. end;
  1448. procedure twithsymtable.clear;
  1449. begin
  1450. { remove no entry from a withsymtable as it is only a pointer to the
  1451. recorddef or objectdef symtable }
  1452. end;
  1453. {****************************************************************************
  1454. TSTT_ExceptionSymtable
  1455. ****************************************************************************}
  1456. constructor tstt_exceptsymtable.create;
  1457. begin
  1458. inherited create('');
  1459. symtabletype:=stt_exceptsymtable;
  1460. end;
  1461. {****************************************************************************
  1462. TMacroSymtable
  1463. ****************************************************************************}
  1464. constructor tmacrosymtable.create(exported: boolean);
  1465. begin
  1466. inherited create('');
  1467. if exported then
  1468. symtabletype:=exportedmacrosymtable
  1469. else
  1470. symtabletype:=localmacrosymtable;
  1471. symtablelevel:=main_program_level;
  1472. end;
  1473. procedure tmacrosymtable.ppuload(ppufile:tcompilerppufile);
  1474. begin
  1475. next:=macrosymtablestack;
  1476. macrosymtablestack:=self;
  1477. inherited ppuload(ppufile);
  1478. { restore symtablestack }
  1479. macrosymtablestack:=next;
  1480. end;
  1481. {*****************************************************************************
  1482. Helper Routines
  1483. *****************************************************************************}
  1484. function findunitsymtable(st:tsymtable):tsymtable;
  1485. begin
  1486. findunitsymtable:=nil;
  1487. repeat
  1488. if not assigned(st) then
  1489. internalerror(5566561);
  1490. case st.symtabletype of
  1491. localsymtable,
  1492. parasymtable,
  1493. staticsymtable :
  1494. exit;
  1495. globalsymtable :
  1496. begin
  1497. findunitsymtable:=st;
  1498. exit;
  1499. end;
  1500. objectsymtable :
  1501. st:=st.defowner.owner;
  1502. recordsymtable :
  1503. begin
  1504. { don't continue when the current
  1505. symtable is used for variant records }
  1506. if trecorddef(st.defowner).isunion then
  1507. begin
  1508. findunitsymtable:=nil;
  1509. exit;
  1510. end
  1511. else
  1512. st:=st.defowner.owner;
  1513. end;
  1514. else
  1515. internalerror(5566562);
  1516. end;
  1517. until false;
  1518. end;
  1519. function FullTypeName(def,otherdef:tdef):string;
  1520. var
  1521. s1,s2 : string;
  1522. begin
  1523. s1:=def.typename;
  1524. { When the names are the same try to include the unit name }
  1525. if assigned(otherdef) and
  1526. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1527. begin
  1528. s2:=otherdef.typename;
  1529. if upper(s1)=upper(s2) then
  1530. s1:=def.owner.realname^+'.'+s1;
  1531. end;
  1532. FullTypeName:=s1;
  1533. end;
  1534. procedure incompatibletypes(def1,def2:tdef);
  1535. begin
  1536. { When there is an errordef there is already an error message show }
  1537. if (def2.deftype=errordef) or
  1538. (def1.deftype=errordef) then
  1539. exit;
  1540. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1541. end;
  1542. procedure hidesym(sym:tsymentry);
  1543. var
  1544. s : string;
  1545. begin
  1546. if assigned(sym.owner) then
  1547. sym.owner.rename(sym.name,'hidden'+sym.name)
  1548. else
  1549. sym.name:='hidden'+sym.name;
  1550. s:='hidden'+tsym(sym).realname;
  1551. stringdispose(tsym(sym)._realname);
  1552. tsym(sym)._realname:=stringdup(s);
  1553. end;
  1554. var
  1555. dupnr : longint; { unique number for duplicate symbols }
  1556. procedure duplicatesym(dupsym,sym:tsymentry);
  1557. var
  1558. st : tsymtable;
  1559. begin
  1560. Message1(sym_e_duplicate_id,tsym(sym).realname);
  1561. st:=findunitsymtable(sym.owner);
  1562. with tsym(sym).fileinfo do
  1563. begin
  1564. if assigned(st) and
  1565. (st.symtabletype=globalsymtable) and
  1566. (not st.iscurrentunit) then
  1567. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  1568. else
  1569. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line));
  1570. end;
  1571. { Rename duplicate sym to an unreachable name, but it can be
  1572. inserted in the symtable without errors }
  1573. if assigned(dupsym) then
  1574. begin
  1575. inc(dupnr);
  1576. dupsym.name:='dup'+tostr(dupnr)+dupsym.name;
  1577. end;
  1578. end;
  1579. {*****************************************************************************
  1580. Search
  1581. *****************************************************************************}
  1582. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1583. var
  1584. speedvalue : cardinal;
  1585. topclass : tobjectdef;
  1586. context : tobjectdef;
  1587. begin
  1588. speedvalue:=getspeedvalue(s);
  1589. srsymtable:=symtablestack;
  1590. while assigned(srsymtable) do
  1591. begin
  1592. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1593. if assigned(srsym) then
  1594. begin
  1595. topclass:=nil;
  1596. { use the class from withsymtable only when it is
  1597. defined in this unit }
  1598. if (srsymtable.symtabletype=withsymtable) and
  1599. assigned(srsymtable.defowner) and
  1600. (srsymtable.defowner.deftype=objectdef) and
  1601. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1602. (srsymtable.defowner.owner.iscurrentunit) then
  1603. topclass:=tobjectdef(srsymtable.defowner)
  1604. else
  1605. begin
  1606. if assigned(current_procinfo) then
  1607. topclass:=current_procinfo.procdef._class;
  1608. end;
  1609. if assigned(current_procinfo) then
  1610. context:=current_procinfo.procdef._class
  1611. else
  1612. context:=nil;
  1613. if tsym(srsym).is_visible_for_object(topclass,context) then
  1614. begin
  1615. { we need to know if a procedure references symbols
  1616. in the static symtable, because then it can't be
  1617. inlined from outside this unit }
  1618. if assigned(current_procinfo) and
  1619. (srsym.owner.symtabletype=staticsymtable) then
  1620. include(current_procinfo.flags,pi_uses_static_symtable);
  1621. searchsym:=true;
  1622. exit;
  1623. end;
  1624. end;
  1625. srsymtable:=srsymtable.next;
  1626. end;
  1627. searchsym:=false;
  1628. end;
  1629. function searchsym_type(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1630. var
  1631. speedvalue : cardinal;
  1632. begin
  1633. speedvalue:=getspeedvalue(s);
  1634. srsymtable:=symtablestack;
  1635. while assigned(srsymtable) do
  1636. begin
  1637. {
  1638. It is not possible to have type defintions in:
  1639. records
  1640. objects
  1641. parameters
  1642. }
  1643. if not(srsymtable.symtabletype in [recordsymtable,objectsymtable,parasymtable]) then
  1644. begin
  1645. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1646. if assigned(srsym) and
  1647. (not assigned(current_procinfo) or
  1648. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1649. begin
  1650. result:=true;
  1651. exit;
  1652. end
  1653. end;
  1654. srsymtable:=srsymtable.next;
  1655. end;
  1656. result:=false;
  1657. end;
  1658. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1659. var
  1660. srsym : tsym;
  1661. begin
  1662. { the caller have to take care if srsym=nil }
  1663. if assigned(p) then
  1664. begin
  1665. srsym:=tsym(p.search(s));
  1666. if assigned(srsym) then
  1667. begin
  1668. searchsymonlyin:=srsym;
  1669. exit;
  1670. end;
  1671. { also check in the local symtbale if it exists }
  1672. if (p.symtabletype=globalsymtable) and
  1673. (p.iscurrentunit) then
  1674. begin
  1675. srsym:=tsym(current_module.localsymtable.search(s));
  1676. if assigned(srsym) then
  1677. begin
  1678. searchsymonlyin:=srsym;
  1679. exit;
  1680. end;
  1681. end
  1682. end;
  1683. searchsymonlyin:=nil;
  1684. end;
  1685. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1686. var
  1687. speedvalue : cardinal;
  1688. topclassh : tobjectdef;
  1689. sym : tsym;
  1690. begin
  1691. speedvalue:=getspeedvalue(s);
  1692. { when the class passed is defined in this unit we
  1693. need to use the scope of that class. This is a trick
  1694. that can be used to access protected members in other
  1695. units. At least kylix supports it this way (PFV) }
  1696. if assigned(classh) and
  1697. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1698. classh.owner.iscurrentunit then
  1699. topclassh:=classh
  1700. else
  1701. begin
  1702. if assigned(current_procinfo) then
  1703. topclassh:=current_procinfo.procdef._class
  1704. else
  1705. topclassh:=nil;
  1706. end;
  1707. sym:=nil;
  1708. while assigned(classh) do
  1709. begin
  1710. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1711. if assigned(sym) and
  1712. tsym(sym).is_visible_for_object(topclassh,current_procinfo.procdef._class) then
  1713. break
  1714. else
  1715. sym:=nil;
  1716. classh:=classh.childof;
  1717. end;
  1718. searchsym_in_class:=sym;
  1719. end;
  1720. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  1721. var
  1722. topclassh : tobjectdef;
  1723. def : tdef;
  1724. sym : tsym;
  1725. begin
  1726. { when the class passed is defined in this unit we
  1727. need to use the scope of that class. This is a trick
  1728. that can be used to access protected members in other
  1729. units. At least kylix supports it this way (PFV) }
  1730. if assigned(classh) and
  1731. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1732. classh.owner.iscurrentunit then
  1733. topclassh:=classh
  1734. else
  1735. begin
  1736. if assigned(current_procinfo) then
  1737. topclassh:=current_procinfo.procdef._class
  1738. else
  1739. topclassh:=nil;
  1740. end;
  1741. sym:=nil;
  1742. def:=nil;
  1743. while assigned(classh) do
  1744. begin
  1745. def:=tdef(classh.symtable.defindex.first);
  1746. while assigned(def) do
  1747. begin
  1748. if (def.deftype=procdef) and
  1749. (po_msgint in tprocdef(def).procoptions) and
  1750. (tprocdef(def).messageinf.i=i) then
  1751. begin
  1752. sym:=tprocdef(def).procsym;
  1753. if assigned(topclassh) then
  1754. begin
  1755. if tprocdef(def).is_visible_for_object(topclassh) then
  1756. break;
  1757. end
  1758. else
  1759. break;
  1760. end;
  1761. def:=tdef(def.indexnext);
  1762. end;
  1763. if assigned(sym) then
  1764. break;
  1765. classh:=classh.childof;
  1766. end;
  1767. searchsym_in_class_by_msgint:=sym;
  1768. end;
  1769. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  1770. var
  1771. topclassh : tobjectdef;
  1772. def : tdef;
  1773. sym : tsym;
  1774. begin
  1775. { when the class passed is defined in this unit we
  1776. need to use the scope of that class. This is a trick
  1777. that can be used to access protected members in other
  1778. units. At least kylix supports it this way (PFV) }
  1779. if assigned(classh) and
  1780. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1781. classh.owner.iscurrentunit then
  1782. topclassh:=classh
  1783. else
  1784. begin
  1785. if assigned(current_procinfo) then
  1786. topclassh:=current_procinfo.procdef._class
  1787. else
  1788. topclassh:=nil;
  1789. end;
  1790. sym:=nil;
  1791. def:=nil;
  1792. while assigned(classh) do
  1793. begin
  1794. def:=tdef(classh.symtable.defindex.first);
  1795. while assigned(def) do
  1796. begin
  1797. if (def.deftype=procdef) and
  1798. (po_msgstr in tprocdef(def).procoptions) and
  1799. (tprocdef(def).messageinf.str=s) then
  1800. begin
  1801. sym:=tprocdef(def).procsym;
  1802. if assigned(topclassh) then
  1803. begin
  1804. if tprocdef(def).is_visible_for_object(topclassh) then
  1805. break;
  1806. end
  1807. else
  1808. break;
  1809. end;
  1810. def:=tdef(def.indexnext);
  1811. end;
  1812. if assigned(sym) then
  1813. break;
  1814. classh:=classh.childof;
  1815. end;
  1816. searchsym_in_class_by_msgstr:=sym;
  1817. end;
  1818. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1819. var st:Tsymtable;
  1820. sym:Tprocsym;
  1821. sv:cardinal;
  1822. curreq,
  1823. besteq : tequaltype;
  1824. currpd,
  1825. bestpd : tprocdef;
  1826. begin
  1827. st:=symtablestack;
  1828. sv:=getspeedvalue('assign');
  1829. besteq:=te_incompatible;
  1830. bestpd:=nil;
  1831. while st<>nil do
  1832. begin
  1833. sym:=Tprocsym(st.speedsearch('assign',sv));
  1834. if sym<>nil then
  1835. begin
  1836. if sym.typ<>procsym then
  1837. internalerror(200402031);
  1838. { if the source type is an alias then this is only the second choice,
  1839. if you mess with this code, check tw4093 }
  1840. currpd:=sym.search_procdef_assignment_operator(from_def,to_def,curreq);
  1841. if curreq>besteq then
  1842. begin
  1843. besteq:=curreq;
  1844. bestpd:=currpd;
  1845. if (besteq=te_exact) then
  1846. break;
  1847. end;
  1848. end;
  1849. st:=st.next;
  1850. end;
  1851. result:=bestpd;
  1852. end;
  1853. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1854. var
  1855. symowner: tsymtable;
  1856. begin
  1857. if not(cs_compilesystem in aktmoduleswitches) then
  1858. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1859. else
  1860. searchsym(s,tsym(srsym),symowner);
  1861. searchsystype :=
  1862. assigned(srsym) and
  1863. (srsym.typ = typesym);
  1864. end;
  1865. {$ifdef notused}
  1866. function searchsysvar(const s: stringid; var srsym: tsym; var symowner: tsymtable): boolean;
  1867. begin
  1868. if not(cs_compilesystem in aktmoduleswitches) then
  1869. begin
  1870. srsym := searchsymonlyin(systemunit,s);
  1871. symowner := systemunit;
  1872. end
  1873. else
  1874. searchsym(s,tsym(srsym),symowner);
  1875. searchsysvar :=
  1876. assigned(srsym) and
  1877. (srsym.typ = globalvarsym);
  1878. end;
  1879. {$endif notused}
  1880. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1881. { searches n in symtable of pd and all anchestors }
  1882. var
  1883. speedvalue : cardinal;
  1884. srsym : tsym;
  1885. begin
  1886. speedvalue:=getspeedvalue(s);
  1887. while assigned(pd) do
  1888. begin
  1889. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1890. if assigned(srsym) then
  1891. begin
  1892. search_class_member:=srsym;
  1893. exit;
  1894. end;
  1895. pd:=pd.childof;
  1896. end;
  1897. search_class_member:=nil;
  1898. end;
  1899. function search_macro(const s : string):tsym;
  1900. var
  1901. p : tsymtable;
  1902. speedvalue : cardinal;
  1903. srsym : tsym;
  1904. begin
  1905. speedvalue:= getspeedvalue(s);
  1906. p:=macrosymtablestack;
  1907. while assigned(p) do
  1908. begin
  1909. srsym:=tsym(p.speedsearch(s,speedvalue));
  1910. if assigned(srsym) then
  1911. begin
  1912. search_macro:= srsym;
  1913. exit;
  1914. end;
  1915. p:=p.next;
  1916. end;
  1917. search_macro:= nil;
  1918. end;
  1919. {*****************************************************************************
  1920. Definition Helpers
  1921. *****************************************************************************}
  1922. procedure globaldef(const s : string;var t:ttype);
  1923. var st : string;
  1924. symt : tsymtable;
  1925. srsym : tsym;
  1926. srsymtable : tsymtable;
  1927. begin
  1928. srsym := nil;
  1929. if pos('.',s) > 0 then
  1930. begin
  1931. st := copy(s,1,pos('.',s)-1);
  1932. searchsym(st,srsym,srsymtable);
  1933. st := copy(s,pos('.',s)+1,255);
  1934. if assigned(srsym) then
  1935. begin
  1936. if srsym.typ = unitsym then
  1937. begin
  1938. symt := tunitsym(srsym).unitsymtable;
  1939. srsym := tsym(symt.search(st));
  1940. end else srsym := nil;
  1941. end;
  1942. end else st := s;
  1943. if srsym = nil then
  1944. searchsym(st,srsym,srsymtable);
  1945. if srsym = nil then
  1946. srsym:=searchsymonlyin(systemunit,st);
  1947. if (not assigned(srsym)) or
  1948. (srsym.typ<>typesym) then
  1949. begin
  1950. Message(type_e_type_id_expected);
  1951. t:=generrortype;
  1952. exit;
  1953. end;
  1954. t := ttypesym(srsym).restype;
  1955. end;
  1956. {****************************************************************************
  1957. Object Helpers
  1958. ****************************************************************************}
  1959. procedure search_class_overloads(aprocsym : tprocsym);
  1960. { searches n in symtable of pd and all anchestors }
  1961. var
  1962. speedvalue : cardinal;
  1963. srsym : tprocsym;
  1964. s : string;
  1965. objdef : tobjectdef;
  1966. begin
  1967. if aprocsym.overloadchecked then
  1968. exit;
  1969. aprocsym.overloadchecked:=true;
  1970. if (aprocsym.owner.symtabletype<>objectsymtable) then
  1971. internalerror(200111021);
  1972. objdef:=tobjectdef(aprocsym.owner.defowner);
  1973. { we start in the parent }
  1974. if not assigned(objdef.childof) then
  1975. exit;
  1976. objdef:=objdef.childof;
  1977. s:=aprocsym.name;
  1978. speedvalue:=getspeedvalue(s);
  1979. while assigned(objdef) do
  1980. begin
  1981. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  1982. if assigned(srsym) then
  1983. begin
  1984. if (srsym.typ<>procsym) then
  1985. internalerror(200111022);
  1986. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1987. begin
  1988. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1989. { we can stop if the overloads were already added
  1990. for the found symbol }
  1991. if srsym.overloadchecked then
  1992. break;
  1993. end;
  1994. end;
  1995. { next parent }
  1996. objdef:=objdef.childof;
  1997. end;
  1998. end;
  1999. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  2000. begin
  2001. if (tsym(p).typ=propertysym) and
  2002. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  2003. ppointer(arg)^:=p;
  2004. end;
  2005. function search_default_property(pd : tobjectdef) : tpropertysym;
  2006. { returns the default property of a class, searches also anchestors }
  2007. var
  2008. _defaultprop : tpropertysym;
  2009. begin
  2010. _defaultprop:=nil;
  2011. while assigned(pd) do
  2012. begin
  2013. pd.symtable.foreach(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2014. if assigned(_defaultprop) then
  2015. break;
  2016. pd:=pd.childof;
  2017. end;
  2018. search_default_property:=_defaultprop;
  2019. end;
  2020. {****************************************************************************
  2021. Macro Helpers
  2022. ****************************************************************************}
  2023. {NOTE: Initially, macrosymtablestack contains initialmacrosymtable.}
  2024. procedure def_system_macro(const name : string);
  2025. var
  2026. mac : tmacro;
  2027. s: string;
  2028. begin
  2029. if name = '' then
  2030. internalerror(2004121201);
  2031. s:= upper(name);
  2032. mac:=tmacro(search_macro(s));
  2033. if not assigned(mac) then
  2034. begin
  2035. mac:=tmacro.create(s);
  2036. if macrosymtablestack.symtabletype=localmacrosymtable then
  2037. macrosymtablestack.insert(mac)
  2038. else
  2039. macrosymtablestack.next.insert(mac)
  2040. end;
  2041. if not mac.defined then
  2042. Message1(parser_c_macro_defined,mac.name);
  2043. mac.defined:=true;
  2044. end;
  2045. procedure set_system_macro(const name, value : string);
  2046. var
  2047. mac : tmacro;
  2048. s: string;
  2049. begin
  2050. if name = '' then
  2051. internalerror(2004121201);
  2052. s:= upper(name);
  2053. mac:=tmacro(search_macro(s));
  2054. if not assigned(mac) then
  2055. begin
  2056. mac:=tmacro.create(s);
  2057. if macrosymtablestack.symtabletype=localmacrosymtable then
  2058. macrosymtablestack.insert(mac)
  2059. else
  2060. macrosymtablestack.next.insert(mac)
  2061. end
  2062. else
  2063. begin
  2064. mac.is_compiler_var:=false;
  2065. if assigned(mac.buftext) then
  2066. freemem(mac.buftext,mac.buflen);
  2067. end;
  2068. Message2(parser_c_macro_set_to,mac.name,value);
  2069. mac.buflen:=length(value);
  2070. getmem(mac.buftext,mac.buflen);
  2071. move(value[1],mac.buftext^,mac.buflen);
  2072. mac.defined:=true;
  2073. end;
  2074. procedure set_system_compvar(const name, value : string);
  2075. var
  2076. mac : tmacro;
  2077. s: string;
  2078. begin
  2079. if name = '' then
  2080. internalerror(2004121201);
  2081. s:= upper(name);
  2082. mac:=tmacro(search_macro(s));
  2083. if not assigned(mac) then
  2084. begin
  2085. mac:=tmacro.create(s);
  2086. mac.is_compiler_var:=true;
  2087. if macrosymtablestack.symtabletype=localmacrosymtable then
  2088. macrosymtablestack.insert(mac)
  2089. else
  2090. macrosymtablestack.next.insert(mac)
  2091. end
  2092. else
  2093. begin
  2094. mac.is_compiler_var:=true;
  2095. if assigned(mac.buftext) then
  2096. freemem(mac.buftext,mac.buflen);
  2097. end;
  2098. Message2(parser_c_macro_set_to,mac.name,value);
  2099. mac.buflen:=length(value);
  2100. getmem(mac.buftext,mac.buflen);
  2101. move(value[1],mac.buftext^,mac.buflen);
  2102. mac.defined:=true;
  2103. end;
  2104. procedure undef_system_macro(const name : string);
  2105. var
  2106. mac : tmacro;
  2107. s: string;
  2108. begin
  2109. if name = '' then
  2110. internalerror(2004121201);
  2111. s:= upper(name);
  2112. mac:=tmacro(search_macro(s));
  2113. if not assigned(mac) then
  2114. {If not found, then it's already undefined.}
  2115. else
  2116. begin
  2117. if mac.defined then
  2118. Message1(parser_c_macro_undefined,mac.name);
  2119. mac.defined:=false;
  2120. mac.is_compiler_var:=false;
  2121. { delete old definition }
  2122. if assigned(mac.buftext) then
  2123. begin
  2124. freemem(mac.buftext,mac.buflen);
  2125. mac.buftext:=nil;
  2126. end;
  2127. end;
  2128. end;
  2129. {$ifdef UNITALIASES}
  2130. {****************************************************************************
  2131. TUNIT_ALIAS
  2132. ****************************************************************************}
  2133. constructor tunit_alias.create(const n:string);
  2134. var
  2135. i : longint;
  2136. begin
  2137. i:=pos('=',n);
  2138. if i=0 then
  2139. fail;
  2140. inherited createname(Copy(n,1,i-1));
  2141. newname:=stringdup(Copy(n,i+1,255));
  2142. end;
  2143. destructor tunit_alias.destroy;
  2144. begin
  2145. stringdispose(newname);
  2146. inherited destroy;
  2147. end;
  2148. procedure addunitalias(const n:string);
  2149. begin
  2150. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2151. end;
  2152. function getunitalias(const n:string):string;
  2153. var
  2154. p : punit_alias;
  2155. begin
  2156. p:=punit_alias(unitaliases^.search(Upper(n)));
  2157. if assigned(p) then
  2158. getunitalias:=punit_alias(p).newname^
  2159. else
  2160. getunitalias:=n;
  2161. end;
  2162. {$endif UNITALIASES}
  2163. {****************************************************************************
  2164. Symtable Stack
  2165. ****************************************************************************}
  2166. {$ifdef DEBUG}
  2167. procedure test_symtablestack;
  2168. var
  2169. p : tsymtable;
  2170. i : longint;
  2171. begin
  2172. p:=symtablestack;
  2173. i:=0;
  2174. while assigned(p) do
  2175. begin
  2176. inc(i);
  2177. p:=p.next;
  2178. if i>500 then
  2179. Message(sym_f_internal_error_in_symtablestack);
  2180. end;
  2181. end;
  2182. procedure list_symtablestack;
  2183. var
  2184. p : tsymtable;
  2185. i : longint;
  2186. begin
  2187. p:=symtablestack;
  2188. i:=0;
  2189. while assigned(p) do
  2190. begin
  2191. inc(i);
  2192. writeln(i,' ',p.name^);
  2193. p:=p.next;
  2194. if i>500 then
  2195. Message(sym_f_internal_error_in_symtablestack);
  2196. end;
  2197. end;
  2198. {$endif DEBUG}
  2199. {****************************************************************************
  2200. Init/Done Symtable
  2201. ****************************************************************************}
  2202. procedure InitSymtable;
  2203. begin
  2204. { Reset symbolstack }
  2205. registerdef:=false;
  2206. symtablestack:=nil;
  2207. macrosymtablestack:=nil;
  2208. systemunit:=nil;
  2209. {$ifdef GDB}
  2210. globaltypecount:=1;
  2211. pglobaltypecount:=@globaltypecount;
  2212. {$endif GDB}
  2213. { create error syms and def }
  2214. generrorsym:=terrorsym.create;
  2215. generrortype.setdef(terrordef.create);
  2216. {$ifdef UNITALIASES}
  2217. { unit aliases }
  2218. unitaliases:=tdictionary.create;
  2219. {$endif}
  2220. initialmacrosymtable:= tmacrosymtable.create(false);
  2221. macrosymtablestack:= initialmacrosymtable;
  2222. { set some global vars to nil, might be important for the ide }
  2223. class_tobject:=nil;
  2224. interface_iunknown:=nil;
  2225. rec_tguid:=nil;
  2226. dupnr:=0;
  2227. end;
  2228. procedure DoneSymtable;
  2229. begin
  2230. generrorsym.free;
  2231. generrortype.def.free;
  2232. {$ifdef UNITALIASES}
  2233. unitaliases.free;
  2234. {$endif}
  2235. initialmacrosymtable.Free;
  2236. end;
  2237. end.