fmodule.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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.moduleid=st.moduleid) then
  226. begin
  227. result:=hp;
  228. exit;
  229. end;
  230. hp:=tmodule(hp.next);
  231. end;
  232. end;
  233. procedure set_current_module(p:tmodule);
  234. begin
  235. { save the state of the scanner }
  236. if assigned(current_scanner) then
  237. current_scanner.tempcloseinputfile;
  238. { set new module }
  239. current_module:=p;
  240. { restore previous module settings }
  241. Fillchar(current_filepos,0,sizeof(current_filepos));
  242. if assigned(current_module) then
  243. begin
  244. current_asmdata:=tasmdata(current_module.asmdata);
  245. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  246. { restore scanner and file positions }
  247. current_scanner:=tscannerfile(current_module.scanner);
  248. if assigned(current_scanner) then
  249. begin
  250. current_scanner.tempopeninputfile;
  251. current_scanner.gettokenpos;
  252. parser_current_file:=current_scanner.inputfile.name^;
  253. end
  254. else
  255. begin
  256. current_filepos.moduleindex:=current_module.unit_index;
  257. parser_current_file:='';
  258. end;
  259. end
  260. else
  261. begin
  262. current_asmdata:=nil;
  263. current_scanner:=nil;
  264. current_debuginfo:=nil;
  265. end;
  266. end;
  267. function get_module(moduleindex : longint) : tmodule;
  268. var
  269. hp : tmodule;
  270. begin
  271. result:=nil;
  272. if moduleindex=0 then
  273. exit;
  274. result:=current_module;
  275. if not(assigned(loaded_units)) then
  276. exit;
  277. hp:=tmodule(loaded_units.first);
  278. while assigned(hp) and (hp.unit_index<>moduleindex) do
  279. hp:=tmodule(hp.next);
  280. result:=hp;
  281. end;
  282. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  283. var
  284. hp : tmodule;
  285. begin
  286. hp:=get_module(moduleindex);
  287. if assigned(hp) then
  288. get_source_file:=hp.sourcefiles.get_file(fileindex)
  289. else
  290. get_source_file:=nil;
  291. end;
  292. procedure addloadedunit(hp:tmodule);
  293. begin
  294. hp.moduleid:=loaded_units.count;
  295. loaded_units.concat(hp);
  296. end;
  297. {****************************************************************************
  298. TLinkContainerItem
  299. ****************************************************************************}
  300. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  301. begin
  302. inherited Create;
  303. data:=stringdup(s);
  304. needlink:=m;
  305. end;
  306. destructor TLinkContainerItem.Destroy;
  307. begin
  308. stringdispose(data);
  309. end;
  310. {****************************************************************************
  311. TLinkContainer
  312. ****************************************************************************}
  313. procedure TLinkContainer.add(const s : string;m:cardinal);
  314. begin
  315. inherited concat(TLinkContainerItem.Create(s,m));
  316. end;
  317. function TLinkContainer.get(var m:cardinal) : string;
  318. var
  319. p : tlinkcontaineritem;
  320. begin
  321. p:=tlinkcontaineritem(inherited getfirst);
  322. if p=nil then
  323. begin
  324. get:='';
  325. m:=0;
  326. end
  327. else
  328. begin
  329. get:=p.data^;
  330. m:=p.needlink;
  331. p.free;
  332. end;
  333. end;
  334. function TLinkContainer.getusemask(mask:cardinal) : string;
  335. var
  336. p : tlinkcontaineritem;
  337. found : boolean;
  338. begin
  339. found:=false;
  340. repeat
  341. p:=tlinkcontaineritem(inherited getfirst);
  342. if p=nil then
  343. begin
  344. getusemask:='';
  345. exit;
  346. end;
  347. getusemask:=p.data^;
  348. found:=(p.needlink and mask)<>0;
  349. p.free;
  350. until found;
  351. end;
  352. function TLinkContainer.find(const s:string):boolean;
  353. var
  354. newnode : tlinkcontaineritem;
  355. begin
  356. find:=false;
  357. newnode:=tlinkcontaineritem(First);
  358. while assigned(newnode) do
  359. begin
  360. if newnode.data^=s then
  361. begin
  362. find:=true;
  363. exit;
  364. end;
  365. newnode:=tlinkcontaineritem(newnode.next);
  366. end;
  367. end;
  368. {****************************************************************************
  369. TUSED_UNIT
  370. ****************************************************************************}
  371. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  372. begin
  373. u:=_u;
  374. in_interface:=intface;
  375. in_uses:=inuses;
  376. unitsym:=usym;
  377. if _u.state=ms_compiled then
  378. begin
  379. checksum:=u.crc;
  380. interface_checksum:=u.interface_crc;
  381. indirect_checksum:=u.indirect_crc;
  382. end
  383. else
  384. begin
  385. checksum:=0;
  386. interface_checksum:=0;
  387. indirect_checksum:=0;
  388. end;
  389. end;
  390. {****************************************************************************
  391. TDENPENDENT_UNIT
  392. ****************************************************************************}
  393. constructor tdependent_unit.create(_u : tmodule);
  394. begin
  395. u:=_u;
  396. end;
  397. {****************************************************************************
  398. TMODULE
  399. ****************************************************************************}
  400. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  401. var
  402. n : string;
  403. begin
  404. n:=ChangeFileExt(ExtractFileName(s),'');
  405. { Programs have the name 'Program' to don't conflict with dup id's }
  406. if _is_unit then
  407. inherited create(n)
  408. else
  409. inherited create('Program');
  410. mainsource:=stringdup(s);
  411. { Dos has the famous 8.3 limit :( }
  412. {$ifdef shortasmprefix}
  413. asmprefix:=stringdup(FixFileName('as'));
  414. {$else}
  415. asmprefix:=stringdup(FixFileName(n));
  416. {$endif}
  417. setfilename(s,true);
  418. localunitsearchpath:=TSearchPathList.Create;
  419. localobjectsearchpath:=TSearchPathList.Create;
  420. localincludesearchpath:=TSearchPathList.Create;
  421. locallibrarysearchpath:=TSearchPathList.Create;
  422. localframeworksearchpath:=TSearchPathList.Create;
  423. used_units:=TLinkedList.Create;
  424. dependent_units:=TLinkedList.Create;
  425. resourcefiles:=TCmdStrList.Create;
  426. linkunitofiles:=TLinkContainer.Create;
  427. linkunitstaticlibs:=TLinkContainer.Create;
  428. linkunitsharedlibs:=TLinkContainer.Create;
  429. linkotherofiles:=TLinkContainer.Create;
  430. linkotherstaticlibs:=TLinkContainer.Create;
  431. linkothersharedlibs:=TLinkContainer.Create;
  432. linkotherframeworks:=TLinkContainer.Create;
  433. mainname:=nil;
  434. FImportLibraryList:=TFPHashObjectList.Create(true);
  435. crc:=0;
  436. interface_crc:=0;
  437. indirect_crc:=0;
  438. flags:=0;
  439. scanner:=nil;
  440. unitmap:=nil;
  441. unitmapsize:=0;
  442. derefmap:=nil;
  443. derefmapsize:=0;
  444. derefmapcnt:=0;
  445. derefdata:=TDynamicArray.Create(1024);
  446. derefdataintflen:=0;
  447. deflist:=TFPObjectList.Create(false);
  448. symlist:=TFPObjectList.Create(false);
  449. wpoinfo:=nil;
  450. checkforwarddefs:=TFPObjectList.Create(false);
  451. globalsymtable:=nil;
  452. localsymtable:=nil;
  453. globalmacrosymtable:=nil;
  454. localmacrosymtable:=nil;
  455. loaded_from:=LoadedFrom;
  456. do_reload:=false;
  457. do_compile:=false;
  458. sources_avail:=true;
  459. mainfilepos.line:=0;
  460. mainfilepos.column:=0;
  461. mainfilepos.fileindex:=0;
  462. recompile_reason:=rr_unknown;
  463. in_interface:=true;
  464. in_global:=true;
  465. is_unit:=_is_unit;
  466. islibrary:=false;
  467. ispackage:=false;
  468. is_dbginfo_written:=false;
  469. mode_switch_allowed:= true;
  470. moduleoptions:=[];
  471. deprecatedmsg:=nil;
  472. _exports:=TLinkedList.Create;
  473. dllscannerinputlist:=TFPHashList.Create;
  474. asmdata:=TAsmData.create(realmodulename^);
  475. InitDebugInfo(self);
  476. end;
  477. destructor tmodule.Destroy;
  478. var
  479. i : longint;
  480. hpi : tprocinfo;
  481. begin
  482. if assigned(unitmap) then
  483. freemem(unitmap);
  484. if assigned(derefmap) then
  485. begin
  486. for i:=0 to derefmapcnt-1 do
  487. stringdispose(derefmap[i].modulename);
  488. freemem(derefmap);
  489. end;
  490. if assigned(_exports) then
  491. _exports.free;
  492. if assigned(dllscannerinputlist) then
  493. dllscannerinputlist.free;
  494. if assigned(scanner) then
  495. begin
  496. { also update current_scanner if it was pointing
  497. to this module }
  498. if current_scanner=tscannerfile(scanner) then
  499. current_scanner:=nil;
  500. tscannerfile(scanner).free;
  501. end;
  502. if assigned(asmdata) then
  503. begin
  504. if current_asmdata=asmdata then
  505. current_asmdata:=nil;
  506. asmdata.free;
  507. end;
  508. if assigned(procinfo) then
  509. begin
  510. if current_procinfo=tprocinfo(procinfo) then
  511. begin
  512. current_procinfo:=nil;
  513. current_structdef:=nil;
  514. current_genericdef:=nil;
  515. current_specializedef:=nil;
  516. end;
  517. { release procinfo tree }
  518. while assigned(procinfo) do
  519. begin
  520. hpi:=tprocinfo(procinfo).parent;
  521. tprocinfo(procinfo).free;
  522. procinfo:=hpi;
  523. end;
  524. end;
  525. DoneDebugInfo(self);
  526. used_units.free;
  527. dependent_units.free;
  528. resourcefiles.Free;
  529. linkunitofiles.Free;
  530. linkunitstaticlibs.Free;
  531. linkunitsharedlibs.Free;
  532. linkotherofiles.Free;
  533. linkotherstaticlibs.Free;
  534. linkothersharedlibs.Free;
  535. linkotherframeworks.Free;
  536. stringdispose(mainname);
  537. FImportLibraryList.Free;
  538. stringdispose(objfilename);
  539. stringdispose(asmfilename);
  540. stringdispose(ppufilename);
  541. stringdispose(importlibfilename);
  542. stringdispose(staticlibfilename);
  543. stringdispose(sharedlibfilename);
  544. stringdispose(exefilename);
  545. stringdispose(outputpath);
  546. stringdispose(path);
  547. stringdispose(realmodulename);
  548. stringdispose(mainsource);
  549. stringdispose(asmprefix);
  550. stringdispose(deprecatedmsg);
  551. localunitsearchpath.Free;
  552. localobjectsearchpath.free;
  553. localincludesearchpath.free;
  554. locallibrarysearchpath.free;
  555. localframeworksearchpath.free;
  556. {$ifdef MEMDEBUG}
  557. memsymtable.start;
  558. {$endif}
  559. derefdata.free;
  560. deflist.free;
  561. symlist.free;
  562. wpoinfo.free;
  563. checkforwarddefs.free;
  564. globalsymtable.free;
  565. localsymtable.free;
  566. globalmacrosymtable.free;
  567. localmacrosymtable.free;
  568. {$ifdef MEMDEBUG}
  569. memsymtable.stop;
  570. {$endif}
  571. stringdispose(modulename);
  572. inherited Destroy;
  573. end;
  574. procedure tmodule.reset;
  575. var
  576. hpi : tprocinfo;
  577. i : longint;
  578. begin
  579. if assigned(scanner) then
  580. begin
  581. { also update current_scanner if it was pointing
  582. to this module }
  583. if current_scanner=tscannerfile(scanner) then
  584. current_scanner:=nil;
  585. tscannerfile(scanner).free;
  586. scanner:=nil;
  587. end;
  588. if assigned(procinfo) then
  589. begin
  590. if current_procinfo=tprocinfo(procinfo) then
  591. begin
  592. current_procinfo:=nil;
  593. current_structdef:=nil;
  594. current_genericdef:=nil;
  595. current_specializedef:=nil;
  596. end;
  597. { release procinfo tree }
  598. while assigned(procinfo) do
  599. begin
  600. hpi:=tprocinfo(procinfo).parent;
  601. tprocinfo(procinfo).free;
  602. procinfo:=hpi;
  603. end;
  604. end;
  605. if assigned(asmdata) then
  606. begin
  607. if current_asmdata=TAsmData(asmdata) then
  608. current_asmdata:=nil;
  609. asmdata.free;
  610. asmdata:=nil;
  611. end;
  612. DoneDebugInfo(self);
  613. globalsymtable.free;
  614. globalsymtable:=nil;
  615. localsymtable.free;
  616. localsymtable:=nil;
  617. globalmacrosymtable.free;
  618. globalmacrosymtable:=nil;
  619. localmacrosymtable.free;
  620. localmacrosymtable:=nil;
  621. deflist.free;
  622. deflist:=TFPObjectList.Create(false);
  623. symlist.free;
  624. symlist:=TFPObjectList.Create(false);
  625. wpoinfo.free;
  626. wpoinfo:=nil;
  627. checkforwarddefs.free;
  628. checkforwarddefs:=TFPObjectList.Create(false);
  629. derefdata.free;
  630. derefdata:=TDynamicArray.Create(1024);
  631. if assigned(unitmap) then
  632. begin
  633. freemem(unitmap);
  634. unitmap:=nil;
  635. end;
  636. if assigned(derefmap) then
  637. begin
  638. for i:=0 to derefmapcnt-1 do
  639. stringdispose(derefmap[i].modulename);
  640. freemem(derefmap);
  641. derefmap:=nil;
  642. end;
  643. unitmapsize:=0;
  644. derefmapsize:=0;
  645. derefmapcnt:=0;
  646. derefdataintflen:=0;
  647. sourcefiles.free;
  648. sourcefiles:=tinputfilemanager.create;
  649. asmdata:=TAsmData.create(realmodulename^);
  650. InitDebugInfo(self);
  651. _exports.free;
  652. _exports:=tlinkedlist.create;
  653. dllscannerinputlist.free;
  654. dllscannerinputlist:=TFPHashList.create;
  655. used_units.free;
  656. used_units:=TLinkedList.Create;
  657. dependent_units.free;
  658. dependent_units:=TLinkedList.Create;
  659. resourcefiles.Free;
  660. resourcefiles:=TCmdStrList.Create;
  661. linkunitofiles.Free;
  662. linkunitofiles:=TLinkContainer.Create;
  663. linkunitstaticlibs.Free;
  664. linkunitstaticlibs:=TLinkContainer.Create;
  665. linkunitsharedlibs.Free;
  666. linkunitsharedlibs:=TLinkContainer.Create;
  667. linkotherofiles.Free;
  668. linkotherofiles:=TLinkContainer.Create;
  669. linkotherstaticlibs.Free;
  670. linkotherstaticlibs:=TLinkContainer.Create;
  671. linkothersharedlibs.Free;
  672. linkothersharedlibs:=TLinkContainer.Create;
  673. linkotherframeworks.Free;
  674. linkotherframeworks:=TLinkContainer.Create;
  675. stringdispose(mainname);
  676. FImportLibraryList.Free;
  677. FImportLibraryList:=TFPHashObjectList.Create;
  678. do_compile:=false;
  679. do_reload:=false;
  680. interface_compiled:=false;
  681. in_interface:=true;
  682. in_global:=true;
  683. mode_switch_allowed:=true;
  684. stringdispose(deprecatedmsg);
  685. moduleoptions:=[];
  686. is_dbginfo_written:=false;
  687. crc:=0;
  688. interface_crc:=0;
  689. indirect_crc:=0;
  690. flags:=0;
  691. mainfilepos.line:=0;
  692. mainfilepos.column:=0;
  693. mainfilepos.fileindex:=0;
  694. recompile_reason:=rr_unknown;
  695. {
  696. The following fields should not
  697. be reset:
  698. mainsource
  699. state
  700. loaded_from
  701. sources_avail
  702. }
  703. end;
  704. procedure tmodule.adddependency(callermodule:tmodule);
  705. begin
  706. { This is not needed for programs }
  707. if not callermodule.is_unit then
  708. exit;
  709. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  710. dependent_units.concat(tdependent_unit.create(callermodule));
  711. end;
  712. procedure tmodule.flagdependent(callermodule:tmodule);
  713. var
  714. pm : tdependent_unit;
  715. begin
  716. { flag all units that depend on this unit for reloading }
  717. pm:=tdependent_unit(current_module.dependent_units.first);
  718. while assigned(pm) do
  719. begin
  720. { We do not have to reload the unit that wants to load
  721. this unit, unless this unit is already compiled during
  722. the loading }
  723. if (pm.u=callermodule) and
  724. (pm.u.state<>ms_compiled) then
  725. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  726. else
  727. if pm.u.state=ms_second_compile then
  728. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  729. else
  730. begin
  731. pm.u.do_reload:=true;
  732. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  733. end;
  734. pm:=tdependent_unit(pm.next);
  735. end;
  736. end;
  737. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  738. var
  739. pu : tused_unit;
  740. begin
  741. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  742. used_units.concat(pu);
  743. addusedunit:=pu;
  744. end;
  745. procedure tmodule.updatemaps;
  746. var
  747. oldmapsize : longint;
  748. hp : tmodule;
  749. i : longint;
  750. begin
  751. { Extend unitmap }
  752. oldmapsize:=unitmapsize;
  753. unitmapsize:=loaded_units.count;
  754. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  755. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  756. { Extend Derefmap }
  757. oldmapsize:=derefmapsize;
  758. derefmapsize:=loaded_units.count;
  759. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  760. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  761. { Add all units to unitmap }
  762. hp:=tmodule(loaded_units.first);
  763. i:=0;
  764. while assigned(hp) do
  765. begin
  766. if hp.moduleid>=unitmapsize then
  767. internalerror(200501151);
  768. { Verify old entries }
  769. if (i<oldmapsize) then
  770. begin
  771. if (hp.moduleid<>i) or
  772. (unitmap[hp.moduleid].u<>hp) then
  773. internalerror(200501156);
  774. end
  775. else
  776. begin
  777. unitmap[hp.moduleid].u:=hp;
  778. unitmap[hp.moduleid].derefidx:=-1;
  779. end;
  780. inc(i);
  781. hp:=tmodule(hp.next);
  782. end;
  783. end;
  784. procedure tmodule.check_hints;
  785. begin
  786. if mo_hint_deprecated in moduleoptions then
  787. if (mo_has_deprecated_msg in moduleoptions) and (deprecatedmsg <> nil) then
  788. Message2(sym_w_deprecated_unit_with_msg,realmodulename^,deprecatedmsg^)
  789. else
  790. Message1(sym_w_deprecated_unit,realmodulename^);
  791. if mo_hint_experimental in moduleoptions then
  792. Message1(sym_w_experimental_unit,realmodulename^);
  793. if mo_hint_platform in moduleoptions then
  794. Message1(sym_w_non_portable_unit,realmodulename^);
  795. if mo_hint_library in moduleoptions then
  796. Message1(sym_w_library_unit,realmodulename^);
  797. if mo_hint_unimplemented in moduleoptions then
  798. Message1(sym_w_non_implemented_unit,realmodulename^);
  799. end;
  800. function tmodule.derefidx_unit(id:longint):longint;
  801. begin
  802. if id>=unitmapsize then
  803. internalerror(2005011511);
  804. if unitmap[id].derefidx=-1 then
  805. begin
  806. unitmap[id].derefidx:=derefmapcnt;
  807. inc(derefmapcnt);
  808. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  809. end;
  810. if unitmap[id].derefidx>=derefmapsize then
  811. internalerror(2005011514);
  812. result:=unitmap[id].derefidx;
  813. end;
  814. function tmodule.resolve_unit(id:longint):tmodule;
  815. var
  816. hp : tmodule;
  817. begin
  818. if id>=derefmapsize then
  819. internalerror(200306231);
  820. result:=derefmap[id].u;
  821. if not assigned(result) then
  822. begin
  823. if not assigned(derefmap[id].modulename) or
  824. (derefmap[id].modulename^='') then
  825. internalerror(200501159);
  826. hp:=tmodule(loaded_units.first);
  827. while assigned(hp) do
  828. begin
  829. { only check for units. The main program is also
  830. as a unit in the loaded_units list. We simply need
  831. to ignore this entry (PFV) }
  832. if hp.is_unit and
  833. (hp.modulename^=derefmap[id].modulename^) then
  834. break;
  835. hp:=tmodule(hp.next);
  836. end;
  837. if not assigned(hp) then
  838. internalerror(2005011510);
  839. derefmap[id].u:=hp;
  840. result:=hp;
  841. end;
  842. end;
  843. procedure tmodule.allunitsused;
  844. var
  845. pu : tused_unit;
  846. begin
  847. pu:=tused_unit(used_units.first);
  848. while assigned(pu) do
  849. begin
  850. if assigned(pu.u.globalsymtable) then
  851. begin
  852. if unitmap[pu.u.moduleid].u<>pu.u then
  853. internalerror(200501157);
  854. { Give a note when the unit is not referenced, skip
  855. this is for units with an initialization/finalization }
  856. if (unitmap[pu.u.moduleid].refs=0) and
  857. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  858. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  859. end;
  860. pu:=tused_unit(pu.next);
  861. end;
  862. end;
  863. procedure tmodule.setmodulename(const s:string);
  864. begin
  865. stringdispose(modulename);
  866. stringdispose(realmodulename);
  867. modulename:=stringdup(upper(s));
  868. realmodulename:=stringdup(s);
  869. { also update asmlibrary names }
  870. current_asmdata.name:=modulename^;
  871. current_asmdata.realname:=realmodulename^;
  872. end;
  873. procedure TModule.AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  874. var
  875. ImportLibrary : TImportLibrary;
  876. ImportSymbol : TFPHashObject;
  877. begin
  878. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  879. if not assigned(ImportLibrary) then
  880. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  881. ImportSymbol:=TFPHashObject(ImportLibrary.ImportSymbolList.Find(symname));
  882. if not assigned(ImportSymbol) then
  883. begin
  884. if not ImportByOrdinalOnly then
  885. { negative ordinal number indicates import by name with ordinal number as hint }
  886. OrdNr:=-OrdNr;
  887. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,symname,OrdNr,isvar);
  888. end;
  889. end;
  890. initialization
  891. {$ifdef MEMDEBUG}
  892. memsymtable:=TMemDebug.create('Symtables');
  893. memsymtable.stop;
  894. {$endif MEMDEBUG}
  895. finalization
  896. {$ifdef MEMDEBUG}
  897. memsymtable.free;
  898. {$endif MEMDEBUG}
  899. end.