symtable.pas 92 KB

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