symtable.pas 80 KB

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