symtable.pas 81 KB

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