fmodule.pas 26 KB

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