fmodule.pas 26 KB

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