fmodule.pas 32 KB

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