fmodule.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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,cfileutils,
  37. globals,finput,ogbase,
  38. symbase,symsym,aasmbase,aasmtai,aasmdata;
  39. const
  40. UNSPECIFIED_LIBRARY_NAME = '<none>';
  41. type
  42. trecompile_reason = (rr_unknown,
  43. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  44. );
  45. tlinkcontaineritem=class(tlinkedlistitem)
  46. public
  47. data : pshortstring;
  48. needlink : cardinal;
  49. constructor Create(const s:string;m:cardinal);
  50. destructor Destroy;override;
  51. end;
  52. tlinkcontainer=class(tlinkedlist)
  53. procedure add(const s : string;m:cardinal);
  54. function get(var m:cardinal) : string;
  55. function getusemask(mask:cardinal) : string;
  56. function find(const s:string):boolean;
  57. end;
  58. tmodule = class;
  59. tused_unit = class;
  60. tunitmaprec = record
  61. u : tmodule;
  62. { number of references }
  63. refs : longint;
  64. { index in the derefmap }
  65. derefidx : longint;
  66. end;
  67. punitmap = ^tunitmaprec;
  68. tderefmaprec = record
  69. u : tmodule;
  70. { modulename, used during ppu load }
  71. modulename : pshortstring;
  72. end;
  73. pderefmap = ^tderefmaprec;
  74. tmodule = class(tmodulebase)
  75. private
  76. FImportLibraryList : TFPHashObjectList;
  77. public
  78. do_reload, { force reloading of the unit }
  79. do_compile, { need to compile the sources }
  80. sources_avail, { if all sources are reachable }
  81. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  82. is_dbginfo_written,
  83. is_reset,
  84. is_unit,
  85. in_interface, { processing the implementation part? }
  86. { allow global settings }
  87. in_global : boolean;
  88. { Whether a mode switch is still allowed at this point in the parsing.}
  89. mode_switch_allowed,
  90. { generate pic helper which loads eip in ecx (for leave procedures) }
  91. requires_ecx_pic_helper,
  92. { generate pic helper which loads eip in ebx (for non leave procedures) }
  93. requires_ebx_pic_helper : boolean;
  94. mainfilepos : tfileposinfo;
  95. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  96. crc,
  97. interface_crc : cardinal;
  98. flags : cardinal; { the PPU flags }
  99. islibrary : boolean; { if it is a library (win32 dll) }
  100. moduleid : longint;
  101. unitmap : punitmap; { mapping of all used units }
  102. unitmapsize : longint; { number of units in the map }
  103. derefmap : pderefmap; { mapping of all units needed for deref }
  104. derefmapcnt : longint; { number of units in the map }
  105. derefmapsize : longint; { number of units in the map }
  106. derefdataintflen : longint;
  107. derefdata : tdynamicarray;
  108. deflist,
  109. symlist : TFPObjectList;
  110. globalsymtable, { pointer to the global symtable of this unit }
  111. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  112. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  113. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  114. scanner : TObject; { scanner object used }
  115. procinfo : TObject; { current procedure being compiled }
  116. asmdata : TObject; { Assembler data }
  117. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  118. debuginfo : TObject;
  119. loaded_from : tmodule;
  120. _exports : tlinkedlist;
  121. dllscannerinputlist : TFPHashList;
  122. resourcefiles : TCmdStrList;
  123. linkunitofiles,
  124. linkunitstaticlibs,
  125. linkunitsharedlibs,
  126. linkotherofiles, { objects,libs loaded from the source }
  127. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  128. linkotherstaticlibs,
  129. linkotherframeworks : tlinkcontainer;
  130. mainname : pshortstring; { alternate name for "main" procedure }
  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. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  176. used to delete them after linking }
  177. procedure set_current_module(p:tmodule);
  178. function get_module(moduleindex : longint) : tmodule;
  179. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  180. procedure addloadedunit(hp:tmodule);
  181. function find_module_from_symtable(st:tsymtable):tmodule;
  182. implementation
  183. uses
  184. SysUtils,
  185. GlobType,
  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. mainname:=nil;
  412. FImportLibraryList:=TFPHashObjectList.Create(true);
  413. crc:=0;
  414. interface_crc:=0;
  415. flags:=0;
  416. scanner:=nil;
  417. unitmap:=nil;
  418. unitmapsize:=0;
  419. derefmap:=nil;
  420. derefmapsize:=0;
  421. derefmapcnt:=0;
  422. derefdata:=TDynamicArray.Create(1024);
  423. derefdataintflen:=0;
  424. deflist:=TFPObjectList.Create(false);
  425. symlist:=TFPObjectList.Create(false);
  426. globalsymtable:=nil;
  427. localsymtable:=nil;
  428. globalmacrosymtable:=nil;
  429. localmacrosymtable:=nil;
  430. loaded_from:=LoadedFrom;
  431. do_reload:=false;
  432. do_compile:=false;
  433. sources_avail:=true;
  434. mainfilepos.line:=0;
  435. mainfilepos.column:=0;
  436. mainfilepos.fileindex:=0;
  437. recompile_reason:=rr_unknown;
  438. in_interface:=true;
  439. in_global:=true;
  440. is_unit:=_is_unit;
  441. islibrary:=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. used_units.free;
  494. dependent_units.free;
  495. resourcefiles.Free;
  496. linkunitofiles.Free;
  497. linkunitstaticlibs.Free;
  498. linkunitsharedlibs.Free;
  499. linkotherofiles.Free;
  500. linkotherstaticlibs.Free;
  501. linkothersharedlibs.Free;
  502. linkotherframeworks.Free;
  503. stringdispose(mainname);
  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. DoneDebugInfo(self);
  622. InitDebugInfo(self);
  623. _exports.free;
  624. _exports:=tlinkedlist.create;
  625. dllscannerinputlist.free;
  626. dllscannerinputlist:=TFPHashList.create;
  627. used_units.free;
  628. used_units:=TLinkedList.Create;
  629. dependent_units.free;
  630. dependent_units:=TLinkedList.Create;
  631. resourcefiles.Free;
  632. resourcefiles:=TCmdStrList.Create;
  633. linkunitofiles.Free;
  634. linkunitofiles:=TLinkContainer.Create;
  635. linkunitstaticlibs.Free;
  636. linkunitstaticlibs:=TLinkContainer.Create;
  637. linkunitsharedlibs.Free;
  638. linkunitsharedlibs:=TLinkContainer.Create;
  639. linkotherofiles.Free;
  640. linkotherofiles:=TLinkContainer.Create;
  641. linkotherstaticlibs.Free;
  642. linkotherstaticlibs:=TLinkContainer.Create;
  643. linkothersharedlibs.Free;
  644. linkothersharedlibs:=TLinkContainer.Create;
  645. linkotherframeworks.Free;
  646. linkotherframeworks:=TLinkContainer.Create;
  647. stringdispose(mainname);
  648. FImportLibraryList.Free;
  649. FImportLibraryList:=TFPHashObjectList.Create;
  650. do_compile:=false;
  651. do_reload:=false;
  652. interface_compiled:=false;
  653. in_interface:=true;
  654. in_global:=true;
  655. mode_switch_allowed:=true;
  656. is_dbginfo_written:=false;
  657. is_reset:=false;
  658. crc:=0;
  659. interface_crc:=0;
  660. flags:=0;
  661. mainfilepos.line:=0;
  662. mainfilepos.column:=0;
  663. mainfilepos.fileindex:=0;
  664. recompile_reason:=rr_unknown;
  665. {
  666. The following fields should not
  667. be reset:
  668. mainsource
  669. state
  670. loaded_from
  671. sources_avail
  672. }
  673. end;
  674. procedure tmodule.adddependency(callermodule:tmodule);
  675. begin
  676. { This is not needed for programs }
  677. if not callermodule.is_unit then
  678. exit;
  679. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  680. dependent_units.concat(tdependent_unit.create(callermodule));
  681. end;
  682. procedure tmodule.flagdependent(callermodule:tmodule);
  683. var
  684. pm : tdependent_unit;
  685. begin
  686. { flag all units that depend on this unit for reloading }
  687. pm:=tdependent_unit(current_module.dependent_units.first);
  688. while assigned(pm) do
  689. begin
  690. { We do not have to reload the unit that wants to load
  691. this unit, unless this unit is already compiled during
  692. the loading }
  693. if (pm.u=callermodule) and
  694. (pm.u.state<>ms_compiled) then
  695. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  696. else
  697. if pm.u.state=ms_second_compile then
  698. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  699. else
  700. begin
  701. pm.u.do_reload:=true;
  702. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  703. end;
  704. pm:=tdependent_unit(pm.next);
  705. end;
  706. end;
  707. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  708. var
  709. pu : tused_unit;
  710. begin
  711. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  712. used_units.concat(pu);
  713. addusedunit:=pu;
  714. end;
  715. procedure tmodule.updatemaps;
  716. var
  717. oldmapsize : longint;
  718. hp : tmodule;
  719. i : longint;
  720. begin
  721. { Extend unitmap }
  722. oldmapsize:=unitmapsize;
  723. unitmapsize:=loaded_units.count;
  724. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  725. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  726. { Extend Derefmap }
  727. oldmapsize:=derefmapsize;
  728. derefmapsize:=loaded_units.count;
  729. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  730. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  731. { Add all units to unitmap }
  732. hp:=tmodule(loaded_units.first);
  733. i:=0;
  734. while assigned(hp) do
  735. begin
  736. if hp.moduleid>=unitmapsize then
  737. internalerror(200501151);
  738. { Verify old entries }
  739. if (i<oldmapsize) then
  740. begin
  741. if (hp.moduleid<>i) or
  742. (unitmap[hp.moduleid].u<>hp) then
  743. internalerror(200501156);
  744. end
  745. else
  746. begin
  747. unitmap[hp.moduleid].u:=hp;
  748. unitmap[hp.moduleid].derefidx:=-1;
  749. end;
  750. inc(i);
  751. hp:=tmodule(hp.next);
  752. end;
  753. end;
  754. function tmodule.derefidx_unit(id:longint):longint;
  755. begin
  756. if id>=unitmapsize then
  757. internalerror(2005011511);
  758. if unitmap[id].derefidx=-1 then
  759. begin
  760. unitmap[id].derefidx:=derefmapcnt;
  761. inc(derefmapcnt);
  762. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  763. end;
  764. if unitmap[id].derefidx>=derefmapsize then
  765. internalerror(2005011514);
  766. result:=unitmap[id].derefidx;
  767. end;
  768. function tmodule.resolve_unit(id:longint):tmodule;
  769. var
  770. hp : tmodule;
  771. begin
  772. if id>=derefmapsize then
  773. internalerror(200306231);
  774. result:=derefmap[id].u;
  775. if not assigned(result) then
  776. begin
  777. if not assigned(derefmap[id].modulename) or
  778. (derefmap[id].modulename^='') then
  779. internalerror(200501159);
  780. hp:=tmodule(loaded_units.first);
  781. while assigned(hp) do
  782. begin
  783. { only check for units. The main program is also
  784. as a unit in the loaded_units list. We simply need
  785. to ignore this entry (PFV) }
  786. if hp.is_unit and
  787. (hp.modulename^=derefmap[id].modulename^) then
  788. break;
  789. hp:=tmodule(hp.next);
  790. end;
  791. if not assigned(hp) then
  792. internalerror(2005011510);
  793. derefmap[id].u:=hp;
  794. result:=hp;
  795. end;
  796. end;
  797. procedure tmodule.allunitsused;
  798. var
  799. pu : tused_unit;
  800. begin
  801. pu:=tused_unit(used_units.first);
  802. while assigned(pu) do
  803. begin
  804. if assigned(pu.u.globalsymtable) then
  805. begin
  806. if unitmap[pu.u.moduleid].u<>pu.u then
  807. internalerror(200501157);
  808. { Give a note when the unit is not referenced, skip
  809. this is for units with an initialization/finalization }
  810. if (unitmap[pu.u.moduleid].refs=0) and
  811. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  812. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  813. end;
  814. pu:=tused_unit(pu.next);
  815. end;
  816. end;
  817. procedure tmodule.setmodulename(const s:string);
  818. begin
  819. stringdispose(modulename);
  820. stringdispose(realmodulename);
  821. modulename:=stringdup(upper(s));
  822. realmodulename:=stringdup(s);
  823. { also update asmlibrary names }
  824. current_asmdata.name:=modulename^;
  825. current_asmdata.realname:=realmodulename^;
  826. end;
  827. procedure TModule.AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  828. var
  829. ImportLibrary : TImportLibrary;
  830. ImportSymbol : TFPHashObject;
  831. begin
  832. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  833. if not assigned(ImportLibrary) then
  834. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  835. ImportSymbol:=TFPHashObject(ImportLibrary.ImportSymbolList.Find(symname));
  836. if not assigned(ImportSymbol) then
  837. begin
  838. if not ImportByOrdinalOnly then
  839. { negative ordinal number indicates import by name with ordinal number as hint }
  840. OrdNr:=-OrdNr;
  841. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,symname,OrdNr,isvar);
  842. end;
  843. end;
  844. initialization
  845. {$ifdef MEMDEBUG}
  846. memsymtable:=TMemDebug.create('Symtables');
  847. memsymtable.stop;
  848. {$endif MEMDEBUG}
  849. finalization
  850. {$ifdef MEMDEBUG}
  851. memsymtable.free;
  852. {$endif MEMDEBUG}
  853. end.