fmodule.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. current_genericdef:=nil;
  516. current_specializedef:=nil;
  517. end;
  518. { release procinfo tree }
  519. while assigned(procinfo) do
  520. begin
  521. hpi:=tprocinfo(procinfo).parent;
  522. tprocinfo(procinfo).free;
  523. procinfo:=hpi;
  524. end;
  525. end;
  526. DoneDebugInfo(self);
  527. used_units.free;
  528. dependent_units.free;
  529. resourcefiles.Free;
  530. linkunitofiles.Free;
  531. linkunitstaticlibs.Free;
  532. linkunitsharedlibs.Free;
  533. linkotherofiles.Free;
  534. linkotherstaticlibs.Free;
  535. linkothersharedlibs.Free;
  536. linkotherframeworks.Free;
  537. stringdispose(mainname);
  538. FImportLibraryList.Free;
  539. stringdispose(objfilename);
  540. stringdispose(asmfilename);
  541. stringdispose(ppufilename);
  542. stringdispose(importlibfilename);
  543. stringdispose(staticlibfilename);
  544. stringdispose(sharedlibfilename);
  545. stringdispose(exefilename);
  546. stringdispose(outputpath);
  547. stringdispose(path);
  548. stringdispose(realmodulename);
  549. stringdispose(mainsource);
  550. stringdispose(asmprefix);
  551. stringdispose(deprecatedmsg);
  552. localunitsearchpath.Free;
  553. localobjectsearchpath.free;
  554. localincludesearchpath.free;
  555. locallibrarysearchpath.free;
  556. localframeworksearchpath.free;
  557. {$ifdef MEMDEBUG}
  558. memsymtable.start;
  559. {$endif}
  560. derefdata.free;
  561. deflist.free;
  562. symlist.free;
  563. wpoinfo.free;
  564. checkforwarddefs.free;
  565. globalsymtable.free;
  566. localsymtable.free;
  567. globalmacrosymtable.free;
  568. localmacrosymtable.free;
  569. {$ifdef MEMDEBUG}
  570. memsymtable.stop;
  571. {$endif}
  572. stringdispose(modulename);
  573. inherited Destroy;
  574. end;
  575. procedure tmodule.reset;
  576. var
  577. hpi : tprocinfo;
  578. i : longint;
  579. begin
  580. if assigned(scanner) then
  581. begin
  582. { also update current_scanner if it was pointing
  583. to this module }
  584. if current_scanner=tscannerfile(scanner) then
  585. current_scanner:=nil;
  586. tscannerfile(scanner).free;
  587. scanner:=nil;
  588. end;
  589. if assigned(procinfo) then
  590. begin
  591. if current_procinfo=tprocinfo(procinfo) then
  592. begin
  593. current_procinfo:=nil;
  594. current_objectdef:=nil;
  595. current_genericdef:=nil;
  596. current_specializedef:=nil;
  597. end;
  598. { release procinfo tree }
  599. while assigned(procinfo) do
  600. begin
  601. hpi:=tprocinfo(procinfo).parent;
  602. tprocinfo(procinfo).free;
  603. procinfo:=hpi;
  604. end;
  605. end;
  606. if assigned(asmdata) then
  607. begin
  608. if current_asmdata=TAsmData(asmdata) then
  609. current_asmdata:=nil;
  610. asmdata.free;
  611. asmdata:=nil;
  612. end;
  613. DoneDebugInfo(self);
  614. globalsymtable.free;
  615. globalsymtable:=nil;
  616. localsymtable.free;
  617. localsymtable:=nil;
  618. globalmacrosymtable.free;
  619. globalmacrosymtable:=nil;
  620. localmacrosymtable.free;
  621. localmacrosymtable:=nil;
  622. deflist.free;
  623. deflist:=TFPObjectList.Create(false);
  624. symlist.free;
  625. symlist:=TFPObjectList.Create(false);
  626. wpoinfo.free;
  627. wpoinfo:=nil;
  628. checkforwarddefs.free;
  629. checkforwarddefs:=TFPObjectList.Create(false);
  630. derefdata.free;
  631. derefdata:=TDynamicArray.Create(1024);
  632. if assigned(unitmap) then
  633. begin
  634. freemem(unitmap);
  635. unitmap:=nil;
  636. end;
  637. if assigned(derefmap) then
  638. begin
  639. for i:=0 to derefmapcnt-1 do
  640. stringdispose(derefmap[i].modulename);
  641. freemem(derefmap);
  642. derefmap:=nil;
  643. end;
  644. unitmapsize:=0;
  645. derefmapsize:=0;
  646. derefmapcnt:=0;
  647. derefdataintflen:=0;
  648. sourcefiles.free;
  649. sourcefiles:=tinputfilemanager.create;
  650. asmdata:=TAsmData.create(realmodulename^);
  651. InitDebugInfo(self);
  652. _exports.free;
  653. _exports:=tlinkedlist.create;
  654. dllscannerinputlist.free;
  655. dllscannerinputlist:=TFPHashList.create;
  656. used_units.free;
  657. used_units:=TLinkedList.Create;
  658. dependent_units.free;
  659. dependent_units:=TLinkedList.Create;
  660. resourcefiles.Free;
  661. resourcefiles:=TCmdStrList.Create;
  662. linkunitofiles.Free;
  663. linkunitofiles:=TLinkContainer.Create;
  664. linkunitstaticlibs.Free;
  665. linkunitstaticlibs:=TLinkContainer.Create;
  666. linkunitsharedlibs.Free;
  667. linkunitsharedlibs:=TLinkContainer.Create;
  668. linkotherofiles.Free;
  669. linkotherofiles:=TLinkContainer.Create;
  670. linkotherstaticlibs.Free;
  671. linkotherstaticlibs:=TLinkContainer.Create;
  672. linkothersharedlibs.Free;
  673. linkothersharedlibs:=TLinkContainer.Create;
  674. linkotherframeworks.Free;
  675. linkotherframeworks:=TLinkContainer.Create;
  676. stringdispose(mainname);
  677. FImportLibraryList.Free;
  678. FImportLibraryList:=TFPHashObjectList.Create;
  679. do_compile:=false;
  680. do_reload:=false;
  681. interface_compiled:=false;
  682. in_interface:=true;
  683. in_global:=true;
  684. mode_switch_allowed:=true;
  685. stringdispose(deprecatedmsg);
  686. moduleoptions:=[];
  687. is_dbginfo_written:=false;
  688. crc:=0;
  689. interface_crc:=0;
  690. indirect_crc:=0;
  691. flags:=0;
  692. mainfilepos.line:=0;
  693. mainfilepos.column:=0;
  694. mainfilepos.fileindex:=0;
  695. recompile_reason:=rr_unknown;
  696. {
  697. The following fields should not
  698. be reset:
  699. mainsource
  700. state
  701. loaded_from
  702. sources_avail
  703. }
  704. end;
  705. procedure tmodule.adddependency(callermodule:tmodule);
  706. begin
  707. { This is not needed for programs }
  708. if not callermodule.is_unit then
  709. exit;
  710. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  711. dependent_units.concat(tdependent_unit.create(callermodule));
  712. end;
  713. procedure tmodule.flagdependent(callermodule:tmodule);
  714. var
  715. pm : tdependent_unit;
  716. begin
  717. { flag all units that depend on this unit for reloading }
  718. pm:=tdependent_unit(current_module.dependent_units.first);
  719. while assigned(pm) do
  720. begin
  721. { We do not have to reload the unit that wants to load
  722. this unit, unless this unit is already compiled during
  723. the loading }
  724. if (pm.u=callermodule) and
  725. (pm.u.state<>ms_compiled) then
  726. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  727. else
  728. if pm.u.state=ms_second_compile then
  729. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  730. else
  731. begin
  732. pm.u.do_reload:=true;
  733. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  734. end;
  735. pm:=tdependent_unit(pm.next);
  736. end;
  737. end;
  738. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  739. var
  740. pu : tused_unit;
  741. begin
  742. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  743. used_units.concat(pu);
  744. addusedunit:=pu;
  745. end;
  746. procedure tmodule.updatemaps;
  747. var
  748. oldmapsize : longint;
  749. hp : tmodule;
  750. i : longint;
  751. begin
  752. { Extend unitmap }
  753. oldmapsize:=unitmapsize;
  754. unitmapsize:=loaded_units.count;
  755. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  756. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  757. { Extend Derefmap }
  758. oldmapsize:=derefmapsize;
  759. derefmapsize:=loaded_units.count;
  760. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  761. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  762. { Add all units to unitmap }
  763. hp:=tmodule(loaded_units.first);
  764. i:=0;
  765. while assigned(hp) do
  766. begin
  767. if hp.moduleid>=unitmapsize then
  768. internalerror(200501151);
  769. { Verify old entries }
  770. if (i<oldmapsize) then
  771. begin
  772. if (hp.moduleid<>i) or
  773. (unitmap[hp.moduleid].u<>hp) then
  774. internalerror(200501156);
  775. end
  776. else
  777. begin
  778. unitmap[hp.moduleid].u:=hp;
  779. unitmap[hp.moduleid].derefidx:=-1;
  780. end;
  781. inc(i);
  782. hp:=tmodule(hp.next);
  783. end;
  784. end;
  785. procedure tmodule.check_hints;
  786. begin
  787. if mo_hint_deprecated in moduleoptions then
  788. if (mo_has_deprecated_msg in moduleoptions) and (deprecatedmsg <> nil) then
  789. Message2(sym_w_deprecated_unit_with_msg,realmodulename^,deprecatedmsg^)
  790. else
  791. Message1(sym_w_deprecated_unit,realmodulename^);
  792. if mo_hint_experimental in moduleoptions then
  793. Message1(sym_w_experimental_unit,realmodulename^);
  794. if mo_hint_platform in moduleoptions then
  795. Message1(sym_w_non_portable_unit,realmodulename^);
  796. if mo_hint_library in moduleoptions then
  797. Message1(sym_w_library_unit,realmodulename^);
  798. if mo_hint_unimplemented in moduleoptions then
  799. Message1(sym_w_non_implemented_unit,realmodulename^);
  800. end;
  801. function tmodule.derefidx_unit(id:longint):longint;
  802. begin
  803. if id>=unitmapsize then
  804. internalerror(2005011511);
  805. if unitmap[id].derefidx=-1 then
  806. begin
  807. unitmap[id].derefidx:=derefmapcnt;
  808. inc(derefmapcnt);
  809. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  810. end;
  811. if unitmap[id].derefidx>=derefmapsize then
  812. internalerror(2005011514);
  813. result:=unitmap[id].derefidx;
  814. end;
  815. function tmodule.resolve_unit(id:longint):tmodule;
  816. var
  817. hp : tmodule;
  818. begin
  819. if id>=derefmapsize then
  820. internalerror(200306231);
  821. result:=derefmap[id].u;
  822. if not assigned(result) then
  823. begin
  824. if not assigned(derefmap[id].modulename) or
  825. (derefmap[id].modulename^='') then
  826. internalerror(200501159);
  827. hp:=tmodule(loaded_units.first);
  828. while assigned(hp) do
  829. begin
  830. { only check for units. The main program is also
  831. as a unit in the loaded_units list. We simply need
  832. to ignore this entry (PFV) }
  833. if hp.is_unit and
  834. (hp.modulename^=derefmap[id].modulename^) then
  835. break;
  836. hp:=tmodule(hp.next);
  837. end;
  838. if not assigned(hp) then
  839. internalerror(2005011510);
  840. derefmap[id].u:=hp;
  841. result:=hp;
  842. end;
  843. end;
  844. procedure tmodule.allunitsused;
  845. var
  846. pu : tused_unit;
  847. begin
  848. pu:=tused_unit(used_units.first);
  849. while assigned(pu) do
  850. begin
  851. if assigned(pu.u.globalsymtable) then
  852. begin
  853. if unitmap[pu.u.moduleid].u<>pu.u then
  854. internalerror(200501157);
  855. { Give a note when the unit is not referenced, skip
  856. this is for units with an initialization/finalization }
  857. if (unitmap[pu.u.moduleid].refs=0) and
  858. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  859. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  860. end;
  861. pu:=tused_unit(pu.next);
  862. end;
  863. end;
  864. procedure tmodule.setmodulename(const s:string);
  865. begin
  866. stringdispose(modulename);
  867. stringdispose(realmodulename);
  868. modulename:=stringdup(upper(s));
  869. realmodulename:=stringdup(s);
  870. { also update asmlibrary names }
  871. current_asmdata.name:=modulename^;
  872. current_asmdata.realname:=realmodulename^;
  873. end;
  874. procedure TModule.AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  875. var
  876. ImportLibrary : TImportLibrary;
  877. ImportSymbol : TFPHashObject;
  878. begin
  879. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  880. if not assigned(ImportLibrary) then
  881. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  882. ImportSymbol:=TFPHashObject(ImportLibrary.ImportSymbolList.Find(symname));
  883. if not assigned(ImportSymbol) then
  884. begin
  885. if not ImportByOrdinalOnly then
  886. { negative ordinal number indicates import by name with ordinal number as hint }
  887. OrdNr:=-OrdNr;
  888. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,symname,OrdNr,isvar);
  889. end;
  890. end;
  891. initialization
  892. {$ifdef MEMDEBUG}
  893. memsymtable:=TMemDebug.create('Symtables');
  894. memsymtable.stop;
  895. {$endif MEMDEBUG}
  896. finalization
  897. {$ifdef MEMDEBUG}
  898. memsymtable.free;
  899. {$endif MEMDEBUG}
  900. end.