symtable.pas 95 KB

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