2
0

fmodule.pas 30 KB

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