symtable.pas 90 KB

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