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_reset,
  97. is_unit,
  98. in_interface, { processing the implementation part? }
  99. { allow global settings }
  100. in_global : boolean;
  101. { Whether a mode switch is still allowed at this point in the parsing.}
  102. mode_switch_allowed,
  103. { generate pic helper which loads eip in ecx (for leave procedures) }
  104. requires_ecx_pic_helper,
  105. { generate pic helper which loads eip in ebx (for non leave procedures) }
  106. requires_ebx_pic_helper : boolean;
  107. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  108. mainfilepos : tfileposinfo;
  109. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  110. crc,
  111. interface_crc,
  112. indirect_crc : cardinal;
  113. flags : cardinal; { the PPU flags }
  114. islibrary : boolean; { if it is a library (win32 dll) }
  115. IsPackage : boolean;
  116. moduleid : longint;
  117. unitmap : punitmap; { mapping of all used units }
  118. unitmapsize : longint; { number of units in the map }
  119. derefmap : pderefmap; { mapping of all units needed for deref }
  120. derefmapcnt : longint; { number of units in the map }
  121. derefmapsize : longint; { number of units in the map }
  122. derefdataintflen : longint;
  123. derefdata : tdynamicarray;
  124. checkforwarddefs,
  125. deflist,
  126. symlist : TFPObjectList;
  127. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  128. globalsymtable, { pointer to the global symtable of this unit }
  129. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  130. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  131. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  132. scanner : TObject; { scanner object used }
  133. procinfo : TObject; { current procedure being compiled }
  134. asmdata : TObject; { Assembler data }
  135. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  136. debuginfo : TObject;
  137. loaded_from : tmodule;
  138. _exports : tlinkedlist;
  139. dllscannerinputlist : TFPHashList;
  140. resourcefiles : TCmdStrList;
  141. linkunitofiles,
  142. linkunitstaticlibs,
  143. linkunitsharedlibs,
  144. linkotherofiles, { objects,libs loaded from the source }
  145. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  146. linkotherstaticlibs,
  147. linkotherframeworks : tlinkcontainer;
  148. mainname : pshortstring; { alternate name for "main" procedure }
  149. used_units : tlinkedlist;
  150. dependent_units : tlinkedlist;
  151. localunitsearchpath, { local searchpaths }
  152. localobjectsearchpath,
  153. localincludesearchpath,
  154. locallibrarysearchpath,
  155. localframeworksearchpath : TSearchPathList;
  156. moduleoptions: tmoduleoptions;
  157. deprecatedmsg: pshortstring;
  158. {create creates a new module which name is stored in 's'. LoadedFrom
  159. points to the module calling it. It is nil for the first compiled
  160. module. This allow inheritence of all path lists. MUST pay attention
  161. to that when creating link.res!!!!(mazen)}
  162. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  163. destructor destroy;override;
  164. procedure reset;virtual;
  165. procedure adddependency(callermodule:tmodule);
  166. procedure flagdependent(callermodule:tmodule);
  167. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  168. procedure updatemaps;
  169. procedure check_hints;
  170. function derefidx_unit(id:longint):longint;
  171. function resolve_unit(id:longint):tmodule;
  172. procedure allunitsused;
  173. procedure setmodulename(const s:string);
  174. procedure AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  175. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  176. end;
  177. tused_unit = class(tlinkedlistitem)
  178. checksum,
  179. interface_checksum,
  180. indirect_checksum: cardinal;
  181. in_uses,
  182. in_interface : boolean;
  183. u : tmodule;
  184. unitsym : tunitsym;
  185. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  186. end;
  187. tdependent_unit = class(tlinkedlistitem)
  188. u : tmodule;
  189. constructor create(_u : tmodule);
  190. end;
  191. var
  192. main_module : tmodule; { Main module of the program }
  193. current_module : tmodule; { Current module which is compiled or loaded }
  194. compiled_module : tmodule; { Current module which is compiled }
  195. usedunits : tlinkedlist; { Used units for this program }
  196. loaded_units : tlinkedlist; { All loaded units }
  197. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  198. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  199. used to delete them after linking }
  200. procedure set_current_module(p:tmodule);
  201. function get_module(moduleindex : longint) : tmodule;
  202. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  203. procedure addloadedunit(hp:tmodule);
  204. function find_module_from_symtable(st:tsymtable):tmodule;
  205. implementation
  206. uses
  207. SysUtils,globals,
  208. verbose,systems,
  209. scanner,ppu,dbgbase,
  210. procinfo,symdef;
  211. {$ifdef MEMDEBUG}
  212. var
  213. memsymtable : TMemDebug;
  214. {$endif}
  215. {*****************************************************************************
  216. Global Functions
  217. *****************************************************************************}
  218. function find_module_from_symtable(st:tsymtable):tmodule;
  219. var
  220. hp : tmodule;
  221. begin
  222. result:=nil;
  223. hp:=tmodule(loaded_units.first);
  224. while assigned(hp) do
  225. begin
  226. if (hp.globalsymtable=st) or
  227. (hp.localsymtable=st) then
  228. begin
  229. result:=hp;
  230. exit;
  231. end;
  232. hp:=tmodule(hp.next);
  233. end;
  234. end;
  235. procedure set_current_module(p:tmodule);
  236. begin
  237. { save the state of the scanner }
  238. if assigned(current_scanner) then
  239. current_scanner.tempcloseinputfile;
  240. { set new module }
  241. current_module:=p;
  242. { restore previous module settings }
  243. Fillchar(current_filepos,0,sizeof(current_filepos));
  244. if assigned(current_module) then
  245. begin
  246. current_asmdata:=tasmdata(current_module.asmdata);
  247. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  248. { restore scanner and file positions }
  249. current_scanner:=tscannerfile(current_module.scanner);
  250. if assigned(current_scanner) then
  251. begin
  252. current_scanner.tempopeninputfile;
  253. current_scanner.gettokenpos;
  254. parser_current_file:=current_scanner.inputfile.name^;
  255. end
  256. else
  257. begin
  258. current_filepos.moduleindex:=current_module.unit_index;
  259. parser_current_file:='';
  260. end;
  261. end
  262. else
  263. begin
  264. current_asmdata:=nil;
  265. current_scanner:=nil;
  266. current_debuginfo:=nil;
  267. end;
  268. end;
  269. function get_module(moduleindex : longint) : tmodule;
  270. var
  271. hp : tmodule;
  272. begin
  273. result:=nil;
  274. if moduleindex=0 then
  275. exit;
  276. result:=current_module;
  277. if not(assigned(loaded_units)) then
  278. exit;
  279. hp:=tmodule(loaded_units.first);
  280. while assigned(hp) and (hp.unit_index<>moduleindex) do
  281. hp:=tmodule(hp.next);
  282. result:=hp;
  283. end;
  284. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  285. var
  286. hp : tmodule;
  287. begin
  288. hp:=get_module(moduleindex);
  289. if assigned(hp) then
  290. get_source_file:=hp.sourcefiles.get_file(fileindex)
  291. else
  292. get_source_file:=nil;
  293. end;
  294. procedure addloadedunit(hp:tmodule);
  295. begin
  296. hp.moduleid:=loaded_units.count;
  297. loaded_units.concat(hp);
  298. end;
  299. {****************************************************************************
  300. TLinkContainerItem
  301. ****************************************************************************}
  302. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  303. begin
  304. inherited Create;
  305. data:=stringdup(s);
  306. needlink:=m;
  307. end;
  308. destructor TLinkContainerItem.Destroy;
  309. begin
  310. stringdispose(data);
  311. end;
  312. {****************************************************************************
  313. TLinkContainer
  314. ****************************************************************************}
  315. procedure TLinkContainer.add(const s : string;m:cardinal);
  316. begin
  317. inherited concat(TLinkContainerItem.Create(s,m));
  318. end;
  319. function TLinkContainer.get(var m:cardinal) : string;
  320. var
  321. p : tlinkcontaineritem;
  322. begin
  323. p:=tlinkcontaineritem(inherited getfirst);
  324. if p=nil then
  325. begin
  326. get:='';
  327. m:=0;
  328. end
  329. else
  330. begin
  331. get:=p.data^;
  332. m:=p.needlink;
  333. p.free;
  334. end;
  335. end;
  336. function TLinkContainer.getusemask(mask:cardinal) : string;
  337. var
  338. p : tlinkcontaineritem;
  339. found : boolean;
  340. begin
  341. found:=false;
  342. repeat
  343. p:=tlinkcontaineritem(inherited getfirst);
  344. if p=nil then
  345. begin
  346. getusemask:='';
  347. exit;
  348. end;
  349. getusemask:=p.data^;
  350. found:=(p.needlink and mask)<>0;
  351. p.free;
  352. until found;
  353. end;
  354. function TLinkContainer.find(const s:string):boolean;
  355. var
  356. newnode : tlinkcontaineritem;
  357. begin
  358. find:=false;
  359. newnode:=tlinkcontaineritem(First);
  360. while assigned(newnode) do
  361. begin
  362. if newnode.data^=s then
  363. begin
  364. find:=true;
  365. exit;
  366. end;
  367. newnode:=tlinkcontaineritem(newnode.next);
  368. end;
  369. end;
  370. {****************************************************************************
  371. TUSED_UNIT
  372. ****************************************************************************}
  373. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  374. begin
  375. u:=_u;
  376. in_interface:=intface;
  377. in_uses:=inuses;
  378. unitsym:=usym;
  379. if _u.state=ms_compiled then
  380. begin
  381. checksum:=u.crc;
  382. interface_checksum:=u.interface_crc;
  383. indirect_checksum:=u.indirect_crc;
  384. end
  385. else
  386. begin
  387. checksum:=0;
  388. interface_checksum:=0;
  389. indirect_checksum:=0;
  390. end;
  391. end;
  392. {****************************************************************************
  393. TDENPENDENT_UNIT
  394. ****************************************************************************}
  395. constructor tdependent_unit.create(_u : tmodule);
  396. begin
  397. u:=_u;
  398. end;
  399. {****************************************************************************
  400. TMODULE
  401. ****************************************************************************}
  402. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  403. var
  404. n : string;
  405. begin
  406. n:=ChangeFileExt(ExtractFileName(s),'');
  407. { Programs have the name 'Program' to don't conflict with dup id's }
  408. if _is_unit then
  409. inherited create(n)
  410. else
  411. inherited create('Program');
  412. mainsource:=stringdup(s);
  413. { Dos has the famous 8.3 limit :( }
  414. {$ifdef shortasmprefix}
  415. asmprefix:=stringdup(FixFileName('as'));
  416. {$else}
  417. asmprefix:=stringdup(FixFileName(n));
  418. {$endif}
  419. setfilename(s,true);
  420. localunitsearchpath:=TSearchPathList.Create;
  421. localobjectsearchpath:=TSearchPathList.Create;
  422. localincludesearchpath:=TSearchPathList.Create;
  423. locallibrarysearchpath:=TSearchPathList.Create;
  424. localframeworksearchpath:=TSearchPathList.Create;
  425. used_units:=TLinkedList.Create;
  426. dependent_units:=TLinkedList.Create;
  427. resourcefiles:=TCmdStrList.Create;
  428. linkunitofiles:=TLinkContainer.Create;
  429. linkunitstaticlibs:=TLinkContainer.Create;
  430. linkunitsharedlibs:=TLinkContainer.Create;
  431. linkotherofiles:=TLinkContainer.Create;
  432. linkotherstaticlibs:=TLinkContainer.Create;
  433. linkothersharedlibs:=TLinkContainer.Create;
  434. linkotherframeworks:=TLinkContainer.Create;
  435. mainname:=nil;
  436. FImportLibraryList:=TFPHashObjectList.Create(true);
  437. crc:=0;
  438. interface_crc:=0;
  439. indirect_crc:=0;
  440. flags:=0;
  441. scanner:=nil;
  442. unitmap:=nil;
  443. unitmapsize:=0;
  444. derefmap:=nil;
  445. derefmapsize:=0;
  446. derefmapcnt:=0;
  447. derefdata:=TDynamicArray.Create(1024);
  448. derefdataintflen:=0;
  449. deflist:=TFPObjectList.Create(false);
  450. symlist:=TFPObjectList.Create(false);
  451. wpoinfo:=nil;
  452. checkforwarddefs:=TFPObjectList.Create(false);
  453. globalsymtable:=nil;
  454. localsymtable:=nil;
  455. globalmacrosymtable:=nil;
  456. localmacrosymtable:=nil;
  457. loaded_from:=LoadedFrom;
  458. do_reload:=false;
  459. do_compile:=false;
  460. sources_avail:=true;
  461. mainfilepos.line:=0;
  462. mainfilepos.column:=0;
  463. mainfilepos.fileindex:=0;
  464. recompile_reason:=rr_unknown;
  465. in_interface:=true;
  466. in_global:=true;
  467. is_unit:=_is_unit;
  468. islibrary:=false;
  469. ispackage:=false;
  470. is_dbginfo_written:=false;
  471. is_reset:=false;
  472. mode_switch_allowed:= true;
  473. moduleoptions:=[];
  474. deprecatedmsg:=nil;
  475. _exports:=TLinkedList.Create;
  476. dllscannerinputlist:=TFPHashList.Create;
  477. asmdata:=TAsmData.create(realmodulename^);
  478. InitDebugInfo(self);
  479. end;
  480. destructor tmodule.Destroy;
  481. var
  482. i : longint;
  483. hpi : tprocinfo;
  484. begin
  485. if assigned(unitmap) then
  486. freemem(unitmap);
  487. if assigned(derefmap) then
  488. begin
  489. for i:=0 to derefmapcnt-1 do
  490. stringdispose(derefmap[i].modulename);
  491. freemem(derefmap);
  492. end;
  493. if assigned(_exports) then
  494. _exports.free;
  495. if assigned(dllscannerinputlist) then
  496. dllscannerinputlist.free;
  497. if assigned(scanner) then
  498. begin
  499. { also update current_scanner if it was pointing
  500. to this module }
  501. if current_scanner=tscannerfile(scanner) then
  502. current_scanner:=nil;
  503. tscannerfile(scanner).free;
  504. end;
  505. if assigned(asmdata) then
  506. begin
  507. if current_asmdata=asmdata then
  508. current_asmdata:=nil;
  509. asmdata.free;
  510. end;
  511. if assigned(procinfo) then
  512. begin
  513. if current_procinfo=tprocinfo(procinfo) then
  514. begin
  515. current_procinfo:=nil;
  516. current_objectdef:=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. end;
  596. { release procinfo tree }
  597. while assigned(procinfo) do
  598. begin
  599. hpi:=tprocinfo(procinfo).parent;
  600. tprocinfo(procinfo).free;
  601. procinfo:=hpi;
  602. end;
  603. end;
  604. if assigned(asmdata) then
  605. begin
  606. if current_asmdata=TAsmData(asmdata) then
  607. current_asmdata:=nil;
  608. asmdata.free;
  609. asmdata:=nil;
  610. end;
  611. DoneDebugInfo(self);
  612. globalsymtable.free;
  613. globalsymtable:=nil;
  614. localsymtable.free;
  615. localsymtable:=nil;
  616. globalmacrosymtable.free;
  617. globalmacrosymtable:=nil;
  618. localmacrosymtable.free;
  619. localmacrosymtable:=nil;
  620. deflist.free;
  621. deflist:=TFPObjectList.Create(false);
  622. symlist.free;
  623. symlist:=TFPObjectList.Create(false);
  624. wpoinfo.free;
  625. wpoinfo:=nil;
  626. checkforwarddefs.free;
  627. checkforwarddefs:=TFPObjectList.Create(false);
  628. derefdata.free;
  629. derefdata:=TDynamicArray.Create(1024);
  630. if assigned(unitmap) then
  631. begin
  632. freemem(unitmap);
  633. unitmap:=nil;
  634. end;
  635. if assigned(derefmap) then
  636. begin
  637. for i:=0 to derefmapcnt-1 do
  638. stringdispose(derefmap[i].modulename);
  639. freemem(derefmap);
  640. derefmap:=nil;
  641. end;
  642. unitmapsize:=0;
  643. derefmapsize:=0;
  644. derefmapcnt:=0;
  645. derefdataintflen:=0;
  646. sourcefiles.free;
  647. sourcefiles:=tinputfilemanager.create;
  648. asmdata:=TAsmData.create(realmodulename^);
  649. InitDebugInfo(self);
  650. _exports.free;
  651. _exports:=tlinkedlist.create;
  652. dllscannerinputlist.free;
  653. dllscannerinputlist:=TFPHashList.create;
  654. used_units.free;
  655. used_units:=TLinkedList.Create;
  656. dependent_units.free;
  657. dependent_units:=TLinkedList.Create;
  658. resourcefiles.Free;
  659. resourcefiles:=TCmdStrList.Create;
  660. linkunitofiles.Free;
  661. linkunitofiles:=TLinkContainer.Create;
  662. linkunitstaticlibs.Free;
  663. linkunitstaticlibs:=TLinkContainer.Create;
  664. linkunitsharedlibs.Free;
  665. linkunitsharedlibs:=TLinkContainer.Create;
  666. linkotherofiles.Free;
  667. linkotherofiles:=TLinkContainer.Create;
  668. linkotherstaticlibs.Free;
  669. linkotherstaticlibs:=TLinkContainer.Create;
  670. linkothersharedlibs.Free;
  671. linkothersharedlibs:=TLinkContainer.Create;
  672. linkotherframeworks.Free;
  673. linkotherframeworks:=TLinkContainer.Create;
  674. stringdispose(mainname);
  675. FImportLibraryList.Free;
  676. FImportLibraryList:=TFPHashObjectList.Create;
  677. do_compile:=false;
  678. do_reload:=false;
  679. interface_compiled:=false;
  680. in_interface:=true;
  681. in_global:=true;
  682. mode_switch_allowed:=true;
  683. stringdispose(deprecatedmsg);
  684. moduleoptions:=[];
  685. is_dbginfo_written:=false;
  686. is_reset:=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.