symtable.pas 94 KB

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