fmodule.pas 31 KB

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