symtable.pas 79 KB

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