symtable.pas 78 KB

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