symtable.pas 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450
  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
  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,plabelsym(p)^.realname)
  226. else
  227. Message1(sym_w_label_not_defined,plabelsym(p)^.realname);
  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,psym(p)^.realname);
  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^,psym(p)^.realname)
  262. else
  263. MessagePos1(psym(p)^.fileinfo,sym_n_local_identifier_not_used,psym(p)^.realname);
  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 not(pvarsym(p)^.varspez in [vs_var,vs_out]) then
  270. MessagePos1(psym(p)^.fileinfo,sym_h_para_identifier_only_set,psym(p)^.realname)
  271. end
  272. else if (vo_is_local_copy in pvarsym(p)^.varoptions) then
  273. begin
  274. if not(pvarsym(p)^.varspez in [vs_var,vs_out]) then
  275. MessagePos1(psym(p)^.fileinfo,sym_h_para_identifier_only_set,psym(p)^.realname);
  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^,psym(p)^.realname)
  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,psym(p)^.realname);
  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^,psym(p)^.realname)
  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],psym(p)^.realname);
  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. is_class(pdef(next^.next^.defowner))) 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. is_class(procinfo^._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) and
  991. (
  992. not(m_delphi in aktmodeswitches) or
  993. is_object(pdef(defowner))
  994. ) then
  995. begin
  996. hsym:=search_class_member(pobjectdef(defowner),sym^.name);
  997. { but private ids can be reused }
  998. if assigned(hsym) and
  999. (not(sp_private in hsym^.symoptions) or
  1000. (hsym^.owner^.defowner^.owner^.symtabletype<>unitsymtable)) then
  1001. begin
  1002. DuplicateSym(hsym);
  1003. exit;
  1004. end;
  1005. end;
  1006. { register definition of typesym }
  1007. if (sym^.typ = typesym) and
  1008. assigned(ptypesym(sym)^.restype.def) then
  1009. begin
  1010. if not(assigned(ptypesym(sym)^.restype.def^.owner)) and
  1011. (ptypesym(sym)^.restype.def^.deftype<>errordef) then
  1012. registerdef(ptypesym(sym)^.restype.def);
  1013. {$ifdef GDB}
  1014. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  1015. (symtabletype in [globalsymtable,staticsymtable]) then
  1016. begin
  1017. ptypesym(sym)^.isusedinstab := true;
  1018. {sym^.concatstabto(debuglist);}
  1019. end;
  1020. {$endif GDB}
  1021. end;
  1022. { insert in index and search hash }
  1023. symindex^.insert(sym);
  1024. symsearch^.insert(sym);
  1025. end;
  1026. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : longint) : psymentry;
  1027. var
  1028. hp : pstoredsym;
  1029. newref : pref;
  1030. begin
  1031. hp:=pstoredsym(inherited speedsearch(s,speedvalue));
  1032. if assigned(hp) then
  1033. begin
  1034. { reject non static members in static procedures,
  1035. be carefull aktprocsym^.definition is not allways
  1036. loaded already (PFV) }
  1037. if (symtabletype=objectsymtable) and
  1038. not(sp_static in hp^.symoptions) and
  1039. allow_only_static
  1040. {assigned(aktprocsym) and
  1041. assigned(aktprocsym^.definition) and
  1042. ((aktprocsym^.definition^.options and postaticmethod)<>0)} then
  1043. Message(sym_e_only_static_in_static);
  1044. if (symtabletype=unitsymtable) and
  1045. assigned(punitsymtable(@self)^.unitsym) then
  1046. inc(punitsymtable(@self)^.unitsym^.refs);
  1047. {$ifdef GDB}
  1048. { if it is a type, we need the stabs of this type
  1049. this might be the cause of the class debug problems
  1050. as TCHILDCLASS.Create did not generate appropriate
  1051. stabs debug info if TCHILDCLASS wasn't used anywhere else PM }
  1052. if (hp^.typ=typesym) and make_ref then
  1053. begin
  1054. if assigned(ptypesym(hp)^.restype.def) then
  1055. pstoreddef(ptypesym(hp)^.restype.def)^.numberstring
  1056. else
  1057. ptypesym(hp)^.isusedinstab:=true;
  1058. end;
  1059. {$endif GDB}
  1060. { unitsym are only loaded for browsing PM }
  1061. { this was buggy anyway because we could use }
  1062. { unitsyms from other units in _USES !! }
  1063. {if (symtabletype=unitsymtable) and (hp^.typ=unitsym) and
  1064. assigned(current_module) and (current_module^.globalsymtable<>@self) then
  1065. hp:=nil;}
  1066. if assigned(hp) and
  1067. (cs_browser in aktmoduleswitches) and make_ref then
  1068. begin
  1069. new(newref,init(hp^.lastref,@akttokenpos));
  1070. { for symbols that are in tables without
  1071. browser info or syssyms (PM) }
  1072. if hp^.refcount=0 then
  1073. begin
  1074. hp^.defref:=newref;
  1075. hp^.lastref:=newref;
  1076. end
  1077. else
  1078. if resolving_forward and assigned(hp^.defref) then
  1079. { put it as second reference }
  1080. begin
  1081. newref^.nextref:=hp^.defref^.nextref;
  1082. hp^.defref^.nextref:=newref;
  1083. hp^.lastref^.nextref:=nil;
  1084. end
  1085. else
  1086. hp^.lastref:=newref;
  1087. inc(hp^.refcount);
  1088. end;
  1089. if assigned(hp) and make_ref then
  1090. begin
  1091. inc(hp^.refs);
  1092. end;
  1093. end;
  1094. speedsearch:=hp;
  1095. end;
  1096. {***********************************************
  1097. Process all entries
  1098. ***********************************************}
  1099. { checks, if all procsyms and methods are defined }
  1100. procedure tstoredsymtable.check_forwards;
  1101. begin
  1102. foreach({$ifdef FPCPROCVAR}@{$endif}check_forward);
  1103. end;
  1104. procedure tstoredsymtable.checklabels;
  1105. begin
  1106. foreach({$ifdef FPCPROCVAR}@{$endif}labeldefined);
  1107. end;
  1108. procedure tstoredsymtable.set_alignment(_alignment : longint);
  1109. var
  1110. sym : pvarsym;
  1111. l : longint;
  1112. begin
  1113. dataalignment:=_alignment;
  1114. if (symtabletype<>parasymtable) then
  1115. internalerror(1111);
  1116. sym:=pvarsym(symindex^.first);
  1117. datasize:=0;
  1118. { there can be only varsyms }
  1119. while assigned(sym) do
  1120. begin
  1121. l:=sym^.getpushsize;
  1122. sym^.address:=datasize;
  1123. datasize:=align(datasize+l,dataalignment);
  1124. sym:=pvarsym(sym^.indexnext);
  1125. end;
  1126. end;
  1127. procedure tstoredsymtable.allunitsused;
  1128. begin
  1129. foreach({$ifdef FPCPROCVAR}@{$endif}unitsymbolused);
  1130. end;
  1131. procedure tstoredsymtable.allsymbolsused;
  1132. begin
  1133. foreach({$ifdef FPCPROCVAR}@{$endif}varsymbolused);
  1134. end;
  1135. procedure tstoredsymtable.allprivatesused;
  1136. begin
  1137. foreach({$ifdef FPCPROCVAR}@{$endif}objectprivatesymbolused);
  1138. end;
  1139. {$ifdef CHAINPROCSYMS}
  1140. procedure tstoredsymtable.chainprocsyms;
  1141. begin
  1142. foreach({$ifdef FPCPROCVAR}@{$endif}chainprocsym);
  1143. end;
  1144. {$endif CHAINPROCSYMS}
  1145. {$ifdef GDB}
  1146. procedure tstoredsymtable.concatstabto(asmlist : paasmoutput);
  1147. begin
  1148. asmoutput:=asmlist;
  1149. if symtabletype in [inlineparasymtable,inlinelocalsymtable] then
  1150. foreach({$ifdef FPCPROCVAR}@{$endif}resetstab);
  1151. foreach({$ifdef FPCPROCVAR}@{$endif}concatstab);
  1152. end;
  1153. {$endif}
  1154. {****************************************************************************
  1155. TWITHSYMTABLE
  1156. ****************************************************************************}
  1157. constructor twithsymtable.init;
  1158. begin
  1159. inherited init(withsymtable);
  1160. direct_with:=false;
  1161. withnode:=nil;
  1162. withrefnode:=nil;
  1163. { we don't need the symsearch }
  1164. dispose(symsearch,done);
  1165. symsearch:=nil;
  1166. end;
  1167. destructor twithsymtable.done;
  1168. begin
  1169. symsearch:=nil;
  1170. inherited done;
  1171. end;
  1172. procedure twithsymtable.clear;
  1173. begin
  1174. { remove no entry from a withsymtable as it is only a pointer to the
  1175. recorddef or objectdef symtable }
  1176. end;
  1177. {****************************************************************************
  1178. PPU Writing Helpers
  1179. ****************************************************************************}
  1180. procedure writesourcefiles;
  1181. var
  1182. hp : pinputfile;
  1183. i,j : longint;
  1184. begin
  1185. { second write the used source files }
  1186. current_ppu^.do_crc:=false;
  1187. hp:=current_module^.sourcefiles^.files;
  1188. { write source files directly in good order }
  1189. j:=0;
  1190. while assigned(hp) do
  1191. begin
  1192. inc(j);
  1193. hp:=hp^.ref_next;
  1194. end;
  1195. while j>0 do
  1196. begin
  1197. hp:=current_module^.sourcefiles^.files;
  1198. for i:=1 to j-1 do
  1199. hp:=hp^.ref_next;
  1200. current_ppu^.putstring(hp^.name^);
  1201. dec(j);
  1202. end;
  1203. current_ppu^.writeentry(ibsourcefiles);
  1204. current_ppu^.do_crc:=true;
  1205. end;
  1206. procedure writeusedmacro(p:pnamedindexobject);
  1207. begin
  1208. if pmacro(p)^.is_used or pmacro(p)^.defined_at_startup then
  1209. begin
  1210. current_ppu^.putstring(p^.name);
  1211. current_ppu^.putbyte(byte(pmacro(p)^.defined_at_startup));
  1212. current_ppu^.putbyte(byte(pmacro(p)^.is_used));
  1213. end;
  1214. end;
  1215. procedure writeusedmacros;
  1216. begin
  1217. current_ppu^.do_crc:=false;
  1218. current_scanner^.macros^.foreach({$ifdef FPCPROCVAR}@{$endif}writeusedmacro);
  1219. current_ppu^.writeentry(ibusedmacros);
  1220. current_ppu^.do_crc:=true;
  1221. end;
  1222. procedure writeusedunit;
  1223. var
  1224. hp : pused_unit;
  1225. begin
  1226. numberunits;
  1227. hp:=pused_unit(current_module^.used_units.first);
  1228. while assigned(hp) do
  1229. begin
  1230. { implementation units should not change
  1231. the CRC PM }
  1232. current_ppu^.do_crc:=hp^.in_interface;
  1233. current_ppu^.putstring(hp^.name^);
  1234. { the checksum should not affect the crc of this unit ! (PFV) }
  1235. current_ppu^.do_crc:=false;
  1236. current_ppu^.putlongint(hp^.checksum);
  1237. current_ppu^.putlongint(hp^.interface_checksum);
  1238. current_ppu^.putbyte(byte(hp^.in_interface));
  1239. current_ppu^.do_crc:=true;
  1240. hp:=pused_unit(hp^.next);
  1241. end;
  1242. current_ppu^.do_interface_crc:=true;
  1243. current_ppu^.writeentry(ibloadunit);
  1244. end;
  1245. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  1246. var
  1247. hcontainer : tlinkcontainer;
  1248. s : string;
  1249. mask : longint;
  1250. begin
  1251. hcontainer.init;
  1252. while not p.empty do
  1253. begin
  1254. s:=p.get(mask);
  1255. if strippath then
  1256. current_ppu^.putstring(SplitFileName(s))
  1257. else
  1258. current_ppu^.putstring(s);
  1259. current_ppu^.putlongint(mask);
  1260. hcontainer.insert(s,mask);
  1261. end;
  1262. current_ppu^.writeentry(id);
  1263. p:=hcontainer;
  1264. end;
  1265. procedure writeunitas(const s : string;unittable : punitsymtable;only_crc : boolean);
  1266. begin
  1267. Message1(unit_u_ppu_write,s);
  1268. { create unit flags }
  1269. with Current_Module^ do
  1270. begin
  1271. {$ifdef GDB}
  1272. if cs_gdb_dbx in aktglobalswitches then
  1273. flags:=flags or uf_has_dbx;
  1274. {$endif GDB}
  1275. if target_os.endian=endian_big then
  1276. flags:=flags or uf_big_endian;
  1277. if cs_browser in aktmoduleswitches then
  1278. flags:=flags or uf_has_browser;
  1279. if cs_local_browser in aktmoduleswitches then
  1280. flags:=flags or uf_local_browser;
  1281. end;
  1282. {$ifdef Test_Double_checksum_write}
  1283. If only_crc then
  1284. Assign(CRCFile,s+'.INT')
  1285. else
  1286. Assign(CRCFile,s+'.IMP');
  1287. Rewrite(CRCFile);
  1288. {$endif def Test_Double_checksum_write}
  1289. { open ppufile }
  1290. current_ppu:=new(pppufile,init(s));
  1291. current_ppu^.crc_only:=only_crc;
  1292. if not current_ppu^.create then
  1293. Message(unit_f_ppu_cannot_write);
  1294. {$ifdef Test_Double_checksum}
  1295. if only_crc then
  1296. begin
  1297. new(current_ppu^.crc_test);
  1298. new(current_ppu^.crc_test2);
  1299. end
  1300. else
  1301. begin
  1302. current_ppu^.crc_test:=Current_Module^.crc_array;
  1303. current_ppu^.crc_index:=Current_Module^.crc_size;
  1304. current_ppu^.crc_test2:=Current_Module^.crc_array2;
  1305. current_ppu^.crc_index2:=Current_Module^.crc_size2;
  1306. end;
  1307. {$endif def Test_Double_checksum}
  1308. current_ppu^.change_endian:=source_os.endian<>target_os.endian;
  1309. { write symbols and definitions }
  1310. unittable^.writeasunit;
  1311. { flush to be sure }
  1312. current_ppu^.flush;
  1313. { create and write header }
  1314. current_ppu^.header.size:=current_ppu^.size;
  1315. current_ppu^.header.checksum:=current_ppu^.crc;
  1316. current_ppu^.header.interface_checksum:=current_ppu^.interface_crc;
  1317. current_ppu^.header.compiler:=wordversion;
  1318. current_ppu^.header.cpu:=word(target_cpu);
  1319. current_ppu^.header.target:=word(target_info.target);
  1320. current_ppu^.header.flags:=current_module^.flags;
  1321. If not only_crc then
  1322. current_ppu^.writeheader;
  1323. { save crc in current_module also }
  1324. current_module^.crc:=current_ppu^.crc;
  1325. current_module^.interface_crc:=current_ppu^.interface_crc;
  1326. if only_crc then
  1327. begin
  1328. {$ifdef Test_Double_checksum}
  1329. Current_Module^.crc_array:=current_ppu^.crc_test;
  1330. current_ppu^.crc_test:=nil;
  1331. Current_Module^.crc_size:=current_ppu^.crc_index2;
  1332. Current_Module^.crc_array2:=current_ppu^.crc_test2;
  1333. current_ppu^.crc_test2:=nil;
  1334. Current_Module^.crc_size2:=current_ppu^.crc_index2;
  1335. {$endif def Test_Double_checksum}
  1336. closecurrentppu;
  1337. end;
  1338. {$ifdef Test_Double_checksum_write}
  1339. close(CRCFile);
  1340. {$endif Test_Double_checksum_write}
  1341. end;
  1342. procedure readusedmacros;
  1343. var
  1344. hs : string;
  1345. mac : pmacro;
  1346. was_defined_at_startup,
  1347. was_used : boolean;
  1348. begin
  1349. while not current_ppu^.endofentry do
  1350. begin
  1351. hs:=current_ppu^.getstring;
  1352. was_defined_at_startup:=boolean(current_ppu^.getbyte);
  1353. was_used:=boolean(current_ppu^.getbyte);
  1354. mac:=pmacro(current_scanner^.macros^.search(hs));
  1355. if assigned(mac) then
  1356. begin
  1357. {$ifndef EXTDEBUG}
  1358. { if we don't have the sources why tell }
  1359. if current_module^.sources_avail then
  1360. {$endif ndef EXTDEBUG}
  1361. if (not was_defined_at_startup) and
  1362. was_used and
  1363. mac^.defined_at_startup then
  1364. Message2(unit_h_cond_not_set_in_last_compile,hs,current_module^.mainsource^);
  1365. end
  1366. else { not assigned }
  1367. if was_defined_at_startup and
  1368. was_used then
  1369. Message2(unit_h_cond_not_set_in_last_compile,hs,current_module^.mainsource^);
  1370. end;
  1371. end;
  1372. procedure readsourcefiles;
  1373. var
  1374. temp,hs : string;
  1375. temp_dir : string;
  1376. main_dir : string;
  1377. incfile_found,
  1378. main_found,
  1379. is_main : boolean;
  1380. ppufiletime,
  1381. source_time : longint;
  1382. hp : pinputfile;
  1383. begin
  1384. ppufiletime:=getnamedfiletime(current_module^.ppufilename^);
  1385. current_module^.sources_avail:=true;
  1386. is_main:=true;
  1387. main_dir:='';
  1388. while not current_ppu^.endofentry do
  1389. begin
  1390. hs:=current_ppu^.getstring;
  1391. temp_dir:='';
  1392. if (current_module^.flags and uf_in_library)<>0 then
  1393. begin
  1394. current_module^.sources_avail:=false;
  1395. temp:=' library';
  1396. end
  1397. else if pos('Macro ',hs)=1 then
  1398. begin
  1399. { we don't want to find this file }
  1400. { but there is a problem with file indexing !! }
  1401. temp:='';
  1402. end
  1403. else
  1404. begin
  1405. { check the date of the source files }
  1406. Source_Time:=GetNamedFileTime(current_module^.path^+hs);
  1407. incfile_found:=false;
  1408. main_found:=false;
  1409. if Source_Time<>-1 then
  1410. hs:=current_module^.path^+hs
  1411. else
  1412. if not(is_main) then
  1413. begin
  1414. Source_Time:=GetNamedFileTime(main_dir+hs);
  1415. if Source_Time<>-1 then
  1416. hs:=main_dir+hs;
  1417. end;
  1418. if (Source_Time=-1) then
  1419. begin
  1420. if is_main then
  1421. temp_dir:=unitsearchpath.FindFile(hs,main_found)
  1422. else
  1423. temp_dir:=includesearchpath.FindFile(hs,incfile_found);
  1424. if incfile_found or main_found then
  1425. begin
  1426. hs:=temp_dir+hs;
  1427. Source_Time:=GetNamedFileTime(hs);
  1428. end
  1429. end;
  1430. if Source_Time=-1 then
  1431. begin
  1432. current_module^.sources_avail:=false;
  1433. temp:=' not found';
  1434. end
  1435. else
  1436. begin
  1437. if main_found then
  1438. main_dir:=temp_dir;
  1439. { time newer? But only allow if the file is not searched
  1440. in the include path (PFV), else you've problems with
  1441. units which use the same includefile names }
  1442. if incfile_found then
  1443. temp:=' found'
  1444. else
  1445. begin
  1446. temp:=' time '+filetimestring(source_time);
  1447. if (source_time>ppufiletime) then
  1448. begin
  1449. current_module^.do_compile:=true;
  1450. current_module^.recompile_reason:=rr_sourcenewer;
  1451. temp:=temp+' *'
  1452. end;
  1453. end;
  1454. end;
  1455. new(hp,init(hs));
  1456. { the indexing is wrong here PM }
  1457. current_module^.sourcefiles^.register_file(hp);
  1458. end;
  1459. if is_main then
  1460. begin
  1461. stringdispose(current_module^.mainsource);
  1462. current_module^.mainsource:=stringdup(hs);
  1463. end;
  1464. Message1(unit_u_ppu_source,hs+temp);
  1465. is_main:=false;
  1466. end;
  1467. { check if we want to rebuild every unit, only if the sources are
  1468. available }
  1469. if do_build and current_module^.sources_avail then
  1470. begin
  1471. current_module^.do_compile:=true;
  1472. current_module^.recompile_reason:=rr_build;
  1473. end;
  1474. end;
  1475. procedure readloadunit;
  1476. var
  1477. hs : string;
  1478. intfchecksum,
  1479. checksum : longint;
  1480. in_interface : boolean;
  1481. begin
  1482. while not current_ppu^.endofentry do
  1483. begin
  1484. hs:=current_ppu^.getstring;
  1485. checksum:=current_ppu^.getlongint;
  1486. intfchecksum:=current_ppu^.getlongint;
  1487. in_interface:=(current_ppu^.getbyte<>0);
  1488. current_module^.used_units.concat(new(pused_unit,init_to_load(hs,checksum,intfchecksum,in_interface)));
  1489. end;
  1490. end;
  1491. procedure readlinkcontainer(var p:tlinkcontainer);
  1492. var
  1493. s : string;
  1494. m : longint;
  1495. begin
  1496. while not current_ppu^.endofentry do
  1497. begin
  1498. s:=current_ppu^.getstring;
  1499. m:=current_ppu^.getlongint;
  1500. p.insert(s,m);
  1501. end;
  1502. end;
  1503. procedure load_interface;
  1504. var
  1505. b : byte;
  1506. newmodulename : string;
  1507. begin
  1508. { read interface part }
  1509. repeat
  1510. b:=current_ppu^.readentry;
  1511. case b of
  1512. ibmodulename :
  1513. begin
  1514. newmodulename:=current_ppu^.getstring;
  1515. if upper(newmodulename)<>current_module^.modulename^ then
  1516. Message2(unit_f_unit_name_error,current_module^.realmodulename^,newmodulename);
  1517. stringdispose(current_module^.modulename);
  1518. stringdispose(current_module^.realmodulename);
  1519. current_module^.modulename:=stringdup(upper(newmodulename));
  1520. current_module^.realmodulename:=stringdup(newmodulename);
  1521. end;
  1522. ibsourcefiles :
  1523. readsourcefiles;
  1524. ibusedmacros :
  1525. readusedmacros;
  1526. ibloadunit :
  1527. readloadunit;
  1528. iblinkunitofiles :
  1529. readlinkcontainer(current_module^.LinkUnitOFiles);
  1530. iblinkunitstaticlibs :
  1531. readlinkcontainer(current_module^.LinkUnitStaticLibs);
  1532. iblinkunitsharedlibs :
  1533. readlinkcontainer(current_module^.LinkUnitSharedLibs);
  1534. iblinkotherofiles :
  1535. readlinkcontainer(current_module^.LinkotherOFiles);
  1536. iblinkotherstaticlibs :
  1537. readlinkcontainer(current_module^.LinkotherStaticLibs);
  1538. iblinkothersharedlibs :
  1539. readlinkcontainer(current_module^.LinkotherSharedLibs);
  1540. ibendinterface :
  1541. break;
  1542. else
  1543. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1544. end;
  1545. until false;
  1546. end;
  1547. {****************************************************************************
  1548. TUNITSYMTABLE
  1549. ****************************************************************************}
  1550. constructor tunitsymtable.init(t : tsymtabletype; const n : string);
  1551. begin
  1552. inherited init(t);
  1553. name:=stringdup(upper(n));
  1554. unitid:=0;
  1555. unitsym:=nil;
  1556. symsearch^.usehash;
  1557. { reset GDB things }
  1558. {$ifdef GDB}
  1559. if (t = globalsymtable) then
  1560. begin
  1561. prev_dbx_counter := dbx_counter;
  1562. dbx_counter := nil;
  1563. end;
  1564. is_stab_written:=false;
  1565. dbx_count := -1;
  1566. if cs_gdb_dbx in aktglobalswitches then
  1567. begin
  1568. dbx_count := 0;
  1569. unittypecount:=1;
  1570. if (symtabletype=globalsymtable) then
  1571. pglobaltypecount := @unittypecount;
  1572. unitid:=current_module^.unitcount;
  1573. debuglist^.concat(new(pai_asm_comment,init(strpnew('Global '+name^+' has index '+tostr(unitid)))));
  1574. debuglist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0'))));
  1575. inc(current_module^.unitcount);
  1576. dbx_count_ok:=false;
  1577. dbx_counter:=@dbx_count;
  1578. do_count_dbx:=true;
  1579. end;
  1580. {$endif GDB}
  1581. end;
  1582. constructor tunitsymtable.loadasunit;
  1583. var
  1584. {$ifdef GDB}
  1585. storeGlobalTypeCount : pword;
  1586. {$endif GDB}
  1587. b : byte;
  1588. begin
  1589. unitsym:=nil;
  1590. unitid:=0;
  1591. {$ifdef GDB}
  1592. if cs_gdb_dbx in aktglobalswitches then
  1593. begin
  1594. UnitTypeCount:=1;
  1595. storeGlobalTypeCount:=PGlobalTypeCount;
  1596. PglobalTypeCount:=@UnitTypeCount;
  1597. end;
  1598. {$endif GDB}
  1599. { load symtables }
  1600. inherited loadas(unitsymtable);
  1601. { set the name after because it is set to nil in tstoredsymtable.load !! }
  1602. name:=stringdup(current_module^.modulename^);
  1603. { dbx count }
  1604. {$ifdef GDB}
  1605. if (current_module^.flags and uf_has_dbx)<>0 then
  1606. begin
  1607. b := current_ppu^.readentry;
  1608. if b <> ibdbxcount then
  1609. Message(unit_f_ppu_dbx_count_problem)
  1610. else
  1611. dbx_count := readlong;
  1612. dbx_count_ok := {true}false;
  1613. end
  1614. else
  1615. begin
  1616. dbx_count := -1;
  1617. dbx_count_ok:=false;
  1618. end;
  1619. if cs_gdb_dbx in aktglobalswitches then
  1620. PGlobalTypeCount:=storeGlobalTypeCount;
  1621. is_stab_written:=false;
  1622. {$endif GDB}
  1623. b:=current_ppu^.readentry;
  1624. if b<>ibendimplementation then
  1625. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1626. end;
  1627. destructor tunitsymtable.done;
  1628. var
  1629. pus : punitsym;
  1630. begin
  1631. pus:=unitsym;
  1632. while assigned(pus) do
  1633. begin
  1634. unitsym:=pus^.prevsym;
  1635. pus^.prevsym:=nil;
  1636. pus^.unitsymtable:=nil;
  1637. pus:=unitsym;
  1638. end;
  1639. inherited done;
  1640. end;
  1641. procedure tunitsymtable.load_symtable_refs;
  1642. var
  1643. b : byte;
  1644. unitindex : word;
  1645. begin
  1646. if ((current_module^.flags and uf_local_browser)<>0) then
  1647. begin
  1648. current_module^.localsymtable:=new(punitsymtable,loadas(staticppusymtable));
  1649. psymtable(current_module^.localsymtable)^.name:=
  1650. stringdup('implementation of '+psymtable(current_module^.globalsymtable)^.name^);
  1651. end;
  1652. { load browser }
  1653. if (current_module^.flags and uf_has_browser)<>0 then
  1654. begin
  1655. {if not (cs_browser in aktmoduleswitches) then
  1656. current_ppu^.skipuntilentry(ibendbrowser)
  1657. else }
  1658. begin
  1659. load_browser;
  1660. unitindex:=1;
  1661. while assigned(current_module^.map^[unitindex]) do
  1662. begin
  1663. {each unit wrote one browser entry }
  1664. load_browser;
  1665. inc(unitindex);
  1666. end;
  1667. b:=current_ppu^.readentry;
  1668. if b<>ibendbrowser then
  1669. Message1(unit_f_ppu_invalid_entry,tostr(b));
  1670. end;
  1671. end;
  1672. if ((current_module^.flags and uf_local_browser)<>0) then
  1673. pstoredsymtable(current_module^.localsymtable)^.load_browser;
  1674. end;
  1675. procedure tunitsymtable.writeasunit;
  1676. var
  1677. pu : pused_unit;
  1678. begin
  1679. { first the unitname }
  1680. current_ppu^.putstring(current_module^.realmodulename^);
  1681. current_ppu^.writeentry(ibmodulename);
  1682. writesourcefiles;
  1683. writeusedmacros;
  1684. writeusedunit;
  1685. { write the objectfiles and libraries that come for this unit,
  1686. preserve the containers becuase they are still needed to load
  1687. the link.res. All doesn't depend on the crc! It doesn't matter
  1688. if a unit is in a .o or .a file }
  1689. current_ppu^.do_crc:=false;
  1690. writelinkcontainer(current_module^.linkunitofiles,iblinkunitofiles,true);
  1691. writelinkcontainer(current_module^.linkunitstaticlibs,iblinkunitstaticlibs,true);
  1692. writelinkcontainer(current_module^.linkunitsharedlibs,iblinkunitsharedlibs,true);
  1693. writelinkcontainer(current_module^.linkotherofiles,iblinkotherofiles,false);
  1694. writelinkcontainer(current_module^.linkotherstaticlibs,iblinkotherstaticlibs,true);
  1695. writelinkcontainer(current_module^.linkothersharedlibs,iblinkothersharedlibs,true);
  1696. current_ppu^.do_crc:=true;
  1697. current_ppu^.writeentry(ibendinterface);
  1698. { write the symtable entries }
  1699. inherited writeas;
  1700. { all after doesn't affect crc }
  1701. current_ppu^.do_crc:=false;
  1702. { write dbx count }
  1703. {$ifdef GDB}
  1704. if cs_gdb_dbx in aktglobalswitches then
  1705. begin
  1706. {$IfDef EXTDEBUG}
  1707. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1708. {$ENDIF EXTDEBUG}
  1709. current_ppu^.putlongint(dbx_count);
  1710. current_ppu^.writeentry(ibdbxcount);
  1711. end;
  1712. {$endif GDB}
  1713. current_ppu^.writeentry(ibendimplementation);
  1714. { write static symtable
  1715. needed for local debugging of unit functions }
  1716. if ((current_module^.flags and uf_local_browser)<>0) and
  1717. assigned(current_module^.localsymtable) then
  1718. pstoredsymtable(current_module^.localsymtable)^.writeas;
  1719. { write all browser section }
  1720. if (current_module^.flags and uf_has_browser)<>0 then
  1721. begin
  1722. write_browser;
  1723. pu:=pused_unit(current_module^.used_units.first);
  1724. while assigned(pu) do
  1725. begin
  1726. pstoredsymtable(pu^.u^.globalsymtable)^.write_browser;
  1727. pu:=pused_unit(pu^.next);
  1728. end;
  1729. current_ppu^.writeentry(ibendbrowser);
  1730. end;
  1731. if ((current_module^.flags and uf_local_browser)<>0) and
  1732. assigned(current_module^.localsymtable) then
  1733. pstoredsymtable(current_module^.localsymtable)^.write_browser;
  1734. { the last entry ibend is written automaticly }
  1735. end;
  1736. {$ifdef GDB}
  1737. function tunitsymtable.getnewtypecount : word;
  1738. begin
  1739. if not (cs_gdb_dbx in aktglobalswitches) then
  1740. getnewtypecount:=tsymtable.getnewtypecount
  1741. else
  1742. if symtabletype = staticsymtable then
  1743. getnewtypecount:=tsymtable.getnewtypecount
  1744. else
  1745. begin
  1746. getnewtypecount:=unittypecount;
  1747. inc(unittypecount);
  1748. end;
  1749. end;
  1750. procedure tunitsymtable.concattypestabto(asmlist : paasmoutput);
  1751. var prev_dbx_count : plongint;
  1752. begin
  1753. if is_stab_written then exit;
  1754. if not assigned(name) then name := stringdup('Main_program');
  1755. if (symtabletype = unitsymtable) and
  1756. (current_module^.globalsymtable<>@Self) then
  1757. begin
  1758. unitid:=current_module^.unitcount;
  1759. inc(current_module^.unitcount);
  1760. end;
  1761. asmlist^.concat(new(pai_asm_comment,init(strpnew('Begin unit '+name^
  1762. +' has index '+tostr(unitid)))));
  1763. if cs_gdb_dbx in aktglobalswitches then
  1764. begin
  1765. if dbx_count_ok then
  1766. begin
  1767. asmlist^.concat(new(pai_asm_comment,init(strpnew('"repeated" unit '+name^
  1768. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count)))));
  1769. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1770. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count)))));
  1771. exit;
  1772. end
  1773. else if (current_module^.globalsymtable<>@Self) then
  1774. begin
  1775. prev_dbx_count := dbx_counter;
  1776. dbx_counter := nil;
  1777. do_count_dbx:=false;
  1778. if symtabletype = unitsymtable then
  1779. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1780. +tostr(N_BINCL)+',0,0,0'))));
  1781. dbx_counter := @dbx_count;
  1782. dbx_count:=0;
  1783. do_count_dbx:=assigned(dbx_counter);
  1784. end;
  1785. end;
  1786. asmoutput:=asmlist;
  1787. foreach({$ifdef FPCPROCVAR}@{$endif}concattypestab);
  1788. if cs_gdb_dbx in aktglobalswitches then
  1789. begin
  1790. if (current_module^.globalsymtable<>@Self) then
  1791. begin
  1792. dbx_counter := prev_dbx_count;
  1793. do_count_dbx:=false;
  1794. asmlist^.concat(new(pai_asm_comment,init(strpnew('End unit '+name^
  1795. +' has index '+tostr(unitid)))));
  1796. asmlist^.concat(new(pai_stabs,init(strpnew('"'+name^+'",'
  1797. +tostr(N_EINCL)+',0,0,0'))));
  1798. do_count_dbx:=assigned(dbx_counter);
  1799. dbx_count_ok := {true}false;
  1800. end;
  1801. end;
  1802. is_stab_written:=true;
  1803. end;
  1804. {$endif}
  1805. {*****************************************************************************
  1806. Helper Routines
  1807. *****************************************************************************}
  1808. procedure numberunits;
  1809. var
  1810. counter : longint;
  1811. hp : pused_unit;
  1812. hp1 : pmodule;
  1813. begin
  1814. { Reset all numbers to -1 }
  1815. hp1:=pmodule(loaded_units.first);
  1816. while assigned(hp1) do
  1817. begin
  1818. if assigned(hp1^.globalsymtable) then
  1819. psymtable(hp1^.globalsymtable)^.unitid:=$ffff;
  1820. hp1:=pmodule(hp1^.next);
  1821. end;
  1822. { Our own symtable gets unitid 0, for a program there is
  1823. no globalsymtable }
  1824. if assigned(current_module^.globalsymtable) then
  1825. psymtable(current_module^.globalsymtable)^.unitid:=0;
  1826. { number units }
  1827. counter:=1;
  1828. hp:=pused_unit(current_module^.used_units.first);
  1829. while assigned(hp) do
  1830. begin
  1831. psymtable(hp^.u^.globalsymtable)^.unitid:=counter;
  1832. inc(counter);
  1833. hp:=pused_unit(hp^.next);
  1834. end;
  1835. end;
  1836. function findunitsymtable(st:psymtable):psymtable;
  1837. begin
  1838. findunitsymtable:=nil;
  1839. repeat
  1840. if not assigned(st) then
  1841. internalerror(5566561);
  1842. case st^.symtabletype of
  1843. localsymtable,
  1844. parasymtable,
  1845. staticsymtable :
  1846. break;
  1847. globalsymtable,
  1848. unitsymtable :
  1849. begin
  1850. findunitsymtable:=st;
  1851. break;
  1852. end;
  1853. objectsymtable,
  1854. recordsymtable :
  1855. st:=st^.defowner^.owner;
  1856. else
  1857. internalerror(5566562);
  1858. end;
  1859. until false;
  1860. end;
  1861. procedure duplicatesym(sym:psym);
  1862. var
  1863. st : psymtable;
  1864. begin
  1865. Message1(sym_e_duplicate_id,sym^.realname);
  1866. st:=findunitsymtable(sym^.owner);
  1867. with sym^.fileinfo do
  1868. begin
  1869. if assigned(st) and (st^.unitid<>0) then
  1870. Message2(sym_h_duplicate_id_where,'unit '+st^.name^,tostr(line))
  1871. else
  1872. Message2(sym_h_duplicate_id_where,current_module^.sourcefiles^.get_file_name(fileindex),tostr(line));
  1873. end;
  1874. end;
  1875. procedure identifier_not_found(const s:string);
  1876. begin
  1877. Message1(sym_e_id_not_found,s);
  1878. { show a fatal that you need -S2 or -Sd, but only
  1879. if we just parsed the a token that has m_class }
  1880. if not(m_class in aktmodeswitches) and
  1881. (s=pattern) and
  1882. (tokeninfo^[idtoken].keyword=m_class) then
  1883. Message(parser_f_need_objfpc_or_delphi_mode);
  1884. end;
  1885. {*****************************************************************************
  1886. Search
  1887. *****************************************************************************}
  1888. procedure getsym(const s : stringid;notfounderror : boolean);
  1889. var
  1890. speedvalue : longint;
  1891. begin
  1892. speedvalue:=getspeedvalue(s);
  1893. lastsrsym:=nil;
  1894. srsymtable:=symtablestack;
  1895. while assigned(srsymtable) do
  1896. begin
  1897. srsym:=psym(srsymtable^.speedsearch(s,speedvalue));
  1898. if assigned(srsym) then
  1899. exit
  1900. else
  1901. srsymtable:=srsymtable^.next;
  1902. end;
  1903. if notfounderror then
  1904. begin
  1905. identifier_not_found(s);
  1906. srsym:=generrorsym;
  1907. end
  1908. else
  1909. srsym:=nil;
  1910. end;
  1911. procedure getsymonlyin(p : psymtable;const s : stringid);
  1912. begin
  1913. { the caller have to take care if srsym=nil (FK) }
  1914. srsym:=nil;
  1915. if assigned(p) then
  1916. begin
  1917. srsymtable:=p;
  1918. srsym:=psym(srsymtable^.search(s));
  1919. if assigned(srsym) then
  1920. exit
  1921. else
  1922. begin
  1923. if (punitsymtable(srsymtable)=punitsymtable(current_module^.globalsymtable)) then
  1924. begin
  1925. getsymonlyin(psymtable(current_module^.localsymtable),s);
  1926. if assigned(srsym) then
  1927. srsymtable:=psymtable(current_module^.localsymtable)
  1928. else
  1929. identifier_not_found(s);
  1930. end
  1931. else
  1932. identifier_not_found(s);
  1933. end;
  1934. end;
  1935. end;
  1936. function search_a_symtable(const symbol:string;symtabletype:tsymtabletype):Psym;
  1937. {Search for a symbol in a specified symbol table. Returns nil if
  1938. the symtable is not found, and also if the symbol cannot be found
  1939. in the desired symtable }
  1940. var hsymtab:Psymtable;
  1941. res:Psym;
  1942. begin
  1943. res:=nil;
  1944. hsymtab:=symtablestack;
  1945. while (hsymtab<>nil) and (hsymtab^.symtabletype<>symtabletype) do
  1946. hsymtab:=hsymtab^.next;
  1947. if hsymtab<>nil then
  1948. {We found the desired symtable. Now check if the symbol we
  1949. search for is defined in it }
  1950. res:=psym(hsymtab^.search(symbol));
  1951. search_a_symtable:=res;
  1952. end;
  1953. {*****************************************************************************
  1954. Definition Helpers
  1955. *****************************************************************************}
  1956. function globaldef(const s : string) : pdef;
  1957. var st : string;
  1958. symt : psymtable;
  1959. begin
  1960. srsym := nil;
  1961. if pos('.',s) > 0 then
  1962. begin
  1963. st := copy(s,1,pos('.',s)-1);
  1964. getsym(st,false);
  1965. st := copy(s,pos('.',s)+1,255);
  1966. if assigned(srsym) then
  1967. begin
  1968. if srsym^.typ = unitsym then
  1969. begin
  1970. symt := punitsym(srsym)^.unitsymtable;
  1971. srsym := psym(symt^.search(st));
  1972. end else srsym := nil;
  1973. end;
  1974. end else st := s;
  1975. if srsym = nil then getsym(st,false);
  1976. if srsym = nil then
  1977. getsymonlyin(systemunit,st);
  1978. if srsym^.typ<>typesym then
  1979. begin
  1980. Message(type_e_type_id_expected);
  1981. exit;
  1982. end;
  1983. globaldef := pdef(ptypesym(srsym)^.restype.def);
  1984. end;
  1985. {****************************************************************************
  1986. Object Helpers
  1987. ****************************************************************************}
  1988. function search_class_member(pd : pobjectdef;const n : string) : psym;
  1989. { searches n in symtable of pd and all anchestors }
  1990. var
  1991. sym : psym;
  1992. begin
  1993. sym:=nil;
  1994. while assigned(pd) do
  1995. begin
  1996. sym:=psym(pd^.symtable^.search(n));
  1997. if assigned(sym) then
  1998. break;
  1999. pd:=pd^.childof;
  2000. end;
  2001. { this is needed for static methods in do_member_read pexpr unit PM
  2002. caused bug0214 }
  2003. if assigned(sym) then
  2004. begin
  2005. srsymtable:=pd^.symtable;
  2006. end;
  2007. search_class_member:=sym;
  2008. end;
  2009. var
  2010. _defaultprop : ppropertysym;
  2011. procedure testfordefaultproperty(p : pnamedindexobject);
  2012. begin
  2013. if (psym(p)^.typ=propertysym) and
  2014. (ppo_defaultproperty in ppropertysym(p)^.propoptions) then
  2015. _defaultprop:=ppropertysym(p);
  2016. end;
  2017. function search_default_property(pd : pobjectdef) : ppropertysym;
  2018. { returns the default property of a class, searches also anchestors }
  2019. begin
  2020. _defaultprop:=nil;
  2021. while assigned(pd) do
  2022. begin
  2023. pd^.symtable^.foreach({$ifdef FPCPROCVAR}@{$endif}testfordefaultproperty);
  2024. if assigned(_defaultprop) then
  2025. break;
  2026. pd:=pd^.childof;
  2027. end;
  2028. search_default_property:=_defaultprop;
  2029. end;
  2030. {$ifdef UNITALIASES}
  2031. {****************************************************************************
  2032. TUNIT_ALIAS
  2033. ****************************************************************************}
  2034. constructor tunit_alias.init(const n:string);
  2035. var
  2036. i : longint;
  2037. begin
  2038. i:=pos('=',n);
  2039. if i=0 then
  2040. fail;
  2041. inherited initname(Copy(n,1,i-1));
  2042. newname:=stringdup(Copy(n,i+1,255));
  2043. end;
  2044. destructor tunit_alias.done;
  2045. begin
  2046. stringdispose(newname);
  2047. inherited done;
  2048. end;
  2049. procedure addunitalias(const n:string);
  2050. begin
  2051. unitaliases^.insert(new(punit_alias,init(Upper(n))));
  2052. end;
  2053. function getunitalias(const n:string):string;
  2054. var
  2055. p : punit_alias;
  2056. begin
  2057. p:=punit_alias(unitaliases^.search(Upper(n)));
  2058. if assigned(p) then
  2059. getunitalias:=punit_alias(p)^.newname^
  2060. else
  2061. getunitalias:=n;
  2062. end;
  2063. {$endif UNITALIASES}
  2064. {****************************************************************************
  2065. Symtable Stack
  2066. ****************************************************************************}
  2067. procedure dellexlevel;
  2068. var
  2069. p : psymtable;
  2070. begin
  2071. p:=symtablestack;
  2072. symtablestack:=p^.next;
  2073. { symbol tables of unit interfaces are never disposed }
  2074. { this is handle by the unit unitm }
  2075. if not(p^.symtabletype in [unitsymtable,globalsymtable,stt_exceptsymtable]) then
  2076. dispose(p,done);
  2077. end;
  2078. procedure RestoreUnitSyms;
  2079. var
  2080. p : psymtable;
  2081. begin
  2082. p:=symtablestack;
  2083. while assigned(p) do
  2084. begin
  2085. if (p^.symtabletype=unitsymtable) and
  2086. assigned(punitsymtable(p)^.unitsym) and
  2087. ((punitsymtable(p)^.unitsym^.owner=psymtable(current_module^.globalsymtable)) or
  2088. (punitsymtable(p)^.unitsym^.owner=psymtable(current_module^.localsymtable))) then
  2089. punitsymtable(p)^.unitsym^.restoreunitsym;
  2090. p:=p^.next;
  2091. end;
  2092. end;
  2093. {$ifdef DEBUG}
  2094. procedure test_symtablestack;
  2095. var
  2096. p : psymtable;
  2097. i : longint;
  2098. begin
  2099. p:=symtablestack;
  2100. i:=0;
  2101. while assigned(p) do
  2102. begin
  2103. inc(i);
  2104. p:=p^.next;
  2105. if i>500 then
  2106. Message(sym_f_internal_error_in_symtablestack);
  2107. end;
  2108. end;
  2109. procedure list_symtablestack;
  2110. var
  2111. p : psymtable;
  2112. i : longint;
  2113. begin
  2114. p:=symtablestack;
  2115. i:=0;
  2116. while assigned(p) do
  2117. begin
  2118. inc(i);
  2119. writeln(i,' ',p^.name^);
  2120. p:=p^.next;
  2121. if i>500 then
  2122. Message(sym_f_internal_error_in_symtablestack);
  2123. end;
  2124. end;
  2125. {$endif DEBUG}
  2126. {****************************************************************************
  2127. Init/Done Symtable
  2128. ****************************************************************************}
  2129. procedure InitSymtable;
  2130. var
  2131. token : ttoken;
  2132. begin
  2133. { Reset symbolstack }
  2134. registerdef:=false;
  2135. read_member:=false;
  2136. symtablestack:=nil;
  2137. systemunit:=nil;
  2138. {$ifdef GDB}
  2139. firstglobaldef:=nil;
  2140. lastglobaldef:=nil;
  2141. globaltypecount:=1;
  2142. pglobaltypecount:=@globaltypecount;
  2143. {$endif GDB}
  2144. { create error syms and def }
  2145. generrorsym:=new(perrorsym,init);
  2146. generrordef:=new(perrordef,init);
  2147. {$ifdef UNITALIASES}
  2148. { unit aliases }
  2149. unitaliases:=new(pdictionary,init);
  2150. {$endif}
  2151. for token:=first_overloaded to last_overloaded do
  2152. overloaded_operators[token]:=nil;
  2153. end;
  2154. procedure DoneSymtable;
  2155. begin
  2156. dispose(generrorsym,done);
  2157. dispose(generrordef,done);
  2158. {$ifdef UNITALIASES}
  2159. dispose(unitaliases,done);
  2160. {$endif}
  2161. {$ifdef MEMDEBUG}
  2162. writeln('Manglednames: ',manglenamesize,' bytes');
  2163. {$endif}
  2164. end;
  2165. end.
  2166. {
  2167. $Log$
  2168. Revision 1.22 2000-12-23 19:50:09 peter
  2169. * fixed mem leak with withsymtable
  2170. Revision 1.21 2000/12/10 20:25:32 peter
  2171. * fixed missing typecast
  2172. Revision 1.20 2000/12/10 14:14:51 florian
  2173. * fixed web bug 1203: class fields can be now redefined
  2174. in Delphi mode though I don't like this :/
  2175. Revision 1.19 2000/11/30 22:16:49 florian
  2176. * moved to i386
  2177. Revision 1.18 2000/11/29 00:30:42 florian
  2178. * unused units removed from uses clause
  2179. * some changes for widestrings
  2180. Revision 1.17 2000/11/28 00:28:07 pierre
  2181. * stabs fixing
  2182. Revision 1.1.2.8 2000/11/17 11:14:37 pierre
  2183. * one more class stabs fix
  2184. Revision 1.16 2000/11/12 22:17:47 peter
  2185. * some realname updates for messages
  2186. Revision 1.15 2000/11/06 15:54:15 florian
  2187. * fixed two bugs to get make cycle work, but it's not enough
  2188. Revision 1.14 2000/11/04 14:25:22 florian
  2189. + merged Attila's changes for interfaces, not tested yet
  2190. Revision 1.13 2000/11/01 23:04:38 peter
  2191. * tprocdef.fullprocname added for better casesensitve writing of
  2192. procedures
  2193. Revision 1.12 2000/10/31 22:02:52 peter
  2194. * symtable splitted, no real code changes
  2195. Revision 1.1.2.7 2000/10/16 19:43:04 pierre
  2196. * trying to correct class stabss once more
  2197. Revision 1.11 2000/10/15 07:47:53 peter
  2198. * unit names and procedure names are stored mixed case
  2199. Revision 1.10 2000/10/14 10:14:53 peter
  2200. * moehrendorf oct 2000 rewrite
  2201. Revision 1.9 2000/10/01 19:48:25 peter
  2202. * lot of compile updates for cg11
  2203. Revision 1.8 2000/09/24 15:06:29 peter
  2204. * use defines.inc
  2205. Revision 1.7 2000/08/27 16:11:54 peter
  2206. * moved some util functions from globals,cobjects to cutils
  2207. * splitted files into finput,fmodule
  2208. Revision 1.6 2000/08/21 11:27:45 pierre
  2209. * fix the stabs problems
  2210. Revision 1.5 2000/08/20 14:58:41 peter
  2211. * give fatal if objfpc/delphi mode things are found (merged)
  2212. Revision 1.1.2.6 2000/08/20 14:56:46 peter
  2213. * give fatal if objfpc/delphi mode things are found
  2214. Revision 1.4 2000/08/16 18:33:54 peter
  2215. * splitted namedobjectitem.next into indexnext and listnext so it
  2216. can be used in both lists
  2217. * don't allow "word = word" type definitions (merged)
  2218. Revision 1.3 2000/08/08 19:28:57 peter
  2219. * memdebug/memory patches (merged)
  2220. * only once illegal directive (merged)
  2221. }