symtable.pas 78 KB

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