fmodule.pas 30 KB

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