fmodule.pas 29 KB

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