symtable.pas 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  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 b_needs_init_final then
  826. exit;
  827. case tsym(p).typ of
  828. varsym :
  829. begin
  830. if not(is_class(tvarsym(p).vartype.def)) and
  831. tstoreddef(tvarsym(p).vartype.def).needs_inittable then
  832. b_needs_init_final:=true;
  833. end;
  834. typedconstsym :
  835. begin
  836. if ttypedconstsym(p).is_writable and
  837. tstoreddef(ttypedconstsym(p).typedconsttype.def).needs_inittable then
  838. b_needs_init_final:=true;
  839. end;
  840. end;
  841. end;
  842. { returns true, if p contains data which needs init/final code }
  843. function tstoredsymtable.needs_init_final : boolean;
  844. begin
  845. b_needs_init_final:=false;
  846. foreach({$ifdef FPCPROCVAR}@{$endif}_needs_init_final,nil);
  847. needs_init_final:=b_needs_init_final;
  848. end;
  849. {****************************************************************************
  850. TAbstractRecordSymtable
  851. ****************************************************************************}
  852. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  853. var
  854. storesymtable : tsymtable;
  855. begin
  856. storesymtable:=aktrecordsymtable;
  857. aktrecordsymtable:=self;
  858. inherited ppuload(ppufile);
  859. aktrecordsymtable:=storesymtable;
  860. end;
  861. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  862. var
  863. oldtyp : byte;
  864. storesymtable : tsymtable;
  865. begin
  866. storesymtable:=aktrecordsymtable;
  867. aktrecordsymtable:=self;
  868. oldtyp:=ppufile.entrytyp;
  869. ppufile.entrytyp:=subentryid;
  870. inherited ppuwrite(ppufile);
  871. ppufile.entrytyp:=oldtyp;
  872. aktrecordsymtable:=storesymtable;
  873. end;
  874. procedure tabstractrecordsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  875. var
  876. storesymtable : tsymtable;
  877. begin
  878. storesymtable:=aktrecordsymtable;
  879. aktrecordsymtable:=self;
  880. inherited load_references(ppufile,locals);
  881. aktrecordsymtable:=storesymtable;
  882. end;
  883. procedure tabstractrecordsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  884. var
  885. storesymtable : tsymtable;
  886. begin
  887. storesymtable:=aktrecordsymtable;
  888. aktrecordsymtable:=self;
  889. inherited write_references(ppufile,locals);
  890. aktrecordsymtable:=storesymtable;
  891. end;
  892. procedure tabstractrecordsymtable.insertvardata(sym : tsymentry);
  893. var
  894. l,varalign : longint;
  895. vardef : tdef;
  896. begin
  897. if sym.typ<>varsym then
  898. internalerror(200208251);
  899. l:=tvarsym(sym).getvaluesize;
  900. vardef:=tvarsym(sym).vartype.def;
  901. { this symbol can't be loaded to a register }
  902. exclude(tvarsym(sym).varoptions,vo_regable);
  903. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  904. { get the alignment size }
  905. if (aktalignment.recordalignmax=-1) then
  906. begin
  907. varalign:=vardef.alignment;
  908. if (varalign>4) and
  909. ((varalign mod 4)<>0) and
  910. (vardef.deftype=arraydef) then
  911. Message1(sym_w_wrong_C_pack,vardef.typename);
  912. if varalign=0 then
  913. varalign:=l;
  914. if (dataalignment<aktalignment.maxCrecordalign) then
  915. begin
  916. if (varalign>16) and (dataalignment<32) then
  917. dataalignment:=32
  918. else if (varalign>12) and (dataalignment<16) then
  919. dataalignment:=16
  920. { 12 is needed for long double }
  921. else if (varalign>8) and (dataalignment<12) then
  922. dataalignment:=12
  923. else if (varalign>4) and (dataalignment<8) then
  924. dataalignment:=8
  925. else if (varalign>2) and (dataalignment<4) then
  926. dataalignment:=4
  927. else if (varalign>1) and (dataalignment<2) then
  928. dataalignment:=2;
  929. end;
  930. dataalignment:=min(dataalignment,aktalignment.maxCrecordalign);
  931. end
  932. else
  933. varalign:=vardef.alignment;
  934. if varalign=0 then
  935. varalign:=size_2_align(l);
  936. varalign:=used_align(varalign,aktalignment.recordalignmin,dataalignment);
  937. tvarsym(sym).address:=align(datasize,varalign);
  938. datasize:=tvarsym(sym).address+l;
  939. end;
  940. {****************************************************************************
  941. TRecordSymtable
  942. ****************************************************************************}
  943. constructor trecordsymtable.create;
  944. begin
  945. inherited create('');
  946. symtabletype:=recordsymtable;
  947. end;
  948. { this procedure is reserved for inserting case variant into
  949. a record symtable }
  950. { the offset is the location of the start of the variant
  951. and datasize and dataalignment corresponds to
  952. the complete size (see code in pdecl unit) PM }
  953. procedure trecordsymtable.insert_in(tsymt : tsymtable;offset : longint);
  954. var
  955. ps,nps : tvarsym;
  956. pd,npd : tdef;
  957. storesize,storealign : longint;
  958. begin
  959. storesize:=tsymt.datasize;
  960. storealign:=tsymt.dataalignment;
  961. tsymt.datasize:=offset;
  962. ps:=tvarsym(symindex.first);
  963. while assigned(ps) do
  964. begin
  965. nps:=tvarsym(ps.indexnext);
  966. { remove from current symtable }
  967. symindex.deleteindex(ps);
  968. ps.left:=nil;
  969. ps.right:=nil;
  970. { add to symt }
  971. ps.owner:=tsymt;
  972. tsymt.datasize:=ps.address+offset;
  973. tsymt.symindex.insert(ps);
  974. tsymt.symsearch.insert(ps);
  975. { update address }
  976. ps.address:=tsymt.datasize;
  977. { next }
  978. ps:=nps;
  979. end;
  980. pd:=tdef(defindex.first);
  981. while assigned(pd) do
  982. begin
  983. npd:=tdef(pd.indexnext);
  984. defindex.deleteindex(pd);
  985. pd.left:=nil;
  986. pd.right:=nil;
  987. tsymt.registerdef(pd);
  988. pd:=npd;
  989. end;
  990. tsymt.datasize:=storesize;
  991. tsymt.dataalignment:=storealign;
  992. end;
  993. {****************************************************************************
  994. TObjectSymtable
  995. ****************************************************************************}
  996. constructor tobjectsymtable.create(const n:string);
  997. begin
  998. inherited create(n);
  999. symtabletype:=objectsymtable;
  1000. end;
  1001. procedure tobjectsymtable.insert(sym:tsymentry);
  1002. var
  1003. hsym : tsym;
  1004. begin
  1005. { check for duplicate field id in inherited classes }
  1006. if (sym.typ=varsym) and
  1007. assigned(defowner) and
  1008. (
  1009. not(m_delphi in aktmodeswitches) or
  1010. is_object(tdef(defowner))
  1011. ) then
  1012. begin
  1013. { but private ids can be reused }
  1014. hsym:=search_class_member(tobjectdef(defowner),sym.name);
  1015. if assigned(hsym) and
  1016. tstoredsym(hsym).is_visible_for_object(tobjectdef(defowner)) then
  1017. begin
  1018. DuplicateSym(hsym);
  1019. exit;
  1020. end;
  1021. end;
  1022. inherited insert(sym);
  1023. end;
  1024. {****************************************************************************
  1025. TAbstractLocalSymtable
  1026. ****************************************************************************}
  1027. procedure tabstractlocalsymtable.ppuload(ppufile:tcompilerppufile);
  1028. var
  1029. storesymtable : tsymtable;
  1030. begin
  1031. storesymtable:=aktlocalsymtable;
  1032. aktlocalsymtable:=self;
  1033. inherited ppuload(ppufile);
  1034. aktlocalsymtable:=storesymtable;
  1035. end;
  1036. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1037. var
  1038. oldtyp : byte;
  1039. storesymtable : tsymtable;
  1040. begin
  1041. storesymtable:=aktlocalsymtable;
  1042. aktlocalsymtable:=self;
  1043. oldtyp:=ppufile.entrytyp;
  1044. ppufile.entrytyp:=subentryid;
  1045. { write definitions }
  1046. writedefs(ppufile);
  1047. { write symbols }
  1048. writesyms(ppufile);
  1049. ppufile.entrytyp:=oldtyp;
  1050. aktlocalsymtable:=storesymtable;
  1051. end;
  1052. procedure tabstractlocalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1053. var
  1054. storesymtable : tsymtable;
  1055. begin
  1056. storesymtable:=aktlocalsymtable;
  1057. aktlocalsymtable:=self;
  1058. inherited load_references(ppufile,locals);
  1059. aktlocalsymtable:=storesymtable;
  1060. end;
  1061. procedure tabstractlocalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1062. var
  1063. storesymtable : tsymtable;
  1064. begin
  1065. storesymtable:=aktlocalsymtable;
  1066. aktlocalsymtable:=self;
  1067. inherited write_references(ppufile,locals);
  1068. aktlocalsymtable:=storesymtable;
  1069. end;
  1070. {****************************************************************************
  1071. TLocalSymtable
  1072. ****************************************************************************}
  1073. constructor tlocalsymtable.create;
  1074. begin
  1075. inherited create('');
  1076. symtabletype:=localsymtable;
  1077. end;
  1078. procedure tlocalsymtable.insert(sym:tsymentry);
  1079. var
  1080. hsym : tsym;
  1081. begin
  1082. if assigned(next) then
  1083. begin
  1084. if (next.symtabletype=parasymtable) then
  1085. begin
  1086. hsym:=tsym(next.search(sym.name));
  1087. if assigned(hsym) then
  1088. begin
  1089. { a parameter and the function can have the same
  1090. name in TP and Delphi, but RESULT not }
  1091. if (m_duplicate_names in aktmodeswitches) and
  1092. (sym.typ=funcretsym) and
  1093. not((m_result in aktmodeswitches) and
  1094. (sym.name='RESULT')) then
  1095. sym.name:='hidden'+sym.name
  1096. else
  1097. begin
  1098. DuplicateSym(hsym);
  1099. exit;
  1100. end;
  1101. end;
  1102. end;
  1103. { check for duplicate id in local symtable of methods }
  1104. if assigned(next.next) and
  1105. { funcretsym is allowed !! }
  1106. (sym.typ <> funcretsym) and
  1107. (next.next.symtabletype=objectsymtable) then
  1108. begin
  1109. hsym:=search_class_member(tobjectdef(next.next.defowner),sym.name);
  1110. if assigned(hsym) and
  1111. { private ids can be reused }
  1112. (not(sp_private in hsym.symoptions) or
  1113. (hsym.owner.defowner.owner.symtabletype<>globalsymtable)) then
  1114. begin
  1115. { delphi allows to reuse the names in a class, but not
  1116. in object (tp7 compatible) }
  1117. if not((m_delphi in aktmodeswitches) and
  1118. is_class(tdef(next.next.defowner))) then
  1119. begin
  1120. DuplicateSym(hsym);
  1121. exit;
  1122. end;
  1123. end;
  1124. end;
  1125. end;
  1126. inherited insert(sym);
  1127. end;
  1128. procedure tlocalsymtable.insertvardata(sym : tsymentry);
  1129. var
  1130. l,varalign : longint;
  1131. begin
  1132. if not(sym.typ in [varsym,funcretsym]) then
  1133. internalerror(200208255);
  1134. case sym.typ of
  1135. varsym :
  1136. begin
  1137. tvarsym(sym).varstate:=vs_declared;
  1138. l:=tvarsym(sym).getvaluesize;
  1139. varalign:=size_2_align(l);
  1140. varalign:=used_align(varalign,aktalignment.localalignmin,aktalignment.localalignmax);
  1141. {$ifdef powerpc}
  1142. { on the powerpc, the local variables are accessed with a positiv offset }
  1143. tvarsym(sym).address:=align(datasize,varalign);
  1144. datasize:=tvarsym(sym).address+l;
  1145. {$else powerpc}
  1146. tvarsym(sym).address:=align(datasize+l,varalign);
  1147. datasize:=tvarsym(sym).address;
  1148. {$endif powerpc}
  1149. end;
  1150. funcretsym :
  1151. begin
  1152. { if retoffset is already set then reuse it, this is needed
  1153. when inserting the result variable }
  1154. if procinfo.return_offset<>0 then
  1155. tfuncretsym(sym).address:=procinfo.return_offset
  1156. else
  1157. begin
  1158. { allocate space in local if ret in register }
  1159. if paramanager.ret_in_reg(tfuncretsym(sym).returntype.def) then
  1160. begin
  1161. l:=tfuncretsym(sym).returntype.def.size;
  1162. varalign:=size_2_align(l);
  1163. varalign:=used_align(varalign,aktalignment.localalignmin,dataalignment);
  1164. tfuncretsym(sym).address:=align(datasize+l,varalign);
  1165. datasize:=tfuncretsym(sym).address;
  1166. procinfo.return_offset:=-tfuncretsym(sym).address;
  1167. end;
  1168. end;
  1169. end;
  1170. end;
  1171. end;
  1172. procedure tlocalsymtable.insertconstdata(sym : tsymentry);
  1173. { this does not affect the local stack space, since all
  1174. typed constansts and initialized variables are always
  1175. put in the .data / .rodata section
  1176. }
  1177. var
  1178. storefilepos : tfileposinfo;
  1179. curconstsegment : taasmoutput;
  1180. l : longint;
  1181. begin
  1182. { Note: this is the same code as tabstractunitsymtable.insertconstdata }
  1183. if sym.typ<>typedconstsym then
  1184. internalerror(200208254);
  1185. storefilepos:=aktfilepos;
  1186. aktfilepos:=tsym(sym).fileinfo;
  1187. if ttypedconstsym(sym).is_writable then
  1188. curconstsegment:=datasegment
  1189. else
  1190. curconstsegment:=consts;
  1191. l:=ttypedconstsym(sym).getsize;
  1192. { insert cut for smartlinking or alignment }
  1193. if (cs_create_smart in aktmoduleswitches) then
  1194. curconstSegment.concat(Tai_cut.Create);
  1195. curconstSegment.concat(Tai_align.create(const_align(l)));
  1196. {$ifdef GDB}
  1197. if cs_debuginfo in aktmoduleswitches then
  1198. concatstabto(curconstsegment);
  1199. {$endif GDB}
  1200. if (cs_create_smart in aktmoduleswitches) or
  1201. DLLSource then
  1202. begin
  1203. curconstSegment.concat(Tai_symbol.Createdataname_global(
  1204. ttypedconstsym(sym).mangledname,l));
  1205. end
  1206. else
  1207. begin
  1208. curconstSegment.concat(Tai_symbol.Createdataname(
  1209. ttypedconstsym(sym).mangledname,l));
  1210. end;
  1211. aktfilepos:=storefilepos;
  1212. end;
  1213. {****************************************************************************
  1214. TParaSymtable
  1215. ****************************************************************************}
  1216. constructor tparasymtable.create;
  1217. begin
  1218. inherited create('');
  1219. symtabletype:=parasymtable;
  1220. dataalignment:=aktalignment.paraalign;
  1221. end;
  1222. procedure tparasymtable.insert(sym:tsymentry);
  1223. var
  1224. hsym : tsym;
  1225. begin
  1226. { check for duplicate id in para symtable of methods }
  1227. if assigned(procinfo) and
  1228. assigned(procinfo._class) and
  1229. { but not in nested procedures !}
  1230. (not(assigned(procinfo.parent)) or
  1231. (assigned(procinfo.parent) and
  1232. not(assigned(procinfo.parent._class)))
  1233. ) and
  1234. { funcretsym is allowed !! }
  1235. (sym.typ<>funcretsym) then
  1236. begin
  1237. hsym:=search_class_member(procinfo._class,sym.name);
  1238. { private ids can be reused }
  1239. if assigned(hsym) and
  1240. tstoredsym(hsym).is_visible_for_object(procinfo._class) then
  1241. begin
  1242. { delphi allows to reuse the names in a class, but not
  1243. in object (tp7 compatible) }
  1244. if not((m_delphi in aktmodeswitches) and
  1245. is_class_or_interface(procinfo._class)) then
  1246. begin
  1247. DuplicateSym(hsym);
  1248. exit;
  1249. end;
  1250. end;
  1251. end;
  1252. inherited insert(sym);
  1253. end;
  1254. procedure tparasymtable.insertvardata(sym : tsymentry);
  1255. var
  1256. l,varalign : longint;
  1257. is_cdecl : boolean;
  1258. begin
  1259. if sym.typ<>varsym then
  1260. internalerror(200208253);
  1261. { retrieve cdecl status }
  1262. if defowner.deftype<>procdef then
  1263. internalerror(200208256);
  1264. is_cdecl:=(tprocdef(defowner).proccalloption in [pocall_cdecl,pocall_cppdecl]);
  1265. { here we need the size of a push instead of the
  1266. size of the data }
  1267. l:=tvarsym(sym).getpushsize(is_cdecl);
  1268. varalign:=size_2_align(l);
  1269. tvarsym(sym).varstate:=vs_assigned;
  1270. { we need the new datasize already aligned so we can't
  1271. use the align_address here }
  1272. tvarsym(sym).address:=datasize;
  1273. varalign:=used_align(varalign,dataalignment,dataalignment);
  1274. datasize:=align(tvarsym(sym).address+l,varalign);
  1275. end;
  1276. {****************************************************************************
  1277. TAbstractUnitSymtable
  1278. ****************************************************************************}
  1279. constructor tabstractunitsymtable.create(const n : string);
  1280. begin
  1281. inherited create(n);
  1282. symsearch.usehash;
  1283. {$ifdef GDB}
  1284. { reset GDB things }
  1285. prev_dbx_counter := dbx_counter;
  1286. dbx_counter := nil;
  1287. is_stab_written:=false;
  1288. dbx_count := -1;
  1289. {$endif GDB}
  1290. end;
  1291. procedure tabstractunitsymtable.insertvardata(sym : tsymentry);
  1292. var
  1293. l,varalign : longint;
  1294. storefilepos : tfileposinfo;
  1295. begin
  1296. if sym.typ<>varsym then
  1297. internalerror(200208252);
  1298. storefilepos:=aktfilepos;
  1299. aktfilepos:=tsym(sym).fileinfo;
  1300. l:=tvarsym(sym).getvaluesize;
  1301. if (vo_is_thread_var in tvarsym(sym).varoptions) then
  1302. inc(l,pointer_size);
  1303. varalign:=var_align(l);
  1304. tvarsym(sym).address:=align(datasize,varalign);
  1305. { insert cut for smartlinking or alignment }
  1306. if (cs_create_smart in aktmoduleswitches) then
  1307. bssSegment.concat(Tai_cut.Create);
  1308. bssSegment.concat(Tai_align.create(varalign));
  1309. datasize:=tvarsym(sym).address+l;
  1310. {$ifdef GDB}
  1311. if cs_debuginfo in aktmoduleswitches then
  1312. concatstabto(bsssegment);
  1313. {$endif GDB}
  1314. if (symtabletype=globalsymtable) or
  1315. (cs_create_smart in aktmoduleswitches) or
  1316. DLLSource or
  1317. (vo_is_exported in tvarsym(sym).varoptions) or
  1318. (vo_is_C_var in tvarsym(sym).varoptions) then
  1319. bssSegment.concat(Tai_datablock.Create_global(tvarsym(sym).mangledname,l))
  1320. else
  1321. bssSegment.concat(Tai_datablock.Create(tvarsym(sym).mangledname,l));
  1322. aktfilepos:=storefilepos;
  1323. end;
  1324. procedure tabstractunitsymtable.insertconstdata(sym : tsymentry);
  1325. var
  1326. storefilepos : tfileposinfo;
  1327. curconstsegment : taasmoutput;
  1328. l : longint;
  1329. begin
  1330. if sym.typ<>typedconstsym then
  1331. internalerror(200208254);
  1332. storefilepos:=aktfilepos;
  1333. aktfilepos:=tsym(sym).fileinfo;
  1334. if ttypedconstsym(sym).is_writable then
  1335. curconstsegment:=datasegment
  1336. else
  1337. curconstsegment:=consts;
  1338. l:=ttypedconstsym(sym).getsize;
  1339. { insert cut for smartlinking or alignment }
  1340. if (cs_create_smart in aktmoduleswitches) then
  1341. curconstSegment.concat(Tai_cut.Create);
  1342. curconstSegment.concat(Tai_align.create(const_align(l)));
  1343. {$ifdef GDB}
  1344. if cs_debuginfo in aktmoduleswitches then
  1345. concatstabto(curconstsegment);
  1346. {$endif GDB}
  1347. if (symtabletype=globalsymtable) or
  1348. (cs_create_smart in aktmoduleswitches) or
  1349. DLLSource then
  1350. begin
  1351. curconstSegment.concat(Tai_symbol.Createdataname_global(
  1352. ttypedconstsym(sym).mangledname,l));
  1353. end
  1354. else
  1355. begin
  1356. curconstSegment.concat(Tai_symbol.Createdataname(
  1357. ttypedconstsym(sym).mangledname,l));
  1358. end;
  1359. aktfilepos:=storefilepos;
  1360. end;
  1361. {$ifdef GDB}
  1362. procedure tabstractunitsymtable.concattypestabto(asmlist : taasmoutput);
  1363. var prev_dbx_count : plongint;
  1364. begin
  1365. if is_stab_written then
  1366. exit;
  1367. if not assigned(name) then
  1368. name := stringdup('Main_program');
  1369. if (symtabletype = globalsymtable) and
  1370. (current_module.globalsymtable<>self) then
  1371. begin
  1372. unitid:=current_module.unitcount;
  1373. inc(current_module.unitcount);
  1374. end;
  1375. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(unitid))));
  1376. if cs_gdb_dbx in aktglobalswitches then
  1377. begin
  1378. if dbx_count_ok then
  1379. begin
  1380. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1381. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count))));
  1382. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1383. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1384. exit;
  1385. end
  1386. else if (current_module.globalsymtable<>self) then
  1387. begin
  1388. prev_dbx_count := dbx_counter;
  1389. dbx_counter := nil;
  1390. do_count_dbx:=false;
  1391. if (symtabletype = globalsymtable) and (unitid<>0) then
  1392. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1393. dbx_counter := @dbx_count;
  1394. dbx_count:=0;
  1395. do_count_dbx:=assigned(dbx_counter);
  1396. end;
  1397. end;
  1398. foreach({$ifdef FPCPROCVAR}@{$endif}concattypestab,asmlist);
  1399. if cs_gdb_dbx in aktglobalswitches then
  1400. begin
  1401. if (current_module.globalsymtable<>self) then
  1402. begin
  1403. dbx_counter := prev_dbx_count;
  1404. do_count_dbx:=false;
  1405. asmList.concat(tai_comment.Create(strpnew('End unit '+name^
  1406. +' has index '+tostr(unitid))));
  1407. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1408. +tostr(N_EINCL)+',0,0,0')));
  1409. do_count_dbx:=assigned(dbx_counter);
  1410. dbx_count_ok := {true}false;
  1411. end;
  1412. end;
  1413. is_stab_written:=true;
  1414. end;
  1415. {$endif GDB}
  1416. {****************************************************************************
  1417. TStaticSymtable
  1418. ****************************************************************************}
  1419. constructor tstaticsymtable.create(const n : string);
  1420. begin
  1421. inherited create(n);
  1422. symtabletype:=staticsymtable;
  1423. end;
  1424. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1425. begin
  1426. aktstaticsymtable:=self;
  1427. next:=symtablestack;
  1428. symtablestack:=self;
  1429. inherited ppuload(ppufile);
  1430. { now we can deref the syms and defs }
  1431. deref;
  1432. { restore symtablestack }
  1433. symtablestack:=next;
  1434. end;
  1435. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1436. begin
  1437. aktstaticsymtable:=self;
  1438. inherited ppuwrite(ppufile);
  1439. end;
  1440. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1441. begin
  1442. aktstaticsymtable:=self;
  1443. inherited load_references(ppufile,locals);
  1444. end;
  1445. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1446. begin
  1447. aktstaticsymtable:=self;
  1448. inherited write_references(ppufile,locals);
  1449. end;
  1450. procedure tstaticsymtable.insert(sym:tsymentry);
  1451. var
  1452. hsym : tsym;
  1453. begin
  1454. { also check the global symtable }
  1455. if assigned(next) and
  1456. (next.unitid=0) then
  1457. begin
  1458. hsym:=tsym(next.search(sym.name));
  1459. if assigned(hsym) then
  1460. begin
  1461. DuplicateSym(hsym);
  1462. exit;
  1463. end;
  1464. end;
  1465. inherited insert(sym);
  1466. end;
  1467. procedure tstaticsymtable.insertvardata(sym : tsymentry);
  1468. begin
  1469. inherited insertvardata(sym);
  1470. { enable unitialized warning for local symbols }
  1471. if sym.typ=varsym then
  1472. tvarsym(sym).varstate:=vs_declared;
  1473. end;
  1474. {****************************************************************************
  1475. TGlobalSymtable
  1476. ****************************************************************************}
  1477. constructor tglobalsymtable.create(const n : string);
  1478. begin
  1479. inherited create(n);
  1480. symtabletype:=globalsymtable;
  1481. unitid:=0;
  1482. unitsym:=nil;
  1483. {$ifdef GDB}
  1484. if cs_gdb_dbx in aktglobalswitches then
  1485. begin
  1486. dbx_count := 0;
  1487. unittypecount:=1;
  1488. pglobaltypecount := @unittypecount;
  1489. {unitid:=current_module.unitcount;}
  1490. debugList.concat(tai_comment.Create(strpnew('Global '+name^+' has index '+tostr(unitid))));
  1491. debugList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1492. {inc(current_module.unitcount);}
  1493. dbx_count_ok:=false;
  1494. dbx_counter:=@dbx_count;
  1495. do_count_dbx:=true;
  1496. end;
  1497. {$endif GDB}
  1498. end;
  1499. destructor tglobalsymtable.destroy;
  1500. var
  1501. pus : tunitsym;
  1502. begin
  1503. pus:=unitsym;
  1504. while assigned(pus) do
  1505. begin
  1506. unitsym:=pus.prevsym;
  1507. pus.prevsym:=nil;
  1508. pus.unitsymtable:=nil;
  1509. pus:=unitsym;
  1510. end;
  1511. inherited destroy;
  1512. end;
  1513. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1514. begin
  1515. {$ifdef GDB}
  1516. if cs_gdb_dbx in aktglobalswitches then
  1517. begin
  1518. UnitTypeCount:=1;
  1519. PglobalTypeCount:=@UnitTypeCount;
  1520. end;
  1521. {$endif GDB}
  1522. symtablelevel:=0;
  1523. {$ifndef NEWMAP}
  1524. current_module.map^[0]:=self;
  1525. {$else NEWMAP}
  1526. current_module.globalsymtable:=self;
  1527. {$endif NEWMAP}
  1528. next:=symtablestack;
  1529. symtablestack:=self;
  1530. inherited ppuload(ppufile);
  1531. { now we can deref the syms and defs }
  1532. deref;
  1533. { restore symtablestack }
  1534. symtablestack:=next;
  1535. {$ifdef NEWMAP}
  1536. { necessary for dependencies }
  1537. current_module.globalsymtable:=nil;
  1538. {$endif NEWMAP}
  1539. end;
  1540. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1541. begin
  1542. { write the symtable entries }
  1543. inherited ppuwrite(ppufile);
  1544. { write dbx count }
  1545. {$ifdef GDB}
  1546. if cs_gdb_dbx in aktglobalswitches then
  1547. begin
  1548. {$IfDef EXTDEBUG}
  1549. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1550. {$ENDIF EXTDEBUG}
  1551. ppufile.do_crc:=false;
  1552. ppufile.putlongint(dbx_count);
  1553. ppufile.writeentry(ibdbxcount);
  1554. ppufile.do_crc:=true;
  1555. end;
  1556. {$endif GDB}
  1557. end;
  1558. procedure tglobalsymtable.insert(sym:tsymentry);
  1559. var
  1560. hsym : tsym;
  1561. begin
  1562. { also check the global symtable }
  1563. if assigned(next) and
  1564. (next.unitid=0) then
  1565. begin
  1566. hsym:=tsym(next.search(sym.name));
  1567. if assigned(hsym) then
  1568. begin
  1569. DuplicateSym(hsym);
  1570. exit;
  1571. end;
  1572. end;
  1573. hsym:=tsym(search(sym.name));
  1574. if assigned(hsym) then
  1575. begin
  1576. { Delphi you can have a symbol with the same name as the
  1577. unit, the unit can then not be accessed anymore using
  1578. <unit>.<id>, so we can hide the symbol }
  1579. if (m_duplicate_names in aktmodeswitches) and
  1580. (hsym.typ=symconst.unitsym) then
  1581. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1582. else
  1583. begin
  1584. DuplicateSym(hsym);
  1585. exit;
  1586. end;
  1587. end;
  1588. inherited insert(sym);
  1589. end;
  1590. procedure tglobalsymtable.insertvardata(sym : tsymentry);
  1591. begin
  1592. inherited insertvardata(sym);
  1593. { this symbol can't be loaded to a register }
  1594. if sym.typ=varsym then
  1595. begin
  1596. exclude(tvarsym(sym).varoptions,vo_regable);
  1597. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  1598. end;
  1599. end;
  1600. {$ifdef GDB}
  1601. function tglobalsymtable.getnewtypecount : word;
  1602. begin
  1603. if not (cs_gdb_dbx in aktglobalswitches) then
  1604. getnewtypecount:=inherited getnewtypecount
  1605. else
  1606. begin
  1607. getnewtypecount:=unittypecount;
  1608. inc(unittypecount);
  1609. end;
  1610. end;
  1611. {$endif}
  1612. {****************************************************************************
  1613. TWITHSYMTABLE
  1614. ****************************************************************************}
  1615. constructor twithsymtable.create(aowner:tdef;asymsearch:TDictionary);
  1616. begin
  1617. inherited create('');
  1618. symtabletype:=withsymtable;
  1619. direct_with:=false;
  1620. withnode:=nil;
  1621. withrefnode:=nil;
  1622. { we don't need the symsearch }
  1623. symsearch.free;
  1624. { set the defaults }
  1625. symsearch:=asymsearch;
  1626. defowner:=aowner;
  1627. end;
  1628. destructor twithsymtable.destroy;
  1629. begin
  1630. symsearch:=nil;
  1631. inherited destroy;
  1632. end;
  1633. procedure twithsymtable.clear;
  1634. begin
  1635. { remove no entry from a withsymtable as it is only a pointer to the
  1636. recorddef or objectdef symtable }
  1637. end;
  1638. {****************************************************************************
  1639. TSTT_ExceptionSymtable
  1640. ****************************************************************************}
  1641. constructor tstt_exceptsymtable.create;
  1642. begin
  1643. inherited create('');
  1644. symtabletype:=stt_exceptsymtable;
  1645. end;
  1646. {*****************************************************************************
  1647. Helper Routines
  1648. *****************************************************************************}
  1649. function findunitsymtable(st:tsymtable):tsymtable;
  1650. begin
  1651. findunitsymtable:=nil;
  1652. repeat
  1653. if not assigned(st) then
  1654. internalerror(5566561);
  1655. case st.symtabletype of
  1656. localsymtable,
  1657. parasymtable,
  1658. staticsymtable :
  1659. exit;
  1660. globalsymtable :
  1661. begin
  1662. findunitsymtable:=st;
  1663. exit;
  1664. end;
  1665. objectsymtable :
  1666. st:=st.defowner.owner;
  1667. recordsymtable :
  1668. begin
  1669. { don't continue when the current
  1670. symtable is used for variant records }
  1671. if trecorddef(st.defowner).isunion then
  1672. begin
  1673. findunitsymtable:=nil;
  1674. exit;
  1675. end
  1676. else
  1677. st:=st.defowner.owner;
  1678. end;
  1679. else
  1680. internalerror(5566562);
  1681. end;
  1682. until false;
  1683. end;
  1684. procedure duplicatesym(sym:tsym);
  1685. var
  1686. st : tsymtable;
  1687. begin
  1688. Message1(sym_e_duplicate_id,sym.realname);
  1689. st:=findunitsymtable(sym.owner);
  1690. with sym.fileinfo do
  1691. begin
  1692. if assigned(st) and (st.unitid<>0) then
  1693. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  1694. else
  1695. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line));
  1696. end;
  1697. end;
  1698. {*****************************************************************************
  1699. Search
  1700. *****************************************************************************}
  1701. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1702. var
  1703. speedvalue : cardinal;
  1704. begin
  1705. speedvalue:=getspeedvalue(s);
  1706. srsymtable:=symtablestack;
  1707. while assigned(srsymtable) do
  1708. begin
  1709. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1710. if assigned(srsym) and
  1711. tstoredsym(srsym).is_visible_for_proc(aktprocdef) then
  1712. begin
  1713. searchsym:=true;
  1714. exit;
  1715. end
  1716. else
  1717. srsymtable:=srsymtable.next;
  1718. end;
  1719. searchsym:=false;
  1720. end;
  1721. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1722. var
  1723. srsym : tsym;
  1724. begin
  1725. { the caller have to take care if srsym=nil }
  1726. if assigned(p) then
  1727. begin
  1728. srsym:=tsym(p.search(s));
  1729. if assigned(srsym) then
  1730. begin
  1731. searchsymonlyin:=srsym;
  1732. exit;
  1733. end;
  1734. { also check in the local symtbale if it exists }
  1735. if (p=tsymtable(current_module.globalsymtable)) then
  1736. begin
  1737. srsym:=tsym(current_module.localsymtable.search(s));
  1738. if assigned(srsym) then
  1739. begin
  1740. searchsymonlyin:=srsym;
  1741. exit;
  1742. end;
  1743. end
  1744. end;
  1745. searchsymonlyin:=nil;
  1746. end;
  1747. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1748. var
  1749. speedvalue : cardinal;
  1750. topclassh : tobjectdef;
  1751. sym : tsym;
  1752. begin
  1753. speedvalue:=getspeedvalue(s);
  1754. { when the class passed is defined in this unit we
  1755. need to use the scope of that class. This is a trick
  1756. that can be used to access protected members in other
  1757. units. At least kylix supports it this way (PFV) }
  1758. if (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1759. (classh.owner.unitid=0) then
  1760. topclassh:=classh
  1761. else
  1762. topclassh:=nil;
  1763. sym:=nil;
  1764. while assigned(classh) do
  1765. begin
  1766. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1767. if assigned(sym) then
  1768. begin
  1769. if assigned(topclassh) then
  1770. begin
  1771. if tstoredsym(sym).is_visible_for_object(topclassh) then
  1772. break;
  1773. end
  1774. else
  1775. begin
  1776. if tstoredsym(sym).is_visible_for_proc(aktprocdef) then
  1777. break;
  1778. end;
  1779. end;
  1780. classh:=classh.childof;
  1781. end;
  1782. searchsym_in_class:=sym;
  1783. end;
  1784. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1785. var
  1786. symowner: tsymtable;
  1787. begin
  1788. if not(cs_compilesystem in aktmoduleswitches) then
  1789. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1790. else
  1791. searchsym(s,tsym(srsym),symowner);
  1792. searchsystype :=
  1793. assigned(srsym) and
  1794. (srsym.typ = typesym);
  1795. end;
  1796. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  1797. begin
  1798. if not(cs_compilesystem in aktmoduleswitches) then
  1799. begin
  1800. srsym := tvarsym(searchsymonlyin(systemunit,s));
  1801. symowner := systemunit;
  1802. end
  1803. else
  1804. searchsym(s,tsym(srsym),symowner);
  1805. searchsysvar :=
  1806. assigned(srsym) and
  1807. (srsym.typ = varsym);
  1808. end;
  1809. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1810. { searches n in symtable of pd and all anchestors }
  1811. var
  1812. speedvalue : cardinal;
  1813. srsym : tsym;
  1814. begin
  1815. speedvalue:=getspeedvalue(s);
  1816. while assigned(pd) do
  1817. begin
  1818. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1819. if assigned(srsym) then
  1820. begin
  1821. search_class_member:=srsym;
  1822. exit;
  1823. end;
  1824. pd:=pd.childof;
  1825. end;
  1826. search_class_member:=nil;
  1827. end;
  1828. {*****************************************************************************
  1829. Definition Helpers
  1830. *****************************************************************************}
  1831. procedure globaldef(const s : string;var t:ttype);
  1832. var st : string;
  1833. symt : tsymtable;
  1834. srsym : tsym;
  1835. srsymtable : tsymtable;
  1836. begin
  1837. srsym := nil;
  1838. if pos('.',s) > 0 then
  1839. begin
  1840. st := copy(s,1,pos('.',s)-1);
  1841. searchsym(st,srsym,srsymtable);
  1842. st := copy(s,pos('.',s)+1,255);
  1843. if assigned(srsym) then
  1844. begin
  1845. if srsym.typ = unitsym then
  1846. begin
  1847. symt := tunitsym(srsym).unitsymtable;
  1848. srsym := tsym(symt.search(st));
  1849. end else srsym := nil;
  1850. end;
  1851. end else st := s;
  1852. if srsym = nil then
  1853. searchsym(st,srsym,srsymtable);
  1854. if srsym = nil then
  1855. srsym:=searchsymonlyin(systemunit,st);
  1856. if (not assigned(srsym)) or
  1857. (srsym.typ<>typesym) then
  1858. begin
  1859. Message(type_e_type_id_expected);
  1860. t:=generrortype;
  1861. exit;
  1862. end;
  1863. t := ttypesym(srsym).restype;
  1864. end;
  1865. {****************************************************************************
  1866. Object Helpers
  1867. ****************************************************************************}
  1868. var
  1869. _defaultprop : tpropertysym;
  1870. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  1871. begin
  1872. if (tsym(p).typ=propertysym) and
  1873. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  1874. _defaultprop:=tpropertysym(p);
  1875. end;
  1876. function search_default_property(pd : tobjectdef) : tpropertysym;
  1877. { returns the default property of a class, searches also anchestors }
  1878. begin
  1879. _defaultprop:=nil;
  1880. while assigned(pd) do
  1881. begin
  1882. pd.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}tstoredsymtable(pd.symtable).testfordefaultproperty,nil);
  1883. if assigned(_defaultprop) then
  1884. break;
  1885. pd:=pd.childof;
  1886. end;
  1887. search_default_property:=_defaultprop;
  1888. end;
  1889. {$ifdef UNITALIASES}
  1890. {****************************************************************************
  1891. TUNIT_ALIAS
  1892. ****************************************************************************}
  1893. constructor tunit_alias.create(const n:string);
  1894. var
  1895. i : longint;
  1896. begin
  1897. i:=pos('=',n);
  1898. if i=0 then
  1899. fail;
  1900. inherited createname(Copy(n,1,i-1));
  1901. newname:=stringdup(Copy(n,i+1,255));
  1902. end;
  1903. destructor tunit_alias.destroy;
  1904. begin
  1905. stringdispose(newname);
  1906. inherited destroy;
  1907. end;
  1908. procedure addunitalias(const n:string);
  1909. begin
  1910. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1911. end;
  1912. function getunitalias(const n:string):string;
  1913. var
  1914. p : punit_alias;
  1915. begin
  1916. p:=punit_alias(unitaliases^.search(Upper(n)));
  1917. if assigned(p) then
  1918. getunitalias:=punit_alias(p).newname^
  1919. else
  1920. getunitalias:=n;
  1921. end;
  1922. {$endif UNITALIASES}
  1923. {****************************************************************************
  1924. Symtable Stack
  1925. ****************************************************************************}
  1926. procedure dellexlevel;
  1927. var
  1928. p : tsymtable;
  1929. begin
  1930. p:=symtablestack;
  1931. symtablestack:=p.next;
  1932. { symbol tables of unit interfaces are never disposed }
  1933. { this is handle by the unit unitm }
  1934. if not(p.symtabletype in [globalsymtable,stt_exceptsymtable]) then
  1935. p.free;
  1936. end;
  1937. procedure RestoreUnitSyms;
  1938. var
  1939. p : tsymtable;
  1940. begin
  1941. p:=symtablestack;
  1942. while assigned(p) do
  1943. begin
  1944. if (p.symtabletype=globalsymtable) and
  1945. assigned(tglobalsymtable(p).unitsym) and
  1946. ((tglobalsymtable(p).unitsym.owner=current_module.globalsymtable) or
  1947. (tglobalsymtable(p).unitsym.owner=current_module.localsymtable)) then
  1948. tglobalsymtable(p).unitsym.restoreunitsym;
  1949. p:=p.next;
  1950. end;
  1951. end;
  1952. {$ifdef DEBUG}
  1953. procedure test_symtablestack;
  1954. var
  1955. p : tsymtable;
  1956. i : longint;
  1957. begin
  1958. p:=symtablestack;
  1959. i:=0;
  1960. while assigned(p) do
  1961. begin
  1962. inc(i);
  1963. p:=p.next;
  1964. if i>500 then
  1965. Message(sym_f_internal_error_in_symtablestack);
  1966. end;
  1967. end;
  1968. procedure list_symtablestack;
  1969. var
  1970. p : tsymtable;
  1971. i : longint;
  1972. begin
  1973. p:=symtablestack;
  1974. i:=0;
  1975. while assigned(p) do
  1976. begin
  1977. inc(i);
  1978. writeln(i,' ',p.name^);
  1979. p:=p.next;
  1980. if i>500 then
  1981. Message(sym_f_internal_error_in_symtablestack);
  1982. end;
  1983. end;
  1984. {$endif DEBUG}
  1985. {****************************************************************************
  1986. Init/Done Symtable
  1987. ****************************************************************************}
  1988. procedure InitSymtable;
  1989. var
  1990. token : ttoken;
  1991. begin
  1992. { Reset symbolstack }
  1993. registerdef:=false;
  1994. read_member:=false;
  1995. symtablestack:=nil;
  1996. systemunit:=nil;
  1997. {$ifdef GDB}
  1998. firstglobaldef:=nil;
  1999. lastglobaldef:=nil;
  2000. globaltypecount:=1;
  2001. pglobaltypecount:=@globaltypecount;
  2002. {$endif GDB}
  2003. { create error syms and def }
  2004. generrorsym:=terrorsym.create;
  2005. generrortype.setdef(terrordef.create);
  2006. {$ifdef UNITALIASES}
  2007. { unit aliases }
  2008. unitaliases:=tdictionary.create;
  2009. {$endif}
  2010. for token:=first_overloaded to last_overloaded do
  2011. overloaded_operators[token]:=nil;
  2012. end;
  2013. procedure DoneSymtable;
  2014. begin
  2015. generrorsym.free;
  2016. generrortype.def.free;
  2017. {$ifdef UNITALIASES}
  2018. unitaliases.free;
  2019. {$endif}
  2020. end;
  2021. end.
  2022. {
  2023. $Log$
  2024. Revision 1.76 2002-11-09 15:29:28 carl
  2025. + bss / constant alignment fixes
  2026. * avoid incrementing address/datasize in local symtable for const's
  2027. Revision 1.75 2002/10/14 19:44:43 peter
  2028. * threadvars need 4 bytes extra for storing the threadvar index
  2029. Revision 1.74 2002/10/06 19:41:31 peter
  2030. * Add finalization of typed consts
  2031. * Finalization of globals in the main program
  2032. Revision 1.73 2002/10/05 12:43:29 carl
  2033. * fixes for Delphi 6 compilation
  2034. (warning : Some features do not work under Delphi)
  2035. Revision 1.72 2002/09/09 19:41:46 peter
  2036. * real fix internalerror for dup ids in union sym
  2037. Revision 1.71 2002/09/09 17:34:16 peter
  2038. * tdicationary.replace added to replace and item in a dictionary. This
  2039. is only allowed for the same name
  2040. * varsyms are inserted in symtable before the types are parsed. This
  2041. fixes the long standing "var longint : longint" bug
  2042. - consume_idlist and idstringlist removed. The loops are inserted
  2043. at the callers place and uses the symtable for duplicate id checking
  2044. Revision 1.70 2002/09/05 19:29:45 peter
  2045. * memdebug enhancements
  2046. Revision 1.69 2002/08/25 19:25:21 peter
  2047. * sym.insert_in_data removed
  2048. * symtable.insertvardata/insertconstdata added
  2049. * removed insert_in_data call from symtable.insert, it needs to be
  2050. called separatly. This allows to deref the address calculation
  2051. * procedures now calculate the parast addresses after the procedure
  2052. directives are parsed. This fixes the cdecl parast problem
  2053. * push_addr_param has an extra argument that specifies if cdecl is used
  2054. or not
  2055. Revision 1.68 2002/08/18 20:06:27 peter
  2056. * inlining is now also allowed in interface
  2057. * renamed write/load to ppuwrite/ppuload
  2058. * tnode storing in ppu
  2059. * nld,ncon,nbas are already updated for storing in ppu
  2060. Revision 1.67 2002/08/17 09:23:43 florian
  2061. * first part of procinfo rewrite
  2062. Revision 1.66 2002/08/11 13:24:15 peter
  2063. * saving of asmsymbols in ppu supported
  2064. * asmsymbollist global is removed and moved into a new class
  2065. tasmlibrarydata that will hold the info of a .a file which
  2066. corresponds with a single module. Added librarydata to tmodule
  2067. to keep the library info stored for the module. In the future the
  2068. objectfiles will also be stored to the tasmlibrarydata class
  2069. * all getlabel/newasmsymbol and friends are moved to the new class
  2070. Revision 1.65 2002/07/23 09:51:27 daniel
  2071. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2072. are worth comitting.
  2073. Revision 1.64 2002/07/16 15:34:21 florian
  2074. * exit is now a syssym instead of a keyword
  2075. Revision 1.63 2002/07/15 19:44:53 florian
  2076. * fixed crash with default parameters and stdcall calling convention
  2077. Revision 1.62 2002/07/01 18:46:28 peter
  2078. * internal linker
  2079. * reorganized aasm layer
  2080. Revision 1.61 2002/05/18 13:34:19 peter
  2081. * readded missing revisions
  2082. Revision 1.60 2002/05/16 19:46:45 carl
  2083. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2084. + try to fix temp allocation (still in ifdef)
  2085. + generic constructor calls
  2086. + start of tassembler / tmodulebase class cleanup
  2087. Revision 1.58 2002/05/12 16:53:15 peter
  2088. * moved entry and exitcode to ncgutil and cgobj
  2089. * foreach gets extra argument for passing local data to the
  2090. iterator function
  2091. * -CR checks also class typecasts at runtime by changing them
  2092. into as
  2093. * fixed compiler to cycle with the -CR option
  2094. * fixed stabs with elf writer, finally the global variables can
  2095. be watched
  2096. * removed a lot of routines from cga unit and replaced them by
  2097. calls to cgobj
  2098. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2099. u32bit then the other is typecasted also to u32bit without giving
  2100. a rangecheck warning/error.
  2101. * fixed pascal calling method with reversing also the high tree in
  2102. the parast, detected by tcalcst3 test
  2103. Revision 1.57 2002/04/04 19:06:05 peter
  2104. * removed unused units
  2105. * use tlocation.size in cg.a_*loc*() routines
  2106. Revision 1.56 2002/03/04 19:10:11 peter
  2107. * removed compiler warnings
  2108. Revision 1.55 2002/02/03 09:30:07 peter
  2109. * more fixes for protected handling
  2110. Revision 1.54 2002/01/29 21:30:25 peter
  2111. * allow also dup id in delphi mode in interfaces
  2112. Revision 1.53 2002/01/29 19:46:00 peter
  2113. * fixed recordsymtable.insert_in() for inserting variant record fields
  2114. to not used symtable.insert() because that also updates alignmentinfo
  2115. which was already set
  2116. Revision 1.52 2002/01/24 18:25:50 peter
  2117. * implicit result variable generation for assembler routines
  2118. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2119. }