2
0

fmodule.pas 29 KB

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