fmodule.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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,aasmtai,aasmdata;
  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 : TObject; { scanner object used }
  115. procinfo : TObject; { current procedure being compiled }
  116. asmdata : TObject; { Assembler data }
  117. asmprefix : pstring; { prefix for the smartlink asmfiles }
  118. loaded_from : tmodule;
  119. uses_imports : boolean; { Set if the module imports from DLL's.}
  120. imports : tlinkedlist;
  121. _exports : tlinkedlist;
  122. externals : tlinkedlist; {Only for DLL scanners by using Unix-style $LINKLIB }
  123. resourcefiles : tstringlist;
  124. linkunitofiles,
  125. linkunitstaticlibs,
  126. linkunitsharedlibs,
  127. linkdlls,
  128. linkotherofiles, { objects,libs loaded from the source }
  129. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  130. linkotherstaticlibs : tlinkcontainer;
  131. used_units : tlinkedlist;
  132. dependent_units : tlinkedlist;
  133. localunitsearchpath, { local searchpaths }
  134. localobjectsearchpath,
  135. localincludesearchpath,
  136. locallibrarysearchpath : TSearchPathList;
  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. asmdata:=TAsmData.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(asmdata) then
  429. begin
  430. if current_asmdata=asmdata then
  431. current_asmdata:=nil;
  432. asmdata.free;
  433. end;
  434. if assigned(procinfo) then
  435. begin
  436. if current_procinfo=tprocinfo(procinfo) then
  437. current_procinfo:=nil;
  438. { release procinfo tree }
  439. while assigned(procinfo) do
  440. begin
  441. hpi:=tprocinfo(procinfo).parent;
  442. tprocinfo(procinfo).free;
  443. procinfo:=hpi;
  444. end;
  445. end;
  446. used_units.free;
  447. dependent_units.free;
  448. resourcefiles.Free;
  449. linkunitofiles.Free;
  450. linkunitstaticlibs.Free;
  451. linkunitsharedlibs.Free;
  452. linkotherofiles.Free;
  453. linkotherstaticlibs.Free;
  454. linkothersharedlibs.Free;
  455. linkdlls.Free;
  456. stringdispose(objfilename);
  457. stringdispose(newfilename);
  458. stringdispose(ppufilename);
  459. stringdispose(importlibfilename);
  460. stringdispose(staticlibfilename);
  461. stringdispose(sharedlibfilename);
  462. stringdispose(exefilename);
  463. stringdispose(outputpath);
  464. stringdispose(path);
  465. stringdispose(realmodulename);
  466. stringdispose(mainsource);
  467. stringdispose(asmprefix);
  468. localunitsearchpath.Free;
  469. localobjectsearchpath.free;
  470. localincludesearchpath.free;
  471. locallibrarysearchpath.free;
  472. {$ifdef MEMDEBUG}
  473. d:=tmemdebug.create(modulename^+' - symtable');
  474. {$endif}
  475. derefdata.free;
  476. if assigned(globalsymtable) then
  477. globalsymtable.free;
  478. if assigned(localsymtable) then
  479. localsymtable.free;
  480. if assigned(globalmacrosymtable) then
  481. globalmacrosymtable.free;
  482. if assigned(localmacrosymtable) then
  483. localmacrosymtable.free;
  484. {$ifdef MEMDEBUG}
  485. d.free;
  486. {$endif}
  487. stringdispose(modulename);
  488. inherited Destroy;
  489. end;
  490. procedure tmodule.reset;
  491. var
  492. hpi : tprocinfo;
  493. i : longint;
  494. begin
  495. if assigned(scanner) then
  496. begin
  497. { also update current_scanner if it was pointing
  498. to this module }
  499. if current_scanner=tscannerfile(scanner) then
  500. current_scanner:=nil;
  501. tscannerfile(scanner).free;
  502. scanner:=nil;
  503. end;
  504. if assigned(procinfo) then
  505. begin
  506. if current_procinfo=tprocinfo(procinfo) then
  507. current_procinfo:=nil;
  508. { release procinfo tree }
  509. while assigned(procinfo) do
  510. begin
  511. hpi:=tprocinfo(procinfo).parent;
  512. tprocinfo(procinfo).free;
  513. procinfo:=hpi;
  514. end;
  515. end;
  516. if assigned(asmdata) then
  517. begin
  518. if current_asmdata=TAsmData(asmdata) then
  519. current_asmdata:=nil;
  520. asmdata.free;
  521. asmdata:=nil;
  522. end;
  523. if assigned(globalsymtable) then
  524. begin
  525. globalsymtable.free;
  526. globalsymtable:=nil;
  527. end;
  528. if assigned(localsymtable) then
  529. begin
  530. localsymtable.free;
  531. localsymtable:=nil;
  532. end;
  533. if assigned(globalmacrosymtable) then
  534. begin
  535. globalmacrosymtable.free;
  536. globalmacrosymtable:=nil;
  537. end;
  538. if assigned(localmacrosymtable) then
  539. begin
  540. localmacrosymtable.free;
  541. localmacrosymtable:=nil;
  542. end;
  543. derefdata.free;
  544. derefdata:=TDynamicArray.Create(1024);
  545. if assigned(unitmap) then
  546. begin
  547. freemem(unitmap);
  548. unitmap:=nil;
  549. end;
  550. if assigned(derefmap) then
  551. begin
  552. for i:=0 to derefmapcnt-1 do
  553. stringdispose(derefmap[i].modulename);
  554. freemem(derefmap);
  555. derefmap:=nil;
  556. end;
  557. unitmapsize:=0;
  558. derefmapsize:=0;
  559. derefmapcnt:=0;
  560. derefdataintflen:=0;
  561. sourcefiles.free;
  562. sourcefiles:=tinputfilemanager.create;
  563. asmdata:=TAsmData.create(realmodulename^);
  564. imports.free;
  565. imports:=tlinkedlist.create;
  566. _exports.free;
  567. _exports:=tlinkedlist.create;
  568. externals.free;
  569. externals:=tlinkedlist.create;
  570. used_units.free;
  571. used_units:=TLinkedList.Create;
  572. dependent_units.free;
  573. dependent_units:=TLinkedList.Create;
  574. resourcefiles.Free;
  575. resourcefiles:=TStringList.Create;
  576. linkunitofiles.Free;
  577. linkunitofiles:=TLinkContainer.Create;
  578. linkunitstaticlibs.Free;
  579. linkunitstaticlibs:=TLinkContainer.Create;
  580. linkunitsharedlibs.Free;
  581. linkunitsharedlibs:=TLinkContainer.Create;
  582. linkotherofiles.Free;
  583. linkotherofiles:=TLinkContainer.Create;
  584. linkotherstaticlibs.Free;
  585. linkotherstaticlibs:=TLinkContainer.Create;
  586. linkothersharedlibs.Free;
  587. linkothersharedlibs:=TLinkContainer.Create;
  588. linkdlls.Free;
  589. linkdlls:=TLinkContainer.Create;
  590. uses_imports:=false;
  591. do_compile:=false;
  592. do_reload:=false;
  593. interface_compiled:=false;
  594. in_interface:=true;
  595. in_global:=true;
  596. mode_switch_allowed:=true;
  597. is_dbginfo_written:=false;
  598. is_reset:=false;
  599. crc:=0;
  600. interface_crc:=0;
  601. flags:=0;
  602. mainfilepos.line:=0;
  603. mainfilepos.column:=0;
  604. mainfilepos.fileindex:=0;
  605. recompile_reason:=rr_unknown;
  606. {
  607. The following fields should not
  608. be reset:
  609. mainsource
  610. state
  611. loaded_from
  612. sources_avail
  613. }
  614. end;
  615. procedure tmodule.adddependency(callermodule:tmodule);
  616. begin
  617. { This is not needed for programs }
  618. if not callermodule.is_unit then
  619. exit;
  620. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  621. dependent_units.concat(tdependent_unit.create(callermodule));
  622. end;
  623. procedure tmodule.flagdependent(callermodule:tmodule);
  624. var
  625. pm : tdependent_unit;
  626. begin
  627. { flag all units that depend on this unit for reloading }
  628. pm:=tdependent_unit(current_module.dependent_units.first);
  629. while assigned(pm) do
  630. begin
  631. { We do not have to reload the unit that wants to load
  632. this unit, unless this unit is already compiled during
  633. the loading }
  634. if (pm.u=callermodule) and
  635. (pm.u.state<>ms_compiled) then
  636. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  637. else
  638. if pm.u.state=ms_second_compile then
  639. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  640. else
  641. begin
  642. pm.u.do_reload:=true;
  643. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  644. end;
  645. pm:=tdependent_unit(pm.next);
  646. end;
  647. end;
  648. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  649. var
  650. pu : tused_unit;
  651. begin
  652. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  653. used_units.concat(pu);
  654. addusedunit:=pu;
  655. end;
  656. procedure tmodule.updatemaps;
  657. var
  658. oldmapsize : longint;
  659. hp : tmodule;
  660. i : longint;
  661. begin
  662. { Extend unitmap }
  663. oldmapsize:=unitmapsize;
  664. unitmapsize:=loaded_units.count;
  665. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  666. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  667. { Extend Derefmap }
  668. oldmapsize:=derefmapsize;
  669. derefmapsize:=loaded_units.count;
  670. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  671. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  672. { Add all units to unitmap }
  673. hp:=tmodule(loaded_units.first);
  674. i:=0;
  675. while assigned(hp) do
  676. begin
  677. if hp.moduleid>=unitmapsize then
  678. internalerror(200501151);
  679. { Verify old entries }
  680. if (i<oldmapsize) then
  681. begin
  682. if (hp.moduleid<>i) or
  683. (unitmap[hp.moduleid].u<>hp) then
  684. internalerror(200501156);
  685. end
  686. else
  687. begin
  688. unitmap[hp.moduleid].u:=hp;
  689. unitmap[hp.moduleid].derefidx:=-1;
  690. end;
  691. inc(i);
  692. hp:=tmodule(hp.next);
  693. end;
  694. end;
  695. function tmodule.derefidx_unit(id:longint):longint;
  696. begin
  697. if id>=unitmapsize then
  698. internalerror(2005011511);
  699. if unitmap[id].derefidx=-1 then
  700. begin
  701. unitmap[id].derefidx:=derefmapcnt;
  702. inc(derefmapcnt);
  703. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  704. end;
  705. if unitmap[id].derefidx>=derefmapsize then
  706. internalerror(2005011514);
  707. result:=unitmap[id].derefidx;
  708. end;
  709. function tmodule.resolve_unit(id:longint):tmodule;
  710. var
  711. hp : tmodule;
  712. begin
  713. if id>=derefmapsize then
  714. internalerror(200306231);
  715. result:=derefmap[id].u;
  716. if not assigned(result) then
  717. begin
  718. if not assigned(derefmap[id].modulename) or
  719. (derefmap[id].modulename^='') then
  720. internalerror(200501159);
  721. hp:=tmodule(loaded_units.first);
  722. while assigned(hp) do
  723. begin
  724. if hp.modulename^=derefmap[id].modulename^ then
  725. break;
  726. hp:=tmodule(hp.next);
  727. end;
  728. if not assigned(hp) then
  729. internalerror(2005011510);
  730. derefmap[id].u:=hp;
  731. result:=hp;
  732. end;
  733. end;
  734. procedure tmodule.allunitsused;
  735. var
  736. pu : tused_unit;
  737. begin
  738. pu:=tused_unit(used_units.first);
  739. while assigned(pu) do
  740. begin
  741. if assigned(pu.u.globalsymtable) then
  742. begin
  743. if unitmap[pu.u.moduleid].u<>pu.u then
  744. internalerror(200501157);
  745. { Give a note when the unit is not referenced, skip
  746. this is for units with an initialization/finalization }
  747. if (unitmap[pu.u.moduleid].refs=0) and
  748. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  749. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  750. end;
  751. pu:=tused_unit(pu.next);
  752. end;
  753. end;
  754. procedure tmodule.setmodulename(const s:string);
  755. begin
  756. stringdispose(modulename);
  757. stringdispose(realmodulename);
  758. modulename:=stringdup(upper(s));
  759. realmodulename:=stringdup(s);
  760. { also update asmlibrary names }
  761. current_asmdata.name:=modulename^;
  762. current_asmdata.realname:=realmodulename^;
  763. end;
  764. end.