fmodule.pas 26 KB

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