symtable.pas 81 KB

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