symtable.pas 79 KB

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