symtable.pas 76 KB

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