symtable.pas 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  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. { common }
  23. cutils,cobjects,
  24. { global }
  25. globtype,tokens,
  26. { symtable }
  27. symconst,symbase,symtype,symdef,symsym,
  28. { assembler }
  29. aasm
  30. ;
  31. {****************************************************************************
  32. Symtable types
  33. ****************************************************************************}
  34. type
  35. pstoredsymtable = ^tstoredsymtable;
  36. tstoredsymtable = object(tsymtable)
  37. constructor init(t : tsymtabletype);
  38. { load/write }
  39. constructor loadas(typ : tsymtabletype);
  40. procedure writeas;
  41. procedure loaddefs;
  42. procedure loadsyms;
  43. procedure writedefs;
  44. procedure writesyms;
  45. procedure deref;
  46. procedure insert(sym : psymentry);virtual;
  47. procedure insert_in(psymt : psymtable;offset : longint);
  48. function speedsearch(const s : stringid;speedvalue : longint) : psymentry;virtual;
  49. procedure allsymbolsused;
  50. procedure allprivatesused;
  51. procedure allunitsused;
  52. procedure check_forwards;
  53. procedure checklabels;
  54. { change alignment for args only parasymtable }
  55. procedure set_alignment(_alignment : longint);
  56. {$ifdef CHAINPROCSYMS}
  57. procedure chainprocsyms;
  58. {$endif CHAINPROCSYMS}
  59. {$ifndef DONOTCHAINOPERATORS}
  60. procedure chainoperators;
  61. {$endif DONOTCHAINOPERATORS}
  62. procedure load_browser;
  63. procedure write_browser;
  64. {$ifdef GDB}
  65. procedure concatstabto(asmlist : paasmoutput);virtual;
  66. function getnewtypecount : word; virtual;
  67. {$endif GDB}
  68. end;
  69. punitsymtable = ^tunitsymtable;
  70. tunitsymtable = object(tstoredsymtable)
  71. unittypecount : word;
  72. unitsym : punitsym;
  73. {$ifdef GDB}
  74. dbx_count : longint;
  75. prev_dbx_counter : plongint;
  76. dbx_count_ok : boolean;
  77. is_stab_written : boolean;
  78. {$endif GDB}
  79. constructor init(t : tsymtabletype;const n : string);
  80. constructor loadasunit;
  81. destructor done;virtual;
  82. procedure writeasunit;
  83. {$ifdef GDB}
  84. procedure concattypestabto(asmlist : paasmoutput);
  85. function getnewtypecount : word; virtual;
  86. {$endif GDB}
  87. procedure load_symtable_refs;
  88. end;
  89. pwithsymtable = ^twithsymtable;
  90. twithsymtable = object(tsymtable)
  91. { used for withsymtable for allowing constructors }
  92. direct_with : boolean;
  93. { in fact it is a ptree }
  94. withnode : pointer;
  95. { ptree to load of direct with var }
  96. { already usable before firstwith
  97. needed for firstpass of function parameters PM }
  98. withrefnode : pointer;
  99. constructor init;
  100. destructor done;virtual;
  101. procedure clear;virtual;
  102. end;
  103. var
  104. srsym : psym; { result of the last search }
  105. srsymtable : psymtable;
  106. lastsrsym : psym; { last sym found in statement }
  107. lastsrsymtable : psymtable;
  108. lastsymknown : boolean;
  109. constsymtable : psymtable; { symtable were the constants can be inserted }
  110. systemunit : punitsymtable; { pointer to the system unit }
  111. read_member : boolean; { reading members of an symtable }
  112. lexlevel : longint; { level of code }
  113. { 1 for main procedure }
  114. { 2 for normal function or proc }
  115. { higher for locals }
  116. {****************************************************************************
  117. Functions
  118. ****************************************************************************}
  119. {*** Misc ***}
  120. function globaldef(const s : string) : pdef;
  121. function findunitsymtable(st:psymtable):psymtable;
  122. procedure duplicatesym(sym:psym);
  123. {*** Search ***}
  124. function search_a_symtable(const symbol:string;symtabletype:tsymtabletype):Psym;
  125. procedure getsym(const s : stringid;notfounderror : boolean);
  126. procedure getsymonlyin(p : psymtable;const s : stringid);
  127. {*** PPU Write/Loading ***}
  128. procedure writeunitas(const s : string;unittable : punitsymtable;only_crc : boolean);
  129. procedure numberunits;
  130. procedure load_interface;
  131. {*** Object Helpers ***}
  132. function search_class_member(pd : pobjectdef;const n : string) : psym;
  133. function search_default_property(pd : pobjectdef) : ppropertysym;
  134. {*** symtable stack ***}
  135. procedure dellexlevel;
  136. procedure RestoreUnitSyms;
  137. {$ifdef DEBUG}
  138. procedure test_symtablestack;
  139. procedure list_symtablestack;
  140. {$endif DEBUG}
  141. {$ifdef UNITALIASES}
  142. type
  143. punit_alias = ^tunit_alias;
  144. tunit_alias = object(tnamedindexobject)
  145. newname : pstring;
  146. constructor init(const n:string);
  147. destructor done;virtual;
  148. end;
  149. var
  150. unitaliases : pdictionary;
  151. procedure addunitalias(const n:string);
  152. function getunitalias(const n:string):string;
  153. {$endif UNITALIASES}
  154. {*** Init / Done ***}
  155. procedure InitSymtable;
  156. procedure DoneSymtable;
  157. const
  158. { last operator which can be overloaded }
  159. first_overloaded = _PLUS;
  160. last_overloaded = _ASSIGNMENT;
  161. type
  162. toverloaded_operators = array[first_overloaded..last_overloaded] of pprocsym;
  163. var
  164. overloaded_operators : toverloaded_operators;
  165. { unequal is not equal}
  166. const
  167. overloaded_names : array [first_overloaded..last_overloaded] of string[16] =
  168. ('plus','minus','star','slash','equal',
  169. 'greater','lower','greater_or_equal',
  170. 'lower_or_equal',
  171. 'sym_diff','starstar',
  172. 'as','is','in','or',
  173. 'and','div','mod','not','shl','shr','xor',
  174. 'assign');
  175. implementation
  176. uses
  177. { global }
  178. version,verbose,globals,
  179. { target }
  180. systems,
  181. { ppu }
  182. symppu,ppu,
  183. { module }
  184. finput,fmodule,
  185. {$ifdef GDB}
  186. gdb,
  187. {$endif GDB}
  188. { scanner }
  189. scanner,
  190. { codegen }
  191. hcodegen { remove !!! }
  192. ;
  193. var
  194. in_loading : boolean; { remove !!! }
  195. {*****************************************************************************
  196. Symbol Call Back Functions
  197. *****************************************************************************}
  198. procedure write_refs(sym : pnamedindexobject);
  199. begin
  200. pstoredsym(sym)^.write_references;
  201. end;
  202. procedure derefsym(p : pnamedindexobject);
  203. begin
  204. psym(p)^.deref;
  205. end;
  206. procedure check_forward(sym : pnamedindexobject);
  207. begin
  208. if psym(sym)^.typ=procsym then
  209. pprocsym(sym)^.check_forward
  210. { check also object method table }
  211. { we needn't to test the def list }
  212. { because each object has to have a type sym }
  213. else
  214. if (psym(sym)^.typ=typesym) and
  215. assigned(ptypesym(sym)^.restype.def) and
  216. (ptypesym(sym)^.restype.def^.deftype=objectdef) then
  217. pobjectdef(ptypesym(sym)^.restype.def)^.check_forwards;
  218. end;
  219. procedure labeldefined(p : pnamedindexobject);
  220. begin
  221. if (psym(p)^.typ=labelsym) and
  222. not(plabelsym(p)^.defined) then
  223. begin
  224. if plabelsym(p)^.used then
  225. Message1(sym_e_label_used_and_not_defined,p^.name)
  226. else
  227. Message1(sym_w_label_not_defined,p^.name);
  228. end;
  229. end;
  230. procedure unitsymbolused(p : pnamedindexobject);
  231. begin
  232. if (psym(p)^.typ=unitsym) and
  233. (punitsym(p)^.refs=0) and
  234. { do not claim for unit name itself !! }
  235. (punitsym(p)^.unitsymtable^.symtabletype=unitsymtable) then
  236. MessagePos2(psym(p)^.fileinfo,sym_n_unit_not_used,
  237. p^.name,current_module^.modulename^);
  238. end;
  239. procedure varsymbolused(p : pnamedindexobject);
  240. begin
  241. if (psym(p)^.typ=varsym) and
  242. ((psym(p)^.owner^.symtabletype in
  243. [parasymtable,localsymtable,objectsymtable,staticsymtable])) then
  244. begin
  245. { unused symbol should be reported only if no }
  246. { error is reported }
  247. { if the symbol is in a register it is used }
  248. { also don't count the value parameters which have local copies }
  249. { also don't claim for high param of open parameters (PM) }
  250. if (Errorcount<>0) or
  251. (copy(p^.name,1,3)='val') or
  252. (copy(p^.name,1,4)='high') then
  253. exit;
  254. if (pvarsym(p)^.refs=0) then
  255. begin
  256. if (psym(p)^.owner^.symtabletype=parasymtable) or (vo_is_local_copy in pvarsym(p)^.varoptions) then
  257. begin
  258. MessagePos1(psym(p)^.fileinfo,sym_h_para_identifier_not_used,p^.name);
  259. end
  260. else if (psym(p)^.owner^.symtabletype=objectsymtable) then
  261. MessagePos2(psym(p)^.fileinfo,sym_n_private_identifier_not_used,psym(p)^.owner^.name^,p^.name)
  262. else
  263. MessagePos1(psym(p)^.fileinfo,sym_n_local_identifier_not_used,p^.name);
  264. end
  265. else if pvarsym(p)^.varstate=vs_assigned then
  266. begin
  267. if (psym(p)^.owner^.symtabletype=parasymtable) then
  268. begin
  269. if (pvarsym(p)^.varspez<>vs_var) then
  270. MessagePos1(psym(p)^.fileinfo,sym_h_para_identifier_only_set,p^.name)
  271. end
  272. else if (vo_is_local_copy in pvarsym(p)^.varoptions) then
  273. begin
  274. if (pvarsym(p)^.varspez<>vs_var) then
  275. MessagePos1(psym(p)^.fileinfo,sym_h_para_identifier_only_set,p^.name);
  276. end
  277. else if (psym(p)^.owner^.symtabletype=objectsymtable) then
  278. MessagePos2(psym(p)^.fileinfo,sym_n_private_identifier_only_set,psym(p)^.owner^.name^,p^.name)
  279. else if (psym(p)^.owner^.symtabletype<>parasymtable) then
  280. if not (vo_is_exported in pvarsym(p)^.varoptions) then
  281. MessagePos1(psym(p)^.fileinfo,sym_n_local_identifier_only_set,p^.name);
  282. end;
  283. end
  284. else if ((psym(p)^.owner^.symtabletype in
  285. [objectsymtable,parasymtable,localsymtable,staticsymtable])) then
  286. begin
  287. if (Errorcount<>0) then
  288. exit;
  289. { do not claim for inherited private fields !! }
  290. if (pstoredsym(p)^.refs=0) and (psym(p)^.owner^.symtabletype=objectsymtable) then
  291. MessagePos2(psym(p)^.fileinfo,sym_n_private_method_not_used,psym(p)^.owner^.name^,p^.name)
  292. { units references are problematic }
  293. else if (pstoredsym(p)^.refs=0) and not(psym(p)^.typ in [funcretsym,enumsym,unitsym]) then
  294. if (psym(p)^.typ<>procsym) or not (pprocsym(p)^.is_global) or
  295. { all program functions are declared global
  296. but unused should still be signaled PM }
  297. ((psym(p)^.owner^.symtabletype=staticsymtable) and
  298. not current_module^.is_unit) then
  299. MessagePos2(psym(p)^.fileinfo,sym_h_local_symbol_not_used,SymTypeName[psym(p)^.typ],p^.name);
  300. end;
  301. end;
  302. procedure TestPrivate(p : pnamedindexobject);
  303. begin
  304. if sp_private in psym(p)^.symoptions then
  305. varsymbolused(p);
  306. end;
  307. procedure objectprivatesymbolused(p : pnamedindexobject);
  308. begin
  309. {
  310. Don't test simple object aliases PM
  311. }
  312. if (psym(p)^.typ=typesym) and
  313. (ptypesym(p)^.restype.def^.deftype=objectdef) and
  314. (ptypesym(p)^.restype.def^.typesym=psym(p)) then
  315. pobjectdef(ptypesym(p)^.restype.def)^.symtable^.foreach(
  316. {$ifdef FPCPROCVAR}@{$endif}TestPrivate);
  317. end;
  318. {$ifdef GDB}
  319. var
  320. asmoutput : paasmoutput;
  321. procedure concatstab(p : pnamedindexobject);
  322. begin
  323. if psym(p)^.typ <> procsym then
  324. pstoredsym(p)^.concatstabto(asmoutput);
  325. end;
  326. procedure resetstab(p : pnamedindexobject);
  327. begin
  328. if psym(p)^.typ <> procsym then
  329. pstoredsym(p)^.isstabwritten:=false;
  330. end;
  331. procedure concattypestab(p : pnamedindexobject);
  332. begin
  333. if psym(p)^.typ = typesym then
  334. begin
  335. pstoredsym(p)^.isstabwritten:=false;
  336. pstoredsym(p)^.concatstabto(asmoutput);
  337. end;
  338. end;
  339. {$endif GDB}
  340. {$ifdef CHAINPROCSYMS}
  341. procedure chainprocsym(p : psym);
  342. var
  343. storesymtablestack : psymtable;
  344. begin
  345. if p^.typ=procsym then
  346. begin
  347. storesymtablestack:=symtablestack;
  348. symtablestack:=p^.owner^.next;
  349. while assigned(symtablestack) do
  350. begin
  351. { search for same procsym in other units }
  352. getsym(p^.name,false);
  353. if assigned(srsym) and (srsym^.typ=procsym) then
  354. begin
  355. pprocsym(p)^.nextprocsym:=pprocsym(srsym);
  356. symtablestack:=storesymtablestack;
  357. exit;
  358. end
  359. else if srsym=nil then
  360. symtablestack:=nil
  361. else
  362. symtablestack:=srsymtable^.next;
  363. end;
  364. symtablestack:=storesymtablestack;
  365. end;
  366. end;
  367. {$endif}
  368. {****************************************************************************
  369. STORED SYMTABLE
  370. ****************************************************************************}
  371. constructor tstoredsymtable.init(t : tsymtabletype);
  372. begin
  373. symtabletype:=t;
  374. symtablelevel:=0;
  375. defowner:=nil;
  376. unitid:=0;
  377. next:=nil;
  378. name:=nil;
  379. address_fixup:=0;
  380. datasize:=0;
  381. if t=parasymtable then
  382. dataalignment:=4
  383. else
  384. dataalignment:=1;
  385. new(symindex,init(indexgrowsize));
  386. new(defindex,init(indexgrowsize));
  387. if symtabletype<>withsymtable then
  388. begin
  389. new(symsearch,init);
  390. symsearch^.noclear:=true;
  391. end
  392. else
  393. symsearch:=nil;
  394. end;
  395. {$ifndef DONOTCHAINOPERATORS}
  396. procedure tstoredsymtable.chainoperators;
  397. var
  398. p : pprocsym;
  399. t : ttoken;
  400. def : pprocdef;
  401. storesymtablestack : psymtable;
  402. begin
  403. storesymtablestack:=symtablestack;
  404. symtablestack:=@self;
  405. make_ref:=false;
  406. for t:=first_overloaded to last_overloaded do
  407. begin
  408. p:=nil;
  409. def:=nil;
  410. overloaded_operators[t]:=nil;
  411. { each operator has a unique lowercased internal name PM }
  412. while assigned(symtablestack) do
  413. begin
  414. getsym(overloaded_names[t],false);
  415. if (t=_STARSTAR) and (srsym=nil) then
  416. begin
  417. symtablestack:=systemunit;
  418. getsym('POWER',false);
  419. end;
  420. if assigned(srsym) then
  421. begin
  422. if (srsym^.typ<>procsym) then
  423. internalerror(12344321);
  424. if assigned(p) then
  425. begin
  426. {$ifdef CHAINPROCSYMS}
  427. p^.nextprocsym:=pprocsym(srsym);
  428. {$endif CHAINPROCSYMS}
  429. def^.nextoverloaded:=pprocsym(srsym)^.definition;
  430. end
  431. else
  432. overloaded_operators[t]:=pprocsym(srsym);
  433. p:=pprocsym(srsym);
  434. def:=p^.definition;
  435. while assigned(def^.nextoverloaded) and
  436. (def^.nextoverloaded^.owner=p^.owner) do
  437. def:=def^.nextoverloaded;
  438. def^.nextoverloaded:=nil;
  439. symtablestack:=srsymtable^.next;
  440. end
  441. else
  442. begin
  443. symtablestack:=nil;
  444. {$ifdef CHAINPROCSYMS}
  445. if assigned(p) then
  446. p^.nextprocsym:=nil;
  447. {$endif CHAINPROCSYMS}
  448. end;
  449. { search for same procsym in other units }
  450. end;
  451. symtablestack:=@self;
  452. end;
  453. make_ref:=true;
  454. symtablestack:=storesymtablestack;
  455. end;
  456. {$endif DONOTCHAINOPERATORS}
  457. procedure tstoredsymtable.loaddefs;
  458. var
  459. hp : pdef;
  460. b : byte;
  461. begin
  462. { load start of definition section, which holds the amount of defs }
  463. if current_ppu^.readentry<>ibstartdefs then
  464. Message(unit_f_ppu_read_error);
  465. current_ppu^.getlongint;
  466. { read definitions }
  467. repeat
  468. b:=current_ppu^.readentry;
  469. case b of
  470. ibpointerdef : hp:=new(ppointerdef,load);
  471. ibarraydef : hp:=new(parraydef,load);
  472. iborddef : hp:=new(porddef,load);
  473. ibfloatdef : hp:=new(pfloatdef,load);
  474. ibprocdef : hp:=new(pprocdef,load);
  475. ibshortstringdef : hp:=new(pstringdef,shortload);
  476. iblongstringdef : hp:=new(pstringdef,longload);
  477. ibansistringdef : hp:=new(pstringdef,ansiload);
  478. ibwidestringdef : hp:=new(pstringdef,wideload);
  479. ibrecorddef : hp:=new(precorddef,load);
  480. ibobjectdef : hp:=new(pobjectdef,load);
  481. ibenumdef : hp:=new(penumdef,load);
  482. ibsetdef : hp:=new(psetdef,load);
  483. ibprocvardef : hp:=new(pprocvardef,load);
  484. ibfiledef : hp:=new(pfiledef,load);
  485. ibclassrefdef : hp:=new(pclassrefdef,load);
  486. ibformaldef : hp:=new(pformaldef,load);
  487. ibenddefs : break;
  488. ibend : Message(unit_f_ppu_read_error);
  489. else
  490. Message1(unit_f_ppu_invalid_entry,tostr(b));
  491. end;
  492. hp^.owner:=@self;
  493. defindex^.insert(hp);
  494. until false;
  495. end;
  496. procedure tstoredsymtable.loadsyms;
  497. var
  498. b : byte;
  499. sym : psym;
  500. begin
  501. { load start of definition section, which holds the amount of defs }
  502. if current_ppu^.readentry<>ibstartsyms then
  503. Message(unit_f_ppu_read_error);
  504. { skip amount of symbols, not used currently }
  505. current_ppu^.getlongint;
  506. { load datasize,dataalignment of this symboltable }
  507. datasize:=current_ppu^.getlongint;
  508. dataalignment:=current_ppu^.getlongint;
  509. { now read the symbols }
  510. repeat
  511. b:=current_ppu^.readentry;
  512. case b of
  513. ibtypesym : sym:=new(ptypesym,load);
  514. ibprocsym : sym:=new(pprocsym,load);
  515. ibconstsym : sym:=new(pconstsym,load);
  516. ibvarsym : sym:=new(pvarsym,load);
  517. ibfuncretsym : sym:=new(pfuncretsym,load);
  518. ibabsolutesym : sym:=new(pabsolutesym,load);
  519. ibenumsym : sym:=new(penumsym,load);
  520. ibtypedconstsym : sym:=new(ptypedconstsym,load);
  521. ibpropertysym : sym:=new(ppropertysym,load);
  522. ibunitsym : sym:=new(punitsym,load);
  523. iblabelsym : sym:=new(plabelsym,load);
  524. ibsyssym : sym:=new(psyssym,load);
  525. ibendsyms : break;
  526. ibend : Message(unit_f_ppu_read_error);
  527. else
  528. Message1(unit_f_ppu_invalid_entry,tostr(b));
  529. end;
  530. sym^.owner:=@self;
  531. symindex^.insert(sym);
  532. symsearch^.insert(sym);
  533. until false;
  534. end;
  535. procedure tstoredsymtable.writedefs;
  536. var
  537. pd : pstoreddef;
  538. begin
  539. { each definition get a number, write then the amount of defs to the
  540. ibstartdef entry }
  541. current_ppu^.putlongint(defindex^.count);
  542. current_ppu^.writeentry(ibstartdefs);
  543. { now write the definition }
  544. pd:=pstoreddef(defindex^.first);
  545. while assigned(pd) do
  546. begin
  547. pd^.write;
  548. pd:=pstoreddef(pd^.indexnext);
  549. end;
  550. { write end of definitions }
  551. current_ppu^.writeentry(ibenddefs);
  552. end;
  553. procedure tstoredsymtable.writesyms;
  554. var
  555. pd : pstoredsym;
  556. begin
  557. { each definition get a number, write then the amount of syms and the
  558. datasize to the ibsymdef entry }
  559. current_ppu^.putlongint(symindex^.count);
  560. current_ppu^.putlongint(datasize);
  561. current_ppu^.putlongint(dataalignment);
  562. current_ppu^.writeentry(ibstartsyms);
  563. { foreach is used to write all symbols }
  564. pd:=pstoredsym(symindex^.first);
  565. while assigned(pd) do
  566. begin
  567. pd^.write;
  568. pd:=pstoredsym(pd^.indexnext);
  569. end;
  570. { end of symbols }
  571. current_ppu^.writeentry(ibendsyms);
  572. end;
  573. {***********************************************
  574. Browser
  575. ***********************************************}
  576. procedure tstoredsymtable.load_browser;
  577. var
  578. b : byte;
  579. sym : pstoredsym;
  580. prdef : pstoreddef;
  581. oldrecsyms : psymtable;
  582. begin
  583. if symtabletype in [recordsymtable,objectsymtable] then
  584. begin
  585. oldrecsyms:=aktrecordsymtable;
  586. aktrecordsymtable:=@self;
  587. end;
  588. if symtabletype in [parasymtable,localsymtable] then
  589. begin
  590. oldrecsyms:=aktlocalsymtable;
  591. aktlocalsymtable:=@self;
  592. end;
  593. if symtabletype=staticppusymtable then
  594. aktstaticsymtable:=@self;
  595. b:=current_ppu^.readentry;
  596. if b <> ibbeginsymtablebrowser then
  597. Message1(unit_f_ppu_invalid_entry,tostr(b));
  598. repeat
  599. b:=current_ppu^.readentry;
  600. case b of
  601. ibsymref : begin
  602. sym:=pstoredsym(readderef);
  603. resolvesym(sym);
  604. if assigned(sym) then
  605. sym^.load_references;
  606. end;
  607. ibdefref : begin
  608. prdef:=pstoreddef(readderef);
  609. resolvedef(prdef);
  610. if assigned(prdef) then
  611. begin
  612. if prdef^.deftype<>procdef then
  613. Message(unit_f_ppu_read_error);
  614. pprocdef(prdef)^.load_references;
  615. end;
  616. end;
  617. ibendsymtablebrowser : break;
  618. else
  619. Message1(unit_f_ppu_invalid_entry,tostr(b));
  620. end;
  621. until false;
  622. if symtabletype in [recordsymtable,objectsymtable] then
  623. aktrecordsymtable:=oldrecsyms;
  624. if symtabletype in [parasymtable,localsymtable] then
  625. aktlocalsymtable:=oldrecsyms;
  626. end;
  627. procedure tstoredsymtable.write_browser;
  628. var
  629. oldrecsyms : psymtable;
  630. begin
  631. { symbol numbering for references
  632. should have been done in write PM
  633. number_symbols;
  634. number_defs; }
  635. if symtabletype in [recordsymtable,objectsymtable] then
  636. begin
  637. oldrecsyms:=aktrecordsymtable;
  638. aktrecordsymtable:=@self;
  639. end;
  640. if symtabletype in [parasymtable,localsymtable] then
  641. begin
  642. oldrecsyms:=aktlocalsymtable;
  643. aktlocalsymtable:=@self;
  644. end;
  645. current_ppu^.writeentry(ibbeginsymtablebrowser);
  646. foreach({$ifdef FPCPROCVAR}@{$endif}write_refs);
  647. current_ppu^.writeentry(ibendsymtablebrowser);
  648. if symtabletype in [recordsymtable,objectsymtable] then
  649. aktrecordsymtable:=oldrecsyms;
  650. if symtabletype in [parasymtable,localsymtable] then
  651. aktlocalsymtable:=oldrecsyms;
  652. end;
  653. {$ifdef GDB}
  654. function tstoredsymtable.getnewtypecount : word;
  655. begin
  656. getnewtypecount:=pglobaltypecount^;
  657. inc(pglobaltypecount^);
  658. end;
  659. {$endif GDB}
  660. procedure order_overloads(p : Pnamedindexobject);
  661. begin
  662. if psym(p)^.typ=procsym then
  663. pprocsym(p)^.order_overloaded;
  664. end;
  665. procedure tstoredsymtable.deref;
  666. var
  667. hp : pdef;
  668. hs : psym;
  669. begin
  670. { first deref the ttypesyms }
  671. hs:=psym(symindex^.first);
  672. while assigned(hs) do
  673. begin
  674. hs^.prederef;
  675. hs:=psym(hs^.indexnext);
  676. end;
  677. { deref the definitions }
  678. hp:=pdef(defindex^.first);
  679. while assigned(hp) do
  680. begin
  681. hp^.deref;
  682. hp:=pdef(hp^.indexnext);
  683. end;
  684. { deref the symbols }
  685. hs:=psym(symindex^.first);
  686. while assigned(hs) do
  687. begin
  688. hs^.deref;
  689. hs:=psym(hs^.indexnext);
  690. end;
  691. end;
  692. { this procedure is reserved for inserting case variant into
  693. a record symtable }
  694. { the offset is the location of the start of the variant
  695. and datasize and dataalignment corresponds to
  696. the complete size (see code in pdecl unit) PM }
  697. procedure tstoredsymtable.insert_in(psymt : psymtable;offset : longint);
  698. var
  699. ps,nps : pvarsym;
  700. pd,npd : pdef;
  701. storesize,storealign : longint;
  702. begin
  703. storesize:=psymt^.datasize;
  704. storealign:=psymt^.dataalignment;
  705. psymt^.datasize:=offset;
  706. ps:=pvarsym(symindex^.first);
  707. while assigned(ps) do
  708. begin
  709. { this is used to insert case variant into the main
  710. record }
  711. psymt^.datasize:=ps^.address+offset;
  712. nps:=pvarsym(ps^.indexnext);
  713. symindex^.deleteindex(ps);
  714. ps^.indexnext:=nil;
  715. ps^.left:=nil;
  716. ps^.right:=nil;
  717. psymt^.insert(ps);
  718. ps:=nps;
  719. end;
  720. pd:=pdef(defindex^.first);
  721. while assigned(pd) do
  722. begin
  723. npd:=pdef(pd^.indexnext);
  724. defindex^.deleteindex(pd);
  725. pd^.indexnext:=nil;
  726. pd^.left:=nil;
  727. pd^.right:=nil;
  728. psymt^.registerdef(pd);
  729. pd:=npd;
  730. end;
  731. psymt^.datasize:=storesize;
  732. psymt^.dataalignment:=storealign;
  733. end;
  734. constructor tstoredsymtable.loadas(typ : tsymtabletype);
  735. var
  736. storesymtable : psymtable;
  737. st_loading : boolean;
  738. begin
  739. st_loading:=in_loading;
  740. in_loading:=true;
  741. symtabletype:=typ;
  742. new(symindex,init(indexgrowsize));
  743. new(defindex,init(indexgrowsize));
  744. new(symsearch,init);
  745. symsearch^.noclear:=true;
  746. { reset }
  747. defowner:=nil;
  748. name:=nil;
  749. if typ=parasymtable then
  750. dataalignment:=4
  751. else
  752. dataalignment:=1;
  753. datasize:=0;
  754. address_fixup:= 0;
  755. unitid:=0;
  756. { setup symtabletype specific things }
  757. case typ of
  758. unitsymtable :
  759. begin
  760. symtablelevel:=0;
  761. {$ifndef NEWMAP}
  762. current_module^.map^[0]:=@self;
  763. {$else NEWMAP}
  764. current_module^.globalsymtable:=@self;
  765. {$endif NEWMAP}
  766. end;
  767. recordsymtable,
  768. objectsymtable :
  769. begin
  770. storesymtable:=aktrecordsymtable;
  771. aktrecordsymtable:=@self;
  772. end;
  773. parasymtable,
  774. localsymtable :
  775. begin
  776. storesymtable:=aktlocalsymtable;
  777. aktlocalsymtable:=@self;
  778. end;
  779. { used for local browser }
  780. staticppusymtable :
  781. begin
  782. aktstaticsymtable:=@self;
  783. symsearch^.usehash;
  784. end;
  785. end;
  786. { we need the correct symtable for registering }
  787. if not (typ in [localsymtable,parasymtable,recordsymtable,objectsymtable]) then
  788. begin
  789. next:=symtablestack;
  790. symtablestack:=@self;
  791. end;
  792. { load definitions }
  793. loaddefs;
  794. { load symbols }
  795. loadsyms;
  796. if not (typ in [localsymtable,parasymtable,recordsymtable,objectsymtable]) then
  797. begin
  798. { now we can deref the syms and defs }
  799. deref;
  800. { restore symtablestack }
  801. symtablestack:=next;
  802. end;
  803. case typ of
  804. unitsymtable :
  805. begin
  806. {$ifdef NEWMAP}
  807. { necessary for dependencies }
  808. current_module^.globalsymtable:=nil;
  809. {$endif NEWMAP}
  810. end;
  811. recordsymtable,
  812. objectsymtable :
  813. aktrecordsymtable:=storesymtable;
  814. localsymtable,
  815. parasymtable :
  816. aktlocalsymtable:=storesymtable;
  817. end;
  818. in_loading:=st_loading;
  819. end;
  820. procedure tstoredsymtable.writeas;
  821. var
  822. oldtyp : byte;
  823. storesymtable : psymtable;
  824. begin
  825. storesymtable:=aktrecordsymtable;
  826. case symtabletype of
  827. recordsymtable,
  828. objectsymtable :
  829. begin
  830. storesymtable:=aktrecordsymtable;
  831. aktrecordsymtable:=@self;
  832. oldtyp:=current_ppu^.entrytyp;
  833. current_ppu^.entrytyp:=subentryid;
  834. end;
  835. parasymtable,
  836. localsymtable :
  837. begin
  838. storesymtable:=aktlocalsymtable;
  839. aktlocalsymtable:=@self;
  840. end;
  841. end;
  842. { order procsym overloads }
  843. foreach({$ifdef FPCPROCVAR}@{$endif}Order_overloads);
  844. { write definitions }
  845. writedefs;
  846. { write symbols }
  847. writesyms;
  848. case symtabletype of
  849. recordsymtable,
  850. objectsymtable :
  851. begin
  852. current_ppu^.entrytyp:=oldtyp;
  853. aktrecordsymtable:=storesymtable;
  854. end;
  855. localsymtable,
  856. parasymtable :
  857. aktlocalsymtable:=storesymtable;
  858. end;
  859. end;
  860. procedure tstoredsymtable.insert(sym:psymentry);
  861. var
  862. hp : psymtable;
  863. hsym : psym;
  864. begin
  865. { set owner and sym indexnb }
  866. sym^.owner:=@self;
  867. {$ifdef CHAINPROCSYMS}
  868. { set the nextprocsym field }
  869. if sym^.typ=procsym then
  870. chainprocsym(sym);
  871. {$endif CHAINPROCSYMS}
  872. { writes the symbol in data segment if required }
  873. { also sets the datasize of owner }
  874. if not in_loading then
  875. pstoredsym(sym)^.insert_in_data;
  876. if (symtabletype in [staticsymtable,globalsymtable]) then
  877. begin
  878. hp:=symtablestack;
  879. while assigned(hp) do
  880. begin
  881. if hp^.symtabletype in [staticsymtable,globalsymtable] then
  882. begin
  883. hsym:=psym(hp^.search(sym^.name));
  884. if assigned(hsym) then
  885. DuplicateSym(hsym);
  886. end;
  887. hp:=hp^.next;
  888. end;
  889. end;
  890. { check the current symtable }
  891. hsym:=psym(search(sym^.name));
  892. if assigned(hsym) then
  893. begin
  894. { in TP and Delphi you can have a local with the
  895. same name as the function, the function is then hidden for
  896. the user. (Under delphi it can still be accessed using result),
  897. but don't allow hiding of RESULT }
  898. if (m_tp in aktmodeswitches) and
  899. (hsym^.typ=funcretsym) and
  900. not((m_result in aktmodeswitches) and
  901. (hsym^.name='RESULT')) then
  902. hsym^.owner^.rename(hsym^.name,'hidden'+hsym^.name)
  903. else
  904. begin
  905. DuplicateSym(hsym);
  906. exit;
  907. end;
  908. end;
  909. { check for duplicate id in local and parasymtable symtable }
  910. if (symtabletype=localsymtable) then
  911. { to be on the save side: }
  912. begin
  913. if assigned(next) and
  914. (next^.symtabletype=parasymtable) then
  915. begin
  916. hsym:=psym(next^.search(sym^.name));
  917. if assigned(hsym) then
  918. begin
  919. { a parameter and the function can have the same
  920. name in TP and Delphi, but RESULT not }
  921. if (m_tp in aktmodeswitches) and
  922. (sym^.typ=funcretsym) and
  923. not((m_result in aktmodeswitches) and
  924. (sym^.name='RESULT')) then
  925. sym^.setname('hidden'+sym^.name)
  926. else
  927. begin
  928. DuplicateSym(hsym);
  929. exit;
  930. end;
  931. end;
  932. end
  933. else if (current_module^.flags and uf_local_browser)=0 then
  934. internalerror(43789);
  935. end;
  936. { check for duplicate id in local symtable of methods }
  937. if (symtabletype=localsymtable) and
  938. assigned(next) and
  939. assigned(next^.next) and
  940. { funcretsym is allowed !! }
  941. (sym^.typ <> funcretsym) and
  942. (next^.next^.symtabletype=objectsymtable) then
  943. begin
  944. hsym:=search_class_member(pobjectdef(next^.next^.defowner),sym^.name);
  945. if assigned(hsym) and
  946. { private ids can be reused }
  947. (not(sp_private in hsym^.symoptions) or
  948. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  949. begin
  950. { delphi allows to reuse the names in a class, but not
  951. in object (tp7 compatible) }
  952. if not((m_delphi in aktmodeswitches) and
  953. (pobjectdef(next^.next^.defowner)^.is_class)) then
  954. begin
  955. DuplicateSym(hsym);
  956. exit;
  957. end;
  958. end;
  959. end;
  960. { check for duplicate id in para symtable of methods }
  961. if (symtabletype=parasymtable) and
  962. assigned(procinfo^._class) and
  963. { but not in nested procedures !}
  964. (not(assigned(procinfo^.parent)) or
  965. (assigned(procinfo^.parent) and
  966. not(assigned(procinfo^.parent^._class)))
  967. ) and
  968. { funcretsym is allowed !! }
  969. (sym^.typ <> funcretsym) then
  970. begin
  971. hsym:=search_class_member(procinfo^._class,sym^.name);
  972. if assigned(hsym) and
  973. { private ids can be reused }
  974. (not(sp_private in hsym^.symoptions) or
  975. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  976. begin
  977. { delphi allows to reuse the names in a class, but not
  978. in object (tp7 compatible) }
  979. if not((m_delphi in aktmodeswitches) and
  980. (procinfo^._class^.is_class)) then
  981. begin
  982. DuplicateSym(hsym);
  983. exit;
  984. end;
  985. end;
  986. end;
  987. { check for duplicate field id in inherited classes }
  988. if (sym^.typ=varsym) and
  989. (symtabletype=objectsymtable) and
  990. assigned(defowner) then
  991. begin
  992. hsym:=search_class_member(pobjectdef(defowner),sym^.name);
  993. { but private ids can be reused }
  994. if assigned(hsym) and
  995. (not(sp_private in hsym^.symoptions) or
  996. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  997. begin
  998. DuplicateSym(hsym);
  999. exit;
  1000. end;
  1001. end;
  1002. { register definition of typesym }
  1003. if (sym^.typ = typesym) and
  1004. assigned(ptypesym(sym)^.restype.def) then
  1005. begin
  1006. if not(assigned(ptypesym(sym)^.restype.def^.owner)) and
  1007. (ptypesym(sym)^.restype.def^.deftype<>errordef) then
  1008. registerdef(ptypesym(sym)^.restype.def);
  1009. {$ifdef GDB}
  1010. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  1011. (symtabletype in [globalsymtable,staticsymtable]) then
  1012. begin
  1013. ptypesym(sym)^.isusedinstab := true;
  1014. {sym^.concatstabto(debuglist);}
  1015. end;
  1016. {$endif GDB}
  1017. end;
  1018. { insert in index and search hash }
  1019. symindex^.insert(sym);
  1020. symsearch^.insert(sym);
  1021. end;
  1022. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : longint) : psymentry;
  1023. var
  1024. hp : pstoredsym;
  1025. newref : pref;
  1026. begin
  1027. hp:=pstoredsym(inherited speedsearch(s,speedvalue));
  1028. if assigned(hp) then
  1029. begin
  1030. { reject non static members in static procedures,
  1031. be carefull aktprocsym^.definition is not allways
  1032. loaded already (PFV) }
  1033. if (symtabletype=objectsymtable) and
  1034. not(sp_static in hp^.symoptions) and
  1035. allow_only_static
  1036. {assigned(aktprocsym) and
  1037. assigned(aktprocsym^.definition) and
  1038. ((aktprocsym^.definition^.options and postaticmethod)<>0)} then
  1039. Message(sym_e_only_static_in_static);
  1040. if (symtabletype=unitsymtable) and
  1041. assigned(punitsymtable(@self)^.unitsym) then
  1042. inc(punitsymtable(@self)^.unitsym^.refs);
  1043. { unitsym are only loaded for browsing PM }
  1044. { this was buggy anyway because we could use }
  1045. { unitsyms from other units in _USES !! }
  1046. {if (symtabletype=unitsymtable) and (hp^.typ=unitsym) and
  1047. assigned(current_module) and (current_module^.globalsymtable<>@self) then
  1048. hp:=nil;}
  1049. if assigned(hp) and
  1050. (cs_browser in aktmoduleswitches) and make_ref then
  1051. begin
  1052. new(newref,init(hp^.lastref,@akttokenpos));
  1053. { for symbols that are in tables without
  1054. browser info or syssyms (PM) }
  1055. if hp^.refcount=0 then
  1056. begin
  1057. hp^.defref:=newref;
  1058. hp^.lastref:=newref;
  1059. end
  1060. else
  1061. if resolving_forward and assigned(hp^.defref) then
  1062. { put it as second reference }
  1063. begin
  1064. newref^.nextref:=hp^.defref^.nextref;
  1065. hp^.defref^.nextref:=newref;
  1066. hp^.lastref^.nextref:=nil;
  1067. end
  1068. else
  1069. hp^.lastref:=newref;
  1070. inc(hp^.refcount);
  1071. end;
  1072. if assigned(hp) and make_ref then
  1073. begin
  1074. inc(hp^.refs);
  1075. end;
  1076. end;
  1077. speedsearch:=hp;
  1078. end;
  1079. {***********************************************
  1080. Process all entries
  1081. ***********************************************}
  1082. { checks, if all procsyms and methods are defined }
  1083. procedure tstoredsymtable.check_forwards;
  1084. begin
  1085. foreach({$ifdef FPCPROCVAR}@{$endif}check_forward);
  1086. end;
  1087. procedure tstoredsymtable.checklabels;
  1088. begin
  1089. foreach({$ifdef FPCPROCVAR}@{$endif}labeldefined);
  1090. end;
  1091. procedure tstoredsymtable.set_alignment(_alignment : longint);
  1092. var
  1093. sym : pvarsym;
  1094. l : longint;
  1095. begin
  1096. dataalignment:=_alignment;
  1097. if (symtabletype<>parasymtable) then
  1098. internalerror(1111);
  1099. sym:=pvarsym(symindex^.first);
  1100. datasize:=0;
  1101. { there can be only varsyms }
  1102. while assigned(sym) do
  1103. begin
  1104. l:=sym^.getpushsize;
  1105. sym^.address:=datasize;
  1106. datasize:=align(datasize+l,dataalignment);
  1107. sym:=pvarsym(sym^.indexnext);
  1108. end;
  1109. end;
  1110. procedure tstoredsymtable.allunitsused;
  1111. begin
  1112. foreach({$ifdef FPCPROCVAR}@{$endif}unitsymbolused);
  1113. end;
  1114. procedure tstoredsymtable.allsymbolsused;
  1115. begin
  1116. foreach({$ifdef FPCPROCVAR}@{$endif}varsymbolused);
  1117. end;
  1118. procedure tstoredsymtable.allprivatesused;
  1119. begin
  1120. foreach({$ifdef FPCPROCVAR}@{$endif}objectprivatesymbolused);
  1121. end;
  1122. {$ifdef CHAINPROCSYMS}
  1123. procedure tstoredsymtable.chainprocsyms;
  1124. begin
  1125. foreach({$ifdef FPCPROCVAR}@{$endif}chainprocsym);
  1126. end;
  1127. {$endif CHAINPROCSYMS}
  1128. {$ifdef GDB}
  1129. procedure tstoredsymtable.concatstabto(asmlist : paasmoutput);
  1130. begin
  1131. asmoutput:=asmlist;
  1132. if symtabletype in [inlineparasymtable,inlinelocalsymtable] then
  1133. foreach({$ifdef FPCPROCVAR}@{$endif}resetstab);
  1134. foreach({$ifdef FPCPROCVAR}@{$endif}concatstab);
  1135. end;
  1136. {$endif}
  1137. {****************************************************************************
  1138. TWITHSYMTABLE
  1139. ****************************************************************************}
  1140. constructor twithsymtable.init;
  1141. begin
  1142. inherited init(withsymtable);
  1143. direct_with:=false;
  1144. withnode:=nil;
  1145. withrefnode:=nil;
  1146. end;
  1147. destructor twithsymtable.done;
  1148. begin
  1149. symsearch:=nil;
  1150. inherited done;
  1151. end;
  1152. procedure twithsymtable.clear;
  1153. begin
  1154. { remove no entry from a withsymtable as it is only a pointer to the
  1155. recorddef or objectdef symtable }
  1156. end;
  1157. {****************************************************************************
  1158. PPU Writing Helpers
  1159. ****************************************************************************}
  1160. procedure writesourcefiles;
  1161. var
  1162. hp : pinputfile;
  1163. i,j : longint;
  1164. begin
  1165. { second write the used source files }
  1166. current_ppu^.do_crc:=false;
  1167. hp:=current_module^.sourcefiles^.files;
  1168. { write source files directly in good order }
  1169. j:=0;
  1170. while assigned(hp) do
  1171. begin
  1172. inc(j);
  1173. hp:=hp^.ref_next;
  1174. end;
  1175. while j>0 do
  1176. begin
  1177. hp:=current_module^.sourcefiles^.files;
  1178. for i:=1 to j-1 do
  1179. hp:=hp^.ref_next;
  1180. current_ppu^.putstring(hp^.name^);
  1181. dec(j);
  1182. end;
  1183. current_ppu^.writeentry(ibsourcefiles);
  1184. current_ppu^.do_crc:=true;
  1185. end;
  1186. procedure writeusedmacro(p:pnamedindexobject);
  1187. begin
  1188. if pmacro(p)^.is_used or pmacro(p)^.defined_at_startup then
  1189. begin
  1190. current_ppu^.putstring(p^.name);
  1191. current_ppu^.putbyte(byte(pmacro(p)^.defined_at_startup));
  1192. current_ppu^.putbyte(byte(pmacro(p)^.is_used));
  1193. end;
  1194. end;
  1195. procedure writeusedmacros;
  1196. begin
  1197. current_ppu^.do_crc:=false;
  1198. current_scanner^.macros^.foreach({$ifdef FPCPROCVAR}@{$endif}writeusedmacro);
  1199. current_ppu^.writeentry(ibusedmacros);
  1200. current_ppu^.do_crc:=true;
  1201. end;
  1202. procedure writeusedunit;
  1203. var
  1204. hp : pused_unit;
  1205. begin
  1206. numberunits;
  1207. hp:=pused_unit(current_module^.used_units.first);
  1208. while assigned(hp) do
  1209. begin
  1210. { implementation units should not change
  1211. the CRC PM }
  1212. current_ppu^.do_crc:=hp^.in_interface;
  1213. current_ppu^.putstring(hp^.name^);
  1214. { the checksum should not affect the crc of this unit ! (PFV) }
  1215. current_ppu^.do_crc:=false;
  1216. current_ppu^.putlongint(hp^.checksum);
  1217. current_ppu^.putlongint(hp^.interface_checksum);
  1218. current_ppu^.putbyte(byte(hp^.in_interface));
  1219. current_ppu^.do_crc:=true;
  1220. hp:=pused_unit(hp^.next);
  1221. end;
  1222. current_ppu^.do_interface_crc:=true;
  1223. current_ppu^.writeentry(ibloadunit);
  1224. end;
  1225. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  1226. var
  1227. hcontainer : tlinkcontainer;
  1228. s : string;
  1229. mask : longint;
  1230. begin
  1231. hcontainer.init;
  1232. while not p.empty do
  1233. begin
  1234. s:=p.get(mask);
  1235. if strippath then
  1236. current_ppu^.putstring(SplitFileName(s))
  1237. else
  1238. current_ppu^.putstring(s);
  1239. current_ppu^.putlongint(mask);
  1240. hcontainer.insert(s,mask);
  1241. end;
  1242. current_ppu^.writeentry(id);
  1243. p:=hcontainer;
  1244. end;
  1245. procedure writeunitas(const s : string;unittable : punitsymtable;only_crc : boolean);
  1246. begin
  1247. Message1(unit_u_ppu_write,s);
  1248. { create unit flags }
  1249. with Current_Module^ do
  1250. begin
  1251. {$ifdef GDB}
  1252. if cs_gdb_dbx in aktglobalswitches then
  1253. flags:=flags or uf_has_dbx;
  1254. {$endif GDB}
  1255. if target_os.endian=endian_big then
  1256. flags:=flags or uf_big_endian;
  1257. if cs_browser in aktmoduleswitches then
  1258. flags:=flags or uf_has_browser;
  1259. if cs_local_browser in aktmoduleswitches then
  1260. flags:=flags or uf_local_browser;
  1261. end;
  1262. {$ifdef Test_Double_checksum_write}
  1263. If only_crc then
  1264. Assign(CRCFile,s+'.INT')
  1265. else
  1266. Assign(CRCFile,s+'.IMP');
  1267. Rewrite(CRCFile);
  1268. {$endif def Test_Double_checksum_write}
  1269. { open ppufile }
  1270. current_ppu:=new(pppufile,init(s));
  1271. current_ppu^.crc_only:=only_crc;
  1272. if not current_ppu^.create then
  1273. Message(unit_f_ppu_cannot_write);
  1274. {$ifdef Test_Double_checksum}
  1275. if only_crc then
  1276. begin
  1277. new(current_ppu^.crc_test);
  1278. new(current_ppu^.crc_test2);
  1279. end
  1280. else
  1281. begin
  1282. current_ppu^.crc_test:=Current_Module^.crc_array;
  1283. current_ppu^.crc_index:=Current_Module^.crc_size;
  1284. current_ppu^.crc_test2:=Current_Module^.crc_array2;
  1285. current_ppu^.crc_index2:=Current_Module^.crc_size2;
  1286. end;
  1287. {$endif def Test_Double_checksum}
  1288. current_ppu^.change_endian:=source_os.endian<>target_os.endian;
  1289. { write symbols and definitions }
  1290. unittable^.writeasunit;
  1291. { flush to be sure }
  1292. current_ppu^.flush;
  1293. { create and write header }
  1294. current_ppu^.header.size:=current_ppu^.size;
  1295. current_ppu^.header.checksum:=current_ppu^.crc;
  1296. current_ppu^.header.interface_checksum:=current_ppu^.interface_crc;
  1297. current_ppu^.header.compiler:=wordversion;
  1298. current_ppu^.header.cpu:=word(target_cpu);
  1299. current_ppu^.header.target:=word(target_info.target);
  1300. current_ppu^.header.flags:=current_module^.flags;
  1301. If not only_crc then
  1302. current_ppu^.writeheader;
  1303. { save crc in current_module also }
  1304. current_module^.crc:=current_ppu^.crc;
  1305. current_module^.interface_crc:=current_ppu^.interface_crc;
  1306. if only_crc then
  1307. begin
  1308. {$ifdef Test_Double_checksum}
  1309. Current_Module^.crc_array:=current_ppu^.crc_test;
  1310. current_ppu^.crc_test:=nil;
  1311. Current_Module^.crc_size:=current_ppu^.crc_index2;
  1312. Current_Module^.crc_array2:=current_ppu^.crc_test2;
  1313. current_ppu^.crc_test2:=nil;
  1314. Current_Module^.crc_size2:=current_ppu^.crc_index2;
  1315. {$endif def Test_Double_checksum}
  1316. closecurrentppu;
  1317. end;
  1318. {$ifdef Test_Double_checksum_write}
  1319. close(CRCFile);
  1320. {$endif Test_Double_checksum_write}
  1321. end;
  1322. procedure readusedmacros;
  1323. var
  1324. hs : string;
  1325. mac : pmacro;
  1326. was_defined_at_startup,
  1327. was_used : boolean;
  1328. begin
  1329. while not current_ppu^.endofentry do
  1330. begin
  1331. hs:=current_ppu^.getstring;
  1332. was_defined_at_startup:=boolean(current_ppu^.getbyte);
  1333. was_used:=boolean(current_ppu^.getbyte);
  1334. mac:=pmacro(current_scanner^.macros^.search(hs));
  1335. if assigned(mac) then
  1336. begin
  1337. {$ifndef EXTDEBUG}
  1338. { if we don't have the sources why tell }
  1339. if current_module^.sources_avail then
  1340. {$endif ndef EXTDEBUG}
  1341. if (not was_defined_at_startup) and
  1342. was_used and
  1343. mac^.defined_at_startup then
  1344. Message2(unit_h_cond_not_set_in_last_compile,hs,current_module^.mainsource^);
  1345. end
  1346. else { not assigned }
  1347. if was_defined_at_startup and
  1348. was_used then
  1349. Message2(unit_h_cond_not_set_in_last_compile,hs,current_module^.mainsource^);
  1350. end;
  1351. end;
  1352. procedure readsourcefiles;
  1353. var
  1354. temp,hs : string;
  1355. temp_dir : string;
  1356. main_dir : string;
  1357. incfile_found,
  1358. main_found,
  1359. is_main : boolean;
  1360. ppufiletime,
  1361. source_time : longint;
  1362. hp : pinputfile;
  1363. begin
  1364. ppufiletime:=getnamedfiletime(current_module^.ppufilename^);
  1365. current_module^.sources_avail:=true;
  1366. is_main:=true;
  1367. main_dir:='';
  1368. while not current_ppu^.endofentry do
  1369. begin
  1370. hs:=current_ppu^.getstring;
  1371. temp_dir:='';
  1372. if (current_module^.flags and uf_in_library)<>0 then
  1373. begin
  1374. current_module^.sources_avail:=false;
  1375. temp:=' library';
  1376. end
  1377. else if pos('Macro ',hs)=1 then
  1378. begin
  1379. { we don't want to find this file }
  1380. { but there is a problem with file indexing !! }
  1381. temp:='';
  1382. end
  1383. else
  1384. begin
  1385. { check the date of the source files }
  1386. Source_Time:=GetNamedFileTime(current_module^.path^+hs);
  1387. incfile_found:=false;
  1388. main_found:=false;
  1389. if Source_Time<>-1 then
  1390. hs:=current_module^.path^+hs
  1391. else
  1392. if not(is_main) then
  1393. begin
  1394. Source_Time:=GetNamedFileTime(main_dir+hs);
  1395. if Source_Time<>-1 then
  1396. hs:=main_dir+hs;
  1397. end;
  1398. if (Source_Time=-1) then
  1399. begin
  1400. if is_main then
  1401. temp_dir:=unitsearchpath.FindFile(hs,main_found)
  1402. else
  1403. temp_dir:=includesearchpath.FindFile(hs,incfile_found);
  1404. if incfile_found or main_found then
  1405. begin
  1406. hs:=temp_dir+hs;
  1407. Source_Time:=GetNamedFileTime(hs);
  1408. end
  1409. end;
  1410. if Source_Time=-1 then
  1411. begin
  1412. current_module^.sources_avail:=false;
  1413. temp:=' not found';
  1414. end
  1415. else
  1416. begin
  1417. if main_found then
  1418. main_dir:=temp_dir;
  1419. { time newer? But only allow if the file is not searched
  1420. in the include path (PFV), else you've problems with
  1421. units which use the same includefile names }
  1422. if incfile_found then
  1423. temp:=' found'
  1424. else
  1425. begin
  1426. temp:=' time '+filetimestring(source_time);
  1427. if (source_time>ppufiletime) then
  1428. begin
  1429. current_module^.do_compile:=true;
  1430. current_module^.recompile_reason:=rr_sourcenewer;
  1431. temp:=temp+' *'
  1432. end;
  1433. end;
  1434. end;
  1435. new(hp,init(hs));
  1436. { the indexing is wrong here PM }
  1437. current_module^.sourcefiles^.register_file(hp);
  1438. end;
  1439. if is_main then
  1440. begin
  1441. stringdispose(current_module^.mainsource);
  1442. current_module^.mainsource:=stringdup(hs);
  1443. end;
  1444. Message1(unit_u_ppu_source,hs+temp);
  1445. is_main:=false;
  1446. end;
  1447. { check if we want to rebuild every unit, only if the sources are
  1448. available }
  1449. if do_build and current_module^.sources_avail then
  1450. begin
  1451. current_module^.do_compile:=true;
  1452. current_module^.recompile_reason:=rr_build;
  1453. end;
  1454. end;
  1455. procedure readloadunit;
  1456. var
  1457. hs : string;
  1458. intfchecksum,
  1459. checksum : longint;
  1460. in_interface : boolean;
  1461. begin
  1462. while not current_ppu^.endofentry do
  1463. begin
  1464. hs:=current_ppu^.getstring;
  1465. checksum:=current_ppu^.getlongint;
  1466. intfchecksum:=current_ppu^.getlongint;
  1467. in_interface:=(current_ppu^.getbyte<>0);
  1468. current_module^.used_units.concat(new(pused_unit,init_to_load(hs,checksum,intfchecksum,in_interface)));
  1469. end;
  1470. end;
  1471. procedure readlinkcontainer(var p:tlinkcontainer);
  1472. var
  1473. s : string;
  1474. m : longint;
  1475. begin
  1476. while not current_ppu^.endofentry do
  1477. begin
  1478. s:=current_ppu^.getstring;
  1479. m:=current_ppu^.getlongint;
  1480. p.insert(s,m);
  1481. end;
  1482. end;
  1483. procedure load_interface;
  1484. var
  1485. b : byte;
  1486. newmodulename : string;
  1487. begin
  1488. { read interface part }
  1489. repeat
  1490. b:=current_ppu^.readentry;
  1491. case b of
  1492. ibmodulename :
  1493. begin
  1494. newmodulename:=current_ppu^.getstring;
  1495. if upper(newmodulename)<>current_module^.modulename^ then
  1496. Message2(unit_f_unit_name_error,current_module^.realmodulename^,newmodulename);
  1497. stringdispose(current_module^.modulename);
  1498. stringdispose(current_module^.realmodulename);
  1499. current_module^.modulename:=stringdup(upper(newmodulename));
  1500. current_module^.realmodulename:=stringdup(newmodulename);
  1501. end;
  1502. ibsourcefiles :
  1503. readsourcefiles;
  1504. ibusedmacros :
  1505. readusedmacros;
  1506. ibloadunit :
  1507. readloadunit;
  1508. iblinkunitofiles :
  1509. readlinkcontainer(current_module^.LinkUnitOFiles);
  1510. iblinkunitstaticlibs :
  1511. readlinkcontainer(current_module^.LinkUnitStaticLibs);
  1512. iblinkunitsharedlibs :
  1513. readlinkcontainer(current_module^.LinkUnitSharedLibs);
  1514. iblinkotherofiles :
  1515. readlinkcontainer(current_module^.LinkotherOFiles);
  1516. iblinkotherstaticlibs :
  1517. readlinkcontainer(current_module^.LinkotherStaticLibs);
  1518. iblinkothersharedlibs :
  1519. readlinkcontainer(current_module^.LinkotherSharedLibs);
  1520. ibendinterface :
  1521. break;
  1522. else
  1523. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1524. end;
  1525. until false;
  1526. end;
  1527. {****************************************************************************
  1528. TUNITSYMTABLE
  1529. ****************************************************************************}
  1530. constructor tunitsymtable.init(t : tsymtabletype; const n : string);
  1531. begin
  1532. inherited init(t);
  1533. name:=stringdup(upper(n));
  1534. unitid:=0;
  1535. unitsym:=nil;
  1536. symsearch^.usehash;
  1537. { reset GDB things }
  1538. {$ifdef GDB}
  1539. if (t = globalsymtable) then
  1540. begin
  1541. prev_dbx_counter := dbx_counter;
  1542. dbx_counter := nil;
  1543. end;
  1544. is_stab_written:=false;
  1545. dbx_count := -1;
  1546. if cs_gdb_dbx in aktglobalswitches then
  1547. begin
  1548. dbx_count := 0;
  1549. unittypecount:=1;
  1550. if (symtabletype=globalsymtable) then
  1551. pglobaltypecount := @unittypecount;
  1552. unitid:=current_module^.unitcount;
  1553. debuglist^.concat(new(pai_asm_comment,init(strpnew('Global '+name^+' has index '+tostr(unitid)))));
  1554. debuglist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0'))));
  1555. inc(current_module^.unitcount);
  1556. dbx_count_ok:=false;
  1557. dbx_counter:=@dbx_count;
  1558. do_count_dbx:=true;
  1559. end;
  1560. {$endif GDB}
  1561. end;
  1562. constructor tunitsymtable.loadasunit;
  1563. var
  1564. {$ifdef GDB}
  1565. storeGlobalTypeCount : pword;
  1566. {$endif GDB}
  1567. b : byte;
  1568. begin
  1569. unitsym:=nil;
  1570. unitid:=0;
  1571. {$ifdef GDB}
  1572. if cs_gdb_dbx in aktglobalswitches then
  1573. begin
  1574. UnitTypeCount:=1;
  1575. storeGlobalTypeCount:=PGlobalTypeCount;
  1576. PglobalTypeCount:=@UnitTypeCount;
  1577. end;
  1578. {$endif GDB}
  1579. { load symtables }
  1580. inherited loadas(unitsymtable);
  1581. { set the name after because it is set to nil in tstoredsymtable.load !! }
  1582. name:=stringdup(current_module^.modulename^);
  1583. { dbx count }
  1584. {$ifdef GDB}
  1585. if (current_module^.flags and uf_has_dbx)<>0 then
  1586. begin
  1587. b := current_ppu^.readentry;
  1588. if b <> ibdbxcount then
  1589. Message(unit_f_ppu_dbx_count_problem)
  1590. else
  1591. dbx_count := readlong;
  1592. dbx_count_ok := {true}false;
  1593. end
  1594. else
  1595. begin
  1596. dbx_count := -1;
  1597. dbx_count_ok:=false;
  1598. end;
  1599. if cs_gdb_dbx in aktglobalswitches then
  1600. PGlobalTypeCount:=storeGlobalTypeCount;
  1601. is_stab_written:=false;
  1602. {$endif GDB}
  1603. b:=current_ppu^.readentry;
  1604. if b<>ibendimplementation then
  1605. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1606. end;
  1607. destructor tunitsymtable.done;
  1608. var
  1609. pus : punitsym;
  1610. begin
  1611. pus:=unitsym;
  1612. while assigned(pus) do
  1613. begin
  1614. unitsym:=pus^.prevsym;
  1615. pus^.prevsym:=nil;
  1616. pus^.unitsymtable:=nil;
  1617. pus:=unitsym;
  1618. end;
  1619. inherited done;
  1620. end;
  1621. procedure tunitsymtable.load_symtable_refs;
  1622. var
  1623. b : byte;
  1624. unitindex : word;
  1625. begin
  1626. if ((current_module^.flags and uf_local_browser)<>0) then
  1627. begin
  1628. current_module^.localsymtable:=new(punitsymtable,loadas(staticppusymtable));
  1629. psymtable(current_module^.localsymtable)^.name:=
  1630. stringdup('implementation of '+psymtable(current_module^.globalsymtable)^.name^);
  1631. end;
  1632. { load browser }
  1633. if (current_module^.flags and uf_has_browser)<>0 then
  1634. begin
  1635. {if not (cs_browser in aktmoduleswitches) then
  1636. current_ppu^.skipuntilentry(ibendbrowser)
  1637. else }
  1638. begin
  1639. load_browser;
  1640. unitindex:=1;
  1641. while assigned(current_module^.map^[unitindex]) do
  1642. begin
  1643. {each unit wrote one browser entry }
  1644. load_browser;
  1645. inc(unitindex);
  1646. end;
  1647. b:=current_ppu^.readentry;
  1648. if b<>ibendbrowser then
  1649. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1650. end;
  1651. end;
  1652. if ((current_module^.flags and uf_local_browser)<>0) then
  1653. pstoredsymtable(current_module^.localsymtable)^.load_browser;
  1654. end;
  1655. procedure tunitsymtable.writeasunit;
  1656. var
  1657. pu : pused_unit;
  1658. begin
  1659. { first the unitname }
  1660. current_ppu^.putstring(current_module^.realmodulename^);
  1661. current_ppu^.writeentry(ibmodulename);
  1662. writesourcefiles;
  1663. writeusedmacros;
  1664. writeusedunit;
  1665. { write the objectfiles and libraries that come for this unit,
  1666. preserve the containers becuase they are still needed to load
  1667. the link.res. All doesn't depend on the crc! It doesn't matter
  1668. if a unit is in a .o or .a file }
  1669. current_ppu^.do_crc:=false;
  1670. writelinkcontainer(current_module^.linkunitofiles,iblinkunitofiles,true);
  1671. writelinkcontainer(current_module^.linkunitstaticlibs,iblinkunitstaticlibs,true);
  1672. writelinkcontainer(current_module^.linkunitsharedlibs,iblinkunitsharedlibs,true);
  1673. writelinkcontainer(current_module^.linkotherofiles,iblinkotherofiles,false);
  1674. writelinkcontainer(current_module^.linkotherstaticlibs,iblinkotherstaticlibs,true);
  1675. writelinkcontainer(current_module^.linkothersharedlibs,iblinkothersharedlibs,true);
  1676. current_ppu^.do_crc:=true;
  1677. current_ppu^.writeentry(ibendinterface);
  1678. { write the symtable entries }
  1679. inherited writeas;
  1680. { all after doesn't affect crc }
  1681. current_ppu^.do_crc:=false;
  1682. { write dbx count }
  1683. {$ifdef GDB}
  1684. if cs_gdb_dbx in aktglobalswitches then
  1685. begin
  1686. {$IfDef EXTDEBUG}
  1687. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1688. {$ENDIF EXTDEBUG}
  1689. current_ppu^.putlongint(dbx_count);
  1690. current_ppu^.writeentry(ibdbxcount);
  1691. end;
  1692. {$endif GDB}
  1693. current_ppu^.writeentry(ibendimplementation);
  1694. { write static symtable
  1695. needed for local debugging of unit functions }
  1696. if ((current_module^.flags and uf_local_browser)<>0) and
  1697. assigned(current_module^.localsymtable) then
  1698. pstoredsymtable(current_module^.localsymtable)^.writeas;
  1699. { write all browser section }
  1700. if (current_module^.flags and uf_has_browser)<>0 then
  1701. begin
  1702. write_browser;
  1703. pu:=pused_unit(current_module^.used_units.first);
  1704. while assigned(pu) do
  1705. begin
  1706. pstoredsymtable(pu^.u^.globalsymtable)^.write_browser;
  1707. pu:=pused_unit(pu^.next);
  1708. end;
  1709. current_ppu^.writeentry(ibendbrowser);
  1710. end;
  1711. if ((current_module^.flags and uf_local_browser)<>0) and
  1712. assigned(current_module^.localsymtable) then
  1713. pstoredsymtable(current_module^.localsymtable)^.write_browser;
  1714. { the last entry ibend is written automaticly }
  1715. end;
  1716. {$ifdef GDB}
  1717. function tunitsymtable.getnewtypecount : word;
  1718. begin
  1719. if not (cs_gdb_dbx in aktglobalswitches) then
  1720. getnewtypecount:=tsymtable.getnewtypecount
  1721. else
  1722. if symtabletype = staticsymtable then
  1723. getnewtypecount:=tsymtable.getnewtypecount
  1724. else
  1725. begin
  1726. getnewtypecount:=unittypecount;
  1727. inc(unittypecount);
  1728. end;
  1729. end;
  1730. procedure tunitsymtable.concattypestabto(asmlist : paasmoutput);
  1731. var prev_dbx_count : plongint;
  1732. begin
  1733. if is_stab_written then exit;
  1734. if not assigned(name) then name := stringdup('Main_program');
  1735. if (symtabletype = unitsymtable) and
  1736. (current_module^.globalsymtable<>@Self) then
  1737. begin
  1738. unitid:=current_module^.unitcount;
  1739. inc(current_module^.unitcount);
  1740. end;
  1741. asmlist^.concat(new(pai_asm_comment,init(strpnew('Begin unit '+name^
  1742. +' has index '+tostr(unitid)))));
  1743. if cs_gdb_dbx in aktglobalswitches then
  1744. begin
  1745. if dbx_count_ok then
  1746. begin
  1747. asmlist^.concat(new(pai_asm_comment,init(strpnew('"repeated" unit '+name^
  1748. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count)))));
  1749. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1750. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count)))));
  1751. exit;
  1752. end
  1753. else if (current_module^.globalsymtable<>@Self) then
  1754. begin
  1755. prev_dbx_count := dbx_counter;
  1756. dbx_counter := nil;
  1757. do_count_dbx:=false;
  1758. if symtabletype = unitsymtable then
  1759. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1760. +tostr(N_BINCL)+',0,0,0'))));
  1761. dbx_counter := @dbx_count;
  1762. dbx_count:=0;
  1763. do_count_dbx:=assigned(dbx_counter);
  1764. end;
  1765. end;
  1766. asmoutput:=asmlist;
  1767. foreach({$ifdef FPCPROCVAR}@{$endif}concattypestab);
  1768. if cs_gdb_dbx in aktglobalswitches then
  1769. begin
  1770. if (current_module^.globalsymtable<>@Self) then
  1771. begin
  1772. dbx_counter := prev_dbx_count;
  1773. do_count_dbx:=false;
  1774. asmlist^.concat(new(pai_asm_comment,init(strpnew('End unit '+name^
  1775. +' has index '+tostr(unitid)))));
  1776. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1777. +tostr(N_EINCL)+',0,0,0'))));
  1778. do_count_dbx:=assigned(dbx_counter);
  1779. dbx_count_ok := {true}false;
  1780. end;
  1781. end;
  1782. is_stab_written:=true;
  1783. end;
  1784. {$endif}
  1785. {*****************************************************************************
  1786. Helper Routines
  1787. *****************************************************************************}
  1788. procedure numberunits;
  1789. var
  1790. counter : longint;
  1791. hp : pused_unit;
  1792. hp1 : pmodule;
  1793. begin
  1794. { Reset all numbers to -1 }
  1795. hp1:=pmodule(loaded_units.first);
  1796. while assigned(hp1) do
  1797. begin
  1798. if assigned(hp1^.globalsymtable) then
  1799. psymtable(hp1^.globalsymtable)^.unitid:=$ffff;
  1800. hp1:=pmodule(hp1^.next);
  1801. end;
  1802. { Our own symtable gets unitid 0, for a program there is
  1803. no globalsymtable }
  1804. if assigned(current_module^.globalsymtable) then
  1805. psymtable(current_module^.globalsymtable)^.unitid:=0;
  1806. { number units }
  1807. counter:=1;
  1808. hp:=pused_unit(current_module^.used_units.first);
  1809. while assigned(hp) do
  1810. begin
  1811. psymtable(hp^.u^.globalsymtable)^.unitid:=counter;
  1812. inc(counter);
  1813. hp:=pused_unit(hp^.next);
  1814. end;
  1815. end;
  1816. function findunitsymtable(st:psymtable):psymtable;
  1817. begin
  1818. findunitsymtable:=nil;
  1819. repeat
  1820. if not assigned(st) then
  1821. internalerror(5566561);
  1822. case st^.symtabletype of
  1823. localsymtable,
  1824. parasymtable,
  1825. staticsymtable :
  1826. break;
  1827. globalsymtable,
  1828. unitsymtable :
  1829. begin
  1830. findunitsymtable:=st;
  1831. break;
  1832. end;
  1833. objectsymtable,
  1834. recordsymtable :
  1835. st:=st^.defowner^.owner;
  1836. else
  1837. internalerror(5566562);
  1838. end;
  1839. until false;
  1840. end;
  1841. procedure duplicatesym(sym:psym);
  1842. var
  1843. st : psymtable;
  1844. begin
  1845. Message1(sym_e_duplicate_id,sym^.name);
  1846. st:=findunitsymtable(sym^.owner);
  1847. with sym^.fileinfo do
  1848. begin
  1849. if assigned(st) and (st^.unitid<>0) then
  1850. Message2(sym_h_duplicate_id_where,'unit '+st^.name^,tostr(line))
  1851. else
  1852. Message2(sym_h_duplicate_id_where,current_module^.sourcefiles^.get_file_name(fileindex),tostr(line));
  1853. end;
  1854. end;
  1855. procedure identifier_not_found(const s:string);
  1856. begin
  1857. Message1(sym_e_id_not_found,s);
  1858. { show a fatal that you need -S2 or -Sd, but only
  1859. if we just parsed the a token that has m_class }
  1860. if not(m_class in aktmodeswitches) and
  1861. (s=pattern) and
  1862. (tokeninfo^[idtoken].keyword=m_class) then
  1863. Message(parser_f_need_objfpc_or_delphi_mode);
  1864. end;
  1865. {*****************************************************************************
  1866. Search
  1867. *****************************************************************************}
  1868. procedure getsym(const s : stringid;notfounderror : boolean);
  1869. var
  1870. speedvalue : longint;
  1871. begin
  1872. speedvalue:=getspeedvalue(s);
  1873. lastsrsym:=nil;
  1874. srsymtable:=symtablestack;
  1875. while assigned(srsymtable) do
  1876. begin
  1877. srsym:=psym(srsymtable^.speedsearch(s,speedvalue));
  1878. if assigned(srsym) then
  1879. exit
  1880. else
  1881. srsymtable:=srsymtable^.next;
  1882. end;
  1883. if notfounderror then
  1884. begin
  1885. identifier_not_found(s);
  1886. srsym:=generrorsym;
  1887. end
  1888. else
  1889. srsym:=nil;
  1890. end;
  1891. procedure getsymonlyin(p : psymtable;const s : stringid);
  1892. begin
  1893. { the caller have to take care if srsym=nil (FK) }
  1894. srsym:=nil;
  1895. if assigned(p) then
  1896. begin
  1897. srsymtable:=p;
  1898. srsym:=psym(srsymtable^.search(s));
  1899. if assigned(srsym) then
  1900. exit
  1901. else
  1902. begin
  1903. if (punitsymtable(srsymtable)=punitsymtable(current_module^.globalsymtable)) then
  1904. begin
  1905. getsymonlyin(psymtable(current_module^.localsymtable),s);
  1906. if assigned(srsym) then
  1907. srsymtable:=psymtable(current_module^.localsymtable)
  1908. else
  1909. identifier_not_found(s);
  1910. end
  1911. else
  1912. identifier_not_found(s);
  1913. end;
  1914. end;
  1915. end;
  1916. function search_a_symtable(const symbol:string;symtabletype:tsymtabletype):Psym;
  1917. {Search for a symbol in a specified symbol table. Returns nil if
  1918. the symtable is not found, and also if the symbol cannot be found
  1919. in the desired symtable }
  1920. var hsymtab:Psymtable;
  1921. res:Psym;
  1922. begin
  1923. res:=nil;
  1924. hsymtab:=symtablestack;
  1925. while (hsymtab<>nil) and (hsymtab^.symtabletype<>symtabletype) do
  1926. hsymtab:=hsymtab^.next;
  1927. if hsymtab<>nil then
  1928. {We found the desired symtable. Now check if the symbol we
  1929. search for is defined in it }
  1930. res:=psym(hsymtab^.search(symbol));
  1931. search_a_symtable:=res;
  1932. end;
  1933. {*****************************************************************************
  1934. Definition Helpers
  1935. *****************************************************************************}
  1936. function globaldef(const s : string) : pdef;
  1937. var st : string;
  1938. symt : psymtable;
  1939. begin
  1940. srsym := nil;
  1941. if pos('.',s) > 0 then
  1942. begin
  1943. st := copy(s,1,pos('.',s)-1);
  1944. getsym(st,false);
  1945. st := copy(s,pos('.',s)+1,255);
  1946. if assigned(srsym) then
  1947. begin
  1948. if srsym^.typ = unitsym then
  1949. begin
  1950. symt := punitsym(srsym)^.unitsymtable;
  1951. srsym := psym(symt^.search(st));
  1952. end else srsym := nil;
  1953. end;
  1954. end else st := s;
  1955. if srsym = nil then getsym(st,false);
  1956. if srsym = nil then
  1957. getsymonlyin(systemunit,st);
  1958. if srsym^.typ<>typesym then
  1959. begin
  1960. Message(type_e_type_id_expected);
  1961. exit;
  1962. end;
  1963. globaldef := pdef(ptypesym(srsym)^.restype.def);
  1964. end;
  1965. {****************************************************************************
  1966. Object Helpers
  1967. ****************************************************************************}
  1968. function search_class_member(pd : pobjectdef;const n : string) : psym;
  1969. { searches n in symtable of pd and all anchestors }
  1970. var
  1971. sym : psym;
  1972. begin
  1973. sym:=nil;
  1974. while assigned(pd) do
  1975. begin
  1976. sym:=psym(pd^.symtable^.search(n));
  1977. if assigned(sym) then
  1978. break;
  1979. pd:=pd^.childof;
  1980. end;
  1981. { this is needed for static methods in do_member_read pexpr unit PM
  1982. caused bug0214 }
  1983. if assigned(sym) then
  1984. begin
  1985. srsymtable:=pd^.symtable;
  1986. end;
  1987. search_class_member:=sym;
  1988. end;
  1989. var
  1990. _defaultprop : ppropertysym;
  1991. procedure testfordefaultproperty(p : pnamedindexobject);
  1992. begin
  1993. if (psym(p)^.typ=propertysym) and
  1994. (ppo_defaultproperty in ppropertysym(p)^.propoptions) then
  1995. _defaultprop:=ppropertysym(p);
  1996. end;
  1997. function search_default_property(pd : pobjectdef) : ppropertysym;
  1998. { returns the default property of a class, searches also anchestors }
  1999. begin
  2000. _defaultprop:=nil;
  2001. while assigned(pd) do
  2002. begin
  2003. pd^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}testfordefaultproperty);
  2004. if assigned(_defaultprop) then
  2005. break;
  2006. pd:=pd^.childof;
  2007. end;
  2008. search_default_property:=_defaultprop;
  2009. end;
  2010. {$ifdef UNITALIASES}
  2011. {****************************************************************************
  2012. TUNIT_ALIAS
  2013. ****************************************************************************}
  2014. constructor tunit_alias.init(const n:string);
  2015. var
  2016. i : longint;
  2017. begin
  2018. i:=pos('=',n);
  2019. if i=0 then
  2020. fail;
  2021. inherited initname(Copy(n,1,i-1));
  2022. newname:=stringdup(Copy(n,i+1,255));
  2023. end;
  2024. destructor tunit_alias.done;
  2025. begin
  2026. stringdispose(newname);
  2027. inherited done;
  2028. end;
  2029. procedure addunitalias(const n:string);
  2030. begin
  2031. unitaliases^.insert(new(punit_alias,init(Upper(n))));
  2032. end;
  2033. function getunitalias(const n:string):string;
  2034. var
  2035. p : punit_alias;
  2036. begin
  2037. p:=punit_alias(unitaliases^.search(Upper(n)));
  2038. if assigned(p) then
  2039. getunitalias:=punit_alias(p)^.newname^
  2040. else
  2041. getunitalias:=n;
  2042. end;
  2043. {$endif UNITALIASES}
  2044. {****************************************************************************
  2045. Symtable Stack
  2046. ****************************************************************************}
  2047. procedure dellexlevel;
  2048. var
  2049. p : psymtable;
  2050. begin
  2051. p:=symtablestack;
  2052. symtablestack:=p^.next;
  2053. { symbol tables of unit interfaces are never disposed }
  2054. { this is handle by the unit unitm }
  2055. if not(p^.symtabletype in [unitsymtable,globalsymtable,stt_exceptsymtable]) then
  2056. dispose(p,done);
  2057. end;
  2058. procedure RestoreUnitSyms;
  2059. var
  2060. p : psymtable;
  2061. begin
  2062. p:=symtablestack;
  2063. while assigned(p) do
  2064. begin
  2065. if (p^.symtabletype=unitsymtable) and
  2066. assigned(punitsymtable(p)^.unitsym) and
  2067. ((punitsymtable(p)^.unitsym^.owner=psymtable(current_module^.globalsymtable)) or
  2068. (punitsymtable(p)^.unitsym^.owner=psymtable(current_module^.localsymtable))) then
  2069. punitsymtable(p)^.unitsym^.restoreunitsym;
  2070. p:=p^.next;
  2071. end;
  2072. end;
  2073. {$ifdef DEBUG}
  2074. procedure test_symtablestack;
  2075. var
  2076. p : psymtable;
  2077. i : longint;
  2078. begin
  2079. p:=symtablestack;
  2080. i:=0;
  2081. while assigned(p) do
  2082. begin
  2083. inc(i);
  2084. p:=p^.next;
  2085. if i>500 then
  2086. Message(sym_f_internal_error_in_symtablestack);
  2087. end;
  2088. end;
  2089. procedure list_symtablestack;
  2090. var
  2091. p : psymtable;
  2092. i : longint;
  2093. begin
  2094. p:=symtablestack;
  2095. i:=0;
  2096. while assigned(p) do
  2097. begin
  2098. inc(i);
  2099. writeln(i,' ',p^.name^);
  2100. p:=p^.next;
  2101. if i>500 then
  2102. Message(sym_f_internal_error_in_symtablestack);
  2103. end;
  2104. end;
  2105. {$endif DEBUG}
  2106. {****************************************************************************
  2107. Init/Done Symtable
  2108. ****************************************************************************}
  2109. procedure InitSymtable;
  2110. var
  2111. token : ttoken;
  2112. begin
  2113. { Reset symbolstack }
  2114. registerdef:=false;
  2115. read_member:=false;
  2116. symtablestack:=nil;
  2117. systemunit:=nil;
  2118. {$ifdef GDB}
  2119. firstglobaldef:=nil;
  2120. lastglobaldef:=nil;
  2121. globaltypecount:=1;
  2122. pglobaltypecount:=@globaltypecount;
  2123. {$endif GDB}
  2124. { create error syms and def }
  2125. generrorsym:=new(perrorsym,init);
  2126. generrordef:=new(perrordef,init);
  2127. {$ifdef UNITALIASES}
  2128. { unit aliases }
  2129. unitaliases:=new(pdictionary,init);
  2130. {$endif}
  2131. for token:=first_overloaded to last_overloaded do
  2132. overloaded_operators[token]:=nil;
  2133. end;
  2134. procedure DoneSymtable;
  2135. begin
  2136. dispose(generrorsym,done);
  2137. dispose(generrordef,done);
  2138. {$ifdef UNITALIASES}
  2139. dispose(unitaliases,done);
  2140. {$endif}
  2141. {$ifdef MEMDEBUG}
  2142. writeln('Manglednames: ',manglenamesize,' bytes');
  2143. {$endif}
  2144. end;
  2145. end.
  2146. {
  2147. $Log$
  2148. Revision 1.13 2000-11-01 23:04:38 peter
  2149. * tprocdef.fullprocname added for better casesensitve writing of
  2150. procedures
  2151. Revision 1.12 2000/10/31 22:02:52 peter
  2152. * symtable splitted, no real code changes
  2153. }