symtable.pas 92 KB

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