symtable.pas 81 KB

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