fmodule.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fmodule;
  18. {$i fpcdefs.inc}
  19. {$ifdef go32v2}
  20. {$define shortasmprefix}
  21. {$endif}
  22. {$ifdef watcom}
  23. {$define shortasmprefix}
  24. {$endif}
  25. {$ifdef tos}
  26. {$define shortasmprefix}
  27. {$endif}
  28. {$ifdef OS2}
  29. { Allthough OS/2 supports long filenames I play it safe and
  30. use 8.3 filenames, because this allows the compiler to run
  31. on a FAT partition. (DM) }
  32. {$define shortasmprefix}
  33. {$endif}
  34. interface
  35. uses
  36. cutils,cclasses,
  37. globals,finput,
  38. symbase,symsym,aasmbase;
  39. type
  40. trecompile_reason = (rr_unknown,
  41. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  42. );
  43. TExternalsItem=class(TLinkedListItem)
  44. public
  45. found : longbool;
  46. data : pstring;
  47. constructor Create(const s:string);
  48. Destructor Destroy;override;
  49. end;
  50. tlinkcontaineritem=class(tlinkedlistitem)
  51. public
  52. data : pstring;
  53. needlink : cardinal;
  54. constructor Create(const s:string;m:cardinal);
  55. destructor Destroy;override;
  56. end;
  57. tlinkcontainer=class(tlinkedlist)
  58. procedure add(const s : string;m:cardinal);
  59. function get(var m:cardinal) : string;
  60. function getusemask(mask:cardinal) : string;
  61. function find(const s:string):boolean;
  62. end;
  63. tmodule = class;
  64. tused_unit = class;
  65. tunitmaprec = record
  66. u : tmodule;
  67. { number of references }
  68. refs : longint;
  69. { index in the derefmap }
  70. derefidx : longint;
  71. end;
  72. punitmap = ^tunitmaprec;
  73. tderefmaprec = record
  74. u : tmodule;
  75. { modulename, used during ppu load }
  76. modulename : pstring;
  77. end;
  78. pderefmap = ^tderefmaprec;
  79. tmodule = class(tmodulebase)
  80. do_reload, { force reloading of the unit }
  81. do_compile, { need to compile the sources }
  82. sources_avail, { if all sources are reachable }
  83. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  84. is_stab_written,
  85. is_reset,
  86. is_unit,
  87. in_interface, { processing the implementation part? }
  88. in_global : boolean; { allow global settings }
  89. mode_switch_allowed : boolean; { Whether a mode switch is still allowed at this point in the parsing.}
  90. mainfilepos : tfileposinfo;
  91. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  92. crc,
  93. interface_crc : cardinal;
  94. flags : cardinal; { the PPU flags }
  95. islibrary : boolean; { if it is a library (win32 dll) }
  96. moduleid : longint;
  97. unitmap : punitmap; { mapping of all used units }
  98. unitmapsize : longint; { number of units in the map }
  99. derefmap : pderefmap; { mapping of all units needed for deref }
  100. derefmapcnt : longint; { number of units in the map }
  101. derefmapsize : longint; { number of units in the map }
  102. derefdataintflen : longint;
  103. derefdata : tdynamicarray;
  104. globalsymtable, { pointer to the global symtable of this unit }
  105. localsymtable : tsymtable;{ pointer to the local symtable of this unit }
  106. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  107. localmacrosymtable : tsymtable;{ pointer to the local macro symtable of this unit }
  108. scanner : pointer; { scanner object used }
  109. procinfo : pointer; { current procedure being compiled }
  110. loaded_from : tmodule;
  111. uses_imports : boolean; { Set if the module imports from DLL's.}
  112. imports : tlinkedlist;
  113. _exports : tlinkedlist;
  114. externals : tlinkedlist; {Only for DLL scanners by using Unix-style $LINKLIB }
  115. resourcefiles : tstringlist;
  116. linkunitofiles,
  117. linkunitstaticlibs,
  118. linkunitsharedlibs,
  119. linkotherofiles, { objects,libs loaded from the source }
  120. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  121. linkotherstaticlibs : tlinkcontainer;
  122. used_units : tlinkedlist;
  123. dependent_units : tlinkedlist;
  124. localunitsearchpath, { local searchpaths }
  125. localobjectsearchpath,
  126. localincludesearchpath,
  127. locallibrarysearchpath : TSearchPathList;
  128. asmprefix : pstring; { prefix for the smartlink asmfiles }
  129. librarydata : tasmlibrarydata; { librarydata for this module }
  130. {create creates a new module which name is stored in 's'. LoadedFrom
  131. points to the module calling it. It is nil for the first compiled
  132. module. This allow inheritence of all path lists. MUST pay attention
  133. to that when creating link.res!!!!(mazen)}
  134. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  135. destructor destroy;override;
  136. procedure reset;virtual;
  137. procedure adddependency(callermodule:tmodule);
  138. procedure flagdependent(callermodule:tmodule);
  139. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  140. procedure updatemaps;
  141. function derefidx_unit(id:longint):longint;
  142. function resolve_unit(id:longint):tmodule;
  143. procedure allunitsused;
  144. procedure setmodulename(const s:string);
  145. end;
  146. tused_unit = class(tlinkedlistitem)
  147. checksum,
  148. interface_checksum : cardinal;
  149. in_uses,
  150. in_interface : boolean;
  151. u : tmodule;
  152. unitsym : tunitsym;
  153. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  154. end;
  155. tdependent_unit = class(tlinkedlistitem)
  156. u : tmodule;
  157. constructor create(_u : tmodule);
  158. end;
  159. var
  160. main_module : tmodule; { Main module of the program }
  161. current_module : tmodule; { Current module which is compiled or loaded }
  162. compiled_module : tmodule; { Current module which is compiled }
  163. usedunits : tlinkedlist; { Used units for this program }
  164. loaded_units : tlinkedlist; { All loaded units }
  165. SmartLinkOFiles : TStringList; { List of .o files which are generated,
  166. used to delete them after linking }
  167. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  168. procedure addloadedunit(hp:tmodule);
  169. implementation
  170. uses
  171. {$IFDEF USE_SYSUTILS}
  172. SysUtils,
  173. GlobType,
  174. {$ELSE USE_SYSUTILS}
  175. dos,
  176. {$ENDIF USE_SYSUTILS}
  177. verbose,systems,
  178. scanner,ppu,
  179. procinfo;
  180. {*****************************************************************************
  181. Global Functions
  182. *****************************************************************************}
  183. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  184. var
  185. hp : tmodule;
  186. begin
  187. hp:=tmodule(loaded_units.first);
  188. while assigned(hp) and (hp.unit_index<>moduleindex) do
  189. hp:=tmodule(hp.next);
  190. if assigned(hp) then
  191. get_source_file:=hp.sourcefiles.get_file(fileindex)
  192. else
  193. get_source_file:=nil;
  194. end;
  195. procedure addloadedunit(hp:tmodule);
  196. begin
  197. hp.moduleid:=loaded_units.count;
  198. loaded_units.concat(hp);
  199. end;
  200. {****************************************************************************
  201. TLinkContainerItem
  202. ****************************************************************************}
  203. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  204. begin
  205. inherited Create;
  206. data:=stringdup(s);
  207. needlink:=m;
  208. end;
  209. destructor TLinkContainerItem.Destroy;
  210. begin
  211. stringdispose(data);
  212. end;
  213. {****************************************************************************
  214. TLinkContainer
  215. ****************************************************************************}
  216. procedure TLinkContainer.add(const s : string;m:cardinal);
  217. begin
  218. inherited concat(TLinkContainerItem.Create(s,m));
  219. end;
  220. function TLinkContainer.get(var m:cardinal) : string;
  221. var
  222. p : tlinkcontaineritem;
  223. begin
  224. p:=tlinkcontaineritem(inherited getfirst);
  225. if p=nil then
  226. begin
  227. get:='';
  228. m:=0;
  229. end
  230. else
  231. begin
  232. get:=p.data^;
  233. m:=p.needlink;
  234. p.free;
  235. end;
  236. end;
  237. function TLinkContainer.getusemask(mask:cardinal) : string;
  238. var
  239. p : tlinkcontaineritem;
  240. found : boolean;
  241. begin
  242. found:=false;
  243. repeat
  244. p:=tlinkcontaineritem(inherited getfirst);
  245. if p=nil then
  246. begin
  247. getusemask:='';
  248. exit;
  249. end;
  250. getusemask:=p.data^;
  251. found:=(p.needlink and mask)<>0;
  252. p.free;
  253. until found;
  254. end;
  255. function TLinkContainer.find(const s:string):boolean;
  256. var
  257. newnode : tlinkcontaineritem;
  258. begin
  259. find:=false;
  260. newnode:=tlinkcontaineritem(First);
  261. while assigned(newnode) do
  262. begin
  263. if newnode.data^=s then
  264. begin
  265. find:=true;
  266. exit;
  267. end;
  268. newnode:=tlinkcontaineritem(newnode.next);
  269. end;
  270. end;
  271. {****************************************************************************
  272. TExternalsItem
  273. ****************************************************************************}
  274. constructor tExternalsItem.Create(const s:string);
  275. begin
  276. inherited Create;
  277. found:=false;
  278. data:=stringdup(s);
  279. end;
  280. destructor tExternalsItem.Destroy;
  281. begin
  282. stringdispose(data);
  283. inherited;
  284. end;
  285. {****************************************************************************
  286. TUSED_UNIT
  287. ****************************************************************************}
  288. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  289. begin
  290. u:=_u;
  291. in_interface:=intface;
  292. in_uses:=inuses;
  293. unitsym:=usym;
  294. if _u.state=ms_compiled then
  295. begin
  296. checksum:=u.crc;
  297. interface_checksum:=u.interface_crc;
  298. end
  299. else
  300. begin
  301. checksum:=0;
  302. interface_checksum:=0;
  303. end;
  304. end;
  305. {****************************************************************************
  306. TDENPENDENT_UNIT
  307. ****************************************************************************}
  308. constructor tdependent_unit.create(_u : tmodule);
  309. begin
  310. u:=_u;
  311. end;
  312. {****************************************************************************
  313. TMODULE
  314. ****************************************************************************}
  315. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  316. var
  317. p : dirstr;
  318. n : namestr;
  319. e : extstr;
  320. begin
  321. {$IFDEF USE_SYSUTILS}
  322. p := SplitPath(s);
  323. n := SplitName(s);
  324. e := SplitExtension(s);
  325. {$ELSE USE_SYSUTILS}
  326. FSplit(s,p,n,e);
  327. {$ENDIF USE_SYSUTILS}
  328. { Programs have the name 'Program' to don't conflict with dup id's }
  329. if _is_unit then
  330. inherited create(n)
  331. else
  332. inherited create('Program');
  333. mainsource:=stringdup(s);
  334. { Dos has the famous 8.3 limit :( }
  335. {$ifdef shortasmprefix}
  336. asmprefix:=stringdup(FixFileName('as'));
  337. {$else}
  338. asmprefix:=stringdup(FixFileName(n));
  339. {$endif}
  340. setfilename(p+n,true);
  341. localunitsearchpath:=TSearchPathList.Create;
  342. localobjectsearchpath:=TSearchPathList.Create;
  343. localincludesearchpath:=TSearchPathList.Create;
  344. locallibrarysearchpath:=TSearchPathList.Create;
  345. used_units:=TLinkedList.Create;
  346. dependent_units:=TLinkedList.Create;
  347. resourcefiles:=TStringList.Create;
  348. linkunitofiles:=TLinkContainer.Create;
  349. linkunitstaticlibs:=TLinkContainer.Create;
  350. linkunitsharedlibs:=TLinkContainer.Create;
  351. linkotherofiles:=TLinkContainer.Create;
  352. linkotherstaticlibs:=TLinkContainer.Create;
  353. linkothersharedlibs:=TLinkContainer.Create;
  354. crc:=0;
  355. interface_crc:=0;
  356. flags:=0;
  357. scanner:=nil;
  358. unitmap:=nil;
  359. unitmapsize:=0;
  360. derefmap:=nil;
  361. derefmapsize:=0;
  362. derefmapcnt:=0;
  363. derefdata:=TDynamicArray.Create(1024);
  364. derefdataintflen:=0;
  365. globalsymtable:=nil;
  366. localsymtable:=nil;
  367. globalmacrosymtable:=nil;
  368. localmacrosymtable:=nil;
  369. loaded_from:=LoadedFrom;
  370. do_reload:=false;
  371. do_compile:=false;
  372. sources_avail:=true;
  373. mainfilepos.line:=0;
  374. mainfilepos.column:=0;
  375. mainfilepos.fileindex:=0;
  376. recompile_reason:=rr_unknown;
  377. in_interface:=true;
  378. in_global:=true;
  379. is_unit:=_is_unit;
  380. islibrary:=false;
  381. is_stab_written:=false;
  382. is_reset:=false;
  383. mode_switch_allowed:= true;
  384. uses_imports:=false;
  385. imports:=TLinkedList.Create;
  386. _exports:=TLinkedList.Create;
  387. externals:=TLinkedList.Create;
  388. librarydata:=tasmlibrarydata.create(realmodulename^);
  389. end;
  390. destructor tmodule.Destroy;
  391. var
  392. {$ifdef MEMDEBUG}
  393. d : tmemdebug;
  394. {$endif}
  395. i : longint;
  396. hpi : tprocinfo;
  397. begin
  398. if assigned(unitmap) then
  399. freemem(unitmap);
  400. if assigned(derefmap) then
  401. begin
  402. for i:=0 to derefmapcnt-1 do
  403. stringdispose(derefmap[i].modulename);
  404. freemem(derefmap);
  405. end;
  406. if assigned(imports) then
  407. imports.free;
  408. if assigned(_exports) then
  409. _exports.free;
  410. if assigned(externals) then
  411. externals.free;
  412. if assigned(scanner) then
  413. begin
  414. { also update current_scanner if it was pointing
  415. to this module }
  416. if current_scanner=tscannerfile(scanner) then
  417. current_scanner:=nil;
  418. tscannerfile(scanner).free;
  419. end;
  420. if assigned(procinfo) then
  421. begin
  422. if current_procinfo=tprocinfo(procinfo) then
  423. current_procinfo:=nil;
  424. { release procinfo tree }
  425. while assigned(procinfo) do
  426. begin
  427. hpi:=tprocinfo(procinfo).parent;
  428. tprocinfo(procinfo).free;
  429. procinfo:=hpi;
  430. end;
  431. end;
  432. used_units.free;
  433. dependent_units.free;
  434. resourcefiles.Free;
  435. linkunitofiles.Free;
  436. linkunitstaticlibs.Free;
  437. linkunitsharedlibs.Free;
  438. linkotherofiles.Free;
  439. linkotherstaticlibs.Free;
  440. linkothersharedlibs.Free;
  441. stringdispose(objfilename);
  442. stringdispose(newfilename);
  443. stringdispose(ppufilename);
  444. stringdispose(staticlibfilename);
  445. stringdispose(sharedlibfilename);
  446. stringdispose(exefilename);
  447. stringdispose(outputpath);
  448. stringdispose(path);
  449. stringdispose(realmodulename);
  450. stringdispose(mainsource);
  451. stringdispose(asmprefix);
  452. localunitsearchpath.Free;
  453. localobjectsearchpath.free;
  454. localincludesearchpath.free;
  455. locallibrarysearchpath.free;
  456. {$ifdef MEMDEBUG}
  457. d:=tmemdebug.create(modulename^+' - symtable');
  458. {$endif}
  459. derefdata.free;
  460. if assigned(globalsymtable) then
  461. globalsymtable.free;
  462. if assigned(localsymtable) then
  463. localsymtable.free;
  464. if assigned(globalmacrosymtable) then
  465. globalmacrosymtable.free;
  466. if assigned(localmacrosymtable) then
  467. localmacrosymtable.free;
  468. {$ifdef MEMDEBUG}
  469. d.free;
  470. {$endif}
  471. {$ifdef MEMDEBUG}
  472. d:=tmemdebug.create(modulename^+' - librarydata');
  473. {$endif}
  474. librarydata.free;
  475. {$ifdef MEMDEBUG}
  476. d.free;
  477. {$endif}
  478. stringdispose(modulename);
  479. inherited Destroy;
  480. end;
  481. procedure tmodule.reset;
  482. var
  483. hpi : tprocinfo;
  484. i : longint;
  485. begin
  486. if assigned(scanner) then
  487. begin
  488. { also update current_scanner if it was pointing
  489. to this module }
  490. if current_scanner=tscannerfile(scanner) then
  491. current_scanner:=nil;
  492. tscannerfile(scanner).free;
  493. scanner:=nil;
  494. end;
  495. if assigned(procinfo) then
  496. begin
  497. if current_procinfo=tprocinfo(procinfo) then
  498. current_procinfo:=nil;
  499. { release procinfo tree }
  500. while assigned(procinfo) do
  501. begin
  502. hpi:=tprocinfo(procinfo).parent;
  503. tprocinfo(procinfo).free;
  504. procinfo:=hpi;
  505. end;
  506. end;
  507. if assigned(globalsymtable) then
  508. begin
  509. globalsymtable.free;
  510. globalsymtable:=nil;
  511. end;
  512. if assigned(localsymtable) then
  513. begin
  514. localsymtable.free;
  515. localsymtable:=nil;
  516. end;
  517. if assigned(globalmacrosymtable) then
  518. begin
  519. globalmacrosymtable.free;
  520. globalmacrosymtable:=nil;
  521. end;
  522. if assigned(localmacrosymtable) then
  523. begin
  524. localmacrosymtable.free;
  525. localmacrosymtable:=nil;
  526. end;
  527. derefdata.free;
  528. derefdata:=TDynamicArray.Create(1024);
  529. if assigned(unitmap) then
  530. begin
  531. freemem(unitmap);
  532. unitmap:=nil;
  533. end;
  534. if assigned(derefmap) then
  535. begin
  536. for i:=0 to derefmapcnt-1 do
  537. stringdispose(derefmap[i].modulename);
  538. freemem(derefmap);
  539. derefmap:=nil;
  540. end;
  541. unitmapsize:=0;
  542. derefmapsize:=0;
  543. derefmapcnt:=0;
  544. derefdataintflen:=0;
  545. sourcefiles.free;
  546. sourcefiles:=tinputfilemanager.create;
  547. librarydata.free;
  548. librarydata:=tasmlibrarydata.create(realmodulename^);
  549. imports.free;
  550. imports:=tlinkedlist.create;
  551. _exports.free;
  552. _exports:=tlinkedlist.create;
  553. externals.free;
  554. externals:=tlinkedlist.create;
  555. used_units.free;
  556. used_units:=TLinkedList.Create;
  557. dependent_units.free;
  558. dependent_units:=TLinkedList.Create;
  559. resourcefiles.Free;
  560. resourcefiles:=TStringList.Create;
  561. linkunitofiles.Free;
  562. linkunitofiles:=TLinkContainer.Create;
  563. linkunitstaticlibs.Free;
  564. linkunitstaticlibs:=TLinkContainer.Create;
  565. linkunitsharedlibs.Free;
  566. linkunitsharedlibs:=TLinkContainer.Create;
  567. linkotherofiles.Free;
  568. linkotherofiles:=TLinkContainer.Create;
  569. linkotherstaticlibs.Free;
  570. linkotherstaticlibs:=TLinkContainer.Create;
  571. linkothersharedlibs.Free;
  572. linkothersharedlibs:=TLinkContainer.Create;
  573. uses_imports:=false;
  574. do_compile:=false;
  575. do_reload:=false;
  576. interface_compiled:=false;
  577. in_interface:=true;
  578. in_global:=true;
  579. mode_switch_allowed:=true;
  580. is_stab_written:=false;
  581. is_reset:=false;
  582. crc:=0;
  583. interface_crc:=0;
  584. flags:=0;
  585. mainfilepos.line:=0;
  586. mainfilepos.column:=0;
  587. mainfilepos.fileindex:=0;
  588. recompile_reason:=rr_unknown;
  589. {
  590. The following fields should not
  591. be reset:
  592. mainsource
  593. state
  594. loaded_from
  595. sources_avail
  596. }
  597. end;
  598. procedure tmodule.adddependency(callermodule:tmodule);
  599. begin
  600. { This is not needed for programs }
  601. if not callermodule.is_unit then
  602. exit;
  603. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  604. dependent_units.concat(tdependent_unit.create(callermodule));
  605. end;
  606. procedure tmodule.flagdependent(callermodule:tmodule);
  607. var
  608. pm : tdependent_unit;
  609. begin
  610. { flag all units that depend on this unit for reloading }
  611. pm:=tdependent_unit(current_module.dependent_units.first);
  612. while assigned(pm) do
  613. begin
  614. { We do not have to reload the unit that wants to load
  615. this unit, unless this unit is already compiled during
  616. the loading }
  617. if (pm.u=callermodule) and
  618. (pm.u.state<>ms_compiled) then
  619. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  620. else
  621. if pm.u.state=ms_second_compile then
  622. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  623. else
  624. begin
  625. pm.u.do_reload:=true;
  626. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  627. end;
  628. pm:=tdependent_unit(pm.next);
  629. end;
  630. end;
  631. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  632. var
  633. pu : tused_unit;
  634. begin
  635. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  636. used_units.concat(pu);
  637. addusedunit:=pu;
  638. end;
  639. procedure tmodule.updatemaps;
  640. var
  641. oldmapsize : longint;
  642. hp : tmodule;
  643. i : longint;
  644. begin
  645. { Extend unitmap }
  646. oldmapsize:=unitmapsize;
  647. unitmapsize:=loaded_units.count;
  648. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  649. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  650. { Extend Derefmap }
  651. oldmapsize:=derefmapsize;
  652. derefmapsize:=loaded_units.count;
  653. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  654. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  655. { Add all units to unitmap }
  656. hp:=tmodule(loaded_units.first);
  657. i:=0;
  658. while assigned(hp) do
  659. begin
  660. if hp.moduleid>=unitmapsize then
  661. internalerror(200501151);
  662. { Verify old entries }
  663. if (i<oldmapsize) then
  664. begin
  665. if (hp.moduleid<>i) or
  666. (unitmap[hp.moduleid].u<>hp) then
  667. internalerror(200501156);
  668. end
  669. else
  670. begin
  671. unitmap[hp.moduleid].u:=hp;
  672. unitmap[hp.moduleid].derefidx:=-1;
  673. end;
  674. inc(i);
  675. hp:=tmodule(hp.next);
  676. end;
  677. end;
  678. function tmodule.derefidx_unit(id:longint):longint;
  679. begin
  680. if id>=unitmapsize then
  681. internalerror(2005011511);
  682. if unitmap[id].derefidx=-1 then
  683. begin
  684. unitmap[id].derefidx:=derefmapcnt;
  685. inc(derefmapcnt);
  686. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  687. end;
  688. if unitmap[id].derefidx>=derefmapsize then
  689. internalerror(2005011514);
  690. result:=unitmap[id].derefidx;
  691. end;
  692. function tmodule.resolve_unit(id:longint):tmodule;
  693. var
  694. hp : tmodule;
  695. begin
  696. if id>=derefmapsize then
  697. internalerror(200306231);
  698. result:=derefmap[id].u;
  699. if not assigned(result) then
  700. begin
  701. if not assigned(derefmap[id].modulename) or
  702. (derefmap[id].modulename^='') then
  703. internalerror(200501159);
  704. hp:=tmodule(loaded_units.first);
  705. while assigned(hp) do
  706. begin
  707. if hp.modulename^=derefmap[id].modulename^ then
  708. break;
  709. hp:=tmodule(hp.next);
  710. end;
  711. if not assigned(hp) then
  712. internalerror(2005011510);
  713. derefmap[id].u:=hp;
  714. result:=hp;
  715. end;
  716. end;
  717. procedure tmodule.allunitsused;
  718. var
  719. pu : tused_unit;
  720. begin
  721. pu:=tused_unit(used_units.first);
  722. while assigned(pu) do
  723. begin
  724. if assigned(pu.u.globalsymtable) then
  725. begin
  726. if unitmap[pu.u.moduleid].u<>pu.u then
  727. internalerror(200501157);
  728. { Give a note when the unit is not referenced, skip
  729. this is for units with an initialization/finalization }
  730. if (unitmap[pu.u.moduleid].refs=0) and
  731. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  732. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  733. end;
  734. pu:=tused_unit(pu.next);
  735. end;
  736. end;
  737. procedure tmodule.setmodulename(const s:string);
  738. begin
  739. stringdispose(modulename);
  740. stringdispose(realmodulename);
  741. modulename:=stringdup(upper(s));
  742. realmodulename:=stringdup(s);
  743. { also update asmlibrary names }
  744. librarydata.name:=modulename^;
  745. librarydata.realname:=realmodulename^;
  746. end;
  747. end.