symtable.pas 92 KB

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