symtable.pas 80 KB

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