symtable.pas 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. {$ifdef TP}
  19. {$N+,E+,F+}
  20. {$endif}
  21. unit symtable;
  22. interface
  23. uses
  24. {$ifdef TP}
  25. {$ifndef Delphi}
  26. objects,
  27. {$endif Delphi}
  28. {$endif}
  29. strings,cobjects,
  30. globtype,globals,tokens,systems,verbose,
  31. symconst,
  32. aasm
  33. ,cpubase
  34. ,cpuinfo
  35. {$ifdef GDB}
  36. ,gdb
  37. {$endif}
  38. ;
  39. {************************************************
  40. Some internal constants
  41. ************************************************}
  42. const
  43. hasharraysize = 256;
  44. {$ifdef TP}
  45. indexgrowsize = 16;
  46. {$else}
  47. indexgrowsize = 64;
  48. {$endif}
  49. {************************************************
  50. Needed forward pointers
  51. ************************************************}
  52. type
  53. { needed for owner (table) of symbol }
  54. psymtable = ^tsymtable;
  55. punitsymtable = ^tunitsymtable;
  56. { needed for names by the definitions }
  57. ptypesym = ^ttypesym;
  58. penumsym = ^tenumsym;
  59. pprocsym = ^tprocsym;
  60. pref = ^tref;
  61. tref = object
  62. nextref : pref;
  63. posinfo : tfileposinfo;
  64. moduleindex : word;
  65. is_written : boolean;
  66. constructor init(ref:pref;pos:pfileposinfo);
  67. destructor done; virtual;
  68. end;
  69. { Deref entry options }
  70. tdereftype = (derefnil,derefaktrecordindex,derefaktstaticindex,
  71. derefunit,derefrecord,derefindex,
  72. dereflocal,derefpara,derefaktlocal);
  73. pderef = ^tderef;
  74. tderef = object
  75. dereftype : tdereftype;
  76. index : word;
  77. next : pderef;
  78. constructor init(typ:tdereftype;i:word);
  79. destructor done;
  80. end;
  81. psymtableentry = ^tsymtableentry;
  82. tsymtableentry = object(tnamedindexobject)
  83. owner : psymtable;
  84. end;
  85. {************************************************
  86. TDef
  87. ************************************************}
  88. {$i symdefh.inc}
  89. {************************************************
  90. TSym
  91. ************************************************}
  92. {$i symsymh.inc}
  93. {************************************************
  94. TSymtable
  95. ************************************************}
  96. tsymtabletype = (invalidsymtable,withsymtable,staticsymtable,
  97. globalsymtable,unitsymtable,
  98. objectsymtable,recordsymtable,
  99. macrosymtable,localsymtable,
  100. parasymtable,inlineparasymtable,
  101. inlinelocalsymtable,stt_exceptsymtable,
  102. { only used for PPU reading of static part
  103. of a unit }
  104. staticppusymtable);
  105. tcallback = procedure(p : psym);
  106. tsearchhasharray = array[0..hasharraysize-1] of psym;
  107. psearchhasharray = ^tsearchhasharray;
  108. tsymtable = object
  109. symtabletype : tsymtabletype;
  110. { each symtable gets a number }
  111. unitid : word{integer give range check errors PM};
  112. name : pstring;
  113. datasize : longint;
  114. dataalignment : longint;
  115. symindex,
  116. defindex : pindexarray;
  117. symsearch : pdictionary;
  118. next : psymtable;
  119. defowner : pdef; { for records and objects }
  120. { alignment used in this symtable }
  121. alignment : longint;
  122. { only used for parameter symtable to determine the offset relative }
  123. { to the frame pointer and for local inline }
  124. address_fixup : longint;
  125. { this saves all definition to allow a proper clean up }
  126. { separate lexlevel from symtable type }
  127. symtablelevel : byte;
  128. constructor init(t : tsymtabletype);
  129. destructor done;virtual;
  130. { access }
  131. function getdefnr(l : longint) : pdef;
  132. function getsymnr(l : longint) : psym;
  133. { load/write }
  134. constructor load;
  135. procedure write;
  136. constructor loadas(typ : tsymtabletype);
  137. procedure writeas;
  138. procedure loaddefs;
  139. procedure loadsyms;
  140. procedure writedefs;
  141. procedure writesyms;
  142. procedure deref;
  143. procedure clear;
  144. function rename(const olds,news : stringid):psym;
  145. procedure foreach(proc2call : tnamedindexcallback);
  146. function insert(sym : psym):psym;
  147. function search(const s : stringid) : psym;
  148. function speedsearch(const s : stringid;speedvalue : longint) : psym;
  149. procedure registerdef(p : pdef);
  150. procedure allsymbolsused;
  151. procedure allunitsused;
  152. procedure check_forwards;
  153. procedure checklabels;
  154. { change alignment for args only parasymtable }
  155. procedure set_alignment(_alignment : byte);
  156. { find arg having offset only parasymtable }
  157. function find_at_offset(l : longint) : pvarsym;
  158. {$ifdef CHAINPROCSYMS}
  159. procedure chainprocsyms;
  160. {$endif CHAINPROCSYMS}
  161. procedure load_browser;
  162. procedure write_browser;
  163. {$ifdef BrowserLog}
  164. procedure writebrowserlog;
  165. {$endif BrowserLog}
  166. {$ifdef GDB}
  167. procedure concatstabto(asmlist : paasmoutput);virtual;
  168. {$endif GDB}
  169. function getnewtypecount : word; virtual;
  170. end;
  171. tunitsymtable = object(tsymtable)
  172. unittypecount : word;
  173. unitsym : punitsym;
  174. {$ifdef GDB}
  175. dbx_count : longint;
  176. prev_dbx_counter : plongint;
  177. dbx_count_ok : boolean;
  178. is_stab_written : boolean;
  179. {$endif GDB}
  180. constructor init(t : tsymtabletype;const n : string);
  181. constructor loadasunit;
  182. destructor done;virtual;
  183. procedure writeasunit;
  184. {$ifdef GDB}
  185. procedure concattypestabto(asmlist : paasmoutput);
  186. {$endif GDB}
  187. procedure load_symtable_refs;
  188. function getnewtypecount : word; virtual;
  189. end;
  190. pwithsymtable = ^twithsymtable;
  191. twithsymtable = object(tsymtable)
  192. { used for withsymtable for allowing constructors }
  193. direct_with : boolean;
  194. { in fact it is a ptree }
  195. withnode : pointer;
  196. { ptree to load of direct with var }
  197. { already usable before firstwith
  198. needed for firstpass of function parameters PM }
  199. withrefnode : pointer;
  200. constructor init;
  201. destructor done;virtual;
  202. end;
  203. {****************************************************************************
  204. Var / Consts
  205. ****************************************************************************}
  206. const
  207. systemunit : punitsymtable = nil; { pointer to the system unit }
  208. current_object_option : tsymoptions = [sp_public];
  209. var
  210. { for STAB debugging }
  211. globaltypecount : word;
  212. pglobaltypecount : pword;
  213. registerdef : boolean; { true, when defs should be registered }
  214. defaultsymtablestack, { symtablestack after default units
  215. have been loaded }
  216. symtablestack : psymtable; { linked list of symtables }
  217. srsym : psym; { result of the last search }
  218. srsymtable : psymtable;
  219. lastsrsym : psym; { last sym found in statement }
  220. lastsrsymtable : psymtable;
  221. lastsymknown : boolean;
  222. constsymtable : psymtable; { symtable were the constants can be
  223. inserted }
  224. voidpointerdef : ppointerdef; { pointer for Void-Pointerdef }
  225. charpointerdef : ppointerdef; { pointer for Char-Pointerdef }
  226. voidfarpointerdef : ppointerdef;
  227. cformaldef : pformaldef; { unique formal definition }
  228. voiddef : porddef; { Pointer to Void (procedure) }
  229. cchardef : porddef; { Pointer to Char }
  230. booldef : porddef; { pointer to boolean type }
  231. u8bitdef : porddef; { Pointer to 8-Bit unsigned }
  232. u16bitdef : porddef; { Pointer to 16-Bit unsigned }
  233. u32bitdef : porddef; { Pointer to 32-Bit unsigned }
  234. s32bitdef : porddef; { Pointer to 32-Bit signed }
  235. cu64bitdef : porddef; { pointer to 64 bit unsigned def }
  236. cs64bitdef : porddef; { pointer to 64 bit signed def, }
  237. { calculated by the int unit on i386 }
  238. s32floatdef : pfloatdef; { pointer for realconstn }
  239. s64floatdef : pfloatdef; { pointer for realconstn }
  240. s80floatdef : pfloatdef; { pointer to type of temp. floats }
  241. s32fixeddef : pfloatdef; { pointer to type of temp. fixed }
  242. cshortstringdef : pstringdef; { pointer to type of short string const }
  243. clongstringdef : pstringdef; { pointer to type of long string const }
  244. cansistringdef : pstringdef; { pointer to type of ansi string const }
  245. cwidestringdef : pstringdef; { pointer to type of wide string const }
  246. openshortstringdef : pstringdef; { pointer to type of an open shortstring,
  247. needed for readln() }
  248. openchararraydef : parraydef; { pointer to type of an open array of char,
  249. needed for readln() }
  250. cfiledef : pfiledef; { get the same definition for all file }
  251. { uses for stabs }
  252. firstglobaldef, { linked list of all globals defs }
  253. lastglobaldef : pdef; { used to reset stabs/ranges }
  254. class_tobject : pobjectdef; { pointer to the anchestor of all }
  255. { clases }
  256. pvmtdef : ppointerdef; { type of classrefs }
  257. aktprocsym : pprocsym; { pointer to the symbol for the
  258. currently be parsed procedure }
  259. aktcallprocsym : pprocsym; { pointer to the symbol for the
  260. currently be called procedure,
  261. only set/unset in firstcall }
  262. aktvarsym : pvarsym; { pointer to the symbol for the
  263. currently read var, only used
  264. for variable directives }
  265. procprefix : string; { eindeutige Namen bei geschachtel- }
  266. { ten Unterprogrammen erzeugen }
  267. lexlevel : longint; { level of code }
  268. { 1 for main procedure }
  269. { 2 for normal function or proc }
  270. { higher for locals }
  271. const
  272. main_program_level = 1;
  273. unit_init_level = 1;
  274. normal_function_level = 2;
  275. in_loading : boolean = false;
  276. {$ifdef i386}
  277. bestrealdef : ^pfloatdef = @s80floatdef;
  278. {$endif}
  279. {$ifdef m68k}
  280. bestrealdef : ^pfloatdef = @s64floatdef;
  281. {$endif}
  282. {$ifdef alpha}
  283. bestrealdef : ^pfloatdef = @s64floatdef;
  284. {$endif}
  285. {$ifdef powerpc}
  286. bestrealdef : ^pfloatdef = @s64floatdef;
  287. {$endif}
  288. var
  289. macros : psymtable; { pointer for die Symboltabelle mit }
  290. { Makros }
  291. read_member : boolean; { true, wenn Members aus einer PPU- }
  292. { Datei gelesen werden, d.h. ein }
  293. { varsym seine Adresse einlesen soll }
  294. generrorsym : psym; { Jokersymbol, wenn das richtige }
  295. { Symbol nicht gefunden wird }
  296. generrordef : pdef; { Jokersymbol for eine fehlerhafte }
  297. { Typdefinition }
  298. aktobjectdef : pobjectdef; { used for private functions check !! }
  299. const
  300. { last operator which can be overloaded }
  301. first_overloaded = _PLUS;
  302. last_overloaded = _ASSIGNMENT;
  303. var
  304. overloaded_operators : array[first_overloaded..last_overloaded] of pprocsym;
  305. { unequal is not equal}
  306. const
  307. overloaded_names : array [first_overloaded..last_overloaded] of string[16] =
  308. ('plus','minus','star','slash','equal',
  309. 'greater','lower','greater_or_equal',
  310. 'lower_or_equal',
  311. 'sym_diff','starstar',
  312. 'as','is','in','or',
  313. 'and','div','mod','shl','shr','xor',
  314. 'assign');
  315. {$ifdef UNITALIASES}
  316. type
  317. punit_alias = ^tunit_alias;
  318. tunit_alias = object(tnamedindexobject)
  319. newname : pstring;
  320. constructor init(const n:string);
  321. destructor done;virtual;
  322. end;
  323. var
  324. unitaliases : pdictionary;
  325. procedure addunitalias(const n:string);
  326. function getunitalias(const n:string):string;
  327. {$endif UNITALIASES}
  328. {****************************************************************************
  329. Functions
  330. ****************************************************************************}
  331. {*** Misc ***}
  332. function globaldef(const s : string) : pdef;
  333. function findunitsymtable(st:psymtable):psymtable;
  334. procedure duplicatesym(sym:psym);
  335. {*** Search ***}
  336. function search_a_symtable(const symbol:string;symtabletype:tsymtabletype):Psym;
  337. procedure getsym(const s : stringid;notfounderror : boolean);
  338. procedure getsymonlyin(p : psymtable;const s : stringid);
  339. {*** PPU Write/Loading ***}
  340. procedure writeunitas(const s : string;unittable : punitsymtable;only_crc : boolean);
  341. procedure closecurrentppu;
  342. procedure numberunits;
  343. procedure load_interface;
  344. {*** GDB ***}
  345. {$ifdef GDB}
  346. function typeglobalnumber(const s : string) : string;
  347. {$endif}
  348. {*** Definition ***}
  349. procedure reset_global_defs;
  350. {*** Object Helpers ***}
  351. function search_class_member(pd : pobjectdef;const n : string) : psym;
  352. function search_default_property(pd : pobjectdef) : ppropertysym;
  353. {*** Macro ***}
  354. procedure def_macro(const s : string);
  355. procedure set_macro(const s : string;value : string);
  356. {*** symtable stack ***}
  357. procedure dellexlevel;
  358. {$ifdef DEBUG}
  359. procedure test_symtablestack;
  360. procedure list_symtablestack;
  361. {$endif DEBUG}
  362. {*** Init / Done ***}
  363. procedure InitSymtable;
  364. procedure DoneSymtable;
  365. implementation
  366. uses
  367. version,
  368. types,ppu,
  369. gendef,files
  370. ,tree
  371. ,cresstr
  372. {$ifdef newcg}
  373. ,cgbase
  374. {$else}
  375. ,hcodegen
  376. {$endif}
  377. {$ifdef BrowserLog}
  378. ,browlog
  379. {$endif BrowserLog}
  380. {$ifdef alignreg}
  381. ,cpuasm
  382. {$endif alignreg}
  383. ;
  384. var
  385. aktrecordsymtable : psymtable; { current record read from ppu symtable }
  386. aktstaticsymtable : psymtable; { current static for local ppu symtable }
  387. aktlocalsymtable : psymtable; { current proc local for local ppu symtable }
  388. {$ifdef GDB}
  389. asmoutput : paasmoutput;
  390. {$endif GDB}
  391. {$ifdef TP}
  392. {$ifndef Delphi}
  393. {$ifndef dpmi}
  394. symbolstream : temsstream; { stream which is used to store some info }
  395. {$else}
  396. symbolstream : tmemorystream;
  397. {$endif}
  398. {$endif Delphi}
  399. {$endif}
  400. {to dispose the global symtable of a unit }
  401. const
  402. dispose_global : boolean = false;
  403. memsizeinc = 2048; { for long stabstrings }
  404. tagtypes : Set of tdeftype =
  405. [recorddef,enumdef,
  406. {$IfNDef GDBKnowsStrings}
  407. stringdef,
  408. {$EndIf not GDBKnowsStrings}
  409. {$IfNDef GDBKnowsFiles}
  410. filedef,
  411. {$EndIf not GDBKnowsFiles}
  412. objectdef];
  413. {*****************************************************************************
  414. Helper Routines
  415. *****************************************************************************}
  416. {$ifdef unused}
  417. function demangledparas(s : string) : string;
  418. var
  419. r : string;
  420. l : longint;
  421. begin
  422. demangledparas:='';
  423. r:=',';
  424. { delete leading $$'s }
  425. l:=pos('$$',s);
  426. while l<>0 do
  427. begin
  428. delete(s,1,l+1);
  429. l:=pos('$$',s);
  430. end;
  431. { delete leading _$'s }
  432. l:=pos('_$',s);
  433. while l<>0 do
  434. begin
  435. delete(s,1,l+1);
  436. l:=pos('_$',s);
  437. end;
  438. l:=pos('$',s);
  439. if l=0 then
  440. exit;
  441. delete(s,1,l);
  442. while s<>'' do
  443. begin
  444. l:=pos('$',s);
  445. if l=0 then
  446. l:=length(s)+1;
  447. r:=r+copy(s,1,l-1)+',';
  448. delete(s,1,l);
  449. end;
  450. delete(r,1,1);
  451. delete(r,length(r),1);
  452. demangledparas:=r;
  453. end;
  454. {$endif}
  455. procedure numberunits;
  456. var
  457. counter : longint;
  458. hp : pused_unit;
  459. hp1 : pmodule;
  460. begin
  461. { Reset all numbers to -1 }
  462. hp1:=pmodule(loaded_units.first);
  463. while assigned(hp1) do
  464. begin
  465. if assigned(hp1^.globalsymtable) then
  466. psymtable(hp1^.globalsymtable)^.unitid:=$ffff;
  467. hp1:=pmodule(hp1^.next);
  468. end;
  469. { Our own symtable gets unitid 0, for a program there is
  470. no globalsymtable }
  471. if assigned(current_module^.globalsymtable) then
  472. psymtable(current_module^.globalsymtable)^.unitid:=0;
  473. { number units }
  474. counter:=1;
  475. hp:=pused_unit(current_module^.used_units.first);
  476. while assigned(hp) do
  477. begin
  478. psymtable(hp^.u^.globalsymtable)^.unitid:=counter;
  479. inc(counter);
  480. hp:=pused_unit(hp^.next);
  481. end;
  482. end;
  483. function findunitsymtable(st:psymtable):psymtable;
  484. begin
  485. findunitsymtable:=nil;
  486. repeat
  487. if not assigned(st) then
  488. internalerror(5566561);
  489. case st^.symtabletype of
  490. localsymtable,
  491. parasymtable,
  492. staticsymtable :
  493. break;
  494. globalsymtable,
  495. unitsymtable :
  496. begin
  497. findunitsymtable:=st;
  498. break;
  499. end;
  500. objectsymtable,
  501. recordsymtable :
  502. st:=st^.defowner^.owner;
  503. else
  504. internalerror(5566562);
  505. end;
  506. until false;
  507. end;
  508. procedure setstring(var p : pchar;const s : string);
  509. begin
  510. {$ifndef Delphi}
  511. {$ifdef TP}
  512. if use_big then
  513. begin
  514. p:=pchar(symbolstream.getsize);
  515. symbolstream.seek(longint(p));
  516. symbolstream.writestr(@s);
  517. end
  518. else
  519. {$endif TP}
  520. {$endif Delphi}
  521. p:=strpnew(s);
  522. end;
  523. procedure duplicatesym(sym:psym);
  524. var
  525. st : psymtable;
  526. begin
  527. Message1(sym_e_duplicate_id,sym^.name);
  528. st:=findunitsymtable(sym^.owner);
  529. if assigned(st) then
  530. begin
  531. with sym^.fileinfo do
  532. begin
  533. if st^.unitid=0 then
  534. Message2(sym_h_duplicate_id_where,current_module^.sourcefiles^.get_file_name(fileindex),tostr(line))
  535. else
  536. Message2(sym_h_duplicate_id_where,'unit '+st^.name^,tostr(line));
  537. end;
  538. end;
  539. end;
  540. {****************************************************************************
  541. TRef
  542. ****************************************************************************}
  543. constructor tref.init(ref :pref;pos : pfileposinfo);
  544. begin
  545. nextref:=nil;
  546. if pos<>nil then
  547. posinfo:=pos^;
  548. if assigned(current_module) then
  549. moduleindex:=current_module^.unit_index;
  550. if assigned(ref) then
  551. ref^.nextref:=@self;
  552. is_written:=false;
  553. end;
  554. destructor tref.done;
  555. var
  556. inputfile : pinputfile;
  557. begin
  558. inputfile:=get_source_file(moduleindex,posinfo.fileindex);
  559. if inputfile<>nil then
  560. dec(inputfile^.ref_count);
  561. if assigned(nextref) then
  562. dispose(nextref,done);
  563. nextref:=nil;
  564. end;
  565. {****************************************************************************
  566. TDeref
  567. ****************************************************************************}
  568. constructor tderef.init(typ:tdereftype;i:word);
  569. begin
  570. dereftype:=typ;
  571. index:=i;
  572. next:=nil;
  573. end;
  574. destructor tderef.done;
  575. begin
  576. end;
  577. {*****************************************************************************
  578. PPU Reading Writing
  579. *****************************************************************************}
  580. {$I symppu.inc}
  581. {*****************************************************************************
  582. Definition Helpers
  583. *****************************************************************************}
  584. function globaldef(const s : string) : pdef;
  585. var st : string;
  586. symt : psymtable;
  587. begin
  588. srsym := nil;
  589. if pos('.',s) > 0 then
  590. begin
  591. st := copy(s,1,pos('.',s)-1);
  592. getsym(st,false);
  593. st := copy(s,pos('.',s)+1,255);
  594. if assigned(srsym) then
  595. begin
  596. if srsym^.typ = unitsym then
  597. begin
  598. symt := punitsym(srsym)^.unitsymtable;
  599. srsym := symt^.search(st);
  600. end else srsym := nil;
  601. end;
  602. end else st := s;
  603. if srsym = nil then getsym(st,false);
  604. if srsym = nil then
  605. getsymonlyin(systemunit,st);
  606. if srsym^.typ<>typesym then
  607. begin
  608. Message(type_e_type_id_expected);
  609. exit;
  610. end;
  611. globaldef := ptypesym(srsym)^.definition;
  612. end;
  613. {*****************************************************************************
  614. Symbol / Definition Resolving
  615. *****************************************************************************}
  616. procedure resolvederef(var p:pderef;var st:psymtable;var idx:word);
  617. var
  618. hp : pderef;
  619. pd : pdef;
  620. begin
  621. st:=nil;
  622. idx:=0;
  623. while assigned(p) do
  624. begin
  625. case p^.dereftype of
  626. derefaktrecordindex :
  627. begin
  628. st:=aktrecordsymtable;
  629. idx:=p^.index;
  630. end;
  631. derefaktstaticindex :
  632. begin
  633. st:=aktstaticsymtable;
  634. idx:=p^.index;
  635. end;
  636. derefaktlocal :
  637. begin
  638. st:=aktlocalsymtable;
  639. idx:=p^.index;
  640. end;
  641. derefunit :
  642. begin
  643. {$ifdef NEWMAP}
  644. st:=psymtable(current_module^.map^[p^.index]^.globalsymtable);
  645. {$else NEWMAP}
  646. st:=psymtable(current_module^.map^[p^.index]);
  647. {$endif NEWMAP}
  648. end;
  649. derefrecord :
  650. begin
  651. pd:=st^.getdefnr(p^.index);
  652. case pd^.deftype of
  653. recorddef :
  654. st:=precorddef(pd)^.symtable;
  655. objectdef :
  656. st:=pobjectdef(pd)^.symtable;
  657. else
  658. internalerror(556658);
  659. end;
  660. end;
  661. dereflocal :
  662. begin
  663. pd:=st^.getdefnr(p^.index);
  664. case pd^.deftype of
  665. procdef :
  666. st:=pprocdef(pd)^.localst;
  667. else
  668. internalerror(556658);
  669. end;
  670. end;
  671. derefpara :
  672. begin
  673. pd:=st^.getdefnr(p^.index);
  674. case pd^.deftype of
  675. procdef :
  676. st:=pprocdef(pd)^.parast;
  677. else
  678. internalerror(556658);
  679. end;
  680. end;
  681. derefindex :
  682. begin
  683. idx:=p^.index;
  684. end;
  685. else
  686. internalerror(556658);
  687. end;
  688. hp:=p;
  689. p:=p^.next;
  690. dispose(hp,done);
  691. end;
  692. end;
  693. procedure resolvedef(var def:pdef);
  694. var
  695. st : psymtable;
  696. idx : word;
  697. begin
  698. resolvederef(pderef(def),st,idx);
  699. if assigned(st) then
  700. def:=st^.getdefnr(idx)
  701. else
  702. def:=nil;
  703. end;
  704. procedure resolvesym(var sym:psym);
  705. var
  706. st : psymtable;
  707. idx : word;
  708. begin
  709. resolvederef(pderef(sym),st,idx);
  710. if assigned(st) then
  711. sym:=st^.getsymnr(idx)
  712. else
  713. sym:=nil;
  714. end;
  715. {*****************************************************************************
  716. Symbol Call Back Functions
  717. *****************************************************************************}
  718. procedure derefsym(p : pnamedindexobject);
  719. begin
  720. psym(p)^.deref;
  721. end;
  722. procedure derefsymsdelayed(p : pnamedindexobject);
  723. begin
  724. if psym(p)^.typ in [absolutesym,propertysym] then
  725. psym(p)^.deref;
  726. end;
  727. procedure check_forward(sym : pnamedindexobject);
  728. begin
  729. if psym(sym)^.typ=procsym then
  730. pprocsym(sym)^.check_forward
  731. { check also object method table }
  732. { we needn't to test the def list }
  733. { because each object has to have a type sym }
  734. else
  735. if (psym(sym)^.typ=typesym) and
  736. assigned(ptypesym(sym)^.definition) and
  737. (ptypesym(sym)^.definition^.deftype=objectdef) then
  738. pobjectdef(ptypesym(sym)^.definition)^.check_forwards;
  739. end;
  740. procedure labeldefined(p : pnamedindexobject);
  741. begin
  742. if (psym(p)^.typ=labelsym) and
  743. not(plabelsym(p)^.defined) then
  744. begin
  745. if plabelsym(p)^.used then
  746. Message1(sym_e_label_used_and_not_defined,p^.name)
  747. else
  748. Message1(sym_w_label_not_defined,p^.name);
  749. end;
  750. end;
  751. procedure unitsymbolused(p : pnamedindexobject);
  752. begin
  753. if (psym(p)^.typ=unitsym) and
  754. (punitsym(p)^.refs=0) then
  755. comment(V_info,'Unit '+p^.name+' is not used');
  756. end;
  757. procedure varsymbolused(p : pnamedindexobject);
  758. begin
  759. if (psym(p)^.typ=varsym) and
  760. ((psym(p)^.owner^.symtabletype in [parasymtable,localsymtable,staticsymtable])) then
  761. { unused symbol should be reported only if no }
  762. { error is reported }
  763. { if the symbol is in a register it is used }
  764. { also don't count the value parameters which have local copies }
  765. { also don't claim for high param of open parameters (PM) }
  766. if (pvarsym(p)^.refs=0) and
  767. (Errorcount=0) and
  768. (copy(p^.name,1,3)<>'val') and
  769. (copy(p^.name,1,4)<>'high') then
  770. begin
  771. if (psym(p)^.owner^.symtabletype=parasymtable) or pvarsym(p)^.islocalcopy then
  772. MessagePos1(psym(p)^.fileinfo,sym_h_para_identifier_not_used,p^.name)
  773. else
  774. MessagePos1(psym(p)^.fileinfo,sym_n_local_identifier_not_used,p^.name);
  775. end;
  776. end;
  777. {$ifdef GDB}
  778. procedure concatstab(p : pnamedindexobject);
  779. begin
  780. if psym(p)^.typ <> procsym then
  781. psym(p)^.concatstabto(asmoutput);
  782. end;
  783. procedure concattypestab(p : pnamedindexobject);
  784. begin
  785. if psym(p)^.typ = typesym then
  786. begin
  787. psym(p)^.isstabwritten:=false;
  788. psym(p)^.concatstabto(asmoutput);
  789. end;
  790. end;
  791. procedure forcestabto(asmlist : paasmoutput; pd : pdef);
  792. begin
  793. if not pd^.is_def_stab_written then
  794. begin
  795. if assigned(pd^.sym) then
  796. pd^.sym^.isusedinstab := true;
  797. pd^.concatstabto(asmlist);
  798. end;
  799. end;
  800. {$endif}
  801. {$ifdef CHAINPROCSYMS}
  802. procedure chainprocsym(p : psym);
  803. var
  804. storesymtablestack : psymtable;
  805. begin
  806. if p^.typ=procsym then
  807. begin
  808. storesymtablestack:=symtablestack;
  809. symtablestack:=p^.owner^.next;
  810. while assigned(symtablestack) do
  811. begin
  812. { search for same procsym in other units }
  813. getsym(p^.name,false);
  814. if assigned(srsym) and (srsym^.typ=procsym) then
  815. begin
  816. pprocsym(p)^.nextprocsym:=pprocsym(srsym);
  817. symtablestack:=storesymtablestack;
  818. exit;
  819. end
  820. else if srsym=nil then
  821. symtablestack:=nil
  822. else
  823. symtablestack:=srsymtable^.next;
  824. end;
  825. symtablestack:=storesymtablestack;
  826. end;
  827. end;
  828. {$endif}
  829. procedure write_refs(sym : pnamedindexobject);
  830. begin
  831. psym(sym)^.write_references;
  832. end;
  833. {$ifdef BrowserLog}
  834. procedure add_to_browserlog(sym : pnamedindexobject);
  835. begin
  836. psym(sym)^.add_to_browserlog;
  837. end;
  838. {$endif UseBrowser}
  839. {*****************************************************************************
  840. Search Symtables for Syms
  841. *****************************************************************************}
  842. procedure getsym(const s : stringid;notfounderror : boolean);
  843. var
  844. speedvalue : longint;
  845. begin
  846. speedvalue:=getspeedvalue(s);
  847. lastsrsym:=nil;
  848. srsymtable:=symtablestack;
  849. while assigned(srsymtable) do
  850. begin
  851. srsym:=srsymtable^.speedsearch(s,speedvalue);
  852. if assigned(srsym) then
  853. exit
  854. else
  855. srsymtable:=srsymtable^.next;
  856. end;
  857. if notfounderror then
  858. begin
  859. Message1(sym_e_id_not_found,s);
  860. srsym:=generrorsym;
  861. end
  862. else
  863. srsym:=nil;
  864. end;
  865. procedure getsymonlyin(p : psymtable;const s : stringid);
  866. begin
  867. { the caller have to take care if srsym=nil (FK) }
  868. srsym:=nil;
  869. if assigned(p) then
  870. begin
  871. srsymtable:=p;
  872. srsym:=srsymtable^.search(s);
  873. if assigned(srsym) then
  874. exit
  875. else
  876. begin
  877. if (punitsymtable(srsymtable)=punitsymtable(current_module^.globalsymtable)) then
  878. begin
  879. getsymonlyin(psymtable(current_module^.localsymtable),s);
  880. if assigned(srsym) then
  881. srsymtable:=psymtable(current_module^.localsymtable)
  882. else
  883. Message1(sym_e_id_not_found,s);
  884. end
  885. else
  886. Message1(sym_e_id_not_found,s);
  887. end;
  888. end;
  889. end;
  890. function search_a_symtable(const symbol:string;symtabletype:tsymtabletype):Psym;
  891. {Search for a symbol in a specified symbol table. Returns nil if
  892. the symtable is not found, and also if the symbol cannot be found
  893. in the desired symtable }
  894. var hsymtab:Psymtable;
  895. res:Psym;
  896. begin
  897. res:=nil;
  898. hsymtab:=symtablestack;
  899. while (hsymtab<>nil) and (hsymtab^.symtabletype<>symtabletype) do
  900. hsymtab:=hsymtab^.next;
  901. if hsymtab<>nil then
  902. {We found the desired symtable. Now check if the symbol we
  903. search for is defined in it }
  904. res:=hsymtab^.search(symbol);
  905. search_a_symtable:=res;
  906. end;
  907. {****************************************************************************
  908. TSYMTABLE
  909. ****************************************************************************}
  910. constructor tsymtable.init(t : tsymtabletype);
  911. begin
  912. symtabletype:=t;
  913. symtablelevel:=0;
  914. defowner:=nil;
  915. unitid:=0;
  916. next:=nil;
  917. name:=nil;
  918. address_fixup:=0;
  919. datasize:=0;
  920. dataalignment:=1;
  921. new(symindex,init(indexgrowsize));
  922. new(defindex,init(indexgrowsize));
  923. if symtabletype<>withsymtable then
  924. begin
  925. new(symsearch,init);
  926. symsearch^.noclear:=true;
  927. end
  928. else
  929. symsearch:=nil;
  930. alignment:=def_alignment;
  931. end;
  932. destructor tsymtable.done;
  933. begin
  934. stringdispose(name);
  935. dispose(symindex,done);
  936. dispose(defindex,done);
  937. { symsearch can already be disposed or set to nil for withsymtable }
  938. if assigned(symsearch) then
  939. begin
  940. dispose(symsearch,done);
  941. symsearch:=nil;
  942. end;
  943. end;
  944. constructor twithsymtable.init;
  945. begin
  946. inherited init(withsymtable);
  947. direct_with:=false;
  948. withnode:=nil;
  949. withrefnode:=nil;
  950. end;
  951. destructor twithsymtable.done;
  952. begin
  953. symsearch:=nil;
  954. inherited done;
  955. end;
  956. {***********************************************
  957. Helpers
  958. ***********************************************}
  959. function tsymtable.getnewtypecount : word;
  960. begin
  961. getnewtypecount:=pglobaltypecount^;
  962. inc(pglobaltypecount^);
  963. end;
  964. procedure tsymtable.registerdef(p : pdef);
  965. begin
  966. defindex^.insert(p);
  967. { set def owner and indexnb }
  968. p^.owner:=@self;
  969. end;
  970. procedure tsymtable.foreach(proc2call : tnamedindexcallback);
  971. begin
  972. symindex^.foreach(proc2call);
  973. end;
  974. {***********************************************
  975. LOAD / WRITE SYMTABLE FROM PPU
  976. ***********************************************}
  977. procedure tsymtable.loaddefs;
  978. var
  979. hp : pdef;
  980. b : byte;
  981. begin
  982. { load start of definition section, which holds the amount of defs }
  983. if current_ppu^.readentry<>ibstartdefs then
  984. Message(unit_f_ppu_read_error);
  985. current_ppu^.getlongint;
  986. { read definitions }
  987. repeat
  988. b:=current_ppu^.readentry;
  989. case b of
  990. ibpointerdef : hp:=new(ppointerdef,load);
  991. ibarraydef : hp:=new(parraydef,load);
  992. iborddef : hp:=new(porddef,load);
  993. ibfloatdef : hp:=new(pfloatdef,load);
  994. ibprocdef : hp:=new(pprocdef,load);
  995. ibshortstringdef : hp:=new(pstringdef,shortload);
  996. iblongstringdef : hp:=new(pstringdef,longload);
  997. ibansistringdef : hp:=new(pstringdef,ansiload);
  998. ibwidestringdef : hp:=new(pstringdef,wideload);
  999. ibrecorddef : hp:=new(precorddef,load);
  1000. ibobjectdef : hp:=new(pobjectdef,load);
  1001. ibenumdef : hp:=new(penumdef,load);
  1002. ibsetdef : hp:=new(psetdef,load);
  1003. ibprocvardef : hp:=new(pprocvardef,load);
  1004. ibfiledef : hp:=new(pfiledef,load);
  1005. ibclassrefdef : hp:=new(pclassrefdef,load);
  1006. ibformaldef : hp:=new(pformaldef,load);
  1007. ibenddefs : break;
  1008. ibend : Message(unit_f_ppu_read_error);
  1009. else
  1010. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1011. end;
  1012. hp^.owner:=@self;
  1013. defindex^.insert(hp);
  1014. until false;
  1015. end;
  1016. procedure tsymtable.loadsyms;
  1017. var
  1018. b : byte;
  1019. sym : psym;
  1020. begin
  1021. { load start of definition section, which holds the amount of defs }
  1022. if current_ppu^.readentry<>ibstartsyms then
  1023. Message(unit_f_ppu_read_error);
  1024. { skip amount of symbols, not used currently }
  1025. current_ppu^.getlongint;
  1026. { load datasize,dataalignment of this symboltable }
  1027. datasize:=current_ppu^.getlongint;
  1028. dataalignment:=current_ppu^.getlongint;
  1029. { now read the symbols }
  1030. repeat
  1031. b:=current_ppu^.readentry;
  1032. case b of
  1033. ibtypesym : sym:=new(ptypesym,load);
  1034. ibprocsym : sym:=new(pprocsym,load);
  1035. ibconstsym : sym:=new(pconstsym,load);
  1036. ibvarsym : sym:=new(pvarsym,load);
  1037. ibfuncretsym : sym:=new(pfuncretsym,load);
  1038. ibabsolutesym : sym:=new(pabsolutesym,load);
  1039. ibenumsym : sym:=new(penumsym,load);
  1040. ibtypedconstsym : sym:=new(ptypedconstsym,load);
  1041. ibpropertysym : sym:=new(ppropertysym,load);
  1042. ibunitsym : sym:=new(punitsym,load);
  1043. iblabelsym : sym:=new(plabelsym,load);
  1044. ibsyssym : sym:=new(psyssym,load);
  1045. ibendsyms : break;
  1046. ibend : Message(unit_f_ppu_read_error);
  1047. else
  1048. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1049. end;
  1050. sym^.owner:=@self;
  1051. symindex^.insert(sym);
  1052. symsearch^.insert(sym);
  1053. until false;
  1054. end;
  1055. procedure tsymtable.writedefs;
  1056. var
  1057. pd : pdef;
  1058. begin
  1059. { each definition get a number, write then the amount of defs to the
  1060. ibstartdef entry }
  1061. current_ppu^.putlongint(defindex^.count);
  1062. current_ppu^.writeentry(ibstartdefs);
  1063. { now write the definition }
  1064. pd:=pdef(defindex^.first);
  1065. while assigned(pd) do
  1066. begin
  1067. pd^.write;
  1068. pd:=pdef(pd^.next);
  1069. end;
  1070. { write end of definitions }
  1071. current_ppu^.writeentry(ibenddefs);
  1072. end;
  1073. procedure tsymtable.writesyms;
  1074. var
  1075. pd : psym;
  1076. begin
  1077. { each definition get a number, write then the amount of syms and the
  1078. datasize to the ibsymdef entry }
  1079. current_ppu^.putlongint(symindex^.count);
  1080. current_ppu^.putlongint(datasize);
  1081. current_ppu^.putlongint(dataalignment);
  1082. current_ppu^.writeentry(ibstartsyms);
  1083. { foreach is used to write all symbols }
  1084. pd:=psym(symindex^.first);
  1085. while assigned(pd) do
  1086. begin
  1087. pd^.write;
  1088. pd:=psym(pd^.next);
  1089. end;
  1090. { end of symbols }
  1091. current_ppu^.writeentry(ibendsyms);
  1092. end;
  1093. procedure tsymtable.deref;
  1094. var
  1095. hp : pdef;
  1096. hs : psym;
  1097. begin
  1098. hp:=pdef(defindex^.first);
  1099. while assigned(hp) do
  1100. begin
  1101. hp^.deref;
  1102. hp^.symderef;
  1103. hp:=pdef(hp^.next);
  1104. end;
  1105. hs:=psym(symindex^.first);
  1106. while assigned(hs) do
  1107. begin
  1108. hs^.deref;
  1109. hs:=psym(hs^.next);
  1110. end;
  1111. end;
  1112. constructor tsymtable.load;
  1113. var
  1114. st_loading : boolean;
  1115. begin
  1116. st_loading:=in_loading;
  1117. in_loading:=true;
  1118. {$ifndef NEWMAP}
  1119. current_module^.map^[0]:=@self;
  1120. {$else NEWMAP}
  1121. current_module^.globalsymtable:=@self;
  1122. {$endif NEWMAP}
  1123. symtabletype:=unitsymtable;
  1124. symtablelevel:=0;
  1125. { unused for units }
  1126. address_fixup:=0;
  1127. datasize:=0;
  1128. defowner:=nil;
  1129. name:=nil;
  1130. unitid:=0;
  1131. defowner:=nil;
  1132. new(symindex,init(indexgrowsize));
  1133. new(defindex,init(indexgrowsize));
  1134. new(symsearch,init);
  1135. symsearch^.usehash;
  1136. symsearch^.noclear:=true;
  1137. alignment:=def_alignment;
  1138. { load definitions }
  1139. loaddefs;
  1140. { load symbols }
  1141. loadsyms;
  1142. { Now we can deref the symbols and definitions }
  1143. if not(symtabletype in [objectsymtable,recordsymtable]) then
  1144. deref;
  1145. {$ifdef NEWMAP}
  1146. { necessary for dependencies }
  1147. current_module^.globalsymtable:=nil;
  1148. {$endif NEWMAP}
  1149. in_loading:=st_loading;
  1150. end;
  1151. procedure tsymtable.write;
  1152. begin
  1153. { write definitions }
  1154. writedefs;
  1155. { write symbols }
  1156. writesyms;
  1157. end;
  1158. constructor tsymtable.loadas(typ : tsymtabletype);
  1159. var
  1160. storesymtable : psymtable;
  1161. st_loading : boolean;
  1162. begin
  1163. st_loading:=in_loading;
  1164. in_loading:=true;
  1165. symtabletype:=typ;
  1166. new(symindex,init(indexgrowsize));
  1167. new(defindex,init(indexgrowsize));
  1168. new(symsearch,init);
  1169. symsearch^.noclear:=true;
  1170. defowner:=nil;
  1171. if typ in [recordsymtable,objectsymtable] then
  1172. begin
  1173. storesymtable:=aktrecordsymtable;
  1174. aktrecordsymtable:=@self;
  1175. end;
  1176. if typ in [parasymtable,localsymtable] then
  1177. begin
  1178. storesymtable:=aktlocalsymtable;
  1179. aktlocalsymtable:=@self;
  1180. end;
  1181. { used for local browser }
  1182. if typ=staticppusymtable then
  1183. begin
  1184. aktstaticsymtable:=@self;
  1185. symsearch^.usehash;
  1186. end;
  1187. name:=nil;
  1188. alignment:=def_alignment;
  1189. { isn't used there }
  1190. datasize:=0;
  1191. address_fixup:= 0;
  1192. { also unused }
  1193. unitid:=0;
  1194. { load definitions }
  1195. { we need the correct symtable for registering }
  1196. if not (typ in [localsymtable,parasymtable,recordsymtable,objectsymtable]) then
  1197. begin
  1198. next:=symtablestack;
  1199. symtablestack:=@self;
  1200. end;
  1201. { load definitions }
  1202. loaddefs;
  1203. { load symbols }
  1204. loadsyms;
  1205. { now we can deref the syms and defs }
  1206. if not (typ in [localsymtable,parasymtable,
  1207. recordsymtable,objectsymtable]) then
  1208. deref;
  1209. if typ in [recordsymtable,objectsymtable] then
  1210. aktrecordsymtable:=storesymtable;
  1211. if typ in [localsymtable,parasymtable] then
  1212. aktlocalsymtable:=storesymtable;
  1213. if not (typ in [localsymtable,parasymtable,recordsymtable,objectsymtable]) then
  1214. begin
  1215. symtablestack:=next;
  1216. end;
  1217. in_loading:=st_loading;
  1218. end;
  1219. procedure tsymtable.writeas;
  1220. var
  1221. oldtyp : byte;
  1222. storesymtable : psymtable;
  1223. begin
  1224. oldtyp:=current_ppu^.entrytyp;
  1225. storesymtable:=aktrecordsymtable;
  1226. if symtabletype in [recordsymtable,objectsymtable] then
  1227. begin
  1228. storesymtable:=aktrecordsymtable;
  1229. aktrecordsymtable:=@self;
  1230. end;
  1231. if symtabletype in [parasymtable,localsymtable] then
  1232. begin
  1233. storesymtable:=aktlocalsymtable;
  1234. aktlocalsymtable:=@self;
  1235. end;
  1236. if (symtabletype in [recordsymtable,objectsymtable]) then
  1237. current_ppu^.entrytyp:=subentryid;
  1238. { write definitions }
  1239. writedefs;
  1240. { write symbols }
  1241. writesyms;
  1242. current_ppu^.entrytyp:=oldtyp;
  1243. if symtabletype in [recordsymtable,objectsymtable] then
  1244. aktrecordsymtable:=storesymtable;
  1245. if symtabletype in [localsymtable,parasymtable] then
  1246. aktlocalsymtable:=storesymtable;
  1247. end;
  1248. {***********************************************
  1249. Get Symbol / Def by Number
  1250. ***********************************************}
  1251. function tsymtable.getsymnr(l : longint) : psym;
  1252. var
  1253. hp : psym;
  1254. begin
  1255. hp:=psym(symindex^.search(l));
  1256. if hp=nil then
  1257. internalerror(10999);
  1258. getsymnr:=hp;
  1259. end;
  1260. function tsymtable.getdefnr(l : longint) : pdef;
  1261. var
  1262. hp : pdef;
  1263. begin
  1264. hp:=pdef(defindex^.search(l));
  1265. if hp=nil then
  1266. internalerror(10998);
  1267. getdefnr:=hp;
  1268. end;
  1269. {***********************************************
  1270. Table Access
  1271. ***********************************************}
  1272. procedure tsymtable.clear;
  1273. begin
  1274. { remove no entry from a withsymtable as it is only a pointer to the
  1275. recorddef or objectdef symtable }
  1276. if symtabletype=withsymtable then
  1277. exit;
  1278. symindex^.clear;
  1279. defindex^.clear;
  1280. end;
  1281. function tsymtable.insert(sym:psym):psym;
  1282. var
  1283. hp : psymtable;
  1284. hsym : psym;
  1285. begin
  1286. { set owner and sym indexnb }
  1287. sym^.owner:=@self;
  1288. {$ifdef CHAINPROCSYMS}
  1289. { set the nextprocsym field }
  1290. if sym^.typ=procsym then
  1291. chainprocsym(sym);
  1292. {$endif CHAINPROCSYMS}
  1293. { writes the symbol in data segment if required }
  1294. { also sets the datasize of owner }
  1295. if not in_loading then
  1296. sym^.insert_in_data;
  1297. if (symtabletype in [staticsymtable,globalsymtable]) then
  1298. begin
  1299. hp:=symtablestack;
  1300. while assigned(hp) do
  1301. begin
  1302. if hp^.symtabletype in [staticsymtable,globalsymtable] then
  1303. begin
  1304. hsym:=hp^.search(sym^.name);
  1305. if assigned(hsym) then
  1306. DuplicateSym(hsym);
  1307. end;
  1308. hp:=hp^.next;
  1309. end;
  1310. end;
  1311. { check for duplicate id in local and parsymtable symtable }
  1312. if (symtabletype=localsymtable) then
  1313. { to be on the sure side: }
  1314. begin
  1315. if assigned(next) and
  1316. (next^.symtabletype=parasymtable) then
  1317. begin
  1318. hsym:=next^.search(sym^.name);
  1319. if assigned(hsym) then
  1320. DuplicateSym(hsym);
  1321. end
  1322. else if (current_module^.flags and uf_local_browser)=0 then
  1323. internalerror(43789);
  1324. end;
  1325. { check for duplicate id in local symtable of methods }
  1326. if (symtabletype=localsymtable) and
  1327. assigned(next) and
  1328. assigned(next^.next) and
  1329. { funcretsym is allowed !! }
  1330. (sym^.typ <> funcretsym) and
  1331. (next^.next^.symtabletype=objectsymtable) then
  1332. begin
  1333. hsym:=search_class_member(pobjectdef(next^.next^.defowner),sym^.name);
  1334. { but private ids can be reused }
  1335. if assigned(hsym) and
  1336. (not(sp_private in hsym^.symoptions) or
  1337. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  1338. DuplicateSym(hsym);
  1339. end;
  1340. { check for duplicate id in para symtable of methods }
  1341. if (symtabletype=parasymtable) and
  1342. assigned(procinfo^._class) and
  1343. { but not in nested procedures !}
  1344. (not(assigned(procinfo^.parent)) or
  1345. (assigned(procinfo^.parent) and
  1346. not(assigned(procinfo^.parent^._class)))
  1347. ) and
  1348. { funcretsym is allowed !! }
  1349. (sym^.typ <> funcretsym) then
  1350. begin
  1351. hsym:=search_class_member(procinfo^._class,sym^.name);
  1352. { but private ids can be reused }
  1353. if assigned(hsym) and
  1354. (not(sp_private in hsym^.symoptions) or
  1355. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  1356. DuplicateSym(hsym);
  1357. end;
  1358. { check for duplicate field id in inherited classes }
  1359. if (sym^.typ=varsym) and
  1360. (symtabletype=objectsymtable) and
  1361. assigned(defowner) then
  1362. begin
  1363. hsym:=search_class_member(pobjectdef(defowner),sym^.name);
  1364. { but private ids can be reused }
  1365. if assigned(hsym) and
  1366. (not(sp_private in hsym^.symoptions) or
  1367. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  1368. DuplicateSym(hsym);
  1369. end;
  1370. { register definition of typesym }
  1371. if (sym^.typ = typesym) and
  1372. assigned(ptypesym(sym)^.definition) then
  1373. begin
  1374. if not(assigned(ptypesym(sym)^.definition^.owner)) and
  1375. (ptypesym(sym)^.definition^.deftype<>errordef) then
  1376. registerdef(ptypesym(sym)^.definition);
  1377. {$ifdef GDB}
  1378. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  1379. (symtabletype in [globalsymtable,staticsymtable]) then
  1380. begin
  1381. ptypesym(sym)^.isusedinstab := true;
  1382. sym^.concatstabto(debuglist);
  1383. end;
  1384. {$endif GDB}
  1385. end;
  1386. { insert in index and search hash }
  1387. symindex^.insert(sym);
  1388. symsearch^.insert(sym);
  1389. insert:=sym;
  1390. end;
  1391. function tsymtable.search(const s : stringid) : psym;
  1392. begin
  1393. {search:=psym(symsearch^.search(s));
  1394. this bypasses the ref generation (PM) }
  1395. search:=speedsearch(s,getspeedvalue(s));
  1396. end;
  1397. function tsymtable.speedsearch(const s : stringid;speedvalue : longint) : psym;
  1398. var
  1399. hp : psym;
  1400. newref : pref;
  1401. begin
  1402. hp:=psym(symsearch^.speedsearch(s,speedvalue));
  1403. if assigned(hp) then
  1404. begin
  1405. { reject non static members in static procedures,
  1406. be carefull aktprocsym^.definition is not allways
  1407. loaded already (PFV) }
  1408. if (symtabletype=objectsymtable) and
  1409. not(sp_static in hp^.symoptions) and
  1410. allow_only_static
  1411. {assigned(aktprocsym) and
  1412. assigned(aktprocsym^.definition) and
  1413. ((aktprocsym^.definition^.options and postaticmethod)<>0)} then
  1414. Message(sym_e_only_static_in_static);
  1415. if (symtabletype=unitsymtable) and
  1416. assigned(punitsymtable(@self)^.unitsym) then
  1417. inc(punitsymtable(@self)^.unitsym^.refs);
  1418. { unitsym are only loaded for browsing PM }
  1419. { this was buggy anyway because we could use }
  1420. { unitsyms from other units in _USES !! }
  1421. if (symtabletype=unitsymtable) and (hp^.typ=unitsym) and
  1422. assigned(current_module) and (current_module^.globalsymtable<>@self) then
  1423. hp:=nil;
  1424. if assigned(hp) and
  1425. (cs_browser in aktmoduleswitches) and make_ref then
  1426. begin
  1427. new(newref,init(hp^.lastref,@tokenpos));
  1428. { for symbols that are in tables without
  1429. browser info or syssyms (PM) }
  1430. if hp^.refcount=0 then
  1431. begin
  1432. hp^.defref:=newref;
  1433. hp^.lastref:=newref;
  1434. end
  1435. else
  1436. if resolving_forward and assigned(hp^.defref) then
  1437. { put it as second reference }
  1438. begin
  1439. newref^.nextref:=hp^.defref^.nextref;
  1440. hp^.defref^.nextref:=newref;
  1441. hp^.lastref^.nextref:=nil;
  1442. end
  1443. else
  1444. hp^.lastref:=newref;
  1445. inc(hp^.refcount);
  1446. end;
  1447. end;
  1448. speedsearch:=hp;
  1449. end;
  1450. function tsymtable.rename(const olds,news : stringid):psym;
  1451. begin
  1452. rename:=psym(symsearch^.rename(olds,news));
  1453. end;
  1454. {***********************************************
  1455. Browser
  1456. ***********************************************}
  1457. procedure tsymtable.load_browser;
  1458. var
  1459. b : byte;
  1460. sym : psym;
  1461. prdef : pdef;
  1462. oldrecsyms : psymtable;
  1463. begin
  1464. if symtabletype in [recordsymtable,objectsymtable] then
  1465. begin
  1466. oldrecsyms:=aktrecordsymtable;
  1467. aktrecordsymtable:=@self;
  1468. end;
  1469. if symtabletype in [parasymtable,localsymtable] then
  1470. begin
  1471. oldrecsyms:=aktlocalsymtable;
  1472. aktlocalsymtable:=@self;
  1473. end;
  1474. if symtabletype=staticppusymtable then
  1475. aktstaticsymtable:=@self;
  1476. b:=current_ppu^.readentry;
  1477. if b <> ibbeginsymtablebrowser then
  1478. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1479. repeat
  1480. b:=current_ppu^.readentry;
  1481. case b of
  1482. ibsymref : begin
  1483. sym:=readsymref;
  1484. resolvesym(sym);
  1485. if assigned(sym) then
  1486. sym^.load_references;
  1487. end;
  1488. ibdefref : begin
  1489. prdef:=readdefref;
  1490. resolvedef(prdef);
  1491. if assigned(prdef) then
  1492. begin
  1493. if prdef^.deftype<>procdef then
  1494. Message(unit_f_ppu_read_error);
  1495. pprocdef(prdef)^.load_references;
  1496. end;
  1497. end;
  1498. ibendsymtablebrowser : break;
  1499. else
  1500. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1501. end;
  1502. until false;
  1503. if symtabletype in [recordsymtable,objectsymtable] then
  1504. aktrecordsymtable:=oldrecsyms;
  1505. if symtabletype in [parasymtable,localsymtable] then
  1506. aktlocalsymtable:=oldrecsyms;
  1507. end;
  1508. procedure tsymtable.write_browser;
  1509. var
  1510. oldrecsyms : psymtable;
  1511. begin
  1512. { symbol numbering for references
  1513. should have been done in write PM
  1514. number_symbols;
  1515. number_defs; }
  1516. if symtabletype in [recordsymtable,objectsymtable] then
  1517. begin
  1518. oldrecsyms:=aktrecordsymtable;
  1519. aktrecordsymtable:=@self;
  1520. end;
  1521. if symtabletype in [parasymtable,localsymtable] then
  1522. begin
  1523. oldrecsyms:=aktlocalsymtable;
  1524. aktlocalsymtable:=@self;
  1525. end;
  1526. current_ppu^.writeentry(ibbeginsymtablebrowser);
  1527. foreach({$ifndef TP}@{$endif}write_refs);
  1528. current_ppu^.writeentry(ibendsymtablebrowser);
  1529. if symtabletype in [recordsymtable,objectsymtable] then
  1530. aktrecordsymtable:=oldrecsyms;
  1531. if symtabletype in [parasymtable,localsymtable] then
  1532. aktlocalsymtable:=oldrecsyms;
  1533. end;
  1534. {$ifdef BrowserLog}
  1535. procedure tsymtable.writebrowserlog;
  1536. begin
  1537. if cs_browser in aktmoduleswitches then
  1538. begin
  1539. if assigned(name) then
  1540. Browserlog.AddLog('---Symtable '+name^)
  1541. else
  1542. begin
  1543. if (symtabletype=recordsymtable) and
  1544. assigned(defowner^.sym) then
  1545. Browserlog.AddLog('---Symtable '+defowner^.sym^.name)
  1546. else
  1547. Browserlog.AddLog('---Symtable with no name');
  1548. end;
  1549. Browserlog.Ident;
  1550. foreach({$ifndef TP}@{$endif}add_to_browserlog);
  1551. browserlog.Unident;
  1552. end;
  1553. end;
  1554. {$endif BrowserLog}
  1555. {***********************************************
  1556. Process all entries
  1557. ***********************************************}
  1558. { checks, if all procsyms and methods are defined }
  1559. procedure tsymtable.check_forwards;
  1560. begin
  1561. foreach({$ifndef TP}@{$endif}check_forward);
  1562. end;
  1563. procedure tsymtable.checklabels;
  1564. begin
  1565. foreach({$ifndef TP}@{$endif}labeldefined);
  1566. end;
  1567. procedure tsymtable.set_alignment(_alignment : byte);
  1568. var
  1569. sym : pvarsym;
  1570. l : longint;
  1571. begin
  1572. { this can not be done if there is an
  1573. hasharray ! }
  1574. alignment:=_alignment;
  1575. if (symtabletype<>parasymtable) then
  1576. internalerror(1111);
  1577. sym:=pvarsym(symindex^.first);
  1578. datasize:=0;
  1579. { there can be only varsyms }
  1580. while assigned(sym) do
  1581. begin
  1582. l:=sym^.getpushsize;
  1583. sym^.address:=datasize;
  1584. datasize:=align(datasize+l,alignment);
  1585. sym:=pvarsym(sym^.next);
  1586. end;
  1587. end;
  1588. function tsymtable.find_at_offset(l : longint) : pvarsym;
  1589. var
  1590. sym : pvarsym;
  1591. begin
  1592. find_at_offset:=nil;
  1593. { this can not be done if there is an
  1594. hasharray ! }
  1595. if (symtabletype<>parasymtable) then
  1596. internalerror(1111);
  1597. sym:=pvarsym(symindex^.first);
  1598. while assigned(sym) do
  1599. begin
  1600. if sym^.address+address_fixup=l then
  1601. begin
  1602. find_at_offset:=sym;
  1603. exit;
  1604. end;
  1605. sym:=pvarsym(sym^.next);
  1606. end;
  1607. end;
  1608. procedure tsymtable.allunitsused;
  1609. begin
  1610. foreach({$ifndef TP}@{$endif}unitsymbolused);
  1611. end;
  1612. procedure tsymtable.allsymbolsused;
  1613. begin
  1614. foreach({$ifndef TP}@{$endif}varsymbolused);
  1615. end;
  1616. {$ifdef CHAINPROCSYMS}
  1617. procedure tsymtable.chainprocsyms;
  1618. begin
  1619. foreach({$ifndef TP}@{$endif}chainprocsym);
  1620. end;
  1621. {$endif CHAINPROCSYMS}
  1622. {$ifdef GDB}
  1623. procedure tsymtable.concatstabto(asmlist : paasmoutput);
  1624. begin
  1625. asmoutput:=asmlist;
  1626. foreach({$ifndef TP}@{$endif}concatstab);
  1627. end;
  1628. {$endif}
  1629. {****************************************************************************
  1630. TUNITSYMTABLE
  1631. ****************************************************************************}
  1632. constructor tunitsymtable.init(t : tsymtabletype; const n : string);
  1633. begin
  1634. inherited init(t);
  1635. name:=stringdup(upper(n));
  1636. unitid:=0;
  1637. unitsym:=nil;
  1638. symsearch^.usehash;
  1639. { reset GDB things }
  1640. {$ifdef GDB}
  1641. if (t = globalsymtable) then
  1642. begin
  1643. prev_dbx_counter := dbx_counter;
  1644. dbx_counter := nil;
  1645. end;
  1646. is_stab_written:=false;
  1647. dbx_count := -1;
  1648. if cs_gdb_dbx in aktglobalswitches then
  1649. begin
  1650. dbx_count := 0;
  1651. unittypecount:=1;
  1652. if (symtabletype=globalsymtable) then
  1653. pglobaltypecount := @unittypecount;
  1654. unitid:=current_module^.unitcount;
  1655. debuglist^.concat(new(pai_asm_comment,init(strpnew('Global '+name^+' has index '+tostr(unitid)))));
  1656. debuglist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0'))));
  1657. inc(current_module^.unitcount);
  1658. dbx_count_ok:=false;
  1659. dbx_counter:=@dbx_count;
  1660. do_count_dbx:=true;
  1661. end;
  1662. {$endif GDB}
  1663. end;
  1664. constructor tunitsymtable.loadasunit;
  1665. var
  1666. storeGlobalTypeCount : pword;
  1667. b : byte;
  1668. begin
  1669. unitsym:=nil;
  1670. unitid:=0;
  1671. {$ifdef GDB}
  1672. if cs_gdb_dbx in aktglobalswitches then
  1673. begin
  1674. UnitTypeCount:=1;
  1675. storeGlobalTypeCount:=PGlobalTypeCount;
  1676. PglobalTypeCount:=@UnitTypeCount;
  1677. end;
  1678. {$endif GDB}
  1679. { load symtables }
  1680. inherited load;
  1681. { set the name after because it is set to nil in tsymtable.load !! }
  1682. name:=stringdup(current_module^.modulename^);
  1683. { dbx count }
  1684. {$ifdef GDB}
  1685. if (current_module^.flags and uf_has_dbx)<>0 then
  1686. begin
  1687. b := current_ppu^.readentry;
  1688. if b <> ibdbxcount then
  1689. Message(unit_f_ppu_dbx_count_problem)
  1690. else
  1691. dbx_count := readlong;
  1692. dbx_count_ok := true;
  1693. end
  1694. else
  1695. begin
  1696. dbx_count := -1;
  1697. dbx_count_ok:=false;
  1698. end;
  1699. if cs_gdb_dbx in aktglobalswitches then
  1700. PGlobalTypeCount:=storeGlobalTypeCount;
  1701. is_stab_written:=false;
  1702. {$endif GDB}
  1703. b:=current_ppu^.readentry;
  1704. if b<>ibendimplementation then
  1705. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1706. end;
  1707. destructor tunitsymtable.done;
  1708. var
  1709. pus : punitsym;
  1710. begin
  1711. pus:=unitsym;
  1712. while assigned(pus) do
  1713. begin
  1714. unitsym:=pus^.prevsym;
  1715. pus^.prevsym:=nil;
  1716. pus^.unitsymtable:=nil;
  1717. pus:=unitsym;
  1718. end;
  1719. inherited done;
  1720. end;
  1721. procedure tunitsymtable.load_symtable_refs;
  1722. var
  1723. b : byte;
  1724. unitindex : word;
  1725. begin
  1726. if ((current_module^.flags and uf_local_browser)<>0) then
  1727. begin
  1728. current_module^.localsymtable:=new(punitsymtable,loadas(staticppusymtable));
  1729. psymtable(current_module^.localsymtable)^.name:=
  1730. stringdup('implementation of '+psymtable(current_module^.globalsymtable)^.name^);
  1731. end;
  1732. { load browser }
  1733. if (current_module^.flags and uf_has_browser)<>0 then
  1734. begin
  1735. {if not (cs_browser in aktmoduleswitches) then
  1736. current_ppu^.skipuntilentry(ibendbrowser)
  1737. else }
  1738. begin
  1739. load_browser;
  1740. unitindex:=1;
  1741. while assigned(current_module^.map^[unitindex]) do
  1742. begin
  1743. {each unit wrote one browser entry }
  1744. load_browser;
  1745. inc(unitindex);
  1746. end;
  1747. b:=current_ppu^.readentry;
  1748. if b<>ibendbrowser then
  1749. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1750. end;
  1751. end;
  1752. if ((current_module^.flags and uf_local_browser)<>0) then
  1753. psymtable(current_module^.localsymtable)^.load_browser;
  1754. end;
  1755. procedure tunitsymtable.writeasunit;
  1756. var
  1757. pu : pused_unit;
  1758. begin
  1759. { first the unitname }
  1760. current_ppu^.putstring(name^);
  1761. current_ppu^.writeentry(ibmodulename);
  1762. writesourcefiles;
  1763. writeusedmacros;
  1764. writeusedunit;
  1765. { write the objectfiles and libraries that come for this unit,
  1766. preserve the containers becuase they are still needed to load
  1767. the link.res. All doesn't depend on the crc! It doesn't matter
  1768. if a unit is in a .o or .a file }
  1769. current_ppu^.do_crc:=false;
  1770. writelinkcontainer(current_module^.linkunitofiles,iblinkunitofiles,true);
  1771. writelinkcontainer(current_module^.linkunitstaticlibs,iblinkunitstaticlibs,true);
  1772. writelinkcontainer(current_module^.linkunitsharedlibs,iblinkunitsharedlibs,true);
  1773. writelinkcontainer(current_module^.linkotherofiles,iblinkotherofiles,false);
  1774. writelinkcontainer(current_module^.linkotherstaticlibs,iblinkotherstaticlibs,true);
  1775. writelinkcontainer(current_module^.linkothersharedlibs,iblinkothersharedlibs,true);
  1776. current_ppu^.do_crc:=true;
  1777. current_ppu^.writeentry(ibendinterface);
  1778. { write the symtable entries }
  1779. inherited write;
  1780. { all after doesn't affect crc }
  1781. current_ppu^.do_crc:=false;
  1782. { write dbx count }
  1783. {$ifdef GDB}
  1784. if cs_gdb_dbx in aktglobalswitches then
  1785. begin
  1786. {$IfDef EXTDEBUG}
  1787. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1788. {$ENDIF EXTDEBUG}
  1789. current_ppu^.putlongint(dbx_count);
  1790. current_ppu^.writeentry(ibdbxcount);
  1791. end;
  1792. {$endif GDB}
  1793. current_ppu^.writeentry(ibendimplementation);
  1794. { write static symtable
  1795. needed for local debugging of unit functions }
  1796. if ((current_module^.flags and uf_local_browser)<>0) and
  1797. assigned(current_module^.localsymtable) then
  1798. psymtable(current_module^.localsymtable)^.write;
  1799. { write all browser section }
  1800. if (current_module^.flags and uf_has_browser)<>0 then
  1801. begin
  1802. write_browser;
  1803. pu:=pused_unit(current_module^.used_units.first);
  1804. while assigned(pu) do
  1805. begin
  1806. psymtable(pu^.u^.globalsymtable)^.write_browser;
  1807. pu:=pused_unit(pu^.next);
  1808. end;
  1809. current_ppu^.writeentry(ibendbrowser);
  1810. end;
  1811. if ((current_module^.flags and uf_local_browser)<>0) and
  1812. assigned(current_module^.localsymtable) then
  1813. psymtable(current_module^.localsymtable)^.write_browser;
  1814. { the last entry ibend is written automaticly }
  1815. end;
  1816. function tunitsymtable.getnewtypecount : word;
  1817. begin
  1818. {$ifdef GDB}
  1819. if not (cs_gdb_dbx in aktglobalswitches) then
  1820. getnewtypecount:=tsymtable.getnewtypecount
  1821. else
  1822. {$endif GDB}
  1823. if symtabletype = staticsymtable then
  1824. getnewtypecount:=tsymtable.getnewtypecount
  1825. else
  1826. begin
  1827. getnewtypecount:=unittypecount;
  1828. inc(unittypecount);
  1829. end;
  1830. end;
  1831. {$ifdef GDB}
  1832. procedure tunitsymtable.concattypestabto(asmlist : paasmoutput);
  1833. var prev_dbx_count : plongint;
  1834. begin
  1835. if is_stab_written then exit;
  1836. if not assigned(name) then name := stringdup('Main_program');
  1837. if (symtabletype = unitsymtable) and
  1838. (current_module^.globalsymtable<>@Self) then
  1839. begin
  1840. unitid:=current_module^.unitcount;
  1841. inc(current_module^.unitcount);
  1842. end;
  1843. asmlist^.concat(new(pai_asm_comment,init(strpnew('Begin unit '+name^
  1844. +' has index '+tostr(unitid)))));
  1845. if cs_gdb_dbx in aktglobalswitches then
  1846. begin
  1847. if dbx_count_ok then
  1848. begin
  1849. asmlist^.concat(new(pai_asm_comment,init(strpnew('"repeated" unit '+name^
  1850. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count)))));
  1851. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1852. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count)))));
  1853. exit;
  1854. end
  1855. else if (current_module^.globalsymtable<>@Self) then
  1856. begin
  1857. prev_dbx_count := dbx_counter;
  1858. dbx_counter := nil;
  1859. do_count_dbx:=false;
  1860. if symtabletype = unitsymtable then
  1861. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1862. +tostr(N_BINCL)+',0,0,0'))));
  1863. dbx_counter := @dbx_count;
  1864. dbx_count:=0;
  1865. do_count_dbx:=assigned(dbx_counter);
  1866. end;
  1867. end;
  1868. asmoutput:=asmlist;
  1869. foreach({$ifndef TP}@{$endif}concattypestab);
  1870. if cs_gdb_dbx in aktglobalswitches then
  1871. begin
  1872. if (current_module^.globalsymtable<>@Self) then
  1873. begin
  1874. dbx_counter := prev_dbx_count;
  1875. do_count_dbx:=false;
  1876. asmlist^.concat(new(pai_asm_comment,init(strpnew('End unit '+name^
  1877. +' has index '+tostr(unitid)))));
  1878. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1879. +tostr(N_EINCL)+',0,0,0'))));
  1880. do_count_dbx:=assigned(dbx_counter);
  1881. dbx_count_ok := true;
  1882. end;
  1883. end;
  1884. is_stab_written:=true;
  1885. end;
  1886. {$endif}
  1887. {****************************************************************************
  1888. Definitions
  1889. ****************************************************************************}
  1890. {$I symdef.inc}
  1891. {****************************************************************************
  1892. Symbols
  1893. ****************************************************************************}
  1894. {$I symsym.inc}
  1895. {****************************************************************************
  1896. GDB Helpers
  1897. ****************************************************************************}
  1898. {$ifdef GDB}
  1899. function typeglobalnumber(const s : string) : string;
  1900. var st : string;
  1901. symt : psymtable;
  1902. old_make_ref : boolean;
  1903. begin
  1904. old_make_ref:=make_ref;
  1905. make_ref:=false;
  1906. typeglobalnumber := '0';
  1907. srsym := nil;
  1908. if pos('.',s) > 0 then
  1909. begin
  1910. st := copy(s,1,pos('.',s)-1);
  1911. getsym(st,false);
  1912. st := copy(s,pos('.',s)+1,255);
  1913. if assigned(srsym) then
  1914. begin
  1915. if srsym^.typ = unitsym then
  1916. begin
  1917. symt := punitsym(srsym)^.unitsymtable;
  1918. srsym := symt^.search(st);
  1919. end else srsym := nil;
  1920. end;
  1921. end else st := s;
  1922. if srsym = nil then getsym(st,true);
  1923. if srsym^.typ<>typesym then
  1924. begin
  1925. Message(type_e_type_id_expected);
  1926. exit;
  1927. end;
  1928. typeglobalnumber := ptypesym(srsym)^.definition^.numberstring;
  1929. make_ref:=old_make_ref;
  1930. end;
  1931. {$endif GDB}
  1932. {****************************************************************************
  1933. Definition Helpers
  1934. ****************************************************************************}
  1935. procedure reset_global_defs;
  1936. var
  1937. def : pdef;
  1938. {$ifdef debug}
  1939. prevdef : pdef;
  1940. {$endif debug}
  1941. begin
  1942. {$ifdef debug}
  1943. prevdef:=nil;
  1944. {$endif debug}
  1945. {$ifdef GDB}
  1946. pglobaltypecount:=@globaltypecount;
  1947. {$endif GDB}
  1948. def:=firstglobaldef;
  1949. while assigned(def) do
  1950. begin
  1951. {$ifdef GDB}
  1952. if assigned(def^.sym) then
  1953. def^.sym^.isusedinstab:=false;
  1954. def^.is_def_stab_written:=false;
  1955. {$endif GDB}
  1956. {if not current_module^.in_implementation then}
  1957. begin
  1958. { reset rangenr's }
  1959. case def^.deftype of
  1960. orddef : porddef(def)^.rangenr:=0;
  1961. enumdef : penumdef(def)^.rangenr:=0;
  1962. arraydef : parraydef(def)^.rangenr:=0;
  1963. end;
  1964. if def^.deftype<>objectdef then
  1965. def^.has_rtti:=false;
  1966. def^.has_inittable:=false;
  1967. end;
  1968. {$ifdef debug}
  1969. prevdef:=def;
  1970. {$endif debug}
  1971. def:=def^.nextglobal;
  1972. end;
  1973. end;
  1974. {****************************************************************************
  1975. Object Helpers
  1976. ****************************************************************************}
  1977. function search_class_member(pd : pobjectdef;const n : string) : psym;
  1978. { searches n in symtable of pd and all anchestors }
  1979. var
  1980. sym : psym;
  1981. begin
  1982. sym:=nil;
  1983. while assigned(pd) do
  1984. begin
  1985. sym:=pd^.symtable^.search(n);
  1986. if assigned(sym) then
  1987. break;
  1988. pd:=pd^.childof;
  1989. end;
  1990. { this is needed for static methods in do_member_read pexpr unit PM
  1991. caused bug0214 }
  1992. if assigned(sym) then
  1993. begin
  1994. srsymtable:=pd^.symtable;
  1995. end;
  1996. search_class_member:=sym;
  1997. end;
  1998. var
  1999. _defaultprop : ppropertysym;
  2000. procedure testfordefaultproperty(p : pnamedindexobject);
  2001. begin
  2002. if (psym(p)^.typ=propertysym) and
  2003. (ppo_defaultproperty in ppropertysym(p)^.propoptions) then
  2004. _defaultprop:=ppropertysym(p);
  2005. end;
  2006. function search_default_property(pd : pobjectdef) : ppropertysym;
  2007. { returns the default property of a class, searches also anchestors }
  2008. begin
  2009. _defaultprop:=nil;
  2010. while assigned(pd) do
  2011. begin
  2012. pd^.symtable^.foreach({$ifndef TP}@{$endif}testfordefaultproperty);
  2013. if assigned(_defaultprop) then
  2014. break;
  2015. pd:=pd^.childof;
  2016. end;
  2017. search_default_property:=_defaultprop;
  2018. end;
  2019. {****************************************************************************
  2020. Macro's
  2021. ****************************************************************************}
  2022. procedure def_macro(const s : string);
  2023. var
  2024. mac : pmacrosym;
  2025. begin
  2026. mac:=pmacrosym(macros^.search(s));
  2027. if mac=nil then
  2028. begin
  2029. mac:=new(pmacrosym,init(s));
  2030. Message1(parser_m_macro_defined,mac^.name);
  2031. macros^.insert(mac);
  2032. end;
  2033. mac^.defined:=true;
  2034. mac^.defined_at_startup:=true;
  2035. end;
  2036. procedure set_macro(const s : string;value : string);
  2037. var
  2038. mac : pmacrosym;
  2039. begin
  2040. mac:=pmacrosym(macros^.search(s));
  2041. if mac=nil then
  2042. begin
  2043. mac:=new(pmacrosym,init(s));
  2044. macros^.insert(mac);
  2045. end
  2046. else
  2047. begin
  2048. if assigned(mac^.buftext) then
  2049. freemem(mac^.buftext,mac^.buflen);
  2050. end;
  2051. Message2(parser_m_macro_set_to,mac^.name,value);
  2052. mac^.buflen:=length(value);
  2053. getmem(mac^.buftext,mac^.buflen);
  2054. move(value[1],mac^.buftext^,mac^.buflen);
  2055. mac^.defined:=true;
  2056. mac^.defined_at_startup:=true;
  2057. end;
  2058. {$ifdef UNITALIASES}
  2059. {****************************************************************************
  2060. TUNIT_ALIAS
  2061. ****************************************************************************}
  2062. constructor tunit_alias.init(const n:string);
  2063. var
  2064. i : longint;
  2065. begin
  2066. i:=pos('=',n);
  2067. if i=0 then
  2068. fail;
  2069. inherited initname(Copy(n,1,i-1));
  2070. newname:=stringdup(Copy(n,i+1,255));
  2071. end;
  2072. destructor tunit_alias.done;
  2073. begin
  2074. stringdispose(newname);
  2075. inherited done;
  2076. end;
  2077. procedure addunitalias(const n:string);
  2078. begin
  2079. unitaliases^.insert(new(punit_alias,init(Upper(n))));
  2080. end;
  2081. function getunitalias(const n:string):string;
  2082. var
  2083. p : punit_alias;
  2084. begin
  2085. p:=punit_alias(unitaliases^.search(Upper(n)));
  2086. if assigned(p) then
  2087. getunitalias:=punit_alias(p)^.newname^
  2088. else
  2089. getunitalias:=n;
  2090. end;
  2091. {$endif UNITALIASES}
  2092. {****************************************************************************
  2093. Symtable Stack
  2094. ****************************************************************************}
  2095. procedure dellexlevel;
  2096. var
  2097. p : psymtable;
  2098. begin
  2099. p:=symtablestack;
  2100. symtablestack:=p^.next;
  2101. { symbol tables of unit interfaces are never disposed }
  2102. { this is handle by the unit unitm }
  2103. if not(p^.symtabletype in [unitsymtable,globalsymtable,stt_exceptsymtable]) or dispose_global then
  2104. dispose(p,done);
  2105. end;
  2106. {$ifdef DEBUG}
  2107. procedure test_symtablestack;
  2108. var
  2109. p : psymtable;
  2110. i : longint;
  2111. begin
  2112. p:=symtablestack;
  2113. i:=0;
  2114. while assigned(p) do
  2115. begin
  2116. inc(i);
  2117. p:=p^.next;
  2118. if i>500 then
  2119. Message(sym_f_internal_error_in_symtablestack);
  2120. end;
  2121. end;
  2122. procedure list_symtablestack;
  2123. var
  2124. p : psymtable;
  2125. i : longint;
  2126. begin
  2127. p:=symtablestack;
  2128. i:=0;
  2129. while assigned(p) do
  2130. begin
  2131. inc(i);
  2132. writeln(i,' ',p^.name^);
  2133. p:=p^.next;
  2134. if i>500 then
  2135. Message(sym_f_internal_error_in_symtablestack);
  2136. end;
  2137. end;
  2138. {$endif DEBUG}
  2139. {****************************************************************************
  2140. Init/Done Symtable
  2141. ****************************************************************************}
  2142. {$ifndef Delphi}
  2143. {$ifdef tp}
  2144. procedure do_streamerror;
  2145. begin
  2146. if symbolstream.status=-2 then
  2147. WriteLn('Error: Not enough EMS memory')
  2148. else
  2149. WriteLn('Error: EMS Error ',symbolstream.status);
  2150. halt(1);
  2151. end;
  2152. {$endif TP}
  2153. {$endif Delphi}
  2154. procedure InitSymtable;
  2155. begin
  2156. {$ifndef Delphi}
  2157. {$ifdef TP}
  2158. { Allocate stream }
  2159. if use_big then
  2160. begin
  2161. streamerror:=@do_streamerror;
  2162. { symbolstream.init('TMPFILE',stcreate,16000); }
  2163. {$ifndef dpmi}
  2164. symbolstream.init(10000,4000000); {using ems streams}
  2165. {$else}
  2166. symbolstream.init(1000000,16000); {using memory streams}
  2167. {$endif}
  2168. if symbolstream.errorinfo=stiniterror then
  2169. do_streamerror;
  2170. { write something, because pos 0 means nil pointer }
  2171. symbolstream.writestr(@inputfile);
  2172. end;
  2173. {$endif tp}
  2174. {$endif Delphi}
  2175. { Reset symbolstack }
  2176. registerdef:=false;
  2177. read_member:=false;
  2178. symtablestack:=nil;
  2179. systemunit:=nil;
  2180. {$ifdef GDB}
  2181. firstglobaldef:=nil;
  2182. lastglobaldef:=nil;
  2183. {$endif GDB}
  2184. globaltypecount:=1;
  2185. pglobaltypecount:=@globaltypecount;
  2186. { create error syms and def }
  2187. generrorsym:=new(perrorsym,init);
  2188. generrordef:=new(perrordef,init);
  2189. {$ifdef UNITALIASES}
  2190. { unit aliases }
  2191. unitaliases:=new(pdictionary,init);
  2192. {$endif}
  2193. for token:=first_overloaded to last_overloaded do
  2194. overloaded_operators[token]:=nil;
  2195. end;
  2196. procedure DoneSymtable;
  2197. begin
  2198. dispose(generrorsym,done);
  2199. dispose(generrordef,done);
  2200. {$ifdef UNITALIASES}
  2201. dispose(unitaliases,done);
  2202. {$endif}
  2203. {$ifndef Delphi}
  2204. {$ifdef TP}
  2205. { close the stream }
  2206. if use_big then
  2207. symbolstream.done;
  2208. {$endif}
  2209. {$endif Delphi}
  2210. end;
  2211. end.
  2212. {
  2213. $Log$
  2214. Revision 1.62 1999-11-15 22:00:48 peter
  2215. * labels used but not defined give error instead of warning, the warning
  2216. is now only with declared but not defined and not used.
  2217. Revision 1.61 1999/11/15 17:52:59 pierre
  2218. + one field added for ttoken record for operator
  2219. linking the id to the corresponding operator token that
  2220. can now now all be overloaded
  2221. * overloaded operators are resetted to nil in InitSymtable
  2222. (bug when trying to compile a uint that overloads operators twice)
  2223. Revision 1.60 1999/11/09 23:35:50 pierre
  2224. + better reference pos for forward defs
  2225. Revision 1.59 1999/11/06 16:21:57 jonas
  2226. + search optimial register to use in alignment code (compile with
  2227. -dalignreg, -dalignregdebug to see chosen register in
  2228. assembler code). Still needs support in ag386bin.
  2229. Revision 1.58 1999/11/06 14:34:28 peter
  2230. * truncated log to 20 revs
  2231. Revision 1.57 1999/11/05 17:18:03 pierre
  2232. * local browsing works at first level
  2233. ie for function defined in interface or implementation
  2234. not yet for functions inside other functions
  2235. Revision 1.56 1999/11/04 23:13:25 peter
  2236. * moved unit alias support into ifdef
  2237. Revision 1.55 1999/11/04 10:54:02 peter
  2238. + -Ua<oldname>=<newname> unit alias support
  2239. Revision 1.54 1999/10/26 12:30:46 peter
  2240. * const parameter is now checked
  2241. * better and generic check if a node can be used for assigning
  2242. * export fixes
  2243. * procvar equal works now (it never had worked at least from 0.99.8)
  2244. * defcoll changed to linkedlist with pparaitem so it can easily be
  2245. walked both directions
  2246. Revision 1.53 1999/10/06 17:39:15 peter
  2247. * fixed stabs writting for forward types
  2248. Revision 1.52 1999/10/03 19:44:42 peter
  2249. * removed objpasunit reference, tvarrec is now searched in systemunit
  2250. where it already was located
  2251. Revision 1.51 1999/10/01 08:02:49 peter
  2252. * forward type declaration rewritten
  2253. Revision 1.50 1999/09/28 20:48:25 florian
  2254. * fixed bug 610
  2255. + added $D- for TP in symtable.pas else it can't be compiled anymore
  2256. (too much symbols :()
  2257. Revision 1.49 1999/09/27 23:44:59 peter
  2258. * procinfo is now a pointer
  2259. * support for result setting in sub procedure
  2260. Revision 1.48 1999/09/12 21:35:38 florian
  2261. * fixed a crash under Linux. Why doesn't have the damned Windows DPMI nil pointer
  2262. protection???
  2263. Revision 1.47 1999/09/12 08:48:09 florian
  2264. * bugs 593 and 607 fixed
  2265. * some other potential bugs with array constructors fixed
  2266. * for classes compiled in $M+ and it's childs, the default access method
  2267. is now published
  2268. * fixed copyright message (it is now 1993-99)
  2269. Revision 1.46 1999/09/10 18:48:10 florian
  2270. * some bug fixes (e.g. must_be_valid and procinfo^.funcret_is_valid)
  2271. * most things for stored properties fixed
  2272. Revision 1.45 1999/09/08 08:05:44 peter
  2273. * fixed bug 248
  2274. Revision 1.44 1999/08/31 15:46:21 pierre
  2275. * do_crc must be false for all browser stuff
  2276. + tmacrosym defined_at_startup set in def_macro and set_macro
  2277. Revision 1.43 1999/08/27 10:39:24 pierre
  2278. * uf_local_browser made problem when computing interface CRC
  2279. Revision 1.42 1999/08/13 21:33:13 peter
  2280. * support for array constructors extended and more error checking
  2281. Revision 1.41 1999/08/13 14:24:22 pierre
  2282. + stabs for classes and classref working,
  2283. a class still needs an ^ to get that content of it,
  2284. but the class fields inside a class don't result into an
  2285. infinite loop anymore!
  2286. Revision 1.40 1999/08/10 16:25:42 pierre
  2287. * unitid changed to word
  2288. Revision 1.39 1999/08/10 12:33:36 pierre
  2289. * pprocsym defined earlier for use in tprocdef
  2290. Revision 1.38 1999/08/05 16:53:18 peter
  2291. * V_Fatal=1, all other V_ are also increased
  2292. * Check for local procedure when assigning procvar
  2293. * fixed comment parsing because directives
  2294. * oldtp mode directives better supported
  2295. * added some messages to errore.msg
  2296. }