symtable.pas 93 KB

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