symtable.pas 97 KB

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