symtable.pas 90 KB

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