fmodule.pas 34 KB

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