fmodule.pas 30 KB

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