symtable.pas 92 KB

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