fmodule.pas 33 KB

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