fmodule.pas 30 KB

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