fmodule.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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,ogbase,
  38. symbase,symsym,aasmbase,aasmtai,aasmdata;
  39. const
  40. UNSPECIFIED_LIBRARY_NAME = '<none>';
  41. type
  42. trecompile_reason = (rr_unknown,
  43. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  44. );
  45. tlinkcontaineritem=class(tlinkedlistitem)
  46. public
  47. data : pstring;
  48. needlink : cardinal;
  49. constructor Create(const s:string;m:cardinal);
  50. destructor Destroy;override;
  51. end;
  52. tlinkcontainer=class(tlinkedlist)
  53. procedure add(const s : string;m:cardinal);
  54. function get(var m:cardinal) : string;
  55. function getusemask(mask:cardinal) : string;
  56. function find(const s:string):boolean;
  57. end;
  58. tmodule = class;
  59. tused_unit = class;
  60. tunitmaprec = record
  61. u : tmodule;
  62. { number of references }
  63. refs : longint;
  64. { index in the derefmap }
  65. derefidx : longint;
  66. end;
  67. punitmap = ^tunitmaprec;
  68. tderefmaprec = record
  69. u : tmodule;
  70. { modulename, used during ppu load }
  71. modulename : pstring;
  72. end;
  73. pderefmap = ^tderefmaprec;
  74. tmodule = class(tmodulebase)
  75. private
  76. FImportLibraryList : TFPHashObjectList;
  77. public
  78. do_reload, { force reloading of the unit }
  79. do_compile, { need to compile the sources }
  80. sources_avail, { if all sources are reachable }
  81. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  82. is_dbginfo_written,
  83. is_reset,
  84. is_unit,
  85. in_interface, { processing the implementation part? }
  86. { allow global settings }
  87. in_global : boolean;
  88. { Whether a mode switch is still allowed at this point in the parsing.}
  89. mode_switch_allowed,
  90. { generate pic helper which loads eip in ecx (for leave procedures) }
  91. requires_ecx_pic_helper,
  92. { generate pic helper which loads eip in ebx (for non leave procedures) }
  93. requires_ebx_pic_helper : boolean;
  94. mainfilepos : tfileposinfo;
  95. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  96. crc,
  97. interface_crc : cardinal;
  98. flags : cardinal; { the PPU flags }
  99. islibrary : boolean; { if it is a library (win32 dll) }
  100. moduleid : longint;
  101. unitmap : punitmap; { mapping of all used units }
  102. unitmapsize : longint; { number of units in the map }
  103. derefmap : pderefmap; { mapping of all units needed for deref }
  104. derefmapcnt : longint; { number of units in the map }
  105. derefmapsize : longint; { number of units in the map }
  106. derefdataintflen : longint;
  107. derefdata : tdynamicarray;
  108. deflist,
  109. symlist : TFPObjectList;
  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. _exports : tlinkedlist;
  120. dllscannerinputlist : TFPHashList;
  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. {create creates a new module which name is stored in 's'. LoadedFrom
  135. points to the module calling it. It is nil for the first compiled
  136. module. This allow inheritence of all path lists. MUST pay attention
  137. to that when creating link.res!!!!(mazen)}
  138. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  139. destructor destroy;override;
  140. procedure reset;virtual;
  141. procedure adddependency(callermodule:tmodule);
  142. procedure flagdependent(callermodule:tmodule);
  143. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  144. procedure updatemaps;
  145. function derefidx_unit(id:longint):longint;
  146. function resolve_unit(id:longint):tmodule;
  147. procedure allunitsused;
  148. procedure setmodulename(const s:string);
  149. procedure AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean);
  150. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  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_module(moduleindex : longint) : tmodule;
  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_module(moduleindex : longint) : tmodule;
  191. var
  192. hp : tmodule;
  193. begin
  194. result:=nil;
  195. if moduleindex=0 then
  196. exit;
  197. result:=current_module;
  198. if not(assigned(loaded_units)) then
  199. exit;
  200. hp:=tmodule(loaded_units.first);
  201. while assigned(hp) and (hp.unit_index<>moduleindex) do
  202. hp:=tmodule(hp.next);
  203. result:=hp;
  204. end;
  205. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  206. var
  207. hp : tmodule;
  208. begin
  209. hp:=get_module(moduleindex);
  210. if assigned(hp) then
  211. get_source_file:=hp.sourcefiles.get_file(fileindex)
  212. else
  213. get_source_file:=nil;
  214. end;
  215. procedure addloadedunit(hp:tmodule);
  216. begin
  217. hp.moduleid:=loaded_units.count;
  218. loaded_units.concat(hp);
  219. end;
  220. {****************************************************************************
  221. TLinkContainerItem
  222. ****************************************************************************}
  223. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  224. begin
  225. inherited Create;
  226. data:=stringdup(s);
  227. needlink:=m;
  228. end;
  229. destructor TLinkContainerItem.Destroy;
  230. begin
  231. stringdispose(data);
  232. end;
  233. {****************************************************************************
  234. TLinkContainer
  235. ****************************************************************************}
  236. procedure TLinkContainer.add(const s : string;m:cardinal);
  237. begin
  238. inherited concat(TLinkContainerItem.Create(s,m));
  239. end;
  240. function TLinkContainer.get(var m:cardinal) : string;
  241. var
  242. p : tlinkcontaineritem;
  243. begin
  244. p:=tlinkcontaineritem(inherited getfirst);
  245. if p=nil then
  246. begin
  247. get:='';
  248. m:=0;
  249. end
  250. else
  251. begin
  252. get:=p.data^;
  253. m:=p.needlink;
  254. p.free;
  255. end;
  256. end;
  257. function TLinkContainer.getusemask(mask:cardinal) : string;
  258. var
  259. p : tlinkcontaineritem;
  260. found : boolean;
  261. begin
  262. found:=false;
  263. repeat
  264. p:=tlinkcontaineritem(inherited getfirst);
  265. if p=nil then
  266. begin
  267. getusemask:='';
  268. exit;
  269. end;
  270. getusemask:=p.data^;
  271. found:=(p.needlink and mask)<>0;
  272. p.free;
  273. until found;
  274. end;
  275. function TLinkContainer.find(const s:string):boolean;
  276. var
  277. newnode : tlinkcontaineritem;
  278. begin
  279. find:=false;
  280. newnode:=tlinkcontaineritem(First);
  281. while assigned(newnode) do
  282. begin
  283. if newnode.data^=s then
  284. begin
  285. find:=true;
  286. exit;
  287. end;
  288. newnode:=tlinkcontaineritem(newnode.next);
  289. end;
  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. FImportLibraryList:=TFPHashObjectList.Create(true);
  361. crc:=0;
  362. interface_crc:=0;
  363. flags:=0;
  364. scanner:=nil;
  365. unitmap:=nil;
  366. unitmapsize:=0;
  367. derefmap:=nil;
  368. derefmapsize:=0;
  369. derefmapcnt:=0;
  370. derefdata:=TDynamicArray.Create(1024);
  371. derefdataintflen:=0;
  372. deflist:=TFPObjectList.Create(false);
  373. symlist:=TFPObjectList.Create(false);
  374. globalsymtable:=nil;
  375. localsymtable:=nil;
  376. globalmacrosymtable:=nil;
  377. localmacrosymtable:=nil;
  378. loaded_from:=LoadedFrom;
  379. do_reload:=false;
  380. do_compile:=false;
  381. sources_avail:=true;
  382. mainfilepos.line:=0;
  383. mainfilepos.column:=0;
  384. mainfilepos.fileindex:=0;
  385. recompile_reason:=rr_unknown;
  386. in_interface:=true;
  387. in_global:=true;
  388. is_unit:=_is_unit;
  389. islibrary:=false;
  390. is_dbginfo_written:=false;
  391. is_reset:=false;
  392. mode_switch_allowed:= true;
  393. _exports:=TLinkedList.Create;
  394. dllscannerinputlist:=TFPHashList.Create;
  395. asmdata:=TAsmData.create(realmodulename^);
  396. end;
  397. destructor tmodule.Destroy;
  398. var
  399. {$ifdef MEMDEBUG}
  400. d : tmemdebug;
  401. {$endif}
  402. i : longint;
  403. hpi : tprocinfo;
  404. begin
  405. if assigned(unitmap) then
  406. freemem(unitmap);
  407. if assigned(derefmap) then
  408. begin
  409. for i:=0 to derefmapcnt-1 do
  410. stringdispose(derefmap[i].modulename);
  411. freemem(derefmap);
  412. end;
  413. if assigned(_exports) then
  414. _exports.free;
  415. if assigned(dllscannerinputlist) then
  416. dllscannerinputlist.free;
  417. if assigned(scanner) then
  418. begin
  419. { also update current_scanner if it was pointing
  420. to this module }
  421. if current_scanner=tscannerfile(scanner) then
  422. current_scanner:=nil;
  423. tscannerfile(scanner).free;
  424. end;
  425. if assigned(asmdata) then
  426. begin
  427. if current_asmdata=asmdata then
  428. current_asmdata:=nil;
  429. asmdata.free;
  430. end;
  431. if assigned(procinfo) then
  432. begin
  433. if current_procinfo=tprocinfo(procinfo) then
  434. current_procinfo:=nil;
  435. { release procinfo tree }
  436. while assigned(procinfo) do
  437. begin
  438. hpi:=tprocinfo(procinfo).parent;
  439. tprocinfo(procinfo).free;
  440. procinfo:=hpi;
  441. end;
  442. end;
  443. used_units.free;
  444. dependent_units.free;
  445. resourcefiles.Free;
  446. linkunitofiles.Free;
  447. linkunitstaticlibs.Free;
  448. linkunitsharedlibs.Free;
  449. linkotherofiles.Free;
  450. linkotherstaticlibs.Free;
  451. linkothersharedlibs.Free;
  452. FImportLibraryList.Free;
  453. stringdispose(objfilename);
  454. stringdispose(newfilename);
  455. stringdispose(ppufilename);
  456. stringdispose(importlibfilename);
  457. stringdispose(staticlibfilename);
  458. stringdispose(sharedlibfilename);
  459. stringdispose(exefilename);
  460. stringdispose(outputpath);
  461. stringdispose(path);
  462. stringdispose(realmodulename);
  463. stringdispose(mainsource);
  464. stringdispose(asmprefix);
  465. localunitsearchpath.Free;
  466. localobjectsearchpath.free;
  467. localincludesearchpath.free;
  468. locallibrarysearchpath.free;
  469. {$ifdef MEMDEBUG}
  470. d:=tmemdebug.create(modulename^+' - symtable');
  471. {$endif}
  472. derefdata.free;
  473. deflist.free;
  474. symlist.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. deflist.free;
  543. deflist:=TFPObjectList.Create(false);
  544. symlist.free;
  545. symlist:=TFPObjectList.Create(false);
  546. derefdata.free;
  547. derefdata:=TDynamicArray.Create(1024);
  548. if assigned(unitmap) then
  549. begin
  550. freemem(unitmap);
  551. unitmap:=nil;
  552. end;
  553. if assigned(derefmap) then
  554. begin
  555. for i:=0 to derefmapcnt-1 do
  556. stringdispose(derefmap[i].modulename);
  557. freemem(derefmap);
  558. derefmap:=nil;
  559. end;
  560. unitmapsize:=0;
  561. derefmapsize:=0;
  562. derefmapcnt:=0;
  563. derefdataintflen:=0;
  564. sourcefiles.free;
  565. sourcefiles:=tinputfilemanager.create;
  566. asmdata:=TAsmData.create(realmodulename^);
  567. _exports.free;
  568. _exports:=tlinkedlist.create;
  569. dllscannerinputlist.free;
  570. dllscannerinputlist:=TFPHashList.create;
  571. used_units.free;
  572. used_units:=TLinkedList.Create;
  573. dependent_units.free;
  574. dependent_units:=TLinkedList.Create;
  575. resourcefiles.Free;
  576. resourcefiles:=TStringList.Create;
  577. linkunitofiles.Free;
  578. linkunitofiles:=TLinkContainer.Create;
  579. linkunitstaticlibs.Free;
  580. linkunitstaticlibs:=TLinkContainer.Create;
  581. linkunitsharedlibs.Free;
  582. linkunitsharedlibs:=TLinkContainer.Create;
  583. linkotherofiles.Free;
  584. linkotherofiles:=TLinkContainer.Create;
  585. linkotherstaticlibs.Free;
  586. linkotherstaticlibs:=TLinkContainer.Create;
  587. linkothersharedlibs.Free;
  588. linkothersharedlibs:=TLinkContainer.Create;
  589. FImportLibraryList.Free;
  590. FImportLibraryList:=TFPHashObjectList.Create;
  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. procedure TModule.AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean);
  765. var
  766. ImportLibrary : TImportLibrary;
  767. ImportSymbol : TFPHashObject;
  768. begin
  769. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  770. if not assigned(ImportLibrary) then
  771. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  772. ImportSymbol:=TFPHashObject(ImportLibrary.ImportSymbolList.Find(symname));
  773. if not assigned(ImportSymbol) then
  774. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,symname,OrdNr,isvar);
  775. end;
  776. end.