fmodule.pas 29 KB

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