symtable.pas 93 KB

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