fmodule.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fmodule;
  18. {$i fpcdefs.inc}
  19. {$ifdef go32v2}
  20. {$define shortasmprefix}
  21. {$endif}
  22. {$ifdef watcom}
  23. {$define shortasmprefix}
  24. {$endif}
  25. {$ifdef tos}
  26. {$define shortasmprefix}
  27. {$endif}
  28. {$ifdef OS2}
  29. { Allthough OS/2 supports long filenames I play it safe and
  30. use 8.3 filenames, because this allows the compiler to run
  31. on a FAT partition. (DM) }
  32. {$define shortasmprefix}
  33. {$endif}
  34. interface
  35. uses
  36. cutils,cclasses,cfileutl,
  37. globtype,finput,ogbase,
  38. symbase,symsym,aasmbase,aasmtai,aasmdata;
  39. const
  40. UNSPECIFIED_LIBRARY_NAME = '<none>';
  41. type
  42. trecompile_reason = (rr_unknown,
  43. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  44. );
  45. tlinkcontaineritem=class(tlinkedlistitem)
  46. public
  47. data : pshortstring;
  48. needlink : cardinal;
  49. constructor Create(const s:string;m:cardinal);
  50. destructor Destroy;override;
  51. end;
  52. tlinkcontainer=class(tlinkedlist)
  53. procedure add(const s : string;m:cardinal);
  54. function get(var m:cardinal) : string;
  55. function getusemask(mask:cardinal) : string;
  56. function find(const s:string):boolean;
  57. end;
  58. tmodule = class;
  59. tused_unit = class;
  60. tunitmaprec = record
  61. u : tmodule;
  62. { number of references }
  63. refs : longint;
  64. { index in the derefmap }
  65. derefidx : longint;
  66. end;
  67. punitmap = ^tunitmaprec;
  68. tderefmaprec = record
  69. u : tmodule;
  70. { modulename, used during ppu load }
  71. modulename : pshortstring;
  72. end;
  73. pderefmap = ^tderefmaprec;
  74. tmodule = class(tmodulebase)
  75. private
  76. FImportLibraryList : TFPHashObjectList;
  77. public
  78. do_reload, { force reloading of the unit }
  79. do_compile, { need to compile the sources }
  80. sources_avail, { if all sources are reachable }
  81. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  82. is_dbginfo_written,
  83. is_reset,
  84. is_unit,
  85. in_interface, { processing the implementation part? }
  86. { allow global settings }
  87. in_global : boolean;
  88. { Whether a mode switch is still allowed at this point in the parsing.}
  89. mode_switch_allowed,
  90. { generate pic helper which loads eip in ecx (for leave procedures) }
  91. requires_ecx_pic_helper,
  92. { generate pic helper which loads eip in ebx (for non leave procedures) }
  93. requires_ebx_pic_helper : boolean;
  94. mainfilepos : tfileposinfo;
  95. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  96. crc,
  97. interface_crc : cardinal;
  98. flags : cardinal; { the PPU flags }
  99. islibrary : boolean; { if it is a library (win32 dll) }
  100. IsPackage : boolean;
  101. moduleid : longint;
  102. unitmap : punitmap; { mapping of all used units }
  103. unitmapsize : longint; { number of units in the map }
  104. derefmap : pderefmap; { mapping of all units needed for deref }
  105. derefmapcnt : longint; { number of units in the map }
  106. derefmapsize : longint; { number of units in the map }
  107. derefdataintflen : longint;
  108. derefdata : tdynamicarray;
  109. deflist,
  110. symlist : TFPObjectList;
  111. globalsymtable, { pointer to the global symtable of this unit }
  112. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  113. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  114. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  115. scanner : TObject; { scanner object used }
  116. procinfo : TObject; { current procedure being compiled }
  117. asmdata : TObject; { Assembler data }
  118. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  119. debuginfo : TObject;
  120. loaded_from : tmodule;
  121. _exports : tlinkedlist;
  122. dllscannerinputlist : TFPHashList;
  123. resourcefiles : TCmdStrList;
  124. linkunitofiles,
  125. linkunitstaticlibs,
  126. linkunitsharedlibs,
  127. linkotherofiles, { objects,libs loaded from the source }
  128. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  129. linkotherstaticlibs,
  130. linkotherframeworks : tlinkcontainer;
  131. mainname : pshortstring; { alternate name for "main" procedure }
  132. used_units : tlinkedlist;
  133. dependent_units : tlinkedlist;
  134. localunitsearchpath, { local searchpaths }
  135. localobjectsearchpath,
  136. localincludesearchpath,
  137. locallibrarysearchpath,
  138. localframeworksearchpath : TSearchPathList;
  139. {create creates a new module which name is stored in 's'. LoadedFrom
  140. points to the module calling it. It is nil for the first compiled
  141. module. This allow inheritence of all path lists. MUST pay attention
  142. to that when creating link.res!!!!(mazen)}
  143. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  144. destructor destroy;override;
  145. procedure reset;virtual;
  146. procedure adddependency(callermodule:tmodule);
  147. procedure flagdependent(callermodule:tmodule);
  148. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  149. procedure updatemaps;
  150. function derefidx_unit(id:longint):longint;
  151. function resolve_unit(id:longint):tmodule;
  152. procedure allunitsused;
  153. procedure setmodulename(const s:string);
  154. procedure AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  155. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  156. end;
  157. tused_unit = class(tlinkedlistitem)
  158. checksum,
  159. interface_checksum : cardinal;
  160. in_uses,
  161. in_interface : boolean;
  162. u : tmodule;
  163. unitsym : tunitsym;
  164. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  165. end;
  166. tdependent_unit = class(tlinkedlistitem)
  167. u : tmodule;
  168. constructor create(_u : tmodule);
  169. end;
  170. var
  171. main_module : tmodule; { Main module of the program }
  172. current_module : tmodule; { Current module which is compiled or loaded }
  173. compiled_module : tmodule; { Current module which is compiled }
  174. usedunits : tlinkedlist; { Used units for this program }
  175. loaded_units : tlinkedlist; { All loaded units }
  176. 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. 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. ispackage:=false;
  443. is_dbginfo_written:=false;
  444. is_reset:=false;
  445. mode_switch_allowed:= true;
  446. _exports:=TLinkedList.Create;
  447. dllscannerinputlist:=TFPHashList.Create;
  448. asmdata:=TAsmData.create(realmodulename^);
  449. InitDebugInfo(self);
  450. end;
  451. destructor tmodule.Destroy;
  452. var
  453. i : longint;
  454. hpi : tprocinfo;
  455. begin
  456. if assigned(unitmap) then
  457. freemem(unitmap);
  458. if assigned(derefmap) then
  459. begin
  460. for i:=0 to derefmapcnt-1 do
  461. stringdispose(derefmap[i].modulename);
  462. freemem(derefmap);
  463. end;
  464. if assigned(_exports) then
  465. _exports.free;
  466. if assigned(dllscannerinputlist) then
  467. dllscannerinputlist.free;
  468. if assigned(scanner) then
  469. begin
  470. { also update current_scanner if it was pointing
  471. to this module }
  472. if current_scanner=tscannerfile(scanner) then
  473. current_scanner:=nil;
  474. tscannerfile(scanner).free;
  475. end;
  476. if assigned(asmdata) then
  477. begin
  478. if current_asmdata=asmdata then
  479. current_asmdata:=nil;
  480. asmdata.free;
  481. end;
  482. if assigned(procinfo) then
  483. begin
  484. if current_procinfo=tprocinfo(procinfo) then
  485. current_procinfo:=nil;
  486. { release procinfo tree }
  487. while assigned(procinfo) do
  488. begin
  489. hpi:=tprocinfo(procinfo).parent;
  490. tprocinfo(procinfo).free;
  491. procinfo:=hpi;
  492. end;
  493. end;
  494. DoneDebugInfo(self);
  495. used_units.free;
  496. dependent_units.free;
  497. resourcefiles.Free;
  498. linkunitofiles.Free;
  499. linkunitstaticlibs.Free;
  500. linkunitsharedlibs.Free;
  501. linkotherofiles.Free;
  502. linkotherstaticlibs.Free;
  503. linkothersharedlibs.Free;
  504. linkotherframeworks.Free;
  505. stringdispose(mainname);
  506. FImportLibraryList.Free;
  507. stringdispose(objfilename);
  508. stringdispose(asmfilename);
  509. stringdispose(ppufilename);
  510. stringdispose(importlibfilename);
  511. stringdispose(staticlibfilename);
  512. stringdispose(sharedlibfilename);
  513. stringdispose(exefilename);
  514. stringdispose(outputpath);
  515. stringdispose(path);
  516. stringdispose(realmodulename);
  517. stringdispose(mainsource);
  518. stringdispose(asmprefix);
  519. localunitsearchpath.Free;
  520. localobjectsearchpath.free;
  521. localincludesearchpath.free;
  522. locallibrarysearchpath.free;
  523. localframeworksearchpath.free;
  524. {$ifdef MEMDEBUG}
  525. memsymtable.start;
  526. {$endif}
  527. derefdata.free;
  528. deflist.free;
  529. symlist.free;
  530. if assigned(globalsymtable) then
  531. globalsymtable.free;
  532. if assigned(localsymtable) then
  533. localsymtable.free;
  534. if assigned(globalmacrosymtable) then
  535. globalmacrosymtable.free;
  536. if assigned(localmacrosymtable) then
  537. localmacrosymtable.free;
  538. {$ifdef MEMDEBUG}
  539. memsymtable.stop;
  540. {$endif}
  541. stringdispose(modulename);
  542. inherited Destroy;
  543. end;
  544. procedure tmodule.reset;
  545. var
  546. hpi : tprocinfo;
  547. i : longint;
  548. begin
  549. if assigned(scanner) then
  550. begin
  551. { also update current_scanner if it was pointing
  552. to this module }
  553. if current_scanner=tscannerfile(scanner) then
  554. current_scanner:=nil;
  555. tscannerfile(scanner).free;
  556. scanner:=nil;
  557. end;
  558. if assigned(procinfo) then
  559. begin
  560. if current_procinfo=tprocinfo(procinfo) then
  561. current_procinfo:=nil;
  562. { release procinfo tree }
  563. while assigned(procinfo) do
  564. begin
  565. hpi:=tprocinfo(procinfo).parent;
  566. tprocinfo(procinfo).free;
  567. procinfo:=hpi;
  568. end;
  569. end;
  570. if assigned(asmdata) then
  571. begin
  572. if current_asmdata=TAsmData(asmdata) then
  573. current_asmdata:=nil;
  574. asmdata.free;
  575. asmdata:=nil;
  576. end;
  577. DoneDebugInfo(self);
  578. if assigned(globalsymtable) then
  579. begin
  580. globalsymtable.free;
  581. globalsymtable:=nil;
  582. end;
  583. if assigned(localsymtable) then
  584. begin
  585. localsymtable.free;
  586. localsymtable:=nil;
  587. end;
  588. if assigned(globalmacrosymtable) then
  589. begin
  590. globalmacrosymtable.free;
  591. globalmacrosymtable:=nil;
  592. end;
  593. if assigned(localmacrosymtable) then
  594. begin
  595. localmacrosymtable.free;
  596. localmacrosymtable:=nil;
  597. end;
  598. deflist.free;
  599. deflist:=TFPObjectList.Create(false);
  600. symlist.free;
  601. symlist:=TFPObjectList.Create(false);
  602. derefdata.free;
  603. derefdata:=TDynamicArray.Create(1024);
  604. if assigned(unitmap) then
  605. begin
  606. freemem(unitmap);
  607. unitmap:=nil;
  608. end;
  609. if assigned(derefmap) then
  610. begin
  611. for i:=0 to derefmapcnt-1 do
  612. stringdispose(derefmap[i].modulename);
  613. freemem(derefmap);
  614. derefmap:=nil;
  615. end;
  616. unitmapsize:=0;
  617. derefmapsize:=0;
  618. derefmapcnt:=0;
  619. derefdataintflen:=0;
  620. sourcefiles.free;
  621. sourcefiles:=tinputfilemanager.create;
  622. asmdata:=TAsmData.create(realmodulename^);
  623. InitDebugInfo(self);
  624. _exports.free;
  625. _exports:=tlinkedlist.create;
  626. dllscannerinputlist.free;
  627. dllscannerinputlist:=TFPHashList.create;
  628. used_units.free;
  629. used_units:=TLinkedList.Create;
  630. dependent_units.free;
  631. dependent_units:=TLinkedList.Create;
  632. resourcefiles.Free;
  633. resourcefiles:=TCmdStrList.Create;
  634. linkunitofiles.Free;
  635. linkunitofiles:=TLinkContainer.Create;
  636. linkunitstaticlibs.Free;
  637. linkunitstaticlibs:=TLinkContainer.Create;
  638. linkunitsharedlibs.Free;
  639. linkunitsharedlibs:=TLinkContainer.Create;
  640. linkotherofiles.Free;
  641. linkotherofiles:=TLinkContainer.Create;
  642. linkotherstaticlibs.Free;
  643. linkotherstaticlibs:=TLinkContainer.Create;
  644. linkothersharedlibs.Free;
  645. linkothersharedlibs:=TLinkContainer.Create;
  646. linkotherframeworks.Free;
  647. linkotherframeworks:=TLinkContainer.Create;
  648. stringdispose(mainname);
  649. FImportLibraryList.Free;
  650. FImportLibraryList:=TFPHashObjectList.Create;
  651. do_compile:=false;
  652. do_reload:=false;
  653. interface_compiled:=false;
  654. in_interface:=true;
  655. in_global:=true;
  656. mode_switch_allowed:=true;
  657. is_dbginfo_written:=false;
  658. is_reset:=false;
  659. crc:=0;
  660. interface_crc:=0;
  661. flags:=0;
  662. mainfilepos.line:=0;
  663. mainfilepos.column:=0;
  664. mainfilepos.fileindex:=0;
  665. recompile_reason:=rr_unknown;
  666. {
  667. The following fields should not
  668. be reset:
  669. mainsource
  670. state
  671. loaded_from
  672. sources_avail
  673. }
  674. end;
  675. procedure tmodule.adddependency(callermodule:tmodule);
  676. begin
  677. { This is not needed for programs }
  678. if not callermodule.is_unit then
  679. exit;
  680. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  681. dependent_units.concat(tdependent_unit.create(callermodule));
  682. end;
  683. procedure tmodule.flagdependent(callermodule:tmodule);
  684. var
  685. pm : tdependent_unit;
  686. begin
  687. { flag all units that depend on this unit for reloading }
  688. pm:=tdependent_unit(current_module.dependent_units.first);
  689. while assigned(pm) do
  690. begin
  691. { We do not have to reload the unit that wants to load
  692. this unit, unless this unit is already compiled during
  693. the loading }
  694. if (pm.u=callermodule) and
  695. (pm.u.state<>ms_compiled) then
  696. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  697. else
  698. if pm.u.state=ms_second_compile then
  699. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  700. else
  701. begin
  702. pm.u.do_reload:=true;
  703. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  704. end;
  705. pm:=tdependent_unit(pm.next);
  706. end;
  707. end;
  708. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  709. var
  710. pu : tused_unit;
  711. begin
  712. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  713. used_units.concat(pu);
  714. addusedunit:=pu;
  715. end;
  716. procedure tmodule.updatemaps;
  717. var
  718. oldmapsize : longint;
  719. hp : tmodule;
  720. i : longint;
  721. begin
  722. { Extend unitmap }
  723. oldmapsize:=unitmapsize;
  724. unitmapsize:=loaded_units.count;
  725. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  726. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  727. { Extend Derefmap }
  728. oldmapsize:=derefmapsize;
  729. derefmapsize:=loaded_units.count;
  730. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  731. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  732. { Add all units to unitmap }
  733. hp:=tmodule(loaded_units.first);
  734. i:=0;
  735. while assigned(hp) do
  736. begin
  737. if hp.moduleid>=unitmapsize then
  738. internalerror(200501151);
  739. { Verify old entries }
  740. if (i<oldmapsize) then
  741. begin
  742. if (hp.moduleid<>i) or
  743. (unitmap[hp.moduleid].u<>hp) then
  744. internalerror(200501156);
  745. end
  746. else
  747. begin
  748. unitmap[hp.moduleid].u:=hp;
  749. unitmap[hp.moduleid].derefidx:=-1;
  750. end;
  751. inc(i);
  752. hp:=tmodule(hp.next);
  753. end;
  754. end;
  755. function tmodule.derefidx_unit(id:longint):longint;
  756. begin
  757. if id>=unitmapsize then
  758. internalerror(2005011511);
  759. if unitmap[id].derefidx=-1 then
  760. begin
  761. unitmap[id].derefidx:=derefmapcnt;
  762. inc(derefmapcnt);
  763. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  764. end;
  765. if unitmap[id].derefidx>=derefmapsize then
  766. internalerror(2005011514);
  767. result:=unitmap[id].derefidx;
  768. end;
  769. function tmodule.resolve_unit(id:longint):tmodule;
  770. var
  771. hp : tmodule;
  772. begin
  773. if id>=derefmapsize then
  774. internalerror(200306231);
  775. result:=derefmap[id].u;
  776. if not assigned(result) then
  777. begin
  778. if not assigned(derefmap[id].modulename) or
  779. (derefmap[id].modulename^='') then
  780. internalerror(200501159);
  781. hp:=tmodule(loaded_units.first);
  782. while assigned(hp) do
  783. begin
  784. { only check for units. The main program is also
  785. as a unit in the loaded_units list. We simply need
  786. to ignore this entry (PFV) }
  787. if hp.is_unit and
  788. (hp.modulename^=derefmap[id].modulename^) then
  789. break;
  790. hp:=tmodule(hp.next);
  791. end;
  792. if not assigned(hp) then
  793. internalerror(2005011510);
  794. derefmap[id].u:=hp;
  795. result:=hp;
  796. end;
  797. end;
  798. procedure tmodule.allunitsused;
  799. var
  800. pu : tused_unit;
  801. begin
  802. pu:=tused_unit(used_units.first);
  803. while assigned(pu) do
  804. begin
  805. if assigned(pu.u.globalsymtable) then
  806. begin
  807. if unitmap[pu.u.moduleid].u<>pu.u then
  808. internalerror(200501157);
  809. { Give a note when the unit is not referenced, skip
  810. this is for units with an initialization/finalization }
  811. if (unitmap[pu.u.moduleid].refs=0) and
  812. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  813. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  814. end;
  815. pu:=tused_unit(pu.next);
  816. end;
  817. end;
  818. procedure tmodule.setmodulename(const s:string);
  819. begin
  820. stringdispose(modulename);
  821. stringdispose(realmodulename);
  822. modulename:=stringdup(upper(s));
  823. realmodulename:=stringdup(s);
  824. { also update asmlibrary names }
  825. current_asmdata.name:=modulename^;
  826. current_asmdata.realname:=realmodulename^;
  827. end;
  828. procedure TModule.AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  829. var
  830. ImportLibrary : TImportLibrary;
  831. ImportSymbol : TFPHashObject;
  832. begin
  833. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  834. if not assigned(ImportLibrary) then
  835. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  836. ImportSymbol:=TFPHashObject(ImportLibrary.ImportSymbolList.Find(symname));
  837. if not assigned(ImportSymbol) then
  838. begin
  839. if not ImportByOrdinalOnly then
  840. { negative ordinal number indicates import by name with ordinal number as hint }
  841. OrdNr:=-OrdNr;
  842. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,symname,OrdNr,isvar);
  843. end;
  844. end;
  845. initialization
  846. {$ifdef MEMDEBUG}
  847. memsymtable:=TMemDebug.create('Symtables');
  848. memsymtable.stop;
  849. {$endif MEMDEBUG}
  850. finalization
  851. {$ifdef MEMDEBUG}
  852. memsymtable.free;
  853. {$endif MEMDEBUG}
  854. end.