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