fmodule.pas 32 KB

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