fmodule.pas 30 KB

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