2
0

fmodule.pas 32 KB

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