fmodule.pas 30 KB

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