symtable.pas 81 KB

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