fmodule.pas 29 KB

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